Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [patch] fix for c++/2416
@ 2008-02-27 18:43 Aleksandar Ristovski
  2008-02-27 19:30 ` Michael Snyder
  2008-02-27 19:47 ` Daniel Jacobowitz
  0 siblings, 2 replies; 19+ messages in thread
From: Aleksandar Ristovski @ 2008-02-27 18:43 UTC (permalink / raw)
  To: gdb-patches

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

Hello,

As described in the bug report 2416, the problem is with casting to a reference. 
The attached patch should fix this.

Testing on head, no change in pass rate encountered (probably suggesting there 
is no test case for this - sample code in the bug report should be a good 
starting point).

Thanks,

Aleksandar Ristovski
QNX Software Systems

2008-02-27  Aleksandar Ristovski <aristovski@qnx.com>

	* eval.c (evaluate_subexp_standard): UNOP_CAST use
	value_cast_pointers when casting reference to reference. Print
	error when reference/non-reference mix.
	* value.c (value_as_address): Call coerce_array only on arrays.
	(unpack_long): Cover C++ cases TYPE_CODE_STRUCT and UNION.



[-- Attachment #2: casting.diff --]
[-- Type: text/plain, Size: 1981 bytes --]

Index: gdb/eval.c
===================================================================
RCS file: /cvs/src/src/gdb/eval.c,v
retrieving revision 1.80
diff -u -p -r1.80 eval.c
--- gdb/eval.c	4 Feb 2008 00:23:04 -0000	1.80
+++ gdb/eval.c	27 Feb 2008 17:01:03 -0000
@@ -1985,8 +1985,18 @@ evaluate_subexp_standard (struct type *e
       arg1 = evaluate_subexp (type, exp, pos, noside);
       if (noside == EVAL_SKIP)
 	goto nosideret;
-      if (type != value_type (arg1))
-	arg1 = value_cast (type, arg1);
+      if (type != value_type (arg1)) 
+	{
+	  if (TYPE_CODE (value_type (arg1)) == TYPE_CODE_REF
+	      && TYPE_CODE (type) == TYPE_CODE_REF)
+	    arg1 = value_cast_pointers (type, arg1);
+	  else if (TYPE_CODE (value_type (arg1)) != TYPE_CODE_REF
+		   && TYPE_CODE (type) != TYPE_CODE_REF)
+	    arg1 = value_cast (type, arg1);
+	  else /* We can not do much here.  */
+	    error (_("Attempt to cast to reference type from non-reference "\
+		     "type or vice versa."));
+	}
       return arg1;
 
     case UNOP_MEMVAL:
Index: gdb/value.c
===================================================================
RCS file: /cvs/src/src/gdb/value.c,v
retrieving revision 1.57
diff -u -p -r1.57 value.c
--- gdb/value.c	18 Jan 2008 17:07:40 -0000	1.57
+++ gdb/value.c	27 Feb 2008 17:01:03 -0000
@@ -1039,7 +1039,8 @@ value_as_address (struct value *val)
       || TYPE_CODE (value_type (val)) == TYPE_CODE_METHOD)
     return VALUE_ADDRESS (val);
 
-  val = coerce_array (val);
+  if (TYPE_CODE (value_type (val)) == TYPE_CODE_ARRAY)
+    val = coerce_array (val);
 
   /* Some architectures (e.g. Harvard), map instruction and data
      addresses onto a single large unified address space.  For
@@ -1120,6 +1121,8 @@ unpack_long (struct type *type, const gd
     case TYPE_CODE_CHAR:
     case TYPE_CODE_RANGE:
     case TYPE_CODE_MEMBERPTR:
+    case TYPE_CODE_STRUCT:
+    case TYPE_CODE_UNION:
       if (nosign)
 	return extract_unsigned_integer (valaddr, len);
       else

^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: [patch] fix for c++/2416
@ 2008-03-31 19:01 Aleksandar Ristovski
  0 siblings, 0 replies; 19+ messages in thread
From: Aleksandar Ristovski @ 2008-03-31 19:01 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches

Aleksandar Ristovski wrote:
> Daniel Jacobowitz wrote:
>> On Thu, Feb 28, 2008 at 01:53:13PM -0500, Aleksandar Ristovski wrote:
>>> See revisited diff (attached). Also, please find attached the 
>>> testcase diff, I added check for reference casting. Do I need to 
>>> provide change log for tests too?
>>
>> The testsuite changes look fine but do need a changelog; that
>> goes in testsuite/ChangeLog.
> 
> 2008-02-28  Aleksandar Ristovski  <aristovski@qnx.com>
> 
>     * gdb.cp/casts.cc: Add class reference variables.
>     * gdb.cp/casts.exp: New test cases for up/down casting references.
> 

Is the testsuite diff (including changelog) ok to commit? The added tests
will 
add 2 more FAIL-s and 2 more PASS-es to the tests, which should turn into 4
more 
PASS-es, pending the fix to PR2416.

> 
>>
>> There's nothing wrong with casting a reference to a non-reference;
>> that coerces the reference.  Also you have to beware typedefs.
>>
>>       enum type_code code1, code2;
>>       code1 = TYPE_CODE (check_typedef (value_type (arg1)));
>>       code2 = TYPE_CODE (check_typedef (value_type (arg2)));
>>
>>       if (code1 == TYPE_CODE_REF && code2 == TYPE_CODE_REF)
>>         arg1 = value_cast_pointers (type, arg1);
>>       else if (code1 == TYPE_CODE_REF)
>>         error (_("Attempt to cast to reference type from non-reference 
>> type."));
>>       else
>>         arg1 = value_cast (type, arg1);
> 
> Allright, then how about this, yet newer and yet more revisited diff? I 
> removed changes to eval.c and let it simply call value_cast as it used 
> to. Now value_cast knows how to handle references.
> 
> 
> 2008-02-28  Aleksandar Ristovski <aristovski@qnx.com>
> 
>     * valops.c (value_cast_structs): New function. Cast related
>     STRUCT types up/down and return cast value. The body of this
>     function comes mostly from value_cast_pointers.
>     (value_cast_pointers): Code for actual cast STRUCT-STRUCT moved
>     to value_cast_structs. Now value_cast_pointers needs only create
>     appropriate reference after using value_cast_structs for actual
>     casting.
>     (value_cast): Handle references.
>     
> 

I haven't received any feedback on this.


^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: [patch] fix for c++/2416
@ 2008-04-01 16:26 Aleksandar Ristovski
  2008-04-01 17:06 ` Daniel Jacobowitz
  0 siblings, 1 reply; 19+ messages in thread
From: Aleksandar Ristovski @ 2008-04-01 16:26 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches

Daniel Jacobowitz wrote:
> On Thu, Feb 28, 2008 at 11:45:07PM -0500, Aleksandar Ristovski wrote:
>> Allright, then how about this, yet newer and yet more revisited diff? I 
>> removed changes to eval.c and let it simply call value_cast as it used
to. 
>> Now value_cast knows how to handle references.
> 
> We were converging on a fix and then there's a whole different patch
> to look at... sorry I couldn't make time until now.
> 
>> @@ -257,6 +290,7 @@ value_cast_pointers (struct type *type, 
>>    return arg2;
>>  }
>>  
>> +
>>  /* Cast value ARG2 to type TYPE and return as a value.
>>     More general than a C cast: accepts any two types of the same length,
>>     and if ARG2 is an lvalue it can be cast into anything at all.  */
> 
> Please drop the whitespace change.
> 
>> @@ -275,6 +309,26 @@ value_cast (struct type *type, struct va
>>    if (value_type (arg2) == type)
>>      return arg2;
>>  
>> +  code1 = TYPE_CODE (check_typedef (type));
>> +
>> +  /* Check if we are casting struct reference to struct reference.  */
>> +  if (code1 == TYPE_CODE_REF)
>> +    {
>> +      /* We dereference type; then we recurse and finally
>> +         we generate value of the given reference. Nothing wrong with 
>> +	 that.  */
>> +      struct type *t1 = check_typedef (type);
>> +      struct type *dereftype = check_typedef (TYPE_TARGET_TYPE (t1));
>> +      struct value *val =  value_cast (dereftype, arg2);
>> +      return value_ref (val); 
>> +    }
> 
> This allows things like "(int&) int_var", by automatically creating
> references.  Should we really do that?
> 

I believe we should.

Here is a dummy c++ program where this is done explicitly:

#include <iostream>
using namespace std;
int main ()
{
   int a = 42;
   int &refa = (int &)a; // cast not needed, yet compiler doesn't complain.
   cout << refa << endl;
   return 0;
}



^ permalink raw reply	[flat|nested] 19+ messages in thread
* Re: [patch] fix for c++/2416
@ 2008-04-01 18:10 Aleksandar Ristovski
  2008-04-01 18:52 ` Daniel Jacobowitz
  0 siblings, 1 reply; 19+ messages in thread
From: Aleksandar Ristovski @ 2008-04-01 18:10 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches

Daniel Jacobowitz wrote:
> If you'll delete the extra blank line, the patch and testcases are OK.

Removed blank line, both code and tests committed. Thanks for reviewing the
patch.

The PR 2416 should probably be updated too... I haven't done that, should I?

Oh... and how do I change gnats password?


BTW, thanks for fixing the date on my previous ChangeLog commit, I had
blindly 
copied my prepared change log without changing the date.


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

end of thread, other threads:[~2008-04-01 17:06 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-02-27 18:43 [patch] fix for c++/2416 Aleksandar Ristovski
2008-02-27 19:30 ` Michael Snyder
2008-02-27 19:36   ` Aleksandar Ristovski
2008-02-27 19:47 ` Daniel Jacobowitz
2008-02-27 20:00   ` Aleksandar Ristovski
2008-02-27 20:09     ` Daniel Jacobowitz
2008-02-27 21:26       ` Aleksandar Ristovski
2008-02-27 23:50         ` Daniel Jacobowitz
2008-02-28 19:12           ` Aleksandar Ristovski
2008-02-28 19:39             ` Daniel Jacobowitz
2008-02-29 10:10               ` Aleksandar Ristovski
2008-03-04 19:39                 ` Aleksandar Ristovski
2008-04-01 14:51                 ` Daniel Jacobowitz
2008-02-29  2:44             ` Michael Snyder
2008-03-31 19:01 Aleksandar Ristovski
2008-04-01 16:26 Aleksandar Ristovski
2008-04-01 17:06 ` Daniel Jacobowitz
2008-04-01 18:10 Aleksandar Ristovski
2008-04-01 18:52 ` Daniel Jacobowitz

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