OSDN Git Service

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