commit 15b9202da53e0bd0c2be55f2f3a91c7de3484cbe
Author: Jason Merrill <jason@redhat.com>
Date:   Sat Jan 7 06:15:05 2012 -0500

    	PR c++/48051
    	* mangle.c (write_expression): Mangle BASELINK scope if
    	BASELINK_QUALIFIED_P.
    	* search.c (adjust_result_of_qualified_name_lookup): Set
    	BASELINK_QUALIFIED_P.
    	* tree.c (cp_tree_equal) [BASELINK]: Compare BASELINK_QUALIFIED_P.
    	* parser.c (cp_parser_postfix_dot_deref_expression): Don't call
    	adjust_result_of_qualified_name_lookup for non-qualified names.

diff --git a/gcc/cp/mangle.c b/gcc/cp/mangle.c
index 60b1870..5f2fa15 100644
--- a/gcc/cp/mangle.c
+++ b/gcc/cp/mangle.c
@@ -2500,7 +2500,9 @@ write_expression (tree expr)
       code = TREE_CODE (expr);
     }
 
-  if (code == BASELINK)
+  if (code == BASELINK
+      && (!type_unknown_p (expr)
+	  || !BASELINK_QUALIFIED_P (expr)))
     {
       expr = BASELINK_FUNCTIONS (expr);
       code = TREE_CODE (expr);
@@ -2583,10 +2585,20 @@ write_expression (tree expr)
       write_string ("at");
       write_type (TREE_OPERAND (expr, 0));
     }
-  else if (TREE_CODE (expr) == SCOPE_REF)
+  else if (code == SCOPE_REF
+	   || code == BASELINK)
     {
-      tree scope = TREE_OPERAND (expr, 0);
-      tree member = TREE_OPERAND (expr, 1);
+      tree scope, member;
+      if (code == SCOPE_REF)
+	{
+	  scope = TREE_OPERAND (expr, 0);
+	  member = TREE_OPERAND (expr, 1);
+	}
+      else
+	{
+	  scope = BINFO_TYPE (BASELINK_ACCESS_BINFO (expr));
+	  member = BASELINK_FUNCTIONS (expr);
+	}
 
       if (!abi_version_at_least (2) && DECL_P (member))
 	{
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index f9e1a13..4c85355 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -6045,9 +6045,9 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser,
 	      parser->qualifying_scope = NULL_TREE;
 	      parser->object_scope = NULL_TREE;
 	    }
-	  if (scope && name && BASELINK_P (name))
+	  if (parser->scope && name && BASELINK_P (name))
 	    adjust_result_of_qualified_name_lookup
-	      (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
+	      (name, parser->scope, scope);
 	  postfix_expression
 	    = finish_class_member_access_expr (postfix_expression, name,
 					       template_p, 
diff --git a/gcc/cp/search.c b/gcc/cp/search.c
index 45fdafc..e48dcec 100644
--- a/gcc/cp/search.c
+++ b/gcc/cp/search.c
@@ -1550,6 +1550,9 @@ adjust_result_of_qualified_name_lookup (tree decl,
 	}
     }
 
+  if (BASELINK_P (decl))
+    BASELINK_QUALIFIED_P (decl) = true;
+
   return decl;
 }
 
diff --git a/gcc/cp/tree.c b/gcc/cp/tree.c
index 8ef3e25..bf8bc05 100644
--- a/gcc/cp/tree.c
+++ b/gcc/cp/tree.c
@@ -2320,6 +2320,7 @@ cp_tree_equal (tree t1, tree t2)
     case BASELINK:
       return (BASELINK_BINFO (t1) == BASELINK_BINFO (t2)
 	      && BASELINK_ACCESS_BINFO (t1) == BASELINK_ACCESS_BINFO (t2)
+	      && BASELINK_QUALIFIED_P (t1) == BASELINK_QUALIFIED_P (t2)
 	      && cp_tree_equal (BASELINK_FUNCTIONS (t1),
 				BASELINK_FUNCTIONS (t2)));
 
diff --git a/gcc/testsuite/g++.dg/abi/mangle48.C b/gcc/testsuite/g++.dg/abi/mangle48.C
index dc9c492..6c0e99c 100644
--- a/gcc/testsuite/g++.dg/abi/mangle48.C
+++ b/gcc/testsuite/g++.dg/abi/mangle48.C
@@ -13,6 +13,10 @@ struct A
   template <class U> auto f() -> decltype (b.f<U>());
   // { dg-final { scan-assembler "_ZN1A1gIiEEDTcldtptfpT1b1fIT_EEEv" } }
   template <class U> auto g() -> decltype (this->b.f<U>());
+  // { dg-final { scan-assembler "_ZN1A1hIiEEDTcldtdtdefpT1bsr1B1fIT_EEEv" } }
+  template <class U> auto h() -> decltype (b.B::f<U>());
+  // { dg-final { scan-assembler "_ZN1A1iIiEEDTcldtptfpT1bsr1B1fIT_EEEv" } }
+  template <class U> auto i() -> decltype (this->b.B::f<U>());
 };
 
 int main()
@@ -20,4 +24,6 @@ int main()
   A a;
   a.f<int>();
   a.g<int>();
+  a.h<int>();
+  a.i<int>();
 }
diff --git a/gcc/testsuite/g++.dg/abi/mangle58.C b/gcc/testsuite/g++.dg/abi/mangle58.C
index 14e5543..54b16f2 100644
--- a/gcc/testsuite/g++.dg/abi/mangle58.C
+++ b/gcc/testsuite/g++.dg/abi/mangle58.C
@@ -6,14 +6,20 @@ struct B {
   static int cmp2(char a, char b);
   // { dg-final { scan-assembler "_ZN1B1fIcEEvR1AIT_X4cmp1EE" } }
   template <typename T> static void f (A<T,cmp1> &);
+  // { dg-final { scan-assembler "_ZN1B1gIcEEvR1AIT_XsrS_4cmp1EE" } }
+  template <typename T> static void g (A<T,B::cmp1> &);
   // { dg-final { scan-assembler "_ZN1B1fIcEEvR1AIT_L_ZNS_4cmp2EccEE" } }
   template <typename T> static void f (A<T,cmp2> &);
+  // { dg-final { scan-assembler "_ZN1B1gIcEEvR1AIT_L_ZNS_4cmp2EccEE" } }
+  template <typename T> static void g (A<T,B::cmp2> &);
 };
 
 void g()
 {
   A<char,B::cmp1> a;
   B::f(a);
+  B::g(a);
   A<char,B::cmp2> a2;
   B::f(a2);
+  B::g(a2);
 }
