OSDN Git Service

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