OSDN Git Service

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