OSDN Git Service

PR c++/22618
[pf3gnuchains/gcc-fork.git] / gcc / cp / search.c
index 5b66000..090510b 100644 (file)
@@ -1,7 +1,7 @@
 /* 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, 2003, 2004 Free Software Foundation, Inc.
+   1999, 2000, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
@@ -18,8 +18,8 @@ GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 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.  */
+the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+Boston, MA 02110-1301, USA.  */
 
 /* High-level class interface.  */
 
@@ -34,101 +34,37 @@ Boston, MA 02111-1307, USA.  */
 #include "rtl.h"
 #include "output.h"
 #include "toplev.h"
-#include "stack.h"
 
-/* Obstack used for remembering decision points of breadth-first.  */
-
-static struct obstack search_obstack;
-
-/* Methods for pushing and popping objects to and from obstacks.  */
-
-struct stack_level *
-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);
-  stack = (struct stack_level *) ((char*)obstack_next_free (obstack) - size);
-  obstack_finish (obstack);
-  stack->obstack = obstack;
-  stack->first = (tree *) obstack_base (obstack);
-  stack->limit = obstack_room (obstack) / sizeof (tree *);
-  return stack;
-}
-
-struct stack_level *
-pop_stack_level (struct stack_level *stack)
-{
-  struct stack_level *tem = stack;
-  struct obstack *obstack = tem->obstack;
-  stack = tem->prev;
-  obstack_free (obstack, tem);
-  return stack;
-}
-
-#define search_level stack_level
-static struct search_level *search_stack;
-
-struct vbase_info 
-{
-  /* The class dominating the hierarchy.  */
-  tree type;
-  /* A pointer to a complete object of the indicated TYPE.  */
-  tree decl_ptr;
-  tree inits;
-};
-
-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 int is_subobject_of_p (tree, tree);
+static tree dfs_lookup_base (tree, void *);
+static tree dfs_dcast_hint_pre (tree, void *);
+static tree dfs_dcast_hint_post (tree, 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 tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
+                            tree (*post_fn) (tree, void *), void *data);
+static void dfs_unmark_r (tree);
+static int check_hidden_convs (tree, int, int, tree, tree, tree);
+static tree split_conversions (tree, tree, tree, tree);
+static int lookup_conversions_r (tree, int, int,
+                                tree, tree, tree, tree, tree *, tree *);
 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_accessible_post (tree, void *);
+static tree dfs_walk_once_accessible_r (tree, bool, bool,
+                                       tree (*pre_fn) (tree, void *),
+                                       tree (*post_fn) (tree, void *),
+                                       void *data);
+static tree dfs_walk_once_accessible (tree, bool,
+                                     tree (*pre_fn) (tree, void *),
+                                     tree (*post_fn) (tree, void *),
+                                     void *data);
 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 (struct stack_level *stack, struct obstack *obstack)
-{
-  struct search_level tem;
-
-  tem.prev = stack;
-  return push_stack_level (obstack, (char *)&tem, sizeof (tem));
-}
-
-/* Discard a level of search allocation.  */
-
-static struct search_level *
-pop_search_level (struct stack_level *obstack)
-{
-  struct search_level *stack = pop_stack_level (obstack);
-
-  return stack;
-}
 \f
 /* Variables for gathering statistics.  */
 #ifdef GATHER_STATISTICS
@@ -141,108 +77,96 @@ static int n_contexts_saved;
 #endif /* GATHER_STATISTICS */
 
 \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.  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
-   discovered. *BINFO_PTR is initialized and a base_kind return value
-   indicates what kind of base was located.
-
-   Otherwise BINFO's bases are searched.  */
-
-static base_kind
-lookup_base_r (tree binfo, tree base, base_access access,
-              bool is_virtual,                 /* inside a virtual part */
-              tree *binfo_ptr)
+/* Data for lookup_base and its workers.  */
+
+struct lookup_base_data_s
+{
+  tree t;              /* type being searched.  */
+  tree base;           /* The base type we're looking for.  */
+  tree binfo;          /* Found binfo.  */
+  bool via_virtual;    /* Found via a virtual path.  */
+  bool ambiguous;      /* Found multiply ambiguous */
+  bool repeated_base;  /* Whether there are repeated bases in the
+                           hierarchy.  */
+  bool want_any;       /* Whether we want any matching binfo.  */
+};
+
+/* Worker function for lookup_base.  See if we've found the desired
+   base and update DATA_ (a pointer to LOOKUP_BASE_DATA_S).  */
+
+static tree
+dfs_lookup_base (tree binfo, void *data_)
 {
-  int i;
-  tree bases, accesses;
-  base_kind found = bk_not_base;
-  
-  if (same_type_p (BINFO_TYPE (binfo), base))
-    {
-      /* We have found a base. Check against what we have found
-         already.  */
-      found = bk_same_type;
-      if (is_virtual)
-       found = bk_via_virtual;
-      
-      if (!*binfo_ptr)
-       *binfo_ptr = binfo;
-      else if (binfo != *binfo_ptr)
+  struct lookup_base_data_s *data = data_;
+
+  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
+    {
+      if (!data->binfo)
        {
-         if (access != ba_any)
-           *binfo_ptr = NULL;
-         else if (!is_virtual)
-           /* Prefer a non-virtual base.  */
-           *binfo_ptr = binfo;
-         found = bk_ambig;
+         data->binfo = binfo;
+         data->via_virtual
+           = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
+
+         if (!data->repeated_base)
+           /* If there are no repeated bases, we can stop now.  */
+           return binfo;
+
+         if (data->want_any && !data->via_virtual)
+           /* If this is a non-virtual base, then we can't do
+              better.  */
+           return binfo;
+
+         return dfs_skip_bases;
        }
-      
-      return found;
-    }
-  
-  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);
-      base_kind bk;
-
-      bk = lookup_base_r (base_binfo, base,
-                         access,
-                         is_virtual || TREE_VIA_VIRTUAL (base_binfo),
-                         binfo_ptr);
-
-      switch (bk)
+      else
        {
-       case bk_ambig:
-         if (access != ba_any)
-           return bk;
-         found = bk;
-         break;
-         
-       case bk_same_type:
-         bk = bk_proper_base;
-         /* Fall through.  */
-       case bk_proper_base:
-         my_friendly_assert (found == bk_not_base, 20010723);
-         found = bk;
-         break;
-         
-       case bk_via_virtual:
-         if (found != bk_ambig)
-           found = bk;
-         break;
-         
-       case bk_not_base:
-         break;
-
-       default:
-         abort ();
+         gcc_assert (binfo != data->binfo);
+
+         /* We've found more than one matching binfo.  */
+         if (!data->want_any)
+           {
+             /* This is immediately ambiguous.  */
+             data->binfo = NULL_TREE;
+             data->ambiguous = true;
+             return error_mark_node;
+           }
+
+         /* Prefer one via a non-virtual path.  */
+         if (!binfo_via_virtual (binfo, data->t))
+           {
+             data->binfo = binfo;
+             data->via_virtual = false;
+             return binfo;
+           }
+
+         /* There must be repeated bases, otherwise we'd have stopped
+            on the first base we found.  */
+         return dfs_skip_bases;
        }
     }
-  return found;
+
+  return NULL_TREE;
 }
 
 /* Returns true if type BASE is accessible in T.  (BASE is known to be
-   a base class of T.)  */
+   a (possibly non-proper) base class of T.)  If CONSIDER_LOCAL_P is
+   true, consider any special access of the current scope, or access
+   bestowed by friendship.  */
 
 bool
-accessible_base_p (tree t, tree base)
+accessible_base_p (tree t, tree base, bool consider_local_p)
 {
   tree decl;
 
   /* [class.access.base]
 
      A base class is said to be accessible if an invented public
-     member of the base class is accessible.  */
+     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);
@@ -250,7 +174,7 @@ accessible_base_p (tree t, tree base)
     decl = TREE_CHAIN (decl);
   while (ANON_AGGR_TYPE_P (t))
     t = TYPE_CONTEXT (t);
-  return accessible_p (t, decl);
+  return accessible_p (t, decl, consider_local_p);
 }
 
 /* Lookup BASE in the hierarchy dominated by T.  Do access checking as
@@ -266,31 +190,59 @@ accessible_base_p (tree t, tree base)
 tree
 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;
+  tree binfo;
+  tree t_binfo;
   base_kind bk;
-  
+
   if (t == error_mark_node || base == error_mark_node)
     {
       if (kind_ptr)
        *kind_ptr = bk_not_base;
       return error_mark_node;
     }
-  my_friendly_assert (TYPE_P (base), 20011127);
-  
+  gcc_assert (TYPE_P (base));
+
   if (!TYPE_P (t))
     {
       t_binfo = t;
       t = BINFO_TYPE (t);
     }
-  else 
-    t_binfo = TYPE_BINFO (t);
+  else
+    {
+      t = complete_type (TYPE_MAIN_VARIANT (t));
+      t_binfo = TYPE_BINFO (t);
+    }
 
-  /* Ensure that the types are instantiated.  */
-  t = complete_type (TYPE_MAIN_VARIANT (t));
   base = complete_type (TYPE_MAIN_VARIANT (base));
-  
-  bk = lookup_base_r (t_binfo, base, access, 0, &binfo);
+
+  if (t_binfo)
+    {
+      struct lookup_base_data_s data;
+
+      data.t = t;
+      data.base = base;
+      data.binfo = NULL_TREE;
+      data.ambiguous = data.via_virtual = false;
+      data.repeated_base = CLASSTYPE_REPEATED_BASE_P (t);
+      data.want_any = access == ba_any;
+
+      dfs_walk_once (t_binfo, dfs_lookup_base, NULL, &data);
+      binfo = data.binfo;
+
+      if (!binfo)
+       bk = data.ambiguous ? bk_ambig : bk_not_base;
+      else if (binfo == t_binfo)
+       bk = bk_same_type;
+      else if (data.via_virtual)
+       bk = bk_via_virtual;
+      else
+       bk = bk_proper_base;
+    }
+  else
+    {
+      binfo = NULL_TREE;
+      bk = bk_not_base;
+    }
 
   /* Check that the base is unambiguous and accessible.  */
   if (access != ba_any)
@@ -300,16 +252,15 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
        break;
 
       case bk_ambig:
-       binfo = NULL_TREE;
        if (!(access & ba_quiet))
          {
-           error ("`%T' is an ambiguous base of `%T'", base, t);
+           error ("%qT is an ambiguous base of %qT", base, t);
            binfo = error_mark_node;
          }
        break;
 
       default:
-       if ((access & ~ba_quiet) != ba_ignore
+       if ((access & ba_check_bit)
            /* 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,
@@ -317,11 +268,11 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
               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))
+           && !accessible_base_p (t, base, !(access & ba_ignore_scope)))
          {
            if (!(access & ba_quiet))
              {
-               error ("`%T' is an inaccessible base of `%T'", base, t);
+               error ("%qT is an inaccessible base of %qT", base, t);
                binfo = error_mark_node;
              }
            else
@@ -333,55 +284,62 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
 
   if (kind_ptr)
     *kind_ptr = bk;
-  
+
   return binfo;
 }
 
-/* Worker function for get_dynamic_cast_base_type.  */
+/* Data for dcast_base_hint walker.  */
 
-static int
-dynamic_cast_base_recurse (tree subtype, tree binfo, bool is_via_virtual,
-                          tree *offset_ptr)
+struct dcast_data_s
+{
+  tree subtype;   /* The base type we're looking for.  */
+  int virt_depth; /* Number of virtual bases encountered from most
+                    derived.  */
+  tree offset;    /* Best hint offset discovered so far.  */
+  bool repeated_base;  /* Whether there are repeated bases in the
+                         hierarchy.  */
+};
+
+/* Worker for dcast_base_hint.  Search for the base type being cast
+   from.  */
+
+static tree
+dfs_dcast_hint_pre (tree binfo, void *data_)
 {
-  tree binfos, accesses;
-  int i, n_baselinks;
-  int worst = -2;
-  
-  if (BINFO_TYPE (binfo) == subtype)
-    {
-      if (is_via_virtual)
-        return -1;
+  struct dcast_data_s *data = data_;
+
+  if (BINFO_VIRTUAL_P (binfo))
+    data->virt_depth++;
+
+  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
+    {
+      if (data->virt_depth)
+       {
+         data->offset = ssize_int (-1);
+         return data->offset;
+       }
+      if (data->offset)
+       data->offset = ssize_int (-3);
       else
-        {
-          *offset_ptr = BINFO_OFFSET (binfo);
-          return 0;
-        }
-    }
-  
-  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 (base_access != access_public_node)
-        continue;
-      rval = dynamic_cast_base_recurse
-             (subtype, base_binfo,
-              is_via_virtual || TREE_VIA_VIRTUAL (base_binfo), offset_ptr);
-      if (worst == -2)
-        worst = rval;
-      else if (rval >= 0)
-        worst = worst >= 0 ? -3 : worst;
-      else if (rval == -1)
-        worst = -1;
-      else if (rval == -3 && worst != -1)
-        worst = -3;
-    }
-  return worst;
+       data->offset = BINFO_OFFSET (binfo);
+
+      return data->repeated_base ? dfs_skip_bases : data->offset;
+    }
+
+  return NULL_TREE;
+}
+
+/* Worker for dcast_base_hint.  Track the virtual depth.  */
+
+static tree
+dfs_dcast_hint_post (tree binfo, void *data_)
+{
+  struct dcast_data_s *data = data_;
+
+  if (BINFO_VIRTUAL_P (binfo))
+    data->virt_depth--;
+
+  return NULL_TREE;
 }
 
 /* The dynamic cast runtime needs a hint about how the static SUBTYPE type
@@ -396,17 +354,18 @@ dynamic_cast_base_recurse (tree subtype, tree binfo, bool is_via_virtual,
    BOFF == -3, SUBTYPE occurs as multiple public non-virtual bases.  */
 
 tree
-get_dynamic_cast_base_type (tree subtype, tree target)
+dcast_base_hint (tree subtype, tree target)
 {
-  tree offset = NULL_TREE;
-  int boff = dynamic_cast_base_recurse (subtype, TYPE_BINFO (target),
-                                        false, &offset);
-  
-  if (!boff)
-    return offset;
-  offset = build_int_2 (boff, -1);
-  TREE_TYPE (offset) = ssizetype;
-  return offset;
+  struct dcast_data_s data;
+
+  data.subtype = subtype;
+  data.virt_depth = 0;
+  data.offset = NULL_TREE;
+  data.repeated_base = CLASSTYPE_REPEATED_BASE_P (target);
+
+  dfs_walk_once_accessible (TYPE_BINFO (target), /*friends=*/false,
+                           dfs_dcast_hint_pre, dfs_dcast_hint_post, &data);
+  return data.offset ? data.offset : ssize_int (-2);
 }
 
 /* Search for a member with name NAME in a multiple inheritance
@@ -427,7 +386,7 @@ lookup_field_1 (tree type, tree name, bool want_type)
   if (TREE_CODE (type) == TEMPLATE_TYPE_PARM
       || TREE_CODE (type) == BOUND_TEMPLATE_TEMPLATE_PARM
       || TREE_CODE (type) == TYPENAME_TYPE)
-    /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and 
+    /* The TYPE_FIELDS of a TEMPLATE_TYPE_PARM and
        BOUND_TEMPLATE_TEMPLATE_PARM are not fields at all;
        instead TYPE_FIELDS is the TEMPLATE_PARM_INDEX.  (Miraculously,
        the code often worked even when we treated the index as a list
@@ -494,7 +453,7 @@ lookup_field_1 (tree type, tree name, bool want_type)
 #ifdef GATHER_STATISTICS
       n_fields_searched++;
 #endif /* GATHER_STATISTICS */
-      my_friendly_assert (DECL_P (field), 0);
+      gcc_assert (DECL_P (field));
       if (DECL_NAME (field) == NULL_TREE
          && ANON_AGGR_TYPE_P (TREE_TYPE (field)))
        {
@@ -503,15 +462,22 @@ lookup_field_1 (tree type, tree name, bool want_type)
            return temp;
        }
       if (TREE_CODE (field) == USING_DECL)
-       /* For now, we're just treating member using declarations as
-          old ARM-style access declarations.  Thus, there's no reason
-          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.  */
-       continue;
+       {
+         /* We generally treat class-scope using-declarations as
+            ARM-style access specifications, because support for the
+            ISO semantics has not been implemented.  So, in general,
+            there's no reason to return a USING_DECL, and the rest of
+            the compiler cannot handle that.  Once the class is
+            defined, USING_DECLs are purged from TYPE_FIELDS; see
+            handle_using_decl.  However, we make special efforts to
+            make using-declarations in class templates and class
+            template partial specializations work correctly.  */
+         if (!DECL_DEPENDENT_P (field))
+           continue;
+       }
 
       if (DECL_NAME (field) == name
-         && (!want_type 
+         && (!want_type
              || TREE_CODE (field) == TYPE_DECL
              || DECL_CLASS_TEMPLATE_P (field)))
        return field;
@@ -526,7 +492,13 @@ lookup_field_1 (tree type, tree name, bool want_type)
   return NULL_TREE;
 }
 
-/* There are a number of cases we need to be aware of here:
+/* Return the FUNCTION_DECL, RECORD_TYPE, UNION_TYPE, or
+   NAMESPACE_DECL corresponding to the innermost non-block scope.  */
+
+tree
+current_scope (void)
+{
+  /* There are a number of cases we need to be aware of here:
                         current_class_type     current_function_decl
      global                    NULL                    NULL
      fn-local                  NULL                    SET
@@ -534,30 +506,26 @@ lookup_field_1 (tree type, tree name, bool want_type)
      class->fn                 SET                     SET
      fn->class                 SET                     SET
 
-   Those last two make life interesting.  If we're in a function which is
-   itself inside a class, we need decls to go into the fn's decls (our
-   second case below).  But if we're in a class and the class itself is
-   inside a function, we need decls to go into the decls for the class.  To
-   achieve this last goal, we must see if, when both current_class_ptr and
-   current_function_decl are set, the class was declared inside that
-   function.  If so, we know to put the decls into the class's scope.  */
-
-tree
-current_scope (void)
-{
-  if (current_function_decl == NULL_TREE)
-    return current_class_type;
-  if (current_class_type == NULL_TREE)
+     Those last two make life interesting.  If we're in a function which is
+     itself inside a class, we need decls to go into the fn's decls (our
+     second case below).  But if we're in a class and the class itself is
+     inside a function, we need decls to go into the decls for the class.  To
+     achieve this last goal, we must see if, when both current_class_ptr and
+     current_function_decl are set, the class was declared inside that
+     function.  If so, we know to put the decls into the class's scope.  */
+  if (current_function_decl && current_class_type
+      && ((DECL_FUNCTION_MEMBER_P (current_function_decl)
+          && same_type_p (DECL_CONTEXT (current_function_decl),
+                          current_class_type))
+         || (DECL_FRIEND_CONTEXT (current_function_decl)
+             && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
+                             current_class_type))))
     return current_function_decl;
-  if ((DECL_FUNCTION_MEMBER_P (current_function_decl)
-       && same_type_p (DECL_CONTEXT (current_function_decl),
-                      current_class_type))
-      || (DECL_FRIEND_CONTEXT (current_function_decl)
-         && same_type_p (DECL_FRIEND_CONTEXT (current_function_decl),
-                         current_class_type)))
+  if (current_class_type)
+    return current_class_type;
+  if (current_function_decl)
     return current_function_decl;
-
-  return current_class_type;
+  return current_namespace;
 }
 
 /* Returns nonzero if we are currently in a function scope.  Note
@@ -585,9 +553,8 @@ at_class_scope_p (void)
 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();
+  tree cs = current_scope ();
+  return cs && TREE_CODE (cs) == NAMESPACE_DECL;
 }
 
 /* Return the scope of DECL, as appropriate when doing name-lookup.  */
@@ -596,11 +563,11 @@ tree
 context_for_name_lookup (tree decl)
 {
   /* [class.union]
-     
+
      For the purposes of name lookup, after the anonymous union
      definition, the members of the anonymous union are considered to
      have been defined in the scope in which the anonymous union is
-     declared.  */ 
+     declared.  */
   tree context = DECL_CONTEXT (decl);
 
   while (context && TYPE_P (context) && ANON_AGGR_TYPE_P (context))
@@ -644,7 +611,7 @@ dfs_access_in_type (tree binfo, void *data)
       else
        access = ak_public;
     }
-  else 
+  else
     {
       /* First, check for an access-declaration that gives us more
         access to the DECL.  The CONST_DECL for an enumeration
@@ -653,11 +620,11 @@ dfs_access_in_type (tree binfo, void *data)
       if (DECL_LANG_SPECIFIC (decl) && !DECL_DISCRIMINATOR_P (decl))
        {
          tree decl_access = purpose_member (type, DECL_ACCESS (decl));
-         
+
          if (decl_access)
            {
              decl_access = TREE_VALUE (decl_access);
-             
+
              if (decl_access == access_public_node)
                access = ak_public;
              else if (decl_access == access_protected_node)
@@ -665,25 +632,22 @@ dfs_access_in_type (tree binfo, void *data)
              else if (decl_access == access_private_node)
                access = ak_private;
              else
-               my_friendly_assert (false, 20030217);
+               gcc_unreachable ();
            }
        }
 
       if (!access)
        {
          int i;
-         int n_baselinks;
-         tree binfos, accesses;
-         
+         tree base_binfo;
+         VEC(tree,gc) *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)
+         accesses = BINFO_BASE_ACCESSES (binfo);
+         for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
            {
-             tree base_binfo = TREE_VEC_ELT (binfos, i);
-             tree base_access = TREE_VEC_ELT (accesses, i);
+             tree base_access = VEC_index (tree, accesses, i);
              access_kind base_access_now = BINFO_ACCESS (base_binfo);
 
              if (base_access_now == ak_none || base_access_now == ak_private)
@@ -718,10 +682,6 @@ dfs_access_in_type (tree binfo, void *data)
   /* Note the access to DECL in TYPE.  */
   SET_BINFO_ACCESS (binfo, access);
 
-  /* Mark TYPE as visited so that if we reach it again we do not
-     duplicate our efforts here.  */
-  BINFO_MARKED (binfo) = 1;
-
   return NULL_TREE;
 }
 
@@ -738,52 +698,16 @@ access_in_type (tree type, tree decl)
 
        If a name can be reached by several paths through a multiple
        inheritance graph, the access is that of the path that gives
-       most access.  
+       most access.
 
     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, unmarkedp, decl);
-  dfs_walk (binfo, dfs_unmark, markedp,  0);
+  dfs_walk_once (binfo, NULL, dfs_access_in_type, decl);
 
   return BINFO_ACCESS (binfo);
 }
 
-/* Called from accessible_p via dfs_walk.  */
-
-static tree
-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 derived class.  */
-  if (BINFO_BASEACCESS (derived, ix) != access_public_node
-      && !is_friend (BINFO_TYPE (derived), current_scope ()))
-    return NULL_TREE;
-
-  return binfo;
-}
-
-/* Called from accessible_p via dfs_walk.  */
-
-static tree
-dfs_accessible_p (tree binfo, void *data ATTRIBUTE_UNUSED)
-{
-  access_kind access;
-
-  BINFO_MARKED (binfo) = 1;
-  access = BINFO_ACCESS (binfo);
-  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
    indicated by BINFO in the context of DERIVED.  */
 
@@ -797,7 +721,7 @@ protected_accessible_p (tree decl, tree derived, tree binfo)
        m as a member of N is protected, and the reference occurs in a
        member or friend of class N, or in a member or friend of a
        class P derived from N, where m as a member of P is private or
-       protected.  
+       protected.
 
     Here DERIVED is a possible P and DECL is m.  accessible_p will
     iterate over various values of N, but the access to m in DERIVED
@@ -817,7 +741,7 @@ protected_accessible_p (tree decl, tree derived, tree binfo)
   /* If m is inaccessible in DERIVED, then it's not a P.  */
   if (access == ak_none)
     return 0;
-  
+
   /* [class.protected]
 
      When a friend or a member function of a derived class references
@@ -836,7 +760,7 @@ protected_accessible_p (tree decl, tree derived, tree binfo)
       tree t = binfo;
       while (BINFO_INHERITANCE_CHAIN (t))
        t = BINFO_INHERITANCE_CHAIN (t);
-      
+
       if (!DERIVED_FROM_P (derived, BINFO_TYPE (t)))
        return 0;
     }
@@ -878,33 +802,56 @@ friend_accessible_p (tree scope, tree decl, tree binfo)
   if (TREE_CODE (scope) == FUNCTION_DECL
       || DECL_FUNCTION_TEMPLATE_P (scope))
     {
-      /* Perhaps this SCOPE is a member of a class which is a 
-        friend.  */ 
-      if (DECL_CLASS_SCOPE_P (decl)
+      /* Perhaps this SCOPE is a member of a class which is a
+        friend.  */
+      if (DECL_CLASS_SCOPE_P (scope)
          && friend_accessible_p (DECL_CONTEXT (scope), decl, binfo))
        return 1;
 
       /* Or an instantiation of something which is a friend.  */
       if (DECL_TEMPLATE_INFO (scope))
-       return friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
+       {
+         int ret;
+         /* Increment processing_template_decl to make sure that
+            dependent_type_p works correctly.  */
+         ++processing_template_decl;
+         ret = friend_accessible_p (DECL_TI_TEMPLATE (scope), decl, binfo);
+         --processing_template_decl;
+         return ret;
+       }
     }
-  else if (CLASSTYPE_TEMPLATE_INFO (scope))
-    return friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
 
   return 0;
 }
 
+/* Called via dfs_walk_once_accessible from accessible_p */
+
+static tree
+dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
+{
+  if (BINFO_ACCESS (binfo) != ak_none)
+    {
+      tree scope = current_scope ();
+      if (scope && TREE_CODE (scope) != NAMESPACE_DECL
+         && is_friend (BINFO_TYPE (binfo), scope))
+       return binfo;
+    }
+
+  return NULL_TREE;
+}
+
 /* 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,
    then we can tell in what context the access is occurring by looking
-   at the most derived class along the path indicated by BINFO.  */
+   at the most derived class along the path indicated by BINFO.  If
+   CONSIDER_LOCAL is true, do consider special access the current
+   scope or friendship thereof we might have.  */
 
-int 
-accessible_p (tree type, tree decl)
+int
+accessible_p (tree type, tree decl, bool consider_local_p)
 {
   tree binfo;
-  tree t;
   tree scope;
   access_kind access;
 
@@ -925,8 +872,12 @@ accessible_p (tree type, tree decl)
   /* 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)
+     instantiation.  However, PROCESSING_TEMPLATE_DECL is set in the
+     parameter list for a template (because we may see dependent types
+     in default arguments for template parameters), and access
+     checking should be performed in the outermost parameter list.  */ 
+  if (processing_template_decl 
+      && (!processing_template_parmlist || processing_template_decl > 1))
     return 1;
 
   if (!TYPE_P (type))
@@ -952,19 +903,23 @@ accessible_p (tree type, tree decl)
        protected, or
 
      --there exists a base class B of N that is accessible at the point
-       of reference, and m is accessible when named in class B.  
+       of reference, and m is accessible when named in class B.
 
     We walk the base class hierarchy, checking these conditions.  */
 
-  /* Figure out where the reference is occurring.  Check to see if
-     DECL is private or protected in this scope, since that will
-     determine whether protected access is allowed.  */
-  if (current_class_type)
-    protected_ok = protected_accessible_p (decl, current_class_type, binfo);
-
-  /* Now, loop through the classes of which we are a friend.  */
-  if (!protected_ok)
-    protected_ok = friend_accessible_p (scope, decl, binfo);
+  if (consider_local_p)
+    {
+      /* Figure out where the reference is occurring.  Check to see if
+        DECL is private or protected in this scope, since that will
+        determine whether protected access is allowed.  */
+      if (current_class_type)
+       protected_ok = protected_accessible_p (decl,
+                                              current_class_type, binfo);
+
+      /* Now, loop through the classes of which we are a friend.  */
+      if (!protected_ok)
+       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.  */
@@ -976,18 +931,15 @@ accessible_p (tree type, tree decl)
   if (access == ak_public
       || (access == ak_protected && protected_ok))
     return 1;
-  else
-    {
-      /* 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;
-    }
+  if (!consider_local_p)
+    return 0;
+
+  /* Walk the hierarchy again, looking for a base class that allows
+     access.  */
+  return dfs_walk_once_accessible (binfo, /*friends=*/true,
+                                  NULL, dfs_accessible_post, NULL)
+    != NULL_TREE;
 }
 
 struct lookup_field_info {
@@ -1008,37 +960,10 @@ struct lookup_field_info {
   const char *errstr;
 };
 
-/* Returns nonzero if BINFO is not hidden by the value found by the
-   lookup so far.  If BINFO is hidden, then there's no need to look in
-   it.  DATA is really a struct lookup_field_info.  Called from
-   lookup_field via breadth_first_search.  */
-
-static tree
-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.  */
-  if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
-    return NULL_TREE;
-
-  /* If this base class is hidden by the best-known value so far, we
-     don't need to look.  */
-  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;
-}
-
 /* Within the scope of a template class, you can refer to the to the
    current specialization with the name of the template itself.  For
    example:
-   
+
      template <typename T> struct S { S* sp; }
 
    Returns nonzero if DECL is such a declaration in a class TYPE.  */
@@ -1053,7 +978,6 @@ template_self_reference_p (tree type, tree decl)
           && DECL_NAME (decl) == constructor_name (type));
 }
 
-
 /* Nonzero for a class member means that it is shared between all objects
    of that class.
 
@@ -1064,7 +988,7 @@ template_self_reference_p (tree type, tree decl)
 
    This function checks that T contains no nonstatic members.  */
 
-static int
+int
 shared_member_p (tree t)
 {
   if (TREE_CODE (t) == VAR_DECL || TREE_CODE (t) == TYPE_DECL \
@@ -1083,6 +1007,26 @@ shared_member_p (tree t)
   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.  */
+
+static int
+is_subobject_of_p (tree parent, tree binfo)
+{
+  tree probe;
+
+  for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
+    {
+      if (probe == binfo)
+       return 1;
+      if (BINFO_VIRTUAL_P (probe))
+       return (binfo_for_vbase (BINFO_TYPE (probe), BINFO_TYPE (binfo))
+               != NULL_TREE);
+    }
+  return 0;
+}
+
 /* DATA is really a struct lookup_field_info.  Look for a field with
    the name indicated there in BINFO.  If this function returns a
    non-NULL value it is the result of the lookup.  Called from
@@ -1095,6 +1039,16 @@ lookup_field_r (tree binfo, void *data)
   tree type = BINFO_TYPE (binfo);
   tree nval = NULL_TREE;
 
+  /* If this is a dependent base, don't look in it.  */
+  if (BINFO_DEPENDENT_BASE_P (binfo))
+    return NULL_TREE;
+
+  /* If this base class is hidden by the best-known value so far, we
+     don't need to look.  */
+  if (lfi->rval_binfo && BINFO_INHERITANCE_CHAIN (binfo) == lfi->rval_binfo
+      && !BINFO_VIRTUAL_P (binfo))
+    return dfs_skip_bases;
+
   /* 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
      with the same name, the type is hidden by the function.  */
@@ -1102,7 +1056,7 @@ lookup_field_r (tree binfo, void *data)
     {
       int idx = lookup_fnfields_1 (type, lfi->name);
       if (idx >= 0)
-       nval = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), idx);
+       nval = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), idx);
     }
 
   if (!nval)
@@ -1112,7 +1066,7 @@ lookup_field_r (tree binfo, void *data)
   /* If there is no declaration with the indicated name in this type,
      then there's nothing to do.  */
   if (!nval)
-    return NULL_TREE;
+    goto done;
 
   /* If we're looking up a type (as with an elaborated type specifier)
      we ignore all non-types we find.  */
@@ -1134,28 +1088,30 @@ lookup_field_r (tree binfo, void *data)
        nval = NULL_TREE;
       if (!nval && CLASSTYPE_NESTED_UTDS (type) != NULL)
        {
-          binding_entry e = binding_table_find (CLASSTYPE_NESTED_UTDS (type),
-                                                lfi->name);
+         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;
+         else
+           goto done;
        }
     }
 
   /* You must name a template base class with a template-id.  */
-  if (!same_type_p (type, lfi->type) 
+  if (!same_type_p (type, lfi->type)
       && template_self_reference_p (type, nval))
-    return NULL_TREE;
+    goto done;
 
   /* 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 && !original_binfo (lfi->rval_binfo, binfo))
+  if (lfi->rval_binfo
+      && !is_subobject_of_p (lfi->rval_binfo, binfo))
+
     {
       if (nval == lfi->rval && shared_member_p (nval))
        /* The two things are really the same.  */
        ;
-      else if (original_binfo (binfo, lfi->rval_binfo))
+      else if (is_subobject_of_p (binfo, lfi->rval_binfo))
        /* The previous value hides the new one.  */
        ;
       else
@@ -1174,7 +1130,7 @@ lookup_field_r (tree binfo, void *data)
          /* Add the new value.  */
          lfi->ambiguous = tree_cons (NULL_TREE, nval, lfi->ambiguous);
          TREE_TYPE (lfi->ambiguous) = error_mark_node;
-         lfi->errstr = "request for member `%D' is ambiguous";
+         lfi->errstr = "request for member %qD is ambiguous";
        }
     }
   else
@@ -1183,10 +1139,14 @@ lookup_field_r (tree binfo, void *data)
       lfi->rval_binfo = binfo;
     }
 
+ done:
+  /* Don't look for constructors or destructors in base classes.  */
+  if (IDENTIFIER_CTOR_OR_DTOR_P (lfi->name))
+    return dfs_skip_bases;
   return NULL_TREE;
 }
 
-/* Return a "baselink" which BASELINK_BINFO, BASELINK_ACCESS_BINFO,
+/* Return a "baselink" with BASELINK_BINFO, BASELINK_ACCESS_BINFO,
    BASELINK_FUNCTIONS, and BASELINK_OPTYPE set to BINFO, ACCESS_BINFO,
    FUNCTIONS, and OPTYPE respectively.  */
 
@@ -1195,13 +1155,12 @@ build_baselink (tree binfo, tree access_binfo, tree functions, tree optype)
 {
   tree baselink;
 
-  my_friendly_assert (TREE_CODE (functions) == FUNCTION_DECL
-                     || TREE_CODE (functions) == TEMPLATE_DECL
-                     || TREE_CODE (functions) == TEMPLATE_ID_EXPR
-                     || TREE_CODE (functions) == OVERLOAD,
-                     20020730);
-  my_friendly_assert (!optype || TYPE_P (optype), 20020730);
-  my_friendly_assert (TREE_TYPE (functions), 20020805);
+  gcc_assert (TREE_CODE (functions) == FUNCTION_DECL
+             || TREE_CODE (functions) == TEMPLATE_DECL
+             || TREE_CODE (functions) == TEMPLATE_ID_EXPR
+             || TREE_CODE (functions) == OVERLOAD);
+  gcc_assert (!optype || TYPE_P (optype));
+  gcc_assert (TREE_TYPE (functions));
 
   baselink = make_node (BASELINK);
   TREE_TYPE (baselink) = TREE_TYPE (functions);
@@ -1241,33 +1200,26 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
 
   const char *errstr = 0;
 
-  my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE, 20030624);
+  gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
 
-  if (TREE_CODE (xbasetype) == TREE_VEC)
+  if (TREE_CODE (xbasetype) == TREE_BINFO)
     {
       type = BINFO_TYPE (xbasetype);
       basetype_path = xbasetype;
     }
   else
     {
-      my_friendly_assert (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)), 20030624);
+      gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)));
       type = xbasetype;
-      basetype_path = TYPE_BINFO (type);
-      my_friendly_assert (!BINFO_INHERITANCE_CHAIN (basetype_path), 980827);
+      xbasetype = NULL_TREE;
     }
 
-  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;
-    }
+  type = complete_type (type);
+  if (!basetype_path)
+    basetype_path = TYPE_BINFO (type);
 
-  complete_type (type);
+  if (!basetype_path)
+    return NULL_TREE;
 
 #ifdef GATHER_STATISTICS
   n_calls_lookup_field++;
@@ -1277,7 +1229,7 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
   lfi.type = type;
   lfi.name = name;
   lfi.want_type = want_type;
-  bfs_walk (basetype_path, &lookup_field_r, &lookup_field_queue_p, &lfi);
+  dfs_walk_all (basetype_path, &lookup_field_r, NULL, &lfi);
   rval = lfi.rval;
   rval_binfo = lfi.rval_binfo;
   if (rval_binfo)
@@ -1288,8 +1240,8 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
      just return NULL_TREE.  */
   if (!protect && lfi.ambiguous)
     return NULL_TREE;
-  
-  if (protect == 2) 
+
+  if (protect == 2)
     {
       if (lfi.ambiguous)
        return lfi.ambiguous;
@@ -1308,11 +1260,11 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
     {
       error (errstr, name, type);
       if (lfi.ambiguous)
-        print_candidates (lfi.ambiguous);
+       print_candidates (lfi.ambiguous);
       rval = error_mark_node;
     }
 
-  if (rval && is_overloaded_fn (rval)) 
+  if (rval && is_overloaded_fn (rval))
     rval = build_baselink (rval_binfo, basetype_path, rval,
                           (IDENTIFIER_TYPENAME_P (name)
                           ? TREE_TYPE (name): NULL_TREE));
@@ -1326,9 +1278,10 @@ tree
 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
 {
   tree rval = lookup_member (xbasetype, name, protect, want_type);
-  
-  /* Ignore functions.  */
-  if (rval && BASELINK_P (rval))
+
+  /* Ignore functions, but propagate the ambiguity list.  */
+  if (!error_operand_p (rval)
+      && (rval && BASELINK_P (rval)))
     return NULL_TREE;
 
   return rval;
@@ -1342,8 +1295,9 @@ lookup_fnfields (tree xbasetype, tree name, int protect)
 {
   tree rval = lookup_member (xbasetype, name, protect, /*want_type=*/false);
 
-  /* Ignore non-functions.  */
-  if (rval && !BASELINK_P (rval))
+  /* Ignore non-functions, but propagate the ambiguity list.  */
+  if (!error_operand_p (rval)
+      && (rval && !BASELINK_P (rval)))
     return NULL_TREE;
 
   return rval;
@@ -1357,52 +1311,35 @@ lookup_fnfields (tree xbasetype, tree name, int protect)
 static int
 lookup_conversion_operator (tree class_type, tree type)
 {
-  int pass;
-  int i;
+  int tpl_slot = -1;
 
-  tree methods = CLASSTYPE_METHOD_VEC (class_type);
+  if (TYPE_HAS_CONVERSION (class_type))
+    {
+      int i;
+      tree fn;
+      VEC(tree,gc) *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;
-         }
-      }
+      for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
+          VEC_iterate (tree, methods, i, fn); ++i)
+       {
+         /* 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 (TREE_CODE (fn) == TEMPLATE_DECL)
+           /* All the templated conversion functions are on the same
+              slot, so remember it.  */
+           tpl_slot = i;
+         else if (same_type_p (DECL_CONV_FN_TYPE (fn), type))
+           return i;
+       }
+    }
 
-  return -1;
+  return tpl_slot;
 }
 
 /* TYPE is a class type. Return the index of the fields within
@@ -1411,49 +1348,74 @@ lookup_conversion_operator (tree class_type, tree type)
 int
 lookup_fnfields_1 (tree type, tree name)
 {
-  tree method_vec;
-  tree *methods;
+  VEC(tree,gc) *method_vec;
+  tree fn;
   tree tmp;
-  int i;
-  int len;
+  size_t i;
 
   if (!CLASS_TYPE_P (type))
     return -1;
 
-  method_vec = CLASSTYPE_METHOD_VEC (type);
+  if (COMPLETE_TYPE_P (type))
+    {
+      if ((name == ctor_identifier
+          || name == base_ctor_identifier
+          || name == complete_ctor_identifier))
+       {
+         if (CLASSTYPE_LAZY_DEFAULT_CTOR (type))
+           lazily_declare_fn (sfk_constructor, type);
+         if (CLASSTYPE_LAZY_COPY_CTOR (type))
+           lazily_declare_fn (sfk_copy_constructor, type);
+       }
+      else if (name == ansi_assopname(NOP_EXPR)
+              && CLASSTYPE_LAZY_ASSIGNMENT_OP (type))
+       lazily_declare_fn (sfk_assignment_operator, type);
+      else if ((name == dtor_identifier
+               || name == base_dtor_identifier
+               || name == complete_dtor_identifier
+               || name == deleting_dtor_identifier)
+              && CLASSTYPE_LAZY_DESTRUCTOR (type))
+       lazily_declare_fn (sfk_destructor, type);
+    }
 
+  method_vec = CLASSTYPE_METHOD_VEC (type);
   if (!method_vec)
     return -1;
 
-  methods = &TREE_VEC_ELT (method_vec, 0);
-  len = TREE_VEC_LENGTH (method_vec);
-
 #ifdef GATHER_STATISTICS
   n_calls_lookup_fnfields_1++;
 #endif /* GATHER_STATISTICS */
 
   /* Constructors are first...  */
   if (name == ctor_identifier)
-    return (methods[CLASSTYPE_CONSTRUCTOR_SLOT] 
-           ? CLASSTYPE_CONSTRUCTOR_SLOT : -1);
+    {
+      fn = CLASSTYPE_CONSTRUCTORS (type);
+      return fn ? CLASSTYPE_CONSTRUCTOR_SLOT : -1;
+    }
   /* and destructors are second.  */
   if (name == dtor_identifier)
-    return (methods[CLASSTYPE_DESTRUCTOR_SLOT]
-           ? CLASSTYPE_DESTRUCTOR_SLOT : -1);
+    {
+      fn = CLASSTYPE_DESTRUCTORS (type);
+      return fn ? 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++;
+  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
+       VEC_iterate (tree, method_vec, i, fn);
+       ++i)
+    if (!DECL_CONV_FN_P (OVL_CURRENT (fn)))
+      break;
 
   /* If the type is complete, use binary search.  */
   if (COMPLETE_TYPE_P (type))
     {
-      int lo = i;
-      int hi = len;
+      int lo;
+      int hi;
 
+      lo = i;
+      hi = VEC_length (tree, method_vec);
       while (lo < hi)
        {
          i = (lo + hi) / 2;
@@ -1462,13 +1424,9 @@ lookup_fnfields_1 (tree type, tree name)
          n_outer_fields_searched++;
 #endif /* GATHER_STATISTICS */
 
-         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)
+         tmp = VEC_index (tree, method_vec, i);
+         tmp = DECL_NAME (OVL_CURRENT (tmp));
+         if (tmp > name)
            hi = i;
          else if (tmp < name)
            lo = i + 1;
@@ -1477,20 +1435,34 @@ lookup_fnfields_1 (tree type, tree name)
        }
     }
   else
-    for (; i < len && methods[i]; ++i)
+    for (; VEC_iterate (tree, method_vec, i, fn); ++i)
       {
 #ifdef GATHER_STATISTICS
        n_outer_fields_searched++;
 #endif /* GATHER_STATISTICS */
-       
-       tmp = OVL_CURRENT (methods[i]);
-       if (DECL_NAME (tmp) == name)
+       if (DECL_NAME (OVL_CURRENT (fn)) == name)
          return i;
       }
 
   return -1;
 }
 
+/* Like lookup_fnfields_1, except that the name is extracted from
+   FUNCTION, which is a FUNCTION_DECL or a TEMPLATE_DECL.  */
+
+int
+class_method_index_for_fn (tree class_type, tree function)
+{
+  gcc_assert (TREE_CODE (function) == FUNCTION_DECL
+             || DECL_FUNCTION_TEMPLATE_P (function));
+
+  return lookup_fnfields_1 (class_type,
+                           DECL_CONSTRUCTOR_P (function) ? ctor_identifier :
+                           DECL_DESTRUCTOR_P (function) ? dtor_identifier :
+                           DECL_NAME (function));
+}
+
+
 /* DECL is the result of a qualified name lookup.  QUALIFYING_SCOPE is
    the class or namespace used to qualify the name.  CONTEXT_CLASS is
    the class corresponding to the object in which DECL will be used.
@@ -1503,17 +1475,17 @@ lookup_fnfields_1 (tree type, tree name)
    `B', not `D'.  This function makes that adjustment.  */
 
 tree
-adjust_result_of_qualified_name_lookup (tree decl, 
+adjust_result_of_qualified_name_lookup (tree decl,
                                        tree qualifying_scope,
                                        tree context_class)
 {
-  if (context_class && CLASS_TYPE_P (qualifying_scope) 
+  if (context_class && CLASS_TYPE_P (qualifying_scope)
       && DERIVED_FROM_P (qualifying_scope, context_class)
       && BASELINK_P (decl))
     {
       tree base;
 
-      my_friendly_assert (CLASS_TYPE_P (context_class), 20020808);
+      gcc_assert (CLASS_TYPE_P (context_class));
 
       /* Look for the QUALIFYING_SCOPE as a base of the CONTEXT_CLASS.
         Because we do not yet know which function will be chosen by
@@ -1521,13 +1493,13 @@ adjust_result_of_qualified_name_lookup (tree decl,
         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);
+                         ba_unique | ba_quiet, NULL);
       if (base)
        {
          BASELINK_ACCESS_BINFO (decl) = base;
-         BASELINK_BINFO (decl) 
+         BASELINK_BINFO (decl)
            = lookup_base (base, BINFO_TYPE (BASELINK_BINFO (decl)),
-                          ba_ignore | ba_quiet,
+                          ba_unique | ba_quiet,
                           NULL);
        }
     }
@@ -1536,164 +1508,285 @@ adjust_result_of_qualified_name_lookup (tree decl,
 }
 
 \f
-/* Walk the class hierarchy dominated by TYPE.  FN is called for each
-   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 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.
-
-   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
+/* Walk the class hierarchy within BINFO, in a depth-first traversal.
+   PRE_FN is called in preorder, while POST_FN is called in postorder.
+   If PRE_FN returns DFS_SKIP_BASES, child binfos will not be
+   walked.  If PRE_FN or POST_FN returns a different non-NULL value,
+   that value is immediately returned and the walk is terminated.  One
+   of PRE_FN and POST_FN can be NULL.  At each node, PRE_FN and
+   POST_FN are passed the binfo to examine and the caller's DATA
+   value.  All paths are walked, thus virtual and morally virtual
+   binfos can be multiply walked.  */
 
-static tree
-bfs_walk (tree binfo,
-         tree (*fn) (tree, void *),
-         tree (*qfn) (tree, int, void *),
-         void *data)
+tree
+dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
+             tree (*post_fn) (tree, void *), void *data)
 {
-  tree rval = NULL_TREE;
-
-  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;
-
-  head = tail = 0;
-  base_buffer[tail++] = binfo;
+  tree rval;
+  unsigned ix;
+  tree base_binfo;
 
-  while (head != tail)
+  /* Call the pre-order walking function.  */
+  if (pre_fn)
     {
-      int n_bases, ix;
-      tree binfo = base_buffer[head++];
-      if (head == base_buffer_size)
-       head = 0;
-
-      /* Is this the one we're looking for?  If so, we're done.  */
-      rval = fn (binfo, data);
+      rval = pre_fn (binfo, data);
       if (rval)
-       goto done;
-
-      n_bases = BINFO_N_BASETYPES (binfo);
-      for (ix = 0; ix != n_bases; ix++)
        {
-         tree base_binfo;
-         
-         if (qfn)
-           base_binfo = (*qfn) (binfo, ix, data);
-         else
-           base_binfo = BINFO_BASETYPE (binfo, ix);
-         
-         if (base_binfo)
-           {
-             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;
-               }
-           }
+         if (rval == dfs_skip_bases)
+           goto skip_bases;
+         return rval;
        }
     }
 
- done:
-  if (base_buffer_size != BFS_WALK_INITIAL_QUEUE_SIZE)
-    free (base_buffer);
-  return rval;
-}
-
-/* Exactly like bfs_walk, except that a depth-first traversal is
-   performed, and PREFN is called in preorder, while POSTFN is called
-   in postorder.  */
+  /* Find the next child binfo to walk.  */
+  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
+    {
+      rval = dfs_walk_all (base_binfo, pre_fn, post_fn, data);
+      if (rval)
+       return rval;
+    }
 
-tree
-dfs_walk_real (tree binfo,
-              tree (*prefn) (tree, void *),
-              tree (*postfn) (tree, void *),
-              tree (*qfn) (tree, int, void *),
-              void *data)
+ skip_bases:
+  /* Call the post-order walking function.  */
+  if (post_fn)
+    {
+      rval = post_fn (binfo, data);
+      gcc_assert (rval != dfs_skip_bases);
+      return rval;
+    }
+
+  return NULL_TREE;
+}
+
+/* Worker for dfs_walk_once.  This behaves as dfs_walk_all, except
+   that binfos are walked at most once.  */
+
+static tree
+dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
+                tree (*post_fn) (tree, void *), void *data)
 {
-  tree rval = NULL_TREE;
+  tree rval;
+  unsigned ix;
+  tree base_binfo;
 
   /* Call the pre-order walking function.  */
-  if (prefn)
+  if (pre_fn)
+    {
+      rval = pre_fn (binfo, data);
+      if (rval)
+       {
+         if (rval == dfs_skip_bases)
+           goto skip_bases;
+
+         return rval;
+       }
+    }
+
+  /* Find the next child binfo to walk.  */
+  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
     {
-      rval = (*prefn) (binfo, data);
+      if (BINFO_VIRTUAL_P (base_binfo))
+       {
+         if (BINFO_MARKED (base_binfo))
+           continue;
+         BINFO_MARKED (base_binfo) = 1;
+       }
+
+      rval = dfs_walk_once_r (base_binfo, pre_fn, post_fn, data);
       if (rval)
        return rval;
     }
 
+ skip_bases:
+  /* Call the post-order walking function.  */
+  if (post_fn)
+    {
+      rval = post_fn (binfo, data);
+      gcc_assert (rval != dfs_skip_bases);
+      return rval;
+    }
+
+  return NULL_TREE;
+}
+
+/* Worker for dfs_walk_once. Recursively unmark the virtual base binfos of
+   BINFO.  */
+
+static void
+dfs_unmark_r (tree binfo)
+{
+  unsigned ix;
+  tree base_binfo;
+
   /* Process the basetypes.  */
-  if (BINFO_BASETYPES (binfo))
+  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
     {
-      int i, n = TREE_VEC_LENGTH (BINFO_BASETYPES (binfo));
-      for (i = 0; i != n; i++)
+      if (BINFO_VIRTUAL_P (base_binfo))
        {
+         if (!BINFO_MARKED (base_binfo))
+           continue;
+         BINFO_MARKED (base_binfo) = 0;
+       }
+      /* Only walk, if it can contain more virtual bases.  */
+      if (CLASSTYPE_VBASECLASSES (BINFO_TYPE (base_binfo)))
+       dfs_unmark_r (base_binfo);
+    }
+}
+
+/* Like dfs_walk_all, except that binfos are not multiply walked.  For
+   non-diamond shaped hierarchies this is the same as dfs_walk_all.
+   For diamond shaped hierarchies we must mark the virtual bases, to
+   avoid multiple walks.  */
+
+tree
+dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
+              tree (*post_fn) (tree, void *), void *data)
+{
+  static int active = 0;  /* We must not be called recursively. */
+  tree rval;
+
+  gcc_assert (pre_fn || post_fn);
+  gcc_assert (!active);
+  active++;
+
+  if (!CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo)))
+    /* We are not diamond shaped, and therefore cannot encounter the
+       same binfo twice.  */
+    rval = dfs_walk_all (binfo, pre_fn, post_fn, data);
+  else
+    {
+      rval = dfs_walk_once_r (binfo, pre_fn, post_fn, data);
+      if (!BINFO_INHERITANCE_CHAIN (binfo))
+       {
+         /* We are at the top of the hierarchy, and can use the
+            CLASSTYPE_VBASECLASSES list for unmarking the virtual
+            bases.  */
+         VEC(tree,gc) *vbases;
+         unsigned ix;
          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;
-           }
+
+         for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
+              VEC_iterate (tree, vbases, ix, base_binfo); ix++)
+           BINFO_MARKED (base_binfo) = 0;
        }
+      else
+       dfs_unmark_r (binfo);
     }
 
-  /* Call the post-order walking function.  */
-  if (postfn)
-    rval = (*postfn) (binfo, data);
-  
+  active--;
+
   return rval;
 }
 
-/* Exactly like bfs_walk, except that a depth-first post-order traversal is
-   performed.  */
+/* Worker function for dfs_walk_once_accessible.  Behaves like
+   dfs_walk_once_r, except (a) FRIENDS_P is true if special
+   access given by the current context should be considered, (b) ONCE
+   indicates whether bases should be marked during traversal.  */
 
-tree
-dfs_walk (tree binfo,
-         tree (*fn) (tree, void *),
-         tree (*qfn) (tree, int, void *),
-         void *data)
+static tree
+dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
+                           tree (*pre_fn) (tree, void *),
+                           tree (*post_fn) (tree, void *), void *data)
 {
-  return dfs_walk_real (binfo, 0, fn, qfn, data);
+  tree rval = NULL_TREE;
+  unsigned ix;
+  tree base_binfo;
+
+  /* Call the pre-order walking function.  */
+  if (pre_fn)
+    {
+      rval = pre_fn (binfo, data);
+      if (rval)
+       {
+         if (rval == dfs_skip_bases)
+           goto skip_bases;
+
+         return rval;
+       }
+    }
+
+  /* Find the next child binfo to walk.  */
+  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
+    {
+      bool mark = once && BINFO_VIRTUAL_P (base_binfo);
+
+      if (mark && BINFO_MARKED (base_binfo))
+       continue;
+
+      /* If the base is inherited via private or protected
+        inheritance, then we can't see it, unless we are a friend of
+        the current binfo.  */
+      if (BINFO_BASE_ACCESS (binfo, ix) != access_public_node)
+       {
+         tree scope;
+         if (!friends_p)
+           continue;
+         scope = current_scope ();
+         if (!scope
+             || TREE_CODE (scope) == NAMESPACE_DECL
+             || !is_friend (BINFO_TYPE (binfo), scope))
+           continue;
+       }
+
+      if (mark)
+       BINFO_MARKED (base_binfo) = 1;
+
+      rval = dfs_walk_once_accessible_r (base_binfo, friends_p, once,
+                                        pre_fn, post_fn, data);
+      if (rval)
+       return rval;
+    }
+
+ skip_bases:
+  /* Call the post-order walking function.  */
+  if (post_fn)
+    {
+      rval = post_fn (binfo, data);
+      gcc_assert (rval != dfs_skip_bases);
+      return rval;
+    }
+
+  return NULL_TREE;
+}
+
+/* Like dfs_walk_once except that only accessible bases are walked.
+   FRIENDS_P indicates whether friendship of the local context
+   should be considered when determining accessibility.  */
+
+static tree
+dfs_walk_once_accessible (tree binfo, bool friends_p,
+                           tree (*pre_fn) (tree, void *),
+                           tree (*post_fn) (tree, void *), void *data)
+{
+  bool diamond_shaped = CLASSTYPE_DIAMOND_SHAPED_P (BINFO_TYPE (binfo));
+  tree rval = dfs_walk_once_accessible_r (binfo, friends_p, diamond_shaped,
+                                         pre_fn, post_fn, data);
+
+  if (diamond_shaped)
+    {
+      if (!BINFO_INHERITANCE_CHAIN (binfo))
+       {
+         /* We are at the top of the hierarchy, and can use the
+            CLASSTYPE_VBASECLASSES list for unmarking the virtual
+            bases.  */
+         VEC(tree,gc) *vbases;
+         unsigned ix;
+         tree base_binfo;
+
+         for (vbases = CLASSTYPE_VBASECLASSES (BINFO_TYPE (binfo)), ix = 0;
+              VEC_iterate (tree, vbases, ix, base_binfo); ix++)
+           BINFO_MARKED (base_binfo) = 0;
+       }
+      else
+       dfs_unmark_r (binfo);
+    }
+  return rval;
 }
 
 /* Check that virtual overrider OVERRIDER is acceptable for base function
    BASEFN. Issue diagnostic, and return zero, if unacceptable.  */
 
-int
+static int
 check_final_overrider (tree overrider, tree basefn)
 {
   tree over_type = TREE_TYPE (overrider);
@@ -1715,12 +1808,12 @@ check_final_overrider (tree overrider, tree basefn)
     {
       /* Potentially covariant.  */
       unsigned base_quals, over_quals;
-      
+
       fail = !POINTER_TYPE_P (base_return);
       if (!fail)
        {
          fail = cp_type_quals (base_return) != cp_type_quals (over_return);
-         
+
          base_return = TREE_TYPE (base_return);
          over_return = TREE_TYPE (over_return);
        }
@@ -1729,7 +1822,7 @@ check_final_overrider (tree overrider, tree basefn)
 
       if ((base_quals & over_quals) != over_quals)
        fail = 1;
-      
+
       if (CLASS_TYPE_P (base_return) && CLASS_TYPE_P (over_return))
        {
          tree binfo = lookup_base (over_return, base_return,
@@ -1748,6 +1841,12 @@ check_final_overrider (tree overrider, tree basefn)
          over_return = non_reference (TREE_TYPE (over_type));
          if (CLASS_TYPE_P (over_return))
            fail = 2;
+         else
+           {
+             warning (0, "deprecated covariant return type for %q+#D",
+                            overrider);
+             warning (0, "  overriding %q+#D", basefn);
+           }
        }
       else
        fail = 2;
@@ -1760,38 +1859,37 @@ check_final_overrider (tree overrider, tree basefn)
     {
       if (fail == 1)
        {
-         cp_error_at ("invalid covariant return type for `%#D'", overrider);
-         cp_error_at ("  overriding `%#D'", basefn);
+         error ("invalid covariant return type for %q+#D", overrider);
+         error ("  overriding %q+#D", basefn);
        }
       else
        {
-         cp_error_at ("conflicting return type specified for `%#D'",
-                      overrider);
-         cp_error_at ("  overriding `%#D'", basefn);
+         error ("conflicting return type specified for %q+#D", overrider);
+         error ("  overriding %q+#D", basefn);
        }
       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))
     {
-      cp_error_at ("looser throw specifier for `%#F'", overrider);
-      cp_error_at ("  overriding `%#F'", basefn);
+      error ("looser throw specifier for %q+#F", overrider);
+      error ("  overriding %q+#F", basefn);
       DECL_INVALID_OVERRIDER_P (overrider) = 1;
       return 0;
     }
-  
+
   return 1;
 }
 
 /* Given a class TYPE, and a function decl FNDECL, look for
    virtual functions in TYPE's hierarchy which FNDECL overrides.
    We do not look in TYPE itself, only its bases.
-   
+
    Returns nonzero, if we find any. Set FNDECL's DECL_VIRTUAL_P, if we
    find that it overrides anything.
-   
+
    We check that every function which is overridden, is correctly
    overridden.  */
 
@@ -1799,17 +1897,16 @@ int
 look_for_overrides (tree type, tree fndecl)
 {
   tree binfo = TYPE_BINFO (type);
-  tree basebinfos = BINFO_BASETYPES (binfo);
-  int nbasebinfos = basebinfos ? TREE_VEC_LENGTH (basebinfos) : 0;
+  tree base_binfo;
   int ix;
   int found = 0;
 
-  for (ix = 0; ix != nbasebinfos; ix++)
+  for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
     {
-      tree basetype = BINFO_TYPE (TREE_VEC_ELT (basebinfos, ix));
-      
+      tree basetype = BINFO_TYPE (base_binfo);
+
       if (TYPE_POLYMORPHIC_P (basetype))
-        found += look_for_overrides_r (basetype, fndecl);
+       found += look_for_overrides_r (basetype, fndecl);
     }
   return found;
 }
@@ -1822,30 +1919,36 @@ look_for_overrides_here (tree type, tree fndecl)
 {
   int ix;
 
+  /* If there are no methods in TYPE (meaning that only implicitly
+     declared methods will ever be provided for TYPE), then there are
+     no virtual functions.  */
+  if (!CLASSTYPE_METHOD_VEC (type))
+    return NULL_TREE;
+
   if (DECL_MAYBE_IN_CHARGE_DESTRUCTOR_P (fndecl))
     ix = CLASSTYPE_DESTRUCTOR_SLOT;
   else
     ix = lookup_fnfields_1 (type, DECL_NAME (fndecl));
   if (ix >= 0)
     {
-      tree fns = TREE_VEC_ELT (CLASSTYPE_METHOD_VEC (type), ix);
-  
+      tree fns = VEC_index (tree, CLASSTYPE_METHOD_VEC (type), ix);
+
       for (; fns; fns = OVL_NEXT (fns))
-        {
-          tree fn = OVL_CURRENT (fns);
+       {
+         tree fn = OVL_CURRENT (fns);
 
-          if (!DECL_VIRTUAL_P (fn))
-            /* Not a virtual.  */;
-          else if (DECL_CONTEXT (fn) != type)
-            /* Introduced with a using declaration.  */;
+         if (!DECL_VIRTUAL_P (fn))
+           /* Not a virtual.  */;
+         else if (DECL_CONTEXT (fn) != type)
+           /* Introduced with a using declaration.  */;
          else if (DECL_STATIC_FUNCTION_P (fndecl))
            {
              tree btypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
              tree dtypes = TYPE_ARG_TYPES (TREE_TYPE (fndecl));
-             if (compparms (TREE_CHAIN (btypes), dtypes))
+             if (compparms (TREE_CHAIN (btypes), dtypes))
                return fn;
-            }
-          else if (same_signature_p (fndecl, fn))
+           }
+         else if (same_signature_p (fndecl, fn))
            return fn;
        }
     }
@@ -1865,8 +1968,8 @@ look_for_overrides_r (tree type, tree fndecl)
        {
          /* A static member function cannot match an inherited
             virtual member function.  */
-         cp_error_at ("`%#D' cannot be declared", fndecl);
-         cp_error_at ("  since `%#D' declared in base class", fn);
+         error ("%q+#D cannot be declared", fndecl);
+         error ("  since %q+#D declared in base class", fn);
        }
       else
        {
@@ -1894,17 +1997,14 @@ dfs_get_pure_virtuals (tree binfo, void *data)
   if (!BINFO_PRIMARY_P (binfo))
     {
       tree virtuals;
-      
+
       for (virtuals = BINFO_VIRTUALS (binfo);
           virtuals;
           virtuals = TREE_CHAIN (virtuals))
        if (DECL_PURE_VIRTUAL_P (BV_FN (virtuals)))
-         CLASSTYPE_PURE_VIRTUALS (type) 
-           = tree_cons (NULL_TREE, BV_FN (virtuals),
-                        CLASSTYPE_PURE_VIRTUALS (type));
+         VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (type),
+                        BV_FN (virtuals));
     }
-  
-  BINFO_MARKED (binfo) = 1;
 
   return NULL_TREE;
 }
@@ -1914,88 +2014,18 @@ dfs_get_pure_virtuals (tree binfo, void *data)
 void
 get_pure_virtuals (tree type)
 {
-  tree vbases;
-
   /* Clear the CLASSTYPE_PURE_VIRTUALS list; whatever is already there
      is going to be overridden.  */
-  CLASSTYPE_PURE_VIRTUALS (type) = NULL_TREE;
+  CLASSTYPE_PURE_VIRTUALS (type) = NULL;
   /* Now, run through all the bases which are not primary bases, and
      collect the pure virtual functions.  We look at the vtable in
      each class to determine what pure virtual functions are present.
      (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, 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));
-
-  for (vbases = CLASSTYPE_VBASECLASSES (type); 
-       vbases; 
-       vbases = TREE_CHAIN (vbases))
-    {
-      tree virtuals;
-
-      for (virtuals = BINFO_VIRTUALS (TREE_VALUE (vbases));
-          virtuals;
-          virtuals = TREE_CHAIN (virtuals))
-       {
-         tree base_fndecl = BV_FN (virtuals);
-         if (DECL_NEEDS_FINAL_OVERRIDER_P (base_fndecl))
-           error ("`%#D' needs a final overrider", base_fndecl);
-       }
-    }
+  dfs_walk_once (TYPE_BINFO (type), NULL, dfs_get_pure_virtuals, type);
 }
 \f
-/* DEPTH-FIRST SEARCH ROUTINES.  */
-
-tree 
-markedp (tree derived, int ix, void *data ATTRIBUTE_UNUSED) 
-{
-  tree binfo = BINFO_BASETYPE (derived, ix);
-  
-  return BINFO_MARKED (binfo) ? binfo : NULL_TREE; 
-}
-
-tree
-unmarkedp (tree derived, int ix, void *data ATTRIBUTE_UNUSED) 
-{
-  tree binfo = BINFO_BASETYPE (derived, ix);
-  
-  return !BINFO_MARKED (binfo) ? binfo : NULL_TREE; 
-}
-
-static tree
-marked_pushdecls_p (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
-{
-  tree binfo = BINFO_BASETYPE (derived, ix);
-  
-  return (!BINFO_DEPENDENT_BASE_P (binfo)
-         && BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE; 
-}
-
-static tree
-unmarked_pushdecls_p (tree derived, int ix, void *data ATTRIBUTE_UNUSED)
-{ 
-  tree binfo = BINFO_BASETYPE (derived, ix);
-  
-  return (!BINFO_DEPENDENT_BASE_P (binfo)
-         && !BINFO_PUSHDECLS_MARKED (binfo)) ? binfo : NULL_TREE;
-}
-
-/* The worker functions for `dfs_walk'.  These do not need to
-   test anything (vis a vis marking) if they are paired with
-   a predicate function (above).  */
-
-tree
-dfs_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
-{
-  BINFO_MARKED (binfo) = 0;
-  return NULL_TREE;
-}
-
-\f
 /* Debug info for C++ classes can get very large; try to avoid
    emitting it everywhere.
 
@@ -2006,15 +2036,7 @@ dfs_unmark (tree binfo, void *data ATTRIBUTE_UNUSED)
 void
 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
-     symbolic references between translation units, but only within a single
-     executable or shared library.
-
-     For DWARF 2, we handle TYPE_DECL_SUPPRESS_DEBUG by pretending
-     that the type was never defined, so we only get the members we
-     actually define.  */
-  if (write_symbols == DWARF_DEBUG || write_symbols == NO_DEBUG)
+  if (write_symbols == NO_DEBUG)
     return;
 
   /* We might have set this earlier in cp_finish_decl.  */
@@ -2050,23 +2072,14 @@ dfs_debug_mark (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
   tree t = BINFO_TYPE (binfo);
 
+  if (CLASSTYPE_DEBUG_REQUESTED (t))
+    return dfs_skip_bases;
+
   CLASSTYPE_DEBUG_REQUESTED (t) = 1;
 
   return NULL_TREE;
 }
 
-/* Returns BINFO if we haven't already noted that we want debugging
-   info for this base class.  */
-
-static tree 
-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);
-}
-
 /* Write out the debugging information for TYPE, whose vtable is being
    emitted.  Also walk through our bases and note that we want to
    write out information for them.  This avoids the problem of not
@@ -2083,201 +2096,9 @@ note_debug_info_needed (tree type)
       rest_of_type_compilation (type, toplevel_bindings_p ());
     }
 
-  dfs_walk (TYPE_BINFO (type), dfs_debug_mark, dfs_debug_unmarkedp, 0);
+  dfs_walk_all (TYPE_BINFO (type), dfs_debug_mark, NULL, 0);
 }
 \f
-/* Subroutines of push_class_decls ().  */
-
-static void
-setup_class_bindings (tree name, int type_binding_p)
-{
-  tree type_binding = NULL_TREE;
-  tree value_binding;
-
-  /* If we've already done the lookup for this declaration, we're
-     done.  */
-  if (IDENTIFIER_CLASS_VALUE (name))
-    return;
-
-  /* First, deal with the type binding.  */
-  if (type_binding_p)
-    {
-      type_binding = lookup_member (current_class_type, name,
-                                   /*protect=*/2, /*want_type=*/true);
-      if (TREE_CODE (type_binding) == TREE_LIST 
-         && TREE_TYPE (type_binding) == error_mark_node)
-       /* NAME is ambiguous.  */
-       push_class_level_binding (name, type_binding);
-      else
-       pushdecl_class_level (type_binding);
-    }
-
-  /* Now, do the value binding.  */
-  value_binding = lookup_member (current_class_type, name,
-                                /*protect=*/2, /*want_type=*/false);
-
-  if (type_binding_p
-      && (TREE_CODE (value_binding) == TYPE_DECL
-         || DECL_CLASS_TEMPLATE_P (value_binding)
-         || (TREE_CODE (value_binding) == TREE_LIST
-             && TREE_TYPE (value_binding) == error_mark_node
-             && (TREE_CODE (TREE_VALUE (value_binding))
-                 == TYPE_DECL))))
-    /* We found a type-binding, even when looking for a non-type
-       binding.  This means that we already processed this binding
-       above.  */;
-  else if (value_binding)
-    {
-      if (TREE_CODE (value_binding) == TREE_LIST 
-         && TREE_TYPE (value_binding) == error_mark_node)
-       /* NAME is ambiguous.  */
-       push_class_level_binding (name, value_binding);
-      else
-       {
-         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);
-       }
-    }
-}
-
-/* Push class-level declarations for any names appearing in BINFO that
-   are TYPE_DECLS.  */
-
-static tree
-dfs_push_type_decls (tree binfo, void *data ATTRIBUTE_UNUSED)
-{
-  tree type;
-  tree fields;
-
-  type = BINFO_TYPE (binfo);
-  for (fields = TYPE_FIELDS (type); fields; fields = TREE_CHAIN (fields))
-    if (DECL_NAME (fields) && TREE_CODE (fields) == TYPE_DECL
-       && !(!same_type_p (type, current_class_type)
-            && template_self_reference_p (type, fields)))
-      setup_class_bindings (DECL_NAME (fields), /*type_binding_p=*/1);
-
-  /* We can't just use BINFO_MARKED because envelope_add_decl uses
-     DERIVED_FROM_P, which calls get_base_distance.  */
-  BINFO_PUSHDECLS_MARKED (binfo) = 1;
-
-  return NULL_TREE;
-}
-
-/* Push class-level declarations for any names appearing in BINFO that
-   are not TYPE_DECLS.  */
-
-static tree
-dfs_push_decls (tree binfo, void *data)
-{
-  tree type = BINFO_TYPE (binfo);
-  tree method_vec;
-  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);
-    }
-
-  BINFO_PUSHDECLS_MARKED (binfo) = 0;
-
-  return NULL_TREE;
-}
-
-/* When entering the scope of a class, we cache all of the
-   fields that that class provides within its inheritance
-   lattice.  Where ambiguities result, we mark them
-   with `error_mark_node' so that if they are encountered
-   without explicit qualification, we can emit an error
-   message.  */
-
-void
-push_class_decls (tree type)
-{
-  search_stack = push_search_level (search_stack, &search_obstack);
-
-  /* Enter type declarations and mark.  */
-  dfs_walk (TYPE_BINFO (type), dfs_push_type_decls, unmarked_pushdecls_p, 0);
-
-  /* Enter non-type declarations and unmark.  */
-  dfs_walk (TYPE_BINFO (type), dfs_push_decls, marked_pushdecls_p, 0);
-}
-
-/* Here's a subroutine we need because C lacks lambdas.  */
-
-static tree
-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 (fields) != FIELD_DECL || DECL_ARTIFICIAL (fields))
-       continue;
-
-      TREE_USED (fields) = 0;
-      if (DECL_NAME (fields) == NULL_TREE
-         && ANON_AGGR_TYPE_P (TREE_TYPE (fields)))
-       unuse_fields (TREE_TYPE (fields));
-    }
-
-  return NULL_TREE;
-}
-
-void
-unuse_fields (tree type)
-{
-  dfs_walk (TYPE_BINFO (type), dfs_unuse_fields, unmarkedp, 0);
-}
-
-void
-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.  */
-  if (search_stack)
-    search_stack = pop_search_level (search_stack);
-}
-
 void
 print_search_statistics (void)
 {
@@ -2293,12 +2114,6 @@ print_search_statistics (void)
 }
 
 void
-init_search_processing (void)
-{
-  gcc_obstack_init (&search_obstack);
-}
-
-void
 reinit_search_statistics (void)
 {
 #ifdef GATHER_STATISTICS
@@ -2311,161 +2126,321 @@ reinit_search_statistics (void)
 #endif /* GATHER_STATISTICS */
 }
 
-static tree
-add_conversions (tree binfo, void *data)
+/* Helper for lookup_conversions_r.  TO_TYPE is the type converted to
+   by a conversion op in base BINFO.  VIRTUAL_DEPTH is nonzero if
+   BINFO is morally virtual, and VIRTUALNESS is nonzero if virtual
+   bases have been encountered already in the tree walk.  PARENT_CONVS
+   is the list of lists of conversion functions that could hide CONV
+   and OTHER_CONVS is the list of lists of conversion functions that
+   could hide or be hidden by CONV, should virtualness be involved in
+   the hierarchy.  Merely checking the conversion op's name is not
+   enough because two conversion operators to the same type can have
+   different names.  Return nonzero if we are visible.  */
+
+static int
+check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
+                   tree to_type, tree parent_convs, tree other_convs)
 {
-  int i;
-  tree method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
-  tree *conversions = (tree *) data;
+  tree level, probe;
 
-  /* Some builtin types have no method vector, not even an empty one.  */
-  if (!method_vec)
-    return NULL_TREE;
+  /* See if we are hidden by a parent conversion.  */
+  for (level = parent_convs; level; level = TREE_CHAIN (level))
+    for (probe = TREE_VALUE (level); probe; probe = TREE_CHAIN (probe))
+      if (same_type_p (to_type, TREE_TYPE (probe)))
+       return 0;
 
-  for (i = 2; i < TREE_VEC_LENGTH (method_vec); ++i)
+  if (virtual_depth || virtualness)
     {
-      tree tmp = TREE_VEC_ELT (method_vec, i);
-      tree name;
+     /* In a virtual hierarchy, we could be hidden, or could hide a
+       conversion function on the other_convs list.  */
+      for (level = other_convs; level; level = TREE_CHAIN (level))
+       {
+         int we_hide_them;
+         int they_hide_us;
+         tree *prev, other;
 
-      if (!tmp || ! DECL_CONV_FN_P (OVL_CURRENT (tmp)))
-       break;
+         if (!(virtual_depth || TREE_STATIC (level)))
+           /* Neither is morally virtual, so cannot hide each other.  */
+           continue;
 
-      name = DECL_NAME (OVL_CURRENT (tmp));
+         if (!TREE_VALUE (level))
+           /* They evaporated away already.  */
+           continue;
 
-      /* Make sure we don't already have this conversion.  */
-      if (! IDENTIFIER_MARKED (name))
-       {
-         tree t;
+         they_hide_us = (virtual_depth
+                         && original_binfo (binfo, TREE_PURPOSE (level)));
+         we_hide_them = (!they_hide_us && TREE_STATIC (level)
+                         && original_binfo (TREE_PURPOSE (level), binfo));
 
-         /* 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)
+         if (!(we_hide_them || they_hide_us))
+           /* Neither is within the other, so no hiding can occur.  */
+           continue;
+
+         for (prev = &TREE_VALUE (level), other = *prev; other;)
            {
-             *conversions = tree_cons (binfo, tmp, *conversions);
-             IDENTIFIER_MARKED (name) = 1;
+             if (same_type_p (to_type, TREE_TYPE (other)))
+               {
+                 if (they_hide_us)
+                   /* We are hidden.  */
+                   return 0;
+
+                 if (we_hide_them)
+                   {
+                     /* We hide the other one.  */
+                     other = TREE_CHAIN (other);
+                     *prev = other;
+                     continue;
+                   }
+               }
+             prev = &TREE_CHAIN (other);
+             other = *prev;
            }
        }
     }
-  return NULL_TREE;
+  return 1;
 }
 
-/* Return a TREE_LIST containing all the non-hidden user-defined
-   conversion functions for TYPE (and its base-classes).  The
-   TREE_VALUE of each node is a FUNCTION_DECL or an OVERLOAD
-   containing the conversion functions.  The TREE_PURPOSE is the BINFO
-   from which the conversion functions in this node were selected.  */
+/* Helper for lookup_conversions_r.  PARENT_CONVS is a list of lists
+   of conversion functions, the first slot will be for the current
+   binfo, if MY_CONVS is non-NULL.  CHILD_CONVS is the list of lists
+   of conversion functions from children of the current binfo,
+   concatenated with conversions from elsewhere in the hierarchy --
+   that list begins with OTHER_CONVS.  Return a single list of lists
+   containing only conversions from the current binfo and its
+   children.  */
 
-tree
-lookup_conversions (tree type)
+static tree
+split_conversions (tree my_convs, tree parent_convs,
+                  tree child_convs, tree other_convs)
 {
   tree t;
-  tree conversions = NULL_TREE;
+  tree prev;
 
-  complete_type (type);
-  bfs_walk (TYPE_BINFO (type), add_conversions, 0, &conversions);
+  /* Remove the original other_convs portion from child_convs.  */
+  for (prev = NULL, t = child_convs;
+       t != other_convs; prev = t, t = TREE_CHAIN (t))
+    continue;
 
-  for (t = conversions; t; t = TREE_CHAIN (t))
-    IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (t)))) = 0;
+  if (prev)
+    TREE_CHAIN (prev) = NULL_TREE;
+  else
+    child_convs = NULL_TREE;
 
-  return conversions;
-}
+  /* Attach the child convs to any we had at this level.  */
+  if (my_convs)
+    {
+      my_convs = parent_convs;
+      TREE_CHAIN (my_convs) = child_convs;
+    }
+  else
+    my_convs = child_convs;
+
+  return my_convs;
+}
+
+/* Worker for lookup_conversions.  Lookup conversion functions in
+   BINFO and its children.  VIRTUAL_DEPTH is nonzero, if BINFO is in
+   a morally virtual base, and VIRTUALNESS is nonzero, if we've
+   encountered virtual bases already in the tree walk.  PARENT_CONVS &
+   PARENT_TPL_CONVS are lists of list of conversions within parent
+   binfos.  OTHER_CONVS and OTHER_TPL_CONVS are conversions found
+   elsewhere in the tree.  Return the conversions found within this
+   portion of the graph in CONVS and TPL_CONVS.  Return nonzero is we
+   encountered virtualness.  We keep template and non-template
+   conversions separate, to avoid unnecessary type comparisons.
+
+   The located conversion functions are held in lists of lists.  The
+   TREE_VALUE of the outer list is the list of conversion functions
+   found in a particular binfo.  The TREE_PURPOSE of both the outer
+   and inner lists is the binfo at which those conversions were
+   found.  TREE_STATIC is set for those lists within of morally
+   virtual binfos.  The TREE_VALUE of the inner list is the conversion
+   function or overload itself.  The TREE_TYPE of each inner list node
+   is the converted-to type.  */
 
-struct overlap_info 
-{
-  tree compare_type;
-  int found_overlap;
-};
+static int
+lookup_conversions_r (tree binfo,
+                     int virtual_depth, int virtualness,
+                     tree parent_convs, tree parent_tpl_convs,
+                     tree other_convs, tree other_tpl_convs,
+                     tree *convs, tree *tpl_convs)
+{
+  int my_virtualness = 0;
+  tree my_convs = NULL_TREE;
+  tree my_tpl_convs = NULL_TREE;
+  tree child_convs = NULL_TREE;
+  tree child_tpl_convs = NULL_TREE;
+  unsigned i;
+  tree base_binfo;
+  VEC(tree,gc) *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (binfo));
+  tree conv;
+
+  /* If we have no conversion operators, then don't look.  */
+  if (!TYPE_HAS_CONVERSION (BINFO_TYPE (binfo)))
+    {
+      *convs = *tpl_convs = NULL_TREE;
 
-/* Check whether the empty class indicated by EMPTY_BINFO is also present
-   at offset 0 in COMPARE_TYPE, and set found_overlap if so.  */
+      return 0;
+    }
 
-static tree
-dfs_check_overlap (tree empty_binfo, void *data)
-{
-  struct overlap_info *oi = (struct overlap_info *) data;
-  tree binfo;
-  for (binfo = TYPE_BINFO (oi->compare_type); 
-       ; 
-       binfo = BINFO_BASETYPE (binfo, 0))
+  if (BINFO_VIRTUAL_P (binfo))
+    virtual_depth++;
+
+  /* First, locate the unhidden ones at this level.  */
+  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
+       VEC_iterate (tree, method_vec, i, conv);
+       ++i)
     {
-      if (BINFO_TYPE (binfo) == BINFO_TYPE (empty_binfo))
+      tree cur = OVL_CURRENT (conv);
+
+      if (!DECL_CONV_FN_P (cur))
+       break;
+
+      if (TREE_CODE (cur) == TEMPLATE_DECL)
        {
-         oi->found_overlap = 1;
-         break;
+         /* Only template conversions can be overloaded, and we must
+            flatten them out and check each one individually.  */
+         tree tpls;
+
+         for (tpls = conv; tpls; tpls = OVL_NEXT (tpls))
+           {
+             tree tpl = OVL_CURRENT (tpls);
+             tree type = DECL_CONV_FN_TYPE (tpl);
+
+             if (check_hidden_convs (binfo, virtual_depth, virtualness,
+                                     type, parent_tpl_convs, other_tpl_convs))
+               {
+                 my_tpl_convs = tree_cons (binfo, tpl, my_tpl_convs);
+                 TREE_TYPE (my_tpl_convs) = type;
+                 if (virtual_depth)
+                   {
+                     TREE_STATIC (my_tpl_convs) = 1;
+                     my_virtualness = 1;
+                   }
+               }
+           }
+       }
+      else
+       {
+         tree name = DECL_NAME (cur);
+
+         if (!IDENTIFIER_MARKED (name))
+           {
+             tree type = DECL_CONV_FN_TYPE (cur);
+
+             if (check_hidden_convs (binfo, virtual_depth, virtualness,
+                                     type, parent_convs, other_convs))
+               {
+                 my_convs = tree_cons (binfo, conv, my_convs);
+                 TREE_TYPE (my_convs) = type;
+                 if (virtual_depth)
+                   {
+                     TREE_STATIC (my_convs) = 1;
+                     my_virtualness = 1;
+                   }
+                 IDENTIFIER_MARKED (name) = 1;
+               }
+           }
        }
-      else if (BINFO_BASETYPES (binfo) == NULL_TREE)
-       break;
     }
 
-  return NULL_TREE;
-}
+  if (my_convs)
+    {
+      parent_convs = tree_cons (binfo, my_convs, parent_convs);
+      if (virtual_depth)
+       TREE_STATIC (parent_convs) = 1;
+    }
 
-/* Trivial function to stop base traversal when we find something.  */
+  if (my_tpl_convs)
+    {
+      parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
+      if (virtual_depth)
+       TREE_STATIC (parent_convs) = 1;
+    }
 
-static tree
-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;
-}
+  child_convs = other_convs;
+  child_tpl_convs = other_tpl_convs;
 
-/* Returns nonzero if EMPTY_TYPE or any of its bases can also be found at
-   offset 0 in NEXT_TYPE.  Used in laying out empty base class subobjects.  */
+  /* Now iterate over each base, looking for more conversions.  */
+  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
+    {
+      tree base_convs, base_tpl_convs;
+      unsigned base_virtualness;
+
+      base_virtualness = lookup_conversions_r (base_binfo,
+                                              virtual_depth, virtualness,
+                                              parent_convs, parent_tpl_convs,
+                                              child_convs, child_tpl_convs,
+                                              &base_convs, &base_tpl_convs);
+      if (base_virtualness)
+       my_virtualness = virtualness = 1;
+      child_convs = chainon (base_convs, child_convs);
+      child_tpl_convs = chainon (base_tpl_convs, child_tpl_convs);
+    }
 
-int
-types_overlap_p (tree empty_type, tree next_type)
-{
-  struct overlap_info oi;
+  /* Unmark the conversions found at this level  */
+  for (conv = my_convs; conv; conv = TREE_CHAIN (conv))
+    IDENTIFIER_MARKED (DECL_NAME (OVL_CURRENT (TREE_VALUE (conv)))) = 0;
 
-  if (! IS_AGGR_TYPE (next_type))
-    return 0;
-  oi.compare_type = next_type;
-  oi.found_overlap = 0;
-  dfs_walk (TYPE_BINFO (empty_type), dfs_check_overlap,
-           dfs_no_overlap_yet, &oi);
-  return oi.found_overlap;
-}
+  *convs = split_conversions (my_convs, parent_convs,
+                             child_convs, other_convs);
+  *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
+                                 child_tpl_convs, other_tpl_convs);
 
-/* Given a vtable VAR, determine which of the inherited classes the vtable
-   inherits (in a loose sense) functions from.
+  return my_virtualness;
+}
 
-   FIXME: This does not work with the new ABI.  */
+/* Return a TREE_LIST containing all the non-hidden user-defined
+   conversion functions for TYPE (and its base-classes).  The
+   TREE_VALUE of each node is the FUNCTION_DECL of the conversion
+   function.  The TREE_PURPOSE is the BINFO from which the conversion
+   functions in this node were selected.  This function is effectively
+   performing a set of member lookups as lookup_fnfield does, but
+   using the type being converted to as the unique key, rather than the
+   field name.  */
 
 tree
-binfo_for_vtable (tree var)
+lookup_conversions (tree type)
 {
-  tree main_binfo = TYPE_BINFO (DECL_CONTEXT (var));
-  tree binfos = TYPE_BINFO_BASETYPES (BINFO_TYPE (main_binfo));
-  int n_baseclasses = CLASSTYPE_N_BASECLASSES (BINFO_TYPE (main_binfo));
-  int i;
+  tree convs, tpl_convs;
+  tree list = NULL_TREE;
+
+  complete_type (type);
+  if (!TYPE_BINFO (type))
+    return NULL_TREE;
+
+  lookup_conversions_r (TYPE_BINFO (type), 0, 0,
+                       NULL_TREE, NULL_TREE, NULL_TREE, NULL_TREE,
+                       &convs, &tpl_convs);
 
-  for (i = 0; i < n_baseclasses; i++)
+  /* Flatten the list-of-lists */
+  for (; convs; convs = TREE_CHAIN (convs))
     {
-      tree base_binfo = TREE_VEC_ELT (binfos, i);
-      if (base_binfo != NULL_TREE && BINFO_VTABLE (base_binfo) == var)
-       return base_binfo;
+      tree probe, next;
+
+      for (probe = TREE_VALUE (convs); probe; probe = next)
+       {
+         next = TREE_CHAIN (probe);
+
+         TREE_CHAIN (probe) = list;
+         list = probe;
+       }
     }
 
-  /* If no secondary base classes matched, return the primary base, if
-     there is one.  */
-  if (CLASSTYPE_HAS_PRIMARY_BASE_P (BINFO_TYPE (main_binfo)))
-    return get_primary_binfo (main_binfo);
+  for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
+    {
+      tree probe, next;
 
-  return main_binfo;
+      for (probe = TREE_VALUE (tpl_convs); probe; probe = next)
+       {
+         next = TREE_CHAIN (probe);
+
+         TREE_CHAIN (probe) = list;
+         list = probe;
+       }
+    }
+
+  return list;
 }
 
 /* Returns the binfo of the first direct or indirect virtual base derived
@@ -2476,7 +2451,7 @@ binfo_from_vbase (tree binfo)
 {
   for (; binfo; binfo = BINFO_INHERITANCE_CHAIN (binfo))
     {
-      if (TREE_VIA_VIRTUAL (binfo))
+      if (BINFO_VIRTUAL_P (binfo))
        return binfo;
     }
   return NULL_TREE;
@@ -2489,10 +2464,14 @@ binfo_from_vbase (tree binfo)
 tree
 binfo_via_virtual (tree binfo, tree limit)
 {
-  for (; binfo && (!limit || !same_type_p (BINFO_TYPE (binfo), limit));
+  if (limit && !CLASSTYPE_VBASECLASSES (limit))
+    /* LIMIT has no virtual bases, so BINFO cannot be via one.  */
+    return NULL_TREE;
+
+  for (; binfo && !SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), limit);
        binfo = BINFO_INHERITANCE_CHAIN (binfo))
     {
-      if (TREE_VIA_VIRTUAL (binfo))
+      if (BINFO_VIRTUAL_P (binfo))
        return binfo;
     }
   return NULL_TREE;
@@ -2506,48 +2485,55 @@ tree
 copied_binfo (tree binfo, tree here)
 {
   tree result = NULL_TREE;
-  
-  if (TREE_VIA_VIRTUAL (binfo))
+
+  if (BINFO_VIRTUAL_P (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);
+
+      result = binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (t));
     }
   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;
-           }
-       }
+      tree cbinfo;
+      tree base_binfo;
+      int ix;
+
+      cbinfo = copied_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
+      for (ix = 0; BINFO_BASE_ITERATE (cbinfo, ix, base_binfo); ix++)
+       if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo), BINFO_TYPE (binfo)))
+         {
+           result = base_binfo;
+           break;
+         }
     }
   else
     {
-      my_friendly_assert (BINFO_TYPE (here) == BINFO_TYPE (binfo), 20030202);
+      gcc_assert (SAME_BINFO_TYPE_P (BINFO_TYPE (here), BINFO_TYPE (binfo)));
       result = here;
     }
 
-  my_friendly_assert (result, 20030202);
+  gcc_assert (result);
   return result;
 }
 
+tree
+binfo_for_vbase (tree base, tree t)
+{
+  unsigned ix;
+  tree binfo;
+  VEC(tree,gc) *vbases;
+
+  for (vbases = CLASSTYPE_VBASECLASSES (t), ix = 0;
+       VEC_iterate (tree, vbases, ix, binfo); ix++)
+    if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), base))
+      return binfo;
+  return NULL;
+}
+
 /* 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
@@ -2557,40 +2543,33 @@ tree
 original_binfo (tree binfo, tree here)
 {
   tree result = NULL;
-  
-  if (BINFO_TYPE (binfo) == BINFO_TYPE (here))
+
+  if (SAME_BINFO_TYPE_P (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_VIRTUAL_P (binfo))
+    result = (CLASSTYPE_VBASECLASSES (BINFO_TYPE (here))
+             ? binfo_for_vbase (BINFO_TYPE (binfo), BINFO_TYPE (here))
+             : NULL_TREE);
   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;
-               }
-           }
+         int ix;
+         tree base_binfo;
+
+         for (ix = 0; (base_binfo = BINFO_BASE_BINFO (base_binfos, ix)); ix++)
+           if (SAME_BINFO_TYPE_P (BINFO_TYPE (base_binfo),
+                                  BINFO_TYPE (binfo)))
+             {
+               result = base_binfo;
+               break;
+             }
        }
     }
-  
+
   return result;
 }