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