]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cp/init.c
* flow.c (insn_dead_p): Detect dead memory stores with auto increments.
[thirdparty/gcc.git] / gcc / cp / init.c
CommitLineData
8d08fdba 1/* Handle initialization things in C++.
d6a8bdff
JL
2 Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
8d08fdba
MS
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6This file is part of GNU CC.
7
8GNU CC is free software; you can redistribute it and/or modify
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
13GNU CC is distributed in the hope that it will be useful,
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
19along with GNU CC; 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"
8d08fdba
MS
27#include "tree.h"
28#include "rtl.h"
29#include "cp-tree.h"
30#include "flags.h"
e8abc66f 31#include "output.h"
eb66be0e 32#include "except.h"
9f617717 33#include "expr.h"
54f92bfb 34#include "toplev.h"
fc6af6e3 35#include "ggc.h"
8d08fdba 36
158991b7
KG
37static void expand_aggr_vbase_init_1 PARAMS ((tree, tree, tree, tree));
38static void construct_virtual_bases PARAMS ((tree, tree, tree, tree, tree));
39static void expand_aggr_init_1 PARAMS ((tree, tree, tree, tree, int));
40static void expand_default_init PARAMS ((tree, tree, tree, tree, int));
86f45d2c 41static tree build_vec_delete_1 PARAMS ((tree, tree, tree, special_function_kind, int));
ff9f1a5d 42static void perform_member_init PARAMS ((tree, tree, int));
fd74ca0b 43static void sort_base_init PARAMS ((tree, tree, tree *, tree *));
158991b7
KG
44static tree build_builtin_delete_call PARAMS ((tree));
45static int member_init_ok_or_else PARAMS ((tree, tree, const char *));
46static void expand_virtual_init PARAMS ((tree, tree));
fd74ca0b 47static tree sort_member_init PARAMS ((tree, tree));
158991b7 48static tree initializing_context PARAMS ((tree));
158991b7
KG
49static void expand_cleanup_for_base PARAMS ((tree, tree));
50static tree get_temp_regvar PARAMS ((tree, tree));
51static tree dfs_initialize_vtbl_ptrs PARAMS ((tree, void *));
834c6dff
MM
52static tree build_new_1 PARAMS ((tree));
53static tree get_cookie_size PARAMS ((tree));
86f45d2c 54static tree build_dtor_call PARAMS ((tree, special_function_kind, int));
ff9f1a5d 55static tree build_field_list PARAMS ((tree, tree, int *));
3ec6bad3 56static tree build_vtbl_address PARAMS ((tree));
8d08fdba 57
8d08fdba
MS
58/* Set up local variable for this file. MUST BE CALLED AFTER
59 INIT_DECL_PROCESSING. */
60
708cae97 61static tree BI_header_type;
8d08fdba
MS
62
63void init_init_processing ()
64{
65 tree fields[1];
66
9cd64686 67 minus_one_node = build_int_2 (-1, -1);
8d08fdba
MS
68
69 /* Define the structure that holds header information for
70 arrays allocated via operator new. */
33848bb0 71 BI_header_type = make_aggr_type (RECORD_TYPE);
721c3b42 72 fields[0] = build_decl (FIELD_DECL, nelts_identifier, sizetype);
3ca7341a 73
8d08fdba 74 finish_builtin_type (BI_header_type, "__new_cookie", fields,
708cae97 75 0, double_type_node);
fc6af6e3 76
9cd64686 77 ggc_add_tree_root (&BI_header_type, 1);
8d08fdba
MS
78}
79
3dbc07b6
MM
80/* We are about to generate some complex initialization code.
81 Conceptually, it is all a single expression. However, we may want
82 to include conditionals, loops, and other such statement-level
83 constructs. Therefore, we build the initialization code inside a
84 statement-expression. This function starts such an expression.
85 STMT_EXPR_P and COMPOUND_STMT_P are filled in by this function;
86 pass them back to finish_init_stmts when the expression is
87 complete. */
88
89void
90begin_init_stmts (stmt_expr_p, compound_stmt_p)
91 tree *stmt_expr_p;
92 tree *compound_stmt_p;
93{
35b1567d
BC
94 if (building_stmt_tree ())
95 *stmt_expr_p = begin_stmt_expr ();
96 else
596fd31c 97 *stmt_expr_p = begin_global_stmt_expr ();
35b1567d
BC
98
99 if (building_stmt_tree ())
100 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
4cf88f57
BC
101 // else
102 // *compound_stmt_p = genrtl_begin_compound_stmt (has_no_scope=1);
3dbc07b6
MM
103}
104
105/* Finish out the statement-expression begun by the previous call to
106 begin_init_stmts. Returns the statement-expression itself. */
107
108tree
109finish_init_stmts (stmt_expr, compound_stmt)
110 tree stmt_expr;
111 tree compound_stmt;
3dbc07b6 112
35b1567d
BC
113{
114 if (building_stmt_tree ())
115 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
35b1567d
BC
116
117 if (building_stmt_tree ())
118 stmt_expr = finish_stmt_expr (stmt_expr);
119 else
596fd31c 120 stmt_expr = finish_global_stmt_expr (stmt_expr);
35b1567d 121
3dbc07b6
MM
122 /* To avoid spurious warnings about unused values, we set
123 TREE_USED. */
124 if (stmt_expr)
125 TREE_USED (stmt_expr) = 1;
126
127 return stmt_expr;
128}
129
130/* Constructors */
131
d569399b 132/* Called from initialize_vtbl_ptrs via dfs_walk. */
7177d104 133
d569399b
MM
134static tree
135dfs_initialize_vtbl_ptrs (binfo, data)
136 tree binfo;
137 void *data;
138{
139 if (!BINFO_PRIMARY_MARKED_P (binfo)
140 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
141 {
142 tree base_ptr = TREE_VALUE ((tree) data);
7177d104 143
d569399b
MM
144 if (TREE_VIA_VIRTUAL (binfo))
145 base_ptr = convert_pointer_to_vbase (BINFO_TYPE (binfo),
146 base_ptr);
147 else
148 base_ptr
149 = build_vbase_path (PLUS_EXPR,
150 build_pointer_type (BINFO_TYPE (binfo)),
151 base_ptr,
152 binfo,
153 /*nonnull=*/1);
154
155 expand_virtual_init (binfo, base_ptr);
156 }
7177d104 157
d569399b
MM
158 SET_BINFO_MARKED (binfo);
159
160 return NULL_TREE;
161}
162
cf2e003b
MM
163/* Initialize all the vtable pointers in the object pointed to by
164 ADDR. */
e92cc029 165
8d08fdba 166void
cf2e003b 167initialize_vtbl_ptrs (addr)
d569399b 168 tree addr;
8d08fdba 169{
cf2e003b
MM
170 tree list;
171 tree type;
172
173 type = TREE_TYPE (TREE_TYPE (addr));
174 list = build_tree_list (type, addr);
d569399b 175
bbd15aac
MM
176 /* Walk through the hierarchy, initializing the vptr in each base
177 class. We do these in pre-order because under the new ABI we
178 can't find the virtual bases for a class until we've initialized
179 the vtbl for that class. */
180 dfs_walk_real (TYPE_BINFO (type), dfs_initialize_vtbl_ptrs,
181 NULL, dfs_unmarked_real_bases_queue_p, list);
d569399b
MM
182 dfs_walk (TYPE_BINFO (type), dfs_unmark,
183 dfs_marked_real_bases_queue_p, type);
3ec6bad3
MM
184
185 /* If we're not using thunks, we may need to adjust the deltas in
186 the vtable to handle virtual base classes correctly. When we are
187 using thunks, we either use construction vtables (which are
188 preloaded with the right answers) or nothing (in which case
189 vitual function calls sometimes don't work right.) */
190 if (TYPE_USES_VIRTUAL_BASECLASSES (type) && !flag_vtable_thunks)
191 fixup_all_virtual_upcast_offsets (addr);
8d08fdba 192}
d569399b 193
8d08fdba 194/* Subroutine of emit_base_init. */
e92cc029 195
8d08fdba 196static void
ff9f1a5d
MM
197perform_member_init (member, init, explicit)
198 tree member, init;
8d08fdba
MS
199 int explicit;
200{
201 tree decl;
202 tree type = TREE_TYPE (member);
eb66be0e 203
ff9f1a5d 204 decl = build_component_ref (current_class_ref, member, NULL_TREE, explicit);
6bdb8141 205
2fbfe9b8
MS
206 if (decl == error_mark_node)
207 return;
208
6bdb8141
JM
209 /* Deal with this here, as we will get confused if we try to call the
210 assignment op for an anonymous union. This can happen in a
211 synthesized copy constructor. */
212 if (ANON_AGGR_TYPE_P (type))
213 {
ff9f1a5d
MM
214 if (init)
215 {
216 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
217 finish_expr_stmt (init);
218 }
6bdb8141
JM
219 }
220 else if (TYPE_NEEDS_CONSTRUCTING (type)
221 || (init && TYPE_HAS_CONSTRUCTOR (type)))
8d08fdba 222 {
fd74ca0b 223 /* Since `init' is already a TREE_LIST on the member_init_list,
8d08fdba
MS
224 only build it into one if we aren't already a list. */
225 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
051e6fd7 226 init = build_tree_list (NULL_TREE, init);
8d08fdba 227
8d08fdba
MS
228 if (explicit
229 && TREE_CODE (type) == ARRAY_TYPE
230 && init != NULL_TREE
231 && TREE_CHAIN (init) == NULL_TREE
232 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
233 {
234 /* Initialization of one array from another. */
f1dedc31
MM
235 finish_expr_stmt
236 (build_vec_init (TREE_OPERAND (decl, 1), decl,
237 array_type_nelts (type), TREE_VALUE (init), 1));
8d08fdba
MS
238 }
239 else
f1dedc31 240 finish_expr_stmt (build_aggr_init (decl, init, 0));
8d08fdba
MS
241 }
242 else
243 {
244 if (init == NULL_TREE)
245 {
246 if (explicit)
247 {
02020185
JM
248 /* default-initialization. */
249 if (AGGREGATE_TYPE_P (type))
316d4b54 250 {
4f0aa416
MM
251 /* This is a default initialization of an aggregate,
252 but not one of non-POD class type. We cleverly
253 notice that the initialization rules in such a
254 case are the same as for initialization with an
255 empty brace-initialization list. We don't want
256 to call build_modify_expr as that will go looking
257 for constructors and such. */
258 tree e = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
259 TREE_SIDE_EFFECTS (e) = 1;
f1dedc31 260 finish_expr_stmt (build (INIT_EXPR, type, decl, e));
316d4b54 261 }
4f0aa416
MM
262 else if (TREE_CODE (type) == REFERENCE_TYPE)
263 cp_error ("default-initialization of `%#D', which has reference type",
264 member);
02020185
JM
265 else
266 init = integer_zero_node;
8d08fdba
MS
267 }
268 /* member traversal: note it leaves init NULL */
269 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
8251199e 270 cp_pedwarn ("uninitialized reference member `%D'", member);
8d08fdba
MS
271 }
272 else if (TREE_CODE (init) == TREE_LIST)
273 {
274 /* There was an explicit member initialization. Do some
275 work in that case. */
276 if (TREE_CHAIN (init))
277 {
8251199e 278 warning ("initializer list treated as compound expression");
8d08fdba
MS
279 init = build_compound_expr (init);
280 }
281 else
282 init = TREE_VALUE (init);
283 }
284
4f0aa416 285 if (init)
f1dedc31 286 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
8d08fdba 287 }
eb66be0e 288
834c6dff 289 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
b7484fbe 290 {
de22184b
MS
291 tree expr;
292
ff9f1a5d 293 expr = build_component_ref (current_class_ref, member, NULL_TREE,
02020185 294 explicit);
3ec6bad3 295 expr = build_delete (type, expr, sfk_complete_destructor,
b7484fbe
MS
296 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
297
298 if (expr != error_mark_node)
f1dedc31 299 finish_subobject (expr);
b7484fbe 300 }
8d08fdba
MS
301}
302
ff9f1a5d
MM
303/* Returns a TREE_LIST containing (as the TREE_PURPOSE of each node) all
304 the FIELD_DECLs on the TYPE_FIELDS list for T, in reverse order. */
305
306static tree
307build_field_list (t, list, uses_unions_p)
308 tree t;
309 tree list;
310 int *uses_unions_p;
311{
312 tree fields;
313
314 /* Note whether or not T is a union. */
315 if (TREE_CODE (t) == UNION_TYPE)
316 *uses_unions_p = 1;
317
318 for (fields = TYPE_FIELDS (t); fields; fields = TREE_CHAIN (fields))
319 {
320 /* Skip CONST_DECLs for enumeration constants and so forth. */
321 if (TREE_CODE (fields) != FIELD_DECL)
322 continue;
323
324 /* Keep track of whether or not any fields are unions. */
325 if (TREE_CODE (TREE_TYPE (fields)) == UNION_TYPE)
326 *uses_unions_p = 1;
327
328 /* For an anonymous struct or union, we must recursively
329 consider the fields of the anonymous type. They can be
330 directly initialized from the constructor. */
331 if (ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
332 {
333 /* Add this field itself. Synthesized copy constructors
334 initialize the entire aggregate. */
335 list = tree_cons (fields, NULL_TREE, list);
336 /* And now add the fields in the anonymous aggregate. */
337 list = build_field_list (TREE_TYPE (fields), list,
338 uses_unions_p);
339 }
340 /* Add this field. */
341 else if (DECL_NAME (fields))
342 list = tree_cons (fields, NULL_TREE, list);
343 }
344
345 return list;
346}
347
fd74ca0b
MM
348/* The MEMBER_INIT_LIST is a TREE_LIST. The TREE_PURPOSE of each list
349 gives a FIELD_DECL in T that needs initialization. The TREE_VALUE
350 gives the initializer, or list of initializer arguments. Sort the
351 MEMBER_INIT_LIST, returning a version that contains the same
352 information but in the order that the fields should actually be
353 initialized. Perform error-checking in the process. */
e92cc029 354
8d08fdba 355static tree
fd74ca0b 356sort_member_init (t, member_init_list)
8d08fdba 357 tree t;
fd74ca0b 358 tree member_init_list;
8d08fdba 359{
ff9f1a5d
MM
360 tree init_list;
361 tree last_field;
362 tree init;
363 int uses_unions_p;
364
365 /* Build up a list of the various fields, in sorted order. */
366 init_list = nreverse (build_field_list (t, NULL_TREE, &uses_unions_p));
367
368 /* Go through the explicit initializers, adding them to the
369 INIT_LIST. */
370 last_field = init_list;
fd74ca0b 371 for (init = member_init_list; init; init = TREE_CHAIN (init))
8d08fdba 372 {
ff9f1a5d
MM
373 tree f;
374 tree initialized_field;
375
376 initialized_field = TREE_PURPOSE (init);
377 my_friendly_assert (TREE_CODE (initialized_field) == FIELD_DECL,
378 20000516);
379
380 /* If the explicit initializers are in sorted order, then the
381 INITIALIZED_FIELD will be for a field following the
382 LAST_FIELD. */
383 for (f = last_field; f; f = TREE_CHAIN (f))
384 if (TREE_PURPOSE (f) == initialized_field)
385 break;
386
387 /* Give a warning, if appropriate. */
388 if (warn_reorder && !f)
389 {
3c7673a5
MM
390 cp_warning_at ("member initializers for `%#D'",
391 TREE_PURPOSE (last_field));
ff9f1a5d
MM
392 cp_warning_at (" and `%#D'", initialized_field);
393 warning (" will be re-ordered to match declaration order");
394 }
b7484fbe 395
ff9f1a5d
MM
396 /* Look again, from the beginning of the list. We must find the
397 field on this loop. */
398 if (!f)
399 {
400 f = init_list;
401 while (TREE_PURPOSE (f) != initialized_field)
402 f = TREE_CHAIN (f);
403 }
404
405 /* If there was already an explicit initializer for this field,
406 issue an error. */
407 if (TREE_TYPE (f))
408 cp_error ("multiple initializations given for member `%D'",
409 initialized_field);
410 else
411 {
412 /* Mark the field as explicitly initialized. */
413 TREE_TYPE (f) = error_mark_node;
414 /* And insert the initializer. */
415 TREE_VALUE (f) = TREE_VALUE (init);
416 }
417
418 /* Remember the location of the last explicitly initialized
419 field. */
420 last_field = f;
421 }
422
423 /* [class.base.init]
b7484fbe 424
ff9f1a5d
MM
425 If a ctor-initializer specifies more than one mem-initializer for
426 multiple members of the same union (including members of
427 anonymous unions), the ctor-initializer is ill-formed. */
428 if (uses_unions_p)
429 {
430 last_field = NULL_TREE;
431 for (init = init_list; init; init = TREE_CHAIN (init))
8d08fdba 432 {
ff9f1a5d
MM
433 tree field;
434 tree field_type;
435 int done;
436
437 /* Skip uninitialized members. */
438 if (!TREE_TYPE (init))
439 continue;
440 /* See if this field is a member of a union, or a member of a
441 structure contained in a union, etc. */
442 field = TREE_PURPOSE (init);
443 for (field_type = DECL_CONTEXT (field);
444 !same_type_p (field_type, t);
445 field_type = TYPE_CONTEXT (field_type))
446 if (TREE_CODE (field_type) == UNION_TYPE)
447 break;
448 /* If this field is not a member of a union, skip it. */
449 if (TREE_CODE (field_type) != UNION_TYPE)
8d08fdba 450 continue;
8d08fdba 451
ff9f1a5d
MM
452 /* It's only an error if we have two initializers for the same
453 union type. */
454 if (!last_field)
6bdb8141 455 {
ff9f1a5d
MM
456 last_field = field;
457 continue;
6bdb8141 458 }
8d08fdba 459
ff9f1a5d
MM
460 /* See if LAST_FIELD and the field initialized by INIT are
461 members of the same union. If so, there's a problem,
462 unless they're actually members of the same structure
463 which is itself a member of a union. For example, given:
8d08fdba 464
ff9f1a5d
MM
465 union { struct { int i; int j; }; };
466
467 initializing both `i' and `j' makes sense. */
468 field_type = DECL_CONTEXT (field);
469 done = 0;
470 do
8d08fdba 471 {
ff9f1a5d
MM
472 tree last_field_type;
473
474 last_field_type = DECL_CONTEXT (last_field);
475 while (1)
00595019 476 {
ff9f1a5d 477 if (same_type_p (last_field_type, field_type))
00595019 478 {
ff9f1a5d
MM
479 if (TREE_CODE (field_type) == UNION_TYPE)
480 cp_error ("initializations for multiple members of `%T'",
481 last_field_type);
482 done = 1;
483 break;
00595019 484 }
8d08fdba 485
ff9f1a5d
MM
486 if (same_type_p (last_field_type, t))
487 break;
8d08fdba 488
ff9f1a5d
MM
489 last_field_type = TYPE_CONTEXT (last_field_type);
490 }
491
492 /* If we've reached the outermost class, then we're
493 done. */
494 if (same_type_p (field_type, t))
495 break;
8d08fdba 496
ff9f1a5d 497 field_type = TYPE_CONTEXT (field_type);
8d08fdba 498 }
ff9f1a5d
MM
499 while (!done);
500
501 last_field = field;
b7484fbe
MS
502 }
503 }
8d08fdba 504
b7484fbe
MS
505 return init_list;
506}
8d08fdba 507
fd74ca0b
MM
508/* Like sort_member_init, but used for initializers of base classes.
509 *RBASE_PTR is filled in with the initializers for non-virtual bases;
510 vbase_ptr gets the virtual bases. */
511
b7484fbe 512static void
fd74ca0b
MM
513sort_base_init (t, base_init_list, rbase_ptr, vbase_ptr)
514 tree t;
515 tree base_init_list;
516 tree *rbase_ptr, *vbase_ptr;
b7484fbe
MS
517{
518 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
519 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
520
521 int i;
522 tree x;
523 tree last;
524
525 /* For warn_reorder. */
526 int last_pos = 0;
527 tree last_base = NULL_TREE;
528
529 tree rbases = NULL_TREE;
530 tree vbases = NULL_TREE;
8d08fdba 531
b7484fbe
MS
532 /* First walk through and splice out vbase and invalid initializers.
533 Also replace names with binfos. */
534
fd74ca0b 535 last = tree_cons (NULL_TREE, NULL_TREE, base_init_list);
b7484fbe
MS
536 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
537 {
be99da77 538 tree basetype = TREE_PURPOSE (x);
a703fb38 539 tree binfo = NULL_TREE;
b7484fbe 540
be99da77 541 if (basetype == NULL_TREE)
b7484fbe
MS
542 {
543 /* Initializer for single base class. Must not
544 use multiple inheritance or this is ambiguous. */
545 switch (n_baseclasses)
8d08fdba 546 {
b7484fbe 547 case 0:
8251199e 548 cp_error ("`%T' does not have a base class to initialize",
b7484fbe
MS
549 current_class_type);
550 return;
551 case 1:
552 break;
553 default:
8251199e 554 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
b7484fbe
MS
555 current_class_type);
556 return;
8d08fdba 557 }
b7484fbe
MS
558 binfo = TREE_VEC_ELT (binfos, 0);
559 }
be99da77 560 else if (is_aggr_type (basetype, 1))
b7484fbe 561 {
be99da77 562 binfo = binfo_or_else (basetype, t);
b7484fbe
MS
563 if (binfo == NULL_TREE)
564 continue;
8d08fdba 565
b7484fbe
MS
566 /* Virtual base classes are special cases. Their initializers
567 are recorded with this constructor, and they are used when
568 this constructor is the top-level constructor called. */
569 if (TREE_VIA_VIRTUAL (binfo))
570 {
a55583e9 571 tree v = binfo_for_vbase (BINFO_TYPE (binfo), t);
b7484fbe
MS
572 vbases = tree_cons (v, TREE_VALUE (x), vbases);
573 continue;
574 }
575 else
576 {
577 /* Otherwise, if it is not an immediate base class, complain. */
578 for (i = n_baseclasses-1; i >= 0; i--)
579 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
580 break;
581 if (i < 0)
582 {
8251199e 583 cp_error ("`%T' is not an immediate base class of `%T'",
be99da77 584 basetype, current_class_type);
b7484fbe
MS
585 continue;
586 }
587 }
8d08fdba 588 }
b7484fbe
MS
589 else
590 my_friendly_abort (365);
591
592 TREE_PURPOSE (x) = binfo;
593 TREE_CHAIN (last) = x;
594 last = x;
8d08fdba 595 }
b7484fbe 596 TREE_CHAIN (last) = NULL_TREE;
8d08fdba 597
b7484fbe
MS
598 /* Now walk through our regular bases and make sure they're initialized. */
599
600 for (i = 0; i < n_baseclasses; ++i)
8d08fdba 601 {
fd74ca0b 602 /* The base for which we're currently initializing. */
b7484fbe 603 tree base_binfo = TREE_VEC_ELT (binfos, i);
fd74ca0b
MM
604 /* The initializer for BASE_BINFO. */
605 tree init;
b7484fbe
MS
606 int pos;
607
608 if (TREE_VIA_VIRTUAL (base_binfo))
609 continue;
610
fd74ca0b
MM
611 /* We haven't found the BASE_BINFO yet. */
612 init = NULL_TREE;
613 /* Loop through all the explicitly initialized bases, looking
614 for an appropriate initializer. */
615 for (x = base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
b7484fbe
MS
616 {
617 tree binfo = TREE_PURPOSE (x);
618
fd74ca0b 619 if (binfo == base_binfo && !init)
b7484fbe
MS
620 {
621 if (warn_reorder)
622 {
623 if (pos < last_pos)
624 {
8251199e
JM
625 cp_warning_at ("base initializers for `%#T'", last_base);
626 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
627 warning (" will be re-ordered to match inheritance order");
b7484fbe
MS
628 }
629 last_pos = pos;
630 last_base = BINFO_TYPE (binfo);
631 }
632
633 /* Make sure we won't try to work on this init again. */
634 TREE_PURPOSE (x) = NULL_TREE;
fd74ca0b
MM
635 init = build_tree_list (binfo, TREE_VALUE (x));
636 }
637 else if (binfo == base_binfo)
638 {
639 cp_error ("base class `%T' already initialized",
640 BINFO_TYPE (binfo));
641 break;
b7484fbe
MS
642 }
643 }
644
645 /* If we didn't find BASE_BINFO in the list, create a dummy entry
646 so the two lists (RBASES and the list of bases) will be
647 symmetrical. */
fd74ca0b
MM
648 if (!init)
649 init = build_tree_list (NULL_TREE, NULL_TREE);
650 rbases = chainon (rbases, init);
8d08fdba
MS
651 }
652
b7484fbe
MS
653 *rbase_ptr = rbases;
654 *vbase_ptr = vbases;
655}
656
8d08fdba 657/* Perform whatever initializations have yet to be done on the base
fd74ca0b
MM
658 class, and non-static data members, of the CURRENT_CLASS_TYPE.
659 These actions are given by the BASE_INIT_LIST and MEM_INIT_LIST,
660 respectively.
8d08fdba
MS
661
662 If there is a need for a call to a constructor, we must surround
663 that call with a pushlevel/poplevel pair, since we are technically
664 at the PARM level of scope.
665
8d08fdba
MS
666 Note that emit_base_init does *not* initialize virtual base
667 classes. That is done specially, elsewhere. */
a9aedbc2 668
3dbc07b6 669void
fd74ca0b
MM
670emit_base_init (mem_init_list, base_init_list)
671 tree mem_init_list;
672 tree base_init_list;
8d08fdba 673{
5566b478 674 tree member;
b7484fbe 675 tree rbase_init_list, vbase_init_list;
cf2e003b 676 tree t = current_class_type;
8d08fdba
MS
677 tree t_binfo = TYPE_BINFO (t);
678 tree binfos = BINFO_BASETYPES (t_binfo);
3dbc07b6
MM
679 int i;
680 int n_baseclasses = BINFO_N_BASETYPES (t_binfo);
8d08fdba 681
fd74ca0b
MM
682 mem_init_list = sort_member_init (t, mem_init_list);
683 sort_base_init (t, base_init_list, &rbase_init_list, &vbase_init_list);
8d08fdba 684
69f00f01
MM
685 /* First, initialize the virtual base classes, if we are
686 constructing the most-derived object. */
b7484fbe
MS
687 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
688 {
689 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
69f00f01
MM
690 construct_virtual_bases (t, current_class_ref, current_class_ptr,
691 vbase_init_list, first_arg);
8d08fdba 692 }
8d08fdba 693
b7484fbe 694 /* Now, perform initialization of non-virtual base classes. */
8d08fdba
MS
695 for (i = 0; i < n_baseclasses; i++)
696 {
8d08fdba 697 tree base_binfo = TREE_VEC_ELT (binfos, i);
b7484fbe
MS
698 tree init = void_list_node;
699
700 if (TREE_VIA_VIRTUAL (base_binfo))
701 continue;
8d08fdba 702
dfbcd65a
JM
703 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
704 999);
8ccc31eb 705
b7484fbe
MS
706 if (TREE_PURPOSE (rbase_init_list))
707 init = TREE_VALUE (rbase_init_list);
708 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
824b9a4c
MS
709 {
710 init = NULL_TREE;
454fa7a7
MM
711 if (extra_warnings
712 && DECL_COPY_CONSTRUCTOR_P (current_function_decl))
8251199e 713 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
824b9a4c
MS
714 BINFO_TYPE (base_binfo));
715 }
8d08fdba 716
b7484fbe
MS
717 if (init != void_list_node)
718 {
4ac14744 719 member = convert_pointer_to_real (base_binfo, current_class_ptr);
fc378698 720 expand_aggr_init_1 (base_binfo, NULL_TREE,
b7484fbe 721 build_indirect_ref (member, NULL_PTR), init,
b370501f 722 LOOKUP_NORMAL);
8d08fdba 723 }
8d08fdba 724
0b8a1e58 725 expand_cleanup_for_base (base_binfo, NULL_TREE);
b7484fbe 726 rbase_init_list = TREE_CHAIN (rbase_init_list);
8d08fdba
MS
727 }
728
d569399b 729 /* Initialize the vtable pointers for the class. */
cf2e003b 730 initialize_vtbl_ptrs (current_class_ptr);
8d08fdba 731
ff9f1a5d 732 while (mem_init_list)
8d08fdba 733 {
ff9f1a5d
MM
734 tree init;
735 tree member;
b7484fbe
MS
736 int from_init_list;
737
ff9f1a5d 738 member = TREE_PURPOSE (mem_init_list);
8d08fdba
MS
739
740 /* See if we had a user-specified member initialization. */
ff9f1a5d 741 if (TREE_TYPE (mem_init_list))
8d08fdba 742 {
b7484fbe
MS
743 init = TREE_VALUE (mem_init_list);
744 from_init_list = 1;
8d08fdba 745 }
b7484fbe 746 else
8d08fdba 747 {
8d08fdba 748 init = DECL_INITIAL (member);
b7484fbe 749 from_init_list = 0;
824b9a4c
MS
750
751 /* Effective C++ rule 12. */
eb448459
MS
752 if (warn_ecpp && init == NULL_TREE
753 && !DECL_ARTIFICIAL (member)
824b9a4c 754 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
8251199e 755 cp_warning ("`%D' should be initialized in the member initialization list", member);
8d08fdba
MS
756 }
757
ff9f1a5d 758 perform_member_init (member, init, from_init_list);
b7484fbe
MS
759 mem_init_list = TREE_CHAIN (mem_init_list);
760 }
8d08fdba
MS
761}
762
3ec6bad3
MM
763/* Returns the address of the vtable (i.e., the value that should be
764 assigned to the vptr) for BINFO. */
765
766static tree
767build_vtbl_address (binfo)
768 tree binfo;
769{
770 tree vtbl;
771
772 /* Figure out what vtable BINFO's vtable is based on, and mark it as
773 used. */
774 vtbl = get_vtbl_decl_for_binfo (binfo);
775 assemble_external (vtbl);
776 TREE_USED (vtbl) = 1;
777
778 /* Now compute the address to use when initializing the vptr. */
779 vtbl = BINFO_VTABLE (binfo);
780 if (TREE_CODE (vtbl) == VAR_DECL)
781 {
782 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
783 TREE_CONSTANT (vtbl) = 1;
784 }
785
786 return vtbl;
787}
788
8d08fdba
MS
789/* This code sets up the virtual function tables appropriate for
790 the pointer DECL. It is a one-ply initialization.
791
792 BINFO is the exact type that DECL is supposed to be. In
793 multiple inheritance, this might mean "C's A" if C : A, B. */
e92cc029 794
8926095f 795static void
7177d104
MS
796expand_virtual_init (binfo, decl)
797 tree binfo, decl;
8d08fdba 798{
7177d104 799 tree type = BINFO_TYPE (binfo);
8d08fdba
MS
800 tree vtbl, vtbl_ptr;
801 tree vtype, vtype_binfo;
3ec6bad3 802 tree vtt_index;
8d08fdba 803
70ae3201 804 /* Compute the location of the vtable. */
d3a3fb6a 805 vtype = DECL_CONTEXT (TYPE_VFIELD (type));
8d08fdba 806 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
3ec6bad3
MM
807
808 /* Compute the initializer for vptr. */
809 vtbl = build_vtbl_address (binfo);
810
811 /* Under the new ABI, we may get this vptr from a VTT, if this is a
812 subobject constructor or subobject destructor. */
813 vtt_index = BINFO_VPTR_INDEX (binfo);
814 if (vtt_index)
815 {
816 tree vtbl2;
817 tree vtt_parm;
818
819 /* Compute the value to use, when there's a VTT. */
820 vtt_parm = DECL_VTT_PARM (current_function_decl);
821 vtbl2 = build (PLUS_EXPR,
822 TREE_TYPE (vtt_parm),
823 vtt_parm,
824 vtt_index);
825 vtbl2 = build1 (INDIRECT_REF, TREE_TYPE (vtbl), vtbl2);
826
827 /* The actual initializer is the VTT value only in the subobject
828 constructor. In maybe_clone_body we'll substitute NULL for
829 the vtt_parm in the case of the non-subobject constructor. */
830 vtbl = build (COND_EXPR,
831 TREE_TYPE (vtbl),
832 DECL_USE_VTT_PARM (current_function_decl),
833 vtbl2,
834 vtbl);
835 }
70ae3201
MM
836
837 /* Compute the location of the vtpr. */
8d08fdba
MS
838 decl = convert_pointer_to_real (vtype_binfo, decl);
839 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
840 if (vtbl_ptr == error_mark_node)
8926095f 841 return;
8d08fdba 842
70ae3201 843 /* Assign the vtable to the vptr. */
6060a796 844 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
f1dedc31 845 finish_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
8d08fdba
MS
846}
847
f33e32a8
MM
848/* If an exception is thrown in a constructor, those base classes already
849 constructed must be destroyed. This function creates the cleanup
0b8a1e58
MM
850 for BINFO, which has just been constructed. If FLAG is non-NULL,
851 it is a DECL which is non-zero when this base needs to be
852 destroyed. */
f33e32a8
MM
853
854static void
0b8a1e58 855expand_cleanup_for_base (binfo, flag)
f33e32a8 856 tree binfo;
0b8a1e58 857 tree flag;
f33e32a8
MM
858{
859 tree expr;
860
834c6dff 861 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
f33e32a8
MM
862 return;
863
0b8a1e58
MM
864 /* Call the destructor. */
865 expr = (build_scoped_method_call
298d6f60 866 (current_class_ref, binfo, base_dtor_identifier, NULL_TREE));
0b8a1e58
MM
867 if (flag)
868 expr = fold (build (COND_EXPR, void_type_node,
869 truthvalue_conversion (flag),
870 expr, integer_zero_node));
871
f1dedc31 872 finish_subobject (expr);
f33e32a8
MM
873}
874
8d08fdba
MS
875/* Subroutine of `expand_aggr_vbase_init'.
876 BINFO is the binfo of the type that is being initialized.
877 INIT_LIST is the list of initializers for the virtual baseclass. */
e92cc029 878
8d08fdba
MS
879static void
880expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
881 tree binfo, exp, addr, init_list;
882{
b7484fbe 883 tree init = purpose_member (binfo, init_list);
8d08fdba 884 tree ref = build_indirect_ref (addr, NULL_PTR);
72b7eeff 885
8d08fdba 886 if (init)
b7484fbe 887 init = TREE_VALUE (init);
8d08fdba 888 /* Call constructors, but don't set up vtables. */
b370501f 889 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
8d08fdba
MS
890}
891
69f00f01
MM
892/* Construct the virtual base-classes of THIS_REF (whose address is
893 THIS_PTR). The object has the indicated TYPE. The construction
894 actually takes place only if FLAG is non-zero. INIT_LIST is list
bbd15aac 895 of initializations for constructors to perform. */
e92cc029 896
8d08fdba 897static void
69f00f01
MM
898construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
899 tree type;
900 tree this_ref;
901 tree this_ptr;
8d08fdba 902 tree init_list;
0b8a1e58 903 tree flag;
8d08fdba 904{
69f00f01 905 tree vbases;
69f00f01
MM
906
907 /* If there are no virtual baseclasses, we shouldn't even be here. */
908 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
909
910 /* First set the pointers in our object that tell us where to find
911 our virtual baseclasses. */
bbd15aac
MM
912 if (!vbase_offsets_in_vtable_p ())
913 {
914 tree if_stmt;
915 tree result;
916
917 if_stmt = begin_if_stmt ();
918 finish_if_stmt_cond (flag, if_stmt);
919 result = init_vbase_pointers (type, this_ptr);
920 if (result)
921 finish_expr_stmt (build_compound_expr (result));
922 finish_then_clause (if_stmt);
923 finish_if_stmt ();
924 }
69f00f01
MM
925
926 /* Now, run through the baseclasses, initializing each. */
927 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
928 vbases = TREE_CHAIN (vbases))
8d08fdba 929 {
f1dedc31
MM
930 tree inner_if_stmt;
931 tree compound_stmt;
a6786148 932 tree exp;
a55583e9 933 tree vbase;
f1dedc31 934
69f00f01
MM
935 /* If there are virtual base classes with destructors, we need to
936 emit cleanups to destroy them if an exception is thrown during
937 the construction process. These exception regions (i.e., the
938 period during which the cleanups must occur) begin from the time
939 the construction is complete to the end of the function. If we
940 create a conditional block in which to initialize the
941 base-classes, then the cleanup region for the virtual base begins
942 inside a block, and ends outside of that block. This situation
943 confuses the sjlj exception-handling code. Therefore, we do not
944 create a single conditional block, but one for each
945 initialization. (That way the cleanup regions always begin
946 in the outer block.) We trust the back-end to figure out
947 that the FLAG will not change across initializations, and
948 avoid doing multiple tests. */
f1dedc31
MM
949 inner_if_stmt = begin_if_stmt ();
950 finish_if_stmt_cond (flag, inner_if_stmt);
951 compound_stmt = begin_compound_stmt (/*has_no_scope=*/1);
a6786148
MM
952
953 /* Compute the location of the virtual base. If we're
954 constructing virtual bases, then we must be the most derived
955 class. Therefore, we don't have to look up the virtual base;
956 we already know where it is. */
a55583e9 957 vbase = TREE_VALUE (vbases);
a6786148
MM
958 exp = build (PLUS_EXPR,
959 TREE_TYPE (this_ptr),
960 this_ptr,
aa1e6de6 961 fold (build1 (NOP_EXPR, TREE_TYPE (this_ptr),
a55583e9 962 BINFO_OFFSET (vbase))));
a6786148 963 exp = build1 (NOP_EXPR,
a55583e9 964 build_pointer_type (BINFO_TYPE (vbase)),
a6786148
MM
965 exp);
966
a55583e9 967 expand_aggr_vbase_init_1 (vbase, this_ref, exp, init_list);
f1dedc31 968 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
9bfadf57 969 finish_then_clause (inner_if_stmt);
f1dedc31 970 finish_if_stmt ();
69f00f01 971
a55583e9 972 expand_cleanup_for_base (vbase, flag);
8d08fdba
MS
973 }
974}
975
2ee887f2 976/* Find the context in which this FIELD can be initialized. */
e92cc029 977
2ee887f2
MS
978static tree
979initializing_context (field)
980 tree field;
981{
982 tree t = DECL_CONTEXT (field);
983
984 /* Anonymous union members can be initialized in the first enclosing
985 non-anonymous union context. */
6bdb8141 986 while (t && ANON_AGGR_TYPE_P (t))
2ee887f2
MS
987 t = TYPE_CONTEXT (t);
988 return t;
989}
990
8d08fdba
MS
991/* Function to give error message if member initialization specification
992 is erroneous. FIELD is the member we decided to initialize.
993 TYPE is the type for which the initialization is being performed.
72b7eeff 994 FIELD must be a member of TYPE.
8d08fdba
MS
995
996 MEMBER_NAME is the name of the member. */
997
998static int
999member_init_ok_or_else (field, type, member_name)
1000 tree field;
1001 tree type;
d8e178a0 1002 const char *member_name;
8d08fdba
MS
1003{
1004 if (field == error_mark_node)
1005 return 0;
2ee887f2 1006 if (field == NULL_TREE || initializing_context (field) != type)
8d08fdba 1007 {
8251199e 1008 cp_error ("class `%T' does not have any field named `%s'", type,
b7484fbe 1009 member_name);
8d08fdba
MS
1010 return 0;
1011 }
b7484fbe
MS
1012 if (TREE_STATIC (field))
1013 {
8251199e 1014 cp_error ("field `%#D' is static; only point of initialization is its declaration",
b7484fbe
MS
1015 field);
1016 return 0;
1017 }
1018
8d08fdba
MS
1019 return 1;
1020}
1021
1022/* If NAME is a viable field name for the aggregate DECL,
1023 and PARMS is a viable parameter list, then expand an _EXPR
1024 which describes this initialization.
1025
1026 Note that we do not need to chase through the class's base classes
1027 to look for NAME, because if it's in that list, it will be handled
1028 by the constructor for that base class.
1029
1030 We do not yet have a fixed-point finder to instantiate types
1031 being fed to overloaded constructors. If there is a unique
fd74ca0b 1032 constructor, then argument types can be got from that one. */
8d08fdba 1033
fd74ca0b 1034tree
8d08fdba
MS
1035expand_member_init (exp, name, init)
1036 tree exp, name, init;
1037{
8d08fdba 1038 tree basetype = NULL_TREE, field;
63d088b7 1039 tree type;
8d08fdba
MS
1040
1041 if (exp == NULL_TREE)
fd74ca0b 1042 return NULL_TREE;
8d08fdba
MS
1043
1044 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
1045
90418208
JM
1046 if (name && TYPE_P (name))
1047 {
1048 basetype = name;
1049 name = TYPE_IDENTIFIER (name);
1050 }
1051 else if (name && TREE_CODE (name) == TYPE_DECL)
be99da77 1052 {
94c82a77 1053 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
45537677 1054 name = DECL_NAME (name);
be99da77
MS
1055 }
1056
8d08fdba
MS
1057 if (name == NULL_TREE && IS_AGGR_TYPE (type))
1058 switch (CLASSTYPE_N_BASECLASSES (type))
1059 {
1060 case 0:
8251199e 1061 error ("base class initializer specified, but no base class to initialize");
fd74ca0b 1062 return NULL_TREE;
8d08fdba
MS
1063 case 1:
1064 basetype = TYPE_BINFO_BASETYPE (type, 0);
1065 break;
1066 default:
8251199e
JM
1067 error ("initializer for unnamed base class ambiguous");
1068 cp_error ("(type `%T' uses multiple inheritance)", type);
fd74ca0b 1069 return NULL_TREE;
8d08fdba
MS
1070 }
1071
41efda8f 1072 my_friendly_assert (init != NULL_TREE, 0);
8d08fdba 1073
41efda8f
MM
1074 /* The grammar should not allow fields which have names that are
1075 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
1076 may assume that this is an attempt to initialize a base class
1077 member of the current type. Otherwise, it is an attempt to
1078 initialize a member field. */
8d08fdba 1079
41efda8f
MM
1080 if (init == void_type_node)
1081 init = NULL_TREE;
8d08fdba 1082
41efda8f
MM
1083 if (name == NULL_TREE || basetype)
1084 {
41efda8f
MM
1085 if (name == NULL_TREE)
1086 {
1087#if 0
1088 if (basetype)
1089 name = TYPE_IDENTIFIER (basetype);
1090 else
8d08fdba 1091 {
8251199e 1092 error ("no base class to initialize");
8d08fdba
MS
1093 return;
1094 }
41efda8f 1095#endif
8d08fdba 1096 }
41efda8f
MM
1097 else if (basetype != type
1098 && ! current_template_parms
1099 && ! vec_binfo_member (basetype,
1100 TYPE_BINFO_BASETYPES (type))
a55583e9 1101 && ! binfo_for_vbase (basetype, type))
8d08fdba 1102 {
41efda8f
MM
1103 if (IDENTIFIER_CLASS_VALUE (name))
1104 goto try_member;
1105 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
8251199e 1106 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
41efda8f
MM
1107 basetype, type);
1108 else
8251199e 1109 cp_error ("type `%T' is not an immediate basetype for `%T'",
41efda8f 1110 basetype, type);
fd74ca0b 1111 return NULL_TREE;
41efda8f 1112 }
8d08fdba 1113
fd74ca0b 1114 init = build_tree_list (basetype, init);
41efda8f
MM
1115 }
1116 else
1117 {
41efda8f
MM
1118 try_member:
1119 field = lookup_field (type, name, 1, 0);
8d08fdba 1120
41efda8f 1121 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
fd74ca0b 1122 return NULL_TREE;
8d08fdba 1123
fd74ca0b 1124 init = build_tree_list (field, init);
41efda8f 1125 }
fd74ca0b
MM
1126
1127 return init;
8d08fdba
MS
1128}
1129
1130/* This is like `expand_member_init', only it stores one aggregate
1131 value into another.
1132
1133 INIT comes in two flavors: it is either a value which
1134 is to be stored in EXP, or it is a parameter list
1135 to go to a constructor, which will operate on EXP.
f30432d7
MS
1136 If INIT is not a parameter list for a constructor, then set
1137 LOOKUP_ONLYCONVERTING.
6060a796
MS
1138 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1139 the initializer, if FLAGS is 0, then it is the (init) form.
8d08fdba 1140 If `init' is a CONSTRUCTOR, then we emit a warning message,
59be0cdd 1141 explaining that such initializations are invalid.
8d08fdba 1142
8d08fdba
MS
1143 If INIT resolves to a CALL_EXPR which happens to return
1144 something of the type we are looking for, then we know
1145 that we can safely use that call to perform the
1146 initialization.
1147
1148 The virtual function table pointer cannot be set up here, because
1149 we do not really know its type.
1150
1151 Virtual baseclass pointers are also set up here.
1152
1153 This never calls operator=().
1154
1155 When initializing, nothing is CONST.
1156
1157 A default copy constructor may have to be used to perform the
1158 initialization.
1159
1160 A constructor or a conversion operator may have to be used to
e92cc029 1161 perform the initialization, but not both, as it would be ambiguous. */
8d08fdba 1162
f1dedc31
MM
1163tree
1164build_aggr_init (exp, init, flags)
8d08fdba 1165 tree exp, init;
6060a796 1166 int flags;
8d08fdba 1167{
f1dedc31
MM
1168 tree stmt_expr;
1169 tree compound_stmt;
1170 int destroy_temps;
8d08fdba
MS
1171 tree type = TREE_TYPE (exp);
1172 int was_const = TREE_READONLY (exp);
f30432d7 1173 int was_volatile = TREE_THIS_VOLATILE (exp);
8d08fdba
MS
1174
1175 if (init == error_mark_node)
f1dedc31 1176 return error_mark_node;
8d08fdba
MS
1177
1178 TREE_READONLY (exp) = 0;
f30432d7
MS
1179 TREE_THIS_VOLATILE (exp) = 0;
1180
1181 if (init && TREE_CODE (init) != TREE_LIST)
1182 flags |= LOOKUP_ONLYCONVERTING;
8d08fdba
MS
1183
1184 if (TREE_CODE (type) == ARRAY_TYPE)
1185 {
1186 /* Must arrange to initialize each element of EXP
1187 from elements of INIT. */
8d08fdba 1188 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
aa54df09
NS
1189
1190 if (init && !itype)
8d08fdba
MS
1191 {
1192 /* Handle bad initializers like:
1193 class COMPLEX {
1194 public:
1195 double re, im;
1196 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1197 ~COMPLEX() {};
1198 };
1199
1200 int main(int argc, char **argv) {
1201 COMPLEX zees(1.0, 0.0)[10];
1202 }
1203 */
aa54df09 1204 cp_error ("bad array initializer");
f1dedc31 1205 return error_mark_node;
8d08fdba 1206 }
aa54df09
NS
1207 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1208 {
1209 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1210 if (init)
1211 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1212 }
f1dedc31
MM
1213 stmt_expr = build_vec_init (exp, exp, array_type_nelts (type), init,
1214 init && same_type_p (TREE_TYPE (init),
1215 TREE_TYPE (exp)));
8d08fdba 1216 TREE_READONLY (exp) = was_const;
f30432d7 1217 TREE_THIS_VOLATILE (exp) = was_volatile;
8d08fdba 1218 TREE_TYPE (exp) = type;
f376e137
MS
1219 if (init)
1220 TREE_TYPE (init) = itype;
f1dedc31 1221 return stmt_expr;
8d08fdba
MS
1222 }
1223
1224 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1225 /* just know that we've seen something for this node */
1226 TREE_USED (exp) = 1;
1227
e7843f33 1228 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
f1dedc31 1229 begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623
BC
1230 destroy_temps = stmts_are_full_exprs_p ();
1231 current_stmt_tree->stmts_are_full_exprs_p = 0;
8d08fdba 1232 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
b370501f 1233 init, LOOKUP_NORMAL|flags);
f1dedc31 1234 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
f2c5f623 1235 current_stmt_tree->stmts_are_full_exprs_p = destroy_temps;
e7843f33 1236 TREE_TYPE (exp) = type;
8d08fdba 1237 TREE_READONLY (exp) = was_const;
f30432d7 1238 TREE_THIS_VOLATILE (exp) = was_volatile;
f1dedc31
MM
1239
1240 return stmt_expr;
8d08fdba
MS
1241}
1242
1243static void
b370501f 1244expand_default_init (binfo, true_exp, exp, init, flags)
8d08fdba
MS
1245 tree binfo;
1246 tree true_exp, exp;
8d08fdba 1247 tree init;
8d08fdba
MS
1248 int flags;
1249{
fc378698 1250 tree type = TREE_TYPE (exp);
9eb71d8c 1251 tree ctor_name;
fc378698 1252
8d08fdba
MS
1253 /* It fails because there may not be a constructor which takes
1254 its own type as the first (or only parameter), but which does
1255 take other types via a conversion. So, if the thing initializing
1256 the expression is a unit element of type X, first try X(X&),
1257 followed by initialization by X. If neither of these work
1258 out, then look hard. */
1259 tree rval;
1260 tree parms;
8d08fdba 1261
277294d7 1262 if (init && TREE_CODE (init) != TREE_LIST
faf5394a
MS
1263 && (flags & LOOKUP_ONLYCONVERTING))
1264 {
1265 /* Base subobjects should only get direct-initialization. */
1266 if (true_exp != exp)
1267 abort ();
1268
c37dc68e
JM
1269 if (flags & DIRECT_BIND)
1270 /* Do nothing. We hit this in two cases: Reference initialization,
1271 where we aren't initializing a real variable, so we don't want
1272 to run a new constructor; and catching an exception, where we
1273 have already built up the constructor call so we could wrap it
1274 in an exception region. */;
27b8d0cd
MM
1275 else if (TREE_CODE (init) == CONSTRUCTOR)
1276 /* A brace-enclosed initializer has whatever type is
1277 required. There's no need to convert it. */
1278 ;
c37dc68e 1279 else
37c46b43 1280 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
faf5394a 1281
c7ae64f2
JM
1282 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1283 /* We need to protect the initialization of a catch parm
1284 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1285 around the TARGET_EXPR for the copy constructor. See
1286 expand_start_catch_block. */
1287 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1288 TREE_OPERAND (init, 0));
1289 else
1290 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1291 TREE_SIDE_EFFECTS (init) = 1;
f1dedc31 1292 finish_expr_stmt (init);
faf5394a
MS
1293 return;
1294 }
1295
b7484fbe
MS
1296 if (init == NULL_TREE
1297 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
8d08fdba
MS
1298 {
1299 parms = init;
db5ae43f
MS
1300 if (parms)
1301 init = TREE_VALUE (parms);
8d08fdba 1302 }
8d08fdba 1303 else
051e6fd7 1304 parms = build_tree_list (NULL_TREE, init);
8d08fdba 1305
9eb71d8c
MM
1306 if (true_exp == exp)
1307 ctor_name = complete_ctor_identifier;
1308 else
1309 ctor_name = base_ctor_identifier;
8d08fdba 1310
9eb71d8c 1311 rval = build_method_call (exp, ctor_name, parms, binfo, flags);
25eb19ff 1312 if (TREE_SIDE_EFFECTS (rval))
35b1567d
BC
1313 {
1314 if (building_stmt_tree ())
1315 finish_expr_stmt (rval);
1316 else
1317 genrtl_expr_stmt (rval);
1318 }
8d08fdba
MS
1319}
1320
1321/* This function is responsible for initializing EXP with INIT
1322 (if any).
1323
1324 BINFO is the binfo of the type for who we are performing the
1325 initialization. For example, if W is a virtual base class of A and B,
1326 and C : A, B.
1327 If we are initializing B, then W must contain B's W vtable, whereas
1328 were we initializing C, W must contain C's W vtable.
1329
1330 TRUE_EXP is nonzero if it is the true expression being initialized.
1331 In this case, it may be EXP, or may just contain EXP. The reason we
1332 need this is because if EXP is a base element of TRUE_EXP, we
1333 don't necessarily know by looking at EXP where its virtual
1334 baseclass fields should really be pointing. But we do know
1335 from TRUE_EXP. In constructors, we don't know anything about
1336 the value being initialized.
1337
8d08fdba
MS
1338 FLAGS is just passes to `build_method_call'. See that function for
1339 its description. */
1340
1341static void
b370501f 1342expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
8d08fdba
MS
1343 tree binfo;
1344 tree true_exp, exp;
1345 tree init;
8d08fdba
MS
1346 int flags;
1347{
1348 tree type = TREE_TYPE (exp);
8d08fdba
MS
1349
1350 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1351
1352 /* Use a function returning the desired type to initialize EXP for us.
1353 If the function is a constructor, and its first argument is
1354 NULL_TREE, know that it was meant for us--just slide exp on
1355 in and expand the constructor. Constructors now come
1356 as TARGET_EXPRs. */
faf5394a
MS
1357
1358 if (init && TREE_CODE (exp) == VAR_DECL
1359 && TREE_CODE (init) == CONSTRUCTOR
1360 && TREE_HAS_CONSTRUCTOR (init))
1361 {
f1dedc31
MM
1362 /* If store_init_value returns NULL_TREE, the INIT has been
1363 record in the DECL_INITIAL for EXP. That means there's
1364 nothing more we have to do. */
1365 if (!store_init_value (exp, init))
faf5394a 1366 {
f1dedc31
MM
1367 if (!building_stmt_tree ())
1368 expand_decl_init (exp);
1369 }
1370 else
c557501d 1371 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
faf5394a
MS
1372 return;
1373 }
1374
9e9ff709
MS
1375 /* We know that expand_default_init can handle everything we want
1376 at this point. */
b370501f 1377 expand_default_init (binfo, true_exp, exp, init, flags);
8d08fdba
MS
1378}
1379
be99da77
MS
1380/* Report an error if TYPE is not a user-defined, aggregate type. If
1381 OR_ELSE is nonzero, give an error message. */
e92cc029 1382
be99da77
MS
1383int
1384is_aggr_type (type, or_else)
1385 tree type;
1386 int or_else;
1387{
1388 if (type == error_mark_node)
1389 return 0;
1390
1391 if (! IS_AGGR_TYPE (type)
73b0fce8 1392 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
a1281f45 1393 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
be99da77
MS
1394 {
1395 if (or_else)
8251199e 1396 cp_error ("`%T' is not an aggregate type", type);
be99da77
MS
1397 return 0;
1398 }
1399 return 1;
1400}
1401
8d08fdba 1402/* Like is_aggr_typedef, but returns typedef if successful. */
e92cc029 1403
8d08fdba
MS
1404tree
1405get_aggr_from_typedef (name, or_else)
1406 tree name;
1407 int or_else;
1408{
1409 tree type;
1410
1411 if (name == error_mark_node)
1412 return NULL_TREE;
1413
1414 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1415 type = IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1416 else
1417 {
1418 if (or_else)
8251199e 1419 cp_error ("`%T' fails to be an aggregate typedef", name);
8d08fdba
MS
1420 return NULL_TREE;
1421 }
1422
1423 if (! IS_AGGR_TYPE (type)
73b0fce8 1424 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
a1281f45 1425 && TREE_CODE (type) != BOUND_TEMPLATE_TEMPLATE_PARM)
8d08fdba
MS
1426 {
1427 if (or_else)
8251199e 1428 cp_error ("type `%T' is of non-aggregate type", type);
8d08fdba
MS
1429 return NULL_TREE;
1430 }
1431 return type;
1432}
1433
1434tree
1435get_type_value (name)
1436 tree name;
1437{
8d08fdba
MS
1438 if (name == error_mark_node)
1439 return NULL_TREE;
1440
1441 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1442 return IDENTIFIER_TYPE_VALUE (name);
8d08fdba
MS
1443 else
1444 return NULL_TREE;
1445}
051e6fd7 1446
8d08fdba 1447\f
51c184be 1448/* This code could just as well go in `class.c', but is placed here for
8d08fdba
MS
1449 modularity. */
1450
be99da77 1451/* For an expression of the form TYPE :: NAME (PARMLIST), build
8d08fdba 1452 the appropriate function call. */
e92cc029 1453
8d08fdba 1454tree
be99da77
MS
1455build_member_call (type, name, parmlist)
1456 tree type, name, parmlist;
8d08fdba 1457{
be99da77 1458 tree t;
386b8a85 1459 tree method_name;
8d08fdba 1460 int dtor = 0;
8d08fdba
MS
1461 tree basetype_path, decl;
1462
72e61a07
JM
1463 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1464 && TREE_CODE (type) == NAMESPACE_DECL)
1465 {
18c32f69
JM
1466 /* 'name' already refers to the decls from the namespace, since we
1467 hit do_identifier for template_ids. */
c2e63b4c
ML
1468 method_name = TREE_OPERAND (name, 0);
1469 /* FIXME: Since we don't do independent names right yet, the
1470 name might also be a LOOKUP_EXPR. Once we resolve this to a
1471 real decl earlier, this can go. This may happen during
1472 tsubst'ing. */
1473 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1474 {
1475 method_name = lookup_namespace_name
1476 (type, TREE_OPERAND (method_name, 0));
1477 TREE_OPERAND (name, 0) = method_name;
1478 }
1479 my_friendly_assert (is_overloaded_fn (method_name), 980519);
72e61a07
JM
1480 return build_x_function_call (name, parmlist, current_class_ref);
1481 }
1482
297a5329
JM
1483 if (DECL_P (name))
1484 name = DECL_NAME (name);
1485
6633d636
MS
1486 if (type == std_node)
1487 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1488 current_class_ref);
30394414
JM
1489 if (TREE_CODE (type) == NAMESPACE_DECL)
1490 return build_x_function_call (lookup_namespace_name (type, name),
1491 parmlist, current_class_ref);
6633d636 1492
8f032717 1493 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
b03a08ee
MM
1494 {
1495 method_name = TREE_OPERAND (name, 0);
8f032717
MM
1496 if (TREE_CODE (method_name) == COMPONENT_REF)
1497 method_name = TREE_OPERAND (method_name, 1);
b03a08ee
MM
1498 if (is_overloaded_fn (method_name))
1499 method_name = DECL_NAME (OVL_CURRENT (method_name));
8f032717 1500 TREE_OPERAND (name, 0) = method_name;
b03a08ee 1501 }
8f032717
MM
1502 else
1503 method_name = name;
386b8a85 1504
8d08fdba
MS
1505 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1506 {
1507 method_name = TREE_OPERAND (method_name, 0);
1508 dtor = 1;
1509 }
1510
a9aedbc2
MS
1511 /* This shouldn't be here, and build_member_call shouldn't appear in
1512 parse.y! (mrs) */
be99da77
MS
1513 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1514 && get_aggr_from_typedef (type, 0) == 0)
a9aedbc2 1515 {
be99da77 1516 tree ns = lookup_name (type, 0);
a9aedbc2
MS
1517 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1518 {
4ac14744 1519 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
a9aedbc2
MS
1520 }
1521 }
1522
be99da77 1523 if (type == NULL_TREE || ! is_aggr_type (type, 1))
8d08fdba
MS
1524 return error_mark_node;
1525
1526 /* An operator we did not like. */
1527 if (name == NULL_TREE)
1528 return error_mark_node;
1529
1530 if (dtor)
1531 {
8251199e 1532 cp_error ("cannot call destructor `%T::~%T' without object", type,
8d08fdba
MS
1533 method_name);
1534 return error_mark_node;
1535 }
1536
51924768 1537 decl = maybe_dummy_object (type, &basetype_path);
8d08fdba 1538
51924768 1539 /* Convert 'this' to the specified type to disambiguate conversion
879fb1de
JM
1540 to the function's context. Apparently Standard C++ says that we
1541 shouldn't do this. */
1542 if (decl == current_class_ref
1543 && ! pedantic
1544 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
8d08fdba 1545 {
4ac14744 1546 tree olddecl = current_class_ptr;
8d08fdba
MS
1547 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1548 if (oldtype != type)
1549 {
91063b51 1550 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
6060a796 1551 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
51924768 1552 decl = build_indirect_ref (decl, NULL_PTR);
8d08fdba 1553 }
8d08fdba
MS
1554 }
1555
71851aaa
MS
1556 if (method_name == constructor_name (type)
1557 || method_name == constructor_name_full (type))
1558 return build_functional_cast (type, parmlist);
4bb0968f 1559 if (lookup_fnfields (basetype_path, method_name, 0))
386b8a85
JM
1560 return build_method_call (decl,
1561 TREE_CODE (name) == TEMPLATE_ID_EXPR
1562 ? name : method_name,
1563 parmlist, basetype_path,
8d08fdba
MS
1564 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1565 if (TREE_CODE (name) == IDENTIFIER_NODE
8926095f 1566 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
8d08fdba
MS
1567 {
1568 if (t == error_mark_node)
1569 return error_mark_node;
1570 if (TREE_CODE (t) == FIELD_DECL)
1571 {
51924768 1572 if (is_dummy_object (decl))
8d08fdba 1573 {
8251199e 1574 cp_error ("invalid use of non-static field `%D'", t);
8d08fdba
MS
1575 return error_mark_node;
1576 }
1577 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1578 }
1579 else if (TREE_CODE (t) == VAR_DECL)
1580 decl = t;
1581 else
1582 {
8251199e 1583 cp_error ("invalid use of member `%D'", t);
8d08fdba
MS
1584 return error_mark_node;
1585 }
3c215895
JM
1586 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1587 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1588 parmlist, NULL_TREE);
8d08fdba
MS
1589 return build_function_call (decl, parmlist);
1590 }
1591 else
1592 {
8251199e 1593 cp_error ("no method `%T::%D'", type, name);
8d08fdba
MS
1594 return error_mark_node;
1595 }
1596}
1597
1598/* Build a reference to a member of an aggregate. This is not a
1599 C++ `&', but really something which can have its address taken,
be99da77
MS
1600 and then act as a pointer to member, for example TYPE :: FIELD
1601 can have its address taken by saying & TYPE :: FIELD.
8d08fdba
MS
1602
1603 @@ Prints out lousy diagnostics for operator <typename>
1604 @@ fields.
1605
51c184be 1606 @@ This function should be rewritten and placed in search.c. */
e92cc029 1607
8d08fdba 1608tree
be99da77
MS
1609build_offset_ref (type, name)
1610 tree type, name;
8d08fdba 1611{
d6479fe7
MM
1612 tree decl, t = error_mark_node;
1613 tree member;
fc378698 1614 tree basebinfo = NULL_TREE;
2a238a97 1615 tree orig_name = name;
8d08fdba 1616
5f311aec 1617 /* class templates can come in as TEMPLATE_DECLs here. */
874503bc 1618 if (TREE_CODE (name) == TEMPLATE_DECL)
93cdc044
JM
1619 return name;
1620
6633d636
MS
1621 if (type == std_node)
1622 return do_scoped_id (name, 0);
1623
53b22f3d
MM
1624 if (processing_template_decl || uses_template_parms (type))
1625 return build_min_nt (SCOPE_REF, type, name);
5566b478 1626
2a238a97
MM
1627 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1628 {
1629 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1630 something like `a.template f<int>' or the like. For the most
1631 part, we treat this just like a.f. We do remember, however,
1632 the template-id that was used. */
1633 name = TREE_OPERAND (orig_name, 0);
e4a84209 1634
c65a922c
TP
1635 if (DECL_P (name))
1636 name = DECL_NAME (name);
1637 else
1638 {
1639 if (TREE_CODE (name) == LOOKUP_EXPR)
1640 /* This can happen during tsubst'ing. */
1641 name = TREE_OPERAND (name, 0);
1642 else
1643 {
1644 if (TREE_CODE (name) == COMPONENT_REF)
1645 name = TREE_OPERAND (name, 1);
1646 if (TREE_CODE (name) == OVERLOAD)
1647 name = DECL_NAME (OVL_CURRENT (name));
1648 }
1649 }
e4a84209 1650
2a238a97
MM
1651 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1652 }
1653
c833d2be
NS
1654 if (type == NULL_TREE)
1655 return error_mark_node;
1656
1657 /* Handle namespace names fully here. */
1658 if (TREE_CODE (type) == NAMESPACE_DECL)
1659 {
1660 t = lookup_namespace_name (type, name);
1661 if (t == error_mark_node)
1662 return t;
1663 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1664 /* Reconstruct the TEMPLATE_ID_EXPR. */
1665 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t),
1666 t, TREE_OPERAND (orig_name, 1));
1667 if (! type_unknown_p (t))
1668 {
1669 mark_used (t);
1670 t = convert_from_reference (t);
1671 }
1672 return t;
1673 }
1674
1675 if (! is_aggr_type (type, 1))
1676 return error_mark_node;
1677
8d08fdba
MS
1678 if (TREE_CODE (name) == BIT_NOT_EXPR)
1679 {
1c2c08a5 1680 if (! check_dtor_name (type, name))
8251199e 1681 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1c2c08a5
JM
1682 type, TREE_OPERAND (name, 0));
1683 name = dtor_identifier;
8d08fdba 1684 }
be99da77 1685
d0f062fb 1686 if (!COMPLETE_TYPE_P (complete_type (type))
61a127b3 1687 && !TYPE_BEING_DEFINED (type))
8d08fdba 1688 {
61a127b3
MM
1689 cp_error ("incomplete type `%T' does not have member `%D'", type,
1690 name);
8d08fdba
MS
1691 return error_mark_node;
1692 }
1693
51924768 1694 decl = maybe_dummy_object (type, &basebinfo);
8d08fdba 1695
d6479fe7 1696 member = lookup_member (basebinfo, name, 1, 0);
00595019 1697
d6479fe7 1698 if (member == error_mark_node)
00595019
MS
1699 return error_mark_node;
1700
aa52c1ff 1701 /* A lot of this logic is now handled in lookup_member. */
3fc5037b 1702 if (member && BASELINK_P (member))
8d08fdba 1703 {
8d08fdba 1704 /* Go from the TREE_BASELINK to the member function info. */
d6479fe7 1705 tree fnfields = member;
8d08fdba
MS
1706 t = TREE_VALUE (fnfields);
1707
2a238a97
MM
1708 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1709 {
1710 /* The FNFIELDS are going to contain functions that aren't
1711 necessarily templates, and templates that don't
1712 necessarily match the explicit template parameters. We
1713 save all the functions, and the explicit parameters, and
1714 then figure out exactly what to instantiate with what
1715 arguments in instantiate_type. */
1716
1717 if (TREE_CODE (t) != OVERLOAD)
1718 /* The code in instantiate_type which will process this
1719 expects to encounter OVERLOADs, not raw functions. */
1720 t = ovl_cons (t, NULL_TREE);
051e6fd7 1721
19420d00
NS
1722 t = build (TEMPLATE_ID_EXPR, TREE_TYPE (t), t,
1723 TREE_OPERAND (orig_name, 1));
1724 t = build (OFFSET_REF, unknown_type_node, decl, t);
1725
1726 PTRMEM_OK_P (t) = 1;
1727
1728 return t;
2a238a97
MM
1729 }
1730
2c73f9f5 1731 if (!really_overloaded_fn (t))
8d08fdba 1732 {
2c73f9f5
ML
1733 /* Get rid of a potential OVERLOAD around it */
1734 t = OVL_CURRENT (t);
1735
fc378698 1736 /* unique functions are handled easily. */
d6479fe7
MM
1737 if (!enforce_access (basebinfo, t))
1738 return error_mark_node;
fc378698 1739 mark_used (t);
848b92e1
JM
1740 if (DECL_STATIC_FUNCTION_P (t))
1741 return t;
19420d00
NS
1742 t = build (OFFSET_REF, TREE_TYPE (t), decl, t);
1743 PTRMEM_OK_P (t) = 1;
1744 return t;
8d08fdba
MS
1745 }
1746
05e0b2f4 1747 TREE_TYPE (fnfields) = unknown_type_node;
19420d00
NS
1748
1749 t = build (OFFSET_REF, unknown_type_node, decl, fnfields);
1750 PTRMEM_OK_P (t) = 1;
1751 return t;
8d08fdba
MS
1752 }
1753
d6479fe7 1754 t = member;
8d08fdba
MS
1755
1756 if (t == NULL_TREE)
1757 {
8251199e 1758 cp_error ("`%D' is not a member of type `%T'", name, type);
8d08fdba
MS
1759 return error_mark_node;
1760 }
1761
1762 if (TREE_CODE (t) == TYPE_DECL)
1763 {
51c184be
MS
1764 TREE_USED (t) = 1;
1765 return t;
8d08fdba
MS
1766 }
1767 /* static class members and class-specific enum
1768 values can be returned without further ado. */
1769 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1770 {
72b7eeff 1771 mark_used (t);
42976354 1772 return convert_from_reference (t);
8d08fdba
MS
1773 }
1774
162bc98d 1775 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
b7484fbe 1776 {
8251199e 1777 cp_error ("illegal pointer to bit field `%D'", t);
b7484fbe
MS
1778 return error_mark_node;
1779 }
1780
8d08fdba 1781 /* static class functions too. */
be99da77
MS
1782 if (TREE_CODE (t) == FUNCTION_DECL
1783 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
8d08fdba
MS
1784 my_friendly_abort (53);
1785
be99da77
MS
1786 /* In member functions, the form `type::name' is no longer
1787 equivalent to `this->type::name', at least not until
1788 resolve_offset_ref. */
19420d00
NS
1789 t = build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1790 PTRMEM_OK_P (t) = 1;
1791 return t;
8d08fdba
MS
1792}
1793
8d08fdba
MS
1794/* If a OFFSET_REF made it through to here, then it did
1795 not have its address taken. */
1796
1797tree
1798resolve_offset_ref (exp)
1799 tree exp;
1800{
1801 tree type = TREE_TYPE (exp);
1802 tree base = NULL_TREE;
1803 tree member;
1804 tree basetype, addr;
1805
4ac14744
MS
1806 if (TREE_CODE (exp) == OFFSET_REF)
1807 {
1808 member = TREE_OPERAND (exp, 1);
1809 base = TREE_OPERAND (exp, 0);
1810 }
1811 else
8d08fdba
MS
1812 {
1813 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1814 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1815 {
8251199e 1816 error ("object missing in use of pointer-to-member construct");
8d08fdba
MS
1817 return error_mark_node;
1818 }
1819 member = exp;
1820 type = TREE_TYPE (type);
4ac14744 1821 base = current_class_ref;
8d08fdba
MS
1822 }
1823
67804825 1824 if (BASELINK_P (member) || TREE_CODE (member) == TEMPLATE_ID_EXPR)
19420d00
NS
1825 return build_unary_op (ADDR_EXPR, exp, 0);
1826
05e0b2f4
JM
1827 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1828 {
19420d00
NS
1829 if (!flag_ms_extensions)
1830 /* A single non-static member, make sure we don't allow a
1831 pointer-to-member. */
1832 exp = ovl_cons (member, NULL_TREE);
1833
05e0b2f4
JM
1834 return build_unary_op (ADDR_EXPR, exp, 0);
1835 }
67804825 1836
8d08fdba 1837 if ((TREE_CODE (member) == VAR_DECL
162bc98d
JM
1838 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1839 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
a359be75 1840 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
8d08fdba
MS
1841 {
1842 /* These were static members. */
1843 if (mark_addressable (member) == 0)
1844 return error_mark_node;
1845 return member;
1846 }
1847
faf5394a
MS
1848 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1849 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1850 return member;
1851
8d08fdba
MS
1852 /* Syntax error can cause a member which should
1853 have been seen as static to be grok'd as non-static. */
4ac14744 1854 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
8d08fdba 1855 {
3aac3c2f
MM
1856 cp_error_at ("member `%D' is non-static but referenced as a static member",
1857 member);
1858 error ("at this point in file");
8d08fdba
MS
1859 return error_mark_node;
1860 }
1861
1862 /* The first case is really just a reference to a member of `this'. */
1863 if (TREE_CODE (member) == FIELD_DECL
51924768 1864 && (base == current_class_ref || is_dummy_object (base)))
8d08fdba 1865 {
5bb1b569 1866 tree expr;
8d08fdba 1867
aa52c1ff
JM
1868 basetype = DECL_CONTEXT (member);
1869
1870 /* Try to get to basetype from 'this'; if that doesn't work,
1871 nothing will. */
1872 base = current_class_ref;
1873
1874 /* First convert to the intermediate base specified, if appropriate. */
8d08fdba 1875 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
aa52c1ff 1876 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type));
39211cd5 1877
aa52c1ff
JM
1878 addr = build_unary_op (ADDR_EXPR, base, 0);
1879 addr = convert_pointer_to (basetype, addr);
1880
1881 if (addr == error_mark_node)
1882 return error_mark_node;
051e6fd7 1883
5bb1b569
MM
1884 expr = build (COMPONENT_REF, TREE_TYPE (member),
1885 build_indirect_ref (addr, NULL_PTR), member);
1886 return convert_from_reference (expr);
8d08fdba
MS
1887 }
1888
f49422da 1889 /* Ensure that we have an object. */
51924768 1890 if (is_dummy_object (base))
f49422da
MS
1891 addr = error_mark_node;
1892 else
5bb1b569
MM
1893 /* If this is a reference to a member function, then return the
1894 address of the member function (which may involve going
1895 through the object's vtable), otherwise, return an expression
1896 for the dereferenced pointer-to-member construct. */
1897 addr = build_unary_op (ADDR_EXPR, base, 0);
8d08fdba 1898
162bc98d 1899 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
8d08fdba 1900 {
f49422da
MS
1901 if (addr == error_mark_node)
1902 {
8251199e 1903 cp_error ("object missing in `%E'", exp);
f49422da
MS
1904 return error_mark_node;
1905 }
1906
162bc98d 1907 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
8d08fdba 1908 addr = convert_pointer_to (basetype, addr);
162bc98d 1909 member = cp_convert (ptrdiff_type_node, member);
051e6fd7 1910
cd8ed629
MM
1911 if (!flag_new_abi)
1912 /* Pointer to data members are offset by one, so that a null
1913 pointer with a real value of 0 is distinguishable from an
1914 offset of the first member of a structure. */
ab76ca54
MM
1915 member = cp_build_binary_op (MINUS_EXPR, member,
1916 cp_convert (ptrdiff_type_node,
1917 integer_one_node));
c91a56d2 1918
8d08fdba 1919 return build1 (INDIRECT_REF, type,
fef71f9d
JM
1920 build (PLUS_EXPR, build_pointer_type (type),
1921 addr, member));
8d08fdba
MS
1922 }
1923 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1924 {
b7484fbe 1925 return get_member_function_from_ptrfunc (&addr, member);
8d08fdba
MS
1926 }
1927 my_friendly_abort (56);
1928 /* NOTREACHED */
1929 return NULL_TREE;
1930}
1931
fc611ce0
MM
1932/* If DECL is a `const' declaration, and its value is a known
1933 constant, then return that value. */
8d08fdba
MS
1934
1935tree
1936decl_constant_value (decl)
1937 tree decl;
1938{
fc611ce0
MM
1939 if (TREE_READONLY_DECL_P (decl)
1940 && ! TREE_THIS_VOLATILE (decl)
61a127b3 1941 && DECL_INITIAL (decl)
bd6dd845 1942 && DECL_INITIAL (decl) != error_mark_node
8d08fdba
MS
1943 /* This is invalid if initial value is not constant.
1944 If it has either a function call, a memory reference,
1945 or a variable, then re-evaluating it could give different results. */
1946 && TREE_CONSTANT (DECL_INITIAL (decl))
1947 /* Check for cases where this is sub-optimal, even though valid. */
61a127b3 1948 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
8d08fdba
MS
1949 return DECL_INITIAL (decl);
1950 return decl;
1951}
1952\f
8d08fdba
MS
1953/* Common subroutines of build_new and build_vec_delete. */
1954
c787dd82 1955/* Call the global __builtin_delete to delete ADDR. */
8d08fdba 1956
bd6dd845 1957static tree
c787dd82
JM
1958build_builtin_delete_call (addr)
1959 tree addr;
8d08fdba 1960{
a6ecf8b6 1961 mark_used (global_delete_fndecl);
0c11ada6 1962 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
8d08fdba
MS
1963}
1964\f
1965/* Generate a C++ "new" expression. DECL is either a TREE_LIST
1966 (which needs to go through some sort of groktypename) or it
1967 is the name of the class we are newing. INIT is an initialization value.
1968 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1969 If INIT is void_type_node, it means do *not* call a constructor
1970 for this instance.
1971
1972 For types with constructors, the data returned is initialized
1973 by the appropriate constructor.
1974
1975 Whether the type has a constructor or not, if it has a pointer
1976 to a virtual function table, then that pointer is set up
1977 here.
1978
1979 Unless I am mistaken, a call to new () will return initialized
1980 data regardless of whether the constructor itself is private or
8926095f 1981 not. NOPE; new fails if the constructor is private (jcm).
8d08fdba
MS
1982
1983 Note that build_new does nothing to assure that any special
1984 alignment requirements of the type are met. Rather, it leaves
1985 it up to malloc to do the right thing. Otherwise, folding to
1986 the right alignment cal cause problems if the user tries to later
1987 free the memory returned by `new'.
1988
1989 PLACEMENT is the `placement' list for user-defined operator new (). */
1990
1991tree
1992build_new (placement, decl, init, use_global_new)
1993 tree placement;
1994 tree decl, init;
1995 int use_global_new;
1996{
a0d5fba7 1997 tree type, rval;
a703fb38 1998 tree nelts = NULL_TREE, t;
8926095f 1999 int has_array = 0;
8d08fdba 2000
8d08fdba
MS
2001 if (decl == error_mark_node)
2002 return error_mark_node;
2003
2004 if (TREE_CODE (decl) == TREE_LIST)
2005 {
2006 tree absdcl = TREE_VALUE (decl);
2007 tree last_absdcl = NULL_TREE;
8d08fdba
MS
2008
2009 if (current_function_decl
2010 && DECL_CONSTRUCTOR_P (current_function_decl))
2aa3110a 2011 my_friendly_assert (immediate_size_expand == 0, 19990926);
8d08fdba
MS
2012
2013 nelts = integer_one_node;
2014
2015 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
8926095f 2016 my_friendly_abort (215);
8d08fdba
MS
2017 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2018 {
2019 last_absdcl = absdcl;
2020 absdcl = TREE_OPERAND (absdcl, 0);
2021 }
2022
2023 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2024 {
2025 /* probably meant to be a vec new */
2026 tree this_nelts;
2027
51c184be
MS
2028 while (TREE_OPERAND (absdcl, 0)
2029 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2030 {
2031 last_absdcl = absdcl;
2032 absdcl = TREE_OPERAND (absdcl, 0);
2033 }
2034
8d08fdba
MS
2035 has_array = 1;
2036 this_nelts = TREE_OPERAND (absdcl, 1);
2037 if (this_nelts != error_mark_node)
2038 {
2039 if (this_nelts == NULL_TREE)
8251199e 2040 error ("new of array type fails to specify size");
5156628f 2041 else if (processing_template_decl)
5566b478
MS
2042 {
2043 nelts = this_nelts;
2044 absdcl = TREE_OPERAND (absdcl, 0);
2045 }
8d08fdba
MS
2046 else
2047 {
6a8f78d5
JM
2048 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
2049 if (build_expr_type_conversion (flags, this_nelts, 0)
2050 == NULL_TREE)
2051 pedwarn ("size in array new must have integral type");
2052
37c46b43 2053 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
8d08fdba
MS
2054 absdcl = TREE_OPERAND (absdcl, 0);
2055 if (this_nelts == integer_zero_node)
2056 {
8251199e 2057 warning ("zero size array reserves no space");
8d08fdba
MS
2058 nelts = integer_zero_node;
2059 }
2060 else
ab76ca54 2061 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
8d08fdba
MS
2062 }
2063 }
2064 else
2065 nelts = integer_zero_node;
2066 }
2067
2068 if (last_absdcl)
2069 TREE_OPERAND (last_absdcl, 0) = absdcl;
2070 else
2071 TREE_VALUE (decl) = absdcl;
2072
a0d5fba7 2073 type = groktypename (decl);
8926095f 2074 if (! type || type == error_mark_node)
2aa3110a 2075 return error_mark_node;
8d08fdba
MS
2076 }
2077 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2078 {
2079 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2080 {
2081 /* An aggregate type. */
2082 type = IDENTIFIER_TYPE_VALUE (decl);
d2e5ee5c 2083 decl = TYPE_MAIN_DECL (type);
8d08fdba
MS
2084 }
2085 else
2086 {
2087 /* A builtin type. */
2088 decl = lookup_name (decl, 1);
2089 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2090 type = TREE_TYPE (decl);
2091 }
8d08fdba
MS
2092 }
2093 else if (TREE_CODE (decl) == TYPE_DECL)
2094 {
2095 type = TREE_TYPE (decl);
8d08fdba
MS
2096 }
2097 else
2098 {
2099 type = decl;
d2e5ee5c 2100 decl = TYPE_MAIN_DECL (type);
8d08fdba
MS
2101 }
2102
5156628f 2103 if (processing_template_decl)
5566b478 2104 {
5566b478 2105 if (has_array)
a09ba2e0
MM
2106 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
2107 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2108 NULL_TREE);
5566b478
MS
2109 else
2110 t = type;
2111
2112 rval = build_min_nt (NEW_EXPR, placement, t, init);
2113 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2114 return rval;
2115 }
2116
8926095f
MS
2117 /* ``A reference cannot be created by the new operator. A reference
2118 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2119 returned by new.'' ARM 5.3.3 */
2120 if (TREE_CODE (type) == REFERENCE_TYPE)
8d08fdba 2121 {
8251199e 2122 error ("new cannot be applied to a reference type");
a0d5fba7 2123 type = TREE_TYPE (type);
8d08fdba
MS
2124 }
2125
b7484fbe
MS
2126 if (TREE_CODE (type) == FUNCTION_TYPE)
2127 {
8251199e 2128 error ("new cannot be applied to a function type");
b7484fbe
MS
2129 return error_mark_node;
2130 }
2131
8926095f
MS
2132 /* When the object being created is an array, the new-expression yields a
2133 pointer to the initial element (if any) of the array. For example,
2134 both new int and new int[10] return an int*. 5.3.4. */
2135 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
8d08fdba 2136 {
8926095f
MS
2137 nelts = array_type_nelts_top (type);
2138 has_array = 1;
a0d5fba7 2139 type = TREE_TYPE (type);
8d08fdba
MS
2140 }
2141
a0d5fba7
JM
2142 if (has_array)
2143 t = build_nt (ARRAY_REF, type, nelts);
2144 else
2145 t = type;
2146
2147 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2148 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2149 TREE_SIDE_EFFECTS (rval) = 1;
b3ab27f3
MM
2150 rval = build_new_1 (rval);
2151 if (rval == error_mark_node)
2152 return error_mark_node;
a0d5fba7
JM
2153
2154 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2155 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2156 TREE_NO_UNUSED_WARNING (rval) = 1;
2157
a0d5fba7
JM
2158 return rval;
2159}
2160
743f140d
PB
2161/* Given a Java class, return a decl for the corresponding java.lang.Class. */
2162
e97f22c9 2163tree
743f140d
PB
2164build_java_class_ref (type)
2165 tree type;
2166{
2167 tree name, class_decl;
669ec2b4
JM
2168 static tree CL_prefix = NULL_TREE;
2169 if (CL_prefix == NULL_TREE)
2170 CL_prefix = get_identifier("_CL_");
743f140d
PB
2171 if (jclass_node == NULL_TREE)
2172 {
2173 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2174 if (jclass_node == NULL_TREE)
2175 fatal("call to Java constructor, while `jclass' undefined");
2176 jclass_node = TREE_TYPE (jclass_node);
2177 }
669ec2b4 2178 name = build_overload_with_type (CL_prefix, type);
743f140d
PB
2179 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2180 if (class_decl == NULL_TREE)
2181 {
743f140d
PB
2182 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2183 TREE_STATIC (class_decl) = 1;
2184 DECL_EXTERNAL (class_decl) = 1;
2185 TREE_PUBLIC (class_decl) = 1;
2186 DECL_ARTIFICIAL (class_decl) = 1;
2187 DECL_IGNORED_P (class_decl) = 1;
2188 pushdecl_top_level (class_decl);
2189 make_decl_rtl (class_decl, NULL_PTR, 1);
743f140d
PB
2190 }
2191 return class_decl;
2192}
2193
834c6dff
MM
2194/* Returns teh size of the cookie to use when allocating an array
2195 whose elements have the indicated TYPE. Assumes that it is already
2196 known that a cookie is needed. */
2197
2198static tree
2199get_cookie_size (type)
2200 tree type;
2201{
2202 tree cookie_size;
2203
2204 if (flag_new_abi)
2205 {
2206 /* Under the new ABI, we need to allocate an additional max
2207 (sizeof (size_t), alignof (true_type)) bytes. */
2208 tree sizetype_size;
2209 tree type_align;
2210
2211 sizetype_size = size_in_bytes (sizetype);
2212 type_align = size_int (TYPE_ALIGN_UNIT (type));
2213 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2214 cookie_size = sizetype_size;
2215 else
2216 cookie_size = type_align;
2217 }
2218 else
708cae97
JM
2219 {
2220 if (TYPE_ALIGN (type) > TYPE_ALIGN (BI_header_type))
2221 return size_int (TYPE_ALIGN_UNIT (type));
2222 else
2223 return size_in_bytes (BI_header_type);
2224 }
834c6dff
MM
2225
2226 return cookie_size;
2227}
2228
a0d5fba7
JM
2229/* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2230 value is immediately handed to expand_expr. */
2231
834c6dff 2232static tree
a0d5fba7
JM
2233build_new_1 (exp)
2234 tree exp;
2235{
56c5d8bf 2236 tree placement, init;
a0d5fba7 2237 tree type, true_type, size, rval;
a703fb38
KG
2238 tree nelts = NULL_TREE;
2239 tree alloc_expr, alloc_node = NULL_TREE;
a0d5fba7 2240 int has_array = 0;
834c6dff 2241 enum tree_code code;
a0d5fba7 2242 int use_cookie, nothrow, check_new;
834c6dff
MM
2243 /* Nonzero if the user wrote `::new' rather than just `new'. */
2244 int globally_qualified_p;
2245 /* Nonzero if we're going to call a global operator new, rather than
2246 a class-specific version. */
a0d5fba7 2247 int use_global_new;
743f140d 2248 int use_java_new = 0;
834c6dff
MM
2249 /* If non-NULL, the number of extra bytes to allocate at the
2250 beginning of the storage allocated for an array-new expression in
2251 order to store the number of elements. */
2252 tree cookie_size = NULL_TREE;
a0d5fba7
JM
2253
2254 placement = TREE_OPERAND (exp, 0);
2255 type = TREE_OPERAND (exp, 1);
2256 init = TREE_OPERAND (exp, 2);
834c6dff 2257 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
a0d5fba7
JM
2258
2259 if (TREE_CODE (type) == ARRAY_REF)
2260 {
2261 has_array = 1;
2262 nelts = TREE_OPERAND (type, 1);
2263 type = TREE_OPERAND (type, 0);
2264 }
2265 true_type = type;
2266
834c6dff
MM
2267 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2268
91063b51 2269 if (CP_TYPE_QUALS (type))
8ccc31eb
MS
2270 type = TYPE_MAIN_VARIANT (type);
2271
8d08fdba
MS
2272 /* If our base type is an array, then make sure we know how many elements
2273 it has. */
2274 while (TREE_CODE (true_type) == ARRAY_TYPE)
2275 {
2276 tree this_nelts = array_type_nelts_top (true_type);
ab76ca54 2277 nelts = cp_build_binary_op (MULT_EXPR, nelts, this_nelts);
8d08fdba
MS
2278 true_type = TREE_TYPE (true_type);
2279 }
5566b478 2280
66543169 2281 if (!complete_type_or_else (true_type, exp))
8f259df3 2282 return error_mark_node;
5566b478 2283
8d08fdba 2284 if (has_array)
ab76ca54
MM
2285 size = fold (cp_build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2286 nelts));
8d08fdba
MS
2287 else
2288 size = size_in_bytes (type);
2289
cc600f33 2290 if (TREE_CODE (true_type) == VOID_TYPE)
e1cd6e56 2291 {
8251199e 2292 error ("invalid type `void' for new");
e1cd6e56
MS
2293 return error_mark_node;
2294 }
2295
a7a64a77
MM
2296 if (abstract_virtuals_error (NULL_TREE, true_type))
2297 return error_mark_node;
8926095f 2298
834c6dff
MM
2299 /* Figure out whether or not we're going to use the global operator
2300 new. */
2301 if (!globally_qualified_p
2302 && IS_AGGR_TYPE (true_type)
2303 && ((!has_array && TYPE_HAS_NEW_OPERATOR (true_type))
2304 || (has_array && TYPE_HAS_ARRAY_NEW_OPERATOR (true_type))))
2305 use_global_new = 0;
2306 else
2307 use_global_new = 1;
2308
2309 /* We only need cookies for arrays containing types for which we
2310 need cookies. */
2311 if (!has_array || !TYPE_VEC_NEW_USES_COOKIE (true_type))
2312 use_cookie = 0;
2313 /* When using placement new, users may not realize that they need
2314 the extra storage. Under the old ABI, we don't allocate the
2315 cookie whenever they use one placement argument of type `void
2316 *'. Under the new ABI, we require that the operator called be
2317 the global placement operator delete[]. */
2318 else if (placement && !TREE_CHAIN (placement)
2319 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2320 ptr_type_node))
2321 use_cookie = (!flag_new_abi || !use_global_new);
2322 /* Otherwise, we need the cookie. */
2323 else
2324 use_cookie = 1;
5156628f 2325
834c6dff
MM
2326 /* Compute the number of extra bytes to allocate, now that we know
2327 whether or not we need the cookie. */
5156628f 2328 if (use_cookie)
fc378698 2329 {
834c6dff
MM
2330 cookie_size = get_cookie_size (true_type);
2331 size = size_binop (PLUS_EXPR, size, cookie_size);
fc378698 2332 }
a28e3c7f 2333
834c6dff
MM
2334 if (has_array && init && pedantic)
2335 cp_pedwarn ("initialization in array new");
2336
e92cc029 2337 /* Allocate the object. */
da4768fe 2338
9bfadf57 2339 if (! placement && TYPE_FOR_JAVA (true_type))
743f140d 2340 {
8c1bd4f5 2341 tree class_addr, alloc_decl;
743f140d
PB
2342 tree class_decl = build_java_class_ref (true_type);
2343 tree class_size = size_in_bytes (true_type);
2344 static char alloc_name[] = "_Jv_AllocObject";
2345 use_java_new = 1;
2346 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2347 if (alloc_decl == NULL_TREE)
2348 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2349 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2350 rval = build_function_call (alloc_decl,
2351 tree_cons (NULL_TREE, class_addr,
2352 build_tree_list (NULL_TREE,
2353 class_size)));
2354 rval = cp_convert (build_pointer_type (true_type), rval);
2355 }
8d08fdba
MS
2356 else
2357 {
834c6dff
MM
2358 tree fnname;
2359 tree args;
2360
2361 args = tree_cons (NULL_TREE, size, placement);
596ea4e5 2362 fnname = ansi_opname (code);
834c6dff
MM
2363
2364 if (use_global_new)
2365 rval = (build_new_function_call
2366 (lookup_function_nonclass (fnname, args),
2367 args));
2368 else
2369 rval = build_method_call (build_dummy_object (true_type),
2370 fnname, args, NULL_TREE,
2371 LOOKUP_NORMAL);
da4768fe 2372 rval = cp_convert (build_pointer_type (true_type), rval);
8d08fdba
MS
2373 }
2374
2bb5d995
JM
2375 if (rval == error_mark_node)
2376 return error_mark_node;
2377
047f64a3
JM
2378 /* unless an allocation function is declared with an empty excep-
2379 tion-specification (_except.spec_), throw(), it indicates failure to
2380 allocate storage by throwing a bad_alloc exception (clause _except_,
2381 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2382 cation function is declared with an empty exception-specification,
2383 throw(), it returns null to indicate failure to allocate storage and a
2384 non-null pointer otherwise.
2385
2386 So check for a null exception spec on the op new we just called. */
2387
2388 nothrow = 0;
2389 if (rval)
2390 {
2391 /* The CALL_EXPR. */
2392 tree t = TREE_OPERAND (rval, 0);
2393 /* The function. */
2394 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
7f477e81 2395 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
047f64a3 2396 }
743f140d 2397 check_new = (flag_check_new || nothrow) && ! use_java_new;
047f64a3 2398
2face519 2399 if ((check_new || flag_exceptions) && rval)
2bb14213 2400 {
2face519
JM
2401 alloc_expr = get_target_expr (rval);
2402 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2bb14213 2403 }
b7484fbe
MS
2404 else
2405 alloc_expr = NULL_TREE;
d18c083e 2406
708cae97
JM
2407 /* if rval is NULL_TREE I don't have to allocate it, but are we
2408 totally sure we have some extra bytes in that case for the
8d08fdba
MS
2409 cookies? And how does that interact with the code below? (mrs) */
2410 /* Finish up some magic for new'ed arrays */
5156628f 2411 if (use_cookie && rval != NULL_TREE)
8d08fdba 2412 {
8d08fdba 2413 tree cookie, exp1;
a0d5fba7 2414 rval = convert (string_type_node, rval); /* for ptr arithmetic */
ab76ca54 2415 rval = save_expr (cp_build_binary_op (PLUS_EXPR, rval, cookie_size));
834c6dff
MM
2416 /* Store the number of bytes allocated so that we can know how
2417 many elements to destroy later. */
2418 if (flag_new_abi)
2419 {
2420 /* Under the new ABI, we use the last sizeof (size_t) bytes
2421 to store the number of elements. */
2422 cookie = build_indirect_ref (build (MINUS_EXPR,
2423 build_pointer_type (sizetype),
2424 rval,
2425 size_in_bytes (sizetype)),
2426 NULL_PTR);
2427 exp1 = build (MODIFY_EXPR, void_type_node, cookie, nelts);
2428 }
2429 else
2430 {
2431 cookie
2432 = build_indirect_ref (build (MINUS_EXPR,
2433 build_pointer_type (BI_header_type),
2434 rval, cookie_size), NULL_PTR);
2435 exp1 = build (MODIFY_EXPR, void_type_node,
2436 build_component_ref (cookie, nelts_identifier,
2437 NULL_TREE, 0),
2438 nelts);
2439 }
2440
2441 /* Build `(cookie = nelts, rval)' and use that as the complete
2442 expression. */
37c46b43 2443 rval = cp_convert (build_pointer_type (true_type), rval);
a0d5fba7 2444 rval = build_compound_expr
e1b3e07d 2445 (tree_cons (NULL_TREE, exp1,
051e6fd7 2446 build_tree_list (NULL_TREE, rval)));
8d08fdba
MS
2447 }
2448
8d08fdba
MS
2449 if (rval == error_mark_node)
2450 return error_mark_node;
8d08fdba
MS
2451
2452 /* Don't call any constructors or do any initialization. */
2453 if (init == void_type_node)
2454 goto done;
2455
8926095f 2456 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
8d08fdba 2457 {
b19b4a78
MS
2458 if (! TYPE_NEEDS_CONSTRUCTING (type)
2459 && ! IS_AGGR_TYPE (type) && ! has_array)
8d08fdba 2460 {
01240200
MM
2461 /* We are processing something like `new int (10)', which
2462 means allocate an int, and initialize it with 10. */
8ccc31eb 2463 tree deref;
01240200 2464 tree deref_type;
8ccc31eb 2465
01240200
MM
2466 /* At present RVAL is a temporary variable, created to hold
2467 the value from the call to `operator new'. We transform
2468 it to (*RVAL = INIT, RVAL). */
8ccc31eb
MS
2469 rval = save_expr (rval);
2470 deref = build_indirect_ref (rval, NULL_PTR);
01240200
MM
2471
2472 /* Even for something like `new const int (10)' we must
2473 allow the expression to be non-const while we do the
2474 initialization. */
2475 deref_type = TREE_TYPE (deref);
91063b51 2476 if (CP_TYPE_CONST_P (deref_type))
01240200 2477 TREE_TYPE (deref)
91063b51
MM
2478 = cp_build_qualified_type (deref_type,
2479 CP_TYPE_QUALS (deref_type)
2480 & ~TYPE_QUAL_CONST);
8ccc31eb 2481 TREE_READONLY (deref) = 0;
8d08fdba 2482
8ccc31eb 2483 if (TREE_CHAIN (init) != NULL_TREE)
8251199e 2484 pedwarn ("initializer list being treated as compound expression");
7215f9a0
MS
2485 else if (TREE_CODE (init) == CONSTRUCTOR)
2486 {
8251199e 2487 pedwarn ("initializer list appears where operand should be used");
7215f9a0
MS
2488 init = TREE_OPERAND (init, 1);
2489 }
8ccc31eb
MS
2490 init = build_compound_expr (init);
2491
2492 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2493 "new", NULL_TREE, 0);
8d08fdba 2494 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
8ccc31eb 2495 build_modify_expr (deref, NOP_EXPR, init),
8d08fdba 2496 rval);
e3417fcd 2497 TREE_NO_UNUSED_WARNING (rval) = 1;
8d08fdba 2498 TREE_SIDE_EFFECTS (rval) = 1;
8d08fdba 2499 }
8ccc31eb
MS
2500 else if (! has_array)
2501 {
2502 tree newrval;
2503 /* Constructors are never virtual. If it has an initialization, we
2504 need to complain if we aren't allowed to use the ctor that took
2505 that argument. */
2506 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2507
3dbc07b6 2508 rval = save_expr (rval);
8ccc31eb
MS
2509 newrval = rval;
2510
2511 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2512 newrval = build_indirect_ref (newrval, NULL_PTR);
2513
9eb71d8c
MM
2514 newrval = build_method_call (newrval,
2515 complete_ctor_identifier,
fc378698 2516 init, TYPE_BINFO (true_type), flags);
8ccc31eb 2517
80170791
MS
2518 if (newrval == NULL_TREE || newrval == error_mark_node)
2519 return error_mark_node;
2520
3dbc07b6 2521 newrval = build (COMPOUND_EXPR, TREE_TYPE (rval), newrval, rval);
80170791
MS
2522 rval = newrval;
2523 TREE_HAS_CONSTRUCTOR (rval) = 1;
8ccc31eb 2524 }
6f80451c 2525 else
b3ab27f3
MM
2526 rval = (build_vec_init
2527 (NULL_TREE,
2528 save_expr (rval),
ab76ca54 2529 cp_build_binary_op (MINUS_EXPR, nelts, integer_one_node),
b3ab27f3
MM
2530 init,
2531 /*from_array=*/0));
1f109f0f 2532
20c39572
JM
2533 /* If any part of the object initialization terminates by throwing an
2534 exception and a suitable deallocation function can be found, the
2535 deallocation function is called to free the memory in which the
2536 object was being constructed, after which the exception continues
2537 to propagate in the context of the new-expression. If no
2538 unambiguous matching deallocation function can be found,
2539 propagating the exception does not cause the object's memory to be
2540 freed. */
743f140d 2541 if (flag_exceptions && alloc_expr && ! use_java_new)
1f109f0f 2542 {
2face519 2543 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
519ebd1e 2544 tree cleanup, fn = NULL_TREE;
834c6dff
MM
2545 int flags = (LOOKUP_NORMAL
2546 | (globally_qualified_p * LOOKUP_GLOBAL));
a7d87521 2547
5355deec
AO
2548 /* The Standard is unclear here, but the right thing to do
2549 is to use the same method for finding deallocation
2550 functions that we use for finding allocation functions. */
2551 flags |= LOOKUP_SPECULATIVELY;
2552
2553 /* We expect alloc_expr to look like a TARGET_EXPR around
2554 a NOP_EXPR around the CALL_EXPR we want. */
2555 fn = TREE_OPERAND (alloc_expr, 1);
2556 fn = TREE_OPERAND (fn, 0);
da4768fe 2557
519ebd1e 2558 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2bb14213 2559
2face519
JM
2560 /* Ack! First we allocate the memory. Then we set our sentry
2561 variable to true, and expand a cleanup that deletes the memory
2562 if sentry is true. Then we run the constructor and store the
2563 returned pointer in buf. Then we clear sentry and return buf. */
2564
da4768fe
JM
2565 if (cleanup)
2566 {
2face519
JM
2567 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2568
2569 begin = get_target_expr (boolean_true_node);
2570 sentry = TREE_OPERAND (begin, 0);
2571
2face519
JM
2572 TREE_OPERAND (begin, 2)
2573 = build (COND_EXPR, void_type_node, sentry,
2574 cleanup, void_zero_node);
2face519
JM
2575
2576 rval = get_target_expr (rval);
2577
2578 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2579 sentry, boolean_false_node);
2face519
JM
2580
2581 buf = TREE_OPERAND (rval, 0);
2582
2583 rval = build (COMPOUND_EXPR, t, begin,
2584 build (COMPOUND_EXPR, t, rval,
2585 build (COMPOUND_EXPR, t, end, buf)));
da4768fe 2586 }
1f109f0f 2587 }
8d08fdba 2588 }
91063b51 2589 else if (CP_TYPE_CONST_P (true_type))
8251199e 2590 cp_error ("uninitialized const in `new' of `%#T'", true_type);
8ccc31eb 2591
8d08fdba 2592 done:
d18c083e 2593
2face519
JM
2594 if (alloc_expr && rval == alloc_node)
2595 {
2596 rval = TREE_OPERAND (alloc_expr, 1);
2597 alloc_expr = NULL_TREE;
2598 }
2599
2600 if (check_new && alloc_expr)
d18c083e 2601 {
b7484fbe 2602 /* Did we modify the storage? */
ab76ca54
MM
2603 tree ifexp = cp_build_binary_op (NE_EXPR, alloc_node,
2604 integer_zero_node);
2face519 2605 rval = build_conditional_expr (ifexp, rval, alloc_node);
d18c083e
MS
2606 }
2607
2face519
JM
2608 if (alloc_expr)
2609 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2610
51c184be
MS
2611 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2612 {
2613 /* The type of new int [3][3] is not int *, but int [3] * */
faf5394a 2614 rval = build_c_cast (build_pointer_type (type), rval);
51c184be
MS
2615 }
2616
00595019 2617 return rval;
8d08fdba
MS
2618}
2619\f
f30432d7 2620static tree
c7edeea3 2621build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
f30432d7 2622 tree base, maxindex, type;
86f45d2c 2623 special_function_kind auto_delete_vec;
f30432d7
MS
2624 int use_global_delete;
2625{
2626 tree virtual_size;
e92cc029 2627 tree ptype = build_pointer_type (type = complete_type (type));
f30432d7
MS
2628 tree size_exp = size_in_bytes (type);
2629
2630 /* Temporary variables used by the loop. */
2631 tree tbase, tbase_init;
2632
2633 /* This is the body of the loop that implements the deletion of a
2634 single element, and moves temp variables to next elements. */
2635 tree body;
2636
2637 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2638 tree loop;
2639
2640 /* This is the thing that governs what to do after the loop has run. */
2641 tree deallocate_expr = 0;
2642
2643 /* This is the BIND_EXPR which holds the outermost iterator of the
2644 loop. It is convenient to set this variable up and test it before
2645 executing any other code in the loop.
2646 This is also the containing expression returned by this function. */
2647 tree controller = NULL_TREE;
2648
834c6dff 2649 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
f30432d7
MS
2650 {
2651 loop = integer_zero_node;
2652 goto no_destructor;
2653 }
2654
708cae97 2655 /* The below is short by the cookie size. */
fed3cef0
RK
2656 virtual_size = size_binop (MULT_EXPR, size_exp,
2657 convert (sizetype, maxindex));
f30432d7 2658
46e8c075 2659 tbase = create_temporary_var (ptype);
f30432d7
MS
2660 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2661 fold (build (PLUS_EXPR, ptype,
2662 base,
2663 virtual_size)));
2664 DECL_REGISTER (tbase) = 1;
4dabb379 2665 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
f30432d7 2666 TREE_SIDE_EFFECTS (controller) = 1;
f30432d7 2667
c7edeea3 2668 body = NULL_TREE;
f30432d7 2669
e1b3e07d 2670 body = tree_cons (NULL_TREE,
86f45d2c 2671 build_delete (ptype, tbase, sfk_complete_destructor,
f30432d7
MS
2672 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2673 body);
2674
e1b3e07d 2675 body = tree_cons (NULL_TREE,
f30432d7
MS
2676 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2677 body);
2678
e1b3e07d 2679 body = tree_cons (NULL_TREE,
f30432d7
MS
2680 build (EXIT_EXPR, void_type_node,
2681 build (EQ_EXPR, boolean_type_node, base, tbase)),
2682 body);
2683
2684 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2685
e1b3e07d
MM
2686 loop = tree_cons (NULL_TREE, tbase_init,
2687 tree_cons (NULL_TREE, loop, NULL_TREE));
f30432d7
MS
2688 loop = build_compound_expr (loop);
2689
2690 no_destructor:
2691 /* If the delete flag is one, or anything else with the low bit set,
2692 delete the storage. */
86f45d2c
MM
2693 deallocate_expr = integer_zero_node;
2694 if (auto_delete_vec != sfk_base_destructor)
f30432d7
MS
2695 {
2696 tree base_tbd;
2697
708cae97 2698 /* The below is short by the cookie size. */
fed3cef0
RK
2699 virtual_size = size_binop (MULT_EXPR, size_exp,
2700 convert (sizetype, maxindex));
f30432d7
MS
2701
2702 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2703 /* no header */
2704 base_tbd = base;
2705 else
2706 {
834c6dff
MM
2707 tree cookie_size;
2708
2709 cookie_size = get_cookie_size (type);
2710 base_tbd
2711 = cp_convert (ptype,
ab76ca54
MM
2712 cp_build_binary_op (MINUS_EXPR,
2713 cp_convert (string_type_node,
2714 base),
2715 cookie_size));
e92cc029 2716 /* True size with header. */
834c6dff 2717 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
f30432d7 2718 }
86f45d2c
MM
2719
2720 if (auto_delete_vec == sfk_deleting_destructor)
2721 deallocate_expr = build_x_delete (base_tbd,
2722 2 | use_global_delete,
2723 virtual_size);
f30432d7
MS
2724 }
2725
2726 if (loop && deallocate_expr != integer_zero_node)
2727 {
e1b3e07d
MM
2728 body = tree_cons (NULL_TREE, loop,
2729 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
f30432d7
MS
2730 body = build_compound_expr (body);
2731 }
2732 else
2733 body = loop;
2734
2735 /* Outermost wrapper: If pointer is null, punt. */
37f88e3e
JM
2736 body = fold (build (COND_EXPR, void_type_node,
2737 fold (build (NE_EXPR, boolean_type_node, base,
2738 integer_zero_node)),
2739 body, integer_zero_node));
f30432d7
MS
2740 body = build1 (NOP_EXPR, void_type_node, body);
2741
2742 if (controller)
2743 {
2744 TREE_OPERAND (controller, 1) = body;
2745 return controller;
2746 }
2747 else
37c46b43 2748 return cp_convert (void_type_node, body);
f30432d7
MS
2749}
2750
c395453c
MM
2751/* Create an unnamed variable of the indicated TYPE. */
2752
f1dedc31
MM
2753tree
2754create_temporary_var (type)
8a72a046
MM
2755 tree type;
2756{
f1dedc31
MM
2757 tree decl;
2758
2759 decl = build_decl (VAR_DECL, NULL_TREE, type);
2760 TREE_USED (decl) = 1;
2761 DECL_ARTIFICIAL (decl) = 1;
2762 DECL_SOURCE_FILE (decl) = input_filename;
2763 DECL_SOURCE_LINE (decl) = lineno;
2764 DECL_IGNORED_P (decl) = 1;
b35d4555 2765 DECL_CONTEXT (decl) = current_function_decl;
f1dedc31 2766
f1dedc31 2767 return decl;
8a72a046
MM
2768}
2769
f1dedc31
MM
2770/* Create a new temporary variable of the indicated TYPE, initialized
2771 to INIT.
8a72a046 2772
f1dedc31
MM
2773 It is not entered into current_binding_level, because that breaks
2774 things when it comes time to do final cleanups (which take place
2775 "outside" the binding contour of the function). */
2776
2777static tree
2778get_temp_regvar (type, init)
2779 tree type, init;
f30432d7 2780{
f1dedc31 2781 tree decl;
8a72a046 2782
f1dedc31 2783 decl = create_temporary_var (type);
24bef158
MM
2784 if (building_stmt_tree ())
2785 add_decl_stmt (decl);
b7b8bcd2
MM
2786 if (!building_stmt_tree ())
2787 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
f1dedc31 2788 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
8a72a046 2789
f1dedc31 2790 return decl;
f30432d7
MS
2791}
2792
f1dedc31
MM
2793/* `build_vec_init' returns tree structure that performs
2794 initialization of a vector of aggregate types.
8d08fdba
MS
2795
2796 DECL is passed only for error reporting, and provides line number
2797 and source file name information.
f1dedc31
MM
2798 BASE is the space where the vector will be. For a vector of Ts,
2799 the type of BASE is `T*'.
8d08fdba
MS
2800 MAXINDEX is the maximum index of the array (one less than the
2801 number of elements).
2802 INIT is the (possibly NULL) initializer.
2803
2804 FROM_ARRAY is 0 if we should init everything with INIT
2805 (i.e., every element initialized from INIT).
2806 FROM_ARRAY is 1 if we should index into INIT in parallel
2807 with initialization of DECL.
2808 FROM_ARRAY is 2 if we should index into INIT in parallel,
2809 but use assignment instead of initialization. */
2810
2811tree
f1dedc31 2812build_vec_init (decl, base, maxindex, init, from_array)
8d08fdba
MS
2813 tree decl, base, maxindex, init;
2814 int from_array;
2815{
2816 tree rval;
8a72a046 2817 tree base2 = NULL_TREE;
8d08fdba 2818 tree size;
e833cb11 2819 tree itype = NULL_TREE;
8a72a046 2820 tree iterator;
f1dedc31
MM
2821 /* The type of an element in the array. */
2822 tree type;
2823 /* The type of a pointer to an element in the array. */
2824 tree ptype;
2825 tree stmt_expr;
2826 tree compound_stmt;
2827 int destroy_temps;
f5984164 2828 tree try_block = NULL_TREE;
486837a7 2829 tree try_body = NULL_TREE;
8a72a046 2830 int num_initialized_elts = 0;
8d08fdba 2831
37c46b43 2832 maxindex = cp_convert (ptrdiff_type_node, maxindex);
8d08fdba
MS
2833 if (maxindex == error_mark_node)
2834 return error_mark_node;
2835
f1dedc31
MM
2836 type = TREE_TYPE (TREE_TYPE (base));
2837 ptype = build_pointer_type (type);
8d08fdba
MS
2838 size = size_in_bytes (type);
2839
f1dedc31
MM
2840 /* The code we are generating looks like:
2841
2842 T* t1 = (T*) base;
2843 T* rval = base;
2844 ptrdiff_t iterator = maxindex;
2845 try {
2846 ... initializations from CONSTRUCTOR ...
2847 if (iterator != -1) {
2848 do {
2849 ... initialize *base ...
2850 ++base;
2851 } while (--iterator != -1);
2852 }
2853 } catch (...) {
2854 ... destroy elements that were constructed ...
2855 }
2856
2857 We can omit the try and catch blocks if we know that the
2858 initialization will never throw an exception, or if the array
2859 elements do not have destructors. If we have a CONSTRUCTOR to
2860 give us initialization information, we emit code to initialize
2861 each of the elements before the loop in the try block, and then
2862 iterate over fewer elements. We can omit the loop completely if
2863 the elements of the array do not have constructors.
2864
2865 We actually wrap the entire body of the above in a STMT_EXPR, for
2866 tidiness.
2867
2868 When copying from array to another, when the array elements have
2869 only trivial copy constructors, we should use __builtin_memcpy
2870 rather than generating a loop. That way, we could take advantage
2871 of whatever cleverness the back-end has for dealing with copies
2872 of blocks of memory. */
2873
2874 begin_init_stmts (&stmt_expr, &compound_stmt);
f2c5f623
BC
2875 destroy_temps = stmts_are_full_exprs_p ();
2876 current_stmt_tree->stmts_are_full_exprs_p = 0;
f1dedc31
MM
2877 rval = get_temp_regvar (ptype,
2878 cp_convert (ptype, default_conversion (base)));
2879 base = get_temp_regvar (ptype, rval);
8a72a046 2880 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
8d08fdba 2881
8a72a046
MM
2882 /* Protect the entire array initialization so that we can destroy
2883 the partially constructed array if an exception is thrown. */
834c6dff 2884 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
ed5511d9
MM
2885 {
2886 try_block = begin_try_block ();
2887 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2888 }
8a72a046
MM
2889
2890 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
3bfdc719 2891 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
8d08fdba 2892 {
8a72a046
MM
2893 /* Do non-default initialization resulting from brace-enclosed
2894 initializers. */
2895
2896 tree elts;
094fe153
JM
2897 from_array = 0;
2898
8a72a046 2899 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
8d08fdba 2900 {
8a72a046 2901 tree elt = TREE_VALUE (elts);
f1dedc31 2902 tree baseref = build1 (INDIRECT_REF, type, base);
8d08fdba 2903
8a72a046 2904 num_initialized_elts++;
8d08fdba 2905
8a72a046 2906 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
f1dedc31 2907 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
8a72a046 2908 else
f1dedc31
MM
2909 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2910 elt));
8a72a046 2911
f1dedc31
MM
2912 finish_expr_stmt (build_modify_expr
2913 (base,
2914 NOP_EXPR,
8a72a046 2915 build (PLUS_EXPR, build_pointer_type (type),
f1dedc31
MM
2916 base, size)));
2917 finish_expr_stmt (build_modify_expr
2918 (iterator,
2919 NOP_EXPR,
8a72a046 2920 build (MINUS_EXPR, ptrdiff_type_node,
f1dedc31 2921 iterator, integer_one_node)));
8d08fdba 2922 }
8d08fdba 2923
8a72a046
MM
2924 /* Clear out INIT so that we don't get confused below. */
2925 init = NULL_TREE;
8d08fdba 2926 }
8a72a046 2927 else if (from_array)
8d08fdba 2928 {
8a72a046
MM
2929 /* If initializing one array from another, initialize element by
2930 element. We rely upon the below calls the do argument
2931 checking. */
2932 if (decl == NULL_TREE)
8d08fdba 2933 {
8a72a046
MM
2934 sorry ("initialization of array from dissimilar array type");
2935 return error_mark_node;
8d08fdba 2936 }
8a72a046
MM
2937 if (init)
2938 {
2939 base2 = default_conversion (init);
2940 itype = TREE_TYPE (base2);
2941 base2 = get_temp_regvar (itype, base2);
2942 itype = TREE_TYPE (itype);
2943 }
2944 else if (TYPE_LANG_SPECIFIC (type)
2945 && TYPE_NEEDS_CONSTRUCTING (type)
2946 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2947 {
2948 error ("initializer ends prematurely");
2949 return error_mark_node;
2950 }
2951 }
8d08fdba 2952
8a72a046
MM
2953 /* Now, default-initialize any remaining elements. We don't need to
2954 do that if a) the type does not need constructing, or b) we've
094fe153
JM
2955 already initialized all the elements.
2956
2957 We do need to keep going if we're copying an array. */
2958
2959 if (from_array
2960 || (TYPE_NEEDS_CONSTRUCTING (type)
665f2503 2961 && ! (host_integerp (maxindex, 0)
05bccae2 2962 && (num_initialized_elts
665f2503 2963 == tree_low_cst (maxindex, 0) + 1))))
8a72a046 2964 {
37e05cd5 2965 /* If the ITERATOR is equal to -1, then we don't have to loop;
8a72a046 2966 we've already initialized all the elements. */
f1dedc31
MM
2967 tree if_stmt;
2968 tree do_stmt;
2969 tree do_body;
2970 tree elt_init;
2971
2972 if_stmt = begin_if_stmt ();
2973 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
9cd64686 2974 iterator, minus_one_node),
f1dedc31 2975 if_stmt);
8d08fdba 2976
8a72a046 2977 /* Otherwise, loop through the elements. */
f1dedc31
MM
2978 do_stmt = begin_do_stmt ();
2979 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2980
2981 /* When we're not building a statement-tree, things are a little
2982 complicated. If, when we recursively call build_aggr_init,
2983 an expression containing a TARGET_EXPR is expanded, then it
2984 may get a cleanup. Then, the result of that expression is
2985 passed to finish_expr_stmt, which will call
2986 expand_start_target_temps/expand_end_target_temps. However,
2987 the latter call will not cause the cleanup to run because
2988 that block will still be on the block stack. So, we call
2989 expand_start_target_temps here manually; the corresponding
2990 call to expand_end_target_temps below will cause the cleanup
2991 to be performed. */
2992 if (!building_stmt_tree ())
2993 expand_start_target_temps ();
0fac6b0b 2994
8d08fdba
MS
2995 if (from_array)
2996 {
2997 tree to = build1 (INDIRECT_REF, type, base);
2998 tree from;
2999
3000 if (base2)
3001 from = build1 (INDIRECT_REF, itype, base2);
3002 else
3003 from = NULL_TREE;
3004
3005 if (from_array == 2)
f1dedc31 3006 elt_init = build_modify_expr (to, NOP_EXPR, from);
8d08fdba 3007 else if (TYPE_NEEDS_CONSTRUCTING (type))
f1dedc31 3008 elt_init = build_aggr_init (to, from, 0);
8d08fdba 3009 else if (from)
f1dedc31 3010 elt_init = build_modify_expr (to, NOP_EXPR, from);
8d08fdba
MS
3011 else
3012 my_friendly_abort (57);
3013 }
3014 else if (TREE_CODE (type) == ARRAY_TYPE)
3015 {
3016 if (init != 0)
3017 sorry ("cannot initialize multi-dimensional array with initializer");
f1dedc31
MM
3018 elt_init = (build_vec_init
3019 (decl,
3020 build1 (NOP_EXPR,
3021 build_pointer_type (TREE_TYPE (type)),
3022 base),
3023 array_type_nelts (type), 0, 0));
3024 }
3025 else
3026 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
3027 init, 0);
3028
3029 /* The initialization of each array element is a
3030 full-expression. */
3031 if (!building_stmt_tree ())
3032 {
35b1567d 3033 genrtl_expr_stmt (elt_init);
f1dedc31 3034 expand_end_target_temps ();
8d08fdba
MS
3035 }
3036 else
f1dedc31 3037 {
f2c5f623 3038 current_stmt_tree->stmts_are_full_exprs_p = 1;
f1dedc31 3039 finish_expr_stmt (elt_init);
f2c5f623 3040 current_stmt_tree->stmts_are_full_exprs_p = 0;
f1dedc31 3041 }
8d08fdba 3042
f1dedc31
MM
3043 finish_expr_stmt (build_modify_expr
3044 (base,
3045 NOP_EXPR,
8a72a046 3046 build (PLUS_EXPR, build_pointer_type (type),
f1dedc31 3047 base, size)));
8d08fdba 3048 if (base2)
f1dedc31
MM
3049 finish_expr_stmt (build_modify_expr
3050 (base2,
3051 NOP_EXPR,
8a72a046 3052 build (PLUS_EXPR, build_pointer_type (type),
f1dedc31 3053 base2, size)));
0fac6b0b 3054
f1dedc31
MM
3055 finish_compound_stmt (/*has_no_scope=*/1, do_body);
3056 finish_do_body (do_stmt);
3057 finish_do_stmt (build (NE_EXPR, boolean_type_node,
3058 build (PREDECREMENT_EXPR,
3059 ptrdiff_type_node,
3060 iterator,
3061 integer_one_node),
9cd64686 3062 minus_one_node),
f1dedc31
MM
3063 do_stmt);
3064
3065 finish_then_clause (if_stmt);
3066 finish_if_stmt ();
8d08fdba 3067 }
8a72a046
MM
3068
3069 /* Make sure to cleanup any partially constructed elements. */
834c6dff 3070 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
f1dedc31
MM
3071 {
3072 tree e;
8d08fdba 3073
ed5511d9
MM
3074 finish_compound_stmt (/*has_no_scope=*/1, try_body);
3075 finish_cleanup_try_block (try_block);
f1dedc31 3076 e = build_vec_delete_1 (rval,
ab76ca54
MM
3077 cp_build_binary_op (MINUS_EXPR, maxindex,
3078 iterator),
f1dedc31 3079 type,
86f45d2c 3080 sfk_base_destructor,
f1dedc31 3081 /*use_global_delete=*/0);
f1dedc31
MM
3082 finish_cleanup (e, try_block);
3083 }
3084
f1dedc31
MM
3085 /* The value of the array initialization is the address of the
3086 first element in the array. */
3087 finish_expr_stmt (rval);
3088
3089 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
f2c5f623 3090 current_stmt_tree->stmts_are_full_exprs_p = destroy_temps;
f1dedc31 3091 return stmt_expr;
8d08fdba
MS
3092}
3093
3094/* Free up storage of type TYPE, at address ADDR.
3095
3096 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3097 of pointer.
3098
3099 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3100 used as the second argument to operator delete. It can include
3101 things like padding and magic size cookies. It has virtual in it,
3102 because if you have a base pointer and you delete through a virtual
3103 destructor, it should be the size of the dynamic object, not the
cab1f180 3104 static object, see Free Store 12.5 ISO C++.
8d08fdba
MS
3105
3106 This does not call any destructors. */
e92cc029 3107
8d08fdba 3108tree
b370501f
KG
3109build_x_delete (addr, which_delete, virtual_size)
3110 tree addr;
a28e3c7f 3111 int which_delete;
8d08fdba
MS
3112 tree virtual_size;
3113{
a28e3c7f
MS
3114 int use_global_delete = which_delete & 1;
3115 int use_vec_delete = !!(which_delete & 2);
a28e3c7f 3116 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
da4768fe 3117 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
8d08fdba 3118
519ebd1e 3119 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
8d08fdba
MS
3120}
3121
86f45d2c
MM
3122/* Call the DTOR_KIND destructor for EXP. FLAGS are as for
3123 build_delete. */
298d6f60
MM
3124
3125static tree
86f45d2c 3126build_dtor_call (exp, dtor_kind, flags)
298d6f60 3127 tree exp;
86f45d2c 3128 special_function_kind dtor_kind;
298d6f60
MM
3129 int flags;
3130{
86f45d2c
MM
3131 tree name;
3132
3133 switch (dtor_kind)
3134 {
3135 case sfk_complete_destructor:
3136 name = complete_dtor_identifier;
3137 break;
3138
3139 case sfk_base_destructor:
3140 name = base_dtor_identifier;
3141 break;
3142
3143 case sfk_deleting_destructor:
3144 name = deleting_dtor_identifier;
3145 break;
3146
3147 default:
3148 my_friendly_abort (20000524);
3149 }
3150 return build_method_call (exp, name, NULL_TREE, NULL_TREE, flags);
298d6f60
MM
3151}
3152
8d08fdba
MS
3153/* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3154 ADDR is an expression which yields the store to be destroyed.
86f45d2c
MM
3155 AUTO_DELETE is the name of the destructor to call, i.e., either
3156 sfk_complete_destructor, sfk_base_destructor, or
3157 sfk_deleting_destructor.
8d08fdba
MS
3158
3159 FLAGS is the logical disjunction of zero or more LOOKUP_
3160 flags. See cp-tree.h for more info.
3161
3162 This function does not delete an object's virtual base classes. */
e92cc029 3163
8d08fdba
MS
3164tree
3165build_delete (type, addr, auto_delete, flags, use_global_delete)
3166 tree type, addr;
86f45d2c 3167 special_function_kind auto_delete;
8d08fdba
MS
3168 int flags;
3169 int use_global_delete;
3170{
8d08fdba
MS
3171 tree member;
3172 tree expr;
3173 tree ref;
8d08fdba
MS
3174
3175 if (addr == error_mark_node)
3176 return error_mark_node;
3177
3178 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3179 set to `error_mark_node' before it gets properly cleaned up. */
3180 if (type == error_mark_node)
3181 return error_mark_node;
3182
3183 type = TYPE_MAIN_VARIANT (type);
3184
3185 if (TREE_CODE (type) == POINTER_TYPE)
3186 {
2986ae00 3187 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
b72801e2 3188 if (!VOID_TYPE_P (type) && !complete_type_or_else (type, addr))
8f259df3 3189 return error_mark_node;
8d08fdba
MS
3190 if (TREE_CODE (type) == ARRAY_TYPE)
3191 goto handle_array;
3192 if (! IS_AGGR_TYPE (type))
3193 {
3194 /* Call the builtin operator delete. */
c787dd82 3195 return build_builtin_delete_call (addr);
8d08fdba
MS
3196 }
3197 if (TREE_SIDE_EFFECTS (addr))
3198 addr = save_expr (addr);
2986ae00
MS
3199
3200 /* throw away const and volatile on target type of addr */
6060a796 3201 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba 3202 ref = build_indirect_ref (addr, NULL_PTR);
8d08fdba
MS
3203 }
3204 else if (TREE_CODE (type) == ARRAY_TYPE)
3205 {
3206 handle_array:
3207 if (TREE_SIDE_EFFECTS (addr))
3208 addr = save_expr (addr);
c407792d
RK
3209 if (TYPE_DOMAIN (type) == NULL_TREE)
3210 {
8251199e 3211 error ("unknown array size in delete");
c407792d
RK
3212 return error_mark_node;
3213 }
8d08fdba 3214 return build_vec_delete (addr, array_type_nelts (type),
c7edeea3 3215 auto_delete, use_global_delete);
8d08fdba
MS
3216 }
3217 else
3218 {
3219 /* Don't check PROTECT here; leave that decision to the
3220 destructor. If the destructor is accessible, call it,
3221 else report error. */
3222 addr = build_unary_op (ADDR_EXPR, addr, 0);
3223 if (TREE_SIDE_EFFECTS (addr))
3224 addr = save_expr (addr);
3225
3226 if (TREE_CONSTANT (addr))
3227 addr = convert_pointer_to (type, addr);
3228 else
6060a796 3229 addr = convert_force (build_pointer_type (type), addr, 0);
8d08fdba 3230
bd6dd845 3231 ref = build_indirect_ref (addr, NULL_PTR);
8d08fdba
MS
3232 }
3233
3234 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3235
834c6dff 3236 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
8d08fdba 3237 {
86f45d2c 3238 if (auto_delete == sfk_base_destructor)
8d08fdba
MS
3239 return void_zero_node;
3240
da4768fe
JM
3241 return build_op_delete_call
3242 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
519ebd1e
JM
3243 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3244 NULL_TREE);
8d08fdba 3245 }
8d08fdba
MS
3246
3247 /* Below, we will reverse the order in which these calls are made.
3248 If we have a destructor, then that destructor will take care
3249 of the base classes; otherwise, we must do that here. */
3250 if (TYPE_HAS_DESTRUCTOR (type))
3251 {
700f8a87 3252 tree do_delete = NULL_TREE;
bd6dd845 3253 tree ifexp;
700f8a87 3254
86f45d2c 3255 if (use_global_delete && auto_delete == sfk_deleting_destructor)
700f8a87 3256 {
86f45d2c
MM
3257 /* Delete the object. */
3258 do_delete = build_builtin_delete_call (addr);
3259 /* Otherwise, treat this like a complete object destructor
3260 call. */
3261 auto_delete = sfk_complete_destructor;
700f8a87 3262 }
8d08fdba 3263
86f45d2c 3264 expr = build_dtor_call (ref, auto_delete, flags);
bd6dd845
MS
3265 if (do_delete)
3266 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
9e9ff709 3267
bd6dd845
MS
3268 if (flags & LOOKUP_DESTRUCTOR)
3269 /* Explicit destructor call; don't check for null pointer. */
3270 ifexp = integer_one_node;
8d08fdba 3271 else
bd6dd845 3272 /* Handle deleting a null pointer. */
ab76ca54 3273 ifexp = fold (cp_build_binary_op (NE_EXPR, addr, integer_zero_node));
8d08fdba 3274
bd6dd845
MS
3275 if (ifexp != integer_one_node)
3276 expr = build (COND_EXPR, void_type_node,
3277 ifexp, expr, void_zero_node);
8d08fdba 3278
8d08fdba
MS
3279 return expr;
3280 }
3281 else
3282 {
bd6dd845 3283 /* We only get here from finish_function for a destructor. */
8d08fdba 3284 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
d43829f9 3285 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (type);
8d08fdba
MS
3286 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3287 tree exprstmt = NULL_TREE;
8d08fdba 3288
2a2480e1
JM
3289 /* Set this again before we call anything, as we might get called
3290 recursively. */
3291 TYPE_HAS_DESTRUCTOR (type) = 1;
3292
bd6dd845
MS
3293 /* If we have member delete or vbases, we call delete in
3294 finish_function. */
86f45d2c 3295 my_friendly_assert (auto_delete == sfk_base_destructor, 20000411);
8d08fdba
MS
3296
3297 /* Take care of the remaining baseclasses. */
298d6f60 3298 for (i = 0; i < n_baseclasses; i++)
8d08fdba
MS
3299 {
3300 base_binfo = TREE_VEC_ELT (binfos, i);
834c6dff 3301 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
8d08fdba
MS
3302 || TREE_VIA_VIRTUAL (base_binfo))
3303 continue;
3304
298d6f60
MM
3305 expr = build_scoped_method_call (ref, base_binfo,
3306 base_dtor_identifier,
3307 NULL_TREE);
8d08fdba 3308
e1b3e07d 3309 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
8d08fdba
MS
3310 }
3311
3312 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3313 {
3314 if (TREE_CODE (member) != FIELD_DECL)
3315 continue;
834c6dff 3316 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
8d08fdba 3317 {
4dabb379 3318 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
8d08fdba 3319 tree this_type = TREE_TYPE (member);
86f45d2c
MM
3320 expr = build_delete (this_type, this_member,
3321 sfk_complete_destructor, flags, 0);
e1b3e07d 3322 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
8d08fdba
MS
3323 }
3324 }
3325
3326 if (exprstmt)
3327 return build_compound_expr (exprstmt);
3328 /* Virtual base classes make this function do nothing. */
3329 return void_zero_node;
3330 }
3331}
3332
3333/* For type TYPE, delete the virtual baseclass objects of DECL. */
3334
3335tree
3336build_vbase_delete (type, decl)
3337 tree type, decl;
3338{
3339 tree vbases = CLASSTYPE_VBASECLASSES (type);
3340 tree result = NULL_TREE;
3341 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3342
3343 my_friendly_assert (addr != error_mark_node, 222);
3344
3345 while (vbases)
3346 {
a55583e9
MM
3347 tree this_addr
3348 = convert_force (build_pointer_type (BINFO_TYPE (TREE_VALUE (vbases))),
3349 addr, 0);
e1b3e07d 3350 result = tree_cons (NULL_TREE,
8d08fdba 3351 build_delete (TREE_TYPE (this_addr), this_addr,
86f45d2c 3352 sfk_base_destructor,
8d08fdba
MS
3353 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3354 result);
3355 vbases = TREE_CHAIN (vbases);
3356 }
3357 return build_compound_expr (nreverse (result));
3358}
3359
3360/* Build a C++ vector delete expression.
3361 MAXINDEX is the number of elements to be deleted.
3362 ELT_SIZE is the nominal size of each element in the vector.
3363 BASE is the expression that should yield the store to be deleted.
8d08fdba
MS
3364 This function expands (or synthesizes) these calls itself.
3365 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
8d08fdba
MS
3366
3367 This also calls delete for virtual baseclasses of elements of the vector.
3368
3369 Update: MAXINDEX is no longer needed. The size can be extracted from the
3370 start of the vector for pointers, and from the type for arrays. We still
3371 use MAXINDEX for arrays because it happens to already have one of the
3372 values we'd have to extract. (We could use MAXINDEX with pointers to
3373 confirm the size, and trap if the numbers differ; not clear that it'd
3374 be worth bothering.) */
e92cc029 3375
8d08fdba 3376tree
c7edeea3 3377build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
5566b478 3378 tree base, maxindex;
86f45d2c 3379 special_function_kind auto_delete_vec;
a28e3c7f 3380 int use_global_delete;
8d08fdba 3381{
f30432d7 3382 tree type;
8d08fdba 3383
c407792d
RK
3384 if (TREE_CODE (base) == OFFSET_REF)
3385 base = resolve_offset_ref (base);
3386
f30432d7 3387 type = TREE_TYPE (base);
c407792d 3388
8d08fdba
MS
3389 base = stabilize_reference (base);
3390
e92cc029 3391 /* Since we can use base many times, save_expr it. */
8d08fdba
MS
3392 if (TREE_SIDE_EFFECTS (base))
3393 base = save_expr (base);
3394
f30432d7 3395 if (TREE_CODE (type) == POINTER_TYPE)
8d08fdba
MS
3396 {
3397 /* Step back one from start of vector, and read dimension. */
834c6dff
MM
3398 tree cookie_addr;
3399
708cae97 3400 type = strip_array_types (TREE_TYPE (type));
834c6dff
MM
3401 if (flag_new_abi)
3402 {
3403 cookie_addr = build (MINUS_EXPR,
3404 build_pointer_type (sizetype),
3405 base,
3406 TYPE_SIZE_UNIT (sizetype));
3407 maxindex = build_indirect_ref (cookie_addr, NULL_PTR);
3408 }
3409 else
3410 {
3411 tree cookie;
3412
3413 cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
708cae97 3414 base, get_cookie_size (type));
834c6dff
MM
3415 cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3416 maxindex = build_component_ref (cookie, nelts_identifier,
3417 NULL_TREE, 0);
3418 }
8d08fdba 3419 }
f30432d7 3420 else if (TREE_CODE (type) == ARRAY_TYPE)
8d08fdba
MS
3421 {
3422 /* get the total number of things in the array, maxindex is a bad name */
f30432d7 3423 maxindex = array_type_nelts_total (type);
834c6dff 3424 type = strip_array_types (type);
8d08fdba
MS
3425 base = build_unary_op (ADDR_EXPR, base, 1);
3426 }
3427 else
3428 {
9e9ff709 3429 if (base != error_mark_node)
8251199e 3430 error ("type to vector delete is neither pointer or array type");
8d08fdba
MS
3431 return error_mark_node;
3432 }
8d08fdba 3433
c7edeea3 3434 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
f30432d7 3435 use_global_delete);
8d08fdba 3436}