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