public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: recognize in-class var tmpl partial spec [PR71954]
@ 2023-08-10 16:08 Patrick Palka
  2023-08-10 21:16 ` Jason Merrill
  0 siblings, 1 reply; 2+ messages in thread
From: Patrick Palka @ 2023-08-10 16:08 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk?

-- >8 --

This makes us recognize variable template partial specializations
defined directly inside the class body.  It seems to suffice to call
check_explicit_specialization when we see a static TEMPLATE_ID_EXPR data
member, which sets SET_DECL_TEMPLATE_SPECIALIZATION and which we
otherwise don't call (for out-of-class partial specializations we call
it from from grokvardecl which is used only for out-of-class definitions).
We also need to make finish_member_template_decl return NULL_TREE for
such partial specializations (matching its behavior class template
partial specializations) so that its caller doesn't try to call
finish_member_declaration on it.

	PR c++/71954

gcc/cp/ChangeLog:

	* decl.cc (grokdeclarator): Use 'dname' instead of
	'unqualified_id' when building the VAR_DECL for a static data
	member.  Call check_explicit_specialization for a
	TEMPLATE_ID_EXPR such member.
	* pt.cc (finish_member_template_decl): Return NULL_TREE
	instead of 'decl' when DECL_TEMPLATE_SPECIALIZATION is not
	set.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1y/var-templ84.C: New test.
	* g++.dg/cpp1y/var-templ84a.C: New test.
---
 gcc/cp/decl.cc                            | 11 ++++++++++-
 gcc/cp/pt.cc                              |  2 +-
 gcc/testsuite/g++.dg/cpp1y/var-templ84.C  | 12 ++++++++++++
 gcc/testsuite/g++.dg/cpp1y/var-templ84a.C | 19 +++++++++++++++++++
 4 files changed, 42 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84.C
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84a.C

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 9fe3a0b98fd..2b3fb313166 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -14440,7 +14440,16 @@ grokdeclarator (const cp_declarator *declarator,
 		/* C++ allows static class members.  All other work
 		   for this is done by grokfield.  */
 		decl = build_lang_decl_loc (id_loc, VAR_DECL,
-					    unqualified_id, type);
+					    dname, type);
+		if (unqualified_id
+		    && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
+		  {
+		    decl = check_explicit_specialization (unqualified_id, decl,
+							  template_count,
+							  concept_p * 8);
+		    if (decl == error_mark_node)
+		      return error_mark_node;
+		  }
 		set_linkage_for_static_data_member (decl);
 		if (concept_p)
 		  error_at (declspecs->locations[ds_concept],
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 2706fa619bf..a4809f034dc 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -314,7 +314,7 @@ finish_member_template_decl (tree decl)
 	  return DECL_TI_TEMPLATE (decl);
 	}
       else
-	return decl;
+	return NULL_TREE;
     }
   else
     error_at (DECL_SOURCE_LOCATION (decl),
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C
new file mode 100644
index 00000000000..39c2a0dc0cc
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C
@@ -0,0 +1,12 @@
+// PR c++/71954
+// { dg-do compile { target c++14 } }
+
+struct A {
+  template<class T> static const int var = 0;
+  template<class T> static const int var<T*> = 1;
+  template<class T> static const int var<const T*> = 2;
+};
+
+static_assert(A::var<int> == 0, "");
+static_assert(A::var<int*> == 1, "");
+static_assert(A::var<const int*> == 2, "");
diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
new file mode 100644
index 00000000000..4aa3a2a7245
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
@@ -0,0 +1,19 @@
+// PR c++/71954
+// A version of var-templ84.C where the partial specializations depend on
+// outer template parameters.
+// { dg-do compile { target c++14 } }
+
+template<class T>
+struct A {
+  template<class U, class V> static const int var = 0;
+  template<class U> static const int var<U*, T> = 1;
+  template<class U> static const int var<const U*, T> = 2;
+};
+
+static_assert(A<int>::var<int, int> == 0, "");
+static_assert(A<int>::var<int*, int> == 1, "");
+static_assert(A<int>::var<const int*, int> == 2, "");
+
+static_assert(A<int>::var<int, char> == 0, "");
+static_assert(A<int>::var<int*, char> == 0, "");
+static_assert(A<int>::var<const int*, char> == 0, "");
-- 
2.42.0.rc0.25.ga82fb66fed


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

* Re: [PATCH] c++: recognize in-class var tmpl partial spec [PR71954]
  2023-08-10 16:08 [PATCH] c++: recognize in-class var tmpl partial spec [PR71954] Patrick Palka
@ 2023-08-10 21:16 ` Jason Merrill
  0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2023-08-10 21:16 UTC (permalink / raw)
  To: Patrick Palka, gcc-patches

On 8/10/23 12:08, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?

OK.

> -- >8 --
> 
> This makes us recognize variable template partial specializations
> defined directly inside the class body.  It seems to suffice to call
> check_explicit_specialization when we see a static TEMPLATE_ID_EXPR data
> member, which sets SET_DECL_TEMPLATE_SPECIALIZATION and which we
> otherwise don't call (for out-of-class partial specializations we call
> it from from grokvardecl which is used only for out-of-class definitions).
> We also need to make finish_member_template_decl return NULL_TREE for
> such partial specializations (matching its behavior class template
> partial specializations) so that its caller doesn't try to call
> finish_member_declaration on it.
> 
> 	PR c++/71954
> 
> gcc/cp/ChangeLog:
> 
> 	* decl.cc (grokdeclarator): Use 'dname' instead of
> 	'unqualified_id' when building the VAR_DECL for a static data
> 	member.  Call check_explicit_specialization for a
> 	TEMPLATE_ID_EXPR such member.
> 	* pt.cc (finish_member_template_decl): Return NULL_TREE
> 	instead of 'decl' when DECL_TEMPLATE_SPECIALIZATION is not
> 	set.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp1y/var-templ84.C: New test.
> 	* g++.dg/cpp1y/var-templ84a.C: New test.
> ---
>   gcc/cp/decl.cc                            | 11 ++++++++++-
>   gcc/cp/pt.cc                              |  2 +-
>   gcc/testsuite/g++.dg/cpp1y/var-templ84.C  | 12 ++++++++++++
>   gcc/testsuite/g++.dg/cpp1y/var-templ84a.C | 19 +++++++++++++++++++
>   4 files changed, 42 insertions(+), 2 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84.C
>   create mode 100644 gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
> 
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 9fe3a0b98fd..2b3fb313166 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -14440,7 +14440,16 @@ grokdeclarator (const cp_declarator *declarator,
>   		/* C++ allows static class members.  All other work
>   		   for this is done by grokfield.  */
>   		decl = build_lang_decl_loc (id_loc, VAR_DECL,
> -					    unqualified_id, type);
> +					    dname, type);
> +		if (unqualified_id
> +		    && TREE_CODE (unqualified_id) == TEMPLATE_ID_EXPR)
> +		  {
> +		    decl = check_explicit_specialization (unqualified_id, decl,
> +							  template_count,
> +							  concept_p * 8);
> +		    if (decl == error_mark_node)
> +		      return error_mark_node;
> +		  }
>   		set_linkage_for_static_data_member (decl);
>   		if (concept_p)
>   		  error_at (declspecs->locations[ds_concept],
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 2706fa619bf..a4809f034dc 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -314,7 +314,7 @@ finish_member_template_decl (tree decl)
>   	  return DECL_TI_TEMPLATE (decl);
>   	}
>         else
> -	return decl;
> +	return NULL_TREE;
>       }
>     else
>       error_at (DECL_SOURCE_LOCATION (decl),
> diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C
> new file mode 100644
> index 00000000000..39c2a0dc0cc
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84.C
> @@ -0,0 +1,12 @@
> +// PR c++/71954
> +// { dg-do compile { target c++14 } }
> +
> +struct A {
> +  template<class T> static const int var = 0;
> +  template<class T> static const int var<T*> = 1;
> +  template<class T> static const int var<const T*> = 2;
> +};
> +
> +static_assert(A::var<int> == 0, "");
> +static_assert(A::var<int*> == 1, "");
> +static_assert(A::var<const int*> == 2, "");
> diff --git a/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
> new file mode 100644
> index 00000000000..4aa3a2a7245
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp1y/var-templ84a.C
> @@ -0,0 +1,19 @@
> +// PR c++/71954
> +// A version of var-templ84.C where the partial specializations depend on
> +// outer template parameters.
> +// { dg-do compile { target c++14 } }
> +
> +template<class T>
> +struct A {
> +  template<class U, class V> static const int var = 0;
> +  template<class U> static const int var<U*, T> = 1;
> +  template<class U> static const int var<const U*, T> = 2;
> +};
> +
> +static_assert(A<int>::var<int, int> == 0, "");
> +static_assert(A<int>::var<int*, int> == 1, "");
> +static_assert(A<int>::var<const int*, int> == 2, "");
> +
> +static_assert(A<int>::var<int, char> == 0, "");
> +static_assert(A<int>::var<int*, char> == 0, "");
> +static_assert(A<int>::var<const int*, char> == 0, "");


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

end of thread, other threads:[~2023-08-10 21:16 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-10 16:08 [PATCH] c++: recognize in-class var tmpl partial spec [PR71954] Patrick Palka
2023-08-10 21:16 ` 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).