Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [rfa] handle integer downsizing correctly in C++ overloading
@ 2003-01-31 19:57 David Carlton
  2003-01-31 20:04 ` David Carlton
  2003-02-04 19:10 ` Daniel Jacobowitz
  0 siblings, 2 replies; 4+ messages in thread
From: David Carlton @ 2003-01-31 19:57 UTC (permalink / raw)
  To: gdb-patches; +Cc: Daniel Jacobowitz

GDB currently won't let you do narrowing integer conversions when
calling an overloaded C++ function: compile this,

int overloadChar(char c)
{
  return 29;
}

int main()
{
  overloadChar(1);
}

break on main, and try to print overloadChar(1).

The problem is that gdbtypes.h defines both INTEGER_COERCION_BADNESS
and INTEGER_CONVERSION_BADNESS.  The former is unacceptably bad; the
latter isn't preferred, but isn't acceptable.  And in all (or almost
all) cases, the type comparison functions use INTEGER_COERCION_BADNESS
when doing narrowing integer comparisons, when they should use
INTEGER_CONVERSION_BADNESS.

In fact, INTEGER_COERCION_BADNESS shouldn't exist: there's no such
thing as an unacceptably bad integer conversion.  So this patch
changes all uses of INTEGER_COERCION_BADNESS to refer to
INTEGER_CONVERSION_BADNESS, deletes INTEGER_COERCION_BADNESS, and also
deletes FLOAT_COERCION_BADNESS (which is similarly unnecessary but
which is already correctly unused).  And there's a testsuite patch
included to catch this as well.

Tested on i686-pc-linux-gnu/GCC3.1/DWARF-2; OK to commit?

David Carlton
carlton@math.stanford.edu

2003-01-31  David Carlton  <carlton@math.stanford.edu>

	* gdbtypes.h: Delete INTEGER_COERCION_BADNESS,
	FLOAT_COERCION_BADNESS.
	* gdbtypes.c (rank_one_type): Replace all uses of
	INTEGER_COERCION_BADNESS by INTEGER_CONVERSION_BADNESS.

2003-01-31  David Carlton  <carlton@math.stanford.edu>

	* gdb.c++/overload.exp: Test intToChar(1).
	* gdb.c++/overload.cc (intToChar): New.
	(main): Call intToChar.

Index: gdbtypes.h
===================================================================
RCS file: /cvs/src/src/gdb/gdbtypes.h,v
retrieving revision 1.42
diff -u -p -r1.42 gdbtypes.h
--- gdbtypes.h	19 Jan 2003 04:06:45 -0000	1.42
+++ gdbtypes.h	31 Jan 2003 19:26:04 -0000
@@ -1211,10 +1211,6 @@ extern int count_virtual_fns (struct typ
 #define TOO_FEW_PARAMS_BADNESS       100
 /* Badness if no conversion among types */
 #define INCOMPATIBLE_TYPE_BADNESS    100
-/* Badness of coercing large integer to smaller size */
-#define INTEGER_COERCION_BADNESS     100
-/* Badness of coercing large floating type to smaller size */
-#define FLOAT_COERCION_BADNESS       100
 
 /* Badness of integral promotion */
 #define INTEGER_PROMOTION_BADNESS      1
Index: gdbtypes.c
===================================================================
RCS file: /cvs/src/src/gdb/gdbtypes.c,v
retrieving revision 1.69
diff -u -p -r1.69 gdbtypes.c
--- gdbtypes.c	17 Jan 2003 19:12:18 -0000	1.69
+++ gdbtypes.c	31 Jan 2003 19:28:36 -0000
@@ -2591,7 +2591,7 @@ rank_one_type (struct type *parm, struct
 		  if (TYPE_NOSIGN (arg))	/* plain char -> plain char */
 		    return 0;
 		  else
-		    return INTEGER_COERCION_BADNESS;	/* signed/unsigned char -> plain char */
+		    return INTEGER_CONVERSION_BADNESS;	/* signed/unsigned char -> plain char */
 		}
 	      else if (TYPE_UNSIGNED (parm))
 		{
@@ -2604,13 +2604,13 @@ rank_one_type (struct type *parm, struct
 			       && integer_types_same_name_p (TYPE_NAME (parm), "long"))
 			return INTEGER_PROMOTION_BADNESS;	/* unsigned int -> unsigned long */
 		      else
-			return INTEGER_COERCION_BADNESS;	/* unsigned long -> unsigned int */
+			return INTEGER_CONVERSION_BADNESS;	/* unsigned long -> unsigned int */
 		    }
 		  else
 		    {
 		      if (integer_types_same_name_p (TYPE_NAME (arg), "long")
 			  && integer_types_same_name_p (TYPE_NAME (parm), "int"))
-			return INTEGER_COERCION_BADNESS;	/* signed long -> unsigned int */
+			return INTEGER_CONVERSION_BADNESS;	/* signed long -> unsigned int */
 		      else
 			return INTEGER_CONVERSION_BADNESS;	/* signed int/long -> unsigned int/long */
 		    }
@@ -2623,15 +2623,15 @@ rank_one_type (struct type *parm, struct
 			   && integer_types_same_name_p (TYPE_NAME (parm), "long"))
 		    return INTEGER_PROMOTION_BADNESS;
 		  else
-		    return INTEGER_COERCION_BADNESS;
+		    return INTEGER_CONVERSION_BADNESS;
 		}
 	      else
-		return INTEGER_COERCION_BADNESS;
+		return INTEGER_CONVERSION_BADNESS;
 	    }
 	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
 	    return INTEGER_PROMOTION_BADNESS;
 	  else
-	    return INTEGER_COERCION_BADNESS;
+	    return INTEGER_CONVERSION_BADNESS;
 	case TYPE_CODE_ENUM:
 	case TYPE_CODE_CHAR:
 	case TYPE_CODE_RANGE:
@@ -2653,7 +2653,7 @@ rank_one_type (struct type *parm, struct
 	case TYPE_CODE_RANGE:
 	case TYPE_CODE_BOOL:
 	case TYPE_CODE_ENUM:
-	  return INTEGER_COERCION_BADNESS;
+	  return INTEGER_CONVERSION_BADNESS;
 	case TYPE_CODE_FLT:
 	  return INT_FLOAT_CONVERSION_BADNESS;
 	default:
@@ -2666,12 +2666,12 @@ rank_one_type (struct type *parm, struct
 	case TYPE_CODE_RANGE:
 	case TYPE_CODE_BOOL:
 	case TYPE_CODE_ENUM:
-	  return INTEGER_COERCION_BADNESS;
+	  return INTEGER_CONVERSION_BADNESS;
 	case TYPE_CODE_FLT:
 	  return INT_FLOAT_CONVERSION_BADNESS;
 	case TYPE_CODE_INT:
 	  if (TYPE_LENGTH (arg) > TYPE_LENGTH (parm))
-	    return INTEGER_COERCION_BADNESS;
+	    return INTEGER_CONVERSION_BADNESS;
 	  else if (TYPE_LENGTH (arg) < TYPE_LENGTH (parm))
 	    return INTEGER_PROMOTION_BADNESS;
 	  /* >>> !! else fall through !! <<< */
@@ -2683,7 +2683,7 @@ rank_one_type (struct type *parm, struct
 	      if (TYPE_NOSIGN (arg))
 		return 0;
 	      else
-		return INTEGER_COERCION_BADNESS;
+		return INTEGER_CONVERSION_BADNESS;
 	    }
 	  else if (TYPE_UNSIGNED (parm))
 	    {
@@ -2695,7 +2695,7 @@ rank_one_type (struct type *parm, struct
 	  else if (!TYPE_NOSIGN (arg) && !TYPE_UNSIGNED (arg))
 	    return 0;
 	  else
-	    return INTEGER_COERCION_BADNESS;
+	    return INTEGER_CONVERSION_BADNESS;
 	default:
 	  return INCOMPATIBLE_TYPE_BADNESS;
 	}
@@ -2708,7 +2708,7 @@ rank_one_type (struct type *parm, struct
 	case TYPE_CODE_RANGE:
 	case TYPE_CODE_BOOL:
 	case TYPE_CODE_ENUM:
-	  return INTEGER_COERCION_BADNESS;
+	  return INTEGER_CONVERSION_BADNESS;
 	case TYPE_CODE_FLT:
 	  return INT_FLOAT_CONVERSION_BADNESS;
 	default:


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

* Re: [rfa] handle integer downsizing correctly in C++ overloading
  2003-01-31 19:57 [rfa] handle integer downsizing correctly in C++ overloading David Carlton
@ 2003-01-31 20:04 ` David Carlton
  2003-02-04 19:10 ` Daniel Jacobowitz
  1 sibling, 0 replies; 4+ messages in thread
From: David Carlton @ 2003-01-31 20:04 UTC (permalink / raw)
  To: gdb-patches; +Cc: Daniel Jacobowitz

On 31 Jan 2003 11:57:29 -0800, David Carlton <carlton@math.Stanford.EDU> said:

> And there's a testsuite patch included to catch this as well.

Whoops, I forgot to include the testsuite patch; here it is, if
anybody's curious.

David Carlton
carlton@math.stanford.edu

2003-01-31  David Carlton  <carlton@math.stanford.edu>

	* gdb.c++/overload.exp: Test intToChar(1).
	* gdb.c++/overload.cc (intToChar): New.
	(main): Call intToChar.

Index: overload.exp
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.c++/overload.exp,v
retrieving revision 1.9
diff -u -p -r1.9 overload.exp
--- overload.exp	13 Jan 2003 20:09:33 -0000	1.9
+++ overload.exp	31 Jan 2003 19:20:38 -0000
@@ -378,3 +378,5 @@ gdb_test "list foo::overloadfnarg(int, i
 gdb_test "list \"foo::overloadfnarg(int, int (*)(int))\"" \
    "int foo::overloadfnarg.*\\(int arg, int \\(\\*foo\\) \\(int\\)\\).*" \
    "list overloaded function with function ptr args - quotes around argument"
+
+gdb_test "print intToChar(1)" ".\[0-9\]* = 297"
Index: overload.cc
===================================================================
RCS file: /cvs/src/src/gdb/testsuite/gdb.c++/overload.cc,v
retrieving revision 1.1.1.3
diff -u -p -r1.1.1.3 overload.cc
--- overload.cc	22 Sep 1999 03:26:23 -0000	1.1.1.3
+++ overload.cc	31 Jan 2003 19:24:11 -0000
@@ -45,6 +45,11 @@ int overloadargs (int a1, int a2, int a3
 
 };
 
+int intToChar (char c)
+{
+  return 297;
+}
+
 void marker1()
 {}
 
@@ -72,6 +77,8 @@ int main () 
        breakpoint();
     #endif
 
+    // Verify that intToChar should work:
+    intToChar(1);
 
     marker1();
     return 0; 


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

* Re: [rfa] handle integer downsizing correctly in C++ overloading
  2003-01-31 19:57 [rfa] handle integer downsizing correctly in C++ overloading David Carlton
  2003-01-31 20:04 ` David Carlton
@ 2003-02-04 19:10 ` Daniel Jacobowitz
  2003-02-04 21:23   ` David Carlton
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Jacobowitz @ 2003-02-04 19:10 UTC (permalink / raw)
  To: David Carlton; +Cc: gdb-patches

On Fri, Jan 31, 2003 at 11:57:29AM -0800, David Carlton wrote:
> GDB currently won't let you do narrowing integer conversions when
> calling an overloaded C++ function: compile this,
> 
> int overloadChar(char c)
> {
>   return 29;
> }
> 
> int main()
> {
>   overloadChar(1);
> }
> 
> break on main, and try to print overloadChar(1).
> 
> The problem is that gdbtypes.h defines both INTEGER_COERCION_BADNESS
> and INTEGER_CONVERSION_BADNESS.  The former is unacceptably bad; the
> latter isn't preferred, but isn't acceptable.  And in all (or almost
> all) cases, the type comparison functions use INTEGER_COERCION_BADNESS
> when doing narrowing integer comparisons, when they should use
> INTEGER_CONVERSION_BADNESS.
> 
> In fact, INTEGER_COERCION_BADNESS shouldn't exist: there's no such
> thing as an unacceptably bad integer conversion.  So this patch
> changes all uses of INTEGER_COERCION_BADNESS to refer to
> INTEGER_CONVERSION_BADNESS, deletes INTEGER_COERCION_BADNESS, and also
> deletes FLOAT_COERCION_BADNESS (which is similarly unnecessary but
> which is already correctly unused).  And there's a testsuite patch
> included to catch this as well.
> 
> Tested on i686-pc-linux-gnu/GCC3.1/DWARF-2; OK to commit?

I (still) don't have a copy of the standard; I think I'll see about
getting one this week.  However, your explanation makes sense, and
matches my reading of the conversion code that GCC uses for overload
resolution (gcc/cp/call.c:build_conv and standard_conversion).  This
patch is OK.


-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer


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

* Re: [rfa] handle integer downsizing correctly in C++ overloading
  2003-02-04 19:10 ` Daniel Jacobowitz
@ 2003-02-04 21:23   ` David Carlton
  0 siblings, 0 replies; 4+ messages in thread
From: David Carlton @ 2003-02-04 21:23 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches

On Tue, 4 Feb 2003 13:32:22 -0500, Daniel Jacobowitz <drow@mvista.com> said:

> I (still) don't have a copy of the standard; I think I'll see about
> getting one this week.  However, your explanation makes sense, and
> matches my reading of the conversion code that GCC uses for overload
> resolution (gcc/cp/call.c:build_conv and standard_conversion).  This
> patch is OK.

Thanks, committed.  To be honest, I would be shocked if we were
getting conversion completely correct even after this patch (even
setting aside scope issues arising from namespaces, subclasses, using
declarations, and the like), but it's an improvement.  I've read the
relevant section of the standard only enough to know that:

* Narrowing integer conversions are definitely acceptable, albeit not
  optimal.

* Anybody who can figure out how overloading and namespaces should
  interact based on what the standard says is a _lot_ better at
  reading standards than I am.

In cases like this, I've started adding code to the .cc files in the
testsuite to verify that the compiler and I agree with what the
standard says, because I don't always trust my understanding of these
issues.

David Carlton
carlton@math.stanford.edu


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

end of thread, other threads:[~2003-02-04 21:23 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-01-31 19:57 [rfa] handle integer downsizing correctly in C++ overloading David Carlton
2003-01-31 20:04 ` David Carlton
2003-02-04 19:10 ` Daniel Jacobowitz
2003-02-04 21:23   ` David Carlton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox