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