OSDN Git Service

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