OSDN Git Service

When a class template is explicitly instantiated, its member should be too.
[pf3gnuchains/gcc-fork.git] / gcc / cp / search.c
index 263cda5..5f4c91c 100644 (file)
@@ -1,23 +1,23 @@
 /* Breadth-first and depth-first routines for
    searching multiple-inheritance lattice for GNU C++.
    Copyright (C) 1987, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
-   1999, 2000, 2002 Free Software Foundation, Inc.
+   1999, 2000, 2002, 2003, 2004 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)
 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
+along with GCC; see the file COPYING.  If not, write to
 the Free Software Foundation, 59 Temple Place - Suite 330,
 Boston, MA 02111-1307, USA.  */
 
@@ -33,7 +33,6 @@ Boston, MA 02111-1307, USA.  */
 #include "flags.h"
 #include "rtl.h"
 #include "output.h"
-#include "ggc.h"
 #include "toplev.h"
 #include "stack.h"
 
@@ -44,10 +43,8 @@ static struct obstack search_obstack;
 /* Methods for pushing and popping objects to and from obstacks.  */
 
 struct stack_level *
-push_stack_level (obstack, tp, size)
-     struct obstack *obstack;
-     char *tp;  /* Sony NewsOS 5.0 compiler doesn't like void * here.  */
-     int size;
+push_stack_level (struct obstack *obstack, char *tp,/* Sony NewsOS 5.0 compiler doesn't like void * here.  */
+                 int size)
 {
   struct stack_level *stack;
   obstack_grow (obstack, tp, size);
@@ -60,8 +57,7 @@ push_stack_level (obstack, tp, size)
 }
 
 struct stack_level *
-pop_stack_level (stack)
-     struct stack_level *stack;
+pop_stack_level (struct stack_level *stack)
 {
   struct stack_level *tem = stack;
   struct obstack *obstack = tem->obstack;
@@ -82,59 +78,41 @@ struct vbase_info
   tree inits;
 };
 
-static tree lookup_field_1 PARAMS ((tree, tree));
-static int is_subobject_of_p PARAMS ((tree, tree, tree));
-static int is_subobject_of_p_1 PARAMS ((tree, tree, tree));
-static tree dfs_check_overlap PARAMS ((tree, void *));
-static tree dfs_no_overlap_yet PARAMS ((tree, void *));
-static base_kind lookup_base_r
-       PARAMS ((tree, tree, base_access, int, int, int, tree *));
-static int dynamic_cast_base_recurse PARAMS ((tree, tree, int, tree *));
-static tree marked_pushdecls_p PARAMS ((tree, void *));
-static tree unmarked_pushdecls_p PARAMS ((tree, void *));
-static tree dfs_debug_unmarkedp PARAMS ((tree, void *));
-static tree dfs_debug_mark PARAMS ((tree, void *));
-static tree dfs_get_vbase_types PARAMS ((tree, void *));
-static tree dfs_push_type_decls PARAMS ((tree, void *));
-static tree dfs_push_decls PARAMS ((tree, void *));
-static tree dfs_unuse_fields PARAMS ((tree, void *));
-static tree add_conversions PARAMS ((tree, void *));
-static int look_for_overrides_r PARAMS ((tree, tree));
-static struct search_level *push_search_level
-       PARAMS ((struct stack_level *, struct obstack *));
-static struct search_level *pop_search_level
-       PARAMS ((struct stack_level *));
-static tree bfs_walk
-       PARAMS ((tree, tree (*) (tree, void *), tree (*) (tree, void *),
-              void *));
-static tree lookup_field_queue_p PARAMS ((tree, void *));
-static int shared_member_p PARAMS ((tree));
-static tree lookup_field_r PARAMS ((tree, void *));
-static tree canonical_binfo PARAMS ((tree));
-static tree shared_marked_p PARAMS ((tree, void *));
-static tree shared_unmarked_p PARAMS ((tree, void *));
-static int  dependent_base_p PARAMS ((tree));
-static tree dfs_accessible_queue_p PARAMS ((tree, void *));
-static tree dfs_accessible_p PARAMS ((tree, void *));
-static tree dfs_access_in_type PARAMS ((tree, void *));
-static access_kind access_in_type PARAMS ((tree, tree));
-static tree dfs_canonical_queue PARAMS ((tree, void *));
-static tree dfs_assert_unmarked_p PARAMS ((tree, void *));
-static void assert_canonical_unmarked PARAMS ((tree));
-static int protected_accessible_p PARAMS ((tree, tree, tree));
-static int friend_accessible_p PARAMS ((tree, tree, tree));
-static void setup_class_bindings PARAMS ((tree, int));
-static int template_self_reference_p PARAMS ((tree, tree));
-static tree dfs_find_vbase_instance PARAMS ((tree, void *));
-static tree dfs_get_pure_virtuals PARAMS ((tree, void *));
-static tree dfs_build_inheritance_graph_order PARAMS ((tree, void *));
+static tree dfs_check_overlap (tree, void *);
+static tree dfs_no_overlap_yet (tree, int, void *);
+static base_kind lookup_base_r (tree, tree, base_access, bool, tree *);
+static int dynamic_cast_base_recurse (tree, tree, bool, tree *);
+static tree marked_pushdecls_p (tree, int, void *);
+static tree unmarked_pushdecls_p (tree, int, void *);
+static tree dfs_debug_unmarkedp (tree, int, void *);
+static tree dfs_debug_mark (tree, void *);
+static tree dfs_push_type_decls (tree, void *);
+static tree dfs_push_decls (tree, void *);
+static tree dfs_unuse_fields (tree, void *);
+static tree add_conversions (tree, void *);
+static int look_for_overrides_r (tree, tree);
+static struct search_level *push_search_level (struct stack_level *,
+                                              struct obstack *);
+static struct search_level *pop_search_level (struct stack_level *);
+static tree bfs_walk (tree, tree (*) (tree, void *),
+                     tree (*) (tree, int, void *), void *);
+static tree lookup_field_queue_p (tree, int, void *);
+static int shared_member_p (tree);
+static tree lookup_field_r (tree, void *);
+static tree dfs_accessible_queue_p (tree, int, void *);
+static tree dfs_accessible_p (tree, void *);
+static tree dfs_access_in_type (tree, void *);
+static access_kind access_in_type (tree, tree);
+static int protected_accessible_p (tree, tree, tree);
+static int friend_accessible_p (tree, tree, tree);
+static void setup_class_bindings (tree, int);
+static int template_self_reference_p (tree, tree);
+static tree dfs_get_pure_virtuals (tree, void *);
 
 /* Allocate a level of searching.  */
 
 static struct search_level *
-push_search_level (stack, obstack)
-     struct stack_level *stack;
-     struct obstack *obstack;
+push_search_level (struct stack_level *stack, struct obstack *obstack)
 {
   struct search_level tem;
 
@@ -145,10 +123,9 @@ push_search_level (stack, obstack)
 /* Discard a level of search allocation.  */
 
 static struct search_level *
-pop_search_level (obstack)
-     struct stack_level *obstack;
+pop_search_level (struct stack_level *obstack)
 {
-  register struct search_level *stack = pop_stack_level (obstack);
+  struct search_level *stack = pop_stack_level (obstack);
 
   return stack;
 }
@@ -166,12 +143,8 @@ static int n_contexts_saved;
 \f
 /* Worker for lookup_base.  BINFO is the binfo we are searching at,
    BASE is the RECORD_TYPE we are searching for.  ACCESS is the
-   required access checks.  WITHIN_CURRENT_SCOPE, IS_NON_PUBLIC and
-   IS_VIRTUAL indicate how BINFO was reached from the start of the
-   search.  WITHIN_CURRENT_SCOPE is true if we met the current scope,
-   or friend thereof (this allows us to determine whether a protected
-   base is accessible or not).  IS_NON_PUBLIC indicates whether BINFO
-   is accessible and IS_VIRTUAL indicates if it is morally virtual.
+   required access checks.  IS_VIRTUAL indicates if BINFO is morally
+   virtual.
 
    If BINFO is of the required type, then *BINFO_PTR is examined to
    compare with any other instance of BASE we might have already
@@ -181,28 +154,14 @@ static int n_contexts_saved;
    Otherwise BINFO's bases are searched.  */
 
 static base_kind
-lookup_base_r (binfo, base, access, within_current_scope,
-              is_non_public, is_virtual, binfo_ptr)
-     tree binfo, base;
-     base_access access;
-     int within_current_scope;
-     int is_non_public;                /* inside a non-public part */
-     int is_virtual;           /* inside a virtual part */
-     tree *binfo_ptr;
+lookup_base_r (tree binfo, tree base, base_access access,
+              bool is_virtual,                 /* inside a virtual part */
+              tree *binfo_ptr)
 {
   int i;
-  tree bases;
+  tree bases, accesses;
   base_kind found = bk_not_base;
   
-  if (access == ba_check
-      && !within_current_scope
-      && is_friend (BINFO_TYPE (binfo), current_scope ()))
-    {
-      /* Do not clear is_non_public here.  If A is a private base of B, A
-        is not allowed to convert a B* to an A*.  */
-      within_current_scope = 1;
-    }
-  
   if (same_type_p (BINFO_TYPE (binfo), base))
     {
       /* We have found a base. Check against what we have found
@@ -210,13 +169,10 @@ lookup_base_r (binfo, base, access, within_current_scope,
       found = bk_same_type;
       if (is_virtual)
        found = bk_via_virtual;
-      if (is_non_public)
-       found = bk_inaccessible;
       
       if (!*binfo_ptr)
        *binfo_ptr = binfo;
-      else if (!is_virtual || !tree_int_cst_equal (BINFO_OFFSET (binfo),
-                                                  BINFO_OFFSET (*binfo_ptr)))
+      else if (binfo != *binfo_ptr)
        {
          if (access != ba_any)
            *binfo_ptr = NULL;
@@ -230,35 +186,18 @@ lookup_base_r (binfo, base, access, within_current_scope,
     }
   
   bases = BINFO_BASETYPES (binfo);
+  accesses = BINFO_BASEACCESSES (binfo);
   if (!bases)
     return bk_not_base;
   
   for (i = TREE_VEC_LENGTH (bases); i--;)
     {
       tree base_binfo = TREE_VEC_ELT (bases, i);
-      int this_non_public = is_non_public;
-      int this_virtual = is_virtual;
       base_kind bk;
 
-      if (access <= ba_ignore)
-       ; /* no change */
-      else if (TREE_VIA_PUBLIC (base_binfo))
-       ; /* no change */
-      else if (access == ba_not_special)
-       this_non_public = 1;
-      else if (TREE_VIA_PROTECTED (base_binfo) && within_current_scope)
-       ; /* no change */
-      else if (is_friend (BINFO_TYPE (binfo), current_scope ()))
-       ; /* no change */
-      else
-       this_non_public = 1;
-      
-      if (TREE_VIA_VIRTUAL (base_binfo))
-       this_virtual = 1;
-      
       bk = lookup_base_r (base_binfo, base,
-                         access, within_current_scope,
-                         this_non_public, this_virtual,
+                         access,
+                         is_virtual || TREE_VIA_VIRTUAL (base_binfo),
                          binfo_ptr);
 
       switch (bk)
@@ -269,17 +208,9 @@ lookup_base_r (binfo, base, access, within_current_scope,
          found = bk;
          break;
          
-       case bk_inaccessible:
-         if (found == bk_not_base)
-           found = bk;
-         my_friendly_assert (found == bk_via_virtual
-                             || found == bk_inaccessible, 20010723);
-         
-         break;
-         
        case bk_same_type:
          bk = bk_proper_base;
-         /* FALLTHROUGH */
+         /* Fall through.  */
        case bk_proper_base:
          my_friendly_assert (found == bk_not_base, 20010723);
          found = bk;
@@ -292,15 +223,45 @@ lookup_base_r (binfo, base, access, within_current_scope,
          
        case bk_not_base:
          break;
+
+       default:
+         abort ();
        }
     }
   return found;
 }
 
+/* Returns true if type BASE is accessible in T.  (BASE is known to be
+   a (possibly non-proper) base class of T.)  */
+
+bool
+accessible_base_p (tree t, tree base)
+{
+  tree decl;
+
+  /* [class.access.base]
+
+     A base class is said to be accessible if an invented public
+     member of the base class is accessible.  
+
+     If BASE is a non-proper base, this condition is trivially
+     true.  */
+  if (same_type_p (t, base))
+    return true;
+  /* Rather than inventing a public member, we use the implicit
+     public typedef created in the scope of every class.  */
+  decl = TYPE_FIELDS (base);
+  while (!DECL_SELF_REFERENCE_P (decl))
+    decl = TREE_CHAIN (decl);
+  while (ANON_AGGR_TYPE_P (t))
+    t = TYPE_CONTEXT (t);
+  return accessible_p (t, decl);
+}
+
 /* Lookup BASE in the hierarchy dominated by T.  Do access checking as
-   ACCESS specifies.  Return the binfo we discover (which might not be
-   canonical).  If KIND_PTR is non-NULL, fill with information about
-   what kind of base we discovered.
+   ACCESS specifies.  Return the binfo we discover.  If KIND_PTR is
+   non-NULL, fill with information about what kind of base we
+   discovered.
 
    If the base is inaccessible, or ambiguous, and the ba_quiet bit is
    not set in ACCESS, then an error is issued and error_mark_node is
@@ -308,10 +269,7 @@ lookup_base_r (binfo, base, access, within_current_scope,
    NULL_TREE is returned.  */
 
 tree
-lookup_base (t, base, access, kind_ptr)
-     tree t, base;
-     base_access access;
-     base_kind *kind_ptr;
+lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
 {
   tree binfo = NULL;           /* The binfo we've found so far.  */
   tree t_binfo = NULL;
@@ -337,33 +295,47 @@ lookup_base (t, base, access, kind_ptr)
   t = complete_type (TYPE_MAIN_VARIANT (t));
   base = complete_type (TYPE_MAIN_VARIANT (base));
   
-  bk = lookup_base_r (t_binfo, base, access & ~ba_quiet,
-                     0, 0, 0, &binfo);
+  bk = lookup_base_r (t_binfo, base, access, 0, &binfo);
+
+  /* Check that the base is unambiguous and accessible.  */
+  if (access != ba_any)
+    switch (bk)
+      {
+      case bk_not_base:
+       break;
+
+      case bk_ambig:
+       binfo = NULL_TREE;
+       if (!(access & ba_quiet))
+         {
+           error ("`%T' is an ambiguous base of `%T'", base, t);
+           binfo = error_mark_node;
+         }
+       break;
+
+      default:
+       if ((access & ~ba_quiet) != ba_ignore
+           /* If BASE is incomplete, then BASE and TYPE are probably
+              the same, in which case BASE is accessible.  If they
+              are not the same, then TYPE is invalid.  In that case,
+              there's no need to issue another error here, and
+              there's no implicit typedef to use in the code that
+              follows, so we skip the check.  */
+           && COMPLETE_TYPE_P (base)
+           && !accessible_base_p (t, base))
+         {
+           if (!(access & ba_quiet))
+             {
+               error ("`%T' is an inaccessible base of `%T'", base, t);
+               binfo = error_mark_node;
+             }
+           else
+             binfo = NULL_TREE;
+           bk = bk_inaccessible;
+         }
+       break;
+      }
 
-  switch (bk)
-    {
-    case bk_inaccessible:
-      binfo = NULL_TREE;
-      if (!(access & ba_quiet))
-       {
-         error ("`%T' is an inaccessible base of `%T'", base, t);
-         binfo = error_mark_node;
-       }
-      break;
-    case bk_ambig:
-      if (access != ba_any)
-       {
-         binfo = NULL_TREE;
-         if (!(access & ba_quiet))
-           {
-             error ("`%T' is an ambiguous base of `%T'", base, t);
-             binfo = error_mark_node;
-           }
-       }
-      break;
-    default:;
-    }
-  
   if (kind_ptr)
     *kind_ptr = bk;
   
@@ -373,19 +345,16 @@ lookup_base (t, base, access, kind_ptr)
 /* Worker function for get_dynamic_cast_base_type.  */
 
 static int
-dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
-     tree subtype;
-     tree binfo;
-     int via_virtual;
-     tree *offset_ptr;
+dynamic_cast_base_recurse (tree subtype, tree binfo, bool is_via_virtual,
+                          tree *offset_ptr)
 {
-  tree binfos;
+  tree binfos, accesses;
   int i, n_baselinks;
   int worst = -2;
   
   if (BINFO_TYPE (binfo) == subtype)
     {
-      if (via_virtual)
+      if (is_via_virtual)
         return -1;
       else
         {
@@ -395,17 +364,19 @@ dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
     }
   
   binfos = BINFO_BASETYPES (binfo);
+  accesses = BINFO_BASEACCESSES (binfo);
   n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
   for (i = 0; i < n_baselinks; i++)
     {
       tree base_binfo = TREE_VEC_ELT (binfos, i);
+      tree base_access = TREE_VEC_ELT (accesses, i);
       int rval;
       
-      if (!TREE_VIA_PUBLIC (base_binfo))
+      if (base_access != access_public_node)
         continue;
       rval = dynamic_cast_base_recurse
              (subtype, base_binfo,
-              via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
+              is_via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
       if (worst == -2)
         worst = rval;
       else if (rval >= 0)
@@ -430,13 +401,11 @@ dynamic_cast_base_recurse (subtype, binfo, via_virtual, offset_ptr)
    BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases.  */
 
 tree
-get_dynamic_cast_base_type (subtype, target)
-     tree subtype;
-     tree target;
+get_dynamic_cast_base_type (tree subtype, tree target)
 {
   tree offset = NULL_TREE;
   int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
-                                        0, &offset);
+                                        false, &offset);
   
   if (!boff)
     return offset;
@@ -445,20 +414,20 @@ get_dynamic_cast_base_type (subtype, target)
   return offset;
 }
 
-/* Search for a member with name NAME in a multiple inheritance lattice
-   specified by TYPE.  If it does not exist, return NULL_TREE.
+/* Search for a member with name NAME in a multiple inheritance
+   lattice specified by TYPE.  If it does not exist, return NULL_TREE.
    If the member is ambiguously referenced, return `error_mark_node'.
-   Otherwise, return the FIELD_DECL.  */
+   Otherwise, return a DECL with the indicated name.  If WANT_TYPE is
+   true, type declarations are preferred.  */
 
 /* Do a 1-level search for NAME as a member of TYPE.  The caller must
    figure out whether it can access this field.  (Since it is only one
    level, this is reasonable.)  */
 
-static tree
-lookup_field_1 (type, name)
-     tree type, name;
+tree
+lookup_field_1 (tree type, tree name, bool want_type)
 {
-  register tree field;
+  tree field;
 
   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
@@ -475,8 +444,8 @@ lookup_field_1 (type, name)
       && DECL_LANG_SPECIFIC (TYPE_NAME (type))
       && DECL_SORTED_FIELDS (TYPE_NAME (type)))
     {
-      tree *fields = &TREE_VEC_ELT (DECL_SORTED_FIELDS (TYPE_NAME (type)), 0);
-      int lo = 0, hi = TREE_VEC_LENGTH (DECL_SORTED_FIELDS (TYPE_NAME (type)));
+      tree *fields = &DECL_SORTED_FIELDS (TYPE_NAME (type))->elts[0];
+      int lo = 0, hi = DECL_SORTED_FIELDS (TYPE_NAME (type))->len;
       int i;
 
       while (lo < hi)
@@ -493,14 +462,28 @@ lookup_field_1 (type, name)
            lo = i + 1;
          else
            {
+             field = NULL_TREE;
+
              /* We might have a nested class and a field with the
                 same name; we sorted them appropriately via
-                field_decl_cmp, so just look for the last field with
-                this name.  */
-             while (i + 1 < hi
-                    && DECL_NAME (fields[i+1]) == name)
-               ++i;
-             return fields[i];
+                field_decl_cmp, so just look for the first or last
+                field with this name.  */
+             if (want_type)
+               {
+                 do
+                   field = fields[i--];
+                 while (i >= lo && DECL_NAME (fields[i]) == name);
+                 if (TREE_CODE (field) != TYPE_DECL
+                     && !DECL_CLASS_TEMPLATE_P (field))
+                   field = NULL_TREE;
+               }
+             else
+               {
+                 do
+                   field = fields[i++];
+                 while (i < hi && DECL_NAME (fields[i]) == name);
+               }
+             return field;
            }
        }
       return NULL_TREE;
@@ -511,7 +494,7 @@ lookup_field_1 (type, name)
 #ifdef GATHER_STATISTICS
   n_calls_lookup_field_1++;
 #endif /* GATHER_STATISTICS */
-  while (field)
+  for (field = TYPE_FIELDS (type); field; field = TREE_CHAIN (field))
     {
 #ifdef GATHER_STATISTICS
       n_fields_searched++;
@@ -520,7 +503,7 @@ lookup_field_1 (type, name)
       if (DECL_NAME (field) == NULL_TREE
          && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
        {
-         tree temp = lookup_field_1 (TREE_TYPE (field), name);
+         tree temp = lookup_field_1 (TREE_TYPE (field), name, want_type);
          if (temp)
            return temp;
        }
@@ -530,10 +513,13 @@ lookup_field_1 (type, name)
           to return a USING_DECL, and the rest of the compiler can't
           handle it.  Once the class is defined, these are purged
           from TYPE_FIELDS anyhow; see handle_using_decl.  */
-       ;
-      else if (DECL_NAME (field) == name)
+       continue;
+
+      if (DECL_NAME (field) == name
+         && (!want_type 
+             || TREE_CODE (field) == TYPE_DECL
+             || DECL_CLASS_TEMPLATE_P (field)))
        return field;
-      field = TREE_CHAIN (field);
     }
   /* Not found.  */
   if (name == vptr_identifier)
@@ -562,7 +548,7 @@ lookup_field_1 (type, name)
    function.  If so, we know to put the decls into the class's scope.  */
 
 tree
-current_scope ()
+current_scope (void)
 {
   if (current_function_decl == NULL_TREE)
     return current_class_type;
@@ -584,7 +570,7 @@ current_scope ()
    not within a member function body of the local class.  */
 
 int
-at_function_scope_p ()
+at_function_scope_p (void)
 {
   tree cs = current_scope ();
   return cs && TREE_CODE (cs) == FUNCTION_DECL;
@@ -593,17 +579,26 @@ at_function_scope_p ()
 /* Returns true if the innermost active scope is a class scope.  */
 
 bool
-at_class_scope_p ()
+at_class_scope_p (void)
 {
   tree cs = current_scope ();
   return cs && TYPE_P (cs);
 }
 
+/* Returns true if the innermost active scope is a namespace scope.  */
+
+bool
+at_namespace_scope_p (void)
+{
+  /* We are in a namespace scope if we are not it a class scope or a
+     function scope.  */
+  return !current_scope();
+}
+
 /* Return the scope of DECL, as appropriate when doing name-lookup.  */
 
 tree
-context_for_name_lookup (decl)
-     tree decl;
+context_for_name_lookup (tree decl)
 {
   /* [class.union]
      
@@ -621,92 +616,23 @@ context_for_name_lookup (decl)
   return context;
 }
 
-/* Return a canonical BINFO if BINFO is a virtual base, or just BINFO
-   otherwise.  */
-
-static tree
-canonical_binfo (binfo)
-     tree binfo;
-{
-  return (TREE_VIA_VIRTUAL (binfo)
-         ? TYPE_BINFO (BINFO_TYPE (binfo)) : binfo);
-}
-
-/* A queue function that simply ensures that we walk into the
-   canonical versions of virtual bases.  */
-
-static tree
-dfs_canonical_queue (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{
-  return canonical_binfo (binfo);
-}
-
-/* Called via dfs_walk from assert_canonical_unmarked.  */
-
-static tree
-dfs_assert_unmarked_p (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{
-  my_friendly_assert (!BINFO_MARKED (binfo), 0);
-  return NULL_TREE;
-}
-
-/* Asserts that all the nodes below BINFO (using the canonical
-   versions of virtual bases) are unmarked.  */
-
-static void
-assert_canonical_unmarked (binfo)
-     tree binfo;
-{
-  dfs_walk (binfo, dfs_assert_unmarked_p, dfs_canonical_queue, 0);
-}
-
-/* If BINFO is marked, return a canonical version of BINFO.
-   Otherwise, return NULL_TREE.  */
-
-static tree
-shared_marked_p (binfo, data)
-     tree binfo;
-     void *data;
-{
-  binfo = canonical_binfo (binfo);
-  return markedp (binfo, data);
-}
-
-/* If BINFO is not marked, return a canonical version of BINFO.
-   Otherwise, return NULL_TREE.  */
-
-static tree
-shared_unmarked_p (binfo, data)
-     tree binfo;
-     void *data;
-{
-  binfo = canonical_binfo (binfo);
-  return unmarkedp (binfo, data);
-}
-
 /* The accessibility routines use BINFO_ACCESS for scratch space
-   during the computation of the accssibility of some declaration.  */
+   during the computation of the accessibility of some declaration.  */
 
 #define BINFO_ACCESS(NODE) \
-  ((access_kind) ((TREE_LANG_FLAG_1 (NODE) << 1) | TREE_LANG_FLAG_6 (NODE)))
+  ((access_kind) ((TREE_PUBLIC (NODE) << 1) | TREE_PRIVATE (NODE)))
 
 /* Set the access associated with NODE to ACCESS.  */
 
 #define SET_BINFO_ACCESS(NODE, ACCESS)                 \
-  ((TREE_LANG_FLAG_1 (NODE) = ((ACCESS) & 2) != 0),    \
-   (TREE_LANG_FLAG_6 (NODE) = ((ACCESS) & 1) != 0))
+  ((TREE_PUBLIC (NODE) = ((ACCESS) & 2) != 0), \
+   (TREE_PRIVATE (NODE) = ((ACCESS) & 1) != 0))
 
 /* Called from access_in_type via dfs_walk.  Calculate the access to
    DATA (which is really a DECL) in BINFO.  */
 
 static tree
-dfs_access_in_type (binfo, data)
-     tree binfo;
-     void *data;
+dfs_access_in_type (tree binfo, void *data)
 {
   tree decl = (tree) data;
   tree type = BINFO_TYPE (binfo);
@@ -714,7 +640,7 @@ dfs_access_in_type (binfo, data)
 
   if (context_for_name_lookup (decl) == type)
     {
-      /* If we have desceneded to the scope of DECL, just note the
+      /* If we have descended to the scope of DECL, just note the
         appropriate access.  */
       if (TREE_PRIVATE (decl))
        access = ak_private;
@@ -732,51 +658,59 @@ dfs_access_in_type (binfo, data)
       if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
        {
          tree decl_access = purpose_member (type, DECL_ACCESS (decl));
+         
          if (decl_access)
-           access = ((access_kind) 
-                     TREE_INT_CST_LOW (TREE_VALUE (decl_access)));
+           {
+             decl_access = TREE_VALUE (decl_access);
+             
+             if (decl_access == access_public_node)
+               access = ak_public;
+             else if (decl_access == access_protected_node)
+               access = ak_protected;
+             else if (decl_access == access_private_node)
+               access = ak_private;
+             else
+               my_friendly_assert (false, 20030217);
+           }
        }
 
       if (!access)
        {
          int i;
          int n_baselinks;
-         tree binfos;
+         tree binfos, accesses;
          
          /* Otherwise, scan our baseclasses, and pick the most favorable
             access.  */
          binfos = BINFO_BASETYPES (binfo);
+         accesses = BINFO_BASEACCESSES (binfo);
          n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
          for (i = 0; i < n_baselinks; ++i)
            {
              tree base_binfo = TREE_VEC_ELT (binfos, i);
-             access_kind base_access 
-               = BINFO_ACCESS (canonical_binfo (base_binfo));
+             tree base_access = TREE_VEC_ELT (accesses, i);
+             access_kind base_access_now = BINFO_ACCESS (base_binfo);
 
-             if (base_access == ak_none || base_access == ak_private)
+             if (base_access_now == ak_none || base_access_now == ak_private)
                /* If it was not accessible in the base, or only
                   accessible as a private member, we can't access it
                   all.  */
-               base_access = ak_none;
-             else if (TREE_VIA_PROTECTED (base_binfo))
-               /* Public and protected members in the base are
+               base_access_now = ak_none;
+             else if (base_access == access_protected_node)
+               /* Public and protected members in the base become
                   protected here.  */
-               base_access = ak_protected;
-             else if (!TREE_VIA_PUBLIC (base_binfo))
-               /* Public and protected members in the base are
+               base_access_now = ak_protected;
+             else if (base_access == access_private_node)
+               /* Public and protected members in the base become
                   private here.  */
-               base_access = ak_private;
+               base_access_now = ak_private;
 
              /* See if the new access, via this base, gives more
                 access than our previous best access.  */
-             if (base_access != ak_none
-                 && (base_access == ak_public
-                     || (base_access == ak_protected
-                         && access != ak_public)
-                     || (base_access == ak_private 
-                         && access == ak_none)))
+             if (base_access_now != ak_none
+                 && (access == ak_none || base_access_now < access))
                {
-                 access = base_access;
+                 access = base_access_now;
 
                  /* If the new access is public, we can't do better.  */
                  if (access == ak_public)
@@ -791,7 +725,7 @@ dfs_access_in_type (binfo, data)
 
   /* Mark TYPE as visited so that if we reach it again we do not
      duplicate our efforts here.  */
-  SET_BINFO_MARKED (binfo);
+  BINFO_MARKED (binfo) = 1;
 
   return NULL_TREE;
 }
@@ -799,9 +733,7 @@ dfs_access_in_type (binfo, data)
 /* Return the access to DECL in TYPE.  */
 
 static access_kind
-access_in_type (type, decl)
-     tree type;
-     tree decl;
+access_in_type (tree type, tree decl)
 {
   tree binfo = TYPE_BINFO (type);
 
@@ -816,62 +748,52 @@ access_in_type (type, decl)
     The algorithm we use is to make a post-order depth-first traversal
     of the base-class hierarchy.  As we come up the tree, we annotate
     each node with the most lenient access.  */
-  dfs_walk_real (binfo, 0, dfs_access_in_type, shared_unmarked_p, decl);
-  dfs_walk (binfo, dfs_unmark, shared_marked_p,  0);
-  assert_canonical_unmarked (binfo);
+  dfs_walk_real (binfo, 0, dfs_access_in_type, unmarkedp, decl);
+  dfs_walk (binfo, dfs_unmark, markedp,  0);
 
   return BINFO_ACCESS (binfo);
 }
 
-/* Called from dfs_accessible_p via dfs_walk.  */
+/* Called from accessible_p via dfs_walk.  */
 
 static tree
-dfs_accessible_queue_p (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+dfs_accessible_queue_p (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
 {
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
   if (BINFO_MARKED (binfo))
     return NULL_TREE;
 
   /* If this class is inherited via private or protected inheritance,
-     then we can't see it, unless we are a friend of the subclass.  */
-  if (!TREE_VIA_PUBLIC (binfo)
-      && !is_friend (BINFO_TYPE (BINFO_INHERITANCE_CHAIN (binfo)),
-                    current_scope ()))
+     then we can't see it, unless we are a friend of the derived class.  */
+  if (BINFO_BASEACCESS (derived, ix) != access_public_node
+      && !is_friend (BINFO_TYPE (derived), current_scope ()))
     return NULL_TREE;
 
-  return canonical_binfo (binfo);
+  return binfo;
 }
 
-/* Called from dfs_accessible_p via dfs_walk.  */
+/* Called from accessible_p via dfs_walk.  */
 
 static tree
-dfs_accessible_p (binfo, data)
-     tree binfo;
-     void *data;
+dfs_accessible_p (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
-  int protected_ok = data != 0;
   access_kind access;
 
-  SET_BINFO_MARKED (binfo);
+  BINFO_MARKED (binfo) = 1;
   access = BINFO_ACCESS (binfo);
-  if (access == ak_public || (access == ak_protected && protected_ok))
-    return binfo;
-  else if (access != ak_none
-          && is_friend (BINFO_TYPE (binfo), current_scope ()))
+  if (access != ak_none
+      && is_friend (BINFO_TYPE (binfo), current_scope ()))
     return binfo;
 
   return NULL_TREE;
 }
 
 /* Returns nonzero if it is OK to access DECL through an object
-   indiated by BINFO in the context of DERIVED.  */
+   indicated by BINFO in the context of DERIVED.  */
 
 static int
-protected_accessible_p (decl, derived, binfo)
-     tree decl;
-     tree derived;
-     tree binfo;
+protected_accessible_p (tree decl, tree derived, tree binfo)
 {
   access_kind access;
 
@@ -931,10 +853,7 @@ protected_accessible_p (decl, derived, binfo)
    to access DECL through the object indicated by BINFO.  */
 
 static int
-friend_accessible_p (scope, decl, binfo)
-     tree scope;
-     tree decl;
-     tree binfo;
+friend_accessible_p (tree scope, tree decl, tree binfo)
 {
   tree befriending_classes;
   tree t;
@@ -980,41 +899,6 @@ friend_accessible_p (scope, decl, binfo)
   return 0;
 }
 
-/* Perform access control on TYPE_DECL or TEMPLATE_DECL VAL, which was
-   looked up in TYPE.  This is fairly complex, so here's the design:
-
-   The lang_extdef nonterminal sets type_lookups to NULL_TREE before we
-     start to process a top-level declaration.
-   As we process the decl-specifier-seq for the declaration, any types we
-     see that might need access control are passed to type_access_control,
-     which defers checking by adding them to type_lookups.
-   When we are done with the decl-specifier-seq, we record the lookups we've
-     seen in the lookups field of the typed_declspecs nonterminal.
-   When we process the first declarator, either in parse_decl or
-     begin_function_definition, we call save_type_access_control,
-     which stores the lookups from the decl-specifier-seq in
-     current_type_lookups.
-   As we finish with each declarator, we process everything in type_lookups
-     via decl_type_access_control, which resets type_lookups to the value of
-     current_type_lookups for subsequent declarators.
-   When we enter a function, we set type_lookups to error_mark_node, so all
-     lookups are processed immediately.  */
-
-void
-type_access_control (type, val)
-     tree type, val;
-{
-  if (val == NULL_TREE
-      || (TREE_CODE (val) != TEMPLATE_DECL && TREE_CODE (val) != TYPE_DECL)
-      || ! DECL_CLASS_SCOPE_P (val))
-    return;
-
-  if (type_lookups == error_mark_node)
-    enforce_access (type, val);
-  else if (! accessible_p (type, val))
-    type_lookups = tree_cons (type, val, type_lookups);
-}
-
 /* DECL is a declaration from a base class of TYPE, which was the
    class used to name DECL.  Return nonzero if, in the current
    context, DECL is accessible.  If TYPE is actually a BINFO node,
@@ -1022,27 +906,34 @@ type_access_control (type, val)
    at the most derived class along the path indicated by BINFO.  */
 
 int 
-accessible_p (type, decl)
-     tree type;
-     tree decl;
-     
+accessible_p (tree type, tree decl)
 {
   tree binfo;
   tree t;
+  tree scope;
+  access_kind access;
 
   /* Nonzero if it's OK to access DECL if it has protected
      accessibility in TYPE.  */
   int protected_ok = 0;
 
-  /* If we're not checking access, everything is accessible.  */
-  if (!scope_chain->check_access)
-    return 1;
-
   /* If this declaration is in a block or namespace scope, there's no
      access control.  */
   if (!TYPE_P (context_for_name_lookup (decl)))
     return 1;
 
+  /* There is no need to perform access checks inside a thunk.  */
+  scope = current_scope ();
+  if (scope && DECL_THUNK_P (scope))
+    return 1;
+
+  /* In a template declaration, we cannot be sure whether the
+     particular specialization that is instantiated will be a friend
+     or not.  Therefore, all access checks are deferred until
+     instantiation.  */
+  if (processing_template_decl)
+    return 1;
+
   if (!TYPE_P (type))
     {
       binfo = type;
@@ -1078,7 +969,7 @@ accessible_p (type, decl)
 
   /* Now, loop through the classes of which we are a friend.  */
   if (!protected_ok)
-    protected_ok = friend_accessible_p (current_scope (), decl, binfo);
+    protected_ok = friend_accessible_p (scope, decl, binfo);
 
   /* Standardize the binfo that access_in_type will use.  We don't
      need to know what path was chosen from this point onwards.  */
@@ -1086,83 +977,22 @@ accessible_p (type, decl)
 
   /* Compute the accessibility of DECL in the class hierarchy
      dominated by type.  */
-  access_in_type (type, decl);
-  /* Walk the hierarchy again, looking for a base class that allows
-     access.  */
-  t = dfs_walk (binfo, dfs_accessible_p, 
-               dfs_accessible_queue_p,
-               protected_ok ? &protected_ok : 0);
-  /* Clear any mark bits.  Note that we have to walk the whole tree
-     here, since we have aborted the previous walk from some point
-     deep in the tree.  */
-  dfs_walk (binfo, dfs_unmark, dfs_canonical_queue,  0);
-  assert_canonical_unmarked (binfo);
-
-  return t != NULL_TREE;
-}
-
-/* Recursive helper funciton for is_subobject_of_p; see that routine
-   for documentation of the parameters.  */
-
-static int
-is_subobject_of_p_1 (parent, binfo, most_derived)
-     tree parent, binfo, most_derived;
-{
-  tree binfos;
-  int i, n_baselinks;
-
-  if (parent == binfo)
+  access = access_in_type (type, decl);
+  if (access == ak_public
+      || (access == ak_protected && protected_ok))
     return 1;
-
-  binfos = BINFO_BASETYPES (binfo);
-  n_baselinks = binfos ? TREE_VEC_LENGTH (binfos) : 0;
-
-  /* Iterate through the base types.  */
-  for (i = 0; i < n_baselinks; i++)
+  else
     {
-      tree base_binfo = TREE_VEC_ELT (binfos, i);
-      tree base_type;
-
-      base_type = TREE_TYPE (base_binfo);
-      if (!CLASS_TYPE_P (base_type))
-       /* If we see a TEMPLATE_TYPE_PARM, or some such, as a base
-          class there's no way to descend into it.  */
-       continue;
-
-      /* Avoid walking into the same virtual base more than once.  */
-      if (TREE_VIA_VIRTUAL (base_binfo))
-       {
-         if (CLASSTYPE_MARKED4 (base_type))
-           continue;
-         SET_CLASSTYPE_MARKED4 (base_type);
-         base_binfo = binfo_for_vbase (base_type, most_derived);
-       }
-
-      if (is_subobject_of_p_1 (parent, base_binfo, most_derived))
-       return 1;
+      /* Walk the hierarchy again, looking for a base class that allows
+        access.  */
+      t = dfs_walk (binfo, dfs_accessible_p, dfs_accessible_queue_p, 0);
+      /* Clear any mark bits.  Note that we have to walk the whole tree
+        here, since we have aborted the previous walk from some point
+        deep in the tree.  */
+      dfs_walk (binfo, dfs_unmark, 0,  0);
+
+      return t != NULL_TREE;
     }
-  return 0;
-}
-
-/* Routine to see if the sub-object denoted by the binfo PARENT can be
-   found as a base class and sub-object of the object denoted by
-   BINFO.  MOST_DERIVED is the most derived type of the hierarchy being
-   searched.  */
-
-static int
-is_subobject_of_p (tree parent, tree binfo, tree most_derived)
-{
-  int result;
-  tree vbase;
-
-  result = is_subobject_of_p_1 (parent, binfo, most_derived);
-  /* Clear the mark bits on virtual bases.  */
-  for (vbase = CLASSTYPE_VBASECLASSES (most_derived);
-       vbase;
-       vbase = TREE_CHAIN (vbase))
-    CLEAR_CLASSTYPE_MARKED4 (TREE_TYPE (TREE_VALUE (vbase)));
-
-  return result;
 }
 
 struct lookup_field_info {
@@ -1179,8 +1009,6 @@ struct lookup_field_info {
   tree ambiguous;
   /* If nonzero, we are looking for types, not data members.  */
   int want_type;
-  /* If nonzero, RVAL was found by looking through a dependent base.  */
-  int from_dep_base_p;
   /* If something went wrong, a message indicating what.  */
   const char *errstr;
 };
@@ -1191,10 +1019,9 @@ struct lookup_field_info {
    lookup_field via breadth_first_search.  */
 
 static tree
-lookup_field_queue_p (binfo, data)
-     tree binfo;
-     void *data;
+lookup_field_queue_p (tree derived, int ix, void *data)
 {
+  tree binfo = BINFO_BASETYPE (derived, ix);
   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
 
   /* Don't look for constructors or destructors in base classes.  */
@@ -1203,11 +1030,13 @@ lookup_field_queue_p (binfo, data)
 
   /* If this base class is hidden by the best-known value so far, we
      don't need to look.  */
-  binfo = CANONICAL_BINFO (binfo, lfi->type);
-  if (!lfi->from_dep_base_p && lfi->rval_binfo
-      && is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
+  if (lfi->rval_binfo && original_binfo (binfo, lfi->rval_binfo))
     return NULL_TREE;
 
+  /* If this is a dependent base, don't look in it.  */
+  if (BINFO_DEPENDENT_BASE_P (binfo))
+    return NULL_TREE;
+  
   return binfo;
 }
 
@@ -1220,9 +1049,7 @@ lookup_field_queue_p (binfo, data)
    Returns nonzero if DECL is such a declaration in a class TYPE.  */
 
 static int
-template_self_reference_p (type, decl)
-     tree type;
-     tree decl;
+template_self_reference_p (tree type, tree decl)
 {
   return  (CLASSTYPE_USE_TEMPLATE (type)
           && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
@@ -1243,8 +1070,7 @@ template_self_reference_p (type, decl)
    This function checks that T contains no nonstatic members.  */
 
 static int
-shared_member_p (t)
-     tree t;
+shared_member_p (tree t)
 {
   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
       || TREE_CODE (t) == CONST_DECL)
@@ -1268,14 +1094,11 @@ shared_member_p (t)
    lookup_field via breadth_first_search.  */
 
 static tree
-lookup_field_r (binfo, data)
-     tree binfo;
-     void *data;
+lookup_field_r (tree binfo, void *data)
 {
   struct lookup_field_info *lfi = (struct lookup_field_info *) data;
   tree type = BINFO_TYPE (binfo);
   tree nval = NULL_TREE;
-  int from_dep_base_p;
 
   /* First, look for a function.  There can't be a function and a data
      member with the same name, and if there's a function and a type
@@ -1289,7 +1112,7 @@ lookup_field_r (binfo, data)
 
   if (!nval)
     /* Look for a data member or type.  */
-    nval = lookup_field_1 (type, lfi->name);
+    nval = lookup_field_1 (type, lfi->name, lfi->want_type);
 
   /* If there is no declaration with the indicated name in this type,
      then there's nothing to do.  */
@@ -1314,11 +1137,12 @@ lookup_field_r (binfo, data)
        }
       else
        nval = NULL_TREE;
-      if (!nval)
+      if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
        {
-         nval = purpose_member (lfi->name, CLASSTYPE_TAGS (type));
-         if (nval)
-           nval = TYPE_MAIN_DECL (TREE_VALUE (nval));
+          binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
+                                                lfi->name);
+         if (e != NULL)
+           nval = TYPE_MAIN_DECL (e->type);
          else 
            return NULL_TREE;
        }
@@ -1329,40 +1153,14 @@ lookup_field_r (binfo, data)
       && template_self_reference_p (type, nval))
     return NULL_TREE;
 
-  from_dep_base_p = dependent_base_p (binfo);
-  if (lfi->from_dep_base_p && !from_dep_base_p)
-    {
-      /* If the new declaration is not found via a dependent base, and
-        the old one was, then we must prefer the new one.  We weren't
-        really supposed to be able to find the old one, so we don't
-        want to be affected by a specialization.  Consider:
-
-          struct B { typedef int I; };
-          template <typename T> struct D1 : virtual public B {}; 
-          template <typename T> struct D :
-          public D1, virtual pubic B { I i; };
-
-        The `I' in `D<T>' is unambigousuly `B::I', regardless of how
-        D1 is specialized.  */
-      lfi->from_dep_base_p = 0;
-      lfi->rval = NULL_TREE;
-      lfi->rval_binfo = NULL_TREE;
-      lfi->ambiguous = NULL_TREE;
-      lfi->errstr = 0;
-    }
-  else if (lfi->rval_binfo && !lfi->from_dep_base_p && from_dep_base_p)
-    /* Similarly, if the old declaration was not found via a dependent
-       base, and the new one is, ignore the new one.  */
-    return NULL_TREE;
-
   /* If the lookup already found a match, and the new value doesn't
      hide the old one, we might have an ambiguity.  */
-  if (lfi->rval_binfo && !is_subobject_of_p (lfi->rval_binfo, binfo, lfi->type))
+  if (lfi->rval_binfo && !original_binfo (lfi->rval_binfo, binfo))
     {
       if (nval == lfi->rval && shared_member_p (nval))
        /* The two things are really the same.  */
        ;
-      else if (is_subobject_of_p (binfo, lfi->rval_binfo, lfi->type))
+      else if (original_binfo (binfo, lfi->rval_binfo))
        /* The previous value hides the new one.  */
        ;
       else
@@ -1386,18 +1184,7 @@ lookup_field_r (binfo, data)
     }
   else
     {
-      if (from_dep_base_p && TREE_CODE (nval) != TYPE_DECL
-         /* We need to return a member template class so we can
-            define partial specializations.  Is there a better
-            way?  */
-         && !DECL_CLASS_TEMPLATE_P (nval))
-       /* The thing we're looking for isn't a type, so the implicit
-          typename extension doesn't apply, so we just pretend we
-          didn't find anything.  */
-       return NULL_TREE;
-
       lfi->rval = nval;
-      lfi->from_dep_base_p = from_dep_base_p;
       lfi->rval_binfo = binfo;
     }
 
@@ -1421,8 +1208,8 @@ build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
   my_friendly_assert (!optype || TYPE_P (optype), 20020730);
   my_friendly_assert (TREE_TYPE (functions), 20020805);
 
-  baselink = build (BASELINK, TREE_TYPE (functions), NULL_TREE,
-                   NULL_TREE, NULL_TREE);
+  baselink = make_node (BASELINK);
+  TREE_TYPE (baselink) = TREE_TYPE (functions);
   BASELINK_BINFO (baselink) = binfo;
   BASELINK_ACCESS_BINFO (baselink) = access_binfo;
   BASELINK_FUNCTIONS (baselink) = functions;
@@ -1444,9 +1231,7 @@ build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
    If nothing can be found return NULL_TREE and do not issue an error.  */
 
 tree
-lookup_member (xbasetype, name, protect, want_type)
-     register tree xbasetype, name;
-     int protect, want_type;
+lookup_member (tree xbasetype, tree name, int protect, bool want_type)
 {
   tree rval, rval_binfo = NULL_TREE;
   tree type = NULL_TREE, basetype_path = NULL_TREE;
@@ -1461,31 +1246,31 @@ lookup_member (xbasetype, name, protect, want_type)
 
   const char *errstr = 0;
 
-  if (xbasetype == current_class_type && TYPE_BEING_DEFINED (xbasetype)
-      && IDENTIFIER_CLASS_VALUE (name))
-    {
-      tree field = IDENTIFIER_CLASS_VALUE (name);
-      if (TREE_CODE (field) != FUNCTION_DECL
-         && ! (want_type && TREE_CODE (field) != TYPE_DECL))
-       /* We're in the scope of this class, and the value has already
-          been looked up.  Just return the cached value.  */
-       return field;
-    }
+  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030624);
 
   if (TREE_CODE (xbasetype) == TREE_VEC)
     {
       type = BINFO_TYPE (xbasetype);
       basetype_path = xbasetype;
     }
-  else if (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)))
+  else
     {
+      my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)), 20030624);
       type = xbasetype;
       basetype_path = TYPE_BINFO (type);
-      my_friendly_assert (BINFO_INHERITANCE_CHAIN (basetype_path) == NULL_TREE,
-                         980827);
+      my_friendly_assert (!BINFO_INHERITANCE_CHAIN (basetype_path), 980827);
+    }
+
+  if (type == current_class_type && TYPE_BEING_DEFINED (type)
+      && IDENTIFIER_CLASS_VALUE (name))
+    {
+      tree field = IDENTIFIER_CLASS_VALUE (name);
+      if (! is_overloaded_fn (field)
+         && ! (want_type && TREE_CODE (field) != TYPE_DECL))
+       /* We're in the scope of this class, and the value has already
+          been looked up.  Just return the cached value.  */
+       return field;
     }
-  else
-    abort ();
 
   complete_type (type);
 
@@ -1493,7 +1278,7 @@ lookup_member (xbasetype, name, protect, want_type)
   n_calls_lookup_field++;
 #endif /* GATHER_STATISTICS */
 
-  memset ((PTR) &lfi, 0, sizeof (lfi));
+  memset (&lfi, 0, sizeof (lfi));
   lfi.type = type;
   lfi.name = name;
   lfi.want_type = want_type;
@@ -1521,9 +1306,8 @@ lookup_member (xbasetype, name, protect, want_type)
 
      In the case of overloaded function names, access control is
      applied to the function selected by overloaded resolution.  */
-  if (rval && protect && !is_overloaded_fn (rval)
-      && !enforce_access (xbasetype, rval))
-    return error_mark_node;
+  if (rval && protect && !is_overloaded_fn (rval))
+    perform_or_defer_access_check (basetype_path, rval);
 
   if (errstr && protect)
     {
@@ -1533,13 +1317,6 @@ lookup_member (xbasetype, name, protect, want_type)
       rval = error_mark_node;
     }
 
-  /* If the thing we found was found via the implicit typename
-     extension, build the typename type.  */
-  if (rval && lfi.from_dep_base_p && !DECL_CLASS_TEMPLATE_P (rval))
-    rval = TYPE_STUB_DECL (build_typename_type (BINFO_TYPE (basetype_path),
-                                               name, name,
-                                               TREE_TYPE (rval)));
-
   if (rval && is_overloaded_fn (rval)) 
     rval = build_baselink (rval_binfo, basetype_path, rval,
                           (IDENTIFIER_TYPENAME_P (name)
@@ -1551,9 +1328,7 @@ lookup_member (xbasetype, name, protect, want_type)
    return NULL_TREE.  */
 
 tree
-lookup_field (xbasetype, name, protect, want_type)
-     register tree xbasetype, name;
-     int protect, want_type;
+lookup_field (tree xbasetype, tree name, int protect, bool want_type)
 {
   tree rval = lookup_member (xbasetype, name, protect, want_type);
   
@@ -1568,11 +1343,9 @@ lookup_field (xbasetype, name, protect, want_type)
    return NULL_TREE.  */
 
 tree
-lookup_fnfields (xbasetype, name, protect)
-     register tree xbasetype, name;
-     int protect;
+lookup_fnfields (tree xbasetype, tree name, int protect)
 {
-  tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/0);
+  tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
 
   /* Ignore non-functions.  */
   if (rval && !BASELINK_P (rval))
@@ -1581,95 +1354,144 @@ lookup_fnfields (xbasetype, name, protect)
   return rval;
 }
 
+/* Return the index in the CLASSTYPE_METHOD_VEC for CLASS_TYPE
+   corresponding to "operator TYPE ()", or -1 if there is no such
+   operator.  Only CLASS_TYPE itself is searched; this routine does
+   not scan the base classes of CLASS_TYPE.  */
+
+static int
+lookup_conversion_operator (tree class_type, tree type)
+{
+  int pass;
+  int i;
+
+  tree methods = CLASSTYPE_METHOD_VEC (class_type);
+
+  for (pass = 0; pass < 2; ++pass)
+    for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 
+        i < TREE_VEC_LENGTH (methods);
+        ++i)
+      {
+       tree fn = TREE_VEC_ELT (methods, i);
+       /* The size of the vector may have some unused slots at the
+          end.  */
+       if (!fn)
+         break;
+
+       /* All the conversion operators come near the beginning of the
+          class.  Therefore, if FN is not a conversion operator, there
+          is no matching conversion operator in CLASS_TYPE.  */
+       fn = OVL_CURRENT (fn);
+       if (!DECL_CONV_FN_P (fn))
+         break;
+       
+       if (pass == 0)
+         {
+           /* On the first pass we only consider exact matches.  If
+              the types match, this slot is the one where the right
+              conversion operators can be found.  */
+           if (TREE_CODE (fn) != TEMPLATE_DECL
+               && same_type_p (DECL_CONV_FN_TYPE (fn), type))
+             return i;
+         }
+       else
+         {
+           /* On the second pass we look for template conversion
+              operators.  It may be possible to instantiate the
+              template to get the type desired.  All of the template
+              conversion operators share a slot.  By looking for
+              templates second we ensure that specializations are
+              preferred over templates.  */
+           if (TREE_CODE (fn) == TEMPLATE_DECL)
+             return i;
+         }
+      }
+
+  return -1;
+}
+
 /* TYPE is a class type. Return the index of the fields within
    the method vector with name NAME, or -1 is no such field exists.  */
 
 int
-lookup_fnfields_1 (type, name)
-     tree type, name;
+lookup_fnfields_1 (tree type, tree name)
 {
-  tree method_vec = (CLASS_TYPE_P (type)
-                    ? CLASSTYPE_METHOD_VEC (type)
-                    : NULL_TREE);
+  tree method_vec;
+  tree *methods;
+  tree tmp;
+  int i;
+  int len;
 
-  if (method_vec != 0)
-    {
-      register int i;
-      register tree *methods = &TREE_VEC_ELT (method_vec, 0);
-      int len = TREE_VEC_LENGTH (method_vec);
-      tree tmp;
+  if (!CLASS_TYPE_P (type))
+    return -1;
 
-#ifdef GATHER_STATISTICS
-      n_calls_lookup_fnfields_1++;
-#endif /* GATHER_STATISTICS */
+  method_vec = CLASSTYPE_METHOD_VEC (type);
+
+  if (!method_vec)
+    return -1;
+
+  methods = &TREE_VEC_ELT (method_vec, 0);
+  len = TREE_VEC_LENGTH (method_vec);
 
-      /* Constructors are first...  */
-      if (name == ctor_identifier)
-       return (methods[CLASSTYPE_CONSTRUCTOR_SLOT] 
-               ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
-      /* and destructors are second.  */
-      if (name == dtor_identifier)
-       return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
-               ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
-
-      for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 
-          i < len && methods[i]; 
-          ++i)
-       {
 #ifdef GATHER_STATISTICS
-         n_outer_fields_searched++;
+  n_calls_lookup_fnfields_1++;
 #endif /* GATHER_STATISTICS */
 
-         tmp = OVL_CURRENT (methods[i]);
-         if (DECL_NAME (tmp) == name)
-           return i;
-
-         /* If the type is complete and we're past the conversion ops,
-            switch to binary search.  */
-         if (! DECL_CONV_FN_P (tmp)
-             && COMPLETE_TYPE_P (type))
-           {
-             int lo = i + 1, hi = len;
+  /* Constructors are first...  */
+  if (name == ctor_identifier)
+    return (methods[CLASSTYPE_CONSTRUCTOR_SLOT] 
+           ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
+  /* and destructors are second.  */
+  if (name == dtor_identifier)
+    return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
+           ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
+  if (IDENTIFIER_TYPENAME_P (name))
+    return lookup_conversion_operator (type, TREE_TYPE (name));
+
+  /* Skip the conversion operators.  */
+  i = CLASSTYPE_FIRST_CONVERSION_SLOT;
+  while (i < len && methods[i] && DECL_CONV_FN_P (OVL_CURRENT (methods[i])))
+    i++;
+
+  /* If the type is complete, use binary search.  */
+  if (COMPLETE_TYPE_P (type))
+    {
+      int lo = i;
+      int hi = len;
 
-             while (lo < hi)
-               {
-                 i = (lo + hi) / 2;
+      while (lo < hi)
+       {
+         i = (lo + hi) / 2;
 
 #ifdef GATHER_STATISTICS
-                 n_outer_fields_searched++;
+         n_outer_fields_searched++;
 #endif /* GATHER_STATISTICS */
 
-                 tmp = DECL_NAME (OVL_CURRENT (methods[i]));
-
-                 if (tmp > name)
-                   hi = i;
-                 else if (tmp < name)
-                   lo = i + 1;
-                 else
-                   return i;
-               }
-             break;
-           }
-       }
-
-      /* If we didn't find it, it might have been a template
-        conversion operator to a templated type.  If there are any,
-        such template conversion operators will all be overloaded on
-        the first conversion slot.  (Note that we don't look for this
-        case above so that we will always find specializations
-        first.)  */
-      if (IDENTIFIER_TYPENAME_P (name)) 
-       {
-         i = CLASSTYPE_FIRST_CONVERSION_SLOT;
-         if (i < len && methods[i])
-           {
-             tmp = OVL_CURRENT (methods[i]);
-             if (TREE_CODE (tmp) == TEMPLATE_DECL
-                 && DECL_TEMPLATE_CONV_FN_P (tmp))
-               return i;
-           }
+         tmp = methods[i];
+         /* This slot may be empty; we allocate more slots than we
+            need.  In that case, the entry we're looking for is
+            closer to the beginning of the list.  */
+         if (tmp)
+           tmp = DECL_NAME (OVL_CURRENT (tmp));
+         if (!tmp || tmp > name)
+           hi = i;
+         else if (tmp < name)
+           lo = i + 1;
+         else
+           return i;
        }
     }
+  else
+    for (; i < len && methods[i]; ++i)
+      {
+#ifdef GATHER_STATISTICS
+       n_outer_fields_searched++;
+#endif /* GATHER_STATISTICS */
+       
+       tmp = OVL_CURRENT (methods[i]);
+       if (DECL_NAME (tmp) == name)
+         return i;
+      }
 
   return -1;
 }
@@ -1698,10 +1520,11 @@ adjust_result_of_qualified_name_lookup (tree decl,
 
       my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
 
-      /* Look for the QUALIFYING_SCOPE as a base of the
-        CONTEXT_CLASS.  If QUALIFYING_SCOPE is ambiguous, we cannot
-        be sure yet than an error has occurred; perhaps the function
-        chosen by overload resolution will be static.  */
+      /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
+        Because we do not yet know which function will be chosen by
+        overload resolution, we cannot yet check either accessibility
+        or ambiguity -- in either case, the choice of a static member
+        function might make the usage valid.  */
       base = lookup_base (context_class, qualifying_scope,
                          ba_ignore | ba_quiet, NULL);
       if (base)
@@ -1722,69 +1545,91 @@ adjust_result_of_qualified_name_lookup (tree decl,
    type in the hierarchy, in a breadth-first preorder traversal.
    If it ever returns a non-NULL value, that value is immediately
    returned and the walk is terminated.  At each node, FN is passed a
-   BINFO indicating the path from the curently visited base-class to
+   BINFO indicating the path from the currently visited base-class to
    TYPE.  Before each base-class is walked QFN is called.  If the
    value returned is nonzero, the base-class is walked; otherwise it
    is not.  If QFN is NULL, it is treated as a function which always
    returns 1.  Both FN and QFN are passed the DATA whenever they are
-   called.  */
+   called.
+
+   Implementation notes: Uses a circular queue, which starts off on
+   the stack but gets moved to the malloc arena if it needs to be
+   enlarged.  The underflow and overflow conditions are
+   indistinguishable except by context: if head == tail and we just
+   moved the head pointer, the queue is empty, but if we just moved
+   the tail pointer, the queue is full.  
+   Start with enough room for ten concurrent base classes.  That
+   will be enough for most hierarchies.  */
+#define BFS_WALK_INITIAL_QUEUE_SIZE 10
 
 static tree
-bfs_walk (binfo, fn, qfn, data)
-     tree binfo;
-     tree (*fn) PARAMS ((tree, void *));
-     tree (*qfn) PARAMS ((tree, void *));
-     void *data;
-{
-  size_t head;
-  size_t tail;
+bfs_walk (tree binfo,
+         tree (*fn) (tree, void *),
+         tree (*qfn) (tree, int, void *),
+         void *data)
+{
   tree rval = NULL_TREE;
-  /* An array of the base classes of BINFO.  These will be built up in
-     breadth-first order, except where QFN prunes the search.  */
-  varray_type bfs_bases;
 
-  /* Start with enough room for ten base classes.  That will be enough
-     for most hierarchies.  */
-  VARRAY_TREE_INIT (bfs_bases, 10, "search_stack");
+  tree bases_initial[BFS_WALK_INITIAL_QUEUE_SIZE];
+  /* A circular queue of the base classes of BINFO.  These will be
+     built up in breadth-first order, except where QFN prunes the
+     search.  */
+  size_t head, tail;
+  size_t base_buffer_size = BFS_WALK_INITIAL_QUEUE_SIZE;
+  tree *base_buffer = bases_initial;
 
-  /* Put the first type into the stack.  */
-  VARRAY_TREE (bfs_bases, 0) = binfo;
-  tail = 1;
+  head = tail = 0;
+  base_buffer[tail++] = binfo;
 
-  for (head = 0; head < tail; ++head)
+  while (head != tail)
     {
-      int i;
-      int n_baselinks;
-      tree binfos;
-
-      /* Pull the next type out of the queue.  */
-      binfo = VARRAY_TREE (bfs_bases, head);
+      int n_bases, ix;
+      tree binfo = base_buffer[head++];
+      if (head == base_buffer_size)
+       head = 0;
 
-      /* If this is the one we're looking for, we're done.  */
-      rval = (*fn) (binfo, data);
+      /* Is this the one we're looking for?  If so, we're done.  */
+      rval = fn (binfo, data);
       if (rval)
-       break;
+       goto done;
 
-      /* Queue up the base types.  */
-      binfos = BINFO_BASETYPES (binfo);
-      n_baselinks = binfos ? TREE_VEC_LENGTH (binfos): 0;
-      for (i = 0; i < n_baselinks; i++)
+      n_bases = BINFO_N_BASETYPES (binfo);
+      for (ix = 0; ix != n_bases; ix++)
        {
-         tree base_binfo = TREE_VEC_ELT (binfos, i);
-
+         tree base_binfo;
+         
          if (qfn)
-           base_binfo = (*qfn) (base_binfo, data);
-
-         if (base_binfo)
+           base_binfo = (*qfn) (binfo, ix, data);
+         else
+           base_binfo = BINFO_BASETYPE (binfo, ix);
+         
+         if (base_binfo)
            {
-             if (tail == VARRAY_SIZE (bfs_bases))
-               VARRAY_GROW (bfs_bases, 2 * VARRAY_SIZE (bfs_bases));
-             VARRAY_TREE (bfs_bases, tail) = base_binfo;
-             ++tail;
+             base_buffer[tail++] = base_binfo;
+             if (tail == base_buffer_size)
+               tail = 0;
+             if (tail == head)
+               {
+                 tree *new_buffer = xmalloc (2 * base_buffer_size
+                                             * sizeof (tree));
+                 memcpy (&new_buffer[0], &base_buffer[0],
+                         tail * sizeof (tree));
+                 memcpy (&new_buffer[head + base_buffer_size],
+                         &base_buffer[head],
+                         (base_buffer_size - head) * sizeof (tree));
+                 if (base_buffer_size != BFS_WALK_INITIAL_QUEUE_SIZE)
+                   free (base_buffer);
+                 base_buffer = new_buffer;
+                 head += base_buffer_size;
+                 base_buffer_size *= 2;
+               }
            }
        }
     }
 
+ done:
+  if (base_buffer_size != BFS_WALK_INITIAL_QUEUE_SIZE)
+    free (base_buffer);
   return rval;
 }
 
@@ -1793,16 +1638,12 @@ bfs_walk (binfo, fn, qfn, data)
    in postorder.  */
 
 tree
-dfs_walk_real (binfo, prefn, postfn, qfn, data)
-     tree binfo;
-     tree (*prefn) PARAMS ((tree, void *));
-     tree (*postfn) PARAMS ((tree, void *));
-     tree (*qfn) PARAMS ((tree, void *));
-     void *data;
+dfs_walk_real (tree binfo,
+              tree (*prefn) (tree, void *),
+              tree (*postfn) (tree, void *),
+              tree (*qfn) (tree, int, void *),
+              void *data)
 {
-  int i;
-  int n_baselinks;
-  tree binfos;
   tree rval = NULL_TREE;
 
   /* Call the pre-order walking function.  */
@@ -1814,20 +1655,24 @@ dfs_walk_real (binfo, prefn, postfn, qfn, data)
     }
 
   /* Process the basetypes.  */
-  binfos = BINFO_BASETYPES (binfo);
-  n_baselinks = BINFO_N_BASETYPES (binfo);
-  for (i = 0; i < n_baselinks; i++)
+  if (BINFO_BASETYPES (binfo))
     {
-      tree base_binfo = TREE_VEC_ELT (binfos, i);
-      
-      if (qfn)
-       base_binfo = (*qfn) (base_binfo, data);
-
-      if (base_binfo)
+      int i, n = TREE_VEC_LENGTH (BINFO_BASETYPES (binfo));
+      for (i = 0; i != n; i++)
        {
-         rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
-         if (rval)
-           return rval;
+         tree base_binfo;
+      
+         if (qfn)
+           base_binfo = (*qfn) (binfo, i, data);
+         else
+           base_binfo = BINFO_BASETYPE (binfo, i);
+         
+         if (base_binfo)
+           {
+             rval = dfs_walk_real (base_binfo, prefn, postfn, qfn, data);
+             if (rval)
+               return rval;
+           }
        }
     }
 
@@ -1842,11 +1687,10 @@ dfs_walk_real (binfo, prefn, postfn, qfn, data)
    performed.  */
 
 tree
-dfs_walk (binfo, fn, qfn, data)
-     tree binfo;
-     tree (*fn) PARAMS ((tree, void *));
-     tree (*qfn) PARAMS ((tree, void *));
-     void *data;
+dfs_walk (tree binfo,
+         tree (*fn) (tree, void *),
+         tree (*qfn) (tree, int, void *),
+         void *data)
 {
   return dfs_walk_real (binfo, 0, fn, qfn, data);
 }
@@ -1855,8 +1699,7 @@ dfs_walk (binfo, fn, qfn, data)
    BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
 
 int
-check_final_overrider (overrider, basefn)
-     tree overrider, basefn;
+check_final_overrider (tree overrider, tree basefn)
 {
   tree over_type = TREE_TYPE (overrider);
   tree base_type = TREE_TYPE (basefn);
@@ -1865,14 +1708,17 @@ check_final_overrider (overrider, basefn)
   tree over_throw = TYPE_RAISES_EXCEPTIONS (over_type);
   tree base_throw = TYPE_RAISES_EXCEPTIONS (base_type);
   int fail = 0;
-  
+
+  if (DECL_INVALID_OVERRIDER_P (overrider))
+    return 0;
+
   if (same_type_p (base_return, over_return))
     /* OK */;
   else if ((CLASS_TYPE_P (over_return) && CLASS_TYPE_P (base_return))
           || (TREE_CODE (base_return) == TREE_CODE (over_return)
               && POINTER_TYPE_P (base_return)))
     {
-      /* Potentially covariant. */
+      /* Potentially covariant.  */
       unsigned base_quals, over_quals;
       
       fail = !POINTER_TYPE_P (base_return);
@@ -1903,10 +1749,8 @@ check_final_overrider (overrider, basefn)
           converting to void *, or qualification conversion.  */
        {
          /* can_convert will permit user defined conversion from a
-            (reference to) class type. We must reject them. */
-         over_return = TREE_TYPE (over_type);
-         if (TREE_CODE (over_return) == REFERENCE_TYPE)
-           over_return = TREE_TYPE (over_return);
+            (reference to) class type. We must reject them.  */
+         over_return = non_reference (TREE_TYPE (over_type));
          if (CLASS_TYPE_P (over_return))
            fail = 2;
        }
@@ -1917,8 +1761,6 @@ check_final_overrider (overrider, basefn)
     fail = 2;
   if (!fail)
     /* OK */;
-  else if (IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
-    return 0;
   else
     {
       if (fail == 1)
@@ -1932,21 +1774,16 @@ check_final_overrider (overrider, basefn)
                       overrider);
          cp_error_at ("  overriding `%#D'", basefn);
        }
-      SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
-                                  DECL_CONTEXT (overrider));
+      DECL_INVALID_OVERRIDER_P (overrider) = 1;
       return 0;
     }
   
   /* Check throw specifier is at least as strict.  */
   if (!comp_except_specs (base_throw, over_throw, 0))
     {
-      if (!IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider)))
-       {
-         cp_error_at ("looser throw specifier for `%#F'", overrider);
-         cp_error_at ("  overriding `%#F'", basefn);
-         SET_IDENTIFIER_ERROR_LOCUS (DECL_ASSEMBLER_NAME (overrider),
-                                     DECL_CONTEXT (overrider));
-       }
+      cp_error_at ("looser throw specifier for `%#F'", overrider);
+      cp_error_at ("  overriding `%#F'", basefn);
+      DECL_INVALID_OVERRIDER_P (overrider) = 1;
       return 0;
     }
   
@@ -1964,8 +1801,7 @@ check_final_overrider (overrider, basefn)
    overridden.  */
 
 int
-look_for_overrides (type, fndecl)
-     tree type, fndecl;
+look_for_overrides (tree type, tree fndecl)
 {
   tree binfo = TYPE_BINFO (type);
   tree basebinfos = BINFO_BASETYPES (binfo);
@@ -1987,8 +1823,7 @@ look_for_overrides (type, fndecl)
    FNDECL.  */
 
 tree
-look_for_overrides_here (type, fndecl)
-     tree type, fndecl;
+look_for_overrides_here (tree type, tree fndecl)
 {
   int ix;
 
@@ -2026,8 +1861,7 @@ look_for_overrides_here (type, fndecl)
    TYPE itself and its bases.  */
 
 static int
-look_for_overrides_r (type, fndecl)
-     tree type, fndecl;
+look_for_overrides_r (tree type, tree fndecl)
 {
   tree fn = look_for_overrides_here (type, fndecl);
   if (fn)
@@ -2052,71 +1886,10 @@ look_for_overrides_r (type, fndecl)
   return look_for_overrides (type, fndecl);
 }
 
-/* A queue function to use with dfs_walk that only walks into
-   canonical bases.  DATA should be the type of the complete object,
-   or a TREE_LIST whose TREE_PURPOSE is the type of the complete
-   object.  By using this function as a queue function, you will walk
-   over exactly those BINFOs that actually exist in the complete
-   object, including those for virtual base classes.  If you
-   SET_BINFO_MARKED for each binfo you process, you are further
-   guaranteed that you will walk into each virtual base class exactly
-   once.  */
-
-tree
-dfs_unmarked_real_bases_queue_p (binfo, data)
-     tree binfo;
-     void *data;
-{
-  if (TREE_VIA_VIRTUAL (binfo))
-    {
-      tree type = (tree) data;
-
-      if (TREE_CODE (type) == TREE_LIST)
-       type = TREE_PURPOSE (type);
-      binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
-    }
-  return unmarkedp (binfo, NULL);
-}
-
-/* Like dfs_unmarked_real_bases_queue_p but walks only into things
-   that are marked, rather than unmarked.  */
-
-tree
-dfs_marked_real_bases_queue_p (binfo, data)
-     tree binfo;
-     void *data;
-{
-  if (TREE_VIA_VIRTUAL (binfo))
-    {
-      tree type = (tree) data;
-
-      if (TREE_CODE (type) == TREE_LIST)
-       type = TREE_PURPOSE (type);
-      binfo = binfo_for_vbase (BINFO_TYPE (binfo), type);
-    }
-  return markedp (binfo, NULL);
-}
-
-/* A queue function that skips all virtual bases (and their 
-   bases).  */
-
-tree
-dfs_skip_vbases (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{
-  if (TREE_VIA_VIRTUAL (binfo))
-    return NULL_TREE;
-
-  return binfo;
-}
-
 /* Called via dfs_walk from dfs_get_pure_virtuals.  */
 
 static tree
-dfs_get_pure_virtuals (binfo, data)
-     tree binfo;
-     void *data;
+dfs_get_pure_virtuals (tree binfo, void *data)
 {
   tree type = (tree) data;
 
@@ -2136,7 +1909,7 @@ dfs_get_pure_virtuals (binfo, data)
                         CLASSTYPE_PURE_VIRTUALS (type));
     }
   
-  SET_BINFO_MARKED (binfo);
+  BINFO_MARKED (binfo) = 1;
 
   return NULL_TREE;
 }
@@ -2144,8 +1917,7 @@ dfs_get_pure_virtuals (binfo, data)
 /* Set CLASSTYPE_PURE_VIRTUALS for TYPE.  */
 
 void
-get_pure_virtuals (type)
-     tree type;
+get_pure_virtuals (tree type)
 {
   tree vbases;
 
@@ -2158,10 +1930,8 @@ get_pure_virtuals (type)
      (A primary base is not interesting because the derived class of
      which it is a primary base will contain vtable entries for the
      pure virtuals in the base class.  */
-  dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals, 
-           dfs_unmarked_real_bases_queue_p, type);
-  dfs_walk (TYPE_BINFO (type), dfs_unmark, 
-           dfs_marked_real_bases_queue_p, type);
+  dfs_walk (TYPE_BINFO (type), dfs_get_pure_virtuals, unmarkedp, type);
+  dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, type);
 
   /* Put the pure virtuals in dfs order.  */
   CLASSTYPE_PURE_VIRTUALS (type) = nreverse (CLASSTYPE_PURE_VIRTUALS (type));
@@ -2186,52 +1956,36 @@ get_pure_virtuals (type)
 /* DEPTH-FIRST SEARCH ROUTINES.  */
 
 tree 
-markedp (binfo, data
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{ 
+markedp (tree derived, int ix, void *data ATTRIBUTE_UNUSED
+{
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
   return BINFO_MARKED (binfo) ? binfo : NULL_TREE; 
 }
 
 tree
-unmarkedp (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+unmarkedp (tree derived, int ix, void *data ATTRIBUTE_UNUSED) 
 {
-  return !BINFO_MARKED (binfo) ? binfo : NULL_TREE;
-}
-
-tree
-marked_vtable_pathp (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{ 
-  return BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; 
-}
-
-tree
-unmarked_vtable_pathp (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{ 
-  return !BINFO_VTABLE_PATH_MARKED (binfo) ? binfo : NULL_TREE; 
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
+  return !BINFO_MARKED (binfo) ? binfo : NULL_TREE; 
 }
 
 static tree
-marked_pushdecls_p (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+marked_pushdecls_p (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
 {
-  return (CLASS_TYPE_P (BINFO_TYPE (binfo))
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
+  return (!BINFO_DEPENDENT_BASE_P (binfo)
          && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE; 
 }
 
 static tree
-unmarked_pushdecls_p (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+unmarked_pushdecls_p (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
 { 
-  return (CLASS_TYPE_P (BINFO_TYPE (binfo))
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
+  return (!BINFO_DEPENDENT_BASE_P (binfo)
          && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
 }
 
@@ -2240,111 +1994,12 @@ unmarked_pushdecls_p (binfo, data)
    a predicate function (above).  */
 
 tree
-dfs_unmark (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{ 
-  CLEAR_BINFO_MARKED (binfo); 
-  return NULL_TREE;
-}
-
-/* get virtual base class types.
-   This adds type to the vbase_types list in reverse dfs order.
-   Ordering is very important, so don't change it.  */
-
-static tree
-dfs_get_vbase_types (binfo, data)
-     tree binfo;
-     void *data;
+dfs_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
-  tree type = (tree) data;
-
-  if (TREE_VIA_VIRTUAL (binfo))
-    CLASSTYPE_VBASECLASSES (type)
-      = tree_cons (BINFO_TYPE (binfo), 
-                  binfo, 
-                  CLASSTYPE_VBASECLASSES (type));
-  SET_BINFO_MARKED (binfo);
+  BINFO_MARKED (binfo) = 0;
   return NULL_TREE;
 }
 
-/* Called via dfs_walk from mark_primary_bases.  Builds the
-   inheritance graph order list of BINFOs.  */
-
-static tree
-dfs_build_inheritance_graph_order (binfo, data)
-     tree binfo;
-     void *data;
-{
-  tree *last_binfo = (tree *) data;
-
-  if (*last_binfo)
-    TREE_CHAIN (*last_binfo) = binfo;
-  *last_binfo = binfo;
-  SET_BINFO_MARKED (binfo);
-  return NULL_TREE;
-}
-
-/* Set CLASSTYPE_VBASECLASSES for TYPE.  */
-
-void
-get_vbase_types (type)
-     tree type;
-{
-  tree last_binfo;
-
-  CLASSTYPE_VBASECLASSES (type) = NULL_TREE;
-  dfs_walk (TYPE_BINFO (type), dfs_get_vbase_types, unmarkedp, type);
-  /* Rely upon the reverse dfs ordering from dfs_get_vbase_types, and now
-     reverse it so that we get normal dfs ordering.  */
-  CLASSTYPE_VBASECLASSES (type) = nreverse (CLASSTYPE_VBASECLASSES (type));
-  dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, 0);
-  /* Thread the BINFOs in inheritance-graph order.  */
-  last_binfo = NULL;
-  dfs_walk_real (TYPE_BINFO (type),
-                dfs_build_inheritance_graph_order,
-                NULL,
-                unmarkedp,
-                &last_binfo);
-  dfs_walk (TYPE_BINFO (type), dfs_unmark, markedp, NULL);
-}
-
-/* Called from find_vbase_instance via dfs_walk.  */
-
-static tree
-dfs_find_vbase_instance (binfo, data)
-     tree binfo;
-     void *data;
-{
-  tree base = TREE_VALUE ((tree) data);
-
-  if (BINFO_PRIMARY_P (binfo)
-      && same_type_p (BINFO_TYPE (binfo), base))
-    return binfo;
-
-  return NULL_TREE;
-}
-
-/* Find the real occurrence of the virtual BASE (a class type) in the
-   hierarchy dominated by TYPE.  */
-
-tree
-find_vbase_instance (base, type)
-     tree base;
-     tree type;
-{
-  tree instance;
-
-  instance = binfo_for_vbase (base, type);
-  if (!BINFO_PRIMARY_P (instance))
-    return instance;
-
-  return dfs_walk (TYPE_BINFO (type), 
-                  dfs_find_vbase_instance, 
-                  NULL,
-                  build_tree_list (type, base));
-}
-
 \f
 /* Debug info for C++ classes can get very large; try to avoid
    emitting it everywhere.
@@ -2354,8 +2009,7 @@ find_vbase_instance (base, type)
    linker.  */
 
 void
-maybe_suppress_debug_info (t)
-     tree t;
+maybe_suppress_debug_info (tree t)
 {
   /* We can't do the usual TYPE_DECL_SUPPRESS_DEBUG thing with DWARF, which
      does not support name references between translation units.  It supports
@@ -2397,9 +2051,7 @@ maybe_suppress_debug_info (t)
    information anyway.  */
 
 static tree
-dfs_debug_mark (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
   tree t = BINFO_TYPE (binfo);
 
@@ -2412,10 +2064,10 @@ dfs_debug_mark (binfo, data)
    info for this base class.  */
 
 static tree 
-dfs_debug_unmarkedp (binfo, data) 
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
-{ 
+dfs_debug_unmarkedp (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
+{
+  tree binfo = BINFO_BASETYPE (derived, ix);
+  
   return (!CLASSTYPE_DEBUG_REQUESTED (BINFO_TYPE (binfo)) 
          ? binfo : NULL_TREE);
 }
@@ -2428,8 +2080,7 @@ dfs_debug_unmarkedp (binfo, data)
    the vtables themselves, were optimized away.  */
 
 void
-note_debug_info_needed (type)
-     tree type;
+note_debug_info_needed (tree type)
 {
   if (TYPE_DECL_SUPPRESS_DEBUG (TYPE_NAME (type)))
     {
@@ -2442,27 +2093,8 @@ note_debug_info_needed (type)
 \f
 /* Subroutines of push_class_decls ().  */
 
-/* Returns 1 iff BINFO is a base we shouldn't really be able to see into,
-   because it (or one of the intermediate bases) depends on template parms.  */
-
-static int
-dependent_base_p (binfo)
-     tree binfo;
-{
-  for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
-    {
-      if (currently_open_class (TREE_TYPE (binfo)))
-       break;
-      if (uses_template_parms (TREE_TYPE (binfo)))
-       return 1;
-    }
-  return 0;
-}
-
 static void
-setup_class_bindings (name, type_binding_p)
-     tree name;
-     int type_binding_p;
+setup_class_bindings (tree name, int type_binding_p)
 {
   tree type_binding = NULL_TREE;
   tree value_binding;
@@ -2476,8 +2108,7 @@ setup_class_bindings (name, type_binding_p)
   if (type_binding_p)
     {
       type_binding = lookup_member (current_class_type, name,
-                                   /*protect=*/2,
-                                   /*want_type=*/1);
+                                   /*protect=*/2, /*want_type=*/true);
       if (TREE_CODE (type_binding) == TREE_LIST 
          && TREE_TYPE (type_binding) == error_mark_node)
        /* NAME is ambiguous.  */
@@ -2488,8 +2119,7 @@ setup_class_bindings (name, type_binding_p)
 
   /* Now, do the value binding.  */
   value_binding = lookup_member (current_class_type, name,
-                                /*protect=*/2,
-                                /*want_type=*/0);
+                                /*protect=*/2, /*want_type=*/false);
 
   if (type_binding_p
       && (TREE_CODE (value_binding) == TYPE_DECL
@@ -2512,6 +2142,20 @@ setup_class_bindings (name, type_binding_p)
          if (BASELINK_P (value_binding))
            /* NAME is some overloaded functions.  */
            value_binding = BASELINK_FUNCTIONS (value_binding);
+         /* Two conversion operators that convert to the same type
+            may have different names.  (See
+            mangle_conv_op_name_for_type.)  To avoid recording the
+            same conversion operator declaration more than once we
+            must check to see that the same operator was not already
+            found under another name.  */
+         if (IDENTIFIER_TYPENAME_P (name)
+             && is_overloaded_fn (value_binding))
+           {
+             tree fns;
+             for (fns = value_binding; fns; fns = OVL_NEXT (fns))
+               if (IDENTIFIER_CLASS_VALUE (DECL_NAME (OVL_CURRENT (fns))))
+                 return;
+           }
          pushdecl_class_level (value_binding);
        }
     }
@@ -2521,9 +2165,7 @@ setup_class_bindings (name, type_binding_p)
    are TYPE_DECLS.  */
 
 static tree
-dfs_push_type_decls (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+dfs_push_type_decls (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
   tree type;
   tree fields;
@@ -2537,7 +2179,7 @@ dfs_push_type_decls (binfo, data)
 
   /* We can't just use BINFO_MARKED because envelope_add_decl uses
      DERIVED_FROM_P, which calls get_base_distance.  */
-  SET_BINFO_PUSHDECLS_MARKED (binfo);
+  BINFO_PUSHDECLS_MARKED (binfo) = 1;
 
   return NULL_TREE;
 }
@@ -2546,50 +2188,41 @@ dfs_push_type_decls (binfo, data)
    are not TYPE_DECLS.  */
 
 static tree
-dfs_push_decls (binfo, data)
-     tree binfo;
-     void *data;
+dfs_push_decls (tree binfo, void *data)
 {
-  tree type;
+  tree type = BINFO_TYPE (binfo);
   tree method_vec;
-  int dep_base_p;
-
-  type = BINFO_TYPE (binfo);
-  dep_base_p = (processing_template_decl && type != current_class_type
-               && dependent_base_p (binfo));
-  if (!dep_base_p)
+  tree fields;
+  
+  for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
+    if (DECL_NAME (fields) 
+       && TREE_CODE (fields) != TYPE_DECL
+       && TREE_CODE (fields) != USING_DECL
+       && !DECL_ARTIFICIAL (fields))
+      setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
+    else if (TREE_CODE (fields) == FIELD_DECL
+            && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
+      dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
+  
+  method_vec = (CLASS_TYPE_P (type) 
+               ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
+  
+  if (method_vec && TREE_VEC_LENGTH (method_vec) >= 3)
     {
-      tree fields;
-      for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
-       if (DECL_NAME (fields) 
-           && TREE_CODE (fields) != TYPE_DECL
-           && TREE_CODE (fields) != USING_DECL
-           && !DECL_ARTIFICIAL (fields))
-         setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/0);
-       else if (TREE_CODE (fields) == FIELD_DECL
-                && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
-         dfs_push_decls (TYPE_BINFO (TREE_TYPE (fields)), data);
-         
-      method_vec = (CLASS_TYPE_P (type) 
-                   ? CLASSTYPE_METHOD_VEC (type) : NULL_TREE);
-
-      if (method_vec && TREE_VEC_LENGTH (method_vec) >= 3)
-       {
-         tree *methods;
-         tree *end;
-
-         /* Farm out constructors and destructors.  */
-         end = TREE_VEC_END (method_vec);
-
-         for (methods = &TREE_VEC_ELT (method_vec, 2);
-              methods < end && *methods;
-              methods++)
-           setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)), 
-                                 /*type_binding_p=*/0);
-       }
+      tree *methods;
+      tree *end;
+      
+      /* Farm out constructors and destructors.  */
+      end = TREE_VEC_END (method_vec);
+      
+      for (methods = &TREE_VEC_ELT (method_vec, 2);
+          methods < end && *methods;
+          methods++)
+       setup_class_bindings (DECL_NAME (OVL_CURRENT (*methods)), 
+                             /*type_binding_p=*/0);
     }
 
-  CLEAR_BINFO_PUSHDECLS_MARKED (binfo);
+  BINFO_PUSHDECLS_MARKED (binfo) = 0;
 
   return NULL_TREE;
 }
@@ -2602,8 +2235,7 @@ dfs_push_decls (binfo, data)
    message.  */
 
 void
-push_class_decls (type)
-     tree type;
+push_class_decls (tree type)
 {
   search_stack = push_search_level (search_stack, &search_obstack);
 
@@ -2617,14 +2249,23 @@ push_class_decls (type)
 /* Here's a subroutine we need because C lacks lambdas.  */
 
 static tree
-dfs_unuse_fields (binfo, data)
-     tree binfo;
-     void *data ATTRIBUTE_UNUSED;
+dfs_unuse_fields (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
   tree type = TREE_TYPE (binfo);
   tree fields;
 
-  for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
+  if (TREE_CODE (type) == TYPENAME_TYPE)
+    fields = TYPENAME_TYPE_FULLNAME (type);
+  else if (TREE_CODE (type) == TYPEOF_TYPE)
+    fields = TYPEOF_TYPE_EXPR (type);
+  else if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
+          || TREE_CODE (type) == TEMPLATE_TEMPLATE_PARM
+          || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM)
+    fields = TEMPLATE_TYPE_PARM_INDEX (type);
+  else
+    fields = TYPE_FIELDS (type);
+
+  for (; fields; fields = TREE_CHAIN (fields))
     {
       if (TREE_CODE (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
        continue;
@@ -2639,14 +2280,13 @@ dfs_unuse_fields (binfo, data)
 }
 
 void
-unuse_fields (type)
-     tree type;
+unuse_fields (tree type)
 {
   dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
 }
 
 void
-pop_class_decls ()
+pop_class_decls (void)
 {
   /* We haven't pushed a search level when dealing with cached classes,
      so we'd better not try to pop it.  */
@@ -2655,7 +2295,7 @@ pop_class_decls ()
 }
 
 void
-print_search_statistics ()
+print_search_statistics (void)
 {
 #ifdef GATHER_STATISTICS
   fprintf (stderr, "%d fields searched in %d[%d] calls to lookup_field[_1]\n",
@@ -2669,13 +2309,13 @@ print_search_statistics ()
 }
 
 void
-init_search_processing ()
+init_search_processing (void)
 {
   gcc_obstack_init (&search_obstack);
 }
 
 void
-reinit_search_statistics ()
+reinit_search_statistics (void)
 {
 #ifdef GATHER_STATISTICS
   n_fields_searched = 0;
@@ -2688,9 +2328,7 @@ reinit_search_statistics ()
 }
 
 static tree
-add_conversions (binfo, data)
-     tree binfo;
-     void *data;
+add_conversions (tree binfo, void *data)
 {
   int i;
   tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
@@ -2713,8 +2351,27 @@ add_conversions (binfo, data)
       /* Make sure we don't already have this conversion.  */
       if (! IDENTIFIER_MARKED (name))
        {
-         *conversions = tree_cons (binfo, tmp, *conversions);
-         IDENTIFIER_MARKED (name) = 1;
+         tree t;
+
+         /* Make sure that we do not already have a conversion
+            operator for this type.  Merely checking the NAME is not
+            enough because two conversion operators to the same type
+            may not have the same NAME.  */
+         for (t = *conversions; t; t = TREE_CHAIN (t))
+           {
+             tree fn;
+             for (fn = TREE_VALUE (t); fn; fn = OVL_NEXT (fn))
+               if (same_type_p (TREE_TYPE (name),
+                                DECL_CONV_FN_TYPE (OVL_CURRENT (fn))))
+                 break;
+             if (fn)
+               break;
+           }
+         if (!t)
+           {
+             *conversions = tree_cons (binfo, tmp, *conversions);
+             IDENTIFIER_MARKED (name) = 1;
+           }
        }
     }
   return NULL_TREE;
@@ -2727,14 +2384,13 @@ add_conversions (binfo, data)
    from which the conversion functions in this node were selected.  */
 
 tree
-lookup_conversions (type)
-     tree type;
+lookup_conversions (tree type)
 {
   tree t;
   tree conversions = NULL_TREE;
 
-  if (COMPLETE_TYPE_P (type))
-    bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
+  complete_type (type);
+  bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
 
   for (t = conversions; t; t = TREE_CHAIN (t))
     IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
@@ -2752,9 +2408,7 @@ struct overlap_info
    at offset 0 in COMPARE_TYPE, and set found_overlap if so.  */
 
 static tree
-dfs_check_overlap (empty_binfo, data)
-     tree empty_binfo;
-     void *data;
+dfs_check_overlap (tree empty_binfo, void *data)
 {
   struct overlap_info *oi = (struct overlap_info *) data;
   tree binfo;
@@ -2777,11 +2431,11 @@ dfs_check_overlap (empty_binfo, data)
 /* Trivial function to stop base traversal when we find something.  */
 
 static tree
-dfs_no_overlap_yet (binfo, data)
-     tree binfo;
-     void *data;
+dfs_no_overlap_yet (tree derived, int ix, void *data)
 {
+  tree binfo = BINFO_BASETYPE (derived, ix);
   struct overlap_info *oi = (struct overlap_info *) data;
+  
   return !oi->found_overlap ? binfo : NULL_TREE;
 }
 
@@ -2789,8 +2443,7 @@ dfs_no_overlap_yet (binfo, data)
    offset 0 in NEXT_TYPE.  Used in laying out empty base class subobjects.  */
 
 int
-types_overlap_p (empty_type, next_type)
-     tree empty_type, next_type;
+types_overlap_p (tree empty_type, tree next_type)
 {
   struct overlap_info oi;
 
@@ -2809,8 +2462,7 @@ types_overlap_p (empty_type, next_type)
    FIXME: This does not work with the new ABI.  */
 
 tree
-binfo_for_vtable (var)
-     tree var;
+binfo_for_vtable (tree var)
 {
   tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
   tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
@@ -2836,8 +2488,7 @@ binfo_for_vtable (var)
    from BINFO, or NULL if binfo is not via virtual.  */
 
 tree
-binfo_from_vbase (binfo)
-     tree binfo;
+binfo_from_vbase (tree binfo)
 {
   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
     {
@@ -2852,9 +2503,7 @@ binfo_from_vbase (binfo)
    via virtual.  */
 
 tree
-binfo_via_virtual (binfo, limit)
-     tree binfo;
-     tree limit;
+binfo_via_virtual (tree binfo, tree limit)
 {
   for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
        binfo = BINFO_INHERITANCE_CHAIN (binfo))
@@ -2865,16 +2514,99 @@ binfo_via_virtual (binfo, limit)
   return NULL_TREE;
 }
 
-/* Returns the BINFO (if any) for the virtual baseclass T of the class
-   C from the CLASSTYPE_VBASECLASSES list.  */
+/* BINFO is a base binfo in the complete type BINFO_TYPE (HERE).
+   Find the equivalent binfo within whatever graph HERE is located.
+   This is the inverse of original_binfo.  */
 
 tree
-binfo_for_vbase (basetype, classtype)
-     tree basetype;
-     tree classtype;
+copied_binfo (tree binfo, tree here)
 {
-  tree binfo;
+  tree result = NULL_TREE;
+  
+  if (TREE_VIA_VIRTUAL (binfo))
+    {
+      tree t;
+
+      for (t = here; BINFO_INHERITANCE_CHAIN (t);
+          t = BINFO_INHERITANCE_CHAIN (t))
+       continue;
+      
+      result = purpose_member (BINFO_TYPE (binfo),
+                              CLASSTYPE_VBASECLASSES (BINFO_TYPE (t)));
+      result = TREE_VALUE (result);
+    }
+  else if (BINFO_INHERITANCE_CHAIN (binfo))
+    {
+      tree base_binfos;
+      int ix, n;
+      
+      base_binfos = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
+      base_binfos = BINFO_BASETYPES (base_binfos);
+      n = TREE_VEC_LENGTH (base_binfos);
+      for (ix = 0; ix != n; ix++)
+       {
+         tree base = TREE_VEC_ELT (base_binfos, ix);
+         
+         if (BINFO_TYPE (base) == BINFO_TYPE (binfo))
+           {
+             result = base;
+             break;
+           }
+       }
+    }
+  else
+    {
+      my_friendly_assert (BINFO_TYPE (here) == BINFO_TYPE (binfo), 20030202);
+      result = here;
+    }
 
-  binfo = purpose_member (basetype, CLASSTYPE_VBASECLASSES (classtype));
-  return binfo ? TREE_VALUE (binfo) : NULL_TREE;
+  my_friendly_assert (result, 20030202);
+  return result;
 }
+
+/* BINFO is some base binfo of HERE, within some other
+   hierarchy. Return the equivalent binfo, but in the hierarchy
+   dominated by HERE.  This is the inverse of copied_binfo.  If BINFO
+   is not a base binfo of HERE, returns NULL_TREE.  */
+
+tree
+original_binfo (tree binfo, tree here)
+{
+  tree result = NULL;
+  
+  if (BINFO_TYPE (binfo) == BINFO_TYPE (here))
+    result = here;
+  else if (TREE_VIA_VIRTUAL (binfo))
+    {
+      result = purpose_member (BINFO_TYPE (binfo),
+                              CLASSTYPE_VBASECLASSES (BINFO_TYPE (here)));
+      if (result)
+       result = TREE_VALUE (result);
+    }
+  else if (BINFO_INHERITANCE_CHAIN (binfo))
+    {
+      tree base_binfos;
+      
+      base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
+      if (base_binfos)
+       {
+         int ix, n;
+         
+         base_binfos = BINFO_BASETYPES (base_binfos);
+         n = TREE_VEC_LENGTH (base_binfos);
+         for (ix = 0; ix != n; ix++)
+           {
+             tree base = TREE_VEC_ELT (base_binfos, ix);
+             
+             if (BINFO_TYPE (base) == BINFO_TYPE (binfo))
+               {
+                 result = base;
+                 break;
+               }
+           }
+       }
+    }
+  
+  return result;
+}
+