Skip to content

Commit

Permalink
Add support for lowering metatype parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DougGregor committed Jan 30, 2025
1 parent 464475e commit 3d72f9f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,29 @@ extension Swift2JavaTranslator {
parameterName: String
) throws -> LoweredParameters {
switch type {
case .function, .metatype, .optional:
case .function, .optional:
throw LoweringError.unhandledType(type)

case .metatype(let instanceType):
return LoweredParameters(
cdeclToOriginal: .unsafeCastPointer(
.passDirectly(parameterName),
swiftType: instanceType
),
cdeclParameters: [
SwiftParameter(
convention: .byValue,
parameterName: parameterName,
type: .nominal(
SwiftNominalType(
nominalTypeDecl: swiftStdlibTypes.unsafeRawPointerDecl
)
)
)
],
javaFFMParameters: [.SwiftPointer]
)

case .nominal(let nominal):
// Types from the Swift standard library that we know about.
if nominal.nominalTypeDecl.moduleName == "Swift",
Expand Down
7 changes: 4 additions & 3 deletions Tests/JExtractSwiftTests/FunctionLoweringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,14 +117,15 @@ final class FunctionLoweringTests {
)
}

@Test("Lowering metatypes", .disabled("Metatypes are not yet lowered"))
@Test("Lowering metatypes")
func lowerMetatype() throws {
try assertLoweredFunction("""
func f(t: Int.Type) { }
""",
expectedCDecl: """
func f(t: UnsafeRawPointer) -> () {
// implementation
@_cdecl("c_f")
func c_f(_ t: UnsafeRawPointer) {
f(t: unsafeBitCast(t, to: Int.self))
}
"""
)
Expand Down

0 comments on commit 3d72f9f

Please sign in to comment.