OSDN Git Service

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