OSDN Git Service

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