Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* RFA: static cast from base class to derived class
@ 2002-08-20 23:11 Jim Blandy
  2002-08-21  5:25 ` Daniel Jacobowitz
  0 siblings, 1 reply; 3+ messages in thread
From: Jim Blandy @ 2002-08-20 23:11 UTC (permalink / raw)
  To: gdb-patches


Once when my grandfather was a boy, he was riding his bicycle along a
gravel path, and going pretty fast, when he hit a stone, went flying
over the handlebars, and landed on his face.  The gravel was ground so
deeply into his face that, even in his forties, he'd still
occasionally encounter, while shaving, bits of rock that had finally
worked their way out to the surface.

Here's a patch for a bit of 1998 HP Merge gravel that has finally
found its way to the surface.

I didn't see any regressions with this patch using either STABS or
Dwarf 2.  I'll put together a regression test for the original bug
tomorrow.

2002-08-20  Jim Blandy  <jimb@redhat.com>

	* valops.c (value_cast): Simplify and correct logic for doing a
	static cast from a pointer to a base class to a pointer to a
	derived class.

Index: gdb/valops.c
===================================================================
RCS file: /cvs/src/src/gdb/valops.c,v
retrieving revision 1.67
diff -c -r1.67 valops.c
*** gdb/valops.c	19 Aug 2002 23:19:53 -0000	1.67
--- gdb/valops.c	21 Aug 2002 02:42:21 -0000
***************
*** 361,378 ****
  				       value_zero (t1, not_lval), 0, t1, 1);
  		  if (v)
  		    {
! 		      struct value *v2 = value_ind (arg2);
! 		      VALUE_ADDRESS (v2) -= VALUE_ADDRESS (v)
! 			+ VALUE_OFFSET (v);
! 
!                       /* JYG: adjust the new pointer value and
! 			 embedded offset. */
!                       v2->aligner.contents[0] -=  VALUE_EMBEDDED_OFFSET (v);
!                       VALUE_EMBEDDED_OFFSET (v2) = 0;
! 
! 		      v2 = value_addr (v2);
! 		      VALUE_TYPE (v2) = type;
! 		      return v2;
  		    }
  		}
  	    }
--- 361,371 ----
  				       value_zero (t1, not_lval), 0, t1, 1);
  		  if (v)
  		    {
!                       CORE_ADDR addr2 = value_as_address (arg2);
!                       addr2 -= (VALUE_ADDRESS (v)
!                                 + VALUE_OFFSET (v)
!                                 + VALUE_EMBEDDED_OFFSET (v));
!                       return value_from_pointer (type, addr2);
  		    }
  		}
  	    }


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

* Re: RFA: static cast from base class to derived class
  2002-08-20 23:11 RFA: static cast from base class to derived class Jim Blandy
@ 2002-08-21  5:25 ` Daniel Jacobowitz
  2002-08-21 10:22   ` Jim Blandy
  0 siblings, 1 reply; 3+ messages in thread
From: Daniel Jacobowitz @ 2002-08-21  5:25 UTC (permalink / raw)
  To: Jim Blandy; +Cc: gdb-patches

On Wed, Aug 21, 2002 at 01:00:52AM -0500, Jim Blandy wrote:
> 
> Once when my grandfather was a boy, he was riding his bicycle along a
> gravel path, and going pretty fast, when he hit a stone, went flying
> over the handlebars, and landed on his face.  The gravel was ground so
> deeply into his face that, even in his forties, he'd still
> occasionally encounter, while shaving, bits of rock that had finally
> worked their way out to the surface.
> 
> Here's a patch for a bit of 1998 HP Merge gravel that has finally
> found its way to the surface.
> 
> I didn't see any regressions with this patch using either STABS or
> Dwarf 2.  I'll put together a regression test for the original bug
> tomorrow.
> 
> 2002-08-20  Jim Blandy  <jimb@redhat.com>
> 
> 	* valops.c (value_cast): Simplify and correct logic for doing a
> 	static cast from a pointer to a base class to a pointer to a
> 	derived class.
> 
> Index: gdb/valops.c
> ===================================================================
> RCS file: /cvs/src/src/gdb/valops.c,v
> retrieving revision 1.67
> diff -c -r1.67 valops.c
> *** gdb/valops.c	19 Aug 2002 23:19:53 -0000	1.67
> --- gdb/valops.c	21 Aug 2002 02:42:21 -0000
> ***************
> *** 361,378 ****
>   				       value_zero (t1, not_lval), 0, t1, 1);
>   		  if (v)
>   		    {
> ! 		      struct value *v2 = value_ind (arg2);
> ! 		      VALUE_ADDRESS (v2) -= VALUE_ADDRESS (v)
> ! 			+ VALUE_OFFSET (v);
> ! 
> !                       /* JYG: adjust the new pointer value and
> ! 			 embedded offset. */
> !                       v2->aligner.contents[0] -=  VALUE_EMBEDDED_OFFSET (v);
> !                       VALUE_EMBEDDED_OFFSET (v2) = 0;
> ! 
> ! 		      v2 = value_addr (v2);
> ! 		      VALUE_TYPE (v2) = type;
> ! 		      return v2;
>   		    }
>   		}
>   	    }
> --- 361,371 ----
>   				       value_zero (t1, not_lval), 0, t1, 1);
>   		  if (v)
>   		    {
> !                       CORE_ADDR addr2 = value_as_address (arg2);
> !                       addr2 -= (VALUE_ADDRESS (v)
> !                                 + VALUE_OFFSET (v)
> !                                 + VALUE_EMBEDDED_OFFSET (v));
> !                       return value_from_pointer (type, addr2);
>   		    }
>   		}
>   	    }

I've got a question... what does VALUE_ADDRESS mean in this context? 
If it means what it normally means (ought to mean?), then taking an
address, subtracting an address, and using it as a pointer doesn't make
a lot of sense.

Answering my own question - it's dereferencing a pointer to a struct at
0.  So we'll actually get an address relative to zero, which makes
everything work out.  This looks good to me.

-- 
Daniel Jacobowitz
MontaVista Software                         Debian GNU/Linux Developer


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

* Re: RFA: static cast from base class to derived class
  2002-08-21  5:25 ` Daniel Jacobowitz
@ 2002-08-21 10:22   ` Jim Blandy
  0 siblings, 0 replies; 3+ messages in thread
From: Jim Blandy @ 2002-08-21 10:22 UTC (permalink / raw)
  To: Daniel Jacobowitz; +Cc: gdb-patches


Daniel Jacobowitz <drow@mvista.com> writes:
> I've got a question... what does VALUE_ADDRESS mean in this context? 
> If it means what it normally means (ought to mean?), then taking an
> address, subtracting an address, and using it as a pointer doesn't make
> a lot of sense.
> 
> Answering my own question - it's dereferencing a pointer to a struct at
> 0.  So we'll actually get an address relative to zero, which makes
> everything work out.  This looks good to me.

Right --- when casting from a base class to a derived class, we use
search_struct_field to find the offset the base class has in the
derived class by creating an imaginary instance of the derived class
at address zero, finding the base class in that, and then using the
base class subobject's address as the offset of the base class in the
derived class.

I wanted to fix the bug in what we did with that offset; beautifying
the way we produced it wasn't on my agenda.  :)

I'll commit this --- thanks.


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

end of thread, other threads:[~2002-08-21 17:22 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-08-20 23:11 RFA: static cast from base class to derived class Jim Blandy
2002-08-21  5:25 ` Daniel Jacobowitz
2002-08-21 10:22   ` Jim Blandy

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