Skip to content

Commit 354ea56

Browse files
committed
now need to emit the swiftjava_JExtractPluginSampleLib_MyCoolSwiftClass_init_TODO_PARAMS_
1 parent c89d6b0 commit 354ea56

File tree

12 files changed

+163
-97
lines changed

12 files changed

+163
-97
lines changed

Plugins/JExtractSwiftPlugin/JExtractSwiftPlugin.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ struct JExtractSwiftBuildToolPlugin: BuildToolPlugin {
3333
.appending(path: "generated")
3434
.appending(path: "java")
3535
let outputDirectorySwift = context.pluginWorkDirectoryURL
36+
.appending(path: "src")
37+
.appending(path: "generated")
38+
.appending(path: "Sources")
3639

3740
var arguments: [String] = [
3841
"--swift-module", sourceModule.name,

Samples/JExtractPluginSampleApp/build.gradle

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.swift.swiftkit.gradle.BuildUtils
1616

1717
import java.nio.file.Files
18+
import java.nio.file.Paths
1819

1920
plugins {
2021
id("build-logic.java-application-conventions")
@@ -35,29 +36,47 @@ java {
3536
}
3637

3738

38-
def buildSwift = tasks.register("build-swift", Exec) {
39+
// This is for development, when we edit the Swift swift-java project, the outputs of the generated sources may change.
40+
// Thus, we also need to watch and re-build the top level project.
41+
def buildJExtractPlugin = tasks.register("swiftBuildJExtractPlugin", Exec) {
42+
description = "Rebuild the swift-java root project"
43+
44+
inputs.file(new File(rootDir, "Package.swift"))
45+
inputs.dir(new File(rootDir, "Sources"))
46+
outputs.dir(new File(rootDir, ".build"))
47+
48+
workingDir = rootDir
49+
commandLine "swift"
50+
args "build"
51+
}
52+
53+
def buildSwift = tasks.register("swiftBuildProject", Exec) {
3954
description = "Builds swift sources, including swift-java source generation"
55+
dependsOn buildJExtractPlugin
4056

41-
// monitor root project Swift sources:
42-
inputs.dir(layout.projectDirectory.dir("Sources/"))
43-
println("INPUT = ${layout.projectDirectory.dir("Sources/")}")
57+
// only because we depend on "live developing" the plugin while using this project to test it
58+
inputs.file(new File(rootDir, "Package.swift"))
59+
inputs.dir(new File(rootDir, "Sources"))
4460

45-
// monitor root java-swift sources (including source generator libs which may be changing):
46-
inputs.dir("$rootDir/Sources")
47-
println("INPUT = ${"$rootDir/Sources"}")
61+
// TODO: we can use package describe --type json to figure out which targets depend on JExtractSwiftPlugin and will produce outputs
62+
inputs.file(layout.projectDirectory.file("Package.swift"))
63+
inputs.dir(layout.projectDirectory.dir("Sources"))
4864

49-
// the swift-java produced Java sources are located here:
50-
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/jextractpluginsampleapp/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/java"))
65+
// outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}/JExtractPluginSampleLib/destination/JExtractSwiftPlugin/src/generated/java"))
66+
outputs.dir(layout.buildDirectory.dir("../.build/plugins/outputs/${layout.projectDirectory.asFile.getName().toLowerCase()}"))
5167

68+
File baseSwiftPluginOutputsDir = layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile
69+
if (!baseSwiftPluginOutputsDir.exists()) {
70+
baseSwiftPluginOutputsDir.mkdirs()
71+
}
5272
Files.walk(layout.buildDirectory.dir("../.build/plugins/outputs/").get().asFile.toPath()).each {
53-
println("PATH === ${it}")
5473
if (it.endsWith("JExtractSwiftPlugin/src/generated/java")) {
5574
outputs.dir(it)
56-
println("MAKE IT AN OUTPUT")
75+
println("OUTPUT = ${it}")
5776
}
5877
}
5978

60-
workingDir = rootDir
79+
workingDir = layout.projectDirectory
6180
commandLine "swift"
6281
args "build"
6382
}
@@ -76,6 +95,8 @@ dependencies {
7695

7796
testImplementation(platform("org.junit:junit-bom:5.10.0"))
7897
testImplementation("org.junit.jupiter:junit-jupiter")
98+
99+
// implementation(swiftDynamicLibrary('')
79100
}
80101

81102
tasks.named('test', Test) {

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414

1515
import Foundation
1616
import JavaTypes
17-
import SwiftSyntax
1817
import OrderedCollections
18+
import SwiftSyntax
1919

2020
/// Any imported (Swift) declaration
2121
protocol ImportedDecl {
@@ -54,7 +54,7 @@ public struct ImportedNominalType: ImportedDecl {
5454
/// The Java class name without the package.
5555
public var javaClassName: String {
5656
switch javaType {
57-
case .class(package: _, name: let name): name
57+
case .class(package: _, let name): name
5858
default: javaType.description
5959
}
6060
}
@@ -125,12 +125,16 @@ public enum SelfParameterVariant {
125125
}
126126

127127
public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
128+
129+
/// Swift module name (e.g. the target name where a type or function was declared)
130+
public var module: String
131+
128132
/// If this function/method is member of a class/struct/protocol,
129133
/// this will contain that declaration's imported name.
130134
///
131135
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
132-
public var parentName: TranslatedType?
133-
public var hasParent: Bool { parentName != nil }
136+
public var parent: TranslatedType?
137+
public var hasParent: Bool { parent != nil }
134138

135139
/// This is a full name such as init(cap:name:).
136140
public var identifier: String
@@ -147,8 +151,8 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
147151
/// A display name to use to refer to the Swift declaration with its
148152
/// enclosing type, if there is one.
149153
public var displayName: String {
150-
if let parentName {
151-
return "\(parentName.swiftTypeName).\(identifier)"
154+
if let parent {
155+
return "\(parent.swiftTypeName).\(identifier)"
152156
}
153157

154158
return identifier
@@ -158,7 +162,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
158162
public var parameters: [ImportedParam]
159163

160164
public func effectiveParameters(selfVariant: SelfParameterVariant?) -> [ImportedParam] {
161-
if let parentName {
165+
if let parent {
162166
var params = parameters
163167

164168
// Add `self: Self` for method calls on a member
@@ -171,21 +175,15 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
171175
case .pointer:
172176
let selfParam: FunctionParameterSyntax = "self$: $swift_pointer"
173177
params.append(
174-
ImportedParam(
175-
param: selfParam,
176-
type: parentName
177-
)
178+
ImportedParam(param: selfParam, type: parent)
178179
)
179180

180181
case .memorySegment:
181182
let selfParam: FunctionParameterSyntax = "self$: $java_lang_foreign_MemorySegment"
182-
var parentForSelf = parentName
183+
var parentForSelf = parent
183184
parentForSelf.javaType = .javaForeignMemorySegment
184185
params.append(
185-
ImportedParam(
186-
param: selfParam,
187-
type: parentForSelf
188-
)
186+
ImportedParam(param: selfParam, type: parentForSelf)
189187
)
190188
}
191189

@@ -197,7 +195,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
197195
}
198196
}
199197

200-
public var swiftMangledName: String = ""
198+
public var accessorThunkName: String {
199+
SwiftKitPrinting.Names.functionThunk(module: self.module, function: self)
200+
}
201201

202202
public var swiftDecl: any DeclSyntaxProtocol
203203

@@ -208,14 +208,16 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
208208
public var isInit: Bool = false
209209

210210
public init(
211+
module: String,
211212
decl: any DeclSyntaxProtocol,
212-
parentName: TranslatedType?,
213+
parent: TranslatedType?,
213214
identifier: String,
214215
returnType: TranslatedType,
215216
parameters: [ImportedParam]
216217
) {
217218
self.swiftDecl = decl
218-
self.parentName = parentName
219+
self.module = module
220+
self.parent = parent
219221
self.identifier = identifier
220222
self.returnType = returnType
221223
self.parameters = parameters
@@ -224,7 +226,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
224226
public var description: String {
225227
"""
226228
ImportedFunc {
227-
mangledName: \(swiftMangledName)
229+
accessorThunkName: \(self.accessorThunkName)
228230
identifier: \(identifier)
229231
returnType: \(returnType)
230232
parameters: \(parameters)
@@ -243,6 +245,9 @@ public enum VariableAccessorKind {
243245
}
244246

245247
public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
248+
249+
public var module: String
250+
246251
/// If this function/method is member of a class/struct/protocol,
247252
/// this will contain that declaration's imported name.
248253
///
@@ -254,7 +259,7 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
254259
public var identifier: String
255260

256261
/// Which accessors are we able to expose.
257-
///
262+
///
258263
/// Usually this will be all the accessors the variable declares,
259264
/// however if the getter is async or throwing we may not be able to import it
260265
/// (yet), and therefore would skip it from the supported set.
@@ -289,38 +294,44 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
289294

290295
switch kind {
291296
case .set:
292-
let newValueParam: FunctionParameterSyntax = "_ newValue: \(self.returnType.cCompatibleSwiftType)"
297+
let newValueParam: FunctionParameterSyntax =
298+
"_ newValue: \(self.returnType.cCompatibleSwiftType)"
293299
var funcDecl = ImportedFunc(
300+
module: self.module,
294301
decl: self.syntax!,
295-
parentName: self.parentName,
302+
parent: self.parentName,
296303
identifier: self.identifier,
297304
returnType: TranslatedType.void,
298305
parameters: [.init(param: newValueParam, type: self.returnType)])
299-
funcDecl.swiftMangledName = self.swiftMangledName + "s" // form mangled name of the getter by adding the suffix
306+
// FIXME: funcDecl.swiftMangledName = self.swiftMangledName + "s" // form mangled name of the getter by adding the suffix
300307
return funcDecl
301308

302309
case .get:
303310
var funcDecl = ImportedFunc(
311+
module: self.module,
304312
decl: self.syntax!,
305-
parentName: self.parentName,
313+
parent: self.parentName,
306314
identifier: self.identifier,
307315
returnType: self.returnType,
308316
parameters: [])
309-
funcDecl.swiftMangledName = self.swiftMangledName + "g" // form mangled name of the getter by adding the suffix
317+
// FIXME: funcDecl.swiftMangledName = self.swiftMangledName + "g" // form mangled name of the getter by adding the suffix
310318
return funcDecl
311319
}
312320
}
313321

314-
public func effectiveAccessorParameters(_ kind: VariableAccessorKind, selfVariant: SelfParameterVariant?) -> [ImportedParam] {
322+
public func effectiveAccessorParameters(
323+
_ kind: VariableAccessorKind, selfVariant: SelfParameterVariant?
324+
) -> [ImportedParam] {
315325
var params: [ImportedParam] = []
316326

317327
if kind == .set {
318-
let newValueParam: FunctionParameterSyntax = "_ newValue: \(raw: self.returnType.swiftTypeName)"
328+
let newValueParam: FunctionParameterSyntax =
329+
"_ newValue: \(raw: self.returnType.swiftTypeName)"
319330
params.append(
320331
ImportedParam(
321332
param: newValueParam,
322333
type: self.returnType)
323-
)
334+
)
324335
}
325336

326337
if let parentName {
@@ -361,10 +372,12 @@ public struct ImportedVariable: ImportedDecl, CustomStringConvertible {
361372
public var syntax: VariableDeclSyntax? = nil
362373

363374
public init(
375+
module: String,
364376
parentName: TranslatedType?,
365377
identifier: String,
366378
returnType: TranslatedType
367379
) {
380+
self.module = module
368381
self.parentName = parentName
369382
self.identifier = identifier
370383
self.returnType = returnType

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ extension Swift2JavaTranslator {
159159
// Prepare type metadata, we're going to need these when invoking e.g. initializers so cache them in a static.
160160
// We call into source swift-java source generated accessors which give us the type of the Swift object:
161161
// TODO: seems we no longer need the mangled name per se, so avoiding such constant and downcall
162-
// printer.printParts(
163-
// "public static final String TYPE_MANGLED_NAME = ",
164-
// SwiftKitPrinting.renderCallGetSwiftTypeMangledName(module: self.swiftModuleName, nominal: decl),
165-
// ";"
166-
// )
162+
// printer.printParts(
163+
// "public static final String TYPE_MANGLED_NAME = ",
164+
// SwiftKitPrinting.renderCallGetSwiftTypeMangledName(module: self.swiftModuleName, nominal: decl),
165+
// ";"
166+
// )
167167
printer.printParts(
168168
"""
169169
public static final SwiftAnyType TYPE_METADATA =
@@ -438,15 +438,15 @@ extension Swift2JavaTranslator {
438438
}
439439

440440
public func printClassConstructors(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
441-
guard let parentName = decl.parentName else {
441+
guard let parentName = decl.parent else {
442442
fatalError("init must be inside a parent type! Was: \(decl)")
443443
}
444444
printer.printSeparator(decl.identifier)
445445

446446
let descClassIdentifier = renderDescClassName(decl)
447447
printer.printTypeDecl("private static class \(descClassIdentifier)") { printer in
448448
printFunctionDescriptorValue(&printer, decl)
449-
printFindMemorySegmentAddrByMangledName(&printer, decl)
449+
printAccessorFunctionAddr(&printer, decl)
450450
printMethodDowncallHandleForAddrDesc(&printer)
451451
}
452452

@@ -516,7 +516,7 @@ extension Swift2JavaTranslator {
516516

517517
printer.printTypeDecl("private static class \(decl.baseIdentifier)") { printer in
518518
printFunctionDescriptorValue(&printer, decl)
519-
printFindMemorySegmentAddrByMangledName(&printer, decl)
519+
printAccessorFunctionAddr(&printer, decl)
520520
printMethodDowncallHandleForAddrDesc(&printer)
521521
}
522522

@@ -611,7 +611,7 @@ extension Swift2JavaTranslator {
611611
}
612612

613613
printFunctionDescriptorValue(&printer, accessor, accessorKind: accessorKind)
614-
printFindMemorySegmentAddrByMangledName(&printer, accessor, accessorKind: accessorKind)
614+
printAccessorFunctionAddr(&printer, accessor, accessorKind: accessorKind)
615615
printMethodDowncallHandleForAddrDesc(&printer, accessorKind: accessorKind)
616616
}
617617
}
@@ -647,13 +647,15 @@ extension Swift2JavaTranslator {
647647
}
648648
}
649649

650-
func printFindMemorySegmentAddrByMangledName(
650+
func printAccessorFunctionAddr(
651651
_ printer: inout CodePrinter, _ decl: ImportedFunc,
652652
accessorKind: VariableAccessorKind? = nil
653653
) {
654654
printer.print(
655655
"""
656-
public static final MemorySegment \(accessorKind.renderAddrFieldName) = \(swiftModuleName).findOrThrow("\(decl.swiftMangledName)");
656+
public static final MemorySegment \(accessorKind.renderAddrFieldName) =
657+
// FIXME: remove old impl \(swiftModuleName).findOrThrow("NO MANGLED NAMES");
658+
\(swiftModuleName).findOrThrow("\(SwiftKitPrinting.Names.functionThunk(module: self.swiftModuleName, function: decl))");
657659
"""
658660
)
659661
}

0 commit comments

Comments
 (0)