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