public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/49003 (DR 1207, use of 'this' in trailing return type)
@ 2011-06-29 21:36 Jason Merrill
  2011-07-04 21:41 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2011-06-29 21:36 UTC (permalink / raw)
  To: gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 168 bytes --]

This patch adds support for use of 'this' (implicitly or explicitly) in 
the trailing-return-type of a member function.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: 49003.patch --]
[-- Type: text/x-patch, Size: 5638 bytes --]

commit 8154a9854f1d5ff3407af1707d112899eb39f013
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jun 29 15:19:46 2011 -0400

    	DR 1207
    	PR c++/49003
    	* cp-tree.h (struct saved_scope): Add x_current_class_ptr,
    	x_current_class_ref.
    	(current_class_ptr, current_class_ref): Use them.
    	* decl.c (build_this_parm): Handle getting the class type.
    	* parser.c (cp_parser_late_return_type_opt): Set up 'this'
    	for use within the trailing return type.

diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 55c88e3..ef25c97 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -953,6 +953,10 @@ struct GTY(()) saved_scope {
   struct cp_binding_level *x_previous_class_level;
   tree x_saved_tree;
 
+  /* Only used for uses of this in trailing return type.  */
+  tree x_current_class_ptr;
+  tree x_current_class_ref;
+
   int x_processing_template_decl;
   int x_processing_specialization;
   BOOL_BITFIELD x_processing_explicit_instantiation : 1;
@@ -1070,12 +1074,14 @@ struct GTY(()) language_function {
    PARM_DECL for the `this' pointer.  The current_class_ref is an
    expression for `*this'.  */
 
-#define current_class_ptr \
-  (cfun && cp_function_chain					\
-   ? cp_function_chain->x_current_class_ptr : NULL_TREE)
-#define current_class_ref \
-  ((cfun && cp_function_chain)                                  \
-   ? cp_function_chain->x_current_class_ref : NULL_TREE)
+#define current_class_ptr			\
+  (*(cfun && cp_function_chain			\
+     ? &cp_function_chain->x_current_class_ptr	\
+     : &scope_chain->x_current_class_ptr))
+#define current_class_ref			\
+  (*(cfun && cp_function_chain			\
+     ? &cp_function_chain->x_current_class_ref	\
+     : &scope_chain->x_current_class_ref))
 
 /* The EH_SPEC_BLOCK for the exception-specifiers for the current
    function, if any.  */
diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index b8435a6..94d686d 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -7001,7 +7001,14 @@ build_this_parm (tree type, cp_cv_quals quals)
   tree parm;
   cp_cv_quals this_quals;
 
-  this_type = type_of_this_parm (type);
+  if (CLASS_TYPE_P (type))
+    {
+      this_type
+	= cp_build_qualified_type (type, quals & ~TYPE_QUAL_RESTRICT);
+      this_type = build_pointer_type (this_type);
+    }
+  else
+    this_type = type_of_this_parm (type);
   /* The `this' parameter is implicitly `const'; it cannot be
      assigned to.  */
   this_quals = (quals & TYPE_QUAL_RESTRICT) | TYPE_QUAL_CONST;
@@ -12675,6 +12682,7 @@ start_preparsed_function (tree decl1, tree attrs, int flags)
 
       cp_function_chain->x_current_class_ref
 	= cp_build_indirect_ref (t, RO_NULL, tf_warning_or_error);
+      /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
       cp_function_chain->x_current_class_ptr = t;
 
       /* Constructors and destructors need to know whether they're "in
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index f1b7976..d79326d 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -1696,7 +1696,7 @@ static cp_cv_quals cp_parser_cv_qualifier_seq_opt
 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
   (cp_parser *);
 static tree cp_parser_late_return_type_opt
-  (cp_parser *);
+  (cp_parser *, cp_cv_quals);
 static tree cp_parser_declarator_id
   (cp_parser *, bool);
 static tree cp_parser_type_id
@@ -14968,7 +14968,7 @@ cp_parser_direct_declarator (cp_parser* parser,
 		  virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
 
 		  late_return
-		    = cp_parser_late_return_type_opt (parser);
+		    = cp_parser_late_return_type_opt (parser, cv_quals);
 
 		  /* Create the function-declarator.  */
 		  declarator = make_call_declarator (declarator,
@@ -15537,9 +15537,10 @@ cp_parser_virt_specifier_seq_opt (cp_parser* parser)
    Returns the type indicated by the type-id.  */
 
 static tree
-cp_parser_late_return_type_opt (cp_parser* parser)
+cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
 {
   cp_token *token;
+  tree type;
 
   /* Peek at the next token.  */
   token = cp_lexer_peek_token (parser->lexer);
@@ -15550,7 +15551,23 @@ cp_parser_late_return_type_opt (cp_parser* parser)
   /* Consume the ->.  */
   cp_lexer_consume_token (parser->lexer);
 
-  return cp_parser_trailing_type_id (parser);
+  if (current_class_type)
+    {
+      /* DR 1207: 'this' is in scope in the trailing return type.  */
+      tree this_parm = build_this_parm (current_class_type, quals);
+      gcc_assert (current_class_ptr == NULL_TREE);
+      current_class_ref
+	= cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
+      /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
+      current_class_ptr = this_parm;
+    }
+
+  type = cp_parser_trailing_type_id (parser);
+
+  if (current_class_type)
+    current_class_ptr = current_class_ref = NULL_TREE;
+
+  return type;
 }
 
 /* Parse a declarator-id.
diff --git a/gcc/testsuite/g++.dg/cpp0x/pr45908.C b/gcc/testsuite/g++.dg/cpp0x/pr45908.C
index 3a85088..45399b5 100644
--- a/gcc/testsuite/g++.dg/cpp0x/pr45908.C
+++ b/gcc/testsuite/g++.dg/cpp0x/pr45908.C
@@ -14,5 +14,5 @@ struct vector {
 class block {
     vector v;
     auto end() const -> decltype(v.begin())
-    { return v.begin(); } // { dg-error "could not convert" }
+    { return v.begin(); }
 };
diff --git a/gcc/testsuite/g++.dg/cpp0x/trailing6.C b/gcc/testsuite/g++.dg/cpp0x/trailing6.C
new file mode 100644
index 0000000..3476e90
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/trailing6.C
@@ -0,0 +1,6 @@
+// PR c++/49003
+// { dg-options -std=c++0x }
+
+struct A {
+    auto a() const -> decltype(this) { return this; }
+};

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: C++ PATCH for c++/49003 (DR 1207, use of 'this' in trailing return type)
  2011-06-29 21:36 C++ PATCH for c++/49003 (DR 1207, use of 'this' in trailing return type) Jason Merrill
@ 2011-07-04 21:41 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2011-07-04 21:41 UTC (permalink / raw)
  To: gcc-patches List

[-- Attachment #1: Type: text/plain, Size: 343 bytes --]

On 06/29/2011 05:15 PM, Jason Merrill wrote:
> This patch adds support for use of 'this' (implicitly or explicitly) in
> the trailing-return-type of a member function.

The above patch wasn't enough, though.  The following patch fixes some 
issues that arose with real uses, including mangling.

Tested x86_64-pc-linux-gnu, applying to trunk.

[-- Attachment #2: this-mangle.patch --]
[-- Type: text/x-patch, Size: 2959 bytes --]

commit ef43a979a3f46150f383b9deab70dd412d66f96b
Author: Jason Merrill <jason@redhat.com>
Date:   Sun Jul 3 17:14:56 2011 -0400

    	DR 1207
    	PR c++/49589
    	* mangle.c (write_expression): Handle 'this'.
    	* parser.c (cp_parser_postfix_dot_deref_expression): Allow
    	incomplete *this.
    	* semantics.c (potential_constant_expression_1): Check that
    	DECL_CONTEXT is set on 'this'.

diff --git a/gcc/cp/mangle.c b/gcc/cp/mangle.c
index 134c9ea..81b772f 100644
--- a/gcc/cp/mangle.c
+++ b/gcc/cp/mangle.c
@@ -2495,6 +2495,11 @@ write_expression (tree expr)
   else if (TREE_CODE_CLASS (code) == tcc_constant
 	   || (abi_version_at_least (2) && code == CONST_DECL))
     write_template_arg_literal (expr);
+  else if (code == PARM_DECL && DECL_ARTIFICIAL (expr))
+    {
+      gcc_assert (!strcmp ("this", IDENTIFIER_POINTER (DECL_NAME (expr))));
+      write_string ("fpT");
+    }
   else if (code == PARM_DECL)
     {
       /* A function parameter used in a late-specified return type.  */
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index d79326d..6bb15ed 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -5281,7 +5281,11 @@ cp_parser_postfix_dot_deref_expression (cp_parser *parser,
 		    postfix_expression);
 	  scope = NULL_TREE;
 	}
-      else
+      /* Unlike the object expression in other contexts, *this is not
+	 required to be of complete type for purposes of class member
+	 access (5.2.5) outside the member function body.  */
+      else if (scope != current_class_ref
+	       && !(processing_template_decl && scope == current_class_type))
 	scope = complete_type_or_else (scope, NULL_TREE);
       /* Let the name lookup machinery know that we are processing a
 	 class member access expression.  */
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index e29705c..619c058 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -7791,7 +7791,8 @@ potential_constant_expression_1 (tree t, bool want_rval, tsubst_flags_t flags)
         STRIP_NOPS (x);
         if (is_this_parameter (x))
 	  {
-	    if (DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)) && want_rval)
+	    if (want_rval && DECL_CONTEXT (x)
+		&& DECL_CONSTRUCTOR_P (DECL_CONTEXT (x)))
 	      {
 		if (flags & tf_error)
 		  sorry ("use of the value of the object being constructed "
diff --git a/gcc/testsuite/g++.dg/abi/mangle48.C b/gcc/testsuite/g++.dg/abi/mangle48.C
new file mode 100644
index 0000000..dc9c492
--- /dev/null
+++ b/gcc/testsuite/g++.dg/abi/mangle48.C
@@ -0,0 +1,23 @@
+// Testcase for 'this' mangling
+// { dg-options -std=c++0x }
+
+struct B
+{
+  template <class U> U f();
+};
+
+struct A
+{
+  B b;
+  // { dg-final { scan-assembler "_ZN1A1fIiEEDTcldtdtdefpT1b1fIT_EEEv" } }
+  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>());
+};
+
+int main()
+{
+  A a;
+  a.f<int>();
+  a.g<int>();
+}

[-- Attachment #3: this-demangle.patch --]
[-- Type: text/x-patch, Size: 2098 bytes --]

commit acbc60694bf95f13f9088ed4d5b3d18780aaf754
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Jul 4 10:44:29 2011 -0400

    	* cp-demangle.c (d_expression): Handle 'this'.
    	(d_print_comp) [DEMANGLE_COMPONENT_FUNCTION_PARAM]: Likewise.

diff --git a/libiberty/cp-demangle.c b/libiberty/cp-demangle.c
index f136322..29badbb 100644
--- a/libiberty/cp-demangle.c
+++ b/libiberty/cp-demangle.c
@@ -2738,10 +2738,18 @@ d_expression (struct d_info *di)
       /* Function parameter used in a late-specified return type.  */
       int index;
       d_advance (di, 2);
-      index = d_compact_number (di);
-      if (index < 0)
-	return NULL;
-
+      if (d_peek_char (di) == 'T')
+	{
+	  /* 'this' parameter.  */
+	  d_advance (di, 1);
+	  index = 0;
+	}
+      else
+	{
+	  index = d_compact_number (di) + 1;
+	  if (index == 0)
+	    return NULL;
+	}
       return d_make_function_param (di, index);
     }
   else if (IS_DIGIT (peek)
@@ -4400,9 +4408,17 @@ d_print_comp (struct d_print_info *dpi, int options,
       return;
 
     case DEMANGLE_COMPONENT_FUNCTION_PARAM:
-      d_append_string (dpi, "{parm#");
-      d_append_num (dpi, dc->u.s_number.number + 1);
-      d_append_char (dpi, '}');
+      {
+	long num = dc->u.s_number.number;
+	if (num == 0)
+	  d_append_string (dpi, "this");
+	else
+	  {
+	    d_append_string (dpi, "{parm#");
+	    d_append_num (dpi, num);
+	    d_append_char (dpi, '}');
+	  }
+      }
       return;
 
     case DEMANGLE_COMPONENT_GLOBAL_CONSTRUCTORS:
diff --git a/libiberty/testsuite/demangle-expected b/libiberty/testsuite/demangle-expected
index 4980cf1..2dc74be 100644
--- a/libiberty/testsuite/demangle-expected
+++ b/libiberty/testsuite/demangle-expected
@@ -3905,6 +3905,10 @@ decltype ({parm#1}+{parm#2}) add<int, double>(int, double)
 --format=gnu-v3
 _Z4add3IidEDTclL_Z1gEfp_fp0_EET_T0_
 decltype (g({parm#1}, {parm#2})) add3<int, double>(int, double)
+# 'this' test
+--format=gnu-v3
+_ZN1A1fIiEEDTcldtdtdefpT1b1fIT_EEEv
+decltype ((((*this).b).(f<int>))()) A::f<int>()
 # new (2008) built in types test
 --format=gnu-v3
 _Z1fDfDdDeDhDsDi

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2011-07-04 21:41 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-06-29 21:36 C++ PATCH for c++/49003 (DR 1207, use of 'this' in trailing return type) Jason Merrill
2011-07-04 21:41 ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).