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