2003-04-02 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / cp / class.c
blob01d4dd215ed6ac0085b7a9688c75d93722db7be1
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)
11 any later version.
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. */
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "toplev.h"
36 #include "lex.h"
37 #include "target.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. */
50 tree name;
52 /* The _TYPE node for the class. */
53 tree type;
55 /* The access specifier pending for new declarations in the scope of
56 this class. */
57 tree access;
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. */
66 tree binfo;
67 /* The type of the most-derived type. */
68 tree derived;
69 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
70 unless ctor_vtbl_p is true. */
71 tree rtti_binfo;
72 /* The negative-index vtable initializers built up so far. These
73 are in order from least negative index to most negative index. */
74 tree inits;
75 /* The last (i.e., most negative) entry in INITS. */
76 tree* last_init;
77 /* The binfo for the virtual base for which we're building
78 vcall offset initializers. */
79 tree vbase;
80 /* The functions in vbase for which we have already provided vcall
81 offsets. */
82 varray_type fns;
83 /* The vtable index of the next vcall or vbase offset. */
84 tree index;
85 /* Nonzero if we are building the initializer for the primary
86 vtable. */
87 int primary_vtbl_p;
88 /* Nonzero if we are building the initializer for a construction
89 vtable. */
90 int ctor_vtbl_p;
91 /* True when adding vcall offset entries to the vtable. False when
92 merely computing the indices. */
93 bool generate_vcall_entries;
94 } vtbl_init_data;
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);
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,
134 int, int, tree);
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);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
158 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
160 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
161 static void add_vcall_offset (tree, tree, vtbl_init_data *);
162 static void layout_vtable_decl (tree, int);
163 static tree dfs_find_final_overrider (tree, void *);
164 static tree dfs_find_final_overrider_post (tree, void *);
165 static tree dfs_find_final_overrider_q (tree, int, void *);
166 static tree find_final_overrider (tree, tree, tree);
167 static int make_new_vtable (tree, tree);
168 static int maybe_indent_hierarchy (FILE *, int, int);
169 static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
170 static void dump_class_hierarchy (tree);
171 static void dump_array (FILE *, tree);
172 static void dump_vtable (tree, tree, tree);
173 static void dump_vtt (tree, tree);
174 static tree build_vtable (tree, tree, tree);
175 static void initialize_vtable (tree, tree);
176 static void initialize_array (tree, tree);
177 static void layout_nonempty_base_or_field (record_layout_info,
178 tree, tree, splay_tree);
179 static tree end_of_class (tree, int);
180 static bool layout_empty_base (tree, tree, splay_tree);
181 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
182 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
183 tree);
184 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
185 static void build_vcall_and_vbase_vtbl_entries (tree,
186 vtbl_init_data *);
187 static void mark_primary_bases (tree);
188 static void clone_constructors_and_destructors (tree);
189 static tree build_clone (tree, tree);
190 static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
191 static tree copy_virtuals (tree);
192 static void build_ctor_vtbl_group (tree, tree);
193 static void build_vtt (tree);
194 static tree binfo_ctor_vtable (tree);
195 static tree *build_vtt_inits (tree, tree, tree *, tree *);
196 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
197 static tree dfs_ctor_vtable_bases_queue_p (tree, int, void *data);
198 static tree dfs_fixup_binfo_vtbls (tree, void *);
199 static int record_subobject_offset (tree, tree, splay_tree);
200 static int check_subobject_offset (tree, tree, splay_tree);
201 static int walk_subobject_offsets (tree, subobject_offset_fn,
202 tree, splay_tree, tree, int);
203 static void record_subobject_offsets (tree, tree, splay_tree, int);
204 static int layout_conflict_p (tree, tree, splay_tree, int);
205 static int splay_tree_compare_integer_csts (splay_tree_key k1,
206 splay_tree_key k2);
207 static void warn_about_ambiguous_bases (tree);
208 static bool type_requires_array_cookie (tree);
209 static bool contains_empty_class_p (tree);
210 static bool base_derived_from (tree, tree);
211 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
212 static tree end_of_base (tree);
213 static tree get_vcall_index (tree, tree);
215 /* Macros for dfs walking during vtt construction. See
216 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
217 and dfs_fixup_binfo_vtbls. */
218 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
219 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
221 /* Variables shared between class.c and call.c. */
223 #ifdef GATHER_STATISTICS
224 int n_vtables = 0;
225 int n_vtable_entries = 0;
226 int n_vtable_searches = 0;
227 int n_vtable_elems = 0;
228 int n_convert_harshness = 0;
229 int n_compute_conversion_costs = 0;
230 int n_build_method_call = 0;
231 int n_inner_fields_searched = 0;
232 #endif
234 /* Convert to or from a base subobject. EXPR is an expression of type
235 `A' or `A*', an expression of type `B' or `B*' is returned. To
236 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
237 the B base instance within A. To convert base A to derived B, CODE
238 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
239 In this latter case, A must not be a morally virtual base of B.
240 NONNULL is true if EXPR is known to be non-NULL (this is only
241 needed when EXPR is of pointer type). CV qualifiers are preserved
242 from EXPR. */
244 tree
245 build_base_path (enum tree_code code,
246 tree expr,
247 tree binfo,
248 int nonnull)
250 tree v_binfo = NULL_TREE;
251 tree d_binfo = NULL_TREE;
252 tree probe;
253 tree offset;
254 tree target_type;
255 tree null_test = NULL;
256 tree ptr_target_type;
257 int fixed_type_p;
258 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
260 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
261 return error_mark_node;
263 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
265 d_binfo = probe;
266 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
267 v_binfo = probe;
270 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
271 if (want_pointer)
272 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
274 my_friendly_assert (code == MINUS_EXPR
275 ? same_type_p (BINFO_TYPE (binfo), probe)
276 : code == PLUS_EXPR
277 ? same_type_p (BINFO_TYPE (d_binfo), probe)
278 : false, 20010723);
280 if (code == MINUS_EXPR && v_binfo)
282 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
283 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
284 return error_mark_node;
287 if (!want_pointer)
288 /* This must happen before the call to save_expr. */
289 expr = build_unary_op (ADDR_EXPR, expr, 0);
291 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
292 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
293 expr = save_expr (expr);
295 if (want_pointer && !nonnull)
296 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
298 offset = BINFO_OFFSET (binfo);
300 if (v_binfo && fixed_type_p <= 0)
302 /* Going via virtual base V_BINFO. We need the static offset
303 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
304 V_BINFO. That offset is an entry in D_BINFO's vtable. */
305 tree v_offset;
307 if (fixed_type_p < 0 && in_base_initializer)
309 /* In a base member initializer, we cannot rely on
310 the vtable being set up. We have to use the vtt_parm. */
311 tree derived = BINFO_INHERITANCE_CHAIN (v_binfo);
313 v_offset = build (PLUS_EXPR, TREE_TYPE (current_vtt_parm),
314 current_vtt_parm, BINFO_VPTR_INDEX (derived));
316 v_offset = build1 (INDIRECT_REF,
317 TREE_TYPE (TYPE_VFIELD (BINFO_TYPE (derived))),
318 v_offset);
321 else
322 v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
323 TREE_TYPE (TREE_TYPE (expr)));
325 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
326 v_offset, BINFO_VPTR_FIELD (v_binfo));
327 v_offset = build1 (NOP_EXPR,
328 build_pointer_type (ptrdiff_type_node),
329 v_offset);
330 v_offset = build_indirect_ref (v_offset, NULL);
331 TREE_CONSTANT (v_offset) = 1;
333 offset = cp_convert (ptrdiff_type_node,
334 size_diffop (offset, BINFO_OFFSET (v_binfo)));
336 if (!integer_zerop (offset))
337 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
339 if (fixed_type_p < 0)
340 /* Negative fixed_type_p means this is a constructor or destructor;
341 virtual base layout is fixed in in-charge [cd]tors, but not in
342 base [cd]tors. */
343 offset = build (COND_EXPR, ptrdiff_type_node,
344 build (EQ_EXPR, boolean_type_node,
345 current_in_charge_parm, integer_zero_node),
346 v_offset,
347 BINFO_OFFSET (binfo));
348 else
349 offset = v_offset;
352 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
354 target_type = cp_build_qualified_type
355 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
356 ptr_target_type = build_pointer_type (target_type);
357 if (want_pointer)
358 target_type = ptr_target_type;
360 expr = build1 (NOP_EXPR, ptr_target_type, expr);
362 if (!integer_zerop (offset))
363 expr = build (code, ptr_target_type, expr, offset);
364 else
365 null_test = NULL;
367 if (!want_pointer)
368 expr = build_indirect_ref (expr, NULL);
370 if (null_test)
371 expr = build (COND_EXPR, target_type, null_test,
372 build1 (NOP_EXPR, target_type, integer_zero_node),
373 expr);
375 return expr;
378 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
379 message is emitted if TYPE is inaccessible. OBJECT is assumed to
380 be non-NULL. */
382 tree
383 convert_to_base (tree object, tree type, bool check_access)
385 tree binfo;
387 binfo = lookup_base (TREE_TYPE (object), type,
388 check_access ? ba_check : ba_ignore,
389 NULL);
390 if (!binfo || binfo == error_mark_node)
391 return error_mark_node;
393 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
397 /* Virtual function things. */
399 static tree
400 build_vtable_entry_ref (tree array_ref, tree instance, tree idx)
402 tree i, i2, vtable, first_fn, basetype;
404 basetype = TREE_TYPE (instance);
405 if (TREE_CODE (basetype) == REFERENCE_TYPE)
406 basetype = TREE_TYPE (basetype);
408 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
409 first_fn = TYPE_BINFO_VTABLE (basetype);
411 i = fold (build_array_ref (first_fn, idx));
412 i = fold (build_c_cast (ptrdiff_type_node,
413 build_unary_op (ADDR_EXPR, i, 0)));
414 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
415 i2 = fold (build_c_cast (ptrdiff_type_node,
416 build_unary_op (ADDR_EXPR, i2, 0)));
417 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
419 if (TREE_CODE (i) != INTEGER_CST)
420 abort ();
422 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
425 /* Given an object INSTANCE, return an expression which yields the
426 vtable element corresponding to INDEX. There are many special
427 cases for INSTANCE which we take care of here, mainly to avoid
428 creating extra tree nodes when we don't have to. */
430 static tree
431 build_vtbl_ref_1 (tree instance, tree idx)
433 tree aref;
434 tree vtbl = NULL_TREE;
436 /* Try to figure out what a reference refers to, and
437 access its virtual function table directly. */
439 int cdtorp = 0;
440 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
442 tree basetype = TREE_TYPE (instance);
443 if (TREE_CODE (basetype) == REFERENCE_TYPE)
444 basetype = TREE_TYPE (basetype);
446 if (fixed_type && !cdtorp)
448 tree binfo = lookup_base (fixed_type, basetype,
449 ba_ignore|ba_quiet, NULL);
450 if (binfo)
451 vtbl = BINFO_VTABLE (binfo);
454 if (!vtbl)
455 vtbl = build_vfield_ref (instance, basetype);
457 assemble_external (vtbl);
459 aref = build_array_ref (vtbl, idx);
460 TREE_CONSTANT (aref) = 1;
462 return aref;
465 tree
466 build_vtbl_ref (tree instance, tree idx)
468 tree aref = build_vtbl_ref_1 (instance, idx);
470 if (flag_vtable_gc)
471 aref = build_vtable_entry_ref (aref, instance, idx);
473 return aref;
476 /* Given an object INSTANCE, return an expression which yields a
477 function pointer corresponding to vtable element INDEX. */
479 tree
480 build_vfn_ref (tree instance, tree idx)
482 tree aref = build_vtbl_ref_1 (instance, idx);
484 /* When using function descriptors, the address of the
485 vtable entry is treated as a function pointer. */
486 if (TARGET_VTABLE_USES_DESCRIPTORS)
487 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
488 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
490 if (flag_vtable_gc)
491 aref = build_vtable_entry_ref (aref, instance, idx);
493 return aref;
496 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
497 for the given TYPE. */
499 static tree
500 get_vtable_name (tree type)
502 return mangle_vtbl_for_type (type);
505 /* Return an IDENTIFIER_NODE for the name of the virtual table table
506 for TYPE. */
508 tree
509 get_vtt_name (tree type)
511 return mangle_vtt_for_type (type);
514 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
515 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
516 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
518 static tree
519 build_vtable (tree class_type, tree name, tree vtable_type)
521 tree decl;
523 decl = build_lang_decl (VAR_DECL, name, vtable_type);
524 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
525 now to avoid confusion in mangle_decl. */
526 SET_DECL_ASSEMBLER_NAME (decl, name);
527 DECL_CONTEXT (decl) = class_type;
528 DECL_ARTIFICIAL (decl) = 1;
529 TREE_STATIC (decl) = 1;
530 TREE_READONLY (decl) = 1;
531 DECL_VIRTUAL_P (decl) = 1;
532 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
534 import_export_vtable (decl, class_type, 0);
536 return decl;
539 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
540 or even complete. If this does not exist, create it. If COMPLETE is
541 nonzero, then complete the definition of it -- that will render it
542 impossible to actually build the vtable, but is useful to get at those
543 which are known to exist in the runtime. */
545 tree
546 get_vtable_decl (tree type, int complete)
548 tree decl;
550 if (CLASSTYPE_VTABLES (type))
551 return CLASSTYPE_VTABLES (type);
553 decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
554 CLASSTYPE_VTABLES (type) = decl;
556 /* At one time the vtable info was grabbed 2 words at a time. This
557 fails on sparc unless you have 8-byte alignment. (tiemann) */
558 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
559 DECL_ALIGN (decl));
561 if (complete)
563 DECL_EXTERNAL (decl) = 1;
564 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
567 return decl;
570 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
571 BV_VCALL_INDEX for each entry is cleared. */
573 static tree
574 copy_virtuals (tree binfo)
576 tree copies;
577 tree t;
579 copies = copy_list (BINFO_VIRTUALS (binfo));
580 for (t = copies; t; t = TREE_CHAIN (t))
581 BV_VCALL_INDEX (t) = NULL_TREE;
583 return copies;
586 /* Build the primary virtual function table for TYPE. If BINFO is
587 non-NULL, build the vtable starting with the initial approximation
588 that it is the same as the one which is the head of the association
589 list. Returns a nonzero value if a new vtable is actually
590 created. */
592 static int
593 build_primary_vtable (tree binfo, tree type)
595 tree decl;
596 tree virtuals;
598 decl = get_vtable_decl (type, /*complete=*/0);
600 if (binfo)
602 if (BINFO_NEW_VTABLE_MARKED (binfo))
603 /* We have already created a vtable for this base, so there's
604 no need to do it again. */
605 return 0;
607 virtuals = copy_virtuals (binfo);
608 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
609 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
610 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
612 else
614 my_friendly_assert (TREE_TYPE (decl) == vtbl_type_node, 20000118);
615 virtuals = NULL_TREE;
618 #ifdef GATHER_STATISTICS
619 n_vtables += 1;
620 n_vtable_elems += list_length (virtuals);
621 #endif
623 /* Initialize the association list for this type, based
624 on our first approximation. */
625 TYPE_BINFO_VTABLE (type) = decl;
626 TYPE_BINFO_VIRTUALS (type) = virtuals;
627 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
628 return 1;
631 /* Give BINFO a new virtual function table which is initialized
632 with a skeleton-copy of its original initialization. The only
633 entry that changes is the `delta' entry, so we can really
634 share a lot of structure.
636 FOR_TYPE is the most derived type which caused this table to
637 be needed.
639 Returns nonzero if we haven't met BINFO before.
641 The order in which vtables are built (by calling this function) for
642 an object must remain the same, otherwise a binary incompatibility
643 can result. */
645 static int
646 build_secondary_vtable (tree binfo)
648 if (BINFO_NEW_VTABLE_MARKED (binfo))
649 /* We already created a vtable for this base. There's no need to
650 do it again. */
651 return 0;
653 /* Remember that we've created a vtable for this BINFO, so that we
654 don't try to do so again. */
655 SET_BINFO_NEW_VTABLE_MARKED (binfo);
657 /* Make fresh virtual list, so we can smash it later. */
658 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
660 /* Secondary vtables are laid out as part of the same structure as
661 the primary vtable. */
662 BINFO_VTABLE (binfo) = NULL_TREE;
663 return 1;
666 /* Create a new vtable for BINFO which is the hierarchy dominated by
667 T. Return nonzero if we actually created a new vtable. */
669 static int
670 make_new_vtable (tree t, tree binfo)
672 if (binfo == TYPE_BINFO (t))
673 /* In this case, it is *type*'s vtable we are modifying. We start
674 with the approximation that its vtable is that of the
675 immediate base class. */
676 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
677 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
678 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
680 else
681 /* This is our very own copy of `basetype' to play with. Later,
682 we will fill in all the virtual functions that override the
683 virtual functions in these base classes which are not defined
684 by the current type. */
685 return build_secondary_vtable (binfo);
688 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
689 (which is in the hierarchy dominated by T) list FNDECL as its
690 BV_FN. DELTA is the required constant adjustment from the `this'
691 pointer where the vtable entry appears to the `this' required when
692 the function is actually called. */
694 static void
695 modify_vtable_entry (tree t,
696 tree binfo,
697 tree fndecl,
698 tree delta,
699 tree *virtuals)
701 tree v;
703 v = *virtuals;
705 if (fndecl != BV_FN (v)
706 || !tree_int_cst_equal (delta, BV_DELTA (v)))
708 /* We need a new vtable for BINFO. */
709 if (make_new_vtable (t, binfo))
711 /* If we really did make a new vtable, we also made a copy
712 of the BINFO_VIRTUALS list. Now, we have to find the
713 corresponding entry in that list. */
714 *virtuals = BINFO_VIRTUALS (binfo);
715 while (BV_FN (*virtuals) != BV_FN (v))
716 *virtuals = TREE_CHAIN (*virtuals);
717 v = *virtuals;
720 BV_DELTA (v) = delta;
721 BV_VCALL_INDEX (v) = NULL_TREE;
722 BV_FN (v) = fndecl;
727 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
728 the method after the class has already been defined because a
729 declaration for it was seen. (Even though that is erroneous, we
730 add the method for improved error recovery.) */
732 void
733 add_method (tree type, tree method, int error_p)
735 int using = (DECL_CONTEXT (method) != type);
736 int len;
737 int slot;
738 tree method_vec;
739 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
740 && DECL_TEMPLATE_CONV_FN_P (method));
742 if (!CLASSTYPE_METHOD_VEC (type))
743 /* Make a new method vector. We start with 8 entries. We must
744 allocate at least two (for constructors and destructors), and
745 we're going to end up with an assignment operator at some point
746 as well.
748 We could use a TREE_LIST for now, and convert it to a TREE_VEC
749 in finish_struct, but we would probably waste more memory
750 making the links in the list than we would by over-allocating
751 the size of the vector here. Furthermore, we would complicate
752 all the code that expects this to be a vector. */
753 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
755 method_vec = CLASSTYPE_METHOD_VEC (type);
756 len = TREE_VEC_LENGTH (method_vec);
758 /* Constructors and destructors go in special slots. */
759 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
760 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
761 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
762 slot = CLASSTYPE_DESTRUCTOR_SLOT;
763 else
765 int have_template_convs_p = 0;
767 /* See if we already have an entry with this name. */
768 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
770 tree m = TREE_VEC_ELT (method_vec, slot);
772 if (!m)
773 break;
774 m = OVL_CURRENT (m);
776 if (template_conv_p)
778 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
779 && DECL_TEMPLATE_CONV_FN_P (m));
781 /* If we need to move things up, see if there's
782 space. */
783 if (!have_template_convs_p)
785 slot = len - 1;
786 if (TREE_VEC_ELT (method_vec, slot))
787 slot++;
789 break;
791 if (DECL_NAME (m) == DECL_NAME (method))
792 break;
795 if (slot == len)
797 /* We need a bigger method vector. */
798 int new_len;
799 tree new_vec;
801 /* In the non-error case, we are processing a class
802 definition. Double the size of the vector to give room
803 for new methods. */
804 if (!error_p)
805 new_len = 2 * len;
806 /* In the error case, the vector is already complete. We
807 don't expect many errors, and the rest of the front-end
808 will get confused if there are empty slots in the vector. */
809 else
810 new_len = len + 1;
812 new_vec = make_tree_vec (new_len);
813 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
814 len * sizeof (tree));
815 len = new_len;
816 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
819 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
821 /* Type conversion operators have to come before ordinary
822 methods; add_conversions depends on this to speed up
823 looking for conversion operators. So, if necessary, we
824 slide some of the vector elements up. In theory, this
825 makes this algorithm O(N^2) but we don't expect many
826 conversion operators. */
827 if (template_conv_p)
828 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
829 else
830 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
832 tree fn = TREE_VEC_ELT (method_vec, slot);
834 if (!fn)
835 /* There are no more entries in the vector, so we
836 can insert the new conversion operator here. */
837 break;
839 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
840 /* We can insert the new function right at the
841 SLOTth position. */
842 break;
845 if (template_conv_p && have_template_convs_p)
846 /*OK*/;
847 else if (!TREE_VEC_ELT (method_vec, slot))
848 /* There is nothing in the Ith slot, so we can avoid
849 moving anything. */
851 else
853 /* We know the last slot in the vector is empty
854 because we know that at this point there's room
855 for a new function. */
856 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
857 &TREE_VEC_ELT (method_vec, slot),
858 (len - slot - 1) * sizeof (tree));
859 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
864 if (template_class_depth (type))
865 /* TYPE is a template class. Don't issue any errors now; wait
866 until instantiation time to complain. */
868 else
870 tree fns;
872 /* Check to see if we've already got this method. */
873 for (fns = TREE_VEC_ELT (method_vec, slot);
874 fns;
875 fns = OVL_NEXT (fns))
877 tree fn = OVL_CURRENT (fns);
878 tree parms1;
879 tree parms2;
880 bool same = 1;
882 if (TREE_CODE (fn) != TREE_CODE (method))
883 continue;
885 /* [over.load] Member function declarations with the
886 same name and the same parameter types cannot be
887 overloaded if any of them is a static member
888 function declaration.
890 [namespace.udecl] When a using-declaration brings names
891 from a base class into a derived class scope, member
892 functions in the derived class override and/or hide member
893 functions with the same name and parameter types in a base
894 class (rather than conflicting). */
895 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
896 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
898 /* Compare the quals on the 'this' parm. Don't compare
899 the whole types, as used functions are treated as
900 coming from the using class in overload resolution. */
901 if (! DECL_STATIC_FUNCTION_P (fn)
902 && ! DECL_STATIC_FUNCTION_P (method)
903 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
904 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
905 same = 0;
907 /* For templates, the template parms must be identical. */
908 if (TREE_CODE (fn) == TEMPLATE_DECL
909 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
910 DECL_TEMPLATE_PARMS (method)))
911 same = 0;
913 if (! DECL_STATIC_FUNCTION_P (fn))
914 parms1 = TREE_CHAIN (parms1);
915 if (! DECL_STATIC_FUNCTION_P (method))
916 parms2 = TREE_CHAIN (parms2);
918 if (same && compparms (parms1, parms2)
919 && (!DECL_CONV_FN_P (fn)
920 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
921 TREE_TYPE (TREE_TYPE (method)))))
923 if (using && DECL_CONTEXT (fn) == type)
924 /* Defer to the local function. */
925 return;
926 else
928 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
929 method, fn, method);
931 /* We don't call duplicate_decls here to merge
932 the declarations because that will confuse
933 things if the methods have inline
934 definitions. In particular, we will crash
935 while processing the definitions. */
936 return;
942 /* Actually insert the new method. */
943 TREE_VEC_ELT (method_vec, slot)
944 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
946 /* Add the new binding. */
947 if (!DECL_CONSTRUCTOR_P (method)
948 && !DECL_DESTRUCTOR_P (method))
949 push_class_level_binding (DECL_NAME (method),
950 TREE_VEC_ELT (method_vec, slot));
953 /* Subroutines of finish_struct. */
955 /* Look through the list of fields for this struct, deleting
956 duplicates as we go. This must be recursive to handle
957 anonymous unions.
959 FIELD is the field which may not appear anywhere in FIELDS.
960 FIELD_PTR, if non-null, is the starting point at which
961 chained deletions may take place.
962 The value returned is the first acceptable entry found
963 in FIELDS.
965 Note that anonymous fields which are not of UNION_TYPE are
966 not duplicates, they are just anonymous fields. This happens
967 when we have unnamed bitfields, for example. */
969 static tree
970 delete_duplicate_fields_1 (tree field, tree fields)
972 tree x;
973 tree prev = 0;
974 if (DECL_NAME (field) == 0)
976 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
977 return fields;
979 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
980 fields = delete_duplicate_fields_1 (x, fields);
981 return fields;
983 else
985 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
987 if (DECL_NAME (x) == 0)
989 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
990 continue;
991 TYPE_FIELDS (TREE_TYPE (x))
992 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
993 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
995 if (prev == 0)
996 fields = TREE_CHAIN (fields);
997 else
998 TREE_CHAIN (prev) = TREE_CHAIN (x);
1001 else if (TREE_CODE (field) == USING_DECL)
1002 /* A using declaration is allowed to appear more than
1003 once. We'll prune these from the field list later, and
1004 handle_using_decl will complain about invalid multiple
1005 uses. */
1007 else if (DECL_NAME (field) == DECL_NAME (x))
1009 if (TREE_CODE (field) == CONST_DECL
1010 && TREE_CODE (x) == CONST_DECL)
1011 cp_error_at ("duplicate enum value `%D'", x);
1012 else if (TREE_CODE (field) == CONST_DECL
1013 || TREE_CODE (x) == CONST_DECL)
1014 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1016 else if (DECL_DECLARES_TYPE_P (field)
1017 && DECL_DECLARES_TYPE_P (x))
1019 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1020 continue;
1021 cp_error_at ("duplicate nested type `%D'", x);
1023 else if (DECL_DECLARES_TYPE_P (field)
1024 || DECL_DECLARES_TYPE_P (x))
1026 /* Hide tag decls. */
1027 if ((TREE_CODE (field) == TYPE_DECL
1028 && DECL_ARTIFICIAL (field))
1029 || (TREE_CODE (x) == TYPE_DECL
1030 && DECL_ARTIFICIAL (x)))
1031 continue;
1032 cp_error_at ("duplicate field `%D' (as type and non-type)",
1035 else
1036 cp_error_at ("duplicate member `%D'", x);
1037 if (prev == 0)
1038 fields = TREE_CHAIN (fields);
1039 else
1040 TREE_CHAIN (prev) = TREE_CHAIN (x);
1044 return fields;
1047 static void
1048 delete_duplicate_fields (tree fields)
1050 tree x;
1051 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1052 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1055 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1056 legit, otherwise return 0. */
1058 static int
1059 alter_access (tree t, tree fdecl, tree access)
1061 tree elem;
1063 if (!DECL_LANG_SPECIFIC (fdecl))
1064 retrofit_lang_decl (fdecl);
1066 if (DECL_DISCRIMINATOR_P (fdecl))
1067 abort ();
1069 elem = purpose_member (t, DECL_ACCESS (fdecl));
1070 if (elem)
1072 if (TREE_VALUE (elem) != access)
1074 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1075 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1076 else
1077 error ("conflicting access specifications for field `%s', ignored",
1078 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1080 else
1082 /* They're changing the access to the same thing they changed
1083 it to before. That's OK. */
1087 else
1089 enforce_access (t, fdecl);
1090 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1091 return 1;
1093 return 0;
1096 /* Process the USING_DECL, which is a member of T. */
1098 static void
1099 handle_using_decl (tree using_decl, tree t)
1101 tree ctype = DECL_INITIAL (using_decl);
1102 tree name = DECL_NAME (using_decl);
1103 tree access
1104 = TREE_PRIVATE (using_decl) ? access_private_node
1105 : TREE_PROTECTED (using_decl) ? access_protected_node
1106 : access_public_node;
1107 tree fdecl, binfo;
1108 tree flist = NULL_TREE;
1109 tree old_value;
1111 binfo = lookup_base (t, ctype, ba_any, NULL);
1112 if (! binfo)
1114 error_not_base_type (t, ctype);
1115 return;
1118 if (constructor_name_p (name, ctype))
1120 cp_error_at ("`%D' names constructor", using_decl);
1121 return;
1123 if (constructor_name_p (name, t))
1125 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1126 return;
1129 fdecl = lookup_member (binfo, name, 0, false);
1131 if (!fdecl)
1133 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1134 return;
1137 if (BASELINK_P (fdecl))
1138 /* Ignore base type this came from. */
1139 fdecl = BASELINK_FUNCTIONS (fdecl);
1141 old_value = IDENTIFIER_CLASS_VALUE (name);
1142 if (old_value)
1144 if (is_overloaded_fn (old_value))
1145 old_value = OVL_CURRENT (old_value);
1147 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1148 /* OK */;
1149 else
1150 old_value = NULL_TREE;
1153 if (is_overloaded_fn (fdecl))
1154 flist = fdecl;
1156 if (! old_value)
1158 else if (is_overloaded_fn (old_value))
1160 if (flist)
1161 /* It's OK to use functions from a base when there are functions with
1162 the same name already present in the current class. */;
1163 else
1165 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1166 cp_error_at (" because of local method `%#D' with same name",
1167 OVL_CURRENT (old_value));
1168 return;
1171 else if (!DECL_ARTIFICIAL (old_value))
1173 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1174 cp_error_at (" because of local member `%#D' with same name", old_value);
1175 return;
1178 /* Make type T see field decl FDECL with access ACCESS.*/
1179 if (flist)
1180 for (; flist; flist = OVL_NEXT (flist))
1182 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1183 alter_access (t, OVL_CURRENT (flist), access);
1185 else
1186 alter_access (t, fdecl, access);
1189 /* Run through the base clases of T, updating
1190 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1191 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1192 the bases. */
1194 static void
1195 check_bases (tree t,
1196 int* cant_have_default_ctor_p,
1197 int* cant_have_const_ctor_p,
1198 int* no_const_asn_ref_p)
1200 int n_baseclasses;
1201 int i;
1202 int seen_non_virtual_nearly_empty_base_p;
1203 tree binfos;
1205 binfos = TYPE_BINFO_BASETYPES (t);
1206 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1207 seen_non_virtual_nearly_empty_base_p = 0;
1209 /* An aggregate cannot have baseclasses. */
1210 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1212 for (i = 0; i < n_baseclasses; ++i)
1214 tree base_binfo;
1215 tree basetype;
1217 /* Figure out what base we're looking at. */
1218 base_binfo = TREE_VEC_ELT (binfos, i);
1219 basetype = TREE_TYPE (base_binfo);
1221 /* If the type of basetype is incomplete, then we already
1222 complained about that fact (and we should have fixed it up as
1223 well). */
1224 if (!COMPLETE_TYPE_P (basetype))
1226 int j;
1227 /* The base type is of incomplete type. It is
1228 probably best to pretend that it does not
1229 exist. */
1230 if (i == n_baseclasses-1)
1231 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1232 TREE_VEC_LENGTH (binfos) -= 1;
1233 n_baseclasses -= 1;
1234 for (j = i; j+1 < n_baseclasses; j++)
1235 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1236 continue;
1239 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1240 here because the case of virtual functions but non-virtual
1241 dtor is handled in finish_struct_1. */
1242 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1243 && TYPE_HAS_DESTRUCTOR (basetype))
1244 warning ("base class `%#T' has a non-virtual destructor",
1245 basetype);
1247 /* If the base class doesn't have copy constructors or
1248 assignment operators that take const references, then the
1249 derived class cannot have such a member automatically
1250 generated. */
1251 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1252 *cant_have_const_ctor_p = 1;
1253 if (TYPE_HAS_ASSIGN_REF (basetype)
1254 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1255 *no_const_asn_ref_p = 1;
1256 /* Similarly, if the base class doesn't have a default
1257 constructor, then the derived class won't have an
1258 automatically generated default constructor. */
1259 if (TYPE_HAS_CONSTRUCTOR (basetype)
1260 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1262 *cant_have_default_ctor_p = 1;
1263 if (! TYPE_HAS_CONSTRUCTOR (t))
1264 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1265 basetype);
1268 if (TREE_VIA_VIRTUAL (base_binfo))
1269 /* A virtual base does not effect nearly emptiness. */
1271 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1273 if (seen_non_virtual_nearly_empty_base_p)
1274 /* And if there is more than one nearly empty base, then the
1275 derived class is not nearly empty either. */
1276 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1277 else
1278 /* Remember we've seen one. */
1279 seen_non_virtual_nearly_empty_base_p = 1;
1281 else if (!is_empty_class (basetype))
1282 /* If the base class is not empty or nearly empty, then this
1283 class cannot be nearly empty. */
1284 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1286 /* A lot of properties from the bases also apply to the derived
1287 class. */
1288 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1289 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1290 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1291 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1292 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1293 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1294 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1295 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1296 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1297 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1298 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1299 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1303 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1304 dominated by TYPE that are primary bases. */
1306 static void
1307 mark_primary_bases (tree type)
1309 tree binfo;
1311 /* Walk the bases in inheritance graph order. */
1312 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1314 tree base_binfo = get_primary_binfo (binfo);
1316 if (!base_binfo)
1317 /* Not a dynamic base. */;
1318 else if (BINFO_PRIMARY_P (base_binfo))
1319 BINFO_LOST_PRIMARY_P (binfo) = 1;
1320 else
1322 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1323 /* A virtual binfo might have been copied from within
1324 another hierarchy. As we're about to use it as a primary
1325 base, make sure the offsets match. */
1326 if (TREE_VIA_VIRTUAL (base_binfo))
1328 tree delta = size_diffop (convert (ssizetype,
1329 BINFO_OFFSET (binfo)),
1330 convert (ssizetype,
1331 BINFO_OFFSET (base_binfo)));
1333 propagate_binfo_offsets (base_binfo, delta);
1339 /* Make the BINFO the primary base of T. */
1341 static void
1342 set_primary_base (tree t, tree binfo)
1344 tree basetype;
1346 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1347 basetype = BINFO_TYPE (binfo);
1348 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1349 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1350 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1353 /* Determine the primary class for T. */
1355 static void
1356 determine_primary_base (tree t)
1358 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1359 tree vbases;
1360 tree type_binfo;
1362 /* If there are no baseclasses, there is certainly no primary base. */
1363 if (n_baseclasses == 0)
1364 return;
1366 type_binfo = TYPE_BINFO (t);
1368 for (i = 0; i < n_baseclasses; i++)
1370 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1371 tree basetype = BINFO_TYPE (base_binfo);
1373 if (TYPE_CONTAINS_VPTR_P (basetype))
1375 /* We prefer a non-virtual base, although a virtual one will
1376 do. */
1377 if (TREE_VIA_VIRTUAL (base_binfo))
1378 continue;
1380 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1382 set_primary_base (t, base_binfo);
1383 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1385 else
1387 tree vfields;
1389 /* Only add unique vfields, and flatten them out as we go. */
1390 for (vfields = CLASSTYPE_VFIELDS (basetype);
1391 vfields;
1392 vfields = TREE_CHAIN (vfields))
1393 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1394 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1395 CLASSTYPE_VFIELDS (t)
1396 = tree_cons (base_binfo,
1397 VF_BASETYPE_VALUE (vfields),
1398 CLASSTYPE_VFIELDS (t));
1403 if (!TYPE_VFIELD (t))
1404 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1406 /* Find the indirect primary bases - those virtual bases which are primary
1407 bases of something else in this hierarchy. */
1408 for (vbases = CLASSTYPE_VBASECLASSES (t);
1409 vbases;
1410 vbases = TREE_CHAIN (vbases))
1412 tree vbase_binfo = TREE_VALUE (vbases);
1414 /* See if this virtual base is an indirect primary base. To be so,
1415 it must be a primary base within the hierarchy of one of our
1416 direct bases. */
1417 for (i = 0; i < n_baseclasses; ++i)
1419 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1420 tree v;
1422 for (v = CLASSTYPE_VBASECLASSES (basetype);
1424 v = TREE_CHAIN (v))
1426 tree base_vbase = TREE_VALUE (v);
1428 if (BINFO_PRIMARY_P (base_vbase)
1429 && same_type_p (BINFO_TYPE (base_vbase),
1430 BINFO_TYPE (vbase_binfo)))
1432 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1433 break;
1437 /* If we've discovered that this virtual base is an indirect
1438 primary base, then we can move on to the next virtual
1439 base. */
1440 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1441 break;
1445 /* A "nearly-empty" virtual base class can be the primary base
1446 class, if no non-virtual polymorphic base can be found. */
1447 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1449 /* If not NULL, this is the best primary base candidate we have
1450 found so far. */
1451 tree candidate = NULL_TREE;
1452 tree base_binfo;
1454 /* Loop over the baseclasses. */
1455 for (base_binfo = TYPE_BINFO (t);
1456 base_binfo;
1457 base_binfo = TREE_CHAIN (base_binfo))
1459 tree basetype = BINFO_TYPE (base_binfo);
1461 if (TREE_VIA_VIRTUAL (base_binfo)
1462 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1464 /* If this is not an indirect primary base, then it's
1465 definitely our primary base. */
1466 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1468 candidate = base_binfo;
1469 break;
1472 /* If this is an indirect primary base, it still could be
1473 our primary base -- unless we later find there's another
1474 nearly-empty virtual base that isn't an indirect
1475 primary base. */
1476 if (!candidate)
1477 candidate = base_binfo;
1481 /* If we've got a primary base, use it. */
1482 if (candidate)
1484 set_primary_base (t, candidate);
1485 CLASSTYPE_VFIELDS (t)
1486 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1490 /* Mark the primary base classes at this point. */
1491 mark_primary_bases (t);
1494 /* Set memoizing fields and bits of T (and its variants) for later
1495 use. */
1497 static void
1498 finish_struct_bits (tree t)
1500 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1502 /* Fix up variants (if any). */
1503 tree variants = TYPE_NEXT_VARIANT (t);
1504 while (variants)
1506 /* These fields are in the _TYPE part of the node, not in
1507 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1508 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1509 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1510 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1511 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1512 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1514 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1515 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1516 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1517 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1518 /* Copy whatever these are holding today. */
1519 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1520 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1521 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1522 TYPE_SIZE (variants) = TYPE_SIZE (t);
1523 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1524 variants = TYPE_NEXT_VARIANT (variants);
1527 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1528 /* For a class w/o baseclasses, `finish_struct' has set
1529 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1530 definition). Similarly for a class whose base classes do not
1531 have vtables. When neither of these is true, we might have
1532 removed abstract virtuals (by providing a definition), added
1533 some (by declaring new ones), or redeclared ones from a base
1534 class. We need to recalculate what's really an abstract virtual
1535 at this point (by looking in the vtables). */
1536 get_pure_virtuals (t);
1538 if (n_baseclasses)
1540 /* Notice whether this class has type conversion functions defined. */
1541 tree binfo = TYPE_BINFO (t);
1542 tree binfos = BINFO_BASETYPES (binfo);
1543 tree basetype;
1545 for (i = n_baseclasses-1; i >= 0; i--)
1547 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1549 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1553 /* If this type has a copy constructor or a destructor, force its mode to
1554 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1555 will cause it to be passed by invisible reference and prevent it from
1556 being returned in a register. */
1557 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1559 tree variants;
1560 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1561 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1563 TYPE_MODE (variants) = BLKmode;
1564 TREE_ADDRESSABLE (variants) = 1;
1569 /* Issue warnings about T having private constructors, but no friends,
1570 and so forth.
1572 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1573 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1574 non-private static member functions. */
1576 static void
1577 maybe_warn_about_overly_private_class (tree t)
1579 int has_member_fn = 0;
1580 int has_nonprivate_method = 0;
1581 tree fn;
1583 if (!warn_ctor_dtor_privacy
1584 /* If the class has friends, those entities might create and
1585 access instances, so we should not warn. */
1586 || (CLASSTYPE_FRIEND_CLASSES (t)
1587 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1588 /* We will have warned when the template was declared; there's
1589 no need to warn on every instantiation. */
1590 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1591 /* There's no reason to even consider warning about this
1592 class. */
1593 return;
1595 /* We only issue one warning, if more than one applies, because
1596 otherwise, on code like:
1598 class A {
1599 // Oops - forgot `public:'
1600 A();
1601 A(const A&);
1602 ~A();
1605 we warn several times about essentially the same problem. */
1607 /* Check to see if all (non-constructor, non-destructor) member
1608 functions are private. (Since there are no friends or
1609 non-private statics, we can't ever call any of the private member
1610 functions.) */
1611 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1612 /* We're not interested in compiler-generated methods; they don't
1613 provide any way to call private members. */
1614 if (!DECL_ARTIFICIAL (fn))
1616 if (!TREE_PRIVATE (fn))
1618 if (DECL_STATIC_FUNCTION_P (fn))
1619 /* A non-private static member function is just like a
1620 friend; it can create and invoke private member
1621 functions, and be accessed without a class
1622 instance. */
1623 return;
1625 has_nonprivate_method = 1;
1626 /* Keep searching for a static member function. */
1628 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1629 has_member_fn = 1;
1632 if (!has_nonprivate_method && has_member_fn)
1634 /* There are no non-private methods, and there's at least one
1635 private member function that isn't a constructor or
1636 destructor. (If all the private members are
1637 constructors/destructors we want to use the code below that
1638 issues error messages specifically referring to
1639 constructors/destructors.) */
1640 int i;
1641 tree binfo = TYPE_BINFO (t);
1643 for (i = 0; i < BINFO_N_BASETYPES (binfo); i++)
1644 if (BINFO_BASEACCESS (binfo, i) != access_private_node)
1646 has_nonprivate_method = 1;
1647 break;
1649 if (!has_nonprivate_method)
1651 warning ("all member functions in class `%T' are private", t);
1652 return;
1656 /* Even if some of the member functions are non-private, the class
1657 won't be useful for much if all the constructors or destructors
1658 are private: such an object can never be created or destroyed. */
1659 if (TYPE_HAS_DESTRUCTOR (t))
1661 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1663 if (TREE_PRIVATE (dtor))
1665 warning ("`%#T' only defines a private destructor and has no friends",
1667 return;
1671 if (TYPE_HAS_CONSTRUCTOR (t))
1673 int nonprivate_ctor = 0;
1675 /* If a non-template class does not define a copy
1676 constructor, one is defined for it, enabling it to avoid
1677 this warning. For a template class, this does not
1678 happen, and so we would normally get a warning on:
1680 template <class T> class C { private: C(); };
1682 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1683 complete non-template or fully instantiated classes have this
1684 flag set. */
1685 if (!TYPE_HAS_INIT_REF (t))
1686 nonprivate_ctor = 1;
1687 else
1688 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1690 fn = OVL_NEXT (fn))
1692 tree ctor = OVL_CURRENT (fn);
1693 /* Ideally, we wouldn't count copy constructors (or, in
1694 fact, any constructor that takes an argument of the
1695 class type as a parameter) because such things cannot
1696 be used to construct an instance of the class unless
1697 you already have one. But, for now at least, we're
1698 more generous. */
1699 if (! TREE_PRIVATE (ctor))
1701 nonprivate_ctor = 1;
1702 break;
1706 if (nonprivate_ctor == 0)
1708 warning ("`%#T' only defines private constructors and has no friends",
1710 return;
1715 /* Function to help qsort sort FIELD_DECLs by name order. */
1717 static int
1718 field_decl_cmp (const void* x_p, const void* y_p)
1720 const tree *const x = x_p;
1721 const tree *const y = y_p;
1722 if (DECL_NAME (*x) == DECL_NAME (*y))
1723 /* A nontype is "greater" than a type. */
1724 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1725 if (DECL_NAME (*x) == NULL_TREE)
1726 return -1;
1727 if (DECL_NAME (*y) == NULL_TREE)
1728 return 1;
1729 if (DECL_NAME (*x) < DECL_NAME (*y))
1730 return -1;
1731 return 1;
1734 static struct {
1735 gt_pointer_operator new_value;
1736 void *cookie;
1737 } resort_data;
1739 /* This routine compares two fields like field_decl_cmp but using the
1740 pointer operator in resort_data. */
1742 static int
1743 resort_field_decl_cmp (const void* x_p, const void* y_p)
1745 const tree *const x = x_p;
1746 const tree *const y = y_p;
1748 if (DECL_NAME (*x) == DECL_NAME (*y))
1749 /* A nontype is "greater" than a type. */
1750 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1751 if (DECL_NAME (*x) == NULL_TREE)
1752 return -1;
1753 if (DECL_NAME (*y) == NULL_TREE)
1754 return 1;
1756 tree d1 = DECL_NAME (*x);
1757 tree d2 = DECL_NAME (*y);
1758 resort_data.new_value (&d1, resort_data.cookie);
1759 resort_data.new_value (&d2, resort_data.cookie);
1760 if (d1 < d2)
1761 return -1;
1763 return 1;
1766 /* Resort DECL_SORTED_FIELDS because pointers have been reordered. */
1768 void
1769 resort_sorted_fields (void* obj,
1770 void* orig_obj ATTRIBUTE_UNUSED ,
1771 gt_pointer_operator new_value,
1772 void* cookie)
1774 tree sf = obj;
1775 resort_data.new_value = new_value;
1776 resort_data.cookie = cookie;
1777 qsort (&TREE_VEC_ELT (sf, 0), TREE_VEC_LENGTH (sf), sizeof (tree),
1778 resort_field_decl_cmp);
1781 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1783 static int
1784 method_name_cmp (const void* m1_p, const void* m2_p)
1786 const tree *const m1 = m1_p;
1787 const tree *const m2 = m2_p;
1789 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1790 return 0;
1791 if (*m1 == NULL_TREE)
1792 return -1;
1793 if (*m2 == NULL_TREE)
1794 return 1;
1795 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1796 return -1;
1797 return 1;
1800 /* This routine compares two fields like method_name_cmp but using the
1801 pointer operator in resort_field_decl_data. */
1803 static int
1804 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1806 const tree *const m1 = m1_p;
1807 const tree *const m2 = m2_p;
1808 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1809 return 0;
1810 if (*m1 == NULL_TREE)
1811 return -1;
1812 if (*m2 == NULL_TREE)
1813 return 1;
1815 tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1816 tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1817 resort_data.new_value (&d1, resort_data.cookie);
1818 resort_data.new_value (&d2, resort_data.cookie);
1819 if (d1 < d2)
1820 return -1;
1822 return 1;
1825 /* Resort TYPE_METHOD_VEC because pointers have been reordered. */
1827 void
1828 resort_type_method_vec (void* obj,
1829 void* orig_obj ATTRIBUTE_UNUSED ,
1830 gt_pointer_operator new_value,
1831 void* cookie)
1833 tree method_vec = obj;
1834 int len = TREE_VEC_LENGTH (method_vec);
1835 int slot;
1837 /* The type conversion ops have to live at the front of the vec, so we
1838 can't sort them. */
1839 for (slot = 2; slot < len; ++slot)
1841 tree fn = TREE_VEC_ELT (method_vec, slot);
1843 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1844 break;
1846 if (len - slot > 1)
1848 resort_data.new_value = new_value;
1849 resort_data.cookie = cookie;
1850 qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
1851 resort_method_name_cmp);
1855 /* Warn about duplicate methods in fn_fields. Also compact method
1856 lists so that lookup can be made faster.
1858 Data Structure: List of method lists. The outer list is a
1859 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1860 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1861 links the entire list of methods for TYPE_METHODS. Friends are
1862 chained in the same way as member functions (? TREE_CHAIN or
1863 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1864 list. That allows them to be quickly deleted, and requires no
1865 extra storage.
1867 Sort methods that are not special (i.e., constructors, destructors,
1868 and type conversion operators) so that we can find them faster in
1869 search. */
1871 static void
1872 finish_struct_methods (tree t)
1874 tree fn_fields;
1875 tree method_vec;
1876 int slot, len;
1878 if (!TYPE_METHODS (t))
1880 /* Clear these for safety; perhaps some parsing error could set
1881 these incorrectly. */
1882 TYPE_HAS_CONSTRUCTOR (t) = 0;
1883 TYPE_HAS_DESTRUCTOR (t) = 0;
1884 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1885 return;
1888 method_vec = CLASSTYPE_METHOD_VEC (t);
1889 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1890 len = TREE_VEC_LENGTH (method_vec);
1892 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1893 and the next few with type conversion operators (if any). */
1894 for (fn_fields = TYPE_METHODS (t); fn_fields;
1895 fn_fields = TREE_CHAIN (fn_fields))
1896 /* Clear out this flag. */
1897 DECL_IN_AGGR_P (fn_fields) = 0;
1899 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
1900 /* We thought there was a destructor, but there wasn't. Some
1901 parse errors cause this anomalous situation. */
1902 TYPE_HAS_DESTRUCTOR (t) = 0;
1904 /* Issue warnings about private constructors and such. If there are
1905 no methods, then some public defaults are generated. */
1906 maybe_warn_about_overly_private_class (t);
1908 /* Now sort the methods. */
1909 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
1910 len--;
1911 TREE_VEC_LENGTH (method_vec) = len;
1913 /* The type conversion ops have to live at the front of the vec, so we
1914 can't sort them. */
1915 for (slot = 2; slot < len; ++slot)
1917 tree fn = TREE_VEC_ELT (method_vec, slot);
1919 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1920 break;
1922 if (len - slot > 1)
1923 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
1924 method_name_cmp);
1927 /* Emit error when a duplicate definition of a type is seen. Patch up. */
1929 void
1930 duplicate_tag_error (tree t)
1932 error ("redefinition of `%#T'", t);
1933 cp_error_at ("previous definition of `%#T'", t);
1935 /* Pretend we haven't defined this type. */
1937 /* All of the component_decl's were TREE_CHAINed together in the parser.
1938 finish_struct_methods walks these chains and assembles all methods with
1939 the same base name into DECL_CHAINs. Now we don't need the parser chains
1940 anymore, so we unravel them. */
1942 /* This used to be in finish_struct, but it turns out that the
1943 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
1944 things... */
1945 if (CLASSTYPE_METHOD_VEC (t))
1947 tree method_vec = CLASSTYPE_METHOD_VEC (t);
1948 int i, len = TREE_VEC_LENGTH (method_vec);
1949 for (i = 0; i < len; i++)
1951 tree unchain = TREE_VEC_ELT (method_vec, i);
1952 while (unchain != NULL_TREE)
1954 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
1955 unchain = OVL_NEXT (unchain);
1960 if (TYPE_LANG_SPECIFIC (t))
1962 tree binfo = TYPE_BINFO (t);
1963 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
1964 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
1965 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
1966 int use_template = CLASSTYPE_USE_TEMPLATE (t);
1968 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
1969 BINFO_BASETYPES(binfo) = NULL_TREE;
1971 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
1972 TYPE_BINFO (t) = binfo;
1973 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
1974 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
1975 TYPE_REDEFINED (t) = 1;
1976 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
1977 CLASSTYPE_USE_TEMPLATE (t) = use_template;
1978 CLASSTYPE_DECL_LIST (t) = NULL_TREE;
1980 TYPE_SIZE (t) = NULL_TREE;
1981 TYPE_MODE (t) = VOIDmode;
1982 TYPE_FIELDS (t) = NULL_TREE;
1983 TYPE_METHODS (t) = NULL_TREE;
1984 TYPE_VFIELD (t) = NULL_TREE;
1985 TYPE_CONTEXT (t) = NULL_TREE;
1987 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
1988 TYPE_LANG_FLAG_0 (t) = 0;
1989 TYPE_LANG_FLAG_1 (t) = 0;
1990 TYPE_LANG_FLAG_2 (t) = 0;
1991 TYPE_LANG_FLAG_3 (t) = 0;
1992 TYPE_LANG_FLAG_4 (t) = 0;
1993 TYPE_LANG_FLAG_5 (t) = 0;
1994 TYPE_LANG_FLAG_6 (t) = 0;
1995 /* But not this one. */
1996 SET_IS_AGGR_TYPE (t, 1);
1999 /* Make BINFO's vtable have N entries, including RTTI entries,
2000 vbase and vcall offsets, etc. Set its type and call the backend
2001 to lay it out. */
2003 static void
2004 layout_vtable_decl (tree binfo, int n)
2006 tree atype;
2007 tree vtable;
2009 atype = build_cplus_array_type (vtable_entry_type,
2010 build_index_type (size_int (n - 1)));
2011 layout_type (atype);
2013 /* We may have to grow the vtable. */
2014 vtable = get_vtbl_decl_for_binfo (binfo);
2015 if (!same_type_p (TREE_TYPE (vtable), atype))
2017 TREE_TYPE (vtable) = atype;
2018 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2019 layout_decl (vtable, 0);
2021 /* At one time the vtable info was grabbed 2 words at a time. This
2022 fails on SPARC unless you have 8-byte alignment. */
2023 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2024 DECL_ALIGN (vtable));
2028 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2029 have the same signature. */
2032 same_signature_p (tree fndecl, tree base_fndecl)
2034 /* One destructor overrides another if they are the same kind of
2035 destructor. */
2036 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2037 && special_function_p (base_fndecl) == special_function_p (fndecl))
2038 return 1;
2039 /* But a non-destructor never overrides a destructor, nor vice
2040 versa, nor do different kinds of destructors override
2041 one-another. For example, a complete object destructor does not
2042 override a deleting destructor. */
2043 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2044 return 0;
2046 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2048 tree types, base_types;
2049 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2050 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2051 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2052 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2053 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2054 return 1;
2056 return 0;
2059 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2060 subobject. */
2062 static bool
2063 base_derived_from (tree derived, tree base)
2065 tree probe;
2067 for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
2069 if (probe == derived)
2070 return true;
2071 else if (TREE_VIA_VIRTUAL (probe))
2072 /* If we meet a virtual base, we can't follow the inheritance
2073 any more. See if the complete type of DERIVED contains
2074 such a virtual base. */
2075 return purpose_member (BINFO_TYPE (probe),
2076 CLASSTYPE_VBASECLASSES (BINFO_TYPE (derived)))
2077 != NULL_TREE;
2079 return false;
2082 typedef struct find_final_overrider_data_s {
2083 /* The function for which we are trying to find a final overrider. */
2084 tree fn;
2085 /* The base class in which the function was declared. */
2086 tree declaring_base;
2087 /* The most derived class in the hierarchy. */
2088 tree most_derived_type;
2089 /* The candidate overriders. */
2090 tree candidates;
2091 /* Binfos which inherited virtually on the currrent path. */
2092 tree vpath;
2093 } find_final_overrider_data;
2095 /* Called from find_final_overrider via dfs_walk. */
2097 static tree
2098 dfs_find_final_overrider (tree binfo, void* data)
2100 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2102 if (binfo == ffod->declaring_base)
2104 /* We've found a path to the declaring base. Walk the path from
2105 derived to base, looking for an overrider for FN. */
2106 tree path, probe, vpath;
2108 /* Build the path, using the inheritance chain and record of
2109 virtual inheritance. */
2110 for (path = NULL_TREE, probe = binfo, vpath = ffod->vpath;;)
2112 path = tree_cons (NULL_TREE, probe, path);
2113 if (same_type_p (BINFO_TYPE (probe), ffod->most_derived_type))
2114 break;
2115 if (TREE_VIA_VIRTUAL (probe))
2117 probe = TREE_VALUE (vpath);
2118 vpath = TREE_CHAIN (vpath);
2120 else
2121 probe = BINFO_INHERITANCE_CHAIN (probe);
2123 /* Now walk path, looking for overrides. */
2124 for (; path; path = TREE_CHAIN (path))
2126 tree method = look_for_overrides_here
2127 (BINFO_TYPE (TREE_VALUE (path)), ffod->fn);
2129 if (method)
2131 tree *candidate = &ffod->candidates;
2132 path = TREE_VALUE (path);
2134 /* Remove any candidates overridden by this new function. */
2135 while (*candidate)
2137 /* If *CANDIDATE overrides METHOD, then METHOD
2138 cannot override anything else on the list. */
2139 if (base_derived_from (TREE_VALUE (*candidate), path))
2140 return NULL_TREE;
2141 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2142 if (base_derived_from (path, TREE_VALUE (*candidate)))
2143 *candidate = TREE_CHAIN (*candidate);
2144 else
2145 candidate = &TREE_CHAIN (*candidate);
2148 /* Add the new function. */
2149 ffod->candidates = tree_cons (method, path, ffod->candidates);
2150 break;
2155 return NULL_TREE;
2158 static tree
2159 dfs_find_final_overrider_q (tree derived, int ix, void *data)
2161 tree binfo = BINFO_BASETYPE (derived, ix);
2162 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2164 if (TREE_VIA_VIRTUAL (binfo))
2165 ffod->vpath = tree_cons (NULL_TREE, derived, ffod->vpath);
2167 return binfo;
2170 static tree
2171 dfs_find_final_overrider_post (tree binfo, void *data)
2173 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2175 if (TREE_VIA_VIRTUAL (binfo) && TREE_CHAIN (ffod->vpath))
2176 ffod->vpath = TREE_CHAIN (ffod->vpath);
2178 return NULL_TREE;
2181 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2182 FN and whose TREE_VALUE is the binfo for the base where the
2183 overriding occurs. BINFO (in the hierarchy dominated by the binfo
2184 DERIVED) is the base object in which FN is declared. */
2186 static tree
2187 find_final_overrider (tree derived, tree binfo, tree fn)
2189 find_final_overrider_data ffod;
2191 /* Getting this right is a little tricky. This is valid:
2193 struct S { virtual void f (); };
2194 struct T { virtual void f (); };
2195 struct U : public S, public T { };
2197 even though calling `f' in `U' is ambiguous. But,
2199 struct R { virtual void f(); };
2200 struct S : virtual public R { virtual void f (); };
2201 struct T : virtual public R { virtual void f (); };
2202 struct U : public S, public T { };
2204 is not -- there's no way to decide whether to put `S::f' or
2205 `T::f' in the vtable for `R'.
2207 The solution is to look at all paths to BINFO. If we find
2208 different overriders along any two, then there is a problem. */
2209 if (DECL_THUNK_P (fn))
2210 fn = THUNK_TARGET (fn);
2212 ffod.fn = fn;
2213 ffod.declaring_base = binfo;
2214 ffod.most_derived_type = BINFO_TYPE (derived);
2215 ffod.candidates = NULL_TREE;
2216 ffod.vpath = NULL_TREE;
2218 dfs_walk_real (derived,
2219 dfs_find_final_overrider,
2220 dfs_find_final_overrider_post,
2221 dfs_find_final_overrider_q,
2222 &ffod);
2224 /* If there was no winner, issue an error message. */
2225 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2227 error ("no unique final overrider for `%D' in `%T'", fn,
2228 BINFO_TYPE (derived));
2229 return error_mark_node;
2232 return ffod.candidates;
2235 /* Return the index of the vcall offset for FN when TYPE is used as a
2236 virtual base. */
2238 static tree
2239 get_vcall_index (tree fn, tree type)
2241 tree v;
2243 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2244 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2245 || same_signature_p (fn, TREE_PURPOSE (v)))
2246 break;
2248 /* There should always be an appropriate index. */
2249 my_friendly_assert (v, 20021103);
2251 return TREE_VALUE (v);
2254 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2255 dominated by T. FN has been overridden in BINFO; VIRTUALS points to the
2256 corresponding position in the BINFO_VIRTUALS list. */
2258 static void
2259 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
2260 unsigned ix)
2262 tree b;
2263 tree overrider;
2264 tree delta;
2265 tree virtual_base;
2266 tree first_defn;
2267 tree overrider_fn, overrider_target;
2268 tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2269 tree over_return, base_return;
2270 bool lost = false;
2272 /* Find the nearest primary base (possibly binfo itself) which defines
2273 this function; this is the class the caller will convert to when
2274 calling FN through BINFO. */
2275 for (b = binfo; ; b = get_primary_binfo (b))
2277 my_friendly_assert (b, 20021227);
2278 if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2279 break;
2281 /* The nearest definition is from a lost primary. */
2282 if (BINFO_LOST_PRIMARY_P (b))
2283 lost = true;
2285 first_defn = b;
2287 /* Find the final overrider. */
2288 overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2289 if (overrider == error_mark_node)
2290 return;
2291 overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2293 /* Check for adjusting covariant return types. */
2294 over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2295 base_return = TREE_TYPE (TREE_TYPE (target_fn));
2297 if (POINTER_TYPE_P (over_return)
2298 && TREE_CODE (over_return) == TREE_CODE (base_return)
2299 && CLASS_TYPE_P (TREE_TYPE (over_return))
2300 && CLASS_TYPE_P (TREE_TYPE (base_return)))
2302 /* If FN is a covariant thunk, we must figure out the adjustment
2303 to the final base FN was converting to. As OVERRIDER_TARGET might
2304 also be converting to the return type of FN, we have to
2305 combine the two conversions here. */
2306 tree fixed_offset, virtual_offset;
2308 if (DECL_THUNK_P (fn))
2310 fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2311 virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2313 else
2314 fixed_offset = virtual_offset = NULL_TREE;
2316 if (!virtual_offset)
2318 /* There was no existing virtual thunk (which takes
2319 precidence). */
2320 tree thunk_binfo;
2321 base_kind kind;
2323 thunk_binfo = lookup_base (TREE_TYPE (over_return),
2324 TREE_TYPE (base_return),
2325 ba_check | ba_quiet, &kind);
2327 if (thunk_binfo && (kind == bk_via_virtual
2328 || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2330 tree offset = BINFO_OFFSET (thunk_binfo);
2332 if (kind == bk_via_virtual)
2334 /* We convert via virtual base. Find the virtual
2335 base and adjust the fixed offset to be from there. */
2336 while (!TREE_VIA_VIRTUAL (thunk_binfo))
2337 thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2339 virtual_offset = thunk_binfo;
2340 offset = size_binop (MINUS_EXPR, offset,
2341 BINFO_OFFSET (virtual_offset));
2343 if (fixed_offset)
2344 /* There was an existing fixed offset, this must be
2345 from the base just converted to, and the base the
2346 FN was thunking to. */
2347 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2348 else
2349 fixed_offset = offset;
2353 if (fixed_offset || virtual_offset)
2354 /* Replace the overriding function with a covariant thunk. We
2355 will emit the overriding function in its own slot as
2356 well. */
2357 overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2358 fixed_offset, virtual_offset);
2360 else
2361 my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2363 /* Assume that we will produce a thunk that convert all the way to
2364 the final overrider, and not to an intermediate virtual base. */
2365 virtual_base = NULL_TREE;
2367 /* See if we can convert to an intermediate virtual base first, and then
2368 use the vcall offset located there to finish the conversion. */
2369 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2371 /* If we find the final overrider, then we can stop
2372 walking. */
2373 if (same_type_p (BINFO_TYPE (b),
2374 BINFO_TYPE (TREE_VALUE (overrider))))
2375 break;
2377 /* If we find a virtual base, and we haven't yet found the
2378 overrider, then there is a virtual base between the
2379 declaring base (first_defn) and the final overrider. */
2380 if (TREE_VIA_VIRTUAL (b))
2382 virtual_base = b;
2383 break;
2387 if (overrider_fn != overrider_target && !virtual_base)
2389 /* The ABI specifies that a covariant thunk includes a mangling
2390 for a this pointer adjustment. This-adjusting thunks that
2391 override a function from a virtual base have a vcall
2392 adjustment. When the virtual base in question is a primary
2393 virtual base, we know the adjustments are zero, (and in the
2394 non-covariant case, we would not use the thunk).
2395 Unfortunately we didn't notice this could happen, when
2396 designing the ABI and so never mandated that such a covariant
2397 thunk should be emitted. Because we must use the ABI mandated
2398 name, we must continue searching from the binfo where we
2399 found the most recent definition of the function, towards the
2400 primary binfo which first introduced the function into the
2401 vtable. If that enters a virtual base, we must use a vcall
2402 this-adjusting thunk. Bleah! */
2403 tree probe;
2405 for (probe = first_defn; (probe = get_primary_binfo (probe));)
2407 if (TREE_VIA_VIRTUAL (probe))
2408 virtual_base = probe;
2409 if ((unsigned) list_length (BINFO_VIRTUALS (probe)) <= ix)
2410 break;
2412 if (virtual_base)
2413 /* Even if we find a virtual base, the correct delta is
2414 between the overrider and the binfo we're building a vtable
2415 for. */
2416 goto virtual_covariant;
2419 /* Compute the constant adjustment to the `this' pointer. The
2420 `this' pointer, when this function is called, will point at BINFO
2421 (or one of its primary bases, which are at the same offset). */
2422 if (virtual_base)
2423 /* The `this' pointer needs to be adjusted from the declaration to
2424 the nearest virtual base. */
2425 delta = size_diffop (BINFO_OFFSET (virtual_base),
2426 BINFO_OFFSET (first_defn));
2427 else if (lost)
2428 /* If the nearest definition is in a lost primary, we don't need an
2429 entry in our vtable. Except possibly in a constructor vtable,
2430 if we happen to get our primary back. In that case, the offset
2431 will be zero, as it will be a primary base. */
2432 delta = size_zero_node;
2433 else
2434 /* The `this' pointer needs to be adjusted from pointing to
2435 BINFO to pointing at the base where the final overrider
2436 appears. */
2437 virtual_covariant:
2438 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2439 BINFO_OFFSET (binfo));
2441 modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2443 if (virtual_base)
2444 BV_VCALL_INDEX (*virtuals)
2445 = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2448 /* Called from modify_all_vtables via dfs_walk. */
2450 static tree
2451 dfs_modify_vtables (tree binfo, void* data)
2453 if (/* There's no need to modify the vtable for a non-virtual
2454 primary base; we're not going to use that vtable anyhow.
2455 We do still need to do this for virtual primary bases, as they
2456 could become non-primary in a construction vtable. */
2457 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2458 /* Similarly, a base without a vtable needs no modification. */
2459 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2461 tree t = (tree) data;
2462 tree virtuals;
2463 tree old_virtuals;
2464 unsigned ix;
2466 make_new_vtable (t, binfo);
2468 /* Now, go through each of the virtual functions in the virtual
2469 function table for BINFO. Find the final overrider, and
2470 update the BINFO_VIRTUALS list appropriately. */
2471 for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
2472 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2473 virtuals;
2474 ix++, virtuals = TREE_CHAIN (virtuals),
2475 old_virtuals = TREE_CHAIN (old_virtuals))
2476 update_vtable_entry_for_fn (t,
2477 binfo,
2478 BV_FN (old_virtuals),
2479 &virtuals, ix);
2482 BINFO_MARKED (binfo) = 1;
2484 return NULL_TREE;
2487 /* Update all of the primary and secondary vtables for T. Create new
2488 vtables as required, and initialize their RTTI information. Each
2489 of the functions in VIRTUALS is declared in T and may override a
2490 virtual function from a base class; find and modify the appropriate
2491 entries to point to the overriding functions. Returns a list, in
2492 declaration order, of the virtual functions that are declared in T,
2493 but do not appear in the primary base class vtable, and which
2494 should therefore be appended to the end of the vtable for T. */
2496 static tree
2497 modify_all_vtables (tree t, tree virtuals)
2499 tree binfo = TYPE_BINFO (t);
2500 tree *fnsp;
2502 /* Update all of the vtables. */
2503 dfs_walk (binfo, dfs_modify_vtables, unmarkedp, t);
2504 dfs_walk (binfo, dfs_unmark, markedp, t);
2506 /* Add virtual functions not already in our primary vtable. These
2507 will be both those introduced by this class, and those overridden
2508 from secondary bases. It does not include virtuals merely
2509 inherited from secondary bases. */
2510 for (fnsp = &virtuals; *fnsp; )
2512 tree fn = TREE_VALUE (*fnsp);
2514 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2515 || DECL_VINDEX (fn) == error_mark_node)
2517 /* We don't need to adjust the `this' pointer when
2518 calling this function. */
2519 BV_DELTA (*fnsp) = integer_zero_node;
2520 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2522 /* This is a function not already in our vtable. Keep it. */
2523 fnsp = &TREE_CHAIN (*fnsp);
2525 else
2526 /* We've already got an entry for this function. Skip it. */
2527 *fnsp = TREE_CHAIN (*fnsp);
2530 return virtuals;
2533 /* Get the base virtual function declarations in T that have the
2534 indicated NAME. */
2536 static tree
2537 get_basefndecls (tree name, tree t)
2539 tree methods;
2540 tree base_fndecls = NULL_TREE;
2541 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2542 int i;
2544 /* Find virtual functions in T with the indicated NAME. */
2545 i = lookup_fnfields_1 (t, name);
2546 if (i != -1)
2547 for (methods = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), i);
2548 methods;
2549 methods = OVL_NEXT (methods))
2551 tree method = OVL_CURRENT (methods);
2553 if (TREE_CODE (method) == FUNCTION_DECL
2554 && DECL_VINDEX (method))
2555 base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
2558 if (base_fndecls)
2559 return base_fndecls;
2561 for (i = 0; i < n_baseclasses; i++)
2563 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2564 base_fndecls = chainon (get_basefndecls (name, basetype),
2565 base_fndecls);
2568 return base_fndecls;
2571 /* If this declaration supersedes the declaration of
2572 a method declared virtual in the base class, then
2573 mark this field as being virtual as well. */
2575 static void
2576 check_for_override (tree decl, tree ctype)
2578 if (TREE_CODE (decl) == TEMPLATE_DECL)
2579 /* In [temp.mem] we have:
2581 A specialization of a member function template does not
2582 override a virtual function from a base class. */
2583 return;
2584 if ((DECL_DESTRUCTOR_P (decl)
2585 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2586 && look_for_overrides (ctype, decl)
2587 && !DECL_STATIC_FUNCTION_P (decl))
2588 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2589 the error_mark_node so that we know it is an overriding
2590 function. */
2591 DECL_VINDEX (decl) = decl;
2593 if (DECL_VIRTUAL_P (decl))
2595 if (!DECL_VINDEX (decl))
2596 DECL_VINDEX (decl) = error_mark_node;
2597 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2601 /* Warn about hidden virtual functions that are not overridden in t.
2602 We know that constructors and destructors don't apply. */
2604 void
2605 warn_hidden (tree t)
2607 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2608 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2609 int i;
2611 /* We go through each separately named virtual function. */
2612 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2614 tree fns;
2615 tree name;
2616 tree fndecl;
2617 tree base_fndecls;
2618 int j;
2620 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2621 have the same name. Figure out what name that is. */
2622 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2623 /* There are no possibly hidden functions yet. */
2624 base_fndecls = NULL_TREE;
2625 /* Iterate through all of the base classes looking for possibly
2626 hidden functions. */
2627 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2629 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2630 base_fndecls = chainon (get_basefndecls (name, basetype),
2631 base_fndecls);
2634 /* If there are no functions to hide, continue. */
2635 if (!base_fndecls)
2636 continue;
2638 /* Remove any overridden functions. */
2639 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2641 fndecl = OVL_CURRENT (fns);
2642 if (DECL_VINDEX (fndecl))
2644 tree *prev = &base_fndecls;
2646 while (*prev)
2647 /* If the method from the base class has the same
2648 signature as the method from the derived class, it
2649 has been overridden. */
2650 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2651 *prev = TREE_CHAIN (*prev);
2652 else
2653 prev = &TREE_CHAIN (*prev);
2657 /* Now give a warning for all base functions without overriders,
2658 as they are hidden. */
2659 while (base_fndecls)
2661 /* Here we know it is a hider, and no overrider exists. */
2662 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2663 cp_warning_at (" by `%D'",
2664 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2665 base_fndecls = TREE_CHAIN (base_fndecls);
2670 /* Check for things that are invalid. There are probably plenty of other
2671 things we should check for also. */
2673 static void
2674 finish_struct_anon (tree t)
2676 tree field;
2678 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2680 if (TREE_STATIC (field))
2681 continue;
2682 if (TREE_CODE (field) != FIELD_DECL)
2683 continue;
2685 if (DECL_NAME (field) == NULL_TREE
2686 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2688 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2689 for (; elt; elt = TREE_CHAIN (elt))
2691 /* We're generally only interested in entities the user
2692 declared, but we also find nested classes by noticing
2693 the TYPE_DECL that we create implicitly. You're
2694 allowed to put one anonymous union inside another,
2695 though, so we explicitly tolerate that. We use
2696 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2697 we also allow unnamed types used for defining fields. */
2698 if (DECL_ARTIFICIAL (elt)
2699 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2700 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2701 continue;
2703 if (constructor_name_p (DECL_NAME (elt), t))
2704 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2705 elt);
2707 if (TREE_CODE (elt) != FIELD_DECL)
2709 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2710 elt);
2711 continue;
2714 if (TREE_PRIVATE (elt))
2715 cp_pedwarn_at ("private member `%#D' in anonymous union",
2716 elt);
2717 else if (TREE_PROTECTED (elt))
2718 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2719 elt);
2721 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2722 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2728 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2729 will be used later during class template instantiation.
2730 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2731 a non-static member data (FIELD_DECL), a member function
2732 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2733 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2734 When FRIEND_P is nonzero, T is either a friend class
2735 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2736 (FUNCTION_DECL, TEMPLATE_DECL). */
2738 void
2739 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2741 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2742 if (CLASSTYPE_TEMPLATE_INFO (type))
2743 CLASSTYPE_DECL_LIST (type)
2744 = tree_cons (friend_p ? NULL_TREE : type,
2745 t, CLASSTYPE_DECL_LIST (type));
2748 /* Create default constructors, assignment operators, and so forth for
2749 the type indicated by T, if they are needed.
2750 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2751 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2752 class cannot have a default constructor, copy constructor taking a
2753 const reference argument, or an assignment operator taking a const
2754 reference, respectively. If a virtual destructor is created, its
2755 DECL is returned; otherwise the return value is NULL_TREE. */
2757 static void
2758 add_implicitly_declared_members (tree t,
2759 int cant_have_default_ctor,
2760 int cant_have_const_cctor,
2761 int cant_have_const_assignment)
2763 tree default_fn;
2764 tree implicit_fns = NULL_TREE;
2765 tree virtual_dtor = NULL_TREE;
2766 tree *f;
2768 ++adding_implicit_members;
2770 /* Destructor. */
2771 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2773 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2774 check_for_override (default_fn, t);
2776 /* If we couldn't make it work, then pretend we didn't need it. */
2777 if (default_fn == void_type_node)
2778 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2779 else
2781 TREE_CHAIN (default_fn) = implicit_fns;
2782 implicit_fns = default_fn;
2784 if (DECL_VINDEX (default_fn))
2785 virtual_dtor = default_fn;
2788 else
2789 /* Any non-implicit destructor is non-trivial. */
2790 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2792 /* Default constructor. */
2793 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2795 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2796 TREE_CHAIN (default_fn) = implicit_fns;
2797 implicit_fns = default_fn;
2800 /* Copy constructor. */
2801 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2803 /* ARM 12.18: You get either X(X&) or X(const X&), but
2804 not both. --Chip */
2805 default_fn
2806 = implicitly_declare_fn (sfk_copy_constructor, t,
2807 /*const_p=*/!cant_have_const_cctor);
2808 TREE_CHAIN (default_fn) = implicit_fns;
2809 implicit_fns = default_fn;
2812 /* Assignment operator. */
2813 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2815 default_fn
2816 = implicitly_declare_fn (sfk_assignment_operator, t,
2817 /*const_p=*/!cant_have_const_assignment);
2818 TREE_CHAIN (default_fn) = implicit_fns;
2819 implicit_fns = default_fn;
2822 /* Now, hook all of the new functions on to TYPE_METHODS,
2823 and add them to the CLASSTYPE_METHOD_VEC. */
2824 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2826 add_method (t, *f, /*error_p=*/0);
2827 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2829 if (abi_version_at_least (2))
2830 /* G++ 3.2 put the implicit destructor at the *beginning* of the
2831 list, which cause the destructor to be emitted in an incorrect
2832 location in the vtable. */
2833 TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2834 else
2836 if (warn_abi && virtual_dtor)
2837 warning ("vtable layout for class `%T' may not be ABI-compliant "
2838 "and may change in a future version of GCC due to implicit "
2839 "virtual destructor",
2841 *f = TYPE_METHODS (t);
2842 TYPE_METHODS (t) = implicit_fns;
2845 --adding_implicit_members;
2848 /* Subroutine of finish_struct_1. Recursively count the number of fields
2849 in TYPE, including anonymous union members. */
2851 static int
2852 count_fields (tree fields)
2854 tree x;
2855 int n_fields = 0;
2856 for (x = fields; x; x = TREE_CHAIN (x))
2858 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2859 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2860 else
2861 n_fields += 1;
2863 return n_fields;
2866 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2867 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2869 static int
2870 add_fields_to_vec (tree fields, tree field_vec, int idx)
2872 tree x;
2873 for (x = fields; x; x = TREE_CHAIN (x))
2875 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2876 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2877 else
2878 TREE_VEC_ELT (field_vec, idx++) = x;
2880 return idx;
2883 /* FIELD is a bit-field. We are finishing the processing for its
2884 enclosing type. Issue any appropriate messages and set appropriate
2885 flags. */
2887 static void
2888 check_bitfield_decl (tree field)
2890 tree type = TREE_TYPE (field);
2891 tree w = NULL_TREE;
2893 /* Detect invalid bit-field type. */
2894 if (DECL_INITIAL (field)
2895 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2897 cp_error_at ("bit-field `%#D' with non-integral type", field);
2898 w = error_mark_node;
2901 /* Detect and ignore out of range field width. */
2902 if (DECL_INITIAL (field))
2904 w = DECL_INITIAL (field);
2906 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2907 STRIP_NOPS (w);
2909 /* detect invalid field size. */
2910 if (TREE_CODE (w) == CONST_DECL)
2911 w = DECL_INITIAL (w);
2912 else
2913 w = decl_constant_value (w);
2915 if (TREE_CODE (w) != INTEGER_CST)
2917 cp_error_at ("bit-field `%D' width not an integer constant",
2918 field);
2919 w = error_mark_node;
2921 else if (tree_int_cst_sgn (w) < 0)
2923 cp_error_at ("negative width in bit-field `%D'", field);
2924 w = error_mark_node;
2926 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2928 cp_error_at ("zero width for bit-field `%D'", field);
2929 w = error_mark_node;
2931 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2932 && TREE_CODE (type) != ENUMERAL_TYPE
2933 && TREE_CODE (type) != BOOLEAN_TYPE)
2934 cp_warning_at ("width of `%D' exceeds its type", field);
2935 else if (TREE_CODE (type) == ENUMERAL_TYPE
2936 && (0 > compare_tree_int (w,
2937 min_precision (TYPE_MIN_VALUE (type),
2938 TREE_UNSIGNED (type)))
2939 || 0 > compare_tree_int (w,
2940 min_precision
2941 (TYPE_MAX_VALUE (type),
2942 TREE_UNSIGNED (type)))))
2943 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
2944 field, type);
2947 /* Remove the bit-field width indicator so that the rest of the
2948 compiler does not treat that value as an initializer. */
2949 DECL_INITIAL (field) = NULL_TREE;
2951 if (w != error_mark_node)
2953 DECL_SIZE (field) = convert (bitsizetype, w);
2954 DECL_BIT_FIELD (field) = 1;
2956 if (integer_zerop (w)
2957 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
2959 #ifdef EMPTY_FIELD_BOUNDARY
2960 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2961 EMPTY_FIELD_BOUNDARY);
2962 #endif
2963 #ifdef PCC_BITFIELD_TYPE_MATTERS
2964 if (PCC_BITFIELD_TYPE_MATTERS)
2966 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2967 TYPE_ALIGN (type));
2968 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2970 #endif
2973 else
2975 /* Non-bit-fields are aligned for their type. */
2976 DECL_BIT_FIELD (field) = 0;
2977 CLEAR_DECL_C_BIT_FIELD (field);
2978 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
2979 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2983 /* FIELD is a non bit-field. We are finishing the processing for its
2984 enclosing type T. Issue any appropriate messages and set appropriate
2985 flags. */
2987 static void
2988 check_field_decl (tree field,
2989 tree t,
2990 int* cant_have_const_ctor,
2991 int* cant_have_default_ctor,
2992 int* no_const_asn_ref,
2993 int* any_default_members)
2995 tree type = strip_array_types (TREE_TYPE (field));
2997 /* An anonymous union cannot contain any fields which would change
2998 the settings of CANT_HAVE_CONST_CTOR and friends. */
2999 if (ANON_UNION_TYPE_P (type))
3001 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3002 structs. So, we recurse through their fields here. */
3003 else if (ANON_AGGR_TYPE_P (type))
3005 tree fields;
3007 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3008 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3009 check_field_decl (fields, t, cant_have_const_ctor,
3010 cant_have_default_ctor, no_const_asn_ref,
3011 any_default_members);
3013 /* Check members with class type for constructors, destructors,
3014 etc. */
3015 else if (CLASS_TYPE_P (type))
3017 /* Never let anything with uninheritable virtuals
3018 make it through without complaint. */
3019 abstract_virtuals_error (field, type);
3021 if (TREE_CODE (t) == UNION_TYPE)
3023 if (TYPE_NEEDS_CONSTRUCTING (type))
3024 cp_error_at ("member `%#D' with constructor not allowed in union",
3025 field);
3026 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3027 cp_error_at ("member `%#D' with destructor not allowed in union",
3028 field);
3029 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3030 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3031 field);
3033 else
3035 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3036 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3037 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3038 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3039 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3042 if (!TYPE_HAS_CONST_INIT_REF (type))
3043 *cant_have_const_ctor = 1;
3045 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3046 *no_const_asn_ref = 1;
3048 if (TYPE_HAS_CONSTRUCTOR (type)
3049 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3050 *cant_have_default_ctor = 1;
3052 if (DECL_INITIAL (field) != NULL_TREE)
3054 /* `build_class_init_list' does not recognize
3055 non-FIELD_DECLs. */
3056 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3057 cp_error_at ("multiple fields in union `%T' initialized");
3058 *any_default_members = 1;
3062 /* Check the data members (both static and non-static), class-scoped
3063 typedefs, etc., appearing in the declaration of T. Issue
3064 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3065 declaration order) of access declarations; each TREE_VALUE in this
3066 list is a USING_DECL.
3068 In addition, set the following flags:
3070 EMPTY_P
3071 The class is empty, i.e., contains no non-static data members.
3073 CANT_HAVE_DEFAULT_CTOR_P
3074 This class cannot have an implicitly generated default
3075 constructor.
3077 CANT_HAVE_CONST_CTOR_P
3078 This class cannot have an implicitly generated copy constructor
3079 taking a const reference.
3081 CANT_HAVE_CONST_ASN_REF
3082 This class cannot have an implicitly generated assignment
3083 operator taking a const reference.
3085 All of these flags should be initialized before calling this
3086 function.
3088 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3089 fields can be added by adding to this chain. */
3091 static void
3092 check_field_decls (tree t, tree *access_decls,
3093 int *cant_have_default_ctor_p,
3094 int *cant_have_const_ctor_p,
3095 int *no_const_asn_ref_p)
3097 tree *field;
3098 tree *next;
3099 int has_pointers;
3100 int any_default_members;
3102 /* First, delete any duplicate fields. */
3103 delete_duplicate_fields (TYPE_FIELDS (t));
3105 /* Assume there are no access declarations. */
3106 *access_decls = NULL_TREE;
3107 /* Assume this class has no pointer members. */
3108 has_pointers = 0;
3109 /* Assume none of the members of this class have default
3110 initializations. */
3111 any_default_members = 0;
3113 for (field = &TYPE_FIELDS (t); *field; field = next)
3115 tree x = *field;
3116 tree type = TREE_TYPE (x);
3118 next = &TREE_CHAIN (x);
3120 if (TREE_CODE (x) == FIELD_DECL)
3122 DECL_PACKED (x) |= TYPE_PACKED (t);
3124 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3125 /* We don't treat zero-width bitfields as making a class
3126 non-empty. */
3128 else
3130 tree element_type;
3132 /* The class is non-empty. */
3133 CLASSTYPE_EMPTY_P (t) = 0;
3134 /* The class is not even nearly empty. */
3135 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3136 /* If one of the data members contains an empty class,
3137 so does T. */
3138 element_type = strip_array_types (type);
3139 if (CLASS_TYPE_P (element_type)
3140 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3141 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3145 if (TREE_CODE (x) == USING_DECL)
3147 /* Prune the access declaration from the list of fields. */
3148 *field = TREE_CHAIN (x);
3150 /* Save the access declarations for our caller. */
3151 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3153 /* Since we've reset *FIELD there's no reason to skip to the
3154 next field. */
3155 next = field;
3156 continue;
3159 if (TREE_CODE (x) == TYPE_DECL
3160 || TREE_CODE (x) == TEMPLATE_DECL)
3161 continue;
3163 /* If we've gotten this far, it's a data member, possibly static,
3164 or an enumerator. */
3166 DECL_CONTEXT (x) = t;
3168 /* ``A local class cannot have static data members.'' ARM 9.4 */
3169 if (current_function_decl && TREE_STATIC (x))
3170 cp_error_at ("field `%D' in local class cannot be static", x);
3172 /* Perform error checking that did not get done in
3173 grokdeclarator. */
3174 if (TREE_CODE (type) == FUNCTION_TYPE)
3176 cp_error_at ("field `%D' invalidly declared function type",
3178 type = build_pointer_type (type);
3179 TREE_TYPE (x) = type;
3181 else if (TREE_CODE (type) == METHOD_TYPE)
3183 cp_error_at ("field `%D' invalidly declared method type", x);
3184 type = build_pointer_type (type);
3185 TREE_TYPE (x) = type;
3187 else if (TREE_CODE (type) == OFFSET_TYPE)
3189 cp_error_at ("field `%D' invalidly declared offset type", x);
3190 type = build_pointer_type (type);
3191 TREE_TYPE (x) = type;
3194 if (type == error_mark_node)
3195 continue;
3197 /* When this goes into scope, it will be a non-local reference. */
3198 DECL_NONLOCAL (x) = 1;
3200 if (TREE_CODE (x) == CONST_DECL)
3201 continue;
3203 if (TREE_CODE (x) == VAR_DECL)
3205 if (TREE_CODE (t) == UNION_TYPE)
3206 /* Unions cannot have static members. */
3207 cp_error_at ("field `%D' declared static in union", x);
3209 continue;
3212 /* Now it can only be a FIELD_DECL. */
3214 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3215 CLASSTYPE_NON_AGGREGATE (t) = 1;
3217 /* If this is of reference type, check if it needs an init.
3218 Also do a little ANSI jig if necessary. */
3219 if (TREE_CODE (type) == REFERENCE_TYPE)
3221 CLASSTYPE_NON_POD_P (t) = 1;
3222 if (DECL_INITIAL (x) == NULL_TREE)
3223 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3225 /* ARM $12.6.2: [A member initializer list] (or, for an
3226 aggregate, initialization by a brace-enclosed list) is the
3227 only way to initialize nonstatic const and reference
3228 members. */
3229 *cant_have_default_ctor_p = 1;
3230 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3232 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3233 && extra_warnings)
3234 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3237 type = strip_array_types (type);
3239 if (TREE_CODE (type) == POINTER_TYPE)
3240 has_pointers = 1;
3242 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3243 CLASSTYPE_HAS_MUTABLE (t) = 1;
3245 if (! pod_type_p (type))
3246 /* DR 148 now allows pointers to members (which are POD themselves),
3247 to be allowed in POD structs. */
3248 CLASSTYPE_NON_POD_P (t) = 1;
3250 if (! zero_init_p (type))
3251 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3253 /* If any field is const, the structure type is pseudo-const. */
3254 if (CP_TYPE_CONST_P (type))
3256 C_TYPE_FIELDS_READONLY (t) = 1;
3257 if (DECL_INITIAL (x) == NULL_TREE)
3258 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3260 /* ARM $12.6.2: [A member initializer list] (or, for an
3261 aggregate, initialization by a brace-enclosed list) is the
3262 only way to initialize nonstatic const and reference
3263 members. */
3264 *cant_have_default_ctor_p = 1;
3265 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3267 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3268 && extra_warnings)
3269 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3271 /* A field that is pseudo-const makes the structure likewise. */
3272 else if (CLASS_TYPE_P (type))
3274 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3275 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3276 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3277 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3280 /* Core issue 80: A nonstatic data member is required to have a
3281 different name from the class iff the class has a
3282 user-defined constructor. */
3283 if (constructor_name_p (x, t) && TYPE_HAS_CONSTRUCTOR (t))
3284 cp_pedwarn_at ("field `%#D' with same name as class", x);
3286 /* We set DECL_C_BIT_FIELD in grokbitfield.
3287 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3288 if (DECL_C_BIT_FIELD (x))
3289 check_bitfield_decl (x);
3290 else
3291 check_field_decl (x, t,
3292 cant_have_const_ctor_p,
3293 cant_have_default_ctor_p,
3294 no_const_asn_ref_p,
3295 &any_default_members);
3298 /* Effective C++ rule 11. */
3299 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3300 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3302 warning ("`%#T' has pointer data members", t);
3304 if (! TYPE_HAS_INIT_REF (t))
3306 warning (" but does not override `%T(const %T&)'", t, t);
3307 if (! TYPE_HAS_ASSIGN_REF (t))
3308 warning (" or `operator=(const %T&)'", t);
3310 else if (! TYPE_HAS_ASSIGN_REF (t))
3311 warning (" but does not override `operator=(const %T&)'", t);
3315 /* Check anonymous struct/anonymous union fields. */
3316 finish_struct_anon (t);
3318 /* We've built up the list of access declarations in reverse order.
3319 Fix that now. */
3320 *access_decls = nreverse (*access_decls);
3323 /* If TYPE is an empty class type, records its OFFSET in the table of
3324 OFFSETS. */
3326 static int
3327 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3329 splay_tree_node n;
3331 if (!is_empty_class (type))
3332 return 0;
3334 /* Record the location of this empty object in OFFSETS. */
3335 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3336 if (!n)
3337 n = splay_tree_insert (offsets,
3338 (splay_tree_key) offset,
3339 (splay_tree_value) NULL_TREE);
3340 n->value = ((splay_tree_value)
3341 tree_cons (NULL_TREE,
3342 type,
3343 (tree) n->value));
3345 return 0;
3348 /* Returns nonzero if TYPE is an empty class type and there is
3349 already an entry in OFFSETS for the same TYPE as the same OFFSET. */
3351 static int
3352 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3354 splay_tree_node n;
3355 tree t;
3357 if (!is_empty_class (type))
3358 return 0;
3360 /* Record the location of this empty object in OFFSETS. */
3361 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3362 if (!n)
3363 return 0;
3365 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3366 if (same_type_p (TREE_VALUE (t), type))
3367 return 1;
3369 return 0;
3372 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3373 F for every subobject, passing it the type, offset, and table of
3374 OFFSETS. If VBASES_P is one, then virtual non-primary bases should
3375 be traversed.
3377 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3378 than MAX_OFFSET will not be walked.
3380 If F returns a nonzero value, the traversal ceases, and that value
3381 is returned. Otherwise, returns zero. */
3383 static int
3384 walk_subobject_offsets (tree type,
3385 subobject_offset_fn f,
3386 tree offset,
3387 splay_tree offsets,
3388 tree max_offset,
3389 int vbases_p)
3391 int r = 0;
3392 tree type_binfo = NULL_TREE;
3394 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3395 stop. */
3396 if (max_offset && INT_CST_LT (max_offset, offset))
3397 return 0;
3399 if (!TYPE_P (type))
3401 if (abi_version_at_least (2))
3402 type_binfo = type;
3403 type = BINFO_TYPE (type);
3406 if (CLASS_TYPE_P (type))
3408 tree field;
3409 tree binfo;
3410 int i;
3412 /* Avoid recursing into objects that are not interesting. */
3413 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3414 return 0;
3416 /* Record the location of TYPE. */
3417 r = (*f) (type, offset, offsets);
3418 if (r)
3419 return r;
3421 /* Iterate through the direct base classes of TYPE. */
3422 if (!type_binfo)
3423 type_binfo = TYPE_BINFO (type);
3424 for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3426 tree binfo_offset;
3428 binfo = BINFO_BASETYPE (type_binfo, i);
3430 if (abi_version_at_least (2)
3431 && TREE_VIA_VIRTUAL (binfo))
3432 continue;
3434 if (!vbases_p
3435 && TREE_VIA_VIRTUAL (binfo)
3436 && !BINFO_PRIMARY_P (binfo))
3437 continue;
3439 if (!abi_version_at_least (2))
3440 binfo_offset = size_binop (PLUS_EXPR,
3441 offset,
3442 BINFO_OFFSET (binfo));
3443 else
3445 tree orig_binfo;
3446 /* We cannot rely on BINFO_OFFSET being set for the base
3447 class yet, but the offsets for direct non-virtual
3448 bases can be calculated by going back to the TYPE. */
3449 orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3450 binfo_offset = size_binop (PLUS_EXPR,
3451 offset,
3452 BINFO_OFFSET (orig_binfo));
3455 r = walk_subobject_offsets (binfo,
3457 binfo_offset,
3458 offsets,
3459 max_offset,
3460 (abi_version_at_least (2)
3461 ? /*vbases_p=*/0 : vbases_p));
3462 if (r)
3463 return r;
3466 if (abi_version_at_least (2))
3468 tree vbase;
3470 /* Iterate through the virtual base classes of TYPE. In G++
3471 3.2, we included virtual bases in the direct base class
3472 loop above, which results in incorrect results; the
3473 correct offsets for virtual bases are only known when
3474 working with the most derived type. */
3475 if (vbases_p)
3476 for (vbase = CLASSTYPE_VBASECLASSES (type);
3477 vbase;
3478 vbase = TREE_CHAIN (vbase))
3480 binfo = TREE_VALUE (vbase);
3481 r = walk_subobject_offsets (binfo,
3483 size_binop (PLUS_EXPR,
3484 offset,
3485 BINFO_OFFSET (binfo)),
3486 offsets,
3487 max_offset,
3488 /*vbases_p=*/0);
3489 if (r)
3490 return r;
3492 else
3494 /* We still have to walk the primary base, if it is
3495 virtual. (If it is non-virtual, then it was walked
3496 above.) */
3497 vbase = get_primary_binfo (type_binfo);
3498 if (vbase && TREE_VIA_VIRTUAL (vbase)
3499 && BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3501 r = (walk_subobject_offsets
3502 (vbase, f, offset,
3503 offsets, max_offset, /*vbases_p=*/0));
3504 if (r)
3505 return r;
3510 /* Iterate through the fields of TYPE. */
3511 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3512 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3514 tree field_offset;
3516 if (abi_version_at_least (2))
3517 field_offset = byte_position (field);
3518 else
3519 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3520 field_offset = DECL_FIELD_OFFSET (field);
3522 r = walk_subobject_offsets (TREE_TYPE (field),
3524 size_binop (PLUS_EXPR,
3525 offset,
3526 field_offset),
3527 offsets,
3528 max_offset,
3529 /*vbases_p=*/1);
3530 if (r)
3531 return r;
3534 else if (TREE_CODE (type) == ARRAY_TYPE)
3536 tree element_type = strip_array_types (type);
3537 tree domain = TYPE_DOMAIN (type);
3538 tree index;
3540 /* Avoid recursing into objects that are not interesting. */
3541 if (!CLASS_TYPE_P (element_type)
3542 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3543 return 0;
3545 /* Step through each of the elements in the array. */
3546 for (index = size_zero_node;
3547 /* G++ 3.2 had an off-by-one error here. */
3548 (abi_version_at_least (2)
3549 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3550 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3551 index = size_binop (PLUS_EXPR, index, size_one_node))
3553 r = walk_subobject_offsets (TREE_TYPE (type),
3555 offset,
3556 offsets,
3557 max_offset,
3558 /*vbases_p=*/1);
3559 if (r)
3560 return r;
3561 offset = size_binop (PLUS_EXPR, offset,
3562 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3563 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3564 there's no point in iterating through the remaining
3565 elements of the array. */
3566 if (max_offset && INT_CST_LT (max_offset, offset))
3567 break;
3571 return 0;
3574 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3575 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3576 examined. */
3578 static void
3579 record_subobject_offsets (tree type,
3580 tree offset,
3581 splay_tree offsets,
3582 int vbases_p)
3584 walk_subobject_offsets (type, record_subobject_offset, offset,
3585 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3588 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3589 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3590 virtual bases of TYPE are examined. */
3592 static int
3593 layout_conflict_p (tree type,
3594 tree offset,
3595 splay_tree offsets,
3596 int vbases_p)
3598 splay_tree_node max_node;
3600 /* Get the node in OFFSETS that indicates the maximum offset where
3601 an empty subobject is located. */
3602 max_node = splay_tree_max (offsets);
3603 /* If there aren't any empty subobjects, then there's no point in
3604 performing this check. */
3605 if (!max_node)
3606 return 0;
3608 return walk_subobject_offsets (type, check_subobject_offset, offset,
3609 offsets, (tree) (max_node->key),
3610 vbases_p);
3613 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3614 non-static data member of the type indicated by RLI. BINFO is the
3615 binfo corresponding to the base subobject, OFFSETS maps offsets to
3616 types already located at those offsets. This function determines
3617 the position of the DECL. */
3619 static void
3620 layout_nonempty_base_or_field (record_layout_info rli,
3621 tree decl,
3622 tree binfo,
3623 splay_tree offsets)
3625 tree offset = NULL_TREE;
3626 bool field_p;
3627 tree type;
3629 if (binfo)
3631 /* For the purposes of determining layout conflicts, we want to
3632 use the class type of BINFO; TREE_TYPE (DECL) will be the
3633 CLASSTYPE_AS_BASE version, which does not contain entries for
3634 zero-sized bases. */
3635 type = TREE_TYPE (binfo);
3636 field_p = false;
3638 else
3640 type = TREE_TYPE (decl);
3641 field_p = true;
3644 /* Try to place the field. It may take more than one try if we have
3645 a hard time placing the field without putting two objects of the
3646 same type at the same address. */
3647 while (1)
3649 struct record_layout_info_s old_rli = *rli;
3651 /* Place this field. */
3652 place_field (rli, decl);
3653 offset = byte_position (decl);
3655 /* We have to check to see whether or not there is already
3656 something of the same type at the offset we're about to use.
3657 For example:
3659 struct S {};
3660 struct T : public S { int i; };
3661 struct U : public S, public T {};
3663 Here, we put S at offset zero in U. Then, we can't put T at
3664 offset zero -- its S component would be at the same address
3665 as the S we already allocated. So, we have to skip ahead.
3666 Since all data members, including those whose type is an
3667 empty class, have nonzero size, any overlap can happen only
3668 with a direct or indirect base-class -- it can't happen with
3669 a data member. */
3670 /* G++ 3.2 did not check for overlaps when placing a non-empty
3671 virtual base. */
3672 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3673 break;
3674 if (layout_conflict_p (field_p ? type : binfo, offset,
3675 offsets, field_p))
3677 /* Strip off the size allocated to this field. That puts us
3678 at the first place we could have put the field with
3679 proper alignment. */
3680 *rli = old_rli;
3682 /* Bump up by the alignment required for the type. */
3683 rli->bitpos
3684 = size_binop (PLUS_EXPR, rli->bitpos,
3685 bitsize_int (binfo
3686 ? CLASSTYPE_ALIGN (type)
3687 : TYPE_ALIGN (type)));
3688 normalize_rli (rli);
3690 else
3691 /* There was no conflict. We're done laying out this field. */
3692 break;
3695 /* Now that we know where it will be placed, update its
3696 BINFO_OFFSET. */
3697 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3698 /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3699 this point because their BINFO_OFFSET is copied from another
3700 hierarchy. Therefore, we may not need to add the entire
3701 OFFSET. */
3702 propagate_binfo_offsets (binfo,
3703 size_diffop (convert (ssizetype, offset),
3704 convert (ssizetype,
3705 BINFO_OFFSET (binfo))));
3708 /* Returns true if TYPE is empty and OFFSET is nonzero. */
3710 static int
3711 empty_base_at_nonzero_offset_p (tree type,
3712 tree offset,
3713 splay_tree offsets ATTRIBUTE_UNUSED)
3715 return is_empty_class (type) && !integer_zerop (offset);
3718 /* Layout the empty base BINFO. EOC indicates the byte currently just
3719 past the end of the class, and should be correctly aligned for a
3720 class of the type indicated by BINFO; OFFSETS gives the offsets of
3721 the empty bases allocated so far. T is the most derived
3722 type. Return nonzero iff we added it at the end. */
3724 static bool
3725 layout_empty_base (tree binfo, tree eoc, splay_tree offsets)
3727 tree alignment;
3728 tree basetype = BINFO_TYPE (binfo);
3729 bool atend = false;
3731 /* This routine should only be used for empty classes. */
3732 my_friendly_assert (is_empty_class (basetype), 20000321);
3733 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3735 if (abi_version_at_least (2))
3736 BINFO_OFFSET (binfo) = size_zero_node;
3737 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3738 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3739 "change in a future version of GCC",
3740 BINFO_TYPE (binfo));
3742 /* This is an empty base class. We first try to put it at offset
3743 zero. */
3744 if (layout_conflict_p (binfo,
3745 BINFO_OFFSET (binfo),
3746 offsets,
3747 /*vbases_p=*/0))
3749 /* That didn't work. Now, we move forward from the next
3750 available spot in the class. */
3751 atend = true;
3752 propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
3753 while (1)
3755 if (!layout_conflict_p (binfo,
3756 BINFO_OFFSET (binfo),
3757 offsets,
3758 /*vbases_p=*/0))
3759 /* We finally found a spot where there's no overlap. */
3760 break;
3762 /* There's overlap here, too. Bump along to the next spot. */
3763 propagate_binfo_offsets (binfo, alignment);
3766 return atend;
3769 /* Layout the the base given by BINFO in the class indicated by RLI.
3770 *BASE_ALIGN is a running maximum of the alignments of
3771 any base class. OFFSETS gives the location of empty base
3772 subobjects. T is the most derived type. Return nonzero if the new
3773 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3774 *NEXT_FIELD, unless BINFO is for an empty base class.
3776 Returns the location at which the next field should be inserted. */
3778 static tree *
3779 build_base_field (record_layout_info rli, tree binfo,
3780 splay_tree offsets, tree *next_field)
3782 tree t = rli->t;
3783 tree basetype = BINFO_TYPE (binfo);
3785 if (!COMPLETE_TYPE_P (basetype))
3786 /* This error is now reported in xref_tag, thus giving better
3787 location information. */
3788 return next_field;
3790 /* Place the base class. */
3791 if (!is_empty_class (basetype))
3793 tree decl;
3795 /* The containing class is non-empty because it has a non-empty
3796 base class. */
3797 CLASSTYPE_EMPTY_P (t) = 0;
3799 /* Create the FIELD_DECL. */
3800 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3801 DECL_ARTIFICIAL (decl) = 1;
3802 DECL_FIELD_CONTEXT (decl) = t;
3803 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3804 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3805 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3806 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3807 DECL_IGNORED_P (decl) = 1;
3809 /* Try to place the field. It may take more than one try if we
3810 have a hard time placing the field without putting two
3811 objects of the same type at the same address. */
3812 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3813 /* Add the new FIELD_DECL to the list of fields for T. */
3814 TREE_CHAIN (decl) = *next_field;
3815 *next_field = decl;
3816 next_field = &TREE_CHAIN (decl);
3818 else
3820 tree eoc;
3821 bool atend;
3823 /* On some platforms (ARM), even empty classes will not be
3824 byte-aligned. */
3825 eoc = round_up (rli_size_unit_so_far (rli),
3826 CLASSTYPE_ALIGN_UNIT (basetype));
3827 atend = layout_empty_base (binfo, eoc, offsets);
3828 /* A nearly-empty class "has no proper base class that is empty,
3829 not morally virtual, and at an offset other than zero." */
3830 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3832 if (atend)
3833 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3834 /* The check above (used in G++ 3.2) is insufficient because
3835 an empty class placed at offset zero might itself have an
3836 empty base at a nonzero offset. */
3837 else if (walk_subobject_offsets (basetype,
3838 empty_base_at_nonzero_offset_p,
3839 size_zero_node,
3840 /*offsets=*/NULL,
3841 /*max_offset=*/NULL_TREE,
3842 /*vbases_p=*/true))
3844 if (abi_version_at_least (2))
3845 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3846 else if (warn_abi)
3847 warning ("class `%T' will be considered nearly empty in a "
3848 "future version of GCC", t);
3852 /* We do not create a FIELD_DECL for empty base classes because
3853 it might overlap some other field. We want to be able to
3854 create CONSTRUCTORs for the class by iterating over the
3855 FIELD_DECLs, and the back end does not handle overlapping
3856 FIELD_DECLs. */
3858 /* An empty virtual base causes a class to be non-empty
3859 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3860 here because that was already done when the virtual table
3861 pointer was created. */
3864 /* Record the offsets of BINFO and its base subobjects. */
3865 record_subobject_offsets (binfo,
3866 BINFO_OFFSET (binfo),
3867 offsets,
3868 /*vbases_p=*/0);
3870 return next_field;
3873 /* Layout all of the non-virtual base classes. Record empty
3874 subobjects in OFFSETS. T is the most derived type. Return nonzero
3875 if the type cannot be nearly empty. The fields created
3876 corresponding to the base classes will be inserted at
3877 *NEXT_FIELD. */
3879 static void
3880 build_base_fields (record_layout_info rli,
3881 splay_tree offsets, tree *next_field)
3883 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3884 subobjects. */
3885 tree t = rli->t;
3886 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
3887 int i;
3889 /* The primary base class is always allocated first. */
3890 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3891 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3892 offsets, next_field);
3894 /* Now allocate the rest of the bases. */
3895 for (i = 0; i < n_baseclasses; ++i)
3897 tree base_binfo;
3899 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
3901 /* The primary base was already allocated above, so we don't
3902 need to allocate it again here. */
3903 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3904 continue;
3906 /* Virtual bases are added at the end (a primary virtual base
3907 will have already been added). */
3908 if (TREE_VIA_VIRTUAL (base_binfo))
3909 continue;
3911 next_field = build_base_field (rli, base_binfo,
3912 offsets, next_field);
3916 /* Go through the TYPE_METHODS of T issuing any appropriate
3917 diagnostics, figuring out which methods override which other
3918 methods, and so forth. */
3920 static void
3921 check_methods (tree t)
3923 tree x;
3925 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3927 /* If this was an evil function, don't keep it in class. */
3928 if (DECL_ASSEMBLER_NAME_SET_P (x)
3929 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3930 continue;
3932 check_for_override (x, t);
3933 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3934 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3936 /* The name of the field is the original field name
3937 Save this in auxiliary field for later overloading. */
3938 if (DECL_VINDEX (x))
3940 TYPE_POLYMORPHIC_P (t) = 1;
3941 if (DECL_PURE_VIRTUAL_P (x))
3942 CLASSTYPE_PURE_VIRTUALS (t)
3943 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3948 /* FN is a constructor or destructor. Clone the declaration to create
3949 a specialized in-charge or not-in-charge version, as indicated by
3950 NAME. */
3952 static tree
3953 build_clone (tree fn, tree name)
3955 tree parms;
3956 tree clone;
3958 /* Copy the function. */
3959 clone = copy_decl (fn);
3960 /* Remember where this function came from. */
3961 DECL_CLONED_FUNCTION (clone) = fn;
3962 DECL_ABSTRACT_ORIGIN (clone) = fn;
3963 /* Reset the function name. */
3964 DECL_NAME (clone) = name;
3965 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3966 /* There's no pending inline data for this function. */
3967 DECL_PENDING_INLINE_INFO (clone) = NULL;
3968 DECL_PENDING_INLINE_P (clone) = 0;
3969 /* And it hasn't yet been deferred. */
3970 DECL_DEFERRED_FN (clone) = 0;
3972 /* The base-class destructor is not virtual. */
3973 if (name == base_dtor_identifier)
3975 DECL_VIRTUAL_P (clone) = 0;
3976 if (TREE_CODE (clone) != TEMPLATE_DECL)
3977 DECL_VINDEX (clone) = NULL_TREE;
3980 /* If there was an in-charge parameter, drop it from the function
3981 type. */
3982 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3984 tree basetype;
3985 tree parmtypes;
3986 tree exceptions;
3988 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3989 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3990 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3991 /* Skip the `this' parameter. */
3992 parmtypes = TREE_CHAIN (parmtypes);
3993 /* Skip the in-charge parameter. */
3994 parmtypes = TREE_CHAIN (parmtypes);
3995 /* And the VTT parm, in a complete [cd]tor. */
3996 if (DECL_HAS_VTT_PARM_P (fn)
3997 && ! DECL_NEEDS_VTT_PARM_P (clone))
3998 parmtypes = TREE_CHAIN (parmtypes);
3999 /* If this is subobject constructor or destructor, add the vtt
4000 parameter. */
4001 TREE_TYPE (clone)
4002 = build_cplus_method_type (basetype,
4003 TREE_TYPE (TREE_TYPE (clone)),
4004 parmtypes);
4005 if (exceptions)
4006 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4007 exceptions);
4010 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
4011 aren't function parameters; those are the template parameters. */
4012 if (TREE_CODE (clone) != TEMPLATE_DECL)
4014 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4015 /* Remove the in-charge parameter. */
4016 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4018 TREE_CHAIN (DECL_ARGUMENTS (clone))
4019 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4020 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4022 /* And the VTT parm, in a complete [cd]tor. */
4023 if (DECL_HAS_VTT_PARM_P (fn))
4025 if (DECL_NEEDS_VTT_PARM_P (clone))
4026 DECL_HAS_VTT_PARM_P (clone) = 1;
4027 else
4029 TREE_CHAIN (DECL_ARGUMENTS (clone))
4030 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4031 DECL_HAS_VTT_PARM_P (clone) = 0;
4035 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4037 DECL_CONTEXT (parms) = clone;
4038 cxx_dup_lang_specific_decl (parms);
4042 /* Create the RTL for this function. */
4043 SET_DECL_RTL (clone, NULL_RTX);
4044 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4046 /* Make it easy to find the CLONE given the FN. */
4047 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4048 TREE_CHAIN (fn) = clone;
4050 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4051 if (TREE_CODE (clone) == TEMPLATE_DECL)
4053 tree result;
4055 DECL_TEMPLATE_RESULT (clone)
4056 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4057 result = DECL_TEMPLATE_RESULT (clone);
4058 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4059 DECL_TI_TEMPLATE (result) = clone;
4061 else if (DECL_DEFERRED_FN (fn))
4062 defer_fn (clone);
4064 return clone;
4067 /* Produce declarations for all appropriate clones of FN. If
4068 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4069 CLASTYPE_METHOD_VEC as well. */
4071 void
4072 clone_function_decl (tree fn, int update_method_vec_p)
4074 tree clone;
4076 /* Avoid inappropriate cloning. */
4077 if (TREE_CHAIN (fn)
4078 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4079 return;
4081 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4083 /* For each constructor, we need two variants: an in-charge version
4084 and a not-in-charge version. */
4085 clone = build_clone (fn, complete_ctor_identifier);
4086 if (update_method_vec_p)
4087 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4088 clone = build_clone (fn, base_ctor_identifier);
4089 if (update_method_vec_p)
4090 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4092 else
4094 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4096 /* For each destructor, we need three variants: an in-charge
4097 version, a not-in-charge version, and an in-charge deleting
4098 version. We clone the deleting version first because that
4099 means it will go second on the TYPE_METHODS list -- and that
4100 corresponds to the correct layout order in the virtual
4101 function table.
4103 For a non-virtual destructor, we do not build a deleting
4104 destructor. */
4105 if (DECL_VIRTUAL_P (fn))
4107 clone = build_clone (fn, deleting_dtor_identifier);
4108 if (update_method_vec_p)
4109 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4111 clone = build_clone (fn, complete_dtor_identifier);
4112 if (update_method_vec_p)
4113 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4114 clone = build_clone (fn, base_dtor_identifier);
4115 if (update_method_vec_p)
4116 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4119 /* Note that this is an abstract function that is never emitted. */
4120 DECL_ABSTRACT (fn) = 1;
4123 /* DECL is an in charge constructor, which is being defined. This will
4124 have had an in class declaration, from whence clones were
4125 declared. An out-of-class definition can specify additional default
4126 arguments. As it is the clones that are involved in overload
4127 resolution, we must propagate the information from the DECL to its
4128 clones. */
4130 void
4131 adjust_clone_args (tree decl)
4133 tree clone;
4135 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4136 clone = TREE_CHAIN (clone))
4138 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4139 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4140 tree decl_parms, clone_parms;
4142 clone_parms = orig_clone_parms;
4144 /* Skip the 'this' parameter. */
4145 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4146 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4148 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4149 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4150 if (DECL_HAS_VTT_PARM_P (decl))
4151 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4153 clone_parms = orig_clone_parms;
4154 if (DECL_HAS_VTT_PARM_P (clone))
4155 clone_parms = TREE_CHAIN (clone_parms);
4157 for (decl_parms = orig_decl_parms; decl_parms;
4158 decl_parms = TREE_CHAIN (decl_parms),
4159 clone_parms = TREE_CHAIN (clone_parms))
4161 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4162 TREE_TYPE (clone_parms)), 20010424);
4164 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4166 /* A default parameter has been added. Adjust the
4167 clone's parameters. */
4168 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4169 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4170 tree type;
4172 clone_parms = orig_decl_parms;
4174 if (DECL_HAS_VTT_PARM_P (clone))
4176 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4177 TREE_VALUE (orig_clone_parms),
4178 clone_parms);
4179 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4181 type = build_cplus_method_type (basetype,
4182 TREE_TYPE (TREE_TYPE (clone)),
4183 clone_parms);
4184 if (exceptions)
4185 type = build_exception_variant (type, exceptions);
4186 TREE_TYPE (clone) = type;
4188 clone_parms = NULL_TREE;
4189 break;
4192 my_friendly_assert (!clone_parms, 20010424);
4196 /* For each of the constructors and destructors in T, create an
4197 in-charge and not-in-charge variant. */
4199 static void
4200 clone_constructors_and_destructors (tree t)
4202 tree fns;
4204 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4205 out now. */
4206 if (!CLASSTYPE_METHOD_VEC (t))
4207 return;
4209 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4210 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4211 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4212 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4215 /* Remove all zero-width bit-fields from T. */
4217 static void
4218 remove_zero_width_bit_fields (tree t)
4220 tree *fieldsp;
4222 fieldsp = &TYPE_FIELDS (t);
4223 while (*fieldsp)
4225 if (TREE_CODE (*fieldsp) == FIELD_DECL
4226 && DECL_C_BIT_FIELD (*fieldsp)
4227 && DECL_INITIAL (*fieldsp))
4228 *fieldsp = TREE_CHAIN (*fieldsp);
4229 else
4230 fieldsp = &TREE_CHAIN (*fieldsp);
4234 /* Returns TRUE iff we need a cookie when dynamically allocating an
4235 array whose elements have the indicated class TYPE. */
4237 static bool
4238 type_requires_array_cookie (tree type)
4240 tree fns;
4241 bool has_two_argument_delete_p = false;
4243 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4245 /* If there's a non-trivial destructor, we need a cookie. In order
4246 to iterate through the array calling the destructor for each
4247 element, we'll have to know how many elements there are. */
4248 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4249 return true;
4251 /* If the usual deallocation function is a two-argument whose second
4252 argument is of type `size_t', then we have to pass the size of
4253 the array to the deallocation function, so we will need to store
4254 a cookie. */
4255 fns = lookup_fnfields (TYPE_BINFO (type),
4256 ansi_opname (VEC_DELETE_EXPR),
4257 /*protect=*/0);
4258 /* If there are no `operator []' members, or the lookup is
4259 ambiguous, then we don't need a cookie. */
4260 if (!fns || fns == error_mark_node)
4261 return false;
4262 /* Loop through all of the functions. */
4263 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4265 tree fn;
4266 tree second_parm;
4268 /* Select the current function. */
4269 fn = OVL_CURRENT (fns);
4270 /* See if this function is a one-argument delete function. If
4271 it is, then it will be the usual deallocation function. */
4272 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4273 if (second_parm == void_list_node)
4274 return false;
4275 /* Otherwise, if we have a two-argument function and the second
4276 argument is `size_t', it will be the usual deallocation
4277 function -- unless there is one-argument function, too. */
4278 if (TREE_CHAIN (second_parm) == void_list_node
4279 && same_type_p (TREE_VALUE (second_parm), sizetype))
4280 has_two_argument_delete_p = true;
4283 return has_two_argument_delete_p;
4286 /* Check the validity of the bases and members declared in T. Add any
4287 implicitly-generated functions (like copy-constructors and
4288 assignment operators). Compute various flag bits (like
4289 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4290 level: i.e., independently of the ABI in use. */
4292 static void
4293 check_bases_and_members (tree t)
4295 /* Nonzero if we are not allowed to generate a default constructor
4296 for this case. */
4297 int cant_have_default_ctor;
4298 /* Nonzero if the implicitly generated copy constructor should take
4299 a non-const reference argument. */
4300 int cant_have_const_ctor;
4301 /* Nonzero if the the implicitly generated assignment operator
4302 should take a non-const reference argument. */
4303 int no_const_asn_ref;
4304 tree access_decls;
4306 /* By default, we use const reference arguments and generate default
4307 constructors. */
4308 cant_have_default_ctor = 0;
4309 cant_have_const_ctor = 0;
4310 no_const_asn_ref = 0;
4312 /* Check all the base-classes. */
4313 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4314 &no_const_asn_ref);
4316 /* Check all the data member declarations. */
4317 check_field_decls (t, &access_decls,
4318 &cant_have_default_ctor,
4319 &cant_have_const_ctor,
4320 &no_const_asn_ref);
4322 /* Check all the method declarations. */
4323 check_methods (t);
4325 /* A nearly-empty class has to be vptr-containing; a nearly empty
4326 class contains just a vptr. */
4327 if (!TYPE_CONTAINS_VPTR_P (t))
4328 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4330 /* Do some bookkeeping that will guide the generation of implicitly
4331 declared member functions. */
4332 TYPE_HAS_COMPLEX_INIT_REF (t)
4333 |= (TYPE_HAS_INIT_REF (t)
4334 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4335 || TYPE_POLYMORPHIC_P (t));
4336 TYPE_NEEDS_CONSTRUCTING (t)
4337 |= (TYPE_HAS_CONSTRUCTOR (t)
4338 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4339 || TYPE_POLYMORPHIC_P (t));
4340 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4341 || TYPE_POLYMORPHIC_P (t));
4342 CLASSTYPE_NON_POD_P (t)
4343 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4344 || TYPE_HAS_ASSIGN_REF (t));
4345 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4346 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4347 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4349 /* Synthesize any needed methods. Note that methods will be synthesized
4350 for anonymous unions; grok_x_components undoes that. */
4351 add_implicitly_declared_members (t, cant_have_default_ctor,
4352 cant_have_const_ctor,
4353 no_const_asn_ref);
4355 /* Create the in-charge and not-in-charge variants of constructors
4356 and destructors. */
4357 clone_constructors_and_destructors (t);
4359 /* Process the using-declarations. */
4360 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4361 handle_using_decl (TREE_VALUE (access_decls), t);
4363 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4364 finish_struct_methods (t);
4366 /* Figure out whether or not we will need a cookie when dynamically
4367 allocating an array of this type. */
4368 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4369 = type_requires_array_cookie (t);
4372 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4373 accordingly. If a new vfield was created (because T doesn't have a
4374 primary base class), then the newly created field is returned. It
4375 is not added to the TYPE_FIELDS list; it is the caller's
4376 responsibility to do that. Accumulate declared virtual functions
4377 on VIRTUALS_P. */
4379 static tree
4380 create_vtable_ptr (tree t, tree* virtuals_p)
4382 tree fn;
4384 /* Collect the virtual functions declared in T. */
4385 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4386 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4387 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4389 tree new_virtual = make_node (TREE_LIST);
4391 BV_FN (new_virtual) = fn;
4392 BV_DELTA (new_virtual) = integer_zero_node;
4394 TREE_CHAIN (new_virtual) = *virtuals_p;
4395 *virtuals_p = new_virtual;
4398 /* If we couldn't find an appropriate base class, create a new field
4399 here. Even if there weren't any new virtual functions, we might need a
4400 new virtual function table if we're supposed to include vptrs in
4401 all classes that need them. */
4402 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4404 /* We build this decl with vtbl_ptr_type_node, which is a
4405 `vtable_entry_type*'. It might seem more precise to use
4406 `vtable_entry_type (*)[N]' where N is the number of firtual
4407 functions. However, that would require the vtable pointer in
4408 base classes to have a different type than the vtable pointer
4409 in derived classes. We could make that happen, but that
4410 still wouldn't solve all the problems. In particular, the
4411 type-based alias analysis code would decide that assignments
4412 to the base class vtable pointer can't alias assignments to
4413 the derived class vtable pointer, since they have different
4414 types. Thus, in a derived class destructor, where the base
4415 class constructor was inlined, we could generate bad code for
4416 setting up the vtable pointer.
4418 Therefore, we use one type for all vtable pointers. We still
4419 use a type-correct type; it's just doesn't indicate the array
4420 bounds. That's better than using `void*' or some such; it's
4421 cleaner, and it let's the alias analysis code know that these
4422 stores cannot alias stores to void*! */
4423 tree field;
4425 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4426 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4427 DECL_VIRTUAL_P (field) = 1;
4428 DECL_ARTIFICIAL (field) = 1;
4429 DECL_FIELD_CONTEXT (field) = t;
4430 DECL_FCONTEXT (field) = t;
4431 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4432 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4434 TYPE_VFIELD (t) = field;
4436 /* This class is non-empty. */
4437 CLASSTYPE_EMPTY_P (t) = 0;
4439 if (CLASSTYPE_N_BASECLASSES (t))
4440 /* If there were any baseclasses, they can't possibly be at
4441 offset zero any more, because that's where the vtable
4442 pointer is. So, converting to a base class is going to
4443 take work. */
4444 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4446 return field;
4449 return NULL_TREE;
4452 /* Fixup the inline function given by INFO now that the class is
4453 complete. */
4455 static void
4456 fixup_pending_inline (tree fn)
4458 if (DECL_PENDING_INLINE_INFO (fn))
4460 tree args = DECL_ARGUMENTS (fn);
4461 while (args)
4463 DECL_CONTEXT (args) = fn;
4464 args = TREE_CHAIN (args);
4469 /* Fixup the inline methods and friends in TYPE now that TYPE is
4470 complete. */
4472 static void
4473 fixup_inline_methods (tree type)
4475 tree method = TYPE_METHODS (type);
4477 if (method && TREE_CODE (method) == TREE_VEC)
4479 if (TREE_VEC_ELT (method, 1))
4480 method = TREE_VEC_ELT (method, 1);
4481 else if (TREE_VEC_ELT (method, 0))
4482 method = TREE_VEC_ELT (method, 0);
4483 else
4484 method = TREE_VEC_ELT (method, 2);
4487 /* Do inline member functions. */
4488 for (; method; method = TREE_CHAIN (method))
4489 fixup_pending_inline (method);
4491 /* Do friends. */
4492 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4493 method;
4494 method = TREE_CHAIN (method))
4495 fixup_pending_inline (TREE_VALUE (method));
4496 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4499 /* Add OFFSET to all base types of BINFO which is a base in the
4500 hierarchy dominated by T.
4502 OFFSET, which is a type offset, is number of bytes. */
4504 static void
4505 propagate_binfo_offsets (tree binfo, tree offset)
4507 int i;
4508 tree primary_binfo;
4510 /* Update BINFO's offset. */
4511 BINFO_OFFSET (binfo)
4512 = convert (sizetype,
4513 size_binop (PLUS_EXPR,
4514 convert (ssizetype, BINFO_OFFSET (binfo)),
4515 offset));
4517 /* Find the primary base class. */
4518 primary_binfo = get_primary_binfo (binfo);
4520 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4521 downwards. */
4522 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4524 tree base_binfo;
4526 /* On the first time through the loop, do the primary base.
4527 Because the primary base need not be an immediate base, we
4528 must handle the primary base specially. */
4529 if (i == -1)
4531 if (!primary_binfo)
4532 continue;
4534 base_binfo = primary_binfo;
4536 else
4538 base_binfo = BINFO_BASETYPE (binfo, i);
4539 /* Don't do the primary base twice. */
4540 if (base_binfo == primary_binfo)
4541 continue;
4544 /* Skip virtual bases that aren't our canonical primary base. */
4545 if (TREE_VIA_VIRTUAL (base_binfo)
4546 && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
4547 continue;
4549 propagate_binfo_offsets (base_binfo, offset);
4553 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4554 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4555 empty subobjects of T. */
4557 static void
4558 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4560 tree vbase;
4561 tree t = rli->t;
4562 bool first_vbase = true;
4563 tree *next_field;
4565 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4566 return;
4568 if (!abi_version_at_least(2))
4570 /* In G++ 3.2, we incorrectly rounded the size before laying out
4571 the virtual bases. */
4572 finish_record_layout (rli, /*free_p=*/false);
4573 #ifdef STRUCTURE_SIZE_BOUNDARY
4574 /* Packed structures don't need to have minimum size. */
4575 if (! TYPE_PACKED (t))
4576 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4577 #endif
4578 rli->offset = TYPE_SIZE_UNIT (t);
4579 rli->bitpos = bitsize_zero_node;
4580 rli->record_align = TYPE_ALIGN (t);
4583 /* Find the last field. The artificial fields created for virtual
4584 bases will go after the last extant field to date. */
4585 next_field = &TYPE_FIELDS (t);
4586 while (*next_field)
4587 next_field = &TREE_CHAIN (*next_field);
4589 /* Go through the virtual bases, allocating space for each virtual
4590 base that is not already a primary base class. These are
4591 allocated in inheritance graph order. */
4592 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
4594 if (!TREE_VIA_VIRTUAL (vbase))
4595 continue;
4597 if (!BINFO_PRIMARY_P (vbase))
4599 tree basetype = TREE_TYPE (vbase);
4601 /* This virtual base is not a primary base of any class in the
4602 hierarchy, so we have to add space for it. */
4603 next_field = build_base_field (rli, vbase,
4604 offsets, next_field);
4606 /* If the first virtual base might have been placed at a
4607 lower address, had we started from CLASSTYPE_SIZE, rather
4608 than TYPE_SIZE, issue a warning. There can be both false
4609 positives and false negatives from this warning in rare
4610 cases; to deal with all the possibilities would probably
4611 require performing both layout algorithms and comparing
4612 the results which is not particularly tractable. */
4613 if (warn_abi
4614 && first_vbase
4615 && (tree_int_cst_lt
4616 (size_binop (CEIL_DIV_EXPR,
4617 round_up (CLASSTYPE_SIZE (t),
4618 CLASSTYPE_ALIGN (basetype)),
4619 bitsize_unit_node),
4620 BINFO_OFFSET (vbase))))
4621 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4622 basetype);
4624 first_vbase = false;
4629 /* Returns the offset of the byte just past the end of the base class
4630 BINFO. */
4632 static tree
4633 end_of_base (tree binfo)
4635 tree size;
4637 if (is_empty_class (BINFO_TYPE (binfo)))
4638 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4639 allocate some space for it. It cannot have virtual bases, so
4640 TYPE_SIZE_UNIT is fine. */
4641 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4642 else
4643 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4645 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4648 /* Returns the offset of the byte just past the end of the base class
4649 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4650 only non-virtual bases are included. */
4652 static tree
4653 end_of_class (tree t, int include_virtuals_p)
4655 tree result = size_zero_node;
4656 tree binfo;
4657 tree offset;
4658 int i;
4660 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4662 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4664 if (!include_virtuals_p
4665 && TREE_VIA_VIRTUAL (binfo)
4666 && BINFO_PRIMARY_BASE_OF (binfo) != TYPE_BINFO (t))
4667 continue;
4669 offset = end_of_base (binfo);
4670 if (INT_CST_LT_UNSIGNED (result, offset))
4671 result = offset;
4674 /* G++ 3.2 did not check indirect virtual bases. */
4675 if (abi_version_at_least (2) && include_virtuals_p)
4676 for (binfo = CLASSTYPE_VBASECLASSES (t);
4677 binfo;
4678 binfo = TREE_CHAIN (binfo))
4680 offset = end_of_base (TREE_VALUE (binfo));
4681 if (INT_CST_LT_UNSIGNED (result, offset))
4682 result = offset;
4685 return result;
4688 /* Warn about bases of T that are inaccessible because they are
4689 ambiguous. For example:
4691 struct S {};
4692 struct T : public S {};
4693 struct U : public S, public T {};
4695 Here, `(S*) new U' is not allowed because there are two `S'
4696 subobjects of U. */
4698 static void
4699 warn_about_ambiguous_bases (tree t)
4701 int i;
4702 tree vbases;
4703 tree basetype;
4705 /* Check direct bases. */
4706 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4708 basetype = TYPE_BINFO_BASETYPE (t, i);
4710 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4711 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4712 basetype, t);
4715 /* Check for ambiguous virtual bases. */
4716 if (extra_warnings)
4717 for (vbases = CLASSTYPE_VBASECLASSES (t);
4718 vbases;
4719 vbases = TREE_CHAIN (vbases))
4721 basetype = BINFO_TYPE (TREE_VALUE (vbases));
4723 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4724 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4725 basetype, t);
4729 /* Compare two INTEGER_CSTs K1 and K2. */
4731 static int
4732 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4734 return tree_int_cst_compare ((tree) k1, (tree) k2);
4737 /* Increase the size indicated in RLI to account for empty classes
4738 that are "off the end" of the class. */
4740 static void
4741 include_empty_classes (record_layout_info rli)
4743 tree eoc;
4744 tree rli_size;
4746 /* It might be the case that we grew the class to allocate a
4747 zero-sized base class. That won't be reflected in RLI, yet,
4748 because we are willing to overlay multiple bases at the same
4749 offset. However, now we need to make sure that RLI is big enough
4750 to reflect the entire class. */
4751 eoc = end_of_class (rli->t,
4752 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4753 rli_size = rli_size_unit_so_far (rli);
4754 if (TREE_CODE (rli_size) == INTEGER_CST
4755 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4757 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4758 rli->bitpos
4759 = size_binop (PLUS_EXPR,
4760 rli->bitpos,
4761 size_binop (MULT_EXPR,
4762 convert (bitsizetype,
4763 size_binop (MINUS_EXPR,
4764 eoc, rli_size)),
4765 bitsize_int (BITS_PER_UNIT)));
4766 normalize_rli (rli);
4770 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4771 BINFO_OFFSETs for all of the base-classes. Position the vtable
4772 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4774 static void
4775 layout_class_type (tree t, tree *virtuals_p)
4777 tree non_static_data_members;
4778 tree field;
4779 tree vptr;
4780 record_layout_info rli;
4781 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4782 types that appear at that offset. */
4783 splay_tree empty_base_offsets;
4784 /* True if the last field layed out was a bit-field. */
4785 bool last_field_was_bitfield = false;
4786 /* The location at which the next field should be inserted. */
4787 tree *next_field;
4788 /* T, as a base class. */
4789 tree base_t;
4791 /* Keep track of the first non-static data member. */
4792 non_static_data_members = TYPE_FIELDS (t);
4794 /* Start laying out the record. */
4795 rli = start_record_layout (t);
4797 /* If possible, we reuse the virtual function table pointer from one
4798 of our base classes. */
4799 determine_primary_base (t);
4801 /* Create a pointer to our virtual function table. */
4802 vptr = create_vtable_ptr (t, virtuals_p);
4804 /* The vptr is always the first thing in the class. */
4805 if (vptr)
4807 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4808 TYPE_FIELDS (t) = vptr;
4809 next_field = &TREE_CHAIN (vptr);
4810 place_field (rli, vptr);
4812 else
4813 next_field = &TYPE_FIELDS (t);
4815 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4816 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4817 NULL, NULL);
4818 build_base_fields (rli, empty_base_offsets, next_field);
4820 /* Layout the non-static data members. */
4821 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4823 tree type;
4824 tree padding;
4825 bool was_unnamed_p = false;
4827 /* We still pass things that aren't non-static data members to
4828 the back-end, in case it wants to do something with them. */
4829 if (TREE_CODE (field) != FIELD_DECL)
4831 place_field (rli, field);
4832 /* If the static data member has incomplete type, keep track
4833 of it so that it can be completed later. (The handling
4834 of pending statics in finish_record_layout is
4835 insufficient; consider:
4837 struct S1;
4838 struct S2 { static S1 s1; };
4840 At this point, finish_record_layout will be called, but
4841 S1 is still incomplete.) */
4842 if (TREE_CODE (field) == VAR_DECL)
4843 maybe_register_incomplete_var (field);
4844 continue;
4847 type = TREE_TYPE (field);
4849 /* If this field is a bit-field whose width is greater than its
4850 type, then there are some special rules for allocating
4851 it. */
4852 if (DECL_C_BIT_FIELD (field)
4853 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4855 integer_type_kind itk;
4856 tree integer_type;
4857 /* We must allocate the bits as if suitably aligned for the
4858 longest integer type that fits in this many bits. type
4859 of the field. Then, we are supposed to use the left over
4860 bits as additional padding. */
4861 for (itk = itk_char; itk != itk_none; ++itk)
4862 if (INT_CST_LT (DECL_SIZE (field),
4863 TYPE_SIZE (integer_types[itk])))
4864 break;
4866 /* ITK now indicates a type that is too large for the
4867 field. We have to back up by one to find the largest
4868 type that fits. */
4869 integer_type = integer_types[itk - 1];
4871 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4872 /* In a union, the padding field must have the full width
4873 of the bit-field; all fields start at offset zero. */
4874 padding = DECL_SIZE (field);
4875 else
4877 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4878 warning ("size assigned to `%T' may not be "
4879 "ABI-compliant and may change in a future "
4880 "version of GCC",
4882 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4883 TYPE_SIZE (integer_type));
4885 #ifdef PCC_BITFIELD_TYPE_MATTERS
4886 /* An unnamed bitfield does not normally affect the
4887 alignment of the containing class on a target where
4888 PCC_BITFIELD_TYPE_MATTERS. But, the C++ ABI does not
4889 make any exceptions for unnamed bitfields when the
4890 bitfields are longer than their types. Therefore, we
4891 temporarily give the field a name. */
4892 if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
4894 was_unnamed_p = true;
4895 DECL_NAME (field) = make_anon_name ();
4897 #endif
4898 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4899 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4900 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4902 else
4903 padding = NULL_TREE;
4905 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4906 empty_base_offsets);
4907 /* If the bit-field had no name originally, remove the name
4908 now. */
4909 if (was_unnamed_p)
4910 DECL_NAME (field) = NULL_TREE;
4912 /* Remember the location of any empty classes in FIELD. */
4913 if (abi_version_at_least (2))
4914 record_subobject_offsets (TREE_TYPE (field),
4915 byte_position(field),
4916 empty_base_offsets,
4917 /*vbases_p=*/1);
4919 /* If a bit-field does not immediately follow another bit-field,
4920 and yet it starts in the middle of a byte, we have failed to
4921 comply with the ABI. */
4922 if (warn_abi
4923 && DECL_C_BIT_FIELD (field)
4924 && !last_field_was_bitfield
4925 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4926 DECL_FIELD_BIT_OFFSET (field),
4927 bitsize_unit_node)))
4928 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4929 field);
4931 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4932 offset of the field. */
4933 if (warn_abi
4934 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4935 byte_position (field))
4936 && contains_empty_class_p (TREE_TYPE (field)))
4937 cp_warning_at ("`%D' contains empty classes which may cause base "
4938 "classes to be placed at different locations in a "
4939 "future version of GCC",
4940 field);
4942 /* If we needed additional padding after this field, add it
4943 now. */
4944 if (padding)
4946 tree padding_field;
4948 padding_field = build_decl (FIELD_DECL,
4949 NULL_TREE,
4950 char_type_node);
4951 DECL_BIT_FIELD (padding_field) = 1;
4952 DECL_SIZE (padding_field) = padding;
4953 DECL_ALIGN (padding_field) = 1;
4954 DECL_USER_ALIGN (padding_field) = 0;
4955 layout_nonempty_base_or_field (rli, padding_field,
4956 NULL_TREE,
4957 empty_base_offsets);
4960 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4963 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4965 /* Make sure that we are on a byte boundary so that the size of
4966 the class without virtual bases will always be a round number
4967 of bytes. */
4968 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4969 normalize_rli (rli);
4972 /* G++ 3.2 does not allow virtual bases to be overlaid with tail
4973 padding. */
4974 if (!abi_version_at_least (2))
4975 include_empty_classes(rli);
4977 /* Delete all zero-width bit-fields from the list of fields. Now
4978 that the type is laid out they are no longer important. */
4979 remove_zero_width_bit_fields (t);
4981 /* Create the version of T used for virtual bases. We do not use
4982 make_aggr_type for this version; this is an artificial type. For
4983 a POD type, we just reuse T. */
4984 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4986 base_t = make_node (TREE_CODE (t));
4988 /* Set the size and alignment for the new type. In G++ 3.2, all
4989 empty classes were considered to have size zero when used as
4990 base classes. */
4991 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4993 TYPE_SIZE (base_t) = bitsize_zero_node;
4994 TYPE_SIZE_UNIT (base_t) = size_zero_node;
4995 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
4996 warning ("layout of classes derived from empty class `%T' "
4997 "may change in a future version of GCC",
5000 else
5002 tree eoc;
5004 /* If the ABI version is not at least two, and the last
5005 field was a bit-field, RLI may not be on a byte
5006 boundary. In particular, rli_size_unit_so_far might
5007 indicate the last complete byte, while rli_size_so_far
5008 indicates the total number of bits used. Therefore,
5009 rli_size_so_far, rather than rli_size_unit_so_far, is
5010 used to compute TYPE_SIZE_UNIT. */
5011 eoc = end_of_class (t, /*include_virtuals_p=*/0);
5012 TYPE_SIZE_UNIT (base_t)
5013 = size_binop (MAX_EXPR,
5014 convert (sizetype,
5015 size_binop (CEIL_DIV_EXPR,
5016 rli_size_so_far (rli),
5017 bitsize_int (BITS_PER_UNIT))),
5018 eoc);
5019 TYPE_SIZE (base_t)
5020 = size_binop (MAX_EXPR,
5021 rli_size_so_far (rli),
5022 size_binop (MULT_EXPR,
5023 convert (bitsizetype, eoc),
5024 bitsize_int (BITS_PER_UNIT)));
5026 TYPE_ALIGN (base_t) = rli->record_align;
5027 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5029 /* Copy the fields from T. */
5030 next_field = &TYPE_FIELDS (base_t);
5031 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5032 if (TREE_CODE (field) == FIELD_DECL)
5034 *next_field = build_decl (FIELD_DECL,
5035 DECL_NAME (field),
5036 TREE_TYPE (field));
5037 DECL_CONTEXT (*next_field) = base_t;
5038 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5039 DECL_FIELD_BIT_OFFSET (*next_field)
5040 = DECL_FIELD_BIT_OFFSET (field);
5041 next_field = &TREE_CHAIN (*next_field);
5044 /* Record the base version of the type. */
5045 CLASSTYPE_AS_BASE (t) = base_t;
5046 TYPE_CONTEXT (base_t) = t;
5048 else
5049 CLASSTYPE_AS_BASE (t) = t;
5051 /* Every empty class contains an empty class. */
5052 if (CLASSTYPE_EMPTY_P (t))
5053 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5055 /* Set the TYPE_DECL for this type to contain the right
5056 value for DECL_OFFSET, so that we can use it as part
5057 of a COMPONENT_REF for multiple inheritance. */
5058 layout_decl (TYPE_MAIN_DECL (t), 0);
5060 /* Now fix up any virtual base class types that we left lying
5061 around. We must get these done before we try to lay out the
5062 virtual function table. As a side-effect, this will remove the
5063 base subobject fields. */
5064 layout_virtual_bases (rli, empty_base_offsets);
5066 /* Make sure that empty classes are reflected in RLI at this
5067 point. */
5068 include_empty_classes(rli);
5070 /* Make sure not to create any structures with zero size. */
5071 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5072 place_field (rli,
5073 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
5075 /* Let the back-end lay out the type. */
5076 finish_record_layout (rli, /*free_p=*/true);
5078 /* Warn about bases that can't be talked about due to ambiguity. */
5079 warn_about_ambiguous_bases (t);
5081 /* Clean up. */
5082 splay_tree_delete (empty_base_offsets);
5085 /* Returns the virtual function with which the vtable for TYPE is
5086 emitted, or NULL_TREE if that heuristic is not applicable to TYPE. */
5088 static tree
5089 key_method (tree type)
5091 tree method;
5093 if (TYPE_FOR_JAVA (type)
5094 || processing_template_decl
5095 || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5096 || CLASSTYPE_INTERFACE_KNOWN (type))
5097 return NULL_TREE;
5099 for (method = TYPE_METHODS (type); method != NULL_TREE;
5100 method = TREE_CHAIN (method))
5101 if (DECL_VINDEX (method) != NULL_TREE
5102 && ! DECL_DECLARED_INLINE_P (method)
5103 && ! DECL_PURE_VIRTUAL_P (method))
5104 return method;
5106 return NULL_TREE;
5109 /* Perform processing required when the definition of T (a class type)
5110 is complete. */
5112 void
5113 finish_struct_1 (tree t)
5115 tree x;
5116 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5117 tree virtuals = NULL_TREE;
5118 int n_fields = 0;
5119 tree vfield;
5121 if (COMPLETE_TYPE_P (t))
5123 if (IS_AGGR_TYPE (t))
5124 error ("redefinition of `%#T'", t);
5125 else
5126 abort ();
5127 popclass ();
5128 return;
5131 /* If this type was previously laid out as a forward reference,
5132 make sure we lay it out again. */
5133 TYPE_SIZE (t) = NULL_TREE;
5134 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5135 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5137 fixup_inline_methods (t);
5139 /* Make assumptions about the class; we'll reset the flags if
5140 necessary. */
5141 CLASSTYPE_EMPTY_P (t) = 1;
5142 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5143 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5145 /* Do end-of-class semantic processing: checking the validity of the
5146 bases and members and add implicitly generated methods. */
5147 check_bases_and_members (t);
5149 /* Find the key method */
5150 if (TYPE_CONTAINS_VPTR_P (t))
5152 CLASSTYPE_KEY_METHOD (t) = key_method (t);
5154 /* If a polymorphic class has no key method, we may emit the vtable
5155 in every translation unit where the class definition appears. */
5156 if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5157 keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5160 /* Layout the class itself. */
5161 layout_class_type (t, &virtuals);
5163 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5164 vfield = TYPE_VFIELD (t);
5165 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5167 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5169 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5170 BINFO_TYPE (primary)),
5171 20010726);
5172 /* The vtable better be at the start. */
5173 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5174 20010726);
5175 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5176 20010726);
5178 vfield = copy_decl (vfield);
5179 DECL_FIELD_CONTEXT (vfield) = t;
5180 TYPE_VFIELD (t) = vfield;
5182 else
5183 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5185 virtuals = modify_all_vtables (t, nreverse (virtuals));
5187 /* If we created a new vtbl pointer for this class, add it to the
5188 list. */
5189 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5190 CLASSTYPE_VFIELDS (t)
5191 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5193 /* If necessary, create the primary vtable for this class. */
5194 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5196 /* We must enter these virtuals into the table. */
5197 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5198 build_primary_vtable (NULL_TREE, t);
5199 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
5200 /* Here we know enough to change the type of our virtual
5201 function table, but we will wait until later this function. */
5202 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5205 if (TYPE_CONTAINS_VPTR_P (t))
5207 int vindex;
5208 tree fn;
5210 if (TYPE_BINFO_VTABLE (t))
5211 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5212 20000116);
5213 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5214 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5215 20000116);
5217 /* Add entries for virtual functions introduced by this class. */
5218 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5220 /* Set DECL_VINDEX for all functions declared in this class. */
5221 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5222 fn;
5223 fn = TREE_CHAIN (fn),
5224 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5225 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5227 tree fndecl = BV_FN (fn);
5229 if (DECL_THUNK_P (fndecl))
5230 /* A thunk. We should never be calling this entry directly
5231 from this vtable -- we'd use the entry for the non
5232 thunk base function. */
5233 DECL_VINDEX (fndecl) = NULL_TREE;
5234 else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5235 DECL_VINDEX (fndecl) = build_shared_int_cst (vindex);
5239 finish_struct_bits (t);
5241 /* Complete the rtl for any static member objects of the type we're
5242 working on. */
5243 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5244 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5245 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5246 DECL_MODE (x) = TYPE_MODE (t);
5248 /* Done with FIELDS...now decide whether to sort these for
5249 faster lookups later.
5251 We use a small number because most searches fail (succeeding
5252 ultimately as the search bores through the inheritance
5253 hierarchy), and we want this failure to occur quickly. */
5255 n_fields = count_fields (TYPE_FIELDS (t));
5256 if (n_fields > 7)
5258 tree field_vec = make_tree_vec (n_fields);
5259 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5260 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5261 field_decl_cmp);
5262 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5263 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5264 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5267 if (TYPE_HAS_CONSTRUCTOR (t))
5269 tree vfields = CLASSTYPE_VFIELDS (t);
5271 for (vfields = CLASSTYPE_VFIELDS (t);
5272 vfields; vfields = TREE_CHAIN (vfields))
5273 /* Mark the fact that constructor for T could affect anybody
5274 inheriting from T who wants to initialize vtables for
5275 VFIELDS's type. */
5276 if (VF_BINFO_VALUE (vfields))
5277 TREE_ADDRESSABLE (vfields) = 1;
5280 /* Make the rtl for any new vtables we have created, and unmark
5281 the base types we marked. */
5282 finish_vtbls (t);
5284 /* Build the VTT for T. */
5285 build_vtt (t);
5287 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5288 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5289 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5291 complete_vars (t);
5293 if (warn_overloaded_virtual)
5294 warn_hidden (t);
5296 maybe_suppress_debug_info (t);
5298 dump_class_hierarchy (t);
5300 /* Finish debugging output for this type. */
5301 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5304 /* When T was built up, the member declarations were added in reverse
5305 order. Rearrange them to declaration order. */
5307 void
5308 unreverse_member_declarations (tree t)
5310 tree next;
5311 tree prev;
5312 tree x;
5314 /* The following lists are all in reverse order. Put them in
5315 declaration order now. */
5316 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5317 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5318 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5320 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5321 reverse order, so we can't just use nreverse. */
5322 prev = NULL_TREE;
5323 for (x = TYPE_FIELDS (t);
5324 x && TREE_CODE (x) != TYPE_DECL;
5325 x = next)
5327 next = TREE_CHAIN (x);
5328 TREE_CHAIN (x) = prev;
5329 prev = x;
5331 if (prev)
5333 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5334 if (prev)
5335 TYPE_FIELDS (t) = prev;
5339 tree
5340 finish_struct (tree t, tree attributes)
5342 const char *saved_filename = input_filename;
5343 int saved_lineno = lineno;
5345 /* Now that we've got all the field declarations, reverse everything
5346 as necessary. */
5347 unreverse_member_declarations (t);
5349 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5351 /* Nadger the current location so that diagnostics point to the start of
5352 the struct, not the end. */
5353 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5354 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5356 if (processing_template_decl)
5358 finish_struct_methods (t);
5359 TYPE_SIZE (t) = bitsize_zero_node;
5361 else
5362 finish_struct_1 (t);
5364 input_filename = saved_filename;
5365 lineno = saved_lineno;
5367 TYPE_BEING_DEFINED (t) = 0;
5369 if (current_class_type)
5370 popclass ();
5371 else
5372 error ("trying to finish struct, but kicked out due to previous parse errors");
5374 if (processing_template_decl && at_function_scope_p ())
5375 add_stmt (build_min (TAG_DEFN, t));
5377 return t;
5380 /* Return the dynamic type of INSTANCE, if known.
5381 Used to determine whether the virtual function table is needed
5382 or not.
5384 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5385 of our knowledge of its type. *NONNULL should be initialized
5386 before this function is called. */
5388 static tree
5389 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5391 switch (TREE_CODE (instance))
5393 case INDIRECT_REF:
5394 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5395 return NULL_TREE;
5396 else
5397 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5398 nonnull, cdtorp);
5400 case CALL_EXPR:
5401 /* This is a call to a constructor, hence it's never zero. */
5402 if (TREE_HAS_CONSTRUCTOR (instance))
5404 if (nonnull)
5405 *nonnull = 1;
5406 return TREE_TYPE (instance);
5408 return NULL_TREE;
5410 case SAVE_EXPR:
5411 /* This is a call to a constructor, hence it's never zero. */
5412 if (TREE_HAS_CONSTRUCTOR (instance))
5414 if (nonnull)
5415 *nonnull = 1;
5416 return TREE_TYPE (instance);
5418 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5420 case RTL_EXPR:
5421 return NULL_TREE;
5423 case PLUS_EXPR:
5424 case MINUS_EXPR:
5425 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5426 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5427 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5428 /* Propagate nonnull. */
5429 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5430 return NULL_TREE;
5432 case NOP_EXPR:
5433 case CONVERT_EXPR:
5434 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5436 case ADDR_EXPR:
5437 if (nonnull)
5438 *nonnull = 1;
5439 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5441 case COMPONENT_REF:
5442 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5444 case VAR_DECL:
5445 case FIELD_DECL:
5446 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5447 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5449 if (nonnull)
5450 *nonnull = 1;
5451 return TREE_TYPE (TREE_TYPE (instance));
5453 /* fall through... */
5454 case TARGET_EXPR:
5455 case PARM_DECL:
5456 case RESULT_DECL:
5457 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5459 if (nonnull)
5460 *nonnull = 1;
5461 return TREE_TYPE (instance);
5463 else if (instance == current_class_ptr)
5465 if (nonnull)
5466 *nonnull = 1;
5468 /* if we're in a ctor or dtor, we know our type. */
5469 if (DECL_LANG_SPECIFIC (current_function_decl)
5470 && (DECL_CONSTRUCTOR_P (current_function_decl)
5471 || DECL_DESTRUCTOR_P (current_function_decl)))
5473 if (cdtorp)
5474 *cdtorp = 1;
5475 return TREE_TYPE (TREE_TYPE (instance));
5478 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5480 /* Reference variables should be references to objects. */
5481 if (nonnull)
5482 *nonnull = 1;
5484 if (TREE_CODE (instance) == VAR_DECL
5485 && DECL_INITIAL (instance))
5486 return fixed_type_or_null (DECL_INITIAL (instance),
5487 nonnull, cdtorp);
5489 return NULL_TREE;
5491 default:
5492 return NULL_TREE;
5496 /* Return nonzero if the dynamic type of INSTANCE is known, and
5497 equivalent to the static type. We also handle the case where
5498 INSTANCE is really a pointer. Return negative if this is a
5499 ctor/dtor. There the dynamic type is known, but this might not be
5500 the most derived base of the original object, and hence virtual
5501 bases may not be layed out according to this type.
5503 Used to determine whether the virtual function table is needed
5504 or not.
5506 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5507 of our knowledge of its type. *NONNULL should be initialized
5508 before this function is called. */
5511 resolves_to_fixed_type_p (tree instance, int* nonnull)
5513 tree t = TREE_TYPE (instance);
5514 int cdtorp = 0;
5516 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5517 if (fixed == NULL_TREE)
5518 return 0;
5519 if (POINTER_TYPE_P (t))
5520 t = TREE_TYPE (t);
5521 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5522 return 0;
5523 return cdtorp ? -1 : 1;
5527 void
5528 init_class_processing (void)
5530 current_class_depth = 0;
5531 current_class_stack_size = 10;
5532 current_class_stack
5533 = (class_stack_node_t) xmalloc (current_class_stack_size
5534 * sizeof (struct class_stack_node));
5535 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5537 ridpointers[(int) RID_PUBLIC] = access_public_node;
5538 ridpointers[(int) RID_PRIVATE] = access_private_node;
5539 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5542 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5543 appropriate for TYPE.
5545 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5546 which can be seen locally to the class. They are shadowed by
5547 any subsequent local declaration (including parameter names).
5549 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5550 which have static meaning (i.e., static members, static
5551 member functions, enum declarations, etc).
5553 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5554 which can be seen locally to the class (as in 1), but
5555 know that we are doing this for declaration purposes
5556 (i.e. friend foo::bar (int)).
5558 So that we may avoid calls to lookup_name, we cache the _TYPE
5559 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5561 For multiple inheritance, we perform a two-pass depth-first search
5562 of the type lattice. The first pass performs a pre-order search,
5563 marking types after the type has had its fields installed in
5564 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5565 unmarks the marked types. If a field or member function name
5566 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5567 that name becomes `error_mark_node'. */
5569 void
5570 pushclass (tree type, bool modify)
5572 type = TYPE_MAIN_VARIANT (type);
5574 /* Make sure there is enough room for the new entry on the stack. */
5575 if (current_class_depth + 1 >= current_class_stack_size)
5577 current_class_stack_size *= 2;
5578 current_class_stack
5579 = (class_stack_node_t) xrealloc (current_class_stack,
5580 current_class_stack_size
5581 * sizeof (struct class_stack_node));
5584 /* Insert a new entry on the class stack. */
5585 current_class_stack[current_class_depth].name = current_class_name;
5586 current_class_stack[current_class_depth].type = current_class_type;
5587 current_class_stack[current_class_depth].access = current_access_specifier;
5588 current_class_stack[current_class_depth].names_used = 0;
5589 current_class_depth++;
5591 /* Now set up the new type. */
5592 current_class_name = TYPE_NAME (type);
5593 if (TREE_CODE (current_class_name) == TYPE_DECL)
5594 current_class_name = DECL_NAME (current_class_name);
5595 current_class_type = type;
5597 /* By default, things in classes are private, while things in
5598 structures or unions are public. */
5599 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5600 ? access_private_node
5601 : access_public_node);
5603 if (previous_class_type != NULL_TREE
5604 && (type != previous_class_type
5605 || !COMPLETE_TYPE_P (previous_class_type))
5606 && current_class_depth == 1)
5608 /* Forcibly remove any old class remnants. */
5609 invalidate_class_lookup_cache ();
5612 /* If we're about to enter a nested class, clear
5613 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5614 if (modify && current_class_depth > 1)
5615 clear_identifier_class_values ();
5617 pushlevel_class ();
5619 if (modify)
5621 if (type != previous_class_type || current_class_depth > 1)
5622 push_class_decls (type);
5623 else
5625 tree item;
5627 /* We are re-entering the same class we just left, so we
5628 don't have to search the whole inheritance matrix to find
5629 all the decls to bind again. Instead, we install the
5630 cached class_shadowed list, and walk through it binding
5631 names and setting up IDENTIFIER_TYPE_VALUEs. */
5632 set_class_shadows (previous_class_values);
5633 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5635 tree id = TREE_PURPOSE (item);
5636 tree decl = TREE_TYPE (item);
5638 push_class_binding (id, decl);
5639 if (TREE_CODE (decl) == TYPE_DECL)
5640 set_identifier_type_value (id, TREE_TYPE (decl));
5642 unuse_fields (type);
5645 storetags (CLASSTYPE_TAGS (type));
5649 /* When we exit a toplevel class scope, we save the
5650 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5651 reenter the class. Here, we've entered some other class, so we
5652 must invalidate our cache. */
5654 void
5655 invalidate_class_lookup_cache (void)
5657 tree t;
5659 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5660 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5661 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5663 previous_class_values = NULL_TREE;
5664 previous_class_type = NULL_TREE;
5667 /* Get out of the current class scope. If we were in a class scope
5668 previously, that is the one popped to. */
5670 void
5671 popclass (void)
5673 poplevel_class ();
5674 pop_class_decls ();
5676 current_class_depth--;
5677 current_class_name = current_class_stack[current_class_depth].name;
5678 current_class_type = current_class_stack[current_class_depth].type;
5679 current_access_specifier = current_class_stack[current_class_depth].access;
5680 if (current_class_stack[current_class_depth].names_used)
5681 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5684 /* Returns 1 if current_class_type is either T or a nested type of T.
5685 We start looking from 1 because entry 0 is from global scope, and has
5686 no type. */
5689 currently_open_class (tree t)
5691 int i;
5692 if (current_class_type && same_type_p (t, current_class_type))
5693 return 1;
5694 for (i = 1; i < current_class_depth; ++i)
5695 if (current_class_stack[i].type
5696 && same_type_p (current_class_stack [i].type, t))
5697 return 1;
5698 return 0;
5701 /* If either current_class_type or one of its enclosing classes are derived
5702 from T, return the appropriate type. Used to determine how we found
5703 something via unqualified lookup. */
5705 tree
5706 currently_open_derived_class (tree t)
5708 int i;
5710 /* The bases of a dependent type are unknown. */
5711 if (dependent_type_p (t))
5712 return NULL_TREE;
5714 if (DERIVED_FROM_P (t, current_class_type))
5715 return current_class_type;
5717 for (i = current_class_depth - 1; i > 0; --i)
5718 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5719 return current_class_stack[i].type;
5721 return NULL_TREE;
5724 /* When entering a class scope, all enclosing class scopes' names with
5725 static meaning (static variables, static functions, types and
5726 enumerators) have to be visible. This recursive function calls
5727 pushclass for all enclosing class contexts until global or a local
5728 scope is reached. TYPE is the enclosed class. */
5730 void
5731 push_nested_class (tree type)
5733 tree context;
5735 /* A namespace might be passed in error cases, like A::B:C. */
5736 if (type == NULL_TREE
5737 || type == error_mark_node
5738 || TREE_CODE (type) == NAMESPACE_DECL
5739 || ! IS_AGGR_TYPE (type)
5740 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5741 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5742 return;
5744 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5746 if (context && CLASS_TYPE_P (context))
5747 push_nested_class (context);
5748 pushclass (type, true);
5751 /* Undoes a push_nested_class call. */
5753 void
5754 pop_nested_class (void)
5756 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5758 popclass ();
5759 if (context && CLASS_TYPE_P (context))
5760 pop_nested_class ();
5763 /* Returns the number of extern "LANG" blocks we are nested within. */
5766 current_lang_depth (void)
5768 return VARRAY_ACTIVE_SIZE (current_lang_base);
5771 /* Set global variables CURRENT_LANG_NAME to appropriate value
5772 so that behavior of name-mangling machinery is correct. */
5774 void
5775 push_lang_context (tree name)
5777 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5779 if (name == lang_name_cplusplus)
5781 current_lang_name = name;
5783 else if (name == lang_name_java)
5785 current_lang_name = name;
5786 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5787 (See record_builtin_java_type in decl.c.) However, that causes
5788 incorrect debug entries if these types are actually used.
5789 So we re-enable debug output after extern "Java". */
5790 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5791 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5792 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5793 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5794 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5795 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5796 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5797 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5799 else if (name == lang_name_c)
5801 current_lang_name = name;
5803 else
5804 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5807 /* Get out of the current language scope. */
5809 void
5810 pop_lang_context (void)
5812 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5813 VARRAY_POP (current_lang_base);
5816 /* Type instantiation routines. */
5818 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5819 matches the TARGET_TYPE. If there is no satisfactory match, return
5820 error_mark_node, and issue an error message if COMPLAIN is
5821 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5822 If TEMPLATE_ONLY, the name of the overloaded function
5823 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5824 template arguments. */
5826 static tree
5827 resolve_address_of_overloaded_function (tree target_type,
5828 tree overload,
5829 int complain,
5830 int ptrmem,
5831 int template_only,
5832 tree explicit_targs)
5834 /* Here's what the standard says:
5836 [over.over]
5838 If the name is a function template, template argument deduction
5839 is done, and if the argument deduction succeeds, the deduced
5840 arguments are used to generate a single template function, which
5841 is added to the set of overloaded functions considered.
5843 Non-member functions and static member functions match targets of
5844 type "pointer-to-function" or "reference-to-function." Nonstatic
5845 member functions match targets of type "pointer-to-member
5846 function;" the function type of the pointer to member is used to
5847 select the member function from the set of overloaded member
5848 functions. If a nonstatic member function is selected, the
5849 reference to the overloaded function name is required to have the
5850 form of a pointer to member as described in 5.3.1.
5852 If more than one function is selected, any template functions in
5853 the set are eliminated if the set also contains a non-template
5854 function, and any given template function is eliminated if the
5855 set contains a second template function that is more specialized
5856 than the first according to the partial ordering rules 14.5.5.2.
5857 After such eliminations, if any, there shall remain exactly one
5858 selected function. */
5860 int is_ptrmem = 0;
5861 int is_reference = 0;
5862 /* We store the matches in a TREE_LIST rooted here. The functions
5863 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5864 interoperability with most_specialized_instantiation. */
5865 tree matches = NULL_TREE;
5866 tree fn;
5868 /* By the time we get here, we should be seeing only real
5869 pointer-to-member types, not the internal POINTER_TYPE to
5870 METHOD_TYPE representation. */
5871 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5872 && (TREE_CODE (TREE_TYPE (target_type))
5873 == METHOD_TYPE)), 0);
5875 if (TREE_CODE (overload) == COMPONENT_REF)
5876 overload = TREE_OPERAND (overload, 1);
5878 /* Check that the TARGET_TYPE is reasonable. */
5879 if (TYPE_PTRFN_P (target_type))
5880 /* This is OK. */;
5881 else if (TYPE_PTRMEMFUNC_P (target_type))
5882 /* This is OK, too. */
5883 is_ptrmem = 1;
5884 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5886 /* This is OK, too. This comes from a conversion to reference
5887 type. */
5888 target_type = build_reference_type (target_type);
5889 is_reference = 1;
5891 else
5893 if (complain)
5894 error ("\
5895 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5896 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5897 return error_mark_node;
5900 /* If we can find a non-template function that matches, we can just
5901 use it. There's no point in generating template instantiations
5902 if we're just going to throw them out anyhow. But, of course, we
5903 can only do this when we don't *need* a template function. */
5904 if (!template_only)
5906 tree fns;
5908 for (fns = overload; fns; fns = OVL_NEXT (fns))
5910 tree fn = OVL_CURRENT (fns);
5911 tree fntype;
5913 if (TREE_CODE (fn) == TEMPLATE_DECL)
5914 /* We're not looking for templates just yet. */
5915 continue;
5917 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5918 != is_ptrmem)
5919 /* We're looking for a non-static member, and this isn't
5920 one, or vice versa. */
5921 continue;
5923 /* See if there's a match. */
5924 fntype = TREE_TYPE (fn);
5925 if (is_ptrmem)
5926 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5927 else if (!is_reference)
5928 fntype = build_pointer_type (fntype);
5930 if (can_convert_arg (target_type, fntype, fn))
5931 matches = tree_cons (fn, NULL_TREE, matches);
5935 /* Now, if we've already got a match (or matches), there's no need
5936 to proceed to the template functions. But, if we don't have a
5937 match we need to look at them, too. */
5938 if (!matches)
5940 tree target_fn_type;
5941 tree target_arg_types;
5942 tree target_ret_type;
5943 tree fns;
5945 if (is_ptrmem)
5946 target_fn_type
5947 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5948 else
5949 target_fn_type = TREE_TYPE (target_type);
5950 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5951 target_ret_type = TREE_TYPE (target_fn_type);
5953 /* Never do unification on the 'this' parameter. */
5954 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5955 target_arg_types = TREE_CHAIN (target_arg_types);
5957 for (fns = overload; fns; fns = OVL_NEXT (fns))
5959 tree fn = OVL_CURRENT (fns);
5960 tree instantiation;
5961 tree instantiation_type;
5962 tree targs;
5964 if (TREE_CODE (fn) != TEMPLATE_DECL)
5965 /* We're only looking for templates. */
5966 continue;
5968 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5969 != is_ptrmem)
5970 /* We're not looking for a non-static member, and this is
5971 one, or vice versa. */
5972 continue;
5974 /* Try to do argument deduction. */
5975 targs = make_tree_vec (DECL_NTPARMS (fn));
5976 if (fn_type_unification (fn, explicit_targs, targs,
5977 target_arg_types, target_ret_type,
5978 DEDUCE_EXACT, -1) != 0)
5979 /* Argument deduction failed. */
5980 continue;
5982 /* Instantiate the template. */
5983 instantiation = instantiate_template (fn, targs,
5984 complain ? tf_error : tf_none);
5985 if (instantiation == error_mark_node)
5986 /* Instantiation failed. */
5987 continue;
5989 /* See if there's a match. */
5990 instantiation_type = TREE_TYPE (instantiation);
5991 if (is_ptrmem)
5992 instantiation_type =
5993 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5994 else if (!is_reference)
5995 instantiation_type = build_pointer_type (instantiation_type);
5996 if (can_convert_arg (target_type, instantiation_type, instantiation))
5997 matches = tree_cons (instantiation, fn, matches);
6000 /* Now, remove all but the most specialized of the matches. */
6001 if (matches)
6003 tree match = most_specialized_instantiation (matches);
6005 if (match != error_mark_node)
6006 matches = tree_cons (match, NULL_TREE, NULL_TREE);
6010 /* Now we should have exactly one function in MATCHES. */
6011 if (matches == NULL_TREE)
6013 /* There were *no* matches. */
6014 if (complain)
6016 error ("no matches converting function `%D' to type `%#T'",
6017 DECL_NAME (OVL_FUNCTION (overload)),
6018 target_type);
6020 /* print_candidates expects a chain with the functions in
6021 TREE_VALUE slots, so we cons one up here (we're losing anyway,
6022 so why be clever?). */
6023 for (; overload; overload = OVL_NEXT (overload))
6024 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6025 matches);
6027 print_candidates (matches);
6029 return error_mark_node;
6031 else if (TREE_CHAIN (matches))
6033 /* There were too many matches. */
6035 if (complain)
6037 tree match;
6039 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6040 DECL_NAME (OVL_FUNCTION (overload)),
6041 target_type);
6043 /* Since print_candidates expects the functions in the
6044 TREE_VALUE slot, we flip them here. */
6045 for (match = matches; match; match = TREE_CHAIN (match))
6046 TREE_VALUE (match) = TREE_PURPOSE (match);
6048 print_candidates (matches);
6051 return error_mark_node;
6054 /* Good, exactly one match. Now, convert it to the correct type. */
6055 fn = TREE_PURPOSE (matches);
6057 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6058 && !ptrmem && !flag_ms_extensions)
6060 static int explained;
6062 if (!complain)
6063 return error_mark_node;
6065 pedwarn ("assuming pointer to member `%D'", fn);
6066 if (!explained)
6068 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6069 explained = 1;
6072 mark_used (fn);
6074 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6075 return build_unary_op (ADDR_EXPR, fn, 0);
6076 else
6078 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6079 will mark the function as addressed, but here we must do it
6080 explicitly. */
6081 cxx_mark_addressable (fn);
6083 return fn;
6087 /* This function will instantiate the type of the expression given in
6088 RHS to match the type of LHSTYPE. If errors exist, then return
6089 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6090 we complain on errors. If we are not complaining, never modify rhs,
6091 as overload resolution wants to try many possible instantiations, in
6092 the hope that at least one will work.
6094 For non-recursive calls, LHSTYPE should be a function, pointer to
6095 function, or a pointer to member function. */
6097 tree
6098 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
6100 int complain = (flags & tf_error);
6101 int strict = (flags & tf_no_attributes)
6102 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6103 int allow_ptrmem = flags & tf_ptrmem_ok;
6105 flags &= ~tf_ptrmem_ok;
6107 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6109 if (complain)
6110 error ("not enough type information");
6111 return error_mark_node;
6114 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6116 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6117 return rhs;
6118 if (flag_ms_extensions
6119 && TYPE_PTRMEMFUNC_P (lhstype)
6120 && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6121 /* Microsoft allows `A::f' to be resolved to a
6122 pointer-to-member. */
6124 else
6126 if (complain)
6127 error ("argument of type `%T' does not match `%T'",
6128 TREE_TYPE (rhs), lhstype);
6129 return error_mark_node;
6133 if (TREE_CODE (rhs) == BASELINK)
6134 rhs = BASELINK_FUNCTIONS (rhs);
6136 /* We don't overwrite rhs if it is an overloaded function.
6137 Copying it would destroy the tree link. */
6138 if (TREE_CODE (rhs) != OVERLOAD)
6139 rhs = copy_node (rhs);
6141 /* This should really only be used when attempting to distinguish
6142 what sort of a pointer to function we have. For now, any
6143 arithmetic operation which is not supported on pointers
6144 is rejected as an error. */
6146 switch (TREE_CODE (rhs))
6148 case TYPE_EXPR:
6149 case CONVERT_EXPR:
6150 case SAVE_EXPR:
6151 case CONSTRUCTOR:
6152 case BUFFER_REF:
6153 abort ();
6154 return error_mark_node;
6156 case INDIRECT_REF:
6157 case ARRAY_REF:
6159 tree new_rhs;
6161 new_rhs = instantiate_type (build_pointer_type (lhstype),
6162 TREE_OPERAND (rhs, 0), flags);
6163 if (new_rhs == error_mark_node)
6164 return error_mark_node;
6166 TREE_TYPE (rhs) = lhstype;
6167 TREE_OPERAND (rhs, 0) = new_rhs;
6168 return rhs;
6171 case NOP_EXPR:
6172 rhs = copy_node (TREE_OPERAND (rhs, 0));
6173 TREE_TYPE (rhs) = unknown_type_node;
6174 return instantiate_type (lhstype, rhs, flags);
6176 case COMPONENT_REF:
6177 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6179 case OFFSET_REF:
6180 rhs = TREE_OPERAND (rhs, 1);
6181 if (BASELINK_P (rhs))
6182 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6183 flags | allow_ptrmem);
6185 /* This can happen if we are forming a pointer-to-member for a
6186 member template. */
6187 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6189 /* Fall through. */
6191 case TEMPLATE_ID_EXPR:
6193 tree fns = TREE_OPERAND (rhs, 0);
6194 tree args = TREE_OPERAND (rhs, 1);
6196 return
6197 resolve_address_of_overloaded_function (lhstype,
6198 fns,
6199 complain,
6200 allow_ptrmem,
6201 /*template_only=*/1,
6202 args);
6205 case OVERLOAD:
6206 case FUNCTION_DECL:
6207 return
6208 resolve_address_of_overloaded_function (lhstype,
6209 rhs,
6210 complain,
6211 allow_ptrmem,
6212 /*template_only=*/0,
6213 /*explicit_targs=*/NULL_TREE);
6215 case TREE_LIST:
6216 /* Now we should have a baselink. */
6217 my_friendly_assert (BASELINK_P (rhs), 990412);
6219 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6221 case CALL_EXPR:
6222 /* This is too hard for now. */
6223 abort ();
6224 return error_mark_node;
6226 case PLUS_EXPR:
6227 case MINUS_EXPR:
6228 case COMPOUND_EXPR:
6229 TREE_OPERAND (rhs, 0)
6230 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6231 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6232 return error_mark_node;
6233 TREE_OPERAND (rhs, 1)
6234 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6235 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6236 return error_mark_node;
6238 TREE_TYPE (rhs) = lhstype;
6239 return rhs;
6241 case MULT_EXPR:
6242 case TRUNC_DIV_EXPR:
6243 case FLOOR_DIV_EXPR:
6244 case CEIL_DIV_EXPR:
6245 case ROUND_DIV_EXPR:
6246 case RDIV_EXPR:
6247 case TRUNC_MOD_EXPR:
6248 case FLOOR_MOD_EXPR:
6249 case CEIL_MOD_EXPR:
6250 case ROUND_MOD_EXPR:
6251 case FIX_ROUND_EXPR:
6252 case FIX_FLOOR_EXPR:
6253 case FIX_CEIL_EXPR:
6254 case FIX_TRUNC_EXPR:
6255 case FLOAT_EXPR:
6256 case NEGATE_EXPR:
6257 case ABS_EXPR:
6258 case MAX_EXPR:
6259 case MIN_EXPR:
6260 case FFS_EXPR:
6262 case BIT_AND_EXPR:
6263 case BIT_IOR_EXPR:
6264 case BIT_XOR_EXPR:
6265 case LSHIFT_EXPR:
6266 case RSHIFT_EXPR:
6267 case LROTATE_EXPR:
6268 case RROTATE_EXPR:
6270 case PREINCREMENT_EXPR:
6271 case PREDECREMENT_EXPR:
6272 case POSTINCREMENT_EXPR:
6273 case POSTDECREMENT_EXPR:
6274 if (complain)
6275 error ("invalid operation on uninstantiated type");
6276 return error_mark_node;
6278 case TRUTH_AND_EXPR:
6279 case TRUTH_OR_EXPR:
6280 case TRUTH_XOR_EXPR:
6281 case LT_EXPR:
6282 case LE_EXPR:
6283 case GT_EXPR:
6284 case GE_EXPR:
6285 case EQ_EXPR:
6286 case NE_EXPR:
6287 case TRUTH_ANDIF_EXPR:
6288 case TRUTH_ORIF_EXPR:
6289 case TRUTH_NOT_EXPR:
6290 if (complain)
6291 error ("not enough type information");
6292 return error_mark_node;
6294 case COND_EXPR:
6295 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6297 if (complain)
6298 error ("not enough type information");
6299 return error_mark_node;
6301 TREE_OPERAND (rhs, 1)
6302 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6303 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6304 return error_mark_node;
6305 TREE_OPERAND (rhs, 2)
6306 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6307 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6308 return error_mark_node;
6310 TREE_TYPE (rhs) = lhstype;
6311 return rhs;
6313 case MODIFY_EXPR:
6314 TREE_OPERAND (rhs, 1)
6315 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6316 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6317 return error_mark_node;
6319 TREE_TYPE (rhs) = lhstype;
6320 return rhs;
6322 case ADDR_EXPR:
6324 if (PTRMEM_OK_P (rhs))
6325 flags |= tf_ptrmem_ok;
6327 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6329 case ENTRY_VALUE_EXPR:
6330 abort ();
6331 return error_mark_node;
6333 case ERROR_MARK:
6334 return error_mark_node;
6336 default:
6337 abort ();
6338 return error_mark_node;
6342 /* Return the name of the virtual function pointer field
6343 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6344 this may have to look back through base types to find the
6345 ultimate field name. (For single inheritance, these could
6346 all be the same name. Who knows for multiple inheritance). */
6348 static tree
6349 get_vfield_name (tree type)
6351 tree binfo = TYPE_BINFO (type);
6352 char *buf;
6354 while (BINFO_BASETYPES (binfo)
6355 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6356 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6357 binfo = BINFO_BASETYPE (binfo, 0);
6359 type = BINFO_TYPE (binfo);
6360 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6361 + TYPE_NAME_LENGTH (type) + 2);
6362 sprintf (buf, VFIELD_NAME_FORMAT,
6363 IDENTIFIER_POINTER (constructor_name (type)));
6364 return get_identifier (buf);
6367 void
6368 print_class_statistics (void)
6370 #ifdef GATHER_STATISTICS
6371 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6372 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6373 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6374 n_build_method_call, n_inner_fields_searched);
6375 if (n_vtables)
6377 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6378 n_vtables, n_vtable_searches);
6379 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6380 n_vtable_entries, n_vtable_elems);
6382 #endif
6385 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6386 according to [class]:
6387 The class-name is also inserted
6388 into the scope of the class itself. For purposes of access checking,
6389 the inserted class name is treated as if it were a public member name. */
6391 void
6392 build_self_reference (void)
6394 tree name = constructor_name (current_class_type);
6395 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6396 tree saved_cas;
6398 DECL_NONLOCAL (value) = 1;
6399 DECL_CONTEXT (value) = current_class_type;
6400 DECL_ARTIFICIAL (value) = 1;
6402 if (processing_template_decl)
6403 value = push_template_decl (value);
6405 saved_cas = current_access_specifier;
6406 current_access_specifier = access_public_node;
6407 finish_member_declaration (value);
6408 current_access_specifier = saved_cas;
6411 /* Returns 1 if TYPE contains only padding bytes. */
6414 is_empty_class (tree type)
6416 if (type == error_mark_node)
6417 return 0;
6419 if (! IS_AGGR_TYPE (type))
6420 return 0;
6422 /* In G++ 3.2, whether or not a class was empty was determined by
6423 looking at its size. */
6424 if (abi_version_at_least (2))
6425 return CLASSTYPE_EMPTY_P (type);
6426 else
6427 return integer_zerop (CLASSTYPE_SIZE (type));
6430 /* Returns true if TYPE contains an empty class. */
6432 static bool
6433 contains_empty_class_p (tree type)
6435 if (is_empty_class (type))
6436 return true;
6437 if (CLASS_TYPE_P (type))
6439 tree field;
6440 int i;
6442 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6443 if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6444 return true;
6445 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6446 if (TREE_CODE (field) == FIELD_DECL
6447 && !DECL_ARTIFICIAL (field)
6448 && is_empty_class (TREE_TYPE (field)))
6449 return true;
6451 else if (TREE_CODE (type) == ARRAY_TYPE)
6452 return contains_empty_class_p (TREE_TYPE (type));
6453 return false;
6456 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6457 a *_TYPE node. NODE can also be a local class. */
6459 tree
6460 get_enclosing_class (tree type)
6462 tree node = type;
6464 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6466 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6468 case 'd':
6469 node = DECL_CONTEXT (node);
6470 break;
6472 case 't':
6473 if (node != type)
6474 return node;
6475 node = TYPE_CONTEXT (node);
6476 break;
6478 default:
6479 abort ();
6482 return NULL_TREE;
6485 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6488 is_base_of_enclosing_class (tree base, tree type)
6490 while (type)
6492 if (lookup_base (type, base, ba_any, NULL))
6493 return 1;
6495 type = get_enclosing_class (type);
6497 return 0;
6500 /* Note that NAME was looked up while the current class was being
6501 defined and that the result of that lookup was DECL. */
6503 void
6504 maybe_note_name_used_in_class (tree name, tree decl)
6506 splay_tree names_used;
6508 /* If we're not defining a class, there's nothing to do. */
6509 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6510 return;
6512 /* If there's already a binding for this NAME, then we don't have
6513 anything to worry about. */
6514 if (IDENTIFIER_CLASS_VALUE (name))
6515 return;
6517 if (!current_class_stack[current_class_depth - 1].names_used)
6518 current_class_stack[current_class_depth - 1].names_used
6519 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6520 names_used = current_class_stack[current_class_depth - 1].names_used;
6522 splay_tree_insert (names_used,
6523 (splay_tree_key) name,
6524 (splay_tree_value) decl);
6527 /* Note that NAME was declared (as DECL) in the current class. Check
6528 to see that the declaration is valid. */
6530 void
6531 note_name_declared_in_class (tree name, tree decl)
6533 splay_tree names_used;
6534 splay_tree_node n;
6536 /* Look to see if we ever used this name. */
6537 names_used
6538 = current_class_stack[current_class_depth - 1].names_used;
6539 if (!names_used)
6540 return;
6542 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6543 if (n)
6545 /* [basic.scope.class]
6547 A name N used in a class S shall refer to the same declaration
6548 in its context and when re-evaluated in the completed scope of
6549 S. */
6550 error ("declaration of `%#D'", decl);
6551 cp_error_at ("changes meaning of `%D' from `%+#D'",
6552 DECL_NAME (OVL_CURRENT (decl)),
6553 (tree) n->value);
6557 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6558 Secondary vtables are merged with primary vtables; this function
6559 will return the VAR_DECL for the primary vtable. */
6561 tree
6562 get_vtbl_decl_for_binfo (tree binfo)
6564 tree decl;
6566 decl = BINFO_VTABLE (binfo);
6567 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6569 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6570 2000403);
6571 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6573 if (decl)
6574 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6575 return decl;
6579 /* Returns the binfo for the primary base of BINFO. If the resulting
6580 BINFO is a virtual base, and it is inherited elsewhere in the
6581 hierarchy, then the returned binfo might not be the primary base of
6582 BINFO in the complete object. Check BINFO_PRIMARY_P or
6583 BINFO_LOST_PRIMARY_P to be sure. */
6585 tree
6586 get_primary_binfo (tree binfo)
6588 tree primary_base;
6589 tree result;
6591 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6592 if (!primary_base)
6593 return NULL_TREE;
6595 result = copied_binfo (primary_base, binfo);
6596 return result;
6599 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6601 static int
6602 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6604 if (!indented_p)
6605 fprintf (stream, "%*s", indent, "");
6606 return 1;
6609 /* Dump the offsets of all the bases rooted at BINFO to STREAM.
6610 INDENT should be zero when called from the top level; it is
6611 incremented recursively. IGO indicates the next expected BINFO in
6612 inheritance graph ordering. */
6614 static tree
6615 dump_class_hierarchy_r (FILE *stream,
6616 int flags,
6617 tree binfo,
6618 tree igo,
6619 int indent)
6621 int indented = 0;
6622 tree base_binfos;
6624 indented = maybe_indent_hierarchy (stream, indent, 0);
6625 fprintf (stream, "%s (0x%lx) ",
6626 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6627 (unsigned long) binfo);
6628 if (binfo != igo)
6630 fprintf (stream, "alternative-path\n");
6631 return igo;
6633 igo = TREE_CHAIN (binfo);
6635 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6636 tree_low_cst (BINFO_OFFSET (binfo), 0));
6637 if (is_empty_class (BINFO_TYPE (binfo)))
6638 fprintf (stream, " empty");
6639 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6640 fprintf (stream, " nearly-empty");
6641 if (TREE_VIA_VIRTUAL (binfo))
6642 fprintf (stream, " virtual");
6643 fprintf (stream, "\n");
6645 indented = 0;
6646 if (BINFO_PRIMARY_BASE_OF (binfo))
6648 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6649 fprintf (stream, " primary-for %s (0x%lx)",
6650 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6651 TFF_PLAIN_IDENTIFIER),
6652 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6654 if (BINFO_LOST_PRIMARY_P (binfo))
6656 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6657 fprintf (stream, " lost-primary");
6659 if (indented)
6660 fprintf (stream, "\n");
6662 if (!(flags & TDF_SLIM))
6664 int indented = 0;
6666 if (BINFO_SUBVTT_INDEX (binfo))
6668 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6669 fprintf (stream, " subvttidx=%s",
6670 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6671 TFF_PLAIN_IDENTIFIER));
6673 if (BINFO_VPTR_INDEX (binfo))
6675 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6676 fprintf (stream, " vptridx=%s",
6677 expr_as_string (BINFO_VPTR_INDEX (binfo),
6678 TFF_PLAIN_IDENTIFIER));
6680 if (BINFO_VPTR_FIELD (binfo))
6682 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6683 fprintf (stream, " vbaseoffset=%s",
6684 expr_as_string (BINFO_VPTR_FIELD (binfo),
6685 TFF_PLAIN_IDENTIFIER));
6687 if (BINFO_VTABLE (binfo))
6689 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6690 fprintf (stream, " vptr=%s",
6691 expr_as_string (BINFO_VTABLE (binfo),
6692 TFF_PLAIN_IDENTIFIER));
6695 if (indented)
6696 fprintf (stream, "\n");
6699 base_binfos = BINFO_BASETYPES (binfo);
6700 if (base_binfos)
6702 int ix, n;
6704 n = TREE_VEC_LENGTH (base_binfos);
6705 for (ix = 0; ix != n; ix++)
6707 tree base_binfo = TREE_VEC_ELT (base_binfos, ix);
6709 igo = dump_class_hierarchy_r (stream, flags, base_binfo,
6710 igo, indent + 2);
6714 return igo;
6717 /* Dump the BINFO hierarchy for T. */
6719 static void
6720 dump_class_hierarchy (tree t)
6722 int flags;
6723 FILE *stream = dump_begin (TDI_class, &flags);
6725 if (!stream)
6726 return;
6728 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6729 fprintf (stream, " size=%lu align=%lu\n",
6730 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6731 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6732 fprintf (stream, " base size=%lu base align=%lu\n",
6733 (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
6734 / BITS_PER_UNIT),
6735 (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
6736 / BITS_PER_UNIT));
6737 dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
6738 fprintf (stream, "\n");
6739 dump_end (TDI_class, stream);
6742 static void
6743 dump_array (FILE * stream, tree decl)
6745 tree inits;
6746 int ix;
6747 HOST_WIDE_INT elt;
6748 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6750 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6751 / BITS_PER_UNIT);
6752 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6753 fprintf (stream, " %s entries",
6754 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6755 TFF_PLAIN_IDENTIFIER));
6756 fprintf (stream, "\n");
6758 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6759 inits; ix++, inits = TREE_CHAIN (inits))
6760 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6761 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6764 static void
6765 dump_vtable (tree t, tree binfo, tree vtable)
6767 int flags;
6768 FILE *stream = dump_begin (TDI_class, &flags);
6770 if (!stream)
6771 return;
6773 if (!(flags & TDF_SLIM))
6775 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6777 fprintf (stream, "%s for %s",
6778 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6779 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6780 if (ctor_vtbl_p)
6782 if (!TREE_VIA_VIRTUAL (binfo))
6783 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6784 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6786 fprintf (stream, "\n");
6787 dump_array (stream, vtable);
6788 fprintf (stream, "\n");
6791 dump_end (TDI_class, stream);
6794 static void
6795 dump_vtt (tree t, tree vtt)
6797 int flags;
6798 FILE *stream = dump_begin (TDI_class, &flags);
6800 if (!stream)
6801 return;
6803 if (!(flags & TDF_SLIM))
6805 fprintf (stream, "VTT for %s\n",
6806 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6807 dump_array (stream, vtt);
6808 fprintf (stream, "\n");
6811 dump_end (TDI_class, stream);
6814 /* Virtual function table initialization. */
6816 /* Create all the necessary vtables for T and its base classes. */
6818 static void
6819 finish_vtbls (tree t)
6821 tree list;
6822 tree vbase;
6824 /* We lay out the primary and secondary vtables in one contiguous
6825 vtable. The primary vtable is first, followed by the non-virtual
6826 secondary vtables in inheritance graph order. */
6827 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6828 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6829 TYPE_BINFO (t), t, list);
6831 /* Then come the virtual bases, also in inheritance graph order. */
6832 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6834 if (!TREE_VIA_VIRTUAL (vbase))
6835 continue;
6836 accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
6839 if (TYPE_BINFO_VTABLE (t))
6840 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6843 /* Initialize the vtable for BINFO with the INITS. */
6845 static void
6846 initialize_vtable (tree binfo, tree inits)
6848 tree decl;
6850 layout_vtable_decl (binfo, list_length (inits));
6851 decl = get_vtbl_decl_for_binfo (binfo);
6852 initialize_array (decl, inits);
6853 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6856 /* Initialize DECL (a declaration for a namespace-scope array) with
6857 the INITS. */
6859 static void
6860 initialize_array (tree decl, tree inits)
6862 tree context;
6864 context = DECL_CONTEXT (decl);
6865 DECL_CONTEXT (decl) = NULL_TREE;
6866 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6867 TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
6868 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6869 DECL_CONTEXT (decl) = context;
6872 /* Build the VTT (virtual table table) for T.
6873 A class requires a VTT if it has virtual bases.
6875 This holds
6876 1 - primary virtual pointer for complete object T
6877 2 - secondary VTTs for each direct non-virtual base of T which requires a
6879 3 - secondary virtual pointers for each direct or indirect base of T which
6880 has virtual bases or is reachable via a virtual path from T.
6881 4 - secondary VTTs for each direct or indirect virtual base of T.
6883 Secondary VTTs look like complete object VTTs without part 4. */
6885 static void
6886 build_vtt (tree t)
6888 tree inits;
6889 tree type;
6890 tree vtt;
6891 tree index;
6893 /* Build up the initializers for the VTT. */
6894 inits = NULL_TREE;
6895 index = size_zero_node;
6896 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6898 /* If we didn't need a VTT, we're done. */
6899 if (!inits)
6900 return;
6902 /* Figure out the type of the VTT. */
6903 type = build_index_type (size_int (list_length (inits) - 1));
6904 type = build_cplus_array_type (const_ptr_type_node, type);
6906 /* Now, build the VTT object itself. */
6907 vtt = build_vtable (t, get_vtt_name (t), type);
6908 initialize_array (vtt, inits);
6909 /* Add the VTT to the vtables list. */
6910 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6911 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
6913 dump_vtt (t, vtt);
6916 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
6917 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
6918 and CHAIN the vtable pointer for this binfo after construction is
6919 complete. VALUE can also be another BINFO, in which case we recurse. */
6921 static tree
6922 binfo_ctor_vtable (tree binfo)
6924 tree vt;
6926 while (1)
6928 vt = BINFO_VTABLE (binfo);
6929 if (TREE_CODE (vt) == TREE_LIST)
6930 vt = TREE_VALUE (vt);
6931 if (TREE_CODE (vt) == TREE_VEC)
6932 binfo = vt;
6933 else
6934 break;
6937 return vt;
6940 /* Recursively build the VTT-initializer for BINFO (which is in the
6941 hierarchy dominated by T). INITS points to the end of the initializer
6942 list to date. INDEX is the VTT index where the next element will be
6943 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
6944 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
6945 for virtual bases of T. When it is not so, we build the constructor
6946 vtables for the BINFO-in-T variant. */
6948 static tree *
6949 build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
6951 int i;
6952 tree b;
6953 tree init;
6954 tree secondary_vptrs;
6955 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
6957 /* We only need VTTs for subobjects with virtual bases. */
6958 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
6959 return inits;
6961 /* We need to use a construction vtable if this is not the primary
6962 VTT. */
6963 if (!top_level_p)
6965 build_ctor_vtbl_group (binfo, t);
6967 /* Record the offset in the VTT where this sub-VTT can be found. */
6968 BINFO_SUBVTT_INDEX (binfo) = *index;
6971 /* Add the address of the primary vtable for the complete object. */
6972 init = binfo_ctor_vtable (binfo);
6973 *inits = build_tree_list (NULL_TREE, init);
6974 inits = &TREE_CHAIN (*inits);
6975 if (top_level_p)
6977 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
6978 BINFO_VPTR_INDEX (binfo) = *index;
6980 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
6982 /* Recursively add the secondary VTTs for non-virtual bases. */
6983 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6985 b = BINFO_BASETYPE (binfo, i);
6986 if (!TREE_VIA_VIRTUAL (b))
6987 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
6988 inits, index);
6991 /* Add secondary virtual pointers for all subobjects of BINFO with
6992 either virtual bases or reachable along a virtual path, except
6993 subobjects that are non-virtual primary bases. */
6994 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
6995 TREE_TYPE (secondary_vptrs) = *index;
6996 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
6997 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
6999 dfs_walk_real (binfo,
7000 dfs_build_secondary_vptr_vtt_inits,
7001 NULL,
7002 dfs_ctor_vtable_bases_queue_p,
7003 secondary_vptrs);
7004 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7005 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7006 secondary_vptrs);
7008 *index = TREE_TYPE (secondary_vptrs);
7010 /* The secondary vptrs come back in reverse order. After we reverse
7011 them, and add the INITS, the last init will be the first element
7012 of the chain. */
7013 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7014 if (secondary_vptrs)
7016 *inits = nreverse (secondary_vptrs);
7017 inits = &TREE_CHAIN (secondary_vptrs);
7018 my_friendly_assert (*inits == NULL_TREE, 20000517);
7021 /* Add the secondary VTTs for virtual bases. */
7022 if (top_level_p)
7023 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7025 if (!TREE_VIA_VIRTUAL (b))
7026 continue;
7028 inits = build_vtt_inits (b, t, inits, index);
7031 if (!top_level_p)
7033 tree data = tree_cons (t, binfo, NULL_TREE);
7034 VTT_TOP_LEVEL_P (data) = 0;
7035 VTT_MARKED_BINFO_P (data) = 0;
7037 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7038 dfs_ctor_vtable_bases_queue_p,
7039 data);
7042 return inits;
7045 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7046 for the base in most derived. DATA is a TREE_LIST who's
7047 TREE_CHAIN is the type of the base being
7048 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7049 flag of DATA indicates that this is a constructor vtable. The
7050 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7052 static tree
7053 dfs_build_secondary_vptr_vtt_inits (tree binfo, void* data)
7055 tree l;
7056 tree t;
7057 tree init;
7058 tree index;
7059 int top_level_p;
7061 l = (tree) data;
7062 t = TREE_CHAIN (l);
7063 top_level_p = VTT_TOP_LEVEL_P (l);
7065 BINFO_MARKED (binfo) = 1;
7067 /* We don't care about bases that don't have vtables. */
7068 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7069 return NULL_TREE;
7071 /* We're only interested in proper subobjects of T. */
7072 if (same_type_p (BINFO_TYPE (binfo), t))
7073 return NULL_TREE;
7075 /* We're not interested in non-virtual primary bases. */
7076 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7077 return NULL_TREE;
7079 /* If BINFO has virtual bases or is reachable via a virtual path
7080 from T, it'll have a secondary vptr. */
7081 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7082 && !binfo_via_virtual (binfo, t))
7083 return NULL_TREE;
7085 /* Record the index where this secondary vptr can be found. */
7086 index = TREE_TYPE (l);
7087 if (top_level_p)
7089 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7090 BINFO_VPTR_INDEX (binfo) = index;
7092 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7093 TYPE_SIZE_UNIT (ptr_type_node));
7095 /* Add the initializer for the secondary vptr itself. */
7096 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7098 /* It's a primary virtual base, and this is not the construction
7099 vtable. Find the base this is primary of in the inheritance graph,
7100 and use that base's vtable now. */
7101 while (BINFO_PRIMARY_BASE_OF (binfo))
7102 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7104 init = binfo_ctor_vtable (binfo);
7105 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7107 return NULL_TREE;
7110 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7111 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7112 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7113 hierarchy. */
7115 static tree
7116 dfs_ctor_vtable_bases_queue_p (tree derived, int ix,
7117 void* data)
7119 tree binfo = BINFO_BASETYPE (derived, ix);
7121 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7122 return NULL_TREE;
7123 return binfo;
7126 /* Called from build_vtt_inits via dfs_walk. After building constructor
7127 vtables and generating the sub-vtt from them, we need to restore the
7128 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7129 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7131 static tree
7132 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7134 BINFO_MARKED (binfo) = 0;
7136 /* We don't care about bases that don't have vtables. */
7137 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7138 return NULL_TREE;
7140 /* If we scribbled the construction vtable vptr into BINFO, clear it
7141 out now. */
7142 if (BINFO_VTABLE (binfo)
7143 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7144 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7145 == TREE_VALUE ((tree) data)))
7146 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7148 return NULL_TREE;
7151 /* Build the construction vtable group for BINFO which is in the
7152 hierarchy dominated by T. */
7154 static void
7155 build_ctor_vtbl_group (tree binfo, tree t)
7157 tree list;
7158 tree type;
7159 tree vtbl;
7160 tree inits;
7161 tree id;
7162 tree vbase;
7164 /* See if we've already created this construction vtable group. */
7165 id = mangle_ctor_vtbl_for_type (t, binfo);
7166 if (IDENTIFIER_GLOBAL_VALUE (id))
7167 return;
7169 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7170 /* Build a version of VTBL (with the wrong type) for use in
7171 constructing the addresses of secondary vtables in the
7172 construction vtable group. */
7173 vtbl = build_vtable (t, id, ptr_type_node);
7174 list = build_tree_list (vtbl, NULL_TREE);
7175 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7176 binfo, t, list);
7178 /* Add the vtables for each of our virtual bases using the vbase in T
7179 binfo. */
7180 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7181 vbase;
7182 vbase = TREE_CHAIN (vbase))
7184 tree b;
7186 if (!TREE_VIA_VIRTUAL (vbase))
7187 continue;
7188 b = copied_binfo (vbase, binfo);
7190 accumulate_vtbl_inits (b, vbase, binfo, t, list);
7192 inits = TREE_VALUE (list);
7194 /* Figure out the type of the construction vtable. */
7195 type = build_index_type (size_int (list_length (inits) - 1));
7196 type = build_cplus_array_type (vtable_entry_type, type);
7197 TREE_TYPE (vtbl) = type;
7199 /* Initialize the construction vtable. */
7200 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7201 initialize_array (vtbl, inits);
7202 dump_vtable (t, binfo, vtbl);
7205 /* Add the vtbl initializers for BINFO (and its bases other than
7206 non-virtual primaries) to the list of INITS. BINFO is in the
7207 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7208 the constructor the vtbl inits should be accumulated for. (If this
7209 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7210 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7211 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7212 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7213 but are not necessarily the same in terms of layout. */
7215 static void
7216 accumulate_vtbl_inits (tree binfo,
7217 tree orig_binfo,
7218 tree rtti_binfo,
7219 tree t,
7220 tree inits)
7222 int i;
7223 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7225 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7226 BINFO_TYPE (orig_binfo)),
7227 20000517);
7229 /* If it doesn't have a vptr, we don't do anything. */
7230 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7231 return;
7233 /* If we're building a construction vtable, we're not interested in
7234 subobjects that don't require construction vtables. */
7235 if (ctor_vtbl_p
7236 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7237 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7238 return;
7240 /* Build the initializers for the BINFO-in-T vtable. */
7241 TREE_VALUE (inits)
7242 = chainon (TREE_VALUE (inits),
7243 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7244 rtti_binfo, t, inits));
7246 /* Walk the BINFO and its bases. We walk in preorder so that as we
7247 initialize each vtable we can figure out at what offset the
7248 secondary vtable lies from the primary vtable. We can't use
7249 dfs_walk here because we need to iterate through bases of BINFO
7250 and RTTI_BINFO simultaneously. */
7251 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7253 tree base_binfo = BINFO_BASETYPE (binfo, i);
7255 /* Skip virtual bases. */
7256 if (TREE_VIA_VIRTUAL (base_binfo))
7257 continue;
7258 accumulate_vtbl_inits (base_binfo,
7259 BINFO_BASETYPE (orig_binfo, i),
7260 rtti_binfo, t,
7261 inits);
7265 /* Called from accumulate_vtbl_inits. Returns the initializers for
7266 the BINFO vtable. */
7268 static tree
7269 dfs_accumulate_vtbl_inits (tree binfo,
7270 tree orig_binfo,
7271 tree rtti_binfo,
7272 tree t,
7273 tree l)
7275 tree inits = NULL_TREE;
7276 tree vtbl = NULL_TREE;
7277 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7279 if (ctor_vtbl_p
7280 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7282 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7283 primary virtual base. If it is not the same primary in
7284 the hierarchy of T, we'll need to generate a ctor vtable
7285 for it, to place at its location in T. If it is the same
7286 primary, we still need a VTT entry for the vtable, but it
7287 should point to the ctor vtable for the base it is a
7288 primary for within the sub-hierarchy of RTTI_BINFO.
7290 There are three possible cases:
7292 1) We are in the same place.
7293 2) We are a primary base within a lost primary virtual base of
7294 RTTI_BINFO.
7295 3) We are primary to something not a base of RTTI_BINFO. */
7297 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7298 tree last = NULL_TREE;
7300 /* First, look through the bases we are primary to for RTTI_BINFO
7301 or a virtual base. */
7302 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7304 last = b;
7305 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7306 break;
7308 /* If we run out of primary links, keep looking down our
7309 inheritance chain; we might be an indirect primary. */
7310 if (b == NULL_TREE)
7311 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7312 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7313 break;
7315 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7316 base B and it is a base of RTTI_BINFO, this is case 2. In
7317 either case, we share our vtable with LAST, i.e. the
7318 derived-most base within B of which we are a primary. */
7319 if (b == rtti_binfo
7320 || (b && purpose_member (BINFO_TYPE (b),
7321 CLASSTYPE_VBASECLASSES (BINFO_TYPE (rtti_binfo)))))
7322 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7323 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7324 binfo_ctor_vtable after everything's been set up. */
7325 vtbl = last;
7327 /* Otherwise, this is case 3 and we get our own. */
7329 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
7330 return inits;
7332 if (!vtbl)
7334 tree index;
7335 int non_fn_entries;
7337 /* Compute the initializer for this vtable. */
7338 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7339 &non_fn_entries);
7341 /* Figure out the position to which the VPTR should point. */
7342 vtbl = TREE_PURPOSE (l);
7343 vtbl = build1 (ADDR_EXPR,
7344 vtbl_ptr_type_node,
7345 vtbl);
7346 TREE_CONSTANT (vtbl) = 1;
7347 index = size_binop (PLUS_EXPR,
7348 size_int (non_fn_entries),
7349 size_int (list_length (TREE_VALUE (l))));
7350 index = size_binop (MULT_EXPR,
7351 TYPE_SIZE_UNIT (vtable_entry_type),
7352 index);
7353 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7354 TREE_CONSTANT (vtbl) = 1;
7357 if (ctor_vtbl_p)
7358 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7359 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7360 straighten this out. */
7361 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7362 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7363 inits = NULL_TREE;
7364 else
7365 /* For an ordinary vtable, set BINFO_VTABLE. */
7366 BINFO_VTABLE (binfo) = vtbl;
7368 return inits;
7371 /* Construct the initializer for BINFO's virtual function table. BINFO
7372 is part of the hierarchy dominated by T. If we're building a
7373 construction vtable, the ORIG_BINFO is the binfo we should use to
7374 find the actual function pointers to put in the vtable - but they
7375 can be overridden on the path to most-derived in the graph that
7376 ORIG_BINFO belongs. Otherwise,
7377 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7378 BINFO that should be indicated by the RTTI information in the
7379 vtable; it will be a base class of T, rather than T itself, if we
7380 are building a construction vtable.
7382 The value returned is a TREE_LIST suitable for wrapping in a
7383 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7384 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7385 number of non-function entries in the vtable.
7387 It might seem that this function should never be called with a
7388 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7389 base is always subsumed by a derived class vtable. However, when
7390 we are building construction vtables, we do build vtables for
7391 primary bases; we need these while the primary base is being
7392 constructed. */
7394 static tree
7395 build_vtbl_initializer (tree binfo,
7396 tree orig_binfo,
7397 tree t,
7398 tree rtti_binfo,
7399 int* non_fn_entries_p)
7401 tree v, b;
7402 tree vfun_inits;
7403 tree vbase;
7404 vtbl_init_data vid;
7406 /* Initialize VID. */
7407 memset (&vid, 0, sizeof (vid));
7408 vid.binfo = binfo;
7409 vid.derived = t;
7410 vid.rtti_binfo = rtti_binfo;
7411 vid.last_init = &vid.inits;
7412 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7413 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7414 vid.generate_vcall_entries = true;
7415 /* The first vbase or vcall offset is at index -3 in the vtable. */
7416 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7418 /* Add entries to the vtable for RTTI. */
7419 build_rtti_vtbl_entries (binfo, &vid);
7421 /* Create an array for keeping track of the functions we've
7422 processed. When we see multiple functions with the same
7423 signature, we share the vcall offsets. */
7424 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7425 /* Add the vcall and vbase offset entries. */
7426 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7427 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7428 build_vbase_offset_vtbl_entries. */
7429 for (vbase = CLASSTYPE_VBASECLASSES (t);
7430 vbase;
7431 vbase = TREE_CHAIN (vbase))
7432 BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase)) = 0;
7434 /* If the target requires padding between data entries, add that now. */
7435 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7437 tree cur, *prev;
7439 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7441 tree add = cur;
7442 int i;
7444 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7445 add = tree_cons (NULL_TREE,
7446 build1 (NOP_EXPR, vtable_entry_type,
7447 null_pointer_node),
7448 add);
7449 *prev = add;
7453 if (non_fn_entries_p)
7454 *non_fn_entries_p = list_length (vid.inits);
7456 /* Go through all the ordinary virtual functions, building up
7457 initializers. */
7458 vfun_inits = NULL_TREE;
7459 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7461 tree delta;
7462 tree vcall_index;
7463 tree fn, fn_original;
7464 tree init = NULL_TREE;
7466 fn = BV_FN (v);
7467 fn_original = fn;
7468 if (DECL_THUNK_P (fn))
7470 if (!DECL_NAME (fn))
7471 finish_thunk (fn);
7472 fn_original = THUNK_TARGET (fn);
7475 /* If the only definition of this function signature along our
7476 primary base chain is from a lost primary, this vtable slot will
7477 never be used, so just zero it out. This is important to avoid
7478 requiring extra thunks which cannot be generated with the function.
7480 We first check this in update_vtable_entry_for_fn, so we handle
7481 restored primary bases properly; we also need to do it here so we
7482 zero out unused slots in ctor vtables, rather than filling themff
7483 with erroneous values (though harmless, apart from relocation
7484 costs). */
7485 for (b = binfo; ; b = get_primary_binfo (b))
7487 /* We found a defn before a lost primary; go ahead as normal. */
7488 if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7489 break;
7491 /* The nearest definition is from a lost primary; clear the
7492 slot. */
7493 if (BINFO_LOST_PRIMARY_P (b))
7495 init = size_zero_node;
7496 break;
7500 if (! init)
7502 /* Pull the offset for `this', and the function to call, out of
7503 the list. */
7504 delta = BV_DELTA (v);
7505 vcall_index = BV_VCALL_INDEX (v);
7507 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7508 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7510 /* You can't call an abstract virtual function; it's abstract.
7511 So, we replace these functions with __pure_virtual. */
7512 if (DECL_PURE_VIRTUAL_P (fn_original))
7513 fn = abort_fndecl;
7514 else if (!integer_zerop (delta) || vcall_index)
7516 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7517 if (!DECL_NAME (fn))
7518 finish_thunk (fn);
7520 /* Take the address of the function, considering it to be of an
7521 appropriate generic type. */
7522 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7523 /* The address of a function can't change. */
7524 TREE_CONSTANT (init) = 1;
7527 /* And add it to the chain of initializers. */
7528 if (TARGET_VTABLE_USES_DESCRIPTORS)
7530 int i;
7531 if (init == size_zero_node)
7532 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7533 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7534 else
7535 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7537 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7538 TREE_OPERAND (init, 0),
7539 build_int_2 (i, 0));
7540 TREE_CONSTANT (fdesc) = 1;
7542 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7545 else
7546 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7549 /* The initializers for virtual functions were built up in reverse
7550 order; straighten them out now. */
7551 vfun_inits = nreverse (vfun_inits);
7553 /* The negative offset initializers are also in reverse order. */
7554 vid.inits = nreverse (vid.inits);
7556 /* Chain the two together. */
7557 return chainon (vid.inits, vfun_inits);
7560 /* Adds to vid->inits the initializers for the vbase and vcall
7561 offsets in BINFO, which is in the hierarchy dominated by T. */
7563 static void
7564 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7566 tree b;
7568 /* If this is a derived class, we must first create entries
7569 corresponding to the primary base class. */
7570 b = get_primary_binfo (binfo);
7571 if (b)
7572 build_vcall_and_vbase_vtbl_entries (b, vid);
7574 /* Add the vbase entries for this base. */
7575 build_vbase_offset_vtbl_entries (binfo, vid);
7576 /* Add the vcall entries for this base. */
7577 build_vcall_offset_vtbl_entries (binfo, vid);
7580 /* Returns the initializers for the vbase offset entries in the vtable
7581 for BINFO (which is part of the class hierarchy dominated by T), in
7582 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7583 where the next vbase offset will go. */
7585 static void
7586 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7588 tree vbase;
7589 tree t;
7590 tree non_primary_binfo;
7592 /* If there are no virtual baseclasses, then there is nothing to
7593 do. */
7594 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7595 return;
7597 t = vid->derived;
7599 /* We might be a primary base class. Go up the inheritance hierarchy
7600 until we find the most derived class of which we are a primary base:
7601 it is the offset of that which we need to use. */
7602 non_primary_binfo = binfo;
7603 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7605 tree b;
7607 /* If we have reached a virtual base, then it must be a primary
7608 base (possibly multi-level) of vid->binfo, or we wouldn't
7609 have called build_vcall_and_vbase_vtbl_entries for it. But it
7610 might be a lost primary, so just skip down to vid->binfo. */
7611 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7613 non_primary_binfo = vid->binfo;
7614 break;
7617 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7618 if (get_primary_binfo (b) != non_primary_binfo)
7619 break;
7620 non_primary_binfo = b;
7623 /* Go through the virtual bases, adding the offsets. */
7624 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7625 vbase;
7626 vbase = TREE_CHAIN (vbase))
7628 tree b;
7629 tree delta;
7631 if (!TREE_VIA_VIRTUAL (vbase))
7632 continue;
7634 /* Find the instance of this virtual base in the complete
7635 object. */
7636 b = copied_binfo (vbase, binfo);
7638 /* If we've already got an offset for this virtual base, we
7639 don't need another one. */
7640 if (BINFO_VTABLE_PATH_MARKED (b))
7641 continue;
7642 BINFO_VTABLE_PATH_MARKED (b) = 1;
7644 /* Figure out where we can find this vbase offset. */
7645 delta = size_binop (MULT_EXPR,
7646 vid->index,
7647 convert (ssizetype,
7648 TYPE_SIZE_UNIT (vtable_entry_type)));
7649 if (vid->primary_vtbl_p)
7650 BINFO_VPTR_FIELD (b) = delta;
7652 if (binfo != TYPE_BINFO (t))
7654 /* The vbase offset had better be the same. */
7655 my_friendly_assert (tree_int_cst_equal (delta,
7656 BINFO_VPTR_FIELD (vbase)),
7657 20030202);
7660 /* The next vbase will come at a more negative offset. */
7661 vid->index = size_binop (MINUS_EXPR, vid->index,
7662 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7664 /* The initializer is the delta from BINFO to this virtual base.
7665 The vbase offsets go in reverse inheritance-graph order, and
7666 we are walking in inheritance graph order so these end up in
7667 the right order. */
7668 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7670 *vid->last_init
7671 = build_tree_list (NULL_TREE,
7672 fold (build1 (NOP_EXPR,
7673 vtable_entry_type,
7674 delta)));
7675 vid->last_init = &TREE_CHAIN (*vid->last_init);
7679 /* Adds the initializers for the vcall offset entries in the vtable
7680 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7681 to VID->INITS. */
7683 static void
7684 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7686 /* We only need these entries if this base is a virtual base. We
7687 compute the indices -- but do not add to the vtable -- when
7688 building the main vtable for a class. */
7689 if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7691 /* We need a vcall offset for each of the virtual functions in this
7692 vtable. For example:
7694 class A { virtual void f (); };
7695 class B1 : virtual public A { virtual void f (); };
7696 class B2 : virtual public A { virtual void f (); };
7697 class C: public B1, public B2 { virtual void f (); };
7699 A C object has a primary base of B1, which has a primary base of A. A
7700 C also has a secondary base of B2, which no longer has a primary base
7701 of A. So the B2-in-C construction vtable needs a secondary vtable for
7702 A, which will adjust the A* to a B2* to call f. We have no way of
7703 knowing what (or even whether) this offset will be when we define B2,
7704 so we store this "vcall offset" in the A sub-vtable and look it up in
7705 a "virtual thunk" for B2::f.
7707 We need entries for all the functions in our primary vtable and
7708 in our non-virtual bases' secondary vtables. */
7709 vid->vbase = binfo;
7710 /* If we are just computing the vcall indices -- but do not need
7711 the actual entries -- not that. */
7712 if (!TREE_VIA_VIRTUAL (binfo))
7713 vid->generate_vcall_entries = false;
7714 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7715 add_vcall_offset_vtbl_entries_r (binfo, vid);
7719 /* Build vcall offsets, starting with those for BINFO. */
7721 static void
7722 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
7724 int i;
7725 tree primary_binfo;
7727 /* Don't walk into virtual bases -- except, of course, for the
7728 virtual base for which we are building vcall offsets. Any
7729 primary virtual base will have already had its offsets generated
7730 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7731 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7732 return;
7734 /* If BINFO has a primary base, process it first. */
7735 primary_binfo = get_primary_binfo (binfo);
7736 if (primary_binfo)
7737 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7739 /* Add BINFO itself to the list. */
7740 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7742 /* Scan the non-primary bases of BINFO. */
7743 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7745 tree base_binfo;
7747 base_binfo = BINFO_BASETYPE (binfo, i);
7748 if (base_binfo != primary_binfo)
7749 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7753 /* Called from build_vcall_offset_vtbl_entries_r. */
7755 static void
7756 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
7758 /* Make entries for the rest of the virtuals. */
7759 if (abi_version_at_least (2))
7761 tree orig_fn;
7763 /* The ABI requires that the methods be processed in declaration
7764 order. G++ 3.2 used the order in the vtable. */
7765 for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
7766 orig_fn;
7767 orig_fn = TREE_CHAIN (orig_fn))
7768 if (DECL_VINDEX (orig_fn))
7769 add_vcall_offset (orig_fn, binfo, vid);
7771 else
7773 tree derived_virtuals;
7774 tree base_virtuals;
7775 tree orig_virtuals;
7776 /* If BINFO is a primary base, the most derived class which has
7777 BINFO as a primary base; otherwise, just BINFO. */
7778 tree non_primary_binfo;
7780 /* We might be a primary base class. Go up the inheritance hierarchy
7781 until we find the most derived class of which we are a primary base:
7782 it is the BINFO_VIRTUALS there that we need to consider. */
7783 non_primary_binfo = binfo;
7784 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7786 tree b;
7788 /* If we have reached a virtual base, then it must be vid->vbase,
7789 because we ignore other virtual bases in
7790 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7791 base (possibly multi-level) of vid->binfo, or we wouldn't
7792 have called build_vcall_and_vbase_vtbl_entries for it. But it
7793 might be a lost primary, so just skip down to vid->binfo. */
7794 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7796 if (non_primary_binfo != vid->vbase)
7797 abort ();
7798 non_primary_binfo = vid->binfo;
7799 break;
7802 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7803 if (get_primary_binfo (b) != non_primary_binfo)
7804 break;
7805 non_primary_binfo = b;
7808 if (vid->ctor_vtbl_p)
7809 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7810 where rtti_binfo is the most derived type. */
7811 non_primary_binfo
7812 = original_binfo (non_primary_binfo, vid->rtti_binfo);
7814 for (base_virtuals = BINFO_VIRTUALS (binfo),
7815 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7816 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7817 base_virtuals;
7818 base_virtuals = TREE_CHAIN (base_virtuals),
7819 derived_virtuals = TREE_CHAIN (derived_virtuals),
7820 orig_virtuals = TREE_CHAIN (orig_virtuals))
7822 tree orig_fn;
7824 /* Find the declaration that originally caused this function to
7825 be present in BINFO_TYPE (binfo). */
7826 orig_fn = BV_FN (orig_virtuals);
7828 /* When processing BINFO, we only want to generate vcall slots for
7829 function slots introduced in BINFO. So don't try to generate
7830 one if the function isn't even defined in BINFO. */
7831 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7832 continue;
7834 add_vcall_offset (orig_fn, binfo, vid);
7839 /* Add a vcall offset entry for ORIG_FN to the vtable. */
7841 static void
7842 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
7844 size_t i;
7845 tree vcall_offset;
7847 /* If there is already an entry for a function with the same
7848 signature as FN, then we do not need a second vcall offset.
7849 Check the list of functions already present in the derived
7850 class vtable. */
7851 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7853 tree derived_entry;
7855 derived_entry = VARRAY_TREE (vid->fns, i);
7856 if (same_signature_p (derived_entry, orig_fn)
7857 /* We only use one vcall offset for virtual destructors,
7858 even though there are two virtual table entries. */
7859 || (DECL_DESTRUCTOR_P (derived_entry)
7860 && DECL_DESTRUCTOR_P (orig_fn)))
7861 return;
7864 /* If we are building these vcall offsets as part of building
7865 the vtable for the most derived class, remember the vcall
7866 offset. */
7867 if (vid->binfo == TYPE_BINFO (vid->derived))
7868 CLASSTYPE_VCALL_INDICES (vid->derived)
7869 = tree_cons (orig_fn, vid->index,
7870 CLASSTYPE_VCALL_INDICES (vid->derived));
7872 /* The next vcall offset will be found at a more negative
7873 offset. */
7874 vid->index = size_binop (MINUS_EXPR, vid->index,
7875 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7877 /* Keep track of this function. */
7878 VARRAY_PUSH_TREE (vid->fns, orig_fn);
7880 if (vid->generate_vcall_entries)
7882 tree base;
7883 tree fn;
7885 /* Find the overriding function. */
7886 fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
7887 if (fn == error_mark_node)
7888 vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
7889 integer_zero_node);
7890 else
7892 base = TREE_VALUE (fn);
7894 /* The vbase we're working on is a primary base of
7895 vid->binfo. But it might be a lost primary, so its
7896 BINFO_OFFSET might be wrong, so we just use the
7897 BINFO_OFFSET from vid->binfo. */
7898 vcall_offset = size_diffop (BINFO_OFFSET (base),
7899 BINFO_OFFSET (vid->binfo));
7900 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7901 vcall_offset));
7903 /* Add the intiailizer to the vtable. */
7904 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7905 vid->last_init = &TREE_CHAIN (*vid->last_init);
7909 /* Return vtbl initializers for the RTTI entries coresponding to the
7910 BINFO's vtable. The RTTI entries should indicate the object given
7911 by VID->rtti_binfo. */
7913 static void
7914 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
7916 tree b;
7917 tree t;
7918 tree basetype;
7919 tree offset;
7920 tree decl;
7921 tree init;
7923 basetype = BINFO_TYPE (binfo);
7924 t = BINFO_TYPE (vid->rtti_binfo);
7926 /* To find the complete object, we will first convert to our most
7927 primary base, and then add the offset in the vtbl to that value. */
7928 b = binfo;
7929 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
7930 && !BINFO_LOST_PRIMARY_P (b))
7932 tree primary_base;
7934 primary_base = get_primary_binfo (b);
7935 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
7936 b = primary_base;
7938 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
7940 /* The second entry is the address of the typeinfo object. */
7941 if (flag_rtti)
7942 decl = build_address (get_tinfo_decl (t));
7943 else
7944 decl = integer_zero_node;
7946 /* Convert the declaration to a type that can be stored in the
7947 vtable. */
7948 init = build_nop (vfunc_ptr_type_node, decl);
7949 *vid->last_init = build_tree_list (NULL_TREE, init);
7950 vid->last_init = &TREE_CHAIN (*vid->last_init);
7952 /* Add the offset-to-top entry. It comes earlier in the vtable that
7953 the the typeinfo entry. Convert the offset to look like a
7954 function pointer, so that we can put it in the vtable. */
7955 init = build_nop (vfunc_ptr_type_node, offset);
7956 *vid->last_init = build_tree_list (NULL_TREE, init);
7957 vid->last_init = &TREE_CHAIN (*vid->last_init);