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));
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. ADDR is the
105 pointer to the object whos vtables we are going to initialize.
107 REAL_BINFO is usually the same as BINFO, except when addr is not of
108 pointer to the type of the real derived type that we want to
109 initialize for. This is the case when addr is a pointer to a sub
110 object of a complete object, and we only want to do part of the
111 complete object's initiailzation of vtable pointers. This is done
112 for all virtual table pointers in virtual base classes. REAL_BINFO
113 is used to find the BINFO_VTABLE that we initialize with. BINFO is
114 used for conversions of addr to subobjects.
116 BINFO_TYPE (real_binfo) must be BINFO_TYPE (binfo).
118 Relies upon binfo being inside TYPE_BINFO (TREE_TYPE (TREE_TYPE
121 expand_direct_vtbls_init (real_binfo, binfo, init_self, can_elide, addr)
122 tree real_binfo, binfo, addr;
123 int init_self, can_elide;
125 tree real_binfos = BINFO_BASETYPES (real_binfo);
126 tree binfos = BINFO_BASETYPES (binfo);
127 int i, n_baselinks = real_binfos ? TREE_VEC_LENGTH (real_binfos) : 0;
129 for (i = 0; i < n_baselinks; i++)
131 tree real_base_binfo = TREE_VEC_ELT (real_binfos, i);
132 tree base_binfo = TREE_VEC_ELT (binfos, i);
133 int is_not_base_vtable =
134 i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (real_binfo));
135 if (! TREE_VIA_VIRTUAL (real_base_binfo))
136 expand_direct_vtbls_init (real_base_binfo, base_binfo,
137 is_not_base_vtable, can_elide, addr);
140 /* Before turning this on, make sure it is correct. */
141 if (can_elide && ! BINFO_MODIFIED (binfo))
144 /* Should we use something besides CLASSTYPE_VFIELDS? */
145 if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (real_binfo)))
147 tree base_ptr = convert_pointer_to_real (binfo, addr);
148 expand_virtual_init (real_binfo, base_ptr);
153 /* Subroutine of emit_base_init. */
155 perform_member_init (member, name, init, explicit)
156 tree member, name, init;
160 tree type = TREE_TYPE (member);
162 if (TYPE_NEEDS_CONSTRUCTING (type)
163 || (init && TYPE_HAS_CONSTRUCTOR (type)))
165 /* Since `init' is already a TREE_LIST on the current_member_init_list,
166 only build it into one if we aren't already a list. */
167 if (init != NULL_TREE && TREE_CODE (init) != TREE_LIST)
168 init = build_tree_list (NULL_TREE, init);
170 decl = build_component_ref (C_C_D, name, 0, explicit);
173 && TREE_CODE (type) == ARRAY_TYPE
175 && TREE_CHAIN (init) == NULL_TREE
176 && TREE_CODE (TREE_TYPE (TREE_VALUE (init))) == ARRAY_TYPE)
178 /* Initialization of one array from another. */
179 expand_vec_init (TREE_OPERAND (decl, 1), decl,
180 array_type_nelts (type), TREE_VALUE (init), 1);
183 expand_aggr_init (decl, init, 0);
187 if (init == NULL_TREE)
191 cp_error ("incomplete initializer for member `%D' of class `%T' which has no constructor",
192 member, current_class_type);
193 init = error_mark_node;
195 /* member traversal: note it leaves init NULL */
196 else if (TREE_CODE (TREE_TYPE (member)) == REFERENCE_TYPE)
197 cp_pedwarn ("uninitialized reference member `%D'", member);
199 else if (TREE_CODE (init) == TREE_LIST)
201 /* There was an explicit member initialization. Do some
202 work in that case. */
203 if (TREE_CHAIN (init))
205 warning ("initializer list treated as compound expression");
206 init = build_compound_expr (init);
209 init = TREE_VALUE (init);
212 /* We only build this with a null init if we got it from the
213 current_member_init_list. */
214 if (init || explicit)
216 decl = build_component_ref (C_C_D, name, 0, explicit);
217 expand_expr_stmt (build_modify_expr (decl, INIT_EXPR, init));
221 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (type))
223 cplus_expand_start_try (1);
224 push_exception_cleanup (build_unary_op (ADDR_EXPR, decl, 0));
228 /* Subroutine of emit_member_init. */
233 tree x, member, name, field, init;
234 tree init_list = NULL_TREE;
235 tree fields_to_unmark = NULL_TREE;
238 for (member = TYPE_FIELDS (t); member ; member = TREE_CHAIN (member))
241 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
243 /* If we cleared this out, then pay no attention to it. */
244 if (TREE_PURPOSE (x) == NULL_TREE)
246 name = TREE_PURPOSE (x);
249 field = (TREE_CODE (name) == COMPONENT_REF
250 ? TREE_OPERAND (name, 1) : IDENTIFIER_CLASS_VALUE (name));
252 /* Let's find out when this happens. */
253 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 348);
254 field = IDENTIFIER_CLASS_VALUE (name);
257 /* If one member shadows another, get the outermost one. */
258 if (TREE_CODE (field) == TREE_LIST)
259 field = TREE_VALUE (field);
263 /* See if we already found an initializer for this field. */
266 if (DECL_NAME (field))
267 cp_error ("multiple initializations given for member `%D'",
272 init_list = chainon (init_list,
273 build_tree_list (name, TREE_VALUE (x)));
274 /* Make sure we won't try to work on this init again. */
275 TREE_PURPOSE (x) = NULL_TREE;
281 /* If we didn't find MEMBER in the list, create a dummy entry
282 so the two lists (INIT_LIST and the list of members) will be
285 init_list = chainon (init_list, build_tree_list (NULL_TREE, NULL_TREE));
288 for (x = current_member_init_list ; x ; x = TREE_CHAIN (x))
290 if (TREE_PURPOSE (x))
292 name = TREE_PURPOSE (x);
293 init = TREE_VALUE (x);
294 /* XXX: this may need the COMPONENT_REF operand 0 check if
295 it turns out we actually get them. */
296 field = IDENTIFIER_CLASS_VALUE (name);
298 /* If one member shadows another, get the outermost one. */
299 if (TREE_CODE (field) == TREE_LIST)
301 field = TREE_VALUE (field);
302 if (decl_type_context (field) != current_class_type)
303 cp_error ("field `%D' not in immediate context", field);
307 /* It turns out if you have an anonymous union in the
308 class, a member from it can end up not being on the
309 list of fields (rather, the type is), and therefore
310 won't be seen by the for loop above. */
312 /* The code in this for loop is derived from a general loop
313 which had this check in it. Theoretically, we've hit
314 every initialization for the list of members in T, so
315 we shouldn't have anything but these left in this list. */
316 my_friendly_assert (DECL_FIELD_CONTEXT (field) != t, 351);
319 if (TREE_HAS_CONSTRUCTOR (field))
321 if (DECL_NAME (field))
322 error ("multiple initializations given for member `%s'",
323 IDENTIFIER_POINTER (DECL_NAME (field)));
327 TREE_HAS_CONSTRUCTOR (field) = 1;
328 fields_to_unmark = tree_cons (NULL_TREE, field, fields_to_unmark);
330 perform_member_init (field, name, init, 1);
331 TREE_PURPOSE (x) = NULL_TREE;
335 /* Unmark fields which are initialized for the base class. */
336 while (fields_to_unmark)
338 TREE_HAS_CONSTRUCTOR (TREE_VALUE (fields_to_unmark)) = 0;
339 /* XXX is this a memory leak? */
340 fields_to_unmark = TREE_CHAIN (fields_to_unmark);
346 /* Perform whatever initializations have yet to be done on the base
347 class of the class variable. These actions are in the global
348 variable CURRENT_BASE_INIT_LIST. Such an action could be
349 NULL_TREE, meaning that the user has explicitly called the base
350 class constructor with no arguments.
352 If there is a need for a call to a constructor, we must surround
353 that call with a pushlevel/poplevel pair, since we are technically
354 at the PARM level of scope.
356 Argument IMMEDIATELY, if zero, forces a new sequence to be
357 generated to contain these new insns, so it can be emitted later.
358 This sequence is saved in the global variable BASE_INIT_INSNS.
359 Otherwise, the insns are emitted into the current sequence.
361 Note that emit_base_init does *not* initialize virtual base
362 classes. That is done specially, elsewhere. */
365 emit_base_init (t, immediately)
369 extern tree in_charge_identifier;
374 tree t_binfo = TYPE_BINFO (t);
375 tree binfos = BINFO_BASETYPES (t_binfo);
376 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
377 int have_init_list = 0, from_init_list;
381 do_pending_stack_adjust ();
385 if (write_symbols == NO_DEBUG)
386 /* As a matter of principle, `start_sequence' should do this. */
389 /* Always emit a line number note so we can step into constructors. */
390 emit_line_note_force (DECL_SOURCE_FILE (current_function_decl),
391 DECL_SOURCE_LINE (current_function_decl));
393 /* In this case, we always need IN_CHARGE_NODE, because we have
394 to know whether to deallocate or not before exiting. */
395 if (flag_handle_exceptions == 2
396 && lookup_name (in_charge_identifier, 0) == NULL_TREE)
398 tree in_charge_node = pushdecl (build_decl (VAR_DECL, in_charge_identifier,
400 store_init_value (in_charge_node, build (EQ_EXPR, integer_type_node,
403 expand_decl (in_charge_node);
404 expand_decl_init (in_charge_node);
407 start = ! TYPE_USES_VIRTUAL_BASECLASSES (t);
408 for (pass = start; pass < 2; pass++)
410 tree vbase_init_list = NULL_TREE;
412 for (init_list = current_base_init_list; init_list;
413 init_list = TREE_CHAIN (init_list))
415 tree basename = TREE_PURPOSE (init_list);
417 tree init = TREE_VALUE (init_list);
419 if (basename == NULL_TREE)
421 /* Initializer for single base class. Must not
422 use multiple inheritance or this is ambiguous. */
423 switch (n_baseclasses)
426 error ("type `%s' does not have a base class to initialize",
427 IDENTIFIER_POINTER (current_class_name));
432 error ("unnamed initializer ambiguous for type `%s' which uses multiple inheritance", IDENTIFIER_POINTER (current_class_name));
435 binfo = TREE_VEC_ELT (binfos, 0);
437 else if (is_aggr_typedef (basename, 1))
439 binfo = binfo_or_else (IDENTIFIER_TYPE_VALUE (basename), t);
440 if (binfo == NULL_TREE)
443 /* Virtual base classes are special cases. Their initializers
444 are recorded with this constructor, and they are used when
445 this constructor is the top-level constructor called. */
446 if (! TREE_VIA_VIRTUAL (binfo))
448 /* Otherwise, if it is not an immediate base class, complain. */
449 for (i = n_baseclasses-1; i >= 0; i--)
450 if (BINFO_TYPE (binfo) == BINFO_TYPE (TREE_VEC_ELT (binfos, i)))
454 error ("type `%s' is not an immediate base class of type `%s'",
455 IDENTIFIER_POINTER (basename),
456 IDENTIFIER_POINTER (current_class_name));
464 /* The base initialization list goes up to the first
465 base class which can actually use it. */
469 char *msgp = (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
470 ? "cannot pass initialization up to class `%s'" : 0;
472 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo))
473 && BINFO_BASETYPES (binfo) != NULL_TREE
474 && TREE_VEC_LENGTH (BINFO_BASETYPES (binfo)) == 1)
476 /* ?? This should be fixed in RENO by forcing
477 default constructors to exist. */
478 SET_BINFO_BASEINIT_MARKED (binfo);
479 binfo = BINFO_BASETYPE (binfo, 0);
482 /* We used to give an error if this wasn't true, saying that
483 there's no constructor for the initialization of basename.
484 This turned out to be incorrect---it should use the
485 default constructor, since a user could try to initialize
486 the class in a derived class's base initializer list. */
487 if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (binfo)))
492 error_with_aggr_type (binfo, msgp);
498 if (BINFO_BASEINIT_MARKED (binfo))
500 msgp = "class `%s' initializer already specified";
501 error (msgp, IDENTIFIER_POINTER (basename));
507 SET_BINFO_BASEINIT_MARKED (binfo);
508 if (TREE_VIA_VIRTUAL (binfo))
510 vbase_init_list = tree_cons (init, BINFO_TYPE (binfo),
517 else if (TREE_VIA_VIRTUAL (binfo))
520 member = convert_pointer_to (binfo, current_class_decl);
521 expand_aggr_init_1 (t_binfo, 0,
522 build_indirect_ref (member, NULL_PTR), init,
523 BINFO_OFFSET_ZEROP (binfo), LOOKUP_COMPLAIN);
524 if (flag_handle_exceptions == 2 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (binfo)))
526 cplus_expand_start_try (1);
527 push_exception_cleanup (member);
533 tree first_arg = TREE_CHAIN (DECL_ARGUMENTS (current_function_decl));
536 if (DECL_NAME (current_function_decl) == NULL_TREE
537 && TREE_CHAIN (first_arg) != NULL_TREE)
539 /* If there are virtual baseclasses without initialization
540 specified, and this is a default X(X&) constructor,
541 build the initialization list so that each virtual baseclass
542 of the new object is initialized from the virtual baseclass
543 of the incoming arg. */
544 tree init_arg = build_unary_op (ADDR_EXPR, TREE_CHAIN (first_arg), 0);
545 for (vbases = CLASSTYPE_VBASECLASSES (t);
546 vbases; vbases = TREE_CHAIN (vbases))
548 if (BINFO_BASEINIT_MARKED (vbases) == 0)
550 member = convert_pointer_to (vbases, init_arg);
551 if (member == init_arg)
552 member = TREE_CHAIN (first_arg);
554 TREE_TYPE (member) = build_reference_type (BINFO_TYPE (vbases));
555 vbase_init_list = tree_cons (convert_from_reference (member),
556 vbases, vbase_init_list);
557 SET_BINFO_BASEINIT_MARKED (vbases);
561 expand_start_cond (first_arg, 0);
562 expand_aggr_vbase_init (t_binfo, C_C_D, current_class_decl,
567 current_base_init_list = NULL_TREE;
569 /* Now, perform default initialization of all base classes which
570 have not yet been initialized, and unmark baseclasses which
571 have been initialized. */
572 for (i = 0; i < n_baseclasses; i++)
574 tree base = current_class_decl;
575 tree base_binfo = TREE_VEC_ELT (binfos, i);
577 if (TYPE_NEEDS_CONSTRUCTING (BINFO_TYPE (base_binfo)))
579 if (! TREE_VIA_VIRTUAL (base_binfo)
580 && ! BINFO_BASEINIT_MARKED (base_binfo))
584 if (BINFO_OFFSET_ZEROP (base_binfo))
585 base = build1 (NOP_EXPR,
586 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
589 base = build (PLUS_EXPR,
590 TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
591 current_class_decl, BINFO_OFFSET (base_binfo));
593 ref = build_indirect_ref (base, NULL_PTR);
594 expand_aggr_init_1 (t_binfo, 0, ref, NULL_TREE,
595 BINFO_OFFSET_ZEROP (base_binfo),
597 if (flag_handle_exceptions == 2
598 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
600 cplus_expand_start_try (1);
601 push_exception_cleanup (base);
605 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
607 if (! TYPE_USES_VIRTUAL_BASECLASSES (t))
609 while (! TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo))
610 && BINFO_BASETYPES (base_binfo) != NULL_TREE
611 && TREE_VEC_LENGTH (BINFO_BASETYPES (base_binfo)) == 1)
613 /* ?? This should be fixed in RENO by forcing
614 default constructors to exist. It is needed for symmetry
616 base_binfo = BINFO_BASETYPE (base_binfo, 0);
617 CLEAR_BINFO_BASEINIT_MARKED (base_binfo);
622 /* Initialize all the virtual function table fields that
623 do come from virtual base classes. */
624 if (TYPE_USES_VIRTUAL_BASECLASSES (t))
625 expand_indirect_vtbls_init (t_binfo, C_C_D, current_class_decl, 0);
626 for (vbases = CLASSTYPE_VBASECLASSES (t); vbases; vbases = TREE_CHAIN (vbases))
627 CLEAR_BINFO_BASEINIT_MARKED (vbases);
629 /* Initialize all the virtual function table fields that
630 do not come from virtual base classes. */
631 expand_direct_vtbls_init (t_binfo, t_binfo, 1, 1, current_class_decl);
633 if (current_member_init_list)
635 init_list = sort_member_init (t);
639 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
644 /* See if we had a user-specified member initialization. */
647 if (TREE_PURPOSE (init_list))
649 name = TREE_PURPOSE (init_list);
650 init = TREE_VALUE (init_list);
653 if (TREE_STATIC (member))
655 error_with_aggr_type (DECL_FIELD_CONTEXT (member),
656 "field `%s::%s' is static; only point of initialization is its declaration",
657 IDENTIFIER_POINTER (TREE_PURPOSE (init_list)));
661 /* Also see if it's ever a COMPONENT_REF here. If it is, we
662 need to do `expand_assignment (name, init, 0, 0);' and
664 my_friendly_assert (TREE_CODE (name) != COMPONENT_REF, 349);
667 init_list = TREE_CHAIN (init_list);
670 if (! from_init_list)
672 /* member could be, for example, a CONST_DECL for an enumerated
673 tag; we don't want to try to initialize that, since it already
675 if (TREE_CODE (member) != FIELD_DECL)
678 name = DECL_NAME (member);
679 init = DECL_INITIAL (member);
682 perform_member_init (member, name, init, from_init_list);
685 current_member_init_list = NULL_TREE;
687 /* It is possible for the initializers to need cleanups.
688 Expand those cleanups now that all the initialization
690 expand_cleanups_to (NULL_TREE);
694 extern rtx base_init_insns;
696 do_pending_stack_adjust ();
697 my_friendly_assert (base_init_insns == 0, 207);
698 base_init_insns = get_insns ();
702 /* All the implicit try blocks we built up will be zapped
703 when we come to a real binding contour boundary. */
706 /* Check that all fields are properly initialized after
707 an assignment to `this'. */
713 for (member = TYPE_FIELDS (t); member; member = TREE_CHAIN (member))
714 if (DECL_NAME (member) && TREE_USED (member))
715 cp_error ("field `%D' used before initialized (after assignment to `this')",
719 /* This code sets up the virtual function tables appropriate for
720 the pointer DECL. It is a one-ply initialization.
722 BINFO is the exact type that DECL is supposed to be. In
723 multiple inheritance, this might mean "C's A" if C : A, B. */
725 expand_virtual_init (binfo, decl)
728 tree type = BINFO_TYPE (binfo);
730 tree vtype, vtype_binfo;
732 /* This code is crusty. Should be simple, like:
733 vtbl = BINFO_VTABLE (binfo);
735 vtype = DECL_CONTEXT (CLASSTYPE_VFIELD (type));
736 vtype_binfo = get_binfo (vtype, TREE_TYPE (TREE_TYPE (decl)), 0);
737 vtbl = BINFO_VTABLE (binfo_value (DECL_FIELD_CONTEXT (CLASSTYPE_VFIELD (type)), binfo));
738 if (!flag_vtable_thunks)
739 assemble_external (vtbl);
740 TREE_USED (vtbl) = 1;
741 vtbl = build1 (ADDR_EXPR, TYPE_POINTER_TO (TREE_TYPE (vtbl)), vtbl);
742 decl = convert_pointer_to_real (vtype_binfo, decl);
743 vtbl_ptr = build_vfield_ref (build_indirect_ref (decl, NULL_PTR), vtype);
744 if (vtbl_ptr == error_mark_node)
747 /* Have to convert VTBL since array sizes may be different. */
748 vtbl = convert_force (TREE_TYPE (vtbl_ptr), vtbl);
749 expand_expr_stmt (build_modify_expr (vtbl_ptr, NOP_EXPR, vtbl));
752 /* Subroutine of `expand_aggr_vbase_init'.
753 BINFO is the binfo of the type that is being initialized.
754 INIT_LIST is the list of initializers for the virtual baseclass. */
756 expand_aggr_vbase_init_1 (binfo, exp, addr, init_list)
757 tree binfo, exp, addr, init_list;
759 tree init = value_member (BINFO_TYPE (binfo), init_list);
760 tree ref = build_indirect_ref (addr, NULL_PTR);
762 init = TREE_PURPOSE (init);
763 /* Call constructors, but don't set up vtables. */
764 expand_aggr_init_1 (binfo, exp, ref, init, 0,
765 LOOKUP_COMPLAIN|LOOKUP_SPECULATIVELY);
766 CLEAR_BINFO_VBASE_INIT_MARKED (binfo);
769 /* Initialize this object's virtual base class pointers. This must be
770 done only at the top-level of the object being constructed.
772 INIT_LIST is list of initialization for constructor to perform. */
774 expand_aggr_vbase_init (binfo, exp, addr, init_list)
780 tree type = BINFO_TYPE (binfo);
782 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
784 tree result = init_vbase_pointers (type, addr);
788 expand_expr_stmt (build_compound_expr (result));
790 /* Mark everything as having an initializer
791 (either explicit or default). */
792 for (vbases = CLASSTYPE_VBASECLASSES (type);
793 vbases; vbases = TREE_CHAIN (vbases))
794 SET_BINFO_VBASE_INIT_MARKED (vbases);
796 /* First, initialize baseclasses which could be baseclasses
797 for other virtual baseclasses. */
798 for (vbases = CLASSTYPE_VBASECLASSES (type);
799 vbases; vbases = TREE_CHAIN (vbases))
800 /* Don't initialize twice. */
801 if (BINFO_VBASE_INIT_MARKED (vbases))
805 while (BINFO_TYPE (vbases) != BINFO_TYPE (TREE_PURPOSE (tmp)))
806 tmp = TREE_CHAIN (tmp);
807 expand_aggr_vbase_init_1 (vbases, exp,
808 TREE_OPERAND (TREE_VALUE (tmp), 0),
812 /* Now initialize the baseclasses which don't have virtual baseclasses. */
813 for (; result; result = TREE_CHAIN (result))
814 /* Don't initialize twice. */
815 if (BINFO_VBASE_INIT_MARKED (TREE_PURPOSE (result)))
817 my_friendly_abort (47);
818 expand_aggr_vbase_init_1 (TREE_PURPOSE (result), exp,
819 TREE_OPERAND (TREE_VALUE (result), 0),
825 /* Subroutine to perform parser actions for member initialization.
826 S_ID is the scoped identifier.
827 NAME is the name of the member.
828 INIT is the initializer, or `void_type_node' if none. */
830 do_member_init (s_id, name, init)
831 tree s_id, name, init;
835 if (current_class_type == NULL_TREE
836 || ! is_aggr_typedef (s_id, 1))
838 binfo = get_binfo (IDENTIFIER_TYPE_VALUE (s_id),
839 current_class_type, 1);
840 if (binfo == error_mark_node)
844 error_not_base_type (IDENTIFIER_TYPE_VALUE (s_id), current_class_type);
848 base = convert_pointer_to (binfo, current_class_decl);
849 expand_member_init (build_indirect_ref (base, NULL_PTR), name, init);
852 /* Function to give error message if member initialization specification
853 is erroneous. FIELD is the member we decided to initialize.
854 TYPE is the type for which the initialization is being performed.
855 FIELD must be a member of TYPE, or the base type from which FIELD
856 comes must not need a constructor.
858 MEMBER_NAME is the name of the member. */
861 member_init_ok_or_else (field, type, member_name)
866 if (field == error_mark_node)
868 if (field == NULL_TREE)
870 cp_error ("class `%T' does not have any field named `%s'", type,
874 if (DECL_CONTEXT (field) != type
875 && TYPE_NEEDS_CONSTRUCTING (DECL_CONTEXT (field)))
877 cp_error ("member `%D' comes from base class needing constructor",
884 /* If NAME is a viable field name for the aggregate DECL,
885 and PARMS is a viable parameter list, then expand an _EXPR
886 which describes this initialization.
888 Note that we do not need to chase through the class's base classes
889 to look for NAME, because if it's in that list, it will be handled
890 by the constructor for that base class.
892 We do not yet have a fixed-point finder to instantiate types
893 being fed to overloaded constructors. If there is a unique
894 constructor, then argument types can be got from that one.
896 If INIT is non-NULL, then it the initialization should
897 be placed in `current_base_init_list', where it will be processed
898 by `emit_base_init'. */
900 expand_member_init (exp, name, init)
901 tree exp, name, init;
903 extern tree ptr_type_node; /* should be in tree.h */
905 tree basetype = NULL_TREE, field;
910 if (exp == NULL_TREE)
911 return; /* complain about this later */
913 type = TYPE_MAIN_VARIANT (TREE_TYPE (exp));
915 if (name == NULL_TREE && IS_AGGR_TYPE (type))
916 switch (CLASSTYPE_N_BASECLASSES (type))
919 error ("base class initializer specified, but no base class to initialize");
922 basetype = TYPE_BINFO_BASETYPE (type, 0);
925 error ("initializer for unnamed base class ambiguous");
926 cp_error ("(type `%T' uses multiple inheritance)", type);
932 /* The grammar should not allow fields which have names
933 that are TYPENAMEs. Therefore, if the field has
934 a non-NULL TREE_TYPE, we may assume that this is an
935 attempt to initialize a base class member of the current
936 type. Otherwise, it is an attempt to initialize a
939 if (init == void_type_node)
942 if (name == NULL_TREE || IDENTIFIER_HAS_TYPE_VALUE (name))
946 if (name == NULL_TREE)
950 name = TYPE_IDENTIFIER (basetype);
953 error ("no base class to initialize");
960 basetype = IDENTIFIER_TYPE_VALUE (name);
962 && ! binfo_member (basetype, TYPE_BINFO (type))
963 && ! binfo_member (basetype, CLASSTYPE_VBASECLASSES (type)))
965 if (IDENTIFIER_CLASS_VALUE (name))
967 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
968 error ("type `%s' is not an immediate or virtual basetype for `%s'",
969 IDENTIFIER_POINTER (name),
970 TYPE_NAME_STRING (type));
972 error ("type `%s' is not an immediate basetype for `%s'",
973 IDENTIFIER_POINTER (name),
974 TYPE_NAME_STRING (type));
979 if (purpose_member (name, current_base_init_list))
981 error ("base class `%s' already initialized",
982 IDENTIFIER_POINTER (name));
986 base_init = build_tree_list (name, init);
987 TREE_TYPE (base_init) = basetype;
988 current_base_init_list = chainon (current_base_init_list, base_init);
995 field = lookup_field (type, name, 1, 0);
997 if (! member_init_ok_or_else (field, type, IDENTIFIER_POINTER (name)))
1000 if (purpose_member (name, current_member_init_list))
1002 error ("field `%s' already initialized", IDENTIFIER_POINTER (name));
1006 member_init = build_tree_list (name, init);
1007 TREE_TYPE (member_init) = TREE_TYPE (field);
1008 current_member_init_list = chainon (current_member_init_list, member_init);
1012 else if (name == NULL_TREE)
1014 compiler_error ("expand_member_init: name == NULL_TREE");
1019 field = lookup_field (basetype, name, 0, 0);
1021 if (! member_init_ok_or_else (field, basetype, IDENTIFIER_POINTER (name)))
1024 /* now see if there is a constructor for this type
1025 which will take these args. */
1027 if (TYPE_HAS_CONSTRUCTOR (TREE_TYPE (field)))
1029 tree parmtypes, fndecl;
1031 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1033 /* just know that we've seen something for this node */
1034 DECL_INITIAL (exp) = error_mark_node;
1035 TREE_USED (exp) = 1;
1037 type = TYPE_MAIN_VARIANT (TREE_TYPE (field));
1038 actual_name = TYPE_IDENTIFIER (type);
1039 parm = build_component_ref (exp, name, 0, 0);
1041 /* Now get to the constructor. */
1042 fndecl = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0);
1043 /* Get past destructor, if any. */
1044 if (TYPE_HAS_DESTRUCTOR (type))
1045 fndecl = DECL_CHAIN (fndecl);
1048 my_friendly_assert (TREE_CODE (fndecl) == FUNCTION_DECL, 209);
1050 /* If the field is unique, we can use the parameter
1051 types to guide possible type instantiation. */
1052 if (DECL_CHAIN (fndecl) == NULL_TREE)
1054 /* There was a confusion here between
1055 FIELD and FNDECL. The following code
1056 should be correct, but abort is here
1058 my_friendly_abort (48);
1059 parmtypes = FUNCTION_ARG_CHAIN (fndecl);
1063 parmtypes = NULL_TREE;
1067 init = convert_arguments (parm, parmtypes, NULL_TREE, fndecl, LOOKUP_NORMAL);
1068 if (init == NULL_TREE || TREE_TYPE (init) != error_mark_node)
1069 rval = build_method_call (NULL_TREE, actual_name, init, NULL_TREE, LOOKUP_NORMAL);
1073 if (rval != error_mark_node)
1075 /* Now, fill in the first parm with our guy */
1076 TREE_VALUE (TREE_OPERAND (rval, 1))
1077 = build_unary_op (ADDR_EXPR, parm, 0);
1078 TREE_TYPE (rval) = ptr_type_node;
1079 TREE_SIDE_EFFECTS (rval) = 1;
1082 else if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (field)))
1084 parm = build_component_ref (exp, name, 0, 0);
1085 expand_aggr_init (parm, NULL_TREE, 0);
1086 rval = error_mark_node;
1089 /* Now initialize the member. It does not have to
1090 be of aggregate type to receive initialization. */
1091 if (rval != error_mark_node)
1092 expand_expr_stmt (rval);
1095 /* This is like `expand_member_init', only it stores one aggregate
1098 INIT comes in two flavors: it is either a value which
1099 is to be stored in EXP, or it is a parameter list
1100 to go to a constructor, which will operate on EXP.
1101 If `init' is a CONSTRUCTOR, then we emit a warning message,
1102 explaining that such initializations are illegal.
1104 ALIAS_THIS is nonzero iff we are initializing something which is
1105 essentially an alias for C_C_D. In this case, the base constructor
1106 may move it on us, and we must keep track of such deviations.
1108 If INIT resolves to a CALL_EXPR which happens to return
1109 something of the type we are looking for, then we know
1110 that we can safely use that call to perform the
1113 The virtual function table pointer cannot be set up here, because
1114 we do not really know its type.
1116 Virtual baseclass pointers are also set up here.
1118 This never calls operator=().
1120 When initializing, nothing is CONST.
1122 A default copy constructor may have to be used to perform the
1125 A constructor or a conversion operator may have to be used to
1126 perform the initialization, but not both, as it would be ambiguous.
1130 expand_aggr_init (exp, init, alias_this)
1134 tree type = TREE_TYPE (exp);
1135 int was_const = TREE_READONLY (exp);
1137 if (init == error_mark_node)
1140 TREE_READONLY (exp) = 0;
1142 if (TREE_CODE (type) == ARRAY_TYPE)
1144 /* Must arrange to initialize each element of EXP
1145 from elements of INIT. */
1146 int was_const_elts = TYPE_READONLY (TREE_TYPE (type));
1147 tree itype = init ? TREE_TYPE (init) : NULL_TREE;
1150 tree atype = build_cplus_array_type (TYPE_MAIN_VARIANT (TREE_TYPE (type)),
1151 TYPE_DOMAIN (type));
1152 if (init && (TREE_TYPE (exp) == TREE_TYPE (init)))
1153 TREE_TYPE (init) = atype;
1154 TREE_TYPE (exp) = atype;
1156 if (init && TREE_TYPE (init) == NULL_TREE)
1158 /* Handle bad initializers like:
1162 COMPLEX(double r = 0.0, double i = 0.0) {re = r; im = i;};
1166 int main(int argc, char **argv) {
1167 COMPLEX zees(1.0, 0.0)[10];
1170 error ("bad array initializer");
1173 expand_vec_init (exp, exp, array_type_nelts (type), init,
1174 init && comptypes (TREE_TYPE (init), TREE_TYPE (exp), 1));
1175 TREE_READONLY (exp) = was_const;
1176 TREE_TYPE (exp) = type;
1177 if (init) TREE_TYPE (init) = itype;
1181 if (TREE_CODE (exp) == VAR_DECL || TREE_CODE (exp) == PARM_DECL)
1182 /* just know that we've seen something for this node */
1183 TREE_USED (exp) = 1;
1186 /* If initializing from a GNU C CONSTRUCTOR, consider the elts in the
1187 constructor as parameters to an implicit GNU C++ constructor. */
1188 if (init && TREE_CODE (init) == CONSTRUCTOR
1189 && TYPE_HAS_CONSTRUCTOR (type)
1190 && TREE_TYPE (init) == type)
1191 init = CONSTRUCTOR_ELTS (init);
1193 expand_aggr_init_1 (TYPE_BINFO (type), exp, exp,
1194 init, alias_this, LOOKUP_NORMAL);
1195 TREE_READONLY (exp) = was_const;
1199 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags)
1207 /* It fails because there may not be a constructor which takes
1208 its own type as the first (or only parameter), but which does
1209 take other types via a conversion. So, if the thing initializing
1210 the expression is a unit element of type X, first try X(X&),
1211 followed by initialization by X. If neither of these work
1212 out, then look hard. */
1215 int xxref_init_possible;
1217 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
1220 if (parms) init = TREE_VALUE (parms);
1222 else if (TREE_CODE (init) == INDIRECT_REF && TREE_HAS_CONSTRUCTOR (init))
1224 rval = convert_for_initialization (exp, type, init, 0, 0, 0, 0);
1225 expand_expr_stmt (rval);
1229 parms = build_tree_list (NULL_TREE, init);
1231 if (TYPE_HAS_INIT_REF (type)
1232 || init == NULL_TREE
1233 || TREE_CHAIN (parms) != NULL_TREE)
1234 xxref_init_possible = 0;
1237 xxref_init_possible = LOOKUP_SPECULATIVELY;
1238 flags &= ~LOOKUP_COMPLAIN;
1241 if (TYPE_USES_VIRTUAL_BASECLASSES (type))
1243 if (true_exp == exp)
1244 parms = tree_cons (NULL_TREE, integer_one_node, parms);
1246 parms = tree_cons (NULL_TREE, integer_zero_node, parms);
1247 flags |= LOOKUP_HAS_IN_CHARGE;
1250 rval = build_method_call (exp, constructor_name_full (type),
1251 parms, binfo, flags|xxref_init_possible);
1252 if (rval == NULL_TREE && xxref_init_possible)
1254 /* It is an error to implement a default copy constructor if
1255 (see ARM 12.8 for details) ... one case being if another
1256 copy constructor already exists. */
1257 tree init_type = TREE_TYPE (init);
1258 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1259 init_type = TREE_TYPE (init_type);
1260 if (TYPE_MAIN_VARIANT (init_type) == TYPE_MAIN_VARIANT (type)
1261 || (IS_AGGR_TYPE (init_type)
1262 && UNIQUELY_DERIVED_FROM_P (type, init_type)))
1264 if (type == BINFO_TYPE (binfo)
1265 && TYPE_USES_VIRTUAL_BASECLASSES (type))
1267 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1268 expand_aggr_vbase_init (binfo, exp, addr, NULL_TREE);
1270 expand_indirect_vtbls_init (binfo, exp, addr, 1);
1272 expand_expr_stmt (build_modify_expr (exp, INIT_EXPR, init));
1276 rval = build_method_call (exp, constructor_name_full (type), parms,
1280 /* Private, protected, or otherwise unavailable. */
1281 if (rval == error_mark_node && (flags&LOOKUP_COMPLAIN))
1282 cp_error ("in base initialization for class `%T'", binfo);
1283 /* A valid initialization using constructor. */
1284 else if (rval != error_mark_node && rval != NULL_TREE)
1286 /* p. 222: if the base class assigns to `this', then that
1287 value is used in the derived class. */
1288 if ((flag_this_is_variable & 1) && alias_this)
1290 TREE_TYPE (rval) = TREE_TYPE (current_class_decl);
1291 expand_assignment (current_class_decl, rval, 0, 0);
1294 expand_expr_stmt (rval);
1296 else if (parms && TREE_CHAIN (parms) == NULL_TREE)
1298 /* If we are initializing one aggregate value
1299 from another, and though there are constructors,
1300 and none accept the initializer, just do a bitwise
1303 The above sounds wrong, ``If a class has any copy
1304 constructor defined, the default copy constructor will
1305 not be generated.'' 12.8 Copying Class Objects (mrs)
1307 @@ This should reject initializer which a constructor
1308 @@ rejected on access gounds, but there is
1309 @@ no way right now to recognize that case with
1310 @@ just `error_mark_node'. */
1312 init = TREE_VALUE (parms);
1313 itype = TREE_TYPE (init);
1314 if (TREE_CODE (itype) == REFERENCE_TYPE)
1316 init = convert_from_reference (init);
1317 itype = TREE_TYPE (init);
1319 itype = TYPE_MAIN_VARIANT (itype);
1321 /* This is currently how the default X(X&) constructor
1323 if (comptypes (TYPE_MAIN_VARIANT (type), itype, 0))
1326 warning ("bitwise copy in initialization of type `%s'",
1327 TYPE_NAME_STRING (type));
1329 rval = build (INIT_EXPR, type, exp, init);
1330 expand_expr_stmt (rval);
1334 cp_error ("in base initialization for class `%T',", binfo);
1335 cp_error ("invalid initializer to constructor for type `%T'", type);
1341 if (init == NULL_TREE)
1342 my_friendly_assert (parms == NULL_TREE, 210);
1343 if (parms == NULL_TREE && TREE_VIA_VIRTUAL (binfo))
1344 cp_error ("virtual baseclass `%T' does not have default initializer", binfo);
1347 cp_error ("in base initialization for class `%T',", binfo);
1348 /* This will make an error message for us. */
1349 build_method_call (exp, constructor_name_full (type), parms, binfo,
1350 (TYPE_USES_VIRTUAL_BASECLASSES (type)
1351 ? LOOKUP_NORMAL|LOOKUP_HAS_IN_CHARGE
1356 /* Constructor has been called, but vtables may be for TYPE
1357 rather than for FOR_TYPE. */
1360 /* This function is responsible for initializing EXP with INIT
1363 BINFO is the binfo of the type for who we are performing the
1364 initialization. For example, if W is a virtual base class of A and B,
1366 If we are initializing B, then W must contain B's W vtable, whereas
1367 were we initializing C, W must contain C's W vtable.
1369 TRUE_EXP is nonzero if it is the true expression being initialized.
1370 In this case, it may be EXP, or may just contain EXP. The reason we
1371 need this is because if EXP is a base element of TRUE_EXP, we
1372 don't necessarily know by looking at EXP where its virtual
1373 baseclass fields should really be pointing. But we do know
1374 from TRUE_EXP. In constructors, we don't know anything about
1375 the value being initialized.
1377 ALIAS_THIS serves the same purpose it serves for expand_aggr_init.
1379 FLAGS is just passes to `build_method_call'. See that function for
1383 expand_aggr_init_1 (binfo, true_exp, exp, init, alias_this, flags)
1390 tree type = TREE_TYPE (exp);
1391 tree init_type = NULL_TREE;
1393 my_friendly_assert (init != error_mark_node && type != error_mark_node, 211);
1395 /* Use a function returning the desired type to initialize EXP for us.
1396 If the function is a constructor, and its first argument is
1397 NULL_TREE, know that it was meant for us--just slide exp on
1398 in and expand the constructor. Constructors now come
1402 tree init_list = NULL_TREE;
1404 if (TREE_CODE (init) == TREE_LIST)
1407 if (TREE_CHAIN (init) == NULL_TREE)
1408 init = TREE_VALUE (init);
1411 init_type = TREE_TYPE (init);
1413 if (TREE_CODE (init) != TREE_LIST)
1415 if (TREE_CODE (init_type) == ERROR_MARK)
1419 /* These lines are found troublesome 5/11/89. */
1420 if (TREE_CODE (init_type) == REFERENCE_TYPE)
1421 init_type = TREE_TYPE (init_type);
1424 /* This happens when we use C++'s functional cast notation.
1425 If the types match, then just use the TARGET_EXPR
1426 directly. Otherwise, we need to create the initializer
1427 separately from the object being initialized. */
1428 if (TREE_CODE (init) == TARGET_EXPR)
1430 if (init_type == type)
1432 if (TREE_CODE (exp) == VAR_DECL
1433 || TREE_CODE (exp) == RESULT_DECL)
1434 /* Unify the initialization targets. */
1435 DECL_RTL (TREE_OPERAND (init, 0)) = DECL_RTL (exp);
1437 DECL_RTL (TREE_OPERAND (init, 0)) = expand_expr (exp, NULL_RTX, 0, 0);
1439 expand_expr_stmt (init);
1444 init = TREE_OPERAND (init, 1);
1445 init = build (CALL_EXPR, init_type,
1446 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1447 TREE_SIDE_EFFECTS (init) = 1;
1449 TREE_RAISES (init) = ??
1452 TREE_VALUE (init_list) = init;
1456 if (init_type == type && TREE_CODE (init) == CALL_EXPR
1458 /* It is legal to directly initialize from a CALL_EXPR
1459 without going through X(X&), apparently. */
1460 && ! TYPE_GETS_INIT_REF (type)
1464 /* A CALL_EXPR is a legitimate form of initialization, so
1465 we should not print this warning message. */
1467 /* Should have gone away due to 5/11/89 change. */
1468 if (TREE_CODE (TREE_TYPE (init)) == REFERENCE_TYPE)
1469 init = convert_from_reference (init);
1471 expand_assignment (exp, init, 0, 0);
1472 if (exp == DECL_RESULT (current_function_decl))
1474 /* Failing this assertion means that the return value
1475 from receives multiple initializations. */
1476 my_friendly_assert (DECL_INITIAL (exp) == NULL_TREE
1477 || DECL_INITIAL (exp) == error_mark_node,
1479 DECL_INITIAL (exp) = init;
1483 else if (init_type == type
1484 && TREE_CODE (init) == COND_EXPR)
1486 /* Push value to be initialized into the cond, where possible.
1487 Avoid spurious warning messages when initializing the
1488 result of this function. */
1489 TREE_OPERAND (init, 1)
1490 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 1));
1491 if (exp == DECL_RESULT (current_function_decl))
1492 DECL_INITIAL (exp) = NULL_TREE;
1493 TREE_OPERAND (init, 2)
1494 = build_modify_expr (exp, INIT_EXPR, TREE_OPERAND (init, 2));
1495 if (exp == DECL_RESULT (current_function_decl))
1496 DECL_INITIAL (exp) = init;
1497 TREE_SIDE_EFFECTS (init) = 1;
1498 expand_expr (init, const0_rtx, VOIDmode, 0);
1504 /* We did not know what we were initializing before. Now we do. */
1505 if (TREE_CODE (init) == TARGET_EXPR)
1507 tree tmp = TREE_OPERAND (TREE_OPERAND (init, 1), 1);
1509 if (TREE_CODE (TREE_VALUE (tmp)) == NOP_EXPR
1510 && TREE_OPERAND (TREE_VALUE (tmp), 0) == integer_zero_node)
1512 /* In order for this to work for RESULT_DECLs, if their
1513 type has a constructor, then they must be BLKmode
1514 so that they will be meaningfully addressable. */
1515 tree arg = build_unary_op (ADDR_EXPR, exp, 0);
1516 init = TREE_OPERAND (init, 1);
1517 init = build (CALL_EXPR, build_pointer_type (TREE_TYPE (init)),
1518 TREE_OPERAND (init, 0), TREE_OPERAND (init, 1), 0);
1519 TREE_SIDE_EFFECTS (init) = 1;
1521 TREE_RAISES (init) = ??
1523 TREE_VALUE (TREE_OPERAND (init, 1))
1524 = convert_pointer_to (TREE_TYPE (TREE_TYPE (TREE_VALUE (tmp))), arg);
1528 expand_assignment (current_function_decl, init, 0, 0);
1531 if (exp == DECL_RESULT (current_function_decl))
1533 if (DECL_INITIAL (DECL_RESULT (current_function_decl)))
1534 fatal ("return value from function receives multiple initializations");
1535 DECL_INITIAL (exp) = init;
1537 expand_expr_stmt (init);
1542 if (TREE_CODE (exp) == VAR_DECL
1543 && TREE_CODE (init) == CONSTRUCTOR
1544 && TREE_HAS_CONSTRUCTOR (init))
1546 tree t = store_init_value (exp, init);
1549 expand_decl_init (exp);
1552 t = build (INIT_EXPR, type, exp, init);
1553 TREE_SIDE_EFFECTS (t) = 1;
1554 expand_expr_stmt (t);
1558 /* Handle this case: when calling a constructor: xyzzy foo(bar);
1559 which really means: xyzzy foo = bar; Ugh!
1561 More useful for this case: xyzzy *foo = new xyzzy (bar); */
1563 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
1565 if (init_list && TREE_CHAIN (init_list))
1567 warning ("initializer list being treated as compound expression");
1568 init = convert (type, build_compound_expr (init_list));
1569 if (init == error_mark_node)
1573 expand_assignment (exp, init, 0, 0);
1577 /* See whether we can go through a type conversion operator.
1578 This wins over going through a non-existent constructor. If
1579 there is a constructor, it is ambiguous. */
1580 if (TREE_CODE (init) != TREE_LIST)
1582 tree ttype = TREE_CODE (init_type) == REFERENCE_TYPE
1583 ? TREE_TYPE (init_type) : init_type;
1585 if (ttype != type && IS_AGGR_TYPE (ttype))
1587 tree rval = build_type_conversion (CONVERT_EXPR, type, init, 0);
1591 /* See if there is a constructor for``type'' that takes a
1592 ``ttype''-typed object. */
1593 tree parms = build_tree_list (NULL_TREE, init);
1594 tree as_cons = NULL_TREE;
1595 if (TYPE_HAS_CONSTRUCTOR (type))
1596 as_cons = build_method_call (exp, constructor_name_full (type),
1598 LOOKUP_SPECULATIVELY|LOOKUP_NO_CONVERSION);
1599 if (as_cons != NULL_TREE && as_cons != error_mark_node)
1600 /* ANSI C++ June 5 1992 WP 12.3.2.6.1 */
1601 cp_error ("ambiguity between conversion to `%T' and constructor",
1604 expand_assignment (exp, rval, 0, 0);
1611 /* Handle default copy constructors here, does not matter if there is
1612 a constructor or not. */
1613 if (type == init_type && IS_AGGR_TYPE (type)
1614 && init && TREE_CODE (init) != TREE_LIST)
1615 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1616 /* Not sure why this is here... */
1617 else if (TYPE_HAS_CONSTRUCTOR (type))
1618 expand_default_init (binfo, true_exp, exp, type, init, alias_this, flags);
1619 else if (TREE_CODE (type) == ARRAY_TYPE)
1621 if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (type)))
1622 expand_vec_init (exp, exp, array_type_nelts (type), init, 0);
1623 else if (TYPE_VIRTUAL_P (TREE_TYPE (type)))
1624 sorry ("arrays of objects with virtual functions but no constructors");
1627 expand_recursive_init (binfo, true_exp, exp, init,
1628 CLASSTYPE_BASE_INIT_LIST (type), alias_this);
1631 /* A pointer which holds the initializer. First call to
1632 expand_aggr_init gets this value pointed to, and sets it to init_null. */
1633 static tree *init_ptr, init_null;
1635 /* Subroutine of expand_recursive_init:
1637 ADDR is the address of the expression being initialized.
1638 INIT_LIST is the cons-list of initializations to be performed.
1639 ALIAS_THIS is its same, lovable self. */
1641 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this)
1642 tree binfo, true_exp, addr;
1648 if (TREE_PURPOSE (init_list))
1650 if (TREE_CODE (TREE_PURPOSE (init_list)) == FIELD_DECL)
1652 tree member = TREE_PURPOSE (init_list);
1653 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1654 tree member_base = build (COMPONENT_REF, TREE_TYPE (member), subexp, member);
1655 if (IS_AGGR_TYPE (TREE_TYPE (member)))
1656 expand_aggr_init (member_base, DECL_INITIAL (member), 0);
1657 else if (TREE_CODE (TREE_TYPE (member)) == ARRAY_TYPE
1658 && TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (member)))
1660 member_base = save_expr (default_conversion (member_base));
1661 expand_vec_init (member, member_base,
1662 array_type_nelts (TREE_TYPE (member)),
1663 DECL_INITIAL (member), 0);
1666 expand_expr_stmt (build_modify_expr (member_base, INIT_EXPR, DECL_INITIAL (member)));
1668 else if (TREE_CODE (TREE_PURPOSE (init_list)) == TREE_LIST)
1670 expand_recursive_init_1 (binfo, true_exp, addr, TREE_PURPOSE (init_list), alias_this);
1671 expand_recursive_init_1 (binfo, true_exp, addr, TREE_VALUE (init_list), alias_this);
1673 else if (TREE_CODE (TREE_PURPOSE (init_list)) == ERROR_MARK)
1675 /* Only initialize the virtual function tables if we
1676 are initializing the ultimate users of those vtables. */
1677 if (TREE_VALUE (init_list))
1679 /* We have to ensure that the first argment to
1680 expand_virtual_init is in binfo's hierarchy. */
1681 /* Is it the case that this is exactly the right binfo? */
1682 /* If it is ok, then fixup expand_virtual_init, to make
1684 expand_virtual_init (get_binfo (TREE_VALUE (init_list), binfo, 0),
1686 if (TREE_VALUE (init_list) == binfo
1687 && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1688 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
1692 my_friendly_abort (49);
1694 else if (TREE_VALUE (init_list)
1695 && TREE_CODE (TREE_VALUE (init_list)) == TREE_VEC)
1697 tree subexp = build_indirect_ref (convert_pointer_to (TREE_VALUE (init_list), addr), NULL_PTR);
1698 expand_aggr_init_1 (binfo, true_exp, subexp, *init_ptr,
1699 alias_this && BINFO_OFFSET_ZEROP (TREE_VALUE (init_list)),
1702 /* INIT_PTR is used up. */
1703 init_ptr = &init_null;
1706 my_friendly_abort (50);
1707 init_list = TREE_CHAIN (init_list);
1711 /* Initialize EXP with INIT. Type EXP does not have a constructor,
1712 but it has a baseclass with a constructor or a virtual function
1713 table which needs initializing.
1715 INIT_LIST is a cons-list describing what parts of EXP actually
1716 need to be initialized. INIT is given to the *unique*, first
1717 constructor within INIT_LIST. If there are multiple first
1718 constructors, such as with multiple inheritance, INIT must
1719 be zero or an ambiguity error is reported.
1721 ALIAS_THIS is passed from `expand_aggr_init'. See comments
1725 expand_recursive_init (binfo, true_exp, exp, init, init_list, alias_this)
1726 tree binfo, true_exp, exp, init;
1730 tree *old_init_ptr = init_ptr;
1731 tree addr = build_unary_op (ADDR_EXPR, exp, 0);
1734 if (true_exp == exp && TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
1736 expand_aggr_vbase_init (binfo, exp, addr, init_list);
1737 expand_indirect_vtbls_init (binfo, true_exp, addr, 1);
1739 expand_recursive_init_1 (binfo, true_exp, addr, init_list, alias_this);
1743 tree type = TREE_TYPE (exp);
1745 if (TREE_CODE (type) == REFERENCE_TYPE)
1746 type = TREE_TYPE (type);
1747 if (IS_AGGR_TYPE (type))
1748 cp_error ("unexpected argument to constructor `%T'", type);
1750 error ("unexpected argument to constructor");
1752 init_ptr = old_init_ptr;
1755 /* Report an error if NAME is not the name of a user-defined,
1756 aggregate type. If OR_ELSE is nonzero, give an error message. */
1758 is_aggr_typedef (name, or_else)
1764 if (name == error_mark_node)
1767 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1768 type = IDENTIFIER_TYPE_VALUE (name);
1772 cp_error ("`%T' is not an aggregate typedef", name);
1776 if (! IS_AGGR_TYPE (type)
1777 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1780 cp_error ("`%T' is not an aggregate type", type);
1786 /* Like is_aggr_typedef, but returns typedef if successful. */
1788 get_aggr_from_typedef (name, or_else)
1794 if (name == error_mark_node)
1797 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1798 type = IDENTIFIER_TYPE_VALUE (name);
1802 cp_error ("`%T' fails to be an aggregate typedef", name);
1806 if (! IS_AGGR_TYPE (type)
1807 && TREE_CODE (type) != TEMPLATE_TYPE_PARM)
1810 cp_error ("type `%T' is of non-aggregate type", type);
1817 get_type_value (name)
1820 if (name == error_mark_node)
1823 if (IDENTIFIER_HAS_TYPE_VALUE (name))
1824 return IDENTIFIER_TYPE_VALUE (name);
1830 /* This code could just as well go in `class.c', but is placed here for
1833 /* For an expression of the form CNAME :: NAME (PARMLIST), build
1834 the appropriate function call. */
1836 build_member_call (cname, name, parmlist)
1837 tree cname, name, parmlist;
1840 tree method_name = name;
1842 int dont_use_this = 0;
1843 tree basetype_path, decl;
1845 if (TREE_CODE (method_name) == BIT_NOT_EXPR)
1847 method_name = TREE_OPERAND (method_name, 0);
1851 if (TREE_CODE (cname) == SCOPE_REF)
1852 cname = resolve_scope_to_name (NULL_TREE, cname);
1854 if (cname == NULL_TREE || ! (type = get_aggr_from_typedef (cname, 1)))
1855 return error_mark_node;
1857 /* An operator we did not like. */
1858 if (name == NULL_TREE)
1859 return error_mark_node;
1864 /* Everything can explicitly call a destructor; see 12.4 */
1865 if (! TYPE_HAS_DESTRUCTOR (type))
1866 cp_error ("type `%#T' does not have a destructor", type);
1869 cp_error ("cannot call destructor `%T::~%T' without object", type,
1871 return error_mark_node;
1874 /* No object? Then just fake one up, and let build_method_call
1875 figure out what to do. */
1876 if (current_class_type == 0
1877 || get_base_distance (type, current_class_type, 0, &basetype_path) == -1)
1882 basetype_path = TYPE_BINFO (type);
1883 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1885 else if (current_class_decl == 0)
1888 decl = build1 (NOP_EXPR, TYPE_POINTER_TO (type), error_mark_node);
1892 tree olddecl = current_class_decl;
1893 tree oldtype = TREE_TYPE (TREE_TYPE (olddecl));
1894 if (oldtype != type)
1896 tree newtype = build_type_variant (type, TYPE_READONLY (oldtype),
1897 TYPE_VOLATILE (oldtype));
1898 decl = convert_force (TYPE_POINTER_TO (newtype), olddecl);
1904 decl = build_indirect_ref (decl, NULL_PTR);
1906 if (t = lookup_fnfields (basetype_path, method_name, 0))
1907 return build_method_call (decl, method_name, parmlist, basetype_path,
1908 LOOKUP_NORMAL|LOOKUP_NONVIRTUAL);
1909 if (TREE_CODE (name) == IDENTIFIER_NODE
1910 && ((t = lookup_field (TYPE_BINFO (type), name, 1, 0))))
1912 if (t == error_mark_node)
1913 return error_mark_node;
1914 if (TREE_CODE (t) == FIELD_DECL)
1918 cp_error ("invalid use of non-static field `%D'", t);
1919 return error_mark_node;
1921 decl = build (COMPONENT_REF, TREE_TYPE (t), decl, t);
1923 else if (TREE_CODE (t) == VAR_DECL)
1927 cp_error ("invalid use of member `%D'", t);
1928 return error_mark_node;
1930 if (TYPE_LANG_SPECIFIC (TREE_TYPE (decl))
1931 && TYPE_OVERLOADS_CALL_EXPR (TREE_TYPE (decl)))
1932 return build_opfncall (CALL_EXPR, LOOKUP_NORMAL, decl, parmlist, NULL_TREE);
1933 return build_function_call (decl, parmlist);
1937 cp_error ("no method `%T::%D'", type, name);
1938 return error_mark_node;
1942 /* Build a reference to a member of an aggregate. This is not a
1943 C++ `&', but really something which can have its address taken,
1944 and then act as a pointer to member, for example CNAME :: FIELD
1945 can have its address taken by saying & CNAME :: FIELD.
1947 @@ Prints out lousy diagnostics for operator <typename>
1950 @@ This function should be rewritten and placed in search.c. */
1952 build_offset_ref (cname, name)
1955 tree decl, type, fnfields, fields, t = error_mark_node;
1956 tree basetypes = NULL_TREE;
1959 if (TREE_CODE (cname) == SCOPE_REF)
1960 cname = resolve_scope_to_name (NULL_TREE, cname);
1962 if (cname == NULL_TREE || ! is_aggr_typedef (cname, 1))
1963 return error_mark_node;
1965 type = IDENTIFIER_TYPE_VALUE (cname);
1967 if (TREE_CODE (name) == BIT_NOT_EXPR)
1970 name = TREE_OPERAND (name, 0);
1973 if (TYPE_SIZE (type) == 0)
1975 t = IDENTIFIER_CLASS_VALUE (name);
1978 cp_error ("incomplete type `%T' does not have member `%D'", type,
1980 return error_mark_node;
1982 if (TREE_CODE (t) == TYPE_DECL || TREE_CODE (t) == VAR_DECL
1983 || TREE_CODE (t) == CONST_DECL)
1988 if (TREE_CODE (t) == FIELD_DECL)
1989 sorry ("use of member in incomplete aggregate type");
1990 else if (TREE_CODE (t) == FUNCTION_DECL)
1991 sorry ("use of member function in incomplete aggregate type");
1993 my_friendly_abort (52);
1994 return error_mark_node;
1998 if (TREE_CODE (name) == TYPE_EXPR)
1999 /* Pass a TYPE_DECL to build_component_type_expr. */
2000 return build_component_type_expr (TYPE_NAME (TREE_TYPE (cname)),
2001 name, NULL_TREE, 1);
2004 fnfields = lookup_fnfields (TYPE_BINFO (type), name, 1);
2005 fields = lookup_field (type, name, 0, 0);
2007 if (fields == error_mark_node || fnfields == error_mark_node)
2008 return error_mark_node;
2010 if (current_class_type == 0
2011 || get_base_distance (type, current_class_type, 0, &basetypes) == -1)
2013 basetypes = TYPE_BINFO (type);
2014 decl = build1 (NOP_EXPR,
2015 IDENTIFIER_TYPE_VALUE (cname),
2018 else if (current_class_decl == 0)
2019 decl = build1 (NOP_EXPR, IDENTIFIER_TYPE_VALUE (cname),
2024 /* A lot of this logic is now handled in lookup_field and
2028 basetypes = TREE_PURPOSE (fnfields);
2030 /* Go from the TREE_BASELINK to the member function info. */
2031 t = TREE_VALUE (fnfields);
2035 if (DECL_FIELD_CONTEXT (fields) == DECL_FIELD_CONTEXT (t))
2037 error ("ambiguous member reference: member `%s' defined as both field and function",
2038 IDENTIFIER_POINTER (name));
2039 return error_mark_node;
2041 if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (fields), DECL_FIELD_CONTEXT (t)))
2043 else if (UNIQUELY_DERIVED_FROM_P (DECL_FIELD_CONTEXT (t), DECL_FIELD_CONTEXT (fields)))
2047 error ("ambiguous member reference: member `%s' derives from distinct classes in multiple inheritance lattice");
2048 return error_mark_node;
2052 if (t == TREE_VALUE (fnfields))
2054 extern int flag_save_memoized_contexts;
2056 /* This does not handle access checking yet. */
2057 if (DECL_CHAIN (t) == NULL_TREE || dtor)
2059 enum access_type access;
2061 /* unique functions are handled easily. */
2063 access = compute_access (basetypes, t);
2064 if (access == access_protected)
2066 cp_error_at ("member function `%#D' is protected", t);
2067 error ("in this context");
2068 return error_mark_node;
2070 if (access == access_private)
2072 cp_error_at ("member function `%#D' is private", t);
2073 error ("in this context");
2074 return error_mark_node;
2076 assemble_external (t);
2077 return build (OFFSET_REF, TREE_TYPE (t), decl, t);
2080 /* overloaded functions may need more work. */
2083 if (TYPE_HAS_DESTRUCTOR (type)
2084 && DECL_CHAIN (DECL_CHAIN (t)) == NULL_TREE)
2090 /* FNFIELDS is most likely allocated on the search_obstack,
2091 which will go away after this class scope. If we need
2092 to save this value for later (either for memoization
2093 or for use as an initializer for a static variable), then
2096 ??? The smart thing to do for the case of saving initializers
2097 is to resolve them before we're done with this scope. */
2098 if (!TREE_PERMANENT (fnfields)
2099 && ((flag_save_memoized_contexts && global_bindings_p ())
2100 || ! allocation_temporary_p ()))
2101 fnfields = copy_list (fnfields);
2102 t = build_tree_list (error_mark_node, fnfields);
2103 TREE_TYPE (t) = build_offset_type (type, unknown_type_node);
2108 /* Now that we know we are looking for a field, see if we
2109 have access to that field. Lookup_field will give us the
2112 t = lookup_field (basetypes, name, 1, 0);
2114 if (t == error_mark_node)
2115 return error_mark_node;
2119 cp_error ("`%D' is not a member of type `%T'", name,
2120 IDENTIFIER_TYPE_VALUE (cname));
2121 return error_mark_node;
2124 if (TREE_CODE (t) == TYPE_DECL)
2129 /* static class members and class-specific enum
2130 values can be returned without further ado. */
2131 if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == CONST_DECL)
2133 assemble_external (t);
2138 /* static class functions too. */
2139 if (TREE_CODE (t) == FUNCTION_DECL && TREE_CODE (TREE_TYPE (t)) == FUNCTION_TYPE)
2140 my_friendly_abort (53);
2142 /* In member functions, the form `cname::name' is no longer
2143 equivalent to `this->cname::name'. */
2144 return build (OFFSET_REF, build_offset_type (type, TREE_TYPE (t)), decl, t);
2147 /* Given an object EXP and a member function reference MEMBER,
2148 return the address of the actual member function. */
2150 get_member_function (exp_addr_ptr, exp, member)
2154 tree ctype = TREE_TYPE (exp);
2155 tree function = save_expr (build_unary_op (ADDR_EXPR, member, 0));
2157 if (TYPE_VIRTUAL_P (ctype)
2158 || (flag_all_virtual == 1 && TYPE_OVERLOADS_METHOD_CALL_EXPR (ctype)))
2163 /* Save away the unadulterated `this' pointer. */
2164 exp_addr = save_expr (*exp_addr_ptr);
2166 /* Cast function to signed integer. */
2167 e0 = build1 (NOP_EXPR, integer_type_node, function);
2169 /* There is a hack here that takes advantage of
2170 twos complement arithmetic, and the fact that
2171 there are more than one UNITS to the WORD.
2172 If the high bit is set for the `function',
2173 then we pretend it is a virtual function,
2174 and the array indexing will knock this bit
2175 out the top, leaving a valid index. */
2176 if (UNITS_PER_WORD <= 1)
2177 my_friendly_abort (54);
2179 e1 = build (GT_EXPR, integer_type_node, e0, integer_zero_node);
2180 e1 = build_compound_expr (tree_cons (NULL_TREE, exp_addr,
2181 build_tree_list (NULL_TREE, e1)));
2182 e1 = save_expr (e1);
2184 if (TREE_SIDE_EFFECTS (*exp_addr_ptr))
2186 exp = build_indirect_ref (exp_addr, NULL_PTR);
2187 *exp_addr_ptr = exp_addr;
2190 /* This is really hairy: if the function pointer is a pointer
2191 to a non-virtual member function, then we can't go mucking
2192 with the `this' pointer (any more than we already have to
2193 this point). If it is a pointer to a virtual member function,
2194 then we have to adjust the `this' pointer according to
2195 what the virtual function table tells us. */
2197 e3 = build_vfn_ref (exp_addr_ptr, exp, e0);
2198 my_friendly_assert (e3 != error_mark_node, 213);
2200 /* Change this pointer type from `void *' to the
2201 type it is really supposed to be. */
2202 TREE_TYPE (e3) = TREE_TYPE (function);
2204 /* If non-virtual, use what we had originally. Otherwise,
2205 use the value we get from the virtual function table. */
2206 *exp_addr_ptr = build_conditional_expr (e1, exp_addr, *exp_addr_ptr);
2208 function = build_conditional_expr (e1, function, e3);
2210 return build_indirect_ref (function, NULL_PTR);
2213 /* If a OFFSET_REF made it through to here, then it did
2214 not have its address taken. */
2217 resolve_offset_ref (exp)
2220 tree type = TREE_TYPE (exp);
2221 tree base = NULL_TREE;
2223 tree basetype, addr;
2225 if (TREE_CODE (exp) == TREE_LIST)
2226 return build_unary_op (ADDR_EXPR, exp, 0);
2228 if (TREE_CODE (exp) != OFFSET_REF)
2230 my_friendly_assert (TREE_CODE (type) == OFFSET_TYPE, 214);
2231 if (TYPE_OFFSET_BASETYPE (type) != current_class_type)
2233 error ("object missing in use of pointer-to-member construct");
2234 return error_mark_node;
2237 type = TREE_TYPE (type);
2242 member = TREE_OPERAND (exp, 1);
2243 base = TREE_OPERAND (exp, 0);
2246 if ((TREE_CODE (member) == VAR_DECL
2247 && ! TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2248 || TREE_CODE (TREE_TYPE (member)) == FUNCTION_TYPE)
2250 /* These were static members. */
2251 if (mark_addressable (member) == 0)
2252 return error_mark_node;
2256 /* Syntax error can cause a member which should
2257 have been seen as static to be grok'd as non-static. */
2258 if (TREE_CODE (member) == FIELD_DECL && C_C_D == NULL_TREE)
2260 if (TREE_ADDRESSABLE (member) == 0)
2262 cp_error_at ("member `%D' is non-static in static member function context", member);
2263 error ("at this point in file");
2264 TREE_ADDRESSABLE (member) = 1;
2266 return error_mark_node;
2269 /* The first case is really just a reference to a member of `this'. */
2270 if (TREE_CODE (member) == FIELD_DECL
2272 || (TREE_CODE (base) == NOP_EXPR
2273 && TREE_OPERAND (base, 0) == error_mark_node)))
2276 enum access_type access;
2278 if (TREE_CODE (exp) == OFFSET_REF && TREE_CODE (type) == OFFSET_TYPE)
2279 basetype = TYPE_OFFSET_BASETYPE (type);
2281 basetype = DECL_CONTEXT (member);
2283 base = current_class_decl;
2285 if (get_base_distance (basetype, TREE_TYPE (TREE_TYPE (base)), 0, &basetype_path) < 0)
2287 error_not_base_type (basetype, TREE_TYPE (TREE_TYPE (base)));
2288 return error_mark_node;
2290 addr = convert_pointer_to (basetype, base);
2291 access = compute_access (basetype_path, member);
2292 if (access == access_public)
2293 return build (COMPONENT_REF, TREE_TYPE (member),
2294 build_indirect_ref (addr, NULL_PTR), member);
2295 if (access == access_protected)
2297 cp_error_at ("member `%D' is protected", member);
2298 error ("in this context");
2299 return error_mark_node;
2301 if (access == access_private)
2303 cp_error_at ("member `%D' is private", member);
2304 error ("in this context");
2305 return error_mark_node;
2307 my_friendly_abort (55);
2310 /* If this is a reference to a member function, then return
2311 the address of the member function (which may involve going
2312 through the object's vtable), otherwise, return an expression
2313 for the dereferenced pointer-to-member construct. */
2314 addr = build_unary_op (ADDR_EXPR, base, 0);
2316 if (TREE_CODE (TREE_TYPE (member)) == METHOD_TYPE)
2318 basetype = DECL_CLASS_CONTEXT (member);
2319 addr = convert_pointer_to (basetype, addr);
2320 return build_unary_op (ADDR_EXPR, get_member_function (&addr, build_indirect_ref (addr, NULL_PTR), member), 0);
2322 else if (TREE_CODE (TREE_TYPE (member)) == OFFSET_TYPE)
2324 basetype = TYPE_OFFSET_BASETYPE (TREE_TYPE (member));
2325 addr = convert_pointer_to (basetype, addr);
2326 member = convert (ptr_type_node, build_unary_op (ADDR_EXPR, member, 0));
2327 return build1 (INDIRECT_REF, type,
2328 build (PLUS_EXPR, ptr_type_node, addr, member));
2330 else if (TYPE_PTRMEMFUNC_P (TREE_TYPE (member)))
2332 return get_member_function_from_ptrfunc (&addr, base, member);
2334 my_friendly_abort (56);
2339 /* Return either DECL or its known constant value (if it has one). */
2342 decl_constant_value (decl)
2345 if (! TREE_THIS_VOLATILE (decl)
2347 /* These may be necessary for C, but they break C++. */
2348 ! TREE_PUBLIC (decl)
2349 /* Don't change a variable array bound or initial value to a constant
2350 in a place where a variable is invalid. */
2353 && DECL_INITIAL (decl) != 0
2354 && TREE_CODE (DECL_INITIAL (decl)) != ERROR_MARK
2355 /* This is invalid if initial value is not constant.
2356 If it has either a function call, a memory reference,
2357 or a variable, then re-evaluating it could give different results. */
2358 && TREE_CONSTANT (DECL_INITIAL (decl))
2359 /* Check for cases where this is sub-optimal, even though valid. */
2360 && TREE_CODE (DECL_INITIAL (decl)) != CONSTRUCTOR
2362 /* We must allow this to work outside of functions so that
2363 static constants can be used for array sizes. */
2364 && current_function_decl != 0
2365 && DECL_MODE (decl) != BLKmode
2368 return DECL_INITIAL (decl);
2372 /* Friend handling routines. */
2373 /* Friend data structures:
2375 Lists of friend functions come from TYPE_DECL nodes. Since all
2376 aggregate types are automatically typedef'd, these nodes are guaranteed
2379 The TREE_PURPOSE of a friend list is the name of the friend,
2380 and its TREE_VALUE is another list.
2382 For each element of that list, either the TREE_VALUE or the TREE_PURPOSE
2383 will be filled in, but not both. The TREE_VALUE of that list is an
2384 individual function which is a friend. The TREE_PURPOSE of that list
2385 indicates a type in which all functions by that name are friends.
2387 Lists of friend classes come from _TYPE nodes. Love that consistency
2391 is_friend_type (type1, type2)
2394 return is_friend (type1, type2);
2398 is_friend (type, supplicant)
2399 tree type, supplicant;
2404 if (supplicant == NULL_TREE || type == NULL_TREE)
2407 declp = (TREE_CODE_CLASS (TREE_CODE (supplicant)) == 'd');
2410 /* It's a function decl. */
2412 tree list = DECL_FRIENDLIST (TYPE_NAME (type));
2413 tree name = DECL_NAME (supplicant);
2414 tree ctype = DECL_CLASS_CONTEXT (supplicant);
2415 for (; list ; list = TREE_CHAIN (list))
2417 if (name == TREE_PURPOSE (list))
2419 tree friends = TREE_VALUE (list);
2420 name = DECL_ASSEMBLER_NAME (supplicant);
2421 for (; friends ; friends = TREE_CHAIN (friends))
2423 if (ctype == TREE_PURPOSE (friends))
2425 if (name == DECL_ASSEMBLER_NAME (TREE_VALUE (friends)))
2435 if (type == supplicant)
2438 list = CLASSTYPE_FRIEND_CLASSES (TREE_TYPE (TYPE_NAME (type)));
2439 for (; list ; list = TREE_CHAIN (list))
2440 if (supplicant == TREE_VALUE (list))
2445 tree context = declp ? DECL_CLASS_CONTEXT (supplicant)
2446 : DECL_CONTEXT (TYPE_NAME (supplicant));
2449 return is_friend (type, context);
2455 /* Add a new friend to the friends of the aggregate type TYPE.
2456 DECL is the FUNCTION_DECL of the friend being added. */
2458 add_friend (type, decl)
2461 tree typedecl = TYPE_NAME (type);
2462 tree list = DECL_FRIENDLIST (typedecl);
2463 tree name = DECL_NAME (decl);
2467 if (name == TREE_PURPOSE (list))
2469 tree friends = TREE_VALUE (list);
2470 for (; friends ; friends = TREE_CHAIN (friends))
2472 if (decl == TREE_VALUE (friends))
2474 cp_pedwarn ("`%D' is already a friend of class `%T'",
2476 cp_pedwarn_at ("previous friend declaration of `%D'",
2477 TREE_VALUE (friends));
2481 TREE_VALUE (list) = tree_cons (error_mark_node, decl,
2485 list = TREE_CHAIN (list);
2487 DECL_FRIENDLIST (typedecl)
2488 = tree_cons (DECL_NAME (decl), build_tree_list (error_mark_node, decl),
2489 DECL_FRIENDLIST (typedecl));
2490 if (DECL_NAME (decl) == ansi_opname[(int) MODIFY_EXPR])
2492 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (decl));
2493 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2494 if (parmtypes && TREE_CHAIN (parmtypes))
2496 tree parmtype = TREE_VALUE (TREE_CHAIN (parmtypes));
2497 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2498 && TREE_TYPE (parmtypes) == TREE_TYPE (typedecl))
2499 TYPE_HAS_ASSIGN_REF (TREE_TYPE (typedecl)) = 1;
2504 /* Declare that every member function NAME in FRIEND_TYPE
2505 (which may be NULL_TREE) is a friend of type TYPE. */
2507 add_friends (type, name, friend_type)
2508 tree type, name, friend_type;
2510 tree typedecl = TYPE_NAME (type);
2511 tree list = DECL_FRIENDLIST (typedecl);
2515 if (name == TREE_PURPOSE (list))
2517 tree friends = TREE_VALUE (list);
2518 while (friends && TREE_PURPOSE (friends) != friend_type)
2519 friends = TREE_CHAIN (friends);
2522 warning ("method `%s::%s' is already a friend of class",
2523 TYPE_NAME_STRING (friend_type),
2524 IDENTIFIER_POINTER (name));
2526 warning ("function `%s' is already a friend of class `%s'",
2527 IDENTIFIER_POINTER (name),
2528 IDENTIFIER_POINTER (DECL_NAME (typedecl)));
2530 TREE_VALUE (list) = tree_cons (friend_type, NULL_TREE,
2534 list = TREE_CHAIN (list);
2536 DECL_FRIENDLIST (typedecl) =
2538 build_tree_list (friend_type, NULL_TREE),
2539 DECL_FRIENDLIST (typedecl));
2540 if (! strncmp (IDENTIFIER_POINTER (name),
2541 IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]),
2542 strlen (IDENTIFIER_POINTER (ansi_opname[(int) MODIFY_EXPR]))))
2544 TYPE_HAS_ASSIGNMENT (TREE_TYPE (typedecl)) = 1;
2545 sorry ("declaring \"friend operator =\" will not find \"operator = (X&)\" if it exists");
2549 /* Set up a cross reference so that type TYPE will make member function
2550 CTYPE::DECL a friend when CTYPE is finally defined. For more than
2551 one, set up a cross reference so that functions with the name DECL
2552 and type CTYPE know that they are friends of TYPE. */
2554 xref_friend (type, decl, ctype)
2555 tree type, decl, ctype;
2557 tree friend_decl = TYPE_NAME (ctype);
2559 tree typedecl = TYPE_NAME (type);
2560 tree t = tree_cons (NULL_TREE, ctype, DECL_UNDEFINED_FRIENDS (typedecl));
2562 DECL_UNDEFINED_FRIENDS (typedecl) = t;
2566 SET_DECL_WAITING_FRIENDS (friend_decl,
2568 DECL_WAITING_FRIENDS (friend_decl)));
2569 TREE_TYPE (DECL_WAITING_FRIENDS (friend_decl)) = decl;
2572 /* Make FRIEND_TYPE a friend class to TYPE. If FRIEND_TYPE has already
2573 been defined, we make all of its member functions friends of
2574 TYPE. If not, we make it a pending friend, which can later be added
2575 when its definition is seen. If a type is defined, then its TYPE_DECL's
2576 DECL_UNDEFINED_FRIENDS contains a (possibly empty) list of friend
2577 classes that are not defined. If a type has not yet been defined,
2578 then the DECL_WAITING_FRIENDS contains a list of types
2579 waiting to make it their friend. Note that these two can both
2580 be in use at the same time! */
2582 make_friend_class (type, friend_type)
2583 tree type, friend_type;
2587 if (IS_SIGNATURE (type))
2589 error ("`friend' declaration in signature definition");
2592 if (IS_SIGNATURE (friend_type))
2594 error ("signature type `%s' declared `friend'",
2595 IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (friend_type))));
2598 if (type == friend_type)
2600 warning ("class `%s' is implicitly friends with itself",
2601 TYPE_NAME_STRING (type));
2605 GNU_xref_hier (TYPE_NAME_STRING (type),
2606 TYPE_NAME_STRING (friend_type), 0, 0, 1);
2608 classes = CLASSTYPE_FRIEND_CLASSES (type);
2609 while (classes && TREE_VALUE (classes) != friend_type)
2610 classes = TREE_CHAIN (classes);
2612 warning ("class `%s' is already friends with class `%s'",
2613 TYPE_NAME_STRING (TREE_VALUE (classes)), TYPE_NAME_STRING (type));
2616 CLASSTYPE_FRIEND_CLASSES (type)
2617 = tree_cons (NULL_TREE, friend_type, CLASSTYPE_FRIEND_CLASSES (type));
2621 /* Main friend processor. This is large, and for modularity purposes,
2622 has been removed from grokdeclarator. It returns `void_type_node'
2623 to indicate that something happened, though a FIELD_DECL is
2626 CTYPE is the class this friend belongs to.
2628 DECLARATOR is the name of the friend.
2630 DECL is the FUNCTION_DECL that the friend is.
2632 In case we are parsing a friend which is part of an inline
2633 definition, we will need to store PARM_DECL chain that comes
2634 with it into the DECL_ARGUMENTS slot of the FUNCTION_DECL.
2636 FLAGS is just used for `grokclassfn'.
2638 QUALS say what special qualifies should apply to the object
2639 pointed to by `this'. */
2641 do_friend (ctype, declarator, decl, parmdecls, flags, quals)
2642 tree ctype, declarator, decl, parmdecls;
2643 enum overload_flags flags;
2646 /* first, lets find out if what we are making a friend needs overloading */
2648 int was_c_linkage = 0;
2650 /* Every decl that gets here is a friend of something. */
2651 DECL_FRIEND_P (decl) = 1;
2653 /* If we find something in scope, let see if it has extern "C" linkage. */
2654 /* This code is pretty general and should be ripped out and reused
2655 as a separate function. */
2656 if (DECL_NAME (decl))
2658 previous_decl = lookup_name (DECL_NAME (decl), 0);
2659 if (previous_decl && TREE_CODE (previous_decl) == TREE_LIST)
2663 if (TREE_TYPE (TREE_VALUE (previous_decl)) == TREE_TYPE (decl))
2665 previous_decl = TREE_VALUE (previous_decl);
2668 previous_decl = TREE_CHAIN (previous_decl);
2670 while (previous_decl);
2673 /* It had extern "C" linkage, so don't overload this. */
2674 if (previous_decl && TREE_CODE (previous_decl) == FUNCTION_DECL
2675 && TREE_TYPE (decl) == TREE_TYPE (previous_decl)
2676 && DECL_LANGUAGE (previous_decl) == lang_c)
2682 tree cname = TYPE_NAME (ctype);
2683 if (TREE_CODE (cname) == TYPE_DECL)
2684 cname = DECL_NAME (cname);
2686 /* A method friend. */
2687 if (TREE_CODE (decl) == FUNCTION_DECL)
2689 if (flags == NO_SPECIAL && ctype && declarator == cname)
2690 DECL_CONSTRUCTOR_P (decl) = 1;
2692 /* This will set up DECL_ARGUMENTS for us. */
2693 grokclassfn (ctype, cname, decl, flags, quals);
2694 if (TYPE_SIZE (ctype) != 0)
2695 check_classfn (ctype, cname, decl);
2697 if (TREE_TYPE (decl) != error_mark_node)
2699 if (TYPE_SIZE (ctype))
2701 /* We don't call pushdecl here yet, or ever on this
2702 actual FUNCTION_DECL. We must preserve its TREE_CHAIN
2704 make_decl_rtl (decl, NULL_PTR, 1);
2705 add_friend (current_class_type, decl);
2709 register char *classname
2710 = IDENTIFIER_POINTER (DECL_NAME (TYPE_NAME (ctype)));
2712 error ("member declared as friend before type `%s' defined",
2719 /* Possibly a bunch of method friends. */
2721 /* Get the class they belong to. */
2722 tree ctype = IDENTIFIER_TYPE_VALUE (cname);
2724 /* This class is defined, use its methods now. */
2725 if (TYPE_SIZE (ctype))
2727 tree fields = lookup_fnfields (TYPE_BINFO (ctype), declarator, 0);
2729 add_friends (current_class_type, declarator, ctype);
2731 error ("method `%s' is not a member of class `%s'",
2732 IDENTIFIER_POINTER (declarator),
2733 IDENTIFIER_POINTER (cname));
2736 /* Note: DECLARATOR actually has more than one; in this
2737 case, we're making sure that fns with the name DECLARATOR
2738 and type CTYPE know they are friends of the current
2740 xref_friend (current_class_type, declarator, ctype);
2741 decl = void_type_node;
2744 /* never overload C functions */
2745 else if (TREE_CODE (decl) == FUNCTION_DECL
2746 && ((IDENTIFIER_LENGTH (declarator) == 4
2747 && IDENTIFIER_POINTER (declarator)[0] == 'm'
2748 && ! strcmp (IDENTIFIER_POINTER (declarator), "main"))
2749 || (IDENTIFIER_LENGTH (declarator) > 10
2750 && IDENTIFIER_POINTER (declarator)[0] == '_'
2751 && IDENTIFIER_POINTER (declarator)[1] == '_'
2752 && strncmp (IDENTIFIER_POINTER (declarator)+2,
2753 "builtin_", 8) == 0)
2756 /* raw "main", and builtin functions never gets overloaded,
2757 but they can become friends. */
2758 TREE_PUBLIC (decl) = 1;
2759 add_friend (current_class_type, decl);
2760 DECL_FRIEND_P (decl) = 1;
2761 decl = void_type_node;
2764 @@ or possibly a friend from a base class ?!? */
2765 else if (TREE_CODE (decl) == FUNCTION_DECL)
2767 /* Friends must all go through the overload machinery,
2768 even though they may not technically be overloaded.
2770 Note that because classes all wind up being top-level
2771 in their scope, their friend wind up in top-level scope as well. */
2772 DECL_ASSEMBLER_NAME (decl)
2773 = build_decl_overload (declarator, TYPE_ARG_TYPES (TREE_TYPE (decl)),
2774 TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE);
2775 DECL_ARGUMENTS (decl) = parmdecls;
2776 DECL_CLASS_CONTEXT (decl) = current_class_type;
2778 /* We can call pushdecl here, because the TREE_CHAIN of this
2779 FUNCTION_DECL is not needed for other purposes. */
2780 decl = pushdecl_top_level (decl);
2782 make_decl_rtl (decl, NULL_PTR, 1);
2783 add_friend (current_class_type, decl);
2785 DECL_FRIEND_P (decl) = 1;
2787 TREE_OVERLOADED (declarator) = 1;
2792 /* @@ Should be able to ingest later definitions of this function
2794 tree decl = IDENTIFIER_GLOBAL_VALUE (declarator);
2795 if (decl == NULL_TREE)
2797 warning ("implicitly declaring `%s' as struct",
2798 IDENTIFIER_POINTER (declarator));
2799 decl = xref_tag (record_type_node, declarator, NULL_TREE, 1);
2800 decl = TYPE_NAME (decl);
2803 /* Allow abbreviated declarations of overloaded functions,
2804 but not if those functions are really class names. */
2805 if (TREE_CODE (decl) == TREE_LIST && TREE_TYPE (TREE_PURPOSE (decl)))
2807 warning ("`friend %s' archaic, use `friend class %s' instead",
2808 IDENTIFIER_POINTER (declarator),
2809 IDENTIFIER_POINTER (declarator));
2810 decl = TREE_TYPE (TREE_PURPOSE (decl));
2813 if (TREE_CODE (decl) == TREE_LIST)
2814 add_friends (current_class_type, TREE_PURPOSE (decl), NULL_TREE);
2816 make_friend_class (current_class_type, TREE_TYPE (decl));
2817 decl = void_type_node;
2822 /* TYPE has now been defined. It may, however, have a number of things
2823 waiting make make it their friend. We resolve these references
2826 embrace_waiting_friends (type)
2829 tree decl = TYPE_NAME (type);
2832 if (TREE_CODE (decl) != TYPE_DECL)
2835 for (waiters = DECL_WAITING_FRIENDS (decl); waiters;
2836 waiters = TREE_CHAIN (waiters))
2838 tree waiter = TREE_PURPOSE (waiters);
2840 tree waiter_prev = TREE_VALUE (waiters);
2842 tree decl = TREE_TYPE (waiters);
2843 tree name = decl ? (TREE_CODE (decl) == IDENTIFIER_NODE
2844 ? decl : DECL_NAME (decl)) : NULL_TREE;
2847 /* @@ There may be work to be done since we have not verified
2848 @@ consistency between original and friend declarations
2849 @@ of the functions waiting to become friends. */
2850 tree field = lookup_fnfields (TYPE_BINFO (type), name, 0);
2853 add_friends (waiter, name, type);
2855 add_friend (waiter, decl);
2857 error_with_file_and_line (DECL_SOURCE_FILE (TYPE_NAME (waiter)),
2858 DECL_SOURCE_LINE (TYPE_NAME (waiter)),
2859 "no method `%s' defined in class `%s' to be friend",
2860 IDENTIFIER_POINTER (DECL_NAME (TREE_TYPE (waiters))),
2861 TYPE_NAME_STRING (type));
2864 make_friend_class (type, waiter);
2867 if (TREE_CHAIN (waiter_prev))
2868 TREE_CHAIN (waiter_prev) = TREE_CHAIN (TREE_CHAIN (waiter_prev));
2870 DECL_UNDEFINED_FRIENDS (TYPE_NAME (waiter)) = NULL_TREE;
2875 /* Common subroutines of build_new and build_vec_delete. */
2877 /* Common interface for calling "builtin" functions that are not
2881 build_builtin_call (type, node, arglist)
2886 tree rval = build (CALL_EXPR, type, node, arglist, 0);
2887 TREE_SIDE_EFFECTS (rval) = 1;
2888 assemble_external (TREE_OPERAND (node, 0));
2889 TREE_USED (TREE_OPERAND (node, 0)) = 1;
2893 /* Generate a C++ "new" expression. DECL is either a TREE_LIST
2894 (which needs to go through some sort of groktypename) or it
2895 is the name of the class we are newing. INIT is an initialization value.
2896 It is either an EXPRLIST, an EXPR_NO_COMMAS, or something in braces.
2897 If INIT is void_type_node, it means do *not* call a constructor
2900 For types with constructors, the data returned is initialized
2901 by the appropriate constructor.
2903 Whether the type has a constructor or not, if it has a pointer
2904 to a virtual function table, then that pointer is set up
2907 Unless I am mistaken, a call to new () will return initialized
2908 data regardless of whether the constructor itself is private or
2909 not. NOPE; new fails if the constructor is private (jcm).
2911 Note that build_new does nothing to assure that any special
2912 alignment requirements of the type are met. Rather, it leaves
2913 it up to malloc to do the right thing. Otherwise, folding to
2914 the right alignment cal cause problems if the user tries to later
2915 free the memory returned by `new'.
2917 PLACEMENT is the `placement' list for user-defined operator new (). */
2920 build_new (placement, decl, init, use_global_new)
2925 tree type, true_type, size, rval;
2928 enum tree_code code = NEW_EXPR;
2930 tree pending_sizes = NULL_TREE;
2932 if (decl == error_mark_node)
2933 return error_mark_node;
2935 if (TREE_CODE (decl) == TREE_LIST)
2937 tree absdcl = TREE_VALUE (decl);
2938 tree last_absdcl = NULL_TREE;
2939 int old_immediate_size_expand;
2941 if (current_function_decl
2942 && DECL_CONSTRUCTOR_P (current_function_decl))
2944 old_immediate_size_expand = immediate_size_expand;
2945 immediate_size_expand = 0;
2948 nelts = integer_one_node;
2950 if (absdcl && TREE_CODE (absdcl) == CALL_EXPR)
2951 my_friendly_abort (215);
2952 while (absdcl && TREE_CODE (absdcl) == INDIRECT_REF)
2954 last_absdcl = absdcl;
2955 absdcl = TREE_OPERAND (absdcl, 0);
2958 if (absdcl && TREE_CODE (absdcl) == ARRAY_REF)
2960 /* probably meant to be a vec new */
2963 while (TREE_OPERAND (absdcl, 0)
2964 && TREE_CODE (TREE_OPERAND (absdcl, 0)) == ARRAY_REF)
2966 last_absdcl = absdcl;
2967 absdcl = TREE_OPERAND (absdcl, 0);
2971 this_nelts = TREE_OPERAND (absdcl, 1);
2972 if (this_nelts != error_mark_node)
2974 if (this_nelts == NULL_TREE)
2975 error ("new of array type fails to specify size");
2978 this_nelts = save_expr (convert (sizetype, this_nelts));
2979 absdcl = TREE_OPERAND (absdcl, 0);
2980 if (this_nelts == integer_zero_node)
2982 warning ("zero size array reserves no space");
2983 nelts = integer_zero_node;
2986 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
2990 nelts = integer_zero_node;
2994 TREE_OPERAND (last_absdcl, 0) = absdcl;
2996 TREE_VALUE (decl) = absdcl;
2998 type = true_type = groktypename (decl);
2999 if (! type || type == error_mark_node)
3001 immediate_size_expand = old_immediate_size_expand;
3002 return error_mark_node;
3005 if (current_function_decl
3006 && DECL_CONSTRUCTOR_P (current_function_decl))
3008 pending_sizes = get_pending_sizes ();
3009 immediate_size_expand = old_immediate_size_expand;
3012 else if (TREE_CODE (decl) == IDENTIFIER_NODE)
3014 if (IDENTIFIER_HAS_TYPE_VALUE (decl))
3016 /* An aggregate type. */
3017 type = IDENTIFIER_TYPE_VALUE (decl);
3018 decl = TYPE_NAME (type);
3022 /* A builtin type. */
3023 decl = lookup_name (decl, 1);
3024 my_friendly_assert (TREE_CODE (decl) == TYPE_DECL, 215);
3025 type = TREE_TYPE (decl);
3029 else if (TREE_CODE (decl) == TYPE_DECL)
3031 type = TREE_TYPE (decl);
3038 decl = TYPE_NAME (type);
3041 /* ``A reference cannot be created by the new operator. A reference
3042 is not an object (8.2.2, 8.4.3), so a pointer to it could not be
3043 returned by new.'' ARM 5.3.3 */
3044 if (TREE_CODE (type) == REFERENCE_TYPE)
3046 error ("new cannot be applied to a reference type");
3047 type = true_type = TREE_TYPE (type);
3050 /* When the object being created is an array, the new-expression yields a
3051 pointer to the initial element (if any) of the array. For example,
3052 both new int and new int[10] return an int*. 5.3.4. */
3053 if (TREE_CODE (type) == ARRAY_TYPE && has_array == 0)
3055 nelts = array_type_nelts_top (type);
3057 type = true_type = TREE_TYPE (type);
3060 if (TYPE_READONLY (type) || TYPE_VOLATILE (type))
3062 pedwarn ("const and volatile types cannot be created with operator new");
3063 type = true_type = TYPE_MAIN_VARIANT (type);
3066 /* If our base type is an array, then make sure we know how many elements
3068 while (TREE_CODE (true_type) == ARRAY_TYPE)
3070 tree this_nelts = array_type_nelts_top (true_type);
3071 nelts = build_binary_op (MULT_EXPR, nelts, this_nelts, 1);
3072 true_type = TREE_TYPE (true_type);
3075 size = fold (build_binary_op (MULT_EXPR, size_in_bytes (true_type),
3078 size = size_in_bytes (type);
3080 if (TYPE_SIZE (true_type) == 0)
3082 if (true_type == void_type_node)
3083 error ("invalid type for new: `void'");
3085 incomplete_type_error (0, true_type);
3086 return error_mark_node;
3089 if (TYPE_LANG_SPECIFIC (true_type)
3090 && CLASSTYPE_ABSTRACT_VIRTUALS (true_type))
3092 abstract_virtuals_error (NULL_TREE, true_type);
3093 return error_mark_node;
3096 if (TYPE_LANG_SPECIFIC (true_type) && IS_SIGNATURE (true_type))
3098 signature_error (NULL_TREE, true_type);
3099 return error_mark_node;
3102 /* Get a little extra space to store a couple of things before the new'ed
3104 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type))
3106 tree extra = BI_header_size;
3108 size = size_binop (PLUS_EXPR, size, extra);
3112 code = VEC_NEW_EXPR;
3114 /* Allocate the object. */
3115 if (! use_global_new && TYPE_LANG_SPECIFIC (true_type)
3116 && (TYPE_GETS_NEW (true_type) & (1 << has_array)))
3117 rval = build_opfncall (code, LOOKUP_NORMAL,
3118 TYPE_POINTER_TO (true_type), size, placement);
3121 rval = build_opfncall (code, LOOKUP_GLOBAL|LOOKUP_COMPLAIN,
3122 ptr_type_node, size, placement);
3123 rval = convert (TYPE_POINTER_TO (true_type), rval);
3125 else if (! has_array && flag_this_is_variable > 0
3126 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3128 if (init == NULL_TREE || TREE_CODE (init) == TREE_LIST)
3132 error ("constructors take parameter lists");
3133 return error_mark_node;
3138 rval = build_builtin_call (build_pointer_type (true_type),
3139 has_array ? BIVN : BIN,
3140 build_tree_list (NULL_TREE, size));
3142 /* See comment above as to why this is disabled. */
3145 rval = build (PLUS_EXPR, TYPE_POINTER_TO (true_type), rval,
3147 rval = build (BIT_AND_EXPR, TYPE_POINTER_TO (true_type),
3148 rval, build1 (BIT_NOT_EXPR, integer_type_node,
3152 TREE_CALLS_NEW (rval) = 1;
3155 /* if rval is NULL_TREE I don't have to allocate it, but are we totally
3156 sure we have some extra bytes in that case for the BI_header_size
3157 cookies? And how does that interact with the code below? (mrs) */
3158 /* Finish up some magic for new'ed arrays */
3159 if (has_array && TYPE_VEC_NEW_USES_COOKIE (true_type) && rval != NULL_TREE)
3161 tree extra = BI_header_size;
3163 rval = convert (ptr_type_node, rval); /* convert to void * first */
3164 rval = convert (string_type_node, rval); /* lets not add void* and ints */
3165 rval = save_expr (build_binary_op (PLUS_EXPR, rval, extra, 1));
3166 /* Store header info. */
3167 cookie = build_indirect_ref (build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3168 rval, extra), NULL_PTR);
3169 exp1 = build (MODIFY_EXPR, void_type_node,
3170 build_component_ref (cookie, nc_nelts_field_id, 0, 0),
3172 TREE_SIDE_EFFECTS (exp1) = 1;
3173 rval = convert (build_pointer_type (true_type), rval);
3174 TREE_CALLS_NEW (rval) = 1;
3175 TREE_SIDE_EFFECTS (rval) = 1;
3176 rval = build_compound_expr (tree_cons (NULL_TREE, exp1,
3177 build_tree_list (NULL_TREE, rval)));
3180 /* We've figured out where the allocation is to go.
3181 If we're not eliding constructors, then if a constructor
3182 is defined, we must go through it. */
3183 if (!has_array && (rval == NULL_TREE || !flag_elide_constructors)
3184 && TYPE_HAS_CONSTRUCTOR (true_type) && init != void_type_node)
3187 /* Constructors are never virtual. If it has an initialization, we
3188 need to complain if we aren't allowed to use the ctor that took
3190 int flags = LOOKUP_NORMAL|LOOKUP_NONVIRTUAL|LOOKUP_COMPLAIN;
3192 /* If a copy constructor might work, set things up so that we can
3193 try that after this. We deliberately don't clear LOOKUP_COMPLAIN
3194 any more, since that would make it impossible to rationally use
3195 the access of a constructor that matches perfectly. */
3197 if (rval != NULL_TREE)
3198 flags |= LOOKUP_SPECULATIVELY;
3201 if (rval && TYPE_USES_VIRTUAL_BASECLASSES (true_type))
3203 init = tree_cons (NULL_TREE, integer_one_node, init);
3204 flags |= LOOKUP_HAS_IN_CHARGE;
3210 if (TREE_CODE (TREE_TYPE (tmp)) == POINTER_TYPE)
3211 tmp = build_indirect_ref (tmp, NULL_PTR);
3213 newrval = build_method_call (tmp, constructor_name_full (true_type),
3214 init, NULL_TREE, flags);
3220 TREE_HAS_CONSTRUCTOR (rval) = 1;
3223 rval = error_mark_node;
3227 if (rval == error_mark_node)
3228 return error_mark_node;
3229 rval = save_expr (rval);
3230 TREE_HAS_CONSTRUCTOR (rval) = 1;
3232 /* Don't call any constructors or do any initialization. */
3233 if (init == void_type_node)
3236 if (TYPE_NEEDS_CONSTRUCTING (type) || init)
3238 if (! TYPE_NEEDS_CONSTRUCTING (type) && ! IS_AGGR_TYPE (type))
3240 /* New 2.0 interpretation: `new int (10)' means
3241 allocate an int, and initialize it with 10. */
3243 init = build_c_cast (type, init);
3244 rval = build (COMPOUND_EXPR, TREE_TYPE (rval),
3245 build_modify_expr (build_indirect_ref (rval, NULL_PTR),
3248 TREE_SIDE_EFFECTS (rval) = 1;
3249 TREE_CALLS_NEW (rval) = 1;
3251 else if (current_function_decl == NULL_TREE)
3253 extern tree static_aggregates;
3255 /* In case of static initialization, SAVE_EXPR is good enough. */
3256 init = copy_to_permanent (init);
3257 rval = copy_to_permanent (rval);
3258 static_aggregates = perm_tree_cons (init, rval, static_aggregates);
3262 /* Have to wrap this in RTL_EXPR for two cases:
3263 in base or member initialization and if we
3264 are a branch of a ?: operator. Since we
3265 can't easily know the latter, just do it always. */
3266 tree xval = make_node (RTL_EXPR);
3268 TREE_TYPE (xval) = TREE_TYPE (rval);
3269 do_pending_stack_adjust ();
3270 start_sequence_for_rtl_expr (xval);
3272 /* As a matter of principle, `start_sequence' should do this. */
3276 rval = expand_vec_init (decl, rval,
3277 build_binary_op (MINUS_EXPR, nelts, integer_one_node, 1),
3280 expand_aggr_init (build_indirect_ref (rval, NULL_PTR), init, 0);
3282 do_pending_stack_adjust ();
3284 TREE_SIDE_EFFECTS (xval) = 1;
3285 TREE_CALLS_NEW (xval) = 1;
3286 RTL_EXPR_SEQUENCE (xval) = get_insns ();
3289 if (TREE_CODE (rval) == SAVE_EXPR)
3291 /* Errors may cause this to not get evaluated. */
3292 if (SAVE_EXPR_RTL (rval) == 0)
3293 SAVE_EXPR_RTL (rval) = const0_rtx;
3294 RTL_EXPR_RTL (xval) = SAVE_EXPR_RTL (rval);
3298 my_friendly_assert (TREE_CODE (rval) == VAR_DECL, 217);
3299 RTL_EXPR_RTL (xval) = DECL_RTL (rval);
3305 if (rval && TREE_TYPE (rval) != build_pointer_type (type))
3307 /* The type of new int [3][3] is not int *, but int [3] * */
3308 rval = build_c_cast (build_pointer_type (type), rval);
3312 rval = build_compound_expr (chainon (pending_sizes,
3313 build_tree_list (NULL_TREE, rval)));
3317 extern tree gc_visible;
3321 rval = save_expr (rval);
3322 /* We don't need a `headof' operation to do this because
3323 we know where the object starts. */
3324 objbits = build1 (INDIRECT_REF, unsigned_type_node,
3325 build (MINUS_EXPR, ptr_type_node,
3326 rval, c_sizeof_nowarn (unsigned_type_node)));
3327 update_expr = build_modify_expr (objbits, BIT_IOR_EXPR, gc_visible);
3328 rval = build_compound_expr (tree_cons (NULL_TREE, rval,
3329 tree_cons (NULL_TREE, update_expr,
3330 build_tree_list (NULL_TREE, rval))));
3333 return save_expr (rval);
3336 /* `expand_vec_init' performs initialization of a vector of aggregate
3339 DECL is passed only for error reporting, and provides line number
3340 and source file name information.
3341 BASE is the space where the vector will be.
3342 MAXINDEX is the maximum index of the array (one less than the
3343 number of elements).
3344 INIT is the (possibly NULL) initializer.
3346 FROM_ARRAY is 0 if we should init everything with INIT
3347 (i.e., every element initialized from INIT).
3348 FROM_ARRAY is 1 if we should index into INIT in parallel
3349 with initialization of DECL.
3350 FROM_ARRAY is 2 if we should index into INIT in parallel,
3351 but use assignment instead of initialization. */
3354 expand_vec_init (decl, base, maxindex, init, from_array)
3355 tree decl, base, maxindex, init;
3359 tree iterator, base2 = NULL_TREE;
3360 tree type = TREE_TYPE (TREE_TYPE (base));
3363 maxindex = convert (integer_type_node, maxindex);
3364 if (maxindex == error_mark_node)
3365 return error_mark_node;
3367 if (current_function_decl == NULL_TREE)
3369 rval = make_tree_vec (3);
3370 TREE_VEC_ELT (rval, 0) = base;
3371 TREE_VEC_ELT (rval, 1) = maxindex;
3372 TREE_VEC_ELT (rval, 2) = init;
3376 size = size_in_bytes (type);
3378 /* Set to zero in case size is <= 0. Optimizer will delete this if
3379 it is not needed. */
3380 rval = get_temp_regvar (TYPE_POINTER_TO (type),
3381 convert (TYPE_POINTER_TO (type), null_pointer_node));
3382 base = default_conversion (base);
3383 base = convert (TYPE_POINTER_TO (type), base);
3384 expand_assignment (rval, base, 0, 0);
3385 base = get_temp_regvar (TYPE_POINTER_TO (type), base);
3387 if (init != NULL_TREE
3388 && TREE_CODE (init) == CONSTRUCTOR
3389 && TREE_TYPE (init) == TREE_TYPE (decl))
3391 /* Initialization of array from {...}. */
3392 tree elts = CONSTRUCTOR_ELTS (init);
3393 tree baseref = build1 (INDIRECT_REF, type, base);
3394 tree baseinc = build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size);
3395 int host_i = TREE_INT_CST_LOW (maxindex);
3397 if (IS_AGGR_TYPE (type))
3402 expand_aggr_init (baseref, TREE_VALUE (elts), 0);
3404 expand_assignment (base, baseinc, 0, 0);
3405 elts = TREE_CHAIN (elts);
3407 /* Initialize any elements by default if possible. */
3410 if (TYPE_NEEDS_CONSTRUCTING (type) == 0)
3413 use_variable (DECL_RTL (base));
3417 iterator = get_temp_regvar (integer_type_node,
3418 build_int_2 (host_i, 0));
3420 goto init_by_default;
3426 expand_assignment (baseref, TREE_VALUE (elts), 0, 0);
3428 expand_assignment (base, baseinc, 0, 0);
3429 elts = TREE_CHAIN (elts);
3433 use_variable (DECL_RTL (base));
3439 iterator = get_temp_regvar (integer_type_node, maxindex);
3443 /* If initializing one array from another,
3444 initialize element by element. */
3447 /* We rely upon the below calls the do argument checking */
3448 if (decl == NULL_TREE)
3450 sorry ("initialization of array from dissimilar array type");
3451 return error_mark_node;
3455 base2 = default_conversion (init);
3456 itype = TREE_TYPE (base2);
3457 base2 = get_temp_regvar (itype, base2);
3458 itype = TREE_TYPE (itype);
3460 else if (TYPE_LANG_SPECIFIC (type)
3461 && TYPE_NEEDS_CONSTRUCTING (type)
3462 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3464 error ("initializer ends prematurely");
3465 return error_mark_node;
3469 expand_start_cond (build (GE_EXPR, integer_type_node,
3470 iterator, integer_zero_node), 0);
3471 expand_start_loop_continue_elsewhere (1);
3475 tree to = build1 (INDIRECT_REF, type, base);
3479 from = build1 (INDIRECT_REF, itype, base2);
3483 if (from_array == 2)
3484 expand_expr_stmt (build_modify_expr (to, NOP_EXPR, from));
3485 else if (TYPE_NEEDS_CONSTRUCTING (type))
3486 expand_aggr_init (to, from, 0);
3488 expand_assignment (to, from, 0, 0);
3490 my_friendly_abort (57);
3492 else if (TREE_CODE (type) == ARRAY_TYPE)
3495 sorry ("cannot initialize multi-dimensional array with initializer");
3496 expand_vec_init (decl, build1 (NOP_EXPR, TYPE_POINTER_TO (TREE_TYPE (type)), base),
3497 array_type_nelts (type), 0, 0);
3500 expand_aggr_init (build1 (INDIRECT_REF, type, base), init, 0);
3502 expand_assignment (base,
3503 build (PLUS_EXPR, TYPE_POINTER_TO (type), base, size),
3506 expand_assignment (base2,
3507 build (PLUS_EXPR, TYPE_POINTER_TO (type), base2, size), 0, 0);
3508 expand_loop_continue_here ();
3509 expand_exit_loop_if_false (0, build (NE_EXPR, integer_type_node,
3510 build (PREDECREMENT_EXPR, integer_type_node, iterator, integer_one_node), minus_one));
3514 use_variable (DECL_RTL (base));
3516 use_variable (DECL_RTL (base2));
3521 use_variable (DECL_RTL (iterator));
3526 use_variable (DECL_RTL (rval));
3530 /* Free up storage of type TYPE, at address ADDR.
3532 TYPE is a POINTER_TYPE and can be ptr_type_node for no special type
3535 VIRTUAL_SIZE is the amount of storage that was allocated, and is
3536 used as the second argument to operator delete. It can include
3537 things like padding and magic size cookies. It has virtual in it,
3538 because if you have a base pointer and you delete through a virtual
3539 destructor, it should be the size of the dynamic object, not the
3540 static object, see Free Store 12.5 ANSI C++ WP.
3542 This does not call any destructors. */
3544 build_x_delete (type, addr, which_delete, virtual_size)
3549 int use_global_delete = which_delete & 1;
3550 int use_vec_delete = !!(which_delete & 2);
3552 enum tree_code code = use_vec_delete ? VEC_DELETE_EXPR : DELETE_EXPR;
3554 if (! use_global_delete && TYPE_LANG_SPECIFIC (TREE_TYPE (type))
3555 && (TYPE_GETS_DELETE (TREE_TYPE (type)) & (1 << use_vec_delete)))
3556 rval = build_opfncall (code, LOOKUP_NORMAL, addr, virtual_size, NULL_TREE);
3558 rval = build_builtin_call (void_type_node, use_vec_delete ? BIVD : BID,
3559 build_tree_list (NULL_TREE, addr));
3563 /* Generate a call to a destructor. TYPE is the type to cast ADDR to.
3564 ADDR is an expression which yields the store to be destroyed.
3565 AUTO_DELETE is nonzero if a call to DELETE should be made or not.
3566 If in the program, (AUTO_DELETE & 2) is non-zero, we tear down the
3567 virtual baseclasses.
3568 If in the program, (AUTO_DELETE & 1) is non-zero, then we deallocate.
3570 FLAGS is the logical disjunction of zero or more LOOKUP_
3571 flags. See cp-tree.h for more info.
3573 This function does not delete an object's virtual base classes. */
3575 build_delete (type, addr, auto_delete, flags, use_global_delete)
3579 int use_global_delete;
3581 tree function, parms;
3587 if (addr == error_mark_node)
3588 return error_mark_node;
3590 /* Can happen when CURRENT_EXCEPTION_OBJECT gets its type
3591 set to `error_mark_node' before it gets properly cleaned up. */
3592 if (type == error_mark_node)
3593 return error_mark_node;
3595 type = TYPE_MAIN_VARIANT (type);
3597 if (TREE_CODE (type) == POINTER_TYPE)
3599 type = TREE_TYPE (type);
3600 if (TYPE_SIZE (type) == 0)
3602 incomplete_type_error (0, type);
3603 return error_mark_node;
3605 if (TREE_CODE (type) == ARRAY_TYPE)
3607 if (! IS_AGGR_TYPE (type))
3609 /* Call the builtin operator delete. */
3610 return build_builtin_call (void_type_node, BID,
3611 build_tree_list (NULL_TREE, addr));
3613 if (TREE_SIDE_EFFECTS (addr))
3614 addr = save_expr (addr);
3615 ref = build_indirect_ref (addr, NULL_PTR);
3618 else if (TREE_CODE (type) == ARRAY_TYPE)
3621 if (TREE_SIDE_EFFECTS (addr))
3622 addr = save_expr (addr);
3623 return build_vec_delete (addr, array_type_nelts (type),
3624 c_sizeof_nowarn (TREE_TYPE (type)),
3625 auto_delete, integer_two_node,
3630 /* Don't check PROTECT here; leave that decision to the
3631 destructor. If the destructor is accessible, call it,
3632 else report error. */
3633 addr = build_unary_op (ADDR_EXPR, addr, 0);
3634 if (TREE_SIDE_EFFECTS (addr))
3635 addr = save_expr (addr);
3637 if (TREE_CONSTANT (addr))
3638 addr = convert_pointer_to (type, addr);
3640 addr = convert_force (build_pointer_type (type), addr);
3642 if (TREE_CODE (addr) == NOP_EXPR
3643 && TREE_OPERAND (addr, 0) == current_class_decl)
3646 ref = build_indirect_ref (addr, NULL_PTR);
3650 my_friendly_assert (IS_AGGR_TYPE (type), 220);
3652 if (! TYPE_NEEDS_DESTRUCTOR (type))
3654 if (auto_delete == integer_zero_node)
3655 return void_zero_node;
3657 /* Pass the size of the object down to the operator delete() in
3658 addition to the ADDR. */
3659 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3661 tree virtual_size = c_sizeof_nowarn (type);
3662 return build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3663 virtual_size, NULL_TREE);
3666 /* Call the builtin operator delete. */
3667 return build_builtin_call (void_type_node, BID,
3668 build_tree_list (NULL_TREE, addr));
3670 parms = build_tree_list (NULL_TREE, addr);
3672 /* Below, we will reverse the order in which these calls are made.
3673 If we have a destructor, then that destructor will take care
3674 of the base classes; otherwise, we must do that here. */
3675 if (TYPE_HAS_DESTRUCTOR (type))
3677 tree dtor = DECL_MAIN_VARIANT (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), 0));
3678 tree basetypes = TYPE_BINFO (type);
3680 if (flags & LOOKUP_PROTECT)
3682 enum access_type access = compute_access (basetypes, dtor);
3684 if (access == access_private)
3686 if (flags & LOOKUP_COMPLAIN)
3687 cp_error ("destructor for type `%T' is private in this scope", type);
3688 return error_mark_node;
3690 else if (access == access_protected)
3692 if (flags & LOOKUP_COMPLAIN)
3693 cp_error ("destructor for type `%T' is protected in this scope", type);
3694 return error_mark_node;
3698 /* Once we are in a destructor, try not going through
3699 the virtual function table to find the next destructor. */
3700 if (DECL_VINDEX (dtor)
3701 && ! (flags & LOOKUP_NONVIRTUAL)
3702 && TREE_CODE (auto_delete) != PARM_DECL
3703 && (ptr == 1 || ! resolves_to_fixed_type_p (ref, 0)))
3705 tree binfo, basetype;
3706 /* The code below is probably all broken. See call.c for the
3707 complete right way to do this. this offsets may not be right
3708 in the below. (mrs) */
3709 /* This destructor must be called via virtual function table. */
3710 dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (DECL_CONTEXT (dtor)), 0);
3711 basetype = DECL_CLASS_CONTEXT (dtor);
3712 binfo = get_binfo (basetype,
3713 TREE_TYPE (TREE_TYPE (TREE_VALUE (parms))),
3715 expr = convert_pointer_to_real (binfo, TREE_VALUE (parms));
3716 if (expr != TREE_VALUE (parms))
3719 ref = build_indirect_ref (expr, NULL_PTR);
3720 TREE_VALUE (parms) = expr;
3722 function = build_vfn_ref (&TREE_VALUE (parms), ref, DECL_VINDEX (dtor));
3723 if (function == error_mark_node)
3724 return error_mark_node;
3725 TREE_TYPE (function) = build_pointer_type (TREE_TYPE (dtor));
3726 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3727 expr = build_function_call (function, parms);
3728 if (ptr && (flags & LOOKUP_DESTRUCTOR) == 0)
3730 /* Handle the case where a virtual destructor is
3731 being called on an item that is 0.
3733 @@ Does this really need to be done? */
3734 tree ifexp = build_binary_op(NE_EXPR, addr, integer_zero_node,1);
3736 if (TREE_CODE (ref) == VAR_DECL
3737 || TREE_CODE (ref) == COMPONENT_REF)
3738 warning ("losing in build_delete");
3740 expr = build (COND_EXPR, void_type_node,
3741 ifexp, expr, void_zero_node);
3748 if ((flags & LOOKUP_DESTRUCTOR)
3749 || TREE_CODE (ref) == VAR_DECL
3750 || TREE_CODE (ref) == PARM_DECL
3751 || TREE_CODE (ref) == COMPONENT_REF
3752 || TREE_CODE (ref) == ARRAY_REF)
3753 /* These can't be 0. */
3754 ifexp = integer_one_node;
3756 /* Handle the case where a non-virtual destructor is
3757 being called on an item that is 0. */
3758 ifexp = build_binary_op (NE_EXPR, addr, integer_zero_node, 1);
3760 /* Used to mean that this destructor was known to be empty,
3761 but that's now obsolete. */
3762 my_friendly_assert (DECL_INITIAL (dtor) != void_type_node, 221);
3764 TREE_CHAIN (parms) = build_tree_list (NULL_TREE, auto_delete);
3765 expr = build_function_call (dtor, parms);
3767 if (ifexp != integer_one_node)
3768 expr = build (COND_EXPR, void_type_node,
3769 ifexp, expr, void_zero_node);
3775 /* This can get visibilities wrong. */
3776 tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
3777 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3778 tree base_binfo = n_baseclasses > 0 ? TREE_VEC_ELT (binfos, 0) : NULL_TREE;
3779 tree exprstmt = NULL_TREE;
3780 tree parent_auto_delete = auto_delete;
3783 /* If this type does not have a destructor, but does have
3784 operator delete, call the parent parent destructor (if any),
3785 but let this node do the deleting. Otherwise, it is ok
3786 to let the parent destructor do the deleting. */
3787 if (TYPE_GETS_REG_DELETE (type) && !use_global_delete)
3789 parent_auto_delete = integer_zero_node;
3790 if (auto_delete == integer_zero_node)
3796 /* This is probably wrong. It should be the size of the
3797 virtual object being deleted. */
3798 virtual_size = c_sizeof_nowarn (type);
3800 expr = build_opfncall (DELETE_EXPR, LOOKUP_NORMAL, addr,
3801 virtual_size, NULL_TREE);
3802 if (expr == error_mark_node)
3803 return error_mark_node;
3804 if (auto_delete != integer_one_node)
3805 cond = build (COND_EXPR, void_type_node,
3806 build (BIT_AND_EXPR, integer_type_node,
3807 auto_delete, integer_one_node),
3808 expr, void_zero_node);
3813 else if (base_binfo == NULL_TREE
3814 || (TREE_VIA_VIRTUAL (base_binfo) == 0
3815 && ! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))))
3819 /* This is probably wrong. It should be the size of the virtual
3820 object being deleted. */
3821 virtual_size = c_sizeof_nowarn (type);
3823 cond = build (COND_EXPR, void_type_node,
3824 build (BIT_AND_EXPR, integer_type_node, auto_delete, integer_one_node),
3825 build_builtin_call (void_type_node, BID,
3826 build_tree_list (NULL_TREE, addr)),
3833 exprstmt = build_tree_list (NULL_TREE, cond);
3836 && ! TREE_VIA_VIRTUAL (base_binfo)
3837 && TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo)))
3839 tree this_auto_delete;
3841 if (BINFO_OFFSET_ZEROP (base_binfo))
3842 this_auto_delete = parent_auto_delete;
3844 this_auto_delete = integer_zero_node;
3846 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), addr,
3847 this_auto_delete, flags, 0);
3848 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3851 /* Take care of the remaining baseclasses. */
3852 for (i = 1; i < n_baseclasses; i++)
3854 base_binfo = TREE_VEC_ELT (binfos, i);
3855 if (! TYPE_NEEDS_DESTRUCTOR (BINFO_TYPE (base_binfo))
3856 || TREE_VIA_VIRTUAL (base_binfo))
3859 /* May be zero offset if other baseclasses are virtual. */
3860 expr = fold (build (PLUS_EXPR, TYPE_POINTER_TO (BINFO_TYPE (base_binfo)),
3861 addr, BINFO_OFFSET (base_binfo)));
3863 expr = build_delete (TYPE_POINTER_TO (BINFO_TYPE (base_binfo)), expr,
3867 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3870 for (member = TYPE_FIELDS (type); member; member = TREE_CHAIN (member))
3872 if (TREE_CODE (member) != FIELD_DECL)
3874 if (TYPE_NEEDS_DESTRUCTOR (TREE_TYPE (member)))
3876 tree this_member = build_component_ref (ref, DECL_NAME (member), 0, 0);
3877 tree this_type = TREE_TYPE (member);
3878 expr = build_delete (this_type, this_member, integer_two_node, flags, 0);
3879 exprstmt = tree_cons (NULL_TREE, expr, exprstmt);
3884 return build_compound_expr (exprstmt);
3885 /* Virtual base classes make this function do nothing. */
3886 return void_zero_node;
3890 /* For type TYPE, delete the virtual baseclass objects of DECL. */
3893 build_vbase_delete (type, decl)
3896 tree vbases = CLASSTYPE_VBASECLASSES (type);
3897 tree result = NULL_TREE;
3898 tree addr = build_unary_op (ADDR_EXPR, decl, 0);
3900 my_friendly_assert (addr != error_mark_node, 222);
3904 tree this_addr = convert_force (TYPE_POINTER_TO (BINFO_TYPE (vbases)),
3906 result = tree_cons (NULL_TREE,
3907 build_delete (TREE_TYPE (this_addr), this_addr,
3909 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 0),
3911 vbases = TREE_CHAIN (vbases);
3913 return build_compound_expr (nreverse (result));
3916 /* Build a C++ vector delete expression.
3917 MAXINDEX is the number of elements to be deleted.
3918 ELT_SIZE is the nominal size of each element in the vector.
3919 BASE is the expression that should yield the store to be deleted.
3920 This function expands (or synthesizes) these calls itself.
3921 AUTO_DELETE_VEC says whether the container (vector) should be deallocated.
3922 AUTO_DELETE say whether each item in the container should be deallocated.
3924 This also calls delete for virtual baseclasses of elements of the vector.
3926 Update: MAXINDEX is no longer needed. The size can be extracted from the
3927 start of the vector for pointers, and from the type for arrays. We still
3928 use MAXINDEX for arrays because it happens to already have one of the
3929 values we'd have to extract. (We could use MAXINDEX with pointers to
3930 confirm the size, and trap if the numbers differ; not clear that it'd
3931 be worth bothering.) */
3933 build_vec_delete (base, maxindex, elt_size, auto_delete_vec, auto_delete,
3935 tree base, maxindex, elt_size;
3936 tree auto_delete_vec, auto_delete;
3937 int use_global_delete;
3939 tree ptype = TREE_TYPE (base);
3942 /* Temporary variables used by the loop. */
3943 tree tbase, size_exp, tbase_init;
3945 /* This is the body of the loop that implements the deletion of a
3946 single element, and moves temp variables to next elements. */
3949 /* This is the LOOP_EXPR that governs the deletion of the elements. */
3952 /* This is the thing that governs what to do after the loop has run. */
3953 tree deallocate_expr = 0;
3955 /* This is the BIND_EXPR which holds the outermost iterator of the
3956 loop. It is convenient to set this variable up and test it before
3957 executing any other code in the loop.
3958 This is also the containing expression returned by this function. */
3959 tree controller = NULL_TREE;
3961 /* This is the BLOCK to record the symbol binding for debugging. */
3964 base = stabilize_reference (base);
3966 /* Since we can use base many times, save_expr it. */
3967 if (TREE_SIDE_EFFECTS (base))
3968 base = save_expr (base);
3970 if (TREE_CODE (ptype) == POINTER_TYPE)
3972 /* Step back one from start of vector, and read dimension. */
3973 tree cookie_addr = build (MINUS_EXPR, TYPE_POINTER_TO (BI_header_type),
3974 base, BI_header_size);
3975 tree cookie = build_indirect_ref (cookie_addr, NULL_PTR);
3976 maxindex = build_component_ref (cookie, nc_nelts_field_id, 0, 0);
3978 ptype = TREE_TYPE (ptype);
3979 while (TREE_CODE (ptype) == ARRAY_TYPE);
3981 else if (TREE_CODE (ptype) == ARRAY_TYPE)
3983 /* get the total number of things in the array, maxindex is a bad name */
3984 maxindex = array_type_nelts_total (ptype);
3985 while (TREE_CODE (ptype) == ARRAY_TYPE)
3986 ptype = TREE_TYPE (ptype);
3987 base = build_unary_op (ADDR_EXPR, base, 1);
3991 error ("type to vector delete is neither pointer or array type");
3992 return error_mark_node;
3995 ptype = TYPE_POINTER_TO (type);
3997 size_exp = size_in_bytes (type);
3999 if (! IS_AGGR_TYPE (type) || ! TYPE_NEEDS_DESTRUCTOR (type))
4001 loop = integer_zero_node;
4005 /* The below is short by BI_header_size */
4006 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4008 tbase = build_decl (VAR_DECL, NULL_TREE, ptype);
4009 tbase_init = build_modify_expr (tbase, NOP_EXPR,
4010 fold (build (PLUS_EXPR, ptype,
4013 DECL_REGISTER (tbase) = 1;
4014 controller = build (BIND_EXPR, void_type_node, tbase, 0, 0);
4015 TREE_SIDE_EFFECTS (controller) = 1;
4016 block = build_block (tbase, 0, 0, 0, 0);
4017 add_block_current_level (block);
4019 if (auto_delete != integer_zero_node
4020 && auto_delete != integer_two_node)
4022 tree base_tbd = convert (ptype,
4023 build_binary_op (MINUS_EXPR,
4024 convert (ptr_type_node, base),
4027 /* This is the real size */
4028 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4029 body = build_tree_list (NULL_TREE,
4030 build_x_delete (ptype, base_tbd,
4031 2 | use_global_delete,
4033 body = build (COND_EXPR, void_type_node,
4034 build (BIT_AND_EXPR, integer_type_node,
4035 auto_delete, integer_one_node),
4036 body, integer_zero_node);
4041 body = tree_cons (NULL_TREE,
4042 build_delete (ptype, tbase, auto_delete,
4043 LOOKUP_NORMAL|LOOKUP_DESTRUCTOR, 1),
4046 body = tree_cons (NULL_TREE,
4047 build_modify_expr (tbase, NOP_EXPR, build (MINUS_EXPR, ptype, tbase, size_exp)),
4050 body = tree_cons (NULL_TREE,
4051 build (EXIT_EXPR, void_type_node,
4052 build (EQ_EXPR, integer_type_node, base, tbase)),
4055 loop = build (LOOP_EXPR, void_type_node, build_compound_expr (body));
4057 loop = tree_cons (NULL_TREE, tbase_init,
4058 tree_cons (NULL_TREE, loop, NULL_TREE));
4059 loop = build_compound_expr (loop);
4062 /* If the delete flag is one, or anything else with the low bit set,
4063 delete the storage. */
4064 if (auto_delete_vec == integer_zero_node
4065 || auto_delete_vec == integer_two_node)
4066 deallocate_expr = integer_zero_node;
4071 /* The below is short by BI_header_size */
4072 virtual_size = fold (size_binop (MULT_EXPR, size_exp, maxindex));
4074 if (! TYPE_VEC_NEW_USES_COOKIE (type))
4079 base_tbd = convert (ptype,
4080 build_binary_op (MINUS_EXPR,
4081 convert (string_type_node, base),
4084 /* True size with header. */
4085 virtual_size = size_binop (PLUS_EXPR, virtual_size, BI_header_size);
4087 deallocate_expr = build_x_delete (ptype, base_tbd,
4088 2 | use_global_delete,
4090 if (auto_delete_vec != integer_one_node)
4091 deallocate_expr = build (COND_EXPR, void_type_node,
4092 build (BIT_AND_EXPR, integer_type_node,
4093 auto_delete_vec, integer_one_node),
4094 deallocate_expr, integer_zero_node);
4097 if (loop && deallocate_expr != integer_zero_node)
4099 body = tree_cons (NULL_TREE, loop,
4100 tree_cons (NULL_TREE, deallocate_expr, NULL_TREE));
4101 body = build_compound_expr (body);
4106 /* Outermost wrapper: If pointer is null, punt. */
4107 body = build (COND_EXPR, void_type_node,
4108 build (NE_EXPR, integer_type_node, base, integer_zero_node),
4109 body, integer_zero_node);
4110 body = build1 (NOP_EXPR, void_type_node, body);
4114 TREE_OPERAND (controller, 1) = body;
4118 return convert (void_type_node, body);