Sources Bugzilla – Bug 5634
support pointer type punning
Last modified: 2009-02-19 21:42:36 UTC
It would be helpful to have a syntax for pointer type casting, for times when the static type of the pointer does not match the dynamic (actual) type. Currently, this requires embedded-C code to get around. Perhaps like this: $ptr("struct tcp_socket")->tcp_socket_field A more C-like syntax would be: ((struct tcp_socket*)$ptr)->tcp_socket_field but parsing that is much more complicated.
Yes, this would be very useful, whatever the syntax. In the example given, the pointer is a target variable ($ptr), but it seems important to be able to cast script variables as well.
Can this support casting to an array like below? ((struct tcp_socket[])$ptr)[1]->tcp_socket_field or $ptr("struct tcp_socket []")[0]->tcp_socket_field
*** Bug 6704 has been marked as a duplicate of this bug. ***
The '$ptr("type")' looks too similar functions calls, especially if we allow script variables or expressions for the pointer source, and parsing a C-like cast just scares me. I was thinking of instead making the cast a builtin function, either a C++-like 'cast<struct tcp_socket *>($ptr)' or a simple 'cast("struct tcp_socket *", $ptr)'. The latter is slightly preferred because it wouldn't change our tokenization. Thoughts?
You might like the @cast() syntax suggested in bug #6704.
I've added typecasting in commit 23ad66b, which I think will be enough for most use cases. (In reply to comment #2) > Can this support casting to an array like below? I did not add such support, but if it's enabled for #4166, then it should work for this @cast too.
Release 0.9 includes @cast() support.