Patchwork C++ PATCH to implement C++11 non-static data member initializers

login
register
mail settings
Submitter Jason Merrill
Date Sept. 26, 2011, 1:59 p.m.
Message ID <4E808553.6090203@redhat.com>
Download mbox | patch
Permalink /patch/116425/
State New
Headers show

Comments

Jason Merrill - Sept. 26, 2011, 1:59 p.m.
On 09/25/2011 11:57 PM, Jason Merrill wrote:
> Ville pointed out that my earlier patch failed to support 'this' in
> NSDMIs. So this patch implements that.

...and this one fixes 'this' in templates.

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

Patch

commit 7893474d771c6449e05f8c85b3e9e418b9c72a57
Author: Jason Merrill <jason@redhat.com>
Date:   Mon Sep 26 09:19:03 2011 -0400

    	* pt.c (tsubst_copy) [PARM_DECL]: Handle 'this' in NSDMI.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index ebadebf..b24b592 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -11740,6 +11740,14 @@  tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
       if (r == NULL)
 	{
 	  tree c;
+
+	  /* We get here for a use of 'this' in an NSDMI.  */
+	  if (DECL_NAME (t) == this_identifier
+	      && at_function_scope_p ()
+	      && current_function_decl
+	      && DECL_CONSTRUCTOR_P (current_function_decl))
+	    return current_class_ptr;
+
 	  /* This can happen for a parameter name used later in a function
 	     declaration (such as in a late-specified return type).  Just
 	     make a dummy decl, since it's only used for its type.  */
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-defer4.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-defer4.C
index 68c8380..65b2e73 100644
--- a/gcc/testsuite/g++.dg/cpp0x/nsdmi-defer4.C
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-defer4.C
@@ -1,4 +1,5 @@ 
 // { dg-options -std=c++0x }
+// { dg-do run }
 
 struct A
 {
diff --git a/gcc/testsuite/g++.dg/cpp0x/nsdmi-template1.C b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template1.C
new file mode 100644
index 0000000..04f1e03
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/nsdmi-template1.C
@@ -0,0 +1,21 @@ 
+// { dg-do run }
+// { dg-options -std=c++0x }
+
+struct base
+{
+  int calc_avg() { return 42; }
+};
+
+template <class T> struct nsdmi : T
+{
+  nsdmi() {}
+  int avg() { return avg_; }
+  int avg_ = this->calc_avg();
+};
+
+int main()
+{
+  nsdmi<base> x;
+  if (x.avg() != 42)
+    __builtin_abort();
+}