1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* This is how we tell when two virtual member functions are really the
43 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
45 extern void set_class_shadows PARAMS ((tree));
47 /* The number of nested classes being processed. If we are not in the
48 scope of any class, this is zero. */
50 int current_class_depth;
52 /* In order to deal with nested classes, we keep a stack of classes.
53 The topmost entry is the innermost class, and is the entry at index
54 CURRENT_CLASS_DEPTH */
56 typedef struct class_stack_node {
57 /* The name of the class. */
60 /* The _TYPE node for the class. */
63 /* The access specifier pending for new declarations in the scope of
67 /* If were defining TYPE, the names used in this class. */
68 splay_tree names_used;
69 }* class_stack_node_t;
71 /* The stack itself. This is an dynamically resized array. The
72 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
73 static int current_class_stack_size;
74 static class_stack_node_t current_class_stack;
76 static tree get_vfield_name PARAMS ((tree));
77 static void finish_struct_anon PARAMS ((tree));
78 static tree build_vbase_pointer PARAMS ((tree, tree));
79 static tree build_vtable_entry PARAMS ((tree, tree, tree));
80 static tree get_vtable_name PARAMS ((tree));
81 static tree get_derived_offset PARAMS ((tree, tree));
82 static tree get_basefndecls PARAMS ((tree, tree));
83 static void set_rtti_entry PARAMS ((tree, tree, tree));
84 static int build_primary_vtable PARAMS ((tree, tree));
85 static int build_secondary_vtable PARAMS ((tree, tree));
86 static tree dfs_finish_vtbls PARAMS ((tree, void *));
87 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, void *));
88 static void finish_vtbls PARAMS ((tree));
89 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
90 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
91 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
92 static void delete_duplicate_fields PARAMS ((tree));
93 static void finish_struct_bits PARAMS ((tree));
94 static int alter_access PARAMS ((tree, tree, tree));
95 static void handle_using_decl PARAMS ((tree, tree));
96 static int overrides PARAMS ((tree, tree));
97 static int strictly_overrides PARAMS ((tree, tree));
98 static void mark_overriders PARAMS ((tree, tree));
99 static void check_for_override PARAMS ((tree, tree));
100 static tree dfs_modify_vtables PARAMS ((tree, void *));
101 static tree modify_all_vtables PARAMS ((tree, int *, tree));
102 static void determine_primary_base PARAMS ((tree, int *));
103 static void finish_struct_methods PARAMS ((tree));
104 static void maybe_warn_about_overly_private_class PARAMS ((tree));
105 static int field_decl_cmp PARAMS ((const tree *, const tree *));
106 static int method_name_cmp PARAMS ((const tree *, const tree *));
107 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
108 static tree fixed_type_or_null PARAMS ((tree, int *));
109 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
111 static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
112 static tree build_vtbl_initializer PARAMS ((tree, tree));
113 static int count_fields PARAMS ((tree));
114 static int add_fields_to_vec PARAMS ((tree, tree, int));
115 static void check_bitfield_decl PARAMS ((tree));
116 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
117 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
119 static void build_base_field PARAMS ((record_layout_info, tree, int *,
120 unsigned int *, varray_type *));
121 static varray_type build_base_fields PARAMS ((record_layout_info, int *));
122 static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
123 static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
125 static void check_methods PARAMS ((tree));
126 static void remove_zero_width_bit_fields PARAMS ((tree));
127 static void check_bases PARAMS ((tree, int *, int *, int *));
128 static void check_bases_and_members PARAMS ((tree, int *));
129 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
130 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
131 static void fixup_pending_inline PARAMS ((struct pending_inline *));
132 static void fixup_inline_methods PARAMS ((tree));
133 static void set_primary_base PARAMS ((tree, int, int *));
134 static tree dfs_propagate_binfo_offsets PARAMS ((tree, void *));
135 static void propagate_binfo_offsets PARAMS ((tree, tree));
136 static void layout_basetypes PARAMS ((tree));
137 static void layout_virtual_bases PARAMS ((tree));
138 static tree dfs_set_offset_for_shared_vbases PARAMS ((tree, void *));
139 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
140 static tree dfs_build_vbase_offset_vtbl_entries PARAMS ((tree, void *));
141 static tree build_vbase_offset_vtbl_entries PARAMS ((tree, tree));
142 static tree dfs_vcall_offset_queue_p PARAMS ((tree, void *));
143 static tree dfs_build_vcall_offset_vtbl_entries PARAMS ((tree, void *));
144 static tree build_vcall_offset_vtbl_entries PARAMS ((tree, tree));
145 static tree dfs_count_virtuals PARAMS ((tree, void *));
146 static void start_vtable PARAMS ((tree, int *));
147 static void layout_vtable_decl PARAMS ((tree, int));
148 static int num_vfun_entries PARAMS ((tree));
149 static tree dfs_find_final_overrider PARAMS ((tree, void *));
150 static tree find_final_overrider PARAMS ((tree, tree, tree));
151 static tree dfs_find_base PARAMS ((tree, void *));
152 static int make_new_vtable PARAMS ((tree, tree));
153 extern void dump_class_hierarchy PARAMS ((tree, int));
154 static tree build_vtable PARAMS ((tree, tree, tree));
155 static void initialize_vtable PARAMS ((tree, tree));
156 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
160 /* Variables shared between class.c and call.c. */
162 #ifdef GATHER_STATISTICS
164 int n_vtable_entries = 0;
165 int n_vtable_searches = 0;
166 int n_vtable_elems = 0;
167 int n_convert_harshness = 0;
168 int n_compute_conversion_costs = 0;
169 int n_build_method_call = 0;
170 int n_inner_fields_searched = 0;
173 /* Virtual base class layout. */
175 /* Returns a list of virtual base class pointers as a chain of
179 build_vbase_pointer_fields (rli, empty_p)
180 record_layout_info rli;
183 /* Chain to hold all the new FIELD_DECLs which point at virtual
186 tree vbase_decls = NULL_TREE;
187 tree binfos = TYPE_BINFO_BASETYPES (rec);
188 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
192 /* Under the new ABI, there are no vbase pointers in the object.
193 Instead, the offsets are stored in the vtable. */
194 if (vbase_offsets_in_vtable_p ())
197 /* Loop over the baseclasses, adding vbase pointers as needed. */
198 for (i = 0; i < n_baseclasses; i++)
200 register tree base_binfo = TREE_VEC_ELT (binfos, i);
201 register tree basetype = BINFO_TYPE (base_binfo);
203 if (TYPE_SIZE (basetype) == 0)
204 /* This error is now reported in xref_tag, thus giving better
205 location information. */
208 /* All basetypes are recorded in the association list of the
211 if (TREE_VIA_VIRTUAL (base_binfo))
216 /* The offset for a virtual base class is only used in computing
217 virtual function tables and for initializing virtual base
218 pointers. It is built once `get_vbase_types' is called. */
220 /* If this basetype can come from another vbase pointer
221 without an additional indirection, we will share
222 that pointer. If an indirection is involved, we
223 make our own pointer. */
224 for (j = 0; j < n_baseclasses; j++)
226 tree other_base_binfo = TREE_VEC_ELT (binfos, j);
227 if (! TREE_VIA_VIRTUAL (other_base_binfo)
228 && BINFO_FOR_VBASE (basetype, BINFO_TYPE (other_base_binfo)))
231 FORMAT_VBASE_NAME (name, basetype);
232 decl = build_vtbl_or_vbase_field (get_identifier (name),
233 get_identifier (VTABLE_BASE),
234 build_pointer_type (basetype),
238 BINFO_VPTR_FIELD (base_binfo) = decl;
239 TREE_CHAIN (decl) = vbase_decls;
240 layout_field (rli, decl);
245 /* The space this decl occupies has already been accounted for. */
253 /* Called from build_vbase_offset_vtbl_entries via dfs_walk. */
256 dfs_build_vbase_offset_vtbl_entries (binfo, data)
260 tree list = (tree) data;
262 if (TREE_TYPE (list) == binfo)
263 /* The TREE_TYPE of LIST is the base class from which we started
264 walking. If that BINFO is virtual it's not a virtual baseclass
267 else if (TREE_VIA_VIRTUAL (binfo))
272 /* Remember the index to the vbase offset for this virtual
274 vbase = BINFO_FOR_VBASE (TREE_TYPE (binfo), TREE_PURPOSE (list));
275 if (!TREE_VALUE (list))
276 BINFO_VPTR_FIELD (vbase) = build_int_2 (-1, 0);
279 BINFO_VPTR_FIELD (vbase) = TREE_PURPOSE (TREE_VALUE (list));
280 BINFO_VPTR_FIELD (vbase)
281 = fold (build (MINUS_EXPR, integer_type_node,
282 BINFO_VPTR_FIELD (vbase), integer_one_node));
285 /* And record the offset at which this virtual base lies in the
287 init = BINFO_OFFSET (binfo);
288 TREE_VALUE (list) = tree_cons (BINFO_VPTR_FIELD (vbase),
289 init, TREE_VALUE (list));
292 SET_BINFO_VTABLE_PATH_MARKED (binfo);
297 /* Returns the initializers for the vbase offset entries in the vtable
298 for BINFO (which is part of the class hierarchy dominated by T), in
302 build_vbase_offset_vtbl_entries (binfo, t)
310 /* Under the old ABI, pointers to virtual bases are stored in each
312 if (!vbase_offsets_in_vtable_p ())
315 /* If there are no virtual baseclasses, then there is nothing to
317 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
322 /* The offsets are allocated in the reverse order of a
323 depth-first left-to-right traversal of the hierarchy. We use
324 BINFO_VTABLE_PATH_MARKED because we are ourselves during a
325 dfs_walk, and so BINFO_MARKED is already in use. */
326 list = build_tree_list (t, NULL_TREE);
327 TREE_TYPE (list) = binfo;
329 dfs_build_vbase_offset_vtbl_entries,
330 dfs_vtable_path_unmarked_real_bases_queue_p,
333 dfs_vtable_path_unmark,
334 dfs_vtable_path_marked_real_bases_queue_p,
336 inits = nreverse (TREE_VALUE (list));
338 /* We've now got offsets in the right order. However, the offsets
339 we've stored are offsets from the beginning of the complete
340 object, and we need offsets from this BINFO. */
341 for (init = inits; init; init = TREE_CHAIN (init))
343 /* The dfs_build_vbase_offset_vtbl_entries routine uses the
344 TREE_PURPOSE to scribble in. But, we need to clear it now so
345 that the values are not perceived as labeled initializers. */
346 TREE_PURPOSE (init) = NULL_TREE;
348 = fold (build1 (NOP_EXPR, vtable_entry_type,
349 size_diffop (TREE_VALUE (init),
350 BINFO_OFFSET (binfo))));
356 typedef struct vcall_offset_data_s
358 /* The binfo for the most-derived type. */
360 /* The binfo for the virtual base for which we're building
363 /* The vcall offset initializers built up so far. */
365 /* The number of vcall offsets accumulated. */
369 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
372 dfs_vcall_offset_queue_p (binfo, data)
376 vcall_offset_data* vod = (vcall_offset_data *) data;
378 return (binfo == vod->vbase) ? binfo : dfs_skip_vbases (binfo, NULL);
381 /* Called from build_vcall_offset_vtbl_entries via dfs_walk. */
384 dfs_build_vcall_offset_vtbl_entries (binfo, data)
388 vcall_offset_data* vod;
392 /* Primary bases are not interesting; all of the virtual
393 function table entries have been overridden. */
394 if (BINFO_PRIMARY_MARKED_P (binfo))
397 vod = (vcall_offset_data *) data;
398 binfo_inits = NULL_TREE;
400 /* We chain the offsets on in reverse order. That's correct --
401 build_vtbl_initializer will straighten them out. */
402 for (virtuals = skip_rtti_stuff (binfo,
406 virtuals = TREE_CHAIN (virtuals))
408 /* Figure out what function we're looking at. */
409 tree fn = TREE_VALUE (virtuals);
410 tree base = DECL_CONTEXT (fn);
411 /* The FN comes from BASE. So, we must caculate the adjustment
412 from the virtual base that derived from BINFO to BASE. */
413 tree base_binfo = get_binfo (base, vod->derived, /*protect=*/0);
416 = tree_cons (NULL_TREE,
417 fold (build1 (NOP_EXPR, vtable_entry_type,
418 size_diffop (BINFO_OFFSET (base_binfo),
419 BINFO_OFFSET (vod->vbase)))),
423 /* Now add the initializers we've just created to the list that will
424 be returned to our caller. */
425 vod->inits = chainon (vod->inits, binfo_inits);
430 /* Returns the initializers for the vcall offset entries in the vtable
431 for BINFO (which is part of the class hierarchy dominated by T), in
435 build_vcall_offset_vtbl_entries (binfo, t)
439 vcall_offset_data vod;
441 /* Under the old ABI, the adjustments to the `this' pointer were made
443 if (!vcall_offsets_in_vtable_p ())
446 /* We only need these entries if this base is a virtual base. */
447 if (!TREE_VIA_VIRTUAL (binfo))
450 /* We need a vcall offset for each of the virtual functions in this
453 class A { virtual void f (); };
454 class B : virtual public A { };
455 class C: virtual public A, public B {};
462 The location of `A' is not at a fixed offset relative to `B'; the
463 offset depends on the complete object derived from `B'. So,
464 `B' vtable contains an entry for `f' that indicates by what
465 amount the `this' pointer for `B' needs to be adjusted to arrive
468 We need entries for all the functions in our primary vtable and
469 in our non-virtual bases vtables. For each base, the entries
470 appear in the same order as in the base; but the bases themselves
471 appear in reverse depth-first, left-to-right order. */
474 vod.inits = NULL_TREE;
476 dfs_build_vcall_offset_vtbl_entries,
477 dfs_vcall_offset_queue_p,
483 /* Returns a pointer to the virtual base class of EXP that has the
484 indicated TYPE. EXP is of class type, not a pointer type. */
487 build_vbase_pointer (exp, type)
490 if (vbase_offsets_in_vtable_p ())
495 /* Find the shared copy of TYPE; that's where the vtable offset
497 vbase = BINFO_FOR_VBASE (type, TREE_TYPE (exp));
498 /* Find the virtual function table pointer. */
499 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
500 /* Compute the location where the offset will lie. */
501 vbase_ptr = build_binary_op (PLUS_EXPR,
503 BINFO_VPTR_FIELD (vbase));
504 vbase_ptr = build1 (NOP_EXPR,
505 build_pointer_type (ptrdiff_type_node),
507 /* Add the contents of this location to EXP. */
508 return build (PLUS_EXPR,
509 build_pointer_type (type),
510 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
511 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
516 FORMAT_VBASE_NAME (name, type);
517 return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
521 /* Build multi-level access to EXPR using hierarchy path PATH.
522 CODE is PLUS_EXPR if we are going with the grain,
523 and MINUS_EXPR if we are not (in which case, we cannot traverse
524 virtual baseclass links).
526 TYPE is the type we want this path to have on exit.
528 NONNULL is non-zero if we know (for any reason) that EXPR is
529 not, in fact, zero. */
532 build_vbase_path (code, type, expr, path, nonnull)
534 tree type, expr, path;
537 register int changed = 0;
538 tree last = NULL_TREE, last_virtual = NULL_TREE;
540 tree null_expr = 0, nonnull_expr;
542 tree offset = integer_zero_node;
544 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
545 return build1 (NOP_EXPR, type, expr);
547 /* We could do better if we had additional logic to convert back to the
548 unconverted type (the static type of the complete object), and then
549 convert back to the type we want. Until that is done, we only optimize
550 if the complete type is the same type as expr has. */
551 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
553 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
554 expr = save_expr (expr);
557 path = reverse_path (path);
559 basetype = BINFO_TYPE (path);
563 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
565 last_virtual = BINFO_TYPE (TREE_VALUE (path));
566 if (code == PLUS_EXPR)
568 changed = ! fixed_type_p;
574 /* We already check for ambiguous things in the caller, just
578 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
579 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
581 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
582 nonnull_expr = build_vbase_pointer (ind, last_virtual);
584 && TREE_CODE (type) == POINTER_TYPE
585 && null_expr == NULL_TREE)
587 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
588 expr = build (COND_EXPR, build_pointer_type (last_virtual),
589 build (EQ_EXPR, boolean_type_node, expr,
591 null_expr, nonnull_expr);
594 /* else we'll figure out the offset below. */
596 /* Happens in the case of parse errors. */
597 if (nonnull_expr == error_mark_node)
598 return error_mark_node;
602 cp_error ("cannot cast up from virtual baseclass `%T'",
604 return error_mark_node;
607 last = TREE_VALUE (path);
608 path = TREE_CHAIN (path);
610 /* LAST is now the last basetype assoc on the path. */
612 /* A pointer to a virtual base member of a non-null object
613 is non-null. Therefore, we only need to test for zeroness once.
614 Make EXPR the canonical expression to deal with here. */
617 TREE_OPERAND (expr, 2) = nonnull_expr;
618 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
619 = TREE_TYPE (nonnull_expr);
624 /* If we go through any virtual base pointers, make sure that
625 casts to BASETYPE from the last virtual base class use
626 the right value for BASETYPE. */
629 tree intype = TREE_TYPE (TREE_TYPE (expr));
631 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
633 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
636 offset = BINFO_OFFSET (last);
638 if (! integer_zerop (offset))
640 /* Bash types to make the backend happy. */
641 offset = cp_convert (type, offset);
643 /* If expr might be 0, we need to preserve that zeroness. */
647 TREE_TYPE (null_expr) = type;
649 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
650 if (TREE_SIDE_EFFECTS (expr))
651 expr = save_expr (expr);
653 return build (COND_EXPR, type,
654 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
656 build (code, type, expr, offset));
658 else return build (code, type, expr, offset);
661 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
662 be used multiple times in initialization of multiple inheritance. */
665 TREE_TYPE (expr) = type;
669 return build1 (NOP_EXPR, type, expr);
673 /* Virtual function things. */
675 /* Build an entry in the virtual function table. DELTA is the offset
676 for the `this' pointer. VCALL_INDEX is the vtable index containing
677 the vcall offset; zero if none. ENTRY is the virtual function
678 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
679 but it may not actually be a virtual function table pointer. (For
680 example, it might be the address of the RTTI object, under the new
684 build_vtable_entry (delta, vcall_index, entry)
689 if (flag_vtable_thunks)
691 HOST_WIDE_INT idelta;
692 HOST_WIDE_INT ivindex;
694 idelta = tree_low_cst (delta, 0);
695 ivindex = tree_low_cst (vcall_index, 0);
696 if ((idelta || ivindex)
697 && ! DECL_PURE_VIRTUAL_P (TREE_OPERAND (entry, 0)))
699 entry = make_thunk (entry, idelta, ivindex);
700 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
701 TREE_READONLY (entry) = 1;
702 TREE_CONSTANT (entry) = 1;
704 #ifdef GATHER_STATISTICS
705 n_vtable_entries += 1;
711 extern int flag_huge_objects;
712 tree elems = tree_cons (NULL_TREE, delta,
713 tree_cons (NULL_TREE, integer_zero_node,
714 build_tree_list (NULL_TREE, entry)));
715 tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
717 /* We don't use vcall offsets when not using vtable thunks. */
718 my_friendly_assert (integer_zerop (vcall_index), 20000125);
720 /* DELTA used to be constructed by `size_int' and/or size_binop,
721 which caused overflow problems when it was negative. That should
724 if (! int_fits_type_p (delta, delta_type_node))
726 if (flag_huge_objects)
727 sorry ("object size exceeds built-in limit for virtual function table implementation");
729 sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
732 TREE_CONSTANT (entry) = 1;
733 TREE_STATIC (entry) = 1;
734 TREE_READONLY (entry) = 1;
736 #ifdef GATHER_STATISTICS
737 n_vtable_entries += 1;
744 /* We want to give the assembler the vtable identifier as well as
745 the offset to the function pointer. So we generate
747 __asm__ __volatile__ (".vtable_entry %c0, %c1"
748 : : "s"(&class_vtable),
749 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
752 build_vtable_entry_ref (basetype, vtbl, idx)
753 tree basetype, vtbl, idx;
755 static char asm_stmt[] = ".vtable_entry %c0, %c1";
758 s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
759 s = build_tree_list (build_string (1, "s"), s);
761 i = build_array_ref (vtbl, idx);
762 if (!flag_vtable_thunks)
763 i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
764 i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
765 i2 = build_array_ref (vtbl, build_int_2(0,0));
766 i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
767 i = build_binary_op (MINUS_EXPR, i, i2);
768 i = build_tree_list (build_string (1, "i"), i);
770 finish_asm_stmt (ridpointers[RID_VOLATILE],
771 build_string (sizeof(asm_stmt)-1, asm_stmt),
772 NULL_TREE, chainon (s, i), NULL_TREE);
775 /* Given an object INSTANCE, return an expression which yields the
776 virtual function vtable element corresponding to INDEX. There are
777 many special cases for INSTANCE which we take care of here, mainly
778 to avoid creating extra tree nodes when we don't have to. */
781 build_vtbl_ref (instance, idx)
785 tree basetype = TREE_TYPE (instance);
787 if (TREE_CODE (basetype) == REFERENCE_TYPE)
788 basetype = TREE_TYPE (basetype);
790 if (instance == current_class_ref)
791 vtbl = build_vfield_ref (instance, basetype);
796 /* Try to figure out what a reference refers to, and
797 access its virtual function table directly. */
798 tree ref = NULL_TREE;
800 if (TREE_CODE (instance) == INDIRECT_REF
801 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
802 ref = TREE_OPERAND (instance, 0);
803 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
806 if (ref && TREE_CODE (ref) == VAR_DECL
807 && DECL_INITIAL (ref))
809 tree init = DECL_INITIAL (ref);
811 while (TREE_CODE (init) == NOP_EXPR
812 || TREE_CODE (init) == NON_LVALUE_EXPR)
813 init = TREE_OPERAND (init, 0);
814 if (TREE_CODE (init) == ADDR_EXPR)
816 init = TREE_OPERAND (init, 0);
817 if (IS_AGGR_TYPE (TREE_TYPE (init))
818 && (TREE_CODE (init) == PARM_DECL
819 || TREE_CODE (init) == VAR_DECL))
825 if (IS_AGGR_TYPE (TREE_TYPE (instance))
826 && (TREE_CODE (instance) == RESULT_DECL
827 || TREE_CODE (instance) == PARM_DECL
828 || TREE_CODE (instance) == VAR_DECL))
829 vtbl = TYPE_BINFO_VTABLE (basetype);
831 vtbl = build_vfield_ref (instance, basetype);
834 assemble_external (vtbl);
837 build_vtable_entry_ref (basetype, vtbl, idx);
839 aref = build_array_ref (vtbl, idx);
844 /* Given an object INSTANCE, return an expression which yields the
845 virtual function corresponding to INDEX. There are many special
846 cases for INSTANCE which we take care of here, mainly to avoid
847 creating extra tree nodes when we don't have to. */
850 build_vfn_ref (ptr_to_instptr, instance, idx)
851 tree *ptr_to_instptr, instance;
854 tree aref = build_vtbl_ref (instance, idx);
856 /* When using thunks, there is no extra delta, and we get the pfn
858 if (flag_vtable_thunks)
863 /* Save the intermediate result in a SAVE_EXPR so we don't have to
864 compute each component of the virtual function pointer twice. */
865 if (TREE_CODE (aref) == INDIRECT_REF)
866 TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
869 = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
871 cp_convert (ptrdiff_type_node,
872 build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
875 return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
878 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
879 for the given TYPE. */
882 get_vtable_name (type)
885 tree type_id = build_typename_overload (type);
886 char *buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
887 + IDENTIFIER_LENGTH (type_id) + 2);
888 const char *ptr = IDENTIFIER_POINTER (type_id);
890 for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
892 /* We don't take off the numbers; build_secondary_vtable uses the
893 DECL_ASSEMBLER_NAME for the type, which includes the number
894 in `3foo'. If we were to pull them off here, we'd end up with
895 something like `_vt.foo.3bar', instead of a uniform definition. */
896 while (ptr[i] >= '0' && ptr[i] <= '9')
899 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, ptr+i);
900 return get_identifier (buf);
903 /* Return the offset to the main vtable for a given base BINFO. */
906 get_vfield_offset (binfo)
910 = size_binop (FLOOR_DIV_EXPR,
911 bit_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
912 bitsize_int (BITS_PER_UNIT));
914 return size_binop (PLUS_EXPR, convert (sizetype, tmp),
915 BINFO_OFFSET (binfo));
918 /* Get the offset to the start of the original binfo that we derived
919 this binfo from. If we find TYPE first, return the offset only
920 that far. The shortened search is useful because the this pointer
921 on method calling is expected to point to a DECL_CONTEXT (fndecl)
922 object, and not a baseclass of it. */
926 get_derived_offset (binfo, type)
929 tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
933 while (BINFO_BASETYPES (binfo)
934 && (i = CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
936 tree binfos = BINFO_BASETYPES (binfo);
937 if (BINFO_TYPE (binfo) == type)
939 binfo = TREE_VEC_ELT (binfos, i);
942 offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
943 return size_binop (MINUS_EXPR, offset1, offset2);
946 /* Update the rtti info for this class. */
949 set_rtti_entry (virtuals, offset, type)
950 tree virtuals, offset, type;
954 if (CLASSTYPE_COM_INTERFACE (type))
958 decl = get_tinfo_decl (type);
959 else if (!new_abi_rtti_p ())
960 /* If someone tries to get RTTI information for a type compiled
961 without RTTI, they're out of luck. By calling __pure_virtual
962 in this case, we give a small clue as to what went wrong. We
963 could consider having a __no_typeinfo function as well, for a
964 more specific hint. */
967 /* For the new-abi, we just point to the type_info object. */
970 if (flag_vtable_thunks)
972 /* The first slot holds the offset. */
973 BV_DELTA (virtuals) = offset;
974 BV_VCALL_INDEX (virtuals) = integer_zero_node;
976 /* The next node holds the decl. */
977 virtuals = TREE_CHAIN (virtuals);
978 offset = integer_zero_node;
981 /* This slot holds the function to call. */
982 BV_DELTA (virtuals) = offset;
983 BV_VCALL_INDEX (virtuals) = integer_zero_node;
984 BV_FN (virtuals) = decl;
987 /* Create a VAR_DECL for a primary or secondary vtable for
988 CLASS_TYPE. Use NAME for the name of the vtable, and VTABLE_TYPE
992 build_vtable (class_type, name, vtable_type)
999 decl = build_lang_decl (VAR_DECL, name, vtable_type);
1000 DECL_CONTEXT (decl) = class_type;
1001 DECL_ARTIFICIAL (decl) = 1;
1002 TREE_STATIC (decl) = 1;
1003 #ifndef WRITABLE_VTABLES
1004 /* Make them READONLY by default. (mrs) */
1005 TREE_READONLY (decl) = 1;
1007 DECL_VIRTUAL_P (decl) = 1;
1008 import_export_vtable (decl, class_type, 0);
1013 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
1014 or even complete. If this does not exist, create it. If COMPLETE is
1015 non-zero, then complete the definition of it -- that will render it
1016 impossible to actually build the vtable, but is useful to get at those
1017 which are known to exist in the runtime. */
1020 get_vtable_decl (type, complete)
1024 tree name = get_vtable_name (type);
1025 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
1029 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
1030 && DECL_VIRTUAL_P (decl), 20000118);
1034 decl = build_vtable (type, name, void_type_node);
1035 decl = pushdecl_top_level (decl);
1036 SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
1038 /* At one time the vtable info was grabbed 2 words at a time. This
1039 fails on sparc unless you have 8-byte alignment. (tiemann) */
1040 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
1044 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
1049 /* Build the primary virtual function table for TYPE. If BINFO is
1050 non-NULL, build the vtable starting with the initial approximation
1051 that it is the same as the one which is the head of the association
1052 list. Returns a non-zero value if a new vtable is actually
1056 build_primary_vtable (binfo, type)
1059 tree virtuals, decl;
1061 decl = get_vtable_decl (type, /*complete=*/0);
1067 if (BINFO_NEW_VTABLE_MARKED (binfo))
1068 /* We have already created a vtable for this base, so there's
1069 no need to do it again. */
1072 virtuals = copy_list (BINFO_VIRTUALS (binfo));
1073 TREE_TYPE (decl) = TREE_TYPE (BINFO_VTABLE (binfo));
1074 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (BINFO_VTABLE (binfo)));
1075 DECL_SIZE_UNIT (decl)
1076 = TYPE_SIZE_UNIT (TREE_TYPE (BINFO_VTABLE (binfo)));
1078 /* Now do rtti stuff. */
1079 offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
1080 offset = size_diffop (size_zero_node, offset);
1081 set_rtti_entry (virtuals, offset, type);
1085 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
1087 virtuals = NULL_TREE;
1090 #ifdef GATHER_STATISTICS
1092 n_vtable_elems += list_length (virtuals);
1095 /* Initialize the association list for this type, based
1096 on our first approximation. */
1097 TYPE_BINFO_VTABLE (type) = decl;
1098 TYPE_BINFO_VIRTUALS (type) = virtuals;
1100 binfo = TYPE_BINFO (type);
1101 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1105 /* Give TYPE a new virtual function table which is initialized
1106 with a skeleton-copy of its original initialization. The only
1107 entry that changes is the `delta' entry, so we can really
1108 share a lot of structure.
1110 FOR_TYPE is the derived type which caused this table to
1113 BINFO is the type association which provided TYPE for FOR_TYPE.
1115 The order in which vtables are built (by calling this function) for
1116 an object must remain the same, otherwise a binary incompatibility
1120 build_secondary_vtable (binfo, for_type)
1121 tree binfo, for_type;
1124 tree orig_decl = BINFO_VTABLE (binfo);
1137 if (TREE_VIA_VIRTUAL (binfo))
1138 my_friendly_assert (binfo == BINFO_FOR_VBASE (BINFO_TYPE (binfo),
1139 current_class_type),
1142 if (BINFO_NEW_VTABLE_MARKED (binfo))
1143 /* We already created a vtable for this base. There's no need to
1147 /* Remember that we've created a vtable for this BINFO, so that we
1148 don't try to do so again. */
1149 SET_BINFO_NEW_VTABLE_MARKED (binfo);
1151 /* Make fresh virtual list, so we can smash it later. */
1152 BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
1154 if (TREE_VIA_VIRTUAL (binfo))
1156 tree binfo1 = BINFO_FOR_VBASE (BINFO_TYPE (binfo), for_type);
1158 /* XXX - This should never happen, if it does, the caller should
1159 ensure that the binfo is from for_type's binfos, not from any
1160 base type's. We can remove all this code after a while. */
1161 if (binfo1 != binfo)
1162 warning ("internal inconsistency: binfo offset error for rtti");
1164 offset = BINFO_OFFSET (binfo1);
1167 offset = BINFO_OFFSET (binfo);
1169 set_rtti_entry (BINFO_VIRTUALS (binfo),
1170 size_diffop (size_zero_node, offset),
1173 /* In the new ABI, secondary vtables are laid out as part of the
1174 same structure as the primary vtable. */
1175 if (merge_primary_and_secondary_vtables_p ())
1177 BINFO_VTABLE (binfo) = NULL_TREE;
1181 /* Create the declaration for the secondary vtable. */
1182 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
1183 buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
1184 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
1186 /* We know that the vtable that we are going to create doesn't exist
1187 yet in the global namespace, and when we finish, it will be
1188 pushed into the global namespace. In complex MI hierarchies, we
1189 have to loop while the name we are thinking of adding is globally
1190 defined, adding more name components to the vtable name as we
1191 loop, until the name is unique. This is because in complex MI
1192 cases, we might have the same base more than once. This means
1193 that the order in which this function is called for vtables must
1194 remain the same, otherwise binary compatibility can be
1199 char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
1203 sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
1205 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
1206 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1207 name = get_identifier (buf);
1209 /* If this name doesn't clash, then we can use it, otherwise
1210 we add more to the name until it is unique. */
1212 if (! IDENTIFIER_GLOBAL_VALUE (name))
1215 /* Set values for next loop through, if the name isn't unique. */
1217 path = BINFO_INHERITANCE_CHAIN (path);
1219 /* We better not run out of stuff to make it unique. */
1220 my_friendly_assert (path != NULL_TREE, 368);
1222 basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
1224 if (for_type == basetype)
1226 /* If we run out of basetypes in the path, we have already
1227 found created a vtable with that name before, we now
1228 resort to tacking on _%d to distinguish them. */
1230 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
1231 buf1 = (char *) alloca (i);
1233 sprintf (buf1, "%s%c%s%c%d",
1234 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
1236 buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
1237 + strlen (buf1) + 1);
1238 sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
1239 name = get_identifier (buf);
1241 /* If this name doesn't clash, then we can use it,
1242 otherwise we add something different to the name until
1244 } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
1246 /* Hey, they really like MI don't they? Increase the 3
1247 above to 6, and the 999 to 999999. :-) */
1248 my_friendly_assert (j <= 999, 369);
1253 i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
1254 new_buf2 = (char *) alloca (i);
1255 sprintf (new_buf2, "%s%c%s",
1256 TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
1260 new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
1261 DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
1262 BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
1264 #ifdef GATHER_STATISTICS
1266 n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
1272 /* Create a new vtable for BINFO which is the hierarchy dominated by
1276 make_new_vtable (t, binfo)
1280 if (binfo == TYPE_BINFO (t))
1281 /* In this case, it is *type*'s vtable we are modifying. We start
1282 with the approximation that it's vtable is that of the
1283 immediate base class. */
1284 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
1287 /* This is our very own copy of `basetype' to play with. Later,
1288 we will fill in all the virtual functions that override the
1289 virtual functions in these base classes which are not defined
1290 by the current type. */
1291 return build_secondary_vtable (binfo, t);
1294 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
1295 (which is in the hierarchy dominated by T) list FNDECL as its
1296 BV_FN. DELTA is the required adjustment from the `this' pointer
1297 where the vtable entry appears to the `this' required when the
1298 function is actually called. */
1301 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
1312 vcall_index = integer_zero_node;
1314 if (fndecl != BV_FN (v)
1315 || !tree_int_cst_equal (delta, BV_DELTA (v))
1316 || !tree_int_cst_equal (vcall_index, BV_VCALL_INDEX (v)))
1320 /* We need a new vtable for BINFO. */
1321 if (make_new_vtable (t, binfo))
1323 /* If we really did make a new vtable, we also made a copy
1324 of the BINFO_VIRTUALS list. Now, we have to find the
1325 corresponding entry in that list. */
1326 *virtuals = BINFO_VIRTUALS (binfo);
1327 while (BV_FN (*virtuals) != BV_FN (v))
1328 *virtuals = TREE_CHAIN (*virtuals);
1332 base_fndecl = BV_FN (v);
1333 BV_DELTA (v) = delta;
1334 BV_VCALL_INDEX (v) = vcall_index;
1337 /* Now assign virtual dispatch information, if unset. We can
1338 dispatch this, through any overridden base function. */
1339 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1341 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
1342 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
1347 /* Call this function whenever its known that a vtable for T is going
1348 to be needed. It's safe to call it more than once. *HAS_VIRTUAL_P
1349 is initialized to the number of slots that are reserved at the
1350 beginning of the vtable for RTTI information. */
1353 start_vtable (t, has_virtual_p)
1357 if (*has_virtual_p == 0 && ! CLASSTYPE_COM_INTERFACE (t))
1359 /* If we are using thunks, use two slots at the front, one
1360 for the offset pointer, one for the tdesc pointer.
1361 For ARM-style vtables, use the same slot for both. */
1362 if (flag_vtable_thunks)
1369 /* Add a virtual function to all the appropriate vtables for the class
1370 T. DECL_VINDEX(X) should be error_mark_node, if we want to
1371 allocate a new slot in our table. If it is error_mark_node, we
1372 know that no other function from another vtable is overridden by X.
1373 HAS_VIRTUAL keeps track of how many virtuals there are in our main
1374 vtable for the type, and we build upon the NEW_VIRTUALS list
1378 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
1379 has_virtual, fndecl, t)
1380 tree *new_virtuals_p;
1381 tree *overridden_virtuals_p;
1384 tree t; /* Structure type. */
1388 /* If this function doesn't override anything from a base class, we
1389 can just assign it a new DECL_VINDEX now. Otherwise, if it does
1390 override something, we keep it around and assign its DECL_VINDEX
1391 later, in modify_all_vtables. */
1392 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
1393 /* We've already dealt with this function. */
1396 new_virtual = build_tree_list (integer_zero_node, fndecl);
1397 BV_VCALL_INDEX (new_virtual) = integer_zero_node;
1399 if (DECL_VINDEX (fndecl) == error_mark_node)
1401 /* FNDECL is a new virtual function; it doesn't override any
1402 virtual function in a base class. */
1404 /* We remember that this was the base sub-object for rtti. */
1405 CLASSTYPE_RTTI (t) = t;
1407 start_vtable (t, has_virtual);
1409 /* Now assign virtual dispatch information. */
1410 DECL_VINDEX (fndecl) = build_shared_int_cst ((*has_virtual)++);
1411 DECL_VIRTUAL_CONTEXT (fndecl) = t;
1413 /* Save the state we've computed on the NEW_VIRTUALS list. */
1414 TREE_CHAIN (new_virtual) = *new_virtuals_p;
1415 *new_virtuals_p = new_virtual;
1419 /* FNDECL overrides a function from a base class. */
1420 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
1421 *overridden_virtuals_p = new_virtual;
1425 extern struct obstack *current_obstack;
1427 /* Add method METHOD to class TYPE.
1429 If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1430 the class type where the method should be added. */
1433 add_method (type, fields, method)
1434 tree type, *fields, method;
1436 int using = (DECL_CONTEXT (method) != type);
1438 if (fields && *fields)
1439 *fields = build_overload (method, *fields);
1446 if (!CLASSTYPE_METHOD_VEC (type))
1447 /* Make a new method vector. We start with 8 entries. We must
1448 allocate at least two (for constructors and destructors), and
1449 we're going to end up with an assignment operator at some
1452 We could use a TREE_LIST for now, and convert it to a
1453 TREE_VEC in finish_struct, but we would probably waste more
1454 memory making the links in the list than we would by
1455 over-allocating the size of the vector here. Furthermore,
1456 we would complicate all the code that expects this to be a
1458 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
1460 method_vec = CLASSTYPE_METHOD_VEC (type);
1461 len = TREE_VEC_LENGTH (method_vec);
1463 if (DECL_NAME (method) == constructor_name (type))
1464 /* A new constructor or destructor. Constructors go in
1465 slot 0; destructors go in slot 1. */
1466 slot = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1469 /* See if we already have an entry with this name. */
1470 for (slot = 2; slot < len; ++slot)
1471 if (!TREE_VEC_ELT (method_vec, slot)
1472 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
1474 == DECL_NAME (method)))
1479 /* We need a bigger method vector. */
1480 tree new_vec = make_tree_vec (2 * len);
1481 bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1482 (PTR) &TREE_VEC_ELT (new_vec, 0),
1483 len * sizeof (tree));
1485 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1488 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
1490 /* Type conversion operators have to come before
1491 ordinary methods; add_conversions depends on this to
1492 speed up looking for conversion operators. So, if
1493 necessary, we slide some of the vector elements up.
1494 In theory, this makes this algorithm O(N^2) but we
1495 don't expect many conversion operators. */
1496 for (slot = 2; slot < len; ++slot)
1498 tree fn = TREE_VEC_ELT (method_vec, slot);
1501 /* There are no more entries in the vector, so we
1502 can insert the new conversion operator here. */
1505 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1506 /* We can insert the new function right at the
1511 if (!TREE_VEC_ELT (method_vec, slot))
1512 /* There is nothing in the Ith slot, so we can avoid
1517 /* We know the last slot in the vector is empty
1518 because we know that at this point there's room
1519 for a new function. */
1520 bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
1521 (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
1522 (len - slot - 1) * sizeof (tree));
1523 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1528 if (template_class_depth (type))
1529 /* TYPE is a template class. Don't issue any errors now; wait
1530 until instantiation time to complain. */
1536 /* Check to see if we've already got this method. */
1537 for (fns = TREE_VEC_ELT (method_vec, slot);
1539 fns = OVL_NEXT (fns))
1541 tree fn = OVL_CURRENT (fns);
1543 if (TREE_CODE (fn) != TREE_CODE (method))
1546 if (TREE_CODE (method) != TEMPLATE_DECL)
1548 /* [over.load] Member function declarations with the
1549 same name and the same parameter types cannot be
1550 overloaded if any of them is a static member
1551 function declaration. */
1552 if ((DECL_STATIC_FUNCTION_P (fn)
1553 != DECL_STATIC_FUNCTION_P (method))
1556 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1557 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1559 if (! DECL_STATIC_FUNCTION_P (fn))
1560 parms1 = TREE_CHAIN (parms1);
1561 if (! DECL_STATIC_FUNCTION_P (method))
1562 parms2 = TREE_CHAIN (parms2);
1564 if (compparms (parms1, parms2))
1567 /* Defer to the local function. */
1570 cp_error ("`%#D' and `%#D' cannot be overloaded",
1575 /* Since this is an ordinary function in a
1576 non-template class, it's mangled name can be used
1577 as a unique identifier. This technique is only
1578 an optimization; we would get the same results if
1579 we just used decls_match here. */
1580 if (DECL_ASSEMBLER_NAME (fn)
1581 != DECL_ASSEMBLER_NAME (method))
1584 else if (!decls_match (fn, method))
1587 /* There has already been a declaration of this method
1588 or member template. */
1589 cp_error_at ("`%D' has already been declared in `%T'",
1592 /* We don't call duplicate_decls here to merge the
1593 declarations because that will confuse things if the
1594 methods have inline definitions. In particular, we
1595 will crash while processing the definitions. */
1600 /* Actually insert the new method. */
1601 TREE_VEC_ELT (method_vec, slot)
1602 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1604 /* Add the new binding. */
1605 if (!DECL_CONSTRUCTOR_P (method)
1606 && !DECL_DESTRUCTOR_P (method))
1607 push_class_level_binding (DECL_NAME (method),
1608 TREE_VEC_ELT (method_vec, slot));
1612 /* Subroutines of finish_struct. */
1614 /* Look through the list of fields for this struct, deleting
1615 duplicates as we go. This must be recursive to handle
1618 FIELD is the field which may not appear anywhere in FIELDS.
1619 FIELD_PTR, if non-null, is the starting point at which
1620 chained deletions may take place.
1621 The value returned is the first acceptable entry found
1624 Note that anonymous fields which are not of UNION_TYPE are
1625 not duplicates, they are just anonymous fields. This happens
1626 when we have unnamed bitfields, for example. */
1629 delete_duplicate_fields_1 (field, fields)
1634 if (DECL_NAME (field) == 0)
1636 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1639 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1640 fields = delete_duplicate_fields_1 (x, fields);
1645 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1647 if (DECL_NAME (x) == 0)
1649 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1651 TYPE_FIELDS (TREE_TYPE (x))
1652 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1653 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1656 fields = TREE_CHAIN (fields);
1658 TREE_CHAIN (prev) = TREE_CHAIN (x);
1661 else if (TREE_CODE (field) == USING_DECL)
1662 /* A using declaration may is allowed to appear more than
1663 once. We'll prune these from the field list later, and
1664 handle_using_decl will complain about invalid multiple
1667 else if (DECL_NAME (field) == DECL_NAME (x))
1669 if (TREE_CODE (field) == CONST_DECL
1670 && TREE_CODE (x) == CONST_DECL)
1671 cp_error_at ("duplicate enum value `%D'", x);
1672 else if (TREE_CODE (field) == CONST_DECL
1673 || TREE_CODE (x) == CONST_DECL)
1674 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1676 else if (DECL_DECLARES_TYPE_P (field)
1677 && DECL_DECLARES_TYPE_P (x))
1679 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1681 cp_error_at ("duplicate nested type `%D'", x);
1683 else if (DECL_DECLARES_TYPE_P (field)
1684 || DECL_DECLARES_TYPE_P (x))
1686 /* Hide tag decls. */
1687 if ((TREE_CODE (field) == TYPE_DECL
1688 && DECL_ARTIFICIAL (field))
1689 || (TREE_CODE (x) == TYPE_DECL
1690 && DECL_ARTIFICIAL (x)))
1692 cp_error_at ("duplicate field `%D' (as type and non-type)",
1696 cp_error_at ("duplicate member `%D'", x);
1698 fields = TREE_CHAIN (fields);
1700 TREE_CHAIN (prev) = TREE_CHAIN (x);
1708 delete_duplicate_fields (fields)
1712 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1713 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1716 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1717 legit, otherwise return 0. */
1720 alter_access (t, fdecl, access)
1725 tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1728 if (TREE_VALUE (elem) != access)
1730 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1731 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1733 error ("conflicting access specifications for field `%s', ignored",
1734 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1738 /* They're changing the access to the same thing they changed
1739 it to before. That's OK. */
1745 enforce_access (t, fdecl);
1746 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1752 /* Process the USING_DECL, which is a member of T. */
1755 handle_using_decl (using_decl, t)
1759 tree ctype = DECL_INITIAL (using_decl);
1760 tree name = DECL_NAME (using_decl);
1762 = TREE_PRIVATE (using_decl) ? access_private_node
1763 : TREE_PROTECTED (using_decl) ? access_protected_node
1764 : access_public_node;
1766 tree flist = NULL_TREE;
1769 binfo = binfo_or_else (ctype, t);
1773 if (name == constructor_name (ctype)
1774 || name == constructor_name_full (ctype))
1776 cp_error_at ("using-declaration for constructor", using_decl);
1780 fdecl = lookup_member (binfo, name, 0, 0);
1784 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1788 if (BASELINK_P (fdecl))
1789 /* Ignore base type this came from. */
1790 fdecl = TREE_VALUE (fdecl);
1792 old_value = IDENTIFIER_CLASS_VALUE (name);
1795 if (is_overloaded_fn (old_value))
1796 old_value = OVL_CURRENT (old_value);
1798 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1801 old_value = NULL_TREE;
1804 if (is_overloaded_fn (fdecl))
1806 else if (! DECL_LANG_SPECIFIC (fdecl))
1807 my_friendly_abort (20000221);
1811 else if (is_overloaded_fn (old_value))
1814 /* It's OK to use functions from a base when there are functions with
1815 the same name already present in the current class. */;
1818 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1819 cp_error_at (" because of local method `%#D' with same name",
1820 OVL_CURRENT (old_value));
1826 cp_error ("`%D' invalid in `%#T'", using_decl, t);
1827 cp_error_at (" because of local field `%#D' with same name", old_value);
1831 /* Make type T see field decl FDECL with access ACCESS.*/
1833 for (; flist; flist = OVL_NEXT (flist))
1835 add_method (t, 0, OVL_CURRENT (flist));
1836 alter_access (t, OVL_CURRENT (flist), access);
1839 alter_access (t, fdecl, access);
1842 /* Run through the base clases of T, updating
1843 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1844 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1848 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1851 int *cant_have_default_ctor_p;
1852 int *cant_have_const_ctor_p;
1853 int *no_const_asn_ref_p;
1857 int seen_nearly_empty_base_p;
1860 binfos = TYPE_BINFO_BASETYPES (t);
1861 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1862 seen_nearly_empty_base_p = 0;
1864 /* An aggregate cannot have baseclasses. */
1865 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1867 for (i = 0; i < n_baseclasses; ++i)
1872 /* Figure out what base we're looking at. */
1873 base_binfo = TREE_VEC_ELT (binfos, i);
1874 basetype = TREE_TYPE (base_binfo);
1876 /* If the type of basetype is incomplete, then we already
1877 complained about that fact (and we should have fixed it up as
1879 if (TYPE_SIZE (basetype) == 0)
1882 /* The base type is of incomplete type. It is
1883 probably best to pretend that it does not
1885 if (i == n_baseclasses-1)
1886 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1887 TREE_VEC_LENGTH (binfos) -= 1;
1889 for (j = i; j+1 < n_baseclasses; j++)
1890 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1894 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1895 here because the case of virtual functions but non-virtual
1896 dtor is handled in finish_struct_1. */
1897 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1898 && TYPE_HAS_DESTRUCTOR (basetype))
1899 cp_warning ("base class `%#T' has a non-virtual destructor",
1902 /* If the base class doesn't have copy constructors or
1903 assignment operators that take const references, then the
1904 derived class cannot have such a member automatically
1906 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1907 *cant_have_const_ctor_p = 1;
1908 if (TYPE_HAS_ASSIGN_REF (basetype)
1909 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1910 *no_const_asn_ref_p = 1;
1911 /* Similarly, if the base class doesn't have a default
1912 constructor, then the derived class won't have an
1913 automatically generated default constructor. */
1914 if (TYPE_HAS_CONSTRUCTOR (basetype)
1915 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1917 *cant_have_default_ctor_p = 1;
1918 if (! TYPE_HAS_CONSTRUCTOR (t))
1919 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1923 /* If the base class is not empty or nearly empty, then this
1924 class cannot be nearly empty. */
1925 if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
1926 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1927 /* And if there is more than one nearly empty base, then the
1928 derived class is not nearly empty either. */
1929 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype)
1930 && seen_nearly_empty_base_p)
1931 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1932 /* If this is the first nearly empty base class, then remember
1934 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1935 seen_nearly_empty_base_p = 1;
1937 /* A lot of properties from the bases also apply to the derived
1939 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1940 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1941 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1942 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1943 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1944 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1945 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1946 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1947 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1948 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1950 /* Derived classes can implicitly become COMified if their bases
1952 if (CLASSTYPE_COM_INTERFACE (basetype))
1953 CLASSTYPE_COM_INTERFACE (t) = 1;
1954 else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
1957 ("COM interface type `%T' with non-COM leftmost base class `%T'",
1959 CLASSTYPE_COM_INTERFACE (t) = 0;
1964 /* Make the Ith baseclass of T its primary base. */
1967 set_primary_base (t, i, has_virtual_p)
1974 CLASSTYPE_VFIELD_PARENT (t) = i;
1975 basetype = BINFO_TYPE (CLASSTYPE_PRIMARY_BINFO (t));
1976 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1977 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1978 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1979 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1980 *has_virtual_p = CLASSTYPE_VSIZE (basetype);
1983 /* Determine the primary class for T. */
1986 determine_primary_base (t, has_virtual_p)
1990 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1992 /* If there are no baseclasses, there is certainly no primary base. */
1993 if (n_baseclasses == 0)
1998 for (i = 0; i < n_baseclasses; i++)
2000 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2001 tree basetype = BINFO_TYPE (base_binfo);
2003 if (TYPE_CONTAINS_VPTR_P (basetype))
2005 /* Even a virtual baseclass can contain our RTTI
2006 information. But, we prefer a non-virtual polymorphic
2008 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2009 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
2011 /* A virtual baseclass can't be the primary base under the
2012 old ABI. And under the new ABI we still prefer a
2013 non-virtual base. */
2014 if (TREE_VIA_VIRTUAL (base_binfo))
2017 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2019 set_primary_base (t, i, has_virtual_p);
2020 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2026 /* Only add unique vfields, and flatten them out as we go. */
2027 for (vfields = CLASSTYPE_VFIELDS (basetype);
2029 vfields = TREE_CHAIN (vfields))
2030 if (VF_BINFO_VALUE (vfields) == NULL_TREE
2031 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
2032 CLASSTYPE_VFIELDS (t)
2033 = tree_cons (base_binfo,
2034 VF_BASETYPE_VALUE (vfields),
2035 CLASSTYPE_VFIELDS (t));
2037 if (*has_virtual_p == 0)
2038 set_primary_base (t, i, has_virtual_p);
2043 if (!TYPE_VFIELD (t))
2044 CLASSTYPE_VFIELD_PARENT (t) = -1;
2046 /* The new ABI allows for the use of a "nearly-empty" virtual base
2047 class as the primary base class if no non-virtual polymorphic
2048 base can be found. */
2049 if (flag_new_abi && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
2050 for (i = 0; i < n_baseclasses; ++i)
2052 tree base_binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), i);
2053 tree basetype = BINFO_TYPE (base_binfo);
2055 if (TREE_VIA_VIRTUAL (base_binfo)
2056 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
2058 set_primary_base (t, i, has_virtual_p);
2059 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
2064 /* Mark the primary base classes at this point. */
2065 mark_primary_bases (t);
2068 /* Set memoizing fields and bits of T (and its variants) for later
2072 finish_struct_bits (t)
2075 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2077 /* Fix up variants (if any). */
2078 tree variants = TYPE_NEXT_VARIANT (t);
2081 /* These fields are in the _TYPE part of the node, not in
2082 the TYPE_LANG_SPECIFIC component, so they are not shared. */
2083 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
2084 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
2085 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
2086 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
2087 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
2089 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
2090 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
2091 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
2092 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
2093 /* Copy whatever these are holding today. */
2094 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
2095 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
2096 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
2097 TYPE_SIZE (variants) = TYPE_SIZE (t);
2098 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
2099 variants = TYPE_NEXT_VARIANT (variants);
2102 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
2103 /* For a class w/o baseclasses, `finish_struct' has set
2104 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
2105 definition). Similarly for a class whose base classes do not
2106 have vtables. When neither of these is true, we might have
2107 removed abstract virtuals (by providing a definition), added
2108 some (by declaring new ones), or redeclared ones from a base
2109 class. We need to recalculate what's really an abstract virtual
2110 at this point (by looking in the vtables). */
2111 get_pure_virtuals (t);
2115 /* Notice whether this class has type conversion functions defined. */
2116 tree binfo = TYPE_BINFO (t);
2117 tree binfos = BINFO_BASETYPES (binfo);
2120 for (i = n_baseclasses-1; i >= 0; i--)
2122 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
2124 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
2128 /* If this type has a copy constructor, force its mode to be BLKmode, and
2129 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
2130 be passed by invisible reference and prevent it from being returned in
2133 Also do this if the class has BLKmode but can still be returned in
2134 registers, since function_cannot_inline_p won't let us inline
2135 functions returning such a type. This affects the HP-PA. */
2136 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
2137 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
2138 && CLASSTYPE_NON_AGGREGATE (t)))
2141 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
2142 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
2144 TYPE_MODE (variants) = BLKmode;
2145 TREE_ADDRESSABLE (variants) = 1;
2150 /* Issue warnings about T having private constructors, but no friends,
2153 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2154 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2155 non-private static member functions. */
2158 maybe_warn_about_overly_private_class (t)
2161 int has_member_fn = 0;
2162 int has_nonprivate_method = 0;
2165 if (!warn_ctor_dtor_privacy
2166 /* If the class has friends, those entities might create and
2167 access instances, so we should not warn. */
2168 || (CLASSTYPE_FRIEND_CLASSES (t)
2169 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2170 /* We will have warned when the template was declared; there's
2171 no need to warn on every instantiation. */
2172 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2173 /* There's no reason to even consider warning about this
2177 /* We only issue one warning, if more than one applies, because
2178 otherwise, on code like:
2181 // Oops - forgot `public:'
2187 we warn several times about essentially the same problem. */
2189 /* Check to see if all (non-constructor, non-destructor) member
2190 functions are private. (Since there are no friends or
2191 non-private statics, we can't ever call any of the private member
2193 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2194 /* We're not interested in compiler-generated methods; they don't
2195 provide any way to call private members. */
2196 if (!DECL_ARTIFICIAL (fn))
2198 if (!TREE_PRIVATE (fn))
2200 if (DECL_STATIC_FUNCTION_P (fn))
2201 /* A non-private static member function is just like a
2202 friend; it can create and invoke private member
2203 functions, and be accessed without a class
2207 has_nonprivate_method = 1;
2210 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2214 if (!has_nonprivate_method && has_member_fn)
2216 /* There are no non-private methods, and there's at least one
2217 private member function that isn't a constructor or
2218 destructor. (If all the private members are
2219 constructors/destructors we want to use the code below that
2220 issues error messages specifically referring to
2221 constructors/destructors.) */
2223 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2224 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2225 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2226 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2228 has_nonprivate_method = 1;
2231 if (!has_nonprivate_method)
2233 cp_warning ("all member functions in class `%T' are private", t);
2238 /* Even if some of the member functions are non-private, the class
2239 won't be useful for much if all the constructors or destructors
2240 are private: such an object can never be created or destroyed. */
2241 if (TYPE_HAS_DESTRUCTOR (t))
2243 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2245 if (TREE_PRIVATE (dtor))
2247 cp_warning ("`%#T' only defines a private destructor and has no friends",
2253 if (TYPE_HAS_CONSTRUCTOR (t))
2255 int nonprivate_ctor = 0;
2257 /* If a non-template class does not define a copy
2258 constructor, one is defined for it, enabling it to avoid
2259 this warning. For a template class, this does not
2260 happen, and so we would normally get a warning on:
2262 template <class T> class C { private: C(); };
2264 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2265 complete non-template or fully instantiated classes have this
2267 if (!TYPE_HAS_INIT_REF (t))
2268 nonprivate_ctor = 1;
2270 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2274 tree ctor = OVL_CURRENT (fn);
2275 /* Ideally, we wouldn't count copy constructors (or, in
2276 fact, any constructor that takes an argument of the
2277 class type as a parameter) because such things cannot
2278 be used to construct an instance of the class unless
2279 you already have one. But, for now at least, we're
2281 if (! TREE_PRIVATE (ctor))
2283 nonprivate_ctor = 1;
2288 if (nonprivate_ctor == 0)
2290 cp_warning ("`%#T' only defines private constructors and has no friends",
2297 /* Function to help qsort sort FIELD_DECLs by name order. */
2300 field_decl_cmp (x, y)
2303 if (DECL_NAME (*x) == DECL_NAME (*y))
2304 /* A nontype is "greater" than a type. */
2305 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2306 if (DECL_NAME (*x) == NULL_TREE)
2308 if (DECL_NAME (*y) == NULL_TREE)
2310 if (DECL_NAME (*x) < DECL_NAME (*y))
2315 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2318 method_name_cmp (m1, m2)
2319 const tree *m1, *m2;
2321 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2323 if (*m1 == NULL_TREE)
2325 if (*m2 == NULL_TREE)
2327 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2332 /* Warn about duplicate methods in fn_fields. Also compact method
2333 lists so that lookup can be made faster.
2335 Data Structure: List of method lists. The outer list is a
2336 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2337 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2338 links the entire list of methods for TYPE_METHODS. Friends are
2339 chained in the same way as member functions (? TREE_CHAIN or
2340 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2341 list. That allows them to be quickly deleted, and requires no
2344 If there are any constructors/destructors, they are moved to the
2345 front of the list. This makes pushclass more efficient.
2347 @@ The above comment is obsolete. It mostly describes what add_method
2348 @@ and add_implicitly_declared_members do.
2350 Sort methods that are not special (i.e., constructors, destructors, and
2351 type conversion operators) so that we can find them faster in search. */
2354 finish_struct_methods (t)
2359 tree ctor_name = constructor_name (t);
2362 if (!TYPE_METHODS (t))
2364 /* Clear these for safety; perhaps some parsing error could set
2365 these incorrectly. */
2366 TYPE_HAS_CONSTRUCTOR (t) = 0;
2367 TYPE_HAS_DESTRUCTOR (t) = 0;
2368 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2372 method_vec = CLASSTYPE_METHOD_VEC (t);
2373 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2374 len = TREE_VEC_LENGTH (method_vec);
2376 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2377 and the next few with type conversion operators (if any). */
2378 for (fn_fields = TYPE_METHODS (t); fn_fields;
2379 fn_fields = TREE_CHAIN (fn_fields))
2381 tree fn_name = DECL_NAME (fn_fields);
2383 /* Clear out this flag.
2385 @@ Doug may figure out how to break
2386 @@ this with nested classes and friends. */
2387 DECL_IN_AGGR_P (fn_fields) = 0;
2389 /* Note here that a copy ctor is private, so we don't dare generate
2390 a default copy constructor for a class that has a member
2391 of this type without making sure they have access to it. */
2392 if (fn_name == ctor_name)
2394 tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2395 tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2397 if (TREE_CODE (parmtype) == REFERENCE_TYPE
2398 && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2400 if (TREE_CHAIN (parmtypes) == NULL_TREE
2401 || TREE_CHAIN (parmtypes) == void_list_node
2402 || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2404 if (TREE_PROTECTED (fn_fields))
2405 TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2406 else if (TREE_PRIVATE (fn_fields))
2407 TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2411 else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2413 tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2415 if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2417 if (TREE_PROTECTED (fn_fields))
2418 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2419 else if (TREE_PRIVATE (fn_fields))
2420 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2425 if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2426 /* We thought there was a destructor, but there wasn't. Some
2427 parse errors cause this anomalous situation. */
2428 TYPE_HAS_DESTRUCTOR (t) = 0;
2430 /* Issue warnings about private constructors and such. If there are
2431 no methods, then some public defaults are generated. */
2432 maybe_warn_about_overly_private_class (t);
2434 /* Now sort the methods. */
2435 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2437 TREE_VEC_LENGTH (method_vec) = len;
2439 /* The type conversion ops have to live at the front of the vec, so we
2441 for (slot = 2; slot < len; ++slot)
2443 tree fn = TREE_VEC_ELT (method_vec, slot);
2445 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2449 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2450 (int (*)(const void *, const void *))method_name_cmp);
2453 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2456 duplicate_tag_error (t)
2459 cp_error ("redefinition of `%#T'", t);
2460 cp_error_at ("previous definition here", t);
2462 /* Pretend we haven't defined this type. */
2464 /* All of the component_decl's were TREE_CHAINed together in the parser.
2465 finish_struct_methods walks these chains and assembles all methods with
2466 the same base name into DECL_CHAINs. Now we don't need the parser chains
2467 anymore, so we unravel them. */
2469 /* This used to be in finish_struct, but it turns out that the
2470 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2472 if (CLASSTYPE_METHOD_VEC (t))
2474 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2475 int i, len = TREE_VEC_LENGTH (method_vec);
2476 for (i = 0; i < len; i++)
2478 tree unchain = TREE_VEC_ELT (method_vec, i);
2479 while (unchain != NULL_TREE)
2481 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2482 unchain = OVL_NEXT (unchain);
2487 if (TYPE_LANG_SPECIFIC (t))
2489 tree binfo = TYPE_BINFO (t);
2490 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2491 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2492 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2493 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2495 bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2496 BINFO_BASETYPES(binfo) = NULL_TREE;
2498 TYPE_BINFO (t) = binfo;
2499 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2500 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2501 TYPE_REDEFINED (t) = 1;
2502 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2503 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2505 TYPE_SIZE (t) = NULL_TREE;
2506 TYPE_MODE (t) = VOIDmode;
2507 TYPE_FIELDS (t) = NULL_TREE;
2508 TYPE_METHODS (t) = NULL_TREE;
2509 TYPE_VFIELD (t) = NULL_TREE;
2510 TYPE_CONTEXT (t) = NULL_TREE;
2511 TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
2514 /* Make the BINFO's vtablehave N entries, including RTTI entries,
2515 vbase and vcall offsets, etc. Set its type and call the backend
2519 layout_vtable_decl (binfo, n)
2526 itype = size_int (n);
2527 atype = build_cplus_array_type (vtable_entry_type,
2528 build_index_type (itype));
2529 layout_type (atype);
2531 /* We may have to grow the vtable. */
2532 if (!same_type_p (TREE_TYPE (BINFO_VTABLE (binfo)), atype))
2534 tree vtable = BINFO_VTABLE (binfo);
2536 TREE_TYPE (vtable) = atype;
2537 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = 0;
2538 layout_decl (vtable, 0);
2540 /* At one time the vtable info was grabbed 2 words at a time. This
2541 fails on Sparc unless you have 8-byte alignment. */
2542 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2543 DECL_ALIGN (vtable));
2547 /* Returns the number of virtual function table entries (excluding
2548 RTTI information, vbase and vcall offests, etc.) in the vtable for
2552 num_vfun_entries (binfo)
2555 return list_length (skip_rtti_stuff (binfo,
2560 /* Called from num_extra_vtbl_entries via dfs_walk. */
2563 dfs_count_virtuals (binfo, data)
2567 /* Non-primary bases are not interesting; all of the virtual
2568 function table entries have been overridden. */
2569 if (!BINFO_PRIMARY_MARKED_P (binfo))
2570 ((vcall_offset_data *) data)->offsets += num_vfun_entries (binfo);
2575 /* Returns the number of extra entries (at negative indices) required
2576 for BINFO's vtable. */
2579 num_extra_vtbl_entries (binfo)
2585 type = BINFO_TYPE (binfo);
2588 /* There is an entry for the offset to each virtual base. */
2589 if (vbase_offsets_in_vtable_p ())
2590 entries += list_length (CLASSTYPE_VBASECLASSES (type));
2592 /* If this is a virtual base, there are entries for each virtual
2593 function defined in this class or its bases. */
2594 if (vcall_offsets_in_vtable_p () && TREE_VIA_VIRTUAL (binfo))
2596 vcall_offset_data vod;
2602 dfs_vcall_offset_queue_p,
2604 entries += vod.offsets;
2607 return entries ? size_int (entries) : size_zero_node;
2610 /* Returns the offset (in bytes) from the beginning of BINFO's vtable
2611 where the vptr should actually point. */
2614 size_extra_vtbl_entries (binfo)
2617 tree offset = size_binop (MULT_EXPR, TYPE_SIZE_UNIT (vtable_entry_type),
2618 num_extra_vtbl_entries (binfo));
2619 return fold (offset);
2622 /* Construct the initializer for BINFOs virtual function table. BINFO
2623 is part of the hierarchy dominated by T. The value returned is a
2624 TREE_LIST suitable for wrapping in a CONSTRUCTOR to use as the
2625 DECL_INITIAL for a vtable. */
2628 build_vtbl_initializer (binfo, t)
2632 tree v = BINFO_VIRTUALS (binfo);
2633 tree inits = NULL_TREE;
2634 tree type = BINFO_TYPE (binfo);
2636 /* Add entries to the vtable that indicate how to adjust the this
2637 pointer when calling a virtual function in this class. */
2638 inits = build_vcall_offset_vtbl_entries (binfo, t);
2640 /* Add entries to the vtable for offsets to our virtual bases. */
2641 inits = chainon (build_vbase_offset_vtbl_entries (binfo, t),
2644 /* Process the RTTI stuff at the head of the list. If we're not
2645 using vtable thunks, then the RTTI entry is just an ordinary
2646 function, and we can process it just like the other virtual
2647 function entries. */
2648 if (!CLASSTYPE_COM_INTERFACE (type) && flag_vtable_thunks)
2653 /* The first entry is an offset. */
2654 offset = TREE_PURPOSE (v);
2655 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST,
2658 /* Convert the offset to look like a function pointer, so that
2659 we can put it in the vtable. */
2660 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
2661 TREE_CONSTANT (init) = 1;
2662 inits = tree_cons (NULL_TREE, init, inits);
2666 if (new_abi_rtti_p ())
2668 tree decl = TREE_VALUE (v);
2671 decl = build_unary_op (ADDR_EXPR, decl, 0);
2673 decl = integer_zero_node;
2674 decl = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
2675 TREE_CONSTANT (decl) = 1;
2676 decl = build_vtable_entry (integer_zero_node, integer_zero_node,
2678 inits = tree_cons (NULL_TREE, decl, inits);
2682 /* In the old abi the second entry (the tdesc pointer) is
2683 just an ordinary function, so it can be dealt with like the
2684 virtual functions. */
2687 /* Go through all the ordinary virtual functions, building up
2697 /* Pull the offset for `this', and the function to call, out of
2699 delta = BV_DELTA (v);
2700 vcall_index = BV_VCALL_INDEX (v);
2702 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
2703 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
2705 /* You can't call an abstract virtual function; it's abstract.
2706 So, we replace these functions with __pure_virtual. */
2707 if (DECL_PURE_VIRTUAL_P (fn))
2710 /* Take the address of the function, considering it to be of an
2711 appropriate generic type. */
2712 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
2713 /* The address of a function can't change. */
2714 TREE_CONSTANT (pfn) = 1;
2715 /* Enter it in the vtable. */
2716 init = build_vtable_entry (delta, vcall_index, pfn);
2717 /* And add it to the chain of initializers. */
2718 inits = tree_cons (NULL_TREE, init, inits);
2724 /* The initializers were built up in reverse order; straighten them
2726 return nreverse (inits);
2729 /* Initialize the vtable for BINFO with the INITS. */
2732 initialize_vtable (binfo, inits)
2739 layout_vtable_decl (binfo, list_length (inits));
2740 decl = BINFO_VTABLE (binfo);
2741 context = DECL_CONTEXT (decl);
2742 DECL_CONTEXT (decl) = 0;
2743 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
2744 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
2745 DECL_CONTEXT (decl) = context;
2748 /* Called from finish_vtbls via dfs_walk. */
2751 dfs_finish_vtbls (binfo, data)
2755 if (!BINFO_PRIMARY_MARKED_P (binfo)
2756 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2757 && BINFO_NEW_VTABLE_MARKED (binfo))
2758 initialize_vtable (binfo,
2759 build_vtbl_initializer (binfo, (tree) data));
2761 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2762 SET_BINFO_MARKED (binfo);
2767 /* Called from finish_vtbls via dfs_walk when using the new ABI.
2768 Accumulates the vtable initializers for all of the vtables into
2769 TREE_VALUE (DATA). */
2772 dfs_accumulate_vtbl_inits (binfo, data)
2776 if (!BINFO_PRIMARY_MARKED_P (binfo)
2777 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo))
2778 && BINFO_NEW_VTABLE_MARKED (binfo))
2784 t = TREE_PURPOSE (l);
2786 /* If this is a secondary vtable, record its location. */
2787 if (binfo != TYPE_BINFO (t))
2791 vtbl = TYPE_BINFO_VTABLE (t);
2792 vtbl = build1 (ADDR_EXPR,
2793 build_pointer_type (TREE_TYPE (vtbl)),
2795 BINFO_VTABLE (binfo)
2796 = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl,
2797 size_binop (MULT_EXPR,
2798 TYPE_SIZE_UNIT (TREE_TYPE (vtbl)),
2799 size_int (list_length (TREE_VALUE (l)))));
2802 /* Add the initializers for this vtable to the initailizers for
2803 the other vtables we've already got. */
2805 = chainon (TREE_VALUE (l),
2806 build_vtbl_initializer (binfo, t));
2809 CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2810 SET_BINFO_MARKED (binfo);
2815 /* Create all the necessary vtables for T and its base classes. */
2821 if (merge_primary_and_secondary_vtables_p ())
2825 /* Under the new ABI, we lay out the primary and secondary
2826 vtables in one contiguous vtable. The primary vtable is
2827 first, followed by the secondary vtables as encountered in a
2828 pre-order depth-first left-to-right traversal. */
2829 list = build_tree_list (t, NULL_TREE);
2830 dfs_walk_real (TYPE_BINFO (t),
2831 dfs_accumulate_vtbl_inits,
2833 dfs_unmarked_real_bases_queue_p,
2835 if (TYPE_BINFO_VTABLE (t))
2836 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
2839 dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls,
2840 dfs_unmarked_real_bases_queue_p, t);
2842 dfs_walk (TYPE_BINFO (t), dfs_unmark,
2843 dfs_marked_real_bases_queue_p, t);
2846 /* True if we should override the given BASE_FNDECL with the given
2850 overrides (fndecl, base_fndecl)
2851 tree fndecl, base_fndecl;
2853 /* Destructors have special names. */
2854 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2855 && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2857 if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2858 || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2860 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2862 tree types, base_types;
2864 retypes = TREE_TYPE (TREE_TYPE (fndecl));
2865 base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2867 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2868 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2869 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2870 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2871 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2877 typedef struct find_final_overrider_data_s {
2878 /* The function for which we are trying to find a final overrider. */
2880 /* The base class in which the function was declared. */
2881 tree declaring_base;
2882 /* The most derived class in the hierarchy. */
2883 tree most_derived_type;
2884 /* The final overriding function. */
2886 /* The BINFO for the class in which the final overriding function
2888 tree overriding_base;
2889 } find_final_overrider_data;
2891 /* Called from find_final_overrider via dfs_walk. */
2894 dfs_find_final_overrider (binfo, data)
2898 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2900 if (same_type_p (BINFO_TYPE (binfo),
2901 BINFO_TYPE (ffod->declaring_base))
2902 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2903 BINFO_OFFSET (ffod->declaring_base)))
2908 /* We've found a path to the declaring base. Walk down the path
2909 looking for an overrider for FN. */
2910 for (path = reverse_path (binfo);
2912 path = TREE_CHAIN (path))
2914 for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
2916 method = TREE_CHAIN (method))
2917 if (DECL_VIRTUAL_P (method) && overrides (method, ffod->fn))
2924 /* If we found an overrider, record the overriding function, and
2925 the base from which it came. */
2928 if (ffod->overriding_fn && ffod->overriding_fn != method)
2930 /* We've found a different overrider along a different
2931 path. That can be OK if the new one overrides the
2934 struct S { virtual void f(); };
2935 struct T : public virtual S { virtual void f(); };
2936 struct U : public virtual S, public virtual T {};
2938 Here `T::f' is the final overrider for `S::f'. */
2939 if (strictly_overrides (method, ffod->overriding_fn))
2941 ffod->overriding_fn = method;
2942 ffod->overriding_base = TREE_VALUE (path);
2944 else if (!strictly_overrides (ffod->overriding_fn, method))
2946 cp_error ("no unique final overrider for `%D' in `%T'",
2947 ffod->most_derived_type,
2949 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
2950 cp_error (" `%#D'", method);
2951 return error_mark_node;
2954 else if (ffod->overriding_base
2955 && (!tree_int_cst_equal
2956 (BINFO_OFFSET (TREE_VALUE (path)),
2957 BINFO_OFFSET (ffod->overriding_base))))
2959 /* We've found two instances of the same base that
2960 provide overriders. */
2961 cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'",
2963 BINFO_TYPE (ffod->overriding_base),
2964 ffod->most_derived_type);
2965 return error_mark_node;
2969 ffod->overriding_fn = method;
2970 ffod->overriding_base = TREE_VALUE (path);
2978 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2979 FN and whose TREE_VALUE is the binfo for the base where the
2980 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2981 base object in which FN is declared. */
2984 find_final_overrider (t, binfo, fn)
2989 find_final_overrider_data ffod;
2991 /* Getting this right is a little tricky. This is legal:
2993 struct S { virtual void f (); };
2994 struct T { virtual void f (); };
2995 struct U : public S, public T { };
2997 even though calling `f' in `U' is ambiguous. But,
2999 struct R { virtual void f(); };
3000 struct S : virtual public R { virtual void f (); };
3001 struct T : virtual public R { virtual void f (); };
3002 struct U : public S, public T { };
3004 is not -- there's no way to decide whether to put `S::f' or
3005 `T::f' in the vtable for `R'.
3007 The solution is to look at all paths to BINFO. If we find
3008 different overriders along any two, then there is a problem. */
3010 ffod.declaring_base = binfo;
3011 ffod.most_derived_type = t;
3012 ffod.overriding_fn = NULL_TREE;
3013 ffod.overriding_base = NULL_TREE;
3015 if (dfs_walk (TYPE_BINFO (t),
3016 dfs_find_final_overrider,
3019 return error_mark_node;
3021 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
3024 /* Return the BINFO_VIRTUALS list for BINFO, without the RTTI stuff at
3025 the front. If non-NULL, N is set to the number of entries
3029 skip_rtti_stuff (binfo, t, n)
3036 if (CLASSTYPE_COM_INTERFACE (t))
3041 virtuals = BINFO_VIRTUALS (binfo);
3044 /* We always reserve a slot for the offset/tdesc entry. */
3047 virtuals = TREE_CHAIN (virtuals);
3049 if (flag_vtable_thunks && virtuals)
3051 /* The second slot is reserved for the tdesc pointer when thunks
3055 virtuals = TREE_CHAIN (virtuals);
3061 /* Called via dfs_walk. Returns BINFO if BINFO has the same type as
3062 DATA (which is really an _TYPE node). */
3065 dfs_find_base (binfo, data)
3069 return (same_type_p (BINFO_TYPE (binfo), (tree) data)
3070 ? binfo : NULL_TREE);
3073 /* Called from modify_all_vtables via dfs_walk. */
3076 dfs_modify_vtables (binfo, data)
3080 if (/* There's no need to modify the vtable for a primary base;
3081 we're not going to use that vtable anyhow. */
3082 !BINFO_PRIMARY_MARKED_P (binfo)
3083 /* Similarly, a base without a vtable needs no modification. */
3084 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
3092 /* If we're supporting RTTI then we always need a new vtable to
3093 point to the RTTI information. Under the new ABI we may need
3094 a new vtable to contain vcall and vbase offsets. */
3095 if (flag_rtti || flag_new_abi)
3096 make_new_vtable (t, binfo);
3098 /* Now, go through each of the virtual functions in the virtual
3099 function table for BINFO. Find the final overrider, and
3100 update the BINFO_VIRTUALS list appropriately. */
3101 for (virtuals = skip_rtti_stuff (binfo, BINFO_TYPE (binfo), NULL),
3102 old_virtuals = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (binfo)),
3106 virtuals = TREE_CHAIN (virtuals),
3107 old_virtuals = TREE_CHAIN (old_virtuals))
3114 HOST_WIDE_INT vindex_val, i;
3117 /* Find the function which originally caused this vtable
3118 entry to be present. */
3119 fn = BV_FN (old_virtuals);
3120 vindex = DECL_VINDEX (fn);
3121 b = dfs_walk (binfo, dfs_find_base, NULL, DECL_VIRTUAL_CONTEXT (fn));
3122 fn = skip_rtti_stuff (TYPE_BINFO (BINFO_TYPE (b)),
3125 vindex_val = tree_low_cst (vindex, 0);
3126 while (i < vindex_val)
3128 fn = TREE_CHAIN (fn);
3133 /* Handle the case of a virtual function defined in BINFO
3135 overrider = find_final_overrider (t, b, fn);
3136 if (overrider == error_mark_node)
3139 /* The `this' pointer needs to be adjusted from pointing to
3140 BINFO to pointing at the base where the final overrider
3142 delta = size_binop (PLUS_EXPR,
3143 get_derived_offset (binfo,
3144 DECL_VIRTUAL_CONTEXT (fn)),
3145 BINFO_OFFSET (binfo));
3146 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
3148 modify_vtable_entry (t,
3150 TREE_PURPOSE (overrider),
3156 SET_BINFO_MARKED (binfo);
3161 /* Update all of the primary and secondary vtables for T. Create new
3162 vtables as required, and initialize their RTTI information. Each
3163 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
3164 function from a base class; find and modify the appropriate entries
3165 to point to the overriding functions. Returns a list, in
3166 declaration order, of the functions that are overridden in this
3167 class, but do not appear in the primary base class vtable, and
3168 which should therefore be appended to the end of the vtable for T. */
3171 modify_all_vtables (t, has_virtual_p, overridden_virtuals)
3174 tree overridden_virtuals;
3178 binfo = TYPE_BINFO (t);
3180 /* Update all of the vtables. */
3183 dfs_unmarked_real_bases_queue_p,
3185 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
3187 /* If we should include overriding functions for secondary vtables
3188 in our primary vtable, add them now. */
3189 if (all_overridden_vfuns_in_vtables_p ())
3191 tree *fnsp = &overridden_virtuals;
3195 tree fn = TREE_VALUE (*fnsp);
3197 if (BINFO_VIRTUALS (binfo)
3198 && !value_member (fn, BINFO_VIRTUALS (binfo)))
3200 /* We know we need a vtable for this class now. */
3201 start_vtable (t, has_virtual_p);
3202 /* Set the vtable index. */
3204 = build_shared_int_cst ((*has_virtual_p)++);
3205 /* We don't need to convert to a base class when calling
3207 DECL_VIRTUAL_CONTEXT (fn) = t;
3209 /* We don't need to adjust the `this' pointer when
3210 calling this function. */
3211 BV_DELTA (*fnsp) = integer_zero_node;
3212 BV_VCALL_INDEX (*fnsp) = integer_zero_node;
3214 /* This is an overridden function not already in our
3216 fnsp = &TREE_CHAIN (*fnsp);
3219 /* We've already got an entry for this function. Skip
3221 *fnsp = TREE_CHAIN (*fnsp);
3225 overridden_virtuals = NULL_TREE;
3227 return overridden_virtuals;
3230 /* Here, we already know that they match in every respect.
3231 All we have to check is where they had their declarations. */
3234 strictly_overrides (fndecl1, fndecl2)
3235 tree fndecl1, fndecl2;
3237 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
3238 DECL_CONTEXT (fndecl1),
3240 if (distance == -2 || distance > 0)
3245 /* Get the base virtual function declarations in T that are either
3246 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
3247 the overrider/hider. */
3250 get_basefndecls (fndecl, t)
3253 tree methods = TYPE_METHODS (t);
3254 tree base_fndecls = NULL_TREE;
3255 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3256 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3260 if (TREE_CODE (methods) == FUNCTION_DECL
3261 && DECL_VINDEX (methods) != NULL_TREE
3262 && DECL_NAME (fndecl) == DECL_NAME (methods))
3263 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
3265 methods = TREE_CHAIN (methods);
3269 return base_fndecls;
3271 for (i = 0; i < n_baseclasses; i++)
3273 tree base_binfo = TREE_VEC_ELT (binfos, i);
3274 tree basetype = BINFO_TYPE (base_binfo);
3276 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3280 return base_fndecls;
3283 /* Mark the functions that have been hidden with their overriders.
3284 Since we start out with all functions already marked with a hider,
3285 no need to mark functions that are just hidden.
3287 Subroutine of warn_hidden. */
3290 mark_overriders (fndecl, base_fndecls)
3291 tree fndecl, base_fndecls;
3293 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3295 if (overrides (fndecl, TREE_VALUE (base_fndecls)))
3296 TREE_PURPOSE (base_fndecls) = fndecl;
3300 /* If this declaration supersedes the declaration of
3301 a method declared virtual in the base class, then
3302 mark this field as being virtual as well. */
3305 check_for_override (decl, ctype)
3308 tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
3309 int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3310 int virtualp = DECL_VIRTUAL_P (decl);
3311 int found_overriden_fn = 0;
3313 for (i = 0; i < n_baselinks; i++)
3315 tree base_binfo = TREE_VEC_ELT (binfos, i);
3316 if (TYPE_POLYMORPHIC_P (BINFO_TYPE (base_binfo)))
3318 tree tmp = get_matching_virtual
3320 DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
3322 if (tmp && !found_overriden_fn)
3324 /* If this function overrides some virtual in some base
3325 class, then the function itself is also necessarily
3326 virtual, even if the user didn't explicitly say so. */
3327 DECL_VIRTUAL_P (decl) = 1;
3329 /* The TMP we really want is the one from the deepest
3330 baseclass on this path, taking care not to
3331 duplicate if we have already found it (via another
3332 path to its virtual baseclass. */
3333 if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3335 cp_error_at ("`static %#D' cannot be declared", decl);
3336 cp_error_at (" since `virtual %#D' declared in base class",
3342 /* Set DECL_VINDEX to a value that is neither an
3343 INTEGER_CST nor the error_mark_node so that
3344 add_virtual_function will realize this is an
3345 overridden function. */
3347 = tree_cons (tmp, NULL_TREE, DECL_VINDEX (decl));
3349 /* We now know that DECL overrides something,
3350 which is all that is important. But, we must
3351 continue to iterate through all the base-classes
3352 in order to allow get_matching_virtual to check for
3353 various illegal overrides. */
3354 found_overriden_fn = 1;
3360 if (DECL_VINDEX (decl) == NULL_TREE)
3361 DECL_VINDEX (decl) = error_mark_node;
3362 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3366 /* Warn about hidden virtual functions that are not overridden in t.
3367 We know that constructors and destructors don't apply. */
3373 tree method_vec = CLASSTYPE_METHOD_VEC (t);
3374 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3377 /* We go through each separately named virtual function. */
3378 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3380 tree fns = TREE_VEC_ELT (method_vec, i);
3381 tree fndecl = NULL_TREE;
3383 tree base_fndecls = NULL_TREE;
3384 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3385 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3387 /* First see if we have any virtual functions in this batch. */
3388 for (; fns; fns = OVL_NEXT (fns))
3390 fndecl = OVL_CURRENT (fns);
3391 if (DECL_VINDEX (fndecl))
3395 if (fns == NULL_TREE)
3398 /* First we get a list of all possible functions that might be
3399 hidden from each base class. */
3400 for (i = 0; i < n_baseclasses; i++)
3402 tree base_binfo = TREE_VEC_ELT (binfos, i);
3403 tree basetype = BINFO_TYPE (base_binfo);
3405 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3409 fns = OVL_NEXT (fns);
3411 /* ...then mark up all the base functions with overriders, preferring
3412 overriders to hiders. */
3414 for (; fns; fns = OVL_NEXT (fns))
3416 fndecl = OVL_CURRENT (fns);
3417 if (DECL_VINDEX (fndecl))
3418 mark_overriders (fndecl, base_fndecls);
3421 /* Now give a warning for all base functions without overriders,
3422 as they are hidden. */
3423 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
3425 if (! overrides (TREE_PURPOSE (base_fndecls),
3426 TREE_VALUE (base_fndecls)))
3428 /* Here we know it is a hider, and no overrider exists. */
3429 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3430 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
3436 /* Check for things that are invalid. There are probably plenty of other
3437 things we should check for also. */
3440 finish_struct_anon (t)
3445 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3447 if (TREE_STATIC (field))
3449 if (TREE_CODE (field) != FIELD_DECL)
3452 if (DECL_NAME (field) == NULL_TREE
3453 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
3455 tree elt = TYPE_FIELDS (TREE_TYPE (field));
3456 for (; elt; elt = TREE_CHAIN (elt))
3458 if (DECL_ARTIFICIAL (elt))
3461 if (DECL_NAME (elt) == constructor_name (t))
3462 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
3465 if (TREE_CODE (elt) != FIELD_DECL)
3467 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
3472 if (TREE_PRIVATE (elt))
3473 cp_pedwarn_at ("private member `%#D' in anonymous union",
3475 else if (TREE_PROTECTED (elt))
3476 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3479 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
3480 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
3486 extern int interface_only, interface_unknown;
3488 /* Create default constructors, assignment operators, and so forth for
3489 the type indicated by T, if they are needed.
3490 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3491 CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3492 cannot have a default constructor, copy constructor taking a const
3493 reference argument, or an assignment operator, respectively. If a
3494 virtual destructor is created, its DECL is returned; otherwise the
3495 return value is NULL_TREE. */
3498 add_implicitly_declared_members (t, cant_have_default_ctor,
3499 cant_have_const_cctor,
3500 cant_have_assignment)
3502 int cant_have_default_ctor;
3503 int cant_have_const_cctor;
3504 int cant_have_assignment;
3507 tree implicit_fns = NULL_TREE;
3508 tree name = TYPE_IDENTIFIER (t);
3509 tree virtual_dtor = NULL_TREE;
3513 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
3515 default_fn = cons_up_default_function (t, name, 0);
3516 check_for_override (default_fn, t);
3518 /* If we couldn't make it work, then pretend we didn't need it. */
3519 if (default_fn == void_type_node)
3520 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3523 TREE_CHAIN (default_fn) = implicit_fns;
3524 implicit_fns = default_fn;
3526 if (DECL_VINDEX (default_fn))
3527 virtual_dtor = default_fn;
3531 /* Any non-implicit destructor is non-trivial. */
3532 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3534 /* Default constructor. */
3535 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3537 default_fn = cons_up_default_function (t, name, 2);
3538 TREE_CHAIN (default_fn) = implicit_fns;
3539 implicit_fns = default_fn;
3542 /* Copy constructor. */
3543 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3545 /* ARM 12.18: You get either X(X&) or X(const X&), but
3547 default_fn = cons_up_default_function (t, name,
3548 3 + cant_have_const_cctor);
3549 TREE_CHAIN (default_fn) = implicit_fns;
3550 implicit_fns = default_fn;
3553 /* Assignment operator. */
3554 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3556 default_fn = cons_up_default_function (t, name,
3557 5 + cant_have_assignment);
3558 TREE_CHAIN (default_fn) = implicit_fns;
3559 implicit_fns = default_fn;
3562 /* Now, hook all of the new functions on to TYPE_METHODS,
3563 and add them to the CLASSTYPE_METHOD_VEC. */
3564 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3565 add_method (t, 0, *f);
3566 *f = TYPE_METHODS (t);
3567 TYPE_METHODS (t) = implicit_fns;
3569 return virtual_dtor;
3572 /* Subroutine of finish_struct_1. Recursively count the number of fields
3573 in TYPE, including anonymous union members. */
3576 count_fields (fields)
3581 for (x = fields; x; x = TREE_CHAIN (x))
3583 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3584 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3591 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3592 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3595 add_fields_to_vec (fields, field_vec, idx)
3596 tree fields, field_vec;
3600 for (x = fields; x; x = TREE_CHAIN (x))
3602 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3603 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3605 TREE_VEC_ELT (field_vec, idx++) = x;
3610 /* FIELD is a bit-field. We are finishing the processing for its
3611 enclosing type. Issue any appropriate messages and set appropriate
3615 check_bitfield_decl (field)
3618 tree type = TREE_TYPE (field);
3620 /* Invalid bit-field size done by grokfield. */
3621 /* Detect invalid bit-field type. Simply checking if TYPE is
3622 integral is insufficient, as that is the array core of the field
3623 type. If TREE_TYPE (field) is integral, then TYPE must be the same. */
3624 if (DECL_INITIAL (field)
3625 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3627 cp_error_at ("bit-field `%#D' with non-integral type", field);
3628 DECL_INITIAL (field) = NULL;
3631 /* Detect and ignore out of range field width. */
3632 if (DECL_INITIAL (field))
3634 tree w = DECL_INITIAL (field);
3636 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3639 /* detect invalid field size. */
3640 if (TREE_CODE (w) == CONST_DECL)
3641 w = DECL_INITIAL (w);
3642 else if (TREE_READONLY_DECL_P (w))
3643 w = decl_constant_value (w);
3645 if (TREE_CODE (w) != INTEGER_CST)
3647 cp_error_at ("bit-field `%D' width not an integer constant",
3649 DECL_INITIAL (field) = NULL_TREE;
3651 else if (tree_int_cst_sgn (w) < 0)
3653 DECL_INITIAL (field) = NULL;
3654 cp_error_at ("negative width in bit-field `%D'", field);
3656 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3658 DECL_INITIAL (field) = NULL;
3659 cp_error_at ("zero width for bit-field `%D'", field);
3661 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3662 && TREE_CODE (type) != ENUMERAL_TYPE
3663 && TREE_CODE (type) != BOOLEAN_TYPE)
3664 cp_warning_at ("width of `%D' exceeds its type", field);
3665 else if (TREE_CODE (type) == ENUMERAL_TYPE
3666 && (0 > compare_tree_int (w,
3667 min_precision (TYPE_MIN_VALUE (type),
3668 TREE_UNSIGNED (type)))
3669 || 0 > compare_tree_int (w,
3671 (TYPE_MAX_VALUE (type),
3672 TREE_UNSIGNED (type)))))
3673 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3676 if (DECL_INITIAL (field))
3678 DECL_INITIAL (field) = NULL_TREE;
3679 DECL_SIZE (field) = convert (bitsizetype, w);
3680 DECL_BIT_FIELD (field) = 1;
3682 if (integer_zerop (w))
3684 #ifdef EMPTY_FIELD_BOUNDARY
3685 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3686 EMPTY_FIELD_BOUNDARY);
3688 #ifdef PCC_BITFIELD_TYPE_MATTERS
3689 if (PCC_BITFIELD_TYPE_MATTERS)
3690 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3697 /* Non-bit-fields are aligned for their type. */
3698 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3701 /* FIELD is a non bit-field. We are finishing the processing for its
3702 enclosing type T. Issue any appropriate messages and set appropriate
3706 check_field_decl (field, t, cant_have_const_ctor,
3707 cant_have_default_ctor, no_const_asn_ref,
3708 any_default_members)
3711 int *cant_have_const_ctor;
3712 int *cant_have_default_ctor;
3713 int *no_const_asn_ref;
3714 int *any_default_members;
3716 tree type = strip_array_types (TREE_TYPE (field));
3718 /* An anonymous union cannot contain any fields which would change
3719 the settings of CANT_HAVE_CONST_CTOR and friends. */
3720 if (ANON_UNION_TYPE_P (type))
3722 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3723 structs. So, we recurse through their fields here. */
3724 else if (ANON_AGGR_TYPE_P (type))
3728 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3729 if (TREE_CODE (field) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3730 check_field_decl (fields, t, cant_have_const_ctor,
3731 cant_have_default_ctor, no_const_asn_ref,
3732 any_default_members);
3734 /* Check members with class type for constructors, destructors,
3736 else if (CLASS_TYPE_P (type))
3738 /* Never let anything with uninheritable virtuals
3739 make it through without complaint. */
3740 abstract_virtuals_error (field, type);
3742 if (TREE_CODE (t) == UNION_TYPE)
3744 if (TYPE_NEEDS_CONSTRUCTING (type))
3745 cp_error_at ("member `%#D' with constructor not allowed in union",
3747 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3748 cp_error_at ("member `%#D' with destructor not allowed in union",
3750 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3751 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3756 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3757 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3758 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3759 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3760 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3763 if (!TYPE_HAS_CONST_INIT_REF (type))
3764 *cant_have_const_ctor = 1;
3766 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3767 *no_const_asn_ref = 1;
3769 if (TYPE_HAS_CONSTRUCTOR (type)
3770 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3771 *cant_have_default_ctor = 1;
3773 if (DECL_INITIAL (field) != NULL_TREE)
3775 /* `build_class_init_list' does not recognize
3777 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3778 cp_error_at ("multiple fields in union `%T' initialized");
3779 *any_default_members = 1;
3782 /* Non-bit-fields are aligned for their type, except packed fields
3783 which require only BITS_PER_UNIT alignment. */
3784 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3785 (DECL_PACKED (field)
3787 : TYPE_ALIGN (TREE_TYPE (field))));
3790 /* Check the data members (both static and non-static), class-scoped
3791 typedefs, etc., appearing in the declaration of T. Issue
3792 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3793 declaration order) of access declarations; each TREE_VALUE in this
3794 list is a USING_DECL.
3796 In addition, set the following flags:
3799 The class is empty, i.e., contains no non-static data members.
3801 CANT_HAVE_DEFAULT_CTOR_P
3802 This class cannot have an implicitly generated default
3805 CANT_HAVE_CONST_CTOR_P
3806 This class cannot have an implicitly generated copy constructor
3807 taking a const reference.
3809 CANT_HAVE_CONST_ASN_REF
3810 This class cannot have an implicitly generated assignment
3811 operator taking a const reference.
3813 All of these flags should be initialized before calling this
3816 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3817 fields can be added by adding to this chain. */
3820 check_field_decls (t, access_decls, empty_p,
3821 cant_have_default_ctor_p, cant_have_const_ctor_p,
3826 int *cant_have_default_ctor_p;
3827 int *cant_have_const_ctor_p;
3828 int *no_const_asn_ref_p;
3833 int any_default_members;
3835 /* First, delete any duplicate fields. */
3836 delete_duplicate_fields (TYPE_FIELDS (t));
3838 /* Assume there are no access declarations. */
3839 *access_decls = NULL_TREE;
3840 /* Assume this class has no pointer members. */
3842 /* Assume none of the members of this class have default
3844 any_default_members = 0;
3846 for (field = &TYPE_FIELDS (t); *field; field = next)
3849 tree type = TREE_TYPE (x);
3851 GNU_xref_member (current_class_name, x);
3853 next = &TREE_CHAIN (x);
3855 if (TREE_CODE (x) == FIELD_DECL)
3857 DECL_PACKED (x) |= TYPE_PACKED (t);
3859 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3860 /* We don't treat zero-width bitfields as making a class
3865 /* The class is non-empty. */
3867 /* The class is not even nearly empty. */
3868 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3872 if (TREE_CODE (x) == USING_DECL)
3874 /* Prune the access declaration from the list of fields. */
3875 *field = TREE_CHAIN (x);
3877 /* Save the access declarations for our caller. */
3878 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3880 /* Since we've reset *FIELD there's no reason to skip to the
3886 if (TREE_CODE (x) == TYPE_DECL
3887 || TREE_CODE (x) == TEMPLATE_DECL)
3890 /* If we've gotten this far, it's a data member, possibly static,
3891 or an enumerator. */
3893 DECL_FIELD_CONTEXT (x) = t;
3895 /* ``A local class cannot have static data members.'' ARM 9.4 */
3896 if (current_function_decl && TREE_STATIC (x))
3897 cp_error_at ("field `%D' in local class cannot be static", x);
3899 /* Perform error checking that did not get done in
3901 if (TREE_CODE (type) == FUNCTION_TYPE)
3903 cp_error_at ("field `%D' invalidly declared function type",
3905 type = build_pointer_type (type);
3906 TREE_TYPE (x) = type;
3908 else if (TREE_CODE (type) == METHOD_TYPE)
3910 cp_error_at ("field `%D' invalidly declared method type", x);
3911 type = build_pointer_type (type);
3912 TREE_TYPE (x) = type;
3914 else if (TREE_CODE (type) == OFFSET_TYPE)
3916 cp_error_at ("field `%D' invalidly declared offset type", x);
3917 type = build_pointer_type (type);
3918 TREE_TYPE (x) = type;
3921 if (type == error_mark_node)
3924 DECL_SAVED_INSNS (x) = 0;
3926 /* When this goes into scope, it will be a non-local reference. */
3927 DECL_NONLOCAL (x) = 1;
3929 if (TREE_CODE (x) == CONST_DECL)
3932 if (TREE_CODE (x) == VAR_DECL)
3934 if (TREE_CODE (t) == UNION_TYPE)
3935 /* Unions cannot have static members. */
3936 cp_error_at ("field `%D' declared static in union", x);
3941 /* Now it can only be a FIELD_DECL. */
3943 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3944 CLASSTYPE_NON_AGGREGATE (t) = 1;
3946 /* If this is of reference type, check if it needs an init.
3947 Also do a little ANSI jig if necessary. */
3948 if (TREE_CODE (type) == REFERENCE_TYPE)
3950 CLASSTYPE_NON_POD_P (t) = 1;
3951 if (DECL_INITIAL (x) == NULL_TREE)
3952 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3954 /* ARM $12.6.2: [A member initializer list] (or, for an
3955 aggregate, initialization by a brace-enclosed list) is the
3956 only way to initialize nonstatic const and reference
3958 *cant_have_default_ctor_p = 1;
3959 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3961 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3964 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3966 cp_warning_at ("non-static reference in class without a constructor", x);
3970 type = strip_array_types (type);
3972 if (TREE_CODE (type) == POINTER_TYPE)
3975 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3976 CLASSTYPE_HAS_MUTABLE (t) = 1;
3978 if (! pod_type_p (type)
3979 /* For some reason, pointers to members are POD types themselves,
3980 but are not allowed in POD structs. Silly. */
3981 || TYPE_PTRMEM_P (type) || TYPE_PTRMEMFUNC_P (type))
3982 CLASSTYPE_NON_POD_P (t) = 1;
3984 /* If any field is const, the structure type is pseudo-const. */
3985 if (CP_TYPE_CONST_P (type))
3987 C_TYPE_FIELDS_READONLY (t) = 1;
3988 if (DECL_INITIAL (x) == NULL_TREE)
3989 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3991 /* ARM $12.6.2: [A member initializer list] (or, for an
3992 aggregate, initialization by a brace-enclosed list) is the
3993 only way to initialize nonstatic const and reference
3995 *cant_have_default_ctor_p = 1;
3996 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3998 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
4001 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
4003 cp_warning_at ("non-static const member in class without a constructor", x);
4006 /* A field that is pseudo-const makes the structure likewise. */
4007 else if (IS_AGGR_TYPE (type))
4009 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
4010 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
4011 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
4014 /* We set DECL_C_BIT_FIELD in grokbitfield.
4015 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
4016 if (DECL_C_BIT_FIELD (x))
4017 check_bitfield_decl (x);
4019 check_field_decl (x, t,
4020 cant_have_const_ctor_p,
4021 cant_have_default_ctor_p,
4023 &any_default_members);
4026 /* Effective C++ rule 11. */
4027 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
4028 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
4030 cp_warning ("`%#T' has pointer data members", t);
4032 if (! TYPE_HAS_INIT_REF (t))
4034 cp_warning (" but does not override `%T(const %T&)'", t, t);
4035 if (! TYPE_HAS_ASSIGN_REF (t))
4036 cp_warning (" or `operator=(const %T&)'", t);
4038 else if (! TYPE_HAS_ASSIGN_REF (t))
4039 cp_warning (" but does not override `operator=(const %T&)'", t);
4043 /* Check anonymous struct/anonymous union fields. */
4044 finish_struct_anon (t);
4046 /* We've built up the list of access declarations in reverse order.
4048 *access_decls = nreverse (*access_decls);
4051 /* Return a FIELD_DECL for a pointer-to-virtual-table or
4052 pointer-to-virtual-base. The NAME, ASSEMBLER_NAME, and TYPE of the
4053 field are as indicated. The CLASS_TYPE in which this field occurs
4054 is also indicated. FCONTEXT is the type that is needed for the debug
4055 info output routines. *EMPTY_P is set to a non-zero value by this
4056 function to indicate that a class containing this field is
4060 build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
4063 tree assembler_name;
4071 /* This class is non-empty. */
4074 /* Build the FIELD_DECL. */
4075 field = build_lang_decl (FIELD_DECL, name, type);
4076 DECL_ASSEMBLER_NAME (field) = assembler_name;
4077 DECL_VIRTUAL_P (field) = 1;
4078 DECL_ARTIFICIAL (field) = 1;
4079 DECL_FIELD_CONTEXT (field) = class_type;
4080 DECL_FCONTEXT (field) = fcontext;
4081 DECL_SAVED_INSNS (field) = 0;
4082 DECL_ALIGN (field) = TYPE_ALIGN (type);
4088 /* Return the BINFO_OFFSET for BINFO as a native integer, not an
4091 static unsigned HOST_WIDE_INT
4092 get_binfo_offset_as_int (binfo)
4097 offset = BINFO_OFFSET (binfo);
4098 my_friendly_assert (TREE_CODE (offset) == INTEGER_CST, 20000313);
4099 my_friendly_assert (TREE_INT_CST_HIGH (offset) == 0, 20000313);
4101 return (unsigned HOST_WIDE_INT) TREE_INT_CST_LOW (offset);
4104 /* Record the type of BINFO in the slot in DATA (which is really a
4105 `varray_type *') corresponding to the BINFO_OFFSET. */
4108 dfs_record_base_offsets (binfo, data)
4113 unsigned HOST_WIDE_INT offset = get_binfo_offset_as_int (binfo);
4115 v = (varray_type *) data;
4116 while (VARRAY_SIZE (*v) <= offset)
4117 VARRAY_GROW (*v, 2 * VARRAY_SIZE (*v));
4118 VARRAY_TREE (*v, offset) = tree_cons (NULL_TREE,
4120 VARRAY_TREE (*v, offset));
4125 /* Returns non-NULL if there is already an entry in DATA (which is
4126 really a `varray_type') indicating that an object with the same
4127 type of BINFO is already at the BINFO_OFFSET for BINFO. */
4130 dfs_search_base_offsets (binfo, data)
4134 if (is_empty_class (BINFO_TYPE (binfo)))
4136 varray_type v = (varray_type) data;
4137 unsigned HOST_WIDE_INT offset;
4140 /* Find the offset for this BINFO. */
4141 offset = get_binfo_offset_as_int (binfo);
4142 /* If we haven't yet encountered any objects at offsets that
4143 big, then there's no conflict. */
4144 if (VARRAY_SIZE (v) <= offset)
4146 /* Otherwise, go through the objects already allocated at this
4148 for (t = VARRAY_TREE (v, offset); t; t = TREE_CHAIN (t))
4149 if (same_type_p (TREE_VALUE (t), BINFO_TYPE (binfo)))
4156 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
4157 non-static data member of the type indicated by RLI. BINFO is the
4158 binfo corresponding to the base subobject, or, if this is a
4159 non-static data-member, a dummy BINFO for the type of the data
4160 member. BINFO may be NULL if checks to see if the field overlaps
4161 an existing field with the same type are not required. V maps
4162 offsets to types already located at those offsets. This function
4163 determines the position of the DECL. */
4166 layout_nonempty_base_or_field (rli, decl, binfo, v)
4167 record_layout_info rli;
4172 /* Try to place the field. It may take more than one try if we have
4173 a hard time placing the field without putting two objects of the
4174 same type at the same address. */
4179 /* Layout this field. */
4180 layout_field (rli, decl);
4182 /* Now that we know where it wil be placed, update its
4184 offset = size_int (CEIL (TREE_INT_CST_LOW (DECL_FIELD_BITPOS (decl)),
4187 propagate_binfo_offsets (binfo, offset);
4189 /* We have to check to see whether or not there is already
4190 something of the same type at the offset we're about to use.
4194 struct T : public S { int i; };
4195 struct U : public S, public T {};
4197 Here, we put S at offset zero in U. Then, we can't put T at
4198 offset zero -- its S component would be at the same address
4199 as the S we already allocated. So, we have to skip ahead.
4200 Since all data members, including those whose type is an
4201 empty class, have non-zero size, any overlap can happen only
4202 with a direct or indirect base-class -- it can't happen with
4204 if (binfo && flag_new_abi && dfs_walk (binfo,
4205 dfs_search_base_offsets,
4209 /* Undo the propogate_binfo_offsets call. */
4210 offset = convert (sizetype,
4211 size_diffop (size_zero_node, offset));
4212 propagate_binfo_offsets (binfo, offset);
4214 /* Strip off the size allocated to this field. That puts us
4215 at the first place we could have put the field with
4216 proper alignment. */
4217 rli->const_size -= TREE_INT_CST_LOW (DECL_SIZE (decl));
4218 /* Bump up by th alignment required for the type, without
4219 virtual base classes. */
4220 rli->const_size += CLASSTYPE_ALIGN (BINFO_TYPE (binfo));
4223 /* There was no conflict. We're done laying out this field. */
4228 /* Build a FIELD_DECL for the base given by BINFO in the class
4229 *indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
4230 *BASE_ALIGN is a running maximum of the alignments of any base
4234 build_base_field (rli, binfo, empty_p, base_align, v)
4235 record_layout_info rli;
4238 unsigned int *base_align;
4241 tree basetype = BINFO_TYPE (binfo);
4244 if (TYPE_SIZE (basetype) == 0)
4245 /* This error is now reported in xref_tag, thus giving better
4246 location information. */
4249 decl = build_lang_decl (FIELD_DECL, NULL_TREE, basetype);
4250 DECL_ARTIFICIAL (decl) = 1;
4251 DECL_FIELD_CONTEXT (decl) = rli->t;
4252 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
4253 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
4254 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
4258 /* Brain damage for backwards compatibility. For no good
4259 reason, the old layout_basetypes made every base at least
4260 as large as the alignment for the bases up to that point,
4261 gratuitously wasting space. So we do the same thing
4263 *base_align = MAX (*base_align, DECL_ALIGN (decl));
4265 = size_binop (MAX_EXPR, DECL_SIZE (decl), bitsize_int (*base_align));
4266 DECL_SIZE_UNIT (decl)
4267 = size_binop (MAX_EXPR, DECL_SIZE_UNIT (decl),
4268 size_int (*base_align / BITS_PER_UNIT));
4271 if (!integer_zerop (DECL_SIZE (decl)))
4273 /* The containing class is non-empty because it has a non-empty
4277 /* Try to place the field. It may take more than one try if we
4278 have a hard time placing the field without putting two
4279 objects of the same type at the same address. */
4280 layout_nonempty_base_or_field (rli, decl, binfo, *v);
4284 /* This code assumes that zero-sized classes have one-byte
4285 alignment. There might someday be a system where that's not
4287 my_friendly_assert (TYPE_ALIGN (basetype) == BITS_PER_UNIT,
4290 /* This is an empty base class. We first try to put it at
4292 if (dfs_walk (binfo, dfs_search_base_offsets, dfs_skip_vbases, *v))
4294 /* That didn't work. Now, we move forward from the next
4295 available spot in the class. */
4296 propagate_binfo_offsets (binfo, size_int (rli->const_size));
4299 if (!dfs_walk (binfo, dfs_search_base_offsets,
4300 dfs_skip_vbases, *v))
4301 /* We finally found a spot where there's no overlap. */
4304 /* There's overlap here, too. Bump along to the next
4306 propagate_binfo_offsets (binfo, size_one_node);
4311 /* Check for inaccessible base classes. If the same base class
4312 appears more than once in the hierarchy, but isn't virtual, then
4314 if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
4315 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4318 /* Record the offsets of BINFO and its base subobjects. */
4320 dfs_record_base_offsets,
4325 /* Layout all of the non-virtual base classes. Returns a map from
4326 offsets to types present at those offsets. */
4329 build_base_fields (rli, empty_p)
4330 record_layout_info rli;
4333 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4336 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
4339 unsigned int base_align = 0;
4341 /* Create the table mapping offsets to empty base classes. */
4342 VARRAY_TREE_INIT (v, 32, "v");
4344 /* Under the new ABI, the primary base class is always allocated
4346 if (flag_new_abi && CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
4347 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
4348 empty_p, &base_align, &v);
4350 /* Now allocate the rest of the bases. */
4351 for (i = 0; i < n_baseclasses; ++i)
4355 /* Under the new ABI, the primary base was already allocated
4356 above, so we don't need to allocate it again here. */
4357 if (flag_new_abi && i == CLASSTYPE_VFIELD_PARENT (rec))
4360 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
4362 /* A primary virtual base class is allocated just like any other
4363 base class, but a non-primary virtual base is allocated
4364 later, in layout_basetypes. */
4365 if (TREE_VIA_VIRTUAL (base_binfo)
4366 && !BINFO_PRIMARY_MARKED_P (base_binfo))
4369 build_base_field (rli, base_binfo, empty_p, &base_align, &v);
4375 /* Go through the TYPE_METHODS of T issuing any appropriate
4376 diagnostics, figuring out which methods override which other
4377 methods, and so forth. */
4384 int seen_one_arg_array_delete_p = 0;
4386 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4388 GNU_xref_member (current_class_name, x);
4390 /* If this was an evil function, don't keep it in class. */
4391 if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4394 DECL_SAVED_INSNS (x) = 0;
4395 check_for_override (x, t);
4396 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4397 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4399 /* The name of the field is the original field name
4400 Save this in auxiliary field for later overloading. */
4401 if (DECL_VINDEX (x))
4403 TYPE_POLYMORPHIC_P (t) = 1;
4404 if (DECL_PURE_VIRTUAL_P (x))
4405 CLASSTYPE_PURE_VIRTUALS (t)
4406 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4409 if (DECL_ARRAY_DELETE_OPERATOR_P (x))
4413 /* When dynamically allocating an array of this type, we
4414 need a "cookie" to record how many elements we allocated,
4415 even if the array elements have no non-trivial
4416 destructor, if the usual array deallocation function
4417 takes a second argument of type size_t. The standard (in
4418 [class.free]) requires that the second argument be set
4420 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
4421 /* This is overly conservative, but we must maintain this
4422 behavior for backwards compatibility. */
4423 if (!flag_new_abi && second_parm != void_list_node)
4424 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4425 /* Under the new ABI, we choose only those function that are
4426 explicitly declared as `operator delete[] (void *,
4428 else if (flag_new_abi
4429 && !seen_one_arg_array_delete_p
4431 && TREE_CHAIN (second_parm) == void_list_node
4432 && same_type_p (TREE_VALUE (second_parm), sizetype))
4433 TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
4434 /* If there's no second parameter, then this is the usual
4435 deallocation function. */
4436 else if (second_parm == void_list_node)
4437 seen_one_arg_array_delete_p = 1;
4442 /* Remove all zero-width bit-fields from T. */
4445 remove_zero_width_bit_fields (t)
4450 fieldsp = &TYPE_FIELDS (t);
4453 if (TREE_CODE (*fieldsp) == FIELD_DECL
4454 && DECL_C_BIT_FIELD (*fieldsp)
4455 && DECL_INITIAL (*fieldsp))
4456 *fieldsp = TREE_CHAIN (*fieldsp);
4458 fieldsp = &TREE_CHAIN (*fieldsp);
4462 /* Check the validity of the bases and members declared in T. Add any
4463 implicitly-generated functions (like copy-constructors and
4464 assignment operators). Compute various flag bits (like
4465 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4466 level: i.e., independently of the ABI in use. */
4469 check_bases_and_members (t, empty_p)
4473 /* Nonzero if we are not allowed to generate a default constructor
4475 int cant_have_default_ctor;
4476 /* Nonzero if the implicitly generated copy constructor should take
4477 a non-const reference argument. */
4478 int cant_have_const_ctor;
4479 /* Nonzero if the the implicitly generated assignment operator
4480 should take a non-const reference argument. */
4481 int no_const_asn_ref;
4484 /* By default, we use const reference arguments and generate default
4486 cant_have_default_ctor = 0;
4487 cant_have_const_ctor = 0;
4488 no_const_asn_ref = 0;
4490 /* Assume that the class is nearly empty; we'll clear this flag if
4491 it turns out not to be nearly empty. */
4492 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4494 /* Check all the base-classes. */
4495 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4498 /* Check all the data member declarations. */
4499 check_field_decls (t, &access_decls, empty_p,
4500 &cant_have_default_ctor,
4501 &cant_have_const_ctor,
4504 /* Check all the method declarations. */
4507 /* A nearly-empty class has to be vptr-containing; a nearly empty
4508 class contains just a vptr. */
4509 if (!TYPE_CONTAINS_VPTR_P (t))
4510 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4512 /* Do some bookkeeping that will guide the generation of implicitly
4513 declared member functions. */
4514 TYPE_HAS_COMPLEX_INIT_REF (t)
4515 |= (TYPE_HAS_INIT_REF (t)
4516 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4517 || TYPE_POLYMORPHIC_P (t));
4518 TYPE_NEEDS_CONSTRUCTING (t)
4519 |= (TYPE_HAS_CONSTRUCTOR (t)
4520 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4521 || TYPE_POLYMORPHIC_P (t));
4522 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4523 || TYPE_POLYMORPHIC_P (t));
4524 CLASSTYPE_NON_POD_P (t)
4525 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4526 || TYPE_HAS_ASSIGN_REF (t));
4527 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4528 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4529 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
4531 /* Synthesize any needed methods. Note that methods will be synthesized
4532 for anonymous unions; grok_x_components undoes that. */
4533 add_implicitly_declared_members (t, cant_have_default_ctor,
4534 cant_have_const_ctor,
4537 /* Process the using-declarations. */
4538 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4539 handle_using_decl (TREE_VALUE (access_decls), t);
4541 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4542 finish_struct_methods (t);
4545 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4546 accordingly. If a new vfield was created (because T doesn't have a
4547 primary base class), then the newly created field is returned. It
4548 is not added to the TYPE_FIELDS list; it is the callers
4549 responsibility to do that. */
4552 create_vtable_ptr (t, empty_p, has_virtual_p,
4553 new_virtuals_p, overridden_virtuals_p)
4557 tree *new_virtuals_p;
4558 tree *overridden_virtuals_p;
4562 /* Loop over the virtual functions, adding them to our various
4564 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4565 if (DECL_VINDEX (fn))
4566 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4567 has_virtual_p, fn, t);
4569 /* Even if there weren't any new virtual functions, we might need a
4570 new virtual function table if we're supposed to include vptrs in
4571 all classes that need them. */
4572 if (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())
4573 start_vtable (t, has_virtual_p);
4575 /* If we couldn't find an appropriate base class, create a new field
4577 if (*has_virtual_p && !TYPE_VFIELD (t))
4579 /* We build this decl with vtbl_ptr_type_node, which is a
4580 `vtable_entry_type*'. It might seem more precise to use
4581 `vtable_entry_type (*)[N]' where N is the number of firtual
4582 functions. However, that would require the vtable pointer in
4583 base classes to have a different type than the vtable pointer
4584 in derived classes. We could make that happen, but that
4585 still wouldn't solve all the problems. In particular, the
4586 type-based alias analysis code would decide that assignments
4587 to the base class vtable pointer can't alias assignments to
4588 the derived class vtable pointer, since they have different
4589 types. Thus, in an derived class destructor, where the base
4590 class constructor was inlined, we could generate bad code for
4591 setting up the vtable pointer.
4593 Therefore, we use one type for all vtable pointers. We still
4594 use a type-correct type; it's just doesn't indicate the array
4595 bounds. That's better than using `void*' or some such; it's
4596 cleaner, and it let's the alias analysis code know that these
4597 stores cannot alias stores to void*! */
4599 = build_vtbl_or_vbase_field (get_vfield_name (t),
4600 get_identifier (VFIELD_BASE),
4606 if (flag_new_abi && CLASSTYPE_N_BASECLASSES (t))
4607 /* If there were any baseclasses, they can't possibly be at
4608 offset zero any more, because that's where the vtable
4609 pointer is. So, converting to a base class is going to
4611 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4613 return TYPE_VFIELD (t);
4619 /* Fixup the inline function given by INFO now that the class is
4623 fixup_pending_inline (info)
4624 struct pending_inline *info;
4629 tree fn = info->fndecl;
4631 args = DECL_ARGUMENTS (fn);
4634 DECL_CONTEXT (args) = fn;
4635 args = TREE_CHAIN (args);
4640 /* Fixup the inline methods and friends in TYPE now that TYPE is
4644 fixup_inline_methods (type)
4647 tree method = TYPE_METHODS (type);
4649 if (method && TREE_CODE (method) == TREE_VEC)
4651 if (TREE_VEC_ELT (method, 1))
4652 method = TREE_VEC_ELT (method, 1);
4653 else if (TREE_VEC_ELT (method, 0))
4654 method = TREE_VEC_ELT (method, 0);
4656 method = TREE_VEC_ELT (method, 2);
4659 /* Do inline member functions. */
4660 for (; method; method = TREE_CHAIN (method))
4661 fixup_pending_inline (DECL_PENDING_INLINE_INFO (method));
4664 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4666 method = TREE_CHAIN (method))
4667 fixup_pending_inline (DECL_PENDING_INLINE_INFO (TREE_VALUE (method)));
4668 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4671 /* Called from propagate_binfo_offsets via dfs_walk. */
4674 dfs_propagate_binfo_offsets (binfo, data)
4678 tree offset = (tree) data;
4680 /* Update the BINFO_OFFSET for this base. */
4681 BINFO_OFFSET (binfo) = size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), offset);
4683 SET_BINFO_MARKED (binfo);
4688 /* Add OFFSET to all base types of BINFO which is a base in the
4689 hierarchy dominated by T.
4691 OFFSET, which is a type offset, is number of bytes.
4693 Note that we don't have to worry about having two paths to the
4694 same base type, since this type owns its association list. */
4697 propagate_binfo_offsets (binfo, offset)
4702 dfs_propagate_binfo_offsets,
4703 dfs_skip_nonprimary_vbases_unmarkedp,
4707 dfs_skip_nonprimary_vbases_markedp,
4711 /* Called via dfs_walk from layout_virtual bases. */
4714 dfs_set_offset_for_shared_vbases (binfo, data)
4718 if (TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_MARKED_P (binfo))
4720 /* Update the shared copy. */
4723 shared_binfo = BINFO_FOR_VBASE (BINFO_TYPE (binfo), (tree) data);
4724 BINFO_OFFSET (shared_binfo) = BINFO_OFFSET (binfo);
4730 /* Called via dfs_walk from layout_virtual bases. */
4733 dfs_set_offset_for_unshared_vbases (binfo, data)
4737 /* If this is a virtual base, make sure it has the same offset as
4738 the shared copy. If it's a primary base, then we know it's
4740 if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_MARKED_P (binfo))
4742 tree t = (tree) data;
4746 vbase = BINFO_FOR_VBASE (BINFO_TYPE (binfo), t);
4747 offset = size_binop (MINUS_EXPR,
4748 BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4749 propagate_binfo_offsets (binfo, offset);
4755 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4756 TYPE_ALIGN and TYPE_SIZE for T. */
4759 layout_virtual_bases (t)
4763 unsigned HOST_WIDE_INT dsize;
4765 /* DSIZE is the size of the class without the virtual bases. */
4766 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4768 /* Make every class have alignment of at least one. */
4769 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4771 /* Go through the virtual bases, allocating space for each virtual
4772 base that is not already a primary base class. */
4773 for (vbase = CLASSTYPE_VBASECLASSES (t);
4775 vbase = TREE_CHAIN (vbase))
4776 if (!BINFO_VBASE_PRIMARY_P (vbase))
4778 /* This virtual base is not a primary base of any class in the
4779 hierarchy, so we have to add space for it. */
4781 unsigned int desired_align;
4783 basetype = BINFO_TYPE (vbase);
4784 desired_align = TYPE_ALIGN (basetype);
4785 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4787 /* Add padding so that we can put the virtual base class at an
4788 appropriately aligned offset. */
4789 dsize = CEIL (dsize, desired_align) * desired_align;
4790 /* And compute the offset of the virtual base. */
4791 propagate_binfo_offsets (vbase,
4792 size_int (CEIL (dsize, BITS_PER_UNIT)));
4793 /* Every virtual baseclass takes a least a UNIT, so that we can
4794 take it's address and get something different for each base. */
4795 dsize += MAX (BITS_PER_UNIT,
4796 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4799 /* Make sure that all of the CLASSTYPE_VBASECLASSES have their
4800 BINFO_OFFSET set correctly. Those we just allocated certainly
4801 will. The others are primary baseclasses; we walk the hierarchy
4802 to find the primary copies and update the shared copy. */
4803 dfs_walk (TYPE_BINFO (t),
4804 dfs_set_offset_for_shared_vbases,
4805 dfs_unmarked_real_bases_queue_p,
4808 /* Now, go through the TYPE_BINFO hierarchy again, setting the
4809 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4810 bases and their direct and indirect bases. The ambiguity checks
4811 in get_base_distance depend on the BINFO_OFFSETs being set
4813 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4814 for (vbase = CLASSTYPE_VBASECLASSES (t);
4816 vbase = TREE_CHAIN (vbase))
4817 dfs_walk (vbase, dfs_set_offset_for_unshared_vbases, NULL, t);
4819 /* Now, make sure that the total size of the type is a multiple of
4821 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4822 TYPE_SIZE (t) = bitsize_int (dsize);
4823 TYPE_SIZE_UNIT (t) = convert (sizetype,
4824 size_binop (FLOOR_DIV_EXPR, TYPE_SIZE (t),
4825 bitsize_int (BITS_PER_UNIT)));
4828 /* Finish the work of layout_record, now taking virtual bases into account.
4829 Also compute the actual offsets that our base classes will have.
4830 This must be performed after the fields are laid out, since virtual
4831 baseclasses must lay down at the end of the record. */
4834 layout_basetypes (rec)
4839 if (CLASSTYPE_N_BASECLASSES (rec) == 0)
4842 #ifdef STRUCTURE_SIZE_BOUNDARY
4843 /* Packed structures don't need to have minimum size. */
4844 if (! TYPE_PACKED (rec))
4845 TYPE_ALIGN (rec) = MAX (TYPE_ALIGN (rec), STRUCTURE_SIZE_BOUNDARY);
4848 /* Allocate the virtual base classes. */
4849 layout_virtual_bases (rec);
4851 /* Get all the virtual base types that this type uses. The
4852 TREE_VALUE slot holds the virtual baseclass type. Note that
4853 get_vbase_types makes copies of the virtual base BINFOs, so that
4854 the vbase_types are unshared. */
4855 for (vbase_types = CLASSTYPE_VBASECLASSES (rec); vbase_types;
4856 vbase_types = TREE_CHAIN (vbase_types))
4859 tree basetype = BINFO_TYPE (vbase_types);
4860 if (get_base_distance (basetype, rec, 0, (tree*)0) == -2)
4861 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4866 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4867 BINFO_OFFSETs for all of the base-classes. Position the vtable
4871 layout_class_type (t, empty_p, has_virtual_p,
4872 new_virtuals_p, overridden_virtuals_p)
4876 tree *new_virtuals_p;
4877 tree *overridden_virtuals_p;
4879 tree non_static_data_members;
4882 record_layout_info rli;
4886 /* Keep track of the first non-static data member. */
4887 non_static_data_members = TYPE_FIELDS (t);
4889 /* Initialize the layout information. */
4890 rli = new_record_layout_info (t);
4892 /* If possible, we reuse the virtual function table pointer from one
4893 of our base classes. */
4894 determine_primary_base (t, has_virtual_p);
4896 /* Create a pointer to our virtual function table. */
4897 vptr = create_vtable_ptr (t, empty_p, has_virtual_p,
4898 new_virtuals_p, overridden_virtuals_p);
4900 /* Under the new ABI, the vptr is always the first thing in the
4902 if (flag_new_abi && vptr)
4904 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4905 layout_field (rli, vptr);
4908 /* Add pointers to all of our virtual base-classes. */
4909 TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
4911 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4912 v = build_base_fields (rli, empty_p);
4914 /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS. Thus,
4915 we have to save this before we start modifying
4916 TYPE_NONCOPIED_PARTS. */
4917 fixup_inline_methods (t);
4919 /* Layout the non-static data members. */
4920 for (field = non_static_data_members;
4922 field = TREE_CHAIN (field))
4928 /* We still pass things that aren't non-static data members to
4929 the back-end, in case it wants to do something with them. */
4930 if (TREE_CODE (field) != FIELD_DECL)
4932 layout_field (rli, field);
4936 type = TREE_TYPE (field);
4938 /* If this field is a bit-field whose width is greater than its
4939 type, then there are some special rules for allocating it
4940 under the new ABI. Under the old ABI, there were no special
4941 rules, but the back-end can't handle bitfields longer than a
4942 `long long', so we use the same mechanism. */
4943 if (DECL_C_BIT_FIELD (field)
4945 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4947 && compare_tree_int (DECL_SIZE (field),
4949 (long_long_unsigned_type_node)) > 0)))
4951 integer_type_kind itk;
4954 /* We must allocate the bits as if suitably aligned for the
4955 longest integer type that fits in this many bits. type
4956 of the field. Then, we are supposed to use the left over
4957 bits as additional padding. */
4958 for (itk = itk_char; itk != itk_none; ++itk)
4959 if (INT_CST_LT (DECL_SIZE (field),
4960 TYPE_SIZE (integer_types[itk])))
4963 /* ITK now indicates a type that is too large for the
4964 field. We have to back up by one to find the largest
4966 integer_type = integer_types[itk - 1];
4967 padding = size_diffop (DECL_SIZE (field),
4968 TYPE_SIZE (integer_type));
4969 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4970 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4973 padding = NULL_TREE;
4975 /* Create a dummy BINFO corresponding to this field. */
4976 binfo = make_binfo (size_zero_node, type, NULL_TREE, NULL_TREE);
4977 unshare_base_binfos (binfo);
4978 layout_nonempty_base_or_field (rli, field, binfo, v);
4980 /* If we needed additional padding after this field, add it
4986 padding_field = build_decl (FIELD_DECL,
4989 DECL_BIT_FIELD (padding_field) = 1;
4990 DECL_SIZE (padding_field) = padding;
4991 DECL_ALIGN (padding_field) = 1;
4992 layout_nonempty_base_or_field (rli, padding_field, NULL_TREE, v);
4999 /* It might be the case that we grew the class to allocate a
5000 zero-sized base class. That won't be reflected in RLI, yet,
5001 because we are willing to overlay multiple bases at the same
5002 offset. However, now we need to make sure that RLI is big enough
5003 to reflect the entire class. */
5004 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
5007 unsigned HOST_WIDE_INT offset;
5009 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
5010 offset = get_binfo_offset_as_int (base_binfo);
5011 if (offset * BITS_PER_UNIT > rli->const_size)
5012 rli->const_size = (offset + 1) * BITS_PER_UNIT;
5015 /* We make all structures have at least one element, so that they
5016 have non-zero size. In the new ABI, the class may be empty even
5017 if it has basetypes. Therefore, we add the fake field after all
5018 the other fields; if there are already FIELD_DECLs on the list,
5019 their offsets will not be disturbed. */
5024 padding = build_lang_decl (FIELD_DECL, NULL_TREE, char_type_node);
5025 layout_field (rli, padding);
5026 TYPE_NONCOPIED_PARTS (t)
5027 = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
5028 TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
5031 /* Under the old ABI, the vptr comes at the very end of the
5033 if (!flag_new_abi && vptr)
5035 layout_field (rli, vptr);
5036 TYPE_FIELDS (t) = chainon (TYPE_FIELDS (t), vptr);
5039 /* Let the back-end lay out the type. Note that at this point we
5040 have only included non-virtual base-classes; we will lay out the
5041 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5042 this call are not necessarily correct; they are just the size and
5043 alignment when no virtual base clases are used. */
5044 finish_record_layout (rli);
5046 /* Delete all zero-width bit-fields from the list of fields. Now
5047 that the type is laid out they are no longer important. */
5048 remove_zero_width_bit_fields (t);
5050 /* Remember the size and alignment of the class before adding
5051 the virtual bases. */
5052 if (*empty_p && flag_new_abi)
5054 CLASSTYPE_SIZE (t) = bitsize_int (0);
5055 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5057 else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
5058 && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
5060 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5061 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5065 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
5066 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
5069 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5071 /* Set the TYPE_DECL for this type to contain the right
5072 value for DECL_OFFSET, so that we can use it as part
5073 of a COMPONENT_REF for multiple inheritance. */
5074 layout_decl (TYPE_MAIN_DECL (t), 0);
5076 /* Now fix up any virtual base class types that we left lying
5077 around. We must get these done before we try to lay out the
5078 virtual function table. As a side-effect, this will remove the
5079 base subobject fields. */
5080 layout_basetypes (t);
5083 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5084 (or C++ class declaration).
5086 For C++, we must handle the building of derived classes.
5087 Also, C++ allows static class members. The way that this is
5088 handled is to keep the field name where it is (as the DECL_NAME
5089 of the field), and place the overloaded decl in the bit position
5090 of the field. layout_record and layout_union will know about this.
5092 More C++ hair: inline functions have text in their
5093 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5094 meaningful tree structure. After the struct has been laid out, set
5095 things up so that this can happen.
5097 And still more: virtual functions. In the case of single inheritance,
5098 when a new virtual function is seen which redefines a virtual function
5099 from the base class, the new virtual function is placed into
5100 the virtual function table at exactly the same address that
5101 it had in the base class. When this is extended to multiple
5102 inheritance, the same thing happens, except that multiple virtual
5103 function tables must be maintained. The first virtual function
5104 table is treated in exactly the same way as in the case of single
5105 inheritance. Additional virtual function tables have different
5106 DELTAs, which tell how to adjust `this' to point to the right thing.
5108 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5116 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5117 a FUNCTION_DECL. Each of these functions is a virtual function
5118 declared in T that does not override any virtual function from a
5120 tree new_virtuals = NULL_TREE;
5121 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5122 except that each declaration here overrides the declaration from
5124 tree overridden_virtuals = NULL_TREE;
5131 if (IS_AGGR_TYPE (t))
5132 cp_error ("redefinition of `%#T'", t);
5134 my_friendly_abort (172);
5139 GNU_xref_decl (current_function_decl, t);
5141 /* If this type was previously laid out as a forward reference,
5142 make sure we lay it out again. */
5143 TYPE_SIZE (t) = NULL_TREE;
5144 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5145 CLASSTYPE_VFIELD_PARENT (t) = -1;
5147 CLASSTYPE_RTTI (t) = NULL_TREE;
5149 /* Do end-of-class semantic processing: checking the validity of the
5150 bases and members and add implicitly generated methods. */
5151 check_bases_and_members (t, &empty);
5153 /* Layout the class itself. */
5154 layout_class_type (t, &empty, &has_virtual,
5155 &new_virtuals, &overridden_virtuals);
5157 /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
5158 might need to know it for setting up the offsets in the vtable
5159 (or in thunks) below. */
5160 vfield = TYPE_VFIELD (t);
5161 if (vfield != NULL_TREE
5162 && DECL_FIELD_CONTEXT (vfield) != t)
5164 tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
5165 tree offset = convert (bitsizetype, BINFO_OFFSET (binfo));
5167 vfield = copy_node (vfield);
5168 copy_lang_decl (vfield);
5170 if (! integer_zerop (offset))
5171 offset = size_binop (MULT_EXPR, offset, bitsize_int (BITS_PER_UNIT));
5173 DECL_FIELD_CONTEXT (vfield) = t;
5174 DECL_FIELD_BITPOS (vfield)
5175 = size_binop (PLUS_EXPR, offset, bit_position (vfield));
5176 TYPE_VFIELD (t) = vfield;
5180 = modify_all_vtables (t, &has_virtual, nreverse (overridden_virtuals));
5182 /* If necessary, create the primary vtable for this class. */
5184 || overridden_virtuals
5185 || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
5187 new_virtuals = nreverse (new_virtuals);
5188 /* We must enter these virtuals into the table. */
5189 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5191 if (! CLASSTYPE_COM_INTERFACE (t))
5193 /* The second slot is for the tdesc pointer when thunks
5195 if (flag_vtable_thunks)
5196 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5198 /* The first slot is for the rtti offset. */
5199 new_virtuals = tree_cons (NULL_TREE, NULL_TREE, new_virtuals);
5201 set_rtti_entry (new_virtuals,
5202 convert (ssizetype, integer_zero_node), t);
5204 build_primary_vtable (NULL_TREE, t);
5206 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5207 /* Here we know enough to change the type of our virtual
5208 function table, but we will wait until later this function. */
5209 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5211 /* If this type has basetypes with constructors, then those
5212 constructors might clobber the virtual function table. But
5213 they don't if the derived class shares the exact vtable of the base
5216 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5218 /* If we didn't need a new vtable, see if we should copy one from
5220 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5222 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5224 /* This class contributes nothing new to the virtual function
5225 table. However, it may have declared functions which
5226 went into the virtual function table "inherited" from the
5227 base class. If so, we grab a copy of those updated functions,
5228 and pretend they are ours. */
5230 /* See if we should steal the virtual info from base class. */
5231 if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
5232 TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
5233 if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
5234 TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
5235 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5236 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5239 if (TYPE_CONTAINS_VPTR_P (t))
5241 if (TYPE_BINFO_VTABLE (t))
5242 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5244 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5245 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5248 CLASSTYPE_VSIZE (t) = has_virtual;
5249 /* Entries for virtual functions defined in the primary base are
5250 followed by entries for new functions unique to this class. */
5251 TYPE_BINFO_VIRTUALS (t)
5252 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5253 /* Finally, add entries for functions that override virtuals
5254 from non-primary bases. */
5255 TYPE_BINFO_VIRTUALS (t)
5256 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5259 /* If we created a new vtbl pointer for this class, add it to the
5261 if (TYPE_VFIELD (t) && CLASSTYPE_VFIELD_PARENT (t) == -1)
5262 CLASSTYPE_VFIELDS (t)
5263 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5265 finish_struct_bits (t);
5267 /* Complete the rtl for any static member objects of the type we're
5269 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5271 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5272 && TREE_TYPE (x) == t)
5274 DECL_MODE (x) = TYPE_MODE (t);
5275 make_decl_rtl (x, NULL, 0);
5279 /* Done with FIELDS...now decide whether to sort these for
5280 faster lookups later.
5282 The C front-end only does this when n_fields > 15. We use
5283 a smaller number because most searches fail (succeeding
5284 ultimately as the search bores through the inheritance
5285 hierarchy), and we want this failure to occur quickly. */
5287 n_fields = count_fields (TYPE_FIELDS (t));
5290 tree field_vec = make_tree_vec (n_fields);
5291 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5292 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5293 (int (*)(const void *, const void *))field_decl_cmp);
5294 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5295 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5296 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5299 if (TYPE_HAS_CONSTRUCTOR (t))
5301 tree vfields = CLASSTYPE_VFIELDS (t);
5305 /* Mark the fact that constructor for T
5306 could affect anybody inheriting from T
5307 who wants to initialize vtables for VFIELDS's type. */
5308 if (VF_DERIVED_VALUE (vfields))
5309 TREE_ADDRESSABLE (vfields) = 1;
5310 vfields = TREE_CHAIN (vfields);
5314 /* Make the rtl for any new vtables we have created, and unmark
5315 the base types we marked. */
5318 if (CLASSTYPE_VSIZE (t) != 0)
5320 /* In addition to this one, all the other vfields should be listed. */
5321 /* Before that can be done, we have to have FIELD_DECLs for them, and
5322 a place to find them. */
5323 TYPE_NONCOPIED_PARTS (t)
5324 = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
5325 TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
5327 if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
5328 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5329 cp_warning ("`%#T' has virtual functions but non-virtual destructor",
5333 hack_incomplete_structures (t);
5335 if (warn_overloaded_virtual)
5338 maybe_suppress_debug_info (t);
5340 /* Finish debugging output for this type. */
5341 rest_of_type_compilation (t, toplevel_bindings_p ());
5344 /* When T was built up, the member declarations were added in reverse
5345 order. Rearrange them to declaration order. */
5348 unreverse_member_declarations (t)
5355 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5356 reverse order. Put them in declaration order now. */
5357 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5358 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5360 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5361 reverse order, so we can't just use nreverse. */
5363 for (x = TYPE_FIELDS (t);
5364 x && TREE_CODE (x) != TYPE_DECL;
5367 next = TREE_CHAIN (x);
5368 TREE_CHAIN (x) = prev;
5373 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5375 TYPE_FIELDS (t) = prev;
5380 finish_struct (t, attributes)
5383 /* Now that we've got all the field declarations, reverse everything
5385 unreverse_member_declarations (t);
5387 cplus_decl_attributes (t, attributes, NULL_TREE);
5389 if (processing_template_decl)
5391 finish_struct_methods (t);
5392 TYPE_SIZE (t) = integer_zero_node;
5395 finish_struct_1 (t);
5397 TYPE_BEING_DEFINED (t) = 0;
5399 if (current_class_type)
5402 error ("trying to finish struct, but kicked out due to previous parse errors.");
5404 if (processing_template_decl)
5406 tree scope = current_scope ();
5407 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5408 add_tree (build_min (TAG_DEFN, t));
5414 /* Return the dynamic type of INSTANCE, if known.
5415 Used to determine whether the virtual function table is needed
5418 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5419 of our knowledge of its type. *NONNULL should be initialized
5420 before this function is called. */
5423 fixed_type_or_null (instance, nonnull)
5427 switch (TREE_CODE (instance))
5430 /* Check that we are not going through a cast of some sort. */
5431 if (TREE_TYPE (instance)
5432 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5433 instance = TREE_OPERAND (instance, 0);
5434 /* fall through... */
5436 /* This is a call to a constructor, hence it's never zero. */
5437 if (TREE_HAS_CONSTRUCTOR (instance))
5441 return TREE_TYPE (instance);
5446 /* This is a call to a constructor, hence it's never zero. */
5447 if (TREE_HAS_CONSTRUCTOR (instance))
5451 return TREE_TYPE (instance);
5453 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5460 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5461 /* Propagate nonnull. */
5462 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5463 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5464 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5469 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5474 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
5477 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
5481 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5482 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5486 return TREE_TYPE (TREE_TYPE (instance));
5488 /* fall through... */
5491 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5495 return TREE_TYPE (instance);
5499 if (instance == current_class_ptr
5500 && flag_this_is_variable <= 0)
5502 /* Normally, 'this' must be non-null. */
5503 if (flag_this_is_variable == 0)
5506 /* <0 means we're in a constructor and we know our type. */
5507 if (flag_this_is_variable < 0)
5508 return TREE_TYPE (TREE_TYPE (instance));
5510 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5511 /* Reference variables should be references to objects. */
5521 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5522 to the static type. We also handle the case where INSTANCE is really
5525 Used to determine whether the virtual function table is needed
5528 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5529 of our knowledge of its type. *NONNULL should be initialized
5530 before this function is called. */
5533 resolves_to_fixed_type_p (instance, nonnull)
5537 tree t = TREE_TYPE (instance);
5538 tree fixed = fixed_type_or_null (instance, nonnull);
5539 if (fixed == NULL_TREE)
5541 if (POINTER_TYPE_P (t))
5543 return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
5548 init_class_processing ()
5550 current_class_depth = 0;
5551 current_class_stack_size = 10;
5553 = (class_stack_node_t) xmalloc (current_class_stack_size
5554 * sizeof (struct class_stack_node));
5556 access_default_node = build_int_2 (0, 0);
5557 access_public_node = build_int_2 (1, 0);
5558 access_protected_node = build_int_2 (2, 0);
5559 access_private_node = build_int_2 (3, 0);
5560 access_default_virtual_node = build_int_2 (4, 0);
5561 access_public_virtual_node = build_int_2 (5, 0);
5562 access_protected_virtual_node = build_int_2 (6, 0);
5563 access_private_virtual_node = build_int_2 (7, 0);
5566 /* Set current scope to NAME. CODE tells us if this is a
5567 STRUCT, UNION, or ENUM environment.
5569 NAME may end up being NULL_TREE if this is an anonymous or
5570 late-bound struct (as in "struct { ... } foo;") */
5572 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5573 appropriate values, found by looking up the type definition of
5576 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5577 which can be seen locally to the class. They are shadowed by
5578 any subsequent local declaration (including parameter names).
5580 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5581 which have static meaning (i.e., static members, static
5582 member functions, enum declarations, etc).
5584 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5585 which can be seen locally to the class (as in 1), but
5586 know that we are doing this for declaration purposes
5587 (i.e. friend foo::bar (int)).
5589 So that we may avoid calls to lookup_name, we cache the _TYPE
5590 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5592 For multiple inheritance, we perform a two-pass depth-first search
5593 of the type lattice. The first pass performs a pre-order search,
5594 marking types after the type has had its fields installed in
5595 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5596 unmarks the marked types. If a field or member function name
5597 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5598 that name becomes `error_mark_node'. */
5601 pushclass (type, modify)
5605 type = TYPE_MAIN_VARIANT (type);
5607 /* Make sure there is enough room for the new entry on the stack. */
5608 if (current_class_depth + 1 >= current_class_stack_size)
5610 current_class_stack_size *= 2;
5612 = (class_stack_node_t) xrealloc (current_class_stack,
5613 current_class_stack_size
5614 * sizeof (struct class_stack_node));
5617 /* Insert a new entry on the class stack. */
5618 current_class_stack[current_class_depth].name = current_class_name;
5619 current_class_stack[current_class_depth].type = current_class_type;
5620 current_class_stack[current_class_depth].access = current_access_specifier;
5621 current_class_stack[current_class_depth].names_used = 0;
5622 current_class_depth++;
5624 /* Now set up the new type. */
5625 current_class_name = TYPE_NAME (type);
5626 if (TREE_CODE (current_class_name) == TYPE_DECL)
5627 current_class_name = DECL_NAME (current_class_name);
5628 current_class_type = type;
5630 /* By default, things in classes are private, while things in
5631 structures or unions are public. */
5632 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5633 ? access_private_node
5634 : access_public_node);
5636 if (previous_class_type != NULL_TREE
5637 && (type != previous_class_type
5638 || TYPE_SIZE (previous_class_type) == NULL_TREE)
5639 && current_class_depth == 1)
5641 /* Forcibly remove any old class remnants. */
5642 invalidate_class_lookup_cache ();
5645 /* If we're about to enter a nested class, clear
5646 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5647 if (modify && current_class_depth > 1)
5648 clear_identifier_class_values ();
5653 if (CLASSTYPE_TEMPLATE_INFO (type))
5654 overload_template_name (type);
5659 if (type != previous_class_type || current_class_depth > 1)
5660 push_class_decls (type);
5665 /* We are re-entering the same class we just left, so we
5666 don't have to search the whole inheritance matrix to find
5667 all the decls to bind again. Instead, we install the
5668 cached class_shadowed list, and walk through it binding
5669 names and setting up IDENTIFIER_TYPE_VALUEs. */
5670 set_class_shadows (previous_class_values);
5671 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5673 tree id = TREE_PURPOSE (item);
5674 tree decl = TREE_TYPE (item);
5676 push_class_binding (id, decl);
5677 if (TREE_CODE (decl) == TYPE_DECL)
5678 set_identifier_type_value (id, TREE_TYPE (decl));
5680 unuse_fields (type);
5683 storetags (CLASSTYPE_TAGS (type));
5687 /* When we exit a toplevel class scope, we save the
5688 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5689 reenter the class. Here, we've entered some other class, so we
5690 must invalidate our cache. */
5693 invalidate_class_lookup_cache ()
5697 /* This code can be seen as a cache miss. When we've cached a
5698 class' scope's bindings and we can't use them, we need to reset
5699 them. This is it! */
5700 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5701 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5703 previous_class_type = NULL_TREE;
5706 /* Get out of the current class scope. If we were in a class scope
5707 previously, that is the one popped to. */
5713 /* Since poplevel_class does the popping of class decls nowadays,
5714 this really only frees the obstack used for these decls. */
5717 current_class_depth--;
5718 current_class_name = current_class_stack[current_class_depth].name;
5719 current_class_type = current_class_stack[current_class_depth].type;
5720 current_access_specifier = current_class_stack[current_class_depth].access;
5721 if (current_class_stack[current_class_depth].names_used)
5722 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5725 /* Returns 1 if current_class_type is either T or a nested type of T.
5726 We start looking from 1 because entry 0 is from global scope, and has
5730 currently_open_class (t)
5734 if (t == current_class_type)
5736 for (i = 1; i < current_class_depth; ++i)
5737 if (current_class_stack [i].type == t)
5742 /* If either current_class_type or one of its enclosing classes are derived
5743 from T, return the appropriate type. Used to determine how we found
5744 something via unqualified lookup. */
5747 currently_open_derived_class (t)
5752 if (DERIVED_FROM_P (t, current_class_type))
5753 return current_class_type;
5755 for (i = current_class_depth - 1; i > 0; --i)
5756 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5757 return current_class_stack[i].type;
5762 /* When entering a class scope, all enclosing class scopes' names with
5763 static meaning (static variables, static functions, types and enumerators)
5764 have to be visible. This recursive function calls pushclass for all
5765 enclosing class contexts until global or a local scope is reached.
5766 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5767 formal of the same name. */
5770 push_nested_class (type, modify)
5776 /* A namespace might be passed in error cases, like A::B:C. */
5777 if (type == NULL_TREE
5778 || type == error_mark_node
5779 || TREE_CODE (type) == NAMESPACE_DECL
5780 || ! IS_AGGR_TYPE (type)
5781 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5782 || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
5785 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5787 if (context && CLASS_TYPE_P (context))
5788 push_nested_class (context, 2);
5789 pushclass (type, modify);
5792 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5797 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5800 if (context && CLASS_TYPE_P (context))
5801 pop_nested_class ();
5804 /* Set global variables CURRENT_LANG_NAME to appropriate value
5805 so that behavior of name-mangling machinery is correct. */
5808 push_lang_context (name)
5811 *current_lang_stack++ = current_lang_name;
5812 if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
5813 >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
5815 size_t old_size = VARRAY_SIZE (current_lang_base);
5817 VARRAY_GROW (current_lang_base, old_size + 10);
5818 current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
5821 if (name == lang_name_cplusplus)
5823 strict_prototype = strict_prototypes_lang_cplusplus;
5824 current_lang_name = name;
5826 else if (name == lang_name_java)
5828 strict_prototype = strict_prototypes_lang_cplusplus;
5829 current_lang_name = name;
5830 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5831 (See record_builtin_java_type in decl.c.) However, that causes
5832 incorrect debug entries if these types are actually used.
5833 So we re-enable debug output after extern "Java". */
5834 DECL_IGNORED_P (java_byte_type_node) = 0;
5835 DECL_IGNORED_P (java_short_type_node) = 0;
5836 DECL_IGNORED_P (java_int_type_node) = 0;
5837 DECL_IGNORED_P (java_long_type_node) = 0;
5838 DECL_IGNORED_P (java_float_type_node) = 0;
5839 DECL_IGNORED_P (java_double_type_node) = 0;
5840 DECL_IGNORED_P (java_char_type_node) = 0;
5841 DECL_IGNORED_P (java_boolean_type_node) = 0;
5843 else if (name == lang_name_c)
5845 strict_prototype = strict_prototypes_lang_c;
5846 current_lang_name = name;
5849 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5852 /* Get out of the current language scope. */
5857 /* Clear the current entry so that garbage collector won't hold on
5859 *current_lang_stack = NULL_TREE;
5860 current_lang_name = *--current_lang_stack;
5861 if (current_lang_name == lang_name_cplusplus
5862 || current_lang_name == lang_name_java)
5863 strict_prototype = strict_prototypes_lang_cplusplus;
5864 else if (current_lang_name == lang_name_c)
5865 strict_prototype = strict_prototypes_lang_c;
5868 /* Type instantiation routines. */
5870 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5871 matches the TARGET_TYPE. If there is no satisfactory match, return
5872 error_mark_node, and issue an error message if COMPLAIN is
5873 non-zero. If TEMPLATE_ONLY, the name of the overloaded function
5874 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5875 template arguments. */
5878 resolve_address_of_overloaded_function (target_type,
5887 tree explicit_targs;
5889 /* Here's what the standard says:
5893 If the name is a function template, template argument deduction
5894 is done, and if the argument deduction succeeds, the deduced
5895 arguments are used to generate a single template function, which
5896 is added to the set of overloaded functions considered.
5898 Non-member functions and static member functions match targets of
5899 type "pointer-to-function" or "reference-to-function." Nonstatic
5900 member functions match targets of type "pointer-to-member
5901 function;" the function type of the pointer to member is used to
5902 select the member function from the set of overloaded member
5903 functions. If a nonstatic member function is selected, the
5904 reference to the overloaded function name is required to have the
5905 form of a pointer to member as described in 5.3.1.
5907 If more than one function is selected, any template functions in
5908 the set are eliminated if the set also contains a non-template
5909 function, and any given template function is eliminated if the
5910 set contains a second template function that is more specialized
5911 than the first according to the partial ordering rules 14.5.5.2.
5912 After such eliminations, if any, there shall remain exactly one
5913 selected function. */
5916 int is_reference = 0;
5917 /* We store the matches in a TREE_LIST rooted here. The functions
5918 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5919 interoperability with most_specialized_instantiation. */
5920 tree matches = NULL_TREE;
5923 /* By the time we get here, we should be seeing only real
5924 pointer-to-member types, not the internal POINTER_TYPE to
5925 METHOD_TYPE representation. */
5926 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5927 && (TREE_CODE (TREE_TYPE (target_type))
5928 == METHOD_TYPE)), 0);
5930 /* Check that the TARGET_TYPE is reasonable. */
5931 if (TYPE_PTRFN_P (target_type))
5934 else if (TYPE_PTRMEMFUNC_P (target_type))
5935 /* This is OK, too. */
5937 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5939 /* This is OK, too. This comes from a conversion to reference
5941 target_type = build_reference_type (target_type);
5947 cp_error("cannot resolve overloaded function `%D' based on conversion to type `%T'",
5948 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5949 return error_mark_node;
5952 /* If we can find a non-template function that matches, we can just
5953 use it. There's no point in generating template instantiations
5954 if we're just going to throw them out anyhow. But, of course, we
5955 can only do this when we don't *need* a template function. */
5960 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5962 tree fn = OVL_FUNCTION (fns);
5965 if (TREE_CODE (fn) == TEMPLATE_DECL)
5966 /* We're not looking for templates just yet. */
5969 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5971 /* We're looking for a non-static member, and this isn't
5972 one, or vice versa. */
5975 /* See if there's a match. */
5976 fntype = TREE_TYPE (fn);
5978 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5979 else if (!is_reference)
5980 fntype = build_pointer_type (fntype);
5982 if (can_convert_arg (target_type, fntype, fn))
5983 matches = tree_cons (fn, NULL_TREE, matches);
5987 /* Now, if we've already got a match (or matches), there's no need
5988 to proceed to the template functions. But, if we don't have a
5989 match we need to look at them, too. */
5992 tree target_fn_type;
5993 tree target_arg_types;
5994 tree target_ret_type;
5999 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6001 target_fn_type = TREE_TYPE (target_type);
6002 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6003 target_ret_type = TREE_TYPE (target_fn_type);
6005 for (fns = overload; fns; fns = OVL_CHAIN (fns))
6007 tree fn = OVL_FUNCTION (fns);
6009 tree instantiation_type;
6012 if (TREE_CODE (fn) != TEMPLATE_DECL)
6013 /* We're only looking for templates. */
6016 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6018 /* We're not looking for a non-static member, and this is
6019 one, or vice versa. */
6022 /* Try to do argument deduction. */
6023 targs = make_tree_vec (DECL_NTPARMS (fn));
6024 if (fn_type_unification (fn, explicit_targs, targs,
6025 target_arg_types, target_ret_type,
6027 /* Argument deduction failed. */
6030 /* Instantiate the template. */
6031 instantiation = instantiate_template (fn, targs);
6032 if (instantiation == error_mark_node)
6033 /* Instantiation failed. */
6036 /* See if there's a match. */
6037 instantiation_type = TREE_TYPE (instantiation);
6039 instantiation_type =
6040 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6041 else if (!is_reference)
6042 instantiation_type = build_pointer_type (instantiation_type);
6043 if (can_convert_arg (target_type, instantiation_type, instantiation))
6044 matches = tree_cons (instantiation, fn, matches);
6047 /* Now, remove all but the most specialized of the matches. */
6050 tree match = most_specialized_instantiation (matches,
6053 if (match != error_mark_node)
6054 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6058 /* Now we should have exactly one function in MATCHES. */
6059 if (matches == NULL_TREE)
6061 /* There were *no* matches. */
6064 cp_error ("no matches converting function `%D' to type `%#T'",
6065 DECL_NAME (OVL_FUNCTION (overload)),
6068 /* print_candidates expects a chain with the functions in
6069 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6070 so why be clever?). */
6071 for (; overload; overload = OVL_NEXT (overload))
6072 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6075 print_candidates (matches);
6077 return error_mark_node;
6079 else if (TREE_CHAIN (matches))
6081 /* There were too many matches. */
6087 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6088 DECL_NAME (OVL_FUNCTION (overload)),
6091 /* Since print_candidates expects the functions in the
6092 TREE_VALUE slot, we flip them here. */
6093 for (match = matches; match; match = TREE_CHAIN (match))
6094 TREE_VALUE (match) = TREE_PURPOSE (match);
6096 print_candidates (matches);
6099 return error_mark_node;
6102 /* Good, exactly one match. Now, convert it to the correct type. */
6103 fn = TREE_PURPOSE (matches);
6107 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6108 return build_unary_op (ADDR_EXPR, fn, 0);
6111 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6112 will mark the function as addressed, but here we must do it
6114 mark_addressable (fn);
6120 /* This function will instantiate the type of the expression given in
6121 RHS to match the type of LHSTYPE. If errors exist, then return
6122 error_mark_node. We only complain is COMPLAIN is set. If we are
6123 not complaining, never modify rhs, as overload resolution wants to
6124 try many possible instantiations, in hopes that at least one will
6127 FLAGS is a bitmask, as we see at the top of the function.
6129 For non-recursive calls, LHSTYPE should be a function, pointer to
6130 function, or a pointer to member function. */
6133 instantiate_type (lhstype, rhs, flags)
6137 int complain = (flags & 1);
6138 int strict = (flags & 2) ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6140 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6143 error ("not enough type information");
6144 return error_mark_node;
6147 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6149 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6152 cp_error ("argument of type `%T' does not match `%T'",
6153 TREE_TYPE (rhs), lhstype);
6154 return error_mark_node;
6157 /* We don't overwrite rhs if it is an overloaded function.
6158 Copying it would destroy the tree link. */
6159 if (TREE_CODE (rhs) != OVERLOAD)
6160 rhs = copy_node (rhs);
6162 /* This should really only be used when attempting to distinguish
6163 what sort of a pointer to function we have. For now, any
6164 arithmetic operation which is not supported on pointers
6165 is rejected as an error. */
6167 switch (TREE_CODE (rhs))
6174 my_friendly_abort (177);
6175 return error_mark_node;
6182 new_rhs = instantiate_type (build_pointer_type (lhstype),
6183 TREE_OPERAND (rhs, 0), flags);
6184 if (new_rhs == error_mark_node)
6185 return error_mark_node;
6187 TREE_TYPE (rhs) = lhstype;
6188 TREE_OPERAND (rhs, 0) = new_rhs;
6193 rhs = copy_node (TREE_OPERAND (rhs, 0));
6194 TREE_TYPE (rhs) = unknown_type_node;
6195 return instantiate_type (lhstype, rhs, flags);
6199 tree r = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6201 if (r != error_mark_node && TYPE_PTRMEMFUNC_P (lhstype)
6202 && complain && !flag_ms_extensions)
6204 /* Note: we check this after the recursive call to avoid
6205 complaining about cases where overload resolution fails. */
6207 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
6208 tree fn = PTRMEM_CST_MEMBER (r);
6210 my_friendly_assert (TREE_CODE (r) == PTRMEM_CST, 990811);
6213 ("object-dependent reference to `%E' can only be used in a call",
6216 (" to form a pointer to member function, say `&%T::%E'",
6224 rhs = TREE_OPERAND (rhs, 1);
6225 if (BASELINK_P (rhs))
6226 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6228 /* This can happen if we are forming a pointer-to-member for a
6230 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6234 case TEMPLATE_ID_EXPR:
6236 resolve_address_of_overloaded_function (lhstype,
6237 TREE_OPERAND (rhs, 0),
6239 /*template_only=*/1,
6240 TREE_OPERAND (rhs, 1));
6244 resolve_address_of_overloaded_function (lhstype,
6247 /*template_only=*/0,
6248 /*explicit_targs=*/NULL_TREE);
6251 /* Now we should have a baselink. */
6252 my_friendly_assert (BASELINK_P (rhs), 990412);
6254 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6257 /* This is too hard for now. */
6258 my_friendly_abort (183);
6259 return error_mark_node;
6264 TREE_OPERAND (rhs, 0)
6265 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6266 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6267 return error_mark_node;
6268 TREE_OPERAND (rhs, 1)
6269 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6270 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6271 return error_mark_node;
6273 TREE_TYPE (rhs) = lhstype;
6277 case TRUNC_DIV_EXPR:
6278 case FLOOR_DIV_EXPR:
6280 case ROUND_DIV_EXPR:
6282 case TRUNC_MOD_EXPR:
6283 case FLOOR_MOD_EXPR:
6285 case ROUND_MOD_EXPR:
6286 case FIX_ROUND_EXPR:
6287 case FIX_FLOOR_EXPR:
6289 case FIX_TRUNC_EXPR:
6305 case PREINCREMENT_EXPR:
6306 case PREDECREMENT_EXPR:
6307 case POSTINCREMENT_EXPR:
6308 case POSTDECREMENT_EXPR:
6310 error ("invalid operation on uninstantiated type");
6311 return error_mark_node;
6313 case TRUTH_AND_EXPR:
6315 case TRUTH_XOR_EXPR:
6322 case TRUTH_ANDIF_EXPR:
6323 case TRUTH_ORIF_EXPR:
6324 case TRUTH_NOT_EXPR:
6326 error ("not enough type information");
6327 return error_mark_node;
6330 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6333 error ("not enough type information");
6334 return error_mark_node;
6336 TREE_OPERAND (rhs, 1)
6337 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6338 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6339 return error_mark_node;
6340 TREE_OPERAND (rhs, 2)
6341 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6342 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6343 return error_mark_node;
6345 TREE_TYPE (rhs) = lhstype;
6349 TREE_OPERAND (rhs, 1)
6350 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6351 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6352 return error_mark_node;
6354 TREE_TYPE (rhs) = lhstype;
6358 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6360 case ENTRY_VALUE_EXPR:
6361 my_friendly_abort (184);
6362 return error_mark_node;
6365 return error_mark_node;
6368 my_friendly_abort (185);
6369 return error_mark_node;
6373 /* Return the name of the virtual function pointer field
6374 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6375 this may have to look back through base types to find the
6376 ultimate field name. (For single inheritance, these could
6377 all be the same name. Who knows for multiple inheritance). */
6380 get_vfield_name (type)
6383 tree binfo = TYPE_BINFO (type);
6386 while (BINFO_BASETYPES (binfo)
6387 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6388 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6389 binfo = BINFO_BASETYPE (binfo, 0);
6391 type = BINFO_TYPE (binfo);
6392 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6393 + TYPE_NAME_LENGTH (type) + 2);
6394 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6395 return get_identifier (buf);
6399 print_class_statistics ()
6401 #ifdef GATHER_STATISTICS
6402 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6403 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6404 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6405 n_build_method_call, n_inner_fields_searched);
6408 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6409 n_vtables, n_vtable_searches);
6410 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6411 n_vtable_entries, n_vtable_elems);
6416 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6417 according to [class]:
6418 The class-name is also inserted
6419 into the scope of the class itself. For purposes of access checking,
6420 the inserted class name is treated as if it were a public member name. */
6423 build_self_reference ()
6425 tree name = constructor_name (current_class_type);
6426 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6429 DECL_NONLOCAL (value) = 1;
6430 DECL_CONTEXT (value) = current_class_type;
6431 DECL_ARTIFICIAL (value) = 1;
6433 if (processing_template_decl)
6434 value = push_template_decl (value);
6436 saved_cas = current_access_specifier;
6437 current_access_specifier = access_public_node;
6438 finish_member_declaration (value);
6439 current_access_specifier = saved_cas;
6442 /* Returns 1 if TYPE contains only padding bytes. */
6445 is_empty_class (type)
6450 if (type == error_mark_node)
6453 if (! IS_AGGR_TYPE (type))
6457 return integer_zerop (CLASSTYPE_SIZE (type));
6459 if (TYPE_BINFO_BASETYPES (type))
6461 t = TYPE_FIELDS (type);
6462 while (t && TREE_CODE (t) != FIELD_DECL)
6464 return (t == NULL_TREE);
6467 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6468 a *_TYPE node. NODE can also be a local class. */
6471 get_enclosing_class (type)
6476 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6478 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6481 node = DECL_CONTEXT (node);
6487 node = TYPE_CONTEXT (node);
6491 my_friendly_abort (0);
6497 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6500 is_base_of_enclosing_class (base, type)
6505 if (get_binfo (base, type, 0))
6508 type = get_enclosing_class (type);
6513 /* Note that NAME was looked up while the current class was being
6514 defined and that the result of that lookup was DECL. */
6517 maybe_note_name_used_in_class (name, decl)
6521 splay_tree names_used;
6523 /* If we're not defining a class, there's nothing to do. */
6524 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6527 /* If there's already a binding for this NAME, then we don't have
6528 anything to worry about. */
6529 if (IDENTIFIER_CLASS_VALUE (name))
6532 if (!current_class_stack[current_class_depth - 1].names_used)
6533 current_class_stack[current_class_depth - 1].names_used
6534 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6535 names_used = current_class_stack[current_class_depth - 1].names_used;
6537 splay_tree_insert (names_used,
6538 (splay_tree_key) name,
6539 (splay_tree_value) decl);
6542 /* Note that NAME was declared (as DECL) in the current class. Check
6543 to see that the declaration is legal. */
6546 note_name_declared_in_class (name, decl)
6550 splay_tree names_used;
6553 /* Look to see if we ever used this name. */
6555 = current_class_stack[current_class_depth - 1].names_used;
6559 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6562 /* [basic.scope.class]
6564 A name N used in a class S shall refer to the same declaration
6565 in its context and when re-evaluated in the completed scope of
6567 cp_error ("declaration of `%#D'", decl);
6568 cp_error_at ("changes meaning of `%s' from `%+#D'",
6569 IDENTIFIER_POINTER (DECL_NAME (OVL_CURRENT (decl))),
6574 /* Dump the offsets of all the bases rooted at BINFO to stderr.
6575 INDENT should be zero when called from the top level; it is
6576 incremented recursively. */
6579 dump_class_hierarchy (binfo, indent)
6585 fprintf (stderr, "%*s0x%lx (%s) ", indent, "",
6586 (unsigned long) binfo,
6587 type_as_string (binfo, TS_PLAIN));
6588 fprintf (stderr, HOST_WIDE_INT_PRINT_DEC,
6589 tree_low_cst (BINFO_OFFSET (binfo), 0));
6590 fprintf (stderr, " %s\n",
6591 BINFO_PRIMARY_MARKED_P (binfo) ? "primary" : "");
6593 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6594 dump_class_hierarchy (BINFO_BASETYPE (binfo, i), indent + 2);