public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [C++ PATCH] Allow some function pointer conversions
@ 2004-10-20 14:34 Nathan Sidwell
  2004-10-20 17:22 ` Mark Mitchell
  0 siblings, 1 reply; 6+ messages in thread
From: Nathan Sidwell @ 2004-10-20 14:34 UTC (permalink / raw)
  To: GCC Patches; +Cc: Mark Mitchell, Andreas Schwab, Jason Merrill

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

This patch implements the smallest parts of DR195 that I think are
appropriate right now.  DR195 is in drafting, but has been kicking around
for several years. The patch fixes the regressions caused by Mark's patch
for 14035 in the C++ testsuite.

The new testcase g++.dg/expr/cast2.C, still fails.  It think it
should probably be removed as the testcase with this patch is doing
more complete checking.  Also I changed g++.old-deja/g++.mike/p10148.C.
I couldn't really see the point of the c-cast there -- it was converting
the function pointer to a base (of the current class) pointer.  I suspect
some parens might have been missed.

I've not documented the casts this patch permits, but will do so once
we've agreed on the desired semantics. Specifically, this patch permits

*) pointer-to-function to be explicitly converted to pointer-to-void,
provided there is no loss of precision.
*) pointer-to-void can be explicitly converted to pointer-to-function,
regardless of precision loss.  The reason for a lack of precision check
here, is so one can convert a function-pointer to a void-pointer and back
again, so long as the void-pointer has at least as many bits.

DR195 does not single out pointer-to-void, but I consider void pointers
to be a reasonable restriction right now.  We can always relax it later.

Let me know if you don't think this is the correct approach.

booted & tested on i686-pc-linux-gnu.

nathan
-- 
Nathan Sidwell    ::   http://www.codesourcery.com   ::     CodeSourcery LLC
nathan@codesourcery.com    ::     http://www.planetfall.pwp.blueyonder.co.uk


[-- Attachment #2: cast.patch --]
[-- Type: text/plain, Size: 5960 bytes --]

2004-10-20  Nathan Sidwell  <nathan@codesourcery.com>

	* typeck.c (composite_pointer_type): Add comment about DR 195
	(build_reinterpret_cast_1): Add for_reinterpret_cast_p parameter.
	Allow function pointer conversions that DR195 suggests.
	(build_reinterpret_cast, build_c_cast): Update
	build_reinterpret_cast_1 calls. 

2004-10-20  Nathan Sidwell  <nathan@codesourcery.com>

	* g++.dg/conversion/dr195.C: New.
	* g++.old-deja/g++.mike/p10148.C: Remove ill-formed cast.

Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.584
diff -c -3 -p -r1.584 typeck.c
*** cp/typeck.c	19 Oct 2004 23:24:18 -0000	1.584
--- cp/typeck.c	20 Oct 2004 12:04:41 -0000
*************** composite_pointer_type (tree t1, tree t2
*** 507,512 ****
--- 507,514 ----
        tree result_type;
  
        if (pedantic && TYPE_PTRFN_P (t2))
+ 	/* Although DR195 suggests allowing this when no precision is
+ 	   lost, that is only allowed in a reinterpret_cast.  */
  	pedwarn ("ISO C++ forbids %s between pointer of type %<void *%> "
                   "and pointer-to-function", location);
        result_type 
*************** convert_member_func_to_ptr (tree type, t
*** 4803,4809 ****
  
  static tree
  build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
! 			  bool *valid_p)
  {
    tree intype;
  
--- 4805,4811 ----
  
  static tree
  build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
! 			  bool for_reinterpret_ref_p, bool *valid_p)
  {
    tree intype;
  
*************** build_reinterpret_cast_1 (tree type, tre
*** 4843,4849 ****
        expr = build_unary_op (ADDR_EXPR, expr, 0);
        if (expr != error_mark_node)
  	expr = build_reinterpret_cast_1
! 	  (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p,
  	   valid_p);
        if (expr != error_mark_node)
  	expr = build_indirect_ref (expr, 0);
--- 4845,4851 ----
        expr = build_unary_op (ADDR_EXPR, expr, 0);
        if (expr != error_mark_node)
  	expr = build_reinterpret_cast_1
! 	  (build_pointer_type (TREE_TYPE (type)), expr, c_cast_p, true,
  	   valid_p);
        if (expr != error_mark_node)
  	expr = build_indirect_ref (expr, 0);
*************** build_reinterpret_cast_1 (tree type, tre
*** 4922,4928 ****
  	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
      {
        if (pedantic || !c_cast_p)
! 	pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
        expr = decl_constant_value (expr);
        return fold_if_not_in_template (build_nop (type, expr));
      }
--- 4924,4947 ----
  	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
      {
        if (pedantic || !c_cast_p)
! 	{
! 	  /* DR 195 suggests allowing such casts if they do not lose
! 	     precision.  We allow conversion to pointer-to-void, if it
! 	     does not lose precision, and we allow conversion from
! 	     pointer-to-void regardless, so that one may convert
! 	     back again without warning.  Such conversions are not
! 	     permitted when we are recursively called to deal with
! 	     reinterpretting reference casts. */
! 	  if (!for_reinterpret_ref_p && VOID_TYPE_P (TREE_TYPE (type)))
! 	    {
! 	      if (TYPE_PRECISION (type) < TYPE_PRECISION (intype))
! 		warning ("conversion from %qT to %qT loses precision",
! 			 intype, type);
! 	    }
! 	  else if (for_reinterpret_ref_p || !VOID_TYPE_P (TREE_TYPE (intype)))
! 	    pedwarn ("ISO C++ forbids casting between pointer-to-function and pointer-to-object");
! 	}
!       
        expr = decl_constant_value (expr);
        return fold_if_not_in_template (build_nop (type, expr));
      }
*************** build_reinterpret_cast (tree type, tree 
*** 4955,4960 ****
--- 4974,4980 ----
      }
  
    return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
+ 				   /*for_reinterpret_ref=*/false,
  				   /*valid_p=*/NULL);
  }
  
*************** build_c_cast (tree type, tree expr)
*** 5157,5162 ****
--- 5177,5183 ----
    /* Or a reinterpret_cast.  */
    if (!valid_p)
      result = build_reinterpret_cast_1 (type, value, /*c_cast_p=*/true,
+ 				       /*for_reinterpret_ref_p=*/false,
  				       &valid_p);
    /* The static_cast or reinterpret_cast may be followed by a
       const_cast.  */
Index: testsuite/g++.dg/conversion/dr195.C
===================================================================
RCS file: testsuite/g++.dg/conversion/dr195.C
diff -N testsuite/g++.dg/conversion/dr195.C
*** /dev/null	1 Jan 1970 00:00:00 -0000
--- testsuite/g++.dg/conversion/dr195.C	20 Oct 2004 13:29:45 -0000
***************
*** 0 ****
--- 1,25 ----
+ // Copyright (C) 2004 Free Software Foundation, Inc.
+ // Contributed by Nathan Sidwell 20 Oct 2004 <nathan@codesourcery.com>
+ 
+ // DR 195 allows conversions between function and object pointers
+ // under some circumstances.
+ 
+ typedef void (*PF)(void);
+ typedef void *PV;
+ typedef int *PO;
+ 
+ 
+ void foo ()
+ {
+   PF pf;
+   PV pv;
+   PO po;
+ 
+   pf = reinterpret_cast <PF>(pv);
+   pv = reinterpret_cast <PV>(pf);
+   pf = reinterpret_cast <PF>(po); // { dg-error "casting between" "" }
+   po = reinterpret_cast <PO>(pf); // { dg-error "casting between" "" }
+ 
+   pv = pf; // { dg-error "invalid conversion" "" }
+   pf = pv; // { dg-error "invalid conversion" "" }
+ }
Index: testsuite/g++.old-deja/g++.mike/p10148.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.mike/p10148.C,v
retrieving revision 1.4
diff -c -3 -p -r1.4 p10148.C
*** testsuite/g++.old-deja/g++.mike/p10148.C	1 May 2003 02:02:45 -0000	1.4
--- testsuite/g++.old-deja/g++.mike/p10148.C	20 Oct 2004 13:29:54 -0000
*************** public:
*** 23,29 ****
  };
  
  void TCRCB::eat () {
!  void *vp = (TIRD*)this->itc;
   this->itc();
  }
  
--- 23,29 ----
  };
  
  void TCRCB::eat () {
!  void *vp = (void *)((TIRD*)this)->itc;
   this->itc();
  }
  

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

* Re: [C++ PATCH] Allow some function pointer conversions
  2004-10-20 14:34 [C++ PATCH] Allow some function pointer conversions Nathan Sidwell
@ 2004-10-20 17:22 ` Mark Mitchell
  2004-10-20 17:26   ` Gabriel Dos Reis
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Mitchell @ 2004-10-20 17:22 UTC (permalink / raw)
  To: Nathan Sidwell; +Cc: GCC Patches, Andreas Schwab, Jason Merrill

Nathan Sidwell wrote:

> This patch implements the smallest parts of DR195 that I think are
> appropriate right now.  DR195 is in drafting, but has been kicking around
> for several years. The patch fixes the regressions caused by Mark's patch
> for 14035 in the C++ testsuite.
>
> The new testcase g++.dg/expr/cast2.C, still fails.  It think it
> should probably be removed as the testcase with this patch is doing
> more complete checking.  Also I changed g++.old-deja/g++.mike/p10148.C.
> I couldn't really see the point of the c-cast there -- it was converting
> the function pointer to a base (of the current class) pointer.  I suspect
> some parens might have been missed.
>
> I've not documented the casts this patch permits, but will do so once
> we've agreed on the desired semantics. Specifically, this patch permits
>
> *) pointer-to-function to be explicitly converted to pointer-to-void,
> provided there is no loss of precision.
> *) pointer-to-void can be explicitly converted to pointer-to-function,
> regardless of precision loss.  The reason for a lack of precision check
> here, is so one can convert a function-pointer to a void-pointer and back
> again, so long as the void-pointer has at least as many bits.
>
> DR195 does not single out pointer-to-void, but I consider void pointers
> to be a reasonable restriction right now.  We can always relax it later.

I think that if we want to implement DR195, I'd be happiest if we just 
removed the diagnostic.  (Before my patch, things were inconsistent; we 
issued a pedwarn for reinterpret_cast, but not for a C-style cast.  I 
made the two match up.) 

I think that we could just remove the diangostic altogether to implement 
DR195.

We could also turn it into a warning, which might be the most 
conservative choice; we would still conform to TC1, but also implement 
DR195.

-- 
Mark Mitchell
CodeSourcery, LLC
(916) 791-8304
mark@codesourcery.com

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

* Re: [C++ PATCH] Allow some function pointer conversions
  2004-10-20 17:22 ` Mark Mitchell
@ 2004-10-20 17:26   ` Gabriel Dos Reis
  2004-10-20 20:03     ` Jason Merrill
  0 siblings, 1 reply; 6+ messages in thread
From: Gabriel Dos Reis @ 2004-10-20 17:26 UTC (permalink / raw)
  To: Mark Mitchell; +Cc: Nathan Sidwell, GCC Patches, Andreas Schwab, Jason Merrill

Mark Mitchell <mark@codesourcery.com> writes:

| Nathan Sidwell wrote:
| 
| > This patch implements the smallest parts of DR195 that I think are
| > appropriate right now.  DR195 is in drafting, but has been kicking around
| > for several years. The patch fixes the regressions caused by Mark's patch
| > for 14035 in the C++ testsuite.
| >
| > The new testcase g++.dg/expr/cast2.C, still fails.  It think it
| > should probably be removed as the testcase with this patch is doing
| > more complete checking.  Also I changed g++.old-deja/g++.mike/p10148.C.
| > I couldn't really see the point of the c-cast there -- it was converting
| > the function pointer to a base (of the current class) pointer.  I suspect
| > some parens might have been missed.
| >
| > I've not documented the casts this patch permits, but will do so once
| > we've agreed on the desired semantics. Specifically, this patch permits
| >
| > *) pointer-to-function to be explicitly converted to pointer-to-void,
| > provided there is no loss of precision.
| > *) pointer-to-void can be explicitly converted to pointer-to-function,
| > regardless of precision loss.  The reason for a lack of precision check
| > here, is so one can convert a function-pointer to a void-pointer and back
| > again, so long as the void-pointer has at least as many bits.
| >
| > DR195 does not single out pointer-to-void, but I consider void pointers
| > to be a reasonable restriction right now.  We can always relax it later.
| 
| I think that if we want to implement DR195, I'd be happiest if we just
| removed the diagnostic.  (Before my patch, things were inconsistent;
| we issued a pedwarn for reinterpret_cast, but not for a C-style cast.
| I made the two match up.) I think that we could just remove the
| diangostic altogether to implement DR195.
| 
| We could also turn it into a warning, which might be the most
| conservative choice; we would still conform to TC1, but also implement
| DR195.

If we believe we really want to implement DR195, then I would
recommend we do not remove the diagnostic, but make it a warning.
Jason?

-- Gaby

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

* Re: [C++ PATCH] Allow some function pointer conversions
  2004-10-20 17:26   ` Gabriel Dos Reis
@ 2004-10-20 20:03     ` Jason Merrill
  2004-10-20 20:38       ` Gabriel Dos Reis
  2004-10-21  0:09       ` Mark Mitchell
  0 siblings, 2 replies; 6+ messages in thread
From: Jason Merrill @ 2004-10-20 20:03 UTC (permalink / raw)
  To: Gabriel Dos Reis
  Cc: Mark Mitchell, Nathan Sidwell, GCC Patches, Andreas Schwab

On 20 Oct 2004 12:25:12 -0500, Gabriel Dos Reis <gdr@cs.tamu.edu> wrote:

> Mark Mitchell <mark@codesourcery.com> writes:
>
> | Nathan Sidwell wrote:
> | 
> | I think that if we want to implement DR195, I'd be happiest if we just
> | removed the diagnostic.  (Before my patch, things were inconsistent;
> | we issued a pedwarn for reinterpret_cast, but not for a C-style cast.
> | I made the two match up.) I think that we could just remove the
> | diangostic altogether to implement DR195.
> | 
> | We could also turn it into a warning, which might be the most
> | conservative choice; we would still conform to TC1, but also implement
> | DR195.
>
> If we believe we really want to implement DR195, then I would
> recommend we do not remove the diagnostic, but make it a warning.

My preference would be for a warning only under -pedantic.  This pattern is
supported on all of our platforms, and is in wide use.  Warning about it
would just be useless noise unless people are specifically interested in
extreme portability.  A hard error is unacceptable.

Jason

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

* Re: [C++ PATCH] Allow some function pointer conversions
  2004-10-20 20:03     ` Jason Merrill
@ 2004-10-20 20:38       ` Gabriel Dos Reis
  2004-10-21  0:09       ` Mark Mitchell
  1 sibling, 0 replies; 6+ messages in thread
From: Gabriel Dos Reis @ 2004-10-20 20:38 UTC (permalink / raw)
  To: Jason Merrill; +Cc: Mark Mitchell, Nathan Sidwell, GCC Patches, Andreas Schwab

Jason Merrill <jason@redhat.com> writes:

| On 20 Oct 2004 12:25:12 -0500, Gabriel Dos Reis <gdr@cs.tamu.edu> wrote:
| 
| > Mark Mitchell <mark@codesourcery.com> writes:
| >
| > | Nathan Sidwell wrote:
| > | 
| > | I think that if we want to implement DR195, I'd be happiest if we just
| > | removed the diagnostic.  (Before my patch, things were inconsistent;
| > | we issued a pedwarn for reinterpret_cast, but not for a C-style cast.
| > | I made the two match up.) I think that we could just remove the
| > | diangostic altogether to implement DR195.
| > | 
| > | We could also turn it into a warning, which might be the most
| > | conservative choice; we would still conform to TC1, but also implement
| > | DR195.
| >
| > If we believe we really want to implement DR195, then I would
| > recommend we do not remove the diagnostic, but make it a warning.
| 
| My preference would be for a warning only under -pedantic.  This pattern is
| supported on all of our platforms, and is in wide use.  Warning about it
| would just be useless noise unless people are specifically interested in
| extreme portability.  A hard error is unacceptable.

noted.

-- Gaby

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

* Re: [C++ PATCH] Allow some function pointer conversions
  2004-10-20 20:03     ` Jason Merrill
  2004-10-20 20:38       ` Gabriel Dos Reis
@ 2004-10-21  0:09       ` Mark Mitchell
  1 sibling, 0 replies; 6+ messages in thread
From: Mark Mitchell @ 2004-10-21  0:09 UTC (permalink / raw)
  To: Jason Merrill
  Cc: Gabriel Dos Reis, Nathan Sidwell, GCC Patches, Andreas Schwab

Jason Merrill wrote:

>On 20 Oct 2004 12:25:12 -0500, Gabriel Dos Reis <gdr@cs.tamu.edu> wrote:
>
>  
>
>>Mark Mitchell <mark@codesourcery.com> writes:
>>
>>| Nathan Sidwell wrote:
>>| 
>>| I think that if we want to implement DR195, I'd be happiest if we just
>>| removed the diagnostic.  (Before my patch, things were inconsistent;
>>| we issued a pedwarn for reinterpret_cast, but not for a C-style cast.
>>| I made the two match up.) I think that we could just remove the
>>| diangostic altogether to implement DR195.
>>| 
>>| We could also turn it into a warning, which might be the most
>>| conservative choice; we would still conform to TC1, but also implement
>>| DR195.
>>
>>If we believe we really want to implement DR195, then I would
>>recommend we do not remove the diagnostic, but make it a warning.
>>    
>>
>
>My preference would be for a warning only under -pedantic.  This pattern is
>supported on all of our platforms, and is in wide use.  Warning about it
>would just be useless noise unless people are specifically interested in
>extreme portability.  A hard error is unacceptable.
>
Good; I think we're in agreement.

Nathan, I see that your patch for DR195 has gone in.  Shall I revert 
that and replace the current pedwarn with a warning, thereby 
implementing the above solution?  Or, do you want to do that?  Or, do 
you not like this plan at all?

Thanks,

-- 
Mark Mitchell
CodeSourcery, LLC
(916) 791-8304
mark@codesourcery.com

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

end of thread, other threads:[~2004-10-20 23:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-10-20 14:34 [C++ PATCH] Allow some function pointer conversions Nathan Sidwell
2004-10-20 17:22 ` Mark Mitchell
2004-10-20 17:26   ` Gabriel Dos Reis
2004-10-20 20:03     ` Jason Merrill
2004-10-20 20:38       ` Gabriel Dos Reis
2004-10-21  0:09       ` Mark Mitchell

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