OSDN Git Service

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