[SOLVED][FlexSpin] Confusion about pointers to structures or compiler bug?
Hey @ersmith
Can you tell me if I'm misusing pointers to structures or if this might be a bug?
Consider this top-level object:
{Spin2_v51} con struct mystruct(... word m1, ... word m2, ... word m3) obj ch: "child" pub main() | mystruct realstruct ch.set_struct(realstruct)
and its child object
{Spin2_v51} con struct mystruct(... word m1, ... word m2, ... word m3) var ^mystruct pstruct pub set_struct(p_hdr) [pstruct] := p_hdr
fails to build with error: Bad number of parameters in call to set_struct: expected 1 found 2
This originated from a UDP header parser/writer I'm working on. My intent is to have the actual instance of the UDP header structure defined in the calling object, and just a pointer to it in the UDP object. The UDP object would be able to indirectly read/modify the instance.
I have similar objects for other network layers (e.g., eth-ii, IPv4) that use the same syntax and they build fine and seem to work - that's why I wasn't sure if I hit a bug.
Thanks!
Comments
@avsa242 I think you forgot an @ in the call to
ch.set_struct
. and also didn't declare the parameter toset_struct
to explicitly be a structure pointer. You have to do at least one or the other in order for the compiler to figure out that you want to pass a structure pointer rather than the whole structure.Great, I was hoping that's all it was. At first, changing only that didn't help (still had garbage data and/or crashes). Despite seeming to work, the earlier protocol dissectors (eth-ii, ipv4) were written the same way they are in my first post. They both built and worked fine, but obviously it was just a case of I got lucky and it "happened to work."
After rewriting them the same way, all three now work correctly.
For (hopefully) clarity, the change I made equates to this change to the first post:
ch.set_struct(@realstruct)
I found if I made that change and defined the param in
set_struct
as a pointer to structure, everything still worked, but a warning about incompatible pointer types was generated during build.@avsa242 The warning about incompatible pointer types may safely be ignored, and I've fixed it in github. The type checker was being a bit too zealous for Spin2. which has very relaxed "types". The root cause is that we automatically convert structs being passed to ^structs to pointers, and also convert @struct to a pointer, with the result that the conversion was happening twice and we were getting a pointer to a pointer to the struct.