diff mbox

C++ PATCH for c++/48945 (defining static constexpr fn outside class)

Message ID 4DD83541.2040403@redhat.com
State New
Headers show

Commit Message

Jason Merrill May 21, 2011, 9:57 p.m. UTC
revert_static_member_fn was complaining about the implicit 'const' on a 
constexpr non-static member function even though it wasn't actually 
written.  The first patch changes the compiler to allow 'const' on a 
constexpr function, which fixes the testcase; the second patch also 
allows the compiler to complain when the user wrote 'const' explicitly.

Tested x86_64-pc-linux-gnu.  First patch applied to trunk and 4.6, 
second trunk only.
diff mbox

Patch

commit 0dd08faa4bad75ac99e29efdae5acbb2e7f174be
Author: Jason Merrill <jason@redhat.com>
Date:   Fri May 20 18:17:39 2011 -0400

    	PR c++/48945
    	* decl.c (revert_static_member_fn): Ignore const on constexpr fn.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index eae7d8e..598af1c 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -13575,10 +13575,15 @@  void
 revert_static_member_fn (tree decl)
 {
   tree stype = static_fn_type (decl);
+  cp_cv_quals quals = type_memfn_quals (stype);
 
-  if (type_memfn_quals (stype) != TYPE_UNQUALIFIED)
+  if (quals != TYPE_UNQUALIFIED)
     {
-      error ("static member function %q#D declared with type qualifiers", decl);
+      if (quals == TYPE_QUAL_CONST && DECL_DECLARED_CONSTEXPR_P (decl))
+	/* The const was implicit, don't complain.  */;
+      else
+	error ("static member function %q#D declared with type qualifiers",
+	       decl);
       stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
     }
   TREE_TYPE (decl) = stype;
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C
new file mode 100644
index 0000000..ba4a251
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C
@@ -0,0 +1,8 @@ 
+// PR c++/48945
+// { dg-options -std=c++0x }
+
+struct A {
+  static constexpr bool is();
+};
+
+constexpr bool A::is() { return true; }

commit bf3db2f49e7927afa0293379700d488dcb98e2c7
Author: Jason Merrill <jason@redhat.com>
Date:   Fri May 20 23:32:39 2011 -0400

    	PR c++/48945
    	* decl.c (grokdeclarator): Don't add set const function-cv-qual
    	for constexpr fns to memfn_quals, just add it to the type.
    	(revert_static_member_fn): Don't complain about quals.
    	(check_static_quals): New.
    	(grokfndecl): Call it.
    	(start_preparsed_function): Don't call revert_static_member_fn.

diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c
index 598af1c..733157a 100644
--- a/gcc/cp/decl.c
+++ b/gcc/cp/decl.c
@@ -6960,6 +6960,17 @@  build_this_parm (tree type, cp_cv_quals quals)
   return parm;
 }
 
+/* DECL is a static member function.  Complain if it was declared
+   with function-cv-quals.  */
+
+static void
+check_static_quals (tree decl, cp_cv_quals quals)
+{
+  if (quals != TYPE_UNQUALIFIED)
+    error ("static member function %q#D declared with type qualifiers",
+	   decl);
+}
+
 /* CTYPE is class type, or null if non-class.
    TYPE is type this FUNCTION_DECL should have, either FUNCTION_TYPE
    or METHOD_TYPE.
@@ -7241,6 +7252,9 @@  grokfndecl (tree ctype,
   if (decl == error_mark_node)
     return NULL_TREE;
 
+  if (DECL_STATIC_FUNCTION_P (decl))
+    check_static_quals (decl, quals);
+
   if (attrlist)
     {
       cplus_decl_attributes (&decl, *attrlist, 0);
@@ -7290,9 +7304,11 @@  grokfndecl (tree ctype,
 
 	  if (DECL_STATIC_FUNCTION_P (old_decl)
 	      && TREE_CODE (TREE_TYPE (decl)) == METHOD_TYPE)
-	    /* Remove the `this' parm added by grokclassfn.
-	       XXX Isn't this done in start_function, too?  */
-	    revert_static_member_fn (decl);
+	    {
+	      /* Remove the `this' parm added by grokclassfn.  */
+	      revert_static_member_fn (decl);
+	      check_static_quals (decl, quals);
+	    }
 	  if (DECL_ARTIFICIAL (old_decl))
 	    {
 	      error ("definition of implicitly-declared %qD", old_decl);
@@ -9356,12 +9372,6 @@  grokdeclarator (const cp_declarator *declarator,
   if (ctype == NULL_TREE && decl_context == FIELD && friendp == 0)
     ctype = current_class_type;
 
-  /* A constexpr non-static member function is implicitly const.  */
-  if (constexpr_p && ctype && staticp == 0
-      && TREE_CODE (type) == FUNCTION_TYPE
-      && sfk != sfk_constructor && sfk != sfk_destructor)
-    memfn_quals |= TYPE_QUAL_CONST;
-
   /* Now TYPE has the actual type.  */
 
   if (returned_attrs)
@@ -9733,7 +9743,12 @@  grokdeclarator (const cp_declarator *declarator,
 
   if (ctype && TREE_CODE (type) == FUNCTION_TYPE && staticp < 2
       && !NEW_DELETE_OPNAME_P (unqualified_id))
-    type = build_memfn_type (type, ctype, memfn_quals);
+    {
+      cp_cv_quals real_quals = memfn_quals;
+      if (constexpr_p && sfk != sfk_constructor && sfk != sfk_destructor)
+	real_quals |= TYPE_QUAL_CONST;
+      type = build_memfn_type (type, ctype, real_quals);
+    }
 
   {
     tree decl;
@@ -12373,12 +12388,8 @@  start_preparsed_function (tree decl1, tree attrs, int flags)
 
   /* Sometimes we don't notice that a function is a static member, and
      build a METHOD_TYPE for it.  Fix that up now.  */
-  if (ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
-      && TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE)
-    {
-      revert_static_member_fn (decl1);
-      ctype = NULL_TREE;
-    }
+  gcc_assert (!(ctype != NULL_TREE && DECL_STATIC_FUNCTION_P (decl1)
+		&& TREE_CODE (TREE_TYPE (decl1)) == METHOD_TYPE));
 
   /* Set up current_class_type, and enter the scope of the class, if
      appropriate.  */
@@ -13578,14 +13589,8 @@  revert_static_member_fn (tree decl)
   cp_cv_quals quals = type_memfn_quals (stype);
 
   if (quals != TYPE_UNQUALIFIED)
-    {
-      if (quals == TYPE_QUAL_CONST && DECL_DECLARED_CONSTEXPR_P (decl))
-	/* The const was implicit, don't complain.  */;
-      else
-	error ("static member function %q#D declared with type qualifiers",
-	       decl);
-      stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
-    }
+    stype = apply_memfn_quals (stype, TYPE_UNQUALIFIED);
+
   TREE_TYPE (decl) = stype;
 
   if (DECL_ARGUMENTS (decl))
diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C
index ba4a251..e46ddaf 100644
--- a/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C
+++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-static7.C
@@ -3,6 +3,8 @@ 
 
 struct A {
   static constexpr bool is();
+  static constexpr bool is_not();
 };
 
 constexpr bool A::is() { return true; }
+constexpr bool A::is_not() const { return true; } // { dg-error "static" }