Skip to content

Commit 08bb002

Browse files
committed
remove swiftjavaerror special handling from swiftextract
1 parent 6fd31f6 commit 08bb002

6 files changed

Lines changed: 66 additions & 60 deletions

File tree

Sources/JExtractSwiftLib/FFM/CDeclLowering/FFMSwift2JavaGenerator+FunctionLowering.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -365,10 +365,6 @@ struct CdeclLowering {
365365
case .foundationData, .essentialsData:
366366
break
367367

368-
case .swiftJavaError:
369-
// SwiftJavaError is a class — treat as arbitrary nominal type below
370-
break
371-
372368
default:
373369
// Unreachable? Should be handled by `CType(cdeclType:)` lowering above.
374370
throw LoweringError.unhandledType(type)

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator+JavaTranslation.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import SwiftJavaJNICore
1818

1919
extension FFMSwift2JavaGenerator {
2020
func translatedDecl(
21-
for decl: ImportedFunc
21+
for decl: ExtractedFunc
2222
) -> TranslatedFunctionDecl? {
2323
if let cached = translatedDecls[decl] {
2424
return cached
@@ -176,7 +176,7 @@ extension FFMSwift2JavaGenerator {
176176
self.javaIdentifiers = javaIdentifiers
177177
}
178178

179-
func translate(_ decl: ImportedFunc) throws -> TranslatedFunctionDecl {
179+
func translate(_ decl: ExtractedFunc) throws -> TranslatedFunctionDecl {
180180
let lowering = CdeclLowering(knownTypes: knownTypes)
181181
let loweredSignature = try lowering.lowerFunctionSignature(decl.functionSignature)
182182

@@ -451,10 +451,6 @@ extension FFMSwift2JavaGenerator {
451451
case .foundationData, .essentialsData:
452452
break
453453

454-
case .swiftJavaError:
455-
// SwiftJavaError is a class — treat as arbitrary nominal type below
456-
break
457-
458454
default:
459455
throw JavaTranslationError.unhandledType(swiftType)
460456
}

Sources/JExtractSwiftLib/FFM/FFMSwift2JavaGenerator.swift

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ package class FFMSwift2JavaGenerator: Swift2JavaGenerator {
3838
var thunkNameRegistry: ThunkNameRegistry = ThunkNameRegistry()
3939

4040
/// Cached Java translation result. 'nil' indicates failed translation.
41-
var translatedDecls: [ImportedFunc: TranslatedFunctionDecl?] = [:]
41+
var translatedDecls: [ExtractedFunc: TranslatedFunctionDecl?] = [:]
4242

4343
/// Duplicate identifier tracking for the current batch of methods being generated.
4444
var currentJavaIdentifiers: JavaIdentifierFactory = JavaIdentifierFactory()
@@ -152,8 +152,8 @@ extension FFMSwift2JavaGenerator {
152152
]
153153

154154
/// Returns the Java class name for a nominal type, applying known-type overrides
155-
func javaClassName(for decl: ImportedNominalType) -> String {
156-
if decl.swiftNominal.knownTypeKind == .swiftJavaError {
155+
func javaClassName(for decl: ExtractedNominalType) -> String {
156+
if decl.swiftNominal.isSwiftJavaErrorType {
157157
return JavaType.swiftJavaErrorException.className!
158158
}
159159
return decl.swiftNominal.name
@@ -171,13 +171,13 @@ extension FFMSwift2JavaGenerator {
171171

172172
/// Every imported public type becomes a public class in its own file in Java.
173173
package func writeExportedJavaSources(printer: inout CodePrinter) throws {
174-
let typesToExport: [(key: String, value: ImportedNominalType)]
174+
let typesToExport: [(key: String, value: ExtractedNominalType)]
175175
if let singleType = config.singleType {
176-
typesToExport = analysis.importedTypes
176+
typesToExport = analysis.extractedTypes
177177
.filter { $0.key == singleType }
178178
.sorted(by: { $0.key < $1.key })
179179
} else {
180-
typesToExport = analysis.importedTypes
180+
typesToExport = analysis.extractedTypes
181181
.sorted(by: { $0.key < $1.key })
182182
}
183183

@@ -227,24 +227,24 @@ extension FFMSwift2JavaGenerator {
227227
printImports(&printer)
228228

229229
self.currentJavaIdentifiers = JavaIdentifierFactory(
230-
self.analysis.importedGlobalFuncs + self.analysis.importedGlobalVariables
230+
self.analysis.extractedGlobalFuncs + self.analysis.extractedGlobalVariables
231231
)
232232

233233
printModuleClass(&printer) { printer in
234234

235-
for decl in analysis.importedGlobalVariables {
235+
for decl in analysis.extractedGlobalVariables {
236236
self.log.trace("Print imported decl: \(decl)")
237237
printFunctionDowncallMethods(&printer, decl)
238238
}
239239

240-
for decl in analysis.importedGlobalFuncs {
240+
for decl in analysis.extractedGlobalFuncs {
241241
self.log.trace("Print imported decl: \(decl)")
242242
printFunctionDowncallMethods(&printer, decl)
243243
}
244244
}
245245
}
246246

247-
func printImportedNominal(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
247+
func printImportedNominal(_ printer: inout CodePrinter, _ decl: ExtractedNominalType) {
248248
printHeader(&printer)
249249
printPackage(&printer)
250250
printImports(&printer) // TODO: we could have some imports be driven from types used in the generated decl
@@ -253,7 +253,7 @@ extension FFMSwift2JavaGenerator {
253253
decl.initializers + decl.variables + decl.methods
254254
)
255255

256-
let isErrorType = decl.swiftNominal.knownTypeKind == .swiftJavaError
256+
let isErrorType = decl.swiftNominal.isSwiftJavaErrorType
257257
self.currentSymbolLookup = isErrorType ? .swiftRuntime : .module
258258

259259
printNominal(&printer, decl) { printer in
@@ -387,10 +387,10 @@ extension FFMSwift2JavaGenerator {
387387

388388
func printNominal(
389389
_ printer: inout CodePrinter,
390-
_ decl: ImportedNominalType,
390+
_ decl: ExtractedNominalType,
391391
body: (inout CodePrinter) -> Void,
392392
) {
393-
let isErrorType = decl.swiftNominal.knownTypeKind == .swiftJavaError
393+
let isErrorType = decl.swiftNominal.isSwiftJavaErrorType
394394

395395
let baseClass: String
396396
let parentProtocol: String
@@ -425,8 +425,8 @@ extension FFMSwift2JavaGenerator {
425425

426426
/// Returns a closure that prints the constructor and related extras for special nominal types
427427
/// (e.g. error types), or `nil` for normal types that use the default layout + constructor
428-
func getSpecialNominalConstructorPrinting(_ decl: ImportedNominalType) -> ((inout CodePrinter) -> Void)? {
429-
if decl.swiftNominal.knownTypeKind == .swiftJavaError {
428+
func getSpecialNominalConstructorPrinting(_ decl: ExtractedNominalType) -> ((inout CodePrinter) -> Void)? {
429+
if decl.swiftNominal.isSwiftJavaErrorType {
430430
return { printer in
431431
// Error constructor: wrap the opaque pointer so it becomes a pointer-to-reference
432432
// (matching the convention used by normal class instance thunks)
@@ -449,8 +449,8 @@ extension FFMSwift2JavaGenerator {
449449

450450
/// Returns a closure that prints post-members extras for special nominal types
451451
/// (e.g. `fetchDescription` for error types), or `nil` for normal types that use `toString()`
452-
func getSpecialNominalPostMembersPrinting(_ decl: ImportedNominalType) -> ((inout CodePrinter) -> Void)? {
453-
if decl.swiftNominal.knownTypeKind == .swiftJavaError {
452+
func getSpecialNominalPostMembersPrinting(_ decl: ExtractedNominalType) -> ((inout CodePrinter) -> Void)? {
453+
if decl.swiftNominal.isSwiftJavaErrorType {
454454
return { printer in
455455
// Error types inherit toString() from Exception; print fetchDescription helper instead
456456
self.printSwiftJavaErrorFetchDescriptionMethod(&printer, decl)
@@ -569,7 +569,7 @@ extension FFMSwift2JavaGenerator {
569569
)
570570
}
571571

572-
private func printClassMemoryLayout(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
572+
private func printClassMemoryLayout(_ printer: inout CodePrinter, _ decl: ExtractedNominalType) {
573573
printer.print(
574574
"""
575575
public static final GroupLayout $LAYOUT = (GroupLayout) SwiftValueWitnessTable.layoutOfSwiftType(TYPE_METADATA.$memorySegment());
@@ -582,7 +582,7 @@ extension FFMSwift2JavaGenerator {
582582

583583
func printToStringMethod(
584584
_ printer: inout CodePrinter,
585-
_ decl: ImportedNominalType,
585+
_ decl: ExtractedNominalType,
586586
) {
587587
printer.print(
588588
"""
@@ -599,7 +599,7 @@ extension FFMSwift2JavaGenerator {
599599
}
600600

601601
/// Print special helper methods for known types like Foundation.Data
602-
func printSpecificTypeHelpers(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
602+
func printSpecificTypeHelpers(_ printer: inout CodePrinter, _ decl: ExtractedNominalType) {
603603
guard let knownType = decl.swiftNominal.knownTypeKind else {
604604
return
605605
}
@@ -615,7 +615,7 @@ extension FFMSwift2JavaGenerator {
615615
/// Print the `fetchDescription` static helper for SwiftJavaError.
616616
/// This calls the `errorDescription()` downcall to get the error message
617617
/// for the super constructor
618-
func printSwiftJavaErrorFetchDescriptionMethod(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
618+
func printSwiftJavaErrorFetchDescriptionMethod(_ printer: inout CodePrinter, _ decl: ExtractedNominalType) {
619619
// Find the errorDescription method's thunk name
620620
let errorDescMethod = decl.methods.first { $0.name == "errorDescription" }
621621
guard let errorDescMethod, let _ = translatedDecl(for: errorDescMethod) else {

Sources/JExtractSwiftLib/JNI/JNIJavaTypeTranslator.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ enum JNIJavaTypeTranslator {
5353
.dictionary,
5454
.set,
5555
.foundationDate, .essentialsDate,
56-
.foundationUUID, .essentialsUUID,
57-
.swiftJavaError:
56+
.foundationUUID, .essentialsUUID:
5857
return nil
5958
}
6059
}
@@ -80,8 +79,7 @@ enum JNIJavaTypeTranslator {
8079
.dictionary,
8180
.set,
8281
.foundationDate, .essentialsDate,
83-
.foundationUUID, .essentialsUUID,
84-
.swiftJavaError:
82+
.foundationUUID, .essentialsUUID:
8583
nil
8684
}
8785
}
@@ -107,8 +105,7 @@ enum JNIJavaTypeTranslator {
107105
.dictionary,
108106
.set,
109107
.foundationDate, .essentialsDate,
110-
.foundationUUID, .essentialsUUID,
111-
.swiftJavaError:
108+
.foundationUUID, .essentialsUUID:
112109
nil
113110
}
114111
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2024-2026 Apple Inc. and the Swift.org project authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See CONTRIBUTORS.txt for the list of Swift.org project authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import SwiftExtract
16+
17+
extension SwiftNominalTypeDeclaration {
18+
/// True if this is swift-java's runtime `SwiftJavaError` type, which jextract
19+
/// maps to a thrown Java exception rather than an ordinary wrapped nominal
20+
package var isSwiftJavaErrorType: Bool {
21+
moduleName == "SwiftRuntimeFunctions" && name == "SwiftJavaError"
22+
}
23+
}
24+
25+
extension SwiftKnownTypeDeclKind {
26+
/// Indicates whether this known type is translated by `wrap-java`
27+
/// into the same type as `jextract`.
28+
///
29+
/// This means we do not have to perform any mapping when passing
30+
/// this type between jextract and wrap-java
31+
package var isDirectlyTranslatedToWrapJava: Bool {
32+
switch self {
33+
case .bool, .int, .uint, .int8, .uint8, .int16, .uint16, .int32, .uint32, .int64, .uint64, .float, .double, .string,
34+
.void:
35+
return true
36+
default:
37+
return false
38+
}
39+
}
40+
}

Sources/SwiftExtract/SwiftTypes/SwiftKnownTypeDecls.swift

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,6 @@ public enum SwiftKnownType: Equatable {
5353
case foundationUUID
5454
case essentialsUUID
5555

56-
// SwiftRuntimeFunctions
57-
case swiftJavaError
58-
5956
public init?(kind: SwiftKnownTypeDeclKind, genericArguments: [SwiftType]?) {
6057
switch kind {
6158
case .bool: self = .bool
@@ -109,7 +106,6 @@ public enum SwiftKnownType: Equatable {
109106
case .essentialsDate: self = .essentialsDate
110107
case .foundationUUID: self = .foundationUUID
111108
case .essentialsUUID: self = .essentialsUUID
112-
case .swiftJavaError: self = .swiftJavaError
113109
}
114110
}
115111

@@ -150,7 +146,6 @@ public enum SwiftKnownType: Equatable {
150146
case .essentialsDate: .essentialsDate
151147
case .foundationUUID: .foundationUUID
152148
case .essentialsUUID: .essentialsUUID
153-
case .swiftJavaError: .swiftJavaError
154149
}
155150
}
156151
}
@@ -195,9 +190,6 @@ public enum SwiftKnownTypeDeclKind: String, Hashable {
195190
case foundationUUID = "Foundation.UUID"
196191
case essentialsUUID = "FoundationEssentials.UUID"
197192

198-
// SwiftRuntimeFunctions
199-
case swiftJavaError = "SwiftRuntimeFunctions.SwiftJavaError"
200-
201193
public var moduleAndName: (module: String, name: String) {
202194
let qualified = self.rawValue
203195
let period = qualified.firstIndex(of: ".")!
@@ -215,19 +207,4 @@ public enum SwiftKnownTypeDeclKind: String, Hashable {
215207
return false
216208
}
217209
}
218-
219-
/// Indicates whether this known type is translated by `wrap-java`
220-
/// into the same type as `jextract`.
221-
///
222-
/// This means we do not have to perform any mapping when passing
223-
/// this type between jextract and wrap-java
224-
public var isDirectlyTranslatedToWrapJava: Bool {
225-
switch self {
226-
case .bool, .int, .uint, .int8, .uint8, .int16, .uint16, .int32, .uint32, .int64, .uint64, .float, .double, .string,
227-
.void:
228-
return true
229-
default:
230-
return false
231-
}
232-
}
233210
}

0 commit comments

Comments
 (0)