RFR: 7903613: Bad nested names are sometimes attached to structs [v2]

Maurizio Cimadamore mcimadamore at openjdk.org
Tue Dec 19 14:42:17 UTC 2023


> 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 with a new target base due to a merge or a rebase. The pull request now contains eight commits:

 - Drop spurious code changes
 - Make nested declaration finder logic more robust
 - Fix out of order test
 - Add tests
 - Tweak nested decl finder to look inside struct members that are themselves structs
 - Add new nested decl scanner
   Tweak EnumConstantLifter to use the new scanner
 - Drop unused field in OutputFactory
 - All tests pass

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

Changes: https://git.openjdk.org/jextract/pull/167/files
 Webrev: https://webrevs.openjdk.org/?repo=jextract&pr=167&range=01
  Stats: 329 lines in 12 files changed: 266 ins; 31 del; 32 mod
  Patch: https://git.openjdk.org/jextract/pull/167.diff
  Fetch: git fetch https://git.openjdk.org/jextract.git pull/167/head:pull/167

PR: https://git.openjdk.org/jextract/pull/167


More information about the jextract-dev mailing list