OSDN Git Service

2002-08-24 Matt Austern <austern@apple.com>
[pf3gnuchains/gcc-fork.git] / gcc / cp / class.c
index cf3b666..db8cfa7 100644 (file)
@@ -1,6 +1,6 @@
 /* Functions related to building classes and their related objects.
    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001  Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002  Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GNU CC.
@@ -33,10 +33,7 @@ Boston, MA 02111-1307, USA.  */
 #include "toplev.h"
 #include "ggc.h"
 #include "lex.h"
-
-#include "obstack.h"
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
+#include "target.h"
 
 /* The number of nested classes being processed.  If we are not in the
    scope of any class, this is zero.  */
@@ -74,7 +71,7 @@ typedef struct vtbl_init_data_s
   /* The negative-index vtable initializers built up so far.  These
      are in order from least negative index to most negative index.  */
   tree inits;
-  /* The last (i.e., most negative entry in INITS.  */
+  /* The last (i.e., most negative) entry in INITS.  */
   tree* last_init;
   /* The binfo for the virtual base for which we're building
      vcall offset initializers.  */
@@ -106,24 +103,19 @@ varray_type local_classes;
 
 static tree get_vfield_name PARAMS ((tree));
 static void finish_struct_anon PARAMS ((tree));
-static tree build_vbase_pointer PARAMS ((tree, tree));
-static tree build_vtable_entry PARAMS ((tree, tree, tree, int));
+static tree build_vtable_entry PARAMS ((tree, tree, tree));
 static tree get_vtable_name PARAMS ((tree));
-static tree get_derived_offset PARAMS ((tree, tree));
 static tree get_basefndecls PARAMS ((tree, tree));
 static int build_primary_vtable PARAMS ((tree, tree));
 static int build_secondary_vtable PARAMS ((tree, tree));
-static tree dfs_finish_vtbls PARAMS ((tree, void *));
 static void finish_vtbls PARAMS ((tree));
 static void modify_vtable_entry PARAMS ((tree, tree, tree, tree, tree *));
-static void add_virtual_function PARAMS ((tree *, tree *, int *, tree, tree));
 static tree delete_duplicate_fields_1 PARAMS ((tree, tree));
 static void delete_duplicate_fields PARAMS ((tree));
 static void finish_struct_bits PARAMS ((tree));
 static int alter_access PARAMS ((tree, tree, tree));
 static void handle_using_decl PARAMS ((tree, tree));
 static int strictly_overrides PARAMS ((tree, tree));
-static void mark_overriders PARAMS ((tree, tree));
 static void check_for_override PARAMS ((tree, tree));
 static tree dfs_modify_vtables PARAMS ((tree, void *));
 static tree modify_all_vtables PARAMS ((tree, int *, tree));
@@ -136,7 +128,8 @@ static tree add_implicitly_declared_members PARAMS ((tree, int, int, int));
 static tree fixed_type_or_null PARAMS ((tree, int *, int *));
 static tree resolve_address_of_overloaded_function PARAMS ((tree, tree, int,
                                                          int, int, tree));
-static void build_vtable_entry_ref PARAMS ((tree, tree, tree));
+static tree build_vtable_entry_ref PARAMS ((tree, tree, tree));
+static tree build_vtbl_ref_1 PARAMS ((tree, tree));
 static tree build_vtbl_initializer PARAMS ((tree, tree, tree, tree, int *));
 static int count_fields PARAMS ((tree));
 static int add_fields_to_vec PARAMS ((tree, tree, int));
@@ -144,23 +137,20 @@ static void check_bitfield_decl PARAMS ((tree));
 static void check_field_decl PARAMS ((tree, tree, int *, int *, int *, int *));
 static void check_field_decls PARAMS ((tree, tree *, int *, int *, int *, 
                                     int *));
-static void build_base_field PARAMS ((record_layout_info, tree, int *,
-                                     splay_tree));
-static void build_base_fields PARAMS ((record_layout_info, int *,
-                                      splay_tree));
-static tree build_vbase_pointer_fields PARAMS ((record_layout_info, int *));
-static tree build_vtbl_or_vbase_field PARAMS ((tree, tree, tree, tree, tree,
-                                              int *));
+static bool build_base_field PARAMS ((record_layout_info, tree, int *,
+                                    splay_tree, tree));
+static bool build_base_fields PARAMS ((record_layout_info, int *,
+                                     splay_tree, tree));
 static void check_methods PARAMS ((tree));
 static void remove_zero_width_bit_fields PARAMS ((tree));
 static void check_bases PARAMS ((tree, int *, int *, int *));
 static void check_bases_and_members PARAMS ((tree, int *));
-static tree create_vtable_ptr PARAMS ((tree, int *, int *, tree *, tree *));
-static void layout_class_type PARAMS ((tree, int *, int *, tree *, tree *));
+static tree create_vtable_ptr PARAMS ((tree, int *, tree *));
+static void layout_class_type PARAMS ((tree, int *, int *, tree *));
 static void fixup_pending_inline PARAMS ((tree));
 static void fixup_inline_methods PARAMS ((tree));
 static void set_primary_base PARAMS ((tree, tree, int *));
-static void propagate_binfo_offsets PARAMS ((tree, tree));
+static void propagate_binfo_offsets PARAMS ((tree, tree, tree));
 static void layout_virtual_bases PARAMS ((tree, splay_tree));
 static tree dfs_set_offset_for_unshared_vbases PARAMS ((tree, void *));
 static void build_vbase_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
@@ -171,16 +161,20 @@ static void layout_vtable_decl PARAMS ((tree, int));
 static tree dfs_find_final_overrider PARAMS ((tree, void *));
 static tree find_final_overrider PARAMS ((tree, tree, tree));
 static int make_new_vtable PARAMS ((tree, tree));
-static void dump_class_hierarchy_r PARAMS ((FILE *, tree, tree, int));
-extern void dump_class_hierarchy PARAMS ((const char *, tree));
+static int maybe_indent_hierarchy PARAMS ((FILE *, int, int));
+static void dump_class_hierarchy_r PARAMS ((FILE *, int, tree, tree, int));
+static void dump_class_hierarchy PARAMS ((tree));
+static void dump_array PARAMS ((FILE *, tree));
+static void dump_vtable PARAMS ((tree, tree, tree));
+static void dump_vtt PARAMS ((tree, tree));
 static tree build_vtable PARAMS ((tree, tree, tree));
 static void initialize_vtable PARAMS ((tree, tree));
 static void initialize_array PARAMS ((tree, tree));
 static void layout_nonempty_base_or_field PARAMS ((record_layout_info,
                                                   tree, tree,
-                                                  splay_tree));
+                                                  splay_tree, tree));
 static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
-static void layout_empty_base PARAMS ((tree, tree, splay_tree));
+static bool layout_empty_base PARAMS ((tree, tree, splay_tree, tree));
 static void accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree, tree));
 static tree dfs_accumulate_vtbl_inits PARAMS ((tree, tree, tree, tree,
                                               tree));
@@ -190,14 +184,16 @@ static void build_vcall_and_vbase_vtbl_entries PARAMS ((tree,
                                                        vtbl_init_data *));
 static void force_canonical_binfo_r PARAMS ((tree, tree, tree, tree));
 static void force_canonical_binfo PARAMS ((tree, tree, tree, tree));
+static tree dfs_unshared_virtual_bases PARAMS ((tree, void *));
 static void mark_primary_bases PARAMS ((tree));
-static tree mark_primary_virtual_base PARAMS ((tree, tree, tree));
+static tree mark_primary_virtual_base PARAMS ((tree, tree));
 static void clone_constructors_and_destructors PARAMS ((tree));
 static tree build_clone PARAMS ((tree, tree));
 static void update_vtable_entry_for_fn PARAMS ((tree, tree, tree, tree *));
 static tree copy_virtuals PARAMS ((tree));
 static void build_ctor_vtbl_group PARAMS ((tree, tree));
 static void build_vtt PARAMS ((tree));
+static tree binfo_ctor_vtable PARAMS ((tree));
 static tree *build_vtt_inits PARAMS ((tree, tree, tree *, tree *));
 static tree dfs_build_secondary_vptr_vtt_inits PARAMS ((tree, void *));
 static tree dfs_ctor_vtable_bases_queue_p PARAMS ((tree, void *data));
@@ -207,18 +203,19 @@ static tree dfs_get_primary_binfo PARAMS ((tree, void*));
 static int record_subobject_offset PARAMS ((tree, tree, splay_tree));
 static int check_subobject_offset PARAMS ((tree, tree, splay_tree));
 static int walk_subobject_offsets PARAMS ((tree, subobject_offset_fn,
-                                          tree, splay_tree, int));
+                                          tree, splay_tree, tree, int));
 static void record_subobject_offsets PARAMS ((tree, tree, splay_tree, int));
 static int layout_conflict_p PARAMS ((tree, tree, splay_tree, int));
 static int splay_tree_compare_integer_csts PARAMS ((splay_tree_key k1,
                                                    splay_tree_key k2));
-
+static void warn_about_ambiguous_direct_bases PARAMS ((tree));
+static bool type_requires_array_cookie PARAMS ((tree));
 
 /* Macros for dfs walking during vtt construction. See
    dfs_ctor_vtable_bases_queue_p, dfs_build_secondary_vptr_vtt_inits
    and dfs_fixup_binfo_vtbls.  */
-#define VTT_TOP_LEVEL_P(node) TREE_UNSIGNED(node)
-#define VTT_MARKED_BINFO_P(node) TREE_USED(node)
+#define VTT_TOP_LEVEL_P(NODE) TREE_UNSIGNED (NODE)
+#define VTT_MARKED_BINFO_P(NODE) TREE_USED (NODE)
 
 /* Variables shared between class.c and call.c.  */
 
@@ -233,432 +230,256 @@ int n_build_method_call = 0;
 int n_inner_fields_searched = 0;
 #endif
 
-/* Virtual base class layout.  */
-
-/* Returns a list of virtual base class pointers as a chain of
-   FIELD_DECLS.  */
+/* Convert to or from a base subobject.  EXPR is an expression of type
+   `A' or `A*', an expression of type `B' or `B*' is returned.  To
+   convert A to a base B, CODE is PLUS_EXPR and BINFO is the binfo for
+   the B base instance within A.  To convert base A to derived B, CODE
+   is MINUS_EXPR and BINFO is the binfo for the A instance within B.
+   In this latter case, A must not be a morally virtual base of B.
+   NONNULL is true if EXPR is known to be non-NULL (this is only
+   needed when EXPR is of pointer type).  CV qualifiers are preserved
+   from EXPR.  */
 
-static tree
-build_vbase_pointer_fields (rli, empty_p)
-     record_layout_info rli;
-     int *empty_p;
+tree
+build_base_path (code, expr, binfo, nonnull)
+     enum tree_code code;
+     tree expr;
+     tree binfo;
+     int nonnull;
 {
-  /* Chain to hold all the new FIELD_DECLs which point at virtual
-     base classes.  */
-  tree rec = rli->t;
-  tree vbase_decls = NULL_TREE;
-  tree binfos = TYPE_BINFO_BASETYPES (rec);
-  int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
-  tree decl;
-  int i;
+  tree v_binfo = NULL_TREE;
+  tree d_binfo = NULL_TREE;
+  tree probe;
+  tree offset;
+  tree target_type;
+  tree null_test = NULL;
+  tree ptr_target_type;
+  int fixed_type_p;
+  int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
 
-  /* Under the new ABI, there are no vbase pointers in the object.
-     Instead, the offsets are stored in the vtable.  */
-  if (vbase_offsets_in_vtable_p ())
-    return NULL_TREE;
+  if (expr == error_mark_node || binfo == error_mark_node || !binfo)
+    return error_mark_node;
 
-  /* Loop over the baseclasses, adding vbase pointers as needed.  */
-  for (i = 0; i < n_baseclasses; i++)
+  for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
     {
-      register tree base_binfo = TREE_VEC_ELT (binfos, i);
-      register tree basetype = BINFO_TYPE (base_binfo);
-
-      if (!COMPLETE_TYPE_P (basetype))
-       /* This error is now reported in xref_tag, thus giving better
-          location information.  */
-       continue;
-
-      /* All basetypes are recorded in the association list of the
-        derived type.  */
-
-      if (TREE_VIA_VIRTUAL (base_binfo))
-       {
-         int j;
-         const char *name;
-
-         /* The offset for a virtual base class is only used in computing
-            virtual function tables and for initializing virtual base
-            pointers.  It is built once `get_vbase_types' is called.  */
-
-         /* If this basetype can come from another vbase pointer
-            without an additional indirection, we will share
-            that pointer.  If an indirection is involved, we
-            make our own pointer.  */
-         for (j = 0; j < n_baseclasses; j++)
-           {
-             tree other_base_binfo = TREE_VEC_ELT (binfos, j);
-             if (! TREE_VIA_VIRTUAL (other_base_binfo)
-                 && binfo_for_vbase (basetype, BINFO_TYPE (other_base_binfo)))
-               goto got_it;
-           }
-         FORMAT_VBASE_NAME (name, basetype);
-         decl = build_vtbl_or_vbase_field (get_identifier (name), 
-                                           get_identifier (VTABLE_BASE),
-                                           build_pointer_type (basetype),
-                                           rec,
-                                           basetype,
-                                           empty_p);
-         BINFO_VPTR_FIELD (base_binfo) = decl;
-         TREE_CHAIN (decl) = vbase_decls;
-         place_field (rli, decl);
-         vbase_decls = decl;
-         *empty_p = 0;
-
-       got_it:
-         /* The space this decl occupies has already been accounted for.  */
-         ;
-       }
+      d_binfo = probe;
+      if (!v_binfo && TREE_VIA_VIRTUAL (probe))
+       v_binfo = probe;
     }
 
-  return vbase_decls;
-}
-
-/* Returns a pointer to the virtual base class of EXP that has the
-   indicated TYPE.  EXP is of class type, not a pointer type.  */
-
-static tree
-build_vbase_pointer (exp, type)
-     tree exp, type;
-{
-  if (vbase_offsets_in_vtable_p ())
-    {
-      tree vbase;
-      tree vbase_ptr;
-
-      /* Find the shared copy of TYPE; that's where the vtable offset
-        is recorded.  */
-      vbase = binfo_for_vbase (type, TREE_TYPE (exp));
-      /* Find the virtual function table pointer.  */
-      vbase_ptr = build_vfield_ref (exp, TREE_TYPE (exp));
-      /* Compute the location where the offset will lie.  */
-      vbase_ptr = build (PLUS_EXPR, 
-                        TREE_TYPE (vbase_ptr),
-                        vbase_ptr,
-                        BINFO_VPTR_FIELD (vbase));
-      vbase_ptr = build1 (NOP_EXPR, 
-                         build_pointer_type (ptrdiff_type_node),
-                         vbase_ptr);
-      /* Add the contents of this location to EXP.  */
-      return build (PLUS_EXPR,
-                   build_pointer_type (type),
-                   build_unary_op (ADDR_EXPR, exp, /*noconvert=*/0),
-                   build1 (INDIRECT_REF, ptrdiff_type_node, vbase_ptr));
-    }
-  else
+  probe = TYPE_MAIN_VARIANT (TREE_TYPE (expr));
+  if (want_pointer)
+    probe = TYPE_MAIN_VARIANT (TREE_TYPE (probe));
+  
+  my_friendly_assert (code == MINUS_EXPR
+                     ? same_type_p (BINFO_TYPE (binfo), probe)
+                     : code == PLUS_EXPR
+                     ? same_type_p (BINFO_TYPE (d_binfo), probe)
+                     : false, 20010723);
+  
+  if (code == MINUS_EXPR && v_binfo)
     {
-      char *name;
-      FORMAT_VBASE_NAME (name, type);
-      return build_component_ref (exp, get_identifier (name), NULL_TREE, 0);
+      error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
+            BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
+      return error_mark_node;
     }
-}
-
-/* Build multi-level access to EXPR using hierarchy path PATH.
-   CODE is PLUS_EXPR if we are going with the grain,
-   and MINUS_EXPR if we are not (in which case, we cannot traverse
-   virtual baseclass links).
-
-   TYPE is the type we want this path to have on exit.
-
-   NONNULL is non-zero if  we know (for any reason) that EXPR is
-   not, in fact, zero.  */
-
-tree
-build_vbase_path (code, type, expr, path, nonnull)
-     enum tree_code code;
-     tree type, expr, path;
-     int nonnull;
-{
-  register int changed = 0;
-  tree last = NULL_TREE, last_virtual = NULL_TREE;
-  int fixed_type_p;
-  tree null_expr = 0, nonnull_expr;
-  tree basetype;
-  tree offset = integer_zero_node;
-
-  if (BINFO_INHERITANCE_CHAIN (path) == NULL_TREE)
-    return build1 (NOP_EXPR, type, expr);
 
-  /* We could do better if we had additional logic to convert back to the
-     unconverted type (the static type of the complete object), and then
-     convert back to the type we want.  Until that is done, we only optimize
-     if the complete type is the same type as expr has.  */
   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
-  if (fixed_type_p < 0)
-    /* Virtual base layout is not fixed, even in ctors and dtors. */
-    fixed_type_p = 0;
-
-  if (!fixed_type_p && TREE_SIDE_EFFECTS (expr))
+  if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
     expr = save_expr (expr);
-  nonnull_expr = expr;
 
-  path = reverse_path (path);
-
-  basetype = BINFO_TYPE (path);
-
-  while (path)
+  if (!want_pointer)
+    expr = build_unary_op (ADDR_EXPR, expr, 0);
+  else if (!nonnull)
+    null_test = build (EQ_EXPR, boolean_type_node, expr, integer_zero_node);
+  
+  offset = BINFO_OFFSET (binfo);
+  
+  if (v_binfo && fixed_type_p <= 0)
     {
-      if (TREE_VIA_VIRTUAL (TREE_VALUE (path)))
-       {
-         last_virtual = BINFO_TYPE (TREE_VALUE (path));
-         if (code == PLUS_EXPR)
-           {
-             changed = ! fixed_type_p;
-
-             if (changed)
-               {
-                 tree ind;
-
-                 /* We already check for ambiguous things in the caller, just
-                    find a path.  */
-                 if (last)
-                   {
-                     tree binfo = get_binfo (last, TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (nonnull_expr))), 0);
-                     nonnull_expr = convert_pointer_to_real (binfo, nonnull_expr);
-                   }
-                 ind = build_indirect_ref (nonnull_expr, NULL_PTR);
-                 nonnull_expr = build_vbase_pointer (ind, last_virtual);
-                 if (nonnull == 0
-                     && TREE_CODE (type) == POINTER_TYPE
-                     && null_expr == NULL_TREE)
-                   {
-                     null_expr = build1 (NOP_EXPR, build_pointer_type (last_virtual), integer_zero_node);
-                     expr = build (COND_EXPR, build_pointer_type (last_virtual),
-                                   build (EQ_EXPR, boolean_type_node, expr,
-                                          integer_zero_node),
-                                   null_expr, nonnull_expr);
-                   }
-               }
-             /* else we'll figure out the offset below.  */
-
-             /* Happens in the case of parse errors.  */
-             if (nonnull_expr == error_mark_node)
-               return error_mark_node;
-           }
-         else
-           {
-             cp_error ("cannot cast up from virtual baseclass `%T'",
-                         last_virtual);
-             return error_mark_node;
-           }
-       }
-      last = TREE_VALUE (path);
-      path = TREE_CHAIN (path);
+      /* Going via virtual base V_BINFO.  We need the static offset
+         from V_BINFO to BINFO, and the dynamic offset from D_BINFO to
+         V_BINFO.  That offset is an entry in D_BINFO's vtable.  */
+      tree v_offset = build_vfield_ref (build_indirect_ref (expr, NULL),
+                                       TREE_TYPE (TREE_TYPE (expr)));
+      
+      v_binfo = binfo_for_vbase (BINFO_TYPE (v_binfo), BINFO_TYPE (d_binfo));
+      
+      v_offset = build (PLUS_EXPR, TREE_TYPE (v_offset),
+                       v_offset,  BINFO_VPTR_FIELD (v_binfo));
+      v_offset = build1 (NOP_EXPR, 
+                        build_pointer_type (ptrdiff_type_node),
+                        v_offset);
+      v_offset = build_indirect_ref (v_offset, NULL);
+      TREE_CONSTANT (v_offset) = 1;
+
+      offset = cp_convert (ptrdiff_type_node,
+                          size_diffop (offset, BINFO_OFFSET (v_binfo)));
+
+      if (!integer_zerop (offset))
+       v_offset = build (code, ptrdiff_type_node, v_offset, offset);
+
+      if (fixed_type_p < 0)
+       /* Negative fixed_type_p means this is a constructor or destructor;
+          virtual base layout is fixed in in-charge [cd]tors, but not in
+          base [cd]tors.  */
+       offset = build (COND_EXPR, ptrdiff_type_node,
+                       build (EQ_EXPR, boolean_type_node,
+                              current_in_charge_parm, integer_zero_node),
+                       v_offset,
+                       BINFO_OFFSET (binfo));
+      else
+       offset = v_offset;
     }
-  /* LAST is now the last basetype assoc on the path.  */
 
-  /* A pointer to a virtual base member of a non-null object
-     is non-null.  Therefore, we only need to test for zeroness once.
-     Make EXPR the canonical expression to deal with here.  */
-  if (null_expr)
-    {
-      TREE_OPERAND (expr, 2) = nonnull_expr;
-      TREE_TYPE (expr) = TREE_TYPE (TREE_OPERAND (expr, 1))
-       = TREE_TYPE (nonnull_expr);
-    }
+  target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
+  
+  target_type = cp_build_qualified_type
+    (target_type, cp_type_quals (TREE_TYPE (TREE_TYPE (expr))));
+  ptr_target_type = build_pointer_type (target_type);
+  if (want_pointer)
+    target_type = ptr_target_type;
+  
+  expr = build1 (NOP_EXPR, ptr_target_type, expr);
+
+  if (!integer_zerop (offset))
+    expr = build (code, ptr_target_type, expr, offset);
   else
-    expr = nonnull_expr;
+    null_test = NULL;
+  
+  if (!want_pointer)
+    expr = build_indirect_ref (expr, NULL);
 
-  /* If we go through any virtual base pointers, make sure that
-     casts to BASETYPE from the last virtual base class use
-     the right value for BASETYPE.  */
-  if (changed)
-    {
-      tree intype = TREE_TYPE (TREE_TYPE (expr));
+  if (null_test)
+    expr = build (COND_EXPR, target_type, null_test,
+                 build1 (NOP_EXPR, target_type, integer_zero_node),
+                 expr);
 
-      if (TYPE_MAIN_VARIANT (intype) != BINFO_TYPE (last))
-       offset
-         = BINFO_OFFSET (get_binfo (last, TYPE_MAIN_VARIANT (intype), 0));
-    }
-  else
-    offset = BINFO_OFFSET (last);
+  return expr;
+}
 
-  if (! integer_zerop (offset))
-    {
-      /* Bash types to make the backend happy.  */
-      offset = cp_convert (type, offset);
+/* Convert OBJECT to the base TYPE.  If CHECK_ACCESS is true, an error
+   message is emitted if TYPE is inaccessible.  OBJECT is assumed to
+   be non-NULL.  */
 
-      /* If expr might be 0, we need to preserve that zeroness.  */
-      if (nonnull == 0)
-       {
-         if (null_expr)
-           TREE_TYPE (null_expr) = type;
-         else
-           null_expr = build1 (NOP_EXPR, type, integer_zero_node);
-         if (TREE_SIDE_EFFECTS (expr))
-           expr = save_expr (expr);
-
-         return build (COND_EXPR, type,
-                       build (EQ_EXPR, boolean_type_node, expr, integer_zero_node),
-                       null_expr,
-                       build (code, type, expr, offset));
-       }
-      else return build (code, type, expr, offset);
-    }
+tree
+convert_to_base (tree object, tree type, bool check_access)
+{
+  tree binfo;
 
-  /* Cannot change the TREE_TYPE of a NOP_EXPR here, since it may
-     be used multiple times in initialization of multiple inheritance.  */
-  if (null_expr)
-    {
-      TREE_TYPE (expr) = type;
-      return expr;
-    }
-  else
-    return build1 (NOP_EXPR, type, expr);
+  binfo = lookup_base (TREE_TYPE (object), type, 
+                      check_access ? ba_check : ba_ignore, 
+                      NULL);
+  if (!binfo || binfo == error_mark_node)
+    return error_mark_node;
+
+  return build_base_path (PLUS_EXPR, object, binfo, /*nonnull=*/1);
 }
 
 \f
 /* Virtual function things.  */
 
-/* We want to give the assembler the vtable identifier as well as
-   the offset to the function pointer.  So we generate
+static tree
+build_vtable_entry_ref (array_ref, instance, idx)
+     tree array_ref, instance, idx;
+{
+  tree i, i2, vtable, first_fn, basetype;
 
-   __asm__ __volatile__ (".vtable_entry %c0, %c1"
-      : : "s"(&class_vtable),
-          "i"((long)&vtbl[idx].pfn - (long)&vtbl[0])); */
+  basetype = TREE_TYPE (instance);
+  if (TREE_CODE (basetype) == REFERENCE_TYPE)
+    basetype = TREE_TYPE (basetype);
 
-static void
-build_vtable_entry_ref (basetype, vtbl, idx)
-     tree basetype, vtbl, idx;
-{
-  static char asm_stmt[] = ".vtable_entry %c0, %c1";
-  tree s, i, i2;
+  vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
+  first_fn = TYPE_BINFO_VTABLE (basetype);
 
-  s = build_unary_op (ADDR_EXPR, 
-                     get_vtbl_decl_for_binfo (TYPE_BINFO (basetype)), 
-                     0);
-  s = build_tree_list (build_string (1, "s"), s);
+  i = fold (build_array_ref (first_fn, idx));
+  i = fold (build_c_cast (ptrdiff_type_node,
+                         build_unary_op (ADDR_EXPR, i, 0)));
+  i2 = fold (build_array_ref (vtable, build_int_2 (0,0)));
+  i2 = fold (build_c_cast (ptrdiff_type_node,
+                          build_unary_op (ADDR_EXPR, i2, 0)));
+  i = fold (cp_build_binary_op (MINUS_EXPR, i, i2));
 
-  i = build_array_ref (vtbl, idx);
-  if (!flag_vtable_thunks)
-    i = build_component_ref (i, pfn_identifier, vtable_entry_type, 0);
-  i = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i, 0));
-  i2 = build_array_ref (vtbl, build_int_2(0,0));
-  i2 = build_c_cast (ptrdiff_type_node, build_unary_op (ADDR_EXPR, i2, 0));
-  i = cp_build_binary_op (MINUS_EXPR, i, i2);
-  i = build_tree_list (build_string (1, "i"), i);
+  if (TREE_CODE (i) != INTEGER_CST)
+    abort ();
 
-  finish_asm_stmt (ridpointers[RID_VOLATILE],
-                  build_string (sizeof(asm_stmt)-1, asm_stmt),
-                  NULL_TREE, chainon (s, i), NULL_TREE);
+  return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
 }
 
 /* Given an object INSTANCE, return an expression which yields the
-   virtual function vtable element corresponding to INDEX.  There are
-   many special cases for INSTANCE which we take care of here, mainly
-   to avoid creating extra tree nodes when we don't have to.  */
+   vtable element corresponding to INDEX.  There are many special
+   cases for INSTANCE which we take care of here, mainly to avoid
+   creating extra tree nodes when we don't have to.  */
 
-tree
-build_vtbl_ref (instance, idx)
+static tree
+build_vtbl_ref_1 (instance, idx)
      tree instance, idx;
 {
-  tree vtbl, aref;
-  tree basetype = TREE_TYPE (instance);
+  tree aref;
+  tree vtbl = NULL_TREE;
+
+  /* Try to figure out what a reference refers to, and
+     access its virtual function table directly.  */
 
+  int cdtorp = 0;
+  tree fixed_type = fixed_type_or_null (instance, NULL, &cdtorp);
+
+  tree basetype = TREE_TYPE (instance);
   if (TREE_CODE (basetype) == REFERENCE_TYPE)
     basetype = TREE_TYPE (basetype);
 
-  if (instance == current_class_ref)
-    vtbl = build_vfield_ref (instance, basetype);
-  else
+  if (fixed_type && !cdtorp)
     {
-      if (optimize)
-       {
-         /* Try to figure out what a reference refers to, and
-            access its virtual function table directly.  */
-         tree ref = NULL_TREE;
-
-         if (TREE_CODE (instance) == INDIRECT_REF
-             && TREE_CODE (TREE_TYPE (TREE_OPERAND (instance, 0))) == REFERENCE_TYPE)
-           ref = TREE_OPERAND (instance, 0);
-         else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
-           ref = instance;
-
-         if (ref && TREE_CODE (ref) == VAR_DECL
-             && DECL_INITIAL (ref))
-           {
-             tree init = DECL_INITIAL (ref);
-
-             while (TREE_CODE (init) == NOP_EXPR
-                    || TREE_CODE (init) == NON_LVALUE_EXPR)
-               init = TREE_OPERAND (init, 0);
-             if (TREE_CODE (init) == ADDR_EXPR)
-               {
-                 init = TREE_OPERAND (init, 0);
-                 if (IS_AGGR_TYPE (TREE_TYPE (init))
-                     && (TREE_CODE (init) == PARM_DECL
-                         || TREE_CODE (init) == VAR_DECL))
-                   instance = init;
-               }
-           }
-       }
+      tree binfo = lookup_base (fixed_type, basetype,
+                               ba_ignore|ba_quiet, NULL);
+      if (binfo)
+       vtbl = BINFO_VTABLE (binfo);
+    }
 
-      if (IS_AGGR_TYPE (TREE_TYPE (instance))
-         && (TREE_CODE (instance) == RESULT_DECL
-             || TREE_CODE (instance) == PARM_DECL
-             || TREE_CODE (instance) == VAR_DECL))
-       {
-         vtbl = TYPE_BINFO_VTABLE (basetype);
-         /* Knowing the dynamic type of INSTANCE we can easily obtain
-            the correct vtable entry.  In the new ABI, we resolve
-            this back to be in terms of the primary vtable.  */
-         if (TREE_CODE (vtbl) == PLUS_EXPR)
-           {
-             idx = fold (build (PLUS_EXPR,
-                                TREE_TYPE (idx),
-                                idx,
-                                build (EXACT_DIV_EXPR,
-                                       TREE_TYPE (idx),
-                                       TREE_OPERAND (vtbl, 1),
-                                       TYPE_SIZE_UNIT (vtable_entry_type))));
-             vtbl = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
-           }
-       }
-      else
-       vtbl = build_vfield_ref (instance, basetype);
+  if (!vtbl)
+    {
+      vtbl = build_vfield_ref (instance, basetype);
     }
 
   assemble_external (vtbl);
 
-  if (flag_vtable_gc)
-    build_vtable_entry_ref (basetype, vtbl, idx);
-
   aref = build_array_ref (vtbl, idx);
+  TREE_CONSTANT (aref) = 1;
 
   return aref;
 }
 
-/* Given an object INSTANCE, return an expression which yields the
-   virtual function corresponding to INDEX.  There are many special
-   cases for INSTANCE which we take care of here, mainly to avoid
-   creating extra tree nodes when we don't have to.  */
-
 tree
-build_vfn_ref (ptr_to_instptr, instance, idx)
-     tree *ptr_to_instptr, instance;
-     tree idx;
+build_vtbl_ref (instance, idx)
+     tree instance, idx;
 {
-  tree aref = build_vtbl_ref (instance, idx);
+  tree aref = build_vtbl_ref_1 (instance, idx);
 
-  /* When using thunks, there is no extra delta, and we get the pfn
-     directly.  */
-  if (flag_vtable_thunks)
-    return aref;
+  if (flag_vtable_gc)
+    aref = build_vtable_entry_ref (aref, instance, idx);
 
-  if (ptr_to_instptr)
-    {
-      /* Save the intermediate result in a SAVE_EXPR so we don't have to
-        compute each component of the virtual function pointer twice.  */ 
-      if (TREE_CODE (aref) == INDIRECT_REF)
-       TREE_OPERAND (aref, 0) = save_expr (TREE_OPERAND (aref, 0));
+  return aref;
+}
 
-      *ptr_to_instptr
-       = build (PLUS_EXPR, TREE_TYPE (*ptr_to_instptr),
-                *ptr_to_instptr,
-                cp_convert (ptrdiff_type_node,
-                            build_component_ref (aref, delta_identifier, NULL_TREE, 0)));
-    }
+/* Given an object INSTANCE, return an expression which yields a
+   function pointer corresponding to vtable element INDEX.  */
+
+tree
+build_vfn_ref (instance, idx)
+     tree instance, idx;
+{
+  tree aref = build_vtbl_ref_1 (instance, idx);
 
-  return build_component_ref (aref, pfn_identifier, NULL_TREE, 0);
+  /* When using function descriptors, the address of the
+     vtable entry is treated as a function pointer.  */
+  if (TARGET_VTABLE_USES_DESCRIPTORS)
+    aref = build1 (NOP_EXPR, TREE_TYPE (aref),
+                  build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1));
+
+  if (flag_vtable_gc)
+    aref = build_vtable_entry_ref (aref, instance, idx);
+
+  return aref;
 }
 
 /* Return the name of the virtual function table (as an IDENTIFIER_NODE)
@@ -681,37 +502,6 @@ get_vtt_name (type)
   return mangle_vtt_for_type (type);
 }
 
-/* Return the offset to the main vtable for a given base BINFO.  */
-
-tree
-get_vfield_offset (binfo)
-     tree binfo;
-{
-  return
-    size_binop (PLUS_EXPR, byte_position (TYPE_VFIELD (BINFO_TYPE (binfo))),
-               BINFO_OFFSET (binfo));
-}
-
-/* Get the offset to the start of the original binfo that we derived
-   this binfo from.  If we find TYPE first, return the offset only
-   that far.  The shortened search is useful because the this pointer
-   on method calling is expected to point to a DECL_CONTEXT (fndecl)
-   object, and not a baseclass of it.   */
-
-static tree
-get_derived_offset (binfo, type)
-     tree binfo, type;
-{
-  tree offset1 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
-  tree offset2;
-
-  while (!same_type_p (BINFO_TYPE (binfo), type))
-    binfo = get_primary_binfo (binfo);
-
-  offset2 = get_vfield_offset (TYPE_BINFO (BINFO_TYPE (binfo)));
-  return size_binop (MINUS_EXPR, offset1, offset2);
-}
-
 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
    (For a secondary vtable for B-in-D, CLASS_TYPE should be D, not B.)
    Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
@@ -725,14 +515,16 @@ build_vtable (class_type, name, vtable_type)
   tree decl;
 
   decl = build_lang_decl (VAR_DECL, name, vtable_type);
+  /* vtable names are already mangled; give them their DECL_ASSEMBLER_NAME
+     now to avoid confusion in mangle_decl.  */
+  SET_DECL_ASSEMBLER_NAME (decl, name);
   DECL_CONTEXT (decl) = class_type;
   DECL_ARTIFICIAL (decl) = 1;
   TREE_STATIC (decl) = 1;
-#ifndef WRITABLE_VTABLES
-  /* Make them READONLY by default. (mrs) */
   TREE_READONLY (decl) = 1;
-#endif
   DECL_VIRTUAL_P (decl) = 1;
+  DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
+
   import_export_vtable (decl, class_type, 0);
 
   return decl;
@@ -760,7 +552,6 @@ get_vtable_decl (type, complete)
     }
   
   decl = build_vtable (type, name, void_type_node);
-  SET_DECL_ASSEMBLER_NAME (decl, name);
   decl = pushdecl_top_level (decl);
   my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
                      20000517);
@@ -794,12 +585,11 @@ copy_virtuals (binfo)
     {
       BV_VCALL_INDEX (t) = NULL_TREE;
       BV_USE_VCALL_INDEX_P (t) = 0;
-      BV_GENERATE_THUNK_WITH_VTABLE_P (t) = 0;
     }
 
   return copies;
 }
-      
+
 /* Build the primary virtual function table for TYPE.  If BINFO is
    non-NULL, build the vtable starting with the initial approximation
    that it is the same as the one which is the head of the association
@@ -847,15 +637,15 @@ build_primary_vtable (binfo, type)
   return 1;
 }
 
-/* Give TYPE a new virtual function table which is initialized
+/* Give BINFO a new virtual function table which is initialized
    with a skeleton-copy of its original initialization.  The only
    entry that changes is the `delta' entry, so we can really
    share a lot of structure.
 
-   FOR_TYPE is the derived type which caused this table to
+   FOR_TYPE is the most derived type which caused this table to
    be needed.
 
-   BINFO is the type association which provided TYPE for FOR_TYPE.
+   Returns non-zero if we haven't met BINFO before.
 
    The order in which vtables are built (by calling this function) for
    an object must remain the same, otherwise a binary incompatibility
@@ -865,155 +655,28 @@ static int
 build_secondary_vtable (binfo, for_type)
      tree binfo, for_type;
 {
-  tree basetype;
-  tree orig_decl = BINFO_VTABLE (binfo);
-  tree name;
-  tree new_decl;
-  tree offset;
-  tree path = binfo;
-  char *buf;
-  const char *buf2;
-  char joiner = '_';
-  int i;
-
-#ifdef JOINER
-  joiner = JOINER;
-#endif
-
-  if (TREE_VIA_VIRTUAL (binfo))
-    my_friendly_assert (binfo == binfo_for_vbase (BINFO_TYPE (binfo),
-                                                 current_class_type),
-                       170);
+  my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
 
-  if (BINFO_NEW_VTABLE_MARKED (binfo, current_class_type))
+  if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
     /* We already created a vtable for this base.  There's no need to
        do it again.  */
     return 0;
 
   /* Remember that we've created a vtable for this BINFO, so that we
      don't try to do so again.  */
-  SET_BINFO_NEW_VTABLE_MARKED (binfo, current_class_type);
+  SET_BINFO_NEW_VTABLE_MARKED (binfo, for_type);
   
   /* Make fresh virtual list, so we can smash it later.  */
   BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
 
-  if (TREE_VIA_VIRTUAL (binfo))
-    {
-      tree binfo1 = binfo_for_vbase (BINFO_TYPE (binfo), for_type);
-
-      /* XXX - This should never happen, if it does, the caller should
-        ensure that the binfo is from for_type's binfos, not from any
-        base type's.  We can remove all this code after a while.  */
-      if (binfo1 != binfo)
-       warning ("internal inconsistency: binfo offset error for rtti");
-
-      offset = BINFO_OFFSET (binfo1);
-    }
-  else
-    offset = BINFO_OFFSET (binfo);
-
-  /* In the new ABI, secondary vtables are laid out as part of the
-     same structure as the primary vtable.  */
-  if (merge_primary_and_secondary_vtables_p ())
-    {
-      BINFO_VTABLE (binfo) = NULL_TREE;
-      return 1;
-    }
-
-  /* Create the declaration for the secondary vtable.  */
-  basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (binfo));
-  buf2 = TYPE_ASSEMBLER_NAME_STRING (basetype);
-  i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1;
-
-  /* We know that the vtable that we are going to create doesn't exist
-     yet in the global namespace, and when we finish, it will be
-     pushed into the global namespace.  In complex MI hierarchies, we
-     have to loop while the name we are thinking of adding is globally
-     defined, adding more name components to the vtable name as we
-     loop, until the name is unique.  This is because in complex MI
-     cases, we might have the same base more than once.  This means
-     that the order in which this function is called for vtables must
-     remain the same, otherwise binary compatibility can be
-     compromised.  */
-
-  while (1)
-    {
-      char *buf1 = (char *) alloca (TYPE_ASSEMBLER_NAME_LENGTH (for_type)
-                                   + 1 + i);
-      char *new_buf2;
-
-      sprintf (buf1, "%s%c%s", TYPE_ASSEMBLER_NAME_STRING (for_type), joiner,
-              buf2);
-      buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX) + strlen (buf1) + 1);
-      sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
-      name = get_identifier (buf);
-
-      /* If this name doesn't clash, then we can use it, otherwise
-        we add more to the name until it is unique.  */
-
-      if (! IDENTIFIER_GLOBAL_VALUE (name))
-       break;
-
-      /* Set values for next loop through, if the name isn't unique.  */
-
-      path = BINFO_INHERITANCE_CHAIN (path);
-
-      /* We better not run out of stuff to make it unique.  */
-      my_friendly_assert (path != NULL_TREE, 368);
-
-      basetype = TYPE_MAIN_VARIANT (BINFO_TYPE (path));
-
-      if (for_type == basetype)
-       {
-         /* If we run out of basetypes in the path, we have already
-            found created a vtable with that name before, we now
-            resort to tacking on _%d to distinguish them.  */
-         int j = 2;
-         i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i + 1 + 3;
-         buf1 = (char *) alloca (i);
-         do {
-           sprintf (buf1, "%s%c%s%c%d",
-                    TYPE_ASSEMBLER_NAME_STRING (basetype), joiner,
-                    buf2, joiner, j);
-           buf = (char *) alloca (strlen (VTABLE_NAME_PREFIX)
-                                  + strlen (buf1) + 1);
-           sprintf (buf, "%s%s", VTABLE_NAME_PREFIX, buf1);
-           name = get_identifier (buf);
-
-           /* If this name doesn't clash, then we can use it,
-              otherwise we add something different to the name until
-              it is unique.  */
-         } while (++j <= 999 && IDENTIFIER_GLOBAL_VALUE (name));
-
-         /* Hey, they really like MI don't they?  Increase the 3
-             above to 6, and the 999 to 999999.  :-)  */
-         my_friendly_assert (j <= 999, 369);
-
-         break;
-       }
-
-      i = TYPE_ASSEMBLER_NAME_LENGTH (basetype) + 1 + i;
-      new_buf2 = (char *) alloca (i);
-      sprintf (new_buf2, "%s%c%s",
-              TYPE_ASSEMBLER_NAME_STRING (basetype), joiner, buf2);
-      buf2 = new_buf2;
-    }
-
-  new_decl = build_vtable (for_type, name, TREE_TYPE (orig_decl));
-  DECL_ALIGN (new_decl) = DECL_ALIGN (orig_decl);
-  DECL_USER_ALIGN (new_decl) = DECL_USER_ALIGN (orig_decl);
-  BINFO_VTABLE (binfo) = pushdecl_top_level (new_decl);
-
-#ifdef GATHER_STATISTICS
-  n_vtables += 1;
-  n_vtable_elems += list_length (BINFO_VIRTUALS (binfo));
-#endif
-
+  /* Secondary vtables are laid out as part of the same structure as
+     the primary vtable.  */
+  BINFO_VTABLE (binfo) = NULL_TREE;
   return 1;
 }
 
 /* Create a new vtable for BINFO which is the hierarchy dominated by
-   T.  */
+   T. Return non-zero if we actually created a new vtable.  */
 
 static int
 make_new_vtable (t, binfo)
@@ -1022,9 +685,11 @@ make_new_vtable (t, binfo)
 {
   if (binfo == TYPE_BINFO (t))
     /* In this case, it is *type*'s vtable we are modifying.  We start
-       with the approximation that it's vtable is that of the
+       with the approximation that its vtable is that of the
        immediate base class.  */
-    return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))), 
+    /* ??? This actually passes TYPE_BINFO (t), not the primary base binfo,
+       since we've updated DECL_CONTEXT (TYPE_VFIELD (t)) by now.  */
+    return build_primary_vtable (TYPE_BINFO (DECL_CONTEXT (TYPE_VFIELD (t))),
                                 t);
   else
     /* This is our very own copy of `basetype' to play with.  Later,
@@ -1075,7 +740,12 @@ modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
       BV_FN (v) = fndecl;
 
       /* Now assign virtual dispatch information, if unset.  We can
-        dispatch this, through any overridden base function.  */
+        dispatch this through any overridden base function.
+
+        FIXME this can choose a secondary vtable if the primary is not
+        also lexically first, leading to useless conversions.
+        In the V3 ABI, there's no reason for DECL_VIRTUAL_CONTEXT to
+        ever be different from DECL_CONTEXT.  */
       if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
        {
          DECL_VINDEX (fndecl) = DECL_VINDEX (base_fndecl);
@@ -1094,64 +764,11 @@ set_vindex (decl, vfuns_p)
 {
   int vindex;
 
-  vindex = (*vfuns_p)++;
+  vindex = *vfuns_p;
+  *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
+              ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
   DECL_VINDEX (decl) = build_shared_int_cst (vindex);
 }
-
-/* Add a virtual function to all the appropriate vtables for the class
-   T.  DECL_VINDEX(X) should be error_mark_node, if we want to
-   allocate a new slot in our table.  If it is error_mark_node, we
-   know that no other function from another vtable is overridden by X.
-   VFUNS_P keeps track of how many virtuals there are in our
-   main vtable for the type, and we build upon the NEW_VIRTUALS list
-   and return it.  */
-
-static void
-add_virtual_function (new_virtuals_p, overridden_virtuals_p,
-                     vfuns_p, fndecl, t)
-     tree *new_virtuals_p;
-     tree *overridden_virtuals_p;
-     int *vfuns_p;
-     tree fndecl;
-     tree t; /* Structure type.  */
-{
-  tree new_virtual;
-
-  /* If this function doesn't override anything from a base class, we
-     can just assign it a new DECL_VINDEX now.  Otherwise, if it does
-     override something, we keep it around and assign its DECL_VINDEX
-     later, in modify_all_vtables.  */
-  if (TREE_CODE (DECL_VINDEX (fndecl)) == INTEGER_CST)
-    /* We've already dealt with this function.  */
-    return;
-
-  new_virtual = make_node (TREE_LIST);
-  BV_FN (new_virtual) = fndecl;
-  BV_DELTA (new_virtual) = integer_zero_node;
-
-  if (DECL_VINDEX (fndecl) == error_mark_node)
-    {
-      /* FNDECL is a new virtual function; it doesn't override any
-        virtual function in a base class.  */
-
-      /* We remember that this was the base sub-object for rtti.  */
-      CLASSTYPE_RTTI (t) = t;
-
-      /* Now assign virtual dispatch information.  */
-      set_vindex (fndecl, vfuns_p);
-      DECL_VIRTUAL_CONTEXT (fndecl) = t;
-
-      /* Save the state we've computed on the NEW_VIRTUALS list.  */
-      TREE_CHAIN (new_virtual) = *new_virtuals_p;
-      *new_virtuals_p = new_virtual;
-    }
-  else
-    {
-      /* FNDECL overrides a function from a base class.  */
-      TREE_CHAIN (new_virtual) = *overridden_virtuals_p;
-      *overridden_virtuals_p = new_virtual;
-    }
-}
 \f
 /* Add method METHOD to class TYPE.  If ERROR_P is true, we are adding
    the method after the class has already been defined because a
@@ -1168,6 +785,8 @@ add_method (type, method, error_p)
   int len;
   int slot;
   tree method_vec;
+  int template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
+                        && DECL_TEMPLATE_CONV_FN_P (method));
 
   if (!CLASSTYPE_METHOD_VEC (type))
     /* Make a new method vector.  We start with 8 entries.  We must
@@ -1192,14 +811,36 @@ add_method (type, method, error_p)
     slot = CLASSTYPE_DESTRUCTOR_SLOT;
   else
     {
+      int have_template_convs_p = 0;
+      
       /* See if we already have an entry with this name.  */
       for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
-       if (!TREE_VEC_ELT (method_vec, slot)
-           || (DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, 
-                                                     slot))) 
-               == DECL_NAME (method)))
-         break;
-               
+       {
+         tree m = TREE_VEC_ELT (method_vec, slot);
+
+         if (!m)
+           break;
+         m = OVL_CURRENT (m);
+         
+         if (template_conv_p)
+           {
+             have_template_convs_p = (TREE_CODE (m) == TEMPLATE_DECL
+                                      && DECL_TEMPLATE_CONV_FN_P (m));
+             
+             /* If we need to move things up, see if there's
+                space. */
+             if (!have_template_convs_p)
+               {
+                 slot = len - 1;
+                 if (TREE_VEC_ELT (method_vec, slot))
+                   slot++;
+               }
+             break;
+           }
+         if (DECL_NAME (m) == DECL_NAME (method))
+           break;
+       }
+      
       if (slot == len)
        {
          /* We need a bigger method vector.  */
@@ -1218,9 +859,8 @@ add_method (type, method, error_p)
            new_len = len + 1;
 
          new_vec = make_tree_vec (new_len);
-         bcopy ((PTR) &TREE_VEC_ELT (method_vec, 0),
-                (PTR) &TREE_VEC_ELT (new_vec, 0),
-                len * sizeof (tree));
+         memcpy (&TREE_VEC_ELT (new_vec, 0), &TREE_VEC_ELT (method_vec, 0),
+                 len * sizeof (tree));
          len = new_len;
          method_vec = CLASSTYPE_METHOD_VEC (type) = new_vec;
        }
@@ -1233,22 +873,27 @@ add_method (type, method, error_p)
             slide some of the vector elements up.  In theory, this
             makes this algorithm O(N^2) but we don't expect many
             conversion operators.  */
-         for (slot = 2; slot < len; ++slot)
-           {
-             tree fn = TREE_VEC_ELT (method_vec, slot);
+         if (template_conv_p)
+           slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
+         else
+           for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT; slot < len; ++slot)
+             {
+               tree fn = TREE_VEC_ELT (method_vec, slot);
   
-             if (!fn)
-               /* There are no more entries in the vector, so we
-                  can insert the new conversion operator here.  */
-               break;
+               if (!fn)
+                 /* There are no more entries in the vector, so we
+                    can insert the new conversion operator here.  */
+                 break;
                  
-             if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
-               /* We can insert the new function right at the
-                  SLOTth position.  */
-               break;
-           }
-  
-         if (!TREE_VEC_ELT (method_vec, slot))
+               if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
+                 /* We can insert the new function right at the
+                    SLOTth position.  */
+                 break;
+             }
+
+         if (template_conv_p && have_template_convs_p)
+           /*OK*/;
+         else if (!TREE_VEC_ELT (method_vec, slot))
            /* There is nothing in the Ith slot, so we can avoid
               moving anything.  */
                ; 
@@ -1257,9 +902,9 @@ add_method (type, method, error_p)
              /* We know the last slot in the vector is empty
                 because we know that at this point there's room
                 for a new function.  */
-             bcopy ((PTR) &TREE_VEC_ELT (method_vec, slot),
-                    (PTR) &TREE_VEC_ELT (method_vec, slot + 1),
-                    (len - slot - 1) * sizeof (tree));
+             memmove (&TREE_VEC_ELT (method_vec, slot + 1),
+                      &TREE_VEC_ELT (method_vec, slot),
+                      (len - slot - 1) * sizeof (tree));
              TREE_VEC_ELT (method_vec, slot) = NULL_TREE;
            }
        }
@@ -1279,53 +924,60 @@ add_method (type, method, error_p)
           fns = OVL_NEXT (fns))
        {
          tree fn = OVL_CURRENT (fns);
-                
+         tree parms1;
+         tree parms2;
+         bool same = 1;
+
          if (TREE_CODE (fn) != TREE_CODE (method))
            continue;
 
-         if (TREE_CODE (method) != TEMPLATE_DECL)
+         /* [over.load] Member function declarations with the
+            same name and the same parameter types cannot be
+            overloaded if any of them is a static member
+            function declaration.
+
+            [namespace.udecl] When a using-declaration brings names
+            from a base class into a derived class scope, member
+            functions in the derived class override and/or hide member
+            functions with the same name and parameter types in a base
+            class (rather than conflicting).  */
+         parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
+         parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
+
+         /* Compare the quals on the 'this' parm.  Don't compare
+            the whole types, as used functions are treated as
+            coming from the using class in overload resolution.  */
+         if (! DECL_STATIC_FUNCTION_P (fn)
+             && ! DECL_STATIC_FUNCTION_P (method)
+             && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
+                 != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
+           same = 0;
+         if (! DECL_STATIC_FUNCTION_P (fn))
+           parms1 = TREE_CHAIN (parms1);
+         if (! DECL_STATIC_FUNCTION_P (method))
+           parms2 = TREE_CHAIN (parms2);
+
+         if (same && compparms (parms1, parms2) 
+             && (!DECL_CONV_FN_P (fn) 
+                 || same_type_p (TREE_TYPE (TREE_TYPE (fn)),
+                                 TREE_TYPE (TREE_TYPE (method)))))
            {
-             /* [over.load] Member function declarations with the
-                same name and the same parameter types cannot be
-                overloaded if any of them is a static member
-                function declaration.  */
-             if ((DECL_STATIC_FUNCTION_P (fn)
-                  != DECL_STATIC_FUNCTION_P (method))
-                 || using)
+             if (using && DECL_CONTEXT (fn) == type)
+               /* Defer to the local function.  */
+               return;
+             else
                {
-                 tree parms1 = TYPE_ARG_TYPES (TREE_TYPE (fn));
-                 tree parms2 = TYPE_ARG_TYPES (TREE_TYPE (method));
-
-                 if (! DECL_STATIC_FUNCTION_P (fn))
-                   parms1 = TREE_CHAIN (parms1);
-                 if (! DECL_STATIC_FUNCTION_P (method))
-                   parms2 = TREE_CHAIN (parms2);
-
-                 if (compparms (parms1, parms2))
-                   {
-                     if (using)
-                       /* Defer to the local function.  */
-                       return;
-                     else
-                       cp_error ("`%#D' and `%#D' cannot be overloaded",
-                                 fn, method);
-                   }
+                 cp_error_at ("`%#D' and `%#D' cannot be overloaded",
+                              method, fn, method);
+
+                 /* We don't call duplicate_decls here to merge
+                    the declarations because that will confuse
+                    things if the methods have inline
+                    definitions.  In particular, we will crash
+                    while processing the definitions.  */
+                 return;
                }
            }
-
-         if (!decls_match (fn, method))
-           continue;
-
-         /* There has already been a declaration of this method
-            or member template.  */
-         cp_error_at ("`%D' has already been declared in `%T'", 
-                      method, type);
-
-         /* We don't call duplicate_decls here to merge the
-            declarations because that will confuse things if the
-            methods have inline definitions.  In particular, we
-            will crash while processing the definitions.  */
-         return;
        }
     }
 
@@ -1333,7 +985,7 @@ add_method (type, method, error_p)
   TREE_VEC_ELT (method_vec, slot) 
     = build_overload (method, TREE_VEC_ELT (method_vec, slot));
 
-      /* Add the new binding.  */ 
+  /* Add the new binding.  */ 
   if (!DECL_CONSTRUCTOR_P (method)
       && !DECL_DESTRUCTOR_P (method))
     push_class_level_binding (DECL_NAME (method),
@@ -1390,7 +1042,7 @@ delete_duplicate_fields_1 (field, fields)
                }
            }
          else if (TREE_CODE (field) == USING_DECL)
-           /* A using declaration may is allowed to appear more than
+           /* A using declaration is allowed to appear more than
               once.  We'll prune these from the field list later, and
               handle_using_decl will complain about invalid multiple
               uses.  */
@@ -1458,6 +1110,9 @@ alter_access (t, fdecl, access)
   if (!DECL_LANG_SPECIFIC (fdecl))
     retrofit_lang_decl (fdecl);
 
+  if (DECL_DISCRIMINATOR_P (fdecl))
+    abort ();
+
   elem = purpose_member (t, DECL_ACCESS (fdecl));
   if (elem)
     {
@@ -1506,14 +1161,12 @@ handle_using_decl (using_decl, t)
   if (! binfo)
     return;
   
-  if (name == constructor_name (ctype)
-      || name == constructor_name_full (ctype))
+  if (constructor_name_p (name, ctype))
     {
       cp_error_at ("`%D' names constructor", using_decl);
       return;
     }
-  if (name == constructor_name (t)
-      || name == constructor_name_full (t))
+  if (constructor_name_p (name, t))
     {
       cp_error_at ("`%D' invalid in `%T'", using_decl, t);
       return;
@@ -1529,7 +1182,7 @@ handle_using_decl (using_decl, t)
 
   if (BASELINK_P (fdecl))
     /* Ignore base type this came from. */
-    fdecl = TREE_VALUE (fdecl);
+    fdecl = BASELINK_FUNCTIONS (fdecl);
 
   old_value = IDENTIFIER_CLASS_VALUE (name);
   if (old_value)
@@ -1594,12 +1247,12 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
 {
   int n_baseclasses;
   int i;
-  int seen_nearly_empty_base_p;
+  int seen_non_virtual_nearly_empty_base_p;
   tree binfos;
 
   binfos = TYPE_BINFO_BASETYPES (t);
   n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
-  seen_nearly_empty_base_p = 0;
+  seen_non_virtual_nearly_empty_base_p = 0;
 
   /* An aggregate cannot have baseclasses.  */
   CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
@@ -1636,7 +1289,7 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
         dtor is handled in finish_struct_1.  */
       if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
          && TYPE_HAS_DESTRUCTOR (basetype))
-       cp_warning ("base class `%#T' has a non-virtual destructor",
+       warning ("base class `%#T' has a non-virtual destructor",
                    basetype);
 
       /* If the base class doesn't have copy constructors or
@@ -1656,23 +1309,27 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
        {
          *cant_have_default_ctor_p = 1;
          if (! TYPE_HAS_CONSTRUCTOR (t))
-            cp_pedwarn ("base `%T' with only non-default constructor in class without a constructor",
+            pedwarn ("base `%T' with only non-default constructor in class without a constructor",
                         basetype);
        }
 
-      /* If the base class is not empty or nearly empty, then this
-        class cannot be nearly empty.  */
-      if (!CLASSTYPE_NEARLY_EMPTY_P (basetype) && !is_empty_class (basetype))
-       CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
-      /* And if there is more than one nearly empty base, then the
-        derived class is not nearly empty either.  */
-      else if (CLASSTYPE_NEARLY_EMPTY_P (basetype) 
-              && seen_nearly_empty_base_p)
-       CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
-      /* If this is the first nearly empty base class, then remember
-        that we saw it.  */
+      if (TREE_VIA_VIRTUAL (base_binfo))
+       /* A virtual base does not effect nearly emptiness. */
+       ;
       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
-       seen_nearly_empty_base_p = 1;
+       {
+         if (seen_non_virtual_nearly_empty_base_p)
+           /* And if there is more than one nearly empty base, then the
+              derived class is not nearly empty either.  */
+           CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
+         else
+           /* Remember we've seen one. */
+           seen_non_virtual_nearly_empty_base_p = 1;
+       }
+      else if (!is_empty_class (basetype))
+       /* If the base class is not empty or nearly empty, then this
+          class cannot be nearly empty.  */
+       CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
 
       /* A lot of properties from the bases also apply to the derived
         class.  */
@@ -1686,24 +1343,12 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
       TYPE_OVERLOADS_ARRAY_REF (t) |= TYPE_OVERLOADS_ARRAY_REF (basetype);
       TYPE_OVERLOADS_ARROW (t) |= TYPE_OVERLOADS_ARROW (basetype);
       TYPE_POLYMORPHIC_P (t) |= TYPE_POLYMORPHIC_P (basetype);
-
-      /* Derived classes can implicitly become COMified if their bases
-        are COM.  */
-      if (CLASSTYPE_COM_INTERFACE (basetype))
-       CLASSTYPE_COM_INTERFACE (t) = 1;
-      else if (i == 0 && CLASSTYPE_COM_INTERFACE (t))
-       {
-         cp_error 
-           ("COM interface type `%T' with non-COM leftmost base class `%T'",
-            t, basetype);
-         CLASSTYPE_COM_INTERFACE (t) = 0;
-       }
     }
 }
 
-/* Binfo FROM is within a virtual heirarchy which is being reseated to
+/* Binfo FROM is within a virtual hierarchy which is being reseated to
    TO. Move primary information from FROM to TO, and recursively traverse
-   into FROM's bases. The heirarchy is dominated by TYPE.  MAPPINGS is an
+   into FROM's bases. The hierarchy is dominated by TYPE.  MAPPINGS is an
    assoc list of binfos that have already been reseated.  */
 
 static void
@@ -1714,10 +1359,15 @@ force_canonical_binfo_r (to, from, type, mappings)
      tree mappings;
 {
   int i, n_baseclasses = BINFO_N_BASETYPES (from);
-  
+
+  my_friendly_assert (to != from, 20010905);
   BINFO_INDIRECT_PRIMARY_P (to)
           = BINFO_INDIRECT_PRIMARY_P (from);
   BINFO_INDIRECT_PRIMARY_P (from) = 0;
+  BINFO_UNSHARED_MARKED (to) = BINFO_UNSHARED_MARKED (from);
+  BINFO_UNSHARED_MARKED (from) = 0;
+  BINFO_LOST_PRIMARY_P (to) = BINFO_LOST_PRIMARY_P (from);
+  BINFO_LOST_PRIMARY_P (from) = 0;
   if (BINFO_PRIMARY_P (from))
     {
       tree primary = BINFO_PRIMARY_BASE_OF (from);
@@ -1731,18 +1381,31 @@ force_canonical_binfo_r (to, from, type, mappings)
       BINFO_PRIMARY_BASE_OF (to) = primary;
       BINFO_PRIMARY_BASE_OF (from) = NULL_TREE;
     }
-  my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)), 20010104);
+  my_friendly_assert (same_type_p (BINFO_TYPE (to), BINFO_TYPE (from)),
+                     20010104);
   mappings = tree_cons (from, to, mappings);
+
+  if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (from))
+      && TREE_VIA_VIRTUAL (CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (from))))
+    {
+      tree from_primary = get_primary_binfo (from);
+      
+      if (BINFO_PRIMARY_BASE_OF (from_primary) == from)
+       force_canonical_binfo (get_primary_binfo (to), from_primary,
+                              type, mappings);
+    }
+  
   for (i = 0; i != n_baseclasses; i++)
     {
       tree from_binfo = BINFO_BASETYPE (from, i);
       tree to_binfo = BINFO_BASETYPE (to, i);
-      
+
       if (TREE_VIA_VIRTUAL (from_binfo))
         {
-          tree shared_binfo = binfo_for_vbase (BINFO_TYPE (from_binfo), type);
-          
-          if (shared_binfo == from_binfo)
+         if (BINFO_PRIMARY_P (from_binfo) &&
+             purpose_member (BINFO_PRIMARY_BASE_OF (from_binfo), mappings))
+           /* This base is a primary of some binfo we have already
+              reseated. We must reseat this one too.  */
             force_canonical_binfo (to_binfo, from_binfo, type, mappings);
         }
       else
@@ -1751,7 +1414,7 @@ force_canonical_binfo_r (to, from, type, mappings)
 }
 
 /* FROM is the canonical binfo for a virtual base. It is being reseated to
-   make TO the canonical binfo, within the heirarchy dominated by TYPE.
+   make TO the canonical binfo, within the hierarchy dominated by TYPE.
    MAPPINGS is an assoc list of binfos that have already been reseated.
    Adjust any non-virtual bases within FROM, and also move any virtual bases
    which are canonical.  This complication arises because selecting primary
@@ -1769,30 +1432,29 @@ force_canonical_binfo (to, from, type, mappings)
 {
   tree assoc = purpose_member (BINFO_TYPE (to),
                               CLASSTYPE_VBASECLASSES (type));
-  TREE_VALUE (assoc) = to;
-  force_canonical_binfo_r (to, from, type, mappings);
+  if (TREE_VALUE (assoc) != to)
+    {
+      TREE_VALUE (assoc) = to;
+      force_canonical_binfo_r (to, from, type, mappings);
+    }
 }
 
-/* Make BASE_BINFO the primary virtual base of BINFO within the hierarchy
-   dominated by TYPE. Returns BASE_BINFO, if it can be made so, NULL
+/* Make BASE_BINFO the a primary virtual base within the hierarchy
+   dominated by TYPE. Returns BASE_BINFO, if it is not already one, NULL
    otherwise (because something else has already made it primary).  */
 
 static tree
-mark_primary_virtual_base (binfo, base_binfo, type)
-     tree binfo;
+mark_primary_virtual_base (base_binfo, type)
      tree base_binfo;
      tree type;
 {
   tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
-  tree delta;
 
   if (BINFO_PRIMARY_P (shared_binfo))
     {
       /* It's already allocated in the hierarchy. BINFO won't have a
-         primary base in this hierachy, even though the complete object
+         primary base in this hierarchy, even though the complete object
          BINFO is for, would do.  */
-      BINFO_LOST_PRIMARY_P (binfo) = 1;
-      
       return NULL_TREE;
     }
      
@@ -1803,13 +1465,60 @@ mark_primary_virtual_base (binfo, base_binfo, type)
   if (base_binfo != shared_binfo)
     force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
 
-  delta = size_diffop (BINFO_OFFSET (binfo), BINFO_OFFSET (base_binfo));
-  if (!integer_zerop (delta))
+  return base_binfo;
+}
+
+/* If BINFO is an unmarked virtual binfo for a class with a primary virtual
+   base, then BINFO has no primary base in this graph.  Called from
+   mark_primary_bases.  DATA is the most derived type. */
+
+static tree dfs_unshared_virtual_bases (binfo, data)
+     tree binfo;
+     void *data;
+{
+  tree t = (tree) data;
+  
+  if (!BINFO_UNSHARED_MARKED (binfo)
+      && CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
     {
-      propagate_binfo_offsets (base_binfo, delta);
-      BINFO_OFFSET (base_binfo) = BINFO_OFFSET (binfo);
+      /* This morally virtual base has a primary base when it
+         is a complete object. We need to locate the shared instance
+         of this binfo in the type dominated by T. We duplicate the
+         primary base information from there to here.  */
+      tree vbase;
+      tree unshared_base;
+      
+      for (vbase = binfo; !TREE_VIA_VIRTUAL (vbase);
+          vbase = BINFO_INHERITANCE_CHAIN (vbase))
+       continue;
+      unshared_base = get_original_base (binfo,
+                                        binfo_for_vbase (BINFO_TYPE (vbase),
+                                                         t));
+      my_friendly_assert (unshared_base != binfo, 20010612);
+      BINFO_LOST_PRIMARY_P (binfo) = BINFO_LOST_PRIMARY_P (unshared_base);
+      if (!BINFO_LOST_PRIMARY_P (binfo))
+       BINFO_PRIMARY_BASE_OF (get_primary_binfo (binfo)) = binfo;
     }
-  return base_binfo;
+  
+  if (binfo != TYPE_BINFO (t))
+    /* The vtable fields will have been copied when duplicating the
+       base binfos. That information is bogus, make sure we don't try
+       and use it. */
+    BINFO_VTABLE (binfo) = NULL_TREE;
+
+  /* If this is a virtual primary base, make sure its offset matches
+     that which it is primary for. */
+  if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo) &&
+      binfo_for_vbase (BINFO_TYPE (binfo), t) == binfo)
+    {
+      tree delta = size_diffop (BINFO_OFFSET (BINFO_PRIMARY_BASE_OF (binfo)),
+                               BINFO_OFFSET (binfo));
+      if (!integer_zerop (delta))
+       propagate_binfo_offsets (binfo, delta, t);
+    }
+  
+  BINFO_UNSHARED_MARKED (binfo) = 0;
+  return NULL;
 }
 
 /* Set BINFO_PRIMARY_BASE_OF for all binfos in the hierarchy
@@ -1833,11 +1542,24 @@ mark_primary_bases (type)
       base_binfo = get_primary_binfo (binfo);
 
       if (TREE_VIA_VIRTUAL (base_binfo))
-        base_binfo = mark_primary_virtual_base (binfo, base_binfo, type);
+        base_binfo = mark_primary_virtual_base (base_binfo, type);
 
       if (base_binfo)
         BINFO_PRIMARY_BASE_OF (base_binfo) = binfo;
+      else
+       BINFO_LOST_PRIMARY_P (binfo) = 1;
+      
+      BINFO_UNSHARED_MARKED (binfo) = 1;
     }
+  /* There could remain unshared morally virtual bases which were not
+     visited in the inheritance graph walk. These bases will have lost
+     their virtual primary base (should they have one). We must now
+     find them. Also we must fix up the BINFO_OFFSETs of primary
+     virtual bases. We could not do that as we went along, as they
+     were originally copied from the bases we inherited from by
+     unshare_base_binfos. That may have decided differently about
+     where a virtual primary base went.  */
+  dfs_walk (TYPE_BINFO (type), dfs_unshared_virtual_bases, NULL, type);
 }
 
 /* Make the BINFO the primary base of T.  */
@@ -1889,9 +1611,8 @@ determine_primary_base (t, vfuns_p)
          if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
            CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
 
-         /* A virtual baseclass can't be the primary base under the
-            old ABI.  And under the new ABI we still prefer a
-            non-virtual base.  */
+         /* We prefer a non-virtual base, although a virtual one will
+            do.  */
          if (TREE_VIA_VIRTUAL (base_binfo))
            continue;
 
@@ -1960,9 +1681,8 @@ determine_primary_base (t, vfuns_p)
        }
     }
 
-  /* The new ABI allows for the use of a "nearly-empty" virtual base
-     class as the primary base class if no non-virtual polymorphic
-     base can be found.  */
+  /* A "nearly-empty" virtual base class can be the primary base
+     class, if no non-virtual polymorphic base can be found.  */
   if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
     {
       /* If not NULL, this is the best primary base candidate we have
@@ -2070,17 +1790,11 @@ finish_struct_bits (t)
        }
     }
 
-  /* If this type has a copy constructor, force its mode to be BLKmode, and
-     force its TREE_ADDRESSABLE bit to be nonzero.  This will cause it to
-     be passed by invisible reference and prevent it from being returned in
-     a register.
-
-     Also do this if the class has BLKmode but can still be returned in
-     registers, since function_cannot_inline_p won't let us inline
-     functions returning such a type.  This affects the HP-PA.  */
-  if (! TYPE_HAS_TRIVIAL_INIT_REF (t)
-      || (TYPE_MODE (t) == BLKmode && ! aggregate_value_p (t)
-         && CLASSTYPE_NON_AGGREGATE (t)))
+  /* If this type has a copy constructor or a destructor, force its mode to
+     be BLKmode, and force its TREE_ADDRESSABLE bit to be nonzero.  This
+     will cause it to be passed by invisible reference and prevent it from
+     being returned in a register.  */
+  if (! TYPE_HAS_TRIVIAL_INIT_REF (t) || TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
     {
       tree variants;
       DECL_MODE (TYPE_MAIN_DECL (t)) = BLKmode;
@@ -2175,7 +1889,7 @@ maybe_warn_about_overly_private_class (t)
          }
       if (!has_nonprivate_method) 
        {
-         cp_warning ("all member functions in class `%T' are private", t);
+         warning ("all member functions in class `%T' are private", t);
          return;
        }
     }
@@ -2189,7 +1903,7 @@ maybe_warn_about_overly_private_class (t)
 
       if (TREE_PRIVATE (dtor))
        {
-         cp_warning ("`%#T' only defines a private destructor and has no friends",
+         warning ("`%#T' only defines a private destructor and has no friends",
                      t);
          return;
        }
@@ -2232,7 +1946,7 @@ maybe_warn_about_overly_private_class (t)
 
       if (nonprivate_ctor == 0)
        {
-         cp_warning ("`%#T' only defines private constructors and has no friends",
+         warning ("`%#T' only defines private constructors and has no friends",
                      t);
          return;
        }
@@ -2353,7 +2067,7 @@ void
 duplicate_tag_error (t)
      tree t;
 {
-  cp_error ("redefinition of `%#T'", t);
+  error ("redefinition of `%#T'", t);
   cp_error_at ("previous definition of `%#T'", t);
 
   /* Pretend we haven't defined this type.  */
@@ -2392,6 +2106,7 @@ duplicate_tag_error (t)
       memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
       BINFO_BASETYPES(binfo) = NULL_TREE;
 
+      TYPE_LANG_SPECIFIC (t)->u.h.is_lang_type_class = 1;
       TYPE_BINFO (t) = binfo;
       CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
       SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
@@ -2405,7 +2120,6 @@ duplicate_tag_error (t)
   TYPE_METHODS (t) = NULL_TREE;
   TYPE_VFIELD (t) = NULL_TREE;
   TYPE_CONTEXT (t) = NULL_TREE;
-  TYPE_NONCOPIED_PARTS (t) = NULL_TREE;
   
   /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above.  */
   TYPE_LANG_FLAG_0 (t) = 0;
@@ -2419,7 +2133,7 @@ duplicate_tag_error (t)
   SET_IS_AGGR_TYPE (t, 1);
 }
 
-/* Make the BINFO's vtablehave N entries, including RTTI entries,
+/* Make BINFO's vtable have N entries, including RTTI entries,
    vbase and vcall offsets, etc.  Set its type and call the backend
    to lay it out.  */
 
@@ -2428,13 +2142,11 @@ layout_vtable_decl (binfo, n)
      tree binfo;
      int n;
 {
-  tree itype;
   tree atype;
   tree vtable;
 
-  itype = size_int (n);
   atype = build_cplus_array_type (vtable_entry_type, 
-                                 build_index_type (itype));
+                                 build_index_type (size_int (n - 1)));
   layout_type (atype);
 
   /* We may have to grow the vtable.  */
@@ -2493,6 +2205,9 @@ typedef struct find_final_overrider_data_s {
   tree most_derived_type;
   /* The final overriding function.  */
   tree overriding_fn;
+  /* The functions that we thought might be final overriders, but
+     aren't.  */
+  tree candidates;
   /* The BINFO for the class in which the final overriding function
      appears.  */
   tree overriding_base;
@@ -2523,13 +2238,8 @@ dfs_find_final_overrider (binfo, data)
           path; 
           path = TREE_CHAIN (path))
        {
-         for (method = TYPE_METHODS (BINFO_TYPE (TREE_VALUE (path)));
-              method;
-              method = TREE_CHAIN (method))
-           if (DECL_VIRTUAL_P (method) 
-               && same_signature_p (method, ffod->fn))
-             break;
-
+         method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
+                                           ffod->fn);
          if (method)
            break;
        }
@@ -2551,49 +2261,91 @@ dfs_find_final_overrider (binfo, data)
                break;
              }
 
-         if (ffod->overriding_fn && ffod->overriding_fn != method)
+         /* If we didn't already have an overrider, or any
+            candidates, then this function is the best candidate so
+            far.  */
+         if (!ffod->overriding_fn && !ffod->candidates)
            {
-             /* We've found a different overrider along a different
-                path.  That can be OK if the new one overrides the
-                old one.  Consider:
-             
-                  struct S { virtual void f(); };
-                  struct T : public virtual S { virtual void f(); };
-                  struct U : public virtual S, public virtual T {};
-             
-                Here `T::f' is the final overrider for `S::f'.  */
-             if (strictly_overrides (method, ffod->overriding_fn))
+             ffod->overriding_fn = method;
+             ffod->overriding_base = TREE_VALUE (path);
+           }
+         else if (ffod->overriding_fn)
+           {
+             /* We had a best overrider; let's see how this compares.  */
+
+             if (ffod->overriding_fn == method
+                 && (tree_int_cst_equal 
+                     (BINFO_OFFSET (TREE_VALUE (path)),
+                      BINFO_OFFSET (ffod->overriding_base))))
+               /* We found the same overrider we already have, and in the
+                  same place; it's still the best.  */;
+             else if (strictly_overrides (ffod->overriding_fn, method))
+               /* The old function overrides this function; it's still the
+                  best.  */;
+             else if (strictly_overrides (method, ffod->overriding_fn))
                {
+                 /* The new function overrides the old; it's now the
+                    best.  */
                  ffod->overriding_fn = method;
                  ffod->overriding_base = TREE_VALUE (path);
                }
-             else if (!strictly_overrides (ffod->overriding_fn, method))
+             else
                {
-                 cp_error ("no unique final overrider for `%D' in `%T'", 
-                           ffod->most_derived_type,
-                           ffod->fn);
-                 cp_error ("candidates are: `%#D'", ffod->overriding_fn);
-                 cp_error ("                `%#D'", method);
-                 return error_mark_node;
+                 /* Ambiguous.  */
+                 ffod->candidates 
+                   = build_tree_list (NULL_TREE,
+                                      ffod->overriding_fn);
+                 if (method != ffod->overriding_fn)
+                   ffod->candidates 
+                     = tree_cons (NULL_TREE, method, ffod->candidates);
+                 ffod->overriding_fn = NULL_TREE;
+                 ffod->overriding_base = NULL_TREE;
                }
            }
-         else if (ffod->overriding_base
-                  && (!tree_int_cst_equal 
-                      (BINFO_OFFSET (TREE_VALUE (path)),
-                       BINFO_OFFSET (ffod->overriding_base))))
-           {
-             /* We've found two instances of the same base that
-                provide overriders.  */
-             cp_error ("no unique final overrider for `%D' since there two instances of `%T' in `%T'", 
-                       ffod->fn,
-                       BINFO_TYPE (ffod->overriding_base),
-                       ffod->most_derived_type);
-             return error_mark_node;
-           }
          else
            {
-             ffod->overriding_fn = method;
-             ffod->overriding_base = TREE_VALUE (path);
+             /* We had a list of ambiguous overrides; let's see how this
+                new one compares.  */
+
+             tree candidates;
+             bool incomparable = false;
+
+             /* If there were previous candidates, and this function
+                overrides all of them, then it is the new best
+                candidate.  */
+             for (candidates = ffod->candidates;
+                  candidates;
+                  candidates = TREE_CHAIN (candidates))
+               {
+                 /* If the candidate overrides the METHOD, then we
+                    needn't worry about it any further.  */
+                 if (strictly_overrides (TREE_VALUE (candidates),
+                                         method))
+                   {
+                     method = NULL_TREE;
+                     break;
+                   }
+
+                 /* If the METHOD doesn't override the candidate,
+                    then it is incomporable.  */
+                 if (!strictly_overrides (method,
+                                          TREE_VALUE (candidates)))
+                   incomparable = true;
+               }
+
+             /* If METHOD overrode all the candidates, then it is the
+                new best candidate.  */
+             if (!candidates && !incomparable)
+               {
+                 ffod->overriding_fn = method;
+                 ffod->overriding_base = TREE_VALUE (path);
+                 ffod->candidates = NULL_TREE;
+               }
+             /* If METHOD didn't override all the candidates, then it
+                is another candidate.  */
+             else if (method && incomparable)
+               ffod->candidates 
+                 = tree_cons (NULL_TREE, method, ffod->candidates);
            }
        }
     }
@@ -2614,7 +2366,7 @@ find_final_overrider (t, binfo, fn)
 {
   find_final_overrider_data ffod;
 
-  /* Getting this right is a little tricky.  This is legal:
+  /* Getting this right is a little tricky.  This is valid:
 
        struct S { virtual void f (); };
        struct T { virtual void f (); };
@@ -2627,7 +2379,7 @@ find_final_overrider (t, binfo, fn)
        struct T : virtual public R { virtual void f (); };
        struct U : public S, public T { };
 
-     is not -- there's no way  to decide whether to put `S::f' or
+     is not -- there's no way to decide whether to put `S::f' or
      `T::f' in the vtable for `R'.  
      
      The solution is to look at all paths to BINFO.  If we find
@@ -2637,19 +2389,43 @@ find_final_overrider (t, binfo, fn)
   ffod.most_derived_type = t;
   ffod.overriding_fn = NULL_TREE;
   ffod.overriding_base = NULL_TREE;
+  ffod.candidates = NULL_TREE;
+
+  dfs_walk (TYPE_BINFO (t),
+           dfs_find_final_overrider,
+           NULL,
+           &ffod);
 
-  if (dfs_walk (TYPE_BINFO (t),
-               dfs_find_final_overrider,
-               NULL,
-               &ffod))
-    return error_mark_node;
+  /* If there was no winner, issue an error message.  */
+  if (!ffod.overriding_fn)
+    {
+      error ("no unique final overrider for `%D' in `%T'", fn, t);
+      return error_mark_node;
+    }
 
   return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
 }
 
-/* Update a entry in the vtable for BINFO, which is in the hierarchy
-   dominated by T.  FN has been overridden in BINFO; VIRTUALS points
-   to the corresponding position in the BINFO_VIRTUALS list.  */
+/* Returns the function from the BINFO_VIRTUALS entry in T which matches
+   the signature of FUNCTION_DECL FN, or NULL_TREE if none.  In other words,
+   the function that the slot in T's primary vtable points to.  */
+
+static tree get_matching_virtual PARAMS ((tree, tree));
+static tree
+get_matching_virtual (t, fn)
+     tree t, fn;
+{
+  tree f;
+
+  for (f = BINFO_VIRTUALS (TYPE_BINFO (t)); f; f = TREE_CHAIN (f))
+    if (same_signature_p (BV_FN (f), fn))
+      return BV_FN (f);
+  return NULL_TREE;
+}
+
+/* Update an entry in the vtable for BINFO, which is in the hierarchy
+   dominated by T.  FN has been overriden in BINFO; VIRTUALS points to the
+   corresponding position in the BINFO_VIRTUALS list.  */
 
 static void
 update_vtable_entry_for_fn (t, binfo, fn, virtuals)
@@ -2662,93 +2438,99 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
   tree overrider;
   tree delta;
   tree virtual_base;
-  int generate_thunk_with_vtable_p;
+  tree first_defn;
+  bool lost = false;
 
-  /* Find the function which originally caused this vtable
-     entry to be present.  */
-  b = binfo;
-  while (1)
+  /* Find the nearest primary base (possibly binfo itself) which defines
+     this function; this is the class the caller will convert to when
+     calling FN through BINFO.  */
+  for (b = binfo; ; b = get_primary_binfo (b))
     {
-      tree primary_base;
-      tree f;
-
-      primary_base = get_primary_binfo (b);
-      if (!primary_base)
+      if (look_for_overrides_here (BINFO_TYPE (b), fn))
        break;
 
-      for (f = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (primary_base)));
-          f;
-          f = TREE_CHAIN (f))
-       if (same_signature_p (BV_FN (f), fn))
-         break;
-
-      if (!f)
-       break;
-
-      fn = BV_FN (f);
-      b = primary_base;
+      /* The nearest definition is from a lost primary.  */
+      if (BINFO_LOST_PRIMARY_P (b))
+       lost = true;
     }
+  first_defn = b;
 
   /* Find the final overrider.  */
   overrider = find_final_overrider (t, b, fn);
   if (overrider == error_mark_node)
     return;
 
-  /* Compute the constant adjustment to the `this' pointer.  The
-     `this' pointer, when this function is called, will point at the
-     class whose vtable this is.  */
-  delta = size_binop (PLUS_EXPR,
-                     get_derived_offset (binfo,
-                                         DECL_VIRTUAL_CONTEXT (fn)),
-                     BINFO_OFFSET (binfo));
+  /* Check for unsupported covariant returns again now that we've
+     calculated the base offsets.  */
+  check_final_overrider (TREE_PURPOSE (overrider), fn);
 
   /* Assume that we will produce a thunk that convert all the way to
      the final overrider, and not to an intermediate virtual base.  */
   virtual_base = NULL_TREE;
 
-  /* Assume that we will always generate thunks with the vtables that
-     reference them.  */
-  generate_thunk_with_vtable_p = 1;
-
-  /* Under the new ABI, we will convert to an intermediate virtual
-     base first, and then use the vcall offset located there to finish
-     the conversion.  */
-  while (b)
+  /* See if we can convert to an intermediate virtual base first, and then
+     use the vcall offset located there to finish the conversion.  */
+  for (; b; b = BINFO_INHERITANCE_CHAIN (b))
     {
-      /* If we find BINFO, then the final overrider is in a class
-        derived from BINFO, so the thunks can be generated with
-        the final overrider.  */
-      if (!virtual_base
-         && same_type_p (BINFO_TYPE (b), BINFO_TYPE (binfo)))
-       generate_thunk_with_vtable_p = 0;
-
-         /* If we find the final overrider, then we can stop
-            walking.  */
+      /* If we find the final overrider, then we can stop
+        walking.  */
       if (same_type_p (BINFO_TYPE (b), 
                       BINFO_TYPE (TREE_VALUE (overrider))))
        break;
 
-         /* If we find a virtual base, and we haven't yet found the
-            overrider, then there is a virtual base between the
-            declaring base and the final overrider.  */
+      /* If we find a virtual base, and we haven't yet found the
+        overrider, then there is a virtual base between the
+        declaring base (first_defn) and the final overrider.  */
       if (!virtual_base && TREE_VIA_VIRTUAL (b))
-       {
-         generate_thunk_with_vtable_p = 1;
-         virtual_base = b;
-       }
-
-      b = BINFO_INHERITANCE_CHAIN (b);
+       virtual_base = b;
     }
 
+  /* Compute the constant adjustment to the `this' pointer.  The
+     `this' pointer, when this function is called, will point at BINFO
+     (or one of its primary bases, which are at the same offset).  */
+
   if (virtual_base)
-    /* The `this' pointer needs to be adjusted to the nearest virtual
-       base.  */
-    delta = size_diffop (BINFO_OFFSET (virtual_base), delta);
+    /* The `this' pointer needs to be adjusted from the declaration to
+       the nearest virtual base.  */
+    delta = size_diffop (BINFO_OFFSET (virtual_base),
+                        BINFO_OFFSET (first_defn));
+  else if (lost)
+    /* If the nearest definition is in a lost primary, we don't need an
+       entry in our vtable.  Except possibly in a constructor vtable,
+       if we happen to get our primary back.  In that case, the offset
+       will be zero, as it will be a primary base.  */
+    delta = size_zero_node;
   else
-    /* The `this' pointer needs to be adjusted from pointing to
-       BINFO to pointing at the base where the final overrider
-       appears.  */
-    delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)), delta);
+    {
+      /* The `this' pointer needs to be adjusted from pointing to
+        BINFO to pointing at the base where the final overrider
+        appears.  */
+      delta = size_diffop (BINFO_OFFSET (TREE_VALUE (overrider)),
+                          BINFO_OFFSET (binfo));
+
+      if (! integer_zerop (delta))
+       {
+         /* We'll need a thunk.  But if we have a (perhaps formerly)
+            primary virtual base, we have a vcall slot for this function,
+            so we can use it rather than create a non-virtual thunk.  */
+         
+         b = get_primary_binfo (first_defn);
+         for (; b; b = get_primary_binfo (b))
+           {
+             tree f = get_matching_virtual (BINFO_TYPE (b), fn);
+             if (!f)
+               /* b doesn't have this function; no suitable vbase.  */
+               break;
+             if (TREE_VIA_VIRTUAL (b))
+               {
+                 /* Found one; we can treat ourselves as a virtual base.  */
+                 virtual_base = binfo;
+                 delta = size_zero_node;
+                 break;
+               }
+           }
+       }
+    }
 
   modify_vtable_entry (t, 
                       binfo, 
@@ -2758,8 +2540,6 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
 
   if (virtual_base)
     BV_USE_VCALL_INDEX_P (*virtuals) = 1;
-  if (generate_thunk_with_vtable_p)
-    BV_GENERATE_THUNK_WITH_VTABLE_P (*virtuals) = 1;
 }
 
 /* Called from modify_all_vtables via dfs_walk.  */
@@ -2770,9 +2550,9 @@ dfs_modify_vtables (binfo, data)
      void *data;
 {
   if (/* There's no need to modify the vtable for a non-virtual
-         primary base; we're not going to use that vtable anyhow
-         (virtual primary bases can become non-primary in a
-         class derivation of this one.) */
+         primary base; we're not going to use that vtable anyhow.
+        We do still need to do this for virtual primary bases, as they
+        could become non-primary in a construction vtable.  */
       (!BINFO_PRIMARY_P (binfo) || TREE_VIA_VIRTUAL (binfo))
       /* Similarly, a base without a vtable needs no modification.  */
       && CLASSTYPE_VFIELDS (BINFO_TYPE (binfo)))
@@ -2783,9 +2563,6 @@ dfs_modify_vtables (binfo, data)
 
       t = (tree) data;
 
-      /* If we're supporting RTTI then we always need a new vtable to
-        point to the RTTI information.  Under the new ABI we may need
-        a new vtable to contain vcall and vbase offsets.  */
       make_new_vtable (t, binfo);
       
       /* Now, go through each of the virtual functions in the virtual
@@ -2809,22 +2586,21 @@ dfs_modify_vtables (binfo, data)
 
 /* Update all of the primary and secondary vtables for T.  Create new
    vtables as required, and initialize their RTTI information.  Each
-   of the functions in OVERRIDDEN_VIRTUALS overrides a virtual
-   function from a base class; find and modify the appropriate entries
-   to point to the overriding functions.  Returns a list, in
-   declaration order, of the functions that are overridden in this
-   class, but do not appear in the primary base class vtable, and
-   which should therefore be appended to the end of the vtable for T.  */
+   of the functions in VIRTUALS is declared in T and may override a
+   virtual function from a base class; find and modify the appropriate
+   entries to point to the overriding functions.  Returns a list, in
+   declaration order, of the virtual functions that are declared in T,
+   but do not appear in the primary base class vtable, and which
+   should therefore be appended to the end of the vtable for T.  */
 
 static tree
-modify_all_vtables (t, vfuns_p, overridden_virtuals)
+modify_all_vtables (t, vfuns_p, virtuals)
      tree t;
      int *vfuns_p;
-     tree overridden_virtuals;
+     tree virtuals;
 {
-  tree binfo;
-
-  binfo = TYPE_BINFO (t);
+  tree binfo = TYPE_BINFO (t);
+  tree *fnsp;
 
   /* Update all of the vtables.  */
   dfs_walk (binfo, 
@@ -2833,114 +2609,88 @@ modify_all_vtables (t, vfuns_p, overridden_virtuals)
            t);
   dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
 
-  /* If we should include overriding functions for secondary vtables
-     in our primary vtable, add them now.  */
-  if (all_overridden_vfuns_in_vtables_p ())
+  /* Add virtual functions not already in our primary vtable. These
+     will be both those introduced by this class, and those overridden
+     from secondary bases.  It does not include virtuals merely
+     inherited from secondary bases.  */
+  for (fnsp = &virtuals; *fnsp; )
     {
-      tree *fnsp = &overridden_virtuals;
+      tree fn = TREE_VALUE (*fnsp);
 
-      while (*fnsp)
+      if (!value_member (fn, BINFO_VIRTUALS (binfo))
+         || DECL_VINDEX (fn) == error_mark_node)
        {
-         tree fn = TREE_VALUE (*fnsp);
-
-         if (!BINFO_VIRTUALS (binfo)
-             || !value_member (fn, BINFO_VIRTUALS (binfo)))
-           {
-             /* Set the vtable index.  */
-             set_vindex (fn, vfuns_p);
-             /* We don't need to convert to a base class when calling
-                this function.  */
-             DECL_VIRTUAL_CONTEXT (fn) = t;
-
-             /* We don't need to adjust the `this' pointer when
-                calling this function.  */
-             BV_DELTA (*fnsp) = integer_zero_node;
-             BV_VCALL_INDEX (*fnsp) = NULL_TREE;
-
-             /* This is an overridden function not already in our
-                vtable.  Keep it.  */
-             fnsp = &TREE_CHAIN (*fnsp);
-           }
-         else
-           /* We've already got an entry for this function.  Skip
-              it.  */
-           *fnsp = TREE_CHAIN (*fnsp);
+         /* Set the vtable index.  */
+         set_vindex (fn, vfuns_p);
+         /* We don't need to convert to a base class when calling
+            this function.  */
+         DECL_VIRTUAL_CONTEXT (fn) = t;
+
+         /* We don't need to adjust the `this' pointer when
+            calling this function.  */
+         BV_DELTA (*fnsp) = integer_zero_node;
+         BV_VCALL_INDEX (*fnsp) = NULL_TREE;
+
+         /* This is a function not already in our vtable.  Keep it.  */
+         fnsp = &TREE_CHAIN (*fnsp);
        }
+      else
+       /* We've already got an entry for this function.  Skip it.  */
+       *fnsp = TREE_CHAIN (*fnsp);
     }
-  else
-    overridden_virtuals = NULL_TREE;
-
-  return overridden_virtuals;
+  
+  return virtuals;
 }
 
 /* Here, we already know that they match in every respect.
-   All we have to check is where they had their declarations.  */
+   All we have to check is where they had their declarations.
+
+   Return non-zero iff FNDECL1 is declared in a class which has a
+   proper base class containing FNDECL2.  We don't care about
+   ambiguity or accessibility.  */
 
 static int 
 strictly_overrides (fndecl1, fndecl2)
      tree fndecl1, fndecl2;
 {
-  int distance = get_base_distance (DECL_CONTEXT (fndecl2),
-                                   DECL_CONTEXT (fndecl1),
-                                   0, (tree *)0);
-  if (distance == -2 || distance > 0)
-    return 1;
-  return 0;
+  base_kind kind;
+  
+  return (lookup_base (DECL_CONTEXT (fndecl1), DECL_CONTEXT (fndecl2),
+                      ba_ignore | ba_quiet, &kind)
+         && kind != bk_same_type);
 }
 
-/* Get the base virtual function declarations in T that are either
-   overridden or hidden by FNDECL as a list.  We set TREE_PURPOSE with
-   the overrider/hider.  */
+/* Get the base virtual function declarations in T that have the
+   indicated NAME.  */
 
 static tree
-get_basefndecls (fndecl, t)
-     tree fndecl, t;
+get_basefndecls (name, t)
+     tree name, t;
 {
-  tree methods = TYPE_METHODS (t);
+  tree methods;
   tree base_fndecls = NULL_TREE;
-  tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
-  int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
-
-  while (methods)
-    {
-      if (TREE_CODE (methods) == FUNCTION_DECL
-         && DECL_VINDEX (methods) != NULL_TREE
-         && DECL_NAME (fndecl) == DECL_NAME (methods))
-       base_fndecls = tree_cons (fndecl, methods, base_fndecls);
+  int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
+  int i;
 
-      methods = TREE_CHAIN (methods);
-    }
+  for (methods = TYPE_METHODS (t); methods; methods = TREE_CHAIN (methods))
+    if (TREE_CODE (methods) == FUNCTION_DECL
+       && DECL_VINDEX (methods) != NULL_TREE
+       && DECL_NAME (methods) == name)
+      base_fndecls = tree_cons (NULL_TREE, methods, base_fndecls);
 
   if (base_fndecls)
     return base_fndecls;
 
   for (i = 0; i < n_baseclasses; i++)
     {
-      tree base_binfo = TREE_VEC_ELT (binfos, i);
-      tree basetype = BINFO_TYPE (base_binfo);
-
-      base_fndecls = chainon (get_basefndecls (fndecl, basetype),
+      tree basetype = TYPE_BINFO_BASETYPE (t, i);
+      base_fndecls = chainon (get_basefndecls (name, basetype),
                              base_fndecls);
     }
 
   return base_fndecls;
 }
 
-/* Mark the functions that have been hidden with their overriders.
-   Since we start out with all functions already marked with a hider,
-   no need to mark functions that are just hidden.
-
-   Subroutine of warn_hidden.  */
-
-static void
-mark_overriders (fndecl, base_fndecls)
-     tree fndecl, base_fndecls;
-{
-  for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
-    if (same_signature_p (fndecl, TREE_VALUE (base_fndecls)))
-      TREE_PURPOSE (base_fndecls) = fndecl;
-}
-
 /* If this declaration supersedes the declaration of
    a method declared virtual in the base class, then
    mark this field as being virtual as well.  */
@@ -2959,16 +2709,14 @@ check_for_override (decl, ctype)
        || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
       && look_for_overrides (ctype, decl)
       && !DECL_STATIC_FUNCTION_P (decl))
-    {
-      /* Set DECL_VINDEX to a value that is neither an
-        INTEGER_CST nor the error_mark_node so that
-        add_virtual_function will realize this is an
-        overriding function.  */
-      DECL_VINDEX (decl) = decl;
-    }
+    /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
+       the error_mark_node so that we know it is an overriding
+       function.  */
+    DECL_VINDEX (decl) = decl;
+
   if (DECL_VIRTUAL_P (decl))
     {
-      if (DECL_VINDEX (decl) == NULL_TREE)
+      if (!DECL_VINDEX (decl))
        DECL_VINDEX (decl) = error_mark_node;
       IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)) = 1;
     }
@@ -2988,57 +2736,59 @@ warn_hidden (t)
   /* We go through each separately named virtual function.  */
   for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
     {
-      tree fns = TREE_VEC_ELT (method_vec, i);
-      tree fndecl = NULL_TREE;
-
-      tree base_fndecls = NULL_TREE;
-      tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
-      int i, n_baseclasses = binfos ? TREE_VEC_LENGTH (binfos) : 0;
-
-      /* First see if we have any virtual functions in this batch.  */
-      for (; fns; fns = OVL_NEXT (fns))
+      tree fns;
+      tree name;
+      tree fndecl;
+      tree base_fndecls;
+      int j;
+
+      /* All functions in this slot in the CLASSTYPE_METHOD_VEC will
+        have the same name.  Figure out what name that is.  */
+      name = DECL_NAME (OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
+      /* There are no possibly hidden functions yet.  */
+      base_fndecls = NULL_TREE;
+      /* Iterate through all of the base classes looking for possibly
+        hidden functions.  */
+      for (j = 0; j < CLASSTYPE_N_BASECLASSES (t); j++)
        {
-         fndecl = OVL_CURRENT (fns);
-         if (DECL_VINDEX (fndecl))
-           break;
+         tree basetype = TYPE_BINFO_BASETYPE (t, j);
+         base_fndecls = chainon (get_basefndecls (name, basetype),
+                                 base_fndecls);
        }
 
-      if (fns == NULL_TREE)
+      /* If there are no functions to hide, continue. */
+      if (!base_fndecls)
        continue;
 
-      /* First we get a list of all possible functions that might be
-        hidden from each base class.  */
-      for (i = 0; i < n_baseclasses; i++)
+      /* Remove any overridden functions. */
+      for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
        {
-         tree base_binfo = TREE_VEC_ELT (binfos, i);
-         tree basetype = BINFO_TYPE (base_binfo);
-
-         base_fndecls = chainon (get_basefndecls (fndecl, basetype),
-                                 base_fndecls);
+         fndecl = OVL_CURRENT (fns);
+         if (DECL_VINDEX (fndecl))
+           {
+             tree *prev = &base_fndecls;
+             
+             while (*prev) 
+               /* If the method from the base class has the same
+                  signature as the method from the derived class, it
+                  has been overridden.  */
+               if (same_signature_p (fndecl, TREE_VALUE (*prev)))
+                 *prev = TREE_CHAIN (*prev);
+               else
+                 prev = &TREE_CHAIN (*prev);
+           }
        }
 
-      fns = OVL_NEXT (fns);
-
-      /* ...then mark up all the base functions with overriders, preferring
-        overriders to hiders.  */
-      if (base_fndecls)
-       for (; fns; fns = OVL_NEXT (fns))
-         {
-           fndecl = OVL_CURRENT (fns);
-           if (DECL_VINDEX (fndecl))
-             mark_overriders (fndecl, base_fndecls);
-         }
-
       /* Now give a warning for all base functions without overriders,
         as they are hidden.  */
-      for (; base_fndecls; base_fndecls = TREE_CHAIN (base_fndecls))
-       if (!same_signature_p (TREE_PURPOSE (base_fndecls),
-                              TREE_VALUE (base_fndecls)))
-         {
-           /* Here we know it is a hider, and no overrider exists.  */
-           cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
-           cp_warning_at ("  by `%D'", TREE_PURPOSE (base_fndecls));
-         }
+      while (base_fndecls) 
+       {
+         /* Here we know it is a hider, and no overrider exists.  */
+         cp_warning_at ("`%D' was hidden", TREE_VALUE (base_fndecls));
+         cp_warning_at ("  by `%D'", 
+                        OVL_CURRENT (TREE_VEC_ELT (method_vec, i)));
+         base_fndecls = TREE_CHAIN (base_fndecls);
+       }
     }
 }
 
@@ -3064,7 +2814,16 @@ finish_struct_anon (t)
          tree elt = TYPE_FIELDS (TREE_TYPE (field));
          for (; elt; elt = TREE_CHAIN (elt))
            {
-             if (DECL_ARTIFICIAL (elt))
+             /* We're generally only interested in entities the user
+                declared, but we also find nested classes by noticing
+                the TYPE_DECL that we create implicitly.  You're
+                allowed to put one anonymous union inside another,
+                though, so we explicitly tolerate that.  We use
+                TYPE_ANONYMOUS_P rather than ANON_AGGR_TYPE_P so that
+                we also allow unnamed types used for defining fields.  */
+             if (DECL_ARTIFICIAL (elt) 
+                 && (!DECL_IMPLICIT_TYPEDEF_P (elt)
+                     || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
                continue;
 
              if (DECL_NAME (elt) == constructor_name (t))
@@ -3115,6 +2874,8 @@ add_implicitly_declared_members (t, cant_have_default_ctor,
   tree virtual_dtor = NULL_TREE;
   tree *f;
 
+  ++adding_implicit_members;
+
   /* Destructor.  */
   if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
     {
@@ -3174,6 +2935,8 @@ add_implicitly_declared_members (t, cant_have_default_ctor,
   *f = TYPE_METHODS (t);
   TYPE_METHODS (t) = implicit_fns;
 
+  --adding_implicit_members;
+
   return virtual_dtor;
 }
 
@@ -3289,7 +3052,8 @@ check_bitfield_decl (field)
       DECL_SIZE (field) = convert (bitsizetype, w);
       DECL_BIT_FIELD (field) = 1;
 
-      if (integer_zerop (w))
+      if (integer_zerop (w)
+         && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
        {
 #ifdef EMPTY_FIELD_BOUNDARY
          DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
@@ -3467,8 +3231,6 @@ check_field_decls (t, access_decls, empty_p,
       tree x = *field;
       tree type = TREE_TYPE (x);
 
-      GNU_xref_member (current_class_name, x);
-
       next = &TREE_CHAIN (x);
 
       if (TREE_CODE (x) == FIELD_DECL)
@@ -3592,6 +3354,9 @@ check_field_decls (t, access_decls, empty_p,
            to be allowed in POD structs.  */
        CLASSTYPE_NON_POD_P (t) = 1;
 
+      if (! zero_init_p (type))
+       CLASSTYPE_NON_ZERO_INIT_P (t) = 1;
+
       /* If any field is const, the structure type is pseudo-const.  */
       if (CP_TYPE_CONST_P (type))
        {
@@ -3640,16 +3405,16 @@ check_field_decls (t, access_decls, empty_p,
   if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
       && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
     {
-      cp_warning ("`%#T' has pointer data members", t);
+      warning ("`%#T' has pointer data members", t);
       
       if (! TYPE_HAS_INIT_REF (t))
        {
-         cp_warning ("  but does not override `%T(const %T&)'", t, t);
+         warning ("  but does not override `%T(const %T&)'", t, t);
          if (! TYPE_HAS_ASSIGN_REF (t))
-           cp_warning ("  or `operator=(const %T&)'", t);
+           warning ("  or `operator=(const %T&)'", t);
        }
       else if (! TYPE_HAS_ASSIGN_REF (t))
-       cp_warning ("  but does not override `operator=(const %T&)'", t);
+       warning ("  but does not override `operator=(const %T&)'", t);
     }
 
 
@@ -3661,43 +3426,6 @@ check_field_decls (t, access_decls, empty_p,
   *access_decls = nreverse (*access_decls);
 }
 
-/* Return a FIELD_DECL for a pointer-to-virtual-table or
-   pointer-to-virtual-base.  The NAME, ASSEMBLER_NAME, and TYPE of the
-   field are as indicated.  The CLASS_TYPE in which this field occurs
-   is also indicated.  FCONTEXT is the type that is needed for the debug
-   info output routines.  *EMPTY_P is set to a non-zero value by this
-   function to indicate that a class containing this field is
-   non-empty.  */
-
-static tree
-build_vtbl_or_vbase_field (name, assembler_name, type, class_type, fcontext,
-                          empty_p)
-     tree name;
-     tree assembler_name;
-     tree type;
-     tree class_type;
-     tree fcontext;
-     int *empty_p;
-{
-  tree field;
-
-  /* This class is non-empty.  */
-  *empty_p = 0;
-
-  /* Build the FIELD_DECL.  */
-  field = build_decl (FIELD_DECL, name, type);
-  SET_DECL_ASSEMBLER_NAME (field, assembler_name);
-  DECL_VIRTUAL_P (field) = 1;
-  DECL_ARTIFICIAL (field) = 1;
-  DECL_FIELD_CONTEXT (field) = class_type;
-  DECL_FCONTEXT (field) = fcontext;
-  DECL_ALIGN (field) = TYPE_ALIGN (type);
-  DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (type);
-
-  /* Return it.  */
-  return field;
-}
-
 /* If TYPE is an empty class type, records its OFFSET in the table of
    OFFSETS.  */
 
@@ -3756,20 +3484,30 @@ check_subobject_offset (type, offset, offsets)
 /* Walk through all the subobjects of TYPE (located at OFFSET).  Call
    F for every subobject, passing it the type, offset, and table of
    OFFSETS.  If VBASES_P is non-zero, then even virtual non-primary
-   bases should be traversed; otherwise, they are ignored.  If F
-   returns a non-zero value, the traversal ceases, and that value is
-   returned.  Otherwise, returns zero.  */
+   bases should be traversed; otherwise, they are ignored.  
+
+   If MAX_OFFSET is non-NULL, then subobjects with an offset greater
+   than MAX_OFFSET will not be walked.
+
+   If F returns a non-zero value, the traversal ceases, and that value
+   is returned.  Otherwise, returns zero.  */
 
 static int
-walk_subobject_offsets (type, f, offset, offsets, vbases_p)
+walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
      tree type;
      subobject_offset_fn f;
      tree offset;
      splay_tree offsets;
+     tree max_offset;
      int vbases_p;
 {
   int r = 0;
 
+  /* If this OFFSET is bigger than the MAX_OFFSET, then we should
+     stop.  */
+  if (max_offset && INT_CST_LT (max_offset, offset))
+    return 0;
+
   if (CLASS_TYPE_P (type))
     {
       tree field;
@@ -3796,6 +3534,7 @@ walk_subobject_offsets (type, f, offset, offsets, vbases_p)
                                                  offset,
                                                  BINFO_OFFSET (binfo)),
                                      offsets,
+                                     max_offset,
                                      vbases_p);
          if (r)
            return r;
@@ -3811,6 +3550,7 @@ walk_subobject_offsets (type, f, offset, offsets, vbases_p)
                                                    offset,
                                                    DECL_FIELD_OFFSET (field)),
                                        offsets,
+                                       max_offset,
                                        /*vbases_p=*/1);
            if (r)
              return r;
@@ -3830,11 +3570,17 @@ walk_subobject_offsets (type, f, offset, offsets, vbases_p)
                                      f,
                                      offset,
                                      offsets,
+                                     max_offset,
                                      /*vbases_p=*/1);
          if (r)
            return r;
          offset = size_binop (PLUS_EXPR, offset, 
                               TYPE_SIZE_UNIT (TREE_TYPE (type)));
+         /* If this new OFFSET is bigger than the MAX_OFFSET, then
+            there's no point in iterating through the remaining
+            elements of the array.  */
+         if (max_offset && INT_CST_LT (max_offset, offset))
+           break;
        }
     }
 
@@ -3853,7 +3599,7 @@ record_subobject_offsets (type, offset, offsets, vbases_p)
      int vbases_p;
 {
   walk_subobject_offsets (type, record_subobject_offset, offset,
-                         offsets, vbases_p);
+                         offsets, /*max_offset=*/NULL_TREE, vbases_p);
 }
 
 /* Returns non-zero if any of the empty subobjects of TYPE (located at
@@ -3867,22 +3613,34 @@ layout_conflict_p (type, offset, offsets, vbases_p)
      splay_tree offsets;
      int vbases_p;
 {
+  splay_tree_node max_node;
+
+  /* Get the node in OFFSETS that indicates the maximum offset where
+     an empty subobject is located.  */
+  max_node = splay_tree_max (offsets);
+  /* If there aren't any empty subobjects, then there's no point in
+     performing this check.  */
+  if (!max_node)
+    return 0;
+
   return walk_subobject_offsets (type, check_subobject_offset, offset,
-                                offsets, vbases_p);
+                                offsets, (tree) (max_node->key),
+                                vbases_p);
 }
 
 /* DECL is a FIELD_DECL corresponding either to a base subobject of a
    non-static data member of the type indicated by RLI.  BINFO is the
    binfo corresponding to the base subobject, OFFSETS maps offsets to
-   types already located at those offsets.  This function determines
-   the position of the DECL.  */
+   types already located at those offsets.  T is the most derived
+   type.  This function determines the position of the DECL.  */
 
 static void
-layout_nonempty_base_or_field (rli, decl, binfo, offsets)
+layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
      record_layout_info rli;
      tree decl;
      tree binfo;
      splay_tree offsets;
+     tree t;
 {
   tree offset = NULL_TREE;
   tree type = TREE_TYPE (decl);
@@ -3939,26 +3697,29 @@ layout_nonempty_base_or_field (rli, decl, binfo, offsets)
        break;
     }
 
-  /* Now that we know where it wil be placed, update its
+  /* Now that we know where it will be placed, update its
      BINFO_OFFSET.  */
   if (binfo && CLASS_TYPE_P (BINFO_TYPE (binfo)))
     propagate_binfo_offsets (binfo, 
-                            convert (ssizetype, offset));
+                            convert (ssizetype, offset), t);
 }
 
 /* Layout the empty base BINFO.  EOC indicates the byte currently just
    past the end of the class, and should be correctly aligned for a
    class of the type indicated by BINFO; OFFSETS gives the offsets of
-   the empty bases allocated so far.  */
+   the empty bases allocated so far. T is the most derived
+   type.  Return non-zero iff we added it at the end. */
 
-static void
-layout_empty_base (binfo, eoc, offsets)
+static bool
+layout_empty_base (binfo, eoc, offsets, t)
      tree binfo;
      tree eoc;
      splay_tree offsets;
+     tree t;
 {
   tree alignment;
   tree basetype = BINFO_TYPE (binfo);
+  bool atend = false;
   
   /* This routine should only be used for empty classes.  */
   my_friendly_assert (is_empty_class (basetype), 20000321);
@@ -3973,7 +3734,8 @@ layout_empty_base (binfo, eoc, offsets)
     {
       /* That didn't work.  Now, we move forward from the next
         available spot in the class.  */
-      propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
+      atend = true;
+      propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
       while (1) 
        {
          if (!layout_conflict_p (BINFO_TYPE (binfo),
@@ -3984,30 +3746,35 @@ layout_empty_base (binfo, eoc, offsets)
            break;
 
          /* There's overlap here, too.  Bump along to the next spot.  */
-         propagate_binfo_offsets (binfo, alignment);
+         propagate_binfo_offsets (binfo, alignment, t);
        }
     }
+  return atend;
 }
 
 /* Build a FIELD_DECL for the base given by BINFO in the class
    indicated by RLI.  If the new object is non-empty, clear *EMPTY_P.
    *BASE_ALIGN is a running maximum of the alignments of any base
-   class.  OFFSETS gives the location of empty base subobjects.  */
+   class.  OFFSETS gives the location of empty base subobjects.  T is
+   the most derived type.  Return non-zero if the new object cannot be
+   nearly-empty. */
 
-static void
-build_base_field (rli, binfo, empty_p, offsets)
+static bool
+build_base_field (rli, binfo, empty_p, offsets, t)
      record_layout_info rli;
      tree binfo;
      int *empty_p;
      splay_tree offsets;
+     tree t;
 {
   tree basetype = BINFO_TYPE (binfo);
   tree decl;
+  bool atend = false;
 
   if (!COMPLETE_TYPE_P (basetype))
     /* This error is now reported in xref_tag, thus giving better
        location information.  */
-    return;
+    return atend;
   
   decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
   DECL_ARTIFICIAL (decl) = 1;
@@ -4016,6 +3783,8 @@ build_base_field (rli, binfo, empty_p, offsets)
   DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
   DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
   DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
+  /* Tell the backend not to round up to TYPE_ALIGN.  */
+  DECL_PACKED (decl) = 1;
   
   if (!integer_zerop (DECL_SIZE (decl)))
     {
@@ -4026,7 +3795,7 @@ build_base_field (rli, binfo, empty_p, offsets)
       /* Try to place the field.  It may take more than one try if we
         have a hard time placing the field without putting two
         objects of the same type at the same address.  */
-      layout_nonempty_base_or_field (rli, decl, binfo, offsets);
+      layout_nonempty_base_or_field (rli, decl, binfo, offsets, t);
     }
   else
     {
@@ -4036,43 +3805,39 @@ build_base_field (rli, binfo, empty_p, offsets)
         byte-aligned.  */
       eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
       eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
-      layout_empty_base (binfo, size_int (eoc), offsets);
+      atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
     }
 
-  /* Check for inaccessible base classes.  If the same base class
-     appears more than once in the hierarchy, but isn't virtual, then
-     it's ambiguous.  */
-  if (get_base_distance (basetype, rli->t, 0, NULL) == -2)
-    cp_warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
-               basetype, rli->t);
-  
   /* Record the offsets of BINFO and its base subobjects.  */
   record_subobject_offsets (BINFO_TYPE (binfo), 
                            BINFO_OFFSET (binfo),
                            offsets, 
                            /*vbases_p=*/0);
+  return atend;
 }
 
 /* Layout all of the non-virtual base classes.  Record empty
-   subobjects in OFFSETS.  */
+   subobjects in OFFSETS.  T is the most derived type.  Return
+   non-zero if the type cannot be nearly empty.  */
 
-static void
-build_base_fields (rli, empty_p, offsets)
+static bool
+build_base_fields (rli, empty_p, offsets, t)
      record_layout_info rli;
      int *empty_p;
      splay_tree offsets;
+     tree t;
 {
   /* Chain to hold all the new FIELD_DECLs which stand in for base class
      subobjects.  */
   tree rec = rli->t;
   int n_baseclasses = CLASSTYPE_N_BASECLASSES (rec);
   int i;
+  bool atend = 0;
 
-  /* Under the new ABI, the primary base class is always allocated
-     first.  */
+  /* The primary base class is always allocated first.  */
   if (CLASSTYPE_HAS_PRIMARY_BASE_P (rec))
     build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (rec), 
-                     empty_p, offsets);
+                     empty_p, offsets, t);
 
   /* Now allocate the rest of the bases.  */
   for (i = 0; i < n_baseclasses; ++i)
@@ -4081,8 +3846,8 @@ build_base_fields (rli, empty_p, offsets)
 
       base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
 
-      /* Under the new ABI, the primary base was already allocated
-        above, so we don't need to allocate it again here.  */
+      /* The primary base was already allocated above, so we don't
+        need to allocate it again here.  */
       if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
        continue;
 
@@ -4093,8 +3858,9 @@ build_base_fields (rli, empty_p, offsets)
          && !BINFO_PRIMARY_P (base_binfo))
        continue;
 
-      build_base_field (rli, base_binfo, empty_p, offsets);
+      atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
     }
+  return atend;
 }
 
 /* Go through the TYPE_METHODS of T issuing any appropriate
@@ -4106,12 +3872,9 @@ check_methods (t)
      tree t;
 {
   tree x;
-  int seen_one_arg_array_delete_p = 0;
 
   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
     {
-      GNU_xref_member (current_class_name, x);
-
       /* If this was an evil function, don't keep it in class.  */
       if (DECL_ASSEMBLER_NAME_SET_P (x) 
          && IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (x)))
@@ -4130,32 +3893,6 @@ check_methods (t)
            CLASSTYPE_PURE_VIRTUALS (t)
              = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
        }
-
-      if (DECL_ARRAY_DELETE_OPERATOR_P (x))
-       {
-         tree second_parm;
-
-         /* When dynamically allocating an array of this type, we
-            need a "cookie" to record how many elements we allocated,
-            even if the array elements have no non-trivial
-            destructor, if the usual array deallocation function
-            takes a second argument of type size_t.  The standard (in
-            [class.free]) requires that the second argument be set
-            correctly.  */
-         second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (x)));
-         /* Under the new ABI, we choose only those function that are
-            explicitly declared as `operator delete[] (void *,
-            size_t)'.  */
-         if (!seen_one_arg_array_delete_p
-             && second_parm
-             && TREE_CHAIN (second_parm) == void_list_node
-             && same_type_p (TREE_VALUE (second_parm), sizetype))
-           TYPE_VEC_DELETE_TAKES_SIZE (t) = 1;
-         /* If there's no second parameter, then this is the usual
-            deallocation function.  */
-         else if (second_parm == void_list_node)
-           seen_one_arg_array_delete_p = 1;
-       }
     }
 }
 
@@ -4178,6 +3915,7 @@ build_clone (fn, name)
   DECL_ABSTRACT_ORIGIN (clone) = fn;
   /* Reset the function name.  */
   DECL_NAME (clone) = name;
+  SET_DECL_ASSEMBLER_NAME (clone, NULL_TREE);
   /* There's no pending inline data for this function.  */
   DECL_PENDING_INLINE_INFO (clone) = NULL;
   DECL_PENDING_INLINE_P (clone) = 0;
@@ -4250,7 +3988,7 @@ build_clone (fn, name)
       for (parms = DECL_ARGUMENTS (clone); parms; parms = TREE_CHAIN (parms))
        {
          DECL_CONTEXT (parms) = clone;
-         copy_lang_decl (parms);
+         cxx_dup_lang_specific_decl (parms);
        }
     }
 
@@ -4337,6 +4075,80 @@ clone_function_decl (fn, update_method_vec_p)
   DECL_ABSTRACT (fn) = 1;
 }
 
+/* DECL is an in charge constructor, which is being defined. This will
+   have had an in class declaration, from whence clones were
+   declared. An out-of-class definition can specify additional default
+   arguments. As it is the clones that are involved in overload
+   resolution, we must propagate the information from the DECL to its
+   clones. */
+
+void
+adjust_clone_args (decl)
+     tree decl;
+{
+  tree clone;
+  
+  for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
+       clone = TREE_CHAIN (clone))
+    {
+      tree orig_clone_parms = TYPE_ARG_TYPES (TREE_TYPE (clone));
+      tree orig_decl_parms = TYPE_ARG_TYPES (TREE_TYPE (decl));
+      tree decl_parms, clone_parms;
+
+      clone_parms = orig_clone_parms;
+      
+      /* Skip the 'this' parameter. */
+      orig_clone_parms = TREE_CHAIN (orig_clone_parms);
+      orig_decl_parms = TREE_CHAIN (orig_decl_parms);
+
+      if (DECL_HAS_IN_CHARGE_PARM_P (decl))
+       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
+      if (DECL_HAS_VTT_PARM_P (decl))
+       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
+      
+      clone_parms = orig_clone_parms;
+      if (DECL_HAS_VTT_PARM_P (clone))
+       clone_parms = TREE_CHAIN (clone_parms);
+      
+      for (decl_parms = orig_decl_parms; decl_parms;
+          decl_parms = TREE_CHAIN (decl_parms),
+            clone_parms = TREE_CHAIN (clone_parms))
+       {
+         my_friendly_assert (same_type_p (TREE_TYPE (decl_parms),
+                                          TREE_TYPE (clone_parms)), 20010424);
+         
+         if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
+           {
+             /* A default parameter has been added. Adjust the
+                clone's parameters. */
+             tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
+             tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
+             tree type;
+
+             clone_parms = orig_decl_parms;
+
+             if (DECL_HAS_VTT_PARM_P (clone))
+               {
+                 clone_parms = tree_cons (TREE_PURPOSE (orig_clone_parms),
+                                          TREE_VALUE (orig_clone_parms),
+                                          clone_parms);
+                 TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
+               }
+             type = build_cplus_method_type (basetype,
+                                             TREE_TYPE (TREE_TYPE (clone)),
+                                             clone_parms);
+             if (exceptions)
+               type = build_exception_variant (type, exceptions);
+             TREE_TYPE (clone) = type;
+             
+             clone_parms = NULL_TREE;
+             break;
+           }
+       }
+      my_friendly_assert (!clone_parms, 20010424);
+    }
+}
+
 /* For each of the constructors and destructors in T, create an
    in-charge and not-in-charge variant.  */
 
@@ -4377,6 +4189,59 @@ remove_zero_width_bit_fields (t)
     }
 }
 
+/* Returns TRUE iff we need a cookie when dynamically allocating an
+   array whose elements have the indicated class TYPE.  */
+
+static bool
+type_requires_array_cookie (type)
+     tree type;
+{
+  tree fns;
+  bool has_two_argument_delete_p = false;
+
+  my_friendly_assert (CLASS_TYPE_P (type), 20010712);
+
+  /* If there's a non-trivial destructor, we need a cookie.  In order
+     to iterate through the array calling the destructor for each
+     element, we'll have to know how many elements there are.  */
+  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
+    return true;
+
+  /* If the usual deallocation function is a two-argument whose second
+     argument is of type `size_t', then we have to pass the size of
+     the array to the deallocation function, so we will need to store
+     a cookie.  */
+  fns = lookup_fnfields (TYPE_BINFO (type), 
+                        ansi_opname (VEC_DELETE_EXPR),
+                        /*protect=*/0);
+  /* If there are no `operator []' members, or the lookup is
+     ambiguous, then we don't need a cookie.  */
+  if (!fns || fns == error_mark_node)
+    return false;
+  /* Loop through all of the functions.  */
+  for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
+    {
+      tree fn;
+      tree second_parm;
+
+      /* Select the current function.  */
+      fn = OVL_CURRENT (fns);
+      /* See if this function is a one-argument delete function.  If
+        it is, then it will be the usual deallocation function.  */
+      second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
+      if (second_parm == void_list_node)
+       return false;
+      /* Otherwise, if we have a two-argument function and the second
+        argument is `size_t', it will be the usual deallocation
+        function -- unless there is one-argument function, too.  */
+      if (TREE_CHAIN (second_parm) == void_list_node
+         && same_type_p (TREE_VALUE (second_parm), sizetype))
+       has_two_argument_delete_p = true;
+    }
+
+  return has_two_argument_delete_p;
+}
+
 /* Check the validity of the bases and members declared in T.  Add any
    implicitly-generated functions (like copy-constructors and
    assignment operators).  Compute various flag bits (like
@@ -4444,7 +4309,7 @@ check_bases_and_members (t, empty_p)
        || TYPE_HAS_ASSIGN_REF (t));
   TYPE_HAS_REAL_ASSIGN_REF (t) |= TYPE_HAS_ASSIGN_REF (t);
   TYPE_HAS_COMPLEX_ASSIGN_REF (t)
-    |= TYPE_HAS_ASSIGN_REF (t) || TYPE_USES_VIRTUAL_BASECLASSES (t);
+    |= TYPE_HAS_ASSIGN_REF (t) || TYPE_CONTAINS_VPTR_P (t);
 
   /* Synthesize any needed methods.  Note that methods will be synthesized
      for anonymous unions; grok_x_components undoes that.  */
@@ -4462,39 +4327,47 @@ check_bases_and_members (t, empty_p)
 
   /* Build and sort the CLASSTYPE_METHOD_VEC.  */
   finish_struct_methods (t);
+
+  /* Figure out whether or not we will need a cookie when dynamically
+     allocating an array of this type.  */
+  TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
+    = type_requires_array_cookie (t);
 }
 
 /* If T needs a pointer to its virtual function table, set TYPE_VFIELD
    accordingly.  If a new vfield was created (because T doesn't have a
    primary base class), then the newly created field is returned.  It
    is not added to the TYPE_FIELDS list; it is the caller's
-   responsibility to do that.  */
+   responsibility to do that.  Accumulate declared virtual functions
+   on VIRTUALS_P.  */
 
 static tree
-create_vtable_ptr (t, empty_p, vfuns_p,
-                  new_virtuals_p, overridden_virtuals_p)
+create_vtable_ptr (t, empty_p, virtuals_p)
      tree t;
      int *empty_p;
-     int *vfuns_p;
-     tree *new_virtuals_p;
-     tree *overridden_virtuals_p;
+     tree *virtuals_p;
 {
   tree fn;
 
-  /* Loop over the virtual functions, adding them to our various
-     vtables.  */
+  /* Collect the virtual functions declared in T.  */
   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
-    if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn))
-      add_virtual_function (new_virtuals_p, overridden_virtuals_p,
-                           vfuns_p, fn, t);
+    if (DECL_VINDEX (fn) && !DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn)
+       && TREE_CODE (DECL_VINDEX (fn)) != INTEGER_CST)
+      {
+       tree new_virtual = make_node (TREE_LIST);
+       
+       BV_FN (new_virtual) = fn;
+       BV_DELTA (new_virtual) = integer_zero_node;
 
+       TREE_CHAIN (new_virtual) = *virtuals_p;
+       *virtuals_p = new_virtual;
+      }
+  
   /* If we couldn't find an appropriate base class, create a new field
      here.  Even if there weren't any new virtual functions, we might need a
      new virtual function table if we're supposed to include vptrs in
      all classes that need them.  */
-  if (!TYPE_VFIELD (t)
-      && (*vfuns_p 
-         || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ())))
+  if (!TYPE_VFIELD (t) && (*virtuals_p || TYPE_CONTAINS_VPTR_P (t)))
     {
       /* We build this decl with vtbl_ptr_type_node, which is a
         `vtable_entry_type*'.  It might seem more precise to use
@@ -4515,13 +4388,21 @@ create_vtable_ptr (t, empty_p, vfuns_p,
         bounds.  That's better than using `void*' or some such; it's
         cleaner, and it let's the alias analysis code know that these
         stores cannot alias stores to void*!  */
-      TYPE_VFIELD (t) 
-       = build_vtbl_or_vbase_field (get_vfield_name (t),
-                                    get_identifier (VFIELD_BASE),
-                                    vtbl_ptr_type_node,
-                                    t,
-                                    t,
-                                    empty_p);
+      tree field;
+
+      field = build_decl (FIELD_DECL, get_vfield_name (t), vtbl_ptr_type_node);
+      SET_DECL_ASSEMBLER_NAME (field, get_identifier (VFIELD_BASE));
+      DECL_VIRTUAL_P (field) = 1;
+      DECL_ARTIFICIAL (field) = 1;
+      DECL_FIELD_CONTEXT (field) = t;
+      DECL_FCONTEXT (field) = t;
+      DECL_ALIGN (field) = TYPE_ALIGN (vtbl_ptr_type_node);
+      DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (vtbl_ptr_type_node);
+      
+      TYPE_VFIELD (t) = field;
+      
+      /* This class is non-empty.  */
+      *empty_p = 0;
 
       if (CLASSTYPE_N_BASECLASSES (t))
        /* If there were any baseclasses, they can't possibly be at
@@ -4530,7 +4411,7 @@ create_vtable_ptr (t, empty_p, vfuns_p,
           take work.  */
        TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
 
-      return TYPE_VFIELD (t);
+      return field;
     }
 
   return NULL_TREE;
@@ -4591,9 +4472,10 @@ fixup_inline_methods (type)
    OFFSET, which is a type offset, is number of bytes.  */
 
 static void
-propagate_binfo_offsets (binfo, offset)
+propagate_binfo_offsets (binfo, offset, t)
      tree binfo;
      tree offset;
+     tree t;
 {
   int i;
   tree primary_binfo;
@@ -4614,9 +4496,9 @@ propagate_binfo_offsets (binfo, offset)
     {
       tree base_binfo;
 
-      /* On the first through the loop, do the primary base.  Because
-        the primary base need not be an immediate base, we must
-        handle the primary base specially.  */
+      /* On the first time through the loop, do the primary base.
+        Because the primary base need not be an immediate base, we
+        must handle the primary base specially.  */
       if (i == -1) 
        {
          if (!primary_binfo) 
@@ -4632,12 +4514,13 @@ propagate_binfo_offsets (binfo, offset)
            continue;
        }
 
-      /* Skip virtual bases that aren't our primary base.  */
+      /* Skip virtual bases that aren't our canonical primary base.  */
       if (TREE_VIA_VIRTUAL (base_binfo)
-         && BINFO_PRIMARY_BASE_OF (base_binfo) != binfo)
+         && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
+             || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
        continue;
 
-      propagate_binfo_offsets (base_binfo, offset);
+      propagate_binfo_offsets (base_binfo, offset, t);
     }
 }
 
@@ -4651,15 +4534,18 @@ dfs_set_offset_for_unshared_vbases (binfo, data)
   /* If this is a virtual base, make sure it has the same offset as
      the shared copy.  If it's a primary base, then we know it's
      correct.  */
-  if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_P (binfo))
+  if (TREE_VIA_VIRTUAL (binfo))
     {
       tree t = (tree) data;
       tree vbase;
       tree offset;
       
       vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
-      offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
-      propagate_binfo_offsets (binfo, offset);
+      if (vbase != binfo)
+       {
+         offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
+         propagate_binfo_offsets (binfo, offset, t);
+       }
     }
 
   return NULL_TREE;
@@ -4674,8 +4560,7 @@ layout_virtual_bases (t, offsets)
      tree t;
      splay_tree offsets;
 {
-  tree vbases;
-  unsigned HOST_WIDE_INT dsize;
+  tree vbases, dsize;
   unsigned HOST_WIDE_INT eoc;
 
   if (CLASSTYPE_N_BASECLASSES (t) == 0)
@@ -4688,16 +4573,14 @@ layout_virtual_bases (t, offsets)
 #endif
 
   /* DSIZE is the size of the class without the virtual bases.  */
-  dsize = tree_low_cst (TYPE_SIZE (t), 1);
+  dsize = TYPE_SIZE (t);
 
   /* Make every class have alignment of at least one.  */
   TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
 
   /* Go through the virtual bases, allocating space for each virtual
-     base that is not already a primary base class.  Under the new
-     ABI, these are allocated according to a depth-first left-to-right
-     postorder traversal; in the new ABI, inheritance graph order is
-     used instead.  */
+     base that is not already a primary base class.  These are
+     allocated in inheritance graph order.  */
   for (vbases = TYPE_BINFO (t);
        vbases; 
        vbases = TREE_CHAIN (vbases))
@@ -4712,7 +4595,7 @@ layout_virtual_bases (t, offsets)
        {
          /* This virtual base is not a primary base of any class in the
             hierarchy, so we have to add space for it.  */
-         tree basetype;
+         tree basetype, usize;
          unsigned int desired_align;
 
          basetype = BINFO_TYPE (vbase);
@@ -4722,30 +4605,33 @@ layout_virtual_bases (t, offsets)
 
          /* Add padding so that we can put the virtual base class at an
             appropriately aligned offset.  */
-         dsize = CEIL (dsize, desired_align) * desired_align;
+         dsize = round_up (dsize, desired_align);
 
-         /* Under the new ABI, we try to squish empty virtual bases in
-            just like ordinary empty bases.  */
+         usize = size_binop (CEIL_DIV_EXPR, dsize, bitsize_unit_node);
+
+         /* We try to squish empty virtual bases in just like
+            ordinary empty bases.  */
          if (is_empty_class (basetype))
            layout_empty_base (vbase,
-                              size_int (CEIL (dsize, BITS_PER_UNIT)),
-                              offsets);
+                              convert (sizetype, usize),
+                              offsets, t);
          else
            {
              tree offset;
 
-             offset = ssize_int (CEIL (dsize, BITS_PER_UNIT));
+             offset = convert (ssizetype, usize);
              offset = size_diffop (offset, 
                                    convert (ssizetype, 
                                             BINFO_OFFSET (vbase)));
 
              /* And compute the offset of the virtual base.  */
-             propagate_binfo_offsets (vbase, offset);
+             propagate_binfo_offsets (vbase, offset, t);
              /* Every virtual baseclass takes a least a UNIT, so that
                 we can take it's address and get something different
                 for each base.  */
-             dsize += MAX (BITS_PER_UNIT,
-                           tree_low_cst (CLASSTYPE_SIZE (basetype), 0));
+             dsize = size_binop (PLUS_EXPR, dsize,
+                                 size_binop (MAX_EXPR, bitsize_unit_node,
+                                             CLASSTYPE_SIZE (basetype)));
            }
 
          /* Keep track of the offsets assigned to this virtual base.  */
@@ -4759,7 +4645,7 @@ layout_virtual_bases (t, offsets)
   /* Now, go through the TYPE_BINFO hierarchy, setting the
      BINFO_OFFSETs correctly for all non-primary copies of the virtual
      bases and their direct and indirect bases.  The ambiguity checks
-     in get_base_distance depend on the BINFO_OFFSETs being set
+     in lookup_base depend on the BINFO_OFFSETs being set
      correctly.  */
   dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
 
@@ -4767,13 +4653,12 @@ layout_virtual_bases (t, offsets)
      class, we didn't update DSIZE above; we were hoping to overlay
      multiple such bases at the same location.  */
   eoc = end_of_class (t, /*include_virtuals_p=*/1);
-  if (eoc * BITS_PER_UNIT > dsize)
-    dsize = (eoc + 1) * BITS_PER_UNIT;
+  dsize = size_binop (MAX_EXPR, dsize, bitsize_int (eoc * BITS_PER_UNIT));
 
   /* Now, make sure that the total size of the type is a multiple of
      its alignment.  */
-  dsize = CEIL (dsize, TYPE_ALIGN (t)) * TYPE_ALIGN (t);
-  TYPE_SIZE (t) = bitsize_int (dsize);
+  dsize = round_up (dsize, TYPE_ALIGN (t));
+  TYPE_SIZE (t) = dsize;
   TYPE_SIZE_UNIT (t) = convert (sizetype,
                                size_binop (CEIL_DIV_EXPR, TYPE_SIZE (t),
                                            bitsize_unit_node));
@@ -4785,8 +4670,9 @@ layout_virtual_bases (t, offsets)
         vbases = TREE_CHAIN (vbases))
       {
        tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
-       if (get_base_distance (basetype, t, 0, (tree*)0) == -2)
-         cp_warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
+       
+       if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
+         warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
                      basetype, t);
       }
 }
@@ -4807,6 +4693,7 @@ end_of_class (t, include_virtuals_p)
     {
       tree base_binfo;
       tree offset;
+      tree size;
       unsigned HOST_WIDE_INT end_of_base;
 
       base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
@@ -4816,9 +4703,16 @@ end_of_class (t, include_virtuals_p)
          && !BINFO_PRIMARY_P (base_binfo))
        continue;
 
+      if (is_empty_class (BINFO_TYPE (base_binfo)))
+       /* An empty class has zero CLASSTYPE_SIZE_UNIT, but we need to
+          allocate some space for it. It cannot have virtual bases,
+          so TYPE_SIZE_UNIT is fine.  */
+       size = TYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
+      else
+       size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo));
       offset = size_binop (PLUS_EXPR, 
                           BINFO_OFFSET (base_binfo),
-                          CLASSTYPE_SIZE_UNIT (BINFO_TYPE (base_binfo)));
+                          size);
       end_of_base = tree_low_cst (offset, /*pos=*/1);
       if (end_of_base > result)
        result = end_of_base;
@@ -4827,6 +4721,32 @@ end_of_class (t, include_virtuals_p)
   return result;
 }
 
+/* Warn about direct bases of T that are inaccessible because they are
+   ambiguous.  For example:
+
+     struct S {};
+     struct T : public S {};
+     struct U : public S, public T {};
+
+   Here, `(S*) new U' is not allowed because there are two `S'
+   subobjects of U.  */
+
+static void
+warn_about_ambiguous_direct_bases (t)
+     tree t;
+{
+  int i;
+
+  for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
+    {
+      tree basetype = TYPE_BINFO_BASETYPE (t, i);
+
+      if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
+       warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
+                   basetype, t);
+    }
+}
+
 /* Compare two INTEGER_CSTs K1 and K2.  */
 
 static int
@@ -4839,16 +4759,14 @@ splay_tree_compare_integer_csts (k1, k2)
 
 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
    BINFO_OFFSETs for all of the base-classes.  Position the vtable
-   pointer.  */
+   pointer.  Accumulate declared virtual functions on VIRTUALS_P. */
 
 static void
-layout_class_type (t, empty_p, vfuns_p, 
-                  new_virtuals_p, overridden_virtuals_p)
+layout_class_type (t, empty_p, vfuns_p, virtuals_p)
      tree t;
      int *empty_p;
      int *vfuns_p;
-     tree *new_virtuals_p;
-     tree *overridden_virtuals_p;
+     tree *virtuals_p;
 {
   tree non_static_data_members;
   tree field;
@@ -4870,11 +4788,9 @@ layout_class_type (t, empty_p, vfuns_p,
   determine_primary_base (t, vfuns_p);
 
   /* Create a pointer to our virtual function table.  */
-  vptr = create_vtable_ptr (t, empty_p, vfuns_p,
-                           new_virtuals_p, overridden_virtuals_p);
+  vptr = create_vtable_ptr (t, empty_p, virtuals_p);
 
-  /* Under the new ABI, the vptr is always the first thing in the
-     class.  */
+  /* The vptr is always the first thing in the class.  */
   if (vptr)
     {
       TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
@@ -4884,16 +4800,9 @@ layout_class_type (t, empty_p, vfuns_p,
   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
   empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 
                                       NULL, NULL);
-  build_base_fields (rli, empty_p, empty_base_offsets);
-  /* Add pointers to all of our virtual base-classes.  */
-  TYPE_FIELDS (t) = chainon (build_vbase_pointer_fields (rli, empty_p),
-                            TYPE_FIELDS (t));
-
-  /* CLASSTYPE_INLINE_FRIENDS is really TYPE_NONCOPIED_PARTS.  Thus,
-     we have to save this before we start modifying
-     TYPE_NONCOPIED_PARTS.  */
-  fixup_inline_methods (t);
-
+  if (build_base_fields (rli, empty_p, empty_base_offsets, t))
+    CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
+  
   /* Layout the non-static data members.  */
   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
     {
@@ -4905,16 +4814,26 @@ layout_class_type (t, empty_p, vfuns_p,
       if (TREE_CODE (field) != FIELD_DECL)
        {
          place_field (rli, field);
+         /* If the static data member has incomplete type, keep track
+            of it so that it can be completed later.  (The handling 
+            of pending statics in finish_record_layout is
+            insufficient; consider:
+
+              struct S1;
+              struct S2 { static S1 s1; };
+              
+             At this point, finish_record_layout will be called, but
+            S1 is still incomplete.)  */
+         if (TREE_CODE (field) == VAR_DECL)
+           maybe_register_incomplete_var (field);
          continue;
        }
 
       type = TREE_TYPE (field);
 
       /* If this field is a bit-field whose width is greater than its
-        type, then there are some special rules for allocating it
-        under the new ABI.  Under the old ABI, there were no special
-        rules, but the back-end can't handle bitfields longer than a
-        `long long', so we use the same mechanism.  */
+        type, then there are some special rules for allocating
+        it.  */
       if (DECL_C_BIT_FIELD (field)
          && INT_CST_LT (TYPE_SIZE (type), DECL_SIZE (field)))
        {
@@ -4944,7 +4863,7 @@ layout_class_type (t, empty_p, vfuns_p,
        padding = NULL_TREE;
 
       layout_nonempty_base_or_field (rli, field, NULL_TREE,
-                                    empty_base_offsets);
+                                    empty_base_offsets, t);
 
       /* If we needed additional padding after this field, add it
         now.  */
@@ -4961,7 +4880,7 @@ layout_class_type (t, empty_p, vfuns_p,
          DECL_USER_ALIGN (padding_field) = 0;
          layout_nonempty_base_or_field (rli, padding_field,
                                         NULL_TREE, 
-                                        empty_base_offsets);
+                                        empty_base_offsets, t);
        }
     }
 
@@ -4974,24 +4893,21 @@ layout_class_type (t, empty_p, vfuns_p,
   if (TREE_CODE (rli_size_unit_so_far (rli)) == INTEGER_CST
       && compare_tree_int (rli_size_unit_so_far (rli), eoc) < 0)
     {
-      rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc + 1));
+      rli->offset = size_binop (MAX_EXPR, rli->offset, size_int (eoc));
       rli->bitpos = bitsize_zero_node;
     }
 
   /* We make all structures have at least one element, so that they
-     have non-zero size.  In the new ABI, the class may be empty even
-     if it has basetypes.  Therefore, we add the fake field after all
-     the other fields; if there are already FIELD_DECLs on the list,
-     their offsets will not be disturbed.  */
-  if (*empty_p)
+     have non-zero size.  The class may be empty even if it has
+     basetypes.  Therefore, we add the fake field after all the other
+     fields; if there are already FIELD_DECLs on the list, their
+     offsets will not be disturbed.  */
+  if (!eoc && *empty_p)
     {
       tree padding;
 
       padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
       place_field (rli, padding);
-      TYPE_NONCOPIED_PARTS (t) 
-       = tree_cons (NULL_TREE, padding, TYPE_NONCOPIED_PARTS (t));
-      TREE_STATIC (TYPE_NONCOPIED_PARTS (t)) = 1;
     }
 
   /* Let the back-end lay out the type. Note that at this point we
@@ -5012,6 +4928,12 @@ layout_class_type (t, empty_p, vfuns_p,
       CLASSTYPE_SIZE (t) = bitsize_zero_node;
       CLASSTYPE_SIZE_UNIT (t) = size_zero_node;
     }
+  /* If this is a POD, we can't reuse its tail padding.  */
+  else if (!CLASSTYPE_NON_POD_P (t))
+    {
+      CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
+      CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
+    }
   else
     {
       CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
@@ -5032,6 +4954,10 @@ layout_class_type (t, empty_p, vfuns_p,
      base subobject fields.  */
   layout_virtual_bases (t, empty_base_offsets);
 
+  /* Warn about direct bases that can't be talked about due to
+     ambiguity.  */
+  warn_about_ambiguous_direct_bases (t);
+
   /* Clean up.  */
   splay_tree_delete (empty_base_offsets);
 }
@@ -5069,15 +4995,8 @@ finish_struct_1 (t)
 {
   tree x;
   int vfuns;
-  /* The NEW_VIRTUALS is a TREE_LIST.  The TREE_VALUE of each node is
-     a FUNCTION_DECL.  Each of these functions is a virtual function
-     declared in T that does not override any virtual function from a
-     base class.  */
-  tree new_virtuals = NULL_TREE;
-  /* The OVERRIDDEN_VIRTUALS list is like the NEW_VIRTUALS list,
-     except that each declaration here overrides the declaration from
-     a base class.  */
-  tree overridden_virtuals = NULL_TREE;
+  /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL. */
+  tree virtuals = NULL_TREE;
   int n_fields = 0;
   tree vfield;
   int empty = 1;
@@ -5085,15 +5004,13 @@ finish_struct_1 (t)
   if (COMPLETE_TYPE_P (t))
     {
       if (IS_AGGR_TYPE (t))
-       cp_error ("redefinition of `%#T'", t);
+       error ("redefinition of `%#T'", t);
       else
-       my_friendly_abort (172);
+       abort ();
       popclass ();
       return;
     }
 
-  GNU_xref_decl (current_function_decl, t);
-
   /* If this type was previously laid out as a forward reference,
      make sure we lay it out again.  */
   TYPE_SIZE (t) = NULL_TREE;
@@ -5102,40 +5019,38 @@ finish_struct_1 (t)
   vfuns = 0;
   CLASSTYPE_RTTI (t) = NULL_TREE;
 
+  fixup_inline_methods (t);
+  
   /* Do end-of-class semantic processing: checking the validity of the
      bases and members and add implicitly generated methods.  */
   check_bases_and_members (t, &empty);
 
   /* Layout the class itself.  */
-  layout_class_type (t, &empty, &vfuns,
-                    &new_virtuals, &overridden_virtuals);
+  layout_class_type (t, &empty, &vfuns, &virtuals);
 
-  if (flag_dump_class_layout)
-    dump_class_hierarchy (*flag_dump_class_layout
-                          ? flag_dump_class_layout : NULL,
-                          t);
-  
-  /* Set up the DECL_FIELD_BITPOS of the vfield if we need to, as we
-     might need to know it for setting up the offsets in the vtable
-     (or in thunks) below.  */
+  /* Make sure that we get our own copy of the vfield FIELD_DECL.  */
   vfield = TYPE_VFIELD (t);
-  if (vfield != NULL_TREE
-      && DECL_FIELD_CONTEXT (vfield) != t)
-    {
-      tree binfo = get_binfo (DECL_FIELD_CONTEXT (vfield), t, 0);
-
+  if (vfield && CLASSTYPE_HAS_PRIMARY_BASE_P (t))
+    {
+      tree primary = CLASSTYPE_PRIMARY_BINFO (t);
+
+      my_friendly_assert (same_type_p (DECL_FIELD_CONTEXT (vfield),
+                                      BINFO_TYPE (primary)),
+                         20010726);
+      /* The vtable better be at the start. */
+      my_friendly_assert (integer_zerop (DECL_FIELD_OFFSET (vfield)),
+                         20010726);
+      my_friendly_assert (integer_zerop (BINFO_OFFSET (primary)),
+                         20010726);
+      
       vfield = copy_decl (vfield);
-
       DECL_FIELD_CONTEXT (vfield) = t;
-      DECL_FIELD_OFFSET (vfield)
-       = size_binop (PLUS_EXPR,
-                     BINFO_OFFSET (binfo),
-                     DECL_FIELD_OFFSET (vfield));
       TYPE_VFIELD (t) = vfield;
     }
+  else
+    my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
 
-  overridden_virtuals 
-    = modify_all_vtables (t, &vfuns, nreverse (overridden_virtuals));
+  virtuals = modify_all_vtables (t, &vfuns, nreverse (virtuals));
 
   /* If we created a new vtbl pointer for this class, add it to the
      list.  */
@@ -5144,11 +5059,8 @@ finish_struct_1 (t)
       = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
 
   /* If necessary, create the primary vtable for this class.  */
-  if (new_virtuals
-      || overridden_virtuals
-      || (TYPE_CONTAINS_VPTR_P (t) && vptrs_present_everywhere_p ()))
+  if (virtuals || TYPE_CONTAINS_VPTR_P (t))
     {
-      new_virtuals = nreverse (new_virtuals);
       /* We must enter these virtuals into the table.  */
       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
        build_primary_vtable (NULL_TREE, t);
@@ -5161,7 +5073,6 @@ finish_struct_1 (t)
         constructors might clobber the virtual function table.  But
         they don't if the derived class shares the exact vtable of the base
         class.  */
-
       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
     }
   /* If we didn't need a new vtable, see if we should copy one from
@@ -5187,14 +5098,8 @@ finish_struct_1 (t)
                            20000116);
 
       CLASSTYPE_VSIZE (t) = vfuns;
-      /* Entries for virtual functions defined in the primary base are
-        followed by entries for new functions unique to this class.  */
-      TYPE_BINFO_VIRTUALS (t) 
-       = chainon (TYPE_BINFO_VIRTUALS (t), new_virtuals);
-      /* Finally, add entries for functions that override virtuals
-        from non-primary bases.  */
-      TYPE_BINFO_VIRTUALS (t) 
-       = chainon (TYPE_BINFO_VIRTUALS (t), overridden_virtuals);
+      /* Add entries for virtual functions introduced by this class.  */
+      TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
     }
 
   finish_struct_bits (t);
@@ -5203,7 +5108,7 @@ finish_struct_1 (t)
      working on.  */
   for (x = TYPE_FIELDS (t); x; x = TREE_CHAIN (x))
     if (TREE_CODE (x) == VAR_DECL && TREE_STATIC (x)
-       && TREE_TYPE (x) == t)
+       && same_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (x)), t))
       DECL_MODE (x) = TYPE_MODE (t);
 
   /* Done with FIELDS...now decide whether to sort these for
@@ -5230,15 +5135,13 @@ finish_struct_1 (t)
     {
       tree vfields = CLASSTYPE_VFIELDS (t);
 
-      while (vfields)
-       {
-         /* Mark the fact that constructor for T
-            could affect anybody inheriting from T
-            who wants to initialize vtables for VFIELDS's type.  */
-         if (VF_DERIVED_VALUE (vfields))
-           TREE_ADDRESSABLE (vfields) = 1;
-         vfields = TREE_CHAIN (vfields);
-       }
+      for (vfields = CLASSTYPE_VFIELDS (t);
+          vfields; vfields = TREE_CHAIN (vfields))
+       /* Mark the fact that constructor for T could affect anybody
+          inheriting from T who wants to initialize vtables for
+          VFIELDS's type.  */
+       if (VF_BINFO_VALUE (vfields))
+         TREE_ADDRESSABLE (vfields) = 1;
     }
 
   /* Make the rtl for any new vtables we have created, and unmark
@@ -5248,28 +5151,19 @@ finish_struct_1 (t)
   /* Build the VTT for T.  */
   build_vtt (t);
 
-  if (TYPE_VFIELD (t))
-    {
-      /* In addition to this one, all the other vfields should be listed.  */
-      /* Before that can be done, we have to have FIELD_DECLs for them, and
-        a place to find them.  */
-      TYPE_NONCOPIED_PARTS (t) 
-       = tree_cons (default_conversion (TYPE_BINFO_VTABLE (t)),
-                    TYPE_VFIELD (t), TYPE_NONCOPIED_PARTS (t));
-
-      if (warn_nonvdtor && TYPE_HAS_DESTRUCTOR (t)
-         && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
-       cp_warning ("`%#T' has virtual functions but non-virtual destructor",
-                   t);
-    }
+  if (warn_nonvdtor && TYPE_POLYMORPHIC_P (t) && TYPE_HAS_DESTRUCTOR (t)
+      && DECL_VINDEX (TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1)) == NULL_TREE)
+    warning ("`%#T' has virtual functions but non-virtual destructor", t);
 
-  hack_incomplete_structures (t);
+  complete_vars (t);
 
   if (warn_overloaded_virtual)
     warn_hidden (t);
 
   maybe_suppress_debug_info (t);
 
+  dump_class_hierarchy (t);
+  
   /* Finish debugging output for this type.  */
   rest_of_type_compilation (t, ! LOCAL_CLASS_P (t));
 }
@@ -5320,7 +5214,7 @@ finish_struct (t, attributes)
      as necessary.  */
   unreverse_member_declarations (t);
 
-  cplus_decl_attributes (t, attributes, NULL_TREE);
+  cplus_decl_attributes (&t, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
 
   /* Nadger the current location so that diagnostics point to the start of
      the struct, not the end.  */
@@ -5343,14 +5237,10 @@ finish_struct (t, attributes)
   if (current_class_type)
     popclass ();
   else
-    error ("trying to finish struct, but kicked out due to previous parse errors.");
+    error ("trying to finish struct, but kicked out due to previous parse errors");
 
-  if (processing_template_decl)
-    {
-      tree scope = current_scope ();
-      if (scope && TREE_CODE (scope) == FUNCTION_DECL)
-       add_stmt (build_min (TAG_DEFN, t));
-    }
+  if (processing_template_decl && at_function_scope_p ())
+    add_stmt (build_min (TAG_DEFN, t));
 
   return t;
 }
@@ -5372,11 +5262,12 @@ fixed_type_or_null (instance, nonnull, cdtorp)
   switch (TREE_CODE (instance))
     {
     case INDIRECT_REF:
-      /* Check that we are not going through a cast of some sort.  */
-      if (TREE_TYPE (instance)
-         == TREE_TYPE (TREE_TYPE (TREE_OPERAND (instance, 0))))
-       instance = TREE_OPERAND (instance, 0);
-      /* fall through...  */
+      if (POINTER_TYPE_P (TREE_TYPE (instance)))
+       return NULL_TREE;
+      else
+       return fixed_type_or_null (TREE_OPERAND (instance, 0),
+                                  nonnull, cdtorp);
+
     case CALL_EXPR:
       /* This is a call to a constructor, hence it's never zero.  */
       if (TREE_HAS_CONSTRUCTOR (instance))
@@ -5406,7 +5297,7 @@ fixed_type_or_null (instance, nonnull, cdtorp)
        return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
        /* Propagate nonnull.  */
-       fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
+       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
       return NULL_TREE;
 
     case NOP_EXPR:
@@ -5433,6 +5324,7 @@ fixed_type_or_null (instance, nonnull, cdtorp)
       /* fall through...  */
     case TARGET_EXPR:
     case PARM_DECL:
+    case RESULT_DECL:
       if (IS_AGGR_TYPE (TREE_TYPE (instance)))
        {
          if (nonnull)
@@ -5459,6 +5351,11 @@ fixed_type_or_null (instance, nonnull, cdtorp)
           /* Reference variables should be references to objects.  */
           if (nonnull)
            *nonnull = 1;
+
+         if (TREE_CODE (instance) == VAR_DECL
+             && DECL_INITIAL (instance))
+           return fixed_type_or_null (DECL_INITIAL (instance),
+                                      nonnull, cdtorp);
        }
       return NULL_TREE;
 
@@ -5467,11 +5364,12 @@ fixed_type_or_null (instance, nonnull, cdtorp)
     }
 }
 
-/* Return non-zero if the dynamic type of INSTANCE is known, and equivalent
-   to the static type.  We also handle the case where INSTANCE is really
-   a pointer. Return negative if this is a ctor/dtor. There the dynamic type
-   is known, but this might not be the most derived base of the original object,
-   and hence virtual bases may not be layed out according to this type.
+/* Return non-zero if the dynamic type of INSTANCE is known, and
+   equivalent to the static type.  We also handle the case where
+   INSTANCE is really a pointer. Return negative if this is a
+   ctor/dtor. There the dynamic type is known, but this might not be
+   the most derived base of the original object, and hence virtual
+   bases may not be layed out according to this type.
 
    Used to determine whether the virtual function table is needed
    or not.
@@ -5508,7 +5406,6 @@ init_class_processing ()
     = (class_stack_node_t) xmalloc (current_class_stack_size 
                                    * sizeof (struct class_stack_node));
   VARRAY_TREE_INIT (local_classes, 8, "local_classes");
-  ggc_add_tree_varray_root (&local_classes, 1);
 
   access_default_node = build_int_2 (0, 0);
   access_public_node = build_int_2 (ak_public, 0);
@@ -5665,8 +5562,6 @@ void
 popclass ()
 {
   poplevel_class ();
-  /* Since poplevel_class does the popping of class decls nowadays,
-     this really only frees the obstack used for these decls.  */
   pop_class_decls ();
 
   current_class_depth--;
@@ -5756,6 +5651,14 @@ pop_nested_class ()
     pop_nested_class ();
 }
 
+/* Returns the number of extern "LANG" blocks we are nested within.  */
+
+int
+current_lang_depth ()
+{
+  return VARRAY_ACTIVE_SIZE (current_lang_base);
+}
+
 /* Set global variables CURRENT_LANG_NAME to appropriate value
    so that behavior of name-mangling machinery is correct.  */
 
@@ -5763,15 +5666,7 @@ void
 push_lang_context (name)
      tree name;
 {
-  *current_lang_stack++ = current_lang_name;
-  if (current_lang_stack - &VARRAY_TREE (current_lang_base, 0)
-      >= (ptrdiff_t) VARRAY_SIZE (current_lang_base))
-    {
-      size_t old_size = VARRAY_SIZE (current_lang_base);
-
-      VARRAY_GROW (current_lang_base, old_size + 10);
-      current_lang_stack = &VARRAY_TREE (current_lang_base, old_size);
-    }
+  VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
 
   if (name == lang_name_cplusplus)
     {
@@ -5806,10 +5701,8 @@ push_lang_context (name)
 void
 pop_lang_context ()
 {
-  /* Clear the current entry so that garbage collector won't hold on
-     to it.  */
-  *current_lang_stack = NULL_TREE;
-  current_lang_name = *--current_lang_stack;
+  current_lang_name = VARRAY_TOP_TREE (current_lang_base);
+  VARRAY_POP (current_lang_base);
 }
 \f
 /* Type instantiation routines.  */
@@ -5896,7 +5789,7 @@ resolve_address_of_overloaded_function (target_type,
   else 
     {
       if (complain)
-       cp_error ("\
+       error ("\
 cannot resolve overloaded function `%D' based on conversion to type `%T'", 
                  DECL_NAME (OVL_FUNCTION (overload)), target_type);
       return error_mark_node;
@@ -6017,7 +5910,7 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
       /* There were *no* matches.  */
       if (complain)
        {
-         cp_error ("no matches converting function `%D' to type `%#T'", 
+         error ("no matches converting function `%D' to type `%#T'", 
                    DECL_NAME (OVL_FUNCTION (overload)),
                    target_type);
 
@@ -6040,7 +5933,7 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
        {
          tree match;
 
-         cp_error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
+         error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
                    DECL_NAME (OVL_FUNCTION (overload)),
                    target_type);
 
@@ -6066,10 +5959,10 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
       if (!complain)
         return error_mark_node;
 
-      cp_pedwarn ("assuming pointer to member `%D'", fn);
+      pedwarn ("assuming pointer to member `%D'", fn);
       if (!explained)
         {
-          cp_pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
+          pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
           explained = 1;
         }
     }
@@ -6082,7 +5975,7 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
       /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
         will mark the function as addressed, but here we must do it
         explicitly.  */
-      mark_addressable (fn);
+      cxx_mark_addressable (fn);
 
       return fn;
     }
@@ -6101,14 +5994,14 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
 tree
 instantiate_type (lhstype, rhs, flags)
      tree lhstype, rhs;
-     enum instantiate_type_flags flags;
+     tsubst_flags_t flags;
 {
-  int complain = (flags & itf_complain);
-  int strict = (flags & itf_no_attributes)
+  int complain = (flags & tf_error);
+  int strict = (flags & tf_no_attributes)
                ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
-  int allow_ptrmem = flags & itf_ptrmem_ok;
+  int allow_ptrmem = flags & tf_ptrmem_ok;
   
-  flags &= ~itf_ptrmem_ok;
+  flags &= ~tf_ptrmem_ok;
   
   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
     {
@@ -6122,11 +6015,14 @@ instantiate_type (lhstype, rhs, flags)
       if (comptypes (lhstype, TREE_TYPE (rhs), strict))
        return rhs;
       if (complain)
-       cp_error ("argument of type `%T' does not match `%T'",
+       error ("argument of type `%T' does not match `%T'",
                  TREE_TYPE (rhs), lhstype);
       return error_mark_node;
     }
 
+  if (TREE_CODE (rhs) == BASELINK)
+    rhs = BASELINK_FUNCTIONS (rhs);
+
   /* We don't overwrite rhs if it is an overloaded function.
      Copying it would destroy the tree link.  */
   if (TREE_CODE (rhs) != OVERLOAD)
@@ -6144,7 +6040,7 @@ instantiate_type (lhstype, rhs, flags)
     case SAVE_EXPR:
     case CONSTRUCTOR:
     case BUFFER_REF:
-      my_friendly_abort (177);
+      abort ();
       return error_mark_node;
 
     case INDIRECT_REF:
@@ -6173,7 +6069,7 @@ instantiate_type (lhstype, rhs, flags)
     case OFFSET_REF:
       rhs = TREE_OPERAND (rhs, 1);
       if (BASELINK_P (rhs))
-       return instantiate_type (lhstype, TREE_VALUE (rhs),
+       return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs),
                                 flags | allow_ptrmem);
 
       /* This can happen if we are forming a pointer-to-member for a
@@ -6209,11 +6105,11 @@ instantiate_type (lhstype, rhs, flags)
       /* Now we should have a baselink. */
       my_friendly_assert (BASELINK_P (rhs), 990412);
 
-      return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
+      return instantiate_type (lhstype, BASELINK_FUNCTIONS (rhs), flags);
 
     case CALL_EXPR:
       /* This is too hard for now.  */
-      my_friendly_abort (183);
+      abort ();
       return error_mark_node;
 
     case PLUS_EXPR:
@@ -6315,19 +6211,19 @@ instantiate_type (lhstype, rhs, flags)
     case ADDR_EXPR:
     {
       if (PTRMEM_OK_P (rhs))
-        flags |= itf_ptrmem_ok;
+        flags |= tf_ptrmem_ok;
       
       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
     }
     case ENTRY_VALUE_EXPR:
-      my_friendly_abort (184);
+      abort ();
       return error_mark_node;
 
     case ERROR_MARK:
       return error_mark_node;
 
     default:
-      my_friendly_abort (185);
+      abort ();
       return error_mark_node;
     }
 }
@@ -6353,7 +6249,8 @@ get_vfield_name (type)
   type = BINFO_TYPE (binfo);
   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
                         + TYPE_NAME_LENGTH (type) + 2);
-  sprintf (buf, VFIELD_NAME_FORMAT, TYPE_NAME_STRING (type));
+  sprintf (buf, VFIELD_NAME_FORMAT,
+          IDENTIFIER_POINTER (constructor_name (type)));
   return get_identifier (buf);
 }
 
@@ -6440,7 +6337,7 @@ get_enclosing_class (type)
          break;
 
        default:
-         my_friendly_abort (0);
+         abort ();
        }
     }
   return NULL_TREE;
@@ -6454,7 +6351,7 @@ is_base_of_enclosing_class (base, type)
 {
   while (type)
     {
-      if (get_binfo (base, type, 0))
+      if (lookup_base (type, base, ba_any, NULL))
        return 1;
 
       type = get_enclosing_class (type);
@@ -6492,7 +6389,7 @@ maybe_note_name_used_in_class (name, decl)
 }
 
 /* Note that NAME was declared (as DECL) in the current class.  Check
-   to see that the declaration is legal.  */
+   to see that the declaration is valid.  */
 
 void
 note_name_declared_in_class (name, decl)
@@ -6516,17 +6413,16 @@ note_name_declared_in_class (name, decl)
         A name N used in a class S shall refer to the same declaration
         in its context and when re-evaluated in the completed scope of
         S.  */
-      cp_error ("declaration of `%#D'", decl);
+      error ("declaration of `%#D'", decl);
       cp_error_at ("changes meaning of `%D' from `%+#D'", 
                   DECL_NAME (OVL_CURRENT (decl)),
                   (tree) n->value);
     }
 }
 
-/* Returns the VAR_DECL for the complete vtable associated with
-   BINFO.  (Under the new ABI, secondary vtables are merged with
-   primary vtables; this function will return the VAR_DECL for the
-   primary vtable.)  */
+/* Returns the VAR_DECL for the complete vtable associated with BINFO.
+   Secondary vtables are merged with primary vtables; this function
+   will return the VAR_DECL for the primary vtable.  */
 
 tree
 get_vtbl_decl_for_binfo (binfo)
@@ -6546,36 +6442,43 @@ get_vtbl_decl_for_binfo (binfo)
   return decl;
 }
 
-/* Called from get_primary_binfo via dfs_walk.  */
+/* Called from get_primary_binfo via dfs_walk.  DATA is a TREE_LIST
+   who's TREE_PURPOSE is the TYPE of the required primary base and
+   who's TREE_VALUE is a list of candidate binfos that we fill in. */
 
 static tree
 dfs_get_primary_binfo (binfo, data)
      tree binfo;
      void *data;
 {
-  tree primary_base = (tree) data;
+  tree cons = (tree) data;
+  tree primary_base = TREE_PURPOSE (cons);
 
   if (TREE_VIA_VIRTUAL (binfo) 
-      && same_type_p (BINFO_TYPE (binfo), BINFO_TYPE (primary_base)))
-    return binfo;
+      && same_type_p (BINFO_TYPE (binfo), primary_base))
+    /* This is the right type of binfo, but it might be an unshared
+       instance, and the shared instance is later in the dfs walk.  We
+       must keep looking.  */
+    TREE_VALUE (cons) = tree_cons (NULL, binfo, TREE_VALUE (cons));
   
   return NULL_TREE;
 }
 
-/* Returns the binfo for the primary base of BINFO.  Note that in a
-   complex hierarchy the resulting BINFO may not actually *be*
-   primary.  In particular if the resulting BINFO is a virtual base,
-   and it occurs elsewhere in the hierarchy, then this occurrence may
-   not actually be a primary base in the complete object.  Check
-   BINFO_PRIMARY_P to be sure.  */
+/* Returns the unshared binfo for the primary base of BINFO.  Note
+   that in a complex hierarchy the resulting BINFO may not actually
+   *be* primary.  In particular if the resulting BINFO is a virtual
+   base, and it occurs elsewhere in the hierarchy, then this
+   occurrence may not actually be a primary base in the complete
+   object.  Check BINFO_PRIMARY_P to be sure.  */
 
 tree
 get_primary_binfo (binfo)
      tree binfo;
 {
   tree primary_base;
-  tree result;
-
+  tree result = NULL_TREE;
+  tree virtuals;
+  
   primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
   if (!primary_base)
     return NULL_TREE;
@@ -6597,34 +6500,89 @@ get_primary_binfo (binfo)
            return base_binfo;
        }
 
-      /* We should always find the primary base.  */
-      my_friendly_abort (20000729);
+      /* We should always find the primary base.  */
+      abort ();
+    }
+
+  /* For a primary virtual base, we have to scan the entire hierarchy
+     rooted at BINFO; the virtual base could be an indirect virtual
+     base.  There could be more than one instance of the primary base
+     in the hierarchy, and if one is the canonical binfo we want that
+     one.  If it exists, it should be the first one we find, but as a
+     consistency check we find them all and make sure.  */
+  virtuals = build_tree_list (BINFO_TYPE (primary_base), NULL_TREE);
+  dfs_walk (binfo, dfs_get_primary_binfo, NULL, virtuals);
+  virtuals = TREE_VALUE (virtuals);
+  
+  /* We must have found at least one instance.  */
+  my_friendly_assert (virtuals, 20010612);
+
+  if (TREE_CHAIN (virtuals))
+    {
+      /* We found more than one instance of the base. We must make
+         sure that, if one is the canonical one, it is the first one
+         we found. As the chain is in reverse dfs order, that means
+         the last on the list.  */
+      tree complete_binfo;
+      tree canonical;
+      
+      for (complete_binfo = binfo;
+          BINFO_INHERITANCE_CHAIN (complete_binfo);
+          complete_binfo = BINFO_INHERITANCE_CHAIN (complete_binfo))
+       continue;
+      canonical = binfo_for_vbase (BINFO_TYPE (primary_base),
+                                  BINFO_TYPE (complete_binfo));
+      
+      for (; virtuals; virtuals = TREE_CHAIN (virtuals))
+       {
+         result = TREE_VALUE (virtuals);
+
+         if (canonical == result)
+           {
+             /* This is the unshared instance. Make sure it was the
+                first one found.  */
+             my_friendly_assert (!TREE_CHAIN (virtuals), 20010612);
+             break;
+           }
+       }
     }
-
-  /* For a primary virtual base, we have to scan the entire hierarchy
-     rooted at BINFO; the virtual base could be an indirect virtual
-     base.  */
-  result = dfs_walk (binfo, dfs_get_primary_binfo, NULL, primary_base);
-  my_friendly_assert (result != NULL_TREE, 20000730);
+  else
+    result = TREE_VALUE (virtuals);
   return result;
 }
 
+/* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
+
+static int
+maybe_indent_hierarchy (stream, indent, indented_p)
+     FILE *stream;
+     int indent;
+     int indented_p;
+{
+  if (!indented_p)
+    fprintf (stream, "%*s", indent, "");
+  return 1;
+}
+
 /* Dump the offsets of all the bases rooted at BINFO (in the hierarchy
    dominated by T) to stderr.  INDENT should be zero when called from
    the top level; it is incremented recursively.  */
 
 static void
-dump_class_hierarchy_r (stream, t, binfo, indent)
+dump_class_hierarchy_r (stream, flags, t, binfo, indent)
      FILE *stream;
+     int flags;
      tree t;
      tree binfo;
      int indent;
 {
   int i;
-
-  fprintf (stream, "%*s0x%lx (%s) ", indent, "",
-          (unsigned long) binfo,
-          type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
+  int indented = 0;
+  
+  indented = maybe_indent_hierarchy (stream, indent, 0);
+  fprintf (stream, "%s (0x%lx) ",
+          type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
+          (unsigned long) binfo);
   fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
           tree_low_cst (BINFO_OFFSET (binfo), 0));
   if (is_empty_class (BINFO_TYPE (binfo)))
@@ -6634,116 +6592,225 @@ dump_class_hierarchy_r (stream, t, binfo, indent)
   if (TREE_VIA_VIRTUAL (binfo))
     {
       tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
-      
+
+      fprintf (stream, " virtual");
       if (canonical == binfo)
-        fprintf (stream, " virtual-canonical");
+        fprintf (stream, " canonical");
       else
-        fprintf (stream, " virtual-non-canonical");
+        fprintf (stream, " non-canonical");
     }
-  if (BINFO_PRIMARY_P (binfo))
-    fprintf (stream, " primary-for 0x%lx (%s)",
-             (unsigned long)BINFO_PRIMARY_BASE_OF (binfo),
-             type_as_string (BINFO_PRIMARY_BASE_OF (binfo), TFF_PLAIN_IDENTIFIER));
-  if (BINFO_LOST_PRIMARY_P (binfo))
-    fprintf (stream, " lost-primary");
   fprintf (stream, "\n");
 
+  indented = 0;
+  if (BINFO_PRIMARY_BASE_OF (binfo))
+    {
+      indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+      fprintf (stream, " primary-for %s (0x%lx)",
+              type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
+                              TFF_PLAIN_IDENTIFIER),
+              (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
+    }
+  if (BINFO_LOST_PRIMARY_P (binfo))
+    {
+      indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+      fprintf (stream, " lost-primary");
+    }
+  if (indented)
+    fprintf (stream, "\n");
+
+  if (!(flags & TDF_SLIM))
+    {
+      int indented = 0;
+      
+      if (BINFO_SUBVTT_INDEX (binfo))
+       {
+         indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+         fprintf (stream, " subvttidx=%s",
+                  expr_as_string (BINFO_SUBVTT_INDEX (binfo),
+                                  TFF_PLAIN_IDENTIFIER));
+       }
+      if (BINFO_VPTR_INDEX (binfo))
+       {
+         indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+         fprintf (stream, " vptridx=%s",
+                  expr_as_string (BINFO_VPTR_INDEX (binfo),
+                                  TFF_PLAIN_IDENTIFIER));
+       }
+      if (BINFO_VPTR_FIELD (binfo))
+       {
+         indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+         fprintf (stream, " vbaseoffset=%s",
+                  expr_as_string (BINFO_VPTR_FIELD (binfo),
+                                  TFF_PLAIN_IDENTIFIER));
+       }
+      if (BINFO_VTABLE (binfo))
+       {
+         indented = maybe_indent_hierarchy (stream, indent + 3, indented);
+         fprintf (stream, " vptr=%s",
+                  expr_as_string (BINFO_VTABLE (binfo),
+                                  TFF_PLAIN_IDENTIFIER));
+       }
+      
+      if (indented)
+       fprintf (stream, "\n");
+    }
+  
+
   for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
-    dump_class_hierarchy_r (stream, t, BINFO_BASETYPE (binfo, i), indent + 2);
+    dump_class_hierarchy_r (stream, flags,
+                           t, BINFO_BASETYPE (binfo, i),
+                           indent + 2);
 }
 
 /* Dump the BINFO hierarchy for T.  */
 
-void
-dump_class_hierarchy (name, t)
-     const char *name;
+static void
+dump_class_hierarchy (t)
      tree t;
 {
-  FILE *stream = stderr;
+  int flags;
+  FILE *stream = dump_begin (TDI_class, &flags);
+
+  if (!stream)
+    return;
   
-  if (name)
-    {
-      static int append = 0;
-      
-      stream = fopen (name, append++ ? "a" : "w");
-      if (!stream)
-        error ("could not open dump file `%s'", name);
-      return;
-    }
-  fprintf (stream, "%s\n",
-           type_as_string (t, TFF_PLAIN_IDENTIFIER));
-  dump_class_hierarchy_r (stream, t, TYPE_BINFO (t), 0);
+  fprintf (stream, "Class %s\n", type_as_string (t, TFF_PLAIN_IDENTIFIER));
+  fprintf (stream, "   size=%lu align=%lu\n",
+          (unsigned long)(tree_low_cst (TYPE_SIZE (t), 0) / BITS_PER_UNIT),
+          (unsigned long)(TYPE_ALIGN (t) / BITS_PER_UNIT));
+  dump_class_hierarchy_r (stream, flags, t, TYPE_BINFO (t), 0);
   fprintf (stream, "\n");
-  if (name)
-    fclose (stream);
+  dump_end (TDI_class, stream);
 }
 
-/* Virtual function table initialization.  */
+static void
+dump_array (stream, decl)
+     FILE *stream;
+     tree decl;
+{
+  tree inits;
+  int ix;
+  HOST_WIDE_INT elt;
+  tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
+
+  elt = (tree_low_cst (TYPE_SIZE (TREE_TYPE (TREE_TYPE (decl))), 0)
+        / BITS_PER_UNIT);
+  fprintf (stream, "%s:", decl_as_string (decl, TFF_PLAIN_IDENTIFIER));
+  fprintf (stream, " %s entries",
+          expr_as_string (size_binop (PLUS_EXPR, size, size_one_node),
+                          TFF_PLAIN_IDENTIFIER));
+  fprintf (stream, "\n");
 
-/* Create all the necessary vtables for T and its base classes.  */
+  for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
+       inits; ix++, inits = TREE_CHAIN (inits))
+    fprintf (stream, "%-4ld  %s\n", (long)(ix * elt),
+            expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
+}
 
 static void
-finish_vtbls (t)
+dump_vtable (t, binfo, vtable)
      tree t;
+     tree binfo;
+     tree vtable;
 {
-  if (merge_primary_and_secondary_vtables_p ())
-    {
-      tree list;
-      tree vbase;
+  int flags;
+  FILE *stream = dump_begin (TDI_class, &flags);
 
-      /* Under the new ABI, we lay out the primary and secondary
-        vtables in one contiguous vtable.  The primary vtable is
-        first, followed by the non-virtual secondary vtables in
-        inheritance graph order.  */
-      list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
-      accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
-                            TYPE_BINFO (t), t, list);
-      /* Then come the virtual bases, also in inheritance graph
-        order.  */
-      for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
+  if (!stream)
+    return;
+
+  if (!(flags & TDF_SLIM))
+    {
+      int ctor_vtbl_p = TYPE_BINFO (t) != binfo;
+      
+      fprintf (stream, "%s for %s",
+              ctor_vtbl_p ? "Construction vtable" : "Vtable",
+              type_as_string (binfo, TFF_PLAIN_IDENTIFIER));
+      if (ctor_vtbl_p)
        {
-         tree real_base;
-         
-         if (!TREE_VIA_VIRTUAL (vbase))
-           continue;
-          
-          /* Although we walk in inheritance order, that might not get the
-             canonical base.  */
-          real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
-          
-         accumulate_vtbl_inits (real_base, real_base,
-                                TYPE_BINFO (t), t, list);
+         if (!TREE_VIA_VIRTUAL (binfo))
+           fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
+         fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
        }
-
-      if (TYPE_BINFO_VTABLE (t))
-       initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
+      fprintf (stream, "\n");
+      dump_array (stream, vtable);
+      fprintf (stream, "\n");
     }
-  else
+  
+  dump_end (TDI_class, stream);
+}
+
+static void
+dump_vtt (t, vtt)
+     tree t;
+     tree vtt;
+{
+  int flags;
+  FILE *stream = dump_begin (TDI_class, &flags);
+
+  if (!stream)
+    return;
+
+  if (!(flags & TDF_SLIM))
     {
-      dfs_walk (TYPE_BINFO (t), dfs_finish_vtbls, 
-               dfs_unmarked_real_bases_queue_p, t);
-      dfs_walk (TYPE_BINFO (t), dfs_unmark, 
-               dfs_marked_real_bases_queue_p, t);
+      fprintf (stream, "VTT for %s\n",
+              type_as_string (t, TFF_PLAIN_IDENTIFIER));
+      dump_array (stream, vtt);
+      fprintf (stream, "\n");
     }
+  
+  dump_end (TDI_class, stream);
 }
 
-/* Called from finish_vtbls via dfs_walk.  */
+/* Virtual function table initialization.  */
 
-static tree
-dfs_finish_vtbls (binfo, data)
-     tree binfo;
-     void *data;
+/* Create all the necessary vtables for T and its base classes.  */
+
+static void
+finish_vtbls (t)
+     tree t;
 {
-  tree t = (tree) data;
+  tree list;
+  tree vbase;
+  int i;
 
-  if (BINFO_NEW_VTABLE_MARKED (binfo, t))
-    initialize_vtable (binfo, 
-                      build_vtbl_initializer (binfo, binfo, t, 
-                                              TYPE_BINFO (t), NULL));
+  /* We lay out the primary and secondary vtables in one contiguous
+     vtable.  The primary vtable is first, followed by the non-virtual
+     secondary vtables in inheritance graph order.  */
+  list = build_tree_list (TYPE_BINFO_VTABLE (t), NULL_TREE);
+  accumulate_vtbl_inits (TYPE_BINFO (t), TYPE_BINFO (t),
+                        TYPE_BINFO (t), t, list);
+  
+  /* Then come the virtual bases, also in inheritance graph order.  */
+  for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
+    {
+      tree real_base;
+         
+      if (!TREE_VIA_VIRTUAL (vbase))
+       continue;
+          
+      /* Although we walk in inheritance order, that might not get the
+         canonical base.  */
+      real_base = binfo_for_vbase (BINFO_TYPE (vbase), t);
+          
+      accumulate_vtbl_inits (real_base, real_base,
+                            TYPE_BINFO (t), t, list);
+    }
 
-  SET_BINFO_MARKED (binfo);
+  /* Fill in BINFO_VPTR_FIELD in the immediate binfos for our virtual
+     base classes, for the benefit of the debugging backends.  */
+  for (i = 0; i < BINFO_N_BASETYPES (TYPE_BINFO (t)); ++i)
+    {
+      tree base = BINFO_BASETYPE (TYPE_BINFO (t), i);
+      if (TREE_VIA_VIRTUAL (base))
+       {
+         vbase = binfo_for_vbase (BINFO_TYPE (base), t);
+         BINFO_VPTR_FIELD (base) = BINFO_VPTR_FIELD (vbase);
+       }
+    }
 
-  return NULL_TREE;
+  if (TYPE_BINFO_VTABLE (t))
+    initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
 }
 
 /* Initialize the vtable for BINFO with the INITS.  */
@@ -6758,6 +6825,7 @@ initialize_vtable (binfo, inits)
   layout_vtable_decl (binfo, list_length (inits));
   decl = get_vtbl_decl_for_binfo (binfo);
   initialize_array (decl, inits);
+  dump_vtable (BINFO_TYPE (binfo), binfo, decl);
 }
 
 /* Initialize DECL (a declaration for a namespace-scope array) with
@@ -6782,7 +6850,8 @@ initialize_array (decl, inits)
    
    This holds
    1 - primary virtual pointer for complete object T
-   2 - secondary VTTs for each direct non-virtual base of T which requires a VTT
+   2 - secondary VTTs for each direct non-virtual base of T which requires a
+       VTT
    3 - secondary virtual pointers for each direct or indirect base of T which
        has virtual bases or is reachable via a virtual path from T.
    4 - secondary VTTs for each direct or indirect virtual base of T.
@@ -6808,18 +6877,19 @@ build_vtt (t)
     return;
 
   /* Figure out the type of the VTT.  */
-  type = build_index_type (size_int (list_length (inits)));
+  type = build_index_type (size_int (list_length (inits) - 1));
   type = build_cplus_array_type (const_ptr_type_node, type);
                                 
   /* Now, build the VTT object itself.  */
   vtt = build_vtable (t, get_vtt_name (t), type);
-  SET_DECL_ASSEMBLER_NAME (vtt, DECL_NAME (vtt));
   pushdecl_top_level (vtt);
   initialize_array (vtt, inits);
+
+  dump_vtt (t, vtt);
 }
 
 /* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
-   from within some heirarchy which is inherited from the type of BINFO.
+   from within some hierarchy which is inherited from the type of BINFO.
    Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
    BINFO.  */
 
@@ -6841,10 +6911,35 @@ get_original_base (base_binfo, binfo)
     if (same_type_p (BINFO_TYPE (base_binfo),
                      BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
       return BINFO_BASETYPE (derived, ix);
-  my_friendly_abort (20010223);
+  abort ();
   return NULL;
 }
 
+/* When building a secondary VTT, BINFO_VTABLE is set to a TREE_LIST with
+   PURPOSE the RTTI_BINFO, VALUE the real vtable pointer for this binfo,
+   and CHAIN the vtable pointer for this binfo after construction is
+   complete.  VALUE can also be another BINFO, in which case we recurse. */
+
+static tree
+binfo_ctor_vtable (binfo)
+     tree binfo;
+{
+  tree vt;
+
+  while (1)
+    {
+      vt = BINFO_VTABLE (binfo);
+      if (TREE_CODE (vt) == TREE_LIST)
+       vt = TREE_VALUE (vt);
+      if (TREE_CODE (vt) == TREE_VEC)
+       binfo = vt;
+      else
+       break;
+    }
+
+  return vt;
+}
+
 /* Recursively build the VTT-initializer for BINFO (which is in the
    hierarchy dominated by T).  INITS points to the end of the initializer
    list to date.  INDEX is the VTT index where the next element will be
@@ -6881,9 +6976,7 @@ build_vtt_inits (binfo, t, inits, index)
     }
 
   /* Add the address of the primary vtable for the complete object.  */
-  init = BINFO_VTABLE (binfo);
-  if (TREE_CODE (init) == TREE_LIST)
-    init = TREE_VALUE (init);
+  init = binfo_ctor_vtable (binfo);
   *inits = build_tree_list (NULL_TREE, init);
   inits = &TREE_CHAIN (*inits);
   if (top_level_p)
@@ -7020,33 +7113,26 @@ dfs_build_secondary_vptr_vtt_inits (binfo, data)
       while (BINFO_PRIMARY_BASE_OF (binfo))
         binfo = BINFO_PRIMARY_BASE_OF (binfo);
     }
-  init = BINFO_VTABLE (binfo);
-  if (TREE_CODE (init) == TREE_LIST)
-    init = TREE_VALUE (init);
+  init = binfo_ctor_vtable (binfo);
   TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
 
   return NULL_TREE;
 }
 
 /* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
-   TREE_UNSIGNED indicates that a constructor vtable is being built.
-   TREE_USED indicates whether marked or unmarked bases should be walked.
-   TREE_PURPOSE is the TREE_TYPE that dominates the hierarchy.  */
+   VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
+   should be walked.  TREE_PURPOSE is the TREE_TYPE that dominates the
+   hierarchy.  */
 
 static tree
 dfs_ctor_vtable_bases_queue_p (binfo, data)
      tree binfo;
      void *data;
 {
-  if (TREE_VIA_VIRTUAL (binfo) && !BINFO_PRIMARY_P (binfo))
-    {
-      tree type = TREE_PURPOSE ((tree) data);
+  if (TREE_VIA_VIRTUAL (binfo))
+     /* Get the shared version.  */
+    binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
 
-      /* This is a non-primary virtual base, get the shared version.  */
-      binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
-      if (VTT_TOP_LEVEL_P ((tree) data) && BINFO_PRIMARY_P (binfo))
-        return NULL_TREE;
-    }
   if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
     return NULL_TREE;
   return binfo;
@@ -7070,7 +7156,8 @@ dfs_fixup_binfo_vtbls (binfo, data)
 
   /* If we scribbled the construction vtable vptr into BINFO, clear it
      out now.  */
-  if (TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
+  if (BINFO_VTABLE (binfo)
+      && TREE_CODE (BINFO_VTABLE (binfo)) == TREE_LIST
       && (TREE_PURPOSE (BINFO_VTABLE (binfo)) 
          == TREE_VALUE ((tree) data)))
     BINFO_VTABLE (binfo) = TREE_CHAIN (BINFO_VTABLE (binfo));
@@ -7093,7 +7180,7 @@ build_ctor_vtbl_group (binfo, t)
   tree id;
   tree vbase;
 
-  /* See if we've already create this construction vtable group.  */
+  /* See if we've already created this construction vtable group.  */
   id = mangle_ctor_vtbl_for_type (t, binfo);
   if (IDENTIFIER_GLOBAL_VALUE (id))
     return;
@@ -7126,13 +7213,14 @@ build_ctor_vtbl_group (binfo, t)
   inits = TREE_VALUE (list);
 
   /* Figure out the type of the construction vtable.  */
-  type = build_index_type (size_int (list_length (inits)));
+  type = build_index_type (size_int (list_length (inits) - 1));
   type = build_cplus_array_type (vtable_entry_type, type);
   TREE_TYPE (vtbl) = type;
 
   /* Initialize the construction vtable.  */
   pushdecl_top_level (vtbl);
   initialize_array (vtbl, inits);
+  dump_vtable (t, binfo, vtbl);
 }
 
 /* Add the vtbl initializers for BINFO (and its bases other than
@@ -7160,6 +7248,10 @@ accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
                                   BINFO_TYPE (orig_binfo)),
                      20000517);
 
+  /* If it doesn't have a vptr, we don't do anything. */
+  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
+    return;
+  
   /* If we're building a construction vtable, we're not interested in
      subobjects that don't require construction vtables.  */
   if (ctor_vtbl_p 
@@ -7192,9 +7284,8 @@ accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
     }
 }
 
-/* Called from accumulate_vtbl_inits when using the new ABI.
-   Accumulates the vtable initializers for all of the vtables into
-   TREE_VALUE (DATA).  Returns the initializers for the BINFO vtable.  */
+/* Called from accumulate_vtbl_inits.  Returns the initializers for
+   the BINFO vtable.  */
 
 static tree
 dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
@@ -7211,49 +7302,56 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
   if (ctor_vtbl_p
       && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
     {
-      /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a primary
-         virtual base.  If it is not the same primary in the hierarchy of T,
-         we'll need to generate a ctor vtable for it, to place at its
-         location in T.  If it is the same primary, we still need a VTT
-         entry for the vtable, but that must be the base it is a
-         primary for within the sub-hierarchy of RTTI_BINFO.  */
-      tree parent;
-      tree best_primary = NULL;
-      tree primary_for;
-      
-      my_friendly_assert (BINFO_PRIMARY_P (binfo), 20010131);
+      /* In the hierarchy of BINFO_TYPE (RTTI_BINFO), this is a
+        primary virtual base.  If it is not the same primary in
+        the hierarchy of T, we'll need to generate a ctor vtable
+        for it, to place at its location in T.  If it is the same
+        primary, we still need a VTT entry for the vtable, but it
+        should point to the ctor vtable for the base it is a
+        primary for within the sub-hierarchy of RTTI_BINFO.
+             
+        There are three possible cases:
+             
+        1) We are in the same place.
+        2) We are a primary base within a lost primary virtual base of
+        RTTI_BINFO.
+        3) We are primary to something not a base of RTTI_BINFO.  */
+         
+      tree b = BINFO_PRIMARY_BASE_OF (binfo);
+      tree last = NULL_TREE;
 
-      for (primary_for = BINFO_PRIMARY_BASE_OF (binfo);
-           primary_for;
-           primary_for = BINFO_PRIMARY_BASE_OF (primary_for))
-        {
-          for (parent = primary_for;
-               parent;
-               parent = BINFO_INHERITANCE_CHAIN (parent))
-             {
-               if (parent == rtti_binfo)
-                 {
-                   best_primary = primary_for;
-                   break;
-                 }
-             }
-           if (!parent)
-             break;
-        }
-      if (best_primary)
-        {
-          vtbl = BINFO_VTABLE (best_primary);
-          if (TREE_CODE (vtbl) == TREE_LIST)
-            {
-              my_friendly_assert (TREE_PURPOSE (vtbl) == rtti_binfo,
-                                  20010126);
-              vtbl = TREE_VALUE (vtbl);
-            }
-        }
+      /* First, look through the bases we are primary to for RTTI_BINFO
+        or a virtual base.  */
+      for (; b; b = BINFO_PRIMARY_BASE_OF (b))
+       {
+         last = b;
+         if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
+           break;
+       }
+      /* If we run out of primary links, keep looking down our
+        inheritance chain; we might be an indirect primary.  */
+      if (b == NULL_TREE)
+       for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
+         if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
+           break;
+
+      /* If we found RTTI_BINFO, this is case 1.  If we found a virtual
+        base B and it is a base of RTTI_BINFO, this is case 2.  In
+        either case, we share our vtable with LAST, i.e. the
+        derived-most base within B of which we are a primary.  */
+      if (b == rtti_binfo
+         || (b && binfo_for_vbase (BINFO_TYPE (b),
+                                   BINFO_TYPE (rtti_binfo))))
+       /* Just set our BINFO_VTABLE to point to LAST, as we may not have
+          set LAST's BINFO_VTABLE yet.  We'll extract the actual vptr in
+          binfo_ctor_vtable after everything's been set up.  */
+       vtbl = last;
+
+      /* Otherwise, this is case 3 and we get our own.  */
     }
   else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
     return inits;
-  
+
   if (!vtbl)
     {
       tree index;
@@ -7268,6 +7366,7 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
       vtbl = build1 (ADDR_EXPR, 
                     vtbl_ptr_type_node,
                     vtbl);
+      TREE_CONSTANT (vtbl) = 1;
       index = size_binop (PLUS_EXPR,
                          size_int (non_fn_entries),
                          size_int (list_length (TREE_VALUE (l))));
@@ -7278,23 +7377,21 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
       TREE_CONSTANT (vtbl) = 1;
     }
 
-  if (!ctor_vtbl_p)
-    {
-      /* For an ordinary vtable, set BINFO_VTABLE.  */
-      BINFO_VTABLE (binfo) = vtbl;
-      if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
-        inits = NULL_TREE;
-    }
-  else
+  if (ctor_vtbl_p)
     /* For a construction vtable, we can't overwrite BINFO_VTABLE.
        So, we make a TREE_LIST.  Later, dfs_fixup_binfo_vtbls will
        straighten this out.  */
     BINFO_VTABLE (binfo) = tree_cons (rtti_binfo, vtbl, BINFO_VTABLE (binfo));
+  else if (BINFO_PRIMARY_P (binfo) && TREE_VIA_VIRTUAL (binfo))
+    inits = NULL_TREE;
+  else
+     /* For an ordinary vtable, set BINFO_VTABLE.  */
+    BINFO_VTABLE (binfo) = vtbl;
 
   return inits;
 }
 
-/* Construct the initializer for BINFOs virtual function table.  BINFO
+/* Construct the initializer for BINFO's virtual function table.  BINFO
    is part of the hierarchy dominated by T.  If we're building a
    construction vtable, the ORIG_BINFO is the binfo we should use to
    find the actual function pointers to put in the vtable - but they
@@ -7325,7 +7422,7 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
      tree rtti_binfo;
      int *non_fn_entries_p;
 {
-  tree v;
+  tree v, b;
   tree vfun_inits;
   tree vbase;
   vtbl_init_data vid;
@@ -7339,7 +7436,7 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
   vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
   vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
   /* The first vbase or vcall offset is at index -3 in the vtable.  */
-  vid.index = ssize_int (-3);
+  vid.index = ssize_int (-3 * TARGET_VTABLE_DATA_ENTRY_DISTANCE);
 
   /* Add entries to the vtable for RTTI.  */
   build_rtti_vtbl_entries (binfo, &vid);
@@ -7350,8 +7447,6 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
   VARRAY_TREE_INIT (vid.fns, 32, "fns");
   /* Add the vcall and vbase offset entries.  */
   build_vcall_and_vbase_vtbl_entries (binfo, &vid);
-  /* Clean up.  */
-  VARRAY_FREE (vid.fns);
   /* Clear BINFO_VTABLE_PATH_MARKED; it's set by
      build_vbase_offset_vtbl_entries.  */
   for (vbase = CLASSTYPE_VBASECLASSES (t); 
@@ -7359,6 +7454,22 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
        vbase = TREE_CHAIN (vbase))
     CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
 
+  /* If the target requires padding between data entries, add that now.  */
+  if (TARGET_VTABLE_DATA_ENTRY_DISTANCE > 1)
+    {
+      tree cur, *prev;
+
+      for (prev = &vid.inits; (cur = *prev); prev = &TREE_CHAIN (cur))
+       {
+         tree add = cur;
+         int i;
+
+         for (i = 1; i < TARGET_VTABLE_DATA_ENTRY_DISTANCE; ++i)
+           add = tree_cons (NULL_TREE, null_pointer_node, add);
+         *prev = add;
+       }
+    }
+
   if (non_fn_entries_p)
     *non_fn_entries_p = list_length (vid.inits);
 
@@ -7371,49 +7482,87 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
       tree vcall_index;
       tree fn;
       tree pfn;
-      tree init;
-      int generate_with_vtable_p = BV_GENERATE_THUNK_WITH_VTABLE_P (v);
+      tree init = NULL_TREE;
       
-      /* Pull the offset for `this', and the function to call, out of
-        the list.  */
-      delta = BV_DELTA (v);
+      fn = BV_FN (v);
 
-      if (BV_USE_VCALL_INDEX_P (v))
+      /* If the only definition of this function signature along our
+        primary base chain is from a lost primary, this vtable slot will
+        never be used, so just zero it out.  This is important to avoid
+        requiring extra thunks which cannot be generated with the function.
+
+        We first check this in update_vtable_entry_for_fn, so we handle
+        restored primary bases properly; we also need to do it here so we
+        zero out unused slots in ctor vtables, rather than filling themff
+        with erroneous values (though harmless, apart from relocation
+        costs).  */
+      for (b = binfo; ; b = get_primary_binfo (b))
        {
-         vcall_index = BV_VCALL_INDEX (v);
-         my_friendly_assert (vcall_index != NULL_TREE, 20000621);
+         /* We found a defn before a lost primary; go ahead as normal.  */
+         if (look_for_overrides_here (BINFO_TYPE (b), fn))
+           break;
+
+         /* The nearest definition is from a lost primary; clear the
+            slot.  */
+         if (BINFO_LOST_PRIMARY_P (b))
+           {
+             init = size_zero_node;
+             break;
+           }
+       }
+
+      if (! init)
+       {
+         /* Pull the offset for `this', and the function to call, out of
+            the list.  */
+         delta = BV_DELTA (v);
+
+         if (BV_USE_VCALL_INDEX_P (v))
+           {
+             vcall_index = BV_VCALL_INDEX (v);
+             my_friendly_assert (vcall_index != NULL_TREE, 20000621);
+           }
+         else
+           vcall_index = NULL_TREE;
+
+         my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
+         my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
+
+         /* You can't call an abstract virtual function; it's abstract.
+            So, we replace these functions with __pure_virtual.  */
+         if (DECL_PURE_VIRTUAL_P (fn))
+           fn = abort_fndecl;
+
+         /* Take the address of the function, considering it to be of an
+            appropriate generic type.  */
+         pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
+         /* The address of a function can't change.  */
+         TREE_CONSTANT (pfn) = 1;
+
+         /* Enter it in the vtable.  */
+         init = build_vtable_entry (delta, vcall_index, pfn);
        }
-      else if (vid.ctor_vtbl_p && BV_VCALL_INDEX (v))
-        {
-          /* In the original, we did not need to use the vcall index, even
-             though there was one, but in a ctor vtable things might be
-             different (a primary virtual base might have moved). Be
-             conservative and use a vcall adjusting thunk.  */
-         vcall_index = BV_VCALL_INDEX (v);
-          generate_with_vtable_p = 1;
-        }
-      else
-        vcall_index = NULL_TREE;
 
-      fn = BV_FN (v);
-      my_friendly_assert (TREE_CODE (delta) == INTEGER_CST, 19990727);
-      my_friendly_assert (TREE_CODE (fn) == FUNCTION_DECL, 19990727);
-
-      /* You can't call an abstract virtual function; it's abstract.
-        So, we replace these functions with __pure_virtual.  */
-      if (DECL_PURE_VIRTUAL_P (fn))
-       fn = abort_fndecl;
-
-      /* Take the address of the function, considering it to be of an
-        appropriate generic type.  */
-      pfn = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
-      /* The address of a function can't change.  */
-      TREE_CONSTANT (pfn) = 1;
-      /* Enter it in the vtable.  */
-      init = build_vtable_entry (delta, vcall_index, pfn,
-                                generate_with_vtable_p);
       /* And add it to the chain of initializers.  */
-      vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
+      if (TARGET_VTABLE_USES_DESCRIPTORS)
+       {
+         int i;
+         if (init == size_zero_node)
+           for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
+             vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
+         else
+           for (i = 0; i < TARGET_VTABLE_USES_DESCRIPTORS; ++i)
+             {
+               tree fdesc = build (FDESC_EXPR, vfunc_ptr_type_node,
+                                   TREE_OPERAND (init, 0),
+                                   build_int_2 (i, 0));
+               TREE_CONSTANT (fdesc) = 1;
+
+               vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
+             }
+       }
+      else
+        vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
     }
 
   /* The initializers for virtual functions were built up in reverse
@@ -7427,7 +7576,7 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
   return chainon (vid.inits, vfun_inits);
 }
 
-/* Sets vid->inits to be the initializers for the vbase and vcall
+/* Adds to vid->inits the initializers for the vbase and vcall
    offsets in BINFO, which is in the hierarchy dominated by T.  */
 
 static void
@@ -7461,11 +7610,7 @@ build_vbase_offset_vtbl_entries (binfo, vid)
 {
   tree vbase;
   tree t;
-
-  /* Under the old ABI, pointers to virtual bases are stored in each
-     object.  */
-  if (!vbase_offsets_in_vtable_p ())
-    return;
+  tree non_primary_binfo;
 
   /* If there are no virtual baseclasses, then there is nothing to
      do.  */
@@ -7473,6 +7618,30 @@ build_vbase_offset_vtbl_entries (binfo, vid)
     return;
 
   t = vid->derived;
+  
+  /* We might be a primary base class.  Go up the inheritance hierarchy
+     until we find the most derived class of which we are a primary base:
+     it is the offset of that which we need to use.  */
+  non_primary_binfo = binfo;
+  while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
+    {
+      tree b;
+
+      /* If we have reached a virtual base, then it must be a primary
+        base (possibly multi-level) of vid->binfo, or we wouldn't
+        have called build_vcall_and_vbase_vtbl_entries for it.  But it
+        might be a lost primary, so just skip down to vid->binfo.  */
+      if (TREE_VIA_VIRTUAL (non_primary_binfo))
+       {
+         non_primary_binfo = vid->binfo;
+         break;
+       }
+
+      b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
+      if (get_primary_binfo (b) != non_primary_binfo)
+       break;
+      non_primary_binfo = b;
+    }
 
   /* Go through the virtual bases, adding the offsets.  */
   for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
@@ -7514,17 +7683,19 @@ build_vbase_offset_vtbl_entries (binfo, vid)
          /* The vbase offset had better be the same.  */
          if (!tree_int_cst_equal (delta,
                                   BINFO_VPTR_FIELD (orig_vbase)))
-           my_friendly_abort (20000403);
+           abort ();
        }
 
       /* The next vbase will come at a more negative offset.  */
-      vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
+      vid->index = size_binop (MINUS_EXPR, vid->index,
+                              ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
 
       /* The initializer is the delta from BINFO to this virtual base.
         The vbase offsets go in reverse inheritance-graph order, and
         we are walking in inheritance graph order so these end up in
         the right order.  */
-      delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (binfo));
+      delta = size_diffop (BINFO_OFFSET (b), BINFO_OFFSET (non_primary_binfo));
+      
       *vid->last_init 
        = build_tree_list (NULL_TREE,
                           fold (build1 (NOP_EXPR, 
@@ -7535,19 +7706,14 @@ build_vbase_offset_vtbl_entries (binfo, vid)
 }
 
 /* Adds the initializers for the vcall offset entries in the vtable
-   for BINFO (which is part of the class hierarchy dominated by T) to
-   VID->INITS.  */
+   for BINFO (which is part of the class hierarchy dominated by VID->DERIVED)
+   to VID->INITS.  */
 
 static void
 build_vcall_offset_vtbl_entries (binfo, vid)
      tree binfo;
      vtbl_init_data *vid;
 {
-  /* Under the old ABI, the adjustments to the `this' pointer were made
-     elsewhere.  */
-  if (!vcall_offsets_in_vtable_p ())
-    return;
-
   /* We only need these entries if this base is a virtual base.  */
   if (!TREE_VIA_VIRTUAL (binfo))
     return;
@@ -7556,22 +7722,20 @@ build_vcall_offset_vtbl_entries (binfo, vid)
      vtable.  For example:
 
        class A { virtual void f (); };
-       class B : virtual public A { };
-       class C: virtual public A, public B {};
-      
-     Now imagine:
-
-       B* b = new C;
-       b->f();
-
-     The location of `A' is not at a fixed offset relative to `B'; the
-     offset depends on the complete object derived from `B'.  So, 
-     `B' vtable contains an entry for `f' that indicates by what
-     amount the `this' pointer for `B' needs to be adjusted to arrive
-     at `A'.  
+       class B1 : virtual public A { virtual void f (); };
+       class B2 : virtual public A { virtual void f (); };
+       class C: public B1, public B2 { virtual void f (); };
+
+     A C object has a primary base of B1, which has a primary base of A.  A
+     C also has a secondary base of B2, which no longer has a primary base
+     of A.  So the B2-in-C construction vtable needs a secondary vtable for
+     A, which will adjust the A* to a B2* to call f.  We have no way of
+     knowing what (or even whether) this offset will be when we define B2,
+     so we store this "vcall offset" in the A sub-vtable and look it up in
+     a "virtual thunk" for B2::f.
 
      We need entries for all the functions in our primary vtable and
-     in our non-virtual bases vtables.  */
+     in our non-virtual bases' secondary vtables.  */
   vid->vbase = binfo;
   /* Now, walk through the non-virtual bases, adding vcall offsets.  */
   add_vcall_offset_vtbl_entries_r (binfo, vid);
@@ -7588,7 +7752,9 @@ add_vcall_offset_vtbl_entries_r (binfo, vid)
   tree primary_binfo;
 
   /* Don't walk into virtual bases -- except, of course, for the
-     virtual base for which we are building vcall offsets.  */
+     virtual base for which we are building vcall offsets.  Any
+     primary virtual base will have already had its offsets generated
+     through the recursion in build_vcall_and_vbase_vtbl_entries.  */
   if (TREE_VIA_VIRTUAL (binfo) && vid->vbase != binfo)
     return;
   
@@ -7622,43 +7788,30 @@ add_vcall_offset_vtbl_entries_1 (binfo, vid)
   tree base_virtuals;
   tree orig_virtuals;
   tree binfo_inits;
-  int lost_primary = 0;
-  /* If BINFO is a primary base, this is the least derived class of
-     BINFO that is not a primary base.  */
+  /* If BINFO is a primary base, the most derived class which has BINFO as
+     a primary base; otherwise, just BINFO.  */
   tree non_primary_binfo;
 
   binfo_inits = NULL_TREE;
 
-  /* We might be a primary base class.  Go up the inheritance
-     hierarchy until we find the class of which we are a primary base:
+  /* We might be a primary base class.  Go up the inheritance hierarchy
+     until we find the most derived class of which we are a primary base:
      it is the BINFO_VIRTUALS there that we need to consider.  */
   non_primary_binfo = binfo;
   while (BINFO_INHERITANCE_CHAIN (non_primary_binfo))
     {
       tree b;
 
-      /* If we have reached a virtual base, then it must be the
-        virtual base for which we are building vcall offsets.  In
-        turn, the virtual base must be a (possibly indirect) primary
-        base of the class that we are initializing, or we wouldn't
-        care about its vtable offsets.  */
+      /* If we have reached a virtual base, then it must be vid->vbase,
+        because we ignore other virtual bases in
+        add_vcall_offset_vtbl_entries_r.  In turn, it must be a primary
+        base (possibly multi-level) of vid->binfo, or we wouldn't
+        have called build_vcall_and_vbase_vtbl_entries for it.  But it
+        might be a lost primary, so just skip down to vid->binfo.  */
       if (TREE_VIA_VIRTUAL (non_primary_binfo))
        {
-         if (vid->ctor_vtbl_p)
-           {
-             tree probe;
-         
-             for (probe = vid->binfo;
-                  probe != non_primary_binfo;
-                  probe = get_primary_binfo (probe))
-               {
-                  if (BINFO_LOST_PRIMARY_P (probe))
-                    {
-                      lost_primary = 1;
-                      break;
-                    }
-               }
-            }
+         if (non_primary_binfo != vid->vbase)
+           abort ();
          non_primary_binfo = vid->binfo;
          break;
        }
@@ -7692,13 +7845,12 @@ add_vcall_offset_vtbl_entries_1 (binfo, vid)
       tree vcall_offset;
 
       /* Find the declaration that originally caused this function to
-        be present.  */
+        be present in BINFO_TYPE (binfo).  */
       orig_fn = BV_FN (orig_virtuals);
 
-      /* We do not need an entry if this function is declared in a
-        virtual base (or one of its virtual bases), and not
-        overridden in the section of the hierarchy dominated by the
-        virtual base for which we are building vcall offsets.  */
+      /* When processing BINFO, we only want to generate vcall slots for
+        function slots introduced in BINFO.  So don't try to generate
+        one if the function isn't even defined in BINFO.  */
       if (!same_type_p (DECL_CONTEXT (orig_fn), BINFO_TYPE (binfo)))
        continue;
 
@@ -7729,16 +7881,19 @@ add_vcall_offset_vtbl_entries_1 (binfo, vid)
       if (i != VARRAY_ACTIVE_SIZE (vid->fns))
        continue;
 
-      /* The FN comes from BASE.  So, we must calculate the adjustment
-        from the virtual base that derived from BINFO to BASE.  */
+      /* The FN comes from BASE.  So, we must calculate the adjustment from
+        vid->vbase to BASE.  We can just look for BASE in the complete
+        object because we are converting from a virtual base, so if there
+        were multiple copies, there would not be a unique final overrider
+        and vid->derived would be ill-formed.  */
       base = DECL_CONTEXT (fn);
-      base_binfo = get_binfo (base, vid->derived, /*protect=*/0);
+      base_binfo = lookup_base (vid->derived, base, ba_any, NULL);
 
       /* Compute the vcall offset.  */
-      vcall_offset = BINFO_OFFSET (vid->vbase);
-      if (lost_primary)
-        vcall_offset = size_binop (PLUS_EXPR, vcall_offset,
-                                   BINFO_OFFSET (vid->binfo));
+      /* As mentioned above, the vbase we're working on is a primary base of
+        vid->binfo.  But it might be a lost primary, so its BINFO_OFFSET
+         might be wrong, so we just use the BINFO_OFFSET from vid->binfo.  */
+      vcall_offset = BINFO_OFFSET (vid->binfo);
       vcall_offset = size_diffop (BINFO_OFFSET (base_binfo),
                                  vcall_offset);
       vcall_offset = fold (build1 (NOP_EXPR, vtable_entry_type,
@@ -7749,7 +7904,7 @@ add_vcall_offset_vtbl_entries_1 (binfo, vid)
 
       /* Keep track of the vtable index where this vcall offset can be
         found.  For a construction vtable, we already made this
-        annotation when we build the original vtable.  */
+        annotation when we built the original vtable.  */
       if (!vid->ctor_vtbl_p)
        BV_VCALL_INDEX (derived_virtuals) = vid->index;
 
@@ -7781,10 +7936,6 @@ build_rtti_vtbl_entries (binfo, vid)
   basetype = BINFO_TYPE (binfo);
   t = BINFO_TYPE (vid->rtti_binfo);
 
-  /* For a COM object there is no RTTI entry.  */
-  if (CLASSTYPE_COM_INTERFACE (basetype))
-    return;
-
   /* To find the complete object, we will first convert to our most
      primary base, and then add the offset in the vtbl to that value.  */
   b = binfo;
@@ -7813,16 +7964,12 @@ build_rtti_vtbl_entries (binfo, vid)
   vid->last_init = &TREE_CHAIN (*vid->last_init);
 
   /* Add the offset-to-top entry.  It comes earlier in the vtable that
-     the the typeinfo entry.  */
-  if (flag_vtable_thunks)
-    {
-      /* Convert the offset to look like a function pointer, so that
-        we can put it in the vtable.  */
-      init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
-      TREE_CONSTANT (init) = 1;
-      *vid->last_init = build_tree_list (NULL_TREE, init);
-      vid->last_init = &TREE_CHAIN (*vid->last_init);
-    }
+     the the typeinfo entry.  Convert the offset to look like a
+     function pointer, so that we can put it in the vtable.  */
+  init = build1 (NOP_EXPR, vfunc_ptr_type_node, offset);
+  TREE_CONSTANT (init) = 1;
+  *vid->last_init = build_tree_list (NULL_TREE, init);
+  vid->last_init = &TREE_CHAIN (*vid->last_init);
 }
 
 /* Build an entry in the virtual function table.  DELTA is the offset
@@ -7834,62 +7981,23 @@ build_rtti_vtbl_entries (binfo, vid)
    ABI.)  */
 
 static tree
-build_vtable_entry (delta, vcall_index, entry, generate_with_vtable_p)
+build_vtable_entry (delta, vcall_index, entry)
      tree delta;
      tree vcall_index;
      tree entry;
-     int generate_with_vtable_p;
 {
-  if (flag_vtable_thunks)
-    {
-      tree fn;
-
-      fn = TREE_OPERAND (entry, 0);
-      if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
-         && fn != abort_fndecl
-         && !DECL_TINFO_FN_P (fn))
-       {
-         entry = make_thunk (entry, delta, vcall_index,
-                             generate_with_vtable_p);
-         entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
-         TREE_READONLY (entry) = 1;
-         TREE_CONSTANT (entry) = 1;
-       }
-#ifdef GATHER_STATISTICS
-      n_vtable_entries += 1;
-#endif
-      return entry;
-    }
-  else
+  tree fn = TREE_OPERAND (entry, 0);
+  
+  if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
+      && fn != abort_fndecl)
     {
-      tree elems = tree_cons (NULL_TREE, delta,
-                             tree_cons (NULL_TREE, integer_zero_node,
-                                        build_tree_list (NULL_TREE, entry)));
-      tree entry = build (CONSTRUCTOR, vtable_entry_type, NULL_TREE, elems);
-
-      /* We don't use vcall offsets when not using vtable thunks.  */
-      my_friendly_assert (vcall_index == NULL_TREE, 20000125);
-
-      /* DELTA used to be constructed by `size_int' and/or size_binop,
-        which caused overflow problems when it was negative.  That should
-        be fixed now.  */
-
-      if (! int_fits_type_p (delta, delta_type_node))
-       {
-         if (flag_huge_objects)
-           sorry ("object size exceeds built-in limit for virtual function table implementation");
-         else
-           sorry ("object size exceeds normal limit for virtual function table implementation, recompile all source and use -fhuge-objects");
-       }
-      
-      TREE_CONSTANT (entry) = 1;
-      TREE_STATIC (entry) = 1;
+      entry = make_thunk (entry, delta, vcall_index);
+      entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
       TREE_READONLY (entry) = 1;
-
+      TREE_CONSTANT (entry) = 1;
+    }
 #ifdef GATHER_STATISTICS
-      n_vtable_entries += 1;
+  n_vtable_entries += 1;
 #endif
-
-      return entry;
-    }
+  return entry;
 }