OSDN Git Service

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