2002-11-07 Jason Thorpe <thorpej@wasabisystems.com>
[official-gcc.git] / gcc / cp / class.c
blob0dffc62ea695443f51d574cb29c01a67f74780cf
1 /* Functions related to building classes and their related objects.
2 Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
4 Contributed by Michael Tiemann (tiemann@cygnus.com)
6 This file is part of GNU CC.
8 GNU CC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU CC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU CC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
24 /* High-level class interface. */
26 #include "config.h"
27 #include "system.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "flags.h"
31 #include "rtl.h"
32 #include "output.h"
33 #include "toplev.h"
34 #include "ggc.h"
35 #include "lex.h"
36 #include "target.h"
38 /* The number of nested classes being processed. If we are not in the
39 scope of any class, this is zero. */
41 int current_class_depth;
43 /* In order to deal with nested classes, we keep a stack of classes.
44 The topmost entry is the innermost class, and is the entry at index
45 CURRENT_CLASS_DEPTH */
47 typedef struct class_stack_node {
48 /* The name of the class. */
49 tree name;
51 /* The _TYPE node for the class. */
52 tree type;
54 /* The access specifier pending for new declarations in the scope of
55 this class. */
56 tree access;
58 /* If were defining TYPE, the names used in this class. */
59 splay_tree names_used;
60 }* class_stack_node_t;
62 typedef struct vtbl_init_data_s
64 /* The base for which we're building initializers. */
65 tree binfo;
66 /* The type of the most-derived type. */
67 tree derived;
68 /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
69 unless ctor_vtbl_p is true. */
70 tree rtti_binfo;
71 /* The negative-index vtable initializers built up so far. These
72 are in order from least negative index to most negative index. */
73 tree inits;
74 /* The last (i.e., most negative) entry in INITS. */
75 tree* last_init;
76 /* The binfo for the virtual base for which we're building
77 vcall offset initializers. */
78 tree vbase;
79 /* The functions in vbase for which we have already provided vcall
80 offsets. */
81 varray_type fns;
82 /* The vtable index of the next vcall or vbase offset. */
83 tree index;
84 /* Nonzero if we are building the initializer for the primary
85 vtable. */
86 int primary_vtbl_p;
87 /* Nonzero if we are building the initializer for a construction
88 vtable. */
89 int ctor_vtbl_p;
90 /* True when adding vcall offset entries to the vtable. False when
91 merely computing the indices. */
92 bool generate_vcall_entries;
93 } vtbl_init_data;
95 /* The type of a function passed to walk_subobject_offsets. */
96 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
98 /* The stack itself. This is an dynamically resized array. The
99 number of elements allocated is CURRENT_CLASS_STACK_SIZE. */
100 static int current_class_stack_size;
101 static class_stack_node_t current_class_stack;
103 /* An array of all local classes present in this translation unit, in
104 declaration order. */
105 varray_type local_classes;
107 static tree get_vfield_name PARAMS ((tree));
108 static void finish_struct_anon PARAMS ((tree));
109 static tree get_vtable_name PARAMS ((tree));
110 static tree get_basefndecls PARAMS ((tree, tree));
111 static int build_primary_vtable PARAMS ((tree, tree));
112 static int build_secondary_vtable PARAMS ((tree, tree));
113 static void finish_vtbls PARAMS ((tree));
114 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
115 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
116 static void delete_duplicate_fields PARAMS ((tree));
117 static void finish_struct_bits PARAMS ((tree));
118 static int alter_access PARAMS ((tree, tree, tree));
119 static void handle_using_decl PARAMS ((tree, tree));
120 static void check_for_override PARAMS ((tree, tree));
121 static tree dfs_modify_vtables PARAMS ((tree, void *));
122 static tree modify_all_vtables PARAMS ((tree, tree));
123 static void determine_primary_base PARAMS ((tree));
124 static void finish_struct_methods PARAMS ((tree));
125 static void maybe_warn_about_overly_private_class PARAMS ((tree));
126 static int field_decl_cmp PARAMS ((const tree *, const tree *));
127 static int method_name_cmp PARAMS ((const tree *, const tree *));
128 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
129 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
130 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
131 int, int, tree));
132 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
133 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
134 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
135 static int count_fields PARAMS ((tree));
136 static int add_fields_to_vec PARAMS ((tree, tree, int));
137 static void check_bitfield_decl PARAMS ((tree));
138 static void check_field_decl (tree, tree, int *, int *, int *, int *);
139 static void check_field_decls (tree, tree *, int *, int *, int *);
140 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
141 static void build_base_fields (record_layout_info, splay_tree, tree *);
142 static void check_methods PARAMS ((tree));
143 static void remove_zero_width_bit_fields PARAMS ((tree));
144 static void check_bases PARAMS ((tree, int *, int *, int *));
145 static void check_bases_and_members (tree);
146 static tree create_vtable_ptr (tree, tree *);
147 static void include_empty_classes (record_layout_info);
148 static void layout_class_type (tree, tree *);
149 static void fixup_pending_inline PARAMS ((tree));
150 static void fixup_inline_methods PARAMS ((tree));
151 static void set_primary_base PARAMS ((tree, tree));
152 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
153 static void layout_virtual_bases (record_layout_info, splay_tree);
154 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
155 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
156 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
157 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
158 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
159 static void layout_vtable_decl PARAMS ((tree, int));
160 static tree dfs_find_final_overrider PARAMS ((tree, void *));
161 static tree find_final_overrider PARAMS ((tree, tree, tree));
162 static int make_new_vtable PARAMS ((tree, tree));
163 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
164 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
165 static void dump_class_hierarchy PARAMS ((tree));
166 static void dump_array PARAMS ((FILE *, tree));
167 static void dump_vtable PARAMS ((tree, tree, tree));
168 static void dump_vtt PARAMS ((tree, tree));
169 static tree build_vtable PARAMS ((tree, tree, tree));
170 static void initialize_vtable PARAMS ((tree, tree));
171 static void initialize_array PARAMS ((tree, tree));
172 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
173 tree, tree, splay_tree));
174 static tree end_of_class PARAMS ((tree, int));
175 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
176 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
177 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
178 tree));
179 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
180 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
181 vtbl_init_data *));
182 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
183 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
184 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
185 static void mark_primary_bases PARAMS ((tree));
186 static tree mark_primary_virtual_base PARAMS ((tree, tree));
187 static void clone_constructors_and_destructors PARAMS ((tree));
188 static tree build_clone PARAMS ((tree, tree));
189 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
190 static tree copy_virtuals PARAMS ((tree));
191 static void build_ctor_vtbl_group PARAMS ((tree, tree));
192 static void build_vtt PARAMS ((tree));
193 static tree binfo_ctor_vtable PARAMS ((tree));
194 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
195 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
196 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
197 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
198 static tree get_original_base PARAMS ((tree, tree));
199 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
200 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
201 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
202 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
203 tree, splay_tree, tree, int));
204 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
205 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
206 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
207 splay_tree_key k2));
208 static void warn_about_ambiguous_bases PARAMS ((tree));
209 static bool type_requires_array_cookie PARAMS ((tree));
210 static bool contains_empty_class_p (tree);
211 static tree dfs_base_derived_from (tree, void *);
212 static bool base_derived_from (tree, tree);
213 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
214 static tree end_of_base (tree);
215 static tree get_vcall_index (tree, tree);
217 /* Macros for dfs walking during vtt construction. See
218 dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
219 and dfs_fixup_binfo_vtbls. */
220 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
221 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
223 /* Variables shared between class.c and call.c. */
225 #ifdef GATHER_STATISTICS
226 int n_vtables = 0;
227 int n_vtable_entries = 0;
228 int n_vtable_searches = 0;
229 int n_vtable_elems = 0;
230 int n_convert_harshness = 0;
231 int n_compute_conversion_costs = 0;
232 int n_build_method_call = 0;
233 int n_inner_fields_searched = 0;
234 #endif
236 /* Convert to or from a base subobject. EXPR is an expression of type
237 `A' or `A*', an expression of type `B' or `B*' is returned. To
238 convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
239 the B base instance within A. To convert base A to derived B, CODE
240 is MINUS_EXPR and BINFO is the binfo for the A instance within B.
241 In this latter case, A must not be a morally virtual base of B.
242 NONNULL is true if EXPR is known to be non-NULL (this is only
243 needed when EXPR is of pointer type). CV qualifiers are preserved
244 from EXPR. */
246 tree
247 build_base_path (code, expr, binfo, nonnull)
248 enum tree_code code;
249 tree expr;
250 tree binfo;
251 int nonnull;
253 tree v_binfo = NULL_TREE;
254 tree d_binfo = NULL_TREE;
255 tree probe;
256 tree offset;
257 tree target_type;
258 tree null_test = NULL;
259 tree ptr_target_type;
260 int fixed_type_p;
261 int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
263 if (expr == error_mark_node || binfo == error_mark_node || !binfo)
264 return error_mark_node;
266 for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
268 d_binfo = probe;
269 if (!v_binfo && TREE_VIA_VIRTUAL (probe))
270 v_binfo = probe;
273 probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
274 if (want_pointer)
275 probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
277 my_friendly_assert (code == MINUS_EXPR
278 ? same_type_p (BINFO_TYPE (binfo), probe)
279 : code == PLUS_EXPR
280 ? same_type_p (BINFO_TYPE (d_binfo), probe)
281 : false, 20010723);
283 if (code == MINUS_EXPR && v_binfo)
285 error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
286 BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
287 return error_mark_node;
290 fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
291 if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
292 expr = save_expr (expr);
294 if (!want_pointer)
295 expr = build_unary_op (ADDR_EXPR, expr, 0);
296 else if (!nonnull)
297 null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
299 offset = BINFO_OFFSET (binfo);
301 if (v_binfo && fixed_type_p <= 0)
303 /* Going via virtual base V_BINFO. We need the static offset
304 from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
305 V_BINFO. That offset is an entry in D_BINFO's vtable. */
306 tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
307 TREE_TYPE (TREE_TYPE (expr)));
309 v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
311 v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
312 v_offset, BINFO_VPTR_FIELD (v_binfo));
313 v_offset = build1 (NOP_EXPR,
314 build_pointer_type (ptrdiff_type_node),
315 v_offset);
316 v_offset = build_indirect_ref (v_offset, NULL);
317 TREE_CONSTANT (v_offset) = 1;
319 offset = cp_convert (ptrdiff_type_node,
320 size_diffop (offset, BINFO_OFFSET (v_binfo)));
322 if (!integer_zerop (offset))
323 v_offset = build (code, ptrdiff_type_node, v_offset, offset);
325 if (fixed_type_p < 0)
326 /* Negative fixed_type_p means this is a constructor or destructor;
327 virtual base layout is fixed in in-charge [cd]tors, but not in
328 base [cd]tors. */
329 offset = build (COND_EXPR, ptrdiff_type_node,
330 build (EQ_EXPR, boolean_type_node,
331 current_in_charge_parm, integer_zero_node),
332 v_offset,
333 BINFO_OFFSET (binfo));
334 else
335 offset = v_offset;
338 target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
340 target_type = cp_build_qualified_type
341 (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
342 ptr_target_type = build_pointer_type (target_type);
343 if (want_pointer)
344 target_type = ptr_target_type;
346 expr = build1 (NOP_EXPR, ptr_target_type, expr);
348 if (!integer_zerop (offset))
349 expr = build (code, ptr_target_type, expr, offset);
350 else
351 null_test = NULL;
353 if (!want_pointer)
354 expr = build_indirect_ref (expr, NULL);
356 if (null_test)
357 expr = build (COND_EXPR, target_type, null_test,
358 build1 (NOP_EXPR, target_type, integer_zero_node),
359 expr);
361 return expr;
364 /* Convert OBJECT to the base TYPE. If CHECK_ACCESS is true, an error
365 message is emitted if TYPE is inaccessible. OBJECT is assumed to
366 be non-NULL. */
368 tree
369 convert_to_base (tree object, tree type, bool check_access)
371 tree binfo;
373 binfo = lookup_base (TREE_TYPE (object), type,
374 check_access ? ba_check : ba_ignore,
375 NULL);
376 if (!binfo || binfo == error_mark_node)
377 return error_mark_node;
379 return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
383 /* Virtual function things. */
385 static tree
386 build_vtable_entry_ref (array_ref, instance, idx)
387 tree array_ref, instance, idx;
389 tree i, i2, vtable, first_fn, basetype;
391 basetype = TREE_TYPE (instance);
392 if (TREE_CODE (basetype) == REFERENCE_TYPE)
393 basetype = TREE_TYPE (basetype);
395 vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
396 first_fn = TYPE_BINFO_VTABLE (basetype);
398 i = fold (build_array_ref (first_fn, idx));
399 i = fold (build_c_cast (ptrdiff_type_node,
400 build_unary_op (ADDR_EXPR, i, 0)));
401 i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
402 i2 = fold (build_c_cast (ptrdiff_type_node,
403 build_unary_op (ADDR_EXPR, i2, 0)));
404 i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
406 if (TREE_CODE (i) != INTEGER_CST)
407 abort ();
409 return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
412 /* Given an object INSTANCE, return an expression which yields the
413 vtable element corresponding to INDEX. There are many special
414 cases for INSTANCE which we take care of here, mainly to avoid
415 creating extra tree nodes when we don't have to. */
417 static tree
418 build_vtbl_ref_1 (instance, idx)
419 tree instance, idx;
421 tree aref;
422 tree vtbl = NULL_TREE;
424 /* Try to figure out what a reference refers to, and
425 access its virtual function table directly. */
427 int cdtorp = 0;
428 tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
430 tree basetype = TREE_TYPE (instance);
431 if (TREE_CODE (basetype) == REFERENCE_TYPE)
432 basetype = TREE_TYPE (basetype);
434 if (fixed_type && !cdtorp)
436 tree binfo = lookup_base (fixed_type, basetype,
437 ba_ignore|ba_quiet, NULL);
438 if (binfo)
439 vtbl = BINFO_VTABLE (binfo);
442 if (!vtbl)
444 vtbl = build_vfield_ref (instance, basetype);
447 assemble_external (vtbl);
449 aref = build_array_ref (vtbl, idx);
450 TREE_CONSTANT (aref) = 1;
452 return aref;
455 tree
456 build_vtbl_ref (instance, idx)
457 tree instance, idx;
459 tree aref = build_vtbl_ref_1 (instance, idx);
461 if (flag_vtable_gc)
462 aref = build_vtable_entry_ref (aref, instance, idx);
464 return aref;
467 /* Given an object INSTANCE, return an expression which yields a
468 function pointer corresponding to vtable element INDEX. */
470 tree
471 build_vfn_ref (instance, idx)
472 tree instance, idx;
474 tree aref = build_vtbl_ref_1 (instance, idx);
476 /* When using function descriptors, the address of the
477 vtable entry is treated as a function pointer. */
478 if (TARGET_VTABLE_USES_DESCRIPTORS)
479 aref = build1 (NOP_EXPR, TREE_TYPE (aref),
480 build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
482 if (flag_vtable_gc)
483 aref = build_vtable_entry_ref (aref, instance, idx);
485 return aref;
488 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
489 for the given TYPE. */
491 static tree
492 get_vtable_name (type)
493 tree type;
495 return mangle_vtbl_for_type (type);
498 /* Return an IDENTIFIER_NODE for the name of the virtual table table
499 for TYPE. */
501 tree
502 get_vtt_name (type)
503 tree type;
505 return mangle_vtt_for_type (type);
508 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
509 (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
510 Use NAME for the name of the vtable, and VTABLE_TYPE for its type. */
512 static tree
513 build_vtable (class_type, name, vtable_type)
514 tree class_type;
515 tree name;
516 tree vtable_type;
518 tree decl;
520 decl = build_lang_decl (VAR_DECL, name, vtable_type);
521 /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
522 now to avoid confusion in mangle_decl. */
523 SET_DECL_ASSEMBLER_NAME (decl, name);
524 DECL_CONTEXT (decl) = class_type;
525 DECL_ARTIFICIAL (decl) = 1;
526 TREE_STATIC (decl) = 1;
527 TREE_READONLY (decl) = 1;
528 DECL_VIRTUAL_P (decl) = 1;
529 DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
531 import_export_vtable (decl, class_type, 0);
533 return decl;
536 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
537 or even complete. If this does not exist, create it. If COMPLETE is
538 nonzero, then complete the definition of it -- that will render it
539 impossible to actually build the vtable, but is useful to get at those
540 which are known to exist in the runtime. */
542 tree
543 get_vtable_decl (type, complete)
544 tree type;
545 int complete;
547 tree decl;
549 if (CLASSTYPE_VTABLES (type))
550 return CLASSTYPE_VTABLES (type);
552 decl = build_vtable (type, get_vtable_name (type), void_type_node);
553 CLASSTYPE_VTABLES (type) = decl;
555 /* At one time the vtable info was grabbed 2 words at a time. This
556 fails on sparc unless you have 8-byte alignment. (tiemann) */
557 DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
558 DECL_ALIGN (decl));
560 if (complete)
562 DECL_EXTERNAL (decl) = 1;
563 cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
566 return decl;
569 /* Returns a copy of the BINFO_VIRTUALS list in BINFO. The
570 BV_VCALL_INDEX for each entry is cleared. */
572 static tree
573 copy_virtuals (binfo)
574 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 (binfo, type)
594 tree binfo, type;
596 tree decl;
597 tree virtuals;
599 decl = get_vtable_decl (type, /*complete=*/0);
601 if (binfo)
603 if (BINFO_NEW_VTABLE_MARKED (binfo, type))
604 /* We have already created a vtable for this base, so there's
605 no need to do it again. */
606 return 0;
608 virtuals = copy_virtuals (binfo);
609 TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
610 DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
611 DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
613 else
615 my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
616 20000118);
617 virtuals = NULL_TREE;
620 #ifdef GATHER_STATISTICS
621 n_vtables += 1;
622 n_vtable_elems += list_length (virtuals);
623 #endif
625 /* Initialize the association list for this type, based
626 on our first approximation. */
627 TYPE_BINFO_VTABLE (type) = decl;
628 TYPE_BINFO_VIRTUALS (type) = virtuals;
629 SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
630 return 1;
633 /* Give BINFO a new virtual function table which is initialized
634 with a skeleton-copy of its original initialization. The only
635 entry that changes is the `delta' entry, so we can really
636 share a lot of structure.
638 FOR_TYPE is the most derived type which caused this table to
639 be needed.
641 Returns nonzero if we haven't met BINFO before.
643 The order in which vtables are built (by calling this function) for
644 an object must remain the same, otherwise a binary incompatibility
645 can result. */
647 static int
648 build_secondary_vtable (binfo, for_type)
649 tree binfo, for_type;
651 my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
653 if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
654 /* We already created a vtable for this base. There's no need to
655 do it again. */
656 return 0;
658 /* Remember that we've created a vtable for this BINFO, so that we
659 don't try to do so again. */
660 SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
662 /* Make fresh virtual list, so we can smash it later. */
663 BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
665 /* Secondary vtables are laid out as part of the same structure as
666 the primary vtable. */
667 BINFO_VTABLE (binfo) = NULL_TREE;
668 return 1;
671 /* Create a new vtable for BINFO which is the hierarchy dominated by
672 T. Return nonzero if we actually created a new vtable. */
674 static int
675 make_new_vtable (t, binfo)
676 tree t;
677 tree binfo;
679 if (binfo == TYPE_BINFO (t))
680 /* In this case, it is *type*'s vtable we are modifying. We start
681 with the approximation that its vtable is that of the
682 immediate base class. */
683 /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
684 since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now. */
685 return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
687 else
688 /* This is our very own copy of `basetype' to play with. Later,
689 we will fill in all the virtual functions that override the
690 virtual functions in these base classes which are not defined
691 by the current type. */
692 return build_secondary_vtable (binfo, t);
695 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
696 (which is in the hierarchy dominated by T) list FNDECL as its
697 BV_FN. DELTA is the required constant adjustment from the `this'
698 pointer where the vtable entry appears to the `this' required when
699 the function is actually called. */
701 static void
702 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
703 tree t;
704 tree binfo;
705 tree fndecl;
706 tree delta;
707 tree *virtuals;
709 tree v;
711 v = *virtuals;
713 if (fndecl != BV_FN (v)
714 || !tree_int_cst_equal (delta, BV_DELTA (v)))
716 tree base_fndecl;
718 /* We need a new vtable for BINFO. */
719 if (make_new_vtable (t, binfo))
721 /* If we really did make a new vtable, we also made a copy
722 of the BINFO_VIRTUALS list. Now, we have to find the
723 corresponding entry in that list. */
724 *virtuals = BINFO_VIRTUALS (binfo);
725 while (BV_FN (*virtuals) != BV_FN (v))
726 *virtuals = TREE_CHAIN (*virtuals);
727 v = *virtuals;
730 base_fndecl = BV_FN (v);
731 BV_DELTA (v) = delta;
732 BV_VCALL_INDEX (v) = NULL_TREE;
733 BV_FN (v) = fndecl;
738 /* Add method METHOD to class TYPE. If ERROR_P is true, we are adding
739 the method after the class has already been defined because a
740 declaration for it was seen. (Even though that is erroneous, we
741 add the method for improved error recovery.) */
743 void
744 add_method (type, method, error_p)
745 tree type;
746 tree method;
747 int error_p;
749 int using = (DECL_CONTEXT (method) != type);
750 int len;
751 int slot;
752 tree method_vec;
753 int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
754 && DECL_TEMPLATE_CONV_FN_P (method));
756 if (!CLASSTYPE_METHOD_VEC (type))
757 /* Make a new method vector. We start with 8 entries. We must
758 allocate at least two (for constructors and destructors), and
759 we're going to end up with an assignment operator at some point
760 as well.
762 We could use a TREE_LIST for now, and convert it to a TREE_VEC
763 in finish_struct, but we would probably waste more memory
764 making the links in the list than we would by over-allocating
765 the size of the vector here. Furthermore, we would complicate
766 all the code that expects this to be a vector. */
767 CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
769 method_vec = CLASSTYPE_METHOD_VEC (type);
770 len = TREE_VEC_LENGTH (method_vec);
772 /* Constructors and destructors go in special slots. */
773 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
774 slot = CLASSTYPE_CONSTRUCTOR_SLOT;
775 else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
776 slot = CLASSTYPE_DESTRUCTOR_SLOT;
777 else
779 int have_template_convs_p = 0;
781 /* See if we already have an entry with this name. */
782 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
784 tree m = TREE_VEC_ELT (method_vec, slot);
786 if (!m)
787 break;
788 m = OVL_CURRENT (m);
790 if (template_conv_p)
792 have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
793 && DECL_TEMPLATE_CONV_FN_P (m));
795 /* If we need to move things up, see if there's
796 space. */
797 if (!have_template_convs_p)
799 slot = len - 1;
800 if (TREE_VEC_ELT (method_vec, slot))
801 slot++;
803 break;
805 if (DECL_NAME (m) == DECL_NAME (method))
806 break;
809 if (slot == len)
811 /* We need a bigger method vector. */
812 int new_len;
813 tree new_vec;
815 /* In the non-error case, we are processing a class
816 definition. Double the size of the vector to give room
817 for new methods. */
818 if (!error_p)
819 new_len = 2 * len;
820 /* In the error case, the vector is already complete. We
821 don't expect many errors, and the rest of the front-end
822 will get confused if there are empty slots in the vector. */
823 else
824 new_len = len + 1;
826 new_vec = make_tree_vec (new_len);
827 memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
828 len * sizeof (tree));
829 len = new_len;
830 method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
833 if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
835 /* Type conversion operators have to come before ordinary
836 methods; add_conversions depends on this to speed up
837 looking for conversion operators. So, if necessary, we
838 slide some of the vector elements up. In theory, this
839 makes this algorithm O(N^2) but we don't expect many
840 conversion operators. */
841 if (template_conv_p)
842 slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
843 else
844 for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
846 tree fn = TREE_VEC_ELT (method_vec, slot);
848 if (!fn)
849 /* There are no more entries in the vector, so we
850 can insert the new conversion operator here. */
851 break;
853 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
854 /* We can insert the new function right at the
855 SLOTth position. */
856 break;
859 if (template_conv_p && have_template_convs_p)
860 /*OK*/;
861 else if (!TREE_VEC_ELT (method_vec, slot))
862 /* There is nothing in the Ith slot, so we can avoid
863 moving anything. */
865 else
867 /* We know the last slot in the vector is empty
868 because we know that at this point there's room
869 for a new function. */
870 memmove (&TREE_VEC_ELT (method_vec, slot + 1),
871 &TREE_VEC_ELT (method_vec, slot),
872 (len - slot - 1) * sizeof (tree));
873 TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
878 if (template_class_depth (type))
879 /* TYPE is a template class. Don't issue any errors now; wait
880 until instantiation time to complain. */
882 else
884 tree fns;
886 /* Check to see if we've already got this method. */
887 for (fns = TREE_VEC_ELT (method_vec, slot);
888 fns;
889 fns = OVL_NEXT (fns))
891 tree fn = OVL_CURRENT (fns);
892 tree parms1;
893 tree parms2;
894 bool same = 1;
896 if (TREE_CODE (fn) != TREE_CODE (method))
897 continue;
899 /* [over.load] Member function declarations with the
900 same name and the same parameter types cannot be
901 overloaded if any of them is a static member
902 function declaration.
904 [namespace.udecl] When a using-declaration brings names
905 from a base class into a derived class scope, member
906 functions in the derived class override and/or hide member
907 functions with the same name and parameter types in a base
908 class (rather than conflicting). */
909 parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
910 parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
912 /* Compare the quals on the 'this' parm. Don't compare
913 the whole types, as used functions are treated as
914 coming from the using class in overload resolution. */
915 if (! DECL_STATIC_FUNCTION_P (fn)
916 && ! DECL_STATIC_FUNCTION_P (method)
917 && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
918 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
919 same = 0;
921 /* For templates, the template parms must be identical. */
922 if (TREE_CODE (fn) == TEMPLATE_DECL
923 && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
924 DECL_TEMPLATE_PARMS (method)))
925 same = 0;
927 if (! DECL_STATIC_FUNCTION_P (fn))
928 parms1 = TREE_CHAIN (parms1);
929 if (! DECL_STATIC_FUNCTION_P (method))
930 parms2 = TREE_CHAIN (parms2);
932 if (same && compparms (parms1, parms2)
933 && (!DECL_CONV_FN_P (fn)
934 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
935 TREE_TYPE (TREE_TYPE (method)))))
937 if (using && DECL_CONTEXT (fn) == type)
938 /* Defer to the local function. */
939 return;
940 else
942 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
943 method, fn, method);
945 /* We don't call duplicate_decls here to merge
946 the declarations because that will confuse
947 things if the methods have inline
948 definitions. In particular, we will crash
949 while processing the definitions. */
950 return;
956 /* Actually insert the new method. */
957 TREE_VEC_ELT (method_vec, slot)
958 = build_overload (method, TREE_VEC_ELT (method_vec, slot));
960 /* Add the new binding. */
961 if (!DECL_CONSTRUCTOR_P (method)
962 && !DECL_DESTRUCTOR_P (method))
963 push_class_level_binding (DECL_NAME (method),
964 TREE_VEC_ELT (method_vec, slot));
967 /* Subroutines of finish_struct. */
969 /* Look through the list of fields for this struct, deleting
970 duplicates as we go. This must be recursive to handle
971 anonymous unions.
973 FIELD is the field which may not appear anywhere in FIELDS.
974 FIELD_PTR, if non-null, is the starting point at which
975 chained deletions may take place.
976 The value returned is the first acceptable entry found
977 in FIELDS.
979 Note that anonymous fields which are not of UNION_TYPE are
980 not duplicates, they are just anonymous fields. This happens
981 when we have unnamed bitfields, for example. */
983 static tree
984 delete_duplicate_fields_1 (field, fields)
985 tree field, fields;
987 tree x;
988 tree prev = 0;
989 if (DECL_NAME (field) == 0)
991 if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
992 return fields;
994 for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
995 fields = delete_duplicate_fields_1 (x, fields);
996 return fields;
998 else
1000 for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1002 if (DECL_NAME (x) == 0)
1004 if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1005 continue;
1006 TYPE_FIELDS (TREE_TYPE (x))
1007 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1008 if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1010 if (prev == 0)
1011 fields = TREE_CHAIN (fields);
1012 else
1013 TREE_CHAIN (prev) = TREE_CHAIN (x);
1016 else if (TREE_CODE (field) == USING_DECL)
1017 /* A using declaration is allowed to appear more than
1018 once. We'll prune these from the field list later, and
1019 handle_using_decl will complain about invalid multiple
1020 uses. */
1022 else if (DECL_NAME (field) == DECL_NAME (x))
1024 if (TREE_CODE (field) == CONST_DECL
1025 && TREE_CODE (x) == CONST_DECL)
1026 cp_error_at ("duplicate enum value `%D'", x);
1027 else if (TREE_CODE (field) == CONST_DECL
1028 || TREE_CODE (x) == CONST_DECL)
1029 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1031 else if (DECL_DECLARES_TYPE_P (field)
1032 && DECL_DECLARES_TYPE_P (x))
1034 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1035 continue;
1036 cp_error_at ("duplicate nested type `%D'", x);
1038 else if (DECL_DECLARES_TYPE_P (field)
1039 || DECL_DECLARES_TYPE_P (x))
1041 /* Hide tag decls. */
1042 if ((TREE_CODE (field) == TYPE_DECL
1043 && DECL_ARTIFICIAL (field))
1044 || (TREE_CODE (x) == TYPE_DECL
1045 && DECL_ARTIFICIAL (x)))
1046 continue;
1047 cp_error_at ("duplicate field `%D' (as type and non-type)",
1050 else
1051 cp_error_at ("duplicate member `%D'", x);
1052 if (prev == 0)
1053 fields = TREE_CHAIN (fields);
1054 else
1055 TREE_CHAIN (prev) = TREE_CHAIN (x);
1059 return fields;
1062 static void
1063 delete_duplicate_fields (fields)
1064 tree fields;
1066 tree x;
1067 for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1068 TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1071 /* Change the access of FDECL to ACCESS in T. Return 1 if change was
1072 legit, otherwise return 0. */
1074 static int
1075 alter_access (t, fdecl, access)
1076 tree t;
1077 tree fdecl;
1078 tree access;
1080 tree elem;
1082 if (!DECL_LANG_SPECIFIC (fdecl))
1083 retrofit_lang_decl (fdecl);
1085 if (DECL_DISCRIMINATOR_P (fdecl))
1086 abort ();
1088 elem = purpose_member (t, DECL_ACCESS (fdecl));
1089 if (elem)
1091 if (TREE_VALUE (elem) != access)
1093 if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1094 cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1095 else
1096 error ("conflicting access specifications for field `%s', ignored",
1097 IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1099 else
1101 /* They're changing the access to the same thing they changed
1102 it to before. That's OK. */
1106 else
1108 enforce_access (t, fdecl);
1109 DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1110 return 1;
1112 return 0;
1115 /* Process the USING_DECL, which is a member of T. */
1117 static void
1118 handle_using_decl (using_decl, t)
1119 tree using_decl;
1120 tree t;
1122 tree ctype = DECL_INITIAL (using_decl);
1123 tree name = DECL_NAME (using_decl);
1124 tree access
1125 = TREE_PRIVATE (using_decl) ? access_private_node
1126 : TREE_PROTECTED (using_decl) ? access_protected_node
1127 : access_public_node;
1128 tree fdecl, binfo;
1129 tree flist = NULL_TREE;
1130 tree old_value;
1132 binfo = lookup_base (t, ctype, ba_any, NULL);
1133 if (! binfo)
1135 error_not_base_type (t, ctype);
1136 return;
1139 if (constructor_name_p (name, ctype))
1141 cp_error_at ("`%D' names constructor", using_decl);
1142 return;
1144 if (constructor_name_p (name, t))
1146 cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1147 return;
1150 fdecl = lookup_member (binfo, name, 0, 0);
1152 if (!fdecl)
1154 cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1155 return;
1158 if (BASELINK_P (fdecl))
1159 /* Ignore base type this came from. */
1160 fdecl = BASELINK_FUNCTIONS (fdecl);
1162 old_value = IDENTIFIER_CLASS_VALUE (name);
1163 if (old_value)
1165 if (is_overloaded_fn (old_value))
1166 old_value = OVL_CURRENT (old_value);
1168 if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1169 /* OK */;
1170 else
1171 old_value = NULL_TREE;
1174 if (is_overloaded_fn (fdecl))
1175 flist = fdecl;
1177 if (! old_value)
1179 else if (is_overloaded_fn (old_value))
1181 if (flist)
1182 /* It's OK to use functions from a base when there are functions with
1183 the same name already present in the current class. */;
1184 else
1186 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1187 cp_error_at (" because of local method `%#D' with same name",
1188 OVL_CURRENT (old_value));
1189 return;
1192 else if (!DECL_ARTIFICIAL (old_value))
1194 cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1195 cp_error_at (" because of local member `%#D' with same name", old_value);
1196 return;
1199 /* Make type T see field decl FDECL with access ACCESS.*/
1200 if (flist)
1201 for (; flist; flist = OVL_NEXT (flist))
1203 add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1204 alter_access (t, OVL_CURRENT (flist), access);
1206 else
1207 alter_access (t, fdecl, access);
1210 /* Run through the base clases of T, updating
1211 CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1212 NO_CONST_ASN_REF_P. Also set flag bits in T based on properties of
1213 the bases. */
1215 static void
1216 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1217 no_const_asn_ref_p)
1218 tree t;
1219 int *cant_have_default_ctor_p;
1220 int *cant_have_const_ctor_p;
1221 int *no_const_asn_ref_p;
1223 int n_baseclasses;
1224 int i;
1225 int seen_non_virtual_nearly_empty_base_p;
1226 tree binfos;
1228 binfos = TYPE_BINFO_BASETYPES (t);
1229 n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1230 seen_non_virtual_nearly_empty_base_p = 0;
1232 /* An aggregate cannot have baseclasses. */
1233 CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1235 for (i = 0; i < n_baseclasses; ++i)
1237 tree base_binfo;
1238 tree basetype;
1240 /* Figure out what base we're looking at. */
1241 base_binfo = TREE_VEC_ELT (binfos, i);
1242 basetype = TREE_TYPE (base_binfo);
1244 /* If the type of basetype is incomplete, then we already
1245 complained about that fact (and we should have fixed it up as
1246 well). */
1247 if (!COMPLETE_TYPE_P (basetype))
1249 int j;
1250 /* The base type is of incomplete type. It is
1251 probably best to pretend that it does not
1252 exist. */
1253 if (i == n_baseclasses-1)
1254 TREE_VEC_ELT (binfos, i) = NULL_TREE;
1255 TREE_VEC_LENGTH (binfos) -= 1;
1256 n_baseclasses -= 1;
1257 for (j = i; j+1 < n_baseclasses; j++)
1258 TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1259 continue;
1262 /* Effective C++ rule 14. We only need to check TYPE_POLYMORPHIC_P
1263 here because the case of virtual functions but non-virtual
1264 dtor is handled in finish_struct_1. */
1265 if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1266 && TYPE_HAS_DESTRUCTOR (basetype))
1267 warning ("base class `%#T' has a non-virtual destructor",
1268 basetype);
1270 /* If the base class doesn't have copy constructors or
1271 assignment operators that take const references, then the
1272 derived class cannot have such a member automatically
1273 generated. */
1274 if (! TYPE_HAS_CONST_INIT_REF (basetype))
1275 *cant_have_const_ctor_p = 1;
1276 if (TYPE_HAS_ASSIGN_REF (basetype)
1277 && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1278 *no_const_asn_ref_p = 1;
1279 /* Similarly, if the base class doesn't have a default
1280 constructor, then the derived class won't have an
1281 automatically generated default constructor. */
1282 if (TYPE_HAS_CONSTRUCTOR (basetype)
1283 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1285 *cant_have_default_ctor_p = 1;
1286 if (! TYPE_HAS_CONSTRUCTOR (t))
1287 pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1288 basetype);
1291 if (TREE_VIA_VIRTUAL (base_binfo))
1292 /* A virtual base does not effect nearly emptiness. */
1294 else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1296 if (seen_non_virtual_nearly_empty_base_p)
1297 /* And if there is more than one nearly empty base, then the
1298 derived class is not nearly empty either. */
1299 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1300 else
1301 /* Remember we've seen one. */
1302 seen_non_virtual_nearly_empty_base_p = 1;
1304 else if (!is_empty_class (basetype))
1305 /* If the base class is not empty or nearly empty, then this
1306 class cannot be nearly empty. */
1307 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1309 /* A lot of properties from the bases also apply to the derived
1310 class. */
1311 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1312 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
1313 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1314 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
1315 |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1316 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1317 TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1318 TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1319 TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1320 TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1321 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
1322 |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1326 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1327 TO. Move primary information from FROM to TO, and recursively traverse
1328 into FROM's bases. The hierarchy is dominated by TYPE. MAPPINGS is an
1329 assoc list of binfos that have already been reseated. */
1331 static void
1332 force_canonical_binfo_r (to, from, type, mappings)
1333 tree to;
1334 tree from;
1335 tree type;
1336 tree mappings;
1338 int i, n_baseclasses = BINFO_N_BASETYPES (from);
1340 my_friendly_assert (to != from, 20010905);
1341 BINFO_INDIRECT_PRIMARY_P (to)
1342 = BINFO_INDIRECT_PRIMARY_P (from);
1343 BINFO_INDIRECT_PRIMARY_P (from) = 0;
1344 BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1345 BINFO_UNSHARED_MARKED (from) = 0;
1346 BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1347 BINFO_LOST_PRIMARY_P (from) = 0;
1348 if (BINFO_PRIMARY_P (from))
1350 tree primary = BINFO_PRIMARY_BASE_OF (from);
1351 tree assoc;
1353 /* We might have just moved the primary base too, see if it's on our
1354 mappings. */
1355 assoc = purpose_member (primary, mappings);
1356 if (assoc)
1357 primary = TREE_VALUE (assoc);
1358 BINFO_PRIMARY_BASE_OF (to) = primary;
1359 BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1361 my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1362 20010104);
1363 mappings = tree_cons (from, to, mappings);
1365 if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1366 && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1368 tree from_primary = get_primary_binfo (from);
1370 if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1371 force_canonical_binfo (get_primary_binfo (to), from_primary,
1372 type, mappings);
1375 for (i = 0; i != n_baseclasses; i++)
1377 tree from_binfo = BINFO_BASETYPE (from, i);
1378 tree to_binfo = BINFO_BASETYPE (to, i);
1380 if (TREE_VIA_VIRTUAL (from_binfo))
1382 if (BINFO_PRIMARY_P (from_binfo) &&
1383 purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1384 /* This base is a primary of some binfo we have already
1385 reseated. We must reseat this one too. */
1386 force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1388 else
1389 force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1393 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1394 make TO the canonical binfo, within the hierarchy dominated by TYPE.
1395 MAPPINGS is an assoc list of binfos that have already been reseated.
1396 Adjust any non-virtual bases within FROM, and also move any virtual bases
1397 which are canonical. This complication arises because selecting primary
1398 bases walks in inheritance graph order, but we don't share binfos for
1399 virtual bases, hence we can fill in the primaries for a virtual base,
1400 and then discover that a later base requires the virtual as its
1401 primary. */
1403 static void
1404 force_canonical_binfo (to, from, type, mappings)
1405 tree to;
1406 tree from;
1407 tree type;
1408 tree mappings;
1410 tree assoc = purpose_member (BINFO_TYPE (to),
1411 CLASSTYPE_VBASECLASSES (type));
1412 if (TREE_VALUE (assoc) != to)
1414 TREE_VALUE (assoc) = to;
1415 force_canonical_binfo_r (to, from, type, mappings);
1419 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1420 dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1421 otherwise (because something else has already made it primary). */
1423 static tree
1424 mark_primary_virtual_base (base_binfo, type)
1425 tree base_binfo;
1426 tree type;
1428 tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1430 if (BINFO_PRIMARY_P (shared_binfo))
1432 /* It's already allocated in the hierarchy. BINFO won't have a
1433 primary base in this hierarchy, even though the complete object
1434 BINFO is for, would do. */
1435 return NULL_TREE;
1438 /* We need to make sure that the assoc list
1439 CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1440 primary BINFO for the virtual base, as this is the one
1441 that'll really exist. */
1442 if (base_binfo != shared_binfo)
1443 force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1445 return base_binfo;
1448 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1449 base, then BINFO has no primary base in this graph. Called from
1450 mark_primary_bases. DATA is the most derived type. */
1452 static tree dfs_unshared_virtual_bases (binfo, data)
1453 tree binfo;
1454 void *data;
1456 tree t = (tree) data;
1458 if (!BINFO_UNSHARED_MARKED (binfo)
1459 && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1461 /* This morally virtual base has a primary base when it
1462 is a complete object. We need to locate the shared instance
1463 of this binfo in the type dominated by T. We duplicate the
1464 primary base information from there to here. */
1465 tree vbase;
1466 tree unshared_base;
1468 for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1469 vbase = BINFO_INHERITANCE_CHAIN (vbase))
1470 continue;
1471 unshared_base = get_original_base (binfo,
1472 binfo_for_vbase (BINFO_TYPE (vbase),
1473 t));
1474 my_friendly_assert (unshared_base != binfo, 20010612);
1475 BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1476 if (!BINFO_LOST_PRIMARY_P (binfo))
1477 BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1480 if (binfo != TYPE_BINFO (t))
1481 /* The vtable fields will have been copied when duplicating the
1482 base binfos. That information is bogus, make sure we don't try
1483 and use it. */
1484 BINFO_VTABLE (binfo) = NULL_TREE;
1486 /* If this is a virtual primary base, make sure its offset matches
1487 that which it is primary for. */
1488 if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1489 binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1491 tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1492 BINFO_OFFSET (binfo));
1493 if (!integer_zerop (delta))
1494 propagate_binfo_offsets (binfo, delta, t);
1497 BINFO_UNSHARED_MARKED (binfo) = 0;
1498 return NULL;
1501 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1502 dominated by TYPE that are primary bases. */
1504 static void
1505 mark_primary_bases (type)
1506 tree type;
1508 tree binfo;
1510 /* Walk the bases in inheritance graph order. */
1511 for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1513 tree base_binfo;
1515 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1516 /* Not a dynamic base. */
1517 continue;
1519 base_binfo = get_primary_binfo (binfo);
1521 if (TREE_VIA_VIRTUAL (base_binfo))
1522 base_binfo = mark_primary_virtual_base (base_binfo, type);
1524 if (base_binfo)
1525 BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1526 else
1527 BINFO_LOST_PRIMARY_P (binfo) = 1;
1529 BINFO_UNSHARED_MARKED (binfo) = 1;
1531 /* There could remain unshared morally virtual bases which were not
1532 visited in the inheritance graph walk. These bases will have lost
1533 their virtual primary base (should they have one). We must now
1534 find them. Also we must fix up the BINFO_OFFSETs of primary
1535 virtual bases. We could not do that as we went along, as they
1536 were originally copied from the bases we inherited from by
1537 unshare_base_binfos. That may have decided differently about
1538 where a virtual primary base went. */
1539 dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1542 /* Make the BINFO the primary base of T. */
1544 static void
1545 set_primary_base (t, binfo)
1546 tree t;
1547 tree binfo;
1549 tree basetype;
1551 CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1552 basetype = BINFO_TYPE (binfo);
1553 TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1554 TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1555 TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1558 /* Determine the primary class for T. */
1560 static void
1561 determine_primary_base (t)
1562 tree t;
1564 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1565 tree vbases;
1566 tree type_binfo;
1568 /* If there are no baseclasses, there is certainly no primary base. */
1569 if (n_baseclasses == 0)
1570 return;
1572 type_binfo = TYPE_BINFO (t);
1574 for (i = 0; i < n_baseclasses; i++)
1576 tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1577 tree basetype = BINFO_TYPE (base_binfo);
1579 if (TYPE_CONTAINS_VPTR_P (basetype))
1581 /* We prefer a non-virtual base, although a virtual one will
1582 do. */
1583 if (TREE_VIA_VIRTUAL (base_binfo))
1584 continue;
1586 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1588 set_primary_base (t, base_binfo);
1589 CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1591 else
1593 tree vfields;
1595 /* Only add unique vfields, and flatten them out as we go. */
1596 for (vfields = CLASSTYPE_VFIELDS (basetype);
1597 vfields;
1598 vfields = TREE_CHAIN (vfields))
1599 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1600 || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1601 CLASSTYPE_VFIELDS (t)
1602 = tree_cons (base_binfo,
1603 VF_BASETYPE_VALUE (vfields),
1604 CLASSTYPE_VFIELDS (t));
1609 if (!TYPE_VFIELD (t))
1610 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1612 /* Find the indirect primary bases - those virtual bases which are primary
1613 bases of something else in this hierarchy. */
1614 for (vbases = CLASSTYPE_VBASECLASSES (t);
1615 vbases;
1616 vbases = TREE_CHAIN (vbases))
1618 tree vbase_binfo = TREE_VALUE (vbases);
1620 /* See if this virtual base is an indirect primary base. To be so,
1621 it must be a primary base within the hierarchy of one of our
1622 direct bases. */
1623 for (i = 0; i < n_baseclasses; ++i)
1625 tree basetype = TYPE_BINFO_BASETYPE (t, i);
1626 tree v;
1628 for (v = CLASSTYPE_VBASECLASSES (basetype);
1630 v = TREE_CHAIN (v))
1632 tree base_vbase = TREE_VALUE (v);
1634 if (BINFO_PRIMARY_P (base_vbase)
1635 && same_type_p (BINFO_TYPE (base_vbase),
1636 BINFO_TYPE (vbase_binfo)))
1638 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1639 break;
1643 /* If we've discovered that this virtual base is an indirect
1644 primary base, then we can move on to the next virtual
1645 base. */
1646 if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1647 break;
1651 /* A "nearly-empty" virtual base class can be the primary base
1652 class, if no non-virtual polymorphic base can be found. */
1653 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1655 /* If not NULL, this is the best primary base candidate we have
1656 found so far. */
1657 tree candidate = NULL_TREE;
1658 tree base_binfo;
1660 /* Loop over the baseclasses. */
1661 for (base_binfo = TYPE_BINFO (t);
1662 base_binfo;
1663 base_binfo = TREE_CHAIN (base_binfo))
1665 tree basetype = BINFO_TYPE (base_binfo);
1667 if (TREE_VIA_VIRTUAL (base_binfo)
1668 && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1670 /* If this is not an indirect primary base, then it's
1671 definitely our primary base. */
1672 if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1674 candidate = base_binfo;
1675 break;
1678 /* If this is an indirect primary base, it still could be
1679 our primary base -- unless we later find there's another
1680 nearly-empty virtual base that isn't an indirect
1681 primary base. */
1682 if (!candidate)
1683 candidate = base_binfo;
1687 /* If we've got a primary base, use it. */
1688 if (candidate)
1690 set_primary_base (t, candidate);
1691 CLASSTYPE_VFIELDS (t)
1692 = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1696 /* Mark the primary base classes at this point. */
1697 mark_primary_bases (t);
1700 /* Set memoizing fields and bits of T (and its variants) for later
1701 use. */
1703 static void
1704 finish_struct_bits (t)
1705 tree t;
1707 int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1709 /* Fix up variants (if any). */
1710 tree variants = TYPE_NEXT_VARIANT (t);
1711 while (variants)
1713 /* These fields are in the _TYPE part of the node, not in
1714 the TYPE_LANG_SPECIFIC component, so they are not shared. */
1715 TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1716 TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1717 TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1718 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
1719 = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1721 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants)
1722 = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1723 TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1724 TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1725 /* Copy whatever these are holding today. */
1726 TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1727 TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1728 TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1729 TYPE_SIZE (variants) = TYPE_SIZE (t);
1730 TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1731 variants = TYPE_NEXT_VARIANT (variants);
1734 if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1735 /* For a class w/o baseclasses, `finish_struct' has set
1736 CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1737 definition). Similarly for a class whose base classes do not
1738 have vtables. When neither of these is true, we might have
1739 removed abstract virtuals (by providing a definition), added
1740 some (by declaring new ones), or redeclared ones from a base
1741 class. We need to recalculate what's really an abstract virtual
1742 at this point (by looking in the vtables). */
1743 get_pure_virtuals (t);
1745 if (n_baseclasses)
1747 /* Notice whether this class has type conversion functions defined. */
1748 tree binfo = TYPE_BINFO (t);
1749 tree binfos = BINFO_BASETYPES (binfo);
1750 tree basetype;
1752 for (i = n_baseclasses-1; i >= 0; i--)
1754 basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1756 TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1760 /* If this type has a copy constructor or a destructor, force its mode to
1761 be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero. This
1762 will cause it to be passed by invisible reference and prevent it from
1763 being returned in a register. */
1764 if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1766 tree variants;
1767 DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1768 for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1770 TYPE_MODE (variants) = BLKmode;
1771 TREE_ADDRESSABLE (variants) = 1;
1776 /* Issue warnings about T having private constructors, but no friends,
1777 and so forth.
1779 HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1780 static members. HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1781 non-private static member functions. */
1783 static void
1784 maybe_warn_about_overly_private_class (t)
1785 tree t;
1787 int has_member_fn = 0;
1788 int has_nonprivate_method = 0;
1789 tree fn;
1791 if (!warn_ctor_dtor_privacy
1792 /* If the class has friends, those entities might create and
1793 access instances, so we should not warn. */
1794 || (CLASSTYPE_FRIEND_CLASSES (t)
1795 || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1796 /* We will have warned when the template was declared; there's
1797 no need to warn on every instantiation. */
1798 || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1799 /* There's no reason to even consider warning about this
1800 class. */
1801 return;
1803 /* We only issue one warning, if more than one applies, because
1804 otherwise, on code like:
1806 class A {
1807 // Oops - forgot `public:'
1808 A();
1809 A(const A&);
1810 ~A();
1813 we warn several times about essentially the same problem. */
1815 /* Check to see if all (non-constructor, non-destructor) member
1816 functions are private. (Since there are no friends or
1817 non-private statics, we can't ever call any of the private member
1818 functions.) */
1819 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1820 /* We're not interested in compiler-generated methods; they don't
1821 provide any way to call private members. */
1822 if (!DECL_ARTIFICIAL (fn))
1824 if (!TREE_PRIVATE (fn))
1826 if (DECL_STATIC_FUNCTION_P (fn))
1827 /* A non-private static member function is just like a
1828 friend; it can create and invoke private member
1829 functions, and be accessed without a class
1830 instance. */
1831 return;
1833 has_nonprivate_method = 1;
1834 break;
1836 else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1837 has_member_fn = 1;
1840 if (!has_nonprivate_method && has_member_fn)
1842 /* There are no non-private methods, and there's at least one
1843 private member function that isn't a constructor or
1844 destructor. (If all the private members are
1845 constructors/destructors we want to use the code below that
1846 issues error messages specifically referring to
1847 constructors/destructors.) */
1848 int i;
1849 tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1850 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1851 if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1852 || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1854 has_nonprivate_method = 1;
1855 break;
1857 if (!has_nonprivate_method)
1859 warning ("all member functions in class `%T' are private", t);
1860 return;
1864 /* Even if some of the member functions are non-private, the class
1865 won't be useful for much if all the constructors or destructors
1866 are private: such an object can never be created or destroyed. */
1867 if (TYPE_HAS_DESTRUCTOR (t))
1869 tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1871 if (TREE_PRIVATE (dtor))
1873 warning ("`%#T' only defines a private destructor and has no friends",
1875 return;
1879 if (TYPE_HAS_CONSTRUCTOR (t))
1881 int nonprivate_ctor = 0;
1883 /* If a non-template class does not define a copy
1884 constructor, one is defined for it, enabling it to avoid
1885 this warning. For a template class, this does not
1886 happen, and so we would normally get a warning on:
1888 template <class T> class C { private: C(); };
1890 To avoid this asymmetry, we check TYPE_HAS_INIT_REF. All
1891 complete non-template or fully instantiated classes have this
1892 flag set. */
1893 if (!TYPE_HAS_INIT_REF (t))
1894 nonprivate_ctor = 1;
1895 else
1896 for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1898 fn = OVL_NEXT (fn))
1900 tree ctor = OVL_CURRENT (fn);
1901 /* Ideally, we wouldn't count copy constructors (or, in
1902 fact, any constructor that takes an argument of the
1903 class type as a parameter) because such things cannot
1904 be used to construct an instance of the class unless
1905 you already have one. But, for now at least, we're
1906 more generous. */
1907 if (! TREE_PRIVATE (ctor))
1909 nonprivate_ctor = 1;
1910 break;
1914 if (nonprivate_ctor == 0)
1916 warning ("`%#T' only defines private constructors and has no friends",
1918 return;
1923 /* Function to help qsort sort FIELD_DECLs by name order. */
1925 static int
1926 field_decl_cmp (x, y)
1927 const tree *x, *y;
1929 if (DECL_NAME (*x) == DECL_NAME (*y))
1930 /* A nontype is "greater" than a type. */
1931 return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1932 if (DECL_NAME (*x) == NULL_TREE)
1933 return -1;
1934 if (DECL_NAME (*y) == NULL_TREE)
1935 return 1;
1936 if (DECL_NAME (*x) < DECL_NAME (*y))
1937 return -1;
1938 return 1;
1941 /* Comparison function to compare two TYPE_METHOD_VEC entries by name. */
1943 static int
1944 method_name_cmp (m1, m2)
1945 const tree *m1, *m2;
1947 if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1948 return 0;
1949 if (*m1 == NULL_TREE)
1950 return -1;
1951 if (*m2 == NULL_TREE)
1952 return 1;
1953 if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1954 return -1;
1955 return 1;
1958 /* Warn about duplicate methods in fn_fields. Also compact method
1959 lists so that lookup can be made faster.
1961 Data Structure: List of method lists. The outer list is a
1962 TREE_LIST, whose TREE_PURPOSE field is the field name and the
1963 TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs. TREE_CHAIN
1964 links the entire list of methods for TYPE_METHODS. Friends are
1965 chained in the same way as member functions (? TREE_CHAIN or
1966 DECL_CHAIN), but they live in the TREE_TYPE field of the outer
1967 list. That allows them to be quickly deleted, and requires no
1968 extra storage.
1970 Sort methods that are not special (i.e., constructors, destructors,
1971 and type conversion operators) so that we can find them faster in
1972 search. */
1974 static void
1975 finish_struct_methods (t)
1976 tree t;
1978 tree fn_fields;
1979 tree method_vec;
1980 int slot, len;
1982 if (!TYPE_METHODS (t))
1984 /* Clear these for safety; perhaps some parsing error could set
1985 these incorrectly. */
1986 TYPE_HAS_CONSTRUCTOR (t) = 0;
1987 TYPE_HAS_DESTRUCTOR (t) = 0;
1988 CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
1989 return;
1992 method_vec = CLASSTYPE_METHOD_VEC (t);
1993 my_friendly_assert (method_vec != NULL_TREE, 19991215);
1994 len = TREE_VEC_LENGTH (method_vec);
1996 /* First fill in entry 0 with the constructors, entry 1 with destructors,
1997 and the next few with type conversion operators (if any). */
1998 for (fn_fields = TYPE_METHODS (t); fn_fields;
1999 fn_fields = TREE_CHAIN (fn_fields))
2000 /* Clear out this flag. */
2001 DECL_IN_AGGR_P (fn_fields) = 0;
2003 if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2004 /* We thought there was a destructor, but there wasn't. Some
2005 parse errors cause this anomalous situation. */
2006 TYPE_HAS_DESTRUCTOR (t) = 0;
2008 /* Issue warnings about private constructors and such. If there are
2009 no methods, then some public defaults are generated. */
2010 maybe_warn_about_overly_private_class (t);
2012 /* Now sort the methods. */
2013 while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2014 len--;
2015 TREE_VEC_LENGTH (method_vec) = len;
2017 /* The type conversion ops have to live at the front of the vec, so we
2018 can't sort them. */
2019 for (slot = 2; slot < len; ++slot)
2021 tree fn = TREE_VEC_ELT (method_vec, slot);
2023 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2024 break;
2026 if (len - slot > 1)
2027 qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2028 (int (*)(const void *, const void *))method_name_cmp);
2031 /* Emit error when a duplicate definition of a type is seen. Patch up. */
2033 void
2034 duplicate_tag_error (t)
2035 tree t;
2037 error ("redefinition of `%#T'", t);
2038 cp_error_at ("previous definition of `%#T'", t);
2040 /* Pretend we haven't defined this type. */
2042 /* All of the component_decl's were TREE_CHAINed together in the parser.
2043 finish_struct_methods walks these chains and assembles all methods with
2044 the same base name into DECL_CHAINs. Now we don't need the parser chains
2045 anymore, so we unravel them. */
2047 /* This used to be in finish_struct, but it turns out that the
2048 TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2049 things... */
2050 if (CLASSTYPE_METHOD_VEC (t))
2052 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2053 int i, len = TREE_VEC_LENGTH (method_vec);
2054 for (i = 0; i < len; i++)
2056 tree unchain = TREE_VEC_ELT (method_vec, i);
2057 while (unchain != NULL_TREE)
2059 TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2060 unchain = OVL_NEXT (unchain);
2065 if (TYPE_LANG_SPECIFIC (t))
2067 tree binfo = TYPE_BINFO (t);
2068 int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2069 int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2070 tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2071 int use_template = CLASSTYPE_USE_TEMPLATE (t);
2073 memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2074 BINFO_BASETYPES(binfo) = NULL_TREE;
2076 TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2077 TYPE_BINFO (t) = binfo;
2078 CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2079 SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2080 TYPE_REDEFINED (t) = 1;
2081 CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2082 CLASSTYPE_USE_TEMPLATE (t) = use_template;
2083 CLASSTYPE_DECL_LIST (t) = NULL_TREE;
2085 TYPE_SIZE (t) = NULL_TREE;
2086 TYPE_MODE (t) = VOIDmode;
2087 TYPE_FIELDS (t) = NULL_TREE;
2088 TYPE_METHODS (t) = NULL_TREE;
2089 TYPE_VFIELD (t) = NULL_TREE;
2090 TYPE_CONTEXT (t) = NULL_TREE;
2092 /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above. */
2093 TYPE_LANG_FLAG_0 (t) = 0;
2094 TYPE_LANG_FLAG_1 (t) = 0;
2095 TYPE_LANG_FLAG_2 (t) = 0;
2096 TYPE_LANG_FLAG_3 (t) = 0;
2097 TYPE_LANG_FLAG_4 (t) = 0;
2098 TYPE_LANG_FLAG_5 (t) = 0;
2099 TYPE_LANG_FLAG_6 (t) = 0;
2100 /* But not this one. */
2101 SET_IS_AGGR_TYPE (t, 1);
2104 /* Make BINFO's vtable have N entries, including RTTI entries,
2105 vbase and vcall offsets, etc. Set its type and call the backend
2106 to lay it out. */
2108 static void
2109 layout_vtable_decl (binfo, n)
2110 tree binfo;
2111 int n;
2113 tree atype;
2114 tree vtable;
2116 atype = build_cplus_array_type (vtable_entry_type,
2117 build_index_type (size_int (n - 1)));
2118 layout_type (atype);
2120 /* We may have to grow the vtable. */
2121 vtable = get_vtbl_decl_for_binfo (binfo);
2122 if (!same_type_p (TREE_TYPE (vtable), atype))
2124 TREE_TYPE (vtable) = atype;
2125 DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2126 layout_decl (vtable, 0);
2128 /* At one time the vtable info was grabbed 2 words at a time. This
2129 fails on SPARC unless you have 8-byte alignment. */
2130 DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2131 DECL_ALIGN (vtable));
2135 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2136 have the same signature. */
2139 same_signature_p (fndecl, base_fndecl)
2140 tree fndecl, base_fndecl;
2142 /* One destructor overrides another if they are the same kind of
2143 destructor. */
2144 if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2145 && special_function_p (base_fndecl) == special_function_p (fndecl))
2146 return 1;
2147 /* But a non-destructor never overrides a destructor, nor vice
2148 versa, nor do different kinds of destructors override
2149 one-another. For example, a complete object destructor does not
2150 override a deleting destructor. */
2151 if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2152 return 0;
2154 if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2156 tree types, base_types;
2157 types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2158 base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2159 if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2160 == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2161 && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2162 return 1;
2164 return 0;
2167 /* Called from base_derived_from via dfs_walk. */
2169 static tree
2170 dfs_base_derived_from (tree binfo, void *data)
2172 tree base = (tree) data;
2174 if (same_type_p (TREE_TYPE (base), TREE_TYPE (binfo))
2175 && tree_int_cst_equal (BINFO_OFFSET (base), BINFO_OFFSET (binfo)))
2176 return error_mark_node;
2178 return NULL_TREE;
2181 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2182 subobject. */
2184 static bool
2185 base_derived_from (tree derived, tree base)
2187 return dfs_walk (derived, dfs_base_derived_from, NULL, base) != NULL_TREE;
2190 typedef struct find_final_overrider_data_s {
2191 /* The function for which we are trying to find a final overrider. */
2192 tree fn;
2193 /* The base class in which the function was declared. */
2194 tree declaring_base;
2195 /* The most derived class in the hierarchy. */
2196 tree most_derived_type;
2197 /* The candidate overriders. */
2198 tree candidates;
2199 } find_final_overrider_data;
2201 /* Called from find_final_overrider via dfs_walk. */
2203 static tree
2204 dfs_find_final_overrider (binfo, data)
2205 tree binfo;
2206 void *data;
2208 find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2210 if (same_type_p (BINFO_TYPE (binfo),
2211 BINFO_TYPE (ffod->declaring_base))
2212 && tree_int_cst_equal (BINFO_OFFSET (binfo),
2213 BINFO_OFFSET (ffod->declaring_base)))
2215 tree path;
2216 tree method;
2218 /* We haven't found an overrider yet. */
2219 method = NULL_TREE;
2220 /* We've found a path to the declaring base. Walk down the path
2221 looking for an overrider for FN. */
2222 for (path = reverse_path (binfo);
2223 path;
2224 path = TREE_CHAIN (path))
2226 method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2227 ffod->fn);
2228 if (method)
2230 path = TREE_VALUE (path);
2231 break;
2235 /* If we found an overrider, record the overriding function, and
2236 the base from which it came. */
2237 if (path)
2239 tree *candidate;
2241 /* Remove any candidates overridden by this new function. */
2242 candidate = &ffod->candidates;
2243 while (*candidate)
2245 /* If *CANDIDATE overrides METHOD, then METHOD
2246 cannot override anything else on the list. */
2247 if (base_derived_from (TREE_VALUE (*candidate), path))
2248 return NULL_TREE;
2249 /* If METHOD overrides *CANDIDATE, remove *CANDIDATE. */
2250 if (base_derived_from (path, TREE_VALUE (*candidate)))
2251 *candidate = TREE_CHAIN (*candidate);
2252 else
2253 candidate = &TREE_CHAIN (*candidate);
2256 /* Add the new function. */
2257 ffod->candidates = tree_cons (method, path, ffod->candidates);
2261 return NULL_TREE;
2264 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2265 FN and whose TREE_VALUE is the binfo for the base where the
2266 overriding occurs. BINFO (in the hierarchy dominated by T) is the
2267 base object in which FN is declared. */
2269 static tree
2270 find_final_overrider (t, binfo, fn)
2271 tree t;
2272 tree binfo;
2273 tree fn;
2275 find_final_overrider_data ffod;
2277 /* Getting this right is a little tricky. This is valid:
2279 struct S { virtual void f (); };
2280 struct T { virtual void f (); };
2281 struct U : public S, public T { };
2283 even though calling `f' in `U' is ambiguous. But,
2285 struct R { virtual void f(); };
2286 struct S : virtual public R { virtual void f (); };
2287 struct T : virtual public R { virtual void f (); };
2288 struct U : public S, public T { };
2290 is not -- there's no way to decide whether to put `S::f' or
2291 `T::f' in the vtable for `R'.
2293 The solution is to look at all paths to BINFO. If we find
2294 different overriders along any two, then there is a problem. */
2295 ffod.fn = fn;
2296 ffod.declaring_base = binfo;
2297 ffod.most_derived_type = t;
2298 ffod.candidates = NULL_TREE;
2300 dfs_walk (TYPE_BINFO (t),
2301 dfs_find_final_overrider,
2302 NULL,
2303 &ffod);
2305 /* If there was no winner, issue an error message. */
2306 if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2308 error ("no unique final overrider for `%D' in `%T'", fn, t);
2309 return error_mark_node;
2312 return ffod.candidates;
2315 /* Return the index of the vcall offset for FN when TYPE is used as a
2316 virtual base. */
2318 static tree
2319 get_vcall_index (tree fn, tree type)
2321 tree v;
2323 for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2324 if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2325 || same_signature_p (fn, TREE_PURPOSE (v)))
2326 break;
2328 /* There should always be an appropriate index. */
2329 my_friendly_assert (v, 20021103);
2331 return TREE_VALUE (v);
2334 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2335 dominated by T. FN has been overriden in BINFO; VIRTUALS points to the
2336 corresponding position in the BINFO_VIRTUALS list. */
2338 static void
2339 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2340 tree t;
2341 tree binfo;
2342 tree fn;
2343 tree *virtuals;
2345 tree b;
2346 tree overrider;
2347 tree delta;
2348 tree virtual_base;
2349 tree first_defn;
2350 bool lost = false;
2352 /* Find the nearest primary base (possibly binfo itself) which defines
2353 this function; this is the class the caller will convert to when
2354 calling FN through BINFO. */
2355 for (b = binfo; ; b = get_primary_binfo (b))
2357 if (look_for_overrides_here (BINFO_TYPE (b), fn))
2358 break;
2360 /* The nearest definition is from a lost primary. */
2361 if (BINFO_LOST_PRIMARY_P (b))
2362 lost = true;
2364 first_defn = b;
2366 /* Find the final overrider. */
2367 overrider = find_final_overrider (t, b, fn);
2368 if (overrider == error_mark_node)
2369 return;
2371 /* Check for unsupported covariant returns again now that we've
2372 calculated the base offsets. */
2373 check_final_overrider (TREE_PURPOSE (overrider), fn);
2375 /* Assume that we will produce a thunk that convert all the way to
2376 the final overrider, and not to an intermediate virtual base. */
2377 virtual_base = NULL_TREE;
2379 /* See if we can convert to an intermediate virtual base first, and then
2380 use the vcall offset located there to finish the conversion. */
2381 for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2383 /* If we find the final overrider, then we can stop
2384 walking. */
2385 if (same_type_p (BINFO_TYPE (b),
2386 BINFO_TYPE (TREE_VALUE (overrider))))
2387 break;
2389 /* If we find a virtual base, and we haven't yet found the
2390 overrider, then there is a virtual base between the
2391 declaring base (first_defn) and the final overrider. */
2392 if (!virtual_base && TREE_VIA_VIRTUAL (b))
2393 virtual_base = b;
2396 /* Compute the constant adjustment to the `this' pointer. The
2397 `this' pointer, when this function is called, will point at BINFO
2398 (or one of its primary bases, which are at the same offset). */
2399 if (virtual_base)
2400 /* The `this' pointer needs to be adjusted from the declaration to
2401 the nearest virtual base. */
2402 delta = size_diffop (BINFO_OFFSET (virtual_base),
2403 BINFO_OFFSET (first_defn));
2404 else if (lost)
2405 /* If the nearest definition is in a lost primary, we don't need an
2406 entry in our vtable. Except possibly in a constructor vtable,
2407 if we happen to get our primary back. In that case, the offset
2408 will be zero, as it will be a primary base. */
2409 delta = size_zero_node;
2410 else
2411 /* The `this' pointer needs to be adjusted from pointing to
2412 BINFO to pointing at the base where the final overrider
2413 appears. */
2414 delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2415 BINFO_OFFSET (binfo));
2417 modify_vtable_entry (t,
2418 binfo,
2419 TREE_PURPOSE (overrider),
2420 delta,
2421 virtuals);
2423 if (virtual_base)
2424 BV_VCALL_INDEX (*virtuals)
2425 = get_vcall_index (TREE_PURPOSE (overrider),
2426 BINFO_TYPE (virtual_base));
2429 /* Called from modify_all_vtables via dfs_walk. */
2431 static tree
2432 dfs_modify_vtables (binfo, data)
2433 tree binfo;
2434 void *data;
2436 if (/* There's no need to modify the vtable for a non-virtual
2437 primary base; we're not going to use that vtable anyhow.
2438 We do still need to do this for virtual primary bases, as they
2439 could become non-primary in a construction vtable. */
2440 (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2441 /* Similarly, a base without a vtable needs no modification. */
2442 && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2444 tree t;
2445 tree virtuals;
2446 tree old_virtuals;
2448 t = (tree) data;
2450 make_new_vtable (t, binfo);
2452 /* Now, go through each of the virtual functions in the virtual
2453 function table for BINFO. Find the final overrider, and
2454 update the BINFO_VIRTUALS list appropriately. */
2455 for (virtuals = BINFO_VIRTUALS (binfo),
2456 old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2457 virtuals;
2458 virtuals = TREE_CHAIN (virtuals),
2459 old_virtuals = TREE_CHAIN (old_virtuals))
2460 update_vtable_entry_for_fn (t,
2461 binfo,
2462 BV_FN (old_virtuals),
2463 &virtuals);
2466 SET_BINFO_MARKED (binfo);
2468 return NULL_TREE;
2471 /* Update all of the primary and secondary vtables for T. Create new
2472 vtables as required, and initialize their RTTI information. Each
2473 of the functions in VIRTUALS is declared in T and may override a
2474 virtual function from a base class; find and modify the appropriate
2475 entries to point to the overriding functions. Returns a list, in
2476 declaration order, of the virtual functions that are declared in T,
2477 but do not appear in the primary base class vtable, and which
2478 should therefore be appended to the end of the vtable for T. */
2480 static tree
2481 modify_all_vtables (t, virtuals)
2482 tree t;
2483 tree virtuals;
2485 tree binfo = TYPE_BINFO (t);
2486 tree *fnsp;
2488 /* Update all of the vtables. */
2489 dfs_walk (binfo,
2490 dfs_modify_vtables,
2491 dfs_unmarked_real_bases_queue_p,
2493 dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2495 /* Add virtual functions not already in our primary vtable. These
2496 will be both those introduced by this class, and those overridden
2497 from secondary bases. It does not include virtuals merely
2498 inherited from secondary bases. */
2499 for (fnsp = &virtuals; *fnsp; )
2501 tree fn = TREE_VALUE (*fnsp);
2503 if (!value_member (fn, BINFO_VIRTUALS (binfo))
2504 || DECL_VINDEX (fn) == error_mark_node)
2506 /* We don't need to adjust the `this' pointer when
2507 calling this function. */
2508 BV_DELTA (*fnsp) = integer_zero_node;
2509 BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2511 /* This is a function not already in our vtable. Keep it. */
2512 fnsp = &TREE_CHAIN (*fnsp);
2514 else
2515 /* We've already got an entry for this function. Skip it. */
2516 *fnsp = TREE_CHAIN (*fnsp);
2519 return virtuals;
2522 /* Get the base virtual function declarations in T that have the
2523 indicated NAME. */
2525 static tree
2526 get_basefndecls (name, t)
2527 tree name, t;
2529 tree methods;
2530 tree base_fndecls = NULL_TREE;
2531 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2532 int i;
2534 for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2535 if (TREE_CODE (methods) == FUNCTION_DECL
2536 && DECL_VINDEX (methods) != NULL_TREE
2537 && DECL_NAME (methods) == name)
2538 base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2540 if (base_fndecls)
2541 return base_fndecls;
2543 for (i = 0; i < n_baseclasses; i++)
2545 tree basetype = TYPE_BINFO_BASETYPE (t, i);
2546 base_fndecls = chainon (get_basefndecls (name, basetype),
2547 base_fndecls);
2550 return base_fndecls;
2553 /* If this declaration supersedes the declaration of
2554 a method declared virtual in the base class, then
2555 mark this field as being virtual as well. */
2557 static void
2558 check_for_override (decl, ctype)
2559 tree decl, ctype;
2561 if (TREE_CODE (decl) == TEMPLATE_DECL)
2562 /* In [temp.mem] we have:
2564 A specialization of a member function template does not
2565 override a virtual function from a base class. */
2566 return;
2567 if ((DECL_DESTRUCTOR_P (decl)
2568 || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2569 && look_for_overrides (ctype, decl)
2570 && !DECL_STATIC_FUNCTION_P (decl))
2571 /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2572 the error_mark_node so that we know it is an overriding
2573 function. */
2574 DECL_VINDEX (decl) = decl;
2576 if (DECL_VIRTUAL_P (decl))
2578 if (!DECL_VINDEX (decl))
2579 DECL_VINDEX (decl) = error_mark_node;
2580 IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2584 /* Warn about hidden virtual functions that are not overridden in t.
2585 We know that constructors and destructors don't apply. */
2587 void
2588 warn_hidden (t)
2589 tree t;
2591 tree method_vec = CLASSTYPE_METHOD_VEC (t);
2592 int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2593 int i;
2595 /* We go through each separately named virtual function. */
2596 for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2598 tree fns;
2599 tree name;
2600 tree fndecl;
2601 tree base_fndecls;
2602 int j;
2604 /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2605 have the same name. Figure out what name that is. */
2606 name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2607 /* There are no possibly hidden functions yet. */
2608 base_fndecls = NULL_TREE;
2609 /* Iterate through all of the base classes looking for possibly
2610 hidden functions. */
2611 for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2613 tree basetype = TYPE_BINFO_BASETYPE (t, j);
2614 base_fndecls = chainon (get_basefndecls (name, basetype),
2615 base_fndecls);
2618 /* If there are no functions to hide, continue. */
2619 if (!base_fndecls)
2620 continue;
2622 /* Remove any overridden functions. */
2623 for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2625 fndecl = OVL_CURRENT (fns);
2626 if (DECL_VINDEX (fndecl))
2628 tree *prev = &base_fndecls;
2630 while (*prev)
2631 /* If the method from the base class has the same
2632 signature as the method from the derived class, it
2633 has been overridden. */
2634 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2635 *prev = TREE_CHAIN (*prev);
2636 else
2637 prev = &TREE_CHAIN (*prev);
2641 /* Now give a warning for all base functions without overriders,
2642 as they are hidden. */
2643 while (base_fndecls)
2645 /* Here we know it is a hider, and no overrider exists. */
2646 cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2647 cp_warning_at (" by `%D'",
2648 OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2649 base_fndecls = TREE_CHAIN (base_fndecls);
2654 /* Check for things that are invalid. There are probably plenty of other
2655 things we should check for also. */
2657 static void
2658 finish_struct_anon (t)
2659 tree t;
2661 tree field;
2663 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2665 if (TREE_STATIC (field))
2666 continue;
2667 if (TREE_CODE (field) != FIELD_DECL)
2668 continue;
2670 if (DECL_NAME (field) == NULL_TREE
2671 && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2673 tree elt = TYPE_FIELDS (TREE_TYPE (field));
2674 for (; elt; elt = TREE_CHAIN (elt))
2676 /* We're generally only interested in entities the user
2677 declared, but we also find nested classes by noticing
2678 the TYPE_DECL that we create implicitly. You're
2679 allowed to put one anonymous union inside another,
2680 though, so we explicitly tolerate that. We use
2681 TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2682 we also allow unnamed types used for defining fields. */
2683 if (DECL_ARTIFICIAL (elt)
2684 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2685 || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2686 continue;
2688 if (DECL_NAME (elt) == constructor_name (t))
2689 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2690 elt);
2692 if (TREE_CODE (elt) != FIELD_DECL)
2694 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2695 elt);
2696 continue;
2699 if (TREE_PRIVATE (elt))
2700 cp_pedwarn_at ("private member `%#D' in anonymous union",
2701 elt);
2702 else if (TREE_PROTECTED (elt))
2703 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2704 elt);
2706 TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2707 TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2713 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2714 will be used later during class template instantiation.
2715 When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2716 a non-static member data (FIELD_DECL), a member function
2717 (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
2718 a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2719 When FRIEND_P is nonzero, T is either a friend class
2720 (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2721 (FUNCTION_DECL, TEMPLATE_DECL). */
2723 void
2724 maybe_add_class_template_decl_list (type, t, friend_p)
2725 tree type;
2726 tree t;
2727 int friend_p;
2729 /* Save some memory by not creating TREE_LIST if TYPE is not template. */
2730 if (CLASSTYPE_TEMPLATE_INFO (type))
2731 CLASSTYPE_DECL_LIST (type)
2732 = tree_cons (friend_p ? NULL_TREE : type,
2733 t, CLASSTYPE_DECL_LIST (type));
2736 /* Create default constructors, assignment operators, and so forth for
2737 the type indicated by T, if they are needed.
2738 CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2739 CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2740 class cannot have a default constructor, copy constructor taking a
2741 const reference argument, or an assignment operator taking a const
2742 reference, respectively. If a virtual destructor is created, its
2743 DECL is returned; otherwise the return value is NULL_TREE. */
2745 static tree
2746 add_implicitly_declared_members (t, cant_have_default_ctor,
2747 cant_have_const_cctor,
2748 cant_have_const_assignment)
2749 tree t;
2750 int cant_have_default_ctor;
2751 int cant_have_const_cctor;
2752 int cant_have_const_assignment;
2754 tree default_fn;
2755 tree implicit_fns = NULL_TREE;
2756 tree virtual_dtor = NULL_TREE;
2757 tree *f;
2759 ++adding_implicit_members;
2761 /* Destructor. */
2762 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2764 default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2765 check_for_override (default_fn, t);
2767 /* If we couldn't make it work, then pretend we didn't need it. */
2768 if (default_fn == void_type_node)
2769 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2770 else
2772 TREE_CHAIN (default_fn) = implicit_fns;
2773 implicit_fns = default_fn;
2775 if (DECL_VINDEX (default_fn))
2776 virtual_dtor = default_fn;
2779 else
2780 /* Any non-implicit destructor is non-trivial. */
2781 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2783 /* Default constructor. */
2784 if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2786 default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2787 TREE_CHAIN (default_fn) = implicit_fns;
2788 implicit_fns = default_fn;
2791 /* Copy constructor. */
2792 if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2794 /* ARM 12.18: You get either X(X&) or X(const X&), but
2795 not both. --Chip */
2796 default_fn
2797 = implicitly_declare_fn (sfk_copy_constructor, t,
2798 /*const_p=*/!cant_have_const_cctor);
2799 TREE_CHAIN (default_fn) = implicit_fns;
2800 implicit_fns = default_fn;
2803 /* Assignment operator. */
2804 if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2806 default_fn
2807 = implicitly_declare_fn (sfk_assignment_operator, t,
2808 /*const_p=*/!cant_have_const_assignment);
2809 TREE_CHAIN (default_fn) = implicit_fns;
2810 implicit_fns = default_fn;
2813 /* Now, hook all of the new functions on to TYPE_METHODS,
2814 and add them to the CLASSTYPE_METHOD_VEC. */
2815 for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2817 add_method (t, *f, /*error_p=*/0);
2818 maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2820 *f = TYPE_METHODS (t);
2821 TYPE_METHODS (t) = implicit_fns;
2823 --adding_implicit_members;
2825 return virtual_dtor;
2828 /* Subroutine of finish_struct_1. Recursively count the number of fields
2829 in TYPE, including anonymous union members. */
2831 static int
2832 count_fields (fields)
2833 tree fields;
2835 tree x;
2836 int n_fields = 0;
2837 for (x = fields; x; x = TREE_CHAIN (x))
2839 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2840 n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2841 else
2842 n_fields += 1;
2844 return n_fields;
2847 /* Subroutine of finish_struct_1. Recursively add all the fields in the
2848 TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX. */
2850 static int
2851 add_fields_to_vec (fields, field_vec, idx)
2852 tree fields, field_vec;
2853 int idx;
2855 tree x;
2856 for (x = fields; x; x = TREE_CHAIN (x))
2858 if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2859 idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2860 else
2861 TREE_VEC_ELT (field_vec, idx++) = x;
2863 return idx;
2866 /* FIELD is a bit-field. We are finishing the processing for its
2867 enclosing type. Issue any appropriate messages and set appropriate
2868 flags. */
2870 static void
2871 check_bitfield_decl (field)
2872 tree field;
2874 tree type = TREE_TYPE (field);
2875 tree w = NULL_TREE;
2877 /* Detect invalid bit-field type. */
2878 if (DECL_INITIAL (field)
2879 && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2881 cp_error_at ("bit-field `%#D' with non-integral type", field);
2882 w = error_mark_node;
2885 /* Detect and ignore out of range field width. */
2886 if (DECL_INITIAL (field))
2888 w = DECL_INITIAL (field);
2890 /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs. */
2891 STRIP_NOPS (w);
2893 /* detect invalid field size. */
2894 if (TREE_CODE (w) == CONST_DECL)
2895 w = DECL_INITIAL (w);
2896 else
2897 w = decl_constant_value (w);
2899 if (TREE_CODE (w) != INTEGER_CST)
2901 cp_error_at ("bit-field `%D' width not an integer constant",
2902 field);
2903 w = error_mark_node;
2905 else if (tree_int_cst_sgn (w) < 0)
2907 cp_error_at ("negative width in bit-field `%D'", field);
2908 w = error_mark_node;
2910 else if (integer_zerop (w) && DECL_NAME (field) != 0)
2912 cp_error_at ("zero width for bit-field `%D'", field);
2913 w = error_mark_node;
2915 else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
2916 && TREE_CODE (type) != ENUMERAL_TYPE
2917 && TREE_CODE (type) != BOOLEAN_TYPE)
2918 cp_warning_at ("width of `%D' exceeds its type", field);
2919 else if (TREE_CODE (type) == ENUMERAL_TYPE
2920 && (0 > compare_tree_int (w,
2921 min_precision (TYPE_MIN_VALUE (type),
2922 TREE_UNSIGNED (type)))
2923 || 0 > compare_tree_int (w,
2924 min_precision
2925 (TYPE_MAX_VALUE (type),
2926 TREE_UNSIGNED (type)))))
2927 cp_warning_at ("`%D' is too small to hold all values of `%#T'",
2928 field, type);
2931 /* Remove the bit-field width indicator so that the rest of the
2932 compiler does not treat that value as an initializer. */
2933 DECL_INITIAL (field) = NULL_TREE;
2935 if (w != error_mark_node)
2937 DECL_SIZE (field) = convert (bitsizetype, w);
2938 DECL_BIT_FIELD (field) = 1;
2940 if (integer_zerop (w)
2941 && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
2943 #ifdef EMPTY_FIELD_BOUNDARY
2944 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2945 EMPTY_FIELD_BOUNDARY);
2946 #endif
2947 #ifdef PCC_BITFIELD_TYPE_MATTERS
2948 if (PCC_BITFIELD_TYPE_MATTERS)
2950 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
2951 TYPE_ALIGN (type));
2952 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2954 #endif
2957 else
2959 /* Non-bit-fields are aligned for their type. */
2960 DECL_BIT_FIELD (field) = 0;
2961 CLEAR_DECL_C_BIT_FIELD (field);
2962 DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
2963 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
2967 /* FIELD is a non bit-field. We are finishing the processing for its
2968 enclosing type T. Issue any appropriate messages and set appropriate
2969 flags. */
2971 static void
2972 check_field_decl (field, t, cant_have_const_ctor,
2973 cant_have_default_ctor, no_const_asn_ref,
2974 any_default_members)
2975 tree field;
2976 tree t;
2977 int *cant_have_const_ctor;
2978 int *cant_have_default_ctor;
2979 int *no_const_asn_ref;
2980 int *any_default_members;
2982 tree type = strip_array_types (TREE_TYPE (field));
2984 /* An anonymous union cannot contain any fields which would change
2985 the settings of CANT_HAVE_CONST_CTOR and friends. */
2986 if (ANON_UNION_TYPE_P (type))
2988 /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
2989 structs. So, we recurse through their fields here. */
2990 else if (ANON_AGGR_TYPE_P (type))
2992 tree fields;
2994 for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
2995 if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
2996 check_field_decl (fields, t, cant_have_const_ctor,
2997 cant_have_default_ctor, no_const_asn_ref,
2998 any_default_members);
3000 /* Check members with class type for constructors, destructors,
3001 etc. */
3002 else if (CLASS_TYPE_P (type))
3004 /* Never let anything with uninheritable virtuals
3005 make it through without complaint. */
3006 abstract_virtuals_error (field, type);
3008 if (TREE_CODE (t) == UNION_TYPE)
3010 if (TYPE_NEEDS_CONSTRUCTING (type))
3011 cp_error_at ("member `%#D' with constructor not allowed in union",
3012 field);
3013 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3014 cp_error_at ("member `%#D' with destructor not allowed in union",
3015 field);
3016 if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3017 cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3018 field);
3020 else
3022 TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3023 TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
3024 |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3025 TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3026 TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3029 if (!TYPE_HAS_CONST_INIT_REF (type))
3030 *cant_have_const_ctor = 1;
3032 if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3033 *no_const_asn_ref = 1;
3035 if (TYPE_HAS_CONSTRUCTOR (type)
3036 && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3037 *cant_have_default_ctor = 1;
3039 if (DECL_INITIAL (field) != NULL_TREE)
3041 /* `build_class_init_list' does not recognize
3042 non-FIELD_DECLs. */
3043 if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3044 cp_error_at ("multiple fields in union `%T' initialized");
3045 *any_default_members = 1;
3048 /* Non-bit-fields are aligned for their type, except packed fields
3049 which require only BITS_PER_UNIT alignment. */
3050 DECL_ALIGN (field) = MAX (DECL_ALIGN (field),
3051 (DECL_PACKED (field)
3052 ? BITS_PER_UNIT
3053 : TYPE_ALIGN (TREE_TYPE (field))));
3054 if (! DECL_PACKED (field))
3055 DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3058 /* Check the data members (both static and non-static), class-scoped
3059 typedefs, etc., appearing in the declaration of T. Issue
3060 appropriate diagnostics. Sets ACCESS_DECLS to a list (in
3061 declaration order) of access declarations; each TREE_VALUE in this
3062 list is a USING_DECL.
3064 In addition, set the following flags:
3066 EMPTY_P
3067 The class is empty, i.e., contains no non-static data members.
3069 CANT_HAVE_DEFAULT_CTOR_P
3070 This class cannot have an implicitly generated default
3071 constructor.
3073 CANT_HAVE_CONST_CTOR_P
3074 This class cannot have an implicitly generated copy constructor
3075 taking a const reference.
3077 CANT_HAVE_CONST_ASN_REF
3078 This class cannot have an implicitly generated assignment
3079 operator taking a const reference.
3081 All of these flags should be initialized before calling this
3082 function.
3084 Returns a pointer to the end of the TYPE_FIELDs chain; additional
3085 fields can be added by adding to this chain. */
3087 static void
3088 check_field_decls (tree t, tree *access_decls,
3089 int *cant_have_default_ctor_p,
3090 int *cant_have_const_ctor_p,
3091 int *no_const_asn_ref_p)
3093 tree *field;
3094 tree *next;
3095 int has_pointers;
3096 int any_default_members;
3098 /* First, delete any duplicate fields. */
3099 delete_duplicate_fields (TYPE_FIELDS (t));
3101 /* Assume there are no access declarations. */
3102 *access_decls = NULL_TREE;
3103 /* Assume this class has no pointer members. */
3104 has_pointers = 0;
3105 /* Assume none of the members of this class have default
3106 initializations. */
3107 any_default_members = 0;
3109 for (field = &TYPE_FIELDS (t); *field; field = next)
3111 tree x = *field;
3112 tree type = TREE_TYPE (x);
3114 next = &TREE_CHAIN (x);
3116 if (TREE_CODE (x) == FIELD_DECL)
3118 DECL_PACKED (x) |= TYPE_PACKED (t);
3120 if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3121 /* We don't treat zero-width bitfields as making a class
3122 non-empty. */
3124 else
3126 tree element_type;
3128 /* The class is non-empty. */
3129 CLASSTYPE_EMPTY_P (t) = 0;
3130 /* The class is not even nearly empty. */
3131 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3132 /* If one of the data members contains an empty class,
3133 so does T. */
3134 element_type = strip_array_types (type);
3135 if (CLASS_TYPE_P (element_type)
3136 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3137 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3141 if (TREE_CODE (x) == USING_DECL)
3143 /* Prune the access declaration from the list of fields. */
3144 *field = TREE_CHAIN (x);
3146 /* Save the access declarations for our caller. */
3147 *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3149 /* Since we've reset *FIELD there's no reason to skip to the
3150 next field. */
3151 next = field;
3152 continue;
3155 if (TREE_CODE (x) == TYPE_DECL
3156 || TREE_CODE (x) == TEMPLATE_DECL)
3157 continue;
3159 /* If we've gotten this far, it's a data member, possibly static,
3160 or an enumerator. */
3162 DECL_CONTEXT (x) = t;
3164 /* ``A local class cannot have static data members.'' ARM 9.4 */
3165 if (current_function_decl && TREE_STATIC (x))
3166 cp_error_at ("field `%D' in local class cannot be static", x);
3168 /* Perform error checking that did not get done in
3169 grokdeclarator. */
3170 if (TREE_CODE (type) == FUNCTION_TYPE)
3172 cp_error_at ("field `%D' invalidly declared function type",
3174 type = build_pointer_type (type);
3175 TREE_TYPE (x) = type;
3177 else if (TREE_CODE (type) == METHOD_TYPE)
3179 cp_error_at ("field `%D' invalidly declared method type", x);
3180 type = build_pointer_type (type);
3181 TREE_TYPE (x) = type;
3183 else if (TREE_CODE (type) == OFFSET_TYPE)
3185 cp_error_at ("field `%D' invalidly declared offset type", x);
3186 type = build_pointer_type (type);
3187 TREE_TYPE (x) = type;
3190 if (type == error_mark_node)
3191 continue;
3193 /* When this goes into scope, it will be a non-local reference. */
3194 DECL_NONLOCAL (x) = 1;
3196 if (TREE_CODE (x) == CONST_DECL)
3197 continue;
3199 if (TREE_CODE (x) == VAR_DECL)
3201 if (TREE_CODE (t) == UNION_TYPE)
3202 /* Unions cannot have static members. */
3203 cp_error_at ("field `%D' declared static in union", x);
3205 continue;
3208 /* Now it can only be a FIELD_DECL. */
3210 if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3211 CLASSTYPE_NON_AGGREGATE (t) = 1;
3213 /* If this is of reference type, check if it needs an init.
3214 Also do a little ANSI jig if necessary. */
3215 if (TREE_CODE (type) == REFERENCE_TYPE)
3217 CLASSTYPE_NON_POD_P (t) = 1;
3218 if (DECL_INITIAL (x) == NULL_TREE)
3219 SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3221 /* ARM $12.6.2: [A member initializer list] (or, for an
3222 aggregate, initialization by a brace-enclosed list) is the
3223 only way to initialize nonstatic const and reference
3224 members. */
3225 *cant_have_default_ctor_p = 1;
3226 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3228 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3229 && extra_warnings)
3230 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3233 type = strip_array_types (type);
3235 if (TREE_CODE (type) == POINTER_TYPE)
3236 has_pointers = 1;
3238 if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3239 CLASSTYPE_HAS_MUTABLE (t) = 1;
3241 if (! pod_type_p (type))
3242 /* DR 148 now allows pointers to members (which are POD themselves),
3243 to be allowed in POD structs. */
3244 CLASSTYPE_NON_POD_P (t) = 1;
3246 if (! zero_init_p (type))
3247 CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3249 /* If any field is const, the structure type is pseudo-const. */
3250 if (CP_TYPE_CONST_P (type))
3252 C_TYPE_FIELDS_READONLY (t) = 1;
3253 if (DECL_INITIAL (x) == NULL_TREE)
3254 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3256 /* ARM $12.6.2: [A member initializer list] (or, for an
3257 aggregate, initialization by a brace-enclosed list) is the
3258 only way to initialize nonstatic const and reference
3259 members. */
3260 *cant_have_default_ctor_p = 1;
3261 TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3263 if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3264 && extra_warnings)
3265 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3267 /* A field that is pseudo-const makes the structure likewise. */
3268 else if (IS_AGGR_TYPE (type))
3270 C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3271 SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3272 CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3273 | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3276 /* Core issue 80: A nonstatic data member is required to have a
3277 different name from the class iff the class has a
3278 user-defined constructor. */
3279 if (DECL_NAME (x) == constructor_name (t)
3280 && TYPE_HAS_CONSTRUCTOR (t))
3281 cp_pedwarn_at ("field `%#D' with same name as class", x);
3283 /* We set DECL_C_BIT_FIELD in grokbitfield.
3284 If the type and width are valid, we'll also set DECL_BIT_FIELD. */
3285 if (DECL_C_BIT_FIELD (x))
3286 check_bitfield_decl (x);
3287 else
3288 check_field_decl (x, t,
3289 cant_have_const_ctor_p,
3290 cant_have_default_ctor_p,
3291 no_const_asn_ref_p,
3292 &any_default_members);
3295 /* Effective C++ rule 11. */
3296 if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3297 && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3299 warning ("`%#T' has pointer data members", t);
3301 if (! TYPE_HAS_INIT_REF (t))
3303 warning (" but does not override `%T(const %T&)'", t, t);
3304 if (! TYPE_HAS_ASSIGN_REF (t))
3305 warning (" or `operator=(const %T&)'", t);
3307 else if (! TYPE_HAS_ASSIGN_REF (t))
3308 warning (" but does not override `operator=(const %T&)'", t);
3312 /* Check anonymous struct/anonymous union fields. */
3313 finish_struct_anon (t);
3315 /* We've built up the list of access declarations in reverse order.
3316 Fix that now. */
3317 *access_decls = nreverse (*access_decls);
3320 /* If TYPE is an empty class type, records its OFFSET in the table of
3321 OFFSETS. */
3323 static int
3324 record_subobject_offset (type, offset, offsets)
3325 tree type;
3326 tree offset;
3327 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 (type, offset, offsets)
3353 tree type;
3354 tree offset;
3355 splay_tree offsets;
3357 splay_tree_node n;
3358 tree t;
3360 if (!is_empty_class (type))
3361 return 0;
3363 /* Record the location of this empty object in OFFSETS. */
3364 n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3365 if (!n)
3366 return 0;
3368 for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3369 if (same_type_p (TREE_VALUE (t), type))
3370 return 1;
3372 return 0;
3375 /* Walk through all the subobjects of TYPE (located at OFFSET). Call
3376 F for every subobject, passing it the type, offset, and table of
3377 OFFSETS. If VBASES_P is nonzero, then even virtual non-primary
3378 bases should be traversed; otherwise, they are ignored.
3380 If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3381 than MAX_OFFSET will not be walked.
3383 If F returns a nonzero value, the traversal ceases, and that value
3384 is returned. Otherwise, returns zero. */
3386 static int
3387 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3388 tree type;
3389 subobject_offset_fn f;
3390 tree offset;
3391 splay_tree offsets;
3392 tree max_offset;
3393 int vbases_p;
3395 int r = 0;
3397 /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3398 stop. */
3399 if (max_offset && INT_CST_LT (max_offset, offset))
3400 return 0;
3402 if (CLASS_TYPE_P (type))
3404 tree field;
3405 tree binfo;
3406 int i;
3408 /* Avoid recursing into objects that are not interesting. */
3409 if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3410 return 0;
3412 /* Record the location of TYPE. */
3413 r = (*f) (type, offset, offsets);
3414 if (r)
3415 return r;
3417 /* Iterate through the direct base classes of TYPE. */
3418 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3420 binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3422 if (abi_version_at_least (2)
3423 && TREE_VIA_VIRTUAL (binfo))
3424 continue;
3426 if (!vbases_p
3427 && TREE_VIA_VIRTUAL (binfo)
3428 && !BINFO_PRIMARY_P (binfo))
3429 continue;
3431 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3433 size_binop (PLUS_EXPR,
3434 offset,
3435 BINFO_OFFSET (binfo)),
3436 offsets,
3437 max_offset,
3438 (abi_version_at_least (2)
3439 ? /*vbases_p=*/0 : vbases_p));
3440 if (r)
3441 return r;
3444 /* Iterate through the virtual base classes of TYPE. In G++
3445 3.2, we included virtual bases in the direct base class loop
3446 above, which results in incorrect results; the correct
3447 offsets for virtual bases are only known when working with
3448 the most derived type. */
3449 if (abi_version_at_least (2) && vbases_p)
3451 tree vbase;
3453 for (vbase = CLASSTYPE_VBASECLASSES (type);
3454 vbase;
3455 vbase = TREE_CHAIN (vbase))
3457 binfo = TREE_VALUE (vbase);
3458 r = walk_subobject_offsets (BINFO_TYPE (binfo),
3460 size_binop (PLUS_EXPR,
3461 offset,
3462 BINFO_OFFSET (binfo)),
3463 offsets,
3464 max_offset,
3465 /*vbases_p=*/0);
3469 /* Iterate through the fields of TYPE. */
3470 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3471 if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3473 tree field_offset;
3475 if (abi_version_at_least (2))
3476 field_offset = byte_position (field);
3477 else
3478 /* In G++ 3.2, DECL_FIELD_OFFSET was used. */
3479 field_offset = DECL_FIELD_OFFSET (field);
3481 r = walk_subobject_offsets (TREE_TYPE (field),
3483 size_binop (PLUS_EXPR,
3484 offset,
3485 field_offset),
3486 offsets,
3487 max_offset,
3488 /*vbases_p=*/1);
3489 if (r)
3490 return r;
3493 else if (TREE_CODE (type) == ARRAY_TYPE)
3495 tree element_type = strip_array_types (type);
3496 tree domain = TYPE_DOMAIN (type);
3497 tree index;
3499 /* Avoid recursing into objects that are not interesting. */
3500 if (!CLASS_TYPE_P (element_type)
3501 || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3502 return 0;
3504 /* Step through each of the elements in the array. */
3505 for (index = size_zero_node;
3506 /* G++ 3.2 had an off-by-one error here. */
3507 (abi_version_at_least (2)
3508 ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3509 : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3510 index = size_binop (PLUS_EXPR, index, size_one_node))
3512 r = walk_subobject_offsets (TREE_TYPE (type),
3514 offset,
3515 offsets,
3516 max_offset,
3517 /*vbases_p=*/1);
3518 if (r)
3519 return r;
3520 offset = size_binop (PLUS_EXPR, offset,
3521 TYPE_SIZE_UNIT (TREE_TYPE (type)));
3522 /* If this new OFFSET is bigger than the MAX_OFFSET, then
3523 there's no point in iterating through the remaining
3524 elements of the array. */
3525 if (max_offset && INT_CST_LT (max_offset, offset))
3526 break;
3530 return 0;
3533 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3534 OFFSETS. If VBASES_P is nonzero, virtual bases of TYPE are
3535 examined. */
3537 static void
3538 record_subobject_offsets (type, offset, offsets, vbases_p)
3539 tree type;
3540 tree offset;
3541 splay_tree offsets;
3542 int vbases_p;
3544 walk_subobject_offsets (type, record_subobject_offset, offset,
3545 offsets, /*max_offset=*/NULL_TREE, vbases_p);
3548 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3549 OFFSET) conflict with entries in OFFSETS. If VBASES_P is nonzero,
3550 virtual bases of TYPE are examined. */
3552 static int
3553 layout_conflict_p (type, offset, offsets, vbases_p)
3554 tree type;
3555 tree offset;
3556 splay_tree offsets;
3557 int vbases_p;
3559 splay_tree_node max_node;
3561 /* Get the node in OFFSETS that indicates the maximum offset where
3562 an empty subobject is located. */
3563 max_node = splay_tree_max (offsets);
3564 /* If there aren't any empty subobjects, then there's no point in
3565 performing this check. */
3566 if (!max_node)
3567 return 0;
3569 return walk_subobject_offsets (type, check_subobject_offset, offset,
3570 offsets, (tree) (max_node->key),
3571 vbases_p);
3574 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3575 non-static data member of the type indicated by RLI. BINFO is the
3576 binfo corresponding to the base subobject, OFFSETS maps offsets to
3577 types already located at those offsets. This function determines
3578 the position of the DECL. */
3580 static void
3581 layout_nonempty_base_or_field (record_layout_info rli,
3582 tree decl,
3583 tree binfo,
3584 splay_tree offsets)
3586 tree t = rli->t;
3587 tree offset = NULL_TREE;
3588 bool field_p;
3589 tree type;
3591 if (binfo)
3593 /* For the purposes of determining layout conflicts, we want to
3594 use the class type of BINFO; TREE_TYPE (DECL) will be the
3595 CLASSTYPE_AS_BASE version, which does not contain entries for
3596 zero-sized bases. */
3597 type = TREE_TYPE (binfo);
3598 field_p = false;
3600 else
3602 type = TREE_TYPE (decl);
3603 field_p = true;
3606 /* Try to place the field. It may take more than one try if we have
3607 a hard time placing the field without putting two objects of the
3608 same type at the same address. */
3609 while (1)
3611 struct record_layout_info_s old_rli = *rli;
3613 /* Place this field. */
3614 place_field (rli, decl);
3615 offset = byte_position (decl);
3617 /* We have to check to see whether or not there is already
3618 something of the same type at the offset we're about to use.
3619 For example:
3621 struct S {};
3622 struct T : public S { int i; };
3623 struct U : public S, public T {};
3625 Here, we put S at offset zero in U. Then, we can't put T at
3626 offset zero -- its S component would be at the same address
3627 as the S we already allocated. So, we have to skip ahead.
3628 Since all data members, including those whose type is an
3629 empty class, have nonzero size, any overlap can happen only
3630 with a direct or indirect base-class -- it can't happen with
3631 a data member. */
3632 /* G++ 3.2 did not check for overlaps when placing a non-empty
3633 virtual base. */
3634 if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3635 break;
3636 if (layout_conflict_p (type, offset, offsets, field_p))
3638 /* Strip off the size allocated to this field. That puts us
3639 at the first place we could have put the field with
3640 proper alignment. */
3641 *rli = old_rli;
3643 /* Bump up by the alignment required for the type. */
3644 rli->bitpos
3645 = size_binop (PLUS_EXPR, rli->bitpos,
3646 bitsize_int (binfo
3647 ? CLASSTYPE_ALIGN (type)
3648 : TYPE_ALIGN (type)));
3649 normalize_rli (rli);
3651 else
3652 /* There was no conflict. We're done laying out this field. */
3653 break;
3656 /* Now that we know where it will be placed, update its
3657 BINFO_OFFSET. */
3658 if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3659 /* Indirect virtual bases may have a non-zero BINFO_OFFSET at
3660 this point because their BINFO_OFFSET is copied from another
3661 hierarchy. Therefore, we may not need to add the entire
3662 OFFSET. */
3663 propagate_binfo_offsets (binfo,
3664 size_diffop (convert (ssizetype, offset),
3665 convert (ssizetype,
3666 BINFO_OFFSET (binfo))),
3670 /* Returns true if TYPE is empty and OFFSET is non-zero. */
3672 static int
3673 empty_base_at_nonzero_offset_p (tree type,
3674 tree offset,
3675 splay_tree offsets ATTRIBUTE_UNUSED)
3677 return is_empty_class (type) && !integer_zerop (offset);
3680 /* Layout the empty base BINFO. EOC indicates the byte currently just
3681 past the end of the class, and should be correctly aligned for a
3682 class of the type indicated by BINFO; OFFSETS gives the offsets of
3683 the empty bases allocated so far. T is the most derived
3684 type. Return nonzero iff we added it at the end. */
3686 static bool
3687 layout_empty_base (binfo, eoc, offsets, t)
3688 tree binfo;
3689 tree eoc;
3690 splay_tree offsets;
3691 tree t;
3693 tree alignment;
3694 tree basetype = BINFO_TYPE (binfo);
3695 bool atend = false;
3697 /* This routine should only be used for empty classes. */
3698 my_friendly_assert (is_empty_class (basetype), 20000321);
3699 alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3701 if (abi_version_at_least (2))
3702 BINFO_OFFSET (binfo) = size_zero_node;
3703 if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3704 warning ("offset of empty base `%T' may not be ABI-compliant and may"
3705 "change in a future version of GCC",
3706 BINFO_TYPE (binfo));
3708 /* This is an empty base class. We first try to put it at offset
3709 zero. */
3710 if (layout_conflict_p (BINFO_TYPE (binfo),
3711 BINFO_OFFSET (binfo),
3712 offsets,
3713 /*vbases_p=*/0))
3715 /* That didn't work. Now, we move forward from the next
3716 available spot in the class. */
3717 atend = true;
3718 propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3719 while (1)
3721 if (!layout_conflict_p (BINFO_TYPE (binfo),
3722 BINFO_OFFSET (binfo),
3723 offsets,
3724 /*vbases_p=*/0))
3725 /* We finally found a spot where there's no overlap. */
3726 break;
3728 /* There's overlap here, too. Bump along to the next spot. */
3729 propagate_binfo_offsets (binfo, alignment, t);
3732 return atend;
3735 /* Layout the the base given by BINFO in the class indicated by RLI.
3736 *BASE_ALIGN is a running maximum of the alignments of
3737 any base class. OFFSETS gives the location of empty base
3738 subobjects. T is the most derived type. Return nonzero if the new
3739 object cannot be nearly-empty. A new FIELD_DECL is inserted at
3740 *NEXT_FIELD, unless BINFO is for an empty base class.
3742 Returns the location at which the next field should be inserted. */
3744 static tree *
3745 build_base_field (record_layout_info rli, tree binfo,
3746 splay_tree offsets, tree *next_field)
3748 tree t = rli->t;
3749 tree basetype = BINFO_TYPE (binfo);
3751 if (!COMPLETE_TYPE_P (basetype))
3752 /* This error is now reported in xref_tag, thus giving better
3753 location information. */
3754 return next_field;
3756 /* Place the base class. */
3757 if (!is_empty_class (basetype))
3759 tree decl;
3761 /* The containing class is non-empty because it has a non-empty
3762 base class. */
3763 CLASSTYPE_EMPTY_P (t) = 0;
3765 /* Create the FIELD_DECL. */
3766 decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3767 DECL_ARTIFICIAL (decl) = 1;
3768 DECL_FIELD_CONTEXT (decl) = t;
3769 DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3770 DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3771 DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3772 DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3773 /* Tell the backend not to round up to TYPE_ALIGN. */
3774 DECL_PACKED (decl) = 1;
3776 /* Try to place the field. It may take more than one try if we
3777 have a hard time placing the field without putting two
3778 objects of the same type at the same address. */
3779 layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3780 /* Add the new FIELD_DECL to the list of fields for T. */
3781 TREE_CHAIN (decl) = *next_field;
3782 *next_field = decl;
3783 next_field = &TREE_CHAIN (decl);
3785 else
3787 tree eoc;
3788 bool atend;
3790 /* On some platforms (ARM), even empty classes will not be
3791 byte-aligned. */
3792 eoc = round_up (rli_size_unit_so_far (rli),
3793 CLASSTYPE_ALIGN_UNIT (basetype));
3794 atend = layout_empty_base (binfo, eoc, offsets, t);
3795 /* A nearly-empty class "has no proper base class that is empty,
3796 not morally virtual, and at an offset other than zero." */
3797 if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3799 if (atend)
3800 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3801 /* The check above (used in G++ 3.2) is insufficient because
3802 an empty class placed at offset zero might itself have an
3803 empty base at a non-zero offset. */
3804 else if (walk_subobject_offsets (basetype,
3805 empty_base_at_nonzero_offset_p,
3806 size_zero_node,
3807 /*offsets=*/NULL,
3808 /*max_offset=*/NULL_TREE,
3809 /*vbases_p=*/true))
3811 if (abi_version_at_least (2))
3812 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3813 else if (warn_abi)
3814 warning ("class `%T' will be considered nearly empty in a "
3815 "future version of GCC", t);
3819 /* We do not create a FIELD_DECL for empty base classes because
3820 it might overlap some other field. We want to be able to
3821 create CONSTRUCTORs for the class by iterating over the
3822 FIELD_DECLs, and the back end does not handle overlapping
3823 FIELD_DECLs. */
3825 /* An empty virtual base causes a class to be non-empty
3826 -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3827 here because that was already done when the virtual table
3828 pointer was created. */
3831 /* Record the offsets of BINFO and its base subobjects. */
3832 record_subobject_offsets (BINFO_TYPE (binfo),
3833 BINFO_OFFSET (binfo),
3834 offsets,
3835 /*vbases_p=*/0);
3837 return next_field;
3840 /* Layout all of the non-virtual base classes. Record empty
3841 subobjects in OFFSETS. T is the most derived type. Return nonzero
3842 if the type cannot be nearly empty. The fields created
3843 corresponding to the base classes will be inserted at
3844 *NEXT_FIELD. */
3846 static void
3847 build_base_fields (record_layout_info rli,
3848 splay_tree offsets, tree *next_field)
3850 /* Chain to hold all the new FIELD_DECLs which stand in for base class
3851 subobjects. */
3852 tree t = rli->t;
3853 int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
3854 int i;
3856 /* The primary base class is always allocated first. */
3857 if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
3858 next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
3859 offsets, next_field);
3861 /* Now allocate the rest of the bases. */
3862 for (i = 0; i < n_baseclasses; ++i)
3864 tree base_binfo;
3866 base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
3868 /* The primary base was already allocated above, so we don't
3869 need to allocate it again here. */
3870 if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
3871 continue;
3873 /* A primary virtual base class is allocated just like any other
3874 base class, but a non-primary virtual base is allocated
3875 later, in layout_virtual_bases. */
3876 if (TREE_VIA_VIRTUAL (base_binfo)
3877 && !BINFO_PRIMARY_P (base_binfo))
3878 continue;
3880 next_field = build_base_field (rli, base_binfo,
3881 offsets, next_field);
3885 /* Go through the TYPE_METHODS of T issuing any appropriate
3886 diagnostics, figuring out which methods override which other
3887 methods, and so forth. */
3889 static void
3890 check_methods (t)
3891 tree t;
3893 tree x;
3895 for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3897 /* If this was an evil function, don't keep it in class. */
3898 if (DECL_ASSEMBLER_NAME_SET_P (x)
3899 && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3900 continue;
3902 check_for_override (x, t);
3903 if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3904 cp_error_at ("initializer specified for non-virtual method `%D'", x);
3906 /* The name of the field is the original field name
3907 Save this in auxiliary field for later overloading. */
3908 if (DECL_VINDEX (x))
3910 TYPE_POLYMORPHIC_P (t) = 1;
3911 if (DECL_PURE_VIRTUAL_P (x))
3912 CLASSTYPE_PURE_VIRTUALS (t)
3913 = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
3918 /* FN is a constructor or destructor. Clone the declaration to create
3919 a specialized in-charge or not-in-charge version, as indicated by
3920 NAME. */
3922 static tree
3923 build_clone (fn, name)
3924 tree fn;
3925 tree name;
3927 tree parms;
3928 tree clone;
3930 /* Copy the function. */
3931 clone = copy_decl (fn);
3932 /* Remember where this function came from. */
3933 DECL_CLONED_FUNCTION (clone) = fn;
3934 DECL_ABSTRACT_ORIGIN (clone) = fn;
3935 /* Reset the function name. */
3936 DECL_NAME (clone) = name;
3937 SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
3938 /* There's no pending inline data for this function. */
3939 DECL_PENDING_INLINE_INFO (clone) = NULL;
3940 DECL_PENDING_INLINE_P (clone) = 0;
3941 /* And it hasn't yet been deferred. */
3942 DECL_DEFERRED_FN (clone) = 0;
3944 /* The base-class destructor is not virtual. */
3945 if (name == base_dtor_identifier)
3947 DECL_VIRTUAL_P (clone) = 0;
3948 if (TREE_CODE (clone) != TEMPLATE_DECL)
3949 DECL_VINDEX (clone) = NULL_TREE;
3952 /* If there was an in-charge parameter, drop it from the function
3953 type. */
3954 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3956 tree basetype;
3957 tree parmtypes;
3958 tree exceptions;
3960 exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
3961 basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
3962 parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
3963 /* Skip the `this' parameter. */
3964 parmtypes = TREE_CHAIN (parmtypes);
3965 /* Skip the in-charge parameter. */
3966 parmtypes = TREE_CHAIN (parmtypes);
3967 /* And the VTT parm, in a complete [cd]tor. */
3968 if (DECL_HAS_VTT_PARM_P (fn)
3969 && ! DECL_NEEDS_VTT_PARM_P (clone))
3970 parmtypes = TREE_CHAIN (parmtypes);
3971 /* If this is subobject constructor or destructor, add the vtt
3972 parameter. */
3973 TREE_TYPE (clone)
3974 = build_cplus_method_type (basetype,
3975 TREE_TYPE (TREE_TYPE (clone)),
3976 parmtypes);
3977 if (exceptions)
3978 TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
3979 exceptions);
3982 /* Copy the function parameters. But, DECL_ARGUMENTS on a TEMPLATE_DECL
3983 aren't function parameters; those are the template parameters. */
3984 if (TREE_CODE (clone) != TEMPLATE_DECL)
3986 DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
3987 /* Remove the in-charge parameter. */
3988 if (DECL_HAS_IN_CHARGE_PARM_P (clone))
3990 TREE_CHAIN (DECL_ARGUMENTS (clone))
3991 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
3992 DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
3994 /* And the VTT parm, in a complete [cd]tor. */
3995 if (DECL_HAS_VTT_PARM_P (fn))
3997 if (DECL_NEEDS_VTT_PARM_P (clone))
3998 DECL_HAS_VTT_PARM_P (clone) = 1;
3999 else
4001 TREE_CHAIN (DECL_ARGUMENTS (clone))
4002 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4003 DECL_HAS_VTT_PARM_P (clone) = 0;
4007 for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4009 DECL_CONTEXT (parms) = clone;
4010 cxx_dup_lang_specific_decl (parms);
4014 /* Create the RTL for this function. */
4015 SET_DECL_RTL (clone, NULL_RTX);
4016 rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4018 /* Make it easy to find the CLONE given the FN. */
4019 TREE_CHAIN (clone) = TREE_CHAIN (fn);
4020 TREE_CHAIN (fn) = clone;
4022 /* If this is a template, handle the DECL_TEMPLATE_RESULT as well. */
4023 if (TREE_CODE (clone) == TEMPLATE_DECL)
4025 tree result;
4027 DECL_TEMPLATE_RESULT (clone)
4028 = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4029 result = DECL_TEMPLATE_RESULT (clone);
4030 DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4031 DECL_TI_TEMPLATE (result) = clone;
4033 else if (DECL_DEFERRED_FN (fn))
4034 defer_fn (clone);
4036 return clone;
4039 /* Produce declarations for all appropriate clones of FN. If
4040 UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4041 CLASTYPE_METHOD_VEC as well. */
4043 void
4044 clone_function_decl (fn, update_method_vec_p)
4045 tree fn;
4046 int update_method_vec_p;
4048 tree clone;
4050 /* Avoid inappropriate cloning. */
4051 if (TREE_CHAIN (fn)
4052 && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4053 return;
4055 if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4057 /* For each constructor, we need two variants: an in-charge version
4058 and a not-in-charge version. */
4059 clone = build_clone (fn, complete_ctor_identifier);
4060 if (update_method_vec_p)
4061 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4062 clone = build_clone (fn, base_ctor_identifier);
4063 if (update_method_vec_p)
4064 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4066 else
4068 my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4070 /* For each destructor, we need three variants: an in-charge
4071 version, a not-in-charge version, and an in-charge deleting
4072 version. We clone the deleting version first because that
4073 means it will go second on the TYPE_METHODS list -- and that
4074 corresponds to the correct layout order in the virtual
4075 function table.
4077 For a non-virtual destructor, we do not build a deleting
4078 destructor. */
4079 if (DECL_VIRTUAL_P (fn))
4081 clone = build_clone (fn, deleting_dtor_identifier);
4082 if (update_method_vec_p)
4083 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4085 clone = build_clone (fn, complete_dtor_identifier);
4086 if (update_method_vec_p)
4087 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4088 clone = build_clone (fn, base_dtor_identifier);
4089 if (update_method_vec_p)
4090 add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4093 /* Note that this is an abstract function that is never emitted. */
4094 DECL_ABSTRACT (fn) = 1;
4097 /* DECL is an in charge constructor, which is being defined. This will
4098 have had an in class declaration, from whence clones were
4099 declared. An out-of-class definition can specify additional default
4100 arguments. As it is the clones that are involved in overload
4101 resolution, we must propagate the information from the DECL to its
4102 clones. */
4104 void
4105 adjust_clone_args (decl)
4106 tree decl;
4108 tree clone;
4110 for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4111 clone = TREE_CHAIN (clone))
4113 tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4114 tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4115 tree decl_parms, clone_parms;
4117 clone_parms = orig_clone_parms;
4119 /* Skip the 'this' parameter. */
4120 orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4121 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4123 if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4124 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4125 if (DECL_HAS_VTT_PARM_P (decl))
4126 orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4128 clone_parms = orig_clone_parms;
4129 if (DECL_HAS_VTT_PARM_P (clone))
4130 clone_parms = TREE_CHAIN (clone_parms);
4132 for (decl_parms = orig_decl_parms; decl_parms;
4133 decl_parms = TREE_CHAIN (decl_parms),
4134 clone_parms = TREE_CHAIN (clone_parms))
4136 my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4137 TREE_TYPE (clone_parms)), 20010424);
4139 if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4141 /* A default parameter has been added. Adjust the
4142 clone's parameters. */
4143 tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4144 tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4145 tree type;
4147 clone_parms = orig_decl_parms;
4149 if (DECL_HAS_VTT_PARM_P (clone))
4151 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4152 TREE_VALUE (orig_clone_parms),
4153 clone_parms);
4154 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4156 type = build_cplus_method_type (basetype,
4157 TREE_TYPE (TREE_TYPE (clone)),
4158 clone_parms);
4159 if (exceptions)
4160 type = build_exception_variant (type, exceptions);
4161 TREE_TYPE (clone) = type;
4163 clone_parms = NULL_TREE;
4164 break;
4167 my_friendly_assert (!clone_parms, 20010424);
4171 /* For each of the constructors and destructors in T, create an
4172 in-charge and not-in-charge variant. */
4174 static void
4175 clone_constructors_and_destructors (t)
4176 tree t;
4178 tree fns;
4180 /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4181 out now. */
4182 if (!CLASSTYPE_METHOD_VEC (t))
4183 return;
4185 for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4186 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4187 for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4188 clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4191 /* Remove all zero-width bit-fields from T. */
4193 static void
4194 remove_zero_width_bit_fields (t)
4195 tree t;
4197 tree *fieldsp;
4199 fieldsp = &TYPE_FIELDS (t);
4200 while (*fieldsp)
4202 if (TREE_CODE (*fieldsp) == FIELD_DECL
4203 && DECL_C_BIT_FIELD (*fieldsp)
4204 && DECL_INITIAL (*fieldsp))
4205 *fieldsp = TREE_CHAIN (*fieldsp);
4206 else
4207 fieldsp = &TREE_CHAIN (*fieldsp);
4211 /* Returns TRUE iff we need a cookie when dynamically allocating an
4212 array whose elements have the indicated class TYPE. */
4214 static bool
4215 type_requires_array_cookie (type)
4216 tree type;
4218 tree fns;
4219 bool has_two_argument_delete_p = false;
4221 my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4223 /* If there's a non-trivial destructor, we need a cookie. In order
4224 to iterate through the array calling the destructor for each
4225 element, we'll have to know how many elements there are. */
4226 if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4227 return true;
4229 /* If the usual deallocation function is a two-argument whose second
4230 argument is of type `size_t', then we have to pass the size of
4231 the array to the deallocation function, so we will need to store
4232 a cookie. */
4233 fns = lookup_fnfields (TYPE_BINFO (type),
4234 ansi_opname (VEC_DELETE_EXPR),
4235 /*protect=*/0);
4236 /* If there are no `operator []' members, or the lookup is
4237 ambiguous, then we don't need a cookie. */
4238 if (!fns || fns == error_mark_node)
4239 return false;
4240 /* Loop through all of the functions. */
4241 for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4243 tree fn;
4244 tree second_parm;
4246 /* Select the current function. */
4247 fn = OVL_CURRENT (fns);
4248 /* See if this function is a one-argument delete function. If
4249 it is, then it will be the usual deallocation function. */
4250 second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4251 if (second_parm == void_list_node)
4252 return false;
4253 /* Otherwise, if we have a two-argument function and the second
4254 argument is `size_t', it will be the usual deallocation
4255 function -- unless there is one-argument function, too. */
4256 if (TREE_CHAIN (second_parm) == void_list_node
4257 && same_type_p (TREE_VALUE (second_parm), sizetype))
4258 has_two_argument_delete_p = true;
4261 return has_two_argument_delete_p;
4264 /* Check the validity of the bases and members declared in T. Add any
4265 implicitly-generated functions (like copy-constructors and
4266 assignment operators). Compute various flag bits (like
4267 CLASSTYPE_NON_POD_T) for T. This routine works purely at the C++
4268 level: i.e., independently of the ABI in use. */
4270 static void
4271 check_bases_and_members (tree t)
4273 /* Nonzero if we are not allowed to generate a default constructor
4274 for this case. */
4275 int cant_have_default_ctor;
4276 /* Nonzero if the implicitly generated copy constructor should take
4277 a non-const reference argument. */
4278 int cant_have_const_ctor;
4279 /* Nonzero if the the implicitly generated assignment operator
4280 should take a non-const reference argument. */
4281 int no_const_asn_ref;
4282 tree access_decls;
4284 /* By default, we use const reference arguments and generate default
4285 constructors. */
4286 cant_have_default_ctor = 0;
4287 cant_have_const_ctor = 0;
4288 no_const_asn_ref = 0;
4290 /* Check all the base-classes. */
4291 check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4292 &no_const_asn_ref);
4294 /* Check all the data member declarations. */
4295 check_field_decls (t, &access_decls,
4296 &cant_have_default_ctor,
4297 &cant_have_const_ctor,
4298 &no_const_asn_ref);
4300 /* Check all the method declarations. */
4301 check_methods (t);
4303 /* A nearly-empty class has to be vptr-containing; a nearly empty
4304 class contains just a vptr. */
4305 if (!TYPE_CONTAINS_VPTR_P (t))
4306 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4308 /* Do some bookkeeping that will guide the generation of implicitly
4309 declared member functions. */
4310 TYPE_HAS_COMPLEX_INIT_REF (t)
4311 |= (TYPE_HAS_INIT_REF (t)
4312 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4313 || TYPE_POLYMORPHIC_P (t));
4314 TYPE_NEEDS_CONSTRUCTING (t)
4315 |= (TYPE_HAS_CONSTRUCTOR (t)
4316 || TYPE_USES_VIRTUAL_BASECLASSES (t)
4317 || TYPE_POLYMORPHIC_P (t));
4318 CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4319 || TYPE_POLYMORPHIC_P (t));
4320 CLASSTYPE_NON_POD_P (t)
4321 |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t)
4322 || TYPE_HAS_ASSIGN_REF (t));
4323 TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4324 TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4325 |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4327 /* Synthesize any needed methods. Note that methods will be synthesized
4328 for anonymous unions; grok_x_components undoes that. */
4329 add_implicitly_declared_members (t, cant_have_default_ctor,
4330 cant_have_const_ctor,
4331 no_const_asn_ref);
4333 /* Create the in-charge and not-in-charge variants of constructors
4334 and destructors. */
4335 clone_constructors_and_destructors (t);
4337 /* Process the using-declarations. */
4338 for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4339 handle_using_decl (TREE_VALUE (access_decls), t);
4341 /* Build and sort the CLASSTYPE_METHOD_VEC. */
4342 finish_struct_methods (t);
4344 /* Figure out whether or not we will need a cookie when dynamically
4345 allocating an array of this type. */
4346 TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4347 = type_requires_array_cookie (t);
4350 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4351 accordingly. If a new vfield was created (because T doesn't have a
4352 primary base class), then the newly created field is returned. It
4353 is not added to the TYPE_FIELDS list; it is the caller's
4354 responsibility to do that. Accumulate declared virtual functions
4355 on VIRTUALS_P. */
4357 static tree
4358 create_vtable_ptr (t, virtuals_p)
4359 tree t;
4360 tree *virtuals_p;
4362 tree fn;
4364 /* Collect the virtual functions declared in T. */
4365 for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4366 if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4367 && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4369 tree new_virtual = make_node (TREE_LIST);
4371 BV_FN (new_virtual) = fn;
4372 BV_DELTA (new_virtual) = integer_zero_node;
4374 TREE_CHAIN (new_virtual) = *virtuals_p;
4375 *virtuals_p = new_virtual;
4378 /* If we couldn't find an appropriate base class, create a new field
4379 here. Even if there weren't any new virtual functions, we might need a
4380 new virtual function table if we're supposed to include vptrs in
4381 all classes that need them. */
4382 if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4384 /* We build this decl with vtbl_ptr_type_node, which is a
4385 `vtable_entry_type*'. It might seem more precise to use
4386 `vtable_entry_type (*)[N]' where N is the number of firtual
4387 functions. However, that would require the vtable pointer in
4388 base classes to have a different type than the vtable pointer
4389 in derived classes. We could make that happen, but that
4390 still wouldn't solve all the problems. In particular, the
4391 type-based alias analysis code would decide that assignments
4392 to the base class vtable pointer can't alias assignments to
4393 the derived class vtable pointer, since they have different
4394 types. Thus, in an derived class destructor, where the base
4395 class constructor was inlined, we could generate bad code for
4396 setting up the vtable pointer.
4398 Therefore, we use one type for all vtable pointers. We still
4399 use a type-correct type; it's just doesn't indicate the array
4400 bounds. That's better than using `void*' or some such; it's
4401 cleaner, and it let's the alias analysis code know that these
4402 stores cannot alias stores to void*! */
4403 tree field;
4405 field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4406 SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4407 DECL_VIRTUAL_P (field) = 1;
4408 DECL_ARTIFICIAL (field) = 1;
4409 DECL_FIELD_CONTEXT (field) = t;
4410 DECL_FCONTEXT (field) = t;
4411 DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4412 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4414 TYPE_VFIELD (t) = field;
4416 /* This class is non-empty. */
4417 CLASSTYPE_EMPTY_P (t) = 0;
4419 if (CLASSTYPE_N_BASECLASSES (t))
4420 /* If there were any baseclasses, they can't possibly be at
4421 offset zero any more, because that's where the vtable
4422 pointer is. So, converting to a base class is going to
4423 take work. */
4424 TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4426 return field;
4429 return NULL_TREE;
4432 /* Fixup the inline function given by INFO now that the class is
4433 complete. */
4435 static void
4436 fixup_pending_inline (fn)
4437 tree fn;
4439 if (DECL_PENDING_INLINE_INFO (fn))
4441 tree args = DECL_ARGUMENTS (fn);
4442 while (args)
4444 DECL_CONTEXT (args) = fn;
4445 args = TREE_CHAIN (args);
4450 /* Fixup the inline methods and friends in TYPE now that TYPE is
4451 complete. */
4453 static void
4454 fixup_inline_methods (type)
4455 tree type;
4457 tree method = TYPE_METHODS (type);
4459 if (method && TREE_CODE (method) == TREE_VEC)
4461 if (TREE_VEC_ELT (method, 1))
4462 method = TREE_VEC_ELT (method, 1);
4463 else if (TREE_VEC_ELT (method, 0))
4464 method = TREE_VEC_ELT (method, 0);
4465 else
4466 method = TREE_VEC_ELT (method, 2);
4469 /* Do inline member functions. */
4470 for (; method; method = TREE_CHAIN (method))
4471 fixup_pending_inline (method);
4473 /* Do friends. */
4474 for (method = CLASSTYPE_INLINE_FRIENDS (type);
4475 method;
4476 method = TREE_CHAIN (method))
4477 fixup_pending_inline (TREE_VALUE (method));
4478 CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4481 /* Add OFFSET to all base types of BINFO which is a base in the
4482 hierarchy dominated by T.
4484 OFFSET, which is a type offset, is number of bytes. */
4486 static void
4487 propagate_binfo_offsets (binfo, offset, t)
4488 tree binfo;
4489 tree offset;
4490 tree t;
4492 int i;
4493 tree primary_binfo;
4495 /* Update BINFO's offset. */
4496 BINFO_OFFSET (binfo)
4497 = convert (sizetype,
4498 size_binop (PLUS_EXPR,
4499 convert (ssizetype, BINFO_OFFSET (binfo)),
4500 offset));
4502 /* Find the primary base class. */
4503 primary_binfo = get_primary_binfo (binfo);
4505 /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4506 downwards. */
4507 for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4509 tree base_binfo;
4511 /* On the first time through the loop, do the primary base.
4512 Because the primary base need not be an immediate base, we
4513 must handle the primary base specially. */
4514 if (i == -1)
4516 if (!primary_binfo)
4517 continue;
4519 base_binfo = primary_binfo;
4521 else
4523 base_binfo = BINFO_BASETYPE (binfo, i);
4524 /* Don't do the primary base twice. */
4525 if (base_binfo == primary_binfo)
4526 continue;
4529 /* Skip virtual bases that aren't our canonical primary base. */
4530 if (TREE_VIA_VIRTUAL (base_binfo)
4531 && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4532 || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4533 continue;
4535 propagate_binfo_offsets (base_binfo, offset, t);
4539 /* Called via dfs_walk from layout_virtual bases. */
4541 static tree
4542 dfs_set_offset_for_unshared_vbases (binfo, data)
4543 tree binfo;
4544 void *data;
4546 /* If this is a virtual base, make sure it has the same offset as
4547 the shared copy. If it's a primary base, then we know it's
4548 correct. */
4549 if (TREE_VIA_VIRTUAL (binfo))
4551 tree t = (tree) data;
4552 tree vbase;
4553 tree offset;
4555 vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4556 if (vbase != binfo)
4558 offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4559 propagate_binfo_offsets (binfo, offset, t);
4563 return NULL_TREE;
4566 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T. Update
4567 TYPE_ALIGN and TYPE_SIZE for T. OFFSETS gives the location of
4568 empty subobjects of T. */
4570 static void
4571 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4573 tree vbases;
4574 tree t = rli->t;
4575 bool first_vbase = true;
4576 tree *next_field;
4578 if (CLASSTYPE_N_BASECLASSES (t) == 0)
4579 return;
4581 if (!abi_version_at_least(2))
4583 /* In G++ 3.2, we incorrectly rounded the size before laying out
4584 the virtual bases. */
4585 finish_record_layout (rli, /*free_p=*/false);
4586 #ifdef STRUCTURE_SIZE_BOUNDARY
4587 /* Packed structures don't need to have minimum size. */
4588 if (! TYPE_PACKED (t))
4589 TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4590 #endif
4591 rli->offset = TYPE_SIZE_UNIT (t);
4592 rli->bitpos = bitsize_zero_node;
4593 rli->record_align = TYPE_ALIGN (t);
4596 /* Find the last field. The artificial fields created for virtual
4597 bases will go after the last extant field to date. */
4598 next_field = &TYPE_FIELDS (t);
4599 while (*next_field)
4600 next_field = &TREE_CHAIN (*next_field);
4602 /* Go through the virtual bases, allocating space for each virtual
4603 base that is not already a primary base class. These are
4604 allocated in inheritance graph order. */
4605 for (vbases = TYPE_BINFO (t);
4606 vbases;
4607 vbases = TREE_CHAIN (vbases))
4609 tree vbase;
4611 if (!TREE_VIA_VIRTUAL (vbases))
4612 continue;
4614 vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4616 if (!BINFO_PRIMARY_P (vbase))
4618 tree basetype = TREE_TYPE (vbase);
4620 /* This virtual base is not a primary base of any class in the
4621 hierarchy, so we have to add space for it. */
4622 next_field = build_base_field (rli, vbase,
4623 offsets, next_field);
4625 /* If the first virtual base might have been placed at a
4626 lower address, had we started from CLASSTYPE_SIZE, rather
4627 than TYPE_SIZE, issue a warning. There can be both false
4628 positives and false negatives from this warning in rare
4629 cases; to deal with all the possibilities would probably
4630 require performing both layout algorithms and comparing
4631 the results which is not particularly tractable. */
4632 if (warn_abi
4633 && first_vbase
4634 && (tree_int_cst_lt
4635 (size_binop (CEIL_DIV_EXPR,
4636 round_up (CLASSTYPE_SIZE (t),
4637 CLASSTYPE_ALIGN (basetype)),
4638 bitsize_unit_node),
4639 BINFO_OFFSET (vbase))))
4640 warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4641 basetype);
4643 first_vbase = false;
4647 /* Now, go through the TYPE_BINFO hierarchy, setting the
4648 BINFO_OFFSETs correctly for all non-primary copies of the virtual
4649 bases and their direct and indirect bases. The ambiguity checks
4650 in lookup_base depend on the BINFO_OFFSETs being set
4651 correctly. */
4652 dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4655 /* Returns the offset of the byte just past the end of the base class
4656 BINFO. */
4658 static tree
4659 end_of_base (tree binfo)
4661 tree size;
4663 if (is_empty_class (BINFO_TYPE (binfo)))
4664 /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4665 allocate some space for it. It cannot have virtual bases, so
4666 TYPE_SIZE_UNIT is fine. */
4667 size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4668 else
4669 size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4671 return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4674 /* Returns the offset of the byte just past the end of the base class
4675 with the highest offset in T. If INCLUDE_VIRTUALS_P is zero, then
4676 only non-virtual bases are included. */
4678 static tree
4679 end_of_class (t, include_virtuals_p)
4680 tree t;
4681 int include_virtuals_p;
4683 tree result = size_zero_node;
4684 tree binfo;
4685 tree offset;
4686 int i;
4688 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4690 binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4692 if (!include_virtuals_p
4693 && TREE_VIA_VIRTUAL (binfo)
4694 && !BINFO_PRIMARY_P (binfo))
4695 continue;
4697 offset = end_of_base (binfo);
4698 if (INT_CST_LT_UNSIGNED (result, offset))
4699 result = offset;
4702 /* G++ 3.2 did not check indirect virtual bases. */
4703 if (abi_version_at_least (2) && include_virtuals_p)
4704 for (binfo = CLASSTYPE_VBASECLASSES (t);
4705 binfo;
4706 binfo = TREE_CHAIN (binfo))
4708 offset = end_of_base (TREE_VALUE (binfo));
4709 if (INT_CST_LT_UNSIGNED (result, offset))
4710 result = offset;
4713 return result;
4716 /* Warn about bases of T that are inaccessible because they are
4717 ambiguous. For example:
4719 struct S {};
4720 struct T : public S {};
4721 struct U : public S, public T {};
4723 Here, `(S*) new U' is not allowed because there are two `S'
4724 subobjects of U. */
4726 static void
4727 warn_about_ambiguous_bases (t)
4728 tree t;
4730 int i;
4731 tree vbases;
4732 tree basetype;
4734 /* Check direct bases. */
4735 for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4737 basetype = TYPE_BINFO_BASETYPE (t, i);
4739 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4740 warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4741 basetype, t);
4744 /* Check for ambiguous virtual bases. */
4745 if (extra_warnings)
4746 for (vbases = CLASSTYPE_VBASECLASSES (t);
4747 vbases;
4748 vbases = TREE_CHAIN (vbases))
4750 basetype = BINFO_TYPE (TREE_VALUE (vbases));
4752 if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4753 warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4754 basetype, t);
4758 /* Compare two INTEGER_CSTs K1 and K2. */
4760 static int
4761 splay_tree_compare_integer_csts (k1, k2)
4762 splay_tree_key k1;
4763 splay_tree_key k2;
4765 return tree_int_cst_compare ((tree) k1, (tree) k2);
4768 /* Increase the size indicated in RLI to account for empty classes
4769 that are "off the end" of the class. */
4771 static void
4772 include_empty_classes (record_layout_info rli)
4774 tree eoc;
4775 tree rli_size;
4777 /* It might be the case that we grew the class to allocate a
4778 zero-sized base class. That won't be reflected in RLI, yet,
4779 because we are willing to overlay multiple bases at the same
4780 offset. However, now we need to make sure that RLI is big enough
4781 to reflect the entire class. */
4782 eoc = end_of_class (rli->t,
4783 CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4784 rli_size = rli_size_unit_so_far (rli);
4785 if (TREE_CODE (rli_size) == INTEGER_CST
4786 && INT_CST_LT_UNSIGNED (rli_size, eoc))
4788 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4789 rli->bitpos
4790 = size_binop (PLUS_EXPR,
4791 rli->bitpos,
4792 size_binop (MULT_EXPR,
4793 convert (bitsizetype,
4794 size_binop (MINUS_EXPR,
4795 eoc, rli_size)),
4796 bitsize_int (BITS_PER_UNIT)));
4797 normalize_rli (rli);
4801 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T. Calculate
4802 BINFO_OFFSETs for all of the base-classes. Position the vtable
4803 pointer. Accumulate declared virtual functions on VIRTUALS_P. */
4805 static void
4806 layout_class_type (tree t, tree *virtuals_p)
4808 tree non_static_data_members;
4809 tree field;
4810 tree vptr;
4811 record_layout_info rli;
4812 /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4813 types that appear at that offset. */
4814 splay_tree empty_base_offsets;
4815 /* True if the last field layed out was a bit-field. */
4816 bool last_field_was_bitfield = false;
4817 /* The location at which the next field should be inserted. */
4818 tree *next_field;
4819 /* T, as a base class. */
4820 tree base_t;
4822 /* Keep track of the first non-static data member. */
4823 non_static_data_members = TYPE_FIELDS (t);
4825 /* Start laying out the record. */
4826 rli = start_record_layout (t);
4828 /* If possible, we reuse the virtual function table pointer from one
4829 of our base classes. */
4830 determine_primary_base (t);
4832 /* Create a pointer to our virtual function table. */
4833 vptr = create_vtable_ptr (t, virtuals_p);
4835 /* The vptr is always the first thing in the class. */
4836 if (vptr)
4838 TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4839 TYPE_FIELDS (t) = vptr;
4840 next_field = &TREE_CHAIN (vptr);
4841 place_field (rli, vptr);
4843 else
4844 next_field = &TYPE_FIELDS (t);
4846 /* Build FIELD_DECLs for all of the non-virtual base-types. */
4847 empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
4848 NULL, NULL);
4849 build_base_fields (rli, empty_base_offsets, next_field);
4851 /* Layout the non-static data members. */
4852 for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4854 tree type;
4855 tree padding;
4857 /* We still pass things that aren't non-static data members to
4858 the back-end, in case it wants to do something with them. */
4859 if (TREE_CODE (field) != FIELD_DECL)
4861 place_field (rli, field);
4862 /* If the static data member has incomplete type, keep track
4863 of it so that it can be completed later. (The handling
4864 of pending statics in finish_record_layout is
4865 insufficient; consider:
4867 struct S1;
4868 struct S2 { static S1 s1; };
4870 At this point, finish_record_layout will be called, but
4871 S1 is still incomplete.) */
4872 if (TREE_CODE (field) == VAR_DECL)
4873 maybe_register_incomplete_var (field);
4874 continue;
4877 type = TREE_TYPE (field);
4879 /* If this field is a bit-field whose width is greater than its
4880 type, then there are some special rules for allocating
4881 it. */
4882 if (DECL_C_BIT_FIELD (field)
4883 && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4885 integer_type_kind itk;
4886 tree integer_type;
4888 /* We must allocate the bits as if suitably aligned for the
4889 longest integer type that fits in this many bits. type
4890 of the field. Then, we are supposed to use the left over
4891 bits as additional padding. */
4892 for (itk = itk_char; itk != itk_none; ++itk)
4893 if (INT_CST_LT (DECL_SIZE (field),
4894 TYPE_SIZE (integer_types[itk])))
4895 break;
4897 /* ITK now indicates a type that is too large for the
4898 field. We have to back up by one to find the largest
4899 type that fits. */
4900 integer_type = integer_types[itk - 1];
4902 if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
4903 /* In a union, the padding field must have the full width
4904 of the bit-field; all fields start at offset zero. */
4905 padding = DECL_SIZE (field);
4906 else
4908 if (warn_abi && TREE_CODE (t) == UNION_TYPE)
4909 warning ("size assigned to `%T' may not be "
4910 "ABI-compliant and may change in a future "
4911 "version of GCC",
4913 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
4914 TYPE_SIZE (integer_type));
4916 DECL_SIZE (field) = TYPE_SIZE (integer_type);
4917 DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4918 DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4920 else
4921 padding = NULL_TREE;
4923 layout_nonempty_base_or_field (rli, field, NULL_TREE,
4924 empty_base_offsets);
4926 /* If a bit-field does not immediately follow another bit-field,
4927 and yet it starts in the middle of a byte, we have failed to
4928 comply with the ABI. */
4929 if (warn_abi
4930 && DECL_C_BIT_FIELD (field)
4931 && !last_field_was_bitfield
4932 && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
4933 DECL_FIELD_BIT_OFFSET (field),
4934 bitsize_unit_node)))
4935 cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC",
4936 field);
4938 /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
4939 offset of the field. */
4940 if (warn_abi
4941 && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
4942 byte_position (field))
4943 && contains_empty_class_p (TREE_TYPE (field)))
4944 cp_warning_at ("`%D' contains empty classes which may cause base "
4945 "classes to be placed at different locations in a "
4946 "future version of GCC",
4947 field);
4949 /* If we needed additional padding after this field, add it
4950 now. */
4951 if (padding)
4953 tree padding_field;
4955 padding_field = build_decl (FIELD_DECL,
4956 NULL_TREE,
4957 char_type_node);
4958 DECL_BIT_FIELD (padding_field) = 1;
4959 DECL_SIZE (padding_field) = padding;
4960 DECL_ALIGN (padding_field) = 1;
4961 DECL_USER_ALIGN (padding_field) = 0;
4962 layout_nonempty_base_or_field (rli, padding_field,
4963 NULL_TREE,
4964 empty_base_offsets);
4967 last_field_was_bitfield = DECL_C_BIT_FIELD (field);
4970 if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
4972 /* Make sure that we are on a byte boundary so that the size of
4973 the class without virtual bases will always be a round number
4974 of bytes. */
4975 rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4976 normalize_rli (rli);
4979 /* Make sure that empty classes are reflected in RLI at this
4980 point. */
4981 include_empty_classes(rli);
4983 /* Delete all zero-width bit-fields from the list of fields. Now
4984 that the type is laid out they are no longer important. */
4985 remove_zero_width_bit_fields (t);
4987 /* Create the version of T used for virtual bases. We do not use
4988 make_aggr_type for this version; this is an artificial type. For
4989 a POD type, we just reuse T. */
4990 if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
4992 base_t = make_node (TREE_CODE (t));
4994 /* Set the size and alignment for the new type. In G++ 3.2, all
4995 empty classes were considered to have size zero when used as
4996 base classes. */
4997 if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
4999 TYPE_SIZE (base_t) = bitsize_zero_node;
5000 TYPE_SIZE_UNIT (base_t) = size_zero_node;
5001 if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5002 warning ("layout of classes derived from empty class `%T' "
5003 "may change in a future version of GCC",
5006 else
5008 TYPE_SIZE (base_t) = rli_size_so_far (rli);
5009 TYPE_SIZE_UNIT (base_t) = rli_size_unit_so_far (rli);
5011 TYPE_ALIGN (base_t) = rli->record_align;
5012 TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5014 /* Copy the fields from T. */
5015 next_field = &TYPE_FIELDS (base_t);
5016 for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5017 if (TREE_CODE (field) == FIELD_DECL)
5019 *next_field = build_decl (FIELD_DECL,
5020 DECL_NAME (field),
5021 TREE_TYPE (field));
5022 DECL_CONTEXT (*next_field) = base_t;
5023 DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5024 DECL_FIELD_BIT_OFFSET (*next_field)
5025 = DECL_FIELD_BIT_OFFSET (field);
5026 next_field = &TREE_CHAIN (*next_field);
5029 /* Record the base version of the type. */
5030 CLASSTYPE_AS_BASE (t) = base_t;
5032 else
5033 CLASSTYPE_AS_BASE (t) = t;
5035 /* Every empty class contains an empty class. */
5036 if (CLASSTYPE_EMPTY_P (t))
5037 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5039 /* Set the TYPE_DECL for this type to contain the right
5040 value for DECL_OFFSET, so that we can use it as part
5041 of a COMPONENT_REF for multiple inheritance. */
5042 layout_decl (TYPE_MAIN_DECL (t), 0);
5044 /* Now fix up any virtual base class types that we left lying
5045 around. We must get these done before we try to lay out the
5046 virtual function table. As a side-effect, this will remove the
5047 base subobject fields. */
5048 layout_virtual_bases (rli, empty_base_offsets);
5050 /* Make sure that empty classes are reflected in RLI at this
5051 point. */
5052 include_empty_classes(rli);
5054 /* Make sure not to create any structures with zero size. */
5055 if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5056 place_field (rli,
5057 build_decl (FIELD_DECL, NULL_TREE, char_type_node));
5059 /* Let the back-end lay out the type. */
5060 finish_record_layout (rli, /*free_p=*/true);
5062 /* Warn about bases that can't be talked about due to ambiguity. */
5063 warn_about_ambiguous_bases (t);
5065 /* Clean up. */
5066 splay_tree_delete (empty_base_offsets);
5069 /* Perform processing required when the definition of T (a class type)
5070 is complete. */
5072 void
5073 finish_struct_1 (t)
5074 tree t;
5076 tree x;
5077 /* A TREE_LIST. The TREE_VALUE of each node is a FUNCTION_DECL. */
5078 tree virtuals = NULL_TREE;
5079 int n_fields = 0;
5080 tree vfield;
5082 if (COMPLETE_TYPE_P (t))
5084 if (IS_AGGR_TYPE (t))
5085 error ("redefinition of `%#T'", t);
5086 else
5087 abort ();
5088 popclass ();
5089 return;
5092 /* If this type was previously laid out as a forward reference,
5093 make sure we lay it out again. */
5094 TYPE_SIZE (t) = NULL_TREE;
5095 CLASSTYPE_GOT_SEMICOLON (t) = 0;
5096 CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5098 fixup_inline_methods (t);
5100 /* Make assumptions about the class; we'll reset the flags if
5101 necessary. */
5102 CLASSTYPE_EMPTY_P (t) = 1;
5103 CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5104 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5106 /* Do end-of-class semantic processing: checking the validity of the
5107 bases and members and add implicitly generated methods. */
5108 check_bases_and_members (t);
5110 /* Layout the class itself. */
5111 layout_class_type (t, &virtuals);
5113 /* Make sure that we get our own copy of the vfield FIELD_DECL. */
5114 vfield = TYPE_VFIELD (t);
5115 if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5117 tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5119 my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5120 BINFO_TYPE (primary)),
5121 20010726);
5122 /* The vtable better be at the start. */
5123 my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5124 20010726);
5125 my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5126 20010726);
5128 vfield = copy_decl (vfield);
5129 DECL_FIELD_CONTEXT (vfield) = t;
5130 TYPE_VFIELD (t) = vfield;
5132 else
5133 my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5135 virtuals = modify_all_vtables (t, nreverse (virtuals));
5137 /* If we created a new vtbl pointer for this class, add it to the
5138 list. */
5139 if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5140 CLASSTYPE_VFIELDS (t)
5141 = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5143 /* If necessary, create the primary vtable for this class. */
5144 if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5146 /* We must enter these virtuals into the table. */
5147 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5148 build_primary_vtable (NULL_TREE, t);
5149 else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5150 /* Here we know enough to change the type of our virtual
5151 function table, but we will wait until later this function. */
5152 build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5155 if (TYPE_CONTAINS_VPTR_P (t))
5157 int vindex;
5158 tree fn;
5160 if (TYPE_BINFO_VTABLE (t))
5161 my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5162 20000116);
5163 if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5164 my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5165 20000116);
5167 /* Add entries for virtual functions introduced by this class. */
5168 TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5170 /* Set DECL_VINDEX for all functions declared in this class. */
5171 for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
5172 fn;
5173 fn = TREE_CHAIN (fn),
5174 vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5175 ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5176 if (TREE_CODE (DECL_VINDEX (BV_FN (fn))) != INTEGER_CST)
5177 DECL_VINDEX (BV_FN (fn)) = build_shared_int_cst (vindex);
5179 /* Add this class to the list of dynamic classes. */
5180 dynamic_classes = tree_cons (NULL_TREE, t, dynamic_classes);
5183 finish_struct_bits (t);
5185 /* Complete the rtl for any static member objects of the type we're
5186 working on. */
5187 for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5188 if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5189 && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5190 DECL_MODE (x) = TYPE_MODE (t);
5192 /* Done with FIELDS...now decide whether to sort these for
5193 faster lookups later.
5195 The C front-end only does this when n_fields > 15. We use
5196 a smaller number because most searches fail (succeeding
5197 ultimately as the search bores through the inheritance
5198 hierarchy), and we want this failure to occur quickly. */
5200 n_fields = count_fields (TYPE_FIELDS (t));
5201 if (n_fields > 7)
5203 tree field_vec = make_tree_vec (n_fields);
5204 add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5205 qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5206 (int (*)(const void *, const void *))field_decl_cmp);
5207 if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5208 retrofit_lang_decl (TYPE_MAIN_DECL (t));
5209 DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5212 if (TYPE_HAS_CONSTRUCTOR (t))
5214 tree vfields = CLASSTYPE_VFIELDS (t);
5216 for (vfields = CLASSTYPE_VFIELDS (t);
5217 vfields; vfields = TREE_CHAIN (vfields))
5218 /* Mark the fact that constructor for T could affect anybody
5219 inheriting from T who wants to initialize vtables for
5220 VFIELDS's type. */
5221 if (VF_BINFO_VALUE (vfields))
5222 TREE_ADDRESSABLE (vfields) = 1;
5225 /* Make the rtl for any new vtables we have created, and unmark
5226 the base types we marked. */
5227 finish_vtbls (t);
5229 /* Build the VTT for T. */
5230 build_vtt (t);
5232 if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5233 && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5234 warning ("`%#T' has virtual functions but non-virtual destructor", t);
5236 complete_vars (t);
5238 if (warn_overloaded_virtual)
5239 warn_hidden (t);
5241 maybe_suppress_debug_info (t);
5243 dump_class_hierarchy (t);
5245 /* Finish debugging output for this type. */
5246 rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5249 /* When T was built up, the member declarations were added in reverse
5250 order. Rearrange them to declaration order. */
5252 void
5253 unreverse_member_declarations (t)
5254 tree t;
5256 tree next;
5257 tree prev;
5258 tree x;
5260 /* The following lists are all in reverse order. Put them in
5261 declaration order now. */
5262 TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5263 CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5264 CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5266 /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5267 reverse order, so we can't just use nreverse. */
5268 prev = NULL_TREE;
5269 for (x = TYPE_FIELDS (t);
5270 x && TREE_CODE (x) != TYPE_DECL;
5271 x = next)
5273 next = TREE_CHAIN (x);
5274 TREE_CHAIN (x) = prev;
5275 prev = x;
5277 if (prev)
5279 TREE_CHAIN (TYPE_FIELDS (t)) = x;
5280 if (prev)
5281 TYPE_FIELDS (t) = prev;
5285 tree
5286 finish_struct (t, attributes)
5287 tree t, attributes;
5289 const char *saved_filename = input_filename;
5290 int saved_lineno = lineno;
5292 /* Now that we've got all the field declarations, reverse everything
5293 as necessary. */
5294 unreverse_member_declarations (t);
5296 cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5298 /* Nadger the current location so that diagnostics point to the start of
5299 the struct, not the end. */
5300 input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5301 lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5303 if (processing_template_decl)
5305 finish_struct_methods (t);
5306 TYPE_SIZE (t) = bitsize_zero_node;
5308 else
5309 finish_struct_1 (t);
5311 input_filename = saved_filename;
5312 lineno = saved_lineno;
5314 TYPE_BEING_DEFINED (t) = 0;
5316 if (current_class_type)
5317 popclass ();
5318 else
5319 error ("trying to finish struct, but kicked out due to previous parse errors");
5321 if (processing_template_decl && at_function_scope_p ())
5322 add_stmt (build_min (TAG_DEFN, t));
5324 return t;
5327 /* Return the dynamic type of INSTANCE, if known.
5328 Used to determine whether the virtual function table is needed
5329 or not.
5331 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5332 of our knowledge of its type. *NONNULL should be initialized
5333 before this function is called. */
5335 static tree
5336 fixed_type_or_null (instance, nonnull, cdtorp)
5337 tree instance;
5338 int *nonnull;
5339 int *cdtorp;
5341 switch (TREE_CODE (instance))
5343 case INDIRECT_REF:
5344 if (POINTER_TYPE_P (TREE_TYPE (instance)))
5345 return NULL_TREE;
5346 else
5347 return fixed_type_or_null (TREE_OPERAND (instance, 0),
5348 nonnull, cdtorp);
5350 case CALL_EXPR:
5351 /* This is a call to a constructor, hence it's never zero. */
5352 if (TREE_HAS_CONSTRUCTOR (instance))
5354 if (nonnull)
5355 *nonnull = 1;
5356 return TREE_TYPE (instance);
5358 return NULL_TREE;
5360 case SAVE_EXPR:
5361 /* This is a call to a constructor, hence it's never zero. */
5362 if (TREE_HAS_CONSTRUCTOR (instance))
5364 if (nonnull)
5365 *nonnull = 1;
5366 return TREE_TYPE (instance);
5368 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5370 case RTL_EXPR:
5371 return NULL_TREE;
5373 case PLUS_EXPR:
5374 case MINUS_EXPR:
5375 if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5376 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5377 if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5378 /* Propagate nonnull. */
5379 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5380 return NULL_TREE;
5382 case NOP_EXPR:
5383 case CONVERT_EXPR:
5384 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5386 case ADDR_EXPR:
5387 if (nonnull)
5388 *nonnull = 1;
5389 return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5391 case COMPONENT_REF:
5392 return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5394 case VAR_DECL:
5395 case FIELD_DECL:
5396 if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5397 && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5399 if (nonnull)
5400 *nonnull = 1;
5401 return TREE_TYPE (TREE_TYPE (instance));
5403 /* fall through... */
5404 case TARGET_EXPR:
5405 case PARM_DECL:
5406 case RESULT_DECL:
5407 if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5409 if (nonnull)
5410 *nonnull = 1;
5411 return TREE_TYPE (instance);
5413 else if (instance == current_class_ptr)
5415 if (nonnull)
5416 *nonnull = 1;
5418 /* if we're in a ctor or dtor, we know our type. */
5419 if (DECL_LANG_SPECIFIC (current_function_decl)
5420 && (DECL_CONSTRUCTOR_P (current_function_decl)
5421 || DECL_DESTRUCTOR_P (current_function_decl)))
5423 if (cdtorp)
5424 *cdtorp = 1;
5425 return TREE_TYPE (TREE_TYPE (instance));
5428 else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5430 /* Reference variables should be references to objects. */
5431 if (nonnull)
5432 *nonnull = 1;
5434 if (TREE_CODE (instance) == VAR_DECL
5435 && DECL_INITIAL (instance))
5436 return fixed_type_or_null (DECL_INITIAL (instance),
5437 nonnull, cdtorp);
5439 return NULL_TREE;
5441 default:
5442 return NULL_TREE;
5446 /* Return nonzero if the dynamic type of INSTANCE is known, and
5447 equivalent to the static type. We also handle the case where
5448 INSTANCE is really a pointer. Return negative if this is a
5449 ctor/dtor. There the dynamic type is known, but this might not be
5450 the most derived base of the original object, and hence virtual
5451 bases may not be layed out according to this type.
5453 Used to determine whether the virtual function table is needed
5454 or not.
5456 *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5457 of our knowledge of its type. *NONNULL should be initialized
5458 before this function is called. */
5461 resolves_to_fixed_type_p (instance, nonnull)
5462 tree instance;
5463 int *nonnull;
5465 tree t = TREE_TYPE (instance);
5466 int cdtorp = 0;
5468 tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5469 if (fixed == NULL_TREE)
5470 return 0;
5471 if (POINTER_TYPE_P (t))
5472 t = TREE_TYPE (t);
5473 if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5474 return 0;
5475 return cdtorp ? -1 : 1;
5479 void
5480 init_class_processing ()
5482 current_class_depth = 0;
5483 current_class_stack_size = 10;
5484 current_class_stack
5485 = (class_stack_node_t) xmalloc (current_class_stack_size
5486 * sizeof (struct class_stack_node));
5487 VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5489 access_default_node = build_int_2 (0, 0);
5490 access_public_node = build_int_2 (ak_public, 0);
5491 access_protected_node = build_int_2 (ak_protected, 0);
5492 access_private_node = build_int_2 (ak_private, 0);
5493 access_default_virtual_node = build_int_2 (4, 0);
5494 access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5495 access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5496 access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5498 ridpointers[(int) RID_PUBLIC] = access_public_node;
5499 ridpointers[(int) RID_PRIVATE] = access_private_node;
5500 ridpointers[(int) RID_PROTECTED] = access_protected_node;
5503 /* Set current scope to NAME. CODE tells us if this is a
5504 STRUCT, UNION, or ENUM environment.
5506 NAME may end up being NULL_TREE if this is an anonymous or
5507 late-bound struct (as in "struct { ... } foo;") */
5509 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5510 appropriate values, found by looking up the type definition of
5511 NAME (as a CODE).
5513 If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5514 which can be seen locally to the class. They are shadowed by
5515 any subsequent local declaration (including parameter names).
5517 If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5518 which have static meaning (i.e., static members, static
5519 member functions, enum declarations, etc).
5521 If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5522 which can be seen locally to the class (as in 1), but
5523 know that we are doing this for declaration purposes
5524 (i.e. friend foo::bar (int)).
5526 So that we may avoid calls to lookup_name, we cache the _TYPE
5527 nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5529 For multiple inheritance, we perform a two-pass depth-first search
5530 of the type lattice. The first pass performs a pre-order search,
5531 marking types after the type has had its fields installed in
5532 the appropriate IDENTIFIER_CLASS_VALUE slot. The second pass merely
5533 unmarks the marked types. If a field or member function name
5534 appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5535 that name becomes `error_mark_node'. */
5537 void
5538 pushclass (type, modify)
5539 tree type;
5540 int modify;
5542 type = TYPE_MAIN_VARIANT (type);
5544 /* Make sure there is enough room for the new entry on the stack. */
5545 if (current_class_depth + 1 >= current_class_stack_size)
5547 current_class_stack_size *= 2;
5548 current_class_stack
5549 = (class_stack_node_t) xrealloc (current_class_stack,
5550 current_class_stack_size
5551 * sizeof (struct class_stack_node));
5554 /* Insert a new entry on the class stack. */
5555 current_class_stack[current_class_depth].name = current_class_name;
5556 current_class_stack[current_class_depth].type = current_class_type;
5557 current_class_stack[current_class_depth].access = current_access_specifier;
5558 current_class_stack[current_class_depth].names_used = 0;
5559 current_class_depth++;
5561 /* Now set up the new type. */
5562 current_class_name = TYPE_NAME (type);
5563 if (TREE_CODE (current_class_name) == TYPE_DECL)
5564 current_class_name = DECL_NAME (current_class_name);
5565 current_class_type = type;
5567 /* By default, things in classes are private, while things in
5568 structures or unions are public. */
5569 current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
5570 ? access_private_node
5571 : access_public_node);
5573 if (previous_class_type != NULL_TREE
5574 && (type != previous_class_type
5575 || !COMPLETE_TYPE_P (previous_class_type))
5576 && current_class_depth == 1)
5578 /* Forcibly remove any old class remnants. */
5579 invalidate_class_lookup_cache ();
5582 /* If we're about to enter a nested class, clear
5583 IDENTIFIER_CLASS_VALUE for the enclosing classes. */
5584 if (modify && current_class_depth > 1)
5585 clear_identifier_class_values ();
5587 pushlevel_class ();
5589 if (modify)
5591 if (type != previous_class_type || current_class_depth > 1)
5592 push_class_decls (type);
5593 else
5595 tree item;
5597 /* We are re-entering the same class we just left, so we
5598 don't have to search the whole inheritance matrix to find
5599 all the decls to bind again. Instead, we install the
5600 cached class_shadowed list, and walk through it binding
5601 names and setting up IDENTIFIER_TYPE_VALUEs. */
5602 set_class_shadows (previous_class_values);
5603 for (item = previous_class_values; item; item = TREE_CHAIN (item))
5605 tree id = TREE_PURPOSE (item);
5606 tree decl = TREE_TYPE (item);
5608 push_class_binding (id, decl);
5609 if (TREE_CODE (decl) == TYPE_DECL)
5610 set_identifier_type_value (id, TREE_TYPE (decl));
5612 unuse_fields (type);
5615 storetags (CLASSTYPE_TAGS (type));
5619 /* When we exit a toplevel class scope, we save the
5620 IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5621 reenter the class. Here, we've entered some other class, so we
5622 must invalidate our cache. */
5624 void
5625 invalidate_class_lookup_cache ()
5627 tree t;
5629 /* The IDENTIFIER_CLASS_VALUEs are no longer valid. */
5630 for (t = previous_class_values; t; t = TREE_CHAIN (t))
5631 IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5633 previous_class_values = NULL_TREE;
5634 previous_class_type = NULL_TREE;
5637 /* Get out of the current class scope. If we were in a class scope
5638 previously, that is the one popped to. */
5640 void
5641 popclass ()
5643 poplevel_class ();
5644 pop_class_decls ();
5646 current_class_depth--;
5647 current_class_name = current_class_stack[current_class_depth].name;
5648 current_class_type = current_class_stack[current_class_depth].type;
5649 current_access_specifier = current_class_stack[current_class_depth].access;
5650 if (current_class_stack[current_class_depth].names_used)
5651 splay_tree_delete (current_class_stack[current_class_depth].names_used);
5654 /* Returns 1 if current_class_type is either T or a nested type of T.
5655 We start looking from 1 because entry 0 is from global scope, and has
5656 no type. */
5659 currently_open_class (t)
5660 tree t;
5662 int i;
5663 if (t == current_class_type)
5664 return 1;
5665 for (i = 1; i < current_class_depth; ++i)
5666 if (current_class_stack [i].type == t)
5667 return 1;
5668 return 0;
5671 /* If either current_class_type or one of its enclosing classes are derived
5672 from T, return the appropriate type. Used to determine how we found
5673 something via unqualified lookup. */
5675 tree
5676 currently_open_derived_class (t)
5677 tree t;
5679 int i;
5681 if (DERIVED_FROM_P (t, current_class_type))
5682 return current_class_type;
5684 for (i = current_class_depth - 1; i > 0; --i)
5685 if (DERIVED_FROM_P (t, current_class_stack[i].type))
5686 return current_class_stack[i].type;
5688 return NULL_TREE;
5691 /* When entering a class scope, all enclosing class scopes' names with
5692 static meaning (static variables, static functions, types and enumerators)
5693 have to be visible. This recursive function calls pushclass for all
5694 enclosing class contexts until global or a local scope is reached.
5695 TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5696 formal of the same name. */
5698 void
5699 push_nested_class (type, modify)
5700 tree type;
5701 int modify;
5703 tree context;
5705 /* A namespace might be passed in error cases, like A::B:C. */
5706 if (type == NULL_TREE
5707 || type == error_mark_node
5708 || TREE_CODE (type) == NAMESPACE_DECL
5709 || ! IS_AGGR_TYPE (type)
5710 || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5711 || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5712 return;
5714 context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5716 if (context && CLASS_TYPE_P (context))
5717 push_nested_class (context, 2);
5718 pushclass (type, modify);
5721 /* Undoes a push_nested_class call. MODIFY is passed on to popclass. */
5723 void
5724 pop_nested_class ()
5726 tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5728 popclass ();
5729 if (context && CLASS_TYPE_P (context))
5730 pop_nested_class ();
5733 /* Returns the number of extern "LANG" blocks we are nested within. */
5736 current_lang_depth ()
5738 return VARRAY_ACTIVE_SIZE (current_lang_base);
5741 /* Set global variables CURRENT_LANG_NAME to appropriate value
5742 so that behavior of name-mangling machinery is correct. */
5744 void
5745 push_lang_context (name)
5746 tree name;
5748 VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5750 if (name == lang_name_cplusplus)
5752 current_lang_name = name;
5754 else if (name == lang_name_java)
5756 current_lang_name = name;
5757 /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5758 (See record_builtin_java_type in decl.c.) However, that causes
5759 incorrect debug entries if these types are actually used.
5760 So we re-enable debug output after extern "Java". */
5761 DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5762 DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5763 DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5764 DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5765 DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5766 DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5767 DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5768 DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5770 else if (name == lang_name_c)
5772 current_lang_name = name;
5774 else
5775 error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5778 /* Get out of the current language scope. */
5780 void
5781 pop_lang_context ()
5783 current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5784 VARRAY_POP (current_lang_base);
5787 /* Type instantiation routines. */
5789 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5790 matches the TARGET_TYPE. If there is no satisfactory match, return
5791 error_mark_node, and issue an error message if COMPLAIN is
5792 nonzero. Permit pointers to member function if PTRMEM is nonzero.
5793 If TEMPLATE_ONLY, the name of the overloaded function
5794 was a template-id, and EXPLICIT_TARGS are the explicitly provided
5795 template arguments. */
5797 static tree
5798 resolve_address_of_overloaded_function (target_type,
5799 overload,
5800 complain,
5801 ptrmem,
5802 template_only,
5803 explicit_targs)
5804 tree target_type;
5805 tree overload;
5806 int complain;
5807 int ptrmem;
5808 int template_only;
5809 tree explicit_targs;
5811 /* Here's what the standard says:
5813 [over.over]
5815 If the name is a function template, template argument deduction
5816 is done, and if the argument deduction succeeds, the deduced
5817 arguments are used to generate a single template function, which
5818 is added to the set of overloaded functions considered.
5820 Non-member functions and static member functions match targets of
5821 type "pointer-to-function" or "reference-to-function." Nonstatic
5822 member functions match targets of type "pointer-to-member
5823 function;" the function type of the pointer to member is used to
5824 select the member function from the set of overloaded member
5825 functions. If a nonstatic member function is selected, the
5826 reference to the overloaded function name is required to have the
5827 form of a pointer to member as described in 5.3.1.
5829 If more than one function is selected, any template functions in
5830 the set are eliminated if the set also contains a non-template
5831 function, and any given template function is eliminated if the
5832 set contains a second template function that is more specialized
5833 than the first according to the partial ordering rules 14.5.5.2.
5834 After such eliminations, if any, there shall remain exactly one
5835 selected function. */
5837 int is_ptrmem = 0;
5838 int is_reference = 0;
5839 /* We store the matches in a TREE_LIST rooted here. The functions
5840 are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5841 interoperability with most_specialized_instantiation. */
5842 tree matches = NULL_TREE;
5843 tree fn;
5845 /* By the time we get here, we should be seeing only real
5846 pointer-to-member types, not the internal POINTER_TYPE to
5847 METHOD_TYPE representation. */
5848 my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5849 && (TREE_CODE (TREE_TYPE (target_type))
5850 == METHOD_TYPE)), 0);
5852 if (TREE_CODE (overload) == COMPONENT_REF)
5853 overload = TREE_OPERAND (overload, 1);
5855 /* Check that the TARGET_TYPE is reasonable. */
5856 if (TYPE_PTRFN_P (target_type))
5857 /* This is OK. */;
5858 else if (TYPE_PTRMEMFUNC_P (target_type))
5859 /* This is OK, too. */
5860 is_ptrmem = 1;
5861 else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5863 /* This is OK, too. This comes from a conversion to reference
5864 type. */
5865 target_type = build_reference_type (target_type);
5866 is_reference = 1;
5868 else
5870 if (complain)
5871 error ("\
5872 cannot resolve overloaded function `%D' based on conversion to type `%T'",
5873 DECL_NAME (OVL_FUNCTION (overload)), target_type);
5874 return error_mark_node;
5877 /* If we can find a non-template function that matches, we can just
5878 use it. There's no point in generating template instantiations
5879 if we're just going to throw them out anyhow. But, of course, we
5880 can only do this when we don't *need* a template function. */
5881 if (!template_only)
5883 tree fns;
5885 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5887 tree fn = OVL_FUNCTION (fns);
5888 tree fntype;
5890 if (TREE_CODE (fn) == TEMPLATE_DECL)
5891 /* We're not looking for templates just yet. */
5892 continue;
5894 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5895 != is_ptrmem)
5896 /* We're looking for a non-static member, and this isn't
5897 one, or vice versa. */
5898 continue;
5900 /* See if there's a match. */
5901 fntype = TREE_TYPE (fn);
5902 if (is_ptrmem)
5903 fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5904 else if (!is_reference)
5905 fntype = build_pointer_type (fntype);
5907 if (can_convert_arg (target_type, fntype, fn))
5908 matches = tree_cons (fn, NULL_TREE, matches);
5912 /* Now, if we've already got a match (or matches), there's no need
5913 to proceed to the template functions. But, if we don't have a
5914 match we need to look at them, too. */
5915 if (!matches)
5917 tree target_fn_type;
5918 tree target_arg_types;
5919 tree target_ret_type;
5920 tree fns;
5922 if (is_ptrmem)
5923 target_fn_type
5924 = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5925 else
5926 target_fn_type = TREE_TYPE (target_type);
5927 target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5928 target_ret_type = TREE_TYPE (target_fn_type);
5930 /* Never do unification on the 'this' parameter. */
5931 if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5932 target_arg_types = TREE_CHAIN (target_arg_types);
5934 for (fns = overload; fns; fns = OVL_CHAIN (fns))
5936 tree fn = OVL_FUNCTION (fns);
5937 tree instantiation;
5938 tree instantiation_type;
5939 tree targs;
5941 if (TREE_CODE (fn) != TEMPLATE_DECL)
5942 /* We're only looking for templates. */
5943 continue;
5945 if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5946 != is_ptrmem)
5947 /* We're not looking for a non-static member, and this is
5948 one, or vice versa. */
5949 continue;
5951 /* Try to do argument deduction. */
5952 targs = make_tree_vec (DECL_NTPARMS (fn));
5953 if (fn_type_unification (fn, explicit_targs, targs,
5954 target_arg_types, target_ret_type,
5955 DEDUCE_EXACT, -1) != 0)
5956 /* Argument deduction failed. */
5957 continue;
5959 /* Instantiate the template. */
5960 instantiation = instantiate_template (fn, targs);
5961 if (instantiation == error_mark_node)
5962 /* Instantiation failed. */
5963 continue;
5965 /* See if there's a match. */
5966 instantiation_type = TREE_TYPE (instantiation);
5967 if (is_ptrmem)
5968 instantiation_type =
5969 build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5970 else if (!is_reference)
5971 instantiation_type = build_pointer_type (instantiation_type);
5972 if (can_convert_arg (target_type, instantiation_type, instantiation))
5973 matches = tree_cons (instantiation, fn, matches);
5976 /* Now, remove all but the most specialized of the matches. */
5977 if (matches)
5979 tree match = most_specialized_instantiation (matches);
5981 if (match != error_mark_node)
5982 matches = tree_cons (match, NULL_TREE, NULL_TREE);
5986 /* Now we should have exactly one function in MATCHES. */
5987 if (matches == NULL_TREE)
5989 /* There were *no* matches. */
5990 if (complain)
5992 error ("no matches converting function `%D' to type `%#T'",
5993 DECL_NAME (OVL_FUNCTION (overload)),
5994 target_type);
5996 /* print_candidates expects a chain with the functions in
5997 TREE_VALUE slots, so we cons one up here (we're losing anyway,
5998 so why be clever?). */
5999 for (; overload; overload = OVL_NEXT (overload))
6000 matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6001 matches);
6003 print_candidates (matches);
6005 return error_mark_node;
6007 else if (TREE_CHAIN (matches))
6009 /* There were too many matches. */
6011 if (complain)
6013 tree match;
6015 error ("converting overloaded function `%D' to type `%#T' is ambiguous",
6016 DECL_NAME (OVL_FUNCTION (overload)),
6017 target_type);
6019 /* Since print_candidates expects the functions in the
6020 TREE_VALUE slot, we flip them here. */
6021 for (match = matches; match; match = TREE_CHAIN (match))
6022 TREE_VALUE (match) = TREE_PURPOSE (match);
6024 print_candidates (matches);
6027 return error_mark_node;
6030 /* Good, exactly one match. Now, convert it to the correct type. */
6031 fn = TREE_PURPOSE (matches);
6033 if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6034 && !ptrmem && !flag_ms_extensions)
6036 static int explained;
6038 if (!complain)
6039 return error_mark_node;
6041 pedwarn ("assuming pointer to member `%D'", fn);
6042 if (!explained)
6044 pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6045 explained = 1;
6048 mark_used (fn);
6050 if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6051 return build_unary_op (ADDR_EXPR, fn, 0);
6052 else
6054 /* The target must be a REFERENCE_TYPE. Above, build_unary_op
6055 will mark the function as addressed, but here we must do it
6056 explicitly. */
6057 cxx_mark_addressable (fn);
6059 return fn;
6063 /* This function will instantiate the type of the expression given in
6064 RHS to match the type of LHSTYPE. If errors exist, then return
6065 error_mark_node. FLAGS is a bit mask. If ITF_COMPLAIN is set, then
6066 we complain on errors. If we are not complaining, never modify rhs,
6067 as overload resolution wants to try many possible instantiations, in
6068 the hope that at least one will work.
6070 For non-recursive calls, LHSTYPE should be a function, pointer to
6071 function, or a pointer to member function. */
6073 tree
6074 instantiate_type (lhstype, rhs, flags)
6075 tree lhstype, rhs;
6076 tsubst_flags_t flags;
6078 int complain = (flags & tf_error);
6079 int strict = (flags & tf_no_attributes)
6080 ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6081 int allow_ptrmem = flags & tf_ptrmem_ok;
6083 flags &= ~tf_ptrmem_ok;
6085 if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6087 if (complain)
6088 error ("not enough type information");
6089 return error_mark_node;
6092 if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6094 if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6095 return rhs;
6096 if (complain)
6097 error ("argument of type `%T' does not match `%T'",
6098 TREE_TYPE (rhs), lhstype);
6099 return error_mark_node;
6102 if (TREE_CODE (rhs) == BASELINK)
6103 rhs = BASELINK_FUNCTIONS (rhs);
6105 /* We don't overwrite rhs if it is an overloaded function.
6106 Copying it would destroy the tree link. */
6107 if (TREE_CODE (rhs) != OVERLOAD)
6108 rhs = copy_node (rhs);
6110 /* This should really only be used when attempting to distinguish
6111 what sort of a pointer to function we have. For now, any
6112 arithmetic operation which is not supported on pointers
6113 is rejected as an error. */
6115 switch (TREE_CODE (rhs))
6117 case TYPE_EXPR:
6118 case CONVERT_EXPR:
6119 case SAVE_EXPR:
6120 case CONSTRUCTOR:
6121 case BUFFER_REF:
6122 abort ();
6123 return error_mark_node;
6125 case INDIRECT_REF:
6126 case ARRAY_REF:
6128 tree new_rhs;
6130 new_rhs = instantiate_type (build_pointer_type (lhstype),
6131 TREE_OPERAND (rhs, 0), flags);
6132 if (new_rhs == error_mark_node)
6133 return error_mark_node;
6135 TREE_TYPE (rhs) = lhstype;
6136 TREE_OPERAND (rhs, 0) = new_rhs;
6137 return rhs;
6140 case NOP_EXPR:
6141 rhs = copy_node (TREE_OPERAND (rhs, 0));
6142 TREE_TYPE (rhs) = unknown_type_node;
6143 return instantiate_type (lhstype, rhs, flags);
6145 case COMPONENT_REF:
6146 return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6148 case OFFSET_REF:
6149 rhs = TREE_OPERAND (rhs, 1);
6150 if (BASELINK_P (rhs))
6151 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6152 flags | allow_ptrmem);
6154 /* This can happen if we are forming a pointer-to-member for a
6155 member template. */
6156 my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6158 /* Fall through. */
6160 case TEMPLATE_ID_EXPR:
6162 tree fns = TREE_OPERAND (rhs, 0);
6163 tree args = TREE_OPERAND (rhs, 1);
6165 return
6166 resolve_address_of_overloaded_function (lhstype,
6167 fns,
6168 complain,
6169 allow_ptrmem,
6170 /*template_only=*/1,
6171 args);
6174 case OVERLOAD:
6175 return
6176 resolve_address_of_overloaded_function (lhstype,
6177 rhs,
6178 complain,
6179 allow_ptrmem,
6180 /*template_only=*/0,
6181 /*explicit_targs=*/NULL_TREE);
6183 case TREE_LIST:
6184 /* Now we should have a baselink. */
6185 my_friendly_assert (BASELINK_P (rhs), 990412);
6187 return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6189 case CALL_EXPR:
6190 /* This is too hard for now. */
6191 abort ();
6192 return error_mark_node;
6194 case PLUS_EXPR:
6195 case MINUS_EXPR:
6196 case COMPOUND_EXPR:
6197 TREE_OPERAND (rhs, 0)
6198 = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6199 if (TREE_OPERAND (rhs, 0) == error_mark_node)
6200 return error_mark_node;
6201 TREE_OPERAND (rhs, 1)
6202 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6203 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6204 return error_mark_node;
6206 TREE_TYPE (rhs) = lhstype;
6207 return rhs;
6209 case MULT_EXPR:
6210 case TRUNC_DIV_EXPR:
6211 case FLOOR_DIV_EXPR:
6212 case CEIL_DIV_EXPR:
6213 case ROUND_DIV_EXPR:
6214 case RDIV_EXPR:
6215 case TRUNC_MOD_EXPR:
6216 case FLOOR_MOD_EXPR:
6217 case CEIL_MOD_EXPR:
6218 case ROUND_MOD_EXPR:
6219 case FIX_ROUND_EXPR:
6220 case FIX_FLOOR_EXPR:
6221 case FIX_CEIL_EXPR:
6222 case FIX_TRUNC_EXPR:
6223 case FLOAT_EXPR:
6224 case NEGATE_EXPR:
6225 case ABS_EXPR:
6226 case MAX_EXPR:
6227 case MIN_EXPR:
6228 case FFS_EXPR:
6230 case BIT_AND_EXPR:
6231 case BIT_IOR_EXPR:
6232 case BIT_XOR_EXPR:
6233 case LSHIFT_EXPR:
6234 case RSHIFT_EXPR:
6235 case LROTATE_EXPR:
6236 case RROTATE_EXPR:
6238 case PREINCREMENT_EXPR:
6239 case PREDECREMENT_EXPR:
6240 case POSTINCREMENT_EXPR:
6241 case POSTDECREMENT_EXPR:
6242 if (complain)
6243 error ("invalid operation on uninstantiated type");
6244 return error_mark_node;
6246 case TRUTH_AND_EXPR:
6247 case TRUTH_OR_EXPR:
6248 case TRUTH_XOR_EXPR:
6249 case LT_EXPR:
6250 case LE_EXPR:
6251 case GT_EXPR:
6252 case GE_EXPR:
6253 case EQ_EXPR:
6254 case NE_EXPR:
6255 case TRUTH_ANDIF_EXPR:
6256 case TRUTH_ORIF_EXPR:
6257 case TRUTH_NOT_EXPR:
6258 if (complain)
6259 error ("not enough type information");
6260 return error_mark_node;
6262 case COND_EXPR:
6263 if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6265 if (complain)
6266 error ("not enough type information");
6267 return error_mark_node;
6269 TREE_OPERAND (rhs, 1)
6270 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6271 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6272 return error_mark_node;
6273 TREE_OPERAND (rhs, 2)
6274 = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6275 if (TREE_OPERAND (rhs, 2) == error_mark_node)
6276 return error_mark_node;
6278 TREE_TYPE (rhs) = lhstype;
6279 return rhs;
6281 case MODIFY_EXPR:
6282 TREE_OPERAND (rhs, 1)
6283 = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6284 if (TREE_OPERAND (rhs, 1) == error_mark_node)
6285 return error_mark_node;
6287 TREE_TYPE (rhs) = lhstype;
6288 return rhs;
6290 case ADDR_EXPR:
6292 if (PTRMEM_OK_P (rhs))
6293 flags |= tf_ptrmem_ok;
6295 return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6297 case ENTRY_VALUE_EXPR:
6298 abort ();
6299 return error_mark_node;
6301 case ERROR_MARK:
6302 return error_mark_node;
6304 default:
6305 abort ();
6306 return error_mark_node;
6310 /* Return the name of the virtual function pointer field
6311 (as an IDENTIFIER_NODE) for the given TYPE. Note that
6312 this may have to look back through base types to find the
6313 ultimate field name. (For single inheritance, these could
6314 all be the same name. Who knows for multiple inheritance). */
6316 static tree
6317 get_vfield_name (type)
6318 tree type;
6320 tree binfo = TYPE_BINFO (type);
6321 char *buf;
6323 while (BINFO_BASETYPES (binfo)
6324 && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6325 && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6326 binfo = BINFO_BASETYPE (binfo, 0);
6328 type = BINFO_TYPE (binfo);
6329 buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6330 + TYPE_NAME_LENGTH (type) + 2);
6331 sprintf (buf, VFIELD_NAME_FORMAT,
6332 IDENTIFIER_POINTER (constructor_name (type)));
6333 return get_identifier (buf);
6336 void
6337 print_class_statistics ()
6339 #ifdef GATHER_STATISTICS
6340 fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6341 fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6342 fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6343 n_build_method_call, n_inner_fields_searched);
6344 if (n_vtables)
6346 fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6347 n_vtables, n_vtable_searches);
6348 fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6349 n_vtable_entries, n_vtable_elems);
6351 #endif
6354 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6355 according to [class]:
6356 The class-name is also inserted
6357 into the scope of the class itself. For purposes of access checking,
6358 the inserted class name is treated as if it were a public member name. */
6360 void
6361 build_self_reference ()
6363 tree name = constructor_name (current_class_type);
6364 tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6365 tree saved_cas;
6367 DECL_NONLOCAL (value) = 1;
6368 DECL_CONTEXT (value) = current_class_type;
6369 DECL_ARTIFICIAL (value) = 1;
6371 if (processing_template_decl)
6372 value = push_template_decl (value);
6374 saved_cas = current_access_specifier;
6375 current_access_specifier = access_public_node;
6376 finish_member_declaration (value);
6377 current_access_specifier = saved_cas;
6380 /* Returns 1 if TYPE contains only padding bytes. */
6383 is_empty_class (type)
6384 tree type;
6386 if (type == error_mark_node)
6387 return 0;
6389 if (! IS_AGGR_TYPE (type))
6390 return 0;
6392 /* In G++ 3.2, whether or not a class was empty was determined by
6393 looking at its size. */
6394 if (abi_version_at_least (2))
6395 return CLASSTYPE_EMPTY_P (type);
6396 else
6397 return integer_zerop (CLASSTYPE_SIZE (type));
6400 /* Returns true if TYPE contains an empty class. */
6402 static bool
6403 contains_empty_class_p (tree type)
6405 if (is_empty_class (type))
6406 return true;
6407 if (CLASS_TYPE_P (type))
6409 tree field;
6410 int i;
6412 for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6413 if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6414 return true;
6415 for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6416 if (TREE_CODE (field) == FIELD_DECL
6417 && !DECL_ARTIFICIAL (field)
6418 && is_empty_class (TREE_TYPE (field)))
6419 return true;
6421 else if (TREE_CODE (type) == ARRAY_TYPE)
6422 return contains_empty_class_p (TREE_TYPE (type));
6423 return false;
6426 /* Find the enclosing class of the given NODE. NODE can be a *_DECL or
6427 a *_TYPE node. NODE can also be a local class. */
6429 tree
6430 get_enclosing_class (type)
6431 tree type;
6433 tree node = type;
6435 while (node && TREE_CODE (node) != NAMESPACE_DECL)
6437 switch (TREE_CODE_CLASS (TREE_CODE (node)))
6439 case 'd':
6440 node = DECL_CONTEXT (node);
6441 break;
6443 case 't':
6444 if (node != type)
6445 return node;
6446 node = TYPE_CONTEXT (node);
6447 break;
6449 default:
6450 abort ();
6453 return NULL_TREE;
6456 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE. */
6459 is_base_of_enclosing_class (base, type)
6460 tree base, type;
6462 while (type)
6464 if (lookup_base (type, base, ba_any, NULL))
6465 return 1;
6467 type = get_enclosing_class (type);
6469 return 0;
6472 /* Note that NAME was looked up while the current class was being
6473 defined and that the result of that lookup was DECL. */
6475 void
6476 maybe_note_name_used_in_class (name, decl)
6477 tree name;
6478 tree decl;
6480 splay_tree names_used;
6482 /* If we're not defining a class, there's nothing to do. */
6483 if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6484 return;
6486 /* If there's already a binding for this NAME, then we don't have
6487 anything to worry about. */
6488 if (IDENTIFIER_CLASS_VALUE (name))
6489 return;
6491 if (!current_class_stack[current_class_depth - 1].names_used)
6492 current_class_stack[current_class_depth - 1].names_used
6493 = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6494 names_used = current_class_stack[current_class_depth - 1].names_used;
6496 splay_tree_insert (names_used,
6497 (splay_tree_key) name,
6498 (splay_tree_value) decl);
6501 /* Note that NAME was declared (as DECL) in the current class. Check
6502 to see that the declaration is valid. */
6504 void
6505 note_name_declared_in_class (name, decl)
6506 tree name;
6507 tree decl;
6509 splay_tree names_used;
6510 splay_tree_node n;
6512 /* Look to see if we ever used this name. */
6513 names_used
6514 = current_class_stack[current_class_depth - 1].names_used;
6515 if (!names_used)
6516 return;
6518 n = splay_tree_lookup (names_used, (splay_tree_key) name);
6519 if (n)
6521 /* [basic.scope.class]
6523 A name N used in a class S shall refer to the same declaration
6524 in its context and when re-evaluated in the completed scope of
6525 S. */
6526 error ("declaration of `%#D'", decl);
6527 cp_error_at ("changes meaning of `%D' from `%+#D'",
6528 DECL_NAME (OVL_CURRENT (decl)),
6529 (tree) n->value);
6533 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6534 Secondary vtables are merged with primary vtables; this function
6535 will return the VAR_DECL for the primary vtable. */
6537 tree
6538 get_vtbl_decl_for_binfo (binfo)
6539 tree binfo;
6541 tree decl;
6543 decl = BINFO_VTABLE (binfo);
6544 if (decl && TREE_CODE (decl) == PLUS_EXPR)
6546 my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6547 2000403);
6548 decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6550 if (decl)
6551 my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6552 return decl;
6555 /* Called from get_primary_binfo via dfs_walk. DATA is a TREE_LIST
6556 who's TREE_PURPOSE is the TYPE of the required primary base and
6557 who's TREE_VALUE is a list of candidate binfos that we fill in. */
6559 static tree
6560 dfs_get_primary_binfo (binfo, data)
6561 tree binfo;
6562 void *data;
6564 tree cons = (tree) data;
6565 tree primary_base = TREE_PURPOSE (cons);
6567 if (TREE_VIA_VIRTUAL (binfo)
6568 && same_type_p (BINFO_TYPE (binfo), primary_base))
6569 /* This is the right type of binfo, but it might be an unshared
6570 instance, and the shared instance is later in the dfs walk. We
6571 must keep looking. */
6572 TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6574 return NULL_TREE;
6577 /* Returns the unshared binfo for the primary base of BINFO. Note
6578 that in a complex hierarchy the resulting BINFO may not actually
6579 *be* primary. In particular if the resulting BINFO is a virtual
6580 base, and it occurs elsewhere in the hierarchy, then this
6581 occurrence may not actually be a primary base in the complete
6582 object. Check BINFO_PRIMARY_P to be sure. */
6584 tree
6585 get_primary_binfo (binfo)
6586 tree binfo;
6588 tree primary_base;
6589 tree result = NULL_TREE;
6590 tree virtuals;
6592 primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6593 if (!primary_base)
6594 return NULL_TREE;
6596 /* A non-virtual primary base is always a direct base, and easy to
6597 find. */
6598 if (!TREE_VIA_VIRTUAL (primary_base))
6600 int i;
6602 /* Scan the direct basetypes until we find a base with the same
6603 type as the primary base. */
6604 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6606 tree base_binfo = BINFO_BASETYPE (binfo, i);
6608 if (same_type_p (BINFO_TYPE (base_binfo),
6609 BINFO_TYPE (primary_base)))
6610 return base_binfo;
6613 /* We should always find the primary base. */
6614 abort ();
6617 /* For a primary virtual base, we have to scan the entire hierarchy
6618 rooted at BINFO; the virtual base could be an indirect virtual
6619 base. There could be more than one instance of the primary base
6620 in the hierarchy, and if one is the canonical binfo we want that
6621 one. If it exists, it should be the first one we find, but as a
6622 consistency check we find them all and make sure. */
6623 virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6624 dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6625 virtuals = TREE_VALUE (virtuals);
6627 /* We must have found at least one instance. */
6628 my_friendly_assert (virtuals, 20010612);
6630 if (TREE_CHAIN (virtuals))
6632 /* We found more than one instance of the base. We must make
6633 sure that, if one is the canonical one, it is the first one
6634 we found. As the chain is in reverse dfs order, that means
6635 the last on the list. */
6636 tree complete_binfo;
6637 tree canonical;
6639 for (complete_binfo = binfo;
6640 BINFO_INHERITANCE_CHAIN (complete_binfo);
6641 complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6642 continue;
6643 canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6644 BINFO_TYPE (complete_binfo));
6646 for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6648 result = TREE_VALUE (virtuals);
6650 if (canonical == result)
6652 /* This is the unshared instance. Make sure it was the
6653 first one found. */
6654 my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6655 break;
6659 else
6660 result = TREE_VALUE (virtuals);
6661 return result;
6664 /* If INDENTED_P is zero, indent to INDENT. Return nonzero. */
6666 static int
6667 maybe_indent_hierarchy (stream, indent, indented_p)
6668 FILE *stream;
6669 int indent;
6670 int indented_p;
6672 if (!indented_p)
6673 fprintf (stream, "%*s", indent, "");
6674 return 1;
6677 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6678 dominated by T) to stderr. INDENT should be zero when called from
6679 the top level; it is incremented recursively. */
6681 static void
6682 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6683 FILE *stream;
6684 int flags;
6685 tree t;
6686 tree binfo;
6687 int indent;
6689 int i;
6690 int indented = 0;
6692 indented = maybe_indent_hierarchy (stream, indent, 0);
6693 fprintf (stream, "%s (0x%lx) ",
6694 type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6695 (unsigned long) binfo);
6696 fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6697 tree_low_cst (BINFO_OFFSET (binfo), 0));
6698 if (is_empty_class (BINFO_TYPE (binfo)))
6699 fprintf (stream, " empty");
6700 else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6701 fprintf (stream, " nearly-empty");
6702 if (TREE_VIA_VIRTUAL (binfo))
6704 tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6706 fprintf (stream, " virtual");
6707 if (canonical == binfo)
6708 fprintf (stream, " canonical");
6709 else
6710 fprintf (stream, " non-canonical");
6712 fprintf (stream, "\n");
6714 indented = 0;
6715 if (BINFO_PRIMARY_BASE_OF (binfo))
6717 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6718 fprintf (stream, " primary-for %s (0x%lx)",
6719 type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6720 TFF_PLAIN_IDENTIFIER),
6721 (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6723 if (BINFO_LOST_PRIMARY_P (binfo))
6725 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6726 fprintf (stream, " lost-primary");
6728 if (indented)
6729 fprintf (stream, "\n");
6731 if (!(flags & TDF_SLIM))
6733 int indented = 0;
6735 if (BINFO_SUBVTT_INDEX (binfo))
6737 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6738 fprintf (stream, " subvttidx=%s",
6739 expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6740 TFF_PLAIN_IDENTIFIER));
6742 if (BINFO_VPTR_INDEX (binfo))
6744 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6745 fprintf (stream, " vptridx=%s",
6746 expr_as_string (BINFO_VPTR_INDEX (binfo),
6747 TFF_PLAIN_IDENTIFIER));
6749 if (BINFO_VPTR_FIELD (binfo))
6751 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6752 fprintf (stream, " vbaseoffset=%s",
6753 expr_as_string (BINFO_VPTR_FIELD (binfo),
6754 TFF_PLAIN_IDENTIFIER));
6756 if (BINFO_VTABLE (binfo))
6758 indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6759 fprintf (stream, " vptr=%s",
6760 expr_as_string (BINFO_VTABLE (binfo),
6761 TFF_PLAIN_IDENTIFIER));
6764 if (indented)
6765 fprintf (stream, "\n");
6769 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6770 dump_class_hierarchy_r (stream, flags,
6771 t, BINFO_BASETYPE (binfo, i),
6772 indent + 2);
6775 /* Dump the BINFO hierarchy for T. */
6777 static void
6778 dump_class_hierarchy (t)
6779 tree t;
6781 int flags;
6782 FILE *stream = dump_begin (TDI_class, &flags);
6784 if (!stream)
6785 return;
6787 fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6788 fprintf (stream, " size=%lu align=%lu\n",
6789 (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6790 (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6791 dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6792 fprintf (stream, "\n");
6793 dump_end (TDI_class, stream);
6796 static void
6797 dump_array (stream, decl)
6798 FILE *stream;
6799 tree decl;
6801 tree inits;
6802 int ix;
6803 HOST_WIDE_INT elt;
6804 tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6806 elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6807 / BITS_PER_UNIT);
6808 fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6809 fprintf (stream, " %s entries",
6810 expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6811 TFF_PLAIN_IDENTIFIER));
6812 fprintf (stream, "\n");
6814 for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6815 inits; ix++, inits = TREE_CHAIN (inits))
6816 fprintf (stream, "%-4ld %s\n", (long)(ix * elt),
6817 expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6820 static void
6821 dump_vtable (t, binfo, vtable)
6822 tree t;
6823 tree binfo;
6824 tree vtable;
6826 int flags;
6827 FILE *stream = dump_begin (TDI_class, &flags);
6829 if (!stream)
6830 return;
6832 if (!(flags & TDF_SLIM))
6834 int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6836 fprintf (stream, "%s for %s",
6837 ctor_vtbl_p ? "Construction vtable" : "Vtable",
6838 type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6839 if (ctor_vtbl_p)
6841 if (!TREE_VIA_VIRTUAL (binfo))
6842 fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6843 fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6845 fprintf (stream, "\n");
6846 dump_array (stream, vtable);
6847 fprintf (stream, "\n");
6850 dump_end (TDI_class, stream);
6853 static void
6854 dump_vtt (t, vtt)
6855 tree t;
6856 tree vtt;
6858 int flags;
6859 FILE *stream = dump_begin (TDI_class, &flags);
6861 if (!stream)
6862 return;
6864 if (!(flags & TDF_SLIM))
6866 fprintf (stream, "VTT for %s\n",
6867 type_as_string (t, TFF_PLAIN_IDENTIFIER));
6868 dump_array (stream, vtt);
6869 fprintf (stream, "\n");
6872 dump_end (TDI_class, stream);
6875 /* Virtual function table initialization. */
6877 /* Create all the necessary vtables for T and its base classes. */
6879 static void
6880 finish_vtbls (t)
6881 tree t;
6883 tree list;
6884 tree vbase;
6885 int i;
6887 /* We lay out the primary and secondary vtables in one contiguous
6888 vtable. The primary vtable is first, followed by the non-virtual
6889 secondary vtables in inheritance graph order. */
6890 list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6891 accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6892 TYPE_BINFO (t), t, list);
6894 /* Then come the virtual bases, also in inheritance graph order. */
6895 for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6897 tree real_base;
6899 if (!TREE_VIA_VIRTUAL (vbase))
6900 continue;
6902 /* Although we walk in inheritance order, that might not get the
6903 canonical base. */
6904 real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6906 accumulate_vtbl_inits (real_base, real_base,
6907 TYPE_BINFO (t), t, list);
6910 /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
6911 base classes, for the benefit of the debugging backends. */
6912 for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
6914 tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
6915 if (TREE_VIA_VIRTUAL (base))
6917 vbase = binfo_for_vbase (BINFO_TYPE (base), t);
6918 BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
6922 if (TYPE_BINFO_VTABLE (t))
6923 initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6926 /* Initialize the vtable for BINFO with the INITS. */
6928 static void
6929 initialize_vtable (binfo, inits)
6930 tree binfo;
6931 tree inits;
6933 tree decl;
6935 layout_vtable_decl (binfo, list_length (inits));
6936 decl = get_vtbl_decl_for_binfo (binfo);
6937 initialize_array (decl, inits);
6938 dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6941 /* Initialize DECL (a declaration for a namespace-scope array) with
6942 the INITS. */
6944 static void
6945 initialize_array (decl, inits)
6946 tree decl;
6947 tree inits;
6949 tree context;
6951 context = DECL_CONTEXT (decl);
6952 DECL_CONTEXT (decl) = NULL_TREE;
6953 DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6954 TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
6955 cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6956 DECL_CONTEXT (decl) = context;
6959 /* Build the VTT (virtual table table) for T.
6960 A class requires a VTT if it has virtual bases.
6962 This holds
6963 1 - primary virtual pointer for complete object T
6964 2 - secondary VTTs for each direct non-virtual base of T which requires a
6966 3 - secondary virtual pointers for each direct or indirect base of T which
6967 has virtual bases or is reachable via a virtual path from T.
6968 4 - secondary VTTs for each direct or indirect virtual base of T.
6970 Secondary VTTs look like complete object VTTs without part 4. */
6972 static void
6973 build_vtt (t)
6974 tree t;
6976 tree inits;
6977 tree type;
6978 tree vtt;
6979 tree index;
6981 /* Build up the initializers for the VTT. */
6982 inits = NULL_TREE;
6983 index = size_zero_node;
6984 build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6986 /* If we didn't need a VTT, we're done. */
6987 if (!inits)
6988 return;
6990 /* Figure out the type of the VTT. */
6991 type = build_index_type (size_int (list_length (inits) - 1));
6992 type = build_cplus_array_type (const_ptr_type_node, type);
6994 /* Now, build the VTT object itself. */
6995 vtt = build_vtable (t, get_vtt_name (t), type);
6996 initialize_array (vtt, inits);
6997 /* Add the VTT to the vtables list. */
6998 TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
6999 TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7001 dump_vtt (t, vtt);
7004 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
7005 from within some hierarchy which is inherited from the type of BINFO.
7006 Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
7007 BINFO. */
7009 static tree
7010 get_original_base (base_binfo, binfo)
7011 tree base_binfo;
7012 tree binfo;
7014 tree derived;
7015 int ix;
7017 if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
7018 return binfo;
7019 if (TREE_VIA_VIRTUAL (base_binfo))
7020 return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7021 derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7023 for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7024 if (same_type_p (BINFO_TYPE (base_binfo),
7025 BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7026 return BINFO_BASETYPE (derived, ix);
7027 abort ();
7028 return NULL;
7031 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7032 PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7033 and CHAIN the vtable pointer for this binfo after construction is
7034 complete. VALUE can also be another BINFO, in which case we recurse. */
7036 static tree
7037 binfo_ctor_vtable (binfo)
7038 tree binfo;
7040 tree vt;
7042 while (1)
7044 vt = BINFO_VTABLE (binfo);
7045 if (TREE_CODE (vt) == TREE_LIST)
7046 vt = TREE_VALUE (vt);
7047 if (TREE_CODE (vt) == TREE_VEC)
7048 binfo = vt;
7049 else
7050 break;
7053 return vt;
7056 /* Recursively build the VTT-initializer for BINFO (which is in the
7057 hierarchy dominated by T). INITS points to the end of the initializer
7058 list to date. INDEX is the VTT index where the next element will be
7059 replaced. Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7060 not a subvtt for some base of T). When that is so, we emit the sub-VTTs
7061 for virtual bases of T. When it is not so, we build the constructor
7062 vtables for the BINFO-in-T variant. */
7064 static tree *
7065 build_vtt_inits (binfo, t, inits, index)
7066 tree binfo;
7067 tree t;
7068 tree *inits;
7069 tree *index;
7071 int i;
7072 tree b;
7073 tree init;
7074 tree secondary_vptrs;
7075 int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7077 /* We only need VTTs for subobjects with virtual bases. */
7078 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7079 return inits;
7081 /* We need to use a construction vtable if this is not the primary
7082 VTT. */
7083 if (!top_level_p)
7085 build_ctor_vtbl_group (binfo, t);
7087 /* Record the offset in the VTT where this sub-VTT can be found. */
7088 BINFO_SUBVTT_INDEX (binfo) = *index;
7091 /* Add the address of the primary vtable for the complete object. */
7092 init = binfo_ctor_vtable (binfo);
7093 *inits = build_tree_list (NULL_TREE, init);
7094 inits = &TREE_CHAIN (*inits);
7095 if (top_level_p)
7097 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7098 BINFO_VPTR_INDEX (binfo) = *index;
7100 *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7102 /* Recursively add the secondary VTTs for non-virtual bases. */
7103 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7105 b = BINFO_BASETYPE (binfo, i);
7106 if (!TREE_VIA_VIRTUAL (b))
7107 inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t,
7108 inits, index);
7111 /* Add secondary virtual pointers for all subobjects of BINFO with
7112 either virtual bases or reachable along a virtual path, except
7113 subobjects that are non-virtual primary bases. */
7114 secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7115 TREE_TYPE (secondary_vptrs) = *index;
7116 VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7117 VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7119 dfs_walk_real (binfo,
7120 dfs_build_secondary_vptr_vtt_inits,
7121 NULL,
7122 dfs_ctor_vtable_bases_queue_p,
7123 secondary_vptrs);
7124 VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7125 dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7126 secondary_vptrs);
7128 *index = TREE_TYPE (secondary_vptrs);
7130 /* The secondary vptrs come back in reverse order. After we reverse
7131 them, and add the INITS, the last init will be the first element
7132 of the chain. */
7133 secondary_vptrs = TREE_VALUE (secondary_vptrs);
7134 if (secondary_vptrs)
7136 *inits = nreverse (secondary_vptrs);
7137 inits = &TREE_CHAIN (secondary_vptrs);
7138 my_friendly_assert (*inits == NULL_TREE, 20000517);
7141 /* Add the secondary VTTs for virtual bases. */
7142 if (top_level_p)
7143 for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7145 tree vbase;
7147 if (!TREE_VIA_VIRTUAL (b))
7148 continue;
7150 vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7151 inits = build_vtt_inits (vbase, t, inits, index);
7154 if (!top_level_p)
7156 tree data = tree_cons (t, binfo, NULL_TREE);
7157 VTT_TOP_LEVEL_P (data) = 0;
7158 VTT_MARKED_BINFO_P (data) = 0;
7160 dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7161 dfs_ctor_vtable_bases_queue_p,
7162 data);
7165 return inits;
7168 /* Called from build_vtt_inits via dfs_walk. BINFO is the binfo
7169 for the base in most derived. DATA is a TREE_LIST who's
7170 TREE_CHAIN is the type of the base being
7171 constructed whilst this secondary vptr is live. The TREE_UNSIGNED
7172 flag of DATA indicates that this is a constructor vtable. The
7173 TREE_TOP_LEVEL flag indicates that this is the primary VTT. */
7175 static tree
7176 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7177 tree binfo;
7178 void *data;
7180 tree l;
7181 tree t;
7182 tree init;
7183 tree index;
7184 int top_level_p;
7186 l = (tree) data;
7187 t = TREE_CHAIN (l);
7188 top_level_p = VTT_TOP_LEVEL_P (l);
7190 SET_BINFO_MARKED (binfo);
7192 /* We don't care about bases that don't have vtables. */
7193 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7194 return NULL_TREE;
7196 /* We're only interested in proper subobjects of T. */
7197 if (same_type_p (BINFO_TYPE (binfo), t))
7198 return NULL_TREE;
7200 /* We're not interested in non-virtual primary bases. */
7201 if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7202 return NULL_TREE;
7204 /* If BINFO has virtual bases or is reachable via a virtual path
7205 from T, it'll have a secondary vptr. */
7206 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7207 && !binfo_via_virtual (binfo, t))
7208 return NULL_TREE;
7210 /* Record the index where this secondary vptr can be found. */
7211 index = TREE_TYPE (l);
7212 if (top_level_p)
7214 my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7215 BINFO_VPTR_INDEX (binfo) = index;
7217 TREE_TYPE (l) = size_binop (PLUS_EXPR, index,
7218 TYPE_SIZE_UNIT (ptr_type_node));
7220 /* Add the initializer for the secondary vptr itself. */
7221 if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7223 /* It's a primary virtual base, and this is not the construction
7224 vtable. Find the base this is primary of in the inheritance graph,
7225 and use that base's vtable now. */
7226 while (BINFO_PRIMARY_BASE_OF (binfo))
7227 binfo = BINFO_PRIMARY_BASE_OF (binfo);
7229 init = binfo_ctor_vtable (binfo);
7230 TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7232 return NULL_TREE;
7235 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7236 VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7237 should be walked. TREE_PURPOSE is the TREE_TYPE that dominates the
7238 hierarchy. */
7240 static tree
7241 dfs_ctor_vtable_bases_queue_p (binfo, data)
7242 tree binfo;
7243 void *data;
7245 if (TREE_VIA_VIRTUAL (binfo))
7246 /* Get the shared version. */
7247 binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7249 if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7250 return NULL_TREE;
7251 return binfo;
7254 /* Called from build_vtt_inits via dfs_walk. After building constructor
7255 vtables and generating the sub-vtt from them, we need to restore the
7256 BINFO_VTABLES that were scribbled on. DATA is a TREE_LIST whose
7257 TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated. */
7259 static tree
7260 dfs_fixup_binfo_vtbls (binfo, data)
7261 tree binfo;
7262 void *data;
7264 CLEAR_BINFO_MARKED (binfo);
7266 /* We don't care about bases that don't have vtables. */
7267 if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7268 return NULL_TREE;
7270 /* If we scribbled the construction vtable vptr into BINFO, clear it
7271 out now. */
7272 if (BINFO_VTABLE (binfo)
7273 && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7274 && (TREE_PURPOSE (BINFO_VTABLE (binfo))
7275 == TREE_VALUE ((tree) data)))
7276 BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7278 return NULL_TREE;
7281 /* Build the construction vtable group for BINFO which is in the
7282 hierarchy dominated by T. */
7284 static void
7285 build_ctor_vtbl_group (binfo, t)
7286 tree binfo;
7287 tree t;
7289 tree list;
7290 tree type;
7291 tree vtbl;
7292 tree inits;
7293 tree id;
7294 tree vbase;
7296 /* See if we've already created this construction vtable group. */
7297 id = mangle_ctor_vtbl_for_type (t, binfo);
7298 if (IDENTIFIER_GLOBAL_VALUE (id))
7299 return;
7301 my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7302 /* Build a version of VTBL (with the wrong type) for use in
7303 constructing the addresses of secondary vtables in the
7304 construction vtable group. */
7305 vtbl = build_vtable (t, id, ptr_type_node);
7306 list = build_tree_list (vtbl, NULL_TREE);
7307 accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7308 binfo, t, list);
7310 /* Add the vtables for each of our virtual bases using the vbase in T
7311 binfo. */
7312 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7313 vbase;
7314 vbase = TREE_CHAIN (vbase))
7316 tree b;
7317 tree orig_base;
7319 if (!TREE_VIA_VIRTUAL (vbase))
7320 continue;
7321 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7322 orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7324 accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7326 inits = TREE_VALUE (list);
7328 /* Figure out the type of the construction vtable. */
7329 type = build_index_type (size_int (list_length (inits) - 1));
7330 type = build_cplus_array_type (vtable_entry_type, type);
7331 TREE_TYPE (vtbl) = type;
7333 /* Initialize the construction vtable. */
7334 CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7335 initialize_array (vtbl, inits);
7336 dump_vtable (t, binfo, vtbl);
7339 /* Add the vtbl initializers for BINFO (and its bases other than
7340 non-virtual primaries) to the list of INITS. BINFO is in the
7341 hierarchy dominated by T. RTTI_BINFO is the binfo within T of
7342 the constructor the vtbl inits should be accumulated for. (If this
7343 is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7344 ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7345 BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7346 graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7347 but are not necessarily the same in terms of layout. */
7349 static void
7350 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7351 tree binfo;
7352 tree orig_binfo;
7353 tree rtti_binfo;
7354 tree t;
7355 tree inits;
7357 int i;
7358 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7360 my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7361 BINFO_TYPE (orig_binfo)),
7362 20000517);
7364 /* If it doesn't have a vptr, we don't do anything. */
7365 if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7366 return;
7368 /* If we're building a construction vtable, we're not interested in
7369 subobjects that don't require construction vtables. */
7370 if (ctor_vtbl_p
7371 && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7372 && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7373 return;
7375 /* Build the initializers for the BINFO-in-T vtable. */
7376 TREE_VALUE (inits)
7377 = chainon (TREE_VALUE (inits),
7378 dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7379 rtti_binfo, t, inits));
7381 /* Walk the BINFO and its bases. We walk in preorder so that as we
7382 initialize each vtable we can figure out at what offset the
7383 secondary vtable lies from the primary vtable. We can't use
7384 dfs_walk here because we need to iterate through bases of BINFO
7385 and RTTI_BINFO simultaneously. */
7386 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7388 tree base_binfo = BINFO_BASETYPE (binfo, i);
7390 /* Skip virtual bases. */
7391 if (TREE_VIA_VIRTUAL (base_binfo))
7392 continue;
7393 accumulate_vtbl_inits (base_binfo,
7394 BINFO_BASETYPE (orig_binfo, i),
7395 rtti_binfo, t,
7396 inits);
7400 /* Called from accumulate_vtbl_inits. Returns the initializers for
7401 the BINFO vtable. */
7403 static tree
7404 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7405 tree binfo;
7406 tree orig_binfo;
7407 tree rtti_binfo;
7408 tree t;
7409 tree l;
7411 tree inits = NULL_TREE;
7412 tree vtbl = NULL_TREE;
7413 int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7415 if (ctor_vtbl_p
7416 && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7418 /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7419 primary virtual base. If it is not the same primary in
7420 the hierarchy of T, we'll need to generate a ctor vtable
7421 for it, to place at its location in T. If it is the same
7422 primary, we still need a VTT entry for the vtable, but it
7423 should point to the ctor vtable for the base it is a
7424 primary for within the sub-hierarchy of RTTI_BINFO.
7426 There are three possible cases:
7428 1) We are in the same place.
7429 2) We are a primary base within a lost primary virtual base of
7430 RTTI_BINFO.
7431 3) We are primary to something not a base of RTTI_BINFO. */
7433 tree b = BINFO_PRIMARY_BASE_OF (binfo);
7434 tree last = NULL_TREE;
7436 /* First, look through the bases we are primary to for RTTI_BINFO
7437 or a virtual base. */
7438 for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7440 last = b;
7441 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7442 break;
7444 /* If we run out of primary links, keep looking down our
7445 inheritance chain; we might be an indirect primary. */
7446 if (b == NULL_TREE)
7447 for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7448 if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7449 break;
7451 /* If we found RTTI_BINFO, this is case 1. If we found a virtual
7452 base B and it is a base of RTTI_BINFO, this is case 2. In
7453 either case, we share our vtable with LAST, i.e. the
7454 derived-most base within B of which we are a primary. */
7455 if (b == rtti_binfo
7456 || (b && binfo_for_vbase (BINFO_TYPE (b),
7457 BINFO_TYPE (rtti_binfo))))
7458 /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7459 set LAST's BINFO_VTABLE yet. We'll extract the actual vptr in
7460 binfo_ctor_vtable after everything's been set up. */
7461 vtbl = last;
7463 /* Otherwise, this is case 3 and we get our own. */
7465 else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7466 return inits;
7468 if (!vtbl)
7470 tree index;
7471 int non_fn_entries;
7473 /* Compute the initializer for this vtable. */
7474 inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7475 &non_fn_entries);
7477 /* Figure out the position to which the VPTR should point. */
7478 vtbl = TREE_PURPOSE (l);
7479 vtbl = build1 (ADDR_EXPR,
7480 vtbl_ptr_type_node,
7481 vtbl);
7482 TREE_CONSTANT (vtbl) = 1;
7483 index = size_binop (PLUS_EXPR,
7484 size_int (non_fn_entries),
7485 size_int (list_length (TREE_VALUE (l))));
7486 index = size_binop (MULT_EXPR,
7487 TYPE_SIZE_UNIT (vtable_entry_type),
7488 index);
7489 vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7490 TREE_CONSTANT (vtbl) = 1;
7493 if (ctor_vtbl_p)
7494 /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7495 So, we make a TREE_LIST. Later, dfs_fixup_binfo_vtbls will
7496 straighten this out. */
7497 BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7498 else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7499 inits = NULL_TREE;
7500 else
7501 /* For an ordinary vtable, set BINFO_VTABLE. */
7502 BINFO_VTABLE (binfo) = vtbl;
7504 return inits;
7507 /* Construct the initializer for BINFO's virtual function table. BINFO
7508 is part of the hierarchy dominated by T. If we're building a
7509 construction vtable, the ORIG_BINFO is the binfo we should use to
7510 find the actual function pointers to put in the vtable - but they
7511 can be overridden on the path to most-derived in the graph that
7512 ORIG_BINFO belongs. Otherwise,
7513 ORIG_BINFO should be the same as BINFO. The RTTI_BINFO is the
7514 BINFO that should be indicated by the RTTI information in the
7515 vtable; it will be a base class of T, rather than T itself, if we
7516 are building a construction vtable.
7518 The value returned is a TREE_LIST suitable for wrapping in a
7519 CONSTRUCTOR to use as the DECL_INITIAL for a vtable. If
7520 NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7521 number of non-function entries in the vtable.
7523 It might seem that this function should never be called with a
7524 BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7525 base is always subsumed by a derived class vtable. However, when
7526 we are building construction vtables, we do build vtables for
7527 primary bases; we need these while the primary base is being
7528 constructed. */
7530 static tree
7531 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7532 tree binfo;
7533 tree orig_binfo;
7534 tree t;
7535 tree rtti_binfo;
7536 int *non_fn_entries_p;
7538 tree v, b;
7539 tree vfun_inits;
7540 tree vbase;
7541 vtbl_init_data vid;
7543 /* Initialize VID. */
7544 memset (&vid, 0, sizeof (vid));
7545 vid.binfo = binfo;
7546 vid.derived = t;
7547 vid.rtti_binfo = rtti_binfo;
7548 vid.last_init = &vid.inits;
7549 vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7550 vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7551 vid.generate_vcall_entries = true;
7552 /* The first vbase or vcall offset is at index -3 in the vtable. */
7553 vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7555 /* Add entries to the vtable for RTTI. */
7556 build_rtti_vtbl_entries (binfo, &vid);
7558 /* Create an array for keeping track of the functions we've
7559 processed. When we see multiple functions with the same
7560 signature, we share the vcall offsets. */
7561 VARRAY_TREE_INIT (vid.fns, 32, "fns");
7562 /* Add the vcall and vbase offset entries. */
7563 build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7564 /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7565 build_vbase_offset_vtbl_entries. */
7566 for (vbase = CLASSTYPE_VBASECLASSES (t);
7567 vbase;
7568 vbase = TREE_CHAIN (vbase))
7569 CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7571 /* If the target requires padding between data entries, add that now. */
7572 if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7574 tree cur, *prev;
7576 for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7578 tree add = cur;
7579 int i;
7581 for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7582 add = tree_cons (NULL_TREE,
7583 build1 (NOP_EXPR, vtable_entry_type,
7584 null_pointer_node),
7585 add);
7586 *prev = add;
7590 if (non_fn_entries_p)
7591 *non_fn_entries_p = list_length (vid.inits);
7593 /* Go through all the ordinary virtual functions, building up
7594 initializers. */
7595 vfun_inits = NULL_TREE;
7596 for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7598 tree delta;
7599 tree vcall_index;
7600 tree fn;
7601 tree init = NULL_TREE;
7603 fn = BV_FN (v);
7605 /* If the only definition of this function signature along our
7606 primary base chain is from a lost primary, this vtable slot will
7607 never be used, so just zero it out. This is important to avoid
7608 requiring extra thunks which cannot be generated with the function.
7610 We first check this in update_vtable_entry_for_fn, so we handle
7611 restored primary bases properly; we also need to do it here so we
7612 zero out unused slots in ctor vtables, rather than filling themff
7613 with erroneous values (though harmless, apart from relocation
7614 costs). */
7615 for (b = binfo; ; b = get_primary_binfo (b))
7617 /* We found a defn before a lost primary; go ahead as normal. */
7618 if (look_for_overrides_here (BINFO_TYPE (b), fn))
7619 break;
7621 /* The nearest definition is from a lost primary; clear the
7622 slot. */
7623 if (BINFO_LOST_PRIMARY_P (b))
7625 init = size_zero_node;
7626 break;
7630 if (! init)
7632 /* Pull the offset for `this', and the function to call, out of
7633 the list. */
7634 delta = BV_DELTA (v);
7635 vcall_index = BV_VCALL_INDEX (v);
7637 my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7638 my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7640 /* You can't call an abstract virtual function; it's abstract.
7641 So, we replace these functions with __pure_virtual. */
7642 if (DECL_PURE_VIRTUAL_P (fn))
7643 fn = abort_fndecl;
7644 else if (!integer_zerop (delta) || vcall_index)
7645 fn = make_thunk (fn, delta, vcall_index);
7646 /* Take the address of the function, considering it to be of an
7647 appropriate generic type. */
7648 init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7649 /* The address of a function can't change. */
7650 TREE_CONSTANT (init) = 1;
7653 /* And add it to the chain of initializers. */
7654 if (TARGET_VTABLE_USES_DESCRIPTORS)
7656 int i;
7657 if (init == size_zero_node)
7658 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7659 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7660 else
7661 for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7663 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7664 TREE_OPERAND (init, 0),
7665 build_int_2 (i, 0));
7666 TREE_CONSTANT (fdesc) = 1;
7668 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7671 else
7672 vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7675 /* The initializers for virtual functions were built up in reverse
7676 order; straighten them out now. */
7677 vfun_inits = nreverse (vfun_inits);
7679 /* The negative offset initializers are also in reverse order. */
7680 vid.inits = nreverse (vid.inits);
7682 /* Chain the two together. */
7683 return chainon (vid.inits, vfun_inits);
7686 /* Adds to vid->inits the initializers for the vbase and vcall
7687 offsets in BINFO, which is in the hierarchy dominated by T. */
7689 static void
7690 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7691 tree binfo;
7692 vtbl_init_data *vid;
7694 tree b;
7696 /* If this is a derived class, we must first create entries
7697 corresponding to the primary base class. */
7698 b = get_primary_binfo (binfo);
7699 if (b)
7700 build_vcall_and_vbase_vtbl_entries (b, vid);
7702 /* Add the vbase entries for this base. */
7703 build_vbase_offset_vtbl_entries (binfo, vid);
7704 /* Add the vcall entries for this base. */
7705 build_vcall_offset_vtbl_entries (binfo, vid);
7708 /* Returns the initializers for the vbase offset entries in the vtable
7709 for BINFO (which is part of the class hierarchy dominated by T), in
7710 reverse order. VBASE_OFFSET_INDEX gives the vtable index
7711 where the next vbase offset will go. */
7713 static void
7714 build_vbase_offset_vtbl_entries (binfo, vid)
7715 tree binfo;
7716 vtbl_init_data *vid;
7718 tree vbase;
7719 tree t;
7720 tree non_primary_binfo;
7722 /* If there are no virtual baseclasses, then there is nothing to
7723 do. */
7724 if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7725 return;
7727 t = vid->derived;
7729 /* We might be a primary base class. Go up the inheritance hierarchy
7730 until we find the most derived class of which we are a primary base:
7731 it is the offset of that which we need to use. */
7732 non_primary_binfo = binfo;
7733 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7735 tree b;
7737 /* If we have reached a virtual base, then it must be a primary
7738 base (possibly multi-level) of vid->binfo, or we wouldn't
7739 have called build_vcall_and_vbase_vtbl_entries for it. But it
7740 might be a lost primary, so just skip down to vid->binfo. */
7741 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7743 non_primary_binfo = vid->binfo;
7744 break;
7747 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7748 if (get_primary_binfo (b) != non_primary_binfo)
7749 break;
7750 non_primary_binfo = b;
7753 /* Go through the virtual bases, adding the offsets. */
7754 for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7755 vbase;
7756 vbase = TREE_CHAIN (vbase))
7758 tree b;
7759 tree delta;
7761 if (!TREE_VIA_VIRTUAL (vbase))
7762 continue;
7764 /* Find the instance of this virtual base in the complete
7765 object. */
7766 b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7768 /* If we've already got an offset for this virtual base, we
7769 don't need another one. */
7770 if (BINFO_VTABLE_PATH_MARKED (b))
7771 continue;
7772 SET_BINFO_VTABLE_PATH_MARKED (b);
7774 /* Figure out where we can find this vbase offset. */
7775 delta = size_binop (MULT_EXPR,
7776 vid->index,
7777 convert (ssizetype,
7778 TYPE_SIZE_UNIT (vtable_entry_type)));
7779 if (vid->primary_vtbl_p)
7780 BINFO_VPTR_FIELD (b) = delta;
7782 if (binfo != TYPE_BINFO (t))
7784 tree orig_vbase;
7786 /* Find the instance of this virtual base in the type of BINFO. */
7787 orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7788 BINFO_TYPE (binfo));
7790 /* The vbase offset had better be the same. */
7791 if (!tree_int_cst_equal (delta,
7792 BINFO_VPTR_FIELD (orig_vbase)))
7793 abort ();
7796 /* The next vbase will come at a more negative offset. */
7797 vid->index = size_binop (MINUS_EXPR, vid->index,
7798 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7800 /* The initializer is the delta from BINFO to this virtual base.
7801 The vbase offsets go in reverse inheritance-graph order, and
7802 we are walking in inheritance graph order so these end up in
7803 the right order. */
7804 delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7806 *vid->last_init
7807 = build_tree_list (NULL_TREE,
7808 fold (build1 (NOP_EXPR,
7809 vtable_entry_type,
7810 delta)));
7811 vid->last_init = &TREE_CHAIN (*vid->last_init);
7815 /* Adds the initializers for the vcall offset entries in the vtable
7816 for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7817 to VID->INITS. */
7819 static void
7820 build_vcall_offset_vtbl_entries (binfo, vid)
7821 tree binfo;
7822 vtbl_init_data *vid;
7824 /* We only need these entries if this base is a virtual base. We
7825 compute the indices -- but do not add to the vtable -- when
7826 building the main vtable for a class. */
7827 if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7829 /* We need a vcall offset for each of the virtual functions in this
7830 vtable. For example:
7832 class A { virtual void f (); };
7833 class B1 : virtual public A { virtual void f (); };
7834 class B2 : virtual public A { virtual void f (); };
7835 class C: public B1, public B2 { virtual void f (); };
7837 A C object has a primary base of B1, which has a primary base of A. A
7838 C also has a secondary base of B2, which no longer has a primary base
7839 of A. So the B2-in-C construction vtable needs a secondary vtable for
7840 A, which will adjust the A* to a B2* to call f. We have no way of
7841 knowing what (or even whether) this offset will be when we define B2,
7842 so we store this "vcall offset" in the A sub-vtable and look it up in
7843 a "virtual thunk" for B2::f.
7845 We need entries for all the functions in our primary vtable and
7846 in our non-virtual bases' secondary vtables. */
7847 vid->vbase = binfo;
7848 /* If we are just computing the vcall indices -- but do not need
7849 the actual entries -- not that. */
7850 if (!TREE_VIA_VIRTUAL (binfo))
7851 vid->generate_vcall_entries = false;
7852 /* Now, walk through the non-virtual bases, adding vcall offsets. */
7853 add_vcall_offset_vtbl_entries_r (binfo, vid);
7857 /* Build vcall offsets, starting with those for BINFO. */
7859 static void
7860 add_vcall_offset_vtbl_entries_r (binfo, vid)
7861 tree binfo;
7862 vtbl_init_data *vid;
7864 int i;
7865 tree primary_binfo;
7867 /* Don't walk into virtual bases -- except, of course, for the
7868 virtual base for which we are building vcall offsets. Any
7869 primary virtual base will have already had its offsets generated
7870 through the recursion in build_vcall_and_vbase_vtbl_entries. */
7871 if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7872 return;
7874 /* If BINFO has a primary base, process it first. */
7875 primary_binfo = get_primary_binfo (binfo);
7876 if (primary_binfo)
7877 add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7879 /* Add BINFO itself to the list. */
7880 add_vcall_offset_vtbl_entries_1 (binfo, vid);
7882 /* Scan the non-primary bases of BINFO. */
7883 for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7885 tree base_binfo;
7887 base_binfo = BINFO_BASETYPE (binfo, i);
7888 if (base_binfo != primary_binfo)
7889 add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7893 /* Called from build_vcall_offset_vtbl_entries_r. */
7895 static void
7896 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7897 tree binfo;
7898 vtbl_init_data* vid;
7900 tree derived_virtuals;
7901 tree base_virtuals;
7902 tree orig_virtuals;
7903 tree binfo_inits;
7904 /* If BINFO is a primary base, the most derived class which has BINFO as
7905 a primary base; otherwise, just BINFO. */
7906 tree non_primary_binfo;
7908 binfo_inits = NULL_TREE;
7910 /* We might be a primary base class. Go up the inheritance hierarchy
7911 until we find the most derived class of which we are a primary base:
7912 it is the BINFO_VIRTUALS there that we need to consider. */
7913 non_primary_binfo = binfo;
7914 while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7916 tree b;
7918 /* If we have reached a virtual base, then it must be vid->vbase,
7919 because we ignore other virtual bases in
7920 add_vcall_offset_vtbl_entries_r. In turn, it must be a primary
7921 base (possibly multi-level) of vid->binfo, or we wouldn't
7922 have called build_vcall_and_vbase_vtbl_entries for it. But it
7923 might be a lost primary, so just skip down to vid->binfo. */
7924 if (TREE_VIA_VIRTUAL (non_primary_binfo))
7926 if (non_primary_binfo != vid->vbase)
7927 abort ();
7928 non_primary_binfo = vid->binfo;
7929 break;
7932 b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7933 if (get_primary_binfo (b) != non_primary_binfo)
7934 break;
7935 non_primary_binfo = b;
7938 if (vid->ctor_vtbl_p)
7939 /* For a ctor vtable we need the equivalent binfo within the hierarchy
7940 where rtti_binfo is the most derived type. */
7941 non_primary_binfo = get_original_base
7942 (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7944 /* Make entries for the rest of the virtuals. */
7945 for (base_virtuals = BINFO_VIRTUALS (binfo),
7946 derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7947 orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7948 base_virtuals;
7949 base_virtuals = TREE_CHAIN (base_virtuals),
7950 derived_virtuals = TREE_CHAIN (derived_virtuals),
7951 orig_virtuals = TREE_CHAIN (orig_virtuals))
7953 tree orig_fn;
7954 tree fn;
7955 size_t i;
7956 tree vcall_offset;
7958 /* Find the declaration that originally caused this function to
7959 be present in BINFO_TYPE (binfo). */
7960 orig_fn = BV_FN (orig_virtuals);
7962 /* When processing BINFO, we only want to generate vcall slots for
7963 function slots introduced in BINFO. So don't try to generate
7964 one if the function isn't even defined in BINFO. */
7965 if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7966 continue;
7968 /* Find the overriding function. */
7969 fn = BV_FN (derived_virtuals);
7971 /* If there is already an entry for a function with the same
7972 signature as FN, then we do not need a second vcall offset.
7973 Check the list of functions already present in the derived
7974 class vtable. */
7975 for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i)
7977 tree derived_entry;
7979 derived_entry = VARRAY_TREE (vid->fns, i);
7980 if (same_signature_p (BV_FN (derived_entry), fn)
7981 /* We only use one vcall offset for virtual destructors,
7982 even though there are two virtual table entries. */
7983 || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7984 && DECL_DESTRUCTOR_P (fn)))
7985 break;
7987 if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7988 continue;
7990 /* If we are building these vcall offsets as part of building
7991 the vtable for the most derived class, remember the vcall
7992 offset. */
7993 if (vid->binfo == TYPE_BINFO (vid->derived))
7994 CLASSTYPE_VCALL_INDICES (vid->derived)
7995 = tree_cons (fn, vid->index, CLASSTYPE_VCALL_INDICES (vid->derived));
7997 /* The next vcall offset will be found at a more negative
7998 offset. */
7999 vid->index = size_binop (MINUS_EXPR, vid->index,
8000 ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8002 /* Keep track of this function. */
8003 VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
8005 if (vid->generate_vcall_entries)
8007 tree base;
8008 tree base_binfo;
8010 /* The FN comes from BASE. So, we must calculate the
8011 adjustment from vid->vbase to BASE. We can just look for
8012 BASE in the complete object because we are converting
8013 from a virtual base, so if there were multiple copies,
8014 there would not be a unique final overrider and
8015 vid->derived would be ill-formed. */
8016 base = DECL_CONTEXT (fn);
8017 base_binfo = lookup_base (vid->derived, base, ba_any, NULL);
8019 /* Compute the vcall offset. */
8020 /* As mentioned above, the vbase we're working on is a
8021 primary base of vid->binfo. But it might be a lost
8022 primary, so its BINFO_OFFSET might be wrong, so we just
8023 use the BINFO_OFFSET from vid->binfo. */
8024 vcall_offset = BINFO_OFFSET (vid->binfo);
8025 vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
8026 vcall_offset);
8027 vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
8028 vcall_offset));
8030 *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
8031 vid->last_init = &TREE_CHAIN (*vid->last_init);
8036 /* Return vtbl initializers for the RTTI entries coresponding to the
8037 BINFO's vtable. The RTTI entries should indicate the object given
8038 by VID->rtti_binfo. */
8040 static void
8041 build_rtti_vtbl_entries (binfo, vid)
8042 tree binfo;
8043 vtbl_init_data *vid;
8045 tree b;
8046 tree t;
8047 tree basetype;
8048 tree offset;
8049 tree decl;
8050 tree init;
8052 basetype = BINFO_TYPE (binfo);
8053 t = BINFO_TYPE (vid->rtti_binfo);
8055 /* To find the complete object, we will first convert to our most
8056 primary base, and then add the offset in the vtbl to that value. */
8057 b = binfo;
8058 while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8059 && !BINFO_LOST_PRIMARY_P (b))
8061 tree primary_base;
8063 primary_base = get_primary_binfo (b);
8064 my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8065 b = primary_base;
8067 offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8069 /* The second entry is the address of the typeinfo object. */
8070 if (flag_rtti)
8071 decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8072 else
8073 decl = integer_zero_node;
8075 /* Convert the declaration to a type that can be stored in the
8076 vtable. */
8077 init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8078 TREE_CONSTANT (init) = 1;
8079 *vid->last_init = build_tree_list (NULL_TREE, init);
8080 vid->last_init = &TREE_CHAIN (*vid->last_init);
8082 /* Add the offset-to-top entry. It comes earlier in the vtable that
8083 the the typeinfo entry. Convert the offset to look like a
8084 function pointer, so that we can put it in the vtable. */
8085 init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8086 TREE_CONSTANT (init) = 1;
8087 *vid->last_init = build_tree_list (NULL_TREE, init);
8088 vid->last_init = &TREE_CHAIN (*vid->last_init);