1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 89, 92-98, 1999 Free Software Foundation, Inc.
3 Contributed by Michael Tiemann (tiemann@cygnus.com)
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* High-level class interface. */
35 /* In C++, structures with well-defined constructors are initialized by
36 those constructors, unasked. CURRENT_BASE_INIT_LIST
37 holds a list of stmts for a BASE_INIT term in the grammar.
38 This list has one element for each base class which must be
39 initialized. The list elements are [basename, init], with
40 type basetype. This allows the possibly anachronistic form
41 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
42 where each successive term can be handed down the constructor
43 line. Perhaps this was not intended. */
44 tree current_base_init_list, current_member_init_list;
46 static void expand_aggr_vbase_init_1 PROTO((tree, tree, tree, tree));
47 static void construct_virtual_bases PROTO((tree, tree, tree, tree, tree));
48 static void expand_aggr_init_1 PROTO((tree, tree, tree, tree, int));
49 static void expand_default_init PROTO((tree, tree, tree, tree, int));
50 static tree build_vec_delete_1 PROTO((tree, tree, tree, tree, tree,
52 static void perform_member_init PROTO((tree, tree, tree, int));
53 static void sort_base_init PROTO((tree, tree *, tree *));
54 static tree build_builtin_delete_call PROTO((tree));
55 static int member_init_ok_or_else PROTO((tree, tree, const char *));
56 static void expand_virtual_init PROTO((tree, tree));
57 static tree sort_member_init PROTO((tree));
58 static tree initializing_context PROTO((tree));
59 static void expand_vec_init_try_block PROTO((tree));
60 static void expand_vec_init_catch_clause PROTO((tree, tree, tree, tree));
61 static tree build_java_class_ref PROTO((tree));
62 static void expand_cleanup_for_base PROTO((tree, tree, tree));
63 static int pvbasecount PROTO((tree, int));
65 /* Cache the identifier nodes for the magic field of a new cookie. */
66 static tree nc_nelts_field_id;
68 static tree minus_one;
70 /* Set up local variable for this file. MUST BE CALLED AFTER
71 INIT_DECL_PROCESSING. */
73 static tree BI_header_type, BI_header_size;
75 void init_init_processing ()
79 minus_one = build_int_2 (-1, -1);
81 /* Define the structure that holds header information for
82 arrays allocated via operator new. */
83 BI_header_type = make_lang_type (RECORD_TYPE);
84 nc_nelts_field_id = get_identifier ("nelts");
85 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
86 finish_builtin_type (BI_header_type, "__new_cookie", fields,
88 BI_header_size = size_in_bytes (BI_header_type);
91 /* Subroutine of emit_base_init. For BINFO, initialize all the
92 virtual function table pointers, except those that come from
93 virtual base classes. Initialize binfo's vtable pointer, if
94 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
95 function table pointers in all bases have been initialized already,
96 probably because their constructors have just be run. ADDR is the
97 pointer to the object whos vtables we are going to initialize.
99 REAL_BINFO is usually the same as BINFO, except when addr is not of
100 pointer to the type of the real derived type that we want to
101 initialize for. This is the case when addr is a pointer to a sub
102 object of a complete object, and we only want to do part of the
103 complete object's initialization of vtable pointers. This is done
104 for all virtual table pointers in virtual base classes. REAL_BINFO
105 is used to find the BINFO_VTABLE that we initialize with. BINFO is
106 used for conversions of addr to subobjects.
108 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
110 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
114 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
115 tree real_binfo, binfo, addr;
116 int init_self, can_elide;
118 tree real_binfos = BINFO_BASETYPES (real_binfo);
119 tree binfos = BINFO_BASETYPES (binfo);
120 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
122 for (i = 0; i < n_baselinks; i++)
124 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
125 tree base_binfo = TREE_VEC_ELT (binfos, i);
126 int is_not_base_vtable
127 = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
128 if (! TREE_VIA_VIRTUAL (real_base_binfo))
129 expand_direct_vtbls_init (real_base_binfo, base_binfo,
130 is_not_base_vtable, can_elide, addr);
133 /* Before turning this on, make sure it is correct. */
134 if (can_elide && ! BINFO_MODIFIED (binfo))
137 /* Should we use something besides CLASSTYPE_VFIELDS? */
138 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
140 tree base_ptr = convert_pointer_to_real (binfo, addr);
141 expand_virtual_init (real_binfo, base_ptr);
146 /* Subroutine of emit_base_init. */
149 perform_member_init (member, name, init, explicit)
150 tree member, name, init;
154 tree type = TREE_TYPE (member);
156 expand_start_target_temps ();
158 if (TYPE_NEEDS_CONSTRUCTING (type)
159 || (init && TYPE_HAS_CONSTRUCTOR (type)))
161 /* Since `init' is already a TREE_LIST on the current_member_init_list,
162 only build it into one if we aren't already a list. */
163 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
164 init = build_expr_list (NULL_TREE, init);
166 decl = build_component_ref (current_class_ref, name, NULL_TREE, explicit);
169 && TREE_CODE (type) == ARRAY_TYPE
171 && TREE_CHAIN (init) == NULL_TREE
172 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
174 /* Initialization of one array from another. */
175 expand_vec_init (TREE_OPERAND (decl, 1), decl,
176 array_type_nelts (type), TREE_VALUE (init), 1);
179 expand_aggr_init (decl, init, 0);
183 if (init == NULL_TREE)
187 /* default-initialization. */
188 if (AGGREGATE_TYPE_P (type))
189 init = build (CONSTRUCTOR, type, NULL_TREE, NULL_TREE);
190 else if (TREE_CODE (type) == REFERENCE_TYPE)
192 cp_error ("default-initialization of `%#D', which has reference type",
194 init = error_mark_node;
197 init = integer_zero_node;
199 /* member traversal: note it leaves init NULL */
200 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
201 cp_pedwarn ("uninitialized reference member `%D'", member);
203 else if (TREE_CODE (init) == TREE_LIST)
205 /* There was an explicit member initialization. Do some
206 work in that case. */
207 if (TREE_CHAIN (init))
209 warning ("initializer list treated as compound expression");
210 init = build_compound_expr (init);
213 init = TREE_VALUE (init);
216 /* We only build this with a null init if we got it from the
217 current_member_init_list. */
218 if (init || explicit)
220 decl = build_component_ref (current_class_ref, name, NULL_TREE,
222 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
226 expand_end_target_temps ();
229 if (TYPE_NEEDS_DESTRUCTOR (type))
233 /* All cleanups must be on the function_obstack. */
234 push_obstacks_nochange ();
235 resume_temporary_allocation ();
237 expr = build_component_ref (current_class_ref, name, NULL_TREE,
239 expr = build_delete (type, expr, integer_zero_node,
240 LOOKUP_NONVIRTUAL|LOOKUP_DESTRUCTOR, 0);
242 if (expr != error_mark_node)
243 add_partial_entry (expr);
249 extern int warn_reorder;
251 /* Subroutine of emit_member_init. */
257 tree x, member, name, field;
258 tree init_list = NULL_TREE;
260 tree last_field = NULL_TREE;
262 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
266 /* member could be, for example, a CONST_DECL for an enumerated
267 tag; we don't want to try to initialize that, since it already
269 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
272 for (x = current_member_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
274 /* If we cleared this out, then pay no attention to it. */
275 if (TREE_PURPOSE (x) == NULL_TREE)
277 name = TREE_PURPOSE (x);
280 /* This happens in templates, since the IDENTIFIER is replaced
281 with the COMPONENT_REF in tsubst_expr. */
282 field = (TREE_CODE (name) == COMPONENT_REF
283 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
285 /* Let's find out when this happens. */
286 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
287 field = IDENTIFIER_CLASS_VALUE (name);
290 /* If one member shadows another, get the outermost one. */
291 if (TREE_CODE (field) == TREE_LIST)
292 field = TREE_VALUE (field);
300 cp_warning_at ("member initializers for `%#D'", last_field);
301 cp_warning_at (" and `%#D'", field);
302 warning (" will be re-ordered to match declaration order");
308 /* Make sure we won't try to work on this init again. */
309 TREE_PURPOSE (x) = NULL_TREE;
310 x = build_tree_list (name, TREE_VALUE (x));
315 /* If we didn't find MEMBER in the list, create a dummy entry
316 so the two lists (INIT_LIST and the list of members) will be
318 x = build_tree_list (NULL_TREE, NULL_TREE);
320 init_list = chainon (init_list, x);
323 /* Initializers for base members go at the end. */
324 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
326 name = TREE_PURPOSE (x);
329 if (purpose_member (name, init_list))
331 cp_error ("multiple initializations given for member `%D'",
332 IDENTIFIER_CLASS_VALUE (name));
336 init_list = chainon (init_list,
337 build_tree_list (name, TREE_VALUE (x)));
338 TREE_PURPOSE (x) = NULL_TREE;
346 sort_base_init (t, rbase_ptr, vbase_ptr)
347 tree t, *rbase_ptr, *vbase_ptr;
349 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
350 int n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
356 /* For warn_reorder. */
358 tree last_base = NULL_TREE;
360 tree rbases = NULL_TREE;
361 tree vbases = NULL_TREE;
363 /* First walk through and splice out vbase and invalid initializers.
364 Also replace names with binfos. */
366 last = tree_cons (NULL_TREE, NULL_TREE, current_base_init_list);
367 for (x = TREE_CHAIN (last); x; x = TREE_CHAIN (x))
369 tree basetype = TREE_PURPOSE (x);
370 tree binfo = NULL_TREE;
372 if (basetype == NULL_TREE)
374 /* Initializer for single base class. Must not
375 use multiple inheritance or this is ambiguous. */
376 switch (n_baseclasses)
379 cp_error ("`%T' does not have a base class to initialize",
385 cp_error ("unnamed initializer ambiguous for `%T' which uses multiple inheritance",
389 binfo = TREE_VEC_ELT (binfos, 0);
391 else if (is_aggr_type (basetype, 1))
393 binfo = binfo_or_else (basetype, t);
394 if (binfo == NULL_TREE)
397 /* Virtual base classes are special cases. Their initializers
398 are recorded with this constructor, and they are used when
399 this constructor is the top-level constructor called. */
400 if (TREE_VIA_VIRTUAL (binfo))
402 tree v = CLASSTYPE_VBASECLASSES (t);
403 while (BINFO_TYPE (v) != BINFO_TYPE (binfo))
406 vbases = tree_cons (v, TREE_VALUE (x), vbases);
411 /* Otherwise, if it is not an immediate base class, complain. */
412 for (i = n_baseclasses-1; i >= 0; i--)
413 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
417 cp_error ("`%T' is not an immediate base class of `%T'",
418 basetype, current_class_type);
424 my_friendly_abort (365);
426 TREE_PURPOSE (x) = binfo;
427 TREE_CHAIN (last) = x;
430 TREE_CHAIN (last) = NULL_TREE;
432 /* Now walk through our regular bases and make sure they're initialized. */
434 for (i = 0; i < n_baseclasses; ++i)
436 tree base_binfo = TREE_VEC_ELT (binfos, i);
439 if (TREE_VIA_VIRTUAL (base_binfo))
442 for (x = current_base_init_list, pos = 0; x; x = TREE_CHAIN (x), ++pos)
444 tree binfo = TREE_PURPOSE (x);
446 if (binfo == NULL_TREE)
449 if (binfo == base_binfo)
455 cp_warning_at ("base initializers for `%#T'", last_base);
456 cp_warning_at (" and `%#T'", BINFO_TYPE (binfo));
457 warning (" will be re-ordered to match inheritance order");
460 last_base = BINFO_TYPE (binfo);
463 /* Make sure we won't try to work on this init again. */
464 TREE_PURPOSE (x) = NULL_TREE;
465 x = build_tree_list (binfo, TREE_VALUE (x));
470 /* If we didn't find BASE_BINFO in the list, create a dummy entry
471 so the two lists (RBASES and the list of bases) will be
473 x = build_tree_list (NULL_TREE, NULL_TREE);
475 rbases = chainon (rbases, x);
482 /* Invoke a base-class destructor. REF is the object being destroyed,
483 BINFO is the base class, and DTOR_ARG indicates whether the base
484 class should invoke delete. */
487 build_base_dtor_call (ref, binfo, dtor_arg)
488 tree ref, binfo, dtor_arg;
490 tree args = NULL_TREE;
491 tree vlist = lookup_name (vlist_identifier, 0);
494 if (TYPE_USES_PVBASES (BINFO_TYPE (binfo)))
496 args = expr_tree_cons (NULL_TREE, vlist, args);
497 dtor_arg = build (BIT_IOR_EXPR, integer_type_node,
498 dtor_arg, build_int_2 (4, 0));
499 dtor_arg = fold (dtor_arg);
501 args = expr_tree_cons (NULL_TREE, dtor_arg, args);
502 call = build_scoped_method_call (ref, binfo, dtor_identifier, args);
504 if (!TYPE_USES_PVBASES (BINFO_TYPE (binfo)))
505 /* For plain inheritance, do not try to adjust __vlist. */
508 /* Now decrement __vlist by the number of slots consumed by the base
510 decr = build_int_2 (pvbasecount (BINFO_TYPE (binfo), 0), 0);
511 decr = build_binary_op (MINUS_EXPR, vlist, decr);
512 decr = build_modify_expr (vlist, NOP_EXPR, decr);
514 return build (COMPOUND_EXPR, void_type_node, call, decr);
517 /* Return the number of vlist entries needed to initialize TYPE,
518 depending on whether it is IN_CHARGE. */
521 pvbasecount (type, in_charge)
529 for (vbase = (CLASSTYPE_VBASECLASSES (type)); vbase;
530 vbase = TREE_CHAIN (vbase))
532 result += list_length (CLASSTYPE_VFIELDS (BINFO_TYPE (vbase)));
534 result += pvbasecount (BINFO_TYPE (vbase), 0);
537 for (i=0; i < CLASSTYPE_N_BASECLASSES (type); i++)
539 tree base = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
540 if (TREE_VIA_VIRTUAL (base))
542 result += pvbasecount (BINFO_TYPE (base), 0);
553 tree vlist = lookup_name (vlist_identifier, 0);
555 name = alloca (strlen (VLIST_NAME_FORMAT)
556 + TYPE_ASSEMBLER_NAME_LENGTH (t) + 2);
557 sprintf (name, VLIST_NAME_FORMAT, TYPE_ASSEMBLER_NAME_STRING (t));
559 expr = get_identifier (name);
560 expr = lookup_name (expr, 0);
561 expr = build1 (ADDR_EXPR, TREE_TYPE (vlist), expr);
562 if (DECL_DESTRUCTOR_FOR_PVBASE_P (current_function_decl))
563 /* Move to the end of the vlist. */
564 expr = build_binary_op (PLUS_EXPR, expr,
565 build_int_2 (pvbasecount (t, 1), 0));
566 expand_expr_stmt (build_modify_expr (vlist, NOP_EXPR, expr));
569 /* Perform whatever initializations have yet to be done on the base
570 class of the class variable. These actions are in the global
571 variable CURRENT_BASE_INIT_LIST. Such an action could be
572 NULL_TREE, meaning that the user has explicitly called the base
573 class constructor with no arguments.
575 If there is a need for a call to a constructor, we must surround
576 that call with a pushlevel/poplevel pair, since we are technically
577 at the PARM level of scope.
579 Argument IMMEDIATELY, if zero, forces a new sequence to be
580 generated to contain these new insns, so it can be emitted later.
581 This sequence is saved in the global variable BASE_INIT_EXPR.
582 Otherwise, the insns are emitted into the current sequence.
584 Note that emit_base_init does *not* initialize virtual base
585 classes. That is done specially, elsewhere. */
587 extern tree base_init_expr, rtl_expr_chain;
590 emit_base_init (t, immediately)
596 tree rbase_init_list, vbase_init_list;
597 tree t_binfo = TYPE_BINFO (t);
598 tree binfos = BINFO_BASETYPES (t_binfo);
599 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
600 tree expr = NULL_TREE;
601 tree vlist = lookup_name (vlist_identifier, 0);
606 do_pending_stack_adjust ();
607 /* Make the RTL_EXPR node temporary, not momentary,
608 so that rtl_expr_chain doesn't become garbage. */
609 momentary = suspend_momentary ();
610 expr = make_node (RTL_EXPR);
611 resume_momentary (momentary);
612 start_sequence_for_rtl_expr (expr);
615 if (write_symbols == NO_DEBUG)
616 /* As a matter of principle, `start_sequence' should do this. */
619 /* Always emit a line number note so we can step into constructors. */
620 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
621 DECL_SOURCE_LINE (current_function_decl));
623 mem_init_list = sort_member_init (t);
624 current_member_init_list = NULL_TREE;
626 sort_base_init (t, &rbase_init_list, &vbase_init_list);
627 current_base_init_list = NULL_TREE;
629 /* First, initialize the virtual base classes, if we are
630 constructing the most-derived object. */
631 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
633 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
634 construct_virtual_bases (t, current_class_ref, current_class_ptr,
635 vbase_init_list, first_arg);
638 /* Now, perform initialization of non-virtual base classes. */
639 for (i = 0; i < n_baseclasses; i++)
641 tree base_binfo = TREE_VEC_ELT (binfos, i);
642 tree init = void_list_node;
644 if (TREE_VIA_VIRTUAL (base_binfo))
647 my_friendly_assert (BINFO_INHERITANCE_CHAIN (base_binfo) == t_binfo,
650 if (TREE_PURPOSE (rbase_init_list))
651 init = TREE_VALUE (rbase_init_list);
652 else if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
655 if (extra_warnings && copy_args_p (current_function_decl))
656 cp_warning ("base class `%#T' should be explicitly initialized in the copy constructor",
657 BINFO_TYPE (base_binfo));
660 if (init != void_list_node)
662 expand_start_target_temps ();
664 member = convert_pointer_to_real (base_binfo, current_class_ptr);
665 expand_aggr_init_1 (base_binfo, NULL_TREE,
666 build_indirect_ref (member, NULL_PTR), init,
669 expand_end_target_temps ();
673 expand_cleanup_for_base (base_binfo, vlist, NULL_TREE);
674 rbase_init_list = TREE_CHAIN (rbase_init_list);
677 /* Initialize all the virtual function table fields that
678 do come from virtual base classes. */
679 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
680 expand_indirect_vtbls_init (t_binfo, current_class_ref, current_class_ptr);
682 /* Initialize all the virtual function table fields that
683 do not come from virtual base classes. */
684 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_ptr);
686 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
691 /* member could be, for example, a CONST_DECL for an enumerated
692 tag; we don't want to try to initialize that, since it already
694 if (TREE_CODE (member) != FIELD_DECL || !DECL_NAME (member))
697 /* See if we had a user-specified member initialization. */
698 if (TREE_PURPOSE (mem_init_list))
700 name = TREE_PURPOSE (mem_init_list);
701 init = TREE_VALUE (mem_init_list);
705 if (TREE_CODE (name) == COMPONENT_REF)
706 name = DECL_NAME (TREE_OPERAND (name, 1));
708 /* Also see if it's ever a COMPONENT_REF here. If it is, we
709 need to do `expand_assignment (name, init, 0, 0);' and
711 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
716 name = DECL_NAME (member);
717 init = DECL_INITIAL (member);
721 /* Effective C++ rule 12. */
722 if (warn_ecpp && init == NULL_TREE
723 && !DECL_ARTIFICIAL (member)
724 && TREE_CODE (TREE_TYPE (member)) != ARRAY_TYPE)
725 cp_warning ("`%D' should be initialized in the member initialization list", member);
728 perform_member_init (member, name, init, from_init_list);
729 mem_init_list = TREE_CHAIN (mem_init_list);
732 /* Now initialize any members from our bases. */
733 while (mem_init_list)
735 tree name, init, field;
737 if (TREE_PURPOSE (mem_init_list))
739 name = TREE_PURPOSE (mem_init_list);
740 init = TREE_VALUE (mem_init_list);
741 /* XXX: this may need the COMPONENT_REF operand 0 check if
742 it turns out we actually get them. */
743 field = IDENTIFIER_CLASS_VALUE (name);
745 /* If one member shadows another, get the outermost one. */
746 if (TREE_CODE (field) == TREE_LIST)
748 field = TREE_VALUE (field);
749 if (decl_type_context (field) != current_class_type)
750 cp_error ("field `%D' not in immediate context", field);
754 /* It turns out if you have an anonymous union in the
755 class, a member from it can end up not being on the
756 list of fields (rather, the type is), and therefore
757 won't be seen by the for loop above. */
759 /* The code in this for loop is derived from a general loop
760 which had this check in it. Theoretically, we've hit
761 every initialization for the list of members in T, so
762 we shouldn't have anything but these left in this list. */
763 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
766 perform_member_init (field, name, init, 1);
768 mem_init_list = TREE_CHAIN (mem_init_list);
773 do_pending_stack_adjust ();
774 my_friendly_assert (base_init_expr == 0, 207);
775 base_init_expr = expr;
776 TREE_TYPE (expr) = void_type_node;
777 RTL_EXPR_RTL (expr) = const0_rtx;
778 RTL_EXPR_SEQUENCE (expr) = get_insns ();
779 rtl_expr_chain = tree_cons (NULL_TREE, expr, rtl_expr_chain);
781 TREE_SIDE_EFFECTS (expr) = 1;
784 /* All the implicit try blocks we built up will be zapped
785 when we come to a real binding contour boundary. */
788 /* Check that all fields are properly initialized after
789 an assignment to `this'. */
796 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
797 if (DECL_NAME (member) && TREE_USED (member))
798 cp_error ("field `%D' used before initialized (after assignment to `this')",
802 /* This code sets up the virtual function tables appropriate for
803 the pointer DECL. It is a one-ply initialization.
805 BINFO is the exact type that DECL is supposed to be. In
806 multiple inheritance, this might mean "C's A" if C : A, B. */
809 expand_virtual_init (binfo, decl)
812 tree type = BINFO_TYPE (binfo);
814 tree vtype, vtype_binfo;
816 /* This code is crusty. Should be simple, like:
817 vtbl = BINFO_VTABLE (binfo);
819 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
820 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
821 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
822 assemble_external (vtbl);
823 TREE_USED (vtbl) = 1;
824 vtbl = build1 (ADDR_EXPR, build_pointer_type (TREE_TYPE (vtbl)), vtbl);
825 decl = convert_pointer_to_real (vtype_binfo, decl);
826 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
827 if (vtbl_ptr == error_mark_node)
830 /* Have to convert VTBL since array sizes may be different. */
831 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl, 0);
832 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
835 /* If an exception is thrown in a constructor, those base classes already
836 constructed must be destroyed. This function creates the cleanup
837 for BINFO, which has just been constructed. If FLAG is non-NULL,
838 it is a DECL which is non-zero when this base needs to be
842 expand_cleanup_for_base (binfo, vlist, flag)
849 if (TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
851 /* All cleanups must be on the function_obstack. */
852 push_obstacks_nochange ();
853 resume_temporary_allocation ();
855 /* Call the destructor. */
856 expr = build_base_dtor_call (current_class_ref, binfo,
859 expr = fold (build (COND_EXPR, void_type_node,
860 truthvalue_conversion (flag),
861 expr, integer_zero_node));
864 add_partial_entry (expr);
867 if (TYPE_USES_PVBASES (BINFO_TYPE (binfo)))
869 /* Increment vlist by number of base's vbase classes. */
870 expr = build_int_2 (pvbasecount (BINFO_TYPE (binfo), 0), 0);
871 expr = build_binary_op (PLUS_EXPR, vlist, expr);
872 expr = build_modify_expr (vlist, NOP_EXPR, expr);
873 expand_expr_stmt (expr);
877 /* Subroutine of `expand_aggr_vbase_init'.
878 BINFO is the binfo of the type that is being initialized.
879 INIT_LIST is the list of initializers for the virtual baseclass. */
882 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
883 tree binfo, exp, addr, init_list;
885 tree init = purpose_member (binfo, init_list);
886 tree ref = build_indirect_ref (addr, NULL_PTR);
888 expand_start_target_temps ();
891 init = TREE_VALUE (init);
892 /* Call constructors, but don't set up vtables. */
893 expand_aggr_init_1 (binfo, exp, ref, init, LOOKUP_COMPLAIN);
895 expand_end_target_temps ();
899 /* Construct the virtual base-classes of THIS_REF (whose address is
900 THIS_PTR). The object has the indicated TYPE. The construction
901 actually takes place only if FLAG is non-zero. INIT_LIST is list
902 of initialization for constructor to perform. */
905 construct_virtual_bases (type, this_ref, this_ptr, init_list, flag)
914 tree vlist = NULL_TREE;
916 /* If there are no virtual baseclasses, we shouldn't even be here. */
917 my_friendly_assert (TYPE_USES_VIRTUAL_BASECLASSES (type), 19990621);
919 /* First set the pointers in our object that tell us where to find
920 our virtual baseclasses. */
921 expand_start_cond (flag, 0);
922 if (TYPE_USES_PVBASES (type))
925 vlist = lookup_name (vlist_identifier, 0);
927 result = init_vbase_pointers (type, this_ptr);
929 expand_expr_stmt (build_compound_expr (result));
932 /* Now, run through the baseclasses, initializing each. */
933 for (vbases = CLASSTYPE_VBASECLASSES (type); vbases;
934 vbases = TREE_CHAIN (vbases))
936 tree tmp = purpose_member (vbases, result);
938 /* If there are virtual base classes with destructors, we need to
939 emit cleanups to destroy them if an exception is thrown during
940 the construction process. These exception regions (i.e., the
941 period during which the cleanups must occur) begin from the time
942 the construction is complete to the end of the function. If we
943 create a conditional block in which to initialize the
944 base-classes, then the cleanup region for the virtual base begins
945 inside a block, and ends outside of that block. This situation
946 confuses the sjlj exception-handling code. Therefore, we do not
947 create a single conditional block, but one for each
948 initialization. (That way the cleanup regions always begin
949 in the outer block.) We trust the back-end to figure out
950 that the FLAG will not change across initializations, and
951 avoid doing multiple tests. */
952 expand_start_cond (flag, 0);
953 expand_aggr_vbase_init_1 (vbases, this_ref,
954 TREE_OPERAND (TREE_VALUE (tmp), 0),
958 expand_cleanup_for_base (vbases, vlist, flag);
962 /* Find the context in which this FIELD can be initialized. */
965 initializing_context (field)
968 tree t = DECL_CONTEXT (field);
970 /* Anonymous union members can be initialized in the first enclosing
971 non-anonymous union context. */
972 while (t && ANON_UNION_TYPE_P (t))
973 t = TYPE_CONTEXT (t);
977 /* Function to give error message if member initialization specification
978 is erroneous. FIELD is the member we decided to initialize.
979 TYPE is the type for which the initialization is being performed.
980 FIELD must be a member of TYPE.
982 MEMBER_NAME is the name of the member. */
985 member_init_ok_or_else (field, type, member_name)
988 const char *member_name;
990 if (field == error_mark_node)
992 if (field == NULL_TREE || initializing_context (field) != type)
994 cp_error ("class `%T' does not have any field named `%s'", type,
998 if (TREE_STATIC (field))
1000 cp_error ("field `%#D' is static; only point of initialization is its declaration",
1008 /* If NAME is a viable field name for the aggregate DECL,
1009 and PARMS is a viable parameter list, then expand an _EXPR
1010 which describes this initialization.
1012 Note that we do not need to chase through the class's base classes
1013 to look for NAME, because if it's in that list, it will be handled
1014 by the constructor for that base class.
1016 We do not yet have a fixed-point finder to instantiate types
1017 being fed to overloaded constructors. If there is a unique
1018 constructor, then argument types can be got from that one.
1020 If INIT is non-NULL, then it the initialization should
1021 be placed in `current_base_init_list', where it will be processed
1022 by `emit_base_init'. */
1025 expand_member_init (exp, name, init)
1026 tree exp, name, init;
1028 tree basetype = NULL_TREE, field;
1031 if (exp == NULL_TREE)
1032 return; /* complain about this later */
1034 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
1036 if (name && TREE_CODE (name) == TYPE_DECL)
1038 basetype = TYPE_MAIN_VARIANT (TREE_TYPE (name));
1039 name = DECL_NAME (name);
1042 if (name == NULL_TREE && IS_AGGR_TYPE (type))
1043 switch (CLASSTYPE_N_BASECLASSES (type))
1046 error ("base class initializer specified, but no base class to initialize");
1049 basetype = TYPE_BINFO_BASETYPE (type, 0);
1052 error ("initializer for unnamed base class ambiguous");
1053 cp_error ("(type `%T' uses multiple inheritance)", type);
1057 my_friendly_assert (init != NULL_TREE, 0);
1059 /* The grammar should not allow fields which have names that are
1060 TYPENAMEs. Therefore, if the field has a non-NULL TREE_TYPE, we
1061 may assume that this is an attempt to initialize a base class
1062 member of the current type. Otherwise, it is an attempt to
1063 initialize a member field. */
1065 if (init == void_type_node)
1068 if (name == NULL_TREE || basetype)
1072 if (name == NULL_TREE)
1076 name = TYPE_IDENTIFIER (basetype);
1079 error ("no base class to initialize");
1084 else if (basetype != type
1085 && ! current_template_parms
1086 && ! vec_binfo_member (basetype,
1087 TYPE_BINFO_BASETYPES (type))
1088 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
1090 if (IDENTIFIER_CLASS_VALUE (name))
1092 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1093 cp_error ("type `%T' is not an immediate or virtual basetype for `%T'",
1096 cp_error ("type `%T' is not an immediate basetype for `%T'",
1101 if (purpose_member (basetype, current_base_init_list))
1103 cp_error ("base class `%T' already initialized", basetype);
1107 if (warn_reorder && current_member_init_list)
1109 cp_warning ("base initializer for `%T'", basetype);
1110 warning (" will be re-ordered to precede member initializations");
1113 base_init = build_tree_list (basetype, init);
1114 current_base_init_list = chainon (current_base_init_list, base_init);
1121 field = lookup_field (type, name, 1, 0);
1123 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1126 if (purpose_member (name, current_member_init_list))
1128 cp_error ("field `%D' already initialized", field);
1132 member_init = build_tree_list (name, init);
1133 current_member_init_list = chainon (current_member_init_list, member_init);
1137 /* This is like `expand_member_init', only it stores one aggregate
1140 INIT comes in two flavors: it is either a value which
1141 is to be stored in EXP, or it is a parameter list
1142 to go to a constructor, which will operate on EXP.
1143 If INIT is not a parameter list for a constructor, then set
1144 LOOKUP_ONLYCONVERTING.
1145 If FLAGS is LOOKUP_ONLYCONVERTING then it is the = init form of
1146 the initializer, if FLAGS is 0, then it is the (init) form.
1147 If `init' is a CONSTRUCTOR, then we emit a warning message,
1148 explaining that such initializations are invalid.
1150 ALIAS_THIS is nonzero iff we are initializing something which is
1151 essentially an alias for current_class_ref. In this case, the base
1152 constructor may move it on us, and we must keep track of such
1155 If INIT resolves to a CALL_EXPR which happens to return
1156 something of the type we are looking for, then we know
1157 that we can safely use that call to perform the
1160 The virtual function table pointer cannot be set up here, because
1161 we do not really know its type.
1163 Virtual baseclass pointers are also set up here.
1165 This never calls operator=().
1167 When initializing, nothing is CONST.
1169 A default copy constructor may have to be used to perform the
1172 A constructor or a conversion operator may have to be used to
1173 perform the initialization, but not both, as it would be ambiguous. */
1176 expand_aggr_init (exp, init, flags)
1180 tree type = TREE_TYPE (exp);
1181 int was_const = TREE_READONLY (exp);
1182 int was_volatile = TREE_THIS_VOLATILE (exp);
1184 if (init == error_mark_node)
1187 TREE_READONLY (exp) = 0;
1188 TREE_THIS_VOLATILE (exp) = 0;
1190 if (init && TREE_CODE (init) != TREE_LIST)
1191 flags |= LOOKUP_ONLYCONVERTING;
1193 if (TREE_CODE (type) == ARRAY_TYPE)
1195 /* Must arrange to initialize each element of EXP
1196 from elements of INIT. */
1197 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1198 if (CP_TYPE_QUALS (type) != TYPE_UNQUALIFIED)
1200 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1202 TREE_TYPE (init) = TYPE_MAIN_VARIANT (itype);
1204 if (init && TREE_TYPE (init) == NULL_TREE)
1206 /* Handle bad initializers like:
1210 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1214 int main(int argc, char **argv) {
1215 COMPLEX zees(1.0, 0.0)[10];
1218 error ("bad array initializer");
1221 expand_vec_init (exp, exp, array_type_nelts (type), init,
1222 init && same_type_p (TREE_TYPE (init),
1224 TREE_READONLY (exp) = was_const;
1225 TREE_THIS_VOLATILE (exp) = was_volatile;
1226 TREE_TYPE (exp) = type;
1228 TREE_TYPE (init) = itype;
1232 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1233 /* just know that we've seen something for this node */
1234 TREE_USED (exp) = 1;
1237 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1238 constructor as parameters to an implicit GNU C++ constructor. */
1239 if (init && TREE_CODE (init) == CONSTRUCTOR
1240 && TYPE_HAS_CONSTRUCTOR (type)
1241 && TREE_TYPE (init) == type)
1242 init = CONSTRUCTOR_ELTS (init);
1245 TREE_TYPE (exp) = TYPE_MAIN_VARIANT (type);
1246 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1247 init, LOOKUP_NORMAL|flags);
1248 TREE_TYPE (exp) = type;
1249 TREE_READONLY (exp) = was_const;
1250 TREE_THIS_VOLATILE (exp) = was_volatile;
1254 no_vlist_base_init (rval, exp, init, binfo, flags)
1255 tree rval, exp, init, binfo;
1258 tree nrval, func, parms;
1260 /* Obtain the vlist-expecting ctor. */
1262 my_friendly_assert (TREE_CODE (func) == CALL_EXPR, 20000131);
1263 func = TREE_OPERAND (func, 0);
1264 my_friendly_assert (TREE_CODE (func) == ADDR_EXPR, 20000132);
1265 func = TREE_OPERAND (func, 0);
1266 my_friendly_assert (TREE_CODE (func) == FUNCTION_DECL, 20000133);
1268 /* If we have already seen a definition for the wrapped function,
1269 we don't need to declare it weak. Also, declare_weak will complain
1271 if (!TREE_ASM_WRITTEN (func))
1272 declare_weak (func);
1274 if (init == NULL_TREE
1275 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1279 init = TREE_VALUE (parms);
1282 parms = build_expr_list (NULL_TREE, init);
1284 flags &= ~LOOKUP_HAS_VLIST;
1286 parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms);
1287 flags |= LOOKUP_HAS_IN_CHARGE;
1289 nrval = build_method_call (exp, ctor_identifier,
1290 parms, binfo, flags);
1292 func = build (NE_EXPR, boolean_type_node,
1293 func, null_pointer_node);
1294 nrval = build (COND_EXPR, void_type_node,
1300 expand_default_init (binfo, true_exp, exp, init, flags)
1306 tree type = TREE_TYPE (exp);
1308 /* It fails because there may not be a constructor which takes
1309 its own type as the first (or only parameter), but which does
1310 take other types via a conversion. So, if the thing initializing
1311 the expression is a unit element of type X, first try X(X&),
1312 followed by initialization by X. If neither of these work
1313 out, then look hard. */
1316 tree vlist = NULL_TREE;
1317 tree orig_init = init;
1319 if (init && TREE_CODE (init) != TREE_LIST
1320 && (flags & LOOKUP_ONLYCONVERTING))
1322 /* Base subobjects should only get direct-initialization. */
1323 if (true_exp != exp)
1326 if (flags & DIRECT_BIND)
1327 /* Do nothing. We hit this in two cases: Reference initialization,
1328 where we aren't initializing a real variable, so we don't want
1329 to run a new constructor; and catching an exception, where we
1330 have already built up the constructor call so we could wrap it
1331 in an exception region. */;
1333 init = ocp_convert (type, init, CONV_IMPLICIT|CONV_FORCE_TEMP, flags);
1335 if (TREE_CODE (init) == TRY_CATCH_EXPR)
1336 /* We need to protect the initialization of a catch parm
1337 with a call to terminate(), which shows up as a TRY_CATCH_EXPR
1338 around the TARGET_EXPR for the copy constructor. See
1339 expand_start_catch_block. */
1340 TREE_OPERAND (init, 0) = build (INIT_EXPR, TREE_TYPE (exp), exp,
1341 TREE_OPERAND (init, 0));
1343 init = build (INIT_EXPR, TREE_TYPE (exp), exp, init);
1344 TREE_SIDE_EFFECTS (init) = 1;
1345 expand_expr_stmt (init);
1349 if (init == NULL_TREE
1350 || (TREE_CODE (init) == TREE_LIST && ! TREE_TYPE (init)))
1354 init = TREE_VALUE (parms);
1357 parms = build_expr_list (NULL_TREE, init);
1359 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1361 if (TYPE_USES_PVBASES (type))
1363 /* In compatibility mode, when not calling a base ctor,
1364 we do not pass the vlist argument. */
1365 if (true_exp == exp)
1366 vlist = flag_vtable_thunks_compat? NULL_TREE : vlist_zero_node;
1368 vlist = lookup_name (vlist_identifier, 0);
1372 parms = expr_tree_cons (NULL_TREE, vlist, parms);
1373 flags |= LOOKUP_HAS_VLIST;
1376 if (true_exp == exp)
1377 parms = expr_tree_cons (NULL_TREE, integer_one_node, parms);
1379 parms = expr_tree_cons (NULL_TREE, integer_zero_node, parms);
1380 flags |= LOOKUP_HAS_IN_CHARGE;
1383 rval = build_method_call (exp, ctor_identifier,
1384 parms, binfo, flags);
1385 if (vlist && true_exp != exp && flag_vtable_thunks_compat)
1387 rval = no_vlist_base_init (rval, exp, orig_init, binfo, flags);
1389 if (TREE_SIDE_EFFECTS (rval))
1390 expand_expr_stmt (rval);
1393 /* This function is responsible for initializing EXP with INIT
1396 BINFO is the binfo of the type for who we are performing the
1397 initialization. For example, if W is a virtual base class of A and B,
1399 If we are initializing B, then W must contain B's W vtable, whereas
1400 were we initializing C, W must contain C's W vtable.
1402 TRUE_EXP is nonzero if it is the true expression being initialized.
1403 In this case, it may be EXP, or may just contain EXP. The reason we
1404 need this is because if EXP is a base element of TRUE_EXP, we
1405 don't necessarily know by looking at EXP where its virtual
1406 baseclass fields should really be pointing. But we do know
1407 from TRUE_EXP. In constructors, we don't know anything about
1408 the value being initialized.
1410 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1412 FLAGS is just passes to `build_method_call'. See that function for
1416 expand_aggr_init_1 (binfo, true_exp, exp, init, flags)
1422 tree type = TREE_TYPE (exp);
1424 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1426 /* Use a function returning the desired type to initialize EXP for us.
1427 If the function is a constructor, and its first argument is
1428 NULL_TREE, know that it was meant for us--just slide exp on
1429 in and expand the constructor. Constructors now come
1432 if (init && TREE_CODE (exp) == VAR_DECL
1433 && TREE_CODE (init) == CONSTRUCTOR
1434 && TREE_HAS_CONSTRUCTOR (init))
1436 tree t = store_init_value (exp, init);
1439 expand_decl_init (exp);
1442 t = build (INIT_EXPR, type, exp, init);
1443 TREE_SIDE_EFFECTS (t) = 1;
1444 expand_expr_stmt (t);
1448 /* We know that expand_default_init can handle everything we want
1450 expand_default_init (binfo, true_exp, exp, init, flags);
1453 /* Report an error if NAME is not the name of a user-defined,
1454 aggregate type. If OR_ELSE is nonzero, give an error message. */
1457 is_aggr_typedef (name, or_else)
1463 if (name == error_mark_node)
1466 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1467 type = IDENTIFIER_TYPE_VALUE (name);
1471 cp_error ("`%T' is not an aggregate typedef", name);
1475 if (! IS_AGGR_TYPE (type)
1476 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1477 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1480 cp_error ("`%T' is not an aggregate type", type);
1486 /* Report an error if TYPE is not a user-defined, aggregate type. If
1487 OR_ELSE is nonzero, give an error message. */
1490 is_aggr_type (type, or_else)
1494 if (type == error_mark_node)
1497 if (! IS_AGGR_TYPE (type)
1498 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1499 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1502 cp_error ("`%T' is not an aggregate type", type);
1508 /* Like is_aggr_typedef, but returns typedef if successful. */
1511 get_aggr_from_typedef (name, or_else)
1517 if (name == error_mark_node)
1520 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1521 type = IDENTIFIER_TYPE_VALUE (name);
1525 cp_error ("`%T' fails to be an aggregate typedef", name);
1529 if (! IS_AGGR_TYPE (type)
1530 && TREE_CODE (type) != TEMPLATE_TYPE_PARM
1531 && TREE_CODE (type) != TEMPLATE_TEMPLATE_PARM)
1534 cp_error ("type `%T' is of non-aggregate type", type);
1541 get_type_value (name)
1544 if (name == error_mark_node)
1547 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1548 return IDENTIFIER_TYPE_VALUE (name);
1554 /* This code could just as well go in `class.c', but is placed here for
1557 /* For an expression of the form TYPE :: NAME (PARMLIST), build
1558 the appropriate function call. */
1561 build_member_call (type, name, parmlist)
1562 tree type, name, parmlist;
1567 tree basetype_path, decl;
1569 if (TREE_CODE (name) == TEMPLATE_ID_EXPR
1570 && TREE_CODE (type) == NAMESPACE_DECL)
1572 /* 'name' already refers to the decls from the namespace, since we
1573 hit do_identifier for template_ids. */
1574 method_name = TREE_OPERAND (name, 0);
1575 /* FIXME: Since we don't do independent names right yet, the
1576 name might also be a LOOKUP_EXPR. Once we resolve this to a
1577 real decl earlier, this can go. This may happen during
1579 if (TREE_CODE (method_name) == LOOKUP_EXPR)
1581 method_name = lookup_namespace_name
1582 (type, TREE_OPERAND (method_name, 0));
1583 TREE_OPERAND (name, 0) = method_name;
1585 my_friendly_assert (is_overloaded_fn (method_name), 980519);
1586 return build_x_function_call (name, parmlist, current_class_ref);
1589 if (type == std_node)
1590 return build_x_function_call (do_scoped_id (name, 0), parmlist,
1592 if (TREE_CODE (type) == NAMESPACE_DECL)
1593 return build_x_function_call (lookup_namespace_name (type, name),
1594 parmlist, current_class_ref);
1596 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1598 method_name = TREE_OPERAND (name, 0);
1599 if (TREE_CODE (method_name) == COMPONENT_REF)
1600 method_name = TREE_OPERAND (method_name, 1);
1601 if (is_overloaded_fn (method_name))
1602 method_name = DECL_NAME (OVL_CURRENT (method_name));
1603 TREE_OPERAND (name, 0) = method_name;
1608 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1610 method_name = TREE_OPERAND (method_name, 0);
1614 /* This shouldn't be here, and build_member_call shouldn't appear in
1616 if (type && TREE_CODE (type) == IDENTIFIER_NODE
1617 && get_aggr_from_typedef (type, 0) == 0)
1619 tree ns = lookup_name (type, 0);
1620 if (ns && TREE_CODE (ns) == NAMESPACE_DECL)
1622 return build_x_function_call (build_offset_ref (type, name), parmlist, current_class_ref);
1626 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1627 return error_mark_node;
1629 /* An operator we did not like. */
1630 if (name == NULL_TREE)
1631 return error_mark_node;
1635 cp_error ("cannot call destructor `%T::~%T' without object", type,
1637 return error_mark_node;
1640 decl = maybe_dummy_object (type, &basetype_path);
1642 /* Convert 'this' to the specified type to disambiguate conversion
1643 to the function's context. Apparently Standard C++ says that we
1644 shouldn't do this. */
1645 if (decl == current_class_ref
1647 && ACCESSIBLY_UNIQUELY_DERIVED_P (type, current_class_type))
1649 tree olddecl = current_class_ptr;
1650 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1651 if (oldtype != type)
1653 tree newtype = build_qualified_type (type, TYPE_QUALS (oldtype));
1654 decl = convert_force (build_pointer_type (newtype), olddecl, 0);
1655 decl = build_indirect_ref (decl, NULL_PTR);
1659 if (method_name == constructor_name (type)
1660 || method_name == constructor_name_full (type))
1661 return build_functional_cast (type, parmlist);
1662 if (lookup_fnfields (basetype_path, method_name, 0))
1663 return build_method_call (decl,
1664 TREE_CODE (name) == TEMPLATE_ID_EXPR
1665 ? name : method_name,
1666 parmlist, basetype_path,
1667 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1668 if (TREE_CODE (name) == IDENTIFIER_NODE
1669 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1671 if (t == error_mark_node)
1672 return error_mark_node;
1673 if (TREE_CODE (t) == FIELD_DECL)
1675 if (is_dummy_object (decl))
1677 cp_error ("invalid use of non-static field `%D'", t);
1678 return error_mark_node;
1680 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1682 else if (TREE_CODE (t) == VAR_DECL)
1686 cp_error ("invalid use of member `%D'", t);
1687 return error_mark_node;
1689 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl)))
1690 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl,
1691 parmlist, NULL_TREE);
1692 return build_function_call (decl, parmlist);
1696 cp_error ("no method `%T::%D'", type, name);
1697 return error_mark_node;
1701 /* Build a reference to a member of an aggregate. This is not a
1702 C++ `&', but really something which can have its address taken,
1703 and then act as a pointer to member, for example TYPE :: FIELD
1704 can have its address taken by saying & TYPE :: FIELD.
1706 @@ Prints out lousy diagnostics for operator <typename>
1709 @@ This function should be rewritten and placed in search.c. */
1712 build_offset_ref (type, name)
1715 tree decl, t = error_mark_node;
1717 tree basebinfo = NULL_TREE;
1718 tree orig_name = name;
1720 /* class templates can come in as TEMPLATE_DECLs here. */
1721 if (TREE_CODE (name) == TEMPLATE_DECL)
1724 if (type == std_node)
1725 return do_scoped_id (name, 0);
1727 if (processing_template_decl || uses_template_parms (type))
1728 return build_min_nt (SCOPE_REF, type, name);
1730 /* Handle namespace names fully here. */
1731 if (TREE_CODE (type) == NAMESPACE_DECL)
1733 t = lookup_namespace_name (type, name);
1734 if (t != error_mark_node && ! type_unknown_p (t))
1737 t = convert_from_reference (t);
1742 if (type == NULL_TREE || ! is_aggr_type (type, 1))
1743 return error_mark_node;
1745 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
1747 /* If the NAME is a TEMPLATE_ID_EXPR, we are looking at
1748 something like `a.template f<int>' or the like. For the most
1749 part, we treat this just like a.f. We do remember, however,
1750 the template-id that was used. */
1751 name = TREE_OPERAND (orig_name, 0);
1753 if (TREE_CODE (name) == LOOKUP_EXPR)
1754 /* This can happen during tsubst'ing. */
1755 name = TREE_OPERAND (name, 0);
1757 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 0);
1760 if (TREE_CODE (name) == BIT_NOT_EXPR)
1762 if (! check_dtor_name (type, name))
1763 cp_error ("qualified type `%T' does not match destructor name `~%T'",
1764 type, TREE_OPERAND (name, 0));
1765 name = dtor_identifier;
1768 /* I think this is wrong, but the draft is unclear. --jason 6/15/98 */
1769 else if (name == constructor_name_full (type)
1770 || name == constructor_name (type))
1771 name = ctor_identifier;
1774 if (TYPE_SIZE (complete_type (type)) == 0
1775 && !TYPE_BEING_DEFINED (type))
1777 cp_error ("incomplete type `%T' does not have member `%D'", type,
1779 return error_mark_node;
1782 decl = maybe_dummy_object (type, &basebinfo);
1784 member = lookup_member (basebinfo, name, 1, 0);
1786 if (member == error_mark_node)
1787 return error_mark_node;
1789 /* A lot of this logic is now handled in lookup_field and
1791 if (member && BASELINK_P (member))
1793 /* Go from the TREE_BASELINK to the member function info. */
1794 tree fnfields = member;
1795 t = TREE_VALUE (fnfields);
1797 if (TREE_CODE (orig_name) == TEMPLATE_ID_EXPR)
1799 /* The FNFIELDS are going to contain functions that aren't
1800 necessarily templates, and templates that don't
1801 necessarily match the explicit template parameters. We
1802 save all the functions, and the explicit parameters, and
1803 then figure out exactly what to instantiate with what
1804 arguments in instantiate_type. */
1806 if (TREE_CODE (t) != OVERLOAD)
1807 /* The code in instantiate_type which will process this
1808 expects to encounter OVERLOADs, not raw functions. */
1809 t = ovl_cons (t, NULL_TREE);
1811 return build (OFFSET_REF,
1814 build (TEMPLATE_ID_EXPR,
1817 TREE_OPERAND (orig_name, 1)));
1820 if (!really_overloaded_fn (t))
1822 /* Get rid of a potential OVERLOAD around it */
1823 t = OVL_CURRENT (t);
1825 /* unique functions are handled easily. */
1826 basebinfo = TREE_PURPOSE (fnfields);
1827 if (!enforce_access (basebinfo, t))
1828 return error_mark_node;
1830 if (DECL_STATIC_FUNCTION_P (t))
1832 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
1835 /* FNFIELDS is most likely allocated on the search_obstack,
1836 which will go away after this class scope. If we need
1837 to save this value for later (i.e. for use as an initializer
1838 for a static variable), then do so here.
1840 ??? The smart thing to do for the case of saving initializers
1841 is to resolve them before we're done with this scope. */
1842 if (!TREE_PERMANENT (fnfields)
1843 && ! allocation_temporary_p ())
1844 fnfields = copy_list (fnfields);
1846 TREE_TYPE (fnfields) = unknown_type_node;
1847 return build (OFFSET_REF, unknown_type_node, decl, fnfields);
1854 cp_error ("`%D' is not a member of type `%T'", name, type);
1855 return error_mark_node;
1858 if (TREE_CODE (t) == TYPE_DECL)
1863 /* static class members and class-specific enum
1864 values can be returned without further ado. */
1865 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
1868 return convert_from_reference (t);
1871 if (TREE_CODE (t) == FIELD_DECL && DECL_C_BIT_FIELD (t))
1873 cp_error ("illegal pointer to bit field `%D'", t);
1874 return error_mark_node;
1877 /* static class functions too. */
1878 if (TREE_CODE (t) == FUNCTION_DECL
1879 && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
1880 my_friendly_abort (53);
1882 /* In member functions, the form `type::name' is no longer
1883 equivalent to `this->type::name', at least not until
1884 resolve_offset_ref. */
1885 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
1888 /* If a OFFSET_REF made it through to here, then it did
1889 not have its address taken. */
1892 resolve_offset_ref (exp)
1895 tree type = TREE_TYPE (exp);
1896 tree base = NULL_TREE;
1898 tree basetype, addr;
1900 if (TREE_CODE (exp) == OFFSET_REF)
1902 member = TREE_OPERAND (exp, 1);
1903 base = TREE_OPERAND (exp, 0);
1907 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
1908 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
1910 error ("object missing in use of pointer-to-member construct");
1911 return error_mark_node;
1914 type = TREE_TYPE (type);
1915 base = current_class_ref;
1918 if (BASELINK_P (member))
1920 cp_pedwarn ("assuming & on overloaded member function");
1921 return build_unary_op (ADDR_EXPR, exp, 0);
1924 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
1926 cp_pedwarn ("assuming & on `%E'", member);
1927 return build_unary_op (ADDR_EXPR, exp, 0);
1930 if ((TREE_CODE (member) == VAR_DECL
1931 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member))
1932 && ! TYPE_PTRMEM_P (TREE_TYPE (member)))
1933 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
1935 /* These were static members. */
1936 if (mark_addressable (member) == 0)
1937 return error_mark_node;
1941 if (TREE_CODE (TREE_TYPE (member)) == POINTER_TYPE
1942 && TREE_CODE (TREE_TYPE (TREE_TYPE (member))) == METHOD_TYPE)
1945 /* Syntax error can cause a member which should
1946 have been seen as static to be grok'd as non-static. */
1947 if (TREE_CODE (member) == FIELD_DECL && current_class_ref == NULL_TREE)
1949 if (TREE_ADDRESSABLE (member) == 0)
1951 cp_error_at ("member `%D' is non-static but referenced as a static member",
1953 error ("at this point in file");
1954 TREE_ADDRESSABLE (member) = 1;
1956 return error_mark_node;
1959 /* The first case is really just a reference to a member of `this'. */
1960 if (TREE_CODE (member) == FIELD_DECL
1961 && (base == current_class_ref || is_dummy_object (base)))
1966 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
1967 basetype = TYPE_OFFSET_BASETYPE (type);
1969 basetype = DECL_CONTEXT (member);
1971 base = current_class_ptr;
1973 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
1975 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
1976 return error_mark_node;
1978 /* Kludge: we need to use basetype_path now, because
1979 convert_pointer_to will bash it. */
1980 enforce_access (basetype_path, member);
1981 addr = convert_pointer_to (basetype, base);
1983 /* Even in the case of illegal access, we form the
1984 COMPONENT_REF; that will allow better error recovery than
1985 just feeding back error_mark_node. */
1986 expr = build (COMPONENT_REF, TREE_TYPE (member),
1987 build_indirect_ref (addr, NULL_PTR), member);
1988 return convert_from_reference (expr);
1991 /* Ensure that we have an object. */
1992 if (is_dummy_object (base))
1993 addr = error_mark_node;
1995 /* If this is a reference to a member function, then return the
1996 address of the member function (which may involve going
1997 through the object's vtable), otherwise, return an expression
1998 for the dereferenced pointer-to-member construct. */
1999 addr = build_unary_op (ADDR_EXPR, base, 0);
2001 if (TYPE_PTRMEM_P (TREE_TYPE (member)))
2003 if (addr == error_mark_node)
2005 cp_error ("object missing in `%E'", exp);
2006 return error_mark_node;
2009 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (TREE_TYPE (member)));
2010 addr = convert_pointer_to (basetype, addr);
2011 member = cp_convert (ptrdiff_type_node, member);
2013 /* Pointer to data members are offset by one, so that a null
2014 pointer with a real value of 0 is distinguishable from an
2015 offset of the first member of a structure. */
2016 member = build_binary_op (MINUS_EXPR, member,
2017 cp_convert (ptrdiff_type_node, integer_one_node));
2019 return build1 (INDIRECT_REF, type,
2020 build (PLUS_EXPR, build_pointer_type (type),
2023 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2025 return get_member_function_from_ptrfunc (&addr, member);
2027 my_friendly_abort (56);
2032 /* Return either DECL or its known constant value (if it has one). */
2035 decl_constant_value (decl)
2038 if (! TREE_THIS_VOLATILE (decl)
2039 && DECL_INITIAL (decl)
2040 && DECL_INITIAL (decl) != error_mark_node
2041 /* This is invalid if initial value is not constant.
2042 If it has either a function call, a memory reference,
2043 or a variable, then re-evaluating it could give different results. */
2044 && TREE_CONSTANT (DECL_INITIAL (decl))
2045 /* Check for cases where this is sub-optimal, even though valid. */
2046 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR)
2047 return DECL_INITIAL (decl);
2051 /* Common subroutines of build_new and build_vec_delete. */
2053 /* Call the global __builtin_delete to delete ADDR. */
2056 build_builtin_delete_call (addr)
2059 mark_used (global_delete_fndecl);
2060 return build_call (global_delete_fndecl,
2061 void_type_node, build_expr_list (NULL_TREE, addr));
2064 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2065 (which needs to go through some sort of groktypename) or it
2066 is the name of the class we are newing. INIT is an initialization value.
2067 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2068 If INIT is void_type_node, it means do *not* call a constructor
2071 For types with constructors, the data returned is initialized
2072 by the appropriate constructor.
2074 Whether the type has a constructor or not, if it has a pointer
2075 to a virtual function table, then that pointer is set up
2078 Unless I am mistaken, a call to new () will return initialized
2079 data regardless of whether the constructor itself is private or
2080 not. NOPE; new fails if the constructor is private (jcm).
2082 Note that build_new does nothing to assure that any special
2083 alignment requirements of the type are met. Rather, it leaves
2084 it up to malloc to do the right thing. Otherwise, folding to
2085 the right alignment cal cause problems if the user tries to later
2086 free the memory returned by `new'.
2088 PLACEMENT is the `placement' list for user-defined operator new (). */
2090 extern int flag_check_new;
2093 build_new (placement, decl, init, use_global_new)
2099 tree nelts = NULL_TREE, t;
2102 tree pending_sizes = NULL_TREE;
2104 if (decl == error_mark_node)
2105 return error_mark_node;
2107 if (TREE_CODE (decl) == TREE_LIST)
2109 tree absdcl = TREE_VALUE (decl);
2110 tree last_absdcl = NULL_TREE;
2111 int old_immediate_size_expand = 0;
2113 if (current_function_decl
2114 && DECL_CONSTRUCTOR_P (current_function_decl))
2116 old_immediate_size_expand = immediate_size_expand;
2117 immediate_size_expand = 0;
2120 nelts = integer_one_node;
2122 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2123 my_friendly_abort (215);
2124 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2126 last_absdcl = absdcl;
2127 absdcl = TREE_OPERAND (absdcl, 0);
2130 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2132 /* probably meant to be a vec new */
2135 while (TREE_OPERAND (absdcl, 0)
2136 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2138 last_absdcl = absdcl;
2139 absdcl = TREE_OPERAND (absdcl, 0);
2143 this_nelts = TREE_OPERAND (absdcl, 1);
2144 if (this_nelts != error_mark_node)
2146 if (this_nelts == NULL_TREE)
2147 error ("new of array type fails to specify size");
2148 else if (processing_template_decl)
2151 absdcl = TREE_OPERAND (absdcl, 0);
2155 int flags = pedantic ? WANT_INT : (WANT_INT | WANT_ENUM);
2156 if (build_expr_type_conversion (flags, this_nelts, 0)
2158 pedwarn ("size in array new must have integral type");
2160 this_nelts = save_expr (cp_convert (sizetype, this_nelts));
2161 absdcl = TREE_OPERAND (absdcl, 0);
2162 if (this_nelts == integer_zero_node)
2164 warning ("zero size array reserves no space");
2165 nelts = integer_zero_node;
2168 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2172 nelts = integer_zero_node;
2176 TREE_OPERAND (last_absdcl, 0) = absdcl;
2178 TREE_VALUE (decl) = absdcl;
2180 type = groktypename (decl);
2181 if (! type || type == error_mark_node)
2183 immediate_size_expand = old_immediate_size_expand;
2184 return error_mark_node;
2187 if (current_function_decl
2188 && DECL_CONSTRUCTOR_P (current_function_decl))
2190 pending_sizes = get_pending_sizes ();
2191 immediate_size_expand = old_immediate_size_expand;
2194 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
2196 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
2198 /* An aggregate type. */
2199 type = IDENTIFIER_TYPE_VALUE (decl);
2200 decl = TYPE_MAIN_DECL (type);
2204 /* A builtin type. */
2205 decl = lookup_name (decl, 1);
2206 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
2207 type = TREE_TYPE (decl);
2210 else if (TREE_CODE (decl) == TYPE_DECL)
2212 type = TREE_TYPE (decl);
2217 decl = TYPE_MAIN_DECL (type);
2220 if (processing_template_decl)
2223 t = min_tree_cons (min_tree_cons (NULL_TREE, type, NULL_TREE),
2224 build_min_nt (ARRAY_REF, NULL_TREE, nelts),
2229 rval = build_min_nt (NEW_EXPR, placement, t, init);
2230 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2234 /* ``A reference cannot be created by the new operator. A reference
2235 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
2236 returned by new.'' ARM 5.3.3 */
2237 if (TREE_CODE (type) == REFERENCE_TYPE)
2239 error ("new cannot be applied to a reference type");
2240 type = TREE_TYPE (type);
2243 if (TREE_CODE (type) == FUNCTION_TYPE)
2245 error ("new cannot be applied to a function type");
2246 return error_mark_node;
2249 /* When the object being created is an array, the new-expression yields a
2250 pointer to the initial element (if any) of the array. For example,
2251 both new int and new int[10] return an int*. 5.3.4. */
2252 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
2254 nelts = array_type_nelts_top (type);
2256 type = TREE_TYPE (type);
2260 t = build_nt (ARRAY_REF, type, nelts);
2264 rval = build (NEW_EXPR, build_pointer_type (type), placement, t, init);
2265 NEW_EXPR_USE_GLOBAL (rval) = use_global_new;
2266 TREE_SIDE_EFFECTS (rval) = 1;
2268 /* Wrap it in a NOP_EXPR so warn_if_unused_value doesn't complain. */
2269 rval = build1 (NOP_EXPR, TREE_TYPE (rval), rval);
2270 TREE_NO_UNUSED_WARNING (rval) = 1;
2273 rval = build_compound_expr (chainon (pending_sizes,
2274 build_expr_list (NULL_TREE, rval)));
2279 /* If non-NULL, a POINTER_TYPE equivalent to (java::lang::Class*). */
2281 static tree jclass_node = NULL_TREE;
2283 /* Given a Java class, return a decl for the corresponding java.lang.Class. */
2286 build_java_class_ref (type)
2289 tree name, class_decl;
2290 static tree CL_prefix = NULL_TREE;
2291 if (CL_prefix == NULL_TREE)
2292 CL_prefix = get_identifier("_CL_");
2293 if (jclass_node == NULL_TREE)
2295 jclass_node = IDENTIFIER_GLOBAL_VALUE (get_identifier("jclass"));
2296 if (jclass_node == NULL_TREE)
2297 fatal("call to Java constructor, while `jclass' undefined");
2298 jclass_node = TREE_TYPE (jclass_node);
2300 name = build_overload_with_type (CL_prefix, type);
2301 class_decl = IDENTIFIER_GLOBAL_VALUE (name);
2302 if (class_decl == NULL_TREE)
2304 push_obstacks_nochange ();
2305 end_temporary_allocation ();
2306 class_decl = build_decl (VAR_DECL, name, TREE_TYPE (jclass_node));
2307 TREE_STATIC (class_decl) = 1;
2308 DECL_EXTERNAL (class_decl) = 1;
2309 TREE_PUBLIC (class_decl) = 1;
2310 DECL_ARTIFICIAL (class_decl) = 1;
2311 DECL_IGNORED_P (class_decl) = 1;
2312 pushdecl_top_level (class_decl);
2313 make_decl_rtl (class_decl, NULL_PTR, 1);
2319 /* Called from cplus_expand_expr when expanding a NEW_EXPR. The return
2320 value is immediately handed to expand_expr. */
2326 tree placement, init;
2327 tree type, true_type, size, rval;
2328 tree nelts = NULL_TREE;
2329 tree alloc_expr, alloc_node = NULL_TREE;
2331 enum tree_code code = NEW_EXPR;
2332 int use_cookie, nothrow, check_new;
2334 int use_java_new = 0;
2336 placement = TREE_OPERAND (exp, 0);
2337 type = TREE_OPERAND (exp, 1);
2338 init = TREE_OPERAND (exp, 2);
2339 use_global_new = NEW_EXPR_USE_GLOBAL (exp);
2341 if (TREE_CODE (type) == ARRAY_REF)
2344 nelts = TREE_OPERAND (type, 1);
2345 type = TREE_OPERAND (type, 0);
2349 if (CP_TYPE_QUALS (type))
2350 type = TYPE_MAIN_VARIANT (type);
2352 /* If our base type is an array, then make sure we know how many elements
2354 while (TREE_CODE (true_type) == ARRAY_TYPE)
2356 tree this_nelts = array_type_nelts_top (true_type);
2357 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts);
2358 true_type = TREE_TYPE (true_type);
2361 if (!complete_type_or_else (true_type, exp))
2362 return error_mark_node;
2365 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
2368 size = size_in_bytes (type);
2370 if (TREE_CODE (true_type) == VOID_TYPE)
2372 error ("invalid type `void' for new");
2373 return error_mark_node;
2376 if (TYPE_LANG_SPECIFIC (true_type)
2377 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
2379 abstract_virtuals_error (NULL_TREE, true_type);
2380 return error_mark_node;
2383 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
2385 signature_error (NULL_TREE, true_type);
2386 return error_mark_node;
2389 /* When we allocate an array, and the corresponding deallocation
2390 function takes a second argument of type size_t, and that's the
2391 "usual deallocation function", we allocate some extra space at
2392 the beginning of the array to store the size of the array.
2394 Well, that's what we should do. For backwards compatibility, we
2395 have to do this whenever there's a two-argument array-delete
2398 FIXME: For -fnew-abi, we don't have to maintain backwards
2399 compatibility and we should fix this. */
2400 use_cookie = (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type)
2401 && ! (placement && ! TREE_CHAIN (placement)
2402 && TREE_TYPE (TREE_VALUE (placement)) == ptr_type_node));
2406 tree extra = BI_header_size;
2408 size = size_binop (PLUS_EXPR, size, extra);
2413 code = VEC_NEW_EXPR;
2415 if (init && pedantic)
2416 cp_pedwarn ("initialization in array new");
2419 /* Allocate the object. */
2421 if (! has_array && ! placement && flag_this_is_variable > 0
2422 && TYPE_NEEDS_CONSTRUCTING (true_type) && init != void_type_node)
2424 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
2428 error ("constructors take parameter lists");
2429 return error_mark_node;
2432 else if (! placement && TYPE_FOR_JAVA (true_type))
2434 tree class_addr, alloc_decl;
2435 tree class_decl = build_java_class_ref (true_type);
2436 tree class_size = size_in_bytes (true_type);
2437 static char alloc_name[] = "_Jv_AllocObject";
2439 alloc_decl = IDENTIFIER_GLOBAL_VALUE (get_identifier (alloc_name));
2440 if (alloc_decl == NULL_TREE)
2441 fatal("call to Java constructor, while `%s' undefined", alloc_name);
2442 class_addr = build1 (ADDR_EXPR, jclass_node, class_decl);
2443 rval = build_function_call (alloc_decl,
2444 tree_cons (NULL_TREE, class_addr,
2445 build_tree_list (NULL_TREE,
2447 rval = cp_convert (build_pointer_type (true_type), rval);
2453 if (flag_exceptions)
2454 /* We will use RVAL when generating an exception handler for
2455 this new-expression, so we must save it. */
2456 susp = suspend_momentary ();
2458 rval = build_op_new_call
2459 (code, true_type, expr_tree_cons (NULL_TREE, size, placement),
2460 LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL));
2461 rval = cp_convert (build_pointer_type (true_type), rval);
2463 if (flag_exceptions)
2464 resume_momentary (susp);
2467 /* unless an allocation function is declared with an empty excep-
2468 tion-specification (_except.spec_), throw(), it indicates failure to
2469 allocate storage by throwing a bad_alloc exception (clause _except_,
2470 _lib.bad.alloc_); it returns a non-null pointer otherwise If the allo-
2471 cation function is declared with an empty exception-specification,
2472 throw(), it returns null to indicate failure to allocate storage and a
2473 non-null pointer otherwise.
2475 So check for a null exception spec on the op new we just called. */
2480 /* The CALL_EXPR. */
2481 tree t = TREE_OPERAND (rval, 0);
2483 t = TREE_OPERAND (TREE_OPERAND (t, 0), 0);
2484 nothrow = TYPE_NOTHROW_P (TREE_TYPE (t));
2486 check_new = (flag_check_new || nothrow) && ! use_java_new;
2488 if ((check_new || flag_exceptions) && rval)
2490 alloc_expr = get_target_expr (rval);
2491 alloc_node = rval = TREE_OPERAND (alloc_expr, 0);
2494 alloc_expr = NULL_TREE;
2496 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
2497 sure we have some extra bytes in that case for the BI_header_size
2498 cookies? And how does that interact with the code below? (mrs) */
2499 /* Finish up some magic for new'ed arrays */
2500 if (use_cookie && rval != NULL_TREE)
2502 tree extra = BI_header_size;
2504 rval = convert (string_type_node, rval); /* for ptr arithmetic */
2505 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra));
2506 /* Store header info. */
2507 cookie = build_indirect_ref (build (MINUS_EXPR,
2508 build_pointer_type (BI_header_type),
2509 rval, extra), NULL_PTR);
2510 exp1 = build (MODIFY_EXPR, void_type_node,
2511 build_component_ref (cookie, nc_nelts_field_id,
2514 TREE_SIDE_EFFECTS (exp1) = 1;
2515 rval = cp_convert (build_pointer_type (true_type), rval);
2516 rval = build_compound_expr
2517 (expr_tree_cons (NULL_TREE, exp1,
2518 build_expr_list (NULL_TREE, rval)));
2521 if (rval == error_mark_node)
2522 return error_mark_node;
2524 /* Don't call any constructors or do any initialization. */
2525 if (init == void_type_node)
2528 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
2530 if (! TYPE_NEEDS_CONSTRUCTING (type)
2531 && ! IS_AGGR_TYPE (type) && ! has_array)
2533 /* We are processing something like `new int (10)', which
2534 means allocate an int, and initialize it with 10. */
2538 /* At present RVAL is a temporary variable, created to hold
2539 the value from the call to `operator new'. We transform
2540 it to (*RVAL = INIT, RVAL). */
2541 rval = save_expr (rval);
2542 deref = build_indirect_ref (rval, NULL_PTR);
2544 /* Even for something like `new const int (10)' we must
2545 allow the expression to be non-const while we do the
2547 deref_type = TREE_TYPE (deref);
2548 if (CP_TYPE_CONST_P (deref_type))
2550 = cp_build_qualified_type (deref_type,
2551 CP_TYPE_QUALS (deref_type)
2552 & ~TYPE_QUAL_CONST);
2553 TREE_READONLY (deref) = 0;
2555 if (TREE_CHAIN (init) != NULL_TREE)
2556 pedwarn ("initializer list being treated as compound expression");
2557 else if (TREE_CODE (init) == CONSTRUCTOR)
2559 pedwarn ("initializer list appears where operand should be used");
2560 init = TREE_OPERAND (init, 1);
2562 init = build_compound_expr (init);
2564 init = convert_for_initialization (deref, type, init, LOOKUP_NORMAL,
2565 "new", NULL_TREE, 0);
2566 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
2567 build_modify_expr (deref, NOP_EXPR, init),
2569 TREE_NO_UNUSED_WARNING (rval) = 1;
2570 TREE_SIDE_EFFECTS (rval) = 1;
2572 else if (! has_array)
2575 /* Constructors are never virtual. If it has an initialization, we
2576 need to complain if we aren't allowed to use the ctor that took
2578 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
2580 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
2582 if (TYPE_USES_PVBASES (true_type)
2583 && !flag_vtable_thunks_compat)
2585 init = expr_tree_cons (NULL_TREE, vlist_zero_node, init);
2586 flags |= LOOKUP_HAS_VLIST;
2588 init = expr_tree_cons (NULL_TREE, integer_one_node, init);
2589 flags |= LOOKUP_HAS_IN_CHARGE;
2593 rval = save_expr (rval);
2596 if (newrval && TREE_CODE (TREE_TYPE (newrval)) == POINTER_TYPE)
2597 newrval = build_indirect_ref (newrval, NULL_PTR);
2599 newrval = build_method_call (newrval, ctor_identifier,
2600 init, TYPE_BINFO (true_type), flags);
2602 if (newrval == NULL_TREE || newrval == error_mark_node)
2603 return error_mark_node;
2605 /* Java constructors compiled by jc1 do not return this. */
2607 newrval = build (COMPOUND_EXPR, TREE_TYPE (newrval),
2610 TREE_HAS_CONSTRUCTOR (rval) = 1;
2613 rval = build (VEC_INIT_EXPR, TREE_TYPE (rval),
2614 save_expr (rval), init, nelts);
2616 /* If any part of the object initialization terminates by throwing an
2617 exception and a suitable deallocation function can be found, the
2618 deallocation function is called to free the memory in which the
2619 object was being constructed, after which the exception continues
2620 to propagate in the context of the new-expression. If no
2621 unambiguous matching deallocation function can be found,
2622 propagating the exception does not cause the object's memory to be
2624 if (flag_exceptions && alloc_expr && ! use_java_new)
2626 enum tree_code dcode = has_array ? VEC_DELETE_EXPR : DELETE_EXPR;
2627 tree cleanup, fn = NULL_TREE;
2628 int flags = LOOKUP_NORMAL | (use_global_new * LOOKUP_GLOBAL);
2630 /* All cleanups must last longer than normal. */
2631 int yes = suspend_momentary ();
2635 flags |= LOOKUP_SPECULATIVELY;
2637 /* We expect alloc_expr to look like a TARGET_EXPR around
2638 a NOP_EXPR around the CALL_EXPR we want. */
2639 fn = TREE_OPERAND (alloc_expr, 1);
2640 fn = TREE_OPERAND (fn, 0);
2643 /* Copy size to the saveable obstack. */
2644 size = mapcar (size, permanent_p);
2646 cleanup = build_op_delete_call (dcode, alloc_node, size, flags, fn);
2648 resume_momentary (yes);
2650 /* Ack! First we allocate the memory. Then we set our sentry
2651 variable to true, and expand a cleanup that deletes the memory
2652 if sentry is true. Then we run the constructor and store the
2653 returned pointer in buf. Then we clear sentry and return buf. */
2657 tree end, sentry, begin, buf, t = TREE_TYPE (rval);
2659 begin = get_target_expr (boolean_true_node);
2660 sentry = TREE_OPERAND (begin, 0);
2662 yes = suspend_momentary ();
2663 TREE_OPERAND (begin, 2)
2664 = build (COND_EXPR, void_type_node, sentry,
2665 cleanup, void_zero_node);
2666 resume_momentary (yes);
2668 rval = get_target_expr (rval);
2670 end = build (MODIFY_EXPR, TREE_TYPE (sentry),
2671 sentry, boolean_false_node);
2672 TREE_SIDE_EFFECTS (end) = 1;
2674 buf = TREE_OPERAND (rval, 0);
2676 rval = build (COMPOUND_EXPR, t, begin,
2677 build (COMPOUND_EXPR, t, rval,
2678 build (COMPOUND_EXPR, t, end, buf)));
2682 else if (CP_TYPE_CONST_P (true_type))
2683 cp_error ("uninitialized const in `new' of `%#T'", true_type);
2687 if (alloc_expr && rval == alloc_node)
2689 rval = TREE_OPERAND (alloc_expr, 1);
2690 alloc_expr = NULL_TREE;
2693 if (check_new && alloc_expr)
2695 /* Did we modify the storage? */
2696 tree ifexp = build_binary_op (NE_EXPR, alloc_node,
2698 rval = build_conditional_expr (ifexp, rval, alloc_node);
2702 rval = build (COMPOUND_EXPR, TREE_TYPE (rval), alloc_expr, rval);
2704 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
2706 /* The type of new int [3][3] is not int *, but int [3] * */
2707 rval = build_c_cast (build_pointer_type (type), rval);
2714 build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,
2716 tree base, maxindex, type;
2717 tree auto_delete_vec, auto_delete;
2718 int use_global_delete;
2721 tree ptype = build_pointer_type (type = complete_type (type));
2722 tree size_exp = size_in_bytes (type);
2724 /* Temporary variables used by the loop. */
2725 tree tbase, tbase_init;
2727 /* This is the body of the loop that implements the deletion of a
2728 single element, and moves temp variables to next elements. */
2731 /* This is the LOOP_EXPR that governs the deletion of the elements. */
2734 /* This is the thing that governs what to do after the loop has run. */
2735 tree deallocate_expr = 0;
2737 /* This is the BIND_EXPR which holds the outermost iterator of the
2738 loop. It is convenient to set this variable up and test it before
2739 executing any other code in the loop.
2740 This is also the containing expression returned by this function. */
2741 tree controller = NULL_TREE;
2743 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
2745 loop = integer_zero_node;
2749 /* The below is short by BI_header_size */
2750 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2752 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
2753 tbase_init = build_modify_expr (tbase, NOP_EXPR,
2754 fold (build (PLUS_EXPR, ptype,
2757 DECL_REGISTER (tbase) = 1;
2758 controller = build (BIND_EXPR, void_type_node, tbase, NULL_TREE, NULL_TREE);
2759 TREE_SIDE_EFFECTS (controller) = 1;
2761 if (auto_delete != integer_zero_node
2762 && auto_delete != integer_two_node)
2764 tree base_tbd = cp_convert (ptype,
2765 build_binary_op (MINUS_EXPR,
2766 cp_convert (ptr_type_node, base),
2768 /* This is the real size */
2769 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2770 body = build_expr_list (NULL_TREE,
2771 build_x_delete (base_tbd,
2772 2 | use_global_delete,
2774 body = build (COND_EXPR, void_type_node,
2775 build (BIT_AND_EXPR, integer_type_node,
2776 auto_delete, integer_one_node),
2777 body, integer_zero_node);
2782 body = expr_tree_cons (NULL_TREE,
2783 build_delete (ptype, tbase, auto_delete,
2784 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
2787 body = expr_tree_cons (NULL_TREE,
2788 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
2791 body = expr_tree_cons (NULL_TREE,
2792 build (EXIT_EXPR, void_type_node,
2793 build (EQ_EXPR, boolean_type_node, base, tbase)),
2796 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
2798 loop = expr_tree_cons (NULL_TREE, tbase_init,
2799 expr_tree_cons (NULL_TREE, loop, NULL_TREE));
2800 loop = build_compound_expr (loop);
2803 /* If the delete flag is one, or anything else with the low bit set,
2804 delete the storage. */
2805 if (auto_delete_vec == integer_zero_node)
2806 deallocate_expr = integer_zero_node;
2811 /* The below is short by BI_header_size */
2812 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
2814 if (! TYPE_VEC_NEW_USES_COOKIE (type))
2819 base_tbd = cp_convert (ptype,
2820 build_binary_op (MINUS_EXPR,
2821 cp_convert (string_type_node, base),
2823 /* True size with header. */
2824 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
2826 deallocate_expr = build_x_delete (base_tbd,
2827 2 | use_global_delete,
2829 if (auto_delete_vec != integer_one_node)
2830 deallocate_expr = build (COND_EXPR, void_type_node,
2831 build (BIT_AND_EXPR, integer_type_node,
2832 auto_delete_vec, integer_one_node),
2833 deallocate_expr, integer_zero_node);
2836 if (loop && deallocate_expr != integer_zero_node)
2838 body = expr_tree_cons (NULL_TREE, loop,
2839 expr_tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
2840 body = build_compound_expr (body);
2845 /* Outermost wrapper: If pointer is null, punt. */
2846 body = build (COND_EXPR, void_type_node,
2847 build (NE_EXPR, boolean_type_node, base, integer_zero_node),
2848 body, integer_zero_node);
2849 body = build1 (NOP_EXPR, void_type_node, body);
2853 TREE_OPERAND (controller, 1) = body;
2857 return cp_convert (void_type_node, body);
2860 /* Protect the vector initialization with a try-block so that we can
2861 destroy the first few elements if constructing a later element
2862 causes an exception to be thrown. TYPE is the type of the array
2866 expand_vec_init_try_block (type)
2869 if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions)
2872 /* The code we generate looks like:
2875 // Initialize the vector.
2877 // Destory the elements that need destroying.
2881 Here we're just beginning the `try'. */
2883 expand_eh_region_start ();
2886 /* Add code to destroy the array elements constructed so far if the
2887 construction of some element in the array causes an exception to be
2888 thrown. RVAL is the address of the last element in the array.
2889 TYPE is the type of the array elements. MAXINDEX is the maximum
2890 allowable index into the array. ITERATOR is an integer variable
2891 indicating how many elements remain to be constructed. */
2894 expand_vec_init_catch_clause (rval, type, maxindex, iterator)
2903 if (!TYPE_NEEDS_DESTRUCTOR (type) || !flag_exceptions)
2906 /* We have to ensure that this can live to the cleanup expansion
2907 time, since we know it is only ever needed once, generate code
2909 push_obstacks_nochange ();
2910 resume_temporary_allocation ();
2912 cleanup = make_node (RTL_EXPR);
2913 TREE_TYPE (cleanup) = void_type_node;
2914 RTL_EXPR_RTL (cleanup) = const0_rtx;
2915 TREE_SIDE_EFFECTS (cleanup) = 1;
2916 do_pending_stack_adjust ();
2917 start_sequence_for_rtl_expr (cleanup);
2919 e = build_vec_delete_1 (rval,
2920 build_binary_op (MINUS_EXPR, maxindex,
2923 /*auto_delete_vec=*/integer_zero_node,
2924 /*auto_delete=*/integer_zero_node,
2925 /*use_global_delete=*/0);
2926 expand_expr (e, const0_rtx, VOIDmode, EXPAND_NORMAL);
2928 do_pending_stack_adjust ();
2929 RTL_EXPR_SEQUENCE (cleanup) = get_insns ();
2931 cleanup = protect_with_terminate (cleanup);
2932 expand_eh_region_end (cleanup);
2936 /* `expand_vec_init' performs initialization of a vector of aggregate
2939 DECL is passed only for error reporting, and provides line number
2940 and source file name information.
2941 BASE is the space where the vector will be.
2942 MAXINDEX is the maximum index of the array (one less than the
2943 number of elements).
2944 INIT is the (possibly NULL) initializer.
2946 FROM_ARRAY is 0 if we should init everything with INIT
2947 (i.e., every element initialized from INIT).
2948 FROM_ARRAY is 1 if we should index into INIT in parallel
2949 with initialization of DECL.
2950 FROM_ARRAY is 2 if we should index into INIT in parallel,
2951 but use assignment instead of initialization. */
2954 expand_vec_init (decl, base, maxindex, init, from_array)
2955 tree decl, base, maxindex, init;
2959 tree base2 = NULL_TREE;
2960 tree type = TREE_TYPE (TREE_TYPE (base));
2962 tree itype = NULL_TREE;
2964 int num_initialized_elts = 0;
2966 maxindex = cp_convert (ptrdiff_type_node, maxindex);
2967 if (maxindex == error_mark_node)
2968 return error_mark_node;
2970 if (current_function_decl == NULL_TREE)
2972 rval = make_tree_vec (3);
2973 TREE_VEC_ELT (rval, 0) = base;
2974 TREE_VEC_ELT (rval, 1) = maxindex;
2975 TREE_VEC_ELT (rval, 2) = init;
2979 size = size_in_bytes (type);
2981 base = default_conversion (base);
2982 base = cp_convert (build_pointer_type (type), base);
2983 rval = get_temp_regvar (build_pointer_type (type), base);
2984 base = get_temp_regvar (build_pointer_type (type), base);
2985 iterator = get_temp_regvar (ptrdiff_type_node, maxindex);
2987 /* Protect the entire array initialization so that we can destroy
2988 the partially constructed array if an exception is thrown. */
2989 expand_vec_init_try_block (type);
2991 if (init != NULL_TREE && TREE_CODE (init) == CONSTRUCTOR
2992 && (!decl || same_type_p (TREE_TYPE (init), TREE_TYPE (decl))))
2994 /* Do non-default initialization resulting from brace-enclosed
2998 tree baseref = build1 (INDIRECT_REF, type, base);
3002 for (elts = CONSTRUCTOR_ELTS (init); elts; elts = TREE_CHAIN (elts))
3004 tree elt = TREE_VALUE (elts);
3006 num_initialized_elts++;
3008 if (IS_AGGR_TYPE (type) || TREE_CODE (type) == ARRAY_TYPE)
3009 expand_aggr_init (baseref, elt, 0);
3011 expand_assignment (baseref, elt, 0, 0);
3013 expand_assignment (base,
3014 build (PLUS_EXPR, build_pointer_type (type),
3017 expand_assignment (iterator,
3018 build (MINUS_EXPR, ptrdiff_type_node,
3019 iterator, integer_one_node),
3023 /* Clear out INIT so that we don't get confused below. */
3027 use_variable (DECL_RTL (base));
3029 else if (from_array)
3031 /* If initializing one array from another, initialize element by
3032 element. We rely upon the below calls the do argument
3034 if (decl == NULL_TREE)
3036 sorry ("initialization of array from dissimilar array type");
3037 return error_mark_node;
3041 base2 = default_conversion (init);
3042 itype = TREE_TYPE (base2);
3043 base2 = get_temp_regvar (itype, base2);
3044 itype = TREE_TYPE (itype);
3046 else if (TYPE_LANG_SPECIFIC (type)
3047 && TYPE_NEEDS_CONSTRUCTING (type)
3048 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3050 error ("initializer ends prematurely");
3051 return error_mark_node;
3055 /* Now, default-initialize any remaining elements. We don't need to
3056 do that if a) the type does not need constructing, or b) we've
3057 already initialized all the elements.
3059 We do need to keep going if we're copying an array. */
3062 || (TYPE_NEEDS_CONSTRUCTING (type)
3063 && !(TREE_CODE (maxindex) == INTEGER_CST
3064 && num_initialized_elts == TREE_INT_CST_LOW (maxindex) + 1)))
3066 /* If the ITERATOR is equal to -1, then we don't have to loop;
3067 we've already initialized all the elements. */
3068 expand_start_cond (build (NE_EXPR, boolean_type_node,
3069 iterator, minus_one),
3072 /* Otherwise, loop through the elements. */
3073 expand_start_loop_continue_elsewhere (1);
3075 /* The initialization of each array element is a full-expression. */
3076 expand_start_target_temps ();
3080 tree to = build1 (INDIRECT_REF, type, base);
3084 from = build1 (INDIRECT_REF, itype, base2);
3088 if (from_array == 2)
3089 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3090 else if (TYPE_NEEDS_CONSTRUCTING (type))
3091 expand_aggr_init (to, from, 0);
3093 expand_assignment (to, from, 0, 0);
3095 my_friendly_abort (57);
3097 else if (TREE_CODE (type) == ARRAY_TYPE)
3100 sorry ("cannot initialize multi-dimensional array with initializer");
3101 expand_vec_init (decl,
3103 build_pointer_type (TREE_TYPE
3106 array_type_nelts (type), 0, 0);
3109 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3111 expand_assignment (base,
3112 build (PLUS_EXPR, build_pointer_type (type),
3115 expand_assignment (base2,
3116 build (PLUS_EXPR, build_pointer_type (type),
3117 base2, size), 0, 0);
3119 /* Cleanup any temporaries needed for the initial value. */
3120 expand_end_target_temps ();
3122 expand_loop_continue_here ();
3123 expand_exit_loop_if_false (0, build (NE_EXPR, boolean_type_node,
3124 build (PREDECREMENT_EXPR,
3132 use_variable (DECL_RTL (base));
3134 use_variable (DECL_RTL (base2));
3141 /* Make sure to cleanup any partially constructed elements. */
3142 expand_vec_init_catch_clause (rval, type, maxindex, iterator);
3146 use_variable (DECL_RTL (iterator));
3147 use_variable (DECL_RTL (rval));
3153 /* Free up storage of type TYPE, at address ADDR.
3155 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3158 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3159 used as the second argument to operator delete. It can include
3160 things like padding and magic size cookies. It has virtual in it,
3161 because if you have a base pointer and you delete through a virtual
3162 destructor, it should be the size of the dynamic object, not the
3163 static object, see Free Store 12.5 ANSI C++ WP.
3165 This does not call any destructors. */
3168 build_x_delete (addr, which_delete, virtual_size)
3173 int use_global_delete = which_delete & 1;
3174 int use_vec_delete = !!(which_delete & 2);
3175 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3176 int flags = LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL);
3178 return build_op_delete_call (code, addr, virtual_size, flags, NULL_TREE);
3181 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3182 ADDR is an expression which yields the store to be destroyed.
3183 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3184 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3185 virtual baseclasses.
3186 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3188 FLAGS is the logical disjunction of zero or more LOOKUP_
3189 flags. See cp-tree.h for more info.
3191 This function does not delete an object's virtual base classes. */
3194 build_delete (type, addr, auto_delete, flags, use_global_delete)
3198 int use_global_delete;
3204 if (addr == error_mark_node)
3205 return error_mark_node;
3207 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3208 set to `error_mark_node' before it gets properly cleaned up. */
3209 if (type == error_mark_node)
3210 return error_mark_node;
3212 type = TYPE_MAIN_VARIANT (type);
3214 if (TREE_CODE (type) == POINTER_TYPE)
3216 type = TYPE_MAIN_VARIANT (TREE_TYPE (type));
3217 if (type != void_type_node && !complete_type_or_else (type, addr))
3218 return error_mark_node;
3219 if (TREE_CODE (type) == ARRAY_TYPE)
3221 if (! IS_AGGR_TYPE (type))
3223 /* Call the builtin operator delete. */
3224 return build_builtin_delete_call (addr);
3226 if (TREE_SIDE_EFFECTS (addr))
3227 addr = save_expr (addr);
3229 /* throw away const and volatile on target type of addr */
3230 addr = convert_force (build_pointer_type (type), addr, 0);
3231 ref = build_indirect_ref (addr, NULL_PTR);
3233 else if (TREE_CODE (type) == ARRAY_TYPE)
3236 if (TREE_SIDE_EFFECTS (addr))
3237 addr = save_expr (addr);
3238 if (TYPE_DOMAIN (type) == NULL_TREE)
3240 error ("unknown array size in delete");
3241 return error_mark_node;
3243 return build_vec_delete (addr, array_type_nelts (type),
3244 auto_delete, integer_zero_node,
3249 /* Don't check PROTECT here; leave that decision to the
3250 destructor. If the destructor is accessible, call it,
3251 else report error. */
3252 addr = build_unary_op (ADDR_EXPR, addr, 0);
3253 if (TREE_SIDE_EFFECTS (addr))
3254 addr = save_expr (addr);
3256 if (TREE_CONSTANT (addr))
3257 addr = convert_pointer_to (type, addr);
3259 addr = convert_force (build_pointer_type (type), addr, 0);
3261 ref = build_indirect_ref (addr, NULL_PTR);
3264 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3266 if (! TYPE_NEEDS_DESTRUCTOR (type))
3268 if (auto_delete == integer_zero_node)
3269 return void_zero_node;
3271 return build_op_delete_call
3272 (DELETE_EXPR, addr, c_sizeof_nowarn (type),
3273 LOOKUP_NORMAL | (use_global_delete * LOOKUP_GLOBAL),
3277 /* Below, we will reverse the order in which these calls are made.
3278 If we have a destructor, then that destructor will take care
3279 of the base classes; otherwise, we must do that here. */
3280 if (TYPE_HAS_DESTRUCTOR (type))
3282 tree passed_auto_delete;
3283 tree do_delete = NULL_TREE;
3286 if (use_global_delete)
3288 tree cond = fold (build (BIT_AND_EXPR, integer_type_node,
3289 auto_delete, integer_one_node));
3290 tree call = build_builtin_delete_call (addr);
3292 cond = fold (build (COND_EXPR, void_type_node, cond,
3293 call, void_zero_node));
3294 if (cond != void_zero_node)
3297 passed_auto_delete = fold (build (BIT_AND_EXPR, integer_type_node,
3298 auto_delete, integer_two_node));
3301 passed_auto_delete = auto_delete;
3303 /* Maybe pass vlist pointer to destructor. */
3304 if (TYPE_USES_PVBASES (type))
3306 /* Pass vlist_zero even if in backwards compatibility mode,
3307 as the extra argument should not hurt if it is not used. */
3308 expr = build_expr_list (NULL_TREE, vlist_zero_node);
3309 flags |= LOOKUP_HAS_VLIST;
3314 expr = expr_tree_cons (NULL_TREE, passed_auto_delete, expr);
3316 expr = build_method_call (ref, dtor_identifier, expr,
3320 expr = build (COMPOUND_EXPR, void_type_node, expr, do_delete);
3322 if (flags & LOOKUP_DESTRUCTOR)
3323 /* Explicit destructor call; don't check for null pointer. */
3324 ifexp = integer_one_node;
3326 /* Handle deleting a null pointer. */
3327 ifexp = fold (build_binary_op (NE_EXPR, addr, integer_zero_node));
3329 if (ifexp != integer_one_node)
3330 expr = build (COND_EXPR, void_type_node,
3331 ifexp, expr, void_zero_node);
3337 /* We only get here from finish_function for a destructor. */
3338 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3339 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3340 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3341 tree exprstmt = NULL_TREE;
3342 tree parent_auto_delete = auto_delete;
3345 /* Set this again before we call anything, as we might get called
3347 TYPE_HAS_DESTRUCTOR (type) = 1;
3349 /* If we have member delete or vbases, we call delete in
3351 if (auto_delete == integer_zero_node)
3353 else if (base_binfo == NULL_TREE
3354 || ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3356 cond = build (COND_EXPR, void_type_node,
3357 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3358 build_builtin_delete_call (addr),
3365 exprstmt = build_expr_list (NULL_TREE, cond);
3368 && ! TREE_VIA_VIRTUAL (base_binfo)
3369 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3371 tree this_auto_delete;
3373 /* Should the base invoke delete? */
3374 if (BINFO_OFFSET_ZEROP (base_binfo))
3375 this_auto_delete = parent_auto_delete;
3377 this_auto_delete = integer_zero_node;
3379 expr = build_base_dtor_call (ref, base_binfo, this_auto_delete);
3380 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3383 /* Take care of the remaining baseclasses. */
3384 for (i = 1; i < n_baseclasses; i++)
3386 base_binfo = TREE_VEC_ELT (binfos, i);
3387 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3388 || TREE_VIA_VIRTUAL (base_binfo))
3391 expr = build_base_dtor_call (ref, base_binfo, integer_zero_node);
3393 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3396 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3398 if (TREE_CODE (member) != FIELD_DECL)
3400 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3402 tree this_member = build_component_ref (ref, DECL_NAME (member), NULL_TREE, 0);
3403 tree this_type = TREE_TYPE (member);
3404 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3405 exprstmt = expr_tree_cons (NULL_TREE, expr, exprstmt);
3410 return build_compound_expr (exprstmt);
3411 /* Virtual base classes make this function do nothing. */
3412 return void_zero_node;
3416 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3419 build_vbase_delete (type, decl)
3422 tree vbases = CLASSTYPE_VBASECLASSES (type);
3423 tree result = NULL_TREE;
3424 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3426 my_friendly_assert (addr != error_mark_node, 222);
3430 tree this_addr = convert_force (build_pointer_type (BINFO_TYPE (vbases)),
3432 result = expr_tree_cons (NULL_TREE,
3433 build_delete (TREE_TYPE (this_addr), this_addr,
3435 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3437 vbases = TREE_CHAIN (vbases);
3439 return build_compound_expr (nreverse (result));
3442 /* Build a C++ vector delete expression.
3443 MAXINDEX is the number of elements to be deleted.
3444 ELT_SIZE is the nominal size of each element in the vector.
3445 BASE is the expression that should yield the store to be deleted.
3446 This function expands (or synthesizes) these calls itself.
3447 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3448 AUTO_DELETE say whether each item in the container should be deallocated.
3450 This also calls delete for virtual baseclasses of elements of the vector.
3452 Update: MAXINDEX is no longer needed. The size can be extracted from the
3453 start of the vector for pointers, and from the type for arrays. We still
3454 use MAXINDEX for arrays because it happens to already have one of the
3455 values we'd have to extract. (We could use MAXINDEX with pointers to
3456 confirm the size, and trap if the numbers differ; not clear that it'd
3457 be worth bothering.) */
3460 build_vec_delete (base, maxindex, auto_delete_vec, auto_delete,
3462 tree base, maxindex;
3463 tree auto_delete_vec, auto_delete;
3464 int use_global_delete;
3468 if (TREE_CODE (base) == OFFSET_REF)
3469 base = resolve_offset_ref (base);
3471 type = TREE_TYPE (base);
3473 base = stabilize_reference (base);
3475 /* Since we can use base many times, save_expr it. */
3476 if (TREE_SIDE_EFFECTS (base))
3477 base = save_expr (base);
3479 if (TREE_CODE (type) == POINTER_TYPE)
3481 /* Step back one from start of vector, and read dimension. */
3482 tree cookie_addr = build (MINUS_EXPR, build_pointer_type (BI_header_type),
3483 base, BI_header_size);
3484 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3485 maxindex = build_component_ref (cookie, nc_nelts_field_id, NULL_TREE, 0);
3487 type = TREE_TYPE (type);
3488 while (TREE_CODE (type) == ARRAY_TYPE);
3490 else if (TREE_CODE (type) == ARRAY_TYPE)
3492 /* get the total number of things in the array, maxindex is a bad name */
3493 maxindex = array_type_nelts_total (type);
3494 while (TREE_CODE (type) == ARRAY_TYPE)
3495 type = TREE_TYPE (type);
3496 base = build_unary_op (ADDR_EXPR, base, 1);
3500 if (base != error_mark_node)
3501 error ("type to vector delete is neither pointer or array type");
3502 return error_mark_node;
3505 return build_vec_delete_1 (base, maxindex, type, auto_delete_vec, auto_delete,