OSDN Git Service

* call.c (struct z_candidate): Add explicit_targs field.
[pf3gnuchains/gcc-fork.git] / gcc / cp / search.c
index 9c3d230..11011e7 100644 (file)
@@ -1,14 +1,15 @@
 /* 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, 2007, 2008, 2009
+   Free Software Foundation, Inc.
    Contributed by Michael Tiemann (tiemann@cygnus.com)
 
 This file is part of GCC.
 
 GCC is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2, or (at your option)
+the Free Software Foundation; either version 3, or (at your option)
 any later version.
 
 GCC is distributed in the hope that it will be useful,
@@ -17,9 +18,8 @@ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
-along with GCC; see the file COPYING.  If not, write to
-the Free Software Foundation, 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.  */
+along with GCC; see the file COPYING3.  If not see
+<http://www.gnu.org/licenses/>.  */
 
 /* High-level class interface.  */
 
@@ -29,17 +29,18 @@ Boston, MA 02111-1307, USA.  */
 #include "tm.h"
 #include "tree.h"
 #include "cp-tree.h"
+#include "intl.h"
 #include "obstack.h"
 #include "flags.h"
 #include "rtl.h"
 #include "output.h"
 #include "toplev.h"
-#include "stack.h"
+#include "target.h"
 
 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 base_kind lookup_base_r (tree, tree, base_access, bool, tree *);
 static tree dfs_debug_mark (tree, void *);
 static tree dfs_walk_once_r (tree, tree (*pre_fn) (tree, void *),
                             tree (*post_fn) (tree, void *), void *data);
@@ -63,7 +64,6 @@ 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 int template_self_reference_p (tree, tree);
 static tree dfs_get_pure_virtuals (tree, void *);
 
 \f
@@ -78,102 +78,91 @@ 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
 {
-  int i;
-  tree base_binfo;
-  base_kind found = bk_not_base;
-  
-  if (SAME_BINFO_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)
-       {
-         if (access != ba_any)
-           *binfo_ptr = NULL;
-         else if (!is_virtual)
-           /* Prefer a non-virtual base.  */
-           *binfo_ptr = binfo;
-         found = bk_ambig;
-       }
-      
-      return found;
-    }
-  
-  for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
+  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_)
+{
+  struct lookup_base_data_s *data = (struct lookup_base_data_s *) data_;
+
+  if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->base))
     {
-      base_kind bk;
+      if (!data->binfo)
+       {
+         data->binfo = binfo;
+         data->via_virtual
+           = binfo_via_virtual (data->binfo, data->t) != NULL_TREE;
 
-      bk = lookup_base_r (base_binfo, base,
-                         access,
-                         is_virtual || BINFO_VIRTUAL_P (base_binfo),
-                         binfo_ptr);
+         if (!data->repeated_base)
+           /* If there are no repeated bases, we can stop now.  */
+           return binfo;
 
-      switch (bk)
+         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;
+       }
+      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:
-         gcc_assert (found == bk_not_base);
-         found = bk;
-         break;
-         
-       case bk_via_virtual:
-         if (found != bk_ambig)
-           found = bk;
-         break;
-         
-       case bk_not_base:
-         break;
-
-       default:
-         gcc_unreachable ();
+         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 (possibly non-proper) 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.  */
@@ -186,7 +175,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
@@ -202,10 +191,10 @@ accessible_base_p (tree t, tree base)
 tree
 lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
 {
-  tree binfo = NULL_TREE;      /* The binfo we've found so far.  */
-  tree t_binfo = NULL_TREE;
+  tree binfo;
+  tree t_binfo;
   base_kind bk;
-  
+
   if (t == error_mark_node || base == error_mark_node)
     {
       if (kind_ptr)
@@ -213,24 +202,51 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
       return error_mark_node;
     }
   gcc_assert (TYPE_P (base));
-  
+
   if (!TYPE_P (t))
     {
       t_binfo = t;
       t = BINFO_TYPE (t);
     }
-  else  
+  else
     {
       t = complete_type (TYPE_MAIN_VARIANT (t));
       t_binfo = TYPE_BINFO (t);
     }
-  
-  base = complete_type (TYPE_MAIN_VARIANT (base));
 
-  if (t_binfo)
-    bk = lookup_base_r (t_binfo, base, access, 0, &binfo);
+  base = TYPE_MAIN_VARIANT (base);
+
+  /* If BASE is incomplete, it can't be a base of T--and instantiating it
+     might cause an error.  */
+  if (t_binfo && CLASS_TYPE_P (base)
+      && (COMPLETE_TYPE_P (base) || TYPE_BEING_DEFINED (base)))
+    {
+      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
-    bk = bk_not_base;
+    {
+      binfo = NULL_TREE;
+      bk = bk_not_base;
+    }
 
   /* Check that the base is unambiguous and accessible.  */
   if (access != ba_any)
@@ -240,7 +256,6 @@ 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 ("%qT is an ambiguous base of %qT", base, t);
@@ -249,7 +264,7 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
        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,
@@ -257,7 +272,7 @@ 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))
              {
@@ -273,7 +288,7 @@ lookup_base (tree t, tree base, base_access access, base_kind *kind_ptr)
 
   if (kind_ptr)
     *kind_ptr = bk;
-  
+
   return binfo;
 }
 
@@ -295,11 +310,11 @@ struct dcast_data_s
 static tree
 dfs_dcast_hint_pre (tree binfo, void *data_)
 {
-  struct dcast_data_s *data = data_;
+  struct dcast_data_s *data = (struct dcast_data_s *) data_;
 
   if (BINFO_VIRTUAL_P (binfo))
     data->virt_depth++;
-  
+
   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), data->subtype))
     {
       if (data->virt_depth)
@@ -323,7 +338,7 @@ dfs_dcast_hint_pre (tree binfo, void *data_)
 static tree
 dfs_dcast_hint_post (tree binfo, void *data_)
 {
-  struct dcast_data_s *data = data_;
+  struct dcast_data_s *data = (struct dcast_data_s *) data_;
 
   if (BINFO_VIRTUAL_P (binfo))
     data->virt_depth--;
@@ -351,7 +366,7 @@ dcast_base_hint (tree subtype, tree target)
   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);
@@ -375,7 +390,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
@@ -383,12 +398,10 @@ lookup_field_1 (tree type, tree name, bool want_type)
        The TYPE_FIELDS of TYPENAME_TYPE is its TYPENAME_TYPE_FULLNAME.  */
     return NULL_TREE;
 
-  if (TYPE_NAME (type)
-      && DECL_LANG_SPECIFIC (TYPE_NAME (type))
-      && DECL_SORTED_FIELDS (TYPE_NAME (type)))
+  if (CLASSTYPE_SORTED_FIELDS (type))
     {
-      tree *fields = &DECL_SORTED_FIELDS (TYPE_NAME (type))->elts[0];
-      int lo = 0, hi = DECL_SORTED_FIELDS (TYPE_NAME (type))->len;
+      tree *fields = &CLASSTYPE_SORTED_FIELDS (type)->elts[0];
+      int lo = 0, hi = CLASSTYPE_SORTED_FIELDS (type)->len;
       int i;
 
       while (lo < hi)
@@ -459,18 +472,14 @@ lookup_field_1 (tree type, tree name, bool want_type)
             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 template classes work
-            correctly.  */
-         if (CLASSTYPE_TEMPLATE_INFO (type)
-             && !CLASSTYPE_USE_TEMPLATE (type)
-             && !TREE_TYPE (field))
-           ;
-         else
+            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;
@@ -485,7 +494,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
@@ -493,30 +508,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
@@ -544,9 +555,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.  */
@@ -555,11 +565,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))
@@ -603,7 +613,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
@@ -612,11 +622,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)
@@ -632,8 +642,8 @@ dfs_access_in_type (tree binfo, void *data)
        {
          int i;
          tree base_binfo;
-         VEC (tree) *accesses;
-         
+         VEC(tree,gc) *accesses;
+
          /* Otherwise, scan our baseclasses, and pick the most favorable
             access.  */
          accesses = BINFO_BASE_ACCESSES (binfo);
@@ -690,7 +700,7 @@ 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
@@ -712,20 +722,13 @@ 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.  
+       class P derived from N, where m as a member of P is public, private
+       or 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
-    does not change.
+    Here DERIVED is a possible P, DECL is m and BINFO_TYPE (binfo) is N.  */
 
-    Note that I believe that the passage above is wrong, and should read
-    "...is private or protected or public"; otherwise you get bizarre results
-    whereby a public using-decl can prevent you from accessing a protected
-    member of a base.  (jason 2000/02/28)  */
-
-  /* If DERIVED isn't derived from m's class, then it can't be a P.  */
-  if (!DERIVED_FROM_P (context_for_name_lookup (decl), derived))
+  /* If DERIVED isn't derived from N, then it can't be a P.  */
+  if (!DERIVED_FROM_P (BINFO_TYPE (binfo), derived))
     return 0;
 
   access = access_in_type (derived, decl);
@@ -733,7 +736,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
@@ -752,7 +755,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;
     }
@@ -784,8 +787,8 @@ friend_accessible_p (tree scope, tree decl, tree binfo)
     if (protected_accessible_p (decl, TREE_VALUE (t), binfo))
       return 1;
 
-  /* Nested classes are implicitly friends of their enclosing types, as
-     per core issue 45 (this is a change from the standard).  */
+  /* Nested classes have the same access as their enclosing types, as
+     per DR 45 (this is a change from the standard).  */
   if (TYPE_P (scope))
     for (t = TYPE_CONTEXT (scope); t && TYPE_P (t); t = TYPE_CONTEXT (t))
       if (protected_accessible_p (decl, t, binfo))
@@ -794,9 +797,9 @@ 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;
 
@@ -812,16 +815,6 @@ friend_accessible_p (tree scope, tree decl, tree binfo)
          return ret;
        }
     }
-  else if (CLASSTYPE_TEMPLATE_INFO (scope))
-    {
-      int ret;
-      /* Increment processing_template_decl to make sure that
-        dependent_type_p works correctly.  */
-      ++processing_template_decl;
-      ret = friend_accessible_p (CLASSTYPE_TI_TEMPLATE (scope), decl, binfo);
-      --processing_template_decl;
-      return ret;
-    }
 
   return 0;
 }
@@ -831,10 +824,14 @@ friend_accessible_p (tree scope, tree decl, tree binfo)
 static tree
 dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
 {
-  if (BINFO_ACCESS (binfo) != ak_none
-      && is_friend (BINFO_TYPE (binfo), current_scope ()))
-    return binfo;
-  
+  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;
 }
 
@@ -842,13 +839,14 @@ dfs_accessible_post (tree binfo, void *data ATTRIBUTE_UNUSED)
    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;
 
@@ -869,8 +867,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))
@@ -896,19 +898,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);
+  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);
+      /* 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.  */
@@ -920,15 +926,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_once_accessible (binfo, /*friends=*/true,
-                                   NULL, dfs_accessible_post, NULL);
-      
-      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 {
@@ -949,24 +955,6 @@ struct lookup_field_info {
   const char *errstr;
 };
 
-/* 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.  */
-
-static int
-template_self_reference_p (tree type, tree decl)
-{
-  return  (CLASSTYPE_USE_TEMPLATE (type)
-          && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (type))
-          && TREE_CODE (decl) == TYPE_DECL
-          && DECL_ARTIFICIAL (decl)
-          && DECL_NAME (decl) == constructor_name (type));
-}
-
 /* Nonzero for a class member means that it is shared between all objects
    of that class.
 
@@ -985,6 +973,7 @@ shared_member_p (tree t)
     return 1;
   if (is_overloaded_fn (t))
     {
+      t = get_fns (t);
       for (; t; t = OVL_NEXT (t))
        {
          tree fn = OVL_CURRENT (t);
@@ -1004,7 +993,7 @@ static int
 is_subobject_of_p (tree parent, tree binfo)
 {
   tree probe;
-  
+
   for (probe = parent; probe; probe = BINFO_INHERITANCE_CHAIN (probe))
     {
       if (probe == binfo)
@@ -1031,7 +1020,7 @@ lookup_field_r (tree binfo, void *data)
   /* 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
@@ -1077,25 +1066,20 @@ 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 
+         else
            goto done;
        }
     }
 
-  /* You must name a template base class with a template-id.  */
-  if (!same_type_p (type, lfi->type) 
-      && template_self_reference_p (type, nval))
-    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
       && !is_subobject_of_p (lfi->rval_binfo, binfo))
-    
+
     {
       if (nval == lfi->rval && shared_member_p (nval))
        /* The two things are really the same.  */
@@ -1119,7 +1103,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 = G_("request for member %qD is ambiguous");
        }
     }
   else
@@ -1189,6 +1173,9 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
 
   const char *errstr = 0;
 
+  if (name == error_mark_node)
+    return NULL_TREE;
+
   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
 
   if (TREE_CODE (xbasetype) == TREE_BINFO)
@@ -1198,7 +1185,8 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
     }
   else
     {
-      gcc_assert (IS_AGGR_TYPE_CODE (TREE_CODE (xbasetype)));
+      if (!RECORD_OR_UNION_CODE_P (TREE_CODE (xbasetype)))
+       return NULL_TREE;
       type = xbasetype;
       xbasetype = NULL_TREE;
     }
@@ -1229,8 +1217,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;
@@ -1241,19 +1229,37 @@ lookup_member (tree xbasetype, tree name, int protect, bool want_type)
   /* [class.access]
 
      In the case of overloaded function names, access control is
-     applied to the function selected by overloaded resolution.  */
-  if (rval && protect && !is_overloaded_fn (rval))
-    perform_or_defer_access_check (basetype_path, rval);
+     applied to the function selected by overloaded resolution.  
+
+     We cannot check here, even if RVAL is only a single non-static
+     member function, since we do not know what the "this" pointer
+     will be.  For:
+
+        class A { protected: void f(); };
+        class B : public A { 
+          void g(A *p) {
+            f(); // OK
+            p->f(); // Not OK.
+          }
+        };
+
+    only the first call to "f" is valid.  However, if the function is
+    static, we can check.  */
+  if (rval && protect 
+      && !really_overloaded_fn (rval)
+      && !(TREE_CODE (rval) == FUNCTION_DECL
+          && DECL_NONSTATIC_MEMBER_FUNCTION_P (rval)))
+    perform_or_defer_access_check (basetype_path, rval, rval);
 
   if (errstr && protect)
     {
       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));
@@ -1267,7 +1273,7 @@ tree
 lookup_field (tree xbasetype, tree name, int protect, bool want_type)
 {
   tree rval = lookup_member (xbasetype, name, protect, want_type);
-  
+
   /* Ignore functions, but propagate the ambiguity list.  */
   if (!error_operand_p (rval)
       && (rval && BASELINK_P (rval)))
@@ -1306,8 +1312,8 @@ lookup_conversion_operator (tree class_type, tree type)
     {
       int i;
       tree fn;
-      VEC(tree) *methods = CLASSTYPE_METHOD_VEC (class_type);
-      
+      VEC(tree,gc) *methods = CLASSTYPE_METHOD_VEC (class_type);
+
       for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
           VEC_iterate (tree, methods, i, fn); ++i)
        {
@@ -1318,7 +1324,7 @@ lookup_conversion_operator (tree class_type, tree 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.  */
@@ -1337,11 +1343,11 @@ lookup_conversion_operator (tree class_type, tree type)
 int
 lookup_fnfields_1 (tree type, tree name)
 {
-  VEC(tree) *method_vec;
+  VEC(tree,gc) *method_vec;
   tree fn;
   tree tmp;
   size_t i;
-  
+
   if (!CLASS_TYPE_P (type))
     return -1;
 
@@ -1355,10 +1361,18 @@ lookup_fnfields_1 (tree type, tree name)
            lazily_declare_fn (sfk_constructor, type);
          if (CLASSTYPE_LAZY_COPY_CTOR (type))
            lazily_declare_fn (sfk_copy_constructor, type);
+         if (CLASSTYPE_LAZY_MOVE_CTOR (type))
+           lazily_declare_fn (sfk_move_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);
@@ -1458,31 +1472,31 @@ class_method_index_for_fn (tree class_type, tree function)
    `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 && context_class != error_mark_node
+      && CLASS_TYPE_P (context_class)
+      && CLASS_TYPE_P (qualifying_scope)
       && DERIVED_FROM_P (qualifying_scope, context_class)
       && BASELINK_P (decl))
     {
       tree base;
 
-      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
         overload resolution, we cannot yet check either accessibility
         or ambiguity -- in either case, the choice of a static member
         function might make the usage valid.  */
       base = lookup_base (context_class, qualifying_scope,
-                         ba_ignore | ba_quiet, NULL);
+                         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);
        }
     }
@@ -1508,7 +1522,7 @@ dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
   tree rval;
   unsigned ix;
   tree base_binfo;
-  
+
   /* Call the pre-order walking function.  */
   if (pre_fn)
     {
@@ -1532,7 +1546,12 @@ dfs_walk_all (tree binfo, tree (*pre_fn) (tree, void *),
  skip_bases:
   /* Call the post-order walking function.  */
   if (post_fn)
-    return post_fn (binfo, data);
+    {
+      rval = post_fn (binfo, data);
+      gcc_assert (rval != dfs_skip_bases);
+      return rval;
+    }
+
   return NULL_TREE;
 }
 
@@ -1546,7 +1565,7 @@ dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
   tree rval;
   unsigned ix;
   tree base_binfo;
-  
+
   /* Call the pre-order walking function.  */
   if (pre_fn)
     {
@@ -1555,7 +1574,7 @@ dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
        {
          if (rval == dfs_skip_bases)
            goto skip_bases;
-         
+
          return rval;
        }
     }
@@ -1569,29 +1588,33 @@ dfs_walk_once_r (tree binfo, tree (*pre_fn) (tree, void *),
            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)
-    return post_fn (binfo, data);
-  
+    {
+      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.  */
   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); ix++)
     {
@@ -1616,10 +1639,13 @@ 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.  */
@@ -1630,12 +1656,12 @@ dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
       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) *vbases;
+            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;
@@ -1643,6 +1669,9 @@ dfs_walk_once (tree binfo, tree (*pre_fn) (tree, void *),
       else
        dfs_unmark_r (binfo);
     }
+
+  active--;
+
   return rval;
 }
 
@@ -1668,7 +1697,7 @@ dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
        {
          if (rval == dfs_skip_bases)
            goto skip_bases;
-         
+
          return rval;
        }
     }
@@ -1680,13 +1709,21 @@ dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
 
       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
-         && !(friends_p && is_friend (BINFO_TYPE (binfo), current_scope ())))
-       continue;
+        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;
@@ -1696,12 +1733,16 @@ dfs_walk_once_accessible_r (tree binfo, bool friends_p, bool once,
       if (rval)
        return rval;
     }
-  
+
  skip_bases:
   /* Call the post-order walking function.  */
   if (post_fn)
-    return post_fn (binfo, data);
-  
+    {
+      rval = post_fn (binfo, data);
+      gcc_assert (rval != dfs_skip_bases);
+      return rval;
+    }
+
   return NULL_TREE;
 }
 
@@ -1717,18 +1758,18 @@ dfs_walk_once_accessible (tree binfo, bool friends_p,
   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) *vbases;
+            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;
@@ -1742,7 +1783,7 @@ dfs_walk_once_accessible (tree binfo, bool friends_p,
 /* 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);
@@ -1764,12 +1805,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);
        }
@@ -1778,7 +1819,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,
@@ -1797,6 +1838,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;
@@ -1809,38 +1856,60 @@ check_final_overrider (tree overrider, tree basefn)
     {
       if (fail == 1)
        {
-         cp_error_at ("invalid covariant return type for %q#D", overrider);
-         cp_error_at ("  overriding %q#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 %q#D",
-                      overrider);
-         cp_error_at ("  overriding %q#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 %q#F", overrider);
-      cp_error_at ("  overriding %q#F", basefn);
+      error ("looser throw specifier for %q+#F", overrider);
+      error ("  overriding %q+#F", basefn);
       DECL_INVALID_OVERRIDER_P (overrider) = 1;
       return 0;
     }
-  
+
+  /* Check for conflicting type attributes.  */
+  if (!targetm.comp_type_attributes (over_type, base_type))
+    {
+      error ("conflicting type attributes specified for %q+#D", overrider);
+      error ("  overriding %q+#D", basefn);
+      DECL_INVALID_OVERRIDER_P (overrider) = 1;
+      return 0;
+    }
+
+  if (DECL_DELETED_FN (basefn) != DECL_DELETED_FN (overrider))
+    {
+      if (DECL_DELETED_FN (overrider))
+       {
+         error ("deleted function %q+D", overrider);
+         error ("overriding non-deleted function %q+D", basefn);
+       }
+      else
+       {
+         error ("non-deleted function %q+D", overrider);
+         error ("overriding deleted function %q+D", basefn);
+       }
+      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.  */
 
@@ -1855,9 +1924,9 @@ look_for_overrides (tree type, tree fndecl)
   for (ix = 0; BINFO_BASE_ITERATE (binfo, ix, base_binfo); 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;
 }
@@ -1883,23 +1952,23 @@ look_for_overrides_here (tree type, tree fndecl)
   if (ix >= 0)
     {
       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;
        }
     }
@@ -1919,8 +1988,8 @@ look_for_overrides_r (tree type, tree fndecl)
        {
          /* A static member function cannot match an inherited
             virtual member function.  */
-         cp_error_at ("%q#D cannot be declared", fndecl);
-         cp_error_at ("  since %q#D declared in base class", fn);
+         error ("%q+#D cannot be declared", fndecl);
+         error ("  since %q+#D declared in base class", fn);
        }
       else
        {
@@ -1948,12 +2017,12 @@ 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)))
-         VEC_safe_push (tree, CLASSTYPE_PURE_VIRTUALS (type),
+         VEC_safe_push (tree, gc, CLASSTYPE_PURE_VIRTUALS (type),
                         BV_FN (virtuals));
     }
 
@@ -1993,6 +2062,10 @@ maybe_suppress_debug_info (tree t)
   /* We might have set this earlier in cp_finish_decl.  */
   TYPE_DECL_SUPPRESS_DEBUG (TYPE_MAIN_DECL (t)) = 0;
 
+  /* Always emit the information for each class every time. */
+  if (flag_emit_class_debug_always)
+    return;
+
   /* If we already know how we're handling this class, handle debug info
      the same way.  */
   if (CLASSTYPE_INTERFACE_KNOWN (t))
@@ -2103,17 +2176,17 @@ check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
   if (virtual_depth || virtualness)
     {
      /* In a virtual hierarchy, we could be hidden, or could hide a
-        conversion function on the other_convs list.  */
+       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 (!(virtual_depth || TREE_STATIC (level)))
-           /* Neither is morally virtual, so cannot hide each other. */
+           /* Neither is morally virtual, so cannot hide each other.  */
            continue;
-         
+
          if (!TREE_VALUE (level))
            /* They evaporated away already.  */
            continue;
@@ -2126,13 +2199,13 @@ check_hidden_convs (tree binfo, int virtual_depth, int virtualness,
          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;)
            {
              if (same_type_p (to_type, TREE_TYPE (other)))
                {
                  if (they_hide_us)
-                   /* We are hidden. */
+                   /* We are hidden.  */
                    return 0;
 
                  if (we_hide_them)
@@ -2166,12 +2239,12 @@ split_conversions (tree my_convs, tree parent_convs,
 {
   tree t;
   tree prev;
-  
+
   /* 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;
-  
+
   if (prev)
     TREE_CHAIN (prev) = NULL_TREE;
   else
@@ -2185,7 +2258,7 @@ split_conversions (tree my_convs, tree parent_convs,
     }
   else
     my_convs = child_convs;
-  
+
   return my_convs;
 }
 
@@ -2223,22 +2296,22 @@ lookup_conversions_r (tree binfo,
   tree child_tpl_convs = NULL_TREE;
   unsigned i;
   tree base_binfo;
-  VEC(tree) *method_vec = CLASSTYPE_METHOD_VEC (BINFO_TYPE (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;
-      
+
       return 0;
     }
-  
+
   if (BINFO_VIRTUAL_P (binfo))
     virtual_depth++;
-  
+
   /* First, locate the unhidden ones at this level.  */
-  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT; 
+  for (i = CLASSTYPE_FIRST_CONVERSION_SLOT;
        VEC_iterate (tree, method_vec, i, conv);
        ++i)
     {
@@ -2257,7 +2330,7 @@ lookup_conversions_r (tree binfo,
            {
              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))
                {
@@ -2278,7 +2351,7 @@ lookup_conversions_r (tree binfo,
          if (!IDENTIFIER_MARKED (name))
            {
              tree type = DECL_CONV_FN_TYPE (cur);
-             
+
              if (check_hidden_convs (binfo, virtual_depth, virtualness,
                                      type, parent_convs, other_convs))
                {
@@ -2301,17 +2374,17 @@ lookup_conversions_r (tree binfo,
       if (virtual_depth)
        TREE_STATIC (parent_convs) = 1;
     }
-  
+
   if (my_tpl_convs)
     {
       parent_tpl_convs = tree_cons (binfo, my_tpl_convs, parent_tpl_convs);
       if (virtual_depth)
-       TREE_STATIC (parent_convs) = 1;
+       TREE_STATIC (parent_tpl_convs) = 1;
     }
 
   child_convs = other_convs;
   child_tpl_convs = other_tpl_convs;
-  
+
   /* Now iterate over each base, looking for more conversions.  */
   for (i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
     {
@@ -2337,7 +2410,7 @@ lookup_conversions_r (tree binfo,
                              child_convs, other_convs);
   *tpl_convs = split_conversions (my_tpl_convs, parent_tpl_convs,
                                  child_tpl_convs, other_tpl_convs);
-  
+
   return my_virtualness;
 }
 
@@ -2348,22 +2421,25 @@ lookup_conversions_r (tree binfo,
    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.  */
+   field name.
+   If LOOKUP_TEMPLATE_CONVS_P is TRUE, the returned TREE_LIST contains
+   the non-hidden user-defined template conversion functions too.  */
 
 tree
-lookup_conversions (tree type)
+lookup_conversions (tree type,
+                   bool lookup_template_convs_p)
 {
   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);
-  
+
   /* Flatten the list-of-lists */
   for (; convs; convs = TREE_CHAIN (convs))
     {
@@ -2377,7 +2453,10 @@ lookup_conversions (tree type)
          list = probe;
        }
     }
-  
+
+  if (lookup_template_convs_p == false)
+    tpl_convs = NULL_TREE;
+
   for (; tpl_convs; tpl_convs = TREE_CHAIN (tpl_convs))
     {
       tree probe, next;
@@ -2390,7 +2469,7 @@ lookup_conversions (tree type)
          list = probe;
        }
     }
-  
+
   return list;
 }
 
@@ -2415,6 +2494,10 @@ binfo_from_vbase (tree binfo)
 tree
 binfo_via_virtual (tree binfo, tree 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))
     {
@@ -2432,7 +2515,7 @@ tree
 copied_binfo (tree binfo, tree here)
 {
   tree result = NULL_TREE;
-  
+
   if (BINFO_VIRTUAL_P (binfo))
     {
       tree t;
@@ -2448,7 +2531,7 @@ copied_binfo (tree binfo, tree here)
       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)))
@@ -2472,8 +2555,8 @@ binfo_for_vbase (tree base, tree t)
 {
   unsigned ix;
   tree binfo;
-  VEC (tree) *vbases;
-  
+  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))
@@ -2490,7 +2573,7 @@ tree
 original_binfo (tree binfo, tree here)
 {
   tree result = NULL;
-  
+
   if (SAME_BINFO_TYPE_P (BINFO_TYPE (binfo), BINFO_TYPE (here)))
     result = here;
   else if (BINFO_VIRTUAL_P (binfo))
@@ -2500,13 +2583,13 @@ original_binfo (tree binfo, tree here)
   else if (BINFO_INHERITANCE_CHAIN (binfo))
     {
       tree base_binfos;
-      
+
       base_binfos = original_binfo (BINFO_INHERITANCE_CHAIN (binfo), here);
       if (base_binfos)
        {
          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)))
@@ -2516,7 +2599,7 @@ original_binfo (tree binfo, tree here)
              }
        }
     }
-  
+
   return result;
 }