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 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
41 /* The number of nested classes being processed. If we are not in the
42 scope of any class, this is zero. */
44 int current_class_depth;
46 /* In order to deal with nested classes, we keep a stack of classes.
47 The topmost entry is the innermost class, and is the entry at index
48 CURRENT_CLASS_DEPTH */
50 typedef struct class_stack_node {
51 /* The name of the class. */
54 /* The _TYPE node for the class. */
57 /* The access specifier pending for new declarations in the scope of
61 /* If were defining TYPE, the names used in this class. */
62 splay_tree names_used;
63 }* class_stack_node_t;
65 typedef struct vtbl_init_data_s
67 /* The base for which we're building initializers. */
69 /* The type of the most-derived type. */
71 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
72 unless ctor_vtbl_p is true. */
74 /* The negative-index vtable initializers built up so far. These
75 are in order from least negative index to most negative index. */
77 /* The last (i.e., most negative) entry in INITS. */
79 /* The binfo for the virtual base for which we're building
80 vcall offset initializers. */
82 /* The functions in vbase for which we have already provided vcall
85 /* The vtable index of the next vcall or vbase offset. */
87 /* Nonzero if we are building the initializer for the primary
90 /* Nonzero if we are building the initializer for a construction
95 /* The type of a function passed to walk_subobject_offsets. */
96 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
98 /* The stack itself. This is an dynamically resized array. The
99 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
100 static int current_class_stack_size;
101 static class_stack_node_t current_class_stack;
103 /* An array of all local classes present in this translation unit, in
104 declaration order. */
105 varray_type local_classes;
107 static tree get_vfield_name PARAMS ((tree));
108 static void finish_struct_anon PARAMS ((tree));
109 static tree build_vbase_pointer PARAMS ((tree, tree));
110 static tree build_vtable_entry PARAMS ((tree, tree, tree));
111 static tree get_vtable_name PARAMS ((tree));
112 static tree get_basefndecls PARAMS ((tree, tree));
113 static int build_primary_vtable PARAMS ((tree, tree));
114 static int build_secondary_vtable PARAMS ((tree, tree));
115 static void finish_vtbls PARAMS ((tree));
116 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
117 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
118 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
119 static void delete_duplicate_fields PARAMS ((tree));
120 static void finish_struct_bits PARAMS ((tree));
121 static int alter_access PARAMS ((tree, tree, tree));
122 static void handle_using_decl PARAMS ((tree, tree));
123 static int strictly_overrides PARAMS ((tree, tree));
124 static void mark_overriders PARAMS ((tree, tree));
125 static void check_for_override PARAMS ((tree, tree));
126 static tree dfs_modify_vtables PARAMS ((tree, void *));
127 static tree modify_all_vtables PARAMS ((tree, int *, tree));
128 static void determine_primary_base PARAMS ((tree, int *));
129 static void finish_struct_methods PARAMS ((tree));
130 static void maybe_warn_about_overly_private_class PARAMS ((tree));
131 static int field_decl_cmp PARAMS ((const tree *, const tree *));
132 static int method_name_cmp PARAMS ((const tree *, const tree *));
133 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
134 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
135 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
137 static void build_vtable_entry_ref PARAMS ((tree, tree));
138 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
139 static int count_fields PARAMS ((tree));
140 static int add_fields_to_vec PARAMS ((tree, tree, int));
141 static void check_bitfield_decl PARAMS ((tree));
142 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
143 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
145 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
147 static bool build_base_fields PARAMS ((record_layout_info, int *,
149 static void check_methods PARAMS ((tree));
150 static void remove_zero_width_bit_fields PARAMS ((tree));
151 static void check_bases PARAMS ((tree, int *, int *, int *));
152 static void check_bases_and_members PARAMS ((tree, int *));
153 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
154 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
155 static void fixup_pending_inline PARAMS ((tree));
156 static void fixup_inline_methods PARAMS ((tree));
157 static void set_primary_base PARAMS ((tree, tree, int *));
158 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
159 static void layout_virtual_bases PARAMS ((tree, splay_tree));
160 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
161 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
162 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
163 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
164 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
165 static void layout_vtable_decl PARAMS ((tree, int));
166 static tree dfs_find_final_overrider PARAMS ((tree, void *));
167 static tree find_final_overrider PARAMS ((tree, tree, tree));
168 static int make_new_vtable PARAMS ((tree, tree));
169 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
170 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
171 static void dump_class_hierarchy PARAMS ((tree));
172 static void dump_array PARAMS ((FILE *, tree));
173 static void dump_vtable PARAMS ((tree, tree, tree));
174 static void dump_vtt PARAMS ((tree, tree));
175 static tree build_vtable PARAMS ((tree, tree, tree));
176 static void initialize_vtable PARAMS ((tree, tree));
177 static void initialize_array PARAMS ((tree, tree));
178 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
181 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
182 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
183 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
184 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
186 static void set_vindex PARAMS ((tree, int *));
187 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
188 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
190 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
191 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
192 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
193 static void mark_primary_bases PARAMS ((tree));
194 static tree mark_primary_virtual_base PARAMS ((tree, tree));
195 static void clone_constructors_and_destructors PARAMS ((tree));
196 static tree build_clone PARAMS ((tree, tree));
197 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
198 static tree copy_virtuals PARAMS ((tree));
199 static void build_ctor_vtbl_group PARAMS ((tree, tree));
200 static void build_vtt PARAMS ((tree));
201 static tree binfo_ctor_vtable PARAMS ((tree));
202 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
203 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
204 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
205 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
206 static tree get_original_base PARAMS ((tree, tree));
207 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
208 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
209 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
210 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
211 tree, splay_tree, tree, int));
212 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
213 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
214 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
216 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
217 static bool type_requires_array_cookie PARAMS ((tree));
219 /* Macros for dfs walking during vtt construction. See
220 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
221 and dfs_fixup_binfo_vtbls. */
222 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
223 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
225 /* Variables shared between class.c and call.c. */
227 #ifdef GATHER_STATISTICS
229 int n_vtable_entries = 0;
230 int n_vtable_searches = 0;
231 int n_vtable_elems = 0;
232 int n_convert_harshness = 0;
233 int n_compute_conversion_costs = 0;
234 int n_build_method_call = 0;
235 int n_inner_fields_searched = 0;
238 /* Virtual base class layout. */
240 /* Returns a pointer to the virtual base class of EXP that has the
241 indicated TYPE. EXP is of class type, not a pointer type. */
244 build_vbase_pointer (exp, type)
250 /* Find the shared copy of TYPE; that's where the vtable offset is
252 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
253 /* Find the virtual function table pointer. */
254 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
255 /* Compute the location where the offset will lie. */
256 vbase_ptr = build (PLUS_EXPR,
257 TREE_TYPE (vbase_ptr),
259 BINFO_VPTR_FIELD (vbase));
260 vbase_ptr = build1 (NOP_EXPR,
261 build_pointer_type (ptrdiff_type_node),
263 /* Add the contents of this location to EXP. */
264 return build (PLUS_EXPR,
265 build_pointer_type (type),
266 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
267 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
270 /* Build multi-level access to EXPR using hierarchy path PATH.
271 CODE is PLUS_EXPR if we are going with the grain,
272 and MINUS_EXPR if we are not (in which case, we cannot traverse
273 virtual baseclass links).
275 TYPE is the type we want this path to have on exit.
277 NONNULL is non-zero if we know (for any reason) that EXPR is
278 not, in fact, zero. */
281 build_vbase_path (code, type, expr, path, nonnull)
283 tree type, expr, path;
286 register int changed = 0;
287 tree last = NULL_TREE, last_virtual = NULL_TREE;
289 tree null_expr = 0, nonnull_expr;
291 tree offset = integer_zero_node;
293 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
294 return build1 (NOP_EXPR, type, expr);
296 /* We could do better if we had additional logic to convert back to the
297 unconverted type (the static type of the complete object), and then
298 convert back to the type we want. Until that is done, we only optimize
299 if the complete type is the same type as expr has. */
300 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
301 if (fixed_type_p < 0)
302 /* Virtual base layout is not fixed, even in ctors and dtors. */
305 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
306 expr = save_expr (expr);
309 path = reverse_path (path);
311 basetype = BINFO_TYPE (path);
315 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
317 last_virtual = BINFO_TYPE (TREE_VALUE (path));
318 if (code == PLUS_EXPR)
320 changed = ! fixed_type_p;
326 /* We already check for ambiguous things in the caller, just
330 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
331 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
333 ind = build_indirect_ref (nonnull_expr, NULL);
334 nonnull_expr = build_vbase_pointer (ind, last_virtual);
336 && TREE_CODE (type) == POINTER_TYPE
337 && null_expr == NULL_TREE)
339 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
340 expr = build (COND_EXPR, build_pointer_type (last_virtual),
341 build (EQ_EXPR, boolean_type_node, expr,
343 null_expr, nonnull_expr);
346 /* else we'll figure out the offset below. */
348 /* Happens in the case of parse errors. */
349 if (nonnull_expr == error_mark_node)
350 return error_mark_node;
354 cp_error ("cannot cast up from virtual baseclass `%T'",
356 return error_mark_node;
359 last = TREE_VALUE (path);
360 path = TREE_CHAIN (path);
362 /* LAST is now the last basetype assoc on the path. */
364 /* A pointer to a virtual base member of a non-null object
365 is non-null. Therefore, we only need to test for zeroness once.
366 Make EXPR the canonical expression to deal with here. */
369 TREE_OPERAND (expr, 2) = nonnull_expr;
370 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
371 = TREE_TYPE (nonnull_expr);
376 /* If we go through any virtual base pointers, make sure that
377 casts to BASETYPE from the last virtual base class use
378 the right value for BASETYPE. */
381 tree intype = TREE_TYPE (TREE_TYPE (expr));
383 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
385 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
388 offset = BINFO_OFFSET (last);
390 if (! integer_zerop (offset))
392 /* Bash types to make the backend happy. */
393 offset = cp_convert (type, offset);
395 /* If expr might be 0, we need to preserve that zeroness. */
399 TREE_TYPE (null_expr) = type;
401 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
402 if (TREE_SIDE_EFFECTS (expr))
403 expr = save_expr (expr);
405 return build (COND_EXPR, type,
406 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
408 build (code, type, expr, offset));
410 else return build (code, type, expr, offset);
413 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
414 be used multiple times in initialization of multiple inheritance. */
417 TREE_TYPE (expr) = type;
421 return build1 (NOP_EXPR, type, expr);
425 /* Virtual function things. */
427 /* We want to give the assembler the vtable identifier as well as
428 the offset to the function pointer. So we generate
430 __asm__ __volatile__ (".vtable_entry %c0, %c1"
431 : : "s"(&class_vtable),
432 "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
435 build_vtable_entry_ref (basetype, idx)
438 static char asm_stmt[] = ".vtable_entry %c0, %c1";
440 tree vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
441 tree first_fn = TYPE_BINFO_VTABLE (basetype);
443 s = build_unary_op (ADDR_EXPR, vtable, 0);
444 s = build_tree_list (build_string (1, "s"), s);
446 i = build_array_ref (first_fn, idx);
447 /* We must not convert to ptrdiff_type node here, since this could widen
448 from a partial to an integral node, which would create a
449 convert_expression that would be in the way of any simplifications. */
450 i = build_c_cast (string_type_node, build_unary_op (ADDR_EXPR, i, 0));
451 i2 = build_array_ref (vtable, build_int_2 (0,0));
452 i2 = build_c_cast (string_type_node, build_unary_op (ADDR_EXPR, i2, 0));
453 i = cp_build_binary_op (MINUS_EXPR, i, i2);
454 i = build_tree_list (build_string (1, "i"), i);
456 finish_asm_stmt (ridpointers[RID_VOLATILE],
457 build_string (sizeof(asm_stmt)-1, asm_stmt),
458 NULL_TREE, chainon (s, i), NULL_TREE);
461 /* Given an object INSTANCE, return an expression which yields the
462 virtual function vtable element corresponding to INDEX. There are
463 many special cases for INSTANCE which we take care of here, mainly
464 to avoid creating extra tree nodes when we don't have to. */
467 build_vtbl_ref (instance, idx)
471 tree basetype = TREE_TYPE (instance);
473 if (TREE_CODE (basetype) == REFERENCE_TYPE)
474 basetype = TREE_TYPE (basetype);
476 if (instance == current_class_ref)
477 vtbl = build_vfield_ref (instance, basetype);
482 /* Try to figure out what a reference refers to, and
483 access its virtual function table directly. */
484 tree ref = NULL_TREE;
486 if (TREE_CODE (instance) == INDIRECT_REF
487 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
488 ref = TREE_OPERAND (instance, 0);
489 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
492 if (ref && TREE_CODE (ref) == VAR_DECL
493 && DECL_INITIAL (ref))
495 tree init = DECL_INITIAL (ref);
497 while (TREE_CODE (init) == NOP_EXPR
498 || TREE_CODE (init) == NON_LVALUE_EXPR)
499 init = TREE_OPERAND (init, 0);
500 if (TREE_CODE (init) == ADDR_EXPR)
502 init = TREE_OPERAND (init, 0);
503 if (IS_AGGR_TYPE (TREE_TYPE (init))
504 && (TREE_CODE (init) == PARM_DECL
505 || TREE_CODE (init) == VAR_DECL))
511 if (IS_AGGR_TYPE (TREE_TYPE (instance))
512 && (TREE_CODE (instance) == RESULT_DECL
513 || TREE_CODE (instance) == PARM_DECL
514 || TREE_CODE (instance) == VAR_DECL))
516 vtbl = TYPE_BINFO_VTABLE (basetype);
517 /* Knowing the dynamic type of INSTANCE we can easily obtain
518 the correct vtable entry. We resolve this back to be in
519 terms of the primary vtable. */
520 if (TREE_CODE (vtbl) == PLUS_EXPR)
522 idx = fold (build (PLUS_EXPR,
525 build (EXACT_DIV_EXPR,
527 TREE_OPERAND (vtbl, 1),
528 TYPE_SIZE_UNIT (vtable_entry_type))));
529 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
533 vtbl = build_vfield_ref (instance, basetype);
536 assemble_external (vtbl);
539 build_vtable_entry_ref (basetype, idx);
541 aref = build_array_ref (vtbl, idx);
546 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
547 for the given TYPE. */
550 get_vtable_name (type)
553 return mangle_vtbl_for_type (type);
556 /* Return an IDENTIFIER_NODE for the name of the virtual table table
563 return mangle_vtt_for_type (type);
566 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
567 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
568 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
571 build_vtable (class_type, name, vtable_type)
578 decl = build_lang_decl (VAR_DECL, name, vtable_type);
579 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
580 now to avoid confusion in mangle_decl. */
581 SET_DECL_ASSEMBLER_NAME (decl, name);
582 DECL_CONTEXT (decl) = class_type;
583 DECL_ARTIFICIAL (decl) = 1;
584 TREE_STATIC (decl) = 1;
585 TREE_READONLY (decl) = 1;
586 DECL_VIRTUAL_P (decl) = 1;
587 import_export_vtable (decl, class_type, 0);
592 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
593 or even complete. If this does not exist, create it. If COMPLETE is
594 non-zero, then complete the definition of it -- that will render it
595 impossible to actually build the vtable, but is useful to get at those
596 which are known to exist in the runtime. */
599 get_vtable_decl (type, complete)
603 tree name = get_vtable_name (type);
604 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
608 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
609 && DECL_VIRTUAL_P (decl), 20000118);
613 decl = build_vtable (type, name, void_type_node);
614 decl = pushdecl_top_level (decl);
615 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
618 /* At one time the vtable info was grabbed 2 words at a time. This
619 fails on sparc unless you have 8-byte alignment. (tiemann) */
620 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
625 DECL_EXTERNAL (decl) = 1;
626 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
632 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
633 BV_VCALL_INDEX for each entry is cleared. */
636 copy_virtuals (binfo)
642 copies = copy_list (BINFO_VIRTUALS (binfo));
643 for (t = copies; t; t = TREE_CHAIN (t))
645 BV_VCALL_INDEX (t) = NULL_TREE;
646 BV_USE_VCALL_INDEX_P (t) = 0;
652 /* Build the primary virtual function table for TYPE. If BINFO is
653 non-NULL, build the vtable starting with the initial approximation
654 that it is the same as the one which is the head of the association
655 list. Returns a non-zero value if a new vtable is actually
659 build_primary_vtable (binfo, type)
665 decl = get_vtable_decl (type, /*complete=*/0);
669 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
670 /* We have already created a vtable for this base, so there's
671 no need to do it again. */
674 virtuals = copy_virtuals (binfo);
675 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
676 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
677 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
681 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
683 virtuals = NULL_TREE;
686 #ifdef GATHER_STATISTICS
688 n_vtable_elems += list_length (virtuals);
691 /* Initialize the association list for this type, based
692 on our first approximation. */
693 TYPE_BINFO_VTABLE (type) = decl;
694 TYPE_BINFO_VIRTUALS (type) = virtuals;
695 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
699 /* Give BINFO a new virtual function table which is initialized
700 with a skeleton-copy of its original initialization. The only
701 entry that changes is the `delta' entry, so we can really
702 share a lot of structure.
704 FOR_TYPE is the most derived type which caused this table to
707 Returns non-zero if we haven't met BINFO before.
709 The order in which vtables are built (by calling this function) for
710 an object must remain the same, otherwise a binary incompatibility
714 build_secondary_vtable (binfo, for_type)
715 tree binfo, for_type;
717 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
719 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
720 /* We already created a vtable for this base. There's no need to
724 /* Remember that we've created a vtable for this BINFO, so that we
725 don't try to do so again. */
726 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
728 /* Make fresh virtual list, so we can smash it later. */
729 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
731 /* Secondary vtables are laid out as part of the same structure as
732 the primary vtable. */
733 BINFO_VTABLE (binfo) = NULL_TREE;
737 /* Create a new vtable for BINFO which is the hierarchy dominated by
738 T. Return non-zero if we actually created a new vtable. */
741 make_new_vtable (t, binfo)
745 if (binfo == TYPE_BINFO (t))
746 /* In this case, it is *type*'s vtable we are modifying. We start
747 with the approximation that its vtable is that of the
748 immediate base class. */
749 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
750 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
751 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
754 /* This is our very own copy of `basetype' to play with. Later,
755 we will fill in all the virtual functions that override the
756 virtual functions in these base classes which are not defined
757 by the current type. */
758 return build_secondary_vtable (binfo, t);
761 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
762 (which is in the hierarchy dominated by T) list FNDECL as its
763 BV_FN. DELTA is the required constant adjustment from the `this'
764 pointer where the vtable entry appears to the `this' required when
765 the function is actually called. */
768 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
779 if (fndecl != BV_FN (v)
780 || !tree_int_cst_equal (delta, BV_DELTA (v)))
784 /* We need a new vtable for BINFO. */
785 if (make_new_vtable (t, binfo))
787 /* If we really did make a new vtable, we also made a copy
788 of the BINFO_VIRTUALS list. Now, we have to find the
789 corresponding entry in that list. */
790 *virtuals = BINFO_VIRTUALS (binfo);
791 while (BV_FN (*virtuals) != BV_FN (v))
792 *virtuals = TREE_CHAIN (*virtuals);
796 base_fndecl = BV_FN (v);
797 BV_DELTA (v) = delta;
798 BV_VCALL_INDEX (v) = NULL_TREE;
801 /* Now assign virtual dispatch information, if unset. We can
802 dispatch this through any overridden base function.
804 FIXME this can choose a secondary vtable if the primary is not
805 also lexically first, leading to useless conversions.
806 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
807 ever be different from DECL_CONTEXT. */
808 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
810 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
811 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
816 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
817 functions present in the vtable so far. */
820 set_vindex (decl, vfuns_p)
826 vindex = (*vfuns_p)++;
827 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
830 /* Add a virtual function to all the appropriate vtables for the class
831 T. DECL_VINDEX(X) should be error_mark_node, if we want to
832 allocate a new slot in our table. If it is error_mark_node, we
833 know that no other function from another vtable is overridden by X.
834 VFUNS_P keeps track of how many virtuals there are in our
835 main vtable for the type, and we build upon the NEW_VIRTUALS list
839 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
841 tree *new_virtuals_p;
842 tree *overridden_virtuals_p;
845 tree t; /* Structure type. */
849 /* If this function doesn't override anything from a base class, we
850 can just assign it a new DECL_VINDEX now. Otherwise, if it does
851 override something, we keep it around and assign its DECL_VINDEX
852 later, in modify_all_vtables. */
853 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
854 /* We've already dealt with this function. */
857 new_virtual = make_node (TREE_LIST);
858 BV_FN (new_virtual) = fndecl;
859 BV_DELTA (new_virtual) = integer_zero_node;
861 if (DECL_VINDEX (fndecl) == error_mark_node)
863 /* FNDECL is a new virtual function; it doesn't override any
864 virtual function in a base class. */
866 /* We remember that this was the base sub-object for rtti. */
867 CLASSTYPE_RTTI (t) = t;
869 /* Now assign virtual dispatch information. */
870 set_vindex (fndecl, vfuns_p);
871 DECL_VIRTUAL_CONTEXT (fndecl) = t;
873 /* Save the state we've computed on the NEW_VIRTUALS list. */
874 TREE_CHAIN (new_virtual) = *new_virtuals_p;
875 *new_virtuals_p = new_virtual;
879 /* FNDECL overrides a function from a base class. */
880 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
881 *overridden_virtuals_p = new_virtual;
885 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
886 the method after the class has already been defined because a
887 declaration for it was seen. (Even though that is erroneous, we
888 add the method for improved error recovery.) */
891 add_method (type, method, error_p)
896 int using = (DECL_CONTEXT (method) != type);
901 if (!CLASSTYPE_METHOD_VEC (type))
902 /* Make a new method vector. We start with 8 entries. We must
903 allocate at least two (for constructors and destructors), and
904 we're going to end up with an assignment operator at some point
907 We could use a TREE_LIST for now, and convert it to a TREE_VEC
908 in finish_struct, but we would probably waste more memory
909 making the links in the list than we would by over-allocating
910 the size of the vector here. Furthermore, we would complicate
911 all the code that expects this to be a vector. */
912 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
914 method_vec = CLASSTYPE_METHOD_VEC (type);
915 len = TREE_VEC_LENGTH (method_vec);
917 /* Constructors and destructors go in special slots. */
918 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
919 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
920 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
921 slot = CLASSTYPE_DESTRUCTOR_SLOT;
924 /* See if we already have an entry with this name. */
925 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
926 if (!TREE_VEC_ELT (method_vec, slot)
927 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
929 == DECL_NAME (method)))
934 /* We need a bigger method vector. */
938 /* In the non-error case, we are processing a class
939 definition. Double the size of the vector to give room
943 /* In the error case, the vector is already complete. We
944 don't expect many errors, and the rest of the front-end
945 will get confused if there are empty slots in the vector. */
949 new_vec = make_tree_vec (new_len);
950 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
951 len * sizeof (tree));
953 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
956 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
958 /* Type conversion operators have to come before ordinary
959 methods; add_conversions depends on this to speed up
960 looking for conversion operators. So, if necessary, we
961 slide some of the vector elements up. In theory, this
962 makes this algorithm O(N^2) but we don't expect many
963 conversion operators. */
964 for (slot = 2; slot < len; ++slot)
966 tree fn = TREE_VEC_ELT (method_vec, slot);
969 /* There are no more entries in the vector, so we
970 can insert the new conversion operator here. */
973 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
974 /* We can insert the new function right at the
979 if (!TREE_VEC_ELT (method_vec, slot))
980 /* There is nothing in the Ith slot, so we can avoid
985 /* We know the last slot in the vector is empty
986 because we know that at this point there's room
987 for a new function. */
988 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
989 &TREE_VEC_ELT (method_vec, slot),
990 (len - slot - 1) * sizeof (tree));
991 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
996 if (template_class_depth (type))
997 /* TYPE is a template class. Don't issue any errors now; wait
998 until instantiation time to complain. */
1004 /* Check to see if we've already got this method. */
1005 for (fns = TREE_VEC_ELT (method_vec, slot);
1007 fns = OVL_NEXT (fns))
1009 tree fn = OVL_CURRENT (fns);
1011 if (TREE_CODE (fn) != TREE_CODE (method))
1014 if (TREE_CODE (method) != TEMPLATE_DECL)
1016 /* [over.load] Member function declarations with the
1017 same name and the same parameter types cannot be
1018 overloaded if any of them is a static member
1019 function declaration. */
1020 if ((DECL_STATIC_FUNCTION_P (fn)
1021 != DECL_STATIC_FUNCTION_P (method))
1024 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1025 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1027 if (! DECL_STATIC_FUNCTION_P (fn))
1028 parms1 = TREE_CHAIN (parms1);
1029 if (! DECL_STATIC_FUNCTION_P (method))
1030 parms2 = TREE_CHAIN (parms2);
1032 if (compparms (parms1, parms2))
1035 /* Defer to the local function. */
1038 cp_error ("`%#D' and `%#D' cannot be overloaded",
1044 if (!decls_match (fn, method))
1047 /* There has already been a declaration of this method
1048 or member template. */
1049 cp_error_at ("`%D' has already been declared in `%T'",
1052 /* We don't call duplicate_decls here to merge the
1053 declarations because that will confuse things if the
1054 methods have inline definitions. In particular, we
1055 will crash while processing the definitions. */
1060 /* Actually insert the new method. */
1061 TREE_VEC_ELT (method_vec, slot)
1062 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1064 /* Add the new binding. */
1065 if (!DECL_CONSTRUCTOR_P (method)
1066 && !DECL_DESTRUCTOR_P (method))
1067 push_class_level_binding (DECL_NAME (method),
1068 TREE_VEC_ELT (method_vec, slot));
1071 /* Subroutines of finish_struct. */
1073 /* Look through the list of fields for this struct, deleting
1074 duplicates as we go. This must be recursive to handle
1077 FIELD is the field which may not appear anywhere in FIELDS.
1078 FIELD_PTR, if non-null, is the starting point at which
1079 chained deletions may take place.
1080 The value returned is the first acceptable entry found
1083 Note that anonymous fields which are not of UNION_TYPE are
1084 not duplicates, they are just anonymous fields. This happens
1085 when we have unnamed bitfields, for example. */
1088 delete_duplicate_fields_1 (field, fields)
1093 if (DECL_NAME (field) == 0)
1095 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1098 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1099 fields = delete_duplicate_fields_1 (x, fields);
1104 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1106 if (DECL_NAME (x) == 0)
1108 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1110 TYPE_FIELDS (TREE_TYPE (x))
1111 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1112 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1115 fields = TREE_CHAIN (fields);
1117 TREE_CHAIN (prev) = TREE_CHAIN (x);
1120 else if (TREE_CODE (field) == USING_DECL)
1121 /* A using declaration may is allowed to appear more than
1122 once. We'll prune these from the field list later, and
1123 handle_using_decl will complain about invalid multiple
1126 else if (DECL_NAME (field) == DECL_NAME (x))
1128 if (TREE_CODE (field) == CONST_DECL
1129 && TREE_CODE (x) == CONST_DECL)
1130 cp_error_at ("duplicate enum value `%D'", x);
1131 else if (TREE_CODE (field) == CONST_DECL
1132 || TREE_CODE (x) == CONST_DECL)
1133 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1135 else if (DECL_DECLARES_TYPE_P (field)
1136 && DECL_DECLARES_TYPE_P (x))
1138 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1140 cp_error_at ("duplicate nested type `%D'", x);
1142 else if (DECL_DECLARES_TYPE_P (field)
1143 || DECL_DECLARES_TYPE_P (x))
1145 /* Hide tag decls. */
1146 if ((TREE_CODE (field) == TYPE_DECL
1147 && DECL_ARTIFICIAL (field))
1148 || (TREE_CODE (x) == TYPE_DECL
1149 && DECL_ARTIFICIAL (x)))
1151 cp_error_at ("duplicate field `%D' (as type and non-type)",
1155 cp_error_at ("duplicate member `%D'", x);
1157 fields = TREE_CHAIN (fields);
1159 TREE_CHAIN (prev) = TREE_CHAIN (x);
1167 delete_duplicate_fields (fields)
1171 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1172 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1175 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1176 legit, otherwise return 0. */
1179 alter_access (t, fdecl, access)
1186 if (!DECL_LANG_SPECIFIC (fdecl))
1187 retrofit_lang_decl (fdecl);
1189 if (DECL_DISCRIMINATOR_P (fdecl))
1192 elem = purpose_member (t, DECL_ACCESS (fdecl));
1195 if (TREE_VALUE (elem) != access)
1197 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1198 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1200 error ("conflicting access specifications for field `%s', ignored",
1201 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1205 /* They're changing the access to the same thing they changed
1206 it to before. That's OK. */
1212 enforce_access (t, fdecl);
1213 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1219 /* Process the USING_DECL, which is a member of T. */
1222 handle_using_decl (using_decl, t)
1226 tree ctype = DECL_INITIAL (using_decl);
1227 tree name = DECL_NAME (using_decl);
1229 = TREE_PRIVATE (using_decl) ? access_private_node
1230 : TREE_PROTECTED (using_decl) ? access_protected_node
1231 : access_public_node;
1233 tree flist = NULL_TREE;
1236 binfo = binfo_or_else (ctype, t);
1240 if (name == constructor_name (ctype)
1241 || name == constructor_name_full (ctype))
1243 cp_error_at ("`%D' names constructor", using_decl);
1246 if (name == constructor_name (t)
1247 || name == constructor_name_full (t))
1249 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1253 fdecl = lookup_member (binfo, name, 0, 0);
1257 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1261 if (BASELINK_P (fdecl))
1262 /* Ignore base type this came from. */
1263 fdecl = TREE_VALUE (fdecl);
1265 old_value = IDENTIFIER_CLASS_VALUE (name);
1268 if (is_overloaded_fn (old_value))
1269 old_value = OVL_CURRENT (old_value);
1271 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1274 old_value = NULL_TREE;
1277 if (is_overloaded_fn (fdecl))
1282 else if (is_overloaded_fn (old_value))
1285 /* It's OK to use functions from a base when there are functions with
1286 the same name already present in the current class. */;
1289 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1290 cp_error_at (" because of local method `%#D' with same name",
1291 OVL_CURRENT (old_value));
1295 else if (!DECL_ARTIFICIAL (old_value))
1297 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1298 cp_error_at (" because of local member `%#D' with same name", old_value);
1302 /* Make type T see field decl FDECL with access ACCESS.*/
1304 for (; flist; flist = OVL_NEXT (flist))
1306 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1307 alter_access (t, OVL_CURRENT (flist), access);
1310 alter_access (t, fdecl, access);
1313 /* Run through the base clases of T, updating
1314 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1315 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1319 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1322 int *cant_have_default_ctor_p;
1323 int *cant_have_const_ctor_p;
1324 int *no_const_asn_ref_p;
1328 int seen_non_virtual_nearly_empty_base_p;
1331 binfos = TYPE_BINFO_BASETYPES (t);
1332 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1333 seen_non_virtual_nearly_empty_base_p = 0;
1335 /* An aggregate cannot have baseclasses. */
1336 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1338 for (i = 0; i < n_baseclasses; ++i)
1343 /* Figure out what base we're looking at. */
1344 base_binfo = TREE_VEC_ELT (binfos, i);
1345 basetype = TREE_TYPE (base_binfo);
1347 /* If the type of basetype is incomplete, then we already
1348 complained about that fact (and we should have fixed it up as
1350 if (!COMPLETE_TYPE_P (basetype))
1353 /* The base type is of incomplete type. It is
1354 probably best to pretend that it does not
1356 if (i == n_baseclasses-1)
1357 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1358 TREE_VEC_LENGTH (binfos) -= 1;
1360 for (j = i; j+1 < n_baseclasses; j++)
1361 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1365 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1366 here because the case of virtual functions but non-virtual
1367 dtor is handled in finish_struct_1. */
1368 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1369 && TYPE_HAS_DESTRUCTOR (basetype))
1370 cp_warning ("base class `%#T' has a non-virtual destructor",
1373 /* If the base class doesn't have copy constructors or
1374 assignment operators that take const references, then the
1375 derived class cannot have such a member automatically
1377 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1378 *cant_have_const_ctor_p = 1;
1379 if (TYPE_HAS_ASSIGN_REF (basetype)
1380 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1381 *no_const_asn_ref_p = 1;
1382 /* Similarly, if the base class doesn't have a default
1383 constructor, then the derived class won't have an
1384 automatically generated default constructor. */
1385 if (TYPE_HAS_CONSTRUCTOR (basetype)
1386 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1388 *cant_have_default_ctor_p = 1;
1389 if (! TYPE_HAS_CONSTRUCTOR (t))
1390 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1394 if (TREE_VIA_VIRTUAL (base_binfo))
1395 /* A virtual base does not effect nearly emptiness. */
1397 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1399 if (seen_non_virtual_nearly_empty_base_p)
1400 /* And if there is more than one nearly empty base, then the
1401 derived class is not nearly empty either. */
1402 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1404 /* Remember we've seen one. */
1405 seen_non_virtual_nearly_empty_base_p = 1;
1407 else if (!is_empty_class (basetype))
1408 /* If the base class is not empty or nearly empty, then this
1409 class cannot be nearly empty. */
1410 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1412 /* A lot of properties from the bases also apply to the derived
1414 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1415 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1416 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1417 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1418 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1419 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1420 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1421 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1422 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1423 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1427 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1428 TO. Move primary information from FROM to TO, and recursively traverse
1429 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1430 assoc list of binfos that have already been reseated. */
1433 force_canonical_binfo_r (to, from, type, mappings)
1439 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1441 my_friendly_assert (to != from, 20010905);
1442 BINFO_INDIRECT_PRIMARY_P (to)
1443 = BINFO_INDIRECT_PRIMARY_P (from);
1444 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1445 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1446 BINFO_UNSHARED_MARKED (from) = 0;
1447 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1448 BINFO_LOST_PRIMARY_P (from) = 0;
1449 if (BINFO_PRIMARY_P (from))
1451 tree primary = BINFO_PRIMARY_BASE_OF (from);
1454 /* We might have just moved the primary base too, see if it's on our
1456 assoc = purpose_member (primary, mappings);
1458 primary = TREE_VALUE (assoc);
1459 BINFO_PRIMARY_BASE_OF (to) = primary;
1460 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1462 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1464 mappings = tree_cons (from, to, mappings);
1466 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1467 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1469 tree from_primary = get_primary_binfo (from);
1471 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1472 force_canonical_binfo (get_primary_binfo (to), from_primary,
1476 for (i = 0; i != n_baseclasses; i++)
1478 tree from_binfo = BINFO_BASETYPE (from, i);
1479 tree to_binfo = BINFO_BASETYPE (to, i);
1481 if (TREE_VIA_VIRTUAL (from_binfo))
1483 if (BINFO_PRIMARY_P (from_binfo) &&
1484 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1485 /* This base is a primary of some binfo we have already
1486 reseated. We must reseat this one too. */
1487 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1490 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1494 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1495 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1496 MAPPINGS is an assoc list of binfos that have already been reseated.
1497 Adjust any non-virtual bases within FROM, and also move any virtual bases
1498 which are canonical. This complication arises because selecting primary
1499 bases walks in inheritance graph order, but we don't share binfos for
1500 virtual bases, hence we can fill in the primaries for a virtual base,
1501 and then discover that a later base requires the virtual as its
1505 force_canonical_binfo (to, from, type, mappings)
1511 tree assoc = purpose_member (BINFO_TYPE (to),
1512 CLASSTYPE_VBASECLASSES (type));
1513 if (TREE_VALUE (assoc) != to)
1515 TREE_VALUE (assoc) = to;
1516 force_canonical_binfo_r (to, from, type, mappings);
1520 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1521 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1522 otherwise (because something else has already made it primary). */
1525 mark_primary_virtual_base (base_binfo, type)
1529 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1531 if (BINFO_PRIMARY_P (shared_binfo))
1533 /* It's already allocated in the hierarchy. BINFO won't have a
1534 primary base in this hierachy, even though the complete object
1535 BINFO is for, would do. */
1539 /* We need to make sure that the assoc list
1540 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1541 primary BINFO for the virtual base, as this is the one
1542 that'll really exist. */
1543 if (base_binfo != shared_binfo)
1544 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1549 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1550 base, then BINFO has no primary base in this graph. Called from
1551 mark_primary_bases. DATA is the most derived type. */
1553 static tree dfs_unshared_virtual_bases (binfo, data)
1557 tree t = (tree) data;
1559 if (!BINFO_UNSHARED_MARKED (binfo)
1560 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1562 /* This morally virtual base has a primary base when it
1563 is a complete object. We need to locate the shared instance
1564 of this binfo in the type dominated by T. We duplicate the
1565 primary base information from there to here. */
1569 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1570 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1572 unshared_base = get_original_base (binfo,
1573 binfo_for_vbase (BINFO_TYPE (vbase),
1575 my_friendly_assert (unshared_base != binfo, 20010612);
1576 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1577 if (!BINFO_LOST_PRIMARY_P (binfo))
1578 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1581 if (binfo != TYPE_BINFO (t))
1582 /* The vtable fields will have been copied when duplicating the
1583 base binfos. That information is bogus, make sure we don't try
1585 BINFO_VTABLE (binfo) = NULL_TREE;
1587 /* If this is a virtual primary base, make sure its offset matches
1588 that which it is primary for. */
1589 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1590 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1592 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1593 BINFO_OFFSET (binfo));
1594 if (!integer_zerop (delta))
1595 propagate_binfo_offsets (binfo, delta, t);
1598 BINFO_UNSHARED_MARKED (binfo) = 0;
1602 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1603 dominated by TYPE that are primary bases. */
1606 mark_primary_bases (type)
1611 /* Walk the bases in inheritance graph order. */
1612 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1616 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1617 /* Not a dynamic base. */
1620 base_binfo = get_primary_binfo (binfo);
1622 if (TREE_VIA_VIRTUAL (base_binfo))
1623 base_binfo = mark_primary_virtual_base (base_binfo, type);
1626 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1628 BINFO_LOST_PRIMARY_P (binfo) = 1;
1630 BINFO_UNSHARED_MARKED (binfo) = 1;
1632 /* There could remain unshared morally virtual bases which were not
1633 visited in the inheritance graph walk. These bases will have lost
1634 their virtual primary base (should they have one). We must now
1635 find them. Also we must fix up the BINFO_OFFSETs of primary
1636 virtual bases. We could not do that as we went along, as they
1637 were originally copied from the bases we inherited from by
1638 unshare_base_binfos. That may have decided differently about
1639 where a virtual primary base went. */
1640 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1643 /* Make the BINFO the primary base of T. */
1646 set_primary_base (t, binfo, vfuns_p)
1653 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1654 basetype = BINFO_TYPE (binfo);
1655 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1656 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1657 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1658 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1659 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1662 /* Determine the primary class for T. */
1665 determine_primary_base (t, vfuns_p)
1669 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1673 /* If there are no baseclasses, there is certainly no primary base. */
1674 if (n_baseclasses == 0)
1677 type_binfo = TYPE_BINFO (t);
1679 for (i = 0; i < n_baseclasses; i++)
1681 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1682 tree basetype = BINFO_TYPE (base_binfo);
1684 if (TYPE_CONTAINS_VPTR_P (basetype))
1686 /* Even a virtual baseclass can contain our RTTI
1687 information. But, we prefer a non-virtual polymorphic
1689 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1690 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1692 /* We prefer a non-virtual base, although a virtual one will
1694 if (TREE_VIA_VIRTUAL (base_binfo))
1697 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1699 set_primary_base (t, base_binfo, vfuns_p);
1700 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1706 /* Only add unique vfields, and flatten them out as we go. */
1707 for (vfields = CLASSTYPE_VFIELDS (basetype);
1709 vfields = TREE_CHAIN (vfields))
1710 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1711 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1712 CLASSTYPE_VFIELDS (t)
1713 = tree_cons (base_binfo,
1714 VF_BASETYPE_VALUE (vfields),
1715 CLASSTYPE_VFIELDS (t));
1720 if (!TYPE_VFIELD (t))
1721 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1723 /* Find the indirect primary bases - those virtual bases which are primary
1724 bases of something else in this hierarchy. */
1725 for (vbases = CLASSTYPE_VBASECLASSES (t);
1727 vbases = TREE_CHAIN (vbases))
1729 tree vbase_binfo = TREE_VALUE (vbases);
1731 /* See if this virtual base is an indirect primary base. To be so,
1732 it must be a primary base within the hierarchy of one of our
1734 for (i = 0; i < n_baseclasses; ++i)
1736 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1739 for (v = CLASSTYPE_VBASECLASSES (basetype);
1743 tree base_vbase = TREE_VALUE (v);
1745 if (BINFO_PRIMARY_P (base_vbase)
1746 && same_type_p (BINFO_TYPE (base_vbase),
1747 BINFO_TYPE (vbase_binfo)))
1749 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1754 /* If we've discovered that this virtual base is an indirect
1755 primary base, then we can move on to the next virtual
1757 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1762 /* A "nearly-empty" virtual base class can be the primary base
1763 class, if no non-virtual polymorphic base can be found. */
1764 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1766 /* If not NULL, this is the best primary base candidate we have
1768 tree candidate = NULL_TREE;
1771 /* Loop over the baseclasses. */
1772 for (base_binfo = TYPE_BINFO (t);
1774 base_binfo = TREE_CHAIN (base_binfo))
1776 tree basetype = BINFO_TYPE (base_binfo);
1778 if (TREE_VIA_VIRTUAL (base_binfo)
1779 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1781 /* If this is not an indirect primary base, then it's
1782 definitely our primary base. */
1783 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1785 candidate = base_binfo;
1789 /* If this is an indirect primary base, it still could be
1790 our primary base -- unless we later find there's another
1791 nearly-empty virtual base that isn't an indirect
1794 candidate = base_binfo;
1798 /* If we've got a primary base, use it. */
1801 set_primary_base (t, candidate, vfuns_p);
1802 CLASSTYPE_VFIELDS (t)
1803 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1807 /* Mark the primary base classes at this point. */
1808 mark_primary_bases (t);
1811 /* Set memoizing fields and bits of T (and its variants) for later
1815 finish_struct_bits (t)
1818 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1820 /* Fix up variants (if any). */
1821 tree variants = TYPE_NEXT_VARIANT (t);
1824 /* These fields are in the _TYPE part of the node, not in
1825 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1826 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1827 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1828 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1829 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1830 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1832 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1833 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1834 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1835 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1836 /* Copy whatever these are holding today. */
1837 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1838 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1839 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1840 TYPE_SIZE (variants) = TYPE_SIZE (t);
1841 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1842 variants = TYPE_NEXT_VARIANT (variants);
1845 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1846 /* For a class w/o baseclasses, `finish_struct' has set
1847 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1848 definition). Similarly for a class whose base classes do not
1849 have vtables. When neither of these is true, we might have
1850 removed abstract virtuals (by providing a definition), added
1851 some (by declaring new ones), or redeclared ones from a base
1852 class. We need to recalculate what's really an abstract virtual
1853 at this point (by looking in the vtables). */
1854 get_pure_virtuals (t);
1858 /* Notice whether this class has type conversion functions defined. */
1859 tree binfo = TYPE_BINFO (t);
1860 tree binfos = BINFO_BASETYPES (binfo);
1863 for (i = n_baseclasses-1; i >= 0; i--)
1865 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1867 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1871 /* If this type has a copy constructor, force its mode to be BLKmode, and
1872 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1873 be passed by invisible reference and prevent it from being returned in
1876 Also do this if the class has BLKmode but can still be returned in
1877 registers, since function_cannot_inline_p won't let us inline
1878 functions returning such a type. This affects the HP-PA. */
1879 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1880 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1881 && CLASSTYPE_NON_AGGREGATE (t)))
1884 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1885 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1887 TYPE_MODE (variants) = BLKmode;
1888 TREE_ADDRESSABLE (variants) = 1;
1893 /* Issue warnings about T having private constructors, but no friends,
1896 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1897 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1898 non-private static member functions. */
1901 maybe_warn_about_overly_private_class (t)
1904 int has_member_fn = 0;
1905 int has_nonprivate_method = 0;
1908 if (!warn_ctor_dtor_privacy
1909 /* If the class has friends, those entities might create and
1910 access instances, so we should not warn. */
1911 || (CLASSTYPE_FRIEND_CLASSES (t)
1912 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1913 /* We will have warned when the template was declared; there's
1914 no need to warn on every instantiation. */
1915 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1916 /* There's no reason to even consider warning about this
1920 /* We only issue one warning, if more than one applies, because
1921 otherwise, on code like:
1924 // Oops - forgot `public:'
1930 we warn several times about essentially the same problem. */
1932 /* Check to see if all (non-constructor, non-destructor) member
1933 functions are private. (Since there are no friends or
1934 non-private statics, we can't ever call any of the private member
1936 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1937 /* We're not interested in compiler-generated methods; they don't
1938 provide any way to call private members. */
1939 if (!DECL_ARTIFICIAL (fn))
1941 if (!TREE_PRIVATE (fn))
1943 if (DECL_STATIC_FUNCTION_P (fn))
1944 /* A non-private static member function is just like a
1945 friend; it can create and invoke private member
1946 functions, and be accessed without a class
1950 has_nonprivate_method = 1;
1953 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1957 if (!has_nonprivate_method && has_member_fn)
1959 /* There are no non-private methods, and there's at least one
1960 private member function that isn't a constructor or
1961 destructor. (If all the private members are
1962 constructors/destructors we want to use the code below that
1963 issues error messages specifically referring to
1964 constructors/destructors.) */
1966 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1967 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1968 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1969 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1971 has_nonprivate_method = 1;
1974 if (!has_nonprivate_method)
1976 cp_warning ("all member functions in class `%T' are private", t);
1981 /* Even if some of the member functions are non-private, the class
1982 won't be useful for much if all the constructors or destructors
1983 are private: such an object can never be created or destroyed. */
1984 if (TYPE_HAS_DESTRUCTOR (t))
1986 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1988 if (TREE_PRIVATE (dtor))
1990 cp_warning ("`%#T' only defines a private destructor and has no friends",
1996 if (TYPE_HAS_CONSTRUCTOR (t))
1998 int nonprivate_ctor = 0;
2000 /* If a non-template class does not define a copy
2001 constructor, one is defined for it, enabling it to avoid
2002 this warning. For a template class, this does not
2003 happen, and so we would normally get a warning on:
2005 template <class T> class C { private: C(); };
2007 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2008 complete non-template or fully instantiated classes have this
2010 if (!TYPE_HAS_INIT_REF (t))
2011 nonprivate_ctor = 1;
2013 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2017 tree ctor = OVL_CURRENT (fn);
2018 /* Ideally, we wouldn't count copy constructors (or, in
2019 fact, any constructor that takes an argument of the
2020 class type as a parameter) because such things cannot
2021 be used to construct an instance of the class unless
2022 you already have one. But, for now at least, we're
2024 if (! TREE_PRIVATE (ctor))
2026 nonprivate_ctor = 1;
2031 if (nonprivate_ctor == 0)
2033 cp_warning ("`%#T' only defines private constructors and has no friends",
2040 /* Function to help qsort sort FIELD_DECLs by name order. */
2043 field_decl_cmp (x, y)
2046 if (DECL_NAME (*x) == DECL_NAME (*y))
2047 /* A nontype is "greater" than a type. */
2048 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2049 if (DECL_NAME (*x) == NULL_TREE)
2051 if (DECL_NAME (*y) == NULL_TREE)
2053 if (DECL_NAME (*x) < DECL_NAME (*y))
2058 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2061 method_name_cmp (m1, m2)
2062 const tree *m1, *m2;
2064 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2066 if (*m1 == NULL_TREE)
2068 if (*m2 == NULL_TREE)
2070 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2075 /* Warn about duplicate methods in fn_fields. Also compact method
2076 lists so that lookup can be made faster.
2078 Data Structure: List of method lists. The outer list is a
2079 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2080 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2081 links the entire list of methods for TYPE_METHODS. Friends are
2082 chained in the same way as member functions (? TREE_CHAIN or
2083 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2084 list. That allows them to be quickly deleted, and requires no
2087 Sort methods that are not special (i.e., constructors, destructors,
2088 and type conversion operators) so that we can find them faster in
2092 finish_struct_methods (t)
2099 if (!TYPE_METHODS (t))
2101 /* Clear these for safety; perhaps some parsing error could set
2102 these incorrectly. */
2103 TYPE_HAS_CONSTRUCTOR (t) = 0;
2104 TYPE_HAS_DESTRUCTOR (t) = 0;
2105 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2109 method_vec = CLASSTYPE_METHOD_VEC (t);
2110 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2111 len = TREE_VEC_LENGTH (method_vec);
2113 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2114 and the next few with type conversion operators (if any). */
2115 for (fn_fields = TYPE_METHODS (t); fn_fields;
2116 fn_fields = TREE_CHAIN (fn_fields))
2117 /* Clear out this flag. */
2118 DECL_IN_AGGR_P (fn_fields) = 0;
2120 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2121 /* We thought there was a destructor, but there wasn't. Some
2122 parse errors cause this anomalous situation. */
2123 TYPE_HAS_DESTRUCTOR (t) = 0;
2125 /* Issue warnings about private constructors and such. If there are
2126 no methods, then some public defaults are generated. */
2127 maybe_warn_about_overly_private_class (t);
2129 /* Now sort the methods. */
2130 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2132 TREE_VEC_LENGTH (method_vec) = len;
2134 /* The type conversion ops have to live at the front of the vec, so we
2136 for (slot = 2; slot < len; ++slot)
2138 tree fn = TREE_VEC_ELT (method_vec, slot);
2140 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2144 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2145 (int (*)(const void *, const void *))method_name_cmp);
2148 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2151 duplicate_tag_error (t)
2154 cp_error ("redefinition of `%#T'", t);
2155 cp_error_at ("previous definition of `%#T'", t);
2157 /* Pretend we haven't defined this type. */
2159 /* All of the component_decl's were TREE_CHAINed together in the parser.
2160 finish_struct_methods walks these chains and assembles all methods with
2161 the same base name into DECL_CHAINs. Now we don't need the parser chains
2162 anymore, so we unravel them. */
2164 /* This used to be in finish_struct, but it turns out that the
2165 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2167 if (CLASSTYPE_METHOD_VEC (t))
2169 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2170 int i, len = TREE_VEC_LENGTH (method_vec);
2171 for (i = 0; i < len; i++)
2173 tree unchain = TREE_VEC_ELT (method_vec, i);
2174 while (unchain != NULL_TREE)
2176 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2177 unchain = OVL_NEXT (unchain);
2182 if (TYPE_LANG_SPECIFIC (t))
2184 tree binfo = TYPE_BINFO (t);
2185 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2186 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2187 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2188 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2190 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2191 BINFO_BASETYPES(binfo) = NULL_TREE;
2193 TYPE_BINFO (t) = binfo;
2194 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2195 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2196 TYPE_REDEFINED (t) = 1;
2197 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2198 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2200 TYPE_SIZE (t) = NULL_TREE;
2201 TYPE_MODE (t) = VOIDmode;
2202 TYPE_FIELDS (t) = NULL_TREE;
2203 TYPE_METHODS (t) = NULL_TREE;
2204 TYPE_VFIELD (t) = NULL_TREE;
2205 TYPE_CONTEXT (t) = NULL_TREE;
2207 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2208 TYPE_LANG_FLAG_0 (t) = 0;
2209 TYPE_LANG_FLAG_1 (t) = 0;
2210 TYPE_LANG_FLAG_2 (t) = 0;
2211 TYPE_LANG_FLAG_3 (t) = 0;
2212 TYPE_LANG_FLAG_4 (t) = 0;
2213 TYPE_LANG_FLAG_5 (t) = 0;
2214 TYPE_LANG_FLAG_6 (t) = 0;
2215 /* But not this one. */
2216 SET_IS_AGGR_TYPE (t, 1);
2219 /* Make BINFO's vtable have N entries, including RTTI entries,
2220 vbase and vcall offsets, etc. Set its type and call the backend
2224 layout_vtable_decl (binfo, n)
2231 atype = build_cplus_array_type (vtable_entry_type,
2232 build_index_type (size_int (n - 1)));
2233 layout_type (atype);
2235 /* We may have to grow the vtable. */
2236 vtable = get_vtbl_decl_for_binfo (binfo);
2237 if (!same_type_p (TREE_TYPE (vtable), atype))
2239 TREE_TYPE (vtable) = atype;
2240 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2241 layout_decl (vtable, 0);
2243 /* At one time the vtable info was grabbed 2 words at a time. This
2244 fails on Sparc unless you have 8-byte alignment. */
2245 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2246 DECL_ALIGN (vtable));
2250 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2251 have the same signature. */
2254 same_signature_p (fndecl, base_fndecl)
2255 tree fndecl, base_fndecl;
2257 /* One destructor overrides another if they are the same kind of
2259 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2260 && special_function_p (base_fndecl) == special_function_p (fndecl))
2262 /* But a non-destructor never overrides a destructor, nor vice
2263 versa, nor do different kinds of destructors override
2264 one-another. For example, a complete object destructor does not
2265 override a deleting destructor. */
2266 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2269 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2271 tree types, base_types;
2272 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2273 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2274 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2275 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2276 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2282 typedef struct find_final_overrider_data_s {
2283 /* The function for which we are trying to find a final overrider. */
2285 /* The base class in which the function was declared. */
2286 tree declaring_base;
2287 /* The most derived class in the hierarchy. */
2288 tree most_derived_type;
2289 /* The final overriding function. */
2291 /* The functions that we thought might be final overriders, but
2294 /* The BINFO for the class in which the final overriding function
2296 tree overriding_base;
2297 } find_final_overrider_data;
2299 /* Called from find_final_overrider via dfs_walk. */
2302 dfs_find_final_overrider (binfo, data)
2306 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2308 if (same_type_p (BINFO_TYPE (binfo),
2309 BINFO_TYPE (ffod->declaring_base))
2310 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2311 BINFO_OFFSET (ffod->declaring_base)))
2316 /* We haven't found an overrider yet. */
2318 /* We've found a path to the declaring base. Walk down the path
2319 looking for an overrider for FN. */
2320 for (path = reverse_path (binfo);
2322 path = TREE_CHAIN (path))
2324 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2330 /* If we found an overrider, record the overriding function, and
2331 the base from which it came. */
2336 /* Assume the path is non-virtual. See if there are any
2337 virtual bases from (but not including) the overrider up
2338 to and including the base where the function is
2340 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2341 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2343 base = ffod->declaring_base;
2347 /* If we didn't already have an overrider, or any
2348 candidates, then this function is the best candidate so
2350 if (!ffod->overriding_fn && !ffod->candidates)
2352 ffod->overriding_fn = method;
2353 ffod->overriding_base = TREE_VALUE (path);
2355 else if (ffod->overriding_fn)
2357 /* We had a best overrider; let's see how this compares. */
2359 if (ffod->overriding_fn == method
2360 && (tree_int_cst_equal
2361 (BINFO_OFFSET (TREE_VALUE (path)),
2362 BINFO_OFFSET (ffod->overriding_base))))
2363 /* We found the same overrider we already have, and in the
2364 same place; it's still the best. */;
2365 else if (strictly_overrides (ffod->overriding_fn, method))
2366 /* The old function overrides this function; it's still the
2368 else if (strictly_overrides (method, ffod->overriding_fn))
2370 /* The new function overrides the old; it's now the
2372 ffod->overriding_fn = method;
2373 ffod->overriding_base = TREE_VALUE (path);
2379 = build_tree_list (NULL_TREE,
2380 ffod->overriding_fn);
2381 if (method != ffod->overriding_fn)
2383 = tree_cons (NULL_TREE, method, ffod->candidates);
2384 ffod->overriding_fn = NULL_TREE;
2385 ffod->overriding_base = NULL_TREE;
2390 /* We had a list of ambiguous overrides; let's see how this
2391 new one compares. */
2394 bool incomparable = false;
2396 /* If there were previous candidates, and this function
2397 overrides all of them, then it is the new best
2399 for (candidates = ffod->candidates;
2401 candidates = TREE_CHAIN (candidates))
2403 /* If the candidate overrides the METHOD, then we
2404 needn't worry about it any further. */
2405 if (strictly_overrides (TREE_VALUE (candidates),
2412 /* If the METHOD doesn't override the candidate,
2413 then it is incomporable. */
2414 if (!strictly_overrides (method,
2415 TREE_VALUE (candidates)))
2416 incomparable = true;
2419 /* If METHOD overrode all the candidates, then it is the
2420 new best candidate. */
2421 if (!candidates && !incomparable)
2423 ffod->overriding_fn = method;
2424 ffod->overriding_base = TREE_VALUE (path);
2425 ffod->candidates = NULL_TREE;
2427 /* If METHOD didn't override all the candidates, then it
2428 is another candidate. */
2429 else if (method && incomparable)
2431 = tree_cons (NULL_TREE, method, ffod->candidates);
2439 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2440 FN and whose TREE_VALUE is the binfo for the base where the
2441 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2442 base object in which FN is declared. */
2445 find_final_overrider (t, binfo, fn)
2450 find_final_overrider_data ffod;
2452 /* Getting this right is a little tricky. This is legal:
2454 struct S { virtual void f (); };
2455 struct T { virtual void f (); };
2456 struct U : public S, public T { };
2458 even though calling `f' in `U' is ambiguous. But,
2460 struct R { virtual void f(); };
2461 struct S : virtual public R { virtual void f (); };
2462 struct T : virtual public R { virtual void f (); };
2463 struct U : public S, public T { };
2465 is not -- there's no way to decide whether to put `S::f' or
2466 `T::f' in the vtable for `R'.
2468 The solution is to look at all paths to BINFO. If we find
2469 different overriders along any two, then there is a problem. */
2471 ffod.declaring_base = binfo;
2472 ffod.most_derived_type = t;
2473 ffod.overriding_fn = NULL_TREE;
2474 ffod.overriding_base = NULL_TREE;
2475 ffod.candidates = NULL_TREE;
2477 dfs_walk (TYPE_BINFO (t),
2478 dfs_find_final_overrider,
2482 /* If there was no winner, issue an error message. */
2483 if (!ffod.overriding_fn)
2485 cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2486 return error_mark_node;
2489 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2492 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2493 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2494 the function that the slot in T's primary vtable points to. */
2496 static tree get_matching_virtual PARAMS ((tree, tree));
2498 get_matching_virtual (t, fn)
2503 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2504 if (same_signature_p (BV_FN (f), fn))
2509 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2510 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2511 corresponding position in the BINFO_VIRTUALS list. */
2514 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2526 /* Find the nearest primary base (possibly binfo itself) which defines
2527 this function; this is the class the caller will convert to when
2528 calling FN through BINFO. */
2529 for (b = binfo; ; b = get_primary_binfo (b))
2531 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2536 /* Find the final overrider. */
2537 overrider = find_final_overrider (t, b, fn);
2538 if (overrider == error_mark_node)
2541 /* Assume that we will produce a thunk that convert all the way to
2542 the final overrider, and not to an intermediate virtual base. */
2543 virtual_base = NULL_TREE;
2545 /* We will convert to an intermediate virtual base first, and then
2546 use the vcall offset located there to finish the conversion. */
2549 /* If we find the final overrider, then we can stop
2551 if (same_type_p (BINFO_TYPE (b),
2552 BINFO_TYPE (TREE_VALUE (overrider))))
2555 /* If we find a virtual base, and we haven't yet found the
2556 overrider, then there is a virtual base between the
2557 declaring base (first_defn) and the final overrider. */
2558 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2561 b = BINFO_INHERITANCE_CHAIN (b);
2564 /* Compute the constant adjustment to the `this' pointer. The
2565 `this' pointer, when this function is called, will point at BINFO
2566 (or one of its primary bases, which are at the same offset). */
2569 /* The `this' pointer needs to be adjusted from the declaration to
2570 the nearest virtual base. */
2571 delta = size_diffop (BINFO_OFFSET (virtual_base),
2572 BINFO_OFFSET (first_defn));
2575 /* The `this' pointer needs to be adjusted from pointing to
2576 BINFO to pointing at the base where the final overrider
2578 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2579 BINFO_OFFSET (binfo));
2581 if (! integer_zerop (delta))
2583 /* We'll need a thunk. But if we have a (perhaps formerly)
2584 primary virtual base, we have a vcall slot for this function,
2585 so we can use it rather than create a non-virtual thunk. */
2587 b = get_primary_binfo (first_defn);
2588 for (; b; b = get_primary_binfo (b))
2590 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2592 /* b doesn't have this function; no suitable vbase. */
2594 if (TREE_VIA_VIRTUAL (b))
2596 /* Found one; we can treat ourselves as a virtual base. */
2597 virtual_base = binfo;
2598 delta = size_zero_node;
2605 modify_vtable_entry (t,
2607 TREE_PURPOSE (overrider),
2612 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2615 /* Called from modify_all_vtables via dfs_walk. */
2618 dfs_modify_vtables (binfo, data)
2622 if (/* There's no need to modify the vtable for a non-virtual
2623 primary base; we're not going to use that vtable anyhow.
2624 We do still need to do this for virtual primary bases, as they
2625 could become non-primary in a construction vtable. */
2626 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2627 /* Similarly, a base without a vtable needs no modification. */
2628 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2636 make_new_vtable (t, binfo);
2638 /* Now, go through each of the virtual functions in the virtual
2639 function table for BINFO. Find the final overrider, and
2640 update the BINFO_VIRTUALS list appropriately. */
2641 for (virtuals = BINFO_VIRTUALS (binfo),
2642 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2644 virtuals = TREE_CHAIN (virtuals),
2645 old_virtuals = TREE_CHAIN (old_virtuals))
2646 update_vtable_entry_for_fn (t,
2648 BV_FN (old_virtuals),
2652 SET_BINFO_MARKED (binfo);
2657 /* Update all of the primary and secondary vtables for T. Create new
2658 vtables as required, and initialize their RTTI information. Each
2659 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2660 function from a base class; find and modify the appropriate entries
2661 to point to the overriding functions. Returns a list, in
2662 declaration order, of the functions that are overridden in this
2663 class, but do not appear in the primary base class vtable, and
2664 which should therefore be appended to the end of the vtable for T. */
2667 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2670 tree overridden_virtuals;
2672 tree binfo = TYPE_BINFO (t);
2675 /* Update all of the vtables. */
2678 dfs_unmarked_real_bases_queue_p,
2680 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2682 /* Include overriding functions for secondary vtables in our primary
2684 for (fnsp = &overridden_virtuals; *fnsp; )
2686 tree fn = TREE_VALUE (*fnsp);
2688 if (!BINFO_VIRTUALS (binfo)
2689 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2691 /* Set the vtable index. */
2692 set_vindex (fn, vfuns_p);
2693 /* We don't need to convert to a base class when calling
2695 DECL_VIRTUAL_CONTEXT (fn) = t;
2697 /* We don't need to adjust the `this' pointer when
2698 calling this function. */
2699 BV_DELTA (*fnsp) = integer_zero_node;
2700 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2702 /* This is an overridden function not already in our
2704 fnsp = &TREE_CHAIN (*fnsp);
2707 /* We've already got an entry for this function. Skip it. */
2708 *fnsp = TREE_CHAIN (*fnsp);
2711 return overridden_virtuals;
2714 /* Here, we already know that they match in every respect.
2715 All we have to check is where they had their declarations. */
2718 strictly_overrides (fndecl1, fndecl2)
2719 tree fndecl1, fndecl2;
2721 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2722 DECL_CONTEXT (fndecl1),
2724 if (distance == -2 || distance > 0)
2729 /* Get the base virtual function declarations in T that are either
2730 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2731 the overrider/hider. */
2734 get_basefndecls (fndecl, t)
2737 tree methods = TYPE_METHODS (t);
2738 tree base_fndecls = NULL_TREE;
2739 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2740 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2744 if (TREE_CODE (methods) == FUNCTION_DECL
2745 && DECL_VINDEX (methods) != NULL_TREE
2746 && DECL_NAME (fndecl) == DECL_NAME (methods))
2747 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2749 methods = TREE_CHAIN (methods);
2753 return base_fndecls;
2755 for (i = 0; i < n_baseclasses; i++)
2757 tree base_binfo = TREE_VEC_ELT (binfos, i);
2758 tree basetype = BINFO_TYPE (base_binfo);
2760 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2764 return base_fndecls;
2767 /* Mark the functions that have been hidden with their overriders.
2768 Since we start out with all functions already marked with a hider,
2769 no need to mark functions that are just hidden.
2771 Subroutine of warn_hidden. */
2774 mark_overriders (fndecl, base_fndecls)
2775 tree fndecl, base_fndecls;
2777 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2778 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2779 TREE_PURPOSE (base_fndecls) = fndecl;
2782 /* If this declaration supersedes the declaration of
2783 a method declared virtual in the base class, then
2784 mark this field as being virtual as well. */
2787 check_for_override (decl, ctype)
2790 if (TREE_CODE (decl) == TEMPLATE_DECL)
2791 /* In [temp.mem] we have:
2793 A specialization of a member function template does not
2794 override a virtual function from a base class. */
2796 if ((DECL_DESTRUCTOR_P (decl)
2797 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2798 && look_for_overrides (ctype, decl)
2799 && !DECL_STATIC_FUNCTION_P (decl))
2801 /* Set DECL_VINDEX to a value that is neither an
2802 INTEGER_CST nor the error_mark_node so that
2803 add_virtual_function will realize this is an
2804 overriding function. */
2805 DECL_VINDEX (decl) = decl;
2807 if (DECL_VIRTUAL_P (decl))
2809 if (DECL_VINDEX (decl) == NULL_TREE)
2810 DECL_VINDEX (decl) = error_mark_node;
2811 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2815 /* Warn about hidden virtual functions that are not overridden in t.
2816 We know that constructors and destructors don't apply. */
2822 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2823 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2826 /* We go through each separately named virtual function. */
2827 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2829 tree fns = TREE_VEC_ELT (method_vec, i);
2830 tree fndecl = NULL_TREE;
2832 tree base_fndecls = NULL_TREE;
2833 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2834 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2836 /* First see if we have any virtual functions in this batch. */
2837 for (; fns; fns = OVL_NEXT (fns))
2839 fndecl = OVL_CURRENT (fns);
2840 if (DECL_VINDEX (fndecl))
2844 if (fns == NULL_TREE)
2847 /* First we get a list of all possible functions that might be
2848 hidden from each base class. */
2849 for (i = 0; i < n_baseclasses; i++)
2851 tree base_binfo = TREE_VEC_ELT (binfos, i);
2852 tree basetype = BINFO_TYPE (base_binfo);
2854 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2858 fns = OVL_NEXT (fns);
2860 /* ...then mark up all the base functions with overriders, preferring
2861 overriders to hiders. */
2863 for (; fns; fns = OVL_NEXT (fns))
2865 fndecl = OVL_CURRENT (fns);
2866 if (DECL_VINDEX (fndecl))
2867 mark_overriders (fndecl, base_fndecls);
2870 /* Now give a warning for all base functions without overriders,
2871 as they are hidden. */
2872 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2873 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
2874 TREE_VALUE (base_fndecls)))
2876 /* Here we know it is a hider, and no overrider exists. */
2877 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2878 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2883 /* Check for things that are invalid. There are probably plenty of other
2884 things we should check for also. */
2887 finish_struct_anon (t)
2892 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2894 if (TREE_STATIC (field))
2896 if (TREE_CODE (field) != FIELD_DECL)
2899 if (DECL_NAME (field) == NULL_TREE
2900 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2902 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2903 for (; elt; elt = TREE_CHAIN (elt))
2905 /* We're generally only interested in entities the user
2906 declared, but we also find nested classes by noticing
2907 the TYPE_DECL that we create implicitly. You're
2908 allowed to put one anonymous union inside another,
2909 though, so we explicitly tolerate that. */
2910 if (DECL_ARTIFICIAL (elt)
2911 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2912 || ANON_AGGR_TYPE_P (TREE_TYPE (elt))))
2915 if (DECL_NAME (elt) == constructor_name (t))
2916 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2919 if (TREE_CODE (elt) != FIELD_DECL)
2921 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2926 if (TREE_PRIVATE (elt))
2927 cp_pedwarn_at ("private member `%#D' in anonymous union",
2929 else if (TREE_PROTECTED (elt))
2930 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2933 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2934 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2940 /* Create default constructors, assignment operators, and so forth for
2941 the type indicated by T, if they are needed.
2942 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2943 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2944 class cannot have a default constructor, copy constructor taking a
2945 const reference argument, or an assignment operator taking a const
2946 reference, respectively. If a virtual destructor is created, its
2947 DECL is returned; otherwise the return value is NULL_TREE. */
2950 add_implicitly_declared_members (t, cant_have_default_ctor,
2951 cant_have_const_cctor,
2952 cant_have_const_assignment)
2954 int cant_have_default_ctor;
2955 int cant_have_const_cctor;
2956 int cant_have_const_assignment;
2959 tree implicit_fns = NULL_TREE;
2960 tree virtual_dtor = NULL_TREE;
2964 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2966 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2967 check_for_override (default_fn, t);
2969 /* If we couldn't make it work, then pretend we didn't need it. */
2970 if (default_fn == void_type_node)
2971 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2974 TREE_CHAIN (default_fn) = implicit_fns;
2975 implicit_fns = default_fn;
2977 if (DECL_VINDEX (default_fn))
2978 virtual_dtor = default_fn;
2982 /* Any non-implicit destructor is non-trivial. */
2983 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2985 /* Default constructor. */
2986 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2988 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2989 TREE_CHAIN (default_fn) = implicit_fns;
2990 implicit_fns = default_fn;
2993 /* Copy constructor. */
2994 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2996 /* ARM 12.18: You get either X(X&) or X(const X&), but
2999 = implicitly_declare_fn (sfk_copy_constructor, t,
3000 /*const_p=*/!cant_have_const_cctor);
3001 TREE_CHAIN (default_fn) = implicit_fns;
3002 implicit_fns = default_fn;
3005 /* Assignment operator. */
3006 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3009 = implicitly_declare_fn (sfk_assignment_operator, t,
3010 /*const_p=*/!cant_have_const_assignment);
3011 TREE_CHAIN (default_fn) = implicit_fns;
3012 implicit_fns = default_fn;
3015 /* Now, hook all of the new functions on to TYPE_METHODS,
3016 and add them to the CLASSTYPE_METHOD_VEC. */
3017 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3018 add_method (t, *f, /*error_p=*/0);
3019 *f = TYPE_METHODS (t);
3020 TYPE_METHODS (t) = implicit_fns;
3022 return virtual_dtor;
3025 /* Subroutine of finish_struct_1. Recursively count the number of fields
3026 in TYPE, including anonymous union members. */
3029 count_fields (fields)
3034 for (x = fields; x; x = TREE_CHAIN (x))
3036 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3037 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3044 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3045 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3048 add_fields_to_vec (fields, field_vec, idx)
3049 tree fields, field_vec;
3053 for (x = fields; x; x = TREE_CHAIN (x))
3055 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3056 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3058 TREE_VEC_ELT (field_vec, idx++) = x;
3063 /* FIELD is a bit-field. We are finishing the processing for its
3064 enclosing type. Issue any appropriate messages and set appropriate
3068 check_bitfield_decl (field)
3071 tree type = TREE_TYPE (field);
3074 /* Detect invalid bit-field type. */
3075 if (DECL_INITIAL (field)
3076 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3078 cp_error_at ("bit-field `%#D' with non-integral type", field);
3079 w = error_mark_node;
3082 /* Detect and ignore out of range field width. */
3083 if (DECL_INITIAL (field))
3085 w = DECL_INITIAL (field);
3087 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3090 /* detect invalid field size. */
3091 if (TREE_CODE (w) == CONST_DECL)
3092 w = DECL_INITIAL (w);
3094 w = decl_constant_value (w);
3096 if (TREE_CODE (w) != INTEGER_CST)
3098 cp_error_at ("bit-field `%D' width not an integer constant",
3100 w = error_mark_node;
3102 else if (tree_int_cst_sgn (w) < 0)
3104 cp_error_at ("negative width in bit-field `%D'", field);
3105 w = error_mark_node;
3107 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3109 cp_error_at ("zero width for bit-field `%D'", field);
3110 w = error_mark_node;
3112 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3113 && TREE_CODE (type) != ENUMERAL_TYPE
3114 && TREE_CODE (type) != BOOLEAN_TYPE)
3115 cp_warning_at ("width of `%D' exceeds its type", field);
3116 else if (TREE_CODE (type) == ENUMERAL_TYPE
3117 && (0 > compare_tree_int (w,
3118 min_precision (TYPE_MIN_VALUE (type),
3119 TREE_UNSIGNED (type)))
3120 || 0 > compare_tree_int (w,
3122 (TYPE_MAX_VALUE (type),
3123 TREE_UNSIGNED (type)))))
3124 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3128 /* Remove the bit-field width indicator so that the rest of the
3129 compiler does not treat that value as an initializer. */
3130 DECL_INITIAL (field) = NULL_TREE;
3132 if (w != error_mark_node)
3134 DECL_SIZE (field) = convert (bitsizetype, w);
3135 DECL_BIT_FIELD (field) = 1;
3137 if (integer_zerop (w))
3139 #ifdef EMPTY_FIELD_BOUNDARY
3140 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3141 EMPTY_FIELD_BOUNDARY);
3143 #ifdef PCC_BITFIELD_TYPE_MATTERS
3144 if (PCC_BITFIELD_TYPE_MATTERS)
3146 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3148 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3155 /* Non-bit-fields are aligned for their type. */
3156 DECL_BIT_FIELD (field) = 0;
3157 CLEAR_DECL_C_BIT_FIELD (field);
3158 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3159 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3163 /* FIELD is a non bit-field. We are finishing the processing for its
3164 enclosing type T. Issue any appropriate messages and set appropriate
3168 check_field_decl (field, t, cant_have_const_ctor,
3169 cant_have_default_ctor, no_const_asn_ref,
3170 any_default_members)
3173 int *cant_have_const_ctor;
3174 int *cant_have_default_ctor;
3175 int *no_const_asn_ref;
3176 int *any_default_members;
3178 tree type = strip_array_types (TREE_TYPE (field));
3180 /* An anonymous union cannot contain any fields which would change
3181 the settings of CANT_HAVE_CONST_CTOR and friends. */
3182 if (ANON_UNION_TYPE_P (type))
3184 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3185 structs. So, we recurse through their fields here. */
3186 else if (ANON_AGGR_TYPE_P (type))
3190 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3191 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3192 check_field_decl (fields, t, cant_have_const_ctor,
3193 cant_have_default_ctor, no_const_asn_ref,
3194 any_default_members);
3196 /* Check members with class type for constructors, destructors,
3198 else if (CLASS_TYPE_P (type))
3200 /* Never let anything with uninheritable virtuals
3201 make it through without complaint. */
3202 abstract_virtuals_error (field, type);
3204 if (TREE_CODE (t) == UNION_TYPE)
3206 if (TYPE_NEEDS_CONSTRUCTING (type))
3207 cp_error_at ("member `%#D' with constructor not allowed in union",
3209 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3210 cp_error_at ("member `%#D' with destructor not allowed in union",
3212 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3213 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3218 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3219 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3220 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3221 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3222 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3225 if (!TYPE_HAS_CONST_INIT_REF (type))
3226 *cant_have_const_ctor = 1;
3228 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3229 *no_const_asn_ref = 1;
3231 if (TYPE_HAS_CONSTRUCTOR (type)
3232 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3233 *cant_have_default_ctor = 1;
3235 if (DECL_INITIAL (field) != NULL_TREE)
3237 /* `build_class_init_list' does not recognize
3239 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3240 cp_error_at ("multiple fields in union `%T' initialized");
3241 *any_default_members = 1;
3244 /* Non-bit-fields are aligned for their type, except packed fields
3245 which require only BITS_PER_UNIT alignment. */
3246 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3247 (DECL_PACKED (field)
3249 : TYPE_ALIGN (TREE_TYPE (field))));
3250 if (! DECL_PACKED (field))
3251 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3254 /* Check the data members (both static and non-static), class-scoped
3255 typedefs, etc., appearing in the declaration of T. Issue
3256 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3257 declaration order) of access declarations; each TREE_VALUE in this
3258 list is a USING_DECL.
3260 In addition, set the following flags:
3263 The class is empty, i.e., contains no non-static data members.
3265 CANT_HAVE_DEFAULT_CTOR_P
3266 This class cannot have an implicitly generated default
3269 CANT_HAVE_CONST_CTOR_P
3270 This class cannot have an implicitly generated copy constructor
3271 taking a const reference.
3273 CANT_HAVE_CONST_ASN_REF
3274 This class cannot have an implicitly generated assignment
3275 operator taking a const reference.
3277 All of these flags should be initialized before calling this
3280 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3281 fields can be added by adding to this chain. */
3284 check_field_decls (t, access_decls, empty_p,
3285 cant_have_default_ctor_p, cant_have_const_ctor_p,
3290 int *cant_have_default_ctor_p;
3291 int *cant_have_const_ctor_p;
3292 int *no_const_asn_ref_p;
3297 int any_default_members;
3299 /* First, delete any duplicate fields. */
3300 delete_duplicate_fields (TYPE_FIELDS (t));
3302 /* Assume there are no access declarations. */
3303 *access_decls = NULL_TREE;
3304 /* Assume this class has no pointer members. */
3306 /* Assume none of the members of this class have default
3308 any_default_members = 0;
3310 for (field = &TYPE_FIELDS (t); *field; field = next)
3313 tree type = TREE_TYPE (x);
3315 GNU_xref_member (current_class_name, x);
3317 next = &TREE_CHAIN (x);
3319 if (TREE_CODE (x) == FIELD_DECL)
3321 DECL_PACKED (x) |= TYPE_PACKED (t);
3323 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3324 /* We don't treat zero-width bitfields as making a class
3329 /* The class is non-empty. */
3331 /* The class is not even nearly empty. */
3332 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3336 if (TREE_CODE (x) == USING_DECL)
3338 /* Prune the access declaration from the list of fields. */
3339 *field = TREE_CHAIN (x);
3341 /* Save the access declarations for our caller. */
3342 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3344 /* Since we've reset *FIELD there's no reason to skip to the
3350 if (TREE_CODE (x) == TYPE_DECL
3351 || TREE_CODE (x) == TEMPLATE_DECL)
3354 /* If we've gotten this far, it's a data member, possibly static,
3355 or an enumerator. */
3357 DECL_CONTEXT (x) = t;
3359 /* ``A local class cannot have static data members.'' ARM 9.4 */
3360 if (current_function_decl && TREE_STATIC (x))
3361 cp_error_at ("field `%D' in local class cannot be static", x);
3363 /* Perform error checking that did not get done in
3365 if (TREE_CODE (type) == FUNCTION_TYPE)
3367 cp_error_at ("field `%D' invalidly declared function type",
3369 type = build_pointer_type (type);
3370 TREE_TYPE (x) = type;
3372 else if (TREE_CODE (type) == METHOD_TYPE)
3374 cp_error_at ("field `%D' invalidly declared method type", x);
3375 type = build_pointer_type (type);
3376 TREE_TYPE (x) = type;
3378 else if (TREE_CODE (type) == OFFSET_TYPE)
3380 cp_error_at ("field `%D' invalidly declared offset type", x);
3381 type = build_pointer_type (type);
3382 TREE_TYPE (x) = type;
3385 if (type == error_mark_node)
3388 /* When this goes into scope, it will be a non-local reference. */
3389 DECL_NONLOCAL (x) = 1;
3391 if (TREE_CODE (x) == CONST_DECL)
3394 if (TREE_CODE (x) == VAR_DECL)
3396 if (TREE_CODE (t) == UNION_TYPE)
3397 /* Unions cannot have static members. */
3398 cp_error_at ("field `%D' declared static in union", x);
3403 /* Now it can only be a FIELD_DECL. */
3405 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3406 CLASSTYPE_NON_AGGREGATE (t) = 1;
3408 /* If this is of reference type, check if it needs an init.
3409 Also do a little ANSI jig if necessary. */
3410 if (TREE_CODE (type) == REFERENCE_TYPE)
3412 CLASSTYPE_NON_POD_P (t) = 1;
3413 if (DECL_INITIAL (x) == NULL_TREE)
3414 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3416 /* ARM $12.6.2: [A member initializer list] (or, for an
3417 aggregate, initialization by a brace-enclosed list) is the
3418 only way to initialize nonstatic const and reference
3420 *cant_have_default_ctor_p = 1;
3421 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3423 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3424 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3427 type = strip_array_types (type);
3429 if (TREE_CODE (type) == POINTER_TYPE)
3432 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3433 CLASSTYPE_HAS_MUTABLE (t) = 1;
3435 if (! pod_type_p (type))
3436 /* DR 148 now allows pointers to members (which are POD themselves),
3437 to be allowed in POD structs. */
3438 CLASSTYPE_NON_POD_P (t) = 1;
3440 /* If any field is const, the structure type is pseudo-const. */
3441 if (CP_TYPE_CONST_P (type))
3443 C_TYPE_FIELDS_READONLY (t) = 1;
3444 if (DECL_INITIAL (x) == NULL_TREE)
3445 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3447 /* ARM $12.6.2: [A member initializer list] (or, for an
3448 aggregate, initialization by a brace-enclosed list) is the
3449 only way to initialize nonstatic const and reference
3451 *cant_have_default_ctor_p = 1;
3452 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3454 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3455 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3457 /* A field that is pseudo-const makes the structure likewise. */
3458 else if (IS_AGGR_TYPE (type))
3460 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3461 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3462 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3465 /* Core issue 80: A nonstatic data member is required to have a
3466 different name from the class iff the class has a
3467 user-defined constructor. */
3468 if (DECL_NAME (x) == constructor_name (t)
3469 && TYPE_HAS_CONSTRUCTOR (t))
3470 cp_pedwarn_at ("field `%#D' with same name as class", x);
3472 /* We set DECL_C_BIT_FIELD in grokbitfield.
3473 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3474 if (DECL_C_BIT_FIELD (x))
3475 check_bitfield_decl (x);
3477 check_field_decl (x, t,
3478 cant_have_const_ctor_p,
3479 cant_have_default_ctor_p,
3481 &any_default_members);
3484 /* Effective C++ rule 11. */
3485 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3486 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3488 cp_warning ("`%#T' has pointer data members", t);
3490 if (! TYPE_HAS_INIT_REF (t))
3492 cp_warning (" but does not override `%T(const %T&)'", t, t);
3493 if (! TYPE_HAS_ASSIGN_REF (t))
3494 cp_warning (" or `operator=(const %T&)'", t);
3496 else if (! TYPE_HAS_ASSIGN_REF (t))
3497 cp_warning (" but does not override `operator=(const %T&)'", t);
3501 /* Check anonymous struct/anonymous union fields. */
3502 finish_struct_anon (t);
3504 /* We've built up the list of access declarations in reverse order.
3506 *access_decls = nreverse (*access_decls);
3509 /* If TYPE is an empty class type, records its OFFSET in the table of
3513 record_subobject_offset (type, offset, offsets)
3520 if (!is_empty_class (type))
3523 /* Record the location of this empty object in OFFSETS. */
3524 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3526 n = splay_tree_insert (offsets,
3527 (splay_tree_key) offset,
3528 (splay_tree_value) NULL_TREE);
3529 n->value = ((splay_tree_value)
3530 tree_cons (NULL_TREE,
3537 /* Returns non-zero if TYPE is an empty class type and there is
3538 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3541 check_subobject_offset (type, offset, offsets)
3549 if (!is_empty_class (type))
3552 /* Record the location of this empty object in OFFSETS. */
3553 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3557 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3558 if (same_type_p (TREE_VALUE (t), type))
3564 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3565 F for every subobject, passing it the type, offset, and table of
3566 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3567 bases should be traversed; otherwise, they are ignored.
3569 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3570 than MAX_OFFSET will not be walked.
3572 If F returns a non-zero value, the traversal ceases, and that value
3573 is returned. Otherwise, returns zero. */
3576 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3578 subobject_offset_fn f;
3586 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3588 if (max_offset && INT_CST_LT (max_offset, offset))
3591 if (CLASS_TYPE_P (type))
3596 /* Record the location of TYPE. */
3597 r = (*f) (type, offset, offsets);
3601 /* Iterate through the direct base classes of TYPE. */
3602 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3604 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3607 && TREE_VIA_VIRTUAL (binfo)
3608 && !BINFO_PRIMARY_P (binfo))
3611 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3613 size_binop (PLUS_EXPR,
3615 BINFO_OFFSET (binfo)),
3623 /* Iterate through the fields of TYPE. */
3624 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3625 if (TREE_CODE (field) == FIELD_DECL)
3627 r = walk_subobject_offsets (TREE_TYPE (field),
3629 size_binop (PLUS_EXPR,
3631 DECL_FIELD_OFFSET (field)),
3639 else if (TREE_CODE (type) == ARRAY_TYPE)
3641 tree domain = TYPE_DOMAIN (type);
3644 /* Step through each of the elements in the array. */
3645 for (index = size_zero_node;
3646 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3647 index = size_binop (PLUS_EXPR, index, size_one_node))
3649 r = walk_subobject_offsets (TREE_TYPE (type),
3657 offset = size_binop (PLUS_EXPR, offset,
3658 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3659 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3660 there's no point in iterating through the remaining
3661 elements of the array. */
3662 if (max_offset && INT_CST_LT (max_offset, offset))
3670 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3671 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3675 record_subobject_offsets (type, offset, offsets, vbases_p)
3681 walk_subobject_offsets (type, record_subobject_offset, offset,
3682 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3685 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3686 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3687 virtual bases of TYPE are examined. */
3690 layout_conflict_p (type, offset, offsets, vbases_p)
3696 splay_tree_node max_node;
3698 /* Get the node in OFFSETS that indicates the maximum offset where
3699 an empty subobject is located. */
3700 max_node = splay_tree_max (offsets);
3701 /* If there aren't any empty subobjects, then there's no point in
3702 performing this check. */
3706 return walk_subobject_offsets (type, check_subobject_offset, offset,
3707 offsets, (tree) (max_node->key),
3711 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3712 non-static data member of the type indicated by RLI. BINFO is the
3713 binfo corresponding to the base subobject, OFFSETS maps offsets to
3714 types already located at those offsets. T is the most derived
3715 type. This function determines the position of the DECL. */
3718 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3719 record_layout_info rli;
3725 tree offset = NULL_TREE;
3726 tree type = TREE_TYPE (decl);
3727 /* If we are laying out a base class, rather than a field, then
3728 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3729 int field_p = !DECL_ARTIFICIAL (decl);
3731 /* Try to place the field. It may take more than one try if we have
3732 a hard time placing the field without putting two objects of the
3733 same type at the same address. */
3736 struct record_layout_info_s old_rli = *rli;
3738 /* Place this field. */
3739 place_field (rli, decl);
3740 offset = byte_position (decl);
3742 /* We have to check to see whether or not there is already
3743 something of the same type at the offset we're about to use.
3747 struct T : public S { int i; };
3748 struct U : public S, public T {};
3750 Here, we put S at offset zero in U. Then, we can't put T at
3751 offset zero -- its S component would be at the same address
3752 as the S we already allocated. So, we have to skip ahead.
3753 Since all data members, including those whose type is an
3754 empty class, have non-zero size, any overlap can happen only
3755 with a direct or indirect base-class -- it can't happen with
3757 if (layout_conflict_p (TREE_TYPE (decl),
3762 /* Strip off the size allocated to this field. That puts us
3763 at the first place we could have put the field with
3764 proper alignment. */
3767 /* Bump up by the alignment required for the type. */
3769 = size_binop (PLUS_EXPR, rli->bitpos,
3771 ? CLASSTYPE_ALIGN (type)
3772 : TYPE_ALIGN (type)));
3773 normalize_rli (rli);
3776 /* There was no conflict. We're done laying out this field. */
3780 /* Now that we know where it will be placed, update its
3782 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3783 propagate_binfo_offsets (binfo,
3784 convert (ssizetype, offset), t);
3787 /* Layout the empty base BINFO. EOC indicates the byte currently just
3788 past the end of the class, and should be correctly aligned for a
3789 class of the type indicated by BINFO; OFFSETS gives the offsets of
3790 the empty bases allocated so far. T is the most derived
3791 type. Return non-zero iff we added it at the end. */
3794 layout_empty_base (binfo, eoc, offsets, t)
3801 tree basetype = BINFO_TYPE (binfo);
3804 /* This routine should only be used for empty classes. */
3805 my_friendly_assert (is_empty_class (basetype), 20000321);
3806 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3808 /* This is an empty base class. We first try to put it at offset
3810 if (layout_conflict_p (BINFO_TYPE (binfo),
3811 BINFO_OFFSET (binfo),
3815 /* That didn't work. Now, we move forward from the next
3816 available spot in the class. */
3818 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3821 if (!layout_conflict_p (BINFO_TYPE (binfo),
3822 BINFO_OFFSET (binfo),
3825 /* We finally found a spot where there's no overlap. */
3828 /* There's overlap here, too. Bump along to the next spot. */
3829 propagate_binfo_offsets (binfo, alignment, t);
3835 /* Build a FIELD_DECL for the base given by BINFO in the class
3836 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3837 *BASE_ALIGN is a running maximum of the alignments of any base
3838 class. OFFSETS gives the location of empty base subobjects. T is
3839 the most derived type. Return non-zero if the new object cannot be
3843 build_base_field (rli, binfo, empty_p, offsets, t)
3844 record_layout_info rli;
3850 tree basetype = BINFO_TYPE (binfo);
3854 if (!COMPLETE_TYPE_P (basetype))
3855 /* This error is now reported in xref_tag, thus giving better
3856 location information. */
3859 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3860 DECL_ARTIFICIAL (decl) = 1;
3861 DECL_FIELD_CONTEXT (decl) = rli->t;
3862 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3863 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3864 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3865 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3867 if (!integer_zerop (DECL_SIZE (decl)))
3869 /* The containing class is non-empty because it has a non-empty
3873 /* Try to place the field. It may take more than one try if we
3874 have a hard time placing the field without putting two
3875 objects of the same type at the same address. */
3876 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3880 unsigned HOST_WIDE_INT eoc;
3882 /* On some platforms (ARM), even empty classes will not be
3884 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3885 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3886 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3889 /* Record the offsets of BINFO and its base subobjects. */
3890 record_subobject_offsets (BINFO_TYPE (binfo),
3891 BINFO_OFFSET (binfo),
3897 /* Layout all of the non-virtual base classes. Record empty
3898 subobjects in OFFSETS. T is the most derived type. Return
3899 non-zero if the type cannot be nearly empty. */
3902 build_base_fields (rli, empty_p, offsets, t)
3903 record_layout_info rli;
3908 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3911 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3915 /* The primary base class is always allocated first. */
3916 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3917 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3918 empty_p, offsets, t);
3920 /* Now allocate the rest of the bases. */
3921 for (i = 0; i < n_baseclasses; ++i)
3925 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3927 /* The primary base was already allocated above, so we don't
3928 need to allocate it again here. */
3929 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3932 /* A primary virtual base class is allocated just like any other
3933 base class, but a non-primary virtual base is allocated
3934 later, in layout_virtual_bases. */
3935 if (TREE_VIA_VIRTUAL (base_binfo)
3936 && !BINFO_PRIMARY_P (base_binfo))
3939 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3944 /* Go through the TYPE_METHODS of T issuing any appropriate
3945 diagnostics, figuring out which methods override which other
3946 methods, and so forth. */
3954 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3956 GNU_xref_member (current_class_name, x);
3958 /* If this was an evil function, don't keep it in class. */
3959 if (DECL_ASSEMBLER_NAME_SET_P (x)
3960 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3963 check_for_override (x, t);
3964 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3965 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3967 /* The name of the field is the original field name
3968 Save this in auxiliary field for later overloading. */
3969 if (DECL_VINDEX (x))
3971 TYPE_POLYMORPHIC_P (t) = 1;
3972 if (DECL_PURE_VIRTUAL_P (x))
3973 CLASSTYPE_PURE_VIRTUALS (t)
3974 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3979 /* FN is a constructor or destructor. Clone the declaration to create
3980 a specialized in-charge or not-in-charge version, as indicated by
3984 build_clone (fn, name)
3991 /* Copy the function. */
3992 clone = copy_decl (fn);
3993 /* Remember where this function came from. */
3994 DECL_CLONED_FUNCTION (clone) = fn;
3995 DECL_ABSTRACT_ORIGIN (clone) = fn;
3996 /* Reset the function name. */
3997 DECL_NAME (clone) = name;
3998 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3999 /* There's no pending inline data for this function. */
4000 DECL_PENDING_INLINE_INFO (clone) = NULL;
4001 DECL_PENDING_INLINE_P (clone) = 0;
4002 /* And it hasn't yet been deferred. */
4003 DECL_DEFERRED_FN (clone) = 0;
4005 /* The base-class destructor is not virtual. */
4006 if (name == base_dtor_identifier)
4008 DECL_VIRTUAL_P (clone) = 0;
4009 if (TREE_CODE (clone) != TEMPLATE_DECL)
4010 DECL_VINDEX (clone) = NULL_TREE;
4013 /* If there was an in-charge parameter, drop it from the function
4015 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4021 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4022 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4023 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4024 /* Skip the `this' parameter. */
4025 parmtypes = TREE_CHAIN (parmtypes);
4026 /* Skip the in-charge parameter. */
4027 parmtypes = TREE_CHAIN (parmtypes);
4028 /* And the VTT parm, in a complete [cd]tor. */
4029 if (DECL_HAS_VTT_PARM_P (fn)
4030 && ! DECL_NEEDS_VTT_PARM_P (clone))
4031 parmtypes = TREE_CHAIN (parmtypes);
4032 /* If this is subobject constructor or destructor, add the vtt
4035 = build_cplus_method_type (basetype,
4036 TREE_TYPE (TREE_TYPE (clone)),
4039 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4043 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4044 aren't function parameters; those are the template parameters. */
4045 if (TREE_CODE (clone) != TEMPLATE_DECL)
4047 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4048 /* Remove the in-charge parameter. */
4049 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4051 TREE_CHAIN (DECL_ARGUMENTS (clone))
4052 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4053 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4055 /* And the VTT parm, in a complete [cd]tor. */
4056 if (DECL_HAS_VTT_PARM_P (fn))
4058 if (DECL_NEEDS_VTT_PARM_P (clone))
4059 DECL_HAS_VTT_PARM_P (clone) = 1;
4062 TREE_CHAIN (DECL_ARGUMENTS (clone))
4063 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4064 DECL_HAS_VTT_PARM_P (clone) = 0;
4068 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4070 DECL_CONTEXT (parms) = clone;
4071 copy_lang_decl (parms);
4075 /* Create the RTL for this function. */
4076 SET_DECL_RTL (clone, NULL_RTX);
4077 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4079 /* Make it easy to find the CLONE given the FN. */
4080 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4081 TREE_CHAIN (fn) = clone;
4083 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4084 if (TREE_CODE (clone) == TEMPLATE_DECL)
4088 DECL_TEMPLATE_RESULT (clone)
4089 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4090 result = DECL_TEMPLATE_RESULT (clone);
4091 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4092 DECL_TI_TEMPLATE (result) = clone;
4094 else if (DECL_DEFERRED_FN (fn))
4100 /* Produce declarations for all appropriate clones of FN. If
4101 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4102 CLASTYPE_METHOD_VEC as well. */
4105 clone_function_decl (fn, update_method_vec_p)
4107 int update_method_vec_p;
4111 /* Avoid inappropriate cloning. */
4113 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4116 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4118 /* For each constructor, we need two variants: an in-charge version
4119 and a not-in-charge version. */
4120 clone = build_clone (fn, complete_ctor_identifier);
4121 if (update_method_vec_p)
4122 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4123 clone = build_clone (fn, base_ctor_identifier);
4124 if (update_method_vec_p)
4125 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4129 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4131 /* For each destructor, we need three variants: an in-charge
4132 version, a not-in-charge version, and an in-charge deleting
4133 version. We clone the deleting version first because that
4134 means it will go second on the TYPE_METHODS list -- and that
4135 corresponds to the correct layout order in the virtual
4138 For a non-virtual destructor, we do not build a deleting
4140 if (DECL_VIRTUAL_P (fn))
4142 clone = build_clone (fn, deleting_dtor_identifier);
4143 if (update_method_vec_p)
4144 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4146 clone = build_clone (fn, complete_dtor_identifier);
4147 if (update_method_vec_p)
4148 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4149 clone = build_clone (fn, base_dtor_identifier);
4150 if (update_method_vec_p)
4151 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4154 /* Note that this is an abstract function that is never emitted. */
4155 DECL_ABSTRACT (fn) = 1;
4158 /* DECL is an in charge constructor, which is being defined. This will
4159 have had an in class declaration, from whence clones were
4160 declared. An out-of-class definition can specify additional default
4161 arguments. As it is the clones that are involved in overload
4162 resolution, we must propagate the information from the DECL to its
4166 adjust_clone_args (decl)
4171 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4172 clone = TREE_CHAIN (clone))
4174 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4175 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4176 tree decl_parms, clone_parms;
4178 clone_parms = orig_clone_parms;
4180 /* Skip the 'this' parameter. */
4181 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4182 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4184 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4185 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4186 if (DECL_HAS_VTT_PARM_P (decl))
4187 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4189 clone_parms = orig_clone_parms;
4190 if (DECL_HAS_VTT_PARM_P (clone))
4191 clone_parms = TREE_CHAIN (clone_parms);
4193 for (decl_parms = orig_decl_parms; decl_parms;
4194 decl_parms = TREE_CHAIN (decl_parms),
4195 clone_parms = TREE_CHAIN (clone_parms))
4197 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4198 TREE_TYPE (clone_parms)), 20010424);
4200 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4202 /* A default parameter has been added. Adjust the
4203 clone's parameters. */
4204 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4205 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4208 clone_parms = orig_decl_parms;
4210 if (DECL_HAS_VTT_PARM_P (clone))
4212 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4213 TREE_VALUE (orig_clone_parms),
4215 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4217 type = build_cplus_method_type (basetype,
4218 TREE_TYPE (TREE_TYPE (clone)),
4221 type = build_exception_variant (type, exceptions);
4222 TREE_TYPE (clone) = type;
4224 clone_parms = NULL_TREE;
4228 my_friendly_assert (!clone_parms, 20010424);
4232 /* For each of the constructors and destructors in T, create an
4233 in-charge and not-in-charge variant. */
4236 clone_constructors_and_destructors (t)
4241 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4243 if (!CLASSTYPE_METHOD_VEC (t))
4246 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4247 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4248 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4249 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4252 /* Remove all zero-width bit-fields from T. */
4255 remove_zero_width_bit_fields (t)
4260 fieldsp = &TYPE_FIELDS (t);
4263 if (TREE_CODE (*fieldsp) == FIELD_DECL
4264 && DECL_C_BIT_FIELD (*fieldsp)
4265 && DECL_INITIAL (*fieldsp))
4266 *fieldsp = TREE_CHAIN (*fieldsp);
4268 fieldsp = &TREE_CHAIN (*fieldsp);
4272 /* Returns TRUE iff we need a cookie when dynamically allocating an
4273 array whose elements have the indicated class TYPE. */
4276 type_requires_array_cookie (type)
4280 bool has_two_argument_delete_p = false;
4282 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4284 /* If there's a non-trivial destructor, we need a cookie. In order
4285 to iterate through the array calling the destructor for each
4286 element, we'll have to know how many elements there are. */
4287 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4290 /* If the usual deallocation function is a two-argument whose second
4291 argument is of type `size_t', then we have to pass the size of
4292 the array to the deallocation function, so we will need to store
4294 fns = lookup_fnfields (TYPE_BINFO (type),
4295 ansi_opname (VEC_DELETE_EXPR),
4297 /* If there are no `operator []' members, or the lookup is
4298 ambiguous, then we don't need a cookie. */
4299 if (!fns || fns == error_mark_node)
4301 /* Loop through all of the functions. */
4302 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
4307 /* Select the current function. */
4308 fn = OVL_CURRENT (fns);
4309 /* See if this function is a one-argument delete function. If
4310 it is, then it will be the usual deallocation function. */
4311 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4312 if (second_parm == void_list_node)
4314 /* Otherwise, if we have a two-argument function and the second
4315 argument is `size_t', it will be the usual deallocation
4316 function -- unless there is one-argument function, too. */
4317 if (TREE_CHAIN (second_parm) == void_list_node
4318 && same_type_p (TREE_VALUE (second_parm), sizetype))
4319 has_two_argument_delete_p = true;
4322 return has_two_argument_delete_p;
4325 /* Check the validity of the bases and members declared in T. Add any
4326 implicitly-generated functions (like copy-constructors and
4327 assignment operators). Compute various flag bits (like
4328 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4329 level: i.e., independently of the ABI in use. */
4332 check_bases_and_members (t, empty_p)
4336 /* Nonzero if we are not allowed to generate a default constructor
4338 int cant_have_default_ctor;
4339 /* Nonzero if the implicitly generated copy constructor should take
4340 a non-const reference argument. */
4341 int cant_have_const_ctor;
4342 /* Nonzero if the the implicitly generated assignment operator
4343 should take a non-const reference argument. */
4344 int no_const_asn_ref;
4347 /* By default, we use const reference arguments and generate default
4349 cant_have_default_ctor = 0;
4350 cant_have_const_ctor = 0;
4351 no_const_asn_ref = 0;
4353 /* Assume that the class is nearly empty; we'll clear this flag if
4354 it turns out not to be nearly empty. */
4355 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4357 /* Check all the base-classes. */
4358 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4361 /* Check all the data member declarations. */
4362 check_field_decls (t, &access_decls, empty_p,
4363 &cant_have_default_ctor,
4364 &cant_have_const_ctor,
4367 /* Check all the method declarations. */
4370 /* A nearly-empty class has to be vptr-containing; a nearly empty
4371 class contains just a vptr. */
4372 if (!TYPE_CONTAINS_VPTR_P (t))
4373 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4375 /* Do some bookkeeping that will guide the generation of implicitly
4376 declared member functions. */
4377 TYPE_HAS_COMPLEX_INIT_REF (t)
4378 |= (TYPE_HAS_INIT_REF (t)
4379 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4380 || TYPE_POLYMORPHIC_P (t));
4381 TYPE_NEEDS_CONSTRUCTING (t)
4382 |= (TYPE_HAS_CONSTRUCTOR (t)
4383 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4384 || TYPE_POLYMORPHIC_P (t));
4385 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4386 || TYPE_POLYMORPHIC_P (t));
4387 CLASSTYPE_NON_POD_P (t)
4388 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4389 || TYPE_HAS_ASSIGN_REF (t));
4390 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4391 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4392 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4394 /* Synthesize any needed methods. Note that methods will be synthesized
4395 for anonymous unions; grok_x_components undoes that. */
4396 add_implicitly_declared_members (t, cant_have_default_ctor,
4397 cant_have_const_ctor,
4400 /* Create the in-charge and not-in-charge variants of constructors
4402 clone_constructors_and_destructors (t);
4404 /* Process the using-declarations. */
4405 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4406 handle_using_decl (TREE_VALUE (access_decls), t);
4408 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4409 finish_struct_methods (t);
4411 /* Figure out whether or not we will need a cookie when dynamically
4412 allocating an array of this type. */
4413 TYPE_LANG_SPECIFIC (t)->vec_new_uses_cookie
4414 = type_requires_array_cookie (t);
4417 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4418 accordingly. If a new vfield was created (because T doesn't have a
4419 primary base class), then the newly created field is returned. It
4420 is not added to the TYPE_FIELDS list; it is the caller's
4421 responsibility to do that. */
4424 create_vtable_ptr (t, empty_p, vfuns_p,
4425 new_virtuals_p, overridden_virtuals_p)
4429 tree *new_virtuals_p;
4430 tree *overridden_virtuals_p;
4434 /* Loop over the virtual functions, adding them to our various
4436 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4437 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4438 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4441 /* If we couldn't find an appropriate base class, create a new field
4442 here. Even if there weren't any new virtual functions, we might need a
4443 new virtual function table if we're supposed to include vptrs in
4444 all classes that need them. */
4445 if (!TYPE_VFIELD (t) && (*vfuns_p || TYPE_CONTAINS_VPTR_P (t)))
4447 /* We build this decl with vtbl_ptr_type_node, which is a
4448 `vtable_entry_type*'. It might seem more precise to use
4449 `vtable_entry_type (*)[N]' where N is the number of firtual
4450 functions. However, that would require the vtable pointer in
4451 base classes to have a different type than the vtable pointer
4452 in derived classes. We could make that happen, but that
4453 still wouldn't solve all the problems. In particular, the
4454 type-based alias analysis code would decide that assignments
4455 to the base class vtable pointer can't alias assignments to
4456 the derived class vtable pointer, since they have different
4457 types. Thus, in an derived class destructor, where the base
4458 class constructor was inlined, we could generate bad code for
4459 setting up the vtable pointer.
4461 Therefore, we use one type for all vtable pointers. We still
4462 use a type-correct type; it's just doesn't indicate the array
4463 bounds. That's better than using `void*' or some such; it's
4464 cleaner, and it let's the alias analysis code know that these
4465 stores cannot alias stores to void*! */
4468 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4469 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4470 DECL_VIRTUAL_P (field) = 1;
4471 DECL_ARTIFICIAL (field) = 1;
4472 DECL_FIELD_CONTEXT (field) = t;
4473 DECL_FCONTEXT (field) = t;
4474 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4475 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4477 TYPE_VFIELD (t) = field;
4479 /* This class is non-empty. */
4482 if (CLASSTYPE_N_BASECLASSES (t))
4483 /* If there were any baseclasses, they can't possibly be at
4484 offset zero any more, because that's where the vtable
4485 pointer is. So, converting to a base class is going to
4487 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4495 /* Fixup the inline function given by INFO now that the class is
4499 fixup_pending_inline (fn)
4502 if (DECL_PENDING_INLINE_INFO (fn))
4504 tree args = DECL_ARGUMENTS (fn);
4507 DECL_CONTEXT (args) = fn;
4508 args = TREE_CHAIN (args);
4513 /* Fixup the inline methods and friends in TYPE now that TYPE is
4517 fixup_inline_methods (type)
4520 tree method = TYPE_METHODS (type);
4522 if (method && TREE_CODE (method) == TREE_VEC)
4524 if (TREE_VEC_ELT (method, 1))
4525 method = TREE_VEC_ELT (method, 1);
4526 else if (TREE_VEC_ELT (method, 0))
4527 method = TREE_VEC_ELT (method, 0);
4529 method = TREE_VEC_ELT (method, 2);
4532 /* Do inline member functions. */
4533 for (; method; method = TREE_CHAIN (method))
4534 fixup_pending_inline (method);
4537 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4539 method = TREE_CHAIN (method))
4540 fixup_pending_inline (TREE_VALUE (method));
4541 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4544 /* Add OFFSET to all base types of BINFO which is a base in the
4545 hierarchy dominated by T.
4547 OFFSET, which is a type offset, is number of bytes. */
4550 propagate_binfo_offsets (binfo, offset, t)
4558 /* Update BINFO's offset. */
4559 BINFO_OFFSET (binfo)
4560 = convert (sizetype,
4561 size_binop (PLUS_EXPR,
4562 convert (ssizetype, BINFO_OFFSET (binfo)),
4565 /* Find the primary base class. */
4566 primary_binfo = get_primary_binfo (binfo);
4568 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4570 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4574 /* On the first time through the loop, do the primary base.
4575 Because the primary base need not be an immediate base, we
4576 must handle the primary base specially. */
4582 base_binfo = primary_binfo;
4586 base_binfo = BINFO_BASETYPE (binfo, i);
4587 /* Don't do the primary base twice. */
4588 if (base_binfo == primary_binfo)
4592 /* Skip virtual bases that aren't our canonical primary base. */
4593 if (TREE_VIA_VIRTUAL (base_binfo)
4594 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4595 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4598 propagate_binfo_offsets (base_binfo, offset, t);
4602 /* Called via dfs_walk from layout_virtual bases. */
4605 dfs_set_offset_for_unshared_vbases (binfo, data)
4609 /* If this is a virtual base, make sure it has the same offset as
4610 the shared copy. If it's a primary base, then we know it's
4612 if (TREE_VIA_VIRTUAL (binfo))
4614 tree t = (tree) data;
4618 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4621 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4622 propagate_binfo_offsets (binfo, offset, t);
4629 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4630 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4631 empty subobjects of T. */
4634 layout_virtual_bases (t, offsets)
4639 unsigned HOST_WIDE_INT dsize;
4640 unsigned HOST_WIDE_INT eoc;
4642 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4645 #ifdef STRUCTURE_SIZE_BOUNDARY
4646 /* Packed structures don't need to have minimum size. */
4647 if (! TYPE_PACKED (t))
4648 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4651 /* DSIZE is the size of the class without the virtual bases. */
4652 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4654 /* Make every class have alignment of at least one. */
4655 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4657 /* Go through the virtual bases, allocating space for each virtual
4658 base that is not already a primary base class. These are
4659 allocated in inheritance graph order. */
4660 for (vbases = TYPE_BINFO (t);
4662 vbases = TREE_CHAIN (vbases))
4666 if (!TREE_VIA_VIRTUAL (vbases))
4668 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4670 if (!BINFO_PRIMARY_P (vbase))
4672 /* This virtual base is not a primary base of any class in the
4673 hierarchy, so we have to add space for it. */
4675 unsigned int desired_align;
4677 basetype = BINFO_TYPE (vbase);
4679 desired_align = CLASSTYPE_ALIGN (basetype);
4680 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4682 /* Add padding so that we can put the virtual base class at an
4683 appropriately aligned offset. */
4684 dsize = CEIL (dsize, desired_align) * desired_align;
4686 /* We try to squish empty virtual bases in just like
4687 ordinary empty bases. */
4688 if (is_empty_class (basetype))
4689 layout_empty_base (vbase,
4690 size_int (CEIL (dsize, BITS_PER_UNIT)),
4696 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4697 offset = size_diffop (offset,
4699 BINFO_OFFSET (vbase)));
4701 /* And compute the offset of the virtual base. */
4702 propagate_binfo_offsets (vbase, offset, t);
4703 /* Every virtual baseclass takes a least a UNIT, so that
4704 we can take it's address and get something different
4706 dsize += MAX (BITS_PER_UNIT,
4707 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4710 /* Keep track of the offsets assigned to this virtual base. */
4711 record_subobject_offsets (BINFO_TYPE (vbase),
4712 BINFO_OFFSET (vbase),
4718 /* Now, go through the TYPE_BINFO hierarchy, setting the
4719 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4720 bases and their direct and indirect bases. The ambiguity checks
4721 in get_base_distance depend on the BINFO_OFFSETs being set
4723 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4725 /* If we had empty base classes that protruded beyond the end of the
4726 class, we didn't update DSIZE above; we were hoping to overlay
4727 multiple such bases at the same location. */
4728 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4729 if (eoc * BITS_PER_UNIT > dsize)
4730 dsize = eoc * BITS_PER_UNIT;
4732 /* Now, make sure that the total size of the type is a multiple of
4734 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4735 TYPE_SIZE (t) = bitsize_int (dsize);
4736 TYPE_SIZE_UNIT (t) = convert (sizetype,
4737 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4738 bitsize_unit_node));
4740 /* Check for ambiguous virtual bases. */
4742 for (vbases = CLASSTYPE_VBASECLASSES (t);
4744 vbases = TREE_CHAIN (vbases))
4746 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4747 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4748 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4753 /* Returns the offset of the byte just past the end of the base class
4754 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4755 only non-virtual bases are included. */
4757 static unsigned HOST_WIDE_INT
4758 end_of_class (t, include_virtuals_p)
4760 int include_virtuals_p;
4762 unsigned HOST_WIDE_INT result = 0;
4765 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4770 unsigned HOST_WIDE_INT end_of_base;
4772 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4774 if (!include_virtuals_p
4775 && TREE_VIA_VIRTUAL (base_binfo)
4776 && !BINFO_PRIMARY_P (base_binfo))
4779 if (is_empty_class (BINFO_TYPE (base_binfo)))
4780 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4781 allocate some space for it. It cannot have virtual bases,
4782 so TYPE_SIZE_UNIT is fine. */
4783 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4785 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4786 offset = size_binop (PLUS_EXPR,
4787 BINFO_OFFSET (base_binfo),
4789 end_of_base = tree_low_cst (offset, /*pos=*/1);
4790 if (end_of_base > result)
4791 result = end_of_base;
4797 /* Warn about direct bases of T that are inaccessible because they are
4798 ambiguous. For example:
4801 struct T : public S {};
4802 struct U : public S, public T {};
4804 Here, `(S*) new U' is not allowed because there are two `S'
4808 warn_about_ambiguous_direct_bases (t)
4813 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4815 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4817 if (get_base_distance (basetype, t, 0, NULL) == -2)
4818 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4823 /* Compare two INTEGER_CSTs K1 and K2. */
4826 splay_tree_compare_integer_csts (k1, k2)
4830 return tree_int_cst_compare ((tree) k1, (tree) k2);
4833 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4834 BINFO_OFFSETs for all of the base-classes. Position the vtable
4838 layout_class_type (t, empty_p, vfuns_p,
4839 new_virtuals_p, overridden_virtuals_p)
4843 tree *new_virtuals_p;
4844 tree *overridden_virtuals_p;
4846 tree non_static_data_members;
4849 record_layout_info rli;
4850 unsigned HOST_WIDE_INT eoc;
4851 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4852 types that appear at that offset. */
4853 splay_tree empty_base_offsets;
4855 /* Keep track of the first non-static data member. */
4856 non_static_data_members = TYPE_FIELDS (t);
4858 /* Start laying out the record. */
4859 rli = start_record_layout (t);
4861 /* If possible, we reuse the virtual function table pointer from one
4862 of our base classes. */
4863 determine_primary_base (t, vfuns_p);
4865 /* Create a pointer to our virtual function table. */
4866 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4867 new_virtuals_p, overridden_virtuals_p);
4869 /* The vptr is always the first thing in the class. */
4872 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4873 place_field (rli, vptr);
4876 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4877 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4879 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4880 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4882 /* Layout the non-static data members. */
4883 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4888 /* We still pass things that aren't non-static data members to
4889 the back-end, in case it wants to do something with them. */
4890 if (TREE_CODE (field) != FIELD_DECL)
4892 place_field (rli, field);
4896 type = TREE_TYPE (field);
4898 /* If this field is a bit-field whose width is greater than its
4899 type, then there are some special rules for allocating
4901 if (DECL_C_BIT_FIELD (field)
4902 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4904 integer_type_kind itk;
4907 /* We must allocate the bits as if suitably aligned for the
4908 longest integer type that fits in this many bits. type
4909 of the field. Then, we are supposed to use the left over
4910 bits as additional padding. */
4911 for (itk = itk_char; itk != itk_none; ++itk)
4912 if (INT_CST_LT (DECL_SIZE (field),
4913 TYPE_SIZE (integer_types[itk])))
4916 /* ITK now indicates a type that is too large for the
4917 field. We have to back up by one to find the largest
4919 integer_type = integer_types[itk - 1];
4920 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4921 TYPE_SIZE (integer_type));
4922 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4923 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4924 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4927 padding = NULL_TREE;
4929 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4930 empty_base_offsets, t);
4932 /* If we needed additional padding after this field, add it
4938 padding_field = build_decl (FIELD_DECL,
4941 DECL_BIT_FIELD (padding_field) = 1;
4942 DECL_SIZE (padding_field) = padding;
4943 DECL_ALIGN (padding_field) = 1;
4944 DECL_USER_ALIGN (padding_field) = 0;
4945 layout_nonempty_base_or_field (rli, padding_field,
4947 empty_base_offsets, t);
4951 /* It might be the case that we grew the class to allocate a
4952 zero-sized base class. That won't be reflected in RLI, yet,
4953 because we are willing to overlay multiple bases at the same
4954 offset. However, now we need to make sure that RLI is big enough
4955 to reflect the entire class. */
4956 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4957 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4958 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4960 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4961 rli->bitpos = bitsize_zero_node;
4964 /* We make all structures have at least one element, so that they
4965 have non-zero size. The class may be empty even if it has
4966 basetypes. Therefore, we add the fake field after all the other
4967 fields; if there are already FIELD_DECLs on the list, their
4968 offsets will not be disturbed. */
4969 if (!eoc && *empty_p)
4973 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
4974 place_field (rli, padding);
4977 /* Let the back-end lay out the type. Note that at this point we
4978 have only included non-virtual base-classes; we will lay out the
4979 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
4980 this call are not necessarily correct; they are just the size and
4981 alignment when no virtual base clases are used. */
4982 finish_record_layout (rli);
4984 /* Delete all zero-width bit-fields from the list of fields. Now
4985 that the type is laid out they are no longer important. */
4986 remove_zero_width_bit_fields (t);
4988 /* Remember the size and alignment of the class before adding
4989 the virtual bases. */
4992 CLASSTYPE_SIZE (t) = bitsize_zero_node;
4993 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
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 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5057 a FUNCTION_DECL. Each of these functions is a virtual function
5058 declared in T that does not override any virtual function from a
5060 tree new_virtuals = NULL_TREE;
5061 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5062 except that each declaration here overrides the declaration from
5064 tree overridden_virtuals = NULL_TREE;
5069 if (COMPLETE_TYPE_P (t))
5071 if (IS_AGGR_TYPE (t))
5072 cp_error ("redefinition of `%#T'", t);
5074 my_friendly_abort (172);
5079 GNU_xref_decl (current_function_decl, t);
5081 /* If this type was previously laid out as a forward reference,
5082 make sure we lay it out again. */
5083 TYPE_SIZE (t) = NULL_TREE;
5084 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5085 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5087 CLASSTYPE_RTTI (t) = NULL_TREE;
5089 fixup_inline_methods (t);
5091 /* Do end-of-class semantic processing: checking the validity of the
5092 bases and members and add implicitly generated methods. */
5093 check_bases_and_members (t, &empty);
5095 /* Layout the class itself. */
5096 layout_class_type (t, &empty, &vfuns,
5097 &new_virtuals, &overridden_virtuals);
5099 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5100 vfield = TYPE_VFIELD (t);
5101 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5103 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5105 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5106 BINFO_TYPE (primary)),
5108 /* The vtable better be at the start. */
5109 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5111 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5114 vfield = copy_decl (vfield);
5115 DECL_FIELD_CONTEXT (vfield) = t;
5116 TYPE_VFIELD (t) = vfield;
5119 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5122 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5124 /* If we created a new vtbl pointer for this class, add it to the
5126 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5127 CLASSTYPE_VFIELDS (t)
5128 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5130 /* If necessary, create the primary vtable for this class. */
5131 if (new_virtuals || overridden_virtuals || TYPE_CONTAINS_VPTR_P (t))
5133 new_virtuals = nreverse (new_virtuals);
5134 /* We must enter these virtuals into the table. */
5135 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5136 build_primary_vtable (NULL_TREE, t);
5137 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5138 /* Here we know enough to change the type of our virtual
5139 function table, but we will wait until later this function. */
5140 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5142 /* If this type has basetypes with constructors, then those
5143 constructors might clobber the virtual function table. But
5144 they don't if the derived class shares the exact vtable of the base
5147 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5149 /* If we didn't need a new vtable, see if we should copy one from
5151 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5153 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5155 /* If this class uses a different vtable than its primary base
5156 then when we will need to initialize our vptr after the base
5157 class constructor runs. */
5158 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5159 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5162 if (TYPE_CONTAINS_VPTR_P (t))
5164 if (TYPE_BINFO_VTABLE (t))
5165 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5167 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5168 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5171 CLASSTYPE_VSIZE (t) = vfuns;
5172 /* Entries for virtual functions defined in the primary base are
5173 followed by entries for new functions unique to this class. */
5174 TYPE_BINFO_VIRTUALS (t)
5175 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5176 /* Finally, add entries for functions that override virtuals
5177 from non-primary bases. */
5178 TYPE_BINFO_VIRTUALS (t)
5179 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5182 finish_struct_bits (t);
5184 /* Complete the rtl for any static member objects of the type we're
5186 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5187 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5188 && TREE_TYPE (x) == t)
5189 DECL_MODE (x) = TYPE_MODE (t);
5191 /* Done with FIELDS...now decide whether to sort these for
5192 faster lookups later.
5194 The C front-end only does this when n_fields > 15. We use
5195 a smaller number because most searches fail (succeeding
5196 ultimately as the search bores through the inheritance
5197 hierarchy), and we want this failure to occur quickly. */
5199 n_fields = count_fields (TYPE_FIELDS (t));
5202 tree field_vec = make_tree_vec (n_fields);
5203 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5204 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5205 (int (*)(const void *, const void *))field_decl_cmp);
5206 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5207 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5208 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5211 if (TYPE_HAS_CONSTRUCTOR (t))
5213 tree vfields = CLASSTYPE_VFIELDS (t);
5217 /* Mark the fact that constructor for T
5218 could affect anybody inheriting from T
5219 who wants to initialize vtables for VFIELDS's type. */
5220 if (VF_DERIVED_VALUE (vfields))
5221 TREE_ADDRESSABLE (vfields) = 1;
5222 vfields = TREE_CHAIN (vfields);
5226 /* Make the rtl for any new vtables we have created, and unmark
5227 the base types we marked. */
5230 /* Build the VTT for T. */
5233 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5234 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5235 cp_warning ("`%#T' has virtual functions but non-virtual destructor", t);
5237 hack_incomplete_structures (t);
5239 if (warn_overloaded_virtual)
5242 maybe_suppress_debug_info (t);
5244 dump_class_hierarchy (t);
5246 /* Finish debugging output for this type. */
5247 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5250 /* When T was built up, the member declarations were added in reverse
5251 order. Rearrange them to declaration order. */
5254 unreverse_member_declarations (t)
5261 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5262 reverse order. Put them in declaration order now. */
5263 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5264 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5266 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5267 reverse order, so we can't just use nreverse. */
5269 for (x = TYPE_FIELDS (t);
5270 x && TREE_CODE (x) != TYPE_DECL;
5273 next = TREE_CHAIN (x);
5274 TREE_CHAIN (x) = prev;
5279 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5281 TYPE_FIELDS (t) = prev;
5286 finish_struct (t, attributes)
5289 const char *saved_filename = input_filename;
5290 int saved_lineno = lineno;
5292 /* Now that we've got all the field declarations, reverse everything
5294 unreverse_member_declarations (t);
5296 cplus_decl_attributes (&t, attributes, NULL_TREE, 0);
5298 /* Nadger the current location so that diagnostics point to the start of
5299 the struct, not the end. */
5300 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5301 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5303 if (processing_template_decl)
5305 finish_struct_methods (t);
5306 TYPE_SIZE (t) = bitsize_zero_node;
5309 finish_struct_1 (t);
5311 input_filename = saved_filename;
5312 lineno = saved_lineno;
5314 TYPE_BEING_DEFINED (t) = 0;
5316 if (current_class_type)
5319 error ("trying to finish struct, but kicked out due to previous parse errors.");
5321 if (processing_template_decl)
5323 tree scope = current_scope ();
5324 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5325 add_stmt (build_min (TAG_DEFN, t));
5331 /* Return the dynamic type of INSTANCE, if known.
5332 Used to determine whether the virtual function table is needed
5335 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5336 of our knowledge of its type. *NONNULL should be initialized
5337 before this function is called. */
5340 fixed_type_or_null (instance, nonnull, cdtorp)
5345 switch (TREE_CODE (instance))
5348 /* Check that we are not going through a cast of some sort. */
5349 if (TREE_TYPE (instance)
5350 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5351 instance = TREE_OPERAND (instance, 0);
5352 /* fall through... */
5354 /* This is a call to a constructor, hence it's never zero. */
5355 if (TREE_HAS_CONSTRUCTOR (instance))
5359 return TREE_TYPE (instance);
5364 /* This is a call to a constructor, hence it's never zero. */
5365 if (TREE_HAS_CONSTRUCTOR (instance))
5369 return TREE_TYPE (instance);
5371 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5378 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5379 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5380 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5381 /* Propagate nonnull. */
5382 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5387 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5392 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5395 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5399 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5400 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5404 return TREE_TYPE (TREE_TYPE (instance));
5406 /* fall through... */
5409 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5413 return TREE_TYPE (instance);
5415 else if (instance == current_class_ptr)
5420 /* if we're in a ctor or dtor, we know our type. */
5421 if (DECL_LANG_SPECIFIC (current_function_decl)
5422 && (DECL_CONSTRUCTOR_P (current_function_decl)
5423 || DECL_DESTRUCTOR_P (current_function_decl)))
5427 return TREE_TYPE (TREE_TYPE (instance));
5430 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5432 /* Reference variables should be references to objects. */
5443 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5444 to the static type. We also handle the case where INSTANCE is really
5445 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5446 is known, but this might not be the most derived base of the original object,
5447 and hence virtual bases may not be layed out according to this type.
5449 Used to determine whether the virtual function table is needed
5452 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5453 of our knowledge of its type. *NONNULL should be initialized
5454 before this function is called. */
5457 resolves_to_fixed_type_p (instance, nonnull)
5461 tree t = TREE_TYPE (instance);
5464 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5465 if (fixed == NULL_TREE)
5467 if (POINTER_TYPE_P (t))
5469 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5471 return cdtorp ? -1 : 1;
5476 init_class_processing ()
5478 current_class_depth = 0;
5479 current_class_stack_size = 10;
5481 = (class_stack_node_t) xmalloc (current_class_stack_size
5482 * sizeof (struct class_stack_node));
5483 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5484 ggc_add_tree_varray_root (&local_classes, 1);
5486 access_default_node = build_int_2 (0, 0);
5487 access_public_node = build_int_2 (ak_public, 0);
5488 access_protected_node = build_int_2 (ak_protected, 0);
5489 access_private_node = build_int_2 (ak_private, 0);
5490 access_default_virtual_node = build_int_2 (4, 0);
5491 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5492 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5493 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5495 ridpointers[(int) RID_PUBLIC] = access_public_node;
5496 ridpointers[(int) RID_PRIVATE] = access_private_node;
5497 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5500 /* Set current scope to NAME. CODE tells us if this is a
5501 STRUCT, UNION, or ENUM environment.
5503 NAME may end up being NULL_TREE if this is an anonymous or
5504 late-bound struct (as in "struct { ... } foo;") */
5506 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5507 appropriate values, found by looking up the type definition of
5510 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5511 which can be seen locally to the class. They are shadowed by
5512 any subsequent local declaration (including parameter names).
5514 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5515 which have static meaning (i.e., static members, static
5516 member functions, enum declarations, etc).
5518 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5519 which can be seen locally to the class (as in 1), but
5520 know that we are doing this for declaration purposes
5521 (i.e. friend foo::bar (int)).
5523 So that we may avoid calls to lookup_name, we cache the _TYPE
5524 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5526 For multiple inheritance, we perform a two-pass depth-first search
5527 of the type lattice. The first pass performs a pre-order search,
5528 marking types after the type has had its fields installed in
5529 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5530 unmarks the marked types. If a field or member function name
5531 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5532 that name becomes `error_mark_node'. */
5535 pushclass (type, modify)
5539 type = TYPE_MAIN_VARIANT (type);
5541 /* Make sure there is enough room for the new entry on the stack. */
5542 if (current_class_depth + 1 >= current_class_stack_size)
5544 current_class_stack_size *= 2;
5546 = (class_stack_node_t) xrealloc (current_class_stack,
5547 current_class_stack_size
5548 * sizeof (struct class_stack_node));
5551 /* Insert a new entry on the class stack. */
5552 current_class_stack[current_class_depth].name = current_class_name;
5553 current_class_stack[current_class_depth].type = current_class_type;
5554 current_class_stack[current_class_depth].access = current_access_specifier;
5555 current_class_stack[current_class_depth].names_used = 0;
5556 current_class_depth++;
5558 /* Now set up the new type. */
5559 current_class_name = TYPE_NAME (type);
5560 if (TREE_CODE (current_class_name) == TYPE_DECL)
5561 current_class_name = DECL_NAME (current_class_name);
5562 current_class_type = type;
5564 /* By default, things in classes are private, while things in
5565 structures or unions are public. */
5566 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5567 ? access_private_node
5568 : access_public_node);
5570 if (previous_class_type != NULL_TREE
5571 && (type != previous_class_type
5572 || !COMPLETE_TYPE_P (previous_class_type))
5573 && current_class_depth == 1)
5575 /* Forcibly remove any old class remnants. */
5576 invalidate_class_lookup_cache ();
5579 /* If we're about to enter a nested class, clear
5580 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5581 if (modify && current_class_depth > 1)
5582 clear_identifier_class_values ();
5588 if (type != previous_class_type || current_class_depth > 1)
5589 push_class_decls (type);
5594 /* We are re-entering the same class we just left, so we
5595 don't have to search the whole inheritance matrix to find
5596 all the decls to bind again. Instead, we install the
5597 cached class_shadowed list, and walk through it binding
5598 names and setting up IDENTIFIER_TYPE_VALUEs. */
5599 set_class_shadows (previous_class_values);
5600 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5602 tree id = TREE_PURPOSE (item);
5603 tree decl = TREE_TYPE (item);
5605 push_class_binding (id, decl);
5606 if (TREE_CODE (decl) == TYPE_DECL)
5607 set_identifier_type_value (id, TREE_TYPE (decl));
5609 unuse_fields (type);
5612 storetags (CLASSTYPE_TAGS (type));
5616 /* When we exit a toplevel class scope, we save the
5617 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5618 reenter the class. Here, we've entered some other class, so we
5619 must invalidate our cache. */
5622 invalidate_class_lookup_cache ()
5626 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5627 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5628 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5630 previous_class_values = NULL_TREE;
5631 previous_class_type = NULL_TREE;
5634 /* Get out of the current class scope. If we were in a class scope
5635 previously, that is the one popped to. */
5641 /* Since poplevel_class does the popping of class decls nowadays,
5642 this really only frees the obstack used for these decls. */
5645 current_class_depth--;
5646 current_class_name = current_class_stack[current_class_depth].name;
5647 current_class_type = current_class_stack[current_class_depth].type;
5648 current_access_specifier = current_class_stack[current_class_depth].access;
5649 if (current_class_stack[current_class_depth].names_used)
5650 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5653 /* Returns 1 if current_class_type is either T or a nested type of T.
5654 We start looking from 1 because entry 0 is from global scope, and has
5658 currently_open_class (t)
5662 if (t == current_class_type)
5664 for (i = 1; i < current_class_depth; ++i)
5665 if (current_class_stack [i].type == t)
5670 /* If either current_class_type or one of its enclosing classes are derived
5671 from T, return the appropriate type. Used to determine how we found
5672 something via unqualified lookup. */
5675 currently_open_derived_class (t)
5680 if (DERIVED_FROM_P (t, current_class_type))
5681 return current_class_type;
5683 for (i = current_class_depth - 1; i > 0; --i)
5684 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5685 return current_class_stack[i].type;
5690 /* When entering a class scope, all enclosing class scopes' names with
5691 static meaning (static variables, static functions, types and enumerators)
5692 have to be visible. This recursive function calls pushclass for all
5693 enclosing class contexts until global or a local scope is reached.
5694 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5695 formal of the same name. */
5698 push_nested_class (type, modify)
5704 /* A namespace might be passed in error cases, like A::B:C. */
5705 if (type == NULL_TREE
5706 || type == error_mark_node
5707 || TREE_CODE (type) == NAMESPACE_DECL
5708 || ! IS_AGGR_TYPE (type)
5709 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5710 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5713 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5715 if (context && CLASS_TYPE_P (context))
5716 push_nested_class (context, 2);
5717 pushclass (type, modify);
5720 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5725 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5728 if (context && CLASS_TYPE_P (context))
5729 pop_nested_class ();
5732 /* Returns the number of extern "LANG" blocks we are nested within. */
5735 current_lang_depth ()
5737 return VARRAY_ACTIVE_SIZE (current_lang_base);
5740 /* Set global variables CURRENT_LANG_NAME to appropriate value
5741 so that behavior of name-mangling machinery is correct. */
5744 push_lang_context (name)
5747 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5749 if (name == lang_name_cplusplus)
5751 current_lang_name = name;
5753 else if (name == lang_name_java)
5755 current_lang_name = name;
5756 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5757 (See record_builtin_java_type in decl.c.) However, that causes
5758 incorrect debug entries if these types are actually used.
5759 So we re-enable debug output after extern "Java". */
5760 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5761 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5762 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5763 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5764 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5765 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5766 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5767 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5769 else if (name == lang_name_c)
5771 current_lang_name = name;
5774 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5777 /* Get out of the current language scope. */
5782 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5783 VARRAY_POP (current_lang_base);
5786 /* Type instantiation routines. */
5788 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5789 matches the TARGET_TYPE. If there is no satisfactory match, return
5790 error_mark_node, and issue an error message if COMPLAIN is
5791 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5792 If TEMPLATE_ONLY, the name of the overloaded function
5793 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5794 template arguments. */
5797 resolve_address_of_overloaded_function (target_type,
5808 tree explicit_targs;
5810 /* Here's what the standard says:
5814 If the name is a function template, template argument deduction
5815 is done, and if the argument deduction succeeds, the deduced
5816 arguments are used to generate a single template function, which
5817 is added to the set of overloaded functions considered.
5819 Non-member functions and static member functions match targets of
5820 type "pointer-to-function" or "reference-to-function." Nonstatic
5821 member functions match targets of type "pointer-to-member
5822 function;" the function type of the pointer to member is used to
5823 select the member function from the set of overloaded member
5824 functions. If a nonstatic member function is selected, the
5825 reference to the overloaded function name is required to have the
5826 form of a pointer to member as described in 5.3.1.
5828 If more than one function is selected, any template functions in
5829 the set are eliminated if the set also contains a non-template
5830 function, and any given template function is eliminated if the
5831 set contains a second template function that is more specialized
5832 than the first according to the partial ordering rules 14.5.5.2.
5833 After such eliminations, if any, there shall remain exactly one
5834 selected function. */
5837 int is_reference = 0;
5838 /* We store the matches in a TREE_LIST rooted here. The functions
5839 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5840 interoperability with most_specialized_instantiation. */
5841 tree matches = NULL_TREE;
5844 /* By the time we get here, we should be seeing only real
5845 pointer-to-member types, not the internal POINTER_TYPE to
5846 METHOD_TYPE representation. */
5847 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5848 && (TREE_CODE (TREE_TYPE (target_type))
5849 == METHOD_TYPE)), 0);
5851 if (TREE_CODE (overload) == COMPONENT_REF)
5852 overload = TREE_OPERAND (overload, 1);
5854 /* Check that the TARGET_TYPE is reasonable. */
5855 if (TYPE_PTRFN_P (target_type))
5857 else if (TYPE_PTRMEMFUNC_P (target_type))
5858 /* This is OK, too. */
5860 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5862 /* This is OK, too. This comes from a conversion to reference
5864 target_type = build_reference_type (target_type);
5871 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5872 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5873 return error_mark_node;
5876 /* If we can find a non-template function that matches, we can just
5877 use it. There's no point in generating template instantiations
5878 if we're just going to throw them out anyhow. But, of course, we
5879 can only do this when we don't *need* a template function. */
5884 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5886 tree fn = OVL_FUNCTION (fns);
5889 if (TREE_CODE (fn) == TEMPLATE_DECL)
5890 /* We're not looking for templates just yet. */
5893 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5895 /* We're looking for a non-static member, and this isn't
5896 one, or vice versa. */
5899 /* See if there's a match. */
5900 fntype = TREE_TYPE (fn);
5902 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5903 else if (!is_reference)
5904 fntype = build_pointer_type (fntype);
5906 if (can_convert_arg (target_type, fntype, fn))
5907 matches = tree_cons (fn, NULL_TREE, matches);
5911 /* Now, if we've already got a match (or matches), there's no need
5912 to proceed to the template functions. But, if we don't have a
5913 match we need to look at them, too. */
5916 tree target_fn_type;
5917 tree target_arg_types;
5918 tree target_ret_type;
5923 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5925 target_fn_type = TREE_TYPE (target_type);
5926 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5927 target_ret_type = TREE_TYPE (target_fn_type);
5929 /* Never do unification on the 'this' parameter. */
5930 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5931 target_arg_types = TREE_CHAIN (target_arg_types);
5933 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5935 tree fn = OVL_FUNCTION (fns);
5937 tree instantiation_type;
5940 if (TREE_CODE (fn) != TEMPLATE_DECL)
5941 /* We're only looking for templates. */
5944 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5946 /* We're not looking for a non-static member, and this is
5947 one, or vice versa. */
5950 /* Try to do argument deduction. */
5951 targs = make_tree_vec (DECL_NTPARMS (fn));
5952 if (fn_type_unification (fn, explicit_targs, targs,
5953 target_arg_types, target_ret_type,
5954 DEDUCE_EXACT, -1) != 0)
5955 /* Argument deduction failed. */
5958 /* Instantiate the template. */
5959 instantiation = instantiate_template (fn, targs);
5960 if (instantiation == error_mark_node)
5961 /* Instantiation failed. */
5964 /* See if there's a match. */
5965 instantiation_type = TREE_TYPE (instantiation);
5967 instantiation_type =
5968 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5969 else if (!is_reference)
5970 instantiation_type = build_pointer_type (instantiation_type);
5971 if (can_convert_arg (target_type, instantiation_type, instantiation))
5972 matches = tree_cons (instantiation, fn, matches);
5975 /* Now, remove all but the most specialized of the matches. */
5978 tree match = most_specialized_instantiation (matches);
5980 if (match != error_mark_node)
5981 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5985 /* Now we should have exactly one function in MATCHES. */
5986 if (matches == NULL_TREE)
5988 /* There were *no* matches. */
5991 cp_error ("no matches converting function `%D' to type `%#T'",
5992 DECL_NAME (OVL_FUNCTION (overload)),
5995 /* print_candidates expects a chain with the functions in
5996 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5997 so why be clever?). */
5998 for (; overload; overload = OVL_NEXT (overload))
5999 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6002 print_candidates (matches);
6004 return error_mark_node;
6006 else if (TREE_CHAIN (matches))
6008 /* There were too many matches. */
6014 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6015 DECL_NAME (OVL_FUNCTION (overload)),
6018 /* Since print_candidates expects the functions in the
6019 TREE_VALUE slot, we flip them here. */
6020 for (match = matches; match; match = TREE_CHAIN (match))
6021 TREE_VALUE (match) = TREE_PURPOSE (match);
6023 print_candidates (matches);
6026 return error_mark_node;
6029 /* Good, exactly one match. Now, convert it to the correct type. */
6030 fn = TREE_PURPOSE (matches);
6032 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6033 && !ptrmem && !flag_ms_extensions)
6035 static int explained;
6038 return error_mark_node;
6040 cp_pedwarn ("assuming pointer to member `%D'", fn);
6043 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6049 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6050 return build_unary_op (ADDR_EXPR, fn, 0);
6053 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6054 will mark the function as addressed, but here we must do it
6056 mark_addressable (fn);
6062 /* This function will instantiate the type of the expression given in
6063 RHS to match the type of LHSTYPE. If errors exist, then return
6064 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6065 we complain on errors. If we are not complaining, never modify rhs,
6066 as overload resolution wants to try many possible instantiations, in
6067 the hope that at least one will work.
6069 For non-recursive calls, LHSTYPE should be a function, pointer to
6070 function, or a pointer to member function. */
6073 instantiate_type (lhstype, rhs, flags)
6075 enum instantiate_type_flags flags;
6077 int complain = (flags & itf_complain);
6078 int strict = (flags & itf_no_attributes)
6079 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6080 int allow_ptrmem = flags & itf_ptrmem_ok;
6082 flags &= ~itf_ptrmem_ok;
6084 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6087 error ("not enough type information");
6088 return error_mark_node;
6091 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6093 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6096 cp_error ("argument of type `%T' does not match `%T'",
6097 TREE_TYPE (rhs), lhstype);
6098 return error_mark_node;
6101 /* We don't overwrite rhs if it is an overloaded function.
6102 Copying it would destroy the tree link. */
6103 if (TREE_CODE (rhs) != OVERLOAD)
6104 rhs = copy_node (rhs);
6106 /* This should really only be used when attempting to distinguish
6107 what sort of a pointer to function we have. For now, any
6108 arithmetic operation which is not supported on pointers
6109 is rejected as an error. */
6111 switch (TREE_CODE (rhs))
6118 my_friendly_abort (177);
6119 return error_mark_node;
6126 new_rhs = instantiate_type (build_pointer_type (lhstype),
6127 TREE_OPERAND (rhs, 0), flags);
6128 if (new_rhs == error_mark_node)
6129 return error_mark_node;
6131 TREE_TYPE (rhs) = lhstype;
6132 TREE_OPERAND (rhs, 0) = new_rhs;
6137 rhs = copy_node (TREE_OPERAND (rhs, 0));
6138 TREE_TYPE (rhs) = unknown_type_node;
6139 return instantiate_type (lhstype, rhs, flags);
6142 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6145 rhs = TREE_OPERAND (rhs, 1);
6146 if (BASELINK_P (rhs))
6147 return instantiate_type (lhstype, TREE_VALUE (rhs),
6148 flags | allow_ptrmem);
6150 /* This can happen if we are forming a pointer-to-member for a
6152 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6156 case TEMPLATE_ID_EXPR:
6158 tree fns = TREE_OPERAND (rhs, 0);
6159 tree args = TREE_OPERAND (rhs, 1);
6162 resolve_address_of_overloaded_function (lhstype,
6166 /*template_only=*/1,
6172 resolve_address_of_overloaded_function (lhstype,
6176 /*template_only=*/0,
6177 /*explicit_targs=*/NULL_TREE);
6180 /* Now we should have a baselink. */
6181 my_friendly_assert (BASELINK_P (rhs), 990412);
6183 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6186 /* This is too hard for now. */
6187 my_friendly_abort (183);
6188 return error_mark_node;
6193 TREE_OPERAND (rhs, 0)
6194 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6195 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6196 return error_mark_node;
6197 TREE_OPERAND (rhs, 1)
6198 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6199 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6200 return error_mark_node;
6202 TREE_TYPE (rhs) = lhstype;
6206 case TRUNC_DIV_EXPR:
6207 case FLOOR_DIV_EXPR:
6209 case ROUND_DIV_EXPR:
6211 case TRUNC_MOD_EXPR:
6212 case FLOOR_MOD_EXPR:
6214 case ROUND_MOD_EXPR:
6215 case FIX_ROUND_EXPR:
6216 case FIX_FLOOR_EXPR:
6218 case FIX_TRUNC_EXPR:
6234 case PREINCREMENT_EXPR:
6235 case PREDECREMENT_EXPR:
6236 case POSTINCREMENT_EXPR:
6237 case POSTDECREMENT_EXPR:
6239 error ("invalid operation on uninstantiated type");
6240 return error_mark_node;
6242 case TRUTH_AND_EXPR:
6244 case TRUTH_XOR_EXPR:
6251 case TRUTH_ANDIF_EXPR:
6252 case TRUTH_ORIF_EXPR:
6253 case TRUTH_NOT_EXPR:
6255 error ("not enough type information");
6256 return error_mark_node;
6259 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6262 error ("not enough type information");
6263 return error_mark_node;
6265 TREE_OPERAND (rhs, 1)
6266 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6267 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6268 return error_mark_node;
6269 TREE_OPERAND (rhs, 2)
6270 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6271 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6272 return error_mark_node;
6274 TREE_TYPE (rhs) = lhstype;
6278 TREE_OPERAND (rhs, 1)
6279 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6280 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6281 return error_mark_node;
6283 TREE_TYPE (rhs) = lhstype;
6288 if (PTRMEM_OK_P (rhs))
6289 flags |= itf_ptrmem_ok;
6291 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6293 case ENTRY_VALUE_EXPR:
6294 my_friendly_abort (184);
6295 return error_mark_node;
6298 return error_mark_node;
6301 my_friendly_abort (185);
6302 return error_mark_node;
6306 /* Return the name of the virtual function pointer field
6307 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6308 this may have to look back through base types to find the
6309 ultimate field name. (For single inheritance, these could
6310 all be the same name. Who knows for multiple inheritance). */
6313 get_vfield_name (type)
6316 tree binfo = TYPE_BINFO (type);
6319 while (BINFO_BASETYPES (binfo)
6320 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6321 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6322 binfo = BINFO_BASETYPE (binfo, 0);
6324 type = BINFO_TYPE (binfo);
6325 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6326 + TYPE_NAME_LENGTH (type) + 2);
6327 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6328 return get_identifier (buf);
6332 print_class_statistics ()
6334 #ifdef GATHER_STATISTICS
6335 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6336 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6337 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6338 n_build_method_call, n_inner_fields_searched);
6341 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6342 n_vtables, n_vtable_searches);
6343 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6344 n_vtable_entries, n_vtable_elems);
6349 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6350 according to [class]:
6351 The class-name is also inserted
6352 into the scope of the class itself. For purposes of access checking,
6353 the inserted class name is treated as if it were a public member name. */
6356 build_self_reference ()
6358 tree name = constructor_name (current_class_type);
6359 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6362 DECL_NONLOCAL (value) = 1;
6363 DECL_CONTEXT (value) = current_class_type;
6364 DECL_ARTIFICIAL (value) = 1;
6366 if (processing_template_decl)
6367 value = push_template_decl (value);
6369 saved_cas = current_access_specifier;
6370 current_access_specifier = access_public_node;
6371 finish_member_declaration (value);
6372 current_access_specifier = saved_cas;
6375 /* Returns 1 if TYPE contains only padding bytes. */
6378 is_empty_class (type)
6381 if (type == error_mark_node)
6384 if (! IS_AGGR_TYPE (type))
6387 return integer_zerop (CLASSTYPE_SIZE (type));
6390 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6391 a *_TYPE node. NODE can also be a local class. */
6394 get_enclosing_class (type)
6399 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6401 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6404 node = DECL_CONTEXT (node);
6410 node = TYPE_CONTEXT (node);
6414 my_friendly_abort (0);
6420 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6423 is_base_of_enclosing_class (base, type)
6428 if (get_binfo (base, type, 0))
6431 type = get_enclosing_class (type);
6436 /* Note that NAME was looked up while the current class was being
6437 defined and that the result of that lookup was DECL. */
6440 maybe_note_name_used_in_class (name, decl)
6444 splay_tree names_used;
6446 /* If we're not defining a class, there's nothing to do. */
6447 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6450 /* If there's already a binding for this NAME, then we don't have
6451 anything to worry about. */
6452 if (IDENTIFIER_CLASS_VALUE (name))
6455 if (!current_class_stack[current_class_depth - 1].names_used)
6456 current_class_stack[current_class_depth - 1].names_used
6457 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6458 names_used = current_class_stack[current_class_depth - 1].names_used;
6460 splay_tree_insert (names_used,
6461 (splay_tree_key) name,
6462 (splay_tree_value) decl);
6465 /* Note that NAME was declared (as DECL) in the current class. Check
6466 to see that the declaration is legal. */
6469 note_name_declared_in_class (name, decl)
6473 splay_tree names_used;
6476 /* Look to see if we ever used this name. */
6478 = current_class_stack[current_class_depth - 1].names_used;
6482 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6485 /* [basic.scope.class]
6487 A name N used in a class S shall refer to the same declaration
6488 in its context and when re-evaluated in the completed scope of
6490 cp_error ("declaration of `%#D'", decl);
6491 cp_error_at ("changes meaning of `%D' from `%+#D'",
6492 DECL_NAME (OVL_CURRENT (decl)),
6497 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6498 Secondary vtables are merged with primary vtables; this function
6499 will return the VAR_DECL for the primary vtable. */
6502 get_vtbl_decl_for_binfo (binfo)
6507 decl = BINFO_VTABLE (binfo);
6508 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6510 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6512 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6515 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6519 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6520 who's TREE_PURPOSE is the TYPE of the required primary base and
6521 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6524 dfs_get_primary_binfo (binfo, data)
6528 tree cons = (tree) data;
6529 tree primary_base = TREE_PURPOSE (cons);
6531 if (TREE_VIA_VIRTUAL (binfo)
6532 && same_type_p (BINFO_TYPE (binfo), primary_base))
6533 /* This is the right type of binfo, but it might be an unshared
6534 instance, and the shared instance is later in the dfs walk. We
6535 must keep looking. */
6536 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6541 /* Returns the unshared binfo for the primary base of BINFO. Note
6542 that in a complex hierarchy the resulting BINFO may not actually
6543 *be* primary. In particular if the resulting BINFO is a virtual
6544 base, and it occurs elsewhere in the hierarchy, then this
6545 occurrence may not actually be a primary base in the complete
6546 object. Check BINFO_PRIMARY_P to be sure. */
6549 get_primary_binfo (binfo)
6553 tree result = NULL_TREE;
6556 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6560 /* A non-virtual primary base is always a direct base, and easy to
6562 if (!TREE_VIA_VIRTUAL (primary_base))
6566 /* Scan the direct basetypes until we find a base with the same
6567 type as the primary base. */
6568 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6570 tree base_binfo = BINFO_BASETYPE (binfo, i);
6572 if (same_type_p (BINFO_TYPE (base_binfo),
6573 BINFO_TYPE (primary_base)))
6577 /* We should always find the primary base. */
6578 my_friendly_abort (20000729);
6581 /* For a primary virtual base, we have to scan the entire hierarchy
6582 rooted at BINFO; the virtual base could be an indirect virtual
6583 base. There could be more than one instance of the primary base
6584 in the hierarchy, and if one is the canonical binfo we want that
6585 one. If it exists, it should be the first one we find, but as a
6586 consistency check we find them all and make sure. */
6587 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6588 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6589 virtuals = TREE_VALUE (virtuals);
6591 /* We must have found at least one instance. */
6592 my_friendly_assert (virtuals, 20010612);
6594 if (TREE_CHAIN (virtuals))
6596 /* We found more than one instance of the base. We must make
6597 sure that, if one is the canonical one, it is the first one
6598 we found. As the chain is in reverse dfs order, that means
6599 the last on the list. */
6600 tree complete_binfo;
6603 for (complete_binfo = binfo;
6604 BINFO_INHERITANCE_CHAIN (complete_binfo);
6605 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6607 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6608 BINFO_TYPE (complete_binfo));
6610 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6612 result = TREE_VALUE (virtuals);
6614 if (canonical == result)
6616 /* This is the unshared instance. Make sure it was the
6618 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6624 result = TREE_VALUE (virtuals);
6628 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6631 maybe_indent_hierarchy (stream, indent, indented_p)
6637 fprintf (stream, "%*s", indent, "");
6641 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6642 dominated by T) to stderr. INDENT should be zero when called from
6643 the top level; it is incremented recursively. */
6646 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6656 indented = maybe_indent_hierarchy (stream, indent, 0);
6657 fprintf (stream, "%s (0x%lx) ",
6658 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6659 (unsigned long) binfo);
6660 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6661 tree_low_cst (BINFO_OFFSET (binfo), 0));
6662 if (is_empty_class (BINFO_TYPE (binfo)))
6663 fprintf (stream, " empty");
6664 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6665 fprintf (stream, " nearly-empty");
6666 if (TREE_VIA_VIRTUAL (binfo))
6668 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6670 fprintf (stream, " virtual");
6671 if (canonical == binfo)
6672 fprintf (stream, " canonical");
6674 fprintf (stream, " non-canonical");
6676 fprintf (stream, "\n");
6679 if (BINFO_PRIMARY_BASE_OF (binfo))
6681 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6682 fprintf (stream, " primary-for %s (0x%lx)",
6683 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6684 TFF_PLAIN_IDENTIFIER),
6685 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6687 if (BINFO_LOST_PRIMARY_P (binfo))
6689 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6690 fprintf (stream, " lost-primary");
6693 fprintf (stream, "\n");
6695 if (!(flags & TDF_SLIM))
6699 if (BINFO_SUBVTT_INDEX (binfo))
6701 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6702 fprintf (stream, " subvttidx=%s",
6703 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6704 TFF_PLAIN_IDENTIFIER));
6706 if (BINFO_VPTR_INDEX (binfo))
6708 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6709 fprintf (stream, " vptridx=%s",
6710 expr_as_string (BINFO_VPTR_INDEX (binfo),
6711 TFF_PLAIN_IDENTIFIER));
6713 if (BINFO_VPTR_FIELD (binfo))
6715 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6716 fprintf (stream, " vbaseoffset=%s",
6717 expr_as_string (BINFO_VPTR_FIELD (binfo),
6718 TFF_PLAIN_IDENTIFIER));
6720 if (BINFO_VTABLE (binfo))
6722 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6723 fprintf (stream, " vptr=%s",
6724 expr_as_string (BINFO_VTABLE (binfo),
6725 TFF_PLAIN_IDENTIFIER));
6729 fprintf (stream, "\n");
6733 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6734 dump_class_hierarchy_r (stream, flags,
6735 t, BINFO_BASETYPE (binfo, i),
6739 /* Dump the BINFO hierarchy for T. */
6742 dump_class_hierarchy (t)
6746 FILE *stream = dump_begin (TDI_class, &flags);
6751 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6752 fprintf (stream, " size=%lu align=%lu\n",
6753 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6754 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6755 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6756 fprintf (stream, "\n");
6757 dump_end (TDI_class, stream);
6761 dump_array (stream, decl)
6768 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6770 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6772 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6773 fprintf (stream, " %s entries",
6774 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6775 TFF_PLAIN_IDENTIFIER));
6776 fprintf (stream, "\n");
6778 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6779 inits; ix++, inits = TREE_CHAIN (inits))
6780 fprintf (stream, "%-4d %s\n", ix * elt,
6781 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6785 dump_vtable (t, binfo, vtable)
6791 FILE *stream = dump_begin (TDI_class, &flags);
6796 if (!(flags & TDF_SLIM))
6798 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6800 fprintf (stream, "%s for %s",
6801 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6802 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6805 if (!TREE_VIA_VIRTUAL (binfo))
6806 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6807 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6809 fprintf (stream, "\n");
6810 dump_array (stream, vtable);
6811 fprintf (stream, "\n");
6814 dump_end (TDI_class, stream);
6823 FILE *stream = dump_begin (TDI_class, &flags);
6828 if (!(flags & TDF_SLIM))
6830 fprintf (stream, "VTT for %s\n",
6831 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6832 dump_array (stream, vtt);
6833 fprintf (stream, "\n");
6836 dump_end (TDI_class, stream);
6839 /* Virtual function table initialization. */
6841 /* Create all the necessary vtables for T and its base classes. */
6850 /* We lay out the primary and secondary vtables in one contiguous
6851 vtable. The primary vtable is first, followed by the non-virtual
6852 secondary vtables in inheritance graph order. */
6853 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6854 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6855 TYPE_BINFO (t), t, list);
6857 /* Then come the virtual bases, also in inheritance graph order. */
6858 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6862 if (!TREE_VIA_VIRTUAL (vbase))
6865 /* Although we walk in inheritance order, that might not get the
6867 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6869 accumulate_vtbl_inits (real_base, real_base,
6870 TYPE_BINFO (t), t, list);
6873 if (TYPE_BINFO_VTABLE (t))
6874 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6877 /* Initialize the vtable for BINFO with the INITS. */
6880 initialize_vtable (binfo, inits)
6886 layout_vtable_decl (binfo, list_length (inits));
6887 decl = get_vtbl_decl_for_binfo (binfo);
6888 initialize_array (decl, inits);
6889 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6892 /* Initialize DECL (a declaration for a namespace-scope array) with
6896 initialize_array (decl, inits)
6902 context = DECL_CONTEXT (decl);
6903 DECL_CONTEXT (decl) = NULL_TREE;
6904 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6905 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6906 DECL_CONTEXT (decl) = context;
6909 /* Build the VTT (virtual table table) for T.
6910 A class requires a VTT if it has virtual bases.
6913 1 - primary virtual pointer for complete object T
6914 2 - secondary VTTs for each direct non-virtual base of T which requires a
6916 3 - secondary virtual pointers for each direct or indirect base of T which
6917 has virtual bases or is reachable via a virtual path from T.
6918 4 - secondary VTTs for each direct or indirect virtual base of T.
6920 Secondary VTTs look like complete object VTTs without part 4. */
6931 /* Build up the initializers for the VTT. */
6933 index = size_zero_node;
6934 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6936 /* If we didn't need a VTT, we're done. */
6940 /* Figure out the type of the VTT. */
6941 type = build_index_type (size_int (list_length (inits) - 1));
6942 type = build_cplus_array_type (const_ptr_type_node, type);
6944 /* Now, build the VTT object itself. */
6945 vtt = build_vtable (t, get_vtt_name (t), type);
6946 pushdecl_top_level (vtt);
6947 initialize_array (vtt, inits);
6952 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6953 from within some heirarchy which is inherited from the type of BINFO.
6954 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6958 get_original_base (base_binfo, binfo)
6965 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6967 if (TREE_VIA_VIRTUAL (base_binfo))
6968 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6969 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6971 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
6972 if (same_type_p (BINFO_TYPE (base_binfo),
6973 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
6974 return BINFO_BASETYPE (derived, ix);
6975 my_friendly_abort (20010223);
6979 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6980 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6981 and CHAIN the vtable pointer for this binfo after construction is
6982 complete. VALUE can also be another BINFO, in which case we recurse. */
6985 binfo_ctor_vtable (binfo)
6992 vt = BINFO_VTABLE (binfo);
6993 if (TREE_CODE (vt) == TREE_LIST)
6994 vt = TREE_VALUE (vt);
6995 if (TREE_CODE (vt) == TREE_VEC)
7004 /* Recursively build the VTT-initializer for BINFO (which is in the
7005 hierarchy dominated by T). INITS points to the end of the initializer
7006 list to date. INDEX is the VTT index where the next element will be
7007 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7008 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7009 for virtual bases of T. When it is not so, we build the constructor
7010 vtables for the BINFO-in-T variant. */
7013 build_vtt_inits (binfo, t, inits, index)
7022 tree secondary_vptrs;
7023 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7025 /* We only need VTTs for subobjects with virtual bases. */
7026 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7029 /* We need to use a construction vtable if this is not the primary
7033 build_ctor_vtbl_group (binfo, t);
7035 /* Record the offset in the VTT where this sub-VTT can be found. */
7036 BINFO_SUBVTT_INDEX (binfo) = *index;
7039 /* Add the address of the primary vtable for the complete object. */
7040 init = binfo_ctor_vtable (binfo);
7041 *inits = build_tree_list (NULL_TREE, init);
7042 inits = &TREE_CHAIN (*inits);
7045 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7046 BINFO_VPTR_INDEX (binfo) = *index;
7048 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7050 /* Recursively add the secondary VTTs for non-virtual bases. */
7051 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7053 b = BINFO_BASETYPE (binfo, i);
7054 if (!TREE_VIA_VIRTUAL (b))
7055 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7059 /* Add secondary virtual pointers for all subobjects of BINFO with
7060 either virtual bases or reachable along a virtual path, except
7061 subobjects that are non-virtual primary bases. */
7062 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7063 TREE_TYPE (secondary_vptrs) = *index;
7064 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7065 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7067 dfs_walk_real (binfo,
7068 dfs_build_secondary_vptr_vtt_inits,
7070 dfs_ctor_vtable_bases_queue_p,
7072 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7073 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7076 *index = TREE_TYPE (secondary_vptrs);
7078 /* The secondary vptrs come back in reverse order. After we reverse
7079 them, and add the INITS, the last init will be the first element
7081 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7082 if (secondary_vptrs)
7084 *inits = nreverse (secondary_vptrs);
7085 inits = &TREE_CHAIN (secondary_vptrs);
7086 my_friendly_assert (*inits == NULL_TREE, 20000517);
7089 /* Add the secondary VTTs for virtual bases. */
7091 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7095 if (!TREE_VIA_VIRTUAL (b))
7098 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7099 inits = build_vtt_inits (vbase, t, inits, index);
7104 tree data = tree_cons (t, binfo, NULL_TREE);
7105 VTT_TOP_LEVEL_P (data) = 0;
7106 VTT_MARKED_BINFO_P (data) = 0;
7108 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7109 dfs_ctor_vtable_bases_queue_p,
7116 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7117 for the base in most derived. DATA is a TREE_LIST who's
7118 TREE_CHAIN is the type of the base being
7119 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7120 flag of DATA indicates that this is a constructor vtable. The
7121 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7124 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7136 top_level_p = VTT_TOP_LEVEL_P (l);
7138 SET_BINFO_MARKED (binfo);
7140 /* We don't care about bases that don't have vtables. */
7141 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7144 /* We're only interested in proper subobjects of T. */
7145 if (same_type_p (BINFO_TYPE (binfo), t))
7148 /* We're not interested in non-virtual primary bases. */
7149 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7152 /* If BINFO has virtual bases or is reachable via a virtual path
7153 from T, it'll have a secondary vptr. */
7154 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7155 && !binfo_via_virtual (binfo, t))
7158 /* Record the index where this secondary vptr can be found. */
7159 index = TREE_TYPE (l);
7162 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7163 BINFO_VPTR_INDEX (binfo) = index;
7165 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7166 TYPE_SIZE_UNIT (ptr_type_node));
7168 /* Add the initializer for the secondary vptr itself. */
7169 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7171 /* It's a primary virtual base, and this is not the construction
7172 vtable. Find the base this is primary of in the inheritance graph,
7173 and use that base's vtable now. */
7174 while (BINFO_PRIMARY_BASE_OF (binfo))
7175 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7177 init = binfo_ctor_vtable (binfo);
7178 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7183 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7184 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7185 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7189 dfs_ctor_vtable_bases_queue_p (binfo, data)
7193 if (TREE_VIA_VIRTUAL (binfo))
7194 /* Get the shared version. */
7195 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7197 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7202 /* Called from build_vtt_inits via dfs_walk. After building constructor
7203 vtables and generating the sub-vtt from them, we need to restore the
7204 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7205 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7208 dfs_fixup_binfo_vtbls (binfo, data)
7212 CLEAR_BINFO_MARKED (binfo);
7214 /* We don't care about bases that don't have vtables. */
7215 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7218 /* If we scribbled the construction vtable vptr into BINFO, clear it
7220 if (BINFO_VTABLE (binfo)
7221 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7222 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7223 == TREE_VALUE ((tree) data)))
7224 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7229 /* Build the construction vtable group for BINFO which is in the
7230 hierarchy dominated by T. */
7233 build_ctor_vtbl_group (binfo, t)
7244 /* See if we've already created this construction vtable group. */
7245 id = mangle_ctor_vtbl_for_type (t, binfo);
7246 if (IDENTIFIER_GLOBAL_VALUE (id))
7249 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7250 /* Build a version of VTBL (with the wrong type) for use in
7251 constructing the addresses of secondary vtables in the
7252 construction vtable group. */
7253 vtbl = build_vtable (t, id, ptr_type_node);
7254 list = build_tree_list (vtbl, NULL_TREE);
7255 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7258 /* Add the vtables for each of our virtual bases using the vbase in T
7260 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7262 vbase = TREE_CHAIN (vbase))
7267 if (!TREE_VIA_VIRTUAL (vbase))
7269 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7270 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7272 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7274 inits = TREE_VALUE (list);
7276 /* Figure out the type of the construction vtable. */
7277 type = build_index_type (size_int (list_length (inits) - 1));
7278 type = build_cplus_array_type (vtable_entry_type, type);
7279 TREE_TYPE (vtbl) = type;
7281 /* Initialize the construction vtable. */
7282 pushdecl_top_level (vtbl);
7283 initialize_array (vtbl, inits);
7284 dump_vtable (t, binfo, vtbl);
7287 /* Add the vtbl initializers for BINFO (and its bases other than
7288 non-virtual primaries) to the list of INITS. BINFO is in the
7289 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7290 the constructor the vtbl inits should be accumulated for. (If this
7291 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7292 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7293 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7294 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7295 but are not necessarily the same in terms of layout. */
7298 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7306 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7308 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7309 BINFO_TYPE (orig_binfo)),
7312 /* If it doesn't have a vptr, we don't do anything. */
7313 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7316 /* If we're building a construction vtable, we're not interested in
7317 subobjects that don't require construction vtables. */
7319 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7320 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7323 /* Build the initializers for the BINFO-in-T vtable. */
7325 = chainon (TREE_VALUE (inits),
7326 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7327 rtti_binfo, t, inits));
7329 /* Walk the BINFO and its bases. We walk in preorder so that as we
7330 initialize each vtable we can figure out at what offset the
7331 secondary vtable lies from the primary vtable. We can't use
7332 dfs_walk here because we need to iterate through bases of BINFO
7333 and RTTI_BINFO simultaneously. */
7334 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7336 tree base_binfo = BINFO_BASETYPE (binfo, i);
7338 /* Skip virtual bases. */
7339 if (TREE_VIA_VIRTUAL (base_binfo))
7341 accumulate_vtbl_inits (base_binfo,
7342 BINFO_BASETYPE (orig_binfo, i),
7348 /* Called from accumulate_vtbl_inits. Returns the initializers for
7349 the BINFO vtable. */
7352 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7359 tree inits = NULL_TREE;
7360 tree vtbl = NULL_TREE;
7361 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7364 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7366 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7367 primary virtual base. If it is not the same primary in
7368 the hierarchy of T, we'll need to generate a ctor vtable
7369 for it, to place at its location in T. If it is the same
7370 primary, we still need a VTT entry for the vtable, but it
7371 should point to the ctor vtable for the base it is a
7372 primary for within the sub-hierarchy of RTTI_BINFO.
7374 There are three possible cases:
7376 1) We are in the same place.
7377 2) We are a primary base within a lost primary virtual base of
7379 3) We are primary to something not a base of RTTI_BINFO. */
7381 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7382 tree last = NULL_TREE;
7384 /* First, look through the bases we are primary to for RTTI_BINFO
7385 or a virtual base. */
7386 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7389 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7392 /* If we run out of primary links, keep looking down our
7393 inheritance chain; we might be an indirect primary. */
7395 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7396 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7399 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7400 base B and it is a base of RTTI_BINFO, this is case 2. In
7401 either case, we share our vtable with LAST, i.e. the
7402 derived-most base within B of which we are a primary. */
7404 || (b && binfo_for_vbase (BINFO_TYPE (b),
7405 BINFO_TYPE (rtti_binfo))))
7406 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7407 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7408 binfo_ctor_vtable after everything's been set up. */
7411 /* Otherwise, this is case 3 and we get our own. */
7413 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7421 /* Compute the initializer for this vtable. */
7422 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7425 /* Figure out the position to which the VPTR should point. */
7426 vtbl = TREE_PURPOSE (l);
7427 vtbl = build1 (ADDR_EXPR,
7430 TREE_CONSTANT (vtbl) = 1;
7431 index = size_binop (PLUS_EXPR,
7432 size_int (non_fn_entries),
7433 size_int (list_length (TREE_VALUE (l))));
7434 index = size_binop (MULT_EXPR,
7435 TYPE_SIZE_UNIT (vtable_entry_type),
7437 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7438 TREE_CONSTANT (vtbl) = 1;
7442 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7443 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7444 straighten this out. */
7445 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7446 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7449 /* For an ordinary vtable, set BINFO_VTABLE. */
7450 BINFO_VTABLE (binfo) = vtbl;
7455 /* Construct the initializer for BINFO's virtual function table. BINFO
7456 is part of the hierarchy dominated by T. If we're building a
7457 construction vtable, the ORIG_BINFO is the binfo we should use to
7458 find the actual function pointers to put in the vtable - but they
7459 can be overridden on the path to most-derived in the graph that
7460 ORIG_BINFO belongs. Otherwise,
7461 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7462 BINFO that should be indicated by the RTTI information in the
7463 vtable; it will be a base class of T, rather than T itself, if we
7464 are building a construction vtable.
7466 The value returned is a TREE_LIST suitable for wrapping in a
7467 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7468 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7469 number of non-function entries in the vtable.
7471 It might seem that this function should never be called with a
7472 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7473 base is always subsumed by a derived class vtable. However, when
7474 we are building construction vtables, we do build vtables for
7475 primary bases; we need these while the primary base is being
7479 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7484 int *non_fn_entries_p;
7491 /* Initialize VID. */
7492 memset (&vid, 0, sizeof (vid));
7495 vid.rtti_binfo = rtti_binfo;
7496 vid.last_init = &vid.inits;
7497 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7498 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7499 /* The first vbase or vcall offset is at index -3 in the vtable. */
7500 vid.index = ssize_int (-3);
7502 /* Add entries to the vtable for RTTI. */
7503 build_rtti_vtbl_entries (binfo, &vid);
7505 /* Create an array for keeping track of the functions we've
7506 processed. When we see multiple functions with the same
7507 signature, we share the vcall offsets. */
7508 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7509 /* Add the vcall and vbase offset entries. */
7510 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7512 VARRAY_FREE (vid.fns);
7513 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7514 build_vbase_offset_vtbl_entries. */
7515 for (vbase = CLASSTYPE_VBASECLASSES (t);
7517 vbase = TREE_CHAIN (vbase))
7518 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7520 if (non_fn_entries_p)
7521 *non_fn_entries_p = list_length (vid.inits);
7523 /* Go through all the ordinary virtual functions, building up
7525 vfun_inits = NULL_TREE;
7526 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7534 /* Pull the offset for `this', and the function to call, out of
7536 delta = BV_DELTA (v);
7538 if (BV_USE_VCALL_INDEX_P (v))
7540 vcall_index = BV_VCALL_INDEX (v);
7541 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7544 vcall_index = NULL_TREE;
7547 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7548 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7550 /* You can't call an abstract virtual function; it's abstract.
7551 So, we replace these functions with __pure_virtual. */
7552 if (DECL_PURE_VIRTUAL_P (fn))
7555 /* Take the address of the function, considering it to be of an
7556 appropriate generic type. */
7557 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7558 /* The address of a function can't change. */
7559 TREE_CONSTANT (pfn) = 1;
7561 /* Enter it in the vtable. */
7562 init = build_vtable_entry (delta, vcall_index, pfn);
7564 /* If the only definition of this function signature along our
7565 primary base chain is from a lost primary, this vtable slot will
7566 never be used, so just zero it out. This is important to avoid
7567 requiring extra thunks which cannot be generated with the function.
7569 We could also handle this in update_vtable_entry_for_fn; doing it
7570 here means we zero out unused slots in ctor vtables as well,
7571 rather than filling them with erroneous values (though harmless,
7572 apart from relocation costs). */
7573 if (fn != abort_fndecl)
7574 for (b = binfo; ; b = get_primary_binfo (b))
7576 /* We found a defn before a lost primary; go ahead as normal. */
7577 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7580 /* The nearest definition is from a lost primary; clear the
7582 if (BINFO_LOST_PRIMARY_P (b))
7584 init = size_zero_node;
7589 /* And add it to the chain of initializers. */
7590 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7593 /* The initializers for virtual functions were built up in reverse
7594 order; straighten them out now. */
7595 vfun_inits = nreverse (vfun_inits);
7597 /* The negative offset initializers are also in reverse order. */
7598 vid.inits = nreverse (vid.inits);
7600 /* Chain the two together. */
7601 return chainon (vid.inits, vfun_inits);
7604 /* Adds to vid->inits the initializers for the vbase and vcall
7605 offsets in BINFO, which is in the hierarchy dominated by T. */
7608 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7610 vtbl_init_data *vid;
7614 /* If this is a derived class, we must first create entries
7615 corresponding to the primary base class. */
7616 b = get_primary_binfo (binfo);
7618 build_vcall_and_vbase_vtbl_entries (b, vid);
7620 /* Add the vbase entries for this base. */
7621 build_vbase_offset_vtbl_entries (binfo, vid);
7622 /* Add the vcall entries for this base. */
7623 build_vcall_offset_vtbl_entries (binfo, vid);
7626 /* Returns the initializers for the vbase offset entries in the vtable
7627 for BINFO (which is part of the class hierarchy dominated by T), in
7628 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7629 where the next vbase offset will go. */
7632 build_vbase_offset_vtbl_entries (binfo, vid)
7634 vtbl_init_data *vid;
7638 tree non_primary_binfo;
7640 /* If there are no virtual baseclasses, then there is nothing to
7642 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7647 /* We might be a primary base class. Go up the inheritance hierarchy
7648 until we find the most derived class of which we are a primary base:
7649 it is the offset of that which we need to use. */
7650 non_primary_binfo = binfo;
7651 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7655 /* If we have reached a virtual base, then it must be a primary
7656 base (possibly multi-level) of vid->binfo, or we wouldn't
7657 have called build_vcall_and_vbase_vtbl_entries for it. But it
7658 might be a lost primary, so just skip down to vid->binfo. */
7659 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7661 non_primary_binfo = vid->binfo;
7665 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7666 if (get_primary_binfo (b) != non_primary_binfo)
7668 non_primary_binfo = b;
7671 /* Go through the virtual bases, adding the offsets. */
7672 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7674 vbase = TREE_CHAIN (vbase))
7679 if (!TREE_VIA_VIRTUAL (vbase))
7682 /* Find the instance of this virtual base in the complete
7684 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7686 /* If we've already got an offset for this virtual base, we
7687 don't need another one. */
7688 if (BINFO_VTABLE_PATH_MARKED (b))
7690 SET_BINFO_VTABLE_PATH_MARKED (b);
7692 /* Figure out where we can find this vbase offset. */
7693 delta = size_binop (MULT_EXPR,
7696 TYPE_SIZE_UNIT (vtable_entry_type)));
7697 if (vid->primary_vtbl_p)
7698 BINFO_VPTR_FIELD (b) = delta;
7700 if (binfo != TYPE_BINFO (t))
7704 /* Find the instance of this virtual base in the type of BINFO. */
7705 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7706 BINFO_TYPE (binfo));
7708 /* The vbase offset had better be the same. */
7709 if (!tree_int_cst_equal (delta,
7710 BINFO_VPTR_FIELD (orig_vbase)))
7711 my_friendly_abort (20000403);
7714 /* The next vbase will come at a more negative offset. */
7715 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7717 /* The initializer is the delta from BINFO to this virtual base.
7718 The vbase offsets go in reverse inheritance-graph order, and
7719 we are walking in inheritance graph order so these end up in
7721 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7724 = build_tree_list (NULL_TREE,
7725 fold (build1 (NOP_EXPR,
7728 vid->last_init = &TREE_CHAIN (*vid->last_init);
7732 /* Adds the initializers for the vcall offset entries in the vtable
7733 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7737 build_vcall_offset_vtbl_entries (binfo, vid)
7739 vtbl_init_data *vid;
7741 /* We only need these entries if this base is a virtual base. */
7742 if (!TREE_VIA_VIRTUAL (binfo))
7745 /* We need a vcall offset for each of the virtual functions in this
7746 vtable. For example:
7748 class A { virtual void f (); };
7749 class B1 : virtual public A { virtual void f (); };
7750 class B2 : virtual public A { virtual void f (); };
7751 class C: public B1, public B2 { virtual void f (); };
7753 A C object has a primary base of B1, which has a primary base of A. A
7754 C also has a secondary base of B2, which no longer has a primary base
7755 of A. So the B2-in-C construction vtable needs a secondary vtable for
7756 A, which will adjust the A* to a B2* to call f. We have no way of
7757 knowing what (or even whether) this offset will be when we define B2,
7758 so we store this "vcall offset" in the A sub-vtable and look it up in
7759 a "virtual thunk" for B2::f.
7761 We need entries for all the functions in our primary vtable and
7762 in our non-virtual bases' secondary vtables. */
7764 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7765 add_vcall_offset_vtbl_entries_r (binfo, vid);
7768 /* Build vcall offsets, starting with those for BINFO. */
7771 add_vcall_offset_vtbl_entries_r (binfo, vid)
7773 vtbl_init_data *vid;
7778 /* Don't walk into virtual bases -- except, of course, for the
7779 virtual base for which we are building vcall offsets. Any
7780 primary virtual base will have already had its offsets generated
7781 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7782 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7785 /* If BINFO has a primary base, process it first. */
7786 primary_binfo = get_primary_binfo (binfo);
7788 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7790 /* Add BINFO itself to the list. */
7791 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7793 /* Scan the non-primary bases of BINFO. */
7794 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7798 base_binfo = BINFO_BASETYPE (binfo, i);
7799 if (base_binfo != primary_binfo)
7800 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7804 /* Called from build_vcall_offset_vtbl_entries_r. */
7807 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7809 vtbl_init_data* vid;
7811 tree derived_virtuals;
7815 /* If BINFO is a primary base, the most derived class which has BINFO as
7816 a primary base; otherwise, just BINFO. */
7817 tree non_primary_binfo;
7819 binfo_inits = NULL_TREE;
7821 /* We might be a primary base class. Go up the inheritance hierarchy
7822 until we find the most derived class of which we are a primary base:
7823 it is the BINFO_VIRTUALS there that we need to consider. */
7824 non_primary_binfo = binfo;
7825 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7829 /* If we have reached a virtual base, then it must be vid->vbase,
7830 because we ignore other virtual bases in
7831 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7832 base (possibly multi-level) of vid->binfo, or we wouldn't
7833 have called build_vcall_and_vbase_vtbl_entries for it. But it
7834 might be a lost primary, so just skip down to vid->binfo. */
7835 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7837 if (non_primary_binfo != vid->vbase)
7839 non_primary_binfo = vid->binfo;
7843 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7844 if (get_primary_binfo (b) != non_primary_binfo)
7846 non_primary_binfo = b;
7849 if (vid->ctor_vtbl_p)
7850 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7851 where rtti_binfo is the most derived type. */
7852 non_primary_binfo = get_original_base
7853 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7855 /* Make entries for the rest of the virtuals. */
7856 for (base_virtuals = BINFO_VIRTUALS (binfo),
7857 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7858 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7860 base_virtuals = TREE_CHAIN (base_virtuals),
7861 derived_virtuals = TREE_CHAIN (derived_virtuals),
7862 orig_virtuals = TREE_CHAIN (orig_virtuals))
7871 /* Find the declaration that originally caused this function to
7872 be present in BINFO_TYPE (binfo). */
7873 orig_fn = BV_FN (orig_virtuals);
7875 /* When processing BINFO, we only want to generate vcall slots for
7876 function slots introduced in BINFO. So don't try to generate
7877 one if the function isn't even defined in BINFO. */
7878 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7881 /* Find the overriding function. */
7882 fn = BV_FN (derived_virtuals);
7884 /* If there is already an entry for a function with the same
7885 signature as FN, then we do not need a second vcall offset.
7886 Check the list of functions already present in the derived
7888 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7892 derived_entry = VARRAY_TREE (vid->fns, i);
7893 if (same_signature_p (BV_FN (derived_entry), fn)
7894 /* We only use one vcall offset for virtual destructors,
7895 even though there are two virtual table entries. */
7896 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7897 && DECL_DESTRUCTOR_P (fn)))
7899 if (!vid->ctor_vtbl_p)
7900 BV_VCALL_INDEX (derived_virtuals)
7901 = BV_VCALL_INDEX (derived_entry);
7905 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7908 /* The FN comes from BASE. So, we must calculate the adjustment from
7909 vid->vbase to BASE. We can just look for BASE in the complete
7910 object because we are converting from a virtual base, so if there
7911 were multiple copies, there would not be a unique final overrider
7912 and vid->derived would be ill-formed. */
7913 base = DECL_CONTEXT (fn);
7914 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7916 /* Compute the vcall offset. */
7917 /* As mentioned above, the vbase we're working on is a primary base of
7918 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7919 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7920 vcall_offset = BINFO_OFFSET (vid->binfo);
7921 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7923 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7926 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7927 vid->last_init = &TREE_CHAIN (*vid->last_init);
7929 /* Keep track of the vtable index where this vcall offset can be
7930 found. For a construction vtable, we already made this
7931 annotation when we built the original vtable. */
7932 if (!vid->ctor_vtbl_p)
7933 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7935 /* The next vcall offset will be found at a more negative
7937 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7939 /* Keep track of this function. */
7940 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7944 /* Return vtbl initializers for the RTTI entries coresponding to the
7945 BINFO's vtable. The RTTI entries should indicate the object given
7946 by VID->rtti_binfo. */
7949 build_rtti_vtbl_entries (binfo, vid)
7951 vtbl_init_data *vid;
7960 basetype = BINFO_TYPE (binfo);
7961 t = BINFO_TYPE (vid->rtti_binfo);
7963 /* To find the complete object, we will first convert to our most
7964 primary base, and then add the offset in the vtbl to that value. */
7966 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7967 && !BINFO_LOST_PRIMARY_P (b))
7971 primary_base = get_primary_binfo (b);
7972 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7975 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7977 /* The second entry is the address of the typeinfo object. */
7979 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
7981 decl = integer_zero_node;
7983 /* Convert the declaration to a type that can be stored in the
7985 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
7986 TREE_CONSTANT (init) = 1;
7987 *vid->last_init = build_tree_list (NULL_TREE, init);
7988 vid->last_init = &TREE_CHAIN (*vid->last_init);
7990 /* Add the offset-to-top entry. It comes earlier in the vtable that
7991 the the typeinfo entry. Convert the offset to look like a
7992 function pointer, so that we can put it in the vtable. */
7993 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
7994 TREE_CONSTANT (init) = 1;
7995 *vid->last_init = build_tree_list (NULL_TREE, init);
7996 vid->last_init = &TREE_CHAIN (*vid->last_init);
7999 /* Build an entry in the virtual function table. DELTA is the offset
8000 for the `this' pointer. VCALL_INDEX is the vtable index containing
8001 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8002 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8003 but it may not actually be a virtual function table pointer. (For
8004 example, it might be the address of the RTTI object, under the new
8008 build_vtable_entry (delta, vcall_index, entry)
8013 tree fn = TREE_OPERAND (entry, 0);
8015 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8016 && fn != abort_fndecl)
8018 entry = make_thunk (entry, delta, vcall_index);
8019 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8020 TREE_READONLY (entry) = 1;
8021 TREE_CONSTANT (entry) = 1;
8023 #ifdef GATHER_STATISTICS
8024 n_vtable_entries += 1;