OSDN Git Service

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