OSDN Git Service

2003-01-22 Nathanael Nerode <neroden@gcc.gnu.org>
[pf3gnuchains/gcc-fork.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2002, 2003  Free Software Foundation, Inc.
4    Contributed by Michael Tiemann (tiemann@cygnus.com)
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
12
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING.  If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22
23
24 /* High-level class interface.  */
25
26 #include "config.h"
27 #include "system.h"
28 #include "coretypes.h"
29 #include "tm.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "flags.h"
33 #include "rtl.h"
34 #include "output.h"
35 #include "toplev.h"
36 #include "lex.h"
37 #include "target.h"
38
39 /* The number of nested classes being processed.  If we are not in the
40    scope of any class, this is zero.  */
41
42 int current_class_depth;
43
44 /* In order to deal with nested classes, we keep a stack of classes.
45    The topmost entry is the innermost class, and is the entry at index
46    CURRENT_CLASS_DEPTH  */
47
48 typedef struct class_stack_node {
49   /* The name of the class.  */
50   tree name;
51
52   /* The _TYPE node for the class.  */
53   tree type;
54
55   /* The access specifier pending for new declarations in the scope of
56      this class.  */
57   tree access;
58
59   /* If were defining TYPE, the names used in this class.  */
60   splay_tree names_used;
61 }* class_stack_node_t;
62
63 typedef struct vtbl_init_data_s
64 {
65   /* The base for which we're building initializers.  */
66   tree binfo;
67   /* The type of the most-derived type.  */
68   tree derived;
69   /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
70      unless ctor_vtbl_p is true.  */
71   tree rtti_binfo;
72   /* The negative-index vtable initializers built up so far.  These
73      are in order from least negative index to most negative index.  */
74   tree inits;
75   /* The last (i.e., most negative) entry in INITS.  */
76   tree* last_init;
77   /* The binfo for the virtual base for which we're building
78      vcall offset initializers.  */
79   tree vbase;
80   /* The functions in vbase for which we have already provided vcall
81      offsets.  */
82   varray_type fns;
83   /* The vtable index of the next vcall or vbase offset.  */
84   tree index;
85   /* Nonzero if we are building the initializer for the primary
86      vtable.  */
87   int primary_vtbl_p;
88   /* Nonzero if we are building the initializer for a construction
89      vtable.  */
90   int ctor_vtbl_p;
91   /* True when adding vcall offset entries to the vtable.  False when
92      merely computing the indices.  */
93   bool generate_vcall_entries;
94 } vtbl_init_data;
95
96 /* The type of a function passed to walk_subobject_offsets.  */
97 typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
98
99 /* The stack itself.  This is a dynamically resized array.  The
100    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
101 static int current_class_stack_size;
102 static class_stack_node_t current_class_stack;
103
104 /* An array of all local classes present in this translation unit, in
105    declaration order.  */
106 varray_type local_classes;
107
108 static tree get_vfield_name (tree);
109 static void finish_struct_anon (tree);
110 static tree get_vtable_name (tree);
111 static tree get_basefndecls (tree, tree);
112 static int build_primary_vtable (tree, tree);
113 static int build_secondary_vtable (tree, tree);
114 static void finish_vtbls (tree);
115 static void modify_vtable_entry (tree, tree, tree, tree, tree *);
116 static tree delete_duplicate_fields_1 (tree, tree);
117 static void delete_duplicate_fields (tree);
118 static void finish_struct_bits (tree);
119 static int alter_access (tree, tree, tree);
120 static void handle_using_decl (tree, tree);
121 static void check_for_override (tree, tree);
122 static tree dfs_modify_vtables (tree, void *);
123 static tree modify_all_vtables (tree, tree);
124 static void determine_primary_base (tree);
125 static void finish_struct_methods (tree);
126 static void maybe_warn_about_overly_private_class (tree);
127 static int field_decl_cmp (const void *, const void *);
128 static int resort_field_decl_cmp (const void *, const void *);
129 static int method_name_cmp (const void *, const void *);
130 static int resort_method_name_cmp (const void *, const void *);
131 static void add_implicitly_declared_members (tree, int, int, int);
132 static tree fixed_type_or_null (tree, int *, int *);
133 static tree resolve_address_of_overloaded_function (tree, tree, int,
134                                                           int, int, tree);
135 static tree build_vtable_entry_ref (tree, tree, tree);
136 static tree build_vtbl_ref_1 (tree, tree);
137 static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
138 static int count_fields (tree);
139 static int add_fields_to_vec (tree, tree, int);
140 static void check_bitfield_decl (tree);
141 static void check_field_decl (tree, tree, int *, int *, int *, int *);
142 static void check_field_decls (tree, tree *, int *, int *, int *);
143 static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
144 static void build_base_fields (record_layout_info, splay_tree, tree *);
145 static void check_methods (tree);
146 static void remove_zero_width_bit_fields (tree);
147 static void check_bases (tree, int *, int *, int *);
148 static void check_bases_and_members (tree);
149 static tree create_vtable_ptr (tree, tree *);
150 static void include_empty_classes (record_layout_info);
151 static void layout_class_type (tree, tree *);
152 static void fixup_pending_inline (tree);
153 static void fixup_inline_methods (tree);
154 static void set_primary_base (tree, tree);
155 static void propagate_binfo_offsets (tree, tree, tree);
156 static void layout_virtual_bases (record_layout_info, splay_tree);
157 static tree dfs_set_offset_for_unshared_vbases (tree, void *);
158 static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
159 static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
160 static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
161 static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
162 static void add_vcall_offset (tree, tree, vtbl_init_data *);
163 static void layout_vtable_decl (tree, int);
164 static tree dfs_find_final_overrider (tree, void *);
165 static tree find_final_overrider (tree, tree, tree);
166 static int make_new_vtable (tree, tree);
167 static int maybe_indent_hierarchy (FILE *, int, int);
168 static void dump_class_hierarchy_r (FILE *, int, tree, tree, int);
169 static void dump_class_hierarchy (tree);
170 static void dump_array (FILE *, tree);
171 static void dump_vtable (tree, tree, tree);
172 static void dump_vtt (tree, tree);
173 static tree build_vtable (tree, tree, tree);
174 static void initialize_vtable (tree, tree);
175 static void initialize_array (tree, tree);
176 static void layout_nonempty_base_or_field (record_layout_info,
177                                                    tree, tree, splay_tree);
178 static tree end_of_class (tree, int);
179 static bool layout_empty_base (tree, tree, splay_tree, tree);
180 static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
181 static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
182                                                tree);
183 static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
184 static void build_vcall_and_vbase_vtbl_entries (tree, 
185                                                         vtbl_init_data *);
186 static void force_canonical_binfo_r (tree, tree, tree, tree);
187 static void force_canonical_binfo (tree, tree, tree, tree);
188 static tree dfs_unshared_virtual_bases (tree, void *);
189 static void mark_primary_bases (tree);
190 static tree mark_primary_virtual_base (tree, tree);
191 static void clone_constructors_and_destructors (tree);
192 static tree build_clone (tree, tree);
193 static void update_vtable_entry_for_fn (tree, tree, tree, tree *);
194 static tree copy_virtuals (tree);
195 static void build_ctor_vtbl_group (tree, tree);
196 static void build_vtt (tree);
197 static tree binfo_ctor_vtable (tree);
198 static tree *build_vtt_inits (tree, tree, tree *, tree *);
199 static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
200 static tree dfs_ctor_vtable_bases_queue_p (tree, void *data);
201 static tree dfs_fixup_binfo_vtbls (tree, void *);
202 static tree get_original_base (tree, tree);
203 static tree dfs_get_primary_binfo (tree, void*);
204 static int record_subobject_offset (tree, tree, splay_tree);
205 static int check_subobject_offset (tree, tree, splay_tree);
206 static int walk_subobject_offsets (tree, subobject_offset_fn,
207                                            tree, splay_tree, tree, int);
208 static void record_subobject_offsets (tree, tree, splay_tree, int);
209 static int layout_conflict_p (tree, tree, splay_tree, int);
210 static int splay_tree_compare_integer_csts (splay_tree_key k1,
211                                                     splay_tree_key k2);
212 static void warn_about_ambiguous_bases (tree);
213 static bool type_requires_array_cookie (tree);
214 static bool contains_empty_class_p (tree);
215 static tree dfs_base_derived_from (tree, void *);
216 static bool base_derived_from (tree, tree);
217 static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
218 static tree end_of_base (tree);
219 static tree get_vcall_index (tree, tree);
220
221 /* Macros for dfs walking during vtt construction. See
222    dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
223    and dfs_fixup_binfo_vtbls.  */
224 #define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
225 #define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
226
227 /* Variables shared between class.c and call.c.  */
228
229 #ifdef GATHER_STATISTICS
230 int n_vtables = 0;
231 int n_vtable_entries = 0;
232 int n_vtable_searches = 0;
233 int n_vtable_elems = 0;
234 int n_convert_harshness = 0;
235 int n_compute_conversion_costs = 0;
236 int n_build_method_call = 0;
237 int n_inner_fields_searched = 0;
238 #endif
239
240 /* Convert to or from a base subobject.  EXPR is an expression of type
241    `A' or `A*', an expression of type `B' or `B*' is returned.  To
242    convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
243    the B base instance within A.  To convert base A to derived B, CODE
244    is MINUS_EXPR and BINFO is the binfo for the A instance within B.
245    In this latter case, A must not be a morally virtual base of B.
246    NONNULL is true if EXPR is known to be non-NULL (this is only
247    needed when EXPR is of pointer type).  CV qualifiers are preserved
248    from EXPR.  */
249
250 tree
251 build_base_path (enum tree_code code,
252                  tree expr,
253                  tree binfo,
254                  int nonnull)
255 {
256   tree v_binfo = NULL_TREE;
257   tree d_binfo = NULL_TREE;
258   tree probe;
259   tree offset;
260   tree target_type;
261   tree null_test = NULL;
262   tree ptr_target_type;
263   int fixed_type_p;
264   int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
265
266   if (expr == error_mark_node || binfo == error_mark_node || !binfo)
267     return error_mark_node;
268
269   for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
270     {
271       d_binfo = probe;
272       if (!v_binfo && TREE_VIA_VIRTUAL (probe))
273         v_binfo = probe;
274     }
275
276   probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
277   if (want_pointer)
278     probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
279   
280   my_friendly_assert (code == MINUS_EXPR
281                       ? same_type_p (BINFO_TYPE (binfo), probe)
282                       : code == PLUS_EXPR
283                       ? same_type_p (BINFO_TYPE (d_binfo), probe)
284                       : false, 20010723);
285   
286   if (code == MINUS_EXPR && v_binfo)
287     {
288       error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
289              BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
290       return error_mark_node;
291     }
292
293   if (!want_pointer)
294     /* This must happen before the call to save_expr.  */
295     expr = build_unary_op (ADDR_EXPR, expr, 0);
296
297   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
298   if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
299     expr = save_expr (expr);
300
301   if (want_pointer && !nonnull)
302     null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
303   
304   offset = BINFO_OFFSET (binfo);
305   
306   if (v_binfo && fixed_type_p <= 0)
307     {
308       /* Going via virtual base V_BINFO.  We need the static offset
309          from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
310          V_BINFO.  That offset is an entry in D_BINFO's vtable.  */
311       tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
312                                         TREE_TYPE (TREE_TYPE (expr)));
313       
314       v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
315       
316       v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
317                         v_offset,  BINFO_VPTR_FIELD (v_binfo));
318       v_offset = build1 (NOP_EXPR, 
319                          build_pointer_type (ptrdiff_type_node),
320                          v_offset);
321       v_offset = build_indirect_ref (v_offset, NULL);
322       TREE_CONSTANT (v_offset) = 1;
323
324       offset = cp_convert (ptrdiff_type_node,
325                            size_diffop (offset, BINFO_OFFSET (v_binfo)));
326
327       if (!integer_zerop (offset))
328         v_offset = build (code, ptrdiff_type_node, v_offset, offset);
329
330       if (fixed_type_p < 0)
331         /* Negative fixed_type_p means this is a constructor or destructor;
332            virtual base layout is fixed in in-charge [cd]tors, but not in
333            base [cd]tors.  */
334         offset = build (COND_EXPR, ptrdiff_type_node,
335                         build (EQ_EXPR, boolean_type_node,
336                                current_in_charge_parm, integer_zero_node),
337                         v_offset,
338                         BINFO_OFFSET (binfo));
339       else
340         offset = v_offset;
341     }
342
343   target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
344   
345   target_type = cp_build_qualified_type
346     (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
347   ptr_target_type = build_pointer_type (target_type);
348   if (want_pointer)
349     target_type = ptr_target_type;
350   
351   expr = build1 (NOP_EXPR, ptr_target_type, expr);
352
353   if (!integer_zerop (offset))
354     expr = build (code, ptr_target_type, expr, offset);
355   else
356     null_test = NULL;
357   
358   if (!want_pointer)
359     expr = build_indirect_ref (expr, NULL);
360
361   if (null_test)
362     expr = build (COND_EXPR, target_type, null_test,
363                   build1 (NOP_EXPR, target_type, integer_zero_node),
364                   expr);
365
366   return expr;
367 }
368
369 /* Convert OBJECT to the base TYPE.  If CHECK_ACCESS is true, an error
370    message is emitted if TYPE is inaccessible.  OBJECT is assumed to
371    be non-NULL.  */
372
373 tree
374 convert_to_base (tree object, tree type, bool check_access)
375 {
376   tree binfo;
377
378   binfo = lookup_base (TREE_TYPE (object), type, 
379                        check_access ? ba_check : ba_ignore, 
380                        NULL);
381   if (!binfo || binfo == error_mark_node)
382     return error_mark_node;
383
384   return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
385 }
386
387 \f
388 /* Virtual function things.  */
389
390 static tree
391 build_vtable_entry_ref (tree array_ref, tree instance, tree idx)
392 {
393   tree i, i2, vtable, first_fn, basetype;
394
395   basetype = TREE_TYPE (instance);
396   if (TREE_CODE (basetype) == REFERENCE_TYPE)
397     basetype = TREE_TYPE (basetype);
398
399   vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
400   first_fn = TYPE_BINFO_VTABLE (basetype);
401
402   i = fold (build_array_ref (first_fn, idx));
403   i = fold (build_c_cast (ptrdiff_type_node,
404                           build_unary_op (ADDR_EXPR, i, 0)));
405   i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
406   i2 = fold (build_c_cast (ptrdiff_type_node,
407                            build_unary_op (ADDR_EXPR, i2, 0)));
408   i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
409
410   if (TREE_CODE (i) != INTEGER_CST)
411     abort ();
412
413   return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
414 }
415
416 /* Given an object INSTANCE, return an expression which yields the
417    vtable element corresponding to INDEX.  There are many special
418    cases for INSTANCE which we take care of here, mainly to avoid
419    creating extra tree nodes when we don't have to.  */
420
421 static tree
422 build_vtbl_ref_1 (tree instance, tree idx)
423 {
424   tree aref;
425   tree vtbl = NULL_TREE;
426
427   /* Try to figure out what a reference refers to, and
428      access its virtual function table directly.  */
429
430   int cdtorp = 0;
431   tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
432
433   tree basetype = TREE_TYPE (instance);
434   if (TREE_CODE (basetype) == REFERENCE_TYPE)
435     basetype = TREE_TYPE (basetype);
436
437   if (fixed_type && !cdtorp)
438     {
439       tree binfo = lookup_base (fixed_type, basetype,
440                                 ba_ignore|ba_quiet, NULL);
441       if (binfo)
442         vtbl = BINFO_VTABLE (binfo);
443     }
444
445   if (!vtbl)
446     {
447       vtbl = build_vfield_ref (instance, basetype);
448     }
449
450   assemble_external (vtbl);
451
452   aref = build_array_ref (vtbl, idx);
453   TREE_CONSTANT (aref) = 1;
454
455   return aref;
456 }
457
458 tree
459 build_vtbl_ref (tree instance, tree idx)
460 {
461   tree aref = build_vtbl_ref_1 (instance, idx);
462
463   if (flag_vtable_gc)
464     aref = build_vtable_entry_ref (aref, instance, idx);
465
466   return aref;
467 }
468
469 /* Given an object INSTANCE, return an expression which yields a
470    function pointer corresponding to vtable element INDEX.  */
471
472 tree
473 build_vfn_ref (tree instance, tree idx)
474 {
475   tree aref = build_vtbl_ref_1 (instance, idx);
476
477   /* When using function descriptors, the address of the
478      vtable entry is treated as a function pointer.  */
479   if (TARGET_VTABLE_USES_DESCRIPTORS)
480     aref = build1 (NOP_EXPR, TREE_TYPE (aref),
481                    build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
482
483   if (flag_vtable_gc)
484     aref = build_vtable_entry_ref (aref, instance, idx);
485
486   return aref;
487 }
488
489 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
490    for the given TYPE.  */
491
492 static tree
493 get_vtable_name (tree type)
494 {
495   return mangle_vtbl_for_type (type);
496 }
497
498 /* Return an IDENTIFIER_NODE for the name of the virtual table table
499    for TYPE.  */
500
501 tree
502 get_vtt_name (tree type)
503 {
504   return mangle_vtt_for_type (type);
505 }
506
507 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
508    (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
509    Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
510
511 static tree
512 build_vtable (tree class_type, tree name, tree vtable_type)
513 {
514   tree decl;
515
516   decl = build_lang_decl (VAR_DECL, name, vtable_type);
517   /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
518      now to avoid confusion in mangle_decl.  */
519   SET_DECL_ASSEMBLER_NAME (decl, name);
520   DECL_CONTEXT (decl) = class_type;
521   DECL_ARTIFICIAL (decl) = 1;
522   TREE_STATIC (decl) = 1;
523   TREE_READONLY (decl) = 1;
524   DECL_VIRTUAL_P (decl) = 1;
525   DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
526
527   import_export_vtable (decl, class_type, 0);
528
529   return decl;
530 }
531
532 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
533    or even complete.  If this does not exist, create it.  If COMPLETE is
534    nonzero, then complete the definition of it -- that will render it
535    impossible to actually build the vtable, but is useful to get at those
536    which are known to exist in the runtime.  */
537
538 tree 
539 get_vtable_decl (tree type, int complete)
540 {
541   tree decl;
542
543   if (CLASSTYPE_VTABLES (type))
544     return CLASSTYPE_VTABLES (type);
545   
546   decl = build_vtable (type, get_vtable_name (type), void_type_node);
547   CLASSTYPE_VTABLES (type) = decl;
548
549   /* At one time the vtable info was grabbed 2 words at a time.  This
550      fails on sparc unless you have 8-byte alignment.  (tiemann) */
551   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
552                            DECL_ALIGN (decl));
553
554   if (complete)
555     {
556       DECL_EXTERNAL (decl) = 1;
557       cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
558     }
559
560   return decl;
561 }
562
563 /* Returns a copy of the BINFO_VIRTUALS list in BINFO.  The
564    BV_VCALL_INDEX for each entry is cleared.  */
565
566 static tree
567 copy_virtuals (tree binfo)
568 {
569   tree copies;
570   tree t;
571
572   copies = copy_list (BINFO_VIRTUALS (binfo));
573   for (t = copies; t; t = TREE_CHAIN (t))
574     BV_VCALL_INDEX (t) = NULL_TREE;
575
576   return copies;
577 }
578
579 /* Build the primary virtual function table for TYPE.  If BINFO is
580    non-NULL, build the vtable starting with the initial approximation
581    that it is the same as the one which is the head of the association
582    list.  Returns a nonzero value if a new vtable is actually
583    created.  */
584
585 static int
586 build_primary_vtable (tree binfo, tree type)
587 {
588   tree decl;
589   tree virtuals;
590
591   decl = get_vtable_decl (type, /*complete=*/0);
592   
593   if (binfo)
594     {
595       if (BINFO_NEW_VTABLE_MARKED (binfo, type))
596         /* We have already created a vtable for this base, so there's
597            no need to do it again.  */
598         return 0;
599       
600       virtuals = copy_virtuals (binfo);
601       TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
602       DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
603       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
604     }
605   else
606     {
607       my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
608                           20000118);
609       virtuals = NULL_TREE;
610     }
611
612 #ifdef GATHER_STATISTICS
613   n_vtables += 1;
614   n_vtable_elems += list_length (virtuals);
615 #endif
616
617   /* Initialize the association list for this type, based
618      on our first approximation.  */
619   TYPE_BINFO_VTABLE (type) = decl;
620   TYPE_BINFO_VIRTUALS (type) = virtuals;
621   SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
622   return 1;
623 }
624
625 /* Give BINFO a new virtual function table which is initialized
626    with a skeleton-copy of its original initialization.  The only
627    entry that changes is the `delta' entry, so we can really
628    share a lot of structure.
629
630    FOR_TYPE is the most derived type which caused this table to
631    be needed.
632
633    Returns nonzero if we haven't met BINFO before.
634
635    The order in which vtables are built (by calling this function) for
636    an object must remain the same, otherwise a binary incompatibility
637    can result.  */
638
639 static int
640 build_secondary_vtable (tree binfo, tree for_type)
641 {
642   my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
643
644   if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
645     /* We already created a vtable for this base.  There's no need to
646        do it again.  */
647     return 0;
648
649   /* Remember that we've created a vtable for this BINFO, so that we
650      don't try to do so again.  */
651   SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
652   
653   /* Make fresh virtual list, so we can smash it later.  */
654   BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
655
656   /* Secondary vtables are laid out as part of the same structure as
657      the primary vtable.  */
658   BINFO_VTABLE (binfo) = NULL_TREE;
659   return 1;
660 }
661
662 /* Create a new vtable for BINFO which is the hierarchy dominated by
663    T. Return nonzero if we actually created a new vtable.  */
664
665 static int
666 make_new_vtable (tree t, tree binfo)
667 {
668   if (binfo == TYPE_BINFO (t))
669     /* In this case, it is *type*'s vtable we are modifying.  We start
670        with the approximation that its vtable is that of the
671        immediate base class.  */
672     /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
673        since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now.  */
674     return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
675                                  t);
676   else
677     /* This is our very own copy of `basetype' to play with.  Later,
678        we will fill in all the virtual functions that override the
679        virtual functions in these base classes which are not defined
680        by the current type.  */
681     return build_secondary_vtable (binfo, t);
682 }
683
684 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
685    (which is in the hierarchy dominated by T) list FNDECL as its
686    BV_FN.  DELTA is the required constant adjustment from the `this'
687    pointer where the vtable entry appears to the `this' required when
688    the function is actually called.  */
689
690 static void
691 modify_vtable_entry (tree t,
692                      tree binfo, 
693                      tree fndecl, 
694                      tree delta, 
695                      tree* virtuals)
696 {
697   tree v;
698
699   v = *virtuals;
700
701   if (fndecl != BV_FN (v)
702       || !tree_int_cst_equal (delta, BV_DELTA (v)))
703     {
704       /* We need a new vtable for BINFO.  */
705       if (make_new_vtable (t, binfo))
706         {
707           /* If we really did make a new vtable, we also made a copy
708              of the BINFO_VIRTUALS list.  Now, we have to find the
709              corresponding entry in that list.  */
710           *virtuals = BINFO_VIRTUALS (binfo);
711           while (BV_FN (*virtuals) != BV_FN (v))
712             *virtuals = TREE_CHAIN (*virtuals);
713           v = *virtuals;
714         }
715
716       BV_DELTA (v) = delta;
717       BV_VCALL_INDEX (v) = NULL_TREE;
718       BV_FN (v) = fndecl;
719     }
720 }
721
722 \f
723 /* Add method METHOD to class TYPE.  If ERROR_P is true, we are adding
724    the method after the class has already been defined because a
725    declaration for it was seen.  (Even though that is erroneous, we
726    add the method for improved error recovery.)  */
727
728 void
729 add_method (tree type, tree method, int error_p)
730 {
731   int using = (DECL_CONTEXT (method) != type);
732   int len;
733   int slot;
734   tree method_vec;
735   int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
736                          && DECL_TEMPLATE_CONV_FN_P (method));
737
738   if (!CLASSTYPE_METHOD_VEC (type))
739     /* Make a new method vector.  We start with 8 entries.  We must
740        allocate at least two (for constructors and destructors), and
741        we're going to end up with an assignment operator at some point
742        as well.
743        
744        We could use a TREE_LIST for now, and convert it to a TREE_VEC
745        in finish_struct, but we would probably waste more memory
746        making the links in the list than we would by over-allocating
747        the size of the vector here.  Furthermore, we would complicate
748        all the code that expects this to be a vector.  */
749     CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
750
751   method_vec = CLASSTYPE_METHOD_VEC (type);
752   len = TREE_VEC_LENGTH (method_vec);
753
754   /* Constructors and destructors go in special slots.  */
755   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
756     slot = CLASSTYPE_CONSTRUCTOR_SLOT;
757   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
758     slot = CLASSTYPE_DESTRUCTOR_SLOT;
759   else
760     {
761       int have_template_convs_p = 0;
762       
763       /* See if we already have an entry with this name.  */
764       for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
765         {
766           tree m = TREE_VEC_ELT (method_vec, slot);
767
768           if (!m)
769             break;
770           m = OVL_CURRENT (m);
771           
772           if (template_conv_p)
773             {
774               have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
775                                        && DECL_TEMPLATE_CONV_FN_P (m));
776               
777               /* If we need to move things up, see if there's
778                  space.  */
779               if (!have_template_convs_p)
780                 {
781                   slot = len - 1;
782                   if (TREE_VEC_ELT (method_vec, slot))
783                     slot++;
784                 }
785               break;
786             }
787           if (DECL_NAME (m) == DECL_NAME (method))
788             break;
789         }
790       
791       if (slot == len)
792         {
793           /* We need a bigger method vector.  */
794           int new_len;
795           tree new_vec;
796
797           /* In the non-error case, we are processing a class
798              definition.  Double the size of the vector to give room
799              for new methods.  */
800           if (!error_p)
801             new_len = 2 * len;
802           /* In the error case, the vector is already complete.  We
803              don't expect many errors, and the rest of the front-end
804              will get confused if there are empty slots in the vector.  */
805           else
806             new_len = len + 1;
807
808           new_vec = make_tree_vec (new_len);
809           memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
810                   len * sizeof (tree));
811           len = new_len;
812           method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
813         }
814
815       if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
816         {
817           /* Type conversion operators have to come before ordinary
818              methods; add_conversions depends on this to speed up
819              looking for conversion operators.  So, if necessary, we
820              slide some of the vector elements up.  In theory, this
821              makes this algorithm O(N^2) but we don't expect many
822              conversion operators.  */
823           if (template_conv_p)
824             slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
825           else
826             for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
827               {
828                 tree fn = TREE_VEC_ELT (method_vec, slot);
829   
830                 if (!fn)
831                   /* There are no more entries in the vector, so we
832                      can insert the new conversion operator here.  */
833                   break;
834                   
835                 if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
836                   /* We can insert the new function right at the
837                      SLOTth position.  */
838                   break;
839               }
840
841           if (template_conv_p && have_template_convs_p)
842             /*OK*/;
843           else if (!TREE_VEC_ELT (method_vec, slot))
844             /* There is nothing in the Ith slot, so we can avoid
845                moving anything.  */
846                 ; 
847           else
848             {
849               /* We know the last slot in the vector is empty
850                  because we know that at this point there's room
851                  for a new function.  */
852               memmove (&TREE_VEC_ELT (method_vec, slot + 1),
853                        &TREE_VEC_ELT (method_vec, slot),
854                        (len - slot - 1) * sizeof (tree));
855               TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
856             }
857         }
858     }
859       
860   if (template_class_depth (type))
861     /* TYPE is a template class.  Don't issue any errors now; wait
862        until instantiation time to complain.  */
863     ;
864   else
865     {
866       tree fns;
867
868       /* Check to see if we've already got this method.  */
869       for (fns = TREE_VEC_ELT (method_vec, slot);
870            fns;
871            fns = OVL_NEXT (fns))
872         {
873           tree fn = OVL_CURRENT (fns);
874           tree parms1;
875           tree parms2;
876           bool same = 1;
877
878           if (TREE_CODE (fn) != TREE_CODE (method))
879             continue;
880
881           /* [over.load] Member function declarations with the
882              same name and the same parameter types cannot be
883              overloaded if any of them is a static member
884              function declaration.
885
886              [namespace.udecl] When a using-declaration brings names
887              from a base class into a derived class scope, member
888              functions in the derived class override and/or hide member
889              functions with the same name and parameter types in a base
890              class (rather than conflicting).  */
891           parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
892           parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
893
894           /* Compare the quals on the 'this' parm.  Don't compare
895              the whole types, as used functions are treated as
896              coming from the using class in overload resolution.  */
897           if (! DECL_STATIC_FUNCTION_P (fn)
898               && ! DECL_STATIC_FUNCTION_P (method)
899               && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
900                   != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
901             same = 0;
902           
903           /* For templates, the template parms must be identical.  */
904           if (TREE_CODE (fn) == TEMPLATE_DECL
905               && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
906                                        DECL_TEMPLATE_PARMS (method)))
907             same = 0;
908           
909           if (! DECL_STATIC_FUNCTION_P (fn))
910             parms1 = TREE_CHAIN (parms1);
911           if (! DECL_STATIC_FUNCTION_P (method))
912             parms2 = TREE_CHAIN (parms2);
913
914           if (same && compparms (parms1, parms2) 
915               && (!DECL_CONV_FN_P (fn) 
916                   || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
917                                   TREE_TYPE (TREE_TYPE (method)))))
918             {
919               if (using && DECL_CONTEXT (fn) == type)
920                 /* Defer to the local function.  */
921                 return;
922               else
923                 {
924                   cp_error_at ("`%#D' and `%#D' cannot be overloaded",
925                                method, fn, method);
926
927                   /* We don't call duplicate_decls here to merge
928                      the declarations because that will confuse
929                      things if the methods have inline
930                      definitions.  In particular, we will crash
931                      while processing the definitions.  */
932                   return;
933                 }
934             }
935         }
936     }
937
938   /* Actually insert the new method.  */
939   TREE_VEC_ELT (method_vec, slot) 
940     = build_overload (method, TREE_VEC_ELT (method_vec, slot));
941
942   /* Add the new binding.  */ 
943   if (!DECL_CONSTRUCTOR_P (method)
944       && !DECL_DESTRUCTOR_P (method))
945     push_class_level_binding (DECL_NAME (method),
946                               TREE_VEC_ELT (method_vec, slot));
947 }
948
949 /* Subroutines of finish_struct.  */
950
951 /* Look through the list of fields for this struct, deleting
952    duplicates as we go.  This must be recursive to handle
953    anonymous unions.
954
955    FIELD is the field which may not appear anywhere in FIELDS.
956    FIELD_PTR, if non-null, is the starting point at which
957    chained deletions may take place.
958    The value returned is the first acceptable entry found
959    in FIELDS.
960
961    Note that anonymous fields which are not of UNION_TYPE are
962    not duplicates, they are just anonymous fields.  This happens
963    when we have unnamed bitfields, for example.  */
964
965 static tree
966 delete_duplicate_fields_1 (tree field, tree fields)
967 {
968   tree x;
969   tree prev = 0;
970   if (DECL_NAME (field) == 0)
971     {
972       if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
973         return fields;
974
975       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
976         fields = delete_duplicate_fields_1 (x, fields);
977       return fields;
978     }
979   else
980     {
981       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
982         {
983           if (DECL_NAME (x) == 0)
984             {
985               if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
986                 continue;
987               TYPE_FIELDS (TREE_TYPE (x))
988                 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
989               if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
990                 {
991                   if (prev == 0)
992                     fields = TREE_CHAIN (fields);
993                   else
994                     TREE_CHAIN (prev) = TREE_CHAIN (x);
995                 }
996             }
997           else if (TREE_CODE (field) == USING_DECL)
998             /* A using declaration is allowed to appear more than
999                once.  We'll prune these from the field list later, and
1000                handle_using_decl will complain about invalid multiple
1001                uses.  */
1002             ;
1003           else if (DECL_NAME (field) == DECL_NAME (x))
1004             {
1005               if (TREE_CODE (field) == CONST_DECL
1006                   && TREE_CODE (x) == CONST_DECL)
1007                 cp_error_at ("duplicate enum value `%D'", x);
1008               else if (TREE_CODE (field) == CONST_DECL
1009                        || TREE_CODE (x) == CONST_DECL)
1010                 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1011                              x);
1012               else if (DECL_DECLARES_TYPE_P (field)
1013                        && DECL_DECLARES_TYPE_P (x))
1014                 {
1015                   if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1016                     continue;
1017                   cp_error_at ("duplicate nested type `%D'", x);
1018                 }
1019               else if (DECL_DECLARES_TYPE_P (field)
1020                        || DECL_DECLARES_TYPE_P (x))
1021                 {
1022                   /* Hide tag decls.  */
1023                   if ((TREE_CODE (field) == TYPE_DECL
1024                        && DECL_ARTIFICIAL (field))
1025                       || (TREE_CODE (x) == TYPE_DECL
1026                           && DECL_ARTIFICIAL (x)))
1027                     continue;
1028                   cp_error_at ("duplicate field `%D' (as type and non-type)",
1029                                x);
1030                 }
1031               else
1032                 cp_error_at ("duplicate member `%D'", x);
1033               if (prev == 0)
1034                 fields = TREE_CHAIN (fields);
1035               else
1036                 TREE_CHAIN (prev) = TREE_CHAIN (x);
1037             }
1038         }
1039     }
1040   return fields;
1041 }
1042
1043 static void
1044 delete_duplicate_fields (tree fields)
1045 {
1046   tree x;
1047   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1048     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1049 }
1050
1051 /* Change the access of FDECL to ACCESS in T.  Return 1 if change was
1052    legit, otherwise return 0.  */
1053
1054 static int
1055 alter_access (tree t, tree fdecl, tree access)
1056 {
1057   tree elem;
1058
1059   if (!DECL_LANG_SPECIFIC (fdecl))
1060     retrofit_lang_decl (fdecl);
1061
1062   if (DECL_DISCRIMINATOR_P (fdecl))
1063     abort ();
1064
1065   elem = purpose_member (t, DECL_ACCESS (fdecl));
1066   if (elem)
1067     {
1068       if (TREE_VALUE (elem) != access)
1069         {
1070           if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1071             cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1072           else
1073             error ("conflicting access specifications for field `%s', ignored",
1074                    IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1075         }
1076       else
1077         {
1078           /* They're changing the access to the same thing they changed
1079              it to before.  That's OK.  */
1080           ;
1081         }
1082     }
1083   else
1084     {
1085       enforce_access (t, fdecl);
1086       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1087       return 1;
1088     }
1089   return 0;
1090 }
1091
1092 /* Process the USING_DECL, which is a member of T.  */
1093
1094 static void
1095 handle_using_decl (tree using_decl, tree t)
1096 {
1097   tree ctype = DECL_INITIAL (using_decl);
1098   tree name = DECL_NAME (using_decl);
1099   tree access
1100     = TREE_PRIVATE (using_decl) ? access_private_node
1101     : TREE_PROTECTED (using_decl) ? access_protected_node
1102     : access_public_node;
1103   tree fdecl, binfo;
1104   tree flist = NULL_TREE;
1105   tree old_value;
1106
1107   binfo = lookup_base (t, ctype, ba_any, NULL);
1108   if (! binfo)
1109     {
1110       error_not_base_type (t, ctype);
1111       return;
1112     }
1113   
1114   if (constructor_name_p (name, ctype))
1115     {
1116       cp_error_at ("`%D' names constructor", using_decl);
1117       return;
1118     }
1119   if (constructor_name_p (name, t))
1120     {
1121       cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1122       return;
1123     }
1124
1125   fdecl = lookup_member (binfo, name, 0, 0);
1126   
1127   if (!fdecl)
1128     {
1129       cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1130       return;
1131     }
1132
1133   if (BASELINK_P (fdecl))
1134     /* Ignore base type this came from.  */
1135     fdecl = BASELINK_FUNCTIONS (fdecl);
1136
1137   old_value = IDENTIFIER_CLASS_VALUE (name);
1138   if (old_value)
1139     {
1140       if (is_overloaded_fn (old_value))
1141         old_value = OVL_CURRENT (old_value);
1142
1143       if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1144         /* OK */;
1145       else
1146         old_value = NULL_TREE;
1147     }
1148
1149   if (is_overloaded_fn (fdecl))
1150     flist = fdecl;
1151
1152   if (! old_value)
1153     ;
1154   else if (is_overloaded_fn (old_value))
1155     {
1156       if (flist)
1157         /* It's OK to use functions from a base when there are functions with
1158            the same name already present in the current class.  */;
1159       else
1160         {
1161           cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1162           cp_error_at ("  because of local method `%#D' with same name",
1163                        OVL_CURRENT (old_value));
1164           return;
1165         }
1166     }
1167   else if (!DECL_ARTIFICIAL (old_value))
1168     {
1169       cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1170       cp_error_at ("  because of local member `%#D' with same name", old_value);
1171       return;
1172     }
1173   
1174   /* Make type T see field decl FDECL with access ACCESS.*/
1175   if (flist)
1176     for (; flist; flist = OVL_NEXT (flist))
1177       {
1178         add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1179         alter_access (t, OVL_CURRENT (flist), access);
1180       }
1181   else
1182     alter_access (t, fdecl, access);
1183 }
1184 \f
1185 /* Run through the base clases of T, updating
1186    CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1187    NO_CONST_ASN_REF_P.  Also set flag bits in T based on properties of
1188    the bases.  */
1189
1190 static void
1191 check_bases (tree t,
1192              int* cant_have_default_ctor_p,
1193              int* cant_have_const_ctor_p,
1194              int* no_const_asn_ref_p)
1195 {
1196   int n_baseclasses;
1197   int i;
1198   int seen_non_virtual_nearly_empty_base_p;
1199   tree binfos;
1200
1201   binfos = TYPE_BINFO_BASETYPES (t);
1202   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1203   seen_non_virtual_nearly_empty_base_p = 0;
1204
1205   /* An aggregate cannot have baseclasses.  */
1206   CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1207
1208   for (i = 0; i < n_baseclasses; ++i) 
1209     {
1210       tree base_binfo;
1211       tree basetype;
1212
1213       /* Figure out what base we're looking at.  */
1214       base_binfo = TREE_VEC_ELT (binfos, i);
1215       basetype = TREE_TYPE (base_binfo);
1216
1217       /* If the type of basetype is incomplete, then we already
1218          complained about that fact (and we should have fixed it up as
1219          well).  */
1220       if (!COMPLETE_TYPE_P (basetype))
1221         {
1222           int j;
1223           /* The base type is of incomplete type.  It is
1224              probably best to pretend that it does not
1225              exist.  */
1226           if (i == n_baseclasses-1)
1227             TREE_VEC_ELT (binfos, i) = NULL_TREE;
1228           TREE_VEC_LENGTH (binfos) -= 1;
1229           n_baseclasses -= 1;
1230           for (j = i; j+1 < n_baseclasses; j++)
1231             TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1232           continue;
1233         }
1234
1235       /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
1236          here because the case of virtual functions but non-virtual
1237          dtor is handled in finish_struct_1.  */
1238       if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1239           && TYPE_HAS_DESTRUCTOR (basetype))
1240         warning ("base class `%#T' has a non-virtual destructor",
1241                     basetype);
1242
1243       /* If the base class doesn't have copy constructors or
1244          assignment operators that take const references, then the
1245          derived class cannot have such a member automatically
1246          generated.  */
1247       if (! TYPE_HAS_CONST_INIT_REF (basetype))
1248         *cant_have_const_ctor_p = 1;
1249       if (TYPE_HAS_ASSIGN_REF (basetype)
1250           && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1251         *no_const_asn_ref_p = 1;
1252       /* Similarly, if the base class doesn't have a default
1253          constructor, then the derived class won't have an
1254          automatically generated default constructor.  */
1255       if (TYPE_HAS_CONSTRUCTOR (basetype)
1256           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1257         {
1258           *cant_have_default_ctor_p = 1;
1259           if (! TYPE_HAS_CONSTRUCTOR (t))
1260             pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1261                         basetype);
1262         }
1263
1264       if (TREE_VIA_VIRTUAL (base_binfo))
1265         /* A virtual base does not effect nearly emptiness.  */
1266         ;
1267       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1268         {
1269           if (seen_non_virtual_nearly_empty_base_p)
1270             /* And if there is more than one nearly empty base, then the
1271                derived class is not nearly empty either.  */
1272             CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1273           else
1274             /* Remember we've seen one.  */
1275             seen_non_virtual_nearly_empty_base_p = 1;
1276         }
1277       else if (!is_empty_class (basetype))
1278         /* If the base class is not empty or nearly empty, then this
1279            class cannot be nearly empty.  */
1280         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1281
1282       /* A lot of properties from the bases also apply to the derived
1283          class.  */
1284       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1285       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
1286         |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1287       TYPE_HAS_COMPLEX_ASSIGN_REF (t) 
1288         |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1289       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1290       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1291       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1292       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1293       TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1294       CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 
1295         |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
1296     }
1297 }
1298
1299 /* Binfo FROM is within a virtual hierarchy which is being reseated to
1300    TO. Move primary information from FROM to TO, and recursively traverse
1301    into FROM's bases. The hierarchy is dominated by TYPE.  MAPPINGS is an
1302    assoc list of binfos that have already been reseated.  */
1303
1304 static void
1305 force_canonical_binfo_r (tree to, tree from, tree type, tree mappings)
1306 {
1307   int i, n_baseclasses = BINFO_N_BASETYPES (from);
1308
1309   my_friendly_assert (to != from, 20010905);
1310   BINFO_INDIRECT_PRIMARY_P (to)
1311           = BINFO_INDIRECT_PRIMARY_P (from);
1312   BINFO_INDIRECT_PRIMARY_P (from) = 0;
1313   BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1314   BINFO_UNSHARED_MARKED (from) = 0;
1315   BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1316   BINFO_LOST_PRIMARY_P (from) = 0;
1317   if (BINFO_PRIMARY_P (from))
1318     {
1319       tree primary = BINFO_PRIMARY_BASE_OF (from);
1320       tree assoc;
1321       
1322       /* We might have just moved the primary base too, see if it's on our
1323          mappings.  */
1324       assoc = purpose_member (primary, mappings);
1325       if (assoc)
1326         primary = TREE_VALUE (assoc);
1327       BINFO_PRIMARY_BASE_OF (to) = primary;
1328       BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1329     }
1330   my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1331                       20010104);
1332   mappings = tree_cons (from, to, mappings);
1333
1334   if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1335       && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1336     {
1337       tree from_primary = get_primary_binfo (from);
1338       
1339       if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1340         force_canonical_binfo (get_primary_binfo (to), from_primary,
1341                                type, mappings);
1342     }
1343   
1344   for (i = 0; i != n_baseclasses; i++)
1345     {
1346       tree from_binfo = BINFO_BASETYPE (from, i);
1347       tree to_binfo = BINFO_BASETYPE (to, i);
1348
1349       if (TREE_VIA_VIRTUAL (from_binfo))
1350         {
1351           if (BINFO_PRIMARY_P (from_binfo) &&
1352               purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1353             /* This base is a primary of some binfo we have already
1354                reseated. We must reseat this one too.  */
1355             force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1356         }
1357       else
1358         force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1359     }
1360 }
1361
1362 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1363    make TO the canonical binfo, within the hierarchy dominated by TYPE.
1364    MAPPINGS is an assoc list of binfos that have already been reseated.
1365    Adjust any non-virtual bases within FROM, and also move any virtual bases
1366    which are canonical.  This complication arises because selecting primary
1367    bases walks in inheritance graph order, but we don't share binfos for
1368    virtual bases, hence we can fill in the primaries for a virtual base,
1369    and then discover that a later base requires the virtual as its
1370    primary.  */
1371
1372 static void
1373 force_canonical_binfo (tree to, tree from, tree type, tree mappings)
1374 {
1375   tree assoc = purpose_member (BINFO_TYPE (to),
1376                                CLASSTYPE_VBASECLASSES (type));
1377   if (TREE_VALUE (assoc) != to)
1378     {
1379       TREE_VALUE (assoc) = to;
1380       force_canonical_binfo_r (to, from, type, mappings);
1381     }
1382 }
1383
1384 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1385    dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1386    otherwise (because something else has already made it primary).  */
1387
1388 static tree
1389 mark_primary_virtual_base (tree base_binfo, tree type)
1390 {
1391   tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1392
1393   if (BINFO_PRIMARY_P (shared_binfo))
1394     {
1395       /* It's already allocated in the hierarchy. BINFO won't have a
1396          primary base in this hierarchy, even though the complete object
1397          BINFO is for, would do.  */
1398       return NULL_TREE;
1399     }
1400      
1401   /* We need to make sure that the assoc list
1402      CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1403      primary BINFO for the virtual base, as this is the one
1404      that'll really exist.  */
1405   if (base_binfo != shared_binfo)
1406     force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1407
1408   return base_binfo;
1409 }
1410
1411 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1412    base, then BINFO has no primary base in this graph.  Called from
1413    mark_primary_bases.  DATA is the most derived type.  */
1414
1415 static tree
1416 dfs_unshared_virtual_bases (tree binfo, void* data)
1417 {
1418   tree t = (tree) data;
1419   
1420   if (!BINFO_UNSHARED_MARKED (binfo)
1421       && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1422     {
1423       /* This morally virtual base has a primary base when it
1424          is a complete object. We need to locate the shared instance
1425          of this binfo in the type dominated by T. We duplicate the
1426          primary base information from there to here.  */
1427       tree vbase;
1428       tree unshared_base;
1429       
1430       for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1431            vbase = BINFO_INHERITANCE_CHAIN (vbase))
1432         continue;
1433       unshared_base = get_original_base (binfo,
1434                                          binfo_for_vbase (BINFO_TYPE (vbase),
1435                                                           t));
1436       my_friendly_assert (unshared_base != binfo, 20010612);
1437       BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1438       if (!BINFO_LOST_PRIMARY_P (binfo))
1439         BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1440     }
1441   
1442   if (binfo != TYPE_BINFO (t))
1443     /* The vtable fields will have been copied when duplicating the
1444        base binfos. That information is bogus, make sure we don't try
1445        and use it.  */
1446     BINFO_VTABLE (binfo) = NULL_TREE;
1447
1448   /* If this is a virtual primary base, make sure its offset matches
1449      that which it is primary for.  */
1450   if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1451       binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1452     {
1453       tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1454                                 BINFO_OFFSET (binfo));
1455       if (!integer_zerop (delta))
1456         propagate_binfo_offsets (binfo, delta, t);
1457     }
1458   
1459   BINFO_UNSHARED_MARKED (binfo) = 0;
1460   return NULL;
1461 }
1462
1463 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1464    dominated by TYPE that are primary bases.  */
1465
1466 static void
1467 mark_primary_bases (tree type)
1468 {
1469   tree binfo;
1470   
1471   /* Walk the bases in inheritance graph order.  */
1472   for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1473     {
1474       tree base_binfo;
1475       
1476       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1477         /* Not a dynamic base.  */
1478         continue;
1479
1480       base_binfo = get_primary_binfo (binfo);
1481
1482       if (TREE_VIA_VIRTUAL (base_binfo))
1483         base_binfo = mark_primary_virtual_base (base_binfo, type);
1484
1485       if (base_binfo)
1486         BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1487       else
1488         BINFO_LOST_PRIMARY_P (binfo) = 1;
1489       
1490       BINFO_UNSHARED_MARKED (binfo) = 1;
1491     }
1492   /* There could remain unshared morally virtual bases which were not
1493      visited in the inheritance graph walk. These bases will have lost
1494      their virtual primary base (should they have one). We must now
1495      find them. Also we must fix up the BINFO_OFFSETs of primary
1496      virtual bases. We could not do that as we went along, as they
1497      were originally copied from the bases we inherited from by
1498      unshare_base_binfos. That may have decided differently about
1499      where a virtual primary base went.  */
1500   dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1501 }
1502
1503 /* Make the BINFO the primary base of T.  */
1504
1505 static void
1506 set_primary_base (tree t, tree binfo)
1507 {
1508   tree basetype;
1509
1510   CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1511   basetype = BINFO_TYPE (binfo);
1512   TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1513   TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1514   TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1515 }
1516
1517 /* Determine the primary class for T.  */
1518
1519 static void
1520 determine_primary_base (tree t)
1521 {
1522   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1523   tree vbases;
1524   tree type_binfo;
1525
1526   /* If there are no baseclasses, there is certainly no primary base.  */
1527   if (n_baseclasses == 0)
1528     return;
1529
1530   type_binfo = TYPE_BINFO (t);
1531
1532   for (i = 0; i < n_baseclasses; i++)
1533     {
1534       tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1535       tree basetype = BINFO_TYPE (base_binfo);
1536
1537       if (TYPE_CONTAINS_VPTR_P (basetype))
1538         {
1539           /* We prefer a non-virtual base, although a virtual one will
1540              do.  */
1541           if (TREE_VIA_VIRTUAL (base_binfo))
1542             continue;
1543
1544           if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1545             {
1546               set_primary_base (t, base_binfo);
1547               CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1548             }
1549           else
1550             {
1551               tree vfields;
1552
1553               /* Only add unique vfields, and flatten them out as we go.  */
1554               for (vfields = CLASSTYPE_VFIELDS (basetype);
1555                    vfields;
1556                    vfields = TREE_CHAIN (vfields))
1557                 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1558                     || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1559                   CLASSTYPE_VFIELDS (t) 
1560                     = tree_cons (base_binfo, 
1561                                  VF_BASETYPE_VALUE (vfields),
1562                                  CLASSTYPE_VFIELDS (t));
1563             }
1564         }
1565     }
1566
1567   if (!TYPE_VFIELD (t))
1568     CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1569
1570   /* Find the indirect primary bases - those virtual bases which are primary
1571      bases of something else in this hierarchy.  */
1572   for (vbases = CLASSTYPE_VBASECLASSES (t);
1573        vbases;
1574        vbases = TREE_CHAIN (vbases)) 
1575     {
1576       tree vbase_binfo = TREE_VALUE (vbases);
1577
1578       /* See if this virtual base is an indirect primary base.  To be so,
1579          it must be a primary base within the hierarchy of one of our
1580          direct bases.  */
1581       for (i = 0; i < n_baseclasses; ++i) 
1582         {
1583           tree basetype = TYPE_BINFO_BASETYPE (t, i);
1584           tree v;
1585
1586           for (v = CLASSTYPE_VBASECLASSES (basetype); 
1587                v; 
1588                v = TREE_CHAIN (v))
1589             {
1590               tree base_vbase = TREE_VALUE (v);
1591               
1592               if (BINFO_PRIMARY_P (base_vbase)
1593                   && same_type_p (BINFO_TYPE (base_vbase),
1594                                   BINFO_TYPE (vbase_binfo)))
1595                 {
1596                   BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1597                   break;
1598                 }
1599             }
1600
1601           /* If we've discovered that this virtual base is an indirect
1602              primary base, then we can move on to the next virtual
1603              base.  */
1604           if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1605             break;
1606         }
1607     }
1608
1609   /* A "nearly-empty" virtual base class can be the primary base
1610      class, if no non-virtual polymorphic base can be found.  */
1611   if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1612     {
1613       /* If not NULL, this is the best primary base candidate we have
1614          found so far.  */
1615       tree candidate = NULL_TREE;
1616       tree base_binfo;
1617
1618       /* Loop over the baseclasses.  */
1619       for (base_binfo = TYPE_BINFO (t);
1620            base_binfo;
1621            base_binfo = TREE_CHAIN (base_binfo))
1622         {
1623           tree basetype = BINFO_TYPE (base_binfo);
1624
1625           if (TREE_VIA_VIRTUAL (base_binfo) 
1626               && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1627             {
1628               /* If this is not an indirect primary base, then it's
1629                  definitely our primary base.  */
1630               if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1631                 {
1632                   candidate = base_binfo;
1633                   break;
1634                 }
1635
1636               /* If this is an indirect primary base, it still could be
1637                  our primary base -- unless we later find there's another
1638                  nearly-empty virtual base that isn't an indirect
1639                  primary base.  */
1640               if (!candidate)
1641                 candidate = base_binfo;
1642             }
1643         }
1644
1645       /* If we've got a primary base, use it.  */
1646       if (candidate)
1647         {
1648           set_primary_base (t, candidate);
1649           CLASSTYPE_VFIELDS (t) 
1650             = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1651         }       
1652     }
1653
1654   /* Mark the primary base classes at this point.  */
1655   mark_primary_bases (t);
1656 }
1657 \f
1658 /* Set memoizing fields and bits of T (and its variants) for later
1659    use.  */
1660
1661 static void
1662 finish_struct_bits (tree t)
1663 {
1664   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1665
1666   /* Fix up variants (if any).  */
1667   tree variants = TYPE_NEXT_VARIANT (t);
1668   while (variants)
1669     {
1670       /* These fields are in the _TYPE part of the node, not in
1671          the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1672       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1673       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1674       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1675       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 
1676         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1677
1678       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants) 
1679         = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1680       TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1681       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1682       /* Copy whatever these are holding today.  */
1683       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1684       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1685       TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1686       TYPE_SIZE (variants) = TYPE_SIZE (t);
1687       TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1688       variants = TYPE_NEXT_VARIANT (variants);
1689     }
1690
1691   if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1692     /* For a class w/o baseclasses, `finish_struct' has set
1693        CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1694        definition). Similarly for a class whose base classes do not
1695        have vtables. When neither of these is true, we might have
1696        removed abstract virtuals (by providing a definition), added
1697        some (by declaring new ones), or redeclared ones from a base
1698        class. We need to recalculate what's really an abstract virtual
1699        at this point (by looking in the vtables).  */
1700       get_pure_virtuals (t);
1701
1702   if (n_baseclasses)
1703     {
1704       /* Notice whether this class has type conversion functions defined.  */
1705       tree binfo = TYPE_BINFO (t);
1706       tree binfos = BINFO_BASETYPES (binfo);
1707       tree basetype;
1708
1709       for (i = n_baseclasses-1; i >= 0; i--)
1710         {
1711           basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1712
1713           TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1714         }
1715     }
1716
1717   /* If this type has a copy constructor or a destructor, force its mode to
1718      be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero.  This
1719      will cause it to be passed by invisible reference and prevent it from
1720      being returned in a register.  */
1721   if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
1722     {
1723       tree variants;
1724       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1725       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1726         {
1727           TYPE_MODE (variants) = BLKmode;
1728           TREE_ADDRESSABLE (variants) = 1;
1729         }
1730     }
1731 }
1732
1733 /* Issue warnings about T having private constructors, but no friends,
1734    and so forth.  
1735
1736    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1737    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1738    non-private static member functions.  */
1739
1740 static void
1741 maybe_warn_about_overly_private_class (tree t)
1742 {
1743   int has_member_fn = 0;
1744   int has_nonprivate_method = 0;
1745   tree fn;
1746
1747   if (!warn_ctor_dtor_privacy
1748       /* If the class has friends, those entities might create and
1749          access instances, so we should not warn.  */
1750       || (CLASSTYPE_FRIEND_CLASSES (t)
1751           || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1752       /* We will have warned when the template was declared; there's
1753          no need to warn on every instantiation.  */
1754       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1755     /* There's no reason to even consider warning about this 
1756        class.  */
1757     return;
1758     
1759   /* We only issue one warning, if more than one applies, because
1760      otherwise, on code like:
1761
1762      class A {
1763        // Oops - forgot `public:'
1764        A();
1765        A(const A&);
1766        ~A();
1767      };
1768
1769      we warn several times about essentially the same problem.  */
1770
1771   /* Check to see if all (non-constructor, non-destructor) member
1772      functions are private.  (Since there are no friends or
1773      non-private statics, we can't ever call any of the private member
1774      functions.)  */
1775   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1776     /* We're not interested in compiler-generated methods; they don't
1777        provide any way to call private members.  */
1778     if (!DECL_ARTIFICIAL (fn)) 
1779       {
1780         if (!TREE_PRIVATE (fn))
1781           {
1782             if (DECL_STATIC_FUNCTION_P (fn)) 
1783               /* A non-private static member function is just like a
1784                  friend; it can create and invoke private member
1785                  functions, and be accessed without a class
1786                  instance.  */
1787               return;
1788                 
1789             has_nonprivate_method = 1;
1790             /* Keep searching for a static member function.  */
1791           }
1792         else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1793           has_member_fn = 1;
1794       } 
1795
1796   if (!has_nonprivate_method && has_member_fn) 
1797     {
1798       /* There are no non-private methods, and there's at least one
1799          private member function that isn't a constructor or
1800          destructor.  (If all the private members are
1801          constructors/destructors we want to use the code below that
1802          issues error messages specifically referring to
1803          constructors/destructors.)  */
1804       int i;
1805       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1806       for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1807         if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1808             || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1809           {
1810             has_nonprivate_method = 1;
1811             break;
1812           }
1813       if (!has_nonprivate_method) 
1814         {
1815           warning ("all member functions in class `%T' are private", t);
1816           return;
1817         }
1818     }
1819
1820   /* Even if some of the member functions are non-private, the class
1821      won't be useful for much if all the constructors or destructors
1822      are private: such an object can never be created or destroyed.  */
1823   if (TYPE_HAS_DESTRUCTOR (t))
1824     {
1825       tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
1826
1827       if (TREE_PRIVATE (dtor))
1828         {
1829           warning ("`%#T' only defines a private destructor and has no friends",
1830                       t);
1831           return;
1832         }
1833     }
1834
1835   if (TYPE_HAS_CONSTRUCTOR (t))
1836     {
1837       int nonprivate_ctor = 0;
1838           
1839       /* If a non-template class does not define a copy
1840          constructor, one is defined for it, enabling it to avoid
1841          this warning.  For a template class, this does not
1842          happen, and so we would normally get a warning on:
1843
1844            template <class T> class C { private: C(); };  
1845           
1846          To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
1847          complete non-template or fully instantiated classes have this
1848          flag set.  */
1849       if (!TYPE_HAS_INIT_REF (t))
1850         nonprivate_ctor = 1;
1851       else 
1852         for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
1853              fn;
1854              fn = OVL_NEXT (fn)) 
1855           {
1856             tree ctor = OVL_CURRENT (fn);
1857             /* Ideally, we wouldn't count copy constructors (or, in
1858                fact, any constructor that takes an argument of the
1859                class type as a parameter) because such things cannot
1860                be used to construct an instance of the class unless
1861                you already have one.  But, for now at least, we're
1862                more generous.  */
1863             if (! TREE_PRIVATE (ctor))
1864               {
1865                 nonprivate_ctor = 1;
1866                 break;
1867               }
1868           }
1869
1870       if (nonprivate_ctor == 0)
1871         {
1872           warning ("`%#T' only defines private constructors and has no friends",
1873                       t);
1874           return;
1875         }
1876     }
1877 }
1878
1879 /* Function to help qsort sort FIELD_DECLs by name order.  */
1880
1881 static int
1882 field_decl_cmp (const void* x_p, const void* y_p)
1883 {
1884   const tree *const x = x_p;
1885   const tree *const y = y_p;
1886   if (DECL_NAME (*x) == DECL_NAME (*y))
1887     /* A nontype is "greater" than a type.  */
1888     return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1889   if (DECL_NAME (*x) == NULL_TREE)
1890     return -1;
1891   if (DECL_NAME (*y) == NULL_TREE)
1892     return 1;
1893   if (DECL_NAME (*x) < DECL_NAME (*y))
1894     return -1;
1895   return 1;
1896 }
1897
1898 static struct {
1899   gt_pointer_operator new_value;
1900   void *cookie;
1901 } resort_data;
1902
1903 /* This routine compares two fields like field_decl_cmp but using the
1904    pointer operator in resort_data.  */
1905
1906 static int
1907 resort_field_decl_cmp (const void* x_p, const void* y_p)
1908 {
1909   const tree *const x = x_p;
1910   const tree *const y = y_p;
1911
1912   if (DECL_NAME (*x) == DECL_NAME (*y))
1913     /* A nontype is "greater" than a type.  */
1914     return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
1915   if (DECL_NAME (*x) == NULL_TREE)
1916     return -1;
1917   if (DECL_NAME (*y) == NULL_TREE)
1918     return 1;
1919   {
1920     tree d1 = DECL_NAME (*x);
1921     tree d2 = DECL_NAME (*y);
1922     resort_data.new_value (&d1, resort_data.cookie);
1923     resort_data.new_value (&d2, resort_data.cookie);
1924     if (d1 < d2)
1925       return -1;
1926   }
1927   return 1;
1928 }
1929
1930 /* Resort DECL_SORTED_FIELDS because pointers have been reordered.  */
1931
1932 void 
1933 resort_sorted_fields (void* obj, 
1934                       void* orig_obj ATTRIBUTE_UNUSED , 
1935                       gt_pointer_operator new_value, 
1936                       void* cookie)
1937 {
1938   tree sf = obj;
1939   resort_data.new_value = new_value;
1940   resort_data.cookie = cookie;
1941   qsort (&TREE_VEC_ELT (sf, 0), TREE_VEC_LENGTH (sf), sizeof (tree),
1942          resort_field_decl_cmp);
1943 }
1944
1945 /* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
1946
1947 static int
1948 method_name_cmp (const void* m1_p, const void* m2_p)
1949 {
1950   const tree *const m1 = m1_p;
1951   const tree *const m2 = m2_p;
1952   
1953   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1954     return 0;
1955   if (*m1 == NULL_TREE)
1956     return -1;
1957   if (*m2 == NULL_TREE)
1958     return 1;
1959   if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
1960     return -1;
1961   return 1;
1962 }
1963
1964 /* This routine compares two fields like method_name_cmp but using the
1965    pointer operator in resort_field_decl_data.  */
1966
1967 static int
1968 resort_method_name_cmp (const void* m1_p, const void* m2_p)
1969 {
1970   const tree *const m1 = m1_p;
1971   const tree *const m2 = m2_p;
1972   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
1973     return 0;
1974   if (*m1 == NULL_TREE)
1975     return -1;
1976   if (*m2 == NULL_TREE)
1977     return 1;
1978   {
1979     tree d1 = DECL_NAME (OVL_CURRENT (*m1));
1980     tree d2 = DECL_NAME (OVL_CURRENT (*m2));
1981     resort_data.new_value (&d1, resort_data.cookie);
1982     resort_data.new_value (&d2, resort_data.cookie);
1983     if (d1 < d2)
1984       return -1;
1985   }
1986   return 1;
1987 }
1988
1989 /* Resort TYPE_METHOD_VEC because pointers have been reordered.  */
1990
1991 void 
1992 resort_type_method_vec (void* obj,
1993                         void* orig_obj ATTRIBUTE_UNUSED ,
1994                         gt_pointer_operator new_value,
1995                         void* cookie)
1996 {
1997   tree method_vec = obj;
1998   int len = TREE_VEC_LENGTH (method_vec);
1999   int slot;
2000
2001   /* The type conversion ops have to live at the front of the vec, so we
2002      can't sort them.  */
2003   for (slot = 2; slot < len; ++slot)
2004     {
2005       tree fn = TREE_VEC_ELT (method_vec, slot);
2006   
2007       if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2008         break;
2009     }
2010   if (len - slot > 1)
2011     {
2012       resort_data.new_value = new_value;
2013       resort_data.cookie = cookie;
2014       qsort (&TREE_VEC_ELT (method_vec, slot), len - slot, sizeof (tree),
2015              resort_method_name_cmp);
2016     }
2017 }
2018
2019 /* Warn about duplicate methods in fn_fields.  Also compact method
2020    lists so that lookup can be made faster.
2021
2022    Data Structure: List of method lists.  The outer list is a
2023    TREE_LIST, whose TREE_PURPOSE field is the field name and the
2024    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
2025    links the entire list of methods for TYPE_METHODS.  Friends are
2026    chained in the same way as member functions (? TREE_CHAIN or
2027    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2028    list.  That allows them to be quickly deleted, and requires no
2029    extra storage.
2030
2031    Sort methods that are not special (i.e., constructors, destructors,
2032    and type conversion operators) so that we can find them faster in
2033    search.  */
2034
2035 static void
2036 finish_struct_methods (tree t)
2037 {
2038   tree fn_fields;
2039   tree method_vec;
2040   int slot, len;
2041
2042   if (!TYPE_METHODS (t))
2043     {
2044       /* Clear these for safety; perhaps some parsing error could set
2045          these incorrectly.  */
2046       TYPE_HAS_CONSTRUCTOR (t) = 0;
2047       TYPE_HAS_DESTRUCTOR (t) = 0;
2048       CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2049       return;
2050     }
2051
2052   method_vec = CLASSTYPE_METHOD_VEC (t);
2053   my_friendly_assert (method_vec != NULL_TREE, 19991215);
2054   len = TREE_VEC_LENGTH (method_vec);
2055
2056   /* First fill in entry 0 with the constructors, entry 1 with destructors,
2057      and the next few with type conversion operators (if any).  */
2058   for (fn_fields = TYPE_METHODS (t); fn_fields; 
2059        fn_fields = TREE_CHAIN (fn_fields))
2060     /* Clear out this flag.  */
2061     DECL_IN_AGGR_P (fn_fields) = 0;
2062
2063   if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2064     /* We thought there was a destructor, but there wasn't.  Some
2065        parse errors cause this anomalous situation.  */
2066     TYPE_HAS_DESTRUCTOR (t) = 0;
2067     
2068   /* Issue warnings about private constructors and such.  If there are
2069      no methods, then some public defaults are generated.  */
2070   maybe_warn_about_overly_private_class (t);
2071
2072   /* Now sort the methods.  */
2073   while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2074     len--;
2075   TREE_VEC_LENGTH (method_vec) = len;
2076
2077   /* The type conversion ops have to live at the front of the vec, so we
2078      can't sort them.  */
2079   for (slot = 2; slot < len; ++slot)
2080     {
2081       tree fn = TREE_VEC_ELT (method_vec, slot);
2082   
2083       if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2084         break;
2085     }
2086   if (len - slot > 1)
2087     qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2088            method_name_cmp);
2089 }
2090
2091 /* Emit error when a duplicate definition of a type is seen.  Patch up.  */
2092
2093 void
2094 duplicate_tag_error (tree t)
2095 {
2096   error ("redefinition of `%#T'", t);
2097   cp_error_at ("previous definition of `%#T'", t);
2098
2099   /* Pretend we haven't defined this type.  */
2100
2101   /* All of the component_decl's were TREE_CHAINed together in the parser.
2102      finish_struct_methods walks these chains and assembles all methods with
2103      the same base name into DECL_CHAINs. Now we don't need the parser chains
2104      anymore, so we unravel them.  */
2105
2106   /* This used to be in finish_struct, but it turns out that the
2107      TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2108      things...  */
2109   if (CLASSTYPE_METHOD_VEC (t)) 
2110     {
2111       tree method_vec = CLASSTYPE_METHOD_VEC (t);
2112       int i, len  = TREE_VEC_LENGTH (method_vec);
2113       for (i = 0; i < len; i++)
2114         {
2115           tree unchain = TREE_VEC_ELT (method_vec, i);
2116           while (unchain != NULL_TREE) 
2117             {
2118               TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2119               unchain = OVL_NEXT (unchain);
2120             }
2121         }
2122     }
2123
2124   if (TYPE_LANG_SPECIFIC (t))
2125     {
2126       tree binfo = TYPE_BINFO (t);
2127       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2128       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2129       tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2130       int use_template = CLASSTYPE_USE_TEMPLATE (t);
2131
2132       memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2133       BINFO_BASETYPES(binfo) = NULL_TREE;
2134
2135       TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
2136       TYPE_BINFO (t) = binfo;
2137       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2138       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2139       TYPE_REDEFINED (t) = 1;
2140       CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2141       CLASSTYPE_USE_TEMPLATE (t) = use_template;
2142       CLASSTYPE_DECL_LIST (t) = NULL_TREE;
2143     }
2144   TYPE_SIZE (t) = NULL_TREE;
2145   TYPE_MODE (t) = VOIDmode;
2146   TYPE_FIELDS (t) = NULL_TREE;
2147   TYPE_METHODS (t) = NULL_TREE;
2148   TYPE_VFIELD (t) = NULL_TREE;
2149   TYPE_CONTEXT (t) = NULL_TREE;
2150   
2151   /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above.  */
2152   TYPE_LANG_FLAG_0 (t) = 0;
2153   TYPE_LANG_FLAG_1 (t) = 0;
2154   TYPE_LANG_FLAG_2 (t) = 0;
2155   TYPE_LANG_FLAG_3 (t) = 0;
2156   TYPE_LANG_FLAG_4 (t) = 0;
2157   TYPE_LANG_FLAG_5 (t) = 0;
2158   TYPE_LANG_FLAG_6 (t) = 0;
2159   /* But not this one.  */
2160   SET_IS_AGGR_TYPE (t, 1);
2161 }
2162
2163 /* Make BINFO's vtable have N entries, including RTTI entries,
2164    vbase and vcall offsets, etc.  Set its type and call the backend
2165    to lay it out.  */
2166
2167 static void
2168 layout_vtable_decl (tree binfo, int n)
2169 {
2170   tree atype;
2171   tree vtable;
2172
2173   atype = build_cplus_array_type (vtable_entry_type, 
2174                                   build_index_type (size_int (n - 1)));
2175   layout_type (atype);
2176
2177   /* We may have to grow the vtable.  */
2178   vtable = get_vtbl_decl_for_binfo (binfo);
2179   if (!same_type_p (TREE_TYPE (vtable), atype))
2180     {
2181       TREE_TYPE (vtable) = atype;
2182       DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2183       layout_decl (vtable, 0);
2184
2185       /* At one time the vtable info was grabbed 2 words at a time.  This
2186          fails on SPARC unless you have 8-byte alignment.  */
2187       DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2188                                  DECL_ALIGN (vtable));
2189     }
2190 }
2191
2192 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2193    have the same signature.  */
2194
2195 int
2196 same_signature_p (tree fndecl, tree base_fndecl)
2197 {
2198   /* One destructor overrides another if they are the same kind of
2199      destructor.  */
2200   if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2201       && special_function_p (base_fndecl) == special_function_p (fndecl))
2202     return 1;
2203   /* But a non-destructor never overrides a destructor, nor vice
2204      versa, nor do different kinds of destructors override
2205      one-another.  For example, a complete object destructor does not
2206      override a deleting destructor.  */
2207   if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2208     return 0;
2209
2210   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2211     {
2212       tree types, base_types;
2213       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2214       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2215       if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2216            == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2217           && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2218         return 1;
2219     }
2220   return 0;
2221 }
2222
2223 /* Called from base_derived_from via dfs_walk.  */
2224
2225 static tree
2226 dfs_base_derived_from (tree binfo, void *data)
2227 {
2228   tree base = (tree) data;
2229
2230   if (same_type_p (TREE_TYPE (base), TREE_TYPE (binfo))
2231       && tree_int_cst_equal (BINFO_OFFSET (base), BINFO_OFFSET (binfo)))
2232     return error_mark_node;
2233
2234   return NULL_TREE;
2235 }
2236
2237 /* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
2238    subobject.  */
2239  
2240 static bool
2241 base_derived_from (tree derived, tree base)
2242 {
2243   return dfs_walk (derived, dfs_base_derived_from, NULL, base) != NULL_TREE;
2244 }
2245
2246 typedef struct find_final_overrider_data_s {
2247   /* The function for which we are trying to find a final overrider.  */
2248   tree fn;
2249   /* The base class in which the function was declared.  */
2250   tree declaring_base;
2251   /* The most derived class in the hierarchy.  */
2252   tree most_derived_type;
2253   /* The candidate overriders.  */
2254   tree candidates;
2255 } find_final_overrider_data;
2256
2257 /* Called from find_final_overrider via dfs_walk.  */
2258
2259 static tree
2260 dfs_find_final_overrider (tree binfo, void* data)
2261 {
2262   find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2263
2264   if (same_type_p (BINFO_TYPE (binfo), 
2265                    BINFO_TYPE (ffod->declaring_base))
2266       && tree_int_cst_equal (BINFO_OFFSET (binfo),
2267                              BINFO_OFFSET (ffod->declaring_base)))
2268     {
2269       tree path;
2270       tree method;
2271
2272       /* We haven't found an overrider yet.  */
2273       method = NULL_TREE;
2274       /* We've found a path to the declaring base.  Walk down the path
2275          looking for an overrider for FN.  */
2276       path = reverse_path (binfo);
2277       while (!same_type_p (BINFO_TYPE (TREE_VALUE (path)),
2278                            ffod->most_derived_type))
2279         path = TREE_CHAIN (path);
2280       while (path)
2281         {
2282           method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2283                                             ffod->fn);
2284           if (method)
2285             {
2286               path = TREE_VALUE (path);
2287               break;
2288             }
2289
2290           path = TREE_CHAIN (path);
2291         }
2292
2293       /* If we found an overrider, record the overriding function, and
2294          the base from which it came.  */
2295       if (path)
2296         {
2297           tree *candidate;
2298
2299           /* Remove any candidates overridden by this new function.  */
2300           candidate = &ffod->candidates;
2301           while (*candidate)
2302             {
2303               /* If *CANDIDATE overrides METHOD, then METHOD
2304                  cannot override anything else on the list.  */
2305               if (base_derived_from (TREE_VALUE (*candidate), path))
2306                   return NULL_TREE;
2307               /* If METHOD overrides *CANDIDATE, remove *CANDIDATE.  */
2308               if (base_derived_from (path, TREE_VALUE (*candidate)))
2309                 *candidate = TREE_CHAIN (*candidate);
2310               else
2311                 candidate = &TREE_CHAIN (*candidate);
2312             }
2313
2314           /* Add the new function.  */
2315           ffod->candidates = tree_cons (method, path, ffod->candidates);
2316         }
2317     }
2318
2319   return NULL_TREE;
2320 }
2321
2322 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2323    FN and whose TREE_VALUE is the binfo for the base where the
2324    overriding occurs.  BINFO (in the hierarchy dominated by the binfo
2325    DERIVED) is the base object in which FN is declared.  */
2326
2327 static tree
2328 find_final_overrider (tree derived, tree binfo, tree fn)
2329 {
2330   find_final_overrider_data ffod;
2331
2332   /* Getting this right is a little tricky.  This is valid:
2333
2334        struct S { virtual void f (); };
2335        struct T { virtual void f (); };
2336        struct U : public S, public T { };
2337
2338      even though calling `f' in `U' is ambiguous.  But, 
2339
2340        struct R { virtual void f(); };
2341        struct S : virtual public R { virtual void f (); };
2342        struct T : virtual public R { virtual void f (); };
2343        struct U : public S, public T { };
2344
2345      is not -- there's no way to decide whether to put `S::f' or
2346      `T::f' in the vtable for `R'.  
2347      
2348      The solution is to look at all paths to BINFO.  If we find
2349      different overriders along any two, then there is a problem.  */
2350   if (DECL_THUNK_P (fn))
2351     fn = THUNK_TARGET (fn);
2352   
2353   ffod.fn = fn;
2354   ffod.declaring_base = binfo;
2355   ffod.most_derived_type = BINFO_TYPE (derived);
2356   ffod.candidates = NULL_TREE;
2357
2358   dfs_walk (derived,
2359             dfs_find_final_overrider,
2360             NULL,
2361             &ffod);
2362
2363   /* If there was no winner, issue an error message.  */
2364   if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
2365     {
2366       error ("no unique final overrider for `%D' in `%T'", fn, 
2367              BINFO_TYPE (derived));
2368       return error_mark_node;
2369     }
2370
2371   return ffod.candidates;
2372 }
2373
2374 /* Return the index of the vcall offset for FN when TYPE is used as a
2375    virtual base.  */
2376
2377 static tree
2378 get_vcall_index (tree fn, tree type)
2379 {
2380   tree v;
2381
2382   for (v = CLASSTYPE_VCALL_INDICES (type); v; v = TREE_CHAIN (v))
2383     if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (TREE_PURPOSE (v)))
2384         || same_signature_p (fn, TREE_PURPOSE (v)))
2385       break;
2386
2387   /* There should always be an appropriate index.  */
2388   my_friendly_assert (v, 20021103);
2389
2390   return TREE_VALUE (v);
2391 }
2392
2393 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2394    dominated by T.  FN has been overridden in BINFO; VIRTUALS points to the
2395    corresponding position in the BINFO_VIRTUALS list.  */
2396
2397 static void
2398 update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals)
2399 {
2400   tree b;
2401   tree overrider;
2402   tree delta;
2403   tree virtual_base;
2404   tree first_defn;
2405   tree overrider_fn, overrider_target;
2406   tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
2407   tree over_return, base_return;
2408   bool lost = false;
2409
2410   /* Find the nearest primary base (possibly binfo itself) which defines
2411      this function; this is the class the caller will convert to when
2412      calling FN through BINFO.  */
2413   for (b = binfo; ; b = get_primary_binfo (b))
2414     {
2415       my_friendly_assert (b, 20021227);
2416       if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
2417         break;
2418
2419       /* The nearest definition is from a lost primary.  */
2420       if (BINFO_LOST_PRIMARY_P (b))
2421         lost = true;
2422     }
2423   first_defn = b;
2424
2425   /* Find the final overrider.  */
2426   overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
2427   if (overrider == error_mark_node)
2428     return;
2429   overrider_target = overrider_fn = TREE_PURPOSE (overrider);
2430   
2431   /* Check for adjusting covariant return types. */
2432   over_return = TREE_TYPE (TREE_TYPE (overrider_target));
2433   base_return = TREE_TYPE (TREE_TYPE (target_fn));
2434   
2435   if (POINTER_TYPE_P (over_return)
2436       && TREE_CODE (over_return) == TREE_CODE (base_return)
2437       && CLASS_TYPE_P (TREE_TYPE (over_return))
2438       && CLASS_TYPE_P (TREE_TYPE (base_return)))
2439     {
2440       /* If FN is a covariant thunk, we must figure out the adjustment
2441          to the final base FN was converting to. As OVERRIDER_TARGET might
2442          also be converting to the return type of FN, we have to
2443          combine the two conversions here.  */
2444       tree fixed_offset, virtual_offset;
2445       
2446       if (DECL_THUNK_P (fn))
2447         {
2448           fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
2449           virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
2450           if (virtual_offset)
2451             virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
2452                                               TREE_TYPE (over_return));
2453         }
2454       else
2455         fixed_offset = virtual_offset = NULL_TREE;
2456
2457       if (!virtual_offset)
2458         {
2459           /* There was no existing virtual thunk (which takes
2460              precidence). */
2461           tree thunk_binfo;
2462           base_kind kind;
2463           
2464           thunk_binfo = lookup_base (TREE_TYPE (over_return),
2465                                      TREE_TYPE (base_return),
2466                                      ba_check | ba_quiet, &kind);
2467
2468           if (thunk_binfo && (kind == bk_via_virtual
2469                               || !BINFO_OFFSET_ZEROP (thunk_binfo)))
2470             {
2471               tree offset = BINFO_OFFSET (thunk_binfo);
2472
2473               if (kind == bk_via_virtual)
2474                 {
2475                   /* We convert via virtual base. Find the virtual
2476                      base and adjust the fixed offset to be from there.  */
2477                   while (!TREE_VIA_VIRTUAL (thunk_binfo))
2478                     thunk_binfo = BINFO_INHERITANCE_CHAIN (thunk_binfo);
2479               
2480                   virtual_offset = binfo_for_vbase (BINFO_TYPE (thunk_binfo),
2481                                                     TREE_TYPE (over_return));
2482                   offset = size_diffop (offset,
2483                                         BINFO_OFFSET (virtual_offset));
2484                 }
2485               if (fixed_offset)
2486                 /* There was an existing fixed offset, this must be
2487                    from the base just converted to, and the base the
2488                    FN was thunking to.  */
2489                 fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
2490               else
2491                 fixed_offset = offset;
2492             }
2493         }
2494       
2495       if (fixed_offset || virtual_offset)
2496         /* Replace the overriding function with a covariant thunk.  We
2497            will emit the overriding function in its own slot as
2498            well. */
2499         overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
2500                                    fixed_offset, virtual_offset);
2501     }
2502   else
2503     my_friendly_assert (!DECL_THUNK_P (fn), 20021231);
2504   
2505   /* Assume that we will produce a thunk that convert all the way to
2506      the final overrider, and not to an intermediate virtual base.  */
2507   virtual_base = NULL_TREE;
2508
2509   /* See if we can convert to an intermediate virtual base first, and then
2510      use the vcall offset located there to finish the conversion.  */
2511   for (; b; b = BINFO_INHERITANCE_CHAIN (b))
2512     {
2513       /* If we find the final overrider, then we can stop
2514          walking.  */
2515       if (same_type_p (BINFO_TYPE (b), 
2516                        BINFO_TYPE (TREE_VALUE (overrider))))
2517         break;
2518
2519       /* If we find a virtual base, and we haven't yet found the
2520          overrider, then there is a virtual base between the
2521          declaring base (first_defn) and the final overrider.  */
2522       if (!virtual_base && TREE_VIA_VIRTUAL (b))
2523         virtual_base = b;
2524     }
2525
2526   /* Compute the constant adjustment to the `this' pointer.  The
2527      `this' pointer, when this function is called, will point at BINFO
2528      (or one of its primary bases, which are at the same offset).  */
2529   if (virtual_base)
2530     /* The `this' pointer needs to be adjusted from the declaration to
2531        the nearest virtual base.  */
2532     delta = size_diffop (BINFO_OFFSET (virtual_base),
2533                          BINFO_OFFSET (first_defn));
2534   else if (lost)
2535     /* If the nearest definition is in a lost primary, we don't need an
2536        entry in our vtable.  Except possibly in a constructor vtable,
2537        if we happen to get our primary back.  In that case, the offset
2538        will be zero, as it will be a primary base.  */
2539     delta = size_zero_node;
2540   else
2541     /* The `this' pointer needs to be adjusted from pointing to
2542        BINFO to pointing at the base where the final overrider
2543        appears.  */
2544     delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2545                          BINFO_OFFSET (binfo));
2546
2547   modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
2548
2549   if (virtual_base)
2550     BV_VCALL_INDEX (*virtuals) 
2551       = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
2552 }
2553
2554 /* Called from modify_all_vtables via dfs_walk.  */
2555
2556 static tree
2557 dfs_modify_vtables (tree binfo, void* data)
2558 {
2559   if (/* There's no need to modify the vtable for a non-virtual
2560          primary base; we're not going to use that vtable anyhow.
2561          We do still need to do this for virtual primary bases, as they
2562          could become non-primary in a construction vtable.  */
2563       (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2564       /* Similarly, a base without a vtable needs no modification.  */
2565       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2566     {
2567       tree t;
2568       tree virtuals;
2569       tree old_virtuals;
2570
2571       t = (tree) data;
2572
2573       make_new_vtable (t, binfo);
2574       
2575       /* Now, go through each of the virtual functions in the virtual
2576          function table for BINFO.  Find the final overrider, and
2577          update the BINFO_VIRTUALS list appropriately.  */
2578       for (virtuals = BINFO_VIRTUALS (binfo),
2579              old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2580            virtuals;
2581            virtuals = TREE_CHAIN (virtuals),
2582              old_virtuals = TREE_CHAIN (old_virtuals))
2583         update_vtable_entry_for_fn (t, 
2584                                     binfo, 
2585                                     BV_FN (old_virtuals),
2586                                     &virtuals);
2587     }
2588
2589   SET_BINFO_MARKED (binfo);
2590
2591   return NULL_TREE;
2592 }
2593
2594 /* Update all of the primary and secondary vtables for T.  Create new
2595    vtables as required, and initialize their RTTI information.  Each
2596    of the functions in VIRTUALS is declared in T and may override a
2597    virtual function from a base class; find and modify the appropriate
2598    entries to point to the overriding functions.  Returns a list, in
2599    declaration order, of the virtual functions that are declared in T,
2600    but do not appear in the primary base class vtable, and which
2601    should therefore be appended to the end of the vtable for T.  */
2602
2603 static tree
2604 modify_all_vtables (tree t, tree virtuals)
2605 {
2606   tree binfo = TYPE_BINFO (t);
2607   tree *fnsp;
2608
2609   /* Update all of the vtables.  */
2610   dfs_walk (binfo, 
2611             dfs_modify_vtables, 
2612             dfs_unmarked_real_bases_queue_p,
2613             t);
2614   dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2615
2616   /* Add virtual functions not already in our primary vtable. These
2617      will be both those introduced by this class, and those overridden
2618      from secondary bases.  It does not include virtuals merely
2619      inherited from secondary bases.  */
2620   for (fnsp = &virtuals; *fnsp; )
2621     {
2622       tree fn = TREE_VALUE (*fnsp);
2623
2624       if (!value_member (fn, BINFO_VIRTUALS (binfo))
2625           || DECL_VINDEX (fn) == error_mark_node)
2626         {
2627           /* We don't need to adjust the `this' pointer when
2628              calling this function.  */
2629           BV_DELTA (*fnsp) = integer_zero_node;
2630           BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2631
2632           /* This is a function not already in our vtable.  Keep it.  */
2633           fnsp = &TREE_CHAIN (*fnsp);
2634         }
2635       else
2636         /* We've already got an entry for this function.  Skip it.  */
2637         *fnsp = TREE_CHAIN (*fnsp);
2638     }
2639
2640   return virtuals;
2641 }
2642
2643 /* Get the base virtual function declarations in T that have the
2644    indicated NAME.  */
2645
2646 static tree
2647 get_basefndecls (tree name, tree t)
2648 {
2649   tree methods;
2650   tree base_fndecls = NULL_TREE;
2651   int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2652   int i;
2653
2654   for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
2655     if (TREE_CODE (methods) == FUNCTION_DECL
2656         && DECL_VINDEX (methods) != NULL_TREE
2657         && DECL_NAME (methods) == name)
2658       base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
2659
2660   if (base_fndecls)
2661     return base_fndecls;
2662
2663   for (i = 0; i < n_baseclasses; i++)
2664     {
2665       tree basetype = TYPE_BINFO_BASETYPE (t, i);
2666       base_fndecls = chainon (get_basefndecls (name, basetype),
2667                               base_fndecls);
2668     }
2669
2670   return base_fndecls;
2671 }
2672
2673 /* If this declaration supersedes the declaration of
2674    a method declared virtual in the base class, then
2675    mark this field as being virtual as well.  */
2676
2677 static void
2678 check_for_override (tree decl, tree ctype)
2679 {
2680   if (TREE_CODE (decl) == TEMPLATE_DECL)
2681     /* In [temp.mem] we have:
2682
2683          A specialization of a member function template does not
2684          override a virtual function from a base class.  */
2685     return;
2686   if ((DECL_DESTRUCTOR_P (decl)
2687        || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2688       && look_for_overrides (ctype, decl)
2689       && !DECL_STATIC_FUNCTION_P (decl))
2690     /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
2691        the error_mark_node so that we know it is an overriding
2692        function.  */
2693     DECL_VINDEX (decl) = decl;
2694
2695   if (DECL_VIRTUAL_P (decl))
2696     {
2697       if (!DECL_VINDEX (decl))
2698         DECL_VINDEX (decl) = error_mark_node;
2699       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2700     }
2701 }
2702
2703 /* Warn about hidden virtual functions that are not overridden in t.
2704    We know that constructors and destructors don't apply.  */
2705
2706 void
2707 warn_hidden (tree t)
2708 {
2709   tree method_vec = CLASSTYPE_METHOD_VEC (t);
2710   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2711   int i;
2712
2713   /* We go through each separately named virtual function.  */
2714   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2715     {
2716       tree fns;
2717       tree name;
2718       tree fndecl;
2719       tree base_fndecls;
2720       int j;
2721
2722       /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
2723          have the same name.  Figure out what name that is.  */
2724       name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2725       /* There are no possibly hidden functions yet.  */
2726       base_fndecls = NULL_TREE;
2727       /* Iterate through all of the base classes looking for possibly
2728          hidden functions.  */
2729       for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
2730         {
2731           tree basetype = TYPE_BINFO_BASETYPE (t, j);
2732           base_fndecls = chainon (get_basefndecls (name, basetype),
2733                                   base_fndecls);
2734         }
2735
2736       /* If there are no functions to hide, continue.  */
2737       if (!base_fndecls)
2738         continue;
2739
2740       /* Remove any overridden functions.  */
2741       for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
2742         {
2743           fndecl = OVL_CURRENT (fns);
2744           if (DECL_VINDEX (fndecl))
2745             {
2746               tree *prev = &base_fndecls;
2747               
2748               while (*prev) 
2749                 /* If the method from the base class has the same
2750                    signature as the method from the derived class, it
2751                    has been overridden.  */
2752                 if (same_signature_p (fndecl, TREE_VALUE (*prev)))
2753                   *prev = TREE_CHAIN (*prev);
2754                 else
2755                   prev = &TREE_CHAIN (*prev);
2756             }
2757         }
2758
2759       /* Now give a warning for all base functions without overriders,
2760          as they are hidden.  */
2761       while (base_fndecls) 
2762         {
2763           /* Here we know it is a hider, and no overrider exists.  */
2764           cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2765           cp_warning_at ("  by `%D'", 
2766                          OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
2767           base_fndecls = TREE_CHAIN (base_fndecls);
2768         }
2769     }
2770 }
2771
2772 /* Check for things that are invalid.  There are probably plenty of other
2773    things we should check for also.  */
2774
2775 static void
2776 finish_struct_anon (tree t)
2777 {
2778   tree field;
2779
2780   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2781     {
2782       if (TREE_STATIC (field))
2783         continue;
2784       if (TREE_CODE (field) != FIELD_DECL)
2785         continue;
2786
2787       if (DECL_NAME (field) == NULL_TREE
2788           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2789         {
2790           tree elt = TYPE_FIELDS (TREE_TYPE (field));
2791           for (; elt; elt = TREE_CHAIN (elt))
2792             {
2793               /* We're generally only interested in entities the user
2794                  declared, but we also find nested classes by noticing
2795                  the TYPE_DECL that we create implicitly.  You're
2796                  allowed to put one anonymous union inside another,
2797                  though, so we explicitly tolerate that.  We use
2798                  TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2799                  we also allow unnamed types used for defining fields.  */
2800               if (DECL_ARTIFICIAL (elt) 
2801                   && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2802                       || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2803                 continue;
2804
2805               if (constructor_name_p (DECL_NAME (elt), t))
2806                 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2807                                elt);
2808
2809               if (TREE_CODE (elt) != FIELD_DECL)
2810                 {
2811                   cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2812                                  elt);
2813                   continue;
2814                 }
2815
2816               if (TREE_PRIVATE (elt))
2817                 cp_pedwarn_at ("private member `%#D' in anonymous union",
2818                                elt);
2819               else if (TREE_PROTECTED (elt))
2820                 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2821                                elt);
2822
2823               TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2824               TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2825             }
2826         }
2827     }
2828 }
2829
2830 /* Add T to CLASSTYPE_DECL_LIST of current_class_type which
2831    will be used later during class template instantiation.
2832    When FRIEND_P is zero, T can be a static member data (VAR_DECL),
2833    a non-static member data (FIELD_DECL), a member function
2834    (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE), 
2835    a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
2836    When FRIEND_P is nonzero, T is either a friend class
2837    (RECORD_TYPE, TEMPLATE_DECL) or a friend function
2838    (FUNCTION_DECL, TEMPLATE_DECL).  */
2839
2840 void
2841 maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
2842 {
2843   /* Save some memory by not creating TREE_LIST if TYPE is not template.  */
2844   if (CLASSTYPE_TEMPLATE_INFO (type))
2845     CLASSTYPE_DECL_LIST (type)
2846       = tree_cons (friend_p ? NULL_TREE : type,
2847                    t, CLASSTYPE_DECL_LIST (type));
2848 }
2849
2850 /* Create default constructors, assignment operators, and so forth for
2851    the type indicated by T, if they are needed.
2852    CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2853    CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2854    class cannot have a default constructor, copy constructor taking a
2855    const reference argument, or an assignment operator taking a const
2856    reference, respectively.  If a virtual destructor is created, its
2857    DECL is returned; otherwise the return value is NULL_TREE.  */
2858
2859 static void
2860 add_implicitly_declared_members (tree t, 
2861                                  int cant_have_default_ctor,
2862                                  int cant_have_const_cctor,
2863                                  int cant_have_const_assignment)
2864 {
2865   tree default_fn;
2866   tree implicit_fns = NULL_TREE;
2867   tree virtual_dtor = NULL_TREE;
2868   tree *f;
2869
2870   ++adding_implicit_members;
2871
2872   /* Destructor.  */
2873   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2874     {
2875       default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2876       check_for_override (default_fn, t);
2877
2878       /* If we couldn't make it work, then pretend we didn't need it.  */
2879       if (default_fn == void_type_node)
2880         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
2881       else
2882         {
2883           TREE_CHAIN (default_fn) = implicit_fns;
2884           implicit_fns = default_fn;
2885
2886           if (DECL_VINDEX (default_fn))
2887             virtual_dtor = default_fn;
2888         }
2889     }
2890   else
2891     /* Any non-implicit destructor is non-trivial.  */
2892     TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
2893
2894   /* Default constructor.  */
2895   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
2896     {
2897       default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
2898       TREE_CHAIN (default_fn) = implicit_fns;
2899       implicit_fns = default_fn;
2900     }
2901
2902   /* Copy constructor.  */
2903   if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
2904     {
2905       /* ARM 12.18: You get either X(X&) or X(const X&), but
2906          not both.  --Chip  */
2907       default_fn 
2908         = implicitly_declare_fn (sfk_copy_constructor, t,
2909                                  /*const_p=*/!cant_have_const_cctor);
2910       TREE_CHAIN (default_fn) = implicit_fns;
2911       implicit_fns = default_fn;
2912     }
2913
2914   /* Assignment operator.  */
2915   if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
2916     {
2917       default_fn 
2918         = implicitly_declare_fn (sfk_assignment_operator, t,
2919                                  /*const_p=*/!cant_have_const_assignment);
2920       TREE_CHAIN (default_fn) = implicit_fns;
2921       implicit_fns = default_fn;
2922     }
2923
2924   /* Now, hook all of the new functions on to TYPE_METHODS,
2925      and add them to the CLASSTYPE_METHOD_VEC.  */
2926   for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
2927     {
2928       add_method (t, *f, /*error_p=*/0);
2929       maybe_add_class_template_decl_list (current_class_type, *f, /*friend_p=*/0);
2930     }
2931   if (abi_version_at_least (2))
2932     /* G++ 3.2 put the implicit destructor at the *beginning* of the
2933        list, which cause the destructor to be emitted in an incorrect
2934        location in the vtable.  */
2935     TYPE_METHODS (t) = chainon (TYPE_METHODS (t), implicit_fns);
2936   else
2937     {
2938       if (warn_abi && virtual_dtor)
2939         warning ("vtable layout for class `%T' may not be ABI-compliant "
2940                  "and may change in a future version of GCC due to implicit "
2941                  "virtual destructor",
2942                  t);
2943       *f = TYPE_METHODS (t);
2944       TYPE_METHODS (t) = implicit_fns;
2945     }
2946
2947   --adding_implicit_members;
2948 }
2949
2950 /* Subroutine of finish_struct_1.  Recursively count the number of fields
2951    in TYPE, including anonymous union members.  */
2952
2953 static int
2954 count_fields (tree fields)
2955 {
2956   tree x;
2957   int n_fields = 0;
2958   for (x = fields; x; x = TREE_CHAIN (x))
2959     {
2960       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2961         n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
2962       else
2963         n_fields += 1;
2964     }
2965   return n_fields;
2966 }
2967
2968 /* Subroutine of finish_struct_1.  Recursively add all the fields in the
2969    TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX.  */
2970
2971 static int
2972 add_fields_to_vec (tree fields, tree field_vec, int idx)
2973 {
2974   tree x;
2975   for (x = fields; x; x = TREE_CHAIN (x))
2976     {
2977       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
2978         idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
2979       else
2980         TREE_VEC_ELT (field_vec, idx++) = x;
2981     }
2982   return idx;
2983 }
2984
2985 /* FIELD is a bit-field.  We are finishing the processing for its
2986    enclosing type.  Issue any appropriate messages and set appropriate
2987    flags.  */
2988
2989 static void
2990 check_bitfield_decl (tree field)
2991 {
2992   tree type = TREE_TYPE (field);
2993   tree w = NULL_TREE;
2994
2995   /* Detect invalid bit-field type.  */
2996   if (DECL_INITIAL (field)
2997       && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
2998     {
2999       cp_error_at ("bit-field `%#D' with non-integral type", field);
3000       w = error_mark_node;
3001     }
3002
3003   /* Detect and ignore out of range field width.  */
3004   if (DECL_INITIAL (field))
3005     {
3006       w = DECL_INITIAL (field);
3007
3008       /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
3009       STRIP_NOPS (w);
3010
3011       /* detect invalid field size.  */
3012       if (TREE_CODE (w) == CONST_DECL)
3013         w = DECL_INITIAL (w);
3014       else
3015         w = decl_constant_value (w);
3016
3017       if (TREE_CODE (w) != INTEGER_CST)
3018         {
3019           cp_error_at ("bit-field `%D' width not an integer constant",
3020                        field);
3021           w = error_mark_node;
3022         }
3023       else if (tree_int_cst_sgn (w) < 0)
3024         {
3025           cp_error_at ("negative width in bit-field `%D'", field);
3026           w = error_mark_node;
3027         }
3028       else if (integer_zerop (w) && DECL_NAME (field) != 0)
3029         {
3030           cp_error_at ("zero width for bit-field `%D'", field);
3031           w = error_mark_node;
3032         }
3033       else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3034                && TREE_CODE (type) != ENUMERAL_TYPE
3035                && TREE_CODE (type) != BOOLEAN_TYPE)
3036         cp_warning_at ("width of `%D' exceeds its type", field);
3037       else if (TREE_CODE (type) == ENUMERAL_TYPE
3038                && (0 > compare_tree_int (w,
3039                                          min_precision (TYPE_MIN_VALUE (type),
3040                                                         TREE_UNSIGNED (type)))
3041                    ||  0 > compare_tree_int (w,
3042                                              min_precision
3043                                              (TYPE_MAX_VALUE (type),
3044                                               TREE_UNSIGNED (type)))))
3045         cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3046                        field, type);
3047     }
3048   
3049   /* Remove the bit-field width indicator so that the rest of the
3050      compiler does not treat that value as an initializer.  */
3051   DECL_INITIAL (field) = NULL_TREE;
3052
3053   if (w != error_mark_node)
3054     {
3055       DECL_SIZE (field) = convert (bitsizetype, w);
3056       DECL_BIT_FIELD (field) = 1;
3057
3058       if (integer_zerop (w)
3059           && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
3060         {
3061 #ifdef EMPTY_FIELD_BOUNDARY
3062           DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3063                                     EMPTY_FIELD_BOUNDARY);
3064 #endif
3065 #ifdef PCC_BITFIELD_TYPE_MATTERS
3066           if (PCC_BITFIELD_TYPE_MATTERS)
3067             {
3068               DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3069                                         TYPE_ALIGN (type));
3070               DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3071             }
3072 #endif
3073         }
3074     }
3075   else
3076     {
3077       /* Non-bit-fields are aligned for their type.  */
3078       DECL_BIT_FIELD (field) = 0;
3079       CLEAR_DECL_C_BIT_FIELD (field);
3080       DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3081       DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3082     }
3083 }
3084
3085 /* FIELD is a non bit-field.  We are finishing the processing for its
3086    enclosing type T.  Issue any appropriate messages and set appropriate
3087    flags.  */
3088
3089 static void
3090 check_field_decl (tree field,
3091                   tree t,
3092                   int* cant_have_const_ctor,
3093                   int* cant_have_default_ctor,
3094                   int* no_const_asn_ref,
3095                   int* any_default_members)
3096 {
3097   tree type = strip_array_types (TREE_TYPE (field));
3098
3099   /* An anonymous union cannot contain any fields which would change
3100      the settings of CANT_HAVE_CONST_CTOR and friends.  */
3101   if (ANON_UNION_TYPE_P (type))
3102     ;
3103   /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3104      structs.  So, we recurse through their fields here.  */
3105   else if (ANON_AGGR_TYPE_P (type))
3106     {
3107       tree fields;
3108
3109       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3110         if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3111           check_field_decl (fields, t, cant_have_const_ctor,
3112                             cant_have_default_ctor, no_const_asn_ref,
3113                             any_default_members);
3114     }
3115   /* Check members with class type for constructors, destructors,
3116      etc.  */
3117   else if (CLASS_TYPE_P (type))
3118     {
3119       /* Never let anything with uninheritable virtuals
3120          make it through without complaint.  */
3121       abstract_virtuals_error (field, type);
3122                       
3123       if (TREE_CODE (t) == UNION_TYPE)
3124         {
3125           if (TYPE_NEEDS_CONSTRUCTING (type))
3126             cp_error_at ("member `%#D' with constructor not allowed in union",
3127                          field);
3128           if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3129             cp_error_at ("member `%#D' with destructor not allowed in union",
3130                          field);
3131           if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3132             cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3133                          field);
3134         }
3135       else
3136         {
3137           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3138           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
3139             |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3140           TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3141           TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3142         }
3143
3144       if (!TYPE_HAS_CONST_INIT_REF (type))
3145         *cant_have_const_ctor = 1;
3146
3147       if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3148         *no_const_asn_ref = 1;
3149
3150       if (TYPE_HAS_CONSTRUCTOR (type)
3151           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3152         *cant_have_default_ctor = 1;
3153     }
3154   if (DECL_INITIAL (field) != NULL_TREE)
3155     {
3156       /* `build_class_init_list' does not recognize
3157          non-FIELD_DECLs.  */
3158       if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3159         cp_error_at ("multiple fields in union `%T' initialized");
3160       *any_default_members = 1;
3161     }
3162
3163   /* Non-bit-fields are aligned for their type, except packed fields
3164      which require only BITS_PER_UNIT alignment.  */
3165   DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3166                             (DECL_PACKED (field) 
3167                              ? BITS_PER_UNIT
3168                              : TYPE_ALIGN (TREE_TYPE (field))));
3169   if (! DECL_PACKED (field))
3170     DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3171 }
3172
3173 /* Check the data members (both static and non-static), class-scoped
3174    typedefs, etc., appearing in the declaration of T.  Issue
3175    appropriate diagnostics.  Sets ACCESS_DECLS to a list (in
3176    declaration order) of access declarations; each TREE_VALUE in this
3177    list is a USING_DECL.
3178
3179    In addition, set the following flags:
3180
3181      EMPTY_P
3182        The class is empty, i.e., contains no non-static data members.
3183
3184      CANT_HAVE_DEFAULT_CTOR_P
3185        This class cannot have an implicitly generated default
3186        constructor.
3187
3188      CANT_HAVE_CONST_CTOR_P
3189        This class cannot have an implicitly generated copy constructor
3190        taking a const reference.
3191
3192      CANT_HAVE_CONST_ASN_REF
3193        This class cannot have an implicitly generated assignment
3194        operator taking a const reference.
3195
3196    All of these flags should be initialized before calling this
3197    function.
3198
3199    Returns a pointer to the end of the TYPE_FIELDs chain; additional
3200    fields can be added by adding to this chain.  */
3201
3202 static void
3203 check_field_decls (tree t, tree *access_decls,
3204                    int *cant_have_default_ctor_p, 
3205                    int *cant_have_const_ctor_p,
3206                    int *no_const_asn_ref_p)
3207 {
3208   tree *field;
3209   tree *next;
3210   int has_pointers;
3211   int any_default_members;
3212
3213   /* First, delete any duplicate fields.  */
3214   delete_duplicate_fields (TYPE_FIELDS (t));
3215
3216   /* Assume there are no access declarations.  */
3217   *access_decls = NULL_TREE;
3218   /* Assume this class has no pointer members.  */
3219   has_pointers = 0;
3220   /* Assume none of the members of this class have default
3221      initializations.  */
3222   any_default_members = 0;
3223
3224   for (field = &TYPE_FIELDS (t); *field; field = next)
3225     {
3226       tree x = *field;
3227       tree type = TREE_TYPE (x);
3228
3229       next = &TREE_CHAIN (x);
3230
3231       if (TREE_CODE (x) == FIELD_DECL)
3232         {
3233           DECL_PACKED (x) |= TYPE_PACKED (t);
3234
3235           if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3236             /* We don't treat zero-width bitfields as making a class
3237                non-empty.  */
3238             ;
3239           else
3240             {
3241               tree element_type;
3242
3243               /* The class is non-empty.  */
3244               CLASSTYPE_EMPTY_P (t) = 0;
3245               /* The class is not even nearly empty.  */
3246               CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3247               /* If one of the data members contains an empty class,
3248                  so does T.  */
3249               element_type = strip_array_types (type);
3250               if (CLASS_TYPE_P (element_type) 
3251                   && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3252                 CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
3253             }
3254         }
3255
3256       if (TREE_CODE (x) == USING_DECL)
3257         {
3258           /* Prune the access declaration from the list of fields.  */
3259           *field = TREE_CHAIN (x);
3260
3261           /* Save the access declarations for our caller.  */
3262           *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3263
3264           /* Since we've reset *FIELD there's no reason to skip to the
3265              next field.  */
3266           next = field;
3267           continue;
3268         }
3269
3270       if (TREE_CODE (x) == TYPE_DECL
3271           || TREE_CODE (x) == TEMPLATE_DECL)
3272         continue;
3273
3274       /* If we've gotten this far, it's a data member, possibly static,
3275          or an enumerator.  */
3276
3277       DECL_CONTEXT (x) = t;
3278
3279       /* ``A local class cannot have static data members.'' ARM 9.4 */
3280       if (current_function_decl && TREE_STATIC (x))
3281         cp_error_at ("field `%D' in local class cannot be static", x);
3282
3283       /* Perform error checking that did not get done in
3284          grokdeclarator.  */
3285       if (TREE_CODE (type) == FUNCTION_TYPE)
3286         {
3287           cp_error_at ("field `%D' invalidly declared function type",
3288                        x);
3289           type = build_pointer_type (type);
3290           TREE_TYPE (x) = type;
3291         }
3292       else if (TREE_CODE (type) == METHOD_TYPE)
3293         {
3294           cp_error_at ("field `%D' invalidly declared method type", x);
3295           type = build_pointer_type (type);
3296           TREE_TYPE (x) = type;
3297         }
3298       else if (TREE_CODE (type) == OFFSET_TYPE)
3299         {
3300           cp_error_at ("field `%D' invalidly declared offset type", x);
3301           type = build_pointer_type (type);
3302           TREE_TYPE (x) = type;
3303         }
3304
3305       if (type == error_mark_node)
3306         continue;
3307           
3308       /* When this goes into scope, it will be a non-local reference.  */
3309       DECL_NONLOCAL (x) = 1;
3310
3311       if (TREE_CODE (x) == CONST_DECL)
3312         continue;
3313
3314       if (TREE_CODE (x) == VAR_DECL)
3315         {
3316           if (TREE_CODE (t) == UNION_TYPE)
3317             /* Unions cannot have static members.  */
3318             cp_error_at ("field `%D' declared static in union", x);
3319               
3320           continue;
3321         }
3322
3323       /* Now it can only be a FIELD_DECL.  */
3324
3325       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3326         CLASSTYPE_NON_AGGREGATE (t) = 1;
3327
3328       /* If this is of reference type, check if it needs an init.
3329          Also do a little ANSI jig if necessary.  */
3330       if (TREE_CODE (type) == REFERENCE_TYPE)
3331         {
3332           CLASSTYPE_NON_POD_P (t) = 1;
3333           if (DECL_INITIAL (x) == NULL_TREE)
3334             SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
3335
3336           /* ARM $12.6.2: [A member initializer list] (or, for an
3337              aggregate, initialization by a brace-enclosed list) is the
3338              only way to initialize nonstatic const and reference
3339              members.  */
3340           *cant_have_default_ctor_p = 1;
3341           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3342
3343           if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3344               && extra_warnings)
3345             cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3346         }
3347
3348       type = strip_array_types (type);
3349       
3350       if (TREE_CODE (type) == POINTER_TYPE)
3351         has_pointers = 1;
3352
3353       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3354         CLASSTYPE_HAS_MUTABLE (t) = 1;
3355
3356       if (! pod_type_p (type))
3357         /* DR 148 now allows pointers to members (which are POD themselves),
3358            to be allowed in POD structs.  */
3359         CLASSTYPE_NON_POD_P (t) = 1;
3360
3361       if (! zero_init_p (type))
3362         CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
3363
3364       /* If any field is const, the structure type is pseudo-const.  */
3365       if (CP_TYPE_CONST_P (type))
3366         {
3367           C_TYPE_FIELDS_READONLY (t) = 1;
3368           if (DECL_INITIAL (x) == NULL_TREE)
3369             SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
3370
3371           /* ARM $12.6.2: [A member initializer list] (or, for an
3372              aggregate, initialization by a brace-enclosed list) is the
3373              only way to initialize nonstatic const and reference
3374              members.  */
3375           *cant_have_default_ctor_p = 1;
3376           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3377
3378           if (! TYPE_HAS_CONSTRUCTOR (t) && CLASSTYPE_NON_AGGREGATE (t)
3379               && extra_warnings)
3380             cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3381         }
3382       /* A field that is pseudo-const makes the structure likewise.  */
3383       else if (IS_AGGR_TYPE (type))
3384         {
3385           C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3386           SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
3387             CLASSTYPE_READONLY_FIELDS_NEED_INIT (t)
3388             | CLASSTYPE_READONLY_FIELDS_NEED_INIT (type));
3389         }
3390
3391       /* Core issue 80: A nonstatic data member is required to have a
3392          different name from the class iff the class has a
3393          user-defined constructor.  */
3394       if (constructor_name_p (x, t) && TYPE_HAS_CONSTRUCTOR (t))
3395         cp_pedwarn_at ("field `%#D' with same name as class", x);
3396
3397       /* We set DECL_C_BIT_FIELD in grokbitfield.
3398          If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
3399       if (DECL_C_BIT_FIELD (x))
3400         check_bitfield_decl (x);
3401       else
3402         check_field_decl (x, t,
3403                           cant_have_const_ctor_p,
3404                           cant_have_default_ctor_p, 
3405                           no_const_asn_ref_p,
3406                           &any_default_members);
3407     }
3408
3409   /* Effective C++ rule 11.  */
3410   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3411       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3412     {
3413       warning ("`%#T' has pointer data members", t);
3414       
3415       if (! TYPE_HAS_INIT_REF (t))
3416         {
3417           warning ("  but does not override `%T(const %T&)'", t, t);
3418           if (! TYPE_HAS_ASSIGN_REF (t))
3419             warning ("  or `operator=(const %T&)'", t);
3420         }
3421       else if (! TYPE_HAS_ASSIGN_REF (t))
3422         warning ("  but does not override `operator=(const %T&)'", t);
3423     }
3424
3425
3426   /* Check anonymous struct/anonymous union fields.  */
3427   finish_struct_anon (t);
3428
3429   /* We've built up the list of access declarations in reverse order.
3430      Fix that now.  */
3431   *access_decls = nreverse (*access_decls);
3432 }
3433
3434 /* If TYPE is an empty class type, records its OFFSET in the table of
3435    OFFSETS.  */
3436
3437 static int
3438 record_subobject_offset (tree type, tree offset, splay_tree offsets)
3439 {
3440   splay_tree_node n;
3441
3442   if (!is_empty_class (type))
3443     return 0;
3444
3445   /* Record the location of this empty object in OFFSETS.  */
3446   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3447   if (!n)
3448     n = splay_tree_insert (offsets, 
3449                            (splay_tree_key) offset,
3450                            (splay_tree_value) NULL_TREE);
3451   n->value = ((splay_tree_value) 
3452               tree_cons (NULL_TREE,
3453                          type,
3454                          (tree) n->value));
3455
3456   return 0;
3457 }
3458
3459 /* Returns nonzero if TYPE is an empty class type and there is
3460    already an entry in OFFSETS for the same TYPE as the same OFFSET.  */
3461
3462 static int
3463 check_subobject_offset (tree type, tree offset, splay_tree offsets)
3464 {
3465   splay_tree_node n;
3466   tree t;
3467
3468   if (!is_empty_class (type))
3469     return 0;
3470
3471   /* Record the location of this empty object in OFFSETS.  */
3472   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3473   if (!n)
3474     return 0;
3475
3476   for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3477     if (same_type_p (TREE_VALUE (t), type))
3478       return 1;
3479
3480   return 0;
3481 }
3482
3483 /* Walk through all the subobjects of TYPE (located at OFFSET).  Call
3484    F for every subobject, passing it the type, offset, and table of
3485    OFFSETS.  If VBASES_P is one, then virtual non-primary bases should
3486    be traversed.
3487
3488    If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3489    than MAX_OFFSET will not be walked.
3490
3491    If F returns a nonzero value, the traversal ceases, and that value
3492    is returned.  Otherwise, returns zero.  */
3493
3494 static int
3495 walk_subobject_offsets (tree type, 
3496                         subobject_offset_fn f, 
3497                         tree offset, 
3498                         splay_tree offsets, 
3499                         tree max_offset, 
3500                         int vbases_p)
3501 {
3502   int r = 0;
3503   tree type_binfo = NULL_TREE;
3504
3505   /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3506      stop.  */
3507   if (max_offset && INT_CST_LT (max_offset, offset))
3508     return 0;
3509
3510   if (!TYPE_P (type)) 
3511     {
3512       if (abi_version_at_least (2))
3513         type_binfo = type;
3514       type = BINFO_TYPE (type);
3515     }
3516
3517   if (CLASS_TYPE_P (type))
3518     {
3519       tree field;
3520       tree binfo;
3521       int i;
3522
3523       /* Avoid recursing into objects that are not interesting.  */
3524       if (!CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
3525         return 0;
3526
3527       /* Record the location of TYPE.  */
3528       r = (*f) (type, offset, offsets);
3529       if (r)
3530         return r;
3531
3532       /* Iterate through the direct base classes of TYPE.  */
3533       if (!type_binfo)
3534         type_binfo = TYPE_BINFO (type);
3535       for (i = 0; i < BINFO_N_BASETYPES (type_binfo); ++i)
3536         {
3537           tree binfo_offset;
3538
3539           binfo = BINFO_BASETYPE (type_binfo, i);
3540
3541           if (abi_version_at_least (2) 
3542               && TREE_VIA_VIRTUAL (binfo))
3543             continue;
3544
3545           if (!vbases_p 
3546               && TREE_VIA_VIRTUAL (binfo) 
3547               && !BINFO_PRIMARY_P (binfo))
3548             continue;
3549
3550           if (!abi_version_at_least (2))
3551             binfo_offset = size_binop (PLUS_EXPR,
3552                                        offset,
3553                                        BINFO_OFFSET (binfo));
3554           else
3555             {
3556               tree orig_binfo;
3557               /* We cannot rely on BINFO_OFFSET being set for the base
3558                  class yet, but the offsets for direct non-virtual
3559                  bases can be calculated by going back to the TYPE.  */
3560               orig_binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3561               binfo_offset = size_binop (PLUS_EXPR,           
3562                                          offset,
3563                                          BINFO_OFFSET (orig_binfo));
3564             }
3565
3566           r = walk_subobject_offsets (binfo,
3567                                       f,
3568                                       binfo_offset,
3569                                       offsets,
3570                                       max_offset,
3571                                       (abi_version_at_least (2) 
3572                                        ? /*vbases_p=*/0 : vbases_p));
3573           if (r)
3574             return r;
3575         }
3576
3577       if (abi_version_at_least (2))
3578         {
3579           tree vbase;
3580
3581           /* Iterate through the virtual base classes of TYPE.  In G++
3582              3.2, we included virtual bases in the direct base class
3583              loop above, which results in incorrect results; the
3584              correct offsets for virtual bases are only known when
3585              working with the most derived type.  */
3586           if (vbases_p)
3587             for (vbase = CLASSTYPE_VBASECLASSES (type);
3588                  vbase;
3589                  vbase = TREE_CHAIN (vbase))
3590               {
3591                 binfo = TREE_VALUE (vbase);
3592                 r = walk_subobject_offsets (binfo,
3593                                             f,
3594                                             size_binop (PLUS_EXPR,
3595                                                         offset,
3596                                                         BINFO_OFFSET (binfo)),
3597                                             offsets,
3598                                             max_offset,
3599                                             /*vbases_p=*/0);
3600                 if (r)
3601                   return r;
3602               }
3603           else
3604             {
3605               /* We still have to walk the primary base, if it is
3606                  virtual.  (If it is non-virtual, then it was walked
3607                  above.)  */
3608               vbase = get_primary_binfo (type_binfo);
3609               if (vbase && TREE_VIA_VIRTUAL (vbase))
3610                 {
3611                   tree derived = type_binfo;
3612                   while (BINFO_INHERITANCE_CHAIN (derived))
3613                     derived = BINFO_INHERITANCE_CHAIN (derived);
3614                   derived = TREE_TYPE (derived);
3615                   vbase = binfo_for_vbase (TREE_TYPE (vbase), derived);
3616
3617                   if (BINFO_PRIMARY_BASE_OF (vbase) == type_binfo)
3618                     {
3619                       r = (walk_subobject_offsets 
3620                            (vbase, f, offset,
3621                             offsets, max_offset, /*vbases_p=*/0));
3622                       if (r)
3623                         return r;
3624                     }
3625                 }
3626             }
3627         }
3628
3629       /* Iterate through the fields of TYPE.  */
3630       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3631         if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
3632           {
3633             tree field_offset;
3634
3635             if (abi_version_at_least (2))
3636               field_offset = byte_position (field);
3637             else
3638               /* In G++ 3.2, DECL_FIELD_OFFSET was used.  */
3639               field_offset = DECL_FIELD_OFFSET (field);
3640
3641             r = walk_subobject_offsets (TREE_TYPE (field),
3642                                         f,
3643                                         size_binop (PLUS_EXPR,
3644                                                     offset,
3645                                                     field_offset),
3646                                         offsets,
3647                                         max_offset,
3648                                         /*vbases_p=*/1);
3649             if (r)
3650               return r;
3651           }
3652     }
3653   else if (TREE_CODE (type) == ARRAY_TYPE)
3654     {
3655       tree element_type = strip_array_types (type);
3656       tree domain = TYPE_DOMAIN (type);
3657       tree index;
3658
3659       /* Avoid recursing into objects that are not interesting.  */
3660       if (!CLASS_TYPE_P (element_type)
3661           || !CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
3662         return 0;
3663
3664       /* Step through each of the elements in the array.  */
3665       for (index = size_zero_node;
3666            /* G++ 3.2 had an off-by-one error here.  */
3667            (abi_version_at_least (2) 
3668             ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
3669             : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
3670            index = size_binop (PLUS_EXPR, index, size_one_node))
3671         {
3672           r = walk_subobject_offsets (TREE_TYPE (type),
3673                                       f,
3674                                       offset,
3675                                       offsets,
3676                                       max_offset,
3677                                       /*vbases_p=*/1);
3678           if (r)
3679             return r;
3680           offset = size_binop (PLUS_EXPR, offset, 
3681                                TYPE_SIZE_UNIT (TREE_TYPE (type)));
3682           /* If this new OFFSET is bigger than the MAX_OFFSET, then
3683              there's no point in iterating through the remaining
3684              elements of the array.  */
3685           if (max_offset && INT_CST_LT (max_offset, offset))
3686             break;
3687         }
3688     }
3689
3690   return 0;
3691 }
3692
3693 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3694    OFFSETS.  If VBASES_P is nonzero, virtual bases of TYPE are
3695    examined.  */
3696
3697 static void
3698 record_subobject_offsets (tree type, 
3699                           tree offset, 
3700                           splay_tree offsets, 
3701                           int vbases_p)
3702 {
3703   walk_subobject_offsets (type, record_subobject_offset, offset,
3704                           offsets, /*max_offset=*/NULL_TREE, vbases_p);
3705 }
3706
3707 /* Returns nonzero if any of the empty subobjects of TYPE (located at
3708    OFFSET) conflict with entries in OFFSETS.  If VBASES_P is nonzero,
3709    virtual bases of TYPE are examined.  */
3710
3711 static int
3712 layout_conflict_p (tree type,
3713                    tree offset, 
3714                    splay_tree offsets, 
3715                    int vbases_p)
3716 {
3717   splay_tree_node max_node;
3718
3719   /* Get the node in OFFSETS that indicates the maximum offset where
3720      an empty subobject is located.  */
3721   max_node = splay_tree_max (offsets);
3722   /* If there aren't any empty subobjects, then there's no point in
3723      performing this check.  */
3724   if (!max_node)
3725     return 0;
3726
3727   return walk_subobject_offsets (type, check_subobject_offset, offset,
3728                                  offsets, (tree) (max_node->key),
3729                                  vbases_p);
3730 }
3731
3732 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3733    non-static data member of the type indicated by RLI.  BINFO is the
3734    binfo corresponding to the base subobject, OFFSETS maps offsets to
3735    types already located at those offsets.  This function determines
3736    the position of the DECL.  */
3737
3738 static void
3739 layout_nonempty_base_or_field (record_layout_info rli, 
3740                                tree decl, 
3741                                tree binfo, 
3742                                splay_tree offsets)
3743 {
3744   tree t = rli->t;
3745   tree offset = NULL_TREE;
3746   bool field_p;
3747   tree type;
3748   
3749   if (binfo)
3750     {
3751       /* For the purposes of determining layout conflicts, we want to
3752          use the class type of BINFO; TREE_TYPE (DECL) will be the
3753          CLASSTYPE_AS_BASE version, which does not contain entries for
3754          zero-sized bases.  */
3755       type = TREE_TYPE (binfo);
3756       field_p = false;
3757     }
3758   else
3759     {
3760       type = TREE_TYPE (decl);
3761       field_p = true;
3762     }
3763
3764   /* Try to place the field.  It may take more than one try if we have
3765      a hard time placing the field without putting two objects of the
3766      same type at the same address.  */
3767   while (1)
3768     {
3769       struct record_layout_info_s old_rli = *rli;
3770
3771       /* Place this field.  */
3772       place_field (rli, decl);
3773       offset = byte_position (decl);
3774  
3775       /* We have to check to see whether or not there is already
3776          something of the same type at the offset we're about to use.
3777          For example:
3778          
3779          struct S {};
3780          struct T : public S { int i; };
3781          struct U : public S, public T {};
3782          
3783          Here, we put S at offset zero in U.  Then, we can't put T at
3784          offset zero -- its S component would be at the same address
3785          as the S we already allocated.  So, we have to skip ahead.
3786          Since all data members, including those whose type is an
3787          empty class, have nonzero size, any overlap can happen only
3788          with a direct or indirect base-class -- it can't happen with
3789          a data member.  */
3790       /* G++ 3.2 did not check for overlaps when placing a non-empty
3791          virtual base.  */
3792       if (!abi_version_at_least (2) && binfo && TREE_VIA_VIRTUAL (binfo))
3793         break;
3794       if (layout_conflict_p (field_p ? type : binfo, offset, 
3795                              offsets, field_p))
3796         {
3797           /* Strip off the size allocated to this field.  That puts us
3798              at the first place we could have put the field with
3799              proper alignment.  */
3800           *rli = old_rli;
3801
3802           /* Bump up by the alignment required for the type.  */
3803           rli->bitpos
3804             = size_binop (PLUS_EXPR, rli->bitpos, 
3805                           bitsize_int (binfo 
3806                                        ? CLASSTYPE_ALIGN (type)
3807                                        : TYPE_ALIGN (type)));
3808           normalize_rli (rli);
3809         }
3810       else
3811         /* There was no conflict.  We're done laying out this field.  */
3812         break;
3813     }
3814
3815   /* Now that we know where it will be placed, update its
3816      BINFO_OFFSET.  */
3817   if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3818     /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
3819        this point because their BINFO_OFFSET is copied from another
3820        hierarchy.  Therefore, we may not need to add the entire
3821        OFFSET.  */
3822     propagate_binfo_offsets (binfo, 
3823                              size_diffop (convert (ssizetype, offset),
3824                                           convert (ssizetype, 
3825                                                    BINFO_OFFSET (binfo))),
3826                              t);
3827 }
3828
3829 /* Returns true if TYPE is empty and OFFSET is nonzero.  */
3830
3831 static int
3832 empty_base_at_nonzero_offset_p (tree type,
3833                                 tree offset,
3834                                 splay_tree offsets ATTRIBUTE_UNUSED)
3835 {
3836   return is_empty_class (type) && !integer_zerop (offset);
3837 }
3838
3839 /* Layout the empty base BINFO.  EOC indicates the byte currently just
3840    past the end of the class, and should be correctly aligned for a
3841    class of the type indicated by BINFO; OFFSETS gives the offsets of
3842    the empty bases allocated so far. T is the most derived
3843    type.  Return nonzero iff we added it at the end.  */
3844
3845 static bool
3846 layout_empty_base (tree binfo, tree eoc, splay_tree offsets, tree t)
3847 {
3848   tree alignment;
3849   tree basetype = BINFO_TYPE (binfo);
3850   bool atend = false;
3851
3852   /* This routine should only be used for empty classes.  */
3853   my_friendly_assert (is_empty_class (basetype), 20000321);
3854   alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3855   
3856   if (abi_version_at_least (2))
3857     BINFO_OFFSET (binfo) = size_zero_node;
3858   if (warn_abi && !integer_zerop (BINFO_OFFSET (binfo)))
3859     warning ("offset of empty base `%T' may not be ABI-compliant and may"
3860              "change in a future version of GCC",
3861              BINFO_TYPE (binfo));
3862
3863   /* This is an empty base class.  We first try to put it at offset
3864      zero.  */
3865   if (layout_conflict_p (binfo,
3866                          BINFO_OFFSET (binfo),
3867                          offsets, 
3868                          /*vbases_p=*/0))
3869     {
3870       /* That didn't work.  Now, we move forward from the next
3871          available spot in the class.  */
3872       atend = true;
3873       propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3874       while (1) 
3875         {
3876           if (!layout_conflict_p (binfo,
3877                                   BINFO_OFFSET (binfo), 
3878                                   offsets,
3879                                   /*vbases_p=*/0))
3880             /* We finally found a spot where there's no overlap.  */
3881             break;
3882
3883           /* There's overlap here, too.  Bump along to the next spot.  */
3884           propagate_binfo_offsets (binfo, alignment, t);
3885         }
3886     }
3887   return atend;
3888 }
3889
3890 /* Layout the the base given by BINFO in the class indicated by RLI.
3891    *BASE_ALIGN is a running maximum of the alignments of
3892    any base class.  OFFSETS gives the location of empty base
3893    subobjects.  T is the most derived type.  Return nonzero if the new
3894    object cannot be nearly-empty.  A new FIELD_DECL is inserted at
3895    *NEXT_FIELD, unless BINFO is for an empty base class.  
3896
3897    Returns the location at which the next field should be inserted.  */
3898
3899 static tree *
3900 build_base_field (record_layout_info rli, tree binfo,
3901                   splay_tree offsets, tree *next_field)
3902 {
3903   tree t = rli->t;
3904   tree basetype = BINFO_TYPE (binfo);
3905
3906   if (!COMPLETE_TYPE_P (basetype))
3907     /* This error is now reported in xref_tag, thus giving better
3908        location information.  */
3909     return next_field;
3910   
3911   /* Place the base class.  */
3912   if (!is_empty_class (basetype))
3913     {
3914       tree decl;
3915
3916       /* The containing class is non-empty because it has a non-empty
3917          base class.  */
3918       CLASSTYPE_EMPTY_P (t) = 0;
3919       
3920       /* Create the FIELD_DECL.  */
3921       decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
3922       DECL_ARTIFICIAL (decl) = 1;
3923       DECL_FIELD_CONTEXT (decl) = t;
3924       DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3925       DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3926       DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3927       DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3928   
3929       /* Try to place the field.  It may take more than one try if we
3930          have a hard time placing the field without putting two
3931          objects of the same type at the same address.  */
3932       layout_nonempty_base_or_field (rli, decl, binfo, offsets);
3933       /* Add the new FIELD_DECL to the list of fields for T.  */
3934       TREE_CHAIN (decl) = *next_field;
3935       *next_field = decl;
3936       next_field = &TREE_CHAIN (decl);
3937     }
3938   else
3939     {
3940       tree eoc;
3941       bool atend;
3942
3943       /* On some platforms (ARM), even empty classes will not be
3944          byte-aligned.  */
3945       eoc = round_up (rli_size_unit_so_far (rli),
3946                       CLASSTYPE_ALIGN_UNIT (basetype));
3947       atend = layout_empty_base (binfo, eoc, offsets, t);
3948       /* A nearly-empty class "has no proper base class that is empty,
3949          not morally virtual, and at an offset other than zero."  */
3950       if (!TREE_VIA_VIRTUAL (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
3951         {
3952           if (atend)
3953             CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3954           /* The check above (used in G++ 3.2) is insufficient  because
3955              an empty class placed at offset zero might itself have an
3956              empty base at a nonzero offset.  */
3957           else if (walk_subobject_offsets (basetype, 
3958                                            empty_base_at_nonzero_offset_p,
3959                                            size_zero_node,
3960                                            /*offsets=*/NULL,
3961                                            /*max_offset=*/NULL_TREE,
3962                                            /*vbases_p=*/true))
3963             {
3964               if (abi_version_at_least (2))
3965                 CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3966               else if (warn_abi)
3967                 warning ("class `%T' will be considered nearly empty in a "
3968                          "future version of GCC", t);
3969             }
3970         }
3971         
3972       /* We do not create a FIELD_DECL for empty base classes because
3973          it might overlap some other field.  We want to be able to
3974          create CONSTRUCTORs for the class by iterating over the
3975          FIELD_DECLs, and the back end does not handle overlapping
3976          FIELD_DECLs.  */
3977
3978       /* An empty virtual base causes a class to be non-empty
3979          -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
3980          here because that was already done when the virtual table
3981          pointer was created.  */
3982     }
3983
3984   /* Record the offsets of BINFO and its base subobjects.  */
3985   record_subobject_offsets (binfo,
3986                             BINFO_OFFSET (binfo),
3987                             offsets, 
3988                             /*vbases_p=*/0);
3989
3990   return next_field;
3991 }
3992
3993 /* Layout all of the non-virtual base classes.  Record empty
3994    subobjects in OFFSETS.  T is the most derived type.  Return nonzero
3995    if the type cannot be nearly empty.  The fields created
3996    corresponding to the base classes will be inserted at
3997    *NEXT_FIELD.  */
3998
3999 static void
4000 build_base_fields (record_layout_info rli,
4001                    splay_tree offsets, tree *next_field)
4002 {
4003   /* Chain to hold all the new FIELD_DECLs which stand in for base class
4004      subobjects.  */
4005   tree t = rli->t;
4006   int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
4007   int i;
4008
4009   /* The primary base class is always allocated first.  */
4010   if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
4011     next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
4012                                    offsets, next_field);
4013
4014   /* Now allocate the rest of the bases.  */
4015   for (i = 0; i < n_baseclasses; ++i)
4016     {
4017       tree base_binfo;
4018
4019       base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4020
4021       /* The primary base was already allocated above, so we don't
4022          need to allocate it again here.  */
4023       if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
4024         continue;
4025
4026       /* A primary virtual base class is allocated just like any other
4027          base class, but a non-primary virtual base is allocated
4028          later, in layout_virtual_bases.  */
4029       if (TREE_VIA_VIRTUAL (base_binfo) 
4030           && !BINFO_PRIMARY_P (base_binfo))
4031         continue;
4032
4033       next_field = build_base_field (rli, base_binfo,
4034                                      offsets, next_field);
4035     }
4036 }
4037
4038 /* Go through the TYPE_METHODS of T issuing any appropriate
4039    diagnostics, figuring out which methods override which other
4040    methods, and so forth.  */
4041
4042 static void
4043 check_methods (tree t)
4044 {
4045   tree x;
4046
4047   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4048     {
4049       /* If this was an evil function, don't keep it in class.  */
4050       if (DECL_ASSEMBLER_NAME_SET_P (x) 
4051           && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
4052         continue;
4053
4054       check_for_override (x, t);
4055       if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
4056         cp_error_at ("initializer specified for non-virtual method `%D'", x);
4057
4058       /* The name of the field is the original field name
4059          Save this in auxiliary field for later overloading.  */
4060       if (DECL_VINDEX (x))
4061         {
4062           TYPE_POLYMORPHIC_P (t) = 1;
4063           if (DECL_PURE_VIRTUAL_P (x))
4064             CLASSTYPE_PURE_VIRTUALS (t)
4065               = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4066         }
4067     }
4068 }
4069
4070 /* FN is a constructor or destructor.  Clone the declaration to create
4071    a specialized in-charge or not-in-charge version, as indicated by
4072    NAME.  */
4073
4074 static tree
4075 build_clone (tree fn, tree name)
4076 {
4077   tree parms;
4078   tree clone;
4079
4080   /* Copy the function.  */
4081   clone = copy_decl (fn);
4082   /* Remember where this function came from.  */
4083   DECL_CLONED_FUNCTION (clone) = fn;
4084   DECL_ABSTRACT_ORIGIN (clone) = fn;
4085   /* Reset the function name.  */
4086   DECL_NAME (clone) = name;
4087   SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4088   /* There's no pending inline data for this function.  */
4089   DECL_PENDING_INLINE_INFO (clone) = NULL;
4090   DECL_PENDING_INLINE_P (clone) = 0;
4091   /* And it hasn't yet been deferred.  */
4092   DECL_DEFERRED_FN (clone) = 0;
4093
4094   /* The base-class destructor is not virtual.  */
4095   if (name == base_dtor_identifier)
4096     {
4097       DECL_VIRTUAL_P (clone) = 0;
4098       if (TREE_CODE (clone) != TEMPLATE_DECL)
4099         DECL_VINDEX (clone) = NULL_TREE;
4100     }
4101
4102   /* If there was an in-charge parameter, drop it from the function
4103      type.  */
4104   if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4105     {
4106       tree basetype;
4107       tree parmtypes;
4108       tree exceptions;
4109
4110       exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4111       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4112       parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4113       /* Skip the `this' parameter.  */
4114       parmtypes = TREE_CHAIN (parmtypes);
4115       /* Skip the in-charge parameter.  */
4116       parmtypes = TREE_CHAIN (parmtypes);
4117       /* And the VTT parm, in a complete [cd]tor.  */
4118       if (DECL_HAS_VTT_PARM_P (fn)
4119           && ! DECL_NEEDS_VTT_PARM_P (clone))
4120         parmtypes = TREE_CHAIN (parmtypes);
4121        /* If this is subobject constructor or destructor, add the vtt
4122          parameter.  */
4123       TREE_TYPE (clone) 
4124         = build_cplus_method_type (basetype,
4125                                    TREE_TYPE (TREE_TYPE (clone)),
4126                                    parmtypes);
4127       if (exceptions)
4128         TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4129                                                      exceptions);
4130     }
4131
4132   /* Copy the function parameters.  But, DECL_ARGUMENTS on a TEMPLATE_DECL
4133      aren't function parameters; those are the template parameters.  */
4134   if (TREE_CODE (clone) != TEMPLATE_DECL)
4135     {
4136       DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4137       /* Remove the in-charge parameter.  */
4138       if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4139         {
4140           TREE_CHAIN (DECL_ARGUMENTS (clone))
4141             = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4142           DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4143         }
4144       /* And the VTT parm, in a complete [cd]tor.  */
4145       if (DECL_HAS_VTT_PARM_P (fn))
4146         {
4147           if (DECL_NEEDS_VTT_PARM_P (clone))
4148             DECL_HAS_VTT_PARM_P (clone) = 1;
4149           else
4150             {
4151               TREE_CHAIN (DECL_ARGUMENTS (clone))
4152                 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4153               DECL_HAS_VTT_PARM_P (clone) = 0;
4154             }
4155         }
4156
4157       for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4158         {
4159           DECL_CONTEXT (parms) = clone;
4160           cxx_dup_lang_specific_decl (parms);
4161         }
4162     }
4163
4164   /* Create the RTL for this function.  */
4165   SET_DECL_RTL (clone, NULL_RTX);
4166   rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4167   
4168   /* Make it easy to find the CLONE given the FN.  */
4169   TREE_CHAIN (clone) = TREE_CHAIN (fn);
4170   TREE_CHAIN (fn) = clone;
4171
4172   /* If this is a template, handle the DECL_TEMPLATE_RESULT as well.  */
4173   if (TREE_CODE (clone) == TEMPLATE_DECL)
4174     {
4175       tree result;
4176
4177       DECL_TEMPLATE_RESULT (clone) 
4178         = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4179       result = DECL_TEMPLATE_RESULT (clone);
4180       DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4181       DECL_TI_TEMPLATE (result) = clone;
4182     }
4183   else if (DECL_DEFERRED_FN (fn))
4184     defer_fn (clone);
4185
4186   return clone;
4187 }
4188
4189 /* Produce declarations for all appropriate clones of FN.  If
4190    UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
4191    CLASTYPE_METHOD_VEC as well.  */
4192
4193 void
4194 clone_function_decl (tree fn, int update_method_vec_p)
4195 {
4196   tree clone;
4197
4198   /* Avoid inappropriate cloning.  */
4199   if (TREE_CHAIN (fn)
4200       && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4201     return;
4202
4203   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4204     {
4205       /* For each constructor, we need two variants: an in-charge version
4206          and a not-in-charge version.  */
4207       clone = build_clone (fn, complete_ctor_identifier);
4208       if (update_method_vec_p)
4209         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4210       clone = build_clone (fn, base_ctor_identifier);
4211       if (update_method_vec_p)
4212         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4213     }
4214   else
4215     {
4216       my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4217
4218       /* For each destructor, we need three variants: an in-charge
4219          version, a not-in-charge version, and an in-charge deleting
4220          version.  We clone the deleting version first because that
4221          means it will go second on the TYPE_METHODS list -- and that
4222          corresponds to the correct layout order in the virtual
4223          function table.  
4224
4225          For a non-virtual destructor, we do not build a deleting
4226          destructor.  */
4227       if (DECL_VIRTUAL_P (fn))
4228         {
4229           clone = build_clone (fn, deleting_dtor_identifier);
4230           if (update_method_vec_p)
4231             add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4232         }
4233       clone = build_clone (fn, complete_dtor_identifier);
4234       if (update_method_vec_p)
4235         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4236       clone = build_clone (fn, base_dtor_identifier);
4237       if (update_method_vec_p)
4238         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4239     }
4240
4241   /* Note that this is an abstract function that is never emitted.  */
4242   DECL_ABSTRACT (fn) = 1;
4243 }
4244
4245 /* DECL is an in charge constructor, which is being defined. This will
4246    have had an in class declaration, from whence clones were
4247    declared. An out-of-class definition can specify additional default
4248    arguments. As it is the clones that are involved in overload
4249    resolution, we must propagate the information from the DECL to its
4250    clones.  */
4251
4252 void
4253 adjust_clone_args (tree decl)
4254 {
4255   tree clone;
4256   
4257   for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4258        clone = TREE_CHAIN (clone))
4259     {
4260       tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4261       tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4262       tree decl_parms, clone_parms;
4263
4264       clone_parms = orig_clone_parms;
4265       
4266       /* Skip the 'this' parameter.  */
4267       orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4268       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4269
4270       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4271         orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4272       if (DECL_HAS_VTT_PARM_P (decl))
4273         orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4274       
4275       clone_parms = orig_clone_parms;
4276       if (DECL_HAS_VTT_PARM_P (clone))
4277         clone_parms = TREE_CHAIN (clone_parms);
4278       
4279       for (decl_parms = orig_decl_parms; decl_parms;
4280            decl_parms = TREE_CHAIN (decl_parms),
4281              clone_parms = TREE_CHAIN (clone_parms))
4282         {
4283           my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4284                                            TREE_TYPE (clone_parms)), 20010424);
4285           
4286           if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4287             {
4288               /* A default parameter has been added. Adjust the
4289                  clone's parameters.  */
4290               tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4291               tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4292               tree type;
4293
4294               clone_parms = orig_decl_parms;
4295
4296               if (DECL_HAS_VTT_PARM_P (clone))
4297                 {
4298                   clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4299                                            TREE_VALUE (orig_clone_parms),
4300                                            clone_parms);
4301                   TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4302                 }
4303               type = build_cplus_method_type (basetype,
4304                                               TREE_TYPE (TREE_TYPE (clone)),
4305                                               clone_parms);
4306               if (exceptions)
4307                 type = build_exception_variant (type, exceptions);
4308               TREE_TYPE (clone) = type;
4309               
4310               clone_parms = NULL_TREE;
4311               break;
4312             }
4313         }
4314       my_friendly_assert (!clone_parms, 20010424);
4315     }
4316 }
4317
4318 /* For each of the constructors and destructors in T, create an
4319    in-charge and not-in-charge variant.  */
4320
4321 static void
4322 clone_constructors_and_destructors (tree t)
4323 {
4324   tree fns;
4325
4326   /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4327      out now.  */
4328   if (!CLASSTYPE_METHOD_VEC (t))
4329     return;
4330
4331   for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4332     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4333   for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4334     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4335 }
4336
4337 /* Remove all zero-width bit-fields from T.  */
4338
4339 static void
4340 remove_zero_width_bit_fields (tree t)
4341 {
4342   tree *fieldsp;
4343
4344   fieldsp = &TYPE_FIELDS (t); 
4345   while (*fieldsp)
4346     {
4347       if (TREE_CODE (*fieldsp) == FIELD_DECL
4348           && DECL_C_BIT_FIELD (*fieldsp) 
4349           && DECL_INITIAL (*fieldsp))
4350         *fieldsp = TREE_CHAIN (*fieldsp);
4351       else
4352         fieldsp = &TREE_CHAIN (*fieldsp);
4353     }
4354 }
4355
4356 /* Returns TRUE iff we need a cookie when dynamically allocating an
4357    array whose elements have the indicated class TYPE.  */
4358
4359 static bool
4360 type_requires_array_cookie (tree type)
4361 {
4362   tree fns;
4363   bool has_two_argument_delete_p = false;
4364
4365   my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4366
4367   /* If there's a non-trivial destructor, we need a cookie.  In order
4368      to iterate through the array calling the destructor for each
4369      element, we'll have to know how many elements there are.  */
4370   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4371     return true;
4372
4373   /* If the usual deallocation function is a two-argument whose second
4374      argument is of type `size_t', then we have to pass the size of
4375      the array to the deallocation function, so we will need to store
4376      a cookie.  */
4377   fns = lookup_fnfields (TYPE_BINFO (type), 
4378                          ansi_opname (VEC_DELETE_EXPR),
4379                          /*protect=*/0);
4380   /* If there are no `operator []' members, or the lookup is
4381      ambiguous, then we don't need a cookie.  */
4382   if (!fns || fns == error_mark_node)
4383     return false;
4384   /* Loop through all of the functions.  */
4385   for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
4386     {
4387       tree fn;
4388       tree second_parm;
4389
4390       /* Select the current function.  */
4391       fn = OVL_CURRENT (fns);
4392       /* See if this function is a one-argument delete function.  If
4393          it is, then it will be the usual deallocation function.  */
4394       second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4395       if (second_parm == void_list_node)
4396         return false;
4397       /* Otherwise, if we have a two-argument function and the second
4398          argument is `size_t', it will be the usual deallocation
4399          function -- unless there is one-argument function, too.  */
4400       if (TREE_CHAIN (second_parm) == void_list_node
4401           && same_type_p (TREE_VALUE (second_parm), sizetype))
4402         has_two_argument_delete_p = true;
4403     }
4404
4405   return has_two_argument_delete_p;
4406 }
4407
4408 /* Check the validity of the bases and members declared in T.  Add any
4409    implicitly-generated functions (like copy-constructors and
4410    assignment operators).  Compute various flag bits (like
4411    CLASSTYPE_NON_POD_T) for T.  This routine works purely at the C++
4412    level: i.e., independently of the ABI in use.  */
4413
4414 static void
4415 check_bases_and_members (tree t)
4416 {
4417   /* Nonzero if we are not allowed to generate a default constructor
4418      for this case.  */
4419   int cant_have_default_ctor;
4420   /* Nonzero if the implicitly generated copy constructor should take
4421      a non-const reference argument.  */
4422   int cant_have_const_ctor;
4423   /* Nonzero if the the implicitly generated assignment operator
4424      should take a non-const reference argument.  */
4425   int no_const_asn_ref;
4426   tree access_decls;
4427
4428   /* By default, we use const reference arguments and generate default
4429      constructors.  */
4430   cant_have_default_ctor = 0;
4431   cant_have_const_ctor = 0;
4432   no_const_asn_ref = 0;
4433
4434   /* Check all the base-classes.  */
4435   check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4436                &no_const_asn_ref);
4437
4438   /* Check all the data member declarations.  */
4439   check_field_decls (t, &access_decls,
4440                      &cant_have_default_ctor,
4441                      &cant_have_const_ctor,
4442                      &no_const_asn_ref);
4443
4444   /* Check all the method declarations.  */
4445   check_methods (t);
4446
4447   /* A nearly-empty class has to be vptr-containing; a nearly empty
4448      class contains just a vptr.  */
4449   if (!TYPE_CONTAINS_VPTR_P (t))
4450     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4451
4452   /* Do some bookkeeping that will guide the generation of implicitly
4453      declared member functions.  */
4454   TYPE_HAS_COMPLEX_INIT_REF (t)
4455     |= (TYPE_HAS_INIT_REF (t) 
4456         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4457         || TYPE_POLYMORPHIC_P (t));
4458   TYPE_NEEDS_CONSTRUCTING (t)
4459     |= (TYPE_HAS_CONSTRUCTOR (t) 
4460         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4461         || TYPE_POLYMORPHIC_P (t));
4462   CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4463                                   || TYPE_POLYMORPHIC_P (t));
4464   CLASSTYPE_NON_POD_P (t)
4465     |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t) 
4466         || TYPE_HAS_ASSIGN_REF (t));
4467   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4468   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4469     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4470
4471   /* Synthesize any needed methods.  Note that methods will be synthesized
4472      for anonymous unions; grok_x_components undoes that.  */
4473   add_implicitly_declared_members (t, cant_have_default_ctor,
4474                                    cant_have_const_ctor,
4475                                    no_const_asn_ref);
4476
4477   /* Create the in-charge and not-in-charge variants of constructors
4478      and destructors.  */
4479   clone_constructors_and_destructors (t);
4480
4481   /* Process the using-declarations.  */
4482   for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4483     handle_using_decl (TREE_VALUE (access_decls), t);
4484
4485   /* Build and sort the CLASSTYPE_METHOD_VEC.  */
4486   finish_struct_methods (t);
4487
4488   /* Figure out whether or not we will need a cookie when dynamically
4489      allocating an array of this type.  */
4490   TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
4491     = type_requires_array_cookie (t);
4492 }
4493
4494 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4495    accordingly.  If a new vfield was created (because T doesn't have a
4496    primary base class), then the newly created field is returned.  It
4497    is not added to the TYPE_FIELDS list; it is the caller's
4498    responsibility to do that.  Accumulate declared virtual functions
4499    on VIRTUALS_P.  */
4500
4501 static tree
4502 create_vtable_ptr (tree t, tree* virtuals_p)
4503 {
4504   tree fn;
4505
4506   /* Collect the virtual functions declared in T.  */
4507   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4508     if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
4509         && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
4510       {
4511         tree new_virtual = make_node (TREE_LIST);
4512         
4513         BV_FN (new_virtual) = fn;
4514         BV_DELTA (new_virtual) = integer_zero_node;
4515
4516         TREE_CHAIN (new_virtual) = *virtuals_p;
4517         *virtuals_p = new_virtual;
4518       }
4519   
4520   /* If we couldn't find an appropriate base class, create a new field
4521      here.  Even if there weren't any new virtual functions, we might need a
4522      new virtual function table if we're supposed to include vptrs in
4523      all classes that need them.  */
4524   if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
4525     {
4526       /* We build this decl with vtbl_ptr_type_node, which is a
4527          `vtable_entry_type*'.  It might seem more precise to use
4528          `vtable_entry_type (*)[N]' where N is the number of firtual
4529          functions.  However, that would require the vtable pointer in
4530          base classes to have a different type than the vtable pointer
4531          in derived classes.  We could make that happen, but that
4532          still wouldn't solve all the problems.  In particular, the
4533          type-based alias analysis code would decide that assignments
4534          to the base class vtable pointer can't alias assignments to
4535          the derived class vtable pointer, since they have different
4536          types.  Thus, in a derived class destructor, where the base
4537          class constructor was inlined, we could generate bad code for
4538          setting up the vtable pointer.  
4539
4540          Therefore, we use one type for all vtable pointers.  We still
4541          use a type-correct type; it's just doesn't indicate the array
4542          bounds.  That's better than using `void*' or some such; it's
4543          cleaner, and it let's the alias analysis code know that these
4544          stores cannot alias stores to void*!  */
4545       tree field;
4546
4547       field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4548       SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4549       DECL_VIRTUAL_P (field) = 1;
4550       DECL_ARTIFICIAL (field) = 1;
4551       DECL_FIELD_CONTEXT (field) = t;
4552       DECL_FCONTEXT (field) = t;
4553       DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4554       DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4555       
4556       TYPE_VFIELD (t) = field;
4557       
4558       /* This class is non-empty.  */
4559       CLASSTYPE_EMPTY_P (t) = 0;
4560
4561       if (CLASSTYPE_N_BASECLASSES (t))
4562         /* If there were any baseclasses, they can't possibly be at
4563            offset zero any more, because that's where the vtable
4564            pointer is.  So, converting to a base class is going to
4565            take work.  */
4566         TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4567
4568       return field;
4569     }
4570
4571   return NULL_TREE;
4572 }
4573
4574 /* Fixup the inline function given by INFO now that the class is
4575    complete.  */
4576
4577 static void
4578 fixup_pending_inline (tree fn)
4579 {
4580   if (DECL_PENDING_INLINE_INFO (fn))
4581     {
4582       tree args = DECL_ARGUMENTS (fn);
4583       while (args)
4584         {
4585           DECL_CONTEXT (args) = fn;
4586           args = TREE_CHAIN (args);
4587         }
4588     }
4589 }
4590
4591 /* Fixup the inline methods and friends in TYPE now that TYPE is
4592    complete.  */
4593
4594 static void
4595 fixup_inline_methods (tree type)
4596 {
4597   tree method = TYPE_METHODS (type);
4598
4599   if (method && TREE_CODE (method) == TREE_VEC)
4600     {
4601       if (TREE_VEC_ELT (method, 1))
4602         method = TREE_VEC_ELT (method, 1);
4603       else if (TREE_VEC_ELT (method, 0))
4604         method = TREE_VEC_ELT (method, 0);
4605       else
4606         method = TREE_VEC_ELT (method, 2);
4607     }
4608
4609   /* Do inline member functions.  */
4610   for (; method; method = TREE_CHAIN (method))
4611     fixup_pending_inline (method);
4612
4613   /* Do friends.  */
4614   for (method = CLASSTYPE_INLINE_FRIENDS (type); 
4615        method; 
4616        method = TREE_CHAIN (method))
4617     fixup_pending_inline (TREE_VALUE (method));
4618   CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4619 }
4620
4621 /* Add OFFSET to all base types of BINFO which is a base in the
4622    hierarchy dominated by T.
4623
4624    OFFSET, which is a type offset, is number of bytes.  */
4625
4626 static void
4627 propagate_binfo_offsets (tree binfo, tree offset, tree t)
4628 {
4629   int i;
4630   tree primary_binfo;
4631
4632   /* Update BINFO's offset.  */
4633   BINFO_OFFSET (binfo)
4634     = convert (sizetype, 
4635                size_binop (PLUS_EXPR,
4636                            convert (ssizetype, BINFO_OFFSET (binfo)),
4637                            offset));
4638
4639   /* Find the primary base class.  */
4640   primary_binfo = get_primary_binfo (binfo);
4641
4642   /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4643      downwards.  */
4644   for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4645     {
4646       tree base_binfo;
4647
4648       /* On the first time through the loop, do the primary base.
4649          Because the primary base need not be an immediate base, we
4650          must handle the primary base specially.  */
4651       if (i == -1) 
4652         {
4653           if (!primary_binfo) 
4654             continue;
4655
4656           base_binfo = primary_binfo;
4657         }
4658       else
4659         {
4660           base_binfo = BINFO_BASETYPE (binfo, i);
4661           /* Don't do the primary base twice.  */
4662           if (base_binfo == primary_binfo)
4663             continue;
4664         }
4665
4666       /* Skip virtual bases that aren't our canonical primary base.  */
4667       if (TREE_VIA_VIRTUAL (base_binfo)
4668           && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4669               || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4670         continue;
4671
4672       propagate_binfo_offsets (base_binfo, offset, t);
4673     }
4674 }
4675
4676 /* Called via dfs_walk from layout_virtual bases.  */
4677
4678 static tree
4679 dfs_set_offset_for_unshared_vbases (tree binfo, void* data)
4680 {
4681   /* If this is a virtual base, make sure it has the same offset as
4682      the shared copy.  If it's a primary base, then we know it's
4683      correct.  */
4684   if (TREE_VIA_VIRTUAL (binfo))
4685     {
4686       tree t = (tree) data;
4687       tree vbase;
4688       tree offset;
4689       
4690       vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4691       if (vbase != binfo)
4692         {
4693           offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4694           propagate_binfo_offsets (binfo, offset, t);
4695         }
4696     }
4697
4698   return NULL_TREE;
4699 }
4700
4701 /* Set BINFO_OFFSET for all of the virtual bases for RLI->T.  Update
4702    TYPE_ALIGN and TYPE_SIZE for T.  OFFSETS gives the location of
4703    empty subobjects of T.  */
4704
4705 static void
4706 layout_virtual_bases (record_layout_info rli, splay_tree offsets)
4707 {
4708   tree vbases;
4709   tree t = rli->t;
4710   bool first_vbase = true;
4711   tree *next_field;
4712
4713   if (CLASSTYPE_N_BASECLASSES (t) == 0)
4714     return;
4715
4716   if (!abi_version_at_least(2))
4717     {
4718       /* In G++ 3.2, we incorrectly rounded the size before laying out
4719          the virtual bases.  */
4720       finish_record_layout (rli, /*free_p=*/false);
4721 #ifdef STRUCTURE_SIZE_BOUNDARY
4722       /* Packed structures don't need to have minimum size.  */
4723       if (! TYPE_PACKED (t))
4724         TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
4725 #endif
4726       rli->offset = TYPE_SIZE_UNIT (t);
4727       rli->bitpos = bitsize_zero_node;
4728       rli->record_align = TYPE_ALIGN (t);
4729     }
4730
4731   /* Find the last field.  The artificial fields created for virtual
4732      bases will go after the last extant field to date.  */
4733   next_field = &TYPE_FIELDS (t);
4734   while (*next_field)
4735     next_field = &TREE_CHAIN (*next_field);
4736
4737   /* Go through the virtual bases, allocating space for each virtual
4738      base that is not already a primary base class.  These are
4739      allocated in inheritance graph order.  */
4740   for (vbases = TYPE_BINFO (t);
4741        vbases; 
4742        vbases = TREE_CHAIN (vbases))
4743     {
4744       tree vbase;
4745
4746       if (!TREE_VIA_VIRTUAL (vbases))
4747         continue;
4748
4749       vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4750
4751       if (!BINFO_PRIMARY_P (vbase))
4752         {
4753           tree basetype = TREE_TYPE (vbase);
4754
4755           /* This virtual base is not a primary base of any class in the
4756              hierarchy, so we have to add space for it.  */
4757           next_field = build_base_field (rli, vbase,
4758                                          offsets, next_field);
4759
4760           /* If the first virtual base might have been placed at a
4761              lower address, had we started from CLASSTYPE_SIZE, rather
4762              than TYPE_SIZE, issue a warning.  There can be both false
4763              positives and false negatives from this warning in rare
4764              cases; to deal with all the possibilities would probably
4765              require performing both layout algorithms and comparing
4766              the results which is not particularly tractable.  */
4767           if (warn_abi
4768               && first_vbase
4769               && (tree_int_cst_lt 
4770                   (size_binop (CEIL_DIV_EXPR,
4771                                round_up (CLASSTYPE_SIZE (t),
4772                                          CLASSTYPE_ALIGN (basetype)),
4773                                bitsize_unit_node),
4774                    BINFO_OFFSET (vbase))))
4775             warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
4776                      basetype);
4777
4778           first_vbase = false;
4779         }
4780     }
4781
4782   /* Now, go through the TYPE_BINFO hierarchy, setting the
4783      BINFO_OFFSETs correctly for all non-primary copies of the virtual
4784      bases and their direct and indirect bases.  The ambiguity checks
4785      in lookup_base depend on the BINFO_OFFSETs being set
4786      correctly.  */
4787   dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4788 }
4789
4790 /* Returns the offset of the byte just past the end of the base class
4791    BINFO.  */
4792
4793 static tree
4794 end_of_base (tree binfo)
4795 {
4796   tree size;
4797
4798   if (is_empty_class (BINFO_TYPE (binfo)))
4799     /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4800        allocate some space for it. It cannot have virtual bases, so
4801        TYPE_SIZE_UNIT is fine.  */
4802     size = TYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4803   else
4804     size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
4805
4806   return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
4807 }
4808
4809 /* Returns the offset of the byte just past the end of the base class
4810    with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
4811    only non-virtual bases are included.  */
4812
4813 static tree
4814 end_of_class (tree t, int include_virtuals_p)
4815 {
4816   tree result = size_zero_node;
4817   tree binfo;
4818   tree offset;
4819   int i;
4820
4821   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4822     {
4823       binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4824
4825       if (!include_virtuals_p
4826           && TREE_VIA_VIRTUAL (binfo) 
4827           && !BINFO_PRIMARY_P (binfo))
4828         continue;
4829
4830       offset = end_of_base (binfo);
4831       if (INT_CST_LT_UNSIGNED (result, offset))
4832         result = offset;
4833     }
4834
4835   /* G++ 3.2 did not check indirect virtual bases.  */
4836   if (abi_version_at_least (2) && include_virtuals_p)
4837     for (binfo = CLASSTYPE_VBASECLASSES (t); 
4838          binfo; 
4839          binfo = TREE_CHAIN (binfo))
4840       {
4841         offset = end_of_base (TREE_VALUE (binfo));
4842         if (INT_CST_LT_UNSIGNED (result, offset))
4843           result = offset;
4844       }
4845
4846   return result;
4847 }
4848
4849 /* Warn about bases of T that are inaccessible because they are
4850    ambiguous.  For example:
4851
4852      struct S {};
4853      struct T : public S {};
4854      struct U : public S, public T {};
4855
4856    Here, `(S*) new U' is not allowed because there are two `S'
4857    subobjects of U.  */
4858
4859 static void
4860 warn_about_ambiguous_bases (tree t)
4861 {
4862   int i;
4863   tree vbases;
4864   tree basetype;
4865
4866   /* Check direct bases.  */
4867   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4868     {
4869       basetype = TYPE_BINFO_BASETYPE (t, i);
4870
4871       if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4872         warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4873                  basetype, t);
4874     }
4875
4876   /* Check for ambiguous virtual bases.  */
4877   if (extra_warnings)
4878     for (vbases = CLASSTYPE_VBASECLASSES (t); 
4879          vbases; 
4880          vbases = TREE_CHAIN (vbases))
4881       {
4882         basetype = BINFO_TYPE (TREE_VALUE (vbases));
4883         
4884         if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
4885           warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4886                    basetype, t);
4887       }
4888 }
4889
4890 /* Compare two INTEGER_CSTs K1 and K2.  */
4891
4892 static int
4893 splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
4894 {
4895   return tree_int_cst_compare ((tree) k1, (tree) k2);
4896 }
4897
4898 /* Increase the size indicated in RLI to account for empty classes
4899    that are "off the end" of the class.  */
4900
4901 static void
4902 include_empty_classes (record_layout_info rli)
4903 {
4904   tree eoc;
4905   tree rli_size;
4906
4907   /* It might be the case that we grew the class to allocate a
4908      zero-sized base class.  That won't be reflected in RLI, yet,
4909      because we are willing to overlay multiple bases at the same
4910      offset.  However, now we need to make sure that RLI is big enough
4911      to reflect the entire class.  */
4912   eoc = end_of_class (rli->t, 
4913                       CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
4914   rli_size = rli_size_unit_so_far (rli);
4915   if (TREE_CODE (rli_size) == INTEGER_CST
4916       && INT_CST_LT_UNSIGNED (rli_size, eoc))
4917     {
4918       rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
4919       rli->bitpos 
4920         = size_binop (PLUS_EXPR, 
4921                       rli->bitpos,
4922                       size_binop (MULT_EXPR,
4923                                   convert (bitsizetype,
4924                                            size_binop (MINUS_EXPR,
4925                                                        eoc, rli_size)),
4926                                   bitsize_int (BITS_PER_UNIT)));
4927       normalize_rli (rli);
4928     }
4929 }
4930
4931 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
4932    BINFO_OFFSETs for all of the base-classes.  Position the vtable
4933    pointer.  Accumulate declared virtual functions on VIRTUALS_P.  */
4934
4935 static void
4936 layout_class_type (tree t, tree *virtuals_p)
4937 {
4938   tree non_static_data_members;
4939   tree field;
4940   tree vptr;
4941   record_layout_info rli;
4942   /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4943      types that appear at that offset.  */
4944   splay_tree empty_base_offsets;
4945   /* True if the last field layed out was a bit-field.  */
4946   bool last_field_was_bitfield = false;
4947   /* The location at which the next field should be inserted.  */
4948   tree *next_field;
4949   /* T, as a base class.  */
4950   tree base_t;
4951
4952   /* Keep track of the first non-static data member.  */
4953   non_static_data_members = TYPE_FIELDS (t);
4954
4955   /* Start laying out the record.  */
4956   rli = start_record_layout (t);
4957
4958   /* If possible, we reuse the virtual function table pointer from one
4959      of our base classes.  */
4960   determine_primary_base (t);
4961
4962   /* Create a pointer to our virtual function table.  */
4963   vptr = create_vtable_ptr (t, virtuals_p);
4964
4965   /* The vptr is always the first thing in the class.  */
4966   if (vptr)
4967     {
4968       TREE_CHAIN (vptr) = TYPE_FIELDS (t);
4969       TYPE_FIELDS (t) = vptr;
4970       next_field = &TREE_CHAIN (vptr);
4971       place_field (rli, vptr);
4972     }
4973   else
4974     next_field = &TYPE_FIELDS (t);
4975
4976   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
4977   empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 
4978                                        NULL, NULL);
4979   build_base_fields (rli, empty_base_offsets, next_field);
4980   
4981   /* Layout the non-static data members.  */
4982   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4983     {
4984       tree type;
4985       tree padding;
4986       bool was_unnamed_p = false;
4987
4988       /* We still pass things that aren't non-static data members to
4989          the back-end, in case it wants to do something with them.  */
4990       if (TREE_CODE (field) != FIELD_DECL)
4991         {
4992           place_field (rli, field);
4993           /* If the static data member has incomplete type, keep track
4994              of it so that it can be completed later.  (The handling 
4995              of pending statics in finish_record_layout is
4996              insufficient; consider:
4997
4998                struct S1;
4999                struct S2 { static S1 s1; };
5000                
5001              At this point, finish_record_layout will be called, but
5002              S1 is still incomplete.)  */
5003           if (TREE_CODE (field) == VAR_DECL)
5004             maybe_register_incomplete_var (field);
5005           continue;
5006         }
5007
5008       type = TREE_TYPE (field);
5009
5010       /* If this field is a bit-field whose width is greater than its
5011          type, then there are some special rules for allocating
5012          it.  */
5013       if (DECL_C_BIT_FIELD (field)
5014           && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
5015         {
5016           integer_type_kind itk;
5017           tree integer_type;
5018           /* We must allocate the bits as if suitably aligned for the
5019              longest integer type that fits in this many bits.  type
5020              of the field.  Then, we are supposed to use the left over
5021              bits as additional padding.  */
5022           for (itk = itk_char; itk != itk_none; ++itk)
5023             if (INT_CST_LT (DECL_SIZE (field), 
5024                             TYPE_SIZE (integer_types[itk])))
5025               break;
5026
5027           /* ITK now indicates a type that is too large for the
5028              field.  We have to back up by one to find the largest
5029              type that fits.  */
5030           integer_type = integer_types[itk - 1];
5031
5032           if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
5033             /* In a union, the padding field must have the full width
5034                of the bit-field; all fields start at offset zero.  */
5035             padding = DECL_SIZE (field);
5036           else
5037             {
5038               if (warn_abi && TREE_CODE (t) == UNION_TYPE)
5039                 warning ("size assigned to `%T' may not be "
5040                          "ABI-compliant and may change in a future "
5041                          "version of GCC", 
5042                          t);
5043               padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
5044                                     TYPE_SIZE (integer_type));
5045             }
5046 #ifdef PCC_BITFIELD_TYPE_MATTERS
5047           /* An unnamed bitfield does not normally affect the
5048              alignment of the containing class on a target where
5049              PCC_BITFIELD_TYPE_MATTERS.  But, the C++ ABI does not
5050              make any exceptions for unnamed bitfields when the
5051              bitfields are longer than their types.  Therefore, we
5052              temporarily give the field a name.  */
5053           if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
5054             {
5055               was_unnamed_p = true;
5056               DECL_NAME (field) = make_anon_name ();
5057             }
5058 #endif
5059           DECL_SIZE (field) = TYPE_SIZE (integer_type);
5060           DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
5061           DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
5062         }
5063       else
5064         padding = NULL_TREE;
5065
5066       layout_nonempty_base_or_field (rli, field, NULL_TREE,
5067                                      empty_base_offsets);
5068       /* If the bit-field had no name originally, remove the name
5069          now.  */
5070       if (was_unnamed_p)
5071         DECL_NAME (field) = NULL_TREE;
5072
5073       /* Remember the location of any empty classes in FIELD.  */
5074       if (abi_version_at_least (2))
5075         record_subobject_offsets (TREE_TYPE (field), 
5076                                   byte_position(field),
5077                                   empty_base_offsets,
5078                                   /*vbases_p=*/1);
5079
5080       /* If a bit-field does not immediately follow another bit-field,
5081          and yet it starts in the middle of a byte, we have failed to
5082          comply with the ABI.  */
5083       if (warn_abi
5084           && DECL_C_BIT_FIELD (field) 
5085           && !last_field_was_bitfield
5086           && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
5087                                          DECL_FIELD_BIT_OFFSET (field),
5088                                          bitsize_unit_node)))
5089         cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC", 
5090                        field);
5091
5092       /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
5093          offset of the field.  */
5094       if (warn_abi 
5095           && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
5096                                   byte_position (field))
5097           && contains_empty_class_p (TREE_TYPE (field)))
5098         cp_warning_at ("`%D' contains empty classes which may cause base "
5099                        "classes to be placed at different locations in a "
5100                        "future version of GCC",
5101                        field);
5102
5103       /* If we needed additional padding after this field, add it
5104          now.  */
5105       if (padding)
5106         {
5107           tree padding_field;
5108
5109           padding_field = build_decl (FIELD_DECL, 
5110                                       NULL_TREE,
5111                                       char_type_node); 
5112           DECL_BIT_FIELD (padding_field) = 1;
5113           DECL_SIZE (padding_field) = padding;
5114           DECL_ALIGN (padding_field) = 1;
5115           DECL_USER_ALIGN (padding_field) = 0;
5116           layout_nonempty_base_or_field (rli, padding_field,
5117                                          NULL_TREE, 
5118                                          empty_base_offsets);
5119         }
5120
5121       last_field_was_bitfield = DECL_C_BIT_FIELD (field);
5122     }
5123
5124   if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
5125     {
5126       /* Make sure that we are on a byte boundary so that the size of
5127          the class without virtual bases will always be a round number
5128          of bytes.  */
5129       rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
5130       normalize_rli (rli);
5131     }
5132
5133   /* G++ 3.2 does not allow virtual bases to be overlaid with tail
5134      padding.  */
5135   if (!abi_version_at_least (2))
5136     include_empty_classes(rli);
5137
5138   /* Delete all zero-width bit-fields from the list of fields.  Now
5139      that the type is laid out they are no longer important.  */
5140   remove_zero_width_bit_fields (t);
5141
5142   /* Create the version of T used for virtual bases.  We do not use
5143      make_aggr_type for this version; this is an artificial type.  For
5144      a POD type, we just reuse T.  */
5145   if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
5146     {
5147       base_t = make_node (TREE_CODE (t));
5148       
5149       /* Set the size and alignment for the new type.  In G++ 3.2, all
5150          empty classes were considered to have size zero when used as
5151          base classes.  */
5152       if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
5153         {
5154           TYPE_SIZE (base_t) = bitsize_zero_node;
5155           TYPE_SIZE_UNIT (base_t) = size_zero_node;
5156           if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
5157             warning ("layout of classes derived from empty class `%T' "
5158                      "may change in a future version of GCC",
5159                      t);
5160         }
5161       else
5162         {
5163           TYPE_SIZE_UNIT (base_t) 
5164             = size_binop (MAX_EXPR,
5165                           rli_size_unit_so_far (rli),
5166                           end_of_class (t, /*include_virtuals_p=*/0));
5167           TYPE_SIZE (base_t) 
5168             = size_binop (MAX_EXPR,
5169                           rli_size_so_far (rli),
5170                           size_binop (MULT_EXPR,
5171                                       convert (bitsizetype,
5172                                                TYPE_SIZE_UNIT (base_t)),
5173                                       bitsize_int (BITS_PER_UNIT)));
5174         }
5175       TYPE_ALIGN (base_t) = rli->record_align;
5176       TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
5177
5178       /* Copy the fields from T.  */
5179       next_field = &TYPE_FIELDS (base_t);
5180       for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
5181         if (TREE_CODE (field) == FIELD_DECL)
5182           {
5183             *next_field = build_decl (FIELD_DECL,
5184                                       DECL_NAME (field), 
5185                                       TREE_TYPE (field));
5186             DECL_CONTEXT (*next_field) = base_t;
5187             DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
5188             DECL_FIELD_BIT_OFFSET (*next_field)
5189               = DECL_FIELD_BIT_OFFSET (field);
5190             next_field = &TREE_CHAIN (*next_field);
5191           }
5192
5193       /* Record the base version of the type.  */
5194       CLASSTYPE_AS_BASE (t) = base_t;
5195       TYPE_CONTEXT (base_t) = t;
5196     }
5197   else
5198     CLASSTYPE_AS_BASE (t) = t;
5199
5200   /* Every empty class contains an empty class.  */
5201   if (CLASSTYPE_EMPTY_P (t))
5202     CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
5203
5204   /* Set the TYPE_DECL for this type to contain the right
5205      value for DECL_OFFSET, so that we can use it as part
5206      of a COMPONENT_REF for multiple inheritance.  */
5207   layout_decl (TYPE_MAIN_DECL (t), 0);
5208
5209   /* Now fix up any virtual base class types that we left lying
5210      around.  We must get these done before we try to lay out the
5211      virtual function table.  As a side-effect, this will remove the
5212      base subobject fields.  */
5213   layout_virtual_bases (rli, empty_base_offsets);
5214
5215   /* Make sure that empty classes are reflected in RLI at this 
5216      point.  */
5217   include_empty_classes(rli);
5218
5219   /* Make sure not to create any structures with zero size.  */
5220   if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
5221     place_field (rli, 
5222                  build_decl (FIELD_DECL, NULL_TREE, char_type_node));
5223
5224   /* Let the back-end lay out the type.  */
5225   finish_record_layout (rli, /*free_p=*/true);
5226
5227   /* Warn about bases that can't be talked about due to ambiguity.  */
5228   warn_about_ambiguous_bases (t);
5229
5230   /* Clean up.  */
5231   splay_tree_delete (empty_base_offsets);
5232 }
5233
5234 /* Returns the virtual function with which the vtable for TYPE is
5235    emitted, or NULL_TREE if that heuristic is not applicable to TYPE.  */
5236
5237 static tree
5238 key_method (tree type)
5239 {
5240   tree method;
5241
5242   if (TYPE_FOR_JAVA (type)
5243       || processing_template_decl
5244       || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
5245       || CLASSTYPE_INTERFACE_KNOWN (type))
5246     return NULL_TREE;
5247
5248   for (method = TYPE_METHODS (type); method != NULL_TREE;
5249        method = TREE_CHAIN (method))
5250     if (DECL_VINDEX (method) != NULL_TREE
5251         && ! DECL_DECLARED_INLINE_P (method)
5252         && ! DECL_PURE_VIRTUAL_P (method))
5253       return method;
5254
5255   return NULL_TREE;
5256 }
5257
5258 /* Perform processing required when the definition of T (a class type)
5259    is complete.  */
5260
5261 void
5262 finish_struct_1 (tree t)
5263 {
5264   tree x;
5265   /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL.  */
5266   tree virtuals = NULL_TREE;
5267   int n_fields = 0;
5268   tree vfield;
5269
5270   if (COMPLETE_TYPE_P (t))
5271     {
5272       if (IS_AGGR_TYPE (t))
5273         error ("redefinition of `%#T'", t);
5274       else
5275         abort ();
5276       popclass ();
5277       return;
5278     }
5279
5280   /* If this type was previously laid out as a forward reference,
5281      make sure we lay it out again.  */
5282   TYPE_SIZE (t) = NULL_TREE;
5283   CLASSTYPE_GOT_SEMICOLON (t) = 0;
5284   CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5285
5286   fixup_inline_methods (t);
5287   
5288   /* Make assumptions about the class; we'll reset the flags if
5289      necessary.  */
5290   CLASSTYPE_EMPTY_P (t) = 1;
5291   CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
5292   CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
5293
5294   /* Do end-of-class semantic processing: checking the validity of the
5295      bases and members and add implicitly generated methods.  */
5296   check_bases_and_members (t);
5297
5298   /* Find the key method */
5299     if (TYPE_CONTAINS_VPTR_P (t))
5300     {
5301       CLASSTYPE_KEY_METHOD (t) = key_method (t);
5302
5303       /* If a polymorphic class has no key method, we may emit the vtable
5304          in every translation unit where the class definition appears. */
5305       if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
5306         keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
5307     }
5308
5309   /* Layout the class itself.  */
5310   layout_class_type (t, &virtuals);
5311
5312   /* Make sure that we get our own copy of the vfield FIELD_DECL.  */
5313   vfield = TYPE_VFIELD (t);
5314   if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5315     {
5316       tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5317
5318       my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5319                                        BINFO_TYPE (primary)),
5320                           20010726);
5321       /* The vtable better be at the start.  */
5322       my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5323                           20010726);
5324       my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5325                           20010726);
5326       
5327       vfield = copy_decl (vfield);
5328       DECL_FIELD_CONTEXT (vfield) = t;
5329       TYPE_VFIELD (t) = vfield;
5330     }
5331   else
5332     my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5333
5334   virtuals = modify_all_vtables (t, nreverse (virtuals));
5335
5336   /* If we created a new vtbl pointer for this class, add it to the
5337      list.  */
5338   if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5339     CLASSTYPE_VFIELDS (t) 
5340       = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5341
5342   /* If necessary, create the primary vtable for this class.  */
5343   if (virtuals || TYPE_CONTAINS_VPTR_P (t))
5344     {
5345       /* We must enter these virtuals into the table.  */
5346       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5347         build_primary_vtable (NULL_TREE, t);
5348       else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5349         /* Here we know enough to change the type of our virtual
5350            function table, but we will wait until later this function.  */
5351         build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5352     }
5353
5354   if (TYPE_CONTAINS_VPTR_P (t))
5355     {
5356       int vindex;
5357       tree fn;
5358
5359       if (TYPE_BINFO_VTABLE (t))
5360         my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5361                             20000116);
5362       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5363         my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5364                             20000116);
5365
5366       /* Add entries for virtual functions introduced by this class.  */
5367       TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
5368
5369       /* Set DECL_VINDEX for all functions declared in this class.  */
5370       for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t)); 
5371            fn; 
5372            fn = TREE_CHAIN (fn), 
5373              vindex += (TARGET_VTABLE_USES_DESCRIPTORS
5374                         ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
5375         {
5376           tree fndecl = BV_FN (fn);
5377
5378           if (DECL_THUNK_P (fndecl))
5379             /* A thunk. We should never be calling this entry directly
5380                from this vtable -- we'd use the entry for the non
5381                thunk base function.  */
5382             DECL_VINDEX (fndecl) = NULL_TREE;
5383           else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
5384             DECL_VINDEX (fndecl) = build_shared_int_cst (vindex);
5385         }
5386     }
5387
5388   finish_struct_bits (t);
5389
5390   /* Complete the rtl for any static member objects of the type we're
5391      working on.  */
5392   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5393     if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5394         && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
5395       DECL_MODE (x) = TYPE_MODE (t);
5396
5397   /* Done with FIELDS...now decide whether to sort these for
5398      faster lookups later.
5399
5400      We use a small number because most searches fail (succeeding
5401      ultimately as the search bores through the inheritance
5402      hierarchy), and we want this failure to occur quickly.  */
5403
5404   n_fields = count_fields (TYPE_FIELDS (t));
5405   if (n_fields > 7)
5406     {
5407       tree field_vec = make_tree_vec (n_fields);
5408       add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5409       qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5410              field_decl_cmp);
5411       if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5412         retrofit_lang_decl (TYPE_MAIN_DECL (t));
5413       DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5414     }
5415
5416   if (TYPE_HAS_CONSTRUCTOR (t))
5417     {
5418       tree vfields = CLASSTYPE_VFIELDS (t);
5419
5420       for (vfields = CLASSTYPE_VFIELDS (t);
5421            vfields; vfields = TREE_CHAIN (vfields))
5422         /* Mark the fact that constructor for T could affect anybody
5423            inheriting from T who wants to initialize vtables for
5424            VFIELDS's type.  */
5425         if (VF_BINFO_VALUE (vfields))
5426           TREE_ADDRESSABLE (vfields) = 1;
5427     }
5428
5429   /* Make the rtl for any new vtables we have created, and unmark
5430      the base types we marked.  */
5431   finish_vtbls (t);
5432   
5433   /* Build the VTT for T.  */
5434   build_vtt (t);
5435
5436   if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5437       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5438     warning ("`%#T' has virtual functions but non-virtual destructor", t);
5439
5440   complete_vars (t);
5441
5442   if (warn_overloaded_virtual)
5443     warn_hidden (t);
5444
5445   maybe_suppress_debug_info (t);
5446
5447   dump_class_hierarchy (t);
5448   
5449   /* Finish debugging output for this type.  */
5450   rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5451 }
5452
5453 /* When T was built up, the member declarations were added in reverse
5454    order.  Rearrange them to declaration order.  */
5455
5456 void
5457 unreverse_member_declarations (tree t)
5458 {
5459   tree next;
5460   tree prev;
5461   tree x;
5462
5463   /* The following lists are all in reverse order.  Put them in
5464      declaration order now.  */
5465   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5466   CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5467   CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
5468
5469   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5470      reverse order, so we can't just use nreverse.  */
5471   prev = NULL_TREE;
5472   for (x = TYPE_FIELDS (t); 
5473        x && TREE_CODE (x) != TYPE_DECL; 
5474        x = next)
5475     {
5476       next = TREE_CHAIN (x);
5477       TREE_CHAIN (x) = prev;
5478       prev = x;
5479     }
5480   if (prev)
5481     {
5482       TREE_CHAIN (TYPE_FIELDS (t)) = x;
5483       if (prev)
5484         TYPE_FIELDS (t) = prev;
5485     }
5486 }
5487
5488 tree
5489 finish_struct (tree t, tree attributes)
5490 {
5491   const char *saved_filename = input_filename;
5492   int saved_lineno = lineno;
5493
5494   /* Now that we've got all the field declarations, reverse everything
5495      as necessary.  */
5496   unreverse_member_declarations (t);
5497
5498   cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5499
5500   /* Nadger the current location so that diagnostics point to the start of
5501      the struct, not the end.  */
5502   input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5503   lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5504
5505   if (processing_template_decl)
5506     {
5507       finish_struct_methods (t);
5508       TYPE_SIZE (t) = bitsize_zero_node;
5509     }
5510   else
5511     finish_struct_1 (t);
5512
5513   input_filename = saved_filename;
5514   lineno = saved_lineno;
5515
5516   TYPE_BEING_DEFINED (t) = 0;
5517
5518   if (current_class_type)
5519     popclass ();
5520   else
5521     error ("trying to finish struct, but kicked out due to previous parse errors");
5522
5523   if (processing_template_decl && at_function_scope_p ())
5524     add_stmt (build_min (TAG_DEFN, t));
5525
5526   return t;
5527 }
5528 \f
5529 /* Return the dynamic type of INSTANCE, if known.
5530    Used to determine whether the virtual function table is needed
5531    or not.
5532
5533    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5534    of our knowledge of its type.  *NONNULL should be initialized
5535    before this function is called.  */
5536
5537 static tree
5538 fixed_type_or_null (tree instance, int* nonnull, int* cdtorp)
5539 {
5540   switch (TREE_CODE (instance))
5541     {
5542     case INDIRECT_REF:
5543       if (POINTER_TYPE_P (TREE_TYPE (instance)))
5544         return NULL_TREE;
5545       else
5546         return fixed_type_or_null (TREE_OPERAND (instance, 0),
5547                                    nonnull, cdtorp);
5548
5549     case CALL_EXPR:
5550       /* This is a call to a constructor, hence it's never zero.  */
5551       if (TREE_HAS_CONSTRUCTOR (instance))
5552         {
5553           if (nonnull)
5554             *nonnull = 1;
5555           return TREE_TYPE (instance);
5556         }
5557       return NULL_TREE;
5558
5559     case SAVE_EXPR:
5560       /* This is a call to a constructor, hence it's never zero.  */
5561       if (TREE_HAS_CONSTRUCTOR (instance))
5562         {
5563           if (nonnull)
5564             *nonnull = 1;
5565           return TREE_TYPE (instance);
5566         }
5567       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5568
5569     case RTL_EXPR:
5570       return NULL_TREE;
5571
5572     case PLUS_EXPR:
5573     case MINUS_EXPR:
5574       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5575         return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5576       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5577         /* Propagate nonnull.  */
5578         return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5579       return NULL_TREE;
5580
5581     case NOP_EXPR:
5582     case CONVERT_EXPR:
5583       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5584
5585     case ADDR_EXPR:
5586       if (nonnull)
5587         *nonnull = 1;
5588       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5589
5590     case COMPONENT_REF:
5591       return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5592
5593     case VAR_DECL:
5594     case FIELD_DECL:
5595       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5596           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5597         {
5598           if (nonnull)
5599             *nonnull = 1;
5600           return TREE_TYPE (TREE_TYPE (instance));
5601         }
5602       /* fall through...  */
5603     case TARGET_EXPR:
5604     case PARM_DECL:
5605     case RESULT_DECL:
5606       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5607         {
5608           if (nonnull)
5609             *nonnull = 1;
5610           return TREE_TYPE (instance);
5611         }
5612       else if (instance == current_class_ptr)
5613         {
5614           if (nonnull)
5615             *nonnull = 1;
5616         
5617           /* if we're in a ctor or dtor, we know our type.  */
5618           if (DECL_LANG_SPECIFIC (current_function_decl)
5619               && (DECL_CONSTRUCTOR_P (current_function_decl)
5620                   || DECL_DESTRUCTOR_P (current_function_decl)))
5621             {
5622               if (cdtorp)
5623                 *cdtorp = 1;
5624               return TREE_TYPE (TREE_TYPE (instance));
5625             }
5626         }
5627       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5628         {
5629           /* Reference variables should be references to objects.  */
5630           if (nonnull)
5631             *nonnull = 1;
5632
5633           if (TREE_CODE (instance) == VAR_DECL
5634               && DECL_INITIAL (instance))
5635             return fixed_type_or_null (DECL_INITIAL (instance),
5636                                        nonnull, cdtorp);
5637         }
5638       return NULL_TREE;
5639
5640     default:
5641       return NULL_TREE;
5642     }
5643 }
5644
5645 /* Return nonzero if the dynamic type of INSTANCE is known, and
5646    equivalent to the static type.  We also handle the case where
5647    INSTANCE is really a pointer. Return negative if this is a
5648    ctor/dtor. There the dynamic type is known, but this might not be
5649    the most derived base of the original object, and hence virtual
5650    bases may not be layed out according to this type.
5651
5652    Used to determine whether the virtual function table is needed
5653    or not.
5654
5655    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5656    of our knowledge of its type.  *NONNULL should be initialized
5657    before this function is called.  */
5658
5659 int
5660 resolves_to_fixed_type_p (tree instance, int* nonnull)
5661 {
5662   tree t = TREE_TYPE (instance);
5663   int cdtorp = 0;
5664   
5665   tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5666   if (fixed == NULL_TREE)
5667     return 0;
5668   if (POINTER_TYPE_P (t))
5669     t = TREE_TYPE (t);
5670   if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5671     return 0;
5672   return cdtorp ? -1 : 1;
5673 }
5674
5675 \f
5676 void
5677 init_class_processing (void)
5678 {
5679   current_class_depth = 0;
5680   current_class_stack_size = 10;
5681   current_class_stack 
5682     = (class_stack_node_t) xmalloc (current_class_stack_size 
5683                                     * sizeof (struct class_stack_node));
5684   VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5685
5686   access_default_node = build_int_2 (0, 0);
5687   access_public_node = build_int_2 (ak_public, 0);
5688   access_protected_node = build_int_2 (ak_protected, 0);
5689   access_private_node = build_int_2 (ak_private, 0);
5690   access_default_virtual_node = build_int_2 (4, 0);
5691   access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5692   access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5693   access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5694
5695   ridpointers[(int) RID_PUBLIC] = access_public_node;
5696   ridpointers[(int) RID_PRIVATE] = access_private_node;
5697   ridpointers[(int) RID_PROTECTED] = access_protected_node;
5698 }
5699
5700 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
5701    appropriate for TYPE.
5702
5703    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5704    which can be seen locally to the class.  They are shadowed by
5705    any subsequent local declaration (including parameter names).
5706
5707    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5708    which have static meaning (i.e., static members, static
5709    member functions, enum declarations, etc).
5710
5711    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5712    which can be seen locally to the class (as in 1), but
5713    know that we are doing this for declaration purposes
5714    (i.e. friend foo::bar (int)).
5715
5716    So that we may avoid calls to lookup_name, we cache the _TYPE
5717    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5718
5719    For multiple inheritance, we perform a two-pass depth-first search
5720    of the type lattice.  The first pass performs a pre-order search,
5721    marking types after the type has had its fields installed in
5722    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
5723    unmarks the marked types.  If a field or member function name
5724    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5725    that name becomes `error_mark_node'.  */
5726
5727 void
5728 pushclass (tree type, int modify)
5729 {
5730   type = TYPE_MAIN_VARIANT (type);
5731
5732   /* Make sure there is enough room for the new entry on the stack.  */
5733   if (current_class_depth + 1 >= current_class_stack_size) 
5734     {
5735       current_class_stack_size *= 2;
5736       current_class_stack
5737         = (class_stack_node_t) xrealloc (current_class_stack,
5738                                          current_class_stack_size
5739                                          * sizeof (struct class_stack_node));
5740     }
5741
5742   /* Insert a new entry on the class stack.  */
5743   current_class_stack[current_class_depth].name = current_class_name;
5744   current_class_stack[current_class_depth].type = current_class_type;
5745   current_class_stack[current_class_depth].access = current_access_specifier;
5746   current_class_stack[current_class_depth].names_used = 0;
5747   current_class_depth++;
5748
5749   /* Now set up the new type.  */
5750   current_class_name = TYPE_NAME (type);
5751   if (TREE_CODE (current_class_name) == TYPE_DECL)
5752     current_class_name = DECL_NAME (current_class_name);
5753   current_class_type = type;
5754
5755   /* By default, things in classes are private, while things in
5756      structures or unions are public.  */
5757   current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
5758                               ? access_private_node 
5759                               : access_public_node);
5760
5761   if (previous_class_type != NULL_TREE
5762       && (type != previous_class_type 
5763           || !COMPLETE_TYPE_P (previous_class_type))
5764       && current_class_depth == 1)
5765     {
5766       /* Forcibly remove any old class remnants.  */
5767       invalidate_class_lookup_cache ();
5768     }
5769
5770   /* If we're about to enter a nested class, clear
5771      IDENTIFIER_CLASS_VALUE for the enclosing classes.  */
5772   if (modify && current_class_depth > 1)
5773     clear_identifier_class_values ();
5774
5775   pushlevel_class ();
5776
5777   if (modify)
5778     {
5779       if (type != previous_class_type || current_class_depth > 1)
5780         push_class_decls (type);
5781       else
5782         {
5783           tree item;
5784
5785           /* We are re-entering the same class we just left, so we
5786              don't have to search the whole inheritance matrix to find
5787              all the decls to bind again.  Instead, we install the
5788              cached class_shadowed list, and walk through it binding
5789              names and setting up IDENTIFIER_TYPE_VALUEs.  */
5790           set_class_shadows (previous_class_values);
5791           for (item = previous_class_values; item; item = TREE_CHAIN (item))
5792             {
5793               tree id = TREE_PURPOSE (item);
5794               tree decl = TREE_TYPE (item);
5795
5796               push_class_binding (id, decl);
5797               if (TREE_CODE (decl) == TYPE_DECL)
5798                 set_identifier_type_value (id, TREE_TYPE (decl));
5799             }
5800           unuse_fields (type);
5801         }
5802
5803       storetags (CLASSTYPE_TAGS (type));
5804     }
5805 }
5806
5807 /* When we exit a toplevel class scope, we save the
5808    IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5809    reenter the class.  Here, we've entered some other class, so we
5810    must invalidate our cache.  */
5811
5812 void
5813 invalidate_class_lookup_cache (void)
5814 {
5815   tree t;
5816   
5817   /* The IDENTIFIER_CLASS_VALUEs are no longer valid.  */
5818   for (t = previous_class_values; t; t = TREE_CHAIN (t))
5819     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5820
5821   previous_class_values = NULL_TREE;
5822   previous_class_type = NULL_TREE;
5823 }
5824  
5825 /* Get out of the current class scope. If we were in a class scope
5826    previously, that is the one popped to.  */
5827
5828 void
5829 popclass (void)
5830 {
5831   poplevel_class ();
5832   pop_class_decls ();
5833
5834   current_class_depth--;
5835   current_class_name = current_class_stack[current_class_depth].name;
5836   current_class_type = current_class_stack[current_class_depth].type;
5837   current_access_specifier = current_class_stack[current_class_depth].access;
5838   if (current_class_stack[current_class_depth].names_used)
5839     splay_tree_delete (current_class_stack[current_class_depth].names_used);
5840 }
5841
5842 /* Returns 1 if current_class_type is either T or a nested type of T.
5843    We start looking from 1 because entry 0 is from global scope, and has
5844    no type.  */
5845
5846 int
5847 currently_open_class (tree t)
5848 {
5849   int i;
5850   if (t == current_class_type)
5851     return 1;
5852   for (i = 1; i < current_class_depth; ++i)
5853     if (current_class_stack [i].type == t)
5854       return 1;
5855   return 0;
5856 }
5857
5858 /* If either current_class_type or one of its enclosing classes are derived
5859    from T, return the appropriate type.  Used to determine how we found
5860    something via unqualified lookup.  */
5861
5862 tree
5863 currently_open_derived_class (tree t)
5864 {
5865   int i;
5866
5867   /* The bases of a dependent type are unknown. */
5868   if (dependent_type_p (t))
5869     return NULL_TREE;
5870
5871   if (DERIVED_FROM_P (t, current_class_type))
5872     return current_class_type;
5873
5874   for (i = current_class_depth - 1; i > 0; --i)
5875     if (DERIVED_FROM_P (t, current_class_stack[i].type))
5876       return current_class_stack[i].type;
5877
5878   return NULL_TREE;
5879 }
5880
5881 /* When entering a class scope, all enclosing class scopes' names with
5882    static meaning (static variables, static functions, types and enumerators)
5883    have to be visible.  This recursive function calls pushclass for all
5884    enclosing class contexts until global or a local scope is reached.
5885    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5886    formal of the same name.  */
5887
5888 void
5889 push_nested_class (tree type, int modify)
5890 {
5891   tree context;
5892
5893   /* A namespace might be passed in error cases, like A::B:C.  */
5894   if (type == NULL_TREE 
5895       || type == error_mark_node 
5896       || TREE_CODE (type) == NAMESPACE_DECL
5897       || ! IS_AGGR_TYPE (type)
5898       || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5899       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5900     return;
5901   
5902   context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5903
5904   if (context && CLASS_TYPE_P (context))
5905     push_nested_class (context, 2);
5906   pushclass (type, modify);
5907 }
5908
5909 /* Undoes a push_nested_class call.  */
5910
5911 void
5912 pop_nested_class (void)
5913 {
5914   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5915
5916   popclass ();
5917   if (context && CLASS_TYPE_P (context))
5918     pop_nested_class ();
5919 }
5920
5921 /* Returns the number of extern "LANG" blocks we are nested within.  */
5922
5923 int
5924 current_lang_depth (void)
5925 {
5926   return VARRAY_ACTIVE_SIZE (current_lang_base);
5927 }
5928
5929 /* Set global variables CURRENT_LANG_NAME to appropriate value
5930    so that behavior of name-mangling machinery is correct.  */
5931
5932 void
5933 push_lang_context (tree name)
5934 {
5935   VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5936
5937   if (name == lang_name_cplusplus)
5938     {
5939       current_lang_name = name;
5940     }
5941   else if (name == lang_name_java)
5942     {
5943       current_lang_name = name;
5944       /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5945          (See record_builtin_java_type in decl.c.)  However, that causes
5946          incorrect debug entries if these types are actually used.
5947          So we re-enable debug output after extern "Java".  */
5948       DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5949       DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5950       DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5951       DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5952       DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5953       DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5954       DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5955       DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5956     }
5957   else if (name == lang_name_c)
5958     {
5959       current_lang_name = name;
5960     }
5961   else
5962     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5963 }
5964   
5965 /* Get out of the current language scope.  */
5966
5967 void
5968 pop_lang_context (void)
5969 {
5970   current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5971   VARRAY_POP (current_lang_base);
5972 }
5973 \f
5974 /* Type instantiation routines.  */
5975
5976 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5977    matches the TARGET_TYPE.  If there is no satisfactory match, return
5978    error_mark_node, and issue an error message if COMPLAIN is
5979    nonzero.  Permit pointers to member function if PTRMEM is nonzero.
5980    If TEMPLATE_ONLY, the name of the overloaded function
5981    was a template-id, and EXPLICIT_TARGS are the explicitly provided
5982    template arguments.  */
5983
5984 static tree
5985 resolve_address_of_overloaded_function (tree target_type, 
5986                                         tree overload,
5987                                         int complain,
5988                                         int ptrmem,
5989                                         int template_only,
5990                                         tree explicit_targs)
5991 {
5992   /* Here's what the standard says:
5993      
5994        [over.over]
5995
5996        If the name is a function template, template argument deduction
5997        is done, and if the argument deduction succeeds, the deduced
5998        arguments are used to generate a single template function, which
5999        is added to the set of overloaded functions considered.
6000
6001        Non-member functions and static member functions match targets of
6002        type "pointer-to-function" or "reference-to-function."  Nonstatic
6003        member functions match targets of type "pointer-to-member
6004        function;" the function type of the pointer to member is used to
6005        select the member function from the set of overloaded member
6006        functions.  If a nonstatic member function is selected, the
6007        reference to the overloaded function name is required to have the
6008        form of a pointer to member as described in 5.3.1.
6009
6010        If more than one function is selected, any template functions in
6011        the set are eliminated if the set also contains a non-template
6012        function, and any given template function is eliminated if the
6013        set contains a second template function that is more specialized
6014        than the first according to the partial ordering rules 14.5.5.2.
6015        After such eliminations, if any, there shall remain exactly one
6016        selected function.  */
6017
6018   int is_ptrmem = 0;
6019   int is_reference = 0;
6020   /* We store the matches in a TREE_LIST rooted here.  The functions
6021      are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
6022      interoperability with most_specialized_instantiation.  */
6023   tree matches = NULL_TREE;
6024   tree fn;
6025
6026   /* By the time we get here, we should be seeing only real
6027      pointer-to-member types, not the internal POINTER_TYPE to
6028      METHOD_TYPE representation.  */
6029   my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
6030                         && (TREE_CODE (TREE_TYPE (target_type)) 
6031                             == METHOD_TYPE)), 0);
6032
6033   if (TREE_CODE (overload) == COMPONENT_REF)
6034     overload = TREE_OPERAND (overload, 1);
6035
6036   /* Check that the TARGET_TYPE is reasonable.  */
6037   if (TYPE_PTRFN_P (target_type))
6038     /* This is OK.  */;
6039   else if (TYPE_PTRMEMFUNC_P (target_type))
6040     /* This is OK, too.  */
6041     is_ptrmem = 1;
6042   else if (TREE_CODE (target_type) == FUNCTION_TYPE)
6043     {
6044       /* This is OK, too.  This comes from a conversion to reference
6045          type.  */
6046       target_type = build_reference_type (target_type);
6047       is_reference = 1;
6048     }
6049   else 
6050     {
6051       if (complain)
6052         error ("\
6053 cannot resolve overloaded function `%D' based on conversion to type `%T'", 
6054                   DECL_NAME (OVL_FUNCTION (overload)), target_type);
6055       return error_mark_node;
6056     }
6057   
6058   /* If we can find a non-template function that matches, we can just
6059      use it.  There's no point in generating template instantiations
6060      if we're just going to throw them out anyhow.  But, of course, we
6061      can only do this when we don't *need* a template function.  */
6062   if (!template_only)
6063     {
6064       tree fns;
6065
6066       for (fns = overload; fns; fns = OVL_NEXT (fns))
6067         {
6068           tree fn = OVL_CURRENT (fns);
6069           tree fntype;
6070
6071           if (TREE_CODE (fn) == TEMPLATE_DECL)
6072             /* We're not looking for templates just yet.  */
6073             continue;
6074
6075           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6076               != is_ptrmem)
6077             /* We're looking for a non-static member, and this isn't
6078                one, or vice versa.  */
6079             continue;
6080         
6081           /* See if there's a match.  */
6082           fntype = TREE_TYPE (fn);
6083           if (is_ptrmem)
6084             fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
6085           else if (!is_reference)
6086             fntype = build_pointer_type (fntype);
6087
6088           if (can_convert_arg (target_type, fntype, fn))
6089             matches = tree_cons (fn, NULL_TREE, matches);
6090         }
6091     }
6092
6093   /* Now, if we've already got a match (or matches), there's no need
6094      to proceed to the template functions.  But, if we don't have a
6095      match we need to look at them, too.  */
6096   if (!matches) 
6097     {
6098       tree target_fn_type;
6099       tree target_arg_types;
6100       tree target_ret_type;
6101       tree fns;
6102
6103       if (is_ptrmem)
6104         target_fn_type
6105           = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
6106       else
6107         target_fn_type = TREE_TYPE (target_type);
6108       target_arg_types = TYPE_ARG_TYPES (target_fn_type);
6109       target_ret_type = TREE_TYPE (target_fn_type);
6110
6111       /* Never do unification on the 'this' parameter.  */
6112       if (TREE_CODE (target_fn_type) == METHOD_TYPE)
6113         target_arg_types = TREE_CHAIN (target_arg_types);
6114           
6115       for (fns = overload; fns; fns = OVL_NEXT (fns))
6116         {
6117           tree fn = OVL_CURRENT (fns);
6118           tree instantiation;
6119           tree instantiation_type;
6120           tree targs;
6121
6122           if (TREE_CODE (fn) != TEMPLATE_DECL)
6123             /* We're only looking for templates.  */
6124             continue;
6125
6126           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
6127               != is_ptrmem)
6128             /* We're not looking for a non-static member, and this is
6129                one, or vice versa.  */
6130             continue;
6131
6132           /* Try to do argument deduction.  */
6133           targs = make_tree_vec (DECL_NTPARMS (fn));
6134           if (fn_type_unification (fn, explicit_targs, targs,
6135                                    target_arg_types, target_ret_type,
6136                                    DEDUCE_EXACT, -1) != 0)
6137             /* Argument deduction failed.  */
6138             continue;
6139
6140           /* Instantiate the template.  */
6141           instantiation = instantiate_template (fn, targs);
6142           if (instantiation == error_mark_node)
6143             /* Instantiation failed.  */
6144             continue;
6145
6146           /* See if there's a match.  */
6147           instantiation_type = TREE_TYPE (instantiation);
6148           if (is_ptrmem)
6149             instantiation_type = 
6150               build_ptrmemfunc_type (build_pointer_type (instantiation_type));
6151           else if (!is_reference)
6152             instantiation_type = build_pointer_type (instantiation_type);
6153           if (can_convert_arg (target_type, instantiation_type, instantiation))
6154             matches = tree_cons (instantiation, fn, matches);
6155         }
6156
6157       /* Now, remove all but the most specialized of the matches.  */
6158       if (matches)
6159         {
6160           tree match = most_specialized_instantiation (matches);
6161
6162           if (match != error_mark_node)
6163             matches = tree_cons (match, NULL_TREE, NULL_TREE);
6164         }
6165     }
6166
6167   /* Now we should have exactly one function in MATCHES.  */
6168   if (matches == NULL_TREE)
6169     {
6170       /* There were *no* matches.  */
6171       if (complain)
6172         {
6173           error ("no matches converting function `%D' to type `%#T'", 
6174                     DECL_NAME (OVL_FUNCTION (overload)),
6175                     target_type);
6176
6177           /* print_candidates expects a chain with the functions in
6178              TREE_VALUE slots, so we cons one up here (we're losing anyway,
6179              so why be clever?).  */
6180           for (; overload; overload = OVL_NEXT (overload))
6181             matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6182                                  matches);
6183           
6184           print_candidates (matches);
6185         }
6186       return error_mark_node;
6187     }
6188   else if (TREE_CHAIN (matches))
6189     {
6190       /* There were too many matches.  */
6191
6192       if (complain)
6193         {
6194           tree match;
6195
6196           error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
6197                     DECL_NAME (OVL_FUNCTION (overload)),
6198                     target_type);
6199
6200           /* Since print_candidates expects the functions in the
6201              TREE_VALUE slot, we flip them here.  */
6202           for (match = matches; match; match = TREE_CHAIN (match))
6203             TREE_VALUE (match) = TREE_PURPOSE (match);
6204
6205           print_candidates (matches);
6206         }
6207       
6208       return error_mark_node;
6209     }
6210
6211   /* Good, exactly one match.  Now, convert it to the correct type.  */
6212   fn = TREE_PURPOSE (matches);
6213
6214   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6215       && !ptrmem && !flag_ms_extensions)
6216     {
6217       static int explained;
6218       
6219       if (!complain)
6220         return error_mark_node;
6221
6222       pedwarn ("assuming pointer to member `%D'", fn);
6223       if (!explained)
6224         {
6225           pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6226           explained = 1;
6227         }
6228     }
6229   mark_used (fn);
6230
6231   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6232     return build_unary_op (ADDR_EXPR, fn, 0);
6233   else
6234     {
6235       /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
6236          will mark the function as addressed, but here we must do it
6237          explicitly.  */
6238       cxx_mark_addressable (fn);
6239
6240       return fn;
6241     }
6242 }
6243
6244 /* This function will instantiate the type of the expression given in
6245    RHS to match the type of LHSTYPE.  If errors exist, then return
6246    error_mark_node. FLAGS is a bit mask.  If ITF_COMPLAIN is set, then
6247    we complain on errors.  If we are not complaining, never modify rhs,
6248    as overload resolution wants to try many possible instantiations, in
6249    the hope that at least one will work.
6250    
6251    For non-recursive calls, LHSTYPE should be a function, pointer to
6252    function, or a pointer to member function.  */
6253
6254 tree
6255 instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
6256 {
6257   int complain = (flags & tf_error);
6258   int strict = (flags & tf_no_attributes)
6259                ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6260   int allow_ptrmem = flags & tf_ptrmem_ok;
6261   
6262   flags &= ~tf_ptrmem_ok;
6263   
6264   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6265     {
6266       if (complain)
6267         error ("not enough type information");
6268       return error_mark_node;
6269     }
6270
6271   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6272     {
6273       if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6274         return rhs;
6275       if (flag_ms_extensions 
6276           && TYPE_PTRMEMFUNC_P (lhstype)
6277           && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
6278         /* Microsoft allows `A::f' to be resolved to a
6279            pointer-to-member.  */
6280         ;
6281       else
6282         {
6283           if (complain)
6284             error ("argument of type `%T' does not match `%T'",
6285                    TREE_TYPE (rhs), lhstype);
6286           return error_mark_node;
6287         }
6288     }
6289
6290   if (TREE_CODE (rhs) == BASELINK)
6291     rhs = BASELINK_FUNCTIONS (rhs);
6292
6293   /* We don't overwrite rhs if it is an overloaded function.
6294      Copying it would destroy the tree link.  */
6295   if (TREE_CODE (rhs) != OVERLOAD)
6296     rhs = copy_node (rhs);
6297
6298   /* This should really only be used when attempting to distinguish
6299      what sort of a pointer to function we have.  For now, any
6300      arithmetic operation which is not supported on pointers
6301      is rejected as an error.  */
6302
6303   switch (TREE_CODE (rhs))
6304     {
6305     case TYPE_EXPR:
6306     case CONVERT_EXPR:
6307     case SAVE_EXPR:
6308     case CONSTRUCTOR:
6309     case BUFFER_REF:
6310       abort ();
6311       return error_mark_node;
6312
6313     case INDIRECT_REF:
6314     case ARRAY_REF:
6315       {
6316         tree new_rhs;
6317
6318         new_rhs = instantiate_type (build_pointer_type (lhstype),
6319                                     TREE_OPERAND (rhs, 0), flags);
6320         if (new_rhs == error_mark_node)
6321           return error_mark_node;
6322
6323         TREE_TYPE (rhs) = lhstype;
6324         TREE_OPERAND (rhs, 0) = new_rhs;
6325         return rhs;
6326       }
6327
6328     case NOP_EXPR:
6329       rhs = copy_node (TREE_OPERAND (rhs, 0));
6330       TREE_TYPE (rhs) = unknown_type_node;
6331       return instantiate_type (lhstype, rhs, flags);
6332
6333     case COMPONENT_REF:
6334       return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6335
6336     case OFFSET_REF:
6337       rhs = TREE_OPERAND (rhs, 1);
6338       if (BASELINK_P (rhs))
6339         return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
6340                                  flags | allow_ptrmem);
6341
6342       /* This can happen if we are forming a pointer-to-member for a
6343          member template.  */
6344       my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6345
6346       /* Fall through.  */
6347
6348     case TEMPLATE_ID_EXPR:
6349       {
6350         tree fns = TREE_OPERAND (rhs, 0);
6351         tree args = TREE_OPERAND (rhs, 1);
6352
6353         return
6354           resolve_address_of_overloaded_function (lhstype,
6355                                                   fns,
6356                                                   complain,
6357                                                   allow_ptrmem,
6358                                                   /*template_only=*/1,
6359                                                   args);
6360       }
6361
6362     case OVERLOAD:
6363     case FUNCTION_DECL:
6364       return 
6365         resolve_address_of_overloaded_function (lhstype, 
6366                                                 rhs,
6367                                                 complain,
6368                                                 allow_ptrmem,
6369                                                 /*template_only=*/0,
6370                                                 /*explicit_targs=*/NULL_TREE);
6371
6372     case TREE_LIST:
6373       /* Now we should have a baselink.  */
6374       my_friendly_assert (BASELINK_P (rhs), 990412);
6375
6376       return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
6377
6378     case CALL_EXPR:
6379       /* This is too hard for now.  */
6380       abort ();
6381       return error_mark_node;
6382
6383     case PLUS_EXPR:
6384     case MINUS_EXPR:
6385     case COMPOUND_EXPR:
6386       TREE_OPERAND (rhs, 0)
6387         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6388       if (TREE_OPERAND (rhs, 0) == error_mark_node)
6389         return error_mark_node;
6390       TREE_OPERAND (rhs, 1)
6391         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6392       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6393         return error_mark_node;
6394
6395       TREE_TYPE (rhs) = lhstype;
6396       return rhs;
6397
6398     case MULT_EXPR:
6399     case TRUNC_DIV_EXPR:
6400     case FLOOR_DIV_EXPR:
6401     case CEIL_DIV_EXPR:
6402     case ROUND_DIV_EXPR:
6403     case RDIV_EXPR:
6404     case TRUNC_MOD_EXPR:
6405     case FLOOR_MOD_EXPR:
6406     case CEIL_MOD_EXPR:
6407     case ROUND_MOD_EXPR:
6408     case FIX_ROUND_EXPR:
6409     case FIX_FLOOR_EXPR:
6410     case FIX_CEIL_EXPR:
6411     case FIX_TRUNC_EXPR:
6412     case FLOAT_EXPR:
6413     case NEGATE_EXPR:
6414     case ABS_EXPR:
6415     case MAX_EXPR:
6416     case MIN_EXPR:
6417     case FFS_EXPR:
6418
6419     case BIT_AND_EXPR:
6420     case BIT_IOR_EXPR:
6421     case BIT_XOR_EXPR:
6422     case LSHIFT_EXPR:
6423     case RSHIFT_EXPR:
6424     case LROTATE_EXPR:
6425     case RROTATE_EXPR:
6426
6427     case PREINCREMENT_EXPR:
6428     case PREDECREMENT_EXPR:
6429     case POSTINCREMENT_EXPR:
6430     case POSTDECREMENT_EXPR:
6431       if (complain)
6432         error ("invalid operation on uninstantiated type");
6433       return error_mark_node;
6434
6435     case TRUTH_AND_EXPR:
6436     case TRUTH_OR_EXPR:
6437     case TRUTH_XOR_EXPR:
6438     case LT_EXPR:
6439     case LE_EXPR:
6440     case GT_EXPR:
6441     case GE_EXPR:
6442     case EQ_EXPR:
6443     case NE_EXPR:
6444     case TRUTH_ANDIF_EXPR:
6445     case TRUTH_ORIF_EXPR:
6446     case TRUTH_NOT_EXPR:
6447       if (complain)
6448         error ("not enough type information");
6449       return error_mark_node;
6450
6451     case COND_EXPR:
6452       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6453         {
6454           if (complain)
6455             error ("not enough type information");
6456           return error_mark_node;
6457         }
6458       TREE_OPERAND (rhs, 1)
6459         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6460       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6461         return error_mark_node;
6462       TREE_OPERAND (rhs, 2)
6463         = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6464       if (TREE_OPERAND (rhs, 2) == error_mark_node)
6465         return error_mark_node;
6466
6467       TREE_TYPE (rhs) = lhstype;
6468       return rhs;
6469
6470     case MODIFY_EXPR:
6471       TREE_OPERAND (rhs, 1)
6472         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6473       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6474         return error_mark_node;
6475
6476       TREE_TYPE (rhs) = lhstype;
6477       return rhs;
6478       
6479     case ADDR_EXPR:
6480     {
6481       if (PTRMEM_OK_P (rhs))
6482         flags |= tf_ptrmem_ok;
6483       
6484       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6485     }
6486     case ENTRY_VALUE_EXPR:
6487       abort ();
6488       return error_mark_node;
6489
6490     case ERROR_MARK:
6491       return error_mark_node;
6492
6493     default:
6494       abort ();
6495       return error_mark_node;
6496     }
6497 }
6498 \f
6499 /* Return the name of the virtual function pointer field
6500    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
6501    this may have to look back through base types to find the
6502    ultimate field name.  (For single inheritance, these could
6503    all be the same name.  Who knows for multiple inheritance).  */
6504
6505 static tree
6506 get_vfield_name (tree type)
6507 {
6508   tree binfo = TYPE_BINFO (type);
6509   char *buf;
6510
6511   while (BINFO_BASETYPES (binfo)
6512          && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6513          && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6514     binfo = BINFO_BASETYPE (binfo, 0);
6515
6516   type = BINFO_TYPE (binfo);
6517   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6518                          + TYPE_NAME_LENGTH (type) + 2);
6519   sprintf (buf, VFIELD_NAME_FORMAT,
6520            IDENTIFIER_POINTER (constructor_name (type)));
6521   return get_identifier (buf);
6522 }
6523
6524 void
6525 print_class_statistics (void)
6526 {
6527 #ifdef GATHER_STATISTICS
6528   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6529   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6530   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6531            n_build_method_call, n_inner_fields_searched);
6532   if (n_vtables)
6533     {
6534       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6535                n_vtables, n_vtable_searches);
6536       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6537                n_vtable_entries, n_vtable_elems);
6538     }
6539 #endif
6540 }
6541
6542 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6543    according to [class]:
6544                                           The class-name is also inserted
6545    into  the scope of the class itself.  For purposes of access checking,
6546    the inserted class name is treated as if it were a public member name.  */
6547
6548 void
6549 build_self_reference (void)
6550 {
6551   tree name = constructor_name (current_class_type);
6552   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6553   tree saved_cas;
6554
6555   DECL_NONLOCAL (value) = 1;
6556   DECL_CONTEXT (value) = current_class_type;
6557   DECL_ARTIFICIAL (value) = 1;
6558
6559   if (processing_template_decl)
6560     value = push_template_decl (value);
6561
6562   saved_cas = current_access_specifier;
6563   current_access_specifier = access_public_node;
6564   finish_member_declaration (value);
6565   current_access_specifier = saved_cas;
6566 }
6567
6568 /* Returns 1 if TYPE contains only padding bytes.  */
6569
6570 int
6571 is_empty_class (tree type)
6572 {
6573   if (type == error_mark_node)
6574     return 0;
6575
6576   if (! IS_AGGR_TYPE (type))
6577     return 0;
6578
6579   /* In G++ 3.2, whether or not a class was empty was determined by
6580      looking at its size.  */
6581   if (abi_version_at_least (2))
6582     return CLASSTYPE_EMPTY_P (type);
6583   else
6584     return integer_zerop (CLASSTYPE_SIZE (type));
6585 }
6586
6587 /* Returns true if TYPE contains an empty class.  */
6588
6589 static bool
6590 contains_empty_class_p (tree type)
6591 {
6592   if (is_empty_class (type))
6593     return true;
6594   if (CLASS_TYPE_P (type))
6595     {
6596       tree field;
6597       int i;
6598
6599       for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
6600         if (contains_empty_class_p (TYPE_BINFO_BASETYPE (type, i)))
6601           return true;
6602       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
6603         if (TREE_CODE (field) == FIELD_DECL
6604             && !DECL_ARTIFICIAL (field)
6605             && is_empty_class (TREE_TYPE (field)))
6606           return true;
6607     }
6608   else if (TREE_CODE (type) == ARRAY_TYPE)
6609     return contains_empty_class_p (TREE_TYPE (type));
6610   return false;
6611 }
6612
6613 /* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
6614    a *_TYPE node.  NODE can also be a local class.  */
6615
6616 tree
6617 get_enclosing_class (tree type)
6618 {
6619   tree node = type;
6620
6621   while (node && TREE_CODE (node) != NAMESPACE_DECL)
6622     {
6623       switch (TREE_CODE_CLASS (TREE_CODE (node)))
6624         {
6625         case 'd':
6626           node = DECL_CONTEXT (node);
6627           break;
6628
6629         case 't':
6630           if (node != type)
6631             return node;
6632           node = TYPE_CONTEXT (node);
6633           break;
6634
6635         default:
6636           abort ();
6637         }
6638     }
6639   return NULL_TREE;
6640 }
6641
6642 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
6643
6644 int
6645 is_base_of_enclosing_class (tree base, tree type)
6646 {
6647   while (type)
6648     {
6649       if (lookup_base (type, base, ba_any, NULL))
6650         return 1;
6651
6652       type = get_enclosing_class (type);
6653     }
6654   return 0;
6655 }
6656
6657 /* Note that NAME was looked up while the current class was being
6658    defined and that the result of that lookup was DECL.  */
6659
6660 void
6661 maybe_note_name_used_in_class (tree name, tree decl)
6662 {
6663   splay_tree names_used;
6664
6665   /* If we're not defining a class, there's nothing to do.  */
6666   if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6667     return;
6668   
6669   /* If there's already a binding for this NAME, then we don't have
6670      anything to worry about.  */
6671   if (IDENTIFIER_CLASS_VALUE (name))
6672     return;
6673
6674   if (!current_class_stack[current_class_depth - 1].names_used)
6675     current_class_stack[current_class_depth - 1].names_used
6676       = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6677   names_used = current_class_stack[current_class_depth - 1].names_used;
6678
6679   splay_tree_insert (names_used,
6680                      (splay_tree_key) name, 
6681                      (splay_tree_value) decl);
6682 }
6683
6684 /* Note that NAME was declared (as DECL) in the current class.  Check
6685    to see that the declaration is valid.  */
6686
6687 void
6688 note_name_declared_in_class (tree name, tree decl)
6689 {
6690   splay_tree names_used;
6691   splay_tree_node n;
6692
6693   /* Look to see if we ever used this name.  */
6694   names_used 
6695     = current_class_stack[current_class_depth - 1].names_used;
6696   if (!names_used)
6697     return;
6698
6699   n = splay_tree_lookup (names_used, (splay_tree_key) name);
6700   if (n)
6701     {
6702       /* [basic.scope.class]
6703          
6704          A name N used in a class S shall refer to the same declaration
6705          in its context and when re-evaluated in the completed scope of
6706          S.  */
6707       error ("declaration of `%#D'", decl);
6708       cp_error_at ("changes meaning of `%D' from `%+#D'", 
6709                    DECL_NAME (OVL_CURRENT (decl)),
6710                    (tree) n->value);
6711     }
6712 }
6713
6714 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6715    Secondary vtables are merged with primary vtables; this function
6716    will return the VAR_DECL for the primary vtable.  */
6717
6718 tree
6719 get_vtbl_decl_for_binfo (tree binfo)
6720 {
6721   tree decl;
6722
6723   decl = BINFO_VTABLE (binfo);
6724   if (decl && TREE_CODE (decl) == PLUS_EXPR)
6725     {
6726       my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6727                           2000403);
6728       decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6729     }
6730   if (decl)
6731     my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6732   return decl;
6733 }
6734
6735 /* Called from get_primary_binfo via dfs_walk.  DATA is a TREE_LIST
6736    who's TREE_PURPOSE is the TYPE of the required primary base and
6737    who's TREE_VALUE is a list of candidate binfos that we fill in.  */
6738
6739 static tree
6740 dfs_get_primary_binfo (tree binfo, void* data)
6741 {
6742   tree cons = (tree) data;
6743   tree primary_base = TREE_PURPOSE (cons);
6744
6745   if (TREE_VIA_VIRTUAL (binfo) 
6746       && same_type_p (BINFO_TYPE (binfo), primary_base))
6747     /* This is the right type of binfo, but it might be an unshared
6748        instance, and the shared instance is later in the dfs walk.  We
6749        must keep looking.  */
6750     TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6751   
6752   return NULL_TREE;
6753 }
6754
6755 /* Returns the unshared binfo for the primary base of BINFO.  Note
6756    that in a complex hierarchy the resulting BINFO may not actually
6757    *be* primary.  In particular if the resulting BINFO is a virtual
6758    base, and it occurs elsewhere in the hierarchy, then this
6759    occurrence may not actually be a primary base in the complete
6760    object.  Check BINFO_PRIMARY_P to be sure.  */
6761
6762 tree
6763 get_primary_binfo (tree binfo)
6764 {
6765   tree primary_base;
6766   tree result = NULL_TREE;
6767   tree virtuals;
6768   
6769   primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6770   if (!primary_base)
6771     return NULL_TREE;
6772
6773   /* A non-virtual primary base is always a direct base, and easy to
6774      find.  */
6775   if (!TREE_VIA_VIRTUAL (primary_base))
6776     {
6777       int i;
6778
6779       /* Scan the direct basetypes until we find a base with the same
6780          type as the primary base.  */
6781       for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6782         {
6783           tree base_binfo = BINFO_BASETYPE (binfo, i);
6784           
6785           if (same_type_p (BINFO_TYPE (base_binfo),
6786                            BINFO_TYPE (primary_base)))
6787             return base_binfo;
6788         }
6789
6790       /* We should always find the primary base.  */
6791       abort ();
6792     }
6793
6794   /* For a primary virtual base, we have to scan the entire hierarchy
6795      rooted at BINFO; the virtual base could be an indirect virtual
6796      base.  There could be more than one instance of the primary base
6797      in the hierarchy, and if one is the canonical binfo we want that
6798      one.  If it exists, it should be the first one we find, but as a
6799      consistency check we find them all and make sure.  */
6800   virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6801   dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6802   virtuals = TREE_VALUE (virtuals);
6803   
6804   /* We must have found at least one instance.  */
6805   my_friendly_assert (virtuals, 20010612);
6806
6807   if (TREE_CHAIN (virtuals))
6808     {
6809       /* We found more than one instance of the base.  If one is the
6810          canonical one, choose that one.  */
6811       tree complete_binfo;
6812       tree canonical;
6813       
6814       for (complete_binfo = binfo;
6815            BINFO_INHERITANCE_CHAIN (complete_binfo);
6816            complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6817         continue;
6818       canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6819                                    BINFO_TYPE (complete_binfo));
6820       
6821       for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6822         {
6823           result = TREE_VALUE (virtuals);
6824
6825           if (canonical == result)
6826             break;
6827         }
6828     }
6829   else
6830     result = TREE_VALUE (virtuals);
6831   return result;
6832 }
6833
6834 /* If INDENTED_P is zero, indent to INDENT. Return nonzero.  */
6835
6836 static int
6837 maybe_indent_hierarchy (FILE * stream, int indent, int indented_p)
6838 {
6839   if (!indented_p)
6840     fprintf (stream, "%*s", indent, "");
6841   return 1;
6842 }
6843
6844 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6845    dominated by T) to stderr.  INDENT should be zero when called from
6846    the top level; it is incremented recursively.  */
6847
6848 static void
6849 dump_class_hierarchy_r (FILE * stream,
6850                         int flags,
6851                         tree t,
6852                         tree binfo,
6853                         int indent)
6854 {
6855   int i;
6856   int indented = 0;
6857   
6858   indented = maybe_indent_hierarchy (stream, indent, 0);
6859   fprintf (stream, "%s (0x%lx) ",
6860            type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6861            (unsigned long) binfo);
6862   fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6863            tree_low_cst (BINFO_OFFSET (binfo), 0));
6864   if (is_empty_class (BINFO_TYPE (binfo)))
6865     fprintf (stream, " empty");
6866   else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6867     fprintf (stream, " nearly-empty");
6868   if (TREE_VIA_VIRTUAL (binfo))
6869     {
6870       tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6871
6872       fprintf (stream, " virtual");
6873       if (canonical == binfo)
6874         fprintf (stream, " canonical");
6875       else
6876         fprintf (stream, " non-canonical");
6877     }
6878   fprintf (stream, "\n");
6879
6880   indented = 0;
6881   if (BINFO_PRIMARY_BASE_OF (binfo))
6882     {
6883       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6884       fprintf (stream, " primary-for %s (0x%lx)",
6885                type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6886                                TFF_PLAIN_IDENTIFIER),
6887                (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6888     }
6889   if (BINFO_LOST_PRIMARY_P (binfo))
6890     {
6891       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6892       fprintf (stream, " lost-primary");
6893     }
6894   if (indented)
6895     fprintf (stream, "\n");
6896
6897   if (!(flags & TDF_SLIM))
6898     {
6899       int indented = 0;
6900       
6901       if (BINFO_SUBVTT_INDEX (binfo))
6902         {
6903           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6904           fprintf (stream, " subvttidx=%s",
6905                    expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6906                                    TFF_PLAIN_IDENTIFIER));
6907         }
6908       if (BINFO_VPTR_INDEX (binfo))
6909         {
6910           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6911           fprintf (stream, " vptridx=%s",
6912                    expr_as_string (BINFO_VPTR_INDEX (binfo),
6913                                    TFF_PLAIN_IDENTIFIER));
6914         }
6915       if (BINFO_VPTR_FIELD (binfo))
6916         {
6917           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6918           fprintf (stream, " vbaseoffset=%s",
6919                    expr_as_string (BINFO_VPTR_FIELD (binfo),
6920                                    TFF_PLAIN_IDENTIFIER));
6921         }
6922       if (BINFO_VTABLE (binfo))
6923         {
6924           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6925           fprintf (stream, " vptr=%s",
6926                    expr_as_string (BINFO_VTABLE (binfo),
6927                                    TFF_PLAIN_IDENTIFIER));
6928         }
6929       
6930       if (indented)
6931         fprintf (stream, "\n");
6932     }
6933   
6934
6935   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6936     dump_class_hierarchy_r (stream, flags,
6937                             t, BINFO_BASETYPE (binfo, i),
6938                             indent + 2);
6939 }
6940
6941 /* Dump the BINFO hierarchy for T.  */
6942
6943 static void
6944 dump_class_hierarchy (tree t)
6945 {
6946   int flags;
6947   FILE *stream = dump_begin (TDI_class, &flags);
6948
6949   if (!stream)
6950     return;
6951   
6952   fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6953   fprintf (stream, "   size=%lu align=%lu\n",
6954            (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6955            (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6956   dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6957   fprintf (stream, "\n");
6958   dump_end (TDI_class, stream);
6959 }
6960
6961 static void
6962 dump_array (FILE * stream, tree decl)
6963 {
6964   tree inits;
6965   int ix;
6966   HOST_WIDE_INT elt;
6967   tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6968
6969   elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6970          / BITS_PER_UNIT);
6971   fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6972   fprintf (stream, " %s entries",
6973            expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6974                            TFF_PLAIN_IDENTIFIER));
6975   fprintf (stream, "\n");
6976
6977   for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6978        inits; ix++, inits = TREE_CHAIN (inits))
6979     fprintf (stream, "%-4ld  %s\n", (long)(ix * elt),
6980              expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6981 }
6982
6983 static void
6984 dump_vtable (tree t, tree binfo, tree vtable)
6985 {
6986   int flags;
6987   FILE *stream = dump_begin (TDI_class, &flags);
6988
6989   if (!stream)
6990     return;
6991
6992   if (!(flags & TDF_SLIM))
6993     {
6994       int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6995       
6996       fprintf (stream, "%s for %s",
6997                ctor_vtbl_p ? "Construction vtable" : "Vtable",
6998                type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6999       if (ctor_vtbl_p)
7000         {
7001           if (!TREE_VIA_VIRTUAL (binfo))
7002             fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
7003           fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
7004         }
7005       fprintf (stream, "\n");
7006       dump_array (stream, vtable);
7007       fprintf (stream, "\n");
7008     }
7009   
7010   dump_end (TDI_class, stream);
7011 }
7012
7013 static void
7014 dump_vtt (tree t, tree vtt)
7015 {
7016   int flags;
7017   FILE *stream = dump_begin (TDI_class, &flags);
7018
7019   if (!stream)
7020     return;
7021
7022   if (!(flags & TDF_SLIM))
7023     {
7024       fprintf (stream, "VTT for %s\n",
7025                type_as_string (t, TFF_PLAIN_IDENTIFIER));
7026       dump_array (stream, vtt);
7027       fprintf (stream, "\n");
7028     }
7029   
7030   dump_end (TDI_class, stream);
7031 }
7032
7033 /* Virtual function table initialization.  */
7034
7035 /* Create all the necessary vtables for T and its base classes.  */
7036
7037 static void
7038 finish_vtbls (tree t)
7039 {
7040   tree list;
7041   tree vbase;
7042   int i;
7043
7044   /* We lay out the primary and secondary vtables in one contiguous
7045      vtable.  The primary vtable is first, followed by the non-virtual
7046      secondary vtables in inheritance graph order.  */
7047   list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
7048   accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
7049                          TYPE_BINFO (t), t, list);
7050   
7051   /* Then come the virtual bases, also in inheritance graph order.  */
7052   for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
7053     {
7054       tree real_base;
7055           
7056       if (!TREE_VIA_VIRTUAL (vbase))
7057         continue;
7058           
7059       /* Although we walk in inheritance order, that might not get the
7060          canonical base.  */
7061       real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
7062           
7063       accumulate_vtbl_inits (real_base, real_base,
7064                              TYPE_BINFO (t), t, list);
7065     }
7066
7067   /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
7068      base classes, for the benefit of the debugging backends.  */
7069   for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
7070     {
7071       tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
7072       if (TREE_VIA_VIRTUAL (base))
7073         {
7074           vbase = binfo_for_vbase (BINFO_TYPE (base), t);
7075           BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
7076         }
7077     }
7078
7079   if (TYPE_BINFO_VTABLE (t))
7080     initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
7081 }
7082
7083 /* Initialize the vtable for BINFO with the INITS.  */
7084
7085 static void
7086 initialize_vtable (tree binfo, tree inits)
7087 {
7088   tree decl;
7089
7090   layout_vtable_decl (binfo, list_length (inits));
7091   decl = get_vtbl_decl_for_binfo (binfo);
7092   initialize_array (decl, inits);
7093   dump_vtable (BINFO_TYPE (binfo), binfo, decl);
7094 }
7095
7096 /* Initialize DECL (a declaration for a namespace-scope array) with
7097    the INITS.  */
7098
7099 static void
7100 initialize_array (tree decl, tree inits)
7101 {
7102   tree context;
7103
7104   context = DECL_CONTEXT (decl);
7105   DECL_CONTEXT (decl) = NULL_TREE;
7106   DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
7107   TREE_HAS_CONSTRUCTOR (DECL_INITIAL (decl)) = 1;
7108   cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
7109   DECL_CONTEXT (decl) = context;
7110 }
7111
7112 /* Build the VTT (virtual table table) for T.
7113    A class requires a VTT if it has virtual bases.
7114    
7115    This holds
7116    1 - primary virtual pointer for complete object T
7117    2 - secondary VTTs for each direct non-virtual base of T which requires a
7118        VTT
7119    3 - secondary virtual pointers for each direct or indirect base of T which
7120        has virtual bases or is reachable via a virtual path from T.
7121    4 - secondary VTTs for each direct or indirect virtual base of T.
7122    
7123    Secondary VTTs look like complete object VTTs without part 4.  */
7124
7125 static void
7126 build_vtt (tree t)
7127 {
7128   tree inits;
7129   tree type;
7130   tree vtt;
7131   tree index;
7132
7133   /* Build up the initializers for the VTT.  */
7134   inits = NULL_TREE;
7135   index = size_zero_node;
7136   build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
7137
7138   /* If we didn't need a VTT, we're done.  */
7139   if (!inits)
7140     return;
7141
7142   /* Figure out the type of the VTT.  */
7143   type = build_index_type (size_int (list_length (inits) - 1));
7144   type = build_cplus_array_type (const_ptr_type_node, type);
7145                                  
7146   /* Now, build the VTT object itself.  */
7147   vtt = build_vtable (t, get_vtt_name (t), type);
7148   initialize_array (vtt, inits);
7149   /* Add the VTT to the vtables list.  */
7150   TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
7151   TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
7152
7153   dump_vtt (t, vtt);
7154 }
7155
7156 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
7157    from within some hierarchy which is inherited from the type of BINFO.
7158    Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
7159    BINFO.  */
7160
7161 static tree
7162 get_original_base (tree base_binfo, tree binfo)
7163 {
7164   tree derived;
7165   int ix;
7166   
7167   if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
7168     return binfo;
7169   if (TREE_VIA_VIRTUAL (base_binfo))
7170     return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
7171   derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
7172   
7173   for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7174     if (same_type_p (BINFO_TYPE (base_binfo),
7175                      BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7176       return BINFO_BASETYPE (derived, ix);
7177   abort ();
7178   return NULL;
7179 }
7180
7181 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7182    PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7183    and CHAIN the vtable pointer for this binfo after construction is
7184    complete.  VALUE can also be another BINFO, in which case we recurse.  */
7185
7186 static tree
7187 binfo_ctor_vtable (tree binfo)
7188 {
7189   tree vt;
7190
7191   while (1)
7192     {
7193       vt = BINFO_VTABLE (binfo);
7194       if (TREE_CODE (vt) == TREE_LIST)
7195         vt = TREE_VALUE (vt);
7196       if (TREE_CODE (vt) == TREE_VEC)
7197         binfo = vt;
7198       else
7199         break;
7200     }
7201
7202   return vt;
7203 }
7204
7205 /* Recursively build the VTT-initializer for BINFO (which is in the
7206    hierarchy dominated by T).  INITS points to the end of the initializer
7207    list to date.  INDEX is the VTT index where the next element will be
7208    replaced.  Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7209    not a subvtt for some base of T).  When that is so, we emit the sub-VTTs
7210    for virtual bases of T. When it is not so, we build the constructor
7211    vtables for the BINFO-in-T variant.  */
7212
7213 static tree *
7214 build_vtt_inits (tree binfo, tree t, tree* inits, tree* index)
7215 {
7216   int i;
7217   tree b;
7218   tree init;
7219   tree secondary_vptrs;
7220   int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7221
7222   /* We only need VTTs for subobjects with virtual bases.  */
7223   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7224     return inits;
7225
7226   /* We need to use a construction vtable if this is not the primary
7227      VTT.  */
7228   if (!top_level_p)
7229     {
7230       build_ctor_vtbl_group (binfo, t);
7231
7232       /* Record the offset in the VTT where this sub-VTT can be found.  */
7233       BINFO_SUBVTT_INDEX (binfo) = *index;
7234     }
7235
7236   /* Add the address of the primary vtable for the complete object.  */
7237   init = binfo_ctor_vtable (binfo);
7238   *inits = build_tree_list (NULL_TREE, init);
7239   inits = &TREE_CHAIN (*inits);
7240   if (top_level_p)
7241     {
7242       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7243       BINFO_VPTR_INDEX (binfo) = *index;
7244     }
7245   *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7246                        
7247   /* Recursively add the secondary VTTs for non-virtual bases.  */
7248   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7249     {
7250       b = BINFO_BASETYPE (binfo, i);
7251       if (!TREE_VIA_VIRTUAL (b))
7252         inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t, 
7253                                  inits, index);
7254     }
7255       
7256   /* Add secondary virtual pointers for all subobjects of BINFO with
7257      either virtual bases or reachable along a virtual path, except
7258      subobjects that are non-virtual primary bases.  */
7259   secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7260   TREE_TYPE (secondary_vptrs) = *index;
7261   VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7262   VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7263   
7264   dfs_walk_real (binfo,
7265                  dfs_build_secondary_vptr_vtt_inits,
7266                  NULL,
7267                  dfs_ctor_vtable_bases_queue_p,
7268                  secondary_vptrs);
7269   VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7270   dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7271             secondary_vptrs);
7272
7273   *index = TREE_TYPE (secondary_vptrs);
7274
7275   /* The secondary vptrs come back in reverse order.  After we reverse
7276      them, and add the INITS, the last init will be the first element
7277      of the chain.  */
7278   secondary_vptrs = TREE_VALUE (secondary_vptrs);
7279   if (secondary_vptrs)
7280     {
7281       *inits = nreverse (secondary_vptrs);
7282       inits = &TREE_CHAIN (secondary_vptrs);
7283       my_friendly_assert (*inits == NULL_TREE, 20000517);
7284     }
7285
7286   /* Add the secondary VTTs for virtual bases.  */
7287   if (top_level_p)
7288     for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7289       {
7290         tree vbase;
7291         
7292         if (!TREE_VIA_VIRTUAL (b))
7293           continue;
7294         
7295         vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7296         inits = build_vtt_inits (vbase, t, inits, index);
7297       }
7298
7299   if (!top_level_p)
7300     {
7301       tree data = tree_cons (t, binfo, NULL_TREE);
7302       VTT_TOP_LEVEL_P (data) = 0;
7303       VTT_MARKED_BINFO_P (data) = 0;
7304       
7305       dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7306                 dfs_ctor_vtable_bases_queue_p,
7307                 data);
7308     }
7309
7310   return inits;
7311 }
7312
7313 /* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo
7314    for the base in most derived. DATA is a TREE_LIST who's
7315    TREE_CHAIN is the type of the base being
7316    constructed whilst this secondary vptr is live.  The TREE_UNSIGNED
7317    flag of DATA indicates that this is a constructor vtable.  The
7318    TREE_TOP_LEVEL flag indicates that this is the primary VTT.  */
7319
7320 static tree
7321 dfs_build_secondary_vptr_vtt_inits (tree binfo, void* data)
7322 {
7323   tree l; 
7324   tree t;
7325   tree init;
7326   tree index;
7327   int top_level_p;
7328
7329   l = (tree) data;
7330   t = TREE_CHAIN (l);
7331   top_level_p = VTT_TOP_LEVEL_P (l);
7332   
7333   SET_BINFO_MARKED (binfo);
7334
7335   /* We don't care about bases that don't have vtables.  */
7336   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7337     return NULL_TREE;
7338
7339   /* We're only interested in proper subobjects of T.  */
7340   if (same_type_p (BINFO_TYPE (binfo), t))
7341     return NULL_TREE;
7342
7343   /* We're not interested in non-virtual primary bases.  */
7344   if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7345     return NULL_TREE;
7346
7347   /* If BINFO has virtual bases or is reachable via a virtual path
7348      from T, it'll have a secondary vptr.  */
7349   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7350       && !binfo_via_virtual (binfo, t))
7351     return NULL_TREE;
7352
7353   /* Record the index where this secondary vptr can be found.  */
7354   index = TREE_TYPE (l);
7355   if (top_level_p)
7356     {
7357       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7358       BINFO_VPTR_INDEX (binfo) = index;
7359     }
7360   TREE_TYPE (l) = size_binop (PLUS_EXPR, index, 
7361                               TYPE_SIZE_UNIT (ptr_type_node));
7362
7363   /* Add the initializer for the secondary vptr itself.  */
7364   if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7365     {
7366       /* It's a primary virtual base, and this is not the construction
7367          vtable. Find the base this is primary of in the inheritance graph,
7368          and use that base's vtable now.  */
7369       while (BINFO_PRIMARY_BASE_OF (binfo))
7370         binfo = BINFO_PRIMARY_BASE_OF (binfo);
7371     }
7372   init = binfo_ctor_vtable (binfo);
7373   TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7374
7375   return NULL_TREE;
7376 }
7377
7378 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7379    VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7380    should be walked.  TREE_PURPOSE is the TREE_TYPE that dominates the
7381    hierarchy.  */
7382
7383 static tree
7384 dfs_ctor_vtable_bases_queue_p (tree binfo, void* data)
7385 {
7386   if (TREE_VIA_VIRTUAL (binfo))
7387      /* Get the shared version.  */
7388     binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7389
7390   if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7391     return NULL_TREE;
7392   return binfo;
7393 }
7394
7395 /* Called from build_vtt_inits via dfs_walk. After building constructor
7396    vtables and generating the sub-vtt from them, we need to restore the
7397    BINFO_VTABLES that were scribbled on.  DATA is a TREE_LIST whose
7398    TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated.  */
7399
7400 static tree
7401 dfs_fixup_binfo_vtbls (tree binfo, void* data)
7402 {
7403   CLEAR_BINFO_MARKED (binfo);
7404
7405   /* We don't care about bases that don't have vtables.  */
7406   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7407     return NULL_TREE;
7408
7409   /* If we scribbled the construction vtable vptr into BINFO, clear it
7410      out now.  */
7411   if (BINFO_VTABLE (binfo)
7412       && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7413       && (TREE_PURPOSE (BINFO_VTABLE (binfo)) 
7414           == TREE_VALUE ((tree) data)))
7415     BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7416
7417   return NULL_TREE;
7418 }
7419
7420 /* Build the construction vtable group for BINFO which is in the
7421    hierarchy dominated by T.  */
7422
7423 static void
7424 build_ctor_vtbl_group (tree binfo, tree t)
7425 {
7426   tree list;
7427   tree type;
7428   tree vtbl;
7429   tree inits;
7430   tree id;
7431   tree vbase;
7432
7433   /* See if we've already created this construction vtable group.  */
7434   id = mangle_ctor_vtbl_for_type (t, binfo);
7435   if (IDENTIFIER_GLOBAL_VALUE (id))
7436     return;
7437
7438   my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7439   /* Build a version of VTBL (with the wrong type) for use in
7440      constructing the addresses of secondary vtables in the
7441      construction vtable group.  */
7442   vtbl = build_vtable (t, id, ptr_type_node);
7443   list = build_tree_list (vtbl, NULL_TREE);
7444   accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7445                          binfo, t, list);
7446
7447   /* Add the vtables for each of our virtual bases using the vbase in T
7448      binfo.  */
7449   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 
7450        vbase; 
7451        vbase = TREE_CHAIN (vbase))
7452     {
7453       tree b;
7454       tree orig_base;
7455
7456       if (!TREE_VIA_VIRTUAL (vbase))
7457         continue;
7458       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7459       orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7460       
7461       accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7462     }
7463   inits = TREE_VALUE (list);
7464
7465   /* Figure out the type of the construction vtable.  */
7466   type = build_index_type (size_int (list_length (inits) - 1));
7467   type = build_cplus_array_type (vtable_entry_type, type);
7468   TREE_TYPE (vtbl) = type;
7469
7470   /* Initialize the construction vtable.  */
7471   CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
7472   initialize_array (vtbl, inits);
7473   dump_vtable (t, binfo, vtbl);
7474 }
7475
7476 /* Add the vtbl initializers for BINFO (and its bases other than
7477    non-virtual primaries) to the list of INITS.  BINFO is in the
7478    hierarchy dominated by T.  RTTI_BINFO is the binfo within T of
7479    the constructor the vtbl inits should be accumulated for. (If this
7480    is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7481    ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7482    BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7483    graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7484    but are not necessarily the same in terms of layout.  */
7485
7486 static void
7487 accumulate_vtbl_inits (tree binfo,
7488                        tree orig_binfo,
7489                        tree rtti_binfo,
7490                        tree t,
7491                        tree inits)
7492 {
7493   int i;
7494   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7495
7496   my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7497                                    BINFO_TYPE (orig_binfo)),
7498                       20000517);
7499
7500   /* If it doesn't have a vptr, we don't do anything.  */
7501   if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7502     return;
7503   
7504   /* If we're building a construction vtable, we're not interested in
7505      subobjects that don't require construction vtables.  */
7506   if (ctor_vtbl_p 
7507       && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7508       && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7509     return;
7510
7511   /* Build the initializers for the BINFO-in-T vtable.  */
7512   TREE_VALUE (inits) 
7513     = chainon (TREE_VALUE (inits),
7514                dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7515                                           rtti_binfo, t, inits));
7516                       
7517   /* Walk the BINFO and its bases.  We walk in preorder so that as we
7518      initialize each vtable we can figure out at what offset the
7519      secondary vtable lies from the primary vtable.  We can't use
7520      dfs_walk here because we need to iterate through bases of BINFO
7521      and RTTI_BINFO simultaneously.  */
7522   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7523     {
7524       tree base_binfo = BINFO_BASETYPE (binfo, i);
7525       
7526       /* Skip virtual bases.  */
7527       if (TREE_VIA_VIRTUAL (base_binfo))
7528         continue;
7529       accumulate_vtbl_inits (base_binfo,
7530                              BINFO_BASETYPE (orig_binfo, i),
7531                              rtti_binfo, t,
7532                              inits);
7533     }
7534 }
7535
7536 /* Called from accumulate_vtbl_inits.  Returns the initializers for
7537    the BINFO vtable.  */
7538
7539 static tree
7540 dfs_accumulate_vtbl_inits (tree binfo,
7541                            tree orig_binfo,
7542                            tree rtti_binfo,
7543                            tree t,
7544                            tree l)
7545 {
7546   tree inits = NULL_TREE;
7547   tree vtbl = NULL_TREE;
7548   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7549
7550   if (ctor_vtbl_p
7551       && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7552     {
7553       /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7554          primary virtual base.  If it is not the same primary in
7555          the hierarchy of T, we'll need to generate a ctor vtable
7556          for it, to place at its location in T.  If it is the same
7557          primary, we still need a VTT entry for the vtable, but it
7558          should point to the ctor vtable for the base it is a
7559          primary for within the sub-hierarchy of RTTI_BINFO.
7560               
7561          There are three possible cases:
7562               
7563          1) We are in the same place.
7564          2) We are a primary base within a lost primary virtual base of
7565          RTTI_BINFO.
7566          3) We are primary to something not a base of RTTI_BINFO.  */
7567           
7568       tree b = BINFO_PRIMARY_BASE_OF (binfo);
7569       tree last = NULL_TREE;
7570
7571       /* First, look through the bases we are primary to for RTTI_BINFO
7572          or a virtual base.  */
7573       for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7574         {
7575           last = b;
7576           if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7577             break;
7578         }
7579       /* If we run out of primary links, keep looking down our
7580          inheritance chain; we might be an indirect primary.  */
7581       if (b == NULL_TREE)
7582         for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7583           if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7584             break;
7585
7586       /* If we found RTTI_BINFO, this is case 1.  If we found a virtual
7587          base B and it is a base of RTTI_BINFO, this is case 2.  In
7588          either case, we share our vtable with LAST, i.e. the
7589          derived-most base within B of which we are a primary.  */
7590       if (b == rtti_binfo
7591           || (b && binfo_for_vbase (BINFO_TYPE (b),
7592                                     BINFO_TYPE (rtti_binfo))))
7593         /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7594            set LAST's BINFO_VTABLE yet.  We'll extract the actual vptr in
7595            binfo_ctor_vtable after everything's been set up.  */
7596         vtbl = last;
7597
7598       /* Otherwise, this is case 3 and we get our own.  */
7599     }
7600   else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7601     return inits;
7602
7603   if (!vtbl)
7604     {
7605       tree index;
7606       int non_fn_entries;
7607
7608       /* Compute the initializer for this vtable.  */
7609       inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7610                                       &non_fn_entries);
7611
7612       /* Figure out the position to which the VPTR should point.  */
7613       vtbl = TREE_PURPOSE (l);
7614       vtbl = build1 (ADDR_EXPR, 
7615                      vtbl_ptr_type_node,
7616                      vtbl);
7617       TREE_CONSTANT (vtbl) = 1;
7618       index = size_binop (PLUS_EXPR,
7619                           size_int (non_fn_entries),
7620                           size_int (list_length (TREE_VALUE (l))));
7621       index = size_binop (MULT_EXPR,
7622                           TYPE_SIZE_UNIT (vtable_entry_type),
7623                           index);
7624       vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7625       TREE_CONSTANT (vtbl) = 1;
7626     }
7627
7628   if (ctor_vtbl_p)
7629     /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7630        So, we make a TREE_LIST.  Later, dfs_fixup_binfo_vtbls will
7631        straighten this out.  */
7632     BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7633   else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7634     inits = NULL_TREE;
7635   else
7636      /* For an ordinary vtable, set BINFO_VTABLE.  */
7637     BINFO_VTABLE (binfo) = vtbl;
7638
7639   return inits;
7640 }
7641
7642 /* Construct the initializer for BINFO's virtual function table.  BINFO
7643    is part of the hierarchy dominated by T.  If we're building a
7644    construction vtable, the ORIG_BINFO is the binfo we should use to
7645    find the actual function pointers to put in the vtable - but they
7646    can be overridden on the path to most-derived in the graph that
7647    ORIG_BINFO belongs.  Otherwise,
7648    ORIG_BINFO should be the same as BINFO.  The RTTI_BINFO is the
7649    BINFO that should be indicated by the RTTI information in the
7650    vtable; it will be a base class of T, rather than T itself, if we
7651    are building a construction vtable.
7652
7653    The value returned is a TREE_LIST suitable for wrapping in a
7654    CONSTRUCTOR to use as the DECL_INITIAL for a vtable.  If
7655    NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7656    number of non-function entries in the vtable.  
7657
7658    It might seem that this function should never be called with a
7659    BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7660    base is always subsumed by a derived class vtable.  However, when
7661    we are building construction vtables, we do build vtables for
7662    primary bases; we need these while the primary base is being
7663    constructed.  */
7664
7665 static tree
7666 build_vtbl_initializer (tree binfo,
7667                         tree orig_binfo,
7668                         tree t,
7669                         tree rtti_binfo,
7670                         int* non_fn_entries_p)
7671 {
7672   tree v, b;
7673   tree vfun_inits;
7674   tree vbase;
7675   vtbl_init_data vid;
7676
7677   /* Initialize VID.  */
7678   memset (&vid, 0, sizeof (vid));
7679   vid.binfo = binfo;
7680   vid.derived = t;
7681   vid.rtti_binfo = rtti_binfo;
7682   vid.last_init = &vid.inits;
7683   vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7684   vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7685   vid.generate_vcall_entries = true;
7686   /* The first vbase or vcall offset is at index -3 in the vtable.  */
7687   vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
7688
7689   /* Add entries to the vtable for RTTI.  */
7690   build_rtti_vtbl_entries (binfo, &vid);
7691
7692   /* Create an array for keeping track of the functions we've
7693      processed.  When we see multiple functions with the same
7694      signature, we share the vcall offsets.  */
7695   VARRAY_TREE_INIT (vid.fns, 32, "fns");
7696   /* Add the vcall and vbase offset entries.  */
7697   build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7698   /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7699      build_vbase_offset_vtbl_entries.  */
7700   for (vbase = CLASSTYPE_VBASECLASSES (t); 
7701        vbase; 
7702        vbase = TREE_CHAIN (vbase))
7703     CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7704
7705   /* If the target requires padding between data entries, add that now.  */
7706   if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
7707     {
7708       tree cur, *prev;
7709
7710       for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
7711         {
7712           tree add = cur;
7713           int i;
7714
7715           for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
7716             add = tree_cons (NULL_TREE,
7717                              build1 (NOP_EXPR, vtable_entry_type,
7718                                      null_pointer_node),
7719                              add);
7720           *prev = add;
7721         }
7722     }
7723
7724   if (non_fn_entries_p)
7725     *non_fn_entries_p = list_length (vid.inits);
7726
7727   /* Go through all the ordinary virtual functions, building up
7728      initializers.  */
7729   vfun_inits = NULL_TREE;
7730   for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7731     {
7732       tree delta;
7733       tree vcall_index;
7734       tree fn, fn_original;
7735       tree init = NULL_TREE;
7736       
7737       fn = BV_FN (v);
7738       fn_original = fn;
7739       if (DECL_THUNK_P (fn))
7740         {
7741           if (!DECL_NAME (fn))
7742             finish_thunk (fn);
7743           fn_original = THUNK_TARGET (fn);
7744         }
7745       
7746       /* If the only definition of this function signature along our
7747          primary base chain is from a lost primary, this vtable slot will
7748          never be used, so just zero it out.  This is important to avoid
7749          requiring extra thunks which cannot be generated with the function.
7750
7751          We first check this in update_vtable_entry_for_fn, so we handle
7752          restored primary bases properly; we also need to do it here so we
7753          zero out unused slots in ctor vtables, rather than filling themff
7754          with erroneous values (though harmless, apart from relocation
7755          costs).  */
7756       for (b = binfo; ; b = get_primary_binfo (b))
7757         {
7758           /* We found a defn before a lost primary; go ahead as normal.  */
7759           if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
7760             break;
7761
7762           /* The nearest definition is from a lost primary; clear the
7763              slot.  */
7764           if (BINFO_LOST_PRIMARY_P (b))
7765             {
7766               init = size_zero_node;
7767               break;
7768             }
7769         }
7770
7771       if (! init)
7772         {
7773           /* Pull the offset for `this', and the function to call, out of
7774              the list.  */
7775           delta = BV_DELTA (v);
7776           vcall_index = BV_VCALL_INDEX (v);
7777
7778           my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7779           my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7780
7781           /* You can't call an abstract virtual function; it's abstract.
7782              So, we replace these functions with __pure_virtual.  */
7783           if (DECL_PURE_VIRTUAL_P (fn_original))
7784             fn = abort_fndecl;
7785           else if (!integer_zerop (delta) || vcall_index)
7786             {
7787               fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
7788               if (!DECL_NAME (fn))
7789                 finish_thunk (fn);
7790             }
7791           /* Take the address of the function, considering it to be of an
7792              appropriate generic type.  */
7793           init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7794           /* The address of a function can't change.  */
7795           TREE_CONSTANT (init) = 1;
7796         }
7797
7798       /* And add it to the chain of initializers.  */
7799       if (TARGET_VTABLE_USES_DESCRIPTORS)
7800         {
7801           int i;
7802           if (init == size_zero_node)
7803             for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7804               vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7805           else
7806             for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7807               {
7808                 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7809                                     TREE_OPERAND (init, 0),
7810                                     build_int_2 (i, 0));
7811                 TREE_CONSTANT (fdesc) = 1;
7812
7813                 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7814               }
7815         }
7816       else
7817         vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7818     }
7819
7820   /* The initializers for virtual functions were built up in reverse
7821      order; straighten them out now.  */
7822   vfun_inits = nreverse (vfun_inits);
7823   
7824   /* The negative offset initializers are also in reverse order.  */
7825   vid.inits = nreverse (vid.inits);
7826
7827   /* Chain the two together.  */
7828   return chainon (vid.inits, vfun_inits);
7829 }
7830
7831 /* Adds to vid->inits the initializers for the vbase and vcall
7832    offsets in BINFO, which is in the hierarchy dominated by T.  */
7833
7834 static void
7835 build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
7836 {
7837   tree b;
7838
7839   /* If this is a derived class, we must first create entries
7840      corresponding to the primary base class.  */
7841   b = get_primary_binfo (binfo);
7842   if (b)
7843     build_vcall_and_vbase_vtbl_entries (b, vid);
7844
7845   /* Add the vbase entries for this base.  */
7846   build_vbase_offset_vtbl_entries (binfo, vid);
7847   /* Add the vcall entries for this base.  */
7848   build_vcall_offset_vtbl_entries (binfo, vid);
7849 }
7850
7851 /* Returns the initializers for the vbase offset entries in the vtable
7852    for BINFO (which is part of the class hierarchy dominated by T), in
7853    reverse order.  VBASE_OFFSET_INDEX gives the vtable index
7854    where the next vbase offset will go.  */
7855
7856 static void
7857 build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7858 {
7859   tree vbase;
7860   tree t;
7861   tree non_primary_binfo;
7862
7863   /* If there are no virtual baseclasses, then there is nothing to
7864      do.  */
7865   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7866     return;
7867
7868   t = vid->derived;
7869   
7870   /* We might be a primary base class.  Go up the inheritance hierarchy
7871      until we find the most derived class of which we are a primary base:
7872      it is the offset of that which we need to use.  */
7873   non_primary_binfo = binfo;
7874   while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7875     {
7876       tree b;
7877
7878       /* If we have reached a virtual base, then it must be a primary
7879          base (possibly multi-level) of vid->binfo, or we wouldn't
7880          have called build_vcall_and_vbase_vtbl_entries for it.  But it
7881          might be a lost primary, so just skip down to vid->binfo.  */
7882       if (TREE_VIA_VIRTUAL (non_primary_binfo))
7883         {
7884           non_primary_binfo = vid->binfo;
7885           break;
7886         }
7887
7888       b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7889       if (get_primary_binfo (b) != non_primary_binfo)
7890         break;
7891       non_primary_binfo = b;
7892     }
7893
7894   /* Go through the virtual bases, adding the offsets.  */
7895   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7896        vbase;
7897        vbase = TREE_CHAIN (vbase))
7898     {
7899       tree b;
7900       tree delta;
7901       
7902       if (!TREE_VIA_VIRTUAL (vbase))
7903         continue;
7904
7905       /* Find the instance of this virtual base in the complete
7906          object.  */
7907       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7908
7909       /* If we've already got an offset for this virtual base, we
7910          don't need another one.  */
7911       if (BINFO_VTABLE_PATH_MARKED (b))
7912         continue;
7913       SET_BINFO_VTABLE_PATH_MARKED (b);
7914
7915       /* Figure out where we can find this vbase offset.  */
7916       delta = size_binop (MULT_EXPR, 
7917                           vid->index,
7918                           convert (ssizetype,
7919                                    TYPE_SIZE_UNIT (vtable_entry_type)));
7920       if (vid->primary_vtbl_p)
7921         BINFO_VPTR_FIELD (b) = delta;
7922
7923       if (binfo != TYPE_BINFO (t))
7924         {
7925           tree orig_vbase;
7926
7927           /* Find the instance of this virtual base in the type of BINFO.  */
7928           orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7929                                         BINFO_TYPE (binfo));
7930
7931           /* The vbase offset had better be the same.  */
7932           if (!tree_int_cst_equal (delta,
7933                                    BINFO_VPTR_FIELD (orig_vbase)))
7934             abort ();
7935         }
7936
7937       /* The next vbase will come at a more negative offset.  */
7938       vid->index = size_binop (MINUS_EXPR, vid->index,
7939                                ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
7940
7941       /* The initializer is the delta from BINFO to this virtual base.
7942          The vbase offsets go in reverse inheritance-graph order, and
7943          we are walking in inheritance graph order so these end up in
7944          the right order.  */
7945       delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7946       
7947       *vid->last_init 
7948         = build_tree_list (NULL_TREE,
7949                            fold (build1 (NOP_EXPR, 
7950                                          vtable_entry_type,
7951                                          delta)));
7952       vid->last_init = &TREE_CHAIN (*vid->last_init);
7953     }
7954 }
7955
7956 /* Adds the initializers for the vcall offset entries in the vtable
7957    for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7958    to VID->INITS.  */
7959
7960 static void
7961 build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
7962 {
7963   /* We only need these entries if this base is a virtual base.  We
7964      compute the indices -- but do not add to the vtable -- when
7965      building the main vtable for a class.  */
7966   if (TREE_VIA_VIRTUAL (binfo) || binfo == TYPE_BINFO (vid->derived))
7967     {
7968       /* We need a vcall offset for each of the virtual functions in this
7969          vtable.  For example:
7970
7971            class A { virtual void f (); };
7972            class B1 : virtual public A { virtual void f (); };
7973            class B2 : virtual public A { virtual void f (); };
7974            class C: public B1, public B2 { virtual void f (); };
7975
7976          A C object has a primary base of B1, which has a primary base of A.  A
7977          C also has a secondary base of B2, which no longer has a primary base
7978          of A.  So the B2-in-C construction vtable needs a secondary vtable for
7979          A, which will adjust the A* to a B2* to call f.  We have no way of
7980          knowing what (or even whether) this offset will be when we define B2,
7981          so we store this "vcall offset" in the A sub-vtable and look it up in
7982          a "virtual thunk" for B2::f.
7983
7984          We need entries for all the functions in our primary vtable and
7985          in our non-virtual bases' secondary vtables.  */
7986       vid->vbase = binfo;
7987       /* If we are just computing the vcall indices -- but do not need
7988          the actual entries -- not that.  */
7989       if (!TREE_VIA_VIRTUAL (binfo))
7990         vid->generate_vcall_entries = false;
7991       /* Now, walk through the non-virtual bases, adding vcall offsets.  */
7992       add_vcall_offset_vtbl_entries_r (binfo, vid);
7993     }
7994 }
7995
7996 /* Build vcall offsets, starting with those for BINFO.  */
7997
7998 static void
7999 add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
8000 {
8001   int i;
8002   tree primary_binfo;
8003
8004   /* Don't walk into virtual bases -- except, of course, for the
8005      virtual base for which we are building vcall offsets.  Any
8006      primary virtual base will have already had its offsets generated
8007      through the recursion in build_vcall_and_vbase_vtbl_entries.  */
8008   if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
8009     return;
8010   
8011   /* If BINFO has a primary base, process it first.  */
8012   primary_binfo = get_primary_binfo (binfo);
8013   if (primary_binfo)
8014     add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
8015
8016   /* Add BINFO itself to the list.  */
8017   add_vcall_offset_vtbl_entries_1 (binfo, vid);
8018
8019   /* Scan the non-primary bases of BINFO.  */
8020   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i) 
8021     {
8022       tree base_binfo;
8023       
8024       base_binfo = BINFO_BASETYPE (binfo, i);
8025       if (base_binfo != primary_binfo)
8026         add_vcall_offset_vtbl_entries_r (base_binfo, vid);
8027     }
8028 }
8029
8030 /* Called from build_vcall_offset_vtbl_entries_r.  */
8031
8032 static void
8033 add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
8034 {
8035   /* Make entries for the rest of the virtuals.  */
8036   if (abi_version_at_least (2))
8037     {
8038       tree orig_fn;
8039
8040       /* The ABI requires that the methods be processed in declaration
8041          order.  G++ 3.2 used the order in the vtable.  */
8042       for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
8043            orig_fn;
8044            orig_fn = TREE_CHAIN (orig_fn))
8045         if (DECL_VINDEX (orig_fn))
8046           add_vcall_offset (orig_fn, binfo, vid);
8047     }
8048   else
8049     {
8050       tree derived_virtuals;
8051       tree base_virtuals;
8052       tree orig_virtuals;
8053       /* If BINFO is a primary base, the most derived class which has
8054          BINFO as a primary base; otherwise, just BINFO.  */
8055       tree non_primary_binfo;
8056
8057       /* We might be a primary base class.  Go up the inheritance hierarchy
8058          until we find the most derived class of which we are a primary base:
8059          it is the BINFO_VIRTUALS there that we need to consider.  */
8060       non_primary_binfo = binfo;
8061       while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
8062         {
8063           tree b;
8064
8065           /* If we have reached a virtual base, then it must be vid->vbase,
8066              because we ignore other virtual bases in
8067              add_vcall_offset_vtbl_entries_r.  In turn, it must be a primary
8068              base (possibly multi-level) of vid->binfo, or we wouldn't
8069              have called build_vcall_and_vbase_vtbl_entries for it.  But it
8070              might be a lost primary, so just skip down to vid->binfo.  */
8071           if (TREE_VIA_VIRTUAL (non_primary_binfo))
8072             {
8073               if (non_primary_binfo != vid->vbase)
8074                 abort ();
8075               non_primary_binfo = vid->binfo;
8076               break;
8077             }
8078
8079           b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
8080           if (get_primary_binfo (b) != non_primary_binfo)
8081             break;
8082           non_primary_binfo = b;
8083         }
8084
8085       if (vid->ctor_vtbl_p)
8086         /* For a ctor vtable we need the equivalent binfo within the hierarchy
8087            where rtti_binfo is the most derived type.  */
8088         non_primary_binfo = get_original_base
8089           (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
8090       
8091       for (base_virtuals = BINFO_VIRTUALS (binfo),
8092              derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
8093              orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
8094            base_virtuals;
8095            base_virtuals = TREE_CHAIN (base_virtuals),
8096              derived_virtuals = TREE_CHAIN (derived_virtuals),
8097              orig_virtuals = TREE_CHAIN (orig_virtuals))
8098         {
8099           tree orig_fn;
8100
8101           /* Find the declaration that originally caused this function to
8102              be present in BINFO_TYPE (binfo).  */
8103           orig_fn = BV_FN (orig_virtuals);
8104
8105           /* When processing BINFO, we only want to generate vcall slots for
8106              function slots introduced in BINFO.  So don't try to generate
8107              one if the function isn't even defined in BINFO.  */
8108           if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
8109             continue;
8110
8111           add_vcall_offset (orig_fn, binfo, vid);
8112         }
8113     }
8114 }
8115
8116 /* Add a vcall offset entry for ORIG_FN to the vtable.  */
8117
8118 static void
8119 add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
8120 {
8121   size_t i;
8122   tree vcall_offset;
8123
8124   /* If there is already an entry for a function with the same
8125      signature as FN, then we do not need a second vcall offset.
8126      Check the list of functions already present in the derived
8127      class vtable.  */
8128   for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i) 
8129     {
8130       tree derived_entry;
8131
8132       derived_entry = VARRAY_TREE (vid->fns, i);
8133       if (same_signature_p (derived_entry, orig_fn)
8134           /* We only use one vcall offset for virtual destructors,
8135              even though there are two virtual table entries.  */
8136           || (DECL_DESTRUCTOR_P (derived_entry)
8137               && DECL_DESTRUCTOR_P (orig_fn)))
8138         return;
8139     }
8140
8141   /* If we are building these vcall offsets as part of building
8142      the vtable for the most derived class, remember the vcall
8143      offset.  */
8144   if (vid->binfo == TYPE_BINFO (vid->derived))
8145     CLASSTYPE_VCALL_INDICES (vid->derived) 
8146       = tree_cons (orig_fn, vid->index, 
8147                    CLASSTYPE_VCALL_INDICES (vid->derived));
8148
8149   /* The next vcall offset will be found at a more negative
8150      offset.  */
8151   vid->index = size_binop (MINUS_EXPR, vid->index,
8152                            ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
8153
8154   /* Keep track of this function.  */
8155   VARRAY_PUSH_TREE (vid->fns, orig_fn);
8156
8157   if (vid->generate_vcall_entries)
8158     {
8159       tree base;
8160       tree fn;
8161
8162       /* Find the overriding function.  */
8163       fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
8164       if (fn == error_mark_node)
8165         vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
8166                                integer_zero_node);
8167       else
8168         {
8169           base = TREE_VALUE (fn);
8170
8171           /* The vbase we're working on is a primary base of
8172              vid->binfo.  But it might be a lost primary, so its
8173              BINFO_OFFSET might be wrong, so we just use the
8174              BINFO_OFFSET from vid->binfo.  */
8175           vcall_offset = size_diffop (BINFO_OFFSET (base),
8176                                       BINFO_OFFSET (vid->binfo));
8177           vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
8178                                        vcall_offset));
8179         }
8180       /* Add the intiailizer to the vtable.  */
8181       *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
8182       vid->last_init = &TREE_CHAIN (*vid->last_init);
8183     }
8184 }
8185
8186 /* Return vtbl initializers for the RTTI entries coresponding to the
8187    BINFO's vtable.  The RTTI entries should indicate the object given
8188    by VID->rtti_binfo.  */
8189
8190 static void
8191 build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
8192 {
8193   tree b;
8194   tree t;
8195   tree basetype;
8196   tree offset;
8197   tree decl;
8198   tree init;
8199
8200   basetype = BINFO_TYPE (binfo);
8201   t = BINFO_TYPE (vid->rtti_binfo);
8202
8203   /* To find the complete object, we will first convert to our most
8204      primary base, and then add the offset in the vtbl to that value.  */
8205   b = binfo;
8206   while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8207          && !BINFO_LOST_PRIMARY_P (b))
8208     {
8209       tree primary_base;
8210
8211       primary_base = get_primary_binfo (b);
8212       my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8213       b = primary_base;
8214     }
8215   offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8216
8217   /* The second entry is the address of the typeinfo object.  */
8218   if (flag_rtti)
8219     decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8220   else
8221     decl = integer_zero_node;
8222   
8223   /* Convert the declaration to a type that can be stored in the
8224      vtable.  */
8225   init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8226   TREE_CONSTANT (init) = 1;
8227   *vid->last_init = build_tree_list (NULL_TREE, init);
8228   vid->last_init = &TREE_CHAIN (*vid->last_init);
8229
8230   /* Add the offset-to-top entry.  It comes earlier in the vtable that
8231      the the typeinfo entry.  Convert the offset to look like a
8232      function pointer, so that we can put it in the vtable.  */
8233   init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8234   TREE_CONSTANT (init) = 1;
8235   *vid->last_init = build_tree_list (NULL_TREE, init);
8236   vid->last_init = &TREE_CHAIN (*vid->last_init);
8237 }