OSDN Git Service

* emit-rtl.c (gen_reg_rtx): Also reallocate reg_decl array.
[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, 2001  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 /* The number of nested classes being processed.  If we are not in the
42    scope of any class, this is zero.  */
43
44 int current_class_depth;
45
46 /* In order to deal with nested classes, we keep a stack of classes.
47    The topmost entry is the innermost class, and is the entry at index
48    CURRENT_CLASS_DEPTH  */
49
50 typedef struct class_stack_node {
51   /* The name of the class.  */
52   tree name;
53
54   /* The _TYPE node for the class.  */
55   tree type;
56
57   /* The access specifier pending for new declarations in the scope of
58      this class.  */
59   tree access;
60
61   /* If were defining TYPE, the names used in this class.  */
62   splay_tree names_used;
63 }* class_stack_node_t;
64
65 typedef struct vtbl_init_data_s
66 {
67   /* The base for which we're building initializers.  */
68   tree binfo;
69   /* The type of the most-derived type.  */
70   tree derived;
71   /* The binfo for the dynamic type. This will be TYPE_BINFO (derived),
72      unless ctor_vtbl_p is true.  */
73   tree rtti_binfo;
74   /* The negative-index vtable initializers built up so far.  These
75      are in order from least negative index to most negative index.  */
76   tree inits;
77   /* The last (i.e., most negative) entry in INITS.  */
78   tree* last_init;
79   /* The binfo for the virtual base for which we're building
80      vcall offset initializers.  */
81   tree vbase;
82   /* The functions in vbase for which we have already provided vcall
83      offsets.  */
84   varray_type fns;
85   /* The vtable index of the next vcall or vbase offset.  */
86   tree index;
87   /* Nonzero if we are building the initializer for the primary
88      vtable.  */
89   int primary_vtbl_p;
90   /* Nonzero if we are building the initializer for a construction
91      vtable.  */
92   int ctor_vtbl_p;
93 } vtbl_init_data;
94
95 /* The type of a function passed to walk_subobject_offsets.  */
96 typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
97
98 /* The stack itself.  This is an dynamically resized array.  The
99    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
100 static int current_class_stack_size;
101 static class_stack_node_t current_class_stack;
102
103 /* An array of all local classes present in this translation unit, in
104    declaration order.  */
105 varray_type local_classes;
106
107 static tree get_vfield_name PARAMS ((tree));
108 static void finish_struct_anon PARAMS ((tree));
109 static tree build_vbase_pointer PARAMS ((tree, tree));
110 static tree build_vtable_entry PARAMS ((tree, tree, tree));
111 static tree get_vtable_name PARAMS ((tree));
112 static tree get_basefndecls PARAMS ((tree, tree));
113 static int build_primary_vtable PARAMS ((tree, tree));
114 static int build_secondary_vtable PARAMS ((tree, tree));
115 static void finish_vtbls PARAMS ((tree));
116 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
117 static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
118 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
119 static void delete_duplicate_fields PARAMS ((tree));
120 static void finish_struct_bits PARAMS ((tree));
121 static int alter_access PARAMS ((tree, tree, tree));
122 static void handle_using_decl PARAMS ((tree, tree));
123 static int strictly_overrides PARAMS ((tree, tree));
124 static void mark_overriders PARAMS ((tree, tree));
125 static void check_for_override PARAMS ((tree, tree));
126 static tree dfs_modify_vtables PARAMS ((tree, void *));
127 static tree modify_all_vtables PARAMS ((tree, int *, tree));
128 static void determine_primary_base PARAMS ((tree, int *));
129 static void finish_struct_methods PARAMS ((tree));
130 static void maybe_warn_about_overly_private_class PARAMS ((tree));
131 static int field_decl_cmp PARAMS ((const tree *, const tree *));
132 static int method_name_cmp PARAMS ((const tree *, const tree *));
133 static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
134 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
135 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
136                                                           int, int, tree));
137 static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
138 static tree build_vtbl_ref_1 PARAMS ((tree, tree));
139 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
140 static int count_fields PARAMS ((tree));
141 static int add_fields_to_vec PARAMS ((tree, tree, int));
142 static void check_bitfield_decl PARAMS ((tree));
143 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
144 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *, 
145                                      int *));
146 static bool build_base_field PARAMS ((record_layout_info, tree, int *,
147                                      splay_tree, tree));
148 static bool build_base_fields PARAMS ((record_layout_info, int *,
149                                       splay_tree, tree));
150 static void check_methods PARAMS ((tree));
151 static void remove_zero_width_bit_fields PARAMS ((tree));
152 static void check_bases PARAMS ((tree, int *, int *, int *));
153 static void check_bases_and_members PARAMS ((tree, int *));
154 static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
155 static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
156 static void fixup_pending_inline PARAMS ((tree));
157 static void fixup_inline_methods PARAMS ((tree));
158 static void set_primary_base PARAMS ((tree, tree, int *));
159 static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
160 static void layout_virtual_bases PARAMS ((tree, splay_tree));
161 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
162 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
163 static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
164 static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
165 static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
166 static void layout_vtable_decl PARAMS ((tree, int));
167 static tree dfs_find_final_overrider PARAMS ((tree, void *));
168 static tree find_final_overrider PARAMS ((tree, tree, tree));
169 static int make_new_vtable PARAMS ((tree, tree));
170 static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
171 static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
172 static void dump_class_hierarchy PARAMS ((tree));
173 static void dump_array PARAMS ((FILE *, tree));
174 static void dump_vtable PARAMS ((tree, tree, tree));
175 static void dump_vtt PARAMS ((tree, tree));
176 static tree build_vtable PARAMS ((tree, tree, tree));
177 static void initialize_vtable PARAMS ((tree, tree));
178 static void initialize_array PARAMS ((tree, tree));
179 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
180                                                    tree, tree,
181                                                    splay_tree, tree));
182 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
183 static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
184 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
185 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
186                                                tree));
187 static void set_vindex PARAMS ((tree, int *));
188 static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
189 static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree, 
190                                                         vtbl_init_data *));
191 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
192 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
193 static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
194 static void mark_primary_bases PARAMS ((tree));
195 static tree mark_primary_virtual_base PARAMS ((tree, tree));
196 static void clone_constructors_and_destructors PARAMS ((tree));
197 static tree build_clone PARAMS ((tree, tree));
198 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
199 static tree copy_virtuals PARAMS ((tree));
200 static void build_ctor_vtbl_group PARAMS ((tree, tree));
201 static void build_vtt PARAMS ((tree));
202 static tree binfo_ctor_vtable PARAMS ((tree));
203 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
204 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
205 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
206 static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
207 static tree get_original_base PARAMS ((tree, tree));
208 static tree dfs_get_primary_binfo PARAMS ((tree, void*));
209 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
210 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
211 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
212                                            tree, splay_tree, tree, int));
213 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
214 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
215 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
216                                                     splay_tree_key k2));
217 static void warn_about_ambiguous_direct_bases PARAMS ((tree));
218 static bool type_requires_array_cookie PARAMS ((tree));
219
220 /* Macros for dfs walking during vtt construction. See
221    dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
222    and dfs_fixup_binfo_vtbls.  */
223 #define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
224 #define VTT_MARKED_BINFO_P(node) TREE_USED(node)
225
226 /* Variables shared between class.c and call.c.  */
227
228 #ifdef GATHER_STATISTICS
229 int n_vtables = 0;
230 int n_vtable_entries = 0;
231 int n_vtable_searches = 0;
232 int n_vtable_elems = 0;
233 int n_convert_harshness = 0;
234 int n_compute_conversion_costs = 0;
235 int n_build_method_call = 0;
236 int n_inner_fields_searched = 0;
237 #endif
238
239 /* Virtual base class layout.  */
240
241 /* Returns a pointer to the virtual base class of EXP that has the
242    indicated TYPE.  EXP is of class type, not a pointer type.  */
243
244 static tree
245 build_vbase_pointer (exp, type)
246      tree exp, type;
247 {
248   tree vbase;
249   tree vbase_ptr;
250
251   /* Find the shared copy of TYPE; that's where the vtable offset is
252      recorded.  */
253   vbase = binfo_for_vbase (type, TREE_TYPE (exp));
254   /* Find the virtual function table pointer.  */
255   vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
256   /* Compute the location where the offset will lie.  */
257   vbase_ptr = build (PLUS_EXPR, 
258                      TREE_TYPE (vbase_ptr),
259                      vbase_ptr,
260                      BINFO_VPTR_FIELD (vbase));
261   vbase_ptr = build1 (NOP_EXPR, 
262                       build_pointer_type (ptrdiff_type_node),
263                       vbase_ptr);
264   /* Add the contents of this location to EXP.  */
265   return build (PLUS_EXPR,
266                 build_pointer_type (type),
267                 build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
268                 build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
269 }
270
271 /* Build multi-level access to EXPR using hierarchy path PATH.
272    CODE is PLUS_EXPR if we are going with the grain,
273    and MINUS_EXPR if we are not (in which case, we cannot traverse
274    virtual baseclass links).
275
276    TYPE is the type we want this path to have on exit.
277
278    NONNULL is non-zero if  we know (for any reason) that EXPR is
279    not, in fact, zero.  */
280
281 tree
282 build_vbase_path (code, type, expr, path, nonnull)
283      enum tree_code code;
284      tree type, expr, path;
285      int nonnull;
286 {
287   register int changed = 0;
288   tree last = NULL_TREE, last_virtual = NULL_TREE;
289   int fixed_type_p;
290   tree null_expr = 0, nonnull_expr;
291   tree basetype;
292   tree offset = integer_zero_node;
293
294   if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
295     return build1 (NOP_EXPR, type, expr);
296
297   /* We could do better if we had additional logic to convert back to the
298      unconverted type (the static type of the complete object), and then
299      convert back to the type we want.  Until that is done, we only optimize
300      if the complete type is the same type as expr has.  */
301   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
302   if (fixed_type_p < 0)
303     /* Virtual base layout is not fixed, even in ctors and dtors. */
304     fixed_type_p = 0;
305
306   if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
307     expr = save_expr (expr);
308   nonnull_expr = expr;
309
310   path = reverse_path (path);
311
312   basetype = BINFO_TYPE (path);
313
314   while (path)
315     {
316       if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
317         {
318           last_virtual = BINFO_TYPE (TREE_VALUE (path));
319           if (code == PLUS_EXPR)
320             {
321               changed = ! fixed_type_p;
322
323               if (changed)
324                 {
325                   tree ind;
326
327                   /* We already check for ambiguous things in the caller, just
328                      find a path.  */
329                   if (last)
330                     {
331                       tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
332                       nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
333                     }
334                   ind = build_indirect_ref (nonnull_expr, NULL);
335                   nonnull_expr = build_vbase_pointer (ind, last_virtual);
336                   if (nonnull == 0
337                       && TREE_CODE (type) == POINTER_TYPE
338                       && null_expr == NULL_TREE)
339                     {
340                       null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
341                       expr = build (COND_EXPR, build_pointer_type (last_virtual),
342                                     build (EQ_EXPR, boolean_type_node, expr,
343                                            integer_zero_node),
344                                     null_expr, nonnull_expr);
345                     }
346                 }
347               /* else we'll figure out the offset below.  */
348
349               /* Happens in the case of parse errors.  */
350               if (nonnull_expr == error_mark_node)
351                 return error_mark_node;
352             }
353           else
354             {
355               cp_error ("cannot cast up from virtual baseclass `%T'",
356                           last_virtual);
357               return error_mark_node;
358             }
359         }
360       last = TREE_VALUE (path);
361       path = TREE_CHAIN (path);
362     }
363   /* LAST is now the last basetype assoc on the path.  */
364
365   /* A pointer to a virtual base member of a non-null object
366      is non-null.  Therefore, we only need to test for zeroness once.
367      Make EXPR the canonical expression to deal with here.  */
368   if (null_expr)
369     {
370       TREE_OPERAND (expr, 2) = nonnull_expr;
371       TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
372         = TREE_TYPE (nonnull_expr);
373     }
374   else
375     expr = nonnull_expr;
376
377   /* If we go through any virtual base pointers, make sure that
378      casts to BASETYPE from the last virtual base class use
379      the right value for BASETYPE.  */
380   if (changed)
381     {
382       tree intype = TREE_TYPE (TREE_TYPE (expr));
383
384       if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
385         offset
386           = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
387     }
388   else
389     offset = BINFO_OFFSET (last);
390
391   if (! integer_zerop (offset))
392     {
393       /* Bash types to make the backend happy.  */
394       offset = cp_convert (type, offset);
395
396       /* If expr might be 0, we need to preserve that zeroness.  */
397       if (nonnull == 0)
398         {
399           if (null_expr)
400             TREE_TYPE (null_expr) = type;
401           else
402             null_expr = build1 (NOP_EXPR, type, integer_zero_node);
403           if (TREE_SIDE_EFFECTS (expr))
404             expr = save_expr (expr);
405
406           return build (COND_EXPR, type,
407                         build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
408                         null_expr,
409                         build (code, type, expr, offset));
410         }
411       else return build (code, type, expr, offset);
412     }
413
414   /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
415      be used multiple times in initialization of multiple inheritance.  */
416   if (null_expr)
417     {
418       TREE_TYPE (expr) = type;
419       return expr;
420     }
421   else
422     return build1 (NOP_EXPR, type, expr);
423 }
424
425 \f
426 /* Virtual function things.  */
427
428 static tree
429 build_vtable_entry_ref (array_ref, instance, idx)
430      tree array_ref, instance, idx;
431 {
432   tree i, i2, vtable, first_fn, basetype;
433
434   basetype = TREE_TYPE (instance);
435   if (TREE_CODE (basetype) == REFERENCE_TYPE)
436     basetype = TREE_TYPE (basetype);
437
438   vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
439   first_fn = TYPE_BINFO_VTABLE (basetype);
440
441   i = fold (build_array_ref (first_fn, idx));
442   i = fold (build_c_cast (ptrdiff_type_node,
443                           build_unary_op (ADDR_EXPR, i, 0)));
444   i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
445   i2 = fold (build_c_cast (ptrdiff_type_node,
446                            build_unary_op (ADDR_EXPR, i2, 0)));
447   i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
448
449   if (TREE_CODE (i) != INTEGER_CST)
450     abort ();
451
452   return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
453 }
454
455 /* Given an object INSTANCE, return an expression which yields the
456    vtable element corresponding to INDEX.  There are many special
457    cases for INSTANCE which we take care of here, mainly to avoid
458    creating extra tree nodes when we don't have to.  */
459
460 static tree
461 build_vtbl_ref_1 (instance, idx)
462      tree instance, idx;
463 {
464   tree vtbl, aref;
465   tree basetype = TREE_TYPE (instance);
466
467   if (TREE_CODE (basetype) == REFERENCE_TYPE)
468     basetype = TREE_TYPE (basetype);
469
470   if (instance == current_class_ref)
471     vtbl = build_vfield_ref (instance, basetype);
472   else
473     {
474       if (optimize)
475         {
476           /* Try to figure out what a reference refers to, and
477              access its virtual function table directly.  */
478           tree ref = NULL_TREE;
479
480           if (TREE_CODE (instance) == INDIRECT_REF
481               && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
482             ref = TREE_OPERAND (instance, 0);
483           else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
484             ref = instance;
485
486           if (ref && TREE_CODE (ref) == VAR_DECL
487               && DECL_INITIAL (ref))
488             {
489               tree init = DECL_INITIAL (ref);
490
491               while (TREE_CODE (init) == NOP_EXPR
492                      || TREE_CODE (init) == NON_LVALUE_EXPR)
493                 init = TREE_OPERAND (init, 0);
494               if (TREE_CODE (init) == ADDR_EXPR)
495                 {
496                   init = TREE_OPERAND (init, 0);
497                   if (IS_AGGR_TYPE (TREE_TYPE (init))
498                       && (TREE_CODE (init) == PARM_DECL
499                           || TREE_CODE (init) == VAR_DECL))
500                     instance = init;
501                 }
502             }
503         }
504
505       if (IS_AGGR_TYPE (TREE_TYPE (instance))
506           && (TREE_CODE (instance) == RESULT_DECL
507               || TREE_CODE (instance) == PARM_DECL
508               || TREE_CODE (instance) == VAR_DECL))
509         {
510           vtbl = TYPE_BINFO_VTABLE (basetype);
511           /* Knowing the dynamic type of INSTANCE we can easily obtain
512              the correct vtable entry.  We resolve this back to be in
513              terms of the primary vtable.  */
514           if (TREE_CODE (vtbl) == PLUS_EXPR)
515             {
516               idx = fold (build (PLUS_EXPR,
517                                  TREE_TYPE (idx),
518                                  idx,
519                                  build (EXACT_DIV_EXPR,
520                                         TREE_TYPE (idx),
521                                         TREE_OPERAND (vtbl, 1),
522                                         TYPE_SIZE_UNIT (vtable_entry_type))));
523               vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
524             }
525         }
526       else
527         vtbl = build_vfield_ref (instance, basetype);
528     }
529
530   assemble_external (vtbl);
531
532   aref = build_array_ref (vtbl, idx);
533
534   return aref;
535 }
536
537 tree
538 build_vtbl_ref (instance, idx)
539      tree instance, idx;
540 {
541   tree aref = build_vtbl_ref_1 (instance, idx);
542
543   if (flag_vtable_gc)
544     aref = build_vtable_entry_ref (aref, instance, idx);
545
546   return aref;
547 }
548
549 /* Given an object INSTANCE, return an expression which yields a
550    function pointer corresponding to vtable element INDEX.  */
551
552 tree
553 build_vfn_ref (instance, idx)
554      tree instance, idx;
555 {
556   tree aref = build_vtbl_ref_1 (instance, idx);
557
558   /* When using function descriptors, the address of the
559      vtable entry is treated as a function pointer.  */
560   if (TARGET_VTABLE_USES_DESCRIPTORS)
561     aref = build1 (NOP_EXPR, TREE_TYPE (aref),
562                    build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
563
564   if (flag_vtable_gc)
565     aref = build_vtable_entry_ref (aref, instance, idx);
566
567   return aref;
568 }
569
570 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
571    for the given TYPE.  */
572
573 static tree
574 get_vtable_name (type)
575      tree type;
576 {
577   return mangle_vtbl_for_type (type);
578 }
579
580 /* Return an IDENTIFIER_NODE for the name of the virtual table table
581    for TYPE.  */
582
583 tree
584 get_vtt_name (type)
585      tree type;
586 {
587   return mangle_vtt_for_type (type);
588 }
589
590 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
591    (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
592    Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
593
594 static tree
595 build_vtable (class_type, name, vtable_type)
596      tree class_type;
597      tree name;
598      tree vtable_type;
599 {
600   tree decl;
601
602   decl = build_lang_decl (VAR_DECL, name, vtable_type);
603   /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
604      now to avoid confusion in mangle_decl.  */
605   SET_DECL_ASSEMBLER_NAME (decl, name);
606   DECL_CONTEXT (decl) = class_type;
607   DECL_ARTIFICIAL (decl) = 1;
608   TREE_STATIC (decl) = 1;
609   TREE_READONLY (decl) = 1;
610   DECL_VIRTUAL_P (decl) = 1;
611   import_export_vtable (decl, class_type, 0);
612
613   return decl;
614 }
615
616 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
617    or even complete.  If this does not exist, create it.  If COMPLETE is
618    non-zero, then complete the definition of it -- that will render it
619    impossible to actually build the vtable, but is useful to get at those
620    which are known to exist in the runtime.  */
621
622 tree 
623 get_vtable_decl (type, complete)
624      tree type;
625      int complete;
626 {
627   tree name = get_vtable_name (type);
628   tree decl = IDENTIFIER_GLOBAL_VALUE (name);
629   
630   if (decl)
631     {
632       my_friendly_assert (TREE_CODE (decl) == VAR_DECL
633                           && DECL_VIRTUAL_P (decl), 20000118);
634       return decl;
635     }
636   
637   decl = build_vtable (type, name, void_type_node);
638   decl = pushdecl_top_level (decl);
639   my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
640                       20000517);
641   
642   /* At one time the vtable info was grabbed 2 words at a time.  This
643      fails on sparc unless you have 8-byte alignment.  (tiemann) */
644   DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
645                            DECL_ALIGN (decl));
646
647   if (complete)
648     {
649       DECL_EXTERNAL (decl) = 1;
650       cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
651     }
652
653   return decl;
654 }
655
656 /* Returns a copy of the BINFO_VIRTUALS list in BINFO.  The
657    BV_VCALL_INDEX for each entry is cleared.  */
658
659 static tree
660 copy_virtuals (binfo)
661      tree binfo;
662 {
663   tree copies;
664   tree t;
665
666   copies = copy_list (BINFO_VIRTUALS (binfo));
667   for (t = copies; t; t = TREE_CHAIN (t))
668     {
669       BV_VCALL_INDEX (t) = NULL_TREE;
670       BV_USE_VCALL_INDEX_P (t) = 0;
671     }
672
673   return copies;
674 }
675
676 /* Build the primary virtual function table for TYPE.  If BINFO is
677    non-NULL, build the vtable starting with the initial approximation
678    that it is the same as the one which is the head of the association
679    list.  Returns a non-zero value if a new vtable is actually
680    created.  */
681
682 static int
683 build_primary_vtable (binfo, type)
684      tree binfo, type;
685 {
686   tree decl;
687   tree virtuals;
688
689   decl = get_vtable_decl (type, /*complete=*/0);
690   
691   if (binfo)
692     {
693       if (BINFO_NEW_VTABLE_MARKED (binfo, type))
694         /* We have already created a vtable for this base, so there's
695            no need to do it again.  */
696         return 0;
697       
698       virtuals = copy_virtuals (binfo);
699       TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
700       DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
701       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
702     }
703   else
704     {
705       my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
706                           20000118);
707       virtuals = NULL_TREE;
708     }
709
710 #ifdef GATHER_STATISTICS
711   n_vtables += 1;
712   n_vtable_elems += list_length (virtuals);
713 #endif
714
715   /* Initialize the association list for this type, based
716      on our first approximation.  */
717   TYPE_BINFO_VTABLE (type) = decl;
718   TYPE_BINFO_VIRTUALS (type) = virtuals;
719   SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
720   return 1;
721 }
722
723 /* Give BINFO a new virtual function table which is initialized
724    with a skeleton-copy of its original initialization.  The only
725    entry that changes is the `delta' entry, so we can really
726    share a lot of structure.
727
728    FOR_TYPE is the most derived type which caused this table to
729    be needed.
730
731    Returns non-zero if we haven't met BINFO before.
732
733    The order in which vtables are built (by calling this function) for
734    an object must remain the same, otherwise a binary incompatibility
735    can result.  */
736
737 static int
738 build_secondary_vtable (binfo, for_type)
739      tree binfo, for_type;
740 {
741   my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
742
743   if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
744     /* We already created a vtable for this base.  There's no need to
745        do it again.  */
746     return 0;
747
748   /* Remember that we've created a vtable for this BINFO, so that we
749      don't try to do so again.  */
750   SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
751   
752   /* Make fresh virtual list, so we can smash it later.  */
753   BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
754
755   /* Secondary vtables are laid out as part of the same structure as
756      the primary vtable.  */
757   BINFO_VTABLE (binfo) = NULL_TREE;
758   return 1;
759 }
760
761 /* Create a new vtable for BINFO which is the hierarchy dominated by
762    T. Return non-zero if we actually created a new vtable.  */
763
764 static int
765 make_new_vtable (t, binfo)
766      tree t;
767      tree binfo;
768 {
769   if (binfo == TYPE_BINFO (t))
770     /* In this case, it is *type*'s vtable we are modifying.  We start
771        with the approximation that its vtable is that of the
772        immediate base class.  */
773     /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
774        since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now.  */
775     return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
776                                  t);
777   else
778     /* This is our very own copy of `basetype' to play with.  Later,
779        we will fill in all the virtual functions that override the
780        virtual functions in these base classes which are not defined
781        by the current type.  */
782     return build_secondary_vtable (binfo, t);
783 }
784
785 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
786    (which is in the hierarchy dominated by T) list FNDECL as its
787    BV_FN.  DELTA is the required constant adjustment from the `this'
788    pointer where the vtable entry appears to the `this' required when
789    the function is actually called.  */
790
791 static void
792 modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
793      tree t;
794      tree binfo;
795      tree fndecl;
796      tree delta;
797      tree *virtuals;
798 {
799   tree v;
800
801   v = *virtuals;
802
803   if (fndecl != BV_FN (v)
804       || !tree_int_cst_equal (delta, BV_DELTA (v)))
805     {
806       tree base_fndecl;
807
808       /* We need a new vtable for BINFO.  */
809       if (make_new_vtable (t, binfo))
810         {
811           /* If we really did make a new vtable, we also made a copy
812              of the BINFO_VIRTUALS list.  Now, we have to find the
813              corresponding entry in that list.  */
814           *virtuals = BINFO_VIRTUALS (binfo);
815           while (BV_FN (*virtuals) != BV_FN (v))
816             *virtuals = TREE_CHAIN (*virtuals);
817           v = *virtuals;
818         }
819
820       base_fndecl = BV_FN (v);
821       BV_DELTA (v) = delta;
822       BV_VCALL_INDEX (v) = NULL_TREE;
823       BV_FN (v) = fndecl;
824
825       /* Now assign virtual dispatch information, if unset.  We can
826          dispatch this through any overridden base function.
827
828          FIXME this can choose a secondary vtable if the primary is not
829          also lexically first, leading to useless conversions.
830          In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
831          ever be different from DECL_CONTEXT.  */
832       if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
833         {
834           DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
835           DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
836         }
837     }
838 }
839
840 /* Set DECL_VINDEX for DECL.  VINDEX_P is the number of virtual
841    functions present in the vtable so far.  */
842
843 static void
844 set_vindex (decl, vfuns_p)
845      tree decl;
846      int *vfuns_p;
847 {
848   int vindex;
849
850   vindex = *vfuns_p;
851   *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
852                ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
853   DECL_VINDEX (decl) = build_shared_int_cst (vindex);
854 }
855
856 /* Add a virtual function to all the appropriate vtables for the class
857    T.  DECL_VINDEX(X) should be error_mark_node, if we want to
858    allocate a new slot in our table.  If it is error_mark_node, we
859    know that no other function from another vtable is overridden by X.
860    VFUNS_P keeps track of how many virtuals there are in our
861    main vtable for the type, and we build upon the NEW_VIRTUALS list
862    and return it.  */
863
864 static void
865 add_virtual_function (new_virtuals_p, overridden_virtuals_p,
866                       vfuns_p, fndecl, t)
867      tree *new_virtuals_p;
868      tree *overridden_virtuals_p;
869      int *vfuns_p;
870      tree fndecl;
871      tree t; /* Structure type.  */
872 {
873   tree new_virtual;
874
875   /* If this function doesn't override anything from a base class, we
876      can just assign it a new DECL_VINDEX now.  Otherwise, if it does
877      override something, we keep it around and assign its DECL_VINDEX
878      later, in modify_all_vtables.  */
879   if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
880     /* We've already dealt with this function.  */
881     return;
882
883   new_virtual = make_node (TREE_LIST);
884   BV_FN (new_virtual) = fndecl;
885   BV_DELTA (new_virtual) = integer_zero_node;
886
887   if (DECL_VINDEX (fndecl) == error_mark_node)
888     {
889       /* FNDECL is a new virtual function; it doesn't override any
890          virtual function in a base class.  */
891
892       /* We remember that this was the base sub-object for rtti.  */
893       CLASSTYPE_RTTI (t) = t;
894
895       /* Now assign virtual dispatch information.  */
896       set_vindex (fndecl, vfuns_p);
897       DECL_VIRTUAL_CONTEXT (fndecl) = t;
898
899       /* Save the state we've computed on the NEW_VIRTUALS list.  */
900       TREE_CHAIN (new_virtual) = *new_virtuals_p;
901       *new_virtuals_p = new_virtual;
902     }
903   else
904     {
905       /* FNDECL overrides a function from a base class.  */
906       TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
907       *overridden_virtuals_p = new_virtual;
908     }
909 }
910 \f
911 /* Add method METHOD to class TYPE.  If ERROR_P is true, we are adding
912    the method after the class has already been defined because a
913    declaration for it was seen.  (Even though that is erroneous, we
914    add the method for improved error recovery.)  */
915
916 void
917 add_method (type, method, error_p)
918      tree type;
919      tree method;
920      int error_p;
921 {
922   int using = (DECL_CONTEXT (method) != type);
923   int len;
924   int slot;
925   tree method_vec;
926
927   if (!CLASSTYPE_METHOD_VEC (type))
928     /* Make a new method vector.  We start with 8 entries.  We must
929        allocate at least two (for constructors and destructors), and
930        we're going to end up with an assignment operator at some point
931        as well.
932        
933        We could use a TREE_LIST for now, and convert it to a TREE_VEC
934        in finish_struct, but we would probably waste more memory
935        making the links in the list than we would by over-allocating
936        the size of the vector here.  Furthermore, we would complicate
937        all the code that expects this to be a vector.  */
938     CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
939
940   method_vec = CLASSTYPE_METHOD_VEC (type);
941   len = TREE_VEC_LENGTH (method_vec);
942
943   /* Constructors and destructors go in special slots.  */
944   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
945     slot = CLASSTYPE_CONSTRUCTOR_SLOT;
946   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
947     slot = CLASSTYPE_DESTRUCTOR_SLOT;
948   else
949     {
950       /* See if we already have an entry with this name.  */
951       for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
952         if (!TREE_VEC_ELT (method_vec, slot)
953             || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, 
954                                                       slot))) 
955                 == DECL_NAME (method)))
956           break;
957                 
958       if (slot == len)
959         {
960           /* We need a bigger method vector.  */
961           int new_len;
962           tree new_vec;
963
964           /* In the non-error case, we are processing a class
965              definition.  Double the size of the vector to give room
966              for new methods.  */
967           if (!error_p)
968             new_len = 2 * len;
969           /* In the error case, the vector is already complete.  We
970              don't expect many errors, and the rest of the front-end
971              will get confused if there are empty slots in the vector.  */
972           else
973             new_len = len + 1;
974
975           new_vec = make_tree_vec (new_len);
976           memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
977                   len * sizeof (tree));
978           len = new_len;
979           method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
980         }
981
982       if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
983         {
984           /* Type conversion operators have to come before ordinary
985              methods; add_conversions depends on this to speed up
986              looking for conversion operators.  So, if necessary, we
987              slide some of the vector elements up.  In theory, this
988              makes this algorithm O(N^2) but we don't expect many
989              conversion operators.  */
990           for (slot = 2; slot < len; ++slot)
991             {
992               tree fn = TREE_VEC_ELT (method_vec, slot);
993   
994               if (!fn)
995                 /* There are no more entries in the vector, so we
996                    can insert the new conversion operator here.  */
997                 break;
998                   
999               if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
1000                 /* We can insert the new function right at the
1001                    SLOTth position.  */
1002                 break;
1003             }
1004   
1005           if (!TREE_VEC_ELT (method_vec, slot))
1006             /* There is nothing in the Ith slot, so we can avoid
1007                moving anything.  */
1008                 ; 
1009           else
1010             {
1011               /* We know the last slot in the vector is empty
1012                  because we know that at this point there's room
1013                  for a new function.  */
1014               memmove (&TREE_VEC_ELT (method_vec, slot + 1),
1015                        &TREE_VEC_ELT (method_vec, slot),
1016                        (len - slot - 1) * sizeof (tree));
1017               TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
1018             }
1019         }
1020     }
1021       
1022   if (template_class_depth (type))
1023     /* TYPE is a template class.  Don't issue any errors now; wait
1024        until instantiation time to complain.  */
1025     ;
1026   else
1027     {
1028       tree fns;
1029
1030       /* Check to see if we've already got this method.  */
1031       for (fns = TREE_VEC_ELT (method_vec, slot);
1032            fns;
1033            fns = OVL_NEXT (fns))
1034         {
1035           tree fn = OVL_CURRENT (fns);
1036                  
1037           if (TREE_CODE (fn) != TREE_CODE (method))
1038             continue;
1039
1040           if (TREE_CODE (method) != TEMPLATE_DECL)
1041             {
1042               /* [over.load] Member function declarations with the
1043                  same name and the same parameter types cannot be
1044                  overloaded if any of them is a static member
1045                  function declaration.  */
1046               if ((DECL_STATIC_FUNCTION_P (fn)
1047                    != DECL_STATIC_FUNCTION_P (method))
1048                   || using)
1049                 {
1050                   tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
1051                   tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
1052
1053                   if (! DECL_STATIC_FUNCTION_P (fn))
1054                     parms1 = TREE_CHAIN (parms1);
1055                   if (! DECL_STATIC_FUNCTION_P (method))
1056                     parms2 = TREE_CHAIN (parms2);
1057
1058                   if (compparms (parms1, parms2))
1059                     {
1060                       if (using)
1061                         /* Defer to the local function.  */
1062                         return;
1063                       else
1064                         cp_error ("`%#D' and `%#D' cannot be overloaded",
1065                                   fn, method);
1066                     }
1067                 }
1068             }
1069
1070           if (!decls_match (fn, method))
1071             continue;
1072
1073           /* There has already been a declaration of this method
1074              or member template.  */
1075           cp_error_at ("`%D' has already been declared in `%T'", 
1076                        method, type);
1077
1078           /* We don't call duplicate_decls here to merge the
1079              declarations because that will confuse things if the
1080              methods have inline definitions.  In particular, we
1081              will crash while processing the definitions.  */
1082           return;
1083         }
1084     }
1085
1086   /* Actually insert the new method.  */
1087   TREE_VEC_ELT (method_vec, slot) 
1088     = build_overload (method, TREE_VEC_ELT (method_vec, slot));
1089
1090       /* Add the new binding.  */ 
1091   if (!DECL_CONSTRUCTOR_P (method)
1092       && !DECL_DESTRUCTOR_P (method))
1093     push_class_level_binding (DECL_NAME (method),
1094                               TREE_VEC_ELT (method_vec, slot));
1095 }
1096
1097 /* Subroutines of finish_struct.  */
1098
1099 /* Look through the list of fields for this struct, deleting
1100    duplicates as we go.  This must be recursive to handle
1101    anonymous unions.
1102
1103    FIELD is the field which may not appear anywhere in FIELDS.
1104    FIELD_PTR, if non-null, is the starting point at which
1105    chained deletions may take place.
1106    The value returned is the first acceptable entry found
1107    in FIELDS.
1108
1109    Note that anonymous fields which are not of UNION_TYPE are
1110    not duplicates, they are just anonymous fields.  This happens
1111    when we have unnamed bitfields, for example.  */
1112
1113 static tree
1114 delete_duplicate_fields_1 (field, fields)
1115      tree field, fields;
1116 {
1117   tree x;
1118   tree prev = 0;
1119   if (DECL_NAME (field) == 0)
1120     {
1121       if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
1122         return fields;
1123
1124       for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
1125         fields = delete_duplicate_fields_1 (x, fields);
1126       return fields;
1127     }
1128   else
1129     {
1130       for (x = fields; x; prev = x, x = TREE_CHAIN (x))
1131         {
1132           if (DECL_NAME (x) == 0)
1133             {
1134               if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
1135                 continue;
1136               TYPE_FIELDS (TREE_TYPE (x))
1137                 = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
1138               if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
1139                 {
1140                   if (prev == 0)
1141                     fields = TREE_CHAIN (fields);
1142                   else
1143                     TREE_CHAIN (prev) = TREE_CHAIN (x);
1144                 }
1145             }
1146           else if (TREE_CODE (field) == USING_DECL)
1147             /* A using declaration may is allowed to appear more than
1148                once.  We'll prune these from the field list later, and
1149                handle_using_decl will complain about invalid multiple
1150                uses.  */
1151             ;
1152           else if (DECL_NAME (field) == DECL_NAME (x))
1153             {
1154               if (TREE_CODE (field) == CONST_DECL
1155                   && TREE_CODE (x) == CONST_DECL)
1156                 cp_error_at ("duplicate enum value `%D'", x);
1157               else if (TREE_CODE (field) == CONST_DECL
1158                        || TREE_CODE (x) == CONST_DECL)
1159                 cp_error_at ("duplicate field `%D' (as enum and non-enum)",
1160                              x);
1161               else if (DECL_DECLARES_TYPE_P (field)
1162                        && DECL_DECLARES_TYPE_P (x))
1163                 {
1164                   if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
1165                     continue;
1166                   cp_error_at ("duplicate nested type `%D'", x);
1167                 }
1168               else if (DECL_DECLARES_TYPE_P (field)
1169                        || DECL_DECLARES_TYPE_P (x))
1170                 {
1171                   /* Hide tag decls.  */
1172                   if ((TREE_CODE (field) == TYPE_DECL
1173                        && DECL_ARTIFICIAL (field))
1174                       || (TREE_CODE (x) == TYPE_DECL
1175                           && DECL_ARTIFICIAL (x)))
1176                     continue;
1177                   cp_error_at ("duplicate field `%D' (as type and non-type)",
1178                                x);
1179                 }
1180               else
1181                 cp_error_at ("duplicate member `%D'", x);
1182               if (prev == 0)
1183                 fields = TREE_CHAIN (fields);
1184               else
1185                 TREE_CHAIN (prev) = TREE_CHAIN (x);
1186             }
1187         }
1188     }
1189   return fields;
1190 }
1191
1192 static void
1193 delete_duplicate_fields (fields)
1194      tree fields;
1195 {
1196   tree x;
1197   for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
1198     TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
1199 }
1200
1201 /* Change the access of FDECL to ACCESS in T.  Return 1 if change was
1202    legit, otherwise return 0.  */
1203
1204 static int
1205 alter_access (t, fdecl, access)
1206      tree t;
1207      tree fdecl;
1208      tree access;
1209 {
1210   tree elem;
1211
1212   if (!DECL_LANG_SPECIFIC (fdecl))
1213     retrofit_lang_decl (fdecl);
1214
1215   if (DECL_DISCRIMINATOR_P (fdecl))
1216     abort ();
1217
1218   elem = purpose_member (t, DECL_ACCESS (fdecl));
1219   if (elem)
1220     {
1221       if (TREE_VALUE (elem) != access)
1222         {
1223           if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
1224             cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
1225           else
1226             error ("conflicting access specifications for field `%s', ignored",
1227                    IDENTIFIER_POINTER (DECL_NAME (fdecl)));
1228         }
1229       else
1230         {
1231           /* They're changing the access to the same thing they changed
1232              it to before.  That's OK.  */
1233           ;
1234         }
1235     }
1236   else
1237     {
1238       enforce_access (t, fdecl);
1239       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
1240       return 1;
1241     }
1242   return 0;
1243 }
1244
1245 /* Process the USING_DECL, which is a member of T.  */
1246
1247 static void
1248 handle_using_decl (using_decl, t)
1249      tree using_decl;
1250      tree t;
1251 {
1252   tree ctype = DECL_INITIAL (using_decl);
1253   tree name = DECL_NAME (using_decl);
1254   tree access
1255     = TREE_PRIVATE (using_decl) ? access_private_node
1256     : TREE_PROTECTED (using_decl) ? access_protected_node
1257     : access_public_node;
1258   tree fdecl, binfo;
1259   tree flist = NULL_TREE;
1260   tree old_value;
1261
1262   binfo = binfo_or_else (ctype, t);
1263   if (! binfo)
1264     return;
1265   
1266   if (name == constructor_name (ctype)
1267       || name == constructor_name_full (ctype))
1268     {
1269       cp_error_at ("`%D' names constructor", using_decl);
1270       return;
1271     }
1272   if (name == constructor_name (t)
1273       || name == constructor_name_full (t))
1274     {
1275       cp_error_at ("`%D' invalid in `%T'", using_decl, t);
1276       return;
1277     }
1278
1279   fdecl = lookup_member (binfo, name, 0, 0);
1280   
1281   if (!fdecl)
1282     {
1283       cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
1284       return;
1285     }
1286
1287   if (BASELINK_P (fdecl))
1288     /* Ignore base type this came from. */
1289     fdecl = TREE_VALUE (fdecl);
1290
1291   old_value = IDENTIFIER_CLASS_VALUE (name);
1292   if (old_value)
1293     {
1294       if (is_overloaded_fn (old_value))
1295         old_value = OVL_CURRENT (old_value);
1296
1297       if (DECL_P (old_value) && DECL_CONTEXT (old_value) == t)
1298         /* OK */;
1299       else
1300         old_value = NULL_TREE;
1301     }
1302
1303   if (is_overloaded_fn (fdecl))
1304     flist = fdecl;
1305
1306   if (! old_value)
1307     ;
1308   else if (is_overloaded_fn (old_value))
1309     {
1310       if (flist)
1311         /* It's OK to use functions from a base when there are functions with
1312            the same name already present in the current class.  */;
1313       else
1314         {
1315           cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1316           cp_error_at ("  because of local method `%#D' with same name",
1317                        OVL_CURRENT (old_value));
1318           return;
1319         }
1320     }
1321   else if (!DECL_ARTIFICIAL (old_value))
1322     {
1323       cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
1324       cp_error_at ("  because of local member `%#D' with same name", old_value);
1325       return;
1326     }
1327   
1328   /* Make type T see field decl FDECL with access ACCESS.*/
1329   if (flist)
1330     for (; flist; flist = OVL_NEXT (flist))
1331       {
1332         add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
1333         alter_access (t, OVL_CURRENT (flist), access);
1334       }
1335   else
1336     alter_access (t, fdecl, access);
1337 }
1338 \f
1339 /* Run through the base clases of T, updating
1340    CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
1341    NO_CONST_ASN_REF_P.  Also set flag bits in T based on properties of
1342    the bases.  */
1343
1344 static void
1345 check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
1346              no_const_asn_ref_p)
1347      tree t;
1348      int *cant_have_default_ctor_p;
1349      int *cant_have_const_ctor_p;
1350      int *no_const_asn_ref_p;
1351 {
1352   int n_baseclasses;
1353   int i;
1354   int seen_non_virtual_nearly_empty_base_p;
1355   tree binfos;
1356
1357   binfos = TYPE_BINFO_BASETYPES (t);
1358   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1359   seen_non_virtual_nearly_empty_base_p = 0;
1360
1361   /* An aggregate cannot have baseclasses.  */
1362   CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
1363
1364   for (i = 0; i < n_baseclasses; ++i) 
1365     {
1366       tree base_binfo;
1367       tree basetype;
1368
1369       /* Figure out what base we're looking at.  */
1370       base_binfo = TREE_VEC_ELT (binfos, i);
1371       basetype = TREE_TYPE (base_binfo);
1372
1373       /* If the type of basetype is incomplete, then we already
1374          complained about that fact (and we should have fixed it up as
1375          well).  */
1376       if (!COMPLETE_TYPE_P (basetype))
1377         {
1378           int j;
1379           /* The base type is of incomplete type.  It is
1380              probably best to pretend that it does not
1381              exist.  */
1382           if (i == n_baseclasses-1)
1383             TREE_VEC_ELT (binfos, i) = NULL_TREE;
1384           TREE_VEC_LENGTH (binfos) -= 1;
1385           n_baseclasses -= 1;
1386           for (j = i; j+1 < n_baseclasses; j++)
1387             TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
1388           continue;
1389         }
1390
1391       /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
1392          here because the case of virtual functions but non-virtual
1393          dtor is handled in finish_struct_1.  */
1394       if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
1395           && TYPE_HAS_DESTRUCTOR (basetype))
1396         cp_warning ("base class `%#T' has a non-virtual destructor",
1397                     basetype);
1398
1399       /* If the base class doesn't have copy constructors or
1400          assignment operators that take const references, then the
1401          derived class cannot have such a member automatically
1402          generated.  */
1403       if (! TYPE_HAS_CONST_INIT_REF (basetype))
1404         *cant_have_const_ctor_p = 1;
1405       if (TYPE_HAS_ASSIGN_REF (basetype)
1406           && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
1407         *no_const_asn_ref_p = 1;
1408       /* Similarly, if the base class doesn't have a default
1409          constructor, then the derived class won't have an
1410          automatically generated default constructor.  */
1411       if (TYPE_HAS_CONSTRUCTOR (basetype)
1412           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
1413         {
1414           *cant_have_default_ctor_p = 1;
1415           if (! TYPE_HAS_CONSTRUCTOR (t))
1416             cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
1417                         basetype);
1418         }
1419
1420       if (TREE_VIA_VIRTUAL (base_binfo))
1421         /* A virtual base does not effect nearly emptiness. */
1422         ;
1423       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
1424         {
1425           if (seen_non_virtual_nearly_empty_base_p)
1426             /* And if there is more than one nearly empty base, then the
1427                derived class is not nearly empty either.  */
1428             CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1429           else
1430             /* Remember we've seen one. */
1431             seen_non_virtual_nearly_empty_base_p = 1;
1432         }
1433       else if (!is_empty_class (basetype))
1434         /* If the base class is not empty or nearly empty, then this
1435            class cannot be nearly empty.  */
1436         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
1437
1438       /* A lot of properties from the bases also apply to the derived
1439          class.  */
1440       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
1441       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
1442         |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
1443       TYPE_HAS_COMPLEX_ASSIGN_REF (t) 
1444         |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
1445       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
1446       TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
1447       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
1448       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
1449       TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
1450     }
1451 }
1452
1453 /* Binfo FROM is within a virtual heirarchy which is being reseated to
1454    TO. Move primary information from FROM to TO, and recursively traverse
1455    into FROM's bases. The heirarchy is dominated by TYPE.  MAPPINGS is an
1456    assoc list of binfos that have already been reseated.  */
1457
1458 static void
1459 force_canonical_binfo_r (to, from, type, mappings)
1460      tree to;
1461      tree from;
1462      tree type;
1463      tree mappings;
1464 {
1465   int i, n_baseclasses = BINFO_N_BASETYPES (from);
1466
1467   my_friendly_assert (to != from, 20010905);
1468   BINFO_INDIRECT_PRIMARY_P (to)
1469           = BINFO_INDIRECT_PRIMARY_P (from);
1470   BINFO_INDIRECT_PRIMARY_P (from) = 0;
1471   BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
1472   BINFO_UNSHARED_MARKED (from) = 0;
1473   BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
1474   BINFO_LOST_PRIMARY_P (from) = 0;
1475   if (BINFO_PRIMARY_P (from))
1476     {
1477       tree primary = BINFO_PRIMARY_BASE_OF (from);
1478       tree assoc;
1479       
1480       /* We might have just moved the primary base too, see if it's on our
1481          mappings.  */
1482       assoc = purpose_member (primary, mappings);
1483       if (assoc)
1484         primary = TREE_VALUE (assoc);
1485       BINFO_PRIMARY_BASE_OF (to) = primary;
1486       BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
1487     }
1488   my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
1489                       20010104);
1490   mappings = tree_cons (from, to, mappings);
1491
1492   if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
1493       && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
1494     {
1495       tree from_primary = get_primary_binfo (from);
1496       
1497       if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
1498         force_canonical_binfo (get_primary_binfo (to), from_primary,
1499                                type, mappings);
1500     }
1501   
1502   for (i = 0; i != n_baseclasses; i++)
1503     {
1504       tree from_binfo = BINFO_BASETYPE (from, i);
1505       tree to_binfo = BINFO_BASETYPE (to, i);
1506
1507       if (TREE_VIA_VIRTUAL (from_binfo))
1508         {
1509           if (BINFO_PRIMARY_P (from_binfo) &&
1510               purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
1511             /* This base is a primary of some binfo we have already
1512                reseated. We must reseat this one too.  */
1513             force_canonical_binfo (to_binfo, from_binfo, type, mappings);
1514         }
1515       else
1516         force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
1517     }
1518 }
1519
1520 /* FROM is the canonical binfo for a virtual base. It is being reseated to
1521    make TO the canonical binfo, within the heirarchy dominated by TYPE.
1522    MAPPINGS is an assoc list of binfos that have already been reseated.
1523    Adjust any non-virtual bases within FROM, and also move any virtual bases
1524    which are canonical.  This complication arises because selecting primary
1525    bases walks in inheritance graph order, but we don't share binfos for
1526    virtual bases, hence we can fill in the primaries for a virtual base,
1527    and then discover that a later base requires the virtual as its
1528    primary.  */
1529
1530 static void
1531 force_canonical_binfo (to, from, type, mappings)
1532      tree to;
1533      tree from;
1534      tree type;
1535      tree mappings;
1536 {
1537   tree assoc = purpose_member (BINFO_TYPE (to),
1538                                CLASSTYPE_VBASECLASSES (type));
1539   if (TREE_VALUE (assoc) != to)
1540     {
1541       TREE_VALUE (assoc) = to;
1542       force_canonical_binfo_r (to, from, type, mappings);
1543     }
1544 }
1545
1546 /* Make BASE_BINFO the a primary virtual base within the hierarchy
1547    dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
1548    otherwise (because something else has already made it primary).  */
1549
1550 static tree
1551 mark_primary_virtual_base (base_binfo, type)
1552      tree base_binfo;
1553      tree type;
1554 {
1555   tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
1556
1557   if (BINFO_PRIMARY_P (shared_binfo))
1558     {
1559       /* It's already allocated in the hierarchy. BINFO won't have a
1560          primary base in this hierachy, even though the complete object
1561          BINFO is for, would do.  */
1562       return NULL_TREE;
1563     }
1564      
1565   /* We need to make sure that the assoc list
1566      CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
1567      primary BINFO for the virtual base, as this is the one
1568      that'll really exist.  */
1569   if (base_binfo != shared_binfo)
1570     force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
1571
1572   return base_binfo;
1573 }
1574
1575 /* If BINFO is an unmarked virtual binfo for a class with a primary virtual
1576    base, then BINFO has no primary base in this graph.  Called from
1577    mark_primary_bases.  DATA is the most derived type. */
1578
1579 static tree dfs_unshared_virtual_bases (binfo, data)
1580      tree binfo;
1581      void *data;
1582 {
1583   tree t = (tree) data;
1584   
1585   if (!BINFO_UNSHARED_MARKED (binfo)
1586       && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1587     {
1588       /* This morally virtual base has a primary base when it
1589          is a complete object. We need to locate the shared instance
1590          of this binfo in the type dominated by T. We duplicate the
1591          primary base information from there to here.  */
1592       tree vbase;
1593       tree unshared_base;
1594       
1595       for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
1596            vbase = BINFO_INHERITANCE_CHAIN (vbase))
1597         continue;
1598       unshared_base = get_original_base (binfo,
1599                                          binfo_for_vbase (BINFO_TYPE (vbase),
1600                                                           t));
1601       my_friendly_assert (unshared_base != binfo, 20010612);
1602       BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
1603       if (!BINFO_LOST_PRIMARY_P (binfo))
1604         BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
1605     }
1606   
1607   if (binfo != TYPE_BINFO (t))
1608     /* The vtable fields will have been copied when duplicating the
1609        base binfos. That information is bogus, make sure we don't try
1610        and use it. */
1611     BINFO_VTABLE (binfo) = NULL_TREE;
1612
1613   /* If this is a virtual primary base, make sure its offset matches
1614      that which it is primary for. */
1615   if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
1616       binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
1617     {
1618       tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
1619                                 BINFO_OFFSET (binfo));
1620       if (!integer_zerop (delta))
1621         propagate_binfo_offsets (binfo, delta, t);
1622     }
1623   
1624   BINFO_UNSHARED_MARKED (binfo) = 0;
1625   return NULL;
1626 }
1627
1628 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
1629    dominated by TYPE that are primary bases.  */
1630
1631 static void
1632 mark_primary_bases (type)
1633      tree type;
1634 {
1635   tree binfo;
1636   
1637   /* Walk the bases in inheritance graph order.  */
1638   for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
1639     {
1640       tree base_binfo;
1641       
1642       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
1643         /* Not a dynamic base. */
1644         continue;
1645
1646       base_binfo = get_primary_binfo (binfo);
1647
1648       if (TREE_VIA_VIRTUAL (base_binfo))
1649         base_binfo = mark_primary_virtual_base (base_binfo, type);
1650
1651       if (base_binfo)
1652         BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
1653       else
1654         BINFO_LOST_PRIMARY_P (binfo) = 1;
1655       
1656       BINFO_UNSHARED_MARKED (binfo) = 1;
1657     }
1658   /* There could remain unshared morally virtual bases which were not
1659      visited in the inheritance graph walk. These bases will have lost
1660      their virtual primary base (should they have one). We must now
1661      find them. Also we must fix up the BINFO_OFFSETs of primary
1662      virtual bases. We could not do that as we went along, as they
1663      were originally copied from the bases we inherited from by
1664      unshare_base_binfos. That may have decided differently about
1665      where a virtual primary base went.  */
1666   dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
1667 }
1668
1669 /* Make the BINFO the primary base of T.  */
1670
1671 static void
1672 set_primary_base (t, binfo, vfuns_p)
1673      tree t;
1674      tree binfo;
1675      int *vfuns_p;
1676 {
1677   tree basetype;
1678
1679   CLASSTYPE_PRIMARY_BINFO (t) = binfo;
1680   basetype = BINFO_TYPE (binfo);
1681   TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
1682   TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
1683   TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
1684   CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1685   *vfuns_p = CLASSTYPE_VSIZE (basetype);
1686 }
1687
1688 /* Determine the primary class for T.  */
1689
1690 static void
1691 determine_primary_base (t, vfuns_p)
1692      tree t;
1693      int *vfuns_p;
1694 {
1695   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1696   tree vbases;
1697   tree type_binfo;
1698
1699   /* If there are no baseclasses, there is certainly no primary base.  */
1700   if (n_baseclasses == 0)
1701     return;
1702
1703   type_binfo = TYPE_BINFO (t);
1704
1705   for (i = 0; i < n_baseclasses; i++)
1706     {
1707       tree base_binfo = BINFO_BASETYPE (type_binfo, i);
1708       tree basetype = BINFO_TYPE (base_binfo);
1709
1710       if (TYPE_CONTAINS_VPTR_P (basetype))
1711         {
1712           /* Even a virtual baseclass can contain our RTTI
1713              information.  But, we prefer a non-virtual polymorphic
1714              baseclass.  */
1715           if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1716             CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
1717
1718           /* We prefer a non-virtual base, although a virtual one will
1719              do.  */
1720           if (TREE_VIA_VIRTUAL (base_binfo))
1721             continue;
1722
1723           if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1724             {
1725               set_primary_base (t, base_binfo, vfuns_p);
1726               CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
1727             }
1728           else
1729             {
1730               tree vfields;
1731
1732               /* Only add unique vfields, and flatten them out as we go.  */
1733               for (vfields = CLASSTYPE_VFIELDS (basetype);
1734                    vfields;
1735                    vfields = TREE_CHAIN (vfields))
1736                 if (VF_BINFO_VALUE (vfields) == NULL_TREE
1737                     || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
1738                   CLASSTYPE_VFIELDS (t) 
1739                     = tree_cons (base_binfo, 
1740                                  VF_BASETYPE_VALUE (vfields),
1741                                  CLASSTYPE_VFIELDS (t));
1742             }
1743         }
1744     }
1745
1746   if (!TYPE_VFIELD (t))
1747     CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
1748
1749   /* Find the indirect primary bases - those virtual bases which are primary
1750      bases of something else in this hierarchy.  */
1751   for (vbases = CLASSTYPE_VBASECLASSES (t);
1752        vbases;
1753        vbases = TREE_CHAIN (vbases)) 
1754     {
1755       tree vbase_binfo = TREE_VALUE (vbases);
1756
1757       /* See if this virtual base is an indirect primary base.  To be so,
1758          it must be a primary base within the hierarchy of one of our
1759          direct bases.  */
1760       for (i = 0; i < n_baseclasses; ++i) 
1761         {
1762           tree basetype = TYPE_BINFO_BASETYPE (t, i);
1763           tree v;
1764
1765           for (v = CLASSTYPE_VBASECLASSES (basetype); 
1766                v; 
1767                v = TREE_CHAIN (v))
1768             {
1769               tree base_vbase = TREE_VALUE (v);
1770               
1771               if (BINFO_PRIMARY_P (base_vbase)
1772                   && same_type_p (BINFO_TYPE (base_vbase),
1773                                   BINFO_TYPE (vbase_binfo)))
1774                 {
1775                   BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
1776                   break;
1777                 }
1778             }
1779
1780           /* If we've discovered that this virtual base is an indirect
1781              primary base, then we can move on to the next virtual
1782              base.  */
1783           if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
1784             break;
1785         }
1786     }
1787
1788   /* A "nearly-empty" virtual base class can be the primary base
1789      class, if no non-virtual polymorphic base can be found.  */
1790   if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
1791     {
1792       /* If not NULL, this is the best primary base candidate we have
1793          found so far.  */
1794       tree candidate = NULL_TREE;
1795       tree base_binfo;
1796
1797       /* Loop over the baseclasses.  */
1798       for (base_binfo = TYPE_BINFO (t);
1799            base_binfo;
1800            base_binfo = TREE_CHAIN (base_binfo))
1801         {
1802           tree basetype = BINFO_TYPE (base_binfo);
1803
1804           if (TREE_VIA_VIRTUAL (base_binfo) 
1805               && CLASSTYPE_NEARLY_EMPTY_P (basetype))
1806             {
1807               /* If this is not an indirect primary base, then it's
1808                  definitely our primary base.  */
1809               if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
1810                 {
1811                   candidate = base_binfo;
1812                   break;
1813                 }
1814
1815               /* If this is an indirect primary base, it still could be
1816                  our primary base -- unless we later find there's another
1817                  nearly-empty virtual base that isn't an indirect
1818                  primary base.  */
1819               if (!candidate)
1820                 candidate = base_binfo;
1821             }
1822         }
1823
1824       /* If we've got a primary base, use it.  */
1825       if (candidate)
1826         {
1827           set_primary_base (t, candidate, vfuns_p);
1828           CLASSTYPE_VFIELDS (t) 
1829             = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
1830         }       
1831     }
1832
1833   /* Mark the primary base classes at this point.  */
1834   mark_primary_bases (t);
1835 }
1836 \f
1837 /* Set memoizing fields and bits of T (and its variants) for later
1838    use.  */
1839
1840 static void
1841 finish_struct_bits (t)
1842      tree t;
1843 {
1844   int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
1845
1846   /* Fix up variants (if any).  */
1847   tree variants = TYPE_NEXT_VARIANT (t);
1848   while (variants)
1849     {
1850       /* These fields are in the _TYPE part of the node, not in
1851          the TYPE_LANG_SPECIFIC component, so they are not shared.  */
1852       TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
1853       TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
1854       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
1855       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 
1856         = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
1857
1858       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants) 
1859         = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
1860       TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
1861       TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
1862       /* Copy whatever these are holding today.  */
1863       TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
1864       TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
1865       TYPE_FIELDS (variants) = TYPE_FIELDS (t);
1866       TYPE_SIZE (variants) = TYPE_SIZE (t);
1867       TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
1868       variants = TYPE_NEXT_VARIANT (variants);
1869     }
1870
1871   if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
1872     /* For a class w/o baseclasses, `finish_struct' has set
1873        CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
1874        definition). Similarly for a class whose base classes do not
1875        have vtables. When neither of these is true, we might have
1876        removed abstract virtuals (by providing a definition), added
1877        some (by declaring new ones), or redeclared ones from a base
1878        class. We need to recalculate what's really an abstract virtual
1879        at this point (by looking in the vtables).  */
1880       get_pure_virtuals (t);
1881
1882   if (n_baseclasses)
1883     {
1884       /* Notice whether this class has type conversion functions defined.  */
1885       tree binfo = TYPE_BINFO (t);
1886       tree binfos = BINFO_BASETYPES (binfo);
1887       tree basetype;
1888
1889       for (i = n_baseclasses-1; i >= 0; i--)
1890         {
1891           basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
1892
1893           TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
1894         }
1895     }
1896
1897   /* If this type has a copy constructor, force its mode to be BLKmode, and
1898      force its TREE_ADDRESSABLE bit to be nonzero.  This will cause it to
1899      be passed by invisible reference and prevent it from being returned in
1900      a register.
1901
1902      Also do this if the class has BLKmode but can still be returned in
1903      registers, since function_cannot_inline_p won't let us inline
1904      functions returning such a type.  This affects the HP-PA.  */
1905   if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
1906       || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
1907           && CLASSTYPE_NON_AGGREGATE (t)))
1908     {
1909       tree variants;
1910       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
1911       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
1912         {
1913           TYPE_MODE (variants) = BLKmode;
1914           TREE_ADDRESSABLE (variants) = 1;
1915         }
1916     }
1917 }
1918
1919 /* Issue warnings about T having private constructors, but no friends,
1920    and so forth.  
1921
1922    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
1923    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
1924    non-private static member functions.  */
1925
1926 static void
1927 maybe_warn_about_overly_private_class (t)
1928      tree t;
1929 {
1930   int has_member_fn = 0;
1931   int has_nonprivate_method = 0;
1932   tree fn;
1933
1934   if (!warn_ctor_dtor_privacy
1935       /* If the class has friends, those entities might create and
1936          access instances, so we should not warn.  */
1937       || (CLASSTYPE_FRIEND_CLASSES (t)
1938           || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))
1939       /* We will have warned when the template was declared; there's
1940          no need to warn on every instantiation.  */
1941       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
1942     /* There's no reason to even consider warning about this 
1943        class.  */
1944     return;
1945     
1946   /* We only issue one warning, if more than one applies, because
1947      otherwise, on code like:
1948
1949      class A {
1950        // Oops - forgot `public:'
1951        A();
1952        A(const A&);
1953        ~A();
1954      };
1955
1956      we warn several times about essentially the same problem.  */
1957
1958   /* Check to see if all (non-constructor, non-destructor) member
1959      functions are private.  (Since there are no friends or
1960      non-private statics, we can't ever call any of the private member
1961      functions.)  */
1962   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
1963     /* We're not interested in compiler-generated methods; they don't
1964        provide any way to call private members.  */
1965     if (!DECL_ARTIFICIAL (fn)) 
1966       {
1967         if (!TREE_PRIVATE (fn))
1968           {
1969             if (DECL_STATIC_FUNCTION_P (fn)) 
1970               /* A non-private static member function is just like a
1971                  friend; it can create and invoke private member
1972                  functions, and be accessed without a class
1973                  instance.  */
1974               return;
1975                 
1976             has_nonprivate_method = 1;
1977             break;
1978           }
1979         else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
1980           has_member_fn = 1;
1981       } 
1982
1983   if (!has_nonprivate_method && has_member_fn) 
1984     {
1985       /* There are no non-private methods, and there's at least one
1986          private member function that isn't a constructor or
1987          destructor.  (If all the private members are
1988          constructors/destructors we want to use the code below that
1989          issues error messages specifically referring to
1990          constructors/destructors.)  */
1991       int i;
1992       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
1993       for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
1994         if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
1995             || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
1996           {
1997             has_nonprivate_method = 1;
1998             break;
1999           }
2000       if (!has_nonprivate_method) 
2001         {
2002           cp_warning ("all member functions in class `%T' are private", t);
2003           return;
2004         }
2005     }
2006
2007   /* Even if some of the member functions are non-private, the class
2008      won't be useful for much if all the constructors or destructors
2009      are private: such an object can never be created or destroyed.  */
2010   if (TYPE_HAS_DESTRUCTOR (t))
2011     {
2012       tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
2013
2014       if (TREE_PRIVATE (dtor))
2015         {
2016           cp_warning ("`%#T' only defines a private destructor and has no friends",
2017                       t);
2018           return;
2019         }
2020     }
2021
2022   if (TYPE_HAS_CONSTRUCTOR (t))
2023     {
2024       int nonprivate_ctor = 0;
2025           
2026       /* If a non-template class does not define a copy
2027          constructor, one is defined for it, enabling it to avoid
2028          this warning.  For a template class, this does not
2029          happen, and so we would normally get a warning on:
2030
2031            template <class T> class C { private: C(); };  
2032           
2033          To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
2034          complete non-template or fully instantiated classes have this
2035          flag set.  */
2036       if (!TYPE_HAS_INIT_REF (t))
2037         nonprivate_ctor = 1;
2038       else 
2039         for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
2040              fn;
2041              fn = OVL_NEXT (fn)) 
2042           {
2043             tree ctor = OVL_CURRENT (fn);
2044             /* Ideally, we wouldn't count copy constructors (or, in
2045                fact, any constructor that takes an argument of the
2046                class type as a parameter) because such things cannot
2047                be used to construct an instance of the class unless
2048                you already have one.  But, for now at least, we're
2049                more generous.  */
2050             if (! TREE_PRIVATE (ctor))
2051               {
2052                 nonprivate_ctor = 1;
2053                 break;
2054               }
2055           }
2056
2057       if (nonprivate_ctor == 0)
2058         {
2059           cp_warning ("`%#T' only defines private constructors and has no friends",
2060                       t);
2061           return;
2062         }
2063     }
2064 }
2065
2066 /* Function to help qsort sort FIELD_DECLs by name order.  */
2067
2068 static int
2069 field_decl_cmp (x, y)
2070      const tree *x, *y;
2071 {
2072   if (DECL_NAME (*x) == DECL_NAME (*y))
2073     /* A nontype is "greater" than a type.  */
2074     return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
2075   if (DECL_NAME (*x) == NULL_TREE)
2076     return -1;
2077   if (DECL_NAME (*y) == NULL_TREE)
2078     return 1;
2079   if (DECL_NAME (*x) < DECL_NAME (*y))
2080     return -1;
2081   return 1;
2082 }
2083
2084 /* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
2085
2086 static int
2087 method_name_cmp (m1, m2)
2088      const tree *m1, *m2;
2089 {
2090   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
2091     return 0;
2092   if (*m1 == NULL_TREE)
2093     return -1;
2094   if (*m2 == NULL_TREE)
2095     return 1;
2096   if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
2097     return -1;
2098   return 1;
2099 }
2100
2101 /* Warn about duplicate methods in fn_fields.  Also compact method
2102    lists so that lookup can be made faster.
2103
2104    Data Structure: List of method lists.  The outer list is a
2105    TREE_LIST, whose TREE_PURPOSE field is the field name and the
2106    TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
2107    links the entire list of methods for TYPE_METHODS.  Friends are
2108    chained in the same way as member functions (? TREE_CHAIN or
2109    DECL_CHAIN), but they live in the TREE_TYPE field of the outer
2110    list.  That allows them to be quickly deleted, and requires no
2111    extra storage.
2112
2113    Sort methods that are not special (i.e., constructors, destructors,
2114    and type conversion operators) so that we can find them faster in
2115    search.  */
2116
2117 static void
2118 finish_struct_methods (t)
2119      tree t;
2120 {
2121   tree fn_fields;
2122   tree method_vec;
2123   int slot, len;
2124
2125   if (!TYPE_METHODS (t))
2126     {
2127       /* Clear these for safety; perhaps some parsing error could set
2128          these incorrectly.  */
2129       TYPE_HAS_CONSTRUCTOR (t) = 0;
2130       TYPE_HAS_DESTRUCTOR (t) = 0;
2131       CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
2132       return;
2133     }
2134
2135   method_vec = CLASSTYPE_METHOD_VEC (t);
2136   my_friendly_assert (method_vec != NULL_TREE, 19991215);
2137   len = TREE_VEC_LENGTH (method_vec);
2138
2139   /* First fill in entry 0 with the constructors, entry 1 with destructors,
2140      and the next few with type conversion operators (if any).  */
2141   for (fn_fields = TYPE_METHODS (t); fn_fields; 
2142        fn_fields = TREE_CHAIN (fn_fields))
2143     /* Clear out this flag.  */
2144     DECL_IN_AGGR_P (fn_fields) = 0;
2145
2146   if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
2147     /* We thought there was a destructor, but there wasn't.  Some
2148        parse errors cause this anomalous situation.  */
2149     TYPE_HAS_DESTRUCTOR (t) = 0;
2150     
2151   /* Issue warnings about private constructors and such.  If there are
2152      no methods, then some public defaults are generated.  */
2153   maybe_warn_about_overly_private_class (t);
2154
2155   /* Now sort the methods.  */
2156   while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
2157     len--;
2158   TREE_VEC_LENGTH (method_vec) = len;
2159
2160   /* The type conversion ops have to live at the front of the vec, so we
2161      can't sort them.  */
2162   for (slot = 2; slot < len; ++slot)
2163     {
2164       tree fn = TREE_VEC_ELT (method_vec, slot);
2165   
2166       if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
2167         break;
2168     }
2169   if (len - slot > 1)
2170     qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
2171            (int (*)(const void *, const void *))method_name_cmp);
2172 }
2173
2174 /* Emit error when a duplicate definition of a type is seen.  Patch up.  */
2175
2176 void
2177 duplicate_tag_error (t)
2178      tree t;
2179 {
2180   cp_error ("redefinition of `%#T'", t);
2181   cp_error_at ("previous definition of `%#T'", t);
2182
2183   /* Pretend we haven't defined this type.  */
2184
2185   /* All of the component_decl's were TREE_CHAINed together in the parser.
2186      finish_struct_methods walks these chains and assembles all methods with
2187      the same base name into DECL_CHAINs. Now we don't need the parser chains
2188      anymore, so we unravel them.  */
2189
2190   /* This used to be in finish_struct, but it turns out that the
2191      TREE_CHAIN is used by dbxout_type_methods and perhaps some other
2192      things...  */
2193   if (CLASSTYPE_METHOD_VEC (t)) 
2194     {
2195       tree method_vec = CLASSTYPE_METHOD_VEC (t);
2196       int i, len  = TREE_VEC_LENGTH (method_vec);
2197       for (i = 0; i < len; i++)
2198         {
2199           tree unchain = TREE_VEC_ELT (method_vec, i);
2200           while (unchain != NULL_TREE) 
2201             {
2202               TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
2203               unchain = OVL_NEXT (unchain);
2204             }
2205         }
2206     }
2207
2208   if (TYPE_LANG_SPECIFIC (t))
2209     {
2210       tree binfo = TYPE_BINFO (t);
2211       int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
2212       int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
2213       tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
2214       int use_template = CLASSTYPE_USE_TEMPLATE (t);
2215
2216       memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
2217       BINFO_BASETYPES(binfo) = NULL_TREE;
2218
2219       TYPE_BINFO (t) = binfo;
2220       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
2221       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
2222       TYPE_REDEFINED (t) = 1;
2223       CLASSTYPE_TEMPLATE_INFO (t) = template_info;
2224       CLASSTYPE_USE_TEMPLATE (t) = use_template;
2225     }
2226   TYPE_SIZE (t) = NULL_TREE;
2227   TYPE_MODE (t) = VOIDmode;
2228   TYPE_FIELDS (t) = NULL_TREE;
2229   TYPE_METHODS (t) = NULL_TREE;
2230   TYPE_VFIELD (t) = NULL_TREE;
2231   TYPE_CONTEXT (t) = NULL_TREE;
2232   
2233   /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above.  */
2234   TYPE_LANG_FLAG_0 (t) = 0;
2235   TYPE_LANG_FLAG_1 (t) = 0;
2236   TYPE_LANG_FLAG_2 (t) = 0;
2237   TYPE_LANG_FLAG_3 (t) = 0;
2238   TYPE_LANG_FLAG_4 (t) = 0;
2239   TYPE_LANG_FLAG_5 (t) = 0;
2240   TYPE_LANG_FLAG_6 (t) = 0;
2241   /* But not this one.  */
2242   SET_IS_AGGR_TYPE (t, 1);
2243 }
2244
2245 /* Make BINFO's vtable have N entries, including RTTI entries,
2246    vbase and vcall offsets, etc.  Set its type and call the backend
2247    to lay it out.  */
2248
2249 static void
2250 layout_vtable_decl (binfo, n)
2251      tree binfo;
2252      int n;
2253 {
2254   tree atype;
2255   tree vtable;
2256
2257   atype = build_cplus_array_type (vtable_entry_type, 
2258                                   build_index_type (size_int (n - 1)));
2259   layout_type (atype);
2260
2261   /* We may have to grow the vtable.  */
2262   vtable = get_vtbl_decl_for_binfo (binfo);
2263   if (!same_type_p (TREE_TYPE (vtable), atype))
2264     {
2265       TREE_TYPE (vtable) = atype;
2266       DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
2267       layout_decl (vtable, 0);
2268
2269       /* At one time the vtable info was grabbed 2 words at a time.  This
2270          fails on Sparc unless you have 8-byte alignment.  */
2271       DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
2272                                  DECL_ALIGN (vtable));
2273     }
2274 }
2275
2276 /* True iff FNDECL and BASE_FNDECL (both non-static member functions)
2277    have the same signature.  */
2278
2279 int
2280 same_signature_p (fndecl, base_fndecl)
2281      tree fndecl, base_fndecl;
2282 {
2283   /* One destructor overrides another if they are the same kind of
2284      destructor.  */
2285   if (DECL_DESTRUCTOR_P (base_fndecl) && DECL_DESTRUCTOR_P (fndecl)
2286       && special_function_p (base_fndecl) == special_function_p (fndecl))
2287     return 1;
2288   /* But a non-destructor never overrides a destructor, nor vice
2289      versa, nor do different kinds of destructors override
2290      one-another.  For example, a complete object destructor does not
2291      override a deleting destructor.  */
2292   if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
2293     return 0;
2294
2295   if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
2296     {
2297       tree types, base_types;
2298       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
2299       base_types = TYPE_ARG_TYPES (TREE_TYPE (base_fndecl));
2300       if ((TYPE_QUALS (TREE_TYPE (TREE_VALUE (base_types)))
2301            == TYPE_QUALS (TREE_TYPE (TREE_VALUE (types))))
2302           && compparms (TREE_CHAIN (base_types), TREE_CHAIN (types)))
2303         return 1;
2304     }
2305   return 0;
2306 }
2307
2308 typedef struct find_final_overrider_data_s {
2309   /* The function for which we are trying to find a final overrider.  */
2310   tree fn;
2311   /* The base class in which the function was declared.  */
2312   tree declaring_base;
2313   /* The most derived class in the hierarchy.  */
2314   tree most_derived_type;
2315   /* The final overriding function.  */
2316   tree overriding_fn;
2317   /* The functions that we thought might be final overriders, but
2318      aren't.  */
2319   tree candidates;
2320   /* The BINFO for the class in which the final overriding function
2321      appears.  */
2322   tree overriding_base;
2323 } find_final_overrider_data;
2324
2325 /* Called from find_final_overrider via dfs_walk.  */
2326
2327 static tree
2328 dfs_find_final_overrider (binfo, data)
2329      tree binfo;
2330      void *data;
2331 {
2332   find_final_overrider_data *ffod = (find_final_overrider_data *) data;
2333
2334   if (same_type_p (BINFO_TYPE (binfo), 
2335                    BINFO_TYPE (ffod->declaring_base))
2336       && tree_int_cst_equal (BINFO_OFFSET (binfo),
2337                              BINFO_OFFSET (ffod->declaring_base)))
2338     {
2339       tree path;
2340       tree method;
2341
2342       /* We haven't found an overrider yet.  */
2343       method = NULL_TREE;
2344       /* We've found a path to the declaring base.  Walk down the path
2345          looking for an overrider for FN.  */
2346       for (path = reverse_path (binfo);
2347            path; 
2348            path = TREE_CHAIN (path))
2349         {
2350           method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
2351                                             ffod->fn);
2352           if (method)
2353             break;
2354         }
2355
2356       /* If we found an overrider, record the overriding function, and
2357          the base from which it came.  */
2358       if (path)
2359         {
2360           tree base;
2361
2362           /* Assume the path is non-virtual.  See if there are any
2363              virtual bases from (but not including) the overrider up
2364              to and including the base where the function is
2365              defined. */
2366           for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
2367             if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
2368               {
2369                 base = ffod->declaring_base;
2370                 break;
2371               }
2372
2373           /* If we didn't already have an overrider, or any
2374              candidates, then this function is the best candidate so
2375              far.  */
2376           if (!ffod->overriding_fn && !ffod->candidates)
2377             {
2378               ffod->overriding_fn = method;
2379               ffod->overriding_base = TREE_VALUE (path);
2380             }
2381           else if (ffod->overriding_fn)
2382             {
2383               /* We had a best overrider; let's see how this compares.  */
2384
2385               if (ffod->overriding_fn == method
2386                   && (tree_int_cst_equal 
2387                       (BINFO_OFFSET (TREE_VALUE (path)),
2388                        BINFO_OFFSET (ffod->overriding_base))))
2389                 /* We found the same overrider we already have, and in the
2390                    same place; it's still the best.  */;
2391               else if (strictly_overrides (ffod->overriding_fn, method))
2392                 /* The old function overrides this function; it's still the
2393                    best.  */;
2394               else if (strictly_overrides (method, ffod->overriding_fn))
2395                 {
2396                   /* The new function overrides the old; it's now the
2397                      best.  */
2398                   ffod->overriding_fn = method;
2399                   ffod->overriding_base = TREE_VALUE (path);
2400                 }
2401               else
2402                 {
2403                   /* Ambiguous.  */
2404                   ffod->candidates 
2405                     = build_tree_list (NULL_TREE,
2406                                        ffod->overriding_fn);
2407                   if (method != ffod->overriding_fn)
2408                     ffod->candidates 
2409                       = tree_cons (NULL_TREE, method, ffod->candidates);
2410                   ffod->overriding_fn = NULL_TREE;
2411                   ffod->overriding_base = NULL_TREE;
2412                 }
2413             }
2414           else
2415             {
2416               /* We had a list of ambiguous overrides; let's see how this
2417                  new one compares.  */
2418
2419               tree candidates;
2420               bool incomparable = false;
2421
2422               /* If there were previous candidates, and this function
2423                  overrides all of them, then it is the new best
2424                  candidate.  */
2425               for (candidates = ffod->candidates;
2426                    candidates;
2427                    candidates = TREE_CHAIN (candidates))
2428                 {
2429                   /* If the candidate overrides the METHOD, then we
2430                      needn't worry about it any further.  */
2431                   if (strictly_overrides (TREE_VALUE (candidates),
2432                                           method))
2433                     {
2434                       method = NULL_TREE;
2435                       break;
2436                     }
2437
2438                   /* If the METHOD doesn't override the candidate,
2439                      then it is incomporable.  */
2440                   if (!strictly_overrides (method,
2441                                            TREE_VALUE (candidates)))
2442                     incomparable = true;
2443                 }
2444
2445               /* If METHOD overrode all the candidates, then it is the
2446                  new best candidate.  */
2447               if (!candidates && !incomparable)
2448                 {
2449                   ffod->overriding_fn = method;
2450                   ffod->overriding_base = TREE_VALUE (path);
2451                   ffod->candidates = NULL_TREE;
2452                 }
2453               /* If METHOD didn't override all the candidates, then it
2454                  is another candidate.  */
2455               else if (method && incomparable)
2456                 ffod->candidates 
2457                   = tree_cons (NULL_TREE, method, ffod->candidates);
2458             }
2459         }
2460     }
2461
2462   return NULL_TREE;
2463 }
2464
2465 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
2466    FN and whose TREE_VALUE is the binfo for the base where the
2467    overriding occurs.  BINFO (in the hierarchy dominated by T) is the
2468    base object in which FN is declared.  */
2469
2470 static tree
2471 find_final_overrider (t, binfo, fn)
2472      tree t;
2473      tree binfo;
2474      tree fn;
2475 {
2476   find_final_overrider_data ffod;
2477
2478   /* Getting this right is a little tricky.  This is legal:
2479
2480        struct S { virtual void f (); };
2481        struct T { virtual void f (); };
2482        struct U : public S, public T { };
2483
2484      even though calling `f' in `U' is ambiguous.  But, 
2485
2486        struct R { virtual void f(); };
2487        struct S : virtual public R { virtual void f (); };
2488        struct T : virtual public R { virtual void f (); };
2489        struct U : public S, public T { };
2490
2491      is not -- there's no way to decide whether to put `S::f' or
2492      `T::f' in the vtable for `R'.  
2493      
2494      The solution is to look at all paths to BINFO.  If we find
2495      different overriders along any two, then there is a problem.  */
2496   ffod.fn = fn;
2497   ffod.declaring_base = binfo;
2498   ffod.most_derived_type = t;
2499   ffod.overriding_fn = NULL_TREE;
2500   ffod.overriding_base = NULL_TREE;
2501   ffod.candidates = NULL_TREE;
2502
2503   dfs_walk (TYPE_BINFO (t),
2504             dfs_find_final_overrider,
2505             NULL,
2506             &ffod);
2507
2508   /* If there was no winner, issue an error message.  */
2509   if (!ffod.overriding_fn)
2510     {
2511       cp_error ("no unique final overrider for `%D' in `%T'", fn, t);
2512       return error_mark_node;
2513     }
2514
2515   return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
2516 }
2517
2518 /* Returns the function from the BINFO_VIRTUALS entry in T which matches
2519    the signature of FUNCTION_DECL FN, or NULL_TREE if none.  In other words,
2520    the function that the slot in T's primary vtable points to.  */
2521
2522 static tree get_matching_virtual PARAMS ((tree, tree));
2523 static tree
2524 get_matching_virtual (t, fn)
2525      tree t, fn;
2526 {
2527   tree f;
2528
2529   for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
2530     if (same_signature_p (BV_FN (f), fn))
2531       return BV_FN (f);
2532   return NULL_TREE;
2533 }
2534
2535 /* Update an entry in the vtable for BINFO, which is in the hierarchy
2536    dominated by T.  FN has been overriden in BINFO; VIRTUALS points to the
2537    corresponding position in the BINFO_VIRTUALS list.  */
2538
2539 static void
2540 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
2541      tree t;
2542      tree binfo;
2543      tree fn;
2544      tree *virtuals;
2545 {
2546   tree b;
2547   tree overrider;
2548   tree delta;
2549   tree virtual_base;
2550   tree first_defn;
2551
2552   /* Find the nearest primary base (possibly binfo itself) which defines
2553      this function; this is the class the caller will convert to when
2554      calling FN through BINFO.  */
2555   for (b = binfo; ; b = get_primary_binfo (b))
2556     {
2557       if (look_for_overrides_here (BINFO_TYPE (b), fn))
2558         break;
2559     }
2560   first_defn = b;
2561
2562   /* Find the final overrider.  */
2563   overrider = find_final_overrider (t, b, fn);
2564   if (overrider == error_mark_node)
2565     return;
2566
2567   /* Assume that we will produce a thunk that convert all the way to
2568      the final overrider, and not to an intermediate virtual base.  */
2569   virtual_base = NULL_TREE;
2570
2571   /* We will convert to an intermediate virtual base first, and then
2572      use the vcall offset located there to finish the conversion.  */
2573   while (b)
2574     {
2575       /* If we find the final overrider, then we can stop
2576          walking.  */
2577       if (same_type_p (BINFO_TYPE (b), 
2578                        BINFO_TYPE (TREE_VALUE (overrider))))
2579         break;
2580
2581       /* If we find a virtual base, and we haven't yet found the
2582          overrider, then there is a virtual base between the
2583          declaring base (first_defn) and the final overrider.  */
2584       if (!virtual_base && TREE_VIA_VIRTUAL (b))
2585         virtual_base = b;
2586
2587       b = BINFO_INHERITANCE_CHAIN (b);
2588     }
2589
2590   /* Compute the constant adjustment to the `this' pointer.  The
2591      `this' pointer, when this function is called, will point at BINFO
2592      (or one of its primary bases, which are at the same offset).  */
2593
2594   if (virtual_base)
2595     /* The `this' pointer needs to be adjusted from the declaration to
2596        the nearest virtual base.  */
2597     delta = size_diffop (BINFO_OFFSET (virtual_base),
2598                          BINFO_OFFSET (first_defn));
2599   else
2600     {
2601       /* The `this' pointer needs to be adjusted from pointing to
2602          BINFO to pointing at the base where the final overrider
2603          appears.  */
2604       delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
2605                            BINFO_OFFSET (binfo));
2606
2607       if (! integer_zerop (delta))
2608         {
2609           /* We'll need a thunk.  But if we have a (perhaps formerly)
2610              primary virtual base, we have a vcall slot for this function,
2611              so we can use it rather than create a non-virtual thunk.  */
2612           
2613           b = get_primary_binfo (first_defn);
2614           for (; b; b = get_primary_binfo (b))
2615             {
2616               tree f = get_matching_virtual (BINFO_TYPE (b), fn);
2617               if (!f)
2618                 /* b doesn't have this function; no suitable vbase.  */
2619                 break;
2620               if (TREE_VIA_VIRTUAL (b))
2621                 {
2622                   /* Found one; we can treat ourselves as a virtual base.  */
2623                   virtual_base = binfo;
2624                   delta = size_zero_node;
2625                   break;
2626                 }
2627             }
2628         }
2629     }
2630
2631   modify_vtable_entry (t, 
2632                        binfo, 
2633                        TREE_PURPOSE (overrider),
2634                        delta,
2635                        virtuals);
2636
2637   if (virtual_base)
2638     BV_USE_VCALL_INDEX_P (*virtuals) = 1;
2639 }
2640
2641 /* Called from modify_all_vtables via dfs_walk.  */
2642
2643 static tree
2644 dfs_modify_vtables (binfo, data)
2645      tree binfo;
2646      void *data;
2647 {
2648   if (/* There's no need to modify the vtable for a non-virtual
2649          primary base; we're not going to use that vtable anyhow.
2650          We do still need to do this for virtual primary bases, as they
2651          could become non-primary in a construction vtable.  */
2652       (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
2653       /* Similarly, a base without a vtable needs no modification.  */
2654       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
2655     {
2656       tree t;
2657       tree virtuals;
2658       tree old_virtuals;
2659
2660       t = (tree) data;
2661
2662       make_new_vtable (t, binfo);
2663       
2664       /* Now, go through each of the virtual functions in the virtual
2665          function table for BINFO.  Find the final overrider, and
2666          update the BINFO_VIRTUALS list appropriately.  */
2667       for (virtuals = BINFO_VIRTUALS (binfo),
2668              old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
2669            virtuals;
2670            virtuals = TREE_CHAIN (virtuals),
2671              old_virtuals = TREE_CHAIN (old_virtuals))
2672         update_vtable_entry_for_fn (t, 
2673                                     binfo, 
2674                                     BV_FN (old_virtuals),
2675                                     &virtuals);
2676     }
2677
2678   SET_BINFO_MARKED (binfo);
2679
2680   return NULL_TREE;
2681 }
2682
2683 /* Update all of the primary and secondary vtables for T.  Create new
2684    vtables as required, and initialize their RTTI information.  Each
2685    of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
2686    function from a base class; find and modify the appropriate entries
2687    to point to the overriding functions.  Returns a list, in
2688    declaration order, of the functions that are overridden in this
2689    class, but do not appear in the primary base class vtable, and
2690    which should therefore be appended to the end of the vtable for T.  */
2691
2692 static tree
2693 modify_all_vtables (t, vfuns_p, overridden_virtuals)
2694      tree t;
2695      int *vfuns_p;
2696      tree overridden_virtuals;
2697 {
2698   tree binfo = TYPE_BINFO (t);
2699   tree *fnsp;
2700
2701   /* Update all of the vtables.  */
2702   dfs_walk (binfo, 
2703             dfs_modify_vtables, 
2704             dfs_unmarked_real_bases_queue_p,
2705             t);
2706   dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
2707
2708   /* Include overriding functions for secondary vtables in our primary
2709      vtable.  */
2710   for (fnsp = &overridden_virtuals; *fnsp; )
2711     {
2712       tree fn = TREE_VALUE (*fnsp);
2713
2714       if (!BINFO_VIRTUALS (binfo)
2715           || !value_member (fn, BINFO_VIRTUALS (binfo)))
2716         {
2717           /* Set the vtable index.  */
2718           set_vindex (fn, vfuns_p);
2719           /* We don't need to convert to a base class when calling
2720              this function.  */
2721           DECL_VIRTUAL_CONTEXT (fn) = t;
2722
2723           /* We don't need to adjust the `this' pointer when
2724              calling this function.  */
2725           BV_DELTA (*fnsp) = integer_zero_node;
2726           BV_VCALL_INDEX (*fnsp) = NULL_TREE;
2727
2728           /* This is an overridden function not already in our
2729              vtable.  Keep it.  */
2730           fnsp = &TREE_CHAIN (*fnsp);
2731         }
2732       else
2733         /* We've already got an entry for this function.  Skip it.  */
2734         *fnsp = TREE_CHAIN (*fnsp);
2735     }
2736   
2737   return overridden_virtuals;
2738 }
2739
2740 /* Here, we already know that they match in every respect.
2741    All we have to check is where they had their declarations.  */
2742
2743 static int 
2744 strictly_overrides (fndecl1, fndecl2)
2745      tree fndecl1, fndecl2;
2746 {
2747   int distance = get_base_distance (DECL_CONTEXT (fndecl2),
2748                                     DECL_CONTEXT (fndecl1),
2749                                     0, (tree *)0);
2750   if (distance == -2 || distance > 0)
2751     return 1;
2752   return 0;
2753 }
2754
2755 /* Get the base virtual function declarations in T that are either
2756    overridden or hidden by FNDECL as a list.  We set TREE_PURPOSE with
2757    the overrider/hider.  */
2758
2759 static tree
2760 get_basefndecls (fndecl, t)
2761      tree fndecl, t;
2762 {
2763   tree methods = TYPE_METHODS (t);
2764   tree base_fndecls = NULL_TREE;
2765   tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2766   int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2767
2768   while (methods)
2769     {
2770       if (TREE_CODE (methods) == FUNCTION_DECL
2771           && DECL_VINDEX (methods) != NULL_TREE
2772           && DECL_NAME (fndecl) == DECL_NAME (methods))
2773         base_fndecls = tree_cons (fndecl, methods, base_fndecls);
2774
2775       methods = TREE_CHAIN (methods);
2776     }
2777
2778   if (base_fndecls)
2779     return base_fndecls;
2780
2781   for (i = 0; i < n_baseclasses; i++)
2782     {
2783       tree base_binfo = TREE_VEC_ELT (binfos, i);
2784       tree basetype = BINFO_TYPE (base_binfo);
2785
2786       base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2787                               base_fndecls);
2788     }
2789
2790   return base_fndecls;
2791 }
2792
2793 /* Mark the functions that have been hidden with their overriders.
2794    Since we start out with all functions already marked with a hider,
2795    no need to mark functions that are just hidden.
2796
2797    Subroutine of warn_hidden.  */
2798
2799 static void
2800 mark_overriders (fndecl, base_fndecls)
2801      tree fndecl, base_fndecls;
2802 {
2803   for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2804     if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
2805       TREE_PURPOSE (base_fndecls) = fndecl;
2806 }
2807
2808 /* If this declaration supersedes the declaration of
2809    a method declared virtual in the base class, then
2810    mark this field as being virtual as well.  */
2811
2812 static void
2813 check_for_override (decl, ctype)
2814      tree decl, ctype;
2815 {
2816   if (TREE_CODE (decl) == TEMPLATE_DECL)
2817     /* In [temp.mem] we have:
2818
2819          A specialization of a member function template does not
2820          override a virtual function from a base class.  */
2821     return;
2822   if ((DECL_DESTRUCTOR_P (decl)
2823        || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
2824       && look_for_overrides (ctype, decl)
2825       && !DECL_STATIC_FUNCTION_P (decl))
2826     {
2827       /* Set DECL_VINDEX to a value that is neither an
2828          INTEGER_CST nor the error_mark_node so that
2829          add_virtual_function will realize this is an
2830          overriding function.  */
2831       DECL_VINDEX (decl) = decl;
2832     }
2833   if (DECL_VIRTUAL_P (decl))
2834     {
2835       if (DECL_VINDEX (decl) == NULL_TREE)
2836         DECL_VINDEX (decl) = error_mark_node;
2837       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
2838     }
2839 }
2840
2841 /* Warn about hidden virtual functions that are not overridden in t.
2842    We know that constructors and destructors don't apply.  */
2843
2844 void
2845 warn_hidden (t)
2846      tree t;
2847 {
2848   tree method_vec = CLASSTYPE_METHOD_VEC (t);
2849   int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
2850   int i;
2851
2852   /* We go through each separately named virtual function.  */
2853   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
2854     {
2855       tree fns = TREE_VEC_ELT (method_vec, i);
2856       tree fndecl = NULL_TREE;
2857
2858       tree base_fndecls = NULL_TREE;
2859       tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
2860       int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
2861
2862       /* First see if we have any virtual functions in this batch.  */
2863       for (; fns; fns = OVL_NEXT (fns))
2864         {
2865           fndecl = OVL_CURRENT (fns);
2866           if (DECL_VINDEX (fndecl))
2867             break;
2868         }
2869
2870       if (fns == NULL_TREE)
2871         continue;
2872
2873       /* First we get a list of all possible functions that might be
2874          hidden from each base class.  */
2875       for (i = 0; i < n_baseclasses; i++)
2876         {
2877           tree base_binfo = TREE_VEC_ELT (binfos, i);
2878           tree basetype = BINFO_TYPE (base_binfo);
2879
2880           base_fndecls = chainon (get_basefndecls (fndecl, basetype),
2881                                   base_fndecls);
2882         }
2883
2884       fns = OVL_NEXT (fns);
2885
2886       /* ...then mark up all the base functions with overriders, preferring
2887          overriders to hiders.  */
2888       if (base_fndecls)
2889         for (; fns; fns = OVL_NEXT (fns))
2890           {
2891             fndecl = OVL_CURRENT (fns);
2892             if (DECL_VINDEX (fndecl))
2893               mark_overriders (fndecl, base_fndecls);
2894           }
2895
2896       /* Now give a warning for all base functions without overriders,
2897          as they are hidden.  */
2898       for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
2899         if (!same_signature_p (TREE_PURPOSE (base_fndecls),
2900                                TREE_VALUE (base_fndecls)))
2901           {
2902             /* Here we know it is a hider, and no overrider exists.  */
2903             cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
2904             cp_warning_at ("  by `%D'", TREE_PURPOSE (base_fndecls));
2905           }
2906     }
2907 }
2908
2909 /* Check for things that are invalid.  There are probably plenty of other
2910    things we should check for also.  */
2911
2912 static void
2913 finish_struct_anon (t)
2914      tree t;
2915 {
2916   tree field;
2917
2918   for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
2919     {
2920       if (TREE_STATIC (field))
2921         continue;
2922       if (TREE_CODE (field) != FIELD_DECL)
2923         continue;
2924
2925       if (DECL_NAME (field) == NULL_TREE
2926           && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
2927         {
2928           tree elt = TYPE_FIELDS (TREE_TYPE (field));
2929           for (; elt; elt = TREE_CHAIN (elt))
2930             {
2931               /* We're generally only interested in entities the user
2932                  declared, but we also find nested classes by noticing
2933                  the TYPE_DECL that we create implicitly.  You're
2934                  allowed to put one anonymous union inside another,
2935                  though, so we explicitly tolerate that.  We use
2936                  TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
2937                  we also allow unnamed types used for defining fields.  */
2938               if (DECL_ARTIFICIAL (elt) 
2939                   && (!DECL_IMPLICIT_TYPEDEF_P (elt)
2940                       || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
2941                 continue;
2942
2943               if (DECL_NAME (elt) == constructor_name (t))
2944                 cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
2945                                elt);
2946
2947               if (TREE_CODE (elt) != FIELD_DECL)
2948                 {
2949                   cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
2950                                  elt);
2951                   continue;
2952                 }
2953
2954               if (TREE_PRIVATE (elt))
2955                 cp_pedwarn_at ("private member `%#D' in anonymous union",
2956                                elt);
2957               else if (TREE_PROTECTED (elt))
2958                 cp_pedwarn_at ("protected member `%#D' in anonymous union",
2959                                elt);
2960
2961               TREE_PRIVATE (elt) = TREE_PRIVATE (field);
2962               TREE_PROTECTED (elt) = TREE_PROTECTED (field);
2963             }
2964         }
2965     }
2966 }
2967
2968 /* Create default constructors, assignment operators, and so forth for
2969    the type indicated by T, if they are needed.
2970    CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
2971    CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
2972    class cannot have a default constructor, copy constructor taking a
2973    const reference argument, or an assignment operator taking a const
2974    reference, respectively.  If a virtual destructor is created, its
2975    DECL is returned; otherwise the return value is NULL_TREE.  */
2976
2977 static tree
2978 add_implicitly_declared_members (t, cant_have_default_ctor,
2979                                  cant_have_const_cctor,
2980                                  cant_have_const_assignment)
2981      tree t;
2982      int cant_have_default_ctor;
2983      int cant_have_const_cctor;
2984      int cant_have_const_assignment;
2985 {
2986   tree default_fn;
2987   tree implicit_fns = NULL_TREE;
2988   tree virtual_dtor = NULL_TREE;
2989   tree *f;
2990
2991   /* Destructor.  */
2992   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
2993     {
2994       default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
2995       check_for_override (default_fn, t);
2996
2997       /* If we couldn't make it work, then pretend we didn't need it.  */
2998       if (default_fn == void_type_node)
2999         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
3000       else
3001         {
3002           TREE_CHAIN (default_fn) = implicit_fns;
3003           implicit_fns = default_fn;
3004
3005           if (DECL_VINDEX (default_fn))
3006             virtual_dtor = default_fn;
3007         }
3008     }
3009   else
3010     /* Any non-implicit destructor is non-trivial.  */
3011     TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
3012
3013   /* Default constructor.  */
3014   if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
3015     {
3016       default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
3017       TREE_CHAIN (default_fn) = implicit_fns;
3018       implicit_fns = default_fn;
3019     }
3020
3021   /* Copy constructor.  */
3022   if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
3023     {
3024       /* ARM 12.18: You get either X(X&) or X(const X&), but
3025          not both.  --Chip  */
3026       default_fn 
3027         = implicitly_declare_fn (sfk_copy_constructor, t,
3028                                  /*const_p=*/!cant_have_const_cctor);
3029       TREE_CHAIN (default_fn) = implicit_fns;
3030       implicit_fns = default_fn;
3031     }
3032
3033   /* Assignment operator.  */
3034   if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
3035     {
3036       default_fn 
3037         = implicitly_declare_fn (sfk_assignment_operator, t,
3038                                  /*const_p=*/!cant_have_const_assignment);
3039       TREE_CHAIN (default_fn) = implicit_fns;
3040       implicit_fns = default_fn;
3041     }
3042
3043   /* Now, hook all of the new functions on to TYPE_METHODS,
3044      and add them to the CLASSTYPE_METHOD_VEC.  */
3045   for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
3046     add_method (t, *f, /*error_p=*/0);
3047   *f = TYPE_METHODS (t);
3048   TYPE_METHODS (t) = implicit_fns;
3049
3050   return virtual_dtor;
3051 }
3052
3053 /* Subroutine of finish_struct_1.  Recursively count the number of fields
3054    in TYPE, including anonymous union members.  */
3055
3056 static int
3057 count_fields (fields)
3058      tree fields;
3059 {
3060   tree x;
3061   int n_fields = 0;
3062   for (x = fields; x; x = TREE_CHAIN (x))
3063     {
3064       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3065         n_fields += count_fields (TYPE_FIELDS (TREE_TYPE (x)));
3066       else
3067         n_fields += 1;
3068     }
3069   return n_fields;
3070 }
3071
3072 /* Subroutine of finish_struct_1.  Recursively add all the fields in the
3073    TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX.  */
3074
3075 static int
3076 add_fields_to_vec (fields, field_vec, idx)
3077      tree fields, field_vec;
3078      int idx;
3079 {
3080   tree x;
3081   for (x = fields; x; x = TREE_CHAIN (x))
3082     {
3083       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
3084         idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
3085       else
3086         TREE_VEC_ELT (field_vec, idx++) = x;
3087     }
3088   return idx;
3089 }
3090
3091 /* FIELD is a bit-field.  We are finishing the processing for its
3092    enclosing type.  Issue any appropriate messages and set appropriate
3093    flags.  */
3094
3095 static void
3096 check_bitfield_decl (field)
3097      tree field;
3098 {
3099   tree type = TREE_TYPE (field);
3100   tree w = NULL_TREE;
3101
3102   /* Detect invalid bit-field type.  */
3103   if (DECL_INITIAL (field)
3104       && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
3105     {
3106       cp_error_at ("bit-field `%#D' with non-integral type", field);
3107       w = error_mark_node;
3108     }
3109
3110   /* Detect and ignore out of range field width.  */
3111   if (DECL_INITIAL (field))
3112     {
3113       w = DECL_INITIAL (field);
3114
3115       /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
3116       STRIP_NOPS (w);
3117
3118       /* detect invalid field size.  */
3119       if (TREE_CODE (w) == CONST_DECL)
3120         w = DECL_INITIAL (w);
3121       else
3122         w = decl_constant_value (w);
3123
3124       if (TREE_CODE (w) != INTEGER_CST)
3125         {
3126           cp_error_at ("bit-field `%D' width not an integer constant",
3127                        field);
3128           w = error_mark_node;
3129         }
3130       else if (tree_int_cst_sgn (w) < 0)
3131         {
3132           cp_error_at ("negative width in bit-field `%D'", field);
3133           w = error_mark_node;
3134         }
3135       else if (integer_zerop (w) && DECL_NAME (field) != 0)
3136         {
3137           cp_error_at ("zero width for bit-field `%D'", field);
3138           w = error_mark_node;
3139         }
3140       else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
3141                && TREE_CODE (type) != ENUMERAL_TYPE
3142                && TREE_CODE (type) != BOOLEAN_TYPE)
3143         cp_warning_at ("width of `%D' exceeds its type", field);
3144       else if (TREE_CODE (type) == ENUMERAL_TYPE
3145                && (0 > compare_tree_int (w,
3146                                          min_precision (TYPE_MIN_VALUE (type),
3147                                                         TREE_UNSIGNED (type)))
3148                    ||  0 > compare_tree_int (w,
3149                                              min_precision
3150                                              (TYPE_MAX_VALUE (type),
3151                                               TREE_UNSIGNED (type)))))
3152         cp_warning_at ("`%D' is too small to hold all values of `%#T'",
3153                        field, type);
3154     }
3155   
3156   /* Remove the bit-field width indicator so that the rest of the
3157      compiler does not treat that value as an initializer.  */
3158   DECL_INITIAL (field) = NULL_TREE;
3159
3160   if (w != error_mark_node)
3161     {
3162       DECL_SIZE (field) = convert (bitsizetype, w);
3163       DECL_BIT_FIELD (field) = 1;
3164
3165       if (integer_zerop (w))
3166         {
3167 #ifdef EMPTY_FIELD_BOUNDARY
3168           DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3169                                     EMPTY_FIELD_BOUNDARY);
3170 #endif
3171 #ifdef PCC_BITFIELD_TYPE_MATTERS
3172           if (PCC_BITFIELD_TYPE_MATTERS)
3173             {
3174               DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3175                                         TYPE_ALIGN (type));
3176               DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3177             }
3178 #endif
3179         }
3180     }
3181   else
3182     {
3183       /* Non-bit-fields are aligned for their type.  */
3184       DECL_BIT_FIELD (field) = 0;
3185       CLEAR_DECL_C_BIT_FIELD (field);
3186       DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
3187       DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
3188     }
3189 }
3190
3191 /* FIELD is a non bit-field.  We are finishing the processing for its
3192    enclosing type T.  Issue any appropriate messages and set appropriate
3193    flags.  */
3194
3195 static void
3196 check_field_decl (field, t, cant_have_const_ctor,
3197                   cant_have_default_ctor, no_const_asn_ref,
3198                   any_default_members)
3199      tree field;
3200      tree t;
3201      int *cant_have_const_ctor;
3202      int *cant_have_default_ctor;
3203      int *no_const_asn_ref;
3204      int *any_default_members;
3205 {
3206   tree type = strip_array_types (TREE_TYPE (field));
3207
3208   /* An anonymous union cannot contain any fields which would change
3209      the settings of CANT_HAVE_CONST_CTOR and friends.  */
3210   if (ANON_UNION_TYPE_P (type))
3211     ;
3212   /* And, we don't set TYPE_HAS_CONST_INIT_REF, etc., for anonymous
3213      structs.  So, we recurse through their fields here.  */
3214   else if (ANON_AGGR_TYPE_P (type))
3215     {
3216       tree fields;
3217
3218       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
3219         if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
3220           check_field_decl (fields, t, cant_have_const_ctor,
3221                             cant_have_default_ctor, no_const_asn_ref,
3222                             any_default_members);
3223     }
3224   /* Check members with class type for constructors, destructors,
3225      etc.  */
3226   else if (CLASS_TYPE_P (type))
3227     {
3228       /* Never let anything with uninheritable virtuals
3229          make it through without complaint.  */
3230       abstract_virtuals_error (field, type);
3231                       
3232       if (TREE_CODE (t) == UNION_TYPE)
3233         {
3234           if (TYPE_NEEDS_CONSTRUCTING (type))
3235             cp_error_at ("member `%#D' with constructor not allowed in union",
3236                          field);
3237           if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
3238             cp_error_at ("member `%#D' with destructor not allowed in union",
3239                          field);
3240           if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
3241             cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
3242                          field);
3243         }
3244       else
3245         {
3246           TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
3247           TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
3248             |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
3249           TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
3250           TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
3251         }
3252
3253       if (!TYPE_HAS_CONST_INIT_REF (type))
3254         *cant_have_const_ctor = 1;
3255
3256       if (!TYPE_HAS_CONST_ASSIGN_REF (type))
3257         *no_const_asn_ref = 1;
3258
3259       if (TYPE_HAS_CONSTRUCTOR (type)
3260           && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
3261         *cant_have_default_ctor = 1;
3262     }
3263   if (DECL_INITIAL (field) != NULL_TREE)
3264     {
3265       /* `build_class_init_list' does not recognize
3266          non-FIELD_DECLs.  */
3267       if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
3268         cp_error_at ("multiple fields in union `%T' initialized");
3269       *any_default_members = 1;
3270     }
3271
3272   /* Non-bit-fields are aligned for their type, except packed fields
3273      which require only BITS_PER_UNIT alignment.  */
3274   DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
3275                             (DECL_PACKED (field) 
3276                              ? BITS_PER_UNIT
3277                              : TYPE_ALIGN (TREE_TYPE (field))));
3278   if (! DECL_PACKED (field))
3279     DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
3280 }
3281
3282 /* Check the data members (both static and non-static), class-scoped
3283    typedefs, etc., appearing in the declaration of T.  Issue
3284    appropriate diagnostics.  Sets ACCESS_DECLS to a list (in
3285    declaration order) of access declarations; each TREE_VALUE in this
3286    list is a USING_DECL.
3287
3288    In addition, set the following flags:
3289
3290      EMPTY_P
3291        The class is empty, i.e., contains no non-static data members.
3292
3293      CANT_HAVE_DEFAULT_CTOR_P
3294        This class cannot have an implicitly generated default
3295        constructor.
3296
3297      CANT_HAVE_CONST_CTOR_P
3298        This class cannot have an implicitly generated copy constructor
3299        taking a const reference.
3300
3301      CANT_HAVE_CONST_ASN_REF
3302        This class cannot have an implicitly generated assignment
3303        operator taking a const reference.
3304
3305    All of these flags should be initialized before calling this
3306    function.
3307
3308    Returns a pointer to the end of the TYPE_FIELDs chain; additional
3309    fields can be added by adding to this chain.  */
3310
3311 static void
3312 check_field_decls (t, access_decls, empty_p, 
3313                    cant_have_default_ctor_p, cant_have_const_ctor_p,
3314                    no_const_asn_ref_p)
3315      tree t;
3316      tree *access_decls;
3317      int *empty_p;
3318      int *cant_have_default_ctor_p;
3319      int *cant_have_const_ctor_p;
3320      int *no_const_asn_ref_p;
3321 {
3322   tree *field;
3323   tree *next;
3324   int has_pointers;
3325   int any_default_members;
3326
3327   /* First, delete any duplicate fields.  */
3328   delete_duplicate_fields (TYPE_FIELDS (t));
3329
3330   /* Assume there are no access declarations.  */
3331   *access_decls = NULL_TREE;
3332   /* Assume this class has no pointer members.  */
3333   has_pointers = 0;
3334   /* Assume none of the members of this class have default
3335      initializations.  */
3336   any_default_members = 0;
3337
3338   for (field = &TYPE_FIELDS (t); *field; field = next)
3339     {
3340       tree x = *field;
3341       tree type = TREE_TYPE (x);
3342
3343       GNU_xref_member (current_class_name, x);
3344
3345       next = &TREE_CHAIN (x);
3346
3347       if (TREE_CODE (x) == FIELD_DECL)
3348         {
3349           DECL_PACKED (x) |= TYPE_PACKED (t);
3350
3351           if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
3352             /* We don't treat zero-width bitfields as making a class
3353                non-empty.  */
3354             ;
3355           else
3356             {
3357               /* The class is non-empty.  */
3358               *empty_p = 0;
3359               /* The class is not even nearly empty.  */
3360               CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
3361             }
3362         }
3363
3364       if (TREE_CODE (x) == USING_DECL)
3365         {
3366           /* Prune the access declaration from the list of fields.  */
3367           *field = TREE_CHAIN (x);
3368
3369           /* Save the access declarations for our caller.  */
3370           *access_decls = tree_cons (NULL_TREE, x, *access_decls);
3371
3372           /* Since we've reset *FIELD there's no reason to skip to the
3373              next field.  */
3374           next = field;
3375           continue;
3376         }
3377
3378       if (TREE_CODE (x) == TYPE_DECL
3379           || TREE_CODE (x) == TEMPLATE_DECL)
3380         continue;
3381
3382       /* If we've gotten this far, it's a data member, possibly static,
3383          or an enumerator.  */
3384
3385       DECL_CONTEXT (x) = t;
3386
3387       /* ``A local class cannot have static data members.'' ARM 9.4 */
3388       if (current_function_decl && TREE_STATIC (x))
3389         cp_error_at ("field `%D' in local class cannot be static", x);
3390
3391       /* Perform error checking that did not get done in
3392          grokdeclarator.  */
3393       if (TREE_CODE (type) == FUNCTION_TYPE)
3394         {
3395           cp_error_at ("field `%D' invalidly declared function type",
3396                        x);
3397           type = build_pointer_type (type);
3398           TREE_TYPE (x) = type;
3399         }
3400       else if (TREE_CODE (type) == METHOD_TYPE)
3401         {
3402           cp_error_at ("field `%D' invalidly declared method type", x);
3403           type = build_pointer_type (type);
3404           TREE_TYPE (x) = type;
3405         }
3406       else if (TREE_CODE (type) == OFFSET_TYPE)
3407         {
3408           cp_error_at ("field `%D' invalidly declared offset type", x);
3409           type = build_pointer_type (type);
3410           TREE_TYPE (x) = type;
3411         }
3412
3413       if (type == error_mark_node)
3414         continue;
3415           
3416       /* When this goes into scope, it will be a non-local reference.  */
3417       DECL_NONLOCAL (x) = 1;
3418
3419       if (TREE_CODE (x) == CONST_DECL)
3420         continue;
3421
3422       if (TREE_CODE (x) == VAR_DECL)
3423         {
3424           if (TREE_CODE (t) == UNION_TYPE)
3425             /* Unions cannot have static members.  */
3426             cp_error_at ("field `%D' declared static in union", x);
3427               
3428           continue;
3429         }
3430
3431       /* Now it can only be a FIELD_DECL.  */
3432
3433       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
3434         CLASSTYPE_NON_AGGREGATE (t) = 1;
3435
3436       /* If this is of reference type, check if it needs an init.
3437          Also do a little ANSI jig if necessary.  */
3438       if (TREE_CODE (type) == REFERENCE_TYPE)
3439         {
3440           CLASSTYPE_NON_POD_P (t) = 1;
3441           if (DECL_INITIAL (x) == NULL_TREE)
3442             CLASSTYPE_REF_FIELDS_NEED_INIT (t) = 1;
3443
3444           /* ARM $12.6.2: [A member initializer list] (or, for an
3445              aggregate, initialization by a brace-enclosed list) is the
3446              only way to initialize nonstatic const and reference
3447              members.  */
3448           *cant_have_default_ctor_p = 1;
3449           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3450
3451           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3452             cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
3453         }
3454
3455       type = strip_array_types (type);
3456       
3457       if (TREE_CODE (type) == POINTER_TYPE)
3458         has_pointers = 1;
3459
3460       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
3461         CLASSTYPE_HAS_MUTABLE (t) = 1;
3462
3463       if (! pod_type_p (type))
3464         /* DR 148 now allows pointers to members (which are POD themselves),
3465            to be allowed in POD structs.  */
3466         CLASSTYPE_NON_POD_P (t) = 1;
3467
3468       /* If any field is const, the structure type is pseudo-const.  */
3469       if (CP_TYPE_CONST_P (type))
3470         {
3471           C_TYPE_FIELDS_READONLY (t) = 1;
3472           if (DECL_INITIAL (x) == NULL_TREE)
3473             CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) = 1;
3474
3475           /* ARM $12.6.2: [A member initializer list] (or, for an
3476              aggregate, initialization by a brace-enclosed list) is the
3477              only way to initialize nonstatic const and reference
3478              members.  */
3479           *cant_have_default_ctor_p = 1;
3480           TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
3481
3482           if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
3483             cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
3484         }
3485       /* A field that is pseudo-const makes the structure likewise.  */
3486       else if (IS_AGGR_TYPE (type))
3487         {
3488           C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
3489           CLASSTYPE_READONLY_FIELDS_NEED_INIT (t) 
3490             |= CLASSTYPE_READONLY_FIELDS_NEED_INIT (type);
3491         }
3492
3493       /* Core issue 80: A nonstatic data member is required to have a
3494          different name from the class iff the class has a
3495          user-defined constructor.  */
3496       if (DECL_NAME (x) == constructor_name (t)
3497           && TYPE_HAS_CONSTRUCTOR (t))
3498         cp_pedwarn_at ("field `%#D' with same name as class", x);
3499
3500       /* We set DECL_C_BIT_FIELD in grokbitfield.
3501          If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
3502       if (DECL_C_BIT_FIELD (x))
3503         check_bitfield_decl (x);
3504       else
3505         check_field_decl (x, t,
3506                           cant_have_const_ctor_p,
3507                           cant_have_default_ctor_p, 
3508                           no_const_asn_ref_p,
3509                           &any_default_members);
3510     }
3511
3512   /* Effective C++ rule 11.  */
3513   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
3514       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
3515     {
3516       cp_warning ("`%#T' has pointer data members", t);
3517       
3518       if (! TYPE_HAS_INIT_REF (t))
3519         {
3520           cp_warning ("  but does not override `%T(const %T&)'", t, t);
3521           if (! TYPE_HAS_ASSIGN_REF (t))
3522             cp_warning ("  or `operator=(const %T&)'", t);
3523         }
3524       else if (! TYPE_HAS_ASSIGN_REF (t))
3525         cp_warning ("  but does not override `operator=(const %T&)'", t);
3526     }
3527
3528
3529   /* Check anonymous struct/anonymous union fields.  */
3530   finish_struct_anon (t);
3531
3532   /* We've built up the list of access declarations in reverse order.
3533      Fix that now.  */
3534   *access_decls = nreverse (*access_decls);
3535 }
3536
3537 /* If TYPE is an empty class type, records its OFFSET in the table of
3538    OFFSETS.  */
3539
3540 static int
3541 record_subobject_offset (type, offset, offsets)
3542      tree type;
3543      tree offset;
3544      splay_tree offsets;
3545 {
3546   splay_tree_node n;
3547
3548   if (!is_empty_class (type))
3549     return 0;
3550
3551   /* Record the location of this empty object in OFFSETS.  */
3552   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3553   if (!n)
3554     n = splay_tree_insert (offsets, 
3555                            (splay_tree_key) offset,
3556                            (splay_tree_value) NULL_TREE);
3557   n->value = ((splay_tree_value) 
3558               tree_cons (NULL_TREE,
3559                          type,
3560                          (tree) n->value));
3561
3562   return 0;
3563 }
3564
3565 /* Returns non-zero if TYPE is an empty class type and there is
3566    already an entry in OFFSETS for the same TYPE as the same OFFSET.  */
3567
3568 static int
3569 check_subobject_offset (type, offset, offsets)
3570      tree type;
3571      tree offset;
3572      splay_tree offsets;
3573 {
3574   splay_tree_node n;
3575   tree t;
3576
3577   if (!is_empty_class (type))
3578     return 0;
3579
3580   /* Record the location of this empty object in OFFSETS.  */
3581   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
3582   if (!n)
3583     return 0;
3584
3585   for (t = (tree) n->value; t; t = TREE_CHAIN (t))
3586     if (same_type_p (TREE_VALUE (t), type))
3587       return 1;
3588
3589   return 0;
3590 }
3591
3592 /* Walk through all the subobjects of TYPE (located at OFFSET).  Call
3593    F for every subobject, passing it the type, offset, and table of
3594    OFFSETS.  If VBASES_P is non-zero, then even virtual non-primary
3595    bases should be traversed; otherwise, they are ignored.  
3596
3597    If MAX_OFFSET is non-NULL, then subobjects with an offset greater
3598    than MAX_OFFSET will not be walked.
3599
3600    If F returns a non-zero value, the traversal ceases, and that value
3601    is returned.  Otherwise, returns zero.  */
3602
3603 static int
3604 walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
3605      tree type;
3606      subobject_offset_fn f;
3607      tree offset;
3608      splay_tree offsets;
3609      tree max_offset;
3610      int vbases_p;
3611 {
3612   int r = 0;
3613
3614   /* If this OFFSET is bigger than the MAX_OFFSET, then we should
3615      stop.  */
3616   if (max_offset && INT_CST_LT (max_offset, offset))
3617     return 0;
3618
3619   if (CLASS_TYPE_P (type))
3620     {
3621       tree field;
3622       int i;
3623
3624       /* Record the location of TYPE.  */
3625       r = (*f) (type, offset, offsets);
3626       if (r)
3627         return r;
3628
3629       /* Iterate through the direct base classes of TYPE.  */
3630       for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
3631         {
3632           tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
3633
3634           if (!vbases_p 
3635               && TREE_VIA_VIRTUAL (binfo) 
3636               && !BINFO_PRIMARY_P (binfo))
3637             continue;
3638
3639           r = walk_subobject_offsets (BINFO_TYPE (binfo),
3640                                       f,
3641                                       size_binop (PLUS_EXPR,
3642                                                   offset,
3643                                                   BINFO_OFFSET (binfo)),
3644                                       offsets,
3645                                       max_offset,
3646                                       vbases_p);
3647           if (r)
3648             return r;
3649         }
3650
3651       /* Iterate through the fields of TYPE.  */
3652       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
3653         if (TREE_CODE (field) == FIELD_DECL)
3654           {
3655             r = walk_subobject_offsets (TREE_TYPE (field),
3656                                         f,
3657                                         size_binop (PLUS_EXPR,
3658                                                     offset,
3659                                                     DECL_FIELD_OFFSET (field)),
3660                                         offsets,
3661                                         max_offset,
3662                                         /*vbases_p=*/1);
3663             if (r)
3664               return r;
3665           }
3666     }
3667   else if (TREE_CODE (type) == ARRAY_TYPE)
3668     {
3669       tree domain = TYPE_DOMAIN (type);
3670       tree index;
3671
3672       /* Step through each of the elements in the array.  */
3673       for (index = size_zero_node; 
3674            INT_CST_LT (index, TYPE_MAX_VALUE (domain));
3675            index = size_binop (PLUS_EXPR, index, size_one_node))
3676         {
3677           r = walk_subobject_offsets (TREE_TYPE (type),
3678                                       f,
3679                                       offset,
3680                                       offsets,
3681                                       max_offset,
3682                                       /*vbases_p=*/1);
3683           if (r)
3684             return r;
3685           offset = size_binop (PLUS_EXPR, offset, 
3686                                TYPE_SIZE_UNIT (TREE_TYPE (type)));
3687           /* If this new OFFSET is bigger than the MAX_OFFSET, then
3688              there's no point in iterating through the remaining
3689              elements of the array.  */
3690           if (max_offset && INT_CST_LT (max_offset, offset))
3691             break;
3692         }
3693     }
3694
3695   return 0;
3696 }
3697
3698 /* Record all of the empty subobjects of TYPE (located at OFFSET) in
3699    OFFSETS.  If VBASES_P is non-zero, virtual bases of TYPE are
3700    examined.  */
3701
3702 static void
3703 record_subobject_offsets (type, offset, offsets, vbases_p)
3704      tree type;
3705      tree offset;
3706      splay_tree offsets;
3707      int vbases_p;
3708 {
3709   walk_subobject_offsets (type, record_subobject_offset, offset,
3710                           offsets, /*max_offset=*/NULL_TREE, vbases_p);
3711 }
3712
3713 /* Returns non-zero if any of the empty subobjects of TYPE (located at
3714    OFFSET) conflict with entries in OFFSETS.  If VBASES_P is non-zero,
3715    virtual bases of TYPE are examined.  */
3716
3717 static int
3718 layout_conflict_p (type, offset, offsets, vbases_p)
3719      tree type;
3720      tree offset;
3721      splay_tree offsets;
3722      int vbases_p;
3723 {
3724   splay_tree_node max_node;
3725
3726   /* Get the node in OFFSETS that indicates the maximum offset where
3727      an empty subobject is located.  */
3728   max_node = splay_tree_max (offsets);
3729   /* If there aren't any empty subobjects, then there's no point in
3730      performing this check.  */
3731   if (!max_node)
3732     return 0;
3733
3734   return walk_subobject_offsets (type, check_subobject_offset, offset,
3735                                  offsets, (tree) (max_node->key),
3736                                  vbases_p);
3737 }
3738
3739 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
3740    non-static data member of the type indicated by RLI.  BINFO is the
3741    binfo corresponding to the base subobject, OFFSETS maps offsets to
3742    types already located at those offsets.  T is the most derived
3743    type.  This function determines the position of the DECL.  */
3744
3745 static void
3746 layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
3747      record_layout_info rli;
3748      tree decl;
3749      tree binfo;
3750      splay_tree offsets;
3751      tree t;
3752 {
3753   tree offset = NULL_TREE;
3754   tree type = TREE_TYPE (decl);
3755   /* If we are laying out a base class, rather than a field, then
3756      DECL_ARTIFICIAL will be set on the FIELD_DECL.  */
3757   int field_p = !DECL_ARTIFICIAL (decl);
3758
3759   /* Try to place the field.  It may take more than one try if we have
3760      a hard time placing the field without putting two objects of the
3761      same type at the same address.  */
3762   while (1)
3763     {
3764       struct record_layout_info_s old_rli = *rli;
3765
3766       /* Place this field.  */
3767       place_field (rli, decl);
3768       offset = byte_position (decl);
3769  
3770       /* We have to check to see whether or not there is already
3771          something of the same type at the offset we're about to use.
3772          For example:
3773          
3774          struct S {};
3775          struct T : public S { int i; };
3776          struct U : public S, public T {};
3777          
3778          Here, we put S at offset zero in U.  Then, we can't put T at
3779          offset zero -- its S component would be at the same address
3780          as the S we already allocated.  So, we have to skip ahead.
3781          Since all data members, including those whose type is an
3782          empty class, have non-zero size, any overlap can happen only
3783          with a direct or indirect base-class -- it can't happen with
3784          a data member.  */
3785       if (layout_conflict_p (TREE_TYPE (decl),
3786                              offset,
3787                              offsets, 
3788                              field_p))
3789         {
3790           /* Strip off the size allocated to this field.  That puts us
3791              at the first place we could have put the field with
3792              proper alignment.  */
3793           *rli = old_rli;
3794
3795           /* Bump up by the alignment required for the type.  */
3796           rli->bitpos
3797             = size_binop (PLUS_EXPR, rli->bitpos, 
3798                           bitsize_int (binfo 
3799                                        ? CLASSTYPE_ALIGN (type)
3800                                        : TYPE_ALIGN (type)));
3801           normalize_rli (rli);
3802         }
3803       else
3804         /* There was no conflict.  We're done laying out this field.  */
3805         break;
3806     }
3807
3808   /* Now that we know where it will be placed, update its
3809      BINFO_OFFSET.  */
3810   if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
3811     propagate_binfo_offsets (binfo, 
3812                              convert (ssizetype, offset), t);
3813 }
3814
3815 /* Layout the empty base BINFO.  EOC indicates the byte currently just
3816    past the end of the class, and should be correctly aligned for a
3817    class of the type indicated by BINFO; OFFSETS gives the offsets of
3818    the empty bases allocated so far. T is the most derived
3819    type.  Return non-zero iff we added it at the end. */
3820
3821 static bool
3822 layout_empty_base (binfo, eoc, offsets, t)
3823      tree binfo;
3824      tree eoc;
3825      splay_tree offsets;
3826      tree t;
3827 {
3828   tree alignment;
3829   tree basetype = BINFO_TYPE (binfo);
3830   bool atend = false;
3831   
3832   /* This routine should only be used for empty classes.  */
3833   my_friendly_assert (is_empty_class (basetype), 20000321);
3834   alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
3835
3836   /* This is an empty base class.  We first try to put it at offset
3837      zero.  */
3838   if (layout_conflict_p (BINFO_TYPE (binfo),
3839                          BINFO_OFFSET (binfo),
3840                          offsets, 
3841                          /*vbases_p=*/0))
3842     {
3843       /* That didn't work.  Now, we move forward from the next
3844          available spot in the class.  */
3845       atend = true;
3846       propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
3847       while (1) 
3848         {
3849           if (!layout_conflict_p (BINFO_TYPE (binfo),
3850                                   BINFO_OFFSET (binfo), 
3851                                   offsets,
3852                                   /*vbases_p=*/0))
3853             /* We finally found a spot where there's no overlap.  */
3854             break;
3855
3856           /* There's overlap here, too.  Bump along to the next spot.  */
3857           propagate_binfo_offsets (binfo, alignment, t);
3858         }
3859     }
3860   return atend;
3861 }
3862
3863 /* Build a FIELD_DECL for the base given by BINFO in the class
3864    indicated by RLI.  If the new object is non-empty, clear *EMPTY_P.
3865    *BASE_ALIGN is a running maximum of the alignments of any base
3866    class.  OFFSETS gives the location of empty base subobjects.  T is
3867    the most derived type.  Return non-zero if the new object cannot be
3868    nearly-empty. */
3869
3870 static bool
3871 build_base_field (rli, binfo, empty_p, offsets, t)
3872      record_layout_info rli;
3873      tree binfo;
3874      int *empty_p;
3875      splay_tree offsets;
3876      tree t;
3877 {
3878   tree basetype = BINFO_TYPE (binfo);
3879   tree decl;
3880   bool atend = false;
3881
3882   if (!COMPLETE_TYPE_P (basetype))
3883     /* This error is now reported in xref_tag, thus giving better
3884        location information.  */
3885     return atend;
3886   
3887   decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
3888   DECL_ARTIFICIAL (decl) = 1;
3889   DECL_FIELD_CONTEXT (decl) = rli->t;
3890   DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
3891   DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
3892   DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
3893   DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
3894   
3895   if (!integer_zerop (DECL_SIZE (decl)))
3896     {
3897       /* The containing class is non-empty because it has a non-empty
3898          base class.  */
3899       *empty_p = 0;
3900
3901       /* Try to place the field.  It may take more than one try if we
3902          have a hard time placing the field without putting two
3903          objects of the same type at the same address.  */
3904       layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
3905     }
3906   else
3907     {
3908       unsigned HOST_WIDE_INT eoc;
3909
3910       /* On some platforms (ARM), even empty classes will not be
3911          byte-aligned.  */
3912       eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
3913       eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
3914       atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
3915     }
3916
3917   /* Record the offsets of BINFO and its base subobjects.  */
3918   record_subobject_offsets (BINFO_TYPE (binfo), 
3919                             BINFO_OFFSET (binfo),
3920                             offsets, 
3921                             /*vbases_p=*/0);
3922   return atend;
3923 }
3924
3925 /* Layout all of the non-virtual base classes.  Record empty
3926    subobjects in OFFSETS.  T is the most derived type.  Return
3927    non-zero if the type cannot be nearly empty.  */
3928
3929 static bool
3930 build_base_fields (rli, empty_p, offsets, t)
3931      record_layout_info rli;
3932      int *empty_p;
3933      splay_tree offsets;
3934      tree t;
3935 {
3936   /* Chain to hold all the new FIELD_DECLs which stand in for base class
3937      subobjects.  */
3938   tree rec = rli->t;
3939   int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
3940   int i;
3941   bool atend = 0;
3942
3943   /* The primary base class is always allocated first.  */
3944   if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
3945     build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec), 
3946                       empty_p, offsets, t);
3947
3948   /* Now allocate the rest of the bases.  */
3949   for (i = 0; i < n_baseclasses; ++i)
3950     {
3951       tree base_binfo;
3952
3953       base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
3954
3955       /* The primary base was already allocated above, so we don't
3956          need to allocate it again here.  */
3957       if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
3958         continue;
3959
3960       /* A primary virtual base class is allocated just like any other
3961          base class, but a non-primary virtual base is allocated
3962          later, in layout_virtual_bases.  */
3963       if (TREE_VIA_VIRTUAL (base_binfo) 
3964           && !BINFO_PRIMARY_P (base_binfo))
3965         continue;
3966
3967       atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
3968     }
3969   return atend;
3970 }
3971
3972 /* Go through the TYPE_METHODS of T issuing any appropriate
3973    diagnostics, figuring out which methods override which other
3974    methods, and so forth.  */
3975
3976 static void
3977 check_methods (t)
3978      tree t;
3979 {
3980   tree x;
3981
3982   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
3983     {
3984       GNU_xref_member (current_class_name, x);
3985
3986       /* If this was an evil function, don't keep it in class.  */
3987       if (DECL_ASSEMBLER_NAME_SET_P (x) 
3988           && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
3989         continue;
3990
3991       check_for_override (x, t);
3992       if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
3993         cp_error_at ("initializer specified for non-virtual method `%D'", x);
3994
3995       /* The name of the field is the original field name
3996          Save this in auxiliary field for later overloading.  */
3997       if (DECL_VINDEX (x))
3998         {
3999           TYPE_POLYMORPHIC_P (t) = 1;
4000           if (DECL_PURE_VIRTUAL_P (x))
4001             CLASSTYPE_PURE_VIRTUALS (t)
4002               = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
4003         }
4004     }
4005 }
4006
4007 /* FN is a constructor or destructor.  Clone the declaration to create
4008    a specialized in-charge or not-in-charge version, as indicated by
4009    NAME.  */
4010
4011 static tree
4012 build_clone (fn, name)
4013      tree fn;
4014      tree name;
4015 {
4016   tree parms;
4017   tree clone;
4018
4019   /* Copy the function.  */
4020   clone = copy_decl (fn);
4021   /* Remember where this function came from.  */
4022   DECL_CLONED_FUNCTION (clone) = fn;
4023   DECL_ABSTRACT_ORIGIN (clone) = fn;
4024   /* Reset the function name.  */
4025   DECL_NAME (clone) = name;
4026   SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
4027   /* There's no pending inline data for this function.  */
4028   DECL_PENDING_INLINE_INFO (clone) = NULL;
4029   DECL_PENDING_INLINE_P (clone) = 0;
4030   /* And it hasn't yet been deferred.  */
4031   DECL_DEFERRED_FN (clone) = 0;
4032
4033   /* The base-class destructor is not virtual.  */
4034   if (name == base_dtor_identifier)
4035     {
4036       DECL_VIRTUAL_P (clone) = 0;
4037       if (TREE_CODE (clone) != TEMPLATE_DECL)
4038         DECL_VINDEX (clone) = NULL_TREE;
4039     }
4040
4041   /* If there was an in-charge parameter, drop it from the function
4042      type.  */
4043   if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4044     {
4045       tree basetype;
4046       tree parmtypes;
4047       tree exceptions;
4048
4049       exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4050       basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4051       parmtypes = TYPE_ARG_TYPES (TREE_TYPE (clone));
4052       /* Skip the `this' parameter.  */
4053       parmtypes = TREE_CHAIN (parmtypes);
4054       /* Skip the in-charge parameter.  */
4055       parmtypes = TREE_CHAIN (parmtypes);
4056       /* And the VTT parm, in a complete [cd]tor.  */
4057       if (DECL_HAS_VTT_PARM_P (fn)
4058           && ! DECL_NEEDS_VTT_PARM_P (clone))
4059         parmtypes = TREE_CHAIN (parmtypes);
4060        /* If this is subobject constructor or destructor, add the vtt
4061          parameter.  */
4062       TREE_TYPE (clone) 
4063         = build_cplus_method_type (basetype,
4064                                    TREE_TYPE (TREE_TYPE (clone)),
4065                                    parmtypes);
4066       if (exceptions)
4067         TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
4068                                                      exceptions);
4069     }
4070
4071   /* Copy the function parameters.  But, DECL_ARGUMENTS on a TEMPLATE_DECL
4072      aren't function parameters; those are the template parameters.  */
4073   if (TREE_CODE (clone) != TEMPLATE_DECL)
4074     {
4075       DECL_ARGUMENTS (clone) = copy_list (DECL_ARGUMENTS (clone));
4076       /* Remove the in-charge parameter.  */
4077       if (DECL_HAS_IN_CHARGE_PARM_P (clone))
4078         {
4079           TREE_CHAIN (DECL_ARGUMENTS (clone))
4080             = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4081           DECL_HAS_IN_CHARGE_PARM_P (clone) = 0;
4082         }
4083       /* And the VTT parm, in a complete [cd]tor.  */
4084       if (DECL_HAS_VTT_PARM_P (fn))
4085         {
4086           if (DECL_NEEDS_VTT_PARM_P (clone))
4087             DECL_HAS_VTT_PARM_P (clone) = 1;
4088           else
4089             {
4090               TREE_CHAIN (DECL_ARGUMENTS (clone))
4091                 = TREE_CHAIN (TREE_CHAIN (DECL_ARGUMENTS (clone)));
4092               DECL_HAS_VTT_PARM_P (clone) = 0;
4093             }
4094         }
4095
4096       for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
4097         {
4098           DECL_CONTEXT (parms) = clone;
4099           copy_lang_decl (parms);
4100         }
4101     }
4102
4103   /* Create the RTL for this function.  */
4104   SET_DECL_RTL (clone, NULL_RTX);
4105   rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
4106   
4107   /* Make it easy to find the CLONE given the FN.  */
4108   TREE_CHAIN (clone) = TREE_CHAIN (fn);
4109   TREE_CHAIN (fn) = clone;
4110
4111   /* If this is a template, handle the DECL_TEMPLATE_RESULT as well.  */
4112   if (TREE_CODE (clone) == TEMPLATE_DECL)
4113     {
4114       tree result;
4115
4116       DECL_TEMPLATE_RESULT (clone) 
4117         = build_clone (DECL_TEMPLATE_RESULT (clone), name);
4118       result = DECL_TEMPLATE_RESULT (clone);
4119       DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
4120       DECL_TI_TEMPLATE (result) = clone;
4121     }
4122   else if (DECL_DEFERRED_FN (fn))
4123     defer_fn (clone);
4124
4125   return clone;
4126 }
4127
4128 /* Produce declarations for all appropriate clones of FN.  If
4129    UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
4130    CLASTYPE_METHOD_VEC as well.  */
4131
4132 void
4133 clone_function_decl (fn, update_method_vec_p)
4134      tree fn;
4135      int update_method_vec_p;
4136 {
4137   tree clone;
4138
4139   /* Avoid inappropriate cloning.  */
4140   if (TREE_CHAIN (fn)
4141       && DECL_CLONED_FUNCTION (TREE_CHAIN (fn)))
4142     return;
4143
4144   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (fn))
4145     {
4146       /* For each constructor, we need two variants: an in-charge version
4147          and a not-in-charge version.  */
4148       clone = build_clone (fn, complete_ctor_identifier);
4149       if (update_method_vec_p)
4150         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4151       clone = build_clone (fn, base_ctor_identifier);
4152       if (update_method_vec_p)
4153         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4154     }
4155   else
4156     {
4157       my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
4158
4159       /* For each destructor, we need three variants: an in-charge
4160          version, a not-in-charge version, and an in-charge deleting
4161          version.  We clone the deleting version first because that
4162          means it will go second on the TYPE_METHODS list -- and that
4163          corresponds to the correct layout order in the virtual
4164          function table.  
4165
4166          For a non-virtual destructor, we do not build a deleting
4167          destructor.  */
4168       if (DECL_VIRTUAL_P (fn))
4169         {
4170           clone = build_clone (fn, deleting_dtor_identifier);
4171           if (update_method_vec_p)
4172             add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4173         }
4174       clone = build_clone (fn, complete_dtor_identifier);
4175       if (update_method_vec_p)
4176         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4177       clone = build_clone (fn, base_dtor_identifier);
4178       if (update_method_vec_p)
4179         add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
4180     }
4181
4182   /* Note that this is an abstract function that is never emitted.  */
4183   DECL_ABSTRACT (fn) = 1;
4184 }
4185
4186 /* DECL is an in charge constructor, which is being defined. This will
4187    have had an in class declaration, from whence clones were
4188    declared. An out-of-class definition can specify additional default
4189    arguments. As it is the clones that are involved in overload
4190    resolution, we must propagate the information from the DECL to its
4191    clones. */
4192
4193 void
4194 adjust_clone_args (decl)
4195      tree decl;
4196 {
4197   tree clone;
4198   
4199   for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
4200        clone = TREE_CHAIN (clone))
4201     {
4202       tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
4203       tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
4204       tree decl_parms, clone_parms;
4205
4206       clone_parms = orig_clone_parms;
4207       
4208       /* Skip the 'this' parameter. */
4209       orig_clone_parms = TREE_CHAIN (orig_clone_parms);
4210       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4211
4212       if (DECL_HAS_IN_CHARGE_PARM_P (decl))
4213         orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4214       if (DECL_HAS_VTT_PARM_P (decl))
4215         orig_decl_parms = TREE_CHAIN (orig_decl_parms);
4216       
4217       clone_parms = orig_clone_parms;
4218       if (DECL_HAS_VTT_PARM_P (clone))
4219         clone_parms = TREE_CHAIN (clone_parms);
4220       
4221       for (decl_parms = orig_decl_parms; decl_parms;
4222            decl_parms = TREE_CHAIN (decl_parms),
4223              clone_parms = TREE_CHAIN (clone_parms))
4224         {
4225           my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
4226                                            TREE_TYPE (clone_parms)), 20010424);
4227           
4228           if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
4229             {
4230               /* A default parameter has been added. Adjust the
4231                  clone's parameters. */
4232               tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
4233               tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
4234               tree type;
4235
4236               clone_parms = orig_decl_parms;
4237
4238               if (DECL_HAS_VTT_PARM_P (clone))
4239                 {
4240                   clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
4241                                            TREE_VALUE (orig_clone_parms),
4242                                            clone_parms);
4243                   TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
4244                 }
4245               type = build_cplus_method_type (basetype,
4246                                               TREE_TYPE (TREE_TYPE (clone)),
4247                                               clone_parms);
4248               if (exceptions)
4249                 type = build_exception_variant (type, exceptions);
4250               TREE_TYPE (clone) = type;
4251               
4252               clone_parms = NULL_TREE;
4253               break;
4254             }
4255         }
4256       my_friendly_assert (!clone_parms, 20010424);
4257     }
4258 }
4259
4260 /* For each of the constructors and destructors in T, create an
4261    in-charge and not-in-charge variant.  */
4262
4263 static void
4264 clone_constructors_and_destructors (t)
4265      tree t;
4266 {
4267   tree fns;
4268
4269   /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
4270      out now.  */
4271   if (!CLASSTYPE_METHOD_VEC (t))
4272     return;
4273
4274   for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4275     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4276   for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
4277     clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
4278 }
4279
4280 /* Remove all zero-width bit-fields from T.  */
4281
4282 static void
4283 remove_zero_width_bit_fields (t)
4284      tree t;
4285 {
4286   tree *fieldsp;
4287
4288   fieldsp = &TYPE_FIELDS (t); 
4289   while (*fieldsp)
4290     {
4291       if (TREE_CODE (*fieldsp) == FIELD_DECL
4292           && DECL_C_BIT_FIELD (*fieldsp) 
4293           && DECL_INITIAL (*fieldsp))
4294         *fieldsp = TREE_CHAIN (*fieldsp);
4295       else
4296         fieldsp = &TREE_CHAIN (*fieldsp);
4297     }
4298 }
4299
4300 /* Returns TRUE iff we need a cookie when dynamically allocating an
4301    array whose elements have the indicated class TYPE.  */
4302
4303 static bool
4304 type_requires_array_cookie (type)
4305      tree type;
4306 {
4307   tree fns;
4308   bool has_two_argument_delete_p = false;
4309
4310   my_friendly_assert (CLASS_TYPE_P (type), 20010712);
4311
4312   /* If there's a non-trivial destructor, we need a cookie.  In order
4313      to iterate through the array calling the destructor for each
4314      element, we'll have to know how many elements there are.  */
4315   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
4316     return true;
4317
4318   /* If the usual deallocation function is a two-argument whose second
4319      argument is of type `size_t', then we have to pass the size of
4320      the array to the deallocation function, so we will need to store
4321      a cookie.  */
4322   fns = lookup_fnfields (TYPE_BINFO (type), 
4323                          ansi_opname (VEC_DELETE_EXPR),
4324                          /*protect=*/0);
4325   /* If there are no `operator []' members, or the lookup is
4326      ambiguous, then we don't need a cookie.  */
4327   if (!fns || fns == error_mark_node)
4328     return false;
4329   /* Loop through all of the functions.  */
4330   for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
4331     {
4332       tree fn;
4333       tree second_parm;
4334
4335       /* Select the current function.  */
4336       fn = OVL_CURRENT (fns);
4337       /* See if this function is a one-argument delete function.  If
4338          it is, then it will be the usual deallocation function.  */
4339       second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
4340       if (second_parm == void_list_node)
4341         return false;
4342       /* Otherwise, if we have a two-argument function and the second
4343          argument is `size_t', it will be the usual deallocation
4344          function -- unless there is one-argument function, too.  */
4345       if (TREE_CHAIN (second_parm) == void_list_node
4346           && same_type_p (TREE_VALUE (second_parm), sizetype))
4347         has_two_argument_delete_p = true;
4348     }
4349
4350   return has_two_argument_delete_p;
4351 }
4352
4353 /* Check the validity of the bases and members declared in T.  Add any
4354    implicitly-generated functions (like copy-constructors and
4355    assignment operators).  Compute various flag bits (like
4356    CLASSTYPE_NON_POD_T) for T.  This routine works purely at the C++
4357    level: i.e., independently of the ABI in use.  */
4358
4359 static void
4360 check_bases_and_members (t, empty_p)
4361      tree t;
4362      int *empty_p;
4363 {
4364   /* Nonzero if we are not allowed to generate a default constructor
4365      for this case.  */
4366   int cant_have_default_ctor;
4367   /* Nonzero if the implicitly generated copy constructor should take
4368      a non-const reference argument.  */
4369   int cant_have_const_ctor;
4370   /* Nonzero if the the implicitly generated assignment operator
4371      should take a non-const reference argument.  */
4372   int no_const_asn_ref;
4373   tree access_decls;
4374
4375   /* By default, we use const reference arguments and generate default
4376      constructors.  */
4377   cant_have_default_ctor = 0;
4378   cant_have_const_ctor = 0;
4379   no_const_asn_ref = 0;
4380
4381   /* Assume that the class is nearly empty; we'll clear this flag if
4382      it turns out not to be nearly empty.  */
4383   CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
4384
4385   /* Check all the base-classes. */
4386   check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
4387                &no_const_asn_ref);
4388
4389   /* Check all the data member declarations.  */
4390   check_field_decls (t, &access_decls, empty_p,
4391                      &cant_have_default_ctor,
4392                      &cant_have_const_ctor,
4393                      &no_const_asn_ref);
4394
4395   /* Check all the method declarations.  */
4396   check_methods (t);
4397
4398   /* A nearly-empty class has to be vptr-containing; a nearly empty
4399      class contains just a vptr.  */
4400   if (!TYPE_CONTAINS_VPTR_P (t))
4401     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4402
4403   /* Do some bookkeeping that will guide the generation of implicitly
4404      declared member functions.  */
4405   TYPE_HAS_COMPLEX_INIT_REF (t)
4406     |= (TYPE_HAS_INIT_REF (t) 
4407         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4408         || TYPE_POLYMORPHIC_P (t));
4409   TYPE_NEEDS_CONSTRUCTING (t)
4410     |= (TYPE_HAS_CONSTRUCTOR (t) 
4411         || TYPE_USES_VIRTUAL_BASECLASSES (t)
4412         || TYPE_POLYMORPHIC_P (t));
4413   CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
4414                                   || TYPE_POLYMORPHIC_P (t));
4415   CLASSTYPE_NON_POD_P (t)
4416     |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t) 
4417         || TYPE_HAS_ASSIGN_REF (t));
4418   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
4419   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
4420     |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
4421
4422   /* Synthesize any needed methods.  Note that methods will be synthesized
4423      for anonymous unions; grok_x_components undoes that.  */
4424   add_implicitly_declared_members (t, cant_have_default_ctor,
4425                                    cant_have_const_ctor,
4426                                    no_const_asn_ref);
4427
4428   /* Create the in-charge and not-in-charge variants of constructors
4429      and destructors.  */
4430   clone_constructors_and_destructors (t);
4431
4432   /* Process the using-declarations.  */
4433   for (; access_decls; access_decls = TREE_CHAIN (access_decls))
4434     handle_using_decl (TREE_VALUE (access_decls), t);
4435
4436   /* Build and sort the CLASSTYPE_METHOD_VEC.  */
4437   finish_struct_methods (t);
4438
4439   /* Figure out whether or not we will need a cookie when dynamically
4440      allocating an array of this type.  */
4441   TYPE_LANG_SPECIFIC (t)->vec_new_uses_cookie
4442     = type_requires_array_cookie (t);
4443 }
4444
4445 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
4446    accordingly.  If a new vfield was created (because T doesn't have a
4447    primary base class), then the newly created field is returned.  It
4448    is not added to the TYPE_FIELDS list; it is the caller's
4449    responsibility to do that.  */
4450
4451 static tree
4452 create_vtable_ptr (t, empty_p, vfuns_p,
4453                    new_virtuals_p, overridden_virtuals_p)
4454      tree t;
4455      int *empty_p;
4456      int *vfuns_p;
4457      tree *new_virtuals_p;
4458      tree *overridden_virtuals_p;
4459 {
4460   tree fn;
4461
4462   /* Loop over the virtual functions, adding them to our various
4463      vtables.  */
4464   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
4465     if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
4466       add_virtual_function (new_virtuals_p, overridden_virtuals_p,
4467                             vfuns_p, fn, t);
4468
4469   /* If we couldn't find an appropriate base class, create a new field
4470      here.  Even if there weren't any new virtual functions, we might need a
4471      new virtual function table if we're supposed to include vptrs in
4472      all classes that need them.  */
4473   if (!TYPE_VFIELD (t) && (*vfuns_p || TYPE_CONTAINS_VPTR_P (t)))
4474     {
4475       /* We build this decl with vtbl_ptr_type_node, which is a
4476          `vtable_entry_type*'.  It might seem more precise to use
4477          `vtable_entry_type (*)[N]' where N is the number of firtual
4478          functions.  However, that would require the vtable pointer in
4479          base classes to have a different type than the vtable pointer
4480          in derived classes.  We could make that happen, but that
4481          still wouldn't solve all the problems.  In particular, the
4482          type-based alias analysis code would decide that assignments
4483          to the base class vtable pointer can't alias assignments to
4484          the derived class vtable pointer, since they have different
4485          types.  Thus, in an derived class destructor, where the base
4486          class constructor was inlined, we could generate bad code for
4487          setting up the vtable pointer.  
4488
4489          Therefore, we use one type for all vtable pointers.  We still
4490          use a type-correct type; it's just doesn't indicate the array
4491          bounds.  That's better than using `void*' or some such; it's
4492          cleaner, and it let's the alias analysis code know that these
4493          stores cannot alias stores to void*!  */
4494       tree field;
4495
4496       field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
4497       SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
4498       DECL_VIRTUAL_P (field) = 1;
4499       DECL_ARTIFICIAL (field) = 1;
4500       DECL_FIELD_CONTEXT (field) = t;
4501       DECL_FCONTEXT (field) = t;
4502       DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
4503       DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
4504       
4505       TYPE_VFIELD (t) = field;
4506       
4507       /* This class is non-empty.  */
4508       *empty_p = 0;
4509
4510       if (CLASSTYPE_N_BASECLASSES (t))
4511         /* If there were any baseclasses, they can't possibly be at
4512            offset zero any more, because that's where the vtable
4513            pointer is.  So, converting to a base class is going to
4514            take work.  */
4515         TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
4516
4517       return field;
4518     }
4519
4520   return NULL_TREE;
4521 }
4522
4523 /* Fixup the inline function given by INFO now that the class is
4524    complete.  */
4525
4526 static void
4527 fixup_pending_inline (fn)
4528      tree fn;
4529 {
4530   if (DECL_PENDING_INLINE_INFO (fn))
4531     {
4532       tree args = DECL_ARGUMENTS (fn);
4533       while (args)
4534         {
4535           DECL_CONTEXT (args) = fn;
4536           args = TREE_CHAIN (args);
4537         }
4538     }
4539 }
4540
4541 /* Fixup the inline methods and friends in TYPE now that TYPE is
4542    complete.  */
4543
4544 static void
4545 fixup_inline_methods (type)
4546      tree type;
4547 {
4548   tree method = TYPE_METHODS (type);
4549
4550   if (method && TREE_CODE (method) == TREE_VEC)
4551     {
4552       if (TREE_VEC_ELT (method, 1))
4553         method = TREE_VEC_ELT (method, 1);
4554       else if (TREE_VEC_ELT (method, 0))
4555         method = TREE_VEC_ELT (method, 0);
4556       else
4557         method = TREE_VEC_ELT (method, 2);
4558     }
4559
4560   /* Do inline member functions.  */
4561   for (; method; method = TREE_CHAIN (method))
4562     fixup_pending_inline (method);
4563
4564   /* Do friends.  */
4565   for (method = CLASSTYPE_INLINE_FRIENDS (type); 
4566        method; 
4567        method = TREE_CHAIN (method))
4568     fixup_pending_inline (TREE_VALUE (method));
4569   CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
4570 }
4571
4572 /* Add OFFSET to all base types of BINFO which is a base in the
4573    hierarchy dominated by T.
4574
4575    OFFSET, which is a type offset, is number of bytes.  */
4576
4577 static void
4578 propagate_binfo_offsets (binfo, offset, t)
4579      tree binfo;
4580      tree offset;
4581      tree t;
4582 {
4583   int i;
4584   tree primary_binfo;
4585
4586   /* Update BINFO's offset.  */
4587   BINFO_OFFSET (binfo)
4588     = convert (sizetype, 
4589                size_binop (PLUS_EXPR,
4590                            convert (ssizetype, BINFO_OFFSET (binfo)),
4591                            offset));
4592
4593   /* Find the primary base class.  */
4594   primary_binfo = get_primary_binfo (binfo);
4595
4596   /* Scan all of the bases, pushing the BINFO_OFFSET adjust
4597      downwards.  */
4598   for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
4599     {
4600       tree base_binfo;
4601
4602       /* On the first time through the loop, do the primary base.
4603          Because the primary base need not be an immediate base, we
4604          must handle the primary base specially.  */
4605       if (i == -1) 
4606         {
4607           if (!primary_binfo) 
4608             continue;
4609
4610           base_binfo = primary_binfo;
4611         }
4612       else
4613         {
4614           base_binfo = BINFO_BASETYPE (binfo, i);
4615           /* Don't do the primary base twice.  */
4616           if (base_binfo == primary_binfo)
4617             continue;
4618         }
4619
4620       /* Skip virtual bases that aren't our canonical primary base.  */
4621       if (TREE_VIA_VIRTUAL (base_binfo)
4622           && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
4623               || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
4624         continue;
4625
4626       propagate_binfo_offsets (base_binfo, offset, t);
4627     }
4628 }
4629
4630 /* Called via dfs_walk from layout_virtual bases.  */
4631
4632 static tree
4633 dfs_set_offset_for_unshared_vbases (binfo, data)
4634      tree binfo;
4635      void *data;
4636 {
4637   /* If this is a virtual base, make sure it has the same offset as
4638      the shared copy.  If it's a primary base, then we know it's
4639      correct.  */
4640   if (TREE_VIA_VIRTUAL (binfo))
4641     {
4642       tree t = (tree) data;
4643       tree vbase;
4644       tree offset;
4645       
4646       vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
4647       if (vbase != binfo)
4648         {
4649           offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
4650           propagate_binfo_offsets (binfo, offset, t);
4651         }
4652     }
4653
4654   return NULL_TREE;
4655 }
4656
4657 /* Set BINFO_OFFSET for all of the virtual bases for T.  Update
4658    TYPE_ALIGN and TYPE_SIZE for T.  OFFSETS gives the location of
4659    empty subobjects of T.  */
4660
4661 static void
4662 layout_virtual_bases (t, offsets)
4663      tree t;
4664      splay_tree offsets;
4665 {
4666   tree vbases;
4667   unsigned HOST_WIDE_INT dsize;
4668   unsigned HOST_WIDE_INT eoc;
4669
4670   if (CLASSTYPE_N_BASECLASSES (t) == 0)
4671     return;
4672
4673 #ifdef STRUCTURE_SIZE_BOUNDARY
4674   /* Packed structures don't need to have minimum size.  */
4675   if (! TYPE_PACKED (t))
4676     TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
4677 #endif
4678
4679   /* DSIZE is the size of the class without the virtual bases.  */
4680   dsize = tree_low_cst (TYPE_SIZE (t), 1);
4681
4682   /* Make every class have alignment of at least one.  */
4683   TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
4684
4685   /* Go through the virtual bases, allocating space for each virtual
4686      base that is not already a primary base class.  These are
4687      allocated in inheritance graph order.  */
4688   for (vbases = TYPE_BINFO (t);
4689        vbases; 
4690        vbases = TREE_CHAIN (vbases))
4691     {
4692       tree vbase;
4693
4694       if (!TREE_VIA_VIRTUAL (vbases))
4695         continue;
4696       vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
4697
4698       if (!BINFO_PRIMARY_P (vbase))
4699         {
4700           /* This virtual base is not a primary base of any class in the
4701              hierarchy, so we have to add space for it.  */
4702           tree basetype;
4703           unsigned int desired_align;
4704
4705           basetype = BINFO_TYPE (vbase);
4706
4707           desired_align = CLASSTYPE_ALIGN (basetype);
4708           TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
4709
4710           /* Add padding so that we can put the virtual base class at an
4711              appropriately aligned offset.  */
4712           dsize = CEIL (dsize, desired_align) * desired_align;
4713
4714           /* We try to squish empty virtual bases in just like
4715              ordinary empty bases.  */
4716           if (is_empty_class (basetype))
4717             layout_empty_base (vbase,
4718                                size_int (CEIL (dsize, BITS_PER_UNIT)),
4719                                offsets, t);
4720           else
4721             {
4722               tree offset;
4723
4724               offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
4725               offset = size_diffop (offset, 
4726                                     convert (ssizetype, 
4727                                              BINFO_OFFSET (vbase)));
4728
4729               /* And compute the offset of the virtual base.  */
4730               propagate_binfo_offsets (vbase, offset, t);
4731               /* Every virtual baseclass takes a least a UNIT, so that
4732                  we can take it's address and get something different
4733                  for each base.  */
4734               dsize += MAX (BITS_PER_UNIT,
4735                             tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
4736             }
4737
4738           /* Keep track of the offsets assigned to this virtual base.  */
4739           record_subobject_offsets (BINFO_TYPE (vbase), 
4740                                     BINFO_OFFSET (vbase),
4741                                     offsets,
4742                                     /*vbases_p=*/0);
4743         }
4744     }
4745
4746   /* Now, go through the TYPE_BINFO hierarchy, setting the
4747      BINFO_OFFSETs correctly for all non-primary copies of the virtual
4748      bases and their direct and indirect bases.  The ambiguity checks
4749      in get_base_distance depend on the BINFO_OFFSETs being set
4750      correctly.  */
4751   dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
4752
4753   /* If we had empty base classes that protruded beyond the end of the
4754      class, we didn't update DSIZE above; we were hoping to overlay
4755      multiple such bases at the same location.  */
4756   eoc = end_of_class (t, /*include_virtuals_p=*/1);
4757   if (eoc * BITS_PER_UNIT > dsize)
4758     dsize = eoc * BITS_PER_UNIT;
4759
4760   /* Now, make sure that the total size of the type is a multiple of
4761      its alignment.  */
4762   dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
4763   TYPE_SIZE (t) = bitsize_int (dsize);
4764   TYPE_SIZE_UNIT (t) = convert (sizetype,
4765                                 size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
4766                                             bitsize_unit_node));
4767
4768   /* Check for ambiguous virtual bases.  */
4769   if (extra_warnings)
4770     for (vbases = CLASSTYPE_VBASECLASSES (t); 
4771          vbases; 
4772          vbases = TREE_CHAIN (vbases))
4773       {
4774         tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
4775         if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
4776           cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
4777                       basetype, t);
4778       }
4779 }
4780
4781 /* Returns the offset of the byte just past the end of the base class
4782    with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
4783    only non-virtual bases are included.  */
4784
4785 static unsigned HOST_WIDE_INT
4786 end_of_class (t, include_virtuals_p)
4787      tree t;
4788      int include_virtuals_p;
4789 {
4790   unsigned HOST_WIDE_INT result = 0;
4791   int i;
4792
4793   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4794     {
4795       tree base_binfo;
4796       tree offset;
4797       tree size;
4798       unsigned HOST_WIDE_INT end_of_base;
4799
4800       base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
4801
4802       if (!include_virtuals_p
4803           && TREE_VIA_VIRTUAL (base_binfo) 
4804           && !BINFO_PRIMARY_P (base_binfo))
4805         continue;
4806
4807       if (is_empty_class (BINFO_TYPE (base_binfo)))
4808         /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
4809            allocate some space for it. It cannot have virtual bases,
4810            so TYPE_SIZE_UNIT is fine.  */
4811         size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4812       else
4813         size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
4814       offset = size_binop (PLUS_EXPR, 
4815                            BINFO_OFFSET (base_binfo),
4816                            size);
4817       end_of_base = tree_low_cst (offset, /*pos=*/1);
4818       if (end_of_base > result)
4819         result = end_of_base;
4820     }
4821
4822   return result;
4823 }
4824
4825 /* Warn about direct bases of T that are inaccessible because they are
4826    ambiguous.  For example:
4827
4828      struct S {};
4829      struct T : public S {};
4830      struct U : public S, public T {};
4831
4832    Here, `(S*) new U' is not allowed because there are two `S'
4833    subobjects of U.  */
4834
4835 static void
4836 warn_about_ambiguous_direct_bases (t)
4837      tree t;
4838 {
4839   int i;
4840
4841   for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
4842     {
4843       tree basetype = TYPE_BINFO_BASETYPE (t, i);
4844
4845       if (get_base_distance (basetype, t, 0, NULL) == -2)
4846         cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
4847                     basetype, t);
4848     }
4849 }
4850
4851 /* Compare two INTEGER_CSTs K1 and K2.  */
4852
4853 static int
4854 splay_tree_compare_integer_csts (k1, k2)
4855      splay_tree_key k1;
4856      splay_tree_key k2;
4857 {
4858   return tree_int_cst_compare ((tree) k1, (tree) k2);
4859 }
4860
4861 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
4862    BINFO_OFFSETs for all of the base-classes.  Position the vtable
4863    pointer.  */
4864
4865 static void
4866 layout_class_type (t, empty_p, vfuns_p, 
4867                    new_virtuals_p, overridden_virtuals_p)
4868      tree t;
4869      int *empty_p;
4870      int *vfuns_p;
4871      tree *new_virtuals_p;
4872      tree *overridden_virtuals_p;
4873 {
4874   tree non_static_data_members;
4875   tree field;
4876   tree vptr;
4877   record_layout_info rli;
4878   unsigned HOST_WIDE_INT eoc;
4879   /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
4880      types that appear at that offset.  */
4881   splay_tree empty_base_offsets;
4882
4883   /* Keep track of the first non-static data member.  */
4884   non_static_data_members = TYPE_FIELDS (t);
4885
4886   /* Start laying out the record.  */
4887   rli = start_record_layout (t);
4888
4889   /* If possible, we reuse the virtual function table pointer from one
4890      of our base classes.  */
4891   determine_primary_base (t, vfuns_p);
4892
4893   /* Create a pointer to our virtual function table.  */
4894   vptr = create_vtable_ptr (t, empty_p, vfuns_p,
4895                             new_virtuals_p, overridden_virtuals_p);
4896
4897   /* The vptr is always the first thing in the class.  */
4898   if (vptr)
4899     {
4900       TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
4901       place_field (rli, vptr);
4902     }
4903
4904   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
4905   empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 
4906                                        NULL, NULL);
4907   if (build_base_fields (rli, empty_p, empty_base_offsets, t))
4908     CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
4909   
4910   /* Layout the non-static data members.  */
4911   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
4912     {
4913       tree type;
4914       tree padding;
4915
4916       /* We still pass things that aren't non-static data members to
4917          the back-end, in case it wants to do something with them.  */
4918       if (TREE_CODE (field) != FIELD_DECL)
4919         {
4920           place_field (rli, field);
4921           continue;
4922         }
4923
4924       type = TREE_TYPE (field);
4925
4926       /* If this field is a bit-field whose width is greater than its
4927          type, then there are some special rules for allocating
4928          it.  */
4929       if (DECL_C_BIT_FIELD (field)
4930           && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
4931         {
4932           integer_type_kind itk;
4933           tree integer_type;
4934
4935           /* We must allocate the bits as if suitably aligned for the
4936              longest integer type that fits in this many bits.  type
4937              of the field.  Then, we are supposed to use the left over
4938              bits as additional padding.  */
4939           for (itk = itk_char; itk != itk_none; ++itk)
4940             if (INT_CST_LT (DECL_SIZE (field), 
4941                             TYPE_SIZE (integer_types[itk])))
4942               break;
4943
4944           /* ITK now indicates a type that is too large for the
4945              field.  We have to back up by one to find the largest
4946              type that fits.  */
4947           integer_type = integer_types[itk - 1];
4948           padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 
4949                                 TYPE_SIZE (integer_type));
4950           DECL_SIZE (field) = TYPE_SIZE (integer_type);
4951           DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
4952           DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
4953         }
4954       else
4955         padding = NULL_TREE;
4956
4957       layout_nonempty_base_or_field (rli, field, NULL_TREE,
4958                                      empty_base_offsets, t);
4959
4960       /* If we needed additional padding after this field, add it
4961          now.  */
4962       if (padding)
4963         {
4964           tree padding_field;
4965
4966           padding_field = build_decl (FIELD_DECL, 
4967                                       NULL_TREE,
4968                                       char_type_node); 
4969           DECL_BIT_FIELD (padding_field) = 1;
4970           DECL_SIZE (padding_field) = padding;
4971           DECL_ALIGN (padding_field) = 1;
4972           DECL_USER_ALIGN (padding_field) = 0;
4973           layout_nonempty_base_or_field (rli, padding_field,
4974                                          NULL_TREE, 
4975                                          empty_base_offsets, t);
4976         }
4977     }
4978
4979   /* It might be the case that we grew the class to allocate a
4980      zero-sized base class.  That won't be reflected in RLI, yet,
4981      because we are willing to overlay multiple bases at the same
4982      offset.  However, now we need to make sure that RLI is big enough
4983      to reflect the entire class.  */
4984   eoc = end_of_class (t, /*include_virtuals_p=*/0);
4985   if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
4986       && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
4987     {
4988       rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
4989       rli->bitpos = bitsize_zero_node;
4990     }
4991
4992   /* We make all structures have at least one element, so that they
4993      have non-zero size.  The class may be empty even if it has
4994      basetypes.  Therefore, we add the fake field after all the other
4995      fields; if there are already FIELD_DECLs on the list, their
4996      offsets will not be disturbed.  */
4997   if (!eoc && *empty_p)
4998     {
4999       tree padding;
5000
5001       padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
5002       place_field (rli, padding);
5003     }
5004
5005   /* Let the back-end lay out the type. Note that at this point we
5006      have only included non-virtual base-classes; we will lay out the
5007      virtual base classes later.  So, the TYPE_SIZE/TYPE_ALIGN after
5008      this call are not necessarily correct; they are just the size and
5009      alignment when no virtual base clases are used.  */
5010   finish_record_layout (rli);
5011
5012   /* Delete all zero-width bit-fields from the list of fields.  Now
5013      that the type is laid out they are no longer important.  */
5014   remove_zero_width_bit_fields (t);
5015
5016   /* Remember the size and alignment of the class before adding
5017      the virtual bases.  */
5018   if (*empty_p)
5019     {
5020       CLASSTYPE_SIZE (t) = bitsize_zero_node;
5021       CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
5022     }
5023   else
5024     {
5025       CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
5026       CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
5027     }
5028
5029   CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
5030   CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
5031
5032   /* Set the TYPE_DECL for this type to contain the right
5033      value for DECL_OFFSET, so that we can use it as part
5034      of a COMPONENT_REF for multiple inheritance.  */
5035   layout_decl (TYPE_MAIN_DECL (t), 0);
5036
5037   /* Now fix up any virtual base class types that we left lying
5038      around.  We must get these done before we try to lay out the
5039      virtual function table.  As a side-effect, this will remove the
5040      base subobject fields.  */
5041   layout_virtual_bases (t, empty_base_offsets);
5042
5043   /* Warn about direct bases that can't be talked about due to
5044      ambiguity.  */
5045   warn_about_ambiguous_direct_bases (t);
5046
5047   /* Clean up.  */
5048   splay_tree_delete (empty_base_offsets);
5049 }
5050
5051 /* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
5052    (or C++ class declaration).
5053
5054    For C++, we must handle the building of derived classes.
5055    Also, C++ allows static class members.  The way that this is
5056    handled is to keep the field name where it is (as the DECL_NAME
5057    of the field), and place the overloaded decl in the bit position
5058    of the field.  layout_record and layout_union will know about this.
5059
5060    More C++ hair: inline functions have text in their
5061    DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
5062    meaningful tree structure.  After the struct has been laid out, set
5063    things up so that this can happen.
5064
5065    And still more: virtual functions.  In the case of single inheritance,
5066    when a new virtual function is seen which redefines a virtual function
5067    from the base class, the new virtual function is placed into
5068    the virtual function table at exactly the same address that
5069    it had in the base class.  When this is extended to multiple
5070    inheritance, the same thing happens, except that multiple virtual
5071    function tables must be maintained.  The first virtual function
5072    table is treated in exactly the same way as in the case of single
5073    inheritance.  Additional virtual function tables have different
5074    DELTAs, which tell how to adjust `this' to point to the right thing.
5075
5076    ATTRIBUTES is the set of decl attributes to be applied, if any.  */
5077
5078 void
5079 finish_struct_1 (t)
5080      tree t;
5081 {
5082   tree x;
5083   int vfuns;
5084   /* The NEW_VIRTUALS is a TREE_LIST.  The TREE_VALUE of each node is
5085      a FUNCTION_DECL.  Each of these functions is a virtual function
5086      declared in T that does not override any virtual function from a
5087      base class.  */
5088   tree new_virtuals = NULL_TREE;
5089   /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
5090      except that each declaration here overrides the declaration from
5091      a base class.  */
5092   tree overridden_virtuals = NULL_TREE;
5093   int n_fields = 0;
5094   tree vfield;
5095   int empty = 1;
5096
5097   if (COMPLETE_TYPE_P (t))
5098     {
5099       if (IS_AGGR_TYPE (t))
5100         cp_error ("redefinition of `%#T'", t);
5101       else
5102         my_friendly_abort (172);
5103       popclass ();
5104       return;
5105     }
5106
5107   GNU_xref_decl (current_function_decl, t);
5108
5109   /* If this type was previously laid out as a forward reference,
5110      make sure we lay it out again.  */
5111   TYPE_SIZE (t) = NULL_TREE;
5112   CLASSTYPE_GOT_SEMICOLON (t) = 0;
5113   CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
5114   vfuns = 0;
5115   CLASSTYPE_RTTI (t) = NULL_TREE;
5116
5117   fixup_inline_methods (t);
5118   
5119   /* Do end-of-class semantic processing: checking the validity of the
5120      bases and members and add implicitly generated methods.  */
5121   check_bases_and_members (t, &empty);
5122
5123   /* Layout the class itself.  */
5124   layout_class_type (t, &empty, &vfuns,
5125                      &new_virtuals, &overridden_virtuals);
5126
5127   /* Make sure that we get our own copy of the vfield FIELD_DECL.  */
5128   vfield = TYPE_VFIELD (t);
5129   if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5130     {
5131       tree primary = CLASSTYPE_PRIMARY_BINFO (t);
5132
5133       my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
5134                                        BINFO_TYPE (primary)),
5135                           20010726);
5136       /* The vtable better be at the start. */
5137       my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
5138                           20010726);
5139       my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
5140                           20010726);
5141       
5142       vfield = copy_decl (vfield);
5143       DECL_FIELD_CONTEXT (vfield) = t;
5144       TYPE_VFIELD (t) = vfield;
5145     }
5146   else
5147     my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
5148
5149   overridden_virtuals 
5150     = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
5151
5152   /* If we created a new vtbl pointer for this class, add it to the
5153      list.  */
5154   if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5155     CLASSTYPE_VFIELDS (t) 
5156       = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
5157
5158   /* If necessary, create the primary vtable for this class.  */
5159   if (new_virtuals || overridden_virtuals || TYPE_CONTAINS_VPTR_P (t))
5160     {
5161       new_virtuals = nreverse (new_virtuals);
5162       /* We must enter these virtuals into the table.  */
5163       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5164         build_primary_vtable (NULL_TREE, t);
5165       else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
5166         /* Here we know enough to change the type of our virtual
5167            function table, but we will wait until later this function.  */
5168         build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
5169
5170       /* If this type has basetypes with constructors, then those
5171          constructors might clobber the virtual function table.  But
5172          they don't if the derived class shares the exact vtable of the base
5173          class.  */
5174
5175       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5176     }
5177   /* If we didn't need a new vtable, see if we should copy one from
5178      the base.  */
5179   else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5180     {
5181       tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
5182
5183       /* If this class uses a different vtable than its primary base
5184          then when we will need to initialize our vptr after the base
5185          class constructor runs.  */
5186       if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
5187         CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
5188     }
5189
5190   if (TYPE_CONTAINS_VPTR_P (t))
5191     {
5192       if (TYPE_BINFO_VTABLE (t))
5193         my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
5194                             20000116);
5195       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
5196         my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
5197                             20000116);
5198
5199       CLASSTYPE_VSIZE (t) = vfuns;
5200       /* Entries for virtual functions defined in the primary base are
5201          followed by entries for new functions unique to this class.  */
5202       TYPE_BINFO_VIRTUALS (t) 
5203         = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
5204       /* Finally, add entries for functions that override virtuals
5205          from non-primary bases.  */
5206       TYPE_BINFO_VIRTUALS (t) 
5207         = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
5208     }
5209
5210   finish_struct_bits (t);
5211
5212   /* Complete the rtl for any static member objects of the type we're
5213      working on.  */
5214   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
5215     if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
5216         && TREE_TYPE (x) == t)
5217       DECL_MODE (x) = TYPE_MODE (t);
5218
5219   /* Done with FIELDS...now decide whether to sort these for
5220      faster lookups later.
5221
5222      The C front-end only does this when n_fields > 15.  We use
5223      a smaller number because most searches fail (succeeding
5224      ultimately as the search bores through the inheritance
5225      hierarchy), and we want this failure to occur quickly.  */
5226
5227   n_fields = count_fields (TYPE_FIELDS (t));
5228   if (n_fields > 7)
5229     {
5230       tree field_vec = make_tree_vec (n_fields);
5231       add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
5232       qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
5233              (int (*)(const void *, const void *))field_decl_cmp);
5234       if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
5235         retrofit_lang_decl (TYPE_MAIN_DECL (t));
5236       DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
5237     }
5238
5239   if (TYPE_HAS_CONSTRUCTOR (t))
5240     {
5241       tree vfields = CLASSTYPE_VFIELDS (t);
5242
5243       while (vfields)
5244         {
5245           /* Mark the fact that constructor for T
5246              could affect anybody inheriting from T
5247              who wants to initialize vtables for VFIELDS's type.  */
5248           if (VF_DERIVED_VALUE (vfields))
5249             TREE_ADDRESSABLE (vfields) = 1;
5250           vfields = TREE_CHAIN (vfields);
5251         }
5252     }
5253
5254   /* Make the rtl for any new vtables we have created, and unmark
5255      the base types we marked.  */
5256   finish_vtbls (t);
5257   
5258   /* Build the VTT for T.  */
5259   build_vtt (t);
5260
5261   if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
5262       && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
5263     cp_warning ("`%#T' has virtual functions but non-virtual destructor", t);
5264
5265   hack_incomplete_structures (t);
5266
5267   if (warn_overloaded_virtual)
5268     warn_hidden (t);
5269
5270   maybe_suppress_debug_info (t);
5271
5272   dump_class_hierarchy (t);
5273   
5274   /* Finish debugging output for this type.  */
5275   rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
5276 }
5277
5278 /* When T was built up, the member declarations were added in reverse
5279    order.  Rearrange them to declaration order.  */
5280
5281 void
5282 unreverse_member_declarations (t)
5283      tree t;
5284 {
5285   tree next;
5286   tree prev;
5287   tree x;
5288
5289   /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
5290      reverse order.  Put them in declaration order now.  */
5291   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
5292   CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
5293
5294   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
5295      reverse order, so we can't just use nreverse.  */
5296   prev = NULL_TREE;
5297   for (x = TYPE_FIELDS (t); 
5298        x && TREE_CODE (x) != TYPE_DECL; 
5299        x = next)
5300     {
5301       next = TREE_CHAIN (x);
5302       TREE_CHAIN (x) = prev;
5303       prev = x;
5304     }
5305   if (prev)
5306     {
5307       TREE_CHAIN (TYPE_FIELDS (t)) = x;
5308       if (prev)
5309         TYPE_FIELDS (t) = prev;
5310     }
5311 }
5312
5313 tree
5314 finish_struct (t, attributes)
5315      tree t, attributes;
5316 {
5317   const char *saved_filename = input_filename;
5318   int saved_lineno = lineno;
5319
5320   /* Now that we've got all the field declarations, reverse everything
5321      as necessary.  */
5322   unreverse_member_declarations (t);
5323
5324   cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
5325
5326   /* Nadger the current location so that diagnostics point to the start of
5327      the struct, not the end.  */
5328   input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
5329   lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
5330
5331   if (processing_template_decl)
5332     {
5333       finish_struct_methods (t);
5334       TYPE_SIZE (t) = bitsize_zero_node;
5335     }
5336   else
5337     finish_struct_1 (t);
5338
5339   input_filename = saved_filename;
5340   lineno = saved_lineno;
5341
5342   TYPE_BEING_DEFINED (t) = 0;
5343
5344   if (current_class_type)
5345     popclass ();
5346   else
5347     error ("trying to finish struct, but kicked out due to previous parse errors.");
5348
5349   if (processing_template_decl)
5350     {
5351       tree scope = current_scope ();
5352       if (scope && TREE_CODE (scope) == FUNCTION_DECL)
5353         add_stmt (build_min (TAG_DEFN, t));
5354     }
5355
5356   return t;
5357 }
5358 \f
5359 /* Return the dynamic type of INSTANCE, if known.
5360    Used to determine whether the virtual function table is needed
5361    or not.
5362
5363    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5364    of our knowledge of its type.  *NONNULL should be initialized
5365    before this function is called.  */
5366
5367 static tree
5368 fixed_type_or_null (instance, nonnull, cdtorp)
5369      tree instance;
5370      int *nonnull;
5371      int *cdtorp;
5372 {
5373   switch (TREE_CODE (instance))
5374     {
5375     case INDIRECT_REF:
5376       /* Check that we are not going through a cast of some sort.  */
5377       if (TREE_TYPE (instance)
5378           == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
5379         instance = TREE_OPERAND (instance, 0);
5380       /* fall through...  */
5381     case CALL_EXPR:
5382       /* This is a call to a constructor, hence it's never zero.  */
5383       if (TREE_HAS_CONSTRUCTOR (instance))
5384         {
5385           if (nonnull)
5386             *nonnull = 1;
5387           return TREE_TYPE (instance);
5388         }
5389       return NULL_TREE;
5390
5391     case SAVE_EXPR:
5392       /* This is a call to a constructor, hence it's never zero.  */
5393       if (TREE_HAS_CONSTRUCTOR (instance))
5394         {
5395           if (nonnull)
5396             *nonnull = 1;
5397           return TREE_TYPE (instance);
5398         }
5399       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5400
5401     case RTL_EXPR:
5402       return NULL_TREE;
5403
5404     case PLUS_EXPR:
5405     case MINUS_EXPR:
5406       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
5407         return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5408       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
5409         /* Propagate nonnull.  */
5410         fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5411       return NULL_TREE;
5412
5413     case NOP_EXPR:
5414     case CONVERT_EXPR:
5415       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5416
5417     case ADDR_EXPR:
5418       if (nonnull)
5419         *nonnull = 1;
5420       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
5421
5422     case COMPONENT_REF:
5423       return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
5424
5425     case VAR_DECL:
5426     case FIELD_DECL:
5427       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
5428           && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
5429         {
5430           if (nonnull)
5431             *nonnull = 1;
5432           return TREE_TYPE (TREE_TYPE (instance));
5433         }
5434       /* fall through...  */
5435     case TARGET_EXPR:
5436     case PARM_DECL:
5437       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
5438         {
5439           if (nonnull)
5440             *nonnull = 1;
5441           return TREE_TYPE (instance);
5442         }
5443       else if (instance == current_class_ptr)
5444         {
5445           if (nonnull)
5446             *nonnull = 1;
5447         
5448           /* if we're in a ctor or dtor, we know our type. */
5449           if (DECL_LANG_SPECIFIC (current_function_decl)
5450               && (DECL_CONSTRUCTOR_P (current_function_decl)
5451                   || DECL_DESTRUCTOR_P (current_function_decl)))
5452             {
5453               if (cdtorp)
5454                 *cdtorp = 1;
5455               return TREE_TYPE (TREE_TYPE (instance));
5456             }
5457         }
5458       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
5459         {
5460           /* Reference variables should be references to objects.  */
5461           if (nonnull)
5462             *nonnull = 1;
5463         }
5464       return NULL_TREE;
5465
5466     default:
5467       return NULL_TREE;
5468     }
5469 }
5470
5471 /* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
5472    to the static type.  We also handle the case where INSTANCE is really
5473    a pointer. Return negative if this is a ctor/dtor. There the dynamic type
5474    is known, but this might not be the most derived base of the original object,
5475    and hence virtual bases may not be layed out according to this type.
5476
5477    Used to determine whether the virtual function table is needed
5478    or not.
5479
5480    *NONNULL is set iff INSTANCE can be known to be nonnull, regardless
5481    of our knowledge of its type.  *NONNULL should be initialized
5482    before this function is called.  */
5483
5484 int
5485 resolves_to_fixed_type_p (instance, nonnull)
5486      tree instance;
5487      int *nonnull;
5488 {
5489   tree t = TREE_TYPE (instance);
5490   int cdtorp = 0;
5491   
5492   tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
5493   if (fixed == NULL_TREE)
5494     return 0;
5495   if (POINTER_TYPE_P (t))
5496     t = TREE_TYPE (t);
5497   if (!same_type_ignoring_top_level_qualifiers_p (t, fixed))
5498     return 0;
5499   return cdtorp ? -1 : 1;
5500 }
5501
5502 \f
5503 void
5504 init_class_processing ()
5505 {
5506   current_class_depth = 0;
5507   current_class_stack_size = 10;
5508   current_class_stack 
5509     = (class_stack_node_t) xmalloc (current_class_stack_size 
5510                                     * sizeof (struct class_stack_node));
5511   VARRAY_TREE_INIT (local_classes, 8, "local_classes");
5512   ggc_add_tree_varray_root (&local_classes, 1);
5513
5514   access_default_node = build_int_2 (0, 0);
5515   access_public_node = build_int_2 (ak_public, 0);
5516   access_protected_node = build_int_2 (ak_protected, 0);
5517   access_private_node = build_int_2 (ak_private, 0);
5518   access_default_virtual_node = build_int_2 (4, 0);
5519   access_public_virtual_node = build_int_2 (4 | ak_public, 0);
5520   access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
5521   access_private_virtual_node = build_int_2 (4 | ak_private, 0);
5522
5523   ridpointers[(int) RID_PUBLIC] = access_public_node;
5524   ridpointers[(int) RID_PRIVATE] = access_private_node;
5525   ridpointers[(int) RID_PROTECTED] = access_protected_node;
5526 }
5527
5528 /* Set current scope to NAME. CODE tells us if this is a
5529    STRUCT, UNION, or ENUM environment.
5530
5531    NAME may end up being NULL_TREE if this is an anonymous or
5532    late-bound struct (as in "struct { ... } foo;")  */
5533
5534 /* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
5535    appropriate values, found by looking up the type definition of
5536    NAME (as a CODE).
5537
5538    If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
5539    which can be seen locally to the class.  They are shadowed by
5540    any subsequent local declaration (including parameter names).
5541
5542    If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
5543    which have static meaning (i.e., static members, static
5544    member functions, enum declarations, etc).
5545
5546    If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
5547    which can be seen locally to the class (as in 1), but
5548    know that we are doing this for declaration purposes
5549    (i.e. friend foo::bar (int)).
5550
5551    So that we may avoid calls to lookup_name, we cache the _TYPE
5552    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
5553
5554    For multiple inheritance, we perform a two-pass depth-first search
5555    of the type lattice.  The first pass performs a pre-order search,
5556    marking types after the type has had its fields installed in
5557    the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
5558    unmarks the marked types.  If a field or member function name
5559    appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
5560    that name becomes `error_mark_node'.  */
5561
5562 void
5563 pushclass (type, modify)
5564      tree type;
5565      int modify;
5566 {
5567   type = TYPE_MAIN_VARIANT (type);
5568
5569   /* Make sure there is enough room for the new entry on the stack.  */
5570   if (current_class_depth + 1 >= current_class_stack_size) 
5571     {
5572       current_class_stack_size *= 2;
5573       current_class_stack
5574         = (class_stack_node_t) xrealloc (current_class_stack,
5575                                          current_class_stack_size
5576                                          * sizeof (struct class_stack_node));
5577     }
5578
5579   /* Insert a new entry on the class stack.  */
5580   current_class_stack[current_class_depth].name = current_class_name;
5581   current_class_stack[current_class_depth].type = current_class_type;
5582   current_class_stack[current_class_depth].access = current_access_specifier;
5583   current_class_stack[current_class_depth].names_used = 0;
5584   current_class_depth++;
5585
5586   /* Now set up the new type.  */
5587   current_class_name = TYPE_NAME (type);
5588   if (TREE_CODE (current_class_name) == TYPE_DECL)
5589     current_class_name = DECL_NAME (current_class_name);
5590   current_class_type = type;
5591
5592   /* By default, things in classes are private, while things in
5593      structures or unions are public.  */
5594   current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
5595                               ? access_private_node 
5596                               : access_public_node);
5597
5598   if (previous_class_type != NULL_TREE
5599       && (type != previous_class_type 
5600           || !COMPLETE_TYPE_P (previous_class_type))
5601       && current_class_depth == 1)
5602     {
5603       /* Forcibly remove any old class remnants.  */
5604       invalidate_class_lookup_cache ();
5605     }
5606
5607   /* If we're about to enter a nested class, clear
5608      IDENTIFIER_CLASS_VALUE for the enclosing classes.  */
5609   if (modify && current_class_depth > 1)
5610     clear_identifier_class_values ();
5611
5612   pushlevel_class ();
5613
5614   if (modify)
5615     {
5616       if (type != previous_class_type || current_class_depth > 1)
5617         push_class_decls (type);
5618       else
5619         {
5620           tree item;
5621
5622           /* We are re-entering the same class we just left, so we
5623              don't have to search the whole inheritance matrix to find
5624              all the decls to bind again.  Instead, we install the
5625              cached class_shadowed list, and walk through it binding
5626              names and setting up IDENTIFIER_TYPE_VALUEs.  */
5627           set_class_shadows (previous_class_values);
5628           for (item = previous_class_values; item; item = TREE_CHAIN (item))
5629             {
5630               tree id = TREE_PURPOSE (item);
5631               tree decl = TREE_TYPE (item);
5632
5633               push_class_binding (id, decl);
5634               if (TREE_CODE (decl) == TYPE_DECL)
5635                 set_identifier_type_value (id, TREE_TYPE (decl));
5636             }
5637           unuse_fields (type);
5638         }
5639
5640       storetags (CLASSTYPE_TAGS (type));
5641     }
5642 }
5643
5644 /* When we exit a toplevel class scope, we save the
5645    IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
5646    reenter the class.  Here, we've entered some other class, so we
5647    must invalidate our cache.  */
5648
5649 void
5650 invalidate_class_lookup_cache ()
5651 {
5652   tree t;
5653   
5654   /* The IDENTIFIER_CLASS_VALUEs are no longer valid.  */
5655   for (t = previous_class_values; t; t = TREE_CHAIN (t))
5656     IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
5657
5658   previous_class_values = NULL_TREE;
5659   previous_class_type = NULL_TREE;
5660 }
5661  
5662 /* Get out of the current class scope. If we were in a class scope
5663    previously, that is the one popped to.  */
5664
5665 void
5666 popclass ()
5667 {
5668   poplevel_class ();
5669   /* Since poplevel_class does the popping of class decls nowadays,
5670      this really only frees the obstack used for these decls.  */
5671   pop_class_decls ();
5672
5673   current_class_depth--;
5674   current_class_name = current_class_stack[current_class_depth].name;
5675   current_class_type = current_class_stack[current_class_depth].type;
5676   current_access_specifier = current_class_stack[current_class_depth].access;
5677   if (current_class_stack[current_class_depth].names_used)
5678     splay_tree_delete (current_class_stack[current_class_depth].names_used);
5679 }
5680
5681 /* Returns 1 if current_class_type is either T or a nested type of T.
5682    We start looking from 1 because entry 0 is from global scope, and has
5683    no type.  */
5684
5685 int
5686 currently_open_class (t)
5687      tree t;
5688 {
5689   int i;
5690   if (t == current_class_type)
5691     return 1;
5692   for (i = 1; i < current_class_depth; ++i)
5693     if (current_class_stack [i].type == t)
5694       return 1;
5695   return 0;
5696 }
5697
5698 /* If either current_class_type or one of its enclosing classes are derived
5699    from T, return the appropriate type.  Used to determine how we found
5700    something via unqualified lookup.  */
5701
5702 tree
5703 currently_open_derived_class (t)
5704      tree t;
5705 {
5706   int i;
5707
5708   if (DERIVED_FROM_P (t, current_class_type))
5709     return current_class_type;
5710
5711   for (i = current_class_depth - 1; i > 0; --i)
5712     if (DERIVED_FROM_P (t, current_class_stack[i].type))
5713       return current_class_stack[i].type;
5714
5715   return NULL_TREE;
5716 }
5717
5718 /* When entering a class scope, all enclosing class scopes' names with
5719    static meaning (static variables, static functions, types and enumerators)
5720    have to be visible.  This recursive function calls pushclass for all
5721    enclosing class contexts until global or a local scope is reached.
5722    TYPE is the enclosed class and MODIFY is equivalent with the pushclass
5723    formal of the same name.  */
5724
5725 void
5726 push_nested_class (type, modify)
5727      tree type;
5728      int modify;
5729 {
5730   tree context;
5731
5732   /* A namespace might be passed in error cases, like A::B:C.  */
5733   if (type == NULL_TREE 
5734       || type == error_mark_node 
5735       || TREE_CODE (type) == NAMESPACE_DECL
5736       || ! IS_AGGR_TYPE (type)
5737       || TREE_CODE (type) == TEMPLATE_TYPE_PARM
5738       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
5739     return;
5740   
5741   context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
5742
5743   if (context && CLASS_TYPE_P (context))
5744     push_nested_class (context, 2);
5745   pushclass (type, modify);
5746 }
5747
5748 /* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
5749
5750 void
5751 pop_nested_class ()
5752 {
5753   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
5754
5755   popclass ();
5756   if (context && CLASS_TYPE_P (context))
5757     pop_nested_class ();
5758 }
5759
5760 /* Returns the number of extern "LANG" blocks we are nested within.  */
5761
5762 int
5763 current_lang_depth ()
5764 {
5765   return VARRAY_ACTIVE_SIZE (current_lang_base);
5766 }
5767
5768 /* Set global variables CURRENT_LANG_NAME to appropriate value
5769    so that behavior of name-mangling machinery is correct.  */
5770
5771 void
5772 push_lang_context (name)
5773      tree name;
5774 {
5775   VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
5776
5777   if (name == lang_name_cplusplus)
5778     {
5779       current_lang_name = name;
5780     }
5781   else if (name == lang_name_java)
5782     {
5783       current_lang_name = name;
5784       /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
5785          (See record_builtin_java_type in decl.c.)  However, that causes
5786          incorrect debug entries if these types are actually used.
5787          So we re-enable debug output after extern "Java". */
5788       DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
5789       DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
5790       DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
5791       DECL_IGNORED_P (TYPE_NAME (java_long_type_node)) = 0;
5792       DECL_IGNORED_P (TYPE_NAME (java_float_type_node)) = 0;
5793       DECL_IGNORED_P (TYPE_NAME (java_double_type_node)) = 0;
5794       DECL_IGNORED_P (TYPE_NAME (java_char_type_node)) = 0;
5795       DECL_IGNORED_P (TYPE_NAME (java_boolean_type_node)) = 0;
5796     }
5797   else if (name == lang_name_c)
5798     {
5799       current_lang_name = name;
5800     }
5801   else
5802     error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
5803 }
5804   
5805 /* Get out of the current language scope.  */
5806
5807 void
5808 pop_lang_context ()
5809 {
5810   current_lang_name = VARRAY_TOP_TREE (current_lang_base);
5811   VARRAY_POP (current_lang_base);
5812 }
5813 \f
5814 /* Type instantiation routines.  */
5815
5816 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
5817    matches the TARGET_TYPE.  If there is no satisfactory match, return
5818    error_mark_node, and issue an error message if COMPLAIN is
5819    non-zero.  Permit pointers to member function if PTRMEM is non-zero.
5820    If TEMPLATE_ONLY, the name of the overloaded function
5821    was a template-id, and EXPLICIT_TARGS are the explicitly provided
5822    template arguments.  */
5823
5824 static tree
5825 resolve_address_of_overloaded_function (target_type, 
5826                                         overload,
5827                                         complain,
5828                                         ptrmem,
5829                                         template_only,
5830                                         explicit_targs)
5831      tree target_type;
5832      tree overload;
5833      int complain;
5834      int ptrmem;
5835      int template_only;
5836      tree explicit_targs;
5837 {
5838   /* Here's what the standard says:
5839      
5840        [over.over]
5841
5842        If the name is a function template, template argument deduction
5843        is done, and if the argument deduction succeeds, the deduced
5844        arguments are used to generate a single template function, which
5845        is added to the set of overloaded functions considered.
5846
5847        Non-member functions and static member functions match targets of
5848        type "pointer-to-function" or "reference-to-function."  Nonstatic
5849        member functions match targets of type "pointer-to-member
5850        function;" the function type of the pointer to member is used to
5851        select the member function from the set of overloaded member
5852        functions.  If a nonstatic member function is selected, the
5853        reference to the overloaded function name is required to have the
5854        form of a pointer to member as described in 5.3.1.
5855
5856        If more than one function is selected, any template functions in
5857        the set are eliminated if the set also contains a non-template
5858        function, and any given template function is eliminated if the
5859        set contains a second template function that is more specialized
5860        than the first according to the partial ordering rules 14.5.5.2.
5861        After such eliminations, if any, there shall remain exactly one
5862        selected function.  */
5863
5864   int is_ptrmem = 0;
5865   int is_reference = 0;
5866   /* We store the matches in a TREE_LIST rooted here.  The functions
5867      are the TREE_PURPOSE, not the TREE_VALUE, in this list, for easy
5868      interoperability with most_specialized_instantiation.  */
5869   tree matches = NULL_TREE;
5870   tree fn;
5871
5872   /* By the time we get here, we should be seeing only real
5873      pointer-to-member types, not the internal POINTER_TYPE to
5874      METHOD_TYPE representation.  */
5875   my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
5876                         && (TREE_CODE (TREE_TYPE (target_type)) 
5877                             == METHOD_TYPE)), 0);
5878
5879   if (TREE_CODE (overload) == COMPONENT_REF)
5880     overload = TREE_OPERAND (overload, 1);
5881
5882   /* Check that the TARGET_TYPE is reasonable.  */
5883   if (TYPE_PTRFN_P (target_type))
5884     /* This is OK.  */;
5885   else if (TYPE_PTRMEMFUNC_P (target_type))
5886     /* This is OK, too.  */
5887     is_ptrmem = 1;
5888   else if (TREE_CODE (target_type) == FUNCTION_TYPE)
5889     {
5890       /* This is OK, too.  This comes from a conversion to reference
5891          type.  */
5892       target_type = build_reference_type (target_type);
5893       is_reference = 1;
5894     }
5895   else 
5896     {
5897       if (complain)
5898         cp_error ("\
5899 cannot resolve overloaded function `%D' based on conversion to type `%T'", 
5900                   DECL_NAME (OVL_FUNCTION (overload)), target_type);
5901       return error_mark_node;
5902     }
5903   
5904   /* If we can find a non-template function that matches, we can just
5905      use it.  There's no point in generating template instantiations
5906      if we're just going to throw them out anyhow.  But, of course, we
5907      can only do this when we don't *need* a template function.  */
5908   if (!template_only)
5909     {
5910       tree fns;
5911
5912       for (fns = overload; fns; fns = OVL_CHAIN (fns))
5913         {
5914           tree fn = OVL_FUNCTION (fns);
5915           tree fntype;
5916
5917           if (TREE_CODE (fn) == TEMPLATE_DECL)
5918             /* We're not looking for templates just yet.  */
5919             continue;
5920
5921           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5922               != is_ptrmem)
5923             /* We're looking for a non-static member, and this isn't
5924                one, or vice versa.  */
5925             continue;
5926         
5927           /* See if there's a match.  */
5928           fntype = TREE_TYPE (fn);
5929           if (is_ptrmem)
5930             fntype = build_ptrmemfunc_type (build_pointer_type (fntype));
5931           else if (!is_reference)
5932             fntype = build_pointer_type (fntype);
5933
5934           if (can_convert_arg (target_type, fntype, fn))
5935             matches = tree_cons (fn, NULL_TREE, matches);
5936         }
5937     }
5938
5939   /* Now, if we've already got a match (or matches), there's no need
5940      to proceed to the template functions.  But, if we don't have a
5941      match we need to look at them, too.  */
5942   if (!matches) 
5943     {
5944       tree target_fn_type;
5945       tree target_arg_types;
5946       tree target_ret_type;
5947       tree fns;
5948
5949       if (is_ptrmem)
5950         target_fn_type
5951           = TREE_TYPE (TYPE_PTRMEMFUNC_FN_TYPE (target_type));
5952       else
5953         target_fn_type = TREE_TYPE (target_type);
5954       target_arg_types = TYPE_ARG_TYPES (target_fn_type);
5955       target_ret_type = TREE_TYPE (target_fn_type);
5956
5957       /* Never do unification on the 'this' parameter.  */
5958       if (TREE_CODE (target_fn_type) == METHOD_TYPE)
5959         target_arg_types = TREE_CHAIN (target_arg_types);
5960           
5961       for (fns = overload; fns; fns = OVL_CHAIN (fns))
5962         {
5963           tree fn = OVL_FUNCTION (fns);
5964           tree instantiation;
5965           tree instantiation_type;
5966           tree targs;
5967
5968           if (TREE_CODE (fn) != TEMPLATE_DECL)
5969             /* We're only looking for templates.  */
5970             continue;
5971
5972           if ((TREE_CODE (TREE_TYPE (fn)) == METHOD_TYPE)
5973               != is_ptrmem)
5974             /* We're not looking for a non-static member, and this is
5975                one, or vice versa.  */
5976             continue;
5977
5978           /* Try to do argument deduction.  */
5979           targs = make_tree_vec (DECL_NTPARMS (fn));
5980           if (fn_type_unification (fn, explicit_targs, targs,
5981                                    target_arg_types, target_ret_type,
5982                                    DEDUCE_EXACT, -1) != 0)
5983             /* Argument deduction failed.  */
5984             continue;
5985
5986           /* Instantiate the template.  */
5987           instantiation = instantiate_template (fn, targs);
5988           if (instantiation == error_mark_node)
5989             /* Instantiation failed.  */
5990             continue;
5991
5992           /* See if there's a match.  */
5993           instantiation_type = TREE_TYPE (instantiation);
5994           if (is_ptrmem)
5995             instantiation_type = 
5996               build_ptrmemfunc_type (build_pointer_type (instantiation_type));
5997           else if (!is_reference)
5998             instantiation_type = build_pointer_type (instantiation_type);
5999           if (can_convert_arg (target_type, instantiation_type, instantiation))
6000             matches = tree_cons (instantiation, fn, matches);
6001         }
6002
6003       /* Now, remove all but the most specialized of the matches.  */
6004       if (matches)
6005         {
6006           tree match = most_specialized_instantiation (matches);
6007
6008           if (match != error_mark_node)
6009             matches = tree_cons (match, NULL_TREE, NULL_TREE);
6010         }
6011     }
6012
6013   /* Now we should have exactly one function in MATCHES.  */
6014   if (matches == NULL_TREE)
6015     {
6016       /* There were *no* matches.  */
6017       if (complain)
6018         {
6019           cp_error ("no matches converting function `%D' to type `%#T'", 
6020                     DECL_NAME (OVL_FUNCTION (overload)),
6021                     target_type);
6022
6023           /* print_candidates expects a chain with the functions in
6024              TREE_VALUE slots, so we cons one up here (we're losing anyway,
6025              so why be clever?).  */
6026           for (; overload; overload = OVL_NEXT (overload))
6027             matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
6028                                  matches);
6029           
6030           print_candidates (matches);
6031         }
6032       return error_mark_node;
6033     }
6034   else if (TREE_CHAIN (matches))
6035     {
6036       /* There were too many matches.  */
6037
6038       if (complain)
6039         {
6040           tree match;
6041
6042           cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
6043                     DECL_NAME (OVL_FUNCTION (overload)),
6044                     target_type);
6045
6046           /* Since print_candidates expects the functions in the
6047              TREE_VALUE slot, we flip them here.  */
6048           for (match = matches; match; match = TREE_CHAIN (match))
6049             TREE_VALUE (match) = TREE_PURPOSE (match);
6050
6051           print_candidates (matches);
6052         }
6053       
6054       return error_mark_node;
6055     }
6056
6057   /* Good, exactly one match.  Now, convert it to the correct type.  */
6058   fn = TREE_PURPOSE (matches);
6059
6060   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
6061       && !ptrmem && !flag_ms_extensions)
6062     {
6063       static int explained;
6064       
6065       if (!complain)
6066         return error_mark_node;
6067
6068       cp_pedwarn ("assuming pointer to member `%D'", fn);
6069       if (!explained)
6070         {
6071           cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
6072           explained = 1;
6073         }
6074     }
6075   mark_used (fn);
6076
6077   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
6078     return build_unary_op (ADDR_EXPR, fn, 0);
6079   else
6080     {
6081       /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
6082          will mark the function as addressed, but here we must do it
6083          explicitly.  */
6084       mark_addressable (fn);
6085
6086       return fn;
6087     }
6088 }
6089
6090 /* This function will instantiate the type of the expression given in
6091    RHS to match the type of LHSTYPE.  If errors exist, then return
6092    error_mark_node. FLAGS is a bit mask.  If ITF_COMPLAIN is set, then
6093    we complain on errors.  If we are not complaining, never modify rhs,
6094    as overload resolution wants to try many possible instantiations, in
6095    the hope that at least one will work.
6096    
6097    For non-recursive calls, LHSTYPE should be a function, pointer to
6098    function, or a pointer to member function.  */
6099
6100 tree
6101 instantiate_type (lhstype, rhs, flags)
6102      tree lhstype, rhs;
6103      enum instantiate_type_flags flags;
6104 {
6105   int complain = (flags & itf_complain);
6106   int strict = (flags & itf_no_attributes)
6107                ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
6108   int allow_ptrmem = flags & itf_ptrmem_ok;
6109   
6110   flags &= ~itf_ptrmem_ok;
6111   
6112   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
6113     {
6114       if (complain)
6115         error ("not enough type information");
6116       return error_mark_node;
6117     }
6118
6119   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
6120     {
6121       if (comptypes (lhstype, TREE_TYPE (rhs), strict))
6122         return rhs;
6123       if (complain)
6124         cp_error ("argument of type `%T' does not match `%T'",
6125                   TREE_TYPE (rhs), lhstype);
6126       return error_mark_node;
6127     }
6128
6129   /* We don't overwrite rhs if it is an overloaded function.
6130      Copying it would destroy the tree link.  */
6131   if (TREE_CODE (rhs) != OVERLOAD)
6132     rhs = copy_node (rhs);
6133
6134   /* This should really only be used when attempting to distinguish
6135      what sort of a pointer to function we have.  For now, any
6136      arithmetic operation which is not supported on pointers
6137      is rejected as an error.  */
6138
6139   switch (TREE_CODE (rhs))
6140     {
6141     case TYPE_EXPR:
6142     case CONVERT_EXPR:
6143     case SAVE_EXPR:
6144     case CONSTRUCTOR:
6145     case BUFFER_REF:
6146       my_friendly_abort (177);
6147       return error_mark_node;
6148
6149     case INDIRECT_REF:
6150     case ARRAY_REF:
6151       {
6152         tree new_rhs;
6153
6154         new_rhs = instantiate_type (build_pointer_type (lhstype),
6155                                     TREE_OPERAND (rhs, 0), flags);
6156         if (new_rhs == error_mark_node)
6157           return error_mark_node;
6158
6159         TREE_TYPE (rhs) = lhstype;
6160         TREE_OPERAND (rhs, 0) = new_rhs;
6161         return rhs;
6162       }
6163
6164     case NOP_EXPR:
6165       rhs = copy_node (TREE_OPERAND (rhs, 0));
6166       TREE_TYPE (rhs) = unknown_type_node;
6167       return instantiate_type (lhstype, rhs, flags);
6168
6169     case COMPONENT_REF:
6170       return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6171
6172     case OFFSET_REF:
6173       rhs = TREE_OPERAND (rhs, 1);
6174       if (BASELINK_P (rhs))
6175         return instantiate_type (lhstype, TREE_VALUE (rhs),
6176                                  flags | allow_ptrmem);
6177
6178       /* This can happen if we are forming a pointer-to-member for a
6179          member template.  */
6180       my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
6181
6182       /* Fall through.  */
6183
6184     case TEMPLATE_ID_EXPR:
6185       {
6186         tree fns = TREE_OPERAND (rhs, 0);
6187         tree args = TREE_OPERAND (rhs, 1);
6188
6189         return
6190           resolve_address_of_overloaded_function (lhstype,
6191                                                   fns,
6192                                                   complain,
6193                                                   allow_ptrmem,
6194                                                   /*template_only=*/1,
6195                                                   args);
6196       }
6197
6198     case OVERLOAD:
6199       return 
6200         resolve_address_of_overloaded_function (lhstype, 
6201                                                 rhs,
6202                                                 complain,
6203                                                 allow_ptrmem,
6204                                                 /*template_only=*/0,
6205                                                 /*explicit_targs=*/NULL_TREE);
6206
6207     case TREE_LIST:
6208       /* Now we should have a baselink. */
6209       my_friendly_assert (BASELINK_P (rhs), 990412);
6210
6211       return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
6212
6213     case CALL_EXPR:
6214       /* This is too hard for now.  */
6215       my_friendly_abort (183);
6216       return error_mark_node;
6217
6218     case PLUS_EXPR:
6219     case MINUS_EXPR:
6220     case COMPOUND_EXPR:
6221       TREE_OPERAND (rhs, 0)
6222         = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6223       if (TREE_OPERAND (rhs, 0) == error_mark_node)
6224         return error_mark_node;
6225       TREE_OPERAND (rhs, 1)
6226         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6227       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6228         return error_mark_node;
6229
6230       TREE_TYPE (rhs) = lhstype;
6231       return rhs;
6232
6233     case MULT_EXPR:
6234     case TRUNC_DIV_EXPR:
6235     case FLOOR_DIV_EXPR:
6236     case CEIL_DIV_EXPR:
6237     case ROUND_DIV_EXPR:
6238     case RDIV_EXPR:
6239     case TRUNC_MOD_EXPR:
6240     case FLOOR_MOD_EXPR:
6241     case CEIL_MOD_EXPR:
6242     case ROUND_MOD_EXPR:
6243     case FIX_ROUND_EXPR:
6244     case FIX_FLOOR_EXPR:
6245     case FIX_CEIL_EXPR:
6246     case FIX_TRUNC_EXPR:
6247     case FLOAT_EXPR:
6248     case NEGATE_EXPR:
6249     case ABS_EXPR:
6250     case MAX_EXPR:
6251     case MIN_EXPR:
6252     case FFS_EXPR:
6253
6254     case BIT_AND_EXPR:
6255     case BIT_IOR_EXPR:
6256     case BIT_XOR_EXPR:
6257     case LSHIFT_EXPR:
6258     case RSHIFT_EXPR:
6259     case LROTATE_EXPR:
6260     case RROTATE_EXPR:
6261
6262     case PREINCREMENT_EXPR:
6263     case PREDECREMENT_EXPR:
6264     case POSTINCREMENT_EXPR:
6265     case POSTDECREMENT_EXPR:
6266       if (complain)
6267         error ("invalid operation on uninstantiated type");
6268       return error_mark_node;
6269
6270     case TRUTH_AND_EXPR:
6271     case TRUTH_OR_EXPR:
6272     case TRUTH_XOR_EXPR:
6273     case LT_EXPR:
6274     case LE_EXPR:
6275     case GT_EXPR:
6276     case GE_EXPR:
6277     case EQ_EXPR:
6278     case NE_EXPR:
6279     case TRUTH_ANDIF_EXPR:
6280     case TRUTH_ORIF_EXPR:
6281     case TRUTH_NOT_EXPR:
6282       if (complain)
6283         error ("not enough type information");
6284       return error_mark_node;
6285
6286     case COND_EXPR:
6287       if (type_unknown_p (TREE_OPERAND (rhs, 0)))
6288         {
6289           if (complain)
6290             error ("not enough type information");
6291           return error_mark_node;
6292         }
6293       TREE_OPERAND (rhs, 1)
6294         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6295       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6296         return error_mark_node;
6297       TREE_OPERAND (rhs, 2)
6298         = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
6299       if (TREE_OPERAND (rhs, 2) == error_mark_node)
6300         return error_mark_node;
6301
6302       TREE_TYPE (rhs) = lhstype;
6303       return rhs;
6304
6305     case MODIFY_EXPR:
6306       TREE_OPERAND (rhs, 1)
6307         = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
6308       if (TREE_OPERAND (rhs, 1) == error_mark_node)
6309         return error_mark_node;
6310
6311       TREE_TYPE (rhs) = lhstype;
6312       return rhs;
6313       
6314     case ADDR_EXPR:
6315     {
6316       if (PTRMEM_OK_P (rhs))
6317         flags |= itf_ptrmem_ok;
6318       
6319       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
6320     }
6321     case ENTRY_VALUE_EXPR:
6322       my_friendly_abort (184);
6323       return error_mark_node;
6324
6325     case ERROR_MARK:
6326       return error_mark_node;
6327
6328     default:
6329       my_friendly_abort (185);
6330       return error_mark_node;
6331     }
6332 }
6333 \f
6334 /* Return the name of the virtual function pointer field
6335    (as an IDENTIFIER_NODE) for the given TYPE.  Note that
6336    this may have to look back through base types to find the
6337    ultimate field name.  (For single inheritance, these could
6338    all be the same name.  Who knows for multiple inheritance).  */
6339
6340 static tree
6341 get_vfield_name (type)
6342      tree type;
6343 {
6344   tree binfo = TYPE_BINFO (type);
6345   char *buf;
6346
6347   while (BINFO_BASETYPES (binfo)
6348          && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
6349          && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
6350     binfo = BINFO_BASETYPE (binfo, 0);
6351
6352   type = BINFO_TYPE (binfo);
6353   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
6354                          + TYPE_NAME_LENGTH (type) + 2);
6355   sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
6356   return get_identifier (buf);
6357 }
6358
6359 void
6360 print_class_statistics ()
6361 {
6362 #ifdef GATHER_STATISTICS
6363   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
6364   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
6365   fprintf (stderr, "build_method_call = %d (inner = %d)\n",
6366            n_build_method_call, n_inner_fields_searched);
6367   if (n_vtables)
6368     {
6369       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
6370                n_vtables, n_vtable_searches);
6371       fprintf (stderr, "vtable entries = %d; vtable elems = %d\n",
6372                n_vtable_entries, n_vtable_elems);
6373     }
6374 #endif
6375 }
6376
6377 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
6378    according to [class]:
6379                                           The class-name is also inserted
6380    into  the scope of the class itself.  For purposes of access checking,
6381    the inserted class name is treated as if it were a public member name.  */
6382
6383 void
6384 build_self_reference ()
6385 {
6386   tree name = constructor_name (current_class_type);
6387   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
6388   tree saved_cas;
6389
6390   DECL_NONLOCAL (value) = 1;
6391   DECL_CONTEXT (value) = current_class_type;
6392   DECL_ARTIFICIAL (value) = 1;
6393
6394   if (processing_template_decl)
6395     value = push_template_decl (value);
6396
6397   saved_cas = current_access_specifier;
6398   current_access_specifier = access_public_node;
6399   finish_member_declaration (value);
6400   current_access_specifier = saved_cas;
6401 }
6402
6403 /* Returns 1 if TYPE contains only padding bytes.  */
6404
6405 int
6406 is_empty_class (type)
6407      tree type;
6408 {
6409   if (type == error_mark_node)
6410     return 0;
6411
6412   if (! IS_AGGR_TYPE (type))
6413     return 0;
6414
6415   return integer_zerop (CLASSTYPE_SIZE (type));
6416 }
6417
6418 /* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
6419    a *_TYPE node.  NODE can also be a local class.  */
6420
6421 tree
6422 get_enclosing_class (type)
6423      tree type;
6424 {
6425   tree node = type;
6426
6427   while (node && TREE_CODE (node) != NAMESPACE_DECL)
6428     {
6429       switch (TREE_CODE_CLASS (TREE_CODE (node)))
6430         {
6431         case 'd':
6432           node = DECL_CONTEXT (node);
6433           break;
6434
6435         case 't':
6436           if (node != type)
6437             return node;
6438           node = TYPE_CONTEXT (node);
6439           break;
6440
6441         default:
6442           my_friendly_abort (0);
6443         }
6444     }
6445   return NULL_TREE;
6446 }
6447
6448 /* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
6449
6450 int
6451 is_base_of_enclosing_class (base, type)
6452      tree base, type;
6453 {
6454   while (type)
6455     {
6456       if (get_binfo (base, type, 0))
6457         return 1;
6458
6459       type = get_enclosing_class (type);
6460     }
6461   return 0;
6462 }
6463
6464 /* Note that NAME was looked up while the current class was being
6465    defined and that the result of that lookup was DECL.  */
6466
6467 void
6468 maybe_note_name_used_in_class (name, decl)
6469      tree name;
6470      tree decl;
6471 {
6472   splay_tree names_used;
6473
6474   /* If we're not defining a class, there's nothing to do.  */
6475   if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
6476     return;
6477   
6478   /* If there's already a binding for this NAME, then we don't have
6479      anything to worry about.  */
6480   if (IDENTIFIER_CLASS_VALUE (name))
6481     return;
6482
6483   if (!current_class_stack[current_class_depth - 1].names_used)
6484     current_class_stack[current_class_depth - 1].names_used
6485       = splay_tree_new (splay_tree_compare_pointers, 0, 0);
6486   names_used = current_class_stack[current_class_depth - 1].names_used;
6487
6488   splay_tree_insert (names_used,
6489                      (splay_tree_key) name, 
6490                      (splay_tree_value) decl);
6491 }
6492
6493 /* Note that NAME was declared (as DECL) in the current class.  Check
6494    to see that the declaration is legal.  */
6495
6496 void
6497 note_name_declared_in_class (name, decl)
6498      tree name;
6499      tree decl;
6500 {
6501   splay_tree names_used;
6502   splay_tree_node n;
6503
6504   /* Look to see if we ever used this name.  */
6505   names_used 
6506     = current_class_stack[current_class_depth - 1].names_used;
6507   if (!names_used)
6508     return;
6509
6510   n = splay_tree_lookup (names_used, (splay_tree_key) name);
6511   if (n)
6512     {
6513       /* [basic.scope.class]
6514          
6515          A name N used in a class S shall refer to the same declaration
6516          in its context and when re-evaluated in the completed scope of
6517          S.  */
6518       cp_error ("declaration of `%#D'", decl);
6519       cp_error_at ("changes meaning of `%D' from `%+#D'", 
6520                    DECL_NAME (OVL_CURRENT (decl)),
6521                    (tree) n->value);
6522     }
6523 }
6524
6525 /* Returns the VAR_DECL for the complete vtable associated with BINFO.
6526    Secondary vtables are merged with primary vtables; this function
6527    will return the VAR_DECL for the primary vtable.  */
6528
6529 tree
6530 get_vtbl_decl_for_binfo (binfo)
6531      tree binfo;
6532 {
6533   tree decl;
6534
6535   decl = BINFO_VTABLE (binfo);
6536   if (decl && TREE_CODE (decl) == PLUS_EXPR)
6537     {
6538       my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
6539                           2000403);
6540       decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
6541     }
6542   if (decl)
6543     my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
6544   return decl;
6545 }
6546
6547 /* Called from get_primary_binfo via dfs_walk.  DATA is a TREE_LIST
6548    who's TREE_PURPOSE is the TYPE of the required primary base and
6549    who's TREE_VALUE is a list of candidate binfos that we fill in. */
6550
6551 static tree
6552 dfs_get_primary_binfo (binfo, data)
6553      tree binfo;
6554      void *data;
6555 {
6556   tree cons = (tree) data;
6557   tree primary_base = TREE_PURPOSE (cons);
6558
6559   if (TREE_VIA_VIRTUAL (binfo) 
6560       && same_type_p (BINFO_TYPE (binfo), primary_base))
6561     /* This is the right type of binfo, but it might be an unshared
6562        instance, and the shared instance is later in the dfs walk.  We
6563        must keep looking.  */
6564     TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
6565   
6566   return NULL_TREE;
6567 }
6568
6569 /* Returns the unshared binfo for the primary base of BINFO.  Note
6570    that in a complex hierarchy the resulting BINFO may not actually
6571    *be* primary.  In particular if the resulting BINFO is a virtual
6572    base, and it occurs elsewhere in the hierarchy, then this
6573    occurrence may not actually be a primary base in the complete
6574    object.  Check BINFO_PRIMARY_P to be sure.  */
6575
6576 tree
6577 get_primary_binfo (binfo)
6578      tree binfo;
6579 {
6580   tree primary_base;
6581   tree result = NULL_TREE;
6582   tree virtuals;
6583   
6584   primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
6585   if (!primary_base)
6586     return NULL_TREE;
6587
6588   /* A non-virtual primary base is always a direct base, and easy to
6589      find.  */
6590   if (!TREE_VIA_VIRTUAL (primary_base))
6591     {
6592       int i;
6593
6594       /* Scan the direct basetypes until we find a base with the same
6595          type as the primary base.  */
6596       for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6597         {
6598           tree base_binfo = BINFO_BASETYPE (binfo, i);
6599           
6600           if (same_type_p (BINFO_TYPE (base_binfo),
6601                            BINFO_TYPE (primary_base)))
6602             return base_binfo;
6603         }
6604
6605       /* We should always find the primary base.  */
6606       my_friendly_abort (20000729);
6607     }
6608
6609   /* For a primary virtual base, we have to scan the entire hierarchy
6610      rooted at BINFO; the virtual base could be an indirect virtual
6611      base.  There could be more than one instance of the primary base
6612      in the hierarchy, and if one is the canonical binfo we want that
6613      one.  If it exists, it should be the first one we find, but as a
6614      consistency check we find them all and make sure.  */
6615   virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
6616   dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
6617   virtuals = TREE_VALUE (virtuals);
6618   
6619   /* We must have found at least one instance.  */
6620   my_friendly_assert (virtuals, 20010612);
6621
6622   if (TREE_CHAIN (virtuals))
6623     {
6624       /* We found more than one instance of the base. We must make
6625          sure that, if one is the canonical one, it is the first one
6626          we found. As the chain is in reverse dfs order, that means
6627          the last on the list.  */
6628       tree complete_binfo;
6629       tree canonical;
6630       
6631       for (complete_binfo = binfo;
6632            BINFO_INHERITANCE_CHAIN (complete_binfo);
6633            complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
6634         continue;
6635       canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
6636                                    BINFO_TYPE (complete_binfo));
6637       
6638       for (; virtuals; virtuals = TREE_CHAIN (virtuals))
6639         {
6640           result = TREE_VALUE (virtuals);
6641
6642           if (canonical == result)
6643             {
6644               /* This is the unshared instance. Make sure it was the
6645                  first one found.  */
6646               my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
6647               break;
6648             }
6649         }
6650     }
6651   else
6652     result = TREE_VALUE (virtuals);
6653   return result;
6654 }
6655
6656 /* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
6657
6658 static int
6659 maybe_indent_hierarchy (stream, indent, indented_p)
6660      FILE *stream;
6661      int indent;
6662      int indented_p;
6663 {
6664   if (!indented_p)
6665     fprintf (stream, "%*s", indent, "");
6666   return 1;
6667 }
6668
6669 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
6670    dominated by T) to stderr.  INDENT should be zero when called from
6671    the top level; it is incremented recursively.  */
6672
6673 static void
6674 dump_class_hierarchy_r (stream, flags, t, binfo, indent)
6675      FILE *stream;
6676      int flags;
6677      tree t;
6678      tree binfo;
6679      int indent;
6680 {
6681   int i;
6682   int indented = 0;
6683   
6684   indented = maybe_indent_hierarchy (stream, indent, 0);
6685   fprintf (stream, "%s (0x%lx) ",
6686            type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
6687            (unsigned long) binfo);
6688   fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
6689            tree_low_cst (BINFO_OFFSET (binfo), 0));
6690   if (is_empty_class (BINFO_TYPE (binfo)))
6691     fprintf (stream, " empty");
6692   else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
6693     fprintf (stream, " nearly-empty");
6694   if (TREE_VIA_VIRTUAL (binfo))
6695     {
6696       tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
6697
6698       fprintf (stream, " virtual");
6699       if (canonical == binfo)
6700         fprintf (stream, " canonical");
6701       else
6702         fprintf (stream, " non-canonical");
6703     }
6704   fprintf (stream, "\n");
6705
6706   indented = 0;
6707   if (BINFO_PRIMARY_BASE_OF (binfo))
6708     {
6709       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6710       fprintf (stream, " primary-for %s (0x%lx)",
6711                type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
6712                                TFF_PLAIN_IDENTIFIER),
6713                (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
6714     }
6715   if (BINFO_LOST_PRIMARY_P (binfo))
6716     {
6717       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6718       fprintf (stream, " lost-primary");
6719     }
6720   if (indented)
6721     fprintf (stream, "\n");
6722
6723   if (!(flags & TDF_SLIM))
6724     {
6725       int indented = 0;
6726       
6727       if (BINFO_SUBVTT_INDEX (binfo))
6728         {
6729           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6730           fprintf (stream, " subvttidx=%s",
6731                    expr_as_string (BINFO_SUBVTT_INDEX (binfo),
6732                                    TFF_PLAIN_IDENTIFIER));
6733         }
6734       if (BINFO_VPTR_INDEX (binfo))
6735         {
6736           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6737           fprintf (stream, " vptridx=%s",
6738                    expr_as_string (BINFO_VPTR_INDEX (binfo),
6739                                    TFF_PLAIN_IDENTIFIER));
6740         }
6741       if (BINFO_VPTR_FIELD (binfo))
6742         {
6743           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6744           fprintf (stream, " vbaseoffset=%s",
6745                    expr_as_string (BINFO_VPTR_FIELD (binfo),
6746                                    TFF_PLAIN_IDENTIFIER));
6747         }
6748       if (BINFO_VTABLE (binfo))
6749         {
6750           indented = maybe_indent_hierarchy (stream, indent + 3, indented);
6751           fprintf (stream, " vptr=%s",
6752                    expr_as_string (BINFO_VTABLE (binfo),
6753                                    TFF_PLAIN_IDENTIFIER));
6754         }
6755       
6756       if (indented)
6757         fprintf (stream, "\n");
6758     }
6759   
6760
6761   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
6762     dump_class_hierarchy_r (stream, flags,
6763                             t, BINFO_BASETYPE (binfo, i),
6764                             indent + 2);
6765 }
6766
6767 /* Dump the BINFO hierarchy for T.  */
6768
6769 static void
6770 dump_class_hierarchy (t)
6771      tree t;
6772 {
6773   int flags;
6774   FILE *stream = dump_begin (TDI_class, &flags);
6775
6776   if (!stream)
6777     return;
6778   
6779   fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6780   fprintf (stream, "   size=%lu align=%lu\n",
6781            (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
6782            (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
6783   dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
6784   fprintf (stream, "\n");
6785   dump_end (TDI_class, stream);
6786 }
6787
6788 static void
6789 dump_array (stream, decl)
6790      FILE *stream;
6791      tree decl;
6792 {
6793   tree inits;
6794   int ix;
6795   HOST_WIDE_INT elt;
6796   tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
6797
6798   elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
6799          / BITS_PER_UNIT);
6800   fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
6801   fprintf (stream, " %s entries",
6802            expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
6803                            TFF_PLAIN_IDENTIFIER));
6804   fprintf (stream, "\n");
6805
6806   for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
6807        inits; ix++, inits = TREE_CHAIN (inits))
6808     fprintf (stream, "%-4d  %s\n", ix * elt,
6809              expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
6810 }
6811
6812 static void
6813 dump_vtable (t, binfo, vtable)
6814      tree t;
6815      tree binfo;
6816      tree vtable;
6817 {
6818   int flags;
6819   FILE *stream = dump_begin (TDI_class, &flags);
6820
6821   if (!stream)
6822     return;
6823
6824   if (!(flags & TDF_SLIM))
6825     {
6826       int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
6827       
6828       fprintf (stream, "%s for %s",
6829                ctor_vtbl_p ? "Construction vtable" : "Vtable",
6830                type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
6831       if (ctor_vtbl_p)
6832         {
6833           if (!TREE_VIA_VIRTUAL (binfo))
6834             fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
6835           fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
6836         }
6837       fprintf (stream, "\n");
6838       dump_array (stream, vtable);
6839       fprintf (stream, "\n");
6840     }
6841   
6842   dump_end (TDI_class, stream);
6843 }
6844
6845 static void
6846 dump_vtt (t, vtt)
6847      tree t;
6848      tree vtt;
6849 {
6850   int flags;
6851   FILE *stream = dump_begin (TDI_class, &flags);
6852
6853   if (!stream)
6854     return;
6855
6856   if (!(flags & TDF_SLIM))
6857     {
6858       fprintf (stream, "VTT for %s\n",
6859                type_as_string (t, TFF_PLAIN_IDENTIFIER));
6860       dump_array (stream, vtt);
6861       fprintf (stream, "\n");
6862     }
6863   
6864   dump_end (TDI_class, stream);
6865 }
6866
6867 /* Virtual function table initialization.  */
6868
6869 /* Create all the necessary vtables for T and its base classes.  */
6870
6871 static void
6872 finish_vtbls (t)
6873      tree t;
6874 {
6875   tree list;
6876   tree vbase;
6877
6878   /* We lay out the primary and secondary vtables in one contiguous
6879      vtable.  The primary vtable is first, followed by the non-virtual
6880      secondary vtables in inheritance graph order.  */
6881   list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
6882   accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
6883                          TYPE_BINFO (t), t, list);
6884   
6885   /* Then come the virtual bases, also in inheritance graph order.  */
6886   for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
6887     {
6888       tree real_base;
6889           
6890       if (!TREE_VIA_VIRTUAL (vbase))
6891         continue;
6892           
6893       /* Although we walk in inheritance order, that might not get the
6894          canonical base.  */
6895       real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
6896           
6897       accumulate_vtbl_inits (real_base, real_base,
6898                              TYPE_BINFO (t), t, list);
6899     }
6900
6901   if (TYPE_BINFO_VTABLE (t))
6902     initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
6903 }
6904
6905 /* Initialize the vtable for BINFO with the INITS.  */
6906
6907 static void
6908 initialize_vtable (binfo, inits)
6909      tree binfo;
6910      tree inits;
6911 {
6912   tree decl;
6913
6914   layout_vtable_decl (binfo, list_length (inits));
6915   decl = get_vtbl_decl_for_binfo (binfo);
6916   initialize_array (decl, inits);
6917   dump_vtable (BINFO_TYPE (binfo), binfo, decl);
6918 }
6919
6920 /* Initialize DECL (a declaration for a namespace-scope array) with
6921    the INITS.  */
6922
6923 static void
6924 initialize_array (decl, inits)
6925   tree decl;
6926   tree inits;
6927 {
6928   tree context;
6929
6930   context = DECL_CONTEXT (decl);
6931   DECL_CONTEXT (decl) = NULL_TREE;
6932   DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
6933   cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
6934   DECL_CONTEXT (decl) = context;
6935 }
6936
6937 /* Build the VTT (virtual table table) for T.
6938    A class requires a VTT if it has virtual bases.
6939    
6940    This holds
6941    1 - primary virtual pointer for complete object T
6942    2 - secondary VTTs for each direct non-virtual base of T which requires a
6943        VTT
6944    3 - secondary virtual pointers for each direct or indirect base of T which
6945        has virtual bases or is reachable via a virtual path from T.
6946    4 - secondary VTTs for each direct or indirect virtual base of T.
6947    
6948    Secondary VTTs look like complete object VTTs without part 4.  */
6949
6950 static void
6951 build_vtt (t)
6952      tree t;
6953 {
6954   tree inits;
6955   tree type;
6956   tree vtt;
6957   tree index;
6958
6959   /* Build up the initializers for the VTT.  */
6960   inits = NULL_TREE;
6961   index = size_zero_node;
6962   build_vtt_inits (TYPE_BINFO (t), t, &inits, &index);
6963
6964   /* If we didn't need a VTT, we're done.  */
6965   if (!inits)
6966     return;
6967
6968   /* Figure out the type of the VTT.  */
6969   type = build_index_type (size_int (list_length (inits) - 1));
6970   type = build_cplus_array_type (const_ptr_type_node, type);
6971                                  
6972   /* Now, build the VTT object itself.  */
6973   vtt = build_vtable (t, get_vtt_name (t), type);
6974   pushdecl_top_level (vtt);
6975   initialize_array (vtt, inits);
6976
6977   dump_vtt (t, vtt);
6978 }
6979
6980 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
6981    from within some heirarchy which is inherited from the type of BINFO.
6982    Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
6983    BINFO.  */
6984
6985 static tree
6986 get_original_base (base_binfo, binfo)
6987      tree base_binfo;
6988      tree binfo;
6989 {
6990   tree derived;
6991   int ix;
6992   
6993   if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
6994     return binfo;
6995   if (TREE_VIA_VIRTUAL (base_binfo))
6996     return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
6997   derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
6998   
6999   for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
7000     if (same_type_p (BINFO_TYPE (base_binfo),
7001                      BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
7002       return BINFO_BASETYPE (derived, ix);
7003   my_friendly_abort (20010223);
7004   return NULL;
7005 }
7006
7007 /* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
7008    PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
7009    and CHAIN the vtable pointer for this binfo after construction is
7010    complete.  VALUE can also be another BINFO, in which case we recurse. */
7011
7012 static tree
7013 binfo_ctor_vtable (binfo)
7014      tree binfo;
7015 {
7016   tree vt;
7017
7018   while (1)
7019     {
7020       vt = BINFO_VTABLE (binfo);
7021       if (TREE_CODE (vt) == TREE_LIST)
7022         vt = TREE_VALUE (vt);
7023       if (TREE_CODE (vt) == TREE_VEC)
7024         binfo = vt;
7025       else
7026         break;
7027     }
7028
7029   return vt;
7030 }
7031
7032 /* Recursively build the VTT-initializer for BINFO (which is in the
7033    hierarchy dominated by T).  INITS points to the end of the initializer
7034    list to date.  INDEX is the VTT index where the next element will be
7035    replaced.  Iff BINFO is the binfo for T, this is the top level VTT (i.e.
7036    not a subvtt for some base of T).  When that is so, we emit the sub-VTTs
7037    for virtual bases of T. When it is not so, we build the constructor
7038    vtables for the BINFO-in-T variant.  */
7039
7040 static tree *
7041 build_vtt_inits (binfo, t, inits, index)
7042      tree binfo;
7043      tree t;
7044      tree *inits;
7045      tree *index;
7046 {
7047   int i;
7048   tree b;
7049   tree init;
7050   tree secondary_vptrs;
7051   int top_level_p = same_type_p (TREE_TYPE (binfo), t);
7052
7053   /* We only need VTTs for subobjects with virtual bases.  */
7054   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7055     return inits;
7056
7057   /* We need to use a construction vtable if this is not the primary
7058      VTT.  */
7059   if (!top_level_p)
7060     {
7061       build_ctor_vtbl_group (binfo, t);
7062
7063       /* Record the offset in the VTT where this sub-VTT can be found.  */
7064       BINFO_SUBVTT_INDEX (binfo) = *index;
7065     }
7066
7067   /* Add the address of the primary vtable for the complete object.  */
7068   init = binfo_ctor_vtable (binfo);
7069   *inits = build_tree_list (NULL_TREE, init);
7070   inits = &TREE_CHAIN (*inits);
7071   if (top_level_p)
7072     {
7073       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7074       BINFO_VPTR_INDEX (binfo) = *index;
7075     }
7076   *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
7077                        
7078   /* Recursively add the secondary VTTs for non-virtual bases.  */
7079   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7080     {
7081       b = BINFO_BASETYPE (binfo, i);
7082       if (!TREE_VIA_VIRTUAL (b))
7083         inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t, 
7084                                  inits, index);
7085     }
7086       
7087   /* Add secondary virtual pointers for all subobjects of BINFO with
7088      either virtual bases or reachable along a virtual path, except
7089      subobjects that are non-virtual primary bases.  */
7090   secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
7091   TREE_TYPE (secondary_vptrs) = *index;
7092   VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
7093   VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
7094   
7095   dfs_walk_real (binfo,
7096                  dfs_build_secondary_vptr_vtt_inits,
7097                  NULL,
7098                  dfs_ctor_vtable_bases_queue_p,
7099                  secondary_vptrs);
7100   VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
7101   dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
7102             secondary_vptrs);
7103
7104   *index = TREE_TYPE (secondary_vptrs);
7105
7106   /* The secondary vptrs come back in reverse order.  After we reverse
7107      them, and add the INITS, the last init will be the first element
7108      of the chain.  */
7109   secondary_vptrs = TREE_VALUE (secondary_vptrs);
7110   if (secondary_vptrs)
7111     {
7112       *inits = nreverse (secondary_vptrs);
7113       inits = &TREE_CHAIN (secondary_vptrs);
7114       my_friendly_assert (*inits == NULL_TREE, 20000517);
7115     }
7116
7117   /* Add the secondary VTTs for virtual bases.  */
7118   if (top_level_p)
7119     for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
7120       {
7121         tree vbase;
7122         
7123         if (!TREE_VIA_VIRTUAL (b))
7124           continue;
7125         
7126         vbase = binfo_for_vbase (BINFO_TYPE (b), t);
7127         inits = build_vtt_inits (vbase, t, inits, index);
7128       }
7129
7130   if (!top_level_p)
7131     {
7132       tree data = tree_cons (t, binfo, NULL_TREE);
7133       VTT_TOP_LEVEL_P (data) = 0;
7134       VTT_MARKED_BINFO_P (data) = 0;
7135       
7136       dfs_walk (binfo, dfs_fixup_binfo_vtbls,
7137                 dfs_ctor_vtable_bases_queue_p,
7138                 data);
7139     }
7140
7141   return inits;
7142 }
7143
7144 /* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo
7145    for the base in most derived. DATA is a TREE_LIST who's
7146    TREE_CHAIN is the type of the base being
7147    constructed whilst this secondary vptr is live.  The TREE_UNSIGNED
7148    flag of DATA indicates that this is a constructor vtable.  The
7149    TREE_TOP_LEVEL flag indicates that this is the primary VTT.  */
7150
7151 static tree
7152 dfs_build_secondary_vptr_vtt_inits (binfo, data)
7153      tree binfo;
7154      void *data;
7155 {
7156   tree l; 
7157   tree t;
7158   tree init;
7159   tree index;
7160   int top_level_p;
7161
7162   l = (tree) data;
7163   t = TREE_CHAIN (l);
7164   top_level_p = VTT_TOP_LEVEL_P (l);
7165   
7166   SET_BINFO_MARKED (binfo);
7167
7168   /* We don't care about bases that don't have vtables.  */
7169   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7170     return NULL_TREE;
7171
7172   /* We're only interested in proper subobjects of T.  */
7173   if (same_type_p (BINFO_TYPE (binfo), t))
7174     return NULL_TREE;
7175
7176   /* We're not interested in non-virtual primary bases.  */
7177   if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
7178     return NULL_TREE;
7179
7180   /* If BINFO has virtual bases or is reachable via a virtual path
7181      from T, it'll have a secondary vptr.  */
7182   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7183       && !binfo_via_virtual (binfo, t))
7184     return NULL_TREE;
7185
7186   /* Record the index where this secondary vptr can be found.  */
7187   index = TREE_TYPE (l);
7188   if (top_level_p)
7189     {
7190       my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
7191       BINFO_VPTR_INDEX (binfo) = index;
7192     }
7193   TREE_TYPE (l) = size_binop (PLUS_EXPR, index, 
7194                               TYPE_SIZE_UNIT (ptr_type_node));
7195
7196   /* Add the initializer for the secondary vptr itself.  */
7197   if (top_level_p && TREE_VIA_VIRTUAL (binfo))
7198     {
7199       /* It's a primary virtual base, and this is not the construction
7200          vtable. Find the base this is primary of in the inheritance graph,
7201          and use that base's vtable now. */
7202       while (BINFO_PRIMARY_BASE_OF (binfo))
7203         binfo = BINFO_PRIMARY_BASE_OF (binfo);
7204     }
7205   init = binfo_ctor_vtable (binfo);
7206   TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
7207
7208   return NULL_TREE;
7209 }
7210
7211 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
7212    VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
7213    should be walked.  TREE_PURPOSE is the TREE_TYPE that dominates the
7214    hierarchy.  */
7215
7216 static tree
7217 dfs_ctor_vtable_bases_queue_p (binfo, data)
7218      tree binfo;
7219      void *data;
7220 {
7221   if (TREE_VIA_VIRTUAL (binfo))
7222      /* Get the shared version.  */
7223     binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
7224
7225   if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
7226     return NULL_TREE;
7227   return binfo;
7228 }
7229
7230 /* Called from build_vtt_inits via dfs_walk. After building constructor
7231    vtables and generating the sub-vtt from them, we need to restore the
7232    BINFO_VTABLES that were scribbled on.  DATA is a TREE_LIST whose
7233    TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated.  */
7234
7235 static tree
7236 dfs_fixup_binfo_vtbls (binfo, data)
7237      tree binfo;
7238      void *data;
7239 {
7240   CLEAR_BINFO_MARKED (binfo);
7241
7242   /* We don't care about bases that don't have vtables.  */
7243   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
7244     return NULL_TREE;
7245
7246   /* If we scribbled the construction vtable vptr into BINFO, clear it
7247      out now.  */
7248   if (BINFO_VTABLE (binfo)
7249       && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
7250       && (TREE_PURPOSE (BINFO_VTABLE (binfo)) 
7251           == TREE_VALUE ((tree) data)))
7252     BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
7253
7254   return NULL_TREE;
7255 }
7256
7257 /* Build the construction vtable group for BINFO which is in the
7258    hierarchy dominated by T.  */
7259
7260 static void
7261 build_ctor_vtbl_group (binfo, t)
7262      tree binfo;
7263      tree t;
7264 {
7265   tree list;
7266   tree type;
7267   tree vtbl;
7268   tree inits;
7269   tree id;
7270   tree vbase;
7271
7272   /* See if we've already created this construction vtable group.  */
7273   id = mangle_ctor_vtbl_for_type (t, binfo);
7274   if (IDENTIFIER_GLOBAL_VALUE (id))
7275     return;
7276
7277   my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
7278   /* Build a version of VTBL (with the wrong type) for use in
7279      constructing the addresses of secondary vtables in the
7280      construction vtable group.  */
7281   vtbl = build_vtable (t, id, ptr_type_node);
7282   list = build_tree_list (vtbl, NULL_TREE);
7283   accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
7284                          binfo, t, list);
7285
7286   /* Add the vtables for each of our virtual bases using the vbase in T
7287      binfo.  */
7288   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 
7289        vbase; 
7290        vbase = TREE_CHAIN (vbase))
7291     {
7292       tree b;
7293       tree orig_base;
7294
7295       if (!TREE_VIA_VIRTUAL (vbase))
7296         continue;
7297       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7298       orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
7299       
7300       accumulate_vtbl_inits (b, orig_base, binfo, t, list);
7301     }
7302   inits = TREE_VALUE (list);
7303
7304   /* Figure out the type of the construction vtable.  */
7305   type = build_index_type (size_int (list_length (inits) - 1));
7306   type = build_cplus_array_type (vtable_entry_type, type);
7307   TREE_TYPE (vtbl) = type;
7308
7309   /* Initialize the construction vtable.  */
7310   pushdecl_top_level (vtbl);
7311   initialize_array (vtbl, inits);
7312   dump_vtable (t, binfo, vtbl);
7313 }
7314
7315 /* Add the vtbl initializers for BINFO (and its bases other than
7316    non-virtual primaries) to the list of INITS.  BINFO is in the
7317    hierarchy dominated by T.  RTTI_BINFO is the binfo within T of
7318    the constructor the vtbl inits should be accumulated for. (If this
7319    is the complete object vtbl then RTTI_BINFO will be TYPE_BINFO (T).)
7320    ORIG_BINFO is the binfo for this object within BINFO_TYPE (RTTI_BINFO).
7321    BINFO is the active base equivalent of ORIG_BINFO in the inheritance
7322    graph of T. Both BINFO and ORIG_BINFO will have the same BINFO_TYPE,
7323    but are not necessarily the same in terms of layout.  */
7324
7325 static void
7326 accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
7327      tree binfo;
7328      tree orig_binfo;
7329      tree rtti_binfo;
7330      tree t;
7331      tree inits;
7332 {
7333   int i;
7334   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7335
7336   my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
7337                                    BINFO_TYPE (orig_binfo)),
7338                       20000517);
7339
7340   /* If it doesn't have a vptr, we don't do anything. */
7341   if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
7342     return;
7343   
7344   /* If we're building a construction vtable, we're not interested in
7345      subobjects that don't require construction vtables.  */
7346   if (ctor_vtbl_p 
7347       && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
7348       && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
7349     return;
7350
7351   /* Build the initializers for the BINFO-in-T vtable.  */
7352   TREE_VALUE (inits) 
7353     = chainon (TREE_VALUE (inits),
7354                dfs_accumulate_vtbl_inits (binfo, orig_binfo,
7355                                           rtti_binfo, t, inits));
7356                       
7357   /* Walk the BINFO and its bases.  We walk in preorder so that as we
7358      initialize each vtable we can figure out at what offset the
7359      secondary vtable lies from the primary vtable.  We can't use
7360      dfs_walk here because we need to iterate through bases of BINFO
7361      and RTTI_BINFO simultaneously.  */
7362   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
7363     {
7364       tree base_binfo = BINFO_BASETYPE (binfo, i);
7365       
7366       /* Skip virtual bases.  */
7367       if (TREE_VIA_VIRTUAL (base_binfo))
7368         continue;
7369       accumulate_vtbl_inits (base_binfo,
7370                              BINFO_BASETYPE (orig_binfo, i),
7371                              rtti_binfo, t,
7372                              inits);
7373     }
7374 }
7375
7376 /* Called from accumulate_vtbl_inits.  Returns the initializers for
7377    the BINFO vtable.  */
7378
7379 static tree
7380 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
7381      tree binfo;
7382      tree orig_binfo;
7383      tree rtti_binfo;
7384      tree t;
7385      tree l;
7386 {
7387   tree inits = NULL_TREE;
7388   tree vtbl = NULL_TREE;
7389   int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7390
7391   if (ctor_vtbl_p
7392       && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
7393     {
7394       /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
7395          primary virtual base.  If it is not the same primary in
7396          the hierarchy of T, we'll need to generate a ctor vtable
7397          for it, to place at its location in T.  If it is the same
7398          primary, we still need a VTT entry for the vtable, but it
7399          should point to the ctor vtable for the base it is a
7400          primary for within the sub-hierarchy of RTTI_BINFO.
7401               
7402          There are three possible cases:
7403               
7404          1) We are in the same place.
7405          2) We are a primary base within a lost primary virtual base of
7406          RTTI_BINFO.
7407          3) We are primary to something not a base of RTTI_BINFO.  */
7408           
7409       tree b = BINFO_PRIMARY_BASE_OF (binfo);
7410       tree last = NULL_TREE;
7411
7412       /* First, look through the bases we are primary to for RTTI_BINFO
7413          or a virtual base.  */
7414       for (; b; b = BINFO_PRIMARY_BASE_OF (b))
7415         {
7416           last = b;
7417           if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7418             break;
7419         }
7420       /* If we run out of primary links, keep looking down our
7421          inheritance chain; we might be an indirect primary.  */
7422       if (b == NULL_TREE)
7423         for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
7424           if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
7425             break;
7426
7427       /* If we found RTTI_BINFO, this is case 1.  If we found a virtual
7428          base B and it is a base of RTTI_BINFO, this is case 2.  In
7429          either case, we share our vtable with LAST, i.e. the
7430          derived-most base within B of which we are a primary.  */
7431       if (b == rtti_binfo
7432           || (b && binfo_for_vbase (BINFO_TYPE (b),
7433                                     BINFO_TYPE (rtti_binfo))))
7434         /* Just set our BINFO_VTABLE to point to LAST, as we may not have
7435            set LAST's BINFO_VTABLE yet.  We'll extract the actual vptr in
7436            binfo_ctor_vtable after everything's been set up.  */
7437         vtbl = last;
7438
7439       /* Otherwise, this is case 3 and we get our own.  */
7440     }
7441   else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
7442     return inits;
7443
7444   if (!vtbl)
7445     {
7446       tree index;
7447       int non_fn_entries;
7448
7449       /* Compute the initializer for this vtable.  */
7450       inits = build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo,
7451                                       &non_fn_entries);
7452
7453       /* Figure out the position to which the VPTR should point.  */
7454       vtbl = TREE_PURPOSE (l);
7455       vtbl = build1 (ADDR_EXPR, 
7456                      vtbl_ptr_type_node,
7457                      vtbl);
7458       TREE_CONSTANT (vtbl) = 1;
7459       index = size_binop (PLUS_EXPR,
7460                           size_int (non_fn_entries),
7461                           size_int (list_length (TREE_VALUE (l))));
7462       index = size_binop (MULT_EXPR,
7463                           TYPE_SIZE_UNIT (vtable_entry_type),
7464                           index);
7465       vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
7466       TREE_CONSTANT (vtbl) = 1;
7467     }
7468
7469   if (ctor_vtbl_p)
7470     /* For a construction vtable, we can't overwrite BINFO_VTABLE.
7471        So, we make a TREE_LIST.  Later, dfs_fixup_binfo_vtbls will
7472        straighten this out.  */
7473     BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
7474   else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
7475     inits = NULL_TREE;
7476   else
7477      /* For an ordinary vtable, set BINFO_VTABLE.  */
7478     BINFO_VTABLE (binfo) = vtbl;
7479
7480   return inits;
7481 }
7482
7483 /* Construct the initializer for BINFO's virtual function table.  BINFO
7484    is part of the hierarchy dominated by T.  If we're building a
7485    construction vtable, the ORIG_BINFO is the binfo we should use to
7486    find the actual function pointers to put in the vtable - but they
7487    can be overridden on the path to most-derived in the graph that
7488    ORIG_BINFO belongs.  Otherwise,
7489    ORIG_BINFO should be the same as BINFO.  The RTTI_BINFO is the
7490    BINFO that should be indicated by the RTTI information in the
7491    vtable; it will be a base class of T, rather than T itself, if we
7492    are building a construction vtable.
7493
7494    The value returned is a TREE_LIST suitable for wrapping in a
7495    CONSTRUCTOR to use as the DECL_INITIAL for a vtable.  If
7496    NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
7497    number of non-function entries in the vtable.  
7498
7499    It might seem that this function should never be called with a
7500    BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
7501    base is always subsumed by a derived class vtable.  However, when
7502    we are building construction vtables, we do build vtables for
7503    primary bases; we need these while the primary base is being
7504    constructed.  */
7505
7506 static tree
7507 build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
7508      tree binfo;
7509      tree orig_binfo;
7510      tree t;
7511      tree rtti_binfo;
7512      int *non_fn_entries_p;
7513 {
7514   tree v, b;
7515   tree vfun_inits;
7516   tree vbase;
7517   vtbl_init_data vid;
7518
7519   /* Initialize VID.  */
7520   memset (&vid, 0, sizeof (vid));
7521   vid.binfo = binfo;
7522   vid.derived = t;
7523   vid.rtti_binfo = rtti_binfo;
7524   vid.last_init = &vid.inits;
7525   vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
7526   vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
7527   /* The first vbase or vcall offset is at index -3 in the vtable.  */
7528   vid.index = ssize_int (-3);
7529
7530   /* Add entries to the vtable for RTTI.  */
7531   build_rtti_vtbl_entries (binfo, &vid);
7532
7533   /* Create an array for keeping track of the functions we've
7534      processed.  When we see multiple functions with the same
7535      signature, we share the vcall offsets.  */
7536   VARRAY_TREE_INIT (vid.fns, 32, "fns");
7537   /* Add the vcall and vbase offset entries.  */
7538   build_vcall_and_vbase_vtbl_entries (binfo, &vid);
7539   /* Clean up.  */
7540   VARRAY_FREE (vid.fns);
7541   /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
7542      build_vbase_offset_vtbl_entries.  */
7543   for (vbase = CLASSTYPE_VBASECLASSES (t); 
7544        vbase; 
7545        vbase = TREE_CHAIN (vbase))
7546     CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
7547
7548   if (non_fn_entries_p)
7549     *non_fn_entries_p = list_length (vid.inits);
7550
7551   /* Go through all the ordinary virtual functions, building up
7552      initializers.  */
7553   vfun_inits = NULL_TREE;
7554   for (v = BINFO_VIRTUALS (orig_binfo); v; v = TREE_CHAIN (v))
7555     {
7556       tree delta;
7557       tree vcall_index;
7558       tree fn;
7559       tree pfn;
7560       tree init;
7561       
7562       /* Pull the offset for `this', and the function to call, out of
7563          the list.  */
7564       delta = BV_DELTA (v);
7565
7566       if (BV_USE_VCALL_INDEX_P (v))
7567         {
7568           vcall_index = BV_VCALL_INDEX (v);
7569           my_friendly_assert (vcall_index != NULL_TREE, 20000621);
7570         }
7571       else
7572         vcall_index = NULL_TREE;
7573
7574       fn = BV_FN (v);
7575       my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
7576       my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
7577
7578       /* You can't call an abstract virtual function; it's abstract.
7579          So, we replace these functions with __pure_virtual.  */
7580       if (DECL_PURE_VIRTUAL_P (fn))
7581         fn = abort_fndecl;
7582
7583       /* Take the address of the function, considering it to be of an
7584          appropriate generic type.  */
7585       pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
7586       /* The address of a function can't change.  */
7587       TREE_CONSTANT (pfn) = 1;
7588
7589       /* Enter it in the vtable.  */
7590       init = build_vtable_entry (delta, vcall_index, pfn);
7591
7592       /* If the only definition of this function signature along our
7593          primary base chain is from a lost primary, this vtable slot will
7594          never be used, so just zero it out.  This is important to avoid
7595          requiring extra thunks which cannot be generated with the function.
7596
7597          We could also handle this in update_vtable_entry_for_fn; doing it
7598          here means we zero out unused slots in ctor vtables as well,
7599          rather than filling them with erroneous values (though harmless,
7600          apart from relocation costs).  */
7601       if (fn != abort_fndecl)
7602         for (b = binfo; ; b = get_primary_binfo (b))
7603           {
7604             /* We found a defn before a lost primary; go ahead as normal.  */
7605             if (look_for_overrides_here (BINFO_TYPE (b), fn))
7606               break;
7607
7608             /* The nearest definition is from a lost primary; clear the
7609                slot.  */
7610             if (BINFO_LOST_PRIMARY_P (b))
7611               {
7612                 init = size_zero_node;
7613                 break;
7614               }
7615           }
7616
7617       /* And add it to the chain of initializers.  */
7618       if (TARGET_VTABLE_USES_DESCRIPTORS)
7619         {
7620           int i;
7621           if (init == size_zero_node)
7622             for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7623               vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7624           else
7625             for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
7626               {
7627                 tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
7628                                     TREE_OPERAND (init, 0),
7629                                     build_int_2 (i, 0));
7630                 TREE_CONSTANT (fdesc) = 1;
7631
7632                 vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
7633               }
7634         }
7635       else
7636         vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
7637     }
7638
7639   /* The initializers for virtual functions were built up in reverse
7640      order; straighten them out now.  */
7641   vfun_inits = nreverse (vfun_inits);
7642   
7643   /* The negative offset initializers are also in reverse order.  */
7644   vid.inits = nreverse (vid.inits);
7645
7646   /* Chain the two together.  */
7647   return chainon (vid.inits, vfun_inits);
7648 }
7649
7650 /* Adds to vid->inits the initializers for the vbase and vcall
7651    offsets in BINFO, which is in the hierarchy dominated by T.  */
7652
7653 static void
7654 build_vcall_and_vbase_vtbl_entries (binfo, vid)
7655      tree binfo;
7656      vtbl_init_data *vid;
7657 {
7658   tree b;
7659
7660   /* If this is a derived class, we must first create entries
7661      corresponding to the primary base class.  */
7662   b = get_primary_binfo (binfo);
7663   if (b)
7664     build_vcall_and_vbase_vtbl_entries (b, vid);
7665
7666   /* Add the vbase entries for this base.  */
7667   build_vbase_offset_vtbl_entries (binfo, vid);
7668   /* Add the vcall entries for this base.  */
7669   build_vcall_offset_vtbl_entries (binfo, vid);
7670 }
7671
7672 /* Returns the initializers for the vbase offset entries in the vtable
7673    for BINFO (which is part of the class hierarchy dominated by T), in
7674    reverse order.  VBASE_OFFSET_INDEX gives the vtable index
7675    where the next vbase offset will go.  */
7676
7677 static void
7678 build_vbase_offset_vtbl_entries (binfo, vid)
7679      tree binfo;
7680      vtbl_init_data *vid;
7681 {
7682   tree vbase;
7683   tree t;
7684   tree non_primary_binfo;
7685
7686   /* If there are no virtual baseclasses, then there is nothing to
7687      do.  */
7688   if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
7689     return;
7690
7691   t = vid->derived;
7692   
7693   /* We might be a primary base class.  Go up the inheritance hierarchy
7694      until we find the most derived class of which we are a primary base:
7695      it is the offset of that which we need to use.  */
7696   non_primary_binfo = binfo;
7697   while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7698     {
7699       tree b;
7700
7701       /* If we have reached a virtual base, then it must be a primary
7702          base (possibly multi-level) of vid->binfo, or we wouldn't
7703          have called build_vcall_and_vbase_vtbl_entries for it.  But it
7704          might be a lost primary, so just skip down to vid->binfo.  */
7705       if (TREE_VIA_VIRTUAL (non_primary_binfo))
7706         {
7707           non_primary_binfo = vid->binfo;
7708           break;
7709         }
7710
7711       b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7712       if (get_primary_binfo (b) != non_primary_binfo)
7713         break;
7714       non_primary_binfo = b;
7715     }
7716
7717   /* Go through the virtual bases, adding the offsets.  */
7718   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
7719        vbase;
7720        vbase = TREE_CHAIN (vbase))
7721     {
7722       tree b;
7723       tree delta;
7724       
7725       if (!TREE_VIA_VIRTUAL (vbase))
7726         continue;
7727
7728       /* Find the instance of this virtual base in the complete
7729          object.  */
7730       b = binfo_for_vbase (BINFO_TYPE (vbase), t);
7731
7732       /* If we've already got an offset for this virtual base, we
7733          don't need another one.  */
7734       if (BINFO_VTABLE_PATH_MARKED (b))
7735         continue;
7736       SET_BINFO_VTABLE_PATH_MARKED (b);
7737
7738       /* Figure out where we can find this vbase offset.  */
7739       delta = size_binop (MULT_EXPR, 
7740                           vid->index,
7741                           convert (ssizetype,
7742                                    TYPE_SIZE_UNIT (vtable_entry_type)));
7743       if (vid->primary_vtbl_p)
7744         BINFO_VPTR_FIELD (b) = delta;
7745
7746       if (binfo != TYPE_BINFO (t))
7747         {
7748           tree orig_vbase;
7749
7750           /* Find the instance of this virtual base in the type of BINFO.  */
7751           orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
7752                                         BINFO_TYPE (binfo));
7753
7754           /* The vbase offset had better be the same.  */
7755           if (!tree_int_cst_equal (delta,
7756                                    BINFO_VPTR_FIELD (orig_vbase)))
7757             my_friendly_abort (20000403);
7758         }
7759
7760       /* The next vbase will come at a more negative offset.  */
7761       vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7762
7763       /* The initializer is the delta from BINFO to this virtual base.
7764          The vbase offsets go in reverse inheritance-graph order, and
7765          we are walking in inheritance graph order so these end up in
7766          the right order.  */
7767       delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
7768       
7769       *vid->last_init 
7770         = build_tree_list (NULL_TREE,
7771                            fold (build1 (NOP_EXPR, 
7772                                          vtable_entry_type,
7773                                          delta)));
7774       vid->last_init = &TREE_CHAIN (*vid->last_init);
7775     }
7776 }
7777
7778 /* Adds the initializers for the vcall offset entries in the vtable
7779    for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
7780    to VID->INITS.  */
7781
7782 static void
7783 build_vcall_offset_vtbl_entries (binfo, vid)
7784      tree binfo;
7785      vtbl_init_data *vid;
7786 {
7787   /* We only need these entries if this base is a virtual base.  */
7788   if (!TREE_VIA_VIRTUAL (binfo))
7789     return;
7790
7791   /* We need a vcall offset for each of the virtual functions in this
7792      vtable.  For example:
7793
7794        class A { virtual void f (); };
7795        class B1 : virtual public A { virtual void f (); };
7796        class B2 : virtual public A { virtual void f (); };
7797        class C: public B1, public B2 { virtual void f (); };
7798
7799      A C object has a primary base of B1, which has a primary base of A.  A
7800      C also has a secondary base of B2, which no longer has a primary base
7801      of A.  So the B2-in-C construction vtable needs a secondary vtable for
7802      A, which will adjust the A* to a B2* to call f.  We have no way of
7803      knowing what (or even whether) this offset will be when we define B2,
7804      so we store this "vcall offset" in the A sub-vtable and look it up in
7805      a "virtual thunk" for B2::f.
7806
7807      We need entries for all the functions in our primary vtable and
7808      in our non-virtual bases' secondary vtables.  */
7809   vid->vbase = binfo;
7810   /* Now, walk through the non-virtual bases, adding vcall offsets.  */
7811   add_vcall_offset_vtbl_entries_r (binfo, vid);
7812 }
7813
7814 /* Build vcall offsets, starting with those for BINFO.  */
7815
7816 static void
7817 add_vcall_offset_vtbl_entries_r (binfo, vid)
7818      tree binfo;
7819      vtbl_init_data *vid;
7820 {
7821   int i;
7822   tree primary_binfo;
7823
7824   /* Don't walk into virtual bases -- except, of course, for the
7825      virtual base for which we are building vcall offsets.  Any
7826      primary virtual base will have already had its offsets generated
7827      through the recursion in build_vcall_and_vbase_vtbl_entries.  */
7828   if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
7829     return;
7830   
7831   /* If BINFO has a primary base, process it first.  */
7832   primary_binfo = get_primary_binfo (binfo);
7833   if (primary_binfo)
7834     add_vcall_offset_vtbl_entries_r (primary_binfo, vid);
7835
7836   /* Add BINFO itself to the list.  */
7837   add_vcall_offset_vtbl_entries_1 (binfo, vid);
7838
7839   /* Scan the non-primary bases of BINFO.  */
7840   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i) 
7841     {
7842       tree base_binfo;
7843       
7844       base_binfo = BINFO_BASETYPE (binfo, i);
7845       if (base_binfo != primary_binfo)
7846         add_vcall_offset_vtbl_entries_r (base_binfo, vid);
7847     }
7848 }
7849
7850 /* Called from build_vcall_offset_vtbl_entries_r.  */
7851
7852 static void
7853 add_vcall_offset_vtbl_entries_1 (binfo, vid)
7854      tree binfo;
7855      vtbl_init_data* vid;
7856 {
7857   tree derived_virtuals;
7858   tree base_virtuals;
7859   tree orig_virtuals;
7860   tree binfo_inits;
7861   /* If BINFO is a primary base, the most derived class which has BINFO as
7862      a primary base; otherwise, just BINFO.  */
7863   tree non_primary_binfo;
7864
7865   binfo_inits = NULL_TREE;
7866
7867   /* We might be a primary base class.  Go up the inheritance hierarchy
7868      until we find the most derived class of which we are a primary base:
7869      it is the BINFO_VIRTUALS there that we need to consider.  */
7870   non_primary_binfo = binfo;
7871   while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
7872     {
7873       tree b;
7874
7875       /* If we have reached a virtual base, then it must be vid->vbase,
7876          because we ignore other virtual bases in
7877          add_vcall_offset_vtbl_entries_r.  In turn, it must be a primary
7878          base (possibly multi-level) of vid->binfo, or we wouldn't
7879          have called build_vcall_and_vbase_vtbl_entries for it.  But it
7880          might be a lost primary, so just skip down to vid->binfo.  */
7881       if (TREE_VIA_VIRTUAL (non_primary_binfo))
7882         {
7883           if (non_primary_binfo != vid->vbase)
7884             abort ();
7885           non_primary_binfo = vid->binfo;
7886           break;
7887         }
7888
7889       b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
7890       if (get_primary_binfo (b) != non_primary_binfo)
7891         break;
7892       non_primary_binfo = b;
7893     }
7894
7895   if (vid->ctor_vtbl_p)
7896     /* For a ctor vtable we need the equivalent binfo within the hierarchy
7897        where rtti_binfo is the most derived type.  */
7898     non_primary_binfo = get_original_base
7899           (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
7900
7901   /* Make entries for the rest of the virtuals.  */
7902   for (base_virtuals = BINFO_VIRTUALS (binfo),
7903          derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
7904          orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
7905        base_virtuals;
7906        base_virtuals = TREE_CHAIN (base_virtuals),
7907          derived_virtuals = TREE_CHAIN (derived_virtuals),
7908          orig_virtuals = TREE_CHAIN (orig_virtuals))
7909     {
7910       tree orig_fn;
7911       tree fn;
7912       tree base;
7913       tree base_binfo;
7914       size_t i;
7915       tree vcall_offset;
7916
7917       /* Find the declaration that originally caused this function to
7918          be present in BINFO_TYPE (binfo).  */
7919       orig_fn = BV_FN (orig_virtuals);
7920
7921       /* When processing BINFO, we only want to generate vcall slots for
7922          function slots introduced in BINFO.  So don't try to generate
7923          one if the function isn't even defined in BINFO.  */
7924       if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
7925         continue;
7926
7927       /* Find the overriding function.  */
7928       fn = BV_FN (derived_virtuals);
7929
7930       /* If there is already an entry for a function with the same
7931          signature as FN, then we do not need a second vcall offset.
7932          Check the list of functions already present in the derived
7933          class vtable.  */
7934       for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i) 
7935         {
7936           tree derived_entry;
7937
7938           derived_entry = VARRAY_TREE (vid->fns, i);
7939           if (same_signature_p (BV_FN (derived_entry), fn)
7940               /* We only use one vcall offset for virtual destructors,
7941                  even though there are two virtual table entries.  */
7942               || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
7943                   && DECL_DESTRUCTOR_P (fn)))
7944             {
7945               if (!vid->ctor_vtbl_p)
7946                 BV_VCALL_INDEX (derived_virtuals) 
7947                   = BV_VCALL_INDEX (derived_entry);
7948               break;
7949             }
7950         }
7951       if (i != VARRAY_ACTIVE_SIZE (vid->fns))
7952         continue;
7953
7954       /* The FN comes from BASE.  So, we must calculate the adjustment from
7955          vid->vbase to BASE.  We can just look for BASE in the complete
7956          object because we are converting from a virtual base, so if there
7957          were multiple copies, there would not be a unique final overrider
7958          and vid->derived would be ill-formed.  */
7959       base = DECL_CONTEXT (fn);
7960       base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
7961
7962       /* Compute the vcall offset.  */
7963       /* As mentioned above, the vbase we're working on is a primary base of
7964          vid->binfo.  But it might be a lost primary, so its BINFO_OFFSET
7965          might be wrong, so we just use the BINFO_OFFSET from vid->binfo.  */
7966       vcall_offset = BINFO_OFFSET (vid->binfo);
7967       vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
7968                                   vcall_offset);
7969       vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
7970                                    vcall_offset));
7971       
7972       *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
7973       vid->last_init = &TREE_CHAIN (*vid->last_init);
7974
7975       /* Keep track of the vtable index where this vcall offset can be
7976          found.  For a construction vtable, we already made this
7977          annotation when we built the original vtable.  */
7978       if (!vid->ctor_vtbl_p)
7979         BV_VCALL_INDEX (derived_virtuals) = vid->index;
7980
7981       /* The next vcall offset will be found at a more negative
7982          offset.  */
7983       vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
7984
7985       /* Keep track of this function.  */
7986       VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
7987     }
7988 }
7989
7990 /* Return vtbl initializers for the RTTI entries coresponding to the
7991    BINFO's vtable.  The RTTI entries should indicate the object given
7992    by VID->rtti_binfo.  */
7993
7994 static void
7995 build_rtti_vtbl_entries (binfo, vid)
7996      tree binfo;
7997      vtbl_init_data *vid;
7998 {
7999   tree b;
8000   tree t;
8001   tree basetype;
8002   tree offset;
8003   tree decl;
8004   tree init;
8005
8006   basetype = BINFO_TYPE (binfo);
8007   t = BINFO_TYPE (vid->rtti_binfo);
8008
8009   /* To find the complete object, we will first convert to our most
8010      primary base, and then add the offset in the vtbl to that value.  */
8011   b = binfo;
8012   while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
8013          && !BINFO_LOST_PRIMARY_P (b))
8014     {
8015       tree primary_base;
8016
8017       primary_base = get_primary_binfo (b);
8018       my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
8019       b = primary_base;
8020     }
8021   offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
8022
8023   /* The second entry is the address of the typeinfo object.  */
8024   if (flag_rtti)
8025     decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
8026   else
8027     decl = integer_zero_node;
8028   
8029   /* Convert the declaration to a type that can be stored in the
8030      vtable.  */
8031   init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
8032   TREE_CONSTANT (init) = 1;
8033   *vid->last_init = build_tree_list (NULL_TREE, init);
8034   vid->last_init = &TREE_CHAIN (*vid->last_init);
8035
8036   /* Add the offset-to-top entry.  It comes earlier in the vtable that
8037      the the typeinfo entry.  Convert the offset to look like a
8038      function pointer, so that we can put it in the vtable.  */
8039   init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
8040   TREE_CONSTANT (init) = 1;
8041   *vid->last_init = build_tree_list (NULL_TREE, init);
8042   vid->last_init = &TREE_CHAIN (*vid->last_init);
8043 }
8044
8045 /* Build an entry in the virtual function table.  DELTA is the offset
8046    for the `this' pointer.  VCALL_INDEX is the vtable index containing
8047    the vcall offset; NULL_TREE if none.  ENTRY is the virtual function
8048    table entry itself.  It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
8049    but it may not actually be a virtual function table pointer.  (For
8050    example, it might be the address of the RTTI object, under the new
8051    ABI.)  */
8052
8053 static tree
8054 build_vtable_entry (delta, vcall_index, entry)
8055      tree delta;
8056      tree vcall_index;
8057      tree entry;
8058 {
8059   tree fn = TREE_OPERAND (entry, 0);
8060   
8061   if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
8062       && fn != abort_fndecl)
8063     {
8064       entry = make_thunk (entry, delta, vcall_index);
8065       entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
8066       TREE_READONLY (entry) = 1;
8067       TREE_CONSTANT (entry) = 1;
8068     }
8069 #ifdef GATHER_STATISTICS
8070   n_vtable_entries += 1;
8071 #endif
8072   return entry;
8073 }