This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[C++ PATCH] function<->object pointer casts


This implements our conclusion of the function-object pointer cast
and DR195 discussion.  One point still needs clarifying though.

Mark's orignal reworking contained
	if (pedantic || !c_cast)
		pedwarn ()
thereby always pedwarning for reinterpret_cast.  Do we want to keep that
distinction? IMO reinterpret_cast and c_cast should be the same wrt
warning behaviour here (the std says both would be ill-formed and DR195
won't distinguish AFAICT).  Hence this patch contains
	if (pedantic)
	   warning ()

booted & tested on i686-pc-linux-gnu, ok?

nathan

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

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

	* typeck.c (composite_pointer_type): Remove comment about DR 195.
	(build_reinterpret_cast_1): Revert DR195 patch. Only emit a
	warning when being pedantic.
	(build_reinterpet_cast, build_c_cast): Adjust.

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

	* g++.dg/conversion/dr195.C: Adjust expected errors for DR195 not
	being implemented.
	* g++.dg/conversion/dr195-1.C: New.
	* g++.old-deja/g++.brendan/operators4.C: Don't be pedantic.
	* g++.old-deja/g++.mike/p10148.C: Likewise.

Index: cp/typeck.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/typeck.c,v
retrieving revision 1.589
diff -c -3 -p -r1.589 typeck.c
*** cp/typeck.c	27 Oct 2004 02:23:14 -0000	1.589
--- cp/typeck.c	28 Oct 2004 09:38:40 -0000
*************** composite_pointer_type (tree t1, tree t2
*** 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 
--- 507,512 ----
*************** convert_member_func_to_ptr (tree type, t
*** 4827,4833 ****
  
  static tree
  build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
! 			  bool for_reinterpret_ref_p, bool *valid_p)
  {
    tree intype;
  
--- 4825,4831 ----
  
  static tree
  build_reinterpret_cast_1 (tree type, tree expr, bool c_cast_p,
! 			  bool *valid_p)
  {
    tree intype;
  
*************** build_reinterpret_cast_1 (tree type, tre
*** 4867,4873 ****
        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);
--- 4865,4871 ----
        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);
*************** build_reinterpret_cast_1 (tree type, tre
*** 4945,4968 ****
    else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
  	   || (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
! 	     reinterpreting 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));
--- 4943,4954 ----
    else if ((TYPE_PTRFN_P (type) && TYPE_PTROBV_P (intype))
  	   || (TYPE_PTRFN_P (intype) && TYPE_PTROBV_P (type)))
      {
!       if (pedantic)
! 	/* Only issue a warning, as we have always supported this
!  	   where possible, and it is necessary in some cases.  DR 195
!  	   addresses this issue, but as of 2004/10/26 is still in
!  	   drafting.  */
!  	warning ("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 
*** 4998,5004 ****
      }
  
    return build_reinterpret_cast_1 (type, expr, /*c_cast_p=*/false,
- 				   /*for_reinterpret_ref=*/false,
  				   /*valid_p=*/NULL);
  }
  
--- 4984,4989 ----
*************** build_c_cast (tree type, tree expr)
*** 5201,5207 ****
    /* 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.  */
--- 5186,5191 ----
Index: testsuite/g++.dg/conversion/dr195.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.dg/conversion/dr195.C,v
retrieving revision 1.1
diff -c -3 -p -r1.1 dr195.C
*** testsuite/g++.dg/conversion/dr195.C	20 Oct 2004 14:30:52 -0000	1.1
--- testsuite/g++.dg/conversion/dr195.C	28 Oct 2004 09:38:58 -0000
***************
*** 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" "" }
  }
--- 1,31 ----
  // Copyright (C) 2004 Free Software Foundation, Inc.
  // Contributed by Nathan Sidwell 20 Oct 2004 <nathan@codesourcery.com>
  
! // DR 195 will allow conversions between function and object pointers
! // under some circumstances. It is in drafting, so we don't implement
! // it (yet).
! 
! // This checks we warn when being pedantic.
  
  typedef void (*PF)(void);
  typedef void *PV;
  typedef int *PO;
  
  void foo ()
  {
    PF pf;
    PV pv;
    PO po;
  
!   /* the following two will almost definitly be ok with 195.  */
!   pf = reinterpret_cast <PF>(pv); // { dg-warning "casting between" "" }
!   pv = reinterpret_cast <PV>(pf); // { dg-warning "casting between" "" }
! 
!   /* the following two might or might not be ok with 195.  */
    pf = reinterpret_cast <PF>(po); // { dg-error "casting between" "" }
    po = reinterpret_cast <PO>(pf); // { dg-error "casting between" "" }
  
+   /* These will never be ok, as they are implicit.  */
    pv = pf; // { dg-error "invalid conversion" "" }
    pf = pv; // { dg-error "invalid conversion" "" }
  }
Index: testsuite/g++.old-deja/g++.brendan/operators4.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.brendan/operators4.C,v
retrieving revision 1.4
diff -c -3 -p -r1.4 operators4.C
*** testsuite/g++.old-deja/g++.brendan/operators4.C	1 May 2003 02:02:35 -0000	1.4
--- testsuite/g++.old-deja/g++.brendan/operators4.C	28 Oct 2004 09:39:03 -0000
***************
*** 1,4 ****
--- 1,6 ----
  // { dg-do assemble  }
+ // { dg-options "" }
+ 
  // GROUPS passed operators
  // Check that the & operator, when applied to a global function
  // or member function returns a proper value as long as the context
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.5
diff -c -3 -p -r1.5 p10148.C
*** testsuite/g++.old-deja/g++.mike/p10148.C	20 Oct 2004 14:30:52 -0000	1.5
--- testsuite/g++.old-deja/g++.mike/p10148.C	28 Oct 2004 09:39:07 -0000
***************
*** 1,4 ****
--- 1,5 ----
  // { dg-do run  }
+ // { dg-options "" }
  // prms-id: 10148
  
  int fail = 1;
// { dg-options "" }

// Copyright (C) 2004 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 20 Oct 2004 <nathan@codesourcery.com>

// DR 195 will allow conversions between function and object pointers
// under some circumstances. It is in drafting, so we don't implement
// it (yet).

// this checks we are silent when not being pedantic.

typedef void (*PF)(void);
typedef void *PV;
typedef int *PO;


void foo ()
{
  PF pf;
  PV pv;
  PO po;

  /* the following two will almost definitly be ok with 195.  */
  pf = reinterpret_cast <PF>(pv);
  pv = reinterpret_cast <PV>(pf);

  /* the following two might or might not be ok with 195.  */
  pf = reinterpret_cast <PF>(po);
  po = reinterpret_cast <PO>(pf);

  /* These will never be ok, as they are implicit.  */
  pv = pf; // { dg-error "invalid conversion" "" }
  pf = pv; // { dg-error "invalid conversion" "" }
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]