]> oss.titaniummirror.com Git - msp430-gcc.git/blobdiff - gcc/cp/class.c
Imported gcc-4.4.3
[msp430-gcc.git] / gcc / cp / class.c
index ac174b3c275163d382e1e33bccf92d5a05a07f8e..2d41cd575a86b29ace6dfced68e48f0f71dace7d 100644 (file)
@@ -1,43 +1,42 @@
 /* Functions related to building classes and their related objects.
    Copyright (C) 1987, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2001, 2002  Free Software Foundation, Inc.
+   1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
+   Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
-This file is part of GNU CC.
+This file is part of GCC.
 
-GNU CC is free software; you can redistribute it and/or modify
+GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
-GNU CC is distributed in the hope that it will be useful,
+GCC is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GNU CC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 
 /* High-level class interface.  */
 
 #include "config.h"
 #include "system.h"
+#include "coretypes.h"
+#include "tm.h"
 #include "tree.h"
 #include "cp-tree.h"
 #include "flags.h"
 #include "rtl.h"
 #include "output.h"
 #include "toplev.h"
-#include "ggc.h"
-#include "lex.h"
 #include "target.h"
-
-#include "obstack.h"
-#define obstack_chunk_alloc xmalloc
-#define obstack_chunk_free free
+#include "convert.h"
+#include "cgraph.h"
+#include "tree-dump.h"
 
 /* The number of nested classes being processed.  If we are not in the
    scope of any class, this is zero.  */
@@ -61,6 +60,10 @@ typedef struct class_stack_node {
 
   /* If were defining TYPE, the names used in this class.  */
   splay_tree names_used;
+
+  /* Nonzero if this class is no longer open, because of a call to
+     push_to_top_level.  */
+  size_t hidden;
 }* class_stack_node_t;
 
 typedef struct vtbl_init_data_s
@@ -82,7 +85,7 @@ typedef struct vtbl_init_data_s
   tree vbase;
   /* The functions in vbase for which we have already provided vcall
      offsets.  */
-  varray_type fns;
+  VEC(tree,gc) *fns;
   /* The vtable index of the next vcall or vbase offset.  */
   tree index;
   /* Nonzero if we are building the initializer for the primary
@@ -91,135 +94,121 @@ typedef struct vtbl_init_data_s
   /* Nonzero if we are building the initializer for a construction
      vtable.  */
   int ctor_vtbl_p;
+  /* True when adding vcall offset entries to the vtable.  False when
+     merely computing the indices.  */
+  bool generate_vcall_entries;
 } vtbl_init_data;
 
 /* The type of a function passed to walk_subobject_offsets.  */
-typedef int (*subobject_offset_fn) PARAMS ((tree, tree, splay_tree));
+typedef int (*subobject_offset_fn) (tree, tree, splay_tree);
 
-/* The stack itself.  This is an dynamically resized array.  The
+/* The stack itself.  This is a dynamically resized array.  The
    number of elements allocated is CURRENT_CLASS_STACK_SIZE.  */
 static int current_class_stack_size;
 static class_stack_node_t current_class_stack;
 
+/* The size of the largest empty class seen in this translation unit.  */
+static GTY (()) tree sizeof_biggest_empty_class;
+
 /* An array of all local classes present in this translation unit, in
    declaration order.  */
-varray_type local_classes;
-
-static tree get_vfield_name PARAMS ((tree));
-static void finish_struct_anon PARAMS ((tree));
-static tree build_vtable_entry PARAMS ((tree, tree, tree));
-static tree get_vtable_name PARAMS ((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 void finish_vtbls PARAMS ((tree));
-static void modify_vtable_entry PARAMS ((tree, tree, tree, 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 check_for_override PARAMS ((tree, tree));
-static tree dfs_modify_vtables PARAMS ((tree, void *));
-static tree modify_all_vtables PARAMS ((tree, int *, tree));
-static void determine_primary_base PARAMS ((tree, int *));
-static void finish_struct_methods PARAMS ((tree));
-static void maybe_warn_about_overly_private_class PARAMS ((tree));
-static int field_decl_cmp PARAMS ((const tree *, const tree *));
-static int method_name_cmp PARAMS ((const tree *, const tree *));
-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 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));
-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 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 *));
-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, 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 *));
-static void add_vcall_offset_vtbl_entries_r PARAMS ((tree, vtbl_init_data *));
-static void add_vcall_offset_vtbl_entries_1 PARAMS ((tree, vtbl_init_data *));
-static void build_vcall_offset_vtbl_entries PARAMS ((tree, vtbl_init_data *));
-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 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, tree));
-static unsigned HOST_WIDE_INT end_of_class PARAMS ((tree, int));
-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));
-static void set_vindex PARAMS ((tree, int *));
-static void build_rtti_vtbl_entries PARAMS ((tree, vtbl_init_data *));
-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));
-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));
-static tree dfs_fixup_binfo_vtbls PARAMS ((tree, void *));
-static tree get_original_base PARAMS ((tree, tree));
-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, 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)
+VEC(tree,gc) *local_classes;
+
+static tree get_vfield_name (tree);
+static void finish_struct_anon (tree);
+static tree get_vtable_name (tree);
+static tree get_basefndecls (tree, tree);
+static int build_primary_vtable (tree, tree);
+static int build_secondary_vtable (tree);
+static void finish_vtbls (tree);
+static void modify_vtable_entry (tree, tree, tree, tree, tree *);
+static void finish_struct_bits (tree);
+static int alter_access (tree, tree, tree);
+static void handle_using_decl (tree, tree);
+static tree dfs_modify_vtables (tree, void *);
+static tree modify_all_vtables (tree, tree);
+static void determine_primary_bases (tree);
+static void finish_struct_methods (tree);
+static void maybe_warn_about_overly_private_class (tree);
+static int method_name_cmp (const void *, const void *);
+static int resort_method_name_cmp (const void *, const void *);
+static void add_implicitly_declared_members (tree, int, int);
+static tree fixed_type_or_null (tree, int *, int *);
+static tree build_simple_base_path (tree expr, tree binfo);
+static tree build_vtbl_ref_1 (tree, tree);
+static tree build_vtbl_initializer (tree, tree, tree, tree, int *);
+static int count_fields (tree);
+static int add_fields_to_record_type (tree, struct sorted_fields_type*, int);
+static bool check_bitfield_decl (tree);
+static void check_field_decl (tree, tree, int *, int *, int *);
+static void check_field_decls (tree, tree *, int *, int *);
+static tree *build_base_field (record_layout_info, tree, splay_tree, tree *);
+static void build_base_fields (record_layout_info, splay_tree, tree *);
+static void check_methods (tree);
+static void remove_zero_width_bit_fields (tree);
+static void check_bases (tree, int *, int *);
+static void check_bases_and_members (tree);
+static tree create_vtable_ptr (tree, tree *);
+static void include_empty_classes (record_layout_info);
+static void layout_class_type (tree, tree *);
+static void fixup_pending_inline (tree);
+static void fixup_inline_methods (tree);
+static void propagate_binfo_offsets (tree, tree);
+static void layout_virtual_bases (record_layout_info, splay_tree);
+static void build_vbase_offset_vtbl_entries (tree, vtbl_init_data *);
+static void add_vcall_offset_vtbl_entries_r (tree, vtbl_init_data *);
+static void add_vcall_offset_vtbl_entries_1 (tree, vtbl_init_data *);
+static void build_vcall_offset_vtbl_entries (tree, vtbl_init_data *);
+static void add_vcall_offset (tree, tree, vtbl_init_data *);
+static void layout_vtable_decl (tree, int);
+static tree dfs_find_final_overrider_pre (tree, void *);
+static tree dfs_find_final_overrider_post (tree, void *);
+static tree find_final_overrider (tree, tree, tree);
+static int make_new_vtable (tree, tree);
+static tree get_primary_binfo (tree);
+static int maybe_indent_hierarchy (FILE *, int, int);
+static tree dump_class_hierarchy_r (FILE *, int, tree, tree, int);
+static void dump_class_hierarchy (tree);
+static void dump_class_hierarchy_1 (FILE *, int, tree);
+static void dump_array (FILE *, tree);
+static void dump_vtable (tree, tree, tree);
+static void dump_vtt (tree, tree);
+static void dump_thunk (FILE *, int, tree);
+static tree build_vtable (tree, tree, tree);
+static void initialize_vtable (tree, tree);
+static void layout_nonempty_base_or_field (record_layout_info,
+                                          tree, tree, splay_tree);
+static tree end_of_class (tree, int);
+static bool layout_empty_base (record_layout_info, tree, tree, splay_tree);
+static void accumulate_vtbl_inits (tree, tree, tree, tree, tree);
+static tree dfs_accumulate_vtbl_inits (tree, tree, tree, tree,
+                                              tree);
+static void build_rtti_vtbl_entries (tree, vtbl_init_data *);
+static void build_vcall_and_vbase_vtbl_entries (tree, vtbl_init_data *);
+static void clone_constructors_and_destructors (tree);
+static tree build_clone (tree, tree);
+static void update_vtable_entry_for_fn (tree, tree, tree, tree *, unsigned);
+static void build_ctor_vtbl_group (tree, tree);
+static void build_vtt (tree);
+static tree binfo_ctor_vtable (tree);
+static tree *build_vtt_inits (tree, tree, tree *, tree *);
+static tree dfs_build_secondary_vptr_vtt_inits (tree, void *);
+static tree dfs_fixup_binfo_vtbls (tree, void *);
+static int record_subobject_offset (tree, tree, splay_tree);
+static int check_subobject_offset (tree, tree, splay_tree);
+static int walk_subobject_offsets (tree, subobject_offset_fn,
+                                  tree, splay_tree, tree, int);
+static void record_subobject_offsets (tree, tree, splay_tree, bool);
+static int layout_conflict_p (tree, tree, splay_tree, int);
+static int splay_tree_compare_integer_csts (splay_tree_key k1,
+                                           splay_tree_key k2);
+static void warn_about_ambiguous_bases (tree);
+static bool type_requires_array_cookie (tree);
+static bool contains_empty_class_p (tree);
+static bool base_derived_from (tree, tree);
+static int empty_base_at_nonzero_offset_p (tree, tree, splay_tree);
+static tree end_of_base (tree);
+static tree get_vcall_index (tree, tree);
 
 /* Variables shared between class.c and call.c.  */
 
@@ -230,7 +219,6 @@ int n_vtable_searches = 0;
 int n_vtable_elems = 0;
 int n_convert_harshness = 0;
 int n_compute_conversion_costs = 0;
-int n_build_method_call = 0;
 int n_inner_fields_searched = 0;
 #endif
 
@@ -245,11 +233,10 @@ int n_inner_fields_searched = 0;
    from EXPR.  */
 
 tree
-build_base_path (code, expr, binfo, nonnull)
-     enum tree_code code;
-     tree expr;
-     tree binfo;
-     int nonnull;
+build_base_path (enum tree_code code,
+                tree expr,
+                tree binfo,
+                int nonnull)
 {
   tree v_binfo = NULL_TREE;
   tree d_binfo = NULL_TREE;
@@ -260,6 +247,8 @@ build_base_path (code, expr, binfo, nonnull)
   tree ptr_target_type;
   int fixed_type_p;
   int want_pointer = TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE;
+  bool has_empty = false;
+  bool virtual_access;
 
   if (expr == error_mark_node || binfo == error_mark_node || !binfo)
     return error_mark_node;
@@ -267,129 +256,343 @@ build_base_path (code, expr, binfo, nonnull)
   for (probe = binfo; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
     {
       d_binfo = probe;
-      if (!v_binfo && TREE_VIA_VIRTUAL (probe))
+      if (is_empty_class (BINFO_TYPE (probe)))
+       has_empty = true;
+      if (!v_binfo && BINFO_VIRTUAL_P (probe))
        v_binfo = probe;
     }
 
   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);
-  
+
+  gcc_assert ((code == MINUS_EXPR
+              && SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), probe))
+             || (code == PLUS_EXPR
+                 && SAME_BINFO_TYPE_P (BINFO_TYPE (d_binfo), probe)));
+
+  if (binfo == d_binfo)
+    /* Nothing to do.  */
+    return expr;
+
   if (code == MINUS_EXPR && v_binfo)
     {
-      error ("cannot convert from base `%T' to derived type `%T' via virtual base `%T'",
+      error ("cannot convert from base %qT to derived type %qT via virtual base %qT",
             BINFO_TYPE (binfo), BINFO_TYPE (d_binfo), BINFO_TYPE (v_binfo));
       return error_mark_node;
     }
 
+  if (!want_pointer)
+    /* This must happen before the call to save_expr.  */
+    expr = cp_build_unary_op (ADDR_EXPR, expr, 0, tf_warning_or_error);
+
+  offset = BINFO_OFFSET (binfo);
   fixed_type_p = resolves_to_fixed_type_p (expr, &nonnull);
-  if (fixed_type_p <= 0 && TREE_SIDE_EFFECTS (expr))
+  target_type = code == PLUS_EXPR ? BINFO_TYPE (binfo) : BINFO_TYPE (d_binfo);
+
+  /* Do we need to look in the vtable for the real offset?  */
+  virtual_access = (v_binfo && fixed_type_p <= 0);
+
+  /* Don't bother with the calculations inside sizeof; they'll ICE if the
+     source type is incomplete and the pointer value doesn't matter.  */
+  if (skip_evaluation)
+    {
+      expr = build_nop (build_pointer_type (target_type), expr);
+      if (!want_pointer)
+       expr = build_indirect_ref (EXPR_LOCATION (expr), expr, NULL);
+      return expr;
+    }
+
+  /* Do we need to check for a null pointer?  */
+  if (want_pointer && !nonnull)
+    {
+      /* If we know the conversion will not actually change the value
+        of EXPR, then we can avoid testing the expression for NULL.
+        We have to avoid generating a COMPONENT_REF for a base class
+        field, because other parts of the compiler know that such
+        expressions are always non-NULL.  */
+      if (!virtual_access && integer_zerop (offset))
+       {
+         tree class_type;
+         /* TARGET_TYPE has been extracted from BINFO, and, is
+            therefore always cv-unqualified.  Extract the
+            cv-qualifiers from EXPR so that the expression returned
+            matches the input.  */
+         class_type = TREE_TYPE (TREE_TYPE (expr));
+         target_type
+           = cp_build_qualified_type (target_type,
+                                      cp_type_quals (class_type));
+         return build_nop (build_pointer_type (target_type), expr);
+       }
+      null_test = error_mark_node;
+    }
+
+  /* Protect against multiple evaluation if necessary.  */
+  if (TREE_SIDE_EFFECTS (expr) && (null_test || virtual_access))
     expr = save_expr (expr);
 
-  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)
+  /* Now that we've saved expr, build the real null test.  */
+  if (null_test)
+    {
+      tree zero = cp_convert (TREE_TYPE (expr), integer_zero_node);
+      null_test = fold_build2 (NE_EXPR, boolean_type_node,
+                              expr, zero);
+    }
+
+  /* If this is a simple base reference, express it as a COMPONENT_REF.  */
+  if (code == PLUS_EXPR && !virtual_access
+      /* We don't build base fields for empty bases, and they aren't very
+        interesting to the optimizers anyway.  */
+      && !has_empty)
+    {
+      expr = cp_build_indirect_ref (expr, NULL, tf_warning_or_error);
+      expr = build_simple_base_path (expr, binfo);
+      if (want_pointer)
+       expr = build_address (expr);
+      target_type = TREE_TYPE (expr);
+      goto out;
+    }
+
+  if (virtual_access)
     {
       /* 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, 
+        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;
+
+      if (fixed_type_p < 0 && in_base_initializer)
+       {
+         /* In a base member initializer, we cannot rely on the
+            vtable being set up.  We have to indirect via the
+            vtt_parm.  */
+         tree t;
+
+         t = TREE_TYPE (TYPE_VFIELD (current_class_type));
+         t = build_pointer_type (t);
+         v_offset = convert (t, current_vtt_parm);
+         v_offset = cp_build_indirect_ref (v_offset, NULL, 
+                                            tf_warning_or_error);
+       }
+      else
+       v_offset = build_vfield_ref (cp_build_indirect_ref (expr, NULL,
+                                                            tf_warning_or_error),
+                                    TREE_TYPE (TREE_TYPE (expr)));
+
+      v_offset = build2 (POINTER_PLUS_EXPR, TREE_TYPE (v_offset),
+                        v_offset, fold_convert (sizetype, 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);
+      v_offset = cp_build_indirect_ref (v_offset, NULL, tf_warning_or_error);
       TREE_CONSTANT (v_offset) = 1;
 
-      offset = cp_convert (ptrdiff_type_node,
-                          size_diffop (offset, BINFO_OFFSET (v_binfo)));
+      offset = convert_to_integer (ptrdiff_type_node,
+                                  size_diffop (offset,
+                                               BINFO_OFFSET (v_binfo)));
 
       if (!integer_zerop (offset))
-       v_offset = build (code, ptrdiff_type_node, v_offset, offset);
+       v_offset = build2 (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));
+       offset = build3 (COND_EXPR, ptrdiff_type_node,
+                        build2 (EQ_EXPR, boolean_type_node,
+                                current_in_charge_parm, integer_zero_node),
+                        v_offset,
+                        convert_to_integer (ptrdiff_type_node,
+                                            BINFO_OFFSET (binfo)));
       else
        offset = v_offset;
     }
 
-  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);
+    {
+      offset = fold_convert (sizetype, offset);
+      if (code == MINUS_EXPR)
+       offset = fold_build1 (NEGATE_EXPR, sizetype, offset);
+      expr = build2 (POINTER_PLUS_EXPR, ptr_target_type, expr, offset);
+    }
   else
     null_test = NULL;
-  
+
   if (!want_pointer)
-    expr = build_indirect_ref (expr, NULL);
+    expr = cp_build_indirect_ref (expr, NULL, tf_warning_or_error);
 
+ out:
   if (null_test)
-    expr = build (COND_EXPR, target_type, null_test,
-                 build1 (NOP_EXPR, target_type, integer_zero_node),
-                 expr);
+    expr = fold_build3 (COND_EXPR, target_type, null_test, expr,
+                       fold_build1 (NOP_EXPR, target_type,
+                                    integer_zero_node));
 
   return expr;
 }
 
-\f
-/* Virtual function things.  */
+/* Subroutine of build_base_path; EXPR and BINFO are as in that function.
+   Perform a derived-to-base conversion by recursively building up a
+   sequence of COMPONENT_REFs to the appropriate base fields.  */
 
 static tree
-build_vtable_entry_ref (array_ref, instance, idx)
-     tree array_ref, instance, idx;
+build_simple_base_path (tree expr, tree binfo)
+{
+  tree type = BINFO_TYPE (binfo);
+  tree d_binfo = BINFO_INHERITANCE_CHAIN (binfo);
+  tree field;
+
+  if (d_binfo == NULL_TREE)
+    {
+      tree temp;
+
+      gcc_assert (TYPE_MAIN_VARIANT (TREE_TYPE (expr)) == type);
+
+      /* Transform `(a, b).x' into `(*(a, &b)).x', `(a ? b : c).x'
+        into `(*(a ?  &b : &c)).x', and so on.  A COND_EXPR is only
+        an lvalue in the front end; only _DECLs and _REFs are lvalues
+        in the back end.  */
+      temp = unary_complex_lvalue (ADDR_EXPR, expr);
+      if (temp)
+       expr = cp_build_indirect_ref (temp, NULL, tf_warning_or_error);
+
+      return expr;
+    }
+
+  /* Recurse.  */
+  expr = build_simple_base_path (expr, d_binfo);
+
+  for (field = TYPE_FIELDS (BINFO_TYPE (d_binfo));
+       field; field = TREE_CHAIN (field))
+    /* Is this the base field created by build_base_field?  */
+    if (TREE_CODE (field) == FIELD_DECL
+       && DECL_FIELD_IS_BASE (field)
+       && TREE_TYPE (field) == type)
+      {
+       /* We don't use build_class_member_access_expr here, as that
+          has unnecessary checks, and more importantly results in
+          recursive calls to dfs_walk_once.  */
+       int type_quals = cp_type_quals (TREE_TYPE (expr));
+
+       expr = build3 (COMPONENT_REF,
+                      cp_build_qualified_type (type, type_quals),
+                      expr, field, NULL_TREE);
+       expr = fold_if_not_in_template (expr);
+
+       /* Mark the expression const or volatile, as appropriate.
+          Even though we've dealt with the type above, we still have
+          to mark the expression itself.  */
+       if (type_quals & TYPE_QUAL_CONST)
+         TREE_READONLY (expr) = 1;
+       if (type_quals & TYPE_QUAL_VOLATILE)
+         TREE_THIS_VOLATILE (expr) = 1;
+
+       return expr;
+      }
+
+  /* Didn't find the base field?!?  */
+  gcc_unreachable ();
+}
+
+/* Convert OBJECT to the base TYPE.  OBJECT is an expression whose
+   type is a class type or a pointer to a class type.  In the former
+   case, TYPE is also a class type; in the latter it is another
+   pointer type.  If CHECK_ACCESS is true, an error message is emitted
+   if TYPE is inaccessible.  If OBJECT has pointer type, the value is
+   assumed to be non-NULL.  */
+
+tree
+convert_to_base (tree object, tree type, bool check_access, bool nonnull)
+{
+  tree binfo;
+  tree object_type;
+
+  if (TYPE_PTR_P (TREE_TYPE (object)))
+    {
+      object_type = TREE_TYPE (TREE_TYPE (object));
+      type = TREE_TYPE (type);
+    }
+  else
+    object_type = TREE_TYPE (object);
+
+  binfo = lookup_base (object_type, type,
+                      check_access ? ba_check : ba_unique,
+                      NULL);
+  if (!binfo || binfo == error_mark_node)
+    return error_mark_node;
+
+  return build_base_path (PLUS_EXPR, object, binfo, nonnull);
+}
+
+/* EXPR is an expression with unqualified class type.  BASE is a base
+   binfo of that class type.  Returns EXPR, converted to the BASE
+   type.  This function assumes that EXPR is the most derived class;
+   therefore virtual bases can be found at their static offsets.  */
+
+tree
+convert_to_base_statically (tree expr, tree base)
 {
-  tree i, i2, vtable, first_fn, basetype;
+  tree expr_type;
+
+  expr_type = TREE_TYPE (expr);
+  if (!SAME_BINFO_TYPE_P (BINFO_TYPE (base), expr_type))
+    {
+      tree pointer_type;
+
+      pointer_type = build_pointer_type (expr_type);
+
+      /* We use fold_build2 and fold_convert below to simplify the trees
+        provided to the optimizers.  It is not safe to call these functions
+        when processing a template because they do not handle C++-specific
+        trees.  */
+      gcc_assert (!processing_template_decl);
+      expr = cp_build_unary_op (ADDR_EXPR, expr, /*noconvert=*/1, 
+                             tf_warning_or_error);
+      if (!integer_zerop (BINFO_OFFSET (base)))
+        expr = fold_build2 (POINTER_PLUS_EXPR, pointer_type, expr,
+                           fold_convert (sizetype, BINFO_OFFSET (base)));
+      expr = fold_convert (build_pointer_type (BINFO_TYPE (base)), expr);
+      expr = build_fold_indirect_ref (expr);
+    }
+
+  return expr;
+}
 
-  basetype = TREE_TYPE (instance);
-  if (TREE_CODE (basetype) == REFERENCE_TYPE)
-    basetype = TREE_TYPE (basetype);
+\f
+tree
+build_vfield_ref (tree datum, tree type)
+{
+  tree vfield, vcontext;
 
-  vtable = get_vtbl_decl_for_binfo (TYPE_BINFO (basetype));
-  first_fn = TYPE_BINFO_VTABLE (basetype);
+  if (datum == error_mark_node)
+    return error_mark_node;
 
-  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));
+  /* First, convert to the requested type.  */
+  if (!same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (datum), type))
+    datum = convert_to_base (datum, type, /*check_access=*/false,
+                            /*nonnull=*/true);
 
-  if (TREE_CODE (i) != INTEGER_CST)
-    abort ();
+  /* Second, the requested type may not be the owner of its own vptr.
+     If not, convert to the base class that owns it.  We cannot use
+     convert_to_base here, because VCONTEXT may appear more than once
+     in the inheritance hierarchy of TYPE, and thus direct conversion
+     between the types may be ambiguous.  Following the path back up
+     one step at a time via primary bases avoids the problem.  */
+  vfield = TYPE_VFIELD (type);
+  vcontext = DECL_CONTEXT (vfield);
+  while (!same_type_ignoring_top_level_qualifiers_p (vcontext, type))
+    {
+      datum = build_simple_base_path (datum, CLASSTYPE_PRIMARY_BINFO (type));
+      type = TREE_TYPE (datum);
+    }
 
-  return build (VTABLE_REF, TREE_TYPE (array_ref), array_ref, vtable, i);
+  return build3 (COMPONENT_REF, TREE_TYPE (vfield), datum, vfield, NULL_TREE);
 }
 
 /* Given an object INSTANCE, return an expression which yields the
@@ -398,8 +601,7 @@ build_vtable_entry_ref (array_ref, instance, idx)
    creating extra tree nodes when we don't have to.  */
 
 static tree
-build_vtbl_ref_1 (instance, idx)
-     tree instance, idx;
+build_vtbl_ref_1 (tree instance, tree idx)
 {
   tree aref;
   tree vtbl = NULL_TREE;
@@ -410,60 +612,56 @@ build_vtbl_ref_1 (instance, idx)
   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);
+  tree basetype = non_reference (TREE_TYPE (instance));
 
   if (fixed_type && !cdtorp)
     {
       tree binfo = lookup_base (fixed_type, basetype,
-                               ba_ignore|ba_quiet, NULL);
+                               ba_unique | ba_quiet, NULL);
       if (binfo)
-       vtbl = BINFO_VTABLE (binfo);
+       vtbl = unshare_expr (BINFO_VTABLE (binfo));
     }
 
   if (!vtbl)
-    {
-      vtbl = build_vfield_ref (instance, basetype);
-    }
+    vtbl = build_vfield_ref (instance, basetype);
 
   assemble_external (vtbl);
 
-  aref = build_array_ref (vtbl, idx);
-  TREE_CONSTANT (aref) = 1;
+  aref = build_array_ref (vtbl, idx, input_location);
+  TREE_CONSTANT (aref) |= TREE_CONSTANT (vtbl) && TREE_CONSTANT (idx);
 
   return aref;
 }
 
 tree
-build_vtbl_ref (instance, idx)
-     tree instance, idx;
+build_vtbl_ref (tree instance, tree idx)
 {
   tree aref = build_vtbl_ref_1 (instance, idx);
 
-  if (flag_vtable_gc)
-    aref = build_vtable_entry_ref (aref, instance, idx);
-
   return aref;
 }
 
-/* Given an object INSTANCE, return an expression which yields a
-   function pointer corresponding to vtable element INDEX.  */
+/* Given a stable object pointer INSTANCE_PTR, return an expression which
+   yields a function pointer corresponding to vtable element INDEX.  */
 
 tree
-build_vfn_ref (instance, idx)
-     tree instance, idx;
+build_vfn_ref (tree instance_ptr, tree idx)
 {
-  tree aref = build_vtbl_ref_1 (instance, idx);
+  tree aref;
+
+  aref = build_vtbl_ref_1 (cp_build_indirect_ref (instance_ptr, 0,
+                                                  tf_warning_or_error), 
+                           idx);
 
   /* 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));
+                  cp_build_unary_op (ADDR_EXPR, aref, /*noconvert=*/1,
+                                   tf_warning_or_error));
 
-  if (flag_vtable_gc)
-    aref = build_vtable_entry_ref (aref, instance, idx);
+  /* Remember this as a method reference, for later devirtualization.  */
+  aref = build3 (OBJ_TYPE_REF, TREE_TYPE (aref), aref, instance_ptr, idx);
 
   return aref;
 }
@@ -472,20 +670,35 @@ build_vfn_ref (instance, idx)
    for the given TYPE.  */
 
 static tree
-get_vtable_name (type)
-     tree type;
+get_vtable_name (tree type)
 {
   return mangle_vtbl_for_type (type);
 }
 
-/* Return an IDENTIFIER_NODE for the name of the virtual table table
-   for TYPE.  */
+/* DECL is an entity associated with TYPE, like a virtual table or an
+   implicitly generated constructor.  Determine whether or not DECL
+   should have external or internal linkage at the object file
+   level.  This routine does not deal with COMDAT linkage and other
+   similar complexities; it simply sets TREE_PUBLIC if it possible for
+   entities in other translation units to contain copies of DECL, in
+   the abstract.  */
 
-tree
-get_vtt_name (type)
-     tree type;
+void
+set_linkage_according_to_type (tree type, tree decl)
 {
-  return mangle_vtt_for_type (type);
+  /* If TYPE involves a local class in a function with internal
+     linkage, then DECL should have internal linkage too.  Other local
+     classes have no linkage -- but if their containing functions
+     have external linkage, it makes sense for DECL to have external
+     linkage too.  That will allow template definitions to be merged,
+     for example.  */
+  if (no_linkage_check (type, /*relaxed_p=*/true))
+    {
+      TREE_PUBLIC (decl) = 0;
+      DECL_INTERFACE_KNOWN (decl) = 1;
+    }
+  else
+    TREE_PUBLIC (decl) = 1;
 }
 
 /* Create a VAR_DECL for a primary or secondary vtable for CLASS_TYPE.
@@ -493,10 +706,7 @@ get_vtt_name (type)
    Use NAME for the name of the vtable, and VTABLE_TYPE for its type.  */
 
 static tree
-build_vtable (class_type, name, vtable_type)
-     tree class_type;
-     tree name;
-     tree vtable_type;
+build_vtable (tree class_type, tree name, tree vtable_type)
 {
   tree decl;
 
@@ -509,102 +719,97 @@ build_vtable (class_type, name, vtable_type)
   TREE_STATIC (decl) = 1;
   TREE_READONLY (decl) = 1;
   DECL_VIRTUAL_P (decl) = 1;
-  import_export_vtable (decl, class_type, 0);
+  DECL_ALIGN (decl) = TARGET_VTABLE_ENTRY_ALIGN;
+  DECL_VTABLE_OR_VTT_P (decl) = 1;
+  /* At one time the vtable info was grabbed 2 words at a time.  This
+     fails on sparc unless you have 8-byte alignment.  (tiemann) */
+  DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
+                          DECL_ALIGN (decl));
+  set_linkage_according_to_type (class_type, decl);
+  /* The vtable has not been defined -- yet.  */
+  DECL_EXTERNAL (decl) = 1;
+  DECL_NOT_REALLY_EXTERN (decl) = 1;
+
+  /* Mark the VAR_DECL node representing the vtable itself as a
+     "gratuitous" one, thereby forcing dwarfout.c to ignore it.  It
+     is rather important that such things be ignored because any
+     effort to actually generate DWARF for them will run into
+     trouble when/if we encounter code like:
+
+     #pragma interface
+     struct S { virtual void member (); };
+
+     because the artificial declaration of the vtable itself (as
+     manufactured by the g++ front end) will say that the vtable is
+     a static member of `S' but only *after* the debug output for
+     the definition of `S' has already been output.  This causes
+     grief because the DWARF entry for the definition of the vtable
+     will try to refer back to an earlier *declaration* of the
+     vtable as a static member of `S' and there won't be one.  We
+     might be able to arrange to have the "vtable static member"
+     attached to the member list for `S' before the debug info for
+     `S' get written (which would solve the problem) but that would
+     require more intrusive changes to the g++ front end.  */
+  DECL_IGNORED_P (decl) = 1;
 
   return decl;
 }
 
 /* Get the VAR_DECL of the vtable for TYPE. TYPE need not be polymorphic,
    or even complete.  If this does not exist, create it.  If COMPLETE is
-   non-zero, then complete the definition of it -- that will render it
+   nonzero, then complete the definition of it -- that will render it
    impossible to actually build the vtable, but is useful to get at those
    which are known to exist in the runtime.  */
 
-tree 
-get_vtable_decl (type, complete)
-     tree type;
-     int complete;
+tree
+get_vtable_decl (tree type, int complete)
 {
-  tree name = get_vtable_name (type);
-  tree decl = IDENTIFIER_GLOBAL_VALUE (name);
-  
-  if (decl)
-    {
-      my_friendly_assert (TREE_CODE (decl) == VAR_DECL
-                          && DECL_VIRTUAL_P (decl), 20000118);
-      return decl;
-    }
-  
-  decl = build_vtable (type, name, void_type_node);
-  decl = pushdecl_top_level (decl);
-  my_friendly_assert (IDENTIFIER_GLOBAL_VALUE (name) == decl,
-                     20000517);
-  
-  /* At one time the vtable info was grabbed 2 words at a time.  This
-     fails on sparc unless you have 8-byte alignment.  (tiemann) */
-  DECL_ALIGN (decl) = MAX (TYPE_ALIGN (double_type_node),
-                          DECL_ALIGN (decl));
+  tree decl;
+
+  if (CLASSTYPE_VTABLES (type))
+    return CLASSTYPE_VTABLES (type);
+
+  decl = build_vtable (type, get_vtable_name (type), vtbl_type_node);
+  CLASSTYPE_VTABLES (type) = decl;
 
   if (complete)
     {
       DECL_EXTERNAL (decl) = 1;
-      cp_finish_decl (decl, NULL_TREE, NULL_TREE, 0);
+      finish_decl (decl, NULL_TREE, NULL_TREE);
     }
 
   return decl;
 }
 
-/* Returns a copy of the BINFO_VIRTUALS list in BINFO.  The
-   BV_VCALL_INDEX for each entry is cleared.  */
-
-static tree
-copy_virtuals (binfo)
-     tree binfo;
-{
-  tree copies;
-  tree t;
-
-  copies = copy_list (BINFO_VIRTUALS (binfo));
-  for (t = copies; t; t = TREE_CHAIN (t))
-    {
-      BV_VCALL_INDEX (t) = NULL_TREE;
-      BV_USE_VCALL_INDEX_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
-   list.  Returns a non-zero value if a new vtable is actually
+   list.  Returns a nonzero value if a new vtable is actually
    created.  */
 
 static int
-build_primary_vtable (binfo, type)
-     tree binfo, type;
+build_primary_vtable (tree binfo, tree type)
 {
   tree decl;
   tree virtuals;
 
   decl = get_vtable_decl (type, /*complete=*/0);
-  
+
   if (binfo)
     {
-      if (BINFO_NEW_VTABLE_MARKED (binfo, type))
+      if (BINFO_NEW_VTABLE_MARKED (binfo))
        /* We have already created a vtable for this base, so there's
           no need to do it again.  */
        return 0;
-      
-      virtuals = copy_virtuals (binfo);
+
+      virtuals = copy_list (BINFO_VIRTUALS (binfo));
       TREE_TYPE (decl) = TREE_TYPE (get_vtbl_decl_for_binfo (binfo));
       DECL_SIZE (decl) = TYPE_SIZE (TREE_TYPE (decl));
       DECL_SIZE_UNIT (decl) = TYPE_SIZE_UNIT (TREE_TYPE (decl));
     }
   else
     {
-      my_friendly_assert (TREE_CODE (TREE_TYPE (decl)) == VOID_TYPE,
-                          20000118);
+      gcc_assert (TREE_TYPE (decl) == vtbl_type_node);
       virtuals = NULL_TREE;
     }
 
@@ -615,9 +820,9 @@ build_primary_vtable (binfo, type)
 
   /* Initialize the association list for this type, based
      on our first approximation.  */
-  TYPE_BINFO_VTABLE (type) = decl;
-  TYPE_BINFO_VIRTUALS (type) = virtuals;
-  SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type), type);
+  BINFO_VTABLE (TYPE_BINFO (type)) = decl;
+  BINFO_VIRTUALS (TYPE_BINFO (type)) = virtuals;
+  SET_BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (type));
   return 1;
 }
 
@@ -629,29 +834,26 @@ build_primary_vtable (binfo, type)
    FOR_TYPE is the most derived type which caused this table to
    be needed.
 
-   Returns non-zero if we haven't met BINFO before.
+   Returns nonzero 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
    can result.  */
 
 static int
-build_secondary_vtable (binfo, for_type)
-     tree binfo, for_type;
+build_secondary_vtable (tree binfo)
 {
-  my_friendly_assert (binfo == CANONICAL_BINFO (binfo, for_type), 20010605);
-
-  if (BINFO_NEW_VTABLE_MARKED (binfo, for_type))
+  if (BINFO_NEW_VTABLE_MARKED (binfo))
     /* 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, for_type);
-  
+  SET_BINFO_NEW_VTABLE_MARKED (binfo);
+
   /* Make fresh virtual list, so we can smash it later.  */
-  BINFO_VIRTUALS (binfo) = copy_virtuals (binfo);
+  BINFO_VIRTUALS (binfo) = copy_list (BINFO_VIRTUALS (binfo));
 
   /* Secondary vtables are laid out as part of the same structure as
      the primary vtable.  */
@@ -660,27 +862,22 @@ build_secondary_vtable (binfo, for_type)
 }
 
 /* Create a new vtable for BINFO which is the hierarchy dominated by
-   T. Return non-zero if we actually created a new vtable.  */
+   T. Return nonzero if we actually created a new vtable.  */
 
 static int
-make_new_vtable (t, binfo)
-     tree t;
-     tree binfo;
+make_new_vtable (tree t, tree binfo)
 {
   if (binfo == TYPE_BINFO (t))
     /* In this case, it is *type*'s vtable we are modifying.  We start
        with the approximation that its vtable is that of the
        immediate base class.  */
-    /* ??? 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);
+    return build_primary_vtable (binfo, t);
   else
     /* This is our very own copy of `basetype' to play with.  Later,
        we will fill in all the virtual functions that override the
        virtual functions in these base classes which are not defined
        by the current type.  */
-    return build_secondary_vtable (binfo, t);
+    return build_secondary_vtable (binfo);
 }
 
 /* Make *VIRTUALS, an entry on the BINFO_VIRTUALS list for BINFO
@@ -690,12 +887,11 @@ make_new_vtable (t, binfo)
    the function is actually called.  */
 
 static void
-modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
-     tree t;
-     tree binfo;
-     tree fndecl;
-     tree delta;
-     tree *virtuals;
+modify_vtable_entry (tree t,
+                    tree binfo,
+                    tree fndecl,
+                    tree delta,
+                    tree *virtuals)
 {
   tree v;
 
@@ -704,8 +900,6 @@ modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
   if (fndecl != BV_FN (v)
       || !tree_int_cst_equal (delta, BV_DELTA (v)))
     {
-      tree base_fndecl;
-
       /* We need a new vtable for BINFO.  */
       if (make_new_vtable (t, binfo))
        {
@@ -718,362 +912,237 @@ modify_vtable_entry (t, binfo, fndecl, delta, virtuals)
          v = *virtuals;
        }
 
-      base_fndecl = BV_FN (v);
       BV_DELTA (v) = delta;
       BV_VCALL_INDEX (v) = NULL_TREE;
       BV_FN (v) = fndecl;
-
-      /* Now assign virtual dispatch information, if unset.  We can
-        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);
-         DECL_VIRTUAL_CONTEXT (fndecl) = DECL_VIRTUAL_CONTEXT (base_fndecl);
-       }
     }
 }
 
-/* Set DECL_VINDEX for DECL.  VINDEX_P is the number of virtual
-   functions present in the vtable so far.  */
-
-static void
-set_vindex (decl, vfuns_p)
-     tree decl;
-     int *vfuns_p;
-{
-  int vindex;
-
-  vindex = *vfuns_p;
-  *vfuns_p += (TARGET_VTABLE_USES_DESCRIPTORS
-              ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
-  DECL_VINDEX (decl) = build_shared_int_cst (vindex);
-}
 \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
-   declaration for it was seen.  (Even though that is erroneous, we
-   add the method for improved error recovery.)  */
+/* Add method METHOD to class TYPE.  If USING_DECL is non-null, it is
+   the USING_DECL naming METHOD.  Returns true if the method could be
+   added to the method vec.  */
+
+bool
+add_method (tree type, tree method, tree using_decl)
+{
+  unsigned slot;
+  tree overload;
+  bool template_conv_p = false;
+  bool conv_p;
+  VEC(tree,gc) *method_vec;
+  bool complete_p;
+  bool insert_p = false;
+  tree current_fns;
+  tree fns;
 
-void
-add_method (type, method, error_p)
-     tree type;
-     tree method;
-     int error_p;
-{
-  int using = (DECL_CONTEXT (method) != type);
-  int len;
-  int slot;
-  tree method_vec;
-
-  if (!CLASSTYPE_METHOD_VEC (type))
-    /* Make a new method vector.  We start with 8 entries.  We must
-       allocate at least two (for constructors and destructors), and
-       we're going to end up with an assignment operator at some point
-       as well.
-       
-       We could use a TREE_LIST for now, and convert it to a TREE_VEC
-       in finish_struct, but we would probably waste more memory
-       making the links in the list than we would by over-allocating
-       the size of the vector here.  Furthermore, we would complicate
-       all the code that expects this to be a vector.  */
-    CLASSTYPE_METHOD_VEC (type) = make_tree_vec (8);
+  if (method == error_mark_node)
+    return false;
+
+  complete_p = COMPLETE_TYPE_P (type);
+  conv_p = DECL_CONV_FN_P (method);
+  if (conv_p)
+    template_conv_p = (TREE_CODE (method) == TEMPLATE_DECL
+                      && DECL_TEMPLATE_CONV_FN_P (method));
 
   method_vec = CLASSTYPE_METHOD_VEC (type);
-  len = TREE_VEC_LENGTH (method_vec);
+  if (!method_vec)
+    {
+      /* Make a new method vector.  We start with 8 entries.  We must
+        allocate at least two (for constructors and destructors), and
+        we're going to end up with an assignment operator at some
+        point as well.  */
+      method_vec = VEC_alloc (tree, gc, 8);
+      /* Create slots for constructors and destructors.  */
+      VEC_quick_push (tree, method_vec, NULL_TREE);
+      VEC_quick_push (tree, method_vec, NULL_TREE);
+      CLASSTYPE_METHOD_VEC (type) = method_vec;
+    }
+
+  /* Maintain TYPE_HAS_USER_CONSTRUCTOR, etc.  */
+  grok_special_member_properties (method);
 
   /* Constructors and destructors go in special slots.  */
   if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (method))
     slot = CLASSTYPE_CONSTRUCTOR_SLOT;
   else if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
-    slot = CLASSTYPE_DESTRUCTOR_SLOT;
-  else
     {
-      /* 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;
-               
-      if (slot == len)
-       {
-         /* We need a bigger method vector.  */
-         int new_len;
-         tree new_vec;
-
-         /* In the non-error case, we are processing a class
-            definition.  Double the size of the vector to give room
-            for new methods.  */
-         if (!error_p)
-           new_len = 2 * len;
-         /* In the error case, the vector is already complete.  We
-            don't expect many errors, and the rest of the front-end
-            will get confused if there are empty slots in the vector.  */
-         else
-           new_len = len + 1;
+      slot = CLASSTYPE_DESTRUCTOR_SLOT;
 
-         new_vec = make_tree_vec (new_len);
-         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;
+      if (TYPE_FOR_JAVA (type))
+       {
+         if (!DECL_ARTIFICIAL (method))
+           error ("Java class %qT cannot have a destructor", type);
+         else if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
+           error ("Java class %qT cannot have an implicit non-trivial "
+                  "destructor",
+                  type);
        }
+    }
+  else
+    {
+      tree m;
 
-      if (DECL_CONV_FN_P (method) && !TREE_VEC_ELT (method_vec, slot))
+      insert_p = true;
+      /* See if we already have an entry with this name.  */
+      for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
+          VEC_iterate (tree, method_vec, slot, m);
+          ++slot)
        {
-         /* Type conversion operators have to come before ordinary
-            methods; add_conversions depends on this to speed up
-            looking for conversion operators.  So, if necessary, we
-            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)
+         m = OVL_CURRENT (m);
+         if (template_conv_p)
            {
-             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 (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
-               /* We can insert the new function right at the
-                  SLOTth position.  */
-               break;
+             if (TREE_CODE (m) == TEMPLATE_DECL
+                 && DECL_TEMPLATE_CONV_FN_P (m))
+               insert_p = false;
+             break;
            }
-  
-         if (!TREE_VEC_ELT (method_vec, slot))
-           /* There is nothing in the Ith slot, so we can avoid
-              moving anything.  */
-               ; 
-         else
+         if (conv_p && !DECL_CONV_FN_P (m))
+           break;
+         if (DECL_NAME (m) == DECL_NAME (method))
            {
-             /* We know the last slot in the vector is empty
-                because we know that at this point there's room
-                for a new function.  */
-             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;
+             insert_p = false;
+             break;
            }
+         if (complete_p
+             && !DECL_CONV_FN_P (m)
+             && DECL_NAME (m) > DECL_NAME (method))
+           break;
        }
     }
-      
-  if (template_class_depth (type))
-    /* TYPE is a template class.  Don't issue any errors now; wait
-       until instantiation time to complain.  */
-    ;
-  else
+  current_fns = insert_p ? NULL_TREE : VEC_index (tree, method_vec, slot);
+
+  /* Check to see if we've already got this method.  */
+  for (fns = current_fns; fns; fns = OVL_NEXT (fns))
     {
-      tree fns;
+      tree fn = OVL_CURRENT (fns);
+      tree fn_type;
+      tree method_type;
+      tree parms1;
+      tree parms2;
 
-      /* Check to see if we've already got this method.  */
-      for (fns = TREE_VEC_ELT (method_vec, slot);
-          fns;
-          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 (fn) != TREE_CODE (method))
-           continue;
+      /* [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).  */
+      fn_type = TREE_TYPE (fn);
+      method_type = TREE_TYPE (method);
+      parms1 = TYPE_ARG_TYPES (fn_type);
+      parms2 = TYPE_ARG_TYPES (method_type);
+
+      /* 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)
+         && TREE_TYPE (TREE_VALUE (parms1)) != error_mark_node
+         && TREE_TYPE (TREE_VALUE (parms2)) != error_mark_node
+         && (TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms1)))
+             != TYPE_QUALS (TREE_TYPE (TREE_VALUE (parms2)))))
+       continue;
+
+      /* For templates, the return type and template parameters
+        must be identical.  */
+      if (TREE_CODE (fn) == TEMPLATE_DECL
+         && (!same_type_p (TREE_TYPE (fn_type),
+                           TREE_TYPE (method_type))
+             || !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
+                                      DECL_TEMPLATE_PARMS (method))))
+       continue;
+
+      if (! DECL_STATIC_FUNCTION_P (fn))
+       parms1 = TREE_CHAIN (parms1);
+      if (! DECL_STATIC_FUNCTION_P (method))
+       parms2 = TREE_CHAIN (parms2);
 
-         /* [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;
-         
-         /* For templates, the template parms must be identical.  */
-         if (TREE_CODE (fn) == TEMPLATE_DECL
-             && !comp_template_parms (DECL_TEMPLATE_PARMS (fn),
-                                      DECL_TEMPLATE_PARMS (method)))
-           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)))))
+      if (compparms (parms1, parms2)
+         && (!DECL_CONV_FN_P (fn)
+             || same_type_p (TREE_TYPE (fn_type),
+                             TREE_TYPE (method_type))))
+       {
+         if (using_decl)
            {
-             if (using && DECL_CONTEXT (fn) == type)
+             if (DECL_CONTEXT (fn) == type)
                /* Defer to the local function.  */
-               return;
+               return false;
+             if (DECL_CONTEXT (fn) == DECL_CONTEXT (method))
+               error ("repeated using declaration %q+D", using_decl);
              else
-               {
-                 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;
-               }
+               error ("using declaration %q+D conflicts with a previous using declaration",
+                      using_decl);
+           }
+         else
+           {
+             error ("%q+#D cannot be overloaded", method);
+             error ("with %q+#D", fn);
            }
+
+         /* 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 false;
        }
     }
 
-  /* Actually insert the new method.  */
-  TREE_VEC_ELT (method_vec, slot) 
-    = build_overload (method, TREE_VEC_ELT (method_vec, slot));
-
-      /* Add the new binding.  */ 
-  if (!DECL_CONSTRUCTOR_P (method)
-      && !DECL_DESTRUCTOR_P (method))
-    push_class_level_binding (DECL_NAME (method),
-                             TREE_VEC_ELT (method_vec, slot));
-}
-
-/* Subroutines of finish_struct.  */
-
-/* Look through the list of fields for this struct, deleting
-   duplicates as we go.  This must be recursive to handle
-   anonymous unions.
+  /* A class should never have more than one destructor.  */
+  if (current_fns && DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (method))
+    return false;
 
-   FIELD is the field which may not appear anywhere in FIELDS.
-   FIELD_PTR, if non-null, is the starting point at which
-   chained deletions may take place.
-   The value returned is the first acceptable entry found
-   in FIELDS.
+  /* Add the new binding.  */
+  overload = build_overload (method, current_fns);
 
-   Note that anonymous fields which are not of UNION_TYPE are
-   not duplicates, they are just anonymous fields.  This happens
-   when we have unnamed bitfields, for example.  */
+  if (conv_p)
+    TYPE_HAS_CONVERSION (type) = 1;
+  else if (slot >= CLASSTYPE_FIRST_CONVERSION_SLOT && !complete_p)
+    push_class_level_binding (DECL_NAME (method), overload);
 
-static tree
-delete_duplicate_fields_1 (field, fields)
-     tree field, fields;
-{
-  tree x;
-  tree prev = 0;
-  if (DECL_NAME (field) == 0)
+  if (insert_p)
     {
-      if (! ANON_AGGR_TYPE_P (TREE_TYPE (field)))
-       return fields;
+      bool reallocated;
 
-      for (x = TYPE_FIELDS (TREE_TYPE (field)); x; x = TREE_CHAIN (x))
-       fields = delete_duplicate_fields_1 (x, fields);
-      return fields;
+      /* We only expect to add few methods in the COMPLETE_P case, so
+        just make room for one more method in that case.  */
+      if (complete_p)
+       reallocated = VEC_reserve_exact (tree, gc, method_vec, 1);
+      else
+       reallocated = VEC_reserve (tree, gc, method_vec, 1);
+      if (reallocated)
+       CLASSTYPE_METHOD_VEC (type) = method_vec;
+      if (slot == VEC_length (tree, method_vec))
+       VEC_quick_push (tree, method_vec, overload);
+      else
+       VEC_quick_insert (tree, method_vec, slot, overload);
     }
   else
-    {
-      for (x = fields; x; prev = x, x = TREE_CHAIN (x))
-       {
-         if (DECL_NAME (x) == 0)
-           {
-             if (! ANON_AGGR_TYPE_P (TREE_TYPE (x)))
-               continue;
-             TYPE_FIELDS (TREE_TYPE (x))
-               = delete_duplicate_fields_1 (field, TYPE_FIELDS (TREE_TYPE (x)));
-             if (TYPE_FIELDS (TREE_TYPE (x)) == 0)
-               {
-                 if (prev == 0)
-                   fields = TREE_CHAIN (fields);
-                 else
-                   TREE_CHAIN (prev) = TREE_CHAIN (x);
-               }
-           }
-         else if (TREE_CODE (field) == USING_DECL)
-           /* 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.  */
-           ;
-         else if (DECL_NAME (field) == DECL_NAME (x))
-           {
-             if (TREE_CODE (field) == CONST_DECL
-                 && TREE_CODE (x) == CONST_DECL)
-               cp_error_at ("duplicate enum value `%D'", x);
-             else if (TREE_CODE (field) == CONST_DECL
-                      || TREE_CODE (x) == CONST_DECL)
-               cp_error_at ("duplicate field `%D' (as enum and non-enum)",
-                            x);
-             else if (DECL_DECLARES_TYPE_P (field)
-                      && DECL_DECLARES_TYPE_P (x))
-               {
-                 if (same_type_p (TREE_TYPE (field), TREE_TYPE (x)))
-                   continue;
-                 cp_error_at ("duplicate nested type `%D'", x);
-               }
-             else if (DECL_DECLARES_TYPE_P (field)
-                      || DECL_DECLARES_TYPE_P (x))
-               {
-                 /* Hide tag decls.  */
-                 if ((TREE_CODE (field) == TYPE_DECL
-                      && DECL_ARTIFICIAL (field))
-                     || (TREE_CODE (x) == TYPE_DECL
-                         && DECL_ARTIFICIAL (x)))
-                   continue;
-                 cp_error_at ("duplicate field `%D' (as type and non-type)",
-                              x);
-               }
-             else
-               cp_error_at ("duplicate member `%D'", x);
-             if (prev == 0)
-               fields = TREE_CHAIN (fields);
-             else
-               TREE_CHAIN (prev) = TREE_CHAIN (x);
-           }
-       }
-    }
-  return fields;
+    /* Replace the current slot.  */
+    VEC_replace (tree, method_vec, slot, overload);
+  return true;
 }
 
-static void
-delete_duplicate_fields (fields)
-     tree fields;
-{
-  tree x;
-  for (x = fields; x && TREE_CHAIN (x); x = TREE_CHAIN (x))
-    TREE_CHAIN (x) = delete_duplicate_fields_1 (x, TREE_CHAIN (x));
-}
+/* Subroutines of finish_struct.  */
 
 /* Change the access of FDECL to ACCESS in T.  Return 1 if change was
    legit, otherwise return 0.  */
 
 static int
-alter_access (t, fdecl, access)
-     tree t;
-     tree fdecl;
-     tree access;
+alter_access (tree t, tree fdecl, tree access)
 {
   tree elem;
 
   if (!DECL_LANG_SPECIFIC (fdecl))
     retrofit_lang_decl (fdecl);
 
-  if (DECL_DISCRIMINATOR_P (fdecl))
-    abort ();
+  gcc_assert (!DECL_DISCRIMINATOR_P (fdecl));
 
   elem = purpose_member (t, DECL_ACCESS (fdecl));
   if (elem)
@@ -1081,10 +1150,11 @@ alter_access (t, fdecl, access)
       if (TREE_VALUE (elem) != access)
        {
          if (TREE_CODE (TREE_TYPE (fdecl)) == FUNCTION_DECL)
-           cp_error_at ("conflicting access specifications for method `%D', ignored", TREE_TYPE (fdecl));
+           error ("conflicting access specifications for method"
+                  " %q+D, ignored", TREE_TYPE (fdecl));
          else
-           error ("conflicting access specifications for field `%s', ignored",
-                  IDENTIFIER_POINTER (DECL_NAME (fdecl)));
+           error ("conflicting access specifications for field %qE, ignored",
+                  DECL_NAME (fdecl));
        }
       else
        {
@@ -1095,7 +1165,7 @@ alter_access (t, fdecl, access)
     }
   else
     {
-      enforce_access (t, fdecl);
+      perform_or_defer_access_check (TYPE_BINFO (t), fdecl, fdecl);
       DECL_ACCESS (fdecl) = tree_cons (t, access, DECL_ACCESS (fdecl));
       return 1;
     }
@@ -1105,53 +1175,20 @@ alter_access (t, fdecl, access)
 /* Process the USING_DECL, which is a member of T.  */
 
 static void
-handle_using_decl (using_decl, t)
-     tree using_decl;
-     tree t;
+handle_using_decl (tree using_decl, tree t)
 {
-  tree ctype = DECL_INITIAL (using_decl);
+  tree decl = USING_DECL_DECLS (using_decl);
   tree name = DECL_NAME (using_decl);
   tree access
     = TREE_PRIVATE (using_decl) ? access_private_node
     : TREE_PROTECTED (using_decl) ? access_protected_node
     : access_public_node;
-  tree fdecl, binfo;
   tree flist = NULL_TREE;
   tree old_value;
 
-  binfo = lookup_base (t, ctype, ba_any, NULL);
-  if (! binfo)
-    {
-      error_not_base_type (t, ctype);
-      return;
-    }
-  
-  if (name == constructor_name (ctype)
-      || name == constructor_name_full (ctype))
-    {
-      cp_error_at ("`%D' names constructor", using_decl);
-      return;
-    }
-  if (name == constructor_name (t)
-      || name == constructor_name_full (t))
-    {
-      cp_error_at ("`%D' invalid in `%T'", using_decl, t);
-      return;
-    }
-
-  fdecl = lookup_member (binfo, name, 0, 0);
-  
-  if (!fdecl)
-    {
-      cp_error_at ("no members matching `%D' in `%#T'", using_decl, ctype);
-      return;
-    }
-
-  if (BASELINK_P (fdecl))
-    /* Ignore base type this came from. */
-    fdecl = TREE_VALUE (fdecl);
+  gcc_assert (!processing_template_decl && decl);
 
-  old_value = IDENTIFIER_CLASS_VALUE (name);
+  old_value = lookup_member (t, name, /*protect=*/0, /*want_type=*/false);
   if (old_value)
     {
       if (is_overloaded_fn (old_value))
@@ -1163,8 +1200,10 @@ handle_using_decl (using_decl, t)
        old_value = NULL_TREE;
     }
 
-  if (is_overloaded_fn (fdecl))
-    flist = fdecl;
+  cp_emit_debug_info_for_using (decl, USING_DECL_SCOPE (using_decl));
+
+  if (is_overloaded_fn (decl))
+    flist = decl;
 
   if (! old_value)
     ;
@@ -1175,89 +1214,59 @@ handle_using_decl (using_decl, t)
           the same name already present in the current class.  */;
       else
        {
-         cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
-         cp_error_at ("  because of local method `%#D' with same name",
-                      OVL_CURRENT (old_value));
+         error ("%q+D invalid in %q#T", using_decl, t);
+         error ("  because of local method %q+#D with same name",
+                OVL_CURRENT (old_value));
          return;
        }
     }
   else if (!DECL_ARTIFICIAL (old_value))
     {
-      cp_error_at ("`%D' invalid in `%#T'", using_decl, t);
-      cp_error_at ("  because of local member `%#D' with same name", old_value);
+      error ("%q+D invalid in %q#T", using_decl, t);
+      error ("  because of local member %q+#D with same name", old_value);
       return;
     }
-  
-  /* Make type T see field decl FDECL with access ACCESS.*/
+
+  /* Make type T see field decl FDECL with access ACCESS.  */
   if (flist)
     for (; flist; flist = OVL_NEXT (flist))
       {
-       add_method (t, OVL_CURRENT (flist), /*error_p=*/0);
+       add_method (t, OVL_CURRENT (flist), using_decl);
        alter_access (t, OVL_CURRENT (flist), access);
       }
   else
-    alter_access (t, fdecl, access);
+    alter_access (t, decl, access);
 }
 \f
-/* Run through the base clases of T, updating
-   CANT_HAVE_DEFAULT_CTOR_P, CANT_HAVE_CONST_CTOR_P, and
-   NO_CONST_ASN_REF_P.  Also set flag bits in T based on properties of
-   the bases.  */
+/* Run through the base classes of T, updating CANT_HAVE_CONST_CTOR_P,
+   and NO_CONST_ASN_REF_P.  Also set flag bits in T based on
+   properties of the bases.  */
 
 static void
-check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
-            no_const_asn_ref_p)
-     tree t;
-     int *cant_have_default_ctor_p;
-     int *cant_have_const_ctor_p;
-     int *no_const_asn_ref_p;
-{
-  int n_baseclasses;
+check_bases (tree t,
+            int* cant_have_const_ctor_p,
+            int* no_const_asn_ref_p)
+{
   int i;
   int seen_non_virtual_nearly_empty_base_p;
-  tree binfos;
+  tree base_binfo;
+  tree binfo;
 
-  binfos = TYPE_BINFO_BASETYPES (t);
-  n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
   seen_non_virtual_nearly_empty_base_p = 0;
 
-  /* An aggregate cannot have baseclasses.  */
-  CLASSTYPE_NON_AGGREGATE (t) |= (n_baseclasses != 0);
-
-  for (i = 0; i < n_baseclasses; ++i) 
+  for (binfo = TYPE_BINFO (t), i = 0;
+       BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
     {
-      tree base_binfo;
-      tree basetype;
+      tree basetype = TREE_TYPE (base_binfo);
 
-      /* Figure out what base we're looking at.  */
-      base_binfo = TREE_VEC_ELT (binfos, i);
-      basetype = TREE_TYPE (base_binfo);
-
-      /* If the type of basetype is incomplete, then we already
-        complained about that fact (and we should have fixed it up as
-        well).  */
-      if (!COMPLETE_TYPE_P (basetype))
-       {
-         int j;
-         /* The base type is of incomplete type.  It is
-            probably best to pretend that it does not
-            exist.  */
-         if (i == n_baseclasses-1)
-           TREE_VEC_ELT (binfos, i) = NULL_TREE;
-         TREE_VEC_LENGTH (binfos) -= 1;
-         n_baseclasses -= 1;
-         for (j = i; j+1 < n_baseclasses; j++)
-           TREE_VEC_ELT (binfos, j) = TREE_VEC_ELT (binfos, j+1);
-         continue;
-       }
+      gcc_assert (COMPLETE_TYPE_P (basetype));
 
       /* Effective C++ rule 14.  We only need to check TYPE_POLYMORPHIC_P
         here because the case of virtual functions but non-virtual
         dtor is handled in finish_struct_1.  */
-      if (warn_ecpp && ! TYPE_POLYMORPHIC_P (basetype)
-         && TYPE_HAS_DESTRUCTOR (basetype))
-       warning ("base class `%#T' has a non-virtual destructor",
-                   basetype);
+      if (!TYPE_POLYMORPHIC_P (basetype))
+       warning (OPT_Weffc__,
+                "base class %q#T has a non-virtual destructor", basetype);
 
       /* If the base class doesn't have copy constructors or
         assignment operators that take const references, then the
@@ -1268,20 +1277,9 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
       if (TYPE_HAS_ASSIGN_REF (basetype)
          && !TYPE_HAS_CONST_ASSIGN_REF (basetype))
        *no_const_asn_ref_p = 1;
-      /* Similarly, if the base class doesn't have a default
-        constructor, then the derived class won't have an
-        automatically generated default constructor.  */
-      if (TYPE_HAS_CONSTRUCTOR (basetype)
-         && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (basetype))
-       {
-         *cant_have_default_ctor_p = 1;
-         if (! TYPE_HAS_CONSTRUCTOR (t))
-            pedwarn ("base `%T' with only non-default constructor in class without a constructor",
-                        basetype);
-       }
 
-      if (TREE_VIA_VIRTUAL (base_binfo))
-       /* A virtual base does not effect nearly emptiness. */
+      if (BINFO_VIRTUAL_P (base_binfo))
+       /* A virtual base does not effect nearly emptiness.  */
        ;
       else if (CLASSTYPE_NEARLY_EMPTY_P (basetype))
        {
@@ -1290,7 +1288,7 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
               derived class is not nearly empty either.  */
            CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
          else
-           /* Remember we've seen one. */
+           /* Remember we've seen one.  */
            seen_non_virtual_nearly_empty_base_p = 1;
        }
       else if (!is_empty_class (basetype))
@@ -1301,490 +1299,216 @@ check_bases (t, cant_have_default_ctor_p, cant_have_const_ctor_p,
       /* A lot of properties from the bases also apply to the derived
         class.  */
       TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (basetype);
-      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
+      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
        |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (basetype);
-      TYPE_HAS_COMPLEX_ASSIGN_REF (t) 
+      TYPE_HAS_COMPLEX_ASSIGN_REF (t)
        |= TYPE_HAS_COMPLEX_ASSIGN_REF (basetype);
       TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (basetype);
-      TYPE_OVERLOADS_CALL_EXPR (t) |= TYPE_OVERLOADS_CALL_EXPR (basetype);
-      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);
-      CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) 
+      CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t)
        |= CLASSTYPE_CONTAINS_EMPTY_CLASS_P (basetype);
+      TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_HAS_COMPLEX_DFLT (basetype);      
     }
 }
 
-/* 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 hierarchy is dominated by TYPE.  MAPPINGS is an
-   assoc list of binfos that have already been reseated.  */
-
-static void
-force_canonical_binfo_r (to, from, type, mappings)
-     tree to;
-     tree from;
-     tree type;
-     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);
-      tree assoc;
-      
-      /* We might have just moved the primary base too, see if it's on our
-         mappings.  */
-      assoc = purpose_member (primary, mappings);
-      if (assoc)
-        primary = TREE_VALUE (assoc);
-      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);
-  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))
-        {
-         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
-        force_canonical_binfo_r (to_binfo, from_binfo, type, mappings);
-    }
-}
-
-/* FROM is the canonical binfo for a virtual base. It is being reseated to
-   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
-   bases walks in inheritance graph order, but we don't share binfos for
-   virtual bases, hence we can fill in the primaries for a virtual base,
-   and then discover that a later base requires the virtual as its
-   primary.  */
+/* Determine all the primary bases within T.  Sets BINFO_PRIMARY_BASE_P for
+   those that are primaries.  Sets BINFO_LOST_PRIMARY_P for those
+   that have had a nearly-empty virtual primary base stolen by some
+   other base in the hierarchy.  Determines CLASSTYPE_PRIMARY_BASE for
+   T.  */
 
 static void
-force_canonical_binfo (to, from, type, mappings)
-     tree to;
-     tree from;
-     tree type;
-     tree mappings;
+determine_primary_bases (tree t)
 {
-  tree assoc = purpose_member (BINFO_TYPE (to),
-                              CLASSTYPE_VBASECLASSES (type));
-  if (TREE_VALUE (assoc) != to)
+  unsigned i;
+  tree primary = NULL_TREE;
+  tree type_binfo = TYPE_BINFO (t);
+  tree base_binfo;
+
+  /* Determine the primary bases of our bases.  */
+  for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
+       base_binfo = TREE_CHAIN (base_binfo))
     {
-      TREE_VALUE (assoc) = to;
-      force_canonical_binfo_r (to, from, type, mappings);
-    }
-}
+      tree primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (base_binfo));
 
-/* 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).  */
+      /* See if we're the non-virtual primary of our inheritance
+        chain.  */
+      if (!BINFO_VIRTUAL_P (base_binfo))
+       {
+         tree parent = BINFO_INHERITANCE_CHAIN (base_binfo);
+         tree parent_primary = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (parent));
+
+         if (parent_primary
+             && SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
+                                   BINFO_TYPE (parent_primary)))
+           /* We are the primary binfo.  */
+           BINFO_PRIMARY_P (base_binfo) = 1;
+       }
+      /* Determine if we have a virtual primary base, and mark it so.
+       */
+      if (primary && BINFO_VIRTUAL_P (primary))
+       {
+         tree this_primary = copied_binfo (primary, base_binfo);
 
-static tree
-mark_primary_virtual_base (base_binfo, type)
-     tree base_binfo;
-     tree type;
-{
-  tree shared_binfo = binfo_for_vbase (BINFO_TYPE (base_binfo), type);
-
-  if (BINFO_PRIMARY_P (shared_binfo))
-    {
-      /* It's already allocated in the hierarchy. BINFO won't have a
-         primary base in this hierarchy, even though the complete object
-         BINFO is for, would do.  */
-      return NULL_TREE;
-    }
-     
-  /* We need to make sure that the assoc list
-     CLASSTYPE_VBASECLASSES of TYPE, indicates this particular
-     primary BINFO for the virtual base, as this is the one
-     that'll really exist.  */
-  if (base_binfo != shared_binfo)
-    force_canonical_binfo (base_binfo, shared_binfo, type, NULL);
-
-  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)))
-    {
-      /* 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;
-    }
-  
-  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
-   dominated by TYPE that are primary bases.  */
-
-static void
-mark_primary_bases (type)
-     tree type;
-{
-  tree binfo;
-  
-  /* Walk the bases in inheritance graph order.  */
-  for (binfo = TYPE_BINFO (type); binfo; binfo = TREE_CHAIN (binfo))
-    {
-      tree base_binfo;
-      
-      if (!CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (binfo)))
-        /* Not a dynamic base. */
-        continue;
-
-      base_binfo = get_primary_binfo (binfo);
-
-      if (TREE_VIA_VIRTUAL (base_binfo))
-        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.  */
-
-static void
-set_primary_base (t, binfo, vfuns_p)
-     tree t;
-     tree binfo;
-     int *vfuns_p;
-{
-  tree basetype;
-
-  CLASSTYPE_PRIMARY_BINFO (t) = binfo;
-  basetype = BINFO_TYPE (binfo);
-  TYPE_BINFO_VTABLE (t) = TYPE_BINFO_VTABLE (basetype);
-  TYPE_BINFO_VIRTUALS (t) = TYPE_BINFO_VIRTUALS (basetype);
-  TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
-  CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
-  *vfuns_p = CLASSTYPE_VSIZE (basetype);
-}
-
-/* Determine the primary class for T.  */
-
-static void
-determine_primary_base (t, vfuns_p)
-     tree t;
-     int *vfuns_p;
-{
-  int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
-  tree vbases;
-  tree type_binfo;
-
-  /* If there are no baseclasses, there is certainly no primary base.  */
-  if (n_baseclasses == 0)
-    return;
-
-  type_binfo = TYPE_BINFO (t);
+         if (BINFO_PRIMARY_P (this_primary))
+           /* Someone already claimed this base.  */
+           BINFO_LOST_PRIMARY_P (base_binfo) = 1;
+         else
+           {
+             tree delta;
 
-  for (i = 0; i < n_baseclasses; i++)
-    {
-      tree base_binfo = BINFO_BASETYPE (type_binfo, i);
-      tree basetype = BINFO_TYPE (base_binfo);
+             BINFO_PRIMARY_P (this_primary) = 1;
+             BINFO_INHERITANCE_CHAIN (this_primary) = base_binfo;
 
-      if (TYPE_CONTAINS_VPTR_P (basetype))
-       {
-         /* Even a virtual baseclass can contain our RTTI
-            information.  But, we prefer a non-virtual polymorphic
-            baseclass.  */
-         if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
-           CLASSTYPE_RTTI (t) = CLASSTYPE_RTTI (basetype);
-
-         /* We prefer a non-virtual base, although a virtual one will
-            do.  */
-         if (TREE_VIA_VIRTUAL (base_binfo))
-           continue;
+             /* A virtual binfo might have been copied from within
+                another hierarchy. As we're about to use it as a
+                primary base, make sure the offsets match.  */
+             delta = size_diffop (convert (ssizetype,
+                                           BINFO_OFFSET (base_binfo)),
+                                  convert (ssizetype,
+                                           BINFO_OFFSET (this_primary)));
 
-         if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
-           {
-             set_primary_base (t, base_binfo, vfuns_p);
-             CLASSTYPE_VFIELDS (t) = copy_list (CLASSTYPE_VFIELDS (basetype));
-           }
-         else
-           {
-             tree vfields;
-
-             /* Only add unique vfields, and flatten them out as we go.  */
-             for (vfields = CLASSTYPE_VFIELDS (basetype);
-                  vfields;
-                  vfields = TREE_CHAIN (vfields))
-               if (VF_BINFO_VALUE (vfields) == NULL_TREE
-                   || ! TREE_VIA_VIRTUAL (VF_BINFO_VALUE (vfields)))
-                 CLASSTYPE_VFIELDS (t) 
-                   = tree_cons (base_binfo, 
-                                VF_BASETYPE_VALUE (vfields),
-                                CLASSTYPE_VFIELDS (t));
+             propagate_binfo_offsets (this_primary, delta);
            }
        }
     }
 
-  if (!TYPE_VFIELD (t))
-    CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
-
-  /* Find the indirect primary bases - those virtual bases which are primary
-     bases of something else in this hierarchy.  */
-  for (vbases = CLASSTYPE_VBASECLASSES (t);
-       vbases;
-       vbases = TREE_CHAIN (vbases)) 
+  /* First look for a dynamic direct non-virtual base.  */
+  for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, base_binfo); i++)
     {
-      tree vbase_binfo = TREE_VALUE (vbases);
+      tree basetype = BINFO_TYPE (base_binfo);
 
-      /* See if this virtual base is an indirect primary base.  To be so,
-         it must be a primary base within the hierarchy of one of our
-         direct bases.  */
-      for (i = 0; i < n_baseclasses; ++i) 
+      if (TYPE_CONTAINS_VPTR_P (basetype) && !BINFO_VIRTUAL_P (base_binfo))
        {
-         tree basetype = TYPE_BINFO_BASETYPE (t, i);
-         tree v;
-
-         for (v = CLASSTYPE_VBASECLASSES (basetype); 
-              v; 
-              v = TREE_CHAIN (v))
-           {
-             tree base_vbase = TREE_VALUE (v);
-             
-             if (BINFO_PRIMARY_P (base_vbase)
-                 && same_type_p (BINFO_TYPE (base_vbase),
-                                 BINFO_TYPE (vbase_binfo)))
-               {
-                 BINFO_INDIRECT_PRIMARY_P (vbase_binfo) = 1;
-                 break;
-               }
-           }
-
-         /* If we've discovered that this virtual base is an indirect
-            primary base, then we can move on to the next virtual
-            base.  */
-         if (BINFO_INDIRECT_PRIMARY_P (vbase_binfo))
-           break;
+         primary = base_binfo;
+         goto 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))
+     class, if no non-virtual polymorphic base can be found.  Look for
+     a nearly-empty virtual dynamic base that is not already a primary
+     base of something in the hierarchy.  If there is no such base,
+     just pick the first nearly-empty virtual base.  */
+
+  for (base_binfo = TREE_CHAIN (type_binfo); base_binfo;
+       base_binfo = TREE_CHAIN (base_binfo))
+    if (BINFO_VIRTUAL_P (base_binfo)
+       && CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (base_binfo)))
+      {
+       if (!BINFO_PRIMARY_P (base_binfo))
+         {
+           /* Found one that is not primary.  */
+           primary = base_binfo;
+           goto found;
+         }
+       else if (!primary)
+         /* Remember the first candidate.  */
+         primary = base_binfo;
+      }
+
+ found:
+  /* If we've got a primary base, use it.  */
+  if (primary)
     {
-      /* If not NULL, this is the best primary base candidate we have
-         found so far.  */
-      tree candidate = NULL_TREE;
-      tree base_binfo;
+      tree basetype = BINFO_TYPE (primary);
 
-      /* Loop over the baseclasses.  */
-      for (base_binfo = TYPE_BINFO (t);
-          base_binfo;
-          base_binfo = TREE_CHAIN (base_binfo))
+      CLASSTYPE_PRIMARY_BINFO (t) = primary;
+      if (BINFO_PRIMARY_P (primary))
+       /* We are stealing a primary base.  */
+       BINFO_LOST_PRIMARY_P (BINFO_INHERITANCE_CHAIN (primary)) = 1;
+      BINFO_PRIMARY_P (primary) = 1;
+      if (BINFO_VIRTUAL_P (primary))
        {
-         tree basetype = BINFO_TYPE (base_binfo);
+         tree delta;
 
-         if (TREE_VIA_VIRTUAL (base_binfo) 
-             && CLASSTYPE_NEARLY_EMPTY_P (basetype))
-           {
-             /* If this is not an indirect primary base, then it's
-                definitely our primary base.  */
-             if (!BINFO_INDIRECT_PRIMARY_P (base_binfo))
-               {
-                 candidate = base_binfo;
-                 break;
-               }
+         BINFO_INHERITANCE_CHAIN (primary) = type_binfo;
+         /* A virtual binfo might have been copied from within
+            another hierarchy. As we're about to use it as a primary
+            base, make sure the offsets match.  */
+         delta = size_diffop (ssize_int (0),
+                              convert (ssizetype, BINFO_OFFSET (primary)));
 
-             /* If this is an indirect primary base, it still could be
-                our primary base -- unless we later find there's another
-                nearly-empty virtual base that isn't an indirect
-                primary base.  */
-             if (!candidate)
-               candidate = base_binfo;
-           }
+         propagate_binfo_offsets (primary, delta);
        }
 
-      /* If we've got a primary base, use it.  */
-      if (candidate)
-       {
-         set_primary_base (t, candidate, vfuns_p);
-         CLASSTYPE_VFIELDS (t) 
-           = copy_list (CLASSTYPE_VFIELDS (BINFO_TYPE (candidate)));
-       }       
-    }
+      primary = TYPE_BINFO (basetype);
 
-  /* Mark the primary base classes at this point.  */
-  mark_primary_bases (t);
+      TYPE_VFIELD (t) = TYPE_VFIELD (basetype);
+      BINFO_VTABLE (type_binfo) = BINFO_VTABLE (primary);
+      BINFO_VIRTUALS (type_binfo) = BINFO_VIRTUALS (primary);
+    }
 }
 \f
 /* Set memoizing fields and bits of T (and its variants) for later
    use.  */
 
 static void
-finish_struct_bits (t)
-     tree t;
+finish_struct_bits (tree t)
 {
-  int i, n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
+  tree variants;
 
   /* Fix up variants (if any).  */
-  tree variants = TYPE_NEXT_VARIANT (t);
-  while (variants)
+  for (variants = TYPE_NEXT_VARIANT (t);
+       variants;
+       variants = TYPE_NEXT_VARIANT (variants))
     {
       /* These fields are in the _TYPE part of the node, not in
         the TYPE_LANG_SPECIFIC component, so they are not shared.  */
-      TYPE_HAS_CONSTRUCTOR (variants) = TYPE_HAS_CONSTRUCTOR (t);
-      TYPE_HAS_DESTRUCTOR (variants) = TYPE_HAS_DESTRUCTOR (t);
+      TYPE_HAS_USER_CONSTRUCTOR (variants) = TYPE_HAS_USER_CONSTRUCTOR (t);
       TYPE_NEEDS_CONSTRUCTING (variants) = TYPE_NEEDS_CONSTRUCTING (t);
-      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants) 
+      TYPE_HAS_NONTRIVIAL_DESTRUCTOR (variants)
        = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
 
-      TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (variants) 
-       = TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t);
       TYPE_POLYMORPHIC_P (variants) = TYPE_POLYMORPHIC_P (t);
-      TYPE_USES_VIRTUAL_BASECLASSES (variants) = TYPE_USES_VIRTUAL_BASECLASSES (t);
-      /* Copy whatever these are holding today.  */
-      TYPE_MIN_VALUE (variants) = TYPE_MIN_VALUE (t);
-      TYPE_MAX_VALUE (variants) = TYPE_MAX_VALUE (t);
-      TYPE_FIELDS (variants) = TYPE_FIELDS (t);
-      TYPE_SIZE (variants) = TYPE_SIZE (t);
-      TYPE_SIZE_UNIT (variants) = TYPE_SIZE_UNIT (t);
-      variants = TYPE_NEXT_VARIANT (variants);
-    }
-
-  if (n_baseclasses && TYPE_POLYMORPHIC_P (t))
-    /* For a class w/o baseclasses, `finish_struct' has set
-       CLASS_TYPE_ABSTRACT_VIRTUALS correctly (by
-       definition). Similarly for a class whose base classes do not
-       have vtables. When neither of these is true, we might have
-       removed abstract virtuals (by providing a definition), added
-       some (by declaring new ones), or redeclared ones from a base
-       class. We need to recalculate what's really an abstract virtual
-       at this point (by looking in the vtables).  */
-      get_pure_virtuals (t);
 
-  if (n_baseclasses)
-    {
-      /* Notice whether this class has type conversion functions defined.  */
-      tree binfo = TYPE_BINFO (t);
-      tree binfos = BINFO_BASETYPES (binfo);
-      tree basetype;
+      TYPE_BINFO (variants) = TYPE_BINFO (t);
 
-      for (i = n_baseclasses-1; i >= 0; i--)
-       {
-         basetype = BINFO_TYPE (TREE_VEC_ELT (binfos, i));
-
-         TYPE_HAS_CONVERSION (t) |= TYPE_HAS_CONVERSION (basetype);
-       }
-    }
+      /* Copy whatever these are holding today.  */
+      TYPE_VFIELD (variants) = TYPE_VFIELD (t);
+      TYPE_METHODS (variants) = TYPE_METHODS (t);
+      TYPE_FIELDS (variants) = TYPE_FIELDS (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.  */
+      /* All variants of a class have the same attributes.  */
+      TYPE_ATTRIBUTES (variants) = TYPE_ATTRIBUTES (t);
+    }
+
+  if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) && TYPE_POLYMORPHIC_P (t))
+    /* For a class w/o baseclasses, 'finish_struct' has set
+       CLASSTYPE_PURE_VIRTUALS correctly (by definition).
+       Similarly for a class whose base classes do not have vtables.
+       When neither of these is true, we might have removed abstract
+       virtuals (by providing a definition), added some (by declaring
+       new ones), or redeclared ones from a base class.  We need to
+       recalculate what's really an abstract virtual at this point (by
+       looking in the vtables).  */
+    get_pure_virtuals (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;
       for (variants = t; variants; variants = TYPE_NEXT_VARIANT (variants))
        {
-         TYPE_MODE (variants) = BLKmode;
+         SET_TYPE_MODE (variants, BLKmode);
          TREE_ADDRESSABLE (variants) = 1;
        }
     }
 }
 
 /* Issue warnings about T having private constructors, but no friends,
-   and so forth.  
+   and so forth.
 
    HAS_NONPRIVATE_METHOD is nonzero if T has any non-private methods or
    static members.  HAS_NONPRIVATE_STATIC_FN is nonzero if T has any
    non-private static member functions.  */
 
 static void
-maybe_warn_about_overly_private_class (t)
-     tree t;
+maybe_warn_about_overly_private_class (tree t)
 {
   int has_member_fn = 0;
   int has_nonprivate_method = 0;
@@ -1798,10 +1522,10 @@ maybe_warn_about_overly_private_class (t)
       /* We will have warned when the template was declared; there's
         no need to warn on every instantiation.  */
       || CLASSTYPE_TEMPLATE_INSTANTIATION (t))
-    /* There's no reason to even consider warning about this 
+    /* There's no reason to even consider warning about this
        class.  */
     return;
-    
+
   /* We only issue one warning, if more than one applies, because
      otherwise, on code like:
 
@@ -1821,25 +1545,25 @@ maybe_warn_about_overly_private_class (t)
   for (fn = TYPE_METHODS (t); fn; fn = TREE_CHAIN (fn))
     /* We're not interested in compiler-generated methods; they don't
        provide any way to call private members.  */
-    if (!DECL_ARTIFICIAL (fn)) 
+    if (!DECL_ARTIFICIAL (fn))
       {
        if (!TREE_PRIVATE (fn))
          {
-           if (DECL_STATIC_FUNCTION_P (fn)) 
+           if (DECL_STATIC_FUNCTION_P (fn))
              /* A non-private static member function is just like a
                 friend; it can create and invoke private member
                 functions, and be accessed without a class
                 instance.  */
              return;
-               
+
            has_nonprivate_method = 1;
-           break;
+           /* Keep searching for a static member function.  */
          }
        else if (!DECL_CONSTRUCTOR_P (fn) && !DECL_DESTRUCTOR_P (fn))
          has_member_fn = 1;
-      } 
+      }
 
-  if (!has_nonprivate_method && has_member_fn) 
+  if (!has_nonprivate_method && has_member_fn)
     {
       /* There are no non-private methods, and there's at least one
         private member function that isn't a constructor or
@@ -1847,18 +1571,19 @@ maybe_warn_about_overly_private_class (t)
         constructors/destructors we want to use the code below that
         issues error messages specifically referring to
         constructors/destructors.)  */
-      int i;
-      tree binfos = BINFO_BASETYPES (TYPE_BINFO (t));
-      for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); i++)
-       if (TREE_VIA_PUBLIC (TREE_VEC_ELT (binfos, i))
-           || TREE_VIA_PROTECTED (TREE_VEC_ELT (binfos, i)))
+      unsigned i;
+      tree binfo = TYPE_BINFO (t);
+
+      for (i = 0; i != BINFO_N_BASE_BINFOS (binfo); i++)
+       if (BINFO_BASE_ACCESS (binfo, i) != access_private_node)
          {
            has_nonprivate_method = 1;
            break;
          }
-      if (!has_nonprivate_method) 
+      if (!has_nonprivate_method)
        {
-         warning ("all member functions in class `%T' are private", t);
+         warning (OPT_Wctor_dtor_privacy,
+                  "all member functions in class %qT are private", t);
          return;
        }
     }
@@ -1866,38 +1591,37 @@ maybe_warn_about_overly_private_class (t)
   /* Even if some of the member functions are non-private, the class
      won't be useful for much if all the constructors or destructors
      are private: such an object can never be created or destroyed.  */
-  if (TYPE_HAS_DESTRUCTOR (t))
+  fn = CLASSTYPE_DESTRUCTORS (t);
+  if (fn && TREE_PRIVATE (fn))
     {
-      tree dtor = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 1);
-
-      if (TREE_PRIVATE (dtor))
-       {
-         warning ("`%#T' only defines a private destructor and has no friends",
-                     t);
-         return;
-       }
+      warning (OPT_Wctor_dtor_privacy,
+              "%q#T only defines a private destructor and has no friends",
+              t);
+      return;
     }
 
-  if (TYPE_HAS_CONSTRUCTOR (t))
+  /* Warn about classes that have private constructors and no friends.  */
+  if (TYPE_HAS_USER_CONSTRUCTOR (t)
+      /* Implicitly generated constructors are always public.  */
+      && (!CLASSTYPE_LAZY_DEFAULT_CTOR (t)
+         || !CLASSTYPE_LAZY_COPY_CTOR (t)))
     {
       int nonprivate_ctor = 0;
-         
+
       /* If a non-template class does not define a copy
         constructor, one is defined for it, enabling it to avoid
         this warning.  For a template class, this does not
         happen, and so we would normally get a warning on:
 
-          template <class T> class C { private: C(); };  
-         
+          template <class T> class C { private: C(); };
+
         To avoid this asymmetry, we check TYPE_HAS_INIT_REF.  All
         complete non-template or fully instantiated classes have this
         flag set.  */
       if (!TYPE_HAS_INIT_REF (t))
        nonprivate_ctor = 1;
-      else 
-       for (fn = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (t), 0);
-            fn;
-            fn = OVL_NEXT (fn)) 
+      else
+       for (fn = CLASSTYPE_CONSTRUCTORS (t); fn; fn = OVL_NEXT (fn))
          {
            tree ctor = OVL_CURRENT (fn);
            /* Ideally, we wouldn't count copy constructors (or, in
@@ -1915,205 +1639,144 @@ maybe_warn_about_overly_private_class (t)
 
       if (nonprivate_ctor == 0)
        {
-         warning ("`%#T' only defines private constructors and has no friends",
-                     t);
+         warning (OPT_Wctor_dtor_privacy,
+                  "%q#T only defines private constructors and has no friends",
+                  t);
          return;
        }
     }
 }
 
-/* Function to help qsort sort FIELD_DECLs by name order.  */
+static struct {
+  gt_pointer_operator new_value;
+  void *cookie;
+} resort_data;
+
+/* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
 
 static int
-field_decl_cmp (x, y)
-     const tree *x, *y;
+method_name_cmp (const void* m1_p, const void* m2_p)
 {
-  if (DECL_NAME (*x) == DECL_NAME (*y))
-    /* A nontype is "greater" than a type.  */
-    return DECL_DECLARES_TYPE_P (*y) - DECL_DECLARES_TYPE_P (*x);
-  if (DECL_NAME (*x) == NULL_TREE)
+  const tree *const m1 = (const tree *) m1_p;
+  const tree *const m2 = (const tree *) m2_p;
+
+  if (*m1 == NULL_TREE && *m2 == NULL_TREE)
+    return 0;
+  if (*m1 == NULL_TREE)
     return -1;
-  if (DECL_NAME (*y) == NULL_TREE)
+  if (*m2 == NULL_TREE)
     return 1;
-  if (DECL_NAME (*x) < DECL_NAME (*y))
+  if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
     return -1;
   return 1;
 }
 
-/* Comparison function to compare two TYPE_METHOD_VEC entries by name.  */
+/* This routine compares two fields like method_name_cmp but using the
+   pointer operator in resort_field_decl_data.  */
 
 static int
-method_name_cmp (m1, m2)
-     const tree *m1, *m2;
+resort_method_name_cmp (const void* m1_p, const void* m2_p)
 {
+  const tree *const m1 = (const tree *) m1_p;
+  const tree *const m2 = (const tree *) m2_p;
   if (*m1 == NULL_TREE && *m2 == NULL_TREE)
     return 0;
   if (*m1 == NULL_TREE)
     return -1;
   if (*m2 == NULL_TREE)
     return 1;
-  if (DECL_NAME (OVL_CURRENT (*m1)) < DECL_NAME (OVL_CURRENT (*m2)))
-    return -1;
+  {
+    tree d1 = DECL_NAME (OVL_CURRENT (*m1));
+    tree d2 = DECL_NAME (OVL_CURRENT (*m2));
+    resort_data.new_value (&d1, resort_data.cookie);
+    resort_data.new_value (&d2, resort_data.cookie);
+    if (d1 < d2)
+      return -1;
+  }
   return 1;
 }
 
-/* Warn about duplicate methods in fn_fields.  Also compact method
-   lists so that lookup can be made faster.
+/* Resort TYPE_METHOD_VEC because pointers have been reordered.  */
+
+void
+resort_type_method_vec (void* obj,
+                       void* orig_obj ATTRIBUTE_UNUSED ,
+                       gt_pointer_operator new_value,
+                       void* cookie)
+{
+  VEC(tree,gc) *method_vec = (VEC(tree,gc) *) obj;
+  int len = VEC_length (tree, method_vec);
+  size_t slot;
+  tree fn;
+
+  /* The type conversion ops have to live at the front of the vec, so we
+     can't sort them.  */
+  for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
+       VEC_iterate (tree, method_vec, slot, fn);
+       ++slot)
+    if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
+      break;
+
+  if (len - slot > 1)
+    {
+      resort_data.new_value = new_value;
+      resort_data.cookie = cookie;
+      qsort (VEC_address (tree, method_vec) + slot, len - slot, sizeof (tree),
+            resort_method_name_cmp);
+    }
+}
 
-   Data Structure: List of method lists.  The outer list is a
-   TREE_LIST, whose TREE_PURPOSE field is the field name and the
-   TREE_VALUE is the DECL_CHAIN of the FUNCTION_DECLs.  TREE_CHAIN
-   links the entire list of methods for TYPE_METHODS.  Friends are
-   chained in the same way as member functions (? TREE_CHAIN or
-   DECL_CHAIN), but they live in the TREE_TYPE field of the outer
-   list.  That allows them to be quickly deleted, and requires no
-   extra storage.
+/* Warn about duplicate methods in fn_fields.
 
    Sort methods that are not special (i.e., constructors, destructors,
    and type conversion operators) so that we can find them faster in
    search.  */
 
 static void
-finish_struct_methods (t)
-     tree t;
+finish_struct_methods (tree t)
 {
   tree fn_fields;
-  tree method_vec;
+  VEC(tree,gc) *method_vec;
   int slot, len;
 
-  if (!TYPE_METHODS (t))
-    {
-      /* Clear these for safety; perhaps some parsing error could set
-        these incorrectly.  */
-      TYPE_HAS_CONSTRUCTOR (t) = 0;
-      TYPE_HAS_DESTRUCTOR (t) = 0;
-      CLASSTYPE_METHOD_VEC (t) = NULL_TREE;
-      return;
-    }
-
   method_vec = CLASSTYPE_METHOD_VEC (t);
-  my_friendly_assert (method_vec != NULL_TREE, 19991215);
-  len = TREE_VEC_LENGTH (method_vec);
+  if (!method_vec)
+    return;
+
+  len = VEC_length (tree, method_vec);
 
-  /* First fill in entry 0 with the constructors, entry 1 with destructors,
-     and the next few with type conversion operators (if any).  */
-  for (fn_fields = TYPE_METHODS (t); fn_fields; 
+  /* Clear DECL_IN_AGGR_P for all functions.  */
+  for (fn_fields = TYPE_METHODS (t); fn_fields;
        fn_fields = TREE_CHAIN (fn_fields))
-    /* Clear out this flag.  */
     DECL_IN_AGGR_P (fn_fields) = 0;
 
-  if (TYPE_HAS_DESTRUCTOR (t) && !CLASSTYPE_DESTRUCTORS (t))
-    /* We thought there was a destructor, but there wasn't.  Some
-       parse errors cause this anomalous situation.  */
-    TYPE_HAS_DESTRUCTOR (t) = 0;
-    
   /* Issue warnings about private constructors and such.  If there are
      no methods, then some public defaults are generated.  */
   maybe_warn_about_overly_private_class (t);
 
-  /* Now sort the methods.  */
-  while (len > 2 && TREE_VEC_ELT (method_vec, len-1) == NULL_TREE)
-    len--;
-  TREE_VEC_LENGTH (method_vec) = len;
-
   /* The type conversion ops have to live at the front of the vec, so we
      can't sort them.  */
-  for (slot = 2; slot < len; ++slot)
-    {
-      tree fn = TREE_VEC_ELT (method_vec, slot);
-  
-      if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
-       break;
-    }
+  for (slot = CLASSTYPE_FIRST_CONVERSION_SLOT;
+       VEC_iterate (tree, method_vec, slot, fn_fields);
+       ++slot)
+    if (!DECL_CONV_FN_P (OVL_CURRENT (fn_fields)))
+      break;
   if (len - slot > 1)
-    qsort (&TREE_VEC_ELT (method_vec, slot), len-slot, sizeof (tree),
-          (int (*)(const void *, const void *))method_name_cmp);
-}
-
-/* Emit error when a duplicate definition of a type is seen.  Patch up.  */
-
-void
-duplicate_tag_error (t)
-     tree t;
-{
-  error ("redefinition of `%#T'", t);
-  cp_error_at ("previous definition of `%#T'", t);
-
-  /* Pretend we haven't defined this type.  */
-
-  /* All of the component_decl's were TREE_CHAINed together in the parser.
-     finish_struct_methods walks these chains and assembles all methods with
-     the same base name into DECL_CHAINs. Now we don't need the parser chains
-     anymore, so we unravel them.  */
-
-  /* This used to be in finish_struct, but it turns out that the
-     TREE_CHAIN is used by dbxout_type_methods and perhaps some other
-     things...  */
-  if (CLASSTYPE_METHOD_VEC (t)) 
-    {
-      tree method_vec = CLASSTYPE_METHOD_VEC (t);
-      int i, len  = TREE_VEC_LENGTH (method_vec);
-      for (i = 0; i < len; i++)
-       {
-         tree unchain = TREE_VEC_ELT (method_vec, i);
-         while (unchain != NULL_TREE) 
-           {
-             TREE_CHAIN (OVL_CURRENT (unchain)) = NULL_TREE;
-             unchain = OVL_NEXT (unchain);
-           }
-       }
-    }
-
-  if (TYPE_LANG_SPECIFIC (t))
-    {
-      tree binfo = TYPE_BINFO (t);
-      int interface_only = CLASSTYPE_INTERFACE_ONLY (t);
-      int interface_unknown = CLASSTYPE_INTERFACE_UNKNOWN (t);
-      tree template_info = CLASSTYPE_TEMPLATE_INFO (t);
-      int use_template = CLASSTYPE_USE_TEMPLATE (t);
-
-      memset ((char *) TYPE_LANG_SPECIFIC (t), 0, sizeof (struct lang_type));
-      BINFO_BASETYPES(binfo) = NULL_TREE;
-
-      TYPE_BINFO (t) = binfo;
-      CLASSTYPE_INTERFACE_ONLY (t) = interface_only;
-      SET_CLASSTYPE_INTERFACE_UNKNOWN_X (t, interface_unknown);
-      TYPE_REDEFINED (t) = 1;
-      CLASSTYPE_TEMPLATE_INFO (t) = template_info;
-      CLASSTYPE_USE_TEMPLATE (t) = use_template;
-    }
-  TYPE_SIZE (t) = NULL_TREE;
-  TYPE_MODE (t) = VOIDmode;
-  TYPE_FIELDS (t) = NULL_TREE;
-  TYPE_METHODS (t) = NULL_TREE;
-  TYPE_VFIELD (t) = NULL_TREE;
-  TYPE_CONTEXT (t) = NULL_TREE;
-  
-  /* Clear TYPE_LANG_FLAGS -- those in TYPE_LANG_SPECIFIC are cleared above.  */
-  TYPE_LANG_FLAG_0 (t) = 0;
-  TYPE_LANG_FLAG_1 (t) = 0;
-  TYPE_LANG_FLAG_2 (t) = 0;
-  TYPE_LANG_FLAG_3 (t) = 0;
-  TYPE_LANG_FLAG_4 (t) = 0;
-  TYPE_LANG_FLAG_5 (t) = 0;
-  TYPE_LANG_FLAG_6 (t) = 0;
-  /* But not this one.  */
-  SET_IS_AGGR_TYPE (t, 1);
+    qsort (VEC_address (tree, method_vec) + slot,
+          len-slot, sizeof (tree), method_name_cmp);
 }
 
 /* Make BINFO's vtable have N entries, including RTTI entries,
-   vbase and vcall offsets, etc.  Set its type and call the backend
+   vbase and vcall offsets, etc.  Set its type and call the back end
    to lay it out.  */
 
 static void
-layout_vtable_decl (binfo, n)
-     tree binfo;
-     int n;
+layout_vtable_decl (tree binfo, int n)
 {
   tree atype;
   tree vtable;
 
-  atype = build_cplus_array_type (vtable_entry_type, 
+  atype = build_cplus_array_type (vtable_entry_type,
                                  build_index_type (size_int (n - 1)));
   layout_type (atype);
 
@@ -2124,11 +1787,6 @@ layout_vtable_decl (binfo, n)
       TREE_TYPE (vtable) = atype;
       DECL_SIZE (vtable) = DECL_SIZE_UNIT (vtable) = NULL_TREE;
       layout_decl (vtable, 0);
-
-      /* At one time the vtable info was grabbed 2 words at a time.  This
-        fails on Sparc unless you have 8-byte alignment.  */
-      DECL_ALIGN (vtable) = MAX (TYPE_ALIGN (double_type_node),
-                                DECL_ALIGN (vtable));
     }
 }
 
@@ -2136,8 +1794,7 @@ layout_vtable_decl (binfo, n)
    have the same signature.  */
 
 int
-same_signature_p (fndecl, base_fndecl)
-     tree fndecl, base_fndecl;
+same_signature_p (const_tree fndecl, const_tree base_fndecl)
 {
   /* One destructor overrides another if they are the same kind of
      destructor.  */
@@ -2151,7 +1808,11 @@ same_signature_p (fndecl, base_fndecl)
   if (DECL_DESTRUCTOR_P (base_fndecl) || DECL_DESTRUCTOR_P (fndecl))
     return 0;
 
-  if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl))
+  if (DECL_NAME (fndecl) == DECL_NAME (base_fndecl)
+      || (DECL_CONV_FN_P (fndecl)
+         && DECL_CONV_FN_P (base_fndecl)
+         && same_type_p (DECL_CONV_FN_TYPE (fndecl),
+                         DECL_CONV_FN_TYPE (base_fndecl))))
     {
       tree types, base_types;
       types = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
@@ -2164,183 +1825,126 @@ same_signature_p (fndecl, base_fndecl)
   return 0;
 }
 
+/* Returns TRUE if DERIVED is a binfo containing the binfo BASE as a
+   subobject.  */
+
+static bool
+base_derived_from (tree derived, tree base)
+{
+  tree probe;
+
+  for (probe = base; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
+    {
+      if (probe == derived)
+       return true;
+      else if (BINFO_VIRTUAL_P (probe))
+       /* If we meet a virtual base, we can't follow the inheritance
+          any more.  See if the complete type of DERIVED contains
+          such a virtual base.  */
+       return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (derived))
+               != NULL_TREE);
+    }
+  return false;
+}
+
 typedef struct find_final_overrider_data_s {
   /* The function for which we are trying to find a final overrider.  */
   tree fn;
   /* The base class in which the function was declared.  */
   tree declaring_base;
-  /* The most derived class in the hierarchy.  */
-  tree most_derived_type;
-  /* The final overriding function.  */
-  tree overriding_fn;
-  /* The functions that we thought might be final overriders, but
-     aren't.  */
+  /* The candidate overriders.  */
   tree candidates;
-  /* The BINFO for the class in which the final overriding function
-     appears.  */
-  tree overriding_base;
+  /* Path to most derived.  */
+  VEC(tree,heap) *path;
 } find_final_overrider_data;
 
-/* Called from find_final_overrider via dfs_walk.  */
+/* Add the overrider along the current path to FFOD->CANDIDATES.
+   Returns true if an overrider was found; false otherwise.  */
 
-static tree
-dfs_find_final_overrider (binfo, data)
-     tree binfo;
-     void *data;
+static bool
+dfs_find_final_overrider_1 (tree binfo,
+                           find_final_overrider_data *ffod,
+                           unsigned depth)
 {
-  find_final_overrider_data *ffod = (find_final_overrider_data *) data;
+  tree method;
 
-  if (same_type_p (BINFO_TYPE (binfo), 
-                  BINFO_TYPE (ffod->declaring_base))
-      && tree_int_cst_equal (BINFO_OFFSET (binfo),
-                            BINFO_OFFSET (ffod->declaring_base)))
-    {
-      tree path;
-      tree method;
-
-      /* We haven't found an overrider yet.  */
-      method = NULL_TREE;
-      /* We've found a path to the declaring base.  Walk down the path
-        looking for an overrider for FN.  */
-      for (path = reverse_path (binfo);
-          path; 
-          path = TREE_CHAIN (path))
+  /* If BINFO is not the most derived type, try a more derived class.
+     A definition there will overrider a definition here.  */
+  if (depth)
+    {
+      depth--;
+      if (dfs_find_final_overrider_1
+         (VEC_index (tree, ffod->path, depth), ffod, depth))
+       return true;
+    }
+
+  method = look_for_overrides_here (BINFO_TYPE (binfo), ffod->fn);
+  if (method)
+    {
+      tree *candidate = &ffod->candidates;
+
+      /* Remove any candidates overridden by this new function.  */
+      while (*candidate)
        {
-         method = look_for_overrides_here (BINFO_TYPE (TREE_VALUE (path)),
-                                           ffod->fn);
-         if (method)
-           break;
+         /* If *CANDIDATE overrides METHOD, then METHOD
+            cannot override anything else on the list.  */
+         if (base_derived_from (TREE_VALUE (*candidate), binfo))
+           return true;
+         /* If METHOD overrides *CANDIDATE, remove *CANDIDATE.  */
+         if (base_derived_from (binfo, TREE_VALUE (*candidate)))
+           *candidate = TREE_CHAIN (*candidate);
+         else
+           candidate = &TREE_CHAIN (*candidate);
        }
 
-      /* If we found an overrider, record the overriding function, and
-        the base from which it came.  */
-      if (path)
-       {
-         tree base;
-
-         /* Assume the path is non-virtual.  See if there are any
-            virtual bases from (but not including) the overrider up
-            to and including the base where the function is
-            defined. */
-         for (base = TREE_CHAIN (path); base; base = TREE_CHAIN (base))
-           if (TREE_VIA_VIRTUAL (TREE_VALUE (base)))
-             {
-               base = ffod->declaring_base;
-               break;
-             }
+      /* Add the new function.  */
+      ffod->candidates = tree_cons (method, binfo, ffod->candidates);
+      return true;
+    }
 
-         /* 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)
-           {
-             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
-               {
-                 /* 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
-           {
-             /* 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;
-                   }
+  return false;
+}
 
-                 /* If the METHOD doesn't override the candidate,
-                    then it is incomporable.  */
-                 if (!strictly_overrides (method,
-                                          TREE_VALUE (candidates)))
-                   incomparable = true;
-               }
+/* Called from find_final_overrider via dfs_walk.  */
 
-             /* 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);
-           }
-       }
-    }
+static tree
+dfs_find_final_overrider_pre (tree binfo, void *data)
+{
+  find_final_overrider_data *ffod = (find_final_overrider_data *) data;
+
+  if (binfo == ffod->declaring_base)
+    dfs_find_final_overrider_1 (binfo, ffod, VEC_length (tree, ffod->path));
+  VEC_safe_push (tree, heap, ffod->path, binfo);
+
+  return NULL_TREE;
+}
+
+static tree
+dfs_find_final_overrider_post (tree binfo ATTRIBUTE_UNUSED, void *data)
+{
+  find_final_overrider_data *ffod = (find_final_overrider_data *) data;
+  VEC_pop (tree, ffod->path);
 
   return NULL_TREE;
 }
 
 /* Returns a TREE_LIST whose TREE_PURPOSE is the final overrider for
    FN and whose TREE_VALUE is the binfo for the base where the
-   overriding occurs.  BINFO (in the hierarchy dominated by T) is the
-   base object in which FN is declared.  */
+   overriding occurs.  BINFO (in the hierarchy dominated by the binfo
+   DERIVED) is the base object in which FN is declared.  */
 
 static tree
-find_final_overrider (t, binfo, fn)
-     tree t;
-     tree binfo;
-     tree fn;
+find_final_overrider (tree derived, tree binfo, tree 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 (); };
        struct U : public S, public T { };
 
-     even though calling `f' in `U' is ambiguous.  But, 
+     even though calling `f' in `U' is ambiguous.  But,
 
        struct R { virtual void f(); };
        struct S : virtual public R { virtual void f (); };
@@ -2348,65 +1952,66 @@ find_final_overrider (t, binfo, fn)
        struct U : public S, public T { };
 
      is not -- there's no way to decide whether to put `S::f' or
-     `T::f' in the vtable for `R'.  
-     
+     `T::f' in the vtable for `R'.
+
      The solution is to look at all paths to BINFO.  If we find
      different overriders along any two, then there is a problem.  */
+  if (DECL_THUNK_P (fn))
+    fn = THUNK_TARGET (fn);
+
+  /* Determine the depth of the hierarchy.  */
   ffod.fn = fn;
   ffod.declaring_base = binfo;
-  ffod.most_derived_type = t;
-  ffod.overriding_fn = NULL_TREE;
-  ffod.overriding_base = NULL_TREE;
   ffod.candidates = NULL_TREE;
+  ffod.path = VEC_alloc (tree, heap, 30);
 
-  dfs_walk (TYPE_BINFO (t),
-           dfs_find_final_overrider,
-           NULL,
-           &ffod);
+  dfs_walk_all (derived, dfs_find_final_overrider_pre,
+               dfs_find_final_overrider_post, &ffod);
+
+  VEC_free (tree, heap, ffod.path);
 
   /* 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;
-    }
+  if (!ffod.candidates || TREE_CHAIN (ffod.candidates))
+    return error_mark_node;
 
-  return build_tree_list (ffod.overriding_fn, ffod.overriding_base);
+  return ffod.candidates;
 }
 
-/* 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.  */
+/* Return the index of the vcall offset for FN when TYPE is used as a
+   virtual base.  */
 
-static tree get_matching_virtual PARAMS ((tree, tree));
 static tree
-get_matching_virtual (t, fn)
-     tree t, fn;
+get_vcall_index (tree fn, tree type)
 {
-  tree f;
+  VEC(tree_pair_s,gc) *indices = CLASSTYPE_VCALL_INDICES (type);
+  tree_pair_p p;
+  unsigned ix;
 
-  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;
+  for (ix = 0; VEC_iterate (tree_pair_s, indices, ix, p); ix++)
+    if ((DECL_DESTRUCTOR_P (fn) && DECL_DESTRUCTOR_P (p->purpose))
+       || same_signature_p (fn, p->purpose))
+      return p->value;
+
+  /* There should always be an appropriate index.  */
+  gcc_unreachable ();
 }
 
 /* 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
+   dominated by T.  FN has been overridden in BINFO; VIRTUALS points to the
    corresponding position in the BINFO_VIRTUALS list.  */
 
 static void
-update_vtable_entry_for_fn (t, binfo, fn, virtuals)
-     tree t;
-     tree binfo;
-     tree fn;
-     tree *virtuals;
+update_vtable_entry_for_fn (tree t, tree binfo, tree fn, tree* virtuals,
+                           unsigned ix)
 {
   tree b;
   tree overrider;
   tree delta;
   tree virtual_base;
   tree first_defn;
+  tree overrider_fn, overrider_target;
+  tree target_fn = DECL_THUNK_P (fn) ? THUNK_TARGET (fn) : fn;
+  tree over_return, base_return;
   bool lost = false;
 
   /* Find the nearest primary base (possibly binfo itself) which defines
@@ -2414,7 +2019,8 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
      calling FN through BINFO.  */
   for (b = binfo; ; b = get_primary_binfo (b))
     {
-      if (look_for_overrides_here (BINFO_TYPE (b), fn))
+      gcc_assert (b);
+      if (look_for_overrides_here (BINFO_TYPE (b), target_fn))
        break;
 
       /* The nearest definition is from a lost primary.  */
@@ -2424,13 +2030,117 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
   first_defn = b;
 
   /* Find the final overrider.  */
-  overrider = find_final_overrider (t, b, fn);
+  overrider = find_final_overrider (TYPE_BINFO (t), b, target_fn);
   if (overrider == error_mark_node)
-    return;
+    {
+      error ("no unique final overrider for %qD in %qT", target_fn, t);
+      return;
+    }
+  overrider_target = overrider_fn = TREE_PURPOSE (overrider);
 
-  /* Check for unsupported covariant returns again now that we've
-     calculated the base offsets.  */
-  check_final_overrider (TREE_PURPOSE (overrider), fn);
+  /* Check for adjusting covariant return types.  */
+  over_return = TREE_TYPE (TREE_TYPE (overrider_target));
+  base_return = TREE_TYPE (TREE_TYPE (target_fn));
+
+  if (POINTER_TYPE_P (over_return)
+      && TREE_CODE (over_return) == TREE_CODE (base_return)
+      && CLASS_TYPE_P (TREE_TYPE (over_return))
+      && CLASS_TYPE_P (TREE_TYPE (base_return))
+      /* If the overrider is invalid, don't even try.  */
+      && !DECL_INVALID_OVERRIDER_P (overrider_target))
+    {
+      /* If FN is a covariant thunk, we must figure out the adjustment
+        to the final base FN was converting to. As OVERRIDER_TARGET might
+        also be converting to the return type of FN, we have to
+        combine the two conversions here.  */
+      tree fixed_offset, virtual_offset;
+
+      over_return = TREE_TYPE (over_return);
+      base_return = TREE_TYPE (base_return);
+
+      if (DECL_THUNK_P (fn))
+       {
+         gcc_assert (DECL_RESULT_THUNK_P (fn));
+         fixed_offset = ssize_int (THUNK_FIXED_OFFSET (fn));
+         virtual_offset = THUNK_VIRTUAL_OFFSET (fn);
+       }
+      else
+       fixed_offset = virtual_offset = NULL_TREE;
+
+      if (virtual_offset)
+       /* Find the equivalent binfo within the return type of the
+          overriding function. We will want the vbase offset from
+          there.  */
+       virtual_offset = binfo_for_vbase (BINFO_TYPE (virtual_offset),
+                                         over_return);
+      else if (!same_type_ignoring_top_level_qualifiers_p
+              (over_return, base_return))
+       {
+         /* There was no existing virtual thunk (which takes
+            precedence).  So find the binfo of the base function's
+            return type within the overriding function's return type.
+            We cannot call lookup base here, because we're inside a
+            dfs_walk, and will therefore clobber the BINFO_MARKED
+            flags.  Fortunately we know the covariancy is valid (it
+            has already been checked), so we can just iterate along
+            the binfos, which have been chained in inheritance graph
+            order.  Of course it is lame that we have to repeat the
+            search here anyway -- we should really be caching pieces
+            of the vtable and avoiding this repeated work.  */
+         tree thunk_binfo, base_binfo;
+
+         /* Find the base binfo within the overriding function's
+            return type.  We will always find a thunk_binfo, except
+            when the covariancy is invalid (which we will have
+            already diagnosed).  */
+         for (base_binfo = TYPE_BINFO (base_return),
+              thunk_binfo = TYPE_BINFO (over_return);
+              thunk_binfo;
+              thunk_binfo = TREE_CHAIN (thunk_binfo))
+           if (SAME_BINFO_TYPE_P (BINFO_TYPE (thunk_binfo),
+                                  BINFO_TYPE (base_binfo)))
+             break;
+
+         /* See if virtual inheritance is involved.  */
+         for (virtual_offset = thunk_binfo;
+              virtual_offset;
+              virtual_offset = BINFO_INHERITANCE_CHAIN (virtual_offset))
+           if (BINFO_VIRTUAL_P (virtual_offset))
+             break;
+
+         if (virtual_offset
+             || (thunk_binfo && !BINFO_OFFSET_ZEROP (thunk_binfo)))
+           {
+             tree offset = convert (ssizetype, BINFO_OFFSET (thunk_binfo));
+
+             if (virtual_offset)
+               {
+                 /* We convert via virtual base.  Adjust the fixed
+                    offset to be from there.  */
+                 offset = size_diffop
+                   (offset, convert
+                    (ssizetype, BINFO_OFFSET (virtual_offset)));
+               }
+             if (fixed_offset)
+               /* There was an existing fixed offset, this must be
+                  from the base just converted to, and the base the
+                  FN was thunking to.  */
+               fixed_offset = size_binop (PLUS_EXPR, fixed_offset, offset);
+             else
+               fixed_offset = offset;
+           }
+       }
+
+      if (fixed_offset || virtual_offset)
+       /* Replace the overriding function with a covariant thunk.  We
+          will emit the overriding function in its own slot as
+          well.  */
+       overrider_fn = make_thunk (overrider_target, /*this_adjusting=*/0,
+                                  fixed_offset, virtual_offset);
+    }
+  else
+    gcc_assert (DECL_INVALID_OVERRIDER_P (overrider_target) ||
+               !DECL_THUNK_P (fn));
 
   /* Assume that we will produce a thunk that convert all the way to
      the final overrider, and not to an intermediate virtual base.  */
@@ -2442,26 +2152,58 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
     {
       /* If we find the final overrider, then we can stop
         walking.  */
-      if (same_type_p (BINFO_TYPE (b), 
-                      BINFO_TYPE (TREE_VALUE (overrider))))
+      if (SAME_BINFO_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 (first_defn) and the final overrider.  */
-      if (!virtual_base && TREE_VIA_VIRTUAL (b))
-       virtual_base = b;
+      if (BINFO_VIRTUAL_P (b))
+       {
+         virtual_base = b;
+         break;
+       }
+    }
+
+  if (overrider_fn != overrider_target && !virtual_base)
+    {
+      /* The ABI specifies that a covariant thunk includes a mangling
+        for a this pointer adjustment.  This-adjusting thunks that
+        override a function from a virtual base have a vcall
+        adjustment.  When the virtual base in question is a primary
+        virtual base, we know the adjustments are zero, (and in the
+        non-covariant case, we would not use the thunk).
+        Unfortunately we didn't notice this could happen, when
+        designing the ABI and so never mandated that such a covariant
+        thunk should be emitted.  Because we must use the ABI mandated
+        name, we must continue searching from the binfo where we
+        found the most recent definition of the function, towards the
+        primary binfo which first introduced the function into the
+        vtable.  If that enters a virtual base, we must use a vcall
+        this-adjusting thunk.  Bleah! */
+      tree probe = first_defn;
+
+      while ((probe = get_primary_binfo (probe))
+            && (unsigned) list_length (BINFO_VIRTUALS (probe)) > ix)
+       if (BINFO_VIRTUAL_P (probe))
+         virtual_base = probe;
+
+      if (virtual_base)
+       /* Even if we find a virtual base, the correct delta is
+          between the overrider and the binfo we're building a vtable
+          for.  */
+       goto virtual_covariant;
     }
 
   /* 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 from the declaration to
        the nearest virtual base.  */
-    delta = size_diffop (BINFO_OFFSET (virtual_base),
-                        BINFO_OFFSET (first_defn));
+    delta = size_diffop (convert (ssizetype, BINFO_OFFSET (virtual_base)),
+                        convert (ssizetype, 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,
@@ -2469,85 +2211,64 @@ update_vtable_entry_for_fn (t, binfo, fn, virtuals)
        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)),
-                          BINFO_OFFSET (binfo));
+    /* The `this' pointer needs to be adjusted from pointing to
+       BINFO to pointing at the base where the final overrider
+       appears.  */
+    virtual_covariant:
+    delta = size_diffop (convert (ssizetype,
+                                 BINFO_OFFSET (TREE_VALUE (overrider))),
+                        convert (ssizetype, 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, 
-                      TREE_PURPOSE (overrider),
-                      delta,
-                      virtuals);
+  modify_vtable_entry (t, binfo, overrider_fn, delta, virtuals);
 
   if (virtual_base)
-    BV_USE_VCALL_INDEX_P (*virtuals) = 1;
+    BV_VCALL_INDEX (*virtuals)
+      = get_vcall_index (overrider_target, BINFO_TYPE (virtual_base));
+  else
+    BV_VCALL_INDEX (*virtuals) = NULL_TREE;
 }
 
 /* Called from modify_all_vtables via dfs_walk.  */
 
 static tree
-dfs_modify_vtables (binfo, data)
-     tree binfo;
-     void *data;
+dfs_modify_vtables (tree binfo, 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.
-        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)))
-    {
-      tree t;
-      tree virtuals;
-      tree old_virtuals;
+  tree t = (tree) data;
+  tree virtuals;
+  tree old_virtuals;
+  unsigned ix;
+
+  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
+    /* A base without a vtable needs no modification, and its bases
+       are uninteresting.  */
+    return dfs_skip_bases;
+
+  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t)
+      && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
+    /* Don't do the primary vtable, if it's new.  */
+    return NULL_TREE;
 
-      t = (tree) data;
+  if (BINFO_PRIMARY_P (binfo) && !BINFO_VIRTUAL_P (binfo))
+    /* There's no need to modify the vtable for a non-virtual 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.  */
+    return NULL_TREE;
 
-      make_new_vtable (t, binfo);
-      
-      /* Now, go through each of the virtual functions in the virtual
-        function table for BINFO.  Find the final overrider, and
-        update the BINFO_VIRTUALS list appropriately.  */
-      for (virtuals = BINFO_VIRTUALS (binfo),
-            old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
-          virtuals;
-          virtuals = TREE_CHAIN (virtuals),
-            old_virtuals = TREE_CHAIN (old_virtuals))
-       update_vtable_entry_for_fn (t, 
-                                   binfo, 
-                                   BV_FN (old_virtuals),
-                                   &virtuals);
-    }
-
-  SET_BINFO_MARKED (binfo);
+  make_new_vtable (t, binfo);
+
+  /* Now, go through each of the virtual functions in the virtual
+     function table for BINFO.  Find the final overrider, and update
+     the BINFO_VIRTUALS list appropriately.  */
+  for (ix = 0, virtuals = BINFO_VIRTUALS (binfo),
+        old_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
+       virtuals;
+       ix++, virtuals = TREE_CHAIN (virtuals),
+        old_virtuals = TREE_CHAIN (old_virtuals))
+    update_vtable_entry_for_fn (t,
+                               binfo,
+                               BV_FN (old_virtuals),
+                               &virtuals, ix);
 
   return NULL_TREE;
 }
@@ -2562,20 +2283,13 @@ dfs_modify_vtables (binfo, data)
    should therefore be appended to the end of the vtable for T.  */
 
 static tree
-modify_all_vtables (t, vfuns_p, virtuals)
-     tree t;
-     int *vfuns_p;
-     tree virtuals;
+modify_all_vtables (tree t, tree virtuals)
 {
   tree binfo = TYPE_BINFO (t);
   tree *fnsp;
 
   /* Update all of the vtables.  */
-  dfs_walk (binfo, 
-           dfs_modify_vtables, 
-           dfs_unmarked_real_bases_queue_p,
-           t);
-  dfs_walk (binfo, dfs_unmark, dfs_marked_real_bases_queue_p, t);
+  dfs_walk_once (binfo, dfs_modify_vtables, NULL, t);
 
   /* Add virtual functions not already in our primary vtable. These
      will be both those introduced by this class, and those overridden
@@ -2588,12 +2302,6 @@ modify_all_vtables (t, vfuns_p, virtuals)
       if (!value_member (fn, BINFO_VIRTUALS (binfo))
          || DECL_VINDEX (fn) == error_mark_node)
        {
-         /* 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;
@@ -2606,52 +2314,41 @@ modify_all_vtables (t, vfuns_p, virtuals)
        /* We've already got an entry for this function.  Skip it.  */
        *fnsp = TREE_CHAIN (*fnsp);
     }
-  
-  return virtuals;
-}
-
-/* Here, we already know that they match in every respect.
-   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;
-{
-  base_kind kind;
-  
-  return (lookup_base (DECL_CONTEXT (fndecl1), DECL_CONTEXT (fndecl2),
-                      ba_ignore | ba_quiet, &kind)
-         && kind != bk_same_type);
+  return virtuals;
 }
 
 /* Get the base virtual function declarations in T that have the
    indicated NAME.  */
 
 static tree
-get_basefndecls (name, t)
-     tree name, t;
+get_basefndecls (tree name, tree t)
 {
   tree methods;
   tree base_fndecls = NULL_TREE;
-  int n_baseclasses = CLASSTYPE_N_BASECLASSES (t);
+  int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
   int i;
 
-  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);
+  /* Find virtual functions in T with the indicated NAME.  */
+  i = lookup_fnfields_1 (t, name);
+  if (i != -1)
+    for (methods = VEC_index (tree, CLASSTYPE_METHOD_VEC (t), i);
+        methods;
+        methods = OVL_NEXT (methods))
+      {
+       tree method = OVL_CURRENT (methods);
+
+       if (TREE_CODE (method) == FUNCTION_DECL
+           && DECL_VINDEX (method))
+         base_fndecls = tree_cons (NULL_TREE, method, base_fndecls);
+      }
 
   if (base_fndecls)
     return base_fndecls;
 
   for (i = 0; i < n_baseclasses; i++)
     {
-      tree basetype = TYPE_BINFO_BASETYPE (t, i);
+      tree basetype = BINFO_TYPE (BINFO_BASE_BINFO (TYPE_BINFO (t), i));
       base_fndecls = chainon (get_basefndecls (name, basetype),
                              base_fndecls);
     }
@@ -2663,18 +2360,18 @@ get_basefndecls (name, t)
    a method declared virtual in the base class, then
    mark this field as being virtual as well.  */
 
-static void
-check_for_override (decl, ctype)
-     tree decl, ctype;
+void
+check_for_override (tree decl, tree ctype)
 {
   if (TREE_CODE (decl) == TEMPLATE_DECL)
     /* In [temp.mem] we have:
 
-         A specialization of a member function template does not
-         override a virtual function from a base class.  */
+        A specialization of a member function template does not
+        override a virtual function from a base class.  */
     return;
   if ((DECL_DESTRUCTOR_P (decl)
-       || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl)))
+       || IDENTIFIER_VIRTUAL_P (DECL_NAME (decl))
+       || DECL_CONV_FN_P (decl))
       && look_for_overrides (ctype, decl)
       && !DECL_STATIC_FUNCTION_P (decl))
     /* Set DECL_VINDEX to a value that is neither an INTEGER_CST nor
@@ -2693,50 +2390,54 @@ check_for_override (decl, ctype)
 /* Warn about hidden virtual functions that are not overridden in t.
    We know that constructors and destructors don't apply.  */
 
-void
-warn_hidden (t)
-     tree t;
+static void
+warn_hidden (tree t)
 {
-  tree method_vec = CLASSTYPE_METHOD_VEC (t);
-  int n_methods = method_vec ? TREE_VEC_LENGTH (method_vec) : 0;
-  int i;
+  VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (t);
+  tree fns;
+  size_t i;
 
   /* We go through each separately named virtual function.  */
-  for (i = 2; i < n_methods && TREE_VEC_ELT (method_vec, i); ++i)
+  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
+       VEC_iterate (tree, method_vec, i, fns);
+       ++i)
     {
-      tree fns;
+      tree fn;
       tree name;
       tree fndecl;
       tree base_fndecls;
+      tree base_binfo;
+      tree binfo;
       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)));
+      name = DECL_NAME (OVL_CURRENT (fns));
       /* 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++)
+      for (binfo = TYPE_BINFO (t), j = 0;
+          BINFO_BASE_ITERATE (binfo, j, base_binfo); j++)
        {
-         tree basetype = TYPE_BINFO_BASETYPE (t, j);
+         tree basetype = BINFO_TYPE (base_binfo);
          base_fndecls = chainon (get_basefndecls (name, basetype),
                                  base_fndecls);
        }
 
-      /* If there are no functions to hide, continue. */
+      /* If there are no functions to hide, continue.  */
       if (!base_fndecls)
        continue;
 
-      /* Remove any overridden functions. */
-      for (fns = TREE_VEC_ELT (method_vec, i); fns; fns = OVL_NEXT (fns))
+      /* Remove any overridden functions.  */
+      for (fn = fns; fn; fn = OVL_NEXT (fn))
        {
-         fndecl = OVL_CURRENT (fns);
+         fndecl = OVL_CURRENT (fn);
          if (DECL_VINDEX (fndecl))
            {
              tree *prev = &base_fndecls;
-             
-             while (*prev) 
+
+             while (*prev)
                /* If the method from the base class has the same
                   signature as the method from the derived class, it
                   has been overridden.  */
@@ -2749,12 +2450,11 @@ warn_hidden (t)
 
       /* Now give a warning for all base functions without overriders,
         as they are hidden.  */
-      while (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)));
+         warning (OPT_Woverloaded_virtual, "%q+D was hidden", TREE_VALUE (base_fndecls));
+         warning (OPT_Woverloaded_virtual, "  by %q+D", fns);
          base_fndecls = TREE_CHAIN (base_fndecls);
        }
     }
@@ -2764,8 +2464,7 @@ warn_hidden (t)
    things we should check for also.  */
 
 static void
-finish_struct_anon (t)
-     tree t;
+finish_struct_anon (tree t)
 {
   tree field;
 
@@ -2779,6 +2478,7 @@ finish_struct_anon (t)
       if (DECL_NAME (field) == NULL_TREE
          && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
        {
+         bool is_union = TREE_CODE (TREE_TYPE (field)) == UNION_TYPE;
          tree elt = TYPE_FIELDS (TREE_TYPE (field));
          for (; elt; elt = TREE_CHAIN (elt))
            {
@@ -2789,28 +2489,36 @@ finish_struct_anon (t)
                 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) 
+             if (DECL_ARTIFICIAL (elt)
                  && (!DECL_IMPLICIT_TYPEDEF_P (elt)
                      || TYPE_ANONYMOUS_P (TREE_TYPE (elt))))
                continue;
 
-             if (DECL_NAME (elt) == constructor_name (t))
-               cp_pedwarn_at ("ISO C++ forbids member `%D' with same name as enclosing class",
-                              elt);
-
              if (TREE_CODE (elt) != FIELD_DECL)
                {
-                 cp_pedwarn_at ("`%#D' invalid; an anonymous union can only have non-static data members",
-                                elt);
+                 if (is_union)
+                   permerror (input_location, "%q+#D invalid; an anonymous union can "
+                              "only have non-static data members", elt);
+                 else
+                   permerror (input_location, "%q+#D invalid; an anonymous struct can "
+                              "only have non-static data members", elt);
                  continue;
                }
 
              if (TREE_PRIVATE (elt))
-               cp_pedwarn_at ("private member `%#D' in anonymous union",
-                              elt);
+               {
+                 if (is_union)
+                   permerror (input_location, "private member %q+#D in anonymous union", elt);
+                 else
+                   permerror (input_location, "private member %q+#D in anonymous struct", elt);
+               }
              else if (TREE_PROTECTED (elt))
-               cp_pedwarn_at ("protected member `%#D' in anonymous union",
-                              elt);
+               {
+                 if (is_union)
+                   permerror (input_location, "protected member %q+#D in anonymous union", elt);
+                 else
+                   permerror (input_location, "protected member %q+#D in anonymous struct", elt);
+               }
 
              TREE_PRIVATE (elt) = TREE_PRIVATE (field);
              TREE_PROTECTED (elt) = TREE_PROTECTED (field);
@@ -2819,101 +2527,124 @@ finish_struct_anon (t)
     }
 }
 
-/* Create default constructors, assignment operators, and so forth for
-   the type indicated by T, if they are needed.
-   CANT_HAVE_DEFAULT_CTOR, CANT_HAVE_CONST_CTOR, and
-   CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason, the
-   class cannot have a default constructor, copy constructor taking a
-   const reference argument, or an assignment operator taking a const
-   reference, respectively.  If a virtual destructor is created, its
-   DECL is returned; otherwise the return value is NULL_TREE.  */
+/* Add T to CLASSTYPE_DECL_LIST of current_class_type which
+   will be used later during class template instantiation.
+   When FRIEND_P is zero, T can be a static member data (VAR_DECL),
+   a non-static member data (FIELD_DECL), a member function
+   (FUNCTION_DECL), a nested type (RECORD_TYPE, ENUM_TYPE),
+   a typedef (TYPE_DECL) or a member class template (TEMPLATE_DECL)
+   When FRIEND_P is nonzero, T is either a friend class
+   (RECORD_TYPE, TEMPLATE_DECL) or a friend function
+   (FUNCTION_DECL, TEMPLATE_DECL).  */
 
-static tree
-add_implicitly_declared_members (t, cant_have_default_ctor,
-                                cant_have_const_cctor,
-                                cant_have_const_assignment)
-     tree t;
-     int cant_have_default_ctor;
-     int cant_have_const_cctor;
-     int cant_have_const_assignment;
+void
+maybe_add_class_template_decl_list (tree type, tree t, int friend_p)
 {
-  tree default_fn;
-  tree implicit_fns = NULL_TREE;
-  tree virtual_dtor = NULL_TREE;
-  tree *f;
+  /* Save some memory by not creating TREE_LIST if TYPE is not template.  */
+  if (CLASSTYPE_TEMPLATE_INFO (type))
+    CLASSTYPE_DECL_LIST (type)
+      = tree_cons (friend_p ? NULL_TREE : type,
+                  t, CLASSTYPE_DECL_LIST (type));
+}
 
-  ++adding_implicit_members;
+/* Create default constructors, assignment operators, and so forth for
+   the type indicated by T, if they are needed.  CANT_HAVE_CONST_CTOR,
+   and CANT_HAVE_CONST_ASSIGNMENT are nonzero if, for whatever reason,
+   the class cannot have a default constructor, copy constructor
+   taking a const reference argument, or an assignment operator taking
+   a const reference, respectively.  */
 
+static void
+add_implicitly_declared_members (tree t,
+                                int cant_have_const_cctor,
+                                int cant_have_const_assignment)
+{
   /* Destructor.  */
-  if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) && !TYPE_HAS_DESTRUCTOR (t))
+  if (!CLASSTYPE_DESTRUCTORS (t))
     {
-      default_fn = implicitly_declare_fn (sfk_destructor, t, /*const_p=*/0);
-      check_for_override (default_fn, t);
-
-      /* If we couldn't make it work, then pretend we didn't need it.  */
-      if (default_fn == void_type_node)
-       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 0;
-      else
+      /* In general, we create destructors lazily.  */
+      CLASSTYPE_LAZY_DESTRUCTOR (t) = 1;
+      /* However, if the implicit destructor is non-trivial
+        destructor, we sometimes have to create it at this point.  */
+      if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t))
        {
-         TREE_CHAIN (default_fn) = implicit_fns;
-         implicit_fns = default_fn;
+         bool lazy_p = true;
+
+         if (TYPE_FOR_JAVA (t))
+           /* If this a Java class, any non-trivial destructor is
+              invalid, even if compiler-generated.  Therefore, if the
+              destructor is non-trivial we create it now.  */
+           lazy_p = false;
+         else
+           {
+             tree binfo;
+             tree base_binfo;
+             int ix;
+
+             /* If the implicit destructor will be virtual, then we must
+                generate it now because (unfortunately) we do not
+                generate virtual tables lazily.  */
+             binfo = TYPE_BINFO (t);
+             for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
+               {
+                 tree base_type;
+                 tree dtor;
+
+                 base_type = BINFO_TYPE (base_binfo);
+                 dtor = CLASSTYPE_DESTRUCTORS (base_type);
+                 if (dtor && DECL_VIRTUAL_P (dtor))
+                   {
+                     lazy_p = false;
+                     break;
+                   }
+               }
+           }
 
-         if (DECL_VINDEX (default_fn))
-           virtual_dtor = default_fn;
+         /* If we can't get away with being lazy, generate the destructor
+            now.  */
+         if (!lazy_p)
+           lazily_declare_fn (sfk_destructor, t);
        }
     }
-  else
-    /* Any non-implicit destructor is non-trivial.  */
-    TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) |= TYPE_HAS_DESTRUCTOR (t);
 
-  /* Default constructor.  */
-  if (! TYPE_HAS_CONSTRUCTOR (t) && ! cant_have_default_ctor)
+  /* [class.ctor]
+
+     If there is no user-declared constructor for a class, a default
+     constructor is implicitly declared.  */
+  if (! TYPE_HAS_USER_CONSTRUCTOR (t))
     {
-      default_fn = implicitly_declare_fn (sfk_constructor, t, /*const_p=*/0);
-      TREE_CHAIN (default_fn) = implicit_fns;
-      implicit_fns = default_fn;
+      TYPE_HAS_DEFAULT_CONSTRUCTOR (t) = 1;
+      CLASSTYPE_LAZY_DEFAULT_CTOR (t) = 1;
     }
 
-  /* Copy constructor.  */
+  /* [class.ctor]
+
+     If a class definition does not explicitly declare a copy
+     constructor, one is declared implicitly.  */
   if (! TYPE_HAS_INIT_REF (t) && ! TYPE_FOR_JAVA (t))
     {
-      /* ARM 12.18: You get either X(X&) or X(const X&), but
-        not both.  --Chip  */
-      default_fn 
-       = implicitly_declare_fn (sfk_copy_constructor, t,
-                                /*const_p=*/!cant_have_const_cctor);
-      TREE_CHAIN (default_fn) = implicit_fns;
-      implicit_fns = default_fn;
+      TYPE_HAS_INIT_REF (t) = 1;
+      TYPE_HAS_CONST_INIT_REF (t) = !cant_have_const_cctor;
+      CLASSTYPE_LAZY_COPY_CTOR (t) = 1;
     }
 
-  /* Assignment operator.  */
-  if (! TYPE_HAS_ASSIGN_REF (t) && ! TYPE_FOR_JAVA (t))
+  /* If there is no assignment operator, one will be created if and
+     when it is needed.  For now, just record whether or not the type
+     of the parameter to the assignment operator will be a const or
+     non-const reference.  */
+  if (!TYPE_HAS_ASSIGN_REF (t) && !TYPE_FOR_JAVA (t))
     {
-      default_fn 
-       = implicitly_declare_fn (sfk_assignment_operator, t,
-                                /*const_p=*/!cant_have_const_assignment);
-      TREE_CHAIN (default_fn) = implicit_fns;
-      implicit_fns = default_fn;
+      TYPE_HAS_ASSIGN_REF (t) = 1;
+      TYPE_HAS_CONST_ASSIGN_REF (t) = !cant_have_const_assignment;
+      CLASSTYPE_LAZY_ASSIGNMENT_OP (t) = 1;
     }
-
-  /* Now, hook all of the new functions on to TYPE_METHODS,
-     and add them to the CLASSTYPE_METHOD_VEC.  */
-  for (f = &implicit_fns; *f; f = &TREE_CHAIN (*f))
-    add_method (t, *f, /*error_p=*/0);
-  *f = TYPE_METHODS (t);
-  TYPE_METHODS (t) = implicit_fns;
-
-  --adding_implicit_members;
-
-  return virtual_dtor;
 }
 
 /* Subroutine of finish_struct_1.  Recursively count the number of fields
    in TYPE, including anonymous union members.  */
 
 static int
-count_fields (fields)
-     tree fields;
+count_fields (tree fields)
 {
   tree x;
   int n_fields = 0;
@@ -2928,122 +2659,97 @@ count_fields (fields)
 }
 
 /* Subroutine of finish_struct_1.  Recursively add all the fields in the
-   TREE_LIST FIELDS to the TREE_VEC FIELD_VEC, starting at offset IDX.  */
+   TREE_LIST FIELDS to the SORTED_FIELDS_TYPE elts, starting at offset IDX.  */
 
 static int
-add_fields_to_vec (fields, field_vec, idx)
-     tree fields, field_vec;
-     int idx;
+add_fields_to_record_type (tree fields, struct sorted_fields_type *field_vec, int idx)
 {
   tree x;
   for (x = fields; x; x = TREE_CHAIN (x))
     {
       if (TREE_CODE (x) == FIELD_DECL && ANON_AGGR_TYPE_P (TREE_TYPE (x)))
-       idx = add_fields_to_vec (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
+       idx = add_fields_to_record_type (TYPE_FIELDS (TREE_TYPE (x)), field_vec, idx);
       else
-       TREE_VEC_ELT (field_vec, idx++) = x;
+       field_vec->elts[idx++] = x;
     }
   return idx;
 }
 
 /* FIELD is a bit-field.  We are finishing the processing for its
    enclosing type.  Issue any appropriate messages and set appropriate
-   flags.  */
+   flags.  Returns false if an error has been diagnosed.  */
 
-static void
-check_bitfield_decl (field)
-     tree field;
+static bool
+check_bitfield_decl (tree field)
 {
   tree type = TREE_TYPE (field);
-  tree w = NULL_TREE;
+  tree w;
+
+  /* Extract the declared width of the bitfield, which has been
+     temporarily stashed in DECL_INITIAL.  */
+  w = DECL_INITIAL (field);
+  gcc_assert (w != NULL_TREE);
+  /* Remove the bit-field width indicator so that the rest of the
+     compiler does not treat that value as an initializer.  */
+  DECL_INITIAL (field) = NULL_TREE;
 
   /* Detect invalid bit-field type.  */
-  if (DECL_INITIAL (field)
-      && ! INTEGRAL_TYPE_P (TREE_TYPE (field)))
+  if (!INTEGRAL_TYPE_P (type))
     {
-      cp_error_at ("bit-field `%#D' with non-integral type", field);
+      error ("bit-field %q+#D with non-integral type", field);
       w = error_mark_node;
     }
-
-  /* Detect and ignore out of range field width.  */
-  if (DECL_INITIAL (field))
+  else
     {
-      w = DECL_INITIAL (field);
-
       /* Avoid the non_lvalue wrapper added by fold for PLUS_EXPRs.  */
       STRIP_NOPS (w);
 
       /* detect invalid field size.  */
-      if (TREE_CODE (w) == CONST_DECL)
-       w = DECL_INITIAL (w);
-      else
-       w = decl_constant_value (w);
+      w = integral_constant_value (w);
 
       if (TREE_CODE (w) != INTEGER_CST)
        {
-         cp_error_at ("bit-field `%D' width not an integer constant",
-                      field);
+         error ("bit-field %q+D width not an integer constant", field);
          w = error_mark_node;
        }
       else if (tree_int_cst_sgn (w) < 0)
        {
-         cp_error_at ("negative width in bit-field `%D'", field);
+         error ("negative width in bit-field %q+D", field);
          w = error_mark_node;
        }
       else if (integer_zerop (w) && DECL_NAME (field) != 0)
        {
-         cp_error_at ("zero width for bit-field `%D'", field);
+         error ("zero width for bit-field %q+D", field);
          w = error_mark_node;
        }
       else if (compare_tree_int (w, TYPE_PRECISION (type)) > 0
               && TREE_CODE (type) != ENUMERAL_TYPE
               && TREE_CODE (type) != BOOLEAN_TYPE)
-       cp_warning_at ("width of `%D' exceeds its type", field);
+       warning (0, "width of %q+D exceeds its type", field);
       else if (TREE_CODE (type) == ENUMERAL_TYPE
               && (0 > compare_tree_int (w,
-                                        min_precision (TYPE_MIN_VALUE (type),
-                                                       TREE_UNSIGNED (type)))
+                                        tree_int_cst_min_precision
+                                        (TYPE_MIN_VALUE (type),
+                                         TYPE_UNSIGNED (type)))
                   ||  0 > compare_tree_int (w,
-                                            min_precision
+                                            tree_int_cst_min_precision
                                             (TYPE_MAX_VALUE (type),
-                                             TREE_UNSIGNED (type)))))
-       cp_warning_at ("`%D' is too small to hold all values of `%#T'",
-                      field, type);
+                                             TYPE_UNSIGNED (type)))))
+       warning (0, "%q+D is too small to hold all values of %q#T", field, type);
     }
-  
-  /* Remove the bit-field width indicator so that the rest of the
-     compiler does not treat that value as an initializer.  */
-  DECL_INITIAL (field) = NULL_TREE;
 
   if (w != error_mark_node)
     {
       DECL_SIZE (field) = convert (bitsizetype, w);
       DECL_BIT_FIELD (field) = 1;
-
-      if (integer_zerop (w)
-         && ! (* targetm.ms_bitfield_layout_p) (DECL_FIELD_CONTEXT (field)))
-       {
-#ifdef EMPTY_FIELD_BOUNDARY
-         DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
-                                   EMPTY_FIELD_BOUNDARY);
-#endif
-#ifdef PCC_BITFIELD_TYPE_MATTERS
-         if (PCC_BITFIELD_TYPE_MATTERS)
-           {
-             DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
-                                       TYPE_ALIGN (type));
-             DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
-           }
-#endif
-       }
+      return true;
     }
   else
     {
       /* Non-bit-fields are aligned for their type.  */
       DECL_BIT_FIELD (field) = 0;
       CLEAR_DECL_C_BIT_FIELD (field);
-      DECL_ALIGN (field) = MAX (DECL_ALIGN (field), TYPE_ALIGN (type));
-      DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (type);
+      return false;
     }
 }
 
@@ -3052,15 +2758,11 @@ check_bitfield_decl (field)
    flags.  */
 
 static void
-check_field_decl (field, t, cant_have_const_ctor,
-                 cant_have_default_ctor, no_const_asn_ref,
-                 any_default_members)
-     tree field;
-     tree t;
-     int *cant_have_const_ctor;
-     int *cant_have_default_ctor;
-     int *no_const_asn_ref;
-     int *any_default_members;
+check_field_decl (tree field,
+                 tree t,
+                 int* cant_have_const_ctor,
+                 int* no_const_asn_ref,
+                 int* any_default_members)
 {
   tree type = strip_array_types (TREE_TYPE (field));
 
@@ -3077,8 +2779,7 @@ check_field_decl (field, t, cant_have_const_ctor,
       for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
        if (TREE_CODE (fields) == FIELD_DECL && !DECL_C_BIT_FIELD (field))
          check_field_decl (fields, t, cant_have_const_ctor,
-                           cant_have_default_ctor, no_const_asn_ref,
-                           any_default_members);
+                           no_const_asn_ref, any_default_members);
     }
   /* Check members with class type for constructors, destructors,
      etc.  */
@@ -3087,26 +2788,26 @@ check_field_decl (field, t, cant_have_const_ctor,
       /* Never let anything with uninheritable virtuals
         make it through without complaint.  */
       abstract_virtuals_error (field, type);
-                     
+
       if (TREE_CODE (t) == UNION_TYPE)
        {
          if (TYPE_NEEDS_CONSTRUCTING (type))
-           cp_error_at ("member `%#D' with constructor not allowed in union",
-                        field);
+           error ("member %q+#D with constructor not allowed in union",
+                  field);
          if (TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type))
-           cp_error_at ("member `%#D' with destructor not allowed in union",
-                        field);
+           error ("member %q+#D with destructor not allowed in union", field);
          if (TYPE_HAS_COMPLEX_ASSIGN_REF (type))
-           cp_error_at ("member `%#D' with copy assignment operator not allowed in union",
-                        field);
+           error ("member %q+#D with copy assignment operator not allowed in union",
+                  field);
        }
       else
        {
          TYPE_NEEDS_CONSTRUCTING (t) |= TYPE_NEEDS_CONSTRUCTING (type);
-         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) 
+         TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
            |= TYPE_HAS_NONTRIVIAL_DESTRUCTOR (type);
          TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_HAS_COMPLEX_ASSIGN_REF (type);
          TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_HAS_COMPLEX_INIT_REF (type);
+         TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_HAS_COMPLEX_DFLT (type);
        }
 
       if (!TYPE_HAS_CONST_INIT_REF (type))
@@ -3114,28 +2815,15 @@ check_field_decl (field, t, cant_have_const_ctor,
 
       if (!TYPE_HAS_CONST_ASSIGN_REF (type))
        *no_const_asn_ref = 1;
-
-      if (TYPE_HAS_CONSTRUCTOR (type)
-         && ! TYPE_HAS_DEFAULT_CONSTRUCTOR (type))
-       *cant_have_default_ctor = 1;
     }
   if (DECL_INITIAL (field) != NULL_TREE)
     {
       /* `build_class_init_list' does not recognize
         non-FIELD_DECLs.  */
       if (TREE_CODE (t) == UNION_TYPE && any_default_members != 0)
-       cp_error_at ("multiple fields in union `%T' initialized");
+       error ("multiple fields in union %qT initialized", t);
       *any_default_members = 1;
     }
-
-  /* Non-bit-fields are aligned for their type, except packed fields
-     which require only BITS_PER_UNIT alignment.  */
-  DECL_ALIGN (field) = MAX (DECL_ALIGN (field), 
-                           (DECL_PACKED (field) 
-                            ? BITS_PER_UNIT
-                            : TYPE_ALIGN (TREE_TYPE (field))));
-  if (! DECL_PACKED (field))
-    DECL_USER_ALIGN (field) |= TYPE_USER_ALIGN (TREE_TYPE (field));
 }
 
 /* Check the data members (both static and non-static), class-scoped
@@ -3149,10 +2837,6 @@ check_field_decl (field, t, cant_have_const_ctor,
      EMPTY_P
        The class is empty, i.e., contains no non-static data members.
 
-     CANT_HAVE_DEFAULT_CTOR_P
-       This class cannot have an implicitly generated default
-       constructor.
-
      CANT_HAVE_CONST_CTOR_P
        This class cannot have an implicitly generated copy constructor
        taking a const reference.
@@ -3168,28 +2852,20 @@ check_field_decl (field, t, cant_have_const_ctor,
    fields can be added by adding to this chain.  */
 
 static void
-check_field_decls (t, access_decls, empty_p, 
-                  cant_have_default_ctor_p, cant_have_const_ctor_p,
-                  no_const_asn_ref_p)
-     tree t;
-     tree *access_decls;
-     int *empty_p;
-     int *cant_have_default_ctor_p;
-     int *cant_have_const_ctor_p;
-     int *no_const_asn_ref_p;
+check_field_decls (tree t, tree *access_decls,
+                  int *cant_have_const_ctor_p,
+                  int *no_const_asn_ref_p)
 {
   tree *field;
   tree *next;
-  int has_pointers;
+  bool has_pointers;
   int any_default_members;
-
-  /* First, delete any duplicate fields.  */
-  delete_duplicate_fields (TYPE_FIELDS (t));
+  int cant_pack = 0;
 
   /* Assume there are no access declarations.  */
   *access_decls = NULL_TREE;
   /* Assume this class has no pointer members.  */
-  has_pointers = 0;
+  has_pointers = false;
   /* Assume none of the members of this class have default
      initializations.  */
   any_default_members = 0;
@@ -3201,31 +2877,6 @@ check_field_decls (t, access_decls, empty_p,
 
       next = &TREE_CHAIN (x);
 
-      if (TREE_CODE (x) == FIELD_DECL)
-       {
-         DECL_PACKED (x) |= TYPE_PACKED (t);
-
-         if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
-           /* We don't treat zero-width bitfields as making a class
-              non-empty.  */
-           ;
-         else
-           {
-             tree element_type;
-
-             /* The class is non-empty.  */
-             *empty_p = 0;
-             /* The class is not even nearly empty.  */
-             CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
-             /* If one of the data members contains an empty class,
-                so does T.  */
-             element_type = strip_array_types (type);
-             if (CLASS_TYPE_P (element_type) 
-                 && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (element_type))
-               CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
-           }
-       }
-
       if (TREE_CODE (x) == USING_DECL)
        {
          /* Prune the access declaration from the list of fields.  */
@@ -3246,62 +2897,60 @@ check_field_decls (t, access_decls, empty_p,
 
       /* If we've gotten this far, it's a data member, possibly static,
         or an enumerator.  */
-
       DECL_CONTEXT (x) = t;
 
-      /* ``A local class cannot have static data members.'' ARM 9.4 */
-      if (current_function_decl && TREE_STATIC (x))
-       cp_error_at ("field `%D' in local class cannot be static", x);
+      /* When this goes into scope, it will be a non-local reference.  */
+      DECL_NONLOCAL (x) = 1;
+
+      if (TREE_CODE (t) == UNION_TYPE)
+       {
+         /* [class.union]
+
+            If a union contains a static data member, or a member of
+            reference type, the program is ill-formed.  */
+         if (TREE_CODE (x) == VAR_DECL)
+           {
+             error ("%q+D may not be static because it is a member of a union", x);
+             continue;
+           }
+         if (TREE_CODE (type) == REFERENCE_TYPE)
+           {
+             error ("%q+D may not have reference type %qT because"
+                    " it is a member of a union",
+                    x, type);
+             continue;
+           }
+       }
 
       /* Perform error checking that did not get done in
         grokdeclarator.  */
       if (TREE_CODE (type) == FUNCTION_TYPE)
        {
-         cp_error_at ("field `%D' invalidly declared function type",
-                      x);
+         error ("field %q+D invalidly declared function type", x);
          type = build_pointer_type (type);
          TREE_TYPE (x) = type;
        }
       else if (TREE_CODE (type) == METHOD_TYPE)
        {
-         cp_error_at ("field `%D' invalidly declared method type", x);
-         type = build_pointer_type (type);
-         TREE_TYPE (x) = type;
-       }
-      else if (TREE_CODE (type) == OFFSET_TYPE)
-       {
-         cp_error_at ("field `%D' invalidly declared offset type", x);
+         error ("field %q+D invalidly declared method type", x);
          type = build_pointer_type (type);
          TREE_TYPE (x) = type;
        }
 
       if (type == error_mark_node)
        continue;
-         
-      /* When this goes into scope, it will be a non-local reference.  */
-      DECL_NONLOCAL (x) = 1;
 
-      if (TREE_CODE (x) == CONST_DECL)
+      if (TREE_CODE (x) == CONST_DECL || TREE_CODE (x) == VAR_DECL)
        continue;
 
-      if (TREE_CODE (x) == VAR_DECL)
-       {
-         if (TREE_CODE (t) == UNION_TYPE)
-           /* Unions cannot have static members.  */
-           cp_error_at ("field `%D' declared static in union", x);
-             
-         continue;
-       }
-
       /* Now it can only be a FIELD_DECL.  */
 
       if (TREE_PRIVATE (x) || TREE_PROTECTED (x))
        CLASSTYPE_NON_AGGREGATE (t) = 1;
 
-      /* If this is of reference type, check if it needs an init.
-        Also do a little ANSI jig if necessary.  */
+      /* If this is of reference type, check if it needs an init.  */
       if (TREE_CODE (type) == REFERENCE_TYPE)
-       {
+       {
          CLASSTYPE_NON_POD_P (t) = 1;
          if (DECL_INITIAL (x) == NULL_TREE)
            SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
@@ -3310,24 +2959,65 @@ check_field_decls (t, access_decls, empty_p,
             aggregate, initialization by a brace-enclosed list) is the
             only way to initialize nonstatic const and reference
             members.  */
-         *cant_have_default_ctor_p = 1;
          TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
-
-         if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
-            cp_warning_at ("non-static reference `%#D' in class without a constructor", x);
        }
 
       type = strip_array_types (type);
-      
-      if (TREE_CODE (type) == POINTER_TYPE)
-       has_pointers = 1;
+
+      if (TYPE_PACKED (t))
+       {
+         if (!pod_type_p (type) && !TYPE_PACKED (type))
+           {
+             warning
+               (0,
+                "ignoring packed attribute because of unpacked non-POD field %q+#D",
+                x);
+             cant_pack = 1;
+           }
+         else if (DECL_C_BIT_FIELD (x)
+                  || TYPE_ALIGN (TREE_TYPE (x)) > BITS_PER_UNIT)
+           DECL_PACKED (x) = 1;
+       }
+
+      if (DECL_C_BIT_FIELD (x) && integer_zerop (DECL_INITIAL (x)))
+       /* We don't treat zero-width bitfields as making a class
+          non-empty.  */
+       ;
+      else
+       {
+         /* The class is non-empty.  */
+         CLASSTYPE_EMPTY_P (t) = 0;
+         /* The class is not even nearly empty.  */
+         CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
+         /* If one of the data members contains an empty class,
+            so does T.  */
+         if (CLASS_TYPE_P (type)
+             && CLASSTYPE_CONTAINS_EMPTY_CLASS_P (type))
+           CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
+       }
+
+      /* This is used by -Weffc++ (see below). Warn only for pointers
+        to members which might hold dynamic memory. So do not warn
+        for pointers to functions or pointers to members.  */
+      if (TYPE_PTR_P (type)
+         && !TYPE_PTRFN_P (type)
+         && !TYPE_PTR_TO_MEMBER_P (type))
+       has_pointers = true;
+
+      if (CLASS_TYPE_P (type))
+       {
+         if (CLASSTYPE_REF_FIELDS_NEED_INIT (type))
+           SET_CLASSTYPE_REF_FIELDS_NEED_INIT (t, 1);
+         if (CLASSTYPE_READONLY_FIELDS_NEED_INIT (type))
+           SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t, 1);
+       }
 
       if (DECL_MUTABLE_P (x) || TYPE_HAS_MUTABLE_P (type))
        CLASSTYPE_HAS_MUTABLE (t) = 1;
 
       if (! pod_type_p (type))
-        /* DR 148 now allows pointers to members (which are POD themselves),
-           to be allowed in POD structs.  */
+       /* DR 148 now allows pointers to members (which are POD themselves),
+          to be allowed in POD structs.  */
        CLASSTYPE_NON_POD_P (t) = 1;
 
       if (! zero_init_p (type))
@@ -3344,14 +3034,10 @@ check_field_decls (t, access_decls, empty_p,
             aggregate, initialization by a brace-enclosed list) is the
             only way to initialize nonstatic const and reference
             members.  */
-         *cant_have_default_ctor_p = 1;
          TYPE_HAS_COMPLEX_ASSIGN_REF (t) = 1;
-
-         if (! TYPE_HAS_CONSTRUCTOR (t) && extra_warnings)
-            cp_warning_at ("non-static const member `%#D' in class without a constructor", x);
        }
       /* A field that is pseudo-const makes the structure likewise.  */
-      else if (IS_AGGR_TYPE (type))
+      else if (CLASS_TYPE_P (type))
        {
          C_TYPE_FIELDS_READONLY (t) |= C_TYPE_FIELDS_READONLY (type);
          SET_CLASSTYPE_READONLY_FIELDS_NEED_INIT (t,
@@ -3361,39 +3047,57 @@ check_field_decls (t, access_decls, empty_p,
 
       /* Core issue 80: A nonstatic data member is required to have a
         different name from the class iff the class has a
-        user-defined constructor.  */
-      if (DECL_NAME (x) == constructor_name (t)
-         && TYPE_HAS_CONSTRUCTOR (t))
-       cp_pedwarn_at ("field `%#D' with same name as class", x);
+        user-declared constructor.  */
+      if (constructor_name_p (DECL_NAME (x), t)
+         && TYPE_HAS_USER_CONSTRUCTOR (t))
+       permerror (input_location, "field %q+#D with same name as class", x);
 
       /* We set DECL_C_BIT_FIELD in grokbitfield.
         If the type and width are valid, we'll also set DECL_BIT_FIELD.  */
-      if (DECL_C_BIT_FIELD (x))
-       check_bitfield_decl (x);
-      else
+      if (! DECL_C_BIT_FIELD (x) || ! check_bitfield_decl (x))
        check_field_decl (x, t,
                          cant_have_const_ctor_p,
-                         cant_have_default_ctor_p, 
                          no_const_asn_ref_p,
                          &any_default_members);
     }
 
-  /* Effective C++ rule 11.  */
-  if (has_pointers && warn_ecpp && TYPE_HAS_CONSTRUCTOR (t)
-      && ! (TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
+  /* Effective C++ rule 11: if a class has dynamic memory held by pointers,
+     it should also define a copy constructor and an assignment operator to
+     implement the correct copy semantic (deep vs shallow, etc.). As it is
+     not feasible to check whether the constructors do allocate dynamic memory
+     and store it within members, we approximate the warning like this:
+
+     -- Warn only if there are members which are pointers
+     -- Warn only if there is a non-trivial constructor (otherwise,
+       there cannot be memory allocated).
+     -- Warn only if there is a non-trivial destructor. We assume that the
+       user at least implemented the cleanup correctly, and a destructor
+       is needed to free dynamic memory.
+
+     This seems enough for practical purposes.  */
+  if (warn_ecpp
+      && has_pointers
+      && TYPE_HAS_USER_CONSTRUCTOR (t)
+      && TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t)
+      && !(TYPE_HAS_INIT_REF (t) && TYPE_HAS_ASSIGN_REF (t)))
     {
-      warning ("`%#T' has pointer data members", t);
-      
+      warning (OPT_Weffc__, "%q#T has pointer data members", t);
+
       if (! TYPE_HAS_INIT_REF (t))
        {
-         warning ("  but does not override `%T(const %T&)'", t, t);
-         if (! TYPE_HAS_ASSIGN_REF (t))
-           warning ("  or `operator=(const %T&)'", t);
+         warning (OPT_Weffc__,
+                  "  but does not override %<%T(const %T&)%>", t, t);
+         if (!TYPE_HAS_ASSIGN_REF (t))
+           warning (OPT_Weffc__, "  or %<operator=(const %T&)%>", t);
        }
       else if (! TYPE_HAS_ASSIGN_REF (t))
-       warning ("  but does not override `operator=(const %T&)'", t);
+       warning (OPT_Weffc__,
+                "  but does not override %<operator=(const %T&)%>", t);
     }
 
+  /* If any of the fields couldn't be packed, unset TYPE_PACKED.  */
+  if (cant_pack)
+    TYPE_PACKED (t) = 0;
 
   /* Check anonymous struct/anonymous union fields.  */
   finish_struct_anon (t);
@@ -3407,10 +3111,7 @@ check_field_decls (t, access_decls, empty_p,
    OFFSETS.  */
 
 static int
-record_subobject_offset (type, offset, offsets)
-     tree type;
-     tree offset;
-     splay_tree offsets;
+record_subobject_offset (tree type, tree offset, splay_tree offsets)
 {
   splay_tree_node n;
 
@@ -3420,10 +3121,10 @@ record_subobject_offset (type, offset, offsets)
   /* Record the location of this empty object in OFFSETS.  */
   n = splay_tree_lookup (offsets, (splay_tree_key) offset);
   if (!n)
-    n = splay_tree_insert (offsets, 
+    n = splay_tree_insert (offsets,
                           (splay_tree_key) offset,
                           (splay_tree_value) NULL_TREE);
-  n->value = ((splay_tree_value) 
+  n->value = ((splay_tree_value)
              tree_cons (NULL_TREE,
                         type,
                         (tree) n->value));
@@ -3431,14 +3132,11 @@ record_subobject_offset (type, offset, offsets)
   return 0;
 }
 
-/* Returns non-zero if TYPE is an empty class type and there is
+/* Returns nonzero if TYPE is an empty class type and there is
    already an entry in OFFSETS for the same TYPE as the same OFFSET.  */
 
 static int
-check_subobject_offset (type, offset, offsets)
-     tree type;
-     tree offset;
-     splay_tree offsets;
+check_subobject_offset (tree type, tree offset, splay_tree offsets)
 {
   splay_tree_node n;
   tree t;
@@ -3460,34 +3158,45 @@ 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.  
+   OFFSETS.  If VBASES_P is one, then virtual non-primary bases should
+   be traversed.
 
    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
+   If F returns a nonzero value, the traversal ceases, and that value
    is returned.  Otherwise, returns zero.  */
 
 static int
-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;
+walk_subobject_offsets (tree type,
+                       subobject_offset_fn f,
+                       tree offset,
+                       splay_tree offsets,
+                       tree max_offset,
+                       int vbases_p)
 {
   int r = 0;
+  tree type_binfo = NULL_TREE;
 
   /* 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 (type == error_mark_node)
+    return 0;
+
+  if (!TYPE_P (type))
+    {
+      if (abi_version_at_least (2))
+       type_binfo = type;
+      type = BINFO_TYPE (type);
+    }
+
   if (CLASS_TYPE_P (type))
     {
       tree field;
+      tree binfo;
       int i;
 
       /* Avoid recursing into objects that are not interesting.  */
@@ -3500,36 +3209,110 @@ walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
        return r;
 
       /* Iterate through the direct base classes of TYPE.  */
-      for (i = 0; i < CLASSTYPE_N_BASECLASSES (type); ++i)
+      if (!type_binfo)
+       type_binfo = TYPE_BINFO (type);
+      for (i = 0; BINFO_BASE_ITERATE (type_binfo, i, binfo); i++)
        {
-         tree binfo = BINFO_BASETYPE (TYPE_BINFO (type), i);
+         tree binfo_offset;
+
+         if (abi_version_at_least (2)
+             && BINFO_VIRTUAL_P (binfo))
+           continue;
 
-         if (!vbases_p 
-             && TREE_VIA_VIRTUAL (binfo) 
+         if (!vbases_p
+             && BINFO_VIRTUAL_P (binfo)
              && !BINFO_PRIMARY_P (binfo))
            continue;
 
-         r = walk_subobject_offsets (BINFO_TYPE (binfo),
+         if (!abi_version_at_least (2))
+           binfo_offset = size_binop (PLUS_EXPR,
+                                      offset,
+                                      BINFO_OFFSET (binfo));
+         else
+           {
+             tree orig_binfo;
+             /* We cannot rely on BINFO_OFFSET being set for the base
+                class yet, but the offsets for direct non-virtual
+                bases can be calculated by going back to the TYPE.  */
+             orig_binfo = BINFO_BASE_BINFO (TYPE_BINFO (type), i);
+             binfo_offset = size_binop (PLUS_EXPR,
+                                        offset,
+                                        BINFO_OFFSET (orig_binfo));
+           }
+
+         r = walk_subobject_offsets (binfo,
                                      f,
-                                     size_binop (PLUS_EXPR,
-                                                 offset,
-                                                 BINFO_OFFSET (binfo)),
+                                     binfo_offset,
                                      offsets,
                                      max_offset,
-                                     vbases_p);
+                                     (abi_version_at_least (2)
+                                      ? /*vbases_p=*/0 : vbases_p));
          if (r)
            return r;
        }
 
+      if (abi_version_at_least (2) && CLASSTYPE_VBASECLASSES (type))
+       {
+         unsigned ix;
+         VEC(tree,gc) *vbases;
+
+         /* Iterate through the virtual base classes of TYPE.  In G++
+            3.2, we included virtual bases in the direct base class
+            loop above, which results in incorrect results; the
+            correct offsets for virtual bases are only known when
+            working with the most derived type.  */
+         if (vbases_p)
+           for (vbases = CLASSTYPE_VBASECLASSES (type), ix = 0;
+                VEC_iterate (tree, vbases, ix, binfo); ix++)
+             {
+               r = walk_subobject_offsets (binfo,
+                                           f,
+                                           size_binop (PLUS_EXPR,
+                                                       offset,
+                                                       BINFO_OFFSET (binfo)),
+                                           offsets,
+                                           max_offset,
+                                           /*vbases_p=*/0);
+               if (r)
+                 return r;
+             }
+         else
+           {
+             /* We still have to walk the primary base, if it is
+                virtual.  (If it is non-virtual, then it was walked
+                above.)  */
+             tree vbase = get_primary_binfo (type_binfo);
+
+             if (vbase && BINFO_VIRTUAL_P (vbase)
+                 && BINFO_PRIMARY_P (vbase)
+                 && BINFO_INHERITANCE_CHAIN (vbase) == type_binfo)
+               {
+                 r = (walk_subobject_offsets
+                      (vbase, f, offset,
+                       offsets, max_offset, /*vbases_p=*/0));
+                 if (r)
+                   return r;
+               }
+           }
+       }
+
       /* Iterate through the fields of TYPE.  */
       for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
-       if (TREE_CODE (field) == FIELD_DECL)
+       if (TREE_CODE (field) == FIELD_DECL && !DECL_ARTIFICIAL (field))
          {
+           tree field_offset;
+
+           if (abi_version_at_least (2))
+             field_offset = byte_position (field);
+           else
+             /* In G++ 3.2, DECL_FIELD_OFFSET was used.  */
+             field_offset = DECL_FIELD_OFFSET (field);
+
            r = walk_subobject_offsets (TREE_TYPE (field),
                                        f,
                                        size_binop (PLUS_EXPR,
                                                    offset,
-                                                   DECL_FIELD_OFFSET (field)),
+                                                   field_offset),
                                        offsets,
                                        max_offset,
                                        /*vbases_p=*/1);
@@ -3549,8 +3332,11 @@ walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
        return 0;
 
       /* Step through each of the elements in the array.  */
-      for (index = size_zero_node; 
-          INT_CST_LT (index, TYPE_MAX_VALUE (domain));
+      for (index = size_zero_node;
+          /* G++ 3.2 had an off-by-one error here.  */
+          (abi_version_at_least (2)
+           ? !INT_CST_LT (TYPE_MAX_VALUE (domain), index)
+           : INT_CST_LT (index, TYPE_MAX_VALUE (domain)));
           index = size_binop (PLUS_EXPR, index, size_one_node))
        {
          r = walk_subobject_offsets (TREE_TYPE (type),
@@ -3561,7 +3347,7 @@ walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
                                      /*vbases_p=*/1);
          if (r)
            return r;
-         offset = size_binop (PLUS_EXPR, offset, 
+         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
@@ -3574,31 +3360,49 @@ walk_subobject_offsets (type, f, offset, offsets, max_offset, vbases_p)
   return 0;
 }
 
-/* Record all of the empty subobjects of TYPE (located at OFFSET) in
-   OFFSETS.  If VBASES_P is non-zero, virtual bases of TYPE are
-   examined.  */
+/* Record all of the empty subobjects of TYPE (either a type or a
+   binfo).  If IS_DATA_MEMBER is true, then a non-static data member
+   is being placed at OFFSET; otherwise, it is a base class that is
+   being placed at OFFSET.  */
 
 static void
-record_subobject_offsets (type, offset, offsets, vbases_p)
-     tree type;
-     tree offset;
-     splay_tree offsets;
-     int vbases_p;
-{
+record_subobject_offsets (tree type,
+                         tree offset,
+                         splay_tree offsets,
+                         bool is_data_member)
+{
+  tree max_offset;
+  /* If recording subobjects for a non-static data member or a
+     non-empty base class , we do not need to record offsets beyond
+     the size of the biggest empty class.  Additional data members
+     will go at the end of the class.  Additional base classes will go
+     either at offset zero (if empty, in which case they cannot
+     overlap with offsets past the size of the biggest empty class) or
+     at the end of the class.
+
+     However, if we are placing an empty base class, then we must record
+     all offsets, as either the empty class is at offset zero (where
+     other empty classes might later be placed) or at the end of the
+     class (where other objects might then be placed, so other empty
+     subobjects might later overlap).  */
+  if (is_data_member
+      || !is_empty_class (BINFO_TYPE (type)))
+    max_offset = sizeof_biggest_empty_class;
+  else
+    max_offset = NULL_TREE;
   walk_subobject_offsets (type, record_subobject_offset, offset,
-                         offsets, /*max_offset=*/NULL_TREE, vbases_p);
+                         offsets, max_offset, is_data_member);
 }
 
-/* Returns non-zero if any of the empty subobjects of TYPE (located at
-   OFFSET) conflict with entries in OFFSETS.  If VBASES_P is non-zero,
+/* Returns nonzero if any of the empty subobjects of TYPE (located at
+   OFFSET) conflict with entries in OFFSETS.  If VBASES_P is nonzero,
    virtual bases of TYPE are examined.  */
 
 static int
-layout_conflict_p (type, offset, offsets, vbases_p)
-     tree type;
-     tree offset;
-     splay_tree offsets;
-     int vbases_p;
+layout_conflict_p (tree type,
+                  tree offset,
+                  splay_tree offsets,
+                  int vbases_p)
 {
   splay_tree_node max_node;
 
@@ -3618,22 +3422,33 @@ layout_conflict_p (type, offset, offsets, 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.  T is the most derived
-   type.  This function determines the position of the DECL.  */
+   types already located at those offsets.  This function determines
+   the position of the DECL.  */
 
 static void
-layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
-     record_layout_info rli;
-     tree decl;
-     tree binfo;
-     splay_tree offsets;
-     tree t;
+layout_nonempty_base_or_field (record_layout_info rli,
+                              tree decl,
+                              tree binfo,
+                              splay_tree offsets)
 {
   tree offset = NULL_TREE;
-  tree type = TREE_TYPE (decl);
-  /* If we are laying out a base class, rather than a field, then
-     DECL_ARTIFICIAL will be set on the FIELD_DECL.  */
-  int field_p = !DECL_ARTIFICIAL (decl);
+  bool field_p;
+  tree type;
+
+  if (binfo)
+    {
+      /* For the purposes of determining layout conflicts, we want to
+        use the class type of BINFO; TREE_TYPE (DECL) will be the
+        CLASSTYPE_AS_BASE version, which does not contain entries for
+        zero-sized bases.  */
+      type = TREE_TYPE (binfo);
+      field_p = false;
+    }
+  else
+    {
+      type = TREE_TYPE (decl);
+      field_p = true;
+    }
 
   /* 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
@@ -3645,26 +3460,32 @@ layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
       /* Place this field.  */
       place_field (rli, decl);
       offset = byte_position (decl);
+
       /* We have to check to see whether or not there is already
         something of the same type at the offset we're about to use.
-        For example:
-        
-        struct S {};
-        struct T : public S { int i; };
-        struct U : public S, public T {};
-        
+        For example, consider:
+
+          struct S {};
+          struct T : public S { int i; };
+          struct U : public S, public T {};
+
         Here, we put S at offset zero in U.  Then, we can't put T at
         offset zero -- its S component would be at the same address
         as the S we already allocated.  So, we have to skip ahead.
         Since all data members, including those whose type is an
-        empty class, have non-zero size, any overlap can happen only
+        empty class, have nonzero size, any overlap can happen only
         with a direct or indirect base-class -- it can't happen with
         a data member.  */
-      if (layout_conflict_p (TREE_TYPE (decl),
-                            offset,
-                            offsets, 
-                            field_p))
+      /* In a union, overlap is permitted; all members are placed at
+        offset zero.  */
+      if (TREE_CODE (rli->t) == UNION_TYPE)
+       break;
+      /* G++ 3.2 did not check for overlaps when placing a non-empty
+        virtual base.  */
+      if (!abi_version_at_least (2) && binfo && BINFO_VIRTUAL_P (binfo))
+       break;
+      if (layout_conflict_p (field_p ? type : binfo, offset,
+                            offsets, field_p))
        {
          /* Strip off the size allocated to this field.  That puts us
             at the first place we could have put the field with
@@ -3673,8 +3494,8 @@ layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
 
          /* Bump up by the alignment required for the type.  */
          rli->bitpos
-           = size_binop (PLUS_EXPR, rli->bitpos, 
-                         bitsize_int (binfo 
+           = size_binop (PLUS_EXPR, rli->bitpos,
+                         bitsize_int (binfo
                                       ? CLASSTYPE_ALIGN (type)
                                       : TYPE_ALIGN (type)));
          normalize_rli (rli);
@@ -3687,167 +3508,243 @@ layout_nonempty_base_or_field (rli, decl, binfo, offsets, t)
   /* 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), t);
+    /* Indirect virtual bases may have a nonzero BINFO_OFFSET at
+       this point because their BINFO_OFFSET is copied from another
+       hierarchy.  Therefore, we may not need to add the entire
+       OFFSET.  */
+    propagate_binfo_offsets (binfo,
+                            size_diffop (convert (ssizetype, offset),
+                                         convert (ssizetype,
+                                                  BINFO_OFFSET (binfo))));
+}
+
+/* Returns true if TYPE is empty and OFFSET is nonzero.  */
+
+static int
+empty_base_at_nonzero_offset_p (tree type,
+                               tree offset,
+                               splay_tree offsets ATTRIBUTE_UNUSED)
+{
+  return is_empty_class (type) && !integer_zerop (offset);
 }
 
 /* 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. T is the most derived
-   type.  Return non-zero iff we added it at the end. */
+   type.  Return nonzero iff we added it at the end.  */
 
 static bool
-layout_empty_base (binfo, eoc, offsets, t)
-     tree binfo;
-     tree eoc;
-     splay_tree offsets;
-     tree t;
+layout_empty_base (record_layout_info rli, tree binfo,
+                  tree eoc, splay_tree offsets)
 {
   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);
+  gcc_assert (is_empty_class (basetype));
   alignment = ssize_int (CLASSTYPE_ALIGN_UNIT (basetype));
 
+  if (!integer_zerop (BINFO_OFFSET (binfo)))
+    {
+      if (abi_version_at_least (2))
+       propagate_binfo_offsets
+         (binfo, size_diffop (size_zero_node, BINFO_OFFSET (binfo)));
+      else
+       warning (OPT_Wabi,
+                "offset of empty base %qT may not be ABI-compliant and may"
+                "change in a future version of GCC",
+                BINFO_TYPE (binfo));
+    }
+
   /* This is an empty base class.  We first try to put it at offset
      zero.  */
-  if (layout_conflict_p (BINFO_TYPE (binfo),
+  if (layout_conflict_p (binfo,
                         BINFO_OFFSET (binfo),
-                        offsets, 
+                        offsets,
                         /*vbases_p=*/0))
     {
       /* That didn't work.  Now, we move forward from the next
         available spot in the class.  */
       atend = true;
-      propagate_binfo_offsets (binfo, convert (ssizetype, eoc), t);
-      while (1) 
+      propagate_binfo_offsets (binfo, convert (ssizetype, eoc));
+      while (1)
        {
-         if (!layout_conflict_p (BINFO_TYPE (binfo),
-                                 BINFO_OFFSET (binfo), 
+         if (!layout_conflict_p (binfo,
+                                 BINFO_OFFSET (binfo),
                                  offsets,
                                  /*vbases_p=*/0))
            /* We finally found a spot where there's no overlap.  */
            break;
 
          /* There's overlap here, too.  Bump along to the next spot.  */
-         propagate_binfo_offsets (binfo, alignment, t);
+         propagate_binfo_offsets (binfo, alignment);
        }
     }
+
+  if (CLASSTYPE_USER_ALIGN (basetype))
+    {
+      rli->record_align = MAX (rli->record_align, CLASSTYPE_ALIGN (basetype));
+      if (warn_packed)
+       rli->unpacked_align = MAX (rli->unpacked_align, CLASSTYPE_ALIGN (basetype));
+      TYPE_USER_ALIGN (rli->t) = 1;
+    }
+
   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.  T is
-   the most derived type.  Return non-zero if the new object cannot be
-   nearly-empty. */
+/* Layout the base given by BINFO in the class indicated by RLI.
+   *BASE_ALIGN is a running maximum of the alignments of
+   any base class.  OFFSETS gives the location of empty base
+   subobjects.  T is the most derived type.  Return nonzero if the new
+   object cannot be nearly-empty.  A new FIELD_DECL is inserted at
+   *NEXT_FIELD, unless BINFO is for an empty base class.
 
-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;
+   Returns the location at which the next field should be inserted.  */
+
+static tree *
+build_base_field (record_layout_info rli, tree binfo,
+                 splay_tree offsets, tree *next_field)
 {
+  tree t = rli->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 atend;
-  
-  decl = build_decl (FIELD_DECL, NULL_TREE, basetype);
-  DECL_ARTIFICIAL (decl) = 1;
-  DECL_FIELD_CONTEXT (decl) = rli->t;
-  DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
-  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)))
+    return next_field;
+
+  /* Place the base class.  */
+  if (!is_empty_class (basetype))
     {
+      tree decl;
+
       /* The containing class is non-empty because it has a non-empty
         base class.  */
-      *empty_p = 0;
-
-      /* 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, t);
+      CLASSTYPE_EMPTY_P (t) = 0;
+
+      /* Create the FIELD_DECL.  */
+      decl = build_decl (FIELD_DECL, NULL_TREE, CLASSTYPE_AS_BASE (basetype));
+      DECL_ARTIFICIAL (decl) = 1;
+      DECL_IGNORED_P (decl) = 1;
+      DECL_FIELD_CONTEXT (decl) = t;
+      if (CLASSTYPE_AS_BASE (basetype))
+       {
+         DECL_SIZE (decl) = CLASSTYPE_SIZE (basetype);
+         DECL_SIZE_UNIT (decl) = CLASSTYPE_SIZE_UNIT (basetype);
+         DECL_ALIGN (decl) = CLASSTYPE_ALIGN (basetype);
+         DECL_USER_ALIGN (decl) = CLASSTYPE_USER_ALIGN (basetype);
+         DECL_MODE (decl) = TYPE_MODE (basetype);
+         DECL_FIELD_IS_BASE (decl) = 1;
+
+         /* 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);
+         /* Add the new FIELD_DECL to the list of fields for T.  */
+         TREE_CHAIN (decl) = *next_field;
+         *next_field = decl;
+         next_field = &TREE_CHAIN (decl);
+       }
     }
   else
     {
-      unsigned HOST_WIDE_INT eoc;
+      tree eoc;
+      bool atend;
 
       /* On some platforms (ARM), even empty classes will not be
         byte-aligned.  */
-      eoc = tree_low_cst (rli_size_unit_so_far (rli), 0);
-      eoc = CEIL (eoc, DECL_ALIGN_UNIT (decl)) * DECL_ALIGN_UNIT (decl);
-      atend |= layout_empty_base (binfo, size_int (eoc), offsets, t);
+      eoc = round_up (rli_size_unit_so_far (rli),
+                     CLASSTYPE_ALIGN_UNIT (basetype));
+      atend = layout_empty_base (rli, binfo, eoc, offsets);
+      /* A nearly-empty class "has no proper base class that is empty,
+        not morally virtual, and at an offset other than zero."  */
+      if (!BINFO_VIRTUAL_P (binfo) && CLASSTYPE_NEARLY_EMPTY_P (t))
+       {
+         if (atend)
+           CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
+         /* The check above (used in G++ 3.2) is insufficient because
+            an empty class placed at offset zero might itself have an
+            empty base at a nonzero offset.  */
+         else if (walk_subobject_offsets (basetype,
+                                          empty_base_at_nonzero_offset_p,
+                                          size_zero_node,
+                                          /*offsets=*/NULL,
+                                          /*max_offset=*/NULL_TREE,
+                                          /*vbases_p=*/true))
+           {
+             if (abi_version_at_least (2))
+               CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
+             else
+               warning (OPT_Wabi,
+                        "class %qT will be considered nearly empty in a "
+                        "future version of GCC", t);
+           }
+       }
+
+      /* We do not create a FIELD_DECL for empty base classes because
+        it might overlap some other field.  We want to be able to
+        create CONSTRUCTORs for the class by iterating over the
+        FIELD_DECLs, and the back end does not handle overlapping
+        FIELD_DECLs.  */
+
+      /* An empty virtual base causes a class to be non-empty
+        -- but in that case we do not need to clear CLASSTYPE_EMPTY_P
+        here because that was already done when the virtual table
+        pointer was created.  */
     }
 
   /* Record the offsets of BINFO and its base subobjects.  */
-  record_subobject_offsets (BINFO_TYPE (binfo), 
+  record_subobject_offsets (binfo,
                            BINFO_OFFSET (binfo),
-                           offsets, 
-                           /*vbases_p=*/0);
-  return atend;
+                           offsets,
+                           /*is_data_member=*/false);
+
+  return next_field;
 }
 
 /* Layout all of the non-virtual base classes.  Record empty
-   subobjects in OFFSETS.  T is the most derived type.  Return
-   non-zero if the type cannot be nearly empty.  */
+   subobjects in OFFSETS.  T is the most derived type.  Return nonzero
+   if the type cannot be nearly empty.  The fields created
+   corresponding to the base classes will be inserted at
+   *NEXT_FIELD.  */
 
-static bool
-build_base_fields (rli, empty_p, offsets, t)
-     record_layout_info rli;
-     int *empty_p;
-     splay_tree offsets;
-     tree t;
+static void
+build_base_fields (record_layout_info rli,
+                  splay_tree offsets, tree *next_field)
 {
   /* 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);
+  tree t = rli->t;
+  int n_baseclasses = BINFO_N_BASE_BINFOS (TYPE_BINFO (t));
   int i;
-  bool atend = 0;
 
   /* 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, t);
+  if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
+    next_field = build_base_field (rli, CLASSTYPE_PRIMARY_BINFO (t),
+                                  offsets, next_field);
 
   /* Now allocate the rest of the bases.  */
   for (i = 0; i < n_baseclasses; ++i)
     {
       tree base_binfo;
 
-      base_binfo = BINFO_BASETYPE (TYPE_BINFO (rec), i);
+      base_binfo = BINFO_BASE_BINFO (TYPE_BINFO (t), i);
 
       /* The primary base was already allocated above, so we don't
         need to allocate it again here.  */
-      if (base_binfo == CLASSTYPE_PRIMARY_BINFO (rec))
+      if (base_binfo == CLASSTYPE_PRIMARY_BINFO (t))
        continue;
 
-      /* A primary virtual base class is allocated just like any other
-        base class, but a non-primary virtual base is allocated
-        later, in layout_virtual_bases.  */
-      if (TREE_VIA_VIRTUAL (base_binfo) 
-         && !BINFO_PRIMARY_P (base_binfo))
+      /* Virtual bases are added at the end (a primary virtual base
+        will have already been added).  */
+      if (BINFO_VIRTUAL_P (base_binfo))
        continue;
 
-      atend |= build_base_field (rli, base_binfo, empty_p, offsets, t);
+      next_field = build_base_field (rli, base_binfo,
+                                    offsets, next_field);
     }
-  return atend;
 }
 
 /* Go through the TYPE_METHODS of T issuing any appropriate
@@ -3855,31 +3752,26 @@ build_base_fields (rli, empty_p, offsets, t)
    methods, and so forth.  */
 
 static void
-check_methods (t)
-     tree t;
+check_methods (tree t)
 {
   tree x;
 
   for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (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)))
-       continue;
-
       check_for_override (x, t);
       if (DECL_PURE_VIRTUAL_P (x) && ! DECL_VINDEX (x))
-       cp_error_at ("initializer specified for non-virtual method `%D'", x);
-
+       error ("initializer specified for non-virtual method %q+D", x);
       /* The name of the field is the original field name
         Save this in auxiliary field for later overloading.  */
       if (DECL_VINDEX (x))
        {
          TYPE_POLYMORPHIC_P (t) = 1;
          if (DECL_PURE_VIRTUAL_P (x))
-           CLASSTYPE_PURE_VIRTUALS (t)
-             = tree_cons (NULL_TREE, x, CLASSTYPE_PURE_VIRTUALS (t));
+           VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
        }
+      /* All user-provided destructors are non-trivial.  */
+      if (DECL_DESTRUCTOR_P (x) && !DECL_DEFAULTED_FN (x))
+       TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t) = 1;
     }
 }
 
@@ -3888,9 +3780,7 @@ check_methods (t)
    NAME.  */
 
 static tree
-build_clone (fn, name)
-     tree fn;
-     tree name;
+build_clone (tree fn, tree name)
 {
   tree parms;
   tree clone;
@@ -3938,13 +3828,16 @@ build_clone (fn, name)
        parmtypes = TREE_CHAIN (parmtypes);
        /* If this is subobject constructor or destructor, add the vtt
         parameter.  */
-      TREE_TYPE (clone) 
-       = build_cplus_method_type (basetype,
-                                  TREE_TYPE (TREE_TYPE (clone)),
-                                  parmtypes);
+      TREE_TYPE (clone)
+       = build_method_type_directly (basetype,
+                                     TREE_TYPE (TREE_TYPE (clone)),
+                                     parmtypes);
       if (exceptions)
        TREE_TYPE (clone) = build_exception_variant (TREE_TYPE (clone),
                                                     exceptions);
+      TREE_TYPE (clone)
+       = cp_build_type_attribute_variant (TREE_TYPE (clone),
+                                          TYPE_ATTRIBUTES (TREE_TYPE (fn)));
     }
 
   /* Copy the function parameters.  But, DECL_ARGUMENTS on a TEMPLATE_DECL
@@ -3975,14 +3868,14 @@ 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);
        }
     }
 
   /* Create the RTL for this function.  */
   SET_DECL_RTL (clone, NULL_RTX);
-  rest_of_decl_compilation (clone, NULL, /*top_level=*/1, at_eof);
-  
+  rest_of_decl_compilation (clone, /*top_level=*/1, at_eof);
+
   /* Make it easy to find the CLONE given the FN.  */
   TREE_CHAIN (clone) = TREE_CHAIN (fn);
   TREE_CHAIN (fn) = clone;
@@ -3992,26 +3885,24 @@ build_clone (fn, name)
     {
       tree result;
 
-      DECL_TEMPLATE_RESULT (clone) 
+      DECL_TEMPLATE_RESULT (clone)
        = build_clone (DECL_TEMPLATE_RESULT (clone), name);
       result = DECL_TEMPLATE_RESULT (clone);
       DECL_TEMPLATE_INFO (result) = copy_node (DECL_TEMPLATE_INFO (result));
       DECL_TI_TEMPLATE (result) = clone;
     }
-  else if (DECL_DEFERRED_FN (fn))
-    defer_fn (clone);
+  else if (pch_file)
+    note_decl_for_pch (clone);
 
   return clone;
 }
 
 /* Produce declarations for all appropriate clones of FN.  If
-   UPDATE_METHOD_VEC_P is non-zero, the clones are added to the
+   UPDATE_METHOD_VEC_P is nonzero, the clones are added to the
    CLASTYPE_METHOD_VEC as well.  */
 
 void
-clone_function_decl (fn, update_method_vec_p)
-     tree fn;
-     int update_method_vec_p;
+clone_function_decl (tree fn, int update_method_vec_p)
 {
   tree clone;
 
@@ -4026,36 +3917,36 @@ clone_function_decl (fn, update_method_vec_p)
         and a not-in-charge version.  */
       clone = build_clone (fn, complete_ctor_identifier);
       if (update_method_vec_p)
-       add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
+       add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
       clone = build_clone (fn, base_ctor_identifier);
       if (update_method_vec_p)
-       add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
+       add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
     }
   else
     {
-      my_friendly_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn), 20000411);
+      gcc_assert (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fn));
 
       /* For each destructor, we need three variants: an in-charge
         version, a not-in-charge version, and an in-charge deleting
         version.  We clone the deleting version first because that
         means it will go second on the TYPE_METHODS list -- and that
         corresponds to the correct layout order in the virtual
-        function table.  
+        function table.
 
-         For a non-virtual destructor, we do not build a deleting
+        For a non-virtual destructor, we do not build a deleting
         destructor.  */
       if (DECL_VIRTUAL_P (fn))
        {
          clone = build_clone (fn, deleting_dtor_identifier);
          if (update_method_vec_p)
-           add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
+           add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
        }
       clone = build_clone (fn, complete_dtor_identifier);
       if (update_method_vec_p)
-       add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
+       add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
       clone = build_clone (fn, base_dtor_identifier);
       if (update_method_vec_p)
-       add_method (DECL_CONTEXT (clone), clone, /*error_p=*/0);
+       add_method (DECL_CONTEXT (clone), clone, NULL_TREE);
     }
 
   /* Note that this is an abstract function that is never emitted.  */
@@ -4067,14 +3958,13 @@ clone_function_decl (fn, update_method_vec_p)
    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. */
+   clones.  */
 
 void
-adjust_clone_args (decl)
-     tree decl;
+adjust_clone_args (tree decl)
 {
   tree clone;
-  
+
   for (clone = TREE_CHAIN (decl); clone && DECL_CLONED_FUNCTION (clone);
        clone = TREE_CHAIN (clone))
     {
@@ -4083,8 +3973,8 @@ adjust_clone_args (decl)
       tree decl_parms, clone_parms;
 
       clone_parms = orig_clone_parms;
-      
-      /* Skip the 'this' parameter. */
+
+      /* Skip the 'this' parameter.  */
       orig_clone_parms = TREE_CHAIN (orig_clone_parms);
       orig_decl_parms = TREE_CHAIN (orig_decl_parms);
 
@@ -4092,22 +3982,22 @@ adjust_clone_args (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);
-         
+         gcc_assert (same_type_p (TREE_TYPE (decl_parms),
+                                  TREE_TYPE (clone_parms)));
+
          if (TREE_PURPOSE (decl_parms) && !TREE_PURPOSE (clone_parms))
            {
              /* A default parameter has been added. Adjust the
-                clone's parameters. */
+                clone's parameters.  */
              tree exceptions = TYPE_RAISES_EXCEPTIONS (TREE_TYPE (clone));
              tree basetype = TYPE_METHOD_BASETYPE (TREE_TYPE (clone));
              tree type;
@@ -4121,54 +4011,166 @@ adjust_clone_args (decl)
                                           clone_parms);
                  TREE_TYPE (clone_parms) = TREE_TYPE (orig_clone_parms);
                }
-             type = build_cplus_method_type (basetype,
-                                             TREE_TYPE (TREE_TYPE (clone)),
-                                             clone_parms);
+             type = build_method_type_directly (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);
+      gcc_assert (!clone_parms);
+    }
+}
+
+/* For each of the constructors and destructors in T, create an
+   in-charge and not-in-charge variant.  */
+
+static void
+clone_constructors_and_destructors (tree t)
+{
+  tree fns;
+
+  /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
+     out now.  */
+  if (!CLASSTYPE_METHOD_VEC (t))
+    return;
+
+  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
+  for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
+}
+
+/* Returns true iff class T has a user-defined constructor other than
+   the default constructor.  */
+
+bool
+type_has_user_nondefault_constructor (tree t)
+{
+  tree fns;
+
+  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
+    return false;
+
+  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    {
+      tree fn = OVL_CURRENT (fns);
+      if (!DECL_ARTIFICIAL (fn)
+         && (TREE_CODE (fn) == TEMPLATE_DECL
+             || (skip_artificial_parms_for (fn, DECL_ARGUMENTS (fn))
+                 != NULL_TREE)))
+       return true;
     }
+
+  return false;
 }
 
-/* For each of the constructors and destructors in T, create an
-   in-charge and not-in-charge variant.  */
+/* Returns true iff FN is a user-provided function, i.e. user-declared
+   and not defaulted at its first declaration.  */
 
-static void
-clone_constructors_and_destructors (t)
-     tree t;
+static bool
+user_provided_p (tree fn)
+{
+  if (TREE_CODE (fn) == TEMPLATE_DECL)
+    return true;
+  else
+    return (!DECL_ARTIFICIAL (fn)
+           && !(DECL_DEFAULTED_FN (fn)
+                && DECL_INITIALIZED_IN_CLASS_P (fn)));
+}
+
+/* Returns true iff class T has a user-provided constructor.  */
+
+bool
+type_has_user_provided_constructor (tree t)
 {
   tree fns;
 
-  /* If for some reason we don't have a CLASSTYPE_METHOD_VEC, we bail
-     out now.  */
+  if (!CLASS_TYPE_P (t))
+    return false;
+
+  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
+    return false;
+
+  /* This can happen in error cases; avoid crashing.  */
   if (!CLASSTYPE_METHOD_VEC (t))
-    return;
+    return false;
 
   for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
-    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
-  for (fns = CLASSTYPE_DESTRUCTORS (t); fns; fns = OVL_NEXT (fns))
-    clone_function_decl (OVL_CURRENT (fns), /*update_method_vec_p=*/1);
+    if (user_provided_p (OVL_CURRENT (fns)))
+      return true;
+
+  return false;
+}
+
+/* Returns true iff class T has a user-provided default constructor.  */
+
+bool
+type_has_user_provided_default_constructor (tree t)
+{
+  tree fns, args;
+
+  if (!TYPE_HAS_USER_CONSTRUCTOR (t))
+    return false;
+
+  for (fns = CLASSTYPE_CONSTRUCTORS (t); fns; fns = OVL_NEXT (fns))
+    {
+      tree fn = OVL_CURRENT (fns);
+      if (TREE_CODE (fn) == FUNCTION_DECL
+         && user_provided_p (fn))
+       {
+         args = FUNCTION_FIRST_USER_PARMTYPE (fn);
+         while (args && TREE_PURPOSE (args))
+           args = TREE_CHAIN (args);
+         if (!args || args == void_list_node)
+           return true;
+       }
+    }
+
+  return false;
+}
+
+/* Returns true if FN can be explicitly defaulted.  */
+
+bool
+defaultable_fn_p (tree fn)
+{
+  if (DECL_CONSTRUCTOR_P (fn))
+    {
+      if (FUNCTION_FIRST_USER_PARMTYPE (fn) == void_list_node)
+       return true;
+      else if (copy_fn_p (fn) > 0
+              && (TREE_CHAIN (FUNCTION_FIRST_USER_PARMTYPE (fn))
+                  == void_list_node))
+       return true;
+      else
+       return false;
+    }
+  else if (DECL_DESTRUCTOR_P (fn))
+    return true;
+  else if (DECL_ASSIGNMENT_OPERATOR_P (fn)
+          && DECL_OVERLOADED_OPERATOR_P (fn) == NOP_EXPR)
+    return copy_fn_p (fn);
+  else
+    return false;
 }
 
 /* Remove all zero-width bit-fields from T.  */
 
 static void
-remove_zero_width_bit_fields (t)
-     tree t;
+remove_zero_width_bit_fields (tree t)
 {
   tree *fieldsp;
 
-  fieldsp = &TYPE_FIELDS (t); 
+  fieldsp = &TYPE_FIELDS (t);
   while (*fieldsp)
     {
       if (TREE_CODE (*fieldsp) == FIELD_DECL
-         && DECL_C_BIT_FIELD (*fieldsp) 
+         && DECL_C_BIT_FIELD (*fieldsp)
          && DECL_INITIAL (*fieldsp))
        *fieldsp = TREE_CHAIN (*fieldsp);
       else
@@ -4180,13 +4182,12 @@ remove_zero_width_bit_fields (t)
    array whose elements have the indicated class TYPE.  */
 
 static bool
-type_requires_array_cookie (type)
-     tree type;
+type_requires_array_cookie (tree type)
 {
   tree fns;
   bool has_two_argument_delete_p = false;
 
-  my_friendly_assert (CLASS_TYPE_P (type), 20010712);
+  gcc_assert (CLASS_TYPE_P (type));
 
   /* If there's a non-trivial destructor, we need a cookie.  In order
      to iterate through the array calling the destructor for each
@@ -4198,7 +4199,7 @@ type_requires_array_cookie (type)
      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), 
+  fns = lookup_fnfields (TYPE_BINFO (type),
                         ansi_opname (VEC_DELETE_EXPR),
                         /*protect=*/0);
   /* If there are no `operator []' members, or the lookup is
@@ -4206,7 +4207,7 @@ type_requires_array_cookie (type)
   if (!fns || fns == error_mark_node)
     return false;
   /* Loop through all of the functions.  */
-  for (fns = TREE_VALUE (fns); fns; fns = OVL_NEXT (fns))
+  for (fns = BASELINK_FUNCTIONS (fns); fns; fns = OVL_NEXT (fns))
     {
       tree fn;
       tree second_parm;
@@ -4218,11 +4219,15 @@ type_requires_array_cookie (type)
       second_parm = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (fn)));
       if (second_parm == void_list_node)
        return false;
+      /* Do not consider this function if its second argument is an
+        ellipsis.  */
+      if (!second_parm)
+       continue;
       /* 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))
+         && same_type_p (TREE_VALUE (second_parm), size_type_node))
        has_two_argument_delete_p = true;
     }
 
@@ -4236,45 +4241,44 @@ type_requires_array_cookie (type)
    level: i.e., independently of the ABI in use.  */
 
 static void
-check_bases_and_members (t, empty_p)
-     tree t;
-     int *empty_p;
+check_bases_and_members (tree t)
 {
-  /* Nonzero if we are not allowed to generate a default constructor
-     for this case.  */
-  int cant_have_default_ctor;
   /* Nonzero if the implicitly generated copy constructor should take
      a non-const reference argument.  */
   int cant_have_const_ctor;
-  /* Nonzero if the the implicitly generated assignment operator
+  /* Nonzero if the implicitly generated assignment operator
      should take a non-const reference argument.  */
   int no_const_asn_ref;
   tree access_decls;
+  bool saved_complex_asn_ref;
+  bool saved_nontrivial_dtor;
 
   /* By default, we use const reference arguments and generate default
      constructors.  */
-  cant_have_default_ctor = 0;
   cant_have_const_ctor = 0;
   no_const_asn_ref = 0;
 
-  /* Assume that the class is nearly empty; we'll clear this flag if
-     it turns out not to be nearly empty.  */
-  CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
-  CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
-
-  /* Check all the base-classes. */
-  check_bases (t, &cant_have_default_ctor, &cant_have_const_ctor,
+  /* Check all the base-classes.  */
+  check_bases (t, &cant_have_const_ctor,
               &no_const_asn_ref);
 
-  /* Check all the data member declarations.  */
-  check_field_decls (t, &access_decls, empty_p,
-                    &cant_have_default_ctor,
-                    &cant_have_const_ctor,
-                    &no_const_asn_ref);
-
   /* Check all the method declarations.  */
   check_methods (t);
 
+  /* Save the initial values of these flags which only indicate whether
+     or not the class has user-provided functions.  As we analyze the
+     bases and members we can set these flags for other reasons.  */
+  saved_complex_asn_ref = TYPE_HAS_COMPLEX_ASSIGN_REF (t);
+  saved_nontrivial_dtor = TYPE_HAS_NONTRIVIAL_DESTRUCTOR (t);
+
+  /* Check all the data member declarations.  We cannot call
+     check_field_decls until we have called check_bases check_methods,
+     as check_field_decls depends on TYPE_HAS_NONTRIVIAL_DESTRUCTOR
+     being set appropriately.  */
+  check_field_decls (t, &access_decls,
+                    &cant_have_const_ctor,
+                    &no_const_asn_ref);
+
   /* A nearly-empty class has to be vptr-containing; a nearly empty
      class contains just a vptr.  */
   if (!TYPE_CONTAINS_VPTR_P (t))
@@ -4282,26 +4286,63 @@ check_bases_and_members (t, empty_p)
 
   /* Do some bookkeeping that will guide the generation of implicitly
      declared member functions.  */
-  TYPE_HAS_COMPLEX_INIT_REF (t)
-    |= (TYPE_HAS_INIT_REF (t) 
-       || TYPE_USES_VIRTUAL_BASECLASSES (t)
-       || TYPE_POLYMORPHIC_P (t));
+  TYPE_HAS_COMPLEX_INIT_REF (t) |= TYPE_CONTAINS_VPTR_P (t);
+  /* We need to call a constructor for this class if it has a
+     user-provided constructor, or if the default constructor is going
+     to initialize the vptr.  (This is not an if-and-only-if;
+     TYPE_NEEDS_CONSTRUCTING is set elsewhere if bases or members
+     themselves need constructing.)  */
   TYPE_NEEDS_CONSTRUCTING (t)
-    |= (TYPE_HAS_CONSTRUCTOR (t) 
-       || TYPE_USES_VIRTUAL_BASECLASSES (t)
-       || TYPE_POLYMORPHIC_P (t));
-  CLASSTYPE_NON_AGGREGATE (t) |= (TYPE_HAS_CONSTRUCTOR (t)
-                                 || TYPE_POLYMORPHIC_P (t));
+    |= (type_has_user_provided_constructor (t) || TYPE_CONTAINS_VPTR_P (t));
+  /* [dcl.init.aggr]
+
+     An aggregate is an array or a class with no user-provided
+     constructors ... and no virtual functions.  
+
+     Again, other conditions for being an aggregate are checked
+     elsewhere.  */
+  CLASSTYPE_NON_AGGREGATE (t)
+    |= (type_has_user_provided_constructor (t) || TYPE_POLYMORPHIC_P (t));
   CLASSTYPE_NON_POD_P (t)
-    |= (CLASSTYPE_NON_AGGREGATE (t) || TYPE_HAS_DESTRUCTOR (t) 
-       || 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_CONTAINS_VPTR_P (t);
-
-  /* Synthesize any needed methods.  Note that methods will be synthesized
-     for anonymous unions; grok_x_components undoes that.  */
-  add_implicitly_declared_members (t, cant_have_default_ctor,
+    |= (CLASSTYPE_NON_AGGREGATE (t)
+       || saved_nontrivial_dtor || saved_complex_asn_ref);
+  TYPE_HAS_COMPLEX_ASSIGN_REF (t) |= TYPE_CONTAINS_VPTR_P (t);
+  TYPE_HAS_COMPLEX_DFLT (t) |= TYPE_CONTAINS_VPTR_P (t);
+
+  /* If the class has no user-declared constructor, but does have
+     non-static const or reference data members that can never be
+     initialized, issue a warning.  */
+  if (warn_uninitialized
+      /* Classes with user-declared constructors are presumed to
+        initialize these members.  */
+      && !TYPE_HAS_USER_CONSTRUCTOR (t)
+      /* Aggregates can be initialized with brace-enclosed
+        initializers.  */
+      && CLASSTYPE_NON_AGGREGATE (t))
+    {
+      tree field;
+
+      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
+       {
+         tree type;
+
+         if (TREE_CODE (field) != FIELD_DECL)
+           continue;
+
+         type = TREE_TYPE (field);
+         if (TREE_CODE (type) == REFERENCE_TYPE)
+           warning (OPT_Wuninitialized, "non-static reference %q+#D "
+                    "in class without a constructor", field);
+         else if (CP_TYPE_CONST_P (type)
+                  && (!CLASS_TYPE_P (type)
+                      || !TYPE_HAS_DEFAULT_CONSTRUCTOR (type)))
+           warning (OPT_Wuninitialized, "non-static const member %q+#D "
+                    "in class without a constructor", field);
+       }
+    }
+
+  /* Synthesize any needed methods.  */
+  add_implicitly_declared_members (t,
                                   cant_have_const_ctor,
                                   no_const_asn_ref);
 
@@ -4318,7 +4359,7 @@ check_bases_and_members (t, empty_p)
 
   /* Figure out whether or not we will need a cookie when dynamically
      allocating an array of this type.  */
-  TYPE_LANG_SPECIFIC (t)->vec_new_uses_cookie
+  TYPE_LANG_SPECIFIC (t)->u.c.vec_new_uses_cookie
     = type_requires_array_cookie (t);
 }
 
@@ -4330,11 +4371,7 @@ check_bases_and_members (t, empty_p)
    on VIRTUALS_P.  */
 
 static tree
-create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p)
-     tree t;
-     int *empty_p;
-     int *vfuns_p;
-     tree *virtuals_p;
+create_vtable_ptr (tree t, tree* virtuals_p)
 {
   tree fn;
 
@@ -4344,14 +4381,15 @@ create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p)
        && 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;
+       BV_VCALL_INDEX (new_virtual) = NULL_TREE;
 
        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
@@ -4360,7 +4398,7 @@ create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p)
     {
       /* We build this decl with vtbl_ptr_type_node, which is a
         `vtable_entry_type*'.  It might seem more precise to use
-        `vtable_entry_type (*)[N]' where N is the number of firtual
+        `vtable_entry_type (*)[N]' where N is the number of virtual
         functions.  However, that would require the vtable pointer in
         base classes to have a different type than the vtable pointer
         in derived classes.  We could make that happen, but that
@@ -4368,11 +4406,11 @@ create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p)
         type-based alias analysis code would decide that assignments
         to the base class vtable pointer can't alias assignments to
         the derived class vtable pointer, since they have different
-        types.  Thus, in an derived class destructor, where the base
+        types.  Thus, in a derived class destructor, where the base
         class constructor was inlined, we could generate bad code for
-        setting up the vtable pointer.  
+        setting up the vtable pointer.
 
-         Therefore, we use one type for all vtable pointers.  We still
+        Therefore, we use one type for all vtable pointers.  We still
         use a type-correct type; it's just doesn't indicate the array
         bounds.  That's better than using `void*' or some such; it's
         cleaner, and it let's the alias analysis code know that these
@@ -4380,25 +4418,15 @@ create_vtable_ptr (t, empty_p, vfuns_p, virtuals_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
-          offset zero any more, because that's where the vtable
-          pointer is.  So, converting to a base class is going to
-          take work.  */
-       TYPE_BASE_CONVS_MAY_REQUIRE_CODE_P (t) = 1;
+      /* This class is non-empty.  */
+      CLASSTYPE_EMPTY_P (t) = 0;
 
       return field;
     }
@@ -4410,8 +4438,7 @@ create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p)
    complete.  */
 
 static void
-fixup_pending_inline (fn)
-     tree fn;
+fixup_pending_inline (tree fn)
 {
   if (DECL_PENDING_INLINE_INFO (fn))
     {
@@ -4428,10 +4455,11 @@ fixup_pending_inline (fn)
    complete.  */
 
 static void
-fixup_inline_methods (type)
-     tree type;
+fixup_inline_methods (tree type)
 {
   tree method = TYPE_METHODS (type);
+  VEC(tree,gc) *friends;
+  unsigned ix;
 
   if (method && TREE_CODE (method) == TREE_VEC)
     {
@@ -4448,11 +4476,10 @@ fixup_inline_methods (type)
     fixup_pending_inline (method);
 
   /* Do friends.  */
-  for (method = CLASSTYPE_INLINE_FRIENDS (type); 
-       method; 
-       method = TREE_CHAIN (method))
-    fixup_pending_inline (TREE_VALUE (method));
-  CLASSTYPE_INLINE_FRIENDS (type) = NULL_TREE;
+  for (friends = CLASSTYPE_INLINE_FRIENDS (type), ix = 0;
+       VEC_iterate (tree, friends, ix, method); ix++)
+    fixup_pending_inline (method);
+  CLASSTYPE_INLINE_FRIENDS (type) = NULL;
 }
 
 /* Add OFFSET to all base types of BINFO which is a base in the
@@ -4461,17 +4488,15 @@ fixup_inline_methods (type)
    OFFSET, which is a type offset, is number of bytes.  */
 
 static void
-propagate_binfo_offsets (binfo, offset, t)
-     tree binfo;
-     tree offset;
-     tree t;
+propagate_binfo_offsets (tree binfo, tree offset)
 {
   int i;
   tree primary_binfo;
+  tree base_binfo;
 
   /* Update BINFO's offset.  */
   BINFO_OFFSET (binfo)
-    = convert (sizetype, 
+    = convert (sizetype,
               size_binop (PLUS_EXPR,
                           convert (ssizetype, BINFO_OFFSET (binfo)),
                           offset));
@@ -4479,150 +4504,76 @@ propagate_binfo_offsets (binfo, offset, t)
   /* Find the primary base class.  */
   primary_binfo = get_primary_binfo (binfo);
 
+  if (primary_binfo && BINFO_INHERITANCE_CHAIN (primary_binfo) == binfo)
+    propagate_binfo_offsets (primary_binfo, offset);
+
   /* Scan all of the bases, pushing the BINFO_OFFSET adjust
      downwards.  */
-  for (i = -1; i < BINFO_N_BASETYPES (binfo); ++i)
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
     {
-      tree base_binfo;
-
-      /* 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) 
-           continue;
-
-         base_binfo = primary_binfo;
-       }
-      else
-       {
-         base_binfo = BINFO_BASETYPE (binfo, i);
-         /* Don't do the primary base twice.  */
-         if (base_binfo == primary_binfo)
-           continue;
-       }
-
-      /* Skip virtual bases that aren't our canonical primary base.  */
-      if (TREE_VIA_VIRTUAL (base_binfo)
-         && (BINFO_PRIMARY_BASE_OF (base_binfo) != binfo
-             || base_binfo != binfo_for_vbase (BINFO_TYPE (base_binfo), t)))
+      /* Don't do the primary base twice.  */
+      if (base_binfo == primary_binfo)
        continue;
 
-      propagate_binfo_offsets (base_binfo, offset, t);
-    }
-}
-
-/* Called via dfs_walk from layout_virtual bases.  */
+      if (BINFO_VIRTUAL_P (base_binfo))
+       continue;
 
-static tree
-dfs_set_offset_for_unshared_vbases (binfo, data)
-     tree binfo;
-     void *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))
-    {
-      tree t = (tree) data;
-      tree vbase;
-      tree offset;
-      
-      vbase = binfo_for_vbase (BINFO_TYPE (binfo), t);
-      if (vbase != binfo)
-       {
-         offset = size_diffop (BINFO_OFFSET (vbase), BINFO_OFFSET (binfo));
-         propagate_binfo_offsets (binfo, offset, t);
-       }
+      propagate_binfo_offsets (base_binfo, offset);
     }
-
-  return NULL_TREE;
 }
 
-/* Set BINFO_OFFSET for all of the virtual bases for T.  Update
+/* Set BINFO_OFFSET for all of the virtual bases for RLI->T.  Update
    TYPE_ALIGN and TYPE_SIZE for T.  OFFSETS gives the location of
    empty subobjects of T.  */
 
 static void
-layout_virtual_bases (t, offsets)
-     tree t;
-     splay_tree offsets;
+layout_virtual_bases (record_layout_info rli, splay_tree offsets)
 {
-  tree vbases, dsize;
-  unsigned HOST_WIDE_INT eoc;
+  tree vbase;
+  tree t = rli->t;
   bool first_vbase = true;
+  tree *next_field;
 
-  if (CLASSTYPE_N_BASECLASSES (t) == 0)
+  if (BINFO_N_BASE_BINFOS (TYPE_BINFO (t)) == 0)
     return;
 
+  if (!abi_version_at_least(2))
+    {
+      /* In G++ 3.2, we incorrectly rounded the size before laying out
+        the virtual bases.  */
+      finish_record_layout (rli, /*free_p=*/false);
 #ifdef STRUCTURE_SIZE_BOUNDARY
-  /* Packed structures don't need to have minimum size.  */
-  if (! TYPE_PACKED (t))
-    TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), STRUCTURE_SIZE_BOUNDARY);
+      /* Packed structures don't need to have minimum size.  */
+      if (! TYPE_PACKED (t))
+       TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), (unsigned) STRUCTURE_SIZE_BOUNDARY);
 #endif
+      rli->offset = TYPE_SIZE_UNIT (t);
+      rli->bitpos = bitsize_zero_node;
+      rli->record_align = TYPE_ALIGN (t);
+    }
 
-  /* DSIZE is the size of the class without the virtual bases.  */
-  dsize = TYPE_SIZE (t);
-
-  /* Make every class have alignment of at least one.  */
-  TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), BITS_PER_UNIT);
+  /* Find the last field.  The artificial fields created for virtual
+     bases will go after the last extant field to date.  */
+  next_field = &TYPE_FIELDS (t);
+  while (*next_field)
+    next_field = &TREE_CHAIN (*next_field);
 
   /* Go through the virtual bases, allocating space for each virtual
      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))
+  for (vbase = TYPE_BINFO (t); vbase; vbase = TREE_CHAIN (vbase))
     {
-      tree vbase;
-
-      if (!TREE_VIA_VIRTUAL (vbases))
+      if (!BINFO_VIRTUAL_P (vbase))
        continue;
 
-      vbase = binfo_for_vbase (BINFO_TYPE (vbases), t);
-
       if (!BINFO_PRIMARY_P (vbase))
        {
+         tree basetype = TREE_TYPE (vbase);
+
          /* This virtual base is not a primary base of any class in the
             hierarchy, so we have to add space for it.  */
-         tree basetype, usize;
-         unsigned int desired_align;
-
-         basetype = BINFO_TYPE (vbase);
-
-         desired_align = CLASSTYPE_ALIGN (basetype);
-         TYPE_ALIGN (t) = MAX (TYPE_ALIGN (t), desired_align);
-
-         /* Add padding so that we can put the virtual base class at an
-            appropriately aligned offset.  */
-         dsize = round_up (dsize, desired_align);
-         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,
-                              convert (sizetype, usize),
-                              offsets, t);
-         else
-           {
-             tree offset;
-
-             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, 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 = size_binop (PLUS_EXPR, dsize,
-                                 size_binop (MAX_EXPR, bitsize_unit_node,
-                                             CLASSTYPE_SIZE (basetype)));
-           }
+         next_field = build_base_field (rli, vbase,
+                                        offsets, next_field);
 
          /* If the first virtual base might have been placed at a
             lower address, had we started from CLASSTYPE_SIZE, rather
@@ -4633,104 +4584,85 @@ layout_virtual_bases (t, offsets)
             the results which is not particularly tractable.  */
          if (warn_abi
              && first_vbase
-             && tree_int_cst_lt (size_binop (CEIL_DIV_EXPR,
-                                             round_up (CLASSTYPE_SIZE (t),
-                                                       desired_align),
-                                             bitsize_unit_node),
-                                 BINFO_OFFSET (vbase)))
-           warning ("offset of virtual base `%T' is not ABI-compliant and may change in a future version of GCC",
+             && (tree_int_cst_lt
+                 (size_binop (CEIL_DIV_EXPR,
+                              round_up (CLASSTYPE_SIZE (t),
+                                        CLASSTYPE_ALIGN (basetype)),
+                              bitsize_unit_node),
+                  BINFO_OFFSET (vbase))))
+           warning (OPT_Wabi,
+                    "offset of virtual base %qT is not ABI-compliant and "
+                    "may change in a future version of GCC",
                     basetype);
 
-         /* Keep track of the offsets assigned to this virtual base.  */
-         record_subobject_offsets (BINFO_TYPE (vbase), 
-                                   BINFO_OFFSET (vbase),
-                                   offsets,
-                                   /*vbases_p=*/0);
-
          first_vbase = false;
        }
     }
+}
 
-  /* 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 lookup_base depend on the BINFO_OFFSETs being set
-     correctly.  */
-  dfs_walk (TYPE_BINFO (t), dfs_set_offset_for_unshared_vbases, NULL, t);
+/* Returns the offset of the byte just past the end of the base class
+   BINFO.  */
 
-  /* If we had empty base classes that protruded beyond the end of the
-     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);
-  dsize = size_binop (MAX_EXPR, dsize, bitsize_int (eoc * BITS_PER_UNIT));
+static tree
+end_of_base (tree binfo)
+{
+  tree size;
 
-  /* Now, make sure that the total size of the type is a multiple of
-     its alignment.  */
-  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));
+  if (!CLASSTYPE_AS_BASE (BINFO_TYPE (binfo)))
+    size = TYPE_SIZE_UNIT (char_type_node);
+  else if (is_empty_class (BINFO_TYPE (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 (binfo));
+  else
+    size = CLASSTYPE_SIZE_UNIT (BINFO_TYPE (binfo));
 
-  /* Check for ambiguous virtual bases.  */
-  if (extra_warnings)
-    for (vbases = CLASSTYPE_VBASECLASSES (t); 
-        vbases; 
-        vbases = TREE_CHAIN (vbases))
-      {
-       tree basetype = BINFO_TYPE (TREE_VALUE (vbases));
-       
-       if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
-         warning ("virtual base `%T' inaccessible in `%T' due to ambiguity",
-                     basetype, t);
-      }
+  return size_binop (PLUS_EXPR, BINFO_OFFSET (binfo), size);
 }
 
 /* Returns the offset of the byte just past the end of the base class
    with the highest offset in T.  If INCLUDE_VIRTUALS_P is zero, then
    only non-virtual bases are included.  */
 
-static unsigned HOST_WIDE_INT
-end_of_class (t, include_virtuals_p)
-     tree t;
-     int include_virtuals_p;
+static tree
+end_of_class (tree t, int include_virtuals_p)
 {
-  unsigned HOST_WIDE_INT result = 0;
+  tree result = size_zero_node;
+  VEC(tree,gc) *vbases;
+  tree binfo;
+  tree base_binfo;
+  tree offset;
   int i;
 
-  for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
+  for (binfo = TYPE_BINFO (t), i = 0;
+       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
     {
-      tree base_binfo;
-      tree offset;
-      tree size;
-      unsigned HOST_WIDE_INT end_of_base;
-
-      base_binfo = BINFO_BASETYPE (TYPE_BINFO (t), i);
-
       if (!include_virtuals_p
-         && TREE_VIA_VIRTUAL (base_binfo) 
-         && !BINFO_PRIMARY_P (base_binfo))
+         && BINFO_VIRTUAL_P (base_binfo)
+         && (!BINFO_PRIMARY_P (base_binfo)
+             || BINFO_INHERITANCE_CHAIN (base_binfo) != TYPE_BINFO (t)))
        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),
-                          size);
-      end_of_base = tree_low_cst (offset, /*pos=*/1);
-      if (end_of_base > result)
-       result = end_of_base;
+      offset = end_of_base (base_binfo);
+      if (INT_CST_LT_UNSIGNED (result, offset))
+       result = offset;
     }
 
+  /* G++ 3.2 did not check indirect virtual bases.  */
+  if (abi_version_at_least (2) && include_virtuals_p)
+    for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
+        VEC_iterate (tree, vbases, i, base_binfo); i++)
+      {
+       offset = end_of_base (base_binfo);
+       if (INT_CST_LT_UNSIGNED (result, offset))
+         result = offset;
+      }
+
   return result;
 }
 
-/* Warn about direct bases of T that are inaccessible because they are
+/* Warn about bases of T that are inaccessible because they are
    ambiguous.  For example:
 
      struct S {};
@@ -4741,52 +4673,113 @@ end_of_class (t, include_virtuals_p)
    subobjects of U.  */
 
 static void
-warn_about_ambiguous_direct_bases (t)
-     tree t;
+warn_about_ambiguous_bases (tree t)
 {
   int i;
+  VEC(tree,gc) *vbases;
+  tree basetype;
+  tree binfo;
+  tree base_binfo;
 
-  for (i = 0; i < CLASSTYPE_N_BASECLASSES (t); ++i)
+  /* If there are no repeated bases, nothing can be ambiguous.  */
+  if (!CLASSTYPE_REPEATED_BASE_P (t))
+    return;
+
+  /* Check direct bases.  */
+  for (binfo = TYPE_BINFO (t), i = 0;
+       BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
     {
-      tree basetype = TYPE_BINFO_BASETYPE (t, i);
+      basetype = BINFO_TYPE (base_binfo);
 
-      if (!lookup_base (t, basetype, ba_ignore | ba_quiet, NULL))
-       warning ("direct base `%T' inaccessible in `%T' due to ambiguity",
-                   basetype, t);
+      if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
+       warning (0, "direct base %qT inaccessible in %qT due to ambiguity",
+                basetype, t);
     }
+
+  /* Check for ambiguous virtual bases.  */
+  if (extra_warnings)
+    for (vbases = CLASSTYPE_VBASECLASSES (t), i = 0;
+        VEC_iterate (tree, vbases, i, binfo); i++)
+      {
+       basetype = BINFO_TYPE (binfo);
+
+       if (!lookup_base (t, basetype, ba_unique | ba_quiet, NULL))
+         warning (OPT_Wextra, "virtual base %qT inaccessible in %qT due to ambiguity",
+                  basetype, t);
+      }
 }
 
 /* Compare two INTEGER_CSTs K1 and K2.  */
 
 static int
-splay_tree_compare_integer_csts (k1, k2)
-     splay_tree_key k1;
-     splay_tree_key k2;
+splay_tree_compare_integer_csts (splay_tree_key k1, splay_tree_key k2)
 {
   return tree_int_cst_compare ((tree) k1, (tree) k2);
 }
 
+/* Increase the size indicated in RLI to account for empty classes
+   that are "off the end" of the class.  */
+
+static void
+include_empty_classes (record_layout_info rli)
+{
+  tree eoc;
+  tree rli_size;
+
+  /* It might be the case that we grew the class to allocate a
+     zero-sized base class.  That won't be reflected in RLI, yet,
+     because we are willing to overlay multiple bases at the same
+     offset.  However, now we need to make sure that RLI is big enough
+     to reflect the entire class.  */
+  eoc = end_of_class (rli->t,
+                     CLASSTYPE_AS_BASE (rli->t) != NULL_TREE);
+  rli_size = rli_size_unit_so_far (rli);
+  if (TREE_CODE (rli_size) == INTEGER_CST
+      && INT_CST_LT_UNSIGNED (rli_size, eoc))
+    {
+      if (!abi_version_at_least (2))
+       /* In version 1 of the ABI, the size of a class that ends with
+          a bitfield was not rounded up to a whole multiple of a
+          byte.  Because rli_size_unit_so_far returns only the number
+          of fully allocated bytes, any extra bits were not included
+          in the size.  */
+       rli->bitpos = round_down (rli->bitpos, BITS_PER_UNIT);
+      else
+       /* The size should have been rounded to a whole byte.  */
+       gcc_assert (tree_int_cst_equal
+                   (rli->bitpos, round_down (rli->bitpos, BITS_PER_UNIT)));
+      rli->bitpos
+       = size_binop (PLUS_EXPR,
+                     rli->bitpos,
+                     size_binop (MULT_EXPR,
+                                 convert (bitsizetype,
+                                          size_binop (MINUS_EXPR,
+                                                      eoc, rli_size)),
+                                 bitsize_int (BITS_PER_UNIT)));
+      normalize_rli (rli);
+    }
+}
+
 /* Calculate the TYPE_SIZE, TYPE_ALIGN, etc for T.  Calculate
    BINFO_OFFSETs for all of the base-classes.  Position the vtable
-   pointer.  Accumulate declared virtual functions on VIRTUALS_P. */
+   pointer.  Accumulate declared virtual functions on VIRTUALS_P.  */
 
 static void
-layout_class_type (t, empty_p, vfuns_p, virtuals_p)
-     tree t;
-     int *empty_p;
-     int *vfuns_p;
-     tree *virtuals_p;
+layout_class_type (tree t, tree *virtuals_p)
 {
   tree non_static_data_members;
   tree field;
   tree vptr;
   record_layout_info rli;
-  unsigned HOST_WIDE_INT eoc;
   /* Maps offsets (represented as INTEGER_CSTs) to a TREE_LIST of
      types that appear at that offset.  */
   splay_tree empty_base_offsets;
   /* True if the last field layed out was a bit-field.  */
   bool last_field_was_bitfield = false;
+  /* The location at which the next field should be inserted.  */
+  tree *next_field;
+  /* T, as a base class.  */
+  tree base_t;
 
   /* Keep track of the first non-static data member.  */
   non_static_data_members = TYPE_FIELDS (t);
@@ -4794,26 +4787,28 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
   /* Start laying out the record.  */
   rli = start_record_layout (t);
 
-  /* If possible, we reuse the virtual function table pointer from one
-     of our base classes.  */
-  determine_primary_base (t, vfuns_p);
+  /* Mark all the primary bases in the hierarchy.  */
+  determine_primary_bases (t);
 
   /* Create a pointer to our virtual function table.  */
-  vptr = create_vtable_ptr (t, empty_p, vfuns_p, virtuals_p);
+  vptr = create_vtable_ptr (t, virtuals_p);
 
   /* The vptr is always the first thing in the class.  */
   if (vptr)
     {
-      TYPE_FIELDS (t) = chainon (vptr, TYPE_FIELDS (t));
+      TREE_CHAIN (vptr) = TYPE_FIELDS (t);
+      TYPE_FIELDS (t) = vptr;
+      next_field = &TREE_CHAIN (vptr);
       place_field (rli, vptr);
     }
+  else
+    next_field = &TYPE_FIELDS (t);
 
   /* Build FIELD_DECLs for all of the non-virtual base-types.  */
-  empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts, 
+  empty_base_offsets = splay_tree_new (splay_tree_compare_integer_csts,
                                       NULL, NULL);
-  if (build_base_fields (rli, empty_p, empty_base_offsets, t))
-    CLASSTYPE_NEARLY_EMPTY_P (t) = 0;
-  
+  build_base_fields (rli, empty_base_offsets, next_field);
+
   /* Layout the non-static data members.  */
   for (field = non_static_data_members; field; field = TREE_CHAIN (field))
     {
@@ -4821,26 +4816,36 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
       tree padding;
 
       /* We still pass things that aren't non-static data members to
-        the back-end, in case it wants to do something with them.  */
+        the back end, in case it wants to do something with them.  */
       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 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
+
+            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);
+           {
+             maybe_register_incomplete_var (field);
+             /* The visibility of static data members is determined
+                at their point of declaration, not their point of
+                definition.  */
+             determine_visibility (field);
+           }
          continue;
        }
 
       type = TREE_TYPE (field);
+      if (type == error_mark_node)
+       continue;
+
+      padding = NULL_TREE;
 
       /* If this field is a bit-field whose width is greater than its
         type, then there are some special rules for allocating
@@ -4850,13 +4855,13 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
        {
          integer_type_kind itk;
          tree integer_type;
-
+         bool was_unnamed_p = false;
          /* We must allocate the bits as if suitably aligned for the
             longest integer type that fits in this many bits.  type
             of the field.  Then, we are supposed to use the left over
             bits as additional padding.  */
          for (itk = itk_char; itk != itk_none; ++itk)
-           if (INT_CST_LT (DECL_SIZE (field), 
+           if (INT_CST_LT (DECL_SIZE (field),
                            TYPE_SIZE (integer_types[itk])))
              break;
 
@@ -4864,29 +4869,127 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
             field.  We have to back up by one to find the largest
             type that fits.  */
          integer_type = integer_types[itk - 1];
-         padding = size_binop (MINUS_EXPR, DECL_SIZE (field), 
-                               TYPE_SIZE (integer_type));
+
+         /* Figure out how much additional padding is required.  GCC
+            3.2 always created a padding field, even if it had zero
+            width.  */
+         if (!abi_version_at_least (2)
+             || INT_CST_LT (TYPE_SIZE (integer_type), DECL_SIZE (field)))
+           {
+             if (abi_version_at_least (2) && TREE_CODE (t) == UNION_TYPE)
+               /* In a union, the padding field must have the full width
+                  of the bit-field; all fields start at offset zero.  */
+               padding = DECL_SIZE (field);
+             else
+               {
+                 if (TREE_CODE (t) == UNION_TYPE)
+                   warning (OPT_Wabi, "size assigned to %qT may not be "
+                            "ABI-compliant and may change in a future "
+                            "version of GCC",
+                            t);
+                 padding = size_binop (MINUS_EXPR, DECL_SIZE (field),
+                                       TYPE_SIZE (integer_type));
+               }
+           }
+#ifdef PCC_BITFIELD_TYPE_MATTERS
+         /* An unnamed bitfield does not normally affect the
+            alignment of the containing class on a target where
+            PCC_BITFIELD_TYPE_MATTERS.  But, the C++ ABI does not
+            make any exceptions for unnamed bitfields when the
+            bitfields are longer than their types.  Therefore, we
+            temporarily give the field a name.  */
+         if (PCC_BITFIELD_TYPE_MATTERS && !DECL_NAME (field))
+           {
+             was_unnamed_p = true;
+             DECL_NAME (field) = make_anon_name ();
+           }
+#endif
          DECL_SIZE (field) = TYPE_SIZE (integer_type);
          DECL_ALIGN (field) = TYPE_ALIGN (integer_type);
          DECL_USER_ALIGN (field) = TYPE_USER_ALIGN (integer_type);
+         layout_nonempty_base_or_field (rli, field, NULL_TREE,
+                                        empty_base_offsets);
+         if (was_unnamed_p)
+           DECL_NAME (field) = NULL_TREE;
+         /* Now that layout has been performed, set the size of the
+            field to the size of its declared type; the rest of the
+            field is effectively invisible.  */
+         DECL_SIZE (field) = TYPE_SIZE (type);
+         /* We must also reset the DECL_MODE of the field.  */
+         if (abi_version_at_least (2))
+           DECL_MODE (field) = TYPE_MODE (type);
+         else if (warn_abi
+                  && DECL_MODE (field) != TYPE_MODE (type))
+           /* Versions of G++ before G++ 3.4 did not reset the
+              DECL_MODE.  */
+           warning (OPT_Wabi,
+                    "the offset of %qD may not be ABI-compliant and may "
+                    "change in a future version of GCC", field);
        }
       else
-       padding = NULL_TREE;
+       layout_nonempty_base_or_field (rli, field, NULL_TREE,
+                                      empty_base_offsets);
 
-      layout_nonempty_base_or_field (rli, field, NULL_TREE,
-                                    empty_base_offsets, t);
+      /* Remember the location of any empty classes in FIELD.  */
+      if (abi_version_at_least (2))
+       record_subobject_offsets (TREE_TYPE (field),
+                                 byte_position(field),
+                                 empty_base_offsets,
+                                 /*is_data_member=*/true);
 
       /* If a bit-field does not immediately follow another bit-field,
         and yet it starts in the middle of a byte, we have failed to
         comply with the ABI.  */
       if (warn_abi
-         && DECL_C_BIT_FIELD (field) 
+         && DECL_C_BIT_FIELD (field)
+         /* The TREE_NO_WARNING flag gets set by Objective-C when
+            laying out an Objective-C class.  The ObjC ABI differs
+            from the C++ ABI, and so we do not want a warning
+            here.  */
+         && !TREE_NO_WARNING (field)
          && !last_field_was_bitfield
          && !integer_zerop (size_binop (TRUNC_MOD_EXPR,
                                         DECL_FIELD_BIT_OFFSET (field),
                                         bitsize_unit_node)))
-       cp_warning_at ("offset of `%D' is not ABI-compliant and may change in a future version of GCC", 
-                      field);
+       warning (OPT_Wabi, "offset of %q+D is not ABI-compliant and may "
+                "change in a future version of GCC", field);
+
+      /* G++ used to use DECL_FIELD_OFFSET as if it were the byte
+        offset of the field.  */
+      if (warn_abi
+         && !tree_int_cst_equal (DECL_FIELD_OFFSET (field),
+                                 byte_position (field))
+         && contains_empty_class_p (TREE_TYPE (field)))
+       warning (OPT_Wabi, "%q+D contains empty classes which may cause base "
+                "classes to be placed at different locations in a "
+                "future version of GCC", field);
+
+      /* The middle end uses the type of expressions to determine the
+        possible range of expression values.  In order to optimize
+        "x.i > 7" to "false" for a 2-bit bitfield "i", the middle end
+        must be made aware of the width of "i", via its type.
+
+        Because C++ does not have integer types of arbitrary width,
+        we must (for the purposes of the front end) convert from the
+        type assigned here to the declared type of the bitfield
+        whenever a bitfield expression is used as an rvalue.
+        Similarly, when assigning a value to a bitfield, the value
+        must be converted to the type given the bitfield here.  */
+      if (DECL_C_BIT_FIELD (field))
+       {
+         unsigned HOST_WIDE_INT width;
+         tree ftype = TREE_TYPE (field);
+         width = tree_low_cst (DECL_SIZE (field), /*unsignedp=*/1);
+         if (width != TYPE_PRECISION (ftype))
+           {
+             TREE_TYPE (field)
+               = c_build_bitfield_integer_type (width,
+                                                TYPE_UNSIGNED (ftype));
+             TREE_TYPE (field)
+               = cp_build_qualified_type (TREE_TYPE (field),
+                                          TYPE_QUALS (ftype));
+           }
+       }
 
       /* If we needed additional padding after this field, add it
         now.  */
@@ -4894,82 +4997,115 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
        {
          tree padding_field;
 
-         padding_field = build_decl (FIELD_DECL, 
+         padding_field = build_decl (FIELD_DECL,
                                      NULL_TREE,
-                                     char_type_node); 
+                                     char_type_node);
          DECL_BIT_FIELD (padding_field) = 1;
          DECL_SIZE (padding_field) = padding;
-         DECL_ALIGN (padding_field) = 1;
-         DECL_USER_ALIGN (padding_field) = 0;
+         DECL_CONTEXT (padding_field) = t;
+         DECL_ARTIFICIAL (padding_field) = 1;
+         DECL_IGNORED_P (padding_field) = 1;
          layout_nonempty_base_or_field (rli, padding_field,
-                                        NULL_TREE, 
-                                        empty_base_offsets, t);
+                                        NULL_TREE,
+                                        empty_base_offsets);
        }
 
       last_field_was_bitfield = DECL_C_BIT_FIELD (field);
     }
 
-  /* It might be the case that we grew the class to allocate a
-     zero-sized base class.  That won't be reflected in RLI, yet,
-     because we are willing to overlay multiple bases at the same
-     offset.  However, now we need to make sure that RLI is big enough
-     to reflect the entire class.  */
-  eoc = end_of_class (t, /*include_virtuals_p=*/0);
-  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));
-      rli->bitpos = bitsize_zero_node;
-    }
-
-  /* We make all structures have at least one element, so that they
-     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)
+  if (abi_version_at_least (2) && !integer_zerop (rli->bitpos))
     {
-      tree padding;
-
-      padding = build_decl (FIELD_DECL, NULL_TREE, char_type_node);
-      place_field (rli, padding);
+      /* Make sure that we are on a byte boundary so that the size of
+        the class without virtual bases will always be a round number
+        of bytes.  */
+      rli->bitpos = round_up (rli->bitpos, BITS_PER_UNIT);
+      normalize_rli (rli);
     }
 
-  /* Let the back-end lay out the type. Note that at this point we
-     have only included non-virtual base-classes; we will lay out the
-     virtual base classes later.  So, the TYPE_SIZE/TYPE_ALIGN after
-     this call are not necessarily correct; they are just the size and
-     alignment when no virtual base clases are used.  */
-  finish_record_layout (rli);
+  /* G++ 3.2 does not allow virtual bases to be overlaid with tail
+     padding.  */
+  if (!abi_version_at_least (2))
+    include_empty_classes(rli);
 
   /* Delete all zero-width bit-fields from the list of fields.  Now
      that the type is laid out they are no longer important.  */
   remove_zero_width_bit_fields (t);
 
-  /* Remember the size and alignment of the class before adding
-     the virtual bases.  */
-  if (*empty_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))
+  /* Create the version of T used for virtual bases.  We do not use
+     make_class_type for this version; this is an artificial type.  For
+     a POD type, we just reuse T.  */
+  if (CLASSTYPE_NON_POD_P (t) || CLASSTYPE_EMPTY_P (t))
     {
-      CLASSTYPE_SIZE (t) = TYPE_SIZE (t);
-      CLASSTYPE_SIZE_UNIT (t) = TYPE_SIZE_UNIT (t);
+      base_t = make_node (TREE_CODE (t));
+
+      /* Set the size and alignment for the new type.  In G++ 3.2, all
+        empty classes were considered to have size zero when used as
+        base classes.  */
+      if (!abi_version_at_least (2) && CLASSTYPE_EMPTY_P (t))
+       {
+         TYPE_SIZE (base_t) = bitsize_zero_node;
+         TYPE_SIZE_UNIT (base_t) = size_zero_node;
+         if (warn_abi && !integer_zerop (rli_size_unit_so_far (rli)))
+           warning (OPT_Wabi,
+                    "layout of classes derived from empty class %qT "
+                    "may change in a future version of GCC",
+                    t);
+       }
+      else
+       {
+         tree eoc;
+
+         /* If the ABI version is not at least two, and the last
+            field was a bit-field, RLI may not be on a byte
+            boundary.  In particular, rli_size_unit_so_far might
+            indicate the last complete byte, while rli_size_so_far
+            indicates the total number of bits used.  Therefore,
+            rli_size_so_far, rather than rli_size_unit_so_far, is
+            used to compute TYPE_SIZE_UNIT.  */
+         eoc = end_of_class (t, /*include_virtuals_p=*/0);
+         TYPE_SIZE_UNIT (base_t)
+           = size_binop (MAX_EXPR,
+                         convert (sizetype,
+                                  size_binop (CEIL_DIV_EXPR,
+                                              rli_size_so_far (rli),
+                                              bitsize_int (BITS_PER_UNIT))),
+                         eoc);
+         TYPE_SIZE (base_t)
+           = size_binop (MAX_EXPR,
+                         rli_size_so_far (rli),
+                         size_binop (MULT_EXPR,
+                                     convert (bitsizetype, eoc),
+                                     bitsize_int (BITS_PER_UNIT)));
+       }
+      TYPE_ALIGN (base_t) = rli->record_align;
+      TYPE_USER_ALIGN (base_t) = TYPE_USER_ALIGN (t);
+
+      /* Copy the fields from T.  */
+      next_field = &TYPE_FIELDS (base_t);
+      for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
+       if (TREE_CODE (field) == FIELD_DECL)
+         {
+           *next_field = build_decl (FIELD_DECL,
+                                     DECL_NAME (field),
+                                     TREE_TYPE (field));
+           DECL_CONTEXT (*next_field) = base_t;
+           DECL_FIELD_OFFSET (*next_field) = DECL_FIELD_OFFSET (field);
+           DECL_FIELD_BIT_OFFSET (*next_field)
+             = DECL_FIELD_BIT_OFFSET (field);
+           DECL_SIZE (*next_field) = DECL_SIZE (field);
+           DECL_MODE (*next_field) = DECL_MODE (field);
+           next_field = &TREE_CHAIN (*next_field);
+         }
+
+      /* Record the base version of the type.  */
+      CLASSTYPE_AS_BASE (t) = base_t;
+      TYPE_CONTEXT (base_t) = t;
     }
   else
-    {
-      CLASSTYPE_SIZE (t) = TYPE_BINFO_SIZE (t);
-      CLASSTYPE_SIZE_UNIT (t) = TYPE_BINFO_SIZE_UNIT (t);
-    }
-
-  CLASSTYPE_ALIGN (t) = TYPE_ALIGN (t);
-  CLASSTYPE_USER_ALIGN (t) = TYPE_USER_ALIGN (t);
+    CLASSTYPE_AS_BASE (t) = t;
 
   /* Every empty class contains an empty class.  */
-  if (*empty_p)
+  if (CLASSTYPE_EMPTY_P (t))
     CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 1;
 
   /* Set the TYPE_DECL for this type to contain the right
@@ -4981,61 +5117,83 @@ layout_class_type (t, empty_p, vfuns_p, virtuals_p)
      around.  We must get these done before we try to lay out the
      virtual function table.  As a side-effect, this will remove the
      base subobject fields.  */
-  layout_virtual_bases (t, empty_base_offsets);
+  layout_virtual_bases (rli, empty_base_offsets);
+
+  /* Make sure that empty classes are reflected in RLI at this
+     point.  */
+  include_empty_classes(rli);
+
+  /* Make sure not to create any structures with zero size.  */
+  if (integer_zerop (rli_size_unit_so_far (rli)) && CLASSTYPE_EMPTY_P (t))
+    place_field (rli,
+                build_decl (FIELD_DECL, NULL_TREE, char_type_node));
 
-  /* Warn about direct bases that can't be talked about due to
-     ambiguity.  */
-  warn_about_ambiguous_direct_bases (t);
+  /* Let the back end lay out the type.  */
+  finish_record_layout (rli, /*free_p=*/true);
+
+  /* Warn about bases that can't be talked about due to ambiguity.  */
+  warn_about_ambiguous_bases (t);
+
+  /* Now that we're done with layout, give the base fields the real types.  */
+  for (field = TYPE_FIELDS (t); field; field = TREE_CHAIN (field))
+    if (DECL_ARTIFICIAL (field) && IS_FAKE_BASE_TYPE (TREE_TYPE (field)))
+      TREE_TYPE (field) = TYPE_CONTEXT (TREE_TYPE (field));
 
   /* Clean up.  */
   splay_tree_delete (empty_base_offsets);
+
+  if (CLASSTYPE_EMPTY_P (t)
+      && tree_int_cst_lt (sizeof_biggest_empty_class,
+                         TYPE_SIZE_UNIT (t)))
+    sizeof_biggest_empty_class = TYPE_SIZE_UNIT (t);
 }
 
-/* Create a RECORD_TYPE or UNION_TYPE node for a C struct or union declaration
-   (or C++ class declaration).
+/* Determine the "key method" for the class type indicated by TYPE,
+   and set CLASSTYPE_KEY_METHOD accordingly.  */
 
-   For C++, we must handle the building of derived classes.
-   Also, C++ allows static class members.  The way that this is
-   handled is to keep the field name where it is (as the DECL_NAME
-   of the field), and place the overloaded decl in the bit position
-   of the field.  layout_record and layout_union will know about this.
+void
+determine_key_method (tree type)
+{
+  tree method;
+
+  if (TYPE_FOR_JAVA (type)
+      || processing_template_decl
+      || CLASSTYPE_TEMPLATE_INSTANTIATION (type)
+      || CLASSTYPE_INTERFACE_KNOWN (type))
+    return;
 
-   More C++ hair: inline functions have text in their
-   DECL_PENDING_INLINE_INFO nodes which must somehow be parsed into
-   meaningful tree structure.  After the struct has been laid out, set
-   things up so that this can happen.
+  /* The key method is the first non-pure virtual function that is not
+     inline at the point of class definition.  On some targets the
+     key function may not be inline; those targets should not call
+     this function until the end of the translation unit.  */
+  for (method = TYPE_METHODS (type); method != NULL_TREE;
+       method = TREE_CHAIN (method))
+    if (DECL_VINDEX (method) != NULL_TREE
+       && ! DECL_DECLARED_INLINE_P (method)
+       && ! DECL_PURE_VIRTUAL_P (method))
+      {
+       CLASSTYPE_KEY_METHOD (type) = method;
+       break;
+      }
 
-   And still more: virtual functions.  In the case of single inheritance,
-   when a new virtual function is seen which redefines a virtual function
-   from the base class, the new virtual function is placed into
-   the virtual function table at exactly the same address that
-   it had in the base class.  When this is extended to multiple
-   inheritance, the same thing happens, except that multiple virtual
-   function tables must be maintained.  The first virtual function
-   table is treated in exactly the same way as in the case of single
-   inheritance.  Additional virtual function tables have different
-   DELTAs, which tell how to adjust `this' to point to the right thing.
+  return;
+}
 
-   ATTRIBUTES is the set of decl attributes to be applied, if any.  */
+/* Perform processing required when the definition of T (a class type)
+   is complete.  */
 
 void
-finish_struct_1 (t)
-     tree t;
+finish_struct_1 (tree t)
 {
   tree x;
-  int vfuns;
-  /* A TREE_LIST.  The TREE_VALUE of each node is a FUNCTION_DECL. */
+  /* 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;
 
   if (COMPLETE_TYPE_P (t))
     {
-      if (IS_AGGR_TYPE (t))
-       error ("redefinition of `%#T'", t);
-      else
-       abort ();
+      gcc_assert (MAYBE_CLASS_TYPE_P (t));
+      error ("redefinition of %q#T", t);
       popclass ();
       return;
     }
@@ -5043,49 +5201,48 @@ finish_struct_1 (t)
   /* If this type was previously laid out as a forward reference,
      make sure we lay it out again.  */
   TYPE_SIZE (t) = NULL_TREE;
-  CLASSTYPE_GOT_SEMICOLON (t) = 0;
   CLASSTYPE_PRIMARY_BINFO (t) = NULL_TREE;
-  vfuns = 0;
-  CLASSTYPE_RTTI (t) = NULL_TREE;
 
   fixup_inline_methods (t);
-  
+
+  /* Make assumptions about the class; we'll reset the flags if
+     necessary.  */
+  CLASSTYPE_EMPTY_P (t) = 1;
+  CLASSTYPE_NEARLY_EMPTY_P (t) = 1;
+  CLASSTYPE_CONTAINS_EMPTY_CLASS_P (t) = 0;
+
   /* 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);
+  check_bases_and_members (t);
 
-  /* Layout the class itself.  */
-  layout_class_type (t, &empty, &vfuns, &virtuals);
-
-  /* Make sure that we get our own copy of the vfield FIELD_DECL.  */
-  vfield = TYPE_VFIELD (t);
-  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;
-      TYPE_VFIELD (t) = vfield;
+  /* Find the key method.  */
+  if (TYPE_CONTAINS_VPTR_P (t))
+    {
+      /* The Itanium C++ ABI permits the key method to be chosen when
+        the class is defined -- even though the key method so
+        selected may later turn out to be an inline function.  On
+        some systems (such as ARM Symbian OS) the key method cannot
+        be determined until the end of the translation unit.  On such
+        systems, we leave CLASSTYPE_KEY_METHOD set to NULL, which
+        will cause the class to be added to KEYED_CLASSES.  Then, in
+        finish_file we will determine the key method.  */
+      if (targetm.cxx.key_method_may_be_inline ())
+       determine_key_method (t);
+
+      /* If a polymorphic class has no key method, we may emit the vtable
+        in every translation unit where the class definition appears.  */
+      if (CLASSTYPE_KEY_METHOD (t) == NULL_TREE)
+       keyed_classes = tree_cons (NULL_TREE, t, keyed_classes);
     }
-  else
-    my_friendly_assert (!vfield || DECL_FIELD_CONTEXT (vfield) == t, 20010726);
 
-  virtuals = modify_all_vtables (t, &vfuns, nreverse (virtuals));
+  /* Layout the class itself.  */
+  layout_class_type (t, &virtuals);
+  if (CLASSTYPE_AS_BASE (t) != t)
+    /* We use the base type for trivial assignments, and hence it
+       needs a mode.  */
+    compute_record_mode (CLASSTYPE_AS_BASE (t));
 
-  /* If we created a new vtbl pointer for this class, add it to the
-     list.  */
-  if (TYPE_VFIELD (t) && !CLASSTYPE_HAS_PRIMARY_BASE_P (t))
-    CLASSTYPE_VFIELDS (t) 
-      = chainon (CLASSTYPE_VFIELDS (t), build_tree_list (NULL_TREE, t));
+  virtuals = modify_all_vtables (t, nreverse (virtuals));
 
   /* If necessary, create the primary vtable for this class.  */
   if (virtuals || TYPE_CONTAINS_VPTR_P (t))
@@ -5093,42 +5250,43 @@ finish_struct_1 (t)
       /* We must enter these virtuals into the table.  */
       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
        build_primary_vtable (NULL_TREE, t);
-      else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t), t))
+      else if (! BINFO_NEW_VTABLE_MARKED (TYPE_BINFO (t)))
        /* Here we know enough to change the type of our virtual
           function table, but we will wait until later this function.  */
        build_primary_vtable (CLASSTYPE_PRIMARY_BINFO (t), t);
-
-      /* If this type has basetypes with constructors, then those
-        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
-     the base.  */
-  else if (CLASSTYPE_HAS_PRIMARY_BASE_P (t))
-    {
-      tree binfo = CLASSTYPE_PRIMARY_BINFO (t);
-
-      /* If this class uses a different vtable than its primary base
-        then when we will need to initialize our vptr after the base
-        class constructor runs.  */
-      if (TYPE_BINFO_VTABLE (t) != BINFO_VTABLE (binfo))
-       CLASSTYPE_NEEDS_VIRTUAL_REINIT (t) = 1;
     }
 
   if (TYPE_CONTAINS_VPTR_P (t))
     {
-      if (TYPE_BINFO_VTABLE (t))
-       my_friendly_assert (DECL_VIRTUAL_P (TYPE_BINFO_VTABLE (t)),
-                           20000116);
+      int vindex;
+      tree fn;
+
+      if (BINFO_VTABLE (TYPE_BINFO (t)))
+       gcc_assert (DECL_VIRTUAL_P (BINFO_VTABLE (TYPE_BINFO (t))));
       if (!CLASSTYPE_HAS_PRIMARY_BASE_P (t))
-       my_friendly_assert (TYPE_BINFO_VIRTUALS (t) == NULL_TREE,
-                           20000116);
+       gcc_assert (BINFO_VIRTUALS (TYPE_BINFO (t)) == NULL_TREE);
 
-      CLASSTYPE_VSIZE (t) = vfuns;
       /* Add entries for virtual functions introduced by this class.  */
-      TYPE_BINFO_VIRTUALS (t) = chainon (TYPE_BINFO_VIRTUALS (t), virtuals);
+      BINFO_VIRTUALS (TYPE_BINFO (t))
+       = chainon (BINFO_VIRTUALS (TYPE_BINFO (t)), virtuals);
+
+      /* Set DECL_VINDEX for all functions declared in this class.  */
+      for (vindex = 0, fn = BINFO_VIRTUALS (TYPE_BINFO (t));
+          fn;
+          fn = TREE_CHAIN (fn),
+            vindex += (TARGET_VTABLE_USES_DESCRIPTORS
+                       ? TARGET_VTABLE_USES_DESCRIPTORS : 1))
+       {
+         tree fndecl = BV_FN (fn);
+
+         if (DECL_THUNK_P (fndecl))
+           /* A thunk. We should never be calling this entry directly
+              from this vtable -- we'd use the entry for the non
+              thunk base function.  */
+           DECL_VINDEX (fndecl) = NULL_TREE;
+         else if (TREE_CODE (DECL_VINDEX (fndecl)) != INTEGER_CST)
+           DECL_VINDEX (fndecl) = build_int_cst (NULL_TREE, vindex);
+       }
     }
 
   finish_struct_bits (t);
@@ -5137,64 +5295,78 @@ 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) != error_mark_node
        && 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
      faster lookups later.
 
-     The C front-end only does this when n_fields > 15.  We use
-     a smaller number because most searches fail (succeeding
+     We use a small number because most searches fail (succeeding
      ultimately as the search bores through the inheritance
      hierarchy), and we want this failure to occur quickly.  */
 
   n_fields = count_fields (TYPE_FIELDS (t));
   if (n_fields > 7)
     {
-      tree field_vec = make_tree_vec (n_fields);
-      add_fields_to_vec (TYPE_FIELDS (t), field_vec, 0);
-      qsort (&TREE_VEC_ELT (field_vec, 0), n_fields, sizeof (tree),
-            (int (*)(const void *, const void *))field_decl_cmp);
+      struct sorted_fields_type *field_vec = GGC_NEWVAR
+        (struct sorted_fields_type,
+         sizeof (struct sorted_fields_type) + n_fields * sizeof (tree));
+      field_vec->len = n_fields;
+      add_fields_to_record_type (TYPE_FIELDS (t), field_vec, 0);
+      qsort (field_vec->elts, n_fields, sizeof (tree),
+            field_decl_cmp);
       if (! DECL_LANG_SPECIFIC (TYPE_MAIN_DECL (t)))
        retrofit_lang_decl (TYPE_MAIN_DECL (t));
       DECL_SORTED_FIELDS (TYPE_MAIN_DECL (t)) = field_vec;
     }
 
-  if (TYPE_HAS_CONSTRUCTOR (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);
-       }
-    }
-
+  /* Complain if one of the field types requires lower visibility.  */
+  constrain_class_visibility (t);
+
   /* Make the rtl for any new vtables we have created, and unmark
      the base types we marked.  */
   finish_vtbls (t);
-  
+
   /* Build the VTT for T.  */
   build_vtt (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);
+  /* This warning does not make sense for Java classes, since they
+     cannot have destructors.  */
+  if (!TYPE_FOR_JAVA (t) && warn_nonvdtor && TYPE_POLYMORPHIC_P (t))
+    {
+      tree dtor;
+
+      dtor = CLASSTYPE_DESTRUCTORS (t);
+      if (/* An implicitly declared destructor is always public.  And,
+            if it were virtual, we would have created it by now.  */
+         !dtor
+         || (!DECL_VINDEX (dtor)
+             && (/* public non-virtual */
+                 (!TREE_PRIVATE (dtor) && !TREE_PROTECTED (dtor))
+                  || (/* non-public non-virtual with friends */
+                      (TREE_PRIVATE (dtor) || TREE_PROTECTED (dtor))
+                       && (CLASSTYPE_FRIEND_CLASSES (t)
+                       || DECL_FRIENDLIST (TYPE_MAIN_DECL (t)))))))
+       warning (OPT_Wnon_virtual_dtor,
+                "%q#T has virtual functions and accessible"
+                " non-virtual destructor", t);
+    }
 
   complete_vars (t);
 
   if (warn_overloaded_virtual)
     warn_hidden (t);
 
+  /* Class layout, assignment of virtual table slots, etc., is now
+     complete.  Give the back end a chance to tweak the visibility of
+     the class or perform any other required target modifications.  */
+  targetm.cxx.adjust_class_at_definition (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));
 }
@@ -5203,23 +5375,22 @@ finish_struct_1 (t)
    order.  Rearrange them to declaration order.  */
 
 void
-unreverse_member_declarations (t)
-     tree t;
+unreverse_member_declarations (tree t)
 {
   tree next;
   tree prev;
   tree x;
 
-  /* The TYPE_FIELDS, TYPE_METHODS, and CLASSTYPE_TAGS are all in
-     reverse order.  Put them in declaration order now.  */
+  /* The following lists are all in reverse order.  Put them in
+     declaration order now.  */
   TYPE_METHODS (t) = nreverse (TYPE_METHODS (t));
-  CLASSTYPE_TAGS (t) = nreverse (CLASSTYPE_TAGS (t));
+  CLASSTYPE_DECL_LIST (t) = nreverse (CLASSTYPE_DECL_LIST (t));
 
   /* Actually, for the TYPE_FIELDS, only the non TYPE_DECLs are in
      reverse order, so we can't just use nreverse.  */
   prev = NULL_TREE;
-  for (x = TYPE_FIELDS (t); 
-       x && TREE_CODE (x) != TYPE_DECL; 
+  for (x = TYPE_FIELDS (t);
+       x && TREE_CODE (x) != TYPE_DECL;
        x = next)
     {
       next = TREE_CHAIN (x);
@@ -5235,11 +5406,9 @@ unreverse_member_declarations (t)
 }
 
 tree
-finish_struct (t, attributes)
-     tree t, attributes;
+finish_struct (tree t, tree attributes)
 {
-  const char *saved_filename = input_filename;
-  int saved_lineno = lineno;
+  location_t saved_loc = input_location;
 
   /* Now that we've got all the field declarations, reverse everything
      as necessary.  */
@@ -5249,19 +5418,33 @@ finish_struct (t, attributes)
 
   /* Nadger the current location so that diagnostics point to the start of
      the struct, not the end.  */
-  input_filename = DECL_SOURCE_FILE (TYPE_NAME (t));
-  lineno = DECL_SOURCE_LINE (TYPE_NAME (t));
+  input_location = DECL_SOURCE_LOCATION (TYPE_NAME (t));
 
   if (processing_template_decl)
     {
+      tree x;
+
       finish_struct_methods (t);
       TYPE_SIZE (t) = bitsize_zero_node;
+      TYPE_SIZE_UNIT (t) = size_zero_node;
+
+      /* We need to emit an error message if this type was used as a parameter
+        and it is an abstract type, even if it is a template. We construct
+        a simple CLASSTYPE_PURE_VIRTUALS list without taking bases into
+        account and we call complete_vars with this type, which will check
+        the PARM_DECLS. Note that while the type is being defined,
+        CLASSTYPE_PURE_VIRTUALS contains the list of the inline friends
+        (see CLASSTYPE_INLINE_FRIENDS) so we need to clear it.  */
+      CLASSTYPE_PURE_VIRTUALS (t) = NULL;
+      for (x = TYPE_METHODS (t); x; x = TREE_CHAIN (x))
+       if (DECL_PURE_VIRTUAL_P (x))
+         VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (t), x);
+      complete_vars (t);
     }
   else
     finish_struct_1 (t);
 
-  input_filename = saved_filename;
-  lineno = saved_lineno;
+  input_location = saved_loc;
 
   TYPE_BEING_DEFINED (t) = 0;
 
@@ -5270,12 +5453,8 @@ finish_struct (t, attributes)
   else
     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;
 }
@@ -5289,19 +5468,17 @@ finish_struct (t, attributes)
    before this function is called.  */
 
 static tree
-fixed_type_or_null (instance, nonnull, cdtorp)
-     tree instance;
-     int *nonnull;
-     int *cdtorp;
+fixed_type_or_null (tree instance, int *nonnull, int *cdtorp)
 {
+#define RECUR(T) fixed_type_or_null((T), nonnull, cdtorp)
+
   switch (TREE_CODE (instance))
     {
     case INDIRECT_REF:
       if (POINTER_TYPE_P (TREE_TYPE (instance)))
        return NULL_TREE;
       else
-       return fixed_type_or_null (TREE_OPERAND (instance, 0),
-                                  nonnull, cdtorp);
+       return RECUR (TREE_OPERAND (instance, 0));
 
     case CALL_EXPR:
       /* This is a call to a constructor, hence it's never zero.  */
@@ -5321,36 +5498,46 @@ fixed_type_or_null (instance, nonnull, cdtorp)
            *nonnull = 1;
          return TREE_TYPE (instance);
        }
-      return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
-
-    case RTL_EXPR:
-      return NULL_TREE;
+      return RECUR (TREE_OPERAND (instance, 0));
 
+    case POINTER_PLUS_EXPR:
     case PLUS_EXPR:
     case MINUS_EXPR:
       if (TREE_CODE (TREE_OPERAND (instance, 0)) == ADDR_EXPR)
-       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
+       return RECUR (TREE_OPERAND (instance, 0));
       if (TREE_CODE (TREE_OPERAND (instance, 1)) == INTEGER_CST)
        /* Propagate nonnull.  */
-       return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
+       return RECUR (TREE_OPERAND (instance, 0));
+
       return NULL_TREE;
 
-    case NOP_EXPR:
-    case CONVERT_EXPR:
-      return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
+    CASE_CONVERT:
+      return RECUR (TREE_OPERAND (instance, 0));
 
     case ADDR_EXPR:
+      instance = TREE_OPERAND (instance, 0);
       if (nonnull)
-       *nonnull = 1;
-      return fixed_type_or_null (TREE_OPERAND (instance, 0), nonnull, cdtorp);
+       {
+         /* Just because we see an ADDR_EXPR doesn't mean we're dealing
+            with a real object -- given &p->f, p can still be null.  */
+         tree t = get_base_address (instance);
+         /* ??? Probably should check DECL_WEAK here.  */
+         if (t && DECL_P (t))
+           *nonnull = 1;
+       }
+      return RECUR (instance);
 
     case COMPONENT_REF:
-      return fixed_type_or_null (TREE_OPERAND (instance, 1), nonnull, cdtorp);
+      /* If this component is really a base class reference, then the field
+        itself isn't definitive.  */
+      if (DECL_FIELD_IS_BASE (TREE_OPERAND (instance, 1)))
+       return RECUR (TREE_OPERAND (instance, 0));
+      return RECUR (TREE_OPERAND (instance, 1));
 
     case VAR_DECL:
     case FIELD_DECL:
       if (TREE_CODE (TREE_TYPE (instance)) == ARRAY_TYPE
-         && IS_AGGR_TYPE (TREE_TYPE (TREE_TYPE (instance))))
+         && MAYBE_CLASS_TYPE_P (TREE_TYPE (TREE_TYPE (instance))))
        {
          if (nonnull)
            *nonnull = 1;
@@ -5360,46 +5547,68 @@ fixed_type_or_null (instance, nonnull, cdtorp)
     case TARGET_EXPR:
     case PARM_DECL:
     case RESULT_DECL:
-      if (IS_AGGR_TYPE (TREE_TYPE (instance)))
+      if (MAYBE_CLASS_TYPE_P (TREE_TYPE (instance)))
        {
          if (nonnull)
            *nonnull = 1;
          return TREE_TYPE (instance);
        }
       else if (instance == current_class_ptr)
-        {
-          if (nonnull)
-            *nonnull = 1;
-        
-          /* if we're in a ctor or dtor, we know our type. */
-          if (DECL_LANG_SPECIFIC (current_function_decl)
-              && (DECL_CONSTRUCTOR_P (current_function_decl)
-                  || DECL_DESTRUCTOR_P (current_function_decl)))
-            {
-              if (cdtorp)
-                *cdtorp = 1;
-              return TREE_TYPE (TREE_TYPE (instance));
-            }
-        }
+       {
+         if (nonnull)
+           *nonnull = 1;
+
+         /* if we're in a ctor or dtor, we know our type.  */
+         if (DECL_LANG_SPECIFIC (current_function_decl)
+             && (DECL_CONSTRUCTOR_P (current_function_decl)
+                 || DECL_DESTRUCTOR_P (current_function_decl)))
+           {
+             if (cdtorp)
+               *cdtorp = 1;
+             return TREE_TYPE (TREE_TYPE (instance));
+           }
+       }
       else if (TREE_CODE (TREE_TYPE (instance)) == REFERENCE_TYPE)
-        {
-          /* Reference variables should be references to objects.  */
-          if (nonnull)
+       {
+         /* We only need one hash table because it is always left empty.  */
+         static htab_t ht;
+         if (!ht)
+           ht = htab_create (37, 
+                             htab_hash_pointer,
+                             htab_eq_pointer,
+                             /*htab_del=*/NULL);
+
+         /* Reference variables should be references to objects.  */
+         if (nonnull)
            *nonnull = 1;
 
+         /* Enter the INSTANCE in a table to prevent recursion; a
+            variable's initializer may refer to the variable
+            itself.  */
          if (TREE_CODE (instance) == VAR_DECL
-             && DECL_INITIAL (instance))
-           return fixed_type_or_null (DECL_INITIAL (instance),
-                                      nonnull, cdtorp);
+             && DECL_INITIAL (instance)
+             && !htab_find (ht, instance))
+           {
+             tree type;
+             void **slot;
+
+             slot = htab_find_slot (ht, instance, INSERT);
+             *slot = instance;
+             type = RECUR (DECL_INITIAL (instance));
+             htab_remove_elt (ht, instance);
+
+             return type;
+           }
        }
       return NULL_TREE;
 
     default:
       return NULL_TREE;
     }
+#undef RECUR
 }
 
-/* Return non-zero if the dynamic type of INSTANCE is known, and
+/* Return nonzero 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
@@ -5414,13 +5623,10 @@ fixed_type_or_null (instance, nonnull, cdtorp)
    before this function is called.  */
 
 int
-resolves_to_fixed_type_p (instance, nonnull)
-     tree instance;
-     int *nonnull;
+resolves_to_fixed_type_p (tree instance, int* nonnull)
 {
   tree t = TREE_TYPE (instance);
   int cdtorp = 0;
-  
   tree fixed = fixed_type_or_null (instance, nonnull, &cdtorp);
   if (fixed == NULL_TREE)
     return 0;
@@ -5433,86 +5639,72 @@ resolves_to_fixed_type_p (instance, nonnull)
 
 \f
 void
-init_class_processing ()
+init_class_processing (void)
 {
   current_class_depth = 0;
   current_class_stack_size = 10;
-  current_class_stack 
-    = (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);
-  access_protected_node = build_int_2 (ak_protected, 0);
-  access_private_node = build_int_2 (ak_private, 0);
-  access_default_virtual_node = build_int_2 (4, 0);
-  access_public_virtual_node = build_int_2 (4 | ak_public, 0);
-  access_protected_virtual_node = build_int_2 (4 | ak_protected, 0);
-  access_private_virtual_node = build_int_2 (4 | ak_private, 0);
+  current_class_stack
+    = XNEWVEC (struct class_stack_node, current_class_stack_size);
+  local_classes = VEC_alloc (tree, gc, 8);
+  sizeof_biggest_empty_class = size_zero_node;
 
   ridpointers[(int) RID_PUBLIC] = access_public_node;
   ridpointers[(int) RID_PRIVATE] = access_private_node;
   ridpointers[(int) RID_PROTECTED] = access_protected_node;
 }
 
-/* Set current scope to NAME. CODE tells us if this is a
-   STRUCT, UNION, or ENUM environment.
+/* Restore the cached PREVIOUS_CLASS_LEVEL.  */
 
-   NAME may end up being NULL_TREE if this is an anonymous or
-   late-bound struct (as in "struct { ... } foo;")  */
-
-/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE to
-   appropriate values, found by looking up the type definition of
-   NAME (as a CODE).
-
-   If MODIFY is 1, we set IDENTIFIER_CLASS_VALUE's of names
-   which can be seen locally to the class.  They are shadowed by
-   any subsequent local declaration (including parameter names).
+static void
+restore_class_cache (void)
+{
+  tree type;
 
-   If MODIFY is 2, we set IDENTIFIER_CLASS_VALUE's of names
-   which have static meaning (i.e., static members, static
-   member functions, enum declarations, etc).
+  /* We are re-entering the same class we just left, so we don't
+     have to search the whole inheritance matrix to find all the
+     decls to bind again.  Instead, we install the cached
+     class_shadowed list and walk through it binding names.  */
+  push_binding_level (previous_class_level);
+  class_binding_level = previous_class_level;
+  /* Restore IDENTIFIER_TYPE_VALUE.  */
+  for (type = class_binding_level->type_shadowed;
+       type;
+       type = TREE_CHAIN (type))
+    SET_IDENTIFIER_TYPE_VALUE (TREE_PURPOSE (type), TREE_TYPE (type));
+}
 
-   If MODIFY is 3, we set IDENTIFIER_CLASS_VALUE of names
-   which can be seen locally to the class (as in 1), but
-   know that we are doing this for declaration purposes
-   (i.e. friend foo::bar (int)).
+/* Set global variables CURRENT_CLASS_NAME and CURRENT_CLASS_TYPE as
+   appropriate for TYPE.
 
    So that we may avoid calls to lookup_name, we cache the _TYPE
    nodes of local TYPE_DECLs in the TREE_TYPE field of the name.
 
    For multiple inheritance, we perform a two-pass depth-first search
-   of the type lattice.  The first pass performs a pre-order search,
-   marking types after the type has had its fields installed in
-   the appropriate IDENTIFIER_CLASS_VALUE slot.  The second pass merely
-   unmarks the marked types.  If a field or member function name
-   appears in an ambiguous way, the IDENTIFIER_CLASS_VALUE of
-   that name becomes `error_mark_node'.  */
+   of the type lattice.  */
 
 void
-pushclass (type, modify)
-     tree type;
-     int modify;
+pushclass (tree type)
 {
+  class_stack_node_t csn;
+
   type = TYPE_MAIN_VARIANT (type);
 
   /* Make sure there is enough room for the new entry on the stack.  */
-  if (current_class_depth + 1 >= current_class_stack_size) 
+  if (current_class_depth + 1 >= current_class_stack_size)
     {
       current_class_stack_size *= 2;
       current_class_stack
-       = (class_stack_node_t) xrealloc (current_class_stack,
-                                        current_class_stack_size
-                                        * sizeof (struct class_stack_node));
+       = XRESIZEVEC (struct class_stack_node, current_class_stack,
+                     current_class_stack_size);
     }
 
   /* Insert a new entry on the class stack.  */
-  current_class_stack[current_class_depth].name = current_class_name;
-  current_class_stack[current_class_depth].type = current_class_type;
-  current_class_stack[current_class_depth].access = current_access_specifier;
-  current_class_stack[current_class_depth].names_used = 0;
+  csn = current_class_stack + current_class_depth;
+  csn->name = current_class_name;
+  csn->type = current_class_type;
+  csn->access = current_access_specifier;
+  csn->names_used = 0;
+  csn->hidden = 0;
   current_class_depth++;
 
   /* Now set up the new type.  */
@@ -5523,84 +5715,43 @@ pushclass (type, modify)
 
   /* By default, things in classes are private, while things in
      structures or unions are public.  */
-  current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type) 
-                             ? access_private_node 
+  current_access_specifier = (CLASSTYPE_DECLARED_CLASS (type)
+                             ? access_private_node
                              : access_public_node);
 
-  if (previous_class_type != NULL_TREE
-      && (type != previous_class_type 
-         || !COMPLETE_TYPE_P (previous_class_type))
+  if (previous_class_level
+      && type != previous_class_level->this_entity
       && current_class_depth == 1)
     {
       /* Forcibly remove any old class remnants.  */
       invalidate_class_lookup_cache ();
     }
 
-  /* If we're about to enter a nested class, clear
-     IDENTIFIER_CLASS_VALUE for the enclosing classes.  */
-  if (modify && current_class_depth > 1)
-    clear_identifier_class_values ();
-
-  pushlevel_class ();
-
-  if (modify)
-    {
-      if (type != previous_class_type || current_class_depth > 1)
-       push_class_decls (type);
-      else
-       {
-         tree item;
-
-         /* We are re-entering the same class we just left, so we
-            don't have to search the whole inheritance matrix to find
-            all the decls to bind again.  Instead, we install the
-            cached class_shadowed list, and walk through it binding
-            names and setting up IDENTIFIER_TYPE_VALUEs.  */
-         set_class_shadows (previous_class_values);
-         for (item = previous_class_values; item; item = TREE_CHAIN (item))
-           {
-             tree id = TREE_PURPOSE (item);
-             tree decl = TREE_TYPE (item);
-
-             push_class_binding (id, decl);
-             if (TREE_CODE (decl) == TYPE_DECL)
-               set_identifier_type_value (id, TREE_TYPE (decl));
-           }
-         unuse_fields (type);
-       }
-
-      storetags (CLASSTYPE_TAGS (type));
-    }
+  if (!previous_class_level
+      || type != previous_class_level->this_entity
+      || current_class_depth > 1)
+    pushlevel_class ();
+  else
+    restore_class_cache ();
 }
 
-/* When we exit a toplevel class scope, we save the
-   IDENTIFIER_CLASS_VALUEs so that we can restore them quickly if we
-   reenter the class.  Here, we've entered some other class, so we
-   must invalidate our cache.  */
+/* When we exit a toplevel class scope, we save its binding level so
+   that we can restore it quickly.  Here, we've entered some other
+   class, so we must invalidate our cache.  */
 
 void
-invalidate_class_lookup_cache ()
+invalidate_class_lookup_cache (void)
 {
-  tree t;
-  
-  /* The IDENTIFIER_CLASS_VALUEs are no longer valid.  */
-  for (t = previous_class_values; t; t = TREE_CHAIN (t))
-    IDENTIFIER_CLASS_VALUE (TREE_PURPOSE (t)) = NULL_TREE;
-
-  previous_class_values = NULL_TREE;
-  previous_class_type = NULL_TREE;
+  previous_class_level = NULL;
 }
+
 /* Get out of the current class scope. If we were in a class scope
    previously, that is the one popped to.  */
 
 void
-popclass ()
+popclass (void)
 {
   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--;
   current_class_name = current_class_stack[current_class_depth].name;
@@ -5610,21 +5761,54 @@ popclass ()
     splay_tree_delete (current_class_stack[current_class_depth].names_used);
 }
 
-/* Returns 1 if current_class_type is either T or a nested type of T.
-   We start looking from 1 because entry 0 is from global scope, and has
-   no type.  */
+/* Mark the top of the class stack as hidden.  */
 
-int
-currently_open_class (t)
-     tree t;
+void
+push_class_stack (void)
+{
+  if (current_class_depth)
+    ++current_class_stack[current_class_depth - 1].hidden;
+}
+
+/* Mark the top of the class stack as un-hidden.  */
+
+void
+pop_class_stack (void)
+{
+  if (current_class_depth)
+    --current_class_stack[current_class_depth - 1].hidden;
+}
+
+/* Returns 1 if the class type currently being defined is either T or
+   a nested type of T.  */
+
+bool
+currently_open_class (tree t)
 {
   int i;
-  if (t == current_class_type)
-    return 1;
-  for (i = 1; i < current_class_depth; ++i)
-    if (current_class_stack [i].type == t)
-      return 1;
-  return 0;
+
+  if (!CLASS_TYPE_P (t))
+    return false;
+
+  /* We start looking from 1 because entry 0 is from global scope,
+     and has no type.  */
+  for (i = current_class_depth; i > 0; --i)
+    {
+      tree c;
+      if (i == current_class_depth)
+       c = current_class_type;
+      else
+       {
+         if (current_class_stack[i].hidden)
+           break;
+         c = current_class_stack[i].type;
+       }
+      if (!c)
+       continue;
+      if (same_type_p (c, t))
+       return true;
+    }
+  return false;
 }
 
 /* If either current_class_type or one of its enclosing classes are derived
@@ -5632,55 +5816,54 @@ currently_open_class (t)
    something via unqualified lookup.  */
 
 tree
-currently_open_derived_class (t)
-     tree t;
+currently_open_derived_class (tree t)
 {
   int i;
 
+  /* The bases of a dependent type are unknown.  */
+  if (dependent_type_p (t))
+    return NULL_TREE;
+
+  if (!current_class_type)
+    return NULL_TREE;
+
   if (DERIVED_FROM_P (t, current_class_type))
     return current_class_type;
 
   for (i = current_class_depth - 1; i > 0; --i)
-    if (DERIVED_FROM_P (t, current_class_stack[i].type))
-      return current_class_stack[i].type;
+    {
+      if (current_class_stack[i].hidden)
+       break;
+      if (DERIVED_FROM_P (t, current_class_stack[i].type))
+       return current_class_stack[i].type;
+    }
 
   return NULL_TREE;
 }
 
 /* When entering a class scope, all enclosing class scopes' names with
-   static meaning (static variables, static functions, types and enumerators)
-   have to be visible.  This recursive function calls pushclass for all
-   enclosing class contexts until global or a local scope is reached.
-   TYPE is the enclosed class and MODIFY is equivalent with the pushclass
-   formal of the same name.  */
+   static meaning (static variables, static functions, types and
+   enumerators) have to be visible.  This recursive function calls
+   pushclass for all enclosing class contexts until global or a local
+   scope is reached.  TYPE is the enclosed class.  */
 
 void
-push_nested_class (type, modify)
-     tree type;
-     int modify;
+push_nested_class (tree type)
 {
-  tree context;
-
   /* A namespace might be passed in error cases, like A::B:C.  */
-  if (type == NULL_TREE 
-      || type == error_mark_node 
-      || TREE_CODE (type) == NAMESPACE_DECL
-      || ! IS_AGGR_TYPE (type)
-      || TREE_CODE (type) == TEMPLATE_TYPE_PARM
-      || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
+  if (type == NULL_TREE
+      || !CLASS_TYPE_P (type))
     return;
-  
-  context = DECL_CONTEXT (TYPE_MAIN_DECL (type));
 
-  if (context && CLASS_TYPE_P (context))
-    push_nested_class (context, 2);
-  pushclass (type, modify);
+  push_nested_class (DECL_CONTEXT (TYPE_MAIN_DECL (type)));
+
+  pushclass (type);
 }
 
-/* Undoes a push_nested_class call.  MODIFY is passed on to popclass.  */
+/* Undoes a push_nested_class call.  */
 
 void
-pop_nested_class ()
+pop_nested_class (void)
 {
   tree context = DECL_CONTEXT (TYPE_MAIN_DECL (current_class_type));
 
@@ -5692,19 +5875,18 @@ pop_nested_class ()
 /* Returns the number of extern "LANG" blocks we are nested within.  */
 
 int
-current_lang_depth ()
+current_lang_depth (void)
 {
-  return VARRAY_ACTIVE_SIZE (current_lang_base);
+  return VEC_length (tree, current_lang_base);
 }
 
 /* Set global variables CURRENT_LANG_NAME to appropriate value
    so that behavior of name-mangling machinery is correct.  */
 
 void
-push_lang_context (name)
-     tree name;
+push_lang_context (tree name)
 {
-  VARRAY_PUSH_TREE (current_lang_base, current_lang_name);
+  VEC_safe_push (tree, gc, current_lang_base, current_lang_name);
 
   if (name == lang_name_cplusplus)
     {
@@ -5716,7 +5898,7 @@ push_lang_context (name)
       /* DECL_IGNORED_P is initially set for these types, to avoid clutter.
         (See record_builtin_java_type in decl.c.)  However, that causes
         incorrect debug entries if these types are actually used.
-        So we re-enable debug output after extern "Java". */
+        So we re-enable debug output after extern "Java".  */
       DECL_IGNORED_P (TYPE_NAME (java_byte_type_node)) = 0;
       DECL_IGNORED_P (TYPE_NAME (java_short_type_node)) = 0;
       DECL_IGNORED_P (TYPE_NAME (java_int_type_node)) = 0;
@@ -5731,44 +5913,43 @@ push_lang_context (name)
       current_lang_name = name;
     }
   else
-    error ("language string `\"%s\"' not recognized", IDENTIFIER_POINTER (name));
+    error ("language string %<\"%E\"%> not recognized", name);
 }
-  
+
 /* Get out of the current language scope.  */
 
 void
-pop_lang_context ()
+pop_lang_context (void)
 {
-  current_lang_name = VARRAY_TOP_TREE (current_lang_base);
-  VARRAY_POP (current_lang_base);
+  current_lang_name = VEC_pop (tree, current_lang_base);
 }
 \f
 /* Type instantiation routines.  */
 
 /* Given an OVERLOAD and a TARGET_TYPE, return the function that
    matches the TARGET_TYPE.  If there is no satisfactory match, return
-   error_mark_node, and issue an error message if COMPLAIN is
-   non-zero.  Permit pointers to member function if PTRMEM is non-zero.
-   If TEMPLATE_ONLY, the name of the overloaded function
-   was a template-id, and EXPLICIT_TARGS are the explicitly provided
-   template arguments.  */
+   error_mark_node, and issue an error & warning messages under
+   control of FLAGS.  Permit pointers to member function if FLAGS
+   permits.  If TEMPLATE_ONLY, the name of the overloaded function was
+   a template-id, and EXPLICIT_TARGS are the explicitly provided
+   template arguments.  
+
+   If OVERLOAD is for one or more member functions, then ACCESS_PATH
+   is the base path used to reference those member functions.  If
+   TF_NO_ACCESS_CONTROL is not set in FLAGS, and the address is
+   resolved to a member function, access checks will be performed and
+   errors issued if appropriate.  */
 
 static tree
-resolve_address_of_overloaded_function (target_type, 
-                                       overload,
-                                       complain,
-                                       ptrmem,
-                                       template_only,
-                                       explicit_targs)
-     tree target_type;
-     tree overload;
-     int complain;
-     int ptrmem;
-     int template_only;
-     tree explicit_targs;
+resolve_address_of_overloaded_function (tree target_type,
+                                       tree overload,
+                                       tsubst_flags_t flags,
+                                       bool template_only,
+                                       tree explicit_targs,
+                                       tree access_path)
 {
   /* Here's what the standard says:
-     
+
        [over.over]
 
        If the name is a function template, template argument deduction
@@ -5804,12 +5985,10 @@ resolve_address_of_overloaded_function (target_type,
   /* By the time we get here, we should be seeing only real
      pointer-to-member types, not the internal POINTER_TYPE to
      METHOD_TYPE representation.  */
-  my_friendly_assert (!(TREE_CODE (target_type) == POINTER_TYPE
-                       && (TREE_CODE (TREE_TYPE (target_type)) 
-                           == METHOD_TYPE)), 0);
+  gcc_assert (TREE_CODE (target_type) != POINTER_TYPE
+             || TREE_CODE (TREE_TYPE (target_type)) != METHOD_TYPE);
 
-  if (TREE_CODE (overload) == COMPONENT_REF)
-    overload = TREE_OPERAND (overload, 1);
+  gcc_assert (is_overloaded_fn (overload));
 
   /* Check that the TARGET_TYPE is reasonable.  */
   if (TYPE_PTRFN_P (target_type))
@@ -5824,15 +6003,15 @@ resolve_address_of_overloaded_function (target_type,
       target_type = build_reference_type (target_type);
       is_reference = 1;
     }
-  else 
+  else
     {
-      if (complain)
-       error ("\
-cannot resolve overloaded function `%D' based on conversion to type `%T'", 
-                 DECL_NAME (OVL_FUNCTION (overload)), target_type);
+      if (flags & tf_error)
+       error ("cannot resolve overloaded function %qD based on"
+              " conversion to type %qT",
+              DECL_NAME (OVL_FUNCTION (overload)), target_type);
       return error_mark_node;
     }
-  
+
   /* If we can find a non-template function that matches, we can just
      use it.  There's no point in generating template instantiations
      if we're just going to throw them out anyhow.  But, of course, we
@@ -5841,9 +6020,9 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
     {
       tree fns;
 
-      for (fns = overload; fns; fns = OVL_CHAIN (fns))
+      for (fns = overload; fns; fns = OVL_NEXT (fns))
        {
-         tree fn = OVL_FUNCTION (fns);
+         tree fn = OVL_CURRENT (fns);
          tree fntype;
 
          if (TREE_CODE (fn) == TEMPLATE_DECL)
@@ -5855,7 +6034,12 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
            /* We're looking for a non-static member, and this isn't
               one, or vice versa.  */
            continue;
-       
+
+         /* Ignore functions which haven't been explicitly
+            declared.  */
+         if (DECL_ANTICIPATED (fn))
+           continue;
+
          /* See if there's a match.  */
          fntype = TREE_TYPE (fn);
          if (is_ptrmem)
@@ -5863,7 +6047,7 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
          else if (!is_reference)
            fntype = build_pointer_type (fntype);
 
-         if (can_convert_arg (target_type, fntype, fn))
+         if (can_convert_arg (target_type, fntype, fn, LOOKUP_NORMAL))
            matches = tree_cons (fn, NULL_TREE, matches);
        }
     }
@@ -5871,7 +6055,7 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
   /* Now, if we've already got a match (or matches), there's no need
      to proceed to the template functions.  But, if we don't have a
      match we need to look at them, too.  */
-  if (!matches) 
+  if (!matches)
     {
       tree target_fn_type;
       tree target_arg_types;
@@ -5889,10 +6073,10 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
       /* Never do unification on the 'this' parameter.  */
       if (TREE_CODE (target_fn_type) == METHOD_TYPE)
        target_arg_types = TREE_CHAIN (target_arg_types);
-         
-      for (fns = overload; fns; fns = OVL_CHAIN (fns))
+
+      for (fns = overload; fns; fns = OVL_NEXT (fns))
        {
-         tree fn = OVL_FUNCTION (fns);
+         tree fn = OVL_CURRENT (fns);
          tree instantiation;
          tree instantiation_type;
          tree targs;
@@ -5911,12 +6095,12 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
          targs = make_tree_vec (DECL_NTPARMS (fn));
          if (fn_type_unification (fn, explicit_targs, targs,
                                   target_arg_types, target_ret_type,
-                                  DEDUCE_EXACT, -1) != 0)
+                                  DEDUCE_EXACT, LOOKUP_NORMAL))
            /* Argument deduction failed.  */
            continue;
 
          /* Instantiate the template.  */
-         instantiation = instantiate_template (fn, targs);
+         instantiation = instantiate_template (fn, targs, flags);
          if (instantiation == error_mark_node)
            /* Instantiation failed.  */
            continue;
@@ -5924,11 +6108,12 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
          /* See if there's a match.  */
          instantiation_type = TREE_TYPE (instantiation);
          if (is_ptrmem)
-           instantiation_type = 
+           instantiation_type =
              build_ptrmemfunc_type (build_pointer_type (instantiation_type));
          else if (!is_reference)
            instantiation_type = build_pointer_type (instantiation_type);
-         if (can_convert_arg (target_type, instantiation_type, instantiation))
+         if (can_convert_arg (target_type, instantiation_type, instantiation,
+                              LOOKUP_NORMAL))
            matches = tree_cons (instantiation, fn, matches);
        }
 
@@ -5938,7 +6123,9 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
          tree match = most_specialized_instantiation (matches);
 
          if (match != error_mark_node)
-           matches = tree_cons (match, NULL_TREE, NULL_TREE);
+           matches = tree_cons (TREE_PURPOSE (match),
+                                NULL_TREE,
+                                NULL_TREE);
        }
     }
 
@@ -5946,74 +6133,104 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
   if (matches == NULL_TREE)
     {
       /* There were *no* matches.  */
-      if (complain)
+      if (flags & tf_error)
        {
-         error ("no matches converting function `%D' to type `%#T'", 
-                   DECL_NAME (OVL_FUNCTION (overload)),
-                   target_type);
+         error ("no matches converting function %qD to type %q#T",
+                DECL_NAME (OVL_FUNCTION (overload)),
+                target_type);
 
          /* print_candidates expects a chain with the functions in
-             TREE_VALUE slots, so we cons one up here (we're losing anyway,
-             so why be clever?).  */
-          for (; overload; overload = OVL_NEXT (overload))
-            matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
+            TREE_VALUE slots, so we cons one up here (we're losing anyway,
+            so why be clever?).  */
+         for (; overload; overload = OVL_NEXT (overload))
+           matches = tree_cons (NULL_TREE, OVL_CURRENT (overload),
                                 matches);
-          
+
          print_candidates (matches);
        }
       return error_mark_node;
     }
   else if (TREE_CHAIN (matches))
     {
-      /* There were too many matches.  */
+      /* There were too many matches.  First check if they're all
+        the same function.  */
+      tree match;
+
+      fn = TREE_PURPOSE (matches);
+      for (match = TREE_CHAIN (matches); match; match = TREE_CHAIN (match))
+       if (!decls_match (fn, TREE_PURPOSE (match)))
+         break;
 
-      if (complain)
+      if (match)
        {
-         tree match;
+         if (flags & tf_error)
+           {
+             error ("converting overloaded function %qD to type %q#T is ambiguous",
+                    DECL_NAME (OVL_FUNCTION (overload)),
+                    target_type);
 
-         error ("converting overloaded function `%D' to type `%#T' is ambiguous", 
-                   DECL_NAME (OVL_FUNCTION (overload)),
-                   target_type);
+             /* Since print_candidates expects the functions in the
+                TREE_VALUE slot, we flip them here.  */
+             for (match = matches; match; match = TREE_CHAIN (match))
+               TREE_VALUE (match) = TREE_PURPOSE (match);
 
-         /* Since print_candidates expects the functions in the
-            TREE_VALUE slot, we flip them here.  */
-         for (match = matches; match; match = TREE_CHAIN (match))
-           TREE_VALUE (match) = TREE_PURPOSE (match);
+             print_candidates (matches);
+           }
 
-         print_candidates (matches);
+         return error_mark_node;
        }
-      
-      return error_mark_node;
     }
 
   /* Good, exactly one match.  Now, convert it to the correct type.  */
   fn = TREE_PURPOSE (matches);
 
   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (fn)
-      && !ptrmem && !flag_ms_extensions)
+      && !(flags & tf_ptrmem_ok) && !flag_ms_extensions)
     {
       static int explained;
-      
-      if (!complain)
-        return error_mark_node;
 
-      pedwarn ("assuming pointer to member `%D'", fn);
+      if (!(flags & tf_error))
+       return error_mark_node;
+
+      permerror (input_location, "assuming pointer to member %qD", fn);
       if (!explained)
-        {
-          pedwarn ("(a pointer to member can only be formed with `&%E')", fn);
-          explained = 1;
-        }
+       {
+         inform (input_location, "(a pointer to member can only be formed with %<&%E%>)", fn);
+         explained = 1;
+       }
+    }
+
+  /* If we're doing overload resolution purely for the purpose of
+     determining conversion sequences, we should not consider the
+     function used.  If this conversion sequence is selected, the
+     function will be marked as used at this point.  */
+  if (!(flags & tf_conv))
+    {
+      /* Make =delete work with SFINAE.  */
+      if (DECL_DELETED_FN (fn) && !(flags & tf_error))
+       return error_mark_node;
+      
+      mark_used (fn);
+    }
+
+  /* We could not check access to member functions when this
+     expression was originally created since we did not know at that
+     time to which function the expression referred.  */
+  if (!(flags & tf_no_access_control) 
+      && DECL_FUNCTION_MEMBER_P (fn))
+    {
+      gcc_assert (access_path);
+      perform_or_defer_access_check (access_path, fn, fn);
     }
-  mark_used (fn);
 
   if (TYPE_PTRFN_P (target_type) || TYPE_PTRMEMFUNC_P (target_type))
-    return build_unary_op (ADDR_EXPR, fn, 0);
+    return cp_build_unary_op (ADDR_EXPR, fn, 0, flags);
   else
     {
-      /* The target must be a REFERENCE_TYPE.  Above, build_unary_op
+      /* The target must be a REFERENCE_TYPE.  Above, cp_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;
     }
@@ -6021,43 +6238,70 @@ cannot resolve overloaded function `%D' based on conversion to type `%T'",
 
 /* This function will instantiate the type of the expression given in
    RHS to match the type of LHSTYPE.  If errors exist, then return
-   error_mark_node. FLAGS is a bit mask.  If ITF_COMPLAIN is set, then
+   error_mark_node. FLAGS is a bit mask.  If TF_ERROR is set, then
    we complain on errors.  If we are not complaining, never modify rhs,
    as overload resolution wants to try many possible instantiations, in
    the hope that at least one will work.
-   
+
    For non-recursive calls, LHSTYPE should be a function, pointer to
    function, or a pointer to member function.  */
 
 tree
-instantiate_type (lhstype, rhs, flags)
-     tree lhstype, rhs;
-     tsubst_flags_t flags;
-{
-  int complain = (flags & tf_error);
-  int strict = (flags & tf_no_attributes)
-               ? COMPARE_NO_ATTRIBUTES : COMPARE_STRICT;
-  int allow_ptrmem = flags & tf_ptrmem_ok;
-  
+instantiate_type (tree lhstype, tree rhs, tsubst_flags_t flags)
+{
+  tsubst_flags_t flags_in = flags;
+  tree access_path = NULL_TREE;
+
   flags &= ~tf_ptrmem_ok;
-  
+
   if (TREE_CODE (lhstype) == UNKNOWN_TYPE)
     {
-      if (complain)
+      if (flags & tf_error)
        error ("not enough type information");
       return error_mark_node;
     }
 
   if (TREE_TYPE (rhs) != NULL_TREE && ! (type_unknown_p (rhs)))
     {
-      if (comptypes (lhstype, TREE_TYPE (rhs), strict))
+      if (same_type_p (lhstype, TREE_TYPE (rhs)))
        return rhs;
-      if (complain)
-       error ("argument of type `%T' does not match `%T'",
-                 TREE_TYPE (rhs), lhstype);
+      if (flag_ms_extensions
+         && TYPE_PTRMEMFUNC_P (lhstype)
+         && !TYPE_PTRMEMFUNC_P (TREE_TYPE (rhs)))
+       /* Microsoft allows `A::f' to be resolved to a
+          pointer-to-member.  */
+       ;
+      else
+       {
+         if (flags & tf_error)
+           error ("argument of type %qT does not match %qT",
+                  TREE_TYPE (rhs), lhstype);
+         return error_mark_node;
+       }
+    }
+
+  if (TREE_CODE (rhs) == BASELINK)
+    {
+      access_path = BASELINK_ACCESS_BINFO (rhs);
+      rhs = BASELINK_FUNCTIONS (rhs);
+    }
+
+  /* If we are in a template, and have a NON_DEPENDENT_EXPR, we cannot
+     deduce any type information.  */
+  if (TREE_CODE (rhs) == NON_DEPENDENT_EXPR)
+    {
+      if (flags & tf_error)
+       error ("not enough type information");
       return error_mark_node;
     }
 
+  /* There only a few kinds of expressions that may have a type
+     dependent on overload resolution.  */
+  gcc_assert (TREE_CODE (rhs) == ADDR_EXPR
+             || TREE_CODE (rhs) == COMPONENT_REF
+             || TREE_CODE (rhs) == COMPOUND_EXPR
+             || really_overloaded_fn (rhs));
+
   /* We don't overwrite rhs if it is an overloaded function.
      Copying it would destroy the tree link.  */
   if (TREE_CODE (rhs) != OVERLOAD)
@@ -6070,46 +6314,27 @@ instantiate_type (lhstype, rhs, flags)
 
   switch (TREE_CODE (rhs))
     {
-    case TYPE_EXPR:
-    case CONVERT_EXPR:
-    case SAVE_EXPR:
-    case CONSTRUCTOR:
-    case BUFFER_REF:
-      abort ();
-      return error_mark_node;
-
-    case INDIRECT_REF:
-    case ARRAY_REF:
+    case COMPONENT_REF:
       {
-       tree new_rhs;
-
-       new_rhs = instantiate_type (build_pointer_type (lhstype),
-                                   TREE_OPERAND (rhs, 0), flags);
-       if (new_rhs == error_mark_node)
-         return error_mark_node;
-
-       TREE_TYPE (rhs) = lhstype;
-       TREE_OPERAND (rhs, 0) = new_rhs;
-       return rhs;
+       tree member = TREE_OPERAND (rhs, 1);
+
+       member = instantiate_type (lhstype, member, flags);
+       if (member != error_mark_node
+           && TREE_SIDE_EFFECTS (TREE_OPERAND (rhs, 0)))
+         /* Do not lose object's side effects.  */
+         return build2 (COMPOUND_EXPR, TREE_TYPE (member),
+                        TREE_OPERAND (rhs, 0), member);
+       return member;
       }
 
-    case NOP_EXPR:
-      rhs = copy_node (TREE_OPERAND (rhs, 0));
-      TREE_TYPE (rhs) = unknown_type_node;
-      return instantiate_type (lhstype, rhs, flags);
-
-    case COMPONENT_REF:
-      return instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
-
     case OFFSET_REF:
       rhs = TREE_OPERAND (rhs, 1);
       if (BASELINK_P (rhs))
-       return instantiate_type (lhstype, TREE_VALUE (rhs),
-                                flags | allow_ptrmem);
+       return instantiate_type (lhstype, rhs, flags_in);
 
       /* This can happen if we are forming a pointer-to-member for a
         member template.  */
-      my_friendly_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR, 0);
+      gcc_assert (TREE_CODE (rhs) == TEMPLATE_ID_EXPR);
 
       /* Fall through.  */
 
@@ -6119,36 +6344,19 @@ instantiate_type (lhstype, rhs, flags)
        tree args = TREE_OPERAND (rhs, 1);
 
        return
-         resolve_address_of_overloaded_function (lhstype,
-                                                 fns,
-                                                 complain,
-                                                 allow_ptrmem,
-                                                 /*template_only=*/1,
-                                                 args);
+         resolve_address_of_overloaded_function (lhstype, fns, flags_in,
+                                                 /*template_only=*/true,
+                                                 args, access_path);
       }
 
     case OVERLOAD:
-      return 
-       resolve_address_of_overloaded_function (lhstype, 
-                                               rhs,
-                                               complain,
-                                               allow_ptrmem,
-                                               /*template_only=*/0,
-                                               /*explicit_targs=*/NULL_TREE);
-
-    case TREE_LIST:
-      /* Now we should have a baselink. */
-      my_friendly_assert (BASELINK_P (rhs), 990412);
-
-      return instantiate_type (lhstype, TREE_VALUE (rhs), flags);
-
-    case CALL_EXPR:
-      /* This is too hard for now.  */
-      abort ();
-      return error_mark_node;
+    case FUNCTION_DECL:
+      return
+       resolve_address_of_overloaded_function (lhstype, rhs, flags_in,
+                                               /*template_only=*/false,
+                                               /*explicit_targs=*/NULL_TREE,
+                                               access_path);
 
-    case PLUS_EXPR:
-    case MINUS_EXPR:
     case COMPOUND_EXPR:
       TREE_OPERAND (rhs, 0)
        = instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
@@ -6162,105 +6370,21 @@ instantiate_type (lhstype, rhs, flags)
       TREE_TYPE (rhs) = lhstype;
       return rhs;
 
-    case MULT_EXPR:
-    case TRUNC_DIV_EXPR:
-    case FLOOR_DIV_EXPR:
-    case CEIL_DIV_EXPR:
-    case ROUND_DIV_EXPR:
-    case RDIV_EXPR:
-    case TRUNC_MOD_EXPR:
-    case FLOOR_MOD_EXPR:
-    case CEIL_MOD_EXPR:
-    case ROUND_MOD_EXPR:
-    case FIX_ROUND_EXPR:
-    case FIX_FLOOR_EXPR:
-    case FIX_CEIL_EXPR:
-    case FIX_TRUNC_EXPR:
-    case FLOAT_EXPR:
-    case NEGATE_EXPR:
-    case ABS_EXPR:
-    case MAX_EXPR:
-    case MIN_EXPR:
-    case FFS_EXPR:
-
-    case BIT_AND_EXPR:
-    case BIT_IOR_EXPR:
-    case BIT_XOR_EXPR:
-    case LSHIFT_EXPR:
-    case RSHIFT_EXPR:
-    case LROTATE_EXPR:
-    case RROTATE_EXPR:
-
-    case PREINCREMENT_EXPR:
-    case PREDECREMENT_EXPR:
-    case POSTINCREMENT_EXPR:
-    case POSTDECREMENT_EXPR:
-      if (complain)
-       error ("invalid operation on uninstantiated type");
-      return error_mark_node;
-
-    case TRUTH_AND_EXPR:
-    case TRUTH_OR_EXPR:
-    case TRUTH_XOR_EXPR:
-    case LT_EXPR:
-    case LE_EXPR:
-    case GT_EXPR:
-    case GE_EXPR:
-    case EQ_EXPR:
-    case NE_EXPR:
-    case TRUTH_ANDIF_EXPR:
-    case TRUTH_ORIF_EXPR:
-    case TRUTH_NOT_EXPR:
-      if (complain)
-       error ("not enough type information");
-      return error_mark_node;
-
-    case COND_EXPR:
-      if (type_unknown_p (TREE_OPERAND (rhs, 0)))
-       {
-         if (complain)
-           error ("not enough type information");
-         return error_mark_node;
-       }
-      TREE_OPERAND (rhs, 1)
-       = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
-      if (TREE_OPERAND (rhs, 1) == error_mark_node)
-       return error_mark_node;
-      TREE_OPERAND (rhs, 2)
-       = instantiate_type (lhstype, TREE_OPERAND (rhs, 2), flags);
-      if (TREE_OPERAND (rhs, 2) == error_mark_node)
-       return error_mark_node;
-
-      TREE_TYPE (rhs) = lhstype;
-      return rhs;
-
-    case MODIFY_EXPR:
-      TREE_OPERAND (rhs, 1)
-       = instantiate_type (lhstype, TREE_OPERAND (rhs, 1), flags);
-      if (TREE_OPERAND (rhs, 1) == error_mark_node)
-       return error_mark_node;
-
-      TREE_TYPE (rhs) = lhstype;
-      return rhs;
-      
     case ADDR_EXPR:
     {
       if (PTRMEM_OK_P (rhs))
-        flags |= tf_ptrmem_ok;
-      
+       flags |= tf_ptrmem_ok;
+
       return instantiate_type (lhstype, TREE_OPERAND (rhs, 0), flags);
     }
-    case ENTRY_VALUE_EXPR:
-      abort ();
-      return error_mark_node;
 
     case ERROR_MARK:
       return error_mark_node;
 
     default:
-      abort ();
-      return error_mark_node;
+      gcc_unreachable ();
     }
+  return error_mark_node;
 }
 \f
 /* Return the name of the virtual function pointer field
@@ -6270,16 +6394,21 @@ instantiate_type (lhstype, rhs, flags)
    all be the same name.  Who knows for multiple inheritance).  */
 
 static tree
-get_vfield_name (type)
-     tree type;
+get_vfield_name (tree type)
 {
-  tree binfo = TYPE_BINFO (type);
+  tree binfo, base_binfo;
   char *buf;
 
-  while (BINFO_BASETYPES (binfo)
-        && TYPE_CONTAINS_VPTR_P (BINFO_TYPE (BINFO_BASETYPE (binfo, 0)))
-        && ! TREE_VIA_VIRTUAL (BINFO_BASETYPE (binfo, 0)))
-    binfo = BINFO_BASETYPE (binfo, 0);
+  for (binfo = TYPE_BINFO (type);
+       BINFO_N_BASE_BINFOS (binfo);
+       binfo = base_binfo)
+    {
+      base_binfo = BINFO_BASE_BINFO (binfo, 0);
+
+      if (BINFO_VIRTUAL_P (base_binfo)
+         || !TYPE_CONTAINS_VPTR_P (BINFO_TYPE (base_binfo)))
+       break;
+    }
 
   type = BINFO_TYPE (binfo);
   buf = (char *) alloca (sizeof (VFIELD_NAME_FORMAT)
@@ -6290,13 +6419,11 @@ get_vfield_name (type)
 }
 
 void
-print_class_statistics ()
+print_class_statistics (void)
 {
 #ifdef GATHER_STATISTICS
   fprintf (stderr, "convert_harshness = %d\n", n_convert_harshness);
   fprintf (stderr, "compute_conversion_costs = %d\n", n_compute_conversion_costs);
-  fprintf (stderr, "build_method_call = %d (inner = %d)\n",
-          n_build_method_call, n_inner_fields_searched);
   if (n_vtables)
     {
       fprintf (stderr, "vtables = %d; vtable searches = %d\n",
@@ -6309,12 +6436,12 @@ print_class_statistics ()
 
 /* Build a dummy reference to ourselves so Derived::Base (and A::A) works,
    according to [class]:
-                                          The class-name is also inserted
+                                         The class-name is also inserted
    into  the scope of the class itself.  For purposes of access checking,
    the inserted class name is treated as if it were a public member name.  */
 
 void
-build_self_reference ()
+build_self_reference (void)
 {
   tree name = constructor_name (current_class_type);
   tree value = build_lang_decl (TYPE_DECL, name, current_class_type);
@@ -6323,6 +6450,7 @@ build_self_reference ()
   DECL_NONLOCAL (value) = 1;
   DECL_CONTEXT (value) = current_class_type;
   DECL_ARTIFICIAL (value) = 1;
+  SET_DECL_SELF_REFERENCE_P (value);
 
   if (processing_template_decl)
     value = push_template_decl (value);
@@ -6336,81 +6464,99 @@ build_self_reference ()
 /* Returns 1 if TYPE contains only padding bytes.  */
 
 int
-is_empty_class (type)
-     tree type;
+is_empty_class (tree type)
 {
   if (type == error_mark_node)
     return 0;
 
-  if (! IS_AGGR_TYPE (type))
+  if (! CLASS_TYPE_P (type))
     return 0;
 
-  return integer_zerop (CLASSTYPE_SIZE (type));
+  /* In G++ 3.2, whether or not a class was empty was determined by
+     looking at its size.  */
+  if (abi_version_at_least (2))
+    return CLASSTYPE_EMPTY_P (type);
+  else
+    return integer_zerop (CLASSTYPE_SIZE (type));
 }
 
-/* Find the enclosing class of the given NODE.  NODE can be a *_DECL or
-   a *_TYPE node.  NODE can also be a local class.  */
+/* Returns true if TYPE contains an empty class.  */
 
-tree
-get_enclosing_class (type)
-     tree type;
+static bool
+contains_empty_class_p (tree type)
 {
-  tree node = type;
-
-  while (node && TREE_CODE (node) != NAMESPACE_DECL)
+  if (is_empty_class (type))
+    return true;
+  if (CLASS_TYPE_P (type))
     {
-      switch (TREE_CODE_CLASS (TREE_CODE (node)))
-       {
-       case 'd':
-         node = DECL_CONTEXT (node);
-         break;
-
-       case 't':
-         if (node != type)
-           return node;
-         node = TYPE_CONTEXT (node);
-         break;
+      tree field;
+      tree binfo;
+      tree base_binfo;
+      int i;
 
-       default:
-         abort ();
-       }
+      for (binfo = TYPE_BINFO (type), i = 0;
+          BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
+       if (contains_empty_class_p (BINFO_TYPE (base_binfo)))
+         return true;
+      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
+       if (TREE_CODE (field) == FIELD_DECL
+           && !DECL_ARTIFICIAL (field)
+           && is_empty_class (TREE_TYPE (field)))
+         return true;
     }
-  return NULL_TREE;
+  else if (TREE_CODE (type) == ARRAY_TYPE)
+    return contains_empty_class_p (TREE_TYPE (type));
+  return false;
 }
 
-/* Return 1 if TYPE or one of its enclosing classes is derived from BASE.  */
+/* Returns true if TYPE contains no actual data, just various
+   possible combinations of empty classes.  */
 
-int
-is_base_of_enclosing_class (base, type)
-     tree base, type;
+bool
+is_really_empty_class (tree type)
 {
-  while (type)
+  if (is_empty_class (type))
+    return true;
+  if (CLASS_TYPE_P (type))
     {
-      if (lookup_base (type, base, ba_any, NULL))
-       return 1;
+      tree field;
+      tree binfo;
+      tree base_binfo;
+      int i;
 
-      type = get_enclosing_class (type);
+      for (binfo = TYPE_BINFO (type), i = 0;
+          BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
+       if (!is_really_empty_class (BINFO_TYPE (base_binfo)))
+         return false;
+      for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
+       if (TREE_CODE (field) == FIELD_DECL
+           && !DECL_ARTIFICIAL (field)
+           && !is_really_empty_class (TREE_TYPE (field)))
+         return false;
+      return true;
     }
-  return 0;
+  else if (TREE_CODE (type) == ARRAY_TYPE)
+    return is_really_empty_class (TREE_TYPE (type));
+  return false;
 }
 
 /* Note that NAME was looked up while the current class was being
    defined and that the result of that lookup was DECL.  */
 
 void
-maybe_note_name_used_in_class (name, decl)
-     tree name;
-     tree decl;
+maybe_note_name_used_in_class (tree name, tree decl)
 {
   splay_tree names_used;
 
   /* If we're not defining a class, there's nothing to do.  */
-  if (!current_class_type || !TYPE_BEING_DEFINED (current_class_type))
+  if (!(innermost_scope_kind() == sk_class
+       && TYPE_BEING_DEFINED (current_class_type)))
     return;
-  
+
   /* If there's already a binding for this NAME, then we don't have
      anything to worry about.  */
-  if (IDENTIFIER_CLASS_VALUE (name))
+  if (lookup_member (current_class_type, name,
+                    /*protect=*/0, /*want_type=*/false))
     return;
 
   if (!current_class_stack[current_class_depth - 1].names_used)
@@ -6419,23 +6565,21 @@ maybe_note_name_used_in_class (name, decl)
   names_used = current_class_stack[current_class_depth - 1].names_used;
 
   splay_tree_insert (names_used,
-                    (splay_tree_key) name, 
+                    (splay_tree_key) name,
                     (splay_tree_value) 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)
-     tree name;
-     tree decl;
+note_name_declared_in_class (tree name, tree decl)
 {
   splay_tree names_used;
   splay_tree_node n;
 
   /* Look to see if we ever used this name.  */
-  names_used 
+  names_used
     = current_class_stack[current_class_depth - 1].names_used;
   if (!names_used)
     return;
@@ -6444,14 +6588,13 @@ note_name_declared_in_class (name, decl)
   if (n)
     {
       /* [basic.scope.class]
-        
+
         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.  */
-      error ("declaration of `%#D'", decl);
-      cp_error_at ("changes meaning of `%D' from `%+#D'", 
-                  DECL_NAME (OVL_CURRENT (decl)),
-                  (tree) n->value);
+      permerror (input_location, "declaration of %q#D", decl);
+      permerror (input_location, "changes meaning of %qD from %q+#D",
+              DECL_NAME (OVL_CURRENT (decl)), (tree) n->value);
     }
 }
 
@@ -6460,190 +6603,95 @@ note_name_declared_in_class (name, decl)
    will return the VAR_DECL for the primary vtable.  */
 
 tree
-get_vtbl_decl_for_binfo (binfo)
-     tree binfo;
+get_vtbl_decl_for_binfo (tree binfo)
 {
   tree decl;
 
   decl = BINFO_VTABLE (binfo);
-  if (decl && TREE_CODE (decl) == PLUS_EXPR)
+  if (decl && TREE_CODE (decl) == POINTER_PLUS_EXPR)
     {
-      my_friendly_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR,
-                         2000403);
+      gcc_assert (TREE_CODE (TREE_OPERAND (decl, 0)) == ADDR_EXPR);
       decl = TREE_OPERAND (TREE_OPERAND (decl, 0), 0);
     }
   if (decl)
-    my_friendly_assert (TREE_CODE (decl) == VAR_DECL, 20000403);
+    gcc_assert (TREE_CODE (decl) == VAR_DECL);
   return decl;
 }
 
-/* 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 cons = (tree) data;
-  tree primary_base = TREE_PURPOSE (cons);
-
-  if (TREE_VIA_VIRTUAL (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 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.  */
+/* Returns the binfo for the primary base of BINFO.  If the resulting
+   BINFO is a virtual base, and it is inherited elsewhere in the
+   hierarchy, then the returned binfo might not be the primary base of
+   BINFO in the complete object.  Check BINFO_PRIMARY_P or
+   BINFO_LOST_PRIMARY_P to be sure.  */
 
-tree
-get_primary_binfo (binfo)
-     tree binfo;
+static tree
+get_primary_binfo (tree binfo)
 {
   tree primary_base;
-  tree result = NULL_TREE;
-  tree virtuals;
-  
+
   primary_base = CLASSTYPE_PRIMARY_BINFO (BINFO_TYPE (binfo));
   if (!primary_base)
     return NULL_TREE;
 
-  /* A non-virtual primary base is always a direct base, and easy to
-     find.  */
-  if (!TREE_VIA_VIRTUAL (primary_base))
-    {
-      int i;
-
-      /* Scan the direct basetypes until we find a base with the same
-        type as the primary base.  */
-      for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
-       {
-         tree base_binfo = BINFO_BASETYPE (binfo, i);
-         
-         if (same_type_p (BINFO_TYPE (base_binfo),
-                          BINFO_TYPE (primary_base)))
-           return base_binfo;
-       }
-
-      /* 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;
-           }
-       }
-    }
-  else
-    result = TREE_VALUE (virtuals);
-  return result;
+  return copied_binfo (primary_base, binfo);
 }
 
-/* If INDENTED_P is zero, indent to INDENT. Return non-zero. */
+/* If INDENTED_P is zero, indent to INDENT. Return nonzero.  */
 
 static int
-maybe_indent_hierarchy (stream, indent, indented_p)
-     FILE *stream;
-     int indent;
-     int indented_p;
+maybe_indent_hierarchy (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.  */
+/* Dump the offsets of all the bases rooted at BINFO to STREAM.
+   INDENT should be zero when called from the top level; it is
+   incremented recursively.  IGO indicates the next expected BINFO in
+   inheritance graph ordering.  */
 
-static void
-dump_class_hierarchy_r (stream, flags, t, binfo, indent)
-     FILE *stream;
-     int flags;
-     tree t;
-     tree binfo;
-     int indent;
+static tree
+dump_class_hierarchy_r (FILE *stream,
+                       int flags,
+                       tree binfo,
+                       tree igo,
+                       int indent)
 {
-  int i;
   int indented = 0;
-  
+  tree base_binfo;
+  int i;
+
   indented = maybe_indent_hierarchy (stream, indent, 0);
   fprintf (stream, "%s (0x%lx) ",
-          type_as_string (binfo, TFF_PLAIN_IDENTIFIER),
+          type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER),
           (unsigned long) binfo);
+  if (binfo != igo)
+    {
+      fprintf (stream, "alternative-path\n");
+      return igo;
+    }
+  igo = TREE_CHAIN (binfo);
+
   fprintf (stream, HOST_WIDE_INT_PRINT_DEC,
           tree_low_cst (BINFO_OFFSET (binfo), 0));
   if (is_empty_class (BINFO_TYPE (binfo)))
     fprintf (stream, " empty");
   else if (CLASSTYPE_NEARLY_EMPTY_P (BINFO_TYPE (binfo)))
     fprintf (stream, " nearly-empty");
-  if (TREE_VIA_VIRTUAL (binfo))
-    {
-      tree canonical = binfo_for_vbase (BINFO_TYPE (binfo), t);
-
-      fprintf (stream, " virtual");
-      if (canonical == binfo)
-        fprintf (stream, " canonical");
-      else
-        fprintf (stream, " non-canonical");
-    }
+  if (BINFO_VIRTUAL_P (binfo))
+    fprintf (stream, " virtual");
   fprintf (stream, "\n");
 
   indented = 0;
-  if (BINFO_PRIMARY_BASE_OF (binfo))
+  if (BINFO_PRIMARY_P (binfo))
     {
       indented = maybe_indent_hierarchy (stream, indent + 3, indented);
       fprintf (stream, " primary-for %s (0x%lx)",
-              type_as_string (BINFO_PRIMARY_BASE_OF (binfo),
+              type_as_string (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
                               TFF_PLAIN_IDENTIFIER),
-              (unsigned long)BINFO_PRIMARY_BASE_OF (binfo));
+              (unsigned long)BINFO_INHERITANCE_CHAIN (binfo));
     }
   if (BINFO_LOST_PRIMARY_P (binfo))
     {
@@ -6656,7 +6704,7 @@ dump_class_hierarchy_r (stream, flags, t, binfo, indent)
   if (!(flags & TDF_SLIM))
     {
       int indented = 0;
-      
+
       if (BINFO_SUBVTT_INDEX (binfo))
        {
          indented = maybe_indent_hierarchy (stream, indent + 3, indented);
@@ -6685,46 +6733,61 @@ dump_class_hierarchy_r (stream, flags, t, binfo, indent)
                   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, flags,
-                           t, BINFO_BASETYPE (binfo, i),
-                           indent + 2);
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
+    igo = dump_class_hierarchy_r (stream, flags, base_binfo, igo, indent + 2);
+
+  return igo;
 }
 
 /* Dump the BINFO hierarchy for T.  */
 
 static void
-dump_class_hierarchy (t)
-     tree t;
+dump_class_hierarchy_1 (FILE *stream, int flags, tree t)
 {
-  int flags;
-  FILE *stream = dump_begin (TDI_class, &flags);
-
-  if (!stream)
-    return;
-  
   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, "   base size=%lu base align=%lu\n",
+          (unsigned long)(tree_low_cst (TYPE_SIZE (CLASSTYPE_AS_BASE (t)), 0)
+                          / BITS_PER_UNIT),
+          (unsigned long)(TYPE_ALIGN (CLASSTYPE_AS_BASE (t))
+                          / BITS_PER_UNIT));
+  dump_class_hierarchy_r (stream, flags, TYPE_BINFO (t), TYPE_BINFO (t), 0);
   fprintf (stream, "\n");
-  dump_end (TDI_class, stream);
+}
+
+/* Debug interface to hierarchy dumping.  */
+
+void
+debug_class (tree t)
+{
+  dump_class_hierarchy_1 (stderr, TDF_SLIM, t);
 }
 
 static void
-dump_array (stream, decl)
-     FILE *stream;
-     tree decl;
+dump_class_hierarchy (tree t)
 {
-  tree inits;
-  int ix;
+  int flags;
+  FILE *stream = dump_begin (TDI_class, &flags);
+
+  if (stream)
+    {
+      dump_class_hierarchy_1 (stream, flags, t);
+      dump_end (TDI_class, stream);
+    }
+}
+
+static void
+dump_array (FILE * stream, tree decl)
+{
+  tree value;
+  unsigned HOST_WIDE_INT ix;
   HOST_WIDE_INT elt;
   tree size = TYPE_MAX_VALUE (TYPE_DOMAIN (TREE_TYPE (decl)));
 
@@ -6736,17 +6799,14 @@ dump_array (stream, decl)
                           TFF_PLAIN_IDENTIFIER));
   fprintf (stream, "\n");
 
-  for (ix = 0, inits = TREE_OPERAND (DECL_INITIAL (decl), 1);
-       inits; ix++, inits = TREE_CHAIN (inits))
+  FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (DECL_INITIAL (decl)),
+                             ix, value)
     fprintf (stream, "%-4ld  %s\n", (long)(ix * elt),
-            expr_as_string (TREE_VALUE (inits), TFF_PLAIN_IDENTIFIER));
+            expr_as_string (value, TFF_PLAIN_IDENTIFIER));
 }
 
 static void
-dump_vtable (t, binfo, vtable)
-     tree t;
-     tree binfo;
-     tree vtable;
+dump_vtable (tree t, tree binfo, tree vtable)
 {
   int flags;
   FILE *stream = dump_begin (TDI_class, &flags);
@@ -6757,13 +6817,13 @@ dump_vtable (t, binfo, vtable)
   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));
+              type_as_string (BINFO_TYPE (binfo), TFF_PLAIN_IDENTIFIER));
       if (ctor_vtbl_p)
        {
-         if (!TREE_VIA_VIRTUAL (binfo))
+         if (!BINFO_VIRTUAL_P (binfo))
            fprintf (stream, " (0x%lx instance)", (unsigned long)binfo);
          fprintf (stream, " in %s", type_as_string (t, TFF_PLAIN_IDENTIFIER));
        }
@@ -6771,14 +6831,12 @@ dump_vtable (t, binfo, vtable)
       dump_array (stream, vtable);
       fprintf (stream, "\n");
     }
-  
+
   dump_end (TDI_class, stream);
 }
 
 static void
-dump_vtt (t, vtt)
-     tree t;
-     tree vtt;
+dump_vtt (tree t, tree vtt)
 {
   int flags;
   FILE *stream = dump_begin (TDI_class, &flags);
@@ -6793,96 +6851,100 @@ dump_vtt (t, vtt)
       dump_array (stream, vtt);
       fprintf (stream, "\n");
     }
-  
+
   dump_end (TDI_class, stream);
 }
 
+/* Dump a function or thunk and its thunkees.  */
+
+static void
+dump_thunk (FILE *stream, int indent, tree thunk)
+{
+  static const char spaces[] = "        ";
+  tree name = DECL_NAME (thunk);
+  tree thunks;
+
+  fprintf (stream, "%.*s%p %s %s", indent, spaces,
+          (void *)thunk,
+          !DECL_THUNK_P (thunk) ? "function"
+          : DECL_THIS_THUNK_P (thunk) ? "this-thunk" : "covariant-thunk",
+          name ? IDENTIFIER_POINTER (name) : "<unset>");
+  if (DECL_THUNK_P (thunk))
+    {
+      HOST_WIDE_INT fixed_adjust = THUNK_FIXED_OFFSET (thunk);
+      tree virtual_adjust = THUNK_VIRTUAL_OFFSET (thunk);
+
+      fprintf (stream, " fixed=" HOST_WIDE_INT_PRINT_DEC, fixed_adjust);
+      if (!virtual_adjust)
+       /*NOP*/;
+      else if (DECL_THIS_THUNK_P (thunk))
+       fprintf (stream, " vcall="  HOST_WIDE_INT_PRINT_DEC,
+                tree_low_cst (virtual_adjust, 0));
+      else
+       fprintf (stream, " vbase=" HOST_WIDE_INT_PRINT_DEC "(%s)",
+                tree_low_cst (BINFO_VPTR_FIELD (virtual_adjust), 0),
+                type_as_string (BINFO_TYPE (virtual_adjust), TFF_SCOPE));
+      if (THUNK_ALIAS (thunk))
+       fprintf (stream, " alias to %p", (void *)THUNK_ALIAS (thunk));
+    }
+  fprintf (stream, "\n");
+  for (thunks = DECL_THUNKS (thunk); thunks; thunks = TREE_CHAIN (thunks))
+    dump_thunk (stream, indent + 2, thunks);
+}
+
+/* Dump the thunks for FN.  */
+
+void
+debug_thunks (tree fn)
+{
+  dump_thunk (stderr, 0, fn);
+}
+
 /* Virtual function table initialization.  */
 
 /* Create all the necessary vtables for T and its base classes.  */
 
 static void
-finish_vtbls (t)
-     tree t;
+finish_vtbls (tree t)
 {
   tree list;
   tree vbase;
-  int i;
 
   /* 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);
+  list = build_tree_list (BINFO_VTABLE (TYPE_BINFO (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))
+      if (!BINFO_VIRTUAL_P (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);
-    }
-
-  /* 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);
-       }
+      accumulate_vtbl_inits (vbase, vbase, TYPE_BINFO (t), t, list);
     }
 
-  if (TYPE_BINFO_VTABLE (t))
+  if (BINFO_VTABLE (TYPE_BINFO (t)))
     initialize_vtable (TYPE_BINFO (t), TREE_VALUE (list));
 }
 
 /* Initialize the vtable for BINFO with the INITS.  */
 
 static void
-initialize_vtable (binfo, inits)
-     tree binfo;
-     tree inits;
+initialize_vtable (tree binfo, tree inits)
 {
   tree decl;
 
   layout_vtable_decl (binfo, list_length (inits));
   decl = get_vtbl_decl_for_binfo (binfo);
-  initialize_array (decl, inits);
+  initialize_artificial_var (decl, inits);
   dump_vtable (BINFO_TYPE (binfo), binfo, decl);
 }
 
-/* Initialize DECL (a declaration for a namespace-scope array) with
-   the INITS.  */
-
-static void
-initialize_array (decl, inits)
-  tree decl;
-  tree inits;
-{
-  tree context;
-
-  context = DECL_CONTEXT (decl);
-  DECL_CONTEXT (decl) = NULL_TREE;
-  DECL_INITIAL (decl) = build_nt (CONSTRUCTOR, NULL_TREE, inits);
-  cp_finish_decl (decl, DECL_INITIAL (decl), NULL_TREE, 0);
-  DECL_CONTEXT (decl) = context;
-}
-
 /* Build the VTT (virtual table table) for T.
    A class requires a VTT if it has virtual bases.
-   
+
    This holds
    1 - primary virtual pointer for complete object T
    2 - secondary VTTs for each direct non-virtual base of T which requires a
@@ -6890,12 +6952,11 @@ initialize_array (decl, inits)
    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.
-   
+
    Secondary VTTs look like complete object VTTs without part 4.  */
 
 static void
-build_vtt (t)
-     tree t;
+build_vtt (tree t)
 {
   tree inits;
   tree type;
@@ -6914,50 +6975,24 @@ build_vtt (t)
   /* Figure out the type of the VTT.  */
   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);
-  pushdecl_top_level (vtt);
-  initialize_array (vtt, inits);
+  vtt = build_vtable (t, mangle_vtt_for_type (t), type);
+  initialize_artificial_var (vtt, inits);
+  /* Add the VTT to the vtables list.  */
+  TREE_CHAIN (vtt) = TREE_CHAIN (CLASSTYPE_VTABLES (t));
+  TREE_CHAIN (CLASSTYPE_VTABLES (t)) = vtt;
 
   dump_vtt (t, vtt);
 }
 
-/* The type corresponding to BASE_BINFO is a base of the type of BINFO, but
-   from within some hierarchy which is inherited from the type of BINFO.
-   Return BASE_BINFO's equivalent binfo from the hierarchy dominated by
-   BINFO.  */
-
-static tree
-get_original_base (base_binfo, binfo)
-     tree base_binfo;
-     tree binfo;
-{
-  tree derived;
-  int ix;
-  
-  if (same_type_p (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
-    return binfo;
-  if (TREE_VIA_VIRTUAL (base_binfo))
-    return binfo_for_vbase (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo));
-  derived = get_original_base (BINFO_INHERITANCE_CHAIN (base_binfo), binfo);
-  
-  for (ix = 0; ix != BINFO_N_BASETYPES (derived); ix++)
-    if (same_type_p (BINFO_TYPE (base_binfo),
-                     BINFO_TYPE (BINFO_BASETYPE (derived, ix))))
-      return BINFO_BASETYPE (derived, ix);
-  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. */
+   complete.  VALUE can also be another BINFO, in which case we recurse.  */
 
 static tree
-binfo_ctor_vtable (binfo)
-     tree binfo;
+binfo_ctor_vtable (tree binfo)
 {
   tree vt;
 
@@ -6966,7 +7001,7 @@ binfo_ctor_vtable (binfo)
       vt = BINFO_VTABLE (binfo);
       if (TREE_CODE (vt) == TREE_LIST)
        vt = TREE_VALUE (vt);
-      if (TREE_CODE (vt) == TREE_VEC)
+      if (TREE_CODE (vt) == TREE_BINFO)
        binfo = vt;
       else
        break;
@@ -6975,6 +7010,22 @@ binfo_ctor_vtable (binfo)
   return vt;
 }
 
+/* Data for secondary VTT initialization.  */
+typedef struct secondary_vptr_vtt_init_data_s
+{
+  /* Is this the primary VTT? */
+  bool top_level_p;
+
+  /* Current index into the VTT.  */
+  tree index;
+
+  /* TREE_LIST of initializers built up.  */
+  tree inits;
+
+  /* The type being constructed by this secondary VTT.  */
+  tree type_being_constructed;
+} secondary_vptr_vtt_init_data;
+
 /* 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
@@ -6984,20 +7035,17 @@ binfo_ctor_vtable (binfo)
    vtables for the BINFO-in-T variant.  */
 
 static tree *
-build_vtt_inits (binfo, t, inits, index)
-     tree binfo;
-     tree t;
-     tree *inits;
-     tree *index;
+build_vtt_inits (tree binfo, tree t, tree *inits, tree *index)
 {
   int i;
   tree b;
   tree init;
   tree secondary_vptrs;
-  int top_level_p = same_type_p (TREE_TYPE (binfo), t);
+  secondary_vptr_vtt_init_data data;
+  int top_level_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
 
   /* We only need VTTs for subobjects with virtual bases.  */
-  if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
+  if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
     return inits;
 
   /* We need to use a construction vtable if this is not the primary
@@ -7016,186 +7064,134 @@ build_vtt_inits (binfo, t, inits, index)
   inits = &TREE_CHAIN (*inits);
   if (top_level_p)
     {
-      my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
+      gcc_assert (!BINFO_VPTR_INDEX (binfo));
       BINFO_VPTR_INDEX (binfo) = *index;
     }
   *index = size_binop (PLUS_EXPR, *index, TYPE_SIZE_UNIT (ptr_type_node));
-                      
+
   /* Recursively add the secondary VTTs for non-virtual bases.  */
-  for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
-    {
-      b = BINFO_BASETYPE (binfo, i);
-      if (!TREE_VIA_VIRTUAL (b))
-       inits = build_vtt_inits (BINFO_BASETYPE (binfo, i), t, 
-                                inits, index);
-    }
-      
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, b); ++i)
+    if (!BINFO_VIRTUAL_P (b))
+      inits = build_vtt_inits (b, t, inits, index);
+
   /* Add secondary virtual pointers for all subobjects of BINFO with
      either virtual bases or reachable along a virtual path, except
      subobjects that are non-virtual primary bases.  */
-  secondary_vptrs = tree_cons (t, NULL_TREE, BINFO_TYPE (binfo));
-  TREE_TYPE (secondary_vptrs) = *index;
-  VTT_TOP_LEVEL_P (secondary_vptrs) = top_level_p;
-  VTT_MARKED_BINFO_P (secondary_vptrs) = 0;
-  
-  dfs_walk_real (binfo,
-                dfs_build_secondary_vptr_vtt_inits,
-                NULL,
-                dfs_ctor_vtable_bases_queue_p,
-                secondary_vptrs);
-  VTT_MARKED_BINFO_P (secondary_vptrs) = 1;
-  dfs_walk (binfo, dfs_unmark, dfs_ctor_vtable_bases_queue_p,
-            secondary_vptrs);
-
-  *index = TREE_TYPE (secondary_vptrs);
+  data.top_level_p = top_level_p;
+  data.index = *index;
+  data.inits = NULL;
+  data.type_being_constructed = BINFO_TYPE (binfo);
+
+  dfs_walk_once (binfo, dfs_build_secondary_vptr_vtt_inits, NULL, &data);
+
+  *index = data.index;
 
   /* The secondary vptrs come back in reverse order.  After we reverse
      them, and add the INITS, the last init will be the first element
      of the chain.  */
-  secondary_vptrs = TREE_VALUE (secondary_vptrs);
+  secondary_vptrs = data.inits;
   if (secondary_vptrs)
     {
       *inits = nreverse (secondary_vptrs);
       inits = &TREE_CHAIN (secondary_vptrs);
-      my_friendly_assert (*inits == NULL_TREE, 20000517);
+      gcc_assert (*inits == NULL_TREE);
     }
 
-  /* Add the secondary VTTs for virtual bases.  */
   if (top_level_p)
+    /* Add the secondary VTTs for virtual bases in inheritance graph
+       order.  */
     for (b = TYPE_BINFO (BINFO_TYPE (binfo)); b; b = TREE_CHAIN (b))
       {
-       tree vbase;
-       
-       if (!TREE_VIA_VIRTUAL (b))
+       if (!BINFO_VIRTUAL_P (b))
          continue;
-       
-       vbase = binfo_for_vbase (BINFO_TYPE (b), t);
-       inits = build_vtt_inits (vbase, t, inits, index);
-      }
 
-  if (!top_level_p)
-    {
-      tree data = tree_cons (t, binfo, NULL_TREE);
-      VTT_TOP_LEVEL_P (data) = 0;
-      VTT_MARKED_BINFO_P (data) = 0;
-      
-      dfs_walk (binfo, dfs_fixup_binfo_vtbls,
-               dfs_ctor_vtable_bases_queue_p,
-               data);
-    }
+       inits = build_vtt_inits (b, t, inits, index);
+      }
+  else
+    /* Remove the ctor vtables we created.  */
+    dfs_walk_all (binfo, dfs_fixup_binfo_vtbls, NULL, binfo);
 
   return inits;
 }
 
-/* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo
-   for the base in most derived. DATA is a TREE_LIST who's
-   TREE_CHAIN is the type of the base being
-   constructed whilst this secondary vptr is live.  The TREE_UNSIGNED
-   flag of DATA indicates that this is a constructor vtable.  The
-   TREE_TOP_LEVEL flag indicates that this is the primary VTT.  */
+/* Called from build_vtt_inits via dfs_walk.  BINFO is the binfo for the base
+   in most derived. DATA is a SECONDARY_VPTR_VTT_INIT_DATA structure.  */
 
 static tree
-dfs_build_secondary_vptr_vtt_inits (binfo, data)
-     tree binfo;
-     void *data;
+dfs_build_secondary_vptr_vtt_inits (tree binfo, void *data_)
 {
-  tree l; 
-  tree t;
-  tree init;
-  tree index;
-  int top_level_p;
-
-  l = (tree) data;
-  t = TREE_CHAIN (l);
-  top_level_p = VTT_TOP_LEVEL_P (l);
-  
-  SET_BINFO_MARKED (binfo);
+  secondary_vptr_vtt_init_data *data = (secondary_vptr_vtt_init_data *)data_;
 
   /* We don't care about bases that don't have vtables.  */
   if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
-    return NULL_TREE;
+    return dfs_skip_bases;
 
-  /* We're only interested in proper subobjects of T.  */
-  if (same_type_p (BINFO_TYPE (binfo), t))
+  /* We're only interested in proper subobjects of the type being
+     constructed.  */
+  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->type_being_constructed))
     return NULL_TREE;
 
-  /* We're not interested in non-virtual primary bases.  */
-  if (!TREE_VIA_VIRTUAL (binfo) && BINFO_PRIMARY_P (binfo))
-    return NULL_TREE;
+  /* We're only interested in bases with virtual bases or reachable
+     via a virtual path from the type being constructed.  */
+  if (!(CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
+       || binfo_via_virtual (binfo, data->type_being_constructed)))
+    return dfs_skip_bases;
 
-  /* If BINFO has virtual bases or is reachable via a virtual path
-     from T, it'll have a secondary vptr.  */
-  if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
-      && !binfo_via_virtual (binfo, t))
+  /* We're not interested in non-virtual primary bases.  */
+  if (!BINFO_VIRTUAL_P (binfo) && BINFO_PRIMARY_P (binfo))
     return NULL_TREE;
 
   /* Record the index where this secondary vptr can be found.  */
-  index = TREE_TYPE (l);
-  if (top_level_p)
+  if (data->top_level_p)
     {
-      my_friendly_assert (!BINFO_VPTR_INDEX (binfo), 20010129);
-      BINFO_VPTR_INDEX (binfo) = index;
+      gcc_assert (!BINFO_VPTR_INDEX (binfo));
+      BINFO_VPTR_INDEX (binfo) = data->index;
+
+      if (BINFO_VIRTUAL_P (binfo))
+       {
+         /* It's a primary virtual base, and this is not a
+            construction vtable.  Find the base this is primary of in
+            the inheritance graph, and use that base's vtable
+            now.  */
+         while (BINFO_PRIMARY_P (binfo))
+           binfo = BINFO_INHERITANCE_CHAIN (binfo);
+       }
     }
-  TREE_TYPE (l) = size_binop (PLUS_EXPR, index, 
-                             TYPE_SIZE_UNIT (ptr_type_node));
 
   /* Add the initializer for the secondary vptr itself.  */
-  if (top_level_p && TREE_VIA_VIRTUAL (binfo))
-    {
-      /* It's a primary virtual base, and this is not the construction
-         vtable. Find the base this is primary of in the inheritance graph,
-         and use that base's vtable now. */
-      while (BINFO_PRIMARY_BASE_OF (binfo))
-        binfo = BINFO_PRIMARY_BASE_OF (binfo);
-    }
-  init = binfo_ctor_vtable (binfo);
-  TREE_VALUE (l) = tree_cons (NULL_TREE, init, TREE_VALUE (l));
+  data->inits = tree_cons (NULL_TREE, binfo_ctor_vtable (binfo), data->inits);
+
+  /* Advance the vtt index.  */
+  data->index = size_binop (PLUS_EXPR, data->index,
+                           TYPE_SIZE_UNIT (ptr_type_node));
 
   return NULL_TREE;
 }
 
-/* dfs_walk_real predicate for building vtables. DATA is a TREE_LIST,
-   VTT_MARKED_BINFO_P indicates whether marked or unmarked bases
-   should be walked.  TREE_PURPOSE is the TREE_TYPE that dominates the
-   hierarchy.  */
+/* Called from build_vtt_inits via dfs_walk. After building
+   constructor vtables and generating the sub-vtt from them, we need
+   to restore the BINFO_VTABLES that were scribbled on.  DATA is the
+   binfo of the base whose sub vtt was generated.  */
 
 static tree
-dfs_ctor_vtable_bases_queue_p (binfo, data)
-     tree binfo;
-     void *data;
+dfs_fixup_binfo_vtbls (tree binfo, void* data)
 {
-  if (TREE_VIA_VIRTUAL (binfo))
-     /* Get the shared version.  */
-    binfo = binfo_for_vbase (BINFO_TYPE (binfo), TREE_PURPOSE ((tree) data));
+  tree vtable = BINFO_VTABLE (binfo);
 
-  if (!BINFO_MARKED (binfo) == VTT_MARKED_BINFO_P ((tree) data))
-    return NULL_TREE;
-  return binfo;
-}
-
-/* Called from build_vtt_inits via dfs_walk. After building constructor
-   vtables and generating the sub-vtt from them, we need to restore the
-   BINFO_VTABLES that were scribbled on.  DATA is a TREE_LIST whose
-   TREE_VALUE is the TREE_TYPE of the base whose sub vtt was generated.  */
-
-static tree
-dfs_fixup_binfo_vtbls (binfo, data)
-     tree binfo;
-     void *data;
-{
-  CLEAR_BINFO_MARKED (binfo);
+  if (!TYPE_CONTAINS_VPTR_P (BINFO_TYPE (binfo)))
+    /* If this class has no vtable, none of its bases do.  */
+    return dfs_skip_bases;
 
-  /* We don't care about bases that don't have vtables.  */
-  if (!TYPE_VFIELD (BINFO_TYPE (binfo)))
+  if (!vtable)
+    /* This might be a primary base, so have no vtable in this
+       hierarchy.  */
     return NULL_TREE;
 
   /* If we scribbled the construction vtable vptr into BINFO, clear it
      out now.  */
-  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));
+  if (TREE_CODE (vtable) == TREE_LIST
+      && (TREE_PURPOSE (vtable) == (tree) data))
+    BINFO_VTABLE (binfo) = TREE_CHAIN (vtable);
 
   return NULL_TREE;
 }
@@ -7204,9 +7200,7 @@ dfs_fixup_binfo_vtbls (binfo, data)
    hierarchy dominated by T.  */
 
 static void
-build_ctor_vtbl_group (binfo, t)
-     tree binfo;
-     tree t;
+build_ctor_vtbl_group (tree binfo, tree t)
 {
   tree list;
   tree type;
@@ -7220,41 +7214,43 @@ build_ctor_vtbl_group (binfo, t)
   if (IDENTIFIER_GLOBAL_VALUE (id))
     return;
 
-  my_friendly_assert (!same_type_p (BINFO_TYPE (binfo), t), 20010124);
+  gcc_assert (!SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t));
   /* Build a version of VTBL (with the wrong type) for use in
      constructing the addresses of secondary vtables in the
      construction vtable group.  */
   vtbl = build_vtable (t, id, ptr_type_node);
+  DECL_CONSTRUCTION_VTABLE_P (vtbl) = 1;
   list = build_tree_list (vtbl, NULL_TREE);
   accumulate_vtbl_inits (binfo, TYPE_BINFO (TREE_TYPE (binfo)),
                         binfo, t, list);
 
   /* Add the vtables for each of our virtual bases using the vbase in T
      binfo.  */
-  for (vbase = TYPE_BINFO (BINFO_TYPE (binfo)); 
-       vbase; 
+  for (vbase = TYPE_BINFO (BINFO_TYPE (binfo));
+       vbase;
        vbase = TREE_CHAIN (vbase))
     {
       tree b;
-      tree orig_base;
 
-      if (!TREE_VIA_VIRTUAL (vbase))
+      if (!BINFO_VIRTUAL_P (vbase))
        continue;
-      b = binfo_for_vbase (BINFO_TYPE (vbase), t);
-      orig_base = binfo_for_vbase (BINFO_TYPE (vbase), BINFO_TYPE (binfo));
-      
-      accumulate_vtbl_inits (b, orig_base, binfo, t, list);
+      b = copied_binfo (vbase, binfo);
+
+      accumulate_vtbl_inits (b, vbase, binfo, t, list);
     }
   inits = TREE_VALUE (list);
 
   /* Figure out the type of the construction vtable.  */
   type = build_index_type (size_int (list_length (inits) - 1));
   type = build_cplus_array_type (vtable_entry_type, type);
+  layout_type (type);
   TREE_TYPE (vtbl) = type;
+  DECL_SIZE (vtbl) = DECL_SIZE_UNIT (vtbl) = NULL_TREE;
+  layout_decl (vtbl, 0);
 
   /* Initialize the construction vtable.  */
-  pushdecl_top_level (vtbl);
-  initialize_array (vtbl, inits);
+  CLASSTYPE_VTABLES (t) = chainon (CLASSTYPE_VTABLES (t), vtbl);
+  initialize_artificial_var (vtbl, inits);
   dump_vtable (t, binfo, vtbl);
 }
 
@@ -7269,51 +7265,47 @@ build_ctor_vtbl_group (binfo, t)
    but are not necessarily the same in terms of layout.  */
 
 static void
-accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
-     tree binfo;
-     tree orig_binfo;
-     tree rtti_binfo;
-     tree t;
-     tree inits;
+accumulate_vtbl_inits (tree binfo,
+                      tree orig_binfo,
+                      tree rtti_binfo,
+                      tree t,
+                      tree inits)
 {
   int i;
-  int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
+  tree base_binfo;
+  int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
 
-  my_friendly_assert (same_type_p (BINFO_TYPE (binfo),
-                                  BINFO_TYPE (orig_binfo)),
-                     20000517);
+  gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (orig_binfo)));
 
-  /* If it doesn't have a vptr, we don't do anything. */
+  /* 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 
-      && !TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo))
+  if (ctor_vtbl_p
+      && !CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo))
       && !binfo_via_virtual (orig_binfo, BINFO_TYPE (rtti_binfo)))
     return;
 
   /* Build the initializers for the BINFO-in-T vtable.  */
-  TREE_VALUE (inits) 
+  TREE_VALUE (inits)
     = chainon (TREE_VALUE (inits),
               dfs_accumulate_vtbl_inits (binfo, orig_binfo,
                                          rtti_binfo, t, inits));
-                     
+
   /* Walk the BINFO and its bases.  We walk in preorder so that as we
      initialize each vtable we can figure out at what offset the
      secondary vtable lies from the primary vtable.  We can't use
      dfs_walk here because we need to iterate through bases of BINFO
      and RTTI_BINFO simultaneously.  */
-  for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i)
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
     {
-      tree base_binfo = BINFO_BASETYPE (binfo, i);
-      
       /* Skip virtual bases.  */
-      if (TREE_VIA_VIRTUAL (base_binfo))
+      if (BINFO_VIRTUAL_P (base_binfo))
        continue;
       accumulate_vtbl_inits (base_binfo,
-                            BINFO_BASETYPE (orig_binfo, i),
+                            BINFO_BASE_BINFO (orig_binfo, i),
                             rtti_binfo, t,
                             inits);
     }
@@ -7323,19 +7315,18 @@ accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, inits)
    the BINFO vtable.  */
 
 static tree
-dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
-     tree binfo;
-     tree orig_binfo;
-     tree rtti_binfo;
-     tree t;
-     tree l;
+dfs_accumulate_vtbl_inits (tree binfo,
+                          tree orig_binfo,
+                          tree rtti_binfo,
+                          tree t,
+                          tree l)
 {
   tree inits = NULL_TREE;
   tree vtbl = NULL_TREE;
-  int ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
+  int ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
 
   if (ctor_vtbl_p
-      && TREE_VIA_VIRTUAL (orig_binfo) && BINFO_PRIMARY_P (orig_binfo))
+      && BINFO_VIRTUAL_P (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
@@ -7344,39 +7335,40 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
         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 b;
       tree last = NULL_TREE;
 
       /* First, look through the bases we are primary to for RTTI_BINFO
         or a virtual base.  */
-      for (; b; b = BINFO_PRIMARY_BASE_OF (b))
+      b = binfo;
+      while (BINFO_PRIMARY_P (b))
        {
+         b = BINFO_INHERITANCE_CHAIN (b);
          last = b;
-         if (TREE_VIA_VIRTUAL (b) || b == rtti_binfo)
-           break;
+         if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
+           goto found;
        }
       /* 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;
+      for (b = last; b; b = BINFO_INHERITANCE_CHAIN (b))
+       if (BINFO_VIRTUAL_P (b) || b == rtti_binfo)
+         break;
+    found:
 
       /* 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))))
+         || (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.  */
@@ -7384,7 +7376,7 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
 
       /* Otherwise, this is case 3 and we get our own.  */
     }
-  else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo, BINFO_TYPE (rtti_binfo)))
+  else if (!BINFO_NEW_VTABLE_MARKED (orig_binfo))
     return inits;
 
   if (!vtbl)
@@ -7398,18 +7390,14 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
 
       /* Figure out the position to which the VPTR should point.  */
       vtbl = TREE_PURPOSE (l);
-      vtbl = build1 (ADDR_EXPR, 
-                    vtbl_ptr_type_node,
-                    vtbl);
-      TREE_CONSTANT (vtbl) = 1;
+      vtbl = build1 (ADDR_EXPR, vtbl_ptr_type_node, vtbl);
       index = size_binop (PLUS_EXPR,
                          size_int (non_fn_entries),
                          size_int (list_length (TREE_VALUE (l))));
       index = size_binop (MULT_EXPR,
                          TYPE_SIZE_UNIT (vtable_entry_type),
                          index);
-      vtbl = build (PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
-      TREE_CONSTANT (vtbl) = 1;
+      vtbl = build2 (POINTER_PLUS_EXPR, TREE_TYPE (vtbl), vtbl, index);
     }
 
   if (ctor_vtbl_p)
@@ -7417,7 +7405,7 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
        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))
+  else if (BINFO_PRIMARY_P (binfo) && BINFO_VIRTUAL_P (binfo))
     inits = NULL_TREE;
   else
      /* For an ordinary vtable, set BINFO_VTABLE.  */
@@ -7426,6 +7414,8 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
   return inits;
 }
 
+static GTY(()) tree abort_fndecl_addr;
+
 /* 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
@@ -7440,7 +7430,7 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
    The value returned is a TREE_LIST suitable for wrapping in a
    CONSTRUCTOR to use as the DECL_INITIAL for a vtable.  If
    NON_FN_ENTRIES_P is not NULL, *NON_FN_ENTRIES_P is set to the
-   number of non-function entries in the vtable.  
+   number of non-function entries in the vtable.
 
    It might seem that this function should never be called with a
    BINFO for which BINFO_PRIMARY_P holds, the vtable for such a
@@ -7450,17 +7440,18 @@ dfs_accumulate_vtbl_inits (binfo, orig_binfo, rtti_binfo, t, l)
    constructed.  */
 
 static tree
-build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
-     tree binfo;
-     tree orig_binfo;
-     tree t;
-     tree rtti_binfo;
-     int *non_fn_entries_p;
+build_vtbl_initializer (tree binfo,
+                       tree orig_binfo,
+                       tree t,
+                       tree rtti_binfo,
+                       int* non_fn_entries_p)
 {
   tree v, b;
   tree vfun_inits;
-  tree vbase;
   vtbl_init_data vid;
+  unsigned ix;
+  tree vbinfo;
+  VEC(tree,gc) *vbases;
 
   /* Initialize VID.  */
   memset (&vid, 0, sizeof (vid));
@@ -7468,10 +7459,11 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
   vid.derived = t;
   vid.rtti_binfo = rtti_binfo;
   vid.last_init = &vid.inits;
-  vid.primary_vtbl_p = (binfo == TYPE_BINFO (t));
-  vid.ctor_vtbl_p = !same_type_p (BINFO_TYPE (rtti_binfo), t);
+  vid.primary_vtbl_p = SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), t);
+  vid.ctor_vtbl_p = !SAME_BINFO_TYPE_P (BINFO_TYPE (rtti_binfo), t);
+  vid.generate_vcall_entries = true;
   /* 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);
@@ -7479,17 +7471,34 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
   /* Create an array for keeping track of the functions we've
      processed.  When we see multiple functions with the same
      signature, we share the vcall offsets.  */
-  VARRAY_TREE_INIT (vid.fns, 32, "fns");
+  vid.fns = VEC_alloc (tree, gc, 32);
   /* 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); 
-       vbase; 
-       vbase = TREE_CHAIN (vbase))
-    CLEAR_BINFO_VTABLE_PATH_MARKED (TREE_VALUE (vbase));
+  for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
+       VEC_iterate (tree, vbases, ix, vbinfo); ix++)
+    BINFO_VTABLE_PATH_MARKED (vbinfo) = 0;
+
+  /* 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,
+                            build1 (NOP_EXPR, vtable_entry_type,
+                                    null_pointer_node),
+                            add);
+         *prev = add;
+       }
+    }
 
   if (non_fn_entries_p)
     *non_fn_entries_p = list_length (vid.inits);
@@ -7501,11 +7510,22 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
     {
       tree delta;
       tree vcall_index;
-      tree fn;
-      tree pfn;
+      tree fn, fn_original;
       tree init = NULL_TREE;
-      
+
       fn = BV_FN (v);
+      fn_original = fn;
+      if (DECL_THUNK_P (fn))
+       {
+         if (!DECL_NAME (fn))
+           finish_thunk (fn);
+         if (THUNK_ALIAS (fn))
+           {
+             fn = THUNK_ALIAS (fn);
+             BV_FN (v) = fn;
+           }
+         fn_original = THUNK_TARGET (fn);
+       }
 
       /* If the only definition of this function signature along our
         primary base chain is from a lost primary, this vtable slot will
@@ -7514,13 +7534,13 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
 
         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
+        zero out unused slots in ctor vtables, rather than filling them
         with erroneous values (though harmless, apart from relocation
         costs).  */
       for (b = binfo; ; b = get_primary_binfo (b))
        {
          /* We found a defn before a lost primary; go ahead as normal.  */
-         if (look_for_overrides_here (BINFO_TYPE (b), fn))
+         if (look_for_overrides_here (BINFO_TYPE (b), fn_original))
            break;
 
          /* The nearest definition is from a lost primary; clear the
@@ -7537,31 +7557,32 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
          /* Pull the offset for `this', and the function to call, out of
             the list.  */
          delta = BV_DELTA (v);
+         vcall_index = BV_VCALL_INDEX (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);
+         gcc_assert (TREE_CODE (delta) == INTEGER_CST);
+         gcc_assert (TREE_CODE (fn) == FUNCTION_DECL);
 
          /* 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);
+         if (DECL_PURE_VIRTUAL_P (fn_original))
+           {
+             fn = abort_fndecl;
+             if (abort_fndecl_addr == NULL)
+               abort_fndecl_addr = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
+             init = abort_fndecl_addr;
+           }
+         else
+           {
+             if (!integer_zerop (delta) || vcall_index)
+               {
+                 fn = make_thunk (fn, /*this_adjusting=*/1, delta, vcall_index);
+                 if (!DECL_NAME (fn))
+                   finish_thunk (fn);
+               }
+             /* Take the address of the function, considering it to be of an
+                appropriate generic type.  */
+             init = build1 (ADDR_EXPR, vfunc_ptr_type_node, fn);
+           }
        }
 
       /* And add it to the chain of initializers.  */
@@ -7574,22 +7595,22 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
          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 fdesc = build2 (FDESC_EXPR, vfunc_ptr_type_node,
+                                    TREE_OPERAND (init, 0),
+                                    build_int_cst (NULL_TREE, i));
                TREE_CONSTANT (fdesc) = 1;
 
                vfun_inits = tree_cons (NULL_TREE, fdesc, vfun_inits);
              }
        }
       else
-        vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
+       vfun_inits = tree_cons (NULL_TREE, init, vfun_inits);
     }
 
   /* The initializers for virtual functions were built up in reverse
      order; straighten them out now.  */
   vfun_inits = nreverse (vfun_inits);
-  
+
   /* The negative offset initializers are also in reverse order.  */
   vid.inits = nreverse (vid.inits);
 
@@ -7601,9 +7622,7 @@ build_vtbl_initializer (binfo, orig_binfo, t, rtti_binfo, non_fn_entries_p)
    offsets in BINFO, which is in the hierarchy dominated by T.  */
 
 static void
-build_vcall_and_vbase_vtbl_entries (binfo, vid)
-     tree binfo;
-     vtbl_init_data *vid;
+build_vcall_and_vbase_vtbl_entries (tree binfo, vtbl_init_data* vid)
 {
   tree b;
 
@@ -7625,9 +7644,7 @@ build_vcall_and_vbase_vtbl_entries (binfo, vid)
    where the next vbase offset will go.  */
 
 static void
-build_vbase_offset_vtbl_entries (binfo, vid)
-     tree binfo;
-     vtbl_init_data *vid;
+build_vbase_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
 {
   tree vbase;
   tree t;
@@ -7635,11 +7652,11 @@ build_vbase_offset_vtbl_entries (binfo, vid)
 
   /* If there are no virtual baseclasses, then there is nothing to
      do.  */
-  if (!TYPE_USES_VIRTUAL_BASECLASSES (BINFO_TYPE (binfo)))
+  if (!CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)))
     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.  */
@@ -7652,7 +7669,7 @@ build_vbase_offset_vtbl_entries (binfo, vid)
         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 (BINFO_VIRTUAL_P (non_primary_binfo))
        {
          non_primary_binfo = vid->binfo;
          break;
@@ -7671,22 +7688,22 @@ build_vbase_offset_vtbl_entries (binfo, vid)
     {
       tree b;
       tree delta;
-      
-      if (!TREE_VIA_VIRTUAL (vbase))
+
+      if (!BINFO_VIRTUAL_P (vbase))
        continue;
 
       /* Find the instance of this virtual base in the complete
         object.  */
-      b = binfo_for_vbase (BINFO_TYPE (vbase), t);
+      b = copied_binfo (vbase, binfo);
 
       /* If we've already got an offset for this virtual base, we
         don't need another one.  */
       if (BINFO_VTABLE_PATH_MARKED (b))
        continue;
-      SET_BINFO_VTABLE_PATH_MARKED (b);
+      BINFO_VTABLE_PATH_MARKED (b) = 1;
 
       /* Figure out where we can find this vbase offset.  */
-      delta = size_binop (MULT_EXPR, 
+      delta = size_binop (MULT_EXPR,
                          vid->index,
                          convert (ssizetype,
                                   TYPE_SIZE_UNIT (vtable_entry_type)));
@@ -7694,33 +7711,24 @@ build_vbase_offset_vtbl_entries (binfo, vid)
        BINFO_VPTR_FIELD (b) = delta;
 
       if (binfo != TYPE_BINFO (t))
-       {
-         tree orig_vbase;
-
-         /* Find the instance of this virtual base in the type of BINFO.  */
-         orig_vbase = binfo_for_vbase (BINFO_TYPE (vbase),
-                                       BINFO_TYPE (binfo));
-
-         /* The vbase offset had better be the same.  */
-         if (!tree_int_cst_equal (delta,
-                                  BINFO_VPTR_FIELD (orig_vbase)))
-           abort ();
-       }
+       /* The vbase offset had better be the same.  */
+       gcc_assert (tree_int_cst_equal (delta, BINFO_VPTR_FIELD (vbase)));
 
       /* 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 (non_primary_binfo));
-      
-      *vid->last_init 
+
+      *vid->last_init
        = build_tree_list (NULL_TREE,
-                          fold (build1 (NOP_EXPR, 
-                                        vtable_entry_type,
-                                        delta)));
+                          fold_build1 (NOP_EXPR,
+                                       vtable_entry_type,
+                                       delta));
       vid->last_init = &TREE_CHAIN (*vid->last_init);
     }
 }
@@ -7730,54 +7738,64 @@ build_vbase_offset_vtbl_entries (binfo, vid)
    to VID->INITS.  */
 
 static void
-build_vcall_offset_vtbl_entries (binfo, vid)
-     tree binfo;
-     vtbl_init_data *vid;
-{
-  /* We only need these entries if this base is a virtual base.  */
-  if (!TREE_VIA_VIRTUAL (binfo))
-    return;
-
-  /* We need a vcall offset for each of the virtual functions in this
-     vtable.  For example:
-
-       class A { virtual void f (); };
-       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' secondary vtables.  */
-  vid->vbase = binfo;
-  /* Now, walk through the non-virtual bases, adding vcall offsets.  */
-  add_vcall_offset_vtbl_entries_r (binfo, vid);
+build_vcall_offset_vtbl_entries (tree binfo, vtbl_init_data* vid)
+{
+  /* We only need these entries if this base is a virtual base.  We
+     compute the indices -- but do not add to the vtable -- when
+     building the main vtable for a class.  */
+  if (binfo == TYPE_BINFO (vid->derived)
+      || (BINFO_VIRTUAL_P (binfo) 
+         /* If BINFO is RTTI_BINFO, then (since BINFO does not
+            correspond to VID->DERIVED), we are building a primary
+            construction virtual table.  Since this is a primary
+            virtual table, we do not need the vcall offsets for
+            BINFO.  */
+         && binfo != vid->rtti_binfo))
+    {
+      /* We need a vcall offset for each of the virtual functions in this
+        vtable.  For example:
+
+          class A { virtual void f (); };
+          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' secondary vtables.  */
+      vid->vbase = binfo;
+      /* If we are just computing the vcall indices -- but do not need
+        the actual entries -- not that.  */
+      if (!BINFO_VIRTUAL_P (binfo))
+       vid->generate_vcall_entries = false;
+      /* Now, walk through the non-virtual bases, adding vcall offsets.  */
+      add_vcall_offset_vtbl_entries_r (binfo, vid);
+    }
 }
 
 /* Build vcall offsets, starting with those for BINFO.  */
 
 static void
-add_vcall_offset_vtbl_entries_r (binfo, vid)
-     tree binfo;
-     vtbl_init_data *vid;
+add_vcall_offset_vtbl_entries_r (tree binfo, vtbl_init_data* vid)
 {
   int i;
   tree primary_binfo;
+  tree base_binfo;
 
   /* Don't walk into virtual bases -- except, of course, for the
      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)
+  if (BINFO_VIRTUAL_P (binfo) && vid->vbase != binfo)
     return;
-  
+
   /* If BINFO has a primary base, process it first.  */
   primary_binfo = get_primary_binfo (binfo);
   if (primary_binfo)
@@ -7787,164 +7805,174 @@ add_vcall_offset_vtbl_entries_r (binfo, vid)
   add_vcall_offset_vtbl_entries_1 (binfo, vid);
 
   /* Scan the non-primary bases of BINFO.  */
-  for (i = 0; i < BINFO_N_BASETYPES (binfo); ++i) 
-    {
-      tree base_binfo;
-      
-      base_binfo = BINFO_BASETYPE (binfo, i);
-      if (base_binfo != primary_binfo)
-       add_vcall_offset_vtbl_entries_r (base_binfo, vid);
-    }
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); ++i)
+    if (base_binfo != primary_binfo)
+      add_vcall_offset_vtbl_entries_r (base_binfo, vid);
 }
 
 /* Called from build_vcall_offset_vtbl_entries_r.  */
 
 static void
-add_vcall_offset_vtbl_entries_1 (binfo, vid)
-     tree binfo;
-     vtbl_init_data* vid;
-{
-  tree derived_virtuals;
-  tree base_virtuals;
-  tree orig_virtuals;
-  tree binfo_inits;
-  /* 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 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))
+add_vcall_offset_vtbl_entries_1 (tree binfo, vtbl_init_data* vid)
+{
+  /* Make entries for the rest of the virtuals.  */
+  if (abi_version_at_least (2))
     {
-      tree b;
+      tree orig_fn;
 
-      /* 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))
+      /* The ABI requires that the methods be processed in declaration
+        order.  G++ 3.2 used the order in the vtable.  */
+      for (orig_fn = TYPE_METHODS (BINFO_TYPE (binfo));
+          orig_fn;
+          orig_fn = TREE_CHAIN (orig_fn))
+       if (DECL_VINDEX (orig_fn))
+         add_vcall_offset (orig_fn, binfo, vid);
+    }
+  else
+    {
+      tree derived_virtuals;
+      tree base_virtuals;
+      tree orig_virtuals;
+      /* If BINFO is a primary base, the most derived class which has
+        BINFO as a primary base; otherwise, just BINFO.  */
+      tree non_primary_binfo;
+
+      /* 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))
        {
-         if (non_primary_binfo != vid->vbase)
-           abort ();
-         non_primary_binfo = vid->binfo;
-         break;
+         tree b;
+
+         /* 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 (BINFO_VIRTUAL_P (non_primary_binfo))
+           {
+             gcc_assert (non_primary_binfo == vid->vbase);
+             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;
        }
 
-      b = BINFO_INHERITANCE_CHAIN (non_primary_binfo);
-      if (get_primary_binfo (b) != non_primary_binfo)
-       break;
-      non_primary_binfo = b;
+      if (vid->ctor_vtbl_p)
+       /* For a ctor vtable we need the equivalent binfo within the hierarchy
+          where rtti_binfo is the most derived type.  */
+       non_primary_binfo
+         = original_binfo (non_primary_binfo, vid->rtti_binfo);
+
+      for (base_virtuals = BINFO_VIRTUALS (binfo),
+            derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
+            orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
+          base_virtuals;
+          base_virtuals = TREE_CHAIN (base_virtuals),
+            derived_virtuals = TREE_CHAIN (derived_virtuals),
+            orig_virtuals = TREE_CHAIN (orig_virtuals))
+       {
+         tree orig_fn;
+
+         /* Find the declaration that originally caused this function to
+            be present in BINFO_TYPE (binfo).  */
+         orig_fn = BV_FN (orig_virtuals);
+
+         /* 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_BINFO_TYPE_P (BINFO_TYPE (binfo), DECL_CONTEXT (orig_fn)))
+           continue;
+
+         add_vcall_offset (orig_fn, binfo, vid);
+       }
     }
+}
 
-  if (vid->ctor_vtbl_p)
-    /* For a ctor vtable we need the equivalent binfo within the hierarchy
-       where rtti_binfo is the most derived type.  */
-    non_primary_binfo = get_original_base
-          (non_primary_binfo, TYPE_BINFO (BINFO_TYPE (vid->rtti_binfo)));
+/* Add a vcall offset entry for ORIG_FN to the vtable.  */
 
-  /* Make entries for the rest of the virtuals.  */
-  for (base_virtuals = BINFO_VIRTUALS (binfo),
-        derived_virtuals = BINFO_VIRTUALS (non_primary_binfo),
-        orig_virtuals = BINFO_VIRTUALS (TYPE_BINFO (BINFO_TYPE (binfo)));
-       base_virtuals;
-       base_virtuals = TREE_CHAIN (base_virtuals),
-        derived_virtuals = TREE_CHAIN (derived_virtuals),
-        orig_virtuals = TREE_CHAIN (orig_virtuals))
+static void
+add_vcall_offset (tree orig_fn, tree binfo, vtbl_init_data *vid)
+{
+  size_t i;
+  tree vcall_offset;
+  tree derived_entry;
+
+  /* If there is already an entry for a function with the same
+     signature as FN, then we do not need a second vcall offset.
+     Check the list of functions already present in the derived
+     class vtable.  */
+  for (i = 0; VEC_iterate (tree, vid->fns, i, derived_entry); ++i)
     {
-      tree orig_fn;
-      tree fn;
-      tree base;
-      tree base_binfo;
-      size_t i;
-      tree vcall_offset;
+      if (same_signature_p (derived_entry, orig_fn)
+         /* We only use one vcall offset for virtual destructors,
+            even though there are two virtual table entries.  */
+         || (DECL_DESTRUCTOR_P (derived_entry)
+             && DECL_DESTRUCTOR_P (orig_fn)))
+       return;
+    }
+
+  /* If we are building these vcall offsets as part of building
+     the vtable for the most derived class, remember the vcall
+     offset.  */
+  if (vid->binfo == TYPE_BINFO (vid->derived))
+    {
+      tree_pair_p elt = VEC_safe_push (tree_pair_s, gc,
+                                      CLASSTYPE_VCALL_INDICES (vid->derived),
+                                      NULL);
+      elt->purpose = orig_fn;
+      elt->value = vid->index;
+    }
 
-      /* Find the declaration that originally caused this function to
-        be present in BINFO_TYPE (binfo).  */
-      orig_fn = BV_FN (orig_virtuals);
+  /* The next vcall offset will be found at a more negative
+     offset.  */
+  vid->index = size_binop (MINUS_EXPR, vid->index,
+                          ssize_int (TARGET_VTABLE_DATA_ENTRY_DISTANCE));
 
-      /* 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;
+  /* Keep track of this function.  */
+  VEC_safe_push (tree, gc, vid->fns, orig_fn);
 
-      /* Find the overriding function.  */
-      fn = BV_FN (derived_virtuals);
+  if (vid->generate_vcall_entries)
+    {
+      tree base;
+      tree fn;
 
-      /* If there is already an entry for a function with the same
-        signature as FN, then we do not need a second vcall offset.
-        Check the list of functions already present in the derived
-        class vtable.  */
-      for (i = 0; i < VARRAY_ACTIVE_SIZE (vid->fns); ++i) 
+      /* Find the overriding function.  */
+      fn = find_final_overrider (vid->rtti_binfo, binfo, orig_fn);
+      if (fn == error_mark_node)
+       vcall_offset = build1 (NOP_EXPR, vtable_entry_type,
+                              integer_zero_node);
+      else
        {
-         tree derived_entry;
-
-         derived_entry = VARRAY_TREE (vid->fns, i);
-         if (same_signature_p (BV_FN (derived_entry), fn)
-             /* We only use one vcall offset for virtual destructors,
-                even though there are two virtual table entries.  */
-             || (DECL_DESTRUCTOR_P (BV_FN (derived_entry))
-                 && DECL_DESTRUCTOR_P (fn)))
-           {
-             if (!vid->ctor_vtbl_p)
-               BV_VCALL_INDEX (derived_virtuals) 
-                 = BV_VCALL_INDEX (derived_entry);
-             break;
-           }
+         base = TREE_VALUE (fn);
+
+         /* 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 = size_diffop (BINFO_OFFSET (base),
+                                     BINFO_OFFSET (vid->binfo));
+         vcall_offset = fold_build1 (NOP_EXPR, vtable_entry_type,
+                                     vcall_offset);
        }
-      if (i != VARRAY_ACTIVE_SIZE (vid->fns))
-       continue;
-
-      /* 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 = lookup_base (vid->derived, base, ba_any, NULL);
-
-      /* Compute the vcall offset.  */
-      /* 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,
-                                  vcall_offset));
-      
+      /* Add the initializer to the vtable.  */
       *vid->last_init = build_tree_list (NULL_TREE, vcall_offset);
       vid->last_init = &TREE_CHAIN (*vid->last_init);
-
-      /* Keep track of the vtable index where this vcall offset can be
-        found.  For a construction vtable, we already made this
-        annotation when we built the original vtable.  */
-      if (!vid->ctor_vtbl_p)
-       BV_VCALL_INDEX (derived_virtuals) = vid->index;
-
-      /* The next vcall offset will be found at a more negative
-        offset.  */
-      vid->index = size_binop (MINUS_EXPR, vid->index, ssize_int (1));
-
-      /* Keep track of this function.  */
-      VARRAY_PUSH_TREE (vid->fns, derived_virtuals);
     }
 }
 
-/* Return vtbl initializers for the RTTI entries coresponding to the
+/* Return vtbl initializers for the RTTI entries corresponding to the
    BINFO's vtable.  The RTTI entries should indicate the object given
    by VID->rtti_binfo.  */
 
 static void
-build_rtti_vtbl_entries (binfo, vid)
-     tree binfo;
-     vtbl_init_data *vid;
+build_rtti_vtbl_entries (tree binfo, vtbl_init_data* vid)
 {
   tree b;
   tree t;
@@ -7960,64 +7988,65 @@ build_rtti_vtbl_entries (binfo, vid)
      primary base, and then add the offset in the vtbl to that value.  */
   b = binfo;
   while (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (b))
-         && !BINFO_LOST_PRIMARY_P (b))
+        && !BINFO_LOST_PRIMARY_P (b))
     {
       tree primary_base;
 
       primary_base = get_primary_binfo (b);
-      my_friendly_assert (BINFO_PRIMARY_BASE_OF (primary_base) == b, 20010127);
+      gcc_assert (BINFO_PRIMARY_P (primary_base)
+                 && BINFO_INHERITANCE_CHAIN (primary_base) == b);
       b = primary_base;
     }
   offset = size_diffop (BINFO_OFFSET (vid->rtti_binfo), BINFO_OFFSET (b));
 
   /* The second entry is the address of the typeinfo object.  */
   if (flag_rtti)
-    decl = build_unary_op (ADDR_EXPR, get_tinfo_decl (t), 0);
+    decl = build_address (get_tinfo_decl (t));
   else
     decl = integer_zero_node;
-  
+
   /* Convert the declaration to a type that can be stored in the
      vtable.  */
-  init = build1 (NOP_EXPR, vfunc_ptr_type_node, decl);
-  TREE_CONSTANT (init) = 1;
+  init = build_nop (vfunc_ptr_type_node, decl);
   *vid->last_init = build_tree_list (NULL_TREE, init);
   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.  Convert the offset to look like a
+  /* Add the offset-to-top entry.  It comes earlier in the vtable than
+     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;
+  init = build_nop (vfunc_ptr_type_node, offset);
   *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
-   for the `this' pointer.  VCALL_INDEX is the vtable index containing
-   the vcall offset; NULL_TREE if none.  ENTRY is the virtual function
-   table entry itself.  It's TREE_TYPE must be VFUNC_PTR_TYPE_NODE,
-   but it may not actually be a virtual function table pointer.  (For
-   example, it might be the address of the RTTI object, under the new
-   ABI.)  */
+/* Fold a OBJ_TYPE_REF expression to the address of a function.
+   KNOWN_TYPE carries the true type of OBJ_TYPE_REF_OBJECT(REF).  */
 
-static tree
-build_vtable_entry (delta, vcall_index, entry)
-     tree delta;
-     tree vcall_index;
-     tree entry;
+tree
+cp_fold_obj_type_ref (tree ref, tree known_type)
 {
-  tree fn = TREE_OPERAND (entry, 0);
-  
-  if ((!integer_zerop (delta) || vcall_index != NULL_TREE)
-      && fn != abort_fndecl)
+  HOST_WIDE_INT index = tree_low_cst (OBJ_TYPE_REF_TOKEN (ref), 1);
+  HOST_WIDE_INT i = 0;
+  tree v = BINFO_VIRTUALS (TYPE_BINFO (known_type));
+  tree fndecl;
+
+  while (i != index)
     {
-      entry = make_thunk (entry, delta, vcall_index);
-      entry = build1 (ADDR_EXPR, vtable_entry_type, entry);
-      TREE_READONLY (entry) = 1;
-      TREE_CONSTANT (entry) = 1;
+      i += (TARGET_VTABLE_USES_DESCRIPTORS
+           ? TARGET_VTABLE_USES_DESCRIPTORS : 1);
+      v = TREE_CHAIN (v);
     }
-#ifdef GATHER_STATISTICS
-  n_vtable_entries += 1;
+
+  fndecl = BV_FN (v);
+
+#ifdef ENABLE_CHECKING
+  gcc_assert (tree_int_cst_equal (OBJ_TYPE_REF_TOKEN (ref),
+                                 DECL_VINDEX (fndecl)));
 #endif
-  return entry;
+
+  cgraph_node (fndecl)->local.vtable_method = true;
+
+  return build_address (fndecl);
 }
+
+#include "gt-cp-class.h"