[foreign-jextract] RFR: 8251256: jextrac code generated for nested structs, unions is incorrect [v2]

Maurizio Cimadamore mcimadamore at openjdk.java.net
Fri Aug 7 11:16:13 UTC 2020


> 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.

Maurizio Cimadamore has updated the pull request incrementally with one additional commit since the last revision:

  Fix indentation in generated code

-------------

Changes:
  - all: https://git.openjdk.java.net/panama-foreign/pull/280/files
  - new: https://git.openjdk.java.net/panama-foreign/pull/280/files/9271d6b4..f1c62878

Webrevs:
 - full: https://webrevs.openjdk.java.net/panama-foreign/280/webrev.01
 - incr: https://webrevs.openjdk.java.net/panama-foreign/280/webrev.00-01

  Stats: 59 lines in 4 files changed: 42 ins; 17 del; 0 mod
  Patch: https://git.openjdk.java.net/panama-foreign/pull/280.diff
  Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/280/head:pull/280

PR: https://git.openjdk.java.net/panama-foreign/pull/280


More information about the panama-dev mailing list