commit 9448b41e5f5a64a2a92519049898fba4c9d43633
Author: Jason Merrill <jason@redhat.com>
Date:   Sat Apr 13 21:12:45 2013 +0200

    	PR c++/52748
    	* pt.c (tsubst) [DECLTYPE_TYPE]: If ~id is an expression
    	rather than a destructor name, it isn't an unqualified-name.
    	(tsubst_copy_and_build): Pass down decltype_flag to operator
    	handling code, too.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 0c7b2ed..5c960e0 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -11800,8 +11800,17 @@ tsubst (tree t, tree args, tsubst_flags_t complain, tree in_decl)
 	else if (DECLTYPE_FOR_LAMBDA_PROXY (t))
 	  type = lambda_proxy_type (type);
 	else
-	  type = finish_decltype_type
-	    (type, DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t), complain);
+	  {
+	    bool id = DECLTYPE_TYPE_ID_EXPR_OR_MEMBER_ACCESS_P (t);
+	    if (id && TREE_CODE (DECLTYPE_TYPE_EXPR (t)) == BIT_NOT_EXPR
+		&& EXPR_P (type))
+	      /* In a template ~id could be either a complement expression
+		 or an unqualified-id naming a destructor; if instantiating
+		 it produces an expression, it's not an id-expression or
+		 member access.  */
+	      id = false;
+	    type = finish_decltype_type (type, id, complain);
+	  }
 	return cp_build_qualified_type_real (type,
 					     cp_type_quals (t)
 					     | cp_type_quals (type),
@@ -13427,9 +13436,8 @@ tsubst_copy_and_build (tree t,
 
   /* N3276 decltype magic only applies to calls at the top level or on the
      right side of a comma.  */
-  if (TREE_CODE (t) != CALL_EXPR
-      && TREE_CODE (t) != COMPOUND_EXPR)
-    complain &= ~tf_decltype;
+  tsubst_flags_t decltype_flag = (complain & tf_decltype);
+  complain &= ~tf_decltype;
 
   switch (TREE_CODE (t))
     {
@@ -13517,7 +13525,8 @@ tsubst_copy_and_build (tree t,
 	      r = convert_from_reference (r);
 	  }
 	else
-	  r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR, complain);
+	  r = build_x_indirect_ref (input_location, r, RO_UNARY_STAR,
+				    complain|decltype_flag);
 	RETURN (r);
       }
 
@@ -13594,7 +13603,8 @@ tsubst_copy_and_build (tree t,
     case POSTINCREMENT_EXPR:
       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
 						args, complain, in_decl);
-      RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1, complain));
+      RETURN (build_x_unary_op (input_location, TREE_CODE (t), op1,
+				complain|decltype_flag));
 
     case PREDECREMENT_EXPR:
     case PREINCREMENT_EXPR:
@@ -13606,7 +13616,8 @@ tsubst_copy_and_build (tree t,
     case REALPART_EXPR:
     case IMAGPART_EXPR:
       RETURN (build_x_unary_op (input_location, TREE_CODE (t),
-			       RECUR (TREE_OPERAND (t, 0)), complain));
+			       RECUR (TREE_OPERAND (t, 0)),
+				complain|decltype_flag));
 
     case FIX_TRUNC_EXPR:
       RETURN (cp_build_unary_op (FIX_TRUNC_EXPR, RECUR (TREE_OPERAND (t, 0)),
@@ -13623,7 +13634,8 @@ tsubst_copy_and_build (tree t,
       else
 	op1 = tsubst_non_call_postfix_expression (op1, args, complain,
 						  in_decl);
-      RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1, complain));
+      RETURN (build_x_unary_op (input_location, ADDR_EXPR, op1,
+				complain|decltype_flag));
 
     case PLUS_EXPR:
     case MINUS_EXPR:
@@ -13672,7 +13684,7 @@ tsubst_copy_and_build (tree t,
 	    ? ERROR_MARK
 	    : TREE_CODE (TREE_OPERAND (t, 1))),
 	   /*overload=*/NULL,
-	   complain);
+	   complain|decltype_flag);
 	if (EXPR_P (r) && TREE_NO_WARNING (t))
 	  TREE_NO_WARNING (r) = TREE_NO_WARNING (t);
 
@@ -13688,7 +13700,8 @@ tsubst_copy_and_build (tree t,
       op1 = tsubst_non_call_postfix_expression (TREE_OPERAND (t, 0),
 						args, complain, in_decl);
       RETURN (build_x_array_ref (EXPR_LOCATION (t), op1,
-				RECUR (TREE_OPERAND (t, 1)), complain));
+				 RECUR (TREE_OPERAND (t, 1)),
+				 complain|decltype_flag));
 
     case SIZEOF_EXPR:
       if (PACK_EXPANSION_P (TREE_OPERAND (t, 0)))
@@ -13781,7 +13794,7 @@ tsubst_copy_and_build (tree t,
 	   RECUR (TREE_OPERAND (t, 0)),
 	   TREE_CODE (TREE_OPERAND (t, 1)),
 	   RECUR (TREE_OPERAND (t, 2)),
-	   complain);
+	   complain|decltype_flag);
 	/* TREE_NO_WARNING must be set if either the expression was
 	   parenthesized or it uses an operator such as >>= rather
 	   than plain assignment.  In the former case, it was already
@@ -13870,7 +13883,7 @@ tsubst_copy_and_build (tree t,
 	RETURN (build_x_compound_expr (EXPR_LOCATION (t),
 				       op0,
 				       RECUR (TREE_OPERAND (t, 1)),
-				       complain));
+				       complain|decltype_flag));
       }
 
     case CALL_EXPR:
@@ -13882,10 +13895,6 @@ tsubst_copy_and_build (tree t,
 	bool koenig_p;
 	tree ret;
 
-	/* Don't pass tf_decltype down to subexpressions.  */
-	tsubst_flags_t decltype_flag = (complain & tf_decltype);
-	complain &= ~tf_decltype;
-
 	function = CALL_EXPR_FN (t);
 	/* When we parsed the expression,  we determined whether or
 	   not Koenig lookup should be performed.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/decltype-call3.C b/gcc/testsuite/g++.dg/cpp0x/decltype-call3.C
index 1d12b01..27797a2 100644
--- a/gcc/testsuite/g++.dg/cpp0x/decltype-call3.C
+++ b/gcc/testsuite/g++.dg/cpp0x/decltype-call3.C
@@ -43,6 +43,50 @@ A operator==(B,B);
 A operator->*(B,B);
 
 #define TRY(E) static_cast<decltype(E)*>(0)
+
+template <class B>
+void f()
+{
+  B b;
+  TRY(b(0));
+  TRY(b[0]);
+  TRY(b=0);
+  TRY(b+=0);
+  TRY(b-=0);
+  TRY(b*=0);
+  TRY(b/=0);
+  TRY(b^=0);
+  TRY(b&=0);
+  TRY(b|=0);
+  TRY(b<<=0);
+  TRY(b>>=0);
+
+  TRY(-b);
+  TRY(+b);
+  TRY(*b);
+  TRY(&b);
+  TRY(!b);
+  TRY(~b);
+  TRY(++b);
+  TRY(--b);
+
+  TRY(b+b);
+  TRY(b-b);
+  TRY(b*b);
+  TRY(b/b);
+  TRY(b%b);
+  TRY(b^b);
+  TRY(b&b);
+  TRY(b|b);
+  TRY(b>b);
+  TRY(b<b);
+  TRY((b,b));
+  TRY(b<<b);
+  TRY(b>>b);
+  TRY(b==b);
+  TRY(b->*b);
+}
+
 int main()
 {
   B b;
@@ -83,4 +127,6 @@ int main()
   TRY(b>>b);
   TRY(b==b);
   TRY(b->*b);
+
+  f<B>();
 }
