OSDN Git Service

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