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