OSDN Git Service

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