[foreign-jextract] RFR: 8251256: jextrac code generated for nested structs, unions is incorrect
Athijegannathan Sundararajan
sundar at openjdk.java.net
Fri Aug 7 08:56:04 UTC 2020
On Fri, 7 Aug 2020 08:53:13 GMT, Athijegannathan Sundararajan <sundar at openjdk.org> wrote:
>> This patch rectifies the behavior of jextract with respect to nested structs and/or unions.
>>
>> More specifically, jextract failed to recursively generate interfaces for anonymous structs and unioins; this led to a
>> situation (as described in the JBS issue) where the only operation a client can do is to retrieve a segment for the
>> nested struct/union, but all nested accessors are not available. The fix allows jextract to recurse; more
>> specifically, if a struct/union field type is itself an anonymous struct/union, the interface for the anonymous
>> struct/union will be generated (nested inside the current struct interface). So, in the example submitted in the JBS
>> issue, it would be possible to poke at the nested union field, as follows:
>> MemorySegment rdma = ibv_send_wr.wr$addr(someSegment);
>> ibv_send_wr.wr.rdma.remote_addr$get(rdma);
>>
>> Note the nesting: `ibv_send_wr.wr.rdma` where:
>>
>> * `ibv_send_wr` is the enclosing struct declaration
>> * `wr` is the union declaration, nested inside the struct
>> * `rdma` is a struct that is one of the union alternatives
>>
>> To make the code more uniform, I've reworked JavaSourceBuilder a little - more specifically:
>>
>> * JavaSourceBuilder is aware of the fact that builders can *nest* into each other
>> * Rather than dumping contents of a child builder into the parent, we use delegation, to make sure that all calls to
>> `append` eventually hit the `StringBuilder` in the toplevel builder --- effectively removing the need to copy
>> * each builder has its own name context (to fixup duplicate names); this follows from the fact that each nested class
>> introduces a fresh scope, so no clashes with names defined outside are possible
>>
>> While I've added no test, I found out that an existing test (Test8244512) was already triggering the scenario described
>> in the JBS issue. I've tweaked the test to take into account the fact that now jextract correctly nests inner structs,
>> as well as the fact that jextract is now adding interfaces even for the anon structs/union attached to struct/union
>> fields.
>
> Marked as reviewed by sundar (Committer).
I tested the patch on Mac and ran all samples as well. All fine.
-------------
PR: https://git.openjdk.java.net/panama-foreign/pull/280
More information about the panama-dev
mailing list