-
Notifications
You must be signed in to change notification settings - Fork 38
[jextract] Misc improvements #223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,11 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
/// store this along with type names as we import them. | ||
let targetJavaPackage: String | ||
|
||
var currentType: ImportedNominalType? = nil | ||
/// Type context stack associated with the syntax. | ||
var typeContext: [(syntaxID: Syntax.ID, type: ImportedNominalType)] = [] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, thank you! |
||
|
||
/// Innermost type context. | ||
var currentType: ImportedNominalType? { typeContext.last?.type } | ||
|
||
/// The current type name as a nested name like A.B.C. | ||
var currentTypeName: String? { self.currentType?.swiftTypeName } | ||
|
@@ -41,37 +45,50 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
super.init(viewMode: .all) | ||
} | ||
|
||
/// Push specified type to the type context associated with the syntax. | ||
func pushTypeContext(syntax: some SyntaxProtocol, importedNominal: ImportedNominalType) { | ||
typeContext.append((syntax.id, importedNominal)) | ||
} | ||
|
||
/// Pop type context if the current context is associated with the syntax. | ||
func popTypeContext(syntax: some SyntaxProtocol) -> Bool { | ||
if typeContext.last?.syntaxID == syntax.id { | ||
typeContext.removeLast() | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
override func visit(_ node: ClassDeclSyntax) -> SyntaxVisitorContinueKind { | ||
log.debug("Visit \(node.kind): \(node)") | ||
log.debug("Visit \(node.kind): '\(node.qualifiedNameForDebug)'") | ||
guard let importedNominalType = translator.importedNominalType(node) else { | ||
return .skipChildren | ||
} | ||
|
||
self.currentType = importedNominalType | ||
self.pushTypeContext(syntax: node, importedNominal: importedNominalType) | ||
return .visitChildren | ||
} | ||
|
||
override func visitPost(_ node: ClassDeclSyntax) { | ||
if currentType != nil { | ||
if self.popTypeContext(syntax: node) { | ||
log.debug("Completed import: \(node.kind) \(node.name)") | ||
self.currentType = nil | ||
} | ||
} | ||
|
||
override func visit(_ node: StructDeclSyntax) -> SyntaxVisitorContinueKind { | ||
log.debug("Visit \(node.kind): \(node)") | ||
log.debug("Visit \(node.kind): \(node.qualifiedNameForDebug)") | ||
guard let importedNominalType = translator.importedNominalType(node) else { | ||
return .skipChildren | ||
} | ||
|
||
self.currentType = importedNominalType | ||
self.pushTypeContext(syntax: node, importedNominal: importedNominalType) | ||
return .visitChildren | ||
} | ||
|
||
override func visitPost(_ node: StructDeclSyntax) { | ||
if currentType != nil { | ||
log.debug("Completed import: \(node.kind) \(node.name)") | ||
self.currentType = nil | ||
if self.popTypeContext(syntax: node) { | ||
log.debug("Completed import: \(node.kind) \(node.qualifiedNameForDebug)") | ||
} | ||
} | ||
|
||
|
@@ -84,13 +101,13 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
return .skipChildren | ||
} | ||
|
||
self.currentType = importedNominalType | ||
self.pushTypeContext(syntax: node, importedNominal: importedNominalType) | ||
return .visitChildren | ||
} | ||
|
||
override func visitPost(_ node: ExtensionDeclSyntax) { | ||
if currentType != nil { | ||
self.currentType = nil | ||
if self.popTypeContext(syntax: node) { | ||
log.debug("Completed import: \(node.kind) \(node.qualifiedNameForDebug)") | ||
} | ||
} | ||
|
||
|
@@ -148,6 +165,10 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
} | ||
|
||
override func visit(_ node: VariableDeclSyntax) -> SyntaxVisitorContinueKind { | ||
guard node.shouldImport(log: log) else { | ||
return .skipChildren | ||
} | ||
|
||
guard let binding = node.bindings.first else { | ||
return .skipChildren | ||
} | ||
|
@@ -156,7 +177,7 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
|
||
// TODO: filter out kinds of variables we cannot import | ||
|
||
self.log.debug("Import variable: \(node.kind) \(fullName)") | ||
self.log.debug("Import variable: \(node.kind) '\(node.qualifiedNameForDebug)'") | ||
|
||
let returnTy: TypeSyntax | ||
if let typeAnnotation = binding.typeAnnotation { | ||
|
@@ -169,7 +190,7 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
do { | ||
javaResultType = try cCompatibleType(for: returnTy) | ||
} catch { | ||
self.log.info("Unable to import variable \(node.debugDescription) - \(error)") | ||
log.info("Unable to import variable '\(node.qualifiedNameForDebug)' - \(error)") | ||
return .skipChildren | ||
} | ||
|
||
|
@@ -190,7 +211,7 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
log.debug("Record variable in \(currentTypeName)") | ||
translator.importedTypes[currentTypeName]!.variables.append(varDecl) | ||
} else { | ||
fatalError("Global variables are not supported yet: \(node.debugDescription)") | ||
fatalError("Global variables are not supported yet: \(node.qualifiedNameForDebug)") | ||
} | ||
|
||
return .skipChildren | ||
|
@@ -206,7 +227,7 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
return .skipChildren | ||
} | ||
|
||
self.log.debug("Import initializer: \(node.kind) \(currentType.javaType.description)") | ||
self.log.debug("Import initializer: \(node.kind) '\(node.qualifiedNameForDebug)'") | ||
let params: [ImportedParam] | ||
do { | ||
params = try node.signature.parameterClause.parameters.map { param in | ||
|
@@ -247,37 +268,24 @@ final class Swift2JavaVisitor: SyntaxVisitor { | |
} | ||
} | ||
|
||
extension DeclGroupSyntax where Self: NamedDeclSyntax { | ||
extension DeclSyntaxProtocol where Self: WithModifiersSyntax & WithAttributesSyntax { | ||
func shouldImport(log: Logger) -> Bool { | ||
guard (accessControlModifiers.first { $0.isPublic }) != nil else { | ||
log.trace("Cannot import \(self.name) because: is not public") | ||
guard accessControlModifiers.contains(where: { $0.isPublic }) else { | ||
log.trace("Skip import '\(self.qualifiedNameForDebug)': not public") | ||
return false | ||
} | ||
|
||
return true | ||
} | ||
} | ||
|
||
extension InitializerDeclSyntax { | ||
func shouldImport(log: Logger) -> Bool { | ||
let isFailable = self.optionalMark != nil | ||
|
||
if isFailable { | ||
log.warning("Skip importing failable initializer: \(self)") | ||
guard !attributes.contains(where: { $0.isJava }) else { | ||
log.trace("Skip import '\(self.qualifiedNameForDebug)': is Java") | ||
return false | ||
} | ||
|
||
// Ok, import it | ||
log.warning("Import initializer: \(self)") | ||
return true | ||
} | ||
} | ||
if let node = self.as(InitializerDeclSyntax.self) { | ||
let isFailable = node.optionalMark != nil | ||
|
||
extension FunctionDeclSyntax { | ||
func shouldImport(log: Logger) -> Bool { | ||
guard (accessControlModifiers.first { $0.isPublic }) != nil else { | ||
log.trace("Cannot import \(self.name) because: is not public") | ||
return false | ||
if isFailable { | ||
log.warning("Skip import '\(self.qualifiedNameForDebug)': failable initializer") | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s neat, thanks!