]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/init.c
DEREncodingException.java: Remove whitespaces.
[thirdparty/gcc.git] / gcc / cp / init.c
CommitLineData
8d08fdba 1/* Handle initialization things in C++.
d6a8bdff 2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
521cc508 3 1999, 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
8d08fdba
MS
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
f5adbb8d 6This file is part of GCC.
8d08fdba 7
f5adbb8d 8GCC is free software; you can redistribute it and/or modify
8d08fdba
MS
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation; either version 2, or (at your option)
11any later version.
12
f5adbb8d 13GCC is distributed in the hope that it will be useful,
8d08fdba
MS
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
17
18You should have received a copy of the GNU General Public License
f5adbb8d 19along with GCC; see the file COPYING. If not, write to
e9fa0c7c
RK
20the Free Software Foundation, 59 Temple Place - Suite 330,
21Boston, MA 02111-1307, USA. */
8d08fdba 22
e92cc029 23/* High-level class interface. */
8d08fdba
MS
24
25#include "config.h"
8d052bc7 26#include "system.h"
4977bab6
ZW
27#include "coretypes.h"
28#include "tm.h"
8d08fdba
MS
29#include "tree.h"
30#include "rtl.h"
8f17b5c5 31#include "expr.h"
8d08fdba
MS
32#include "cp-tree.h"
33#include "flags.h"
e8abc66f 34#include "output.h"
eb66be0e 35#include "except.h"
54f92bfb 36#include "toplev.h"
46e995e0 37#include "target.h"
8d08fdba 38
2a3398e1
NS
39static bool begin_init_stmts (tree *, tree *);
40static tree finish_init_stmts (bool, tree, tree);
2282d28d 41static void construct_virtual_base (tree, tree);
362efdc1
NN
42static void expand_aggr_init_1 (tree, tree, tree, tree, int);
43static void expand_default_init (tree, tree, tree, tree, int);
44static tree build_vec_delete_1 (tree, tree, tree, special_function_kind, int);
2282d28d 45static void perform_member_init (tree, tree);
362efdc1
NN
46static tree build_builtin_delete_call (tree);
47static int member_init_ok_or_else (tree, tree, tree);
48static void expand_virtual_init (tree, tree);
2282d28d 49static tree sort_mem_initializers (tree, tree);
362efdc1
NN
50static tree initializing_context (tree);
51static void expand_cleanup_for_base (tree, tree);
52static tree get_temp_regvar (tree, tree);
53static tree dfs_initialize_vtbl_ptrs (tree, void *);
54static tree build_default_init (tree, tree);
55static tree build_new_1 (tree);
362efdc1
NN
56static tree build_dtor_call (tree, special_function_kind, int);
57static tree build_field_list (tree, tree, int *);
58static tree build_vtbl_address (tree);
8d08fdba 59
3dbc07b6
MM
60/* We are about to generate some complex initialization code.
61 Conceptually, it is all a single expression. However, we may want
62 to include conditionals, loops, and other such statement-level
63 constructs. Therefore, we build the initialization code inside a
64 statement-expression. This function starts such an expression.
65 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
66 pass them back to finish_init_stmts when the expression is
67 complete. */
68
2a3398e1 69static bool
362efdc1 70begin_init_stmts (tree *stmt_expr_p, tree *compound_stmt_p)
3dbc07b6 71{
2a3398e1 72 bool is_global = !building_stmt_tree ();
35b1567d 73
2a3398e1 74 *stmt_expr_p = begin_stmt_expr ();
325c3691 75 *compound_stmt_p = begin_compound_stmt (BCS_NO_SCOPE);
2a3398e1
NS
76
77 return is_global;
3dbc07b6
MM
78}
79
80/* Finish out the statement-expression begun by the previous call to
81 begin_init_stmts. Returns the statement-expression itself. */
82
2a3398e1
NS
83static tree
84finish_init_stmts (bool is_global, tree stmt_expr, tree compound_stmt)
35b1567d 85{
7a3397c7 86 finish_compound_stmt (compound_stmt);
35b1567d 87
303b7406 88 stmt_expr = finish_stmt_expr (stmt_expr, true);
3dbc07b6 89
50bc768d 90 gcc_assert (!building_stmt_tree () == is_global);
2a3398e1 91
3dbc07b6
MM
92 return stmt_expr;
93}
94
95/* Constructors */
96
338d90b8
NS
97/* Called from initialize_vtbl_ptrs via dfs_walk. BINFO is the base
98 which we want to initialize the vtable pointer for, DATA is
99 TREE_LIST whose TREE_VALUE is the this ptr expression. */
7177d104 100
d569399b 101static tree
362efdc1 102dfs_initialize_vtbl_ptrs (tree binfo, void *data)
d569399b 103{
809e3e7f 104 if ((!BINFO_PRIMARY_P (binfo) || BINFO_VIRTUAL_P (binfo))
bcb1079e 105 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
d569399b
MM
106 {
107 tree base_ptr = TREE_VALUE ((tree) data);
7177d104 108
338d90b8 109 base_ptr = build_base_path (PLUS_EXPR, base_ptr, binfo, /*nonnull=*/1);
d569399b
MM
110
111 expand_virtual_init (binfo, base_ptr);
112 }
7177d104 113
dbbf88d1 114 BINFO_MARKED (binfo) = 1;
d569399b
MM
115
116 return NULL_TREE;
117}
118
cf2e003b
MM
119/* Initialize all the vtable pointers in the object pointed to by
120 ADDR. */
e92cc029 121
8d08fdba 122void
362efdc1 123initialize_vtbl_ptrs (tree addr)
8d08fdba 124{
cf2e003b
MM
125 tree list;
126 tree type;
127
128 type = TREE_TYPE (TREE_TYPE (addr));
129 list = build_tree_list (type, addr);
d569399b 130
bbd15aac 131 /* Walk through the hierarchy, initializing the vptr in each base
1f5a253a 132 class. We do these in pre-order because we can't find the virtual
3461fba7
NS
133 bases for a class until we've initialized the vtbl for that
134 class. */
dbbf88d1
NS
135 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
136 NULL, unmarkedp, list);
137 dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type);
8d08fdba 138}
d569399b 139
17bbb839
MM
140/* Return an expression for the zero-initialization of an object with
141 type T. This expression will either be a constant (in the case
142 that T is a scalar), or a CONSTRUCTOR (in the case that T is an
143 aggregate). In either case, the value can be used as DECL_INITIAL
144 for a decl of the indicated TYPE; it is a valid static initializer.
1cb8292f
MM
145 If NELTS is non-NULL, and TYPE is an ARRAY_TYPE, NELTS is the
146 number of elements in the array. If STATIC_STORAGE_P is TRUE,
147 initializers are only generated for entities for which
148 zero-initialization does not simply mean filling the storage with
149 zero bytes. */
94e6e4c4
AO
150
151tree
1cb8292f 152build_zero_init (tree type, tree nelts, bool static_storage_p)
94e6e4c4 153{
17bbb839
MM
154 tree init = NULL_TREE;
155
156 /* [dcl.init]
157
158 To zero-initialization storage for an object of type T means:
159
160 -- if T is a scalar type, the storage is set to the value of zero
161 converted to T.
162
163 -- if T is a non-union class type, the storage for each nonstatic
164 data member and each base-class subobject is zero-initialized.
165
166 -- if T is a union type, the storage for its first data member is
167 zero-initialized.
168
169 -- if T is an array type, the storage for each element is
170 zero-initialized.
171
172 -- if T is a reference type, no initialization is performed. */
94e6e4c4 173
50bc768d 174 gcc_assert (nelts == NULL_TREE || TREE_CODE (nelts) == INTEGER_CST);
7a1d37e9 175
17bbb839
MM
176 if (type == error_mark_node)
177 ;
178 else if (static_storage_p && zero_init_p (type))
179 /* In order to save space, we do not explicitly build initializers
180 for items that do not need them. GCC's semantics are that
181 items with static storage duration that are not otherwise
182 initialized are initialized to zero. */
183 ;
184 else if (SCALAR_TYPE_P (type))
185 init = convert (type, integer_zero_node);
186 else if (CLASS_TYPE_P (type))
187 {
188 tree field;
189 tree inits;
190
191 /* Build a constructor to contain the initializations. */
dcf92453 192 init = build_constructor (type, NULL_TREE);
17bbb839
MM
193 /* Iterate over the fields, building initializations. */
194 inits = NULL_TREE;
195 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
196 {
197 if (TREE_CODE (field) != FIELD_DECL)
198 continue;
199
200 /* Note that for class types there will be FIELD_DECLs
201 corresponding to base classes as well. Thus, iterating
202 over TYPE_FIELDs will result in correct initialization of
203 all of the subobjects. */
204 if (static_storage_p && !zero_init_p (TREE_TYPE (field)))
205 inits = tree_cons (field,
206 build_zero_init (TREE_TYPE (field),
1cb8292f 207 /*nelts=*/NULL_TREE,
17bbb839
MM
208 static_storage_p),
209 inits);
210
211 /* For unions, only the first field is initialized. */
212 if (TREE_CODE (type) == UNION_TYPE)
213 break;
214 }
215 CONSTRUCTOR_ELTS (init) = nreverse (inits);
216 }
217 else if (TREE_CODE (type) == ARRAY_TYPE)
94e6e4c4 218 {
17bbb839
MM
219 tree index;
220 tree max_index;
221 tree inits;
222
223 /* Build a constructor to contain the initializations. */
dcf92453 224 init = build_constructor (type, NULL_TREE);
17bbb839
MM
225 /* Iterate over the array elements, building initializations. */
226 inits = NULL_TREE;
1cb8292f 227 max_index = nelts ? nelts : array_type_nelts (type);
50bc768d 228 gcc_assert (TREE_CODE (max_index) == INTEGER_CST);
7a1d37e9 229
a8e6c82a
MM
230 /* A zero-sized array, which is accepted as an extension, will
231 have an upper bound of -1. */
232 if (!tree_int_cst_equal (max_index, integer_minus_one_node))
233 for (index = size_zero_node;
234 !tree_int_cst_lt (max_index, index);
235 index = size_binop (PLUS_EXPR, index, size_one_node))
236 inits = tree_cons (index,
237 build_zero_init (TREE_TYPE (type),
238 /*nelts=*/NULL_TREE,
239 static_storage_p),
240 inits);
17bbb839 241 CONSTRUCTOR_ELTS (init) = nreverse (inits);
94e6e4c4
AO
242 }
243 else if (TREE_CODE (type) == REFERENCE_TYPE)
17bbb839 244 ;
94e6e4c4 245 else
17bbb839 246 abort ();
94e6e4c4 247
17bbb839
MM
248 /* In all cases, the initializer is a constant. */
249 if (init)
6de9cd9a
DN
250 {
251 TREE_CONSTANT (init) = 1;
252 TREE_INVARIANT (init) = 1;
253 }
94e6e4c4
AO
254
255 return init;
256}
257
1cb8292f
MM
258/* Build an expression for the default-initialization of an object of
259 the indicated TYPE. If NELTS is non-NULL, and TYPE is an
260 ARRAY_TYPE, NELTS is the number of elements in the array. If
261 initialization of TYPE requires calling constructors, this function
262 returns NULL_TREE; the caller is responsible for arranging for the
263 constructors to be called. */
f30efcb7 264
17bbb839 265static tree
362efdc1 266build_default_init (tree type, tree nelts)
17bbb839
MM
267{
268 /* [dcl.init]:
f30efcb7 269
17bbb839 270 To default-initialize an object of type T means:
f30efcb7 271
17bbb839
MM
272 --if T is a non-POD class type (clause _class_), the default construc-
273 tor for T is called (and the initialization is ill-formed if T has
274 no accessible default constructor);
f30efcb7 275
17bbb839 276 --if T is an array type, each element is default-initialized;
f30efcb7 277
17bbb839 278 --otherwise, the storage for the object is zero-initialized.
f30efcb7 279
17bbb839
MM
280 A program that calls for default-initialization of an entity of refer-
281 ence type is ill-formed. */
282
283 /* If TYPE_NEEDS_CONSTRUCTING is true, the caller is responsible for
284 performing the initialization. This is confusing in that some
285 non-PODs do not have TYPE_NEEDS_CONSTRUCTING set. (For example,
286 a class with a pointer-to-data member as a non-static data member
287 does not have TYPE_NEEDS_CONSTRUCTING set.) Therefore, we end up
288 passing non-PODs to build_zero_init below, which is contrary to
289 the semantics quoted above from [dcl.init].
290
291 It happens, however, that the behavior of the constructor the
292 standard says we should have generated would be precisely the
293 same as that obtained by calling build_zero_init below, so things
294 work out OK. */
7a1d37e9
MA
295 if (TYPE_NEEDS_CONSTRUCTING (type)
296 || (nelts && TREE_CODE (nelts) != INTEGER_CST))
f30efcb7 297 return NULL_TREE;
17bbb839
MM
298
299 /* At this point, TYPE is either a POD class type, an array of POD
cd0be382 300 classes, or something even more innocuous. */
1cb8292f 301 return build_zero_init (type, nelts, /*static_storage_p=*/false);
f30efcb7
JM
302}
303
2282d28d
MM
304/* Initialize MEMBER, a FIELD_DECL, with INIT, a TREE_LIST of
305 arguments. If TREE_LIST is void_type_node, an empty initializer
306 list was given; if NULL_TREE no initializer was given. */
e92cc029 307
8d08fdba 308static void
2282d28d 309perform_member_init (tree member, tree init)
8d08fdba
MS
310{
311 tree decl;
312 tree type = TREE_TYPE (member);
2282d28d 313 bool explicit;
eb66be0e 314
2282d28d
MM
315 explicit = (init != NULL_TREE);
316
317 /* Effective C++ rule 12 requires that all data members be
318 initialized. */
319 if (warn_ecpp && !explicit && TREE_CODE (type) != ARRAY_TYPE)
320 warning ("`%D' should be initialized in the member initialization "
321 "list",
322 member);
323
324 if (init == void_type_node)
325 init = NULL_TREE;
326
327 /* Get an lvalue for the data member. */
50ad9642
MM
328 decl = build_class_member_access_expr (current_class_ref, member,
329 /*access_path=*/NULL_TREE,
330 /*preserve_reference=*/true);
2fbfe9b8
MS
331 if (decl == error_mark_node)
332 return;
333
6bdb8141
JM
334 /* Deal with this here, as we will get confused if we try to call the
335 assignment op for an anonymous union. This can happen in a
336 synthesized copy constructor. */
337 if (ANON_AGGR_TYPE_P (type))
338 {
ff9f1a5d
MM
339 if (init)
340 {
f293ce4b 341 init = build2 (INIT_EXPR, type, decl, TREE_VALUE (init));
ff9f1a5d
MM
342 finish_expr_stmt (init);
343 }
6bdb8141 344 }
92a62aad 345 else if (TYPE_NEEDS_CONSTRUCTING (type))
8d08fdba 346 {
8d08fdba
MS
347 if (explicit
348 && TREE_CODE (type) == ARRAY_TYPE
349 && init != NULL_TREE
350 && TREE_CHAIN (init) == NULL_TREE
351 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
352 {
353 /* Initialization of one array from another. */
a48cccea
JM
354 finish_expr_stmt (build_vec_init (decl, NULL_TREE, TREE_VALUE (init),
355 /* from_array=*/1));
8d08fdba
MS
356 }
357 else
f1dedc31 358 finish_expr_stmt (build_aggr_init (decl, init, 0));
8d08fdba
MS
359 }
360 else
361 {
362 if (init == NULL_TREE)
363 {
364 if (explicit)
365 {
1cb8292f 366 init = build_default_init (type, /*nelts=*/NULL_TREE);
f30efcb7 367 if (TREE_CODE (type) == REFERENCE_TYPE)
33bd39a2 368 warning
f30efcb7
JM
369 ("default-initialization of `%#D', which has reference type",
370 member);
8d08fdba
MS
371 }
372 /* member traversal: note it leaves init NULL */
f30efcb7 373 else if (TREE_CODE (type) == REFERENCE_TYPE)
33bd39a2 374 pedwarn ("uninitialized reference member `%D'", member);
58ec3cc5 375 else if (CP_TYPE_CONST_P (type))
f3dfc177 376 pedwarn ("uninitialized member `%D' with `const' type `%T'",
58ec3cc5 377 member, type);
8d08fdba
MS
378 }
379 else if (TREE_CODE (init) == TREE_LIST)
c7b62f14
NS
380 /* There was an explicit member initialization. Do some work
381 in that case. */
382 init = build_x_compound_expr_from_list (init, "member initializer");
8d08fdba 383
4f0aa416 384 if (init)
f1dedc31 385 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
8d08fdba 386 }
eb66be0e 387
834c6dff 388 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
b7484fbe 389 {
de22184b
MS
390 tree expr;
391
50ad9642
MM
392 expr = build_class_member_access_expr (current_class_ref, member,
393 /*access_path=*/NULL_TREE,
394 /*preserve_reference=*/false);
3ec6bad3 395 expr = build_delete (type, expr, sfk_complete_destructor,
b7484fbe
MS
396 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
397
398 if (expr != error_mark_node)
659e5a7a 399 finish_eh_cleanup (expr);
b7484fbe 400 }
8d08fdba
MS
401}
402
ff9f1a5d
MM
403/* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
404 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
405
406static tree
362efdc1 407build_field_list (tree t, tree list, int *uses_unions_p)
ff9f1a5d
MM
408{
409 tree fields;
410
01c3fb15
JM
411 *uses_unions_p = 0;
412
ff9f1a5d
MM
413 /* Note whether or not T is a union. */
414 if (TREE_CODE (t) == UNION_TYPE)
415 *uses_unions_p = 1;
416
417 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
418 {
419 /* Skip CONST_DECLs for enumeration constants and so forth. */
17bbb839 420 if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
ff9f1a5d
MM
421 continue;
422
423 /* Keep track of whether or not any fields are unions. */
424 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
425 *uses_unions_p = 1;
426
427 /* For an anonymous struct or union, we must recursively
428 consider the fields of the anonymous type. They can be
429 directly initialized from the constructor. */
430 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
431 {
432 /* Add this field itself. Synthesized copy constructors
433 initialize the entire aggregate. */
434 list = tree_cons (fields, NULL_TREE, list);
435 /* And now add the fields in the anonymous aggregate. */
436 list = build_field_list (TREE_TYPE (fields), list,
437 uses_unions_p);
438 }
439 /* Add this field. */
440 else if (DECL_NAME (fields))
441 list = tree_cons (fields, NULL_TREE, list);
442 }
443
444 return list;
445}
446
2282d28d
MM
447/* The MEM_INITS are a TREE_LIST. The TREE_PURPOSE of each list gives
448 a FIELD_DECL or BINFO in T that needs initialization. The
449 TREE_VALUE gives the initializer, or list of initializer arguments.
450
451 Return a TREE_LIST containing all of the initializations required
452 for T, in the order in which they should be performed. The output
453 list has the same format as the input. */
e92cc029 454
8d08fdba 455static tree
2282d28d 456sort_mem_initializers (tree t, tree mem_inits)
8d08fdba 457{
ff9f1a5d 458 tree init;
fa743e8c 459 tree base, binfo, base_binfo;
2282d28d
MM
460 tree sorted_inits;
461 tree next_subobject;
9ba5ff0f 462 VEC (tree) *vbases;
2282d28d 463 int i;
ff9f1a5d
MM
464 int uses_unions_p;
465
2282d28d
MM
466 /* Build up a list of initializations. The TREE_PURPOSE of entry
467 will be the subobject (a FIELD_DECL or BINFO) to initialize. The
468 TREE_VALUE will be the constructor arguments, or NULL if no
469 explicit initialization was provided. */
470 sorted_inits = NULL_TREE;
58c42dc2 471
2282d28d 472 /* Process the virtual bases. */
9ba5ff0f
NS
473 for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
474 VEC_iterate (tree, vbases, i, base); i++)
58c42dc2
NS
475 sorted_inits = tree_cons (base, NULL_TREE, sorted_inits);
476
2282d28d 477 /* Process the direct bases. */
fa743e8c
NS
478 for (binfo = TYPE_BINFO (t), i = 0;
479 BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
480 if (!BINFO_VIRTUAL_P (base_binfo))
481 sorted_inits = tree_cons (base_binfo, NULL_TREE, sorted_inits);
482
2282d28d
MM
483 /* Process the non-static data members. */
484 sorted_inits = build_field_list (t, sorted_inits, &uses_unions_p);
485 /* Reverse the entire list of initializations, so that they are in
486 the order that they will actually be performed. */
487 sorted_inits = nreverse (sorted_inits);
488
489 /* If the user presented the initializers in an order different from
490 that in which they will actually occur, we issue a warning. Keep
491 track of the next subobject which can be explicitly initialized
492 without issuing a warning. */
493 next_subobject = sorted_inits;
494
495 /* Go through the explicit initializers, filling in TREE_PURPOSE in
496 the SORTED_INITS. */
497 for (init = mem_inits; init; init = TREE_CHAIN (init))
498 {
499 tree subobject;
500 tree subobject_init;
501
502 subobject = TREE_PURPOSE (init);
503
504 /* If the explicit initializers are in sorted order, then
505 SUBOBJECT will be NEXT_SUBOBJECT, or something following
506 it. */
507 for (subobject_init = next_subobject;
508 subobject_init;
509 subobject_init = TREE_CHAIN (subobject_init))
510 if (TREE_PURPOSE (subobject_init) == subobject)
ff9f1a5d
MM
511 break;
512
2282d28d
MM
513 /* Issue a warning if the explicit initializer order does not
514 match that which will actually occur. */
515 if (warn_reorder && !subobject_init)
ff9f1a5d 516 {
2282d28d
MM
517 if (TREE_CODE (TREE_PURPOSE (next_subobject)) == FIELD_DECL)
518 cp_warning_at ("`%D' will be initialized after",
519 TREE_PURPOSE (next_subobject));
520 else
521 warning ("base `%T' will be initialized after",
522 TREE_PURPOSE (next_subobject));
523 if (TREE_CODE (subobject) == FIELD_DECL)
524 cp_warning_at (" `%#D'", subobject);
525 else
526 warning (" base `%T'", subobject);
d9dd2c4e 527 warning (" when initialized here");
ff9f1a5d 528 }
b7484fbe 529
2282d28d
MM
530 /* Look again, from the beginning of the list. */
531 if (!subobject_init)
ff9f1a5d 532 {
2282d28d
MM
533 subobject_init = sorted_inits;
534 while (TREE_PURPOSE (subobject_init) != subobject)
535 subobject_init = TREE_CHAIN (subobject_init);
ff9f1a5d 536 }
2282d28d
MM
537
538 /* It is invalid to initialize the same subobject more than
539 once. */
540 if (TREE_VALUE (subobject_init))
ff9f1a5d 541 {
2282d28d
MM
542 if (TREE_CODE (subobject) == FIELD_DECL)
543 error ("multiple initializations given for `%D'", subobject);
544 else
545 error ("multiple initializations given for base `%T'",
546 subobject);
ff9f1a5d
MM
547 }
548
2282d28d
MM
549 /* Record the initialization. */
550 TREE_VALUE (subobject_init) = TREE_VALUE (init);
551 next_subobject = subobject_init;
ff9f1a5d
MM
552 }
553
554 /* [class.base.init]
b7484fbe 555
ff9f1a5d
MM
556 If a ctor-initializer specifies more than one mem-initializer for
557 multiple members of the same union (including members of
558 anonymous unions), the ctor-initializer is ill-formed. */
559 if (uses_unions_p)
560 {
2282d28d
MM
561 tree last_field = NULL_TREE;
562 for (init = sorted_inits; init; init = TREE_CHAIN (init))
8d08fdba 563 {
ff9f1a5d
MM
564 tree field;
565 tree field_type;
566 int done;
567
2282d28d
MM
568 /* Skip uninitialized members and base classes. */
569 if (!TREE_VALUE (init)
570 || TREE_CODE (TREE_PURPOSE (init)) != FIELD_DECL)
ff9f1a5d
MM
571 continue;
572 /* See if this field is a member of a union, or a member of a
573 structure contained in a union, etc. */
574 field = TREE_PURPOSE (init);
575 for (field_type = DECL_CONTEXT (field);
576 !same_type_p (field_type, t);
577 field_type = TYPE_CONTEXT (field_type))
578 if (TREE_CODE (field_type) == UNION_TYPE)
579 break;
580 /* If this field is not a member of a union, skip it. */
581 if (TREE_CODE (field_type) != UNION_TYPE)
8d08fdba 582 continue;
8d08fdba 583
ff9f1a5d
MM
584 /* It's only an error if we have two initializers for the same
585 union type. */
586 if (!last_field)
6bdb8141 587 {
ff9f1a5d
MM
588 last_field = field;
589 continue;
6bdb8141 590 }
8d08fdba 591
ff9f1a5d
MM
592 /* See if LAST_FIELD and the field initialized by INIT are
593 members of the same union. If so, there's a problem,
594 unless they're actually members of the same structure
595 which is itself a member of a union. For example, given:
8d08fdba 596
ff9f1a5d
MM
597 union { struct { int i; int j; }; };
598
599 initializing both `i' and `j' makes sense. */
600 field_type = DECL_CONTEXT (field);
601 done = 0;
602 do
8d08fdba 603 {
ff9f1a5d
MM
604 tree last_field_type;
605
606 last_field_type = DECL_CONTEXT (last_field);
607 while (1)
00595019 608 {
ff9f1a5d 609 if (same_type_p (last_field_type, field_type))
00595019 610 {
ff9f1a5d 611 if (TREE_CODE (field_type) == UNION_TYPE)
33bd39a2 612 error ("initializations for multiple members of `%T'",
ff9f1a5d
MM
613 last_field_type);
614 done = 1;
615 break;
00595019 616 }
8d08fdba 617
ff9f1a5d
MM
618 if (same_type_p (last_field_type, t))
619 break;
8d08fdba 620
ff9f1a5d
MM
621 last_field_type = TYPE_CONTEXT (last_field_type);
622 }
623
624 /* If we've reached the outermost class, then we're
625 done. */
626 if (same_type_p (field_type, t))
627 break;
8d08fdba 628
ff9f1a5d 629 field_type = TYPE_CONTEXT (field_type);
8d08fdba 630 }
ff9f1a5d
MM
631 while (!done);
632
633 last_field = field;
b7484fbe
MS
634 }
635 }
8d08fdba 636
2282d28d 637 return sorted_inits;
b7484fbe
MS
638}
639
2282d28d
MM
640/* Initialize all bases and members of CURRENT_CLASS_TYPE. MEM_INITS
641 is a TREE_LIST giving the explicit mem-initializer-list for the
642 constructor. The TREE_PURPOSE of each entry is a subobject (a
643 FIELD_DECL or a BINFO) of the CURRENT_CLASS_TYPE. The TREE_VALUE
644 is a TREE_LIST giving the arguments to the constructor or
645 void_type_node for an empty list of arguments. */
a9aedbc2 646
3dbc07b6 647void
2282d28d 648emit_mem_initializers (tree mem_inits)
8d08fdba 649{
2282d28d
MM
650 /* Sort the mem-initializers into the order in which the
651 initializations should be performed. */
652 mem_inits = sort_mem_initializers (current_class_type, mem_inits);
8d08fdba 653
1f5a253a
NS
654 in_base_initializer = 1;
655
2282d28d
MM
656 /* Initialize base classes. */
657 while (mem_inits
658 && TREE_CODE (TREE_PURPOSE (mem_inits)) != FIELD_DECL)
8d08fdba 659 {
2282d28d
MM
660 tree subobject = TREE_PURPOSE (mem_inits);
661 tree arguments = TREE_VALUE (mem_inits);
662
663 /* If these initializations are taking place in a copy
664 constructor, the base class should probably be explicitly
665 initialized. */
666 if (extra_warnings && !arguments
667 && DECL_COPY_CONSTRUCTOR_P (current_function_decl)
668 && TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (subobject)))
669 warning ("base class `%#T' should be explicitly initialized in the "
670 "copy constructor",
671 BINFO_TYPE (subobject));
672
673 /* If an explicit -- but empty -- initializer list was present,
674 treat it just like default initialization at this point. */
675 if (arguments == void_type_node)
676 arguments = NULL_TREE;
677
678 /* Initialize the base. */
809e3e7f 679 if (BINFO_VIRTUAL_P (subobject))
2282d28d
MM
680 construct_virtual_base (subobject, arguments);
681 else
b7484fbe 682 {
2282d28d
MM
683 tree base_addr;
684
685 base_addr = build_base_path (PLUS_EXPR, current_class_ptr,
686 subobject, 1);
687 expand_aggr_init_1 (subobject, NULL_TREE,
688 build_indirect_ref (base_addr, NULL),
689 arguments,
b370501f 690 LOOKUP_NORMAL);
2282d28d 691 expand_cleanup_for_base (subobject, NULL_TREE);
8d08fdba 692 }
8d08fdba 693
2282d28d 694 mem_inits = TREE_CHAIN (mem_inits);
8d08fdba 695 }
1f5a253a 696 in_base_initializer = 0;
8d08fdba 697
2282d28d 698 /* Initialize the vptrs. */
cf2e003b 699 initialize_vtbl_ptrs (current_class_ptr);
1f5a253a 700
2282d28d
MM
701 /* Initialize the data members. */
702 while (mem_inits)
8d08fdba 703 {
2282d28d
MM
704 perform_member_init (TREE_PURPOSE (mem_inits),
705 TREE_VALUE (mem_inits));
706 mem_inits = TREE_CHAIN (mem_inits);
b7484fbe 707 }
8d08fdba
MS
708}
709
3ec6bad3
MM
710/* Returns the address of the vtable (i.e., the value that should be
711 assigned to the vptr) for BINFO. */
712
713static tree
362efdc1 714build_vtbl_address (tree binfo)
3ec6bad3 715{
9965d119 716 tree binfo_for = binfo;
3ec6bad3
MM
717 tree vtbl;
718
fc6633e0 719 if (BINFO_VPTR_INDEX (binfo) && BINFO_VIRTUAL_P (binfo))
9965d119
NS
720 /* If this is a virtual primary base, then the vtable we want to store
721 is that for the base this is being used as the primary base of. We
722 can't simply skip the initialization, because we may be expanding the
723 inits of a subobject constructor where the virtual base layout
724 can be different. */
fc6633e0
NS
725 while (BINFO_PRIMARY_P (binfo_for))
726 binfo_for = BINFO_INHERITANCE_CHAIN (binfo_for);
9965d119 727
3ec6bad3
MM
728 /* Figure out what vtable BINFO's vtable is based on, and mark it as
729 used. */
9965d119 730 vtbl = get_vtbl_decl_for_binfo (binfo_for);
3ec6bad3
MM
731 assemble_external (vtbl);
732 TREE_USED (vtbl) = 1;
733
734 /* Now compute the address to use when initializing the vptr. */
6de9cd9a 735 vtbl = unshare_expr (BINFO_VTABLE (binfo_for));
3ec6bad3 736 if (TREE_CODE (vtbl) == VAR_DECL)
6de9cd9a 737 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
3ec6bad3
MM
738
739 return vtbl;
740}
741
8d08fdba
MS
742/* This code sets up the virtual function tables appropriate for
743 the pointer DECL. It is a one-ply initialization.
744
745 BINFO is the exact type that DECL is supposed to be. In
746 multiple inheritance, this might mean "C's A" if C : A, B. */
e92cc029 747
8926095f 748static void
362efdc1 749expand_virtual_init (tree binfo, tree decl)
8d08fdba 750{
8d08fdba 751 tree vtbl, vtbl_ptr;
3ec6bad3 752 tree vtt_index;
8d08fdba 753
3ec6bad3
MM
754 /* Compute the initializer for vptr. */
755 vtbl = build_vtbl_address (binfo);
756
3461fba7
NS
757 /* We may get this vptr from a VTT, if this is a subobject
758 constructor or subobject destructor. */
3ec6bad3
MM
759 vtt_index = BINFO_VPTR_INDEX (binfo);
760 if (vtt_index)
761 {
762 tree vtbl2;
763 tree vtt_parm;
764
765 /* Compute the value to use, when there's a VTT. */
e0fff4b3 766 vtt_parm = current_vtt_parm;
f293ce4b
RS
767 vtbl2 = build2 (PLUS_EXPR,
768 TREE_TYPE (vtt_parm),
769 vtt_parm,
770 vtt_index);
6de9cd9a
DN
771 vtbl2 = build_indirect_ref (vtbl2, NULL);
772 vtbl2 = convert (TREE_TYPE (vtbl), vtbl2);
3ec6bad3
MM
773
774 /* The actual initializer is the VTT value only in the subobject
775 constructor. In maybe_clone_body we'll substitute NULL for
776 the vtt_parm in the case of the non-subobject constructor. */
f293ce4b
RS
777 vtbl = build3 (COND_EXPR,
778 TREE_TYPE (vtbl),
779 build2 (EQ_EXPR, boolean_type_node,
780 current_in_charge_parm, integer_zero_node),
781 vtbl2,
782 vtbl);
3ec6bad3 783 }
70ae3201
MM
784
785 /* Compute the location of the vtpr. */
338d90b8
NS
786 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL),
787 TREE_TYPE (binfo));
50bc768d 788 gcc_assert (vtbl_ptr != error_mark_node);
8d08fdba 789
70ae3201 790 /* Assign the vtable to the vptr. */
6060a796 791 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
f1dedc31 792 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
8d08fdba
MS
793}
794
f33e32a8
MM
795/* If an exception is thrown in a constructor, those base classes already
796 constructed must be destroyed. This function creates the cleanup
0b8a1e58 797 for BINFO, which has just been constructed. If FLAG is non-NULL,
838dfd8a 798 it is a DECL which is nonzero when this base needs to be
0b8a1e58 799 destroyed. */
f33e32a8
MM
800
801static void
362efdc1 802expand_cleanup_for_base (tree binfo, tree flag)
f33e32a8
MM
803{
804 tree expr;
805
834c6dff 806 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
f33e32a8
MM
807 return;
808
0b8a1e58 809 /* Call the destructor. */
4ba126e4
MM
810 expr = build_special_member_call (current_class_ref,
811 base_dtor_identifier,
812 NULL_TREE,
813 binfo,
814 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
0b8a1e58 815 if (flag)
f293ce4b
RS
816 expr = fold (build3 (COND_EXPR, void_type_node,
817 c_common_truthvalue_conversion (flag),
818 expr, integer_zero_node));
0b8a1e58 819
659e5a7a 820 finish_eh_cleanup (expr);
f33e32a8
MM
821}
822
2282d28d
MM
823/* Construct the virtual base-class VBASE passing the ARGUMENTS to its
824 constructor. */
e92cc029 825
8d08fdba 826static void
2282d28d 827construct_virtual_base (tree vbase, tree arguments)
8d08fdba 828{
2282d28d 829 tree inner_if_stmt;
2282d28d
MM
830 tree exp;
831 tree flag;
832
833 /* If there are virtual base classes with destructors, we need to
834 emit cleanups to destroy them if an exception is thrown during
835 the construction process. These exception regions (i.e., the
836 period during which the cleanups must occur) begin from the time
837 the construction is complete to the end of the function. If we
838 create a conditional block in which to initialize the
839 base-classes, then the cleanup region for the virtual base begins
840 inside a block, and ends outside of that block. This situation
841 confuses the sjlj exception-handling code. Therefore, we do not
842 create a single conditional block, but one for each
843 initialization. (That way the cleanup regions always begin
844 in the outer block.) We trust the back-end to figure out
845 that the FLAG will not change across initializations, and
846 avoid doing multiple tests. */
847 flag = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
848 inner_if_stmt = begin_if_stmt ();
849 finish_if_stmt_cond (flag, inner_if_stmt);
2282d28d
MM
850
851 /* Compute the location of the virtual base. If we're
852 constructing virtual bases, then we must be the most derived
853 class. Therefore, we don't have to look up the virtual base;
854 we already know where it is. */
22ed7e5f
MM
855 exp = convert_to_base_statically (current_class_ref, vbase);
856
857 expand_aggr_init_1 (vbase, current_class_ref, exp, arguments,
858 LOOKUP_COMPLAIN);
2282d28d 859 finish_then_clause (inner_if_stmt);
325c3691 860 finish_if_stmt (inner_if_stmt);
2282d28d
MM
861
862 expand_cleanup_for_base (vbase, flag);
8d08fdba
MS
863}
864
2ee887f2 865/* Find the context in which this FIELD can be initialized. */
e92cc029 866
2ee887f2 867static tree
362efdc1 868initializing_context (tree field)
2ee887f2
MS
869{
870 tree t = DECL_CONTEXT (field);
871
872 /* Anonymous union members can be initialized in the first enclosing
873 non-anonymous union context. */
6bdb8141 874 while (t && ANON_AGGR_TYPE_P (t))
2ee887f2
MS
875 t = TYPE_CONTEXT (t);
876 return t;
877}
878
8d08fdba
MS
879/* Function to give error message if member initialization specification
880 is erroneous. FIELD is the member we decided to initialize.
881 TYPE is the type for which the initialization is being performed.
72b7eeff 882 FIELD must be a member of TYPE.
8d08fdba
MS
883
884 MEMBER_NAME is the name of the member. */
885
886static int
362efdc1 887member_init_ok_or_else (tree field, tree type, tree member_name)
8d08fdba
MS
888{
889 if (field == error_mark_node)
890 return 0;
a723baf1 891 if (!field)
8d08fdba 892 {
33bd39a2 893 error ("class `%T' does not have any field named `%D'", type,
a723baf1 894 member_name);
8d08fdba
MS
895 return 0;
896 }
a723baf1 897 if (TREE_CODE (field) == VAR_DECL)
b7484fbe 898 {
a723baf1
MM
899 error ("`%#D' is a static data member; it can only be "
900 "initialized at its definition",
901 field);
902 return 0;
903 }
904 if (TREE_CODE (field) != FIELD_DECL)
905 {
906 error ("`%#D' is not a non-static data member of `%T'",
907 field, type);
908 return 0;
909 }
910 if (initializing_context (field) != type)
911 {
912 error ("class `%T' does not have any field named `%D'", type,
913 member_name);
b7484fbe
MS
914 return 0;
915 }
916
8d08fdba
MS
917 return 1;
918}
919
2282d28d
MM
920/* NAME is a FIELD_DECL, an IDENTIFIER_NODE which names a field, or it
921 is a _TYPE node or TYPE_DECL which names a base for that type.
1f5a253a
NS
922 Check the validity of NAME, and return either the base _TYPE, base
923 binfo, or the FIELD_DECL of the member. If NAME is invalid, return
2282d28d 924 NULL_TREE and issue a diagnostic.
8d08fdba 925
36a68fe7
NS
926 An old style unnamed direct single base construction is permitted,
927 where NAME is NULL. */
8d08fdba 928
fd74ca0b 929tree
1f5a253a 930expand_member_init (tree name)
8d08fdba 931{
2282d28d
MM
932 tree basetype;
933 tree field;
8d08fdba 934
2282d28d 935 if (!current_class_ref)
fd74ca0b 936 return NULL_TREE;
8d08fdba 937
36a68fe7 938 if (!name)
90418208 939 {
36a68fe7
NS
940 /* This is an obsolete unnamed base class initializer. The
941 parser will already have warned about its use. */
604a3205 942 switch (BINFO_N_BASE_BINFOS (TYPE_BINFO (current_class_type)))
36a68fe7
NS
943 {
944 case 0:
33bd39a2 945 error ("unnamed initializer for `%T', which has no base classes",
2282d28d 946 current_class_type);
36a68fe7
NS
947 return NULL_TREE;
948 case 1:
604a3205
NS
949 basetype = BINFO_TYPE
950 (BINFO_BASE_BINFO (TYPE_BINFO (current_class_type), 0));
36a68fe7
NS
951 break;
952 default:
33bd39a2 953 error ("unnamed initializer for `%T', which uses multiple inheritance",
2282d28d 954 current_class_type);
36a68fe7
NS
955 return NULL_TREE;
956 }
90418208 957 }
36a68fe7 958 else if (TYPE_P (name))
be99da77 959 {
a82d6da5 960 basetype = TYPE_MAIN_VARIANT (name);
36a68fe7 961 name = TYPE_NAME (name);
be99da77 962 }
36a68fe7
NS
963 else if (TREE_CODE (name) == TYPE_DECL)
964 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
2282d28d
MM
965 else
966 basetype = NULL_TREE;
8d08fdba 967
36a68fe7 968 if (basetype)
41efda8f 969 {
d9148cf4
MM
970 tree class_binfo;
971 tree direct_binfo;
972 tree virtual_binfo;
973 int i;
2282d28d 974
36a68fe7 975 if (current_template_parms)
1f5a253a 976 return basetype;
2282d28d 977
d9148cf4
MM
978 class_binfo = TYPE_BINFO (current_class_type);
979 direct_binfo = NULL_TREE;
980 virtual_binfo = NULL_TREE;
981
982 /* Look for a direct base. */
fa743e8c
NS
983 for (i = 0; BINFO_BASE_ITERATE (class_binfo, i, direct_binfo); ++i)
984 if (same_type_p (basetype, BINFO_TYPE (direct_binfo)))
985 break;
986
d9148cf4
MM
987 /* Look for a virtual base -- unless the direct base is itself
988 virtual. */
809e3e7f 989 if (!direct_binfo || !BINFO_VIRTUAL_P (direct_binfo))
58c42dc2 990 virtual_binfo = binfo_for_vbase (basetype, current_class_type);
d9148cf4
MM
991
992 /* [class.base.init]
993
994 If a mem-initializer-id is ambiguous because it designates
995 both a direct non-virtual base class and an inherited virtual
996 base class, the mem-initializer is ill-formed. */
997 if (direct_binfo && virtual_binfo)
998 {
999 error ("'%D' is both a direct base and an indirect virtual base",
1000 basetype);
1001 return NULL_TREE;
1002 }
1003
1004 if (!direct_binfo && !virtual_binfo)
8d08fdba 1005 {
2282d28d 1006 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
33bd39a2 1007 error ("type `%D' is not a direct or virtual base of `%T'",
2282d28d 1008 name, current_class_type);
41efda8f 1009 else
33bd39a2 1010 error ("type `%D' is not a direct base of `%T'",
2282d28d 1011 name, current_class_type);
fd74ca0b 1012 return NULL_TREE;
41efda8f 1013 }
d9148cf4
MM
1014
1015 return direct_binfo ? direct_binfo : virtual_binfo;
41efda8f
MM
1016 }
1017 else
1018 {
2282d28d 1019 if (TREE_CODE (name) == IDENTIFIER_NODE)
86ac0575 1020 field = lookup_field (current_class_type, name, 1, false);
2282d28d
MM
1021 else
1022 field = name;
8d08fdba 1023
2282d28d 1024 if (member_init_ok_or_else (field, current_class_type, name))
1f5a253a 1025 return field;
41efda8f 1026 }
fd74ca0b 1027
2282d28d 1028 return NULL_TREE;
8d08fdba
MS
1029}
1030
1031/* This is like `expand_member_init', only it stores one aggregate
1032 value into another.
1033
1034 INIT comes in two flavors: it is either a value which
1035 is to be stored in EXP, or it is a parameter list
1036 to go to a constructor, which will operate on EXP.
f30432d7
MS
1037 If INIT is not a parameter list for a constructor, then set
1038 LOOKUP_ONLYCONVERTING.
6060a796
MS
1039 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1040 the initializer, if FLAGS is 0, then it is the (init) form.
8d08fdba 1041 If `init' is a CONSTRUCTOR, then we emit a warning message,
59be0cdd 1042 explaining that such initializations are invalid.
8d08fdba 1043
8d08fdba
MS
1044 If INIT resolves to a CALL_EXPR which happens to return
1045 something of the type we are looking for, then we know
1046 that we can safely use that call to perform the
1047 initialization.
1048
1049 The virtual function table pointer cannot be set up here, because
1050 we do not really know its type.
1051
8d08fdba
MS
1052 This never calls operator=().
1053
1054 When initializing, nothing is CONST.
1055
1056 A default copy constructor may have to be used to perform the
1057 initialization.
1058
1059 A constructor or a conversion operator may have to be used to
e92cc029 1060 perform the initialization, but not both, as it would be ambiguous. */
8d08fdba 1061
f1dedc31 1062tree
362efdc1 1063build_aggr_init (tree exp, tree init, int flags)
8d08fdba 1064{
f1dedc31
MM
1065 tree stmt_expr;
1066 tree compound_stmt;
1067 int destroy_temps;
8d08fdba
MS
1068 tree type = TREE_TYPE (exp);
1069 int was_const = TREE_READONLY (exp);
f30432d7 1070 int was_volatile = TREE_THIS_VOLATILE (exp);
2a3398e1 1071 int is_global;
8d08fdba
MS
1072
1073 if (init == error_mark_node)
f1dedc31 1074 return error_mark_node;
8d08fdba
MS
1075
1076 TREE_READONLY (exp) = 0;
f30432d7
MS
1077 TREE_THIS_VOLATILE (exp) = 0;
1078
1079 if (init && TREE_CODE (init) != TREE_LIST)
1080 flags |= LOOKUP_ONLYCONVERTING;
8d08fdba
MS
1081
1082 if (TREE_CODE (type) == ARRAY_TYPE)
1083 {
671cb993
MM
1084 tree itype;
1085
92a62aad
MM
1086 /* An array may not be initialized use the parenthesized
1087 initialization form -- unless the initializer is "()". */
1088 if (init && TREE_CODE (init) == TREE_LIST)
8d08fdba 1089 {
33bd39a2 1090 error ("bad array initializer");
f1dedc31 1091 return error_mark_node;
8d08fdba 1092 }
92a62aad
MM
1093 /* Must arrange to initialize each element of EXP
1094 from elements of INIT. */
671cb993 1095 itype = init ? TREE_TYPE (init) : NULL_TREE;
89d684bb 1096 if (cp_type_quals (type) != TYPE_UNQUALIFIED)
b2153b98
KL
1097 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1098 if (itype && cp_type_quals (itype) != TYPE_UNQUALIFIED)
92a62aad 1099 itype = TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
a48cccea 1100 stmt_expr = build_vec_init (exp, NULL_TREE, init,
92a62aad
MM
1101 itype && same_type_p (itype,
1102 TREE_TYPE (exp)));
8d08fdba 1103 TREE_READONLY (exp) = was_const;
f30432d7 1104 TREE_THIS_VOLATILE (exp) = was_volatile;
8d08fdba 1105 TREE_TYPE (exp) = type;
f376e137
MS
1106 if (init)
1107 TREE_TYPE (init) = itype;
f1dedc31 1108 return stmt_expr;
8d08fdba
MS
1109 }
1110
1111 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
f4f206f4 1112 /* Just know that we've seen something for this node. */
8d08fdba
MS
1113 TREE_USED (exp) = 1;
1114
e7843f33 1115 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
2a3398e1 1116 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623 1117 destroy_temps = stmts_are_full_exprs_p ();
ae499cce 1118 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8d08fdba 1119 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
b370501f 1120 init, LOOKUP_NORMAL|flags);
2a3398e1 1121 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
ae499cce 1122 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
e7843f33 1123 TREE_TYPE (exp) = type;
8d08fdba 1124 TREE_READONLY (exp) = was_const;
f30432d7 1125 TREE_THIS_VOLATILE (exp) = was_volatile;
f1dedc31
MM
1126
1127 return stmt_expr;
8d08fdba
MS
1128}
1129
6f30f1f1
JM
1130/* Like build_aggr_init, but not just for aggregates. */
1131
1132tree
362efdc1 1133build_init (tree decl, tree init, int flags)
6f30f1f1
JM
1134{
1135 tree expr;
1136
6de9cd9a 1137 if (TREE_CODE (TREE_TYPE (decl)) == ARRAY_TYPE)
6f30f1f1 1138 expr = build_aggr_init (decl, init, flags);
6de9cd9a
DN
1139 else if (CLASS_TYPE_P (TREE_TYPE (decl)))
1140 expr = build_special_member_call (decl, complete_ctor_identifier,
1141 build_tree_list (NULL_TREE, init),
cad7e87b 1142 TREE_TYPE (decl),
6de9cd9a 1143 LOOKUP_NORMAL|flags);
6f30f1f1 1144 else
f293ce4b 1145 expr = build2 (INIT_EXPR, TREE_TYPE (decl), decl, init);
8e3df2de 1146
6f30f1f1
JM
1147 return expr;
1148}
1149
8d08fdba 1150static void
362efdc1 1151expand_default_init (tree binfo, tree true_exp, tree exp, tree init, int flags)
8d08fdba 1152{
fc378698 1153 tree type = TREE_TYPE (exp);
9eb71d8c 1154 tree ctor_name;
fc378698 1155
8d08fdba
MS
1156 /* It fails because there may not be a constructor which takes
1157 its own type as the first (or only parameter), but which does
1158 take other types via a conversion. So, if the thing initializing
1159 the expression is a unit element of type X, first try X(X&),
1160 followed by initialization by X. If neither of these work
1161 out, then look hard. */
1162 tree rval;
1163 tree parms;
8d08fdba 1164
277294d7 1165 if (init && TREE_CODE (init) != TREE_LIST
faf5394a
MS
1166 && (flags & LOOKUP_ONLYCONVERTING))
1167 {
1168 /* Base subobjects should only get direct-initialization. */
1169 if (true_exp != exp)
1170 abort ();
1171
c37dc68e
JM
1172 if (flags & DIRECT_BIND)
1173 /* Do nothing. We hit this in two cases: Reference initialization,
1174 where we aren't initializing a real variable, so we don't want
1175 to run a new constructor; and catching an exception, where we
1176 have already built up the constructor call so we could wrap it
1177 in an exception region. */;
92a62aad 1178 else if (BRACE_ENCLOSED_INITIALIZER_P (init))
8e3df2de 1179 {
b216f69b 1180 /* A brace-enclosed initializer for an aggregate. */
50bc768d 1181 gcc_assert (CP_AGGREGATE_TYPE_P (type));
b216f69b 1182 init = digest_init (type, init, (tree *)NULL);
8e3df2de 1183 }
c37dc68e 1184 else
37c46b43 1185 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
faf5394a 1186
4e8dca1c
JM
1187 if (TREE_CODE (init) == MUST_NOT_THROW_EXPR)
1188 /* We need to protect the initialization of a catch parm with a
1189 call to terminate(), which shows up as a MUST_NOT_THROW_EXPR
c7ae64f2 1190 around the TARGET_EXPR for the copy constructor. See
4e8dca1c
JM
1191 initialize_handler_parm. */
1192 {
f293ce4b
RS
1193 TREE_OPERAND (init, 0) = build2 (INIT_EXPR, TREE_TYPE (exp), exp,
1194 TREE_OPERAND (init, 0));
4e8dca1c
JM
1195 TREE_TYPE (init) = void_type_node;
1196 }
c7ae64f2 1197 else
f293ce4b 1198 init = build2 (INIT_EXPR, TREE_TYPE (exp), exp, init);
c7ae64f2 1199 TREE_SIDE_EFFECTS (init) = 1;
f1dedc31 1200 finish_expr_stmt (init);
faf5394a
MS
1201 return;
1202 }
1203
b7484fbe
MS
1204 if (init == NULL_TREE
1205 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
8d08fdba
MS
1206 {
1207 parms = init;
db5ae43f
MS
1208 if (parms)
1209 init = TREE_VALUE (parms);
8d08fdba 1210 }
8d08fdba 1211 else
051e6fd7 1212 parms = build_tree_list (NULL_TREE, init);
8d08fdba 1213
9eb71d8c
MM
1214 if (true_exp == exp)
1215 ctor_name = complete_ctor_identifier;
1216 else
1217 ctor_name = base_ctor_identifier;
8d08fdba 1218
4ba126e4 1219 rval = build_special_member_call (exp, ctor_name, parms, binfo, flags);
25eb19ff 1220 if (TREE_SIDE_EFFECTS (rval))
e895113a 1221 finish_expr_stmt (convert_to_void (rval, NULL));
8d08fdba
MS
1222}
1223
1224/* This function is responsible for initializing EXP with INIT
1225 (if any).
1226
1227 BINFO is the binfo of the type for who we are performing the
1228 initialization. For example, if W is a virtual base class of A and B,
1229 and C : A, B.
1230 If we are initializing B, then W must contain B's W vtable, whereas
1231 were we initializing C, W must contain C's W vtable.
1232
1233 TRUE_EXP is nonzero if it is the true expression being initialized.
1234 In this case, it may be EXP, or may just contain EXP. The reason we
1235 need this is because if EXP is a base element of TRUE_EXP, we
1236 don't necessarily know by looking at EXP where its virtual
1237 baseclass fields should really be pointing. But we do know
1238 from TRUE_EXP. In constructors, we don't know anything about
1239 the value being initialized.
1240
9f880ef9
MM
1241 FLAGS is just passed to `build_new_method_call'. See that function
1242 for its description. */
8d08fdba
MS
1243
1244static void
362efdc1 1245expand_aggr_init_1 (tree binfo, tree true_exp, tree exp, tree init, int flags)
8d08fdba
MS
1246{
1247 tree type = TREE_TYPE (exp);
8d08fdba 1248
50bc768d
NS
1249 gcc_assert (init != error_mark_node && type != error_mark_node);
1250 gcc_assert (building_stmt_tree ());
8d08fdba
MS
1251
1252 /* Use a function returning the desired type to initialize EXP for us.
1253 If the function is a constructor, and its first argument is
1254 NULL_TREE, know that it was meant for us--just slide exp on
1255 in and expand the constructor. Constructors now come
1256 as TARGET_EXPRs. */
faf5394a
MS
1257
1258 if (init && TREE_CODE (exp) == VAR_DECL
1259 && TREE_CODE (init) == CONSTRUCTOR
1260 && TREE_HAS_CONSTRUCTOR (init))
1261 {
f1dedc31
MM
1262 /* If store_init_value returns NULL_TREE, the INIT has been
1263 record in the DECL_INITIAL for EXP. That means there's
1264 nothing more we have to do. */
25ebb82a
RH
1265 init = store_init_value (exp, init);
1266 if (init)
1267 finish_expr_stmt (init);
faf5394a
MS
1268 return;
1269 }
1270
9e9ff709
MS
1271 /* We know that expand_default_init can handle everything we want
1272 at this point. */
b370501f 1273 expand_default_init (binfo, true_exp, exp, init, flags);
8d08fdba
MS
1274}
1275
be99da77
MS
1276/* Report an error if TYPE is not a user-defined, aggregate type. If
1277 OR_ELSE is nonzero, give an error message. */
e92cc029 1278
be99da77 1279int
362efdc1 1280is_aggr_type (tree type, int or_else)
be99da77
MS
1281{
1282 if (type == error_mark_node)
1283 return 0;
1284
1285 if (! IS_AGGR_TYPE (type)
73b0fce8 1286 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
a1281f45 1287 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
be99da77
MS
1288 {
1289 if (or_else)
33bd39a2 1290 error ("`%T' is not an aggregate type", type);
be99da77
MS
1291 return 0;
1292 }
1293 return 1;
1294}
1295
8d08fdba 1296tree
362efdc1 1297get_type_value (tree name)
8d08fdba 1298{
8d08fdba
MS
1299 if (name == error_mark_node)
1300 return NULL_TREE;
1301
1302 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1303 return IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1304 else
1305 return NULL_TREE;
1306}
051e6fd7 1307
a5ac359a
MM
1308/* Build a reference to a member of an aggregate. This is not a C++
1309 `&', but really something which can have its address taken, and
1310 then act as a pointer to member, for example TYPE :: FIELD can have
1311 its address taken by saying & TYPE :: FIELD. ADDRESS_P is true if
1312 this expression is the operand of "&".
8d08fdba
MS
1313
1314 @@ Prints out lousy diagnostics for operator <typename>
1315 @@ fields.
1316
51c184be 1317 @@ This function should be rewritten and placed in search.c. */
e92cc029 1318
8d08fdba 1319tree
a5ac359a 1320build_offset_ref (tree type, tree name, bool address_p)
8d08fdba 1321{
8d245821 1322 tree decl;
d6479fe7 1323 tree member;
fc378698 1324 tree basebinfo = NULL_TREE;
2a238a97 1325 tree orig_name = name;
8d08fdba 1326
5f311aec 1327 /* class templates can come in as TEMPLATE_DECLs here. */
874503bc 1328 if (TREE_CODE (name) == TEMPLATE_DECL)
93cdc044
JM
1329 return name;
1330
3601f003 1331 if (dependent_type_p (type) || type_dependent_expression_p (name))
53b22f3d 1332 return build_min_nt (SCOPE_REF, type, name);
5566b478 1333
2a238a97
MM
1334 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1335 {
1336 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1337 something like `a.template f<int>' or the like. For the most
1338 part, we treat this just like a.f. We do remember, however,
1339 the template-id that was used. */
1340 name = TREE_OPERAND (orig_name, 0);
e4a84209 1341
c65a922c
TP
1342 if (DECL_P (name))
1343 name = DECL_NAME (name);
1344 else
1345 {
10b1d5e7
MM
1346 if (TREE_CODE (name) == COMPONENT_REF)
1347 name = TREE_OPERAND (name, 1);
1348 if (TREE_CODE (name) == OVERLOAD)
1349 name = DECL_NAME (OVL_CURRENT (name));
c65a922c 1350 }
e4a84209 1351
50bc768d 1352 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
2a238a97
MM
1353 }
1354
c833d2be
NS
1355 if (type == NULL_TREE)
1356 return error_mark_node;
1357
1358 /* Handle namespace names fully here. */
1359 if (TREE_CODE (type) == NAMESPACE_DECL)
1360 {
8d245821 1361 tree t = lookup_namespace_name (type, name);
c833d2be
NS
1362 if (t == error_mark_node)
1363 return t;
1364 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1365 /* Reconstruct the TEMPLATE_ID_EXPR. */
f293ce4b
RS
1366 t = build2 (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1367 t, TREE_OPERAND (orig_name, 1));
c833d2be
NS
1368 if (! type_unknown_p (t))
1369 {
1370 mark_used (t);
1371 t = convert_from_reference (t);
1372 }
1373 return t;
1374 }
1375
1376 if (! is_aggr_type (type, 1))
1377 return error_mark_node;
1378
8d08fdba
MS
1379 if (TREE_CODE (name) == BIT_NOT_EXPR)
1380 {
1c2c08a5 1381 if (! check_dtor_name (type, name))
33bd39a2 1382 error ("qualified type `%T' does not match destructor name `~%T'",
1c2c08a5
JM
1383 type, TREE_OPERAND (name, 0));
1384 name = dtor_identifier;
8d08fdba 1385 }
be99da77 1386
d0f062fb 1387 if (!COMPLETE_TYPE_P (complete_type (type))
61a127b3 1388 && !TYPE_BEING_DEFINED (type))
8d08fdba 1389 {
33bd39a2 1390 error ("incomplete type `%T' does not have member `%D'", type,
61a127b3 1391 name);
8d08fdba
MS
1392 return error_mark_node;
1393 }
1394
3601f003 1395 /* Set up BASEBINFO for member lookup. */
51924768 1396 decl = maybe_dummy_object (type, &basebinfo);
8d08fdba 1397
a723baf1 1398 if (BASELINK_P (name) || DECL_P (name))
50ad9642
MM
1399 member = name;
1400 else
1401 {
1402 member = lookup_member (basebinfo, name, 1, 0);
1403
1404 if (member == error_mark_node)
1405 return error_mark_node;
1406 }
00595019 1407
a5ac359a
MM
1408 if (!member)
1409 {
1410 error ("`%D' is not a member of type `%T'", name, type);
1411 return error_mark_node;
1412 }
1413
3601f003
KL
1414 if (processing_template_decl)
1415 {
1416 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1417 return build_min (SCOPE_REF, TREE_TYPE (member), type, orig_name);
1418 else
1419 return build_min (SCOPE_REF, TREE_TYPE (member), type, name);
1420 }
1421
a5ac359a
MM
1422 if (TREE_CODE (member) == TYPE_DECL)
1423 {
1424 TREE_USED (member) = 1;
1425 return member;
1426 }
1427 /* static class members and class-specific enum
1428 values can be returned without further ado. */
1429 if (TREE_CODE (member) == VAR_DECL || TREE_CODE (member) == CONST_DECL)
1430 {
1431 mark_used (member);
1432 return convert_from_reference (member);
1433 }
1434
1435 if (TREE_CODE (member) == FIELD_DECL && DECL_C_BIT_FIELD (member))
1436 {
1437 error ("invalid pointer to bit-field `%D'", member);
1438 return error_mark_node;
1439 }
1440
aa52c1ff 1441 /* A lot of this logic is now handled in lookup_member. */
a5ac359a 1442 if (BASELINK_P (member))
8d08fdba 1443 {
8d08fdba 1444 /* Go from the TREE_BASELINK to the member function info. */
d6479fe7 1445 tree fnfields = member;
8d245821 1446 tree t = BASELINK_FUNCTIONS (fnfields);
8d08fdba 1447
2a238a97
MM
1448 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1449 {
1450 /* The FNFIELDS are going to contain functions that aren't
1451 necessarily templates, and templates that don't
1452 necessarily match the explicit template parameters. We
1453 save all the functions, and the explicit parameters, and
1454 then figure out exactly what to instantiate with what
1455 arguments in instantiate_type. */
1456
1457 if (TREE_CODE (t) != OVERLOAD)
1458 /* The code in instantiate_type which will process this
1459 expects to encounter OVERLOADs, not raw functions. */
1460 t = ovl_cons (t, NULL_TREE);
051e6fd7 1461
f293ce4b
RS
1462 t = build2 (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1463 TREE_OPERAND (orig_name, 1));
1464 t = build2 (OFFSET_REF, unknown_type_node, decl, t);
19420d00
NS
1465
1466 PTRMEM_OK_P (t) = 1;
1467
1468 return t;
2a238a97
MM
1469 }
1470
50ad9642 1471 if (TREE_CODE (t) != TEMPLATE_ID_EXPR && !really_overloaded_fn (t))
8d08fdba 1472 {
f4f206f4 1473 /* Get rid of a potential OVERLOAD around it. */
2c73f9f5
ML
1474 t = OVL_CURRENT (t);
1475
b54f5338
KL
1476 /* Unique functions are handled easily. */
1477
1478 /* For non-static member of base class, we need a special rule
1479 for access checking [class.protected]:
1480
1481 If the access is to form a pointer to member, the
1482 nested-name-specifier shall name the derived class
1483 (or any class derived from that class). */
1484 if (address_p && DECL_P (t)
1485 && DECL_NONSTATIC_MEMBER_P (t))
1486 perform_or_defer_access_check (TYPE_BINFO (type), t);
1487 else
1488 perform_or_defer_access_check (basebinfo, t);
1489
fc378698 1490 mark_used (t);
848b92e1
JM
1491 if (DECL_STATIC_FUNCTION_P (t))
1492 return t;
a5ac359a
MM
1493 member = t;
1494 }
1495 else
1496 {
1497 TREE_TYPE (fnfields) = unknown_type_node;
1498 member = fnfields;
8d08fdba 1499 }
8d08fdba 1500 }
b54f5338
KL
1501 else if (address_p && TREE_CODE (member) == FIELD_DECL)
1502 /* We need additional test besides the one in
1503 check_accessibility_of_qualified_id in case it is
1504 a pointer to non-static member. */
1505 perform_or_defer_access_check (TYPE_BINFO (type), member);
8d08fdba 1506
a5ac359a 1507 if (!address_p)
8d08fdba 1508 {
a5ac359a
MM
1509 /* If MEMBER is non-static, then the program has fallen afoul of
1510 [expr.prim]:
8d08fdba 1511
a5ac359a
MM
1512 An id-expression that denotes a nonstatic data member or
1513 nonstatic member function of a class can only be used:
8d08fdba 1514
a5ac359a
MM
1515 -- as part of a class member access (_expr.ref_) in which the
1516 object-expression refers to the member's class or a class
1517 derived from that class, or
b7484fbe 1518
a5ac359a
MM
1519 -- to form a pointer to member (_expr.unary.op_), or
1520
1521 -- in the body of a nonstatic member function of that class or
1522 of a class derived from that class (_class.mfct.nonstatic_), or
1523
1524 -- in a mem-initializer for a constructor for that class or for
1525 a class derived from that class (_class.base.init_). */
1526 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (member))
1527 {
e9525111
MM
1528 /* Build a representation of a the qualified name suitable
1529 for use as the operand to "&" -- even though the "&" is
1530 not actually present. */
f293ce4b 1531 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
a5ac359a
MM
1532 /* In Microsoft mode, treat a non-static member function as if
1533 it were a pointer-to-member. */
1534 if (flag_ms_extensions)
1535 {
a5ac359a
MM
1536 PTRMEM_OK_P (member) = 1;
1537 return build_unary_op (ADDR_EXPR, member, 0);
1538 }
e9525111
MM
1539 error ("invalid use of non-static member function `%D'",
1540 TREE_OPERAND (member, 1));
1541 return member;
a5ac359a
MM
1542 }
1543 else if (TREE_CODE (member) == FIELD_DECL)
1544 {
1545 error ("invalid use of non-static data member `%D'", member);
1546 return error_mark_node;
1547 }
1548 return member;
1549 }
8d08fdba 1550
be99da77
MS
1551 /* In member functions, the form `type::name' is no longer
1552 equivalent to `this->type::name', at least not until
1553 resolve_offset_ref. */
f293ce4b 1554 member = build2 (OFFSET_REF, TREE_TYPE (member), decl, member);
8d245821
MM
1555 PTRMEM_OK_P (member) = 1;
1556 return member;
8d08fdba
MS
1557}
1558
fc611ce0
MM
1559/* If DECL is a `const' declaration, and its value is a known
1560 constant, then return that value. */
8d08fdba
MS
1561
1562tree
362efdc1 1563decl_constant_value (tree decl)
8d08fdba 1564{
a1652802
MM
1565 /* When we build a COND_EXPR, we don't know whether it will be used
1566 as an lvalue or as an rvalue. If it is an lvalue, it's not safe
1567 to replace the second and third operands with their
1568 initializers. So, we do that here. */
1569 if (TREE_CODE (decl) == COND_EXPR)
1570 {
1571 tree d1;
1572 tree d2;
1573
1574 d1 = decl_constant_value (TREE_OPERAND (decl, 1));
1575 d2 = decl_constant_value (TREE_OPERAND (decl, 2));
1576
1577 if (d1 != TREE_OPERAND (decl, 1) || d2 != TREE_OPERAND (decl, 2))
f293ce4b
RS
1578 return build3 (COND_EXPR,
1579 TREE_TYPE (decl),
1580 TREE_OPERAND (decl, 0), d1, d2);
a1652802
MM
1581 }
1582
c353b8e3
MM
1583 if (DECL_P (decl)
1584 && (/* Enumeration constants are constant. */
1585 TREE_CODE (decl) == CONST_DECL
1586 /* And so are variables with a 'const' type -- unless they
1587 are also 'volatile'. */
1588 || CP_TYPE_CONST_NON_VOLATILE_P (TREE_TYPE (decl)))
4f976745 1589 && TREE_CODE (decl) != PARM_DECL
61a127b3 1590 && DECL_INITIAL (decl)
bd6dd845 1591 && DECL_INITIAL (decl) != error_mark_node
8d08fdba
MS
1592 /* This is invalid if initial value is not constant.
1593 If it has either a function call, a memory reference,
1594 or a variable, then re-evaluating it could give different results. */
1595 && TREE_CONSTANT (DECL_INITIAL (decl))
1596 /* Check for cases where this is sub-optimal, even though valid. */
61a127b3 1597 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
8d08fdba
MS
1598 return DECL_INITIAL (decl);
1599 return decl;
1600}
1601\f
8d08fdba
MS
1602/* Common subroutines of build_new and build_vec_delete. */
1603
c787dd82 1604/* Call the global __builtin_delete to delete ADDR. */
8d08fdba 1605
bd6dd845 1606static tree
362efdc1 1607build_builtin_delete_call (tree addr)
8d08fdba 1608{
a6ecf8b6 1609 mark_used (global_delete_fndecl);
0c11ada6 1610 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
8d08fdba
MS
1611}
1612\f
1613/* Generate a C++ "new" expression. DECL is either a TREE_LIST
1614 (which needs to go through some sort of groktypename) or it
1615 is the name of the class we are newing. INIT is an initialization value.
1616 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1617 If INIT is void_type_node, it means do *not* call a constructor
1618 for this instance.
1619
1620 For types with constructors, the data returned is initialized
1621 by the appropriate constructor.
1622
1623 Whether the type has a constructor or not, if it has a pointer
1624 to a virtual function table, then that pointer is set up
1625 here.
1626
1627 Unless I am mistaken, a call to new () will return initialized
1628 data regardless of whether the constructor itself is private or
8926095f 1629 not. NOPE; new fails if the constructor is private (jcm).
8d08fdba
MS
1630
1631 Note that build_new does nothing to assure that any special
1632 alignment requirements of the type are met. Rather, it leaves
1633 it up to malloc to do the right thing. Otherwise, folding to
1634 the right alignment cal cause problems if the user tries to later
1635 free the memory returned by `new'.
1636
1637 PLACEMENT is the `placement' list for user-defined operator new (). */
1638
1639tree
058b15c1
MM
1640build_new (tree placement, tree type, tree nelts, tree init,
1641 int use_global_new)
8d08fdba 1642{
058b15c1 1643 tree rval;
8d08fdba 1644
058b15c1 1645 if (type == error_mark_node)
8d08fdba
MS
1646 return error_mark_node;
1647
5156628f 1648 if (processing_template_decl)
5566b478 1649 {
c006d942 1650 rval = build_min (NEW_EXPR, build_pointer_type (type),
058b15c1 1651 placement, type, nelts, init);
5566b478 1652 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
c1cca8d4 1653 TREE_SIDE_EFFECTS (rval) = 1;
5566b478
MS
1654 return rval;
1655 }
1656
8926095f
MS
1657 /* ``A reference cannot be created by the new operator. A reference
1658 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
1659 returned by new.'' ARM 5.3.3 */
1660 if (TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 1661 {
8251199e 1662 error ("new cannot be applied to a reference type");
a0d5fba7 1663 type = TREE_TYPE (type);
8d08fdba
MS
1664 }
1665
b7484fbe
MS
1666 if (TREE_CODE (type) == FUNCTION_TYPE)
1667 {
8251199e 1668 error ("new cannot be applied to a function type");
b7484fbe
MS
1669 return error_mark_node;
1670 }
1671
f293ce4b
RS
1672 rval = build4 (NEW_EXPR, build_pointer_type (type), placement, type,
1673 nelts, init);
a0d5fba7
JM
1674 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
1675 TREE_SIDE_EFFECTS (rval) = 1;
b3ab27f3
MM
1676 rval = build_new_1 (rval);
1677 if (rval == error_mark_node)
1678 return error_mark_node;
a0d5fba7
JM
1679
1680 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
1681 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
6de9cd9a 1682 TREE_NO_WARNING (rval) = 1;
a0d5fba7 1683
a0d5fba7
JM
1684 return rval;
1685}
1686
c6002625 1687/* Given a Java class, return a decl for the corresponding java.lang.Class. */
743f140d 1688
e97f22c9 1689tree
362efdc1 1690build_java_class_ref (tree type)
743f140d 1691{
ae0ed63a 1692 tree name = NULL_TREE, class_decl;
d1a458c4
TT
1693 static tree CL_suffix = NULL_TREE;
1694 if (CL_suffix == NULL_TREE)
1695 CL_suffix = get_identifier("class$");
743f140d
PB
1696 if (jclass_node == NULL_TREE)
1697 {
400500c4 1698 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier ("jclass"));
743f140d 1699 if (jclass_node == NULL_TREE)
400500c4
RK
1700 fatal_error ("call to Java constructor, while `jclass' undefined");
1701
743f140d
PB
1702 jclass_node = TREE_TYPE (jclass_node);
1703 }
23d4e4cc 1704
f4f206f4 1705 /* Mangle the class$ field. */
1f84ec23
MM
1706 {
1707 tree field;
1708 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
1709 if (DECL_NAME (field) == CL_suffix)
1710 {
92643fea
MM
1711 mangle_decl (field);
1712 name = DECL_ASSEMBLER_NAME (field);
1f84ec23
MM
1713 break;
1714 }
1715 if (!field)
1f978f5f 1716 internal_error ("can't find class$");
23d4e4cc 1717 }
23d4e4cc 1718
743f140d
PB
1719 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
1720 if (class_decl == NULL_TREE)
1721 {
743f140d
PB
1722 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
1723 TREE_STATIC (class_decl) = 1;
1724 DECL_EXTERNAL (class_decl) = 1;
1725 TREE_PUBLIC (class_decl) = 1;
1726 DECL_ARTIFICIAL (class_decl) = 1;
1727 DECL_IGNORED_P (class_decl) = 1;
1728 pushdecl_top_level (class_decl);
0e6df31e 1729 make_decl_rtl (class_decl);
743f140d
PB
1730 }
1731 return class_decl;
1732}
1733
834c6dff 1734
a0d5fba7
JM
1735/* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
1736 value is immediately handed to expand_expr. */
1737
834c6dff 1738static tree
362efdc1 1739build_new_1 (tree exp)
a0d5fba7 1740{
56c5d8bf 1741 tree placement, init;
6de9cd9a 1742 tree true_type, size, rval;
f4f4610e
MM
1743 /* The type of the new-expression. (This type is always a pointer
1744 type.) */
1745 tree pointer_type;
1746 /* The type pointed to by POINTER_TYPE. */
1747 tree type;
1748 /* The type being allocated. For "new T[...]" this will be an
1749 ARRAY_TYPE. */
f30efcb7 1750 tree full_type;
f4f4610e
MM
1751 /* A pointer type pointing to to the FULL_TYPE. */
1752 tree full_pointer_type;
a48cccea 1753 tree outer_nelts = NULL_TREE;
a703fb38 1754 tree nelts = NULL_TREE;
f4f4610e
MM
1755 tree alloc_call, alloc_expr;
1756 /* The address returned by the call to "operator new". This node is
1757 a VAR_DECL and is therefore reusable. */
1758 tree alloc_node;
46ff5047 1759 tree alloc_fn;
8b5e2ce4 1760 tree cookie_expr, init_expr;
a0d5fba7 1761 int has_array = 0;
834c6dff 1762 enum tree_code code;
089d6ea7 1763 int nothrow, check_new;
834c6dff
MM
1764 /* Nonzero if the user wrote `::new' rather than just `new'. */
1765 int globally_qualified_p;
743f140d 1766 int use_java_new = 0;
834c6dff
MM
1767 /* If non-NULL, the number of extra bytes to allocate at the
1768 beginning of the storage allocated for an array-new expression in
1769 order to store the number of elements. */
1770 tree cookie_size = NULL_TREE;
3f41ffd8
MM
1771 /* True if the function we are calling is a placement allocation
1772 function. */
1773 bool placement_allocation_fn_p;
4f649415 1774 tree args = NULL_TREE;
f4f4610e 1775 /* True if the storage must be initialized, either by a constructor
34cd5ae7 1776 or due to an explicit new-initializer. */
f4f4610e
MM
1777 bool is_initialized;
1778 /* The address of the thing allocated, not including any cookie. In
1779 particular, if an array cookie is in use, DATA_ADDR is the
1780 address of the first array element. This node is a VAR_DECL, and
1781 is therefore reusable. */
1782 tree data_addr;
6de9cd9a 1783 tree init_preeval_expr = NULL_TREE;
a0d5fba7
JM
1784
1785 placement = TREE_OPERAND (exp, 0);
1786 type = TREE_OPERAND (exp, 1);
058b15c1
MM
1787 nelts = TREE_OPERAND (exp, 2);
1788 init = TREE_OPERAND (exp, 3);
834c6dff 1789 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
a0d5fba7 1790
058b15c1 1791 if (nelts)
a0d5fba7 1792 {
9117ccad
RH
1793 tree index;
1794
a0d5fba7 1795 has_array = 1;
058b15c1 1796 outer_nelts = nelts;
9117ccad
RH
1797
1798 /* ??? The middle-end will error on us for building a VLA outside a
1799 function context. Methinks that's not it's purvey. So we'll do
1800 our own VLA layout later. */
1801
a48cccea 1802 full_type = build_cplus_array_type (type, NULL_TREE);
9117ccad
RH
1803
1804 index = convert (sizetype, nelts);
1805 index = size_binop (MINUS_EXPR, index, size_one_node);
1806 TYPE_DOMAIN (full_type) = build_index_type (index);
a0d5fba7 1807 }
f30efcb7
JM
1808 else
1809 full_type = type;
1810
a0d5fba7
JM
1811 true_type = type;
1812
834c6dff
MM
1813 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
1814
8d08fdba
MS
1815 /* If our base type is an array, then make sure we know how many elements
1816 it has. */
1817 while (TREE_CODE (true_type) == ARRAY_TYPE)
1818 {
1819 tree this_nelts = array_type_nelts_top (true_type);
ab76ca54 1820 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
8d08fdba
MS
1821 true_type = TREE_TYPE (true_type);
1822 }
5566b478 1823
66543169 1824 if (!complete_type_or_else (true_type, exp))
8f259df3 1825 return error_mark_node;
5566b478 1826
cc600f33 1827 if (TREE_CODE (true_type) == VOID_TYPE)
e1cd6e56 1828 {
8251199e 1829 error ("invalid type `void' for new");
e1cd6e56
MS
1830 return error_mark_node;
1831 }
1832
a7a64a77
MM
1833 if (abstract_virtuals_error (NULL_TREE, true_type))
1834 return error_mark_node;
8926095f 1835
f4f4610e
MM
1836 is_initialized = (TYPE_NEEDS_CONSTRUCTING (type) || init);
1837 if (CP_TYPE_CONST_P (true_type) && !is_initialized)
1838 {
1839 error ("uninitialized const in `new' of `%#T'", true_type);
1840 return error_mark_node;
1841 }
1842
089d6ea7
MM
1843 size = size_in_bytes (true_type);
1844 if (has_array)
9117ccad
RH
1845 {
1846 tree n, bitsize;
1847
1848 /* Do our own VLA layout. Setting TYPE_SIZE/_UNIT is necessary in
1849 order for the <INIT_EXPR <*foo> <CONSTRUCTOR ...>> to be valid. */
1850
1851 n = convert (sizetype, nelts);
1852 size = size_binop (MULT_EXPR, size, n);
1853 TYPE_SIZE_UNIT (full_type) = size;
1854
1855 n = convert (bitsizetype, nelts);
1856 bitsize = size_binop (MULT_EXPR, TYPE_SIZE (true_type), n);
1857 TYPE_SIZE (full_type) = bitsize;
1858 }
a28e3c7f 1859
e92cc029 1860 /* Allocate the object. */
9bfadf57 1861 if (! placement && TYPE_FOR_JAVA (true_type))
743f140d 1862 {
8c1bd4f5 1863 tree class_addr, alloc_decl;
743f140d 1864 tree class_decl = build_java_class_ref (true_type);
8b60264b 1865 static const char alloc_name[] = "_Jv_AllocObject";
6de9cd9a 1866
743f140d 1867 use_java_new = 1;
6de9cd9a 1868 alloc_decl = NULL;
6961a592
GB
1869 if (!get_global_value_if_present (get_identifier (alloc_name),
1870 &alloc_decl))
b1e5b86c 1871 {
6961a592
GB
1872 error ("call to Java constructor with `%s' undefined", alloc_name);
1873 return error_mark_node;
1874 }
1875 else if (really_overloaded_fn (alloc_decl))
b1e5b86c 1876 {
6961a592
GB
1877 error ("`%D' should never be overloaded", alloc_decl);
1878 return error_mark_node;
1879 }
1880 alloc_decl = OVL_CURRENT (alloc_decl);
743f140d 1881 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
96790071
JM
1882 alloc_call = (build_function_call
1883 (alloc_decl,
245c3c04 1884 build_tree_list (NULL_TREE, class_addr)));
743f140d 1885 }
8d08fdba
MS
1886 else
1887 {
834c6dff 1888 tree fnname;
9f880ef9 1889 tree fns;
834c6dff 1890
596ea4e5 1891 fnname = ansi_opname (code);
834c6dff 1892
089d6ea7
MM
1893 if (!globally_qualified_p
1894 && CLASS_TYPE_P (true_type)
1895 && (has_array
1896 ? TYPE_HAS_ARRAY_NEW_OPERATOR (true_type)
1897 : TYPE_HAS_NEW_OPERATOR (true_type)))
1898 {
1899 /* Use a class-specific operator new. */
1900 /* If a cookie is required, add some extra space. */
1901 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
1902 {
46e995e0 1903 cookie_size = targetm.cxx.get_cookie_size (true_type);
089d6ea7
MM
1904 size = size_binop (PLUS_EXPR, size, cookie_size);
1905 }
1906 /* Create the argument list. */
1907 args = tree_cons (NULL_TREE, size, placement);
9f880ef9
MM
1908 /* Do name-lookup to find the appropriate operator. */
1909 fns = lookup_fnfields (true_type, fnname, /*protect=*/2);
1910 if (TREE_CODE (fns) == TREE_LIST)
1911 {
1912 error ("request for member `%D' is ambiguous", fnname);
1913 print_candidates (fns);
1914 return error_mark_node;
1915 }
1916 alloc_call = build_new_method_call (build_dummy_object (true_type),
1917 fns, args,
1918 /*conversion_path=*/NULL_TREE,
1919 LOOKUP_NORMAL);
089d6ea7 1920 }
834c6dff 1921 else
089d6ea7
MM
1922 {
1923 /* Use a global operator new. */
125e6594
MM
1924 /* See if a cookie might be required. */
1925 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
46e995e0 1926 cookie_size = targetm.cxx.get_cookie_size (true_type);
125e6594
MM
1927 else
1928 cookie_size = NULL_TREE;
1929
1930 alloc_call = build_operator_new_call (fnname, placement,
1931 &size, &cookie_size);
089d6ea7 1932 }
8d08fdba
MS
1933 }
1934
96790071 1935 if (alloc_call == error_mark_node)
2bb5d995
JM
1936 return error_mark_node;
1937
a6111661
JM
1938 /* In the simple case, we can stop now. */
1939 pointer_type = build_pointer_type (type);
1940 if (!cookie_size && !is_initialized)
1941 return build_nop (pointer_type, alloc_call);
1942
1943 /* While we're working, use a pointer to the type we've actually
1944 allocated. Store the result of the call in a variable so that we
1945 can use it more than once. */
1946 full_pointer_type = build_pointer_type (full_type);
1947 alloc_expr = get_target_expr (build_nop (full_pointer_type, alloc_call));
1948 alloc_node = TARGET_EXPR_SLOT (alloc_expr);
1949
1950 /* Strip any COMPOUND_EXPRs from ALLOC_CALL. */
1951 while (TREE_CODE (alloc_call) == COMPOUND_EXPR)
1952 alloc_call = TREE_OPERAND (alloc_call, 1);
1953 alloc_fn = get_callee_fndecl (alloc_call);
50bc768d 1954 gcc_assert (alloc_fn != NULL_TREE);
089d6ea7 1955
3f41ffd8
MM
1956 /* Now, check to see if this function is actually a placement
1957 allocation function. This can happen even when PLACEMENT is NULL
1958 because we might have something like:
1959
1960 struct S { void* operator new (size_t, int i = 0); };
1961
1962 A call to `new S' will get this allocation function, even though
1963 there is no explicit placement argument. If there is more than
1964 one argument, or there are variable arguments, then this is a
1965 placement allocation function. */
1966 placement_allocation_fn_p
46ff5047
MM
1967 = (type_num_arguments (TREE_TYPE (alloc_fn)) > 1
1968 || varargs_function_p (alloc_fn));
96790071 1969
a6111661
JM
1970 /* Preevaluate the placement args so that we don't reevaluate them for a
1971 placement delete. */
1972 if (placement_allocation_fn_p)
1973 {
6de9cd9a
DN
1974 tree inits;
1975 stabilize_call (alloc_call, &inits);
a6111661 1976 if (inits)
f293ce4b
RS
1977 alloc_expr = build2 (COMPOUND_EXPR, TREE_TYPE (alloc_expr), inits,
1978 alloc_expr);
a6111661
JM
1979 }
1980
047f64a3
JM
1981 /* unless an allocation function is declared with an empty excep-
1982 tion-specification (_except.spec_), throw(), it indicates failure to
1983 allocate storage by throwing a bad_alloc exception (clause _except_,
1984 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
1985 cation function is declared with an empty exception-specification,
1986 throw(), it returns null to indicate failure to allocate storage and a
1987 non-null pointer otherwise.
1988
1989 So check for a null exception spec on the op new we just called. */
1990
46ff5047 1991 nothrow = TYPE_NOTHROW_P (TREE_TYPE (alloc_fn));
743f140d 1992 check_new = (flag_check_new || nothrow) && ! use_java_new;
047f64a3 1993
089d6ea7 1994 if (cookie_size)
8d08fdba 1995 {
96790071 1996 tree cookie;
46e995e0 1997 tree cookie_ptr;
f4f4610e
MM
1998
1999 /* Adjust so we're pointing to the start of the object. */
f293ce4b
RS
2000 data_addr = get_target_expr (build2 (PLUS_EXPR, full_pointer_type,
2001 alloc_node, cookie_size));
96790071 2002
834c6dff 2003 /* Store the number of bytes allocated so that we can know how
3461fba7
NS
2004 many elements to destroy later. We use the last sizeof
2005 (size_t) bytes to store the number of elements. */
f293ce4b
RS
2006 cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype),
2007 data_addr, size_in_bytes (sizetype));
46e995e0 2008 cookie = build_indirect_ref (cookie_ptr, NULL);
1f84ec23 2009
f293ce4b 2010 cookie_expr = build2 (MODIFY_EXPR, sizetype, cookie, nelts);
46e995e0
PB
2011
2012 if (targetm.cxx.cookie_has_size ())
2013 {
2014 /* Also store the element size. */
f293ce4b
RS
2015 cookie_ptr = build2 (MINUS_EXPR, build_pointer_type (sizetype),
2016 cookie_ptr, size_in_bytes (sizetype));
46e995e0 2017 cookie = build_indirect_ref (cookie_ptr, NULL);
f293ce4b
RS
2018 cookie = build2 (MODIFY_EXPR, sizetype, cookie,
2019 size_in_bytes(true_type));
2020 cookie_expr = build2 (COMPOUND_EXPR, TREE_TYPE (cookie_expr),
2021 cookie, cookie_expr);
46e995e0 2022 }
8b5e2ce4 2023 data_addr = TARGET_EXPR_SLOT (data_addr);
8d08fdba 2024 }
96790071 2025 else
8b5e2ce4
JM
2026 {
2027 cookie_expr = NULL_TREE;
2028 data_addr = alloc_node;
2029 }
8d08fdba 2030
6de9cd9a
DN
2031 /* Now initialize the allocated object. Note that we preevaluate the
2032 initialization expression, apart from the actual constructor call or
2033 assignment--we do this because we want to delay the allocation as long
2034 as possible in order to minimize the size of the exception region for
2035 placement delete. */
f4f4610e 2036 if (is_initialized)
8d08fdba 2037 {
6de9cd9a
DN
2038 bool stable;
2039
f4f4610e 2040 init_expr = build_indirect_ref (data_addr, NULL);
f30efcb7
JM
2041
2042 if (init == void_zero_node)
1cb8292f 2043 init = build_default_init (full_type, nelts);
6de9cd9a 2044 else if (init && has_array)
33bd39a2 2045 pedwarn ("ISO C++ forbids initialization in array new");
f30efcb7
JM
2046
2047 if (has_array)
6de9cd9a
DN
2048 {
2049 init_expr
2050 = build_vec_init (init_expr,
2051 cp_build_binary_op (MINUS_EXPR, outer_nelts,
2052 integer_one_node),
2053 init, /*from_array=*/0);
2054
2055 /* An array initialization is stable because the initialization
2056 of each element is a full-expression, so the temporaries don't
2057 leak out. */
2058 stable = true;
2059 }
f30efcb7 2060 else if (TYPE_NEEDS_CONSTRUCTING (type))
6de9cd9a
DN
2061 {
2062 init_expr = build_special_member_call (init_expr,
2063 complete_ctor_identifier,
cad7e87b 2064 init, true_type,
6de9cd9a
DN
2065 LOOKUP_NORMAL);
2066 stable = stabilize_init (init_expr, &init_preeval_expr);
2067 }
f30efcb7 2068 else
8d08fdba 2069 {
01240200
MM
2070 /* We are processing something like `new int (10)', which
2071 means allocate an int, and initialize it with 10. */
f30efcb7
JM
2072
2073 if (TREE_CODE (init) == TREE_LIST)
c7b62f14 2074 init = build_x_compound_expr_from_list (init, "new initializer");
6de9cd9a 2075
f30efcb7
JM
2076 else if (TREE_CODE (init) == CONSTRUCTOR
2077 && TREE_TYPE (init) == NULL_TREE)
6de9cd9a 2078 abort ();
80170791 2079
f30efcb7 2080 init_expr = build_modify_expr (init_expr, INIT_EXPR, init);
6de9cd9a 2081 stable = stabilize_init (init_expr, &init_preeval_expr);
96790071
JM
2082 }
2083
2084 if (init_expr == error_mark_node)
2085 return error_mark_node;
1f109f0f 2086
20c39572
JM
2087 /* If any part of the object initialization terminates by throwing an
2088 exception and a suitable deallocation function can be found, the
2089 deallocation function is called to free the memory in which the
2090 object was being constructed, after which the exception continues
2091 to propagate in the context of the new-expression. If no
2092 unambiguous matching deallocation function can be found,
2093 propagating the exception does not cause the object's memory to be
2094 freed. */
96790071 2095 if (flag_exceptions && ! use_java_new)
1f109f0f 2096 {
2face519 2097 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
96790071 2098 tree cleanup;
a7d87521 2099
5355deec 2100 /* The Standard is unclear here, but the right thing to do
f4f4610e
MM
2101 is to use the same method for finding deallocation
2102 functions that we use for finding allocation functions. */
5bd61841
MM
2103 cleanup = build_op_delete_call (dcode, alloc_node, size,
2104 globally_qualified_p,
3f41ffd8
MM
2105 (placement_allocation_fn_p
2106 ? alloc_call : NULL_TREE));
2bb14213 2107
6de9cd9a
DN
2108 if (!cleanup)
2109 /* We're done. */;
2110 else if (stable)
2111 /* This is much simpler if we were able to preevaluate all of
2112 the arguments to the constructor call. */
f293ce4b
RS
2113 init_expr = build2 (TRY_CATCH_EXPR, void_type_node,
2114 init_expr, cleanup);
6de9cd9a
DN
2115 else
2116 /* Ack! First we allocate the memory. Then we set our sentry
2117 variable to true, and expand a cleanup that deletes the
2118 memory if sentry is true. Then we run the constructor, and
2119 finally clear the sentry.
2120
2121 We need to do this because we allocate the space first, so
2122 if there are any temporaries with cleanups in the
2123 constructor args and we weren't able to preevaluate them, we
2124 need this EH region to extend until end of full-expression
2125 to preserve nesting. */
da4768fe 2126 {
96790071 2127 tree end, sentry, begin;
2face519
JM
2128
2129 begin = get_target_expr (boolean_true_node);
659e5a7a 2130 CLEANUP_EH_ONLY (begin) = 1;
2face519 2131
659e5a7a
JM
2132 sentry = TARGET_EXPR_SLOT (begin);
2133
2134 TARGET_EXPR_CLEANUP (begin)
f293ce4b
RS
2135 = build3 (COND_EXPR, void_type_node, sentry,
2136 cleanup, void_zero_node);
2face519 2137
f293ce4b
RS
2138 end = build2 (MODIFY_EXPR, TREE_TYPE (sentry),
2139 sentry, boolean_false_node);
2face519 2140
96790071 2141 init_expr
f293ce4b
RS
2142 = build2 (COMPOUND_EXPR, void_type_node, begin,
2143 build2 (COMPOUND_EXPR, void_type_node, init_expr,
2144 end));
da4768fe 2145 }
6de9cd9a 2146
1f109f0f 2147 }
f4f4610e 2148 }
8b5e2ce4
JM
2149 else
2150 init_expr = NULL_TREE;
2151
2152 /* Now build up the return value in reverse order. */
96790071 2153
8b5e2ce4 2154 rval = data_addr;
2face519 2155
8b5e2ce4 2156 if (init_expr)
f293ce4b 2157 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_expr, rval);
8b5e2ce4 2158 if (cookie_expr)
f293ce4b 2159 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), cookie_expr, rval);
8b5e2ce4
JM
2160
2161 if (rval == alloc_node)
2162 /* If we don't have an initializer or a cookie, strip the TARGET_EXPR
2163 and return the call (which doesn't need to be adjusted). */
2164 rval = TARGET_EXPR_INITIAL (alloc_expr);
2165 else
d18c083e 2166 {
8b5e2ce4
JM
2167 if (check_new)
2168 {
2169 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2170 integer_zero_node);
2171 rval = build_conditional_expr (ifexp, rval, alloc_node);
2172 }
d18c083e 2173
8b5e2ce4
JM
2174 /* Perform the allocation before anything else, so that ALLOC_NODE
2175 has been initialized before we start using it. */
f293ce4b 2176 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
8b5e2ce4 2177 }
51c184be 2178
6de9cd9a 2179 if (init_preeval_expr)
f293ce4b 2180 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), init_preeval_expr, rval);
6de9cd9a 2181
f4f4610e 2182 /* Convert to the final type. */
d04a575f
MM
2183 rval = build_nop (pointer_type, rval);
2184
2185 /* A new-expression is never an lvalue. */
2186 if (real_lvalue_p (rval))
2187 rval = build1 (NON_LVALUE_EXPR, TREE_TYPE (rval), rval);
2188
2189 return rval;
8d08fdba
MS
2190}
2191\f
f30432d7 2192static tree
362efdc1
NN
2193build_vec_delete_1 (tree base, tree maxindex, tree type,
2194 special_function_kind auto_delete_vec, int use_global_delete)
f30432d7
MS
2195{
2196 tree virtual_size;
e92cc029 2197 tree ptype = build_pointer_type (type = complete_type (type));
f30432d7
MS
2198 tree size_exp = size_in_bytes (type);
2199
2200 /* Temporary variables used by the loop. */
2201 tree tbase, tbase_init;
2202
2203 /* This is the body of the loop that implements the deletion of a
2204 single element, and moves temp variables to next elements. */
2205 tree body;
2206
2207 /* This is the LOOP_EXPR that governs the deletion of the elements. */
c7b62f14 2208 tree loop = 0;
f30432d7
MS
2209
2210 /* This is the thing that governs what to do after the loop has run. */
2211 tree deallocate_expr = 0;
2212
2213 /* This is the BIND_EXPR which holds the outermost iterator of the
2214 loop. It is convenient to set this variable up and test it before
2215 executing any other code in the loop.
2216 This is also the containing expression returned by this function. */
2217 tree controller = NULL_TREE;
2218
b2153b98
KL
2219 /* We should only have 1-D arrays here. */
2220 if (TREE_CODE (type) == ARRAY_TYPE)
2221 abort ();
2222
834c6dff 2223 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
c7b62f14 2224 goto no_destructor;
f30432d7 2225
708cae97 2226 /* The below is short by the cookie size. */
fed3cef0
RK
2227 virtual_size = size_binop (MULT_EXPR, size_exp,
2228 convert (sizetype, maxindex));
f30432d7 2229
46e8c075 2230 tbase = create_temporary_var (ptype);
f30432d7 2231 tbase_init = build_modify_expr (tbase, NOP_EXPR,
f293ce4b
RS
2232 fold (build2 (PLUS_EXPR, ptype,
2233 base,
2234 virtual_size)));
f30432d7 2235 DECL_REGISTER (tbase) = 1;
f293ce4b
RS
2236 controller = build3 (BIND_EXPR, void_type_node, tbase,
2237 NULL_TREE, NULL_TREE);
f30432d7 2238 TREE_SIDE_EFFECTS (controller) = 1;
f30432d7 2239
f293ce4b
RS
2240 body = build1 (EXIT_EXPR, void_type_node,
2241 build2 (EQ_EXPR, boolean_type_node, base, tbase));
c7b62f14
NS
2242 body = build_compound_expr
2243 (body, build_modify_expr (tbase, NOP_EXPR,
f293ce4b 2244 build2 (MINUS_EXPR, ptype, tbase, size_exp)));
c7b62f14
NS
2245 body = build_compound_expr
2246 (body, build_delete (ptype, tbase, sfk_complete_destructor,
2247 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1));
f30432d7 2248
f293ce4b 2249 loop = build1 (LOOP_EXPR, void_type_node, body);
c7b62f14 2250 loop = build_compound_expr (tbase_init, loop);
f30432d7
MS
2251
2252 no_destructor:
2253 /* If the delete flag is one, or anything else with the low bit set,
2254 delete the storage. */
86f45d2c 2255 if (auto_delete_vec != sfk_base_destructor)
f30432d7
MS
2256 {
2257 tree base_tbd;
2258
708cae97 2259 /* The below is short by the cookie size. */
fed3cef0
RK
2260 virtual_size = size_binop (MULT_EXPR, size_exp,
2261 convert (sizetype, maxindex));
f30432d7
MS
2262
2263 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2264 /* no header */
2265 base_tbd = base;
2266 else
2267 {
834c6dff
MM
2268 tree cookie_size;
2269
46e995e0 2270 cookie_size = targetm.cxx.get_cookie_size (type);
834c6dff
MM
2271 base_tbd
2272 = cp_convert (ptype,
ab76ca54
MM
2273 cp_build_binary_op (MINUS_EXPR,
2274 cp_convert (string_type_node,
2275 base),
2276 cookie_size));
e92cc029 2277 /* True size with header. */
834c6dff 2278 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
f30432d7 2279 }
86f45d2c
MM
2280
2281 if (auto_delete_vec == sfk_deleting_destructor)
2282 deallocate_expr = build_x_delete (base_tbd,
2283 2 | use_global_delete,
2284 virtual_size);
f30432d7
MS
2285 }
2286
c7b62f14
NS
2287 body = loop;
2288 if (!deallocate_expr)
2289 ;
2290 else if (!body)
2291 body = deallocate_expr;
f30432d7 2292 else
c7b62f14
NS
2293 body = build_compound_expr (body, deallocate_expr);
2294
2295 if (!body)
2296 body = integer_zero_node;
2297
f30432d7 2298 /* Outermost wrapper: If pointer is null, punt. */
f293ce4b
RS
2299 body = fold (build3 (COND_EXPR, void_type_node,
2300 fold (build2 (NE_EXPR, boolean_type_node, base,
2301 convert (TREE_TYPE (base),
2302 integer_zero_node))),
2303 body, integer_zero_node));
f30432d7
MS
2304 body = build1 (NOP_EXPR, void_type_node, body);
2305
2306 if (controller)
2307 {
2308 TREE_OPERAND (controller, 1) = body;
4e8dca1c 2309 body = controller;
f30432d7 2310 }
4e8dca1c
JM
2311
2312 if (TREE_CODE (base) == SAVE_EXPR)
2313 /* Pre-evaluate the SAVE_EXPR outside of the BIND_EXPR. */
f293ce4b 2314 body = build2 (COMPOUND_EXPR, void_type_node, base, body);
4e8dca1c 2315
8d245821 2316 return convert_to_void (body, /*implicit=*/NULL);
f30432d7
MS
2317}
2318
c395453c
MM
2319/* Create an unnamed variable of the indicated TYPE. */
2320
f1dedc31 2321tree
362efdc1 2322create_temporary_var (tree type)
8a72a046 2323{
f1dedc31
MM
2324 tree decl;
2325
2326 decl = build_decl (VAR_DECL, NULL_TREE, type);
2327 TREE_USED (decl) = 1;
2328 DECL_ARTIFICIAL (decl) = 1;
f31686a3 2329 DECL_SOURCE_LOCATION (decl) = input_location;
f1dedc31 2330 DECL_IGNORED_P (decl) = 1;
b35d4555 2331 DECL_CONTEXT (decl) = current_function_decl;
f1dedc31 2332
f1dedc31 2333 return decl;
8a72a046
MM
2334}
2335
f1dedc31
MM
2336/* Create a new temporary variable of the indicated TYPE, initialized
2337 to INIT.
8a72a046 2338
f1dedc31
MM
2339 It is not entered into current_binding_level, because that breaks
2340 things when it comes time to do final cleanups (which take place
2341 "outside" the binding contour of the function). */
2342
2343static tree
362efdc1 2344get_temp_regvar (tree type, tree init)
f30432d7 2345{
f1dedc31 2346 tree decl;
8a72a046 2347
f1dedc31 2348 decl = create_temporary_var (type);
350fae66 2349 add_decl_expr (decl);
2a3398e1 2350
f1dedc31 2351 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
8a72a046 2352
f1dedc31 2353 return decl;
f30432d7
MS
2354}
2355
f1dedc31
MM
2356/* `build_vec_init' returns tree structure that performs
2357 initialization of a vector of aggregate types.
8d08fdba 2358
f30efcb7 2359 BASE is a reference to the vector, of ARRAY_TYPE.
a48cccea
JM
2360 MAXINDEX is the maximum index of the array (one less than the
2361 number of elements). It is only used if
2362 TYPE_DOMAIN (TREE_TYPE (BASE)) == NULL_TREE.
8d08fdba
MS
2363 INIT is the (possibly NULL) initializer.
2364
2365 FROM_ARRAY is 0 if we should init everything with INIT
2366 (i.e., every element initialized from INIT).
2367 FROM_ARRAY is 1 if we should index into INIT in parallel
2368 with initialization of DECL.
2369 FROM_ARRAY is 2 if we should index into INIT in parallel,
2370 but use assignment instead of initialization. */
2371
2372tree
362efdc1 2373build_vec_init (tree base, tree maxindex, tree init, int from_array)
8d08fdba
MS
2374{
2375 tree rval;
8a72a046 2376 tree base2 = NULL_TREE;
8d08fdba 2377 tree size;
e833cb11 2378 tree itype = NULL_TREE;
8a72a046 2379 tree iterator;
f30efcb7
JM
2380 /* The type of the array. */
2381 tree atype = TREE_TYPE (base);
f1dedc31 2382 /* The type of an element in the array. */
f30efcb7 2383 tree type = TREE_TYPE (atype);
f1dedc31
MM
2384 /* The type of a pointer to an element in the array. */
2385 tree ptype;
2386 tree stmt_expr;
2387 tree compound_stmt;
2388 int destroy_temps;
f5984164 2389 tree try_block = NULL_TREE;
8a72a046 2390 int num_initialized_elts = 0;
2a3398e1
NS
2391 bool is_global;
2392
a48cccea
JM
2393 if (TYPE_DOMAIN (atype))
2394 maxindex = array_type_nelts (atype);
2395
2396 if (maxindex == NULL_TREE || maxindex == error_mark_node)
8d08fdba
MS
2397 return error_mark_node;
2398
c8a3d889
AO
2399 if (init
2400 && (from_array == 2
2401 ? (!CLASS_TYPE_P (type) || !TYPE_HAS_COMPLEX_ASSIGN_REF (type))
2402 : !TYPE_NEEDS_CONSTRUCTING (type))
f30efcb7
JM
2403 && ((TREE_CODE (init) == CONSTRUCTOR
2404 /* Don't do this if the CONSTRUCTOR might contain something
2405 that might throw and require us to clean up. */
2406 && (CONSTRUCTOR_ELTS (init) == NULL_TREE
2407 || ! TYPE_HAS_NONTRIVIAL_DESTRUCTOR (target_type (type))))
2408 || from_array))
2409 {
2410 /* Do non-default initialization of POD arrays resulting from
2411 brace-enclosed initializers. In this case, digest_init and
2412 store_constructor will handle the semantics for us. */
2413
f293ce4b 2414 stmt_expr = build2 (INIT_EXPR, atype, base, init);
f30efcb7
JM
2415 return stmt_expr;
2416 }
2417
2418 maxindex = cp_convert (ptrdiff_type_node, maxindex);
f1dedc31 2419 ptype = build_pointer_type (type);
8d08fdba 2420 size = size_in_bytes (type);
f30efcb7 2421 if (TREE_CODE (TREE_TYPE (base)) == ARRAY_TYPE)
0a72704b 2422 base = cp_convert (ptype, decay_conversion (base));
8d08fdba 2423
f1dedc31 2424 /* The code we are generating looks like:
303b7406 2425 ({
f1dedc31 2426 T* t1 = (T*) base;
f30efcb7 2427 T* rval = t1;
f1dedc31
MM
2428 ptrdiff_t iterator = maxindex;
2429 try {
4977bab6 2430 for (; iterator != -1; --iterator) {
f30efcb7
JM
2431 ... initialize *t1 ...
2432 ++t1;
4977bab6 2433 }
f1dedc31
MM
2434 } catch (...) {
2435 ... destroy elements that were constructed ...
2436 }
303b7406
NS
2437 rval;
2438 })
f1dedc31
MM
2439
2440 We can omit the try and catch blocks if we know that the
2441 initialization will never throw an exception, or if the array
f30efcb7 2442 elements do not have destructors. We can omit the loop completely if
f1dedc31
MM
2443 the elements of the array do not have constructors.
2444
2445 We actually wrap the entire body of the above in a STMT_EXPR, for
2446 tidiness.
2447
2448 When copying from array to another, when the array elements have
2449 only trivial copy constructors, we should use __builtin_memcpy
2450 rather than generating a loop. That way, we could take advantage
2451 of whatever cleverness the back-end has for dealing with copies
2452 of blocks of memory. */
2453
2a3398e1 2454 is_global = begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623 2455 destroy_temps = stmts_are_full_exprs_p ();
ae499cce 2456 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
f30efcb7 2457 rval = get_temp_regvar (ptype, base);
f1dedc31 2458 base = get_temp_regvar (ptype, rval);
8a72a046 2459 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
8d08fdba 2460
8a72a046 2461 /* Protect the entire array initialization so that we can destroy
f30efcb7
JM
2462 the partially constructed array if an exception is thrown.
2463 But don't do this if we're assigning. */
2464 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2465 && from_array != 2)
ed5511d9
MM
2466 {
2467 try_block = begin_try_block ();
ed5511d9 2468 }
8a72a046 2469
f30efcb7 2470 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR)
8d08fdba 2471 {
f30efcb7
JM
2472 /* Do non-default initialization of non-POD arrays resulting from
2473 brace-enclosed initializers. */
8a72a046
MM
2474
2475 tree elts;
094fe153
JM
2476 from_array = 0;
2477
8a72a046 2478 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
8d08fdba 2479 {
8a72a046 2480 tree elt = TREE_VALUE (elts);
f1dedc31 2481 tree baseref = build1 (INDIRECT_REF, type, base);
8d08fdba 2482
8a72a046 2483 num_initialized_elts++;
8d08fdba 2484
67c03833 2485 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
8a72a046 2486 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
f1dedc31 2487 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
8a72a046 2488 else
f1dedc31
MM
2489 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2490 elt));
67c03833 2491 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8a72a046 2492
f30efcb7
JM
2493 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
2494 finish_expr_stmt (build_unary_op (PREDECREMENT_EXPR, iterator, 0));
8d08fdba 2495 }
8d08fdba 2496
8a72a046
MM
2497 /* Clear out INIT so that we don't get confused below. */
2498 init = NULL_TREE;
8d08fdba 2499 }
8a72a046 2500 else if (from_array)
8d08fdba 2501 {
8a72a046
MM
2502 /* If initializing one array from another, initialize element by
2503 element. We rely upon the below calls the do argument
2504 checking. */
8a72a046
MM
2505 if (init)
2506 {
0a72704b 2507 base2 = decay_conversion (init);
8a72a046
MM
2508 itype = TREE_TYPE (base2);
2509 base2 = get_temp_regvar (itype, base2);
2510 itype = TREE_TYPE (itype);
2511 }
2512 else if (TYPE_LANG_SPECIFIC (type)
2513 && TYPE_NEEDS_CONSTRUCTING (type)
2514 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2515 {
2516 error ("initializer ends prematurely");
2517 return error_mark_node;
2518 }
2519 }
8d08fdba 2520
8a72a046
MM
2521 /* Now, default-initialize any remaining elements. We don't need to
2522 do that if a) the type does not need constructing, or b) we've
094fe153
JM
2523 already initialized all the elements.
2524
2525 We do need to keep going if we're copying an array. */
2526
2527 if (from_array
2528 || (TYPE_NEEDS_CONSTRUCTING (type)
665f2503 2529 && ! (host_integerp (maxindex, 0)
05bccae2 2530 && (num_initialized_elts
665f2503 2531 == tree_low_cst (maxindex, 0) + 1))))
8a72a046 2532 {
37e05cd5 2533 /* If the ITERATOR is equal to -1, then we don't have to loop;
8a72a046 2534 we've already initialized all the elements. */
4977bab6 2535 tree for_stmt;
f1dedc31
MM
2536 tree elt_init;
2537
4977bab6
ZW
2538 for_stmt = begin_for_stmt ();
2539 finish_for_init_stmt (for_stmt);
f293ce4b
RS
2540 finish_for_cond (build2 (NE_EXPR, boolean_type_node,
2541 iterator, integer_minus_one_node),
4977bab6
ZW
2542 for_stmt);
2543 finish_for_expr (build_unary_op (PREDECREMENT_EXPR, iterator, 0),
2544 for_stmt);
8d08fdba
MS
2545
2546 if (from_array)
2547 {
2548 tree to = build1 (INDIRECT_REF, type, base);
2549 tree from;
2550
2551 if (base2)
2552 from = build1 (INDIRECT_REF, itype, base2);
2553 else
2554 from = NULL_TREE;
2555
2556 if (from_array == 2)
f1dedc31 2557 elt_init = build_modify_expr (to, NOP_EXPR, from);
8d08fdba 2558 else if (TYPE_NEEDS_CONSTRUCTING (type))
f1dedc31 2559 elt_init = build_aggr_init (to, from, 0);
8d08fdba 2560 else if (from)
f1dedc31 2561 elt_init = build_modify_expr (to, NOP_EXPR, from);
8d08fdba 2562 else
a98facb0 2563 abort ();
8d08fdba
MS
2564 }
2565 else if (TREE_CODE (type) == ARRAY_TYPE)
2566 {
2567 if (init != 0)
f30efcb7
JM
2568 sorry
2569 ("cannot initialize multi-dimensional array with initializer");
2570 elt_init = build_vec_init (build1 (INDIRECT_REF, type, base),
a48cccea 2571 0, 0, 0);
f1dedc31
MM
2572 }
2573 else
2574 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2575 init, 0);
2576
2a3398e1
NS
2577 current_stmt_tree ()->stmts_are_full_exprs_p = 1;
2578 finish_expr_stmt (elt_init);
2579 current_stmt_tree ()->stmts_are_full_exprs_p = 0;
8d08fdba 2580
f30efcb7 2581 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base, 0));
8d08fdba 2582 if (base2)
f30efcb7 2583 finish_expr_stmt (build_unary_op (PREINCREMENT_EXPR, base2, 0));
0fac6b0b 2584
4977bab6 2585 finish_for_stmt (for_stmt);
8d08fdba 2586 }
8a72a046
MM
2587
2588 /* Make sure to cleanup any partially constructed elements. */
f30efcb7
JM
2589 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type)
2590 && from_array != 2)
f1dedc31
MM
2591 {
2592 tree e;
b2153b98
KL
2593 tree m = cp_build_binary_op (MINUS_EXPR, maxindex, iterator);
2594
2595 /* Flatten multi-dimensional array since build_vec_delete only
2596 expects one-dimensional array. */
2597 if (TREE_CODE (type) == ARRAY_TYPE)
2598 {
2599 m = cp_build_binary_op (MULT_EXPR, m,
2600 array_type_nelts_total (type));
2601 type = strip_array_types (type);
2602 }
8d08fdba 2603
ed5511d9 2604 finish_cleanup_try_block (try_block);
303b7406 2605 e = build_vec_delete_1 (rval, m, type, sfk_base_destructor,
f1dedc31 2606 /*use_global_delete=*/0);
f1dedc31
MM
2607 finish_cleanup (e, try_block);
2608 }
2609
303b7406
NS
2610 /* The value of the array initialization is the array itself, RVAL
2611 is a pointer to the first element. */
325c3691 2612 finish_stmt_expr_expr (rval, stmt_expr);
f1dedc31 2613
2a3398e1 2614 stmt_expr = finish_init_stmts (is_global, stmt_expr, compound_stmt);
303b7406 2615
04c06002 2616 /* Now convert make the result have the correct type. */
303b7406
NS
2617 atype = build_pointer_type (atype);
2618 stmt_expr = build1 (NOP_EXPR, atype, stmt_expr);
2619 stmt_expr = build_indirect_ref (stmt_expr, NULL);
2620
ae499cce 2621 current_stmt_tree ()->stmts_are_full_exprs_p = destroy_temps;
f1dedc31 2622 return stmt_expr;
8d08fdba
MS
2623}
2624
2625/* Free up storage of type TYPE, at address ADDR.
2626
2627 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
2628 of pointer.
2629
2630 VIRTUAL_SIZE is the amount of storage that was allocated, and is
2631 used as the second argument to operator delete. It can include
2632 things like padding and magic size cookies. It has virtual in it,
2633 because if you have a base pointer and you delete through a virtual
2634 destructor, it should be the size of the dynamic object, not the
cab1f180 2635 static object, see Free Store 12.5 ISO C++.
8d08fdba
MS
2636
2637 This does not call any destructors. */
e92cc029 2638
8d08fdba 2639tree
362efdc1 2640build_x_delete (tree addr, int which_delete, tree virtual_size)
8d08fdba 2641{
a28e3c7f
MS
2642 int use_global_delete = which_delete & 1;
2643 int use_vec_delete = !!(which_delete & 2);
a28e3c7f 2644 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
8d08fdba 2645
5bd61841
MM
2646 return build_op_delete_call (code, addr, virtual_size, use_global_delete,
2647 NULL_TREE);
8d08fdba
MS
2648}
2649
86f45d2c
MM
2650/* Call the DTOR_KIND destructor for EXP. FLAGS are as for
2651 build_delete. */
298d6f60
MM
2652
2653static tree
362efdc1 2654build_dtor_call (tree exp, special_function_kind dtor_kind, int flags)
298d6f60 2655{
86f45d2c 2656 tree name;
ee76b931 2657 tree fn;
86f45d2c
MM
2658 switch (dtor_kind)
2659 {
2660 case sfk_complete_destructor:
2661 name = complete_dtor_identifier;
2662 break;
2663
2664 case sfk_base_destructor:
2665 name = base_dtor_identifier;
2666 break;
2667
2668 case sfk_deleting_destructor:
2669 name = deleting_dtor_identifier;
2670 break;
2671
2672 default:
a98facb0 2673 abort ();
86f45d2c 2674 }
ee76b931
MM
2675
2676 exp = convert_from_reference (exp);
2677 fn = lookup_fnfields (TREE_TYPE (exp), name, /*protect=*/2);
2678 return build_new_method_call (exp, fn,
2679 /*args=*/NULL_TREE,
2680 /*conversion_path=*/NULL_TREE,
2681 flags);
298d6f60
MM
2682}
2683
8d08fdba
MS
2684/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
2685 ADDR is an expression which yields the store to be destroyed.
86f45d2c
MM
2686 AUTO_DELETE is the name of the destructor to call, i.e., either
2687 sfk_complete_destructor, sfk_base_destructor, or
2688 sfk_deleting_destructor.
8d08fdba
MS
2689
2690 FLAGS is the logical disjunction of zero or more LOOKUP_
ade3dc07 2691 flags. See cp-tree.h for more info. */
e92cc029 2692
8d08fdba 2693tree
362efdc1
NN
2694build_delete (tree type, tree addr, special_function_kind auto_delete,
2695 int flags, int use_global_delete)
8d08fdba 2696{
8d08fdba 2697 tree expr;
8d08fdba
MS
2698
2699 if (addr == error_mark_node)
2700 return error_mark_node;
2701
2702 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
2703 set to `error_mark_node' before it gets properly cleaned up. */
2704 if (type == error_mark_node)
2705 return error_mark_node;
2706
2707 type = TYPE_MAIN_VARIANT (type);
2708
2709 if (TREE_CODE (type) == POINTER_TYPE)
2710 {
b1e5b86c
GB
2711 bool complete_p = true;
2712
2986ae00 2713 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
8d08fdba
MS
2714 if (TREE_CODE (type) == ARRAY_TYPE)
2715 goto handle_array;
23b4deba 2716
b1e5b86c
GB
2717 /* We don't want to warn about delete of void*, only other
2718 incomplete types. Deleting other incomplete types
2719 invokes undefined behavior, but it is not ill-formed, so
2720 compile to something that would even do The Right Thing
2721 (TM) should the type have a trivial dtor and no delete
2722 operator. */
2723 if (!VOID_TYPE_P (type))
8d08fdba 2724 {
b1e5b86c
GB
2725 complete_type (type);
2726 if (!COMPLETE_TYPE_P (type))
2727 {
2728 warning ("possible problem detected in invocation of "
2729 "delete operator:");
2730 cxx_incomplete_type_diagnostic (addr, type, 1);
061cae1f
NS
2731 inform ("neither the destructor nor the class-specific "
2732 "operator delete will be called, even if they are "
b1e5b86c
GB
2733 "declared when the class is defined.");
2734 complete_p = false;
2735 }
8d08fdba 2736 }
b1e5b86c
GB
2737 if (VOID_TYPE_P (type) || !complete_p || !IS_AGGR_TYPE (type))
2738 /* Call the builtin operator delete. */
2739 return build_builtin_delete_call (addr);
8d08fdba
MS
2740 if (TREE_SIDE_EFFECTS (addr))
2741 addr = save_expr (addr);
2986ae00 2742
f4f206f4 2743 /* Throw away const and volatile on target type of addr. */
6060a796 2744 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba
MS
2745 }
2746 else if (TREE_CODE (type) == ARRAY_TYPE)
2747 {
2748 handle_array:
6742d92b 2749
c407792d
RK
2750 if (TYPE_DOMAIN (type) == NULL_TREE)
2751 {
8251199e 2752 error ("unknown array size in delete");
c407792d
RK
2753 return error_mark_node;
2754 }
8d08fdba 2755 return build_vec_delete (addr, array_type_nelts (type),
c7edeea3 2756 auto_delete, use_global_delete);
8d08fdba
MS
2757 }
2758 else
2759 {
2760 /* Don't check PROTECT here; leave that decision to the
2761 destructor. If the destructor is accessible, call it,
2762 else report error. */
2763 addr = build_unary_op (ADDR_EXPR, addr, 0);
2764 if (TREE_SIDE_EFFECTS (addr))
2765 addr = save_expr (addr);
2766
60696c53 2767 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba
MS
2768 }
2769
50bc768d 2770 gcc_assert (IS_AGGR_TYPE (type));
8d08fdba 2771
834c6dff 2772 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8d08fdba 2773 {
60696c53 2774 if (auto_delete != sfk_deleting_destructor)
8d08fdba
MS
2775 return void_zero_node;
2776
da4768fe 2777 return build_op_delete_call
5bd61841 2778 (DELETE_EXPR, addr, cxx_sizeof_nowarn (type), use_global_delete,
519ebd1e 2779 NULL_TREE);
8d08fdba 2780 }
ade3dc07 2781 else
8d08fdba 2782 {
700f8a87 2783 tree do_delete = NULL_TREE;
bd6dd845 2784 tree ifexp;
700f8a87 2785
50bc768d 2786 gcc_assert (TYPE_HAS_DESTRUCTOR (type));
ade3dc07 2787
52682a1b
MM
2788 /* For `::delete x', we must not use the deleting destructor
2789 since then we would not be sure to get the global `operator
2790 delete'. */
86f45d2c 2791 if (use_global_delete && auto_delete == sfk_deleting_destructor)
700f8a87 2792 {
1b4a93f7
MM
2793 /* We will use ADDR multiple times so we must save it. */
2794 addr = save_expr (addr);
c6002625 2795 /* Delete the object. */
86f45d2c
MM
2796 do_delete = build_builtin_delete_call (addr);
2797 /* Otherwise, treat this like a complete object destructor
2798 call. */
2799 auto_delete = sfk_complete_destructor;
700f8a87 2800 }
52682a1b
MM
2801 /* If the destructor is non-virtual, there is no deleting
2802 variant. Instead, we must explicitly call the appropriate
2803 `operator delete' here. */
2804 else if (!DECL_VIRTUAL_P (CLASSTYPE_DESTRUCTORS (type))
2805 && auto_delete == sfk_deleting_destructor)
2806 {
1b4a93f7
MM
2807 /* We will use ADDR multiple times so we must save it. */
2808 addr = save_expr (addr);
2809 /* Build the call. */
52682a1b
MM
2810 do_delete = build_op_delete_call (DELETE_EXPR,
2811 addr,
ea793912 2812 cxx_sizeof_nowarn (type),
5bd61841 2813 /*global_p=*/false,
52682a1b
MM
2814 NULL_TREE);
2815 /* Call the complete object destructor. */
2816 auto_delete = sfk_complete_destructor;
2817 }
e3fe84e5
JM
2818 else if (auto_delete == sfk_deleting_destructor
2819 && TYPE_GETS_REG_DELETE (type))
2820 {
2821 /* Make sure we have access to the member op delete, even though
2822 we'll actually be calling it from the destructor. */
ea793912 2823 build_op_delete_call (DELETE_EXPR, addr, cxx_sizeof_nowarn (type),
5bd61841 2824 /*global_p=*/false, NULL_TREE);
e3fe84e5 2825 }
8d08fdba 2826
3e411c3f 2827 expr = build_dtor_call (build_indirect_ref (addr, NULL),
1b4a93f7 2828 auto_delete, flags);
bd6dd845 2829 if (do_delete)
f293ce4b 2830 expr = build2 (COMPOUND_EXPR, void_type_node, expr, do_delete);
9e9ff709 2831
bd6dd845
MS
2832 if (flags & LOOKUP_DESTRUCTOR)
2833 /* Explicit destructor call; don't check for null pointer. */
2834 ifexp = integer_one_node;
8d08fdba 2835 else
bd6dd845 2836 /* Handle deleting a null pointer. */
ab76ca54 2837 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
8d08fdba 2838
bd6dd845 2839 if (ifexp != integer_one_node)
f293ce4b
RS
2840 expr = build3 (COND_EXPR, void_type_node,
2841 ifexp, expr, void_zero_node);
8d08fdba 2842
8d08fdba
MS
2843 return expr;
2844 }
ade3dc07 2845}
8d08fdba 2846
ade3dc07
JM
2847/* At the beginning of a destructor, push cleanups that will call the
2848 destructors for our base classes and members.
2a2480e1 2849
a29e1034 2850 Called from begin_destructor_body. */
8d08fdba 2851
ade3dc07 2852void
edaf3e03 2853push_base_cleanups (void)
ade3dc07 2854{
fa743e8c
NS
2855 tree binfo, base_binfo;
2856 int i;
ade3dc07
JM
2857 tree member;
2858 tree expr;
9ba5ff0f 2859 VEC (tree) *vbases;
8d08fdba 2860
ade3dc07
JM
2861 /* Run destructors for all virtual baseclasses. */
2862 if (TYPE_USES_VIRTUAL_BASECLASSES (current_class_type))
2863 {
ade3dc07 2864 tree cond = (condition_conversion
f293ce4b
RS
2865 (build2 (BIT_AND_EXPR, integer_type_node,
2866 current_in_charge_parm,
2867 integer_two_node)));
8d08fdba 2868
58c42dc2 2869 /* The CLASSTYPE_VBASECLASSES vector is in initialization
ade3dc07 2870 order, which is also the right order for pushing cleanups. */
9ba5ff0f
NS
2871 for (vbases = CLASSTYPE_VBASECLASSES (current_class_type), i = 0;
2872 VEC_iterate (tree, vbases, i, base_binfo); i++)
8d08fdba 2873 {
9ba5ff0f 2874 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
8d08fdba 2875 {
4ba126e4
MM
2876 expr = build_special_member_call (current_class_ref,
2877 base_dtor_identifier,
2878 NULL_TREE,
9ba5ff0f 2879 base_binfo,
4ba126e4
MM
2880 (LOOKUP_NORMAL
2881 | LOOKUP_NONVIRTUAL));
f293ce4b
RS
2882 expr = build3 (COND_EXPR, void_type_node, cond,
2883 expr, void_zero_node);
ade3dc07 2884 finish_decl_cleanup (NULL_TREE, expr);
8d08fdba
MS
2885 }
2886 }
ade3dc07
JM
2887 }
2888
ade3dc07 2889 /* Take care of the remaining baseclasses. */
fa743e8c
NS
2890 for (binfo = TYPE_BINFO (current_class_type), i = 0;
2891 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
ade3dc07 2892 {
ade3dc07 2893 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
809e3e7f 2894 || BINFO_VIRTUAL_P (base_binfo))
ade3dc07
JM
2895 continue;
2896
4ba126e4
MM
2897 expr = build_special_member_call (current_class_ref,
2898 base_dtor_identifier,
2899 NULL_TREE, base_binfo,
2900 LOOKUP_NORMAL | LOOKUP_NONVIRTUAL);
ade3dc07
JM
2901 finish_decl_cleanup (NULL_TREE, expr);
2902 }
2903
2904 for (member = TYPE_FIELDS (current_class_type); member;
2905 member = TREE_CHAIN (member))
2906 {
17bbb839 2907 if (TREE_CODE (member) != FIELD_DECL || DECL_ARTIFICIAL (member))
ade3dc07
JM
2908 continue;
2909 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
2910 {
50ad9642
MM
2911 tree this_member = (build_class_member_access_expr
2912 (current_class_ref, member,
2913 /*access_path=*/NULL_TREE,
2914 /*preserve_reference=*/false));
ade3dc07
JM
2915 tree this_type = TREE_TYPE (member);
2916 expr = build_delete (this_type, this_member,
2917 sfk_complete_destructor,
2918 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR|LOOKUP_NORMAL,
2919 0);
2920 finish_decl_cleanup (NULL_TREE, expr);
2921 }
8d08fdba
MS
2922 }
2923}
2924
2925/* For type TYPE, delete the virtual baseclass objects of DECL. */
2926
2927tree
362efdc1 2928build_vbase_delete (tree type, tree decl)
8d08fdba 2929{
58c42dc2
NS
2930 unsigned ix;
2931 tree binfo;
c7b62f14 2932 tree result;
9ba5ff0f 2933 VEC (tree) *vbases;
8d08fdba
MS
2934 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
2935
50bc768d 2936 gcc_assert (addr != error_mark_node);
8d08fdba 2937
58c42dc2 2938 result = convert_to_void (integer_zero_node, NULL);
9ba5ff0f
NS
2939 for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
2940 VEC_iterate (tree, vbases, ix, binfo); ix++)
8d08fdba 2941 {
c7b62f14 2942 tree base_addr = convert_force
58c42dc2 2943 (build_pointer_type (BINFO_TYPE (binfo)), addr, 0);
c7b62f14
NS
2944 tree base_delete = build_delete
2945 (TREE_TYPE (base_addr), base_addr, sfk_base_destructor,
2946 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0);
2947
2948 result = build_compound_expr (result, base_delete);
8d08fdba 2949 }
c7b62f14 2950 return result;
8d08fdba
MS
2951}
2952
2953/* Build a C++ vector delete expression.
2954 MAXINDEX is the number of elements to be deleted.
2955 ELT_SIZE is the nominal size of each element in the vector.
2956 BASE is the expression that should yield the store to be deleted.
8d08fdba
MS
2957 This function expands (or synthesizes) these calls itself.
2958 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
8d08fdba
MS
2959
2960 This also calls delete for virtual baseclasses of elements of the vector.
2961
2962 Update: MAXINDEX is no longer needed. The size can be extracted from the
2963 start of the vector for pointers, and from the type for arrays. We still
2964 use MAXINDEX for arrays because it happens to already have one of the
2965 values we'd have to extract. (We could use MAXINDEX with pointers to
2966 confirm the size, and trap if the numbers differ; not clear that it'd
2967 be worth bothering.) */
e92cc029 2968
8d08fdba 2969tree
362efdc1
NN
2970build_vec_delete (tree base, tree maxindex,
2971 special_function_kind auto_delete_vec, int use_global_delete)
8d08fdba 2972{
f30432d7 2973 tree type;
49b7aacb
JM
2974 tree rval;
2975 tree base_init = NULL_TREE;
8d08fdba 2976
f30432d7 2977 type = TREE_TYPE (base);
c407792d 2978
f30432d7 2979 if (TREE_CODE (type) == POINTER_TYPE)
8d08fdba
MS
2980 {
2981 /* Step back one from start of vector, and read dimension. */
834c6dff
MM
2982 tree cookie_addr;
2983
6742d92b 2984 if (TREE_SIDE_EFFECTS (base))
49b7aacb
JM
2985 {
2986 base_init = get_target_expr (base);
2987 base = TARGET_EXPR_SLOT (base_init);
2988 }
708cae97 2989 type = strip_array_types (TREE_TYPE (type));
f293ce4b
RS
2990 cookie_addr = build2 (MINUS_EXPR,
2991 build_pointer_type (sizetype),
2992 base,
2993 TYPE_SIZE_UNIT (sizetype));
3e411c3f 2994 maxindex = build_indirect_ref (cookie_addr, NULL);
8d08fdba 2995 }
f30432d7 2996 else if (TREE_CODE (type) == ARRAY_TYPE)
8d08fdba 2997 {
f4f206f4
KH
2998 /* Get the total number of things in the array, maxindex is a
2999 bad name. */
f30432d7 3000 maxindex = array_type_nelts_total (type);
834c6dff 3001 type = strip_array_types (type);
8d08fdba 3002 base = build_unary_op (ADDR_EXPR, base, 1);
6742d92b 3003 if (TREE_SIDE_EFFECTS (base))
49b7aacb
JM
3004 {
3005 base_init = get_target_expr (base);
3006 base = TARGET_EXPR_SLOT (base_init);
3007 }
8d08fdba
MS
3008 }
3009 else
3010 {
9e9ff709 3011 if (base != error_mark_node)
8251199e 3012 error ("type to vector delete is neither pointer or array type");
8d08fdba
MS
3013 return error_mark_node;
3014 }
8d08fdba 3015
49b7aacb 3016 rval = build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
f30432d7 3017 use_global_delete);
49b7aacb 3018 if (base_init)
f293ce4b 3019 rval = build2 (COMPOUND_EXPR, TREE_TYPE (rval), base_init, rval);
49b7aacb
JM
3020
3021 return rval;
8d08fdba 3022}