1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 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. */
39 #define obstack_chunk_alloc xmalloc
40 #define obstack_chunk_free free
42 /* The number of nested classes being processed. If we are not in the
43 scope of any class, this is zero. */
45 int current_class_depth;
47 /* In order to deal with nested classes, we keep a stack of classes.
48 The topmost entry is the innermost class, and is the entry at index
49 CURRENT_CLASS_DEPTH */
51 typedef struct class_stack_node {
52 /* The name of the class. */
55 /* The _TYPE node for the class. */
58 /* The access specifier pending for new declarations in the scope of
62 /* If were defining TYPE, the names used in this class. */
63 splay_tree names_used;
64 }* class_stack_node_t;
66 typedef struct vtbl_init_data_s
68 /* The base for which we're building initializers. */
70 /* The type of the most-derived type. */
72 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
73 unless ctor_vtbl_p is true. */
75 /* The negative-index vtable initializers built up so far. These
76 are in order from least negative index to most negative index. */
78 /* The last (i.e., most negative) entry in INITS. */
80 /* The binfo for the virtual base for which we're building
81 vcall offset initializers. */
83 /* The functions in vbase for which we have already provided vcall
86 /* The vtable index of the next vcall or vbase offset. */
88 /* Nonzero if we are building the initializer for the primary
91 /* Nonzero if we are building the initializer for a construction
96 /* The type of a function passed to walk_subobject_offsets. */
97 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
99 /* The stack itself. This is an dynamically resized array. The
100 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
101 static int current_class_stack_size;
102 static class_stack_node_t current_class_stack;
104 /* An array of all local classes present in this translation unit, in
105 declaration order. */
106 varray_type local_classes;
108 static tree get_vfield_name PARAMS ((tree));
109 static void finish_struct_anon PARAMS ((tree));
110 static tree build_vtable_entry PARAMS ((tree, tree, tree));
111 static tree get_vtable_name PARAMS ((tree));
112 static tree get_basefndecls PARAMS ((tree, tree));
113 static int build_primary_vtable PARAMS ((tree, tree));
114 static int build_secondary_vtable PARAMS ((tree, tree));
115 static void finish_vtbls PARAMS ((tree));
116 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
117 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
118 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
119 static void delete_duplicate_fields PARAMS ((tree));
120 static void finish_struct_bits PARAMS ((tree));
121 static int alter_access PARAMS ((tree, tree, tree));
122 static void handle_using_decl PARAMS ((tree, tree));
123 static int strictly_overrides PARAMS ((tree, tree));
124 static void check_for_override PARAMS ((tree, tree));
125 static tree dfs_modify_vtables PARAMS ((tree, void *));
126 static tree modify_all_vtables PARAMS ((tree, int *, tree));
127 static void determine_primary_base PARAMS ((tree, int *));
128 static void finish_struct_methods PARAMS ((tree));
129 static void maybe_warn_about_overly_private_class PARAMS ((tree));
130 static int field_decl_cmp PARAMS ((const tree *, const tree *));
131 static int method_name_cmp PARAMS ((const tree *, const tree *));
132 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
133 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
134 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
136 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
137 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
138 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
139 static int count_fields PARAMS ((tree));
140 static int add_fields_to_vec PARAMS ((tree, tree, int));
141 static void check_bitfield_decl PARAMS ((tree));
142 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
143 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
145 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
147 static bool build_base_fields PARAMS ((record_layout_info, int *,
149 static void check_methods PARAMS ((tree));
150 static void remove_zero_width_bit_fields PARAMS ((tree));
151 static void check_bases PARAMS ((tree, int *, int *, int *));
152 static void check_bases_and_members PARAMS ((tree, int *));
153 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
154 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
155 static void fixup_pending_inline PARAMS ((tree));
156 static void fixup_inline_methods PARAMS ((tree));
157 static void set_primary_base PARAMS ((tree, tree, int *));
158 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
159 static void layout_virtual_bases PARAMS ((tree, splay_tree));
160 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
161 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
162 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
163 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
164 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
165 static void layout_vtable_decl PARAMS ((tree, int));
166 static tree dfs_find_final_overrider PARAMS ((tree, void *));
167 static tree find_final_overrider PARAMS ((tree, tree, tree));
168 static int make_new_vtable PARAMS ((tree, tree));
169 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
170 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
171 static void dump_class_hierarchy PARAMS ((tree));
172 static void dump_array PARAMS ((FILE *, tree));
173 static void dump_vtable PARAMS ((tree, tree, tree));
174 static void dump_vtt PARAMS ((tree, tree));
175 static tree build_vtable PARAMS ((tree, tree, tree));
176 static void initialize_vtable PARAMS ((tree, tree));
177 static void initialize_array PARAMS ((tree, tree));
178 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
181 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
182 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
183 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
184 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
186 static void set_vindex PARAMS ((tree, int *));
187 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
188 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
190 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
191 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
192 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
193 static void mark_primary_bases PARAMS ((tree));
194 static tree mark_primary_virtual_base PARAMS ((tree, tree));
195 static void clone_constructors_and_destructors PARAMS ((tree));
196 static tree build_clone PARAMS ((tree, tree));
197 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
198 static tree copy_virtuals PARAMS ((tree));
199 static void build_ctor_vtbl_group PARAMS ((tree, tree));
200 static void build_vtt PARAMS ((tree));
201 static tree binfo_ctor_vtable PARAMS ((tree));
202 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
203 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
204 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
205 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
206 static tree get_original_base PARAMS ((tree, tree));
207 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
208 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
209 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
210 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
211 tree, splay_tree, tree, int));
212 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
213 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
214 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
216 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
217 static bool type_requires_array_cookie PARAMS ((tree));
219 /* Macros for dfs walking during vtt construction. See
220 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
221 and dfs_fixup_binfo_vtbls. */
222 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
223 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
225 /* Variables shared between class.c and call.c. */
227 #ifdef GATHER_STATISTICS
229 int n_vtable_entries = 0;
230 int n_vtable_searches = 0;
231 int n_vtable_elems = 0;
232 int n_convert_harshness = 0;
233 int n_compute_conversion_costs = 0;
234 int n_build_method_call = 0;
235 int n_inner_fields_searched = 0;
238 /* Convert to or from a base subobject. EXPR is an expression of type
239 `A' or `A*', an expression of type `B' or `B*' is returned. To
240 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
241 the B base instance within A. To convert base A to derived B, CODE
242 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
243 In this latter case, A must not be a morally virtual base of B.
244 NONNULL is true if EXPR is known to be non-NULL (this is only
245 needed when EXPR is of pointer type). CV qualifiers are preserved
249 build_base_path (code, expr, binfo, nonnull)
255 tree v_binfo = NULL_TREE;
256 tree d_binfo = NULL_TREE;
260 tree null_test = NULL;
261 tree ptr_target_type;
263 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
265 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
266 return error_mark_node;
268 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
271 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
275 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
277 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
279 my_friendly_assert (code == MINUS_EXPR
280 ? same_type_p (BINFO_TYPE (binfo), probe)
282 ? same_type_p (BINFO_TYPE (d_binfo), probe)
285 if (code == MINUS_EXPR && v_binfo)
287 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
288 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
289 return error_mark_node;
292 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
293 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
294 expr = save_expr (expr);
297 expr = build_unary_op (ADDR_EXPR, expr, 0);
299 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
301 offset = BINFO_OFFSET (binfo);
303 if (v_binfo && fixed_type_p <= 0)
305 /* Going via virtual base V_BINFO. We need the static offset
306 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
307 V_BINFO. That offset is an entry in D_BINFO's vtable. */
308 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
309 TREE_TYPE (TREE_TYPE (expr)));
311 v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
313 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
314 v_offset, BINFO_VPTR_FIELD (v_binfo));
315 v_offset = build1 (NOP_EXPR,
316 build_pointer_type (ptrdiff_type_node),
318 v_offset = build_indirect_ref (v_offset, NULL);
319 TREE_CONSTANT (v_offset) = 1;
321 offset = cp_convert (ptrdiff_type_node,
322 size_diffop (offset, BINFO_OFFSET (v_binfo)));
324 if (!integer_zerop (offset))
325 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
327 if (fixed_type_p < 0)
328 /* Negative fixed_type_p means this is a constructor or destructor;
329 virtual base layout is fixed in in-charge [cd]tors, but not in
331 offset = build (COND_EXPR, ptrdiff_type_node,
332 build (EQ_EXPR, boolean_type_node,
333 current_in_charge_parm, integer_zero_node),
335 BINFO_OFFSET (binfo));
340 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
342 target_type = cp_build_qualified_type
343 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
344 ptr_target_type = build_pointer_type (target_type);
346 target_type = ptr_target_type;
348 expr = build1 (NOP_EXPR, ptr_target_type, expr);
350 if (!integer_zerop (offset))
351 expr = build (code, ptr_target_type, expr, offset);
356 expr = build_indirect_ref (expr, NULL);
359 expr = build (COND_EXPR, target_type, null_test,
360 build1 (NOP_EXPR, target_type, integer_zero_node),
367 /* Virtual function things. */
370 build_vtable_entry_ref (array_ref, instance, idx)
371 tree array_ref, instance, idx;
373 tree i, i2, vtable, first_fn, basetype;
375 basetype = TREE_TYPE (instance);
376 if (TREE_CODE (basetype) == REFERENCE_TYPE)
377 basetype = TREE_TYPE (basetype);
379 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
380 first_fn = TYPE_BINFO_VTABLE (basetype);
382 i = fold (build_array_ref (first_fn, idx));
383 i = fold (build_c_cast (ptrdiff_type_node,
384 build_unary_op (ADDR_EXPR, i, 0)));
385 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
386 i2 = fold (build_c_cast (ptrdiff_type_node,
387 build_unary_op (ADDR_EXPR, i2, 0)));
388 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
390 if (TREE_CODE (i) != INTEGER_CST)
393 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
396 /* Given an object INSTANCE, return an expression which yields the
397 vtable element corresponding to INDEX. There are many special
398 cases for INSTANCE which we take care of here, mainly to avoid
399 creating extra tree nodes when we don't have to. */
402 build_vtbl_ref_1 (instance, idx)
406 tree vtbl = NULL_TREE;
408 /* Try to figure out what a reference refers to, and
409 access its virtual function table directly. */
412 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
414 tree basetype = TREE_TYPE (instance);
415 if (TREE_CODE (basetype) == REFERENCE_TYPE)
416 basetype = TREE_TYPE (basetype);
418 if (fixed_type && !cdtorp)
420 tree binfo = lookup_base (fixed_type, basetype,
421 ba_ignore|ba_quiet, NULL);
423 vtbl = BINFO_VTABLE (binfo);
428 vtbl = build_vfield_ref (instance, basetype);
431 assemble_external (vtbl);
433 aref = build_array_ref (vtbl, idx);
434 TREE_CONSTANT (aref) = 1;
440 build_vtbl_ref (instance, idx)
443 tree aref = build_vtbl_ref_1 (instance, idx);
446 aref = build_vtable_entry_ref (aref, instance, idx);
451 /* Given an object INSTANCE, return an expression which yields a
452 function pointer corresponding to vtable element INDEX. */
455 build_vfn_ref (instance, idx)
458 tree aref = build_vtbl_ref_1 (instance, idx);
460 /* When using function descriptors, the address of the
461 vtable entry is treated as a function pointer. */
462 if (TARGET_VTABLE_USES_DESCRIPTORS)
463 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
464 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
467 aref = build_vtable_entry_ref (aref, instance, idx);
472 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
473 for the given TYPE. */
476 get_vtable_name (type)
479 return mangle_vtbl_for_type (type);
482 /* Return an IDENTIFIER_NODE for the name of the virtual table table
489 return mangle_vtt_for_type (type);
492 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
493 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
494 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
497 build_vtable (class_type, name, vtable_type)
504 decl = build_lang_decl (VAR_DECL, name, vtable_type);
505 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
506 now to avoid confusion in mangle_decl. */
507 SET_DECL_ASSEMBLER_NAME (decl, name);
508 DECL_CONTEXT (decl) = class_type;
509 DECL_ARTIFICIAL (decl) = 1;
510 TREE_STATIC (decl) = 1;
511 TREE_READONLY (decl) = 1;
512 DECL_VIRTUAL_P (decl) = 1;
513 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
515 import_export_vtable (decl, class_type, 0);
520 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
521 or even complete. If this does not exist, create it. If COMPLETE is
522 non-zero, then complete the definition of it -- that will render it
523 impossible to actually build the vtable, but is useful to get at those
524 which are known to exist in the runtime. */
527 get_vtable_decl (type, complete)
531 tree name = get_vtable_name (type);
532 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
536 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
537 && DECL_VIRTUAL_P (decl), 20000118);
541 decl = build_vtable (type, name, void_type_node);
542 decl = pushdecl_top_level (decl);
543 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
546 /* At one time the vtable info was grabbed 2 words at a time. This
547 fails on sparc unless you have 8-byte alignment. (tiemann) */
548 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
553 DECL_EXTERNAL (decl) = 1;
554 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
560 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
561 BV_VCALL_INDEX for each entry is cleared. */
564 copy_virtuals (binfo)
570 copies = copy_list (BINFO_VIRTUALS (binfo));
571 for (t = copies; t; t = TREE_CHAIN (t))
573 BV_VCALL_INDEX (t) = NULL_TREE;
574 BV_USE_VCALL_INDEX_P (t) = 0;
580 /* Build the primary virtual function table for TYPE. If BINFO is
581 non-NULL, build the vtable starting with the initial approximation
582 that it is the same as the one which is the head of the association
583 list. Returns a non-zero value if a new vtable is actually
587 build_primary_vtable (binfo, type)
593 decl = get_vtable_decl (type, /*complete=*/0);
597 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
598 /* We have already created a vtable for this base, so there's
599 no need to do it again. */
602 virtuals = copy_virtuals (binfo);
603 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
604 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
605 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
609 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
611 virtuals = NULL_TREE;
614 #ifdef GATHER_STATISTICS
616 n_vtable_elems += list_length (virtuals);
619 /* Initialize the association list for this type, based
620 on our first approximation. */
621 TYPE_BINFO_VTABLE (type) = decl;
622 TYPE_BINFO_VIRTUALS (type) = virtuals;
623 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
627 /* Give BINFO a new virtual function table which is initialized
628 with a skeleton-copy of its original initialization. The only
629 entry that changes is the `delta' entry, so we can really
630 share a lot of structure.
632 FOR_TYPE is the most derived type which caused this table to
635 Returns non-zero if we haven't met BINFO before.
637 The order in which vtables are built (by calling this function) for
638 an object must remain the same, otherwise a binary incompatibility
642 build_secondary_vtable (binfo, for_type)
643 tree binfo, for_type;
645 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
647 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
648 /* We already created a vtable for this base. There's no need to
652 /* Remember that we've created a vtable for this BINFO, so that we
653 don't try to do so again. */
654 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
656 /* Make fresh virtual list, so we can smash it later. */
657 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
659 /* Secondary vtables are laid out as part of the same structure as
660 the primary vtable. */
661 BINFO_VTABLE (binfo) = NULL_TREE;
665 /* Create a new vtable for BINFO which is the hierarchy dominated by
666 T. Return non-zero if we actually created a new vtable. */
669 make_new_vtable (t, binfo)
673 if (binfo == TYPE_BINFO (t))
674 /* In this case, it is *type*'s vtable we are modifying. We start
675 with the approximation that its vtable is that of the
676 immediate base class. */
677 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
678 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
679 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
682 /* This is our very own copy of `basetype' to play with. Later,
683 we will fill in all the virtual functions that override the
684 virtual functions in these base classes which are not defined
685 by the current type. */
686 return build_secondary_vtable (binfo, t);
689 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
690 (which is in the hierarchy dominated by T) list FNDECL as its
691 BV_FN. DELTA is the required constant adjustment from the `this'
692 pointer where the vtable entry appears to the `this' required when
693 the function is actually called. */
696 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
707 if (fndecl != BV_FN (v)
708 || !tree_int_cst_equal (delta, BV_DELTA (v)))
712 /* We need a new vtable for BINFO. */
713 if (make_new_vtable (t, binfo))
715 /* If we really did make a new vtable, we also made a copy
716 of the BINFO_VIRTUALS list. Now, we have to find the
717 corresponding entry in that list. */
718 *virtuals = BINFO_VIRTUALS (binfo);
719 while (BV_FN (*virtuals) != BV_FN (v))
720 *virtuals = TREE_CHAIN (*virtuals);
724 base_fndecl = BV_FN (v);
725 BV_DELTA (v) = delta;
726 BV_VCALL_INDEX (v) = NULL_TREE;
729 /* Now assign virtual dispatch information, if unset. We can
730 dispatch this through any overridden base function.
732 FIXME this can choose a secondary vtable if the primary is not
733 also lexically first, leading to useless conversions.
734 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
735 ever be different from DECL_CONTEXT. */
736 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
738 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
739 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
744 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
745 functions present in the vtable so far. */
748 set_vindex (decl, vfuns_p)
755 *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
756 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
757 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
760 /* Add a virtual function to all the appropriate vtables for the class
761 T. DECL_VINDEX(X) should be error_mark_node, if we want to
762 allocate a new slot in our table. If it is error_mark_node, we
763 know that no other function from another vtable is overridden by X.
764 VFUNS_P keeps track of how many virtuals there are in our
765 main vtable for the type, and we build upon the NEW_VIRTUALS list
769 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
771 tree *new_virtuals_p;
772 tree *overridden_virtuals_p;
775 tree t; /* Structure type. */
779 /* If this function doesn't override anything from a base class, we
780 can just assign it a new DECL_VINDEX now. Otherwise, if it does
781 override something, we keep it around and assign its DECL_VINDEX
782 later, in modify_all_vtables. */
783 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
784 /* We've already dealt with this function. */
787 new_virtual = make_node (TREE_LIST);
788 BV_FN (new_virtual) = fndecl;
789 BV_DELTA (new_virtual) = integer_zero_node;
791 if (DECL_VINDEX (fndecl) == error_mark_node)
793 /* FNDECL is a new virtual function; it doesn't override any
794 virtual function in a base class. */
796 /* We remember that this was the base sub-object for rtti. */
797 CLASSTYPE_RTTI (t) = t;
799 /* Now assign virtual dispatch information. */
800 set_vindex (fndecl, vfuns_p);
801 DECL_VIRTUAL_CONTEXT (fndecl) = t;
803 /* Save the state we've computed on the NEW_VIRTUALS list. */
804 TREE_CHAIN (new_virtual) = *new_virtuals_p;
805 *new_virtuals_p = new_virtual;
809 /* FNDECL overrides a function from a base class. */
810 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
811 *overridden_virtuals_p = new_virtual;
815 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
816 the method after the class has already been defined because a
817 declaration for it was seen. (Even though that is erroneous, we
818 add the method for improved error recovery.) */
821 add_method (type, method, error_p)
826 int using = (DECL_CONTEXT (method) != type);
830 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
831 && DECL_TEMPLATE_CONV_FN_P (method));
833 if (!CLASSTYPE_METHOD_VEC (type))
834 /* Make a new method vector. We start with 8 entries. We must
835 allocate at least two (for constructors and destructors), and
836 we're going to end up with an assignment operator at some point
839 We could use a TREE_LIST for now, and convert it to a TREE_VEC
840 in finish_struct, but we would probably waste more memory
841 making the links in the list than we would by over-allocating
842 the size of the vector here. Furthermore, we would complicate
843 all the code that expects this to be a vector. */
844 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
846 method_vec = CLASSTYPE_METHOD_VEC (type);
847 len = TREE_VEC_LENGTH (method_vec);
849 /* Constructors and destructors go in special slots. */
850 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
851 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
852 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
853 slot = CLASSTYPE_DESTRUCTOR_SLOT;
856 int have_template_convs_p = 0;
858 /* See if we already have an entry with this name. */
859 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
861 tree m = TREE_VEC_ELT (method_vec, slot);
869 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
870 && DECL_TEMPLATE_CONV_FN_P (m));
872 /* If we need to move things up, see if there's
874 if (!have_template_convs_p)
877 if (TREE_VEC_ELT (method_vec, slot))
882 if (DECL_NAME (m) == DECL_NAME (method))
888 /* We need a bigger method vector. */
892 /* In the non-error case, we are processing a class
893 definition. Double the size of the vector to give room
897 /* In the error case, the vector is already complete. We
898 don't expect many errors, and the rest of the front-end
899 will get confused if there are empty slots in the vector. */
903 new_vec = make_tree_vec (new_len);
904 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
905 len * sizeof (tree));
907 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
910 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
912 /* Type conversion operators have to come before ordinary
913 methods; add_conversions depends on this to speed up
914 looking for conversion operators. So, if necessary, we
915 slide some of the vector elements up. In theory, this
916 makes this algorithm O(N^2) but we don't expect many
917 conversion operators. */
919 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
921 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
923 tree fn = TREE_VEC_ELT (method_vec, slot);
926 /* There are no more entries in the vector, so we
927 can insert the new conversion operator here. */
930 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
931 /* We can insert the new function right at the
936 if (template_conv_p && have_template_convs_p)
938 else if (!TREE_VEC_ELT (method_vec, slot))
939 /* There is nothing in the Ith slot, so we can avoid
944 /* We know the last slot in the vector is empty
945 because we know that at this point there's room
946 for a new function. */
947 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
948 &TREE_VEC_ELT (method_vec, slot),
949 (len - slot - 1) * sizeof (tree));
950 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
955 if (template_class_depth (type))
956 /* TYPE is a template class. Don't issue any errors now; wait
957 until instantiation time to complain. */
963 /* Check to see if we've already got this method. */
964 for (fns = TREE_VEC_ELT (method_vec, slot);
966 fns = OVL_NEXT (fns))
968 tree fn = OVL_CURRENT (fns);
970 if (TREE_CODE (fn) != TREE_CODE (method))
973 if (TREE_CODE (method) != TEMPLATE_DECL)
975 /* [over.load] Member function declarations with the
976 same name and the same parameter types cannot be
977 overloaded if any of them is a static member
978 function declaration.
980 [namespace.udecl] When a using-declaration brings names
981 from a base class into a derived class scope, member
982 functions in the derived class override and/or hide member
983 functions with the same name and parameter types in a base
984 class (rather than conflicting). */
985 if ((DECL_STATIC_FUNCTION_P (fn)
986 != DECL_STATIC_FUNCTION_P (method))
989 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
990 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
993 /* Compare the quals on the 'this' parm. Don't compare
994 the whole types, as used functions are treated as
995 coming from the using class in overload resolution. */
997 && ! DECL_STATIC_FUNCTION_P (fn)
998 && ! DECL_STATIC_FUNCTION_P (method)
999 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
1000 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
1002 if (! DECL_STATIC_FUNCTION_P (fn))
1003 parms1 = TREE_CHAIN (parms1);
1004 if (! DECL_STATIC_FUNCTION_P (method))
1005 parms2 = TREE_CHAIN (parms2);
1007 if (same && compparms (parms1, parms2))
1009 if (using && DECL_CONTEXT (fn) == type)
1010 /* Defer to the local function. */
1013 error ("`%#D' and `%#D' cannot be overloaded",
1019 if (!decls_match (fn, method))
1022 /* There has already been a declaration of this method
1023 or member template. */
1024 cp_error_at ("`%D' has already been declared in `%T'",
1027 /* We don't call duplicate_decls here to merge the
1028 declarations because that will confuse things if the
1029 methods have inline definitions. In particular, we
1030 will crash while processing the definitions. */
1035 /* Actually insert the new method. */
1036 TREE_VEC_ELT (method_vec, slot)
1037 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1039 /* Add the new binding. */
1040 if (!DECL_CONSTRUCTOR_P (method)
1041 && !DECL_DESTRUCTOR_P (method))
1042 push_class_level_binding (DECL_NAME (method),
1043 TREE_VEC_ELT (method_vec, slot));
1046 /* Subroutines of finish_struct. */
1048 /* Look through the list of fields for this struct, deleting
1049 duplicates as we go. This must be recursive to handle
1052 FIELD is the field which may not appear anywhere in FIELDS.
1053 FIELD_PTR, if non-null, is the starting point at which
1054 chained deletions may take place.
1055 The value returned is the first acceptable entry found
1058 Note that anonymous fields which are not of UNION_TYPE are
1059 not duplicates, they are just anonymous fields. This happens
1060 when we have unnamed bitfields, for example. */
1063 delete_duplicate_fields_1 (field, fields)
1068 if (DECL_NAME (field) == 0)
1070 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1073 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1074 fields = delete_duplicate_fields_1 (x, fields);
1079 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1081 if (DECL_NAME (x) == 0)
1083 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1085 TYPE_FIELDS (TREE_TYPE (x))
1086 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1087 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1090 fields = TREE_CHAIN (fields);
1092 TREE_CHAIN (prev) = TREE_CHAIN (x);
1095 else if (TREE_CODE (field) == USING_DECL)
1096 /* A using declaration is allowed to appear more than
1097 once. We'll prune these from the field list later, and
1098 handle_using_decl will complain about invalid multiple
1101 else if (DECL_NAME (field) == DECL_NAME (x))
1103 if (TREE_CODE (field) == CONST_DECL
1104 && TREE_CODE (x) == CONST_DECL)
1105 cp_error_at ("duplicate enum value `%D'", x);
1106 else if (TREE_CODE (field) == CONST_DECL
1107 || TREE_CODE (x) == CONST_DECL)
1108 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1110 else if (DECL_DECLARES_TYPE_P (field)
1111 && DECL_DECLARES_TYPE_P (x))
1113 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1115 cp_error_at ("duplicate nested type `%D'", x);
1117 else if (DECL_DECLARES_TYPE_P (field)
1118 || DECL_DECLARES_TYPE_P (x))
1120 /* Hide tag decls. */
1121 if ((TREE_CODE (field) == TYPE_DECL
1122 && DECL_ARTIFICIAL (field))
1123 || (TREE_CODE (x) == TYPE_DECL
1124 && DECL_ARTIFICIAL (x)))
1126 cp_error_at ("duplicate field `%D' (as type and non-type)",
1130 cp_error_at ("duplicate member `%D'", x);
1132 fields = TREE_CHAIN (fields);
1134 TREE_CHAIN (prev) = TREE_CHAIN (x);
1142 delete_duplicate_fields (fields)
1146 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1147 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1150 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1151 legit, otherwise return 0. */
1154 alter_access (t, fdecl, access)
1161 if (!DECL_LANG_SPECIFIC (fdecl))
1162 retrofit_lang_decl (fdecl);
1164 if (DECL_DISCRIMINATOR_P (fdecl))
1167 elem = purpose_member (t, DECL_ACCESS (fdecl));
1170 if (TREE_VALUE (elem) != access)
1172 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1173 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1175 error ("conflicting access specifications for field `%s', ignored",
1176 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1180 /* They're changing the access to the same thing they changed
1181 it to before. That's OK. */
1187 enforce_access (t, fdecl);
1188 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1194 /* Process the USING_DECL, which is a member of T. */
1197 handle_using_decl (using_decl, t)
1201 tree ctype = DECL_INITIAL (using_decl);
1202 tree name = DECL_NAME (using_decl);
1204 = TREE_PRIVATE (using_decl) ? access_private_node
1205 : TREE_PROTECTED (using_decl) ? access_protected_node
1206 : access_public_node;
1208 tree flist = NULL_TREE;
1211 binfo = binfo_or_else (ctype, t);
1215 if (name == constructor_name (ctype)
1216 || name == constructor_name_full (ctype))
1218 cp_error_at ("`%D' names constructor", using_decl);
1221 if (name == constructor_name (t)
1222 || name == constructor_name_full (t))
1224 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1228 fdecl = lookup_member (binfo, name, 0, 0);
1232 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1236 if (BASELINK_P (fdecl))
1237 /* Ignore base type this came from. */
1238 fdecl = TREE_VALUE (fdecl);
1240 old_value = IDENTIFIER_CLASS_VALUE (name);
1243 if (is_overloaded_fn (old_value))
1244 old_value = OVL_CURRENT (old_value);
1246 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1249 old_value = NULL_TREE;
1252 if (is_overloaded_fn (fdecl))
1257 else if (is_overloaded_fn (old_value))
1260 /* It's OK to use functions from a base when there are functions with
1261 the same name already present in the current class. */;
1264 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1265 cp_error_at (" because of local method `%#D' with same name",
1266 OVL_CURRENT (old_value));
1270 else if (!DECL_ARTIFICIAL (old_value))
1272 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1273 cp_error_at (" because of local member `%#D' with same name", old_value);
1277 /* Make type T see field decl FDECL with access ACCESS.*/
1279 for (; flist; flist = OVL_NEXT (flist))
1281 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1282 alter_access (t, OVL_CURRENT (flist), access);
1285 alter_access (t, fdecl, access);
1288 /* Run through the base clases of T, updating
1289 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1290 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1294 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1297 int *cant_have_default_ctor_p;
1298 int *cant_have_const_ctor_p;
1299 int *no_const_asn_ref_p;
1303 int seen_non_virtual_nearly_empty_base_p;
1306 binfos = TYPE_BINFO_BASETYPES (t);
1307 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1308 seen_non_virtual_nearly_empty_base_p = 0;
1310 /* An aggregate cannot have baseclasses. */
1311 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1313 for (i = 0; i < n_baseclasses; ++i)
1318 /* Figure out what base we're looking at. */
1319 base_binfo = TREE_VEC_ELT (binfos, i);
1320 basetype = TREE_TYPE (base_binfo);
1322 /* If the type of basetype is incomplete, then we already
1323 complained about that fact (and we should have fixed it up as
1325 if (!COMPLETE_TYPE_P (basetype))
1328 /* The base type is of incomplete type. It is
1329 probably best to pretend that it does not
1331 if (i == n_baseclasses-1)
1332 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1333 TREE_VEC_LENGTH (binfos) -= 1;
1335 for (j = i; j+1 < n_baseclasses; j++)
1336 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1340 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1341 here because the case of virtual functions but non-virtual
1342 dtor is handled in finish_struct_1. */
1343 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1344 && TYPE_HAS_DESTRUCTOR (basetype))
1345 warning ("base class `%#T' has a non-virtual destructor",
1348 /* If the base class doesn't have copy constructors or
1349 assignment operators that take const references, then the
1350 derived class cannot have such a member automatically
1352 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1353 *cant_have_const_ctor_p = 1;
1354 if (TYPE_HAS_ASSIGN_REF (basetype)
1355 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1356 *no_const_asn_ref_p = 1;
1357 /* Similarly, if the base class doesn't have a default
1358 constructor, then the derived class won't have an
1359 automatically generated default constructor. */
1360 if (TYPE_HAS_CONSTRUCTOR (basetype)
1361 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1363 *cant_have_default_ctor_p = 1;
1364 if (! TYPE_HAS_CONSTRUCTOR (t))
1365 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1369 if (TREE_VIA_VIRTUAL (base_binfo))
1370 /* A virtual base does not effect nearly emptiness. */
1372 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1374 if (seen_non_virtual_nearly_empty_base_p)
1375 /* And if there is more than one nearly empty base, then the
1376 derived class is not nearly empty either. */
1377 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1379 /* Remember we've seen one. */
1380 seen_non_virtual_nearly_empty_base_p = 1;
1382 else if (!is_empty_class (basetype))
1383 /* If the base class is not empty or nearly empty, then this
1384 class cannot be nearly empty. */
1385 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1387 /* A lot of properties from the bases also apply to the derived
1389 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1390 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1391 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1392 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1393 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1394 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1395 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1396 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1397 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1398 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1402 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1403 TO. Move primary information from FROM to TO, and recursively traverse
1404 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1405 assoc list of binfos that have already been reseated. */
1408 force_canonical_binfo_r (to, from, type, mappings)
1414 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1416 my_friendly_assert (to != from, 20010905);
1417 BINFO_INDIRECT_PRIMARY_P (to)
1418 = BINFO_INDIRECT_PRIMARY_P (from);
1419 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1420 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1421 BINFO_UNSHARED_MARKED (from) = 0;
1422 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1423 BINFO_LOST_PRIMARY_P (from) = 0;
1424 if (BINFO_PRIMARY_P (from))
1426 tree primary = BINFO_PRIMARY_BASE_OF (from);
1429 /* We might have just moved the primary base too, see if it's on our
1431 assoc = purpose_member (primary, mappings);
1433 primary = TREE_VALUE (assoc);
1434 BINFO_PRIMARY_BASE_OF (to) = primary;
1435 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1437 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1439 mappings = tree_cons (from, to, mappings);
1441 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1442 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1444 tree from_primary = get_primary_binfo (from);
1446 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1447 force_canonical_binfo (get_primary_binfo (to), from_primary,
1451 for (i = 0; i != n_baseclasses; i++)
1453 tree from_binfo = BINFO_BASETYPE (from, i);
1454 tree to_binfo = BINFO_BASETYPE (to, i);
1456 if (TREE_VIA_VIRTUAL (from_binfo))
1458 if (BINFO_PRIMARY_P (from_binfo) &&
1459 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1460 /* This base is a primary of some binfo we have already
1461 reseated. We must reseat this one too. */
1462 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1465 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1469 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1470 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1471 MAPPINGS is an assoc list of binfos that have already been reseated.
1472 Adjust any non-virtual bases within FROM, and also move any virtual bases
1473 which are canonical. This complication arises because selecting primary
1474 bases walks in inheritance graph order, but we don't share binfos for
1475 virtual bases, hence we can fill in the primaries for a virtual base,
1476 and then discover that a later base requires the virtual as its
1480 force_canonical_binfo (to, from, type, mappings)
1486 tree assoc = purpose_member (BINFO_TYPE (to),
1487 CLASSTYPE_VBASECLASSES (type));
1488 if (TREE_VALUE (assoc) != to)
1490 TREE_VALUE (assoc) = to;
1491 force_canonical_binfo_r (to, from, type, mappings);
1495 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1496 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1497 otherwise (because something else has already made it primary). */
1500 mark_primary_virtual_base (base_binfo, type)
1504 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1506 if (BINFO_PRIMARY_P (shared_binfo))
1508 /* It's already allocated in the hierarchy. BINFO won't have a
1509 primary base in this hierarchy, even though the complete object
1510 BINFO is for, would do. */
1514 /* We need to make sure that the assoc list
1515 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1516 primary BINFO for the virtual base, as this is the one
1517 that'll really exist. */
1518 if (base_binfo != shared_binfo)
1519 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1524 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1525 base, then BINFO has no primary base in this graph. Called from
1526 mark_primary_bases. DATA is the most derived type. */
1528 static tree dfs_unshared_virtual_bases (binfo, data)
1532 tree t = (tree) data;
1534 if (!BINFO_UNSHARED_MARKED (binfo)
1535 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1537 /* This morally virtual base has a primary base when it
1538 is a complete object. We need to locate the shared instance
1539 of this binfo in the type dominated by T. We duplicate the
1540 primary base information from there to here. */
1544 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1545 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1547 unshared_base = get_original_base (binfo,
1548 binfo_for_vbase (BINFO_TYPE (vbase),
1550 my_friendly_assert (unshared_base != binfo, 20010612);
1551 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1552 if (!BINFO_LOST_PRIMARY_P (binfo))
1553 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1556 if (binfo != TYPE_BINFO (t))
1557 /* The vtable fields will have been copied when duplicating the
1558 base binfos. That information is bogus, make sure we don't try
1560 BINFO_VTABLE (binfo) = NULL_TREE;
1562 /* If this is a virtual primary base, make sure its offset matches
1563 that which it is primary for. */
1564 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1565 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1567 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1568 BINFO_OFFSET (binfo));
1569 if (!integer_zerop (delta))
1570 propagate_binfo_offsets (binfo, delta, t);
1573 BINFO_UNSHARED_MARKED (binfo) = 0;
1577 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1578 dominated by TYPE that are primary bases. */
1581 mark_primary_bases (type)
1586 /* Walk the bases in inheritance graph order. */
1587 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1591 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1592 /* Not a dynamic base. */
1595 base_binfo = get_primary_binfo (binfo);
1597 if (TREE_VIA_VIRTUAL (base_binfo))
1598 base_binfo = mark_primary_virtual_base (base_binfo, type);
1601 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1603 BINFO_LOST_PRIMARY_P (binfo) = 1;
1605 BINFO_UNSHARED_MARKED (binfo) = 1;
1607 /* There could remain unshared morally virtual bases which were not
1608 visited in the inheritance graph walk. These bases will have lost
1609 their virtual primary base (should they have one). We must now
1610 find them. Also we must fix up the BINFO_OFFSETs of primary
1611 virtual bases. We could not do that as we went along, as they
1612 were originally copied from the bases we inherited from by
1613 unshare_base_binfos. That may have decided differently about
1614 where a virtual primary base went. */
1615 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1618 /* Make the BINFO the primary base of T. */
1621 set_primary_base (t, binfo, vfuns_p)
1628 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1629 basetype = BINFO_TYPE (binfo);
1630 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1631 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1632 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1633 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1634 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1637 /* Determine the primary class for T. */
1640 determine_primary_base (t, vfuns_p)
1644 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1648 /* If there are no baseclasses, there is certainly no primary base. */
1649 if (n_baseclasses == 0)
1652 type_binfo = TYPE_BINFO (t);
1654 for (i = 0; i < n_baseclasses; i++)
1656 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1657 tree basetype = BINFO_TYPE (base_binfo);
1659 if (TYPE_CONTAINS_VPTR_P (basetype))
1661 /* Even a virtual baseclass can contain our RTTI
1662 information. But, we prefer a non-virtual polymorphic
1664 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1665 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1667 /* We prefer a non-virtual base, although a virtual one will
1669 if (TREE_VIA_VIRTUAL (base_binfo))
1672 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1674 set_primary_base (t, base_binfo, vfuns_p);
1675 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1681 /* Only add unique vfields, and flatten them out as we go. */
1682 for (vfields = CLASSTYPE_VFIELDS (basetype);
1684 vfields = TREE_CHAIN (vfields))
1685 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1686 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1687 CLASSTYPE_VFIELDS (t)
1688 = tree_cons (base_binfo,
1689 VF_BASETYPE_VALUE (vfields),
1690 CLASSTYPE_VFIELDS (t));
1695 if (!TYPE_VFIELD (t))
1696 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1698 /* Find the indirect primary bases - those virtual bases which are primary
1699 bases of something else in this hierarchy. */
1700 for (vbases = CLASSTYPE_VBASECLASSES (t);
1702 vbases = TREE_CHAIN (vbases))
1704 tree vbase_binfo = TREE_VALUE (vbases);
1706 /* See if this virtual base is an indirect primary base. To be so,
1707 it must be a primary base within the hierarchy of one of our
1709 for (i = 0; i < n_baseclasses; ++i)
1711 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1714 for (v = CLASSTYPE_VBASECLASSES (basetype);
1718 tree base_vbase = TREE_VALUE (v);
1720 if (BINFO_PRIMARY_P (base_vbase)
1721 && same_type_p (BINFO_TYPE (base_vbase),
1722 BINFO_TYPE (vbase_binfo)))
1724 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1729 /* If we've discovered that this virtual base is an indirect
1730 primary base, then we can move on to the next virtual
1732 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1737 /* A "nearly-empty" virtual base class can be the primary base
1738 class, if no non-virtual polymorphic base can be found. */
1739 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1741 /* If not NULL, this is the best primary base candidate we have
1743 tree candidate = NULL_TREE;
1746 /* Loop over the baseclasses. */
1747 for (base_binfo = TYPE_BINFO (t);
1749 base_binfo = TREE_CHAIN (base_binfo))
1751 tree basetype = BINFO_TYPE (base_binfo);
1753 if (TREE_VIA_VIRTUAL (base_binfo)
1754 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1756 /* If this is not an indirect primary base, then it's
1757 definitely our primary base. */
1758 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1760 candidate = base_binfo;
1764 /* If this is an indirect primary base, it still could be
1765 our primary base -- unless we later find there's another
1766 nearly-empty virtual base that isn't an indirect
1769 candidate = base_binfo;
1773 /* If we've got a primary base, use it. */
1776 set_primary_base (t, candidate, vfuns_p);
1777 CLASSTYPE_VFIELDS (t)
1778 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1782 /* Mark the primary base classes at this point. */
1783 mark_primary_bases (t);
1786 /* Set memoizing fields and bits of T (and its variants) for later
1790 finish_struct_bits (t)
1793 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1795 /* Fix up variants (if any). */
1796 tree variants = TYPE_NEXT_VARIANT (t);
1799 /* These fields are in the _TYPE part of the node, not in
1800 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1801 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1802 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1803 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1804 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1805 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1807 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1808 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1809 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1810 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1811 /* Copy whatever these are holding today. */
1812 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1813 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1814 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1815 TYPE_SIZE (variants) = TYPE_SIZE (t);
1816 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1817 variants = TYPE_NEXT_VARIANT (variants);
1820 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1821 /* For a class w/o baseclasses, `finish_struct' has set
1822 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1823 definition). Similarly for a class whose base classes do not
1824 have vtables. When neither of these is true, we might have
1825 removed abstract virtuals (by providing a definition), added
1826 some (by declaring new ones), or redeclared ones from a base
1827 class. We need to recalculate what's really an abstract virtual
1828 at this point (by looking in the vtables). */
1829 get_pure_virtuals (t);
1833 /* Notice whether this class has type conversion functions defined. */
1834 tree binfo = TYPE_BINFO (t);
1835 tree binfos = BINFO_BASETYPES (binfo);
1838 for (i = n_baseclasses-1; i >= 0; i--)
1840 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1842 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1846 /* If this type has a copy constructor or a destructor, force its mode to
1847 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1848 will cause it to be passed by invisible reference and prevent it from
1849 being returned in a register. */
1850 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1853 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1854 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1856 TYPE_MODE (variants) = BLKmode;
1857 TREE_ADDRESSABLE (variants) = 1;
1862 /* Issue warnings about T having private constructors, but no friends,
1865 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1866 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1867 non-private static member functions. */
1870 maybe_warn_about_overly_private_class (t)
1873 int has_member_fn = 0;
1874 int has_nonprivate_method = 0;
1877 if (!warn_ctor_dtor_privacy
1878 /* If the class has friends, those entities might create and
1879 access instances, so we should not warn. */
1880 || (CLASSTYPE_FRIEND_CLASSES (t)
1881 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1882 /* We will have warned when the template was declared; there's
1883 no need to warn on every instantiation. */
1884 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1885 /* There's no reason to even consider warning about this
1889 /* We only issue one warning, if more than one applies, because
1890 otherwise, on code like:
1893 // Oops - forgot `public:'
1899 we warn several times about essentially the same problem. */
1901 /* Check to see if all (non-constructor, non-destructor) member
1902 functions are private. (Since there are no friends or
1903 non-private statics, we can't ever call any of the private member
1905 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1906 /* We're not interested in compiler-generated methods; they don't
1907 provide any way to call private members. */
1908 if (!DECL_ARTIFICIAL (fn))
1910 if (!TREE_PRIVATE (fn))
1912 if (DECL_STATIC_FUNCTION_P (fn))
1913 /* A non-private static member function is just like a
1914 friend; it can create and invoke private member
1915 functions, and be accessed without a class
1919 has_nonprivate_method = 1;
1922 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1926 if (!has_nonprivate_method && has_member_fn)
1928 /* There are no non-private methods, and there's at least one
1929 private member function that isn't a constructor or
1930 destructor. (If all the private members are
1931 constructors/destructors we want to use the code below that
1932 issues error messages specifically referring to
1933 constructors/destructors.) */
1935 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1936 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1937 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1938 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1940 has_nonprivate_method = 1;
1943 if (!has_nonprivate_method)
1945 warning ("all member functions in class `%T' are private", t);
1950 /* Even if some of the member functions are non-private, the class
1951 won't be useful for much if all the constructors or destructors
1952 are private: such an object can never be created or destroyed. */
1953 if (TYPE_HAS_DESTRUCTOR (t))
1955 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1957 if (TREE_PRIVATE (dtor))
1959 warning ("`%#T' only defines a private destructor and has no friends",
1965 if (TYPE_HAS_CONSTRUCTOR (t))
1967 int nonprivate_ctor = 0;
1969 /* If a non-template class does not define a copy
1970 constructor, one is defined for it, enabling it to avoid
1971 this warning. For a template class, this does not
1972 happen, and so we would normally get a warning on:
1974 template <class T> class C { private: C(); };
1976 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1977 complete non-template or fully instantiated classes have this
1979 if (!TYPE_HAS_INIT_REF (t))
1980 nonprivate_ctor = 1;
1982 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1986 tree ctor = OVL_CURRENT (fn);
1987 /* Ideally, we wouldn't count copy constructors (or, in
1988 fact, any constructor that takes an argument of the
1989 class type as a parameter) because such things cannot
1990 be used to construct an instance of the class unless
1991 you already have one. But, for now at least, we're
1993 if (! TREE_PRIVATE (ctor))
1995 nonprivate_ctor = 1;
2000 if (nonprivate_ctor == 0)
2002 warning ("`%#T' only defines private constructors and has no friends",
2009 /* Function to help qsort sort FIELD_DECLs by name order. */
2012 field_decl_cmp (x, y)
2015 if (DECL_NAME (*x) == DECL_NAME (*y))
2016 /* A nontype is "greater" than a type. */
2017 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2018 if (DECL_NAME (*x) == NULL_TREE)
2020 if (DECL_NAME (*y) == NULL_TREE)
2022 if (DECL_NAME (*x) < DECL_NAME (*y))
2027 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2030 method_name_cmp (m1, m2)
2031 const tree *m1, *m2;
2033 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2035 if (*m1 == NULL_TREE)
2037 if (*m2 == NULL_TREE)
2039 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2044 /* Warn about duplicate methods in fn_fields. Also compact method
2045 lists so that lookup can be made faster.
2047 Data Structure: List of method lists. The outer list is a
2048 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2049 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2050 links the entire list of methods for TYPE_METHODS. Friends are
2051 chained in the same way as member functions (? TREE_CHAIN or
2052 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2053 list. That allows them to be quickly deleted, and requires no
2056 Sort methods that are not special (i.e., constructors, destructors,
2057 and type conversion operators) so that we can find them faster in
2061 finish_struct_methods (t)
2068 if (!TYPE_METHODS (t))
2070 /* Clear these for safety; perhaps some parsing error could set
2071 these incorrectly. */
2072 TYPE_HAS_CONSTRUCTOR (t) = 0;
2073 TYPE_HAS_DESTRUCTOR (t) = 0;
2074 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2078 method_vec = CLASSTYPE_METHOD_VEC (t);
2079 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2080 len = TREE_VEC_LENGTH (method_vec);
2082 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2083 and the next few with type conversion operators (if any). */
2084 for (fn_fields = TYPE_METHODS (t); fn_fields;
2085 fn_fields = TREE_CHAIN (fn_fields))
2086 /* Clear out this flag. */
2087 DECL_IN_AGGR_P (fn_fields) = 0;
2089 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2090 /* We thought there was a destructor, but there wasn't. Some
2091 parse errors cause this anomalous situation. */
2092 TYPE_HAS_DESTRUCTOR (t) = 0;
2094 /* Issue warnings about private constructors and such. If there are
2095 no methods, then some public defaults are generated. */
2096 maybe_warn_about_overly_private_class (t);
2098 /* Now sort the methods. */
2099 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2101 TREE_VEC_LENGTH (method_vec) = len;
2103 /* The type conversion ops have to live at the front of the vec, so we
2105 for (slot = 2; slot < len; ++slot)
2107 tree fn = TREE_VEC_ELT (method_vec, slot);
2109 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2113 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2114 (int (*)(const void *, const void *))method_name_cmp);
2117 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2120 duplicate_tag_error (t)
2123 error ("redefinition of `%#T'", t);
2124 cp_error_at ("previous definition of `%#T'", t);
2126 /* Pretend we haven't defined this type. */
2128 /* All of the component_decl's were TREE_CHAINed together in the parser.
2129 finish_struct_methods walks these chains and assembles all methods with
2130 the same base name into DECL_CHAINs. Now we don't need the parser chains
2131 anymore, so we unravel them. */
2133 /* This used to be in finish_struct, but it turns out that the
2134 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2136 if (CLASSTYPE_METHOD_VEC (t))
2138 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2139 int i, len = TREE_VEC_LENGTH (method_vec);
2140 for (i = 0; i < len; i++)
2142 tree unchain = TREE_VEC_ELT (method_vec, i);
2143 while (unchain != NULL_TREE)
2145 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2146 unchain = OVL_NEXT (unchain);
2151 if (TYPE_LANG_SPECIFIC (t))
2153 tree binfo = TYPE_BINFO (t);
2154 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2155 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2156 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2157 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2159 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2160 BINFO_BASETYPES(binfo) = NULL_TREE;
2162 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2163 TYPE_BINFO (t) = binfo;
2164 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2165 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2166 TYPE_REDEFINED (t) = 1;
2167 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2168 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2170 TYPE_SIZE (t) = NULL_TREE;
2171 TYPE_MODE (t) = VOIDmode;
2172 TYPE_FIELDS (t) = NULL_TREE;
2173 TYPE_METHODS (t) = NULL_TREE;
2174 TYPE_VFIELD (t) = NULL_TREE;
2175 TYPE_CONTEXT (t) = NULL_TREE;
2177 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2178 TYPE_LANG_FLAG_0 (t) = 0;
2179 TYPE_LANG_FLAG_1 (t) = 0;
2180 TYPE_LANG_FLAG_2 (t) = 0;
2181 TYPE_LANG_FLAG_3 (t) = 0;
2182 TYPE_LANG_FLAG_4 (t) = 0;
2183 TYPE_LANG_FLAG_5 (t) = 0;
2184 TYPE_LANG_FLAG_6 (t) = 0;
2185 /* But not this one. */
2186 SET_IS_AGGR_TYPE (t, 1);
2189 /* Make BINFO's vtable have N entries, including RTTI entries,
2190 vbase and vcall offsets, etc. Set its type and call the backend
2194 layout_vtable_decl (binfo, n)
2201 atype = build_cplus_array_type (vtable_entry_type,
2202 build_index_type (size_int (n - 1)));
2203 layout_type (atype);
2205 /* We may have to grow the vtable. */
2206 vtable = get_vtbl_decl_for_binfo (binfo);
2207 if (!same_type_p (TREE_TYPE (vtable), atype))
2209 TREE_TYPE (vtable) = atype;
2210 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2211 layout_decl (vtable, 0);
2213 /* At one time the vtable info was grabbed 2 words at a time. This
2214 fails on Sparc unless you have 8-byte alignment. */
2215 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2216 DECL_ALIGN (vtable));
2220 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2221 have the same signature. */
2224 same_signature_p (fndecl, base_fndecl)
2225 tree fndecl, base_fndecl;
2227 /* One destructor overrides another if they are the same kind of
2229 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2230 && special_function_p (base_fndecl) == special_function_p (fndecl))
2232 /* But a non-destructor never overrides a destructor, nor vice
2233 versa, nor do different kinds of destructors override
2234 one-another. For example, a complete object destructor does not
2235 override a deleting destructor. */
2236 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2239 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2241 tree types, base_types;
2242 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2243 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2244 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2245 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2246 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2252 typedef struct find_final_overrider_data_s {
2253 /* The function for which we are trying to find a final overrider. */
2255 /* The base class in which the function was declared. */
2256 tree declaring_base;
2257 /* The most derived class in the hierarchy. */
2258 tree most_derived_type;
2259 /* The final overriding function. */
2261 /* The functions that we thought might be final overriders, but
2264 /* The BINFO for the class in which the final overriding function
2266 tree overriding_base;
2267 } find_final_overrider_data;
2269 /* Called from find_final_overrider via dfs_walk. */
2272 dfs_find_final_overrider (binfo, data)
2276 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2278 if (same_type_p (BINFO_TYPE (binfo),
2279 BINFO_TYPE (ffod->declaring_base))
2280 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2281 BINFO_OFFSET (ffod->declaring_base)))
2286 /* We haven't found an overrider yet. */
2288 /* We've found a path to the declaring base. Walk down the path
2289 looking for an overrider for FN. */
2290 for (path = reverse_path (binfo);
2292 path = TREE_CHAIN (path))
2294 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2300 /* If we found an overrider, record the overriding function, and
2301 the base from which it came. */
2306 /* Assume the path is non-virtual. See if there are any
2307 virtual bases from (but not including) the overrider up
2308 to and including the base where the function is
2310 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2311 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2313 base = ffod->declaring_base;
2317 /* If we didn't already have an overrider, or any
2318 candidates, then this function is the best candidate so
2320 if (!ffod->overriding_fn && !ffod->candidates)
2322 ffod->overriding_fn = method;
2323 ffod->overriding_base = TREE_VALUE (path);
2325 else if (ffod->overriding_fn)
2327 /* We had a best overrider; let's see how this compares. */
2329 if (ffod->overriding_fn == method
2330 && (tree_int_cst_equal
2331 (BINFO_OFFSET (TREE_VALUE (path)),
2332 BINFO_OFFSET (ffod->overriding_base))))
2333 /* We found the same overrider we already have, and in the
2334 same place; it's still the best. */;
2335 else if (strictly_overrides (ffod->overriding_fn, method))
2336 /* The old function overrides this function; it's still the
2338 else if (strictly_overrides (method, ffod->overriding_fn))
2340 /* The new function overrides the old; it's now the
2342 ffod->overriding_fn = method;
2343 ffod->overriding_base = TREE_VALUE (path);
2349 = build_tree_list (NULL_TREE,
2350 ffod->overriding_fn);
2351 if (method != ffod->overriding_fn)
2353 = tree_cons (NULL_TREE, method, ffod->candidates);
2354 ffod->overriding_fn = NULL_TREE;
2355 ffod->overriding_base = NULL_TREE;
2360 /* We had a list of ambiguous overrides; let's see how this
2361 new one compares. */
2364 bool incomparable = false;
2366 /* If there were previous candidates, and this function
2367 overrides all of them, then it is the new best
2369 for (candidates = ffod->candidates;
2371 candidates = TREE_CHAIN (candidates))
2373 /* If the candidate overrides the METHOD, then we
2374 needn't worry about it any further. */
2375 if (strictly_overrides (TREE_VALUE (candidates),
2382 /* If the METHOD doesn't override the candidate,
2383 then it is incomporable. */
2384 if (!strictly_overrides (method,
2385 TREE_VALUE (candidates)))
2386 incomparable = true;
2389 /* If METHOD overrode all the candidates, then it is the
2390 new best candidate. */
2391 if (!candidates && !incomparable)
2393 ffod->overriding_fn = method;
2394 ffod->overriding_base = TREE_VALUE (path);
2395 ffod->candidates = NULL_TREE;
2397 /* If METHOD didn't override all the candidates, then it
2398 is another candidate. */
2399 else if (method && incomparable)
2401 = tree_cons (NULL_TREE, method, ffod->candidates);
2409 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2410 FN and whose TREE_VALUE is the binfo for the base where the
2411 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2412 base object in which FN is declared. */
2415 find_final_overrider (t, binfo, fn)
2420 find_final_overrider_data ffod;
2422 /* Getting this right is a little tricky. This is legal:
2424 struct S { virtual void f (); };
2425 struct T { virtual void f (); };
2426 struct U : public S, public T { };
2428 even though calling `f' in `U' is ambiguous. But,
2430 struct R { virtual void f(); };
2431 struct S : virtual public R { virtual void f (); };
2432 struct T : virtual public R { virtual void f (); };
2433 struct U : public S, public T { };
2435 is not -- there's no way to decide whether to put `S::f' or
2436 `T::f' in the vtable for `R'.
2438 The solution is to look at all paths to BINFO. If we find
2439 different overriders along any two, then there is a problem. */
2441 ffod.declaring_base = binfo;
2442 ffod.most_derived_type = t;
2443 ffod.overriding_fn = NULL_TREE;
2444 ffod.overriding_base = NULL_TREE;
2445 ffod.candidates = NULL_TREE;
2447 dfs_walk (TYPE_BINFO (t),
2448 dfs_find_final_overrider,
2452 /* If there was no winner, issue an error message. */
2453 if (!ffod.overriding_fn)
2455 error ("no unique final overrider for `%D' in `%T'", fn, t);
2456 return error_mark_node;
2459 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2462 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2463 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2464 the function that the slot in T's primary vtable points to. */
2466 static tree get_matching_virtual PARAMS ((tree, tree));
2468 get_matching_virtual (t, fn)
2473 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2474 if (same_signature_p (BV_FN (f), fn))
2479 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2480 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2481 corresponding position in the BINFO_VIRTUALS list. */
2484 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2497 /* Find the nearest primary base (possibly binfo itself) which defines
2498 this function; this is the class the caller will convert to when
2499 calling FN through BINFO. */
2500 for (b = binfo; ; b = get_primary_binfo (b))
2502 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2505 /* The nearest definition is from a lost primary. */
2506 if (BINFO_LOST_PRIMARY_P (b))
2511 /* Find the final overrider. */
2512 overrider = find_final_overrider (t, b, fn);
2513 if (overrider == error_mark_node)
2516 /* Check for unsupported covariant returns again now that we've
2517 calculated the base offsets. */
2518 check_final_overrider (TREE_PURPOSE (overrider), fn);
2520 /* Assume that we will produce a thunk that convert all the way to
2521 the final overrider, and not to an intermediate virtual base. */
2522 virtual_base = NULL_TREE;
2524 /* See if we can convert to an intermediate virtual base first, and then
2525 use the vcall offset located there to finish the conversion. */
2526 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2528 /* If we find the final overrider, then we can stop
2530 if (same_type_p (BINFO_TYPE (b),
2531 BINFO_TYPE (TREE_VALUE (overrider))))
2534 /* If we find a virtual base, and we haven't yet found the
2535 overrider, then there is a virtual base between the
2536 declaring base (first_defn) and the final overrider. */
2537 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2541 /* Compute the constant adjustment to the `this' pointer. The
2542 `this' pointer, when this function is called, will point at BINFO
2543 (or one of its primary bases, which are at the same offset). */
2546 /* The `this' pointer needs to be adjusted from the declaration to
2547 the nearest virtual base. */
2548 delta = size_diffop (BINFO_OFFSET (virtual_base),
2549 BINFO_OFFSET (first_defn));
2551 /* If the nearest definition is in a lost primary, we don't need an
2552 entry in our vtable. Except possibly in a constructor vtable,
2553 if we happen to get our primary back. In that case, the offset
2554 will be zero, as it will be a primary base. */
2555 delta = size_zero_node;
2558 /* The `this' pointer needs to be adjusted from pointing to
2559 BINFO to pointing at the base where the final overrider
2561 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2562 BINFO_OFFSET (binfo));
2564 if (! integer_zerop (delta))
2566 /* We'll need a thunk. But if we have a (perhaps formerly)
2567 primary virtual base, we have a vcall slot for this function,
2568 so we can use it rather than create a non-virtual thunk. */
2570 b = get_primary_binfo (first_defn);
2571 for (; b; b = get_primary_binfo (b))
2573 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2575 /* b doesn't have this function; no suitable vbase. */
2577 if (TREE_VIA_VIRTUAL (b))
2579 /* Found one; we can treat ourselves as a virtual base. */
2580 virtual_base = binfo;
2581 delta = size_zero_node;
2588 modify_vtable_entry (t,
2590 TREE_PURPOSE (overrider),
2595 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2598 /* Called from modify_all_vtables via dfs_walk. */
2601 dfs_modify_vtables (binfo, data)
2605 if (/* There's no need to modify the vtable for a non-virtual
2606 primary base; we're not going to use that vtable anyhow.
2607 We do still need to do this for virtual primary bases, as they
2608 could become non-primary in a construction vtable. */
2609 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2610 /* Similarly, a base without a vtable needs no modification. */
2611 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2619 make_new_vtable (t, binfo);
2621 /* Now, go through each of the virtual functions in the virtual
2622 function table for BINFO. Find the final overrider, and
2623 update the BINFO_VIRTUALS list appropriately. */
2624 for (virtuals = BINFO_VIRTUALS (binfo),
2625 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2627 virtuals = TREE_CHAIN (virtuals),
2628 old_virtuals = TREE_CHAIN (old_virtuals))
2629 update_vtable_entry_for_fn (t,
2631 BV_FN (old_virtuals),
2635 SET_BINFO_MARKED (binfo);
2640 /* Update all of the primary and secondary vtables for T. Create new
2641 vtables as required, and initialize their RTTI information. Each
2642 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2643 function from a base class; find and modify the appropriate entries
2644 to point to the overriding functions. Returns a list, in
2645 declaration order, of the functions that are overridden in this
2646 class, but do not appear in the primary base class vtable, and
2647 which should therefore be appended to the end of the vtable for T. */
2650 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2653 tree overridden_virtuals;
2655 tree binfo = TYPE_BINFO (t);
2658 /* Update all of the vtables. */
2661 dfs_unmarked_real_bases_queue_p,
2663 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2665 /* Include overriding functions for secondary vtables in our primary
2667 for (fnsp = &overridden_virtuals; *fnsp; )
2669 tree fn = TREE_VALUE (*fnsp);
2671 if (!BINFO_VIRTUALS (binfo)
2672 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2674 /* Set the vtable index. */
2675 set_vindex (fn, vfuns_p);
2676 /* We don't need to convert to a base class when calling
2678 DECL_VIRTUAL_CONTEXT (fn) = t;
2680 /* We don't need to adjust the `this' pointer when
2681 calling this function. */
2682 BV_DELTA (*fnsp) = integer_zero_node;
2683 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2685 /* This is an overridden function not already in our
2687 fnsp = &TREE_CHAIN (*fnsp);
2690 /* We've already got an entry for this function. Skip it. */
2691 *fnsp = TREE_CHAIN (*fnsp);
2694 return overridden_virtuals;
2697 /* Here, we already know that they match in every respect.
2698 All we have to check is where they had their declarations.
2700 Return non-zero iff FNDECL1 is declared in a class which has a
2701 proper base class containing FNDECL2. We don't care about
2702 ambiguity or accessibility. */
2705 strictly_overrides (fndecl1, fndecl2)
2706 tree fndecl1, fndecl2;
2710 return (lookup_base (DECL_CONTEXT (fndecl1), DECL_CONTEXT (fndecl2),
2711 ba_ignore | ba_quiet, &kind)
2712 && kind != bk_same_type);
2715 /* Get the base virtual function declarations in T that have the
2719 get_basefndecls (name, t)
2723 tree base_fndecls = NULL_TREE;
2724 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2727 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2728 if (TREE_CODE (methods) == FUNCTION_DECL
2729 && DECL_VINDEX (methods) != NULL_TREE
2730 && DECL_NAME (methods) == name)
2731 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2734 return base_fndecls;
2736 for (i = 0; i < n_baseclasses; i++)
2738 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2739 base_fndecls = chainon (get_basefndecls (name, basetype),
2743 return base_fndecls;
2746 /* If this declaration supersedes the declaration of
2747 a method declared virtual in the base class, then
2748 mark this field as being virtual as well. */
2751 check_for_override (decl, ctype)
2754 if (TREE_CODE (decl) == TEMPLATE_DECL)
2755 /* In [temp.mem] we have:
2757 A specialization of a member function template does not
2758 override a virtual function from a base class. */
2760 if ((DECL_DESTRUCTOR_P (decl)
2761 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2762 && look_for_overrides (ctype, decl)
2763 && !DECL_STATIC_FUNCTION_P (decl))
2765 /* Set DECL_VINDEX to a value that is neither an
2766 INTEGER_CST nor the error_mark_node so that
2767 add_virtual_function will realize this is an
2768 overriding function. */
2769 DECL_VINDEX (decl) = decl;
2771 if (DECL_VIRTUAL_P (decl))
2773 if (DECL_VINDEX (decl) == NULL_TREE)
2774 DECL_VINDEX (decl) = error_mark_node;
2775 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2779 /* Warn about hidden virtual functions that are not overridden in t.
2780 We know that constructors and destructors don't apply. */
2786 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2787 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2790 /* We go through each separately named virtual function. */
2791 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2799 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2800 have the same name. Figure out what name that is. */
2801 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2802 /* There are no possibly hidden functions yet. */
2803 base_fndecls = NULL_TREE;
2804 /* Iterate through all of the base classes looking for possibly
2805 hidden functions. */
2806 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2808 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2809 base_fndecls = chainon (get_basefndecls (name, basetype),
2813 /* If there are no functions to hide, continue. */
2817 /* Remove any overridden functions. */
2818 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2820 fndecl = OVL_CURRENT (fns);
2821 if (DECL_VINDEX (fndecl))
2823 tree *prev = &base_fndecls;
2826 /* If the method from the base class has the same
2827 signature as the method from the derived class, it
2828 has been overridden. */
2829 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2830 *prev = TREE_CHAIN (*prev);
2832 prev = &TREE_CHAIN (*prev);
2836 /* Now give a warning for all base functions without overriders,
2837 as they are hidden. */
2838 while (base_fndecls)
2840 /* Here we know it is a hider, and no overrider exists. */
2841 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2842 cp_warning_at (" by `%D'",
2843 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2844 base_fndecls = TREE_CHAIN (base_fndecls);
2849 /* Check for things that are invalid. There are probably plenty of other
2850 things we should check for also. */
2853 finish_struct_anon (t)
2858 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2860 if (TREE_STATIC (field))
2862 if (TREE_CODE (field) != FIELD_DECL)
2865 if (DECL_NAME (field) == NULL_TREE
2866 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2868 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2869 for (; elt; elt = TREE_CHAIN (elt))
2871 /* We're generally only interested in entities the user
2872 declared, but we also find nested classes by noticing
2873 the TYPE_DECL that we create implicitly. You're
2874 allowed to put one anonymous union inside another,
2875 though, so we explicitly tolerate that. We use
2876 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2877 we also allow unnamed types used for defining fields. */
2878 if (DECL_ARTIFICIAL (elt)
2879 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2880 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2883 if (DECL_NAME (elt) == constructor_name (t))
2884 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2887 if (TREE_CODE (elt) != FIELD_DECL)
2889 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2894 if (TREE_PRIVATE (elt))
2895 cp_pedwarn_at ("private member `%#D' in anonymous union",
2897 else if (TREE_PROTECTED (elt))
2898 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2901 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2902 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2908 /* Create default constructors, assignment operators, and so forth for
2909 the type indicated by T, if they are needed.
2910 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2911 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2912 class cannot have a default constructor, copy constructor taking a
2913 const reference argument, or an assignment operator taking a const
2914 reference, respectively. If a virtual destructor is created, its
2915 DECL is returned; otherwise the return value is NULL_TREE. */
2918 add_implicitly_declared_members (t, cant_have_default_ctor,
2919 cant_have_const_cctor,
2920 cant_have_const_assignment)
2922 int cant_have_default_ctor;
2923 int cant_have_const_cctor;
2924 int cant_have_const_assignment;
2927 tree implicit_fns = NULL_TREE;
2928 tree virtual_dtor = NULL_TREE;
2931 ++adding_implicit_members;
2934 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2936 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2937 check_for_override (default_fn, t);
2939 /* If we couldn't make it work, then pretend we didn't need it. */
2940 if (default_fn == void_type_node)
2941 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2944 TREE_CHAIN (default_fn) = implicit_fns;
2945 implicit_fns = default_fn;
2947 if (DECL_VINDEX (default_fn))
2948 virtual_dtor = default_fn;
2952 /* Any non-implicit destructor is non-trivial. */
2953 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2955 /* Default constructor. */
2956 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2958 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2959 TREE_CHAIN (default_fn) = implicit_fns;
2960 implicit_fns = default_fn;
2963 /* Copy constructor. */
2964 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2966 /* ARM 12.18: You get either X(X&) or X(const X&), but
2969 = implicitly_declare_fn (sfk_copy_constructor, t,
2970 /*const_p=*/!cant_have_const_cctor);
2971 TREE_CHAIN (default_fn) = implicit_fns;
2972 implicit_fns = default_fn;
2975 /* Assignment operator. */
2976 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2979 = implicitly_declare_fn (sfk_assignment_operator, t,
2980 /*const_p=*/!cant_have_const_assignment);
2981 TREE_CHAIN (default_fn) = implicit_fns;
2982 implicit_fns = default_fn;
2985 /* Now, hook all of the new functions on to TYPE_METHODS,
2986 and add them to the CLASSTYPE_METHOD_VEC. */
2987 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2988 add_method (t, *f, /*error_p=*/0);
2989 *f = TYPE_METHODS (t);
2990 TYPE_METHODS (t) = implicit_fns;
2992 --adding_implicit_members;
2994 return virtual_dtor;
2997 /* Subroutine of finish_struct_1. Recursively count the number of fields
2998 in TYPE, including anonymous union members. */
3001 count_fields (fields)
3006 for (x = fields; x; x = TREE_CHAIN (x))
3008 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3009 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3016 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3017 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3020 add_fields_to_vec (fields, field_vec, idx)
3021 tree fields, field_vec;
3025 for (x = fields; x; x = TREE_CHAIN (x))
3027 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3028 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3030 TREE_VEC_ELT (field_vec, idx++) = x;
3035 /* FIELD is a bit-field. We are finishing the processing for its
3036 enclosing type. Issue any appropriate messages and set appropriate
3040 check_bitfield_decl (field)
3043 tree type = TREE_TYPE (field);
3046 /* Detect invalid bit-field type. */
3047 if (DECL_INITIAL (field)
3048 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3050 cp_error_at ("bit-field `%#D' with non-integral type", field);
3051 w = error_mark_node;
3054 /* Detect and ignore out of range field width. */
3055 if (DECL_INITIAL (field))
3057 w = DECL_INITIAL (field);
3059 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3062 /* detect invalid field size. */
3063 if (TREE_CODE (w) == CONST_DECL)
3064 w = DECL_INITIAL (w);
3066 w = decl_constant_value (w);
3068 if (TREE_CODE (w) != INTEGER_CST)
3070 cp_error_at ("bit-field `%D' width not an integer constant",
3072 w = error_mark_node;
3074 else if (tree_int_cst_sgn (w) < 0)
3076 cp_error_at ("negative width in bit-field `%D'", field);
3077 w = error_mark_node;
3079 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3081 cp_error_at ("zero width for bit-field `%D'", field);
3082 w = error_mark_node;
3084 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3085 && TREE_CODE (type) != ENUMERAL_TYPE
3086 && TREE_CODE (type) != BOOLEAN_TYPE)
3087 cp_warning_at ("width of `%D' exceeds its type", field);
3088 else if (TREE_CODE (type) == ENUMERAL_TYPE
3089 && (0 > compare_tree_int (w,
3090 min_precision (TYPE_MIN_VALUE (type),
3091 TREE_UNSIGNED (type)))
3092 || 0 > compare_tree_int (w,
3094 (TYPE_MAX_VALUE (type),
3095 TREE_UNSIGNED (type)))))
3096 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3100 /* Remove the bit-field width indicator so that the rest of the
3101 compiler does not treat that value as an initializer. */
3102 DECL_INITIAL (field) = NULL_TREE;
3104 if (w != error_mark_node)
3106 DECL_SIZE (field) = convert (bitsizetype, w);
3107 DECL_BIT_FIELD (field) = 1;
3109 if (integer_zerop (w)
3110 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
3112 #ifdef EMPTY_FIELD_BOUNDARY
3113 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3114 EMPTY_FIELD_BOUNDARY);
3116 #ifdef PCC_BITFIELD_TYPE_MATTERS
3117 if (PCC_BITFIELD_TYPE_MATTERS)
3119 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3121 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3128 /* Non-bit-fields are aligned for their type. */
3129 DECL_BIT_FIELD (field) = 0;
3130 CLEAR_DECL_C_BIT_FIELD (field);
3131 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3132 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3136 /* FIELD is a non bit-field. We are finishing the processing for its
3137 enclosing type T. Issue any appropriate messages and set appropriate
3141 check_field_decl (field, t, cant_have_const_ctor,
3142 cant_have_default_ctor, no_const_asn_ref,
3143 any_default_members)
3146 int *cant_have_const_ctor;
3147 int *cant_have_default_ctor;
3148 int *no_const_asn_ref;
3149 int *any_default_members;
3151 tree type = strip_array_types (TREE_TYPE (field));
3153 /* An anonymous union cannot contain any fields which would change
3154 the settings of CANT_HAVE_CONST_CTOR and friends. */
3155 if (ANON_UNION_TYPE_P (type))
3157 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3158 structs. So, we recurse through their fields here. */
3159 else if (ANON_AGGR_TYPE_P (type))
3163 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3164 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3165 check_field_decl (fields, t, cant_have_const_ctor,
3166 cant_have_default_ctor, no_const_asn_ref,
3167 any_default_members);
3169 /* Check members with class type for constructors, destructors,
3171 else if (CLASS_TYPE_P (type))
3173 /* Never let anything with uninheritable virtuals
3174 make it through without complaint. */
3175 abstract_virtuals_error (field, type);
3177 if (TREE_CODE (t) == UNION_TYPE)
3179 if (TYPE_NEEDS_CONSTRUCTING (type))
3180 cp_error_at ("member `%#D' with constructor not allowed in union",
3182 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3183 cp_error_at ("member `%#D' with destructor not allowed in union",
3185 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3186 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3191 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3192 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3193 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3194 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3195 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3198 if (!TYPE_HAS_CONST_INIT_REF (type))
3199 *cant_have_const_ctor = 1;
3201 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3202 *no_const_asn_ref = 1;
3204 if (TYPE_HAS_CONSTRUCTOR (type)
3205 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3206 *cant_have_default_ctor = 1;
3208 if (DECL_INITIAL (field) != NULL_TREE)
3210 /* `build_class_init_list' does not recognize
3212 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3213 cp_error_at ("multiple fields in union `%T' initialized");
3214 *any_default_members = 1;
3217 /* Non-bit-fields are aligned for their type, except packed fields
3218 which require only BITS_PER_UNIT alignment. */
3219 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3220 (DECL_PACKED (field)
3222 : TYPE_ALIGN (TREE_TYPE (field))));
3223 if (! DECL_PACKED (field))
3224 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3227 /* Check the data members (both static and non-static), class-scoped
3228 typedefs, etc., appearing in the declaration of T. Issue
3229 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3230 declaration order) of access declarations; each TREE_VALUE in this
3231 list is a USING_DECL.
3233 In addition, set the following flags:
3236 The class is empty, i.e., contains no non-static data members.
3238 CANT_HAVE_DEFAULT_CTOR_P
3239 This class cannot have an implicitly generated default
3242 CANT_HAVE_CONST_CTOR_P
3243 This class cannot have an implicitly generated copy constructor
3244 taking a const reference.
3246 CANT_HAVE_CONST_ASN_REF
3247 This class cannot have an implicitly generated assignment
3248 operator taking a const reference.
3250 All of these flags should be initialized before calling this
3253 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3254 fields can be added by adding to this chain. */
3257 check_field_decls (t, access_decls, empty_p,
3258 cant_have_default_ctor_p, cant_have_const_ctor_p,
3263 int *cant_have_default_ctor_p;
3264 int *cant_have_const_ctor_p;
3265 int *no_const_asn_ref_p;
3270 int any_default_members;
3272 /* First, delete any duplicate fields. */
3273 delete_duplicate_fields (TYPE_FIELDS (t));
3275 /* Assume there are no access declarations. */
3276 *access_decls = NULL_TREE;
3277 /* Assume this class has no pointer members. */
3279 /* Assume none of the members of this class have default
3281 any_default_members = 0;
3283 for (field = &TYPE_FIELDS (t); *field; field = next)
3286 tree type = TREE_TYPE (x);
3288 next = &TREE_CHAIN (x);
3290 if (TREE_CODE (x) == FIELD_DECL)
3292 DECL_PACKED (x) |= TYPE_PACKED (t);
3294 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3295 /* We don't treat zero-width bitfields as making a class
3300 /* The class is non-empty. */
3302 /* The class is not even nearly empty. */
3303 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3307 if (TREE_CODE (x) == USING_DECL)
3309 /* Prune the access declaration from the list of fields. */
3310 *field = TREE_CHAIN (x);
3312 /* Save the access declarations for our caller. */
3313 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3315 /* Since we've reset *FIELD there's no reason to skip to the
3321 if (TREE_CODE (x) == TYPE_DECL
3322 || TREE_CODE (x) == TEMPLATE_DECL)
3325 /* If we've gotten this far, it's a data member, possibly static,
3326 or an enumerator. */
3328 DECL_CONTEXT (x) = t;
3330 /* ``A local class cannot have static data members.'' ARM 9.4 */
3331 if (current_function_decl && TREE_STATIC (x))
3332 cp_error_at ("field `%D' in local class cannot be static", x);
3334 /* Perform error checking that did not get done in
3336 if (TREE_CODE (type) == FUNCTION_TYPE)
3338 cp_error_at ("field `%D' invalidly declared function type",
3340 type = build_pointer_type (type);
3341 TREE_TYPE (x) = type;
3343 else if (TREE_CODE (type) == METHOD_TYPE)
3345 cp_error_at ("field `%D' invalidly declared method type", x);
3346 type = build_pointer_type (type);
3347 TREE_TYPE (x) = type;
3349 else if (TREE_CODE (type) == OFFSET_TYPE)
3351 cp_error_at ("field `%D' invalidly declared offset type", x);
3352 type = build_pointer_type (type);
3353 TREE_TYPE (x) = type;
3356 if (type == error_mark_node)
3359 /* When this goes into scope, it will be a non-local reference. */
3360 DECL_NONLOCAL (x) = 1;
3362 if (TREE_CODE (x) == CONST_DECL)
3365 if (TREE_CODE (x) == VAR_DECL)
3367 if (TREE_CODE (t) == UNION_TYPE)
3368 /* Unions cannot have static members. */
3369 cp_error_at ("field `%D' declared static in union", x);
3374 /* Now it can only be a FIELD_DECL. */
3376 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3377 CLASSTYPE_NON_AGGREGATE (t) = 1;
3379 /* If this is of reference type, check if it needs an init.
3380 Also do a little ANSI jig if necessary. */
3381 if (TREE_CODE (type) == REFERENCE_TYPE)
3383 CLASSTYPE_NON_POD_P (t) = 1;
3384 if (DECL_INITIAL (x) == NULL_TREE)
3385 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3387 /* ARM $12.6.2: [A member initializer list] (or, for an
3388 aggregate, initialization by a brace-enclosed list) is the
3389 only way to initialize nonstatic const and reference
3391 *cant_have_default_ctor_p = 1;
3392 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3394 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3395 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3398 type = strip_array_types (type);
3400 if (TREE_CODE (type) == POINTER_TYPE)
3403 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3404 CLASSTYPE_HAS_MUTABLE (t) = 1;
3406 if (! pod_type_p (type))
3407 /* DR 148 now allows pointers to members (which are POD themselves),
3408 to be allowed in POD structs. */
3409 CLASSTYPE_NON_POD_P (t) = 1;
3411 if (! zero_init_p (type))
3412 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3414 /* If any field is const, the structure type is pseudo-const. */
3415 if (CP_TYPE_CONST_P (type))
3417 C_TYPE_FIELDS_READONLY (t) = 1;
3418 if (DECL_INITIAL (x) == NULL_TREE)
3419 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3421 /* ARM $12.6.2: [A member initializer list] (or, for an
3422 aggregate, initialization by a brace-enclosed list) is the
3423 only way to initialize nonstatic const and reference
3425 *cant_have_default_ctor_p = 1;
3426 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3428 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3429 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3431 /* A field that is pseudo-const makes the structure likewise. */
3432 else if (IS_AGGR_TYPE (type))
3434 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3435 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3436 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3439 /* Core issue 80: A nonstatic data member is required to have a
3440 different name from the class iff the class has a
3441 user-defined constructor. */
3442 if (DECL_NAME (x) == constructor_name (t)
3443 && TYPE_HAS_CONSTRUCTOR (t))
3444 cp_pedwarn_at ("field `%#D' with same name as class", x);
3446 /* We set DECL_C_BIT_FIELD in grokbitfield.
3447 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3448 if (DECL_C_BIT_FIELD (x))
3449 check_bitfield_decl (x);
3451 check_field_decl (x, t,
3452 cant_have_const_ctor_p,
3453 cant_have_default_ctor_p,
3455 &any_default_members);
3458 /* Effective C++ rule 11. */
3459 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3460 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3462 warning ("`%#T' has pointer data members", t);
3464 if (! TYPE_HAS_INIT_REF (t))
3466 warning (" but does not override `%T(const %T&)'", t, t);
3467 if (! TYPE_HAS_ASSIGN_REF (t))
3468 warning (" or `operator=(const %T&)'", t);
3470 else if (! TYPE_HAS_ASSIGN_REF (t))
3471 warning (" but does not override `operator=(const %T&)'", t);
3475 /* Check anonymous struct/anonymous union fields. */
3476 finish_struct_anon (t);
3478 /* We've built up the list of access declarations in reverse order.
3480 *access_decls = nreverse (*access_decls);
3483 /* If TYPE is an empty class type, records its OFFSET in the table of
3487 record_subobject_offset (type, offset, offsets)
3494 if (!is_empty_class (type))
3497 /* Record the location of this empty object in OFFSETS. */
3498 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3500 n = splay_tree_insert (offsets,
3501 (splay_tree_key) offset,
3502 (splay_tree_value) NULL_TREE);
3503 n->value = ((splay_tree_value)
3504 tree_cons (NULL_TREE,
3511 /* Returns non-zero if TYPE is an empty class type and there is
3512 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3515 check_subobject_offset (type, offset, offsets)
3523 if (!is_empty_class (type))
3526 /* Record the location of this empty object in OFFSETS. */
3527 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3531 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3532 if (same_type_p (TREE_VALUE (t), type))
3538 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3539 F for every subobject, passing it the type, offset, and table of
3540 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3541 bases should be traversed; otherwise, they are ignored.
3543 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3544 than MAX_OFFSET will not be walked.
3546 If F returns a non-zero value, the traversal ceases, and that value
3547 is returned. Otherwise, returns zero. */
3550 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3552 subobject_offset_fn f;
3560 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3562 if (max_offset && INT_CST_LT (max_offset, offset))
3565 if (CLASS_TYPE_P (type))
3570 /* Record the location of TYPE. */
3571 r = (*f) (type, offset, offsets);
3575 /* Iterate through the direct base classes of TYPE. */
3576 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3578 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3581 && TREE_VIA_VIRTUAL (binfo)
3582 && !BINFO_PRIMARY_P (binfo))
3585 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3587 size_binop (PLUS_EXPR,
3589 BINFO_OFFSET (binfo)),
3597 /* Iterate through the fields of TYPE. */
3598 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3599 if (TREE_CODE (field) == FIELD_DECL)
3601 r = walk_subobject_offsets (TREE_TYPE (field),
3603 size_binop (PLUS_EXPR,
3605 DECL_FIELD_OFFSET (field)),
3613 else if (TREE_CODE (type) == ARRAY_TYPE)
3615 tree domain = TYPE_DOMAIN (type);
3618 /* Step through each of the elements in the array. */
3619 for (index = size_zero_node;
3620 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3621 index = size_binop (PLUS_EXPR, index, size_one_node))
3623 r = walk_subobject_offsets (TREE_TYPE (type),
3631 offset = size_binop (PLUS_EXPR, offset,
3632 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3633 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3634 there's no point in iterating through the remaining
3635 elements of the array. */
3636 if (max_offset && INT_CST_LT (max_offset, offset))
3644 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3645 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3649 record_subobject_offsets (type, offset, offsets, vbases_p)
3655 walk_subobject_offsets (type, record_subobject_offset, offset,
3656 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3659 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3660 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3661 virtual bases of TYPE are examined. */
3664 layout_conflict_p (type, offset, offsets, vbases_p)
3670 splay_tree_node max_node;
3672 /* Get the node in OFFSETS that indicates the maximum offset where
3673 an empty subobject is located. */
3674 max_node = splay_tree_max (offsets);
3675 /* If there aren't any empty subobjects, then there's no point in
3676 performing this check. */
3680 return walk_subobject_offsets (type, check_subobject_offset, offset,
3681 offsets, (tree) (max_node->key),
3685 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3686 non-static data member of the type indicated by RLI. BINFO is the
3687 binfo corresponding to the base subobject, OFFSETS maps offsets to
3688 types already located at those offsets. T is the most derived
3689 type. This function determines the position of the DECL. */
3692 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3693 record_layout_info rli;
3699 tree offset = NULL_TREE;
3700 tree type = TREE_TYPE (decl);
3701 /* If we are laying out a base class, rather than a field, then
3702 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3703 int field_p = !DECL_ARTIFICIAL (decl);
3705 /* Try to place the field. It may take more than one try if we have
3706 a hard time placing the field without putting two objects of the
3707 same type at the same address. */
3710 struct record_layout_info_s old_rli = *rli;
3712 /* Place this field. */
3713 place_field (rli, decl);
3714 offset = byte_position (decl);
3716 /* We have to check to see whether or not there is already
3717 something of the same type at the offset we're about to use.
3721 struct T : public S { int i; };
3722 struct U : public S, public T {};
3724 Here, we put S at offset zero in U. Then, we can't put T at
3725 offset zero -- its S component would be at the same address
3726 as the S we already allocated. So, we have to skip ahead.
3727 Since all data members, including those whose type is an
3728 empty class, have non-zero size, any overlap can happen only
3729 with a direct or indirect base-class -- it can't happen with
3731 if (layout_conflict_p (TREE_TYPE (decl),
3736 /* Strip off the size allocated to this field. That puts us
3737 at the first place we could have put the field with
3738 proper alignment. */
3741 /* Bump up by the alignment required for the type. */
3743 = size_binop (PLUS_EXPR, rli->bitpos,
3745 ? CLASSTYPE_ALIGN (type)
3746 : TYPE_ALIGN (type)));
3747 normalize_rli (rli);
3750 /* There was no conflict. We're done laying out this field. */
3754 /* Now that we know where it will be placed, update its
3756 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3757 propagate_binfo_offsets (binfo,
3758 convert (ssizetype, offset), t);
3761 /* Layout the empty base BINFO. EOC indicates the byte currently just
3762 past the end of the class, and should be correctly aligned for a
3763 class of the type indicated by BINFO; OFFSETS gives the offsets of
3764 the empty bases allocated so far. T is the most derived
3765 type. Return non-zero iff we added it at the end. */
3768 layout_empty_base (binfo, eoc, offsets, t)
3775 tree basetype = BINFO_TYPE (binfo);
3778 /* This routine should only be used for empty classes. */
3779 my_friendly_assert (is_empty_class (basetype), 20000321);
3780 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3782 /* This is an empty base class. We first try to put it at offset
3784 if (layout_conflict_p (BINFO_TYPE (binfo),
3785 BINFO_OFFSET (binfo),
3789 /* That didn't work. Now, we move forward from the next
3790 available spot in the class. */
3792 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3795 if (!layout_conflict_p (BINFO_TYPE (binfo),
3796 BINFO_OFFSET (binfo),
3799 /* We finally found a spot where there's no overlap. */
3802 /* There's overlap here, too. Bump along to the next spot. */
3803 propagate_binfo_offsets (binfo, alignment, t);
3809 /* Build a FIELD_DECL for the base given by BINFO in the class
3810 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3811 *BASE_ALIGN is a running maximum of the alignments of any base
3812 class. OFFSETS gives the location of empty base subobjects. T is
3813 the most derived type. Return non-zero if the new object cannot be
3817 build_base_field (rli, binfo, empty_p, offsets, t)
3818 record_layout_info rli;
3824 tree basetype = BINFO_TYPE (binfo);
3828 if (!COMPLETE_TYPE_P (basetype))
3829 /* This error is now reported in xref_tag, thus giving better
3830 location information. */
3833 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3834 DECL_ARTIFICIAL (decl) = 1;
3835 DECL_FIELD_CONTEXT (decl) = rli->t;
3836 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3837 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3838 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3839 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3841 if (!integer_zerop (DECL_SIZE (decl)))
3843 /* The containing class is non-empty because it has a non-empty
3847 /* Try to place the field. It may take more than one try if we
3848 have a hard time placing the field without putting two
3849 objects of the same type at the same address. */
3850 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3854 unsigned HOST_WIDE_INT eoc;
3856 /* On some platforms (ARM), even empty classes will not be
3858 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3859 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3860 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3863 /* Record the offsets of BINFO and its base subobjects. */
3864 record_subobject_offsets (BINFO_TYPE (binfo),
3865 BINFO_OFFSET (binfo),
3871 /* Layout all of the non-virtual base classes. Record empty
3872 subobjects in OFFSETS. T is the most derived type. Return
3873 non-zero if the type cannot be nearly empty. */
3876 build_base_fields (rli, empty_p, offsets, t)
3877 record_layout_info rli;
3882 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3885 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3889 /* The primary base class is always allocated first. */
3890 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3891 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3892 empty_p, offsets, t);
3894 /* Now allocate the rest of the bases. */
3895 for (i = 0; i < n_baseclasses; ++i)
3899 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3901 /* The primary base was already allocated above, so we don't
3902 need to allocate it again here. */
3903 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3906 /* A primary virtual base class is allocated just like any other
3907 base class, but a non-primary virtual base is allocated
3908 later, in layout_virtual_bases. */
3909 if (TREE_VIA_VIRTUAL (base_binfo)
3910 && !BINFO_PRIMARY_P (base_binfo))
3913 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3918 /* Go through the TYPE_METHODS of T issuing any appropriate
3919 diagnostics, figuring out which methods override which other
3920 methods, and so forth. */
3928 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3930 /* If this was an evil function, don't keep it in class. */
3931 if (DECL_ASSEMBLER_NAME_SET_P (x)
3932 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3935 check_for_override (x, t);
3936 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3937 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3939 /* The name of the field is the original field name
3940 Save this in auxiliary field for later overloading. */
3941 if (DECL_VINDEX (x))
3943 TYPE_POLYMORPHIC_P (t) = 1;
3944 if (DECL_PURE_VIRTUAL_P (x))
3945 CLASSTYPE_PURE_VIRTUALS (t)
3946 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3951 /* FN is a constructor or destructor. Clone the declaration to create
3952 a specialized in-charge or not-in-charge version, as indicated by
3956 build_clone (fn, name)
3963 /* Copy the function. */
3964 clone = copy_decl (fn);
3965 /* Remember where this function came from. */
3966 DECL_CLONED_FUNCTION (clone) = fn;
3967 DECL_ABSTRACT_ORIGIN (clone) = fn;
3968 /* Reset the function name. */
3969 DECL_NAME (clone) = name;
3970 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3971 /* There's no pending inline data for this function. */
3972 DECL_PENDING_INLINE_INFO (clone) = NULL;
3973 DECL_PENDING_INLINE_P (clone) = 0;
3974 /* And it hasn't yet been deferred. */
3975 DECL_DEFERRED_FN (clone) = 0;
3977 /* The base-class destructor is not virtual. */
3978 if (name == base_dtor_identifier)
3980 DECL_VIRTUAL_P (clone) = 0;
3981 if (TREE_CODE (clone) != TEMPLATE_DECL)
3982 DECL_VINDEX (clone) = NULL_TREE;
3985 /* If there was an in-charge parameter, drop it from the function
3987 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3993 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3994 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3995 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3996 /* Skip the `this' parameter. */
3997 parmtypes = TREE_CHAIN (parmtypes);
3998 /* Skip the in-charge parameter. */
3999 parmtypes = TREE_CHAIN (parmtypes);
4000 /* And the VTT parm, in a complete [cd]tor. */
4001 if (DECL_HAS_VTT_PARM_P (fn)
4002 && ! DECL_NEEDS_VTT_PARM_P (clone))
4003 parmtypes = TREE_CHAIN (parmtypes);
4004 /* If this is subobject constructor or destructor, add the vtt
4007 = build_cplus_method_type (basetype,
4008 TREE_TYPE (TREE_TYPE (clone)),
4011 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4015 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4016 aren't function parameters; those are the template parameters. */
4017 if (TREE_CODE (clone) != TEMPLATE_DECL)
4019 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4020 /* Remove the in-charge parameter. */
4021 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4023 TREE_CHAIN (DECL_ARGUMENTS (clone))
4024 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4025 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4027 /* And the VTT parm, in a complete [cd]tor. */
4028 if (DECL_HAS_VTT_PARM_P (fn))
4030 if (DECL_NEEDS_VTT_PARM_P (clone))
4031 DECL_HAS_VTT_PARM_P (clone) = 1;
4034 TREE_CHAIN (DECL_ARGUMENTS (clone))
4035 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4036 DECL_HAS_VTT_PARM_P (clone) = 0;
4040 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4042 DECL_CONTEXT (parms) = clone;
4043 cxx_dup_lang_specific_decl (parms);
4047 /* Create the RTL for this function. */
4048 SET_DECL_RTL (clone, NULL_RTX);
4049 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4051 /* Make it easy to find the CLONE given the FN. */
4052 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4053 TREE_CHAIN (fn) = clone;
4055 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4056 if (TREE_CODE (clone) == TEMPLATE_DECL)
4060 DECL_TEMPLATE_RESULT (clone)
4061 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4062 result = DECL_TEMPLATE_RESULT (clone);
4063 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4064 DECL_TI_TEMPLATE (result) = clone;
4066 else if (DECL_DEFERRED_FN (fn))
4072 /* Produce declarations for all appropriate clones of FN. If
4073 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4074 CLASTYPE_METHOD_VEC as well. */
4077 clone_function_decl (fn, update_method_vec_p)
4079 int update_method_vec_p;
4083 /* Avoid inappropriate cloning. */
4085 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4088 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4090 /* For each constructor, we need two variants: an in-charge version
4091 and a not-in-charge version. */
4092 clone = build_clone (fn, complete_ctor_identifier);
4093 if (update_method_vec_p)
4094 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4095 clone = build_clone (fn, base_ctor_identifier);
4096 if (update_method_vec_p)
4097 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4101 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4103 /* For each destructor, we need three variants: an in-charge
4104 version, a not-in-charge version, and an in-charge deleting
4105 version. We clone the deleting version first because that
4106 means it will go second on the TYPE_METHODS list -- and that
4107 corresponds to the correct layout order in the virtual
4110 For a non-virtual destructor, we do not build a deleting
4112 if (DECL_VIRTUAL_P (fn))
4114 clone = build_clone (fn, deleting_dtor_identifier);
4115 if (update_method_vec_p)
4116 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4118 clone = build_clone (fn, complete_dtor_identifier);
4119 if (update_method_vec_p)
4120 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4121 clone = build_clone (fn, base_dtor_identifier);
4122 if (update_method_vec_p)
4123 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4126 /* Note that this is an abstract function that is never emitted. */
4127 DECL_ABSTRACT (fn) = 1;
4130 /* DECL is an in charge constructor, which is being defined. This will
4131 have had an in class declaration, from whence clones were
4132 declared. An out-of-class definition can specify additional default
4133 arguments. As it is the clones that are involved in overload
4134 resolution, we must propagate the information from the DECL to its
4138 adjust_clone_args (decl)
4143 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4144 clone = TREE_CHAIN (clone))
4146 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4147 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4148 tree decl_parms, clone_parms;
4150 clone_parms = orig_clone_parms;
4152 /* Skip the 'this' parameter. */
4153 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4154 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4156 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4157 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4158 if (DECL_HAS_VTT_PARM_P (decl))
4159 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4161 clone_parms = orig_clone_parms;
4162 if (DECL_HAS_VTT_PARM_P (clone))
4163 clone_parms = TREE_CHAIN (clone_parms);
4165 for (decl_parms = orig_decl_parms; decl_parms;
4166 decl_parms = TREE_CHAIN (decl_parms),
4167 clone_parms = TREE_CHAIN (clone_parms))
4169 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4170 TREE_TYPE (clone_parms)), 20010424);
4172 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4174 /* A default parameter has been added. Adjust the
4175 clone's parameters. */
4176 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4177 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4180 clone_parms = orig_decl_parms;
4182 if (DECL_HAS_VTT_PARM_P (clone))
4184 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4185 TREE_VALUE (orig_clone_parms),
4187 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4189 type = build_cplus_method_type (basetype,
4190 TREE_TYPE (TREE_TYPE (clone)),
4193 type = build_exception_variant (type, exceptions);
4194 TREE_TYPE (clone) = type;
4196 clone_parms = NULL_TREE;
4200 my_friendly_assert (!clone_parms, 20010424);
4204 /* For each of the constructors and destructors in T, create an
4205 in-charge and not-in-charge variant. */
4208 clone_constructors_and_destructors (t)
4213 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4215 if (!CLASSTYPE_METHOD_VEC (t))
4218 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4219 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4220 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4221 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4224 /* Remove all zero-width bit-fields from T. */
4227 remove_zero_width_bit_fields (t)
4232 fieldsp = &TYPE_FIELDS (t);
4235 if (TREE_CODE (*fieldsp) == FIELD_DECL
4236 && DECL_C_BIT_FIELD (*fieldsp)
4237 && DECL_INITIAL (*fieldsp))
4238 *fieldsp = TREE_CHAIN (*fieldsp);
4240 fieldsp = &TREE_CHAIN (*fieldsp);
4244 /* Returns TRUE iff we need a cookie when dynamically allocating an
4245 array whose elements have the indicated class TYPE. */
4248 type_requires_array_cookie (type)
4252 bool has_two_argument_delete_p = false;
4254 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4256 /* If there's a non-trivial destructor, we need a cookie. In order
4257 to iterate through the array calling the destructor for each
4258 element, we'll have to know how many elements there are. */
4259 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4262 /* If the usual deallocation function is a two-argument whose second
4263 argument is of type `size_t', then we have to pass the size of
4264 the array to the deallocation function, so we will need to store
4266 fns = lookup_fnfields (TYPE_BINFO (type),
4267 ansi_opname (VEC_DELETE_EXPR),
4269 /* If there are no `operator []' members, or the lookup is
4270 ambiguous, then we don't need a cookie. */
4271 if (!fns || fns == error_mark_node)
4273 /* Loop through all of the functions. */
4274 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
4279 /* Select the current function. */
4280 fn = OVL_CURRENT (fns);
4281 /* See if this function is a one-argument delete function. If
4282 it is, then it will be the usual deallocation function. */
4283 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4284 if (second_parm == void_list_node)
4286 /* Otherwise, if we have a two-argument function and the second
4287 argument is `size_t', it will be the usual deallocation
4288 function -- unless there is one-argument function, too. */
4289 if (TREE_CHAIN (second_parm) == void_list_node
4290 && same_type_p (TREE_VALUE (second_parm), sizetype))
4291 has_two_argument_delete_p = true;
4294 return has_two_argument_delete_p;
4297 /* Check the validity of the bases and members declared in T. Add any
4298 implicitly-generated functions (like copy-constructors and
4299 assignment operators). Compute various flag bits (like
4300 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4301 level: i.e., independently of the ABI in use. */
4304 check_bases_and_members (t, empty_p)
4308 /* Nonzero if we are not allowed to generate a default constructor
4310 int cant_have_default_ctor;
4311 /* Nonzero if the implicitly generated copy constructor should take
4312 a non-const reference argument. */
4313 int cant_have_const_ctor;
4314 /* Nonzero if the the implicitly generated assignment operator
4315 should take a non-const reference argument. */
4316 int no_const_asn_ref;
4319 /* By default, we use const reference arguments and generate default
4321 cant_have_default_ctor = 0;
4322 cant_have_const_ctor = 0;
4323 no_const_asn_ref = 0;
4325 /* Assume that the class is nearly empty; we'll clear this flag if
4326 it turns out not to be nearly empty. */
4327 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4329 /* Check all the base-classes. */
4330 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4333 /* Check all the data member declarations. */
4334 check_field_decls (t, &access_decls, empty_p,
4335 &cant_have_default_ctor,
4336 &cant_have_const_ctor,
4339 /* Check all the method declarations. */
4342 /* A nearly-empty class has to be vptr-containing; a nearly empty
4343 class contains just a vptr. */
4344 if (!TYPE_CONTAINS_VPTR_P (t))
4345 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4347 /* Do some bookkeeping that will guide the generation of implicitly
4348 declared member functions. */
4349 TYPE_HAS_COMPLEX_INIT_REF (t)
4350 |= (TYPE_HAS_INIT_REF (t)
4351 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4352 || TYPE_POLYMORPHIC_P (t));
4353 TYPE_NEEDS_CONSTRUCTING (t)
4354 |= (TYPE_HAS_CONSTRUCTOR (t)
4355 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4356 || TYPE_POLYMORPHIC_P (t));
4357 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4358 || TYPE_POLYMORPHIC_P (t));
4359 CLASSTYPE_NON_POD_P (t)
4360 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4361 || TYPE_HAS_ASSIGN_REF (t));
4362 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4363 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4364 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4366 /* Synthesize any needed methods. Note that methods will be synthesized
4367 for anonymous unions; grok_x_components undoes that. */
4368 add_implicitly_declared_members (t, cant_have_default_ctor,
4369 cant_have_const_ctor,
4372 /* Create the in-charge and not-in-charge variants of constructors
4374 clone_constructors_and_destructors (t);
4376 /* Process the using-declarations. */
4377 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4378 handle_using_decl (TREE_VALUE (access_decls), t);
4380 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4381 finish_struct_methods (t);
4383 /* Figure out whether or not we will need a cookie when dynamically
4384 allocating an array of this type. */
4385 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4386 = type_requires_array_cookie (t);
4389 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4390 accordingly. If a new vfield was created (because T doesn't have a
4391 primary base class), then the newly created field is returned. It
4392 is not added to the TYPE_FIELDS list; it is the caller's
4393 responsibility to do that. */
4396 create_vtable_ptr (t, empty_p, vfuns_p,
4397 new_virtuals_p, overridden_virtuals_p)
4401 tree *new_virtuals_p;
4402 tree *overridden_virtuals_p;
4406 /* Loop over the virtual functions, adding them to our various
4408 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4409 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4410 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4413 /* If we couldn't find an appropriate base class, create a new field
4414 here. Even if there weren't any new virtual functions, we might need a
4415 new virtual function table if we're supposed to include vptrs in
4416 all classes that need them. */
4417 if (!TYPE_VFIELD (t) && (*vfuns_p || TYPE_CONTAINS_VPTR_P (t)))
4419 /* We build this decl with vtbl_ptr_type_node, which is a
4420 `vtable_entry_type*'. It might seem more precise to use
4421 `vtable_entry_type (*)[N]' where N is the number of firtual
4422 functions. However, that would require the vtable pointer in
4423 base classes to have a different type than the vtable pointer
4424 in derived classes. We could make that happen, but that
4425 still wouldn't solve all the problems. In particular, the
4426 type-based alias analysis code would decide that assignments
4427 to the base class vtable pointer can't alias assignments to
4428 the derived class vtable pointer, since they have different
4429 types. Thus, in an derived class destructor, where the base
4430 class constructor was inlined, we could generate bad code for
4431 setting up the vtable pointer.
4433 Therefore, we use one type for all vtable pointers. We still
4434 use a type-correct type; it's just doesn't indicate the array
4435 bounds. That's better than using `void*' or some such; it's
4436 cleaner, and it let's the alias analysis code know that these
4437 stores cannot alias stores to void*! */
4440 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4441 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4442 DECL_VIRTUAL_P (field) = 1;
4443 DECL_ARTIFICIAL (field) = 1;
4444 DECL_FIELD_CONTEXT (field) = t;
4445 DECL_FCONTEXT (field) = t;
4446 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4447 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4449 TYPE_VFIELD (t) = field;
4451 /* This class is non-empty. */
4454 if (CLASSTYPE_N_BASECLASSES (t))
4455 /* If there were any baseclasses, they can't possibly be at
4456 offset zero any more, because that's where the vtable
4457 pointer is. So, converting to a base class is going to
4459 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4467 /* Fixup the inline function given by INFO now that the class is
4471 fixup_pending_inline (fn)
4474 if (DECL_PENDING_INLINE_INFO (fn))
4476 tree args = DECL_ARGUMENTS (fn);
4479 DECL_CONTEXT (args) = fn;
4480 args = TREE_CHAIN (args);
4485 /* Fixup the inline methods and friends in TYPE now that TYPE is
4489 fixup_inline_methods (type)
4492 tree method = TYPE_METHODS (type);
4494 if (method && TREE_CODE (method) == TREE_VEC)
4496 if (TREE_VEC_ELT (method, 1))
4497 method = TREE_VEC_ELT (method, 1);
4498 else if (TREE_VEC_ELT (method, 0))
4499 method = TREE_VEC_ELT (method, 0);
4501 method = TREE_VEC_ELT (method, 2);
4504 /* Do inline member functions. */
4505 for (; method; method = TREE_CHAIN (method))
4506 fixup_pending_inline (method);
4509 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4511 method = TREE_CHAIN (method))
4512 fixup_pending_inline (TREE_VALUE (method));
4513 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4516 /* Add OFFSET to all base types of BINFO which is a base in the
4517 hierarchy dominated by T.
4519 OFFSET, which is a type offset, is number of bytes. */
4522 propagate_binfo_offsets (binfo, offset, t)
4530 /* Update BINFO's offset. */
4531 BINFO_OFFSET (binfo)
4532 = convert (sizetype,
4533 size_binop (PLUS_EXPR,
4534 convert (ssizetype, BINFO_OFFSET (binfo)),
4537 /* Find the primary base class. */
4538 primary_binfo = get_primary_binfo (binfo);
4540 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4542 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4546 /* On the first time through the loop, do the primary base.
4547 Because the primary base need not be an immediate base, we
4548 must handle the primary base specially. */
4554 base_binfo = primary_binfo;
4558 base_binfo = BINFO_BASETYPE (binfo, i);
4559 /* Don't do the primary base twice. */
4560 if (base_binfo == primary_binfo)
4564 /* Skip virtual bases that aren't our canonical primary base. */
4565 if (TREE_VIA_VIRTUAL (base_binfo)
4566 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4567 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4570 propagate_binfo_offsets (base_binfo, offset, t);
4574 /* Called via dfs_walk from layout_virtual bases. */
4577 dfs_set_offset_for_unshared_vbases (binfo, data)
4581 /* If this is a virtual base, make sure it has the same offset as
4582 the shared copy. If it's a primary base, then we know it's
4584 if (TREE_VIA_VIRTUAL (binfo))
4586 tree t = (tree) data;
4590 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4593 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4594 propagate_binfo_offsets (binfo, offset, t);
4601 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4602 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4603 empty subobjects of T. */
4606 layout_virtual_bases (t, offsets)
4611 unsigned HOST_WIDE_INT eoc;
4613 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4616 #ifdef STRUCTURE_SIZE_BOUNDARY
4617 /* Packed structures don't need to have minimum size. */
4618 if (! TYPE_PACKED (t))
4619 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4622 /* DSIZE is the size of the class without the virtual bases. */
4623 dsize = TYPE_SIZE (t);
4625 /* Make every class have alignment of at least one. */
4626 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4628 /* Go through the virtual bases, allocating space for each virtual
4629 base that is not already a primary base class. These are
4630 allocated in inheritance graph order. */
4631 for (vbases = TYPE_BINFO (t);
4633 vbases = TREE_CHAIN (vbases))
4637 if (!TREE_VIA_VIRTUAL (vbases))
4639 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4641 if (!BINFO_PRIMARY_P (vbase))
4643 /* This virtual base is not a primary base of any class in the
4644 hierarchy, so we have to add space for it. */
4645 tree basetype, usize;
4646 unsigned int desired_align;
4648 basetype = BINFO_TYPE (vbase);
4650 desired_align = CLASSTYPE_ALIGN (basetype);
4651 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4653 /* Add padding so that we can put the virtual base class at an
4654 appropriately aligned offset. */
4655 dsize = round_up (dsize, desired_align);
4657 usize = size_binop (CEIL_DIV_EXPR, dsize, bitsize_unit_node);
4659 /* We try to squish empty virtual bases in just like
4660 ordinary empty bases. */
4661 if (is_empty_class (basetype))
4662 layout_empty_base (vbase,
4663 convert (sizetype, usize),
4669 offset = convert (ssizetype, usize);
4670 offset = size_diffop (offset,
4672 BINFO_OFFSET (vbase)));
4674 /* And compute the offset of the virtual base. */
4675 propagate_binfo_offsets (vbase, offset, t);
4676 /* Every virtual baseclass takes a least a UNIT, so that
4677 we can take it's address and get something different
4679 dsize = size_binop (PLUS_EXPR, dsize,
4680 size_binop (MAX_EXPR, bitsize_unit_node,
4681 CLASSTYPE_SIZE (basetype)));
4684 /* Keep track of the offsets assigned to this virtual base. */
4685 record_subobject_offsets (BINFO_TYPE (vbase),
4686 BINFO_OFFSET (vbase),
4692 /* Now, go through the TYPE_BINFO hierarchy, setting the
4693 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4694 bases and their direct and indirect bases. The ambiguity checks
4695 in lookup_base depend on the BINFO_OFFSETs being set
4697 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4699 /* If we had empty base classes that protruded beyond the end of the
4700 class, we didn't update DSIZE above; we were hoping to overlay
4701 multiple such bases at the same location. */
4702 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4703 dsize = size_binop (MAX_EXPR, dsize, bitsize_int (eoc * BITS_PER_UNIT));
4705 /* Now, make sure that the total size of the type is a multiple of
4707 dsize = round_up (dsize, TYPE_ALIGN (t));
4708 TYPE_SIZE (t) = dsize;
4709 TYPE_SIZE_UNIT (t) = convert (sizetype,
4710 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4711 bitsize_unit_node));
4713 /* Check for ambiguous virtual bases. */
4715 for (vbases = CLASSTYPE_VBASECLASSES (t);
4717 vbases = TREE_CHAIN (vbases))
4719 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4721 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4722 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4727 /* Returns the offset of the byte just past the end of the base class
4728 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4729 only non-virtual bases are included. */
4731 static unsigned HOST_WIDE_INT
4732 end_of_class (t, include_virtuals_p)
4734 int include_virtuals_p;
4736 unsigned HOST_WIDE_INT result = 0;
4739 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4744 unsigned HOST_WIDE_INT end_of_base;
4746 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4748 if (!include_virtuals_p
4749 && TREE_VIA_VIRTUAL (base_binfo)
4750 && !BINFO_PRIMARY_P (base_binfo))
4753 if (is_empty_class (BINFO_TYPE (base_binfo)))
4754 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4755 allocate some space for it. It cannot have virtual bases,
4756 so TYPE_SIZE_UNIT is fine. */
4757 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4759 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4760 offset = size_binop (PLUS_EXPR,
4761 BINFO_OFFSET (base_binfo),
4763 end_of_base = tree_low_cst (offset, /*pos=*/1);
4764 if (end_of_base > result)
4765 result = end_of_base;
4771 /* Warn about direct bases of T that are inaccessible because they are
4772 ambiguous. For example:
4775 struct T : public S {};
4776 struct U : public S, public T {};
4778 Here, `(S*) new U' is not allowed because there are two `S'
4782 warn_about_ambiguous_direct_bases (t)
4787 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4789 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4791 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4792 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4797 /* Compare two INTEGER_CSTs K1 and K2. */
4800 splay_tree_compare_integer_csts (k1, k2)
4804 return tree_int_cst_compare ((tree) k1, (tree) k2);
4807 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4808 BINFO_OFFSETs for all of the base-classes. Position the vtable
4812 layout_class_type (t, empty_p, vfuns_p,
4813 new_virtuals_p, overridden_virtuals_p)
4817 tree *new_virtuals_p;
4818 tree *overridden_virtuals_p;
4820 tree non_static_data_members;
4823 record_layout_info rli;
4824 unsigned HOST_WIDE_INT eoc;
4825 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4826 types that appear at that offset. */
4827 splay_tree empty_base_offsets;
4829 /* Keep track of the first non-static data member. */
4830 non_static_data_members = TYPE_FIELDS (t);
4832 /* Start laying out the record. */
4833 rli = start_record_layout (t);
4835 /* If possible, we reuse the virtual function table pointer from one
4836 of our base classes. */
4837 determine_primary_base (t, vfuns_p);
4839 /* Create a pointer to our virtual function table. */
4840 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4841 new_virtuals_p, overridden_virtuals_p);
4843 /* The vptr is always the first thing in the class. */
4846 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4847 place_field (rli, vptr);
4850 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4851 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4853 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4854 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4856 /* Layout the non-static data members. */
4857 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4862 /* We still pass things that aren't non-static data members to
4863 the back-end, in case it wants to do something with them. */
4864 if (TREE_CODE (field) != FIELD_DECL)
4866 place_field (rli, field);
4867 /* If the static data member has incomplete type, keep track
4868 of it so that it can be completed later. (The handling
4869 of pending statics in finish_record_layout is
4870 insufficient; consider:
4873 struct S2 { static S1 s1; };
4875 At this point, finish_record_layout will be called, but
4876 S1 is still incomplete.) */
4877 if (TREE_CODE (field) == VAR_DECL)
4878 maybe_register_incomplete_var (field);
4882 type = TREE_TYPE (field);
4884 /* If this field is a bit-field whose width is greater than its
4885 type, then there are some special rules for allocating
4887 if (DECL_C_BIT_FIELD (field)
4888 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4890 integer_type_kind itk;
4893 /* We must allocate the bits as if suitably aligned for the
4894 longest integer type that fits in this many bits. type
4895 of the field. Then, we are supposed to use the left over
4896 bits as additional padding. */
4897 for (itk = itk_char; itk != itk_none; ++itk)
4898 if (INT_CST_LT (DECL_SIZE (field),
4899 TYPE_SIZE (integer_types[itk])))
4902 /* ITK now indicates a type that is too large for the
4903 field. We have to back up by one to find the largest
4905 integer_type = integer_types[itk - 1];
4906 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4907 TYPE_SIZE (integer_type));
4908 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4909 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4910 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4913 padding = NULL_TREE;
4915 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4916 empty_base_offsets, t);
4918 /* If we needed additional padding after this field, add it
4924 padding_field = build_decl (FIELD_DECL,
4927 DECL_BIT_FIELD (padding_field) = 1;
4928 DECL_SIZE (padding_field) = padding;
4929 DECL_ALIGN (padding_field) = 1;
4930 DECL_USER_ALIGN (padding_field) = 0;
4931 layout_nonempty_base_or_field (rli, padding_field,
4933 empty_base_offsets, t);
4937 /* It might be the case that we grew the class to allocate a
4938 zero-sized base class. That won't be reflected in RLI, yet,
4939 because we are willing to overlay multiple bases at the same
4940 offset. However, now we need to make sure that RLI is big enough
4941 to reflect the entire class. */
4942 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4943 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4944 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4946 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4947 rli->bitpos = bitsize_zero_node;
4950 /* We make all structures have at least one element, so that they
4951 have non-zero size. The class may be empty even if it has
4952 basetypes. Therefore, we add the fake field after all the other
4953 fields; if there are already FIELD_DECLs on the list, their
4954 offsets will not be disturbed. */
4955 if (!eoc && *empty_p)
4959 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4960 place_field (rli, padding);
4963 /* Let the back-end lay out the type. Note that at this point we
4964 have only included non-virtual base-classes; we will lay out the
4965 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4966 this call are not necessarily correct; they are just the size and
4967 alignment when no virtual base clases are used. */
4968 finish_record_layout (rli);
4970 /* Delete all zero-width bit-fields from the list of fields. Now
4971 that the type is laid out they are no longer important. */
4972 remove_zero_width_bit_fields (t);
4974 /* Remember the size and alignment of the class before adding
4975 the virtual bases. */
4978 CLASSTYPE_SIZE (t) = bitsize_zero_node;
4979 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4983 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4984 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
4987 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
4988 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
4990 /* Set the TYPE_DECL for this type to contain the right
4991 value for DECL_OFFSET, so that we can use it as part
4992 of a COMPONENT_REF for multiple inheritance. */
4993 layout_decl (TYPE_MAIN_DECL (t), 0);
4995 /* Now fix up any virtual base class types that we left lying
4996 around. We must get these done before we try to lay out the
4997 virtual function table. As a side-effect, this will remove the
4998 base subobject fields. */
4999 layout_virtual_bases (t, empty_base_offsets);
5001 /* Warn about direct bases that can't be talked about due to
5003 warn_about_ambiguous_direct_bases (t);
5006 splay_tree_delete (empty_base_offsets);
5009 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5010 (or C++ class declaration).
5012 For C++, we must handle the building of derived classes.
5013 Also, C++ allows static class members. The way that this is
5014 handled is to keep the field name where it is (as the DECL_NAME
5015 of the field), and place the overloaded decl in the bit position
5016 of the field. layout_record and layout_union will know about this.
5018 More C++ hair: inline functions have text in their
5019 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5020 meaningful tree structure. After the struct has been laid out, set
5021 things up so that this can happen.
5023 And still more: virtual functions. In the case of single inheritance,
5024 when a new virtual function is seen which redefines a virtual function
5025 from the base class, the new virtual function is placed into
5026 the virtual function table at exactly the same address that
5027 it had in the base class. When this is extended to multiple
5028 inheritance, the same thing happens, except that multiple virtual
5029 function tables must be maintained. The first virtual function
5030 table is treated in exactly the same way as in the case of single
5031 inheritance. Additional virtual function tables have different
5032 DELTAs, which tell how to adjust `this' to point to the right thing.
5034 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5042 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5043 a FUNCTION_DECL. Each of these functions is a virtual function
5044 declared in T that does not override any virtual function from a
5046 tree new_virtuals = NULL_TREE;
5047 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5048 except that each declaration here overrides the declaration from
5050 tree overridden_virtuals = NULL_TREE;
5055 if (COMPLETE_TYPE_P (t))
5057 if (IS_AGGR_TYPE (t))
5058 error ("redefinition of `%#T'", t);
5065 /* If this type was previously laid out as a forward reference,
5066 make sure we lay it out again. */
5067 TYPE_SIZE (t) = NULL_TREE;
5068 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5069 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5071 CLASSTYPE_RTTI (t) = NULL_TREE;
5073 fixup_inline_methods (t);
5075 /* Do end-of-class semantic processing: checking the validity of the
5076 bases and members and add implicitly generated methods. */
5077 check_bases_and_members (t, &empty);
5079 /* Layout the class itself. */
5080 layout_class_type (t, &empty, &vfuns,
5081 &new_virtuals, &overridden_virtuals);
5083 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5084 vfield = TYPE_VFIELD (t);
5085 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5087 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5089 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5090 BINFO_TYPE (primary)),
5092 /* The vtable better be at the start. */
5093 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5095 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5098 vfield = copy_decl (vfield);
5099 DECL_FIELD_CONTEXT (vfield) = t;
5100 TYPE_VFIELD (t) = vfield;
5103 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5106 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5108 /* If we created a new vtbl pointer for this class, add it to the
5110 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5111 CLASSTYPE_VFIELDS (t)
5112 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5114 /* If necessary, create the primary vtable for this class. */
5115 if (new_virtuals || overridden_virtuals || TYPE_CONTAINS_VPTR_P (t))
5117 new_virtuals = nreverse (new_virtuals);
5118 /* We must enter these virtuals into the table. */
5119 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5120 build_primary_vtable (NULL_TREE, t);
5121 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5122 /* Here we know enough to change the type of our virtual
5123 function table, but we will wait until later this function. */
5124 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5126 /* If this type has basetypes with constructors, then those
5127 constructors might clobber the virtual function table. But
5128 they don't if the derived class shares the exact vtable of the base
5131 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5133 /* If we didn't need a new vtable, see if we should copy one from
5135 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5137 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5139 /* If this class uses a different vtable than its primary base
5140 then when we will need to initialize our vptr after the base
5141 class constructor runs. */
5142 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5143 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5146 if (TYPE_CONTAINS_VPTR_P (t))
5148 if (TYPE_BINFO_VTABLE (t))
5149 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5151 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5152 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5155 CLASSTYPE_VSIZE (t) = vfuns;
5156 /* Entries for virtual functions defined in the primary base are
5157 followed by entries for new functions unique to this class. */
5158 TYPE_BINFO_VIRTUALS (t)
5159 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5160 /* Finally, add entries for functions that override virtuals
5161 from non-primary bases. */
5162 TYPE_BINFO_VIRTUALS (t)
5163 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5166 finish_struct_bits (t);
5168 /* Complete the rtl for any static member objects of the type we're
5170 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5171 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5172 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5173 DECL_MODE (x) = TYPE_MODE (t);
5175 /* Done with FIELDS...now decide whether to sort these for
5176 faster lookups later.
5178 The C front-end only does this when n_fields > 15. We use
5179 a smaller number because most searches fail (succeeding
5180 ultimately as the search bores through the inheritance
5181 hierarchy), and we want this failure to occur quickly. */
5183 n_fields = count_fields (TYPE_FIELDS (t));
5186 tree field_vec = make_tree_vec (n_fields);
5187 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5188 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5189 (int (*)(const void *, const void *))field_decl_cmp);
5190 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5191 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5192 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5195 if (TYPE_HAS_CONSTRUCTOR (t))
5197 tree vfields = CLASSTYPE_VFIELDS (t);
5201 /* Mark the fact that constructor for T
5202 could affect anybody inheriting from T
5203 who wants to initialize vtables for VFIELDS's type. */
5204 if (VF_DERIVED_VALUE (vfields))
5205 TREE_ADDRESSABLE (vfields) = 1;
5206 vfields = TREE_CHAIN (vfields);
5210 /* Make the rtl for any new vtables we have created, and unmark
5211 the base types we marked. */
5214 /* Build the VTT for T. */
5217 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5218 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5219 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5223 if (warn_overloaded_virtual)
5226 maybe_suppress_debug_info (t);
5228 dump_class_hierarchy (t);
5230 /* Finish debugging output for this type. */
5231 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5234 /* When T was built up, the member declarations were added in reverse
5235 order. Rearrange them to declaration order. */
5238 unreverse_member_declarations (t)
5245 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5246 reverse order. Put them in declaration order now. */
5247 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5248 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5250 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5251 reverse order, so we can't just use nreverse. */
5253 for (x = TYPE_FIELDS (t);
5254 x && TREE_CODE (x) != TYPE_DECL;
5257 next = TREE_CHAIN (x);
5258 TREE_CHAIN (x) = prev;
5263 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5265 TYPE_FIELDS (t) = prev;
5270 finish_struct (t, attributes)
5273 const char *saved_filename = input_filename;
5274 int saved_lineno = lineno;
5276 /* Now that we've got all the field declarations, reverse everything
5278 unreverse_member_declarations (t);
5280 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5282 /* Nadger the current location so that diagnostics point to the start of
5283 the struct, not the end. */
5284 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5285 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5287 if (processing_template_decl)
5289 finish_struct_methods (t);
5290 TYPE_SIZE (t) = bitsize_zero_node;
5293 finish_struct_1 (t);
5295 input_filename = saved_filename;
5296 lineno = saved_lineno;
5298 TYPE_BEING_DEFINED (t) = 0;
5300 if (current_class_type)
5303 error ("trying to finish struct, but kicked out due to previous parse errors");
5305 if (processing_template_decl)
5307 tree scope = current_scope ();
5308 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5309 add_stmt (build_min (TAG_DEFN, t));
5315 /* Return the dynamic type of INSTANCE, if known.
5316 Used to determine whether the virtual function table is needed
5319 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5320 of our knowledge of its type. *NONNULL should be initialized
5321 before this function is called. */
5324 fixed_type_or_null (instance, nonnull, cdtorp)
5329 switch (TREE_CODE (instance))
5332 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5335 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5339 /* This is a call to a constructor, hence it's never zero. */
5340 if (TREE_HAS_CONSTRUCTOR (instance))
5344 return TREE_TYPE (instance);
5349 /* This is a call to a constructor, hence it's never zero. */
5350 if (TREE_HAS_CONSTRUCTOR (instance))
5354 return TREE_TYPE (instance);
5356 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5363 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5364 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5365 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5366 /* Propagate nonnull. */
5367 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5372 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5377 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5380 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5384 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5385 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5389 return TREE_TYPE (TREE_TYPE (instance));
5391 /* fall through... */
5395 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5399 return TREE_TYPE (instance);
5401 else if (instance == current_class_ptr)
5406 /* if we're in a ctor or dtor, we know our type. */
5407 if (DECL_LANG_SPECIFIC (current_function_decl)
5408 && (DECL_CONSTRUCTOR_P (current_function_decl)
5409 || DECL_DESTRUCTOR_P (current_function_decl)))
5413 return TREE_TYPE (TREE_TYPE (instance));
5416 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5418 /* Reference variables should be references to objects. */
5422 if (TREE_CODE (instance) == VAR_DECL
5423 && DECL_INITIAL (instance))
5424 return fixed_type_or_null (DECL_INITIAL (instance),
5434 /* Return non-zero if the dynamic type of INSTANCE is known, and
5435 equivalent to the static type. We also handle the case where
5436 INSTANCE is really a pointer. Return negative if this is a
5437 ctor/dtor. There the dynamic type is known, but this might not be
5438 the most derived base of the original object, and hence virtual
5439 bases may not be layed out according to this type.
5441 Used to determine whether the virtual function table is needed
5444 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5445 of our knowledge of its type. *NONNULL should be initialized
5446 before this function is called. */
5449 resolves_to_fixed_type_p (instance, nonnull)
5453 tree t = TREE_TYPE (instance);
5456 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5457 if (fixed == NULL_TREE)
5459 if (POINTER_TYPE_P (t))
5461 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5463 return cdtorp ? -1 : 1;
5468 init_class_processing ()
5470 current_class_depth = 0;
5471 current_class_stack_size = 10;
5473 = (class_stack_node_t) xmalloc (current_class_stack_size
5474 * sizeof (struct class_stack_node));
5475 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5477 access_default_node = build_int_2 (0, 0);
5478 access_public_node = build_int_2 (ak_public, 0);
5479 access_protected_node = build_int_2 (ak_protected, 0);
5480 access_private_node = build_int_2 (ak_private, 0);
5481 access_default_virtual_node = build_int_2 (4, 0);
5482 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5483 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5484 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5486 ridpointers[(int) RID_PUBLIC] = access_public_node;
5487 ridpointers[(int) RID_PRIVATE] = access_private_node;
5488 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5491 /* Set current scope to NAME. CODE tells us if this is a
5492 STRUCT, UNION, or ENUM environment.
5494 NAME may end up being NULL_TREE if this is an anonymous or
5495 late-bound struct (as in "struct { ... } foo;") */
5497 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5498 appropriate values, found by looking up the type definition of
5501 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5502 which can be seen locally to the class. They are shadowed by
5503 any subsequent local declaration (including parameter names).
5505 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5506 which have static meaning (i.e., static members, static
5507 member functions, enum declarations, etc).
5509 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5510 which can be seen locally to the class (as in 1), but
5511 know that we are doing this for declaration purposes
5512 (i.e. friend foo::bar (int)).
5514 So that we may avoid calls to lookup_name, we cache the _TYPE
5515 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5517 For multiple inheritance, we perform a two-pass depth-first search
5518 of the type lattice. The first pass performs a pre-order search,
5519 marking types after the type has had its fields installed in
5520 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5521 unmarks the marked types. If a field or member function name
5522 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5523 that name becomes `error_mark_node'. */
5526 pushclass (type, modify)
5530 type = TYPE_MAIN_VARIANT (type);
5532 /* Make sure there is enough room for the new entry on the stack. */
5533 if (current_class_depth + 1 >= current_class_stack_size)
5535 current_class_stack_size *= 2;
5537 = (class_stack_node_t) xrealloc (current_class_stack,
5538 current_class_stack_size
5539 * sizeof (struct class_stack_node));
5542 /* Insert a new entry on the class stack. */
5543 current_class_stack[current_class_depth].name = current_class_name;
5544 current_class_stack[current_class_depth].type = current_class_type;
5545 current_class_stack[current_class_depth].access = current_access_specifier;
5546 current_class_stack[current_class_depth].names_used = 0;
5547 current_class_depth++;
5549 /* Now set up the new type. */
5550 current_class_name = TYPE_NAME (type);
5551 if (TREE_CODE (current_class_name) == TYPE_DECL)
5552 current_class_name = DECL_NAME (current_class_name);
5553 current_class_type = type;
5555 /* By default, things in classes are private, while things in
5556 structures or unions are public. */
5557 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5558 ? access_private_node
5559 : access_public_node);
5561 if (previous_class_type != NULL_TREE
5562 && (type != previous_class_type
5563 || !COMPLETE_TYPE_P (previous_class_type))
5564 && current_class_depth == 1)
5566 /* Forcibly remove any old class remnants. */
5567 invalidate_class_lookup_cache ();
5570 /* If we're about to enter a nested class, clear
5571 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5572 if (modify && current_class_depth > 1)
5573 clear_identifier_class_values ();
5579 if (type != previous_class_type || current_class_depth > 1)
5580 push_class_decls (type);
5585 /* We are re-entering the same class we just left, so we
5586 don't have to search the whole inheritance matrix to find
5587 all the decls to bind again. Instead, we install the
5588 cached class_shadowed list, and walk through it binding
5589 names and setting up IDENTIFIER_TYPE_VALUEs. */
5590 set_class_shadows (previous_class_values);
5591 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5593 tree id = TREE_PURPOSE (item);
5594 tree decl = TREE_TYPE (item);
5596 push_class_binding (id, decl);
5597 if (TREE_CODE (decl) == TYPE_DECL)
5598 set_identifier_type_value (id, TREE_TYPE (decl));
5600 unuse_fields (type);
5603 storetags (CLASSTYPE_TAGS (type));
5607 /* When we exit a toplevel class scope, we save the
5608 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5609 reenter the class. Here, we've entered some other class, so we
5610 must invalidate our cache. */
5613 invalidate_class_lookup_cache ()
5617 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5618 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5619 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5621 previous_class_values = NULL_TREE;
5622 previous_class_type = NULL_TREE;
5625 /* Get out of the current class scope. If we were in a class scope
5626 previously, that is the one popped to. */
5632 /* Since poplevel_class does the popping of class decls nowadays,
5633 this really only frees the obstack used for these decls. */
5636 current_class_depth--;
5637 current_class_name = current_class_stack[current_class_depth].name;
5638 current_class_type = current_class_stack[current_class_depth].type;
5639 current_access_specifier = current_class_stack[current_class_depth].access;
5640 if (current_class_stack[current_class_depth].names_used)
5641 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5644 /* Returns 1 if current_class_type is either T or a nested type of T.
5645 We start looking from 1 because entry 0 is from global scope, and has
5649 currently_open_class (t)
5653 if (t == current_class_type)
5655 for (i = 1; i < current_class_depth; ++i)
5656 if (current_class_stack [i].type == t)
5661 /* If either current_class_type or one of its enclosing classes are derived
5662 from T, return the appropriate type. Used to determine how we found
5663 something via unqualified lookup. */
5666 currently_open_derived_class (t)
5671 if (DERIVED_FROM_P (t, current_class_type))
5672 return current_class_type;
5674 for (i = current_class_depth - 1; i > 0; --i)
5675 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5676 return current_class_stack[i].type;
5681 /* When entering a class scope, all enclosing class scopes' names with
5682 static meaning (static variables, static functions, types and enumerators)
5683 have to be visible. This recursive function calls pushclass for all
5684 enclosing class contexts until global or a local scope is reached.
5685 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5686 formal of the same name. */
5689 push_nested_class (type, modify)
5695 /* A namespace might be passed in error cases, like A::B:C. */
5696 if (type == NULL_TREE
5697 || type == error_mark_node
5698 || TREE_CODE (type) == NAMESPACE_DECL
5699 || ! IS_AGGR_TYPE (type)
5700 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5701 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5704 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5706 if (context && CLASS_TYPE_P (context))
5707 push_nested_class (context, 2);
5708 pushclass (type, modify);
5711 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5716 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5719 if (context && CLASS_TYPE_P (context))
5720 pop_nested_class ();
5723 /* Returns the number of extern "LANG" blocks we are nested within. */
5726 current_lang_depth ()
5728 return VARRAY_ACTIVE_SIZE (current_lang_base);
5731 /* Set global variables CURRENT_LANG_NAME to appropriate value
5732 so that behavior of name-mangling machinery is correct. */
5735 push_lang_context (name)
5738 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5740 if (name == lang_name_cplusplus)
5742 current_lang_name = name;
5744 else if (name == lang_name_java)
5746 current_lang_name = name;
5747 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5748 (See record_builtin_java_type in decl.c.) However, that causes
5749 incorrect debug entries if these types are actually used.
5750 So we re-enable debug output after extern "Java". */
5751 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5752 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5753 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5754 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5755 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5756 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5757 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5758 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5760 else if (name == lang_name_c)
5762 current_lang_name = name;
5765 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5768 /* Get out of the current language scope. */
5773 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5774 VARRAY_POP (current_lang_base);
5777 /* Type instantiation routines. */
5779 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5780 matches the TARGET_TYPE. If there is no satisfactory match, return
5781 error_mark_node, and issue an error message if COMPLAIN is
5782 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5783 If TEMPLATE_ONLY, the name of the overloaded function
5784 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5785 template arguments. */
5788 resolve_address_of_overloaded_function (target_type,
5799 tree explicit_targs;
5801 /* Here's what the standard says:
5805 If the name is a function template, template argument deduction
5806 is done, and if the argument deduction succeeds, the deduced
5807 arguments are used to generate a single template function, which
5808 is added to the set of overloaded functions considered.
5810 Non-member functions and static member functions match targets of
5811 type "pointer-to-function" or "reference-to-function." Nonstatic
5812 member functions match targets of type "pointer-to-member
5813 function;" the function type of the pointer to member is used to
5814 select the member function from the set of overloaded member
5815 functions. If a nonstatic member function is selected, the
5816 reference to the overloaded function name is required to have the
5817 form of a pointer to member as described in 5.3.1.
5819 If more than one function is selected, any template functions in
5820 the set are eliminated if the set also contains a non-template
5821 function, and any given template function is eliminated if the
5822 set contains a second template function that is more specialized
5823 than the first according to the partial ordering rules 14.5.5.2.
5824 After such eliminations, if any, there shall remain exactly one
5825 selected function. */
5828 int is_reference = 0;
5829 /* We store the matches in a TREE_LIST rooted here. The functions
5830 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5831 interoperability with most_specialized_instantiation. */
5832 tree matches = NULL_TREE;
5835 /* By the time we get here, we should be seeing only real
5836 pointer-to-member types, not the internal POINTER_TYPE to
5837 METHOD_TYPE representation. */
5838 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5839 && (TREE_CODE (TREE_TYPE (target_type))
5840 == METHOD_TYPE)), 0);
5842 if (TREE_CODE (overload) == COMPONENT_REF)
5843 overload = TREE_OPERAND (overload, 1);
5845 /* Check that the TARGET_TYPE is reasonable. */
5846 if (TYPE_PTRFN_P (target_type))
5848 else if (TYPE_PTRMEMFUNC_P (target_type))
5849 /* This is OK, too. */
5851 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5853 /* This is OK, too. This comes from a conversion to reference
5855 target_type = build_reference_type (target_type);
5862 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5863 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5864 return error_mark_node;
5867 /* If we can find a non-template function that matches, we can just
5868 use it. There's no point in generating template instantiations
5869 if we're just going to throw them out anyhow. But, of course, we
5870 can only do this when we don't *need* a template function. */
5875 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5877 tree fn = OVL_FUNCTION (fns);
5880 if (TREE_CODE (fn) == TEMPLATE_DECL)
5881 /* We're not looking for templates just yet. */
5884 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5886 /* We're looking for a non-static member, and this isn't
5887 one, or vice versa. */
5890 /* See if there's a match. */
5891 fntype = TREE_TYPE (fn);
5893 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5894 else if (!is_reference)
5895 fntype = build_pointer_type (fntype);
5897 if (can_convert_arg (target_type, fntype, fn))
5898 matches = tree_cons (fn, NULL_TREE, matches);
5902 /* Now, if we've already got a match (or matches), there's no need
5903 to proceed to the template functions. But, if we don't have a
5904 match we need to look at them, too. */
5907 tree target_fn_type;
5908 tree target_arg_types;
5909 tree target_ret_type;
5914 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5916 target_fn_type = TREE_TYPE (target_type);
5917 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5918 target_ret_type = TREE_TYPE (target_fn_type);
5920 /* Never do unification on the 'this' parameter. */
5921 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5922 target_arg_types = TREE_CHAIN (target_arg_types);
5924 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5926 tree fn = OVL_FUNCTION (fns);
5928 tree instantiation_type;
5931 if (TREE_CODE (fn) != TEMPLATE_DECL)
5932 /* We're only looking for templates. */
5935 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5937 /* We're not looking for a non-static member, and this is
5938 one, or vice versa. */
5941 /* Try to do argument deduction. */
5942 targs = make_tree_vec (DECL_NTPARMS (fn));
5943 if (fn_type_unification (fn, explicit_targs, targs,
5944 target_arg_types, target_ret_type,
5945 DEDUCE_EXACT, -1) != 0)
5946 /* Argument deduction failed. */
5949 /* Instantiate the template. */
5950 instantiation = instantiate_template (fn, targs);
5951 if (instantiation == error_mark_node)
5952 /* Instantiation failed. */
5955 /* See if there's a match. */
5956 instantiation_type = TREE_TYPE (instantiation);
5958 instantiation_type =
5959 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5960 else if (!is_reference)
5961 instantiation_type = build_pointer_type (instantiation_type);
5962 if (can_convert_arg (target_type, instantiation_type, instantiation))
5963 matches = tree_cons (instantiation, fn, matches);
5966 /* Now, remove all but the most specialized of the matches. */
5969 tree match = most_specialized_instantiation (matches);
5971 if (match != error_mark_node)
5972 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5976 /* Now we should have exactly one function in MATCHES. */
5977 if (matches == NULL_TREE)
5979 /* There were *no* matches. */
5982 error ("no matches converting function `%D' to type `%#T'",
5983 DECL_NAME (OVL_FUNCTION (overload)),
5986 /* print_candidates expects a chain with the functions in
5987 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5988 so why be clever?). */
5989 for (; overload; overload = OVL_NEXT (overload))
5990 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5993 print_candidates (matches);
5995 return error_mark_node;
5997 else if (TREE_CHAIN (matches))
5999 /* There were too many matches. */
6005 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6006 DECL_NAME (OVL_FUNCTION (overload)),
6009 /* Since print_candidates expects the functions in the
6010 TREE_VALUE slot, we flip them here. */
6011 for (match = matches; match; match = TREE_CHAIN (match))
6012 TREE_VALUE (match) = TREE_PURPOSE (match);
6014 print_candidates (matches);
6017 return error_mark_node;
6020 /* Good, exactly one match. Now, convert it to the correct type. */
6021 fn = TREE_PURPOSE (matches);
6023 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6024 && !ptrmem && !flag_ms_extensions)
6026 static int explained;
6029 return error_mark_node;
6031 pedwarn ("assuming pointer to member `%D'", fn);
6034 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6040 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6041 return build_unary_op (ADDR_EXPR, fn, 0);
6044 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6045 will mark the function as addressed, but here we must do it
6047 cxx_mark_addressable (fn);
6053 /* This function will instantiate the type of the expression given in
6054 RHS to match the type of LHSTYPE. If errors exist, then return
6055 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6056 we complain on errors. If we are not complaining, never modify rhs,
6057 as overload resolution wants to try many possible instantiations, in
6058 the hope that at least one will work.
6060 For non-recursive calls, LHSTYPE should be a function, pointer to
6061 function, or a pointer to member function. */
6064 instantiate_type (lhstype, rhs, flags)
6066 tsubst_flags_t flags;
6068 int complain = (flags & tf_error);
6069 int strict = (flags & tf_no_attributes)
6070 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6071 int allow_ptrmem = flags & tf_ptrmem_ok;
6073 flags &= ~tf_ptrmem_ok;
6075 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6078 error ("not enough type information");
6079 return error_mark_node;
6082 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6084 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6087 error ("argument of type `%T' does not match `%T'",
6088 TREE_TYPE (rhs), lhstype);
6089 return error_mark_node;
6092 /* We don't overwrite rhs if it is an overloaded function.
6093 Copying it would destroy the tree link. */
6094 if (TREE_CODE (rhs) != OVERLOAD)
6095 rhs = copy_node (rhs);
6097 /* This should really only be used when attempting to distinguish
6098 what sort of a pointer to function we have. For now, any
6099 arithmetic operation which is not supported on pointers
6100 is rejected as an error. */
6102 switch (TREE_CODE (rhs))
6110 return error_mark_node;
6117 new_rhs = instantiate_type (build_pointer_type (lhstype),
6118 TREE_OPERAND (rhs, 0), flags);
6119 if (new_rhs == error_mark_node)
6120 return error_mark_node;
6122 TREE_TYPE (rhs) = lhstype;
6123 TREE_OPERAND (rhs, 0) = new_rhs;
6128 rhs = copy_node (TREE_OPERAND (rhs, 0));
6129 TREE_TYPE (rhs) = unknown_type_node;
6130 return instantiate_type (lhstype, rhs, flags);
6133 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6136 rhs = TREE_OPERAND (rhs, 1);
6137 if (BASELINK_P (rhs))
6138 return instantiate_type (lhstype, TREE_VALUE (rhs),
6139 flags | allow_ptrmem);
6141 /* This can happen if we are forming a pointer-to-member for a
6143 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6147 case TEMPLATE_ID_EXPR:
6149 tree fns = TREE_OPERAND (rhs, 0);
6150 tree args = TREE_OPERAND (rhs, 1);
6153 resolve_address_of_overloaded_function (lhstype,
6157 /*template_only=*/1,
6163 resolve_address_of_overloaded_function (lhstype,
6167 /*template_only=*/0,
6168 /*explicit_targs=*/NULL_TREE);
6171 /* Now we should have a baselink. */
6172 my_friendly_assert (BASELINK_P (rhs), 990412);
6174 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6177 /* This is too hard for now. */
6179 return error_mark_node;
6184 TREE_OPERAND (rhs, 0)
6185 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6186 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6187 return error_mark_node;
6188 TREE_OPERAND (rhs, 1)
6189 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6190 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6191 return error_mark_node;
6193 TREE_TYPE (rhs) = lhstype;
6197 case TRUNC_DIV_EXPR:
6198 case FLOOR_DIV_EXPR:
6200 case ROUND_DIV_EXPR:
6202 case TRUNC_MOD_EXPR:
6203 case FLOOR_MOD_EXPR:
6205 case ROUND_MOD_EXPR:
6206 case FIX_ROUND_EXPR:
6207 case FIX_FLOOR_EXPR:
6209 case FIX_TRUNC_EXPR:
6225 case PREINCREMENT_EXPR:
6226 case PREDECREMENT_EXPR:
6227 case POSTINCREMENT_EXPR:
6228 case POSTDECREMENT_EXPR:
6230 error ("invalid operation on uninstantiated type");
6231 return error_mark_node;
6233 case TRUTH_AND_EXPR:
6235 case TRUTH_XOR_EXPR:
6242 case TRUTH_ANDIF_EXPR:
6243 case TRUTH_ORIF_EXPR:
6244 case TRUTH_NOT_EXPR:
6246 error ("not enough type information");
6247 return error_mark_node;
6250 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6253 error ("not enough type information");
6254 return error_mark_node;
6256 TREE_OPERAND (rhs, 1)
6257 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6258 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6259 return error_mark_node;
6260 TREE_OPERAND (rhs, 2)
6261 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6262 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6263 return error_mark_node;
6265 TREE_TYPE (rhs) = lhstype;
6269 TREE_OPERAND (rhs, 1)
6270 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6271 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6272 return error_mark_node;
6274 TREE_TYPE (rhs) = lhstype;
6279 if (PTRMEM_OK_P (rhs))
6280 flags |= tf_ptrmem_ok;
6282 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6284 case ENTRY_VALUE_EXPR:
6286 return error_mark_node;
6289 return error_mark_node;
6293 return error_mark_node;
6297 /* Return the name of the virtual function pointer field
6298 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6299 this may have to look back through base types to find the
6300 ultimate field name. (For single inheritance, these could
6301 all be the same name. Who knows for multiple inheritance). */
6304 get_vfield_name (type)
6307 tree binfo = TYPE_BINFO (type);
6310 while (BINFO_BASETYPES (binfo)
6311 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6312 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6313 binfo = BINFO_BASETYPE (binfo, 0);
6315 type = BINFO_TYPE (binfo);
6316 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6317 + TYPE_NAME_LENGTH (type) + 2);
6318 sprintf (buf, VFIELD_NAME_FORMAT,
6319 IDENTIFIER_POINTER (constructor_name (type)));
6320 return get_identifier (buf);
6324 print_class_statistics ()
6326 #ifdef GATHER_STATISTICS
6327 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6328 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6329 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6330 n_build_method_call, n_inner_fields_searched);
6333 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6334 n_vtables, n_vtable_searches);
6335 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6336 n_vtable_entries, n_vtable_elems);
6341 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6342 according to [class]:
6343 The class-name is also inserted
6344 into the scope of the class itself. For purposes of access checking,
6345 the inserted class name is treated as if it were a public member name. */
6348 build_self_reference ()
6350 tree name = constructor_name (current_class_type);
6351 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6354 DECL_NONLOCAL (value) = 1;
6355 DECL_CONTEXT (value) = current_class_type;
6356 DECL_ARTIFICIAL (value) = 1;
6358 if (processing_template_decl)
6359 value = push_template_decl (value);
6361 saved_cas = current_access_specifier;
6362 current_access_specifier = access_public_node;
6363 finish_member_declaration (value);
6364 current_access_specifier = saved_cas;
6367 /* Returns 1 if TYPE contains only padding bytes. */
6370 is_empty_class (type)
6373 if (type == error_mark_node)
6376 if (! IS_AGGR_TYPE (type))
6379 return integer_zerop (CLASSTYPE_SIZE (type));
6382 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6383 a *_TYPE node. NODE can also be a local class. */
6386 get_enclosing_class (type)
6391 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6393 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6396 node = DECL_CONTEXT (node);
6402 node = TYPE_CONTEXT (node);
6412 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6415 is_base_of_enclosing_class (base, type)
6420 if (lookup_base (type, base, ba_any, NULL))
6423 type = get_enclosing_class (type);
6428 /* Note that NAME was looked up while the current class was being
6429 defined and that the result of that lookup was DECL. */
6432 maybe_note_name_used_in_class (name, decl)
6436 splay_tree names_used;
6438 /* If we're not defining a class, there's nothing to do. */
6439 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6442 /* If there's already a binding for this NAME, then we don't have
6443 anything to worry about. */
6444 if (IDENTIFIER_CLASS_VALUE (name))
6447 if (!current_class_stack[current_class_depth - 1].names_used)
6448 current_class_stack[current_class_depth - 1].names_used
6449 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6450 names_used = current_class_stack[current_class_depth - 1].names_used;
6452 splay_tree_insert (names_used,
6453 (splay_tree_key) name,
6454 (splay_tree_value) decl);
6457 /* Note that NAME was declared (as DECL) in the current class. Check
6458 to see that the declaration is legal. */
6461 note_name_declared_in_class (name, decl)
6465 splay_tree names_used;
6468 /* Look to see if we ever used this name. */
6470 = current_class_stack[current_class_depth - 1].names_used;
6474 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6477 /* [basic.scope.class]
6479 A name N used in a class S shall refer to the same declaration
6480 in its context and when re-evaluated in the completed scope of
6482 error ("declaration of `%#D'", decl);
6483 cp_error_at ("changes meaning of `%D' from `%+#D'",
6484 DECL_NAME (OVL_CURRENT (decl)),
6489 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6490 Secondary vtables are merged with primary vtables; this function
6491 will return the VAR_DECL for the primary vtable. */
6494 get_vtbl_decl_for_binfo (binfo)
6499 decl = BINFO_VTABLE (binfo);
6500 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6502 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6504 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6507 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6511 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6512 who's TREE_PURPOSE is the TYPE of the required primary base and
6513 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6516 dfs_get_primary_binfo (binfo, data)
6520 tree cons = (tree) data;
6521 tree primary_base = TREE_PURPOSE (cons);
6523 if (TREE_VIA_VIRTUAL (binfo)
6524 && same_type_p (BINFO_TYPE (binfo), primary_base))
6525 /* This is the right type of binfo, but it might be an unshared
6526 instance, and the shared instance is later in the dfs walk. We
6527 must keep looking. */
6528 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6533 /* Returns the unshared binfo for the primary base of BINFO. Note
6534 that in a complex hierarchy the resulting BINFO may not actually
6535 *be* primary. In particular if the resulting BINFO is a virtual
6536 base, and it occurs elsewhere in the hierarchy, then this
6537 occurrence may not actually be a primary base in the complete
6538 object. Check BINFO_PRIMARY_P to be sure. */
6541 get_primary_binfo (binfo)
6545 tree result = NULL_TREE;
6548 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6552 /* A non-virtual primary base is always a direct base, and easy to
6554 if (!TREE_VIA_VIRTUAL (primary_base))
6558 /* Scan the direct basetypes until we find a base with the same
6559 type as the primary base. */
6560 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6562 tree base_binfo = BINFO_BASETYPE (binfo, i);
6564 if (same_type_p (BINFO_TYPE (base_binfo),
6565 BINFO_TYPE (primary_base)))
6569 /* We should always find the primary base. */
6573 /* For a primary virtual base, we have to scan the entire hierarchy
6574 rooted at BINFO; the virtual base could be an indirect virtual
6575 base. There could be more than one instance of the primary base
6576 in the hierarchy, and if one is the canonical binfo we want that
6577 one. If it exists, it should be the first one we find, but as a
6578 consistency check we find them all and make sure. */
6579 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6580 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6581 virtuals = TREE_VALUE (virtuals);
6583 /* We must have found at least one instance. */
6584 my_friendly_assert (virtuals, 20010612);
6586 if (TREE_CHAIN (virtuals))
6588 /* We found more than one instance of the base. We must make
6589 sure that, if one is the canonical one, it is the first one
6590 we found. As the chain is in reverse dfs order, that means
6591 the last on the list. */
6592 tree complete_binfo;
6595 for (complete_binfo = binfo;
6596 BINFO_INHERITANCE_CHAIN (complete_binfo);
6597 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6599 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6600 BINFO_TYPE (complete_binfo));
6602 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6604 result = TREE_VALUE (virtuals);
6606 if (canonical == result)
6608 /* This is the unshared instance. Make sure it was the
6610 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6616 result = TREE_VALUE (virtuals);
6620 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6623 maybe_indent_hierarchy (stream, indent, indented_p)
6629 fprintf (stream, "%*s", indent, "");
6633 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6634 dominated by T) to stderr. INDENT should be zero when called from
6635 the top level; it is incremented recursively. */
6638 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6648 indented = maybe_indent_hierarchy (stream, indent, 0);
6649 fprintf (stream, "%s (0x%lx) ",
6650 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6651 (unsigned long) binfo);
6652 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6653 tree_low_cst (BINFO_OFFSET (binfo), 0));
6654 if (is_empty_class (BINFO_TYPE (binfo)))
6655 fprintf (stream, " empty");
6656 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6657 fprintf (stream, " nearly-empty");
6658 if (TREE_VIA_VIRTUAL (binfo))
6660 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6662 fprintf (stream, " virtual");
6663 if (canonical == binfo)
6664 fprintf (stream, " canonical");
6666 fprintf (stream, " non-canonical");
6668 fprintf (stream, "\n");
6671 if (BINFO_PRIMARY_BASE_OF (binfo))
6673 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6674 fprintf (stream, " primary-for %s (0x%lx)",
6675 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6676 TFF_PLAIN_IDENTIFIER),
6677 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6679 if (BINFO_LOST_PRIMARY_P (binfo))
6681 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6682 fprintf (stream, " lost-primary");
6685 fprintf (stream, "\n");
6687 if (!(flags & TDF_SLIM))
6691 if (BINFO_SUBVTT_INDEX (binfo))
6693 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6694 fprintf (stream, " subvttidx=%s",
6695 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6696 TFF_PLAIN_IDENTIFIER));
6698 if (BINFO_VPTR_INDEX (binfo))
6700 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6701 fprintf (stream, " vptridx=%s",
6702 expr_as_string (BINFO_VPTR_INDEX (binfo),
6703 TFF_PLAIN_IDENTIFIER));
6705 if (BINFO_VPTR_FIELD (binfo))
6707 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6708 fprintf (stream, " vbaseoffset=%s",
6709 expr_as_string (BINFO_VPTR_FIELD (binfo),
6710 TFF_PLAIN_IDENTIFIER));
6712 if (BINFO_VTABLE (binfo))
6714 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6715 fprintf (stream, " vptr=%s",
6716 expr_as_string (BINFO_VTABLE (binfo),
6717 TFF_PLAIN_IDENTIFIER));
6721 fprintf (stream, "\n");
6725 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6726 dump_class_hierarchy_r (stream, flags,
6727 t, BINFO_BASETYPE (binfo, i),
6731 /* Dump the BINFO hierarchy for T. */
6734 dump_class_hierarchy (t)
6738 FILE *stream = dump_begin (TDI_class, &flags);
6743 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6744 fprintf (stream, " size=%lu align=%lu\n",
6745 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6746 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6747 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6748 fprintf (stream, "\n");
6749 dump_end (TDI_class, stream);
6753 dump_array (stream, decl)
6760 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6762 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6764 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6765 fprintf (stream, " %s entries",
6766 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6767 TFF_PLAIN_IDENTIFIER));
6768 fprintf (stream, "\n");
6770 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6771 inits; ix++, inits = TREE_CHAIN (inits))
6772 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6773 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6777 dump_vtable (t, binfo, vtable)
6783 FILE *stream = dump_begin (TDI_class, &flags);
6788 if (!(flags & TDF_SLIM))
6790 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6792 fprintf (stream, "%s for %s",
6793 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6794 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6797 if (!TREE_VIA_VIRTUAL (binfo))
6798 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6799 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6801 fprintf (stream, "\n");
6802 dump_array (stream, vtable);
6803 fprintf (stream, "\n");
6806 dump_end (TDI_class, stream);
6815 FILE *stream = dump_begin (TDI_class, &flags);
6820 if (!(flags & TDF_SLIM))
6822 fprintf (stream, "VTT for %s\n",
6823 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6824 dump_array (stream, vtt);
6825 fprintf (stream, "\n");
6828 dump_end (TDI_class, stream);
6831 /* Virtual function table initialization. */
6833 /* Create all the necessary vtables for T and its base classes. */
6843 /* We lay out the primary and secondary vtables in one contiguous
6844 vtable. The primary vtable is first, followed by the non-virtual
6845 secondary vtables in inheritance graph order. */
6846 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6847 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6848 TYPE_BINFO (t), t, list);
6850 /* Then come the virtual bases, also in inheritance graph order. */
6851 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6855 if (!TREE_VIA_VIRTUAL (vbase))
6858 /* Although we walk in inheritance order, that might not get the
6860 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6862 accumulate_vtbl_inits (real_base, real_base,
6863 TYPE_BINFO (t), t, list);
6866 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
6867 base classes, for the benefit of the debugging backends. */
6868 for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
6870 tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
6871 if (TREE_VIA_VIRTUAL (base))
6873 vbase = binfo_for_vbase (BINFO_TYPE (base), t);
6874 BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
6878 if (TYPE_BINFO_VTABLE (t))
6879 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6882 /* Initialize the vtable for BINFO with the INITS. */
6885 initialize_vtable (binfo, inits)
6891 layout_vtable_decl (binfo, list_length (inits));
6892 decl = get_vtbl_decl_for_binfo (binfo);
6893 initialize_array (decl, inits);
6894 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6897 /* Initialize DECL (a declaration for a namespace-scope array) with
6901 initialize_array (decl, inits)
6907 context = DECL_CONTEXT (decl);
6908 DECL_CONTEXT (decl) = NULL_TREE;
6909 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6910 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6911 DECL_CONTEXT (decl) = context;
6914 /* Build the VTT (virtual table table) for T.
6915 A class requires a VTT if it has virtual bases.
6918 1 - primary virtual pointer for complete object T
6919 2 - secondary VTTs for each direct non-virtual base of T which requires a
6921 3 - secondary virtual pointers for each direct or indirect base of T which
6922 has virtual bases or is reachable via a virtual path from T.
6923 4 - secondary VTTs for each direct or indirect virtual base of T.
6925 Secondary VTTs look like complete object VTTs without part 4. */
6936 /* Build up the initializers for the VTT. */
6938 index = size_zero_node;
6939 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6941 /* If we didn't need a VTT, we're done. */
6945 /* Figure out the type of the VTT. */
6946 type = build_index_type (size_int (list_length (inits) - 1));
6947 type = build_cplus_array_type (const_ptr_type_node, type);
6949 /* Now, build the VTT object itself. */
6950 vtt = build_vtable (t, get_vtt_name (t), type);
6951 pushdecl_top_level (vtt);
6952 initialize_array (vtt, inits);
6957 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6958 from within some hierarchy which is inherited from the type of BINFO.
6959 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6963 get_original_base (base_binfo, binfo)
6970 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6972 if (TREE_VIA_VIRTUAL (base_binfo))
6973 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6974 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6976 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
6977 if (same_type_p (BINFO_TYPE (base_binfo),
6978 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
6979 return BINFO_BASETYPE (derived, ix);
6984 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6985 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6986 and CHAIN the vtable pointer for this binfo after construction is
6987 complete. VALUE can also be another BINFO, in which case we recurse. */
6990 binfo_ctor_vtable (binfo)
6997 vt = BINFO_VTABLE (binfo);
6998 if (TREE_CODE (vt) == TREE_LIST)
6999 vt = TREE_VALUE (vt);
7000 if (TREE_CODE (vt) == TREE_VEC)
7009 /* Recursively build the VTT-initializer for BINFO (which is in the
7010 hierarchy dominated by T). INITS points to the end of the initializer
7011 list to date. INDEX is the VTT index where the next element will be
7012 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7013 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7014 for virtual bases of T. When it is not so, we build the constructor
7015 vtables for the BINFO-in-T variant. */
7018 build_vtt_inits (binfo, t, inits, index)
7027 tree secondary_vptrs;
7028 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7030 /* We only need VTTs for subobjects with virtual bases. */
7031 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7034 /* We need to use a construction vtable if this is not the primary
7038 build_ctor_vtbl_group (binfo, t);
7040 /* Record the offset in the VTT where this sub-VTT can be found. */
7041 BINFO_SUBVTT_INDEX (binfo) = *index;
7044 /* Add the address of the primary vtable for the complete object. */
7045 init = binfo_ctor_vtable (binfo);
7046 *inits = build_tree_list (NULL_TREE, init);
7047 inits = &TREE_CHAIN (*inits);
7050 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7051 BINFO_VPTR_INDEX (binfo) = *index;
7053 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7055 /* Recursively add the secondary VTTs for non-virtual bases. */
7056 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7058 b = BINFO_BASETYPE (binfo, i);
7059 if (!TREE_VIA_VIRTUAL (b))
7060 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7064 /* Add secondary virtual pointers for all subobjects of BINFO with
7065 either virtual bases or reachable along a virtual path, except
7066 subobjects that are non-virtual primary bases. */
7067 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7068 TREE_TYPE (secondary_vptrs) = *index;
7069 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7070 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7072 dfs_walk_real (binfo,
7073 dfs_build_secondary_vptr_vtt_inits,
7075 dfs_ctor_vtable_bases_queue_p,
7077 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7078 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7081 *index = TREE_TYPE (secondary_vptrs);
7083 /* The secondary vptrs come back in reverse order. After we reverse
7084 them, and add the INITS, the last init will be the first element
7086 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7087 if (secondary_vptrs)
7089 *inits = nreverse (secondary_vptrs);
7090 inits = &TREE_CHAIN (secondary_vptrs);
7091 my_friendly_assert (*inits == NULL_TREE, 20000517);
7094 /* Add the secondary VTTs for virtual bases. */
7096 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7100 if (!TREE_VIA_VIRTUAL (b))
7103 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7104 inits = build_vtt_inits (vbase, t, inits, index);
7109 tree data = tree_cons (t, binfo, NULL_TREE);
7110 VTT_TOP_LEVEL_P (data) = 0;
7111 VTT_MARKED_BINFO_P (data) = 0;
7113 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7114 dfs_ctor_vtable_bases_queue_p,
7121 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7122 for the base in most derived. DATA is a TREE_LIST who's
7123 TREE_CHAIN is the type of the base being
7124 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7125 flag of DATA indicates that this is a constructor vtable. The
7126 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7129 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7141 top_level_p = VTT_TOP_LEVEL_P (l);
7143 SET_BINFO_MARKED (binfo);
7145 /* We don't care about bases that don't have vtables. */
7146 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7149 /* We're only interested in proper subobjects of T. */
7150 if (same_type_p (BINFO_TYPE (binfo), t))
7153 /* We're not interested in non-virtual primary bases. */
7154 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7157 /* If BINFO has virtual bases or is reachable via a virtual path
7158 from T, it'll have a secondary vptr. */
7159 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7160 && !binfo_via_virtual (binfo, t))
7163 /* Record the index where this secondary vptr can be found. */
7164 index = TREE_TYPE (l);
7167 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7168 BINFO_VPTR_INDEX (binfo) = index;
7170 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7171 TYPE_SIZE_UNIT (ptr_type_node));
7173 /* Add the initializer for the secondary vptr itself. */
7174 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7176 /* It's a primary virtual base, and this is not the construction
7177 vtable. Find the base this is primary of in the inheritance graph,
7178 and use that base's vtable now. */
7179 while (BINFO_PRIMARY_BASE_OF (binfo))
7180 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7182 init = binfo_ctor_vtable (binfo);
7183 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7188 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7189 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7190 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7194 dfs_ctor_vtable_bases_queue_p (binfo, data)
7198 if (TREE_VIA_VIRTUAL (binfo))
7199 /* Get the shared version. */
7200 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7202 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7207 /* Called from build_vtt_inits via dfs_walk. After building constructor
7208 vtables and generating the sub-vtt from them, we need to restore the
7209 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7210 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7213 dfs_fixup_binfo_vtbls (binfo, data)
7217 CLEAR_BINFO_MARKED (binfo);
7219 /* We don't care about bases that don't have vtables. */
7220 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7223 /* If we scribbled the construction vtable vptr into BINFO, clear it
7225 if (BINFO_VTABLE (binfo)
7226 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7227 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7228 == TREE_VALUE ((tree) data)))
7229 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7234 /* Build the construction vtable group for BINFO which is in the
7235 hierarchy dominated by T. */
7238 build_ctor_vtbl_group (binfo, t)
7249 /* See if we've already created this construction vtable group. */
7250 id = mangle_ctor_vtbl_for_type (t, binfo);
7251 if (IDENTIFIER_GLOBAL_VALUE (id))
7254 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7255 /* Build a version of VTBL (with the wrong type) for use in
7256 constructing the addresses of secondary vtables in the
7257 construction vtable group. */
7258 vtbl = build_vtable (t, id, ptr_type_node);
7259 list = build_tree_list (vtbl, NULL_TREE);
7260 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7263 /* Add the vtables for each of our virtual bases using the vbase in T
7265 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7267 vbase = TREE_CHAIN (vbase))
7272 if (!TREE_VIA_VIRTUAL (vbase))
7274 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7275 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7277 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7279 inits = TREE_VALUE (list);
7281 /* Figure out the type of the construction vtable. */
7282 type = build_index_type (size_int (list_length (inits) - 1));
7283 type = build_cplus_array_type (vtable_entry_type, type);
7284 TREE_TYPE (vtbl) = type;
7286 /* Initialize the construction vtable. */
7287 pushdecl_top_level (vtbl);
7288 initialize_array (vtbl, inits);
7289 dump_vtable (t, binfo, vtbl);
7292 /* Add the vtbl initializers for BINFO (and its bases other than
7293 non-virtual primaries) to the list of INITS. BINFO is in the
7294 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7295 the constructor the vtbl inits should be accumulated for. (If this
7296 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7297 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7298 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7299 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7300 but are not necessarily the same in terms of layout. */
7303 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7311 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7313 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7314 BINFO_TYPE (orig_binfo)),
7317 /* If it doesn't have a vptr, we don't do anything. */
7318 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7321 /* If we're building a construction vtable, we're not interested in
7322 subobjects that don't require construction vtables. */
7324 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7325 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7328 /* Build the initializers for the BINFO-in-T vtable. */
7330 = chainon (TREE_VALUE (inits),
7331 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7332 rtti_binfo, t, inits));
7334 /* Walk the BINFO and its bases. We walk in preorder so that as we
7335 initialize each vtable we can figure out at what offset the
7336 secondary vtable lies from the primary vtable. We can't use
7337 dfs_walk here because we need to iterate through bases of BINFO
7338 and RTTI_BINFO simultaneously. */
7339 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7341 tree base_binfo = BINFO_BASETYPE (binfo, i);
7343 /* Skip virtual bases. */
7344 if (TREE_VIA_VIRTUAL (base_binfo))
7346 accumulate_vtbl_inits (base_binfo,
7347 BINFO_BASETYPE (orig_binfo, i),
7353 /* Called from accumulate_vtbl_inits. Returns the initializers for
7354 the BINFO vtable. */
7357 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7364 tree inits = NULL_TREE;
7365 tree vtbl = NULL_TREE;
7366 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7369 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7371 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7372 primary virtual base. If it is not the same primary in
7373 the hierarchy of T, we'll need to generate a ctor vtable
7374 for it, to place at its location in T. If it is the same
7375 primary, we still need a VTT entry for the vtable, but it
7376 should point to the ctor vtable for the base it is a
7377 primary for within the sub-hierarchy of RTTI_BINFO.
7379 There are three possible cases:
7381 1) We are in the same place.
7382 2) We are a primary base within a lost primary virtual base of
7384 3) We are primary to something not a base of RTTI_BINFO. */
7386 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7387 tree last = NULL_TREE;
7389 /* First, look through the bases we are primary to for RTTI_BINFO
7390 or a virtual base. */
7391 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7394 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7397 /* If we run out of primary links, keep looking down our
7398 inheritance chain; we might be an indirect primary. */
7400 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7401 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7404 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7405 base B and it is a base of RTTI_BINFO, this is case 2. In
7406 either case, we share our vtable with LAST, i.e. the
7407 derived-most base within B of which we are a primary. */
7409 || (b && binfo_for_vbase (BINFO_TYPE (b),
7410 BINFO_TYPE (rtti_binfo))))
7411 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7412 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7413 binfo_ctor_vtable after everything's been set up. */
7416 /* Otherwise, this is case 3 and we get our own. */
7418 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7426 /* Compute the initializer for this vtable. */
7427 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7430 /* Figure out the position to which the VPTR should point. */
7431 vtbl = TREE_PURPOSE (l);
7432 vtbl = build1 (ADDR_EXPR,
7435 TREE_CONSTANT (vtbl) = 1;
7436 index = size_binop (PLUS_EXPR,
7437 size_int (non_fn_entries),
7438 size_int (list_length (TREE_VALUE (l))));
7439 index = size_binop (MULT_EXPR,
7440 TYPE_SIZE_UNIT (vtable_entry_type),
7442 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7443 TREE_CONSTANT (vtbl) = 1;
7447 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7448 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7449 straighten this out. */
7450 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7451 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7454 /* For an ordinary vtable, set BINFO_VTABLE. */
7455 BINFO_VTABLE (binfo) = vtbl;
7460 /* Construct the initializer for BINFO's virtual function table. BINFO
7461 is part of the hierarchy dominated by T. If we're building a
7462 construction vtable, the ORIG_BINFO is the binfo we should use to
7463 find the actual function pointers to put in the vtable - but they
7464 can be overridden on the path to most-derived in the graph that
7465 ORIG_BINFO belongs. Otherwise,
7466 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7467 BINFO that should be indicated by the RTTI information in the
7468 vtable; it will be a base class of T, rather than T itself, if we
7469 are building a construction vtable.
7471 The value returned is a TREE_LIST suitable for wrapping in a
7472 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7473 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7474 number of non-function entries in the vtable.
7476 It might seem that this function should never be called with a
7477 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7478 base is always subsumed by a derived class vtable. However, when
7479 we are building construction vtables, we do build vtables for
7480 primary bases; we need these while the primary base is being
7484 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7489 int *non_fn_entries_p;
7496 /* Initialize VID. */
7497 memset (&vid, 0, sizeof (vid));
7500 vid.rtti_binfo = rtti_binfo;
7501 vid.last_init = &vid.inits;
7502 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7503 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7504 /* The first vbase or vcall offset is at index -3 in the vtable. */
7505 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7507 /* Add entries to the vtable for RTTI. */
7508 build_rtti_vtbl_entries (binfo, &vid);
7510 /* Create an array for keeping track of the functions we've
7511 processed. When we see multiple functions with the same
7512 signature, we share the vcall offsets. */
7513 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7514 /* Add the vcall and vbase offset entries. */
7515 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7516 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7517 build_vbase_offset_vtbl_entries. */
7518 for (vbase = CLASSTYPE_VBASECLASSES (t);
7520 vbase = TREE_CHAIN (vbase))
7521 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7523 /* If the target requires padding between data entries, add that now. */
7524 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7528 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7533 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7534 add = tree_cons (NULL_TREE, null_pointer_node, add);
7539 if (non_fn_entries_p)
7540 *non_fn_entries_p = list_length (vid.inits);
7542 /* Go through all the ordinary virtual functions, building up
7544 vfun_inits = NULL_TREE;
7545 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7551 tree init = NULL_TREE;
7555 /* If the only definition of this function signature along our
7556 primary base chain is from a lost primary, this vtable slot will
7557 never be used, so just zero it out. This is important to avoid
7558 requiring extra thunks which cannot be generated with the function.
7560 We first check this in update_vtable_entry_for_fn, so we handle
7561 restored primary bases properly; we also need to do it here so we
7562 zero out unused slots in ctor vtables, rather than filling themff
7563 with erroneous values (though harmless, apart from relocation
7565 for (b = binfo; ; b = get_primary_binfo (b))
7567 /* We found a defn before a lost primary; go ahead as normal. */
7568 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7571 /* The nearest definition is from a lost primary; clear the
7573 if (BINFO_LOST_PRIMARY_P (b))
7575 init = size_zero_node;
7582 /* Pull the offset for `this', and the function to call, out of
7584 delta = BV_DELTA (v);
7586 if (BV_USE_VCALL_INDEX_P (v))
7588 vcall_index = BV_VCALL_INDEX (v);
7589 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7592 vcall_index = NULL_TREE;
7594 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7595 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7597 /* You can't call an abstract virtual function; it's abstract.
7598 So, we replace these functions with __pure_virtual. */
7599 if (DECL_PURE_VIRTUAL_P (fn))
7602 /* Take the address of the function, considering it to be of an
7603 appropriate generic type. */
7604 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7605 /* The address of a function can't change. */
7606 TREE_CONSTANT (pfn) = 1;
7608 /* Enter it in the vtable. */
7609 init = build_vtable_entry (delta, vcall_index, pfn);
7612 /* And add it to the chain of initializers. */
7613 if (TARGET_VTABLE_USES_DESCRIPTORS)
7616 if (init == size_zero_node)
7617 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7618 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7620 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7622 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7623 TREE_OPERAND (init, 0),
7624 build_int_2 (i, 0));
7625 TREE_CONSTANT (fdesc) = 1;
7627 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7631 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7634 /* The initializers for virtual functions were built up in reverse
7635 order; straighten them out now. */
7636 vfun_inits = nreverse (vfun_inits);
7638 /* The negative offset initializers are also in reverse order. */
7639 vid.inits = nreverse (vid.inits);
7641 /* Chain the two together. */
7642 return chainon (vid.inits, vfun_inits);
7645 /* Adds to vid->inits the initializers for the vbase and vcall
7646 offsets in BINFO, which is in the hierarchy dominated by T. */
7649 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7651 vtbl_init_data *vid;
7655 /* If this is a derived class, we must first create entries
7656 corresponding to the primary base class. */
7657 b = get_primary_binfo (binfo);
7659 build_vcall_and_vbase_vtbl_entries (b, vid);
7661 /* Add the vbase entries for this base. */
7662 build_vbase_offset_vtbl_entries (binfo, vid);
7663 /* Add the vcall entries for this base. */
7664 build_vcall_offset_vtbl_entries (binfo, vid);
7667 /* Returns the initializers for the vbase offset entries in the vtable
7668 for BINFO (which is part of the class hierarchy dominated by T), in
7669 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7670 where the next vbase offset will go. */
7673 build_vbase_offset_vtbl_entries (binfo, vid)
7675 vtbl_init_data *vid;
7679 tree non_primary_binfo;
7681 /* If there are no virtual baseclasses, then there is nothing to
7683 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7688 /* We might be a primary base class. Go up the inheritance hierarchy
7689 until we find the most derived class of which we are a primary base:
7690 it is the offset of that which we need to use. */
7691 non_primary_binfo = binfo;
7692 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7696 /* If we have reached a virtual base, then it must be a primary
7697 base (possibly multi-level) of vid->binfo, or we wouldn't
7698 have called build_vcall_and_vbase_vtbl_entries for it. But it
7699 might be a lost primary, so just skip down to vid->binfo. */
7700 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7702 non_primary_binfo = vid->binfo;
7706 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7707 if (get_primary_binfo (b) != non_primary_binfo)
7709 non_primary_binfo = b;
7712 /* Go through the virtual bases, adding the offsets. */
7713 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7715 vbase = TREE_CHAIN (vbase))
7720 if (!TREE_VIA_VIRTUAL (vbase))
7723 /* Find the instance of this virtual base in the complete
7725 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7727 /* If we've already got an offset for this virtual base, we
7728 don't need another one. */
7729 if (BINFO_VTABLE_PATH_MARKED (b))
7731 SET_BINFO_VTABLE_PATH_MARKED (b);
7733 /* Figure out where we can find this vbase offset. */
7734 delta = size_binop (MULT_EXPR,
7737 TYPE_SIZE_UNIT (vtable_entry_type)));
7738 if (vid->primary_vtbl_p)
7739 BINFO_VPTR_FIELD (b) = delta;
7741 if (binfo != TYPE_BINFO (t))
7745 /* Find the instance of this virtual base in the type of BINFO. */
7746 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7747 BINFO_TYPE (binfo));
7749 /* The vbase offset had better be the same. */
7750 if (!tree_int_cst_equal (delta,
7751 BINFO_VPTR_FIELD (orig_vbase)))
7755 /* The next vbase will come at a more negative offset. */
7756 vid->index = size_binop (MINUS_EXPR, vid->index,
7757 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7759 /* The initializer is the delta from BINFO to this virtual base.
7760 The vbase offsets go in reverse inheritance-graph order, and
7761 we are walking in inheritance graph order so these end up in
7763 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7766 = build_tree_list (NULL_TREE,
7767 fold (build1 (NOP_EXPR,
7770 vid->last_init = &TREE_CHAIN (*vid->last_init);
7774 /* Adds the initializers for the vcall offset entries in the vtable
7775 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7779 build_vcall_offset_vtbl_entries (binfo, vid)
7781 vtbl_init_data *vid;
7783 /* We only need these entries if this base is a virtual base. */
7784 if (!TREE_VIA_VIRTUAL (binfo))
7787 /* We need a vcall offset for each of the virtual functions in this
7788 vtable. For example:
7790 class A { virtual void f (); };
7791 class B1 : virtual public A { virtual void f (); };
7792 class B2 : virtual public A { virtual void f (); };
7793 class C: public B1, public B2 { virtual void f (); };
7795 A C object has a primary base of B1, which has a primary base of A. A
7796 C also has a secondary base of B2, which no longer has a primary base
7797 of A. So the B2-in-C construction vtable needs a secondary vtable for
7798 A, which will adjust the A* to a B2* to call f. We have no way of
7799 knowing what (or even whether) this offset will be when we define B2,
7800 so we store this "vcall offset" in the A sub-vtable and look it up in
7801 a "virtual thunk" for B2::f.
7803 We need entries for all the functions in our primary vtable and
7804 in our non-virtual bases' secondary vtables. */
7806 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7807 add_vcall_offset_vtbl_entries_r (binfo, vid);
7810 /* Build vcall offsets, starting with those for BINFO. */
7813 add_vcall_offset_vtbl_entries_r (binfo, vid)
7815 vtbl_init_data *vid;
7820 /* Don't walk into virtual bases -- except, of course, for the
7821 virtual base for which we are building vcall offsets. Any
7822 primary virtual base will have already had its offsets generated
7823 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7824 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7827 /* If BINFO has a primary base, process it first. */
7828 primary_binfo = get_primary_binfo (binfo);
7830 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7832 /* Add BINFO itself to the list. */
7833 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7835 /* Scan the non-primary bases of BINFO. */
7836 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7840 base_binfo = BINFO_BASETYPE (binfo, i);
7841 if (base_binfo != primary_binfo)
7842 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7846 /* Called from build_vcall_offset_vtbl_entries_r. */
7849 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7851 vtbl_init_data* vid;
7853 tree derived_virtuals;
7857 /* If BINFO is a primary base, the most derived class which has BINFO as
7858 a primary base; otherwise, just BINFO. */
7859 tree non_primary_binfo;
7861 binfo_inits = NULL_TREE;
7863 /* We might be a primary base class. Go up the inheritance hierarchy
7864 until we find the most derived class of which we are a primary base:
7865 it is the BINFO_VIRTUALS there that we need to consider. */
7866 non_primary_binfo = binfo;
7867 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7871 /* If we have reached a virtual base, then it must be vid->vbase,
7872 because we ignore other virtual bases in
7873 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7874 base (possibly multi-level) of vid->binfo, or we wouldn't
7875 have called build_vcall_and_vbase_vtbl_entries for it. But it
7876 might be a lost primary, so just skip down to vid->binfo. */
7877 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7879 if (non_primary_binfo != vid->vbase)
7881 non_primary_binfo = vid->binfo;
7885 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7886 if (get_primary_binfo (b) != non_primary_binfo)
7888 non_primary_binfo = b;
7891 if (vid->ctor_vtbl_p)
7892 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7893 where rtti_binfo is the most derived type. */
7894 non_primary_binfo = get_original_base
7895 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7897 /* Make entries for the rest of the virtuals. */
7898 for (base_virtuals = BINFO_VIRTUALS (binfo),
7899 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7900 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7902 base_virtuals = TREE_CHAIN (base_virtuals),
7903 derived_virtuals = TREE_CHAIN (derived_virtuals),
7904 orig_virtuals = TREE_CHAIN (orig_virtuals))
7913 /* Find the declaration that originally caused this function to
7914 be present in BINFO_TYPE (binfo). */
7915 orig_fn = BV_FN (orig_virtuals);
7917 /* When processing BINFO, we only want to generate vcall slots for
7918 function slots introduced in BINFO. So don't try to generate
7919 one if the function isn't even defined in BINFO. */
7920 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7923 /* Find the overriding function. */
7924 fn = BV_FN (derived_virtuals);
7926 /* If there is already an entry for a function with the same
7927 signature as FN, then we do not need a second vcall offset.
7928 Check the list of functions already present in the derived
7930 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7934 derived_entry = VARRAY_TREE (vid->fns, i);
7935 if (same_signature_p (BV_FN (derived_entry), fn)
7936 /* We only use one vcall offset for virtual destructors,
7937 even though there are two virtual table entries. */
7938 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7939 && DECL_DESTRUCTOR_P (fn)))
7941 if (!vid->ctor_vtbl_p)
7942 BV_VCALL_INDEX (derived_virtuals)
7943 = BV_VCALL_INDEX (derived_entry);
7947 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7950 /* The FN comes from BASE. So, we must calculate the adjustment from
7951 vid->vbase to BASE. We can just look for BASE in the complete
7952 object because we are converting from a virtual base, so if there
7953 were multiple copies, there would not be a unique final overrider
7954 and vid->derived would be ill-formed. */
7955 base = DECL_CONTEXT (fn);
7956 base_binfo = lookup_base (vid->derived, base, ba_any, NULL);
7958 /* Compute the vcall offset. */
7959 /* As mentioned above, the vbase we're working on is a primary base of
7960 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7961 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7962 vcall_offset = BINFO_OFFSET (vid->binfo);
7963 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7965 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7968 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7969 vid->last_init = &TREE_CHAIN (*vid->last_init);
7971 /* Keep track of the vtable index where this vcall offset can be
7972 found. For a construction vtable, we already made this
7973 annotation when we built the original vtable. */
7974 if (!vid->ctor_vtbl_p)
7975 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7977 /* The next vcall offset will be found at a more negative
7979 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7981 /* Keep track of this function. */
7982 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7986 /* Return vtbl initializers for the RTTI entries coresponding to the
7987 BINFO's vtable. The RTTI entries should indicate the object given
7988 by VID->rtti_binfo. */
7991 build_rtti_vtbl_entries (binfo, vid)
7993 vtbl_init_data *vid;
8002 basetype = BINFO_TYPE (binfo);
8003 t = BINFO_TYPE (vid->rtti_binfo);
8005 /* To find the complete object, we will first convert to our most
8006 primary base, and then add the offset in the vtbl to that value. */
8008 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8009 && !BINFO_LOST_PRIMARY_P (b))
8013 primary_base = get_primary_binfo (b);
8014 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8017 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8019 /* The second entry is the address of the typeinfo object. */
8021 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8023 decl = integer_zero_node;
8025 /* Convert the declaration to a type that can be stored in the
8027 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8028 TREE_CONSTANT (init) = 1;
8029 *vid->last_init = build_tree_list (NULL_TREE, init);
8030 vid->last_init = &TREE_CHAIN (*vid->last_init);
8032 /* Add the offset-to-top entry. It comes earlier in the vtable that
8033 the the typeinfo entry. Convert the offset to look like a
8034 function pointer, so that we can put it in the vtable. */
8035 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8036 TREE_CONSTANT (init) = 1;
8037 *vid->last_init = build_tree_list (NULL_TREE, init);
8038 vid->last_init = &TREE_CHAIN (*vid->last_init);
8041 /* Build an entry in the virtual function table. DELTA is the offset
8042 for the `this' pointer. VCALL_INDEX is the vtable index containing
8043 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8044 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8045 but it may not actually be a virtual function table pointer. (For
8046 example, it might be the address of the RTTI object, under the new
8050 build_vtable_entry (delta, vcall_index, entry)
8055 tree fn = TREE_OPERAND (entry, 0);
8057 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8058 && fn != abort_fndecl)
8060 entry = make_thunk (entry, delta, vcall_index);
8061 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8062 TREE_READONLY (entry) = 1;
8063 TREE_CONSTANT (entry) = 1;
8065 #ifdef GATHER_STATISTICS
8066 n_vtable_entries += 1;