Skip to content

[JExtract] Move 'apiKind' property to 'ImportedFunc' #251

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 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ extension Swift2JavaTranslator {
enclosingType: try enclosingType.map { try SwiftType($0, symbolTable: symbolTable) },
symbolTable: symbolTable
)
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature, apiKind: .function)
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature)
}

/// Lower the given initializer to a C-compatible entrypoint,
Expand All @@ -46,7 +46,7 @@ extension Swift2JavaTranslator {
symbolTable: symbolTable
)

return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature, apiKind: .initializer)
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature)
}

/// Lower the given variable decl to a C-compatible entrypoint,
Expand All @@ -69,7 +69,7 @@ extension Swift2JavaTranslator {
enclosingType: try enclosingType.map { try SwiftType($0, symbolTable: symbolTable) },
symbolTable: symbolTable
)
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature, apiKind: isSet ? .setter : .getter)
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature)
}
}

Expand All @@ -82,8 +82,7 @@ struct CdeclLowering {
///
/// Throws an error if this function cannot be lowered for any reason.
func lowerFunctionSignature(
_ signature: SwiftFunctionSignature,
apiKind: SwiftAPIKind
_ signature: SwiftFunctionSignature
) throws -> LoweredFunctionSignature {
// Lower the self parameter.
let loweredSelf: LoweredParameter? = switch signature.selfParameter {
Expand Down Expand Up @@ -111,7 +110,6 @@ struct CdeclLowering {

return LoweredFunctionSignature(
original: signature,
apiKind: apiKind,
selfParameter: loweredSelf,
parameters: loweredParameters,
result: loweredResult
Expand Down Expand Up @@ -436,13 +434,6 @@ struct CdeclLowering {
}
}

package enum SwiftAPIKind {
case function
case initializer
case getter
case setter
}

/// Represent a Swift parameter in the cdecl thunk.
struct LoweredParameter: Equatable {
/// Lowered parameters in cdecl thunk.
Expand Down Expand Up @@ -487,8 +478,6 @@ extension LoweredResult {
public struct LoweredFunctionSignature: Equatable {
var original: SwiftFunctionSignature

var apiKind: SwiftAPIKind

var selfParameter: LoweredParameter?
var parameters: [LoweredParameter]
var result: LoweredResult
Expand Down Expand Up @@ -520,9 +509,10 @@ public struct LoweredFunctionSignature: Equatable {
extension LoweredFunctionSignature {
/// Produce the `@_cdecl` thunk for this lowered function signature that will
/// call into the original function.
public func cdeclThunk(
package func cdeclThunk(
cName: String,
swiftAPIName: String,
as apiKind: SwiftAPIKind,
stdlibTypes: SwiftStandardLibraryTypes
) -> FunctionDeclSyntax {

Expand Down Expand Up @@ -563,7 +553,7 @@ extension LoweredFunctionSignature {

// Build callee expression.
let callee: ExprSyntax = if let selfExpr {
if case .initializer = self.apiKind {
if case .initializer = apiKind {
// Don't bother to create explicit ${Self}.init expression.
selfExpr
} else {
Expand Down
19 changes: 12 additions & 7 deletions Sources/JExtractSwift/ImportedDecls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@ import SwiftSyntax
/// Any imported (Swift) declaration
protocol ImportedDecl: AnyObject {}

public typealias JavaPackage = String
package enum SwiftAPIKind {
case function
case initializer
case getter
case setter
}

/// Describes a Swift nominal type (e.g., a class, struct, enum) that has been
/// imported and is being translated into Java.
Expand Down Expand Up @@ -49,6 +54,8 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {

var translatedSignature: TranslatedFunctionSignature

package var apiKind: SwiftAPIKind

public var signatureString: String {
// FIXME: Remove comments and normalize trivia.
self.swiftDecl.signatureString
Expand All @@ -67,10 +74,6 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
try! loweredSignature.cFunctionDecl(cName: cName)
}

package var kind: SwiftAPIKind {
loweredSignature.apiKind
}

var parentType: SwiftType? {
guard let selfParameter = swiftSignature.selfParameter else {
return nil
Expand All @@ -94,7 +97,7 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
/// A display name to use to refer to the Swift declaration with its
/// enclosing type, if there is one.
public var displayName: String {
let prefix = switch self.kind {
let prefix = switch self.apiKind {
case .getter: "getter:"
case .setter: "setter:"
case .function, .initializer: ""
Expand All @@ -113,18 +116,20 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
module: String,
swiftDecl: any DeclSyntaxProtocol,
name: String,
apiKind: SwiftAPIKind,
translatedSignature: TranslatedFunctionSignature
) {
self.module = module
self.name = name
self.swiftDecl = swiftDecl
self.apiKind = apiKind
self.translatedSignature = translatedSignature
}

public var description: String {
"""
ImportedFunc {
kind: \(kind)
apiKind: \(apiKind)
module: \(module)
name: \(name)
signature: \(self.swiftDecl.signatureString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ extension Swift2JavaTranslator {
public func printJavaBindingWrapperMethod(
_ printer: inout CodePrinter,
_ decl: ImportedFunc) {
let methodName: String = switch decl.kind {
let methodName: String = switch decl.apiKind {
case .getter: "get\(decl.name.toCamelCase)"
case .setter: "set\(decl.name.toCamelCase)"
case .function, .initializer: decl.name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@ import JavaTypes

extension Swift2JavaTranslator {
func translate(
swiftSignature: SwiftFunctionSignature,
as apiKind: SwiftAPIKind
swiftSignature: SwiftFunctionSignature
) throws -> TranslatedFunctionSignature {
let lowering = CdeclLowering(swiftStdlibTypes: self.swiftStdlibTypes)
let loweredSignature = try lowering.lowerFunctionSignature(swiftSignature, apiKind: apiKind)
let loweredSignature = try lowering.lowerFunctionSignature(swiftSignature)

let translation = JavaTranslation(swiftStdlibTypes: self.swiftStdlibTypes)
let translated = try translation.translate(loweredFunctionSignature: loweredSignature)
Expand Down
9 changes: 6 additions & 3 deletions Sources/JExtractSwift/Swift2JavaVisitor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
translatedSignature = try translator.translate(swiftSignature: swiftSignature, as: .function)
translatedSignature = try translator.translate(swiftSignature: swiftSignature)
} catch {
self.log.debug("Failed to translate: '\(node.qualifiedNameForDebug)'; \(error)")
return .skipChildren
Expand All @@ -138,6 +138,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
module: translator.swiftModuleName,
swiftDecl: node,
name: node.name.text,
apiKind: .function,
translatedSignature: translatedSignature
)

Expand Down Expand Up @@ -173,7 +174,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
translatedSignature = try translator.translate(swiftSignature: swiftSignature, as: kind)
translatedSignature = try translator.translate(swiftSignature: swiftSignature)
} catch {
self.log.debug("Failed to translate: \(node.qualifiedNameForDebug); \(error)")
throw error
Expand All @@ -183,6 +184,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
module: translator.swiftModuleName,
swiftDecl: node,
name: varName,
apiKind: kind,
translatedSignature: translatedSignature
)

Expand Down Expand Up @@ -227,7 +229,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
enclosingType: self.currentSwiftType,
symbolTable: self.translator.symbolTable
)
translatedSignature = try translator.translate(swiftSignature: swiftSignature, as: .initializer)
translatedSignature = try translator.translate(swiftSignature: swiftSignature)
} catch {
self.log.debug("Failed to translate: \(node.qualifiedNameForDebug); \(error)")
return .skipChildren
Expand All @@ -236,6 +238,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
module: translator.swiftModuleName,
swiftDecl: node,
name: "init",
apiKind: .initializer,
translatedSignature: translatedSignature
)

Expand Down
1 change: 1 addition & 0 deletions Sources/JExtractSwift/SwiftThunkTranslator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ struct SwiftThunkTranslator {
let thunkFunc = decl.loweredSignature.cdeclThunk(
cName: thunkName,
swiftAPIName: decl.name,
as: decl.apiKind,
stdlibTypes: st.swiftStdlibTypes
)
return [DeclSyntax(thunkFunc)]
Expand Down
2 changes: 1 addition & 1 deletion Sources/JExtractSwift/ThunkNameRegistry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ package struct ThunkNameRegistry {
}

let suffix: String
switch decl.kind {
switch decl.apiKind {
case .getter:
suffix = "$get"
case .setter:
Expand Down
5 changes: 5 additions & 0 deletions Tests/JExtractSwiftTests/Asserts/LoweringAssertions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,30 @@ func assertLoweredFunction(
translator.prepareForTranslation()

let swiftFunctionName: String
let apiKind: SwiftAPIKind
let loweredFunction: LoweredFunctionSignature
if let inputFunction = inputDecl.as(FunctionDeclSyntax.self) {
loweredFunction = try translator.lowerFunctionSignature(
inputFunction,
enclosingType: enclosingType
)
swiftFunctionName = inputFunction.name.text
apiKind = .function
} else if let inputInitializer = inputDecl.as(InitializerDeclSyntax.self) {
loweredFunction = try translator.lowerFunctionSignature(
inputInitializer,
enclosingType: enclosingType
)
swiftFunctionName = "init"
apiKind = .initializer
} else {
fatalError("Unhandling declaration kind for lowering")
}

let loweredCDecl = loweredFunction.cdeclThunk(
cName: "c_\(swiftFunctionName)",
swiftAPIName: swiftFunctionName,
as: apiKind,
stdlibTypes: translator.swiftStdlibTypes
)

Expand Down Expand Up @@ -124,6 +128,7 @@ func assertLoweredVariableAccessor(
let loweredCDecl = loweredFunction?.cdeclThunk(
cName: "c_\(swiftVariableName)",
swiftAPIName: swiftVariableName,
as: isSet ? .setter : .getter,
stdlibTypes: translator.swiftStdlibTypes
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ extension FunctionDescriptorTests {
let accessorDecl: ImportedFunc? =
st.importedTypes.values.compactMap {
$0.variables.first {
$0.name == identifier && $0.kind == accessorKind
$0.name == identifier && $0.apiKind == accessorKind
}
}.first
guard let accessorDecl else {
Expand Down