public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* PATCH: fix comparison of C++ pointers to member functions
@ 2007-11-13 10:17 Ollie Wild
  2007-11-13 10:24 ` Ollie Wild
  0 siblings, 1 reply; 3+ messages in thread
From: Ollie Wild @ 2007-11-13 10:17 UTC (permalink / raw)
  To: GCC Patches

Section 5.10 of the C++ standard states:

"In addition, pointers to members can be compared, or a pointer to
member and a null pointer constant.  Pointer to member conversions
(4.11) and qualification conversions (4.4) are performed to bring them
to a common type."

The C++ front end fails to perform the indicated conversions when
comparing pointers to member functions.  This patch rectifies this.

Tested with a C/C++/Java bootstrap and testsuite on i686-pc-linux-gnu.

Ollie

:ADDPATCH c++:

2007-11-12  Ollie Wild  <aaw@google.com>

        * typeck.c (build_binary_op): Add conversion of pointers to member
        functions appearing as operands to the equality operators.

2007-11-12  Ollie Wild  <aaw@google.com>

        * g++.dg/conversion/ptrmem9.C: New test.

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

* Re: PATCH: fix comparison of C++ pointers to member functions
  2007-11-13 10:17 PATCH: fix comparison of C++ pointers to member functions Ollie Wild
@ 2007-11-13 10:24 ` Ollie Wild
  2007-11-28 15:01   ` Ollie Wild
  0 siblings, 1 reply; 3+ messages in thread
From: Ollie Wild @ 2007-11-13 10:24 UTC (permalink / raw)
  To: GCC Patches

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

On Nov 12, 2007 10:15 PM, Ollie Wild <aaw@google.com> wrote:
> Section 5.10 of the C++ standard states:
>
> "In addition, pointers to members can be compared, or a pointer to
> member and a null pointer constant.  Pointer to member conversions
> (4.11) and qualification conversions (4.4) are performed to bring them
> to a common type."
>
> The C++ front end fails to perform the indicated conversions when
> comparing pointers to member functions.  This patch rectifies this.

It might help if I attached the patch.

Ollie

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: ptrmem_equality.patch --]
[-- Type: text/x-patch; name=ptrmem_equality.patch, Size: 1915 bytes --]

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index d167de2..270defe 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3392,9 +3392,9 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
 	}
       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
 	return cp_build_binary_op (code, op1, op0);
-      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
-	       && same_type_p (type0, type1))
+      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
 	{
+	  tree type;
 	  /* E will be the final comparison.  */
 	  tree e;
 	  /* E1 and E2 are for scratch.  */
@@ -3405,6 +3405,16 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
 	  tree delta0;
 	  tree delta1;
 
+	  type = composite_pointer_type (type0, type1, op0, op1, "comparison");
+
+	  if (TREE_TYPE (op0) != type)
+	    op0 = cp_convert_and_check (type, op0);
+	  if (TREE_TYPE (op1) != type)
+	    op1 = cp_convert_and_check (type, op1);
+
+	  if (op0 == error_mark_node || op1 == error_mark_node)
+	    return error_mark_node;
+
 	  if (TREE_SIDE_EFFECTS (op0))
 	    op0 = save_expr (op0);
 	  if (TREE_SIDE_EFFECTS (op1))
diff --git a/gcc/testsuite/g++.dg/conversion/ptrmem9.C b/gcc/testsuite/g++.dg/conversion/ptrmem9.C
new file mode 100644
index 0000000..2ccd683
--- /dev/null
+++ b/gcc/testsuite/g++.dg/conversion/ptrmem9.C
@@ -0,0 +1,26 @@
+// Copyright (C) 2007 Free Software Foundation
+// Contributed by Ollie Wild <aaw@google.com>
+// { dg-do compile }
+
+// Test implicit conversion of pointers to member functions appearing as
+// operands of the equality operators.
+
+struct B { };
+
+struct BV { };
+
+struct D : B, virtual BV { };
+
+struct C { };
+
+void f ()
+{
+  void (D::*pd) () = 0;
+  void (B::*pb) () = 0;
+  void (BV::*pbv) () = 0;
+  void (C::*pc) () = 0;
+
+  pd == pb;
+  pd == pbv;  // { dg-error "" }
+  pd == pc;   // { dg-error "" }
+}

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

* Re: PATCH: fix comparison of C++ pointers to member functions
  2007-11-13 10:24 ` Ollie Wild
@ 2007-11-28 15:01   ` Ollie Wild
  0 siblings, 0 replies; 3+ messages in thread
From: Ollie Wild @ 2007-11-28 15:01 UTC (permalink / raw)
  To: GCC Patches

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

On Nov 12, 2007 10:17 PM, Ollie Wild <aaw@google.com> wrote:
>
> It might help if I attached the patch.

It's been a couple weeks since I sent this out, so here's a quick
ping.  Details reproduced below:

Section 5.10 of the C++ standard states:

"In addition, pointers to members can be compared, or a pointer to
member and a null pointer constant.  Pointer to member conversions
(4.11) and qualification conversions (4.4) are performed to bring them
to a common type."

The C++ front end fails to perform the indicated conversions when
comparing pointers to member functions.  This patch rectifies this.

Tested with a C/C++/Java bootstrap and testsuite on i686-pc-linux-gnu.

Ollie

:ADDPATCH c++:

2007-11-12  Ollie Wild  <aaw@google.com>

       * typeck.c (build_binary_op): Add conversion of pointers to member
       functions appearing as operands to the equality operators.

2007-11-12  Ollie Wild  <aaw@google.com>

       * g++.dg/conversion/ptrmem9.C: New test.

[-- Attachment #2: ptrmem_equality.txt --]
[-- Type: text/plain, Size: 1915 bytes --]

diff --git a/gcc/cp/typeck.c b/gcc/cp/typeck.c
index d167de2..270defe 100644
--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -3392,9 +3392,9 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
 	}
       else if (TYPE_PTRMEMFUNC_P (type1) && null_ptr_cst_p (op0))
 	return cp_build_binary_op (code, op1, op0);
-      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1)
-	       && same_type_p (type0, type1))
+      else if (TYPE_PTRMEMFUNC_P (type0) && TYPE_PTRMEMFUNC_P (type1))
 	{
+	  tree type;
 	  /* E will be the final comparison.  */
 	  tree e;
 	  /* E1 and E2 are for scratch.  */
@@ -3405,6 +3405,16 @@ build_binary_op (enum tree_code code, tree orig_op0, tree orig_op1,
 	  tree delta0;
 	  tree delta1;
 
+	  type = composite_pointer_type (type0, type1, op0, op1, "comparison");
+
+	  if (TREE_TYPE (op0) != type)
+	    op0 = cp_convert_and_check (type, op0);
+	  if (TREE_TYPE (op1) != type)
+	    op1 = cp_convert_and_check (type, op1);
+
+	  if (op0 == error_mark_node || op1 == error_mark_node)
+	    return error_mark_node;
+
 	  if (TREE_SIDE_EFFECTS (op0))
 	    op0 = save_expr (op0);
 	  if (TREE_SIDE_EFFECTS (op1))
diff --git a/gcc/testsuite/g++.dg/conversion/ptrmem9.C b/gcc/testsuite/g++.dg/conversion/ptrmem9.C
new file mode 100644
index 0000000..2ccd683
--- /dev/null
+++ b/gcc/testsuite/g++.dg/conversion/ptrmem9.C
@@ -0,0 +1,26 @@
+// Copyright (C) 2007 Free Software Foundation
+// Contributed by Ollie Wild <aaw@google.com>
+// { dg-do compile }
+
+// Test implicit conversion of pointers to member functions appearing as
+// operands of the equality operators.
+
+struct B { };
+
+struct BV { };
+
+struct D : B, virtual BV { };
+
+struct C { };
+
+void f ()
+{
+  void (D::*pd) () = 0;
+  void (B::*pb) () = 0;
+  void (BV::*pbv) () = 0;
+  void (C::*pc) () = 0;
+
+  pd == pb;
+  pd == pbv;  // { dg-error "" }
+  pd == pc;   // { dg-error "" }
+}

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

end of thread, other threads:[~2007-11-28 10:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-11-13 10:17 PATCH: fix comparison of C++ pointers to member functions Ollie Wild
2007-11-13 10:24 ` Ollie Wild
2007-11-28 15:01   ` Ollie Wild

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).