RFR: 7903613: Bad nested names are sometimes attached to structs [v6]
Maurizio Cimadamore
mcimadamore at openjdk.org
Wed Dec 20 16:21:20 UTC 2023
On Wed, 20 Dec 2023 16:10:24 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:
>> The `NameMangler` visitor is used to compute the Java name of a jextract declaration. This is implemented as a declaration visitor. Unfortunately, the logic that computes the Java name can be sensitive to the order in which declarations are visited (because this visitor features a "parent" declaration, whose contents affect as to whether a "nested" struct name is generated or not).
>>
>> In reality, the logic of the name mangler needs to be able to disambiguate between structs that are either anonymous, or already declared somewhere else, and structs that are declared as part of a typedef, variable, function parameter/return declaration. In the former case, we either need no Java name (anonymous struct) or a toplevel Java name. In the latter we need a nested struct name (as the struct class will be nested inside some other class).
>>
>> This PR introduces a new visitor which tags all struct/union/enum declarations which fall in the latter bucket. This is done with an algorithm which:
>>
>> 1. visits all declarations in a toplevel header
>> 2. remembers which scoped declarations have been seen *directly* (e.g. as part of the visit)
>> 3. keeps track of which scoped declarations can be seen *indirectly* (e.g. because they are behind some declared type)
>> 4. subtracts the declarations in (2) from the declarations in (3), and visits the declarations in the remaining set
>> 5. keeps performing (2), (3), (4) until there's no declaration in (3)
>>
>> All scoped declarations that appear exclusively as part of some declared type are augmented with the `NestedDecl` attribute, which is then read when calling `Utils::nestedDeclarationFor`. This ensures that all the jextract visitor only recurse on a scoped declaration attached to a type which is known not to have been seen anywhere else. As a result, the behavior of the name mangler is independent of the order in which declarations are seen.
>>
>> It should be possible, in principle, to leverage this infrastructure to define a declaration visitor that automatically looks inside "nested declarations" (so that subsequent visitors don't really need to concern with following declared types).
>>
>> I've tested this change with windows.h, which works as expected.
>
> Maurizio Cimadamore has updated the pull request incrementally with five additional commits since the last revision:
>
> - Fix mangling
> Add mangling test for nested decls
> - Add more comments
> - Fix function pointer typedef mangled names for nested struct in param/returns
> - Better names for function parameter/return structs
> - Deal with param/return nested decls
src/main/java/org/openjdk/jextract/impl/NameMangler.java line 163:
> 161: public Void visitScoped(Declaration.Scoped scoped, Declaration parent) {
> 162: if (Utils.isEnum(scoped)) {
> 163: scoped.members().forEach(fieldTree -> fieldTree.accept(this, null));
for enums, we don't have to create a new named scope. Also, enums are the only case where jextract leaves an anon enum at the toplevel (where parent == null), so better to skip.
test/jtreg/generator/nestedTypes/nested_types_names.h line 25:
> 23:
> 24: // function declarations
> 25: struct { int x; } f1(struct { int y; } p);
This is a pretty convoluted test where we check declaration in parameter/return position. There were many issues in these cases, as the old jextract mostly didn't care about such structs (and that was not problematic, given that layouts were inlined all the time).
test/testng/org/openjdk/jextract/test/toolprovider/TestClassGeneration.java line 118:
> 116: { "Foo", C_FLOAT.withName("f"), float.class, 10F },
> 117: { "Foo", C_DOUBLE.withName("d"), double.class, 10D },
> 118: { "Bar$0", C_INT.withName("a"), int.class, 10 },
This change is needed because the test looks for a private field (the OFFSET field), and such field is declared in the typedef class' superclass. The old logic was generating things backwards, so we didn't have this issue.
-------------
PR Review Comment: https://git.openjdk.org/jextract/pull/167#discussion_r1432917601
PR Review Comment: https://git.openjdk.org/jextract/pull/167#discussion_r1432920952
PR Review Comment: https://git.openjdk.org/jextract/pull/167#discussion_r1432919583
More information about the jextract-dev
mailing list