OSDN Git Service

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