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. */
38 /* The number of nested classes being processed. If we are not in the
39 scope of any class, this is zero. */
41 int current_class_depth;
43 /* In order to deal with nested classes, we keep a stack of classes.
44 The topmost entry is the innermost class, and is the entry at index
45 CURRENT_CLASS_DEPTH */
47 typedef struct class_stack_node {
48 /* The name of the class. */
51 /* The _TYPE node for the class. */
54 /* The access specifier pending for new declarations in the scope of
58 /* If were defining TYPE, the names used in this class. */
59 splay_tree names_used;
60 }* class_stack_node_t;
62 typedef struct vtbl_init_data_s
64 /* The base for which we're building initializers. */
66 /* The type of the most-derived type. */
68 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
69 unless ctor_vtbl_p is true. */
71 /* The negative-index vtable initializers built up so far. These
72 are in order from least negative index to most negative index. */
74 /* The last (i.e., most negative) entry in INITS. */
76 /* The binfo for the virtual base for which we're building
77 vcall offset initializers. */
79 /* The functions in vbase for which we have already provided vcall
82 /* The vtable index of the next vcall or vbase offset. */
84 /* Nonzero if we are building the initializer for the primary
87 /* Nonzero if we are building the initializer for a construction
92 /* The type of a function passed to walk_subobject_offsets. */
93 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
95 /* The stack itself. This is an dynamically resized array. The
96 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
97 static int current_class_stack_size;
98 static class_stack_node_t current_class_stack;
100 /* An array of all local classes present in this translation unit, in
101 declaration order. */
102 varray_type local_classes;
104 static tree get_vfield_name PARAMS ((tree));
105 static void finish_struct_anon PARAMS ((tree));
106 static tree build_vtable_entry PARAMS ((tree, tree, tree));
107 static tree get_vtable_name PARAMS ((tree));
108 static tree get_basefndecls PARAMS ((tree, tree));
109 static int build_primary_vtable PARAMS ((tree, tree));
110 static int build_secondary_vtable PARAMS ((tree, tree));
111 static void finish_vtbls PARAMS ((tree));
112 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
113 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
114 static void delete_duplicate_fields PARAMS ((tree));
115 static void finish_struct_bits PARAMS ((tree));
116 static int alter_access PARAMS ((tree, tree, tree));
117 static void handle_using_decl PARAMS ((tree, tree));
118 static int strictly_overrides PARAMS ((tree, tree));
119 static void check_for_override PARAMS ((tree, tree));
120 static tree dfs_modify_vtables PARAMS ((tree, void *));
121 static tree modify_all_vtables PARAMS ((tree, int *, tree));
122 static void determine_primary_base PARAMS ((tree, int *));
123 static void finish_struct_methods PARAMS ((tree));
124 static void maybe_warn_about_overly_private_class PARAMS ((tree));
125 static int field_decl_cmp PARAMS ((const tree *, const tree *));
126 static int method_name_cmp PARAMS ((const tree *, const tree *));
127 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
128 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
129 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
131 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
132 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
133 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
134 static int count_fields PARAMS ((tree));
135 static int add_fields_to_vec PARAMS ((tree, tree, int));
136 static void check_bitfield_decl PARAMS ((tree));
137 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
138 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
140 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
142 static bool build_base_fields PARAMS ((record_layout_info, int *,
144 static void check_methods PARAMS ((tree));
145 static void remove_zero_width_bit_fields PARAMS ((tree));
146 static void check_bases PARAMS ((tree, int *, int *, int *));
147 static void check_bases_and_members PARAMS ((tree, int *));
148 static tree create_vtable_ptr PARAMS ((tree, int *, tree *));
149 static void layout_class_type PARAMS ((tree, int *, int *, tree *));
150 static void fixup_pending_inline PARAMS ((tree));
151 static void fixup_inline_methods PARAMS ((tree));
152 static void set_primary_base PARAMS ((tree, tree, int *));
153 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
154 static void layout_virtual_bases PARAMS ((tree, splay_tree));
155 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
156 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
157 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
158 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
159 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
160 static void layout_vtable_decl PARAMS ((tree, int));
161 static tree dfs_find_final_overrider PARAMS ((tree, void *));
162 static tree find_final_overrider PARAMS ((tree, tree, tree));
163 static int make_new_vtable PARAMS ((tree, tree));
164 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
165 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
166 static void dump_class_hierarchy PARAMS ((tree));
167 static void dump_array PARAMS ((FILE *, tree));
168 static void dump_vtable PARAMS ((tree, tree, tree));
169 static void dump_vtt PARAMS ((tree, tree));
170 static tree build_vtable PARAMS ((tree, tree, tree));
171 static void initialize_vtable PARAMS ((tree, tree));
172 static void initialize_array PARAMS ((tree, tree));
173 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
176 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
177 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
178 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
179 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
181 static void set_vindex PARAMS ((tree, int *));
182 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
183 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
185 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
186 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
187 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
188 static void mark_primary_bases PARAMS ((tree));
189 static tree mark_primary_virtual_base PARAMS ((tree, tree));
190 static void clone_constructors_and_destructors PARAMS ((tree));
191 static tree build_clone PARAMS ((tree, tree));
192 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
193 static tree copy_virtuals PARAMS ((tree));
194 static void build_ctor_vtbl_group PARAMS ((tree, tree));
195 static void build_vtt PARAMS ((tree));
196 static tree binfo_ctor_vtable PARAMS ((tree));
197 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
198 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
199 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
200 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
201 static tree get_original_base PARAMS ((tree, tree));
202 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
203 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
204 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
205 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
206 tree, splay_tree, tree, int));
207 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
208 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
209 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
211 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
212 static bool type_requires_array_cookie PARAMS ((tree));
214 /* Macros for dfs walking during vtt construction. See
215 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
216 and dfs_fixup_binfo_vtbls. */
217 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
218 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
220 /* Variables shared between class.c and call.c. */
222 #ifdef GATHER_STATISTICS
224 int n_vtable_entries = 0;
225 int n_vtable_searches = 0;
226 int n_vtable_elems = 0;
227 int n_convert_harshness = 0;
228 int n_compute_conversion_costs = 0;
229 int n_build_method_call = 0;
230 int n_inner_fields_searched = 0;
233 /* Convert to or from a base subobject. EXPR is an expression of type
234 `A' or `A*', an expression of type `B' or `B*' is returned. To
235 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
236 the B base instance within A. To convert base A to derived B, CODE
237 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
238 In this latter case, A must not be a morally virtual base of B.
239 NONNULL is true if EXPR is known to be non-NULL (this is only
240 needed when EXPR is of pointer type). CV qualifiers are preserved
244 build_base_path (code, expr, binfo, nonnull)
250 tree v_binfo = NULL_TREE;
251 tree d_binfo = NULL_TREE;
255 tree null_test = NULL;
256 tree ptr_target_type;
258 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
260 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
261 return error_mark_node;
263 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
266 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 my_friendly_assert (code == MINUS_EXPR
275 ? same_type_p (BINFO_TYPE (binfo), probe)
277 ? same_type_p (BINFO_TYPE (d_binfo), probe)
280 if (code == MINUS_EXPR && v_binfo)
282 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
283 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
284 return error_mark_node;
287 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
288 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
289 expr = save_expr (expr);
292 expr = build_unary_op (ADDR_EXPR, expr, 0);
294 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
296 offset = BINFO_OFFSET (binfo);
298 if (v_binfo && fixed_type_p <= 0)
300 /* Going via virtual base V_BINFO. We need the static offset
301 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
302 V_BINFO. That offset is an entry in D_BINFO's vtable. */
303 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
304 TREE_TYPE (TREE_TYPE (expr)));
306 v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
308 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
309 v_offset, BINFO_VPTR_FIELD (v_binfo));
310 v_offset = build1 (NOP_EXPR,
311 build_pointer_type (ptrdiff_type_node),
313 v_offset = build_indirect_ref (v_offset, NULL);
314 TREE_CONSTANT (v_offset) = 1;
316 offset = cp_convert (ptrdiff_type_node,
317 size_diffop (offset, BINFO_OFFSET (v_binfo)));
319 if (!integer_zerop (offset))
320 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
322 if (fixed_type_p < 0)
323 /* Negative fixed_type_p means this is a constructor or destructor;
324 virtual base layout is fixed in in-charge [cd]tors, but not in
326 offset = build (COND_EXPR, ptrdiff_type_node,
327 build (EQ_EXPR, boolean_type_node,
328 current_in_charge_parm, integer_zero_node),
330 BINFO_OFFSET (binfo));
335 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
337 target_type = cp_build_qualified_type
338 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
339 ptr_target_type = build_pointer_type (target_type);
341 target_type = ptr_target_type;
343 expr = build1 (NOP_EXPR, ptr_target_type, expr);
345 if (!integer_zerop (offset))
346 expr = build (code, ptr_target_type, expr, offset);
351 expr = build_indirect_ref (expr, NULL);
354 expr = build (COND_EXPR, target_type, null_test,
355 build1 (NOP_EXPR, target_type, integer_zero_node),
361 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
362 message is emitted if TYPE is inaccessible. OBJECT is assumed to
366 convert_to_base (tree object, tree type, bool check_access)
370 binfo = lookup_base (TREE_TYPE (object), type,
371 check_access ? ba_check : ba_ignore,
373 if (!binfo || binfo == error_mark_node)
374 return error_mark_node;
376 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
380 /* Virtual function things. */
383 build_vtable_entry_ref (array_ref, instance, idx)
384 tree array_ref, instance, idx;
386 tree i, i2, vtable, first_fn, basetype;
388 basetype = TREE_TYPE (instance);
389 if (TREE_CODE (basetype) == REFERENCE_TYPE)
390 basetype = TREE_TYPE (basetype);
392 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
393 first_fn = TYPE_BINFO_VTABLE (basetype);
395 i = fold (build_array_ref (first_fn, idx));
396 i = fold (build_c_cast (ptrdiff_type_node,
397 build_unary_op (ADDR_EXPR, i, 0)));
398 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
399 i2 = fold (build_c_cast (ptrdiff_type_node,
400 build_unary_op (ADDR_EXPR, i2, 0)));
401 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
403 if (TREE_CODE (i) != INTEGER_CST)
406 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
409 /* Given an object INSTANCE, return an expression which yields the
410 vtable element corresponding to INDEX. There are many special
411 cases for INSTANCE which we take care of here, mainly to avoid
412 creating extra tree nodes when we don't have to. */
415 build_vtbl_ref_1 (instance, idx)
419 tree vtbl = NULL_TREE;
421 /* Try to figure out what a reference refers to, and
422 access its virtual function table directly. */
425 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
427 tree basetype = TREE_TYPE (instance);
428 if (TREE_CODE (basetype) == REFERENCE_TYPE)
429 basetype = TREE_TYPE (basetype);
431 if (fixed_type && !cdtorp)
433 tree binfo = lookup_base (fixed_type, basetype,
434 ba_ignore|ba_quiet, NULL);
436 vtbl = BINFO_VTABLE (binfo);
441 vtbl = build_vfield_ref (instance, basetype);
444 assemble_external (vtbl);
446 aref = build_array_ref (vtbl, idx);
447 TREE_CONSTANT (aref) = 1;
453 build_vtbl_ref (instance, idx)
456 tree aref = build_vtbl_ref_1 (instance, idx);
459 aref = build_vtable_entry_ref (aref, instance, idx);
464 /* Given an object INSTANCE, return an expression which yields a
465 function pointer corresponding to vtable element INDEX. */
468 build_vfn_ref (instance, idx)
471 tree aref = build_vtbl_ref_1 (instance, idx);
473 /* When using function descriptors, the address of the
474 vtable entry is treated as a function pointer. */
475 if (TARGET_VTABLE_USES_DESCRIPTORS)
476 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
477 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
480 aref = build_vtable_entry_ref (aref, instance, idx);
485 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
486 for the given TYPE. */
489 get_vtable_name (type)
492 return mangle_vtbl_for_type (type);
495 /* Return an IDENTIFIER_NODE for the name of the virtual table table
502 return mangle_vtt_for_type (type);
505 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
506 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
507 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
510 build_vtable (class_type, name, vtable_type)
517 decl = build_lang_decl (VAR_DECL, name, vtable_type);
518 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
519 now to avoid confusion in mangle_decl. */
520 SET_DECL_ASSEMBLER_NAME (decl, name);
521 DECL_CONTEXT (decl) = class_type;
522 DECL_ARTIFICIAL (decl) = 1;
523 TREE_STATIC (decl) = 1;
524 TREE_READONLY (decl) = 1;
525 DECL_VIRTUAL_P (decl) = 1;
526 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
528 import_export_vtable (decl, class_type, 0);
533 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
534 or even complete. If this does not exist, create it. If COMPLETE is
535 nonzero, then complete the definition of it -- that will render it
536 impossible to actually build the vtable, but is useful to get at those
537 which are known to exist in the runtime. */
540 get_vtable_decl (type, complete)
544 tree name = get_vtable_name (type);
545 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
549 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
550 && DECL_VIRTUAL_P (decl), 20000118);
554 decl = build_vtable (type, name, void_type_node);
555 decl = pushdecl_top_level (decl);
556 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
559 /* At one time the vtable info was grabbed 2 words at a time. This
560 fails on sparc unless you have 8-byte alignment. (tiemann) */
561 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
566 DECL_EXTERNAL (decl) = 1;
567 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
573 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
574 BV_VCALL_INDEX for each entry is cleared. */
577 copy_virtuals (binfo)
583 copies = copy_list (BINFO_VIRTUALS (binfo));
584 for (t = copies; t; t = TREE_CHAIN (t))
586 BV_VCALL_INDEX (t) = NULL_TREE;
587 BV_USE_VCALL_INDEX_P (t) = 0;
593 /* Build the primary virtual function table for TYPE. If BINFO is
594 non-NULL, build the vtable starting with the initial approximation
595 that it is the same as the one which is the head of the association
596 list. Returns a nonzero value if a new vtable is actually
600 build_primary_vtable (binfo, type)
606 decl = get_vtable_decl (type, /*complete=*/0);
610 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
611 /* We have already created a vtable for this base, so there's
612 no need to do it again. */
615 virtuals = copy_virtuals (binfo);
616 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
617 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
618 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
622 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
624 virtuals = NULL_TREE;
627 #ifdef GATHER_STATISTICS
629 n_vtable_elems += list_length (virtuals);
632 /* Initialize the association list for this type, based
633 on our first approximation. */
634 TYPE_BINFO_VTABLE (type) = decl;
635 TYPE_BINFO_VIRTUALS (type) = virtuals;
636 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
640 /* Give BINFO a new virtual function table which is initialized
641 with a skeleton-copy of its original initialization. The only
642 entry that changes is the `delta' entry, so we can really
643 share a lot of structure.
645 FOR_TYPE is the most derived type which caused this table to
648 Returns nonzero if we haven't met BINFO before.
650 The order in which vtables are built (by calling this function) for
651 an object must remain the same, otherwise a binary incompatibility
655 build_secondary_vtable (binfo, for_type)
656 tree binfo, for_type;
658 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
660 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
661 /* We already created a vtable for this base. There's no need to
665 /* Remember that we've created a vtable for this BINFO, so that we
666 don't try to do so again. */
667 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
669 /* Make fresh virtual list, so we can smash it later. */
670 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
672 /* Secondary vtables are laid out as part of the same structure as
673 the primary vtable. */
674 BINFO_VTABLE (binfo) = NULL_TREE;
678 /* Create a new vtable for BINFO which is the hierarchy dominated by
679 T. Return nonzero if we actually created a new vtable. */
682 make_new_vtable (t, binfo)
686 if (binfo == TYPE_BINFO (t))
687 /* In this case, it is *type*'s vtable we are modifying. We start
688 with the approximation that its vtable is that of the
689 immediate base class. */
690 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
691 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
692 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
695 /* This is our very own copy of `basetype' to play with. Later,
696 we will fill in all the virtual functions that override the
697 virtual functions in these base classes which are not defined
698 by the current type. */
699 return build_secondary_vtable (binfo, t);
702 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
703 (which is in the hierarchy dominated by T) list FNDECL as its
704 BV_FN. DELTA is the required constant adjustment from the `this'
705 pointer where the vtable entry appears to the `this' required when
706 the function is actually called. */
709 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
720 if (fndecl != BV_FN (v)
721 || !tree_int_cst_equal (delta, BV_DELTA (v)))
725 /* We need a new vtable for BINFO. */
726 if (make_new_vtable (t, binfo))
728 /* If we really did make a new vtable, we also made a copy
729 of the BINFO_VIRTUALS list. Now, we have to find the
730 corresponding entry in that list. */
731 *virtuals = BINFO_VIRTUALS (binfo);
732 while (BV_FN (*virtuals) != BV_FN (v))
733 *virtuals = TREE_CHAIN (*virtuals);
737 base_fndecl = BV_FN (v);
738 BV_DELTA (v) = delta;
739 BV_VCALL_INDEX (v) = NULL_TREE;
742 /* Now assign virtual dispatch information, if unset. We can
743 dispatch this through any overridden base function.
745 FIXME this can choose a secondary vtable if the primary is not
746 also lexically first, leading to useless conversions.
747 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
748 ever be different from DECL_CONTEXT. */
749 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
751 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
752 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
757 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
758 functions present in the vtable so far. */
761 set_vindex (decl, vfuns_p)
768 *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
769 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
770 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
773 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
774 the method after the class has already been defined because a
775 declaration for it was seen. (Even though that is erroneous, we
776 add the method for improved error recovery.) */
779 add_method (type, method, error_p)
784 int using = (DECL_CONTEXT (method) != type);
788 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
789 && DECL_TEMPLATE_CONV_FN_P (method));
791 if (!CLASSTYPE_METHOD_VEC (type))
792 /* Make a new method vector. We start with 8 entries. We must
793 allocate at least two (for constructors and destructors), and
794 we're going to end up with an assignment operator at some point
797 We could use a TREE_LIST for now, and convert it to a TREE_VEC
798 in finish_struct, but we would probably waste more memory
799 making the links in the list than we would by over-allocating
800 the size of the vector here. Furthermore, we would complicate
801 all the code that expects this to be a vector. */
802 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
804 method_vec = CLASSTYPE_METHOD_VEC (type);
805 len = TREE_VEC_LENGTH (method_vec);
807 /* Constructors and destructors go in special slots. */
808 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
809 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
810 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
811 slot = CLASSTYPE_DESTRUCTOR_SLOT;
814 int have_template_convs_p = 0;
816 /* See if we already have an entry with this name. */
817 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
819 tree m = TREE_VEC_ELT (method_vec, slot);
827 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
828 && DECL_TEMPLATE_CONV_FN_P (m));
830 /* If we need to move things up, see if there's
832 if (!have_template_convs_p)
835 if (TREE_VEC_ELT (method_vec, slot))
840 if (DECL_NAME (m) == DECL_NAME (method))
846 /* We need a bigger method vector. */
850 /* In the non-error case, we are processing a class
851 definition. Double the size of the vector to give room
855 /* In the error case, the vector is already complete. We
856 don't expect many errors, and the rest of the front-end
857 will get confused if there are empty slots in the vector. */
861 new_vec = make_tree_vec (new_len);
862 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
863 len * sizeof (tree));
865 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
868 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
870 /* Type conversion operators have to come before ordinary
871 methods; add_conversions depends on this to speed up
872 looking for conversion operators. So, if necessary, we
873 slide some of the vector elements up. In theory, this
874 makes this algorithm O(N^2) but we don't expect many
875 conversion operators. */
877 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
879 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
881 tree fn = TREE_VEC_ELT (method_vec, slot);
884 /* There are no more entries in the vector, so we
885 can insert the new conversion operator here. */
888 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
889 /* We can insert the new function right at the
894 if (template_conv_p && have_template_convs_p)
896 else if (!TREE_VEC_ELT (method_vec, slot))
897 /* There is nothing in the Ith slot, so we can avoid
902 /* We know the last slot in the vector is empty
903 because we know that at this point there's room
904 for a new function. */
905 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
906 &TREE_VEC_ELT (method_vec, slot),
907 (len - slot - 1) * sizeof (tree));
908 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
913 if (template_class_depth (type))
914 /* TYPE is a template class. Don't issue any errors now; wait
915 until instantiation time to complain. */
921 /* Check to see if we've already got this method. */
922 for (fns = TREE_VEC_ELT (method_vec, slot);
924 fns = OVL_NEXT (fns))
926 tree fn = OVL_CURRENT (fns);
931 if (TREE_CODE (fn) != TREE_CODE (method))
934 /* [over.load] Member function declarations with the
935 same name and the same parameter types cannot be
936 overloaded if any of them is a static member
937 function declaration.
939 [namespace.udecl] When a using-declaration brings names
940 from a base class into a derived class scope, member
941 functions in the derived class override and/or hide member
942 functions with the same name and parameter types in a base
943 class (rather than conflicting). */
944 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
945 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
947 /* Compare the quals on the 'this' parm. Don't compare
948 the whole types, as used functions are treated as
949 coming from the using class in overload resolution. */
950 if (! DECL_STATIC_FUNCTION_P (fn)
951 && ! DECL_STATIC_FUNCTION_P (method)
952 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
953 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
955 if (! DECL_STATIC_FUNCTION_P (fn))
956 parms1 = TREE_CHAIN (parms1);
957 if (! DECL_STATIC_FUNCTION_P (method))
958 parms2 = TREE_CHAIN (parms2);
960 if (same && compparms (parms1, parms2)
961 && (!DECL_CONV_FN_P (fn)
962 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
963 TREE_TYPE (TREE_TYPE (method)))))
965 if (using && DECL_CONTEXT (fn) == type)
966 /* Defer to the local function. */
970 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
973 /* We don't call duplicate_decls here to merge
974 the declarations because that will confuse
975 things if the methods have inline
976 definitions. In particular, we will crash
977 while processing the definitions. */
984 /* Actually insert the new method. */
985 TREE_VEC_ELT (method_vec, slot)
986 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
988 /* Add the new binding. */
989 if (!DECL_CONSTRUCTOR_P (method)
990 && !DECL_DESTRUCTOR_P (method))
991 push_class_level_binding (DECL_NAME (method),
992 TREE_VEC_ELT (method_vec, slot));
995 /* Subroutines of finish_struct. */
997 /* Look through the list of fields for this struct, deleting
998 duplicates as we go. This must be recursive to handle
1001 FIELD is the field which may not appear anywhere in FIELDS.
1002 FIELD_PTR, if non-null, is the starting point at which
1003 chained deletions may take place.
1004 The value returned is the first acceptable entry found
1007 Note that anonymous fields which are not of UNION_TYPE are
1008 not duplicates, they are just anonymous fields. This happens
1009 when we have unnamed bitfields, for example. */
1012 delete_duplicate_fields_1 (field, fields)
1017 if (DECL_NAME (field) == 0)
1019 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1022 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1023 fields = delete_duplicate_fields_1 (x, fields);
1028 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1030 if (DECL_NAME (x) == 0)
1032 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1034 TYPE_FIELDS (TREE_TYPE (x))
1035 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1036 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1039 fields = TREE_CHAIN (fields);
1041 TREE_CHAIN (prev) = TREE_CHAIN (x);
1044 else if (TREE_CODE (field) == USING_DECL)
1045 /* A using declaration is allowed to appear more than
1046 once. We'll prune these from the field list later, and
1047 handle_using_decl will complain about invalid multiple
1050 else if (DECL_NAME (field) == DECL_NAME (x))
1052 if (TREE_CODE (field) == CONST_DECL
1053 && TREE_CODE (x) == CONST_DECL)
1054 cp_error_at ("duplicate enum value `%D'", x);
1055 else if (TREE_CODE (field) == CONST_DECL
1056 || TREE_CODE (x) == CONST_DECL)
1057 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1059 else if (DECL_DECLARES_TYPE_P (field)
1060 && DECL_DECLARES_TYPE_P (x))
1062 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1064 cp_error_at ("duplicate nested type `%D'", x);
1066 else if (DECL_DECLARES_TYPE_P (field)
1067 || DECL_DECLARES_TYPE_P (x))
1069 /* Hide tag decls. */
1070 if ((TREE_CODE (field) == TYPE_DECL
1071 && DECL_ARTIFICIAL (field))
1072 || (TREE_CODE (x) == TYPE_DECL
1073 && DECL_ARTIFICIAL (x)))
1075 cp_error_at ("duplicate field `%D' (as type and non-type)",
1079 cp_error_at ("duplicate member `%D'", x);
1081 fields = TREE_CHAIN (fields);
1083 TREE_CHAIN (prev) = TREE_CHAIN (x);
1091 delete_duplicate_fields (fields)
1095 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1096 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1099 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1100 legit, otherwise return 0. */
1103 alter_access (t, fdecl, access)
1110 if (!DECL_LANG_SPECIFIC (fdecl))
1111 retrofit_lang_decl (fdecl);
1113 if (DECL_DISCRIMINATOR_P (fdecl))
1116 elem = purpose_member (t, DECL_ACCESS (fdecl));
1119 if (TREE_VALUE (elem) != access)
1121 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1122 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1124 error ("conflicting access specifications for field `%s', ignored",
1125 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1129 /* They're changing the access to the same thing they changed
1130 it to before. That's OK. */
1136 enforce_access (t, fdecl);
1137 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1143 /* Process the USING_DECL, which is a member of T. */
1146 handle_using_decl (using_decl, t)
1150 tree ctype = DECL_INITIAL (using_decl);
1151 tree name = DECL_NAME (using_decl);
1153 = TREE_PRIVATE (using_decl) ? access_private_node
1154 : TREE_PROTECTED (using_decl) ? access_protected_node
1155 : access_public_node;
1157 tree flist = NULL_TREE;
1160 binfo = binfo_or_else (ctype, t);
1164 if (constructor_name_p (name, ctype))
1166 cp_error_at ("`%D' names constructor", using_decl);
1169 if (constructor_name_p (name, t))
1171 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1175 fdecl = lookup_member (binfo, name, 0, 0);
1179 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1183 if (BASELINK_P (fdecl))
1184 /* Ignore base type this came from. */
1185 fdecl = BASELINK_FUNCTIONS (fdecl);
1187 old_value = IDENTIFIER_CLASS_VALUE (name);
1190 if (is_overloaded_fn (old_value))
1191 old_value = OVL_CURRENT (old_value);
1193 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1196 old_value = NULL_TREE;
1199 if (is_overloaded_fn (fdecl))
1204 else if (is_overloaded_fn (old_value))
1207 /* It's OK to use functions from a base when there are functions with
1208 the same name already present in the current class. */;
1211 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1212 cp_error_at (" because of local method `%#D' with same name",
1213 OVL_CURRENT (old_value));
1217 else if (!DECL_ARTIFICIAL (old_value))
1219 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1220 cp_error_at (" because of local member `%#D' with same name", old_value);
1224 /* Make type T see field decl FDECL with access ACCESS.*/
1226 for (; flist; flist = OVL_NEXT (flist))
1228 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1229 alter_access (t, OVL_CURRENT (flist), access);
1232 alter_access (t, fdecl, access);
1235 /* Run through the base clases of T, updating
1236 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1237 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1241 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1244 int *cant_have_default_ctor_p;
1245 int *cant_have_const_ctor_p;
1246 int *no_const_asn_ref_p;
1250 int seen_non_virtual_nearly_empty_base_p;
1253 binfos = TYPE_BINFO_BASETYPES (t);
1254 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1255 seen_non_virtual_nearly_empty_base_p = 0;
1257 /* An aggregate cannot have baseclasses. */
1258 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1260 for (i = 0; i < n_baseclasses; ++i)
1265 /* Figure out what base we're looking at. */
1266 base_binfo = TREE_VEC_ELT (binfos, i);
1267 basetype = TREE_TYPE (base_binfo);
1269 /* If the type of basetype is incomplete, then we already
1270 complained about that fact (and we should have fixed it up as
1272 if (!COMPLETE_TYPE_P (basetype))
1275 /* The base type is of incomplete type. It is
1276 probably best to pretend that it does not
1278 if (i == n_baseclasses-1)
1279 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1280 TREE_VEC_LENGTH (binfos) -= 1;
1282 for (j = i; j+1 < n_baseclasses; j++)
1283 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1287 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1288 here because the case of virtual functions but non-virtual
1289 dtor is handled in finish_struct_1. */
1290 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1291 && TYPE_HAS_DESTRUCTOR (basetype))
1292 warning ("base class `%#T' has a non-virtual destructor",
1295 /* If the base class doesn't have copy constructors or
1296 assignment operators that take const references, then the
1297 derived class cannot have such a member automatically
1299 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1300 *cant_have_const_ctor_p = 1;
1301 if (TYPE_HAS_ASSIGN_REF (basetype)
1302 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1303 *no_const_asn_ref_p = 1;
1304 /* Similarly, if the base class doesn't have a default
1305 constructor, then the derived class won't have an
1306 automatically generated default constructor. */
1307 if (TYPE_HAS_CONSTRUCTOR (basetype)
1308 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1310 *cant_have_default_ctor_p = 1;
1311 if (! TYPE_HAS_CONSTRUCTOR (t))
1312 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1316 if (TREE_VIA_VIRTUAL (base_binfo))
1317 /* A virtual base does not effect nearly emptiness. */
1319 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1321 if (seen_non_virtual_nearly_empty_base_p)
1322 /* And if there is more than one nearly empty base, then the
1323 derived class is not nearly empty either. */
1324 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1326 /* Remember we've seen one. */
1327 seen_non_virtual_nearly_empty_base_p = 1;
1329 else if (!is_empty_class (basetype))
1330 /* If the base class is not empty or nearly empty, then this
1331 class cannot be nearly empty. */
1332 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1334 /* A lot of properties from the bases also apply to the derived
1336 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1337 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1338 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1339 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1340 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1341 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1342 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1343 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1344 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1345 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1349 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1350 TO. Move primary information from FROM to TO, and recursively traverse
1351 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1352 assoc list of binfos that have already been reseated. */
1355 force_canonical_binfo_r (to, from, type, mappings)
1361 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1363 my_friendly_assert (to != from, 20010905);
1364 BINFO_INDIRECT_PRIMARY_P (to)
1365 = BINFO_INDIRECT_PRIMARY_P (from);
1366 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1367 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1368 BINFO_UNSHARED_MARKED (from) = 0;
1369 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1370 BINFO_LOST_PRIMARY_P (from) = 0;
1371 if (BINFO_PRIMARY_P (from))
1373 tree primary = BINFO_PRIMARY_BASE_OF (from);
1376 /* We might have just moved the primary base too, see if it's on our
1378 assoc = purpose_member (primary, mappings);
1380 primary = TREE_VALUE (assoc);
1381 BINFO_PRIMARY_BASE_OF (to) = primary;
1382 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1384 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1386 mappings = tree_cons (from, to, mappings);
1388 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1389 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1391 tree from_primary = get_primary_binfo (from);
1393 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1394 force_canonical_binfo (get_primary_binfo (to), from_primary,
1398 for (i = 0; i != n_baseclasses; i++)
1400 tree from_binfo = BINFO_BASETYPE (from, i);
1401 tree to_binfo = BINFO_BASETYPE (to, i);
1403 if (TREE_VIA_VIRTUAL (from_binfo))
1405 if (BINFO_PRIMARY_P (from_binfo) &&
1406 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1407 /* This base is a primary of some binfo we have already
1408 reseated. We must reseat this one too. */
1409 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1412 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1416 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1417 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1418 MAPPINGS is an assoc list of binfos that have already been reseated.
1419 Adjust any non-virtual bases within FROM, and also move any virtual bases
1420 which are canonical. This complication arises because selecting primary
1421 bases walks in inheritance graph order, but we don't share binfos for
1422 virtual bases, hence we can fill in the primaries for a virtual base,
1423 and then discover that a later base requires the virtual as its
1427 force_canonical_binfo (to, from, type, mappings)
1433 tree assoc = purpose_member (BINFO_TYPE (to),
1434 CLASSTYPE_VBASECLASSES (type));
1435 if (TREE_VALUE (assoc) != to)
1437 TREE_VALUE (assoc) = to;
1438 force_canonical_binfo_r (to, from, type, mappings);
1442 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1443 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1444 otherwise (because something else has already made it primary). */
1447 mark_primary_virtual_base (base_binfo, type)
1451 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1453 if (BINFO_PRIMARY_P (shared_binfo))
1455 /* It's already allocated in the hierarchy. BINFO won't have a
1456 primary base in this hierarchy, even though the complete object
1457 BINFO is for, would do. */
1461 /* We need to make sure that the assoc list
1462 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1463 primary BINFO for the virtual base, as this is the one
1464 that'll really exist. */
1465 if (base_binfo != shared_binfo)
1466 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1471 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1472 base, then BINFO has no primary base in this graph. Called from
1473 mark_primary_bases. DATA is the most derived type. */
1475 static tree dfs_unshared_virtual_bases (binfo, data)
1479 tree t = (tree) data;
1481 if (!BINFO_UNSHARED_MARKED (binfo)
1482 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1484 /* This morally virtual base has a primary base when it
1485 is a complete object. We need to locate the shared instance
1486 of this binfo in the type dominated by T. We duplicate the
1487 primary base information from there to here. */
1491 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1492 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1494 unshared_base = get_original_base (binfo,
1495 binfo_for_vbase (BINFO_TYPE (vbase),
1497 my_friendly_assert (unshared_base != binfo, 20010612);
1498 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1499 if (!BINFO_LOST_PRIMARY_P (binfo))
1500 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1503 if (binfo != TYPE_BINFO (t))
1504 /* The vtable fields will have been copied when duplicating the
1505 base binfos. That information is bogus, make sure we don't try
1507 BINFO_VTABLE (binfo) = NULL_TREE;
1509 /* If this is a virtual primary base, make sure its offset matches
1510 that which it is primary for. */
1511 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1512 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1514 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1515 BINFO_OFFSET (binfo));
1516 if (!integer_zerop (delta))
1517 propagate_binfo_offsets (binfo, delta, t);
1520 BINFO_UNSHARED_MARKED (binfo) = 0;
1524 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1525 dominated by TYPE that are primary bases. */
1528 mark_primary_bases (type)
1533 /* Walk the bases in inheritance graph order. */
1534 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1538 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1539 /* Not a dynamic base. */
1542 base_binfo = get_primary_binfo (binfo);
1544 if (TREE_VIA_VIRTUAL (base_binfo))
1545 base_binfo = mark_primary_virtual_base (base_binfo, type);
1548 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1550 BINFO_LOST_PRIMARY_P (binfo) = 1;
1552 BINFO_UNSHARED_MARKED (binfo) = 1;
1554 /* There could remain unshared morally virtual bases which were not
1555 visited in the inheritance graph walk. These bases will have lost
1556 their virtual primary base (should they have one). We must now
1557 find them. Also we must fix up the BINFO_OFFSETs of primary
1558 virtual bases. We could not do that as we went along, as they
1559 were originally copied from the bases we inherited from by
1560 unshare_base_binfos. That may have decided differently about
1561 where a virtual primary base went. */
1562 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1565 /* Make the BINFO the primary base of T. */
1568 set_primary_base (t, binfo, vfuns_p)
1575 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1576 basetype = BINFO_TYPE (binfo);
1577 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1578 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1579 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1580 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1581 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1584 /* Determine the primary class for T. */
1587 determine_primary_base (t, vfuns_p)
1591 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1595 /* If there are no baseclasses, there is certainly no primary base. */
1596 if (n_baseclasses == 0)
1599 type_binfo = TYPE_BINFO (t);
1601 for (i = 0; i < n_baseclasses; i++)
1603 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1604 tree basetype = BINFO_TYPE (base_binfo);
1606 if (TYPE_CONTAINS_VPTR_P (basetype))
1608 /* Even a virtual baseclass can contain our RTTI
1609 information. But, we prefer a non-virtual polymorphic
1611 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1612 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1614 /* We prefer a non-virtual base, although a virtual one will
1616 if (TREE_VIA_VIRTUAL (base_binfo))
1619 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1621 set_primary_base (t, base_binfo, vfuns_p);
1622 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1628 /* Only add unique vfields, and flatten them out as we go. */
1629 for (vfields = CLASSTYPE_VFIELDS (basetype);
1631 vfields = TREE_CHAIN (vfields))
1632 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1633 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1634 CLASSTYPE_VFIELDS (t)
1635 = tree_cons (base_binfo,
1636 VF_BASETYPE_VALUE (vfields),
1637 CLASSTYPE_VFIELDS (t));
1642 if (!TYPE_VFIELD (t))
1643 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1645 /* Find the indirect primary bases - those virtual bases which are primary
1646 bases of something else in this hierarchy. */
1647 for (vbases = CLASSTYPE_VBASECLASSES (t);
1649 vbases = TREE_CHAIN (vbases))
1651 tree vbase_binfo = TREE_VALUE (vbases);
1653 /* See if this virtual base is an indirect primary base. To be so,
1654 it must be a primary base within the hierarchy of one of our
1656 for (i = 0; i < n_baseclasses; ++i)
1658 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1661 for (v = CLASSTYPE_VBASECLASSES (basetype);
1665 tree base_vbase = TREE_VALUE (v);
1667 if (BINFO_PRIMARY_P (base_vbase)
1668 && same_type_p (BINFO_TYPE (base_vbase),
1669 BINFO_TYPE (vbase_binfo)))
1671 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1676 /* If we've discovered that this virtual base is an indirect
1677 primary base, then we can move on to the next virtual
1679 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1684 /* A "nearly-empty" virtual base class can be the primary base
1685 class, if no non-virtual polymorphic base can be found. */
1686 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1688 /* If not NULL, this is the best primary base candidate we have
1690 tree candidate = NULL_TREE;
1693 /* Loop over the baseclasses. */
1694 for (base_binfo = TYPE_BINFO (t);
1696 base_binfo = TREE_CHAIN (base_binfo))
1698 tree basetype = BINFO_TYPE (base_binfo);
1700 if (TREE_VIA_VIRTUAL (base_binfo)
1701 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1703 /* If this is not an indirect primary base, then it's
1704 definitely our primary base. */
1705 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1707 candidate = base_binfo;
1711 /* If this is an indirect primary base, it still could be
1712 our primary base -- unless we later find there's another
1713 nearly-empty virtual base that isn't an indirect
1716 candidate = base_binfo;
1720 /* If we've got a primary base, use it. */
1723 set_primary_base (t, candidate, vfuns_p);
1724 CLASSTYPE_VFIELDS (t)
1725 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1729 /* Mark the primary base classes at this point. */
1730 mark_primary_bases (t);
1733 /* Set memoizing fields and bits of T (and its variants) for later
1737 finish_struct_bits (t)
1740 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1742 /* Fix up variants (if any). */
1743 tree variants = TYPE_NEXT_VARIANT (t);
1746 /* These fields are in the _TYPE part of the node, not in
1747 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1748 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1749 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1750 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1751 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1752 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1754 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1755 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1756 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1757 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1758 /* Copy whatever these are holding today. */
1759 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1760 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1761 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1762 TYPE_SIZE (variants) = TYPE_SIZE (t);
1763 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1764 variants = TYPE_NEXT_VARIANT (variants);
1767 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1768 /* For a class w/o baseclasses, `finish_struct' has set
1769 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1770 definition). Similarly for a class whose base classes do not
1771 have vtables. When neither of these is true, we might have
1772 removed abstract virtuals (by providing a definition), added
1773 some (by declaring new ones), or redeclared ones from a base
1774 class. We need to recalculate what's really an abstract virtual
1775 at this point (by looking in the vtables). */
1776 get_pure_virtuals (t);
1780 /* Notice whether this class has type conversion functions defined. */
1781 tree binfo = TYPE_BINFO (t);
1782 tree binfos = BINFO_BASETYPES (binfo);
1785 for (i = n_baseclasses-1; i >= 0; i--)
1787 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1789 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1793 /* If this type has a copy constructor or a destructor, force its mode to
1794 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1795 will cause it to be passed by invisible reference and prevent it from
1796 being returned in a register. */
1797 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1800 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1801 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1803 TYPE_MODE (variants) = BLKmode;
1804 TREE_ADDRESSABLE (variants) = 1;
1809 /* Issue warnings about T having private constructors, but no friends,
1812 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1813 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1814 non-private static member functions. */
1817 maybe_warn_about_overly_private_class (t)
1820 int has_member_fn = 0;
1821 int has_nonprivate_method = 0;
1824 if (!warn_ctor_dtor_privacy
1825 /* If the class has friends, those entities might create and
1826 access instances, so we should not warn. */
1827 || (CLASSTYPE_FRIEND_CLASSES (t)
1828 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1829 /* We will have warned when the template was declared; there's
1830 no need to warn on every instantiation. */
1831 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1832 /* There's no reason to even consider warning about this
1836 /* We only issue one warning, if more than one applies, because
1837 otherwise, on code like:
1840 // Oops - forgot `public:'
1846 we warn several times about essentially the same problem. */
1848 /* Check to see if all (non-constructor, non-destructor) member
1849 functions are private. (Since there are no friends or
1850 non-private statics, we can't ever call any of the private member
1852 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1853 /* We're not interested in compiler-generated methods; they don't
1854 provide any way to call private members. */
1855 if (!DECL_ARTIFICIAL (fn))
1857 if (!TREE_PRIVATE (fn))
1859 if (DECL_STATIC_FUNCTION_P (fn))
1860 /* A non-private static member function is just like a
1861 friend; it can create and invoke private member
1862 functions, and be accessed without a class
1866 has_nonprivate_method = 1;
1869 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1873 if (!has_nonprivate_method && has_member_fn)
1875 /* There are no non-private methods, and there's at least one
1876 private member function that isn't a constructor or
1877 destructor. (If all the private members are
1878 constructors/destructors we want to use the code below that
1879 issues error messages specifically referring to
1880 constructors/destructors.) */
1882 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1883 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1884 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1885 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1887 has_nonprivate_method = 1;
1890 if (!has_nonprivate_method)
1892 warning ("all member functions in class `%T' are private", t);
1897 /* Even if some of the member functions are non-private, the class
1898 won't be useful for much if all the constructors or destructors
1899 are private: such an object can never be created or destroyed. */
1900 if (TYPE_HAS_DESTRUCTOR (t))
1902 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1904 if (TREE_PRIVATE (dtor))
1906 warning ("`%#T' only defines a private destructor and has no friends",
1912 if (TYPE_HAS_CONSTRUCTOR (t))
1914 int nonprivate_ctor = 0;
1916 /* If a non-template class does not define a copy
1917 constructor, one is defined for it, enabling it to avoid
1918 this warning. For a template class, this does not
1919 happen, and so we would normally get a warning on:
1921 template <class T> class C { private: C(); };
1923 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1924 complete non-template or fully instantiated classes have this
1926 if (!TYPE_HAS_INIT_REF (t))
1927 nonprivate_ctor = 1;
1929 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1933 tree ctor = OVL_CURRENT (fn);
1934 /* Ideally, we wouldn't count copy constructors (or, in
1935 fact, any constructor that takes an argument of the
1936 class type as a parameter) because such things cannot
1937 be used to construct an instance of the class unless
1938 you already have one. But, for now at least, we're
1940 if (! TREE_PRIVATE (ctor))
1942 nonprivate_ctor = 1;
1947 if (nonprivate_ctor == 0)
1949 warning ("`%#T' only defines private constructors and has no friends",
1956 /* Function to help qsort sort FIELD_DECLs by name order. */
1959 field_decl_cmp (x, y)
1962 if (DECL_NAME (*x) == DECL_NAME (*y))
1963 /* A nontype is "greater" than a type. */
1964 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1965 if (DECL_NAME (*x) == NULL_TREE)
1967 if (DECL_NAME (*y) == NULL_TREE)
1969 if (DECL_NAME (*x) < DECL_NAME (*y))
1974 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1977 method_name_cmp (m1, m2)
1978 const tree *m1, *m2;
1980 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1982 if (*m1 == NULL_TREE)
1984 if (*m2 == NULL_TREE)
1986 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1991 /* Warn about duplicate methods in fn_fields. Also compact method
1992 lists so that lookup can be made faster.
1994 Data Structure: List of method lists. The outer list is a
1995 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1996 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1997 links the entire list of methods for TYPE_METHODS. Friends are
1998 chained in the same way as member functions (? TREE_CHAIN or
1999 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2000 list. That allows them to be quickly deleted, and requires no
2003 Sort methods that are not special (i.e., constructors, destructors,
2004 and type conversion operators) so that we can find them faster in
2008 finish_struct_methods (t)
2015 if (!TYPE_METHODS (t))
2017 /* Clear these for safety; perhaps some parsing error could set
2018 these incorrectly. */
2019 TYPE_HAS_CONSTRUCTOR (t) = 0;
2020 TYPE_HAS_DESTRUCTOR (t) = 0;
2021 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2025 method_vec = CLASSTYPE_METHOD_VEC (t);
2026 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2027 len = TREE_VEC_LENGTH (method_vec);
2029 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2030 and the next few with type conversion operators (if any). */
2031 for (fn_fields = TYPE_METHODS (t); fn_fields;
2032 fn_fields = TREE_CHAIN (fn_fields))
2033 /* Clear out this flag. */
2034 DECL_IN_AGGR_P (fn_fields) = 0;
2036 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2037 /* We thought there was a destructor, but there wasn't. Some
2038 parse errors cause this anomalous situation. */
2039 TYPE_HAS_DESTRUCTOR (t) = 0;
2041 /* Issue warnings about private constructors and such. If there are
2042 no methods, then some public defaults are generated. */
2043 maybe_warn_about_overly_private_class (t);
2045 /* Now sort the methods. */
2046 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2048 TREE_VEC_LENGTH (method_vec) = len;
2050 /* The type conversion ops have to live at the front of the vec, so we
2052 for (slot = 2; slot < len; ++slot)
2054 tree fn = TREE_VEC_ELT (method_vec, slot);
2056 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2060 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2061 (int (*)(const void *, const void *))method_name_cmp);
2064 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2067 duplicate_tag_error (t)
2070 error ("redefinition of `%#T'", t);
2071 cp_error_at ("previous definition of `%#T'", t);
2073 /* Pretend we haven't defined this type. */
2075 /* All of the component_decl's were TREE_CHAINed together in the parser.
2076 finish_struct_methods walks these chains and assembles all methods with
2077 the same base name into DECL_CHAINs. Now we don't need the parser chains
2078 anymore, so we unravel them. */
2080 /* This used to be in finish_struct, but it turns out that the
2081 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2083 if (CLASSTYPE_METHOD_VEC (t))
2085 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2086 int i, len = TREE_VEC_LENGTH (method_vec);
2087 for (i = 0; i < len; i++)
2089 tree unchain = TREE_VEC_ELT (method_vec, i);
2090 while (unchain != NULL_TREE)
2092 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2093 unchain = OVL_NEXT (unchain);
2098 if (TYPE_LANG_SPECIFIC (t))
2100 tree binfo = TYPE_BINFO (t);
2101 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2102 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2103 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2104 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2106 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2107 BINFO_BASETYPES(binfo) = NULL_TREE;
2109 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2110 TYPE_BINFO (t) = binfo;
2111 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2112 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2113 TYPE_REDEFINED (t) = 1;
2114 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2115 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2117 TYPE_SIZE (t) = NULL_TREE;
2118 TYPE_MODE (t) = VOIDmode;
2119 TYPE_FIELDS (t) = NULL_TREE;
2120 TYPE_METHODS (t) = NULL_TREE;
2121 TYPE_VFIELD (t) = NULL_TREE;
2122 TYPE_CONTEXT (t) = NULL_TREE;
2124 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2125 TYPE_LANG_FLAG_0 (t) = 0;
2126 TYPE_LANG_FLAG_1 (t) = 0;
2127 TYPE_LANG_FLAG_2 (t) = 0;
2128 TYPE_LANG_FLAG_3 (t) = 0;
2129 TYPE_LANG_FLAG_4 (t) = 0;
2130 TYPE_LANG_FLAG_5 (t) = 0;
2131 TYPE_LANG_FLAG_6 (t) = 0;
2132 /* But not this one. */
2133 SET_IS_AGGR_TYPE (t, 1);
2136 /* Make BINFO's vtable have N entries, including RTTI entries,
2137 vbase and vcall offsets, etc. Set its type and call the backend
2141 layout_vtable_decl (binfo, n)
2148 atype = build_cplus_array_type (vtable_entry_type,
2149 build_index_type (size_int (n - 1)));
2150 layout_type (atype);
2152 /* We may have to grow the vtable. */
2153 vtable = get_vtbl_decl_for_binfo (binfo);
2154 if (!same_type_p (TREE_TYPE (vtable), atype))
2156 TREE_TYPE (vtable) = atype;
2157 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2158 layout_decl (vtable, 0);
2160 /* At one time the vtable info was grabbed 2 words at a time. This
2161 fails on SPARC unless you have 8-byte alignment. */
2162 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2163 DECL_ALIGN (vtable));
2167 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2168 have the same signature. */
2171 same_signature_p (fndecl, base_fndecl)
2172 tree fndecl, base_fndecl;
2174 /* One destructor overrides another if they are the same kind of
2176 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2177 && special_function_p (base_fndecl) == special_function_p (fndecl))
2179 /* But a non-destructor never overrides a destructor, nor vice
2180 versa, nor do different kinds of destructors override
2181 one-another. For example, a complete object destructor does not
2182 override a deleting destructor. */
2183 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2186 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2188 tree types, base_types;
2189 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2190 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2191 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2192 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2193 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2199 typedef struct find_final_overrider_data_s {
2200 /* The function for which we are trying to find a final overrider. */
2202 /* The base class in which the function was declared. */
2203 tree declaring_base;
2204 /* The most derived class in the hierarchy. */
2205 tree most_derived_type;
2206 /* The final overriding function. */
2208 /* The functions that we thought might be final overriders, but
2211 /* The BINFO for the class in which the final overriding function
2213 tree overriding_base;
2214 } find_final_overrider_data;
2216 /* Called from find_final_overrider via dfs_walk. */
2219 dfs_find_final_overrider (binfo, data)
2223 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2225 if (same_type_p (BINFO_TYPE (binfo),
2226 BINFO_TYPE (ffod->declaring_base))
2227 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2228 BINFO_OFFSET (ffod->declaring_base)))
2233 /* We haven't found an overrider yet. */
2235 /* We've found a path to the declaring base. Walk down the path
2236 looking for an overrider for FN. */
2237 for (path = reverse_path (binfo);
2239 path = TREE_CHAIN (path))
2241 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2247 /* If we found an overrider, record the overriding function, and
2248 the base from which it came. */
2253 /* Assume the path is non-virtual. See if there are any
2254 virtual bases from (but not including) the overrider up
2255 to and including the base where the function is
2257 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2258 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2260 base = ffod->declaring_base;
2264 /* If we didn't already have an overrider, or any
2265 candidates, then this function is the best candidate so
2267 if (!ffod->overriding_fn && !ffod->candidates)
2269 ffod->overriding_fn = method;
2270 ffod->overriding_base = TREE_VALUE (path);
2272 else if (ffod->overriding_fn)
2274 /* We had a best overrider; let's see how this compares. */
2276 if (ffod->overriding_fn == method
2277 && (tree_int_cst_equal
2278 (BINFO_OFFSET (TREE_VALUE (path)),
2279 BINFO_OFFSET (ffod->overriding_base))))
2280 /* We found the same overrider we already have, and in the
2281 same place; it's still the best. */;
2282 else if (strictly_overrides (ffod->overriding_fn, method))
2283 /* The old function overrides this function; it's still the
2285 else if (strictly_overrides (method, ffod->overriding_fn))
2287 /* The new function overrides the old; it's now the
2289 ffod->overriding_fn = method;
2290 ffod->overriding_base = TREE_VALUE (path);
2296 = build_tree_list (NULL_TREE,
2297 ffod->overriding_fn);
2298 if (method != ffod->overriding_fn)
2300 = tree_cons (NULL_TREE, method, ffod->candidates);
2301 ffod->overriding_fn = NULL_TREE;
2302 ffod->overriding_base = NULL_TREE;
2307 /* We had a list of ambiguous overrides; let's see how this
2308 new one compares. */
2311 bool incomparable = false;
2313 /* If there were previous candidates, and this function
2314 overrides all of them, then it is the new best
2316 for (candidates = ffod->candidates;
2318 candidates = TREE_CHAIN (candidates))
2320 /* If the candidate overrides the METHOD, then we
2321 needn't worry about it any further. */
2322 if (strictly_overrides (TREE_VALUE (candidates),
2329 /* If the METHOD doesn't override the candidate,
2330 then it is incomporable. */
2331 if (!strictly_overrides (method,
2332 TREE_VALUE (candidates)))
2333 incomparable = true;
2336 /* If METHOD overrode all the candidates, then it is the
2337 new best candidate. */
2338 if (!candidates && !incomparable)
2340 ffod->overriding_fn = method;
2341 ffod->overriding_base = TREE_VALUE (path);
2342 ffod->candidates = NULL_TREE;
2344 /* If METHOD didn't override all the candidates, then it
2345 is another candidate. */
2346 else if (method && incomparable)
2348 = tree_cons (NULL_TREE, method, ffod->candidates);
2356 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2357 FN and whose TREE_VALUE is the binfo for the base where the
2358 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2359 base object in which FN is declared. */
2362 find_final_overrider (t, binfo, fn)
2367 find_final_overrider_data ffod;
2369 /* Getting this right is a little tricky. This is valid:
2371 struct S { virtual void f (); };
2372 struct T { virtual void f (); };
2373 struct U : public S, public T { };
2375 even though calling `f' in `U' is ambiguous. But,
2377 struct R { virtual void f(); };
2378 struct S : virtual public R { virtual void f (); };
2379 struct T : virtual public R { virtual void f (); };
2380 struct U : public S, public T { };
2382 is not -- there's no way to decide whether to put `S::f' or
2383 `T::f' in the vtable for `R'.
2385 The solution is to look at all paths to BINFO. If we find
2386 different overriders along any two, then there is a problem. */
2388 ffod.declaring_base = binfo;
2389 ffod.most_derived_type = t;
2390 ffod.overriding_fn = NULL_TREE;
2391 ffod.overriding_base = NULL_TREE;
2392 ffod.candidates = NULL_TREE;
2394 dfs_walk (TYPE_BINFO (t),
2395 dfs_find_final_overrider,
2399 /* If there was no winner, issue an error message. */
2400 if (!ffod.overriding_fn)
2402 error ("no unique final overrider for `%D' in `%T'", fn, t);
2403 return error_mark_node;
2406 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2409 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2410 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2411 the function that the slot in T's primary vtable points to. */
2413 static tree get_matching_virtual PARAMS ((tree, tree));
2415 get_matching_virtual (t, fn)
2420 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2421 if (same_signature_p (BV_FN (f), fn))
2426 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2427 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2428 corresponding position in the BINFO_VIRTUALS list. */
2431 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2444 /* Find the nearest primary base (possibly binfo itself) which defines
2445 this function; this is the class the caller will convert to when
2446 calling FN through BINFO. */
2447 for (b = binfo; ; b = get_primary_binfo (b))
2449 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2452 /* The nearest definition is from a lost primary. */
2453 if (BINFO_LOST_PRIMARY_P (b))
2458 /* Find the final overrider. */
2459 overrider = find_final_overrider (t, b, fn);
2460 if (overrider == error_mark_node)
2463 /* Check for unsupported covariant returns again now that we've
2464 calculated the base offsets. */
2465 check_final_overrider (TREE_PURPOSE (overrider), fn);
2467 /* Assume that we will produce a thunk that convert all the way to
2468 the final overrider, and not to an intermediate virtual base. */
2469 virtual_base = NULL_TREE;
2471 /* See if we can convert to an intermediate virtual base first, and then
2472 use the vcall offset located there to finish the conversion. */
2473 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2475 /* If we find the final overrider, then we can stop
2477 if (same_type_p (BINFO_TYPE (b),
2478 BINFO_TYPE (TREE_VALUE (overrider))))
2481 /* If we find a virtual base, and we haven't yet found the
2482 overrider, then there is a virtual base between the
2483 declaring base (first_defn) and the final overrider. */
2484 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2488 /* Compute the constant adjustment to the `this' pointer. The
2489 `this' pointer, when this function is called, will point at BINFO
2490 (or one of its primary bases, which are at the same offset). */
2493 /* The `this' pointer needs to be adjusted from the declaration to
2494 the nearest virtual base. */
2495 delta = size_diffop (BINFO_OFFSET (virtual_base),
2496 BINFO_OFFSET (first_defn));
2498 /* If the nearest definition is in a lost primary, we don't need an
2499 entry in our vtable. Except possibly in a constructor vtable,
2500 if we happen to get our primary back. In that case, the offset
2501 will be zero, as it will be a primary base. */
2502 delta = size_zero_node;
2505 /* The `this' pointer needs to be adjusted from pointing to
2506 BINFO to pointing at the base where the final overrider
2508 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2509 BINFO_OFFSET (binfo));
2511 if (! integer_zerop (delta))
2513 /* We'll need a thunk. But if we have a (perhaps formerly)
2514 primary virtual base, we have a vcall slot for this function,
2515 so we can use it rather than create a non-virtual thunk. */
2517 b = get_primary_binfo (first_defn);
2518 for (; b; b = get_primary_binfo (b))
2520 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2522 /* b doesn't have this function; no suitable vbase. */
2524 if (TREE_VIA_VIRTUAL (b))
2526 /* Found one; we can treat ourselves as a virtual base. */
2527 virtual_base = binfo;
2528 delta = size_zero_node;
2535 modify_vtable_entry (t,
2537 TREE_PURPOSE (overrider),
2542 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2545 /* Called from modify_all_vtables via dfs_walk. */
2548 dfs_modify_vtables (binfo, data)
2552 if (/* There's no need to modify the vtable for a non-virtual
2553 primary base; we're not going to use that vtable anyhow.
2554 We do still need to do this for virtual primary bases, as they
2555 could become non-primary in a construction vtable. */
2556 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2557 /* Similarly, a base without a vtable needs no modification. */
2558 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2566 make_new_vtable (t, binfo);
2568 /* Now, go through each of the virtual functions in the virtual
2569 function table for BINFO. Find the final overrider, and
2570 update the BINFO_VIRTUALS list appropriately. */
2571 for (virtuals = BINFO_VIRTUALS (binfo),
2572 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2574 virtuals = TREE_CHAIN (virtuals),
2575 old_virtuals = TREE_CHAIN (old_virtuals))
2576 update_vtable_entry_for_fn (t,
2578 BV_FN (old_virtuals),
2582 SET_BINFO_MARKED (binfo);
2587 /* Update all of the primary and secondary vtables for T. Create new
2588 vtables as required, and initialize their RTTI information. Each
2589 of the functions in VIRTUALS is declared in T and may override a
2590 virtual function from a base class; find and modify the appropriate
2591 entries to point to the overriding functions. Returns a list, in
2592 declaration order, of the virtual functions that are declared in T,
2593 but do not appear in the primary base class vtable, and which
2594 should therefore be appended to the end of the vtable for T. */
2597 modify_all_vtables (t, vfuns_p, virtuals)
2602 tree binfo = TYPE_BINFO (t);
2605 /* Update all of the vtables. */
2608 dfs_unmarked_real_bases_queue_p,
2610 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2612 /* Add virtual functions not already in our primary vtable. These
2613 will be both those introduced by this class, and those overridden
2614 from secondary bases. It does not include virtuals merely
2615 inherited from secondary bases. */
2616 for (fnsp = &virtuals; *fnsp; )
2618 tree fn = TREE_VALUE (*fnsp);
2620 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2621 || DECL_VINDEX (fn) == error_mark_node)
2623 /* Set the vtable index. */
2624 set_vindex (fn, vfuns_p);
2625 /* We don't need to convert to a base class when calling
2627 DECL_VIRTUAL_CONTEXT (fn) = t;
2629 /* We don't need to adjust the `this' pointer when
2630 calling this function. */
2631 BV_DELTA (*fnsp) = integer_zero_node;
2632 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2634 /* This is a function not already in our vtable. Keep it. */
2635 fnsp = &TREE_CHAIN (*fnsp);
2638 /* We've already got an entry for this function. Skip it. */
2639 *fnsp = TREE_CHAIN (*fnsp);
2645 /* Here, we already know that they match in every respect.
2646 All we have to check is where they had their declarations.
2648 Return nonzero iff FNDECL1 is declared in a class which has a
2649 proper base class containing FNDECL2. We don't care about
2650 ambiguity or accessibility. */
2653 strictly_overrides (fndecl1, fndecl2)
2654 tree fndecl1, fndecl2;
2658 return (lookup_base (DECL_CONTEXT (fndecl1), DECL_CONTEXT (fndecl2),
2659 ba_ignore | ba_quiet, &kind)
2660 && kind != bk_same_type);
2663 /* Get the base virtual function declarations in T that have the
2667 get_basefndecls (name, t)
2671 tree base_fndecls = NULL_TREE;
2672 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2675 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2676 if (TREE_CODE (methods) == FUNCTION_DECL
2677 && DECL_VINDEX (methods) != NULL_TREE
2678 && DECL_NAME (methods) == name)
2679 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2682 return base_fndecls;
2684 for (i = 0; i < n_baseclasses; i++)
2686 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2687 base_fndecls = chainon (get_basefndecls (name, basetype),
2691 return base_fndecls;
2694 /* If this declaration supersedes the declaration of
2695 a method declared virtual in the base class, then
2696 mark this field as being virtual as well. */
2699 check_for_override (decl, ctype)
2702 if (TREE_CODE (decl) == TEMPLATE_DECL)
2703 /* In [temp.mem] we have:
2705 A specialization of a member function template does not
2706 override a virtual function from a base class. */
2708 if ((DECL_DESTRUCTOR_P (decl)
2709 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2710 && look_for_overrides (ctype, decl)
2711 && !DECL_STATIC_FUNCTION_P (decl))
2712 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2713 the error_mark_node so that we know it is an overriding
2715 DECL_VINDEX (decl) = decl;
2717 if (DECL_VIRTUAL_P (decl))
2719 if (!DECL_VINDEX (decl))
2720 DECL_VINDEX (decl) = error_mark_node;
2721 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2725 /* Warn about hidden virtual functions that are not overridden in t.
2726 We know that constructors and destructors don't apply. */
2732 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2733 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2736 /* We go through each separately named virtual function. */
2737 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2745 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2746 have the same name. Figure out what name that is. */
2747 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2748 /* There are no possibly hidden functions yet. */
2749 base_fndecls = NULL_TREE;
2750 /* Iterate through all of the base classes looking for possibly
2751 hidden functions. */
2752 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2754 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2755 base_fndecls = chainon (get_basefndecls (name, basetype),
2759 /* If there are no functions to hide, continue. */
2763 /* Remove any overridden functions. */
2764 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2766 fndecl = OVL_CURRENT (fns);
2767 if (DECL_VINDEX (fndecl))
2769 tree *prev = &base_fndecls;
2772 /* If the method from the base class has the same
2773 signature as the method from the derived class, it
2774 has been overridden. */
2775 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2776 *prev = TREE_CHAIN (*prev);
2778 prev = &TREE_CHAIN (*prev);
2782 /* Now give a warning for all base functions without overriders,
2783 as they are hidden. */
2784 while (base_fndecls)
2786 /* Here we know it is a hider, and no overrider exists. */
2787 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2788 cp_warning_at (" by `%D'",
2789 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2790 base_fndecls = TREE_CHAIN (base_fndecls);
2795 /* Check for things that are invalid. There are probably plenty of other
2796 things we should check for also. */
2799 finish_struct_anon (t)
2804 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2806 if (TREE_STATIC (field))
2808 if (TREE_CODE (field) != FIELD_DECL)
2811 if (DECL_NAME (field) == NULL_TREE
2812 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2814 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2815 for (; elt; elt = TREE_CHAIN (elt))
2817 /* We're generally only interested in entities the user
2818 declared, but we also find nested classes by noticing
2819 the TYPE_DECL that we create implicitly. You're
2820 allowed to put one anonymous union inside another,
2821 though, so we explicitly tolerate that. We use
2822 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2823 we also allow unnamed types used for defining fields. */
2824 if (DECL_ARTIFICIAL (elt)
2825 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2826 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2829 if (DECL_NAME (elt) == constructor_name (t))
2830 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2833 if (TREE_CODE (elt) != FIELD_DECL)
2835 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2840 if (TREE_PRIVATE (elt))
2841 cp_pedwarn_at ("private member `%#D' in anonymous union",
2843 else if (TREE_PROTECTED (elt))
2844 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2847 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2848 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2854 /* Create default constructors, assignment operators, and so forth for
2855 the type indicated by T, if they are needed.
2856 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2857 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2858 class cannot have a default constructor, copy constructor taking a
2859 const reference argument, or an assignment operator taking a const
2860 reference, respectively. If a virtual destructor is created, its
2861 DECL is returned; otherwise the return value is NULL_TREE. */
2864 add_implicitly_declared_members (t, cant_have_default_ctor,
2865 cant_have_const_cctor,
2866 cant_have_const_assignment)
2868 int cant_have_default_ctor;
2869 int cant_have_const_cctor;
2870 int cant_have_const_assignment;
2873 tree implicit_fns = NULL_TREE;
2874 tree virtual_dtor = NULL_TREE;
2877 ++adding_implicit_members;
2880 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2882 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2883 check_for_override (default_fn, t);
2885 /* If we couldn't make it work, then pretend we didn't need it. */
2886 if (default_fn == void_type_node)
2887 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2890 TREE_CHAIN (default_fn) = implicit_fns;
2891 implicit_fns = default_fn;
2893 if (DECL_VINDEX (default_fn))
2894 virtual_dtor = default_fn;
2898 /* Any non-implicit destructor is non-trivial. */
2899 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2901 /* Default constructor. */
2902 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2904 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2905 TREE_CHAIN (default_fn) = implicit_fns;
2906 implicit_fns = default_fn;
2909 /* Copy constructor. */
2910 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2912 /* ARM 12.18: You get either X(X&) or X(const X&), but
2915 = implicitly_declare_fn (sfk_copy_constructor, t,
2916 /*const_p=*/!cant_have_const_cctor);
2917 TREE_CHAIN (default_fn) = implicit_fns;
2918 implicit_fns = default_fn;
2921 /* Assignment operator. */
2922 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2925 = implicitly_declare_fn (sfk_assignment_operator, t,
2926 /*const_p=*/!cant_have_const_assignment);
2927 TREE_CHAIN (default_fn) = implicit_fns;
2928 implicit_fns = default_fn;
2931 /* Now, hook all of the new functions on to TYPE_METHODS,
2932 and add them to the CLASSTYPE_METHOD_VEC. */
2933 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2934 add_method (t, *f, /*error_p=*/0);
2935 *f = TYPE_METHODS (t);
2936 TYPE_METHODS (t) = implicit_fns;
2938 --adding_implicit_members;
2940 return virtual_dtor;
2943 /* Subroutine of finish_struct_1. Recursively count the number of fields
2944 in TYPE, including anonymous union members. */
2947 count_fields (fields)
2952 for (x = fields; x; x = TREE_CHAIN (x))
2954 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2955 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2962 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2963 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2966 add_fields_to_vec (fields, field_vec, idx)
2967 tree fields, field_vec;
2971 for (x = fields; x; x = TREE_CHAIN (x))
2973 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2974 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2976 TREE_VEC_ELT (field_vec, idx++) = x;
2981 /* FIELD is a bit-field. We are finishing the processing for its
2982 enclosing type. Issue any appropriate messages and set appropriate
2986 check_bitfield_decl (field)
2989 tree type = TREE_TYPE (field);
2992 /* Detect invalid bit-field type. */
2993 if (DECL_INITIAL (field)
2994 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2996 cp_error_at ("bit-field `%#D' with non-integral type", field);
2997 w = error_mark_node;
3000 /* Detect and ignore out of range field width. */
3001 if (DECL_INITIAL (field))
3003 w = DECL_INITIAL (field);
3005 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3008 /* detect invalid field size. */
3009 if (TREE_CODE (w) == CONST_DECL)
3010 w = DECL_INITIAL (w);
3012 w = decl_constant_value (w);
3014 if (TREE_CODE (w) != INTEGER_CST)
3016 cp_error_at ("bit-field `%D' width not an integer constant",
3018 w = error_mark_node;
3020 else if (tree_int_cst_sgn (w) < 0)
3022 cp_error_at ("negative width in bit-field `%D'", field);
3023 w = error_mark_node;
3025 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3027 cp_error_at ("zero width for bit-field `%D'", field);
3028 w = error_mark_node;
3030 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3031 && TREE_CODE (type) != ENUMERAL_TYPE
3032 && TREE_CODE (type) != BOOLEAN_TYPE)
3033 cp_warning_at ("width of `%D' exceeds its type", field);
3034 else if (TREE_CODE (type) == ENUMERAL_TYPE
3035 && (0 > compare_tree_int (w,
3036 min_precision (TYPE_MIN_VALUE (type),
3037 TREE_UNSIGNED (type)))
3038 || 0 > compare_tree_int (w,
3040 (TYPE_MAX_VALUE (type),
3041 TREE_UNSIGNED (type)))))
3042 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3046 /* Remove the bit-field width indicator so that the rest of the
3047 compiler does not treat that value as an initializer. */
3048 DECL_INITIAL (field) = NULL_TREE;
3050 if (w != error_mark_node)
3052 DECL_SIZE (field) = convert (bitsizetype, w);
3053 DECL_BIT_FIELD (field) = 1;
3055 if (integer_zerop (w)
3056 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
3058 #ifdef EMPTY_FIELD_BOUNDARY
3059 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3060 EMPTY_FIELD_BOUNDARY);
3062 #ifdef PCC_BITFIELD_TYPE_MATTERS
3063 if (PCC_BITFIELD_TYPE_MATTERS)
3065 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3067 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3074 /* Non-bit-fields are aligned for their type. */
3075 DECL_BIT_FIELD (field) = 0;
3076 CLEAR_DECL_C_BIT_FIELD (field);
3077 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3078 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3082 /* FIELD is a non bit-field. We are finishing the processing for its
3083 enclosing type T. Issue any appropriate messages and set appropriate
3087 check_field_decl (field, t, cant_have_const_ctor,
3088 cant_have_default_ctor, no_const_asn_ref,
3089 any_default_members)
3092 int *cant_have_const_ctor;
3093 int *cant_have_default_ctor;
3094 int *no_const_asn_ref;
3095 int *any_default_members;
3097 tree type = strip_array_types (TREE_TYPE (field));
3099 /* An anonymous union cannot contain any fields which would change
3100 the settings of CANT_HAVE_CONST_CTOR and friends. */
3101 if (ANON_UNION_TYPE_P (type))
3103 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3104 structs. So, we recurse through their fields here. */
3105 else if (ANON_AGGR_TYPE_P (type))
3109 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3110 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3111 check_field_decl (fields, t, cant_have_const_ctor,
3112 cant_have_default_ctor, no_const_asn_ref,
3113 any_default_members);
3115 /* Check members with class type for constructors, destructors,
3117 else if (CLASS_TYPE_P (type))
3119 /* Never let anything with uninheritable virtuals
3120 make it through without complaint. */
3121 abstract_virtuals_error (field, type);
3123 if (TREE_CODE (t) == UNION_TYPE)
3125 if (TYPE_NEEDS_CONSTRUCTING (type))
3126 cp_error_at ("member `%#D' with constructor not allowed in union",
3128 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3129 cp_error_at ("member `%#D' with destructor not allowed in union",
3131 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3132 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3137 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3138 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3139 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3140 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3141 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3144 if (!TYPE_HAS_CONST_INIT_REF (type))
3145 *cant_have_const_ctor = 1;
3147 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3148 *no_const_asn_ref = 1;
3150 if (TYPE_HAS_CONSTRUCTOR (type)
3151 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3152 *cant_have_default_ctor = 1;
3154 if (DECL_INITIAL (field) != NULL_TREE)
3156 /* `build_class_init_list' does not recognize
3158 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3159 cp_error_at ("multiple fields in union `%T' initialized");
3160 *any_default_members = 1;
3163 /* Non-bit-fields are aligned for their type, except packed fields
3164 which require only BITS_PER_UNIT alignment. */
3165 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3166 (DECL_PACKED (field)
3168 : TYPE_ALIGN (TREE_TYPE (field))));
3169 if (! DECL_PACKED (field))
3170 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3173 /* Check the data members (both static and non-static), class-scoped
3174 typedefs, etc., appearing in the declaration of T. Issue
3175 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3176 declaration order) of access declarations; each TREE_VALUE in this
3177 list is a USING_DECL.
3179 In addition, set the following flags:
3182 The class is empty, i.e., contains no non-static data members.
3184 CANT_HAVE_DEFAULT_CTOR_P
3185 This class cannot have an implicitly generated default
3188 CANT_HAVE_CONST_CTOR_P
3189 This class cannot have an implicitly generated copy constructor
3190 taking a const reference.
3192 CANT_HAVE_CONST_ASN_REF
3193 This class cannot have an implicitly generated assignment
3194 operator taking a const reference.
3196 All of these flags should be initialized before calling this
3199 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3200 fields can be added by adding to this chain. */
3203 check_field_decls (t, access_decls, empty_p,
3204 cant_have_default_ctor_p, cant_have_const_ctor_p,
3209 int *cant_have_default_ctor_p;
3210 int *cant_have_const_ctor_p;
3211 int *no_const_asn_ref_p;
3216 int any_default_members;
3218 /* First, delete any duplicate fields. */
3219 delete_duplicate_fields (TYPE_FIELDS (t));
3221 /* Assume there are no access declarations. */
3222 *access_decls = NULL_TREE;
3223 /* Assume this class has no pointer members. */
3225 /* Assume none of the members of this class have default
3227 any_default_members = 0;
3229 for (field = &TYPE_FIELDS (t); *field; field = next)
3232 tree type = TREE_TYPE (x);
3234 next = &TREE_CHAIN (x);
3236 if (TREE_CODE (x) == FIELD_DECL)
3238 DECL_PACKED (x) |= TYPE_PACKED (t);
3240 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3241 /* We don't treat zero-width bitfields as making a class
3246 /* The class is non-empty. */
3248 /* The class is not even nearly empty. */
3249 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3253 if (TREE_CODE (x) == USING_DECL)
3255 /* Prune the access declaration from the list of fields. */
3256 *field = TREE_CHAIN (x);
3258 /* Save the access declarations for our caller. */
3259 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3261 /* Since we've reset *FIELD there's no reason to skip to the
3267 if (TREE_CODE (x) == TYPE_DECL
3268 || TREE_CODE (x) == TEMPLATE_DECL)
3271 /* If we've gotten this far, it's a data member, possibly static,
3272 or an enumerator. */
3274 DECL_CONTEXT (x) = t;
3276 /* ``A local class cannot have static data members.'' ARM 9.4 */
3277 if (current_function_decl && TREE_STATIC (x))
3278 cp_error_at ("field `%D' in local class cannot be static", x);
3280 /* Perform error checking that did not get done in
3282 if (TREE_CODE (type) == FUNCTION_TYPE)
3284 cp_error_at ("field `%D' invalidly declared function type",
3286 type = build_pointer_type (type);
3287 TREE_TYPE (x) = type;
3289 else if (TREE_CODE (type) == METHOD_TYPE)
3291 cp_error_at ("field `%D' invalidly declared method type", x);
3292 type = build_pointer_type (type);
3293 TREE_TYPE (x) = type;
3295 else if (TREE_CODE (type) == OFFSET_TYPE)
3297 cp_error_at ("field `%D' invalidly declared offset type", x);
3298 type = build_pointer_type (type);
3299 TREE_TYPE (x) = type;
3302 if (type == error_mark_node)
3305 /* When this goes into scope, it will be a non-local reference. */
3306 DECL_NONLOCAL (x) = 1;
3308 if (TREE_CODE (x) == CONST_DECL)
3311 if (TREE_CODE (x) == VAR_DECL)
3313 if (TREE_CODE (t) == UNION_TYPE)
3314 /* Unions cannot have static members. */
3315 cp_error_at ("field `%D' declared static in union", x);
3320 /* Now it can only be a FIELD_DECL. */
3322 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3323 CLASSTYPE_NON_AGGREGATE (t) = 1;
3325 /* If this is of reference type, check if it needs an init.
3326 Also do a little ANSI jig if necessary. */
3327 if (TREE_CODE (type) == REFERENCE_TYPE)
3329 CLASSTYPE_NON_POD_P (t) = 1;
3330 if (DECL_INITIAL (x) == NULL_TREE)
3331 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3333 /* ARM $12.6.2: [A member initializer list] (or, for an
3334 aggregate, initialization by a brace-enclosed list) is the
3335 only way to initialize nonstatic const and reference
3337 *cant_have_default_ctor_p = 1;
3338 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3340 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3341 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3344 type = strip_array_types (type);
3346 if (TREE_CODE (type) == POINTER_TYPE)
3349 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3350 CLASSTYPE_HAS_MUTABLE (t) = 1;
3352 if (! pod_type_p (type))
3353 /* DR 148 now allows pointers to members (which are POD themselves),
3354 to be allowed in POD structs. */
3355 CLASSTYPE_NON_POD_P (t) = 1;
3357 if (! zero_init_p (type))
3358 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3360 /* If any field is const, the structure type is pseudo-const. */
3361 if (CP_TYPE_CONST_P (type))
3363 C_TYPE_FIELDS_READONLY (t) = 1;
3364 if (DECL_INITIAL (x) == NULL_TREE)
3365 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3367 /* ARM $12.6.2: [A member initializer list] (or, for an
3368 aggregate, initialization by a brace-enclosed list) is the
3369 only way to initialize nonstatic const and reference
3371 *cant_have_default_ctor_p = 1;
3372 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3374 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3375 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3377 /* A field that is pseudo-const makes the structure likewise. */
3378 else if (IS_AGGR_TYPE (type))
3380 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3381 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3382 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3385 /* Core issue 80: A nonstatic data member is required to have a
3386 different name from the class iff the class has a
3387 user-defined constructor. */
3388 if (DECL_NAME (x) == constructor_name (t)
3389 && TYPE_HAS_CONSTRUCTOR (t))
3390 cp_pedwarn_at ("field `%#D' with same name as class", x);
3392 /* We set DECL_C_BIT_FIELD in grokbitfield.
3393 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3394 if (DECL_C_BIT_FIELD (x))
3395 check_bitfield_decl (x);
3397 check_field_decl (x, t,
3398 cant_have_const_ctor_p,
3399 cant_have_default_ctor_p,
3401 &any_default_members);
3404 /* Effective C++ rule 11. */
3405 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3406 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3408 warning ("`%#T' has pointer data members", t);
3410 if (! TYPE_HAS_INIT_REF (t))
3412 warning (" but does not override `%T(const %T&)'", t, t);
3413 if (! TYPE_HAS_ASSIGN_REF (t))
3414 warning (" or `operator=(const %T&)'", t);
3416 else if (! TYPE_HAS_ASSIGN_REF (t))
3417 warning (" but does not override `operator=(const %T&)'", t);
3421 /* Check anonymous struct/anonymous union fields. */
3422 finish_struct_anon (t);
3424 /* We've built up the list of access declarations in reverse order.
3426 *access_decls = nreverse (*access_decls);
3429 /* If TYPE is an empty class type, records its OFFSET in the table of
3433 record_subobject_offset (type, offset, offsets)
3440 if (!is_empty_class (type))
3443 /* Record the location of this empty object in OFFSETS. */
3444 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3446 n = splay_tree_insert (offsets,
3447 (splay_tree_key) offset,
3448 (splay_tree_value) NULL_TREE);
3449 n->value = ((splay_tree_value)
3450 tree_cons (NULL_TREE,
3457 /* Returns nonzero if TYPE is an empty class type and there is
3458 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3461 check_subobject_offset (type, offset, offsets)
3469 if (!is_empty_class (type))
3472 /* Record the location of this empty object in OFFSETS. */
3473 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3477 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3478 if (same_type_p (TREE_VALUE (t), type))
3484 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3485 F for every subobject, passing it the type, offset, and table of
3486 OFFSETS. If VBASES_P is nonzero, then even virtual non-primary
3487 bases should be traversed; otherwise, they are ignored.
3489 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3490 than MAX_OFFSET will not be walked.
3492 If F returns a nonzero value, the traversal ceases, and that value
3493 is returned. Otherwise, returns zero. */
3496 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3498 subobject_offset_fn f;
3506 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3508 if (max_offset && INT_CST_LT (max_offset, offset))
3511 if (CLASS_TYPE_P (type))
3516 /* Record the location of TYPE. */
3517 r = (*f) (type, offset, offsets);
3521 /* Iterate through the direct base classes of TYPE. */
3522 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3524 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3527 && TREE_VIA_VIRTUAL (binfo)
3528 && !BINFO_PRIMARY_P (binfo))
3531 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3533 size_binop (PLUS_EXPR,
3535 BINFO_OFFSET (binfo)),
3543 /* Iterate through the fields of TYPE. */
3544 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3545 if (TREE_CODE (field) == FIELD_DECL)
3547 r = walk_subobject_offsets (TREE_TYPE (field),
3549 size_binop (PLUS_EXPR,
3551 DECL_FIELD_OFFSET (field)),
3559 else if (TREE_CODE (type) == ARRAY_TYPE)
3561 tree domain = TYPE_DOMAIN (type);
3564 /* Step through each of the elements in the array. */
3565 for (index = size_zero_node;
3566 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3567 index = size_binop (PLUS_EXPR, index, size_one_node))
3569 r = walk_subobject_offsets (TREE_TYPE (type),
3577 offset = size_binop (PLUS_EXPR, offset,
3578 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3579 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3580 there's no point in iterating through the remaining
3581 elements of the array. */
3582 if (max_offset && INT_CST_LT (max_offset, offset))
3590 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3591 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3595 record_subobject_offsets (type, offset, offsets, vbases_p)
3601 walk_subobject_offsets (type, record_subobject_offset, offset,
3602 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3605 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3606 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3607 virtual bases of TYPE are examined. */
3610 layout_conflict_p (type, offset, offsets, vbases_p)
3616 splay_tree_node max_node;
3618 /* Get the node in OFFSETS that indicates the maximum offset where
3619 an empty subobject is located. */
3620 max_node = splay_tree_max (offsets);
3621 /* If there aren't any empty subobjects, then there's no point in
3622 performing this check. */
3626 return walk_subobject_offsets (type, check_subobject_offset, offset,
3627 offsets, (tree) (max_node->key),
3631 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3632 non-static data member of the type indicated by RLI. BINFO is the
3633 binfo corresponding to the base subobject, OFFSETS maps offsets to
3634 types already located at those offsets. T is the most derived
3635 type. This function determines the position of the DECL. */
3638 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3639 record_layout_info rli;
3645 tree offset = NULL_TREE;
3646 tree type = TREE_TYPE (decl);
3647 /* If we are laying out a base class, rather than a field, then
3648 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3649 int field_p = !DECL_ARTIFICIAL (decl);
3651 /* Try to place the field. It may take more than one try if we have
3652 a hard time placing the field without putting two objects of the
3653 same type at the same address. */
3656 struct record_layout_info_s old_rli = *rli;
3658 /* Place this field. */
3659 place_field (rli, decl);
3660 offset = byte_position (decl);
3662 /* We have to check to see whether or not there is already
3663 something of the same type at the offset we're about to use.
3667 struct T : public S { int i; };
3668 struct U : public S, public T {};
3670 Here, we put S at offset zero in U. Then, we can't put T at
3671 offset zero -- its S component would be at the same address
3672 as the S we already allocated. So, we have to skip ahead.
3673 Since all data members, including those whose type is an
3674 empty class, have nonzero size, any overlap can happen only
3675 with a direct or indirect base-class -- it can't happen with
3677 if (layout_conflict_p (TREE_TYPE (decl),
3682 /* Strip off the size allocated to this field. That puts us
3683 at the first place we could have put the field with
3684 proper alignment. */
3687 /* Bump up by the alignment required for the type. */
3689 = size_binop (PLUS_EXPR, rli->bitpos,
3691 ? CLASSTYPE_ALIGN (type)
3692 : TYPE_ALIGN (type)));
3693 normalize_rli (rli);
3696 /* There was no conflict. We're done laying out this field. */
3700 /* Now that we know where it will be placed, update its
3702 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3703 propagate_binfo_offsets (binfo,
3704 convert (ssizetype, offset), t);
3707 /* Layout the empty base BINFO. EOC indicates the byte currently just
3708 past the end of the class, and should be correctly aligned for a
3709 class of the type indicated by BINFO; OFFSETS gives the offsets of
3710 the empty bases allocated so far. T is the most derived
3711 type. Return nonzero iff we added it at the end. */
3714 layout_empty_base (binfo, eoc, offsets, t)
3721 tree basetype = BINFO_TYPE (binfo);
3724 /* This routine should only be used for empty classes. */
3725 my_friendly_assert (is_empty_class (basetype), 20000321);
3726 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3728 /* This is an empty base class. We first try to put it at offset
3730 if (layout_conflict_p (BINFO_TYPE (binfo),
3731 BINFO_OFFSET (binfo),
3735 /* That didn't work. Now, we move forward from the next
3736 available spot in the class. */
3738 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3741 if (!layout_conflict_p (BINFO_TYPE (binfo),
3742 BINFO_OFFSET (binfo),
3745 /* We finally found a spot where there's no overlap. */
3748 /* There's overlap here, too. Bump along to the next spot. */
3749 propagate_binfo_offsets (binfo, alignment, t);
3755 /* Build a FIELD_DECL for the base given by BINFO in the class
3756 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3757 *BASE_ALIGN is a running maximum of the alignments of any base
3758 class. OFFSETS gives the location of empty base subobjects. T is
3759 the most derived type. Return nonzero if the new object cannot be
3763 build_base_field (rli, binfo, empty_p, offsets, t)
3764 record_layout_info rli;
3770 tree basetype = BINFO_TYPE (binfo);
3774 if (!COMPLETE_TYPE_P (basetype))
3775 /* This error is now reported in xref_tag, thus giving better
3776 location information. */
3779 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3780 DECL_ARTIFICIAL (decl) = 1;
3781 DECL_FIELD_CONTEXT (decl) = rli->t;
3782 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3783 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3784 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3785 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3786 /* Tell the backend not to round up to TYPE_ALIGN. */
3787 DECL_PACKED (decl) = 1;
3789 if (!integer_zerop (DECL_SIZE (decl)))
3791 /* The containing class is non-empty because it has a non-empty
3795 /* Try to place the field. It may take more than one try if we
3796 have a hard time placing the field without putting two
3797 objects of the same type at the same address. */
3798 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3802 unsigned HOST_WIDE_INT eoc;
3804 /* On some platforms (ARM), even empty classes will not be
3806 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3807 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3808 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3811 /* Record the offsets of BINFO and its base subobjects. */
3812 record_subobject_offsets (BINFO_TYPE (binfo),
3813 BINFO_OFFSET (binfo),
3819 /* Layout all of the non-virtual base classes. Record empty
3820 subobjects in OFFSETS. T is the most derived type. Return
3821 nonzero if the type cannot be nearly empty. */
3824 build_base_fields (rli, empty_p, offsets, t)
3825 record_layout_info rli;
3830 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3833 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3837 /* The primary base class is always allocated first. */
3838 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3839 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3840 empty_p, offsets, t);
3842 /* Now allocate the rest of the bases. */
3843 for (i = 0; i < n_baseclasses; ++i)
3847 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3849 /* The primary base was already allocated above, so we don't
3850 need to allocate it again here. */
3851 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3854 /* A primary virtual base class is allocated just like any other
3855 base class, but a non-primary virtual base is allocated
3856 later, in layout_virtual_bases. */
3857 if (TREE_VIA_VIRTUAL (base_binfo)
3858 && !BINFO_PRIMARY_P (base_binfo))
3861 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3866 /* Go through the TYPE_METHODS of T issuing any appropriate
3867 diagnostics, figuring out which methods override which other
3868 methods, and so forth. */
3876 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3878 /* If this was an evil function, don't keep it in class. */
3879 if (DECL_ASSEMBLER_NAME_SET_P (x)
3880 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3883 check_for_override (x, t);
3884 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3885 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3887 /* The name of the field is the original field name
3888 Save this in auxiliary field for later overloading. */
3889 if (DECL_VINDEX (x))
3891 TYPE_POLYMORPHIC_P (t) = 1;
3892 if (DECL_PURE_VIRTUAL_P (x))
3893 CLASSTYPE_PURE_VIRTUALS (t)
3894 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3899 /* FN is a constructor or destructor. Clone the declaration to create
3900 a specialized in-charge or not-in-charge version, as indicated by
3904 build_clone (fn, name)
3911 /* Copy the function. */
3912 clone = copy_decl (fn);
3913 /* Remember where this function came from. */
3914 DECL_CLONED_FUNCTION (clone) = fn;
3915 DECL_ABSTRACT_ORIGIN (clone) = fn;
3916 /* Reset the function name. */
3917 DECL_NAME (clone) = name;
3918 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3919 /* There's no pending inline data for this function. */
3920 DECL_PENDING_INLINE_INFO (clone) = NULL;
3921 DECL_PENDING_INLINE_P (clone) = 0;
3922 /* And it hasn't yet been deferred. */
3923 DECL_DEFERRED_FN (clone) = 0;
3925 /* The base-class destructor is not virtual. */
3926 if (name == base_dtor_identifier)
3928 DECL_VIRTUAL_P (clone) = 0;
3929 if (TREE_CODE (clone) != TEMPLATE_DECL)
3930 DECL_VINDEX (clone) = NULL_TREE;
3933 /* If there was an in-charge parameter, drop it from the function
3935 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3941 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3942 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3943 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3944 /* Skip the `this' parameter. */
3945 parmtypes = TREE_CHAIN (parmtypes);
3946 /* Skip the in-charge parameter. */
3947 parmtypes = TREE_CHAIN (parmtypes);
3948 /* And the VTT parm, in a complete [cd]tor. */
3949 if (DECL_HAS_VTT_PARM_P (fn)
3950 && ! DECL_NEEDS_VTT_PARM_P (clone))
3951 parmtypes = TREE_CHAIN (parmtypes);
3952 /* If this is subobject constructor or destructor, add the vtt
3955 = build_cplus_method_type (basetype,
3956 TREE_TYPE (TREE_TYPE (clone)),
3959 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3963 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3964 aren't function parameters; those are the template parameters. */
3965 if (TREE_CODE (clone) != TEMPLATE_DECL)
3967 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3968 /* Remove the in-charge parameter. */
3969 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3971 TREE_CHAIN (DECL_ARGUMENTS (clone))
3972 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3973 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3975 /* And the VTT parm, in a complete [cd]tor. */
3976 if (DECL_HAS_VTT_PARM_P (fn))
3978 if (DECL_NEEDS_VTT_PARM_P (clone))
3979 DECL_HAS_VTT_PARM_P (clone) = 1;
3982 TREE_CHAIN (DECL_ARGUMENTS (clone))
3983 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3984 DECL_HAS_VTT_PARM_P (clone) = 0;
3988 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
3990 DECL_CONTEXT (parms) = clone;
3991 cxx_dup_lang_specific_decl (parms);
3995 /* Create the RTL for this function. */
3996 SET_DECL_RTL (clone, NULL_RTX);
3997 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
3999 /* Make it easy to find the CLONE given the FN. */
4000 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4001 TREE_CHAIN (fn) = clone;
4003 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4004 if (TREE_CODE (clone) == TEMPLATE_DECL)
4008 DECL_TEMPLATE_RESULT (clone)
4009 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4010 result = DECL_TEMPLATE_RESULT (clone);
4011 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4012 DECL_TI_TEMPLATE (result) = clone;
4014 else if (DECL_DEFERRED_FN (fn))
4020 /* Produce declarations for all appropriate clones of FN. If
4021 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4022 CLASTYPE_METHOD_VEC as well. */
4025 clone_function_decl (fn, update_method_vec_p)
4027 int update_method_vec_p;
4031 /* Avoid inappropriate cloning. */
4033 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4036 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4038 /* For each constructor, we need two variants: an in-charge version
4039 and a not-in-charge version. */
4040 clone = build_clone (fn, complete_ctor_identifier);
4041 if (update_method_vec_p)
4042 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4043 clone = build_clone (fn, base_ctor_identifier);
4044 if (update_method_vec_p)
4045 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4049 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4051 /* For each destructor, we need three variants: an in-charge
4052 version, a not-in-charge version, and an in-charge deleting
4053 version. We clone the deleting version first because that
4054 means it will go second on the TYPE_METHODS list -- and that
4055 corresponds to the correct layout order in the virtual
4058 For a non-virtual destructor, we do not build a deleting
4060 if (DECL_VIRTUAL_P (fn))
4062 clone = build_clone (fn, deleting_dtor_identifier);
4063 if (update_method_vec_p)
4064 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4066 clone = build_clone (fn, complete_dtor_identifier);
4067 if (update_method_vec_p)
4068 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4069 clone = build_clone (fn, base_dtor_identifier);
4070 if (update_method_vec_p)
4071 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4074 /* Note that this is an abstract function that is never emitted. */
4075 DECL_ABSTRACT (fn) = 1;
4078 /* DECL is an in charge constructor, which is being defined. This will
4079 have had an in class declaration, from whence clones were
4080 declared. An out-of-class definition can specify additional default
4081 arguments. As it is the clones that are involved in overload
4082 resolution, we must propagate the information from the DECL to its
4086 adjust_clone_args (decl)
4091 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4092 clone = TREE_CHAIN (clone))
4094 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4095 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4096 tree decl_parms, clone_parms;
4098 clone_parms = orig_clone_parms;
4100 /* Skip the 'this' parameter. */
4101 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4102 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4104 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4105 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4106 if (DECL_HAS_VTT_PARM_P (decl))
4107 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4109 clone_parms = orig_clone_parms;
4110 if (DECL_HAS_VTT_PARM_P (clone))
4111 clone_parms = TREE_CHAIN (clone_parms);
4113 for (decl_parms = orig_decl_parms; decl_parms;
4114 decl_parms = TREE_CHAIN (decl_parms),
4115 clone_parms = TREE_CHAIN (clone_parms))
4117 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4118 TREE_TYPE (clone_parms)), 20010424);
4120 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4122 /* A default parameter has been added. Adjust the
4123 clone's parameters. */
4124 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4125 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4128 clone_parms = orig_decl_parms;
4130 if (DECL_HAS_VTT_PARM_P (clone))
4132 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4133 TREE_VALUE (orig_clone_parms),
4135 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4137 type = build_cplus_method_type (basetype,
4138 TREE_TYPE (TREE_TYPE (clone)),
4141 type = build_exception_variant (type, exceptions);
4142 TREE_TYPE (clone) = type;
4144 clone_parms = NULL_TREE;
4148 my_friendly_assert (!clone_parms, 20010424);
4152 /* For each of the constructors and destructors in T, create an
4153 in-charge and not-in-charge variant. */
4156 clone_constructors_and_destructors (t)
4161 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4163 if (!CLASSTYPE_METHOD_VEC (t))
4166 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4167 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4168 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4169 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4172 /* Remove all zero-width bit-fields from T. */
4175 remove_zero_width_bit_fields (t)
4180 fieldsp = &TYPE_FIELDS (t);
4183 if (TREE_CODE (*fieldsp) == FIELD_DECL
4184 && DECL_C_BIT_FIELD (*fieldsp)
4185 && DECL_INITIAL (*fieldsp))
4186 *fieldsp = TREE_CHAIN (*fieldsp);
4188 fieldsp = &TREE_CHAIN (*fieldsp);
4192 /* Returns TRUE iff we need a cookie when dynamically allocating an
4193 array whose elements have the indicated class TYPE. */
4196 type_requires_array_cookie (type)
4200 bool has_two_argument_delete_p = false;
4202 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4204 /* If there's a non-trivial destructor, we need a cookie. In order
4205 to iterate through the array calling the destructor for each
4206 element, we'll have to know how many elements there are. */
4207 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4210 /* If the usual deallocation function is a two-argument whose second
4211 argument is of type `size_t', then we have to pass the size of
4212 the array to the deallocation function, so we will need to store
4214 fns = lookup_fnfields (TYPE_BINFO (type),
4215 ansi_opname (VEC_DELETE_EXPR),
4217 /* If there are no `operator []' members, or the lookup is
4218 ambiguous, then we don't need a cookie. */
4219 if (!fns || fns == error_mark_node)
4221 /* Loop through all of the functions. */
4222 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4227 /* Select the current function. */
4228 fn = OVL_CURRENT (fns);
4229 /* See if this function is a one-argument delete function. If
4230 it is, then it will be the usual deallocation function. */
4231 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4232 if (second_parm == void_list_node)
4234 /* Otherwise, if we have a two-argument function and the second
4235 argument is `size_t', it will be the usual deallocation
4236 function -- unless there is one-argument function, too. */
4237 if (TREE_CHAIN (second_parm) == void_list_node
4238 && same_type_p (TREE_VALUE (second_parm), sizetype))
4239 has_two_argument_delete_p = true;
4242 return has_two_argument_delete_p;
4245 /* Check the validity of the bases and members declared in T. Add any
4246 implicitly-generated functions (like copy-constructors and
4247 assignment operators). Compute various flag bits (like
4248 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4249 level: i.e., independently of the ABI in use. */
4252 check_bases_and_members (t, empty_p)
4256 /* Nonzero if we are not allowed to generate a default constructor
4258 int cant_have_default_ctor;
4259 /* Nonzero if the implicitly generated copy constructor should take
4260 a non-const reference argument. */
4261 int cant_have_const_ctor;
4262 /* Nonzero if the the implicitly generated assignment operator
4263 should take a non-const reference argument. */
4264 int no_const_asn_ref;
4267 /* By default, we use const reference arguments and generate default
4269 cant_have_default_ctor = 0;
4270 cant_have_const_ctor = 0;
4271 no_const_asn_ref = 0;
4273 /* Assume that the class is nearly empty; we'll clear this flag if
4274 it turns out not to be nearly empty. */
4275 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4277 /* Check all the base-classes. */
4278 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4281 /* Check all the data member declarations. */
4282 check_field_decls (t, &access_decls, empty_p,
4283 &cant_have_default_ctor,
4284 &cant_have_const_ctor,
4287 /* Check all the method declarations. */
4290 /* A nearly-empty class has to be vptr-containing; a nearly empty
4291 class contains just a vptr. */
4292 if (!TYPE_CONTAINS_VPTR_P (t))
4293 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4295 /* Do some bookkeeping that will guide the generation of implicitly
4296 declared member functions. */
4297 TYPE_HAS_COMPLEX_INIT_REF (t)
4298 |= (TYPE_HAS_INIT_REF (t)
4299 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4300 || TYPE_POLYMORPHIC_P (t));
4301 TYPE_NEEDS_CONSTRUCTING (t)
4302 |= (TYPE_HAS_CONSTRUCTOR (t)
4303 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4304 || TYPE_POLYMORPHIC_P (t));
4305 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4306 || TYPE_POLYMORPHIC_P (t));
4307 CLASSTYPE_NON_POD_P (t)
4308 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4309 || TYPE_HAS_ASSIGN_REF (t));
4310 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4311 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4312 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4314 /* Synthesize any needed methods. Note that methods will be synthesized
4315 for anonymous unions; grok_x_components undoes that. */
4316 add_implicitly_declared_members (t, cant_have_default_ctor,
4317 cant_have_const_ctor,
4320 /* Create the in-charge and not-in-charge variants of constructors
4322 clone_constructors_and_destructors (t);
4324 /* Process the using-declarations. */
4325 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4326 handle_using_decl (TREE_VALUE (access_decls), t);
4328 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4329 finish_struct_methods (t);
4331 /* Figure out whether or not we will need a cookie when dynamically
4332 allocating an array of this type. */
4333 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4334 = type_requires_array_cookie (t);
4337 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4338 accordingly. If a new vfield was created (because T doesn't have a
4339 primary base class), then the newly created field is returned. It
4340 is not added to the TYPE_FIELDS list; it is the caller's
4341 responsibility to do that. Accumulate declared virtual functions
4345 create_vtable_ptr (t, empty_p, virtuals_p)
4352 /* Collect the virtual functions declared in T. */
4353 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4354 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4355 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4357 tree new_virtual = make_node (TREE_LIST);
4359 BV_FN (new_virtual) = fn;
4360 BV_DELTA (new_virtual) = integer_zero_node;
4362 TREE_CHAIN (new_virtual) = *virtuals_p;
4363 *virtuals_p = new_virtual;
4366 /* If we couldn't find an appropriate base class, create a new field
4367 here. Even if there weren't any new virtual functions, we might need a
4368 new virtual function table if we're supposed to include vptrs in
4369 all classes that need them. */
4370 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4372 /* We build this decl with vtbl_ptr_type_node, which is a
4373 `vtable_entry_type*'. It might seem more precise to use
4374 `vtable_entry_type (*)[N]' where N is the number of firtual
4375 functions. However, that would require the vtable pointer in
4376 base classes to have a different type than the vtable pointer
4377 in derived classes. We could make that happen, but that
4378 still wouldn't solve all the problems. In particular, the
4379 type-based alias analysis code would decide that assignments
4380 to the base class vtable pointer can't alias assignments to
4381 the derived class vtable pointer, since they have different
4382 types. Thus, in an derived class destructor, where the base
4383 class constructor was inlined, we could generate bad code for
4384 setting up the vtable pointer.
4386 Therefore, we use one type for all vtable pointers. We still
4387 use a type-correct type; it's just doesn't indicate the array
4388 bounds. That's better than using `void*' or some such; it's
4389 cleaner, and it let's the alias analysis code know that these
4390 stores cannot alias stores to void*! */
4393 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4394 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4395 DECL_VIRTUAL_P (field) = 1;
4396 DECL_ARTIFICIAL (field) = 1;
4397 DECL_FIELD_CONTEXT (field) = t;
4398 DECL_FCONTEXT (field) = t;
4399 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4400 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4402 TYPE_VFIELD (t) = field;
4404 /* This class is non-empty. */
4407 if (CLASSTYPE_N_BASECLASSES (t))
4408 /* If there were any baseclasses, they can't possibly be at
4409 offset zero any more, because that's where the vtable
4410 pointer is. So, converting to a base class is going to
4412 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4420 /* Fixup the inline function given by INFO now that the class is
4424 fixup_pending_inline (fn)
4427 if (DECL_PENDING_INLINE_INFO (fn))
4429 tree args = DECL_ARGUMENTS (fn);
4432 DECL_CONTEXT (args) = fn;
4433 args = TREE_CHAIN (args);
4438 /* Fixup the inline methods and friends in TYPE now that TYPE is
4442 fixup_inline_methods (type)
4445 tree method = TYPE_METHODS (type);
4447 if (method && TREE_CODE (method) == TREE_VEC)
4449 if (TREE_VEC_ELT (method, 1))
4450 method = TREE_VEC_ELT (method, 1);
4451 else if (TREE_VEC_ELT (method, 0))
4452 method = TREE_VEC_ELT (method, 0);
4454 method = TREE_VEC_ELT (method, 2);
4457 /* Do inline member functions. */
4458 for (; method; method = TREE_CHAIN (method))
4459 fixup_pending_inline (method);
4462 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4464 method = TREE_CHAIN (method))
4465 fixup_pending_inline (TREE_VALUE (method));
4466 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4469 /* Add OFFSET to all base types of BINFO which is a base in the
4470 hierarchy dominated by T.
4472 OFFSET, which is a type offset, is number of bytes. */
4475 propagate_binfo_offsets (binfo, offset, t)
4483 /* Update BINFO's offset. */
4484 BINFO_OFFSET (binfo)
4485 = convert (sizetype,
4486 size_binop (PLUS_EXPR,
4487 convert (ssizetype, BINFO_OFFSET (binfo)),
4490 /* Find the primary base class. */
4491 primary_binfo = get_primary_binfo (binfo);
4493 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4495 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4499 /* On the first time through the loop, do the primary base.
4500 Because the primary base need not be an immediate base, we
4501 must handle the primary base specially. */
4507 base_binfo = primary_binfo;
4511 base_binfo = BINFO_BASETYPE (binfo, i);
4512 /* Don't do the primary base twice. */
4513 if (base_binfo == primary_binfo)
4517 /* Skip virtual bases that aren't our canonical primary base. */
4518 if (TREE_VIA_VIRTUAL (base_binfo)
4519 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4520 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4523 propagate_binfo_offsets (base_binfo, offset, t);
4527 /* Called via dfs_walk from layout_virtual bases. */
4530 dfs_set_offset_for_unshared_vbases (binfo, data)
4534 /* If this is a virtual base, make sure it has the same offset as
4535 the shared copy. If it's a primary base, then we know it's
4537 if (TREE_VIA_VIRTUAL (binfo))
4539 tree t = (tree) data;
4543 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4546 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4547 propagate_binfo_offsets (binfo, offset, t);
4554 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4555 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4556 empty subobjects of T. */
4559 layout_virtual_bases (t, offsets)
4564 unsigned HOST_WIDE_INT eoc;
4565 bool first_vbase = true;
4567 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4570 #ifdef STRUCTURE_SIZE_BOUNDARY
4571 /* Packed structures don't need to have minimum size. */
4572 if (! TYPE_PACKED (t))
4573 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4576 /* DSIZE is the size of the class without the virtual bases. */
4577 if (abi_version_at_least(2))
4578 dsize = CLASSTYPE_SIZE (t);
4580 dsize = TYPE_SIZE (t);
4582 /* Make every class have alignment of at least one. */
4583 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4585 /* Go through the virtual bases, allocating space for each virtual
4586 base that is not already a primary base class. These are
4587 allocated in inheritance graph order. */
4588 for (vbases = TYPE_BINFO (t);
4590 vbases = TREE_CHAIN (vbases))
4594 if (!TREE_VIA_VIRTUAL (vbases))
4597 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4599 if (!BINFO_PRIMARY_P (vbase))
4601 /* This virtual base is not a primary base of any class in the
4602 hierarchy, so we have to add space for it. */
4603 tree basetype, usize;
4604 unsigned int desired_align;
4606 basetype = BINFO_TYPE (vbase);
4608 desired_align = CLASSTYPE_ALIGN (basetype);
4609 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4611 /* Add padding so that we can put the virtual base class at an
4612 appropriately aligned offset. */
4613 dsize = round_up (dsize, desired_align);
4614 usize = size_binop (CEIL_DIV_EXPR, dsize, bitsize_unit_node);
4616 /* We try to squish empty virtual bases in just like
4617 ordinary empty bases. */
4618 if (is_empty_class (basetype))
4619 layout_empty_base (vbase,
4620 convert (sizetype, usize),
4626 offset = convert (ssizetype, usize);
4627 offset = size_diffop (offset,
4629 BINFO_OFFSET (vbase)));
4631 /* And compute the offset of the virtual base. */
4632 propagate_binfo_offsets (vbase, offset, t);
4633 /* Every virtual baseclass takes a least a UNIT, so that
4634 we can take it's address and get something different
4636 dsize = size_binop (PLUS_EXPR, dsize,
4637 size_binop (MAX_EXPR, bitsize_unit_node,
4638 CLASSTYPE_SIZE (basetype)));
4641 /* If the first virtual base might have been placed at a
4642 lower address, had we started from CLASSTYPE_SIZE, rather
4643 than TYPE_SIZE, issue a warning. There can be both false
4644 positives and false negatives from this warning in rare
4645 cases; to deal with all the possibilities would probably
4646 require performing both layout algorithms and comparing
4647 the results which is not particularly tractable. */
4650 && tree_int_cst_lt (size_binop (CEIL_DIV_EXPR,
4651 round_up (CLASSTYPE_SIZE (t),
4654 BINFO_OFFSET (vbase)))
4655 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4658 /* Keep track of the offsets assigned to this virtual base. */
4659 record_subobject_offsets (BINFO_TYPE (vbase),
4660 BINFO_OFFSET (vbase),
4664 first_vbase = false;
4668 /* Now, go through the TYPE_BINFO hierarchy, setting the
4669 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4670 bases and their direct and indirect bases. The ambiguity checks
4671 in lookup_base depend on the BINFO_OFFSETs being set
4673 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4675 /* If we had empty base classes that protruded beyond the end of the
4676 class, we didn't update DSIZE above; we were hoping to overlay
4677 multiple such bases at the same location. */
4678 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4679 dsize = size_binop (MAX_EXPR, dsize, bitsize_int (eoc * BITS_PER_UNIT));
4681 /* Now, make sure that the total size of the type is a multiple of
4683 dsize = round_up (dsize, TYPE_ALIGN (t));
4684 TYPE_SIZE (t) = dsize;
4685 TYPE_SIZE_UNIT (t) = convert (sizetype,
4686 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4687 bitsize_unit_node));
4689 /* Check for ambiguous virtual bases. */
4691 for (vbases = CLASSTYPE_VBASECLASSES (t);
4693 vbases = TREE_CHAIN (vbases))
4695 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4697 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4698 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4703 /* Returns the offset of the byte just past the end of the base class
4704 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4705 only non-virtual bases are included. */
4707 static unsigned HOST_WIDE_INT
4708 end_of_class (t, include_virtuals_p)
4710 int include_virtuals_p;
4712 unsigned HOST_WIDE_INT result = 0;
4715 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4720 unsigned HOST_WIDE_INT end_of_base;
4722 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4724 if (!include_virtuals_p
4725 && TREE_VIA_VIRTUAL (base_binfo)
4726 && !BINFO_PRIMARY_P (base_binfo))
4729 if (is_empty_class (BINFO_TYPE (base_binfo)))
4730 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4731 allocate some space for it. It cannot have virtual bases,
4732 so TYPE_SIZE_UNIT is fine. */
4733 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4735 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4736 offset = size_binop (PLUS_EXPR,
4737 BINFO_OFFSET (base_binfo),
4739 end_of_base = tree_low_cst (offset, /*pos=*/1);
4740 if (end_of_base > result)
4741 result = end_of_base;
4747 /* Warn about direct bases of T that are inaccessible because they are
4748 ambiguous. For example:
4751 struct T : public S {};
4752 struct U : public S, public T {};
4754 Here, `(S*) new U' is not allowed because there are two `S'
4758 warn_about_ambiguous_direct_bases (t)
4763 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4765 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4767 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4768 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4773 /* Compare two INTEGER_CSTs K1 and K2. */
4776 splay_tree_compare_integer_csts (k1, k2)
4780 return tree_int_cst_compare ((tree) k1, (tree) k2);
4783 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4784 BINFO_OFFSETs for all of the base-classes. Position the vtable
4785 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4788 layout_class_type (t, empty_p, vfuns_p, virtuals_p)
4794 tree non_static_data_members;
4797 record_layout_info rli;
4798 unsigned HOST_WIDE_INT eoc;
4799 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4800 types that appear at that offset. */
4801 splay_tree empty_base_offsets;
4802 /* True if the last field layed out was a bit-field. */
4803 bool last_field_was_bitfield = false;
4805 /* Keep track of the first non-static data member. */
4806 non_static_data_members = TYPE_FIELDS (t);
4808 /* Start laying out the record. */
4809 rli = start_record_layout (t);
4811 /* If possible, we reuse the virtual function table pointer from one
4812 of our base classes. */
4813 determine_primary_base (t, vfuns_p);
4815 /* Create a pointer to our virtual function table. */
4816 vptr = create_vtable_ptr (t, empty_p, virtuals_p);
4818 /* The vptr is always the first thing in the class. */
4821 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4822 place_field (rli, vptr);
4825 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4826 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4828 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4829 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4831 /* Layout the non-static data members. */
4832 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4837 /* We still pass things that aren't non-static data members to
4838 the back-end, in case it wants to do something with them. */
4839 if (TREE_CODE (field) != FIELD_DECL)
4841 place_field (rli, field);
4842 /* If the static data member has incomplete type, keep track
4843 of it so that it can be completed later. (The handling
4844 of pending statics in finish_record_layout is
4845 insufficient; consider:
4848 struct S2 { static S1 s1; };
4850 At this point, finish_record_layout will be called, but
4851 S1 is still incomplete.) */
4852 if (TREE_CODE (field) == VAR_DECL)
4853 maybe_register_incomplete_var (field);
4857 type = TREE_TYPE (field);
4859 /* If this field is a bit-field whose width is greater than its
4860 type, then there are some special rules for allocating
4862 if (DECL_C_BIT_FIELD (field)
4863 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4865 integer_type_kind itk;
4868 /* We must allocate the bits as if suitably aligned for the
4869 longest integer type that fits in this many bits. type
4870 of the field. Then, we are supposed to use the left over
4871 bits as additional padding. */
4872 for (itk = itk_char; itk != itk_none; ++itk)
4873 if (INT_CST_LT (DECL_SIZE (field),
4874 TYPE_SIZE (integer_types[itk])))
4877 /* ITK now indicates a type that is too large for the
4878 field. We have to back up by one to find the largest
4880 integer_type = integer_types[itk - 1];
4882 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4883 /* In a union, the padding field must have the full width
4884 of the bit-field; all fields start at offset zero. */
4885 padding = DECL_SIZE (field);
4888 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4889 warning ("size assigned to `%T' may not be "
4890 "ABI-compliant and may change in a future "
4893 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4894 TYPE_SIZE (integer_type));
4896 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4897 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4898 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4901 padding = NULL_TREE;
4903 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4904 empty_base_offsets, t);
4906 /* If a bit-field does not immediately follow another bit-field,
4907 and yet it starts in the middle of a byte, we have failed to
4908 comply with the ABI. */
4910 && DECL_C_BIT_FIELD (field)
4911 && !last_field_was_bitfield
4912 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4913 DECL_FIELD_BIT_OFFSET (field),
4914 bitsize_unit_node)))
4915 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4918 /* If we needed additional padding after this field, add it
4924 padding_field = build_decl (FIELD_DECL,
4927 DECL_BIT_FIELD (padding_field) = 1;
4928 DECL_SIZE (padding_field) = padding;
4929 DECL_ALIGN (padding_field) = 1;
4930 DECL_USER_ALIGN (padding_field) = 0;
4931 layout_nonempty_base_or_field (rli, padding_field,
4933 empty_base_offsets, t);
4936 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4939 /* It might be the case that we grew the class to allocate a
4940 zero-sized base class. That won't be reflected in RLI, yet,
4941 because we are willing to overlay multiple bases at the same
4942 offset. However, now we need to make sure that RLI is big enough
4943 to reflect the entire class. */
4944 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4945 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4946 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4948 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4949 rli->bitpos = bitsize_zero_node;
4952 /* We make all structures have at least one element, so that they
4953 have nonzero size. The class may be empty even if it has
4954 basetypes. Therefore, we add the fake field after all the other
4955 fields; if there are already FIELD_DECLs on the list, their
4956 offsets will not be disturbed. */
4957 if (!eoc && *empty_p)
4961 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4962 place_field (rli, padding);
4964 else if (abi_version_at_least (2)
4965 && !integer_zerop (rli->bitpos))
4966 /* Make sure that we are on a byte boundary so that the size of
4967 the class without virtual bases will always be a round number
4969 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4971 /* Let the back-end lay out the type. Note that at this point we
4972 have only included non-virtual base-classes; we will lay out the
4973 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4974 this call are not necessarily correct; they are just the size and
4975 alignment when no virtual base clases are used. */
4976 finish_record_layout (rli);
4978 /* Delete all zero-width bit-fields from the list of fields. Now
4979 that the type is laid out they are no longer important. */
4980 remove_zero_width_bit_fields (t);
4982 /* Remember the size and alignment of the class before adding
4983 the virtual bases. */
4986 CLASSTYPE_SIZE (t) = bitsize_zero_node;
4987 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
4989 /* If this is a POD, we can't reuse its tail padding. */
4990 else if (!CLASSTYPE_NON_POD_P (t))
4992 CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
4993 CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
4997 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
4998 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5001 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5002 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5004 /* Set the TYPE_DECL for this type to contain the right
5005 value for DECL_OFFSET, so that we can use it as part
5006 of a COMPONENT_REF for multiple inheritance. */
5007 layout_decl (TYPE_MAIN_DECL (t), 0);
5009 /* Now fix up any virtual base class types that we left lying
5010 around. We must get these done before we try to lay out the
5011 virtual function table. As a side-effect, this will remove the
5012 base subobject fields. */
5013 layout_virtual_bases (t, empty_base_offsets);
5015 /* Warn about direct bases that can't be talked about due to
5017 warn_about_ambiguous_direct_bases (t);
5020 splay_tree_delete (empty_base_offsets);
5023 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5024 (or C++ class declaration).
5026 For C++, we must handle the building of derived classes.
5027 Also, C++ allows static class members. The way that this is
5028 handled is to keep the field name where it is (as the DECL_NAME
5029 of the field), and place the overloaded decl in the bit position
5030 of the field. layout_record and layout_union will know about this.
5032 More C++ hair: inline functions have text in their
5033 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5034 meaningful tree structure. After the struct has been laid out, set
5035 things up so that this can happen.
5037 And still more: virtual functions. In the case of single inheritance,
5038 when a new virtual function is seen which redefines a virtual function
5039 from the base class, the new virtual function is placed into
5040 the virtual function table at exactly the same address that
5041 it had in the base class. When this is extended to multiple
5042 inheritance, the same thing happens, except that multiple virtual
5043 function tables must be maintained. The first virtual function
5044 table is treated in exactly the same way as in the case of single
5045 inheritance. Additional virtual function tables have different
5046 DELTAs, which tell how to adjust `this' to point to the right thing.
5048 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5056 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5057 tree virtuals = NULL_TREE;
5062 if (COMPLETE_TYPE_P (t))
5064 if (IS_AGGR_TYPE (t))
5065 error ("redefinition of `%#T'", t);
5072 /* If this type was previously laid out as a forward reference,
5073 make sure we lay it out again. */
5074 TYPE_SIZE (t) = NULL_TREE;
5075 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5076 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5078 CLASSTYPE_RTTI (t) = NULL_TREE;
5080 fixup_inline_methods (t);
5082 /* Do end-of-class semantic processing: checking the validity of the
5083 bases and members and add implicitly generated methods. */
5084 check_bases_and_members (t, &empty);
5086 /* Layout the class itself. */
5087 layout_class_type (t, &empty, &vfuns, &virtuals);
5089 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5090 vfield = TYPE_VFIELD (t);
5091 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5093 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5095 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5096 BINFO_TYPE (primary)),
5098 /* The vtable better be at the start. */
5099 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5101 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5104 vfield = copy_decl (vfield);
5105 DECL_FIELD_CONTEXT (vfield) = t;
5106 TYPE_VFIELD (t) = vfield;
5109 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5111 virtuals = modify_all_vtables (t, &vfuns, nreverse (virtuals));
5113 /* If we created a new vtbl pointer for this class, add it to the
5115 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5116 CLASSTYPE_VFIELDS (t)
5117 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5119 /* If necessary, create the primary vtable for this class. */
5120 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5122 /* We must enter these virtuals into the table. */
5123 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5124 build_primary_vtable (NULL_TREE, t);
5125 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5126 /* Here we know enough to change the type of our virtual
5127 function table, but we will wait until later this function. */
5128 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5130 /* If this type has basetypes with constructors, then those
5131 constructors might clobber the virtual function table. But
5132 they don't if the derived class shares the exact vtable of the base
5134 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5136 /* If we didn't need a new vtable, see if we should copy one from
5138 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5140 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5142 /* If this class uses a different vtable than its primary base
5143 then when we will need to initialize our vptr after the base
5144 class constructor runs. */
5145 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5146 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5149 if (TYPE_CONTAINS_VPTR_P (t))
5151 if (TYPE_BINFO_VTABLE (t))
5152 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5154 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5155 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5158 CLASSTYPE_VSIZE (t) = vfuns;
5159 /* Add entries for virtual functions introduced by this class. */
5160 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5163 finish_struct_bits (t);
5165 /* Complete the rtl for any static member objects of the type we're
5167 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5168 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5169 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5170 DECL_MODE (x) = TYPE_MODE (t);
5172 /* Done with FIELDS...now decide whether to sort these for
5173 faster lookups later.
5175 The C front-end only does this when n_fields > 15. We use
5176 a smaller number because most searches fail (succeeding
5177 ultimately as the search bores through the inheritance
5178 hierarchy), and we want this failure to occur quickly. */
5180 n_fields = count_fields (TYPE_FIELDS (t));
5183 tree field_vec = make_tree_vec (n_fields);
5184 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5185 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5186 (int (*)(const void *, const void *))field_decl_cmp);
5187 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5188 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5189 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5192 if (TYPE_HAS_CONSTRUCTOR (t))
5194 tree vfields = CLASSTYPE_VFIELDS (t);
5196 for (vfields = CLASSTYPE_VFIELDS (t);
5197 vfields; vfields = TREE_CHAIN (vfields))
5198 /* Mark the fact that constructor for T could affect anybody
5199 inheriting from T who wants to initialize vtables for
5201 if (VF_BINFO_VALUE (vfields))
5202 TREE_ADDRESSABLE (vfields) = 1;
5205 /* Make the rtl for any new vtables we have created, and unmark
5206 the base types we marked. */
5209 /* Build the VTT for T. */
5212 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5213 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5214 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5218 if (warn_overloaded_virtual)
5221 maybe_suppress_debug_info (t);
5223 dump_class_hierarchy (t);
5225 /* Finish debugging output for this type. */
5226 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5229 /* When T was built up, the member declarations were added in reverse
5230 order. Rearrange them to declaration order. */
5233 unreverse_member_declarations (t)
5240 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5241 reverse order. Put them in declaration order now. */
5242 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5243 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5245 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5246 reverse order, so we can't just use nreverse. */
5248 for (x = TYPE_FIELDS (t);
5249 x && TREE_CODE (x) != TYPE_DECL;
5252 next = TREE_CHAIN (x);
5253 TREE_CHAIN (x) = prev;
5258 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5260 TYPE_FIELDS (t) = prev;
5265 finish_struct (t, attributes)
5268 const char *saved_filename = input_filename;
5269 int saved_lineno = lineno;
5271 /* Now that we've got all the field declarations, reverse everything
5273 unreverse_member_declarations (t);
5275 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5277 /* Nadger the current location so that diagnostics point to the start of
5278 the struct, not the end. */
5279 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5280 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5282 if (processing_template_decl)
5284 finish_struct_methods (t);
5285 TYPE_SIZE (t) = bitsize_zero_node;
5288 finish_struct_1 (t);
5290 input_filename = saved_filename;
5291 lineno = saved_lineno;
5293 TYPE_BEING_DEFINED (t) = 0;
5295 if (current_class_type)
5298 error ("trying to finish struct, but kicked out due to previous parse errors");
5300 if (processing_template_decl && at_function_scope_p ())
5301 add_stmt (build_min (TAG_DEFN, t));
5306 /* Return the dynamic type of INSTANCE, if known.
5307 Used to determine whether the virtual function table is needed
5310 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5311 of our knowledge of its type. *NONNULL should be initialized
5312 before this function is called. */
5315 fixed_type_or_null (instance, nonnull, cdtorp)
5320 switch (TREE_CODE (instance))
5323 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5326 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5330 /* This is a call to a constructor, hence it's never zero. */
5331 if (TREE_HAS_CONSTRUCTOR (instance))
5335 return TREE_TYPE (instance);
5340 /* This is a call to a constructor, hence it's never zero. */
5341 if (TREE_HAS_CONSTRUCTOR (instance))
5345 return TREE_TYPE (instance);
5347 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5354 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5355 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5356 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5357 /* Propagate nonnull. */
5358 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5363 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5368 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5371 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5375 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5376 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5380 return TREE_TYPE (TREE_TYPE (instance));
5382 /* fall through... */
5386 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5390 return TREE_TYPE (instance);
5392 else if (instance == current_class_ptr)
5397 /* if we're in a ctor or dtor, we know our type. */
5398 if (DECL_LANG_SPECIFIC (current_function_decl)
5399 && (DECL_CONSTRUCTOR_P (current_function_decl)
5400 || DECL_DESTRUCTOR_P (current_function_decl)))
5404 return TREE_TYPE (TREE_TYPE (instance));
5407 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5409 /* Reference variables should be references to objects. */
5413 if (TREE_CODE (instance) == VAR_DECL
5414 && DECL_INITIAL (instance))
5415 return fixed_type_or_null (DECL_INITIAL (instance),
5425 /* Return nonzero if the dynamic type of INSTANCE is known, and
5426 equivalent to the static type. We also handle the case where
5427 INSTANCE is really a pointer. Return negative if this is a
5428 ctor/dtor. There the dynamic type is known, but this might not be
5429 the most derived base of the original object, and hence virtual
5430 bases may not be layed out according to this type.
5432 Used to determine whether the virtual function table is needed
5435 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5436 of our knowledge of its type. *NONNULL should be initialized
5437 before this function is called. */
5440 resolves_to_fixed_type_p (instance, nonnull)
5444 tree t = TREE_TYPE (instance);
5447 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5448 if (fixed == NULL_TREE)
5450 if (POINTER_TYPE_P (t))
5452 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5454 return cdtorp ? -1 : 1;
5459 init_class_processing ()
5461 current_class_depth = 0;
5462 current_class_stack_size = 10;
5464 = (class_stack_node_t) xmalloc (current_class_stack_size
5465 * sizeof (struct class_stack_node));
5466 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5468 access_default_node = build_int_2 (0, 0);
5469 access_public_node = build_int_2 (ak_public, 0);
5470 access_protected_node = build_int_2 (ak_protected, 0);
5471 access_private_node = build_int_2 (ak_private, 0);
5472 access_default_virtual_node = build_int_2 (4, 0);
5473 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5474 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5475 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5477 ridpointers[(int) RID_PUBLIC] = access_public_node;
5478 ridpointers[(int) RID_PRIVATE] = access_private_node;
5479 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5482 /* Set current scope to NAME. CODE tells us if this is a
5483 STRUCT, UNION, or ENUM environment.
5485 NAME may end up being NULL_TREE if this is an anonymous or
5486 late-bound struct (as in "struct { ... } foo;") */
5488 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5489 appropriate values, found by looking up the type definition of
5492 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5493 which can be seen locally to the class. They are shadowed by
5494 any subsequent local declaration (including parameter names).
5496 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5497 which have static meaning (i.e., static members, static
5498 member functions, enum declarations, etc).
5500 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5501 which can be seen locally to the class (as in 1), but
5502 know that we are doing this for declaration purposes
5503 (i.e. friend foo::bar (int)).
5505 So that we may avoid calls to lookup_name, we cache the _TYPE
5506 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5508 For multiple inheritance, we perform a two-pass depth-first search
5509 of the type lattice. The first pass performs a pre-order search,
5510 marking types after the type has had its fields installed in
5511 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5512 unmarks the marked types. If a field or member function name
5513 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5514 that name becomes `error_mark_node'. */
5517 pushclass (type, modify)
5521 type = TYPE_MAIN_VARIANT (type);
5523 /* Make sure there is enough room for the new entry on the stack. */
5524 if (current_class_depth + 1 >= current_class_stack_size)
5526 current_class_stack_size *= 2;
5528 = (class_stack_node_t) xrealloc (current_class_stack,
5529 current_class_stack_size
5530 * sizeof (struct class_stack_node));
5533 /* Insert a new entry on the class stack. */
5534 current_class_stack[current_class_depth].name = current_class_name;
5535 current_class_stack[current_class_depth].type = current_class_type;
5536 current_class_stack[current_class_depth].access = current_access_specifier;
5537 current_class_stack[current_class_depth].names_used = 0;
5538 current_class_depth++;
5540 /* Now set up the new type. */
5541 current_class_name = TYPE_NAME (type);
5542 if (TREE_CODE (current_class_name) == TYPE_DECL)
5543 current_class_name = DECL_NAME (current_class_name);
5544 current_class_type = type;
5546 /* By default, things in classes are private, while things in
5547 structures or unions are public. */
5548 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5549 ? access_private_node
5550 : access_public_node);
5552 if (previous_class_type != NULL_TREE
5553 && (type != previous_class_type
5554 || !COMPLETE_TYPE_P (previous_class_type))
5555 && current_class_depth == 1)
5557 /* Forcibly remove any old class remnants. */
5558 invalidate_class_lookup_cache ();
5561 /* If we're about to enter a nested class, clear
5562 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5563 if (modify && current_class_depth > 1)
5564 clear_identifier_class_values ();
5570 if (type != previous_class_type || current_class_depth > 1)
5571 push_class_decls (type);
5576 /* We are re-entering the same class we just left, so we
5577 don't have to search the whole inheritance matrix to find
5578 all the decls to bind again. Instead, we install the
5579 cached class_shadowed list, and walk through it binding
5580 names and setting up IDENTIFIER_TYPE_VALUEs. */
5581 set_class_shadows (previous_class_values);
5582 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5584 tree id = TREE_PURPOSE (item);
5585 tree decl = TREE_TYPE (item);
5587 push_class_binding (id, decl);
5588 if (TREE_CODE (decl) == TYPE_DECL)
5589 set_identifier_type_value (id, TREE_TYPE (decl));
5591 unuse_fields (type);
5594 storetags (CLASSTYPE_TAGS (type));
5598 /* When we exit a toplevel class scope, we save the
5599 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5600 reenter the class. Here, we've entered some other class, so we
5601 must invalidate our cache. */
5604 invalidate_class_lookup_cache ()
5608 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5609 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5610 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5612 previous_class_values = NULL_TREE;
5613 previous_class_type = NULL_TREE;
5616 /* Get out of the current class scope. If we were in a class scope
5617 previously, that is the one popped to. */
5625 current_class_depth--;
5626 current_class_name = current_class_stack[current_class_depth].name;
5627 current_class_type = current_class_stack[current_class_depth].type;
5628 current_access_specifier = current_class_stack[current_class_depth].access;
5629 if (current_class_stack[current_class_depth].names_used)
5630 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5633 /* Returns 1 if current_class_type is either T or a nested type of T.
5634 We start looking from 1 because entry 0 is from global scope, and has
5638 currently_open_class (t)
5642 if (t == current_class_type)
5644 for (i = 1; i < current_class_depth; ++i)
5645 if (current_class_stack [i].type == t)
5650 /* If either current_class_type or one of its enclosing classes are derived
5651 from T, return the appropriate type. Used to determine how we found
5652 something via unqualified lookup. */
5655 currently_open_derived_class (t)
5660 if (DERIVED_FROM_P (t, current_class_type))
5661 return current_class_type;
5663 for (i = current_class_depth - 1; i > 0; --i)
5664 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5665 return current_class_stack[i].type;
5670 /* When entering a class scope, all enclosing class scopes' names with
5671 static meaning (static variables, static functions, types and enumerators)
5672 have to be visible. This recursive function calls pushclass for all
5673 enclosing class contexts until global or a local scope is reached.
5674 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5675 formal of the same name. */
5678 push_nested_class (type, modify)
5684 /* A namespace might be passed in error cases, like A::B:C. */
5685 if (type == NULL_TREE
5686 || type == error_mark_node
5687 || TREE_CODE (type) == NAMESPACE_DECL
5688 || ! IS_AGGR_TYPE (type)
5689 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5690 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5693 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5695 if (context && CLASS_TYPE_P (context))
5696 push_nested_class (context, 2);
5697 pushclass (type, modify);
5700 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5705 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5708 if (context && CLASS_TYPE_P (context))
5709 pop_nested_class ();
5712 /* Returns the number of extern "LANG" blocks we are nested within. */
5715 current_lang_depth ()
5717 return VARRAY_ACTIVE_SIZE (current_lang_base);
5720 /* Set global variables CURRENT_LANG_NAME to appropriate value
5721 so that behavior of name-mangling machinery is correct. */
5724 push_lang_context (name)
5727 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5729 if (name == lang_name_cplusplus)
5731 current_lang_name = name;
5733 else if (name == lang_name_java)
5735 current_lang_name = name;
5736 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5737 (See record_builtin_java_type in decl.c.) However, that causes
5738 incorrect debug entries if these types are actually used.
5739 So we re-enable debug output after extern "Java". */
5740 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5741 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5742 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5743 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5744 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5745 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5746 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5747 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5749 else if (name == lang_name_c)
5751 current_lang_name = name;
5754 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5757 /* Get out of the current language scope. */
5762 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5763 VARRAY_POP (current_lang_base);
5766 /* Type instantiation routines. */
5768 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5769 matches the TARGET_TYPE. If there is no satisfactory match, return
5770 error_mark_node, and issue an error message if COMPLAIN is
5771 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5772 If TEMPLATE_ONLY, the name of the overloaded function
5773 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5774 template arguments. */
5777 resolve_address_of_overloaded_function (target_type,
5788 tree explicit_targs;
5790 /* Here's what the standard says:
5794 If the name is a function template, template argument deduction
5795 is done, and if the argument deduction succeeds, the deduced
5796 arguments are used to generate a single template function, which
5797 is added to the set of overloaded functions considered.
5799 Non-member functions and static member functions match targets of
5800 type "pointer-to-function" or "reference-to-function." Nonstatic
5801 member functions match targets of type "pointer-to-member
5802 function;" the function type of the pointer to member is used to
5803 select the member function from the set of overloaded member
5804 functions. If a nonstatic member function is selected, the
5805 reference to the overloaded function name is required to have the
5806 form of a pointer to member as described in 5.3.1.
5808 If more than one function is selected, any template functions in
5809 the set are eliminated if the set also contains a non-template
5810 function, and any given template function is eliminated if the
5811 set contains a second template function that is more specialized
5812 than the first according to the partial ordering rules 14.5.5.2.
5813 After such eliminations, if any, there shall remain exactly one
5814 selected function. */
5817 int is_reference = 0;
5818 /* We store the matches in a TREE_LIST rooted here. The functions
5819 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5820 interoperability with most_specialized_instantiation. */
5821 tree matches = NULL_TREE;
5824 /* By the time we get here, we should be seeing only real
5825 pointer-to-member types, not the internal POINTER_TYPE to
5826 METHOD_TYPE representation. */
5827 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5828 && (TREE_CODE (TREE_TYPE (target_type))
5829 == METHOD_TYPE)), 0);
5831 if (TREE_CODE (overload) == COMPONENT_REF)
5832 overload = TREE_OPERAND (overload, 1);
5834 /* Check that the TARGET_TYPE is reasonable. */
5835 if (TYPE_PTRFN_P (target_type))
5837 else if (TYPE_PTRMEMFUNC_P (target_type))
5838 /* This is OK, too. */
5840 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5842 /* This is OK, too. This comes from a conversion to reference
5844 target_type = build_reference_type (target_type);
5851 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5852 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5853 return error_mark_node;
5856 /* If we can find a non-template function that matches, we can just
5857 use it. There's no point in generating template instantiations
5858 if we're just going to throw them out anyhow. But, of course, we
5859 can only do this when we don't *need* a template function. */
5864 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5866 tree fn = OVL_FUNCTION (fns);
5869 if (TREE_CODE (fn) == TEMPLATE_DECL)
5870 /* We're not looking for templates just yet. */
5873 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5875 /* We're looking for a non-static member, and this isn't
5876 one, or vice versa. */
5879 /* See if there's a match. */
5880 fntype = TREE_TYPE (fn);
5882 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5883 else if (!is_reference)
5884 fntype = build_pointer_type (fntype);
5886 if (can_convert_arg (target_type, fntype, fn))
5887 matches = tree_cons (fn, NULL_TREE, matches);
5891 /* Now, if we've already got a match (or matches), there's no need
5892 to proceed to the template functions. But, if we don't have a
5893 match we need to look at them, too. */
5896 tree target_fn_type;
5897 tree target_arg_types;
5898 tree target_ret_type;
5903 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5905 target_fn_type = TREE_TYPE (target_type);
5906 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5907 target_ret_type = TREE_TYPE (target_fn_type);
5909 /* Never do unification on the 'this' parameter. */
5910 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5911 target_arg_types = TREE_CHAIN (target_arg_types);
5913 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5915 tree fn = OVL_FUNCTION (fns);
5917 tree instantiation_type;
5920 if (TREE_CODE (fn) != TEMPLATE_DECL)
5921 /* We're only looking for templates. */
5924 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5926 /* We're not looking for a non-static member, and this is
5927 one, or vice versa. */
5930 /* Try to do argument deduction. */
5931 targs = make_tree_vec (DECL_NTPARMS (fn));
5932 if (fn_type_unification (fn, explicit_targs, targs,
5933 target_arg_types, target_ret_type,
5934 DEDUCE_EXACT, -1) != 0)
5935 /* Argument deduction failed. */
5938 /* Instantiate the template. */
5939 instantiation = instantiate_template (fn, targs);
5940 if (instantiation == error_mark_node)
5941 /* Instantiation failed. */
5944 /* See if there's a match. */
5945 instantiation_type = TREE_TYPE (instantiation);
5947 instantiation_type =
5948 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5949 else if (!is_reference)
5950 instantiation_type = build_pointer_type (instantiation_type);
5951 if (can_convert_arg (target_type, instantiation_type, instantiation))
5952 matches = tree_cons (instantiation, fn, matches);
5955 /* Now, remove all but the most specialized of the matches. */
5958 tree match = most_specialized_instantiation (matches);
5960 if (match != error_mark_node)
5961 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5965 /* Now we should have exactly one function in MATCHES. */
5966 if (matches == NULL_TREE)
5968 /* There were *no* matches. */
5971 error ("no matches converting function `%D' to type `%#T'",
5972 DECL_NAME (OVL_FUNCTION (overload)),
5975 /* print_candidates expects a chain with the functions in
5976 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5977 so why be clever?). */
5978 for (; overload; overload = OVL_NEXT (overload))
5979 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
5982 print_candidates (matches);
5984 return error_mark_node;
5986 else if (TREE_CHAIN (matches))
5988 /* There were too many matches. */
5994 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
5995 DECL_NAME (OVL_FUNCTION (overload)),
5998 /* Since print_candidates expects the functions in the
5999 TREE_VALUE slot, we flip them here. */
6000 for (match = matches; match; match = TREE_CHAIN (match))
6001 TREE_VALUE (match) = TREE_PURPOSE (match);
6003 print_candidates (matches);
6006 return error_mark_node;
6009 /* Good, exactly one match. Now, convert it to the correct type. */
6010 fn = TREE_PURPOSE (matches);
6012 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6013 && !ptrmem && !flag_ms_extensions)
6015 static int explained;
6018 return error_mark_node;
6020 pedwarn ("assuming pointer to member `%D'", fn);
6023 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6029 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6030 return build_unary_op (ADDR_EXPR, fn, 0);
6033 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6034 will mark the function as addressed, but here we must do it
6036 cxx_mark_addressable (fn);
6042 /* This function will instantiate the type of the expression given in
6043 RHS to match the type of LHSTYPE. If errors exist, then return
6044 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6045 we complain on errors. If we are not complaining, never modify rhs,
6046 as overload resolution wants to try many possible instantiations, in
6047 the hope that at least one will work.
6049 For non-recursive calls, LHSTYPE should be a function, pointer to
6050 function, or a pointer to member function. */
6053 instantiate_type (lhstype, rhs, flags)
6055 tsubst_flags_t flags;
6057 int complain = (flags & tf_error);
6058 int strict = (flags & tf_no_attributes)
6059 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6060 int allow_ptrmem = flags & tf_ptrmem_ok;
6062 flags &= ~tf_ptrmem_ok;
6064 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6067 error ("not enough type information");
6068 return error_mark_node;
6071 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6073 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6076 error ("argument of type `%T' does not match `%T'",
6077 TREE_TYPE (rhs), lhstype);
6078 return error_mark_node;
6081 if (TREE_CODE (rhs) == BASELINK)
6082 rhs = BASELINK_FUNCTIONS (rhs);
6084 /* We don't overwrite rhs if it is an overloaded function.
6085 Copying it would destroy the tree link. */
6086 if (TREE_CODE (rhs) != OVERLOAD)
6087 rhs = copy_node (rhs);
6089 /* This should really only be used when attempting to distinguish
6090 what sort of a pointer to function we have. For now, any
6091 arithmetic operation which is not supported on pointers
6092 is rejected as an error. */
6094 switch (TREE_CODE (rhs))
6102 return error_mark_node;
6109 new_rhs = instantiate_type (build_pointer_type (lhstype),
6110 TREE_OPERAND (rhs, 0), flags);
6111 if (new_rhs == error_mark_node)
6112 return error_mark_node;
6114 TREE_TYPE (rhs) = lhstype;
6115 TREE_OPERAND (rhs, 0) = new_rhs;
6120 rhs = copy_node (TREE_OPERAND (rhs, 0));
6121 TREE_TYPE (rhs) = unknown_type_node;
6122 return instantiate_type (lhstype, rhs, flags);
6125 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6128 rhs = TREE_OPERAND (rhs, 1);
6129 if (BASELINK_P (rhs))
6130 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6131 flags | allow_ptrmem);
6133 /* This can happen if we are forming a pointer-to-member for a
6135 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6139 case TEMPLATE_ID_EXPR:
6141 tree fns = TREE_OPERAND (rhs, 0);
6142 tree args = TREE_OPERAND (rhs, 1);
6145 resolve_address_of_overloaded_function (lhstype,
6149 /*template_only=*/1,
6155 resolve_address_of_overloaded_function (lhstype,
6159 /*template_only=*/0,
6160 /*explicit_targs=*/NULL_TREE);
6163 /* Now we should have a baselink. */
6164 my_friendly_assert (BASELINK_P (rhs), 990412);
6166 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6169 /* This is too hard for now. */
6171 return error_mark_node;
6176 TREE_OPERAND (rhs, 0)
6177 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6178 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6179 return error_mark_node;
6180 TREE_OPERAND (rhs, 1)
6181 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6182 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6183 return error_mark_node;
6185 TREE_TYPE (rhs) = lhstype;
6189 case TRUNC_DIV_EXPR:
6190 case FLOOR_DIV_EXPR:
6192 case ROUND_DIV_EXPR:
6194 case TRUNC_MOD_EXPR:
6195 case FLOOR_MOD_EXPR:
6197 case ROUND_MOD_EXPR:
6198 case FIX_ROUND_EXPR:
6199 case FIX_FLOOR_EXPR:
6201 case FIX_TRUNC_EXPR:
6217 case PREINCREMENT_EXPR:
6218 case PREDECREMENT_EXPR:
6219 case POSTINCREMENT_EXPR:
6220 case POSTDECREMENT_EXPR:
6222 error ("invalid operation on uninstantiated type");
6223 return error_mark_node;
6225 case TRUTH_AND_EXPR:
6227 case TRUTH_XOR_EXPR:
6234 case TRUTH_ANDIF_EXPR:
6235 case TRUTH_ORIF_EXPR:
6236 case TRUTH_NOT_EXPR:
6238 error ("not enough type information");
6239 return error_mark_node;
6242 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6245 error ("not enough type information");
6246 return error_mark_node;
6248 TREE_OPERAND (rhs, 1)
6249 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6250 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6251 return error_mark_node;
6252 TREE_OPERAND (rhs, 2)
6253 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6254 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6255 return error_mark_node;
6257 TREE_TYPE (rhs) = lhstype;
6261 TREE_OPERAND (rhs, 1)
6262 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6263 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6264 return error_mark_node;
6266 TREE_TYPE (rhs) = lhstype;
6271 if (PTRMEM_OK_P (rhs))
6272 flags |= tf_ptrmem_ok;
6274 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6276 case ENTRY_VALUE_EXPR:
6278 return error_mark_node;
6281 return error_mark_node;
6285 return error_mark_node;
6289 /* Return the name of the virtual function pointer field
6290 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6291 this may have to look back through base types to find the
6292 ultimate field name. (For single inheritance, these could
6293 all be the same name. Who knows for multiple inheritance). */
6296 get_vfield_name (type)
6299 tree binfo = TYPE_BINFO (type);
6302 while (BINFO_BASETYPES (binfo)
6303 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6304 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6305 binfo = BINFO_BASETYPE (binfo, 0);
6307 type = BINFO_TYPE (binfo);
6308 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6309 + TYPE_NAME_LENGTH (type) + 2);
6310 sprintf (buf, VFIELD_NAME_FORMAT,
6311 IDENTIFIER_POINTER (constructor_name (type)));
6312 return get_identifier (buf);
6316 print_class_statistics ()
6318 #ifdef GATHER_STATISTICS
6319 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6320 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6321 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6322 n_build_method_call, n_inner_fields_searched);
6325 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6326 n_vtables, n_vtable_searches);
6327 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6328 n_vtable_entries, n_vtable_elems);
6333 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6334 according to [class]:
6335 The class-name is also inserted
6336 into the scope of the class itself. For purposes of access checking,
6337 the inserted class name is treated as if it were a public member name. */
6340 build_self_reference ()
6342 tree name = constructor_name (current_class_type);
6343 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6346 DECL_NONLOCAL (value) = 1;
6347 DECL_CONTEXT (value) = current_class_type;
6348 DECL_ARTIFICIAL (value) = 1;
6350 if (processing_template_decl)
6351 value = push_template_decl (value);
6353 saved_cas = current_access_specifier;
6354 current_access_specifier = access_public_node;
6355 finish_member_declaration (value);
6356 current_access_specifier = saved_cas;
6359 /* Returns 1 if TYPE contains only padding bytes. */
6362 is_empty_class (type)
6365 if (type == error_mark_node)
6368 if (! IS_AGGR_TYPE (type))
6371 return integer_zerop (CLASSTYPE_SIZE (type));
6374 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6375 a *_TYPE node. NODE can also be a local class. */
6378 get_enclosing_class (type)
6383 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6385 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6388 node = DECL_CONTEXT (node);
6394 node = TYPE_CONTEXT (node);
6404 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6407 is_base_of_enclosing_class (base, type)
6412 if (lookup_base (type, base, ba_any, NULL))
6415 type = get_enclosing_class (type);
6420 /* Note that NAME was looked up while the current class was being
6421 defined and that the result of that lookup was DECL. */
6424 maybe_note_name_used_in_class (name, decl)
6428 splay_tree names_used;
6430 /* If we're not defining a class, there's nothing to do. */
6431 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6434 /* If there's already a binding for this NAME, then we don't have
6435 anything to worry about. */
6436 if (IDENTIFIER_CLASS_VALUE (name))
6439 if (!current_class_stack[current_class_depth - 1].names_used)
6440 current_class_stack[current_class_depth - 1].names_used
6441 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6442 names_used = current_class_stack[current_class_depth - 1].names_used;
6444 splay_tree_insert (names_used,
6445 (splay_tree_key) name,
6446 (splay_tree_value) decl);
6449 /* Note that NAME was declared (as DECL) in the current class. Check
6450 to see that the declaration is valid. */
6453 note_name_declared_in_class (name, decl)
6457 splay_tree names_used;
6460 /* Look to see if we ever used this name. */
6462 = current_class_stack[current_class_depth - 1].names_used;
6466 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6469 /* [basic.scope.class]
6471 A name N used in a class S shall refer to the same declaration
6472 in its context and when re-evaluated in the completed scope of
6474 error ("declaration of `%#D'", decl);
6475 cp_error_at ("changes meaning of `%D' from `%+#D'",
6476 DECL_NAME (OVL_CURRENT (decl)),
6481 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6482 Secondary vtables are merged with primary vtables; this function
6483 will return the VAR_DECL for the primary vtable. */
6486 get_vtbl_decl_for_binfo (binfo)
6491 decl = BINFO_VTABLE (binfo);
6492 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6494 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6496 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6499 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6503 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6504 who's TREE_PURPOSE is the TYPE of the required primary base and
6505 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6508 dfs_get_primary_binfo (binfo, data)
6512 tree cons = (tree) data;
6513 tree primary_base = TREE_PURPOSE (cons);
6515 if (TREE_VIA_VIRTUAL (binfo)
6516 && same_type_p (BINFO_TYPE (binfo), primary_base))
6517 /* This is the right type of binfo, but it might be an unshared
6518 instance, and the shared instance is later in the dfs walk. We
6519 must keep looking. */
6520 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6525 /* Returns the unshared binfo for the primary base of BINFO. Note
6526 that in a complex hierarchy the resulting BINFO may not actually
6527 *be* primary. In particular if the resulting BINFO is a virtual
6528 base, and it occurs elsewhere in the hierarchy, then this
6529 occurrence may not actually be a primary base in the complete
6530 object. Check BINFO_PRIMARY_P to be sure. */
6533 get_primary_binfo (binfo)
6537 tree result = NULL_TREE;
6540 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6544 /* A non-virtual primary base is always a direct base, and easy to
6546 if (!TREE_VIA_VIRTUAL (primary_base))
6550 /* Scan the direct basetypes until we find a base with the same
6551 type as the primary base. */
6552 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6554 tree base_binfo = BINFO_BASETYPE (binfo, i);
6556 if (same_type_p (BINFO_TYPE (base_binfo),
6557 BINFO_TYPE (primary_base)))
6561 /* We should always find the primary base. */
6565 /* For a primary virtual base, we have to scan the entire hierarchy
6566 rooted at BINFO; the virtual base could be an indirect virtual
6567 base. There could be more than one instance of the primary base
6568 in the hierarchy, and if one is the canonical binfo we want that
6569 one. If it exists, it should be the first one we find, but as a
6570 consistency check we find them all and make sure. */
6571 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6572 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6573 virtuals = TREE_VALUE (virtuals);
6575 /* We must have found at least one instance. */
6576 my_friendly_assert (virtuals, 20010612);
6578 if (TREE_CHAIN (virtuals))
6580 /* We found more than one instance of the base. We must make
6581 sure that, if one is the canonical one, it is the first one
6582 we found. As the chain is in reverse dfs order, that means
6583 the last on the list. */
6584 tree complete_binfo;
6587 for (complete_binfo = binfo;
6588 BINFO_INHERITANCE_CHAIN (complete_binfo);
6589 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6591 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6592 BINFO_TYPE (complete_binfo));
6594 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6596 result = TREE_VALUE (virtuals);
6598 if (canonical == result)
6600 /* This is the unshared instance. Make sure it was the
6602 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6608 result = TREE_VALUE (virtuals);
6612 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6615 maybe_indent_hierarchy (stream, indent, indented_p)
6621 fprintf (stream, "%*s", indent, "");
6625 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6626 dominated by T) to stderr. INDENT should be zero when called from
6627 the top level; it is incremented recursively. */
6630 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6640 indented = maybe_indent_hierarchy (stream, indent, 0);
6641 fprintf (stream, "%s (0x%lx) ",
6642 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6643 (unsigned long) binfo);
6644 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6645 tree_low_cst (BINFO_OFFSET (binfo), 0));
6646 if (is_empty_class (BINFO_TYPE (binfo)))
6647 fprintf (stream, " empty");
6648 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6649 fprintf (stream, " nearly-empty");
6650 if (TREE_VIA_VIRTUAL (binfo))
6652 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6654 fprintf (stream, " virtual");
6655 if (canonical == binfo)
6656 fprintf (stream, " canonical");
6658 fprintf (stream, " non-canonical");
6660 fprintf (stream, "\n");
6663 if (BINFO_PRIMARY_BASE_OF (binfo))
6665 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6666 fprintf (stream, " primary-for %s (0x%lx)",
6667 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6668 TFF_PLAIN_IDENTIFIER),
6669 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6671 if (BINFO_LOST_PRIMARY_P (binfo))
6673 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6674 fprintf (stream, " lost-primary");
6677 fprintf (stream, "\n");
6679 if (!(flags & TDF_SLIM))
6683 if (BINFO_SUBVTT_INDEX (binfo))
6685 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6686 fprintf (stream, " subvttidx=%s",
6687 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6688 TFF_PLAIN_IDENTIFIER));
6690 if (BINFO_VPTR_INDEX (binfo))
6692 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6693 fprintf (stream, " vptridx=%s",
6694 expr_as_string (BINFO_VPTR_INDEX (binfo),
6695 TFF_PLAIN_IDENTIFIER));
6697 if (BINFO_VPTR_FIELD (binfo))
6699 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6700 fprintf (stream, " vbaseoffset=%s",
6701 expr_as_string (BINFO_VPTR_FIELD (binfo),
6702 TFF_PLAIN_IDENTIFIER));
6704 if (BINFO_VTABLE (binfo))
6706 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6707 fprintf (stream, " vptr=%s",
6708 expr_as_string (BINFO_VTABLE (binfo),
6709 TFF_PLAIN_IDENTIFIER));
6713 fprintf (stream, "\n");
6717 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6718 dump_class_hierarchy_r (stream, flags,
6719 t, BINFO_BASETYPE (binfo, i),
6723 /* Dump the BINFO hierarchy for T. */
6726 dump_class_hierarchy (t)
6730 FILE *stream = dump_begin (TDI_class, &flags);
6735 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6736 fprintf (stream, " size=%lu align=%lu\n",
6737 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6738 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6739 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6740 fprintf (stream, "\n");
6741 dump_end (TDI_class, stream);
6745 dump_array (stream, decl)
6752 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6754 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6756 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6757 fprintf (stream, " %s entries",
6758 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6759 TFF_PLAIN_IDENTIFIER));
6760 fprintf (stream, "\n");
6762 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6763 inits; ix++, inits = TREE_CHAIN (inits))
6764 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6765 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6769 dump_vtable (t, binfo, vtable)
6775 FILE *stream = dump_begin (TDI_class, &flags);
6780 if (!(flags & TDF_SLIM))
6782 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6784 fprintf (stream, "%s for %s",
6785 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6786 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6789 if (!TREE_VIA_VIRTUAL (binfo))
6790 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6791 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6793 fprintf (stream, "\n");
6794 dump_array (stream, vtable);
6795 fprintf (stream, "\n");
6798 dump_end (TDI_class, stream);
6807 FILE *stream = dump_begin (TDI_class, &flags);
6812 if (!(flags & TDF_SLIM))
6814 fprintf (stream, "VTT for %s\n",
6815 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6816 dump_array (stream, vtt);
6817 fprintf (stream, "\n");
6820 dump_end (TDI_class, stream);
6823 /* Virtual function table initialization. */
6825 /* Create all the necessary vtables for T and its base classes. */
6835 /* We lay out the primary and secondary vtables in one contiguous
6836 vtable. The primary vtable is first, followed by the non-virtual
6837 secondary vtables in inheritance graph order. */
6838 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6839 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6840 TYPE_BINFO (t), t, list);
6842 /* Then come the virtual bases, also in inheritance graph order. */
6843 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6847 if (!TREE_VIA_VIRTUAL (vbase))
6850 /* Although we walk in inheritance order, that might not get the
6852 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6854 accumulate_vtbl_inits (real_base, real_base,
6855 TYPE_BINFO (t), t, list);
6858 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
6859 base classes, for the benefit of the debugging backends. */
6860 for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
6862 tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
6863 if (TREE_VIA_VIRTUAL (base))
6865 vbase = binfo_for_vbase (BINFO_TYPE (base), t);
6866 BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
6870 if (TYPE_BINFO_VTABLE (t))
6871 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6874 /* Initialize the vtable for BINFO with the INITS. */
6877 initialize_vtable (binfo, inits)
6883 layout_vtable_decl (binfo, list_length (inits));
6884 decl = get_vtbl_decl_for_binfo (binfo);
6885 initialize_array (decl, inits);
6886 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6889 /* Initialize DECL (a declaration for a namespace-scope array) with
6893 initialize_array (decl, inits)
6899 context = DECL_CONTEXT (decl);
6900 DECL_CONTEXT (decl) = NULL_TREE;
6901 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6902 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6903 DECL_CONTEXT (decl) = context;
6906 /* Build the VTT (virtual table table) for T.
6907 A class requires a VTT if it has virtual bases.
6910 1 - primary virtual pointer for complete object T
6911 2 - secondary VTTs for each direct non-virtual base of T which requires a
6913 3 - secondary virtual pointers for each direct or indirect base of T which
6914 has virtual bases or is reachable via a virtual path from T.
6915 4 - secondary VTTs for each direct or indirect virtual base of T.
6917 Secondary VTTs look like complete object VTTs without part 4. */
6928 /* Build up the initializers for the VTT. */
6930 index = size_zero_node;
6931 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6933 /* If we didn't need a VTT, we're done. */
6937 /* Figure out the type of the VTT. */
6938 type = build_index_type (size_int (list_length (inits) - 1));
6939 type = build_cplus_array_type (const_ptr_type_node, type);
6941 /* Now, build the VTT object itself. */
6942 vtt = build_vtable (t, get_vtt_name (t), type);
6943 pushdecl_top_level (vtt);
6944 initialize_array (vtt, inits);
6949 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6950 from within some hierarchy which is inherited from the type of BINFO.
6951 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6955 get_original_base (base_binfo, binfo)
6962 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6964 if (TREE_VIA_VIRTUAL (base_binfo))
6965 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6966 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6968 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
6969 if (same_type_p (BINFO_TYPE (base_binfo),
6970 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
6971 return BINFO_BASETYPE (derived, ix);
6976 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6977 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6978 and CHAIN the vtable pointer for this binfo after construction is
6979 complete. VALUE can also be another BINFO, in which case we recurse. */
6982 binfo_ctor_vtable (binfo)
6989 vt = BINFO_VTABLE (binfo);
6990 if (TREE_CODE (vt) == TREE_LIST)
6991 vt = TREE_VALUE (vt);
6992 if (TREE_CODE (vt) == TREE_VEC)
7001 /* Recursively build the VTT-initializer for BINFO (which is in the
7002 hierarchy dominated by T). INITS points to the end of the initializer
7003 list to date. INDEX is the VTT index where the next element will be
7004 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7005 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7006 for virtual bases of T. When it is not so, we build the constructor
7007 vtables for the BINFO-in-T variant. */
7010 build_vtt_inits (binfo, t, inits, index)
7019 tree secondary_vptrs;
7020 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7022 /* We only need VTTs for subobjects with virtual bases. */
7023 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7026 /* We need to use a construction vtable if this is not the primary
7030 build_ctor_vtbl_group (binfo, t);
7032 /* Record the offset in the VTT where this sub-VTT can be found. */
7033 BINFO_SUBVTT_INDEX (binfo) = *index;
7036 /* Add the address of the primary vtable for the complete object. */
7037 init = binfo_ctor_vtable (binfo);
7038 *inits = build_tree_list (NULL_TREE, init);
7039 inits = &TREE_CHAIN (*inits);
7042 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7043 BINFO_VPTR_INDEX (binfo) = *index;
7045 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7047 /* Recursively add the secondary VTTs for non-virtual bases. */
7048 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7050 b = BINFO_BASETYPE (binfo, i);
7051 if (!TREE_VIA_VIRTUAL (b))
7052 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7056 /* Add secondary virtual pointers for all subobjects of BINFO with
7057 either virtual bases or reachable along a virtual path, except
7058 subobjects that are non-virtual primary bases. */
7059 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7060 TREE_TYPE (secondary_vptrs) = *index;
7061 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7062 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7064 dfs_walk_real (binfo,
7065 dfs_build_secondary_vptr_vtt_inits,
7067 dfs_ctor_vtable_bases_queue_p,
7069 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7070 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7073 *index = TREE_TYPE (secondary_vptrs);
7075 /* The secondary vptrs come back in reverse order. After we reverse
7076 them, and add the INITS, the last init will be the first element
7078 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7079 if (secondary_vptrs)
7081 *inits = nreverse (secondary_vptrs);
7082 inits = &TREE_CHAIN (secondary_vptrs);
7083 my_friendly_assert (*inits == NULL_TREE, 20000517);
7086 /* Add the secondary VTTs for virtual bases. */
7088 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7092 if (!TREE_VIA_VIRTUAL (b))
7095 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7096 inits = build_vtt_inits (vbase, t, inits, index);
7101 tree data = tree_cons (t, binfo, NULL_TREE);
7102 VTT_TOP_LEVEL_P (data) = 0;
7103 VTT_MARKED_BINFO_P (data) = 0;
7105 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7106 dfs_ctor_vtable_bases_queue_p,
7113 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7114 for the base in most derived. DATA is a TREE_LIST who's
7115 TREE_CHAIN is the type of the base being
7116 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7117 flag of DATA indicates that this is a constructor vtable. The
7118 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7121 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7133 top_level_p = VTT_TOP_LEVEL_P (l);
7135 SET_BINFO_MARKED (binfo);
7137 /* We don't care about bases that don't have vtables. */
7138 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7141 /* We're only interested in proper subobjects of T. */
7142 if (same_type_p (BINFO_TYPE (binfo), t))
7145 /* We're not interested in non-virtual primary bases. */
7146 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7149 /* If BINFO has virtual bases or is reachable via a virtual path
7150 from T, it'll have a secondary vptr. */
7151 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7152 && !binfo_via_virtual (binfo, t))
7155 /* Record the index where this secondary vptr can be found. */
7156 index = TREE_TYPE (l);
7159 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7160 BINFO_VPTR_INDEX (binfo) = index;
7162 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7163 TYPE_SIZE_UNIT (ptr_type_node));
7165 /* Add the initializer for the secondary vptr itself. */
7166 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7168 /* It's a primary virtual base, and this is not the construction
7169 vtable. Find the base this is primary of in the inheritance graph,
7170 and use that base's vtable now. */
7171 while (BINFO_PRIMARY_BASE_OF (binfo))
7172 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7174 init = binfo_ctor_vtable (binfo);
7175 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7180 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7181 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7182 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7186 dfs_ctor_vtable_bases_queue_p (binfo, data)
7190 if (TREE_VIA_VIRTUAL (binfo))
7191 /* Get the shared version. */
7192 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7194 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7199 /* Called from build_vtt_inits via dfs_walk. After building constructor
7200 vtables and generating the sub-vtt from them, we need to restore the
7201 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7202 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7205 dfs_fixup_binfo_vtbls (binfo, data)
7209 CLEAR_BINFO_MARKED (binfo);
7211 /* We don't care about bases that don't have vtables. */
7212 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7215 /* If we scribbled the construction vtable vptr into BINFO, clear it
7217 if (BINFO_VTABLE (binfo)
7218 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7219 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7220 == TREE_VALUE ((tree) data)))
7221 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7226 /* Build the construction vtable group for BINFO which is in the
7227 hierarchy dominated by T. */
7230 build_ctor_vtbl_group (binfo, t)
7241 /* See if we've already created this construction vtable group. */
7242 id = mangle_ctor_vtbl_for_type (t, binfo);
7243 if (IDENTIFIER_GLOBAL_VALUE (id))
7246 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7247 /* Build a version of VTBL (with the wrong type) for use in
7248 constructing the addresses of secondary vtables in the
7249 construction vtable group. */
7250 vtbl = build_vtable (t, id, ptr_type_node);
7251 list = build_tree_list (vtbl, NULL_TREE);
7252 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7255 /* Add the vtables for each of our virtual bases using the vbase in T
7257 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7259 vbase = TREE_CHAIN (vbase))
7264 if (!TREE_VIA_VIRTUAL (vbase))
7266 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7267 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7269 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7271 inits = TREE_VALUE (list);
7273 /* Figure out the type of the construction vtable. */
7274 type = build_index_type (size_int (list_length (inits) - 1));
7275 type = build_cplus_array_type (vtable_entry_type, type);
7276 TREE_TYPE (vtbl) = type;
7278 /* Initialize the construction vtable. */
7279 pushdecl_top_level (vtbl);
7280 initialize_array (vtbl, inits);
7281 dump_vtable (t, binfo, vtbl);
7284 /* Add the vtbl initializers for BINFO (and its bases other than
7285 non-virtual primaries) to the list of INITS. BINFO is in the
7286 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7287 the constructor the vtbl inits should be accumulated for. (If this
7288 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7289 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7290 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7291 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7292 but are not necessarily the same in terms of layout. */
7295 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7303 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7305 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7306 BINFO_TYPE (orig_binfo)),
7309 /* If it doesn't have a vptr, we don't do anything. */
7310 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7313 /* If we're building a construction vtable, we're not interested in
7314 subobjects that don't require construction vtables. */
7316 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7317 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7320 /* Build the initializers for the BINFO-in-T vtable. */
7322 = chainon (TREE_VALUE (inits),
7323 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7324 rtti_binfo, t, inits));
7326 /* Walk the BINFO and its bases. We walk in preorder so that as we
7327 initialize each vtable we can figure out at what offset the
7328 secondary vtable lies from the primary vtable. We can't use
7329 dfs_walk here because we need to iterate through bases of BINFO
7330 and RTTI_BINFO simultaneously. */
7331 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7333 tree base_binfo = BINFO_BASETYPE (binfo, i);
7335 /* Skip virtual bases. */
7336 if (TREE_VIA_VIRTUAL (base_binfo))
7338 accumulate_vtbl_inits (base_binfo,
7339 BINFO_BASETYPE (orig_binfo, i),
7345 /* Called from accumulate_vtbl_inits. Returns the initializers for
7346 the BINFO vtable. */
7349 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7356 tree inits = NULL_TREE;
7357 tree vtbl = NULL_TREE;
7358 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7361 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7363 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7364 primary virtual base. If it is not the same primary in
7365 the hierarchy of T, we'll need to generate a ctor vtable
7366 for it, to place at its location in T. If it is the same
7367 primary, we still need a VTT entry for the vtable, but it
7368 should point to the ctor vtable for the base it is a
7369 primary for within the sub-hierarchy of RTTI_BINFO.
7371 There are three possible cases:
7373 1) We are in the same place.
7374 2) We are a primary base within a lost primary virtual base of
7376 3) We are primary to something not a base of RTTI_BINFO. */
7378 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7379 tree last = NULL_TREE;
7381 /* First, look through the bases we are primary to for RTTI_BINFO
7382 or a virtual base. */
7383 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7386 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7389 /* If we run out of primary links, keep looking down our
7390 inheritance chain; we might be an indirect primary. */
7392 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7393 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7396 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7397 base B and it is a base of RTTI_BINFO, this is case 2. In
7398 either case, we share our vtable with LAST, i.e. the
7399 derived-most base within B of which we are a primary. */
7401 || (b && binfo_for_vbase (BINFO_TYPE (b),
7402 BINFO_TYPE (rtti_binfo))))
7403 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7404 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7405 binfo_ctor_vtable after everything's been set up. */
7408 /* Otherwise, this is case 3 and we get our own. */
7410 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7418 /* Compute the initializer for this vtable. */
7419 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7422 /* Figure out the position to which the VPTR should point. */
7423 vtbl = TREE_PURPOSE (l);
7424 vtbl = build1 (ADDR_EXPR,
7427 TREE_CONSTANT (vtbl) = 1;
7428 index = size_binop (PLUS_EXPR,
7429 size_int (non_fn_entries),
7430 size_int (list_length (TREE_VALUE (l))));
7431 index = size_binop (MULT_EXPR,
7432 TYPE_SIZE_UNIT (vtable_entry_type),
7434 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7435 TREE_CONSTANT (vtbl) = 1;
7439 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7440 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7441 straighten this out. */
7442 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7443 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7446 /* For an ordinary vtable, set BINFO_VTABLE. */
7447 BINFO_VTABLE (binfo) = vtbl;
7452 /* Construct the initializer for BINFO's virtual function table. BINFO
7453 is part of the hierarchy dominated by T. If we're building a
7454 construction vtable, the ORIG_BINFO is the binfo we should use to
7455 find the actual function pointers to put in the vtable - but they
7456 can be overridden on the path to most-derived in the graph that
7457 ORIG_BINFO belongs. Otherwise,
7458 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7459 BINFO that should be indicated by the RTTI information in the
7460 vtable; it will be a base class of T, rather than T itself, if we
7461 are building a construction vtable.
7463 The value returned is a TREE_LIST suitable for wrapping in a
7464 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7465 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7466 number of non-function entries in the vtable.
7468 It might seem that this function should never be called with a
7469 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7470 base is always subsumed by a derived class vtable. However, when
7471 we are building construction vtables, we do build vtables for
7472 primary bases; we need these while the primary base is being
7476 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7481 int *non_fn_entries_p;
7488 /* Initialize VID. */
7489 memset (&vid, 0, sizeof (vid));
7492 vid.rtti_binfo = rtti_binfo;
7493 vid.last_init = &vid.inits;
7494 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7495 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7496 /* The first vbase or vcall offset is at index -3 in the vtable. */
7497 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7499 /* Add entries to the vtable for RTTI. */
7500 build_rtti_vtbl_entries (binfo, &vid);
7502 /* Create an array for keeping track of the functions we've
7503 processed. When we see multiple functions with the same
7504 signature, we share the vcall offsets. */
7505 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7506 /* Add the vcall and vbase offset entries. */
7507 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7508 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7509 build_vbase_offset_vtbl_entries. */
7510 for (vbase = CLASSTYPE_VBASECLASSES (t);
7512 vbase = TREE_CHAIN (vbase))
7513 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7515 /* If the target requires padding between data entries, add that now. */
7516 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7520 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7525 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7526 add = tree_cons (NULL_TREE, null_pointer_node, add);
7531 if (non_fn_entries_p)
7532 *non_fn_entries_p = list_length (vid.inits);
7534 /* Go through all the ordinary virtual functions, building up
7536 vfun_inits = NULL_TREE;
7537 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7543 tree init = NULL_TREE;
7547 /* If the only definition of this function signature along our
7548 primary base chain is from a lost primary, this vtable slot will
7549 never be used, so just zero it out. This is important to avoid
7550 requiring extra thunks which cannot be generated with the function.
7552 We first check this in update_vtable_entry_for_fn, so we handle
7553 restored primary bases properly; we also need to do it here so we
7554 zero out unused slots in ctor vtables, rather than filling themff
7555 with erroneous values (though harmless, apart from relocation
7557 for (b = binfo; ; b = get_primary_binfo (b))
7559 /* We found a defn before a lost primary; go ahead as normal. */
7560 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7563 /* The nearest definition is from a lost primary; clear the
7565 if (BINFO_LOST_PRIMARY_P (b))
7567 init = size_zero_node;
7574 /* Pull the offset for `this', and the function to call, out of
7576 delta = BV_DELTA (v);
7578 if (BV_USE_VCALL_INDEX_P (v))
7580 vcall_index = BV_VCALL_INDEX (v);
7581 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7584 vcall_index = NULL_TREE;
7586 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7587 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7589 /* You can't call an abstract virtual function; it's abstract.
7590 So, we replace these functions with __pure_virtual. */
7591 if (DECL_PURE_VIRTUAL_P (fn))
7594 /* Take the address of the function, considering it to be of an
7595 appropriate generic type. */
7596 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7597 /* The address of a function can't change. */
7598 TREE_CONSTANT (pfn) = 1;
7600 /* Enter it in the vtable. */
7601 init = build_vtable_entry (delta, vcall_index, pfn);
7604 /* And add it to the chain of initializers. */
7605 if (TARGET_VTABLE_USES_DESCRIPTORS)
7608 if (init == size_zero_node)
7609 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7610 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7612 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7614 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7615 TREE_OPERAND (init, 0),
7616 build_int_2 (i, 0));
7617 TREE_CONSTANT (fdesc) = 1;
7619 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7623 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7626 /* The initializers for virtual functions were built up in reverse
7627 order; straighten them out now. */
7628 vfun_inits = nreverse (vfun_inits);
7630 /* The negative offset initializers are also in reverse order. */
7631 vid.inits = nreverse (vid.inits);
7633 /* Chain the two together. */
7634 return chainon (vid.inits, vfun_inits);
7637 /* Adds to vid->inits the initializers for the vbase and vcall
7638 offsets in BINFO, which is in the hierarchy dominated by T. */
7641 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7643 vtbl_init_data *vid;
7647 /* If this is a derived class, we must first create entries
7648 corresponding to the primary base class. */
7649 b = get_primary_binfo (binfo);
7651 build_vcall_and_vbase_vtbl_entries (b, vid);
7653 /* Add the vbase entries for this base. */
7654 build_vbase_offset_vtbl_entries (binfo, vid);
7655 /* Add the vcall entries for this base. */
7656 build_vcall_offset_vtbl_entries (binfo, vid);
7659 /* Returns the initializers for the vbase offset entries in the vtable
7660 for BINFO (which is part of the class hierarchy dominated by T), in
7661 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7662 where the next vbase offset will go. */
7665 build_vbase_offset_vtbl_entries (binfo, vid)
7667 vtbl_init_data *vid;
7671 tree non_primary_binfo;
7673 /* If there are no virtual baseclasses, then there is nothing to
7675 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7680 /* We might be a primary base class. Go up the inheritance hierarchy
7681 until we find the most derived class of which we are a primary base:
7682 it is the offset of that which we need to use. */
7683 non_primary_binfo = binfo;
7684 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7688 /* If we have reached a virtual base, then it must be a primary
7689 base (possibly multi-level) of vid->binfo, or we wouldn't
7690 have called build_vcall_and_vbase_vtbl_entries for it. But it
7691 might be a lost primary, so just skip down to vid->binfo. */
7692 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7694 non_primary_binfo = vid->binfo;
7698 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7699 if (get_primary_binfo (b) != non_primary_binfo)
7701 non_primary_binfo = b;
7704 /* Go through the virtual bases, adding the offsets. */
7705 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7707 vbase = TREE_CHAIN (vbase))
7712 if (!TREE_VIA_VIRTUAL (vbase))
7715 /* Find the instance of this virtual base in the complete
7717 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7719 /* If we've already got an offset for this virtual base, we
7720 don't need another one. */
7721 if (BINFO_VTABLE_PATH_MARKED (b))
7723 SET_BINFO_VTABLE_PATH_MARKED (b);
7725 /* Figure out where we can find this vbase offset. */
7726 delta = size_binop (MULT_EXPR,
7729 TYPE_SIZE_UNIT (vtable_entry_type)));
7730 if (vid->primary_vtbl_p)
7731 BINFO_VPTR_FIELD (b) = delta;
7733 if (binfo != TYPE_BINFO (t))
7737 /* Find the instance of this virtual base in the type of BINFO. */
7738 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7739 BINFO_TYPE (binfo));
7741 /* The vbase offset had better be the same. */
7742 if (!tree_int_cst_equal (delta,
7743 BINFO_VPTR_FIELD (orig_vbase)))
7747 /* The next vbase will come at a more negative offset. */
7748 vid->index = size_binop (MINUS_EXPR, vid->index,
7749 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7751 /* The initializer is the delta from BINFO to this virtual base.
7752 The vbase offsets go in reverse inheritance-graph order, and
7753 we are walking in inheritance graph order so these end up in
7755 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7758 = build_tree_list (NULL_TREE,
7759 fold (build1 (NOP_EXPR,
7762 vid->last_init = &TREE_CHAIN (*vid->last_init);
7766 /* Adds the initializers for the vcall offset entries in the vtable
7767 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7771 build_vcall_offset_vtbl_entries (binfo, vid)
7773 vtbl_init_data *vid;
7775 /* We only need these entries if this base is a virtual base. */
7776 if (!TREE_VIA_VIRTUAL (binfo))
7779 /* We need a vcall offset for each of the virtual functions in this
7780 vtable. For example:
7782 class A { virtual void f (); };
7783 class B1 : virtual public A { virtual void f (); };
7784 class B2 : virtual public A { virtual void f (); };
7785 class C: public B1, public B2 { virtual void f (); };
7787 A C object has a primary base of B1, which has a primary base of A. A
7788 C also has a secondary base of B2, which no longer has a primary base
7789 of A. So the B2-in-C construction vtable needs a secondary vtable for
7790 A, which will adjust the A* to a B2* to call f. We have no way of
7791 knowing what (or even whether) this offset will be when we define B2,
7792 so we store this "vcall offset" in the A sub-vtable and look it up in
7793 a "virtual thunk" for B2::f.
7795 We need entries for all the functions in our primary vtable and
7796 in our non-virtual bases' secondary vtables. */
7798 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7799 add_vcall_offset_vtbl_entries_r (binfo, vid);
7802 /* Build vcall offsets, starting with those for BINFO. */
7805 add_vcall_offset_vtbl_entries_r (binfo, vid)
7807 vtbl_init_data *vid;
7812 /* Don't walk into virtual bases -- except, of course, for the
7813 virtual base for which we are building vcall offsets. Any
7814 primary virtual base will have already had its offsets generated
7815 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7816 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7819 /* If BINFO has a primary base, process it first. */
7820 primary_binfo = get_primary_binfo (binfo);
7822 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7824 /* Add BINFO itself to the list. */
7825 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7827 /* Scan the non-primary bases of BINFO. */
7828 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7832 base_binfo = BINFO_BASETYPE (binfo, i);
7833 if (base_binfo != primary_binfo)
7834 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7838 /* Called from build_vcall_offset_vtbl_entries_r. */
7841 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7843 vtbl_init_data* vid;
7845 tree derived_virtuals;
7849 /* If BINFO is a primary base, the most derived class which has BINFO as
7850 a primary base; otherwise, just BINFO. */
7851 tree non_primary_binfo;
7853 binfo_inits = NULL_TREE;
7855 /* We might be a primary base class. Go up the inheritance hierarchy
7856 until we find the most derived class of which we are a primary base:
7857 it is the BINFO_VIRTUALS there that we need to consider. */
7858 non_primary_binfo = binfo;
7859 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7863 /* If we have reached a virtual base, then it must be vid->vbase,
7864 because we ignore other virtual bases in
7865 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7866 base (possibly multi-level) of vid->binfo, or we wouldn't
7867 have called build_vcall_and_vbase_vtbl_entries for it. But it
7868 might be a lost primary, so just skip down to vid->binfo. */
7869 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7871 if (non_primary_binfo != vid->vbase)
7873 non_primary_binfo = vid->binfo;
7877 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7878 if (get_primary_binfo (b) != non_primary_binfo)
7880 non_primary_binfo = b;
7883 if (vid->ctor_vtbl_p)
7884 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7885 where rtti_binfo is the most derived type. */
7886 non_primary_binfo = get_original_base
7887 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7889 /* Make entries for the rest of the virtuals. */
7890 for (base_virtuals = BINFO_VIRTUALS (binfo),
7891 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7892 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7894 base_virtuals = TREE_CHAIN (base_virtuals),
7895 derived_virtuals = TREE_CHAIN (derived_virtuals),
7896 orig_virtuals = TREE_CHAIN (orig_virtuals))
7905 /* Find the declaration that originally caused this function to
7906 be present in BINFO_TYPE (binfo). */
7907 orig_fn = BV_FN (orig_virtuals);
7909 /* When processing BINFO, we only want to generate vcall slots for
7910 function slots introduced in BINFO. So don't try to generate
7911 one if the function isn't even defined in BINFO. */
7912 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7915 /* Find the overriding function. */
7916 fn = BV_FN (derived_virtuals);
7918 /* If there is already an entry for a function with the same
7919 signature as FN, then we do not need a second vcall offset.
7920 Check the list of functions already present in the derived
7922 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7926 derived_entry = VARRAY_TREE (vid->fns, i);
7927 if (same_signature_p (BV_FN (derived_entry), fn)
7928 /* We only use one vcall offset for virtual destructors,
7929 even though there are two virtual table entries. */
7930 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7931 && DECL_DESTRUCTOR_P (fn)))
7933 if (!vid->ctor_vtbl_p)
7934 BV_VCALL_INDEX (derived_virtuals)
7935 = BV_VCALL_INDEX (derived_entry);
7939 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7942 /* The FN comes from BASE. So, we must calculate the adjustment from
7943 vid->vbase to BASE. We can just look for BASE in the complete
7944 object because we are converting from a virtual base, so if there
7945 were multiple copies, there would not be a unique final overrider
7946 and vid->derived would be ill-formed. */
7947 base = DECL_CONTEXT (fn);
7948 base_binfo = lookup_base (vid->derived, base, ba_any, NULL);
7950 /* Compute the vcall offset. */
7951 /* As mentioned above, the vbase we're working on is a primary base of
7952 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7953 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7954 vcall_offset = BINFO_OFFSET (vid->binfo);
7955 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7957 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7960 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7961 vid->last_init = &TREE_CHAIN (*vid->last_init);
7963 /* Keep track of the vtable index where this vcall offset can be
7964 found. For a construction vtable, we already made this
7965 annotation when we built the original vtable. */
7966 if (!vid->ctor_vtbl_p)
7967 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7969 /* The next vcall offset will be found at a more negative
7971 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7973 /* Keep track of this function. */
7974 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7978 /* Return vtbl initializers for the RTTI entries coresponding to the
7979 BINFO's vtable. The RTTI entries should indicate the object given
7980 by VID->rtti_binfo. */
7983 build_rtti_vtbl_entries (binfo, vid)
7985 vtbl_init_data *vid;
7994 basetype = BINFO_TYPE (binfo);
7995 t = BINFO_TYPE (vid->rtti_binfo);
7997 /* To find the complete object, we will first convert to our most
7998 primary base, and then add the offset in the vtbl to that value. */
8000 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8001 && !BINFO_LOST_PRIMARY_P (b))
8005 primary_base = get_primary_binfo (b);
8006 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8009 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8011 /* The second entry is the address of the typeinfo object. */
8013 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8015 decl = integer_zero_node;
8017 /* Convert the declaration to a type that can be stored in the
8019 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8020 TREE_CONSTANT (init) = 1;
8021 *vid->last_init = build_tree_list (NULL_TREE, init);
8022 vid->last_init = &TREE_CHAIN (*vid->last_init);
8024 /* Add the offset-to-top entry. It comes earlier in the vtable that
8025 the the typeinfo entry. Convert the offset to look like a
8026 function pointer, so that we can put it in the vtable. */
8027 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8028 TREE_CONSTANT (init) = 1;
8029 *vid->last_init = build_tree_list (NULL_TREE, init);
8030 vid->last_init = &TREE_CHAIN (*vid->last_init);
8033 /* Build an entry in the virtual function table. DELTA is the offset
8034 for the `this' pointer. VCALL_INDEX is the vtable index containing
8035 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8036 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8037 but it may not actually be a virtual function table pointer. (For
8038 example, it might be the address of the RTTI object, under the new
8042 build_vtable_entry (delta, vcall_index, entry)
8047 tree fn = TREE_OPERAND (entry, 0);
8049 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8050 && fn != abort_fndecl)
8052 entry = make_thunk (entry, delta, vcall_index);
8053 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8054 TREE_READONLY (entry) = 1;
8055 TREE_CONSTANT (entry) = 1;
8057 #ifdef GATHER_STATISTICS
8058 n_vtable_entries += 1;