Skip to content

Commit d332703

Browse files
authored
Merge pull request #222 from rintaro/jextract-multifiles
[jextract] Prepare all inputs before analyze()
2 parents 0b1fc44 + 36e74f6 commit d332703

File tree

3 files changed

+46
-31
lines changed

3 files changed

+46
-31
lines changed

Sources/JExtractSwift/Swift2Java.swift

+15-8
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,23 @@ public struct SwiftToJava: ParsableCommand {
7474
}
7575
}
7676

77-
for file in allFiles where canExtract(from: file) {
78-
translator.log.debug("Importing module '\(swiftModule)', file: \(file)")
79-
80-
try translator.analyze(file: file.path)
81-
try translator.writeExportedJavaSources(outputDirectory: outputDirectoryJava)
82-
try translator.writeSwiftThunkSources(outputDirectory: outputDirectorySwift)
83-
84-
log.debug("[swift-java] Imported interface file: \(file.path)")
77+
// Register files to the translator.
78+
for file in allFiles {
79+
guard canExtract(from: file) else {
80+
continue
81+
}
82+
guard let data = fileManager.contents(atPath: file.path) else {
83+
continue
84+
}
85+
guard let text = String(data:data, encoding: .utf8) else {
86+
continue
87+
}
88+
translator.add(filePath: file.path, text: text)
8589
}
8690

91+
try translator.analyze()
92+
try translator.writeSwiftThunkSources(outputDirectory: outputDirectorySwift)
93+
try translator.writeExportedJavaSources(outputDirectory: outputDirectoryJava)
8794
try translator.writeExportedJavaModule(outputDirectory: outputDirectoryJava)
8895
print("[swift-java] Generated Java sources (\(packageName)) in: \(outputDirectoryJava)/")
8996
print("[swift-java] Imported Swift module '\(swiftModule)': " + "done.".green)

Sources/JExtractSwift/Swift2JavaTranslator.swift

+29-21
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,15 @@ public final class Swift2JavaTranslator {
2424

2525
package var log = Logger(label: "translator", logLevel: .info)
2626

27+
// ==== Input
28+
29+
struct Input {
30+
let filePath: String
31+
let syntax: Syntax
32+
}
33+
34+
var inputs: [Input] = []
35+
2736
// ==== Output configuration
2837
let javaPackage: String
2938

@@ -39,7 +48,7 @@ public final class Swift2JavaTranslator {
3948
/// type representation.
4049
package var importedTypes: [String: ImportedNominalType] = [:]
4150

42-
public var swiftStdlibTypes: SwiftStandardLibraryTypes
51+
package var swiftStdlibTypes: SwiftStandardLibraryTypes
4352

4453
let symbolTable: SwiftSymbolTable
4554
let nominalResolution: NominalTypeResolution = NominalTypeResolution()
@@ -78,43 +87,42 @@ extension Swift2JavaTranslator {
7887
/// a checked truncation operation at the Java/Swift board.
7988
var javaPrimitiveForSwiftInt: JavaType { .long }
8089

81-
public func analyze(
90+
package func add(filePath: String, text: String) {
91+
log.trace("Adding: \(filePath)")
92+
let sourceFileSyntax = Parser.parse(source: text)
93+
self.nominalResolution.addSourceFile(sourceFileSyntax)
94+
self.inputs.append(Input(filePath: filePath, syntax: Syntax(sourceFileSyntax)))
95+
}
96+
97+
/// Convenient method for analyzing single file.
98+
package func analyze(
8299
file: String,
83-
text: String? = nil
100+
text: String
84101
) throws {
85-
guard text != nil || FileManager.default.fileExists(atPath: file) else {
86-
throw Swift2JavaTranslatorError(message: "Missing input file: \(file)")
87-
}
88-
89-
log.trace("Analyze: \(file)")
90-
let text = try text ?? String(contentsOfFile: file)
91-
92-
try analyzeSwiftInterface(interfaceFilePath: file, text: text)
93-
94-
log.debug("Done processing: \(file)")
102+
self.add(filePath: file, text: text)
103+
try self.analyze()
95104
}
96105

97-
package func analyzeSwiftInterface(interfaceFilePath: String, text: String) throws {
98-
let sourceFileSyntax = Parser.parse(source: text)
99-
100-
addSourceFile(sourceFileSyntax)
106+
/// Analyze registered inputs.
107+
func analyze() throws {
101108
prepareForTranslation()
102109

103110
let visitor = Swift2JavaVisitor(
104111
moduleName: self.swiftModuleName,
105112
targetJavaPackage: self.javaPackage,
106113
translator: self
107114
)
108-
visitor.walk(sourceFileSyntax)
109-
}
110115

111-
package func addSourceFile(_ sourceFile: SourceFileSyntax) {
112-
nominalResolution.addSourceFile(sourceFile)
116+
for input in self.inputs {
117+
log.trace("Analyzing \(input.filePath)")
118+
visitor.walk(input.syntax)
119+
}
113120
}
114121

115122
package func prepareForTranslation() {
116123
nominalResolution.bindExtensions()
117124

125+
// Prepare symbol table for nominal type names.
118126
for (_, node) in nominalResolution.topLevelNominalTypes {
119127
symbolTable.parsedModule.addNominalTypeDeclaration(node, parent: nil)
120128
}

Tests/JExtractSwiftTests/Asserts/LoweringAssertions.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ func assertLoweredFunction(
2222
_ inputDecl: DeclSyntax,
2323
javaPackage: String = "org.swift.mypackage",
2424
swiftModuleName: String = "MyModule",
25-
sourceFile: SourceFileSyntax? = nil,
25+
sourceFile: String? = nil,
2626
enclosingType: TypeSyntax? = nil,
2727
expectedCDecl: DeclSyntax,
2828
expectedCFunction: String,
@@ -37,7 +37,7 @@ func assertLoweredFunction(
3737
)
3838

3939
if let sourceFile {
40-
translator.addSourceFile(sourceFile)
40+
translator.add(filePath: "Fake.swift", text: sourceFile)
4141
}
4242

4343
translator.prepareForTranslation()

0 commit comments

Comments
 (0)