[foreign-jextract] RFR: 8252759: Jextract flattens nested anonymous structs and unions when computing layouts [v3]

Jorn Vernee jvernee at openjdk.java.net
Fri Jan 15 14:38:36 UTC 2021


> Hi,
> 
> This patch changes the way jextract handles nested structs and unions.
> 
> Currently, when encountering an anonymous struct or union, jextract folds it's fields into the parent layout. But, this can lead to incorrect layouts, especially for unions. e.g. see the following example:
> 
>     struct Foo {
>         int x;
>         union {
>             int y;
>             int z;
>         };
>     }; 
>     
> becomes:
> 
>     static final MemoryLayout Foo$struct$LAYOUT_ = MemoryLayout.ofStruct(
>         C_INT.withName("x"),
>         C_INT.withName("y"),
>         C_INT.withName("z")
>     ).withName("Foo"); 
>     
> In this case, the offset of the `z` field will not be correct.
> 
> This patch changes the jextract behaviour to preserve the nested layouts of anonymous unions and structs, so the example becomes:
> 
>     static final MemoryLayout Foo$struct$LAYOUT_ = MemoryLayout.ofStruct(
>         C_INT.withName("x"),
>         MemoryLayout.ofUnion(
>             C_INT.withName("y"),
>             C_INT.withName("z")
>         ).withName("$anon$0")
>     ).withName("Foo"); 
>     
> Here, the anonymous layout gets a generated name which is needed so that the `y` and `z` fields can still be reached from the root layout with layout paths to create var handles for instance.
> 
> Just the layout is changed, the Foo java class that we generate still has getters and setters for `y` and `z`.
> 
> In the patch this behaviour is implemented by temporarily adding a 'prefix' (`"$anon$0"` in this case) to the `StructBuilder`, which is used in the generated layout paths to reach the nested fields.
> 
> Thanks,
> Jorn

Jorn Vernee has updated the pull request incrementally with one additional commit since the last revision:

  Unify tests

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

Changes:
  - all: https://git.openjdk.java.net/panama-foreign/pull/434/files
  - new: https://git.openjdk.java.net/panama-foreign/pull/434/files/c3e4f479..4a6bb618

Webrevs:
 - full: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=434&range=02
 - incr: https://webrevs.openjdk.java.net/?repo=panama-foreign&pr=434&range=01-02

  Stats: 102 lines in 4 files changed: 13 ins; 89 del; 0 mod
  Patch: https://git.openjdk.java.net/panama-foreign/pull/434.diff
  Fetch: git fetch https://git.openjdk.java.net/panama-foreign pull/434/head:pull/434

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


More information about the panama-dev mailing list