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 tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
138 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
139 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
140 static int count_fields PARAMS ((tree));
141 static int add_fields_to_vec PARAMS ((tree, tree, int));
142 static void check_bitfield_decl PARAMS ((tree));
143 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
144 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *,
146 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
148 static bool build_base_fields PARAMS ((record_layout_info, int *,
150 static void check_methods PARAMS ((tree));
151 static void remove_zero_width_bit_fields PARAMS ((tree));
152 static void check_bases PARAMS ((tree, int *, int *, int *));
153 static void check_bases_and_members PARAMS ((tree, int *));
154 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
155 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
156 static void fixup_pending_inline PARAMS ((tree));
157 static void fixup_inline_methods PARAMS ((tree));
158 static void set_primary_base PARAMS ((tree, tree, int *));
159 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
160 static void layout_virtual_bases PARAMS ((tree, splay_tree));
161 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
162 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
163 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
164 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
165 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
166 static void layout_vtable_decl PARAMS ((tree, int));
167 static tree dfs_find_final_overrider PARAMS ((tree, void *));
168 static tree find_final_overrider PARAMS ((tree, tree, tree));
169 static int make_new_vtable PARAMS ((tree, tree));
170 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
171 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
172 static void dump_class_hierarchy PARAMS ((tree));
173 static void dump_array PARAMS ((FILE *, tree));
174 static void dump_vtable PARAMS ((tree, tree, tree));
175 static void dump_vtt PARAMS ((tree, tree));
176 static tree build_vtable PARAMS ((tree, tree, tree));
177 static void initialize_vtable PARAMS ((tree, tree));
178 static void initialize_array PARAMS ((tree, tree));
179 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
182 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
183 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
184 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
185 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
187 static void set_vindex PARAMS ((tree, int *));
188 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
189 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
191 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
192 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
193 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
194 static void mark_primary_bases PARAMS ((tree));
195 static tree mark_primary_virtual_base PARAMS ((tree, tree));
196 static void clone_constructors_and_destructors PARAMS ((tree));
197 static tree build_clone PARAMS ((tree, tree));
198 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
199 static tree copy_virtuals PARAMS ((tree));
200 static void build_ctor_vtbl_group PARAMS ((tree, tree));
201 static void build_vtt PARAMS ((tree));
202 static tree binfo_ctor_vtable PARAMS ((tree));
203 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
204 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
205 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
206 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
207 static tree get_original_base PARAMS ((tree, tree));
208 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
209 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
210 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
211 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
212 tree, splay_tree, tree, int));
213 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
214 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
215 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
217 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
218 static bool type_requires_array_cookie PARAMS ((tree));
220 /* Macros for dfs walking during vtt construction. See
221 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
222 and dfs_fixup_binfo_vtbls. */
223 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
224 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
226 /* Variables shared between class.c and call.c. */
228 #ifdef GATHER_STATISTICS
230 int n_vtable_entries = 0;
231 int n_vtable_searches = 0;
232 int n_vtable_elems = 0;
233 int n_convert_harshness = 0;
234 int n_compute_conversion_costs = 0;
235 int n_build_method_call = 0;
236 int n_inner_fields_searched = 0;
239 /* Virtual base class layout. */
241 /* Returns a pointer to the virtual base class of EXP that has the
242 indicated TYPE. EXP is of class type, not a pointer type. */
245 build_vbase_pointer (exp, type)
251 /* Find the shared copy of TYPE; that's where the vtable offset is
253 vbase = binfo_for_vbase (type, TREE_TYPE (exp));
254 /* Find the virtual function table pointer. */
255 vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
256 /* Compute the location where the offset will lie. */
257 vbase_ptr = build (PLUS_EXPR,
258 TREE_TYPE (vbase_ptr),
260 BINFO_VPTR_FIELD (vbase));
261 vbase_ptr = build1 (NOP_EXPR,
262 build_pointer_type (ptrdiff_type_node),
264 /* Add the contents of this location to EXP. */
265 return build (PLUS_EXPR,
266 build_pointer_type (type),
267 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
268 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
271 /* Build multi-level access to EXPR using hierarchy path PATH.
272 CODE is PLUS_EXPR if we are going with the grain,
273 and MINUS_EXPR if we are not (in which case, we cannot traverse
274 virtual baseclass links).
276 TYPE is the type we want this path to have on exit.
278 NONNULL is non-zero if we know (for any reason) that EXPR is
279 not, in fact, zero. */
282 build_vbase_path (code, type, expr, path, nonnull)
284 tree type, expr, path;
287 register int changed = 0;
288 tree last = NULL_TREE, last_virtual = NULL_TREE;
290 tree null_expr = 0, nonnull_expr;
292 tree offset = integer_zero_node;
294 if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
295 return build1 (NOP_EXPR, type, expr);
297 /* We could do better if we had additional logic to convert back to the
298 unconverted type (the static type of the complete object), and then
299 convert back to the type we want. Until that is done, we only optimize
300 if the complete type is the same type as expr has. */
301 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
302 if (fixed_type_p < 0)
303 /* Virtual base layout is not fixed, even in ctors and dtors. */
306 if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
307 expr = save_expr (expr);
310 path = reverse_path (path);
312 basetype = BINFO_TYPE (path);
316 if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
318 last_virtual = BINFO_TYPE (TREE_VALUE (path));
319 if (code == PLUS_EXPR)
321 changed = ! fixed_type_p;
327 /* We already check for ambiguous things in the caller, just
331 tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
332 nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
334 ind = build_indirect_ref (nonnull_expr, NULL);
335 nonnull_expr = build_vbase_pointer (ind, last_virtual);
337 && TREE_CODE (type) == POINTER_TYPE
338 && null_expr == NULL_TREE)
340 null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
341 expr = build (COND_EXPR, build_pointer_type (last_virtual),
342 build (EQ_EXPR, boolean_type_node, expr,
344 null_expr, nonnull_expr);
347 /* else we'll figure out the offset below. */
349 /* Happens in the case of parse errors. */
350 if (nonnull_expr == error_mark_node)
351 return error_mark_node;
355 cp_error ("cannot cast up from virtual baseclass `%T'",
357 return error_mark_node;
360 last = TREE_VALUE (path);
361 path = TREE_CHAIN (path);
363 /* LAST is now the last basetype assoc on the path. */
365 /* A pointer to a virtual base member of a non-null object
366 is non-null. Therefore, we only need to test for zeroness once.
367 Make EXPR the canonical expression to deal with here. */
370 TREE_OPERAND (expr, 2) = nonnull_expr;
371 TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
372 = TREE_TYPE (nonnull_expr);
377 /* If we go through any virtual base pointers, make sure that
378 casts to BASETYPE from the last virtual base class use
379 the right value for BASETYPE. */
382 tree intype = TREE_TYPE (TREE_TYPE (expr));
384 if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
386 = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
389 offset = BINFO_OFFSET (last);
391 if (! integer_zerop (offset))
393 /* Bash types to make the backend happy. */
394 offset = cp_convert (type, offset);
396 /* If expr might be 0, we need to preserve that zeroness. */
400 TREE_TYPE (null_expr) = type;
402 null_expr = build1 (NOP_EXPR, type, integer_zero_node);
403 if (TREE_SIDE_EFFECTS (expr))
404 expr = save_expr (expr);
406 return build (COND_EXPR, type,
407 build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
409 build (code, type, expr, offset));
411 else return build (code, type, expr, offset);
414 /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
415 be used multiple times in initialization of multiple inheritance. */
418 TREE_TYPE (expr) = type;
422 return build1 (NOP_EXPR, type, expr);
426 /* Virtual function things. */
429 build_vtable_entry_ref (array_ref, instance, idx)
430 tree array_ref, instance, idx;
432 tree i, i2, vtable, first_fn, basetype;
434 basetype = TREE_TYPE (instance);
435 if (TREE_CODE (basetype) == REFERENCE_TYPE)
436 basetype = TREE_TYPE (basetype);
438 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
439 first_fn = TYPE_BINFO_VTABLE (basetype);
441 i = fold (build_array_ref (first_fn, idx));
442 i = fold (build_c_cast (ptrdiff_type_node,
443 build_unary_op (ADDR_EXPR, i, 0)));
444 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
445 i2 = fold (build_c_cast (ptrdiff_type_node,
446 build_unary_op (ADDR_EXPR, i2, 0)));
447 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
449 if (TREE_CODE (i) != INTEGER_CST)
452 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
455 /* Given an object INSTANCE, return an expression which yields the
456 vtable element corresponding to INDEX. There are many special
457 cases for INSTANCE which we take care of here, mainly to avoid
458 creating extra tree nodes when we don't have to. */
461 build_vtbl_ref_1 (instance, idx)
465 tree basetype = TREE_TYPE (instance);
467 if (TREE_CODE (basetype) == REFERENCE_TYPE)
468 basetype = TREE_TYPE (basetype);
470 if (instance == current_class_ref)
471 vtbl = build_vfield_ref (instance, basetype);
476 /* Try to figure out what a reference refers to, and
477 access its virtual function table directly. */
478 tree ref = NULL_TREE;
480 if (TREE_CODE (instance) == INDIRECT_REF
481 && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
482 ref = TREE_OPERAND (instance, 0);
483 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
486 if (ref && TREE_CODE (ref) == VAR_DECL
487 && DECL_INITIAL (ref))
489 tree init = DECL_INITIAL (ref);
491 while (TREE_CODE (init) == NOP_EXPR
492 || TREE_CODE (init) == NON_LVALUE_EXPR)
493 init = TREE_OPERAND (init, 0);
494 if (TREE_CODE (init) == ADDR_EXPR)
496 init = TREE_OPERAND (init, 0);
497 if (IS_AGGR_TYPE (TREE_TYPE (init))
498 && (TREE_CODE (init) == PARM_DECL
499 || TREE_CODE (init) == VAR_DECL))
505 if (IS_AGGR_TYPE (TREE_TYPE (instance))
506 && (TREE_CODE (instance) == RESULT_DECL
507 || TREE_CODE (instance) == PARM_DECL
508 || TREE_CODE (instance) == VAR_DECL))
510 vtbl = TYPE_BINFO_VTABLE (basetype);
511 /* Knowing the dynamic type of INSTANCE we can easily obtain
512 the correct vtable entry. We resolve this back to be in
513 terms of the primary vtable. */
514 if (TREE_CODE (vtbl) == PLUS_EXPR)
516 idx = fold (build (PLUS_EXPR,
519 build (EXACT_DIV_EXPR,
521 TREE_OPERAND (vtbl, 1),
522 TYPE_SIZE_UNIT (vtable_entry_type))));
523 vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
527 vtbl = build_vfield_ref (instance, basetype);
530 assemble_external (vtbl);
532 aref = build_array_ref (vtbl, idx);
538 build_vtbl_ref (instance, idx)
541 tree aref = build_vtbl_ref_1 (instance, idx);
544 aref = build_vtable_entry_ref (aref, instance, idx);
549 /* Given an object INSTANCE, return an expression which yields a
550 function pointer corresponding to vtable element INDEX. */
553 build_vfn_ref (instance, idx)
556 tree aref = build_vtbl_ref_1 (instance, idx);
558 /* When using function descriptors, the address of the
559 vtable entry is treated as a function pointer. */
560 if (TARGET_VTABLE_USES_DESCRIPTORS)
561 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
562 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
565 aref = build_vtable_entry_ref (aref, instance, idx);
570 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
571 for the given TYPE. */
574 get_vtable_name (type)
577 return mangle_vtbl_for_type (type);
580 /* Return an IDENTIFIER_NODE for the name of the virtual table table
587 return mangle_vtt_for_type (type);
590 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
591 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
592 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
595 build_vtable (class_type, name, vtable_type)
602 decl = build_lang_decl (VAR_DECL, name, vtable_type);
603 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
604 now to avoid confusion in mangle_decl. */
605 SET_DECL_ASSEMBLER_NAME (decl, name);
606 DECL_CONTEXT (decl) = class_type;
607 DECL_ARTIFICIAL (decl) = 1;
608 TREE_STATIC (decl) = 1;
609 TREE_READONLY (decl) = 1;
610 DECL_VIRTUAL_P (decl) = 1;
611 import_export_vtable (decl, class_type, 0);
616 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
617 or even complete. If this does not exist, create it. If COMPLETE is
618 non-zero, then complete the definition of it -- that will render it
619 impossible to actually build the vtable, but is useful to get at those
620 which are known to exist in the runtime. */
623 get_vtable_decl (type, complete)
627 tree name = get_vtable_name (type);
628 tree decl = IDENTIFIER_GLOBAL_VALUE (name);
632 my_friendly_assert (TREE_CODE (decl) == VAR_DECL
633 && DECL_VIRTUAL_P (decl), 20000118);
637 decl = build_vtable (type, name, void_type_node);
638 decl = pushdecl_top_level (decl);
639 my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
642 /* At one time the vtable info was grabbed 2 words at a time. This
643 fails on sparc unless you have 8-byte alignment. (tiemann) */
644 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
649 DECL_EXTERNAL (decl) = 1;
650 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
656 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
657 BV_VCALL_INDEX for each entry is cleared. */
660 copy_virtuals (binfo)
666 copies = copy_list (BINFO_VIRTUALS (binfo));
667 for (t = copies; t; t = TREE_CHAIN (t))
669 BV_VCALL_INDEX (t) = NULL_TREE;
670 BV_USE_VCALL_INDEX_P (t) = 0;
676 /* Build the primary virtual function table for TYPE. If BINFO is
677 non-NULL, build the vtable starting with the initial approximation
678 that it is the same as the one which is the head of the association
679 list. Returns a non-zero value if a new vtable is actually
683 build_primary_vtable (binfo, type)
689 decl = get_vtable_decl (type, /*complete=*/0);
693 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
694 /* We have already created a vtable for this base, so there's
695 no need to do it again. */
698 virtuals = copy_virtuals (binfo);
699 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
700 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
701 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
705 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
707 virtuals = NULL_TREE;
710 #ifdef GATHER_STATISTICS
712 n_vtable_elems += list_length (virtuals);
715 /* Initialize the association list for this type, based
716 on our first approximation. */
717 TYPE_BINFO_VTABLE (type) = decl;
718 TYPE_BINFO_VIRTUALS (type) = virtuals;
719 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
723 /* Give BINFO a new virtual function table which is initialized
724 with a skeleton-copy of its original initialization. The only
725 entry that changes is the `delta' entry, so we can really
726 share a lot of structure.
728 FOR_TYPE is the most derived type which caused this table to
731 Returns non-zero if we haven't met BINFO before.
733 The order in which vtables are built (by calling this function) for
734 an object must remain the same, otherwise a binary incompatibility
738 build_secondary_vtable (binfo, for_type)
739 tree binfo, for_type;
741 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
743 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
744 /* We already created a vtable for this base. There's no need to
748 /* Remember that we've created a vtable for this BINFO, so that we
749 don't try to do so again. */
750 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
752 /* Make fresh virtual list, so we can smash it later. */
753 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
755 /* Secondary vtables are laid out as part of the same structure as
756 the primary vtable. */
757 BINFO_VTABLE (binfo) = NULL_TREE;
761 /* Create a new vtable for BINFO which is the hierarchy dominated by
762 T. Return non-zero if we actually created a new vtable. */
765 make_new_vtable (t, binfo)
769 if (binfo == TYPE_BINFO (t))
770 /* In this case, it is *type*'s vtable we are modifying. We start
771 with the approximation that its vtable is that of the
772 immediate base class. */
773 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
774 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
775 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
778 /* This is our very own copy of `basetype' to play with. Later,
779 we will fill in all the virtual functions that override the
780 virtual functions in these base classes which are not defined
781 by the current type. */
782 return build_secondary_vtable (binfo, t);
785 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
786 (which is in the hierarchy dominated by T) list FNDECL as its
787 BV_FN. DELTA is the required constant adjustment from the `this'
788 pointer where the vtable entry appears to the `this' required when
789 the function is actually called. */
792 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
803 if (fndecl != BV_FN (v)
804 || !tree_int_cst_equal (delta, BV_DELTA (v)))
808 /* We need a new vtable for BINFO. */
809 if (make_new_vtable (t, binfo))
811 /* If we really did make a new vtable, we also made a copy
812 of the BINFO_VIRTUALS list. Now, we have to find the
813 corresponding entry in that list. */
814 *virtuals = BINFO_VIRTUALS (binfo);
815 while (BV_FN (*virtuals) != BV_FN (v))
816 *virtuals = TREE_CHAIN (*virtuals);
820 base_fndecl = BV_FN (v);
821 BV_DELTA (v) = delta;
822 BV_VCALL_INDEX (v) = NULL_TREE;
825 /* Now assign virtual dispatch information, if unset. We can
826 dispatch this through any overridden base function.
828 FIXME this can choose a secondary vtable if the primary is not
829 also lexically first, leading to useless conversions.
830 In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
831 ever be different from DECL_CONTEXT. */
832 if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
834 DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
835 DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
840 /* Set DECL_VINDEX for DECL. VINDEX_P is the number of virtual
841 functions present in the vtable so far. */
844 set_vindex (decl, vfuns_p)
851 *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
852 ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
853 DECL_VINDEX (decl) = build_shared_int_cst (vindex);
856 /* Add a virtual function to all the appropriate vtables for the class
857 T. DECL_VINDEX(X) should be error_mark_node, if we want to
858 allocate a new slot in our table. If it is error_mark_node, we
859 know that no other function from another vtable is overridden by X.
860 VFUNS_P keeps track of how many virtuals there are in our
861 main vtable for the type, and we build upon the NEW_VIRTUALS list
865 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
867 tree *new_virtuals_p;
868 tree *overridden_virtuals_p;
871 tree t; /* Structure type. */
875 /* If this function doesn't override anything from a base class, we
876 can just assign it a new DECL_VINDEX now. Otherwise, if it does
877 override something, we keep it around and assign its DECL_VINDEX
878 later, in modify_all_vtables. */
879 if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
880 /* We've already dealt with this function. */
883 new_virtual = make_node (TREE_LIST);
884 BV_FN (new_virtual) = fndecl;
885 BV_DELTA (new_virtual) = integer_zero_node;
887 if (DECL_VINDEX (fndecl) == error_mark_node)
889 /* FNDECL is a new virtual function; it doesn't override any
890 virtual function in a base class. */
892 /* We remember that this was the base sub-object for rtti. */
893 CLASSTYPE_RTTI (t) = t;
895 /* Now assign virtual dispatch information. */
896 set_vindex (fndecl, vfuns_p);
897 DECL_VIRTUAL_CONTEXT (fndecl) = t;
899 /* Save the state we've computed on the NEW_VIRTUALS list. */
900 TREE_CHAIN (new_virtual) = *new_virtuals_p;
901 *new_virtuals_p = new_virtual;
905 /* FNDECL overrides a function from a base class. */
906 TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
907 *overridden_virtuals_p = new_virtual;
911 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
912 the method after the class has already been defined because a
913 declaration for it was seen. (Even though that is erroneous, we
914 add the method for improved error recovery.) */
917 add_method (type, method, error_p)
922 int using = (DECL_CONTEXT (method) != type);
927 if (!CLASSTYPE_METHOD_VEC (type))
928 /* Make a new method vector. We start with 8 entries. We must
929 allocate at least two (for constructors and destructors), and
930 we're going to end up with an assignment operator at some point
933 We could use a TREE_LIST for now, and convert it to a TREE_VEC
934 in finish_struct, but we would probably waste more memory
935 making the links in the list than we would by over-allocating
936 the size of the vector here. Furthermore, we would complicate
937 all the code that expects this to be a vector. */
938 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
940 method_vec = CLASSTYPE_METHOD_VEC (type);
941 len = TREE_VEC_LENGTH (method_vec);
943 /* Constructors and destructors go in special slots. */
944 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
945 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
946 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
947 slot = CLASSTYPE_DESTRUCTOR_SLOT;
950 /* See if we already have an entry with this name. */
951 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
952 if (!TREE_VEC_ELT (method_vec, slot)
953 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec,
955 == DECL_NAME (method)))
960 /* We need a bigger method vector. */
964 /* In the non-error case, we are processing a class
965 definition. Double the size of the vector to give room
969 /* In the error case, the vector is already complete. We
970 don't expect many errors, and the rest of the front-end
971 will get confused if there are empty slots in the vector. */
975 new_vec = make_tree_vec (new_len);
976 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
977 len * sizeof (tree));
979 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
982 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
984 /* Type conversion operators have to come before ordinary
985 methods; add_conversions depends on this to speed up
986 looking for conversion operators. So, if necessary, we
987 slide some of the vector elements up. In theory, this
988 makes this algorithm O(N^2) but we don't expect many
989 conversion operators. */
990 for (slot = 2; slot < len; ++slot)
992 tree fn = TREE_VEC_ELT (method_vec, slot);
995 /* There are no more entries in the vector, so we
996 can insert the new conversion operator here. */
999 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1000 /* We can insert the new function right at the
1005 if (!TREE_VEC_ELT (method_vec, slot))
1006 /* There is nothing in the Ith slot, so we can avoid
1011 /* We know the last slot in the vector is empty
1012 because we know that at this point there's room
1013 for a new function. */
1014 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
1015 &TREE_VEC_ELT (method_vec, slot),
1016 (len - slot - 1) * sizeof (tree));
1017 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1022 if (template_class_depth (type))
1023 /* TYPE is a template class. Don't issue any errors now; wait
1024 until instantiation time to complain. */
1030 /* Check to see if we've already got this method. */
1031 for (fns = TREE_VEC_ELT (method_vec, slot);
1033 fns = OVL_NEXT (fns))
1035 tree fn = OVL_CURRENT (fns);
1037 if (TREE_CODE (fn) != TREE_CODE (method))
1040 if (TREE_CODE (method) != TEMPLATE_DECL)
1042 /* [over.load] Member function declarations with the
1043 same name and the same parameter types cannot be
1044 overloaded if any of them is a static member
1045 function declaration. */
1046 if ((DECL_STATIC_FUNCTION_P (fn)
1047 != DECL_STATIC_FUNCTION_P (method))
1050 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1051 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1053 if (! DECL_STATIC_FUNCTION_P (fn))
1054 parms1 = TREE_CHAIN (parms1);
1055 if (! DECL_STATIC_FUNCTION_P (method))
1056 parms2 = TREE_CHAIN (parms2);
1058 if (compparms (parms1, parms2))
1061 /* Defer to the local function. */
1064 cp_error ("`%#D' and `%#D' cannot be overloaded",
1070 if (!decls_match (fn, method))
1073 /* There has already been a declaration of this method
1074 or member template. */
1075 cp_error_at ("`%D' has already been declared in `%T'",
1078 /* We don't call duplicate_decls here to merge the
1079 declarations because that will confuse things if the
1080 methods have inline definitions. In particular, we
1081 will crash while processing the definitions. */
1086 /* Actually insert the new method. */
1087 TREE_VEC_ELT (method_vec, slot)
1088 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1090 /* Add the new binding. */
1091 if (!DECL_CONSTRUCTOR_P (method)
1092 && !DECL_DESTRUCTOR_P (method))
1093 push_class_level_binding (DECL_NAME (method),
1094 TREE_VEC_ELT (method_vec, slot));
1097 /* Subroutines of finish_struct. */
1099 /* Look through the list of fields for this struct, deleting
1100 duplicates as we go. This must be recursive to handle
1103 FIELD is the field which may not appear anywhere in FIELDS.
1104 FIELD_PTR, if non-null, is the starting point at which
1105 chained deletions may take place.
1106 The value returned is the first acceptable entry found
1109 Note that anonymous fields which are not of UNION_TYPE are
1110 not duplicates, they are just anonymous fields. This happens
1111 when we have unnamed bitfields, for example. */
1114 delete_duplicate_fields_1 (field, fields)
1119 if (DECL_NAME (field) == 0)
1121 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1124 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1125 fields = delete_duplicate_fields_1 (x, fields);
1130 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1132 if (DECL_NAME (x) == 0)
1134 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1136 TYPE_FIELDS (TREE_TYPE (x))
1137 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1138 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1141 fields = TREE_CHAIN (fields);
1143 TREE_CHAIN (prev) = TREE_CHAIN (x);
1146 else if (TREE_CODE (field) == USING_DECL)
1147 /* A using declaration may is allowed to appear more than
1148 once. We'll prune these from the field list later, and
1149 handle_using_decl will complain about invalid multiple
1152 else if (DECL_NAME (field) == DECL_NAME (x))
1154 if (TREE_CODE (field) == CONST_DECL
1155 && TREE_CODE (x) == CONST_DECL)
1156 cp_error_at ("duplicate enum value `%D'", x);
1157 else if (TREE_CODE (field) == CONST_DECL
1158 || TREE_CODE (x) == CONST_DECL)
1159 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1161 else if (DECL_DECLARES_TYPE_P (field)
1162 && DECL_DECLARES_TYPE_P (x))
1164 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1166 cp_error_at ("duplicate nested type `%D'", x);
1168 else if (DECL_DECLARES_TYPE_P (field)
1169 || DECL_DECLARES_TYPE_P (x))
1171 /* Hide tag decls. */
1172 if ((TREE_CODE (field) == TYPE_DECL
1173 && DECL_ARTIFICIAL (field))
1174 || (TREE_CODE (x) == TYPE_DECL
1175 && DECL_ARTIFICIAL (x)))
1177 cp_error_at ("duplicate field `%D' (as type and non-type)",
1181 cp_error_at ("duplicate member `%D'", x);
1183 fields = TREE_CHAIN (fields);
1185 TREE_CHAIN (prev) = TREE_CHAIN (x);
1193 delete_duplicate_fields (fields)
1197 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1198 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1201 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1202 legit, otherwise return 0. */
1205 alter_access (t, fdecl, access)
1212 if (!DECL_LANG_SPECIFIC (fdecl))
1213 retrofit_lang_decl (fdecl);
1215 if (DECL_DISCRIMINATOR_P (fdecl))
1218 elem = purpose_member (t, DECL_ACCESS (fdecl));
1221 if (TREE_VALUE (elem) != access)
1223 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1224 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1226 error ("conflicting access specifications for field `%s', ignored",
1227 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1231 /* They're changing the access to the same thing they changed
1232 it to before. That's OK. */
1238 enforce_access (t, fdecl);
1239 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1245 /* Process the USING_DECL, which is a member of T. */
1248 handle_using_decl (using_decl, t)
1252 tree ctype = DECL_INITIAL (using_decl);
1253 tree name = DECL_NAME (using_decl);
1255 = TREE_PRIVATE (using_decl) ? access_private_node
1256 : TREE_PROTECTED (using_decl) ? access_protected_node
1257 : access_public_node;
1259 tree flist = NULL_TREE;
1262 binfo = binfo_or_else (ctype, t);
1266 if (name == constructor_name (ctype)
1267 || name == constructor_name_full (ctype))
1269 cp_error_at ("`%D' names constructor", using_decl);
1272 if (name == constructor_name (t)
1273 || name == constructor_name_full (t))
1275 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1279 fdecl = lookup_member (binfo, name, 0, 0);
1283 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1287 if (BASELINK_P (fdecl))
1288 /* Ignore base type this came from. */
1289 fdecl = TREE_VALUE (fdecl);
1291 old_value = IDENTIFIER_CLASS_VALUE (name);
1294 if (is_overloaded_fn (old_value))
1295 old_value = OVL_CURRENT (old_value);
1297 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1300 old_value = NULL_TREE;
1303 if (is_overloaded_fn (fdecl))
1308 else if (is_overloaded_fn (old_value))
1311 /* It's OK to use functions from a base when there are functions with
1312 the same name already present in the current class. */;
1315 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1316 cp_error_at (" because of local method `%#D' with same name",
1317 OVL_CURRENT (old_value));
1321 else if (!DECL_ARTIFICIAL (old_value))
1323 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1324 cp_error_at (" because of local member `%#D' with same name", old_value);
1328 /* Make type T see field decl FDECL with access ACCESS.*/
1330 for (; flist; flist = OVL_NEXT (flist))
1332 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1333 alter_access (t, OVL_CURRENT (flist), access);
1336 alter_access (t, fdecl, access);
1339 /* Run through the base clases of T, updating
1340 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1341 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1345 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1348 int *cant_have_default_ctor_p;
1349 int *cant_have_const_ctor_p;
1350 int *no_const_asn_ref_p;
1354 int seen_non_virtual_nearly_empty_base_p;
1357 binfos = TYPE_BINFO_BASETYPES (t);
1358 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1359 seen_non_virtual_nearly_empty_base_p = 0;
1361 /* An aggregate cannot have baseclasses. */
1362 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1364 for (i = 0; i < n_baseclasses; ++i)
1369 /* Figure out what base we're looking at. */
1370 base_binfo = TREE_VEC_ELT (binfos, i);
1371 basetype = TREE_TYPE (base_binfo);
1373 /* If the type of basetype is incomplete, then we already
1374 complained about that fact (and we should have fixed it up as
1376 if (!COMPLETE_TYPE_P (basetype))
1379 /* The base type is of incomplete type. It is
1380 probably best to pretend that it does not
1382 if (i == n_baseclasses-1)
1383 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1384 TREE_VEC_LENGTH (binfos) -= 1;
1386 for (j = i; j+1 < n_baseclasses; j++)
1387 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1391 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1392 here because the case of virtual functions but non-virtual
1393 dtor is handled in finish_struct_1. */
1394 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1395 && TYPE_HAS_DESTRUCTOR (basetype))
1396 cp_warning ("base class `%#T' has a non-virtual destructor",
1399 /* If the base class doesn't have copy constructors or
1400 assignment operators that take const references, then the
1401 derived class cannot have such a member automatically
1403 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1404 *cant_have_const_ctor_p = 1;
1405 if (TYPE_HAS_ASSIGN_REF (basetype)
1406 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1407 *no_const_asn_ref_p = 1;
1408 /* Similarly, if the base class doesn't have a default
1409 constructor, then the derived class won't have an
1410 automatically generated default constructor. */
1411 if (TYPE_HAS_CONSTRUCTOR (basetype)
1412 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1414 *cant_have_default_ctor_p = 1;
1415 if (! TYPE_HAS_CONSTRUCTOR (t))
1416 cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1420 if (TREE_VIA_VIRTUAL (base_binfo))
1421 /* A virtual base does not effect nearly emptiness. */
1423 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1425 if (seen_non_virtual_nearly_empty_base_p)
1426 /* And if there is more than one nearly empty base, then the
1427 derived class is not nearly empty either. */
1428 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1430 /* Remember we've seen one. */
1431 seen_non_virtual_nearly_empty_base_p = 1;
1433 else if (!is_empty_class (basetype))
1434 /* If the base class is not empty or nearly empty, then this
1435 class cannot be nearly empty. */
1436 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1438 /* A lot of properties from the bases also apply to the derived
1440 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1441 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1442 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1443 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1444 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1445 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1446 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1447 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1448 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1449 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1453 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1454 TO. Move primary information from FROM to TO, and recursively traverse
1455 into FROM's bases. The heirarchy is dominated by TYPE. MAPPINGS is an
1456 assoc list of binfos that have already been reseated. */
1459 force_canonical_binfo_r (to, from, type, mappings)
1465 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1467 my_friendly_assert (to != from, 20010905);
1468 BINFO_INDIRECT_PRIMARY_P (to)
1469 = BINFO_INDIRECT_PRIMARY_P (from);
1470 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1471 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1472 BINFO_UNSHARED_MARKED (from) = 0;
1473 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1474 BINFO_LOST_PRIMARY_P (from) = 0;
1475 if (BINFO_PRIMARY_P (from))
1477 tree primary = BINFO_PRIMARY_BASE_OF (from);
1480 /* We might have just moved the primary base too, see if it's on our
1482 assoc = purpose_member (primary, mappings);
1484 primary = TREE_VALUE (assoc);
1485 BINFO_PRIMARY_BASE_OF (to) = primary;
1486 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1488 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1490 mappings = tree_cons (from, to, mappings);
1492 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1493 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1495 tree from_primary = get_primary_binfo (from);
1497 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1498 force_canonical_binfo (get_primary_binfo (to), from_primary,
1502 for (i = 0; i != n_baseclasses; i++)
1504 tree from_binfo = BINFO_BASETYPE (from, i);
1505 tree to_binfo = BINFO_BASETYPE (to, i);
1507 if (TREE_VIA_VIRTUAL (from_binfo))
1509 if (BINFO_PRIMARY_P (from_binfo) &&
1510 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1511 /* This base is a primary of some binfo we have already
1512 reseated. We must reseat this one too. */
1513 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1516 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1520 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1521 make TO the canonical binfo, within the heirarchy dominated by TYPE.
1522 MAPPINGS is an assoc list of binfos that have already been reseated.
1523 Adjust any non-virtual bases within FROM, and also move any virtual bases
1524 which are canonical. This complication arises because selecting primary
1525 bases walks in inheritance graph order, but we don't share binfos for
1526 virtual bases, hence we can fill in the primaries for a virtual base,
1527 and then discover that a later base requires the virtual as its
1531 force_canonical_binfo (to, from, type, mappings)
1537 tree assoc = purpose_member (BINFO_TYPE (to),
1538 CLASSTYPE_VBASECLASSES (type));
1539 if (TREE_VALUE (assoc) != to)
1541 TREE_VALUE (assoc) = to;
1542 force_canonical_binfo_r (to, from, type, mappings);
1546 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1547 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1548 otherwise (because something else has already made it primary). */
1551 mark_primary_virtual_base (base_binfo, type)
1555 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1557 if (BINFO_PRIMARY_P (shared_binfo))
1559 /* It's already allocated in the hierarchy. BINFO won't have a
1560 primary base in this hierachy, even though the complete object
1561 BINFO is for, would do. */
1565 /* We need to make sure that the assoc list
1566 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1567 primary BINFO for the virtual base, as this is the one
1568 that'll really exist. */
1569 if (base_binfo != shared_binfo)
1570 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1575 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1576 base, then BINFO has no primary base in this graph. Called from
1577 mark_primary_bases. DATA is the most derived type. */
1579 static tree dfs_unshared_virtual_bases (binfo, data)
1583 tree t = (tree) data;
1585 if (!BINFO_UNSHARED_MARKED (binfo)
1586 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1588 /* This morally virtual base has a primary base when it
1589 is a complete object. We need to locate the shared instance
1590 of this binfo in the type dominated by T. We duplicate the
1591 primary base information from there to here. */
1595 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1596 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1598 unshared_base = get_original_base (binfo,
1599 binfo_for_vbase (BINFO_TYPE (vbase),
1601 my_friendly_assert (unshared_base != binfo, 20010612);
1602 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1603 if (!BINFO_LOST_PRIMARY_P (binfo))
1604 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1607 if (binfo != TYPE_BINFO (t))
1608 /* The vtable fields will have been copied when duplicating the
1609 base binfos. That information is bogus, make sure we don't try
1611 BINFO_VTABLE (binfo) = NULL_TREE;
1613 /* If this is a virtual primary base, make sure its offset matches
1614 that which it is primary for. */
1615 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1616 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1618 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1619 BINFO_OFFSET (binfo));
1620 if (!integer_zerop (delta))
1621 propagate_binfo_offsets (binfo, delta, t);
1624 BINFO_UNSHARED_MARKED (binfo) = 0;
1628 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1629 dominated by TYPE that are primary bases. */
1632 mark_primary_bases (type)
1637 /* Walk the bases in inheritance graph order. */
1638 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1642 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1643 /* Not a dynamic base. */
1646 base_binfo = get_primary_binfo (binfo);
1648 if (TREE_VIA_VIRTUAL (base_binfo))
1649 base_binfo = mark_primary_virtual_base (base_binfo, type);
1652 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1654 BINFO_LOST_PRIMARY_P (binfo) = 1;
1656 BINFO_UNSHARED_MARKED (binfo) = 1;
1658 /* There could remain unshared morally virtual bases which were not
1659 visited in the inheritance graph walk. These bases will have lost
1660 their virtual primary base (should they have one). We must now
1661 find them. Also we must fix up the BINFO_OFFSETs of primary
1662 virtual bases. We could not do that as we went along, as they
1663 were originally copied from the bases we inherited from by
1664 unshare_base_binfos. That may have decided differently about
1665 where a virtual primary base went. */
1666 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1669 /* Make the BINFO the primary base of T. */
1672 set_primary_base (t, binfo, vfuns_p)
1679 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1680 basetype = BINFO_TYPE (binfo);
1681 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1682 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1683 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1684 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1685 *vfuns_p = CLASSTYPE_VSIZE (basetype);
1688 /* Determine the primary class for T. */
1691 determine_primary_base (t, vfuns_p)
1695 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1699 /* If there are no baseclasses, there is certainly no primary base. */
1700 if (n_baseclasses == 0)
1703 type_binfo = TYPE_BINFO (t);
1705 for (i = 0; i < n_baseclasses; i++)
1707 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1708 tree basetype = BINFO_TYPE (base_binfo);
1710 if (TYPE_CONTAINS_VPTR_P (basetype))
1712 /* Even a virtual baseclass can contain our RTTI
1713 information. But, we prefer a non-virtual polymorphic
1715 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1716 CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1718 /* We prefer a non-virtual base, although a virtual one will
1720 if (TREE_VIA_VIRTUAL (base_binfo))
1723 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1725 set_primary_base (t, base_binfo, vfuns_p);
1726 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1732 /* Only add unique vfields, and flatten them out as we go. */
1733 for (vfields = CLASSTYPE_VFIELDS (basetype);
1735 vfields = TREE_CHAIN (vfields))
1736 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1737 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1738 CLASSTYPE_VFIELDS (t)
1739 = tree_cons (base_binfo,
1740 VF_BASETYPE_VALUE (vfields),
1741 CLASSTYPE_VFIELDS (t));
1746 if (!TYPE_VFIELD (t))
1747 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1749 /* Find the indirect primary bases - those virtual bases which are primary
1750 bases of something else in this hierarchy. */
1751 for (vbases = CLASSTYPE_VBASECLASSES (t);
1753 vbases = TREE_CHAIN (vbases))
1755 tree vbase_binfo = TREE_VALUE (vbases);
1757 /* See if this virtual base is an indirect primary base. To be so,
1758 it must be a primary base within the hierarchy of one of our
1760 for (i = 0; i < n_baseclasses; ++i)
1762 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1765 for (v = CLASSTYPE_VBASECLASSES (basetype);
1769 tree base_vbase = TREE_VALUE (v);
1771 if (BINFO_PRIMARY_P (base_vbase)
1772 && same_type_p (BINFO_TYPE (base_vbase),
1773 BINFO_TYPE (vbase_binfo)))
1775 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1780 /* If we've discovered that this virtual base is an indirect
1781 primary base, then we can move on to the next virtual
1783 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1788 /* A "nearly-empty" virtual base class can be the primary base
1789 class, if no non-virtual polymorphic base can be found. */
1790 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1792 /* If not NULL, this is the best primary base candidate we have
1794 tree candidate = NULL_TREE;
1797 /* Loop over the baseclasses. */
1798 for (base_binfo = TYPE_BINFO (t);
1800 base_binfo = TREE_CHAIN (base_binfo))
1802 tree basetype = BINFO_TYPE (base_binfo);
1804 if (TREE_VIA_VIRTUAL (base_binfo)
1805 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1807 /* If this is not an indirect primary base, then it's
1808 definitely our primary base. */
1809 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1811 candidate = base_binfo;
1815 /* If this is an indirect primary base, it still could be
1816 our primary base -- unless we later find there's another
1817 nearly-empty virtual base that isn't an indirect
1820 candidate = base_binfo;
1824 /* If we've got a primary base, use it. */
1827 set_primary_base (t, candidate, vfuns_p);
1828 CLASSTYPE_VFIELDS (t)
1829 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1833 /* Mark the primary base classes at this point. */
1834 mark_primary_bases (t);
1837 /* Set memoizing fields and bits of T (and its variants) for later
1841 finish_struct_bits (t)
1844 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1846 /* Fix up variants (if any). */
1847 tree variants = TYPE_NEXT_VARIANT (t);
1850 /* These fields are in the _TYPE part of the node, not in
1851 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1852 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1853 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1854 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1855 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1856 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1858 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1859 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1860 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1861 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1862 /* Copy whatever these are holding today. */
1863 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1864 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1865 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1866 TYPE_SIZE (variants) = TYPE_SIZE (t);
1867 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1868 variants = TYPE_NEXT_VARIANT (variants);
1871 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1872 /* For a class w/o baseclasses, `finish_struct' has set
1873 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1874 definition). Similarly for a class whose base classes do not
1875 have vtables. When neither of these is true, we might have
1876 removed abstract virtuals (by providing a definition), added
1877 some (by declaring new ones), or redeclared ones from a base
1878 class. We need to recalculate what's really an abstract virtual
1879 at this point (by looking in the vtables). */
1880 get_pure_virtuals (t);
1884 /* Notice whether this class has type conversion functions defined. */
1885 tree binfo = TYPE_BINFO (t);
1886 tree binfos = BINFO_BASETYPES (binfo);
1889 for (i = n_baseclasses-1; i >= 0; i--)
1891 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1893 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1897 /* If this type has a copy constructor, force its mode to be BLKmode, and
1898 force its TREE_ADDRESSABLE bit to be nonzero. This will cause it to
1899 be passed by invisible reference and prevent it from being returned in
1902 Also do this if the class has BLKmode but can still be returned in
1903 registers, since function_cannot_inline_p won't let us inline
1904 functions returning such a type. This affects the HP-PA. */
1905 if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1906 || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1907 && CLASSTYPE_NON_AGGREGATE (t)))
1910 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1911 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1913 TYPE_MODE (variants) = BLKmode;
1914 TREE_ADDRESSABLE (variants) = 1;
1919 /* Issue warnings about T having private constructors, but no friends,
1922 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1923 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1924 non-private static member functions. */
1927 maybe_warn_about_overly_private_class (t)
1930 int has_member_fn = 0;
1931 int has_nonprivate_method = 0;
1934 if (!warn_ctor_dtor_privacy
1935 /* If the class has friends, those entities might create and
1936 access instances, so we should not warn. */
1937 || (CLASSTYPE_FRIEND_CLASSES (t)
1938 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1939 /* We will have warned when the template was declared; there's
1940 no need to warn on every instantiation. */
1941 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1942 /* There's no reason to even consider warning about this
1946 /* We only issue one warning, if more than one applies, because
1947 otherwise, on code like:
1950 // Oops - forgot `public:'
1956 we warn several times about essentially the same problem. */
1958 /* Check to see if all (non-constructor, non-destructor) member
1959 functions are private. (Since there are no friends or
1960 non-private statics, we can't ever call any of the private member
1962 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1963 /* We're not interested in compiler-generated methods; they don't
1964 provide any way to call private members. */
1965 if (!DECL_ARTIFICIAL (fn))
1967 if (!TREE_PRIVATE (fn))
1969 if (DECL_STATIC_FUNCTION_P (fn))
1970 /* A non-private static member function is just like a
1971 friend; it can create and invoke private member
1972 functions, and be accessed without a class
1976 has_nonprivate_method = 1;
1979 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1983 if (!has_nonprivate_method && has_member_fn)
1985 /* There are no non-private methods, and there's at least one
1986 private member function that isn't a constructor or
1987 destructor. (If all the private members are
1988 constructors/destructors we want to use the code below that
1989 issues error messages specifically referring to
1990 constructors/destructors.) */
1992 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1993 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1994 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1995 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1997 has_nonprivate_method = 1;
2000 if (!has_nonprivate_method)
2002 cp_warning ("all member functions in class `%T' are private", t);
2007 /* Even if some of the member functions are non-private, the class
2008 won't be useful for much if all the constructors or destructors
2009 are private: such an object can never be created or destroyed. */
2010 if (TYPE_HAS_DESTRUCTOR (t))
2012 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2014 if (TREE_PRIVATE (dtor))
2016 cp_warning ("`%#T' only defines a private destructor and has no friends",
2022 if (TYPE_HAS_CONSTRUCTOR (t))
2024 int nonprivate_ctor = 0;
2026 /* If a non-template class does not define a copy
2027 constructor, one is defined for it, enabling it to avoid
2028 this warning. For a template class, this does not
2029 happen, and so we would normally get a warning on:
2031 template <class T> class C { private: C(); };
2033 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
2034 complete non-template or fully instantiated classes have this
2036 if (!TYPE_HAS_INIT_REF (t))
2037 nonprivate_ctor = 1;
2039 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2043 tree ctor = OVL_CURRENT (fn);
2044 /* Ideally, we wouldn't count copy constructors (or, in
2045 fact, any constructor that takes an argument of the
2046 class type as a parameter) because such things cannot
2047 be used to construct an instance of the class unless
2048 you already have one. But, for now at least, we're
2050 if (! TREE_PRIVATE (ctor))
2052 nonprivate_ctor = 1;
2057 if (nonprivate_ctor == 0)
2059 cp_warning ("`%#T' only defines private constructors and has no friends",
2066 /* Function to help qsort sort FIELD_DECLs by name order. */
2069 field_decl_cmp (x, y)
2072 if (DECL_NAME (*x) == DECL_NAME (*y))
2073 /* A nontype is "greater" than a type. */
2074 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2075 if (DECL_NAME (*x) == NULL_TREE)
2077 if (DECL_NAME (*y) == NULL_TREE)
2079 if (DECL_NAME (*x) < DECL_NAME (*y))
2084 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
2087 method_name_cmp (m1, m2)
2088 const tree *m1, *m2;
2090 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2092 if (*m1 == NULL_TREE)
2094 if (*m2 == NULL_TREE)
2096 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2101 /* Warn about duplicate methods in fn_fields. Also compact method
2102 lists so that lookup can be made faster.
2104 Data Structure: List of method lists. The outer list is a
2105 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2106 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2107 links the entire list of methods for TYPE_METHODS. Friends are
2108 chained in the same way as member functions (? TREE_CHAIN or
2109 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2110 list. That allows them to be quickly deleted, and requires no
2113 Sort methods that are not special (i.e., constructors, destructors,
2114 and type conversion operators) so that we can find them faster in
2118 finish_struct_methods (t)
2125 if (!TYPE_METHODS (t))
2127 /* Clear these for safety; perhaps some parsing error could set
2128 these incorrectly. */
2129 TYPE_HAS_CONSTRUCTOR (t) = 0;
2130 TYPE_HAS_DESTRUCTOR (t) = 0;
2131 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2135 method_vec = CLASSTYPE_METHOD_VEC (t);
2136 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2137 len = TREE_VEC_LENGTH (method_vec);
2139 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2140 and the next few with type conversion operators (if any). */
2141 for (fn_fields = TYPE_METHODS (t); fn_fields;
2142 fn_fields = TREE_CHAIN (fn_fields))
2143 /* Clear out this flag. */
2144 DECL_IN_AGGR_P (fn_fields) = 0;
2146 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2147 /* We thought there was a destructor, but there wasn't. Some
2148 parse errors cause this anomalous situation. */
2149 TYPE_HAS_DESTRUCTOR (t) = 0;
2151 /* Issue warnings about private constructors and such. If there are
2152 no methods, then some public defaults are generated. */
2153 maybe_warn_about_overly_private_class (t);
2155 /* Now sort the methods. */
2156 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2158 TREE_VEC_LENGTH (method_vec) = len;
2160 /* The type conversion ops have to live at the front of the vec, so we
2162 for (slot = 2; slot < len; ++slot)
2164 tree fn = TREE_VEC_ELT (method_vec, slot);
2166 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2170 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2171 (int (*)(const void *, const void *))method_name_cmp);
2174 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2177 duplicate_tag_error (t)
2180 cp_error ("redefinition of `%#T'", t);
2181 cp_error_at ("previous definition of `%#T'", t);
2183 /* Pretend we haven't defined this type. */
2185 /* All of the component_decl's were TREE_CHAINed together in the parser.
2186 finish_struct_methods walks these chains and assembles all methods with
2187 the same base name into DECL_CHAINs. Now we don't need the parser chains
2188 anymore, so we unravel them. */
2190 /* This used to be in finish_struct, but it turns out that the
2191 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2193 if (CLASSTYPE_METHOD_VEC (t))
2195 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2196 int i, len = TREE_VEC_LENGTH (method_vec);
2197 for (i = 0; i < len; i++)
2199 tree unchain = TREE_VEC_ELT (method_vec, i);
2200 while (unchain != NULL_TREE)
2202 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2203 unchain = OVL_NEXT (unchain);
2208 if (TYPE_LANG_SPECIFIC (t))
2210 tree binfo = TYPE_BINFO (t);
2211 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2212 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2213 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2214 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2216 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2217 BINFO_BASETYPES(binfo) = NULL_TREE;
2219 TYPE_BINFO (t) = binfo;
2220 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2221 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2222 TYPE_REDEFINED (t) = 1;
2223 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2224 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2226 TYPE_SIZE (t) = NULL_TREE;
2227 TYPE_MODE (t) = VOIDmode;
2228 TYPE_FIELDS (t) = NULL_TREE;
2229 TYPE_METHODS (t) = NULL_TREE;
2230 TYPE_VFIELD (t) = NULL_TREE;
2231 TYPE_CONTEXT (t) = NULL_TREE;
2233 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2234 TYPE_LANG_FLAG_0 (t) = 0;
2235 TYPE_LANG_FLAG_1 (t) = 0;
2236 TYPE_LANG_FLAG_2 (t) = 0;
2237 TYPE_LANG_FLAG_3 (t) = 0;
2238 TYPE_LANG_FLAG_4 (t) = 0;
2239 TYPE_LANG_FLAG_5 (t) = 0;
2240 TYPE_LANG_FLAG_6 (t) = 0;
2241 /* But not this one. */
2242 SET_IS_AGGR_TYPE (t, 1);
2245 /* Make BINFO's vtable have N entries, including RTTI entries,
2246 vbase and vcall offsets, etc. Set its type and call the backend
2250 layout_vtable_decl (binfo, n)
2257 atype = build_cplus_array_type (vtable_entry_type,
2258 build_index_type (size_int (n - 1)));
2259 layout_type (atype);
2261 /* We may have to grow the vtable. */
2262 vtable = get_vtbl_decl_for_binfo (binfo);
2263 if (!same_type_p (TREE_TYPE (vtable), atype))
2265 TREE_TYPE (vtable) = atype;
2266 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2267 layout_decl (vtable, 0);
2269 /* At one time the vtable info was grabbed 2 words at a time. This
2270 fails on Sparc unless you have 8-byte alignment. */
2271 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2272 DECL_ALIGN (vtable));
2276 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2277 have the same signature. */
2280 same_signature_p (fndecl, base_fndecl)
2281 tree fndecl, base_fndecl;
2283 /* One destructor overrides another if they are the same kind of
2285 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2286 && special_function_p (base_fndecl) == special_function_p (fndecl))
2288 /* But a non-destructor never overrides a destructor, nor vice
2289 versa, nor do different kinds of destructors override
2290 one-another. For example, a complete object destructor does not
2291 override a deleting destructor. */
2292 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2295 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2297 tree types, base_types;
2298 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2299 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2300 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2301 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2302 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2308 typedef struct find_final_overrider_data_s {
2309 /* The function for which we are trying to find a final overrider. */
2311 /* The base class in which the function was declared. */
2312 tree declaring_base;
2313 /* The most derived class in the hierarchy. */
2314 tree most_derived_type;
2315 /* The final overriding function. */
2317 /* The functions that we thought might be final overriders, but
2320 /* The BINFO for the class in which the final overriding function
2322 tree overriding_base;
2323 } find_final_overrider_data;
2325 /* Called from find_final_overrider via dfs_walk. */
2328 dfs_find_final_overrider (binfo, data)
2332 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2334 if (same_type_p (BINFO_TYPE (binfo),
2335 BINFO_TYPE (ffod->declaring_base))
2336 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2337 BINFO_OFFSET (ffod->declaring_base)))
2342 /* We haven't found an overrider yet. */
2344 /* We've found a path to the declaring base. Walk down the path
2345 looking for an overrider for FN. */
2346 for (path = reverse_path (binfo);
2348 path = TREE_CHAIN (path))
2350 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2356 /* If we found an overrider, record the overriding function, and
2357 the base from which it came. */
2362 /* Assume the path is non-virtual. See if there are any
2363 virtual bases from (but not including) the overrider up
2364 to and including the base where the function is
2366 for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2367 if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2369 base = ffod->declaring_base;
2373 /* If we didn't already have an overrider, or any
2374 candidates, then this function is the best candidate so
2376 if (!ffod->overriding_fn && !ffod->candidates)
2378 ffod->overriding_fn = method;
2379 ffod->overriding_base = TREE_VALUE (path);
2381 else if (ffod->overriding_fn)
2383 /* We had a best overrider; let's see how this compares. */
2385 if (ffod->overriding_fn == method
2386 && (tree_int_cst_equal
2387 (BINFO_OFFSET (TREE_VALUE (path)),
2388 BINFO_OFFSET (ffod->overriding_base))))
2389 /* We found the same overrider we already have, and in the
2390 same place; it's still the best. */;
2391 else if (strictly_overrides (ffod->overriding_fn, method))
2392 /* The old function overrides this function; it's still the
2394 else if (strictly_overrides (method, ffod->overriding_fn))
2396 /* The new function overrides the old; it's now the
2398 ffod->overriding_fn = method;
2399 ffod->overriding_base = TREE_VALUE (path);
2405 = build_tree_list (NULL_TREE,
2406 ffod->overriding_fn);
2407 if (method != ffod->overriding_fn)
2409 = tree_cons (NULL_TREE, method, ffod->candidates);
2410 ffod->overriding_fn = NULL_TREE;
2411 ffod->overriding_base = NULL_TREE;
2416 /* We had a list of ambiguous overrides; let's see how this
2417 new one compares. */
2420 bool incomparable = false;
2422 /* If there were previous candidates, and this function
2423 overrides all of them, then it is the new best
2425 for (candidates = ffod->candidates;
2427 candidates = TREE_CHAIN (candidates))
2429 /* If the candidate overrides the METHOD, then we
2430 needn't worry about it any further. */
2431 if (strictly_overrides (TREE_VALUE (candidates),
2438 /* If the METHOD doesn't override the candidate,
2439 then it is incomporable. */
2440 if (!strictly_overrides (method,
2441 TREE_VALUE (candidates)))
2442 incomparable = true;
2445 /* If METHOD overrode all the candidates, then it is the
2446 new best candidate. */
2447 if (!candidates && !incomparable)
2449 ffod->overriding_fn = method;
2450 ffod->overriding_base = TREE_VALUE (path);
2451 ffod->candidates = NULL_TREE;
2453 /* If METHOD didn't override all the candidates, then it
2454 is another candidate. */
2455 else if (method && incomparable)
2457 = tree_cons (NULL_TREE, method, ffod->candidates);
2465 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2466 FN and whose TREE_VALUE is the binfo for the base where the
2467 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2468 base object in which FN is declared. */
2471 find_final_overrider (t, binfo, fn)
2476 find_final_overrider_data ffod;
2478 /* Getting this right is a little tricky. This is legal:
2480 struct S { virtual void f (); };
2481 struct T { virtual void f (); };
2482 struct U : public S, public T { };
2484 even though calling `f' in `U' is ambiguous. But,
2486 struct R { virtual void f(); };
2487 struct S : virtual public R { virtual void f (); };
2488 struct T : virtual public R { virtual void f (); };
2489 struct U : public S, public T { };
2491 is not -- there's no way to decide whether to put `S::f' or
2492 `T::f' in the vtable for `R'.
2494 The solution is to look at all paths to BINFO. If we find
2495 different overriders along any two, then there is a problem. */
2497 ffod.declaring_base = binfo;
2498 ffod.most_derived_type = t;
2499 ffod.overriding_fn = NULL_TREE;
2500 ffod.overriding_base = NULL_TREE;
2501 ffod.candidates = NULL_TREE;
2503 dfs_walk (TYPE_BINFO (t),
2504 dfs_find_final_overrider,
2508 /* If there was no winner, issue an error message. */
2509 if (!ffod.overriding_fn)
2511 cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2512 return error_mark_node;
2515 return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2518 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2519 the signature of FUNCTION_DECL FN, or NULL_TREE if none. In other words,
2520 the function that the slot in T's primary vtable points to. */
2522 static tree get_matching_virtual PARAMS ((tree, tree));
2524 get_matching_virtual (t, fn)
2529 for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2530 if (same_signature_p (BV_FN (f), fn))
2535 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2536 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2537 corresponding position in the BINFO_VIRTUALS list. */
2540 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2552 /* Find the nearest primary base (possibly binfo itself) which defines
2553 this function; this is the class the caller will convert to when
2554 calling FN through BINFO. */
2555 for (b = binfo; ; b = get_primary_binfo (b))
2557 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2562 /* Find the final overrider. */
2563 overrider = find_final_overrider (t, b, fn);
2564 if (overrider == error_mark_node)
2567 /* Assume that we will produce a thunk that convert all the way to
2568 the final overrider, and not to an intermediate virtual base. */
2569 virtual_base = NULL_TREE;
2571 /* We will convert to an intermediate virtual base first, and then
2572 use the vcall offset located there to finish the conversion. */
2575 /* If we find the final overrider, then we can stop
2577 if (same_type_p (BINFO_TYPE (b),
2578 BINFO_TYPE (TREE_VALUE (overrider))))
2581 /* If we find a virtual base, and we haven't yet found the
2582 overrider, then there is a virtual base between the
2583 declaring base (first_defn) and the final overrider. */
2584 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2587 b = BINFO_INHERITANCE_CHAIN (b);
2590 /* Compute the constant adjustment to the `this' pointer. The
2591 `this' pointer, when this function is called, will point at BINFO
2592 (or one of its primary bases, which are at the same offset). */
2595 /* The `this' pointer needs to be adjusted from the declaration to
2596 the nearest virtual base. */
2597 delta = size_diffop (BINFO_OFFSET (virtual_base),
2598 BINFO_OFFSET (first_defn));
2601 /* The `this' pointer needs to be adjusted from pointing to
2602 BINFO to pointing at the base where the final overrider
2604 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2605 BINFO_OFFSET (binfo));
2607 if (! integer_zerop (delta))
2609 /* We'll need a thunk. But if we have a (perhaps formerly)
2610 primary virtual base, we have a vcall slot for this function,
2611 so we can use it rather than create a non-virtual thunk. */
2613 b = get_primary_binfo (first_defn);
2614 for (; b; b = get_primary_binfo (b))
2616 tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2618 /* b doesn't have this function; no suitable vbase. */
2620 if (TREE_VIA_VIRTUAL (b))
2622 /* Found one; we can treat ourselves as a virtual base. */
2623 virtual_base = binfo;
2624 delta = size_zero_node;
2631 modify_vtable_entry (t,
2633 TREE_PURPOSE (overrider),
2638 BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2641 /* Called from modify_all_vtables via dfs_walk. */
2644 dfs_modify_vtables (binfo, data)
2648 if (/* There's no need to modify the vtable for a non-virtual
2649 primary base; we're not going to use that vtable anyhow.
2650 We do still need to do this for virtual primary bases, as they
2651 could become non-primary in a construction vtable. */
2652 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2653 /* Similarly, a base without a vtable needs no modification. */
2654 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2662 make_new_vtable (t, binfo);
2664 /* Now, go through each of the virtual functions in the virtual
2665 function table for BINFO. Find the final overrider, and
2666 update the BINFO_VIRTUALS list appropriately. */
2667 for (virtuals = BINFO_VIRTUALS (binfo),
2668 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2670 virtuals = TREE_CHAIN (virtuals),
2671 old_virtuals = TREE_CHAIN (old_virtuals))
2672 update_vtable_entry_for_fn (t,
2674 BV_FN (old_virtuals),
2678 SET_BINFO_MARKED (binfo);
2683 /* Update all of the primary and secondary vtables for T. Create new
2684 vtables as required, and initialize their RTTI information. Each
2685 of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2686 function from a base class; find and modify the appropriate entries
2687 to point to the overriding functions. Returns a list, in
2688 declaration order, of the functions that are overridden in this
2689 class, but do not appear in the primary base class vtable, and
2690 which should therefore be appended to the end of the vtable for T. */
2693 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2696 tree overridden_virtuals;
2698 tree binfo = TYPE_BINFO (t);
2701 /* Update all of the vtables. */
2704 dfs_unmarked_real_bases_queue_p,
2706 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2708 /* Include overriding functions for secondary vtables in our primary
2710 for (fnsp = &overridden_virtuals; *fnsp; )
2712 tree fn = TREE_VALUE (*fnsp);
2714 if (!BINFO_VIRTUALS (binfo)
2715 || !value_member (fn, BINFO_VIRTUALS (binfo)))
2717 /* Set the vtable index. */
2718 set_vindex (fn, vfuns_p);
2719 /* We don't need to convert to a base class when calling
2721 DECL_VIRTUAL_CONTEXT (fn) = t;
2723 /* We don't need to adjust the `this' pointer when
2724 calling this function. */
2725 BV_DELTA (*fnsp) = integer_zero_node;
2726 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2728 /* This is an overridden function not already in our
2730 fnsp = &TREE_CHAIN (*fnsp);
2733 /* We've already got an entry for this function. Skip it. */
2734 *fnsp = TREE_CHAIN (*fnsp);
2737 return overridden_virtuals;
2740 /* Here, we already know that they match in every respect.
2741 All we have to check is where they had their declarations. */
2744 strictly_overrides (fndecl1, fndecl2)
2745 tree fndecl1, fndecl2;
2747 int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2748 DECL_CONTEXT (fndecl1),
2750 if (distance == -2 || distance > 0)
2755 /* Get the base virtual function declarations in T that are either
2756 overridden or hidden by FNDECL as a list. We set TREE_PURPOSE with
2757 the overrider/hider. */
2760 get_basefndecls (fndecl, t)
2763 tree methods = TYPE_METHODS (t);
2764 tree base_fndecls = NULL_TREE;
2765 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2766 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2770 if (TREE_CODE (methods) == FUNCTION_DECL
2771 && DECL_VINDEX (methods) != NULL_TREE
2772 && DECL_NAME (fndecl) == DECL_NAME (methods))
2773 base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2775 methods = TREE_CHAIN (methods);
2779 return base_fndecls;
2781 for (i = 0; i < n_baseclasses; i++)
2783 tree base_binfo = TREE_VEC_ELT (binfos, i);
2784 tree basetype = BINFO_TYPE (base_binfo);
2786 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2790 return base_fndecls;
2793 /* Mark the functions that have been hidden with their overriders.
2794 Since we start out with all functions already marked with a hider,
2795 no need to mark functions that are just hidden.
2797 Subroutine of warn_hidden. */
2800 mark_overriders (fndecl, base_fndecls)
2801 tree fndecl, base_fndecls;
2803 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2804 if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2805 TREE_PURPOSE (base_fndecls) = fndecl;
2808 /* If this declaration supersedes the declaration of
2809 a method declared virtual in the base class, then
2810 mark this field as being virtual as well. */
2813 check_for_override (decl, ctype)
2816 if (TREE_CODE (decl) == TEMPLATE_DECL)
2817 /* In [temp.mem] we have:
2819 A specialization of a member function template does not
2820 override a virtual function from a base class. */
2822 if ((DECL_DESTRUCTOR_P (decl)
2823 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2824 && look_for_overrides (ctype, decl)
2825 && !DECL_STATIC_FUNCTION_P (decl))
2827 /* Set DECL_VINDEX to a value that is neither an
2828 INTEGER_CST nor the error_mark_node so that
2829 add_virtual_function will realize this is an
2830 overriding function. */
2831 DECL_VINDEX (decl) = decl;
2833 if (DECL_VIRTUAL_P (decl))
2835 if (DECL_VINDEX (decl) == NULL_TREE)
2836 DECL_VINDEX (decl) = error_mark_node;
2837 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2841 /* Warn about hidden virtual functions that are not overridden in t.
2842 We know that constructors and destructors don't apply. */
2848 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2849 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2852 /* We go through each separately named virtual function. */
2853 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2855 tree fns = TREE_VEC_ELT (method_vec, i);
2856 tree fndecl = NULL_TREE;
2858 tree base_fndecls = NULL_TREE;
2859 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2860 int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2862 /* First see if we have any virtual functions in this batch. */
2863 for (; fns; fns = OVL_NEXT (fns))
2865 fndecl = OVL_CURRENT (fns);
2866 if (DECL_VINDEX (fndecl))
2870 if (fns == NULL_TREE)
2873 /* First we get a list of all possible functions that might be
2874 hidden from each base class. */
2875 for (i = 0; i < n_baseclasses; i++)
2877 tree base_binfo = TREE_VEC_ELT (binfos, i);
2878 tree basetype = BINFO_TYPE (base_binfo);
2880 base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2884 fns = OVL_NEXT (fns);
2886 /* ...then mark up all the base functions with overriders, preferring
2887 overriders to hiders. */
2889 for (; fns; fns = OVL_NEXT (fns))
2891 fndecl = OVL_CURRENT (fns);
2892 if (DECL_VINDEX (fndecl))
2893 mark_overriders (fndecl, base_fndecls);
2896 /* Now give a warning for all base functions without overriders,
2897 as they are hidden. */
2898 for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2899 if (!same_signature_p (TREE_PURPOSE (base_fndecls),
2900 TREE_VALUE (base_fndecls)))
2902 /* Here we know it is a hider, and no overrider exists. */
2903 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2904 cp_warning_at (" by `%D'", TREE_PURPOSE (base_fndecls));
2909 /* Check for things that are invalid. There are probably plenty of other
2910 things we should check for also. */
2913 finish_struct_anon (t)
2918 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2920 if (TREE_STATIC (field))
2922 if (TREE_CODE (field) != FIELD_DECL)
2925 if (DECL_NAME (field) == NULL_TREE
2926 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2928 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2929 for (; elt; elt = TREE_CHAIN (elt))
2931 /* We're generally only interested in entities the user
2932 declared, but we also find nested classes by noticing
2933 the TYPE_DECL that we create implicitly. You're
2934 allowed to put one anonymous union inside another,
2935 though, so we explicitly tolerate that. We use
2936 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2937 we also allow unnamed types used for defining fields. */
2938 if (DECL_ARTIFICIAL (elt)
2939 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2940 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2943 if (DECL_NAME (elt) == constructor_name (t))
2944 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2947 if (TREE_CODE (elt) != FIELD_DECL)
2949 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2954 if (TREE_PRIVATE (elt))
2955 cp_pedwarn_at ("private member `%#D' in anonymous union",
2957 else if (TREE_PROTECTED (elt))
2958 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2961 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2962 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2968 /* Create default constructors, assignment operators, and so forth for
2969 the type indicated by T, if they are needed.
2970 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2971 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2972 class cannot have a default constructor, copy constructor taking a
2973 const reference argument, or an assignment operator taking a const
2974 reference, respectively. If a virtual destructor is created, its
2975 DECL is returned; otherwise the return value is NULL_TREE. */
2978 add_implicitly_declared_members (t, cant_have_default_ctor,
2979 cant_have_const_cctor,
2980 cant_have_const_assignment)
2982 int cant_have_default_ctor;
2983 int cant_have_const_cctor;
2984 int cant_have_const_assignment;
2987 tree implicit_fns = NULL_TREE;
2988 tree virtual_dtor = NULL_TREE;
2992 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2994 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2995 check_for_override (default_fn, t);
2997 /* If we couldn't make it work, then pretend we didn't need it. */
2998 if (default_fn == void_type_node)
2999 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3002 TREE_CHAIN (default_fn) = implicit_fns;
3003 implicit_fns = default_fn;
3005 if (DECL_VINDEX (default_fn))
3006 virtual_dtor = default_fn;
3010 /* Any non-implicit destructor is non-trivial. */
3011 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3013 /* Default constructor. */
3014 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3016 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3017 TREE_CHAIN (default_fn) = implicit_fns;
3018 implicit_fns = default_fn;
3021 /* Copy constructor. */
3022 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3024 /* ARM 12.18: You get either X(X&) or X(const X&), but
3027 = implicitly_declare_fn (sfk_copy_constructor, t,
3028 /*const_p=*/!cant_have_const_cctor);
3029 TREE_CHAIN (default_fn) = implicit_fns;
3030 implicit_fns = default_fn;
3033 /* Assignment operator. */
3034 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3037 = implicitly_declare_fn (sfk_assignment_operator, t,
3038 /*const_p=*/!cant_have_const_assignment);
3039 TREE_CHAIN (default_fn) = implicit_fns;
3040 implicit_fns = default_fn;
3043 /* Now, hook all of the new functions on to TYPE_METHODS,
3044 and add them to the CLASSTYPE_METHOD_VEC. */
3045 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3046 add_method (t, *f, /*error_p=*/0);
3047 *f = TYPE_METHODS (t);
3048 TYPE_METHODS (t) = implicit_fns;
3050 return virtual_dtor;
3053 /* Subroutine of finish_struct_1. Recursively count the number of fields
3054 in TYPE, including anonymous union members. */
3057 count_fields (fields)
3062 for (x = fields; x; x = TREE_CHAIN (x))
3064 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3065 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3072 /* Subroutine of finish_struct_1. Recursively add all the fields in the
3073 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
3076 add_fields_to_vec (fields, field_vec, idx)
3077 tree fields, field_vec;
3081 for (x = fields; x; x = TREE_CHAIN (x))
3083 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3084 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3086 TREE_VEC_ELT (field_vec, idx++) = x;
3091 /* FIELD is a bit-field. We are finishing the processing for its
3092 enclosing type. Issue any appropriate messages and set appropriate
3096 check_bitfield_decl (field)
3099 tree type = TREE_TYPE (field);
3102 /* Detect invalid bit-field type. */
3103 if (DECL_INITIAL (field)
3104 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3106 cp_error_at ("bit-field `%#D' with non-integral type", field);
3107 w = error_mark_node;
3110 /* Detect and ignore out of range field width. */
3111 if (DECL_INITIAL (field))
3113 w = DECL_INITIAL (field);
3115 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3118 /* detect invalid field size. */
3119 if (TREE_CODE (w) == CONST_DECL)
3120 w = DECL_INITIAL (w);
3122 w = decl_constant_value (w);
3124 if (TREE_CODE (w) != INTEGER_CST)
3126 cp_error_at ("bit-field `%D' width not an integer constant",
3128 w = error_mark_node;
3130 else if (tree_int_cst_sgn (w) < 0)
3132 cp_error_at ("negative width in bit-field `%D'", field);
3133 w = error_mark_node;
3135 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3137 cp_error_at ("zero width for bit-field `%D'", field);
3138 w = error_mark_node;
3140 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3141 && TREE_CODE (type) != ENUMERAL_TYPE
3142 && TREE_CODE (type) != BOOLEAN_TYPE)
3143 cp_warning_at ("width of `%D' exceeds its type", field);
3144 else if (TREE_CODE (type) == ENUMERAL_TYPE
3145 && (0 > compare_tree_int (w,
3146 min_precision (TYPE_MIN_VALUE (type),
3147 TREE_UNSIGNED (type)))
3148 || 0 > compare_tree_int (w,
3150 (TYPE_MAX_VALUE (type),
3151 TREE_UNSIGNED (type)))))
3152 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3156 /* Remove the bit-field width indicator so that the rest of the
3157 compiler does not treat that value as an initializer. */
3158 DECL_INITIAL (field) = NULL_TREE;
3160 if (w != error_mark_node)
3162 DECL_SIZE (field) = convert (bitsizetype, w);
3163 DECL_BIT_FIELD (field) = 1;
3165 if (integer_zerop (w))
3167 #ifdef EMPTY_FIELD_BOUNDARY
3168 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3169 EMPTY_FIELD_BOUNDARY);
3171 #ifdef PCC_BITFIELD_TYPE_MATTERS
3172 if (PCC_BITFIELD_TYPE_MATTERS)
3174 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3176 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3183 /* Non-bit-fields are aligned for their type. */
3184 DECL_BIT_FIELD (field) = 0;
3185 CLEAR_DECL_C_BIT_FIELD (field);
3186 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3187 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3191 /* FIELD is a non bit-field. We are finishing the processing for its
3192 enclosing type T. Issue any appropriate messages and set appropriate
3196 check_field_decl (field, t, cant_have_const_ctor,
3197 cant_have_default_ctor, no_const_asn_ref,
3198 any_default_members)
3201 int *cant_have_const_ctor;
3202 int *cant_have_default_ctor;
3203 int *no_const_asn_ref;
3204 int *any_default_members;
3206 tree type = strip_array_types (TREE_TYPE (field));
3208 /* An anonymous union cannot contain any fields which would change
3209 the settings of CANT_HAVE_CONST_CTOR and friends. */
3210 if (ANON_UNION_TYPE_P (type))
3212 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3213 structs. So, we recurse through their fields here. */
3214 else if (ANON_AGGR_TYPE_P (type))
3218 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3219 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3220 check_field_decl (fields, t, cant_have_const_ctor,
3221 cant_have_default_ctor, no_const_asn_ref,
3222 any_default_members);
3224 /* Check members with class type for constructors, destructors,
3226 else if (CLASS_TYPE_P (type))
3228 /* Never let anything with uninheritable virtuals
3229 make it through without complaint. */
3230 abstract_virtuals_error (field, type);
3232 if (TREE_CODE (t) == UNION_TYPE)
3234 if (TYPE_NEEDS_CONSTRUCTING (type))
3235 cp_error_at ("member `%#D' with constructor not allowed in union",
3237 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3238 cp_error_at ("member `%#D' with destructor not allowed in union",
3240 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3241 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3246 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3247 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3248 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3249 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3250 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3253 if (!TYPE_HAS_CONST_INIT_REF (type))
3254 *cant_have_const_ctor = 1;
3256 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3257 *no_const_asn_ref = 1;
3259 if (TYPE_HAS_CONSTRUCTOR (type)
3260 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3261 *cant_have_default_ctor = 1;
3263 if (DECL_INITIAL (field) != NULL_TREE)
3265 /* `build_class_init_list' does not recognize
3267 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3268 cp_error_at ("multiple fields in union `%T' initialized");
3269 *any_default_members = 1;
3272 /* Non-bit-fields are aligned for their type, except packed fields
3273 which require only BITS_PER_UNIT alignment. */
3274 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3275 (DECL_PACKED (field)
3277 : TYPE_ALIGN (TREE_TYPE (field))));
3278 if (! DECL_PACKED (field))
3279 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3282 /* Check the data members (both static and non-static), class-scoped
3283 typedefs, etc., appearing in the declaration of T. Issue
3284 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3285 declaration order) of access declarations; each TREE_VALUE in this
3286 list is a USING_DECL.
3288 In addition, set the following flags:
3291 The class is empty, i.e., contains no non-static data members.
3293 CANT_HAVE_DEFAULT_CTOR_P
3294 This class cannot have an implicitly generated default
3297 CANT_HAVE_CONST_CTOR_P
3298 This class cannot have an implicitly generated copy constructor
3299 taking a const reference.
3301 CANT_HAVE_CONST_ASN_REF
3302 This class cannot have an implicitly generated assignment
3303 operator taking a const reference.
3305 All of these flags should be initialized before calling this
3308 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3309 fields can be added by adding to this chain. */
3312 check_field_decls (t, access_decls, empty_p,
3313 cant_have_default_ctor_p, cant_have_const_ctor_p,
3318 int *cant_have_default_ctor_p;
3319 int *cant_have_const_ctor_p;
3320 int *no_const_asn_ref_p;
3325 int any_default_members;
3327 /* First, delete any duplicate fields. */
3328 delete_duplicate_fields (TYPE_FIELDS (t));
3330 /* Assume there are no access declarations. */
3331 *access_decls = NULL_TREE;
3332 /* Assume this class has no pointer members. */
3334 /* Assume none of the members of this class have default
3336 any_default_members = 0;
3338 for (field = &TYPE_FIELDS (t); *field; field = next)
3341 tree type = TREE_TYPE (x);
3343 GNU_xref_member (current_class_name, x);
3345 next = &TREE_CHAIN (x);
3347 if (TREE_CODE (x) == FIELD_DECL)
3349 DECL_PACKED (x) |= TYPE_PACKED (t);
3351 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3352 /* We don't treat zero-width bitfields as making a class
3357 /* The class is non-empty. */
3359 /* The class is not even nearly empty. */
3360 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3364 if (TREE_CODE (x) == USING_DECL)
3366 /* Prune the access declaration from the list of fields. */
3367 *field = TREE_CHAIN (x);
3369 /* Save the access declarations for our caller. */
3370 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3372 /* Since we've reset *FIELD there's no reason to skip to the
3378 if (TREE_CODE (x) == TYPE_DECL
3379 || TREE_CODE (x) == TEMPLATE_DECL)
3382 /* If we've gotten this far, it's a data member, possibly static,
3383 or an enumerator. */
3385 DECL_CONTEXT (x) = t;
3387 /* ``A local class cannot have static data members.'' ARM 9.4 */
3388 if (current_function_decl && TREE_STATIC (x))
3389 cp_error_at ("field `%D' in local class cannot be static", x);
3391 /* Perform error checking that did not get done in
3393 if (TREE_CODE (type) == FUNCTION_TYPE)
3395 cp_error_at ("field `%D' invalidly declared function type",
3397 type = build_pointer_type (type);
3398 TREE_TYPE (x) = type;
3400 else if (TREE_CODE (type) == METHOD_TYPE)
3402 cp_error_at ("field `%D' invalidly declared method type", x);
3403 type = build_pointer_type (type);
3404 TREE_TYPE (x) = type;
3406 else if (TREE_CODE (type) == OFFSET_TYPE)
3408 cp_error_at ("field `%D' invalidly declared offset type", x);
3409 type = build_pointer_type (type);
3410 TREE_TYPE (x) = type;
3413 if (type == error_mark_node)
3416 /* When this goes into scope, it will be a non-local reference. */
3417 DECL_NONLOCAL (x) = 1;
3419 if (TREE_CODE (x) == CONST_DECL)
3422 if (TREE_CODE (x) == VAR_DECL)
3424 if (TREE_CODE (t) == UNION_TYPE)
3425 /* Unions cannot have static members. */
3426 cp_error_at ("field `%D' declared static in union", x);
3431 /* Now it can only be a FIELD_DECL. */
3433 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3434 CLASSTYPE_NON_AGGREGATE (t) = 1;
3436 /* If this is of reference type, check if it needs an init.
3437 Also do a little ANSI jig if necessary. */
3438 if (TREE_CODE (type) == REFERENCE_TYPE)
3440 CLASSTYPE_NON_POD_P (t) = 1;
3441 if (DECL_INITIAL (x) == NULL_TREE)
3442 CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3444 /* ARM $12.6.2: [A member initializer list] (or, for an
3445 aggregate, initialization by a brace-enclosed list) is the
3446 only way to initialize nonstatic const and reference
3448 *cant_have_default_ctor_p = 1;
3449 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3451 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3452 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3455 type = strip_array_types (type);
3457 if (TREE_CODE (type) == POINTER_TYPE)
3460 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3461 CLASSTYPE_HAS_MUTABLE (t) = 1;
3463 if (! pod_type_p (type))
3464 /* DR 148 now allows pointers to members (which are POD themselves),
3465 to be allowed in POD structs. */
3466 CLASSTYPE_NON_POD_P (t) = 1;
3468 /* If any field is const, the structure type is pseudo-const. */
3469 if (CP_TYPE_CONST_P (type))
3471 C_TYPE_FIELDS_READONLY (t) = 1;
3472 if (DECL_INITIAL (x) == NULL_TREE)
3473 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3475 /* ARM $12.6.2: [A member initializer list] (or, for an
3476 aggregate, initialization by a brace-enclosed list) is the
3477 only way to initialize nonstatic const and reference
3479 *cant_have_default_ctor_p = 1;
3480 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3482 if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3483 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3485 /* A field that is pseudo-const makes the structure likewise. */
3486 else if (IS_AGGR_TYPE (type))
3488 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3489 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3490 |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3493 /* Core issue 80: A nonstatic data member is required to have a
3494 different name from the class iff the class has a
3495 user-defined constructor. */
3496 if (DECL_NAME (x) == constructor_name (t)
3497 && TYPE_HAS_CONSTRUCTOR (t))
3498 cp_pedwarn_at ("field `%#D' with same name as class", x);
3500 /* We set DECL_C_BIT_FIELD in grokbitfield.
3501 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3502 if (DECL_C_BIT_FIELD (x))
3503 check_bitfield_decl (x);
3505 check_field_decl (x, t,
3506 cant_have_const_ctor_p,
3507 cant_have_default_ctor_p,
3509 &any_default_members);
3512 /* Effective C++ rule 11. */
3513 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3514 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3516 cp_warning ("`%#T' has pointer data members", t);
3518 if (! TYPE_HAS_INIT_REF (t))
3520 cp_warning (" but does not override `%T(const %T&)'", t, t);
3521 if (! TYPE_HAS_ASSIGN_REF (t))
3522 cp_warning (" or `operator=(const %T&)'", t);
3524 else if (! TYPE_HAS_ASSIGN_REF (t))
3525 cp_warning (" but does not override `operator=(const %T&)'", t);
3529 /* Check anonymous struct/anonymous union fields. */
3530 finish_struct_anon (t);
3532 /* We've built up the list of access declarations in reverse order.
3534 *access_decls = nreverse (*access_decls);
3537 /* If TYPE is an empty class type, records its OFFSET in the table of
3541 record_subobject_offset (type, offset, offsets)
3548 if (!is_empty_class (type))
3551 /* Record the location of this empty object in OFFSETS. */
3552 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3554 n = splay_tree_insert (offsets,
3555 (splay_tree_key) offset,
3556 (splay_tree_value) NULL_TREE);
3557 n->value = ((splay_tree_value)
3558 tree_cons (NULL_TREE,
3565 /* Returns non-zero if TYPE is an empty class type and there is
3566 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3569 check_subobject_offset (type, offset, offsets)
3577 if (!is_empty_class (type))
3580 /* Record the location of this empty object in OFFSETS. */
3581 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3585 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3586 if (same_type_p (TREE_VALUE (t), type))
3592 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3593 F for every subobject, passing it the type, offset, and table of
3594 OFFSETS. If VBASES_P is non-zero, then even virtual non-primary
3595 bases should be traversed; otherwise, they are ignored.
3597 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3598 than MAX_OFFSET will not be walked.
3600 If F returns a non-zero value, the traversal ceases, and that value
3601 is returned. Otherwise, returns zero. */
3604 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3606 subobject_offset_fn f;
3614 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3616 if (max_offset && INT_CST_LT (max_offset, offset))
3619 if (CLASS_TYPE_P (type))
3624 /* Record the location of TYPE. */
3625 r = (*f) (type, offset, offsets);
3629 /* Iterate through the direct base classes of TYPE. */
3630 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3632 tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3635 && TREE_VIA_VIRTUAL (binfo)
3636 && !BINFO_PRIMARY_P (binfo))
3639 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3641 size_binop (PLUS_EXPR,
3643 BINFO_OFFSET (binfo)),
3651 /* Iterate through the fields of TYPE. */
3652 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3653 if (TREE_CODE (field) == FIELD_DECL)
3655 r = walk_subobject_offsets (TREE_TYPE (field),
3657 size_binop (PLUS_EXPR,
3659 DECL_FIELD_OFFSET (field)),
3667 else if (TREE_CODE (type) == ARRAY_TYPE)
3669 tree domain = TYPE_DOMAIN (type);
3672 /* Step through each of the elements in the array. */
3673 for (index = size_zero_node;
3674 INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3675 index = size_binop (PLUS_EXPR, index, size_one_node))
3677 r = walk_subobject_offsets (TREE_TYPE (type),
3685 offset = size_binop (PLUS_EXPR, offset,
3686 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3687 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3688 there's no point in iterating through the remaining
3689 elements of the array. */
3690 if (max_offset && INT_CST_LT (max_offset, offset))
3698 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3699 OFFSETS. If VBASES_P is non-zero, virtual bases of TYPE are
3703 record_subobject_offsets (type, offset, offsets, vbases_p)
3709 walk_subobject_offsets (type, record_subobject_offset, offset,
3710 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3713 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3714 OFFSET) conflict with entries in OFFSETS. If VBASES_P is non-zero,
3715 virtual bases of TYPE are examined. */
3718 layout_conflict_p (type, offset, offsets, vbases_p)
3724 splay_tree_node max_node;
3726 /* Get the node in OFFSETS that indicates the maximum offset where
3727 an empty subobject is located. */
3728 max_node = splay_tree_max (offsets);
3729 /* If there aren't any empty subobjects, then there's no point in
3730 performing this check. */
3734 return walk_subobject_offsets (type, check_subobject_offset, offset,
3735 offsets, (tree) (max_node->key),
3739 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3740 non-static data member of the type indicated by RLI. BINFO is the
3741 binfo corresponding to the base subobject, OFFSETS maps offsets to
3742 types already located at those offsets. T is the most derived
3743 type. This function determines the position of the DECL. */
3746 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3747 record_layout_info rli;
3753 tree offset = NULL_TREE;
3754 tree type = TREE_TYPE (decl);
3755 /* If we are laying out a base class, rather than a field, then
3756 DECL_ARTIFICIAL will be set on the FIELD_DECL. */
3757 int field_p = !DECL_ARTIFICIAL (decl);
3759 /* Try to place the field. It may take more than one try if we have
3760 a hard time placing the field without putting two objects of the
3761 same type at the same address. */
3764 struct record_layout_info_s old_rli = *rli;
3766 /* Place this field. */
3767 place_field (rli, decl);
3768 offset = byte_position (decl);
3770 /* We have to check to see whether or not there is already
3771 something of the same type at the offset we're about to use.
3775 struct T : public S { int i; };
3776 struct U : public S, public T {};
3778 Here, we put S at offset zero in U. Then, we can't put T at
3779 offset zero -- its S component would be at the same address
3780 as the S we already allocated. So, we have to skip ahead.
3781 Since all data members, including those whose type is an
3782 empty class, have non-zero size, any overlap can happen only
3783 with a direct or indirect base-class -- it can't happen with
3785 if (layout_conflict_p (TREE_TYPE (decl),
3790 /* Strip off the size allocated to this field. That puts us
3791 at the first place we could have put the field with
3792 proper alignment. */
3795 /* Bump up by the alignment required for the type. */
3797 = size_binop (PLUS_EXPR, rli->bitpos,
3799 ? CLASSTYPE_ALIGN (type)
3800 : TYPE_ALIGN (type)));
3801 normalize_rli (rli);
3804 /* There was no conflict. We're done laying out this field. */
3808 /* Now that we know where it will be placed, update its
3810 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3811 propagate_binfo_offsets (binfo,
3812 convert (ssizetype, offset), t);
3815 /* Layout the empty base BINFO. EOC indicates the byte currently just
3816 past the end of the class, and should be correctly aligned for a
3817 class of the type indicated by BINFO; OFFSETS gives the offsets of
3818 the empty bases allocated so far. T is the most derived
3819 type. Return non-zero iff we added it at the end. */
3822 layout_empty_base (binfo, eoc, offsets, t)
3829 tree basetype = BINFO_TYPE (binfo);
3832 /* This routine should only be used for empty classes. */
3833 my_friendly_assert (is_empty_class (basetype), 20000321);
3834 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3836 /* This is an empty base class. We first try to put it at offset
3838 if (layout_conflict_p (BINFO_TYPE (binfo),
3839 BINFO_OFFSET (binfo),
3843 /* That didn't work. Now, we move forward from the next
3844 available spot in the class. */
3846 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3849 if (!layout_conflict_p (BINFO_TYPE (binfo),
3850 BINFO_OFFSET (binfo),
3853 /* We finally found a spot where there's no overlap. */
3856 /* There's overlap here, too. Bump along to the next spot. */
3857 propagate_binfo_offsets (binfo, alignment, t);
3863 /* Build a FIELD_DECL for the base given by BINFO in the class
3864 indicated by RLI. If the new object is non-empty, clear *EMPTY_P.
3865 *BASE_ALIGN is a running maximum of the alignments of any base
3866 class. OFFSETS gives the location of empty base subobjects. T is
3867 the most derived type. Return non-zero if the new object cannot be
3871 build_base_field (rli, binfo, empty_p, offsets, t)
3872 record_layout_info rli;
3878 tree basetype = BINFO_TYPE (binfo);
3882 if (!COMPLETE_TYPE_P (basetype))
3883 /* This error is now reported in xref_tag, thus giving better
3884 location information. */
3887 decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3888 DECL_ARTIFICIAL (decl) = 1;
3889 DECL_FIELD_CONTEXT (decl) = rli->t;
3890 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3891 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3892 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3893 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3895 if (!integer_zerop (DECL_SIZE (decl)))
3897 /* The containing class is non-empty because it has a non-empty
3901 /* Try to place the field. It may take more than one try if we
3902 have a hard time placing the field without putting two
3903 objects of the same type at the same address. */
3904 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3908 unsigned HOST_WIDE_INT eoc;
3910 /* On some platforms (ARM), even empty classes will not be
3912 eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3913 eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3914 atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3917 /* Record the offsets of BINFO and its base subobjects. */
3918 record_subobject_offsets (BINFO_TYPE (binfo),
3919 BINFO_OFFSET (binfo),
3925 /* Layout all of the non-virtual base classes. Record empty
3926 subobjects in OFFSETS. T is the most derived type. Return
3927 non-zero if the type cannot be nearly empty. */
3930 build_base_fields (rli, empty_p, offsets, t)
3931 record_layout_info rli;
3936 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3939 int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3943 /* The primary base class is always allocated first. */
3944 if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3945 build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec),
3946 empty_p, offsets, t);
3948 /* Now allocate the rest of the bases. */
3949 for (i = 0; i < n_baseclasses; ++i)
3953 base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3955 /* The primary base was already allocated above, so we don't
3956 need to allocate it again here. */
3957 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3960 /* A primary virtual base class is allocated just like any other
3961 base class, but a non-primary virtual base is allocated
3962 later, in layout_virtual_bases. */
3963 if (TREE_VIA_VIRTUAL (base_binfo)
3964 && !BINFO_PRIMARY_P (base_binfo))
3967 atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3972 /* Go through the TYPE_METHODS of T issuing any appropriate
3973 diagnostics, figuring out which methods override which other
3974 methods, and so forth. */
3982 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3984 GNU_xref_member (current_class_name, x);
3986 /* If this was an evil function, don't keep it in class. */
3987 if (DECL_ASSEMBLER_NAME_SET_P (x)
3988 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3991 check_for_override (x, t);
3992 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3993 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3995 /* The name of the field is the original field name
3996 Save this in auxiliary field for later overloading. */
3997 if (DECL_VINDEX (x))
3999 TYPE_POLYMORPHIC_P (t) = 1;
4000 if (DECL_PURE_VIRTUAL_P (x))
4001 CLASSTYPE_PURE_VIRTUALS (t)
4002 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4007 /* FN is a constructor or destructor. Clone the declaration to create
4008 a specialized in-charge or not-in-charge version, as indicated by
4012 build_clone (fn, name)
4019 /* Copy the function. */
4020 clone = copy_decl (fn);
4021 /* Remember where this function came from. */
4022 DECL_CLONED_FUNCTION (clone) = fn;
4023 DECL_ABSTRACT_ORIGIN (clone) = fn;
4024 /* Reset the function name. */
4025 DECL_NAME (clone) = name;
4026 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4027 /* There's no pending inline data for this function. */
4028 DECL_PENDING_INLINE_INFO (clone) = NULL;
4029 DECL_PENDING_INLINE_P (clone) = 0;
4030 /* And it hasn't yet been deferred. */
4031 DECL_DEFERRED_FN (clone) = 0;
4033 /* The base-class destructor is not virtual. */
4034 if (name == base_dtor_identifier)
4036 DECL_VIRTUAL_P (clone) = 0;
4037 if (TREE_CODE (clone) != TEMPLATE_DECL)
4038 DECL_VINDEX (clone) = NULL_TREE;
4041 /* If there was an in-charge parameter, drop it from the function
4043 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4049 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4050 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4051 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4052 /* Skip the `this' parameter. */
4053 parmtypes = TREE_CHAIN (parmtypes);
4054 /* Skip the in-charge parameter. */
4055 parmtypes = TREE_CHAIN (parmtypes);
4056 /* And the VTT parm, in a complete [cd]tor. */
4057 if (DECL_HAS_VTT_PARM_P (fn)
4058 && ! DECL_NEEDS_VTT_PARM_P (clone))
4059 parmtypes = TREE_CHAIN (parmtypes);
4060 /* If this is subobject constructor or destructor, add the vtt
4063 = build_cplus_method_type (basetype,
4064 TREE_TYPE (TREE_TYPE (clone)),
4067 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4071 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4072 aren't function parameters; those are the template parameters. */
4073 if (TREE_CODE (clone) != TEMPLATE_DECL)
4075 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4076 /* Remove the in-charge parameter. */
4077 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4079 TREE_CHAIN (DECL_ARGUMENTS (clone))
4080 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4081 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4083 /* And the VTT parm, in a complete [cd]tor. */
4084 if (DECL_HAS_VTT_PARM_P (fn))
4086 if (DECL_NEEDS_VTT_PARM_P (clone))
4087 DECL_HAS_VTT_PARM_P (clone) = 1;
4090 TREE_CHAIN (DECL_ARGUMENTS (clone))
4091 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4092 DECL_HAS_VTT_PARM_P (clone) = 0;
4096 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4098 DECL_CONTEXT (parms) = clone;
4099 copy_lang_decl (parms);
4103 /* Create the RTL for this function. */
4104 SET_DECL_RTL (clone, NULL_RTX);
4105 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4107 /* Make it easy to find the CLONE given the FN. */
4108 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4109 TREE_CHAIN (fn) = clone;
4111 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4112 if (TREE_CODE (clone) == TEMPLATE_DECL)
4116 DECL_TEMPLATE_RESULT (clone)
4117 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4118 result = DECL_TEMPLATE_RESULT (clone);
4119 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4120 DECL_TI_TEMPLATE (result) = clone;
4122 else if (DECL_DEFERRED_FN (fn))
4128 /* Produce declarations for all appropriate clones of FN. If
4129 UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4130 CLASTYPE_METHOD_VEC as well. */
4133 clone_function_decl (fn, update_method_vec_p)
4135 int update_method_vec_p;
4139 /* Avoid inappropriate cloning. */
4141 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4144 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4146 /* For each constructor, we need two variants: an in-charge version
4147 and a not-in-charge version. */
4148 clone = build_clone (fn, complete_ctor_identifier);
4149 if (update_method_vec_p)
4150 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4151 clone = build_clone (fn, base_ctor_identifier);
4152 if (update_method_vec_p)
4153 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4157 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4159 /* For each destructor, we need three variants: an in-charge
4160 version, a not-in-charge version, and an in-charge deleting
4161 version. We clone the deleting version first because that
4162 means it will go second on the TYPE_METHODS list -- and that
4163 corresponds to the correct layout order in the virtual
4166 For a non-virtual destructor, we do not build a deleting
4168 if (DECL_VIRTUAL_P (fn))
4170 clone = build_clone (fn, deleting_dtor_identifier);
4171 if (update_method_vec_p)
4172 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4174 clone = build_clone (fn, complete_dtor_identifier);
4175 if (update_method_vec_p)
4176 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4177 clone = build_clone (fn, base_dtor_identifier);
4178 if (update_method_vec_p)
4179 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4182 /* Note that this is an abstract function that is never emitted. */
4183 DECL_ABSTRACT (fn) = 1;
4186 /* DECL is an in charge constructor, which is being defined. This will
4187 have had an in class declaration, from whence clones were
4188 declared. An out-of-class definition can specify additional default
4189 arguments. As it is the clones that are involved in overload
4190 resolution, we must propagate the information from the DECL to its
4194 adjust_clone_args (decl)
4199 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4200 clone = TREE_CHAIN (clone))
4202 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4203 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4204 tree decl_parms, clone_parms;
4206 clone_parms = orig_clone_parms;
4208 /* Skip the 'this' parameter. */
4209 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4210 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4212 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4213 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4214 if (DECL_HAS_VTT_PARM_P (decl))
4215 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4217 clone_parms = orig_clone_parms;
4218 if (DECL_HAS_VTT_PARM_P (clone))
4219 clone_parms = TREE_CHAIN (clone_parms);
4221 for (decl_parms = orig_decl_parms; decl_parms;
4222 decl_parms = TREE_CHAIN (decl_parms),
4223 clone_parms = TREE_CHAIN (clone_parms))
4225 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4226 TREE_TYPE (clone_parms)), 20010424);
4228 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4230 /* A default parameter has been added. Adjust the
4231 clone's parameters. */
4232 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4233 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4236 clone_parms = orig_decl_parms;
4238 if (DECL_HAS_VTT_PARM_P (clone))
4240 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4241 TREE_VALUE (orig_clone_parms),
4243 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4245 type = build_cplus_method_type (basetype,
4246 TREE_TYPE (TREE_TYPE (clone)),
4249 type = build_exception_variant (type, exceptions);
4250 TREE_TYPE (clone) = type;
4252 clone_parms = NULL_TREE;
4256 my_friendly_assert (!clone_parms, 20010424);
4260 /* For each of the constructors and destructors in T, create an
4261 in-charge and not-in-charge variant. */
4264 clone_constructors_and_destructors (t)
4269 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4271 if (!CLASSTYPE_METHOD_VEC (t))
4274 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4275 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4276 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4277 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4280 /* Remove all zero-width bit-fields from T. */
4283 remove_zero_width_bit_fields (t)
4288 fieldsp = &TYPE_FIELDS (t);
4291 if (TREE_CODE (*fieldsp) == FIELD_DECL
4292 && DECL_C_BIT_FIELD (*fieldsp)
4293 && DECL_INITIAL (*fieldsp))
4294 *fieldsp = TREE_CHAIN (*fieldsp);
4296 fieldsp = &TREE_CHAIN (*fieldsp);
4300 /* Returns TRUE iff we need a cookie when dynamically allocating an
4301 array whose elements have the indicated class TYPE. */
4304 type_requires_array_cookie (type)
4308 bool has_two_argument_delete_p = false;
4310 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4312 /* If there's a non-trivial destructor, we need a cookie. In order
4313 to iterate through the array calling the destructor for each
4314 element, we'll have to know how many elements there are. */
4315 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4318 /* If the usual deallocation function is a two-argument whose second
4319 argument is of type `size_t', then we have to pass the size of
4320 the array to the deallocation function, so we will need to store
4322 fns = lookup_fnfields (TYPE_BINFO (type),
4323 ansi_opname (VEC_DELETE_EXPR),
4325 /* If there are no `operator []' members, or the lookup is
4326 ambiguous, then we don't need a cookie. */
4327 if (!fns || fns == error_mark_node)
4329 /* Loop through all of the functions. */
4330 for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
4335 /* Select the current function. */
4336 fn = OVL_CURRENT (fns);
4337 /* See if this function is a one-argument delete function. If
4338 it is, then it will be the usual deallocation function. */
4339 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4340 if (second_parm == void_list_node)
4342 /* Otherwise, if we have a two-argument function and the second
4343 argument is `size_t', it will be the usual deallocation
4344 function -- unless there is one-argument function, too. */
4345 if (TREE_CHAIN (second_parm) == void_list_node
4346 && same_type_p (TREE_VALUE (second_parm), sizetype))
4347 has_two_argument_delete_p = true;
4350 return has_two_argument_delete_p;
4353 /* Check the validity of the bases and members declared in T. Add any
4354 implicitly-generated functions (like copy-constructors and
4355 assignment operators). Compute various flag bits (like
4356 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4357 level: i.e., independently of the ABI in use. */
4360 check_bases_and_members (t, empty_p)
4364 /* Nonzero if we are not allowed to generate a default constructor
4366 int cant_have_default_ctor;
4367 /* Nonzero if the implicitly generated copy constructor should take
4368 a non-const reference argument. */
4369 int cant_have_const_ctor;
4370 /* Nonzero if the the implicitly generated assignment operator
4371 should take a non-const reference argument. */
4372 int no_const_asn_ref;
4375 /* By default, we use const reference arguments and generate default
4377 cant_have_default_ctor = 0;
4378 cant_have_const_ctor = 0;
4379 no_const_asn_ref = 0;
4381 /* Assume that the class is nearly empty; we'll clear this flag if
4382 it turns out not to be nearly empty. */
4383 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4385 /* Check all the base-classes. */
4386 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4389 /* Check all the data member declarations. */
4390 check_field_decls (t, &access_decls, empty_p,
4391 &cant_have_default_ctor,
4392 &cant_have_const_ctor,
4395 /* Check all the method declarations. */
4398 /* A nearly-empty class has to be vptr-containing; a nearly empty
4399 class contains just a vptr. */
4400 if (!TYPE_CONTAINS_VPTR_P (t))
4401 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4403 /* Do some bookkeeping that will guide the generation of implicitly
4404 declared member functions. */
4405 TYPE_HAS_COMPLEX_INIT_REF (t)
4406 |= (TYPE_HAS_INIT_REF (t)
4407 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4408 || TYPE_POLYMORPHIC_P (t));
4409 TYPE_NEEDS_CONSTRUCTING (t)
4410 |= (TYPE_HAS_CONSTRUCTOR (t)
4411 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4412 || TYPE_POLYMORPHIC_P (t));
4413 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4414 || TYPE_POLYMORPHIC_P (t));
4415 CLASSTYPE_NON_POD_P (t)
4416 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4417 || TYPE_HAS_ASSIGN_REF (t));
4418 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4419 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4420 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4422 /* Synthesize any needed methods. Note that methods will be synthesized
4423 for anonymous unions; grok_x_components undoes that. */
4424 add_implicitly_declared_members (t, cant_have_default_ctor,
4425 cant_have_const_ctor,
4428 /* Create the in-charge and not-in-charge variants of constructors
4430 clone_constructors_and_destructors (t);
4432 /* Process the using-declarations. */
4433 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4434 handle_using_decl (TREE_VALUE (access_decls), t);
4436 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4437 finish_struct_methods (t);
4439 /* Figure out whether or not we will need a cookie when dynamically
4440 allocating an array of this type. */
4441 TYPE_LANG_SPECIFIC (t)->vec_new_uses_cookie
4442 = type_requires_array_cookie (t);
4445 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4446 accordingly. If a new vfield was created (because T doesn't have a
4447 primary base class), then the newly created field is returned. It
4448 is not added to the TYPE_FIELDS list; it is the caller's
4449 responsibility to do that. */
4452 create_vtable_ptr (t, empty_p, vfuns_p,
4453 new_virtuals_p, overridden_virtuals_p)
4457 tree *new_virtuals_p;
4458 tree *overridden_virtuals_p;
4462 /* Loop over the virtual functions, adding them to our various
4464 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4465 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4466 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4469 /* If we couldn't find an appropriate base class, create a new field
4470 here. Even if there weren't any new virtual functions, we might need a
4471 new virtual function table if we're supposed to include vptrs in
4472 all classes that need them. */
4473 if (!TYPE_VFIELD (t) && (*vfuns_p || TYPE_CONTAINS_VPTR_P (t)))
4475 /* We build this decl with vtbl_ptr_type_node, which is a
4476 `vtable_entry_type*'. It might seem more precise to use
4477 `vtable_entry_type (*)[N]' where N is the number of firtual
4478 functions. However, that would require the vtable pointer in
4479 base classes to have a different type than the vtable pointer
4480 in derived classes. We could make that happen, but that
4481 still wouldn't solve all the problems. In particular, the
4482 type-based alias analysis code would decide that assignments
4483 to the base class vtable pointer can't alias assignments to
4484 the derived class vtable pointer, since they have different
4485 types. Thus, in an derived class destructor, where the base
4486 class constructor was inlined, we could generate bad code for
4487 setting up the vtable pointer.
4489 Therefore, we use one type for all vtable pointers. We still
4490 use a type-correct type; it's just doesn't indicate the array
4491 bounds. That's better than using `void*' or some such; it's
4492 cleaner, and it let's the alias analysis code know that these
4493 stores cannot alias stores to void*! */
4496 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4497 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4498 DECL_VIRTUAL_P (field) = 1;
4499 DECL_ARTIFICIAL (field) = 1;
4500 DECL_FIELD_CONTEXT (field) = t;
4501 DECL_FCONTEXT (field) = t;
4502 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4503 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4505 TYPE_VFIELD (t) = field;
4507 /* This class is non-empty. */
4510 if (CLASSTYPE_N_BASECLASSES (t))
4511 /* If there were any baseclasses, they can't possibly be at
4512 offset zero any more, because that's where the vtable
4513 pointer is. So, converting to a base class is going to
4515 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4523 /* Fixup the inline function given by INFO now that the class is
4527 fixup_pending_inline (fn)
4530 if (DECL_PENDING_INLINE_INFO (fn))
4532 tree args = DECL_ARGUMENTS (fn);
4535 DECL_CONTEXT (args) = fn;
4536 args = TREE_CHAIN (args);
4541 /* Fixup the inline methods and friends in TYPE now that TYPE is
4545 fixup_inline_methods (type)
4548 tree method = TYPE_METHODS (type);
4550 if (method && TREE_CODE (method) == TREE_VEC)
4552 if (TREE_VEC_ELT (method, 1))
4553 method = TREE_VEC_ELT (method, 1);
4554 else if (TREE_VEC_ELT (method, 0))
4555 method = TREE_VEC_ELT (method, 0);
4557 method = TREE_VEC_ELT (method, 2);
4560 /* Do inline member functions. */
4561 for (; method; method = TREE_CHAIN (method))
4562 fixup_pending_inline (method);
4565 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4567 method = TREE_CHAIN (method))
4568 fixup_pending_inline (TREE_VALUE (method));
4569 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4572 /* Add OFFSET to all base types of BINFO which is a base in the
4573 hierarchy dominated by T.
4575 OFFSET, which is a type offset, is number of bytes. */
4578 propagate_binfo_offsets (binfo, offset, t)
4586 /* Update BINFO's offset. */
4587 BINFO_OFFSET (binfo)
4588 = convert (sizetype,
4589 size_binop (PLUS_EXPR,
4590 convert (ssizetype, BINFO_OFFSET (binfo)),
4593 /* Find the primary base class. */
4594 primary_binfo = get_primary_binfo (binfo);
4596 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4598 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4602 /* On the first time through the loop, do the primary base.
4603 Because the primary base need not be an immediate base, we
4604 must handle the primary base specially. */
4610 base_binfo = primary_binfo;
4614 base_binfo = BINFO_BASETYPE (binfo, i);
4615 /* Don't do the primary base twice. */
4616 if (base_binfo == primary_binfo)
4620 /* Skip virtual bases that aren't our canonical primary base. */
4621 if (TREE_VIA_VIRTUAL (base_binfo)
4622 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4623 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4626 propagate_binfo_offsets (base_binfo, offset, t);
4630 /* Called via dfs_walk from layout_virtual bases. */
4633 dfs_set_offset_for_unshared_vbases (binfo, data)
4637 /* If this is a virtual base, make sure it has the same offset as
4638 the shared copy. If it's a primary base, then we know it's
4640 if (TREE_VIA_VIRTUAL (binfo))
4642 tree t = (tree) data;
4646 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4649 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4650 propagate_binfo_offsets (binfo, offset, t);
4657 /* Set BINFO_OFFSET for all of the virtual bases for T. Update
4658 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4659 empty subobjects of T. */
4662 layout_virtual_bases (t, offsets)
4667 unsigned HOST_WIDE_INT dsize;
4668 unsigned HOST_WIDE_INT eoc;
4670 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4673 #ifdef STRUCTURE_SIZE_BOUNDARY
4674 /* Packed structures don't need to have minimum size. */
4675 if (! TYPE_PACKED (t))
4676 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4679 /* DSIZE is the size of the class without the virtual bases. */
4680 dsize = tree_low_cst (TYPE_SIZE (t), 1);
4682 /* Make every class have alignment of at least one. */
4683 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4685 /* Go through the virtual bases, allocating space for each virtual
4686 base that is not already a primary base class. These are
4687 allocated in inheritance graph order. */
4688 for (vbases = TYPE_BINFO (t);
4690 vbases = TREE_CHAIN (vbases))
4694 if (!TREE_VIA_VIRTUAL (vbases))
4696 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4698 if (!BINFO_PRIMARY_P (vbase))
4700 /* This virtual base is not a primary base of any class in the
4701 hierarchy, so we have to add space for it. */
4703 unsigned int desired_align;
4705 basetype = BINFO_TYPE (vbase);
4707 desired_align = CLASSTYPE_ALIGN (basetype);
4708 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4710 /* Add padding so that we can put the virtual base class at an
4711 appropriately aligned offset. */
4712 dsize = CEIL (dsize, desired_align) * desired_align;
4714 /* We try to squish empty virtual bases in just like
4715 ordinary empty bases. */
4716 if (is_empty_class (basetype))
4717 layout_empty_base (vbase,
4718 size_int (CEIL (dsize, BITS_PER_UNIT)),
4724 offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4725 offset = size_diffop (offset,
4727 BINFO_OFFSET (vbase)));
4729 /* And compute the offset of the virtual base. */
4730 propagate_binfo_offsets (vbase, offset, t);
4731 /* Every virtual baseclass takes a least a UNIT, so that
4732 we can take it's address and get something different
4734 dsize += MAX (BITS_PER_UNIT,
4735 tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4738 /* Keep track of the offsets assigned to this virtual base. */
4739 record_subobject_offsets (BINFO_TYPE (vbase),
4740 BINFO_OFFSET (vbase),
4746 /* Now, go through the TYPE_BINFO hierarchy, setting the
4747 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4748 bases and their direct and indirect bases. The ambiguity checks
4749 in get_base_distance depend on the BINFO_OFFSETs being set
4751 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4753 /* If we had empty base classes that protruded beyond the end of the
4754 class, we didn't update DSIZE above; we were hoping to overlay
4755 multiple such bases at the same location. */
4756 eoc = end_of_class (t, /*include_virtuals_p=*/1);
4757 if (eoc * BITS_PER_UNIT > dsize)
4758 dsize = eoc * BITS_PER_UNIT;
4760 /* Now, make sure that the total size of the type is a multiple of
4762 dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4763 TYPE_SIZE (t) = bitsize_int (dsize);
4764 TYPE_SIZE_UNIT (t) = convert (sizetype,
4765 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4766 bitsize_unit_node));
4768 /* Check for ambiguous virtual bases. */
4770 for (vbases = CLASSTYPE_VBASECLASSES (t);
4772 vbases = TREE_CHAIN (vbases))
4774 tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4775 if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4776 cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4781 /* Returns the offset of the byte just past the end of the base class
4782 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4783 only non-virtual bases are included. */
4785 static unsigned HOST_WIDE_INT
4786 end_of_class (t, include_virtuals_p)
4788 int include_virtuals_p;
4790 unsigned HOST_WIDE_INT result = 0;
4793 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4798 unsigned HOST_WIDE_INT end_of_base;
4800 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4802 if (!include_virtuals_p
4803 && TREE_VIA_VIRTUAL (base_binfo)
4804 && !BINFO_PRIMARY_P (base_binfo))
4807 if (is_empty_class (BINFO_TYPE (base_binfo)))
4808 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4809 allocate some space for it. It cannot have virtual bases,
4810 so TYPE_SIZE_UNIT is fine. */
4811 size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4813 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4814 offset = size_binop (PLUS_EXPR,
4815 BINFO_OFFSET (base_binfo),
4817 end_of_base = tree_low_cst (offset, /*pos=*/1);
4818 if (end_of_base > result)
4819 result = end_of_base;
4825 /* Warn about direct bases of T that are inaccessible because they are
4826 ambiguous. For example:
4829 struct T : public S {};
4830 struct U : public S, public T {};
4832 Here, `(S*) new U' is not allowed because there are two `S'
4836 warn_about_ambiguous_direct_bases (t)
4841 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4843 tree basetype = TYPE_BINFO_BASETYPE (t, i);
4845 if (get_base_distance (basetype, t, 0, NULL) == -2)
4846 cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4851 /* Compare two INTEGER_CSTs K1 and K2. */
4854 splay_tree_compare_integer_csts (k1, k2)
4858 return tree_int_cst_compare ((tree) k1, (tree) k2);
4861 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4862 BINFO_OFFSETs for all of the base-classes. Position the vtable
4866 layout_class_type (t, empty_p, vfuns_p,
4867 new_virtuals_p, overridden_virtuals_p)
4871 tree *new_virtuals_p;
4872 tree *overridden_virtuals_p;
4874 tree non_static_data_members;
4877 record_layout_info rli;
4878 unsigned HOST_WIDE_INT eoc;
4879 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4880 types that appear at that offset. */
4881 splay_tree empty_base_offsets;
4883 /* Keep track of the first non-static data member. */
4884 non_static_data_members = TYPE_FIELDS (t);
4886 /* Start laying out the record. */
4887 rli = start_record_layout (t);
4889 /* If possible, we reuse the virtual function table pointer from one
4890 of our base classes. */
4891 determine_primary_base (t, vfuns_p);
4893 /* Create a pointer to our virtual function table. */
4894 vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4895 new_virtuals_p, overridden_virtuals_p);
4897 /* The vptr is always the first thing in the class. */
4900 TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4901 place_field (rli, vptr);
4904 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4905 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4907 if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4908 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4910 /* Layout the non-static data members. */
4911 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4916 /* We still pass things that aren't non-static data members to
4917 the back-end, in case it wants to do something with them. */
4918 if (TREE_CODE (field) != FIELD_DECL)
4920 place_field (rli, field);
4924 type = TREE_TYPE (field);
4926 /* If this field is a bit-field whose width is greater than its
4927 type, then there are some special rules for allocating
4929 if (DECL_C_BIT_FIELD (field)
4930 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4932 integer_type_kind itk;
4935 /* We must allocate the bits as if suitably aligned for the
4936 longest integer type that fits in this many bits. type
4937 of the field. Then, we are supposed to use the left over
4938 bits as additional padding. */
4939 for (itk = itk_char; itk != itk_none; ++itk)
4940 if (INT_CST_LT (DECL_SIZE (field),
4941 TYPE_SIZE (integer_types[itk])))
4944 /* ITK now indicates a type that is too large for the
4945 field. We have to back up by one to find the largest
4947 integer_type = integer_types[itk - 1];
4948 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4949 TYPE_SIZE (integer_type));
4950 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4951 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4952 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4955 padding = NULL_TREE;
4957 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4958 empty_base_offsets, t);
4960 /* If we needed additional padding after this field, add it
4966 padding_field = build_decl (FIELD_DECL,
4969 DECL_BIT_FIELD (padding_field) = 1;
4970 DECL_SIZE (padding_field) = padding;
4971 DECL_ALIGN (padding_field) = 1;
4972 DECL_USER_ALIGN (padding_field) = 0;
4973 layout_nonempty_base_or_field (rli, padding_field,
4975 empty_base_offsets, t);
4979 /* It might be the case that we grew the class to allocate a
4980 zero-sized base class. That won't be reflected in RLI, yet,
4981 because we are willing to overlay multiple bases at the same
4982 offset. However, now we need to make sure that RLI is big enough
4983 to reflect the entire class. */
4984 eoc = end_of_class (t, /*include_virtuals_p=*/0);
4985 if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4986 && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4988 rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4989 rli->bitpos = bitsize_zero_node;
4992 /* We make all structures have at least one element, so that they
4993 have non-zero size. The class may be empty even if it has
4994 basetypes. Therefore, we add the fake field after all the other
4995 fields; if there are already FIELD_DECLs on the list, their
4996 offsets will not be disturbed. */
4997 if (!eoc && *empty_p)
5001 padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
5002 place_field (rli, padding);
5005 /* Let the back-end lay out the type. Note that at this point we
5006 have only included non-virtual base-classes; we will lay out the
5007 virtual base classes later. So, the TYPE_SIZE/TYPE_ALIGN after
5008 this call are not necessarily correct; they are just the size and
5009 alignment when no virtual base clases are used. */
5010 finish_record_layout (rli);
5012 /* Delete all zero-width bit-fields from the list of fields. Now
5013 that the type is laid out they are no longer important. */
5014 remove_zero_width_bit_fields (t);
5016 /* Remember the size and alignment of the class before adding
5017 the virtual bases. */
5020 CLASSTYPE_SIZE (t) = bitsize_zero_node;
5021 CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5025 CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5026 CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5029 CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5030 CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5032 /* Set the TYPE_DECL for this type to contain the right
5033 value for DECL_OFFSET, so that we can use it as part
5034 of a COMPONENT_REF for multiple inheritance. */
5035 layout_decl (TYPE_MAIN_DECL (t), 0);
5037 /* Now fix up any virtual base class types that we left lying
5038 around. We must get these done before we try to lay out the
5039 virtual function table. As a side-effect, this will remove the
5040 base subobject fields. */
5041 layout_virtual_bases (t, empty_base_offsets);
5043 /* Warn about direct bases that can't be talked about due to
5045 warn_about_ambiguous_direct_bases (t);
5048 splay_tree_delete (empty_base_offsets);
5051 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5052 (or C++ class declaration).
5054 For C++, we must handle the building of derived classes.
5055 Also, C++ allows static class members. The way that this is
5056 handled is to keep the field name where it is (as the DECL_NAME
5057 of the field), and place the overloaded decl in the bit position
5058 of the field. layout_record and layout_union will know about this.
5060 More C++ hair: inline functions have text in their
5061 DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5062 meaningful tree structure. After the struct has been laid out, set
5063 things up so that this can happen.
5065 And still more: virtual functions. In the case of single inheritance,
5066 when a new virtual function is seen which redefines a virtual function
5067 from the base class, the new virtual function is placed into
5068 the virtual function table at exactly the same address that
5069 it had in the base class. When this is extended to multiple
5070 inheritance, the same thing happens, except that multiple virtual
5071 function tables must be maintained. The first virtual function
5072 table is treated in exactly the same way as in the case of single
5073 inheritance. Additional virtual function tables have different
5074 DELTAs, which tell how to adjust `this' to point to the right thing.
5076 ATTRIBUTES is the set of decl attributes to be applied, if any. */
5084 /* The NEW_VIRTUALS is a TREE_LIST. The TREE_VALUE of each node is
5085 a FUNCTION_DECL. Each of these functions is a virtual function
5086 declared in T that does not override any virtual function from a
5088 tree new_virtuals = NULL_TREE;
5089 /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5090 except that each declaration here overrides the declaration from
5092 tree overridden_virtuals = NULL_TREE;
5097 if (COMPLETE_TYPE_P (t))
5099 if (IS_AGGR_TYPE (t))
5100 cp_error ("redefinition of `%#T'", t);
5102 my_friendly_abort (172);
5107 GNU_xref_decl (current_function_decl, t);
5109 /* If this type was previously laid out as a forward reference,
5110 make sure we lay it out again. */
5111 TYPE_SIZE (t) = NULL_TREE;
5112 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5113 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5115 CLASSTYPE_RTTI (t) = NULL_TREE;
5117 fixup_inline_methods (t);
5119 /* Do end-of-class semantic processing: checking the validity of the
5120 bases and members and add implicitly generated methods. */
5121 check_bases_and_members (t, &empty);
5123 /* Layout the class itself. */
5124 layout_class_type (t, &empty, &vfuns,
5125 &new_virtuals, &overridden_virtuals);
5127 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5128 vfield = TYPE_VFIELD (t);
5129 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5131 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5133 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5134 BINFO_TYPE (primary)),
5136 /* The vtable better be at the start. */
5137 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5139 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5142 vfield = copy_decl (vfield);
5143 DECL_FIELD_CONTEXT (vfield) = t;
5144 TYPE_VFIELD (t) = vfield;
5147 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5150 = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5152 /* If we created a new vtbl pointer for this class, add it to the
5154 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5155 CLASSTYPE_VFIELDS (t)
5156 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5158 /* If necessary, create the primary vtable for this class. */
5159 if (new_virtuals || overridden_virtuals || TYPE_CONTAINS_VPTR_P (t))
5161 new_virtuals = nreverse (new_virtuals);
5162 /* We must enter these virtuals into the table. */
5163 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5164 build_primary_vtable (NULL_TREE, t);
5165 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5166 /* Here we know enough to change the type of our virtual
5167 function table, but we will wait until later this function. */
5168 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5170 /* If this type has basetypes with constructors, then those
5171 constructors might clobber the virtual function table. But
5172 they don't if the derived class shares the exact vtable of the base
5175 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5177 /* If we didn't need a new vtable, see if we should copy one from
5179 else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5181 tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5183 /* If this class uses a different vtable than its primary base
5184 then when we will need to initialize our vptr after the base
5185 class constructor runs. */
5186 if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5187 CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5190 if (TYPE_CONTAINS_VPTR_P (t))
5192 if (TYPE_BINFO_VTABLE (t))
5193 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5195 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5196 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5199 CLASSTYPE_VSIZE (t) = vfuns;
5200 /* Entries for virtual functions defined in the primary base are
5201 followed by entries for new functions unique to this class. */
5202 TYPE_BINFO_VIRTUALS (t)
5203 = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5204 /* Finally, add entries for functions that override virtuals
5205 from non-primary bases. */
5206 TYPE_BINFO_VIRTUALS (t)
5207 = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5210 finish_struct_bits (t);
5212 /* Complete the rtl for any static member objects of the type we're
5214 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5215 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5216 && TREE_TYPE (x) == t)
5217 DECL_MODE (x) = TYPE_MODE (t);
5219 /* Done with FIELDS...now decide whether to sort these for
5220 faster lookups later.
5222 The C front-end only does this when n_fields > 15. We use
5223 a smaller number because most searches fail (succeeding
5224 ultimately as the search bores through the inheritance
5225 hierarchy), and we want this failure to occur quickly. */
5227 n_fields = count_fields (TYPE_FIELDS (t));
5230 tree field_vec = make_tree_vec (n_fields);
5231 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5232 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5233 (int (*)(const void *, const void *))field_decl_cmp);
5234 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5235 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5236 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5239 if (TYPE_HAS_CONSTRUCTOR (t))
5241 tree vfields = CLASSTYPE_VFIELDS (t);
5245 /* Mark the fact that constructor for T
5246 could affect anybody inheriting from T
5247 who wants to initialize vtables for VFIELDS's type. */
5248 if (VF_DERIVED_VALUE (vfields))
5249 TREE_ADDRESSABLE (vfields) = 1;
5250 vfields = TREE_CHAIN (vfields);
5254 /* Make the rtl for any new vtables we have created, and unmark
5255 the base types we marked. */
5258 /* Build the VTT for T. */
5261 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5262 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5263 cp_warning ("`%#T' has virtual functions but non-virtual destructor", t);
5265 hack_incomplete_structures (t);
5267 if (warn_overloaded_virtual)
5270 maybe_suppress_debug_info (t);
5272 dump_class_hierarchy (t);
5274 /* Finish debugging output for this type. */
5275 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5278 /* When T was built up, the member declarations were added in reverse
5279 order. Rearrange them to declaration order. */
5282 unreverse_member_declarations (t)
5289 /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5290 reverse order. Put them in declaration order now. */
5291 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5292 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5294 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5295 reverse order, so we can't just use nreverse. */
5297 for (x = TYPE_FIELDS (t);
5298 x && TREE_CODE (x) != TYPE_DECL;
5301 next = TREE_CHAIN (x);
5302 TREE_CHAIN (x) = prev;
5307 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5309 TYPE_FIELDS (t) = prev;
5314 finish_struct (t, attributes)
5317 const char *saved_filename = input_filename;
5318 int saved_lineno = lineno;
5320 /* Now that we've got all the field declarations, reverse everything
5322 unreverse_member_declarations (t);
5324 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5326 /* Nadger the current location so that diagnostics point to the start of
5327 the struct, not the end. */
5328 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5329 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5331 if (processing_template_decl)
5333 finish_struct_methods (t);
5334 TYPE_SIZE (t) = bitsize_zero_node;
5337 finish_struct_1 (t);
5339 input_filename = saved_filename;
5340 lineno = saved_lineno;
5342 TYPE_BEING_DEFINED (t) = 0;
5344 if (current_class_type)
5347 error ("trying to finish struct, but kicked out due to previous parse errors.");
5349 if (processing_template_decl)
5351 tree scope = current_scope ();
5352 if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5353 add_stmt (build_min (TAG_DEFN, t));
5359 /* Return the dynamic type of INSTANCE, if known.
5360 Used to determine whether the virtual function table is needed
5363 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5364 of our knowledge of its type. *NONNULL should be initialized
5365 before this function is called. */
5368 fixed_type_or_null (instance, nonnull, cdtorp)
5373 switch (TREE_CODE (instance))
5376 /* Check that we are not going through a cast of some sort. */
5377 if (TREE_TYPE (instance)
5378 == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5379 instance = TREE_OPERAND (instance, 0);
5380 /* fall through... */
5382 /* This is a call to a constructor, hence it's never zero. */
5383 if (TREE_HAS_CONSTRUCTOR (instance))
5387 return TREE_TYPE (instance);
5392 /* This is a call to a constructor, hence it's never zero. */
5393 if (TREE_HAS_CONSTRUCTOR (instance))
5397 return TREE_TYPE (instance);
5399 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5406 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5407 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5408 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5409 /* Propagate nonnull. */
5410 fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5415 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5420 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5423 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5427 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5428 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5432 return TREE_TYPE (TREE_TYPE (instance));
5434 /* fall through... */
5437 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5441 return TREE_TYPE (instance);
5443 else if (instance == current_class_ptr)
5448 /* if we're in a ctor or dtor, we know our type. */
5449 if (DECL_LANG_SPECIFIC (current_function_decl)
5450 && (DECL_CONSTRUCTOR_P (current_function_decl)
5451 || DECL_DESTRUCTOR_P (current_function_decl)))
5455 return TREE_TYPE (TREE_TYPE (instance));
5458 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5460 /* Reference variables should be references to objects. */
5471 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5472 to the static type. We also handle the case where INSTANCE is really
5473 a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5474 is known, but this might not be the most derived base of the original object,
5475 and hence virtual bases may not be layed out according to this type.
5477 Used to determine whether the virtual function table is needed
5480 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5481 of our knowledge of its type. *NONNULL should be initialized
5482 before this function is called. */
5485 resolves_to_fixed_type_p (instance, nonnull)
5489 tree t = TREE_TYPE (instance);
5492 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5493 if (fixed == NULL_TREE)
5495 if (POINTER_TYPE_P (t))
5497 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5499 return cdtorp ? -1 : 1;
5504 init_class_processing ()
5506 current_class_depth = 0;
5507 current_class_stack_size = 10;
5509 = (class_stack_node_t) xmalloc (current_class_stack_size
5510 * sizeof (struct class_stack_node));
5511 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5512 ggc_add_tree_varray_root (&local_classes, 1);
5514 access_default_node = build_int_2 (0, 0);
5515 access_public_node = build_int_2 (ak_public, 0);
5516 access_protected_node = build_int_2 (ak_protected, 0);
5517 access_private_node = build_int_2 (ak_private, 0);
5518 access_default_virtual_node = build_int_2 (4, 0);
5519 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5520 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5521 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5523 ridpointers[(int) RID_PUBLIC] = access_public_node;
5524 ridpointers[(int) RID_PRIVATE] = access_private_node;
5525 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5528 /* Set current scope to NAME. CODE tells us if this is a
5529 STRUCT, UNION, or ENUM environment.
5531 NAME may end up being NULL_TREE if this is an anonymous or
5532 late-bound struct (as in "struct { ... } foo;") */
5534 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5535 appropriate values, found by looking up the type definition of
5538 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5539 which can be seen locally to the class. They are shadowed by
5540 any subsequent local declaration (including parameter names).
5542 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5543 which have static meaning (i.e., static members, static
5544 member functions, enum declarations, etc).
5546 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5547 which can be seen locally to the class (as in 1), but
5548 know that we are doing this for declaration purposes
5549 (i.e. friend foo::bar (int)).
5551 So that we may avoid calls to lookup_name, we cache the _TYPE
5552 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5554 For multiple inheritance, we perform a two-pass depth-first search
5555 of the type lattice. The first pass performs a pre-order search,
5556 marking types after the type has had its fields installed in
5557 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5558 unmarks the marked types. If a field or member function name
5559 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5560 that name becomes `error_mark_node'. */
5563 pushclass (type, modify)
5567 type = TYPE_MAIN_VARIANT (type);
5569 /* Make sure there is enough room for the new entry on the stack. */
5570 if (current_class_depth + 1 >= current_class_stack_size)
5572 current_class_stack_size *= 2;
5574 = (class_stack_node_t) xrealloc (current_class_stack,
5575 current_class_stack_size
5576 * sizeof (struct class_stack_node));
5579 /* Insert a new entry on the class stack. */
5580 current_class_stack[current_class_depth].name = current_class_name;
5581 current_class_stack[current_class_depth].type = current_class_type;
5582 current_class_stack[current_class_depth].access = current_access_specifier;
5583 current_class_stack[current_class_depth].names_used = 0;
5584 current_class_depth++;
5586 /* Now set up the new type. */
5587 current_class_name = TYPE_NAME (type);
5588 if (TREE_CODE (current_class_name) == TYPE_DECL)
5589 current_class_name = DECL_NAME (current_class_name);
5590 current_class_type = type;
5592 /* By default, things in classes are private, while things in
5593 structures or unions are public. */
5594 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5595 ? access_private_node
5596 : access_public_node);
5598 if (previous_class_type != NULL_TREE
5599 && (type != previous_class_type
5600 || !COMPLETE_TYPE_P (previous_class_type))
5601 && current_class_depth == 1)
5603 /* Forcibly remove any old class remnants. */
5604 invalidate_class_lookup_cache ();
5607 /* If we're about to enter a nested class, clear
5608 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5609 if (modify && current_class_depth > 1)
5610 clear_identifier_class_values ();
5616 if (type != previous_class_type || current_class_depth > 1)
5617 push_class_decls (type);
5622 /* We are re-entering the same class we just left, so we
5623 don't have to search the whole inheritance matrix to find
5624 all the decls to bind again. Instead, we install the
5625 cached class_shadowed list, and walk through it binding
5626 names and setting up IDENTIFIER_TYPE_VALUEs. */
5627 set_class_shadows (previous_class_values);
5628 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5630 tree id = TREE_PURPOSE (item);
5631 tree decl = TREE_TYPE (item);
5633 push_class_binding (id, decl);
5634 if (TREE_CODE (decl) == TYPE_DECL)
5635 set_identifier_type_value (id, TREE_TYPE (decl));
5637 unuse_fields (type);
5640 storetags (CLASSTYPE_TAGS (type));
5644 /* When we exit a toplevel class scope, we save the
5645 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5646 reenter the class. Here, we've entered some other class, so we
5647 must invalidate our cache. */
5650 invalidate_class_lookup_cache ()
5654 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5655 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5656 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5658 previous_class_values = NULL_TREE;
5659 previous_class_type = NULL_TREE;
5662 /* Get out of the current class scope. If we were in a class scope
5663 previously, that is the one popped to. */
5669 /* Since poplevel_class does the popping of class decls nowadays,
5670 this really only frees the obstack used for these decls. */
5673 current_class_depth--;
5674 current_class_name = current_class_stack[current_class_depth].name;
5675 current_class_type = current_class_stack[current_class_depth].type;
5676 current_access_specifier = current_class_stack[current_class_depth].access;
5677 if (current_class_stack[current_class_depth].names_used)
5678 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5681 /* Returns 1 if current_class_type is either T or a nested type of T.
5682 We start looking from 1 because entry 0 is from global scope, and has
5686 currently_open_class (t)
5690 if (t == current_class_type)
5692 for (i = 1; i < current_class_depth; ++i)
5693 if (current_class_stack [i].type == t)
5698 /* If either current_class_type or one of its enclosing classes are derived
5699 from T, return the appropriate type. Used to determine how we found
5700 something via unqualified lookup. */
5703 currently_open_derived_class (t)
5708 if (DERIVED_FROM_P (t, current_class_type))
5709 return current_class_type;
5711 for (i = current_class_depth - 1; i > 0; --i)
5712 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5713 return current_class_stack[i].type;
5718 /* When entering a class scope, all enclosing class scopes' names with
5719 static meaning (static variables, static functions, types and enumerators)
5720 have to be visible. This recursive function calls pushclass for all
5721 enclosing class contexts until global or a local scope is reached.
5722 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5723 formal of the same name. */
5726 push_nested_class (type, modify)
5732 /* A namespace might be passed in error cases, like A::B:C. */
5733 if (type == NULL_TREE
5734 || type == error_mark_node
5735 || TREE_CODE (type) == NAMESPACE_DECL
5736 || ! IS_AGGR_TYPE (type)
5737 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5738 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5741 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5743 if (context && CLASS_TYPE_P (context))
5744 push_nested_class (context, 2);
5745 pushclass (type, modify);
5748 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5753 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5756 if (context && CLASS_TYPE_P (context))
5757 pop_nested_class ();
5760 /* Returns the number of extern "LANG" blocks we are nested within. */
5763 current_lang_depth ()
5765 return VARRAY_ACTIVE_SIZE (current_lang_base);
5768 /* Set global variables CURRENT_LANG_NAME to appropriate value
5769 so that behavior of name-mangling machinery is correct. */
5772 push_lang_context (name)
5775 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5777 if (name == lang_name_cplusplus)
5779 current_lang_name = name;
5781 else if (name == lang_name_java)
5783 current_lang_name = name;
5784 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5785 (See record_builtin_java_type in decl.c.) However, that causes
5786 incorrect debug entries if these types are actually used.
5787 So we re-enable debug output after extern "Java". */
5788 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5789 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5790 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5791 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5792 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5793 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5794 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5795 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5797 else if (name == lang_name_c)
5799 current_lang_name = name;
5802 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5805 /* Get out of the current language scope. */
5810 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5811 VARRAY_POP (current_lang_base);
5814 /* Type instantiation routines. */
5816 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5817 matches the TARGET_TYPE. If there is no satisfactory match, return
5818 error_mark_node, and issue an error message if COMPLAIN is
5819 non-zero. Permit pointers to member function if PTRMEM is non-zero.
5820 If TEMPLATE_ONLY, the name of the overloaded function
5821 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5822 template arguments. */
5825 resolve_address_of_overloaded_function (target_type,
5836 tree explicit_targs;
5838 /* Here's what the standard says:
5842 If the name is a function template, template argument deduction
5843 is done, and if the argument deduction succeeds, the deduced
5844 arguments are used to generate a single template function, which
5845 is added to the set of overloaded functions considered.
5847 Non-member functions and static member functions match targets of
5848 type "pointer-to-function" or "reference-to-function." Nonstatic
5849 member functions match targets of type "pointer-to-member
5850 function;" the function type of the pointer to member is used to
5851 select the member function from the set of overloaded member
5852 functions. If a nonstatic member function is selected, the
5853 reference to the overloaded function name is required to have the
5854 form of a pointer to member as described in 5.3.1.
5856 If more than one function is selected, any template functions in
5857 the set are eliminated if the set also contains a non-template
5858 function, and any given template function is eliminated if the
5859 set contains a second template function that is more specialized
5860 than the first according to the partial ordering rules 14.5.5.2.
5861 After such eliminations, if any, there shall remain exactly one
5862 selected function. */
5865 int is_reference = 0;
5866 /* We store the matches in a TREE_LIST rooted here. The functions
5867 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5868 interoperability with most_specialized_instantiation. */
5869 tree matches = NULL_TREE;
5872 /* By the time we get here, we should be seeing only real
5873 pointer-to-member types, not the internal POINTER_TYPE to
5874 METHOD_TYPE representation. */
5875 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5876 && (TREE_CODE (TREE_TYPE (target_type))
5877 == METHOD_TYPE)), 0);
5879 if (TREE_CODE (overload) == COMPONENT_REF)
5880 overload = TREE_OPERAND (overload, 1);
5882 /* Check that the TARGET_TYPE is reasonable. */
5883 if (TYPE_PTRFN_P (target_type))
5885 else if (TYPE_PTRMEMFUNC_P (target_type))
5886 /* This is OK, too. */
5888 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5890 /* This is OK, too. This comes from a conversion to reference
5892 target_type = build_reference_type (target_type);
5899 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5900 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5901 return error_mark_node;
5904 /* If we can find a non-template function that matches, we can just
5905 use it. There's no point in generating template instantiations
5906 if we're just going to throw them out anyhow. But, of course, we
5907 can only do this when we don't *need* a template function. */
5912 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5914 tree fn = OVL_FUNCTION (fns);
5917 if (TREE_CODE (fn) == TEMPLATE_DECL)
5918 /* We're not looking for templates just yet. */
5921 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5923 /* We're looking for a non-static member, and this isn't
5924 one, or vice versa. */
5927 /* See if there's a match. */
5928 fntype = TREE_TYPE (fn);
5930 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5931 else if (!is_reference)
5932 fntype = build_pointer_type (fntype);
5934 if (can_convert_arg (target_type, fntype, fn))
5935 matches = tree_cons (fn, NULL_TREE, matches);
5939 /* Now, if we've already got a match (or matches), there's no need
5940 to proceed to the template functions. But, if we don't have a
5941 match we need to look at them, too. */
5944 tree target_fn_type;
5945 tree target_arg_types;
5946 tree target_ret_type;
5951 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5953 target_fn_type = TREE_TYPE (target_type);
5954 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5955 target_ret_type = TREE_TYPE (target_fn_type);
5957 /* Never do unification on the 'this' parameter. */
5958 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5959 target_arg_types = TREE_CHAIN (target_arg_types);
5961 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5963 tree fn = OVL_FUNCTION (fns);
5965 tree instantiation_type;
5968 if (TREE_CODE (fn) != TEMPLATE_DECL)
5969 /* We're only looking for templates. */
5972 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5974 /* We're not looking for a non-static member, and this is
5975 one, or vice versa. */
5978 /* Try to do argument deduction. */
5979 targs = make_tree_vec (DECL_NTPARMS (fn));
5980 if (fn_type_unification (fn, explicit_targs, targs,
5981 target_arg_types, target_ret_type,
5982 DEDUCE_EXACT, -1) != 0)
5983 /* Argument deduction failed. */
5986 /* Instantiate the template. */
5987 instantiation = instantiate_template (fn, targs);
5988 if (instantiation == error_mark_node)
5989 /* Instantiation failed. */
5992 /* See if there's a match. */
5993 instantiation_type = TREE_TYPE (instantiation);
5995 instantiation_type =
5996 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5997 else if (!is_reference)
5998 instantiation_type = build_pointer_type (instantiation_type);
5999 if (can_convert_arg (target_type, instantiation_type, instantiation))
6000 matches = tree_cons (instantiation, fn, matches);
6003 /* Now, remove all but the most specialized of the matches. */
6006 tree match = most_specialized_instantiation (matches);
6008 if (match != error_mark_node)
6009 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6013 /* Now we should have exactly one function in MATCHES. */
6014 if (matches == NULL_TREE)
6016 /* There were *no* matches. */
6019 cp_error ("no matches converting function `%D' to type `%#T'",
6020 DECL_NAME (OVL_FUNCTION (overload)),
6023 /* print_candidates expects a chain with the functions in
6024 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6025 so why be clever?). */
6026 for (; overload; overload = OVL_NEXT (overload))
6027 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6030 print_candidates (matches);
6032 return error_mark_node;
6034 else if (TREE_CHAIN (matches))
6036 /* There were too many matches. */
6042 cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6043 DECL_NAME (OVL_FUNCTION (overload)),
6046 /* Since print_candidates expects the functions in the
6047 TREE_VALUE slot, we flip them here. */
6048 for (match = matches; match; match = TREE_CHAIN (match))
6049 TREE_VALUE (match) = TREE_PURPOSE (match);
6051 print_candidates (matches);
6054 return error_mark_node;
6057 /* Good, exactly one match. Now, convert it to the correct type. */
6058 fn = TREE_PURPOSE (matches);
6060 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6061 && !ptrmem && !flag_ms_extensions)
6063 static int explained;
6066 return error_mark_node;
6068 cp_pedwarn ("assuming pointer to member `%D'", fn);
6071 cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6077 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6078 return build_unary_op (ADDR_EXPR, fn, 0);
6081 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6082 will mark the function as addressed, but here we must do it
6084 mark_addressable (fn);
6090 /* This function will instantiate the type of the expression given in
6091 RHS to match the type of LHSTYPE. If errors exist, then return
6092 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6093 we complain on errors. If we are not complaining, never modify rhs,
6094 as overload resolution wants to try many possible instantiations, in
6095 the hope that at least one will work.
6097 For non-recursive calls, LHSTYPE should be a function, pointer to
6098 function, or a pointer to member function. */
6101 instantiate_type (lhstype, rhs, flags)
6103 enum instantiate_type_flags flags;
6105 int complain = (flags & itf_complain);
6106 int strict = (flags & itf_no_attributes)
6107 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6108 int allow_ptrmem = flags & itf_ptrmem_ok;
6110 flags &= ~itf_ptrmem_ok;
6112 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6115 error ("not enough type information");
6116 return error_mark_node;
6119 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6121 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6124 cp_error ("argument of type `%T' does not match `%T'",
6125 TREE_TYPE (rhs), lhstype);
6126 return error_mark_node;
6129 /* We don't overwrite rhs if it is an overloaded function.
6130 Copying it would destroy the tree link. */
6131 if (TREE_CODE (rhs) != OVERLOAD)
6132 rhs = copy_node (rhs);
6134 /* This should really only be used when attempting to distinguish
6135 what sort of a pointer to function we have. For now, any
6136 arithmetic operation which is not supported on pointers
6137 is rejected as an error. */
6139 switch (TREE_CODE (rhs))
6146 my_friendly_abort (177);
6147 return error_mark_node;
6154 new_rhs = instantiate_type (build_pointer_type (lhstype),
6155 TREE_OPERAND (rhs, 0), flags);
6156 if (new_rhs == error_mark_node)
6157 return error_mark_node;
6159 TREE_TYPE (rhs) = lhstype;
6160 TREE_OPERAND (rhs, 0) = new_rhs;
6165 rhs = copy_node (TREE_OPERAND (rhs, 0));
6166 TREE_TYPE (rhs) = unknown_type_node;
6167 return instantiate_type (lhstype, rhs, flags);
6170 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6173 rhs = TREE_OPERAND (rhs, 1);
6174 if (BASELINK_P (rhs))
6175 return instantiate_type (lhstype, TREE_VALUE (rhs),
6176 flags | allow_ptrmem);
6178 /* This can happen if we are forming a pointer-to-member for a
6180 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6184 case TEMPLATE_ID_EXPR:
6186 tree fns = TREE_OPERAND (rhs, 0);
6187 tree args = TREE_OPERAND (rhs, 1);
6190 resolve_address_of_overloaded_function (lhstype,
6194 /*template_only=*/1,
6200 resolve_address_of_overloaded_function (lhstype,
6204 /*template_only=*/0,
6205 /*explicit_targs=*/NULL_TREE);
6208 /* Now we should have a baselink. */
6209 my_friendly_assert (BASELINK_P (rhs), 990412);
6211 return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6214 /* This is too hard for now. */
6215 my_friendly_abort (183);
6216 return error_mark_node;
6221 TREE_OPERAND (rhs, 0)
6222 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6223 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6224 return error_mark_node;
6225 TREE_OPERAND (rhs, 1)
6226 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6227 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6228 return error_mark_node;
6230 TREE_TYPE (rhs) = lhstype;
6234 case TRUNC_DIV_EXPR:
6235 case FLOOR_DIV_EXPR:
6237 case ROUND_DIV_EXPR:
6239 case TRUNC_MOD_EXPR:
6240 case FLOOR_MOD_EXPR:
6242 case ROUND_MOD_EXPR:
6243 case FIX_ROUND_EXPR:
6244 case FIX_FLOOR_EXPR:
6246 case FIX_TRUNC_EXPR:
6262 case PREINCREMENT_EXPR:
6263 case PREDECREMENT_EXPR:
6264 case POSTINCREMENT_EXPR:
6265 case POSTDECREMENT_EXPR:
6267 error ("invalid operation on uninstantiated type");
6268 return error_mark_node;
6270 case TRUTH_AND_EXPR:
6272 case TRUTH_XOR_EXPR:
6279 case TRUTH_ANDIF_EXPR:
6280 case TRUTH_ORIF_EXPR:
6281 case TRUTH_NOT_EXPR:
6283 error ("not enough type information");
6284 return error_mark_node;
6287 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6290 error ("not enough type information");
6291 return error_mark_node;
6293 TREE_OPERAND (rhs, 1)
6294 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6295 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6296 return error_mark_node;
6297 TREE_OPERAND (rhs, 2)
6298 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6299 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6300 return error_mark_node;
6302 TREE_TYPE (rhs) = lhstype;
6306 TREE_OPERAND (rhs, 1)
6307 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6308 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6309 return error_mark_node;
6311 TREE_TYPE (rhs) = lhstype;
6316 if (PTRMEM_OK_P (rhs))
6317 flags |= itf_ptrmem_ok;
6319 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6321 case ENTRY_VALUE_EXPR:
6322 my_friendly_abort (184);
6323 return error_mark_node;
6326 return error_mark_node;
6329 my_friendly_abort (185);
6330 return error_mark_node;
6334 /* Return the name of the virtual function pointer field
6335 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6336 this may have to look back through base types to find the
6337 ultimate field name. (For single inheritance, these could
6338 all be the same name. Who knows for multiple inheritance). */
6341 get_vfield_name (type)
6344 tree binfo = TYPE_BINFO (type);
6347 while (BINFO_BASETYPES (binfo)
6348 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6349 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6350 binfo = BINFO_BASETYPE (binfo, 0);
6352 type = BINFO_TYPE (binfo);
6353 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6354 + TYPE_NAME_LENGTH (type) + 2);
6355 sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6356 return get_identifier (buf);
6360 print_class_statistics ()
6362 #ifdef GATHER_STATISTICS
6363 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6364 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6365 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6366 n_build_method_call, n_inner_fields_searched);
6369 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6370 n_vtables, n_vtable_searches);
6371 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6372 n_vtable_entries, n_vtable_elems);
6377 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6378 according to [class]:
6379 The class-name is also inserted
6380 into the scope of the class itself. For purposes of access checking,
6381 the inserted class name is treated as if it were a public member name. */
6384 build_self_reference ()
6386 tree name = constructor_name (current_class_type);
6387 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6390 DECL_NONLOCAL (value) = 1;
6391 DECL_CONTEXT (value) = current_class_type;
6392 DECL_ARTIFICIAL (value) = 1;
6394 if (processing_template_decl)
6395 value = push_template_decl (value);
6397 saved_cas = current_access_specifier;
6398 current_access_specifier = access_public_node;
6399 finish_member_declaration (value);
6400 current_access_specifier = saved_cas;
6403 /* Returns 1 if TYPE contains only padding bytes. */
6406 is_empty_class (type)
6409 if (type == error_mark_node)
6412 if (! IS_AGGR_TYPE (type))
6415 return integer_zerop (CLASSTYPE_SIZE (type));
6418 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6419 a *_TYPE node. NODE can also be a local class. */
6422 get_enclosing_class (type)
6427 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6429 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6432 node = DECL_CONTEXT (node);
6438 node = TYPE_CONTEXT (node);
6442 my_friendly_abort (0);
6448 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6451 is_base_of_enclosing_class (base, type)
6456 if (get_binfo (base, type, 0))
6459 type = get_enclosing_class (type);
6464 /* Note that NAME was looked up while the current class was being
6465 defined and that the result of that lookup was DECL. */
6468 maybe_note_name_used_in_class (name, decl)
6472 splay_tree names_used;
6474 /* If we're not defining a class, there's nothing to do. */
6475 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6478 /* If there's already a binding for this NAME, then we don't have
6479 anything to worry about. */
6480 if (IDENTIFIER_CLASS_VALUE (name))
6483 if (!current_class_stack[current_class_depth - 1].names_used)
6484 current_class_stack[current_class_depth - 1].names_used
6485 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6486 names_used = current_class_stack[current_class_depth - 1].names_used;
6488 splay_tree_insert (names_used,
6489 (splay_tree_key) name,
6490 (splay_tree_value) decl);
6493 /* Note that NAME was declared (as DECL) in the current class. Check
6494 to see that the declaration is legal. */
6497 note_name_declared_in_class (name, decl)
6501 splay_tree names_used;
6504 /* Look to see if we ever used this name. */
6506 = current_class_stack[current_class_depth - 1].names_used;
6510 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6513 /* [basic.scope.class]
6515 A name N used in a class S shall refer to the same declaration
6516 in its context and when re-evaluated in the completed scope of
6518 cp_error ("declaration of `%#D'", decl);
6519 cp_error_at ("changes meaning of `%D' from `%+#D'",
6520 DECL_NAME (OVL_CURRENT (decl)),
6525 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6526 Secondary vtables are merged with primary vtables; this function
6527 will return the VAR_DECL for the primary vtable. */
6530 get_vtbl_decl_for_binfo (binfo)
6535 decl = BINFO_VTABLE (binfo);
6536 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6538 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6540 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6543 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6547 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6548 who's TREE_PURPOSE is the TYPE of the required primary base and
6549 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6552 dfs_get_primary_binfo (binfo, data)
6556 tree cons = (tree) data;
6557 tree primary_base = TREE_PURPOSE (cons);
6559 if (TREE_VIA_VIRTUAL (binfo)
6560 && same_type_p (BINFO_TYPE (binfo), primary_base))
6561 /* This is the right type of binfo, but it might be an unshared
6562 instance, and the shared instance is later in the dfs walk. We
6563 must keep looking. */
6564 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6569 /* Returns the unshared binfo for the primary base of BINFO. Note
6570 that in a complex hierarchy the resulting BINFO may not actually
6571 *be* primary. In particular if the resulting BINFO is a virtual
6572 base, and it occurs elsewhere in the hierarchy, then this
6573 occurrence may not actually be a primary base in the complete
6574 object. Check BINFO_PRIMARY_P to be sure. */
6577 get_primary_binfo (binfo)
6581 tree result = NULL_TREE;
6584 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6588 /* A non-virtual primary base is always a direct base, and easy to
6590 if (!TREE_VIA_VIRTUAL (primary_base))
6594 /* Scan the direct basetypes until we find a base with the same
6595 type as the primary base. */
6596 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6598 tree base_binfo = BINFO_BASETYPE (binfo, i);
6600 if (same_type_p (BINFO_TYPE (base_binfo),
6601 BINFO_TYPE (primary_base)))
6605 /* We should always find the primary base. */
6606 my_friendly_abort (20000729);
6609 /* For a primary virtual base, we have to scan the entire hierarchy
6610 rooted at BINFO; the virtual base could be an indirect virtual
6611 base. There could be more than one instance of the primary base
6612 in the hierarchy, and if one is the canonical binfo we want that
6613 one. If it exists, it should be the first one we find, but as a
6614 consistency check we find them all and make sure. */
6615 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6616 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6617 virtuals = TREE_VALUE (virtuals);
6619 /* We must have found at least one instance. */
6620 my_friendly_assert (virtuals, 20010612);
6622 if (TREE_CHAIN (virtuals))
6624 /* We found more than one instance of the base. We must make
6625 sure that, if one is the canonical one, it is the first one
6626 we found. As the chain is in reverse dfs order, that means
6627 the last on the list. */
6628 tree complete_binfo;
6631 for (complete_binfo = binfo;
6632 BINFO_INHERITANCE_CHAIN (complete_binfo);
6633 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6635 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6636 BINFO_TYPE (complete_binfo));
6638 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6640 result = TREE_VALUE (virtuals);
6642 if (canonical == result)
6644 /* This is the unshared instance. Make sure it was the
6646 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6652 result = TREE_VALUE (virtuals);
6656 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6659 maybe_indent_hierarchy (stream, indent, indented_p)
6665 fprintf (stream, "%*s", indent, "");
6669 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6670 dominated by T) to stderr. INDENT should be zero when called from
6671 the top level; it is incremented recursively. */
6674 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6684 indented = maybe_indent_hierarchy (stream, indent, 0);
6685 fprintf (stream, "%s (0x%lx) ",
6686 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6687 (unsigned long) binfo);
6688 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6689 tree_low_cst (BINFO_OFFSET (binfo), 0));
6690 if (is_empty_class (BINFO_TYPE (binfo)))
6691 fprintf (stream, " empty");
6692 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6693 fprintf (stream, " nearly-empty");
6694 if (TREE_VIA_VIRTUAL (binfo))
6696 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6698 fprintf (stream, " virtual");
6699 if (canonical == binfo)
6700 fprintf (stream, " canonical");
6702 fprintf (stream, " non-canonical");
6704 fprintf (stream, "\n");
6707 if (BINFO_PRIMARY_BASE_OF (binfo))
6709 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6710 fprintf (stream, " primary-for %s (0x%lx)",
6711 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6712 TFF_PLAIN_IDENTIFIER),
6713 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6715 if (BINFO_LOST_PRIMARY_P (binfo))
6717 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6718 fprintf (stream, " lost-primary");
6721 fprintf (stream, "\n");
6723 if (!(flags & TDF_SLIM))
6727 if (BINFO_SUBVTT_INDEX (binfo))
6729 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6730 fprintf (stream, " subvttidx=%s",
6731 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6732 TFF_PLAIN_IDENTIFIER));
6734 if (BINFO_VPTR_INDEX (binfo))
6736 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6737 fprintf (stream, " vptridx=%s",
6738 expr_as_string (BINFO_VPTR_INDEX (binfo),
6739 TFF_PLAIN_IDENTIFIER));
6741 if (BINFO_VPTR_FIELD (binfo))
6743 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6744 fprintf (stream, " vbaseoffset=%s",
6745 expr_as_string (BINFO_VPTR_FIELD (binfo),
6746 TFF_PLAIN_IDENTIFIER));
6748 if (BINFO_VTABLE (binfo))
6750 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6751 fprintf (stream, " vptr=%s",
6752 expr_as_string (BINFO_VTABLE (binfo),
6753 TFF_PLAIN_IDENTIFIER));
6757 fprintf (stream, "\n");
6761 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6762 dump_class_hierarchy_r (stream, flags,
6763 t, BINFO_BASETYPE (binfo, i),
6767 /* Dump the BINFO hierarchy for T. */
6770 dump_class_hierarchy (t)
6774 FILE *stream = dump_begin (TDI_class, &flags);
6779 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6780 fprintf (stream, " size=%lu align=%lu\n",
6781 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6782 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6783 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6784 fprintf (stream, "\n");
6785 dump_end (TDI_class, stream);
6789 dump_array (stream, decl)
6796 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6798 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6800 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6801 fprintf (stream, " %s entries",
6802 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6803 TFF_PLAIN_IDENTIFIER));
6804 fprintf (stream, "\n");
6806 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6807 inits; ix++, inits = TREE_CHAIN (inits))
6808 fprintf (stream, "%-4d %s\n", ix * elt,
6809 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6813 dump_vtable (t, binfo, vtable)
6819 FILE *stream = dump_begin (TDI_class, &flags);
6824 if (!(flags & TDF_SLIM))
6826 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6828 fprintf (stream, "%s for %s",
6829 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6830 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6833 if (!TREE_VIA_VIRTUAL (binfo))
6834 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6835 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6837 fprintf (stream, "\n");
6838 dump_array (stream, vtable);
6839 fprintf (stream, "\n");
6842 dump_end (TDI_class, stream);
6851 FILE *stream = dump_begin (TDI_class, &flags);
6856 if (!(flags & TDF_SLIM))
6858 fprintf (stream, "VTT for %s\n",
6859 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6860 dump_array (stream, vtt);
6861 fprintf (stream, "\n");
6864 dump_end (TDI_class, stream);
6867 /* Virtual function table initialization. */
6869 /* Create all the necessary vtables for T and its base classes. */
6878 /* We lay out the primary and secondary vtables in one contiguous
6879 vtable. The primary vtable is first, followed by the non-virtual
6880 secondary vtables in inheritance graph order. */
6881 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6882 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6883 TYPE_BINFO (t), t, list);
6885 /* Then come the virtual bases, also in inheritance graph order. */
6886 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6890 if (!TREE_VIA_VIRTUAL (vbase))
6893 /* Although we walk in inheritance order, that might not get the
6895 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6897 accumulate_vtbl_inits (real_base, real_base,
6898 TYPE_BINFO (t), t, list);
6901 if (TYPE_BINFO_VTABLE (t))
6902 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6905 /* Initialize the vtable for BINFO with the INITS. */
6908 initialize_vtable (binfo, inits)
6914 layout_vtable_decl (binfo, list_length (inits));
6915 decl = get_vtbl_decl_for_binfo (binfo);
6916 initialize_array (decl, inits);
6917 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6920 /* Initialize DECL (a declaration for a namespace-scope array) with
6924 initialize_array (decl, inits)
6930 context = DECL_CONTEXT (decl);
6931 DECL_CONTEXT (decl) = NULL_TREE;
6932 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6933 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6934 DECL_CONTEXT (decl) = context;
6937 /* Build the VTT (virtual table table) for T.
6938 A class requires a VTT if it has virtual bases.
6941 1 - primary virtual pointer for complete object T
6942 2 - secondary VTTs for each direct non-virtual base of T which requires a
6944 3 - secondary virtual pointers for each direct or indirect base of T which
6945 has virtual bases or is reachable via a virtual path from T.
6946 4 - secondary VTTs for each direct or indirect virtual base of T.
6948 Secondary VTTs look like complete object VTTs without part 4. */
6959 /* Build up the initializers for the VTT. */
6961 index = size_zero_node;
6962 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6964 /* If we didn't need a VTT, we're done. */
6968 /* Figure out the type of the VTT. */
6969 type = build_index_type (size_int (list_length (inits) - 1));
6970 type = build_cplus_array_type (const_ptr_type_node, type);
6972 /* Now, build the VTT object itself. */
6973 vtt = build_vtable (t, get_vtt_name (t), type);
6974 pushdecl_top_level (vtt);
6975 initialize_array (vtt, inits);
6980 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6981 from within some heirarchy which is inherited from the type of BINFO.
6982 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6986 get_original_base (base_binfo, binfo)
6993 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6995 if (TREE_VIA_VIRTUAL (base_binfo))
6996 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6997 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6999 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7000 if (same_type_p (BINFO_TYPE (base_binfo),
7001 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7002 return BINFO_BASETYPE (derived, ix);
7003 my_friendly_abort (20010223);
7007 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7008 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7009 and CHAIN the vtable pointer for this binfo after construction is
7010 complete. VALUE can also be another BINFO, in which case we recurse. */
7013 binfo_ctor_vtable (binfo)
7020 vt = BINFO_VTABLE (binfo);
7021 if (TREE_CODE (vt) == TREE_LIST)
7022 vt = TREE_VALUE (vt);
7023 if (TREE_CODE (vt) == TREE_VEC)
7032 /* Recursively build the VTT-initializer for BINFO (which is in the
7033 hierarchy dominated by T). INITS points to the end of the initializer
7034 list to date. INDEX is the VTT index where the next element will be
7035 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7036 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7037 for virtual bases of T. When it is not so, we build the constructor
7038 vtables for the BINFO-in-T variant. */
7041 build_vtt_inits (binfo, t, inits, index)
7050 tree secondary_vptrs;
7051 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7053 /* We only need VTTs for subobjects with virtual bases. */
7054 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7057 /* We need to use a construction vtable if this is not the primary
7061 build_ctor_vtbl_group (binfo, t);
7063 /* Record the offset in the VTT where this sub-VTT can be found. */
7064 BINFO_SUBVTT_INDEX (binfo) = *index;
7067 /* Add the address of the primary vtable for the complete object. */
7068 init = binfo_ctor_vtable (binfo);
7069 *inits = build_tree_list (NULL_TREE, init);
7070 inits = &TREE_CHAIN (*inits);
7073 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7074 BINFO_VPTR_INDEX (binfo) = *index;
7076 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7078 /* Recursively add the secondary VTTs for non-virtual bases. */
7079 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7081 b = BINFO_BASETYPE (binfo, i);
7082 if (!TREE_VIA_VIRTUAL (b))
7083 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7087 /* Add secondary virtual pointers for all subobjects of BINFO with
7088 either virtual bases or reachable along a virtual path, except
7089 subobjects that are non-virtual primary bases. */
7090 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7091 TREE_TYPE (secondary_vptrs) = *index;
7092 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7093 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7095 dfs_walk_real (binfo,
7096 dfs_build_secondary_vptr_vtt_inits,
7098 dfs_ctor_vtable_bases_queue_p,
7100 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7101 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7104 *index = TREE_TYPE (secondary_vptrs);
7106 /* The secondary vptrs come back in reverse order. After we reverse
7107 them, and add the INITS, the last init will be the first element
7109 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7110 if (secondary_vptrs)
7112 *inits = nreverse (secondary_vptrs);
7113 inits = &TREE_CHAIN (secondary_vptrs);
7114 my_friendly_assert (*inits == NULL_TREE, 20000517);
7117 /* Add the secondary VTTs for virtual bases. */
7119 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7123 if (!TREE_VIA_VIRTUAL (b))
7126 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7127 inits = build_vtt_inits (vbase, t, inits, index);
7132 tree data = tree_cons (t, binfo, NULL_TREE);
7133 VTT_TOP_LEVEL_P (data) = 0;
7134 VTT_MARKED_BINFO_P (data) = 0;
7136 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7137 dfs_ctor_vtable_bases_queue_p,
7144 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7145 for the base in most derived. DATA is a TREE_LIST who's
7146 TREE_CHAIN is the type of the base being
7147 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7148 flag of DATA indicates that this is a constructor vtable. The
7149 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7152 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7164 top_level_p = VTT_TOP_LEVEL_P (l);
7166 SET_BINFO_MARKED (binfo);
7168 /* We don't care about bases that don't have vtables. */
7169 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7172 /* We're only interested in proper subobjects of T. */
7173 if (same_type_p (BINFO_TYPE (binfo), t))
7176 /* We're not interested in non-virtual primary bases. */
7177 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7180 /* If BINFO has virtual bases or is reachable via a virtual path
7181 from T, it'll have a secondary vptr. */
7182 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7183 && !binfo_via_virtual (binfo, t))
7186 /* Record the index where this secondary vptr can be found. */
7187 index = TREE_TYPE (l);
7190 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7191 BINFO_VPTR_INDEX (binfo) = index;
7193 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7194 TYPE_SIZE_UNIT (ptr_type_node));
7196 /* Add the initializer for the secondary vptr itself. */
7197 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7199 /* It's a primary virtual base, and this is not the construction
7200 vtable. Find the base this is primary of in the inheritance graph,
7201 and use that base's vtable now. */
7202 while (BINFO_PRIMARY_BASE_OF (binfo))
7203 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7205 init = binfo_ctor_vtable (binfo);
7206 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7211 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7212 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7213 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7217 dfs_ctor_vtable_bases_queue_p (binfo, data)
7221 if (TREE_VIA_VIRTUAL (binfo))
7222 /* Get the shared version. */
7223 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7225 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7230 /* Called from build_vtt_inits via dfs_walk. After building constructor
7231 vtables and generating the sub-vtt from them, we need to restore the
7232 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7233 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7236 dfs_fixup_binfo_vtbls (binfo, data)
7240 CLEAR_BINFO_MARKED (binfo);
7242 /* We don't care about bases that don't have vtables. */
7243 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7246 /* If we scribbled the construction vtable vptr into BINFO, clear it
7248 if (BINFO_VTABLE (binfo)
7249 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7250 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7251 == TREE_VALUE ((tree) data)))
7252 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7257 /* Build the construction vtable group for BINFO which is in the
7258 hierarchy dominated by T. */
7261 build_ctor_vtbl_group (binfo, t)
7272 /* See if we've already created this construction vtable group. */
7273 id = mangle_ctor_vtbl_for_type (t, binfo);
7274 if (IDENTIFIER_GLOBAL_VALUE (id))
7277 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7278 /* Build a version of VTBL (with the wrong type) for use in
7279 constructing the addresses of secondary vtables in the
7280 construction vtable group. */
7281 vtbl = build_vtable (t, id, ptr_type_node);
7282 list = build_tree_list (vtbl, NULL_TREE);
7283 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7286 /* Add the vtables for each of our virtual bases using the vbase in T
7288 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7290 vbase = TREE_CHAIN (vbase))
7295 if (!TREE_VIA_VIRTUAL (vbase))
7297 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7298 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7300 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7302 inits = TREE_VALUE (list);
7304 /* Figure out the type of the construction vtable. */
7305 type = build_index_type (size_int (list_length (inits) - 1));
7306 type = build_cplus_array_type (vtable_entry_type, type);
7307 TREE_TYPE (vtbl) = type;
7309 /* Initialize the construction vtable. */
7310 pushdecl_top_level (vtbl);
7311 initialize_array (vtbl, inits);
7312 dump_vtable (t, binfo, vtbl);
7315 /* Add the vtbl initializers for BINFO (and its bases other than
7316 non-virtual primaries) to the list of INITS. BINFO is in the
7317 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7318 the constructor the vtbl inits should be accumulated for. (If this
7319 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7320 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7321 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7322 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7323 but are not necessarily the same in terms of layout. */
7326 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7334 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7336 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7337 BINFO_TYPE (orig_binfo)),
7340 /* If it doesn't have a vptr, we don't do anything. */
7341 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7344 /* If we're building a construction vtable, we're not interested in
7345 subobjects that don't require construction vtables. */
7347 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7348 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7351 /* Build the initializers for the BINFO-in-T vtable. */
7353 = chainon (TREE_VALUE (inits),
7354 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7355 rtti_binfo, t, inits));
7357 /* Walk the BINFO and its bases. We walk in preorder so that as we
7358 initialize each vtable we can figure out at what offset the
7359 secondary vtable lies from the primary vtable. We can't use
7360 dfs_walk here because we need to iterate through bases of BINFO
7361 and RTTI_BINFO simultaneously. */
7362 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7364 tree base_binfo = BINFO_BASETYPE (binfo, i);
7366 /* Skip virtual bases. */
7367 if (TREE_VIA_VIRTUAL (base_binfo))
7369 accumulate_vtbl_inits (base_binfo,
7370 BINFO_BASETYPE (orig_binfo, i),
7376 /* Called from accumulate_vtbl_inits. Returns the initializers for
7377 the BINFO vtable. */
7380 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7387 tree inits = NULL_TREE;
7388 tree vtbl = NULL_TREE;
7389 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7392 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7394 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7395 primary virtual base. If it is not the same primary in
7396 the hierarchy of T, we'll need to generate a ctor vtable
7397 for it, to place at its location in T. If it is the same
7398 primary, we still need a VTT entry for the vtable, but it
7399 should point to the ctor vtable for the base it is a
7400 primary for within the sub-hierarchy of RTTI_BINFO.
7402 There are three possible cases:
7404 1) We are in the same place.
7405 2) We are a primary base within a lost primary virtual base of
7407 3) We are primary to something not a base of RTTI_BINFO. */
7409 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7410 tree last = NULL_TREE;
7412 /* First, look through the bases we are primary to for RTTI_BINFO
7413 or a virtual base. */
7414 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7417 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7420 /* If we run out of primary links, keep looking down our
7421 inheritance chain; we might be an indirect primary. */
7423 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7424 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7427 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7428 base B and it is a base of RTTI_BINFO, this is case 2. In
7429 either case, we share our vtable with LAST, i.e. the
7430 derived-most base within B of which we are a primary. */
7432 || (b && binfo_for_vbase (BINFO_TYPE (b),
7433 BINFO_TYPE (rtti_binfo))))
7434 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7435 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7436 binfo_ctor_vtable after everything's been set up. */
7439 /* Otherwise, this is case 3 and we get our own. */
7441 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7449 /* Compute the initializer for this vtable. */
7450 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7453 /* Figure out the position to which the VPTR should point. */
7454 vtbl = TREE_PURPOSE (l);
7455 vtbl = build1 (ADDR_EXPR,
7458 TREE_CONSTANT (vtbl) = 1;
7459 index = size_binop (PLUS_EXPR,
7460 size_int (non_fn_entries),
7461 size_int (list_length (TREE_VALUE (l))));
7462 index = size_binop (MULT_EXPR,
7463 TYPE_SIZE_UNIT (vtable_entry_type),
7465 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7466 TREE_CONSTANT (vtbl) = 1;
7470 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7471 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7472 straighten this out. */
7473 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7474 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7477 /* For an ordinary vtable, set BINFO_VTABLE. */
7478 BINFO_VTABLE (binfo) = vtbl;
7483 /* Construct the initializer for BINFO's virtual function table. BINFO
7484 is part of the hierarchy dominated by T. If we're building a
7485 construction vtable, the ORIG_BINFO is the binfo we should use to
7486 find the actual function pointers to put in the vtable - but they
7487 can be overridden on the path to most-derived in the graph that
7488 ORIG_BINFO belongs. Otherwise,
7489 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7490 BINFO that should be indicated by the RTTI information in the
7491 vtable; it will be a base class of T, rather than T itself, if we
7492 are building a construction vtable.
7494 The value returned is a TREE_LIST suitable for wrapping in a
7495 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7496 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7497 number of non-function entries in the vtable.
7499 It might seem that this function should never be called with a
7500 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7501 base is always subsumed by a derived class vtable. However, when
7502 we are building construction vtables, we do build vtables for
7503 primary bases; we need these while the primary base is being
7507 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7512 int *non_fn_entries_p;
7519 /* Initialize VID. */
7520 memset (&vid, 0, sizeof (vid));
7523 vid.rtti_binfo = rtti_binfo;
7524 vid.last_init = &vid.inits;
7525 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7526 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7527 /* The first vbase or vcall offset is at index -3 in the vtable. */
7528 vid.index = ssize_int (-3);
7530 /* Add entries to the vtable for RTTI. */
7531 build_rtti_vtbl_entries (binfo, &vid);
7533 /* Create an array for keeping track of the functions we've
7534 processed. When we see multiple functions with the same
7535 signature, we share the vcall offsets. */
7536 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7537 /* Add the vcall and vbase offset entries. */
7538 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7540 VARRAY_FREE (vid.fns);
7541 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7542 build_vbase_offset_vtbl_entries. */
7543 for (vbase = CLASSTYPE_VBASECLASSES (t);
7545 vbase = TREE_CHAIN (vbase))
7546 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7548 if (non_fn_entries_p)
7549 *non_fn_entries_p = list_length (vid.inits);
7551 /* Go through all the ordinary virtual functions, building up
7553 vfun_inits = NULL_TREE;
7554 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7562 /* Pull the offset for `this', and the function to call, out of
7564 delta = BV_DELTA (v);
7566 if (BV_USE_VCALL_INDEX_P (v))
7568 vcall_index = BV_VCALL_INDEX (v);
7569 my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7572 vcall_index = NULL_TREE;
7575 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7576 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7578 /* You can't call an abstract virtual function; it's abstract.
7579 So, we replace these functions with __pure_virtual. */
7580 if (DECL_PURE_VIRTUAL_P (fn))
7583 /* Take the address of the function, considering it to be of an
7584 appropriate generic type. */
7585 pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7586 /* The address of a function can't change. */
7587 TREE_CONSTANT (pfn) = 1;
7589 /* Enter it in the vtable. */
7590 init = build_vtable_entry (delta, vcall_index, pfn);
7592 /* If the only definition of this function signature along our
7593 primary base chain is from a lost primary, this vtable slot will
7594 never be used, so just zero it out. This is important to avoid
7595 requiring extra thunks which cannot be generated with the function.
7597 We could also handle this in update_vtable_entry_for_fn; doing it
7598 here means we zero out unused slots in ctor vtables as well,
7599 rather than filling them with erroneous values (though harmless,
7600 apart from relocation costs). */
7601 if (fn != abort_fndecl)
7602 for (b = binfo; ; b = get_primary_binfo (b))
7604 /* We found a defn before a lost primary; go ahead as normal. */
7605 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7608 /* The nearest definition is from a lost primary; clear the
7610 if (BINFO_LOST_PRIMARY_P (b))
7612 init = size_zero_node;
7617 /* And add it to the chain of initializers. */
7618 if (TARGET_VTABLE_USES_DESCRIPTORS)
7621 if (init == size_zero_node)
7622 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7623 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7625 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7627 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7628 TREE_OPERAND (init, 0),
7629 build_int_2 (i, 0));
7630 TREE_CONSTANT (fdesc) = 1;
7632 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7636 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7639 /* The initializers for virtual functions were built up in reverse
7640 order; straighten them out now. */
7641 vfun_inits = nreverse (vfun_inits);
7643 /* The negative offset initializers are also in reverse order. */
7644 vid.inits = nreverse (vid.inits);
7646 /* Chain the two together. */
7647 return chainon (vid.inits, vfun_inits);
7650 /* Adds to vid->inits the initializers for the vbase and vcall
7651 offsets in BINFO, which is in the hierarchy dominated by T. */
7654 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7656 vtbl_init_data *vid;
7660 /* If this is a derived class, we must first create entries
7661 corresponding to the primary base class. */
7662 b = get_primary_binfo (binfo);
7664 build_vcall_and_vbase_vtbl_entries (b, vid);
7666 /* Add the vbase entries for this base. */
7667 build_vbase_offset_vtbl_entries (binfo, vid);
7668 /* Add the vcall entries for this base. */
7669 build_vcall_offset_vtbl_entries (binfo, vid);
7672 /* Returns the initializers for the vbase offset entries in the vtable
7673 for BINFO (which is part of the class hierarchy dominated by T), in
7674 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7675 where the next vbase offset will go. */
7678 build_vbase_offset_vtbl_entries (binfo, vid)
7680 vtbl_init_data *vid;
7684 tree non_primary_binfo;
7686 /* If there are no virtual baseclasses, then there is nothing to
7688 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7693 /* We might be a primary base class. Go up the inheritance hierarchy
7694 until we find the most derived class of which we are a primary base:
7695 it is the offset of that which we need to use. */
7696 non_primary_binfo = binfo;
7697 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7701 /* If we have reached a virtual base, then it must be a primary
7702 base (possibly multi-level) of vid->binfo, or we wouldn't
7703 have called build_vcall_and_vbase_vtbl_entries for it. But it
7704 might be a lost primary, so just skip down to vid->binfo. */
7705 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7707 non_primary_binfo = vid->binfo;
7711 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7712 if (get_primary_binfo (b) != non_primary_binfo)
7714 non_primary_binfo = b;
7717 /* Go through the virtual bases, adding the offsets. */
7718 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7720 vbase = TREE_CHAIN (vbase))
7725 if (!TREE_VIA_VIRTUAL (vbase))
7728 /* Find the instance of this virtual base in the complete
7730 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7732 /* If we've already got an offset for this virtual base, we
7733 don't need another one. */
7734 if (BINFO_VTABLE_PATH_MARKED (b))
7736 SET_BINFO_VTABLE_PATH_MARKED (b);
7738 /* Figure out where we can find this vbase offset. */
7739 delta = size_binop (MULT_EXPR,
7742 TYPE_SIZE_UNIT (vtable_entry_type)));
7743 if (vid->primary_vtbl_p)
7744 BINFO_VPTR_FIELD (b) = delta;
7746 if (binfo != TYPE_BINFO (t))
7750 /* Find the instance of this virtual base in the type of BINFO. */
7751 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7752 BINFO_TYPE (binfo));
7754 /* The vbase offset had better be the same. */
7755 if (!tree_int_cst_equal (delta,
7756 BINFO_VPTR_FIELD (orig_vbase)))
7757 my_friendly_abort (20000403);
7760 /* The next vbase will come at a more negative offset. */
7761 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7763 /* The initializer is the delta from BINFO to this virtual base.
7764 The vbase offsets go in reverse inheritance-graph order, and
7765 we are walking in inheritance graph order so these end up in
7767 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7770 = build_tree_list (NULL_TREE,
7771 fold (build1 (NOP_EXPR,
7774 vid->last_init = &TREE_CHAIN (*vid->last_init);
7778 /* Adds the initializers for the vcall offset entries in the vtable
7779 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7783 build_vcall_offset_vtbl_entries (binfo, vid)
7785 vtbl_init_data *vid;
7787 /* We only need these entries if this base is a virtual base. */
7788 if (!TREE_VIA_VIRTUAL (binfo))
7791 /* We need a vcall offset for each of the virtual functions in this
7792 vtable. For example:
7794 class A { virtual void f (); };
7795 class B1 : virtual public A { virtual void f (); };
7796 class B2 : virtual public A { virtual void f (); };
7797 class C: public B1, public B2 { virtual void f (); };
7799 A C object has a primary base of B1, which has a primary base of A. A
7800 C also has a secondary base of B2, which no longer has a primary base
7801 of A. So the B2-in-C construction vtable needs a secondary vtable for
7802 A, which will adjust the A* to a B2* to call f. We have no way of
7803 knowing what (or even whether) this offset will be when we define B2,
7804 so we store this "vcall offset" in the A sub-vtable and look it up in
7805 a "virtual thunk" for B2::f.
7807 We need entries for all the functions in our primary vtable and
7808 in our non-virtual bases' secondary vtables. */
7810 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7811 add_vcall_offset_vtbl_entries_r (binfo, vid);
7814 /* Build vcall offsets, starting with those for BINFO. */
7817 add_vcall_offset_vtbl_entries_r (binfo, vid)
7819 vtbl_init_data *vid;
7824 /* Don't walk into virtual bases -- except, of course, for the
7825 virtual base for which we are building vcall offsets. Any
7826 primary virtual base will have already had its offsets generated
7827 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7828 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7831 /* If BINFO has a primary base, process it first. */
7832 primary_binfo = get_primary_binfo (binfo);
7834 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7836 /* Add BINFO itself to the list. */
7837 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7839 /* Scan the non-primary bases of BINFO. */
7840 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7844 base_binfo = BINFO_BASETYPE (binfo, i);
7845 if (base_binfo != primary_binfo)
7846 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7850 /* Called from build_vcall_offset_vtbl_entries_r. */
7853 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7855 vtbl_init_data* vid;
7857 tree derived_virtuals;
7861 /* If BINFO is a primary base, the most derived class which has BINFO as
7862 a primary base; otherwise, just BINFO. */
7863 tree non_primary_binfo;
7865 binfo_inits = NULL_TREE;
7867 /* We might be a primary base class. Go up the inheritance hierarchy
7868 until we find the most derived class of which we are a primary base:
7869 it is the BINFO_VIRTUALS there that we need to consider. */
7870 non_primary_binfo = binfo;
7871 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7875 /* If we have reached a virtual base, then it must be vid->vbase,
7876 because we ignore other virtual bases in
7877 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7878 base (possibly multi-level) of vid->binfo, or we wouldn't
7879 have called build_vcall_and_vbase_vtbl_entries for it. But it
7880 might be a lost primary, so just skip down to vid->binfo. */
7881 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7883 if (non_primary_binfo != vid->vbase)
7885 non_primary_binfo = vid->binfo;
7889 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7890 if (get_primary_binfo (b) != non_primary_binfo)
7892 non_primary_binfo = b;
7895 if (vid->ctor_vtbl_p)
7896 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7897 where rtti_binfo is the most derived type. */
7898 non_primary_binfo = get_original_base
7899 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7901 /* Make entries for the rest of the virtuals. */
7902 for (base_virtuals = BINFO_VIRTUALS (binfo),
7903 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7904 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7906 base_virtuals = TREE_CHAIN (base_virtuals),
7907 derived_virtuals = TREE_CHAIN (derived_virtuals),
7908 orig_virtuals = TREE_CHAIN (orig_virtuals))
7917 /* Find the declaration that originally caused this function to
7918 be present in BINFO_TYPE (binfo). */
7919 orig_fn = BV_FN (orig_virtuals);
7921 /* When processing BINFO, we only want to generate vcall slots for
7922 function slots introduced in BINFO. So don't try to generate
7923 one if the function isn't even defined in BINFO. */
7924 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7927 /* Find the overriding function. */
7928 fn = BV_FN (derived_virtuals);
7930 /* If there is already an entry for a function with the same
7931 signature as FN, then we do not need a second vcall offset.
7932 Check the list of functions already present in the derived
7934 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7938 derived_entry = VARRAY_TREE (vid->fns, i);
7939 if (same_signature_p (BV_FN (derived_entry), fn)
7940 /* We only use one vcall offset for virtual destructors,
7941 even though there are two virtual table entries. */
7942 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7943 && DECL_DESTRUCTOR_P (fn)))
7945 if (!vid->ctor_vtbl_p)
7946 BV_VCALL_INDEX (derived_virtuals)
7947 = BV_VCALL_INDEX (derived_entry);
7951 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7954 /* The FN comes from BASE. So, we must calculate the adjustment from
7955 vid->vbase to BASE. We can just look for BASE in the complete
7956 object because we are converting from a virtual base, so if there
7957 were multiple copies, there would not be a unique final overrider
7958 and vid->derived would be ill-formed. */
7959 base = DECL_CONTEXT (fn);
7960 base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7962 /* Compute the vcall offset. */
7963 /* As mentioned above, the vbase we're working on is a primary base of
7964 vid->binfo. But it might be a lost primary, so its BINFO_OFFSET
7965 might be wrong, so we just use the BINFO_OFFSET from vid->binfo. */
7966 vcall_offset = BINFO_OFFSET (vid->binfo);
7967 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7969 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7972 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7973 vid->last_init = &TREE_CHAIN (*vid->last_init);
7975 /* Keep track of the vtable index where this vcall offset can be
7976 found. For a construction vtable, we already made this
7977 annotation when we built the original vtable. */
7978 if (!vid->ctor_vtbl_p)
7979 BV_VCALL_INDEX (derived_virtuals) = vid->index;
7981 /* The next vcall offset will be found at a more negative
7983 vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7985 /* Keep track of this function. */
7986 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7990 /* Return vtbl initializers for the RTTI entries coresponding to the
7991 BINFO's vtable. The RTTI entries should indicate the object given
7992 by VID->rtti_binfo. */
7995 build_rtti_vtbl_entries (binfo, vid)
7997 vtbl_init_data *vid;
8006 basetype = BINFO_TYPE (binfo);
8007 t = BINFO_TYPE (vid->rtti_binfo);
8009 /* To find the complete object, we will first convert to our most
8010 primary base, and then add the offset in the vtbl to that value. */
8012 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8013 && !BINFO_LOST_PRIMARY_P (b))
8017 primary_base = get_primary_binfo (b);
8018 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8021 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8023 /* The second entry is the address of the typeinfo object. */
8025 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8027 decl = integer_zero_node;
8029 /* Convert the declaration to a type that can be stored in the
8031 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8032 TREE_CONSTANT (init) = 1;
8033 *vid->last_init = build_tree_list (NULL_TREE, init);
8034 vid->last_init = &TREE_CHAIN (*vid->last_init);
8036 /* Add the offset-to-top entry. It comes earlier in the vtable that
8037 the the typeinfo entry. Convert the offset to look like a
8038 function pointer, so that we can put it in the vtable. */
8039 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8040 TREE_CONSTANT (init) = 1;
8041 *vid->last_init = build_tree_list (NULL_TREE, init);
8042 vid->last_init = &TREE_CHAIN (*vid->last_init);
8045 /* Build an entry in the virtual function table. DELTA is the offset
8046 for the `this' pointer. VCALL_INDEX is the vtable index containing
8047 the vcall offset; NULL_TREE if none. ENTRY is the virtual function
8048 table entry itself. It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8049 but it may not actually be a virtual function table pointer. (For
8050 example, it might be the address of the RTTI object, under the new
8054 build_vtable_entry (delta, vcall_index, entry)
8059 tree fn = TREE_OPERAND (entry, 0);
8061 if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8062 && fn != abort_fndecl)
8064 entry = make_thunk (entry, delta, vcall_index);
8065 entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8066 TREE_READONLY (entry) = 1;
8067 TREE_CONSTANT (entry) = 1;
8069 #ifdef GATHER_STATISTICS
8070 n_vtable_entries += 1;