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