1 /* Handle initialization things in C++.
2 Copyright (C) 1987, 1989, 1992, 1993, 1994 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
22 /* High-level class interface. */
33 /* In C++, structures with well-defined constructors are initialized by
34 those constructors, unasked. CURRENT_BASE_INIT_LIST
35 holds a list of stmts for a BASE_INIT term in the grammar.
36 This list has one element for each base class which must be
37 initialized. The list elements are [basename, init], with
38 type basetype. This allows the possibly anachronistic form
39 (assuming d : a, b, c) "d (int a) : c(a+5), b (a-4), a (a+3)"
40 where each successive term can be handed down the constructor
41 line. Perhaps this was not intended. */
42 tree current_base_init_list, current_member_init_list;
44 void emit_base_init ();
45 void check_base_init ();
46 static void expand_aggr_vbase_init ();
47 void expand_member_init ();
48 void expand_aggr_init ();
50 static void expand_aggr_init_1 ();
51 static void expand_recursive_init_1 ();
52 static void expand_recursive_init ();
53 static void expand_virtual_init PROTO((tree, tree, tree));
54 tree expand_vec_init ();
56 static void add_friend (), add_friends ();
58 /* Cache _builtin_new and _builtin_delete exprs. */
59 static tree BIN, BID, BIVN, BIVD;
61 /* Cache the identifier nodes for the two magic field of a new cookie. */
62 static tree nc_nelts_field_id;
64 static tree nc_ptr_2comp_field_id;
67 static tree minus_one;
69 /* Set up local variable for this file. MUST BE CALLED AFTER
70 INIT_DECL_PROCESSING. */
72 tree BI_header_type, BI_header_size;
74 void init_init_processing ()
78 /* Define implicit `operator new' and `operator delete' functions. */
79 BIN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) NEW_EXPR])));
80 TREE_USED (TREE_OPERAND (BIN, 0)) = 0;
81 BID = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) DELETE_EXPR])));
82 TREE_USED (TREE_OPERAND (BID, 0)) = 0;
83 BIVN = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_NEW_EXPR])));
84 TREE_USED (TREE_OPERAND (BIVN, 0)) = 0;
85 BIVD = default_conversion (get_first_fn (IDENTIFIER_GLOBAL_VALUE (ansi_opname[(int) VEC_DELETE_EXPR])));
86 TREE_USED (TREE_OPERAND (BIVD, 0)) = 0;
87 minus_one = build_int_2 (-1, -1);
89 /* Define the structure that holds header information for
90 arrays allocated via operator new. */
91 BI_header_type = make_lang_type (RECORD_TYPE);
92 nc_nelts_field_id = get_identifier ("nelts");
93 fields[0] = build_lang_field_decl (FIELD_DECL, nc_nelts_field_id, sizetype);
94 finish_builtin_type (BI_header_type, "__new_cookie", fields,
96 BI_header_size = size_in_bytes (BI_header_type);
99 /* Subroutine of emit_base_init. For BINFO, initialize all the
100 virtual function table pointers, except those that come from
101 virtual base classes. Initialize binfo's vtable pointer, if
102 INIT_SELF is true. CAN_ELIDE is true when we know that all virtual
103 function table pointers in all bases have been initialized already,
104 probably because their constructors have just be run. */
106 init_vtbl_ptrs (binfo, init_self, can_elide)
108 int init_self, can_elide;
111 tree binfos = BINFO_BASETYPES (binfo);
112 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
114 for (i = 0; i < n_baselinks; i++)
116 tree base_binfo = TREE_VEC_ELT (binfos, i);
117 int is_not_base_vtable =
118 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
119 if (! TREE_VIA_VIRTUAL (base_binfo))
120 init_vtbl_ptrs (base_binfo, is_not_base_vtable, can_elide);
123 /* Before turning this on, make sure it is correct. */
124 if (can_elide && ! BINFO_MODIFIED (binfo))
127 /* Should we use something besides CLASSTYPE_VFIELDS? */
128 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
130 tree base_ptr = convert_pointer_to_real (binfo, current_class_decl);
131 expand_virtual_init (binfo, binfo, base_ptr);
136 /* Subroutine of emit_base_init. */
138 perform_member_init (member, name, init, explicit)
139 tree member, name, init;
143 tree type = TREE_TYPE (member);
145 if (TYPE_NEEDS_CONSTRUCTING (type)
146 || (init && TYPE_HAS_CONSTRUCTOR (type)))
148 /* Since `init' is already a TREE_LIST on the current_member_init_list,
149 only build it into one if we aren't already a list. */
150 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
151 init = build_tree_list (NULL_TREE, init);
153 decl = build_component_ref (C_C_D, name, 0, explicit);
156 && TREE_CODE (type) == ARRAY_TYPE
158 && TREE_CHAIN (init) == NULL_TREE
159 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
161 /* Initialization of one array from another. */
162 expand_vec_init (TREE_OPERAND (decl, 1), decl,
163 array_type_nelts (type), TREE_VALUE (init), 1);
166 expand_aggr_init (decl, init, 0);
170 if (init == NULL_TREE)
174 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
175 member, current_class_type);
176 init = error_mark_node;
178 /* member traversal: note it leaves init NULL */
179 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
180 cp_pedwarn ("uninitialized reference member `%D'", member);
182 else if (TREE_CODE (init) == TREE_LIST)
184 /* There was an explicit member initialization. Do some
185 work in that case. */
186 if (TREE_CHAIN (init))
188 warning ("initializer list treated as compound expression");
189 init = build_compound_expr (init);
192 init = TREE_VALUE (init);
195 /* We only build this with a null init if we got it from the
196 current_member_init_list. */
197 if (init || explicit)
199 decl = build_component_ref (C_C_D, name, 0, explicit);
200 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
204 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
206 cplus_expand_start_try (1);
207 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
211 /* Subroutine of emit_member_init. */
216 tree x, member, name, field, init;
217 tree init_list = NULL_TREE;
218 tree fields_to_unmark = NULL_TREE;
221 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
224 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
226 /* If we cleared this out, then pay no attention to it. */
227 if (TREE_PURPOSE (x) == NULL_TREE)
229 name = TREE_PURPOSE (x);
232 field = (TREE_CODE (name) == COMPONENT_REF
233 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
235 /* Let's find out when this happens. */
236 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
237 field = IDENTIFIER_CLASS_VALUE (name);
240 /* If one member shadows another, get the outermost one. */
241 if (TREE_CODE (field) == TREE_LIST)
242 field = TREE_VALUE (field);
246 /* See if we already found an initializer for this field. */
249 if (DECL_NAME (field))
250 cp_error ("multiple initializations given for member `%D'",
255 init_list = chainon (init_list,
256 build_tree_list (name, TREE_VALUE (x)));
257 /* Make sure we won't try to work on this init again. */
258 TREE_PURPOSE (x) = NULL_TREE;
264 /* If we didn't find MEMBER in the list, create a dummy entry
265 so the two lists (INIT_LIST and the list of members) will be
268 init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE));
271 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
273 if (TREE_PURPOSE (x))
275 name = TREE_PURPOSE (x);
276 init = TREE_VALUE (x);
277 /* XXX: this may need the COMPONENT_REF operand 0 check if
278 it turns out we actually get them. */
279 field = IDENTIFIER_CLASS_VALUE (name);
281 /* If one member shadows another, get the outermost one. */
282 if (TREE_CODE (field) == TREE_LIST)
284 field = TREE_VALUE (field);
285 if (decl_type_context (field) != current_class_type)
286 cp_error ("field `%D' not in immediate context", field);
290 /* It turns out if you have an anonymous union in the
291 class, a member from it can end up not being on the
292 list of fields (rather, the type is), and therefore
293 won't be seen by the for loop above. */
295 /* The code in this for loop is derived from a general loop
296 which had this check in it. Theoretically, we've hit
297 every initialization for the list of members in T, so
298 we shouldn't have anything but these left in this list. */
299 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
302 if (TREE_HAS_CONSTRUCTOR (field))
304 if (DECL_NAME (field))
305 error ("multiple initializations given for member `%s'",
306 IDENTIFIER_POINTER (DECL_NAME (field)));
310 TREE_HAS_CONSTRUCTOR (field) = 1;
311 fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
313 perform_member_init (field, name, init, 1);
314 TREE_PURPOSE (x) = NULL_TREE;
318 /* Unmark fields which are initialized for the base class. */
319 while (fields_to_unmark)
321 TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
322 /* XXX is this a memory leak? */
323 fields_to_unmark = TREE_CHAIN (fields_to_unmark);
329 /* Perform whatever initializations have yet to be done on the base
330 class of the class variable. These actions are in the global
331 variable CURRENT_BASE_INIT_LIST. Such an action could be
332 NULL_TREE, meaning that the user has explicitly called the base
333 class constructor with no arguments.
335 If there is a need for a call to a constructor, we must surround
336 that call with a pushlevel/poplevel pair, since we are technically
337 at the PARM level of scope.
339 Argument IMMEDIATELY, if zero, forces a new sequence to be
340 generated to contain these new insns, so it can be emitted later.
341 This sequence is saved in the global variable BASE_INIT_INSNS.
342 Otherwise, the insns are emitted into the current sequence.
344 Note that emit_base_init does *not* initialize virtual base
345 classes. That is done specially, elsewhere. */
348 emit_base_init (t, immediately)
352 extern tree in_charge_identifier;
357 tree t_binfo = TYPE_BINFO (t);
358 tree binfos = BINFO_BASETYPES (t_binfo);
359 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
360 int have_init_list = 0, from_init_list;
364 do_pending_stack_adjust ();
368 if (write_symbols == NO_DEBUG)
369 /* As a matter of principle, `start_sequence' should do this. */
372 /* Always emit a line number note so we can step into constructors. */
373 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
374 DECL_SOURCE_LINE (current_function_decl));
376 /* In this case, we always need IN_CHARGE_NODE, because we have
377 to know whether to deallocate or not before exiting. */
378 if (flag_handle_exceptions == 2
379 && lookup_name (in_charge_identifier, 0) == NULL_TREE)
381 tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
383 store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
386 expand_decl (in_charge_node);
387 expand_decl_init (in_charge_node);
390 start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
391 for (pass = start; pass < 2; pass++)
393 tree vbase_init_list = NULL_TREE;
395 for (init_list = current_base_init_list; init_list;
396 init_list = TREE_CHAIN (init_list))
398 tree basename = TREE_PURPOSE (init_list);
400 tree init = TREE_VALUE (init_list);
402 if (basename == NULL_TREE)
404 /* Initializer for single base class. Must not
405 use multiple inheritance or this is ambiguous. */
406 switch (n_baseclasses)
409 error ("type `%s' does not have a base class to initialize",
410 IDENTIFIER_POINTER (current_class_name));
415 error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
418 binfo = TREE_VEC_ELT (binfos, 0);
420 else if (is_aggr_typedef (basename, 1))
422 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
423 if (binfo == NULL_TREE)
426 /* Virtual base classes are special cases. Their initializers
427 are recorded with this constructor, and they are used when
428 this constructor is the top-level constructor called. */
429 if (! TREE_VIA_VIRTUAL (binfo))
431 /* Otherwise, if it is not an immediate base class, complain. */
432 for (i = n_baseclasses-1; i >= 0; i--)
433 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
437 error ("type `%s' is not an immediate base class of type `%s'",
438 IDENTIFIER_POINTER (basename),
439 IDENTIFIER_POINTER (current_class_name));
447 /* The base initialization list goes up to the first
448 base class which can actually use it. */
452 char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
453 ? "cannot pass initialization up to class `%s'" : 0;
455 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
456 && BINFO_BASETYPES (binfo) != NULL_TREE
457 && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
459 /* ?? This should be fixed in RENO by forcing
460 default constructors to exist. */
461 SET_BINFO_BASEINIT_MARKED (binfo);
462 binfo = BINFO_BASETYPE (binfo, 0);
465 /* We used to give an error if this wasn't true, saying that
466 there's no constructor for the initialization of basename.
467 This turned out to be incorrect---it should use the
468 default constructor, since a user could try to initialize
469 the class in a derived class's base initializer list. */
470 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
475 error_with_aggr_type (binfo, msgp);
481 if (BINFO_BASEINIT_MARKED (binfo))
483 msgp = "class `%s' initializer already specified";
484 error (msgp, IDENTIFIER_POINTER (basename));
490 SET_BINFO_BASEINIT_MARKED (binfo);
491 if (TREE_VIA_VIRTUAL (binfo))
493 vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
500 else if (TREE_VIA_VIRTUAL (binfo))
503 member = convert_pointer_to (binfo, current_class_decl);
504 expand_aggr_init_1 (t_binfo, 0,
505 build_indirect_ref (member, NULL_PTR), init,
506 BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN);
507 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
509 cplus_expand_start_try (1);
510 push_exception_cleanup (member);
516 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
519 if (DECL_NAME (current_function_decl) == NULL_TREE
520 && TREE_CHAIN (first_arg) != NULL_TREE)
522 /* If there are virtual baseclasses without initialization
523 specified, and this is a default X(X&) constructor,
524 build the initialization list so that each virtual baseclass
525 of the new object is initialized from the virtual baseclass
526 of the incoming arg. */
527 tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
528 for (vbases = CLASSTYPE_VBASECLASSES (t);
529 vbases; vbases = TREE_CHAIN (vbases))
531 if (BINFO_BASEINIT_MARKED (vbases) == 0)
533 member = convert_pointer_to (vbases, init_arg);
534 if (member == init_arg)
535 member = TREE_CHAIN (first_arg);
537 TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
538 vbase_init_list = tree_cons (convert_from_reference (member),
539 vbases, vbase_init_list);
540 SET_BINFO_BASEINIT_MARKED (vbases);
544 expand_start_cond (first_arg, 0);
545 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
550 current_base_init_list = NULL_TREE;
552 /* Now, perform default initialization of all base classes which
553 have not yet been initialized, and unmark baseclasses which
554 have been initialized. */
555 for (i = 0; i < n_baseclasses; i++)
557 tree base = current_class_decl;
558 tree base_binfo = TREE_VEC_ELT (binfos, i);
560 if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
562 if (! TREE_VIA_VIRTUAL (base_binfo)
563 && ! BINFO_BASEINIT_MARKED (base_binfo))
567 if (BINFO_OFFSET_ZEROP (base_binfo))
568 base = build1 (NOP_EXPR,
569 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
572 base = build (PLUS_EXPR,
573 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
574 current_class_decl, BINFO_OFFSET (base_binfo));
576 ref = build_indirect_ref (base, NULL_PTR);
577 expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
578 BINFO_OFFSET_ZEROP (base_binfo),
580 if (flag_handle_exceptions == 2
581 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
583 cplus_expand_start_try (1);
584 push_exception_cleanup (base);
588 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
590 if (! TYPE_USES_VIRTUAL_BASECLASSES (t))
592 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))
593 && BINFO_BASETYPES (base_binfo) != NULL_TREE
594 && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1)
596 /* ?? This should be fixed in RENO by forcing
597 default constructors to exist. It is needed for symmetry
599 base_binfo = BINFO_BASETYPE (base_binfo, 0);
600 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
605 /* Initialize all the virtual function table fields that
606 do come from virtual base classes. */
607 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
608 expand_vbase_vtables_init (t_binfo, t_binfo,
609 C_C_D, current_class_decl, 0);
610 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
611 CLEAR_BINFO_BASEINIT_MARKED (vbases);
613 /* Initialize all the virtual function table fields that
614 do not come from virtual base classes. */
615 init_vtbl_ptrs (t_binfo, 0, 1);
617 if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (t))
618 expand_virtual_init (TYPE_BINFO (t), t, current_class_decl);
620 if (current_member_init_list)
622 init_list = sort_member_init (t);
626 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
631 /* See if we had a user-specified member initialization. */
634 if (TREE_PURPOSE (init_list))
636 name = TREE_PURPOSE (init_list);
637 init = TREE_VALUE (init_list);
640 if (TREE_STATIC (member))
642 error_with_aggr_type (DECL_FIELD_CONTEXT (member),
643 "field `%s::%s' is static; only point of initialization is its declaration",
644 IDENTIFIER_POINTER (TREE_PURPOSE (init_list)));
648 /* Also see if it's ever a COMPONENT_REF here. If it is, we
649 need to do `expand_assignment (name, init, 0, 0);' and
651 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
654 init_list = TREE_CHAIN (init_list);
657 if (! from_init_list)
659 /* member could be, for example, a CONST_DECL for an enumerated
660 tag; we don't want to try to initialize that, since it already
662 if (TREE_CODE (member) != FIELD_DECL)
665 name = DECL_NAME (member);
666 init = DECL_INITIAL (member);
669 perform_member_init (member, name, init, from_init_list);
672 current_member_init_list = NULL_TREE;
674 /* It is possible for the initializers to need cleanups.
675 Expand those cleanups now that all the initialization
677 expand_cleanups_to (NULL_TREE);
681 extern rtx base_init_insns;
683 do_pending_stack_adjust ();
684 my_friendly_assert (base_init_insns == 0, 207);
685 base_init_insns = get_insns ();
689 /* All the implicit try blocks we built up will be zapped
690 when we come to a real binding contour boundary. */
693 /* Check that all fields are properly initialized after
694 an assignment to `this'. */
700 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
701 if (DECL_NAME (member) && TREE_USED (member))
702 cp_error ("field `%D' used before initialized (after assignment to `this')",
706 /* This code sets up the virtual function tables appropriate for
707 the pointer DECL. It is a one-ply initialization.
709 BINFO is the exact type that DECL is supposed to be. In
710 multiple inheritance, this might mean "C's A" if C : A, B. */
712 expand_virtual_init (main_binfo, binfo, decl)
713 tree main_binfo, binfo;
718 tree vtype, vtype_binfo;
720 if (TREE_CODE (binfo) == TREE_VEC)
721 type = BINFO_TYPE (binfo);
722 else if (TREE_CODE (binfo) == RECORD_TYPE)
725 binfo = TYPE_BINFO (type);
728 my_friendly_abort (46);
730 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
731 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
733 /* This code suggests that it's time to rewrite how we handle
734 replicated baseclasses in G++. */
735 if (get_base_distance (vtype, TREE_TYPE (TREE_TYPE (decl)),
736 0, (tree *) 0) == -2)
738 tree binfos = TYPE_BINFO_BASETYPES (TREE_TYPE (TREE_TYPE (decl)));
739 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
741 for (i = n_baselinks-1; i >= 0; i--)
743 tree base_binfo = TREE_VEC_ELT (binfos, i);
746 if (get_base_distance (vtype, BINFO_TYPE (base_binfo), 0, 0) == -1)
749 if (TREE_VIA_VIRTUAL (base_binfo))
750 this_decl = build_vbase_pointer (build_indirect_ref (decl, NULL_PTR), BINFO_TYPE (base_binfo));
751 else if (BINFO_OFFSET_ZEROP (base_binfo))
752 this_decl = build1 (NOP_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
755 this_decl = build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
756 decl, BINFO_OFFSET (base_binfo));
757 expand_virtual_init (main_binfo, base_binfo, this_decl);
766 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
768 /* The below does not work when we have to step through the
769 vfield, on our way down to the most base class for the
771 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)),
772 BINFO_TYPE (main_binfo)));
775 my_friendly_assert (BINFO_TYPE (main_binfo) == BINFO_TYPE (binfo), 208);
776 vtbl = BINFO_VTABLE (main_binfo);
778 assemble_external (vtbl);
779 TREE_USED (vtbl) = 1;
780 vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
782 decl = convert_pointer_to_real (vtype_binfo, decl);
783 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
784 if (vtbl_ptr == error_mark_node)
787 /* Have to convert VTBL since array sizes may be different. */
788 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR,
789 convert (TREE_TYPE (vtbl_ptr), vtbl)));
792 /* Subroutine of `expand_aggr_vbase_init'.
793 BINFO is the binfo of the type that is being initialized.
794 INIT_LIST is the list of initializers for the virtual baseclass. */
796 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
797 tree binfo, exp, addr, init_list;
799 tree init = value_member (BINFO_TYPE (binfo), init_list);
800 tree ref = build_indirect_ref (addr, NULL_PTR);
802 init = TREE_PURPOSE (init);
803 /* Call constructors, but don't set up vtables. */
804 expand_aggr_init_1 (binfo, exp, ref, init, 0,
805 LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
806 CLEAR_BINFO_VBASE_INIT_MARKED (binfo);
809 /* Initialize this object's virtual base class pointers. This must be
810 done only at the top-level of the object being constructed.
812 INIT_LIST is list of initialization for constructor to perform. */
814 expand_aggr_vbase_init (binfo, exp, addr, init_list)
820 tree type = BINFO_TYPE (binfo);
822 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
824 tree result = init_vbase_pointers (type, addr);
828 expand_expr_stmt (build_compound_expr (result));
830 /* Mark everything as having an initializer
831 (either explicit or default). */
832 for (vbases = CLASSTYPE_VBASECLASSES (type);
833 vbases; vbases = TREE_CHAIN (vbases))
834 SET_BINFO_VBASE_INIT_MARKED (vbases);
836 /* First, initialize baseclasses which could be baseclasses
837 for other virtual baseclasses. */
838 for (vbases = CLASSTYPE_VBASECLASSES (type);
839 vbases; vbases = TREE_CHAIN (vbases))
840 /* Don't initialize twice. */
841 if (BINFO_VBASE_INIT_MARKED (vbases))
845 while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp)))
846 tmp = TREE_CHAIN (tmp);
847 expand_aggr_vbase_init_1 (vbases, exp,
848 TREE_OPERAND (TREE_VALUE (tmp), 0),
852 /* Now initialize the baseclasses which don't have virtual baseclasses. */
853 for (; result; result = TREE_CHAIN (result))
854 /* Don't initialize twice. */
855 if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result)))
857 my_friendly_abort (47);
858 expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
859 TREE_OPERAND (TREE_VALUE (result), 0),
865 /* Subroutine to perform parser actions for member initialization.
866 S_ID is the scoped identifier.
867 NAME is the name of the member.
868 INIT is the initializer, or `void_type_node' if none. */
870 do_member_init (s_id, name, init)
871 tree s_id, name, init;
875 if (current_class_type == NULL_TREE
876 || ! is_aggr_typedef (s_id, 1))
878 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
879 current_class_type, 1);
880 if (binfo == error_mark_node)
884 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
888 base = convert_pointer_to (binfo, current_class_decl);
889 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
892 /* Function to give error message if member initialization specification
893 is erroneous. FIELD is the member we decided to initialize.
894 TYPE is the type for which the initialization is being performed.
895 FIELD must be a member of TYPE, or the base type from which FIELD
896 comes must not need a constructor.
898 MEMBER_NAME is the name of the member. */
901 member_init_ok_or_else (field, type, member_name)
906 if (field == error_mark_node)
908 if (field == NULL_TREE)
910 cp_error ("class `%T' does not have any field named `%s'", type,
914 if (DECL_CONTEXT (field) != type
915 && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
917 cp_error ("member `%D' comes from base class needing constructor",
924 /* If NAME is a viable field name for the aggregate DECL,
925 and PARMS is a viable parameter list, then expand an _EXPR
926 which describes this initialization.
928 Note that we do not need to chase through the class's base classes
929 to look for NAME, because if it's in that list, it will be handled
930 by the constructor for that base class.
932 We do not yet have a fixed-point finder to instantiate types
933 being fed to overloaded constructors. If there is a unique
934 constructor, then argument types can be got from that one.
936 If INIT is non-NULL, then it the initialization should
937 be placed in `current_base_init_list', where it will be processed
938 by `emit_base_init'. */
940 expand_member_init (exp, name, init)
941 tree exp, name, init;
943 extern tree ptr_type_node; /* should be in tree.h */
945 tree basetype = NULL_TREE, field;
950 if (exp == NULL_TREE)
951 return; /* complain about this later */
953 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
955 if (name == NULL_TREE && IS_AGGR_TYPE (type))
956 switch (CLASSTYPE_N_BASECLASSES (type))
959 error ("base class initializer specified, but no base class to initialize");
962 basetype = TYPE_BINFO_BASETYPE (type, 0);
965 error ("initializer for unnamed base class ambiguous");
966 cp_error ("(type `%T' uses multiple inheritance)", type);
972 /* The grammar should not allow fields which have names
973 that are TYPENAMEs. Therefore, if the field has
974 a non-NULL TREE_TYPE, we may assume that this is an
975 attempt to initialize a base class member of the current
976 type. Otherwise, it is an attempt to initialize a
979 if (init == void_type_node)
982 if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
986 if (name == NULL_TREE)
990 name = TYPE_IDENTIFIER (basetype);
993 error ("no base class to initialize");
1000 basetype = IDENTIFIER_TYPE_VALUE (name);
1001 if (basetype != type
1002 && ! binfo_member (basetype, TYPE_BINFO (type))
1003 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
1005 if (IDENTIFIER_CLASS_VALUE (name))
1007 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1008 error ("type `%s' is not an immediate or virtual basetype for `%s'",
1009 IDENTIFIER_POINTER (name),
1010 TYPE_NAME_STRING (type));
1012 error ("type `%s' is not an immediate basetype for `%s'",
1013 IDENTIFIER_POINTER (name),
1014 TYPE_NAME_STRING (type));
1019 if (purpose_member (name, current_base_init_list))
1021 error ("base class `%s' already initialized",
1022 IDENTIFIER_POINTER (name));
1026 base_init = build_tree_list (name, init);
1027 TREE_TYPE (base_init) = basetype;
1028 current_base_init_list = chainon (current_base_init_list, base_init);
1035 field = lookup_field (type, name, 1, 0);
1037 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1040 if (purpose_member (name, current_member_init_list))
1042 error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
1046 member_init = build_tree_list (name, init);
1047 TREE_TYPE (member_init) = TREE_TYPE (field);
1048 current_member_init_list = chainon (current_member_init_list, member_init);
1052 else if (name == NULL_TREE)
1054 compiler_error ("expand_member_init: name == NULL_TREE");
1059 field = lookup_field (basetype, name, 0, 0);
1061 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1064 /* now see if there is a constructor for this type
1065 which will take these args. */
1067 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1069 tree parmtypes, fndecl;
1071 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1073 /* just know that we've seen something for this node */
1074 DECL_INITIAL (exp) = error_mark_node;
1075 TREE_USED (exp) = 1;
1077 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1078 actual_name = TYPE_IDENTIFIER (type);
1079 parm = build_component_ref (exp, name, 0, 0);
1081 /* Now get to the constructor. */
1082 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1083 /* Get past destructor, if any. */
1084 if (TYPE_HAS_DESTRUCTOR (type))
1085 fndecl = DECL_CHAIN (fndecl);
1088 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1090 /* If the field is unique, we can use the parameter
1091 types to guide possible type instantiation. */
1092 if (DECL_CHAIN (fndecl) == NULL_TREE)
1094 /* There was a confusion here between
1095 FIELD and FNDECL. The following code
1096 should be correct, but abort is here
1098 my_friendly_abort (48);
1099 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1103 parmtypes = NULL_TREE;
1107 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1108 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1109 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1113 if (rval != error_mark_node)
1115 /* Now, fill in the first parm with our guy */
1116 TREE_VALUE (TREE_OPERAND (rval, 1))
1117 = build_unary_op (ADDR_EXPR, parm, 0);
1118 TREE_TYPE (rval) = ptr_type_node;
1119 TREE_SIDE_EFFECTS (rval) = 1;
1122 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1124 parm = build_component_ref (exp, name, 0, 0);
1125 expand_aggr_init (parm, NULL_TREE, 0);
1126 rval = error_mark_node;
1129 /* Now initialize the member. It does not have to
1130 be of aggregate type to receive initialization. */
1131 if (rval != error_mark_node)
1132 expand_expr_stmt (rval);
1135 /* This is like `expand_member_init', only it stores one aggregate
1138 INIT comes in two flavors: it is either a value which
1139 is to be stored in EXP, or it is a parameter list
1140 to go to a constructor, which will operate on EXP.
1141 If `init' is a CONSTRUCTOR, then we emit a warning message,
1142 explaining that such initializations are illegal.
1144 ALIAS_THIS is nonzero iff we are initializing something which is
1145 essentially an alias for C_C_D. In this case, the base constructor
1146 may move it on us, and we must keep track of such deviations.
1148 If INIT resolves to a CALL_EXPR which happens to return
1149 something of the type we are looking for, then we know
1150 that we can safely use that call to perform the
1153 The virtual function table pointer cannot be set up here, because
1154 we do not really know its type.
1156 Virtual baseclass pointers are also set up here.
1158 This never calls operator=().
1160 When initializing, nothing is CONST.
1162 A default copy constructor may have to be used to perform the
1165 A constructor or a conversion operator may have to be used to
1166 perform the initialization, but not both, as it would be ambiguous.
1170 expand_aggr_init (exp, init, alias_this)
1174 tree type = TREE_TYPE (exp);
1175 int was_const = TREE_READONLY (exp);
1177 if (init == error_mark_node)
1180 TREE_READONLY (exp) = 0;
1182 if (TREE_CODE (type) == ARRAY_TYPE)
1184 /* Must arrange to initialize each element of EXP
1185 from elements of INIT. */
1186 int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1187 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1190 tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
1191 TYPE_DOMAIN (type));
1192 if (init && (TREE_TYPE (exp) == TREE_TYPE (init)))
1193 TREE_TYPE (init) = atype;
1194 TREE_TYPE (exp) = atype;
1196 if (init && TREE_TYPE (init) == NULL_TREE)
1198 /* Handle bad initializers like:
1202 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1206 int main(int argc, char **argv) {
1207 COMPLEX zees(1.0, 0.0)[10];
1210 error ("bad array initializer");
1213 expand_vec_init (exp, exp, array_type_nelts (type), init,
1214 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1215 TREE_READONLY (exp) = was_const;
1216 TREE_TYPE (exp) = type;
1217 if (init) TREE_TYPE (init) = itype;
1221 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1222 /* just know that we've seen something for this node */
1223 TREE_USED (exp) = 1;
1226 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1227 constructor as parameters to an implicit GNU C++ constructor. */
1228 if (init && TREE_CODE (init) == CONSTRUCTOR
1229 && TYPE_HAS_CONSTRUCTOR (type)
1230 && TREE_TYPE (init) == type)
1231 init = CONSTRUCTOR_ELTS (init);
1233 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1234 init, alias_this, LOOKUP_NORMAL);
1235 TREE_READONLY (exp) = was_const;
1239 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1247 /* It fails because there may not be a constructor which takes
1248 its own type as the first (or only parameter), but which does
1249 take other types via a conversion. So, if the thing initializing
1250 the expression is a unit element of type X, first try X(X&),
1251 followed by initialization by X. If neither of these work
1252 out, then look hard. */
1255 int xxref_init_possible;
1257 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1260 if (parms) init = TREE_VALUE (parms);
1262 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
1264 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1265 expand_expr_stmt (rval);
1269 parms = build_tree_list (NULL_TREE, init);
1271 if (TYPE_HAS_INIT_REF (type)
1272 || init == NULL_TREE
1273 || TREE_CHAIN (parms) != NULL_TREE)
1274 xxref_init_possible = 0;
1277 xxref_init_possible = LOOKUP_SPECULATIVELY;
1278 flags &= ~LOOKUP_COMPLAIN;
1281 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1283 if (true_exp == exp)
1284 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1286 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1287 flags |= LOOKUP_HAS_IN_CHARGE;
1290 rval = build_method_call (exp, constructor_name_full (type),
1291 parms, binfo, flags|xxref_init_possible);
1292 if (rval == NULL_TREE && xxref_init_possible)
1294 /* It is an error to implement a default copy constructor if
1295 (see ARM 12.8 for details) ... one case being if another
1296 copy constructor already exists. */
1297 tree init_type = TREE_TYPE (init);
1298 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1299 init_type = TREE_TYPE (init_type);
1300 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
1301 || (IS_AGGR_TYPE (init_type)
1302 && UNIQUELY_DERIVED_FROM_P (type, init_type)))
1304 if (type == BINFO_TYPE (binfo)
1305 && TYPE_USES_VIRTUAL_BASECLASSES (type))
1307 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1308 expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE);
1310 expand_vbase_vtables_init (binfo, binfo, exp, addr, 1);
1312 expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1316 rval = build_method_call (exp, constructor_name_full (type), parms,
1320 /* Private, protected, or otherwise unavailable. */
1321 if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1322 cp_error ("in base initialization for class `%T'", binfo);
1323 /* A valid initialization using constructor. */
1324 else if (rval != error_mark_node && rval != NULL_TREE)
1326 /* p. 222: if the base class assigns to `this', then that
1327 value is used in the derived class. */
1328 if ((flag_this_is_variable & 1) && alias_this)
1330 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1331 expand_assignment (current_class_decl, rval, 0, 0);
1334 expand_expr_stmt (rval);
1336 else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1338 /* If we are initializing one aggregate value
1339 from another, and though there are constructors,
1340 and none accept the initializer, just do a bitwise
1343 The above sounds wrong, ``If a class has any copy
1344 constructor defined, the default copy constructor will
1345 not be generated.'' 12.8 Copying Class Objects (mrs)
1347 @@ This should reject initializer which a constructor
1348 @@ rejected on access gounds, but there is
1349 @@ no way right now to recognize that case with
1350 @@ just `error_mark_node'. */
1352 init = TREE_VALUE (parms);
1353 itype = TREE_TYPE (init);
1354 if (TREE_CODE (itype) == REFERENCE_TYPE)
1356 init = convert_from_reference (init);
1357 itype = TREE_TYPE (init);
1359 itype = TYPE_MAIN_VARIANT (itype);
1361 /* This is currently how the default X(X&) constructor
1363 if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1366 warning ("bitwise copy in initialization of type `%s'",
1367 TYPE_NAME_STRING (type));
1369 rval = build (INIT_EXPR, type, exp, init);
1370 expand_expr_stmt (rval);
1374 cp_error ("in base initialization for class `%T',", binfo);
1375 cp_error ("invalid initializer to constructor for type `%T'", type);
1381 if (init == NULL_TREE)
1382 my_friendly_assert (parms == NULL_TREE, 210);
1383 if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
1384 cp_error ("virtual baseclass `%T' does not have default initializer", binfo);
1387 cp_error ("in base initialization for class `%T',", binfo);
1388 /* This will make an error message for us. */
1389 build_method_call (exp, constructor_name_full (type), parms, binfo,
1390 (TYPE_USES_VIRTUAL_BASECLASSES (type)
1391 ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1396 /* Constructor has been called, but vtables may be for TYPE
1397 rather than for FOR_TYPE. */
1400 /* This function is responsible for initializing EXP with INIT
1403 BINFO is the binfo of the type for who we are performing the
1404 initialization. For example, if W is a virtual base class of A and B,
1406 If we are initializing B, then W must contain B's W vtable, whereas
1407 were we initializing C, W must contain C's W vtable.
1409 TRUE_EXP is nonzero if it is the true expression being initialized.
1410 In this case, it may be EXP, or may just contain EXP. The reason we
1411 need this is because if EXP is a base element of TRUE_EXP, we
1412 don't necessarily know by looking at EXP where its virtual
1413 baseclass fields should really be pointing. But we do know
1414 from TRUE_EXP. In constructors, we don't know anything about
1415 the value being initialized.
1417 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1419 FLAGS is just passes to `build_method_call'. See that function for
1423 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1430 tree type = TREE_TYPE (exp);
1431 tree init_type = NULL_TREE;
1433 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1435 /* Use a function returning the desired type to initialize EXP for us.
1436 If the function is a constructor, and its first argument is
1437 NULL_TREE, know that it was meant for us--just slide exp on
1438 in and expand the constructor. Constructors now come
1442 tree init_list = NULL_TREE;
1444 if (TREE_CODE (init) == TREE_LIST)
1447 if (TREE_CHAIN (init) == NULL_TREE)
1448 init = TREE_VALUE (init);
1451 init_type = TREE_TYPE (init);
1453 if (TREE_CODE (init) != TREE_LIST)
1455 if (TREE_CODE (init_type) == ERROR_MARK)
1459 /* These lines are found troublesome 5/11/89. */
1460 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1461 init_type = TREE_TYPE (init_type);
1464 /* This happens when we use C++'s functional cast notation.
1465 If the types match, then just use the TARGET_EXPR
1466 directly. Otherwise, we need to create the initializer
1467 separately from the object being initialized. */
1468 if (TREE_CODE (init) == TARGET_EXPR)
1470 if (init_type == type)
1472 if (TREE_CODE (exp) == VAR_DECL
1473 || TREE_CODE (exp) == RESULT_DECL)
1474 /* Unify the initialization targets. */
1475 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1477 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1479 expand_expr_stmt (init);
1484 init = TREE_OPERAND (init, 1);
1485 init = build (CALL_EXPR, init_type,
1486 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1487 TREE_SIDE_EFFECTS (init) = 1;
1489 TREE_RAISES (init) = ??
1492 TREE_VALUE (init_list) = init;
1496 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1498 /* It is legal to directly initialize from a CALL_EXPR
1499 without going through X(X&), apparently. */
1500 && ! TYPE_GETS_INIT_REF (type)
1504 /* A CALL_EXPR is a legitimate form of initialization, so
1505 we should not print this warning message. */
1507 /* Should have gone away due to 5/11/89 change. */
1508 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1509 init = convert_from_reference (init);
1511 expand_assignment (exp, init, 0, 0);
1512 if (exp == DECL_RESULT (current_function_decl))
1514 /* Failing this assertion means that the return value
1515 from receives multiple initializations. */
1516 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1517 || DECL_INITIAL (exp) == error_mark_node,
1519 DECL_INITIAL (exp) = init;
1523 else if (init_type == type
1524 && TREE_CODE (init) == COND_EXPR)
1526 /* Push value to be initialized into the cond, where possible.
1527 Avoid spurious warning messages when initializing the
1528 result of this function. */
1529 TREE_OPERAND (init, 1)
1530 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1531 if (exp == DECL_RESULT (current_function_decl))
1532 DECL_INITIAL (exp) = NULL_TREE;
1533 TREE_OPERAND (init, 2)
1534 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1535 if (exp == DECL_RESULT (current_function_decl))
1536 DECL_INITIAL (exp) = init;
1537 TREE_SIDE_EFFECTS (init) = 1;
1538 expand_expr (init, const0_rtx, VOIDmode, 0);
1544 /* We did not know what we were initializing before. Now we do. */
1545 if (TREE_CODE (init) == TARGET_EXPR)
1547 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1549 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1550 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1552 /* In order for this to work for RESULT_DECLs, if their
1553 type has a constructor, then they must be BLKmode
1554 so that they will be meaningfully addressable. */
1555 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1556 init = TREE_OPERAND (init, 1);
1557 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1558 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1559 TREE_SIDE_EFFECTS (init) = 1;
1561 TREE_RAISES (init) = ??
1563 TREE_VALUE (TREE_OPERAND (init, 1))
1564 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1568 expand_assignment (current_function_decl, init, 0, 0);
1571 if (exp == DECL_RESULT (current_function_decl))
1573 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1574 fatal ("return value from function receives multiple initializations");
1575 DECL_INITIAL (exp) = init;
1577 expand_expr_stmt (init);
1582 if (TREE_CODE (exp) == VAR_DECL
1583 && TREE_CODE (init) == CONSTRUCTOR
1584 && TREE_HAS_CONSTRUCTOR (init))
1586 tree t = store_init_value (exp, init);
1589 expand_decl_init (exp);
1592 t = build (INIT_EXPR, type, exp, init);
1593 TREE_SIDE_EFFECTS (t) = 1;
1594 expand_expr_stmt (t);
1598 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1599 which really means: xyzzy foo = bar; Ugh!
1601 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1603 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1605 if (init_list && TREE_CHAIN (init_list))
1607 warning ("initializer list being treated as compound expression");
1608 init = convert (type, build_compound_expr (init_list));
1609 if (init == error_mark_node)
1613 expand_assignment (exp, init, 0, 0);
1617 /* See whether we can go through a type conversion operator.
1618 This wins over going through a non-existent constructor. If
1619 there is a constructor, it is ambiguous. */
1620 if (TREE_CODE (init) != TREE_LIST)
1622 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1623 ? TREE_TYPE (init_type) : init_type;
1625 if (ttype != type && IS_AGGR_TYPE (ttype))
1627 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1631 /* See if there is a constructor for``type'' that takes a
1632 ``ttype''-typed object. */
1633 tree parms = build_tree_list (NULL_TREE, init);
1634 tree as_cons = NULL_TREE;
1635 if (TYPE_HAS_CONSTRUCTOR (type))
1636 as_cons = build_method_call (exp, constructor_name_full (type),
1638 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1639 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1640 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1641 cp_error ("ambiguity between conversion to `%T' and constructor",
1644 expand_assignment (exp, rval, 0, 0);
1651 /* Handle default copy constructors here, does not matter if there is
1652 a constructor or not. */
1653 if (type == init_type && IS_AGGR_TYPE (type)
1654 && init && TREE_CODE (init) != TREE_LIST)
1655 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1656 /* Not sure why this is here... */
1657 else if (TYPE_HAS_CONSTRUCTOR (type))
1658 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1659 else if (TREE_CODE (type) == ARRAY_TYPE)
1661 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1662 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1663 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1664 sorry ("arrays of objects with virtual functions but no constructors");
1667 expand_recursive_init (binfo, true_exp, exp, init,
1668 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1671 /* A pointer which holds the initializer. First call to
1672 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1673 static tree *init_ptr, init_null;
1675 /* Subroutine of expand_recursive_init:
1677 ADDR is the address of the expression being initialized.
1678 INIT_LIST is the cons-list of initializations to be performed.
1679 ALIAS_THIS is its same, lovable self. */
1681 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1682 tree binfo, true_exp, addr;
1688 if (TREE_PURPOSE (init_list))
1690 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1692 tree member = TREE_PURPOSE (init_list);
1693 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1694 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1695 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1696 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1697 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1698 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1700 member_base = save_expr (default_conversion (member_base));
1701 expand_vec_init (member, member_base,
1702 array_type_nelts (TREE_TYPE (member)),
1703 DECL_INITIAL (member), 0);
1706 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1708 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1710 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1711 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1713 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1715 /* Only initialize the virtual function tables if we
1716 are initializing the ultimate users of those vtables. */
1717 if (TREE_VALUE (init_list))
1719 /* We have to ensure that the second argment to
1720 expand_virtual_init is in binfo's hierarchy. */
1721 expand_virtual_init (binfo,
1722 get_binfo (TREE_VALUE (init_list), binfo, 0),
1724 if (TREE_VALUE (init_list) == binfo
1725 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1726 expand_vbase_vtables_init (binfo,binfo, true_exp, addr, 1);
1730 my_friendly_abort (49);
1732 else if (TREE_VALUE (init_list)
1733 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1735 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1736 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1737 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1740 /* INIT_PTR is used up. */
1741 init_ptr = &init_null;
1744 my_friendly_abort (50);
1745 init_list = TREE_CHAIN (init_list);
1749 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1750 but it has a baseclass with a constructor or a virtual function
1751 table which needs initializing.
1753 INIT_LIST is a cons-list describing what parts of EXP actually
1754 need to be initialized. INIT is given to the *unique*, first
1755 constructor within INIT_LIST. If there are multiple first
1756 constructors, such as with multiple inheritance, INIT must
1757 be zero or an ambiguity error is reported.
1759 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1763 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1764 tree binfo, true_exp, exp, init;
1768 tree *old_init_ptr = init_ptr;
1769 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1772 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1774 expand_aggr_vbase_init (binfo, exp, addr, init_list);
1775 expand_vbase_vtables_init (binfo, binfo, true_exp, addr, 1);
1777 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1781 tree type = TREE_TYPE (exp);
1783 if (TREE_CODE (type) == REFERENCE_TYPE)
1784 type = TREE_TYPE (type);
1785 if (IS_AGGR_TYPE (type))
1786 cp_error ("unexpected argument to constructor `%T'", type);
1788 error ("unexpected argument to constructor");
1790 init_ptr = old_init_ptr;
1793 /* Report an error if NAME is not the name of a user-defined,
1794 aggregate type. If OR_ELSE is nonzero, give an error message. */
1796 is_aggr_typedef (name, or_else)
1802 if (name == error_mark_node)
1805 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1806 type = IDENTIFIER_TYPE_VALUE (name);
1810 cp_error ("`%T' is not an aggregate typedef", name);
1814 if (! IS_AGGR_TYPE (type)
1815 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1818 cp_error ("`%T' is not an aggregate type", type);
1824 /* Like is_aggr_typedef, but returns typedef if successful. */
1826 get_aggr_from_typedef (name, or_else)
1832 if (name == error_mark_node)
1835 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1836 type = IDENTIFIER_TYPE_VALUE (name);
1840 cp_error ("`%T' fails to be an aggregate typedef", name);
1844 if (! IS_AGGR_TYPE (type)
1845 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1848 cp_error ("type `%T' is of non-aggregate type", type);
1855 get_type_value (name)
1858 if (name == error_mark_node)
1861 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1862 return IDENTIFIER_TYPE_VALUE (name);
1868 /* This code could just as well go in `class.c', but is placed here for
1871 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1872 the appropriate function call. */
1874 build_member_call (cname, name, parmlist)
1875 tree cname, name, parmlist;
1878 tree method_name = name;
1880 int dont_use_this = 0;
1881 tree basetype_path, decl;
1883 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1885 method_name = TREE_OPERAND (method_name, 0);
1889 if (TREE_CODE (cname) == SCOPE_REF)
1890 cname = resolve_scope_to_name (NULL_TREE, cname);
1892 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1893 return error_mark_node;
1895 /* An operator we did not like. */
1896 if (name == NULL_TREE)
1897 return error_mark_node;
1902 /* Everything can explicitly call a destructor; see 12.4 */
1903 if (! TYPE_HAS_DESTRUCTOR (type))
1904 cp_error ("type `%#T' does not have a destructor", type);
1907 cp_error ("cannot call destructor `%T::~%T' without object", type,
1909 return error_mark_node;
1912 /* No object? Then just fake one up, and let build_method_call
1913 figure out what to do. */
1914 if (current_class_type == 0
1915 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1920 basetype_path = NULL_TREE;
1921 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1923 else if (current_class_decl == 0)
1926 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1930 tree olddecl = current_class_decl;
1931 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1932 if (oldtype != type)
1934 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1935 TYPE_VOLATILE (oldtype));
1936 decl = convert_force (TYPE_POINTER_TO (newtype), olddecl);
1942 decl = build_indirect_ref (decl, NULL_PTR);
1944 if (t = lookup_fnfields (TYPE_BINFO (type), method_name, 0))
1945 return build_method_call (decl, method_name, parmlist, basetype_path,
1946 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1947 if (TREE_CODE (name) == IDENTIFIER_NODE
1948 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1950 if (t == error_mark_node)
1951 return error_mark_node;
1952 if (TREE_CODE (t) == FIELD_DECL)
1956 cp_error ("invalid use of non-static field `%D'", t);
1957 return error_mark_node;
1959 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1961 else if (TREE_CODE (t) == VAR_DECL)
1965 cp_error ("invalid use of member `%D'", t);
1966 return error_mark_node;
1968 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1969 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1970 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1971 return build_function_call (decl, parmlist);
1975 cp_error ("no method `%T::%D'", type, name);
1976 return error_mark_node;
1980 /* Build a reference to a member of an aggregate. This is not a
1981 C++ `&', but really something which can have its address taken,
1982 and then act as a pointer to member, for example CNAME :: FIELD
1983 can have its address taken by saying & CNAME :: FIELD.
1985 @@ Prints out lousy diagnostics for operator <typename>
1988 @@ This function should be rewritten and placed in search.c. */
1990 build_offset_ref (cname, name)
1993 tree decl, type, fnfields, fields, t = error_mark_node;
1994 tree basetypes = NULL_TREE;
1997 if (TREE_CODE (cname) == SCOPE_REF)
1998 cname = resolve_scope_to_name (NULL_TREE, cname);
2000 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
2001 return error_mark_node;
2003 type = IDENTIFIER_TYPE_VALUE (cname);
2005 if (TREE_CODE (name) == BIT_NOT_EXPR)
2008 name = TREE_OPERAND (name, 0);
2011 if (TYPE_SIZE (type) == 0)
2013 t = IDENTIFIER_CLASS_VALUE (name);
2016 cp_error ("incomplete type `%T' does not have member `%D'", type,
2018 return error_mark_node;
2020 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
2021 || TREE_CODE (t) == CONST_DECL)
2026 if (TREE_CODE (t) == FIELD_DECL)
2027 sorry ("use of member in incomplete aggregate type");
2028 else if (TREE_CODE (t) == FUNCTION_DECL)
2029 sorry ("use of member function in incomplete aggregate type");
2031 my_friendly_abort (52);
2032 return error_mark_node;
2035 if (TREE_CODE (name) == TYPE_EXPR)
2036 /* Pass a TYPE_DECL to build_component_type_expr. */
2037 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
2038 name, NULL_TREE, 1);
2040 fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
2041 fields = lookup_field (type, name, 0, 0);
2043 if (fields == error_mark_node || fnfields == error_mark_node)
2044 return error_mark_node;
2046 if (current_class_type == 0
2047 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
2049 basetypes = TYPE_BINFO (type);
2050 decl = build1 (NOP_EXPR,
2051 IDENTIFIER_TYPE_VALUE (cname),
2054 else if (current_class_decl == 0)
2055 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
2060 /* A lot of this logic is now handled in lookup_field and
2064 basetypes = TREE_PURPOSE (fnfields);
2066 /* Go from the TREE_BASELINK to the member function info. */
2067 t = TREE_VALUE (fnfields);
2071 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
2073 error ("ambiguous member reference: member `%s' defined as both field and function",
2074 IDENTIFIER_POINTER (name));
2075 return error_mark_node;
2077 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
2079 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
2083 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
2084 return error_mark_node;
2088 if (t == TREE_VALUE (fnfields))
2090 extern int flag_save_memoized_contexts;
2092 /* This does not handle access checking yet. */
2093 if (DECL_CHAIN (t) == NULL_TREE || dtor)
2095 enum access_type access;
2097 /* unique functions are handled easily. */
2099 access = compute_access (basetypes, t);
2100 if (access == access_protected)
2102 cp_error_at ("member function `%#D' is protected", t);
2103 error ("in this context");
2104 return error_mark_node;
2106 if (access == access_private)
2108 cp_error_at ("member function `%#D' is private", t);
2109 error ("in this context");
2110 return error_mark_node;
2112 assemble_external (t);
2113 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2116 /* overloaded functions may need more work. */
2119 if (TYPE_HAS_DESTRUCTOR (type)
2120 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2126 /* FNFIELDS is most likely allocated on the search_obstack,
2127 which will go away after this class scope. If we need
2128 to save this value for later (either for memoization
2129 or for use as an initializer for a static variable), then
2132 ??? The smart thing to do for the case of saving initializers
2133 is to resolve them before we're done with this scope. */
2134 if (!TREE_PERMANENT (fnfields)
2135 && ((flag_save_memoized_contexts && global_bindings_p ())
2136 || ! allocation_temporary_p ()))
2137 fnfields = copy_list (fnfields);
2138 t = build_tree_list (error_mark_node, fnfields);
2139 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2144 /* Now that we know we are looking for a field, see if we
2145 have access to that field. Lookup_field will give us the
2148 t = lookup_field (basetypes, name, 1, 0);
2150 if (t == error_mark_node)
2151 return error_mark_node;
2155 cp_error ("`%D' is not a member of type `%T'", name,
2156 IDENTIFIER_TYPE_VALUE (cname));
2157 return error_mark_node;
2160 if (TREE_CODE (t) == TYPE_DECL)
2165 /* static class members and class-specific enum
2166 values can be returned without further ado. */
2167 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2169 assemble_external (t);
2174 /* static class functions too. */
2175 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2176 my_friendly_abort (53);
2178 /* In member functions, the form `cname::name' is no longer
2179 equivalent to `this->cname::name'. */
2180 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2183 /* Given an object EXP and a member function reference MEMBER,
2184 return the address of the actual member function. */
2186 get_member_function (exp_addr_ptr, exp, member)
2190 tree ctype = TREE_TYPE (exp);
2191 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2193 if (TYPE_VIRTUAL_P (ctype)
2194 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2199 /* Save away the unadulterated `this' pointer. */
2200 exp_addr = save_expr (*exp_addr_ptr);
2202 /* Cast function to signed integer. */
2203 e0 = build1 (NOP_EXPR, integer_type_node, function);
2205 #ifdef VTABLE_USES_MASK
2206 /* If we are willing to limit the number of
2207 virtual functions a class may have to some
2208 *small* number, then if, for a function address,
2209 we are passed some small number, we know that
2210 it is a virtual function index, and work from there. */
2211 e1 = build (BIT_AND_EXPR, integer_type_node, e0, vtbl_mask);
2213 /* There is a hack here that takes advantage of
2214 twos complement arithmetic, and the fact that
2215 there are more than one UNITS to the WORD.
2216 If the high bit is set for the `function',
2217 then we pretend it is a virtual function,
2218 and the array indexing will knock this bit
2219 out the top, leaving a valid index. */
2220 if (UNITS_PER_WORD <= 1)
2221 my_friendly_abort (54);
2223 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2224 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2225 build_tree_list (NULL_TREE, e1)));
2226 e1 = save_expr (e1);
2229 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2231 exp = build_indirect_ref (exp_addr, NULL_PTR);
2232 *exp_addr_ptr = exp_addr;
2235 /* This is really hairy: if the function pointer is a pointer
2236 to a non-virtual member function, then we can't go mucking
2237 with the `this' pointer (any more than we already have to
2238 this point). If it is a pointer to a virtual member function,
2239 then we have to adjust the `this' pointer according to
2240 what the virtual function table tells us. */
2242 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2243 my_friendly_assert (e3 != error_mark_node, 213);
2245 /* Change this pointer type from `void *' to the
2246 type it is really supposed to be. */
2247 TREE_TYPE (e3) = TREE_TYPE (function);
2249 /* If non-virtual, use what we had originally. Otherwise,
2250 use the value we get from the virtual function table. */
2251 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2253 function = build_conditional_expr (e1, function, e3);
2255 return build_indirect_ref (function, NULL_PTR);
2258 /* If a OFFSET_REF made it through to here, then it did
2259 not have its address taken. */
2262 resolve_offset_ref (exp)
2265 tree type = TREE_TYPE (exp);
2266 tree base = NULL_TREE;
2268 tree basetype, addr;
2270 if (TREE_CODE (exp) == TREE_LIST)
2271 return build_unary_op (ADDR_EXPR, exp, 0);
2273 if (TREE_CODE (exp) != OFFSET_REF)
2275 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2276 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2278 error ("object missing in use of pointer-to-member construct");
2279 return error_mark_node;
2282 type = TREE_TYPE (type);
2287 member = TREE_OPERAND (exp, 1);
2288 base = TREE_OPERAND (exp, 0);
2291 if ((TREE_CODE (member) == VAR_DECL
2292 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2293 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2295 /* These were static members. */
2296 if (mark_addressable (member) == 0)
2297 return error_mark_node;
2301 /* Syntax error can cause a member which should
2302 have been seen as static to be grok'd as non-static. */
2303 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2305 if (TREE_ADDRESSABLE (member) == 0)
2307 cp_error_at ("member `%D' is non-static in static member function context", member);
2308 error ("at this point in file");
2309 TREE_ADDRESSABLE (member) = 1;
2311 return error_mark_node;
2314 /* The first case is really just a reference to a member of `this'. */
2315 if (TREE_CODE (member) == FIELD_DECL
2317 || (TREE_CODE (base) == NOP_EXPR
2318 && TREE_OPERAND (base, 0) == error_mark_node)))
2321 enum access_type access;
2323 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2325 basetype = TYPE_OFFSET_BASETYPE (type);
2326 base = convert_pointer_to (basetype, current_class_decl);
2329 base = current_class_decl;
2330 basetype = DECL_CONTEXT (member);
2332 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2334 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2335 return error_mark_node;
2337 addr = convert_pointer_to (basetype, base);
2338 access = compute_access (basetype_path, member);
2339 if (access == access_public)
2340 return build (COMPONENT_REF, TREE_TYPE (member),
2341 build_indirect_ref (addr, NULL_PTR), member);
2342 if (access == access_protected)
2344 cp_error_at ("member `%D' is protected", member);
2345 error ("in this context");
2346 return error_mark_node;
2348 if (access == access_private)
2350 cp_error_at ("member `%D' is private", member);
2351 error ("in this context");
2352 return error_mark_node;
2354 my_friendly_abort (55);
2357 /* If this is a reference to a member function, then return
2358 the address of the member function (which may involve going
2359 through the object's vtable), otherwise, return an expression
2360 for the dereferenced pointer-to-member construct. */
2361 addr = build_unary_op (ADDR_EXPR, base, 0);
2363 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2365 basetype = DECL_CLASS_CONTEXT (member);
2366 addr = convert_pointer_to (basetype, addr);
2367 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2369 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2371 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2372 addr = convert_pointer_to (basetype, addr);
2373 member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
2374 return build1 (INDIRECT_REF, type,
2375 build (PLUS_EXPR, ptr_type_node, addr, member));
2377 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2379 return get_member_function_from_ptrfunc (&addr, base, member);
2381 my_friendly_abort (56);
2386 /* Return either DECL or its known constant value (if it has one). */
2389 decl_constant_value (decl)
2392 if (! TREE_THIS_VOLATILE (decl)
2394 /* These may be necessary for C, but they break C++. */
2395 ! TREE_PUBLIC (decl)
2396 /* Don't change a variable array bound or initial value to a constant
2397 in a place where a variable is invalid. */
2400 && DECL_INITIAL (decl) != 0
2401 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2402 /* This is invalid if initial value is not constant.
2403 If it has either a function call, a memory reference,
2404 or a variable, then re-evaluating it could give different results. */
2405 && TREE_CONSTANT (DECL_INITIAL (decl))
2406 /* Check for cases where this is sub-optimal, even though valid. */
2407 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2409 /* We must allow this to work outside of functions so that
2410 static constants can be used for array sizes. */
2411 && current_function_decl != 0
2412 && DECL_MODE (decl) != BLKmode
2415 return DECL_INITIAL (decl);
2419 /* Friend handling routines. */
2420 /* Friend data structures:
2422 Lists of friend functions come from TYPE_DECL nodes. Since all
2423 aggregate types are automatically typedef'd, these nodes are guaranteed
2426 The TREE_PURPOSE of a friend list is the name of the friend,
2427 and its TREE_VALUE is another list.
2429 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2430 will be filled in, but not both. The TREE_VALUE of that list is an
2431 individual function which is a friend. The TREE_PURPOSE of that list
2432 indicates a type in which all functions by that name are friends.
2434 Lists of friend classes come from _TYPE nodes. Love that consistency
2438 is_friend_type (type1, type2)
2441 return is_friend (type1, type2);
2445 is_friend (type, supplicant)
2446 tree type, supplicant;
2451 if (supplicant == NULL_TREE || type == NULL_TREE)
2454 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2457 /* It's a function decl. */
2459 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2460 tree name = DECL_NAME (supplicant);
2461 tree ctype = DECL_CLASS_CONTEXT (supplicant);
2462 for (; list ; list = TREE_CHAIN (list))
2464 if (name == TREE_PURPOSE (list))
2466 tree friends = TREE_VALUE (list);
2467 name = DECL_ASSEMBLER_NAME (supplicant);
2468 for (; friends ; friends = TREE_CHAIN (friends))
2470 if (ctype == TREE_PURPOSE (friends))
2472 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2482 if (type == supplicant)
2485 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2486 for (; list ; list = TREE_CHAIN (list))
2487 if (supplicant == TREE_VALUE (list))
2492 tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2493 : DECL_CONTEXT (TYPE_NAME (supplicant));
2496 return is_friend (type, context);
2502 /* Add a new friend to the friends of the aggregate type TYPE.
2503 DECL is the FUNCTION_DECL of the friend being added. */
2505 add_friend (type, decl)
2508 tree typedecl = TYPE_NAME (type);
2509 tree list = DECL_FRIENDLIST (typedecl);
2510 tree name = DECL_NAME (decl);
2514 if (name == TREE_PURPOSE (list))
2516 tree friends = TREE_VALUE (list);
2517 for (; friends ; friends = TREE_CHAIN (friends))
2519 if (decl == TREE_VALUE (friends))
2521 cp_pedwarn ("`%D' is already a friend of class `%T'",
2523 cp_pedwarn_at ("previous friend declaration of `%D'",
2524 TREE_VALUE (friends));
2528 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2532 list = TREE_CHAIN (list);
2534 DECL_FRIENDLIST (typedecl)
2535 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2536 DECL_FRIENDLIST (typedecl));
2537 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2539 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2540 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2541 if (parmtypes && TREE_CHAIN (parmtypes))
2543 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2544 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2545 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2546 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2551 /* Declare that every member function NAME in FRIEND_TYPE
2552 (which may be NULL_TREE) is a friend of type TYPE. */
2554 add_friends (type, name, friend_type)
2555 tree type, name, friend_type;
2557 tree typedecl = TYPE_NAME (type);
2558 tree list = DECL_FRIENDLIST (typedecl);
2562 if (name == TREE_PURPOSE (list))
2564 tree friends = TREE_VALUE (list);
2565 while (friends && TREE_PURPOSE (friends) != friend_type)
2566 friends = TREE_CHAIN (friends);
2569 warning ("method `%s::%s' is already a friend of class",
2570 TYPE_NAME_STRING (friend_type),
2571 IDENTIFIER_POINTER (name));
2573 warning ("function `%s' is already a friend of class `%s'",
2574 IDENTIFIER_POINTER (name),
2575 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2577 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2581 list = TREE_CHAIN (list);
2583 DECL_FRIENDLIST (typedecl) =
2585 build_tree_list (friend_type, NULL_TREE),
2586 DECL_FRIENDLIST (typedecl));
2587 if (! strncmp (IDENTIFIER_POINTER (name),
2588 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2589 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2591 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2592 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2596 /* Set up a cross reference so that type TYPE will make member function
2597 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2598 one, set up a cross reference so that functions with the name DECL
2599 and type CTYPE know that they are friends of TYPE. */
2601 xref_friend (type, decl, ctype)
2602 tree type, decl, ctype;
2604 tree friend_decl = TYPE_NAME (ctype);
2606 tree typedecl = TYPE_NAME (type);
2607 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2609 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2613 SET_DECL_WAITING_FRIENDS (friend_decl,
2615 DECL_WAITING_FRIENDS (friend_decl)));
2616 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2619 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2620 been defined, we make all of its member functions friends of
2621 TYPE. If not, we make it a pending friend, which can later be added
2622 when its definition is seen. If a type is defined, then its TYPE_DECL's
2623 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2624 classes that are not defined. If a type has not yet been defined,
2625 then the DECL_WAITING_FRIENDS contains a list of types
2626 waiting to make it their friend. Note that these two can both
2627 be in use at the same time! */
2629 make_friend_class (type, friend_type)
2630 tree type, friend_type;
2634 if (IS_SIGNATURE (type))
2636 error ("`friend' declaration in signature definition");
2639 if (IS_SIGNATURE (friend_type))
2641 error ("signature type `%s' declared `friend'",
2642 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2645 if (type == friend_type)
2647 warning ("class `%s' is implicitly friends with itself",
2648 TYPE_NAME_STRING (type));
2652 GNU_xref_hier (TYPE_NAME_STRING (type),
2653 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2655 classes = CLASSTYPE_FRIEND_CLASSES (type);
2656 while (classes && TREE_VALUE (classes) != friend_type)
2657 classes = TREE_CHAIN (classes);
2659 warning ("class `%s' is already friends with class `%s'",
2660 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2663 CLASSTYPE_FRIEND_CLASSES (type)
2664 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2668 /* Main friend processor. This is large, and for modularity purposes,
2669 has been removed from grokdeclarator. It returns `void_type_node'
2670 to indicate that something happened, though a FIELD_DECL is
2673 CTYPE is the class this friend belongs to.
2675 DECLARATOR is the name of the friend.
2677 DECL is the FUNCTION_DECL that the friend is.
2679 In case we are parsing a friend which is part of an inline
2680 definition, we will need to store PARM_DECL chain that comes
2681 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2683 FLAGS is just used for `grokclassfn'.
2685 QUALS say what special qualifies should apply to the object
2686 pointed to by `this'. */
2688 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2689 tree ctype, declarator, decl, parmdecls;
2690 enum overload_flags flags;
2693 /* first, lets find out if what we are making a friend needs overloading */
2695 int was_c_linkage = 0;
2697 /* Every decl that gets here is a friend of something. */
2698 DECL_FRIEND_P (decl) = 1;
2700 /* If we find something in scope, let see if it has extern "C" linkage. */
2701 /* This code is pretty general and should be ripped out and reused
2702 as a separate function. */
2703 if (DECL_NAME (decl))
2705 previous_decl = lookup_name (DECL_NAME (decl), 0);
2706 if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
2710 if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
2712 previous_decl = TREE_VALUE (previous_decl);
2715 previous_decl = TREE_CHAIN (previous_decl);
2717 while (previous_decl);
2720 /* It had extern "C" linkage, so don't overload this. */
2721 if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL
2722 && TREE_TYPE (decl) == TREE_TYPE (previous_decl)
2723 && DECL_LANGUAGE (previous_decl) == lang_c)
2729 tree cname = TYPE_NAME (ctype);
2730 if (TREE_CODE (cname) == TYPE_DECL)
2731 cname = DECL_NAME (cname);
2733 /* A method friend. */
2734 if (TREE_CODE (decl) == FUNCTION_DECL)
2736 if (flags == NO_SPECIAL && ctype && declarator == cname)
2737 DECL_CONSTRUCTOR_P (decl) = 1;
2739 /* This will set up DECL_ARGUMENTS for us. */
2740 grokclassfn (ctype, cname, decl, flags, quals);
2741 if (TYPE_SIZE (ctype) != 0)
2742 check_classfn (ctype, cname, decl);
2744 if (TREE_TYPE (decl) != error_mark_node)
2746 if (TYPE_SIZE (ctype))
2748 /* We don't call pushdecl here yet, or ever on this
2749 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2751 make_decl_rtl (decl, NULL_PTR, 1);
2752 add_friend (current_class_type, decl);
2756 register char *classname
2757 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2759 error ("member declared as friend before type `%s' defined",
2766 /* Possibly a bunch of method friends. */
2768 /* Get the class they belong to. */
2769 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2771 /* This class is defined, use its methods now. */
2772 if (TYPE_SIZE (ctype))
2774 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2776 add_friends (current_class_type, declarator, ctype);
2778 error ("method `%s' is not a member of class `%s'",
2779 IDENTIFIER_POINTER (declarator),
2780 IDENTIFIER_POINTER (cname));
2783 /* Note: DECLARATOR actually has more than one; in this
2784 case, we're making sure that fns with the name DECLARATOR
2785 and type CTYPE know they are friends of the current
2787 xref_friend (current_class_type, declarator, ctype);
2788 decl = void_type_node;
2791 /* never overload C functions */
2792 else if (TREE_CODE (decl) == FUNCTION_DECL
2793 && ((IDENTIFIER_LENGTH (declarator) == 4
2794 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2795 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2796 || (IDENTIFIER_LENGTH (declarator) > 10
2797 && IDENTIFIER_POINTER (declarator)[0] == '_'
2798 && IDENTIFIER_POINTER (declarator)[1] == '_'
2799 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2800 "builtin_", 8) == 0)
2803 /* raw "main", and builtin functions never gets overloaded,
2804 but they can become friends. */
2805 TREE_PUBLIC (decl) = 1;
2806 add_friend (current_class_type, decl);
2807 DECL_FRIEND_P (decl) = 1;
2808 decl = void_type_node;
2811 @@ or possibly a friend from a base class ?!? */
2812 else if (TREE_CODE (decl) == FUNCTION_DECL)
2814 /* Friends must all go through the overload machinery,
2815 even though they may not technically be overloaded.
2817 Note that because classes all wind up being top-level
2818 in their scope, their friend wind up in top-level scope as well. */
2819 DECL_ASSEMBLER_NAME (decl)
2820 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2821 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2822 DECL_ARGUMENTS (decl) = parmdecls;
2823 DECL_CLASS_CONTEXT (decl) = current_class_type;
2825 /* We can call pushdecl here, because the TREE_CHAIN of this
2826 FUNCTION_DECL is not needed for other purposes. */
2827 decl = pushdecl_top_level (decl);
2829 make_decl_rtl (decl, NULL_PTR, 1);
2830 add_friend (current_class_type, decl);
2832 DECL_FRIEND_P (decl) = 1;
2833 TREE_OVERLOADED (declarator) = 1;
2837 /* @@ Should be able to ingest later definitions of this function
2839 tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2840 if (decl == NULL_TREE)
2842 warning ("implicitly declaring `%s' as struct",
2843 IDENTIFIER_POINTER (declarator));
2844 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2845 decl = TYPE_NAME (decl);
2848 /* Allow abbreviated declarations of overloaded functions,
2849 but not if those functions are really class names. */
2850 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2852 warning ("`friend %s' archaic, use `friend class %s' instead",
2853 IDENTIFIER_POINTER (declarator),
2854 IDENTIFIER_POINTER (declarator));
2855 decl = TREE_TYPE (TREE_PURPOSE (decl));
2858 if (TREE_CODE (decl) == TREE_LIST)
2859 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2861 make_friend_class (current_class_type, TREE_TYPE (decl));
2862 decl = void_type_node;
2867 /* TYPE has now been defined. It may, however, have a number of things
2868 waiting make make it their friend. We resolve these references
2871 embrace_waiting_friends (type)
2874 tree decl = TYPE_NAME (type);
2877 if (TREE_CODE (decl) != TYPE_DECL)
2880 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2881 waiters = TREE_CHAIN (waiters))
2883 tree waiter = TREE_PURPOSE (waiters);
2885 tree waiter_prev = TREE_VALUE (waiters);
2887 tree decl = TREE_TYPE (waiters);
2888 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2889 ? decl : DECL_NAME (decl)) : NULL_TREE;
2892 /* @@ There may be work to be done since we have not verified
2893 @@ consistency between original and friend declarations
2894 @@ of the functions waiting to become friends. */
2895 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2898 add_friends (waiter, name, type);
2900 add_friend (waiter, decl);
2902 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2903 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2904 "no method `%s' defined in class `%s' to be friend",
2905 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2906 TYPE_NAME_STRING (type));
2909 make_friend_class (type, waiter);
2912 if (TREE_CHAIN (waiter_prev))
2913 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2915 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2920 /* Common subroutines of build_new and build_vec_delete. */
2922 /* Common interface for calling "builtin" functions that are not
2926 build_builtin_call (type, node, arglist)
2931 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2932 TREE_SIDE_EFFECTS (rval) = 1;
2933 assemble_external (TREE_OPERAND (node, 0));
2934 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2938 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2939 (which needs to go through some sort of groktypename) or it
2940 is the name of the class we are newing. INIT is an initialization value.
2941 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2942 If INIT is void_type_node, it means do *not* call a constructor
2945 For types with constructors, the data returned is initialized
2946 by the appropriate constructor.
2948 Whether the type has a constructor or not, if it has a pointer
2949 to a virtual function table, then that pointer is set up
2952 Unless I am mistaken, a call to new () will return initialized
2953 data regardless of whether the constructor itself is private or
2954 not. NOPE; new fails if the constructor is private (jcm).
2956 Note that build_new does nothing to assure that any special
2957 alignment requirements of the type are met. Rather, it leaves
2958 it up to malloc to do the right thing. Otherwise, folding to
2959 the right alignment cal cause problems if the user tries to later
2960 free the memory returned by `new'.
2962 PLACEMENT is the `placement' list for user-defined operator new (). */
2965 build_new (placement, decl, init, use_global_new)
2970 tree type, true_type, size, rval;
2973 enum tree_code code = NEW_EXPR;
2975 tree pending_sizes = NULL_TREE;
2977 if (decl == error_mark_node)
2978 return error_mark_node;
2980 if (TREE_CODE (decl) == TREE_LIST)
2982 tree absdcl = TREE_VALUE (decl);
2983 tree last_absdcl = NULL_TREE;
2984 int old_immediate_size_expand;
2986 if (current_function_decl
2987 && DECL_CONSTRUCTOR_P (current_function_decl))
2989 old_immediate_size_expand = immediate_size_expand;
2990 immediate_size_expand = 0;
2993 nelts = integer_one_node;
2995 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2996 my_friendly_abort (215);
2997 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2999 last_absdcl = absdcl;
3000 absdcl = TREE_OPERAND (absdcl, 0);
3003 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
3005 /* probably meant to be a vec new */
3008 while (TREE_OPERAND (absdcl, 0)
3009 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
3011 last_absdcl = absdcl;
3012 absdcl = TREE_OPERAND (absdcl, 0);
3016 this_nelts = TREE_OPERAND (absdcl, 1);
3017 if (this_nelts != error_mark_node)
3019 if (this_nelts == NULL_TREE)
3020 error ("new of array type fails to specify size");
3023 this_nelts = save_expr (convert (sizetype, this_nelts));
3024 absdcl = TREE_OPERAND (absdcl, 0);
3025 if (this_nelts == integer_zero_node)
3027 warning ("zero size array reserves no space");
3028 nelts = integer_zero_node;
3031 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3035 nelts = integer_zero_node;
3039 TREE_OPERAND (last_absdcl, 0) = absdcl;
3041 TREE_VALUE (decl) = absdcl;
3043 type = true_type = groktypename (decl);
3044 if (! type || type == error_mark_node)
3046 immediate_size_expand = old_immediate_size_expand;
3047 return error_mark_node;
3050 if (current_function_decl
3051 && DECL_CONSTRUCTOR_P (current_function_decl))
3053 pending_sizes = get_pending_sizes ();
3054 immediate_size_expand = old_immediate_size_expand;
3057 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
3059 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
3061 /* An aggregate type. */
3062 type = IDENTIFIER_TYPE_VALUE (decl);
3063 decl = TYPE_NAME (type);
3067 /* A builtin type. */
3068 decl = lookup_name (decl, 1);
3069 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
3070 type = TREE_TYPE (decl);
3074 else if (TREE_CODE (decl) == TYPE_DECL)
3076 type = TREE_TYPE (decl);
3083 decl = TYPE_NAME (type);
3086 /* ``A reference cannot be created by the new operator. A reference
3087 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3088 returned by new.'' ARM 5.3.3 */
3089 if (TREE_CODE (type) == REFERENCE_TYPE)
3091 error ("new cannot be applied to a reference type");
3092 type = true_type = TREE_TYPE (type);
3095 /* When the object being created is an array, the new-expression yields a
3096 pointer to the initial element (if any) of the array. For example,
3097 both new int and new int[10] return an int*. 5.3.4. */
3098 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
3100 nelts = array_type_nelts_top (type);
3102 type = true_type = TREE_TYPE (type);
3105 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
3107 pedwarn ("const and volatile types cannot be created with operator new");
3108 type = true_type = TYPE_MAIN_VARIANT (type);
3111 /* If our base type is an array, then make sure we know how many elements
3113 while (TREE_CODE (true_type) == ARRAY_TYPE)
3115 tree this_nelts = array_type_nelts_top (true_type);
3116 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3117 true_type = TREE_TYPE (true_type);
3120 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3123 size = size_in_bytes (type);
3125 if (TYPE_SIZE (true_type) == 0)
3127 if (true_type == void_type_node)
3128 error ("invalid type for new: `void'");
3130 incomplete_type_error (0, true_type);
3131 return error_mark_node;
3134 if (TYPE_LANG_SPECIFIC (true_type)
3135 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3137 abstract_virtuals_error (NULL_TREE, true_type);
3138 return error_mark_node;
3141 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3143 signature_error (NULL_TREE, true_type);
3144 return error_mark_node;
3147 /* Get a little extra space to store a couple of things before the new'ed
3149 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
3151 tree extra = BI_header_size;
3153 size = size_binop (PLUS_EXPR, size, extra);
3157 code = VEC_NEW_EXPR;
3159 /* Allocate the object. */
3160 if (! use_global_new && TYPE_LANG_SPECIFIC (true_type)
3161 && (TYPE_GETS_NEW (true_type) & (1 << has_array)))
3162 rval = build_opfncall (code, LOOKUP_NORMAL,
3163 TYPE_POINTER_TO (true_type), size, placement);
3166 rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3167 ptr_type_node, size, placement);
3168 rval = convert (TYPE_POINTER_TO (true_type), rval);
3170 else if (! has_array && flag_this_is_variable > 0
3171 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3173 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3177 error ("constructors take parameter lists");
3178 return error_mark_node;
3183 rval = build_builtin_call (build_pointer_type (true_type),
3184 has_array ? BIVN : BIN,
3185 build_tree_list (NULL_TREE, size));
3187 /* See comment above as to why this is disabled. */
3190 rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3192 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3193 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3197 TREE_CALLS_NEW (rval) = 1;
3200 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3201 sure we have some extra bytes in that case for the BI_header_size
3202 cookies? And how does that interact with the code below? (mrs) */
3203 /* Finish up some magic for new'ed arrays */
3204 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) && rval != NULL_TREE)
3206 tree extra = BI_header_size;
3208 rval = convert (ptr_type_node, rval); /* convert to void * first */
3209 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3210 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3211 /* Store header info. */
3212 cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3213 rval, extra), NULL_PTR);
3214 exp1 = build (MODIFY_EXPR, void_type_node,
3215 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3217 TREE_SIDE_EFFECTS (exp1) = 1;
3218 rval = convert (build_pointer_type (true_type), rval);
3219 TREE_CALLS_NEW (rval) = 1;
3220 TREE_SIDE_EFFECTS (rval) = 1;
3221 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3222 build_tree_list (NULL_TREE, rval)));
3225 /* We've figured out where the allocation is to go.
3226 If we're not eliding constructors, then if a constructor
3227 is defined, we must go through it. */
3228 if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
3229 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3232 /* Constructors are never virtual. If it has an initialization, we
3233 need to complain if we aren't allowed to use the ctor that took
3235 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3237 /* If a copy constructor might work, set things up so that we can
3238 try that after this. We deliberately don't clear LOOKUP_COMPLAIN
3239 any more, since that would make it impossible to rationally use
3240 the access of a constructor that matches perfectly. */
3242 if (rval != NULL_TREE)
3243 flags |= LOOKUP_SPECULATIVELY;
3246 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3248 init = tree_cons (NULL_TREE, integer_one_node, init);
3249 flags |= LOOKUP_HAS_IN_CHARGE;
3255 if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE)
3256 tmp = build_indirect_ref (tmp, NULL_PTR);
3258 newrval = build_method_call (tmp, constructor_name_full (true_type),
3259 init, NULL_TREE, flags);
3265 TREE_HAS_CONSTRUCTOR (rval) = 1;
3268 rval = error_mark_node;
3272 if (rval == error_mark_node)
3273 return error_mark_node;
3274 rval = save_expr (rval);
3275 TREE_HAS_CONSTRUCTOR (rval) = 1;
3277 /* Don't call any constructors or do any initialization. */
3278 if (init == void_type_node)
3281 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
3283 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
3285 /* New 2.0 interpretation: `new int (10)' means
3286 allocate an int, and initialize it with 10. */
3288 init = build_c_cast (type, init);
3289 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3290 build_modify_expr (build_indirect_ref (rval, NULL_PTR),
3293 TREE_SIDE_EFFECTS (rval) = 1;
3294 TREE_CALLS_NEW (rval) = 1;
3296 else if (current_function_decl == NULL_TREE)
3298 extern tree static_aggregates;
3300 /* In case of static initialization, SAVE_EXPR is good enough. */
3301 init = copy_to_permanent (init);
3302 rval = copy_to_permanent (rval);
3303 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3307 /* Have to wrap this in RTL_EXPR for two cases:
3308 in base or member initialization and if we
3309 are a branch of a ?: operator. Since we
3310 can't easily know the latter, just do it always. */
3311 tree xval = make_node (RTL_EXPR);
3313 TREE_TYPE (xval) = TREE_TYPE (rval);
3314 do_pending_stack_adjust ();
3315 start_sequence_for_rtl_expr (xval);
3317 /* As a matter of principle, `start_sequence' should do this. */
3321 rval = expand_vec_init (decl, rval,
3322 build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
3325 expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0);
3327 do_pending_stack_adjust ();
3329 TREE_SIDE_EFFECTS (xval) = 1;
3330 TREE_CALLS_NEW (xval) = 1;
3331 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3334 if (TREE_CODE (rval) == SAVE_EXPR)
3336 /* Errors may cause this to not get evaluated. */
3337 if (SAVE_EXPR_RTL (rval) == 0)
3338 SAVE_EXPR_RTL (rval) = const0_rtx;
3339 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3343 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3344 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3350 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3352 /* The type of new int [3][3] is not int *, but int [3] * */
3353 rval = build_c_cast (build_pointer_type (type), rval);
3357 rval = build_compound_expr (chainon (pending_sizes,
3358 build_tree_list (NULL_TREE, rval)));
3362 extern tree gc_visible;
3366 rval = save_expr (rval);
3367 /* We don't need a `headof' operation to do this because
3368 we know where the object starts. */
3369 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3370 build (MINUS_EXPR, ptr_type_node,
3371 rval, c_sizeof_nowarn (unsigned_type_node)));
3372 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3373 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3374 tree_cons (NULL_TREE, update_expr,
3375 build_tree_list (NULL_TREE, rval))));
3378 return save_expr (rval);
3381 /* `expand_vec_init' performs initialization of a vector of aggregate
3384 DECL is passed only for error reporting, and provides line number
3385 and source file name information.
3386 BASE is the space where the vector will be.
3387 MAXINDEX is the maximum index of the array (one less than the
3388 number of elements).
3389 INIT is the (possibly NULL) initializer.
3391 FROM_ARRAY is 0 if we should init everything with INIT
3392 (i.e., every element initialized from INIT).
3393 FROM_ARRAY is 1 if we should index into INIT in parallel
3394 with initialization of DECL.
3395 FROM_ARRAY is 2 if we should index into INIT in parallel,
3396 but use assignment instead of initialization. */
3399 expand_vec_init (decl, base, maxindex, init, from_array)
3400 tree decl, base, maxindex, init;
3404 tree iterator, base2 = NULL_TREE;
3405 tree type = TREE_TYPE (TREE_TYPE (base));
3408 maxindex = convert (integer_type_node, maxindex);
3409 if (maxindex == error_mark_node)
3410 return error_mark_node;
3412 if (current_function_decl == NULL_TREE)
3414 rval = make_tree_vec (3);
3415 TREE_VEC_ELT (rval, 0) = base;
3416 TREE_VEC_ELT (rval, 1) = maxindex;
3417 TREE_VEC_ELT (rval, 2) = init;
3421 size = size_in_bytes (type);
3423 /* Set to zero in case size is <= 0. Optimizer will delete this if
3424 it is not needed. */
3425 rval = get_temp_regvar (TYPE_POINTER_TO (type),
3426 convert (TYPE_POINTER_TO (type), null_pointer_node));
3427 base = default_conversion (base);
3428 base = convert (TYPE_POINTER_TO (type), base);
3429 expand_assignment (rval, base, 0, 0);
3430 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3432 if (init != NULL_TREE
3433 && TREE_CODE (init) == CONSTRUCTOR
3434 && TREE_TYPE (init) == TREE_TYPE (decl))
3436 /* Initialization of array from {...}. */
3437 tree elts = CONSTRUCTOR_ELTS (init);
3438 tree baseref = build1 (INDIRECT_REF, type, base);
3439 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3440 int host_i = TREE_INT_CST_LOW (maxindex);
3442 if (IS_AGGR_TYPE (type))
3447 expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3449 expand_assignment (base, baseinc, 0, 0);
3450 elts = TREE_CHAIN (elts);
3452 /* Initialize any elements by default if possible. */
3455 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3458 use_variable (DECL_RTL (base));
3462 iterator = get_temp_regvar (integer_type_node,
3463 build_int_2 (host_i, 0));
3465 goto init_by_default;
3471 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3473 expand_assignment (base, baseinc, 0, 0);
3474 elts = TREE_CHAIN (elts);
3478 use_variable (DECL_RTL (base));
3484 iterator = get_temp_regvar (integer_type_node, maxindex);
3488 /* If initializing one array from another,
3489 initialize element by element. */
3492 /* We rely upon the below calls the do argument checking */
3493 if (decl == NULL_TREE)
3495 sorry ("initialization of array from dissimilar array type");
3496 return error_mark_node;
3500 base2 = default_conversion (init);
3501 itype = TREE_TYPE (base2);
3502 base2 = get_temp_regvar (itype, base2);
3503 itype = TREE_TYPE (itype);
3505 else if (TYPE_LANG_SPECIFIC (type)
3506 && TYPE_NEEDS_CONSTRUCTING (type)
3507 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3509 error ("initializer ends prematurely");
3510 return error_mark_node;
3514 expand_start_cond (build (GE_EXPR, integer_type_node,
3515 iterator, integer_zero_node), 0);
3516 expand_start_loop_continue_elsewhere (1);
3520 tree to = build1 (INDIRECT_REF, type, base);
3524 from = build1 (INDIRECT_REF, itype, base2);
3528 if (from_array == 2)
3529 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3530 else if (TYPE_NEEDS_CONSTRUCTING (type))
3531 expand_aggr_init (to, from, 0);
3533 expand_assignment (to, from, 0, 0);
3535 my_friendly_abort (57);
3537 else if (TREE_CODE (type) == ARRAY_TYPE)
3540 sorry ("cannot initialize multi-dimensional array with initializer");
3541 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3542 array_type_nelts (type), 0, 0);
3545 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3547 expand_assignment (base,
3548 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3551 expand_assignment (base2,
3552 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3553 expand_loop_continue_here ();
3554 expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
3555 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3559 use_variable (DECL_RTL (base));
3561 use_variable (DECL_RTL (base2));
3566 use_variable (DECL_RTL (iterator));
3571 use_variable (DECL_RTL (rval));
3575 /* Free up storage of type TYPE, at address ADDR.
3577 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3580 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3581 used as the second argument to operator delete. It can include
3582 things like padding and magic size cookies. It has virtual in it,
3583 because if you have a base pointer and you delete through a virtual
3584 destructor, it should be the size of the dynamic object, not the
3585 static object, see Free Store 12.5 ANSI C++ WP.
3587 This does not call any destructors. */
3589 build_x_delete (type, addr, which_delete, virtual_size)
3594 int use_global_delete = which_delete & 1;
3595 int use_vec_delete = !!(which_delete & 2);
3597 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3599 if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3600 && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete)))
3601 rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
3603 rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID,
3604 build_tree_list (NULL_TREE, addr));
3608 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3609 ADDR is an expression which yields the store to be destroyed.
3610 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3611 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3612 virtual baseclasses.
3613 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3615 FLAGS is the logical disjunction of zero or more LOOKUP_
3616 flags. See cp-tree.h for more info.
3618 This function does not delete an object's virtual base classes. */
3620 build_delete (type, addr, auto_delete, flags, use_global_delete)
3624 int use_global_delete;
3626 tree function, parms;
3632 if (addr == error_mark_node)
3633 return error_mark_node;
3635 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3636 set to `error_mark_node' before it gets properly cleaned up. */
3637 if (type == error_mark_node)
3638 return error_mark_node;
3640 type = TYPE_MAIN_VARIANT (type);
3642 if (TREE_CODE (type) == POINTER_TYPE)
3644 type = TREE_TYPE (type);
3645 if (TYPE_SIZE (type) == 0)
3647 incomplete_type_error (0, type);
3648 return error_mark_node;
3650 if (TREE_CODE (type) == ARRAY_TYPE)
3652 if (! IS_AGGR_TYPE (type))
3654 /* Call the builtin operator delete. */
3655 return build_builtin_call (void_type_node, BID,
3656 build_tree_list (NULL_TREE, addr));
3658 if (TREE_SIDE_EFFECTS (addr))
3659 addr = save_expr (addr);
3660 ref = build_indirect_ref (addr, NULL_PTR);
3663 else if (TREE_CODE (type) == ARRAY_TYPE)
3666 if (TREE_SIDE_EFFECTS (addr))
3667 addr = save_expr (addr);
3668 return build_vec_delete (addr, array_type_nelts (type),
3669 c_sizeof_nowarn (TREE_TYPE (type)),
3670 auto_delete, integer_two_node,
3675 /* Don't check PROTECT here; leave that decision to the
3676 destructor. If the destructor is accessible, call it,
3677 else report error. */
3678 addr = build_unary_op (ADDR_EXPR, addr, 0);
3679 if (TREE_SIDE_EFFECTS (addr))
3680 addr = save_expr (addr);
3682 if (TREE_CONSTANT (addr))
3683 addr = convert_pointer_to (type, addr);
3685 addr = convert_force (build_pointer_type (type), addr);
3687 if (TREE_CODE (addr) == NOP_EXPR
3688 && TREE_OPERAND (addr, 0) == current_class_decl)
3691 ref = build_indirect_ref (addr, NULL_PTR);
3695 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3697 if (! TYPE_NEEDS_DESTRUCTOR (type))
3699 if (auto_delete == integer_zero_node)
3700 return void_zero_node;
3702 /* Pass the size of the object down to the operator delete() in
3703 addition to the ADDR. */
3704 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3706 tree virtual_size = c_sizeof_nowarn (type);
3707 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3708 virtual_size, NULL_TREE);
3711 /* Call the builtin operator delete. */
3712 return build_builtin_call (void_type_node, BID,
3713 build_tree_list (NULL_TREE, addr));
3715 parms = build_tree_list (NULL_TREE, addr);
3717 /* Below, we will reverse the order in which these calls are made.
3718 If we have a destructor, then that destructor will take care
3719 of the base classes; otherwise, we must do that here. */
3720 if (TYPE_HAS_DESTRUCTOR (type))
3722 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3723 tree basetypes = TYPE_BINFO (type);
3725 if (flags & LOOKUP_PROTECT)
3727 enum access_type access = compute_access (basetypes, dtor);
3729 if (access == access_private)
3731 if (flags & LOOKUP_COMPLAIN)
3732 cp_error ("destructor for type `%T' is private in this scope", type);
3733 return error_mark_node;
3735 else if (access == access_protected)
3737 if (flags & LOOKUP_COMPLAIN)
3738 cp_error ("destructor for type `%T' is protected in this scope", type);
3739 return error_mark_node;
3743 /* Once we are in a destructor, try not going through
3744 the virtual function table to find the next destructor. */
3745 if (DECL_VINDEX (dtor)
3746 && ! (flags & LOOKUP_NONVIRTUAL)
3747 && TREE_CODE (auto_delete) != PARM_DECL
3748 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3750 tree binfo, basetype;
3751 /* The code below is probably all broken. See call.c for the
3752 complete right way to do this. this offsets may not be right
3753 in the below. (mrs) */
3754 /* This destructor must be called via virtual function table. */
3755 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3756 basetype = DECL_CLASS_CONTEXT (dtor);
3757 binfo = get_binfo (basetype,
3758 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3760 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3761 if (expr != TREE_VALUE (parms))
3764 ref = build_indirect_ref (expr, NULL_PTR);
3765 TREE_VALUE (parms) = expr;
3767 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3768 if (function == error_mark_node)
3769 return error_mark_node;
3770 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3771 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3772 expr = build_function_call (function, parms);
3773 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3775 /* Handle the case where a virtual destructor is
3776 being called on an item that is 0.
3778 @@ Does this really need to be done? */
3779 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3781 if (TREE_CODE (ref) == VAR_DECL
3782 || TREE_CODE (ref) == COMPONENT_REF)
3783 warning ("losing in build_delete");
3785 expr = build (COND_EXPR, void_type_node,
3786 ifexp, expr, void_zero_node);
3793 if ((flags & LOOKUP_DESTRUCTOR)
3794 || TREE_CODE (ref) == VAR_DECL
3795 || TREE_CODE (ref) == PARM_DECL
3796 || TREE_CODE (ref) == COMPONENT_REF
3797 || TREE_CODE (ref) == ARRAY_REF)
3798 /* These can't be 0. */
3799 ifexp = integer_one_node;
3801 /* Handle the case where a non-virtual destructor is
3802 being called on an item that is 0. */
3803 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3805 /* Used to mean that this destructor was known to be empty,
3806 but that's now obsolete. */
3807 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3809 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3810 expr = build_function_call (dtor, parms);
3812 if (ifexp != integer_one_node)
3813 expr = build (COND_EXPR, void_type_node,
3814 ifexp, expr, void_zero_node);
3820 /* This can get visibilities wrong. */
3821 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3822 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3823 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3824 tree exprstmt = NULL_TREE;
3825 tree parent_auto_delete = auto_delete;
3828 /* If this type does not have a destructor, but does have
3829 operator delete, call the parent parent destructor (if any),
3830 but let this node do the deleting. Otherwise, it is ok
3831 to let the parent destructor do the deleting. */
3832 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3834 parent_auto_delete = integer_zero_node;
3835 if (auto_delete == integer_zero_node)
3841 /* This is probably wrong. It should be the size of the
3842 virtual object being deleted. */
3843 virtual_size = c_sizeof_nowarn (type);
3845 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3846 virtual_size, NULL_TREE);
3847 if (expr == error_mark_node)
3848 return error_mark_node;
3849 if (auto_delete != integer_one_node)
3850 cond = build (COND_EXPR, void_type_node,
3851 build (BIT_AND_EXPR, integer_type_node,
3852 auto_delete, integer_one_node),
3853 expr, void_zero_node);
3858 else if (base_binfo == NULL_TREE
3859 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3860 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3864 /* This is probably wrong. It should be the size of the virtual
3865 object being deleted. */
3866 virtual_size = c_sizeof_nowarn (type);
3868 cond = build (COND_EXPR, void_type_node,
3869 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3870 build_builtin_call (void_type_node, BID,
3871 build_tree_list (NULL_TREE, addr)),
3878 exprstmt = build_tree_list (NULL_TREE, cond);
3881 && ! TREE_VIA_VIRTUAL (base_binfo)
3882 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3884 tree this_auto_delete;
3886 if (BINFO_OFFSET_ZEROP (base_binfo))
3887 this_auto_delete = parent_auto_delete;
3889 this_auto_delete = integer_zero_node;
3891 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3892 this_auto_delete, flags, 0);
3893 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3896 /* Take care of the remaining baseclasses. */
3897 for (i = 1; i < n_baseclasses; i++)
3899 base_binfo = TREE_VEC_ELT (binfos, i);
3900 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3901 || TREE_VIA_VIRTUAL (base_binfo))
3904 /* May be zero offset if other baseclasses are virtual. */
3905 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3906 addr, BINFO_OFFSET (base_binfo)));
3908 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3912 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3915 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3917 if (TREE_CODE (member) != FIELD_DECL)
3919 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3921 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3922 tree this_type = TREE_TYPE (member);
3923 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3924 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3929 return build_compound_expr (exprstmt);
3930 /* Virtual base classes make this function do nothing. */
3931 return void_zero_node;
3935 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3938 build_vbase_delete (type, decl)
3941 tree vbases = CLASSTYPE_VBASECLASSES (type);
3942 tree result = NULL_TREE;
3943 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3945 my_friendly_assert (addr != error_mark_node, 222);
3949 tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
3951 result = tree_cons (NULL_TREE,
3952 build_delete (TREE_TYPE (this_addr), this_addr,
3954 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3956 vbases = TREE_CHAIN (vbases);
3958 return build_compound_expr (nreverse (result));
3961 /* Build a C++ vector delete expression.
3962 MAXINDEX is the number of elements to be deleted.
3963 ELT_SIZE is the nominal size of each element in the vector.
3964 BASE is the expression that should yield the store to be deleted.
3965 This function expands (or synthesizes) these calls itself.
3966 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3967 AUTO_DELETE say whether each item in the container should be deallocated.
3969 This also calls delete for virtual baseclasses of elements of the vector.
3971 Update: MAXINDEX is no longer needed. The size can be extracted from the
3972 start of the vector for pointers, and from the type for arrays. We still
3973 use MAXINDEX for arrays because it happens to already have one of the
3974 values we'd have to extract. (We could use MAXINDEX with pointers to
3975 confirm the size, and trap if the numbers differ; not clear that it'd
3976 be worth bothering.) */
3978 build_vec_delete (base, maxindex, elt_size, auto_delete_vec, auto_delete,
3980 tree base, maxindex, elt_size;
3981 tree auto_delete_vec, auto_delete;
3982 int use_global_delete;
3984 tree ptype = TREE_TYPE (base);
3987 /* Temporary variables used by the loop. */
3988 tree tbase, size_exp, tbase_init;
3990 /* This is the body of the loop that implements the deletion of a
3991 single element, and moves temp variables to next elements. */
3994 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3997 /* This is the thing that governs what to do after the loop has run. */
3998 tree deallocate_expr = 0;
4000 /* This is the BIND_EXPR which holds the outermost iterator of the
4001 loop. It is convenient to set this variable up and test it before
4002 executing any other code in the loop.
4003 This is also the containing expression returned by this function. */
4004 tree controller = NULL_TREE;
4006 /* This is the BLOCK to record the symbol binding for debugging. */
4009 base = stabilize_reference (base);
4011 /* Since we can use base many times, save_expr it. */
4012 if (TREE_SIDE_EFFECTS (base))
4013 base = save_expr (base);
4015 if (TREE_CODE (ptype) == POINTER_TYPE)
4017 /* Step back one from start of vector, and read dimension. */
4018 tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
4019 base, BI_header_size);
4020 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
4021 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
4023 ptype = TREE_TYPE (ptype);
4024 while (TREE_CODE (ptype) == ARRAY_TYPE);
4026 else if (TREE_CODE (ptype) == ARRAY_TYPE)
4028 /* get the total number of things in the array, maxindex is a bad name */
4029 maxindex = array_type_nelts_total (ptype);
4030 while (TREE_CODE (ptype) == ARRAY_TYPE)
4031 ptype = TREE_TYPE (ptype);
4032 base = build_unary_op (ADDR_EXPR, base, 1);
4036 error ("type to vector delete is neither pointer or array type");
4037 return error_mark_node;
4040 ptype = TYPE_POINTER_TO (type);
4042 size_exp = size_in_bytes (type);
4044 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
4046 loop = integer_zero_node;
4050 /* The below is short by BI_header_size */
4051 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4053 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4054 tbase_init = build_modify_expr (tbase, NOP_EXPR,
4055 fold (build (PLUS_EXPR, ptype,
4058 DECL_REGISTER (tbase) = 1;
4059 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4060 TREE_SIDE_EFFECTS (controller) = 1;
4061 block = build_block (tbase, 0, 0, 0, 0);
4062 add_block_current_level (block);
4064 if (auto_delete != integer_zero_node
4065 && auto_delete != integer_two_node)
4067 tree base_tbd = convert (ptype,
4068 build_binary_op (MINUS_EXPR,
4069 convert (ptr_type_node, base),
4072 /* This is the real size */
4073 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4074 body = build_tree_list (NULL_TREE,
4075 build_x_delete (ptype, base_tbd,
4076 2 | use_global_delete,
4078 body = build (COND_EXPR, void_type_node,
4079 build (BIT_AND_EXPR, integer_type_node,
4080 auto_delete, integer_one_node),
4081 body, integer_zero_node);
4086 body = tree_cons (NULL_TREE,
4087 build_delete (ptype, tbase, auto_delete,
4088 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
4091 body = tree_cons (NULL_TREE,
4092 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4095 body = tree_cons (NULL_TREE,
4096 build (EXIT_EXPR, void_type_node,
4097 build (EQ_EXPR, integer_type_node, base, tbase)),
4100 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4102 loop = tree_cons (NULL_TREE, tbase_init,
4103 tree_cons (NULL_TREE, loop, NULL_TREE));
4104 loop = build_compound_expr (loop);
4107 /* If the delete flag is one, or anything else with the low bit set,
4108 delete the storage. */
4109 if (auto_delete_vec == integer_zero_node
4110 || auto_delete_vec == integer_two_node)
4111 deallocate_expr = integer_zero_node;
4116 /* The below is short by BI_header_size */
4117 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4119 if (! TYPE_VEC_NEW_USES_COOKIE (type))
4124 base_tbd = convert (ptype,
4125 build_binary_op (MINUS_EXPR,
4126 convert (string_type_node, base),
4129 /* True size with header. */
4130 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4132 deallocate_expr = build_x_delete (ptype, base_tbd,
4133 2 | use_global_delete,
4135 if (auto_delete_vec != integer_one_node)
4136 deallocate_expr = build (COND_EXPR, void_type_node,
4137 build (BIT_AND_EXPR, integer_type_node,
4138 auto_delete_vec, integer_one_node),
4139 deallocate_expr, integer_zero_node);
4142 if (loop && deallocate_expr != integer_zero_node)
4144 body = tree_cons (NULL_TREE, loop,
4145 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4146 body = build_compound_expr (body);
4151 /* Outermost wrapper: If pointer is null, punt. */
4152 body = build (COND_EXPR, void_type_node,
4153 build (NE_EXPR, integer_type_node, base, integer_zero_node),
4154 body, integer_zero_node);
4155 body = build1 (NOP_EXPR, void_type_node, body);
4159 TREE_OPERAND (controller, 1) = body;
4163 return convert (void_type_node, body);