gcc
[official-gcc.git] / gcc / cp / init.c
blobff8e0583434eb30889e951e44218937b6613bbf6
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)
6 This file is part of GNU CC.
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.
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.
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. */
23 /* High-level class interface. */
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"
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));
55 /* Set up local variable for this file. MUST BE CALLED AFTER
56 INIT_DECL_PROCESSING. */
58 static tree BI_header_type, BI_header_size;
60 void init_init_processing ()
62 tree fields[1];
64 minus_one_node = build_int_2 (-1, -1);
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);
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);
79 ggc_add_tree_root (&BI_header_type, 1);
80 ggc_add_tree_root (&BI_header_size, 1);
83 /* Called from initialize_vtbl_ptrs via dfs_walk. */
85 static tree
86 dfs_initialize_vtbl_ptrs (binfo, data)
87 tree binfo;
88 void *data;
90 if (!BINFO_PRIMARY_MARKED_P (binfo)
91 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
93 tree base_ptr = TREE_VALUE ((tree) data);
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);
106 expand_virtual_init (binfo, base_ptr);
109 SET_BINFO_MARKED (binfo);
111 return NULL_TREE;
114 /* Initialize all the vtable pointers for the hierarchy dominated by
115 TYPE. */
117 void
118 initialize_vtbl_ptrs (type, addr)
119 tree type;
120 tree addr;
122 tree list = build_tree_list (type, addr);
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);
137 /* 348 - 351 */
138 /* Subroutine of emit_base_init. */
140 static void
141 perform_member_init (member, name, init, explicit)
142 tree member, name, init;
143 int explicit;
145 tree decl;
146 tree type = TREE_TYPE (member);
148 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
150 if (decl == error_mark_node)
151 return;
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))
158 init = build (INIT_EXPR, type, decl, TREE_VALUE (init));
159 finish_expr_stmt (init);
161 else if (TYPE_NEEDS_CONSTRUCTING (type)
162 || (init && TYPE_HAS_CONSTRUCTOR (type)))
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);
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)
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));
180 else
181 finish_expr_stmt (build_aggr_init (decl, init, 0));
183 else
185 if (init == NULL_TREE)
187 if (explicit)
189 /* default-initialization. */
190 if (AGGREGATE_TYPE_P (type))
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));
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;
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);
213 else if (TREE_CODE (init) == TREE_LIST)
215 /* There was an explicit member initialization. Do some
216 work in that case. */
217 if (TREE_CHAIN (init))
219 warning ("initializer list treated as compound expression");
220 init = build_compound_expr (init);
222 else
223 init = TREE_VALUE (init);
226 if (init)
227 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
230 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
232 tree expr;
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);
239 if (expr != error_mark_node)
240 finish_subobject (expr);
244 extern int warn_reorder;
246 /* Subroutine of emit_member_init. */
248 static tree
249 sort_member_init (t)
250 tree t;
252 tree x, member, name, field;
253 tree init_list = NULL_TREE;
254 int last_pos = 0;
255 tree last_field = NULL_TREE;
257 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
259 int pos;
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;
267 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
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);
274 if (TREE_CODE (name) == IDENTIFIER_NODE)
275 field = IDENTIFIER_CLASS_VALUE (name);
276 else
278 my_friendly_assert (TREE_CODE (name) == FIELD_DECL, 348);
279 field = name;
282 /* If one member shadows another, get the outermost one. */
283 if (TREE_CODE (field) == TREE_LIST)
284 field = TREE_VALUE (field);
286 if (field == member)
288 if (warn_reorder)
290 if (pos < last_pos)
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");
296 last_pos = pos;
297 last_field = field;
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;
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);
315 /* Initializers for base members go at the end. */
316 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
318 name = TREE_PURPOSE (x);
319 if (name)
321 if (purpose_member (name, init_list))
323 cp_error ("multiple initializations given for member `%D'",
324 IDENTIFIER_CLASS_VALUE (name));
325 continue;
328 init_list = chainon (init_list,
329 build_tree_list (name, TREE_VALUE (x)));
330 TREE_PURPOSE (x) = NULL_TREE;
334 return init_list;
337 static void
338 sort_base_init (t, rbase_ptr, vbase_ptr)
339 tree t, *rbase_ptr, *vbase_ptr;
341 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
342 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
344 int i;
345 tree x;
346 tree last;
348 /* For warn_reorder. */
349 int last_pos = 0;
350 tree last_base = NULL_TREE;
352 tree rbases = NULL_TREE;
353 tree vbases = NULL_TREE;
355 /* First walk through and splice out vbase and invalid initializers.
356 Also replace names with binfos. */
358 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
359 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
361 tree basetype = TREE_PURPOSE (x);
362 tree binfo = NULL_TREE;
364 if (basetype == NULL_TREE)
366 /* Initializer for single base class. Must not
367 use multiple inheritance or this is ambiguous. */
368 switch (n_baseclasses)
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;
381 binfo = TREE_VEC_ELT (binfos, 0);
383 else if (is_aggr_type (basetype, 1))
385 binfo = binfo_or_else (basetype, t);
386 if (binfo == NULL_TREE)
387 continue;
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))
394 tree v = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
395 vbases = tree_cons (v, TREE_VALUE (x), vbases);
396 continue;
398 else
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)
406 cp_error ("`%T' is not an immediate base class of `%T'",
407 basetype, current_class_type);
408 continue;
412 else
413 my_friendly_abort (365);
415 TREE_PURPOSE (x) = binfo;
416 TREE_CHAIN (last) = x;
417 last = x;
419 TREE_CHAIN (last) = NULL_TREE;
421 /* Now walk through our regular bases and make sure they're initialized. */
423 for (i = 0; i < n_baseclasses; ++i)
425 tree base_binfo = TREE_VEC_ELT (binfos, i);
426 int pos;
428 if (TREE_VIA_VIRTUAL (base_binfo))
429 continue;
431 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
433 tree binfo = TREE_PURPOSE (x);
435 if (binfo == NULL_TREE)
436 continue;
438 if (binfo == base_binfo)
440 if (warn_reorder)
442 if (pos < last_pos)
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");
448 last_pos = pos;
449 last_base = BINFO_TYPE (binfo);
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;
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);
467 *rbase_ptr = rbases;
468 *vbase_ptr = vbases;
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.
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.
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.
486 Note that emit_base_init does *not* initialize virtual base
487 classes. That is done specially, elsewhere. */
489 tree
490 emit_base_init (t)
491 tree t;
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;
502 mem_init_list = sort_member_init (t);
503 current_member_init_list = NULL_TREE;
505 sort_base_init (t, &rbase_init_list, &vbase_init_list);
506 current_base_init_list = NULL_TREE;
508 begin_init_stmts (&stmt_expr, &compound_stmt);
510 /* First, initialize the virtual base classes, if we are
511 constructing the most-derived object. */
512 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
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);
519 /* Now, perform initialization of non-virtual base classes. */
520 for (i = 0; i < n_baseclasses; i++)
522 tree base_binfo = TREE_VEC_ELT (binfos, i);
523 tree init = void_list_node;
525 if (TREE_VIA_VIRTUAL (base_binfo))
526 continue;
528 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
529 999);
531 if (TREE_PURPOSE (rbase_init_list))
532 init = TREE_VALUE (rbase_init_list);
533 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
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));
541 if (init != void_list_node)
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);
549 expand_cleanup_for_base (base_binfo, NULL_TREE);
550 rbase_init_list = TREE_CHAIN (rbase_init_list);
553 /* Initialize the vtable pointers for the class. */
554 initialize_vtbl_ptrs (t, current_class_ptr);
556 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
558 tree init, name;
559 int from_init_list;
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;
567 /* See if we had a user-specified member initialization. */
568 if (TREE_PURPOSE (mem_init_list))
570 name = TREE_PURPOSE (mem_init_list);
571 init = TREE_VALUE (mem_init_list);
572 from_init_list = 1;
574 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE
575 || TREE_CODE (name) == FIELD_DECL, 349);
577 else
579 name = DECL_NAME (member);
580 init = DECL_INITIAL (member);
582 from_init_list = 0;
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);
591 perform_member_init (member, name, init, from_init_list);
592 mem_init_list = TREE_CHAIN (mem_init_list);
595 /* Now initialize any members from our bases. */
596 while (mem_init_list)
598 tree name, init, field;
600 if (TREE_PURPOSE (mem_init_list))
602 name = TREE_PURPOSE (mem_init_list);
603 init = TREE_VALUE (mem_init_list);
605 if (TREE_CODE (name) == IDENTIFIER_NODE)
606 field = IDENTIFIER_CLASS_VALUE (name);
607 else
608 field = name;
610 /* If one member shadows another, get the outermost one. */
611 if (TREE_CODE (field) == TREE_LIST)
613 field = TREE_VALUE (field);
614 if (decl_type_context (field) != current_class_type)
615 cp_error ("field `%D' not in immediate context", field);
618 perform_member_init (field, name, init, 1);
620 mem_init_list = TREE_CHAIN (mem_init_list);
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);
628 /* Check that all fields are properly initialized after
629 an assignment to `this'. Called only when such an assignment
630 is actually noted. */
632 void
633 check_base_init (t)
634 tree t;
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);
643 /* This code sets up the virtual function tables appropriate for
644 the pointer DECL. It is a one-ply initialization.
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. */
649 static void
650 expand_virtual_init (binfo, decl)
651 tree binfo, decl;
653 tree type = BINFO_TYPE (binfo);
654 tree vtbl, vtbl_ptr;
655 tree vtype, vtype_binfo;
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));
662 if (TREE_CODE (vtbl) == VAR_DECL)
664 assemble_external (vtbl);
665 TREE_USED (vtbl) = 1;
666 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
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);
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));
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;
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));
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. */
699 static void
700 expand_cleanup_for_base (binfo, flag)
701 tree binfo;
702 tree flag;
704 tree expr;
706 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (binfo)))
707 return;
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));
718 finish_subobject (expr);
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. */
725 static void
726 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
727 tree binfo, exp, addr, init_list;
729 tree init = purpose_member (binfo, init_list);
730 tree ref = build_indirect_ref (addr, NULL_PTR);
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);
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. */
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;
751 tree vbases;
753 /* If there are no virtual baseclasses, we shouldn't even be here. */
754 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
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 ())
760 tree if_stmt;
761 tree result;
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 ();
772 /* Now, run through the baseclasses, initializing each. */
773 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
774 vbases = TREE_CHAIN (vbases))
776 tree inner_if_stmt;
777 tree compound_stmt;
778 tree exp;
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);
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);
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 ();
816 expand_cleanup_for_base (vbases, flag);
820 /* Find the context in which this FIELD can be initialized. */
822 static tree
823 initializing_context (field)
824 tree field;
826 tree t = DECL_CONTEXT (field);
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;
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.
840 MEMBER_NAME is the name of the member. */
842 static int
843 member_init_ok_or_else (field, type, member_name)
844 tree field;
845 tree type;
846 const char *member_name;
848 if (field == error_mark_node)
849 return 0;
850 if (field == NULL_TREE || initializing_context (field) != type)
852 cp_error ("class `%T' does not have any field named `%s'", type,
853 member_name);
854 return 0;
856 if (TREE_STATIC (field))
858 cp_error ("field `%#D' is static; only point of initialization is its declaration",
859 field);
860 return 0;
863 return 1;
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.
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.
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.
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'. */
882 void
883 expand_member_init (exp, name, init)
884 tree exp, name, init;
886 tree basetype = NULL_TREE, field;
887 tree type;
889 if (exp == NULL_TREE)
890 return; /* complain about this later */
892 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
894 if (name && TREE_CODE (name) == TYPE_DECL)
896 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
897 name = DECL_NAME (name);
900 if (name == NULL_TREE && IS_AGGR_TYPE (type))
901 switch (CLASSTYPE_N_BASECLASSES (type))
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;
915 my_friendly_assert (init != NULL_TREE, 0);
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. */
923 if (init == void_type_node)
924 init = NULL_TREE;
926 if (name == NULL_TREE || basetype)
928 tree base_init;
930 if (name == NULL_TREE)
932 #if 0
933 if (basetype)
934 name = TYPE_IDENTIFIER (basetype);
935 else
937 error ("no base class to initialize");
938 return;
940 #endif
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))
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;
959 if (purpose_member (basetype, current_base_init_list))
961 cp_error ("base class `%T' already initialized", basetype);
962 return;
965 if (warn_reorder && current_member_init_list)
967 cp_warning ("base initializer for `%T'", basetype);
968 warning (" will be re-ordered to precede member initializations");
971 base_init = build_tree_list (basetype, init);
972 current_base_init_list = chainon (current_base_init_list, base_init);
974 else
976 tree member_init;
978 try_member:
979 field = lookup_field (type, name, 1, 0);
981 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
982 return;
984 if (purpose_member (name, current_member_init_list))
986 cp_error ("field `%D' already initialized", field);
987 return;
990 member_init = build_tree_list (name, init);
991 current_member_init_list = chainon (current_member_init_list, member_init);
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. */
1004 void
1005 begin_init_stmts (stmt_expr_p, compound_stmt_p)
1006 tree *stmt_expr_p;
1007 tree *compound_stmt_p;
1009 *stmt_expr_p = begin_stmt_expr ();
1010 *compound_stmt_p = begin_compound_stmt (/*has_no_scope=*/1);
1013 /* Finish out the statement-expression begun by the previous call to
1014 begin_init_stmts. Returns the statement-expression itself. */
1016 tree
1017 finish_init_stmts (stmt_expr, compound_stmt)
1018 tree stmt_expr;
1019 tree compound_stmt;
1021 finish_compound_stmt (/*has_no_scope=*/1, compound_stmt);
1022 stmt_expr = finish_stmt_expr (stmt_expr);
1024 /* To avoid spurious warnings about unused values, we set
1025 TREE_USED. */
1026 if (stmt_expr)
1027 TREE_USED (stmt_expr) = 1;
1029 return stmt_expr;
1032 /* This is like `expand_member_init', only it stores one aggregate
1033 value into another.
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.
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.
1050 The virtual function table pointer cannot be set up here, because
1051 we do not really know its type.
1053 Virtual baseclass pointers are also set up here.
1055 This never calls operator=().
1057 When initializing, nothing is CONST.
1059 A default copy constructor may have to be used to perform the
1060 initialization.
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. */
1065 tree
1066 build_aggr_init (exp, init, flags)
1067 tree exp, init;
1068 int flags;
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);
1077 if (init == error_mark_node)
1078 return error_mark_node;
1080 TREE_READONLY (exp) = 0;
1081 TREE_THIS_VOLATILE (exp) = 0;
1083 if (init && TREE_CODE (init) != TREE_LIST)
1084 flags |= LOOKUP_ONLYCONVERTING;
1086 if (TREE_CODE (type) == ARRAY_TYPE)
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)
1093 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1094 if (init)
1095 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1097 if (init && TREE_TYPE (init) == NULL_TREE)
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() {};
1107 int main(int argc, char **argv) {
1108 COMPLEX zees(1.0, 0.0)[10];
1111 error ("bad array initializer");
1112 return error_mark_node;
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;
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;
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;
1141 return stmt_expr;
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;
1151 tree type = TREE_TYPE (exp);
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;
1162 if (init && TREE_CODE (init) != TREE_LIST
1163 && (flags & LOOKUP_ONLYCONVERTING))
1165 /* Base subobjects should only get direct-initialization. */
1166 if (true_exp != exp)
1167 abort ();
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. */
1179 else
1180 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
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;
1196 if (init == NULL_TREE
1197 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1199 parms = init;
1200 if (parms)
1201 init = TREE_VALUE (parms);
1203 else
1204 parms = build_tree_list (NULL_TREE, init);
1206 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
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;
1215 rval = build_method_call (exp, ctor_identifier,
1216 parms, binfo, flags);
1217 if (TREE_SIDE_EFFECTS (rval))
1218 finish_expr_stmt (rval);
1221 /* This function is responsible for initializing EXP with INIT
1222 (if any).
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.
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.
1238 FLAGS is just passes to `build_method_call'. See that function for
1239 its description. */
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;
1248 tree type = TREE_TYPE (exp);
1250 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
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. */
1258 if (init && TREE_CODE (exp) == VAR_DECL
1259 && TREE_CODE (init) == CONSTRUCTOR
1260 && TREE_HAS_CONSTRUCTOR (init))
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))
1267 if (!building_stmt_tree ())
1268 expand_decl_init (exp);
1270 else
1271 finish_expr_stmt (build (INIT_EXPR, type, exp, init));
1272 return;
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);
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. */
1284 is_aggr_typedef (name, or_else)
1285 tree name;
1286 int or_else;
1288 tree type;
1290 if (name == error_mark_node)
1291 return 0;
1293 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1294 type = IDENTIFIER_TYPE_VALUE (name);
1295 else
1297 if (or_else)
1298 cp_error ("`%T' is not an aggregate typedef", name);
1299 return 0;
1302 if (! IS_AGGR_TYPE (type)
1303 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1304 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1306 if (or_else)
1307 cp_error ("`%T' is not an aggregate type", type);
1308 return 0;
1310 return 1;
1313 /* Report an error if TYPE is not a user-defined, aggregate type. If
1314 OR_ELSE is nonzero, give an error message. */
1317 is_aggr_type (type, or_else)
1318 tree type;
1319 int or_else;
1321 if (type == error_mark_node)
1322 return 0;
1324 if (! IS_AGGR_TYPE (type)
1325 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1326 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1328 if (or_else)
1329 cp_error ("`%T' is not an aggregate type", type);
1330 return 0;
1332 return 1;
1335 /* Like is_aggr_typedef, but returns typedef if successful. */
1337 tree
1338 get_aggr_from_typedef (name, or_else)
1339 tree name;
1340 int or_else;
1342 tree type;
1344 if (name == error_mark_node)
1345 return NULL_TREE;
1347 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1348 type = IDENTIFIER_TYPE_VALUE (name);
1349 else
1351 if (or_else)
1352 cp_error ("`%T' fails to be an aggregate typedef", name);
1353 return NULL_TREE;
1356 if (! IS_AGGR_TYPE (type)
1357 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1358 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1360 if (or_else)
1361 cp_error ("type `%T' is of non-aggregate type", type);
1362 return NULL_TREE;
1364 return type;
1367 tree
1368 get_type_value (name)
1369 tree name;
1371 if (name == error_mark_node)
1372 return NULL_TREE;
1374 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1375 return IDENTIFIER_TYPE_VALUE (name);
1376 else
1377 return NULL_TREE;
1381 /* This code could just as well go in `class.c', but is placed here for
1382 modularity. */
1384 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1385 the appropriate function call. */
1387 tree
1388 build_member_call (type, name, parmlist)
1389 tree type, name, parmlist;
1391 tree t;
1392 tree method_name;
1393 int dtor = 0;
1394 tree basetype_path, decl;
1396 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1397 && TREE_CODE (type) == NAMESPACE_DECL)
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)
1408 method_name = lookup_namespace_name
1409 (type, TREE_OPERAND (method_name, 0));
1410 TREE_OPERAND (name, 0) = method_name;
1412 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1413 return build_x_function_call (name, parmlist, current_class_ref);
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);
1423 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
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;
1432 else
1433 method_name = name;
1435 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1437 method_name = TREE_OPERAND (method_name, 0);
1438 dtor = 1;
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)
1446 tree ns = lookup_name (type, 0);
1447 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1449 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1453 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1454 return error_mark_node;
1456 /* An operator we did not like. */
1457 if (name == NULL_TREE)
1458 return error_mark_node;
1460 if (dtor)
1462 cp_error ("cannot call destructor `%T::~%T' without object", type,
1463 method_name);
1464 return error_mark_node;
1467 decl = maybe_dummy_object (type, &basetype_path);
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))
1476 tree olddecl = current_class_ptr;
1477 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1478 if (oldtype != type)
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);
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))))
1498 if (t == error_mark_node)
1499 return error_mark_node;
1500 if (TREE_CODE (t) == FIELD_DECL)
1502 if (is_dummy_object (decl))
1504 cp_error ("invalid use of non-static field `%D'", t);
1505 return error_mark_node;
1507 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1509 else if (TREE_CODE (t) == VAR_DECL)
1510 decl = t;
1511 else
1513 cp_error ("invalid use of member `%D'", t);
1514 return error_mark_node;
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);
1521 else
1523 cp_error ("no method `%T::%D'", type, name);
1524 return error_mark_node;
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.
1533 @@ Prints out lousy diagnostics for operator <typename>
1534 @@ fields.
1536 @@ This function should be rewritten and placed in search.c. */
1538 tree
1539 build_offset_ref (type, name)
1540 tree type, name;
1542 tree decl, t = error_mark_node;
1543 tree member;
1544 tree basebinfo = NULL_TREE;
1545 tree orig_name = name;
1547 /* class templates can come in as TEMPLATE_DECLs here. */
1548 if (TREE_CODE (name) == TEMPLATE_DECL)
1549 return name;
1551 if (type == std_node)
1552 return do_scoped_id (name, 0);
1554 if (processing_template_decl || uses_template_parms (type))
1555 return build_min_nt (SCOPE_REF, type, name);
1557 /* Handle namespace names fully here. */
1558 if (TREE_CODE (type) == NAMESPACE_DECL)
1560 t = lookup_namespace_name (type, name);
1561 if (t != error_mark_node && ! type_unknown_p (t))
1563 mark_used (t);
1564 t = convert_from_reference (t);
1566 return t;
1569 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1570 return error_mark_node;
1572 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
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);
1580 if (DECL_P (name))
1581 name = DECL_NAME (name);
1582 else
1584 if (TREE_CODE (name) == LOOKUP_EXPR)
1585 /* This can happen during tsubst'ing. */
1586 name = TREE_OPERAND (name, 0);
1587 else
1589 if (TREE_CODE (name) == COMPONENT_REF)
1590 name = TREE_OPERAND (name, 1);
1591 if (TREE_CODE (name) == OVERLOAD)
1592 name = DECL_NAME (OVL_CURRENT (name));
1596 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1599 if (TREE_CODE (name) == BIT_NOT_EXPR)
1601 if (! check_dtor_name (type, name))
1602 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1603 type, TREE_OPERAND (name, 0));
1604 name = dtor_identifier;
1606 #if 0
1607 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1608 else if (name == constructor_name_full (type)
1609 || name == constructor_name (type))
1610 name = ctor_identifier;
1611 #endif
1613 if (!COMPLETE_TYPE_P (complete_type (type))
1614 && !TYPE_BEING_DEFINED (type))
1616 cp_error ("incomplete type `%T' does not have member `%D'", type,
1617 name);
1618 return error_mark_node;
1621 decl = maybe_dummy_object (type, &basebinfo);
1623 member = lookup_member (basebinfo, name, 1, 0);
1625 if (member == error_mark_node)
1626 return error_mark_node;
1628 /* A lot of this logic is now handled in lookup_member. */
1629 if (member && BASELINK_P (member))
1631 /* Go from the TREE_BASELINK to the member function info. */
1632 tree fnfields = member;
1633 t = TREE_VALUE (fnfields);
1635 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1637 /* The FNFIELDS are going to contain functions that aren't
1638 necessarily templates, and templates that don't
1639 necessarily match the explicit template parameters. We
1640 save all the functions, and the explicit parameters, and
1641 then figure out exactly what to instantiate with what
1642 arguments in instantiate_type. */
1644 if (TREE_CODE (t) != OVERLOAD)
1645 /* The code in instantiate_type which will process this
1646 expects to encounter OVERLOADs, not raw functions. */
1647 t = ovl_cons (t, NULL_TREE);
1649 return build (OFFSET_REF,
1650 unknown_type_node,
1651 decl,
1652 build (TEMPLATE_ID_EXPR,
1653 TREE_TYPE (t),
1655 TREE_OPERAND (orig_name, 1)));
1658 if (!really_overloaded_fn (t))
1660 /* Get rid of a potential OVERLOAD around it */
1661 t = OVL_CURRENT (t);
1663 /* unique functions are handled easily. */
1664 if (!enforce_access (basebinfo, t))
1665 return error_mark_node;
1666 mark_used (t);
1667 if (DECL_STATIC_FUNCTION_P (t))
1668 return t;
1669 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1672 TREE_TYPE (fnfields) = unknown_type_node;
1673 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1676 t = member;
1678 if (t == NULL_TREE)
1680 cp_error ("`%D' is not a member of type `%T'", name, type);
1681 return error_mark_node;
1684 if (TREE_CODE (t) == TYPE_DECL)
1686 TREE_USED (t) = 1;
1687 return t;
1689 /* static class members and class-specific enum
1690 values can be returned without further ado. */
1691 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1693 mark_used (t);
1694 return convert_from_reference (t);
1697 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1699 cp_error ("illegal pointer to bit field `%D'", t);
1700 return error_mark_node;
1703 /* static class functions too. */
1704 if (TREE_CODE (t) == FUNCTION_DECL
1705 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1706 my_friendly_abort (53);
1708 /* In member functions, the form `type::name' is no longer
1709 equivalent to `this->type::name', at least not until
1710 resolve_offset_ref. */
1711 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1714 /* If a OFFSET_REF made it through to here, then it did
1715 not have its address taken. */
1717 tree
1718 resolve_offset_ref (exp)
1719 tree exp;
1721 tree type = TREE_TYPE (exp);
1722 tree base = NULL_TREE;
1723 tree member;
1724 tree basetype, addr;
1726 if (TREE_CODE (exp) == OFFSET_REF)
1728 member = TREE_OPERAND (exp, 1);
1729 base = TREE_OPERAND (exp, 0);
1731 else
1733 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1734 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1736 error ("object missing in use of pointer-to-member construct");
1737 return error_mark_node;
1739 member = exp;
1740 type = TREE_TYPE (type);
1741 base = current_class_ref;
1744 if (BASELINK_P (member))
1746 if (! flag_ms_extensions)
1747 cp_pedwarn ("assuming & on overloaded member function");
1748 return build_unary_op (ADDR_EXPR, exp, 0);
1751 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1753 if (! flag_ms_extensions)
1754 cp_pedwarn ("assuming & on `%E'", member);
1755 return build_unary_op (ADDR_EXPR, exp, 0);
1758 if ((TREE_CODE (member) == VAR_DECL
1759 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1760 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1761 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1763 /* These were static members. */
1764 if (mark_addressable (member) == 0)
1765 return error_mark_node;
1766 return member;
1769 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1770 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1771 return member;
1773 /* Syntax error can cause a member which should
1774 have been seen as static to be grok'd as non-static. */
1775 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1777 if (TREE_ADDRESSABLE (member) == 0)
1779 cp_error_at ("member `%D' is non-static but referenced as a static member",
1780 member);
1781 error ("at this point in file");
1782 TREE_ADDRESSABLE (member) = 1;
1784 return error_mark_node;
1787 /* The first case is really just a reference to a member of `this'. */
1788 if (TREE_CODE (member) == FIELD_DECL
1789 && (base == current_class_ref || is_dummy_object (base)))
1791 tree expr;
1793 basetype = DECL_CONTEXT (member);
1795 /* Try to get to basetype from 'this'; if that doesn't work,
1796 nothing will. */
1797 base = current_class_ref;
1799 /* First convert to the intermediate base specified, if appropriate. */
1800 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1801 base = build_scoped_ref (base, TYPE_OFFSET_BASETYPE (type));
1803 addr = build_unary_op (ADDR_EXPR, base, 0);
1804 addr = convert_pointer_to (basetype, addr);
1806 if (addr == error_mark_node)
1807 return error_mark_node;
1809 expr = build (COMPONENT_REF, TREE_TYPE (member),
1810 build_indirect_ref (addr, NULL_PTR), member);
1811 return convert_from_reference (expr);
1814 /* Ensure that we have an object. */
1815 if (is_dummy_object (base))
1816 addr = error_mark_node;
1817 else
1818 /* If this is a reference to a member function, then return the
1819 address of the member function (which may involve going
1820 through the object's vtable), otherwise, return an expression
1821 for the dereferenced pointer-to-member construct. */
1822 addr = build_unary_op (ADDR_EXPR, base, 0);
1824 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
1826 if (addr == error_mark_node)
1828 cp_error ("object missing in `%E'", exp);
1829 return error_mark_node;
1832 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
1833 addr = convert_pointer_to (basetype, addr);
1834 member = cp_convert (ptrdiff_type_node, member);
1836 if (!flag_new_abi)
1837 /* Pointer to data members are offset by one, so that a null
1838 pointer with a real value of 0 is distinguishable from an
1839 offset of the first member of a structure. */
1840 member = build_binary_op (MINUS_EXPR, member,
1841 cp_convert (ptrdiff_type_node,
1842 integer_one_node));
1844 return build1 (INDIRECT_REF, type,
1845 build (PLUS_EXPR, build_pointer_type (type),
1846 addr, member));
1848 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
1850 return get_member_function_from_ptrfunc (&addr, member);
1852 my_friendly_abort (56);
1853 /* NOTREACHED */
1854 return NULL_TREE;
1857 /* Return either DECL or its known constant value (if it has one). */
1859 tree
1860 decl_constant_value (decl)
1861 tree decl;
1863 if (! TREE_THIS_VOLATILE (decl)
1864 && DECL_INITIAL (decl)
1865 && DECL_INITIAL (decl) != error_mark_node
1866 /* This is invalid if initial value is not constant.
1867 If it has either a function call, a memory reference,
1868 or a variable, then re-evaluating it could give different results. */
1869 && TREE_CONSTANT (DECL_INITIAL (decl))
1870 /* Check for cases where this is sub-optimal, even though valid. */
1871 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
1872 return DECL_INITIAL (decl);
1873 return decl;
1876 /* Common subroutines of build_new and build_vec_delete. */
1878 /* Call the global __builtin_delete to delete ADDR. */
1880 static tree
1881 build_builtin_delete_call (addr)
1882 tree addr;
1884 mark_used (global_delete_fndecl);
1885 return build_call (global_delete_fndecl, build_tree_list (NULL_TREE, addr));
1888 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
1889 (which needs to go through some sort of groktypename) or it
1890 is the name of the class we are newing. INIT is an initialization value.
1891 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
1892 If INIT is void_type_node, it means do *not* call a constructor
1893 for this instance.
1895 For types with constructors, the data returned is initialized
1896 by the appropriate constructor.
1898 Whether the type has a constructor or not, if it has a pointer
1899 to a virtual function table, then that pointer is set up
1900 here.
1902 Unless I am mistaken, a call to new () will return initialized
1903 data regardless of whether the constructor itself is private or
1904 not. NOPE; new fails if the constructor is private (jcm).
1906 Note that build_new does nothing to assure that any special
1907 alignment requirements of the type are met. Rather, it leaves
1908 it up to malloc to do the right thing. Otherwise, folding to
1909 the right alignment cal cause problems if the user tries to later
1910 free the memory returned by `new'.
1912 PLACEMENT is the `placement' list for user-defined operator new (). */
1914 extern int flag_check_new;
1916 tree
1917 build_new (placement, decl, init, use_global_new)
1918 tree placement;
1919 tree decl, init;
1920 int use_global_new;
1922 tree type, rval;
1923 tree nelts = NULL_TREE, t;
1924 int has_array = 0;
1926 if (decl == error_mark_node)
1927 return error_mark_node;
1929 if (TREE_CODE (decl) == TREE_LIST)
1931 tree absdcl = TREE_VALUE (decl);
1932 tree last_absdcl = NULL_TREE;
1934 if (current_function_decl
1935 && DECL_CONSTRUCTOR_P (current_function_decl))
1936 my_friendly_assert (immediate_size_expand == 0, 19990926);
1938 nelts = integer_one_node;
1940 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
1941 my_friendly_abort (215);
1942 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
1944 last_absdcl = absdcl;
1945 absdcl = TREE_OPERAND (absdcl, 0);
1948 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
1950 /* probably meant to be a vec new */
1951 tree this_nelts;
1953 while (TREE_OPERAND (absdcl, 0)
1954 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
1956 last_absdcl = absdcl;
1957 absdcl = TREE_OPERAND (absdcl, 0);
1960 has_array = 1;
1961 this_nelts = TREE_OPERAND (absdcl, 1);
1962 if (this_nelts != error_mark_node)
1964 if (this_nelts == NULL_TREE)
1965 error ("new of array type fails to specify size");
1966 else if (processing_template_decl)
1968 nelts = this_nelts;
1969 absdcl = TREE_OPERAND (absdcl, 0);
1971 else
1973 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
1974 if (build_expr_type_conversion (flags, this_nelts, 0)
1975 == NULL_TREE)
1976 pedwarn ("size in array new must have integral type");
1978 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
1979 absdcl = TREE_OPERAND (absdcl, 0);
1980 if (this_nelts == integer_zero_node)
1982 warning ("zero size array reserves no space");
1983 nelts = integer_zero_node;
1985 else
1986 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
1989 else
1990 nelts = integer_zero_node;
1993 if (last_absdcl)
1994 TREE_OPERAND (last_absdcl, 0) = absdcl;
1995 else
1996 TREE_VALUE (decl) = absdcl;
1998 type = groktypename (decl);
1999 if (! type || type == error_mark_node)
2000 return error_mark_node;
2002 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2004 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2006 /* An aggregate type. */
2007 type = IDENTIFIER_TYPE_VALUE (decl);
2008 decl = TYPE_MAIN_DECL (type);
2010 else
2012 /* A builtin type. */
2013 decl = lookup_name (decl, 1);
2014 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2015 type = TREE_TYPE (decl);
2018 else if (TREE_CODE (decl) == TYPE_DECL)
2020 type = TREE_TYPE (decl);
2022 else
2024 type = decl;
2025 decl = TYPE_MAIN_DECL (type);
2028 if (processing_template_decl)
2030 if (has_array)
2031 t = tree_cons (tree_cons (NULL_TREE, type, NULL_TREE),
2032 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2033 NULL_TREE);
2034 else
2035 t = type;
2037 rval = build_min_nt (NEW_EXPR, placement, t, init);
2038 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2039 return rval;
2042 /* ``A reference cannot be created by the new operator. A reference
2043 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2044 returned by new.'' ARM 5.3.3 */
2045 if (TREE_CODE (type) == REFERENCE_TYPE)
2047 error ("new cannot be applied to a reference type");
2048 type = TREE_TYPE (type);
2051 if (TREE_CODE (type) == FUNCTION_TYPE)
2053 error ("new cannot be applied to a function type");
2054 return error_mark_node;
2057 /* When the object being created is an array, the new-expression yields a
2058 pointer to the initial element (if any) of the array. For example,
2059 both new int and new int[10] return an int*. 5.3.4. */
2060 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2062 nelts = array_type_nelts_top (type);
2063 has_array = 1;
2064 type = TREE_TYPE (type);
2067 if (has_array)
2068 t = build_nt (ARRAY_REF, type, nelts);
2069 else
2070 t = type;
2072 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2073 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2074 TREE_SIDE_EFFECTS (rval) = 1;
2075 rval = build_new_1 (rval);
2076 if (rval == error_mark_node)
2077 return error_mark_node;
2079 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2080 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2081 TREE_NO_UNUSED_WARNING (rval) = 1;
2083 return rval;
2086 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2088 tree
2089 build_java_class_ref (type)
2090 tree type;
2092 tree name, class_decl;
2093 static tree CL_prefix = NULL_TREE;
2094 if (CL_prefix == NULL_TREE)
2095 CL_prefix = get_identifier("_CL_");
2096 if (jclass_node == NULL_TREE)
2098 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2099 if (jclass_node == NULL_TREE)
2100 fatal("call to Java constructor, while `jclass' undefined");
2101 jclass_node = TREE_TYPE (jclass_node);
2103 name = build_overload_with_type (CL_prefix, type);
2104 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2105 if (class_decl == NULL_TREE)
2107 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2108 TREE_STATIC (class_decl) = 1;
2109 DECL_EXTERNAL (class_decl) = 1;
2110 TREE_PUBLIC (class_decl) = 1;
2111 DECL_ARTIFICIAL (class_decl) = 1;
2112 DECL_IGNORED_P (class_decl) = 1;
2113 pushdecl_top_level (class_decl);
2114 make_decl_rtl (class_decl, NULL_PTR, 1);
2116 return class_decl;
2119 /* Returns teh size of the cookie to use when allocating an array
2120 whose elements have the indicated TYPE. Assumes that it is already
2121 known that a cookie is needed. */
2123 static tree
2124 get_cookie_size (type)
2125 tree type;
2127 tree cookie_size;
2129 if (flag_new_abi)
2131 /* Under the new ABI, we need to allocate an additional max
2132 (sizeof (size_t), alignof (true_type)) bytes. */
2133 tree sizetype_size;
2134 tree type_align;
2136 sizetype_size = size_in_bytes (sizetype);
2137 type_align = size_int (TYPE_ALIGN_UNIT (type));
2138 if (INT_CST_LT_UNSIGNED (type_align, sizetype_size))
2139 cookie_size = sizetype_size;
2140 else
2141 cookie_size = type_align;
2143 else
2144 cookie_size = BI_header_size;
2146 return cookie_size;
2149 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2150 value is immediately handed to expand_expr. */
2152 static tree
2153 build_new_1 (exp)
2154 tree exp;
2156 tree placement, init;
2157 tree type, true_type, size, rval;
2158 tree nelts = NULL_TREE;
2159 tree alloc_expr, alloc_node = NULL_TREE;
2160 int has_array = 0;
2161 enum tree_code code;
2162 int use_cookie, nothrow, check_new;
2163 /* Nonzero if the user wrote `::new' rather than just `new'. */
2164 int globally_qualified_p;
2165 /* Nonzero if we're going to call a global operator new, rather than
2166 a class-specific version. */
2167 int use_global_new;
2168 int use_java_new = 0;
2169 /* If non-NULL, the number of extra bytes to allocate at the
2170 beginning of the storage allocated for an array-new expression in
2171 order to store the number of elements. */
2172 tree cookie_size = NULL_TREE;
2174 placement = TREE_OPERAND (exp, 0);
2175 type = TREE_OPERAND (exp, 1);
2176 init = TREE_OPERAND (exp, 2);
2177 globally_qualified_p = NEW_EXPR_USE_GLOBAL (exp);
2179 if (TREE_CODE (type) == ARRAY_REF)
2181 has_array = 1;
2182 nelts = TREE_OPERAND (type, 1);
2183 type = TREE_OPERAND (type, 0);
2185 true_type = type;
2187 code = has_array ? VEC_NEW_EXPR : NEW_EXPR;
2189 if (CP_TYPE_QUALS (type))
2190 type = TYPE_MAIN_VARIANT (type);
2192 /* If our base type is an array, then make sure we know how many elements
2193 it has. */
2194 while (TREE_CODE (true_type) == ARRAY_TYPE)
2196 tree this_nelts = array_type_nelts_top (true_type);
2197 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2198 true_type = TREE_TYPE (true_type);
2201 if (!complete_type_or_else (true_type, exp))
2202 return error_mark_node;
2204 if (has_array)
2205 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2206 nelts));
2207 else
2208 size = size_in_bytes (type);
2210 if (TREE_CODE (true_type) == VOID_TYPE)
2212 error ("invalid type `void' for new");
2213 return error_mark_node;
2216 if (abstract_virtuals_error (NULL_TREE, true_type))
2217 return error_mark_node;
2219 /* Figure out whether or not we're going to use the global operator
2220 new. */
2221 if (!globally_qualified_p
2222 && IS_AGGR_TYPE (true_type)
2223 && ((!has_array && TYPE_HAS_NEW_OPERATOR (true_type))
2224 || (has_array && TYPE_HAS_ARRAY_NEW_OPERATOR (true_type))))
2225 use_global_new = 0;
2226 else
2227 use_global_new = 1;
2229 /* We only need cookies for arrays containing types for which we
2230 need cookies. */
2231 if (!has_array || !TYPE_VEC_NEW_USES_COOKIE (true_type))
2232 use_cookie = 0;
2233 /* When using placement new, users may not realize that they need
2234 the extra storage. Under the old ABI, we don't allocate the
2235 cookie whenever they use one placement argument of type `void
2236 *'. Under the new ABI, we require that the operator called be
2237 the global placement operator delete[]. */
2238 else if (placement && !TREE_CHAIN (placement)
2239 && same_type_p (TREE_TYPE (TREE_VALUE (placement)),
2240 ptr_type_node))
2241 use_cookie = (!flag_new_abi || !use_global_new);
2242 /* Otherwise, we need the cookie. */
2243 else
2244 use_cookie = 1;
2246 /* Compute the number of extra bytes to allocate, now that we know
2247 whether or not we need the cookie. */
2248 if (use_cookie)
2250 cookie_size = get_cookie_size (true_type);
2251 size = size_binop (PLUS_EXPR, size, cookie_size);
2254 if (has_array && init && pedantic)
2255 cp_pedwarn ("initialization in array new");
2257 /* Allocate the object. */
2259 if (! placement && TYPE_FOR_JAVA (true_type))
2261 tree class_addr, alloc_decl;
2262 tree class_decl = build_java_class_ref (true_type);
2263 tree class_size = size_in_bytes (true_type);
2264 static char alloc_name[] = "_Jv_AllocObject";
2265 use_java_new = 1;
2266 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2267 if (alloc_decl == NULL_TREE)
2268 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2269 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2270 rval = build_function_call (alloc_decl,
2271 tree_cons (NULL_TREE, class_addr,
2272 build_tree_list (NULL_TREE,
2273 class_size)));
2274 rval = cp_convert (build_pointer_type (true_type), rval);
2276 else
2278 tree fnname;
2279 tree args;
2281 args = tree_cons (NULL_TREE, size, placement);
2282 fnname = ansi_opname[code];
2284 if (use_global_new)
2285 rval = (build_new_function_call
2286 (lookup_function_nonclass (fnname, args),
2287 args));
2288 else
2289 rval = build_method_call (build_dummy_object (true_type),
2290 fnname, args, NULL_TREE,
2291 LOOKUP_NORMAL);
2292 rval = cp_convert (build_pointer_type (true_type), rval);
2295 /* unless an allocation function is declared with an empty excep-
2296 tion-specification (_except.spec_), throw(), it indicates failure to
2297 allocate storage by throwing a bad_alloc exception (clause _except_,
2298 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2299 cation function is declared with an empty exception-specification,
2300 throw(), it returns null to indicate failure to allocate storage and a
2301 non-null pointer otherwise.
2303 So check for a null exception spec on the op new we just called. */
2305 nothrow = 0;
2306 if (rval)
2308 /* The CALL_EXPR. */
2309 tree t = TREE_OPERAND (rval, 0);
2310 /* The function. */
2311 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2312 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2314 check_new = (flag_check_new || nothrow) && ! use_java_new;
2316 if ((check_new || flag_exceptions) && rval)
2318 alloc_expr = get_target_expr (rval);
2319 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2321 else
2322 alloc_expr = NULL_TREE;
2324 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2325 sure we have some extra bytes in that case for the BI_header_size
2326 cookies? And how does that interact with the code below? (mrs) */
2327 /* Finish up some magic for new'ed arrays */
2328 if (use_cookie && rval != NULL_TREE)
2330 tree cookie, exp1;
2331 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2332 rval = save_expr (build_binary_op (PLUS_EXPR, rval, cookie_size));
2333 /* Store the number of bytes allocated so that we can know how
2334 many elements to destroy later. */
2335 if (flag_new_abi)
2337 /* Under the new ABI, we use the last sizeof (size_t) bytes
2338 to store the number of elements. */
2339 cookie = build_indirect_ref (build (MINUS_EXPR,
2340 build_pointer_type (sizetype),
2341 rval,
2342 size_in_bytes (sizetype)),
2343 NULL_PTR);
2344 exp1 = build (MODIFY_EXPR, void_type_node, cookie, nelts);
2346 else
2348 cookie
2349 = build_indirect_ref (build (MINUS_EXPR,
2350 build_pointer_type (BI_header_type),
2351 rval, cookie_size), NULL_PTR);
2352 exp1 = build (MODIFY_EXPR, void_type_node,
2353 build_component_ref (cookie, nelts_identifier,
2354 NULL_TREE, 0),
2355 nelts);
2358 /* Build `(cookie = nelts, rval)' and use that as the complete
2359 expression. */
2360 rval = cp_convert (build_pointer_type (true_type), rval);
2361 rval = build_compound_expr
2362 (tree_cons (NULL_TREE, exp1,
2363 build_tree_list (NULL_TREE, rval)));
2366 if (rval == error_mark_node)
2367 return error_mark_node;
2369 /* Don't call any constructors or do any initialization. */
2370 if (init == void_type_node)
2371 goto done;
2373 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2375 if (! TYPE_NEEDS_CONSTRUCTING (type)
2376 && ! IS_AGGR_TYPE (type) && ! has_array)
2378 /* We are processing something like `new int (10)', which
2379 means allocate an int, and initialize it with 10. */
2380 tree deref;
2381 tree deref_type;
2383 /* At present RVAL is a temporary variable, created to hold
2384 the value from the call to `operator new'. We transform
2385 it to (*RVAL = INIT, RVAL). */
2386 rval = save_expr (rval);
2387 deref = build_indirect_ref (rval, NULL_PTR);
2389 /* Even for something like `new const int (10)' we must
2390 allow the expression to be non-const while we do the
2391 initialization. */
2392 deref_type = TREE_TYPE (deref);
2393 if (CP_TYPE_CONST_P (deref_type))
2394 TREE_TYPE (deref)
2395 = cp_build_qualified_type (deref_type,
2396 CP_TYPE_QUALS (deref_type)
2397 & ~TYPE_QUAL_CONST);
2398 TREE_READONLY (deref) = 0;
2400 if (TREE_CHAIN (init) != NULL_TREE)
2401 pedwarn ("initializer list being treated as compound expression");
2402 else if (TREE_CODE (init) == CONSTRUCTOR)
2404 pedwarn ("initializer list appears where operand should be used");
2405 init = TREE_OPERAND (init, 1);
2407 init = build_compound_expr (init);
2409 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2410 "new", NULL_TREE, 0);
2411 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2412 build_modify_expr (deref, NOP_EXPR, init),
2413 rval);
2414 TREE_NO_UNUSED_WARNING (rval) = 1;
2415 TREE_SIDE_EFFECTS (rval) = 1;
2417 else if (! has_array)
2419 tree newrval;
2420 /* Constructors are never virtual. If it has an initialization, we
2421 need to complain if we aren't allowed to use the ctor that took
2422 that argument. */
2423 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2425 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2427 init = tree_cons (NULL_TREE, integer_one_node, init);
2428 flags |= LOOKUP_HAS_IN_CHARGE;
2431 if (use_java_new)
2432 rval = save_expr (rval);
2433 newrval = rval;
2435 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2436 newrval = build_indirect_ref (newrval, NULL_PTR);
2438 newrval = build_method_call (newrval, ctor_identifier,
2439 init, TYPE_BINFO (true_type), flags);
2441 if (newrval == NULL_TREE || newrval == error_mark_node)
2442 return error_mark_node;
2444 /* Java constructors compiled by jc1 do not return this. */
2445 if (use_java_new)
2446 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2447 newrval, rval);
2448 rval = newrval;
2449 TREE_HAS_CONSTRUCTOR (rval) = 1;
2451 else
2452 rval = (build_vec_init
2453 (NULL_TREE,
2454 save_expr (rval),
2455 build_binary_op (MINUS_EXPR, nelts, integer_one_node),
2456 init,
2457 /*from_array=*/0));
2459 /* If any part of the object initialization terminates by throwing an
2460 exception and a suitable deallocation function can be found, the
2461 deallocation function is called to free the memory in which the
2462 object was being constructed, after which the exception continues
2463 to propagate in the context of the new-expression. If no
2464 unambiguous matching deallocation function can be found,
2465 propagating the exception does not cause the object's memory to be
2466 freed. */
2467 if (flag_exceptions && alloc_expr && ! use_java_new)
2469 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2470 tree cleanup, fn = NULL_TREE;
2471 int flags = (LOOKUP_NORMAL
2472 | (globally_qualified_p * LOOKUP_GLOBAL));
2474 /* The Standard is unclear here, but the right thing to do
2475 is to use the same method for finding deallocation
2476 functions that we use for finding allocation functions. */
2477 flags |= LOOKUP_SPECULATIVELY;
2479 /* We expect alloc_expr to look like a TARGET_EXPR around
2480 a NOP_EXPR around the CALL_EXPR we want. */
2481 fn = TREE_OPERAND (alloc_expr, 1);
2482 fn = TREE_OPERAND (fn, 0);
2484 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2486 /* Ack! First we allocate the memory. Then we set our sentry
2487 variable to true, and expand a cleanup that deletes the memory
2488 if sentry is true. Then we run the constructor and store the
2489 returned pointer in buf. Then we clear sentry and return buf. */
2491 if (cleanup)
2493 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2495 begin = get_target_expr (boolean_true_node);
2496 sentry = TREE_OPERAND (begin, 0);
2498 TREE_OPERAND (begin, 2)
2499 = build (COND_EXPR, void_type_node, sentry,
2500 cleanup, void_zero_node);
2502 rval = get_target_expr (rval);
2504 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2505 sentry, boolean_false_node);
2507 buf = TREE_OPERAND (rval, 0);
2509 rval = build (COMPOUND_EXPR, t, begin,
2510 build (COMPOUND_EXPR, t, rval,
2511 build (COMPOUND_EXPR, t, end, buf)));
2515 else if (CP_TYPE_CONST_P (true_type))
2516 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2518 done:
2520 if (alloc_expr && rval == alloc_node)
2522 rval = TREE_OPERAND (alloc_expr, 1);
2523 alloc_expr = NULL_TREE;
2526 if (check_new && alloc_expr)
2528 /* Did we modify the storage? */
2529 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2530 integer_zero_node);
2531 rval = build_conditional_expr (ifexp, rval, alloc_node);
2534 if (alloc_expr)
2535 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2537 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2539 /* The type of new int [3][3] is not int *, but int [3] * */
2540 rval = build_c_cast (build_pointer_type (type), rval);
2543 return rval;
2546 static tree
2547 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, use_global_delete)
2548 tree base, maxindex, type;
2549 tree auto_delete_vec;
2550 int use_global_delete;
2552 tree virtual_size;
2553 tree ptype = build_pointer_type (type = complete_type (type));
2554 tree size_exp = size_in_bytes (type);
2556 /* Temporary variables used by the loop. */
2557 tree tbase, tbase_init;
2559 /* This is the body of the loop that implements the deletion of a
2560 single element, and moves temp variables to next elements. */
2561 tree body;
2563 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2564 tree loop;
2566 /* This is the thing that governs what to do after the loop has run. */
2567 tree deallocate_expr = 0;
2569 /* This is the BIND_EXPR which holds the outermost iterator of the
2570 loop. It is convenient to set this variable up and test it before
2571 executing any other code in the loop.
2572 This is also the containing expression returned by this function. */
2573 tree controller = NULL_TREE;
2575 if (! IS_AGGR_TYPE (type) || TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
2577 loop = integer_zero_node;
2578 goto no_destructor;
2581 /* The below is short by BI_header_size */
2582 virtual_size = size_binop (MULT_EXPR, size_exp,
2583 convert (sizetype, maxindex));
2585 tbase = create_temporary_var (ptype);
2586 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2587 fold (build (PLUS_EXPR, ptype,
2588 base,
2589 virtual_size)));
2590 DECL_REGISTER (tbase) = 1;
2591 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2592 TREE_SIDE_EFFECTS (controller) = 1;
2594 body = NULL_TREE;
2596 body = tree_cons (NULL_TREE,
2597 build_delete (ptype, tbase, integer_two_node,
2598 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2599 body);
2601 body = tree_cons (NULL_TREE,
2602 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2603 body);
2605 body = tree_cons (NULL_TREE,
2606 build (EXIT_EXPR, void_type_node,
2607 build (EQ_EXPR, boolean_type_node, base, tbase)),
2608 body);
2610 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2612 loop = tree_cons (NULL_TREE, tbase_init,
2613 tree_cons (NULL_TREE, loop, NULL_TREE));
2614 loop = build_compound_expr (loop);
2616 no_destructor:
2617 /* If the delete flag is one, or anything else with the low bit set,
2618 delete the storage. */
2619 if (auto_delete_vec == integer_zero_node)
2620 deallocate_expr = integer_zero_node;
2621 else
2623 tree base_tbd;
2625 /* The below is short by BI_header_size */
2626 virtual_size = size_binop (MULT_EXPR, size_exp,
2627 convert (sizetype, maxindex));
2629 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2630 /* no header */
2631 base_tbd = base;
2632 else
2634 tree cookie_size;
2636 cookie_size = get_cookie_size (type);
2637 base_tbd
2638 = cp_convert (ptype,
2639 build_binary_op (MINUS_EXPR,
2640 cp_convert (string_type_node, base),
2641 cookie_size));
2642 /* True size with header. */
2643 virtual_size = size_binop (PLUS_EXPR, virtual_size, cookie_size);
2645 deallocate_expr = build_x_delete (base_tbd,
2646 2 | use_global_delete,
2647 virtual_size);
2648 deallocate_expr = fold (build (COND_EXPR, void_type_node,
2649 fold (build (BIT_AND_EXPR,
2650 integer_type_node,
2651 auto_delete_vec,
2652 integer_one_node)),
2653 deallocate_expr, integer_zero_node));
2656 if (loop && deallocate_expr != integer_zero_node)
2658 body = tree_cons (NULL_TREE, loop,
2659 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2660 body = build_compound_expr (body);
2662 else
2663 body = loop;
2665 /* Outermost wrapper: If pointer is null, punt. */
2666 body = fold (build (COND_EXPR, void_type_node,
2667 fold (build (NE_EXPR, boolean_type_node, base,
2668 integer_zero_node)),
2669 body, integer_zero_node));
2670 body = build1 (NOP_EXPR, void_type_node, body);
2672 if (controller)
2674 TREE_OPERAND (controller, 1) = body;
2675 return controller;
2677 else
2678 return cp_convert (void_type_node, body);
2681 tree
2682 create_temporary_var (type)
2683 tree type;
2685 tree decl;
2687 decl = build_decl (VAR_DECL, NULL_TREE, type);
2688 TREE_USED (decl) = 1;
2689 DECL_ARTIFICIAL (decl) = 1;
2690 DECL_SOURCE_FILE (decl) = input_filename;
2691 DECL_SOURCE_LINE (decl) = lineno;
2692 DECL_IGNORED_P (decl) = 1;
2693 DECL_CONTEXT (decl) = current_function_decl;
2695 return decl;
2698 /* Create a new temporary variable of the indicated TYPE, initialized
2699 to INIT.
2701 It is not entered into current_binding_level, because that breaks
2702 things when it comes time to do final cleanups (which take place
2703 "outside" the binding contour of the function). */
2705 static tree
2706 get_temp_regvar (type, init)
2707 tree type, init;
2709 tree decl;
2711 decl = create_temporary_var (type);
2712 if (building_stmt_tree ())
2713 add_decl_stmt (decl);
2714 if (!building_stmt_tree ())
2715 DECL_RTL (decl) = assign_temp (type, 2, 0, 1);
2716 finish_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
2718 return decl;
2721 /* `build_vec_init' returns tree structure that performs
2722 initialization of a vector of aggregate types.
2724 DECL is passed only for error reporting, and provides line number
2725 and source file name information.
2726 BASE is the space where the vector will be. For a vector of Ts,
2727 the type of BASE is `T*'.
2728 MAXINDEX is the maximum index of the array (one less than the
2729 number of elements).
2730 INIT is the (possibly NULL) initializer.
2732 FROM_ARRAY is 0 if we should init everything with INIT
2733 (i.e., every element initialized from INIT).
2734 FROM_ARRAY is 1 if we should index into INIT in parallel
2735 with initialization of DECL.
2736 FROM_ARRAY is 2 if we should index into INIT in parallel,
2737 but use assignment instead of initialization. */
2739 tree
2740 build_vec_init (decl, base, maxindex, init, from_array)
2741 tree decl, base, maxindex, init;
2742 int from_array;
2744 tree rval;
2745 tree base2 = NULL_TREE;
2746 tree size;
2747 tree itype = NULL_TREE;
2748 tree iterator;
2749 /* The type of an element in the array. */
2750 tree type;
2751 /* The type of a pointer to an element in the array. */
2752 tree ptype;
2753 tree stmt_expr;
2754 tree compound_stmt;
2755 int destroy_temps;
2756 tree try_block = NULL_TREE;
2757 tree try_body = NULL_TREE;
2758 int num_initialized_elts = 0;
2760 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2761 if (maxindex == error_mark_node)
2762 return error_mark_node;
2764 type = TREE_TYPE (TREE_TYPE (base));
2765 ptype = build_pointer_type (type);
2766 size = size_in_bytes (type);
2768 /* The code we are generating looks like:
2770 T* t1 = (T*) base;
2771 T* rval = base;
2772 ptrdiff_t iterator = maxindex;
2773 try {
2774 ... initializations from CONSTRUCTOR ...
2775 if (iterator != -1) {
2776 do {
2777 ... initialize *base ...
2778 ++base;
2779 } while (--iterator != -1);
2781 } catch (...) {
2782 ... destroy elements that were constructed ...
2785 We can omit the try and catch blocks if we know that the
2786 initialization will never throw an exception, or if the array
2787 elements do not have destructors. If we have a CONSTRUCTOR to
2788 give us initialization information, we emit code to initialize
2789 each of the elements before the loop in the try block, and then
2790 iterate over fewer elements. We can omit the loop completely if
2791 the elements of the array do not have constructors.
2793 We actually wrap the entire body of the above in a STMT_EXPR, for
2794 tidiness.
2796 When copying from array to another, when the array elements have
2797 only trivial copy constructors, we should use __builtin_memcpy
2798 rather than generating a loop. That way, we could take advantage
2799 of whatever cleverness the back-end has for dealing with copies
2800 of blocks of memory. */
2802 begin_init_stmts (&stmt_expr, &compound_stmt);
2803 destroy_temps = stmts_are_full_exprs_p;
2804 stmts_are_full_exprs_p = 0;
2805 rval = get_temp_regvar (ptype,
2806 cp_convert (ptype, default_conversion (base)));
2807 base = get_temp_regvar (ptype, rval);
2808 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2810 /* Protect the entire array initialization so that we can destroy
2811 the partially constructed array if an exception is thrown. */
2812 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
2814 try_block = begin_try_block ();
2815 try_body = begin_compound_stmt (/*has_no_scope=*/1);
2818 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2819 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2821 /* Do non-default initialization resulting from brace-enclosed
2822 initializers. */
2824 tree elts;
2825 from_array = 0;
2827 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
2829 tree elt = TREE_VALUE (elts);
2830 tree baseref = build1 (INDIRECT_REF, type, base);
2832 num_initialized_elts++;
2834 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
2835 finish_expr_stmt (build_aggr_init (baseref, elt, 0));
2836 else
2837 finish_expr_stmt (build_modify_expr (baseref, NOP_EXPR,
2838 elt));
2840 finish_expr_stmt (build_modify_expr
2841 (base,
2842 NOP_EXPR,
2843 build (PLUS_EXPR, build_pointer_type (type),
2844 base, size)));
2845 finish_expr_stmt (build_modify_expr
2846 (iterator,
2847 NOP_EXPR,
2848 build (MINUS_EXPR, ptrdiff_type_node,
2849 iterator, integer_one_node)));
2852 /* Clear out INIT so that we don't get confused below. */
2853 init = NULL_TREE;
2855 else if (from_array)
2857 /* If initializing one array from another, initialize element by
2858 element. We rely upon the below calls the do argument
2859 checking. */
2860 if (decl == NULL_TREE)
2862 sorry ("initialization of array from dissimilar array type");
2863 return error_mark_node;
2865 if (init)
2867 base2 = default_conversion (init);
2868 itype = TREE_TYPE (base2);
2869 base2 = get_temp_regvar (itype, base2);
2870 itype = TREE_TYPE (itype);
2872 else if (TYPE_LANG_SPECIFIC (type)
2873 && TYPE_NEEDS_CONSTRUCTING (type)
2874 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
2876 error ("initializer ends prematurely");
2877 return error_mark_node;
2881 /* Now, default-initialize any remaining elements. We don't need to
2882 do that if a) the type does not need constructing, or b) we've
2883 already initialized all the elements.
2885 We do need to keep going if we're copying an array. */
2887 if (from_array
2888 || (TYPE_NEEDS_CONSTRUCTING (type)
2889 && ! (host_integerp (maxindex, 0)
2890 && (num_initialized_elts
2891 == tree_low_cst (maxindex, 0) + 1))))
2893 /* If the ITERATOR is equal to -1, then we don't have to loop;
2894 we've already initialized all the elements. */
2895 tree if_stmt;
2896 tree do_stmt;
2897 tree do_body;
2898 tree elt_init;
2900 if_stmt = begin_if_stmt ();
2901 finish_if_stmt_cond (build (NE_EXPR, boolean_type_node,
2902 iterator, minus_one_node),
2903 if_stmt);
2905 /* Otherwise, loop through the elements. */
2906 do_stmt = begin_do_stmt ();
2907 do_body = begin_compound_stmt (/*has_no_scope=*/1);
2909 /* When we're not building a statement-tree, things are a little
2910 complicated. If, when we recursively call build_aggr_init,
2911 an expression containing a TARGET_EXPR is expanded, then it
2912 may get a cleanup. Then, the result of that expression is
2913 passed to finish_expr_stmt, which will call
2914 expand_start_target_temps/expand_end_target_temps. However,
2915 the latter call will not cause the cleanup to run because
2916 that block will still be on the block stack. So, we call
2917 expand_start_target_temps here manually; the corresponding
2918 call to expand_end_target_temps below will cause the cleanup
2919 to be performed. */
2920 if (!building_stmt_tree ())
2921 expand_start_target_temps ();
2923 if (from_array)
2925 tree to = build1 (INDIRECT_REF, type, base);
2926 tree from;
2928 if (base2)
2929 from = build1 (INDIRECT_REF, itype, base2);
2930 else
2931 from = NULL_TREE;
2933 if (from_array == 2)
2934 elt_init = build_modify_expr (to, NOP_EXPR, from);
2935 else if (TYPE_NEEDS_CONSTRUCTING (type))
2936 elt_init = build_aggr_init (to, from, 0);
2937 else if (from)
2938 elt_init = build_modify_expr (to, NOP_EXPR, from);
2939 else
2940 my_friendly_abort (57);
2942 else if (TREE_CODE (type) == ARRAY_TYPE)
2944 if (init != 0)
2945 sorry ("cannot initialize multi-dimensional array with initializer");
2946 elt_init = (build_vec_init
2947 (decl,
2948 build1 (NOP_EXPR,
2949 build_pointer_type (TREE_TYPE (type)),
2950 base),
2951 array_type_nelts (type), 0, 0));
2953 else
2954 elt_init = build_aggr_init (build1 (INDIRECT_REF, type, base),
2955 init, 0);
2957 /* The initialization of each array element is a
2958 full-expression. */
2959 if (!building_stmt_tree ())
2961 finish_expr_stmt (elt_init);
2962 expand_end_target_temps ();
2964 else
2966 stmts_are_full_exprs_p = 1;
2967 finish_expr_stmt (elt_init);
2968 stmts_are_full_exprs_p = 0;
2971 finish_expr_stmt (build_modify_expr
2972 (base,
2973 NOP_EXPR,
2974 build (PLUS_EXPR, build_pointer_type (type),
2975 base, size)));
2976 if (base2)
2977 finish_expr_stmt (build_modify_expr
2978 (base2,
2979 NOP_EXPR,
2980 build (PLUS_EXPR, build_pointer_type (type),
2981 base2, size)));
2983 finish_compound_stmt (/*has_no_scope=*/1, do_body);
2984 finish_do_body (do_stmt);
2985 finish_do_stmt (build (NE_EXPR, boolean_type_node,
2986 build (PREDECREMENT_EXPR,
2987 ptrdiff_type_node,
2988 iterator,
2989 integer_one_node),
2990 minus_one_node),
2991 do_stmt);
2993 finish_then_clause (if_stmt);
2994 finish_if_stmt ();
2997 /* Make sure to cleanup any partially constructed elements. */
2998 if (flag_exceptions && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3000 tree e;
3002 finish_compound_stmt (/*has_no_scope=*/1, try_body);
3003 finish_cleanup_try_block (try_block);
3004 e = build_vec_delete_1 (rval,
3005 build_binary_op (MINUS_EXPR, maxindex,
3006 iterator),
3007 type,
3008 /*auto_delete_vec=*/integer_zero_node,
3009 /*use_global_delete=*/0);
3010 finish_cleanup (e, try_block);
3013 /* The value of the array initialization is the address of the
3014 first element in the array. */
3015 finish_expr_stmt (rval);
3017 stmt_expr = finish_init_stmts (stmt_expr, compound_stmt);
3018 stmts_are_full_exprs_p = destroy_temps;
3019 return stmt_expr;
3022 /* Free up storage of type TYPE, at address ADDR.
3024 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3025 of pointer.
3027 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3028 used as the second argument to operator delete. It can include
3029 things like padding and magic size cookies. It has virtual in it,
3030 because if you have a base pointer and you delete through a virtual
3031 destructor, it should be the size of the dynamic object, not the
3032 static object, see Free Store 12.5 ISO C++.
3034 This does not call any destructors. */
3036 tree
3037 build_x_delete (addr, which_delete, virtual_size)
3038 tree addr;
3039 int which_delete;
3040 tree virtual_size;
3042 int use_global_delete = which_delete & 1;
3043 int use_vec_delete = !!(which_delete & 2);
3044 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3045 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
3047 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
3050 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3051 ADDR is an expression which yields the store to be destroyed.
3052 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3053 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3054 virtual baseclasses.
3055 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3057 FLAGS is the logical disjunction of zero or more LOOKUP_
3058 flags. See cp-tree.h for more info.
3060 This function does not delete an object's virtual base classes. */
3062 tree
3063 build_delete (type, addr, auto_delete, flags, use_global_delete)
3064 tree type, addr;
3065 tree auto_delete;
3066 int flags;
3067 int use_global_delete;
3069 tree member;
3070 tree expr;
3071 tree ref;
3073 if (addr == error_mark_node)
3074 return error_mark_node;
3076 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3077 set to `error_mark_node' before it gets properly cleaned up. */
3078 if (type == error_mark_node)
3079 return error_mark_node;
3081 type = TYPE_MAIN_VARIANT (type);
3083 if (TREE_CODE (type) == POINTER_TYPE)
3085 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3086 if (type != void_type_node && !complete_type_or_else (type, addr))
3087 return error_mark_node;
3088 if (TREE_CODE (type) == ARRAY_TYPE)
3089 goto handle_array;
3090 if (! IS_AGGR_TYPE (type))
3092 /* Call the builtin operator delete. */
3093 return build_builtin_delete_call (addr);
3095 if (TREE_SIDE_EFFECTS (addr))
3096 addr = save_expr (addr);
3098 /* throw away const and volatile on target type of addr */
3099 addr = convert_force (build_pointer_type (type), addr, 0);
3100 ref = build_indirect_ref (addr, NULL_PTR);
3102 else if (TREE_CODE (type) == ARRAY_TYPE)
3104 handle_array:
3105 if (TREE_SIDE_EFFECTS (addr))
3106 addr = save_expr (addr);
3107 if (TYPE_DOMAIN (type) == NULL_TREE)
3109 error ("unknown array size in delete");
3110 return error_mark_node;
3112 return build_vec_delete (addr, array_type_nelts (type),
3113 auto_delete, use_global_delete);
3115 else
3117 /* Don't check PROTECT here; leave that decision to the
3118 destructor. If the destructor is accessible, call it,
3119 else report error. */
3120 addr = build_unary_op (ADDR_EXPR, addr, 0);
3121 if (TREE_SIDE_EFFECTS (addr))
3122 addr = save_expr (addr);
3124 if (TREE_CONSTANT (addr))
3125 addr = convert_pointer_to (type, addr);
3126 else
3127 addr = convert_force (build_pointer_type (type), addr, 0);
3129 ref = build_indirect_ref (addr, NULL_PTR);
3132 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3134 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (type))
3136 if (auto_delete == integer_zero_node)
3137 return void_zero_node;
3139 return build_op_delete_call
3140 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3141 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3142 NULL_TREE);
3145 /* Below, we will reverse the order in which these calls are made.
3146 If we have a destructor, then that destructor will take care
3147 of the base classes; otherwise, we must do that here. */
3148 if (TYPE_HAS_DESTRUCTOR (type))
3150 tree passed_auto_delete;
3151 tree do_delete = NULL_TREE;
3152 tree ifexp;
3154 if (use_global_delete)
3156 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3157 auto_delete, integer_one_node));
3158 tree call = build_builtin_delete_call (addr);
3160 cond = fold (build (COND_EXPR, void_type_node, cond,
3161 call, void_zero_node));
3162 if (cond != void_zero_node)
3163 do_delete = cond;
3165 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3166 auto_delete, integer_two_node));
3168 else
3169 passed_auto_delete = auto_delete;
3171 expr = build_method_call
3172 (ref, dtor_identifier, build_tree_list (NULL_TREE, passed_auto_delete),
3173 NULL_TREE, flags);
3175 if (do_delete)
3176 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3178 if (flags & LOOKUP_DESTRUCTOR)
3179 /* Explicit destructor call; don't check for null pointer. */
3180 ifexp = integer_one_node;
3181 else
3182 /* Handle deleting a null pointer. */
3183 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3185 if (ifexp != integer_one_node)
3186 expr = build (COND_EXPR, void_type_node,
3187 ifexp, expr, void_zero_node);
3189 return expr;
3191 else
3193 /* We only get here from finish_function for a destructor. */
3194 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3195 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3196 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3197 tree exprstmt = NULL_TREE;
3198 tree parent_auto_delete = auto_delete;
3199 tree cond;
3201 /* Set this again before we call anything, as we might get called
3202 recursively. */
3203 TYPE_HAS_DESTRUCTOR (type) = 1;
3205 /* If we have member delete or vbases, we call delete in
3206 finish_function. */
3207 if (auto_delete == integer_zero_node)
3208 cond = NULL_TREE;
3209 else if (base_binfo == NULL_TREE
3210 || TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3212 cond = build (COND_EXPR, void_type_node,
3213 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3214 build_builtin_delete_call (addr),
3215 void_zero_node);
3217 else
3218 cond = NULL_TREE;
3220 if (cond)
3221 exprstmt = build_tree_list (NULL_TREE, cond);
3223 if (base_binfo
3224 && ! TREE_VIA_VIRTUAL (base_binfo)
3225 && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3227 tree this_auto_delete;
3229 if (BINFO_OFFSET_ZEROP (base_binfo))
3230 this_auto_delete = parent_auto_delete;
3231 else
3232 this_auto_delete = integer_zero_node;
3234 expr = build_scoped_method_call
3235 (ref, base_binfo, dtor_identifier,
3236 build_tree_list (NULL_TREE, this_auto_delete));
3237 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3240 /* Take care of the remaining baseclasses. */
3241 for (i = 1; i < n_baseclasses; i++)
3243 base_binfo = TREE_VEC_ELT (binfos, i);
3244 if (TYPE_HAS_TRIVIAL_DESTRUCTOR (BINFO_TYPE (base_binfo))
3245 || TREE_VIA_VIRTUAL (base_binfo))
3246 continue;
3248 expr = build_scoped_method_call
3249 (ref, base_binfo, dtor_identifier,
3250 build_tree_list (NULL_TREE, integer_zero_node));
3252 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3255 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3257 if (TREE_CODE (member) != FIELD_DECL)
3258 continue;
3259 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (TREE_TYPE (member)))
3261 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3262 tree this_type = TREE_TYPE (member);
3263 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3264 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3268 if (exprstmt)
3269 return build_compound_expr (exprstmt);
3270 /* Virtual base classes make this function do nothing. */
3271 return void_zero_node;
3275 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3277 tree
3278 build_vbase_delete (type, decl)
3279 tree type, decl;
3281 tree vbases = CLASSTYPE_VBASECLASSES (type);
3282 tree result = NULL_TREE;
3283 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3285 my_friendly_assert (addr != error_mark_node, 222);
3287 while (vbases)
3289 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3290 addr, 0);
3291 result = tree_cons (NULL_TREE,
3292 build_delete (TREE_TYPE (this_addr), this_addr,
3293 integer_zero_node,
3294 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3295 result);
3296 vbases = TREE_CHAIN (vbases);
3298 return build_compound_expr (nreverse (result));
3301 /* Build a C++ vector delete expression.
3302 MAXINDEX is the number of elements to be deleted.
3303 ELT_SIZE is the nominal size of each element in the vector.
3304 BASE is the expression that should yield the store to be deleted.
3305 This function expands (or synthesizes) these calls itself.
3306 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3308 This also calls delete for virtual baseclasses of elements of the vector.
3310 Update: MAXINDEX is no longer needed. The size can be extracted from the
3311 start of the vector for pointers, and from the type for arrays. We still
3312 use MAXINDEX for arrays because it happens to already have one of the
3313 values we'd have to extract. (We could use MAXINDEX with pointers to
3314 confirm the size, and trap if the numbers differ; not clear that it'd
3315 be worth bothering.) */
3317 tree
3318 build_vec_delete (base, maxindex, auto_delete_vec, use_global_delete)
3319 tree base, maxindex;
3320 tree auto_delete_vec;
3321 int use_global_delete;
3323 tree type;
3325 if (TREE_CODE (base) == OFFSET_REF)
3326 base = resolve_offset_ref (base);
3328 type = TREE_TYPE (base);
3330 base = stabilize_reference (base);
3332 /* Since we can use base many times, save_expr it. */
3333 if (TREE_SIDE_EFFECTS (base))
3334 base = save_expr (base);
3336 if (TREE_CODE (type) == POINTER_TYPE)
3338 /* Step back one from start of vector, and read dimension. */
3339 tree cookie_addr;
3341 if (flag_new_abi)
3343 cookie_addr = build (MINUS_EXPR,
3344 build_pointer_type (sizetype),
3345 base,
3346 TYPE_SIZE_UNIT (sizetype));
3347 maxindex = build_indirect_ref (cookie_addr, NULL_PTR);
3349 else
3351 tree cookie;
3353 cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3354 base, BI_header_size);
3355 cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3356 maxindex = build_component_ref (cookie, nelts_identifier,
3357 NULL_TREE, 0);
3360 type = strip_array_types (TREE_TYPE (type));
3362 else if (TREE_CODE (type) == ARRAY_TYPE)
3364 /* get the total number of things in the array, maxindex is a bad name */
3365 maxindex = array_type_nelts_total (type);
3366 type = strip_array_types (type);
3367 base = build_unary_op (ADDR_EXPR, base, 1);
3369 else
3371 if (base != error_mark_node)
3372 error ("type to vector delete is neither pointer or array type");
3373 return error_mark_node;
3376 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec,
3377 use_global_delete);