OSDN Git Service

* alpha.md (addsi3, subsi3): No new temporaries once cse is
[pf3gnuchains/gcc-fork.git] / gcc / cp / class.c
1 /* Functions related to building classes and their related objects.
2    Copyright (C) 1987, 92, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3    Contributed by Michael Tiemann (tiemann@cygnus.com)
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22
23 /* High-level class interface.  */
24
25 #include "config.h"
26 #include "system.h"
27 #include "tree.h"
28 #include "cp-tree.h"
29 #include "flags.h"
30 #include "rtl.h"
31 #include "output.h"
32 #include "toplev.h"
33
34 #include "obstack.h"
35 #define obstack_chunk_alloc xmalloc
36 #define obstack_chunk_free free
37
38 extern struct obstack permanent_obstack;
39
40 /* This is how we tell when two virtual member functions are really the
41    same.  */
42 #define SAME_FN(FN1DECL, FN2DECL) (DECL_ASSEMBLER_NAME (FN1DECL) == DECL_ASSEMBLER_NAME (FN2DECL))
43
44 extern void set_class_shadows PROTO ((tree));
45
46 /* The number of nested classes being processed.  If we are not in the
47    scope of any class, this is zero.  */
48
49 int current_class_depth;
50
51 /* In order to deal with nested classes, we keep a stack of classes.
52    The topmost entry is the innermost class, and is the entry at index
53    CURRENT_CLASS_DEPTH  */
54
55 typedef struct class_stack_node {
56   /* The name of the class.  */
57   tree name;
58
59   /* The _TYPE node for the class.  */
60   tree type;
61
62   /* The access specifier pending for new declarations in the scope of
63      this class.  */
64   tree access;
65 }* class_stack_node_t;
66
67 /* The stack itself.  This is an dynamically resized array.  The
68    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
69 static int current_class_stack_size;
70 static class_stack_node_t current_class_stack;
71
72 /* When we're processing a member function, current_class_ptr is the
73    PARM_DECL for the `this' pointer.  The current_class_ref is an
74    expression for `*this'.  */
75 tree current_class_ptr, current_class_ref;
76
77 /* The following two can be derived from the previous one */
78 tree current_class_name;        /* IDENTIFIER_NODE: name of current class */
79 tree current_class_type;        /* _TYPE: the type of the current class */
80 tree current_access_specifier;
81 tree previous_class_type;       /* _TYPE: the previous type that was a class */
82 tree previous_class_values;     /* TREE_LIST: copy of the class_shadowed list
83                                    when leaving an outermost class scope.  */
84
85 struct base_info;
86
87 static tree get_vfield_name PROTO((tree));
88 static void finish_struct_anon PROTO((tree));
89 static tree build_vbase_pointer PROTO((tree, tree));
90 static tree build_vtable_entry PROTO((tree, tree));
91 static tree get_vtable_name PROTO((tree));
92 static tree get_derived_offset PROTO((tree, tree));
93 static tree get_basefndecls PROTO((tree, tree));
94 static void set_rtti_entry PROTO((tree, tree, tree));
95 static tree build_vtable PROTO((tree, tree));
96 static void prepare_fresh_vtable PROTO((tree, tree));
97 static void fixup_vtable_deltas1 PROTO((tree, tree));
98 static void fixup_vtable_deltas PROTO((tree, int, tree));
99 static void finish_vtbls PROTO((tree, int, tree));
100 static void modify_vtable_entry PROTO((tree, tree, tree));
101 static tree get_vtable_entry_n PROTO((tree, unsigned HOST_WIDE_INT));
102 static void add_virtual_function PROTO((tree *, tree *, int *, tree, tree));
103 static tree delete_duplicate_fields_1 PROTO((tree, tree));
104 static void delete_duplicate_fields PROTO((tree));
105 static void finish_struct_bits PROTO((tree, int));
106 static int alter_access PROTO((tree, tree, tree, tree));
107 static void handle_using_decl PROTO((tree, tree, tree, tree));
108 static int overrides PROTO((tree, tree));
109 static int strictly_overrides PROTO((tree, tree));
110 static void merge_overrides PROTO((tree, tree, int, tree));
111 static void override_one_vtable PROTO((tree, tree, tree));
112 static void mark_overriders PROTO((tree, tree));
113 static void check_for_override PROTO((tree, tree));
114 static tree maybe_fixup_vptrs PROTO((tree, tree, tree));
115 static tree get_class_offset_1 PROTO((tree, tree, tree, tree, tree));
116 static tree get_class_offset PROTO((tree, tree, tree, tree));
117 static void modify_one_vtable PROTO((tree, tree, tree, tree));
118 static void modify_all_vtables PROTO((tree, tree, tree));
119 static void modify_all_direct_vtables PROTO((tree, int, tree, tree,
120                                              tree));
121 static void modify_all_indirect_vtables PROTO((tree, int, int, tree,
122                                                tree, tree));
123 static void build_class_init_list PROTO((tree));
124 static int finish_base_struct PROTO((tree, struct base_info *));
125 static void finish_struct_methods PROTO((tree));
126 static void maybe_warn_about_overly_private_class PROTO ((tree));
127 static void check_member_decl_is_same_in_complete_scope PROTO((tree, tree));
128 static tree make_method_vec PROTO((int));
129 static void free_method_vec PROTO((tree));
130 static tree add_implicitly_declared_members PROTO((tree, int, int, int));
131
132 /* Way of stacking language names.  */
133 tree *current_lang_base, *current_lang_stack;
134 int current_lang_stacksize;
135
136 /* Names of languages we recognize.  */
137 tree lang_name_c, lang_name_cplusplus, lang_name_java;
138 tree current_lang_name;
139
140 /* When layout out an aggregate type, the size of the
141    basetypes (virtual and non-virtual) is passed to layout_record
142    via this node.  */
143 static tree base_layout_decl;
144
145 /* Constants used for access control.  */
146 tree access_default_node; /* 0 */
147 tree access_public_node; /* 1 */
148 tree access_protected_node; /* 2 */
149 tree access_private_node; /* 3 */
150 tree access_default_virtual_node; /* 4 */
151 tree access_public_virtual_node; /* 5 */
152 tree access_protected_virtual_node; /* 6 */
153 tree access_private_virtual_node; /* 7 */
154
155 /* Variables shared between class.c and call.c.  */
156
157 #ifdef GATHER_STATISTICS
158 int n_vtables = 0;
159 int n_vtable_entries = 0;
160 int n_vtable_searches = 0;
161 int n_vtable_elems = 0;
162 int n_convert_harshness = 0;
163 int n_compute_conversion_costs = 0;
164 int n_build_method_call = 0;
165 int n_inner_fields_searched = 0;
166 #endif
167
168 /* Virtual baseclass things.  */
169
170 static tree
171 build_vbase_pointer (exp, type)
172      tree exp, type;
173 {
174   char *name;
175   FORMAT_VBASE_NAME (name, type);
176
177   return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
178 }
179
180 #if 0
181 /* Is the type of the EXPR, the complete type of the object?
182    If we are going to be wrong, we must be conservative, and return 0.  */
183
184 static int
185 complete_type_p (expr)
186      tree expr;
187 {
188   tree type = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
189   while (1)
190     {
191       switch (TREE_CODE (expr))
192         {
193         case SAVE_EXPR:
194         case INDIRECT_REF:
195         case ADDR_EXPR:
196         case NOP_EXPR:
197         case CONVERT_EXPR:
198           expr = TREE_OPERAND (expr, 0);
199           continue;
200
201         case CALL_EXPR: 
202           if (! TREE_HAS_CONSTRUCTOR (expr))
203             break;
204           /* fall through...  */
205         case VAR_DECL:
206         case FIELD_DECL:
207           if (TREE_CODE (TREE_TYPE (expr)) == ARRAY_TYPE
208               && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (expr)))
209               && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
210             return 1;
211           /* fall through...  */
212         case TARGET_EXPR:
213         case PARM_DECL:
214           if (IS_AGGR_TYPE (TREE_TYPE (expr))
215               && TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type)
216             return 1;
217           /* fall through...  */
218         case PLUS_EXPR:
219         default:
220           break;
221         }
222       break;
223     }
224   return 0;
225 }
226 #endif
227
228 /* Build multi-level access to EXPR using hierarchy path PATH.
229    CODE is PLUS_EXPR if we are going with the grain,
230    and MINUS_EXPR if we are not (in which case, we cannot traverse
231    virtual baseclass links).
232
233    TYPE is the type we want this path to have on exit.
234
235    NONNULL is non-zero if  we know (for any reason) that EXPR is
236    not, in fact, zero.  */
237
238 tree
239 build_vbase_path (code, type, expr, path, nonnull)
240      enum tree_code code;
241      tree type, expr, path;
242      int nonnull;
243 {
244   register int changed = 0;
245   tree last = NULL_TREE, last_virtual = NULL_TREE;
246   int fixed_type_p;
247   tree null_expr = 0, nonnull_expr;
248   tree basetype;
249   tree offset = integer_zero_node;
250
251   if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
252     return build1 (NOP_EXPR, type, expr);
253
254   /* If -fthis-is-variable, we might have set nonnull incorrectly.  We
255      don't care enough to get this right, so just clear it.  */
256   if (flag_this_is_variable > 0)
257     nonnull = 0;
258
259   /* We could do better if we had additional logic to convert back to the
260      unconverted type (the static type of the complete object), and then
261      convert back to the type we want.  Until that is done, we only optimize
262      if the complete type is the same type as expr has.  */
263   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
264
265   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
266     expr = save_expr (expr);
267   nonnull_expr = expr;
268
269   if (BINFO_INHERITANCE_CHAIN (path))
270     path = reverse_path (path);
271
272   basetype = BINFO_TYPE (path);
273
274   while (path)
275     {
276       if (TREE_VIA_VIRTUAL (path))
277         {
278           last_virtual = BINFO_TYPE (path);
279           if (code == PLUS_EXPR)
280             {
281               changed = ! fixed_type_p;
282
283               if (changed)
284                 {
285                   tree ind;
286
287                   /* We already check for ambiguous things in the caller, just
288                      find a path.  */
289                   if (last)
290                     {
291                       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
292                       nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
293                     }
294                   ind = build_indirect_ref (nonnull_expr, NULL_PTR);
295                   nonnull_expr = build_vbase_pointer (ind, last_virtual);
296                   if (nonnull == 0
297                       && TREE_CODE (type) == POINTER_TYPE
298                       && null_expr == NULL_TREE)
299                     {
300                       null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
301                       expr = build (COND_EXPR, build_pointer_type (last_virtual),
302                                     build (EQ_EXPR, boolean_type_node, expr,
303                                            integer_zero_node),
304                                     null_expr, nonnull_expr);
305                     }
306                 }
307               /* else we'll figure out the offset below.  */
308
309               /* Happens in the case of parse errors.  */
310               if (nonnull_expr == error_mark_node)
311                 return error_mark_node;
312             }
313           else
314             {
315               cp_error ("cannot cast up from virtual baseclass `%T'",
316                           last_virtual);
317               return error_mark_node;
318             }
319         }
320       last = path;
321       path = BINFO_INHERITANCE_CHAIN (path);
322     }
323   /* LAST is now the last basetype assoc on the path.  */
324
325   /* A pointer to a virtual base member of a non-null object
326      is non-null.  Therefore, we only need to test for zeroness once.
327      Make EXPR the canonical expression to deal with here.  */
328   if (null_expr)
329     {
330       TREE_OPERAND (expr, 2) = nonnull_expr;
331       TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
332         = TREE_TYPE (nonnull_expr);
333     }
334   else
335     expr = nonnull_expr;
336
337   /* If we go through any virtual base pointers, make sure that
338      casts to BASETYPE from the last virtual base class use
339      the right value for BASETYPE.  */
340   if (changed)
341     {
342       tree intype = TREE_TYPE (TREE_TYPE (expr));
343       if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
344         {
345           tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (intype), 0);
346           offset = BINFO_OFFSET (binfo);
347         }
348     }
349   else
350     {
351       if (last_virtual)
352         {
353           offset = BINFO_OFFSET (binfo_member (last_virtual,
354                                                CLASSTYPE_VBASECLASSES (basetype)));
355           offset = size_binop (PLUS_EXPR, offset, BINFO_OFFSET (last));
356         }
357       else
358         offset = BINFO_OFFSET (last);
359     }
360
361   if (TREE_INT_CST_LOW (offset))
362     {
363       /* Bash types to make the backend happy.  */
364       offset = cp_convert (type, offset);
365 #if 0
366       /* This shouldn't be necessary.  (mrs) */
367       expr = build1 (NOP_EXPR, type, expr);
368 #endif
369
370       /* If expr might be 0, we need to preserve that zeroness.  */
371       if (nonnull == 0)
372         {
373           if (null_expr)
374             TREE_TYPE (null_expr) = type;
375           else
376             null_expr = build1 (NOP_EXPR, type, integer_zero_node);
377           if (TREE_SIDE_EFFECTS (expr))
378             expr = save_expr (expr);
379
380           return build (COND_EXPR, type,
381                         build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
382                         null_expr,
383                         build (code, type, expr, offset));
384         }
385       else return build (code, type, expr, offset);
386     }
387
388   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
389      be used multiple times in initialization of multiple inheritance.  */
390   if (null_expr)
391     {
392       TREE_TYPE (expr) = type;
393       return expr;
394     }
395   else
396     return build1 (NOP_EXPR, type, expr);
397 }
398
399 /* Virtual function things.  */
400
401 /* Build an entry in the virtual function table.
402    DELTA is the offset for the `this' pointer.
403    PFN is an ADDR_EXPR containing a pointer to the virtual function.
404    Note that the index (DELTA2) in the virtual function table
405    is always 0.  */
406
407 static tree
408 build_vtable_entry (delta, pfn)
409      tree delta, pfn;
410 {
411   if (flag_vtable_thunks)
412     {
413       HOST_WIDE_INT idelta = TREE_INT_CST_LOW (delta);
414       if (idelta && ! DECL_ABSTRACT_VIRTUAL_P (TREE_OPERAND (pfn, 0)))
415         {
416           pfn = build1 (ADDR_EXPR, vtable_entry_type,
417                         make_thunk (pfn, idelta));
418           TREE_READONLY (pfn) = 1;
419           TREE_CONSTANT (pfn) = 1;
420         }
421 #ifdef GATHER_STATISTICS
422       n_vtable_entries += 1;
423 #endif
424       return pfn;
425     }
426   else
427     {
428       extern int flag_huge_objects;
429       tree elems = expr_tree_cons (NULL_TREE, delta,
430                               expr_tree_cons (NULL_TREE, integer_zero_node,
431                                          build_expr_list (NULL_TREE, pfn)));
432       tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
433
434       /* DELTA used to be constructed by `size_int' and/or size_binop,
435          which caused overflow problems when it was negative.  That should
436          be fixed now.  */
437
438       if (! int_fits_type_p (delta, delta_type_node))
439         {
440           if (flag_huge_objects)
441             sorry ("object size exceeds built-in limit for virtual function table implementation");
442           else
443             sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
444         }
445       
446       TREE_CONSTANT (entry) = 1;
447       TREE_STATIC (entry) = 1;
448       TREE_READONLY (entry) = 1;
449
450 #ifdef GATHER_STATISTICS
451       n_vtable_entries += 1;
452 #endif
453
454       return entry;
455     }
456 }
457
458 /* We want to give the assembler the vtable identifier as well as
459    the offset to the function pointer.  So we generate
460
461    __asm__ __volatile__ (".vtable_entry %c0, %c1"
462       : : "s"(&class_vtable),
463           "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
464
465 static void
466 build_vtable_entry_ref (basetype, vtbl, idx)
467      tree basetype, vtbl, idx;
468 {
469   static char asm_stmt[] = ".vtable_entry %c0, %c1";
470   tree s, i, i2;
471
472   s = build_unary_op (ADDR_EXPR, TYPE_BINFO_VTABLE (basetype), 0);
473   s = build_tree_list (build_string (1, "s"), s);
474
475   i = build_array_ref (vtbl, idx);
476   if (!flag_vtable_thunks)
477     i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
478   i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
479   i2 = build_array_ref (vtbl, build_int_2(0,0));
480   i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
481   i = build_binary_op (MINUS_EXPR, i, i2, 0);
482   i = build_tree_list (build_string (1, "i"), i);
483
484   expand_asm_operands (build_string (sizeof(asm_stmt)-1, asm_stmt),
485                        NULL_TREE, chainon (s, i), NULL_TREE, 1, NULL, 0);
486 }
487
488 /* Given an object INSTANCE, return an expression which yields the
489    virtual function vtable element corresponding to INDEX.  There are
490    many special cases for INSTANCE which we take care of here, mainly
491    to avoid creating extra tree nodes when we don't have to.  */
492
493 tree
494 build_vtbl_ref (instance, idx)
495      tree instance, idx;
496 {
497   tree vtbl, aref;
498   tree basetype = TREE_TYPE (instance);
499
500   if (TREE_CODE (basetype) == REFERENCE_TYPE)
501     basetype = TREE_TYPE (basetype);
502
503   if (instance == current_class_ref)
504     vtbl = build_vfield_ref (instance, basetype);
505   else
506     {
507       if (optimize)
508         {
509           /* Try to figure out what a reference refers to, and
510              access its virtual function table directly.  */
511           tree ref = NULL_TREE;
512
513           if (TREE_CODE (instance) == INDIRECT_REF
514               && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
515             ref = TREE_OPERAND (instance, 0);
516           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
517             ref = instance;
518
519           if (ref && TREE_CODE (ref) == VAR_DECL
520               && DECL_INITIAL (ref))
521             {
522               tree init = DECL_INITIAL (ref);
523
524               while (TREE_CODE (init) == NOP_EXPR
525                      || TREE_CODE (init) == NON_LVALUE_EXPR)
526                 init = TREE_OPERAND (init, 0);
527               if (TREE_CODE (init) == ADDR_EXPR)
528                 {
529                   init = TREE_OPERAND (init, 0);
530                   if (IS_AGGR_TYPE (TREE_TYPE (init))
531                       && (TREE_CODE (init) == PARM_DECL
532                           || TREE_CODE (init) == VAR_DECL))
533                     instance = init;
534                 }
535             }
536         }
537
538       if (IS_AGGR_TYPE (TREE_TYPE (instance))
539           && (TREE_CODE (instance) == RESULT_DECL
540               || TREE_CODE (instance) == PARM_DECL
541               || TREE_CODE (instance) == VAR_DECL))
542         vtbl = TYPE_BINFO_VTABLE (basetype);
543       else
544         vtbl = build_vfield_ref (instance, basetype);
545     }
546
547   assemble_external (vtbl);
548
549   if (flag_vtable_gc)
550     build_vtable_entry_ref (basetype, vtbl, idx);
551
552   aref = build_array_ref (vtbl, idx);
553
554   return aref;
555 }
556
557 /* Given an object INSTANCE, return an expression which yields the
558    virtual function corresponding to INDEX.  There are many special
559    cases for INSTANCE which we take care of here, mainly to avoid
560    creating extra tree nodes when we don't have to.  */
561
562 tree
563 build_vfn_ref (ptr_to_instptr, instance, idx)
564      tree *ptr_to_instptr, instance;
565      tree idx;
566 {
567   tree aref = build_vtbl_ref (instance, idx);
568
569   /* When using thunks, there is no extra delta, and we get the pfn
570      directly.  */
571   if (flag_vtable_thunks)
572     return aref;
573
574   if (ptr_to_instptr)
575     {
576       /* Save the intermediate result in a SAVE_EXPR so we don't have to
577          compute each component of the virtual function pointer twice.  */ 
578       if (TREE_CODE (aref) == INDIRECT_REF)
579         TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
580
581       *ptr_to_instptr
582         = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
583                  *ptr_to_instptr,
584                  cp_convert (ptrdiff_type_node,
585                              build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
586     }
587
588   return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
589 }
590
591 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
592    for the given TYPE.  */
593
594 static tree
595 get_vtable_name (type)
596      tree type;
597 {
598   tree type_id = build_typename_overload (type);
599   char *buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
600                                + IDENTIFIER_LENGTH (type_id) + 2);
601   char *ptr = IDENTIFIER_POINTER (type_id);
602   int i;
603   for (i = 0; ptr[i] == OPERATOR_TYPENAME_FORMAT[i]; i++) ;
604 #if 0
605   /* We don't take off the numbers; prepare_fresh_vtable uses the
606      DECL_ASSEMBLER_NAME for the type, which includes the number
607      in `3foo'.  If we were to pull them off here, we'd end up with
608      something like `_vt.foo.3bar', instead of a uniform definition.  */
609   while (ptr[i] >= '0' && ptr[i] <= '9')
610     i += 1;
611 #endif
612   sprintf (buf, VTABLE_NAME_FORMAT, ptr+i);
613   return get_identifier (buf);
614 }
615
616 /* Return the offset to the main vtable for a given base BINFO.  */
617
618 tree
619 get_vfield_offset (binfo)
620      tree binfo;
621 {
622   tree tmp
623     = size_binop (FLOOR_DIV_EXPR,
624                   DECL_FIELD_BITPOS (CLASSTYPE_VFIELD (BINFO_TYPE (binfo))),
625                   size_int (BITS_PER_UNIT));
626   tmp = convert (sizetype, tmp);
627   return size_binop (PLUS_EXPR, tmp, BINFO_OFFSET (binfo));
628 }
629
630 /* Get the offset to the start of the original binfo that we derived
631    this binfo from.  If we find TYPE first, return the offset only
632    that far.  The shortened search is useful because the this pointer
633    on method calling is expected to point to a DECL_CONTEXT (fndecl)
634    object, and not a baseclass of it.  */
635
636 static tree
637 get_derived_offset (binfo, type)
638      tree binfo, type;
639 {
640   tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
641   tree offset2;
642   int i;
643   while (BINFO_BASETYPES (binfo)
644          && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
645     {
646       tree binfos = BINFO_BASETYPES (binfo);
647       if (BINFO_TYPE (binfo) == type)
648         break;
649       binfo = TREE_VEC_ELT (binfos, i);
650     }
651   offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
652   return size_binop (MINUS_EXPR, offset1, offset2);
653 }
654
655 /* Update the rtti info for this class.  */
656
657 static void
658 set_rtti_entry (virtuals, offset, type)
659      tree virtuals, offset, type;
660 {
661   tree vfn;
662
663   if (flag_rtti)
664     vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, get_tinfo_fn (type));
665   else
666     vfn = build1 (NOP_EXPR, vfunc_ptr_type_node, size_zero_node);
667   TREE_CONSTANT (vfn) = 1;
668
669   if (! flag_vtable_thunks)
670     TREE_VALUE (virtuals) = build_vtable_entry (offset, vfn);
671   else
672     {
673       tree voff = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
674       TREE_CONSTANT (voff) = 1;
675
676       TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, voff);
677
678       /* The second slot is for the tdesc pointer when thunks are used.  */
679       TREE_VALUE (TREE_CHAIN (virtuals))
680         = build_vtable_entry (integer_zero_node, vfn);
681     }
682 }
683
684 /* Build a virtual function for type TYPE.
685    If BINFO is non-NULL, build the vtable starting with the initial
686    approximation that it is the same as the one which is the head of
687    the association list.  */
688
689 static tree
690 build_vtable (binfo, type)
691      tree binfo, type;
692 {
693   tree name = get_vtable_name (type);
694   tree virtuals, decl;
695
696   if (binfo)
697     {
698       tree offset;
699
700       virtuals = copy_list (BINFO_VIRTUALS (binfo));
701       decl = build_decl (VAR_DECL, name, TREE_TYPE (BINFO_VTABLE (binfo)));
702
703       /* Now do rtti stuff.  */
704       offset = get_derived_offset (TYPE_BINFO (type), NULL_TREE);
705       offset = ssize_binop (MINUS_EXPR, integer_zero_node, offset);
706       set_rtti_entry (virtuals, offset, type);
707     }
708   else
709     {
710       virtuals = NULL_TREE;
711       decl = build_decl (VAR_DECL, name, void_type_node);
712     }
713
714 #ifdef GATHER_STATISTICS
715   n_vtables += 1;
716   n_vtable_elems += list_length (virtuals);
717 #endif
718
719   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
720   import_export_vtable (decl, type, 0);
721
722   decl = pushdecl_top_level (decl);
723   SET_IDENTIFIER_GLOBAL_VALUE (name, decl);
724   /* Initialize the association list for this type, based
725      on our first approximation.  */
726   TYPE_BINFO_VTABLE (type) = decl;
727   TYPE_BINFO_VIRTUALS (type) = virtuals;
728
729   DECL_ARTIFICIAL (decl) = 1;
730   TREE_STATIC (decl) = 1;
731 #ifndef WRITABLE_VTABLES
732   /* Make them READONLY by default. (mrs) */
733   TREE_READONLY (decl) = 1;
734 #endif
735   /* At one time the vtable info was grabbed 2 words at a time.  This
736      fails on sparc unless you have 8-byte alignment.  (tiemann) */
737   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
738                            DECL_ALIGN (decl));
739
740   DECL_VIRTUAL_P (decl) = 1;
741   DECL_CONTEXT (decl) = type;
742
743   binfo = TYPE_BINFO (type);
744   SET_BINFO_NEW_VTABLE_MARKED (binfo);
745   return decl;
746 }
747
748 extern tree signed_size_zero_node;
749
750 /* Give TYPE a new virtual function table which is initialized
751    with a skeleton-copy of its original initialization.  The only
752    entry that changes is the `delta' entry, so we can really
753    share a lot of structure.
754
755    FOR_TYPE is the derived type which caused this table to
756    be needed.
757
758    BINFO is the type association which provided TYPE for FOR_TYPE.
759
760    The order in which vtables are built (by calling this function) for
761    an object must remain the same, otherwise a binary incompatibility
762    can result.  */
763
764 static void
765 prepare_fresh_vtable (binfo, for_type)
766      tree binfo, for_type;
767 {
768   tree basetype;
769   tree orig_decl = BINFO_VTABLE (binfo);
770   tree name;
771   tree new_decl;
772   tree offset;
773   tree path = binfo;
774   char *buf, *buf2;
775   char joiner = '_';
776   int i;
777
778 #ifdef JOINER
779   joiner = JOINER;
780 #endif
781
782   basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
783
784   buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
785   i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
786
787   /* We know that the vtable that we are going to create doesn't exist
788      yet in the global namespace, and when we finish, it will be
789      pushed into the global namespace.  In complex MI hierarchies, we
790      have to loop while the name we are thinking of adding is globally
791      defined, adding more name components to the vtable name as we
792      loop, until the name is unique.  This is because in complex MI
793      cases, we might have the same base more than once.  This means
794      that the order in which this function is called for vtables must
795      remain the same, otherwise binary compatibility can be
796      compromised.  */
797
798   while (1)
799     {
800       char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
801                                     + 1 + i);
802       char *new_buf2;
803
804       sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
805                buf2);
806       buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT) + strlen (buf1) + 1);
807       sprintf (buf, VTABLE_NAME_FORMAT, buf1);
808       name = get_identifier (buf);
809
810       /* If this name doesn't clash, then we can use it, otherwise
811          we add more to the name until it is unique.  */
812
813       if (! IDENTIFIER_GLOBAL_VALUE (name))
814         break;
815
816       /* Set values for next loop through, if the name isn't unique.  */
817
818       path = BINFO_INHERITANCE_CHAIN (path);
819
820       /* We better not run out of stuff to make it unique.  */
821       my_friendly_assert (path != NULL_TREE, 368);
822
823       basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
824
825       if (for_type == basetype)
826         {
827           /* If we run out of basetypes in the path, we have already
828              found created a vtable with that name before, we now
829              resort to tacking on _%d to distinguish them.  */
830           int j = 2;
831           i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
832           buf1 = (char *) alloca (i);
833           do {
834             sprintf (buf1, "%s%c%s%c%d",
835                      TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
836                      buf2, joiner, j);
837             buf = (char *) alloca (strlen (VTABLE_NAME_FORMAT)
838                                    + strlen (buf1) + 1);
839             sprintf (buf, VTABLE_NAME_FORMAT, buf1);
840             name = get_identifier (buf);
841
842             /* If this name doesn't clash, then we can use it,
843                otherwise we add something different to the name until
844                it is unique.  */
845           } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
846
847           /* Hey, they really like MI don't they?  Increase the 3
848              above to 6, and the 999 to 999999.  :-)  */
849           my_friendly_assert (j <= 999, 369);
850
851           break;
852         }
853
854       i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
855       new_buf2 = (char *) alloca (i);
856       sprintf (new_buf2, "%s%c%s",
857                TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
858       buf2 = new_buf2;
859     }
860
861   new_decl = build_decl (VAR_DECL, name, TREE_TYPE (orig_decl));
862   /* Remember which class this vtable is really for.  */
863   DECL_CONTEXT (new_decl) = for_type;
864
865   DECL_ARTIFICIAL (new_decl) = 1;
866   TREE_STATIC (new_decl) = 1;
867   BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
868   DECL_VIRTUAL_P (new_decl) = 1;
869 #ifndef WRITABLE_VTABLES
870   /* Make them READONLY by default. (mrs) */
871   TREE_READONLY (new_decl) = 1;
872 #endif
873   DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
874
875   /* Make fresh virtual list, so we can smash it later.  */
876   BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
877
878   if (TREE_VIA_VIRTUAL (binfo))
879     {
880       tree binfo1 = binfo_member (BINFO_TYPE (binfo), 
881                                   CLASSTYPE_VBASECLASSES (for_type));
882
883       /* XXX - This should never happen, if it does, the caller should
884          ensure that the binfo is from for_type's binfos, not from any
885          base type's.  We can remove all this code after a while.  */
886       if (binfo1 != binfo)
887         warning ("internal inconsistency: binfo offset error for rtti");
888
889       offset = BINFO_OFFSET (binfo1);
890     }
891   else
892     offset = BINFO_OFFSET (binfo);
893
894   set_rtti_entry (BINFO_VIRTUALS (binfo),
895                   ssize_binop (MINUS_EXPR, integer_zero_node, offset),
896                   for_type);
897
898 #ifdef GATHER_STATISTICS
899   n_vtables += 1;
900   n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
901 #endif
902
903   /* Set TREE_PUBLIC and TREE_EXTERN as appropriate.  */
904   import_export_vtable (new_decl, for_type, 0);
905
906   if (TREE_VIA_VIRTUAL (binfo))
907     my_friendly_assert (binfo == binfo_member (BINFO_TYPE (binfo),
908                                    CLASSTYPE_VBASECLASSES (current_class_type)),
909                         170);
910   SET_BINFO_NEW_VTABLE_MARKED (binfo);
911 }
912
913 #if 0
914 /* Access the virtual function table entry that logically
915    contains BASE_FNDECL.  VIRTUALS is the virtual function table's
916    initializer.  We can run off the end, when dealing with virtual
917    destructors in MI situations, return NULL_TREE in that case.  */
918
919 static tree
920 get_vtable_entry (virtuals, base_fndecl)
921      tree virtuals, base_fndecl;
922 {
923   unsigned HOST_WIDE_INT n = (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
924            ? (TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl))
925               & (((unsigned HOST_WIDE_INT)1<<(BITS_PER_WORD-1))-1))
926            : TREE_INT_CST_LOW (DECL_VINDEX (base_fndecl)));
927
928 #ifdef GATHER_STATISTICS
929   n_vtable_searches += n;
930 #endif
931
932   while (n > 0 && virtuals)
933     {
934       --n;
935       virtuals = TREE_CHAIN (virtuals);
936     }
937   return virtuals;
938 }
939 #endif
940
941 /* Put new entry ENTRY into virtual function table initializer
942    VIRTUALS.
943
944    Also update DECL_VINDEX (FNDECL).  */
945
946 static void
947 modify_vtable_entry (old_entry_in_list, new_entry, fndecl)
948      tree old_entry_in_list, new_entry, fndecl;
949 {
950   tree base_fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (old_entry_in_list)), 0);
951
952 #ifdef NOTQUITE
953   cp_warning ("replaced %D with %D", DECL_ASSEMBLER_NAME (base_fndecl),
954               DECL_ASSEMBLER_NAME (fndecl));
955 #endif
956   TREE_VALUE (old_entry_in_list) = new_entry;
957
958   /* Now assign virtual dispatch information, if unset.  */
959   /* We can dispatch this, through any overridden base function.  */
960   if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
961     {
962       DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
963       DECL_CONTEXT (fndecl) = DECL_CONTEXT (base_fndecl);
964     }
965 }
966
967 /* Access the virtual function table entry N.  VIRTUALS is the virtual
968    function table's initializer.  */
969
970 static tree
971 get_vtable_entry_n (virtuals, n)
972      tree virtuals;
973      unsigned HOST_WIDE_INT n;
974 {
975   while (n > 0)
976     {
977       --n;
978       virtuals = TREE_CHAIN (virtuals);
979     }
980   return virtuals;
981 }
982
983 /* Add a virtual function to all the appropriate vtables for the class
984    T.  DECL_VINDEX(X) should be error_mark_node, if we want to
985    allocate a new slot in our table.  If it is error_mark_node, we
986    know that no other function from another vtable is overridden by X.
987    HAS_VIRTUAL keeps track of how many virtuals there are in our main
988    vtable for the type, and we build upon the PENDING_VIRTUALS list
989    and return it.  */
990
991 static void
992 add_virtual_function (pv, phv, has_virtual, fndecl, t)
993      tree *pv, *phv;
994      int *has_virtual;
995      tree fndecl;
996      tree t; /* Structure type.  */
997 {
998   tree pending_virtuals = *pv;
999   tree pending_hard_virtuals = *phv;
1000
1001   /* FUNCTION_TYPEs and OFFSET_TYPEs no longer freely
1002      convert to void *.  Make such a conversion here.  */
1003   tree vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
1004   TREE_CONSTANT (vfn) = 1;
1005
1006 #ifndef DUMB_USER
1007   if (current_class_type == 0)
1008     cp_warning ("internal problem, current_class_type is zero when adding `%D', please report",
1009                 fndecl);
1010   if (current_class_type && t != current_class_type)
1011     cp_warning ("internal problem, current_class_type differs when adding `%D', please report",
1012                 fndecl);
1013 #endif
1014
1015   /* If the virtual function is a redefinition of a prior one,
1016      figure out in which base class the new definition goes,
1017      and if necessary, make a fresh virtual function table
1018      to hold that entry.  */
1019   if (DECL_VINDEX (fndecl) == error_mark_node)
1020     {
1021       tree entry;
1022
1023       /* We remember that this was the base sub-object for rtti.  */
1024       CLASSTYPE_RTTI (t) = t;
1025
1026       /* If we are using thunks, use two slots at the front, one
1027          for the offset pointer, one for the tdesc pointer.  */
1028       if (*has_virtual == 0 && flag_vtable_thunks)
1029         {
1030           *has_virtual = 1;
1031         }
1032
1033       /* Build a new INT_CST for this DECL_VINDEX.  */
1034       {
1035         static tree index_table[256];
1036         tree idx;
1037         /* We skip a slot for the offset/tdesc entry.  */
1038         int i = ++(*has_virtual);
1039
1040         if (i >= 256 || index_table[i] == 0)
1041           {
1042             idx = build_int_2 (i, 0);
1043             if (i < 256)
1044               index_table[i] = idx;
1045           }
1046         else
1047           idx = index_table[i];
1048
1049         /* Now assign virtual dispatch information.  */
1050         DECL_VINDEX (fndecl) = idx;
1051         DECL_CONTEXT (fndecl) = t;
1052       }
1053       entry = build_vtable_entry (integer_zero_node, vfn);
1054       pending_virtuals = tree_cons (DECL_VINDEX (fndecl), entry, pending_virtuals);
1055     }
1056   /* Might already be INTEGER_CST if declared twice in class.  We will
1057      give error later or we've already given it.  */
1058   else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
1059     {
1060       /* Need an entry in some other virtual function table.
1061          Deal with this after we have laid out our virtual base classes.  */
1062       pending_hard_virtuals = temp_tree_cons (fndecl, vfn, pending_hard_virtuals);
1063     }
1064   *pv = pending_virtuals;
1065   *phv = pending_hard_virtuals;
1066 }
1067 \f
1068 /* Obstack on which to build the vector of class methods.  */
1069 struct obstack class_obstack;
1070 extern struct obstack *current_obstack;
1071
1072 /* These are method vectors that were too small for the number of
1073    methods in some class, and so were abandoned.  */
1074 static tree free_method_vecs;
1075
1076 /* Returns a method vector with enough room for N methods.  N should
1077    be a power of two.  */
1078
1079 static tree
1080 make_method_vec (n)
1081      int n;
1082 {
1083   tree new_vec;
1084   tree* t;
1085   
1086   for (t = &free_method_vecs; *t; t = &(TREE_CHAIN (*t)))
1087     /* Note that we don't use >= n here because we don't want to
1088        allocate a very large vector where it isn't needed.  */
1089     if (TREE_VEC_LENGTH (*t) == n)
1090       {
1091         new_vec = *t;
1092         *t = TREE_CHAIN (new_vec);
1093         TREE_CHAIN (new_vec) = NULL_TREE;
1094         bzero ((PTR) &TREE_VEC_ELT (new_vec, 0), n * sizeof (tree));
1095         return new_vec;
1096       }
1097
1098   new_vec = make_tree_vec (n);
1099   return new_vec;
1100 }
1101
1102 /* Free the method vector VEC.  */
1103
1104 static void
1105 free_method_vec (vec)
1106      tree vec;
1107 {
1108   TREE_CHAIN (vec) = free_method_vecs;
1109   free_method_vecs = vec;
1110 }
1111
1112 /* Add method METHOD to class TYPE.
1113
1114    If non-NULL, FIELDS is the entry in the METHOD_VEC vector entry of
1115    the class type where the method should be added.  */
1116
1117 void
1118 add_method (type, fields, method)
1119      tree type, *fields, method;
1120 {
1121   push_obstacks (&permanent_obstack, &permanent_obstack);
1122
1123   /* Setting the DECL_CONTEXT and DECL_CLASS_CONTEXT here is probably
1124      redundant.  */
1125   DECL_CONTEXT (method) = type;
1126   DECL_CLASS_CONTEXT (method) = type;
1127   
1128   if (fields && *fields)
1129     *fields = build_overload (method, *fields);
1130   else 
1131     {
1132       int len;
1133       tree method_vec;
1134
1135       if (!CLASSTYPE_METHOD_VEC (type))
1136         /* Make a new method vector.  We start with 8 entries.  We must
1137            allocate at least two (for constructors and destructors), and
1138            we're going to end up with an assignment operator at some
1139            point as well.  
1140
1141            We could use a TREE_LIST for now, and convert it to a
1142            TREE_VEC in finish_struct, but we would probably waste more
1143            memory making the links in the list than we would by
1144            over-allocating the size of the vector here.  Furthermore,
1145            we would complicate all the code that expects this to be a
1146            vector.  We keep a free list of vectors that we outgrew so
1147            that we don't really waste any memory.  */
1148         CLASSTYPE_METHOD_VEC (type) = make_method_vec (8);
1149
1150       method_vec = CLASSTYPE_METHOD_VEC (type);
1151       len = TREE_VEC_LENGTH (method_vec);
1152
1153       if (DECL_NAME (method) == constructor_name (type))
1154         {
1155           /* A new constructor or destructor.  Constructors go in 
1156              slot 0; destructors go in slot 1.  */
1157           int slot 
1158             = DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (method)) ? 1 : 0;
1159
1160           TREE_VEC_ELT (method_vec, slot)
1161             = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1162         }
1163       else
1164         {
1165           int i;
1166
1167           /* See if we already have an entry with this name.  */
1168           for (i = 2; i < len; ++i)
1169             if (!TREE_VEC_ELT (method_vec, i)
1170                 || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i))) 
1171                     == DECL_NAME (method)))
1172               break;
1173                 
1174           if (i == len)
1175             {
1176               /* We need a bigger method vector.  */
1177               tree new_vec = make_method_vec (2 * len);
1178               bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
1179                      (PTR) &TREE_VEC_ELT (new_vec, 0),
1180                      len * sizeof (tree));
1181               free_method_vec (method_vec);
1182               len = 2 * len;
1183               method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
1184             }
1185           else if (template_class_depth (type))
1186             /* TYPE is a template class.  Don't issue any errors now;
1187                wait until instantiation time to complain.  */
1188               ;
1189           else
1190             {
1191               tree fns;
1192
1193               /* Check to see if we've already got this method.  */
1194               for (fns = TREE_VEC_ELT (method_vec, i);
1195                    fns;
1196                    fns = OVL_NEXT (fns))
1197                 {
1198                   tree fn = OVL_CURRENT (fns);
1199                  
1200                   if (TREE_CODE (fn) != TREE_CODE (method))
1201                     continue;
1202
1203                   if (TREE_CODE (method) != TEMPLATE_DECL)
1204                     {
1205                       /* Since this is an ordinary function in a
1206                          non-template class, it's mangled name can be
1207                          used as a unique identifier.  This technique
1208                          is only an optimization; we would get the
1209                          same results if we just used decls_match
1210                          here.  */
1211                       if (DECL_ASSEMBLER_NAME (fn) 
1212                           != DECL_ASSEMBLER_NAME (method))
1213                         continue;
1214                     }
1215                   else if (!decls_match (fn, method))
1216                     continue;
1217
1218                   /* There has already been a declaration of this
1219                      method or member template.  */
1220                   cp_error_at ("`%D' has already been declared in `%T'", 
1221                                method, type);
1222
1223                   /* We don't call duplicate_decls here to merege the
1224                      declarations because that will confuse things if
1225                      the methods have inline definitions In
1226                      particular, we will crash while processing the
1227                      definitions.  */
1228                   return;
1229                 }
1230             }
1231
1232           if (DECL_CONV_FN_P (method))
1233             {
1234               /* Type conversion operators have to come before
1235                  ordinary methods; add_conversions depends on this to
1236                  speed up looking for conversion operators.  So, if
1237                  necessary, we slide some of the vector elements up.
1238                  In theory, this makes this algorithm O(N^2) but we
1239                  don't expect many conversion operators.  */
1240               for (i = 2; i < len; ++i)
1241                 {
1242                   tree fn = TREE_VEC_ELT (method_vec, i);
1243
1244                   if (!fn)
1245                     /* There are no more entries in the vector, so we
1246                        can insert the new conversion operator here.  */
1247                     break;
1248                   
1249                   if (! DECL_CONV_FN_P (OVL_CURRENT (fn)))
1250                     /* We can insert the new function right at the Ith
1251                        position.  */
1252                     break;
1253                 }
1254
1255               if (!TREE_VEC_ELT (method_vec, i))
1256                 /* There is nothing in the Ith slot, so we can avoid
1257                    moving anything.  */
1258                 ; 
1259               else
1260                 {
1261                   /* We know the last slot in the vector is empty
1262                      because we know that at this point there's room for
1263                      a new function.  */
1264                   bcopy ((PTR) &TREE_VEC_ELT (method_vec, i),
1265                          (PTR) &TREE_VEC_ELT (method_vec, i + 1),
1266                          (len - i - 1) * sizeof (tree));
1267                   TREE_VEC_ELT (method_vec, i) = NULL_TREE;
1268                 }
1269             }
1270
1271           /* Actually insert the new method.  */
1272           TREE_VEC_ELT (method_vec, i) 
1273             = build_overload (method, TREE_VEC_ELT (method_vec, i));
1274         }
1275       
1276       if (TYPE_BINFO_BASETYPES (type) && CLASSTYPE_BASELINK_VEC (type))
1277         {
1278           /* ??? May be better to know whether these can be extended?  */
1279           tree baselink_vec = CLASSTYPE_BASELINK_VEC (type);
1280           
1281           TREE_VEC_LENGTH (baselink_vec) += 1;
1282           CLASSTYPE_BASELINK_VEC (type) = copy_node (baselink_vec);
1283           TREE_VEC_LENGTH (baselink_vec) -= 1;
1284           
1285           TREE_VEC_ELT (CLASSTYPE_BASELINK_VEC (type), len) = 0;
1286         }
1287     }
1288   pop_obstacks ();
1289 }
1290
1291 /* Subroutines of finish_struct.  */
1292
1293 /* Look through the list of fields for this struct, deleting
1294    duplicates as we go.  This must be recursive to handle
1295    anonymous unions.
1296
1297    FIELD is the field which may not appear anywhere in FIELDS.
1298    FIELD_PTR, if non-null, is the starting point at which
1299    chained deletions may take place.
1300    The value returned is the first acceptable entry found
1301    in FIELDS.
1302
1303    Note that anonymous fields which are not of UNION_TYPE are
1304    not duplicates, they are just anonymous fields.  This happens
1305    when we have unnamed bitfields, for example.  */
1306
1307 static tree
1308 delete_duplicate_fields_1 (field, fields)
1309      tree field, fields;
1310 {
1311   tree x;
1312   tree prev = 0;
1313   if (DECL_NAME (field) == 0)
1314     {
1315       if (TREE_CODE (TREE_TYPE (field)) != UNION_TYPE)
1316         return fields;
1317
1318       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1319         fields = delete_duplicate_fields_1 (x, fields);
1320       return fields;
1321     }
1322   else
1323     {
1324       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1325         {
1326           if (DECL_NAME (x) == 0)
1327             {
1328               if (TREE_CODE (TREE_TYPE (x)) != UNION_TYPE)
1329                 continue;
1330               TYPE_FIELDS (TREE_TYPE (x))
1331                 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1332               if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1333                 {
1334                   if (prev == 0)
1335                     fields = TREE_CHAIN (fields);
1336                   else
1337                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1338                 }
1339             }
1340           else
1341             {
1342               if (DECL_NAME (field) == DECL_NAME (x))
1343                 {
1344                   if (TREE_CODE (field) == CONST_DECL
1345                       && TREE_CODE (x) == CONST_DECL)
1346                     cp_error_at ("duplicate enum value `%D'", x);
1347                   else if (TREE_CODE (field) == CONST_DECL
1348                            || TREE_CODE (x) == CONST_DECL)
1349                     cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1350                                 x);
1351                   else if (DECL_DECLARES_TYPE_P (field)
1352                            && DECL_DECLARES_TYPE_P (x))
1353                     {
1354                       if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1355                         continue;
1356                       cp_error_at ("duplicate nested type `%D'", x);
1357                     }
1358                   else if (DECL_DECLARES_TYPE_P (field)
1359                            || DECL_DECLARES_TYPE_P (x))
1360                     {
1361                       /* Hide tag decls.  */
1362                       if ((TREE_CODE (field) == TYPE_DECL
1363                            && DECL_ARTIFICIAL (field))
1364                           || (TREE_CODE (x) == TYPE_DECL
1365                               && DECL_ARTIFICIAL (x)))
1366                         continue;
1367                       cp_error_at ("duplicate field `%D' (as type and non-type)",
1368                                    x);
1369                     }
1370                   else
1371                     cp_error_at ("duplicate member `%D'", x);
1372                   if (prev == 0)
1373                     fields = TREE_CHAIN (fields);
1374                   else
1375                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1376                 }
1377             }
1378         }
1379     }
1380   return fields;
1381 }
1382
1383 static void
1384 delete_duplicate_fields (fields)
1385      tree fields;
1386 {
1387   tree x;
1388   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1389     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1390 }
1391
1392 /* Change the access of FDECL to ACCESS in T.  The access to FDECL is
1393    along the path given by BINFO.  Return 1 if change was legit,
1394    otherwise return 0.  */
1395
1396 static int
1397 alter_access (t, binfo, fdecl, access)
1398      tree t;
1399      tree binfo;
1400      tree fdecl;
1401      tree access;
1402 {
1403   tree elem = purpose_member (t, DECL_ACCESS (fdecl));
1404   if (elem)
1405     {
1406       if (TREE_VALUE (elem) != access)
1407         {
1408           if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1409             cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1410           else
1411             error ("conflicting access specifications for field `%s', ignored",
1412                    IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1413         }
1414       else
1415         {
1416           /* They're changing the access to the same thing they changed
1417              it to before.  That's OK.  */
1418           ;
1419         }
1420     }
1421   else
1422     {
1423       enforce_access (binfo, fdecl);
1424
1425       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1426       return 1;
1427     }
1428   return 0;
1429 }
1430
1431 /* Process the USING_DECL, which is a member of T.  The METHOD_VEC, if
1432    non-NULL, is the methods of T.  The FIELDS are the fields of T.
1433    Returns 1 if the USING_DECL was valid, 0 otherwise.  */
1434
1435 void
1436 handle_using_decl (using_decl, t, method_vec, fields)
1437      tree using_decl;
1438      tree t;
1439      tree method_vec;
1440      tree fields;
1441 {
1442   tree ctype = DECL_INITIAL (using_decl);
1443   tree name = DECL_NAME (using_decl);
1444   tree access
1445     = TREE_PRIVATE (using_decl) ? access_private_node
1446     : TREE_PROTECTED (using_decl) ? access_protected_node
1447     : access_public_node;
1448   tree fdecl, binfo;
1449   tree flist = NULL_TREE;
1450   tree tmp;
1451   int i;
1452   int n_methods;
1453
1454   binfo = binfo_or_else (ctype, t);
1455   if (! binfo)
1456     return;
1457   
1458   if (name == constructor_name (ctype)
1459       || name == constructor_name_full (ctype))
1460     cp_error_at ("using-declaration for constructor", using_decl);
1461   
1462   fdecl = lookup_member (binfo, name, 0, 0);
1463   
1464   if (!fdecl)
1465     {
1466       cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1467       return;
1468     }
1469
1470   /* Functions are represented as TREE_LIST, with the purpose
1471      being the type and the value the functions. Other members
1472      come as themselves. */
1473   if (TREE_CODE (fdecl) == TREE_LIST)
1474     /* Ignore base type this came from. */
1475     fdecl = TREE_VALUE (fdecl);
1476
1477   if (TREE_CODE (fdecl) == OVERLOAD)
1478     {
1479       /* We later iterate over all functions. */
1480       flist = fdecl;
1481       fdecl = OVL_FUNCTION (flist);
1482     }
1483   
1484   name = DECL_NAME (fdecl);
1485   n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
1486   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); i++)
1487     if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
1488         == name)
1489       {
1490         cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1491         cp_error_at ("  because of local method `%#D' with same name",
1492                      OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
1493         return;
1494       }
1495
1496   if (! DECL_LANG_SPECIFIC (fdecl))
1497     /* We don't currently handle DECL_ACCESS for TYPE_DECLs; just return.  */
1498     return;
1499   
1500   for (tmp = fields; tmp; tmp = TREE_CHAIN (tmp))
1501     if (DECL_NAME (tmp) == name)
1502       {
1503         cp_error ("cannot adjust access to `%#D' in `%#T'", fdecl, t);
1504         cp_error_at ("  because of local field `%#D' with same name", tmp);
1505         return;
1506       }
1507   
1508   /* Make type T see field decl FDECL with access ACCESS.*/
1509   if (flist)
1510     {
1511       while (flist)
1512         {
1513           if (alter_access (t, binfo, OVL_FUNCTION (flist), 
1514                             access) == 0)
1515             return;
1516           flist = OVL_CHAIN (flist);
1517         }
1518     }
1519   else
1520     alter_access (t, binfo, fdecl, access);
1521 }
1522
1523 /* If FOR_TYPE needs to reinitialize virtual function table pointers
1524    for TYPE's sub-objects, add such reinitializations to BASE_INIT_LIST.
1525    Returns BASE_INIT_LIST appropriately modified.  */
1526
1527 static tree
1528 maybe_fixup_vptrs (for_type, binfo, base_init_list)
1529      tree for_type, binfo, base_init_list;
1530 {
1531   /* Now reinitialize any slots that don't fall under our virtual
1532      function table pointer.  */
1533   tree vfields = CLASSTYPE_VFIELDS (BINFO_TYPE (binfo));
1534   while (vfields)
1535     {
1536       tree basetype = VF_NORMAL_VALUE (vfields)
1537         ? TYPE_MAIN_VARIANT (VF_NORMAL_VALUE (vfields))
1538           : VF_BASETYPE_VALUE (vfields);
1539
1540       tree base_binfo = get_binfo (basetype, for_type, 0);
1541       /* Punt until this is implemented.  */
1542       if (1 /* BINFO_MODIFIED (base_binfo) */)
1543         {
1544           tree base_offset = get_vfield_offset (base_binfo);
1545           if (! tree_int_cst_equal (base_offset, get_vfield_offset (TYPE_BINFO (for_type)))
1546               && ! tree_int_cst_equal (base_offset, get_vfield_offset (binfo)))
1547             base_init_list = tree_cons (error_mark_node, base_binfo,
1548                                         base_init_list);
1549         }
1550       vfields = TREE_CHAIN (vfields);
1551     }
1552   return base_init_list;
1553 }
1554
1555 /* If TYPE does not have a constructor, then the compiler must
1556    manually deal with all of the initialization this type requires.
1557
1558    If a base initializer exists only to fill in the virtual function
1559    table pointer, then we mark that fact with the TREE_VIRTUAL bit.
1560    This way, we avoid multiple initializations of the same field by
1561    each virtual function table up the class hierarchy.
1562
1563    Virtual base class pointers are not initialized here.  They are
1564    initialized only at the "top level" of object creation.  If we
1565    initialized them here, we would have to skip a lot of work.  */
1566
1567 static void
1568 build_class_init_list (type)
1569      tree type;
1570 {
1571   tree base_init_list = NULL_TREE;
1572   tree member_init_list = NULL_TREE;
1573
1574   /* Since we build member_init_list and base_init_list using
1575      tree_cons, backwards fields the all through work.  */
1576   tree x;
1577   tree binfos = BINFO_BASETYPES (TYPE_BINFO (type));
1578   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1579
1580   for (x = TYPE_FIELDS (type); x; x = TREE_CHAIN (x))
1581     {
1582       if (TREE_CODE (x) != FIELD_DECL)
1583         continue;
1584
1585       if (TYPE_NEEDS_CONSTRUCTING (TREE_TYPE (x))
1586           || DECL_INITIAL (x) != NULL_TREE)
1587         member_init_list = tree_cons (x, type, member_init_list);
1588     }
1589   member_init_list = nreverse (member_init_list);
1590
1591   /* We will end up doing this last.  Need special marker
1592      to avoid infinite regress.  */
1593   if (TYPE_VIRTUAL_P (type))
1594     {
1595       base_init_list = build_tree_list (error_mark_node, TYPE_BINFO (type));
1596       if (CLASSTYPE_NEEDS_VIRTUAL_REINIT (type) == 0)
1597         TREE_VALUE (base_init_list) = NULL_TREE;
1598       TREE_ADDRESSABLE (base_init_list) = 1;
1599     }
1600
1601   /* Each base class which needs to have initialization
1602      of some kind gets to make such requests known here.  */
1603   for (i = n_baseclasses-1; i >= 0; i--)
1604     {
1605       tree base_binfo = TREE_VEC_ELT (binfos, i);
1606       tree blist;
1607
1608       /* Don't initialize virtual baseclasses this way.  */
1609       if (TREE_VIA_VIRTUAL (base_binfo))
1610         continue;
1611
1612       if (TYPE_HAS_CONSTRUCTOR (BINFO_TYPE (base_binfo)))
1613         {
1614           /* ...and the last shall come first...  */
1615           base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1616           base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1617           continue;
1618         }
1619
1620       if ((blist = CLASSTYPE_BASE_INIT_LIST (BINFO_TYPE (base_binfo))) == NULL_TREE)
1621         /* Nothing to initialize.  */
1622         continue;
1623
1624       /* ...ditto...  */
1625       base_init_list = maybe_fixup_vptrs (type, base_binfo, base_init_list);
1626
1627       /* This is normally true for single inheritance.
1628          The win is we can shrink the chain of initializations
1629          to be done by only converting to the actual type
1630          we are interested in.  */
1631       if (TREE_VALUE (blist)
1632           && TREE_CODE (TREE_VALUE (blist)) == TREE_VEC
1633           && tree_int_cst_equal (BINFO_OFFSET (base_binfo),
1634                                  BINFO_OFFSET (TREE_VALUE (blist))))
1635         {
1636           if (base_init_list)
1637             {
1638               /* Does it do more than just fill in a
1639                  virtual function table pointer?  */
1640               if (! TREE_ADDRESSABLE (blist))
1641                 base_init_list = build_tree_list (blist, base_init_list);
1642               /* Can we get by just with the virtual function table
1643                  pointer that it fills in?  */
1644               else if (TREE_ADDRESSABLE (base_init_list)
1645                        && TREE_VALUE (base_init_list) == 0)
1646                 base_init_list = blist;
1647               /* Maybe, but it is not obvious as the previous case.  */
1648               else if (! CLASSTYPE_NEEDS_VIRTUAL_REINIT (type))
1649                 {
1650                   tree last = tree_last (base_init_list);
1651                   while (TREE_VALUE (last)
1652                          && TREE_CODE (TREE_VALUE (last)) == TREE_LIST)
1653                     last = tree_last (TREE_VALUE (last));
1654                   if (TREE_VALUE (last) == 0)
1655                     base_init_list = build_tree_list (blist, base_init_list);
1656                 }
1657             }
1658           else
1659             base_init_list = blist;
1660         }
1661       else
1662         {
1663           /* The function expand_aggr_init knows how to do the
1664              initialization of `basetype' without getting
1665              an explicit `blist'.  */
1666           if (base_init_list)
1667             base_init_list = tree_cons (NULL_TREE, base_binfo, base_init_list);
1668           else
1669             base_init_list = CLASSTYPE_BINFO_AS_LIST (BINFO_TYPE (base_binfo));
1670         }
1671     }
1672
1673   if (base_init_list)
1674     {
1675       if (member_init_list)
1676         CLASSTYPE_BASE_INIT_LIST (type) =
1677           build_tree_list (base_init_list, member_init_list);
1678       else
1679         CLASSTYPE_BASE_INIT_LIST (type) = base_init_list;
1680     }
1681   else if (member_init_list)
1682     CLASSTYPE_BASE_INIT_LIST (type) = member_init_list;
1683 }
1684 \f
1685 struct base_info
1686 {
1687   int has_virtual;
1688   int max_has_virtual;
1689   int n_ancestors;
1690   tree vfield;
1691   tree vfields;
1692   tree rtti;
1693   char cant_have_default_ctor;
1694   char cant_have_const_ctor;
1695   char no_const_asn_ref;
1696 };
1697
1698 /* Record information about type T derived from its base classes.
1699    Store most of that information in T itself, and place the
1700    remaining information in the struct BASE_INFO.
1701
1702    Propagate basetype offsets throughout the lattice.  Note that the
1703    lattice topped by T is really a pair: it's a DAG that gives the
1704    structure of the derivation hierarchy, and it's a list of the
1705    virtual baseclasses that appear anywhere in the DAG.  When a vbase
1706    type appears in the DAG, it's offset is 0, and it's children start
1707    their offsets from that point.  When a vbase type appears in the list,
1708    its offset is the offset it has in the hierarchy, and its children's
1709    offsets include that offset in theirs.
1710
1711    Returns the index of the first base class to have virtual functions,
1712    or -1 if no such base class.  */
1713
1714 static int
1715 finish_base_struct (t, b)
1716      tree t;
1717      struct base_info *b;
1718 {
1719   tree binfos = TYPE_BINFO_BASETYPES (t);
1720   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
1721   int first_vfn_base_index = -1;
1722   bzero ((char *) b, sizeof (struct base_info));
1723
1724   for (i = 0; i < n_baseclasses; i++)
1725     {
1726       tree base_binfo = TREE_VEC_ELT (binfos, i);
1727       tree basetype = BINFO_TYPE (base_binfo);
1728
1729       /* Effective C++ rule 14.  We only need to check TYPE_VIRTUAL_P
1730          here because the case of virtual functions but non-virtual
1731          dtor is handled in finish_struct_1.  */
1732       if (warn_ecpp && ! TYPE_VIRTUAL_P (basetype)
1733           && TYPE_HAS_DESTRUCTOR (basetype))
1734         cp_warning ("base class `%#T' has a non-virtual destructor", basetype);
1735
1736       /* If the type of basetype is incomplete, then
1737          we already complained about that fact
1738          (and we should have fixed it up as well).  */
1739       if (TYPE_SIZE (basetype) == 0)
1740         {
1741           int j;
1742           /* The base type is of incomplete type.  It is
1743              probably best to pretend that it does not
1744              exist.  */
1745           if (i == n_baseclasses-1)
1746             TREE_VEC_ELT (binfos, i) = NULL_TREE;
1747           TREE_VEC_LENGTH (binfos) -= 1;
1748           n_baseclasses -= 1;
1749           for (j = i; j+1 < n_baseclasses; j++)
1750             TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1751         }
1752
1753       if (! TYPE_HAS_CONST_INIT_REF (basetype))
1754         b->cant_have_const_ctor = 1;
1755
1756       if (TYPE_HAS_CONSTRUCTOR (basetype)
1757           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1758         {
1759           b->cant_have_default_ctor = 1;
1760           if (! TYPE_HAS_CONSTRUCTOR (t))
1761             {
1762               cp_pedwarn ("base `%T' with only non-default constructor",
1763                           basetype);
1764               cp_pedwarn ("in class without a constructor");
1765             }
1766         }
1767
1768       if (TYPE_HAS_ASSIGN_REF (basetype)
1769           && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1770         b->no_const_asn_ref = 1;
1771
1772       b->n_ancestors += CLASSTYPE_N_SUPERCLASSES (basetype);
1773       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1774       TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (basetype);
1775       TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1776       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1777
1778       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1779       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1780       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1781
1782       if (! TREE_VIA_VIRTUAL (base_binfo))
1783         CLASSTYPE_N_SUPERCLASSES (t) += 1;
1784
1785       if (TYPE_VIRTUAL_P (basetype))
1786         {
1787           /* Ensure that this is set from at least a virtual base
1788              class.  */
1789           if (b->rtti == NULL_TREE)
1790             b->rtti = CLASSTYPE_RTTI (basetype);
1791
1792           /* Don't borrow virtuals from virtual baseclasses.  */
1793           if (TREE_VIA_VIRTUAL (base_binfo))
1794             continue;
1795
1796           if (first_vfn_base_index < 0)
1797             {
1798               tree vfields;
1799               first_vfn_base_index = i;
1800
1801               /* Update these two, now that we know what vtable we are
1802                  going to extend.  This is so that we can add virtual
1803                  functions, and override them properly.  */
1804               TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1805               TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1806               b->has_virtual = CLASSTYPE_VSIZE (basetype);
1807               b->vfield = CLASSTYPE_VFIELD (basetype);
1808               b->vfields = copy_list (CLASSTYPE_VFIELDS (basetype));
1809               vfields = b->vfields;
1810               while (vfields)
1811                 {
1812                   if (VF_BINFO_VALUE (vfields) == NULL_TREE
1813                       || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1814                     {
1815                       tree value = VF_BASETYPE_VALUE (vfields);
1816                       if (DECL_NAME (CLASSTYPE_VFIELD (value))
1817                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1818                         VF_NORMAL_VALUE (b->vfields) = basetype;
1819                       else
1820                         VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1821                     }
1822                   vfields = TREE_CHAIN (vfields);
1823                 }
1824               CLASSTYPE_VFIELD (t) = b->vfield;
1825             }
1826           else
1827             {
1828               /* Only add unique vfields, and flatten them out as we go.  */
1829               tree vfields = CLASSTYPE_VFIELDS (basetype);
1830               while (vfields)
1831                 {
1832                   if (VF_BINFO_VALUE (vfields) == NULL_TREE
1833                       || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1834                     {
1835                       tree value = VF_BASETYPE_VALUE (vfields);
1836                       b->vfields = tree_cons (base_binfo, value, b->vfields);
1837                       if (DECL_NAME (CLASSTYPE_VFIELD (value))
1838                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1839                         VF_NORMAL_VALUE (b->vfields) = basetype;
1840                       else
1841                         VF_NORMAL_VALUE (b->vfields) = VF_NORMAL_VALUE (vfields);
1842                     }
1843                   vfields = TREE_CHAIN (vfields);
1844                 }
1845
1846               if (b->has_virtual == 0)
1847                 {
1848                   first_vfn_base_index = i;
1849
1850                   /* Update these two, now that we know what vtable we are
1851                      going to extend.  This is so that we can add virtual
1852                      functions, and override them properly.  */
1853                   TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1854                   TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1855                   b->has_virtual = CLASSTYPE_VSIZE (basetype);
1856                   b->vfield = CLASSTYPE_VFIELD (basetype);
1857                   CLASSTYPE_VFIELD (t) = b->vfield;
1858                   /* When we install the first one, set the VF_NORMAL_VALUE
1859                      to be the current class, as this it is the most derived
1860                      class.  Hopefully, this is not set to something else
1861                      later.  (mrs) */
1862                   vfields = b->vfields;
1863                   while (vfields)
1864                     {
1865                       if (DECL_NAME (CLASSTYPE_VFIELD (t))
1866                           == DECL_NAME (CLASSTYPE_VFIELD (basetype)))
1867                         {
1868                           VF_NORMAL_VALUE (vfields) = t;
1869                           /* There should only be one of them!  And it should
1870                              always be found, if we get into here.  (mrs)  */
1871                           break;
1872                         }
1873                       vfields = TREE_CHAIN (vfields);
1874                     }
1875                 }
1876             }
1877         }
1878     }
1879
1880   {
1881     tree vfields;
1882     /* Find the base class with the largest number of virtual functions.  */
1883     for (vfields = b->vfields; vfields; vfields = TREE_CHAIN (vfields))
1884       {
1885         if (CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields)) > b->max_has_virtual)
1886           b->max_has_virtual = CLASSTYPE_VSIZE (VF_BASETYPE_VALUE (vfields));
1887         if (VF_DERIVED_VALUE (vfields)
1888             && CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields)) > b->max_has_virtual)
1889           b->max_has_virtual = CLASSTYPE_VSIZE (VF_DERIVED_VALUE (vfields));
1890       }
1891   }
1892
1893   if (b->vfield == 0)
1894     /* If all virtual functions come only from virtual baseclasses.  */
1895     return -1;
1896
1897   /* Update the rtti base if we have a non-virtual base class version
1898      of it.  */
1899   b->rtti = CLASSTYPE_RTTI (BINFO_TYPE (TREE_VEC_ELT (binfos, first_vfn_base_index)));
1900
1901   return first_vfn_base_index;
1902 }
1903 \f
1904 /* Set memoizing fields and bits of T (and its variants) for later use.
1905    MAX_HAS_VIRTUAL is the largest size of any T's virtual function tables.  */
1906
1907 static void
1908 finish_struct_bits (t, max_has_virtual)
1909      tree t;
1910      int max_has_virtual;
1911 {
1912   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1913
1914   /* Fix up variants (if any).  */
1915   tree variants = TYPE_NEXT_VARIANT (t);
1916   while (variants)
1917     {
1918       /* These fields are in the _TYPE part of the node, not in
1919          the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1920       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1921       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1922       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1923       TYPE_NEEDS_DESTRUCTOR (variants) = TYPE_NEEDS_DESTRUCTOR (t);
1924
1925       TYPE_USES_COMPLEX_INHERITANCE (variants) = TYPE_USES_COMPLEX_INHERITANCE (t);
1926       TYPE_VIRTUAL_P (variants) = TYPE_VIRTUAL_P (t);
1927       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1928       /* Copy whatever these are holding today.  */
1929       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1930       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1931       TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1932       TYPE_SIZE (variants) = TYPE_SIZE (t);
1933       TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1934       variants = TYPE_NEXT_VARIANT (variants);
1935     }
1936
1937   if (n_baseclasses && max_has_virtual)
1938     {
1939       /* Done by `finish_struct' for classes without baseclasses.  */
1940       int might_have_abstract_virtuals = CLASSTYPE_ABSTRACT_VIRTUALS (t) != 0;
1941       tree binfos = TYPE_BINFO_BASETYPES (t);
1942       for (i = n_baseclasses-1; i >= 0; i--)
1943         {
1944           might_have_abstract_virtuals
1945             |= (CLASSTYPE_ABSTRACT_VIRTUALS (BINFO_TYPE (TREE_VEC_ELT (binfos, i))) != 0);
1946           if (might_have_abstract_virtuals)
1947             break;
1948         }
1949       if (might_have_abstract_virtuals)
1950         {
1951           /* We use error_mark_node from override_one_vtable to signal
1952              an artificial abstract.  */
1953           if (CLASSTYPE_ABSTRACT_VIRTUALS (t) == error_mark_node)
1954             CLASSTYPE_ABSTRACT_VIRTUALS (t) = NULL_TREE;
1955           CLASSTYPE_ABSTRACT_VIRTUALS (t) = get_abstract_virtuals (t);
1956         }
1957     }
1958
1959   if (n_baseclasses)
1960     {
1961       /* Notice whether this class has type conversion functions defined.  */
1962       tree binfo = TYPE_BINFO (t);
1963       tree binfos = BINFO_BASETYPES (binfo);
1964       tree basetype;
1965
1966       for (i = n_baseclasses-1; i >= 0; i--)
1967         {
1968           basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1969
1970           TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1971           if (CLASSTYPE_MAX_DEPTH (basetype) >= CLASSTYPE_MAX_DEPTH (t))
1972             CLASSTYPE_MAX_DEPTH (t) = CLASSTYPE_MAX_DEPTH (basetype) + 1;
1973         }
1974     }
1975
1976   /* If this type has a copy constructor, force its mode to be BLKmode, and
1977      force its TREE_ADDRESSABLE bit to be nonzero.  This will cause it to
1978      be passed by invisible reference and prevent it from being returned in
1979      a register.
1980
1981      Also do this if the class has BLKmode but can still be returned in
1982      registers, since function_cannot_inline_p won't let us inline
1983      functions returning such a type.  This affects the HP-PA.  */
1984   if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1985       || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1986           && CLASSTYPE_NON_AGGREGATE (t)))
1987     {
1988       tree variants;
1989       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1990       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1991         {
1992           TYPE_MODE (variants) = BLKmode;
1993           TREE_ADDRESSABLE (variants) = 1;
1994         }
1995     }
1996 }
1997
1998 /* Issue warnings about T having private constructors, but no friends,
1999    and so forth.  
2000
2001    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
2002    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
2003    non-private static member functions.  */
2004
2005 static void
2006 maybe_warn_about_overly_private_class (t)
2007      tree t;
2008 {
2009   int has_member_fn = 0;
2010   int has_nonprivate_method = 0;
2011   tree fn;
2012
2013   if (!warn_ctor_dtor_privacy
2014       /* If the class has friends, those entities might create and
2015          access instances, so we should not warn.  */
2016       || (CLASSTYPE_FRIEND_CLASSES (t)
2017           || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
2018       /* We will have warned when the template was declared; there's
2019          no need to warn on every instantiation.  */
2020       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
2021     /* There's no reason to even consider warning about this 
2022        class.  */
2023     return;
2024     
2025   /* We only issue one warning, if more than one applies, because
2026      otherwise, on code like:
2027
2028      class A {
2029        // Oops - forgot `public:'
2030        A();
2031        A(const A&);
2032        ~A();
2033      };
2034
2035      we warn several times about essentially the same problem.  */
2036
2037   /* Check to see if all (non-constructor, non-destructor) member
2038      functions are private.  (Since there are no friends or
2039      non-private statics, we can't ever call any of the private member
2040      functions.)  */
2041   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
2042     /* We're not interested in compiler-generated methods; they don't
2043        provide any way to call private members.  */
2044     if (!DECL_ARTIFICIAL (fn)) 
2045       {
2046         if (!TREE_PRIVATE (fn))
2047           {
2048             if (DECL_STATIC_FUNCTION_P (fn)) 
2049               /* A non-private static member function is just like a
2050                  friend; it can create and invoke private member
2051                  functions, and be accessed without a class
2052                  instance.  */
2053               return;
2054                 
2055             has_nonprivate_method = 1;
2056             break;
2057           }
2058         else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
2059           has_member_fn = 1;
2060       } 
2061
2062   if (!has_nonprivate_method && has_member_fn) 
2063     {
2064       /* There are no non-private methods, and there's at least one
2065          private member function that isn't a constructor or
2066          destructor.  (If all the private members are
2067          constructors/destructors we want to use the code below that
2068          issues error messages specifically referring to
2069          constructors/destructors.)  */
2070       int i;
2071       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2072       for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
2073         if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
2074             || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
2075           {
2076             has_nonprivate_method = 1;
2077             break;
2078           }
2079       if (!has_nonprivate_method) 
2080         {
2081           cp_warning ("all member functions in class `%T' are private", t);
2082           return;
2083         }
2084     }
2085
2086   /* Even if some of the member functions are non-private, the class
2087      won't be useful for much if all the constructors or destructors
2088      are private: such an object can never be created or destroyed.  */
2089   if (TYPE_HAS_DESTRUCTOR (t))
2090     {
2091       tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2092
2093       if (TREE_PRIVATE (dtor))
2094         {
2095           cp_warning ("`%#T' only defines a private destructor and has no friends",
2096                       t);
2097           return;
2098         }
2099     }
2100
2101   if (TYPE_HAS_CONSTRUCTOR (t))
2102     {
2103       int nonprivate_ctor = 0;
2104           
2105       /* If a non-template class does not define a copy
2106          constructor, one is defined for it, enabling it to avoid
2107          this warning.  For a template class, this does not
2108          happen, and so we would normally get a warning on:
2109
2110            template <class T> class C { private: C(); };  
2111           
2112          To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
2113          complete non-template or fully instantiated classes have this
2114          flag set.  */
2115       if (!TYPE_HAS_INIT_REF (t))
2116         nonprivate_ctor = 1;
2117       else 
2118         for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2119              fn;
2120              fn = OVL_NEXT (fn)) 
2121           {
2122             tree ctor = OVL_CURRENT (fn);
2123             /* Ideally, we wouldn't count copy constructors (or, in
2124                fact, any constructor that takes an argument of the
2125                class type as a parameter) because such things cannot
2126                be used to construct an instance of the class unless
2127                you already have one.  But, for now at least, we're
2128                more generous.  */
2129             if (! TREE_PRIVATE (ctor))
2130               {
2131                 nonprivate_ctor = 1;
2132                 break;
2133               }
2134           }
2135
2136       if (nonprivate_ctor == 0)
2137         {
2138           cp_warning ("`%#T' only defines private constructors and has no friends",
2139                       t);
2140           return;
2141         }
2142     }
2143 }
2144
2145
2146 /* Warn about duplicate methods in fn_fields.  Also compact method
2147    lists so that lookup can be made faster.
2148
2149    Data Structure: List of method lists.  The outer list is a
2150    TREE_LIST, whose TREE_PURPOSE field is the field name and the
2151    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
2152    links the entire list of methods for TYPE_METHODS.  Friends are
2153    chained in the same way as member functions (? TREE_CHAIN or
2154    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2155    list.  That allows them to be quickly deleted, and requires no
2156    extra storage.
2157
2158    If there are any constructors/destructors, they are moved to the
2159    front of the list.  This makes pushclass more efficient.
2160
2161    We also link each field which has shares a name with its baseclass
2162    to the head of the list of fields for that base class.  This allows
2163    us to reduce search time in places like `build_method_call' to
2164    consider only reasonably likely functions.   */
2165
2166 static void
2167 finish_struct_methods (t)
2168      tree t;
2169 {
2170   tree fn_fields;
2171   tree method_vec = CLASSTYPE_METHOD_VEC (t);
2172   tree ctor_name = constructor_name (t);
2173   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
2174
2175   /* First fill in entry 0 with the constructors, entry 1 with destructors,
2176      and the next few with type conversion operators (if any).  */
2177   for (fn_fields = TYPE_METHODS (t); fn_fields; 
2178        fn_fields = TREE_CHAIN (fn_fields))
2179     {
2180       tree fn_name = DECL_NAME (fn_fields);
2181
2182       /* Clear out this flag.
2183
2184          @@ Doug may figure out how to break
2185          @@ this with nested classes and friends.  */
2186       DECL_IN_AGGR_P (fn_fields) = 0;
2187
2188       /* Note here that a copy ctor is private, so we don't dare generate
2189          a default copy constructor for a class that has a member
2190          of this type without making sure they have access to it.  */
2191       if (fn_name == ctor_name)
2192         {
2193           tree parmtypes = FUNCTION_ARG_CHAIN (fn_fields);
2194           tree parmtype = parmtypes ? TREE_VALUE (parmtypes) : void_type_node;
2195           
2196           if (TREE_CODE (parmtype) == REFERENCE_TYPE
2197               && TYPE_MAIN_VARIANT (TREE_TYPE (parmtype)) == t)
2198             {
2199               if (TREE_CHAIN (parmtypes) == NULL_TREE
2200                   || TREE_CHAIN (parmtypes) == void_list_node
2201                   || TREE_PURPOSE (TREE_CHAIN (parmtypes)))
2202                 {
2203                   if (TREE_PROTECTED (fn_fields))
2204                     TYPE_HAS_NONPUBLIC_CTOR (t) = 1;
2205                   else if (TREE_PRIVATE (fn_fields))
2206                     TYPE_HAS_NONPUBLIC_CTOR (t) = 2;
2207                 }
2208             }
2209         }
2210       else if (fn_name == ansi_opname[(int) MODIFY_EXPR])
2211         {
2212           tree parmtype = TREE_VALUE (FUNCTION_ARG_CHAIN (fn_fields));
2213
2214           if (copy_assignment_arg_p (parmtype, DECL_VIRTUAL_P (fn_fields)))
2215             {
2216               if (TREE_PROTECTED (fn_fields))
2217                 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 1;
2218               else if (TREE_PRIVATE (fn_fields))
2219                 TYPE_HAS_NONPUBLIC_ASSIGN_REF (t) = 2;
2220             }
2221         }
2222     }
2223
2224   if (TYPE_HAS_DESTRUCTOR (t) && !TREE_VEC_ELT (method_vec, 1))
2225     /* We thought there was a destructor, but there wasn't.  Some
2226        parse errors cause this anomalous situation.  */
2227     TYPE_HAS_DESTRUCTOR (t) = 0;
2228     
2229   /* Issue warnings about private constructors and such.  If there are
2230      no methods, then some public defaults are generated.  */
2231   maybe_warn_about_overly_private_class (t); 
2232
2233   /* Now for each member function (except for constructors and
2234      destructors), compute where member functions of the same
2235      name reside in base classes.  */
2236   if (n_baseclasses != 0
2237       && method_vec
2238       && TREE_VEC_LENGTH (method_vec) > 2)
2239     {
2240       int len = TREE_VEC_LENGTH (method_vec);
2241       tree baselink_vec = make_tree_vec (len);
2242       int any_links = 0;
2243       tree baselink_binfo = build_tree_list (NULL_TREE, TYPE_BINFO (t));
2244
2245       for (i = 2; i < len && TREE_VEC_ELT (method_vec, i); i++)
2246         {
2247           tree ovl = TREE_VEC_ELT (method_vec, i);
2248
2249           TREE_VEC_ELT (baselink_vec, i)
2250             = get_baselinks (baselink_binfo, t, 
2251                              DECL_NAME (OVL_CURRENT (ovl)));
2252           if (TREE_VEC_ELT (baselink_vec, i) != 0)
2253             any_links = 1;
2254         }
2255       if (any_links != 0)
2256         CLASSTYPE_BASELINK_VEC (t) = baselink_vec;
2257       else
2258         obstack_free (current_obstack, baselink_vec);
2259     }
2260 }
2261
2262 /* Emit error when a duplicate definition of a type is seen.  Patch up.  */
2263
2264 void
2265 duplicate_tag_error (t)
2266      tree t;
2267 {
2268   cp_error ("redefinition of `%#T'", t);
2269   cp_error_at ("previous definition here", t);
2270
2271   /* Pretend we haven't defined this type.  */
2272
2273   /* All of the component_decl's were TREE_CHAINed together in the parser.
2274      finish_struct_methods walks these chains and assembles all methods with
2275      the same base name into DECL_CHAINs. Now we don't need the parser chains
2276      anymore, so we unravel them.  */
2277
2278   /* This used to be in finish_struct, but it turns out that the
2279      TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2280      things...  */
2281   if (CLASSTYPE_METHOD_VEC (t)) 
2282     {
2283       tree method_vec = CLASSTYPE_METHOD_VEC (t);
2284       int i, len  = TREE_VEC_LENGTH (method_vec);
2285       for (i = 0; i < len; i++)
2286         {
2287           tree unchain = TREE_VEC_ELT (method_vec, i);
2288           while (unchain != NULL_TREE) 
2289             {
2290               TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2291               unchain = OVL_NEXT (unchain);
2292             }
2293         }
2294     }
2295
2296   if (TYPE_LANG_SPECIFIC (t))
2297     {
2298       tree as_list = CLASSTYPE_AS_LIST (t);
2299       tree binfo = TYPE_BINFO (t);
2300       tree binfo_as_list = CLASSTYPE_BINFO_AS_LIST (t);
2301       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2302       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2303
2304       bzero ((char *) TYPE_LANG_SPECIFIC (t), sizeof (struct lang_type));
2305       BINFO_BASETYPES(binfo) = NULL_TREE;
2306
2307       CLASSTYPE_AS_LIST (t) = as_list;
2308       TYPE_BINFO (t) = binfo;
2309       CLASSTYPE_BINFO_AS_LIST (t) = binfo_as_list;
2310       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2311       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2312       TYPE_REDEFINED (t) = 1;
2313     }
2314   TYPE_SIZE (t) = NULL_TREE;
2315   TYPE_MODE (t) = VOIDmode;
2316   TYPE_FIELDS (t) = NULL_TREE;
2317   TYPE_METHODS (t) = NULL_TREE;
2318   TYPE_VFIELD (t) = NULL_TREE;
2319   TYPE_CONTEXT (t) = NULL_TREE;
2320 }
2321
2322 /* finish up all new vtables.  */
2323
2324 static void
2325 finish_vtbls (binfo, do_self, t)
2326      tree binfo;
2327      int do_self;
2328      tree t;
2329 {
2330   tree binfos = BINFO_BASETYPES (binfo);
2331   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2332
2333   /* Should we use something besides CLASSTYPE_VFIELDS? */
2334   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2335     {
2336       if (BINFO_NEW_VTABLE_MARKED (binfo))
2337         {
2338           tree decl, context;
2339
2340           decl = BINFO_VTABLE (binfo);
2341           context = DECL_CONTEXT (decl);
2342           DECL_CONTEXT (decl) = 0;
2343           if (DECL_INITIAL (decl) != BINFO_VIRTUALS (binfo))
2344             DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE,
2345                                             BINFO_VIRTUALS (binfo));
2346           cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0, 0);
2347           DECL_CONTEXT (decl) = context;
2348         }
2349       CLEAR_BINFO_NEW_VTABLE_MARKED (binfo);
2350     }
2351
2352   for (i = 0; i < n_baselinks; i++)
2353     {
2354       tree base_binfo = TREE_VEC_ELT (binfos, i);
2355       int is_not_base_vtable
2356         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2357       if (TREE_VIA_VIRTUAL (base_binfo))
2358         {
2359           base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2360         }
2361       finish_vtbls (base_binfo, is_not_base_vtable, t);
2362     }
2363 }
2364
2365 /* True if we should override the given BASE_FNDECL with the given
2366    FNDECL.  */
2367
2368 static int
2369 overrides (fndecl, base_fndecl)
2370      tree fndecl, base_fndecl;
2371 {
2372   /* Destructors have special names.  */
2373   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2374       && DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2375     return 1;
2376   if (DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (base_fndecl))
2377       || DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (fndecl)))
2378     return 0;
2379   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2380     {
2381       tree types, base_types;
2382 #if 0
2383       retypes = TREE_TYPE (TREE_TYPE (fndecl));
2384       base_retypes = TREE_TYPE (TREE_TYPE (base_fndecl));
2385 #endif
2386       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2387       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2388       if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2389            == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2390           && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2391         return 1;
2392     }
2393   return 0;
2394 }
2395
2396 static tree
2397 get_class_offset_1 (parent, binfo, context, t, fndecl)
2398      tree parent, binfo, context, t, fndecl;
2399 {
2400   tree binfos = BINFO_BASETYPES (binfo);
2401   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2402   tree rval = NULL_TREE;
2403
2404   if (binfo == parent)
2405     return error_mark_node;
2406
2407   for (i = 0; i < n_baselinks; i++)
2408     {
2409       tree base_binfo = TREE_VEC_ELT (binfos, i);
2410       tree nrval;
2411
2412       if (TREE_VIA_VIRTUAL (base_binfo))
2413         base_binfo = binfo_member (BINFO_TYPE (base_binfo),
2414                                    CLASSTYPE_VBASECLASSES (t));
2415       nrval = get_class_offset_1 (parent, base_binfo, context, t, fndecl);
2416       /* See if we have a new value */
2417       if (nrval && (nrval != error_mark_node || rval==0))
2418         {
2419           /* Only compare if we have two offsets */
2420           if (rval && rval != error_mark_node
2421               && ! tree_int_cst_equal (nrval, rval))
2422             {
2423               /* Only give error if the two offsets are different */
2424               error ("every virtual function must have a unique final overrider");
2425               cp_error ("  found two (or more) `%T' class subobjects in `%T'", context, t);
2426               cp_error ("  with virtual `%D' from virtual base class", fndecl);
2427               return rval;
2428             }
2429           rval = nrval;
2430         }
2431         
2432       if (rval && BINFO_TYPE (binfo) == context)
2433         {
2434           my_friendly_assert (rval == error_mark_node
2435                               || tree_int_cst_equal (rval, BINFO_OFFSET (binfo)), 999);
2436           rval = BINFO_OFFSET (binfo);
2437         }
2438     }
2439   return rval;
2440 }
2441
2442 /* Get the offset to the CONTEXT subobject that is related to the
2443    given BINFO.  */
2444
2445 static tree
2446 get_class_offset (context, t, binfo, fndecl)
2447      tree context, t, binfo, fndecl;
2448 {
2449   tree first_binfo = binfo;
2450   tree offset;
2451   int i;
2452
2453   if (context == t)
2454     return integer_zero_node;
2455
2456   if (BINFO_TYPE (binfo) == context)
2457     return BINFO_OFFSET (binfo);
2458
2459   /* Check less derived binfos first.  */
2460   while (BINFO_BASETYPES (binfo)
2461          && (i=CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo))) != -1)
2462     {
2463       tree binfos = BINFO_BASETYPES (binfo);
2464       binfo = TREE_VEC_ELT (binfos, i);
2465       if (BINFO_TYPE (binfo) == context)
2466         return BINFO_OFFSET (binfo);
2467     }
2468
2469   /* Ok, not found in the less derived binfos, now check the more
2470      derived binfos.  */
2471   offset = get_class_offset_1 (first_binfo, TYPE_BINFO (t), context, t, fndecl);
2472   if (offset==0 || TREE_CODE (offset) != INTEGER_CST)
2473     my_friendly_abort (999);    /* we have to find it.  */
2474   return offset;
2475 }
2476
2477 /* Skip RTTI information at the front of the virtual list.  */
2478
2479 unsigned HOST_WIDE_INT
2480 skip_rtti_stuff (virtuals)
2481      tree *virtuals;
2482 {
2483   int n;
2484
2485   n = 0;
2486   if (*virtuals)
2487     {
2488       /* We always reserve a slot for the offset/tdesc entry.  */
2489       ++n;
2490       *virtuals = TREE_CHAIN (*virtuals);
2491     }
2492   if (flag_vtable_thunks && *virtuals)
2493     {
2494       /* The second slot is reserved for the tdesc pointer when thunks
2495          are used.  */
2496       ++n;
2497       *virtuals = TREE_CHAIN (*virtuals);
2498     }
2499   return n;
2500 }
2501
2502 static void
2503 modify_one_vtable (binfo, t, fndecl, pfn)
2504      tree binfo, t, fndecl, pfn;
2505 {
2506   tree virtuals = BINFO_VIRTUALS (binfo);
2507   unsigned HOST_WIDE_INT n;
2508   
2509   /* update rtti entry */
2510   if (flag_rtti)
2511     {
2512       if (binfo == TYPE_BINFO (t))
2513         {
2514           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2515             build_vtable (TYPE_BINFO (DECL_CONTEXT (CLASSTYPE_VFIELD (t))), t);
2516         }
2517       else
2518         {
2519           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2520             prepare_fresh_vtable (binfo, t);
2521         }
2522     }
2523   if (fndecl == NULL_TREE)
2524     return;
2525
2526   n = skip_rtti_stuff (&virtuals);
2527
2528   while (virtuals)
2529     {
2530       tree current_fndecl = TREE_VALUE (virtuals);
2531       current_fndecl = FNADDR_FROM_VTABLE_ENTRY (current_fndecl);
2532       current_fndecl = TREE_OPERAND (current_fndecl, 0);
2533       if (current_fndecl && overrides (fndecl, current_fndecl))
2534         {
2535           tree base_offset, offset;
2536           tree context = DECL_CLASS_CONTEXT (fndecl);
2537           tree vfield = CLASSTYPE_VFIELD (t);
2538           tree this_offset;
2539
2540           offset = get_class_offset (context, t, binfo, fndecl);
2541
2542           /* Find the right offset for the this pointer based on the
2543              base class we just found.  We have to take into
2544              consideration the virtual base class pointers that we
2545              stick in before the virtual function table pointer.
2546
2547              Also, we want just the delta between the most base class
2548              that we derived this vfield from and us.  */
2549           base_offset = size_binop (PLUS_EXPR,
2550                                     get_derived_offset (binfo, DECL_CONTEXT (current_fndecl)),
2551                                     BINFO_OFFSET (binfo));
2552           this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2553
2554           if (binfo == TYPE_BINFO (t))
2555             {
2556               /* In this case, it is *type*'s vtable we are modifying.
2557                  We start with the approximation that it's vtable is that
2558                  of the immediate base class.  */
2559               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2560                 build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2561             }
2562           else
2563             {
2564               /* This is our very own copy of `basetype' to play with.
2565                  Later, we will fill in all the virtual functions
2566                  that override the virtual functions in these base classes
2567                  which are not defined by the current type.  */
2568               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2569                 prepare_fresh_vtable (binfo, t);
2570             }
2571
2572 #ifdef NOTQUITE
2573           cp_warning ("in %D", DECL_NAME (BINFO_VTABLE (binfo)));
2574 #endif
2575           modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2576                                build_vtable_entry (this_offset, pfn),
2577                                fndecl);
2578         }
2579       ++n;
2580       virtuals = TREE_CHAIN (virtuals);
2581     }
2582 }
2583
2584 /* These are the ones that are not through virtual base classes.  */
2585
2586 static void
2587 modify_all_direct_vtables (binfo, do_self, t, fndecl, pfn)
2588      tree binfo;
2589      int do_self;
2590      tree t, fndecl, pfn;
2591 {
2592   tree binfos = BINFO_BASETYPES (binfo);
2593   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2594
2595   /* Should we use something besides CLASSTYPE_VFIELDS? */
2596   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2597     {
2598       modify_one_vtable (binfo, t, fndecl, pfn);
2599     }
2600
2601   for (i = 0; i < n_baselinks; i++)
2602     {
2603       tree base_binfo = TREE_VEC_ELT (binfos, i);
2604       int is_not_base_vtable
2605         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2606       if (! TREE_VIA_VIRTUAL (base_binfo))
2607         modify_all_direct_vtables (base_binfo, is_not_base_vtable, t, fndecl, pfn);
2608     }
2609 }
2610
2611 /* Fixup all the delta entries in this one vtable that need updating.  */
2612
2613 static void
2614 fixup_vtable_deltas1 (binfo, t)
2615      tree binfo, t;
2616 {
2617   tree virtuals = BINFO_VIRTUALS (binfo);
2618   unsigned HOST_WIDE_INT n;
2619   
2620   n = skip_rtti_stuff (&virtuals);
2621
2622   while (virtuals)
2623     {
2624       tree fndecl = TREE_VALUE (virtuals);
2625       tree pfn = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2626       tree delta = DELTA_FROM_VTABLE_ENTRY (fndecl);
2627       fndecl = TREE_OPERAND (pfn, 0);
2628       if (fndecl)
2629         {
2630           tree base_offset, offset;
2631           tree context = DECL_CLASS_CONTEXT (fndecl);
2632           tree vfield = CLASSTYPE_VFIELD (t);
2633           tree this_offset;
2634
2635           offset = get_class_offset (context, t, binfo, fndecl);
2636
2637           /* Find the right offset for the this pointer based on the
2638              base class we just found.  We have to take into
2639              consideration the virtual base class pointers that we
2640              stick in before the virtual function table pointer.
2641
2642              Also, we want just the delta between the most base class
2643              that we derived this vfield from and us.  */
2644           base_offset = size_binop (PLUS_EXPR,
2645                                     get_derived_offset (binfo,
2646                                                         DECL_CONTEXT (fndecl)),
2647                                     BINFO_OFFSET (binfo));
2648           this_offset = ssize_binop (MINUS_EXPR, offset, base_offset);
2649
2650           if (! tree_int_cst_equal (this_offset, delta))
2651             {
2652               /* Make sure we can modify the derived association with immunity.  */
2653               if (binfo == TYPE_BINFO (t))
2654                 {
2655                   /* In this case, it is *type*'s vtable we are modifying.
2656                      We start with the approximation that it's vtable is that
2657                      of the immediate base class.  */
2658                   if (! BINFO_NEW_VTABLE_MARKED (binfo))
2659                     build_vtable (TYPE_BINFO (DECL_CONTEXT (vfield)), t);
2660                 }
2661               else
2662                 {
2663                   /* This is our very own copy of `basetype' to play with.
2664                      Later, we will fill in all the virtual functions
2665                      that override the virtual functions in these base classes
2666                      which are not defined by the current type.  */
2667                   if (! BINFO_NEW_VTABLE_MARKED (binfo))
2668                     prepare_fresh_vtable (binfo, t);
2669                 }
2670
2671               modify_vtable_entry (get_vtable_entry_n (BINFO_VIRTUALS (binfo), n),
2672                                    build_vtable_entry (this_offset, pfn),
2673                                    fndecl);
2674             }
2675         }
2676       ++n;
2677       virtuals = TREE_CHAIN (virtuals);
2678     }
2679 }
2680
2681 /* Fixup all the delta entries in all the direct vtables that need updating.
2682    This happens when we have non-overridden virtual functions from a
2683    virtual base class, that are at a different offset, in the new
2684    hierarchy, because the layout of the virtual bases has changed.  */
2685
2686 static void
2687 fixup_vtable_deltas (binfo, init_self, t)
2688      tree binfo;
2689      int init_self;
2690      tree t;
2691 {
2692   tree binfos = BINFO_BASETYPES (binfo);
2693   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2694
2695   for (i = 0; i < n_baselinks; i++)
2696     {
2697       tree base_binfo = TREE_VEC_ELT (binfos, i);
2698       int is_not_base_vtable
2699         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2700       if (! TREE_VIA_VIRTUAL (base_binfo))
2701         fixup_vtable_deltas (base_binfo, is_not_base_vtable, t);
2702     }
2703   /* Should we use something besides CLASSTYPE_VFIELDS? */
2704   if (init_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2705     {
2706       fixup_vtable_deltas1 (binfo, t);
2707     }
2708 }
2709
2710 /* These are the ones that are through virtual base classes.  */
2711
2712 static void
2713 modify_all_indirect_vtables (binfo, do_self, via_virtual, t, fndecl, pfn)
2714      tree binfo;
2715      int do_self, via_virtual;
2716      tree t, fndecl, pfn;
2717 {
2718   tree binfos = BINFO_BASETYPES (binfo);
2719   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2720
2721   /* Should we use something besides CLASSTYPE_VFIELDS? */
2722   if (do_self && via_virtual && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2723     {
2724       modify_one_vtable (binfo, t, fndecl, pfn);
2725     }
2726
2727   for (i = 0; i < n_baselinks; i++)
2728     {
2729       tree base_binfo = TREE_VEC_ELT (binfos, i);
2730       int is_not_base_vtable
2731         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2732       if (TREE_VIA_VIRTUAL (base_binfo))
2733         {
2734           via_virtual = 1;
2735           base_binfo = binfo_member (BINFO_TYPE (base_binfo), CLASSTYPE_VBASECLASSES (t));
2736         }
2737       modify_all_indirect_vtables (base_binfo, is_not_base_vtable, via_virtual, t, fndecl, pfn);
2738     }
2739 }
2740
2741 static void
2742 modify_all_vtables (t, fndecl, vfn)
2743      tree t, fndecl, vfn;
2744 {
2745   /* Do these first, so that we will make use of any non-virtual class's
2746      vtable, over a virtual classes vtable.  */
2747   modify_all_direct_vtables (TYPE_BINFO (t), 1, t, fndecl, vfn);
2748   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
2749     modify_all_indirect_vtables (TYPE_BINFO (t), 1, 0, t, fndecl, vfn);
2750 }
2751
2752 /* Here, we already know that they match in every respect.
2753    All we have to check is where they had their declarations.  */
2754
2755 static int 
2756 strictly_overrides (fndecl1, fndecl2)
2757      tree fndecl1, fndecl2;
2758 {
2759   int distance = get_base_distance (DECL_CLASS_CONTEXT (fndecl2),
2760                                     DECL_CLASS_CONTEXT (fndecl1),
2761                                     0, (tree *)0);
2762   if (distance == -2 || distance > 0)
2763     return 1;
2764   return 0;
2765 }
2766
2767 /* Merge overrides for one vtable.
2768    If we want to merge in same function, we are fine.
2769    else
2770      if one has a DECL_CLASS_CONTEXT that is a parent of the
2771        other, than choose the more derived one
2772      else
2773        potentially ill-formed (see 10.3 [class.virtual])
2774        we have to check later to see if there was an
2775        override in this class.  If there was ok, if not
2776        then it is ill-formed.  (mrs)
2777
2778    We take special care to reuse a vtable, if we can.  */
2779
2780 static void
2781 override_one_vtable (binfo, old, t)
2782      tree binfo, old, t;
2783 {
2784   tree virtuals = BINFO_VIRTUALS (binfo);
2785   tree old_virtuals = BINFO_VIRTUALS (old);
2786   enum { REUSE_NEW, REUSE_OLD, UNDECIDED, NEITHER } choose = UNDECIDED;
2787
2788   /* If we have already committed to modifying it, then don't try and
2789      reuse another vtable.  */
2790   if (BINFO_NEW_VTABLE_MARKED (binfo))
2791     choose = NEITHER;
2792
2793   skip_rtti_stuff (&virtuals);
2794   skip_rtti_stuff (&old_virtuals);
2795
2796   while (virtuals)
2797     {
2798       tree fndecl = TREE_VALUE (virtuals);
2799       tree old_fndecl = TREE_VALUE (old_virtuals);
2800       fndecl = FNADDR_FROM_VTABLE_ENTRY (fndecl);
2801       old_fndecl = FNADDR_FROM_VTABLE_ENTRY (old_fndecl);
2802       fndecl = TREE_OPERAND (fndecl, 0);
2803       old_fndecl = TREE_OPERAND (old_fndecl, 0);
2804       /* First check to see if they are the same.  */
2805       if (DECL_ASSEMBLER_NAME (fndecl) == DECL_ASSEMBLER_NAME (old_fndecl))
2806         {
2807           /* No need to do anything.  */
2808         }
2809       else if (strictly_overrides (fndecl, old_fndecl))
2810         {
2811           if (choose == UNDECIDED)
2812             choose = REUSE_NEW;
2813           else if (choose == REUSE_OLD)
2814             {
2815               choose = NEITHER;
2816               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2817                 {
2818                   prepare_fresh_vtable (binfo, t);
2819                   override_one_vtable (binfo, old, t);
2820                   return;
2821                 }
2822             }
2823         }
2824       else if (strictly_overrides (old_fndecl, fndecl))
2825         {
2826           if (choose == UNDECIDED)
2827             choose = REUSE_OLD;
2828           else if (choose == REUSE_NEW)
2829             {
2830               choose = NEITHER;
2831               if (! BINFO_NEW_VTABLE_MARKED (binfo))
2832                 {
2833                   prepare_fresh_vtable (binfo, t);
2834                   override_one_vtable (binfo, old, t);
2835                   return;
2836                 }
2837               TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2838             }
2839           else if (choose == NEITHER)
2840             {
2841               TREE_VALUE (virtuals) = TREE_VALUE (old_virtuals);
2842             }  
2843         }
2844       else
2845         {
2846           choose = NEITHER;
2847           if (! BINFO_NEW_VTABLE_MARKED (binfo))
2848             {
2849               prepare_fresh_vtable (binfo, t);
2850               override_one_vtable (binfo, old, t);
2851               return;
2852             }
2853           {
2854             /* This MUST be overridden, or the class is ill-formed.  */
2855             /* For now, we just make it abstract.  */
2856             tree fndecl = TREE_OPERAND (FNADDR_FROM_VTABLE_ENTRY (TREE_VALUE (virtuals)), 0);
2857             tree vfn;
2858
2859             fndecl = copy_node (fndecl);
2860             copy_lang_decl (fndecl);
2861             DECL_ABSTRACT_VIRTUAL_P (fndecl) = 1;
2862             DECL_NEEDS_FINAL_OVERRIDER_P (fndecl) = 1;
2863             /* Make sure we search for it later.  */
2864             if (! CLASSTYPE_ABSTRACT_VIRTUALS (t))
2865               CLASSTYPE_ABSTRACT_VIRTUALS (t) = error_mark_node;
2866
2867             vfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fndecl);
2868             TREE_CONSTANT (vfn) = 1;
2869             
2870             /* We can use integer_zero_node, as we will core dump
2871                if this is used anyway.  */
2872             TREE_VALUE (virtuals) = build_vtable_entry (integer_zero_node, vfn);
2873           }
2874         }
2875       virtuals = TREE_CHAIN (virtuals);
2876       old_virtuals = TREE_CHAIN (old_virtuals);
2877     }
2878
2879   /* Let's reuse the old vtable.  */
2880   if (choose == REUSE_OLD)
2881     {
2882       BINFO_VTABLE (binfo) = BINFO_VTABLE (old);
2883       BINFO_VIRTUALS (binfo) = BINFO_VIRTUALS (old);
2884     }
2885 }
2886
2887 /* Merge in overrides for virtual bases.
2888    BINFO is the hierarchy we want to modify, and OLD has the potential
2889    overrides.  */
2890
2891 static void
2892 merge_overrides (binfo, old, do_self, t)
2893      tree binfo, old;
2894      int do_self;
2895      tree t;
2896 {
2897   tree binfos = BINFO_BASETYPES (binfo);
2898   tree old_binfos = BINFO_BASETYPES (old);
2899   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2900
2901   /* Should we use something besides CLASSTYPE_VFIELDS? */
2902   if (do_self && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2903     {
2904       override_one_vtable (binfo, old, t);
2905     }
2906
2907   for (i = 0; i < n_baselinks; i++)
2908     {
2909       tree base_binfo = TREE_VEC_ELT (binfos, i);
2910       tree old_base_binfo = TREE_VEC_ELT (old_binfos, i);
2911       int is_not_base_vtable
2912         = i != CLASSTYPE_VFIELD_PARENT (BINFO_TYPE (binfo));
2913       if (! TREE_VIA_VIRTUAL (base_binfo))
2914         merge_overrides (base_binfo, old_base_binfo, is_not_base_vtable, t);
2915     }
2916 }
2917
2918 /* Get the base virtual function declarations in T that are either
2919    overridden or hidden by FNDECL as a list.  We set TREE_PURPOSE with
2920    the overrider/hider.  */
2921
2922 static tree
2923 get_basefndecls (fndecl, t)
2924      tree fndecl, t;
2925 {
2926   tree methods = TYPE_METHODS (t);
2927   tree base_fndecls = NULL_TREE;
2928   tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2929   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2930
2931   while (methods)
2932     {
2933       if (TREE_CODE (methods) == FUNCTION_DECL
2934           && DECL_VINDEX (methods) != NULL_TREE
2935           && DECL_NAME (fndecl) == DECL_NAME (methods))
2936         base_fndecls = temp_tree_cons (fndecl, methods, base_fndecls);
2937
2938       methods = TREE_CHAIN (methods);
2939     }
2940
2941   if (base_fndecls)
2942     return base_fndecls;
2943
2944   for (i = 0; i < n_baseclasses; i++)
2945     {
2946       tree base_binfo = TREE_VEC_ELT (binfos, i);
2947       tree basetype = BINFO_TYPE (base_binfo);
2948
2949       base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2950                               base_fndecls);
2951     }
2952
2953   return base_fndecls;
2954 }
2955
2956 /* Mark the functions that have been hidden with their overriders.
2957    Since we start out with all functions already marked with a hider,
2958    no need to mark functions that are just hidden.  */
2959
2960 static void
2961 mark_overriders (fndecl, base_fndecls)
2962      tree fndecl, base_fndecls;
2963 {
2964   while (base_fndecls)
2965     {
2966       if (overrides (TREE_VALUE (base_fndecls), fndecl))
2967         TREE_PURPOSE (base_fndecls) = fndecl;
2968
2969       base_fndecls = TREE_CHAIN (base_fndecls);
2970     }
2971 }
2972
2973 /* If this declaration supersedes the declaration of
2974    a method declared virtual in the base class, then
2975    mark this field as being virtual as well.  */
2976
2977 static void
2978 check_for_override (decl, ctype)
2979      tree decl, ctype;
2980 {
2981   tree binfos = BINFO_BASETYPES (TYPE_BINFO (ctype));
2982   int i, n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2983   int virtualp = DECL_VIRTUAL_P (decl);
2984
2985   for (i = 0; i < n_baselinks; i++)
2986     {
2987       tree base_binfo = TREE_VEC_ELT (binfos, i);
2988       if (TYPE_VIRTUAL_P (BINFO_TYPE (base_binfo)))
2989         {
2990           tree tmp = get_matching_virtual
2991             (base_binfo, decl,
2992              DESTRUCTOR_NAME_P (DECL_ASSEMBLER_NAME (decl)));
2993           if (tmp)
2994             {
2995               /* If this function overrides some virtual in some base
2996                  class, then the function itself is also necessarily
2997                  virtual, even if the user didn't explicitly say so.  */
2998               DECL_VIRTUAL_P (decl) = 1;
2999
3000               /* The TMP we really want is the one from the deepest
3001                  baseclass on this path, taking care not to
3002                  duplicate if we have already found it (via another
3003                  path to its virtual baseclass.  */
3004               if (TREE_CODE (TREE_TYPE (decl)) == FUNCTION_TYPE)
3005                 {
3006                   cp_error_at ("method `%D' may not be declared static",
3007                                decl);
3008                   cp_error_at ("(since `%D' declared virtual in base class.)",
3009                                tmp);
3010                   break;
3011                 }
3012               virtualp = 1;
3013
3014 #if 0 /* The signature of an overriding function is not changed.  */
3015               {
3016                 /* The argument types may have changed...  */
3017                 tree type = TREE_TYPE (decl);
3018                 tree argtypes = TYPE_ARG_TYPES (type);
3019                 tree base_variant = TREE_TYPE (TREE_VALUE (argtypes));
3020                 tree raises = TYPE_RAISES_EXCEPTIONS (type);
3021
3022                 argtypes = commonparms (TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (tmp))),
3023                                         TREE_CHAIN (argtypes));
3024                 /* But the return type has not.  */
3025                 type = build_cplus_method_type (base_variant, TREE_TYPE (type), argtypes);
3026                 if (raises)
3027                   type = build_exception_variant (type, raises);
3028                 TREE_TYPE (decl) = type;
3029               }
3030 #endif
3031               DECL_VINDEX (decl)
3032                 = tree_cons (NULL_TREE, tmp, DECL_VINDEX (decl));
3033               break;
3034             }
3035         }
3036     }
3037   if (virtualp)
3038     {
3039       if (DECL_VINDEX (decl) == NULL_TREE)
3040         DECL_VINDEX (decl) = error_mark_node;
3041       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
3042     }
3043 }
3044
3045 /* Warn about hidden virtual functions that are not overridden in t.
3046    We know that constructors and destructors don't apply.  */
3047
3048 void
3049 warn_hidden (t)
3050      tree t;
3051 {
3052   tree method_vec = CLASSTYPE_METHOD_VEC (t);
3053   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3054   int i;
3055
3056   /* We go through each separately named virtual function.  */
3057   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3058     {
3059       tree fns = TREE_VEC_ELT (method_vec, i);
3060       tree fndecl;
3061
3062       tree base_fndecls = NULL_TREE;
3063       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3064       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3065
3066       fndecl = OVL_CURRENT (fns);
3067       if (DECL_VINDEX (fndecl) == NULL_TREE)
3068         continue;
3069
3070       /* First we get a list of all possible functions that might be
3071          hidden from each base class.  */
3072       for (i = 0; i < n_baseclasses; i++)
3073         {
3074           tree base_binfo = TREE_VEC_ELT (binfos, i);
3075           tree basetype = BINFO_TYPE (base_binfo);
3076
3077           base_fndecls = chainon (get_basefndecls (fndecl, basetype),
3078                                   base_fndecls);
3079         }
3080
3081       fns = OVL_NEXT (fns);
3082       if (fns)
3083         fndecl = OVL_CURRENT (fns);
3084       else
3085         fndecl = NULL_TREE;
3086
3087       /* ...then mark up all the base functions with overriders, preferring
3088          overriders to hiders.  */
3089       if (base_fndecls)
3090         while (fndecl)
3091           {
3092             mark_overriders (fndecl, base_fndecls);
3093             
3094             fns = OVL_NEXT (fns);
3095             if (fns)
3096               fndecl = OVL_CURRENT (fns);
3097             else
3098               fndecl = NULL_TREE;
3099           }
3100
3101       /* Now give a warning for all base functions without overriders,
3102          as they are hidden.  */
3103       while (base_fndecls)
3104         {
3105           if (! overrides (TREE_VALUE (base_fndecls),
3106                            TREE_PURPOSE (base_fndecls)))
3107             {
3108               /* Here we know it is a hider, and no overrider exists.  */
3109               cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
3110               cp_warning_at ("  by `%D'", TREE_PURPOSE (base_fndecls));
3111             }
3112
3113           base_fndecls = TREE_CHAIN (base_fndecls);
3114         }
3115     }
3116 }
3117
3118 /* Check for things that are invalid.  There are probably plenty of other
3119    things we should check for also.  */
3120
3121 static void
3122 finish_struct_anon (t)
3123      tree t;
3124 {
3125   tree field;
3126   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
3127     {
3128       if (TREE_STATIC (field))
3129         continue;
3130       if (TREE_CODE (field) != FIELD_DECL)
3131         continue;
3132
3133       if (DECL_NAME (field) == NULL_TREE
3134           && TREE_CODE (TREE_TYPE (field)) == UNION_TYPE)
3135         {
3136           tree* uelt = &TYPE_FIELDS (TREE_TYPE (field));
3137           for (; *uelt; uelt = &TREE_CHAIN (*uelt))
3138             {
3139               if (TREE_CODE (*uelt) != FIELD_DECL)
3140                 continue;
3141
3142               if (TREE_PRIVATE (*uelt))
3143                 cp_pedwarn_at ("private member `%#D' in anonymous union",
3144                                *uelt);
3145               else if (TREE_PROTECTED (*uelt))
3146                 cp_pedwarn_at ("protected member `%#D' in anonymous union",
3147                                *uelt);
3148
3149               TREE_PRIVATE (*uelt) = TREE_PRIVATE (field);
3150               TREE_PROTECTED (*uelt) = TREE_PROTECTED (field);
3151             }
3152         }
3153     }
3154 }
3155
3156 extern int interface_only, interface_unknown;
3157
3158 /* Create default constructors, assignment operators, and so forth for
3159    the type indicated by T, if they are needed.
3160    CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
3161    CANT_HAVE_ASSIGNMENT are nonzero if, for whatever reason, the class
3162    cannot have a default constructor, copy constructor taking a const
3163    reference argument, or an assignment operator, respectively.  If a
3164    virtual destructor is created, its DECL is returned; otherwise the
3165    return value is NULL_TREE.  */
3166
3167 static tree
3168 add_implicitly_declared_members (t, cant_have_default_ctor,
3169                                  cant_have_const_cctor,
3170                                  cant_have_assignment)
3171      tree t;
3172      int cant_have_default_ctor;
3173      int cant_have_const_cctor;
3174      int cant_have_assignment;
3175 {
3176   tree default_fn;
3177   tree implicit_fns = NULL_TREE;
3178   tree name = TYPE_IDENTIFIER (t);
3179   tree virtual_dtor = NULL_TREE;
3180   tree *f;
3181
3182   /* Destructor.  */
3183   if (TYPE_NEEDS_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t)
3184       && !IS_SIGNATURE (t))
3185     {
3186       default_fn = cons_up_default_function (t, name, 0);
3187       check_for_override (default_fn, t);
3188
3189       /* If we couldn't make it work, then pretend we didn't need it.  */
3190       if (default_fn == void_type_node)
3191         TYPE_NEEDS_DESTRUCTOR (t) = 0;
3192       else
3193         {
3194           TREE_CHAIN (default_fn) = implicit_fns;
3195           implicit_fns = default_fn;
3196
3197           if (DECL_VINDEX (default_fn))
3198             virtual_dtor = default_fn;
3199         }
3200     }
3201   TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3202
3203   /* Default constructor.  */
3204   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor
3205       && ! IS_SIGNATURE (t))
3206     {
3207       default_fn = cons_up_default_function (t, name, 2);
3208       TREE_CHAIN (default_fn) = implicit_fns;
3209       implicit_fns = default_fn;
3210     }
3211
3212   /* Copy constructor.  */
3213   if (! TYPE_HAS_INIT_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3214     {
3215       /* ARM 12.18: You get either X(X&) or X(const X&), but
3216          not both.  --Chip  */
3217       default_fn = cons_up_default_function (t, name,
3218                                              3 + cant_have_const_cctor);
3219       TREE_CHAIN (default_fn) = implicit_fns;
3220       implicit_fns = default_fn;
3221     }
3222
3223   /* Assignment operator.  */
3224   if (! TYPE_HAS_ASSIGN_REF (t) && ! IS_SIGNATURE (t) && ! TYPE_FOR_JAVA (t))
3225     {
3226       default_fn = cons_up_default_function (t, name,
3227                                              5 + cant_have_assignment);
3228       TREE_CHAIN (default_fn) = implicit_fns;
3229       implicit_fns = default_fn;
3230     }
3231
3232   /* Now, hook all of the new functions on to TYPE_METHODS,
3233      and add them to the CLASSTYPE_METHOD_VEC.  */
3234   for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3235     add_method (t, 0, *f);
3236   *f = TYPE_METHODS (t);
3237   TYPE_METHODS (t) = implicit_fns;
3238
3239   return virtual_dtor;
3240 }
3241
3242 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
3243    (or C++ class declaration).
3244
3245    For C++, we must handle the building of derived classes.
3246    Also, C++ allows static class members.  The way that this is
3247    handled is to keep the field name where it is (as the DECL_NAME
3248    of the field), and place the overloaded decl in the DECL_FIELD_BITPOS
3249    of the field.  layout_record and layout_union will know about this.
3250
3251    More C++ hair: inline functions have text in their
3252    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
3253    meaningful tree structure.  After the struct has been laid out, set
3254    things up so that this can happen.
3255
3256    And still more: virtual functions.  In the case of single inheritance,
3257    when a new virtual function is seen which redefines a virtual function
3258    from the base class, the new virtual function is placed into
3259    the virtual function table at exactly the same address that
3260    it had in the base class.  When this is extended to multiple
3261    inheritance, the same thing happens, except that multiple virtual
3262    function tables must be maintained.  The first virtual function
3263    table is treated in exactly the same way as in the case of single
3264    inheritance.  Additional virtual function tables have different
3265    DELTAs, which tell how to adjust `this' to point to the right thing.
3266
3267    ATTRIBUTES is the set of decl attributes to be applied, if any.  */
3268
3269 tree
3270 finish_struct_1 (t, warn_anon)
3271      tree t;
3272      int warn_anon;
3273 {
3274   int old;
3275   enum tree_code code = TREE_CODE (t);
3276   tree fields = TYPE_FIELDS (t);
3277   tree x, last_x, method_vec;
3278   int has_virtual;
3279   int max_has_virtual;
3280   tree pending_virtuals = NULL_TREE;
3281   tree pending_hard_virtuals = NULL_TREE;
3282   tree abstract_virtuals = NULL_TREE;
3283   tree vfield;
3284   tree vfields;
3285   tree virtual_dtor;
3286   int cant_have_default_ctor;
3287   int cant_have_const_ctor;
3288   int no_const_asn_ref;
3289
3290   /* The index of the first base class which has virtual
3291      functions.  Only applied to non-virtual baseclasses.  */
3292   int first_vfn_base_index;
3293
3294   int n_baseclasses;
3295   int any_default_members = 0;
3296   int const_sans_init = 0;
3297   int ref_sans_init = 0;
3298   tree access_decls = NULL_TREE;
3299   int aggregate = 1;
3300   int empty = 1;
3301   int has_pointers = 0;
3302
3303   if (warn_anon && code != UNION_TYPE && ANON_AGGRNAME_P (TYPE_IDENTIFIER (t)))
3304     pedwarn ("anonymous class type not used to declare any objects");
3305
3306   if (TYPE_SIZE (t))
3307     {
3308       if (IS_AGGR_TYPE (t))
3309         cp_error ("redefinition of `%#T'", t);
3310       else
3311         my_friendly_abort (172);
3312       popclass (0);
3313       return t;
3314     }
3315
3316   GNU_xref_decl (current_function_decl, t);
3317
3318   /* If this type was previously laid out as a forward reference,
3319      make sure we lay it out again.  */
3320
3321   TYPE_SIZE (t) = NULL_TREE;
3322   CLASSTYPE_GOT_SEMICOLON (t) = 0;
3323
3324 #if 0
3325   /* This is in general too late to do this.  I moved the main case up to
3326      left_curly, what else needs to move?  */
3327   if (! IS_SIGNATURE (t))
3328     {
3329       my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3330       my_friendly_assert (CLASSTYPE_INTERFACE_KNOWN (t) == ! interface_unknown, 999);
3331     }
3332 #endif
3333
3334   old = suspend_momentary ();
3335
3336   /* Install struct as DECL_FIELD_CONTEXT of each field decl.
3337      Also process specified field sizes.
3338      Set DECL_FIELD_SIZE to the specified size, or 0 if none specified.
3339      The specified size is found in the DECL_INITIAL.
3340      Store 0 there, except for ": 0" fields (so we can find them
3341      and delete them, below).  */
3342
3343   if (TYPE_BINFO_BASETYPES (t))
3344     n_baseclasses = TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (t));
3345   else
3346     n_baseclasses = 0;
3347
3348   if (n_baseclasses > 0)
3349     {
3350       struct base_info base_info;
3351
3352       first_vfn_base_index = finish_base_struct (t, &base_info);
3353       /* Remember where we got our vfield from.  */
3354       CLASSTYPE_VFIELD_PARENT (t) = first_vfn_base_index;
3355       has_virtual = base_info.has_virtual;
3356       max_has_virtual = base_info.max_has_virtual;
3357       CLASSTYPE_N_SUPERCLASSES (t) += base_info.n_ancestors;
3358       vfield = base_info.vfield;
3359       vfields = base_info.vfields;
3360       CLASSTYPE_RTTI (t) = base_info.rtti;
3361       cant_have_default_ctor = base_info.cant_have_default_ctor;
3362       cant_have_const_ctor = base_info.cant_have_const_ctor;
3363       no_const_asn_ref = base_info.no_const_asn_ref;
3364       aggregate = 0;
3365     }
3366   else
3367     {
3368       first_vfn_base_index = -1;
3369       has_virtual = 0;
3370       max_has_virtual = has_virtual;
3371       vfield = NULL_TREE;
3372       vfields = NULL_TREE;
3373       CLASSTYPE_RTTI (t) = NULL_TREE;
3374       cant_have_default_ctor = 0;
3375       cant_have_const_ctor = 0;
3376       no_const_asn_ref = 0;
3377     }
3378
3379 #if 0
3380   /* Both of these should be done before now.  */
3381   if (write_virtuals == 3 && CLASSTYPE_INTERFACE_KNOWN (t)
3382       && ! IS_SIGNATURE (t))
3383     {
3384       my_friendly_assert (CLASSTYPE_INTERFACE_ONLY (t) == interface_only, 999);
3385       my_friendly_assert (CLASSTYPE_VTABLE_NEEDS_WRITING (t) == ! interface_only, 999);
3386     }
3387 #endif
3388
3389   /* The three of these are approximations which may later be
3390      modified.  Needed at this point to make add_virtual_function
3391      and modify_vtable_entries work.  */
3392   CLASSTYPE_VFIELDS (t) = vfields;
3393   CLASSTYPE_VFIELD (t) = vfield;
3394
3395   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3396     {
3397       GNU_xref_member (current_class_name, x);
3398
3399       /* If this was an evil function, don't keep it in class.  */
3400       if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3401         continue;
3402
3403       /* Do both of these, even though they're in the same union;
3404          if the insn `r' member and the size `i' member are
3405          different sizes, as on the alpha, the larger of the two
3406          will end up with garbage in it.  */
3407       DECL_SAVED_INSNS (x) = NULL_RTX;
3408       DECL_FIELD_SIZE (x) = 0;
3409
3410       check_for_override (x, t);
3411       if (DECL_ABSTRACT_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3412         cp_error_at ("initializer specified for non-virtual method `%D'", x);
3413
3414       /* The name of the field is the original field name
3415          Save this in auxiliary field for later overloading.  */
3416       if (DECL_VINDEX (x))
3417         {
3418           add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3419                                 &has_virtual, x, t);
3420           if (DECL_ABSTRACT_VIRTUAL_P (x))
3421             abstract_virtuals = tree_cons (NULL_TREE, x, abstract_virtuals);
3422 #if 0
3423           /* XXX Why did I comment this out?  (jason) */
3424           else
3425             TREE_USED (x) = 1;
3426 #endif
3427         }
3428     }
3429
3430   if (n_baseclasses)
3431     fields = chainon (build_vbase_pointer_fields (t), fields);
3432
3433   last_x = NULL_TREE;
3434   for (x = fields; x; x = TREE_CHAIN (x))
3435     {
3436       GNU_xref_member (current_class_name, x);
3437
3438       if (TREE_CODE (x) == FIELD_DECL)
3439         {
3440           DECL_PACKED (x) |= TYPE_PACKED (t);
3441           empty = 0;
3442         }
3443
3444       if (TREE_CODE (x) == USING_DECL)
3445         {
3446           /* Save access declarations for later.  */
3447           if (last_x)
3448             TREE_CHAIN (last_x) = TREE_CHAIN (x);
3449           else
3450             fields = TREE_CHAIN (x);
3451           
3452           access_decls = scratch_tree_cons (NULL_TREE, x, access_decls);
3453           continue;
3454         }
3455
3456       last_x = x;
3457
3458       if (TREE_CODE (x) == TYPE_DECL
3459           || TREE_CODE (x) == TEMPLATE_DECL)
3460         continue;
3461
3462       /* If we've gotten this far, it's a data member, possibly static,
3463          or an enumerator.  */
3464
3465       DECL_FIELD_CONTEXT (x) = t;
3466
3467       /* ``A local class cannot have static data members.'' ARM 9.4 */
3468       if (current_function_decl && TREE_STATIC (x))
3469         cp_error_at ("field `%D' in local class cannot be static", x);
3470
3471       /* Perform error checking that did not get done in
3472          grokdeclarator.  */
3473       if (TREE_CODE (TREE_TYPE (x)) == FUNCTION_TYPE)
3474         {
3475           cp_error_at ("field `%D' invalidly declared function type",
3476                        x);
3477           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3478         }
3479       else if (TREE_CODE (TREE_TYPE (x)) == METHOD_TYPE)
3480         {
3481           cp_error_at ("field `%D' invalidly declared method type", x);
3482           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3483         }
3484       else if (TREE_CODE (TREE_TYPE (x)) == OFFSET_TYPE)
3485         {
3486           cp_error_at ("field `%D' invalidly declared offset type", x);
3487           TREE_TYPE (x) = build_pointer_type (TREE_TYPE (x));
3488         }
3489
3490 #if 0
3491       if (DECL_NAME (x) == constructor_name (t))
3492         cant_have_default_ctor = 1;
3493 #endif
3494
3495       if (TREE_TYPE (x) == error_mark_node)
3496         continue;
3497           
3498       DECL_SAVED_INSNS (x) = NULL_RTX;
3499       DECL_FIELD_SIZE (x) = 0;
3500
3501       /* When this goes into scope, it will be a non-local reference.  */
3502       DECL_NONLOCAL (x) = 1;
3503
3504       if (TREE_CODE (x) == CONST_DECL)
3505         continue;
3506
3507       if (TREE_CODE (x) == VAR_DECL)
3508         {
3509           if (TREE_CODE (t) == UNION_TYPE)
3510             /* Unions cannot have static members.  */
3511             cp_error_at ("field `%D' declared static in union", x);
3512               
3513           continue;
3514         }
3515
3516       /* Now it can only be a FIELD_DECL.  */
3517
3518       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3519         aggregate = 0;
3520
3521       /* If this is of reference type, check if it needs an init.
3522          Also do a little ANSI jig if necessary.  */
3523       if (TREE_CODE (TREE_TYPE (x)) == REFERENCE_TYPE)
3524         {
3525           if (DECL_INITIAL (x) == NULL_TREE)
3526             ref_sans_init = 1;
3527
3528           /* ARM $12.6.2: [A member initializer list] (or, for an
3529              aggregate, initialization by a brace-enclosed list) is the
3530              only way to initialize nonstatic const and reference
3531              members.  */
3532           cant_have_default_ctor = 1;
3533           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3534
3535           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3536             {
3537               if (DECL_NAME (x))
3538                 cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3539               else
3540                 cp_warning_at ("non-static reference in class without a constructor", x);
3541             }
3542         }
3543
3544       if (TREE_CODE (TREE_TYPE (x)) == POINTER_TYPE)
3545         has_pointers = 1;
3546
3547       /* If any field is const, the structure type is pseudo-const.  */
3548       if (TREE_READONLY (x))
3549         {
3550           C_TYPE_FIELDS_READONLY (t) = 1;
3551           if (DECL_INITIAL (x) == NULL_TREE)
3552             const_sans_init = 1;
3553
3554           /* ARM $12.6.2: [A member initializer list] (or, for an
3555              aggregate, initialization by a brace-enclosed list) is the
3556              only way to initialize nonstatic const and reference
3557              members.  */
3558           cant_have_default_ctor = 1;
3559           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3560
3561           if (! TYPE_HAS_CONSTRUCTOR (t) && !IS_SIGNATURE (t)
3562               && extra_warnings)
3563             {
3564               if (DECL_NAME (x))
3565                 cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3566               else
3567                 cp_warning_at ("non-static const member in class without a constructor", x);
3568             }
3569         }
3570       else
3571         {
3572           /* A field that is pseudo-const makes the structure
3573              likewise.  */
3574           tree t1 = TREE_TYPE (x);
3575           while (TREE_CODE (t1) == ARRAY_TYPE)
3576             t1 = TREE_TYPE (t1);
3577           if (IS_AGGR_TYPE (t1))
3578             {
3579               if (C_TYPE_FIELDS_READONLY (t1))
3580                 C_TYPE_FIELDS_READONLY (t) = 1;
3581               if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (t1))
3582                 const_sans_init = 1;
3583             }
3584         }
3585
3586       /* We set DECL_C_BIT_FIELD in grokbitfield.
3587          If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
3588       if (DECL_C_BIT_FIELD (x))
3589         {
3590           /* Invalid bit-field size done by grokfield.  */
3591           /* Detect invalid bit-field type.  */
3592           if (DECL_INITIAL (x)
3593               && ! INTEGRAL_TYPE_P (TREE_TYPE (x)))
3594             {
3595               cp_error_at ("bit-field `%#D' with non-integral type", x);
3596               DECL_INITIAL (x) = NULL;
3597             }
3598
3599           /* Detect and ignore out of range field width.  */
3600           if (DECL_INITIAL (x))
3601             {
3602               tree w = DECL_INITIAL (x);
3603               register int width = 0;
3604
3605               /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
3606               STRIP_NOPS (w);
3607
3608               /* detect invalid field size.  */
3609               if (TREE_CODE (w) == CONST_DECL)
3610                 w = DECL_INITIAL (w);
3611               else if (TREE_READONLY_DECL_P (w))
3612                 w = decl_constant_value (w);
3613
3614               if (TREE_CODE (w) != INTEGER_CST)
3615                 {
3616                   cp_error_at ("bit-field `%D' width not an integer constant",
3617                                x);
3618                   DECL_INITIAL (x) = NULL_TREE;
3619                 }
3620               else if (width = TREE_INT_CST_LOW (w),
3621                        width < 0)
3622                 {
3623                   DECL_INITIAL (x) = NULL;
3624                   cp_error_at ("negative width in bit-field `%D'", x);
3625                 }
3626               else if (width == 0 && DECL_NAME (x) != 0)
3627                 {
3628                   DECL_INITIAL (x) = NULL;
3629                   cp_error_at ("zero width for bit-field `%D'", x);
3630                 }
3631               else if (width
3632                        > TYPE_PRECISION (long_long_unsigned_type_node))
3633                 {
3634                   /* The backend will dump if you try to use something
3635                      too big; avoid that.  */
3636                   DECL_INITIAL (x) = NULL;
3637                   sorry ("bit-fields larger than %d bits",
3638                          TYPE_PRECISION (long_long_unsigned_type_node));
3639                   cp_error_at ("  in declaration of `%D'", x);
3640                 }
3641               else if (width > TYPE_PRECISION (TREE_TYPE (x))
3642                        && TREE_CODE (TREE_TYPE (x)) != ENUMERAL_TYPE
3643                        && TREE_CODE (TREE_TYPE (x)) != BOOLEAN_TYPE)
3644                 {
3645                   cp_warning_at ("width of `%D' exceeds its type", x);
3646                 }
3647               else if (TREE_CODE (TREE_TYPE (x)) == ENUMERAL_TYPE
3648                        && ((min_precision (TYPE_MIN_VALUE (TREE_TYPE (x)),
3649                                            TREE_UNSIGNED (TREE_TYPE (x))) > width)
3650                            || (min_precision (TYPE_MAX_VALUE (TREE_TYPE (x)),
3651                                               TREE_UNSIGNED (TREE_TYPE (x))) > width)))
3652                 {
3653                   cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3654                                  x, TREE_TYPE (x));
3655                 }
3656
3657               if (DECL_INITIAL (x) == NULL_TREE)
3658                 ;
3659               else if (width == 0)
3660                 {
3661 #ifdef EMPTY_FIELD_BOUNDARY
3662                   DECL_ALIGN (x) = MAX (DECL_ALIGN (x), EMPTY_FIELD_BOUNDARY);
3663 #endif
3664 #ifdef PCC_BITFIELD_TYPE_MATTERS
3665                   DECL_ALIGN (x) = MAX (DECL_ALIGN (x),
3666                                         TYPE_ALIGN (TREE_TYPE (x)));
3667 #endif
3668                 }
3669               else
3670                 {
3671                   DECL_INITIAL (x) = NULL_TREE;
3672                   DECL_FIELD_SIZE (x) = width;
3673                   DECL_BIT_FIELD (x) = 1;
3674                 }
3675             }
3676           else
3677             /* Non-bit-fields are aligned for their type.  */
3678             DECL_ALIGN (x) = MAX (DECL_ALIGN (x), TYPE_ALIGN (TREE_TYPE (x)));
3679         }
3680       else
3681         {
3682           tree type = TREE_TYPE (x);
3683
3684           while (TREE_CODE (type) == ARRAY_TYPE)
3685             type = TREE_TYPE (type);
3686
3687           if (TYPE_LANG_SPECIFIC (type) && ! ANON_UNION_P (x)
3688               && ! TYPE_PTRMEMFUNC_P (type))
3689             {
3690               /* Never let anything with uninheritable virtuals
3691                  make it through without complaint.  */
3692               if (CLASSTYPE_ABSTRACT_VIRTUALS (type))
3693                 abstract_virtuals_error (x, type);
3694                       
3695               /* Don't let signatures make it through either.  */
3696               if (IS_SIGNATURE (type))
3697                 signature_error (x, type);
3698                       
3699               if (code == UNION_TYPE)
3700                 {
3701                   char *fie = NULL;
3702                   if (TYPE_NEEDS_CONSTRUCTING (type))
3703                     fie = "constructor";
3704                   else if (TYPE_NEEDS_DESTRUCTOR (type))
3705                     fie = "destructor";
3706                   else if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3707                     fie = "copy assignment operator";
3708                   if (fie)
3709                     cp_error_at ("member `%#D' with %s not allowed in union", x,
3710                                  fie);
3711                 }
3712               else
3713                 {
3714                   TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3715                   TYPE_NEEDS_DESTRUCTOR (t) |= TYPE_NEEDS_DESTRUCTOR (type);
3716                   TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3717                   TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3718                 }
3719
3720               if (!TYPE_HAS_CONST_INIT_REF (type))
3721                 cant_have_const_ctor = 1;
3722
3723               if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3724                 no_const_asn_ref = 1;
3725
3726               if (TYPE_HAS_CONSTRUCTOR (type)
3727                   && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3728                 {
3729                   cant_have_default_ctor = 1;
3730 #if 0
3731                   /* This is wrong for aggregates.  */
3732                   if (! TYPE_HAS_CONSTRUCTOR (t))
3733                     {
3734                       if (DECL_NAME (x))
3735                         cp_pedwarn_at ("member `%#D' with only non-default constructor", x);
3736                       else
3737                         cp_pedwarn_at ("member with only non-default constructor", x);
3738                       cp_pedwarn_at ("in class without a constructor",
3739                                      x);
3740                     }
3741 #endif
3742                 }
3743             }
3744           if (DECL_INITIAL (x) != NULL_TREE)
3745             {
3746               /* `build_class_init_list' does not recognize
3747                  non-FIELD_DECLs.  */
3748               if (code == UNION_TYPE && any_default_members != 0)
3749                 cp_error_at ("multiple fields in union `%T' initialized");
3750               any_default_members = 1;
3751             }
3752         }
3753     }
3754
3755   /* If this type has any constant members which did not come
3756      with their own initialization, mark that fact here.  It is
3757      not an error here, since such types can be saved either by their
3758      constructors, or by fortuitous initialization.  */
3759   CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = const_sans_init;
3760   CLASSTYPE_REF_FIELDS_NEED_INIT (t) = ref_sans_init;
3761   CLASSTYPE_ABSTRACT_VIRTUALS (t) = abstract_virtuals;
3762
3763   /* Effective C++ rule 11.  */
3764   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3765       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3766     {
3767       cp_warning ("`%#T' has pointer data members", t);
3768       
3769       if (! TYPE_HAS_INIT_REF (t))
3770         {
3771           cp_warning ("  but does not override `%T(const %T&)'", t, t);
3772           if (! TYPE_HAS_ASSIGN_REF (t))
3773             cp_warning ("  or `operator=(const %T&)'", t);
3774         }
3775       else if (! TYPE_HAS_ASSIGN_REF (t))
3776         cp_warning ("  but does not override `operator=(const %T&)'", t);
3777     }
3778   
3779   /* Do some bookkeeping that will guide the generation of implicitly
3780      declared member functions.  */
3781   TYPE_HAS_COMPLEX_INIT_REF (t)
3782     |= (TYPE_HAS_INIT_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3783         || has_virtual || any_default_members);
3784   TYPE_NEEDS_CONSTRUCTING (t)
3785     |= (TYPE_HAS_CONSTRUCTOR (t) || TYPE_USES_VIRTUAL_BASECLASSES (t)
3786         || has_virtual || any_default_members);
3787   if (! IS_SIGNATURE (t))
3788     CLASSTYPE_NON_AGGREGATE (t)
3789       = ! aggregate || has_virtual || TYPE_HAS_CONSTRUCTOR (t);
3790   TYPE_HAS_REAL_ASSIGNMENT (t) |= TYPE_HAS_ASSIGNMENT (t);
3791   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
3792   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
3793     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
3794
3795   /* Synthesize any needed methods.  Note that methods will be synthesized
3796      for anonymous unions; grok_x_components undoes that.  */
3797   virtual_dtor 
3798     = add_implicitly_declared_members (t, cant_have_default_ctor,
3799                                        cant_have_const_ctor,
3800                                        no_const_asn_ref);
3801   if (virtual_dtor)
3802     add_virtual_function (&pending_virtuals, &pending_hard_virtuals,
3803                           &has_virtual, virtual_dtor, t);
3804
3805   if (TYPE_METHODS (t))
3806     {
3807       finish_struct_methods (t);
3808       method_vec = CLASSTYPE_METHOD_VEC (t);
3809     }
3810   else
3811     {
3812       method_vec = 0;
3813
3814       /* Just in case these got accidentally
3815          filled in by syntax errors.  */
3816       TYPE_HAS_CONSTRUCTOR (t) = 0;
3817       TYPE_HAS_DESTRUCTOR (t) = 0;
3818     }
3819
3820   for (access_decls = nreverse (access_decls); access_decls;
3821        access_decls = TREE_CHAIN (access_decls))
3822     handle_using_decl (TREE_VALUE (access_decls), t, method_vec, fields); 
3823
3824   if (vfield == NULL_TREE && has_virtual)
3825     {
3826       /* We build this decl with vtbl_ptr_type_node, which is a
3827          `vtable_entry_type*'.  It might seem more precise to use
3828          `vtable_entry_type (*)[N]' where N is the number of firtual
3829          functions.  However, that would require the vtable pointer in
3830          base classes to have a different type than the vtable pointer
3831          in derived classes.  We could make that happen, but that
3832          still wouldn't solve all the problems.  In particular, the
3833          type-based alias analysis code would decide that assignments
3834          to the base class vtable pointer can't alias assignments to
3835          the derived class vtable pointer, since they have different
3836          types.  Thus, in an derived class destructor, where the base
3837          class constructor was inlined, we could generate bad code for
3838          setting up the vtable pointer.  
3839
3840          Therefore, we use one type for all vtable pointers.  We still
3841          use a type-correct type; it's just doesn't indicate the array
3842          bounds.  That's better than using `void*' or some such; it's
3843          cleaner, and it let's the alias analysis code know that these
3844          stores cannot alias stores to void*!  */
3845       vfield = build_lang_field_decl (FIELD_DECL, get_vfield_name (t),
3846                                       vtbl_ptr_type_node);
3847       /* If you change any of the below, take a look at all the
3848          other VFIELD_BASEs and VTABLE_BASEs in the code, and change
3849          them too.  */
3850       DECL_ASSEMBLER_NAME (vfield) = get_identifier (VFIELD_BASE);
3851       CLASSTYPE_VFIELD (t) = vfield;
3852       DECL_VIRTUAL_P (vfield) = 1;
3853       DECL_ARTIFICIAL (vfield) = 1;
3854       DECL_FIELD_CONTEXT (vfield) = t;
3855       DECL_CLASS_CONTEXT (vfield) = t;
3856       DECL_FCONTEXT (vfield) = t;
3857       DECL_SAVED_INSNS (vfield) = NULL_RTX;
3858       DECL_FIELD_SIZE (vfield) = 0;
3859       DECL_ALIGN (vfield) = TYPE_ALIGN (ptr_type_node);
3860 #if 0
3861       /* This is more efficient, but breaks binary compatibility, turn
3862          it on sometime when we don't care.  If we turn it on, we also
3863          have to enable the code in dfs_init_vbase_pointers.  */
3864       /* vfield is always first entry in structure.  */
3865       TREE_CHAIN (vfield) = fields;
3866       fields = vfield;
3867 #else
3868       if (last_x)
3869         {
3870           my_friendly_assert (TREE_CHAIN (last_x) == NULL_TREE, 175);
3871           TREE_CHAIN (last_x) = vfield;
3872           last_x = vfield;
3873         }
3874       else
3875         fields = vfield;
3876 #endif
3877       empty = 0;
3878       vfields = chainon (vfields, CLASSTYPE_AS_LIST (t));
3879     }
3880
3881   /* Now DECL_INITIAL is null on all members except for zero-width bit-fields.
3882      And they have already done their work.
3883
3884      C++: maybe we will support default field initialization some day...  */
3885
3886   /* Delete all zero-width bit-fields from the front of the fieldlist */
3887   while (fields && DECL_C_BIT_FIELD (fields)
3888          && DECL_INITIAL (fields))
3889     fields = TREE_CHAIN (fields);
3890   /* Delete all such fields from the rest of the fields.  */
3891   for (x = fields; x;)
3892     {
3893       if (TREE_CHAIN (x) && DECL_C_BIT_FIELD (TREE_CHAIN (x))
3894           && DECL_INITIAL (TREE_CHAIN (x)))
3895         TREE_CHAIN (x) = TREE_CHAIN (TREE_CHAIN (x));
3896       else
3897         x = TREE_CHAIN (x);
3898     }
3899   /* Delete all duplicate fields from the fields */
3900   delete_duplicate_fields (fields);
3901
3902   /* Catch function/field name conflict.  We don't need to do this for a
3903      signature, since it can only contain the fields constructed in
3904      append_signature_fields.  */
3905   if (! IS_SIGNATURE (t))
3906     {
3907       int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
3908       for (x = fields; x; x = TREE_CHAIN (x))
3909         {
3910           tree name = DECL_NAME (x);
3911           int i;
3912
3913           if (TREE_CODE (x) == TYPE_DECL && DECL_ARTIFICIAL (x))
3914             continue;
3915
3916           for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
3917             if (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)))
3918                 == name)
3919               {
3920                 cp_error_at ("data member `%#D' conflicts with", x);
3921                 cp_error_at ("function member `%#D'",
3922                              OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
3923                 break;
3924               }
3925         }
3926     }
3927
3928   /* Now we have the final fieldlist for the data fields.  Record it,
3929      then lay out the structure or union (including the fields).  */
3930
3931   TYPE_FIELDS (t) = fields;
3932
3933   if (n_baseclasses)
3934     {
3935       last_x = build_base_fields (t);
3936
3937       /* If all our bases are empty, we can be empty too.  */
3938       for (x = last_x; empty && x; x = TREE_CHAIN (x))
3939         if (DECL_SIZE (x) != integer_zero_node)
3940           empty = 0;
3941     }
3942   if (empty)
3943     {
3944       /* C++: do not let empty structures exist.  */
3945       tree decl = build_lang_field_decl
3946         (FIELD_DECL, NULL_TREE, char_type_node);
3947       TREE_CHAIN (decl) = fields;
3948       TYPE_FIELDS (t) = decl;
3949     }
3950   if (n_baseclasses)
3951     TYPE_FIELDS (t) = chainon (last_x, TYPE_FIELDS (t));
3952
3953   layout_type (t);
3954
3955   /* Remember the size and alignment of the class before adding
3956      the virtual bases.  */
3957   if (empty && flag_new_abi)
3958     CLASSTYPE_SIZE (t) = integer_zero_node;
3959   else if (flag_new_abi && TYPE_HAS_COMPLEX_INIT_REF (t)
3960            && TYPE_HAS_COMPLEX_ASSIGN_REF (t))
3961     CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
3962   else
3963     CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
3964   CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
3965
3966   finish_struct_anon (t);
3967
3968   /* Set the TYPE_DECL for this type to contain the right
3969      value for DECL_OFFSET, so that we can use it as part
3970      of a COMPONENT_REF for multiple inheritance.  */
3971
3972   layout_decl (TYPE_MAIN_DECL (t), 0);
3973
3974   /* Now fix up any virtual base class types that we left lying
3975      around.  We must get these done before we try to lay out the
3976      virtual function table.  */
3977   pending_hard_virtuals = nreverse (pending_hard_virtuals);
3978
3979   if (n_baseclasses)
3980     /* layout_basetypes will remove the base subobject fields.  */
3981     max_has_virtual = layout_basetypes (t, max_has_virtual);
3982   else if (empty)
3983     TYPE_FIELDS (t) = fields;
3984
3985   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
3986     {
3987       tree vbases;
3988
3989       vbases = CLASSTYPE_VBASECLASSES (t);
3990       CLASSTYPE_N_VBASECLASSES (t) = list_length (vbases);
3991
3992       {
3993         /* Now fixup overrides of all functions in vtables from all
3994            direct or indirect virtual base classes.  */
3995         tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
3996         int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
3997
3998         for (i = 0; i < n_baseclasses; i++)
3999           {
4000             tree base_binfo = TREE_VEC_ELT (binfos, i);
4001             tree basetype = BINFO_TYPE (base_binfo);
4002             tree vbases;
4003
4004             vbases = CLASSTYPE_VBASECLASSES (basetype);
4005             while (vbases)
4006               {
4007                 merge_overrides (binfo_member (BINFO_TYPE (vbases),
4008                                                CLASSTYPE_VBASECLASSES (t)),
4009                                  vbases, 1, t);
4010                 vbases = TREE_CHAIN (vbases);
4011               }
4012           }
4013         }
4014     }
4015
4016   /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
4017      might need to know it for setting up the offsets in the vtable
4018      (or in thunks) below.  */
4019   if (vfield != NULL_TREE
4020       && DECL_FIELD_CONTEXT (vfield) != t)
4021     {
4022       tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4023       tree offset = BINFO_OFFSET (binfo);
4024
4025       vfield = copy_node (vfield);
4026       copy_lang_decl (vfield);
4027
4028       if (! integer_zerop (offset))
4029         offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4030       DECL_FIELD_CONTEXT (vfield) = t;
4031       DECL_CLASS_CONTEXT (vfield) = t;
4032       DECL_FIELD_BITPOS (vfield)
4033         = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4034       CLASSTYPE_VFIELD (t) = vfield;
4035     }
4036     
4037 #ifdef NOTQUITE
4038   cp_warning ("Doing hard virtuals for %T...", t);
4039 #endif
4040
4041   if (has_virtual > max_has_virtual)
4042     max_has_virtual = has_virtual;
4043   if (max_has_virtual > 0)
4044     TYPE_VIRTUAL_P (t) = 1;
4045
4046   if (flag_rtti && TYPE_VIRTUAL_P (t) && !pending_hard_virtuals)
4047     modify_all_vtables (t, NULL_TREE, NULL_TREE);
4048
4049   while (pending_hard_virtuals)
4050     {
4051       modify_all_vtables (t,
4052                           TREE_PURPOSE (pending_hard_virtuals),
4053                           TREE_VALUE (pending_hard_virtuals));
4054       pending_hard_virtuals = TREE_CHAIN (pending_hard_virtuals);
4055     }
4056   
4057   if (TYPE_USES_VIRTUAL_BASECLASSES (t))
4058     {
4059       tree vbases;
4060       /* Now fixup any virtual function entries from virtual bases
4061          that have different deltas.  This has to come after we do the
4062          pending hard virtuals, as we might have a function that comes
4063          from multiple virtual base instances that is only overridden
4064          by a hard virtual above.  */
4065       vbases = CLASSTYPE_VBASECLASSES (t);
4066       while (vbases)
4067         {
4068           /* We might be able to shorten the amount of work we do by
4069              only doing this for vtables that come from virtual bases
4070              that have differing offsets, but don't want to miss any
4071              entries.  */
4072           fixup_vtable_deltas (vbases, 1, t);
4073           vbases = TREE_CHAIN (vbases);
4074         }
4075     }
4076
4077   /* Under our model of GC, every C++ class gets its own virtual
4078      function table, at least virtually.  */
4079   if (pending_virtuals)
4080     {
4081       pending_virtuals = nreverse (pending_virtuals);
4082       /* We must enter these virtuals into the table.  */
4083       if (first_vfn_base_index < 0)
4084         {
4085           /* The second slot is for the tdesc pointer when thunks are used.  */
4086           if (flag_vtable_thunks)
4087             pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4088
4089           /* The first slot is for the rtti offset.  */
4090           pending_virtuals = tree_cons (NULL_TREE, NULL_TREE, pending_virtuals);
4091
4092           set_rtti_entry (pending_virtuals,
4093                           convert (ssizetype, integer_zero_node), t);
4094           build_vtable (NULL_TREE, t);
4095         }
4096       else
4097         {
4098           /* Here we know enough to change the type of our virtual
4099              function table, but we will wait until later this function.  */
4100
4101           if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
4102             build_vtable (TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index), t);
4103         }
4104
4105       /* If this type has basetypes with constructors, then those
4106          constructors might clobber the virtual function table.  But
4107          they don't if the derived class shares the exact vtable of the base
4108          class.  */
4109
4110       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4111     }
4112   else if (first_vfn_base_index >= 0)
4113     {
4114       tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (t), first_vfn_base_index);
4115       /* This class contributes nothing new to the virtual function
4116          table.  However, it may have declared functions which
4117          went into the virtual function table "inherited" from the
4118          base class.  If so, we grab a copy of those updated functions,
4119          and pretend they are ours.  */
4120
4121       /* See if we should steal the virtual info from base class.  */
4122       if (TYPE_BINFO_VTABLE (t) == NULL_TREE)
4123         TYPE_BINFO_VTABLE (t) = BINFO_VTABLE (binfo);
4124       if (TYPE_BINFO_VIRTUALS (t) == NULL_TREE)
4125         TYPE_BINFO_VIRTUALS (t) = BINFO_VIRTUALS (binfo);
4126       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
4127         CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
4128     }
4129
4130   if (max_has_virtual || first_vfn_base_index >= 0)
4131     {
4132       CLASSTYPE_VSIZE (t) = has_virtual;
4133       if (first_vfn_base_index >= 0)
4134         {
4135           if (pending_virtuals)
4136             TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t),
4137                                                 pending_virtuals);
4138         }
4139       else if (has_virtual)
4140         {
4141           TYPE_BINFO_VIRTUALS (t) = pending_virtuals;
4142           DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)) = 1;
4143         }
4144     }
4145
4146   /* Now lay out the virtual function table.  */
4147   if (has_virtual)
4148     {
4149       /* Use size_int so values are memoized in common cases.  */
4150       tree itype = build_index_type (size_int (has_virtual));
4151       tree atype = build_cplus_array_type (vtable_entry_type, itype);
4152
4153       layout_type (atype);
4154
4155       CLASSTYPE_VFIELD (t) = vfield;
4156
4157       /* We may have to grow the vtable.  */
4158       if (TREE_TYPE (TYPE_BINFO_VTABLE (t)) != atype)
4159         {
4160           TREE_TYPE (TYPE_BINFO_VTABLE (t)) = atype;
4161           DECL_SIZE (TYPE_BINFO_VTABLE (t)) = 0;
4162           layout_decl (TYPE_BINFO_VTABLE (t), 0);
4163           /* At one time the vtable info was grabbed 2 words at a time.  This
4164              fails on sparc unless you have 8-byte alignment.  (tiemann) */
4165           DECL_ALIGN (TYPE_BINFO_VTABLE (t))
4166             = MAX (TYPE_ALIGN (double_type_node),
4167                    DECL_ALIGN (TYPE_BINFO_VTABLE (t)));
4168         }
4169     }
4170   else if (first_vfn_base_index >= 0)
4171     CLASSTYPE_VFIELD (t) = vfield;
4172   CLASSTYPE_VFIELDS (t) = vfields;
4173
4174   finish_struct_bits (t, max_has_virtual);
4175
4176   /* Complete the rtl for any static member objects of the type we're
4177      working on.  */
4178   for (x = fields; x; x = TREE_CHAIN (x))
4179     {
4180       if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
4181           && TREE_TYPE (x) == t)
4182         {
4183           DECL_MODE (x) = TYPE_MODE (t);
4184           make_decl_rtl (x, NULL, 0);
4185         }
4186     }
4187
4188   if (TYPE_HAS_CONSTRUCTOR (t))
4189     {
4190       tree vfields = CLASSTYPE_VFIELDS (t);
4191
4192       while (vfields)
4193         {
4194           /* Mark the fact that constructor for T
4195              could affect anybody inheriting from T
4196              who wants to initialize vtables for VFIELDS's type.  */
4197           if (VF_DERIVED_VALUE (vfields))
4198             TREE_ADDRESSABLE (vfields) = 1;
4199           vfields = TREE_CHAIN (vfields);
4200         }
4201       if (any_default_members != 0)
4202         build_class_init_list (t);
4203     }
4204   else if (TYPE_NEEDS_CONSTRUCTING (t))
4205     build_class_init_list (t);
4206
4207   /* Write out inline function definitions.  */
4208   do_inline_function_hair (t, CLASSTYPE_INLINE_FRIENDS (t));
4209   CLASSTYPE_INLINE_FRIENDS (t) = 0;
4210
4211   if (CLASSTYPE_VSIZE (t) != 0)
4212     {
4213 #if 0
4214       /* This is now done above.  */
4215       if (DECL_FIELD_CONTEXT (vfield) != t)
4216         {
4217           tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
4218           tree offset = BINFO_OFFSET (binfo);
4219
4220           vfield = copy_node (vfield);
4221           copy_lang_decl (vfield);
4222
4223           if (! integer_zerop (offset))
4224             offset = size_binop (MULT_EXPR, offset, size_int (BITS_PER_UNIT));
4225           DECL_FIELD_CONTEXT (vfield) = t;
4226           DECL_CLASS_CONTEXT (vfield) = t;
4227           DECL_FIELD_BITPOS (vfield)
4228             = size_binop (PLUS_EXPR, offset, DECL_FIELD_BITPOS (vfield));
4229           CLASSTYPE_VFIELD (t) = vfield;
4230         }
4231 #endif
4232
4233       /* In addition to this one, all the other vfields should be listed.  */
4234       /* Before that can be done, we have to have FIELD_DECLs for them, and
4235          a place to find them.  */
4236       TYPE_NONCOPIED_PARTS (t) = build_tree_list (default_conversion (TYPE_BINFO_VTABLE (t)), vfield);
4237
4238       if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
4239           && DECL_VINDEX (TREE_VEC_ELT (method_vec, 1)) == NULL_TREE)
4240         cp_warning ("`%#T' has virtual functions but non-virtual destructor",
4241                     t);
4242     }
4243
4244   /* Make the rtl for any new vtables we have created, and unmark
4245      the base types we marked.  */
4246   finish_vtbls (TYPE_BINFO (t), 1, t);
4247   hack_incomplete_structures (t);
4248
4249 #if 0
4250   if (TYPE_NAME (t) && TYPE_IDENTIFIER (t))
4251     undo_template_name_overload (TYPE_IDENTIFIER (t), 1);
4252 #endif
4253
4254   resume_momentary (old);
4255
4256   if (warn_overloaded_virtual)
4257     warn_hidden (t);
4258
4259 #if 0
4260   /* This has to be done after we have sorted out what to do with
4261      the enclosing type.  */
4262   if (write_symbols != DWARF_DEBUG)
4263     {
4264       /* Be smarter about nested classes here.  If a type is nested,
4265          only output it if we would output the enclosing type.  */
4266       if (DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (t)))
4267         DECL_IGNORED_P (TYPE_MAIN_DECL (t)) = TREE_ASM_WRITTEN (TYPE_MAIN_DECL (t));
4268     }
4269 #endif
4270
4271   if (write_symbols != DWARF_DEBUG && write_symbols != DWARF2_DEBUG)
4272     {
4273       /* If the type has methods, we want to think about cutting down
4274          the amount of symbol table stuff we output.  The value stored in
4275          the TYPE_DECL's DECL_IGNORED_P slot is a first approximation.
4276          For example, if a member function is seen and we decide to
4277          write out that member function, then we can change the value
4278          of the DECL_IGNORED_P slot, and the type will be output when
4279          that member function's debug info is written out.
4280
4281          We can't do this with DWARF, which does not support name
4282          references between translation units.  */
4283       if (CLASSTYPE_METHOD_VEC (t))
4284         {
4285           /* Don't output full info about any type
4286              which does not have its implementation defined here.  */
4287           if (CLASSTYPE_INTERFACE_ONLY (t))
4288             TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4289 #if 0
4290           /* XXX do something about this.  */
4291           else if (CLASSTYPE_INTERFACE_UNKNOWN (t))
4292             /* Only a first approximation!  */
4293             TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4294 #endif
4295         }
4296       else if (CLASSTYPE_INTERFACE_ONLY (t))
4297         TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 1;
4298     }
4299
4300   /* Finish debugging output for this type.  */
4301   rest_of_type_compilation (t, toplevel_bindings_p ());
4302
4303   return t;
4304 }
4305
4306 /* In [basic.scope.class] we have:
4307
4308      A name N used in a class S shall refer to the same declaration in
4309      its context and when re-evaluated in the completed scope of S.
4310      
4311    This function checks this condition for X, which is a member of
4312    T.  */
4313
4314 static void
4315 check_member_decl_is_same_in_complete_scope (t, x)
4316      tree t;
4317      tree x;
4318 {
4319   /* A name N used in a class S shall refer to the same declaration in
4320      its context and when re-evaluated in the completed scope of S.
4321      
4322      Enums, types and static vars have already been checked.  */
4323   if (TREE_CODE (x) != USING_DECL 
4324       && TREE_CODE (x) != TYPE_DECL && !DECL_CLASS_TEMPLATE_P (x)
4325       && TREE_CODE (x) != CONST_DECL && TREE_CODE (x) != VAR_DECL)
4326     {
4327       tree name = DECL_NAME (x);
4328       tree icv;
4329
4330       /* Don't get confused by access decls.  */
4331       if (name && TREE_CODE (name) == IDENTIFIER_NODE)
4332         icv = IDENTIFIER_CLASS_VALUE (name);
4333       else
4334         icv = NULL_TREE;
4335
4336       if (icv
4337           /* Don't complain about constructors.  */
4338           && name != constructor_name (current_class_type)
4339           /* Or inherited names.  */
4340           && id_in_current_class (name)
4341           /* Or shadowed tags.  */
4342           && !(TREE_CODE (icv) == TYPE_DECL && DECL_CONTEXT (icv) == t))
4343         {
4344           cp_pedwarn_at ("declaration of identifier `%D' as `%+#D'",
4345                          name, x);
4346           cp_pedwarn_at ("conflicts with other use in class as `%#D'",
4347                          icv);
4348         }
4349     }
4350 }
4351
4352 /* When T was built up, the member declarations were added in reverse
4353    order.  Rearrange them to declaration order.  */
4354
4355 void
4356 unreverse_member_declarations (t)
4357      tree t;
4358 {
4359   tree next;
4360   tree prev;
4361   tree x;
4362
4363   /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
4364      reverse order.  Put them in declaration order now.  */
4365   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
4366   CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
4367
4368   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
4369      reverse order, so we can't just use nreverse.  */
4370   prev = NULL_TREE;
4371   for (x = TYPE_FIELDS (t); 
4372        x && TREE_CODE (x) != TYPE_DECL; 
4373        x = next)
4374     {
4375       next = TREE_CHAIN (x);
4376       TREE_CHAIN (x) = prev;
4377       prev = x;
4378     }
4379   if (prev)
4380     {
4381       TREE_CHAIN (TYPE_FIELDS (t)) = x;
4382       if (prev)
4383         TYPE_FIELDS (t) = prev;
4384     }
4385 }
4386
4387 tree
4388 finish_struct (t, attributes, warn_anon)
4389      tree t, attributes;
4390      int warn_anon;
4391 {
4392   tree name = TYPE_NAME (t);
4393   tree x;
4394
4395   if (TREE_CODE (name) == TYPE_DECL)
4396     {
4397       extern int lineno;
4398           
4399       DECL_SOURCE_FILE (name) = input_filename;
4400       /* For TYPE_DECL that are not typedefs (those marked with a line
4401          number of zero, we don't want to mark them as real typedefs.
4402          If this fails one needs to make sure real typedefs have a
4403          previous line number, even if it is wrong, that way the below
4404          will fill in the right line number.  (mrs) */
4405       if (DECL_SOURCE_LINE (name))
4406         DECL_SOURCE_LINE (name) = lineno;
4407       CLASSTYPE_SOURCE_LINE (t) = lineno;
4408       name = DECL_NAME (name);
4409     }
4410
4411   /* Append the fields we need for constructing signature tables.  */
4412   if (IS_SIGNATURE (t))
4413     append_signature_fields (t);
4414
4415   /* Now that we've got all the field declarations, reverse everything
4416      as necessary.  */
4417   unreverse_member_declarations (t);
4418
4419   if (flag_optional_diags) 
4420     {
4421       for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
4422         check_member_decl_is_same_in_complete_scope (t, x);
4423       for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
4424         check_member_decl_is_same_in_complete_scope (t, x);
4425     }
4426
4427   /* Mark all the tags in the class as class-local.  */
4428   for (x = CLASSTYPE_TAGS (t); x; x = TREE_CHAIN (x))
4429     TREE_NONLOCAL_FLAG (TREE_VALUE (x)) = 0;
4430
4431   cplus_decl_attributes (t, attributes, NULL_TREE);
4432
4433   if (processing_template_decl)
4434     {
4435       tree d = getdecls ();
4436       for (; d; d = TREE_CHAIN (d))
4437         {
4438           /* If this is the decl for the class or one of the template
4439              parms, we've seen all the injected decls.  */
4440           if ((TREE_CODE (d) == TYPE_DECL
4441                && (TREE_TYPE (d) == t
4442                    || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TYPE_PARM
4443                    || TREE_CODE (TREE_TYPE (d)) == TEMPLATE_TEMPLATE_PARM))
4444               || TREE_CODE (d) == CONST_DECL)
4445             break;
4446           /* Don't inject cache decls.  */
4447           else if (IDENTIFIER_TEMPLATE (DECL_NAME (d)))
4448             continue;
4449           DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t))
4450             = tree_cons (NULL_TREE, d,
4451                          DECL_TEMPLATE_INJECT (CLASSTYPE_TI_TEMPLATE (t)));
4452         }
4453       finish_struct_methods (t);
4454       TYPE_SIZE (t) = integer_zero_node;
4455     }      
4456   else
4457     t = finish_struct_1 (t, warn_anon);
4458
4459   TYPE_BEING_DEFINED (t) = 0;
4460   if (current_class_type)
4461     popclass (0);
4462   else
4463     error ("trying to finish struct, but kicked out due to previous parse errors.");
4464
4465   return t;
4466 }
4467 \f
4468 /* Return the dynamic type of INSTANCE, if known.
4469    Used to determine whether the virtual function table is needed
4470    or not.
4471
4472    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4473    of our knowledge of its type.  */
4474
4475 tree
4476 fixed_type_or_null (instance, nonnull)
4477      tree instance;
4478      int *nonnull;
4479 {
4480   switch (TREE_CODE (instance))
4481     {
4482     case INDIRECT_REF:
4483       /* Check that we are not going through a cast of some sort.  */
4484       if (TREE_TYPE (instance)
4485           == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
4486         instance = TREE_OPERAND (instance, 0);
4487       /* fall through...  */
4488     case CALL_EXPR:
4489       /* This is a call to a constructor, hence it's never zero.  */
4490       if (TREE_HAS_CONSTRUCTOR (instance))
4491         {
4492           if (nonnull)
4493             *nonnull = 1;
4494           return TREE_TYPE (instance);
4495         }
4496       return NULL_TREE;
4497
4498     case SAVE_EXPR:
4499       /* This is a call to a constructor, hence it's never zero.  */
4500       if (TREE_HAS_CONSTRUCTOR (instance))
4501         {
4502           if (nonnull)
4503             *nonnull = 1;
4504           return TREE_TYPE (instance);
4505         }
4506       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4507
4508     case RTL_EXPR:
4509       return NULL_TREE;
4510
4511     case PLUS_EXPR:
4512     case MINUS_EXPR:
4513       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
4514         /* Propagate nonnull.  */
4515         fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4516       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
4517         return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4518       return NULL_TREE;
4519
4520     case NOP_EXPR:
4521     case CONVERT_EXPR:
4522       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4523
4524     case ADDR_EXPR:
4525       if (nonnull)
4526         *nonnull = 1;
4527       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull);
4528
4529     case COMPONENT_REF:
4530       return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull);
4531
4532     case VAR_DECL:
4533     case FIELD_DECL:
4534       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
4535           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
4536         {
4537           if (nonnull)
4538             *nonnull = 1;
4539           return TREE_TYPE (TREE_TYPE (instance));
4540         }
4541       /* fall through...  */
4542     case TARGET_EXPR:
4543     case PARM_DECL:
4544       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
4545         {
4546           if (nonnull)
4547             *nonnull = 1;
4548           return TREE_TYPE (instance);
4549         }
4550       else if (nonnull)
4551         {
4552           if (instance == current_class_ptr
4553               && flag_this_is_variable <= 0)
4554             {
4555               /* Normally, 'this' must be non-null.  */
4556               if (flag_this_is_variable == 0)
4557                 *nonnull = 1;
4558
4559               /* <0 means we're in a constructor and we know our type.  */
4560               if (flag_this_is_variable < 0)
4561                 return TREE_TYPE (TREE_TYPE (instance));
4562             }
4563           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
4564             /* Reference variables should be references to objects.  */
4565             *nonnull = 1;
4566         }
4567       return NULL_TREE;
4568
4569     default:
4570       return NULL_TREE;
4571     }
4572 }
4573
4574 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
4575    to the static type.  We also handle the case where INSTANCE is really
4576    a pointer.
4577
4578    Used to determine whether the virtual function table is needed
4579    or not.
4580
4581    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
4582    of our knowledge of its type.  */
4583
4584 int
4585 resolves_to_fixed_type_p (instance, nonnull)
4586      tree instance;
4587      int *nonnull;
4588 {
4589   tree t = TREE_TYPE (instance);
4590   tree fixed = fixed_type_or_null (instance, nonnull);
4591   if (fixed == NULL_TREE)
4592     return 0;
4593   if (POINTER_TYPE_P (t))
4594     t = TREE_TYPE (t);
4595   return same_type_p (TYPE_MAIN_VARIANT (t), TYPE_MAIN_VARIANT (fixed));
4596 }
4597
4598 \f
4599 void
4600 init_class_processing ()
4601 {
4602   current_class_depth = 0;
4603   current_class_stack_size = 10;
4604   current_class_stack 
4605     = (class_stack_node_t) xmalloc (current_class_stack_size 
4606                                     * sizeof (struct class_stack_node));
4607
4608   current_lang_stacksize = 10;
4609   current_lang_base = (tree *)xmalloc(current_lang_stacksize * sizeof (tree));
4610   current_lang_stack = current_lang_base;
4611
4612   access_default_node = build_int_2 (0, 0);
4613   access_public_node = build_int_2 (1, 0);
4614   access_protected_node = build_int_2 (2, 0);
4615   access_private_node = build_int_2 (3, 0);
4616   access_default_virtual_node = build_int_2 (4, 0);
4617   access_public_virtual_node = build_int_2 (5, 0);
4618   access_protected_virtual_node = build_int_2 (6, 0);
4619   access_private_virtual_node = build_int_2 (7, 0);
4620
4621   /* Keep these values lying around.  */
4622   base_layout_decl = build_lang_field_decl (FIELD_DECL, NULL_TREE, error_mark_node);
4623   TREE_TYPE (base_layout_decl) = make_node (RECORD_TYPE);
4624
4625   gcc_obstack_init (&class_obstack);
4626 }
4627
4628 /* Set current scope to NAME. CODE tells us if this is a
4629    STRUCT, UNION, or ENUM environment.
4630
4631    NAME may end up being NULL_TREE if this is an anonymous or
4632    late-bound struct (as in "struct { ... } foo;")  */
4633
4634 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
4635    appropriate values, found by looking up the type definition of
4636    NAME (as a CODE).
4637
4638    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
4639    which can be seen locally to the class.  They are shadowed by
4640    any subsequent local declaration (including parameter names).
4641
4642    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
4643    which have static meaning (i.e., static members, static
4644    member functions, enum declarations, etc).
4645
4646    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
4647    which can be seen locally to the class (as in 1), but
4648    know that we are doing this for declaration purposes
4649    (i.e. friend foo::bar (int)).
4650
4651    So that we may avoid calls to lookup_name, we cache the _TYPE
4652    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
4653
4654    For multiple inheritance, we perform a two-pass depth-first search
4655    of the type lattice.  The first pass performs a pre-order search,
4656    marking types after the type has had its fields installed in
4657    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
4658    unmarks the marked types.  If a field or member function name
4659    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
4660    that name becomes `error_mark_node'.  */
4661
4662 void
4663 pushclass (type, modify)
4664      tree type;
4665      int modify;
4666 {
4667   type = TYPE_MAIN_VARIANT (type);
4668
4669   /* Make sure there is enough room for the new entry on the stack.  */
4670   if (current_class_depth + 1 >= current_class_stack_size) 
4671     {
4672       current_class_stack_size *= 2;
4673       current_class_stack
4674         = (class_stack_node_t) xrealloc (current_class_stack,
4675                                          current_class_stack_size
4676                                          * sizeof (struct class_stack_node));
4677     }
4678
4679   /* Insert a new entry on the class stack.  */
4680   current_class_stack[current_class_depth].name = current_class_name;
4681   current_class_stack[current_class_depth].type = current_class_type;
4682   current_class_stack[current_class_depth].access = current_access_specifier;
4683   current_class_depth++;
4684
4685   /* Now set up the new type.  */
4686   current_class_name = TYPE_NAME (type);
4687   if (TREE_CODE (current_class_name) == TYPE_DECL)
4688     current_class_name = DECL_NAME (current_class_name);
4689   current_class_type = type;
4690
4691   /* By default, things in classes are private, while things in
4692      structures or unions are public.  */
4693   current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
4694                               ? access_private_node 
4695                               : access_public_node);
4696
4697   if (previous_class_type != NULL_TREE
4698       && (type != previous_class_type || TYPE_SIZE (previous_class_type) == NULL_TREE)
4699       && current_class_depth == 1)
4700     {
4701       /* Forcibly remove any old class remnants.  */
4702       popclass (-1);
4703       previous_class_type = NULL_TREE;
4704     }
4705
4706   pushlevel_class ();
4707
4708 #if 0
4709   if (CLASSTYPE_TEMPLATE_INFO (type))
4710     overload_template_name (type);
4711 #endif
4712
4713   if (modify)
4714     {
4715       tree tags;
4716       tree this_fndecl = current_function_decl;
4717
4718       if (current_function_decl
4719           && DECL_CONTEXT (current_function_decl)
4720           && TREE_CODE (DECL_CONTEXT (current_function_decl)) == FUNCTION_DECL)
4721         current_function_decl = DECL_CONTEXT (current_function_decl);
4722       else
4723         current_function_decl = NULL_TREE;
4724
4725       if (type != previous_class_type || current_class_depth > 1)
4726         {
4727 #ifdef MI_MATRIX
4728           build_mi_matrix (type);
4729           push_class_decls (type);
4730           free_mi_matrix ();
4731 #else
4732           push_class_decls (type);
4733 #endif
4734         }
4735       else
4736         {
4737           tree item;
4738
4739           /* Hooray, we successfully cached; let's just install the
4740              cached class_shadowed list, and walk through it to get the
4741              IDENTIFIER_TYPE_VALUEs correct.  */
4742           set_class_shadows (previous_class_values);
4743           for (item = previous_class_values; item; item = TREE_CHAIN (item))
4744             {
4745               tree id = TREE_PURPOSE (item);
4746               tree decl = IDENTIFIER_CLASS_VALUE (id);
4747
4748               if (TREE_CODE (decl) == TYPE_DECL)
4749                 set_identifier_type_value (id, TREE_TYPE (decl));
4750             }
4751           unuse_fields (type);
4752         }
4753
4754       for (tags = CLASSTYPE_TAGS (type); tags; tags = TREE_CHAIN (tags))
4755         {
4756           tree tag_type = TREE_VALUE (tags);
4757
4758           TREE_NONLOCAL_FLAG (tag_type) = 1;
4759           if (! TREE_PURPOSE (tags))
4760             continue;
4761           if (! (IS_AGGR_TYPE_CODE (TREE_CODE (tag_type))
4762                  && CLASSTYPE_IS_TEMPLATE (tag_type)))
4763             pushtag (TREE_PURPOSE (tags), tag_type, 0);
4764           else
4765             pushdecl_class_level (CLASSTYPE_TI_TEMPLATE (tag_type));
4766         }
4767
4768       current_function_decl = this_fndecl;
4769     }
4770 }
4771  
4772 /* Get out of the current class scope. If we were in a class scope
4773    previously, that is the one popped to.  The flag MODIFY tells whether
4774    the current scope declarations needs to be modified as a result of
4775    popping to the previous scope.  0 is used for class definitions.  */
4776
4777 void
4778 popclass (modify)
4779      int modify;
4780 {
4781   if (modify < 0)
4782     {
4783       /* Back this old class out completely.  */
4784       tree tags = CLASSTYPE_TAGS (previous_class_type);
4785       tree t;
4786
4787       /* This code can be seen as a cache miss.  When we've cached a
4788          class' scope's bindings and we can't use them, we need to reset
4789          them.  This is it!  */
4790       for (t = previous_class_values; t; t = TREE_CHAIN (t))
4791         IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
4792       while (tags)
4793         {
4794           TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4795           tags = TREE_CHAIN (tags);
4796         }
4797       goto ret;
4798     }
4799
4800   if (modify)
4801     {
4802       /* Just remove from this class what didn't make
4803          it into IDENTIFIER_CLASS_VALUE.  */
4804       tree tags = CLASSTYPE_TAGS (current_class_type);
4805
4806       while (tags)
4807         {
4808           TREE_NONLOCAL_FLAG (TREE_VALUE (tags)) = 0;
4809           tags = TREE_CHAIN (tags);
4810         }
4811     }
4812
4813   /* Force clearing of IDENTIFIER_CLASS_VALUEs after a class definition,
4814      since not all class decls make it there currently.  */
4815   poplevel_class (! modify);
4816
4817   /* Since poplevel_class does the popping of class decls nowadays,
4818      this really only frees the obstack used for these decls.
4819      That's why it had to be moved down here.  */
4820   if (modify)
4821     pop_class_decls ();
4822
4823   current_class_depth--;
4824   current_class_name = current_class_stack[current_class_depth].name;
4825   current_class_type = current_class_stack[current_class_depth].type;
4826   current_access_specifier = current_class_stack[current_class_depth].access;
4827
4828  ret:
4829   ;
4830 }
4831
4832 /* Returns 1 if current_class_type is either T or a nested type of T.  */
4833
4834 int
4835 currently_open_class (t)
4836      tree t;
4837 {
4838   int i;
4839   if (t == current_class_type)
4840     return 1;
4841   for (i = 0; i < current_class_depth; ++i)
4842     if (current_class_stack [i].type == t)
4843       return 1;
4844   return 0;
4845 }
4846
4847 /* When entering a class scope, all enclosing class scopes' names with
4848    static meaning (static variables, static functions, types and enumerators)
4849    have to be visible.  This recursive function calls pushclass for all
4850    enclosing class contexts until global or a local scope is reached.
4851    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
4852    formal of the same name.  */
4853
4854 void
4855 push_nested_class (type, modify)
4856      tree type;
4857      int modify;
4858 {
4859   tree context;
4860
4861   my_friendly_assert (!type || TREE_CODE (type) != NAMESPACE_DECL, 980711);
4862
4863   if (type == NULL_TREE || type == error_mark_node || ! IS_AGGR_TYPE (type)
4864       || TREE_CODE (type) == TEMPLATE_TYPE_PARM
4865       || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM)
4866     return;
4867   
4868   context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
4869
4870   if (context && TREE_CODE (context) == RECORD_TYPE)
4871     push_nested_class (context, 2);
4872   pushclass (type, modify);
4873 }
4874
4875 /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
4876
4877 void
4878 pop_nested_class (modify)
4879      int modify;
4880 {
4881   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
4882
4883   popclass (modify);
4884   if (context && TREE_CODE (context) == RECORD_TYPE)
4885     pop_nested_class (modify);
4886 }
4887
4888 /* Set global variables CURRENT_LANG_NAME to appropriate value
4889    so that behavior of name-mangling machinery is correct.  */
4890
4891 void
4892 push_lang_context (name)
4893      tree name;
4894 {
4895   *current_lang_stack++ = current_lang_name;
4896   if (current_lang_stack >= current_lang_base + current_lang_stacksize)
4897     {
4898       current_lang_base
4899         = (tree *)xrealloc (current_lang_base,
4900                             sizeof (tree) * (current_lang_stacksize + 10));
4901       current_lang_stack = current_lang_base + current_lang_stacksize;
4902       current_lang_stacksize += 10;
4903     }
4904
4905   if (name == lang_name_cplusplus || name == lang_name_java)
4906     {
4907       strict_prototype = strict_prototypes_lang_cplusplus;
4908       current_lang_name = name;
4909     }
4910   else if (name == lang_name_c)
4911     {
4912       strict_prototype = strict_prototypes_lang_c;
4913       current_lang_name = name;
4914     }
4915   else
4916     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
4917 }
4918   
4919 /* Get out of the current language scope.  */
4920
4921 void
4922 pop_lang_context ()
4923 {
4924   current_lang_name = *--current_lang_stack;
4925   if (current_lang_name == lang_name_cplusplus
4926       || current_lang_name == lang_name_java)
4927     strict_prototype = strict_prototypes_lang_cplusplus;
4928   else if (current_lang_name == lang_name_c)
4929     strict_prototype = strict_prototypes_lang_c;
4930 }
4931 \f
4932 /* Type instantiation routines.  */
4933
4934 static tree
4935 validate_lhs (lhstype, complain)
4936      tree lhstype;
4937      int complain;
4938 {
4939   if (TYPE_PTRMEMFUNC_P (lhstype))
4940     lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
4941
4942   if (TREE_CODE (lhstype) == POINTER_TYPE)
4943     {
4944       if (TREE_CODE (TREE_TYPE (lhstype)) == FUNCTION_TYPE
4945           || TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
4946         lhstype = TREE_TYPE (lhstype);
4947       else
4948         {
4949           if (complain)
4950             error ("invalid type combination for overload");
4951           return error_mark_node;
4952         }
4953     }
4954   return lhstype;
4955 }
4956
4957 /* This function will instantiate the type of the expression given in
4958    RHS to match the type of LHSTYPE.  If errors exist, then return
4959    error_mark_node.  If only complain is COMPLAIN is set.  If we are
4960    not complaining, never modify rhs, as overload resolution wants to
4961    try many possible instantiations, in hopes that at least one will
4962    work.
4963
4964    For non-recursive calls, LHSTYPE should be a function, pointer to
4965    function, or a pointer to member function.  */
4966
4967 tree
4968 instantiate_type (lhstype, rhs, complain)
4969      tree lhstype, rhs;
4970      int complain;
4971 {
4972   tree explicit_targs = NULL_TREE;
4973   int template_only = 0;
4974
4975   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
4976     {
4977       if (complain)
4978         error ("not enough type information");
4979       return error_mark_node;
4980     }
4981
4982   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
4983     {
4984       if (same_type_p (lhstype, TREE_TYPE (rhs)))
4985         return rhs;
4986       if (complain)
4987         cp_error ("argument of type `%T' does not match `%T'",
4988                   TREE_TYPE (rhs), lhstype);
4989       return error_mark_node;
4990     }
4991
4992   /* We don't overwrite rhs if it is an overloaded function.
4993      Copying it would destroy the tree link.  */
4994   if (TREE_CODE (rhs) != OVERLOAD)
4995     rhs = copy_node (rhs);
4996
4997   /* This should really only be used when attempting to distinguish
4998      what sort of a pointer to function we have.  For now, any
4999      arithmetic operation which is not supported on pointers
5000      is rejected as an error.  */
5001
5002   switch (TREE_CODE (rhs))
5003     {
5004     case TYPE_EXPR:
5005     case CONVERT_EXPR:
5006     case SAVE_EXPR:
5007     case CONSTRUCTOR:
5008     case BUFFER_REF:
5009       my_friendly_abort (177);
5010       return error_mark_node;
5011
5012     case INDIRECT_REF:
5013     case ARRAY_REF:
5014       {
5015         tree new_rhs;
5016
5017         new_rhs = instantiate_type (build_pointer_type (lhstype),
5018                                     TREE_OPERAND (rhs, 0), complain);
5019         if (new_rhs == error_mark_node)
5020           return error_mark_node;
5021
5022         TREE_TYPE (rhs) = lhstype;
5023         TREE_OPERAND (rhs, 0) = new_rhs;
5024         return rhs;
5025       }
5026
5027     case NOP_EXPR:
5028       rhs = copy_node (TREE_OPERAND (rhs, 0));
5029       TREE_TYPE (rhs) = unknown_type_node;
5030       return instantiate_type (lhstype, rhs, complain);
5031
5032     case COMPONENT_REF:
5033       {
5034         tree field = TREE_OPERAND (rhs, 1);
5035         if (TREE_CODE (field) == TREE_LIST)
5036           {
5037             tree function = instantiate_type (lhstype, field, complain);
5038             if (function == error_mark_node)
5039               return error_mark_node;
5040             my_friendly_assert (TREE_CODE (function) == FUNCTION_DECL, 185);
5041
5042             if (! DECL_STATIC_FUNCTION_P (function))
5043               {
5044                 tree t = TREE_TYPE (TREE_OPERAND (rhs, 0));
5045                 if (TYPE_MAIN_VARIANT (t) == current_class_type)
5046                   t = constructor_name (t);
5047
5048                 cp_error ("object-dependent reference to `%D' can only be used in a call",
5049                           function);
5050                 cp_error ("  to form a pointer to member function, say `&%T::%D'",
5051                           t, DECL_NAME (function));
5052               }
5053
5054             mark_used (function);
5055             return function;
5056           }
5057
5058         /* I could not trigger this code. MvL */
5059         my_friendly_abort (980326);
5060         return rhs;
5061       }
5062
5063     case OFFSET_REF:
5064       /* This can happen if we are forming a pointer-to-member for a
5065          member template.  */
5066       rhs = TREE_OPERAND (rhs, 1);
5067       my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
5068         
5069       /* Fall through.  */
5070
5071     case TEMPLATE_ID_EXPR:
5072       {
5073         explicit_targs = TREE_OPERAND (rhs, 1);
5074         template_only = 1;
5075         rhs = TREE_OPERAND (rhs, 0);
5076       }
5077       /* fall through */
5078       my_friendly_assert (TREE_CODE (rhs) == OVERLOAD, 980401);
5079
5080     case OVERLOAD:
5081       {
5082         tree elem, elems;
5083
5084         /* Check that the LHSTYPE and the RHS are reasonable.  */
5085         lhstype = validate_lhs (lhstype, complain);
5086         if (lhstype == error_mark_node)
5087           return lhstype;
5088
5089         if (TREE_CODE (lhstype) != FUNCTION_TYPE
5090             && TREE_CODE (lhstype) != METHOD_TYPE)
5091           {
5092             if (complain)
5093               cp_error("cannot resolve overloaded function `%D' " 
5094                        "based on non-function type `%T'", 
5095                        DECL_NAME (OVL_FUNCTION (rhs)), lhstype);
5096             return error_mark_node;
5097           }
5098         
5099         /* Look for an exact match, by searching through the
5100            overloaded functions.  */
5101         if (template_only)
5102           /* If we're processing a template-id, only a template
5103              function can match, so we don't look through the
5104              overloaded functions.  */
5105           ;
5106         else for (elems = rhs; elems; elems = OVL_CHAIN (elems))
5107           {
5108             elem = OVL_FUNCTION (elems);
5109             if (TREE_CODE (elem) == FUNCTION_DECL
5110                 && same_type_p (lhstype, TREE_TYPE (elem)))
5111               {
5112                 mark_used (elem);
5113                 return elem;
5114               }
5115           }
5116
5117         /* No overloaded function was an exact match.  See if we can
5118            instantiate some template to match.  */
5119         {
5120           tree save_elem = 0;
5121           elems = rhs;
5122           if (TREE_CODE (elems) == TREE_LIST)
5123             elems = TREE_VALUE (rhs);
5124           for (; elems; elems = OVL_NEXT (elems))
5125             if (TREE_CODE (elem = OVL_CURRENT (elems)) == TEMPLATE_DECL)
5126               {
5127                 int n = DECL_NTPARMS (elem);
5128                 tree t = make_scratch_vec (n);
5129                 int i;
5130                 i = type_unification
5131                   (DECL_INNERMOST_TEMPLATE_PARMS (elem), t,
5132                    TYPE_ARG_TYPES (TREE_TYPE (elem)),
5133                    TYPE_ARG_TYPES (lhstype), explicit_targs, DEDUCE_EXACT, 1);
5134                 if (i == 0)
5135                   {
5136                     if (save_elem)
5137                       {
5138                         cp_error ("ambiguous template instantiation converting to `%#T'", lhstype);
5139                         return error_mark_node;
5140                       }
5141                     save_elem = instantiate_template (elem, t);
5142                     /* Check the return type.  */
5143                     if (!same_type_p (TREE_TYPE (lhstype),
5144                                       TREE_TYPE (TREE_TYPE (save_elem))))
5145                       save_elem = 0;
5146                   }
5147               }
5148           if (save_elem)
5149             {
5150               mark_used (save_elem);
5151               return save_elem;
5152             }
5153         }
5154
5155         /* There's no exact match, and no templates can be
5156            instantiated to match.  The last thing we try is to see if
5157            some ordinary overloaded function is close enough.  If
5158            we're only looking for template functions, we don't do
5159            this.  */
5160         if (!template_only)
5161           {
5162             for (elems = rhs; elems; elems = OVL_NEXT (elems))
5163               {
5164                 elem = OVL_CURRENT (elems);
5165                 if (TREE_CODE (elem) == FUNCTION_DECL
5166                     && comp_target_types (lhstype, TREE_TYPE (elem), 1) > 0)
5167                   break;
5168               }
5169             if (elems)
5170               {
5171                 tree save_elem = elem;
5172                 for (elems = OVL_CHAIN (elems); elems; 
5173                      elems = OVL_CHAIN (elems))
5174                   {
5175                     elem = OVL_FUNCTION (elems);
5176                     if (TREE_CODE (elem) == FUNCTION_DECL
5177                         && comp_target_types (lhstype, TREE_TYPE (elem), 0) >0)
5178                       break;
5179                   }
5180                 if (elems)
5181                   {
5182                     if (complain)
5183                       {
5184                         cp_error 
5185                           ("cannot resolve overload to target type `%#T'",
5186                            lhstype);
5187                         cp_error_at ("  ambiguity between `%#D'", save_elem); 
5188                         cp_error_at ("  and `%#D', at least", elem);
5189                       }
5190                     return error_mark_node;
5191                   }
5192                 mark_used (save_elem);
5193                 return save_elem;
5194               }
5195           }
5196
5197         /* We failed to find a match.  */
5198         if (complain)
5199           {
5200             cp_error ("cannot resolve overload to target type `%#T'", lhstype);
5201             cp_error 
5202               ("  because no suitable overload of function `%D' exists",
5203                DECL_NAME (OVL_FUNCTION (rhs)));
5204           }
5205         return error_mark_node;
5206       }
5207
5208     case TREE_LIST:
5209       {
5210         tree elem, baselink, name = NULL_TREE;
5211
5212         if (TREE_PURPOSE (rhs) == error_mark_node)
5213           {
5214             /* Make sure we don't drop the non-local flag, as the old code
5215                would rely on it. */
5216             int nl = TREE_NONLOCAL_FLAG (rhs);
5217             /* We don't need the type of this node. */
5218             rhs = TREE_VALUE (rhs);
5219             my_friendly_assert (TREE_NONLOCAL_FLAG (rhs) == nl, 980331);
5220           }
5221
5222         /* Now we should have a baselink. */
5223         my_friendly_assert (TREE_CODE (TREE_PURPOSE (rhs)) == TREE_VEC,
5224                             980331);
5225         /* First look for an exact match.  Search member functions.
5226            May have to undo what `default_conversion' might do to
5227            lhstype.  */
5228
5229         lhstype = validate_lhs (lhstype, complain);
5230         if (lhstype == error_mark_node)
5231           return lhstype;
5232
5233         my_friendly_assert (TREE_CHAIN (rhs) == NULL_TREE, 181);
5234         my_friendly_assert (TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL
5235                             || TREE_CODE (TREE_VALUE (rhs)) == OVERLOAD,
5236                             182);
5237
5238         for (baselink = rhs; baselink;
5239              baselink = next_baselink (baselink))
5240           {
5241             elem = TREE_VALUE (baselink);
5242             while (elem)
5243               if (same_type_p (lhstype, TREE_TYPE (OVL_CURRENT (elem))))
5244                 {
5245                   mark_used (OVL_CURRENT (elem));
5246                   return OVL_CURRENT (elem);
5247                 }
5248               else
5249                 elem = OVL_NEXT (elem);
5250           }
5251
5252         name = rhs;
5253         while (TREE_CODE (name) == TREE_LIST)
5254           name = TREE_VALUE (name);
5255         name = DECL_NAME (OVL_CURRENT (name));
5256
5257         if (complain)
5258           cp_error ("no compatible member functions named `%D'", name);
5259         return error_mark_node;
5260       }
5261
5262     case CALL_EXPR:
5263       /* This is too hard for now.  */
5264       my_friendly_abort (183);
5265       return error_mark_node;
5266
5267     case PLUS_EXPR:
5268     case MINUS_EXPR:
5269     case COMPOUND_EXPR:
5270       TREE_OPERAND (rhs, 0)
5271         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), complain);
5272       if (TREE_OPERAND (rhs, 0) == error_mark_node)
5273         return error_mark_node;
5274       TREE_OPERAND (rhs, 1)
5275         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5276       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5277         return error_mark_node;
5278
5279       TREE_TYPE (rhs) = lhstype;
5280       return rhs;
5281
5282     case MULT_EXPR:
5283     case TRUNC_DIV_EXPR:
5284     case FLOOR_DIV_EXPR:
5285     case CEIL_DIV_EXPR:
5286     case ROUND_DIV_EXPR:
5287     case RDIV_EXPR:
5288     case TRUNC_MOD_EXPR:
5289     case FLOOR_MOD_EXPR:
5290     case CEIL_MOD_EXPR:
5291     case ROUND_MOD_EXPR:
5292     case FIX_ROUND_EXPR:
5293     case FIX_FLOOR_EXPR:
5294     case FIX_CEIL_EXPR:
5295     case FIX_TRUNC_EXPR:
5296     case FLOAT_EXPR:
5297     case NEGATE_EXPR:
5298     case ABS_EXPR:
5299     case MAX_EXPR:
5300     case MIN_EXPR:
5301     case FFS_EXPR:
5302
5303     case BIT_AND_EXPR:
5304     case BIT_IOR_EXPR:
5305     case BIT_XOR_EXPR:
5306     case LSHIFT_EXPR:
5307     case RSHIFT_EXPR:
5308     case LROTATE_EXPR:
5309     case RROTATE_EXPR:
5310
5311     case PREINCREMENT_EXPR:
5312     case PREDECREMENT_EXPR:
5313     case POSTINCREMENT_EXPR:
5314     case POSTDECREMENT_EXPR:
5315       if (complain)
5316         error ("invalid operation on uninstantiated type");
5317       return error_mark_node;
5318
5319     case TRUTH_AND_EXPR:
5320     case TRUTH_OR_EXPR:
5321     case TRUTH_XOR_EXPR:
5322     case LT_EXPR:
5323     case LE_EXPR:
5324     case GT_EXPR:
5325     case GE_EXPR:
5326     case EQ_EXPR:
5327     case NE_EXPR:
5328     case TRUTH_ANDIF_EXPR:
5329     case TRUTH_ORIF_EXPR:
5330     case TRUTH_NOT_EXPR:
5331       if (complain)
5332         error ("not enough type information");
5333       return error_mark_node;
5334
5335     case COND_EXPR:
5336       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
5337         {
5338           if (complain)
5339             error ("not enough type information");
5340           return error_mark_node;
5341         }
5342       TREE_OPERAND (rhs, 1)
5343         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5344       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5345         return error_mark_node;
5346       TREE_OPERAND (rhs, 2)
5347         = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), complain);
5348       if (TREE_OPERAND (rhs, 2) == error_mark_node)
5349         return error_mark_node;
5350
5351       TREE_TYPE (rhs) = lhstype;
5352       return rhs;
5353
5354     case MODIFY_EXPR:
5355       TREE_OPERAND (rhs, 1)
5356         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), complain);
5357       if (TREE_OPERAND (rhs, 1) == error_mark_node)
5358         return error_mark_node;
5359
5360       TREE_TYPE (rhs) = lhstype;
5361       return rhs;
5362       
5363     case ADDR_EXPR:
5364       if (TYPE_PTRMEMFUNC_P (lhstype))
5365         lhstype = TYPE_PTRMEMFUNC_FN_TYPE (lhstype);
5366       else if (TREE_CODE (lhstype) != POINTER_TYPE)
5367         {
5368           if (complain)
5369             error ("type for resolving address of overloaded function must be pointer type");
5370           return error_mark_node;
5371         }
5372       {
5373         tree fn = instantiate_type (TREE_TYPE (lhstype), TREE_OPERAND (rhs, 0), complain);
5374         if (fn == error_mark_node)
5375           return error_mark_node;
5376         mark_addressable (fn);
5377         TREE_TYPE (rhs) = lhstype;
5378         TREE_OPERAND (rhs, 0) = fn;
5379         TREE_CONSTANT (rhs) = staticp (fn);
5380         if (TREE_CODE (lhstype) == POINTER_TYPE
5381             && TREE_CODE (TREE_TYPE (lhstype)) == METHOD_TYPE)
5382           {
5383             build_ptrmemfunc_type (lhstype);
5384             rhs = build_ptrmemfunc (lhstype, rhs, 0);
5385           }
5386       }
5387       return rhs;
5388
5389     case ENTRY_VALUE_EXPR:
5390       my_friendly_abort (184);
5391       return error_mark_node;
5392
5393     case ERROR_MARK:
5394       return error_mark_node;
5395
5396     default:
5397       my_friendly_abort (185);
5398       return error_mark_node;
5399     }
5400 }
5401 \f
5402 /* Return the name of the virtual function pointer field
5403    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
5404    this may have to look back through base types to find the
5405    ultimate field name.  (For single inheritance, these could
5406    all be the same name.  Who knows for multiple inheritance).  */
5407
5408 static tree
5409 get_vfield_name (type)
5410      tree type;
5411 {
5412   tree binfo = TYPE_BINFO (type);
5413   char *buf;
5414
5415   while (BINFO_BASETYPES (binfo)
5416          && TYPE_VIRTUAL_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
5417          && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
5418     binfo = BINFO_BASETYPE (binfo, 0);
5419
5420   type = BINFO_TYPE (binfo);
5421   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
5422                          + TYPE_NAME_LENGTH (type) + 2);
5423   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
5424   return get_identifier (buf);
5425 }
5426
5427 void
5428 print_class_statistics ()
5429 {
5430 #ifdef GATHER_STATISTICS
5431   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
5432   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
5433   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
5434            n_build_method_call, n_inner_fields_searched);
5435   if (n_vtables)
5436     {
5437       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
5438                n_vtables, n_vtable_searches);
5439       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
5440                n_vtable_entries, n_vtable_elems);
5441     }
5442 #endif
5443 }
5444
5445 /* Push an obstack which is sufficiently long-lived to hold such class
5446    decls that may be cached in the previous_class_values list.  For now, let's
5447    use the permanent obstack, later we may create a dedicated obstack just
5448    for this purpose.  The effect is undone by pop_obstacks.  */
5449
5450 void
5451 maybe_push_cache_obstack ()
5452 {
5453   push_obstacks_nochange ();
5454   if (current_class_depth == 1)
5455     current_obstack = &permanent_obstack;
5456 }
5457
5458 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
5459    according to [class]:
5460                                           The class-name is also inserted
5461    into  the scope of the class itself.  For purposes of access checking,
5462    the inserted class name is treated as if it were a public member name.  */
5463
5464 tree
5465 build_self_reference ()
5466 {
5467   tree name = constructor_name (current_class_type);
5468   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
5469   DECL_NONLOCAL (value) = 1;
5470   DECL_CONTEXT (value) = current_class_type;
5471   DECL_CLASS_CONTEXT (value) = current_class_type;
5472   DECL_ARTIFICIAL (value) = 1;
5473
5474   pushdecl_class_level (value);
5475   return value;
5476 }
5477
5478 /* Returns 1 if TYPE contains only padding bytes.  */
5479
5480 int
5481 is_empty_class (type)
5482      tree type;
5483 {
5484   tree t;
5485
5486   if (type == error_mark_node)
5487     return 0;
5488
5489   if (! IS_AGGR_TYPE (type))
5490     return 0;
5491
5492   if (flag_new_abi)
5493     return CLASSTYPE_SIZE (type) == integer_zero_node;
5494
5495   if (TYPE_BINFO_BASETYPES (type))
5496     return 0;
5497   t = TYPE_FIELDS (type);
5498   while (t && TREE_CODE (t) != FIELD_DECL)
5499     t = TREE_CHAIN (t);
5500   return (t == NULL_TREE);
5501 }
5502
5503 /* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
5504    a *_TYPE node.  NODE can also be a local class.  */
5505
5506 tree
5507 get_enclosing_class (type)
5508      tree type;
5509 {
5510   tree node = type;
5511
5512   while (node && TREE_CODE (node) != NAMESPACE_DECL)
5513     {
5514       switch (TREE_CODE_CLASS (TREE_CODE (node)))
5515         {
5516         case 'd':
5517           node = DECL_CONTEXT (node);
5518           break;
5519
5520         case 't':
5521           if (node != type)
5522             return node;
5523           node = TYPE_CONTEXT (node);
5524           break;
5525
5526         default:
5527           my_friendly_abort (0);
5528         }
5529     }
5530   return NULL_TREE;
5531 }
5532
5533 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
5534
5535 int
5536 is_base_of_enclosing_class (base, type)
5537      tree base, type;
5538 {
5539   while (type)
5540     {
5541       if (get_binfo (base, type, 0))
5542         return 1;
5543
5544       type = get_enclosing_class (type);
5545     }
5546   return 0;
5547 }