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 import_export_vtable (decl
, class_type
, 0);
518 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
519 or even complete. If this does not exist, create it. If COMPLETE is
520 non-zero, then complete the definition of it -- that will render it
521 impossible to actually build the vtable, but is useful to get at those
522 which are known to exist in the runtime. */
525 get_vtable_decl (type
, complete
)
529 tree name
= get_vtable_name (type
);
530 tree decl
= IDENTIFIER_GLOBAL_VALUE (name
);
534 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
535 && DECL_VIRTUAL_P (decl
), 20000118);
539 decl
= build_vtable (type
, name
, void_type_node
);
540 decl
= pushdecl_top_level (decl
);
541 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name
) == decl
,
544 /* At one time the vtable info was grabbed 2 words at a time. This
545 fails on sparc unless you have 8-byte alignment. (tiemann) */
546 DECL_ALIGN (decl
) = MAX (TYPE_ALIGN (double_type_node
),
551 DECL_EXTERNAL (decl
) = 1;
552 cp_finish_decl (decl
, NULL_TREE
, NULL_TREE
, 0);
558 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
559 BV_VCALL_INDEX for each entry is cleared. */
562 copy_virtuals (binfo
)
568 copies
= copy_list (BINFO_VIRTUALS (binfo
));
569 for (t
= copies
; t
; t
= TREE_CHAIN (t
))
571 BV_VCALL_INDEX (t
) = NULL_TREE
;
572 BV_USE_VCALL_INDEX_P (t
) = 0;
578 /* Build the primary virtual function table for TYPE. If BINFO is
579 non-NULL, build the vtable starting with the initial approximation
580 that it is the same as the one which is the head of the association
581 list. Returns a non-zero value if a new vtable is actually
585 build_primary_vtable (binfo
, type
)
591 decl
= get_vtable_decl (type
, /*complete=*/0);
595 if (BINFO_NEW_VTABLE_MARKED (binfo
, type
))
596 /* We have already created a vtable for this base, so there's
597 no need to do it again. */
600 virtuals
= copy_virtuals (binfo
);
601 TREE_TYPE (decl
) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo
));
602 DECL_SIZE (decl
) = TYPE_SIZE (TREE_TYPE (decl
));
603 DECL_SIZE_UNIT (decl
) = TYPE_SIZE_UNIT (TREE_TYPE (decl
));
607 my_friendly_assert (TREE_CODE (TREE_TYPE (decl
)) == VOID_TYPE
,
609 virtuals
= NULL_TREE
;
612 #ifdef GATHER_STATISTICS
614 n_vtable_elems
+= list_length (virtuals
);
617 /* Initialize the association list for this type, based
618 on our first approximation. */
619 TYPE_BINFO_VTABLE (type
) = decl
;
620 TYPE_BINFO_VIRTUALS (type
) = virtuals
;
621 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type
), type
);
625 /* Give BINFO a new virtual function table which is initialized
626 with a skeleton-copy of its original initialization. The only
627 entry that changes is the `delta' entry, so we can really
628 share a lot of structure.
630 FOR_TYPE is the most derived type which caused this table to
633 Returns non-zero if we haven't met BINFO before.
635 The order in which vtables are built (by calling this function) for
636 an object must remain the same, otherwise a binary incompatibility
640 build_secondary_vtable (binfo
, for_type
)
641 tree binfo
, for_type
;
643 my_friendly_assert (binfo
== CANONICAL_BINFO (binfo
, for_type
), 20010605);
645 if (BINFO_NEW_VTABLE_MARKED (binfo
, for_type
))
646 /* We already created a vtable for this base. There's no need to
650 /* Remember that we've created a vtable for this BINFO, so that we
651 don't try to do so again. */
652 SET_BINFO_NEW_VTABLE_MARKED (binfo
, for_type
);
654 /* Make fresh virtual list, so we can smash it later. */
655 BINFO_VIRTUALS (binfo
) = copy_virtuals (binfo
);
657 /* Secondary vtables are laid out as part of the same structure as
658 the primary vtable. */
659 BINFO_VTABLE (binfo
) = NULL_TREE
;
663 /* Create a new vtable for BINFO which is the hierarchy dominated by
664 T. Return non-zero if we actually created a new vtable. */
667 make_new_vtable (t
, binfo
)
671 if (binfo
== TYPE_BINFO (t
))
672 /* In this case, it is *type*'s vtable we are modifying. We start
673 with the approximation that its vtable is that of the
674 immediate base class. */
675 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
676 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
677 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t
))),
680 /* This is our very own copy of `basetype' to play with. Later,
681 we will fill in all the virtual functions that override the
682 virtual functions in these base classes which are not defined
683 by the current type. */
684 return build_secondary_vtable (binfo
, t
);
687 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
688 (which is in the hierarchy dominated by T) list FNDECL as its
689 BV_FN. DELTA is the required constant adjustment from the `this'
690 pointer where the vtable entry appears to the `this' required when
691 the function is actually called. */
694 modify_vtable_entry (t
, binfo
, fndecl
, delta
, virtuals
)
705 if (fndecl
!= BV_FN (v
)
706 || !tree_int_cst_equal (delta
, BV_DELTA (v
)))
710 /* We need a new vtable for BINFO. */
711 if (make_new_vtable (t
, binfo
))
713 /* If we really did make a new vtable, we also made a copy
714 of the BINFO_VIRTUALS list. Now, we have to find the
715 corresponding entry in that list. */
716 *virtuals
= BINFO_VIRTUALS (binfo
);
717 while (BV_FN (*virtuals
) != BV_FN (v
))
718 *virtuals
= TREE_CHAIN (*virtuals
);
722 base_fndecl
= BV_FN (v
);
723 BV_DELTA (v
) = delta
;
724 BV_VCALL_INDEX (v
) = NULL_TREE
;
727 /* Now assign virtual dispatch information, if unset. We can
728 dispatch this through any overridden base function.
730 FIXME this can choose a secondary vtable if the primary is not
731 also lexically first, leading to useless conversions.
732 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
733 ever be different from DECL_CONTEXT. */
734 if (TREE_CODE (DECL_VINDEX (fndecl
)) != INTEGER_CST
)
736 DECL_VINDEX (fndecl
) = DECL_VINDEX (base_fndecl
);
737 DECL_VIRTUAL_CONTEXT (fndecl
) = DECL_VIRTUAL_CONTEXT (base_fndecl
);
742 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
743 functions present in the vtable so far. */
746 set_vindex (decl
, vfuns_p
)
753 *vfuns_p
+= (TARGET_VTABLE_USES_DESCRIPTORS
754 ? TARGET_VTABLE_USES_DESCRIPTORS
: 1);
755 DECL_VINDEX (decl
) = build_shared_int_cst (vindex
);
758 /* Add a virtual function to all the appropriate vtables for the class
759 T. DECL_VINDEX(X) should be error_mark_node, if we want to
760 allocate a new slot in our table. If it is error_mark_node, we
761 know that no other function from another vtable is overridden by X.
762 VFUNS_P keeps track of how many virtuals there are in our
763 main vtable for the type, and we build upon the NEW_VIRTUALS list
767 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
769 tree
*new_virtuals_p
;
770 tree
*overridden_virtuals_p
;
773 tree t
; /* Structure type. */
777 /* If this function doesn't override anything from a base class, we
778 can just assign it a new DECL_VINDEX now. Otherwise, if it does
779 override something, we keep it around and assign its DECL_VINDEX
780 later, in modify_all_vtables. */
781 if (TREE_CODE (DECL_VINDEX (fndecl
)) == INTEGER_CST
)
782 /* We've already dealt with this function. */
785 new_virtual
= make_node (TREE_LIST
);
786 BV_FN (new_virtual
) = fndecl
;
787 BV_DELTA (new_virtual
) = integer_zero_node
;
789 if (DECL_VINDEX (fndecl
) == error_mark_node
)
791 /* FNDECL is a new virtual function; it doesn't override any
792 virtual function in a base class. */
794 /* We remember that this was the base sub-object for rtti. */
795 CLASSTYPE_RTTI (t
) = t
;
797 /* Now assign virtual dispatch information. */
798 set_vindex (fndecl
, vfuns_p
);
799 DECL_VIRTUAL_CONTEXT (fndecl
) = t
;
801 /* Save the state we've computed on the NEW_VIRTUALS list. */
802 TREE_CHAIN (new_virtual
) = *new_virtuals_p
;
803 *new_virtuals_p
= new_virtual
;
807 /* FNDECL overrides a function from a base class. */
808 TREE_CHAIN (new_virtual
) = *overridden_virtuals_p
;
809 *overridden_virtuals_p
= new_virtual
;
813 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
814 the method after the class has already been defined because a
815 declaration for it was seen. (Even though that is erroneous, we
816 add the method for improved error recovery.) */
819 add_method (type
, method
, error_p
)
824 int using = (DECL_CONTEXT (method
) != type
);
828 int template_conv_p
= (TREE_CODE (method
) == TEMPLATE_DECL
829 && DECL_TEMPLATE_CONV_FN_P (method
));
831 if (!CLASSTYPE_METHOD_VEC (type
))
832 /* Make a new method vector. We start with 8 entries. We must
833 allocate at least two (for constructors and destructors), and
834 we're going to end up with an assignment operator at some point
837 We could use a TREE_LIST for now, and convert it to a TREE_VEC
838 in finish_struct, but we would probably waste more memory
839 making the links in the list than we would by over-allocating
840 the size of the vector here. Furthermore, we would complicate
841 all the code that expects this to be a vector. */
842 CLASSTYPE_METHOD_VEC (type
) = make_tree_vec (8);
844 method_vec
= CLASSTYPE_METHOD_VEC (type
);
845 len
= TREE_VEC_LENGTH (method_vec
);
847 /* Constructors and destructors go in special slots. */
848 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method
))
849 slot
= CLASSTYPE_CONSTRUCTOR_SLOT
;
850 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method
))
851 slot
= CLASSTYPE_DESTRUCTOR_SLOT
;
854 int have_template_convs_p
= 0;
856 /* See if we already have an entry with this name. */
857 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
; slot
< len
; ++slot
)
859 tree m
= TREE_VEC_ELT (method_vec
, slot
);
867 have_template_convs_p
= (TREE_CODE (m
) == TEMPLATE_DECL
868 && DECL_TEMPLATE_CONV_FN_P (m
));
870 /* If we need to move things up, see if there's
872 if (!have_template_convs_p
)
875 if (TREE_VEC_ELT (method_vec
, slot
))
880 if (DECL_NAME (m
) == DECL_NAME (method
))
886 /* We need a bigger method vector. */
890 /* In the non-error case, we are processing a class
891 definition. Double the size of the vector to give room
895 /* In the error case, the vector is already complete. We
896 don't expect many errors, and the rest of the front-end
897 will get confused if there are empty slots in the vector. */
901 new_vec
= make_tree_vec (new_len
);
902 memcpy (&TREE_VEC_ELT (new_vec
, 0), &TREE_VEC_ELT (method_vec
, 0),
903 len
* sizeof (tree
));
905 method_vec
= CLASSTYPE_METHOD_VEC (type
) = new_vec
;
908 if (DECL_CONV_FN_P (method
) && !TREE_VEC_ELT (method_vec
, slot
))
910 /* Type conversion operators have to come before ordinary
911 methods; add_conversions depends on this to speed up
912 looking for conversion operators. So, if necessary, we
913 slide some of the vector elements up. In theory, this
914 makes this algorithm O(N^2) but we don't expect many
915 conversion operators. */
917 slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
;
919 for (slot
= CLASSTYPE_FIRST_CONVERSION_SLOT
; slot
< len
; ++slot
)
921 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
924 /* There are no more entries in the vector, so we
925 can insert the new conversion operator here. */
928 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
929 /* We can insert the new function right at the
934 if (template_conv_p
&& have_template_convs_p
)
936 else if (!TREE_VEC_ELT (method_vec
, slot
))
937 /* There is nothing in the Ith slot, so we can avoid
942 /* We know the last slot in the vector is empty
943 because we know that at this point there's room
944 for a new function. */
945 memmove (&TREE_VEC_ELT (method_vec
, slot
+ 1),
946 &TREE_VEC_ELT (method_vec
, slot
),
947 (len
- slot
- 1) * sizeof (tree
));
948 TREE_VEC_ELT (method_vec
, slot
) = NULL_TREE
;
953 if (template_class_depth (type
))
954 /* TYPE is a template class. Don't issue any errors now; wait
955 until instantiation time to complain. */
961 /* Check to see if we've already got this method. */
962 for (fns
= TREE_VEC_ELT (method_vec
, slot
);
964 fns
= OVL_NEXT (fns
))
966 tree fn
= OVL_CURRENT (fns
);
968 if (TREE_CODE (fn
) != TREE_CODE (method
))
971 if (TREE_CODE (method
) != TEMPLATE_DECL
)
973 /* [over.load] Member function declarations with the
974 same name and the same parameter types cannot be
975 overloaded if any of them is a static member
976 function declaration.
978 [namespace.udecl] When a using-declaration brings names
979 from a base class into a derived class scope, member
980 functions in the derived class override and/or hide member
981 functions with the same name and parameter types in a base
982 class (rather than conflicting). */
983 if ((DECL_STATIC_FUNCTION_P (fn
)
984 != DECL_STATIC_FUNCTION_P (method
))
987 tree parms1
= TYPE_ARG_TYPES (TREE_TYPE (fn
));
988 tree parms2
= TYPE_ARG_TYPES (TREE_TYPE (method
));
991 /* Compare the quals on the 'this' parm. Don't compare
992 the whole types, as used functions are treated as
993 coming from the using class in overload resolution. */
995 && ! DECL_STATIC_FUNCTION_P (fn
)
996 && ! DECL_STATIC_FUNCTION_P (method
)
997 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1
)))
998 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2
)))))
1000 if (! DECL_STATIC_FUNCTION_P (fn
))
1001 parms1
= TREE_CHAIN (parms1
);
1002 if (! DECL_STATIC_FUNCTION_P (method
))
1003 parms2
= TREE_CHAIN (parms2
);
1005 if (same
&& compparms (parms1
, parms2
))
1007 if (using && DECL_CONTEXT (fn
) == type
)
1008 /* Defer to the local function. */
1011 error ("`%#D' and `%#D' cannot be overloaded",
1017 if (!decls_match (fn
, method
))
1020 /* There has already been a declaration of this method
1021 or member template. */
1022 cp_error_at ("`%D' has already been declared in `%T'",
1025 /* We don't call duplicate_decls here to merge the
1026 declarations because that will confuse things if the
1027 methods have inline definitions. In particular, we
1028 will crash while processing the definitions. */
1033 /* Actually insert the new method. */
1034 TREE_VEC_ELT (method_vec
, slot
)
1035 = build_overload (method
, TREE_VEC_ELT (method_vec
, slot
));
1037 /* Add the new binding. */
1038 if (!DECL_CONSTRUCTOR_P (method
)
1039 && !DECL_DESTRUCTOR_P (method
))
1040 push_class_level_binding (DECL_NAME (method
),
1041 TREE_VEC_ELT (method_vec
, slot
));
1044 /* Subroutines of finish_struct. */
1046 /* Look through the list of fields for this struct, deleting
1047 duplicates as we go. This must be recursive to handle
1050 FIELD is the field which may not appear anywhere in FIELDS.
1051 FIELD_PTR, if non-null, is the starting point at which
1052 chained deletions may take place.
1053 The value returned is the first acceptable entry found
1056 Note that anonymous fields which are not of UNION_TYPE are
1057 not duplicates, they are just anonymous fields. This happens
1058 when we have unnamed bitfields, for example. */
1061 delete_duplicate_fields_1 (field
, fields
)
1066 if (DECL_NAME (field
) == 0)
1068 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
1071 for (x
= TYPE_FIELDS (TREE_TYPE (field
)); x
; x
= TREE_CHAIN (x
))
1072 fields
= delete_duplicate_fields_1 (x
, fields
);
1077 for (x
= fields
; x
; prev
= x
, x
= TREE_CHAIN (x
))
1079 if (DECL_NAME (x
) == 0)
1081 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
1083 TYPE_FIELDS (TREE_TYPE (x
))
1084 = delete_duplicate_fields_1 (field
, TYPE_FIELDS (TREE_TYPE (x
)));
1085 if (TYPE_FIELDS (TREE_TYPE (x
)) == 0)
1088 fields
= TREE_CHAIN (fields
);
1090 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1093 else if (TREE_CODE (field
) == USING_DECL
)
1094 /* A using declaration is allowed to appear more than
1095 once. We'll prune these from the field list later, and
1096 handle_using_decl will complain about invalid multiple
1099 else if (DECL_NAME (field
) == DECL_NAME (x
))
1101 if (TREE_CODE (field
) == CONST_DECL
1102 && TREE_CODE (x
) == CONST_DECL
)
1103 cp_error_at ("duplicate enum value `%D'", x
);
1104 else if (TREE_CODE (field
) == CONST_DECL
1105 || TREE_CODE (x
) == CONST_DECL
)
1106 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1108 else if (DECL_DECLARES_TYPE_P (field
)
1109 && DECL_DECLARES_TYPE_P (x
))
1111 if (same_type_p (TREE_TYPE (field
), TREE_TYPE (x
)))
1113 cp_error_at ("duplicate nested type `%D'", x
);
1115 else if (DECL_DECLARES_TYPE_P (field
)
1116 || DECL_DECLARES_TYPE_P (x
))
1118 /* Hide tag decls. */
1119 if ((TREE_CODE (field
) == TYPE_DECL
1120 && DECL_ARTIFICIAL (field
))
1121 || (TREE_CODE (x
) == TYPE_DECL
1122 && DECL_ARTIFICIAL (x
)))
1124 cp_error_at ("duplicate field `%D' (as type and non-type)",
1128 cp_error_at ("duplicate member `%D'", x
);
1130 fields
= TREE_CHAIN (fields
);
1132 TREE_CHAIN (prev
) = TREE_CHAIN (x
);
1140 delete_duplicate_fields (fields
)
1144 for (x
= fields
; x
&& TREE_CHAIN (x
); x
= TREE_CHAIN (x
))
1145 TREE_CHAIN (x
) = delete_duplicate_fields_1 (x
, TREE_CHAIN (x
));
1148 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1149 legit, otherwise return 0. */
1152 alter_access (t
, fdecl
, access
)
1159 if (!DECL_LANG_SPECIFIC (fdecl
))
1160 retrofit_lang_decl (fdecl
);
1162 if (DECL_DISCRIMINATOR_P (fdecl
))
1165 elem
= purpose_member (t
, DECL_ACCESS (fdecl
));
1168 if (TREE_VALUE (elem
) != access
)
1170 if (TREE_CODE (TREE_TYPE (fdecl
)) == FUNCTION_DECL
)
1171 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl
));
1173 error ("conflicting access specifications for field `%s', ignored",
1174 IDENTIFIER_POINTER (DECL_NAME (fdecl
)));
1178 /* They're changing the access to the same thing they changed
1179 it to before. That's OK. */
1185 enforce_access (t
, fdecl
);
1186 DECL_ACCESS (fdecl
) = tree_cons (t
, access
, DECL_ACCESS (fdecl
));
1192 /* Process the USING_DECL, which is a member of T. */
1195 handle_using_decl (using_decl
, t
)
1199 tree ctype
= DECL_INITIAL (using_decl
);
1200 tree name
= DECL_NAME (using_decl
);
1202 = TREE_PRIVATE (using_decl
) ? access_private_node
1203 : TREE_PROTECTED (using_decl
) ? access_protected_node
1204 : access_public_node
;
1206 tree flist
= NULL_TREE
;
1209 binfo
= binfo_or_else (ctype
, t
);
1213 if (name
== constructor_name (ctype
)
1214 || name
== constructor_name_full (ctype
))
1216 cp_error_at ("`%D' names constructor", using_decl
);
1219 if (name
== constructor_name (t
)
1220 || name
== constructor_name_full (t
))
1222 cp_error_at ("`%D' invalid in `%T'", using_decl
, t
);
1226 fdecl
= lookup_member (binfo
, name
, 0, 0);
1230 cp_error_at ("no members matching `%D' in `%#T'", using_decl
, ctype
);
1234 if (BASELINK_P (fdecl
))
1235 /* Ignore base type this came from. */
1236 fdecl
= TREE_VALUE (fdecl
);
1238 old_value
= IDENTIFIER_CLASS_VALUE (name
);
1241 if (is_overloaded_fn (old_value
))
1242 old_value
= OVL_CURRENT (old_value
);
1244 if (DECL_P (old_value
) && DECL_CONTEXT (old_value
) == t
)
1247 old_value
= NULL_TREE
;
1250 if (is_overloaded_fn (fdecl
))
1255 else if (is_overloaded_fn (old_value
))
1258 /* It's OK to use functions from a base when there are functions with
1259 the same name already present in the current class. */;
1262 cp_error_at ("`%D' invalid in `%#T'", using_decl
, t
);
1263 cp_error_at (" because of local method `%#D' with same name",
1264 OVL_CURRENT (old_value
));
1268 else if (!DECL_ARTIFICIAL (old_value
))
1270 cp_error_at ("`%D' invalid in `%#T'", using_decl
, t
);
1271 cp_error_at (" because of local member `%#D' with same name", old_value
);
1275 /* Make type T see field decl FDECL with access ACCESS.*/
1277 for (; flist
; flist
= OVL_NEXT (flist
))
1279 add_method (t
, OVL_CURRENT (flist
), /*error_p=*/0);
1280 alter_access (t
, OVL_CURRENT (flist
), access
);
1283 alter_access (t
, fdecl
, access
);
1286 /* Run through the base clases of T, updating
1287 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1288 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1292 check_bases (t
, cant_have_default_ctor_p
, cant_have_const_ctor_p
,
1295 int *cant_have_default_ctor_p
;
1296 int *cant_have_const_ctor_p
;
1297 int *no_const_asn_ref_p
;
1301 int seen_non_virtual_nearly_empty_base_p
;
1304 binfos
= TYPE_BINFO_BASETYPES (t
);
1305 n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1306 seen_non_virtual_nearly_empty_base_p
= 0;
1308 /* An aggregate cannot have baseclasses. */
1309 CLASSTYPE_NON_AGGREGATE (t
) |= (n_baseclasses
!= 0);
1311 for (i
= 0; i
< n_baseclasses
; ++i
)
1316 /* Figure out what base we're looking at. */
1317 base_binfo
= TREE_VEC_ELT (binfos
, i
);
1318 basetype
= TREE_TYPE (base_binfo
);
1320 /* If the type of basetype is incomplete, then we already
1321 complained about that fact (and we should have fixed it up as
1323 if (!COMPLETE_TYPE_P (basetype
))
1326 /* The base type is of incomplete type. It is
1327 probably best to pretend that it does not
1329 if (i
== n_baseclasses
-1)
1330 TREE_VEC_ELT (binfos
, i
) = NULL_TREE
;
1331 TREE_VEC_LENGTH (binfos
) -= 1;
1333 for (j
= i
; j
+1 < n_baseclasses
; j
++)
1334 TREE_VEC_ELT (binfos
, j
) = TREE_VEC_ELT (binfos
, j
+1);
1338 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1339 here because the case of virtual functions but non-virtual
1340 dtor is handled in finish_struct_1. */
1341 if (warn_ecpp
&& ! TYPE_POLYMORPHIC_P (basetype
)
1342 && TYPE_HAS_DESTRUCTOR (basetype
))
1343 warning ("base class `%#T' has a non-virtual destructor",
1346 /* If the base class doesn't have copy constructors or
1347 assignment operators that take const references, then the
1348 derived class cannot have such a member automatically
1350 if (! TYPE_HAS_CONST_INIT_REF (basetype
))
1351 *cant_have_const_ctor_p
= 1;
1352 if (TYPE_HAS_ASSIGN_REF (basetype
)
1353 && !TYPE_HAS_CONST_ASSIGN_REF (basetype
))
1354 *no_const_asn_ref_p
= 1;
1355 /* Similarly, if the base class doesn't have a default
1356 constructor, then the derived class won't have an
1357 automatically generated default constructor. */
1358 if (TYPE_HAS_CONSTRUCTOR (basetype
)
1359 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype
))
1361 *cant_have_default_ctor_p
= 1;
1362 if (! TYPE_HAS_CONSTRUCTOR (t
))
1363 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1367 if (TREE_VIA_VIRTUAL (base_binfo
))
1368 /* A virtual base does not effect nearly emptiness. */
1370 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1372 if (seen_non_virtual_nearly_empty_base_p
)
1373 /* And if there is more than one nearly empty base, then the
1374 derived class is not nearly empty either. */
1375 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1377 /* Remember we've seen one. */
1378 seen_non_virtual_nearly_empty_base_p
= 1;
1380 else if (!is_empty_class (basetype
))
1381 /* If the base class is not empty or nearly empty, then this
1382 class cannot be nearly empty. */
1383 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
1385 /* A lot of properties from the bases also apply to the derived
1387 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (basetype
);
1388 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
1389 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype
);
1390 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
1391 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype
);
1392 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (basetype
);
1393 TYPE_OVERLOADS_CALL_EXPR (t
) |= TYPE_OVERLOADS_CALL_EXPR (basetype
);
1394 TYPE_OVERLOADS_ARRAY_REF (t
) |= TYPE_OVERLOADS_ARRAY_REF (basetype
);
1395 TYPE_OVERLOADS_ARROW (t
) |= TYPE_OVERLOADS_ARROW (basetype
);
1396 TYPE_POLYMORPHIC_P (t
) |= TYPE_POLYMORPHIC_P (basetype
);
1400 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1401 TO. Move primary information from FROM to TO, and recursively traverse
1402 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1403 assoc list of binfos that have already been reseated. */
1406 force_canonical_binfo_r (to
, from
, type
, mappings
)
1412 int i
, n_baseclasses
= BINFO_N_BASETYPES (from
);
1414 my_friendly_assert (to
!= from
, 20010905);
1415 BINFO_INDIRECT_PRIMARY_P (to
)
1416 = BINFO_INDIRECT_PRIMARY_P (from
);
1417 BINFO_INDIRECT_PRIMARY_P (from
) = 0;
1418 BINFO_UNSHARED_MARKED (to
) = BINFO_UNSHARED_MARKED (from
);
1419 BINFO_UNSHARED_MARKED (from
) = 0;
1420 BINFO_LOST_PRIMARY_P (to
) = BINFO_LOST_PRIMARY_P (from
);
1421 BINFO_LOST_PRIMARY_P (from
) = 0;
1422 if (BINFO_PRIMARY_P (from
))
1424 tree primary
= BINFO_PRIMARY_BASE_OF (from
);
1427 /* We might have just moved the primary base too, see if it's on our
1429 assoc
= purpose_member (primary
, mappings
);
1431 primary
= TREE_VALUE (assoc
);
1432 BINFO_PRIMARY_BASE_OF (to
) = primary
;
1433 BINFO_PRIMARY_BASE_OF (from
) = NULL_TREE
;
1435 my_friendly_assert (same_type_p (BINFO_TYPE (to
), BINFO_TYPE (from
)),
1437 mappings
= tree_cons (from
, to
, mappings
);
1439 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from
))
1440 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from
))))
1442 tree from_primary
= get_primary_binfo (from
);
1444 if (BINFO_PRIMARY_BASE_OF (from_primary
) == from
)
1445 force_canonical_binfo (get_primary_binfo (to
), from_primary
,
1449 for (i
= 0; i
!= n_baseclasses
; i
++)
1451 tree from_binfo
= BINFO_BASETYPE (from
, i
);
1452 tree to_binfo
= BINFO_BASETYPE (to
, i
);
1454 if (TREE_VIA_VIRTUAL (from_binfo
))
1456 if (BINFO_PRIMARY_P (from_binfo
) &&
1457 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo
), mappings
))
1458 /* This base is a primary of some binfo we have already
1459 reseated. We must reseat this one too. */
1460 force_canonical_binfo (to_binfo
, from_binfo
, type
, mappings
);
1463 force_canonical_binfo_r (to_binfo
, from_binfo
, type
, mappings
);
1467 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1468 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1469 MAPPINGS is an assoc list of binfos that have already been reseated.
1470 Adjust any non-virtual bases within FROM, and also move any virtual bases
1471 which are canonical. This complication arises because selecting primary
1472 bases walks in inheritance graph order, but we don't share binfos for
1473 virtual bases, hence we can fill in the primaries for a virtual base,
1474 and then discover that a later base requires the virtual as its
1478 force_canonical_binfo (to
, from
, type
, mappings
)
1484 tree assoc
= purpose_member (BINFO_TYPE (to
),
1485 CLASSTYPE_VBASECLASSES (type
));
1486 if (TREE_VALUE (assoc
) != to
)
1488 TREE_VALUE (assoc
) = to
;
1489 force_canonical_binfo_r (to
, from
, type
, mappings
);
1493 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1494 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1495 otherwise (because something else has already made it primary). */
1498 mark_primary_virtual_base (base_binfo
, type
)
1502 tree shared_binfo
= binfo_for_vbase (BINFO_TYPE (base_binfo
), type
);
1504 if (BINFO_PRIMARY_P (shared_binfo
))
1506 /* It's already allocated in the hierarchy. BINFO won't have a
1507 primary base in this hierarchy, even though the complete object
1508 BINFO is for, would do. */
1512 /* We need to make sure that the assoc list
1513 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1514 primary BINFO for the virtual base, as this is the one
1515 that'll really exist. */
1516 if (base_binfo
!= shared_binfo
)
1517 force_canonical_binfo (base_binfo
, shared_binfo
, type
, NULL
);
1522 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1523 base, then BINFO has no primary base in this graph. Called from
1524 mark_primary_bases. DATA is the most derived type. */
1526 static tree
dfs_unshared_virtual_bases (binfo
, data
)
1530 tree t
= (tree
) data
;
1532 if (!BINFO_UNSHARED_MARKED (binfo
)
1533 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo
)))
1535 /* This morally virtual base has a primary base when it
1536 is a complete object. We need to locate the shared instance
1537 of this binfo in the type dominated by T. We duplicate the
1538 primary base information from there to here. */
1542 for (vbase
= binfo
; !TREE_VIA_VIRTUAL (vbase
);
1543 vbase
= BINFO_INHERITANCE_CHAIN (vbase
))
1545 unshared_base
= get_original_base (binfo
,
1546 binfo_for_vbase (BINFO_TYPE (vbase
),
1548 my_friendly_assert (unshared_base
!= binfo
, 20010612);
1549 BINFO_LOST_PRIMARY_P (binfo
) = BINFO_LOST_PRIMARY_P (unshared_base
);
1550 if (!BINFO_LOST_PRIMARY_P (binfo
))
1551 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo
)) = binfo
;
1554 if (binfo
!= TYPE_BINFO (t
))
1555 /* The vtable fields will have been copied when duplicating the
1556 base binfos. That information is bogus, make sure we don't try
1558 BINFO_VTABLE (binfo
) = NULL_TREE
;
1560 /* If this is a virtual primary base, make sure its offset matches
1561 that which it is primary for. */
1562 if (BINFO_PRIMARY_P (binfo
) && TREE_VIA_VIRTUAL (binfo
) &&
1563 binfo_for_vbase (BINFO_TYPE (binfo
), t
) == binfo
)
1565 tree delta
= size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo
)),
1566 BINFO_OFFSET (binfo
));
1567 if (!integer_zerop (delta
))
1568 propagate_binfo_offsets (binfo
, delta
, t
);
1571 BINFO_UNSHARED_MARKED (binfo
) = 0;
1575 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1576 dominated by TYPE that are primary bases. */
1579 mark_primary_bases (type
)
1584 /* Walk the bases in inheritance graph order. */
1585 for (binfo
= TYPE_BINFO (type
); binfo
; binfo
= TREE_CHAIN (binfo
))
1589 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo
)))
1590 /* Not a dynamic base. */
1593 base_binfo
= get_primary_binfo (binfo
);
1595 if (TREE_VIA_VIRTUAL (base_binfo
))
1596 base_binfo
= mark_primary_virtual_base (base_binfo
, type
);
1599 BINFO_PRIMARY_BASE_OF (base_binfo
) = binfo
;
1601 BINFO_LOST_PRIMARY_P (binfo
) = 1;
1603 BINFO_UNSHARED_MARKED (binfo
) = 1;
1605 /* There could remain unshared morally virtual bases which were not
1606 visited in the inheritance graph walk. These bases will have lost
1607 their virtual primary base (should they have one). We must now
1608 find them. Also we must fix up the BINFO_OFFSETs of primary
1609 virtual bases. We could not do that as we went along, as they
1610 were originally copied from the bases we inherited from by
1611 unshare_base_binfos. That may have decided differently about
1612 where a virtual primary base went. */
1613 dfs_walk (TYPE_BINFO (type
), dfs_unshared_virtual_bases
, NULL
, type
);
1616 /* Make the BINFO the primary base of T. */
1619 set_primary_base (t
, binfo
, vfuns_p
)
1626 CLASSTYPE_PRIMARY_BINFO (t
) = binfo
;
1627 basetype
= BINFO_TYPE (binfo
);
1628 TYPE_BINFO_VTABLE (t
) = TYPE_BINFO_VTABLE (basetype
);
1629 TYPE_BINFO_VIRTUALS (t
) = TYPE_BINFO_VIRTUALS (basetype
);
1630 TYPE_VFIELD (t
) = TYPE_VFIELD (basetype
);
1631 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
1632 *vfuns_p
= CLASSTYPE_VSIZE (basetype
);
1635 /* Determine the primary class for T. */
1638 determine_primary_base (t
, vfuns_p
)
1642 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1646 /* If there are no baseclasses, there is certainly no primary base. */
1647 if (n_baseclasses
== 0)
1650 type_binfo
= TYPE_BINFO (t
);
1652 for (i
= 0; i
< n_baseclasses
; i
++)
1654 tree base_binfo
= BINFO_BASETYPE (type_binfo
, i
);
1655 tree basetype
= BINFO_TYPE (base_binfo
);
1657 if (TYPE_CONTAINS_VPTR_P (basetype
))
1659 /* Even a virtual baseclass can contain our RTTI
1660 information. But, we prefer a non-virtual polymorphic
1662 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1663 CLASSTYPE_RTTI (t
) = CLASSTYPE_RTTI (basetype
);
1665 /* We prefer a non-virtual base, although a virtual one will
1667 if (TREE_VIA_VIRTUAL (base_binfo
))
1670 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1672 set_primary_base (t
, base_binfo
, vfuns_p
);
1673 CLASSTYPE_VFIELDS (t
) = copy_list (CLASSTYPE_VFIELDS (basetype
));
1679 /* Only add unique vfields, and flatten them out as we go. */
1680 for (vfields
= CLASSTYPE_VFIELDS (basetype
);
1682 vfields
= TREE_CHAIN (vfields
))
1683 if (VF_BINFO_VALUE (vfields
) == NULL_TREE
1684 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields
)))
1685 CLASSTYPE_VFIELDS (t
)
1686 = tree_cons (base_binfo
,
1687 VF_BASETYPE_VALUE (vfields
),
1688 CLASSTYPE_VFIELDS (t
));
1693 if (!TYPE_VFIELD (t
))
1694 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
1696 /* Find the indirect primary bases - those virtual bases which are primary
1697 bases of something else in this hierarchy. */
1698 for (vbases
= CLASSTYPE_VBASECLASSES (t
);
1700 vbases
= TREE_CHAIN (vbases
))
1702 tree vbase_binfo
= TREE_VALUE (vbases
);
1704 /* See if this virtual base is an indirect primary base. To be so,
1705 it must be a primary base within the hierarchy of one of our
1707 for (i
= 0; i
< n_baseclasses
; ++i
)
1709 tree basetype
= TYPE_BINFO_BASETYPE (t
, i
);
1712 for (v
= CLASSTYPE_VBASECLASSES (basetype
);
1716 tree base_vbase
= TREE_VALUE (v
);
1718 if (BINFO_PRIMARY_P (base_vbase
)
1719 && same_type_p (BINFO_TYPE (base_vbase
),
1720 BINFO_TYPE (vbase_binfo
)))
1722 BINFO_INDIRECT_PRIMARY_P (vbase_binfo
) = 1;
1727 /* If we've discovered that this virtual base is an indirect
1728 primary base, then we can move on to the next virtual
1730 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo
))
1735 /* A "nearly-empty" virtual base class can be the primary base
1736 class, if no non-virtual polymorphic base can be found. */
1737 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
1739 /* If not NULL, this is the best primary base candidate we have
1741 tree candidate
= NULL_TREE
;
1744 /* Loop over the baseclasses. */
1745 for (base_binfo
= TYPE_BINFO (t
);
1747 base_binfo
= TREE_CHAIN (base_binfo
))
1749 tree basetype
= BINFO_TYPE (base_binfo
);
1751 if (TREE_VIA_VIRTUAL (base_binfo
)
1752 && CLASSTYPE_NEARLY_EMPTY_P (basetype
))
1754 /* If this is not an indirect primary base, then it's
1755 definitely our primary base. */
1756 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo
))
1758 candidate
= base_binfo
;
1762 /* If this is an indirect primary base, it still could be
1763 our primary base -- unless we later find there's another
1764 nearly-empty virtual base that isn't an indirect
1767 candidate
= base_binfo
;
1771 /* If we've got a primary base, use it. */
1774 set_primary_base (t
, candidate
, vfuns_p
);
1775 CLASSTYPE_VFIELDS (t
)
1776 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate
)));
1780 /* Mark the primary base classes at this point. */
1781 mark_primary_bases (t
);
1784 /* Set memoizing fields and bits of T (and its variants) for later
1788 finish_struct_bits (t
)
1791 int i
, n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
1793 /* Fix up variants (if any). */
1794 tree variants
= TYPE_NEXT_VARIANT (t
);
1797 /* These fields are in the _TYPE part of the node, not in
1798 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1799 TYPE_HAS_CONSTRUCTOR (variants
) = TYPE_HAS_CONSTRUCTOR (t
);
1800 TYPE_HAS_DESTRUCTOR (variants
) = TYPE_HAS_DESTRUCTOR (t
);
1801 TYPE_NEEDS_CONSTRUCTING (variants
) = TYPE_NEEDS_CONSTRUCTING (t
);
1802 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants
)
1803 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
);
1805 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants
)
1806 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
);
1807 TYPE_POLYMORPHIC_P (variants
) = TYPE_POLYMORPHIC_P (t
);
1808 TYPE_USES_VIRTUAL_BASECLASSES (variants
) = TYPE_USES_VIRTUAL_BASECLASSES (t
);
1809 /* Copy whatever these are holding today. */
1810 TYPE_MIN_VALUE (variants
) = TYPE_MIN_VALUE (t
);
1811 TYPE_MAX_VALUE (variants
) = TYPE_MAX_VALUE (t
);
1812 TYPE_FIELDS (variants
) = TYPE_FIELDS (t
);
1813 TYPE_SIZE (variants
) = TYPE_SIZE (t
);
1814 TYPE_SIZE_UNIT (variants
) = TYPE_SIZE_UNIT (t
);
1815 variants
= TYPE_NEXT_VARIANT (variants
);
1818 if (n_baseclasses
&& TYPE_POLYMORPHIC_P (t
))
1819 /* For a class w/o baseclasses, `finish_struct' has set
1820 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1821 definition). Similarly for a class whose base classes do not
1822 have vtables. When neither of these is true, we might have
1823 removed abstract virtuals (by providing a definition), added
1824 some (by declaring new ones), or redeclared ones from a base
1825 class. We need to recalculate what's really an abstract virtual
1826 at this point (by looking in the vtables). */
1827 get_pure_virtuals (t
);
1831 /* Notice whether this class has type conversion functions defined. */
1832 tree binfo
= TYPE_BINFO (t
);
1833 tree binfos
= BINFO_BASETYPES (binfo
);
1836 for (i
= n_baseclasses
-1; i
>= 0; i
--)
1838 basetype
= BINFO_TYPE (TREE_VEC_ELT (binfos
, i
));
1840 TYPE_HAS_CONVERSION (t
) |= TYPE_HAS_CONVERSION (basetype
);
1844 /* If this type has a copy constructor or a destructor, force its mode to
1845 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1846 will cause it to be passed by invisible reference and prevent it from
1847 being returned in a register. */
1848 if (! TYPE_HAS_TRIVIAL_INIT_REF (t
) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
))
1851 DECL_MODE (TYPE_MAIN_DECL (t
)) = BLKmode
;
1852 for (variants
= t
; variants
; variants
= TYPE_NEXT_VARIANT (variants
))
1854 TYPE_MODE (variants
) = BLKmode
;
1855 TREE_ADDRESSABLE (variants
) = 1;
1860 /* Issue warnings about T having private constructors, but no friends,
1863 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1864 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1865 non-private static member functions. */
1868 maybe_warn_about_overly_private_class (t
)
1871 int has_member_fn
= 0;
1872 int has_nonprivate_method
= 0;
1875 if (!warn_ctor_dtor_privacy
1876 /* If the class has friends, those entities might create and
1877 access instances, so we should not warn. */
1878 || (CLASSTYPE_FRIEND_CLASSES (t
)
1879 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t
)))
1880 /* We will have warned when the template was declared; there's
1881 no need to warn on every instantiation. */
1882 || CLASSTYPE_TEMPLATE_INSTANTIATION (t
))
1883 /* There's no reason to even consider warning about this
1887 /* We only issue one warning, if more than one applies, because
1888 otherwise, on code like:
1891 // Oops - forgot `public:'
1897 we warn several times about essentially the same problem. */
1899 /* Check to see if all (non-constructor, non-destructor) member
1900 functions are private. (Since there are no friends or
1901 non-private statics, we can't ever call any of the private member
1903 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
1904 /* We're not interested in compiler-generated methods; they don't
1905 provide any way to call private members. */
1906 if (!DECL_ARTIFICIAL (fn
))
1908 if (!TREE_PRIVATE (fn
))
1910 if (DECL_STATIC_FUNCTION_P (fn
))
1911 /* A non-private static member function is just like a
1912 friend; it can create and invoke private member
1913 functions, and be accessed without a class
1917 has_nonprivate_method
= 1;
1920 else if (!DECL_CONSTRUCTOR_P (fn
) && !DECL_DESTRUCTOR_P (fn
))
1924 if (!has_nonprivate_method
&& has_member_fn
)
1926 /* There are no non-private methods, and there's at least one
1927 private member function that isn't a constructor or
1928 destructor. (If all the private members are
1929 constructors/destructors we want to use the code below that
1930 issues error messages specifically referring to
1931 constructors/destructors.) */
1933 tree binfos
= BINFO_BASETYPES (TYPE_BINFO (t
));
1934 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); i
++)
1935 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos
, i
))
1936 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos
, i
)))
1938 has_nonprivate_method
= 1;
1941 if (!has_nonprivate_method
)
1943 warning ("all member functions in class `%T' are private", t
);
1948 /* Even if some of the member functions are non-private, the class
1949 won't be useful for much if all the constructors or destructors
1950 are private: such an object can never be created or destroyed. */
1951 if (TYPE_HAS_DESTRUCTOR (t
))
1953 tree dtor
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1);
1955 if (TREE_PRIVATE (dtor
))
1957 warning ("`%#T' only defines a private destructor and has no friends",
1963 if (TYPE_HAS_CONSTRUCTOR (t
))
1965 int nonprivate_ctor
= 0;
1967 /* If a non-template class does not define a copy
1968 constructor, one is defined for it, enabling it to avoid
1969 this warning. For a template class, this does not
1970 happen, and so we would normally get a warning on:
1972 template <class T> class C { private: C(); };
1974 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1975 complete non-template or fully instantiated classes have this
1977 if (!TYPE_HAS_INIT_REF (t
))
1978 nonprivate_ctor
= 1;
1980 for (fn
= TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 0);
1984 tree ctor
= OVL_CURRENT (fn
);
1985 /* Ideally, we wouldn't count copy constructors (or, in
1986 fact, any constructor that takes an argument of the
1987 class type as a parameter) because such things cannot
1988 be used to construct an instance of the class unless
1989 you already have one. But, for now at least, we're
1991 if (! TREE_PRIVATE (ctor
))
1993 nonprivate_ctor
= 1;
1998 if (nonprivate_ctor
== 0)
2000 warning ("`%#T' only defines private constructors and has no friends",
2007 /* Function to help qsort sort FIELD_DECLs by name order. */
2010 field_decl_cmp (x
, y
)
2013 if (DECL_NAME (*x
) == DECL_NAME (*y
))
2014 /* A nontype is "greater" than a type. */
2015 return DECL_DECLARES_TYPE_P (*y
) - DECL_DECLARES_TYPE_P (*x
);
2016 if (DECL_NAME (*x
) == NULL_TREE
)
2018 if (DECL_NAME (*y
) == NULL_TREE
)
2020 if (DECL_NAME (*x
) < DECL_NAME (*y
))
2025 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2028 method_name_cmp (m1
, m2
)
2029 const tree
*m1
, *m2
;
2031 if (*m1
== NULL_TREE
&& *m2
== NULL_TREE
)
2033 if (*m1
== NULL_TREE
)
2035 if (*m2
== NULL_TREE
)
2037 if (DECL_NAME (OVL_CURRENT (*m1
)) < DECL_NAME (OVL_CURRENT (*m2
)))
2042 /* Warn about duplicate methods in fn_fields. Also compact method
2043 lists so that lookup can be made faster.
2045 Data Structure: List of method lists. The outer list is a
2046 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2047 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2048 links the entire list of methods for TYPE_METHODS. Friends are
2049 chained in the same way as member functions (? TREE_CHAIN or
2050 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2051 list. That allows them to be quickly deleted, and requires no
2054 Sort methods that are not special (i.e., constructors, destructors,
2055 and type conversion operators) so that we can find them faster in
2059 finish_struct_methods (t
)
2066 if (!TYPE_METHODS (t
))
2068 /* Clear these for safety; perhaps some parsing error could set
2069 these incorrectly. */
2070 TYPE_HAS_CONSTRUCTOR (t
) = 0;
2071 TYPE_HAS_DESTRUCTOR (t
) = 0;
2072 CLASSTYPE_METHOD_VEC (t
) = NULL_TREE
;
2076 method_vec
= CLASSTYPE_METHOD_VEC (t
);
2077 my_friendly_assert (method_vec
!= NULL_TREE
, 19991215);
2078 len
= TREE_VEC_LENGTH (method_vec
);
2080 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2081 and the next few with type conversion operators (if any). */
2082 for (fn_fields
= TYPE_METHODS (t
); fn_fields
;
2083 fn_fields
= TREE_CHAIN (fn_fields
))
2084 /* Clear out this flag. */
2085 DECL_IN_AGGR_P (fn_fields
) = 0;
2087 if (TYPE_HAS_DESTRUCTOR (t
) && !CLASSTYPE_DESTRUCTORS (t
))
2088 /* We thought there was a destructor, but there wasn't. Some
2089 parse errors cause this anomalous situation. */
2090 TYPE_HAS_DESTRUCTOR (t
) = 0;
2092 /* Issue warnings about private constructors and such. If there are
2093 no methods, then some public defaults are generated. */
2094 maybe_warn_about_overly_private_class (t
);
2096 /* Now sort the methods. */
2097 while (len
> 2 && TREE_VEC_ELT (method_vec
, len
-1) == NULL_TREE
)
2099 TREE_VEC_LENGTH (method_vec
) = len
;
2101 /* The type conversion ops have to live at the front of the vec, so we
2103 for (slot
= 2; slot
< len
; ++slot
)
2105 tree fn
= TREE_VEC_ELT (method_vec
, slot
);
2107 if (!DECL_CONV_FN_P (OVL_CURRENT (fn
)))
2111 qsort (&TREE_VEC_ELT (method_vec
, slot
), len
-slot
, sizeof (tree
),
2112 (int (*)(const void *, const void *))method_name_cmp
);
2115 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2118 duplicate_tag_error (t
)
2121 error ("redefinition of `%#T'", t
);
2122 cp_error_at ("previous definition of `%#T'", t
);
2124 /* Pretend we haven't defined this type. */
2126 /* All of the component_decl's were TREE_CHAINed together in the parser.
2127 finish_struct_methods walks these chains and assembles all methods with
2128 the same base name into DECL_CHAINs. Now we don't need the parser chains
2129 anymore, so we unravel them. */
2131 /* This used to be in finish_struct, but it turns out that the
2132 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2134 if (CLASSTYPE_METHOD_VEC (t
))
2136 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2137 int i
, len
= TREE_VEC_LENGTH (method_vec
);
2138 for (i
= 0; i
< len
; i
++)
2140 tree unchain
= TREE_VEC_ELT (method_vec
, i
);
2141 while (unchain
!= NULL_TREE
)
2143 TREE_CHAIN (OVL_CURRENT (unchain
)) = NULL_TREE
;
2144 unchain
= OVL_NEXT (unchain
);
2149 if (TYPE_LANG_SPECIFIC (t
))
2151 tree binfo
= TYPE_BINFO (t
);
2152 int interface_only
= CLASSTYPE_INTERFACE_ONLY (t
);
2153 int interface_unknown
= CLASSTYPE_INTERFACE_UNKNOWN (t
);
2154 tree template_info
= CLASSTYPE_TEMPLATE_INFO (t
);
2155 int use_template
= CLASSTYPE_USE_TEMPLATE (t
);
2157 memset ((char *) TYPE_LANG_SPECIFIC (t
), 0, sizeof (struct lang_type
));
2158 BINFO_BASETYPES(binfo
) = NULL_TREE
;
2160 TYPE_LANG_SPECIFIC (t
)->u
.h
.is_lang_type_class
= 1;
2161 TYPE_BINFO (t
) = binfo
;
2162 CLASSTYPE_INTERFACE_ONLY (t
) = interface_only
;
2163 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t
, interface_unknown
);
2164 TYPE_REDEFINED (t
) = 1;
2165 CLASSTYPE_TEMPLATE_INFO (t
) = template_info
;
2166 CLASSTYPE_USE_TEMPLATE (t
) = use_template
;
2168 TYPE_SIZE (t
) = NULL_TREE
;
2169 TYPE_MODE (t
) = VOIDmode
;
2170 TYPE_FIELDS (t
) = NULL_TREE
;
2171 TYPE_METHODS (t
) = NULL_TREE
;
2172 TYPE_VFIELD (t
) = NULL_TREE
;
2173 TYPE_CONTEXT (t
) = NULL_TREE
;
2175 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2176 TYPE_LANG_FLAG_0 (t
) = 0;
2177 TYPE_LANG_FLAG_1 (t
) = 0;
2178 TYPE_LANG_FLAG_2 (t
) = 0;
2179 TYPE_LANG_FLAG_3 (t
) = 0;
2180 TYPE_LANG_FLAG_4 (t
) = 0;
2181 TYPE_LANG_FLAG_5 (t
) = 0;
2182 TYPE_LANG_FLAG_6 (t
) = 0;
2183 /* But not this one. */
2184 SET_IS_AGGR_TYPE (t
, 1);
2187 /* Make BINFO's vtable have N entries, including RTTI entries,
2188 vbase and vcall offsets, etc. Set its type and call the backend
2192 layout_vtable_decl (binfo
, n
)
2199 atype
= build_cplus_array_type (vtable_entry_type
,
2200 build_index_type (size_int (n
- 1)));
2201 layout_type (atype
);
2203 /* We may have to grow the vtable. */
2204 vtable
= get_vtbl_decl_for_binfo (binfo
);
2205 if (!same_type_p (TREE_TYPE (vtable
), atype
))
2207 TREE_TYPE (vtable
) = atype
;
2208 DECL_SIZE (vtable
) = DECL_SIZE_UNIT (vtable
) = NULL_TREE
;
2209 layout_decl (vtable
, 0);
2211 /* At one time the vtable info was grabbed 2 words at a time. This
2212 fails on Sparc unless you have 8-byte alignment. */
2213 DECL_ALIGN (vtable
) = MAX (TYPE_ALIGN (double_type_node
),
2214 DECL_ALIGN (vtable
));
2218 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2219 have the same signature. */
2222 same_signature_p (fndecl
, base_fndecl
)
2223 tree fndecl
, base_fndecl
;
2225 /* One destructor overrides another if they are the same kind of
2227 if (DECL_DESTRUCTOR_P (base_fndecl
) && DECL_DESTRUCTOR_P (fndecl
)
2228 && special_function_p (base_fndecl
) == special_function_p (fndecl
))
2230 /* But a non-destructor never overrides a destructor, nor vice
2231 versa, nor do different kinds of destructors override
2232 one-another. For example, a complete object destructor does not
2233 override a deleting destructor. */
2234 if (DECL_DESTRUCTOR_P (base_fndecl
) || DECL_DESTRUCTOR_P (fndecl
))
2237 if (DECL_NAME (fndecl
) == DECL_NAME (base_fndecl
))
2239 tree types
, base_types
;
2240 types
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
2241 base_types
= TYPE_ARG_TYPES (TREE_TYPE (base_fndecl
));
2242 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types
)))
2243 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types
))))
2244 && compparms (TREE_CHAIN (base_types
), TREE_CHAIN (types
)))
2250 typedef struct find_final_overrider_data_s
{
2251 /* The function for which we are trying to find a final overrider. */
2253 /* The base class in which the function was declared. */
2254 tree declaring_base
;
2255 /* The most derived class in the hierarchy. */
2256 tree most_derived_type
;
2257 /* The final overriding function. */
2259 /* The functions that we thought might be final overriders, but
2262 /* The BINFO for the class in which the final overriding function
2264 tree overriding_base
;
2265 } find_final_overrider_data
;
2267 /* Called from find_final_overrider via dfs_walk. */
2270 dfs_find_final_overrider (binfo
, data
)
2274 find_final_overrider_data
*ffod
= (find_final_overrider_data
*) data
;
2276 if (same_type_p (BINFO_TYPE (binfo
),
2277 BINFO_TYPE (ffod
->declaring_base
))
2278 && tree_int_cst_equal (BINFO_OFFSET (binfo
),
2279 BINFO_OFFSET (ffod
->declaring_base
)))
2284 /* We haven't found an overrider yet. */
2286 /* We've found a path to the declaring base. Walk down the path
2287 looking for an overrider for FN. */
2288 for (path
= reverse_path (binfo
);
2290 path
= TREE_CHAIN (path
))
2292 method
= look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path
)),
2298 /* If we found an overrider, record the overriding function, and
2299 the base from which it came. */
2304 /* Assume the path is non-virtual. See if there are any
2305 virtual bases from (but not including) the overrider up
2306 to and including the base where the function is
2308 for (base
= TREE_CHAIN (path
); base
; base
= TREE_CHAIN (base
))
2309 if (TREE_VIA_VIRTUAL (TREE_VALUE (base
)))
2311 base
= ffod
->declaring_base
;
2315 /* If we didn't already have an overrider, or any
2316 candidates, then this function is the best candidate so
2318 if (!ffod
->overriding_fn
&& !ffod
->candidates
)
2320 ffod
->overriding_fn
= method
;
2321 ffod
->overriding_base
= TREE_VALUE (path
);
2323 else if (ffod
->overriding_fn
)
2325 /* We had a best overrider; let's see how this compares. */
2327 if (ffod
->overriding_fn
== method
2328 && (tree_int_cst_equal
2329 (BINFO_OFFSET (TREE_VALUE (path
)),
2330 BINFO_OFFSET (ffod
->overriding_base
))))
2331 /* We found the same overrider we already have, and in the
2332 same place; it's still the best. */;
2333 else if (strictly_overrides (ffod
->overriding_fn
, method
))
2334 /* The old function overrides this function; it's still the
2336 else if (strictly_overrides (method
, ffod
->overriding_fn
))
2338 /* The new function overrides the old; it's now the
2340 ffod
->overriding_fn
= method
;
2341 ffod
->overriding_base
= TREE_VALUE (path
);
2347 = build_tree_list (NULL_TREE
,
2348 ffod
->overriding_fn
);
2349 if (method
!= ffod
->overriding_fn
)
2351 = tree_cons (NULL_TREE
, method
, ffod
->candidates
);
2352 ffod
->overriding_fn
= NULL_TREE
;
2353 ffod
->overriding_base
= NULL_TREE
;
2358 /* We had a list of ambiguous overrides; let's see how this
2359 new one compares. */
2362 bool incomparable
= false;
2364 /* If there were previous candidates, and this function
2365 overrides all of them, then it is the new best
2367 for (candidates
= ffod
->candidates
;
2369 candidates
= TREE_CHAIN (candidates
))
2371 /* If the candidate overrides the METHOD, then we
2372 needn't worry about it any further. */
2373 if (strictly_overrides (TREE_VALUE (candidates
),
2380 /* If the METHOD doesn't override the candidate,
2381 then it is incomporable. */
2382 if (!strictly_overrides (method
,
2383 TREE_VALUE (candidates
)))
2384 incomparable
= true;
2387 /* If METHOD overrode all the candidates, then it is the
2388 new best candidate. */
2389 if (!candidates
&& !incomparable
)
2391 ffod
->overriding_fn
= method
;
2392 ffod
->overriding_base
= TREE_VALUE (path
);
2393 ffod
->candidates
= NULL_TREE
;
2395 /* If METHOD didn't override all the candidates, then it
2396 is another candidate. */
2397 else if (method
&& incomparable
)
2399 = tree_cons (NULL_TREE
, method
, ffod
->candidates
);
2407 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2408 FN and whose TREE_VALUE is the binfo for the base where the
2409 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2410 base object in which FN is declared. */
2413 find_final_overrider (t
, binfo
, fn
)
2418 find_final_overrider_data ffod
;
2420 /* Getting this right is a little tricky. This is legal:
2422 struct S { virtual void f (); };
2423 struct T { virtual void f (); };
2424 struct U : public S, public T { };
2426 even though calling `f' in `U' is ambiguous. But,
2428 struct R { virtual void f(); };
2429 struct S : virtual public R { virtual void f (); };
2430 struct T : virtual public R { virtual void f (); };
2431 struct U : public S, public T { };
2433 is not -- there's no way to decide whether to put `S::f' or
2434 `T::f' in the vtable for `R'.
2436 The solution is to look at all paths to BINFO. If we find
2437 different overriders along any two, then there is a problem. */
2439 ffod
.declaring_base
= binfo
;
2440 ffod
.most_derived_type
= t
;
2441 ffod
.overriding_fn
= NULL_TREE
;
2442 ffod
.overriding_base
= NULL_TREE
;
2443 ffod
.candidates
= NULL_TREE
;
2445 dfs_walk (TYPE_BINFO (t
),
2446 dfs_find_final_overrider
,
2450 /* If there was no winner, issue an error message. */
2451 if (!ffod
.overriding_fn
)
2453 error ("no unique final overrider for `%D' in `%T'", fn
, t
);
2454 return error_mark_node
;
2457 return build_tree_list (ffod
.overriding_fn
, ffod
.overriding_base
);
2460 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2461 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2462 the function that the slot in T's primary vtable points to. */
2464 static tree get_matching_virtual
PARAMS ((tree
, tree
));
2466 get_matching_virtual (t
, fn
)
2471 for (f
= BINFO_VIRTUALS (TYPE_BINFO (t
)); f
; f
= TREE_CHAIN (f
))
2472 if (same_signature_p (BV_FN (f
), fn
))
2477 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2478 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2479 corresponding position in the BINFO_VIRTUALS list. */
2482 update_vtable_entry_for_fn (t
, binfo
, fn
, virtuals
)
2495 /* Find the nearest primary base (possibly binfo itself) which defines
2496 this function; this is the class the caller will convert to when
2497 calling FN through BINFO. */
2498 for (b
= binfo
; ; b
= get_primary_binfo (b
))
2500 if (look_for_overrides_here (BINFO_TYPE (b
), fn
))
2503 /* The nearest definition is from a lost primary. */
2504 if (BINFO_LOST_PRIMARY_P (b
))
2509 /* Find the final overrider. */
2510 overrider
= find_final_overrider (t
, b
, fn
);
2511 if (overrider
== error_mark_node
)
2514 /* Check for unsupported covariant returns again now that we've
2515 calculated the base offsets. */
2516 check_final_overrider (TREE_PURPOSE (overrider
), fn
);
2518 /* Assume that we will produce a thunk that convert all the way to
2519 the final overrider, and not to an intermediate virtual base. */
2520 virtual_base
= NULL_TREE
;
2522 /* See if we can convert to an intermediate virtual base first, and then
2523 use the vcall offset located there to finish the conversion. */
2524 for (; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
2526 /* If we find the final overrider, then we can stop
2528 if (same_type_p (BINFO_TYPE (b
),
2529 BINFO_TYPE (TREE_VALUE (overrider
))))
2532 /* If we find a virtual base, and we haven't yet found the
2533 overrider, then there is a virtual base between the
2534 declaring base (first_defn) and the final overrider. */
2535 if (!virtual_base
&& TREE_VIA_VIRTUAL (b
))
2539 /* Compute the constant adjustment to the `this' pointer. The
2540 `this' pointer, when this function is called, will point at BINFO
2541 (or one of its primary bases, which are at the same offset). */
2544 /* The `this' pointer needs to be adjusted from the declaration to
2545 the nearest virtual base. */
2546 delta
= size_diffop (BINFO_OFFSET (virtual_base
),
2547 BINFO_OFFSET (first_defn
));
2549 /* If the nearest definition is in a lost primary, we don't need an
2550 entry in our vtable. Except possibly in a constructor vtable,
2551 if we happen to get our primary back. In that case, the offset
2552 will be zero, as it will be a primary base. */
2553 delta
= size_zero_node
;
2556 /* The `this' pointer needs to be adjusted from pointing to
2557 BINFO to pointing at the base where the final overrider
2559 delta
= size_diffop (BINFO_OFFSET (TREE_VALUE (overrider
)),
2560 BINFO_OFFSET (binfo
));
2562 if (! integer_zerop (delta
))
2564 /* We'll need a thunk. But if we have a (perhaps formerly)
2565 primary virtual base, we have a vcall slot for this function,
2566 so we can use it rather than create a non-virtual thunk. */
2568 b
= get_primary_binfo (first_defn
);
2569 for (; b
; b
= get_primary_binfo (b
))
2571 tree f
= get_matching_virtual (BINFO_TYPE (b
), fn
);
2573 /* b doesn't have this function; no suitable vbase. */
2575 if (TREE_VIA_VIRTUAL (b
))
2577 /* Found one; we can treat ourselves as a virtual base. */
2578 virtual_base
= binfo
;
2579 delta
= size_zero_node
;
2586 modify_vtable_entry (t
,
2588 TREE_PURPOSE (overrider
),
2593 BV_USE_VCALL_INDEX_P (*virtuals
) = 1;
2596 /* Called from modify_all_vtables via dfs_walk. */
2599 dfs_modify_vtables (binfo
, data
)
2603 if (/* There's no need to modify the vtable for a non-virtual
2604 primary base; we're not going to use that vtable anyhow.
2605 We do still need to do this for virtual primary bases, as they
2606 could become non-primary in a construction vtable. */
2607 (!BINFO_PRIMARY_P (binfo
) || TREE_VIA_VIRTUAL (binfo
))
2608 /* Similarly, a base without a vtable needs no modification. */
2609 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo
)))
2617 make_new_vtable (t
, binfo
);
2619 /* Now, go through each of the virtual functions in the virtual
2620 function table for BINFO. Find the final overrider, and
2621 update the BINFO_VIRTUALS list appropriately. */
2622 for (virtuals
= BINFO_VIRTUALS (binfo
),
2623 old_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
2625 virtuals
= TREE_CHAIN (virtuals
),
2626 old_virtuals
= TREE_CHAIN (old_virtuals
))
2627 update_vtable_entry_for_fn (t
,
2629 BV_FN (old_virtuals
),
2633 SET_BINFO_MARKED (binfo
);
2638 /* Update all of the primary and secondary vtables for T. Create new
2639 vtables as required, and initialize their RTTI information. Each
2640 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2641 function from a base class; find and modify the appropriate entries
2642 to point to the overriding functions. Returns a list, in
2643 declaration order, of the functions that are overridden in this
2644 class, but do not appear in the primary base class vtable, and
2645 which should therefore be appended to the end of the vtable for T. */
2648 modify_all_vtables (t
, vfuns_p
, overridden_virtuals
)
2651 tree overridden_virtuals
;
2653 tree binfo
= TYPE_BINFO (t
);
2656 /* Update all of the vtables. */
2659 dfs_unmarked_real_bases_queue_p
,
2661 dfs_walk (binfo
, dfs_unmark
, dfs_marked_real_bases_queue_p
, t
);
2663 /* Include overriding functions for secondary vtables in our primary
2665 for (fnsp
= &overridden_virtuals
; *fnsp
; )
2667 tree fn
= TREE_VALUE (*fnsp
);
2669 if (!BINFO_VIRTUALS (binfo
)
2670 || !value_member (fn
, BINFO_VIRTUALS (binfo
)))
2672 /* Set the vtable index. */
2673 set_vindex (fn
, vfuns_p
);
2674 /* We don't need to convert to a base class when calling
2676 DECL_VIRTUAL_CONTEXT (fn
) = t
;
2678 /* We don't need to adjust the `this' pointer when
2679 calling this function. */
2680 BV_DELTA (*fnsp
) = integer_zero_node
;
2681 BV_VCALL_INDEX (*fnsp
) = NULL_TREE
;
2683 /* This is an overridden function not already in our
2685 fnsp
= &TREE_CHAIN (*fnsp
);
2688 /* We've already got an entry for this function. Skip it. */
2689 *fnsp
= TREE_CHAIN (*fnsp
);
2692 return overridden_virtuals
;
2695 /* Here, we already know that they match in every respect.
2696 All we have to check is where they had their declarations.
2698 Return non-zero iff FNDECL1 is declared in a class which has a
2699 proper base class containing FNDECL2. We don't care about
2700 ambiguity or accessibility. */
2703 strictly_overrides (fndecl1
, fndecl2
)
2704 tree fndecl1
, fndecl2
;
2708 return (lookup_base (DECL_CONTEXT (fndecl1
), DECL_CONTEXT (fndecl2
),
2709 ba_ignore
| ba_quiet
, &kind
)
2710 && kind
!= bk_same_type
);
2713 /* Get the base virtual function declarations in T that have the
2717 get_basefndecls (name
, t
)
2721 tree base_fndecls
= NULL_TREE
;
2722 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (t
);
2725 for (methods
= TYPE_METHODS (t
); methods
; methods
= TREE_CHAIN (methods
))
2726 if (TREE_CODE (methods
) == FUNCTION_DECL
2727 && DECL_VINDEX (methods
) != NULL_TREE
2728 && DECL_NAME (methods
) == name
)
2729 base_fndecls
= tree_cons (NULL_TREE
, methods
, base_fndecls
);
2732 return base_fndecls
;
2734 for (i
= 0; i
< n_baseclasses
; i
++)
2736 tree basetype
= TYPE_BINFO_BASETYPE (t
, i
);
2737 base_fndecls
= chainon (get_basefndecls (name
, basetype
),
2741 return base_fndecls
;
2744 /* If this declaration supersedes the declaration of
2745 a method declared virtual in the base class, then
2746 mark this field as being virtual as well. */
2749 check_for_override (decl
, ctype
)
2752 if (TREE_CODE (decl
) == TEMPLATE_DECL
)
2753 /* In [temp.mem] we have:
2755 A specialization of a member function template does not
2756 override a virtual function from a base class. */
2758 if ((DECL_DESTRUCTOR_P (decl
)
2759 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)))
2760 && look_for_overrides (ctype
, decl
)
2761 && !DECL_STATIC_FUNCTION_P (decl
))
2763 /* Set DECL_VINDEX to a value that is neither an
2764 INTEGER_CST nor the error_mark_node so that
2765 add_virtual_function will realize this is an
2766 overriding function. */
2767 DECL_VINDEX (decl
) = decl
;
2769 if (DECL_VIRTUAL_P (decl
))
2771 if (DECL_VINDEX (decl
) == NULL_TREE
)
2772 DECL_VINDEX (decl
) = error_mark_node
;
2773 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl
)) = 1;
2777 /* Warn about hidden virtual functions that are not overridden in t.
2778 We know that constructors and destructors don't apply. */
2784 tree method_vec
= CLASSTYPE_METHOD_VEC (t
);
2785 int n_methods
= method_vec
? TREE_VEC_LENGTH (method_vec
) : 0;
2788 /* We go through each separately named virtual function. */
2789 for (i
= 2; i
< n_methods
&& TREE_VEC_ELT (method_vec
, i
); ++i
)
2797 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2798 have the same name. Figure out what name that is. */
2799 name
= DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
2800 /* There are no possibly hidden functions yet. */
2801 base_fndecls
= NULL_TREE
;
2802 /* Iterate through all of the base classes looking for possibly
2803 hidden functions. */
2804 for (j
= 0; j
< CLASSTYPE_N_BASECLASSES (t
); j
++)
2806 tree basetype
= TYPE_BINFO_BASETYPE (t
, j
);
2807 base_fndecls
= chainon (get_basefndecls (name
, basetype
),
2811 /* If there are no functions to hide, continue. */
2815 /* Remove any overridden functions. */
2816 for (fns
= TREE_VEC_ELT (method_vec
, i
); fns
; fns
= OVL_NEXT (fns
))
2818 fndecl
= OVL_CURRENT (fns
);
2819 if (DECL_VINDEX (fndecl
))
2821 tree
*prev
= &base_fndecls
;
2824 /* If the method from the base class has the same
2825 signature as the method from the derived class, it
2826 has been overridden. */
2827 if (same_signature_p (fndecl
, TREE_VALUE (*prev
)))
2828 *prev
= TREE_CHAIN (*prev
);
2830 prev
= &TREE_CHAIN (*prev
);
2834 /* Now give a warning for all base functions without overriders,
2835 as they are hidden. */
2836 while (base_fndecls
)
2838 /* Here we know it is a hider, and no overrider exists. */
2839 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls
));
2840 cp_warning_at (" by `%D'",
2841 OVL_CURRENT (TREE_VEC_ELT (method_vec
, i
)));
2842 base_fndecls
= TREE_CHAIN (base_fndecls
);
2847 /* Check for things that are invalid. There are probably plenty of other
2848 things we should check for also. */
2851 finish_struct_anon (t
)
2856 for (field
= TYPE_FIELDS (t
); field
; field
= TREE_CHAIN (field
))
2858 if (TREE_STATIC (field
))
2860 if (TREE_CODE (field
) != FIELD_DECL
)
2863 if (DECL_NAME (field
) == NULL_TREE
2864 && ANON_AGGR_TYPE_P (TREE_TYPE (field
)))
2866 tree elt
= TYPE_FIELDS (TREE_TYPE (field
));
2867 for (; elt
; elt
= TREE_CHAIN (elt
))
2869 /* We're generally only interested in entities the user
2870 declared, but we also find nested classes by noticing
2871 the TYPE_DECL that we create implicitly. You're
2872 allowed to put one anonymous union inside another,
2873 though, so we explicitly tolerate that. We use
2874 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2875 we also allow unnamed types used for defining fields. */
2876 if (DECL_ARTIFICIAL (elt
)
2877 && (!DECL_IMPLICIT_TYPEDEF_P (elt
)
2878 || TYPE_ANONYMOUS_P (TREE_TYPE (elt
))))
2881 if (DECL_NAME (elt
) == constructor_name (t
))
2882 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2885 if (TREE_CODE (elt
) != FIELD_DECL
)
2887 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2892 if (TREE_PRIVATE (elt
))
2893 cp_pedwarn_at ("private member `%#D' in anonymous union",
2895 else if (TREE_PROTECTED (elt
))
2896 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2899 TREE_PRIVATE (elt
) = TREE_PRIVATE (field
);
2900 TREE_PROTECTED (elt
) = TREE_PROTECTED (field
);
2906 /* Create default constructors, assignment operators, and so forth for
2907 the type indicated by T, if they are needed.
2908 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2909 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2910 class cannot have a default constructor, copy constructor taking a
2911 const reference argument, or an assignment operator taking a const
2912 reference, respectively. If a virtual destructor is created, its
2913 DECL is returned; otherwise the return value is NULL_TREE. */
2916 add_implicitly_declared_members (t
, cant_have_default_ctor
,
2917 cant_have_const_cctor
,
2918 cant_have_const_assignment
)
2920 int cant_have_default_ctor
;
2921 int cant_have_const_cctor
;
2922 int cant_have_const_assignment
;
2925 tree implicit_fns
= NULL_TREE
;
2926 tree virtual_dtor
= NULL_TREE
;
2929 ++adding_implicit_members
;
2932 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) && !TYPE_HAS_DESTRUCTOR (t
))
2934 default_fn
= implicitly_declare_fn (sfk_destructor
, t
, /*const_p=*/0);
2935 check_for_override (default_fn
, t
);
2937 /* If we couldn't make it work, then pretend we didn't need it. */
2938 if (default_fn
== void_type_node
)
2939 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) = 0;
2942 TREE_CHAIN (default_fn
) = implicit_fns
;
2943 implicit_fns
= default_fn
;
2945 if (DECL_VINDEX (default_fn
))
2946 virtual_dtor
= default_fn
;
2950 /* Any non-implicit destructor is non-trivial. */
2951 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
) |= TYPE_HAS_DESTRUCTOR (t
);
2953 /* Default constructor. */
2954 if (! TYPE_HAS_CONSTRUCTOR (t
) && ! cant_have_default_ctor
)
2956 default_fn
= implicitly_declare_fn (sfk_constructor
, t
, /*const_p=*/0);
2957 TREE_CHAIN (default_fn
) = implicit_fns
;
2958 implicit_fns
= default_fn
;
2961 /* Copy constructor. */
2962 if (! TYPE_HAS_INIT_REF (t
) && ! TYPE_FOR_JAVA (t
))
2964 /* ARM 12.18: You get either X(X&) or X(const X&), but
2967 = implicitly_declare_fn (sfk_copy_constructor
, t
,
2968 /*const_p=*/!cant_have_const_cctor
);
2969 TREE_CHAIN (default_fn
) = implicit_fns
;
2970 implicit_fns
= default_fn
;
2973 /* Assignment operator. */
2974 if (! TYPE_HAS_ASSIGN_REF (t
) && ! TYPE_FOR_JAVA (t
))
2977 = implicitly_declare_fn (sfk_assignment_operator
, t
,
2978 /*const_p=*/!cant_have_const_assignment
);
2979 TREE_CHAIN (default_fn
) = implicit_fns
;
2980 implicit_fns
= default_fn
;
2983 /* Now, hook all of the new functions on to TYPE_METHODS,
2984 and add them to the CLASSTYPE_METHOD_VEC. */
2985 for (f
= &implicit_fns
; *f
; f
= &TREE_CHAIN (*f
))
2986 add_method (t
, *f
, /*error_p=*/0);
2987 *f
= TYPE_METHODS (t
);
2988 TYPE_METHODS (t
) = implicit_fns
;
2990 --adding_implicit_members
;
2992 return virtual_dtor
;
2995 /* Subroutine of finish_struct_1. Recursively count the number of fields
2996 in TYPE, including anonymous union members. */
2999 count_fields (fields
)
3004 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3006 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3007 n_fields
+= count_fields (TYPE_FIELDS (TREE_TYPE (x
)));
3014 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3015 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3018 add_fields_to_vec (fields
, field_vec
, idx
)
3019 tree fields
, field_vec
;
3023 for (x
= fields
; x
; x
= TREE_CHAIN (x
))
3025 if (TREE_CODE (x
) == FIELD_DECL
&& ANON_AGGR_TYPE_P (TREE_TYPE (x
)))
3026 idx
= add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x
)), field_vec
, idx
);
3028 TREE_VEC_ELT (field_vec
, idx
++) = x
;
3033 /* FIELD is a bit-field. We are finishing the processing for its
3034 enclosing type. Issue any appropriate messages and set appropriate
3038 check_bitfield_decl (field
)
3041 tree type
= TREE_TYPE (field
);
3044 /* Detect invalid bit-field type. */
3045 if (DECL_INITIAL (field
)
3046 && ! INTEGRAL_TYPE_P (TREE_TYPE (field
)))
3048 cp_error_at ("bit-field `%#D' with non-integral type", field
);
3049 w
= error_mark_node
;
3052 /* Detect and ignore out of range field width. */
3053 if (DECL_INITIAL (field
))
3055 w
= DECL_INITIAL (field
);
3057 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3060 /* detect invalid field size. */
3061 if (TREE_CODE (w
) == CONST_DECL
)
3062 w
= DECL_INITIAL (w
);
3064 w
= decl_constant_value (w
);
3066 if (TREE_CODE (w
) != INTEGER_CST
)
3068 cp_error_at ("bit-field `%D' width not an integer constant",
3070 w
= error_mark_node
;
3072 else if (tree_int_cst_sgn (w
) < 0)
3074 cp_error_at ("negative width in bit-field `%D'", field
);
3075 w
= error_mark_node
;
3077 else if (integer_zerop (w
) && DECL_NAME (field
) != 0)
3079 cp_error_at ("zero width for bit-field `%D'", field
);
3080 w
= error_mark_node
;
3082 else if (compare_tree_int (w
, TYPE_PRECISION (type
)) > 0
3083 && TREE_CODE (type
) != ENUMERAL_TYPE
3084 && TREE_CODE (type
) != BOOLEAN_TYPE
)
3085 cp_warning_at ("width of `%D' exceeds its type", field
);
3086 else if (TREE_CODE (type
) == ENUMERAL_TYPE
3087 && (0 > compare_tree_int (w
,
3088 min_precision (TYPE_MIN_VALUE (type
),
3089 TREE_UNSIGNED (type
)))
3090 || 0 > compare_tree_int (w
,
3092 (TYPE_MAX_VALUE (type
),
3093 TREE_UNSIGNED (type
)))))
3094 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3098 /* Remove the bit-field width indicator so that the rest of the
3099 compiler does not treat that value as an initializer. */
3100 DECL_INITIAL (field
) = NULL_TREE
;
3102 if (w
!= error_mark_node
)
3104 DECL_SIZE (field
) = convert (bitsizetype
, w
);
3105 DECL_BIT_FIELD (field
) = 1;
3107 if (integer_zerop (w
)
3108 && ! (* targetm
.ms_bitfield_layout_p
) (DECL_FIELD_CONTEXT (field
)))
3110 #ifdef EMPTY_FIELD_BOUNDARY
3111 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3112 EMPTY_FIELD_BOUNDARY
);
3114 #ifdef PCC_BITFIELD_TYPE_MATTERS
3115 if (PCC_BITFIELD_TYPE_MATTERS
)
3117 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3119 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (type
);
3126 /* Non-bit-fields are aligned for their type. */
3127 DECL_BIT_FIELD (field
) = 0;
3128 CLEAR_DECL_C_BIT_FIELD (field
);
3129 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
), TYPE_ALIGN (type
));
3130 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (type
);
3134 /* FIELD is a non bit-field. We are finishing the processing for its
3135 enclosing type T. Issue any appropriate messages and set appropriate
3139 check_field_decl (field
, t
, cant_have_const_ctor
,
3140 cant_have_default_ctor
, no_const_asn_ref
,
3141 any_default_members
)
3144 int *cant_have_const_ctor
;
3145 int *cant_have_default_ctor
;
3146 int *no_const_asn_ref
;
3147 int *any_default_members
;
3149 tree type
= strip_array_types (TREE_TYPE (field
));
3151 /* An anonymous union cannot contain any fields which would change
3152 the settings of CANT_HAVE_CONST_CTOR and friends. */
3153 if (ANON_UNION_TYPE_P (type
))
3155 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3156 structs. So, we recurse through their fields here. */
3157 else if (ANON_AGGR_TYPE_P (type
))
3161 for (fields
= TYPE_FIELDS (type
); fields
; fields
= TREE_CHAIN (fields
))
3162 if (TREE_CODE (fields
) == FIELD_DECL
&& !DECL_C_BIT_FIELD (field
))
3163 check_field_decl (fields
, t
, cant_have_const_ctor
,
3164 cant_have_default_ctor
, no_const_asn_ref
,
3165 any_default_members
);
3167 /* Check members with class type for constructors, destructors,
3169 else if (CLASS_TYPE_P (type
))
3171 /* Never let anything with uninheritable virtuals
3172 make it through without complaint. */
3173 abstract_virtuals_error (field
, type
);
3175 if (TREE_CODE (t
) == UNION_TYPE
)
3177 if (TYPE_NEEDS_CONSTRUCTING (type
))
3178 cp_error_at ("member `%#D' with constructor not allowed in union",
3180 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
3181 cp_error_at ("member `%#D' with destructor not allowed in union",
3183 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type
))
3184 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3189 TYPE_NEEDS_CONSTRUCTING (t
) |= TYPE_NEEDS_CONSTRUCTING (type
);
3190 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t
)
3191 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
);
3192 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type
);
3193 TYPE_HAS_COMPLEX_INIT_REF (t
) |= TYPE_HAS_COMPLEX_INIT_REF (type
);
3196 if (!TYPE_HAS_CONST_INIT_REF (type
))
3197 *cant_have_const_ctor
= 1;
3199 if (!TYPE_HAS_CONST_ASSIGN_REF (type
))
3200 *no_const_asn_ref
= 1;
3202 if (TYPE_HAS_CONSTRUCTOR (type
)
3203 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type
))
3204 *cant_have_default_ctor
= 1;
3206 if (DECL_INITIAL (field
) != NULL_TREE
)
3208 /* `build_class_init_list' does not recognize
3210 if (TREE_CODE (t
) == UNION_TYPE
&& any_default_members
!= 0)
3211 cp_error_at ("multiple fields in union `%T' initialized");
3212 *any_default_members
= 1;
3215 /* Non-bit-fields are aligned for their type, except packed fields
3216 which require only BITS_PER_UNIT alignment. */
3217 DECL_ALIGN (field
) = MAX (DECL_ALIGN (field
),
3218 (DECL_PACKED (field
)
3220 : TYPE_ALIGN (TREE_TYPE (field
))));
3221 if (! DECL_PACKED (field
))
3222 DECL_USER_ALIGN (field
) |= TYPE_USER_ALIGN (TREE_TYPE (field
));
3225 /* Check the data members (both static and non-static), class-scoped
3226 typedefs, etc., appearing in the declaration of T. Issue
3227 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3228 declaration order) of access declarations; each TREE_VALUE in this
3229 list is a USING_DECL.
3231 In addition, set the following flags:
3234 The class is empty, i.e., contains no non-static data members.
3236 CANT_HAVE_DEFAULT_CTOR_P
3237 This class cannot have an implicitly generated default
3240 CANT_HAVE_CONST_CTOR_P
3241 This class cannot have an implicitly generated copy constructor
3242 taking a const reference.
3244 CANT_HAVE_CONST_ASN_REF
3245 This class cannot have an implicitly generated assignment
3246 operator taking a const reference.
3248 All of these flags should be initialized before calling this
3251 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3252 fields can be added by adding to this chain. */
3255 check_field_decls (t
, access_decls
, empty_p
,
3256 cant_have_default_ctor_p
, cant_have_const_ctor_p
,
3261 int *cant_have_default_ctor_p
;
3262 int *cant_have_const_ctor_p
;
3263 int *no_const_asn_ref_p
;
3268 int any_default_members
;
3270 /* First, delete any duplicate fields. */
3271 delete_duplicate_fields (TYPE_FIELDS (t
));
3273 /* Assume there are no access declarations. */
3274 *access_decls
= NULL_TREE
;
3275 /* Assume this class has no pointer members. */
3277 /* Assume none of the members of this class have default
3279 any_default_members
= 0;
3281 for (field
= &TYPE_FIELDS (t
); *field
; field
= next
)
3284 tree type
= TREE_TYPE (x
);
3286 next
= &TREE_CHAIN (x
);
3288 if (TREE_CODE (x
) == FIELD_DECL
)
3290 DECL_PACKED (x
) |= TYPE_PACKED (t
);
3292 if (DECL_C_BIT_FIELD (x
) && integer_zerop (DECL_INITIAL (x
)))
3293 /* We don't treat zero-width bitfields as making a class
3298 /* The class is non-empty. */
3300 /* The class is not even nearly empty. */
3301 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
3305 if (TREE_CODE (x
) == USING_DECL
)
3307 /* Prune the access declaration from the list of fields. */
3308 *field
= TREE_CHAIN (x
);
3310 /* Save the access declarations for our caller. */
3311 *access_decls
= tree_cons (NULL_TREE
, x
, *access_decls
);
3313 /* Since we've reset *FIELD there's no reason to skip to the
3319 if (TREE_CODE (x
) == TYPE_DECL
3320 || TREE_CODE (x
) == TEMPLATE_DECL
)
3323 /* If we've gotten this far, it's a data member, possibly static,
3324 or an enumerator. */
3326 DECL_CONTEXT (x
) = t
;
3328 /* ``A local class cannot have static data members.'' ARM 9.4 */
3329 if (current_function_decl
&& TREE_STATIC (x
))
3330 cp_error_at ("field `%D' in local class cannot be static", x
);
3332 /* Perform error checking that did not get done in
3334 if (TREE_CODE (type
) == FUNCTION_TYPE
)
3336 cp_error_at ("field `%D' invalidly declared function type",
3338 type
= build_pointer_type (type
);
3339 TREE_TYPE (x
) = type
;
3341 else if (TREE_CODE (type
) == METHOD_TYPE
)
3343 cp_error_at ("field `%D' invalidly declared method type", x
);
3344 type
= build_pointer_type (type
);
3345 TREE_TYPE (x
) = type
;
3347 else if (TREE_CODE (type
) == OFFSET_TYPE
)
3349 cp_error_at ("field `%D' invalidly declared offset type", x
);
3350 type
= build_pointer_type (type
);
3351 TREE_TYPE (x
) = type
;
3354 if (type
== error_mark_node
)
3357 /* When this goes into scope, it will be a non-local reference. */
3358 DECL_NONLOCAL (x
) = 1;
3360 if (TREE_CODE (x
) == CONST_DECL
)
3363 if (TREE_CODE (x
) == VAR_DECL
)
3365 if (TREE_CODE (t
) == UNION_TYPE
)
3366 /* Unions cannot have static members. */
3367 cp_error_at ("field `%D' declared static in union", x
);
3372 /* Now it can only be a FIELD_DECL. */
3374 if (TREE_PRIVATE (x
) || TREE_PROTECTED (x
))
3375 CLASSTYPE_NON_AGGREGATE (t
) = 1;
3377 /* If this is of reference type, check if it needs an init.
3378 Also do a little ANSI jig if necessary. */
3379 if (TREE_CODE (type
) == REFERENCE_TYPE
)
3381 CLASSTYPE_NON_POD_P (t
) = 1;
3382 if (DECL_INITIAL (x
) == NULL_TREE
)
3383 CLASSTYPE_REF_FIELDS_NEED_INIT (t
) = 1;
3385 /* ARM $12.6.2: [A member initializer list] (or, for an
3386 aggregate, initialization by a brace-enclosed list) is the
3387 only way to initialize nonstatic const and reference
3389 *cant_have_default_ctor_p
= 1;
3390 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3392 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3393 cp_warning_at ("non-static reference `%#D' in class without a constructor", x
);
3396 type
= strip_array_types (type
);
3398 if (TREE_CODE (type
) == POINTER_TYPE
)
3401 if (DECL_MUTABLE_P (x
) || TYPE_HAS_MUTABLE_P (type
))
3402 CLASSTYPE_HAS_MUTABLE (t
) = 1;
3404 if (! pod_type_p (type
))
3405 /* DR 148 now allows pointers to members (which are POD themselves),
3406 to be allowed in POD structs. */
3407 CLASSTYPE_NON_POD_P (t
) = 1;
3409 if (! zero_init_p (type
))
3410 CLASSTYPE_NON_ZERO_INIT_P (t
) = 1;
3412 /* If any field is const, the structure type is pseudo-const. */
3413 if (CP_TYPE_CONST_P (type
))
3415 C_TYPE_FIELDS_READONLY (t
) = 1;
3416 if (DECL_INITIAL (x
) == NULL_TREE
)
3417 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
) = 1;
3419 /* ARM $12.6.2: [A member initializer list] (or, for an
3420 aggregate, initialization by a brace-enclosed list) is the
3421 only way to initialize nonstatic const and reference
3423 *cant_have_default_ctor_p
= 1;
3424 TYPE_HAS_COMPLEX_ASSIGN_REF (t
) = 1;
3426 if (! TYPE_HAS_CONSTRUCTOR (t
) && extra_warnings
)
3427 cp_warning_at ("non-static const member `%#D' in class without a constructor", x
);
3429 /* A field that is pseudo-const makes the structure likewise. */
3430 else if (IS_AGGR_TYPE (type
))
3432 C_TYPE_FIELDS_READONLY (t
) |= C_TYPE_FIELDS_READONLY (type
);
3433 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t
)
3434 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type
);
3437 /* Core issue 80: A nonstatic data member is required to have a
3438 different name from the class iff the class has a
3439 user-defined constructor. */
3440 if (DECL_NAME (x
) == constructor_name (t
)
3441 && TYPE_HAS_CONSTRUCTOR (t
))
3442 cp_pedwarn_at ("field `%#D' with same name as class", x
);
3444 /* We set DECL_C_BIT_FIELD in grokbitfield.
3445 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3446 if (DECL_C_BIT_FIELD (x
))
3447 check_bitfield_decl (x
);
3449 check_field_decl (x
, t
,
3450 cant_have_const_ctor_p
,
3451 cant_have_default_ctor_p
,
3453 &any_default_members
);
3456 /* Effective C++ rule 11. */
3457 if (has_pointers
&& warn_ecpp
&& TYPE_HAS_CONSTRUCTOR (t
)
3458 && ! (TYPE_HAS_INIT_REF (t
) && TYPE_HAS_ASSIGN_REF (t
)))
3460 warning ("`%#T' has pointer data members", t
);
3462 if (! TYPE_HAS_INIT_REF (t
))
3464 warning (" but does not override `%T(const %T&)'", t
, t
);
3465 if (! TYPE_HAS_ASSIGN_REF (t
))
3466 warning (" or `operator=(const %T&)'", t
);
3468 else if (! TYPE_HAS_ASSIGN_REF (t
))
3469 warning (" but does not override `operator=(const %T&)'", t
);
3473 /* Check anonymous struct/anonymous union fields. */
3474 finish_struct_anon (t
);
3476 /* We've built up the list of access declarations in reverse order.
3478 *access_decls
= nreverse (*access_decls
);
3481 /* If TYPE is an empty class type, records its OFFSET in the table of
3485 record_subobject_offset (type
, offset
, offsets
)
3492 if (!is_empty_class (type
))
3495 /* Record the location of this empty object in OFFSETS. */
3496 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3498 n
= splay_tree_insert (offsets
,
3499 (splay_tree_key
) offset
,
3500 (splay_tree_value
) NULL_TREE
);
3501 n
->value
= ((splay_tree_value
)
3502 tree_cons (NULL_TREE
,
3509 /* Returns non-zero if TYPE is an empty class type and there is
3510 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3513 check_subobject_offset (type
, offset
, offsets
)
3521 if (!is_empty_class (type
))
3524 /* Record the location of this empty object in OFFSETS. */
3525 n
= splay_tree_lookup (offsets
, (splay_tree_key
) offset
);
3529 for (t
= (tree
) n
->value
; t
; t
= TREE_CHAIN (t
))
3530 if (same_type_p (TREE_VALUE (t
), type
))
3536 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3537 F for every subobject, passing it the type, offset, and table of
3538 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3539 bases should be traversed; otherwise, they are ignored.
3541 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3542 than MAX_OFFSET will not be walked.
3544 If F returns a non-zero value, the traversal ceases, and that value
3545 is returned. Otherwise, returns zero. */
3548 walk_subobject_offsets (type
, f
, offset
, offsets
, max_offset
, vbases_p
)
3550 subobject_offset_fn f
;
3558 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3560 if (max_offset
&& INT_CST_LT (max_offset
, offset
))
3563 if (CLASS_TYPE_P (type
))
3568 /* Record the location of TYPE. */
3569 r
= (*f
) (type
, offset
, offsets
);
3573 /* Iterate through the direct base classes of TYPE. */
3574 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (type
); ++i
)
3576 tree binfo
= BINFO_BASETYPE (TYPE_BINFO (type
), i
);
3579 && TREE_VIA_VIRTUAL (binfo
)
3580 && !BINFO_PRIMARY_P (binfo
))
3583 r
= walk_subobject_offsets (BINFO_TYPE (binfo
),
3585 size_binop (PLUS_EXPR
,
3587 BINFO_OFFSET (binfo
)),
3595 /* Iterate through the fields of TYPE. */
3596 for (field
= TYPE_FIELDS (type
); field
; field
= TREE_CHAIN (field
))
3597 if (TREE_CODE (field
) == FIELD_DECL
)
3599 r
= walk_subobject_offsets (TREE_TYPE (field
),
3601 size_binop (PLUS_EXPR
,
3603 DECL_FIELD_OFFSET (field
)),
3611 else if (TREE_CODE (type
) == ARRAY_TYPE
)
3613 tree domain
= TYPE_DOMAIN (type
);
3616 /* Step through each of the elements in the array. */
3617 for (index
= size_zero_node
;
3618 INT_CST_LT (index
, TYPE_MAX_VALUE (domain
));
3619 index
= size_binop (PLUS_EXPR
, index
, size_one_node
))
3621 r
= walk_subobject_offsets (TREE_TYPE (type
),
3629 offset
= size_binop (PLUS_EXPR
, offset
,
3630 TYPE_SIZE_UNIT (TREE_TYPE (type
)));
3631 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3632 there's no point in iterating through the remaining
3633 elements of the array. */
3634 if (max_offset
&& INT_CST_LT (max_offset
, offset
))
3642 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3643 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3647 record_subobject_offsets (type
, offset
, offsets
, vbases_p
)
3653 walk_subobject_offsets (type
, record_subobject_offset
, offset
,
3654 offsets
, /*max_offset=*/NULL_TREE
, vbases_p
);
3657 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3658 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3659 virtual bases of TYPE are examined. */
3662 layout_conflict_p (type
, offset
, offsets
, vbases_p
)
3668 splay_tree_node max_node
;
3670 /* Get the node in OFFSETS that indicates the maximum offset where
3671 an empty subobject is located. */
3672 max_node
= splay_tree_max (offsets
);
3673 /* If there aren't any empty subobjects, then there's no point in
3674 performing this check. */
3678 return walk_subobject_offsets (type
, check_subobject_offset
, offset
,
3679 offsets
, (tree
) (max_node
->key
),
3683 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3684 non-static data member of the type indicated by RLI. BINFO is the
3685 binfo corresponding to the base subobject, OFFSETS maps offsets to
3686 types already located at those offsets. T is the most derived
3687 type. This function determines the position of the DECL. */
3690 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
, t
)
3691 record_layout_info rli
;
3697 tree offset
= NULL_TREE
;
3698 tree type
= TREE_TYPE (decl
);
3699 /* If we are laying out a base class, rather than a field, then
3700 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3701 int field_p
= !DECL_ARTIFICIAL (decl
);
3703 /* Try to place the field. It may take more than one try if we have
3704 a hard time placing the field without putting two objects of the
3705 same type at the same address. */
3708 struct record_layout_info_s old_rli
= *rli
;
3710 /* Place this field. */
3711 place_field (rli
, decl
);
3712 offset
= byte_position (decl
);
3714 /* We have to check to see whether or not there is already
3715 something of the same type at the offset we're about to use.
3719 struct T : public S { int i; };
3720 struct U : public S, public T {};
3722 Here, we put S at offset zero in U. Then, we can't put T at
3723 offset zero -- its S component would be at the same address
3724 as the S we already allocated. So, we have to skip ahead.
3725 Since all data members, including those whose type is an
3726 empty class, have non-zero size, any overlap can happen only
3727 with a direct or indirect base-class -- it can't happen with
3729 if (layout_conflict_p (TREE_TYPE (decl
),
3734 /* Strip off the size allocated to this field. That puts us
3735 at the first place we could have put the field with
3736 proper alignment. */
3739 /* Bump up by the alignment required for the type. */
3741 = size_binop (PLUS_EXPR
, rli
->bitpos
,
3743 ? CLASSTYPE_ALIGN (type
)
3744 : TYPE_ALIGN (type
)));
3745 normalize_rli (rli
);
3748 /* There was no conflict. We're done laying out this field. */
3752 /* Now that we know where it will be placed, update its
3754 if (binfo
&& CLASS_TYPE_P (BINFO_TYPE (binfo
)))
3755 propagate_binfo_offsets (binfo
,
3756 convert (ssizetype
, offset
), t
);
3759 /* Layout the empty base BINFO. EOC indicates the byte currently just
3760 past the end of the class, and should be correctly aligned for a
3761 class of the type indicated by BINFO; OFFSETS gives the offsets of
3762 the empty bases allocated so far. T is the most derived
3763 type. Return non-zero iff we added it at the end. */
3766 layout_empty_base (binfo
, eoc
, offsets
, t
)
3773 tree basetype
= BINFO_TYPE (binfo
);
3776 /* This routine should only be used for empty classes. */
3777 my_friendly_assert (is_empty_class (basetype
), 20000321);
3778 alignment
= ssize_int (CLASSTYPE_ALIGN_UNIT (basetype
));
3780 /* This is an empty base class. We first try to put it at offset
3782 if (layout_conflict_p (BINFO_TYPE (binfo
),
3783 BINFO_OFFSET (binfo
),
3787 /* That didn't work. Now, we move forward from the next
3788 available spot in the class. */
3790 propagate_binfo_offsets (binfo
, convert (ssizetype
, eoc
), t
);
3793 if (!layout_conflict_p (BINFO_TYPE (binfo
),
3794 BINFO_OFFSET (binfo
),
3797 /* We finally found a spot where there's no overlap. */
3800 /* There's overlap here, too. Bump along to the next spot. */
3801 propagate_binfo_offsets (binfo
, alignment
, t
);
3807 /* Build a FIELD_DECL for the base given by BINFO in the class
3808 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3809 *BASE_ALIGN is a running maximum of the alignments of any base
3810 class. OFFSETS gives the location of empty base subobjects. T is
3811 the most derived type. Return non-zero if the new object cannot be
3815 build_base_field (rli
, binfo
, empty_p
, offsets
, t
)
3816 record_layout_info rli
;
3822 tree basetype
= BINFO_TYPE (binfo
);
3826 if (!COMPLETE_TYPE_P (basetype
))
3827 /* This error is now reported in xref_tag, thus giving better
3828 location information. */
3831 decl
= build_decl (FIELD_DECL
, NULL_TREE
, basetype
);
3832 DECL_ARTIFICIAL (decl
) = 1;
3833 DECL_FIELD_CONTEXT (decl
) = rli
->t
;
3834 DECL_SIZE (decl
) = CLASSTYPE_SIZE (basetype
);
3835 DECL_SIZE_UNIT (decl
) = CLASSTYPE_SIZE_UNIT (basetype
);
3836 DECL_ALIGN (decl
) = CLASSTYPE_ALIGN (basetype
);
3837 DECL_USER_ALIGN (decl
) = CLASSTYPE_USER_ALIGN (basetype
);
3839 if (!integer_zerop (DECL_SIZE (decl
)))
3841 /* The containing class is non-empty because it has a non-empty
3845 /* Try to place the field. It may take more than one try if we
3846 have a hard time placing the field without putting two
3847 objects of the same type at the same address. */
3848 layout_nonempty_base_or_field (rli
, decl
, binfo
, offsets
, t
);
3852 unsigned HOST_WIDE_INT eoc
;
3854 /* On some platforms (ARM), even empty classes will not be
3856 eoc
= tree_low_cst (rli_size_unit_so_far (rli
), 0);
3857 eoc
= CEIL (eoc
, DECL_ALIGN_UNIT (decl
)) * DECL_ALIGN_UNIT (decl
);
3858 atend
|= layout_empty_base (binfo
, size_int (eoc
), offsets
, t
);
3861 /* Record the offsets of BINFO and its base subobjects. */
3862 record_subobject_offsets (BINFO_TYPE (binfo
),
3863 BINFO_OFFSET (binfo
),
3869 /* Layout all of the non-virtual base classes. Record empty
3870 subobjects in OFFSETS. T is the most derived type. Return
3871 non-zero if the type cannot be nearly empty. */
3874 build_base_fields (rli
, empty_p
, offsets
, t
)
3875 record_layout_info rli
;
3880 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3883 int n_baseclasses
= CLASSTYPE_N_BASECLASSES (rec
);
3887 /* The primary base class is always allocated first. */
3888 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec
))
3889 build_base_field (rli
, CLASSTYPE_PRIMARY_BINFO (rec
),
3890 empty_p
, offsets
, t
);
3892 /* Now allocate the rest of the bases. */
3893 for (i
= 0; i
< n_baseclasses
; ++i
)
3897 base_binfo
= BINFO_BASETYPE (TYPE_BINFO (rec
), i
);
3899 /* The primary base was already allocated above, so we don't
3900 need to allocate it again here. */
3901 if (base_binfo
== CLASSTYPE_PRIMARY_BINFO (rec
))
3904 /* A primary virtual base class is allocated just like any other
3905 base class, but a non-primary virtual base is allocated
3906 later, in layout_virtual_bases. */
3907 if (TREE_VIA_VIRTUAL (base_binfo
)
3908 && !BINFO_PRIMARY_P (base_binfo
))
3911 atend
|= build_base_field (rli
, base_binfo
, empty_p
, offsets
, t
);
3916 /* Go through the TYPE_METHODS of T issuing any appropriate
3917 diagnostics, figuring out which methods override which other
3918 methods, and so forth. */
3926 for (x
= TYPE_METHODS (t
); x
; x
= TREE_CHAIN (x
))
3928 /* If this was an evil function, don't keep it in class. */
3929 if (DECL_ASSEMBLER_NAME_SET_P (x
)
3930 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x
)))
3933 check_for_override (x
, t
);
3934 if (DECL_PURE_VIRTUAL_P (x
) && ! DECL_VINDEX (x
))
3935 cp_error_at ("initializer specified for non-virtual method `%D'", x
);
3937 /* The name of the field is the original field name
3938 Save this in auxiliary field for later overloading. */
3939 if (DECL_VINDEX (x
))
3941 TYPE_POLYMORPHIC_P (t
) = 1;
3942 if (DECL_PURE_VIRTUAL_P (x
))
3943 CLASSTYPE_PURE_VIRTUALS (t
)
3944 = tree_cons (NULL_TREE
, x
, CLASSTYPE_PURE_VIRTUALS (t
));
3949 /* FN is a constructor or destructor. Clone the declaration to create
3950 a specialized in-charge or not-in-charge version, as indicated by
3954 build_clone (fn
, name
)
3961 /* Copy the function. */
3962 clone
= copy_decl (fn
);
3963 /* Remember where this function came from. */
3964 DECL_CLONED_FUNCTION (clone
) = fn
;
3965 DECL_ABSTRACT_ORIGIN (clone
) = fn
;
3966 /* Reset the function name. */
3967 DECL_NAME (clone
) = name
;
3968 SET_DECL_ASSEMBLER_NAME (clone
, NULL_TREE
);
3969 /* There's no pending inline data for this function. */
3970 DECL_PENDING_INLINE_INFO (clone
) = NULL
;
3971 DECL_PENDING_INLINE_P (clone
) = 0;
3972 /* And it hasn't yet been deferred. */
3973 DECL_DEFERRED_FN (clone
) = 0;
3975 /* The base-class destructor is not virtual. */
3976 if (name
== base_dtor_identifier
)
3978 DECL_VIRTUAL_P (clone
) = 0;
3979 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
3980 DECL_VINDEX (clone
) = NULL_TREE
;
3983 /* If there was an in-charge parameter, drop it from the function
3985 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
3991 exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
3992 basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
3993 parmtypes
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
3994 /* Skip the `this' parameter. */
3995 parmtypes
= TREE_CHAIN (parmtypes
);
3996 /* Skip the in-charge parameter. */
3997 parmtypes
= TREE_CHAIN (parmtypes
);
3998 /* And the VTT parm, in a complete [cd]tor. */
3999 if (DECL_HAS_VTT_PARM_P (fn
)
4000 && ! DECL_NEEDS_VTT_PARM_P (clone
))
4001 parmtypes
= TREE_CHAIN (parmtypes
);
4002 /* If this is subobject constructor or destructor, add the vtt
4005 = build_cplus_method_type (basetype
,
4006 TREE_TYPE (TREE_TYPE (clone
)),
4009 TREE_TYPE (clone
) = build_exception_variant (TREE_TYPE (clone
),
4013 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4014 aren't function parameters; those are the template parameters. */
4015 if (TREE_CODE (clone
) != TEMPLATE_DECL
)
4017 DECL_ARGUMENTS (clone
) = copy_list (DECL_ARGUMENTS (clone
));
4018 /* Remove the in-charge parameter. */
4019 if (DECL_HAS_IN_CHARGE_PARM_P (clone
))
4021 TREE_CHAIN (DECL_ARGUMENTS (clone
))
4022 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone
)));
4023 DECL_HAS_IN_CHARGE_PARM_P (clone
) = 0;
4025 /* And the VTT parm, in a complete [cd]tor. */
4026 if (DECL_HAS_VTT_PARM_P (fn
))
4028 if (DECL_NEEDS_VTT_PARM_P (clone
))
4029 DECL_HAS_VTT_PARM_P (clone
) = 1;
4032 TREE_CHAIN (DECL_ARGUMENTS (clone
))
4033 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone
)));
4034 DECL_HAS_VTT_PARM_P (clone
) = 0;
4038 for (parms
= DECL_ARGUMENTS (clone
); parms
; parms
= TREE_CHAIN (parms
))
4040 DECL_CONTEXT (parms
) = clone
;
4041 cxx_dup_lang_specific_decl (parms
);
4045 /* Create the RTL for this function. */
4046 SET_DECL_RTL (clone
, NULL_RTX
);
4047 rest_of_decl_compilation (clone
, NULL
, /*top_level=*/1, at_eof
);
4049 /* Make it easy to find the CLONE given the FN. */
4050 TREE_CHAIN (clone
) = TREE_CHAIN (fn
);
4051 TREE_CHAIN (fn
) = clone
;
4053 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4054 if (TREE_CODE (clone
) == TEMPLATE_DECL
)
4058 DECL_TEMPLATE_RESULT (clone
)
4059 = build_clone (DECL_TEMPLATE_RESULT (clone
), name
);
4060 result
= DECL_TEMPLATE_RESULT (clone
);
4061 DECL_TEMPLATE_INFO (result
) = copy_node (DECL_TEMPLATE_INFO (result
));
4062 DECL_TI_TEMPLATE (result
) = clone
;
4064 else if (DECL_DEFERRED_FN (fn
))
4070 /* Produce declarations for all appropriate clones of FN. If
4071 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4072 CLASTYPE_METHOD_VEC as well. */
4075 clone_function_decl (fn
, update_method_vec_p
)
4077 int update_method_vec_p
;
4081 /* Avoid inappropriate cloning. */
4083 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn
)))
4086 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn
))
4088 /* For each constructor, we need two variants: an in-charge version
4089 and a not-in-charge version. */
4090 clone
= build_clone (fn
, complete_ctor_identifier
);
4091 if (update_method_vec_p
)
4092 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4093 clone
= build_clone (fn
, base_ctor_identifier
);
4094 if (update_method_vec_p
)
4095 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4099 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
), 20000411);
4101 /* For each destructor, we need three variants: an in-charge
4102 version, a not-in-charge version, and an in-charge deleting
4103 version. We clone the deleting version first because that
4104 means it will go second on the TYPE_METHODS list -- and that
4105 corresponds to the correct layout order in the virtual
4108 For a non-virtual destructor, we do not build a deleting
4110 if (DECL_VIRTUAL_P (fn
))
4112 clone
= build_clone (fn
, deleting_dtor_identifier
);
4113 if (update_method_vec_p
)
4114 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4116 clone
= build_clone (fn
, complete_dtor_identifier
);
4117 if (update_method_vec_p
)
4118 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4119 clone
= build_clone (fn
, base_dtor_identifier
);
4120 if (update_method_vec_p
)
4121 add_method (DECL_CONTEXT (clone
), clone
, /*error_p=*/0);
4124 /* Note that this is an abstract function that is never emitted. */
4125 DECL_ABSTRACT (fn
) = 1;
4128 /* DECL is an in charge constructor, which is being defined. This will
4129 have had an in class declaration, from whence clones were
4130 declared. An out-of-class definition can specify additional default
4131 arguments. As it is the clones that are involved in overload
4132 resolution, we must propagate the information from the DECL to its
4136 adjust_clone_args (decl
)
4141 for (clone
= TREE_CHAIN (decl
); clone
&& DECL_CLONED_FUNCTION (clone
);
4142 clone
= TREE_CHAIN (clone
))
4144 tree orig_clone_parms
= TYPE_ARG_TYPES (TREE_TYPE (clone
));
4145 tree orig_decl_parms
= TYPE_ARG_TYPES (TREE_TYPE (decl
));
4146 tree decl_parms
, clone_parms
;
4148 clone_parms
= orig_clone_parms
;
4150 /* Skip the 'this' parameter. */
4151 orig_clone_parms
= TREE_CHAIN (orig_clone_parms
);
4152 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4154 if (DECL_HAS_IN_CHARGE_PARM_P (decl
))
4155 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4156 if (DECL_HAS_VTT_PARM_P (decl
))
4157 orig_decl_parms
= TREE_CHAIN (orig_decl_parms
);
4159 clone_parms
= orig_clone_parms
;
4160 if (DECL_HAS_VTT_PARM_P (clone
))
4161 clone_parms
= TREE_CHAIN (clone_parms
);
4163 for (decl_parms
= orig_decl_parms
; decl_parms
;
4164 decl_parms
= TREE_CHAIN (decl_parms
),
4165 clone_parms
= TREE_CHAIN (clone_parms
))
4167 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms
),
4168 TREE_TYPE (clone_parms
)), 20010424);
4170 if (TREE_PURPOSE (decl_parms
) && !TREE_PURPOSE (clone_parms
))
4172 /* A default parameter has been added. Adjust the
4173 clone's parameters. */
4174 tree exceptions
= TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone
));
4175 tree basetype
= TYPE_METHOD_BASETYPE (TREE_TYPE (clone
));
4178 clone_parms
= orig_decl_parms
;
4180 if (DECL_HAS_VTT_PARM_P (clone
))
4182 clone_parms
= tree_cons (TREE_PURPOSE (orig_clone_parms
),
4183 TREE_VALUE (orig_clone_parms
),
4185 TREE_TYPE (clone_parms
) = TREE_TYPE (orig_clone_parms
);
4187 type
= build_cplus_method_type (basetype
,
4188 TREE_TYPE (TREE_TYPE (clone
)),
4191 type
= build_exception_variant (type
, exceptions
);
4192 TREE_TYPE (clone
) = type
;
4194 clone_parms
= NULL_TREE
;
4198 my_friendly_assert (!clone_parms
, 20010424);
4202 /* For each of the constructors and destructors in T, create an
4203 in-charge and not-in-charge variant. */
4206 clone_constructors_and_destructors (t
)
4211 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4213 if (!CLASSTYPE_METHOD_VEC (t
))
4216 for (fns
= CLASSTYPE_CONSTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4217 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4218 for (fns
= CLASSTYPE_DESTRUCTORS (t
); fns
; fns
= OVL_NEXT (fns
))
4219 clone_function_decl (OVL_CURRENT (fns
), /*update_method_vec_p=*/1);
4222 /* Remove all zero-width bit-fields from T. */
4225 remove_zero_width_bit_fields (t
)
4230 fieldsp
= &TYPE_FIELDS (t
);
4233 if (TREE_CODE (*fieldsp
) == FIELD_DECL
4234 && DECL_C_BIT_FIELD (*fieldsp
)
4235 && DECL_INITIAL (*fieldsp
))
4236 *fieldsp
= TREE_CHAIN (*fieldsp
);
4238 fieldsp
= &TREE_CHAIN (*fieldsp
);
4242 /* Returns TRUE iff we need a cookie when dynamically allocating an
4243 array whose elements have the indicated class TYPE. */
4246 type_requires_array_cookie (type
)
4250 bool has_two_argument_delete_p
= false;
4252 my_friendly_assert (CLASS_TYPE_P (type
), 20010712);
4254 /* If there's a non-trivial destructor, we need a cookie. In order
4255 to iterate through the array calling the destructor for each
4256 element, we'll have to know how many elements there are. */
4257 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type
))
4260 /* If the usual deallocation function is a two-argument whose second
4261 argument is of type `size_t', then we have to pass the size of
4262 the array to the deallocation function, so we will need to store
4264 fns
= lookup_fnfields (TYPE_BINFO (type
),
4265 ansi_opname (VEC_DELETE_EXPR
),
4267 /* If there are no `operator []' members, or the lookup is
4268 ambiguous, then we don't need a cookie. */
4269 if (!fns
|| fns
== error_mark_node
)
4271 /* Loop through all of the functions. */
4272 for (fns
= TREE_VALUE (fns
); fns
; fns
= OVL_NEXT (fns
))
4277 /* Select the current function. */
4278 fn
= OVL_CURRENT (fns
);
4279 /* See if this function is a one-argument delete function. If
4280 it is, then it will be the usual deallocation function. */
4281 second_parm
= TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn
)));
4282 if (second_parm
== void_list_node
)
4284 /* Otherwise, if we have a two-argument function and the second
4285 argument is `size_t', it will be the usual deallocation
4286 function -- unless there is one-argument function, too. */
4287 if (TREE_CHAIN (second_parm
) == void_list_node
4288 && same_type_p (TREE_VALUE (second_parm
), sizetype
))
4289 has_two_argument_delete_p
= true;
4292 return has_two_argument_delete_p
;
4295 /* Check the validity of the bases and members declared in T. Add any
4296 implicitly-generated functions (like copy-constructors and
4297 assignment operators). Compute various flag bits (like
4298 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4299 level: i.e., independently of the ABI in use. */
4302 check_bases_and_members (t
, empty_p
)
4306 /* Nonzero if we are not allowed to generate a default constructor
4308 int cant_have_default_ctor
;
4309 /* Nonzero if the implicitly generated copy constructor should take
4310 a non-const reference argument. */
4311 int cant_have_const_ctor
;
4312 /* Nonzero if the the implicitly generated assignment operator
4313 should take a non-const reference argument. */
4314 int no_const_asn_ref
;
4317 /* By default, we use const reference arguments and generate default
4319 cant_have_default_ctor
= 0;
4320 cant_have_const_ctor
= 0;
4321 no_const_asn_ref
= 0;
4323 /* Assume that the class is nearly empty; we'll clear this flag if
4324 it turns out not to be nearly empty. */
4325 CLASSTYPE_NEARLY_EMPTY_P (t
) = 1;
4327 /* Check all the base-classes. */
4328 check_bases (t
, &cant_have_default_ctor
, &cant_have_const_ctor
,
4331 /* Check all the data member declarations. */
4332 check_field_decls (t
, &access_decls
, empty_p
,
4333 &cant_have_default_ctor
,
4334 &cant_have_const_ctor
,
4337 /* Check all the method declarations. */
4340 /* A nearly-empty class has to be vptr-containing; a nearly empty
4341 class contains just a vptr. */
4342 if (!TYPE_CONTAINS_VPTR_P (t
))
4343 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4345 /* Do some bookkeeping that will guide the generation of implicitly
4346 declared member functions. */
4347 TYPE_HAS_COMPLEX_INIT_REF (t
)
4348 |= (TYPE_HAS_INIT_REF (t
)
4349 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4350 || TYPE_POLYMORPHIC_P (t
));
4351 TYPE_NEEDS_CONSTRUCTING (t
)
4352 |= (TYPE_HAS_CONSTRUCTOR (t
)
4353 || TYPE_USES_VIRTUAL_BASECLASSES (t
)
4354 || TYPE_POLYMORPHIC_P (t
));
4355 CLASSTYPE_NON_AGGREGATE (t
) |= (TYPE_HAS_CONSTRUCTOR (t
)
4356 || TYPE_POLYMORPHIC_P (t
));
4357 CLASSTYPE_NON_POD_P (t
)
4358 |= (CLASSTYPE_NON_AGGREGATE (t
) || TYPE_HAS_DESTRUCTOR (t
)
4359 || TYPE_HAS_ASSIGN_REF (t
));
4360 TYPE_HAS_REAL_ASSIGN_REF (t
) |= TYPE_HAS_ASSIGN_REF (t
);
4361 TYPE_HAS_COMPLEX_ASSIGN_REF (t
)
4362 |= TYPE_HAS_ASSIGN_REF (t
) || TYPE_CONTAINS_VPTR_P (t
);
4364 /* Synthesize any needed methods. Note that methods will be synthesized
4365 for anonymous unions; grok_x_components undoes that. */
4366 add_implicitly_declared_members (t
, cant_have_default_ctor
,
4367 cant_have_const_ctor
,
4370 /* Create the in-charge and not-in-charge variants of constructors
4372 clone_constructors_and_destructors (t
);
4374 /* Process the using-declarations. */
4375 for (; access_decls
; access_decls
= TREE_CHAIN (access_decls
))
4376 handle_using_decl (TREE_VALUE (access_decls
), t
);
4378 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4379 finish_struct_methods (t
);
4381 /* Figure out whether or not we will need a cookie when dynamically
4382 allocating an array of this type. */
4383 TYPE_LANG_SPECIFIC (t
)->u
.c
.vec_new_uses_cookie
4384 = type_requires_array_cookie (t
);
4387 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4388 accordingly. If a new vfield was created (because T doesn't have a
4389 primary base class), then the newly created field is returned. It
4390 is not added to the TYPE_FIELDS list; it is the caller's
4391 responsibility to do that. */
4394 create_vtable_ptr (t
, empty_p
, vfuns_p
,
4395 new_virtuals_p
, overridden_virtuals_p
)
4399 tree
*new_virtuals_p
;
4400 tree
*overridden_virtuals_p
;
4404 /* Loop over the virtual functions, adding them to our various
4406 for (fn
= TYPE_METHODS (t
); fn
; fn
= TREE_CHAIN (fn
))
4407 if (DECL_VINDEX (fn
) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn
))
4408 add_virtual_function (new_virtuals_p
, overridden_virtuals_p
,
4411 /* If we couldn't find an appropriate base class, create a new field
4412 here. Even if there weren't any new virtual functions, we might need a
4413 new virtual function table if we're supposed to include vptrs in
4414 all classes that need them. */
4415 if (!TYPE_VFIELD (t
) && (*vfuns_p
|| TYPE_CONTAINS_VPTR_P (t
)))
4417 /* We build this decl with vtbl_ptr_type_node, which is a
4418 `vtable_entry_type*'. It might seem more precise to use
4419 `vtable_entry_type (*)[N]' where N is the number of firtual
4420 functions. However, that would require the vtable pointer in
4421 base classes to have a different type than the vtable pointer
4422 in derived classes. We could make that happen, but that
4423 still wouldn't solve all the problems. In particular, the
4424 type-based alias analysis code would decide that assignments
4425 to the base class vtable pointer can't alias assignments to
4426 the derived class vtable pointer, since they have different
4427 types. Thus, in an derived class destructor, where the base
4428 class constructor was inlined, we could generate bad code for
4429 setting up the vtable pointer.
4431 Therefore, we use one type for all vtable pointers. We still
4432 use a type-correct type; it's just doesn't indicate the array
4433 bounds. That's better than using `void*' or some such; it's
4434 cleaner, and it let's the alias analysis code know that these
4435 stores cannot alias stores to void*! */
4438 field
= build_decl (FIELD_DECL
, get_vfield_name (t
), vtbl_ptr_type_node
);
4439 SET_DECL_ASSEMBLER_NAME (field
, get_identifier (VFIELD_BASE
));
4440 DECL_VIRTUAL_P (field
) = 1;
4441 DECL_ARTIFICIAL (field
) = 1;
4442 DECL_FIELD_CONTEXT (field
) = t
;
4443 DECL_FCONTEXT (field
) = t
;
4444 DECL_ALIGN (field
) = TYPE_ALIGN (vtbl_ptr_type_node
);
4445 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (vtbl_ptr_type_node
);
4447 TYPE_VFIELD (t
) = field
;
4449 /* This class is non-empty. */
4452 if (CLASSTYPE_N_BASECLASSES (t
))
4453 /* If there were any baseclasses, they can't possibly be at
4454 offset zero any more, because that's where the vtable
4455 pointer is. So, converting to a base class is going to
4457 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t
) = 1;
4465 /* Fixup the inline function given by INFO now that the class is
4469 fixup_pending_inline (fn
)
4472 if (DECL_PENDING_INLINE_INFO (fn
))
4474 tree args
= DECL_ARGUMENTS (fn
);
4477 DECL_CONTEXT (args
) = fn
;
4478 args
= TREE_CHAIN (args
);
4483 /* Fixup the inline methods and friends in TYPE now that TYPE is
4487 fixup_inline_methods (type
)
4490 tree method
= TYPE_METHODS (type
);
4492 if (method
&& TREE_CODE (method
) == TREE_VEC
)
4494 if (TREE_VEC_ELT (method
, 1))
4495 method
= TREE_VEC_ELT (method
, 1);
4496 else if (TREE_VEC_ELT (method
, 0))
4497 method
= TREE_VEC_ELT (method
, 0);
4499 method
= TREE_VEC_ELT (method
, 2);
4502 /* Do inline member functions. */
4503 for (; method
; method
= TREE_CHAIN (method
))
4504 fixup_pending_inline (method
);
4507 for (method
= CLASSTYPE_INLINE_FRIENDS (type
);
4509 method
= TREE_CHAIN (method
))
4510 fixup_pending_inline (TREE_VALUE (method
));
4511 CLASSTYPE_INLINE_FRIENDS (type
) = NULL_TREE
;
4514 /* Add OFFSET to all base types of BINFO which is a base in the
4515 hierarchy dominated by T.
4517 OFFSET, which is a type offset, is number of bytes. */
4520 propagate_binfo_offsets (binfo
, offset
, t
)
4528 /* Update BINFO's offset. */
4529 BINFO_OFFSET (binfo
)
4530 = convert (sizetype
,
4531 size_binop (PLUS_EXPR
,
4532 convert (ssizetype
, BINFO_OFFSET (binfo
)),
4535 /* Find the primary base class. */
4536 primary_binfo
= get_primary_binfo (binfo
);
4538 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4540 for (i
= -1; i
< BINFO_N_BASETYPES (binfo
); ++i
)
4544 /* On the first time through the loop, do the primary base.
4545 Because the primary base need not be an immediate base, we
4546 must handle the primary base specially. */
4552 base_binfo
= primary_binfo
;
4556 base_binfo
= BINFO_BASETYPE (binfo
, i
);
4557 /* Don't do the primary base twice. */
4558 if (base_binfo
== primary_binfo
)
4562 /* Skip virtual bases that aren't our canonical primary base. */
4563 if (TREE_VIA_VIRTUAL (base_binfo
)
4564 && (BINFO_PRIMARY_BASE_OF (base_binfo
) != binfo
4565 || base_binfo
!= binfo_for_vbase (BINFO_TYPE (base_binfo
), t
)))
4568 propagate_binfo_offsets (base_binfo
, offset
, t
);
4572 /* Called via dfs_walk from layout_virtual bases. */
4575 dfs_set_offset_for_unshared_vbases (binfo
, data
)
4579 /* If this is a virtual base, make sure it has the same offset as
4580 the shared copy. If it's a primary base, then we know it's
4582 if (TREE_VIA_VIRTUAL (binfo
))
4584 tree t
= (tree
) data
;
4588 vbase
= binfo_for_vbase (BINFO_TYPE (binfo
), t
);
4591 offset
= size_diffop (BINFO_OFFSET (vbase
), BINFO_OFFSET (binfo
));
4592 propagate_binfo_offsets (binfo
, offset
, t
);
4599 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4600 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4601 empty subobjects of T. */
4604 layout_virtual_bases (t
, offsets
)
4609 unsigned HOST_WIDE_INT eoc
;
4611 if (CLASSTYPE_N_BASECLASSES (t
) == 0)
4614 #ifdef STRUCTURE_SIZE_BOUNDARY
4615 /* Packed structures don't need to have minimum size. */
4616 if (! TYPE_PACKED (t
))
4617 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), STRUCTURE_SIZE_BOUNDARY
);
4620 /* DSIZE is the size of the class without the virtual bases. */
4621 dsize
= TYPE_SIZE (t
);
4623 /* Make every class have alignment of at least one. */
4624 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), BITS_PER_UNIT
);
4626 /* Go through the virtual bases, allocating space for each virtual
4627 base that is not already a primary base class. These are
4628 allocated in inheritance graph order. */
4629 for (vbases
= TYPE_BINFO (t
);
4631 vbases
= TREE_CHAIN (vbases
))
4635 if (!TREE_VIA_VIRTUAL (vbases
))
4637 vbase
= binfo_for_vbase (BINFO_TYPE (vbases
), t
);
4639 if (!BINFO_PRIMARY_P (vbase
))
4641 /* This virtual base is not a primary base of any class in the
4642 hierarchy, so we have to add space for it. */
4643 tree basetype
, usize
;
4644 unsigned int desired_align
;
4646 basetype
= BINFO_TYPE (vbase
);
4648 desired_align
= CLASSTYPE_ALIGN (basetype
);
4649 TYPE_ALIGN (t
) = MAX (TYPE_ALIGN (t
), desired_align
);
4651 /* Add padding so that we can put the virtual base class at an
4652 appropriately aligned offset. */
4653 dsize
= round_up (dsize
, desired_align
);
4655 usize
= size_binop (CEIL_DIV_EXPR
, dsize
, bitsize_unit_node
);
4657 /* We try to squish empty virtual bases in just like
4658 ordinary empty bases. */
4659 if (is_empty_class (basetype
))
4660 layout_empty_base (vbase
,
4661 convert (sizetype
, usize
),
4667 offset
= convert (ssizetype
, usize
);
4668 offset
= size_diffop (offset
,
4670 BINFO_OFFSET (vbase
)));
4672 /* And compute the offset of the virtual base. */
4673 propagate_binfo_offsets (vbase
, offset
, t
);
4674 /* Every virtual baseclass takes a least a UNIT, so that
4675 we can take it's address and get something different
4677 dsize
= size_binop (PLUS_EXPR
, dsize
,
4678 size_binop (MAX_EXPR
, bitsize_unit_node
,
4679 CLASSTYPE_SIZE (basetype
)));
4682 /* Keep track of the offsets assigned to this virtual base. */
4683 record_subobject_offsets (BINFO_TYPE (vbase
),
4684 BINFO_OFFSET (vbase
),
4690 /* Now, go through the TYPE_BINFO hierarchy, setting the
4691 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4692 bases and their direct and indirect bases. The ambiguity checks
4693 in lookup_base depend on the BINFO_OFFSETs being set
4695 dfs_walk (TYPE_BINFO (t
), dfs_set_offset_for_unshared_vbases
, NULL
, t
);
4697 /* If we had empty base classes that protruded beyond the end of the
4698 class, we didn't update DSIZE above; we were hoping to overlay
4699 multiple such bases at the same location. */
4700 eoc
= end_of_class (t
, /*include_virtuals_p=*/1);
4701 dsize
= size_binop (MAX_EXPR
, dsize
, bitsize_int (eoc
* BITS_PER_UNIT
));
4703 /* Now, make sure that the total size of the type is a multiple of
4705 dsize
= round_up (dsize
, TYPE_ALIGN (t
));
4706 TYPE_SIZE (t
) = dsize
;
4707 TYPE_SIZE_UNIT (t
) = convert (sizetype
,
4708 size_binop (CEIL_DIV_EXPR
, TYPE_SIZE (t
),
4709 bitsize_unit_node
));
4711 /* Check for ambiguous virtual bases. */
4713 for (vbases
= CLASSTYPE_VBASECLASSES (t
);
4715 vbases
= TREE_CHAIN (vbases
))
4717 tree basetype
= BINFO_TYPE (TREE_VALUE (vbases
));
4719 if (!lookup_base (t
, basetype
, ba_ignore
| ba_quiet
, NULL
))
4720 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4725 /* Returns the offset of the byte just past the end of the base class
4726 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4727 only non-virtual bases are included. */
4729 static unsigned HOST_WIDE_INT
4730 end_of_class (t
, include_virtuals_p
)
4732 int include_virtuals_p
;
4734 unsigned HOST_WIDE_INT result
= 0;
4737 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); ++i
)
4742 unsigned HOST_WIDE_INT end_of_base
;
4744 base_binfo
= BINFO_BASETYPE (TYPE_BINFO (t
), i
);
4746 if (!include_virtuals_p
4747 && TREE_VIA_VIRTUAL (base_binfo
)
4748 && !BINFO_PRIMARY_P (base_binfo
))
4751 if (is_empty_class (BINFO_TYPE (base_binfo
)))
4752 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4753 allocate some space for it. It cannot have virtual bases,
4754 so TYPE_SIZE_UNIT is fine. */
4755 size
= TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo
));
4757 size
= CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo
));
4758 offset
= size_binop (PLUS_EXPR
,
4759 BINFO_OFFSET (base_binfo
),
4761 end_of_base
= tree_low_cst (offset
, /*pos=*/1);
4762 if (end_of_base
> result
)
4763 result
= end_of_base
;
4769 /* Warn about direct bases of T that are inaccessible because they are
4770 ambiguous. For example:
4773 struct T : public S {};
4774 struct U : public S, public T {};
4776 Here, `(S*) new U' is not allowed because there are two `S'
4780 warn_about_ambiguous_direct_bases (t
)
4785 for (i
= 0; i
< CLASSTYPE_N_BASECLASSES (t
); ++i
)
4787 tree basetype
= TYPE_BINFO_BASETYPE (t
, i
);
4789 if (!lookup_base (t
, basetype
, ba_ignore
| ba_quiet
, NULL
))
4790 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4795 /* Compare two INTEGER_CSTs K1 and K2. */
4798 splay_tree_compare_integer_csts (k1
, k2
)
4802 return tree_int_cst_compare ((tree
) k1
, (tree
) k2
);
4805 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4806 BINFO_OFFSETs for all of the base-classes. Position the vtable
4810 layout_class_type (t
, empty_p
, vfuns_p
,
4811 new_virtuals_p
, overridden_virtuals_p
)
4815 tree
*new_virtuals_p
;
4816 tree
*overridden_virtuals_p
;
4818 tree non_static_data_members
;
4821 record_layout_info rli
;
4822 unsigned HOST_WIDE_INT eoc
;
4823 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4824 types that appear at that offset. */
4825 splay_tree empty_base_offsets
;
4827 /* Keep track of the first non-static data member. */
4828 non_static_data_members
= TYPE_FIELDS (t
);
4830 /* Start laying out the record. */
4831 rli
= start_record_layout (t
);
4833 /* If possible, we reuse the virtual function table pointer from one
4834 of our base classes. */
4835 determine_primary_base (t
, vfuns_p
);
4837 /* Create a pointer to our virtual function table. */
4838 vptr
= create_vtable_ptr (t
, empty_p
, vfuns_p
,
4839 new_virtuals_p
, overridden_virtuals_p
);
4841 /* The vptr is always the first thing in the class. */
4844 TYPE_FIELDS (t
) = chainon (vptr
, TYPE_FIELDS (t
));
4845 place_field (rli
, vptr
);
4848 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4849 empty_base_offsets
= splay_tree_new (splay_tree_compare_integer_csts
,
4851 if (build_base_fields (rli
, empty_p
, empty_base_offsets
, t
))
4852 CLASSTYPE_NEARLY_EMPTY_P (t
) = 0;
4854 /* Layout the non-static data members. */
4855 for (field
= non_static_data_members
; field
; field
= TREE_CHAIN (field
))
4860 /* We still pass things that aren't non-static data members to
4861 the back-end, in case it wants to do something with them. */
4862 if (TREE_CODE (field
) != FIELD_DECL
)
4864 place_field (rli
, field
);
4865 /* If the static data member has incomplete type, keep track
4866 of it so that it can be completed later. (The handling
4867 of pending statics in finish_record_layout is
4868 insufficient; consider:
4871 struct S2 { static S1 s1; };
4873 At this point, finish_record_layout will be called, but
4874 S1 is still incomplete.) */
4875 if (TREE_CODE (field
) == VAR_DECL
)
4876 maybe_register_incomplete_var (field
);
4880 type
= TREE_TYPE (field
);
4882 /* If this field is a bit-field whose width is greater than its
4883 type, then there are some special rules for allocating
4885 if (DECL_C_BIT_FIELD (field
)
4886 && INT_CST_LT (TYPE_SIZE (type
), DECL_SIZE (field
)))
4888 integer_type_kind itk
;
4891 /* We must allocate the bits as if suitably aligned for the
4892 longest integer type that fits in this many bits. type
4893 of the field. Then, we are supposed to use the left over
4894 bits as additional padding. */
4895 for (itk
= itk_char
; itk
!= itk_none
; ++itk
)
4896 if (INT_CST_LT (DECL_SIZE (field
),
4897 TYPE_SIZE (integer_types
[itk
])))
4900 /* ITK now indicates a type that is too large for the
4901 field. We have to back up by one to find the largest
4903 integer_type
= integer_types
[itk
- 1];
4904 padding
= size_binop (MINUS_EXPR
, DECL_SIZE (field
),
4905 TYPE_SIZE (integer_type
));
4906 DECL_SIZE (field
) = TYPE_SIZE (integer_type
);
4907 DECL_ALIGN (field
) = TYPE_ALIGN (integer_type
);
4908 DECL_USER_ALIGN (field
) = TYPE_USER_ALIGN (integer_type
);
4911 padding
= NULL_TREE
;
4913 layout_nonempty_base_or_field (rli
, field
, NULL_TREE
,
4914 empty_base_offsets
, t
);
4916 /* If we needed additional padding after this field, add it
4922 padding_field
= build_decl (FIELD_DECL
,
4925 DECL_BIT_FIELD (padding_field
) = 1;
4926 DECL_SIZE (padding_field
) = padding
;
4927 DECL_ALIGN (padding_field
) = 1;
4928 DECL_USER_ALIGN (padding_field
) = 0;
4929 layout_nonempty_base_or_field (rli
, padding_field
,
4931 empty_base_offsets
, t
);
4935 /* It might be the case that we grew the class to allocate a
4936 zero-sized base class. That won't be reflected in RLI, yet,
4937 because we are willing to overlay multiple bases at the same
4938 offset. However, now we need to make sure that RLI is big enough
4939 to reflect the entire class. */
4940 eoc
= end_of_class (t
, /*include_virtuals_p=*/0);
4941 if (TREE_CODE (rli_size_unit_so_far (rli
)) == INTEGER_CST
4942 && compare_tree_int (rli_size_unit_so_far (rli
), eoc
) < 0)
4944 rli
->offset
= size_binop (MAX_EXPR
, rli
->offset
, size_int (eoc
));
4945 rli
->bitpos
= bitsize_zero_node
;
4948 /* We make all structures have at least one element, so that they
4949 have non-zero size. The class may be empty even if it has
4950 basetypes. Therefore, we add the fake field after all the other
4951 fields; if there are already FIELD_DECLs on the list, their
4952 offsets will not be disturbed. */
4953 if (!eoc
&& *empty_p
)
4957 padding
= build_decl (FIELD_DECL
, NULL_TREE
, char_type_node
);
4958 place_field (rli
, padding
);
4961 /* Let the back-end lay out the type. Note that at this point we
4962 have only included non-virtual base-classes; we will lay out the
4963 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4964 this call are not necessarily correct; they are just the size and
4965 alignment when no virtual base clases are used. */
4966 finish_record_layout (rli
);
4968 /* Delete all zero-width bit-fields from the list of fields. Now
4969 that the type is laid out they are no longer important. */
4970 remove_zero_width_bit_fields (t
);
4972 /* Remember the size and alignment of the class before adding
4973 the virtual bases. */
4976 CLASSTYPE_SIZE (t
) = bitsize_zero_node
;
4977 CLASSTYPE_SIZE_UNIT (t
) = size_zero_node
;
4981 CLASSTYPE_SIZE (t
) = TYPE_BINFO_SIZE (t
);
4982 CLASSTYPE_SIZE_UNIT (t
) = TYPE_BINFO_SIZE_UNIT (t
);
4985 CLASSTYPE_ALIGN (t
) = TYPE_ALIGN (t
);
4986 CLASSTYPE_USER_ALIGN (t
) = TYPE_USER_ALIGN (t
);
4988 /* Set the TYPE_DECL for this type to contain the right
4989 value for DECL_OFFSET, so that we can use it as part
4990 of a COMPONENT_REF for multiple inheritance. */
4991 layout_decl (TYPE_MAIN_DECL (t
), 0);
4993 /* Now fix up any virtual base class types that we left lying
4994 around. We must get these done before we try to lay out the
4995 virtual function table. As a side-effect, this will remove the
4996 base subobject fields. */
4997 layout_virtual_bases (t
, empty_base_offsets
);
4999 /* Warn about direct bases that can't be talked about due to
5001 warn_about_ambiguous_direct_bases (t
);
5004 splay_tree_delete (empty_base_offsets
);
5007 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5008 (or C++ class declaration).
5010 For C++, we must handle the building of derived classes.
5011 Also, C++ allows static class members. The way that this is
5012 handled is to keep the field name where it is (as the DECL_NAME
5013 of the field), and place the overloaded decl in the bit position
5014 of the field. layout_record and layout_union will know about this.
5016 More C++ hair: inline functions have text in their
5017 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5018 meaningful tree structure. After the struct has been laid out, set
5019 things up so that this can happen.
5021 And still more: virtual functions. In the case of single inheritance,
5022 when a new virtual function is seen which redefines a virtual function
5023 from the base class, the new virtual function is placed into
5024 the virtual function table at exactly the same address that
5025 it had in the base class. When this is extended to multiple
5026 inheritance, the same thing happens, except that multiple virtual
5027 function tables must be maintained. The first virtual function
5028 table is treated in exactly the same way as in the case of single
5029 inheritance. Additional virtual function tables have different
5030 DELTAs, which tell how to adjust `this' to point to the right thing.
5032 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5040 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5041 a FUNCTION_DECL. Each of these functions is a virtual function
5042 declared in T that does not override any virtual function from a
5044 tree new_virtuals
= NULL_TREE
;
5045 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5046 except that each declaration here overrides the declaration from
5048 tree overridden_virtuals
= NULL_TREE
;
5053 if (COMPLETE_TYPE_P (t
))
5055 if (IS_AGGR_TYPE (t
))
5056 error ("redefinition of `%#T'", t
);
5063 /* If this type was previously laid out as a forward reference,
5064 make sure we lay it out again. */
5065 TYPE_SIZE (t
) = NULL_TREE
;
5066 CLASSTYPE_GOT_SEMICOLON (t
) = 0;
5067 CLASSTYPE_PRIMARY_BINFO (t
) = NULL_TREE
;
5069 CLASSTYPE_RTTI (t
) = NULL_TREE
;
5071 fixup_inline_methods (t
);
5073 /* Do end-of-class semantic processing: checking the validity of the
5074 bases and members and add implicitly generated methods. */
5075 check_bases_and_members (t
, &empty
);
5077 /* Layout the class itself. */
5078 layout_class_type (t
, &empty
, &vfuns
,
5079 &new_virtuals
, &overridden_virtuals
);
5081 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5082 vfield
= TYPE_VFIELD (t
);
5083 if (vfield
&& CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5085 tree primary
= CLASSTYPE_PRIMARY_BINFO (t
);
5087 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield
),
5088 BINFO_TYPE (primary
)),
5090 /* The vtable better be at the start. */
5091 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield
)),
5093 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary
)),
5096 vfield
= copy_decl (vfield
);
5097 DECL_FIELD_CONTEXT (vfield
) = t
;
5098 TYPE_VFIELD (t
) = vfield
;
5101 my_friendly_assert (!vfield
|| DECL_FIELD_CONTEXT (vfield
) == t
, 20010726);
5104 = modify_all_vtables (t
, &vfuns
, nreverse (overridden_virtuals
));
5106 /* If we created a new vtbl pointer for this class, add it to the
5108 if (TYPE_VFIELD (t
) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5109 CLASSTYPE_VFIELDS (t
)
5110 = chainon (CLASSTYPE_VFIELDS (t
), build_tree_list (NULL_TREE
, t
));
5112 /* If necessary, create the primary vtable for this class. */
5113 if (new_virtuals
|| overridden_virtuals
|| TYPE_CONTAINS_VPTR_P (t
))
5115 new_virtuals
= nreverse (new_virtuals
);
5116 /* We must enter these virtuals into the table. */
5117 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5118 build_primary_vtable (NULL_TREE
, t
);
5119 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t
), t
))
5120 /* Here we know enough to change the type of our virtual
5121 function table, but we will wait until later this function. */
5122 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t
), t
);
5124 /* If this type has basetypes with constructors, then those
5125 constructors might clobber the virtual function table. But
5126 they don't if the derived class shares the exact vtable of the base
5129 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5131 /* If we didn't need a new vtable, see if we should copy one from
5133 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5135 tree binfo
= CLASSTYPE_PRIMARY_BINFO (t
);
5137 /* If this class uses a different vtable than its primary base
5138 then when we will need to initialize our vptr after the base
5139 class constructor runs. */
5140 if (TYPE_BINFO_VTABLE (t
) != BINFO_VTABLE (binfo
))
5141 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t
) = 1;
5144 if (TYPE_CONTAINS_VPTR_P (t
))
5146 if (TYPE_BINFO_VTABLE (t
))
5147 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t
)),
5149 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t
))
5150 my_friendly_assert (TYPE_BINFO_VIRTUALS (t
) == NULL_TREE
,
5153 CLASSTYPE_VSIZE (t
) = vfuns
;
5154 /* Entries for virtual functions defined in the primary base are
5155 followed by entries for new functions unique to this class. */
5156 TYPE_BINFO_VIRTUALS (t
)
5157 = chainon (TYPE_BINFO_VIRTUALS (t
), new_virtuals
);
5158 /* Finally, add entries for functions that override virtuals
5159 from non-primary bases. */
5160 TYPE_BINFO_VIRTUALS (t
)
5161 = chainon (TYPE_BINFO_VIRTUALS (t
), overridden_virtuals
);
5164 finish_struct_bits (t
);
5166 /* Complete the rtl for any static member objects of the type we're
5168 for (x
= TYPE_FIELDS (t
); x
; x
= TREE_CHAIN (x
))
5169 if (TREE_CODE (x
) == VAR_DECL
&& TREE_STATIC (x
)
5170 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x
)), t
))
5171 DECL_MODE (x
) = TYPE_MODE (t
);
5173 /* Done with FIELDS...now decide whether to sort these for
5174 faster lookups later.
5176 The C front-end only does this when n_fields > 15. We use
5177 a smaller number because most searches fail (succeeding
5178 ultimately as the search bores through the inheritance
5179 hierarchy), and we want this failure to occur quickly. */
5181 n_fields
= count_fields (TYPE_FIELDS (t
));
5184 tree field_vec
= make_tree_vec (n_fields
);
5185 add_fields_to_vec (TYPE_FIELDS (t
), field_vec
, 0);
5186 qsort (&TREE_VEC_ELT (field_vec
, 0), n_fields
, sizeof (tree
),
5187 (int (*)(const void *, const void *))field_decl_cmp
);
5188 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t
)))
5189 retrofit_lang_decl (TYPE_MAIN_DECL (t
));
5190 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t
)) = field_vec
;
5193 if (TYPE_HAS_CONSTRUCTOR (t
))
5195 tree vfields
= CLASSTYPE_VFIELDS (t
);
5199 /* Mark the fact that constructor for T
5200 could affect anybody inheriting from T
5201 who wants to initialize vtables for VFIELDS's type. */
5202 if (VF_DERIVED_VALUE (vfields
))
5203 TREE_ADDRESSABLE (vfields
) = 1;
5204 vfields
= TREE_CHAIN (vfields
);
5208 /* Make the rtl for any new vtables we have created, and unmark
5209 the base types we marked. */
5212 /* Build the VTT for T. */
5215 if (warn_nonvdtor
&& TYPE_POLYMORPHIC_P (t
) && TYPE_HAS_DESTRUCTOR (t
)
5216 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t
), 1)) == NULL_TREE
)
5217 warning ("`%#T' has virtual functions but non-virtual destructor", t
);
5221 if (warn_overloaded_virtual
)
5224 maybe_suppress_debug_info (t
);
5226 dump_class_hierarchy (t
);
5228 /* Finish debugging output for this type. */
5229 rest_of_type_compilation (t
, ! LOCAL_CLASS_P (t
));
5232 /* When T was built up, the member declarations were added in reverse
5233 order. Rearrange them to declaration order. */
5236 unreverse_member_declarations (t
)
5243 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5244 reverse order. Put them in declaration order now. */
5245 TYPE_METHODS (t
) = nreverse (TYPE_METHODS (t
));
5246 CLASSTYPE_TAGS (t
) = nreverse (CLASSTYPE_TAGS (t
));
5248 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5249 reverse order, so we can't just use nreverse. */
5251 for (x
= TYPE_FIELDS (t
);
5252 x
&& TREE_CODE (x
) != TYPE_DECL
;
5255 next
= TREE_CHAIN (x
);
5256 TREE_CHAIN (x
) = prev
;
5261 TREE_CHAIN (TYPE_FIELDS (t
)) = x
;
5263 TYPE_FIELDS (t
) = prev
;
5268 finish_struct (t
, attributes
)
5271 const char *saved_filename
= input_filename
;
5272 int saved_lineno
= lineno
;
5274 /* Now that we've got all the field declarations, reverse everything
5276 unreverse_member_declarations (t
);
5278 cplus_decl_attributes (&t
, attributes
, (int) ATTR_FLAG_TYPE_IN_PLACE
);
5280 /* Nadger the current location so that diagnostics point to the start of
5281 the struct, not the end. */
5282 input_filename
= DECL_SOURCE_FILE (TYPE_NAME (t
));
5283 lineno
= DECL_SOURCE_LINE (TYPE_NAME (t
));
5285 if (processing_template_decl
)
5287 finish_struct_methods (t
);
5288 TYPE_SIZE (t
) = bitsize_zero_node
;
5291 finish_struct_1 (t
);
5293 input_filename
= saved_filename
;
5294 lineno
= saved_lineno
;
5296 TYPE_BEING_DEFINED (t
) = 0;
5298 if (current_class_type
)
5301 error ("trying to finish struct, but kicked out due to previous parse errors");
5303 if (processing_template_decl
)
5305 tree scope
= current_scope ();
5306 if (scope
&& TREE_CODE (scope
) == FUNCTION_DECL
)
5307 add_stmt (build_min (TAG_DEFN
, t
));
5313 /* Return the dynamic type of INSTANCE, if known.
5314 Used to determine whether the virtual function table is needed
5317 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5318 of our knowledge of its type. *NONNULL should be initialized
5319 before this function is called. */
5322 fixed_type_or_null (instance
, nonnull
, cdtorp
)
5327 switch (TREE_CODE (instance
))
5330 if (POINTER_TYPE_P (TREE_TYPE (instance
)))
5333 return fixed_type_or_null (TREE_OPERAND (instance
, 0),
5337 /* This is a call to a constructor, hence it's never zero. */
5338 if (TREE_HAS_CONSTRUCTOR (instance
))
5342 return TREE_TYPE (instance
);
5347 /* This is a call to a constructor, hence it's never zero. */
5348 if (TREE_HAS_CONSTRUCTOR (instance
))
5352 return TREE_TYPE (instance
);
5354 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5361 if (TREE_CODE (TREE_OPERAND (instance
, 0)) == ADDR_EXPR
)
5362 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5363 if (TREE_CODE (TREE_OPERAND (instance
, 1)) == INTEGER_CST
)
5364 /* Propagate nonnull. */
5365 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5370 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5375 return fixed_type_or_null (TREE_OPERAND (instance
, 0), nonnull
, cdtorp
);
5378 return fixed_type_or_null (TREE_OPERAND (instance
, 1), nonnull
, cdtorp
);
5382 if (TREE_CODE (TREE_TYPE (instance
)) == ARRAY_TYPE
5383 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance
))))
5387 return TREE_TYPE (TREE_TYPE (instance
));
5389 /* fall through... */
5393 if (IS_AGGR_TYPE (TREE_TYPE (instance
)))
5397 return TREE_TYPE (instance
);
5399 else if (instance
== current_class_ptr
)
5404 /* if we're in a ctor or dtor, we know our type. */
5405 if (DECL_LANG_SPECIFIC (current_function_decl
)
5406 && (DECL_CONSTRUCTOR_P (current_function_decl
)
5407 || DECL_DESTRUCTOR_P (current_function_decl
)))
5411 return TREE_TYPE (TREE_TYPE (instance
));
5414 else if (TREE_CODE (TREE_TYPE (instance
)) == REFERENCE_TYPE
)
5416 /* Reference variables should be references to objects. */
5420 if (TREE_CODE (instance
) == VAR_DECL
5421 && DECL_INITIAL (instance
))
5422 return fixed_type_or_null (DECL_INITIAL (instance
),
5432 /* Return non-zero if the dynamic type of INSTANCE is known, and
5433 equivalent to the static type. We also handle the case where
5434 INSTANCE is really a pointer. Return negative if this is a
5435 ctor/dtor. There the dynamic type is known, but this might not be
5436 the most derived base of the original object, and hence virtual
5437 bases may not be layed out according to this type.
5439 Used to determine whether the virtual function table is needed
5442 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5443 of our knowledge of its type. *NONNULL should be initialized
5444 before this function is called. */
5447 resolves_to_fixed_type_p (instance
, nonnull
)
5451 tree t
= TREE_TYPE (instance
);
5454 tree fixed
= fixed_type_or_null (instance
, nonnull
, &cdtorp
);
5455 if (fixed
== NULL_TREE
)
5457 if (POINTER_TYPE_P (t
))
5459 if (!same_type_ignoring_top_level_qualifiers_p (t
, fixed
))
5461 return cdtorp
? -1 : 1;
5466 init_class_processing ()
5468 current_class_depth
= 0;
5469 current_class_stack_size
= 10;
5471 = (class_stack_node_t
) xmalloc (current_class_stack_size
5472 * sizeof (struct class_stack_node
));
5473 VARRAY_TREE_INIT (local_classes
, 8, "local_classes");
5475 access_default_node
= build_int_2 (0, 0);
5476 access_public_node
= build_int_2 (ak_public
, 0);
5477 access_protected_node
= build_int_2 (ak_protected
, 0);
5478 access_private_node
= build_int_2 (ak_private
, 0);
5479 access_default_virtual_node
= build_int_2 (4, 0);
5480 access_public_virtual_node
= build_int_2 (4 | ak_public
, 0);
5481 access_protected_virtual_node
= build_int_2 (4 | ak_protected
, 0);
5482 access_private_virtual_node
= build_int_2 (4 | ak_private
, 0);
5484 ridpointers
[(int) RID_PUBLIC
] = access_public_node
;
5485 ridpointers
[(int) RID_PRIVATE
] = access_private_node
;
5486 ridpointers
[(int) RID_PROTECTED
] = access_protected_node
;
5489 /* Set current scope to NAME. CODE tells us if this is a
5490 STRUCT, UNION, or ENUM environment.
5492 NAME may end up being NULL_TREE if this is an anonymous or
5493 late-bound struct (as in "struct { ... } foo;") */
5495 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5496 appropriate values, found by looking up the type definition of
5499 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5500 which can be seen locally to the class. They are shadowed by
5501 any subsequent local declaration (including parameter names).
5503 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5504 which have static meaning (i.e., static members, static
5505 member functions, enum declarations, etc).
5507 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5508 which can be seen locally to the class (as in 1), but
5509 know that we are doing this for declaration purposes
5510 (i.e. friend foo::bar (int)).
5512 So that we may avoid calls to lookup_name, we cache the _TYPE
5513 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5515 For multiple inheritance, we perform a two-pass depth-first search
5516 of the type lattice. The first pass performs a pre-order search,
5517 marking types after the type has had its fields installed in
5518 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5519 unmarks the marked types. If a field or member function name
5520 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5521 that name becomes `error_mark_node'. */
5524 pushclass (type
, modify
)
5528 type
= TYPE_MAIN_VARIANT (type
);
5530 /* Make sure there is enough room for the new entry on the stack. */
5531 if (current_class_depth
+ 1 >= current_class_stack_size
)
5533 current_class_stack_size
*= 2;
5535 = (class_stack_node_t
) xrealloc (current_class_stack
,
5536 current_class_stack_size
5537 * sizeof (struct class_stack_node
));
5540 /* Insert a new entry on the class stack. */
5541 current_class_stack
[current_class_depth
].name
= current_class_name
;
5542 current_class_stack
[current_class_depth
].type
= current_class_type
;
5543 current_class_stack
[current_class_depth
].access
= current_access_specifier
;
5544 current_class_stack
[current_class_depth
].names_used
= 0;
5545 current_class_depth
++;
5547 /* Now set up the new type. */
5548 current_class_name
= TYPE_NAME (type
);
5549 if (TREE_CODE (current_class_name
) == TYPE_DECL
)
5550 current_class_name
= DECL_NAME (current_class_name
);
5551 current_class_type
= type
;
5553 /* By default, things in classes are private, while things in
5554 structures or unions are public. */
5555 current_access_specifier
= (CLASSTYPE_DECLARED_CLASS (type
)
5556 ? access_private_node
5557 : access_public_node
);
5559 if (previous_class_type
!= NULL_TREE
5560 && (type
!= previous_class_type
5561 || !COMPLETE_TYPE_P (previous_class_type
))
5562 && current_class_depth
== 1)
5564 /* Forcibly remove any old class remnants. */
5565 invalidate_class_lookup_cache ();
5568 /* If we're about to enter a nested class, clear
5569 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5570 if (modify
&& current_class_depth
> 1)
5571 clear_identifier_class_values ();
5577 if (type
!= previous_class_type
|| current_class_depth
> 1)
5578 push_class_decls (type
);
5583 /* We are re-entering the same class we just left, so we
5584 don't have to search the whole inheritance matrix to find
5585 all the decls to bind again. Instead, we install the
5586 cached class_shadowed list, and walk through it binding
5587 names and setting up IDENTIFIER_TYPE_VALUEs. */
5588 set_class_shadows (previous_class_values
);
5589 for (item
= previous_class_values
; item
; item
= TREE_CHAIN (item
))
5591 tree id
= TREE_PURPOSE (item
);
5592 tree decl
= TREE_TYPE (item
);
5594 push_class_binding (id
, decl
);
5595 if (TREE_CODE (decl
) == TYPE_DECL
)
5596 set_identifier_type_value (id
, TREE_TYPE (decl
));
5598 unuse_fields (type
);
5601 storetags (CLASSTYPE_TAGS (type
));
5605 /* When we exit a toplevel class scope, we save the
5606 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5607 reenter the class. Here, we've entered some other class, so we
5608 must invalidate our cache. */
5611 invalidate_class_lookup_cache ()
5615 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5616 for (t
= previous_class_values
; t
; t
= TREE_CHAIN (t
))
5617 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t
)) = NULL_TREE
;
5619 previous_class_values
= NULL_TREE
;
5620 previous_class_type
= NULL_TREE
;
5623 /* Get out of the current class scope. If we were in a class scope
5624 previously, that is the one popped to. */
5630 /* Since poplevel_class does the popping of class decls nowadays,
5631 this really only frees the obstack used for these decls. */
5634 current_class_depth
--;
5635 current_class_name
= current_class_stack
[current_class_depth
].name
;
5636 current_class_type
= current_class_stack
[current_class_depth
].type
;
5637 current_access_specifier
= current_class_stack
[current_class_depth
].access
;
5638 if (current_class_stack
[current_class_depth
].names_used
)
5639 splay_tree_delete (current_class_stack
[current_class_depth
].names_used
);
5642 /* Returns 1 if current_class_type is either T or a nested type of T.
5643 We start looking from 1 because entry 0 is from global scope, and has
5647 currently_open_class (t
)
5651 if (t
== current_class_type
)
5653 for (i
= 1; i
< current_class_depth
; ++i
)
5654 if (current_class_stack
[i
].type
== t
)
5659 /* If either current_class_type or one of its enclosing classes are derived
5660 from T, return the appropriate type. Used to determine how we found
5661 something via unqualified lookup. */
5664 currently_open_derived_class (t
)
5669 if (DERIVED_FROM_P (t
, current_class_type
))
5670 return current_class_type
;
5672 for (i
= current_class_depth
- 1; i
> 0; --i
)
5673 if (DERIVED_FROM_P (t
, current_class_stack
[i
].type
))
5674 return current_class_stack
[i
].type
;
5679 /* When entering a class scope, all enclosing class scopes' names with
5680 static meaning (static variables, static functions, types and enumerators)
5681 have to be visible. This recursive function calls pushclass for all
5682 enclosing class contexts until global or a local scope is reached.
5683 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5684 formal of the same name. */
5687 push_nested_class (type
, modify
)
5693 /* A namespace might be passed in error cases, like A::B:C. */
5694 if (type
== NULL_TREE
5695 || type
== error_mark_node
5696 || TREE_CODE (type
) == NAMESPACE_DECL
5697 || ! IS_AGGR_TYPE (type
)
5698 || TREE_CODE (type
) == TEMPLATE_TYPE_PARM
5699 || TREE_CODE (type
) == BOUND_TEMPLATE_TEMPLATE_PARM
)
5702 context
= DECL_CONTEXT (TYPE_MAIN_DECL (type
));
5704 if (context
&& CLASS_TYPE_P (context
))
5705 push_nested_class (context
, 2);
5706 pushclass (type
, modify
);
5709 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5714 tree context
= DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type
));
5717 if (context
&& CLASS_TYPE_P (context
))
5718 pop_nested_class ();
5721 /* Returns the number of extern "LANG" blocks we are nested within. */
5724 current_lang_depth ()
5726 return VARRAY_ACTIVE_SIZE (current_lang_base
);
5729 /* Set global variables CURRENT_LANG_NAME to appropriate value
5730 so that behavior of name-mangling machinery is correct. */
5733 push_lang_context (name
)
5736 VARRAY_PUSH_TREE (current_lang_base
, current_lang_name
);
5738 if (name
== lang_name_cplusplus
)
5740 current_lang_name
= name
;
5742 else if (name
== lang_name_java
)
5744 current_lang_name
= name
;
5745 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5746 (See record_builtin_java_type in decl.c.) However, that causes
5747 incorrect debug entries if these types are actually used.
5748 So we re-enable debug output after extern "Java". */
5749 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node
)) = 0;
5750 DECL_IGNORED_P (TYPE_NAME (java_short_type_node
)) = 0;
5751 DECL_IGNORED_P (TYPE_NAME (java_int_type_node
)) = 0;
5752 DECL_IGNORED_P (TYPE_NAME (java_long_type_node
)) = 0;
5753 DECL_IGNORED_P (TYPE_NAME (java_float_type_node
)) = 0;
5754 DECL_IGNORED_P (TYPE_NAME (java_double_type_node
)) = 0;
5755 DECL_IGNORED_P (TYPE_NAME (java_char_type_node
)) = 0;
5756 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node
)) = 0;
5758 else if (name
== lang_name_c
)
5760 current_lang_name
= name
;
5763 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name
));
5766 /* Get out of the current language scope. */
5771 current_lang_name
= VARRAY_TOP_TREE (current_lang_base
);
5772 VARRAY_POP (current_lang_base
);
5775 /* Type instantiation routines. */
5777 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5778 matches the TARGET_TYPE. If there is no satisfactory match, return
5779 error_mark_node, and issue an error message if COMPLAIN is
5780 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5781 If TEMPLATE_ONLY, the name of the overloaded function
5782 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5783 template arguments. */
5786 resolve_address_of_overloaded_function (target_type
,
5797 tree explicit_targs
;
5799 /* Here's what the standard says:
5803 If the name is a function template, template argument deduction
5804 is done, and if the argument deduction succeeds, the deduced
5805 arguments are used to generate a single template function, which
5806 is added to the set of overloaded functions considered.
5808 Non-member functions and static member functions match targets of
5809 type "pointer-to-function" or "reference-to-function." Nonstatic
5810 member functions match targets of type "pointer-to-member
5811 function;" the function type of the pointer to member is used to
5812 select the member function from the set of overloaded member
5813 functions. If a nonstatic member function is selected, the
5814 reference to the overloaded function name is required to have the
5815 form of a pointer to member as described in 5.3.1.
5817 If more than one function is selected, any template functions in
5818 the set are eliminated if the set also contains a non-template
5819 function, and any given template function is eliminated if the
5820 set contains a second template function that is more specialized
5821 than the first according to the partial ordering rules 14.5.5.2.
5822 After such eliminations, if any, there shall remain exactly one
5823 selected function. */
5826 int is_reference
= 0;
5827 /* We store the matches in a TREE_LIST rooted here. The functions
5828 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5829 interoperability with most_specialized_instantiation. */
5830 tree matches
= NULL_TREE
;
5833 /* By the time we get here, we should be seeing only real
5834 pointer-to-member types, not the internal POINTER_TYPE to
5835 METHOD_TYPE representation. */
5836 my_friendly_assert (!(TREE_CODE (target_type
) == POINTER_TYPE
5837 && (TREE_CODE (TREE_TYPE (target_type
))
5838 == METHOD_TYPE
)), 0);
5840 if (TREE_CODE (overload
) == COMPONENT_REF
)
5841 overload
= TREE_OPERAND (overload
, 1);
5843 /* Check that the TARGET_TYPE is reasonable. */
5844 if (TYPE_PTRFN_P (target_type
))
5846 else if (TYPE_PTRMEMFUNC_P (target_type
))
5847 /* This is OK, too. */
5849 else if (TREE_CODE (target_type
) == FUNCTION_TYPE
)
5851 /* This is OK, too. This comes from a conversion to reference
5853 target_type
= build_reference_type (target_type
);
5860 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5861 DECL_NAME (OVL_FUNCTION (overload
)), target_type
);
5862 return error_mark_node
;
5865 /* If we can find a non-template function that matches, we can just
5866 use it. There's no point in generating template instantiations
5867 if we're just going to throw them out anyhow. But, of course, we
5868 can only do this when we don't *need* a template function. */
5873 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5875 tree fn
= OVL_FUNCTION (fns
);
5878 if (TREE_CODE (fn
) == TEMPLATE_DECL
)
5879 /* We're not looking for templates just yet. */
5882 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5884 /* We're looking for a non-static member, and this isn't
5885 one, or vice versa. */
5888 /* See if there's a match. */
5889 fntype
= TREE_TYPE (fn
);
5891 fntype
= build_ptrmemfunc_type (build_pointer_type (fntype
));
5892 else if (!is_reference
)
5893 fntype
= build_pointer_type (fntype
);
5895 if (can_convert_arg (target_type
, fntype
, fn
))
5896 matches
= tree_cons (fn
, NULL_TREE
, matches
);
5900 /* Now, if we've already got a match (or matches), there's no need
5901 to proceed to the template functions. But, if we don't have a
5902 match we need to look at them, too. */
5905 tree target_fn_type
;
5906 tree target_arg_types
;
5907 tree target_ret_type
;
5912 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type
));
5914 target_fn_type
= TREE_TYPE (target_type
);
5915 target_arg_types
= TYPE_ARG_TYPES (target_fn_type
);
5916 target_ret_type
= TREE_TYPE (target_fn_type
);
5918 /* Never do unification on the 'this' parameter. */
5919 if (TREE_CODE (target_fn_type
) == METHOD_TYPE
)
5920 target_arg_types
= TREE_CHAIN (target_arg_types
);
5922 for (fns
= overload
; fns
; fns
= OVL_CHAIN (fns
))
5924 tree fn
= OVL_FUNCTION (fns
);
5926 tree instantiation_type
;
5929 if (TREE_CODE (fn
) != TEMPLATE_DECL
)
5930 /* We're only looking for templates. */
5933 if ((TREE_CODE (TREE_TYPE (fn
)) == METHOD_TYPE
)
5935 /* We're not looking for a non-static member, and this is
5936 one, or vice versa. */
5939 /* Try to do argument deduction. */
5940 targs
= make_tree_vec (DECL_NTPARMS (fn
));
5941 if (fn_type_unification (fn
, explicit_targs
, targs
,
5942 target_arg_types
, target_ret_type
,
5943 DEDUCE_EXACT
, -1) != 0)
5944 /* Argument deduction failed. */
5947 /* Instantiate the template. */
5948 instantiation
= instantiate_template (fn
, targs
);
5949 if (instantiation
== error_mark_node
)
5950 /* Instantiation failed. */
5953 /* See if there's a match. */
5954 instantiation_type
= TREE_TYPE (instantiation
);
5956 instantiation_type
=
5957 build_ptrmemfunc_type (build_pointer_type (instantiation_type
));
5958 else if (!is_reference
)
5959 instantiation_type
= build_pointer_type (instantiation_type
);
5960 if (can_convert_arg (target_type
, instantiation_type
, instantiation
))
5961 matches
= tree_cons (instantiation
, fn
, matches
);
5964 /* Now, remove all but the most specialized of the matches. */
5967 tree match
= most_specialized_instantiation (matches
);
5969 if (match
!= error_mark_node
)
5970 matches
= tree_cons (match
, NULL_TREE
, NULL_TREE
);
5974 /* Now we should have exactly one function in MATCHES. */
5975 if (matches
== NULL_TREE
)
5977 /* There were *no* matches. */
5980 error ("no matches converting function `%D' to type `%#T'",
5981 DECL_NAME (OVL_FUNCTION (overload
)),
5984 /* print_candidates expects a chain with the functions in
5985 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5986 so why be clever?). */
5987 for (; overload
; overload
= OVL_NEXT (overload
))
5988 matches
= tree_cons (NULL_TREE
, OVL_CURRENT (overload
),
5991 print_candidates (matches
);
5993 return error_mark_node
;
5995 else if (TREE_CHAIN (matches
))
5997 /* There were too many matches. */
6003 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6004 DECL_NAME (OVL_FUNCTION (overload
)),
6007 /* Since print_candidates expects the functions in the
6008 TREE_VALUE slot, we flip them here. */
6009 for (match
= matches
; match
; match
= TREE_CHAIN (match
))
6010 TREE_VALUE (match
) = TREE_PURPOSE (match
);
6012 print_candidates (matches
);
6015 return error_mark_node
;
6018 /* Good, exactly one match. Now, convert it to the correct type. */
6019 fn
= TREE_PURPOSE (matches
);
6021 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn
)
6022 && !ptrmem
&& !flag_ms_extensions
)
6024 static int explained
;
6027 return error_mark_node
;
6029 pedwarn ("assuming pointer to member `%D'", fn
);
6032 pedwarn ("(a pointer to member can only be formed with `&%E')", fn
);
6038 if (TYPE_PTRFN_P (target_type
) || TYPE_PTRMEMFUNC_P (target_type
))
6039 return build_unary_op (ADDR_EXPR
, fn
, 0);
6042 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6043 will mark the function as addressed, but here we must do it
6045 cxx_mark_addressable (fn
);
6051 /* This function will instantiate the type of the expression given in
6052 RHS to match the type of LHSTYPE. If errors exist, then return
6053 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6054 we complain on errors. If we are not complaining, never modify rhs,
6055 as overload resolution wants to try many possible instantiations, in
6056 the hope that at least one will work.
6058 For non-recursive calls, LHSTYPE should be a function, pointer to
6059 function, or a pointer to member function. */
6062 instantiate_type (lhstype
, rhs
, flags
)
6064 tsubst_flags_t flags
;
6066 int complain
= (flags
& tf_error
);
6067 int strict
= (flags
& tf_no_attributes
)
6068 ? COMPARE_NO_ATTRIBUTES
: COMPARE_STRICT
;
6069 int allow_ptrmem
= flags
& tf_ptrmem_ok
;
6071 flags
&= ~tf_ptrmem_ok
;
6073 if (TREE_CODE (lhstype
) == UNKNOWN_TYPE
)
6076 error ("not enough type information");
6077 return error_mark_node
;
6080 if (TREE_TYPE (rhs
) != NULL_TREE
&& ! (type_unknown_p (rhs
)))
6082 if (comptypes (lhstype
, TREE_TYPE (rhs
), strict
))
6085 error ("argument of type `%T' does not match `%T'",
6086 TREE_TYPE (rhs
), lhstype
);
6087 return error_mark_node
;
6090 /* We don't overwrite rhs if it is an overloaded function.
6091 Copying it would destroy the tree link. */
6092 if (TREE_CODE (rhs
) != OVERLOAD
)
6093 rhs
= copy_node (rhs
);
6095 /* This should really only be used when attempting to distinguish
6096 what sort of a pointer to function we have. For now, any
6097 arithmetic operation which is not supported on pointers
6098 is rejected as an error. */
6100 switch (TREE_CODE (rhs
))
6108 return error_mark_node
;
6115 new_rhs
= instantiate_type (build_pointer_type (lhstype
),
6116 TREE_OPERAND (rhs
, 0), flags
);
6117 if (new_rhs
== error_mark_node
)
6118 return error_mark_node
;
6120 TREE_TYPE (rhs
) = lhstype
;
6121 TREE_OPERAND (rhs
, 0) = new_rhs
;
6126 rhs
= copy_node (TREE_OPERAND (rhs
, 0));
6127 TREE_TYPE (rhs
) = unknown_type_node
;
6128 return instantiate_type (lhstype
, rhs
, flags
);
6131 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6134 rhs
= TREE_OPERAND (rhs
, 1);
6135 if (BASELINK_P (rhs
))
6136 return instantiate_type (lhstype
, TREE_VALUE (rhs
),
6137 flags
| allow_ptrmem
);
6139 /* This can happen if we are forming a pointer-to-member for a
6141 my_friendly_assert (TREE_CODE (rhs
) == TEMPLATE_ID_EXPR
, 0);
6145 case TEMPLATE_ID_EXPR
:
6147 tree fns
= TREE_OPERAND (rhs
, 0);
6148 tree args
= TREE_OPERAND (rhs
, 1);
6151 resolve_address_of_overloaded_function (lhstype
,
6155 /*template_only=*/1,
6161 resolve_address_of_overloaded_function (lhstype
,
6165 /*template_only=*/0,
6166 /*explicit_targs=*/NULL_TREE
);
6169 /* Now we should have a baselink. */
6170 my_friendly_assert (BASELINK_P (rhs
), 990412);
6172 return instantiate_type (lhstype
, TREE_VALUE (rhs
), flags
);
6175 /* This is too hard for now. */
6177 return error_mark_node
;
6182 TREE_OPERAND (rhs
, 0)
6183 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6184 if (TREE_OPERAND (rhs
, 0) == error_mark_node
)
6185 return error_mark_node
;
6186 TREE_OPERAND (rhs
, 1)
6187 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6188 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6189 return error_mark_node
;
6191 TREE_TYPE (rhs
) = lhstype
;
6195 case TRUNC_DIV_EXPR
:
6196 case FLOOR_DIV_EXPR
:
6198 case ROUND_DIV_EXPR
:
6200 case TRUNC_MOD_EXPR
:
6201 case FLOOR_MOD_EXPR
:
6203 case ROUND_MOD_EXPR
:
6204 case FIX_ROUND_EXPR
:
6205 case FIX_FLOOR_EXPR
:
6207 case FIX_TRUNC_EXPR
:
6223 case PREINCREMENT_EXPR
:
6224 case PREDECREMENT_EXPR
:
6225 case POSTINCREMENT_EXPR
:
6226 case POSTDECREMENT_EXPR
:
6228 error ("invalid operation on uninstantiated type");
6229 return error_mark_node
;
6231 case TRUTH_AND_EXPR
:
6233 case TRUTH_XOR_EXPR
:
6240 case TRUTH_ANDIF_EXPR
:
6241 case TRUTH_ORIF_EXPR
:
6242 case TRUTH_NOT_EXPR
:
6244 error ("not enough type information");
6245 return error_mark_node
;
6248 if (type_unknown_p (TREE_OPERAND (rhs
, 0)))
6251 error ("not enough type information");
6252 return error_mark_node
;
6254 TREE_OPERAND (rhs
, 1)
6255 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6256 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6257 return error_mark_node
;
6258 TREE_OPERAND (rhs
, 2)
6259 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 2), flags
);
6260 if (TREE_OPERAND (rhs
, 2) == error_mark_node
)
6261 return error_mark_node
;
6263 TREE_TYPE (rhs
) = lhstype
;
6267 TREE_OPERAND (rhs
, 1)
6268 = instantiate_type (lhstype
, TREE_OPERAND (rhs
, 1), flags
);
6269 if (TREE_OPERAND (rhs
, 1) == error_mark_node
)
6270 return error_mark_node
;
6272 TREE_TYPE (rhs
) = lhstype
;
6277 if (PTRMEM_OK_P (rhs
))
6278 flags
|= tf_ptrmem_ok
;
6280 return instantiate_type (lhstype
, TREE_OPERAND (rhs
, 0), flags
);
6282 case ENTRY_VALUE_EXPR
:
6284 return error_mark_node
;
6287 return error_mark_node
;
6291 return error_mark_node
;
6295 /* Return the name of the virtual function pointer field
6296 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6297 this may have to look back through base types to find the
6298 ultimate field name. (For single inheritance, these could
6299 all be the same name. Who knows for multiple inheritance). */
6302 get_vfield_name (type
)
6305 tree binfo
= TYPE_BINFO (type
);
6308 while (BINFO_BASETYPES (binfo
)
6309 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo
, 0)))
6310 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo
, 0)))
6311 binfo
= BINFO_BASETYPE (binfo
, 0);
6313 type
= BINFO_TYPE (binfo
);
6314 buf
= (char *) alloca (sizeof (VFIELD_NAME_FORMAT
)
6315 + TYPE_NAME_LENGTH (type
) + 2);
6316 sprintf (buf
, VFIELD_NAME_FORMAT
,
6317 IDENTIFIER_POINTER (constructor_name (type
)));
6318 return get_identifier (buf
);
6322 print_class_statistics ()
6324 #ifdef GATHER_STATISTICS
6325 fprintf (stderr
, "convert_harshness = %d\n", n_convert_harshness
);
6326 fprintf (stderr
, "compute_conversion_costs = %d\n", n_compute_conversion_costs
);
6327 fprintf (stderr
, "build_method_call = %d (inner = %d)\n",
6328 n_build_method_call
, n_inner_fields_searched
);
6331 fprintf (stderr
, "vtables = %d; vtable searches = %d\n",
6332 n_vtables
, n_vtable_searches
);
6333 fprintf (stderr
, "vtable entries = %d; vtable elems = %d\n",
6334 n_vtable_entries
, n_vtable_elems
);
6339 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6340 according to [class]:
6341 The class-name is also inserted
6342 into the scope of the class itself. For purposes of access checking,
6343 the inserted class name is treated as if it were a public member name. */
6346 build_self_reference ()
6348 tree name
= constructor_name (current_class_type
);
6349 tree value
= build_lang_decl (TYPE_DECL
, name
, current_class_type
);
6352 DECL_NONLOCAL (value
) = 1;
6353 DECL_CONTEXT (value
) = current_class_type
;
6354 DECL_ARTIFICIAL (value
) = 1;
6356 if (processing_template_decl
)
6357 value
= push_template_decl (value
);
6359 saved_cas
= current_access_specifier
;
6360 current_access_specifier
= access_public_node
;
6361 finish_member_declaration (value
);
6362 current_access_specifier
= saved_cas
;
6365 /* Returns 1 if TYPE contains only padding bytes. */
6368 is_empty_class (type
)
6371 if (type
== error_mark_node
)
6374 if (! IS_AGGR_TYPE (type
))
6377 return integer_zerop (CLASSTYPE_SIZE (type
));
6380 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6381 a *_TYPE node. NODE can also be a local class. */
6384 get_enclosing_class (type
)
6389 while (node
&& TREE_CODE (node
) != NAMESPACE_DECL
)
6391 switch (TREE_CODE_CLASS (TREE_CODE (node
)))
6394 node
= DECL_CONTEXT (node
);
6400 node
= TYPE_CONTEXT (node
);
6410 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6413 is_base_of_enclosing_class (base
, type
)
6418 if (lookup_base (type
, base
, ba_any
, NULL
))
6421 type
= get_enclosing_class (type
);
6426 /* Note that NAME was looked up while the current class was being
6427 defined and that the result of that lookup was DECL. */
6430 maybe_note_name_used_in_class (name
, decl
)
6434 splay_tree names_used
;
6436 /* If we're not defining a class, there's nothing to do. */
6437 if (!current_class_type
|| !TYPE_BEING_DEFINED (current_class_type
))
6440 /* If there's already a binding for this NAME, then we don't have
6441 anything to worry about. */
6442 if (IDENTIFIER_CLASS_VALUE (name
))
6445 if (!current_class_stack
[current_class_depth
- 1].names_used
)
6446 current_class_stack
[current_class_depth
- 1].names_used
6447 = splay_tree_new (splay_tree_compare_pointers
, 0, 0);
6448 names_used
= current_class_stack
[current_class_depth
- 1].names_used
;
6450 splay_tree_insert (names_used
,
6451 (splay_tree_key
) name
,
6452 (splay_tree_value
) decl
);
6455 /* Note that NAME was declared (as DECL) in the current class. Check
6456 to see that the declaration is legal. */
6459 note_name_declared_in_class (name
, decl
)
6463 splay_tree names_used
;
6466 /* Look to see if we ever used this name. */
6468 = current_class_stack
[current_class_depth
- 1].names_used
;
6472 n
= splay_tree_lookup (names_used
, (splay_tree_key
) name
);
6475 /* [basic.scope.class]
6477 A name N used in a class S shall refer to the same declaration
6478 in its context and when re-evaluated in the completed scope of
6480 error ("declaration of `%#D'", decl
);
6481 cp_error_at ("changes meaning of `%D' from `%+#D'",
6482 DECL_NAME (OVL_CURRENT (decl
)),
6487 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6488 Secondary vtables are merged with primary vtables; this function
6489 will return the VAR_DECL for the primary vtable. */
6492 get_vtbl_decl_for_binfo (binfo
)
6497 decl
= BINFO_VTABLE (binfo
);
6498 if (decl
&& TREE_CODE (decl
) == PLUS_EXPR
)
6500 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl
, 0)) == ADDR_EXPR
,
6502 decl
= TREE_OPERAND (TREE_OPERAND (decl
, 0), 0);
6505 my_friendly_assert (TREE_CODE (decl
) == VAR_DECL
, 20000403);
6509 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6510 who's TREE_PURPOSE is the TYPE of the required primary base and
6511 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6514 dfs_get_primary_binfo (binfo
, data
)
6518 tree cons
= (tree
) data
;
6519 tree primary_base
= TREE_PURPOSE (cons
);
6521 if (TREE_VIA_VIRTUAL (binfo
)
6522 && same_type_p (BINFO_TYPE (binfo
), primary_base
))
6523 /* This is the right type of binfo, but it might be an unshared
6524 instance, and the shared instance is later in the dfs walk. We
6525 must keep looking. */
6526 TREE_VALUE (cons
) = tree_cons (NULL
, binfo
, TREE_VALUE (cons
));
6531 /* Returns the unshared binfo for the primary base of BINFO. Note
6532 that in a complex hierarchy the resulting BINFO may not actually
6533 *be* primary. In particular if the resulting BINFO is a virtual
6534 base, and it occurs elsewhere in the hierarchy, then this
6535 occurrence may not actually be a primary base in the complete
6536 object. Check BINFO_PRIMARY_P to be sure. */
6539 get_primary_binfo (binfo
)
6543 tree result
= NULL_TREE
;
6546 primary_base
= CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo
));
6550 /* A non-virtual primary base is always a direct base, and easy to
6552 if (!TREE_VIA_VIRTUAL (primary_base
))
6556 /* Scan the direct basetypes until we find a base with the same
6557 type as the primary base. */
6558 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6560 tree base_binfo
= BINFO_BASETYPE (binfo
, i
);
6562 if (same_type_p (BINFO_TYPE (base_binfo
),
6563 BINFO_TYPE (primary_base
)))
6567 /* We should always find the primary base. */
6571 /* For a primary virtual base, we have to scan the entire hierarchy
6572 rooted at BINFO; the virtual base could be an indirect virtual
6573 base. There could be more than one instance of the primary base
6574 in the hierarchy, and if one is the canonical binfo we want that
6575 one. If it exists, it should be the first one we find, but as a
6576 consistency check we find them all and make sure. */
6577 virtuals
= build_tree_list (BINFO_TYPE (primary_base
), NULL_TREE
);
6578 dfs_walk (binfo
, dfs_get_primary_binfo
, NULL
, virtuals
);
6579 virtuals
= TREE_VALUE (virtuals
);
6581 /* We must have found at least one instance. */
6582 my_friendly_assert (virtuals
, 20010612);
6584 if (TREE_CHAIN (virtuals
))
6586 /* We found more than one instance of the base. We must make
6587 sure that, if one is the canonical one, it is the first one
6588 we found. As the chain is in reverse dfs order, that means
6589 the last on the list. */
6590 tree complete_binfo
;
6593 for (complete_binfo
= binfo
;
6594 BINFO_INHERITANCE_CHAIN (complete_binfo
);
6595 complete_binfo
= BINFO_INHERITANCE_CHAIN (complete_binfo
))
6597 canonical
= binfo_for_vbase (BINFO_TYPE (primary_base
),
6598 BINFO_TYPE (complete_binfo
));
6600 for (; virtuals
; virtuals
= TREE_CHAIN (virtuals
))
6602 result
= TREE_VALUE (virtuals
);
6604 if (canonical
== result
)
6606 /* This is the unshared instance. Make sure it was the
6608 my_friendly_assert (!TREE_CHAIN (virtuals
), 20010612);
6614 result
= TREE_VALUE (virtuals
);
6618 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6621 maybe_indent_hierarchy (stream
, indent
, indented_p
)
6627 fprintf (stream
, "%*s", indent
, "");
6631 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6632 dominated by T) to stderr. INDENT should be zero when called from
6633 the top level; it is incremented recursively. */
6636 dump_class_hierarchy_r (stream
, flags
, t
, binfo
, indent
)
6646 indented
= maybe_indent_hierarchy (stream
, indent
, 0);
6647 fprintf (stream
, "%s (0x%lx) ",
6648 type_as_string (binfo
, TFF_PLAIN_IDENTIFIER
),
6649 (unsigned long) binfo
);
6650 fprintf (stream
, HOST_WIDE_INT_PRINT_DEC
,
6651 tree_low_cst (BINFO_OFFSET (binfo
), 0));
6652 if (is_empty_class (BINFO_TYPE (binfo
)))
6653 fprintf (stream
, " empty");
6654 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo
)))
6655 fprintf (stream
, " nearly-empty");
6656 if (TREE_VIA_VIRTUAL (binfo
))
6658 tree canonical
= binfo_for_vbase (BINFO_TYPE (binfo
), t
);
6660 fprintf (stream
, " virtual");
6661 if (canonical
== binfo
)
6662 fprintf (stream
, " canonical");
6664 fprintf (stream
, " non-canonical");
6666 fprintf (stream
, "\n");
6669 if (BINFO_PRIMARY_BASE_OF (binfo
))
6671 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6672 fprintf (stream
, " primary-for %s (0x%lx)",
6673 type_as_string (BINFO_PRIMARY_BASE_OF (binfo
),
6674 TFF_PLAIN_IDENTIFIER
),
6675 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo
));
6677 if (BINFO_LOST_PRIMARY_P (binfo
))
6679 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6680 fprintf (stream
, " lost-primary");
6683 fprintf (stream
, "\n");
6685 if (!(flags
& TDF_SLIM
))
6689 if (BINFO_SUBVTT_INDEX (binfo
))
6691 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6692 fprintf (stream
, " subvttidx=%s",
6693 expr_as_string (BINFO_SUBVTT_INDEX (binfo
),
6694 TFF_PLAIN_IDENTIFIER
));
6696 if (BINFO_VPTR_INDEX (binfo
))
6698 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6699 fprintf (stream
, " vptridx=%s",
6700 expr_as_string (BINFO_VPTR_INDEX (binfo
),
6701 TFF_PLAIN_IDENTIFIER
));
6703 if (BINFO_VPTR_FIELD (binfo
))
6705 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6706 fprintf (stream
, " vbaseoffset=%s",
6707 expr_as_string (BINFO_VPTR_FIELD (binfo
),
6708 TFF_PLAIN_IDENTIFIER
));
6710 if (BINFO_VTABLE (binfo
))
6712 indented
= maybe_indent_hierarchy (stream
, indent
+ 3, indented
);
6713 fprintf (stream
, " vptr=%s",
6714 expr_as_string (BINFO_VTABLE (binfo
),
6715 TFF_PLAIN_IDENTIFIER
));
6719 fprintf (stream
, "\n");
6723 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
6724 dump_class_hierarchy_r (stream
, flags
,
6725 t
, BINFO_BASETYPE (binfo
, i
),
6729 /* Dump the BINFO hierarchy for T. */
6732 dump_class_hierarchy (t
)
6736 FILE *stream
= dump_begin (TDI_class
, &flags
);
6741 fprintf (stream
, "Class %s\n", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
6742 fprintf (stream
, " size=%lu align=%lu\n",
6743 (unsigned long)(tree_low_cst (TYPE_SIZE (t
), 0) / BITS_PER_UNIT
),
6744 (unsigned long)(TYPE_ALIGN (t
) / BITS_PER_UNIT
));
6745 dump_class_hierarchy_r (stream
, flags
, t
, TYPE_BINFO (t
), 0);
6746 fprintf (stream
, "\n");
6747 dump_end (TDI_class
, stream
);
6751 dump_array (stream
, decl
)
6758 tree size
= TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl
)));
6760 elt
= (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl
))), 0)
6762 fprintf (stream
, "%s:", decl_as_string (decl
, TFF_PLAIN_IDENTIFIER
));
6763 fprintf (stream
, " %s entries",
6764 expr_as_string (size_binop (PLUS_EXPR
, size
, size_one_node
),
6765 TFF_PLAIN_IDENTIFIER
));
6766 fprintf (stream
, "\n");
6768 for (ix
= 0, inits
= TREE_OPERAND (DECL_INITIAL (decl
), 1);
6769 inits
; ix
++, inits
= TREE_CHAIN (inits
))
6770 fprintf (stream
, "%-4ld %s\n", (long)(ix
* elt
),
6771 expr_as_string (TREE_VALUE (inits
), TFF_PLAIN_IDENTIFIER
));
6775 dump_vtable (t
, binfo
, vtable
)
6781 FILE *stream
= dump_begin (TDI_class
, &flags
);
6786 if (!(flags
& TDF_SLIM
))
6788 int ctor_vtbl_p
= TYPE_BINFO (t
) != binfo
;
6790 fprintf (stream
, "%s for %s",
6791 ctor_vtbl_p
? "Construction vtable" : "Vtable",
6792 type_as_string (binfo
, TFF_PLAIN_IDENTIFIER
));
6795 if (!TREE_VIA_VIRTUAL (binfo
))
6796 fprintf (stream
, " (0x%lx instance)", (unsigned long)binfo
);
6797 fprintf (stream
, " in %s", type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
6799 fprintf (stream
, "\n");
6800 dump_array (stream
, vtable
);
6801 fprintf (stream
, "\n");
6804 dump_end (TDI_class
, stream
);
6813 FILE *stream
= dump_begin (TDI_class
, &flags
);
6818 if (!(flags
& TDF_SLIM
))
6820 fprintf (stream
, "VTT for %s\n",
6821 type_as_string (t
, TFF_PLAIN_IDENTIFIER
));
6822 dump_array (stream
, vtt
);
6823 fprintf (stream
, "\n");
6826 dump_end (TDI_class
, stream
);
6829 /* Virtual function table initialization. */
6831 /* Create all the necessary vtables for T and its base classes. */
6841 /* We lay out the primary and secondary vtables in one contiguous
6842 vtable. The primary vtable is first, followed by the non-virtual
6843 secondary vtables in inheritance graph order. */
6844 list
= build_tree_list (TYPE_BINFO_VTABLE (t
), NULL_TREE
);
6845 accumulate_vtbl_inits (TYPE_BINFO (t
), TYPE_BINFO (t
),
6846 TYPE_BINFO (t
), t
, list
);
6848 /* Then come the virtual bases, also in inheritance graph order. */
6849 for (vbase
= TYPE_BINFO (t
); vbase
; vbase
= TREE_CHAIN (vbase
))
6853 if (!TREE_VIA_VIRTUAL (vbase
))
6856 /* Although we walk in inheritance order, that might not get the
6858 real_base
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
6860 accumulate_vtbl_inits (real_base
, real_base
,
6861 TYPE_BINFO (t
), t
, list
);
6864 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
6865 base classes, for the benefit of the debugging backends. */
6866 for (i
= 0; i
< BINFO_N_BASETYPES (TYPE_BINFO (t
)); ++i
)
6868 tree base
= BINFO_BASETYPE (TYPE_BINFO (t
), i
);
6869 if (TREE_VIA_VIRTUAL (base
))
6871 vbase
= binfo_for_vbase (BINFO_TYPE (base
), t
);
6872 BINFO_VPTR_FIELD (base
) = BINFO_VPTR_FIELD (vbase
);
6876 if (TYPE_BINFO_VTABLE (t
))
6877 initialize_vtable (TYPE_BINFO (t
), TREE_VALUE (list
));
6880 /* Initialize the vtable for BINFO with the INITS. */
6883 initialize_vtable (binfo
, inits
)
6889 layout_vtable_decl (binfo
, list_length (inits
));
6890 decl
= get_vtbl_decl_for_binfo (binfo
);
6891 initialize_array (decl
, inits
);
6892 dump_vtable (BINFO_TYPE (binfo
), binfo
, decl
);
6895 /* Initialize DECL (a declaration for a namespace-scope array) with
6899 initialize_array (decl
, inits
)
6905 context
= DECL_CONTEXT (decl
);
6906 DECL_CONTEXT (decl
) = NULL_TREE
;
6907 DECL_INITIAL (decl
) = build_nt (CONSTRUCTOR
, NULL_TREE
, inits
);
6908 cp_finish_decl (decl
, DECL_INITIAL (decl
), NULL_TREE
, 0);
6909 DECL_CONTEXT (decl
) = context
;
6912 /* Build the VTT (virtual table table) for T.
6913 A class requires a VTT if it has virtual bases.
6916 1 - primary virtual pointer for complete object T
6917 2 - secondary VTTs for each direct non-virtual base of T which requires a
6919 3 - secondary virtual pointers for each direct or indirect base of T which
6920 has virtual bases or is reachable via a virtual path from T.
6921 4 - secondary VTTs for each direct or indirect virtual base of T.
6923 Secondary VTTs look like complete object VTTs without part 4. */
6934 /* Build up the initializers for the VTT. */
6936 index
= size_zero_node
;
6937 build_vtt_inits (TYPE_BINFO (t
), t
, &inits
, &index
);
6939 /* If we didn't need a VTT, we're done. */
6943 /* Figure out the type of the VTT. */
6944 type
= build_index_type (size_int (list_length (inits
) - 1));
6945 type
= build_cplus_array_type (const_ptr_type_node
, type
);
6947 /* Now, build the VTT object itself. */
6948 vtt
= build_vtable (t
, get_vtt_name (t
), type
);
6949 pushdecl_top_level (vtt
);
6950 initialize_array (vtt
, inits
);
6955 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6956 from within some hierarchy which is inherited from the type of BINFO.
6957 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6961 get_original_base (base_binfo
, binfo
)
6968 if (same_type_p (BINFO_TYPE (base_binfo
), BINFO_TYPE (binfo
)))
6970 if (TREE_VIA_VIRTUAL (base_binfo
))
6971 return binfo_for_vbase (BINFO_TYPE (base_binfo
), BINFO_TYPE (binfo
));
6972 derived
= get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo
), binfo
);
6974 for (ix
= 0; ix
!= BINFO_N_BASETYPES (derived
); ix
++)
6975 if (same_type_p (BINFO_TYPE (base_binfo
),
6976 BINFO_TYPE (BINFO_BASETYPE (derived
, ix
))))
6977 return BINFO_BASETYPE (derived
, ix
);
6982 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6983 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6984 and CHAIN the vtable pointer for this binfo after construction is
6985 complete. VALUE can also be another BINFO, in which case we recurse. */
6988 binfo_ctor_vtable (binfo
)
6995 vt
= BINFO_VTABLE (binfo
);
6996 if (TREE_CODE (vt
) == TREE_LIST
)
6997 vt
= TREE_VALUE (vt
);
6998 if (TREE_CODE (vt
) == TREE_VEC
)
7007 /* Recursively build the VTT-initializer for BINFO (which is in the
7008 hierarchy dominated by T). INITS points to the end of the initializer
7009 list to date. INDEX is the VTT index where the next element will be
7010 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7011 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7012 for virtual bases of T. When it is not so, we build the constructor
7013 vtables for the BINFO-in-T variant. */
7016 build_vtt_inits (binfo
, t
, inits
, index
)
7025 tree secondary_vptrs
;
7026 int top_level_p
= same_type_p (TREE_TYPE (binfo
), t
);
7028 /* We only need VTTs for subobjects with virtual bases. */
7029 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
)))
7032 /* We need to use a construction vtable if this is not the primary
7036 build_ctor_vtbl_group (binfo
, t
);
7038 /* Record the offset in the VTT where this sub-VTT can be found. */
7039 BINFO_SUBVTT_INDEX (binfo
) = *index
;
7042 /* Add the address of the primary vtable for the complete object. */
7043 init
= binfo_ctor_vtable (binfo
);
7044 *inits
= build_tree_list (NULL_TREE
, init
);
7045 inits
= &TREE_CHAIN (*inits
);
7048 my_friendly_assert (!BINFO_VPTR_INDEX (binfo
), 20010129);
7049 BINFO_VPTR_INDEX (binfo
) = *index
;
7051 *index
= size_binop (PLUS_EXPR
, *index
, TYPE_SIZE_UNIT (ptr_type_node
));
7053 /* Recursively add the secondary VTTs for non-virtual bases. */
7054 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
7056 b
= BINFO_BASETYPE (binfo
, i
);
7057 if (!TREE_VIA_VIRTUAL (b
))
7058 inits
= build_vtt_inits (BINFO_BASETYPE (binfo
, i
), t
,
7062 /* Add secondary virtual pointers for all subobjects of BINFO with
7063 either virtual bases or reachable along a virtual path, except
7064 subobjects that are non-virtual primary bases. */
7065 secondary_vptrs
= tree_cons (t
, NULL_TREE
, BINFO_TYPE (binfo
));
7066 TREE_TYPE (secondary_vptrs
) = *index
;
7067 VTT_TOP_LEVEL_P (secondary_vptrs
) = top_level_p
;
7068 VTT_MARKED_BINFO_P (secondary_vptrs
) = 0;
7070 dfs_walk_real (binfo
,
7071 dfs_build_secondary_vptr_vtt_inits
,
7073 dfs_ctor_vtable_bases_queue_p
,
7075 VTT_MARKED_BINFO_P (secondary_vptrs
) = 1;
7076 dfs_walk (binfo
, dfs_unmark
, dfs_ctor_vtable_bases_queue_p
,
7079 *index
= TREE_TYPE (secondary_vptrs
);
7081 /* The secondary vptrs come back in reverse order. After we reverse
7082 them, and add the INITS, the last init will be the first element
7084 secondary_vptrs
= TREE_VALUE (secondary_vptrs
);
7085 if (secondary_vptrs
)
7087 *inits
= nreverse (secondary_vptrs
);
7088 inits
= &TREE_CHAIN (secondary_vptrs
);
7089 my_friendly_assert (*inits
== NULL_TREE
, 20000517);
7092 /* Add the secondary VTTs for virtual bases. */
7094 for (b
= TYPE_BINFO (BINFO_TYPE (binfo
)); b
; b
= TREE_CHAIN (b
))
7098 if (!TREE_VIA_VIRTUAL (b
))
7101 vbase
= binfo_for_vbase (BINFO_TYPE (b
), t
);
7102 inits
= build_vtt_inits (vbase
, t
, inits
, index
);
7107 tree data
= tree_cons (t
, binfo
, NULL_TREE
);
7108 VTT_TOP_LEVEL_P (data
) = 0;
7109 VTT_MARKED_BINFO_P (data
) = 0;
7111 dfs_walk (binfo
, dfs_fixup_binfo_vtbls
,
7112 dfs_ctor_vtable_bases_queue_p
,
7119 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7120 for the base in most derived. DATA is a TREE_LIST who's
7121 TREE_CHAIN is the type of the base being
7122 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7123 flag of DATA indicates that this is a constructor vtable. The
7124 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7127 dfs_build_secondary_vptr_vtt_inits (binfo
, data
)
7139 top_level_p
= VTT_TOP_LEVEL_P (l
);
7141 SET_BINFO_MARKED (binfo
);
7143 /* We don't care about bases that don't have vtables. */
7144 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
7147 /* We're only interested in proper subobjects of T. */
7148 if (same_type_p (BINFO_TYPE (binfo
), t
))
7151 /* We're not interested in non-virtual primary bases. */
7152 if (!TREE_VIA_VIRTUAL (binfo
) && BINFO_PRIMARY_P (binfo
))
7155 /* If BINFO has virtual bases or is reachable via a virtual path
7156 from T, it'll have a secondary vptr. */
7157 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
))
7158 && !binfo_via_virtual (binfo
, t
))
7161 /* Record the index where this secondary vptr can be found. */
7162 index
= TREE_TYPE (l
);
7165 my_friendly_assert (!BINFO_VPTR_INDEX (binfo
), 20010129);
7166 BINFO_VPTR_INDEX (binfo
) = index
;
7168 TREE_TYPE (l
) = size_binop (PLUS_EXPR
, index
,
7169 TYPE_SIZE_UNIT (ptr_type_node
));
7171 /* Add the initializer for the secondary vptr itself. */
7172 if (top_level_p
&& TREE_VIA_VIRTUAL (binfo
))
7174 /* It's a primary virtual base, and this is not the construction
7175 vtable. Find the base this is primary of in the inheritance graph,
7176 and use that base's vtable now. */
7177 while (BINFO_PRIMARY_BASE_OF (binfo
))
7178 binfo
= BINFO_PRIMARY_BASE_OF (binfo
);
7180 init
= binfo_ctor_vtable (binfo
);
7181 TREE_VALUE (l
) = tree_cons (NULL_TREE
, init
, TREE_VALUE (l
));
7186 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7187 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7188 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7192 dfs_ctor_vtable_bases_queue_p (binfo
, data
)
7196 if (TREE_VIA_VIRTUAL (binfo
))
7197 /* Get the shared version. */
7198 binfo
= binfo_for_vbase (BINFO_TYPE (binfo
), TREE_PURPOSE ((tree
) data
));
7200 if (!BINFO_MARKED (binfo
) == VTT_MARKED_BINFO_P ((tree
) data
))
7205 /* Called from build_vtt_inits via dfs_walk. After building constructor
7206 vtables and generating the sub-vtt from them, we need to restore the
7207 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7208 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7211 dfs_fixup_binfo_vtbls (binfo
, data
)
7215 CLEAR_BINFO_MARKED (binfo
);
7217 /* We don't care about bases that don't have vtables. */
7218 if (!TYPE_VFIELD (BINFO_TYPE (binfo
)))
7221 /* If we scribbled the construction vtable vptr into BINFO, clear it
7223 if (BINFO_VTABLE (binfo
)
7224 && TREE_CODE (BINFO_VTABLE (binfo
)) == TREE_LIST
7225 && (TREE_PURPOSE (BINFO_VTABLE (binfo
))
7226 == TREE_VALUE ((tree
) data
)))
7227 BINFO_VTABLE (binfo
) = TREE_CHAIN (BINFO_VTABLE (binfo
));
7232 /* Build the construction vtable group for BINFO which is in the
7233 hierarchy dominated by T. */
7236 build_ctor_vtbl_group (binfo
, t
)
7247 /* See if we've already created this construction vtable group. */
7248 id
= mangle_ctor_vtbl_for_type (t
, binfo
);
7249 if (IDENTIFIER_GLOBAL_VALUE (id
))
7252 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo
), t
), 20010124);
7253 /* Build a version of VTBL (with the wrong type) for use in
7254 constructing the addresses of secondary vtables in the
7255 construction vtable group. */
7256 vtbl
= build_vtable (t
, id
, ptr_type_node
);
7257 list
= build_tree_list (vtbl
, NULL_TREE
);
7258 accumulate_vtbl_inits (binfo
, TYPE_BINFO (TREE_TYPE (binfo
)),
7261 /* Add the vtables for each of our virtual bases using the vbase in T
7263 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
7265 vbase
= TREE_CHAIN (vbase
))
7270 if (!TREE_VIA_VIRTUAL (vbase
))
7272 b
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
7273 orig_base
= binfo_for_vbase (BINFO_TYPE (vbase
), BINFO_TYPE (binfo
));
7275 accumulate_vtbl_inits (b
, orig_base
, binfo
, t
, list
);
7277 inits
= TREE_VALUE (list
);
7279 /* Figure out the type of the construction vtable. */
7280 type
= build_index_type (size_int (list_length (inits
) - 1));
7281 type
= build_cplus_array_type (vtable_entry_type
, type
);
7282 TREE_TYPE (vtbl
) = type
;
7284 /* Initialize the construction vtable. */
7285 pushdecl_top_level (vtbl
);
7286 initialize_array (vtbl
, inits
);
7287 dump_vtable (t
, binfo
, vtbl
);
7290 /* Add the vtbl initializers for BINFO (and its bases other than
7291 non-virtual primaries) to the list of INITS. BINFO is in the
7292 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7293 the constructor the vtbl inits should be accumulated for. (If this
7294 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7295 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7296 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7297 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7298 but are not necessarily the same in terms of layout. */
7301 accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, t
, inits
)
7309 int ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7311 my_friendly_assert (same_type_p (BINFO_TYPE (binfo
),
7312 BINFO_TYPE (orig_binfo
)),
7315 /* If it doesn't have a vptr, we don't do anything. */
7316 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo
)))
7319 /* If we're building a construction vtable, we're not interested in
7320 subobjects that don't require construction vtables. */
7322 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
))
7323 && !binfo_via_virtual (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
7326 /* Build the initializers for the BINFO-in-T vtable. */
7328 = chainon (TREE_VALUE (inits
),
7329 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
,
7330 rtti_binfo
, t
, inits
));
7332 /* Walk the BINFO and its bases. We walk in preorder so that as we
7333 initialize each vtable we can figure out at what offset the
7334 secondary vtable lies from the primary vtable. We can't use
7335 dfs_walk here because we need to iterate through bases of BINFO
7336 and RTTI_BINFO simultaneously. */
7337 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
7339 tree base_binfo
= BINFO_BASETYPE (binfo
, i
);
7341 /* Skip virtual bases. */
7342 if (TREE_VIA_VIRTUAL (base_binfo
))
7344 accumulate_vtbl_inits (base_binfo
,
7345 BINFO_BASETYPE (orig_binfo
, i
),
7351 /* Called from accumulate_vtbl_inits. Returns the initializers for
7352 the BINFO vtable. */
7355 dfs_accumulate_vtbl_inits (binfo
, orig_binfo
, rtti_binfo
, t
, l
)
7362 tree inits
= NULL_TREE
;
7363 tree vtbl
= NULL_TREE
;
7364 int ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7367 && TREE_VIA_VIRTUAL (orig_binfo
) && BINFO_PRIMARY_P (orig_binfo
))
7369 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7370 primary virtual base. If it is not the same primary in
7371 the hierarchy of T, we'll need to generate a ctor vtable
7372 for it, to place at its location in T. If it is the same
7373 primary, we still need a VTT entry for the vtable, but it
7374 should point to the ctor vtable for the base it is a
7375 primary for within the sub-hierarchy of RTTI_BINFO.
7377 There are three possible cases:
7379 1) We are in the same place.
7380 2) We are a primary base within a lost primary virtual base of
7382 3) We are primary to something not a base of RTTI_BINFO. */
7384 tree b
= BINFO_PRIMARY_BASE_OF (binfo
);
7385 tree last
= NULL_TREE
;
7387 /* First, look through the bases we are primary to for RTTI_BINFO
7388 or a virtual base. */
7389 for (; b
; b
= BINFO_PRIMARY_BASE_OF (b
))
7392 if (TREE_VIA_VIRTUAL (b
) || b
== rtti_binfo
)
7395 /* If we run out of primary links, keep looking down our
7396 inheritance chain; we might be an indirect primary. */
7398 for (b
= last
; b
; b
= BINFO_INHERITANCE_CHAIN (b
))
7399 if (TREE_VIA_VIRTUAL (b
) || b
== rtti_binfo
)
7402 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7403 base B and it is a base of RTTI_BINFO, this is case 2. In
7404 either case, we share our vtable with LAST, i.e. the
7405 derived-most base within B of which we are a primary. */
7407 || (b
&& binfo_for_vbase (BINFO_TYPE (b
),
7408 BINFO_TYPE (rtti_binfo
))))
7409 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7410 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7411 binfo_ctor_vtable after everything's been set up. */
7414 /* Otherwise, this is case 3 and we get our own. */
7416 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo
, BINFO_TYPE (rtti_binfo
)))
7424 /* Compute the initializer for this vtable. */
7425 inits
= build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
,
7428 /* Figure out the position to which the VPTR should point. */
7429 vtbl
= TREE_PURPOSE (l
);
7430 vtbl
= build1 (ADDR_EXPR
,
7433 TREE_CONSTANT (vtbl
) = 1;
7434 index
= size_binop (PLUS_EXPR
,
7435 size_int (non_fn_entries
),
7436 size_int (list_length (TREE_VALUE (l
))));
7437 index
= size_binop (MULT_EXPR
,
7438 TYPE_SIZE_UNIT (vtable_entry_type
),
7440 vtbl
= build (PLUS_EXPR
, TREE_TYPE (vtbl
), vtbl
, index
);
7441 TREE_CONSTANT (vtbl
) = 1;
7445 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7446 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7447 straighten this out. */
7448 BINFO_VTABLE (binfo
) = tree_cons (rtti_binfo
, vtbl
, BINFO_VTABLE (binfo
));
7449 else if (BINFO_PRIMARY_P (binfo
) && TREE_VIA_VIRTUAL (binfo
))
7452 /* For an ordinary vtable, set BINFO_VTABLE. */
7453 BINFO_VTABLE (binfo
) = vtbl
;
7458 /* Construct the initializer for BINFO's virtual function table. BINFO
7459 is part of the hierarchy dominated by T. If we're building a
7460 construction vtable, the ORIG_BINFO is the binfo we should use to
7461 find the actual function pointers to put in the vtable - but they
7462 can be overridden on the path to most-derived in the graph that
7463 ORIG_BINFO belongs. Otherwise,
7464 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7465 BINFO that should be indicated by the RTTI information in the
7466 vtable; it will be a base class of T, rather than T itself, if we
7467 are building a construction vtable.
7469 The value returned is a TREE_LIST suitable for wrapping in a
7470 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7471 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7472 number of non-function entries in the vtable.
7474 It might seem that this function should never be called with a
7475 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7476 base is always subsumed by a derived class vtable. However, when
7477 we are building construction vtables, we do build vtables for
7478 primary bases; we need these while the primary base is being
7482 build_vtbl_initializer (binfo
, orig_binfo
, t
, rtti_binfo
, non_fn_entries_p
)
7487 int *non_fn_entries_p
;
7494 /* Initialize VID. */
7495 memset (&vid
, 0, sizeof (vid
));
7498 vid
.rtti_binfo
= rtti_binfo
;
7499 vid
.last_init
= &vid
.inits
;
7500 vid
.primary_vtbl_p
= (binfo
== TYPE_BINFO (t
));
7501 vid
.ctor_vtbl_p
= !same_type_p (BINFO_TYPE (rtti_binfo
), t
);
7502 /* The first vbase or vcall offset is at index -3 in the vtable. */
7503 vid
.index
= ssize_int (-3);
7505 /* Add entries to the vtable for RTTI. */
7506 build_rtti_vtbl_entries (binfo
, &vid
);
7508 /* Create an array for keeping track of the functions we've
7509 processed. When we see multiple functions with the same
7510 signature, we share the vcall offsets. */
7511 VARRAY_TREE_INIT (vid
.fns
, 32, "fns");
7512 /* Add the vcall and vbase offset entries. */
7513 build_vcall_and_vbase_vtbl_entries (binfo
, &vid
);
7514 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7515 build_vbase_offset_vtbl_entries. */
7516 for (vbase
= CLASSTYPE_VBASECLASSES (t
);
7518 vbase
= TREE_CHAIN (vbase
))
7519 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase
));
7521 if (non_fn_entries_p
)
7522 *non_fn_entries_p
= list_length (vid
.inits
);
7524 /* Go through all the ordinary virtual functions, building up
7526 vfun_inits
= NULL_TREE
;
7527 for (v
= BINFO_VIRTUALS (orig_binfo
); v
; v
= TREE_CHAIN (v
))
7533 tree init
= NULL_TREE
;
7537 /* If the only definition of this function signature along our
7538 primary base chain is from a lost primary, this vtable slot will
7539 never be used, so just zero it out. This is important to avoid
7540 requiring extra thunks which cannot be generated with the function.
7542 We first check this in update_vtable_entry_for_fn, so we handle
7543 restored primary bases properly; we also need to do it here so we
7544 zero out unused slots in ctor vtables, rather than filling themff
7545 with erroneous values (though harmless, apart from relocation
7547 for (b
= binfo
; ; b
= get_primary_binfo (b
))
7549 /* We found a defn before a lost primary; go ahead as normal. */
7550 if (look_for_overrides_here (BINFO_TYPE (b
), fn
))
7553 /* The nearest definition is from a lost primary; clear the
7555 if (BINFO_LOST_PRIMARY_P (b
))
7557 init
= size_zero_node
;
7564 /* Pull the offset for `this', and the function to call, out of
7566 delta
= BV_DELTA (v
);
7568 if (BV_USE_VCALL_INDEX_P (v
))
7570 vcall_index
= BV_VCALL_INDEX (v
);
7571 my_friendly_assert (vcall_index
!= NULL_TREE
, 20000621);
7574 vcall_index
= NULL_TREE
;
7576 my_friendly_assert (TREE_CODE (delta
) == INTEGER_CST
, 19990727);
7577 my_friendly_assert (TREE_CODE (fn
) == FUNCTION_DECL
, 19990727);
7579 /* You can't call an abstract virtual function; it's abstract.
7580 So, we replace these functions with __pure_virtual. */
7581 if (DECL_PURE_VIRTUAL_P (fn
))
7584 /* Take the address of the function, considering it to be of an
7585 appropriate generic type. */
7586 pfn
= build1 (ADDR_EXPR
, vfunc_ptr_type_node
, fn
);
7587 /* The address of a function can't change. */
7588 TREE_CONSTANT (pfn
) = 1;
7590 /* Enter it in the vtable. */
7591 init
= build_vtable_entry (delta
, vcall_index
, pfn
);
7594 /* And add it to the chain of initializers. */
7595 if (TARGET_VTABLE_USES_DESCRIPTORS
)
7598 if (init
== size_zero_node
)
7599 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
7600 vfun_inits
= tree_cons (NULL_TREE
, init
, vfun_inits
);
7602 for (i
= 0; i
< TARGET_VTABLE_USES_DESCRIPTORS
; ++i
)
7604 tree fdesc
= build (FDESC_EXPR
, vfunc_ptr_type_node
,
7605 TREE_OPERAND (init
, 0),
7606 build_int_2 (i
, 0));
7607 TREE_CONSTANT (fdesc
) = 1;
7609 vfun_inits
= tree_cons (NULL_TREE
, fdesc
, vfun_inits
);
7613 vfun_inits
= tree_cons (NULL_TREE
, init
, vfun_inits
);
7616 /* The initializers for virtual functions were built up in reverse
7617 order; straighten them out now. */
7618 vfun_inits
= nreverse (vfun_inits
);
7620 /* The negative offset initializers are also in reverse order. */
7621 vid
.inits
= nreverse (vid
.inits
);
7623 /* Chain the two together. */
7624 return chainon (vid
.inits
, vfun_inits
);
7627 /* Adds to vid->inits the initializers for the vbase and vcall
7628 offsets in BINFO, which is in the hierarchy dominated by T. */
7631 build_vcall_and_vbase_vtbl_entries (binfo
, vid
)
7633 vtbl_init_data
*vid
;
7637 /* If this is a derived class, we must first create entries
7638 corresponding to the primary base class. */
7639 b
= get_primary_binfo (binfo
);
7641 build_vcall_and_vbase_vtbl_entries (b
, vid
);
7643 /* Add the vbase entries for this base. */
7644 build_vbase_offset_vtbl_entries (binfo
, vid
);
7645 /* Add the vcall entries for this base. */
7646 build_vcall_offset_vtbl_entries (binfo
, vid
);
7649 /* Returns the initializers for the vbase offset entries in the vtable
7650 for BINFO (which is part of the class hierarchy dominated by T), in
7651 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7652 where the next vbase offset will go. */
7655 build_vbase_offset_vtbl_entries (binfo
, vid
)
7657 vtbl_init_data
*vid
;
7661 tree non_primary_binfo
;
7663 /* If there are no virtual baseclasses, then there is nothing to
7665 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo
)))
7670 /* We might be a primary base class. Go up the inheritance hierarchy
7671 until we find the most derived class of which we are a primary base:
7672 it is the offset of that which we need to use. */
7673 non_primary_binfo
= binfo
;
7674 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
7678 /* If we have reached a virtual base, then it must be a primary
7679 base (possibly multi-level) of vid->binfo, or we wouldn't
7680 have called build_vcall_and_vbase_vtbl_entries for it. But it
7681 might be a lost primary, so just skip down to vid->binfo. */
7682 if (TREE_VIA_VIRTUAL (non_primary_binfo
))
7684 non_primary_binfo
= vid
->binfo
;
7688 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
7689 if (get_primary_binfo (b
) != non_primary_binfo
)
7691 non_primary_binfo
= b
;
7694 /* Go through the virtual bases, adding the offsets. */
7695 for (vbase
= TYPE_BINFO (BINFO_TYPE (binfo
));
7697 vbase
= TREE_CHAIN (vbase
))
7702 if (!TREE_VIA_VIRTUAL (vbase
))
7705 /* Find the instance of this virtual base in the complete
7707 b
= binfo_for_vbase (BINFO_TYPE (vbase
), t
);
7709 /* If we've already got an offset for this virtual base, we
7710 don't need another one. */
7711 if (BINFO_VTABLE_PATH_MARKED (b
))
7713 SET_BINFO_VTABLE_PATH_MARKED (b
);
7715 /* Figure out where we can find this vbase offset. */
7716 delta
= size_binop (MULT_EXPR
,
7719 TYPE_SIZE_UNIT (vtable_entry_type
)));
7720 if (vid
->primary_vtbl_p
)
7721 BINFO_VPTR_FIELD (b
) = delta
;
7723 if (binfo
!= TYPE_BINFO (t
))
7727 /* Find the instance of this virtual base in the type of BINFO. */
7728 orig_vbase
= binfo_for_vbase (BINFO_TYPE (vbase
),
7729 BINFO_TYPE (binfo
));
7731 /* The vbase offset had better be the same. */
7732 if (!tree_int_cst_equal (delta
,
7733 BINFO_VPTR_FIELD (orig_vbase
)))
7737 /* The next vbase will come at a more negative offset. */
7738 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
, ssize_int (1));
7740 /* The initializer is the delta from BINFO to this virtual base.
7741 The vbase offsets go in reverse inheritance-graph order, and
7742 we are walking in inheritance graph order so these end up in
7744 delta
= size_diffop (BINFO_OFFSET (b
), BINFO_OFFSET (non_primary_binfo
));
7747 = build_tree_list (NULL_TREE
,
7748 fold (build1 (NOP_EXPR
,
7751 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7755 /* Adds the initializers for the vcall offset entries in the vtable
7756 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7760 build_vcall_offset_vtbl_entries (binfo
, vid
)
7762 vtbl_init_data
*vid
;
7764 /* We only need these entries if this base is a virtual base. */
7765 if (!TREE_VIA_VIRTUAL (binfo
))
7768 /* We need a vcall offset for each of the virtual functions in this
7769 vtable. For example:
7771 class A { virtual void f (); };
7772 class B1 : virtual public A { virtual void f (); };
7773 class B2 : virtual public A { virtual void f (); };
7774 class C: public B1, public B2 { virtual void f (); };
7776 A C object has a primary base of B1, which has a primary base of A. A
7777 C also has a secondary base of B2, which no longer has a primary base
7778 of A. So the B2-in-C construction vtable needs a secondary vtable for
7779 A, which will adjust the A* to a B2* to call f. We have no way of
7780 knowing what (or even whether) this offset will be when we define B2,
7781 so we store this "vcall offset" in the A sub-vtable and look it up in
7782 a "virtual thunk" for B2::f.
7784 We need entries for all the functions in our primary vtable and
7785 in our non-virtual bases' secondary vtables. */
7787 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7788 add_vcall_offset_vtbl_entries_r (binfo
, vid
);
7791 /* Build vcall offsets, starting with those for BINFO. */
7794 add_vcall_offset_vtbl_entries_r (binfo
, vid
)
7796 vtbl_init_data
*vid
;
7801 /* Don't walk into virtual bases -- except, of course, for the
7802 virtual base for which we are building vcall offsets. Any
7803 primary virtual base will have already had its offsets generated
7804 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7805 if (TREE_VIA_VIRTUAL (binfo
) && vid
->vbase
!= binfo
)
7808 /* If BINFO has a primary base, process it first. */
7809 primary_binfo
= get_primary_binfo (binfo
);
7811 add_vcall_offset_vtbl_entries_r (primary_binfo
, vid
);
7813 /* Add BINFO itself to the list. */
7814 add_vcall_offset_vtbl_entries_1 (binfo
, vid
);
7816 /* Scan the non-primary bases of BINFO. */
7817 for (i
= 0; i
< BINFO_N_BASETYPES (binfo
); ++i
)
7821 base_binfo
= BINFO_BASETYPE (binfo
, i
);
7822 if (base_binfo
!= primary_binfo
)
7823 add_vcall_offset_vtbl_entries_r (base_binfo
, vid
);
7827 /* Called from build_vcall_offset_vtbl_entries_r. */
7830 add_vcall_offset_vtbl_entries_1 (binfo
, vid
)
7832 vtbl_init_data
* vid
;
7834 tree derived_virtuals
;
7838 /* If BINFO is a primary base, the most derived class which has BINFO as
7839 a primary base; otherwise, just BINFO. */
7840 tree non_primary_binfo
;
7842 binfo_inits
= NULL_TREE
;
7844 /* We might be a primary base class. Go up the inheritance hierarchy
7845 until we find the most derived class of which we are a primary base:
7846 it is the BINFO_VIRTUALS there that we need to consider. */
7847 non_primary_binfo
= binfo
;
7848 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo
))
7852 /* If we have reached a virtual base, then it must be vid->vbase,
7853 because we ignore other virtual bases in
7854 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7855 base (possibly multi-level) of vid->binfo, or we wouldn't
7856 have called build_vcall_and_vbase_vtbl_entries for it. But it
7857 might be a lost primary, so just skip down to vid->binfo. */
7858 if (TREE_VIA_VIRTUAL (non_primary_binfo
))
7860 if (non_primary_binfo
!= vid
->vbase
)
7862 non_primary_binfo
= vid
->binfo
;
7866 b
= BINFO_INHERITANCE_CHAIN (non_primary_binfo
);
7867 if (get_primary_binfo (b
) != non_primary_binfo
)
7869 non_primary_binfo
= b
;
7872 if (vid
->ctor_vtbl_p
)
7873 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7874 where rtti_binfo is the most derived type. */
7875 non_primary_binfo
= get_original_base
7876 (non_primary_binfo
, TYPE_BINFO (BINFO_TYPE (vid
->rtti_binfo
)));
7878 /* Make entries for the rest of the virtuals. */
7879 for (base_virtuals
= BINFO_VIRTUALS (binfo
),
7880 derived_virtuals
= BINFO_VIRTUALS (non_primary_binfo
),
7881 orig_virtuals
= BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo
)));
7883 base_virtuals
= TREE_CHAIN (base_virtuals
),
7884 derived_virtuals
= TREE_CHAIN (derived_virtuals
),
7885 orig_virtuals
= TREE_CHAIN (orig_virtuals
))
7894 /* Find the declaration that originally caused this function to
7895 be present in BINFO_TYPE (binfo). */
7896 orig_fn
= BV_FN (orig_virtuals
);
7898 /* When processing BINFO, we only want to generate vcall slots for
7899 function slots introduced in BINFO. So don't try to generate
7900 one if the function isn't even defined in BINFO. */
7901 if (!same_type_p (DECL_CONTEXT (orig_fn
), BINFO_TYPE (binfo
)))
7904 /* Find the overriding function. */
7905 fn
= BV_FN (derived_virtuals
);
7907 /* If there is already an entry for a function with the same
7908 signature as FN, then we do not need a second vcall offset.
7909 Check the list of functions already present in the derived
7911 for (i
= 0; i
< VARRAY_ACTIVE_SIZE (vid
->fns
); ++i
)
7915 derived_entry
= VARRAY_TREE (vid
->fns
, i
);
7916 if (same_signature_p (BV_FN (derived_entry
), fn
)
7917 /* We only use one vcall offset for virtual destructors,
7918 even though there are two virtual table entries. */
7919 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry
))
7920 && DECL_DESTRUCTOR_P (fn
)))
7922 if (!vid
->ctor_vtbl_p
)
7923 BV_VCALL_INDEX (derived_virtuals
)
7924 = BV_VCALL_INDEX (derived_entry
);
7928 if (i
!= VARRAY_ACTIVE_SIZE (vid
->fns
))
7931 /* The FN comes from BASE. So, we must calculate the adjustment from
7932 vid->vbase to BASE. We can just look for BASE in the complete
7933 object because we are converting from a virtual base, so if there
7934 were multiple copies, there would not be a unique final overrider
7935 and vid->derived would be ill-formed. */
7936 base
= DECL_CONTEXT (fn
);
7937 base_binfo
= lookup_base (vid
->derived
, base
, ba_any
, NULL
);
7939 /* Compute the vcall offset. */
7940 /* As mentioned above, the vbase we're working on is a primary base of
7941 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7942 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7943 vcall_offset
= BINFO_OFFSET (vid
->binfo
);
7944 vcall_offset
= size_diffop (BINFO_OFFSET (base_binfo
),
7946 vcall_offset
= fold (build1 (NOP_EXPR
, vtable_entry_type
,
7949 *vid
->last_init
= build_tree_list (NULL_TREE
, vcall_offset
);
7950 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
7952 /* Keep track of the vtable index where this vcall offset can be
7953 found. For a construction vtable, we already made this
7954 annotation when we built the original vtable. */
7955 if (!vid
->ctor_vtbl_p
)
7956 BV_VCALL_INDEX (derived_virtuals
) = vid
->index
;
7958 /* The next vcall offset will be found at a more negative
7960 vid
->index
= size_binop (MINUS_EXPR
, vid
->index
, ssize_int (1));
7962 /* Keep track of this function. */
7963 VARRAY_PUSH_TREE (vid
->fns
, derived_virtuals
);
7967 /* Return vtbl initializers for the RTTI entries coresponding to the
7968 BINFO's vtable. The RTTI entries should indicate the object given
7969 by VID->rtti_binfo. */
7972 build_rtti_vtbl_entries (binfo
, vid
)
7974 vtbl_init_data
*vid
;
7983 basetype
= BINFO_TYPE (binfo
);
7984 t
= BINFO_TYPE (vid
->rtti_binfo
);
7986 /* To find the complete object, we will first convert to our most
7987 primary base, and then add the offset in the vtbl to that value. */
7989 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b
))
7990 && !BINFO_LOST_PRIMARY_P (b
))
7994 primary_base
= get_primary_binfo (b
);
7995 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base
) == b
, 20010127);
7998 offset
= size_diffop (BINFO_OFFSET (vid
->rtti_binfo
), BINFO_OFFSET (b
));
8000 /* The second entry is the address of the typeinfo object. */
8002 decl
= build_unary_op (ADDR_EXPR
, get_tinfo_decl (t
), 0);
8004 decl
= integer_zero_node
;
8006 /* Convert the declaration to a type that can be stored in the
8008 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, decl
);
8009 TREE_CONSTANT (init
) = 1;
8010 *vid
->last_init
= build_tree_list (NULL_TREE
, init
);
8011 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
8013 /* Add the offset-to-top entry. It comes earlier in the vtable that
8014 the the typeinfo entry. Convert the offset to look like a
8015 function pointer, so that we can put it in the vtable. */
8016 init
= build1 (NOP_EXPR
, vfunc_ptr_type_node
, offset
);
8017 TREE_CONSTANT (init
) = 1;
8018 *vid
->last_init
= build_tree_list (NULL_TREE
, init
);
8019 vid
->last_init
= &TREE_CHAIN (*vid
->last_init
);
8022 /* Build an entry in the virtual function table. DELTA is the offset
8023 for the `this' pointer. VCALL_INDEX is the vtable index containing
8024 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8025 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8026 but it may not actually be a virtual function table pointer. (For
8027 example, it might be the address of the RTTI object, under the new
8031 build_vtable_entry (delta
, vcall_index
, entry
)
8036 tree fn
= TREE_OPERAND (entry
, 0);
8038 if ((!integer_zerop (delta
) || vcall_index
!= NULL_TREE
)
8039 && fn
!= abort_fndecl
)
8041 entry
= make_thunk (entry
, delta
, vcall_index
);
8042 entry
= build1 (ADDR_EXPR
, vtable_entry_type
, entry
);
8043 TREE_READONLY (entry
) = 1;
8044 TREE_CONSTANT (entry
) = 1;
8046 #ifdef GATHER_STATISTICS
8047 n_vtable_entries
+= 1;