1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GCC.
8 GCC 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 GCC 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 GCC; 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. */
28 #include "coretypes.h"
39 /* The number of nested classes being processed. If we are not in the
40 scope of any class, this is zero. */
42 int current_class_depth;
44 /* In order to deal with nested classes, we keep a stack of classes.
45 The topmost entry is the innermost class, and is the entry at index
46 CURRENT_CLASS_DEPTH */
48 typedef struct class_stack_node {
49 /* The name of the class. */
52 /* The _TYPE node for the class. */
55 /* The access specifier pending for new declarations in the scope of
59 /* If were defining TYPE, the names used in this class. */
60 splay_tree names_used;
61 }* class_stack_node_t;
63 typedef struct vtbl_init_data_s
65 /* The base for which we're building initializers. */
67 /* The type of the most-derived type. */
69 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
70 unless ctor_vtbl_p is true. */
72 /* The negative-index vtable initializers built up so far. These
73 are in order from least negative index to most negative index. */
75 /* The last (i.e., most negative) entry in INITS. */
77 /* The binfo for the virtual base for which we're building
78 vcall offset initializers. */
80 /* The functions in vbase for which we have already provided vcall
83 /* The vtable index of the next vcall or vbase offset. */
85 /* Nonzero if we are building the initializer for the primary
88 /* Nonzero if we are building the initializer for a construction
91 /* True when adding vcall offset entries to the vtable. False when
92 merely computing the indices. */
93 bool generate_vcall_entries;
96 /* The type of a function passed to walk_subobject_offsets. */
97 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
99 /* The stack itself. This is a dynamically resized array. The
100 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
101 static int current_class_stack_size;
102 static class_stack_node_t current_class_stack;
104 /* An array of all local classes present in this translation unit, in
105 declaration order. */
106 varray_type local_classes;
108 static tree get_vfield_name (tree);
109 static void finish_struct_anon (tree);
110 static tree get_vtable_name (tree);
111 static tree get_basefndecls (tree, tree);
112 static int build_primary_vtable (tree, tree);
113 static int build_secondary_vtable (tree, tree);
114 static void finish_vtbls (tree);
115 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
116 static tree delete_duplicate_fields_1 (tree, tree);
117 static void delete_duplicate_fields (tree);
118 static void finish_struct_bits (tree);
119 static int alter_access (tree, tree, tree);
120 static void handle_using_decl (tree, tree);
121 static void check_for_override (tree, tree);
122 static tree dfs_modify_vtables (tree, void *);
123 static tree modify_all_vtables (tree, tree);
124 static void determine_primary_base (tree);
125 static void finish_struct_methods (tree);
126 static void maybe_warn_about_overly_private_class (tree);
127 static int field_decl_cmp (const void *, const void *);
128 static int resort_field_decl_cmp (const void *, const void *);
129 static int method_name_cmp (const void *, const void *);
130 static int resort_method_name_cmp (const void *, const void *);
131 static void add_implicitly_declared_members (tree, int, int, int);
132 static tree fixed_type_or_null (tree, int *, int *);
133 static tree resolve_address_of_overloaded_function (tree, tree, int,
135 static tree build_vtable_entry_ref (tree, tree, tree);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
138 static int count_fields (tree);
139 static int add_fields_to_vec (tree, tree, int);
140 static void check_bitfield_decl (tree);
141 static void check_field_decl (tree, tree, int *, int *, int *, int *);
142 static void check_field_decls (tree, tree *, int *, int *, int *);
143 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
144 static void build_base_fields (record_layout_info, splay_tree, tree *);
145 static void check_methods (tree);
146 static void remove_zero_width_bit_fields (tree);
147 static void check_bases (tree, int *, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void fixup_pending_inline (tree);
153 static void fixup_inline_methods (tree);
154 static void set_primary_base (tree, tree);
155 static void propagate_binfo_offsets (tree, tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static tree dfs_set_offset_for_unshared_vbases (tree, void *);
158 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
161 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset (tree, tree, vtbl_init_data *);
163 static void layout_vtable_decl (tree, int);
164 static tree dfs_find_final_overrider (tree, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static int maybe_indent_hierarchy (FILE *, int, int);
168 static void dump_class_hierarchy_r (FILE *, int, tree, tree, int);
169 static void dump_class_hierarchy (tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static tree build_vtable (tree, tree, tree);
174 static void initialize_vtable (tree, tree);
175 static void initialize_array (tree, tree);
176 static void layout_nonempty_base_or_field (record_layout_info,
177 tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (tree, tree, splay_tree, tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
181 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
183 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
184 static void build_vcall_and_vbase_vtbl_entries (tree,
186 static void force_canonical_binfo_r (tree, tree, tree, tree);
187 static void force_canonical_binfo (tree, tree, tree, tree);
188 static tree dfs_unshared_virtual_bases (tree, void *);
189 static void mark_primary_bases (tree);
190 static tree mark_primary_virtual_base (tree, tree);
191 static void clone_constructors_and_destructors (tree);
192 static tree build_clone (tree, tree);
193 static void update_vtable_entry_for_fn (tree, tree, tree, tree *);
194 static tree copy_virtuals (tree);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static tree *build_vtt_inits (tree, tree, tree *, tree *);
199 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
200 static tree dfs_ctor_vtable_bases_queue_p (tree, void *data);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static tree get_original_base (tree, tree);
203 static tree dfs_get_primary_binfo (tree, void*);
204 static int record_subobject_offset (tree, tree, splay_tree);
205 static int check_subobject_offset (tree, tree, splay_tree);
206 static int walk_subobject_offsets (tree, subobject_offset_fn,
207 tree, splay_tree, tree, int);
208 static void record_subobject_offsets (tree, tree, splay_tree, int);
209 static int layout_conflict_p (tree, tree, splay_tree, int);
210 static int splay_tree_compare_integer_csts (splay_tree_key k1,
212 static void warn_about_ambiguous_bases (tree);
213 static bool type_requires_array_cookie (tree);
214 static bool contains_empty_class_p (tree);
215 static tree dfs_base_derived_from (tree, void *);
216 static bool base_derived_from (tree, tree);
217 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
218 static tree end_of_base (tree);
219 static tree get_vcall_index (tree, tree);
221 /* Macros for dfs walking during vtt construction. See
222 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
223 and dfs_fixup_binfo_vtbls. */
224 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
225 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
227 /* Variables shared between class.c and call.c. */
229 #ifdef GATHER_STATISTICS
231 int n_vtable_entries = 0;
232 int n_vtable_searches = 0;
233 int n_vtable_elems = 0;
234 int n_convert_harshness = 0;
235 int n_compute_conversion_costs = 0;
236 int n_build_method_call = 0;
237 int n_inner_fields_searched = 0;
240 /* Convert to or from a base subobject. EXPR is an expression of type
241 `A' or `A*', an expression of type `B' or `B*' is returned. To
242 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
243 the B base instance within A. To convert base A to derived B, CODE
244 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
245 In this latter case, A must not be a morally virtual base of B.
246 NONNULL is true if EXPR is known to be non-NULL (this is only
247 needed when EXPR is of pointer type). CV qualifiers are preserved
251 build_base_path (enum tree_code code,
256 tree v_binfo = NULL_TREE;
257 tree d_binfo = NULL_TREE;
261 tree null_test = NULL;
262 tree ptr_target_type;
264 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
266 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267 return error_mark_node;
269 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
272 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
276 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
278 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
280 my_friendly_assert (code == MINUS_EXPR
281 ? same_type_p (BINFO_TYPE (binfo), probe)
283 ? same_type_p (BINFO_TYPE (d_binfo), probe)
286 if (code == MINUS_EXPR && v_binfo)
288 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
289 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
290 return error_mark_node;
294 /* This must happen before the call to save_expr. */
295 expr = build_unary_op (ADDR_EXPR, expr, 0);
297 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
298 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
299 expr = save_expr (expr);
301 if (want_pointer && !nonnull)
302 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
304 offset = BINFO_OFFSET (binfo);
306 if (v_binfo && fixed_type_p <= 0)
308 /* Going via virtual base V_BINFO. We need the static offset
309 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
310 V_BINFO. That offset is an entry in D_BINFO's vtable. */
311 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
312 TREE_TYPE (TREE_TYPE (expr)));
314 v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
316 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
317 v_offset, BINFO_VPTR_FIELD (v_binfo));
318 v_offset = build1 (NOP_EXPR,
319 build_pointer_type (ptrdiff_type_node),
321 v_offset = build_indirect_ref (v_offset, NULL);
322 TREE_CONSTANT (v_offset) = 1;
324 offset = cp_convert (ptrdiff_type_node,
325 size_diffop (offset, BINFO_OFFSET (v_binfo)));
327 if (!integer_zerop (offset))
328 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
330 if (fixed_type_p < 0)
331 /* Negative fixed_type_p means this is a constructor or destructor;
332 virtual base layout is fixed in in-charge [cd]tors, but not in
334 offset = build (COND_EXPR, ptrdiff_type_node,
335 build (EQ_EXPR, boolean_type_node,
336 current_in_charge_parm, integer_zero_node),
338 BINFO_OFFSET (binfo));
343 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
345 target_type = cp_build_qualified_type
346 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
347 ptr_target_type = build_pointer_type (target_type);
349 target_type = ptr_target_type;
351 expr = build1 (NOP_EXPR, ptr_target_type, expr);
353 if (!integer_zerop (offset))
354 expr = build (code, ptr_target_type, expr, offset);
359 expr = build_indirect_ref (expr, NULL);
362 expr = build (COND_EXPR, target_type, null_test,
363 build1 (NOP_EXPR, target_type, integer_zero_node),
369 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
370 message is emitted if TYPE is inaccessible. OBJECT is assumed to
374 convert_to_base (tree object, tree type, bool check_access)
378 binfo = lookup_base (TREE_TYPE (object), type,
379 check_access ? ba_check : ba_ignore,
381 if (!binfo || binfo == error_mark_node)
382 return error_mark_node;
384 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
388 /* Virtual function things. */
391 build_vtable_entry_ref (tree array_ref, tree instance, tree idx)
393 tree i, i2, vtable, first_fn, basetype;
395 basetype = TREE_TYPE (instance);
396 if (TREE_CODE (basetype) == REFERENCE_TYPE)
397 basetype = TREE_TYPE (basetype);
399 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
400 first_fn = TYPE_BINFO_VTABLE (basetype);
402 i = fold (build_array_ref (first_fn, idx));
403 i = fold (build_c_cast (ptrdiff_type_node,
404 build_unary_op (ADDR_EXPR, i, 0)));
405 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
406 i2 = fold (build_c_cast (ptrdiff_type_node,
407 build_unary_op (ADDR_EXPR, i2, 0)));
408 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
410 if (TREE_CODE (i) != INTEGER_CST)
413 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
416 /* Given an object INSTANCE, return an expression which yields the
417 vtable element corresponding to INDEX. There are many special
418 cases for INSTANCE which we take care of here, mainly to avoid
419 creating extra tree nodes when we don't have to. */
422 build_vtbl_ref_1 (tree instance, tree idx)
425 tree vtbl = NULL_TREE;
427 /* Try to figure out what a reference refers to, and
428 access its virtual function table directly. */
431 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
433 tree basetype = TREE_TYPE (instance);
434 if (TREE_CODE (basetype) == REFERENCE_TYPE)
435 basetype = TREE_TYPE (basetype);
437 if (fixed_type && !cdtorp)
439 tree binfo = lookup_base (fixed_type, basetype,
440 ba_ignore|ba_quiet, NULL);
442 vtbl = BINFO_VTABLE (binfo);
447 vtbl = build_vfield_ref (instance, basetype);
450 assemble_external (vtbl);
452 aref = build_array_ref (vtbl, idx);
453 TREE_CONSTANT (aref) = 1;
459 build_vtbl_ref (tree instance, tree idx)
461 tree aref = build_vtbl_ref_1 (instance, idx);
464 aref = build_vtable_entry_ref (aref, instance, idx);
469 /* Given an object INSTANCE, return an expression which yields a
470 function pointer corresponding to vtable element INDEX. */
473 build_vfn_ref (tree instance, tree idx)
475 tree aref = build_vtbl_ref_1 (instance, idx);
477 /* When using function descriptors, the address of the
478 vtable entry is treated as a function pointer. */
479 if (TARGET_VTABLE_USES_DESCRIPTORS)
480 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
481 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
484 aref = build_vtable_entry_ref (aref, instance, idx);
489 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
490 for the given TYPE. */
493 get_vtable_name (tree type)
495 return mangle_vtbl_for_type (type);
498 /* Return an IDENTIFIER_NODE for the name of the virtual table table
502 get_vtt_name (tree type)
504 return mangle_vtt_for_type (type);
507 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
508 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
509 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
512 build_vtable (tree class_type, tree name, tree vtable_type)
516 decl = build_lang_decl (VAR_DECL, name, vtable_type);
517 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
518 now to avoid confusion in mangle_decl. */
519 SET_DECL_ASSEMBLER_NAME (decl, name);
520 DECL_CONTEXT (decl) = class_type;
521 DECL_ARTIFICIAL (decl) = 1;
522 TREE_STATIC (decl) = 1;
523 TREE_READONLY (decl) = 1;
524 DECL_VIRTUAL_P (decl) = 1;
525 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
527 import_export_vtable (decl, class_type, 0);
532 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
533 or even complete. If this does not exist, create it. If COMPLETE is
534 nonzero, then complete the definition of it -- that will render it
535 impossible to actually build the vtable, but is useful to get at those
536 which are known to exist in the runtime. */
539 get_vtable_decl (tree type, int complete)
543 if (CLASSTYPE_VTABLES (type))
544 return CLASSTYPE_VTABLES (type);
546 decl = build_vtable (type, get_vtable_name (type), void_type_node);
547 CLASSTYPE_VTABLES (type) = decl;
549 /* At one time the vtable info was grabbed 2 words at a time. This
550 fails on sparc unless you have 8-byte alignment. (tiemann) */
551 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
556 DECL_EXTERNAL (decl) = 1;
557 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
563 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
564 BV_VCALL_INDEX for each entry is cleared. */
567 copy_virtuals (tree binfo)
572 copies = copy_list (BINFO_VIRTUALS (binfo));
573 for (t = copies; t; t = TREE_CHAIN (t))
574 BV_VCALL_INDEX (t) = NULL_TREE;
579 /* Build the primary virtual function table for TYPE. If BINFO is
580 non-NULL, build the vtable starting with the initial approximation
581 that it is the same as the one which is the head of the association
582 list. Returns a nonzero value if a new vtable is actually
586 build_primary_vtable (tree binfo, tree type)
591 decl = get_vtable_decl (type, /*complete=*/0);
595 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
596 /* We have already created a vtable for this base, so there's
597 no need to do it again. */
600 virtuals = copy_virtuals (binfo);
601 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
602 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
603 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
607 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
609 virtuals = NULL_TREE;
612 #ifdef GATHER_STATISTICS
614 n_vtable_elems += list_length (virtuals);
617 /* Initialize the association list for this type, based
618 on our first approximation. */
619 TYPE_BINFO_VTABLE (type) = decl;
620 TYPE_BINFO_VIRTUALS (type) = virtuals;
621 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
625 /* Give BINFO a new virtual function table which is initialized
626 with a skeleton-copy of its original initialization. The only
627 entry that changes is the `delta' entry, so we can really
628 share a lot of structure.
630 FOR_TYPE is the most derived type which caused this table to
633 Returns nonzero if we haven't met BINFO before.
635 The order in which vtables are built (by calling this function) for
636 an object must remain the same, otherwise a binary incompatibility
640 build_secondary_vtable (tree binfo, tree for_type)
642 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
644 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
645 /* We already created a vtable for this base. There's no need to
649 /* Remember that we've created a vtable for this BINFO, so that we
650 don't try to do so again. */
651 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
653 /* Make fresh virtual list, so we can smash it later. */
654 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
656 /* Secondary vtables are laid out as part of the same structure as
657 the primary vtable. */
658 BINFO_VTABLE (binfo) = NULL_TREE;
662 /* Create a new vtable for BINFO which is the hierarchy dominated by
663 T. Return nonzero if we actually created a new vtable. */
666 make_new_vtable (tree t, tree binfo)
668 if (binfo == TYPE_BINFO (t))
669 /* In this case, it is *type*'s vtable we are modifying. We start
670 with the approximation that its vtable is that of the
671 immediate base class. */
672 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
673 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
674 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
677 /* This is our very own copy of `basetype' to play with. Later,
678 we will fill in all the virtual functions that override the
679 virtual functions in these base classes which are not defined
680 by the current type. */
681 return build_secondary_vtable (binfo, t);
684 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
685 (which is in the hierarchy dominated by T) list FNDECL as its
686 BV_FN. DELTA is the required constant adjustment from the `this'
687 pointer where the vtable entry appears to the `this' required when
688 the function is actually called. */
691 modify_vtable_entry (tree t,
701 if (fndecl != BV_FN (v)
702 || !tree_int_cst_equal (delta, BV_DELTA (v)))
704 /* We need a new vtable for BINFO. */
705 if (make_new_vtable (t, binfo))
707 /* If we really did make a new vtable, we also made a copy
708 of the BINFO_VIRTUALS list. Now, we have to find the
709 corresponding entry in that list. */
710 *virtuals = BINFO_VIRTUALS (binfo);
711 while (BV_FN (*virtuals) != BV_FN (v))
712 *virtuals = TREE_CHAIN (*virtuals);
716 BV_DELTA (v) = delta;
717 BV_VCALL_INDEX (v) = NULL_TREE;
723 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
724 the method after the class has already been defined because a
725 declaration for it was seen. (Even though that is erroneous, we
726 add the method for improved error recovery.) */
729 add_method (tree type, tree method, int error_p)
731 int using = (DECL_CONTEXT (method) != type);
735 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
736 && DECL_TEMPLATE_CONV_FN_P (method));
738 if (!CLASSTYPE_METHOD_VEC (type))
739 /* Make a new method vector. We start with 8 entries. We must
740 allocate at least two (for constructors and destructors), and
741 we're going to end up with an assignment operator at some point
744 We could use a TREE_LIST for now, and convert it to a TREE_VEC
745 in finish_struct, but we would probably waste more memory
746 making the links in the list than we would by over-allocating
747 the size of the vector here. Furthermore, we would complicate
748 all the code that expects this to be a vector. */
749 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
751 method_vec = CLASSTYPE_METHOD_VEC (type);
752 len = TREE_VEC_LENGTH (method_vec);
754 /* Constructors and destructors go in special slots. */
755 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
756 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
757 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
758 slot = CLASSTYPE_DESTRUCTOR_SLOT;
761 int have_template_convs_p = 0;
763 /* See if we already have an entry with this name. */
764 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
766 tree m = TREE_VEC_ELT (method_vec, slot);
774 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
775 && DECL_TEMPLATE_CONV_FN_P (m));
777 /* If we need to move things up, see if there's
779 if (!have_template_convs_p)
782 if (TREE_VEC_ELT (method_vec, slot))
787 if (DECL_NAME (m) == DECL_NAME (method))
793 /* We need a bigger method vector. */
797 /* In the non-error case, we are processing a class
798 definition. Double the size of the vector to give room
802 /* In the error case, the vector is already complete. We
803 don't expect many errors, and the rest of the front-end
804 will get confused if there are empty slots in the vector. */
808 new_vec = make_tree_vec (new_len);
809 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
810 len * sizeof (tree));
812 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
815 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
817 /* Type conversion operators have to come before ordinary
818 methods; add_conversions depends on this to speed up
819 looking for conversion operators. So, if necessary, we
820 slide some of the vector elements up. In theory, this
821 makes this algorithm O(N^2) but we don't expect many
822 conversion operators. */
824 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
826 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
828 tree fn = TREE_VEC_ELT (method_vec, slot);
831 /* There are no more entries in the vector, so we
832 can insert the new conversion operator here. */
835 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
836 /* We can insert the new function right at the
841 if (template_conv_p && have_template_convs_p)
843 else if (!TREE_VEC_ELT (method_vec, slot))
844 /* There is nothing in the Ith slot, so we can avoid
849 /* We know the last slot in the vector is empty
850 because we know that at this point there's room
851 for a new function. */
852 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
853 &TREE_VEC_ELT (method_vec, slot),
854 (len - slot - 1) * sizeof (tree));
855 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
860 if (template_class_depth (type))
861 /* TYPE is a template class. Don't issue any errors now; wait
862 until instantiation time to complain. */
868 /* Check to see if we've already got this method. */
869 for (fns = TREE_VEC_ELT (method_vec, slot);
871 fns = OVL_NEXT (fns))
873 tree fn = OVL_CURRENT (fns);
878 if (TREE_CODE (fn) != TREE_CODE (method))
881 /* [over.load] Member function declarations with the
882 same name and the same parameter types cannot be
883 overloaded if any of them is a static member
884 function declaration.
886 [namespace.udecl] When a using-declaration brings names
887 from a base class into a derived class scope, member
888 functions in the derived class override and/or hide member
889 functions with the same name and parameter types in a base
890 class (rather than conflicting). */
891 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
892 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
894 /* Compare the quals on the 'this' parm. Don't compare
895 the whole types, as used functions are treated as
896 coming from the using class in overload resolution. */
897 if (! DECL_STATIC_FUNCTION_P (fn)
898 && ! DECL_STATIC_FUNCTION_P (method)
899 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
900 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
903 /* For templates, the template parms must be identical. */
904 if (TREE_CODE (fn) == TEMPLATE_DECL
905 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
906 DECL_TEMPLATE_PARMS (method)))
909 if (! DECL_STATIC_FUNCTION_P (fn))
910 parms1 = TREE_CHAIN (parms1);
911 if (! DECL_STATIC_FUNCTION_P (method))
912 parms2 = TREE_CHAIN (parms2);
914 if (same && compparms (parms1, parms2)
915 && (!DECL_CONV_FN_P (fn)
916 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
917 TREE_TYPE (TREE_TYPE (method)))))
919 if (using && DECL_CONTEXT (fn) == type)
920 /* Defer to the local function. */
924 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
927 /* We don't call duplicate_decls here to merge
928 the declarations because that will confuse
929 things if the methods have inline
930 definitions. In particular, we will crash
931 while processing the definitions. */
938 /* Actually insert the new method. */
939 TREE_VEC_ELT (method_vec, slot)
940 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
942 /* Add the new binding. */
943 if (!DECL_CONSTRUCTOR_P (method)
944 && !DECL_DESTRUCTOR_P (method))
945 push_class_level_binding (DECL_NAME (method),
946 TREE_VEC_ELT (method_vec, slot));
949 /* Subroutines of finish_struct. */
951 /* Look through the list of fields for this struct, deleting
952 duplicates as we go. This must be recursive to handle
955 FIELD is the field which may not appear anywhere in FIELDS.
956 FIELD_PTR, if non-null, is the starting point at which
957 chained deletions may take place.
958 The value returned is the first acceptable entry found
961 Note that anonymous fields which are not of UNION_TYPE are
962 not duplicates, they are just anonymous fields. This happens
963 when we have unnamed bitfields, for example. */
966 delete_duplicate_fields_1 (tree field, tree fields)
970 if (DECL_NAME (field) == 0)
972 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
975 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
976 fields = delete_duplicate_fields_1 (x, fields);
981 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
983 if (DECL_NAME (x) == 0)
985 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
987 TYPE_FIELDS (TREE_TYPE (x))
988 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
989 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
992 fields = TREE_CHAIN (fields);
994 TREE_CHAIN (prev) = TREE_CHAIN (x);
997 else if (TREE_CODE (field) == USING_DECL)
998 /* A using declaration is allowed to appear more than
999 once. We'll prune these from the field list later, and
1000 handle_using_decl will complain about invalid multiple
1003 else if (DECL_NAME (field) == DECL_NAME (x))
1005 if (TREE_CODE (field) == CONST_DECL
1006 && TREE_CODE (x) == CONST_DECL)
1007 cp_error_at ("duplicate enum value `%D'", x);
1008 else if (TREE_CODE (field) == CONST_DECL
1009 || TREE_CODE (x) == CONST_DECL)
1010 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1012 else if (DECL_DECLARES_TYPE_P (field)
1013 && DECL_DECLARES_TYPE_P (x))
1015 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1017 cp_error_at ("duplicate nested type `%D'", x);
1019 else if (DECL_DECLARES_TYPE_P (field)
1020 || DECL_DECLARES_TYPE_P (x))
1022 /* Hide tag decls. */
1023 if ((TREE_CODE (field) == TYPE_DECL
1024 && DECL_ARTIFICIAL (field))
1025 || (TREE_CODE (x) == TYPE_DECL
1026 && DECL_ARTIFICIAL (x)))
1028 cp_error_at ("duplicate field `%D' (as type and non-type)",
1032 cp_error_at ("duplicate member `%D'", x);
1034 fields = TREE_CHAIN (fields);
1036 TREE_CHAIN (prev) = TREE_CHAIN (x);
1044 delete_duplicate_fields (tree fields)
1047 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1048 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1051 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1052 legit, otherwise return 0. */
1055 alter_access (tree t, tree fdecl, tree access)
1059 if (!DECL_LANG_SPECIFIC (fdecl))
1060 retrofit_lang_decl (fdecl);
1062 if (DECL_DISCRIMINATOR_P (fdecl))
1065 elem = purpose_member (t, DECL_ACCESS (fdecl));
1068 if (TREE_VALUE (elem) != access)
1070 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1071 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1073 error ("conflicting access specifications for field `%s', ignored",
1074 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1078 /* They're changing the access to the same thing they changed
1079 it to before. That's OK. */
1085 enforce_access (t, fdecl);
1086 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1092 /* Process the USING_DECL, which is a member of T. */
1095 handle_using_decl (tree using_decl, tree t)
1097 tree ctype = DECL_INITIAL (using_decl);
1098 tree name = DECL_NAME (using_decl);
1100 = TREE_PRIVATE (using_decl) ? access_private_node
1101 : TREE_PROTECTED (using_decl) ? access_protected_node
1102 : access_public_node;
1104 tree flist = NULL_TREE;
1107 binfo = lookup_base (t, ctype, ba_any, NULL);
1110 error_not_base_type (t, ctype);
1114 if (constructor_name_p (name, ctype))
1116 cp_error_at ("`%D' names constructor", using_decl);
1119 if (constructor_name_p (name, t))
1121 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1125 fdecl = lookup_member (binfo, name, 0, 0);
1129 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1133 if (BASELINK_P (fdecl))
1134 /* Ignore base type this came from. */
1135 fdecl = BASELINK_FUNCTIONS (fdecl);
1137 old_value = IDENTIFIER_CLASS_VALUE (name);
1140 if (is_overloaded_fn (old_value))
1141 old_value = OVL_CURRENT (old_value);
1143 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1146 old_value = NULL_TREE;
1149 if (is_overloaded_fn (fdecl))
1154 else if (is_overloaded_fn (old_value))
1157 /* It's OK to use functions from a base when there are functions with
1158 the same name already present in the current class. */;
1161 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1162 cp_error_at (" because of local method `%#D' with same name",
1163 OVL_CURRENT (old_value));
1167 else if (!DECL_ARTIFICIAL (old_value))
1169 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1170 cp_error_at (" because of local member `%#D' with same name", old_value);
1174 /* Make type T see field decl FDECL with access ACCESS.*/
1176 for (; flist; flist = OVL_NEXT (flist))
1178 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1179 alter_access (t, OVL_CURRENT (flist), access);
1182 alter_access (t, fdecl, access);
1185 /* Run through the base clases of T, updating
1186 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1187 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1191 check_bases (tree t,
1192 int* cant_have_default_ctor_p,
1193 int* cant_have_const_ctor_p,
1194 int* no_const_asn_ref_p)
1198 int seen_non_virtual_nearly_empty_base_p;
1201 binfos = TYPE_BINFO_BASETYPES (t);
1202 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1203 seen_non_virtual_nearly_empty_base_p = 0;
1205 /* An aggregate cannot have baseclasses. */
1206 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1208 for (i = 0; i < n_baseclasses; ++i)
1213 /* Figure out what base we're looking at. */
1214 base_binfo = TREE_VEC_ELT (binfos, i);
1215 basetype = TREE_TYPE (base_binfo);
1217 /* If the type of basetype is incomplete, then we already
1218 complained about that fact (and we should have fixed it up as
1220 if (!COMPLETE_TYPE_P (basetype))
1223 /* The base type is of incomplete type. It is
1224 probably best to pretend that it does not
1226 if (i == n_baseclasses-1)
1227 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1228 TREE_VEC_LENGTH (binfos) -= 1;
1230 for (j = i; j+1 < n_baseclasses; j++)
1231 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1235 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1236 here because the case of virtual functions but non-virtual
1237 dtor is handled in finish_struct_1. */
1238 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1239 && TYPE_HAS_DESTRUCTOR (basetype))
1240 warning ("base class `%#T' has a non-virtual destructor",
1243 /* If the base class doesn't have copy constructors or
1244 assignment operators that take const references, then the
1245 derived class cannot have such a member automatically
1247 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1248 *cant_have_const_ctor_p = 1;
1249 if (TYPE_HAS_ASSIGN_REF (basetype)
1250 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1251 *no_const_asn_ref_p = 1;
1252 /* Similarly, if the base class doesn't have a default
1253 constructor, then the derived class won't have an
1254 automatically generated default constructor. */
1255 if (TYPE_HAS_CONSTRUCTOR (basetype)
1256 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1258 *cant_have_default_ctor_p = 1;
1259 if (! TYPE_HAS_CONSTRUCTOR (t))
1260 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1264 if (TREE_VIA_VIRTUAL (base_binfo))
1265 /* A virtual base does not effect nearly emptiness. */
1267 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1269 if (seen_non_virtual_nearly_empty_base_p)
1270 /* And if there is more than one nearly empty base, then the
1271 derived class is not nearly empty either. */
1272 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1274 /* Remember we've seen one. */
1275 seen_non_virtual_nearly_empty_base_p = 1;
1277 else if (!is_empty_class (basetype))
1278 /* If the base class is not empty or nearly empty, then this
1279 class cannot be nearly empty. */
1280 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1282 /* A lot of properties from the bases also apply to the derived
1284 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1285 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1286 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1287 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1288 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1289 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1290 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1291 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1292 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1293 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1294 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1295 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1299 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1300 TO. Move primary information from FROM to TO, and recursively traverse
1301 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1302 assoc list of binfos that have already been reseated. */
1305 force_canonical_binfo_r (tree to, tree from, tree type, tree mappings)
1307 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1309 my_friendly_assert (to != from, 20010905);
1310 BINFO_INDIRECT_PRIMARY_P (to)
1311 = BINFO_INDIRECT_PRIMARY_P (from);
1312 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1313 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1314 BINFO_UNSHARED_MARKED (from) = 0;
1315 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1316 BINFO_LOST_PRIMARY_P (from) = 0;
1317 if (BINFO_PRIMARY_P (from))
1319 tree primary = BINFO_PRIMARY_BASE_OF (from);
1322 /* We might have just moved the primary base too, see if it's on our
1324 assoc = purpose_member (primary, mappings);
1326 primary = TREE_VALUE (assoc);
1327 BINFO_PRIMARY_BASE_OF (to) = primary;
1328 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1330 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1332 mappings = tree_cons (from, to, mappings);
1334 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1335 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1337 tree from_primary = get_primary_binfo (from);
1339 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1340 force_canonical_binfo (get_primary_binfo (to), from_primary,
1344 for (i = 0; i != n_baseclasses; i++)
1346 tree from_binfo = BINFO_BASETYPE (from, i);
1347 tree to_binfo = BINFO_BASETYPE (to, i);
1349 if (TREE_VIA_VIRTUAL (from_binfo))
1351 if (BINFO_PRIMARY_P (from_binfo) &&
1352 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1353 /* This base is a primary of some binfo we have already
1354 reseated. We must reseat this one too. */
1355 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1358 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1362 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1363 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1364 MAPPINGS is an assoc list of binfos that have already been reseated.
1365 Adjust any non-virtual bases within FROM, and also move any virtual bases
1366 which are canonical. This complication arises because selecting primary
1367 bases walks in inheritance graph order, but we don't share binfos for
1368 virtual bases, hence we can fill in the primaries for a virtual base,
1369 and then discover that a later base requires the virtual as its
1373 force_canonical_binfo (tree to, tree from, tree type, tree mappings)
1375 tree assoc = purpose_member (BINFO_TYPE (to),
1376 CLASSTYPE_VBASECLASSES (type));
1377 if (TREE_VALUE (assoc) != to)
1379 TREE_VALUE (assoc) = to;
1380 force_canonical_binfo_r (to, from, type, mappings);
1384 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1385 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1386 otherwise (because something else has already made it primary). */
1389 mark_primary_virtual_base (tree base_binfo, tree type)
1391 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1393 if (BINFO_PRIMARY_P (shared_binfo))
1395 /* It's already allocated in the hierarchy. BINFO won't have a
1396 primary base in this hierarchy, even though the complete object
1397 BINFO is for, would do. */
1401 /* We need to make sure that the assoc list
1402 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1403 primary BINFO for the virtual base, as this is the one
1404 that'll really exist. */
1405 if (base_binfo != shared_binfo)
1406 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1411 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1412 base, then BINFO has no primary base in this graph. Called from
1413 mark_primary_bases. DATA is the most derived type. */
1416 dfs_unshared_virtual_bases (tree binfo, void* data)
1418 tree t = (tree) data;
1420 if (!BINFO_UNSHARED_MARKED (binfo)
1421 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1423 /* This morally virtual base has a primary base when it
1424 is a complete object. We need to locate the shared instance
1425 of this binfo in the type dominated by T. We duplicate the
1426 primary base information from there to here. */
1430 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1431 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1433 unshared_base = get_original_base (binfo,
1434 binfo_for_vbase (BINFO_TYPE (vbase),
1436 my_friendly_assert (unshared_base != binfo, 20010612);
1437 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1438 if (!BINFO_LOST_PRIMARY_P (binfo))
1439 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1442 if (binfo != TYPE_BINFO (t))
1443 /* The vtable fields will have been copied when duplicating the
1444 base binfos. That information is bogus, make sure we don't try
1446 BINFO_VTABLE (binfo) = NULL_TREE;
1448 /* If this is a virtual primary base, make sure its offset matches
1449 that which it is primary for. */
1450 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1451 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1453 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1454 BINFO_OFFSET (binfo));
1455 if (!integer_zerop (delta))
1456 propagate_binfo_offsets (binfo, delta, t);
1459 BINFO_UNSHARED_MARKED (binfo) = 0;
1463 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1464 dominated by TYPE that are primary bases. */
1467 mark_primary_bases (tree type)
1471 /* Walk the bases in inheritance graph order. */
1472 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1476 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1477 /* Not a dynamic base. */
1480 base_binfo = get_primary_binfo (binfo);
1482 if (TREE_VIA_VIRTUAL (base_binfo))
1483 base_binfo = mark_primary_virtual_base (base_binfo, type);
1486 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1488 BINFO_LOST_PRIMARY_P (binfo) = 1;
1490 BINFO_UNSHARED_MARKED (binfo) = 1;
1492 /* There could remain unshared morally virtual bases which were not
1493 visited in the inheritance graph walk. These bases will have lost
1494 their virtual primary base (should they have one). We must now
1495 find them. Also we must fix up the BINFO_OFFSETs of primary
1496 virtual bases. We could not do that as we went along, as they
1497 were originally copied from the bases we inherited from by
1498 unshare_base_binfos. That may have decided differently about
1499 where a virtual primary base went. */
1500 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1503 /* Make the BINFO the primary base of T. */
1506 set_primary_base (tree t, tree binfo)
1510 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1511 basetype = BINFO_TYPE (binfo);
1512 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1513 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1514 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1517 /* Determine the primary class for T. */
1520 determine_primary_base (tree t)
1522 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1526 /* If there are no baseclasses, there is certainly no primary base. */
1527 if (n_baseclasses == 0)
1530 type_binfo = TYPE_BINFO (t);
1532 for (i = 0; i < n_baseclasses; i++)
1534 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1535 tree basetype = BINFO_TYPE (base_binfo);
1537 if (TYPE_CONTAINS_VPTR_P (basetype))
1539 /* We prefer a non-virtual base, although a virtual one will
1541 if (TREE_VIA_VIRTUAL (base_binfo))
1544 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1546 set_primary_base (t, base_binfo);
1547 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1553 /* Only add unique vfields, and flatten them out as we go. */
1554 for (vfields = CLASSTYPE_VFIELDS (basetype);
1556 vfields = TREE_CHAIN (vfields))
1557 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1558 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1559 CLASSTYPE_VFIELDS (t)
1560 = tree_cons (base_binfo,
1561 VF_BASETYPE_VALUE (vfields),
1562 CLASSTYPE_VFIELDS (t));
1567 if (!TYPE_VFIELD (t))
1568 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1570 /* Find the indirect primary bases - those virtual bases which are primary
1571 bases of something else in this hierarchy. */
1572 for (vbases = CLASSTYPE_VBASECLASSES (t);
1574 vbases = TREE_CHAIN (vbases))
1576 tree vbase_binfo = TREE_VALUE (vbases);
1578 /* See if this virtual base is an indirect primary base. To be so,
1579 it must be a primary base within the hierarchy of one of our
1581 for (i = 0; i < n_baseclasses; ++i)
1583 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1586 for (v = CLASSTYPE_VBASECLASSES (basetype);
1590 tree base_vbase = TREE_VALUE (v);
1592 if (BINFO_PRIMARY_P (base_vbase)
1593 && same_type_p (BINFO_TYPE (base_vbase),
1594 BINFO_TYPE (vbase_binfo)))
1596 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1601 /* If we've discovered that this virtual base is an indirect
1602 primary base, then we can move on to the next virtual
1604 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1609 /* A "nearly-empty" virtual base class can be the primary base
1610 class, if no non-virtual polymorphic base can be found. */
1611 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1613 /* If not NULL, this is the best primary base candidate we have
1615 tree candidate = NULL_TREE;
1618 /* Loop over the baseclasses. */
1619 for (base_binfo = TYPE_BINFO (t);
1621 base_binfo = TREE_CHAIN (base_binfo))
1623 tree basetype = BINFO_TYPE (base_binfo);
1625 if (TREE_VIA_VIRTUAL (base_binfo)
1626 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1628 /* If this is not an indirect primary base, then it's
1629 definitely our primary base. */
1630 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1632 candidate = base_binfo;
1636 /* If this is an indirect primary base, it still could be
1637 our primary base -- unless we later find there's another
1638 nearly-empty virtual base that isn't an indirect
1641 candidate = base_binfo;
1645 /* If we've got a primary base, use it. */
1648 set_primary_base (t, candidate);
1649 CLASSTYPE_VFIELDS (t)
1650 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1654 /* Mark the primary base classes at this point. */
1655 mark_primary_bases (t);
1658 /* Set memoizing fields and bits of T (and its variants) for later
1662 finish_struct_bits (tree t)
1664 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1666 /* Fix up variants (if any). */
1667 tree variants = TYPE_NEXT_VARIANT (t);
1670 /* These fields are in the _TYPE part of the node, not in
1671 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1672 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1673 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1674 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1675 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1676 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1678 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1679 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1680 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1681 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1682 /* Copy whatever these are holding today. */
1683 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1684 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1685 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1686 TYPE_SIZE (variants) = TYPE_SIZE (t);
1687 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1688 variants = TYPE_NEXT_VARIANT (variants);
1691 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1692 /* For a class w/o baseclasses, `finish_struct' has set
1693 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1694 definition). Similarly for a class whose base classes do not
1695 have vtables. When neither of these is true, we might have
1696 removed abstract virtuals (by providing a definition), added
1697 some (by declaring new ones), or redeclared ones from a base
1698 class. We need to recalculate what's really an abstract virtual
1699 at this point (by looking in the vtables). */
1700 get_pure_virtuals (t);
1704 /* Notice whether this class has type conversion functions defined. */
1705 tree binfo = TYPE_BINFO (t);
1706 tree binfos = BINFO_BASETYPES (binfo);
1709 for (i = n_baseclasses-1; i >= 0; i--)
1711 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1713 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1717 /* If this type has a copy constructor or a destructor, force its mode to
1718 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1719 will cause it to be passed by invisible reference and prevent it from
1720 being returned in a register. */
1721 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1724 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1725 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1727 TYPE_MODE (variants) = BLKmode;
1728 TREE_ADDRESSABLE (variants) = 1;
1733 /* Issue warnings about T having private constructors, but no friends,
1736 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1737 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1738 non-private static member functions. */
1741 maybe_warn_about_overly_private_class (tree t)
1743 int has_member_fn = 0;
1744 int has_nonprivate_method = 0;
1747 if (!warn_ctor_dtor_privacy
1748 /* If the class has friends, those entities might create and
1749 access instances, so we should not warn. */
1750 || (CLASSTYPE_FRIEND_CLASSES (t)
1751 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1752 /* We will have warned when the template was declared; there's
1753 no need to warn on every instantiation. */
1754 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1755 /* There's no reason to even consider warning about this
1759 /* We only issue one warning, if more than one applies, because
1760 otherwise, on code like:
1763 // Oops - forgot `public:'
1769 we warn several times about essentially the same problem. */
1771 /* Check to see if all (non-constructor, non-destructor) member
1772 functions are private. (Since there are no friends or
1773 non-private statics, we can't ever call any of the private member
1775 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1776 /* We're not interested in compiler-generated methods; they don't
1777 provide any way to call private members. */
1778 if (!DECL_ARTIFICIAL (fn))
1780 if (!TREE_PRIVATE (fn))
1782 if (DECL_STATIC_FUNCTION_P (fn))
1783 /* A non-private static member function is just like a
1784 friend; it can create and invoke private member
1785 functions, and be accessed without a class
1789 has_nonprivate_method = 1;
1790 /* Keep searching for a static member function. */
1792 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1796 if (!has_nonprivate_method && has_member_fn)
1798 /* There are no non-private methods, and there's at least one
1799 private member function that isn't a constructor or
1800 destructor. (If all the private members are
1801 constructors/destructors we want to use the code below that
1802 issues error messages specifically referring to
1803 constructors/destructors.) */
1805 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1806 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1807 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1808 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1810 has_nonprivate_method = 1;
1813 if (!has_nonprivate_method)
1815 warning ("all member functions in class `%T' are private", t);
1820 /* Even if some of the member functions are non-private, the class
1821 won't be useful for much if all the constructors or destructors
1822 are private: such an object can never be created or destroyed. */
1823 if (TYPE_HAS_DESTRUCTOR (t))
1825 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1827 if (TREE_PRIVATE (dtor))
1829 warning ("`%#T' only defines a private destructor and has no friends",
1835 if (TYPE_HAS_CONSTRUCTOR (t))
1837 int nonprivate_ctor = 0;
1839 /* If a non-template class does not define a copy
1840 constructor, one is defined for it, enabling it to avoid
1841 this warning. For a template class, this does not
1842 happen, and so we would normally get a warning on:
1844 template <class T> class C { private: C(); };
1846 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1847 complete non-template or fully instantiated classes have this
1849 if (!TYPE_HAS_INIT_REF (t))
1850 nonprivate_ctor = 1;
1852 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1856 tree ctor = OVL_CURRENT (fn);
1857 /* Ideally, we wouldn't count copy constructors (or, in
1858 fact, any constructor that takes an argument of the
1859 class type as a parameter) because such things cannot
1860 be used to construct an instance of the class unless
1861 you already have one. But, for now at least, we're
1863 if (! TREE_PRIVATE (ctor))
1865 nonprivate_ctor = 1;
1870 if (nonprivate_ctor == 0)
1872 warning ("`%#T' only defines private constructors and has no friends",
1879 /* Function to help qsort sort FIELD_DECLs by name order. */
1882 field_decl_cmp (const void* x_p, const void* y_p)
1884 const tree *const x = x_p;
1885 const tree *const y = y_p;
1886 if (DECL_NAME (*x) == DECL_NAME (*y))
1887 /* A nontype is "greater" than a type. */
1888 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1889 if (DECL_NAME (*x) == NULL_TREE)
1891 if (DECL_NAME (*y) == NULL_TREE)
1893 if (DECL_NAME (*x) < DECL_NAME (*y))
1899 gt_pointer_operator new_value;
1903 /* This routine compares two fields like field_decl_cmp but using the
1904 pointer operator in resort_data. */
1907 resort_field_decl_cmp (const void* x_p, const void* y_p)
1909 const tree *const x = x_p;
1910 const tree *const y = y_p;
1912 if (DECL_NAME (*x) == DECL_NAME (*y))
1913 /* A nontype is "greater" than a type. */
1914 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1915 if (DECL_NAME (*x) == NULL_TREE)
1917 if (DECL_NAME (*y) == NULL_TREE)
1920 tree d1 = DECL_NAME (*x);
1921 tree d2 = DECL_NAME (*y);
1922 resort_data.new_value (&d1, resort_data.cookie);
1923 resort_data.new_value (&d2, resort_data.cookie);
1930 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
1933 resort_sorted_fields (void* obj,
1934 void* orig_obj ATTRIBUTE_UNUSED ,
1935 gt_pointer_operator new_value,
1939 resort_data.new_value = new_value;
1940 resort_data.cookie = cookie;
1941 qsort (&TREE_VEC_ELT (sf, 0), TREE_VEC_LENGTH (sf), sizeof (tree),
1942 resort_field_decl_cmp);
1945 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1948 method_name_cmp (const void* m1_p, const void* m2_p)
1950 const tree *const m1 = m1_p;
1951 const tree *const m2 = m2_p;
1953 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1955 if (*m1 == NULL_TREE)
1957 if (*m2 == NULL_TREE)
1959 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1964 /* This routine compares two fields like method_name_cmp but using the
1965 pointer operator in resort_field_decl_data. */
1968 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1970 const tree *const m1 = m1_p;
1971 const tree *const m2 = m2_p;
1972 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1974 if (*m1 == NULL_TREE)
1976 if (*m2 == NULL_TREE)
1979 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1980 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1981 resort_data.new_value (&d1, resort_data.cookie);
1982 resort_data.new_value (&d2, resort_data.cookie);
1989 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1992 resort_type_method_vec (void* obj,
1993 void* orig_obj ATTRIBUTE_UNUSED ,
1994 gt_pointer_operator new_value,
1997 tree method_vec = obj;
1998 int len = TREE_VEC_LENGTH (method_vec);
2001 /* The type conversion ops have to live at the front of the vec, so we
2003 for (slot = 2; slot < len; ++slot)
2005 tree fn = TREE_VEC_ELT (method_vec, slot);
2007 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2012 resort_data.new_value = new_value;
2013 resort_data.cookie = cookie;
2014 qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
2015 resort_method_name_cmp);
2019 /* Warn about duplicate methods in fn_fields. Also compact method
2020 lists so that lookup can be made faster.
2022 Data Structure: List of method lists. The outer list is a
2023 TREE_LIST, whose TREE_PURPOSE field is the field name and the
2024 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
2025 links the entire list of methods for TYPE_METHODS. Friends are
2026 chained in the same way as member functions (? TREE_CHAIN or
2027 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2028 list. That allows them to be quickly deleted, and requires no
2031 Sort methods that are not special (i.e., constructors, destructors,
2032 and type conversion operators) so that we can find them faster in
2036 finish_struct_methods (tree t)
2042 if (!TYPE_METHODS (t))
2044 /* Clear these for safety; perhaps some parsing error could set
2045 these incorrectly. */
2046 TYPE_HAS_CONSTRUCTOR (t) = 0;
2047 TYPE_HAS_DESTRUCTOR (t) = 0;
2048 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2052 method_vec = CLASSTYPE_METHOD_VEC (t);
2053 my_friendly_assert (method_vec != NULL_TREE, 19991215);
2054 len = TREE_VEC_LENGTH (method_vec);
2056 /* First fill in entry 0 with the constructors, entry 1 with destructors,
2057 and the next few with type conversion operators (if any). */
2058 for (fn_fields = TYPE_METHODS (t); fn_fields;
2059 fn_fields = TREE_CHAIN (fn_fields))
2060 /* Clear out this flag. */
2061 DECL_IN_AGGR_P (fn_fields) = 0;
2063 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2064 /* We thought there was a destructor, but there wasn't. Some
2065 parse errors cause this anomalous situation. */
2066 TYPE_HAS_DESTRUCTOR (t) = 0;
2068 /* Issue warnings about private constructors and such. If there are
2069 no methods, then some public defaults are generated. */
2070 maybe_warn_about_overly_private_class (t);
2072 /* Now sort the methods. */
2073 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2075 TREE_VEC_LENGTH (method_vec) = len;
2077 /* The type conversion ops have to live at the front of the vec, so we
2079 for (slot = 2; slot < len; ++slot)
2081 tree fn = TREE_VEC_ELT (method_vec, slot);
2083 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2087 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2091 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2094 duplicate_tag_error (tree t)
2096 error ("redefinition of `%#T'", t);
2097 cp_error_at ("previous definition of `%#T'", t);
2099 /* Pretend we haven't defined this type. */
2101 /* All of the component_decl's were TREE_CHAINed together in the parser.
2102 finish_struct_methods walks these chains and assembles all methods with
2103 the same base name into DECL_CHAINs. Now we don't need the parser chains
2104 anymore, so we unravel them. */
2106 /* This used to be in finish_struct, but it turns out that the
2107 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2109 if (CLASSTYPE_METHOD_VEC (t))
2111 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2112 int i, len = TREE_VEC_LENGTH (method_vec);
2113 for (i = 0; i < len; i++)
2115 tree unchain = TREE_VEC_ELT (method_vec, i);
2116 while (unchain != NULL_TREE)
2118 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2119 unchain = OVL_NEXT (unchain);
2124 if (TYPE_LANG_SPECIFIC (t))
2126 tree binfo = TYPE_BINFO (t);
2127 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2128 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2129 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2130 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2132 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2133 BINFO_BASETYPES(binfo) = NULL_TREE;
2135 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2136 TYPE_BINFO (t) = binfo;
2137 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2138 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2139 TYPE_REDEFINED (t) = 1;
2140 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2141 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2142 CLASSTYPE_DECL_LIST (t) = NULL_TREE;
2144 TYPE_SIZE (t) = NULL_TREE;
2145 TYPE_MODE (t) = VOIDmode;
2146 TYPE_FIELDS (t) = NULL_TREE;
2147 TYPE_METHODS (t) = NULL_TREE;
2148 TYPE_VFIELD (t) = NULL_TREE;
2149 TYPE_CONTEXT (t) = NULL_TREE;
2151 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2152 TYPE_LANG_FLAG_0 (t) = 0;
2153 TYPE_LANG_FLAG_1 (t) = 0;
2154 TYPE_LANG_FLAG_2 (t) = 0;
2155 TYPE_LANG_FLAG_3 (t) = 0;
2156 TYPE_LANG_FLAG_4 (t) = 0;
2157 TYPE_LANG_FLAG_5 (t) = 0;
2158 TYPE_LANG_FLAG_6 (t) = 0;
2159 /* But not this one. */
2160 SET_IS_AGGR_TYPE (t, 1);
2163 /* Make BINFO's vtable have N entries, including RTTI entries,
2164 vbase and vcall offsets, etc. Set its type and call the backend
2168 layout_vtable_decl (tree binfo, int n)
2173 atype = build_cplus_array_type (vtable_entry_type,
2174 build_index_type (size_int (n - 1)));
2175 layout_type (atype);
2177 /* We may have to grow the vtable. */
2178 vtable = get_vtbl_decl_for_binfo (binfo);
2179 if (!same_type_p (TREE_TYPE (vtable), atype))
2181 TREE_TYPE (vtable) = atype;
2182 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2183 layout_decl (vtable, 0);
2185 /* At one time the vtable info was grabbed 2 words at a time. This
2186 fails on SPARC unless you have 8-byte alignment. */
2187 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2188 DECL_ALIGN (vtable));
2192 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2193 have the same signature. */
2196 same_signature_p (tree fndecl, tree base_fndecl)
2198 /* One destructor overrides another if they are the same kind of
2200 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2201 && special_function_p (base_fndecl) == special_function_p (fndecl))
2203 /* But a non-destructor never overrides a destructor, nor vice
2204 versa, nor do different kinds of destructors override
2205 one-another. For example, a complete object destructor does not
2206 override a deleting destructor. */
2207 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2210 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2212 tree types, base_types;
2213 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2214 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2215 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2216 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2217 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2223 /* Called from base_derived_from via dfs_walk. */
2226 dfs_base_derived_from (tree binfo, void *data)
2228 tree base = (tree) data;
2230 if (same_type_p (TREE_TYPE (base), TREE_TYPE (binfo))
2231 && tree_int_cst_equal (BINFO_OFFSET (base), BINFO_OFFSET (binfo)))
2232 return error_mark_node;
2237 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2241 base_derived_from (tree derived, tree base)
2243 return dfs_walk (derived, dfs_base_derived_from, NULL, base) != NULL_TREE;
2246 typedef struct find_final_overrider_data_s {
2247 /* The function for which we are trying to find a final overrider. */
2249 /* The base class in which the function was declared. */
2250 tree declaring_base;
2251 /* The most derived class in the hierarchy. */
2252 tree most_derived_type;
2253 /* The candidate overriders. */
2255 } find_final_overrider_data;
2257 /* Called from find_final_overrider via dfs_walk. */
2260 dfs_find_final_overrider (tree binfo, void* data)
2262 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2264 if (same_type_p (BINFO_TYPE (binfo),
2265 BINFO_TYPE (ffod->declaring_base))
2266 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2267 BINFO_OFFSET (ffod->declaring_base)))
2272 /* We haven't found an overrider yet. */
2274 /* We've found a path to the declaring base. Walk down the path
2275 looking for an overrider for FN. */
2276 path = reverse_path (binfo);
2277 while (!same_type_p (BINFO_TYPE (TREE_VALUE (path)),
2278 ffod->most_derived_type))
2279 path = TREE_CHAIN (path);
2282 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2286 path = TREE_VALUE (path);
2290 path = TREE_CHAIN (path);
2293 /* If we found an overrider, record the overriding function, and
2294 the base from which it came. */
2299 /* Remove any candidates overridden by this new function. */
2300 candidate = &ffod->candidates;
2303 /* If *CANDIDATE overrides METHOD, then METHOD
2304 cannot override anything else on the list. */
2305 if (base_derived_from (TREE_VALUE (*candidate), path))
2307 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2308 if (base_derived_from (path, TREE_VALUE (*candidate)))
2309 *candidate = TREE_CHAIN (*candidate);
2311 candidate = &TREE_CHAIN (*candidate);
2314 /* Add the new function. */
2315 ffod->candidates = tree_cons (method, path, ffod->candidates);
2322 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2323 FN and whose TREE_VALUE is the binfo for the base where the
2324 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2325 DERIVED) is the base object in which FN is declared. */
2328 find_final_overrider (tree derived, tree binfo, tree fn)
2330 find_final_overrider_data ffod;
2332 /* Getting this right is a little tricky. This is valid:
2334 struct S { virtual void f (); };
2335 struct T { virtual void f (); };
2336 struct U : public S, public T { };
2338 even though calling `f' in `U' is ambiguous. But,
2340 struct R { virtual void f(); };
2341 struct S : virtual public R { virtual void f (); };
2342 struct T : virtual public R { virtual void f (); };
2343 struct U : public S, public T { };
2345 is not -- there's no way to decide whether to put `S::f' or
2346 `T::f' in the vtable for `R'.
2348 The solution is to look at all paths to BINFO. If we find
2349 different overriders along any two, then there is a problem. */
2350 if (DECL_THUNK_P (fn))
2351 fn = THUNK_TARGET (fn);
2354 ffod.declaring_base = binfo;
2355 ffod.most_derived_type = BINFO_TYPE (derived);
2356 ffod.candidates = NULL_TREE;
2359 dfs_find_final_overrider,
2363 /* If there was no winner, issue an error message. */
2364 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2366 error ("no unique final overrider for `%D' in `%T'", fn,
2367 BINFO_TYPE (derived));
2368 return error_mark_node;
2371 return ffod.candidates;
2374 /* Return the index of the vcall offset for FN when TYPE is used as a
2378 get_vcall_index (tree fn, tree type)
2382 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2383 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2384 || same_signature_p (fn, TREE_PURPOSE (v)))
2387 /* There should always be an appropriate index. */
2388 my_friendly_assert (v, 20021103);
2390 return TREE_VALUE (v);
2393 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2394 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
2395 corresponding position in the BINFO_VIRTUALS list. */
2398 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals)
2405 tree overrider_fn, overrider_target;
2406 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2407 tree over_return, base_return;
2410 /* Find the nearest primary base (possibly binfo itself) which defines
2411 this function; this is the class the caller will convert to when
2412 calling FN through BINFO. */
2413 for (b = binfo; ; b = get_primary_binfo (b))
2415 my_friendly_assert (b, 20021227);
2416 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2419 /* The nearest definition is from a lost primary. */
2420 if (BINFO_LOST_PRIMARY_P (b))
2425 /* Find the final overrider. */
2426 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2427 if (overrider == error_mark_node)
2429 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2431 /* Check for adjusting covariant return types. */
2432 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2433 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2435 if (POINTER_TYPE_P (over_return)
2436 && TREE_CODE (over_return) == TREE_CODE (base_return)
2437 && CLASS_TYPE_P (TREE_TYPE (over_return))
2438 && CLASS_TYPE_P (TREE_TYPE (base_return)))
2440 /* If FN is a covariant thunk, we must figure out the adjustment
2441 to the final base FN was converting to. As OVERRIDER_TARGET might
2442 also be converting to the return type of FN, we have to
2443 combine the two conversions here. */
2444 tree fixed_offset, virtual_offset;
2446 if (DECL_THUNK_P (fn))
2448 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2449 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2451 virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2452 TREE_TYPE (over_return));
2455 fixed_offset = virtual_offset = NULL_TREE;
2457 if (!virtual_offset)
2459 /* There was no existing virtual thunk (which takes
2464 thunk_binfo = lookup_base (TREE_TYPE (over_return),
2465 TREE_TYPE (base_return),
2466 ba_check | ba_quiet, &kind);
2468 if (thunk_binfo && (kind == bk_via_virtual
2469 || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2471 tree offset = BINFO_OFFSET (thunk_binfo);
2473 if (kind == bk_via_virtual)
2475 /* We convert via virtual base. Find the virtual
2476 base and adjust the fixed offset to be from there. */
2477 while (!TREE_VIA_VIRTUAL (thunk_binfo))
2478 thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2480 virtual_offset = binfo_for_vbase (BINFO_TYPE (thunk_binfo),
2481 TREE_TYPE (over_return));
2482 offset = size_diffop (offset,
2483 BINFO_OFFSET (virtual_offset));
2486 /* There was an existing fixed offset, this must be
2487 from the base just converted to, and the base the
2488 FN was thunking to. */
2489 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2491 fixed_offset = offset;
2495 if (fixed_offset || virtual_offset)
2496 /* Replace the overriding function with a covariant thunk. We
2497 will emit the overriding function in its own slot as
2499 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2500 fixed_offset, virtual_offset);
2503 my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2505 /* Assume that we will produce a thunk that convert all the way to
2506 the final overrider, and not to an intermediate virtual base. */
2507 virtual_base = NULL_TREE;
2509 /* See if we can convert to an intermediate virtual base first, and then
2510 use the vcall offset located there to finish the conversion. */
2511 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2513 /* If we find the final overrider, then we can stop
2515 if (same_type_p (BINFO_TYPE (b),
2516 BINFO_TYPE (TREE_VALUE (overrider))))
2519 /* If we find a virtual base, and we haven't yet found the
2520 overrider, then there is a virtual base between the
2521 declaring base (first_defn) and the final overrider. */
2522 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2526 /* Compute the constant adjustment to the `this' pointer. The
2527 `this' pointer, when this function is called, will point at BINFO
2528 (or one of its primary bases, which are at the same offset). */
2530 /* The `this' pointer needs to be adjusted from the declaration to
2531 the nearest virtual base. */
2532 delta = size_diffop (BINFO_OFFSET (virtual_base),
2533 BINFO_OFFSET (first_defn));
2535 /* If the nearest definition is in a lost primary, we don't need an
2536 entry in our vtable. Except possibly in a constructor vtable,
2537 if we happen to get our primary back. In that case, the offset
2538 will be zero, as it will be a primary base. */
2539 delta = size_zero_node;
2541 /* The `this' pointer needs to be adjusted from pointing to
2542 BINFO to pointing at the base where the final overrider
2544 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2545 BINFO_OFFSET (binfo));
2547 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2550 BV_VCALL_INDEX (*virtuals)
2551 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2554 /* Called from modify_all_vtables via dfs_walk. */
2557 dfs_modify_vtables (tree binfo, void* data)
2559 if (/* There's no need to modify the vtable for a non-virtual
2560 primary base; we're not going to use that vtable anyhow.
2561 We do still need to do this for virtual primary bases, as they
2562 could become non-primary in a construction vtable. */
2563 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2564 /* Similarly, a base without a vtable needs no modification. */
2565 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2573 make_new_vtable (t, binfo);
2575 /* Now, go through each of the virtual functions in the virtual
2576 function table for BINFO. Find the final overrider, and
2577 update the BINFO_VIRTUALS list appropriately. */
2578 for (virtuals = BINFO_VIRTUALS (binfo),
2579 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2581 virtuals = TREE_CHAIN (virtuals),
2582 old_virtuals = TREE_CHAIN (old_virtuals))
2583 update_vtable_entry_for_fn (t,
2585 BV_FN (old_virtuals),
2589 SET_BINFO_MARKED (binfo);
2594 /* Update all of the primary and secondary vtables for T. Create new
2595 vtables as required, and initialize their RTTI information. Each
2596 of the functions in VIRTUALS is declared in T and may override a
2597 virtual function from a base class; find and modify the appropriate
2598 entries to point to the overriding functions. Returns a list, in
2599 declaration order, of the virtual functions that are declared in T,
2600 but do not appear in the primary base class vtable, and which
2601 should therefore be appended to the end of the vtable for T. */
2604 modify_all_vtables (tree t, tree virtuals)
2606 tree binfo = TYPE_BINFO (t);
2609 /* Update all of the vtables. */
2612 dfs_unmarked_real_bases_queue_p,
2614 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2616 /* Add virtual functions not already in our primary vtable. These
2617 will be both those introduced by this class, and those overridden
2618 from secondary bases. It does not include virtuals merely
2619 inherited from secondary bases. */
2620 for (fnsp = &virtuals; *fnsp; )
2622 tree fn = TREE_VALUE (*fnsp);
2624 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2625 || DECL_VINDEX (fn) == error_mark_node)
2627 /* We don't need to adjust the `this' pointer when
2628 calling this function. */
2629 BV_DELTA (*fnsp) = integer_zero_node;
2630 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2632 /* This is a function not already in our vtable. Keep it. */
2633 fnsp = &TREE_CHAIN (*fnsp);
2636 /* We've already got an entry for this function. Skip it. */
2637 *fnsp = TREE_CHAIN (*fnsp);
2643 /* Get the base virtual function declarations in T that have the
2647 get_basefndecls (tree name, tree t)
2650 tree base_fndecls = NULL_TREE;
2651 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2654 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2655 if (TREE_CODE (methods) == FUNCTION_DECL
2656 && DECL_VINDEX (methods) != NULL_TREE
2657 && DECL_NAME (methods) == name)
2658 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2661 return base_fndecls;
2663 for (i = 0; i < n_baseclasses; i++)
2665 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2666 base_fndecls = chainon (get_basefndecls (name, basetype),
2670 return base_fndecls;
2673 /* If this declaration supersedes the declaration of
2674 a method declared virtual in the base class, then
2675 mark this field as being virtual as well. */
2678 check_for_override (tree decl, tree ctype)
2680 if (TREE_CODE (decl) == TEMPLATE_DECL)
2681 /* In [temp.mem] we have:
2683 A specialization of a member function template does not
2684 override a virtual function from a base class. */
2686 if ((DECL_DESTRUCTOR_P (decl)
2687 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2688 && look_for_overrides (ctype, decl)
2689 && !DECL_STATIC_FUNCTION_P (decl))
2690 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2691 the error_mark_node so that we know it is an overriding
2693 DECL_VINDEX (decl) = decl;
2695 if (DECL_VIRTUAL_P (decl))
2697 if (!DECL_VINDEX (decl))
2698 DECL_VINDEX (decl) = error_mark_node;
2699 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2703 /* Warn about hidden virtual functions that are not overridden in t.
2704 We know that constructors and destructors don't apply. */
2707 warn_hidden (tree t)
2709 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2710 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2713 /* We go through each separately named virtual function. */
2714 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2722 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2723 have the same name. Figure out what name that is. */
2724 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2725 /* There are no possibly hidden functions yet. */
2726 base_fndecls = NULL_TREE;
2727 /* Iterate through all of the base classes looking for possibly
2728 hidden functions. */
2729 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2731 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2732 base_fndecls = chainon (get_basefndecls (name, basetype),
2736 /* If there are no functions to hide, continue. */
2740 /* Remove any overridden functions. */
2741 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2743 fndecl = OVL_CURRENT (fns);
2744 if (DECL_VINDEX (fndecl))
2746 tree *prev = &base_fndecls;
2749 /* If the method from the base class has the same
2750 signature as the method from the derived class, it
2751 has been overridden. */
2752 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2753 *prev = TREE_CHAIN (*prev);
2755 prev = &TREE_CHAIN (*prev);
2759 /* Now give a warning for all base functions without overriders,
2760 as they are hidden. */
2761 while (base_fndecls)
2763 /* Here we know it is a hider, and no overrider exists. */
2764 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2765 cp_warning_at (" by `%D'",
2766 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2767 base_fndecls = TREE_CHAIN (base_fndecls);
2772 /* Check for things that are invalid. There are probably plenty of other
2773 things we should check for also. */
2776 finish_struct_anon (tree t)
2780 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2782 if (TREE_STATIC (field))
2784 if (TREE_CODE (field) != FIELD_DECL)
2787 if (DECL_NAME (field) == NULL_TREE
2788 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2790 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2791 for (; elt; elt = TREE_CHAIN (elt))
2793 /* We're generally only interested in entities the user
2794 declared, but we also find nested classes by noticing
2795 the TYPE_DECL that we create implicitly. You're
2796 allowed to put one anonymous union inside another,
2797 though, so we explicitly tolerate that. We use
2798 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2799 we also allow unnamed types used for defining fields. */
2800 if (DECL_ARTIFICIAL (elt)
2801 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2802 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2805 if (constructor_name_p (DECL_NAME (elt), t))
2806 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2809 if (TREE_CODE (elt) != FIELD_DECL)
2811 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2816 if (TREE_PRIVATE (elt))
2817 cp_pedwarn_at ("private member `%#D' in anonymous union",
2819 else if (TREE_PROTECTED (elt))
2820 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2823 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2824 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2830 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2831 will be used later during class template instantiation.
2832 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2833 a non-static member data (FIELD_DECL), a member function
2834 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2835 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2836 When FRIEND_P is nonzero, T is either a friend class
2837 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2838 (FUNCTION_DECL, TEMPLATE_DECL). */
2841 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2843 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2844 if (CLASSTYPE_TEMPLATE_INFO (type))
2845 CLASSTYPE_DECL_LIST (type)
2846 = tree_cons (friend_p ? NULL_TREE : type,
2847 t, CLASSTYPE_DECL_LIST (type));
2850 /* Create default constructors, assignment operators, and so forth for
2851 the type indicated by T, if they are needed.
2852 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2853 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2854 class cannot have a default constructor, copy constructor taking a
2855 const reference argument, or an assignment operator taking a const
2856 reference, respectively. If a virtual destructor is created, its
2857 DECL is returned; otherwise the return value is NULL_TREE. */
2860 add_implicitly_declared_members (tree t,
2861 int cant_have_default_ctor,
2862 int cant_have_const_cctor,
2863 int cant_have_const_assignment)
2866 tree implicit_fns = NULL_TREE;
2867 tree virtual_dtor = NULL_TREE;
2870 ++adding_implicit_members;
2873 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2875 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2876 check_for_override (default_fn, t);
2878 /* If we couldn't make it work, then pretend we didn't need it. */
2879 if (default_fn == void_type_node)
2880 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2883 TREE_CHAIN (default_fn) = implicit_fns;
2884 implicit_fns = default_fn;
2886 if (DECL_VINDEX (default_fn))
2887 virtual_dtor = default_fn;
2891 /* Any non-implicit destructor is non-trivial. */
2892 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2894 /* Default constructor. */
2895 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2897 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2898 TREE_CHAIN (default_fn) = implicit_fns;
2899 implicit_fns = default_fn;
2902 /* Copy constructor. */
2903 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2905 /* ARM 12.18: You get either X(X&) or X(const X&), but
2908 = implicitly_declare_fn (sfk_copy_constructor, t,
2909 /*const_p=*/!cant_have_const_cctor);
2910 TREE_CHAIN (default_fn) = implicit_fns;
2911 implicit_fns = default_fn;
2914 /* Assignment operator. */
2915 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2918 = implicitly_declare_fn (sfk_assignment_operator, t,
2919 /*const_p=*/!cant_have_const_assignment);
2920 TREE_CHAIN (default_fn) = implicit_fns;
2921 implicit_fns = default_fn;
2924 /* Now, hook all of the new functions on to TYPE_METHODS,
2925 and add them to the CLASSTYPE_METHOD_VEC. */
2926 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2928 add_method (t, *f, /*error_p=*/0);
2929 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2931 if (abi_version_at_least (2))
2932 /* G++ 3.2 put the implicit destructor at the *beginning* of the
2933 list, which cause the destructor to be emitted in an incorrect
2934 location in the vtable. */
2935 TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2938 if (warn_abi && virtual_dtor)
2939 warning ("vtable layout for class `%T' may not be ABI-compliant "
2940 "and may change in a future version of GCC due to implicit "
2941 "virtual destructor",
2943 *f = TYPE_METHODS (t);
2944 TYPE_METHODS (t) = implicit_fns;
2947 --adding_implicit_members;
2950 /* Subroutine of finish_struct_1. Recursively count the number of fields
2951 in TYPE, including anonymous union members. */
2954 count_fields (tree fields)
2958 for (x = fields; x; x = TREE_CHAIN (x))
2960 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2961 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2968 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2969 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2972 add_fields_to_vec (tree fields, tree field_vec, int idx)
2975 for (x = fields; x; x = TREE_CHAIN (x))
2977 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2978 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2980 TREE_VEC_ELT (field_vec, idx++) = x;
2985 /* FIELD is a bit-field. We are finishing the processing for its
2986 enclosing type. Issue any appropriate messages and set appropriate
2990 check_bitfield_decl (tree field)
2992 tree type = TREE_TYPE (field);
2995 /* Detect invalid bit-field type. */
2996 if (DECL_INITIAL (field)
2997 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2999 cp_error_at ("bit-field `%#D' with non-integral type", field);
3000 w = error_mark_node;
3003 /* Detect and ignore out of range field width. */
3004 if (DECL_INITIAL (field))
3006 w = DECL_INITIAL (field);
3008 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
3011 /* detect invalid field size. */
3012 if (TREE_CODE (w) == CONST_DECL)
3013 w = DECL_INITIAL (w);
3015 w = decl_constant_value (w);
3017 if (TREE_CODE (w) != INTEGER_CST)
3019 cp_error_at ("bit-field `%D' width not an integer constant",
3021 w = error_mark_node;
3023 else if (tree_int_cst_sgn (w) < 0)
3025 cp_error_at ("negative width in bit-field `%D'", field);
3026 w = error_mark_node;
3028 else if (integer_zerop (w) && DECL_NAME (field) != 0)
3030 cp_error_at ("zero width for bit-field `%D'", field);
3031 w = error_mark_node;
3033 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3034 && TREE_CODE (type) != ENUMERAL_TYPE
3035 && TREE_CODE (type) != BOOLEAN_TYPE)
3036 cp_warning_at ("width of `%D' exceeds its type", field);
3037 else if (TREE_CODE (type) == ENUMERAL_TYPE
3038 && (0 > compare_tree_int (w,
3039 min_precision (TYPE_MIN_VALUE (type),
3040 TREE_UNSIGNED (type)))
3041 || 0 > compare_tree_int (w,
3043 (TYPE_MAX_VALUE (type),
3044 TREE_UNSIGNED (type)))))
3045 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3049 /* Remove the bit-field width indicator so that the rest of the
3050 compiler does not treat that value as an initializer. */
3051 DECL_INITIAL (field) = NULL_TREE;
3053 if (w != error_mark_node)
3055 DECL_SIZE (field) = convert (bitsizetype, w);
3056 DECL_BIT_FIELD (field) = 1;
3058 if (integer_zerop (w)
3059 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
3061 #ifdef EMPTY_FIELD_BOUNDARY
3062 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3063 EMPTY_FIELD_BOUNDARY);
3065 #ifdef PCC_BITFIELD_TYPE_MATTERS
3066 if (PCC_BITFIELD_TYPE_MATTERS)
3068 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3070 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3077 /* Non-bit-fields are aligned for their type. */
3078 DECL_BIT_FIELD (field) = 0;
3079 CLEAR_DECL_C_BIT_FIELD (field);
3080 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3081 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3085 /* FIELD is a non bit-field. We are finishing the processing for its
3086 enclosing type T. Issue any appropriate messages and set appropriate
3090 check_field_decl (tree field,
3092 int* cant_have_const_ctor,
3093 int* cant_have_default_ctor,
3094 int* no_const_asn_ref,
3095 int* any_default_members)
3097 tree type = strip_array_types (TREE_TYPE (field));
3099 /* An anonymous union cannot contain any fields which would change
3100 the settings of CANT_HAVE_CONST_CTOR and friends. */
3101 if (ANON_UNION_TYPE_P (type))
3103 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3104 structs. So, we recurse through their fields here. */
3105 else if (ANON_AGGR_TYPE_P (type))
3109 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3110 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3111 check_field_decl (fields, t, cant_have_const_ctor,
3112 cant_have_default_ctor, no_const_asn_ref,
3113 any_default_members);
3115 /* Check members with class type for constructors, destructors,
3117 else if (CLASS_TYPE_P (type))
3119 /* Never let anything with uninheritable virtuals
3120 make it through without complaint. */
3121 abstract_virtuals_error (field, type);
3123 if (TREE_CODE (t) == UNION_TYPE)
3125 if (TYPE_NEEDS_CONSTRUCTING (type))
3126 cp_error_at ("member `%#D' with constructor not allowed in union",
3128 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3129 cp_error_at ("member `%#D' with destructor not allowed in union",
3131 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3132 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3137 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3138 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3139 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3140 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3141 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3144 if (!TYPE_HAS_CONST_INIT_REF (type))
3145 *cant_have_const_ctor = 1;
3147 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3148 *no_const_asn_ref = 1;
3150 if (TYPE_HAS_CONSTRUCTOR (type)
3151 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3152 *cant_have_default_ctor = 1;
3154 if (DECL_INITIAL (field) != NULL_TREE)
3156 /* `build_class_init_list' does not recognize
3158 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3159 cp_error_at ("multiple fields in union `%T' initialized");
3160 *any_default_members = 1;
3163 /* Non-bit-fields are aligned for their type, except packed fields
3164 which require only BITS_PER_UNIT alignment. */
3165 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3166 (DECL_PACKED (field)
3168 : TYPE_ALIGN (TREE_TYPE (field))));
3169 if (! DECL_PACKED (field))
3170 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3173 /* Check the data members (both static and non-static), class-scoped
3174 typedefs, etc., appearing in the declaration of T. Issue
3175 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3176 declaration order) of access declarations; each TREE_VALUE in this
3177 list is a USING_DECL.
3179 In addition, set the following flags:
3182 The class is empty, i.e., contains no non-static data members.
3184 CANT_HAVE_DEFAULT_CTOR_P
3185 This class cannot have an implicitly generated default
3188 CANT_HAVE_CONST_CTOR_P
3189 This class cannot have an implicitly generated copy constructor
3190 taking a const reference.
3192 CANT_HAVE_CONST_ASN_REF
3193 This class cannot have an implicitly generated assignment
3194 operator taking a const reference.
3196 All of these flags should be initialized before calling this
3199 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3200 fields can be added by adding to this chain. */
3203 check_field_decls (tree t, tree *access_decls,
3204 int *cant_have_default_ctor_p,
3205 int *cant_have_const_ctor_p,
3206 int *no_const_asn_ref_p)
3211 int any_default_members;
3213 /* First, delete any duplicate fields. */
3214 delete_duplicate_fields (TYPE_FIELDS (t));
3216 /* Assume there are no access declarations. */
3217 *access_decls = NULL_TREE;
3218 /* Assume this class has no pointer members. */
3220 /* Assume none of the members of this class have default
3222 any_default_members = 0;
3224 for (field = &TYPE_FIELDS (t); *field; field = next)
3227 tree type = TREE_TYPE (x);
3229 next = &TREE_CHAIN (x);
3231 if (TREE_CODE (x) == FIELD_DECL)
3233 DECL_PACKED (x) |= TYPE_PACKED (t);
3235 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3236 /* We don't treat zero-width bitfields as making a class
3243 /* The class is non-empty. */
3244 CLASSTYPE_EMPTY_P (t) = 0;
3245 /* The class is not even nearly empty. */
3246 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3247 /* If one of the data members contains an empty class,
3249 element_type = strip_array_types (type);
3250 if (CLASS_TYPE_P (element_type)
3251 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3252 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3256 if (TREE_CODE (x) == USING_DECL)
3258 /* Prune the access declaration from the list of fields. */
3259 *field = TREE_CHAIN (x);
3261 /* Save the access declarations for our caller. */
3262 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3264 /* Since we've reset *FIELD there's no reason to skip to the
3270 if (TREE_CODE (x) == TYPE_DECL
3271 || TREE_CODE (x) == TEMPLATE_DECL)
3274 /* If we've gotten this far, it's a data member, possibly static,
3275 or an enumerator. */
3277 DECL_CONTEXT (x) = t;
3279 /* ``A local class cannot have static data members.'' ARM 9.4 */
3280 if (current_function_decl && TREE_STATIC (x))
3281 cp_error_at ("field `%D' in local class cannot be static", x);
3283 /* Perform error checking that did not get done in
3285 if (TREE_CODE (type) == FUNCTION_TYPE)
3287 cp_error_at ("field `%D' invalidly declared function type",
3289 type = build_pointer_type (type);
3290 TREE_TYPE (x) = type;
3292 else if (TREE_CODE (type) == METHOD_TYPE)
3294 cp_error_at ("field `%D' invalidly declared method type", x);
3295 type = build_pointer_type (type);
3296 TREE_TYPE (x) = type;
3298 else if (TREE_CODE (type) == OFFSET_TYPE)
3300 cp_error_at ("field `%D' invalidly declared offset type", x);
3301 type = build_pointer_type (type);
3302 TREE_TYPE (x) = type;
3305 if (type == error_mark_node)
3308 /* When this goes into scope, it will be a non-local reference. */
3309 DECL_NONLOCAL (x) = 1;
3311 if (TREE_CODE (x) == CONST_DECL)
3314 if (TREE_CODE (x) == VAR_DECL)
3316 if (TREE_CODE (t) == UNION_TYPE)
3317 /* Unions cannot have static members. */
3318 cp_error_at ("field `%D' declared static in union", x);
3323 /* Now it can only be a FIELD_DECL. */
3325 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3326 CLASSTYPE_NON_AGGREGATE (t) = 1;
3328 /* If this is of reference type, check if it needs an init.
3329 Also do a little ANSI jig if necessary. */
3330 if (TREE_CODE (type) == REFERENCE_TYPE)
3332 CLASSTYPE_NON_POD_P (t) = 1;
3333 if (DECL_INITIAL (x) == NULL_TREE)
3334 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3336 /* ARM $12.6.2: [A member initializer list] (or, for an
3337 aggregate, initialization by a brace-enclosed list) is the
3338 only way to initialize nonstatic const and reference
3340 *cant_have_default_ctor_p = 1;
3341 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3343 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3345 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3348 type = strip_array_types (type);
3350 if (TREE_CODE (type) == POINTER_TYPE)
3353 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3354 CLASSTYPE_HAS_MUTABLE (t) = 1;
3356 if (! pod_type_p (type))
3357 /* DR 148 now allows pointers to members (which are POD themselves),
3358 to be allowed in POD structs. */
3359 CLASSTYPE_NON_POD_P (t) = 1;
3361 if (! zero_init_p (type))
3362 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3364 /* If any field is const, the structure type is pseudo-const. */
3365 if (CP_TYPE_CONST_P (type))
3367 C_TYPE_FIELDS_READONLY (t) = 1;
3368 if (DECL_INITIAL (x) == NULL_TREE)
3369 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3371 /* ARM $12.6.2: [A member initializer list] (or, for an
3372 aggregate, initialization by a brace-enclosed list) is the
3373 only way to initialize nonstatic const and reference
3375 *cant_have_default_ctor_p = 1;
3376 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3378 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3380 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3382 /* A field that is pseudo-const makes the structure likewise. */
3383 else if (IS_AGGR_TYPE (type))
3385 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3386 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3387 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3388 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3391 /* Core issue 80: A nonstatic data member is required to have a
3392 different name from the class iff the class has a
3393 user-defined constructor. */
3394 if (constructor_name_p (x, t) && TYPE_HAS_CONSTRUCTOR (t))
3395 cp_pedwarn_at ("field `%#D' with same name as class", x);
3397 /* We set DECL_C_BIT_FIELD in grokbitfield.
3398 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3399 if (DECL_C_BIT_FIELD (x))
3400 check_bitfield_decl (x);
3402 check_field_decl (x, t,
3403 cant_have_const_ctor_p,
3404 cant_have_default_ctor_p,
3406 &any_default_members);
3409 /* Effective C++ rule 11. */
3410 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3411 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3413 warning ("`%#T' has pointer data members", t);
3415 if (! TYPE_HAS_INIT_REF (t))
3417 warning (" but does not override `%T(const %T&)'", t, t);
3418 if (! TYPE_HAS_ASSIGN_REF (t))
3419 warning (" or `operator=(const %T&)'", t);
3421 else if (! TYPE_HAS_ASSIGN_REF (t))
3422 warning (" but does not override `operator=(const %T&)'", t);
3426 /* Check anonymous struct/anonymous union fields. */
3427 finish_struct_anon (t);
3429 /* We've built up the list of access declarations in reverse order.
3431 *access_decls = nreverse (*access_decls);
3434 /* If TYPE is an empty class type, records its OFFSET in the table of
3438 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3442 if (!is_empty_class (type))
3445 /* Record the location of this empty object in OFFSETS. */
3446 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3448 n = splay_tree_insert (offsets,
3449 (splay_tree_key) offset,
3450 (splay_tree_value) NULL_TREE);
3451 n->value = ((splay_tree_value)
3452 tree_cons (NULL_TREE,
3459 /* Returns nonzero if TYPE is an empty class type and there is
3460 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3463 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3468 if (!is_empty_class (type))
3471 /* Record the location of this empty object in OFFSETS. */
3472 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3476 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3477 if (same_type_p (TREE_VALUE (t), type))
3483 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3484 F for every subobject, passing it the type, offset, and table of
3485 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3488 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3489 than MAX_OFFSET will not be walked.
3491 If F returns a nonzero value, the traversal ceases, and that value
3492 is returned. Otherwise, returns zero. */
3495 walk_subobject_offsets (tree type,
3496 subobject_offset_fn f,
3503 tree type_binfo = NULL_TREE;
3505 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3507 if (max_offset && INT_CST_LT (max_offset, offset))
3512 if (abi_version_at_least (2))
3514 type = BINFO_TYPE (type);
3517 if (CLASS_TYPE_P (type))
3523 /* Avoid recursing into objects that are not interesting. */
3524 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3527 /* Record the location of TYPE. */
3528 r = (*f) (type, offset, offsets);
3532 /* Iterate through the direct base classes of TYPE. */
3534 type_binfo = TYPE_BINFO (type);
3535 for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3539 binfo = BINFO_BASETYPE (type_binfo, i);
3541 if (abi_version_at_least (2)
3542 && TREE_VIA_VIRTUAL (binfo))
3546 && TREE_VIA_VIRTUAL (binfo)
3547 && !BINFO_PRIMARY_P (binfo))
3550 if (!abi_version_at_least (2))
3551 binfo_offset = size_binop (PLUS_EXPR,
3553 BINFO_OFFSET (binfo));
3557 /* We cannot rely on BINFO_OFFSET being set for the base
3558 class yet, but the offsets for direct non-virtual
3559 bases can be calculated by going back to the TYPE. */
3560 orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3561 binfo_offset = size_binop (PLUS_EXPR,
3563 BINFO_OFFSET (orig_binfo));
3566 r = walk_subobject_offsets (binfo,
3571 (abi_version_at_least (2)
3572 ? /*vbases_p=*/0 : vbases_p));
3577 if (abi_version_at_least (2))
3581 /* Iterate through the virtual base classes of TYPE. In G++
3582 3.2, we included virtual bases in the direct base class
3583 loop above, which results in incorrect results; the
3584 correct offsets for virtual bases are only known when
3585 working with the most derived type. */
3587 for (vbase = CLASSTYPE_VBASECLASSES (type);
3589 vbase = TREE_CHAIN (vbase))
3591 binfo = TREE_VALUE (vbase);
3592 r = walk_subobject_offsets (binfo,
3594 size_binop (PLUS_EXPR,
3596 BINFO_OFFSET (binfo)),
3605 /* We still have to walk the primary base, if it is
3606 virtual. (If it is non-virtual, then it was walked
3608 vbase = get_primary_binfo (type_binfo);
3609 if (vbase && TREE_VIA_VIRTUAL (vbase))
3611 tree derived = type_binfo;
3612 while (BINFO_INHERITANCE_CHAIN (derived))
3613 derived = BINFO_INHERITANCE_CHAIN (derived);
3614 derived = TREE_TYPE (derived);
3615 vbase = binfo_for_vbase (TREE_TYPE (vbase), derived);
3617 if (BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3619 r = (walk_subobject_offsets
3621 offsets, max_offset, /*vbases_p=*/0));
3629 /* Iterate through the fields of TYPE. */
3630 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3631 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3635 if (abi_version_at_least (2))
3636 field_offset = byte_position (field);
3638 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3639 field_offset = DECL_FIELD_OFFSET (field);
3641 r = walk_subobject_offsets (TREE_TYPE (field),
3643 size_binop (PLUS_EXPR,
3653 else if (TREE_CODE (type) == ARRAY_TYPE)
3655 tree element_type = strip_array_types (type);
3656 tree domain = TYPE_DOMAIN (type);
3659 /* Avoid recursing into objects that are not interesting. */
3660 if (!CLASS_TYPE_P (element_type)
3661 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3664 /* Step through each of the elements in the array. */
3665 for (index = size_zero_node;
3666 /* G++ 3.2 had an off-by-one error here. */
3667 (abi_version_at_least (2)
3668 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3669 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3670 index = size_binop (PLUS_EXPR, index, size_one_node))
3672 r = walk_subobject_offsets (TREE_TYPE (type),
3680 offset = size_binop (PLUS_EXPR, offset,
3681 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3682 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3683 there's no point in iterating through the remaining
3684 elements of the array. */
3685 if (max_offset && INT_CST_LT (max_offset, offset))
3693 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3694 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3698 record_subobject_offsets (tree type,
3703 walk_subobject_offsets (type, record_subobject_offset, offset,
3704 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3707 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3708 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3709 virtual bases of TYPE are examined. */
3712 layout_conflict_p (tree type,
3717 splay_tree_node max_node;
3719 /* Get the node in OFFSETS that indicates the maximum offset where
3720 an empty subobject is located. */
3721 max_node = splay_tree_max (offsets);
3722 /* If there aren't any empty subobjects, then there's no point in
3723 performing this check. */
3727 return walk_subobject_offsets (type, check_subobject_offset, offset,
3728 offsets, (tree) (max_node->key),
3732 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3733 non-static data member of the type indicated by RLI. BINFO is the
3734 binfo corresponding to the base subobject, OFFSETS maps offsets to
3735 types already located at those offsets. This function determines
3736 the position of the DECL. */
3739 layout_nonempty_base_or_field (record_layout_info rli,
3745 tree offset = NULL_TREE;
3751 /* For the purposes of determining layout conflicts, we want to
3752 use the class type of BINFO; TREE_TYPE (DECL) will be the
3753 CLASSTYPE_AS_BASE version, which does not contain entries for
3754 zero-sized bases. */
3755 type = TREE_TYPE (binfo);
3760 type = TREE_TYPE (decl);
3764 /* Try to place the field. It may take more than one try if we have
3765 a hard time placing the field without putting two objects of the
3766 same type at the same address. */
3769 struct record_layout_info_s old_rli = *rli;
3771 /* Place this field. */
3772 place_field (rli, decl);
3773 offset = byte_position (decl);
3775 /* We have to check to see whether or not there is already
3776 something of the same type at the offset we're about to use.
3780 struct T : public S { int i; };
3781 struct U : public S, public T {};
3783 Here, we put S at offset zero in U. Then, we can't put T at
3784 offset zero -- its S component would be at the same address
3785 as the S we already allocated. So, we have to skip ahead.
3786 Since all data members, including those whose type is an
3787 empty class, have nonzero size, any overlap can happen only
3788 with a direct or indirect base-class -- it can't happen with
3790 /* G++ 3.2 did not check for overlaps when placing a non-empty
3792 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3794 if (layout_conflict_p (field_p ? type : binfo, offset,
3797 /* Strip off the size allocated to this field. That puts us
3798 at the first place we could have put the field with
3799 proper alignment. */
3802 /* Bump up by the alignment required for the type. */
3804 = size_binop (PLUS_EXPR, rli->bitpos,
3806 ? CLASSTYPE_ALIGN (type)
3807 : TYPE_ALIGN (type)));
3808 normalize_rli (rli);
3811 /* There was no conflict. We're done laying out this field. */
3815 /* Now that we know where it will be placed, update its
3817 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3818 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3819 this point because their BINFO_OFFSET is copied from another
3820 hierarchy. Therefore, we may not need to add the entire
3822 propagate_binfo_offsets (binfo,
3823 size_diffop (convert (ssizetype, offset),
3825 BINFO_OFFSET (binfo))),
3829 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3832 empty_base_at_nonzero_offset_p (tree type,
3834 splay_tree offsets ATTRIBUTE_UNUSED)
3836 return is_empty_class (type) && !integer_zerop (offset);
3839 /* Layout the empty base BINFO. EOC indicates the byte currently just
3840 past the end of the class, and should be correctly aligned for a
3841 class of the type indicated by BINFO; OFFSETS gives the offsets of
3842 the empty bases allocated so far. T is the most derived
3843 type. Return nonzero iff we added it at the end. */
3846 layout_empty_base (tree binfo, tree eoc, splay_tree offsets, tree t)
3849 tree basetype = BINFO_TYPE (binfo);
3852 /* This routine should only be used for empty classes. */
3853 my_friendly_assert (is_empty_class (basetype), 20000321);
3854 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3856 if (abi_version_at_least (2))
3857 BINFO_OFFSET (binfo) = size_zero_node;
3858 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3859 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3860 "change in a future version of GCC",
3861 BINFO_TYPE (binfo));
3863 /* This is an empty base class. We first try to put it at offset
3865 if (layout_conflict_p (binfo,
3866 BINFO_OFFSET (binfo),
3870 /* That didn't work. Now, we move forward from the next
3871 available spot in the class. */
3873 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3876 if (!layout_conflict_p (binfo,
3877 BINFO_OFFSET (binfo),
3880 /* We finally found a spot where there's no overlap. */
3883 /* There's overlap here, too. Bump along to the next spot. */
3884 propagate_binfo_offsets (binfo, alignment, t);
3890 /* Layout the the base given by BINFO in the class indicated by RLI.
3891 *BASE_ALIGN is a running maximum of the alignments of
3892 any base class. OFFSETS gives the location of empty base
3893 subobjects. T is the most derived type. Return nonzero if the new
3894 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3895 *NEXT_FIELD, unless BINFO is for an empty base class.
3897 Returns the location at which the next field should be inserted. */
3900 build_base_field (record_layout_info rli, tree binfo,
3901 splay_tree offsets, tree *next_field)
3904 tree basetype = BINFO_TYPE (binfo);
3906 if (!COMPLETE_TYPE_P (basetype))
3907 /* This error is now reported in xref_tag, thus giving better
3908 location information. */
3911 /* Place the base class. */
3912 if (!is_empty_class (basetype))
3916 /* The containing class is non-empty because it has a non-empty
3918 CLASSTYPE_EMPTY_P (t) = 0;
3920 /* Create the FIELD_DECL. */
3921 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3922 DECL_ARTIFICIAL (decl) = 1;
3923 DECL_FIELD_CONTEXT (decl) = t;
3924 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3925 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3926 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3927 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3929 /* Try to place the field. It may take more than one try if we
3930 have a hard time placing the field without putting two
3931 objects of the same type at the same address. */
3932 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3933 /* Add the new FIELD_DECL to the list of fields for T. */
3934 TREE_CHAIN (decl) = *next_field;
3936 next_field = &TREE_CHAIN (decl);
3943 /* On some platforms (ARM), even empty classes will not be
3945 eoc = round_up (rli_size_unit_so_far (rli),
3946 CLASSTYPE_ALIGN_UNIT (basetype));
3947 atend = layout_empty_base (binfo, eoc, offsets, t);
3948 /* A nearly-empty class "has no proper base class that is empty,
3949 not morally virtual, and at an offset other than zero." */
3950 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3953 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3954 /* The check above (used in G++ 3.2) is insufficient because
3955 an empty class placed at offset zero might itself have an
3956 empty base at a nonzero offset. */
3957 else if (walk_subobject_offsets (basetype,
3958 empty_base_at_nonzero_offset_p,
3961 /*max_offset=*/NULL_TREE,
3964 if (abi_version_at_least (2))
3965 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3967 warning ("class `%T' will be considered nearly empty in a "
3968 "future version of GCC", t);
3972 /* We do not create a FIELD_DECL for empty base classes because
3973 it might overlap some other field. We want to be able to
3974 create CONSTRUCTORs for the class by iterating over the
3975 FIELD_DECLs, and the back end does not handle overlapping
3978 /* An empty virtual base causes a class to be non-empty
3979 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3980 here because that was already done when the virtual table
3981 pointer was created. */
3984 /* Record the offsets of BINFO and its base subobjects. */
3985 record_subobject_offsets (binfo,
3986 BINFO_OFFSET (binfo),
3993 /* Layout all of the non-virtual base classes. Record empty
3994 subobjects in OFFSETS. T is the most derived type. Return nonzero
3995 if the type cannot be nearly empty. The fields created
3996 corresponding to the base classes will be inserted at
4000 build_base_fields (record_layout_info rli,
4001 splay_tree offsets, tree *next_field)
4003 /* Chain to hold all the new FIELD_DECLs which stand in for base class
4006 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
4009 /* The primary base class is always allocated first. */
4010 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4011 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4012 offsets, next_field);
4014 /* Now allocate the rest of the bases. */
4015 for (i = 0; i < n_baseclasses; ++i)
4019 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4021 /* The primary base was already allocated above, so we don't
4022 need to allocate it again here. */
4023 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4026 /* A primary virtual base class is allocated just like any other
4027 base class, but a non-primary virtual base is allocated
4028 later, in layout_virtual_bases. */
4029 if (TREE_VIA_VIRTUAL (base_binfo)
4030 && !BINFO_PRIMARY_P (base_binfo))
4033 next_field = build_base_field (rli, base_binfo,
4034 offsets, next_field);
4038 /* Go through the TYPE_METHODS of T issuing any appropriate
4039 diagnostics, figuring out which methods override which other
4040 methods, and so forth. */
4043 check_methods (tree t)
4047 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4049 /* If this was an evil function, don't keep it in class. */
4050 if (DECL_ASSEMBLER_NAME_SET_P (x)
4051 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4054 check_for_override (x, t);
4055 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4056 cp_error_at ("initializer specified for non-virtual method `%D'", x);
4058 /* The name of the field is the original field name
4059 Save this in auxiliary field for later overloading. */
4060 if (DECL_VINDEX (x))
4062 TYPE_POLYMORPHIC_P (t) = 1;
4063 if (DECL_PURE_VIRTUAL_P (x))
4064 CLASSTYPE_PURE_VIRTUALS (t)
4065 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4070 /* FN is a constructor or destructor. Clone the declaration to create
4071 a specialized in-charge or not-in-charge version, as indicated by
4075 build_clone (tree fn, tree name)
4080 /* Copy the function. */
4081 clone = copy_decl (fn);
4082 /* Remember where this function came from. */
4083 DECL_CLONED_FUNCTION (clone) = fn;
4084 DECL_ABSTRACT_ORIGIN (clone) = fn;
4085 /* Reset the function name. */
4086 DECL_NAME (clone) = name;
4087 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4088 /* There's no pending inline data for this function. */
4089 DECL_PENDING_INLINE_INFO (clone) = NULL;
4090 DECL_PENDING_INLINE_P (clone) = 0;
4091 /* And it hasn't yet been deferred. */
4092 DECL_DEFERRED_FN (clone) = 0;
4094 /* The base-class destructor is not virtual. */
4095 if (name == base_dtor_identifier)
4097 DECL_VIRTUAL_P (clone) = 0;
4098 if (TREE_CODE (clone) != TEMPLATE_DECL)
4099 DECL_VINDEX (clone) = NULL_TREE;
4102 /* If there was an in-charge parameter, drop it from the function
4104 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4110 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4111 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4112 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4113 /* Skip the `this' parameter. */
4114 parmtypes = TREE_CHAIN (parmtypes);
4115 /* Skip the in-charge parameter. */
4116 parmtypes = TREE_CHAIN (parmtypes);
4117 /* And the VTT parm, in a complete [cd]tor. */
4118 if (DECL_HAS_VTT_PARM_P (fn)
4119 && ! DECL_NEEDS_VTT_PARM_P (clone))
4120 parmtypes = TREE_CHAIN (parmtypes);
4121 /* If this is subobject constructor or destructor, add the vtt
4124 = build_cplus_method_type (basetype,
4125 TREE_TYPE (TREE_TYPE (clone)),
4128 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4132 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4133 aren't function parameters; those are the template parameters. */
4134 if (TREE_CODE (clone) != TEMPLATE_DECL)
4136 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4137 /* Remove the in-charge parameter. */
4138 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4140 TREE_CHAIN (DECL_ARGUMENTS (clone))
4141 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4142 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4144 /* And the VTT parm, in a complete [cd]tor. */
4145 if (DECL_HAS_VTT_PARM_P (fn))
4147 if (DECL_NEEDS_VTT_PARM_P (clone))
4148 DECL_HAS_VTT_PARM_P (clone) = 1;
4151 TREE_CHAIN (DECL_ARGUMENTS (clone))
4152 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4153 DECL_HAS_VTT_PARM_P (clone) = 0;
4157 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4159 DECL_CONTEXT (parms) = clone;
4160 cxx_dup_lang_specific_decl (parms);
4164 /* Create the RTL for this function. */
4165 SET_DECL_RTL (clone, NULL_RTX);
4166 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4168 /* Make it easy to find the CLONE given the FN. */
4169 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4170 TREE_CHAIN (fn) = clone;
4172 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4173 if (TREE_CODE (clone) == TEMPLATE_DECL)
4177 DECL_TEMPLATE_RESULT (clone)
4178 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4179 result = DECL_TEMPLATE_RESULT (clone);
4180 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4181 DECL_TI_TEMPLATE (result) = clone;
4183 else if (DECL_DEFERRED_FN (fn))
4189 /* Produce declarations for all appropriate clones of FN. If
4190 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4191 CLASTYPE_METHOD_VEC as well. */
4194 clone_function_decl (tree fn, int update_method_vec_p)
4198 /* Avoid inappropriate cloning. */
4200 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4203 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4205 /* For each constructor, we need two variants: an in-charge version
4206 and a not-in-charge version. */
4207 clone = build_clone (fn, complete_ctor_identifier);
4208 if (update_method_vec_p)
4209 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4210 clone = build_clone (fn, base_ctor_identifier);
4211 if (update_method_vec_p)
4212 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4216 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4218 /* For each destructor, we need three variants: an in-charge
4219 version, a not-in-charge version, and an in-charge deleting
4220 version. We clone the deleting version first because that
4221 means it will go second on the TYPE_METHODS list -- and that
4222 corresponds to the correct layout order in the virtual
4225 For a non-virtual destructor, we do not build a deleting
4227 if (DECL_VIRTUAL_P (fn))
4229 clone = build_clone (fn, deleting_dtor_identifier);
4230 if (update_method_vec_p)
4231 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4233 clone = build_clone (fn, complete_dtor_identifier);
4234 if (update_method_vec_p)
4235 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4236 clone = build_clone (fn, base_dtor_identifier);
4237 if (update_method_vec_p)
4238 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4241 /* Note that this is an abstract function that is never emitted. */
4242 DECL_ABSTRACT (fn) = 1;
4245 /* DECL is an in charge constructor, which is being defined. This will
4246 have had an in class declaration, from whence clones were
4247 declared. An out-of-class definition can specify additional default
4248 arguments. As it is the clones that are involved in overload
4249 resolution, we must propagate the information from the DECL to its
4253 adjust_clone_args (tree decl)
4257 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4258 clone = TREE_CHAIN (clone))
4260 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4261 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4262 tree decl_parms, clone_parms;
4264 clone_parms = orig_clone_parms;
4266 /* Skip the 'this' parameter. */
4267 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4268 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4270 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4271 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4272 if (DECL_HAS_VTT_PARM_P (decl))
4273 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4275 clone_parms = orig_clone_parms;
4276 if (DECL_HAS_VTT_PARM_P (clone))
4277 clone_parms = TREE_CHAIN (clone_parms);
4279 for (decl_parms = orig_decl_parms; decl_parms;
4280 decl_parms = TREE_CHAIN (decl_parms),
4281 clone_parms = TREE_CHAIN (clone_parms))
4283 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4284 TREE_TYPE (clone_parms)), 20010424);
4286 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4288 /* A default parameter has been added. Adjust the
4289 clone's parameters. */
4290 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4291 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4294 clone_parms = orig_decl_parms;
4296 if (DECL_HAS_VTT_PARM_P (clone))
4298 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4299 TREE_VALUE (orig_clone_parms),
4301 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4303 type = build_cplus_method_type (basetype,
4304 TREE_TYPE (TREE_TYPE (clone)),
4307 type = build_exception_variant (type, exceptions);
4308 TREE_TYPE (clone) = type;
4310 clone_parms = NULL_TREE;
4314 my_friendly_assert (!clone_parms, 20010424);
4318 /* For each of the constructors and destructors in T, create an
4319 in-charge and not-in-charge variant. */
4322 clone_constructors_and_destructors (tree t)
4326 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4328 if (!CLASSTYPE_METHOD_VEC (t))
4331 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4332 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4333 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4334 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4337 /* Remove all zero-width bit-fields from T. */
4340 remove_zero_width_bit_fields (tree t)
4344 fieldsp = &TYPE_FIELDS (t);
4347 if (TREE_CODE (*fieldsp) == FIELD_DECL
4348 && DECL_C_BIT_FIELD (*fieldsp)
4349 && DECL_INITIAL (*fieldsp))
4350 *fieldsp = TREE_CHAIN (*fieldsp);
4352 fieldsp = &TREE_CHAIN (*fieldsp);
4356 /* Returns TRUE iff we need a cookie when dynamically allocating an
4357 array whose elements have the indicated class TYPE. */
4360 type_requires_array_cookie (tree type)
4363 bool has_two_argument_delete_p = false;
4365 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4367 /* If there's a non-trivial destructor, we need a cookie. In order
4368 to iterate through the array calling the destructor for each
4369 element, we'll have to know how many elements there are. */
4370 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4373 /* If the usual deallocation function is a two-argument whose second
4374 argument is of type `size_t', then we have to pass the size of
4375 the array to the deallocation function, so we will need to store
4377 fns = lookup_fnfields (TYPE_BINFO (type),
4378 ansi_opname (VEC_DELETE_EXPR),
4380 /* If there are no `operator []' members, or the lookup is
4381 ambiguous, then we don't need a cookie. */
4382 if (!fns || fns == error_mark_node)
4384 /* Loop through all of the functions. */
4385 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4390 /* Select the current function. */
4391 fn = OVL_CURRENT (fns);
4392 /* See if this function is a one-argument delete function. If
4393 it is, then it will be the usual deallocation function. */
4394 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4395 if (second_parm == void_list_node)
4397 /* Otherwise, if we have a two-argument function and the second
4398 argument is `size_t', it will be the usual deallocation
4399 function -- unless there is one-argument function, too. */
4400 if (TREE_CHAIN (second_parm) == void_list_node
4401 && same_type_p (TREE_VALUE (second_parm), sizetype))
4402 has_two_argument_delete_p = true;
4405 return has_two_argument_delete_p;
4408 /* Check the validity of the bases and members declared in T. Add any
4409 implicitly-generated functions (like copy-constructors and
4410 assignment operators). Compute various flag bits (like
4411 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4412 level: i.e., independently of the ABI in use. */
4415 check_bases_and_members (tree t)
4417 /* Nonzero if we are not allowed to generate a default constructor
4419 int cant_have_default_ctor;
4420 /* Nonzero if the implicitly generated copy constructor should take
4421 a non-const reference argument. */
4422 int cant_have_const_ctor;
4423 /* Nonzero if the the implicitly generated assignment operator
4424 should take a non-const reference argument. */
4425 int no_const_asn_ref;
4428 /* By default, we use const reference arguments and generate default
4430 cant_have_default_ctor = 0;
4431 cant_have_const_ctor = 0;
4432 no_const_asn_ref = 0;
4434 /* Check all the base-classes. */
4435 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4438 /* Check all the data member declarations. */
4439 check_field_decls (t, &access_decls,
4440 &cant_have_default_ctor,
4441 &cant_have_const_ctor,
4444 /* Check all the method declarations. */
4447 /* A nearly-empty class has to be vptr-containing; a nearly empty
4448 class contains just a vptr. */
4449 if (!TYPE_CONTAINS_VPTR_P (t))
4450 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4452 /* Do some bookkeeping that will guide the generation of implicitly
4453 declared member functions. */
4454 TYPE_HAS_COMPLEX_INIT_REF (t)
4455 |= (TYPE_HAS_INIT_REF (t)
4456 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4457 || TYPE_POLYMORPHIC_P (t));
4458 TYPE_NEEDS_CONSTRUCTING (t)
4459 |= (TYPE_HAS_CONSTRUCTOR (t)
4460 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4461 || TYPE_POLYMORPHIC_P (t));
4462 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4463 || TYPE_POLYMORPHIC_P (t));
4464 CLASSTYPE_NON_POD_P (t)
4465 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4466 || TYPE_HAS_ASSIGN_REF (t));
4467 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4468 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4469 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4471 /* Synthesize any needed methods. Note that methods will be synthesized
4472 for anonymous unions; grok_x_components undoes that. */
4473 add_implicitly_declared_members (t, cant_have_default_ctor,
4474 cant_have_const_ctor,
4477 /* Create the in-charge and not-in-charge variants of constructors
4479 clone_constructors_and_destructors (t);
4481 /* Process the using-declarations. */
4482 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4483 handle_using_decl (TREE_VALUE (access_decls), t);
4485 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4486 finish_struct_methods (t);
4488 /* Figure out whether or not we will need a cookie when dynamically
4489 allocating an array of this type. */
4490 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4491 = type_requires_array_cookie (t);
4494 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4495 accordingly. If a new vfield was created (because T doesn't have a
4496 primary base class), then the newly created field is returned. It
4497 is not added to the TYPE_FIELDS list; it is the caller's
4498 responsibility to do that. Accumulate declared virtual functions
4502 create_vtable_ptr (tree t, tree* virtuals_p)
4506 /* Collect the virtual functions declared in T. */
4507 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4508 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4509 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4511 tree new_virtual = make_node (TREE_LIST);
4513 BV_FN (new_virtual) = fn;
4514 BV_DELTA (new_virtual) = integer_zero_node;
4516 TREE_CHAIN (new_virtual) = *virtuals_p;
4517 *virtuals_p = new_virtual;
4520 /* If we couldn't find an appropriate base class, create a new field
4521 here. Even if there weren't any new virtual functions, we might need a
4522 new virtual function table if we're supposed to include vptrs in
4523 all classes that need them. */
4524 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4526 /* We build this decl with vtbl_ptr_type_node, which is a
4527 `vtable_entry_type*'. It might seem more precise to use
4528 `vtable_entry_type (*)[N]' where N is the number of firtual
4529 functions. However, that would require the vtable pointer in
4530 base classes to have a different type than the vtable pointer
4531 in derived classes. We could make that happen, but that
4532 still wouldn't solve all the problems. In particular, the
4533 type-based alias analysis code would decide that assignments
4534 to the base class vtable pointer can't alias assignments to
4535 the derived class vtable pointer, since they have different
4536 types. Thus, in a derived class destructor, where the base
4537 class constructor was inlined, we could generate bad code for
4538 setting up the vtable pointer.
4540 Therefore, we use one type for all vtable pointers. We still
4541 use a type-correct type; it's just doesn't indicate the array
4542 bounds. That's better than using `void*' or some such; it's
4543 cleaner, and it let's the alias analysis code know that these
4544 stores cannot alias stores to void*! */
4547 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4548 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4549 DECL_VIRTUAL_P (field) = 1;
4550 DECL_ARTIFICIAL (field) = 1;
4551 DECL_FIELD_CONTEXT (field) = t;
4552 DECL_FCONTEXT (field) = t;
4553 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4554 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4556 TYPE_VFIELD (t) = field;
4558 /* This class is non-empty. */
4559 CLASSTYPE_EMPTY_P (t) = 0;
4561 if (CLASSTYPE_N_BASECLASSES (t))
4562 /* If there were any baseclasses, they can't possibly be at
4563 offset zero any more, because that's where the vtable
4564 pointer is. So, converting to a base class is going to
4566 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4574 /* Fixup the inline function given by INFO now that the class is
4578 fixup_pending_inline (tree fn)
4580 if (DECL_PENDING_INLINE_INFO (fn))
4582 tree args = DECL_ARGUMENTS (fn);
4585 DECL_CONTEXT (args) = fn;
4586 args = TREE_CHAIN (args);
4591 /* Fixup the inline methods and friends in TYPE now that TYPE is
4595 fixup_inline_methods (tree type)
4597 tree method = TYPE_METHODS (type);
4599 if (method && TREE_CODE (method) == TREE_VEC)
4601 if (TREE_VEC_ELT (method, 1))
4602 method = TREE_VEC_ELT (method, 1);
4603 else if (TREE_VEC_ELT (method, 0))
4604 method = TREE_VEC_ELT (method, 0);
4606 method = TREE_VEC_ELT (method, 2);
4609 /* Do inline member functions. */
4610 for (; method; method = TREE_CHAIN (method))
4611 fixup_pending_inline (method);
4614 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4616 method = TREE_CHAIN (method))
4617 fixup_pending_inline (TREE_VALUE (method));
4618 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4621 /* Add OFFSET to all base types of BINFO which is a base in the
4622 hierarchy dominated by T.
4624 OFFSET, which is a type offset, is number of bytes. */
4627 propagate_binfo_offsets (tree binfo, tree offset, tree t)
4632 /* Update BINFO's offset. */
4633 BINFO_OFFSET (binfo)
4634 = convert (sizetype,
4635 size_binop (PLUS_EXPR,
4636 convert (ssizetype, BINFO_OFFSET (binfo)),
4639 /* Find the primary base class. */
4640 primary_binfo = get_primary_binfo (binfo);
4642 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4644 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4648 /* On the first time through the loop, do the primary base.
4649 Because the primary base need not be an immediate base, we
4650 must handle the primary base specially. */
4656 base_binfo = primary_binfo;
4660 base_binfo = BINFO_BASETYPE (binfo, i);
4661 /* Don't do the primary base twice. */
4662 if (base_binfo == primary_binfo)
4666 /* Skip virtual bases that aren't our canonical primary base. */
4667 if (TREE_VIA_VIRTUAL (base_binfo)
4668 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4669 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4672 propagate_binfo_offsets (base_binfo, offset, t);
4676 /* Called via dfs_walk from layout_virtual bases. */
4679 dfs_set_offset_for_unshared_vbases (tree binfo, void* data)
4681 /* If this is a virtual base, make sure it has the same offset as
4682 the shared copy. If it's a primary base, then we know it's
4684 if (TREE_VIA_VIRTUAL (binfo))
4686 tree t = (tree) data;
4690 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4693 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4694 propagate_binfo_offsets (binfo, offset, t);
4701 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4702 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4703 empty subobjects of T. */
4706 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4710 bool first_vbase = true;
4713 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4716 if (!abi_version_at_least(2))
4718 /* In G++ 3.2, we incorrectly rounded the size before laying out
4719 the virtual bases. */
4720 finish_record_layout (rli, /*free_p=*/false);
4721 #ifdef STRUCTURE_SIZE_BOUNDARY
4722 /* Packed structures don't need to have minimum size. */
4723 if (! TYPE_PACKED (t))
4724 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4726 rli->offset = TYPE_SIZE_UNIT (t);
4727 rli->bitpos = bitsize_zero_node;
4728 rli->record_align = TYPE_ALIGN (t);
4731 /* Find the last field. The artificial fields created for virtual
4732 bases will go after the last extant field to date. */
4733 next_field = &TYPE_FIELDS (t);
4735 next_field = &TREE_CHAIN (*next_field);
4737 /* Go through the virtual bases, allocating space for each virtual
4738 base that is not already a primary base class. These are
4739 allocated in inheritance graph order. */
4740 for (vbases = TYPE_BINFO (t);
4742 vbases = TREE_CHAIN (vbases))
4746 if (!TREE_VIA_VIRTUAL (vbases))
4749 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4751 if (!BINFO_PRIMARY_P (vbase))
4753 tree basetype = TREE_TYPE (vbase);
4755 /* This virtual base is not a primary base of any class in the
4756 hierarchy, so we have to add space for it. */
4757 next_field = build_base_field (rli, vbase,
4758 offsets, next_field);
4760 /* If the first virtual base might have been placed at a
4761 lower address, had we started from CLASSTYPE_SIZE, rather
4762 than TYPE_SIZE, issue a warning. There can be both false
4763 positives and false negatives from this warning in rare
4764 cases; to deal with all the possibilities would probably
4765 require performing both layout algorithms and comparing
4766 the results which is not particularly tractable. */
4770 (size_binop (CEIL_DIV_EXPR,
4771 round_up (CLASSTYPE_SIZE (t),
4772 CLASSTYPE_ALIGN (basetype)),
4774 BINFO_OFFSET (vbase))))
4775 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4778 first_vbase = false;
4782 /* Now, go through the TYPE_BINFO hierarchy, setting the
4783 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4784 bases and their direct and indirect bases. The ambiguity checks
4785 in lookup_base depend on the BINFO_OFFSETs being set
4787 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4790 /* Returns the offset of the byte just past the end of the base class
4794 end_of_base (tree binfo)
4798 if (is_empty_class (BINFO_TYPE (binfo)))
4799 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4800 allocate some space for it. It cannot have virtual bases, so
4801 TYPE_SIZE_UNIT is fine. */
4802 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4804 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4806 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4809 /* Returns the offset of the byte just past the end of the base class
4810 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4811 only non-virtual bases are included. */
4814 end_of_class (tree t, int include_virtuals_p)
4816 tree result = size_zero_node;
4821 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4823 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4825 if (!include_virtuals_p
4826 && TREE_VIA_VIRTUAL (binfo)
4827 && !BINFO_PRIMARY_P (binfo))
4830 offset = end_of_base (binfo);
4831 if (INT_CST_LT_UNSIGNED (result, offset))
4835 /* G++ 3.2 did not check indirect virtual bases. */
4836 if (abi_version_at_least (2) && include_virtuals_p)
4837 for (binfo = CLASSTYPE_VBASECLASSES (t);
4839 binfo = TREE_CHAIN (binfo))
4841 offset = end_of_base (TREE_VALUE (binfo));
4842 if (INT_CST_LT_UNSIGNED (result, offset))
4849 /* Warn about bases of T that are inaccessible because they are
4850 ambiguous. For example:
4853 struct T : public S {};
4854 struct U : public S, public T {};
4856 Here, `(S*) new U' is not allowed because there are two `S'
4860 warn_about_ambiguous_bases (tree t)
4866 /* Check direct bases. */
4867 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4869 basetype = TYPE_BINFO_BASETYPE (t, i);
4871 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4872 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4876 /* Check for ambiguous virtual bases. */
4878 for (vbases = CLASSTYPE_VBASECLASSES (t);
4880 vbases = TREE_CHAIN (vbases))
4882 basetype = BINFO_TYPE (TREE_VALUE (vbases));
4884 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4885 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4890 /* Compare two INTEGER_CSTs K1 and K2. */
4893 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4895 return tree_int_cst_compare ((tree) k1, (tree) k2);
4898 /* Increase the size indicated in RLI to account for empty classes
4899 that are "off the end" of the class. */
4902 include_empty_classes (record_layout_info rli)
4907 /* It might be the case that we grew the class to allocate a
4908 zero-sized base class. That won't be reflected in RLI, yet,
4909 because we are willing to overlay multiple bases at the same
4910 offset. However, now we need to make sure that RLI is big enough
4911 to reflect the entire class. */
4912 eoc = end_of_class (rli->t,
4913 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4914 rli_size = rli_size_unit_so_far (rli);
4915 if (TREE_CODE (rli_size) == INTEGER_CST
4916 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4918 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4920 = size_binop (PLUS_EXPR,
4922 size_binop (MULT_EXPR,
4923 convert (bitsizetype,
4924 size_binop (MINUS_EXPR,
4926 bitsize_int (BITS_PER_UNIT)));
4927 normalize_rli (rli);
4931 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4932 BINFO_OFFSETs for all of the base-classes. Position the vtable
4933 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4936 layout_class_type (tree t, tree *virtuals_p)
4938 tree non_static_data_members;
4941 record_layout_info rli;
4942 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4943 types that appear at that offset. */
4944 splay_tree empty_base_offsets;
4945 /* True if the last field layed out was a bit-field. */
4946 bool last_field_was_bitfield = false;
4947 /* The location at which the next field should be inserted. */
4949 /* T, as a base class. */
4952 /* Keep track of the first non-static data member. */
4953 non_static_data_members = TYPE_FIELDS (t);
4955 /* Start laying out the record. */
4956 rli = start_record_layout (t);
4958 /* If possible, we reuse the virtual function table pointer from one
4959 of our base classes. */
4960 determine_primary_base (t);
4962 /* Create a pointer to our virtual function table. */
4963 vptr = create_vtable_ptr (t, virtuals_p);
4965 /* The vptr is always the first thing in the class. */
4968 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4969 TYPE_FIELDS (t) = vptr;
4970 next_field = &TREE_CHAIN (vptr);
4971 place_field (rli, vptr);
4974 next_field = &TYPE_FIELDS (t);
4976 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4977 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4979 build_base_fields (rli, empty_base_offsets, next_field);
4981 /* Layout the non-static data members. */
4982 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4986 bool was_unnamed_p = false;
4988 /* We still pass things that aren't non-static data members to
4989 the back-end, in case it wants to do something with them. */
4990 if (TREE_CODE (field) != FIELD_DECL)
4992 place_field (rli, field);
4993 /* If the static data member has incomplete type, keep track
4994 of it so that it can be completed later. (The handling
4995 of pending statics in finish_record_layout is
4996 insufficient; consider:
4999 struct S2 { static S1 s1; };
5001 At this point, finish_record_layout will be called, but
5002 S1 is still incomplete.) */
5003 if (TREE_CODE (field) == VAR_DECL)
5004 maybe_register_incomplete_var (field);
5008 type = TREE_TYPE (field);
5010 /* If this field is a bit-field whose width is greater than its
5011 type, then there are some special rules for allocating
5013 if (DECL_C_BIT_FIELD (field)
5014 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5016 integer_type_kind itk;
5018 /* We must allocate the bits as if suitably aligned for the
5019 longest integer type that fits in this many bits. type
5020 of the field. Then, we are supposed to use the left over
5021 bits as additional padding. */
5022 for (itk = itk_char; itk != itk_none; ++itk)
5023 if (INT_CST_LT (DECL_SIZE (field),
5024 TYPE_SIZE (integer_types[itk])))
5027 /* ITK now indicates a type that is too large for the
5028 field. We have to back up by one to find the largest
5030 integer_type = integer_types[itk - 1];
5032 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5033 /* In a union, the padding field must have the full width
5034 of the bit-field; all fields start at offset zero. */
5035 padding = DECL_SIZE (field);
5038 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
5039 warning ("size assigned to `%T' may not be "
5040 "ABI-compliant and may change in a future "
5043 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5044 TYPE_SIZE (integer_type));
5046 #ifdef PCC_BITFIELD_TYPE_MATTERS
5047 /* An unnamed bitfield does not normally affect the
5048 alignment of the containing class on a target where
5049 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
5050 make any exceptions for unnamed bitfields when the
5051 bitfields are longer than their types. Therefore, we
5052 temporarily give the field a name. */
5053 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5055 was_unnamed_p = true;
5056 DECL_NAME (field) = make_anon_name ();
5059 DECL_SIZE (field) = TYPE_SIZE (integer_type);
5060 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5061 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5064 padding = NULL_TREE;
5066 layout_nonempty_base_or_field (rli, field, NULL_TREE,
5067 empty_base_offsets);
5068 /* If the bit-field had no name originally, remove the name
5071 DECL_NAME (field) = NULL_TREE;
5073 /* Remember the location of any empty classes in FIELD. */
5074 if (abi_version_at_least (2))
5075 record_subobject_offsets (TREE_TYPE (field),
5076 byte_position(field),
5080 /* If a bit-field does not immediately follow another bit-field,
5081 and yet it starts in the middle of a byte, we have failed to
5082 comply with the ABI. */
5084 && DECL_C_BIT_FIELD (field)
5085 && !last_field_was_bitfield
5086 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5087 DECL_FIELD_BIT_OFFSET (field),
5088 bitsize_unit_node)))
5089 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
5092 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5093 offset of the field. */
5095 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5096 byte_position (field))
5097 && contains_empty_class_p (TREE_TYPE (field)))
5098 cp_warning_at ("`%D' contains empty classes which may cause base "
5099 "classes to be placed at different locations in a "
5100 "future version of GCC",
5103 /* If we needed additional padding after this field, add it
5109 padding_field = build_decl (FIELD_DECL,
5112 DECL_BIT_FIELD (padding_field) = 1;
5113 DECL_SIZE (padding_field) = padding;
5114 DECL_ALIGN (padding_field) = 1;
5115 DECL_USER_ALIGN (padding_field) = 0;
5116 layout_nonempty_base_or_field (rli, padding_field,
5118 empty_base_offsets);
5121 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5124 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5126 /* Make sure that we are on a byte boundary so that the size of
5127 the class without virtual bases will always be a round number
5129 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
5130 normalize_rli (rli);
5133 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5135 if (!abi_version_at_least (2))
5136 include_empty_classes(rli);
5138 /* Delete all zero-width bit-fields from the list of fields. Now
5139 that the type is laid out they are no longer important. */
5140 remove_zero_width_bit_fields (t);
5142 /* Create the version of T used for virtual bases. We do not use
5143 make_aggr_type for this version; this is an artificial type. For
5144 a POD type, we just reuse T. */
5145 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5147 base_t = make_node (TREE_CODE (t));
5149 /* Set the size and alignment for the new type. In G++ 3.2, all
5150 empty classes were considered to have size zero when used as
5152 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5154 TYPE_SIZE (base_t) = bitsize_zero_node;
5155 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5156 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5157 warning ("layout of classes derived from empty class `%T' "
5158 "may change in a future version of GCC",
5163 TYPE_SIZE_UNIT (base_t)
5164 = size_binop (MAX_EXPR,
5165 rli_size_unit_so_far (rli),
5166 end_of_class (t, /*include_virtuals_p=*/0));
5168 = size_binop (MAX_EXPR,
5169 rli_size_so_far (rli),
5170 size_binop (MULT_EXPR,
5171 convert (bitsizetype,
5172 TYPE_SIZE_UNIT (base_t)),
5173 bitsize_int (BITS_PER_UNIT)));
5175 TYPE_ALIGN (base_t) = rli->record_align;
5176 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5178 /* Copy the fields from T. */
5179 next_field = &TYPE_FIELDS (base_t);
5180 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5181 if (TREE_CODE (field) == FIELD_DECL)
5183 *next_field = build_decl (FIELD_DECL,
5186 DECL_CONTEXT (*next_field) = base_t;
5187 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5188 DECL_FIELD_BIT_OFFSET (*next_field)
5189 = DECL_FIELD_BIT_OFFSET (field);
5190 next_field = &TREE_CHAIN (*next_field);
5193 /* Record the base version of the type. */
5194 CLASSTYPE_AS_BASE (t) = base_t;
5195 TYPE_CONTEXT (base_t) = t;
5198 CLASSTYPE_AS_BASE (t) = t;
5200 /* Every empty class contains an empty class. */
5201 if (CLASSTYPE_EMPTY_P (t))
5202 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5204 /* Set the TYPE_DECL for this type to contain the right
5205 value for DECL_OFFSET, so that we can use it as part
5206 of a COMPONENT_REF for multiple inheritance. */
5207 layout_decl (TYPE_MAIN_DECL (t), 0);
5209 /* Now fix up any virtual base class types that we left lying
5210 around. We must get these done before we try to lay out the
5211 virtual function table. As a side-effect, this will remove the
5212 base subobject fields. */
5213 layout_virtual_bases (rli, empty_base_offsets);
5215 /* Make sure that empty classes are reflected in RLI at this
5217 include_empty_classes(rli);
5219 /* Make sure not to create any structures with zero size. */
5220 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5222 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
5224 /* Let the back-end lay out the type. */
5225 finish_record_layout (rli, /*free_p=*/true);
5227 /* Warn about bases that can't be talked about due to ambiguity. */
5228 warn_about_ambiguous_bases (t);
5231 splay_tree_delete (empty_base_offsets);
5234 /* Returns the virtual function with which the vtable for TYPE is
5235 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
5238 key_method (tree type)
5242 if (TYPE_FOR_JAVA (type)
5243 || processing_template_decl
5244 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5245 || CLASSTYPE_INTERFACE_KNOWN (type))
5248 for (method = TYPE_METHODS (type); method != NULL_TREE;
5249 method = TREE_CHAIN (method))
5250 if (DECL_VINDEX (method) != NULL_TREE
5251 && ! DECL_DECLARED_INLINE_P (method)
5252 && ! DECL_PURE_VIRTUAL_P (method))
5258 /* Perform processing required when the definition of T (a class type)
5262 finish_struct_1 (tree t)
5265 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5266 tree virtuals = NULL_TREE;
5270 if (COMPLETE_TYPE_P (t))
5272 if (IS_AGGR_TYPE (t))
5273 error ("redefinition of `%#T'", t);
5280 /* If this type was previously laid out as a forward reference,
5281 make sure we lay it out again. */
5282 TYPE_SIZE (t) = NULL_TREE;
5283 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5284 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5286 fixup_inline_methods (t);
5288 /* Make assumptions about the class; we'll reset the flags if
5290 CLASSTYPE_EMPTY_P (t) = 1;
5291 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5292 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5294 /* Do end-of-class semantic processing: checking the validity of the
5295 bases and members and add implicitly generated methods. */
5296 check_bases_and_members (t);
5298 /* Find the key method */
5299 if (TYPE_CONTAINS_VPTR_P (t))
5301 CLASSTYPE_KEY_METHOD (t) = key_method (t);
5303 /* If a polymorphic class has no key method, we may emit the vtable
5304 in every translation unit where the class definition appears. */
5305 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5306 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5309 /* Layout the class itself. */
5310 layout_class_type (t, &virtuals);
5312 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5313 vfield = TYPE_VFIELD (t);
5314 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5316 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5318 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5319 BINFO_TYPE (primary)),
5321 /* The vtable better be at the start. */
5322 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5324 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5327 vfield = copy_decl (vfield);
5328 DECL_FIELD_CONTEXT (vfield) = t;
5329 TYPE_VFIELD (t) = vfield;
5332 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5334 virtuals = modify_all_vtables (t, nreverse (virtuals));
5336 /* If we created a new vtbl pointer for this class, add it to the
5338 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5339 CLASSTYPE_VFIELDS (t)
5340 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5342 /* If necessary, create the primary vtable for this class. */
5343 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5345 /* We must enter these virtuals into the table. */
5346 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5347 build_primary_vtable (NULL_TREE, t);
5348 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5349 /* Here we know enough to change the type of our virtual
5350 function table, but we will wait until later this function. */
5351 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5354 if (TYPE_CONTAINS_VPTR_P (t))
5359 if (TYPE_BINFO_VTABLE (t))
5360 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5362 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5363 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5366 /* Add entries for virtual functions introduced by this class. */
5367 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5369 /* Set DECL_VINDEX for all functions declared in this class. */
5370 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5372 fn = TREE_CHAIN (fn),
5373 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5374 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5376 tree fndecl = BV_FN (fn);
5378 if (DECL_THUNK_P (fndecl))
5379 /* A thunk. We should never be calling this entry directly
5380 from this vtable -- we'd use the entry for the non
5381 thunk base function. */
5382 DECL_VINDEX (fndecl) = NULL_TREE;
5383 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5384 DECL_VINDEX (fndecl) = build_shared_int_cst (vindex);
5388 finish_struct_bits (t);
5390 /* Complete the rtl for any static member objects of the type we're
5392 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5393 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5394 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5395 DECL_MODE (x) = TYPE_MODE (t);
5397 /* Done with FIELDS...now decide whether to sort these for
5398 faster lookups later.
5400 We use a small number because most searches fail (succeeding
5401 ultimately as the search bores through the inheritance
5402 hierarchy), and we want this failure to occur quickly. */
5404 n_fields = count_fields (TYPE_FIELDS (t));
5407 tree field_vec = make_tree_vec (n_fields);
5408 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5409 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5411 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5412 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5413 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5416 if (TYPE_HAS_CONSTRUCTOR (t))
5418 tree vfields = CLASSTYPE_VFIELDS (t);
5420 for (vfields = CLASSTYPE_VFIELDS (t);
5421 vfields; vfields = TREE_CHAIN (vfields))
5422 /* Mark the fact that constructor for T could affect anybody
5423 inheriting from T who wants to initialize vtables for
5425 if (VF_BINFO_VALUE (vfields))
5426 TREE_ADDRESSABLE (vfields) = 1;
5429 /* Make the rtl for any new vtables we have created, and unmark
5430 the base types we marked. */
5433 /* Build the VTT for T. */
5436 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5437 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5438 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5442 if (warn_overloaded_virtual)
5445 maybe_suppress_debug_info (t);
5447 dump_class_hierarchy (t);
5449 /* Finish debugging output for this type. */
5450 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5453 /* When T was built up, the member declarations were added in reverse
5454 order. Rearrange them to declaration order. */
5457 unreverse_member_declarations (tree t)
5463 /* The following lists are all in reverse order. Put them in
5464 declaration order now. */
5465 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5466 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5467 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5469 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5470 reverse order, so we can't just use nreverse. */
5472 for (x = TYPE_FIELDS (t);
5473 x && TREE_CODE (x) != TYPE_DECL;
5476 next = TREE_CHAIN (x);
5477 TREE_CHAIN (x) = prev;
5482 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5484 TYPE_FIELDS (t) = prev;
5489 finish_struct (tree t, tree attributes)
5491 const char *saved_filename = input_filename;
5492 int saved_lineno = lineno;
5494 /* Now that we've got all the field declarations, reverse everything
5496 unreverse_member_declarations (t);
5498 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5500 /* Nadger the current location so that diagnostics point to the start of
5501 the struct, not the end. */
5502 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5503 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5505 if (processing_template_decl)
5507 finish_struct_methods (t);
5508 TYPE_SIZE (t) = bitsize_zero_node;
5511 finish_struct_1 (t);
5513 input_filename = saved_filename;
5514 lineno = saved_lineno;
5516 TYPE_BEING_DEFINED (t) = 0;
5518 if (current_class_type)
5521 error ("trying to finish struct, but kicked out due to previous parse errors");
5523 if (processing_template_decl && at_function_scope_p ())
5524 add_stmt (build_min (TAG_DEFN, t));
5529 /* Return the dynamic type of INSTANCE, if known.
5530 Used to determine whether the virtual function table is needed
5533 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5534 of our knowledge of its type. *NONNULL should be initialized
5535 before this function is called. */
5538 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5540 switch (TREE_CODE (instance))
5543 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5546 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5550 /* This is a call to a constructor, hence it's never zero. */
5551 if (TREE_HAS_CONSTRUCTOR (instance))
5555 return TREE_TYPE (instance);
5560 /* This is a call to a constructor, hence it's never zero. */
5561 if (TREE_HAS_CONSTRUCTOR (instance))
5565 return TREE_TYPE (instance);
5567 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5574 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5575 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5576 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5577 /* Propagate nonnull. */
5578 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5583 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5588 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5591 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5595 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5596 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5600 return TREE_TYPE (TREE_TYPE (instance));
5602 /* fall through... */
5606 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5610 return TREE_TYPE (instance);
5612 else if (instance == current_class_ptr)
5617 /* if we're in a ctor or dtor, we know our type. */
5618 if (DECL_LANG_SPECIFIC (current_function_decl)
5619 && (DECL_CONSTRUCTOR_P (current_function_decl)
5620 || DECL_DESTRUCTOR_P (current_function_decl)))
5624 return TREE_TYPE (TREE_TYPE (instance));
5627 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5629 /* Reference variables should be references to objects. */
5633 if (TREE_CODE (instance) == VAR_DECL
5634 && DECL_INITIAL (instance))
5635 return fixed_type_or_null (DECL_INITIAL (instance),
5645 /* Return nonzero if the dynamic type of INSTANCE is known, and
5646 equivalent to the static type. We also handle the case where
5647 INSTANCE is really a pointer. Return negative if this is a
5648 ctor/dtor. There the dynamic type is known, but this might not be
5649 the most derived base of the original object, and hence virtual
5650 bases may not be layed out according to this type.
5652 Used to determine whether the virtual function table is needed
5655 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5656 of our knowledge of its type. *NONNULL should be initialized
5657 before this function is called. */
5660 resolves_to_fixed_type_p (tree instance, int* nonnull)
5662 tree t = TREE_TYPE (instance);
5665 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5666 if (fixed == NULL_TREE)
5668 if (POINTER_TYPE_P (t))
5670 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5672 return cdtorp ? -1 : 1;
5677 init_class_processing (void)
5679 current_class_depth = 0;
5680 current_class_stack_size = 10;
5682 = (class_stack_node_t) xmalloc (current_class_stack_size
5683 * sizeof (struct class_stack_node));
5684 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5686 access_default_node = build_int_2 (0, 0);
5687 access_public_node = build_int_2 (ak_public, 0);
5688 access_protected_node = build_int_2 (ak_protected, 0);
5689 access_private_node = build_int_2 (ak_private, 0);
5690 access_default_virtual_node = build_int_2 (4, 0);
5691 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5692 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5693 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5695 ridpointers[(int) RID_PUBLIC] = access_public_node;
5696 ridpointers[(int) RID_PRIVATE] = access_private_node;
5697 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5700 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5701 appropriate for TYPE.
5703 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5704 which can be seen locally to the class. They are shadowed by
5705 any subsequent local declaration (including parameter names).
5707 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5708 which have static meaning (i.e., static members, static
5709 member functions, enum declarations, etc).
5711 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5712 which can be seen locally to the class (as in 1), but
5713 know that we are doing this for declaration purposes
5714 (i.e. friend foo::bar (int)).
5716 So that we may avoid calls to lookup_name, we cache the _TYPE
5717 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5719 For multiple inheritance, we perform a two-pass depth-first search
5720 of the type lattice. The first pass performs a pre-order search,
5721 marking types after the type has had its fields installed in
5722 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5723 unmarks the marked types. If a field or member function name
5724 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5725 that name becomes `error_mark_node'. */
5728 pushclass (tree type, int modify)
5730 type = TYPE_MAIN_VARIANT (type);
5732 /* Make sure there is enough room for the new entry on the stack. */
5733 if (current_class_depth + 1 >= current_class_stack_size)
5735 current_class_stack_size *= 2;
5737 = (class_stack_node_t) xrealloc (current_class_stack,
5738 current_class_stack_size
5739 * sizeof (struct class_stack_node));
5742 /* Insert a new entry on the class stack. */
5743 current_class_stack[current_class_depth].name = current_class_name;
5744 current_class_stack[current_class_depth].type = current_class_type;
5745 current_class_stack[current_class_depth].access = current_access_specifier;
5746 current_class_stack[current_class_depth].names_used = 0;
5747 current_class_depth++;
5749 /* Now set up the new type. */
5750 current_class_name = TYPE_NAME (type);
5751 if (TREE_CODE (current_class_name) == TYPE_DECL)
5752 current_class_name = DECL_NAME (current_class_name);
5753 current_class_type = type;
5755 /* By default, things in classes are private, while things in
5756 structures or unions are public. */
5757 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5758 ? access_private_node
5759 : access_public_node);
5761 if (previous_class_type != NULL_TREE
5762 && (type != previous_class_type
5763 || !COMPLETE_TYPE_P (previous_class_type))
5764 && current_class_depth == 1)
5766 /* Forcibly remove any old class remnants. */
5767 invalidate_class_lookup_cache ();
5770 /* If we're about to enter a nested class, clear
5771 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5772 if (modify && current_class_depth > 1)
5773 clear_identifier_class_values ();
5779 if (type != previous_class_type || current_class_depth > 1)
5780 push_class_decls (type);
5785 /* We are re-entering the same class we just left, so we
5786 don't have to search the whole inheritance matrix to find
5787 all the decls to bind again. Instead, we install the
5788 cached class_shadowed list, and walk through it binding
5789 names and setting up IDENTIFIER_TYPE_VALUEs. */
5790 set_class_shadows (previous_class_values);
5791 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5793 tree id = TREE_PURPOSE (item);
5794 tree decl = TREE_TYPE (item);
5796 push_class_binding (id, decl);
5797 if (TREE_CODE (decl) == TYPE_DECL)
5798 set_identifier_type_value (id, TREE_TYPE (decl));
5800 unuse_fields (type);
5803 storetags (CLASSTYPE_TAGS (type));
5807 /* When we exit a toplevel class scope, we save the
5808 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5809 reenter the class. Here, we've entered some other class, so we
5810 must invalidate our cache. */
5813 invalidate_class_lookup_cache (void)
5817 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5818 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5819 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5821 previous_class_values = NULL_TREE;
5822 previous_class_type = NULL_TREE;
5825 /* Get out of the current class scope. If we were in a class scope
5826 previously, that is the one popped to. */
5834 current_class_depth--;
5835 current_class_name = current_class_stack[current_class_depth].name;
5836 current_class_type = current_class_stack[current_class_depth].type;
5837 current_access_specifier = current_class_stack[current_class_depth].access;
5838 if (current_class_stack[current_class_depth].names_used)
5839 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5842 /* Returns 1 if current_class_type is either T or a nested type of T.
5843 We start looking from 1 because entry 0 is from global scope, and has
5847 currently_open_class (tree t)
5850 if (t == current_class_type)
5852 for (i = 1; i < current_class_depth; ++i)
5853 if (current_class_stack [i].type == t)
5858 /* If either current_class_type or one of its enclosing classes are derived
5859 from T, return the appropriate type. Used to determine how we found
5860 something via unqualified lookup. */
5863 currently_open_derived_class (tree t)
5867 /* The bases of a dependent type are unknown. */
5868 if (dependent_type_p (t))
5871 if (DERIVED_FROM_P (t, current_class_type))
5872 return current_class_type;
5874 for (i = current_class_depth - 1; i > 0; --i)
5875 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5876 return current_class_stack[i].type;
5881 /* When entering a class scope, all enclosing class scopes' names with
5882 static meaning (static variables, static functions, types and enumerators)
5883 have to be visible. This recursive function calls pushclass for all
5884 enclosing class contexts until global or a local scope is reached.
5885 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5886 formal of the same name. */
5889 push_nested_class (tree type, int modify)
5893 /* A namespace might be passed in error cases, like A::B:C. */
5894 if (type == NULL_TREE
5895 || type == error_mark_node
5896 || TREE_CODE (type) == NAMESPACE_DECL
5897 || ! IS_AGGR_TYPE (type)
5898 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5899 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5902 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5904 if (context && CLASS_TYPE_P (context))
5905 push_nested_class (context, 2);
5906 pushclass (type, modify);
5909 /* Undoes a push_nested_class call. */
5912 pop_nested_class (void)
5914 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5917 if (context && CLASS_TYPE_P (context))
5918 pop_nested_class ();
5921 /* Returns the number of extern "LANG" blocks we are nested within. */
5924 current_lang_depth (void)
5926 return VARRAY_ACTIVE_SIZE (current_lang_base);
5929 /* Set global variables CURRENT_LANG_NAME to appropriate value
5930 so that behavior of name-mangling machinery is correct. */
5933 push_lang_context (tree name)
5935 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5937 if (name == lang_name_cplusplus)
5939 current_lang_name = name;
5941 else if (name == lang_name_java)
5943 current_lang_name = name;
5944 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5945 (See record_builtin_java_type in decl.c.) However, that causes
5946 incorrect debug entries if these types are actually used.
5947 So we re-enable debug output after extern "Java". */
5948 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5949 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5950 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5951 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5952 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5953 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5954 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5955 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5957 else if (name == lang_name_c)
5959 current_lang_name = name;
5962 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5965 /* Get out of the current language scope. */
5968 pop_lang_context (void)
5970 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5971 VARRAY_POP (current_lang_base);
5974 /* Type instantiation routines. */
5976 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5977 matches the TARGET_TYPE. If there is no satisfactory match, return
5978 error_mark_node, and issue an error message if COMPLAIN is
5979 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5980 If TEMPLATE_ONLY, the name of the overloaded function
5981 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5982 template arguments. */
5985 resolve_address_of_overloaded_function (tree target_type,
5990 tree explicit_targs)
5992 /* Here's what the standard says:
5996 If the name is a function template, template argument deduction
5997 is done, and if the argument deduction succeeds, the deduced
5998 arguments are used to generate a single template function, which
5999 is added to the set of overloaded functions considered.
6001 Non-member functions and static member functions match targets of
6002 type "pointer-to-function" or "reference-to-function." Nonstatic
6003 member functions match targets of type "pointer-to-member
6004 function;" the function type of the pointer to member is used to
6005 select the member function from the set of overloaded member
6006 functions. If a nonstatic member function is selected, the
6007 reference to the overloaded function name is required to have the
6008 form of a pointer to member as described in 5.3.1.
6010 If more than one function is selected, any template functions in
6011 the set are eliminated if the set also contains a non-template
6012 function, and any given template function is eliminated if the
6013 set contains a second template function that is more specialized
6014 than the first according to the partial ordering rules 14.5.5.2.
6015 After such eliminations, if any, there shall remain exactly one
6016 selected function. */
6019 int is_reference = 0;
6020 /* We store the matches in a TREE_LIST rooted here. The functions
6021 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6022 interoperability with most_specialized_instantiation. */
6023 tree matches = NULL_TREE;
6026 /* By the time we get here, we should be seeing only real
6027 pointer-to-member types, not the internal POINTER_TYPE to
6028 METHOD_TYPE representation. */
6029 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
6030 && (TREE_CODE (TREE_TYPE (target_type))
6031 == METHOD_TYPE)), 0);
6033 if (TREE_CODE (overload) == COMPONENT_REF)
6034 overload = TREE_OPERAND (overload, 1);
6036 /* Check that the TARGET_TYPE is reasonable. */
6037 if (TYPE_PTRFN_P (target_type))
6039 else if (TYPE_PTRMEMFUNC_P (target_type))
6040 /* This is OK, too. */
6042 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6044 /* This is OK, too. This comes from a conversion to reference
6046 target_type = build_reference_type (target_type);
6053 cannot resolve overloaded function `%D' based on conversion to type `%T'",
6054 DECL_NAME (OVL_FUNCTION (overload)), target_type);
6055 return error_mark_node;
6058 /* If we can find a non-template function that matches, we can just
6059 use it. There's no point in generating template instantiations
6060 if we're just going to throw them out anyhow. But, of course, we
6061 can only do this when we don't *need* a template function. */
6066 for (fns = overload; fns; fns = OVL_NEXT (fns))
6068 tree fn = OVL_CURRENT (fns);
6071 if (TREE_CODE (fn) == TEMPLATE_DECL)
6072 /* We're not looking for templates just yet. */
6075 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6077 /* We're looking for a non-static member, and this isn't
6078 one, or vice versa. */
6081 /* See if there's a match. */
6082 fntype = TREE_TYPE (fn);
6084 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6085 else if (!is_reference)
6086 fntype = build_pointer_type (fntype);
6088 if (can_convert_arg (target_type, fntype, fn))
6089 matches = tree_cons (fn, NULL_TREE, matches);
6093 /* Now, if we've already got a match (or matches), there's no need
6094 to proceed to the template functions. But, if we don't have a
6095 match we need to look at them, too. */
6098 tree target_fn_type;
6099 tree target_arg_types;
6100 tree target_ret_type;
6105 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6107 target_fn_type = TREE_TYPE (target_type);
6108 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6109 target_ret_type = TREE_TYPE (target_fn_type);
6111 /* Never do unification on the 'this' parameter. */
6112 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
6113 target_arg_types = TREE_CHAIN (target_arg_types);
6115 for (fns = overload; fns; fns = OVL_NEXT (fns))
6117 tree fn = OVL_CURRENT (fns);
6119 tree instantiation_type;
6122 if (TREE_CODE (fn) != TEMPLATE_DECL)
6123 /* We're only looking for templates. */
6126 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6128 /* We're not looking for a non-static member, and this is
6129 one, or vice versa. */
6132 /* Try to do argument deduction. */
6133 targs = make_tree_vec (DECL_NTPARMS (fn));
6134 if (fn_type_unification (fn, explicit_targs, targs,
6135 target_arg_types, target_ret_type,
6136 DEDUCE_EXACT, -1) != 0)
6137 /* Argument deduction failed. */
6140 /* Instantiate the template. */
6141 instantiation = instantiate_template (fn, targs);
6142 if (instantiation == error_mark_node)
6143 /* Instantiation failed. */
6146 /* See if there's a match. */
6147 instantiation_type = TREE_TYPE (instantiation);
6149 instantiation_type =
6150 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6151 else if (!is_reference)
6152 instantiation_type = build_pointer_type (instantiation_type);
6153 if (can_convert_arg (target_type, instantiation_type, instantiation))
6154 matches = tree_cons (instantiation, fn, matches);
6157 /* Now, remove all but the most specialized of the matches. */
6160 tree match = most_specialized_instantiation (matches);
6162 if (match != error_mark_node)
6163 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6167 /* Now we should have exactly one function in MATCHES. */
6168 if (matches == NULL_TREE)
6170 /* There were *no* matches. */
6173 error ("no matches converting function `%D' to type `%#T'",
6174 DECL_NAME (OVL_FUNCTION (overload)),
6177 /* print_candidates expects a chain with the functions in
6178 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6179 so why be clever?). */
6180 for (; overload; overload = OVL_NEXT (overload))
6181 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6184 print_candidates (matches);
6186 return error_mark_node;
6188 else if (TREE_CHAIN (matches))
6190 /* There were too many matches. */
6196 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6197 DECL_NAME (OVL_FUNCTION (overload)),
6200 /* Since print_candidates expects the functions in the
6201 TREE_VALUE slot, we flip them here. */
6202 for (match = matches; match; match = TREE_CHAIN (match))
6203 TREE_VALUE (match) = TREE_PURPOSE (match);
6205 print_candidates (matches);
6208 return error_mark_node;
6211 /* Good, exactly one match. Now, convert it to the correct type. */
6212 fn = TREE_PURPOSE (matches);
6214 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6215 && !ptrmem && !flag_ms_extensions)
6217 static int explained;
6220 return error_mark_node;
6222 pedwarn ("assuming pointer to member `%D'", fn);
6225 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6231 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6232 return build_unary_op (ADDR_EXPR, fn, 0);
6235 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6236 will mark the function as addressed, but here we must do it
6238 cxx_mark_addressable (fn);
6244 /* This function will instantiate the type of the expression given in
6245 RHS to match the type of LHSTYPE. If errors exist, then return
6246 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6247 we complain on errors. If we are not complaining, never modify rhs,
6248 as overload resolution wants to try many possible instantiations, in
6249 the hope that at least one will work.
6251 For non-recursive calls, LHSTYPE should be a function, pointer to
6252 function, or a pointer to member function. */
6255 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
6257 int complain = (flags & tf_error);
6258 int strict = (flags & tf_no_attributes)
6259 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6260 int allow_ptrmem = flags & tf_ptrmem_ok;
6262 flags &= ~tf_ptrmem_ok;
6264 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6267 error ("not enough type information");
6268 return error_mark_node;
6271 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6273 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6275 if (flag_ms_extensions
6276 && TYPE_PTRMEMFUNC_P (lhstype)
6277 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6278 /* Microsoft allows `A::f' to be resolved to a
6279 pointer-to-member. */
6284 error ("argument of type `%T' does not match `%T'",
6285 TREE_TYPE (rhs), lhstype);
6286 return error_mark_node;
6290 if (TREE_CODE (rhs) == BASELINK)
6291 rhs = BASELINK_FUNCTIONS (rhs);
6293 /* We don't overwrite rhs if it is an overloaded function.
6294 Copying it would destroy the tree link. */
6295 if (TREE_CODE (rhs) != OVERLOAD)
6296 rhs = copy_node (rhs);
6298 /* This should really only be used when attempting to distinguish
6299 what sort of a pointer to function we have. For now, any
6300 arithmetic operation which is not supported on pointers
6301 is rejected as an error. */
6303 switch (TREE_CODE (rhs))
6311 return error_mark_node;
6318 new_rhs = instantiate_type (build_pointer_type (lhstype),
6319 TREE_OPERAND (rhs, 0), flags);
6320 if (new_rhs == error_mark_node)
6321 return error_mark_node;
6323 TREE_TYPE (rhs) = lhstype;
6324 TREE_OPERAND (rhs, 0) = new_rhs;
6329 rhs = copy_node (TREE_OPERAND (rhs, 0));
6330 TREE_TYPE (rhs) = unknown_type_node;
6331 return instantiate_type (lhstype, rhs, flags);
6334 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6337 rhs = TREE_OPERAND (rhs, 1);
6338 if (BASELINK_P (rhs))
6339 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6340 flags | allow_ptrmem);
6342 /* This can happen if we are forming a pointer-to-member for a
6344 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6348 case TEMPLATE_ID_EXPR:
6350 tree fns = TREE_OPERAND (rhs, 0);
6351 tree args = TREE_OPERAND (rhs, 1);
6354 resolve_address_of_overloaded_function (lhstype,
6358 /*template_only=*/1,
6365 resolve_address_of_overloaded_function (lhstype,
6369 /*template_only=*/0,
6370 /*explicit_targs=*/NULL_TREE);
6373 /* Now we should have a baselink. */
6374 my_friendly_assert (BASELINK_P (rhs), 990412);
6376 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6379 /* This is too hard for now. */
6381 return error_mark_node;
6386 TREE_OPERAND (rhs, 0)
6387 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6388 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6389 return error_mark_node;
6390 TREE_OPERAND (rhs, 1)
6391 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6392 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6393 return error_mark_node;
6395 TREE_TYPE (rhs) = lhstype;
6399 case TRUNC_DIV_EXPR:
6400 case FLOOR_DIV_EXPR:
6402 case ROUND_DIV_EXPR:
6404 case TRUNC_MOD_EXPR:
6405 case FLOOR_MOD_EXPR:
6407 case ROUND_MOD_EXPR:
6408 case FIX_ROUND_EXPR:
6409 case FIX_FLOOR_EXPR:
6411 case FIX_TRUNC_EXPR:
6427 case PREINCREMENT_EXPR:
6428 case PREDECREMENT_EXPR:
6429 case POSTINCREMENT_EXPR:
6430 case POSTDECREMENT_EXPR:
6432 error ("invalid operation on uninstantiated type");
6433 return error_mark_node;
6435 case TRUTH_AND_EXPR:
6437 case TRUTH_XOR_EXPR:
6444 case TRUTH_ANDIF_EXPR:
6445 case TRUTH_ORIF_EXPR:
6446 case TRUTH_NOT_EXPR:
6448 error ("not enough type information");
6449 return error_mark_node;
6452 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6455 error ("not enough type information");
6456 return error_mark_node;
6458 TREE_OPERAND (rhs, 1)
6459 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6460 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6461 return error_mark_node;
6462 TREE_OPERAND (rhs, 2)
6463 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6464 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6465 return error_mark_node;
6467 TREE_TYPE (rhs) = lhstype;
6471 TREE_OPERAND (rhs, 1)
6472 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6473 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6474 return error_mark_node;
6476 TREE_TYPE (rhs) = lhstype;
6481 if (PTRMEM_OK_P (rhs))
6482 flags |= tf_ptrmem_ok;
6484 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6486 case ENTRY_VALUE_EXPR:
6488 return error_mark_node;
6491 return error_mark_node;
6495 return error_mark_node;
6499 /* Return the name of the virtual function pointer field
6500 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6501 this may have to look back through base types to find the
6502 ultimate field name. (For single inheritance, these could
6503 all be the same name. Who knows for multiple inheritance). */
6506 get_vfield_name (tree type)
6508 tree binfo = TYPE_BINFO (type);
6511 while (BINFO_BASETYPES (binfo)
6512 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6513 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6514 binfo = BINFO_BASETYPE (binfo, 0);
6516 type = BINFO_TYPE (binfo);
6517 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6518 + TYPE_NAME_LENGTH (type) + 2);
6519 sprintf (buf, VFIELD_NAME_FORMAT,
6520 IDENTIFIER_POINTER (constructor_name (type)));
6521 return get_identifier (buf);
6525 print_class_statistics (void)
6527 #ifdef GATHER_STATISTICS
6528 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6529 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6530 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6531 n_build_method_call, n_inner_fields_searched);
6534 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6535 n_vtables, n_vtable_searches);
6536 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6537 n_vtable_entries, n_vtable_elems);
6542 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6543 according to [class]:
6544 The class-name is also inserted
6545 into the scope of the class itself. For purposes of access checking,
6546 the inserted class name is treated as if it were a public member name. */
6549 build_self_reference (void)
6551 tree name = constructor_name (current_class_type);
6552 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6555 DECL_NONLOCAL (value) = 1;
6556 DECL_CONTEXT (value) = current_class_type;
6557 DECL_ARTIFICIAL (value) = 1;
6559 if (processing_template_decl)
6560 value = push_template_decl (value);
6562 saved_cas = current_access_specifier;
6563 current_access_specifier = access_public_node;
6564 finish_member_declaration (value);
6565 current_access_specifier = saved_cas;
6568 /* Returns 1 if TYPE contains only padding bytes. */
6571 is_empty_class (tree type)
6573 if (type == error_mark_node)
6576 if (! IS_AGGR_TYPE (type))
6579 /* In G++ 3.2, whether or not a class was empty was determined by
6580 looking at its size. */
6581 if (abi_version_at_least (2))
6582 return CLASSTYPE_EMPTY_P (type);
6584 return integer_zerop (CLASSTYPE_SIZE (type));
6587 /* Returns true if TYPE contains an empty class. */
6590 contains_empty_class_p (tree type)
6592 if (is_empty_class (type))
6594 if (CLASS_TYPE_P (type))
6599 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6600 if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6602 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6603 if (TREE_CODE (field) == FIELD_DECL
6604 && !DECL_ARTIFICIAL (field)
6605 && is_empty_class (TREE_TYPE (field)))
6608 else if (TREE_CODE (type) == ARRAY_TYPE)
6609 return contains_empty_class_p (TREE_TYPE (type));
6613 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6614 a *_TYPE node. NODE can also be a local class. */
6617 get_enclosing_class (tree type)
6621 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6623 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6626 node = DECL_CONTEXT (node);
6632 node = TYPE_CONTEXT (node);
6642 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6645 is_base_of_enclosing_class (tree base, tree type)
6649 if (lookup_base (type, base, ba_any, NULL))
6652 type = get_enclosing_class (type);
6657 /* Note that NAME was looked up while the current class was being
6658 defined and that the result of that lookup was DECL. */
6661 maybe_note_name_used_in_class (tree name, tree decl)
6663 splay_tree names_used;
6665 /* If we're not defining a class, there's nothing to do. */
6666 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6669 /* If there's already a binding for this NAME, then we don't have
6670 anything to worry about. */
6671 if (IDENTIFIER_CLASS_VALUE (name))
6674 if (!current_class_stack[current_class_depth - 1].names_used)
6675 current_class_stack[current_class_depth - 1].names_used
6676 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6677 names_used = current_class_stack[current_class_depth - 1].names_used;
6679 splay_tree_insert (names_used,
6680 (splay_tree_key) name,
6681 (splay_tree_value) decl);
6684 /* Note that NAME was declared (as DECL) in the current class. Check
6685 to see that the declaration is valid. */
6688 note_name_declared_in_class (tree name, tree decl)
6690 splay_tree names_used;
6693 /* Look to see if we ever used this name. */
6695 = current_class_stack[current_class_depth - 1].names_used;
6699 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6702 /* [basic.scope.class]
6704 A name N used in a class S shall refer to the same declaration
6705 in its context and when re-evaluated in the completed scope of
6707 error ("declaration of `%#D'", decl);
6708 cp_error_at ("changes meaning of `%D' from `%+#D'",
6709 DECL_NAME (OVL_CURRENT (decl)),
6714 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6715 Secondary vtables are merged with primary vtables; this function
6716 will return the VAR_DECL for the primary vtable. */
6719 get_vtbl_decl_for_binfo (tree binfo)
6723 decl = BINFO_VTABLE (binfo);
6724 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6726 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6728 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6731 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6735 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6736 who's TREE_PURPOSE is the TYPE of the required primary base and
6737 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6740 dfs_get_primary_binfo (tree binfo, void* data)
6742 tree cons = (tree) data;
6743 tree primary_base = TREE_PURPOSE (cons);
6745 if (TREE_VIA_VIRTUAL (binfo)
6746 && same_type_p (BINFO_TYPE (binfo), primary_base))
6747 /* This is the right type of binfo, but it might be an unshared
6748 instance, and the shared instance is later in the dfs walk. We
6749 must keep looking. */
6750 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6755 /* Returns the unshared binfo for the primary base of BINFO. Note
6756 that in a complex hierarchy the resulting BINFO may not actually
6757 *be* primary. In particular if the resulting BINFO is a virtual
6758 base, and it occurs elsewhere in the hierarchy, then this
6759 occurrence may not actually be a primary base in the complete
6760 object. Check BINFO_PRIMARY_P to be sure. */
6763 get_primary_binfo (tree binfo)
6766 tree result = NULL_TREE;
6769 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6773 /* A non-virtual primary base is always a direct base, and easy to
6775 if (!TREE_VIA_VIRTUAL (primary_base))
6779 /* Scan the direct basetypes until we find a base with the same
6780 type as the primary base. */
6781 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6783 tree base_binfo = BINFO_BASETYPE (binfo, i);
6785 if (same_type_p (BINFO_TYPE (base_binfo),
6786 BINFO_TYPE (primary_base)))
6790 /* We should always find the primary base. */
6794 /* For a primary virtual base, we have to scan the entire hierarchy
6795 rooted at BINFO; the virtual base could be an indirect virtual
6796 base. There could be more than one instance of the primary base
6797 in the hierarchy, and if one is the canonical binfo we want that
6798 one. If it exists, it should be the first one we find, but as a
6799 consistency check we find them all and make sure. */
6800 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6801 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6802 virtuals = TREE_VALUE (virtuals);
6804 /* We must have found at least one instance. */
6805 my_friendly_assert (virtuals, 20010612);
6807 if (TREE_CHAIN (virtuals))
6809 /* We found more than one instance of the base. If one is the
6810 canonical one, choose that one. */
6811 tree complete_binfo;
6814 for (complete_binfo = binfo;
6815 BINFO_INHERITANCE_CHAIN (complete_binfo);
6816 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6818 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6819 BINFO_TYPE (complete_binfo));
6821 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6823 result = TREE_VALUE (virtuals);
6825 if (canonical == result)
6830 result = TREE_VALUE (virtuals);
6834 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6837 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6840 fprintf (stream, "%*s", indent, "");
6844 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6845 dominated by T) to stderr. INDENT should be zero when called from
6846 the top level; it is incremented recursively. */
6849 dump_class_hierarchy_r (FILE * stream,
6858 indented = maybe_indent_hierarchy (stream, indent, 0);
6859 fprintf (stream, "%s (0x%lx) ",
6860 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6861 (unsigned long) binfo);
6862 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6863 tree_low_cst (BINFO_OFFSET (binfo), 0));
6864 if (is_empty_class (BINFO_TYPE (binfo)))
6865 fprintf (stream, " empty");
6866 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6867 fprintf (stream, " nearly-empty");
6868 if (TREE_VIA_VIRTUAL (binfo))
6870 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6872 fprintf (stream, " virtual");
6873 if (canonical == binfo)
6874 fprintf (stream, " canonical");
6876 fprintf (stream, " non-canonical");
6878 fprintf (stream, "\n");
6881 if (BINFO_PRIMARY_BASE_OF (binfo))
6883 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6884 fprintf (stream, " primary-for %s (0x%lx)",
6885 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6886 TFF_PLAIN_IDENTIFIER),
6887 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6889 if (BINFO_LOST_PRIMARY_P (binfo))
6891 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6892 fprintf (stream, " lost-primary");
6895 fprintf (stream, "\n");
6897 if (!(flags & TDF_SLIM))
6901 if (BINFO_SUBVTT_INDEX (binfo))
6903 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6904 fprintf (stream, " subvttidx=%s",
6905 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6906 TFF_PLAIN_IDENTIFIER));
6908 if (BINFO_VPTR_INDEX (binfo))
6910 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6911 fprintf (stream, " vptridx=%s",
6912 expr_as_string (BINFO_VPTR_INDEX (binfo),
6913 TFF_PLAIN_IDENTIFIER));
6915 if (BINFO_VPTR_FIELD (binfo))
6917 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6918 fprintf (stream, " vbaseoffset=%s",
6919 expr_as_string (BINFO_VPTR_FIELD (binfo),
6920 TFF_PLAIN_IDENTIFIER));
6922 if (BINFO_VTABLE (binfo))
6924 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6925 fprintf (stream, " vptr=%s",
6926 expr_as_string (BINFO_VTABLE (binfo),
6927 TFF_PLAIN_IDENTIFIER));
6931 fprintf (stream, "\n");
6935 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6936 dump_class_hierarchy_r (stream, flags,
6937 t, BINFO_BASETYPE (binfo, i),
6941 /* Dump the BINFO hierarchy for T. */
6944 dump_class_hierarchy (tree t)
6947 FILE *stream = dump_begin (TDI_class, &flags);
6952 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6953 fprintf (stream, " size=%lu align=%lu\n",
6954 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6955 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6956 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6957 fprintf (stream, "\n");
6958 dump_end (TDI_class, stream);
6962 dump_array (FILE * stream, tree decl)
6967 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6969 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6971 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6972 fprintf (stream, " %s entries",
6973 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6974 TFF_PLAIN_IDENTIFIER));
6975 fprintf (stream, "\n");
6977 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6978 inits; ix++, inits = TREE_CHAIN (inits))
6979 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6980 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6984 dump_vtable (tree t, tree binfo, tree vtable)
6987 FILE *stream = dump_begin (TDI_class, &flags);
6992 if (!(flags & TDF_SLIM))
6994 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6996 fprintf (stream, "%s for %s",
6997 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6998 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
7001 if (!TREE_VIA_VIRTUAL (binfo))
7002 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7003 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7005 fprintf (stream, "\n");
7006 dump_array (stream, vtable);
7007 fprintf (stream, "\n");
7010 dump_end (TDI_class, stream);
7014 dump_vtt (tree t, tree vtt)
7017 FILE *stream = dump_begin (TDI_class, &flags);
7022 if (!(flags & TDF_SLIM))
7024 fprintf (stream, "VTT for %s\n",
7025 type_as_string (t, TFF_PLAIN_IDENTIFIER));
7026 dump_array (stream, vtt);
7027 fprintf (stream, "\n");
7030 dump_end (TDI_class, stream);
7033 /* Virtual function table initialization. */
7035 /* Create all the necessary vtables for T and its base classes. */
7038 finish_vtbls (tree t)
7044 /* We lay out the primary and secondary vtables in one contiguous
7045 vtable. The primary vtable is first, followed by the non-virtual
7046 secondary vtables in inheritance graph order. */
7047 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
7048 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
7049 TYPE_BINFO (t), t, list);
7051 /* Then come the virtual bases, also in inheritance graph order. */
7052 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7056 if (!TREE_VIA_VIRTUAL (vbase))
7059 /* Although we walk in inheritance order, that might not get the
7061 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
7063 accumulate_vtbl_inits (real_base, real_base,
7064 TYPE_BINFO (t), t, list);
7067 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
7068 base classes, for the benefit of the debugging backends. */
7069 for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
7071 tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
7072 if (TREE_VIA_VIRTUAL (base))
7074 vbase = binfo_for_vbase (BINFO_TYPE (base), t);
7075 BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
7079 if (TYPE_BINFO_VTABLE (t))
7080 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
7083 /* Initialize the vtable for BINFO with the INITS. */
7086 initialize_vtable (tree binfo, tree inits)
7090 layout_vtable_decl (binfo, list_length (inits));
7091 decl = get_vtbl_decl_for_binfo (binfo);
7092 initialize_array (decl, inits);
7093 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7096 /* Initialize DECL (a declaration for a namespace-scope array) with
7100 initialize_array (tree decl, tree inits)
7104 context = DECL_CONTEXT (decl);
7105 DECL_CONTEXT (decl) = NULL_TREE;
7106 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
7107 TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
7108 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
7109 DECL_CONTEXT (decl) = context;
7112 /* Build the VTT (virtual table table) for T.
7113 A class requires a VTT if it has virtual bases.
7116 1 - primary virtual pointer for complete object T
7117 2 - secondary VTTs for each direct non-virtual base of T which requires a
7119 3 - secondary virtual pointers for each direct or indirect base of T which
7120 has virtual bases or is reachable via a virtual path from T.
7121 4 - secondary VTTs for each direct or indirect virtual base of T.
7123 Secondary VTTs look like complete object VTTs without part 4. */
7133 /* Build up the initializers for the VTT. */
7135 index = size_zero_node;
7136 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7138 /* If we didn't need a VTT, we're done. */
7142 /* Figure out the type of the VTT. */
7143 type = build_index_type (size_int (list_length (inits) - 1));
7144 type = build_cplus_array_type (const_ptr_type_node, type);
7146 /* Now, build the VTT object itself. */
7147 vtt = build_vtable (t, get_vtt_name (t), type);
7148 initialize_array (vtt, inits);
7149 /* Add the VTT to the vtables list. */
7150 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
7151 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7156 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
7157 from within some hierarchy which is inherited from the type of BINFO.
7158 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
7162 get_original_base (tree base_binfo, tree binfo)
7167 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
7169 if (TREE_VIA_VIRTUAL (base_binfo))
7170 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7171 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7173 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7174 if (same_type_p (BINFO_TYPE (base_binfo),
7175 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7176 return BINFO_BASETYPE (derived, ix);
7181 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7182 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7183 and CHAIN the vtable pointer for this binfo after construction is
7184 complete. VALUE can also be another BINFO, in which case we recurse. */
7187 binfo_ctor_vtable (tree binfo)
7193 vt = BINFO_VTABLE (binfo);
7194 if (TREE_CODE (vt) == TREE_LIST)
7195 vt = TREE_VALUE (vt);
7196 if (TREE_CODE (vt) == TREE_VEC)
7205 /* Recursively build the VTT-initializer for BINFO (which is in the
7206 hierarchy dominated by T). INITS points to the end of the initializer
7207 list to date. INDEX is the VTT index where the next element will be
7208 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7209 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7210 for virtual bases of T. When it is not so, we build the constructor
7211 vtables for the BINFO-in-T variant. */
7214 build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
7219 tree secondary_vptrs;
7220 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7222 /* We only need VTTs for subobjects with virtual bases. */
7223 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7226 /* We need to use a construction vtable if this is not the primary
7230 build_ctor_vtbl_group (binfo, t);
7232 /* Record the offset in the VTT where this sub-VTT can be found. */
7233 BINFO_SUBVTT_INDEX (binfo) = *index;
7236 /* Add the address of the primary vtable for the complete object. */
7237 init = binfo_ctor_vtable (binfo);
7238 *inits = build_tree_list (NULL_TREE, init);
7239 inits = &TREE_CHAIN (*inits);
7242 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7243 BINFO_VPTR_INDEX (binfo) = *index;
7245 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7247 /* Recursively add the secondary VTTs for non-virtual bases. */
7248 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7250 b = BINFO_BASETYPE (binfo, i);
7251 if (!TREE_VIA_VIRTUAL (b))
7252 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7256 /* Add secondary virtual pointers for all subobjects of BINFO with
7257 either virtual bases or reachable along a virtual path, except
7258 subobjects that are non-virtual primary bases. */
7259 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7260 TREE_TYPE (secondary_vptrs) = *index;
7261 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7262 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7264 dfs_walk_real (binfo,
7265 dfs_build_secondary_vptr_vtt_inits,
7267 dfs_ctor_vtable_bases_queue_p,
7269 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7270 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7273 *index = TREE_TYPE (secondary_vptrs);
7275 /* The secondary vptrs come back in reverse order. After we reverse
7276 them, and add the INITS, the last init will be the first element
7278 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7279 if (secondary_vptrs)
7281 *inits = nreverse (secondary_vptrs);
7282 inits = &TREE_CHAIN (secondary_vptrs);
7283 my_friendly_assert (*inits == NULL_TREE, 20000517);
7286 /* Add the secondary VTTs for virtual bases. */
7288 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7292 if (!TREE_VIA_VIRTUAL (b))
7295 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7296 inits = build_vtt_inits (vbase, t, inits, index);
7301 tree data = tree_cons (t, binfo, NULL_TREE);
7302 VTT_TOP_LEVEL_P (data) = 0;
7303 VTT_MARKED_BINFO_P (data) = 0;
7305 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7306 dfs_ctor_vtable_bases_queue_p,
7313 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7314 for the base in most derived. DATA is a TREE_LIST who's
7315 TREE_CHAIN is the type of the base being
7316 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7317 flag of DATA indicates that this is a constructor vtable. The
7318 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7321 dfs_build_secondary_vptr_vtt_inits (tree binfo, void* data)
7331 top_level_p = VTT_TOP_LEVEL_P (l);
7333 SET_BINFO_MARKED (binfo);
7335 /* We don't care about bases that don't have vtables. */
7336 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7339 /* We're only interested in proper subobjects of T. */
7340 if (same_type_p (BINFO_TYPE (binfo), t))
7343 /* We're not interested in non-virtual primary bases. */
7344 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7347 /* If BINFO has virtual bases or is reachable via a virtual path
7348 from T, it'll have a secondary vptr. */
7349 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7350 && !binfo_via_virtual (binfo, t))
7353 /* Record the index where this secondary vptr can be found. */
7354 index = TREE_TYPE (l);
7357 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7358 BINFO_VPTR_INDEX (binfo) = index;
7360 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7361 TYPE_SIZE_UNIT (ptr_type_node));
7363 /* Add the initializer for the secondary vptr itself. */
7364 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7366 /* It's a primary virtual base, and this is not the construction
7367 vtable. Find the base this is primary of in the inheritance graph,
7368 and use that base's vtable now. */
7369 while (BINFO_PRIMARY_BASE_OF (binfo))
7370 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7372 init = binfo_ctor_vtable (binfo);
7373 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7378 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7379 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7380 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7384 dfs_ctor_vtable_bases_queue_p (tree binfo, void* data)
7386 if (TREE_VIA_VIRTUAL (binfo))
7387 /* Get the shared version. */
7388 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7390 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7395 /* Called from build_vtt_inits via dfs_walk. After building constructor
7396 vtables and generating the sub-vtt from them, we need to restore the
7397 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7398 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7401 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7403 CLEAR_BINFO_MARKED (binfo);
7405 /* We don't care about bases that don't have vtables. */
7406 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7409 /* If we scribbled the construction vtable vptr into BINFO, clear it
7411 if (BINFO_VTABLE (binfo)
7412 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7413 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7414 == TREE_VALUE ((tree) data)))
7415 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7420 /* Build the construction vtable group for BINFO which is in the
7421 hierarchy dominated by T. */
7424 build_ctor_vtbl_group (tree binfo, tree t)
7433 /* See if we've already created this construction vtable group. */
7434 id = mangle_ctor_vtbl_for_type (t, binfo);
7435 if (IDENTIFIER_GLOBAL_VALUE (id))
7438 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7439 /* Build a version of VTBL (with the wrong type) for use in
7440 constructing the addresses of secondary vtables in the
7441 construction vtable group. */
7442 vtbl = build_vtable (t, id, ptr_type_node);
7443 list = build_tree_list (vtbl, NULL_TREE);
7444 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7447 /* Add the vtables for each of our virtual bases using the vbase in T
7449 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7451 vbase = TREE_CHAIN (vbase))
7456 if (!TREE_VIA_VIRTUAL (vbase))
7458 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7459 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7461 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7463 inits = TREE_VALUE (list);
7465 /* Figure out the type of the construction vtable. */
7466 type = build_index_type (size_int (list_length (inits) - 1));
7467 type = build_cplus_array_type (vtable_entry_type, type);
7468 TREE_TYPE (vtbl) = type;
7470 /* Initialize the construction vtable. */
7471 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7472 initialize_array (vtbl, inits);
7473 dump_vtable (t, binfo, vtbl);
7476 /* Add the vtbl initializers for BINFO (and its bases other than
7477 non-virtual primaries) to the list of INITS. BINFO is in the
7478 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7479 the constructor the vtbl inits should be accumulated for. (If this
7480 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7481 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7482 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7483 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7484 but are not necessarily the same in terms of layout. */
7487 accumulate_vtbl_inits (tree binfo,
7494 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7496 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7497 BINFO_TYPE (orig_binfo)),
7500 /* If it doesn't have a vptr, we don't do anything. */
7501 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7504 /* If we're building a construction vtable, we're not interested in
7505 subobjects that don't require construction vtables. */
7507 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7508 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7511 /* Build the initializers for the BINFO-in-T vtable. */
7513 = chainon (TREE_VALUE (inits),
7514 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7515 rtti_binfo, t, inits));
7517 /* Walk the BINFO and its bases. We walk in preorder so that as we
7518 initialize each vtable we can figure out at what offset the
7519 secondary vtable lies from the primary vtable. We can't use
7520 dfs_walk here because we need to iterate through bases of BINFO
7521 and RTTI_BINFO simultaneously. */
7522 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7524 tree base_binfo = BINFO_BASETYPE (binfo, i);
7526 /* Skip virtual bases. */
7527 if (TREE_VIA_VIRTUAL (base_binfo))
7529 accumulate_vtbl_inits (base_binfo,
7530 BINFO_BASETYPE (orig_binfo, i),
7536 /* Called from accumulate_vtbl_inits. Returns the initializers for
7537 the BINFO vtable. */
7540 dfs_accumulate_vtbl_inits (tree binfo,
7546 tree inits = NULL_TREE;
7547 tree vtbl = NULL_TREE;
7548 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7551 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7553 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7554 primary virtual base. If it is not the same primary in
7555 the hierarchy of T, we'll need to generate a ctor vtable
7556 for it, to place at its location in T. If it is the same
7557 primary, we still need a VTT entry for the vtable, but it
7558 should point to the ctor vtable for the base it is a
7559 primary for within the sub-hierarchy of RTTI_BINFO.
7561 There are three possible cases:
7563 1) We are in the same place.
7564 2) We are a primary base within a lost primary virtual base of
7566 3) We are primary to something not a base of RTTI_BINFO. */
7568 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7569 tree last = NULL_TREE;
7571 /* First, look through the bases we are primary to for RTTI_BINFO
7572 or a virtual base. */
7573 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7576 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7579 /* If we run out of primary links, keep looking down our
7580 inheritance chain; we might be an indirect primary. */
7582 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7583 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7586 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7587 base B and it is a base of RTTI_BINFO, this is case 2. In
7588 either case, we share our vtable with LAST, i.e. the
7589 derived-most base within B of which we are a primary. */
7591 || (b && binfo_for_vbase (BINFO_TYPE (b),
7592 BINFO_TYPE (rtti_binfo))))
7593 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7594 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7595 binfo_ctor_vtable after everything's been set up. */
7598 /* Otherwise, this is case 3 and we get our own. */
7600 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7608 /* Compute the initializer for this vtable. */
7609 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7612 /* Figure out the position to which the VPTR should point. */
7613 vtbl = TREE_PURPOSE (l);
7614 vtbl = build1 (ADDR_EXPR,
7617 TREE_CONSTANT (vtbl) = 1;
7618 index = size_binop (PLUS_EXPR,
7619 size_int (non_fn_entries),
7620 size_int (list_length (TREE_VALUE (l))));
7621 index = size_binop (MULT_EXPR,
7622 TYPE_SIZE_UNIT (vtable_entry_type),
7624 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7625 TREE_CONSTANT (vtbl) = 1;
7629 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7630 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7631 straighten this out. */
7632 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7633 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7636 /* For an ordinary vtable, set BINFO_VTABLE. */
7637 BINFO_VTABLE (binfo) = vtbl;
7642 /* Construct the initializer for BINFO's virtual function table. BINFO
7643 is part of the hierarchy dominated by T. If we're building a
7644 construction vtable, the ORIG_BINFO is the binfo we should use to
7645 find the actual function pointers to put in the vtable - but they
7646 can be overridden on the path to most-derived in the graph that
7647 ORIG_BINFO belongs. Otherwise,
7648 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7649 BINFO that should be indicated by the RTTI information in the
7650 vtable; it will be a base class of T, rather than T itself, if we
7651 are building a construction vtable.
7653 The value returned is a TREE_LIST suitable for wrapping in a
7654 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7655 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7656 number of non-function entries in the vtable.
7658 It might seem that this function should never be called with a
7659 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7660 base is always subsumed by a derived class vtable. However, when
7661 we are building construction vtables, we do build vtables for
7662 primary bases; we need these while the primary base is being
7666 build_vtbl_initializer (tree binfo,
7670 int* non_fn_entries_p)
7677 /* Initialize VID. */
7678 memset (&vid, 0, sizeof (vid));
7681 vid.rtti_binfo = rtti_binfo;
7682 vid.last_init = &vid.inits;
7683 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7684 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7685 vid.generate_vcall_entries = true;
7686 /* The first vbase or vcall offset is at index -3 in the vtable. */
7687 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7689 /* Add entries to the vtable for RTTI. */
7690 build_rtti_vtbl_entries (binfo, &vid);
7692 /* Create an array for keeping track of the functions we've
7693 processed. When we see multiple functions with the same
7694 signature, we share the vcall offsets. */
7695 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7696 /* Add the vcall and vbase offset entries. */
7697 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7698 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7699 build_vbase_offset_vtbl_entries. */
7700 for (vbase = CLASSTYPE_VBASECLASSES (t);
7702 vbase = TREE_CHAIN (vbase))
7703 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7705 /* If the target requires padding between data entries, add that now. */
7706 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7710 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7715 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7716 add = tree_cons (NULL_TREE,
7717 build1 (NOP_EXPR, vtable_entry_type,
7724 if (non_fn_entries_p)
7725 *non_fn_entries_p = list_length (vid.inits);
7727 /* Go through all the ordinary virtual functions, building up
7729 vfun_inits = NULL_TREE;
7730 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7734 tree fn, fn_original;
7735 tree init = NULL_TREE;
7739 if (DECL_THUNK_P (fn))
7741 if (!DECL_NAME (fn))
7743 fn_original = THUNK_TARGET (fn);
7746 /* If the only definition of this function signature along our
7747 primary base chain is from a lost primary, this vtable slot will
7748 never be used, so just zero it out. This is important to avoid
7749 requiring extra thunks which cannot be generated with the function.
7751 We first check this in update_vtable_entry_for_fn, so we handle
7752 restored primary bases properly; we also need to do it here so we
7753 zero out unused slots in ctor vtables, rather than filling themff
7754 with erroneous values (though harmless, apart from relocation
7756 for (b = binfo; ; b = get_primary_binfo (b))
7758 /* We found a defn before a lost primary; go ahead as normal. */
7759 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7762 /* The nearest definition is from a lost primary; clear the
7764 if (BINFO_LOST_PRIMARY_P (b))
7766 init = size_zero_node;
7773 /* Pull the offset for `this', and the function to call, out of
7775 delta = BV_DELTA (v);
7776 vcall_index = BV_VCALL_INDEX (v);
7778 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7779 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7781 /* You can't call an abstract virtual function; it's abstract.
7782 So, we replace these functions with __pure_virtual. */
7783 if (DECL_PURE_VIRTUAL_P (fn_original))
7785 else if (!integer_zerop (delta) || vcall_index)
7787 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7788 if (!DECL_NAME (fn))
7791 /* Take the address of the function, considering it to be of an
7792 appropriate generic type. */
7793 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7794 /* The address of a function can't change. */
7795 TREE_CONSTANT (init) = 1;
7798 /* And add it to the chain of initializers. */
7799 if (TARGET_VTABLE_USES_DESCRIPTORS)
7802 if (init == size_zero_node)
7803 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7804 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7806 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7808 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7809 TREE_OPERAND (init, 0),
7810 build_int_2 (i, 0));
7811 TREE_CONSTANT (fdesc) = 1;
7813 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7817 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7820 /* The initializers for virtual functions were built up in reverse
7821 order; straighten them out now. */
7822 vfun_inits = nreverse (vfun_inits);
7824 /* The negative offset initializers are also in reverse order. */
7825 vid.inits = nreverse (vid.inits);
7827 /* Chain the two together. */
7828 return chainon (vid.inits, vfun_inits);
7831 /* Adds to vid->inits the initializers for the vbase and vcall
7832 offsets in BINFO, which is in the hierarchy dominated by T. */
7835 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7839 /* If this is a derived class, we must first create entries
7840 corresponding to the primary base class. */
7841 b = get_primary_binfo (binfo);
7843 build_vcall_and_vbase_vtbl_entries (b, vid);
7845 /* Add the vbase entries for this base. */
7846 build_vbase_offset_vtbl_entries (binfo, vid);
7847 /* Add the vcall entries for this base. */
7848 build_vcall_offset_vtbl_entries (binfo, vid);
7851 /* Returns the initializers for the vbase offset entries in the vtable
7852 for BINFO (which is part of the class hierarchy dominated by T), in
7853 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7854 where the next vbase offset will go. */
7857 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7861 tree non_primary_binfo;
7863 /* If there are no virtual baseclasses, then there is nothing to
7865 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7870 /* We might be a primary base class. Go up the inheritance hierarchy
7871 until we find the most derived class of which we are a primary base:
7872 it is the offset of that which we need to use. */
7873 non_primary_binfo = binfo;
7874 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7878 /* If we have reached a virtual base, then it must be a primary
7879 base (possibly multi-level) of vid->binfo, or we wouldn't
7880 have called build_vcall_and_vbase_vtbl_entries for it. But it
7881 might be a lost primary, so just skip down to vid->binfo. */
7882 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7884 non_primary_binfo = vid->binfo;
7888 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7889 if (get_primary_binfo (b) != non_primary_binfo)
7891 non_primary_binfo = b;
7894 /* Go through the virtual bases, adding the offsets. */
7895 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7897 vbase = TREE_CHAIN (vbase))
7902 if (!TREE_VIA_VIRTUAL (vbase))
7905 /* Find the instance of this virtual base in the complete
7907 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7909 /* If we've already got an offset for this virtual base, we
7910 don't need another one. */
7911 if (BINFO_VTABLE_PATH_MARKED (b))
7913 SET_BINFO_VTABLE_PATH_MARKED (b);
7915 /* Figure out where we can find this vbase offset. */
7916 delta = size_binop (MULT_EXPR,
7919 TYPE_SIZE_UNIT (vtable_entry_type)));
7920 if (vid->primary_vtbl_p)
7921 BINFO_VPTR_FIELD (b) = delta;
7923 if (binfo != TYPE_BINFO (t))
7927 /* Find the instance of this virtual base in the type of BINFO. */
7928 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7929 BINFO_TYPE (binfo));
7931 /* The vbase offset had better be the same. */
7932 if (!tree_int_cst_equal (delta,
7933 BINFO_VPTR_FIELD (orig_vbase)))
7937 /* The next vbase will come at a more negative offset. */
7938 vid->index = size_binop (MINUS_EXPR, vid->index,
7939 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7941 /* The initializer is the delta from BINFO to this virtual base.
7942 The vbase offsets go in reverse inheritance-graph order, and
7943 we are walking in inheritance graph order so these end up in
7945 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7948 = build_tree_list (NULL_TREE,
7949 fold (build1 (NOP_EXPR,
7952 vid->last_init = &TREE_CHAIN (*vid->last_init);
7956 /* Adds the initializers for the vcall offset entries in the vtable
7957 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7961 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7963 /* We only need these entries if this base is a virtual base. We
7964 compute the indices -- but do not add to the vtable -- when
7965 building the main vtable for a class. */
7966 if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7968 /* We need a vcall offset for each of the virtual functions in this
7969 vtable. For example:
7971 class A { virtual void f (); };
7972 class B1 : virtual public A { virtual void f (); };
7973 class B2 : virtual public A { virtual void f (); };
7974 class C: public B1, public B2 { virtual void f (); };
7976 A C object has a primary base of B1, which has a primary base of A. A
7977 C also has a secondary base of B2, which no longer has a primary base
7978 of A. So the B2-in-C construction vtable needs a secondary vtable for
7979 A, which will adjust the A* to a B2* to call f. We have no way of
7980 knowing what (or even whether) this offset will be when we define B2,
7981 so we store this "vcall offset" in the A sub-vtable and look it up in
7982 a "virtual thunk" for B2::f.
7984 We need entries for all the functions in our primary vtable and
7985 in our non-virtual bases' secondary vtables. */
7987 /* If we are just computing the vcall indices -- but do not need
7988 the actual entries -- not that. */
7989 if (!TREE_VIA_VIRTUAL (binfo))
7990 vid->generate_vcall_entries = false;
7991 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7992 add_vcall_offset_vtbl_entries_r (binfo, vid);
7996 /* Build vcall offsets, starting with those for BINFO. */
7999 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8004 /* Don't walk into virtual bases -- except, of course, for the
8005 virtual base for which we are building vcall offsets. Any
8006 primary virtual base will have already had its offsets generated
8007 through the recursion in build_vcall_and_vbase_vtbl_entries. */
8008 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
8011 /* If BINFO has a primary base, process it first. */
8012 primary_binfo = get_primary_binfo (binfo);
8014 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8016 /* Add BINFO itself to the list. */
8017 add_vcall_offset_vtbl_entries_1 (binfo, vid);
8019 /* Scan the non-primary bases of BINFO. */
8020 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
8024 base_binfo = BINFO_BASETYPE (binfo, i);
8025 if (base_binfo != primary_binfo)
8026 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8030 /* Called from build_vcall_offset_vtbl_entries_r. */
8033 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8035 /* Make entries for the rest of the virtuals. */
8036 if (abi_version_at_least (2))
8040 /* The ABI requires that the methods be processed in declaration
8041 order. G++ 3.2 used the order in the vtable. */
8042 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8044 orig_fn = TREE_CHAIN (orig_fn))
8045 if (DECL_VINDEX (orig_fn))
8046 add_vcall_offset (orig_fn, binfo, vid);
8050 tree derived_virtuals;
8053 /* If BINFO is a primary base, the most derived class which has
8054 BINFO as a primary base; otherwise, just BINFO. */
8055 tree non_primary_binfo;
8057 /* We might be a primary base class. Go up the inheritance hierarchy
8058 until we find the most derived class of which we are a primary base:
8059 it is the BINFO_VIRTUALS there that we need to consider. */
8060 non_primary_binfo = binfo;
8061 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8065 /* If we have reached a virtual base, then it must be vid->vbase,
8066 because we ignore other virtual bases in
8067 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
8068 base (possibly multi-level) of vid->binfo, or we wouldn't
8069 have called build_vcall_and_vbase_vtbl_entries for it. But it
8070 might be a lost primary, so just skip down to vid->binfo. */
8071 if (TREE_VIA_VIRTUAL (non_primary_binfo))
8073 if (non_primary_binfo != vid->vbase)
8075 non_primary_binfo = vid->binfo;
8079 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8080 if (get_primary_binfo (b) != non_primary_binfo)
8082 non_primary_binfo = b;
8085 if (vid->ctor_vtbl_p)
8086 /* For a ctor vtable we need the equivalent binfo within the hierarchy
8087 where rtti_binfo is the most derived type. */
8088 non_primary_binfo = get_original_base
8089 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
8091 for (base_virtuals = BINFO_VIRTUALS (binfo),
8092 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8093 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8095 base_virtuals = TREE_CHAIN (base_virtuals),
8096 derived_virtuals = TREE_CHAIN (derived_virtuals),
8097 orig_virtuals = TREE_CHAIN (orig_virtuals))
8101 /* Find the declaration that originally caused this function to
8102 be present in BINFO_TYPE (binfo). */
8103 orig_fn = BV_FN (orig_virtuals);
8105 /* When processing BINFO, we only want to generate vcall slots for
8106 function slots introduced in BINFO. So don't try to generate
8107 one if the function isn't even defined in BINFO. */
8108 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
8111 add_vcall_offset (orig_fn, binfo, vid);
8116 /* Add a vcall offset entry for ORIG_FN to the vtable. */
8119 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8124 /* If there is already an entry for a function with the same
8125 signature as FN, then we do not need a second vcall offset.
8126 Check the list of functions already present in the derived
8128 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
8132 derived_entry = VARRAY_TREE (vid->fns, i);
8133 if (same_signature_p (derived_entry, orig_fn)
8134 /* We only use one vcall offset for virtual destructors,
8135 even though there are two virtual table entries. */
8136 || (DECL_DESTRUCTOR_P (derived_entry)
8137 && DECL_DESTRUCTOR_P (orig_fn)))
8141 /* If we are building these vcall offsets as part of building
8142 the vtable for the most derived class, remember the vcall
8144 if (vid->binfo == TYPE_BINFO (vid->derived))
8145 CLASSTYPE_VCALL_INDICES (vid->derived)
8146 = tree_cons (orig_fn, vid->index,
8147 CLASSTYPE_VCALL_INDICES (vid->derived));
8149 /* The next vcall offset will be found at a more negative
8151 vid->index = size_binop (MINUS_EXPR, vid->index,
8152 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8154 /* Keep track of this function. */
8155 VARRAY_PUSH_TREE (vid->fns, orig_fn);
8157 if (vid->generate_vcall_entries)
8162 /* Find the overriding function. */
8163 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8164 if (fn == error_mark_node)
8165 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
8169 base = TREE_VALUE (fn);
8171 /* The vbase we're working on is a primary base of
8172 vid->binfo. But it might be a lost primary, so its
8173 BINFO_OFFSET might be wrong, so we just use the
8174 BINFO_OFFSET from vid->binfo. */
8175 vcall_offset = size_diffop (BINFO_OFFSET (base),
8176 BINFO_OFFSET (vid->binfo));
8177 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
8180 /* Add the intiailizer to the vtable. */
8181 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
8182 vid->last_init = &TREE_CHAIN (*vid->last_init);
8186 /* Return vtbl initializers for the RTTI entries coresponding to the
8187 BINFO's vtable. The RTTI entries should indicate the object given
8188 by VID->rtti_binfo. */
8191 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8200 basetype = BINFO_TYPE (binfo);
8201 t = BINFO_TYPE (vid->rtti_binfo);
8203 /* To find the complete object, we will first convert to our most
8204 primary base, and then add the offset in the vtbl to that value. */
8206 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8207 && !BINFO_LOST_PRIMARY_P (b))
8211 primary_base = get_primary_binfo (b);
8212 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8215 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8217 /* The second entry is the address of the typeinfo object. */
8219 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8221 decl = integer_zero_node;
8223 /* Convert the declaration to a type that can be stored in the
8225 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8226 TREE_CONSTANT (init) = 1;
8227 *vid->last_init = build_tree_list (NULL_TREE, init);
8228 vid->last_init = &TREE_CHAIN (*vid->last_init);
8230 /* Add the offset-to-top entry. It comes earlier in the vtable that
8231 the the typeinfo entry. Convert the offset to look like a
8232 function pointer, so that we can put it in the vtable. */
8233 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8234 TREE_CONSTANT (init) = 1;
8235 *vid->last_init = build_tree_list (NULL_TREE, init);
8236 vid->last_init = &TREE_CHAIN (*vid->last_init);