OSDN Git Service

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