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

Maurizio Cimadamore mcimadamore at openjdk.java.net
Thu Aug 6 18:02:32 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.

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

Commit messages:
 - Fix whitespaces
 - Add recursive builder hierarchy to allow for uniform nesting

Changes: https://git.openjdk.java.net/panama-foreign/pull/280/files
 Webrev: https://webrevs.openjdk.java.net/panama-foreign/280/webrev.00
  Issue: https://bugs.openjdk.java.net/browse/JDK-8251256
  Stats: 292 lines in 6 files changed: 99 ins; 41 del; 152 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