Skip to content

Commit 31b9712

Browse files
committed
import enum cases
1 parent a3791cf commit 31b9712

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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+
public enum Vehicle {
16+
case car
17+
case bicycle
18+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"javaPackage": "com.example.swift",
33
"mode": "jni",
4+
"logLevel": ["debug"]
45
}

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ package class ImportedNominalType: ImportedDecl {
3232
package var initializers: [ImportedFunc] = []
3333
package var methods: [ImportedFunc] = []
3434
package var variables: [ImportedFunc] = []
35+
package var cases: [ImportedEnumCase] = []
3536

3637
init(swiftNominal: SwiftNominalTypeDeclaration) {
3738
self.swiftNominal = swiftNominal
@@ -46,6 +47,28 @@ package class ImportedNominalType: ImportedDecl {
4647
}
4748
}
4849

50+
public final class ImportedEnumCase: ImportedDecl, CustomStringConvertible {
51+
/// The case name
52+
public var name: String
53+
54+
/// The enum parameters
55+
var parameters: [SwiftEnumCaseParameter]
56+
57+
init(name: String, parameters: [SwiftEnumCaseParameter]) {
58+
self.name = name
59+
self.parameters = parameters
60+
}
61+
62+
public var description: String {
63+
"""
64+
ImportedEnumCase {
65+
name: \(name),
66+
parameters: \(parameters)
67+
}
68+
"""
69+
}
70+
}
71+
4972
public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
5073
/// Swift module name (e.g. the target name where a type or function was declared)
5174
public var module: String

Sources/JExtractSwiftLib/JNI/JNISwift2JavaGenerator+JavaBindingsPrinting.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ extension JNISwift2JavaGenerator {
125125

126126
printer.println()
127127

128+
if decl.swiftNominal.kind == .enum {
129+
printEnumHelpers(&printer, decl)
130+
printer.println()
131+
}
132+
128133
for initializer in decl.initializers {
129134
printFunctionDowncallMethods(&printer, initializer)
130135
printer.println()
@@ -187,6 +192,16 @@ extension JNISwift2JavaGenerator {
187192
}
188193
}
189194

195+
private func printEnumHelpers(_ printer: inout CodePrinter, _ decl: ImportedNominalType) {
196+
printer.printBraceBlock("public enum Discriminator") { printer in
197+
printer.print(
198+
decl.cases.map { $0.name.uppercased() }.joined(separator: ",\n")
199+
)
200+
}
201+
202+
printer.println()
203+
}
204+
190205
private func printFunctionDowncallMethods(
191206
_ printer: inout CodePrinter,
192207
_ decl: ImportedFunc

Sources/JExtractSwiftLib/Swift2JavaVisitor.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ final class Swift2JavaVisitor {
6565
case .subscriptDecl:
6666
// TODO: Implement
6767
break
68+
case .enumCaseDecl(let node):
69+
self.visit(enumCaseDecl: node, in: parent)
6870

6971
default:
7072
break
@@ -131,6 +133,27 @@ final class Swift2JavaVisitor {
131133
}
132134
}
133135

136+
func visit(enumCaseDecl node: EnumCaseDeclSyntax, in typeContext: ImportedNominalType?) {
137+
do {
138+
for caseElement in node.elements {
139+
self.log.debug("Import case \(caseElement.name) of enum \(node.qualifiedNameForDebug)")
140+
141+
let parameters = try caseElement.parameterClause?.parameters.map {
142+
try SwiftEnumCaseParameter($0, lookupContext: translator.lookupContext)
143+
}
144+
145+
let imported = ImportedEnumCase(
146+
name: caseElement.name.text,
147+
parameters: parameters ?? []
148+
)
149+
150+
typeContext?.cases.append(imported)
151+
}
152+
} catch {
153+
self.log.debug("Failed to import: \(node.qualifiedNameForDebug); \(error)")
154+
}
155+
}
156+
134157
func visit(variableDecl node: VariableDeclSyntax, in typeContext: ImportedNominalType?) {
135158
guard node.shouldExtract(config: config, log: log) else {
136159
return
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2025 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 SwiftSyntax
16+
17+
struct SwiftEnumCaseParameter: Equatable {
18+
var name: String?
19+
var type: SwiftType
20+
}
21+
22+
extension SwiftEnumCaseParameter {
23+
init(
24+
_ node: EnumCaseParameterSyntax,
25+
lookupContext: SwiftTypeLookupContext
26+
) throws {
27+
28+
self.init(
29+
name: node.firstName?.text,
30+
type: try SwiftType(node.type, lookupContext: lookupContext)
31+
)
32+
}
33+
}

0 commit comments

Comments
 (0)