Skip to content

Commit e7d7a21

Browse files
authored
Merge pull request #3 from DougGregor/imported-func-cleanup
Replace `ImportedFunc.swiftDemangledMangledName` with a Swift full name
2 parents 347c915 + 99e9ba1 commit e7d7a21

File tree

6 files changed

+44
-31
lines changed

6 files changed

+44
-31
lines changed

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@ public struct ImportedTypeName: Hashable {
8686
public var swiftTypeName: String
8787

8888
public var swiftMangledName: String = ""
89-
public var swiftDemangledMangledName: String = ""
9089

9190
public var javaType: JavaType
9291

@@ -124,7 +123,8 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
124123
public var parentName: ImportedTypeName?
125124
public var hasParent: Bool { parentName != nil }
126125

127-
public var identifier: String // FIXME: this is init(cap:name:) complete swift identifier; change that to be base
126+
/// This is a full name such as init(cap:name:).
127+
public var identifier: String
128128

129129
public var baseIdentifier: String {
130130
guard let idx = identifier.firstIndex(of: "(") else {
@@ -133,6 +133,16 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
133133
return String(identifier[..<idx])
134134
}
135135

136+
/// A display name to use to refer to the Swift declaration with its
137+
/// enclosing type.
138+
public var displayName: String {
139+
if let parentName {
140+
return "\(parentName.swiftTypeName).\(identifier)"
141+
}
142+
143+
return identifier
144+
}
145+
136146
public var returnType: ImportedTypeName
137147
public var parameters: [ImportedParam]
138148

@@ -173,7 +183,6 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
173183
}
174184

175185
public var swiftMangledName: String = ""
176-
public var swiftDemangledMangledName: String = ""
177186

178187
public var swiftDeclRaw: String? = nil
179188

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ extension Swift2JavaTranslator {
441441
public func printDowncallMethods(_ printer: inout CodePrinter, _ decl: ImportedFunc) {
442442
printer.printSeparator(decl.identifier)
443443

444-
printer.printTypeDecl("private static class \(decl.identifier)") { printer in
444+
printer.printTypeDecl("private static class \(decl.baseIdentifier)") { printer in
445445
printFunctionDescriptorValue(&printer, decl);
446446
printFindMemorySegmentAddrByMangledName(&printer, decl)
447447
printMethodDowncallHandleForAddrDesc(&printer)
@@ -455,8 +455,8 @@ extension Swift2JavaTranslator {
455455
* \(/*TODO: make a printSnippet func*/decl.swiftDeclRaw ?? "")
456456
* }
457457
*/
458-
public static FunctionDescriptor \(decl.identifier)$descriptor() {
459-
return \(decl.identifier).DESC;
458+
public static FunctionDescriptor \(decl.baseIdentifier)$descriptor() {
459+
return \(decl.baseIdentifier).DESC;
460460
}
461461
"""
462462
)
@@ -469,8 +469,8 @@ extension Swift2JavaTranslator {
469469
* \(/*TODO: make a printSnippet func*/decl.swiftDeclRaw ?? "")
470470
* }
471471
*/
472-
public static MethodHandle \(decl.identifier)$handle() {
473-
return \(decl.identifier).HANDLE;
472+
public static MethodHandle \(decl.baseIdentifier)$handle() {
473+
return \(decl.baseIdentifier).HANDLE;
474474
}
475475
"""
476476
)
@@ -483,8 +483,8 @@ extension Swift2JavaTranslator {
483483
* \(/*TODO: make a printSnippet func*/decl.swiftDeclRaw ?? "")
484484
* }
485485
*/
486-
public static MemorySegment \(decl.identifier)$address() {
487-
return \(decl.identifier).ADDR;
486+
public static MemorySegment \(decl.baseIdentifier)$address() {
487+
return \(decl.baseIdentifier).ADDR;
488488
}
489489
"""
490490
)
@@ -502,9 +502,8 @@ extension Swift2JavaTranslator {
502502
printer.print(
503503
"""
504504
/**
505-
* Demangled representation:
506505
* {@snippet lang = Swift:
507-
* \(decl.swiftDemangledMangledName)
506+
* \(decl.displayName)
508507
* }
509508
*/
510509
public static final MemorySegment ADDR = \(swiftModuleName).findOrThrow("\(decl.swiftMangledName)");
@@ -543,8 +542,8 @@ extension Swift2JavaTranslator {
543542
* \(/*TODO: make a printSnippet func*/decl.swiftDeclRaw ?? "")
544543
* }
545544
*/
546-
public static \(returnTy) \(decl.identifier)(\(renderJavaParamDecls(decl, selfVariant: .wrapper))) {
547-
\(maybeReturnCast) \(decl.identifier)(\(renderForwardParams(decl, selfVariant: .memorySegment)));
545+
public static \(returnTy) \(decl.baseIdentifier)(\(renderJavaParamDecls(decl, selfVariant: .wrapper))) {
546+
\(maybeReturnCast) \(decl.baseIdentifier)(\(renderForwardParams(decl, selfVariant: .memorySegment)));
548547
}
549548
"""
550549
)
@@ -558,8 +557,8 @@ extension Swift2JavaTranslator {
558557
* \(/*TODO: make a printSnippet func*/decl.swiftDeclRaw ?? "")
559558
* }
560559
*/
561-
public static \(returnTy) \(decl.identifier)(\(renderJavaParamDecls(decl, selfVariant: selfVariant))) {
562-
var mh$ = \(decl.identifier).HANDLE;
560+
public static \(returnTy) \(decl.baseIdentifier)(\(renderJavaParamDecls(decl, selfVariant: selfVariant))) {
561+
var mh$ = \(decl.baseIdentifier).HANDLE;
563562
try {
564563
if (TRACE_DOWNCALLS) {
565564
traceDowncall(\(renderForwardParams(decl, selfVariant: .memorySegment)));

Sources/JExtractSwift/Swift2JavaVisitor.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,18 @@ final class Swift2JavaVisitor: SyntaxVisitor {
103103
return .skipChildren
104104
}
105105

106+
let argumentLabels = node.signature.parameterClause.parameters.map { param in
107+
param.firstName.identifier?.name ?? "_"
108+
}
109+
let argumentLabelsStr = String(argumentLabels.flatMap { label in
110+
label + ":"
111+
})
112+
113+
let fullName = "\(node.name.text)(\(argumentLabelsStr))"
114+
106115
var funcDecl = ImportedFunc(
107116
parentName: currentTypeDecl?.name,
108-
identifier: node.name.text,
117+
identifier: fullName,
109118
returnType: javaResultType,
110119
parameters: params
111120
)
@@ -156,7 +165,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
156165

157166
var funcDecl = ImportedFunc(
158167
parentName: currentTypeDecl.name,
159-
identifier: initIdentifier, // FIXME: what is the name of the inits?
168+
identifier: initIdentifier,
160169
returnType: currentTypeDecl.name,
161170
parameters: params
162171
)

Sources/JExtractSwift/SwiftDylib.swift

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ package struct SwiftDylib { // FIXME: remove this entire utility; replace with
4646
if let name = names.first {
4747
log.trace("Selected mangled name for '\(decl.name.javaType.description)': \(name)")
4848
decl.name.swiftMangledName = name.mangledName
49-
decl.name.swiftDemangledMangledName = name.descriptiveName
5049
}
5150

5251
return decl
@@ -60,11 +59,10 @@ package struct SwiftDylib { // FIXME: remove this entire utility; replace with
6059
}
6160

6261
var decl = decl
63-
let names = try await nmSymbolNames(grepDemangled: [decl.identifier])
62+
let names = try await nmSymbolNames(grepDemangled: [decl.baseIdentifier])
6463
if let name = names.first {
6564
log.trace("Selected mangled name for '\(decl.identifier)': \(name)")
6665
decl.swiftMangledName = name.mangledName
67-
decl.swiftDemangledMangledName = name.descriptiveName
6866
}
6967

7068
return decl
@@ -85,7 +83,6 @@ package struct SwiftDylib { // FIXME: remove this entire utility; replace with
8583
if let name = names.first {
8684
log.trace("Selected mangled name for '\(decl.identifier)': \(name)")
8785
decl.swiftMangledName = name.mangledName
88-
decl.swiftDemangledMangledName = name.descriptiveName
8986
}
9087

9188
return decl
@@ -103,7 +100,6 @@ package struct SwiftDylib { // FIXME: remove this entire utility; replace with
103100
if let name = names.first {
104101
log.trace("Selected mangled name: \(name)")
105102
decl.swiftMangledName = name.mangledName
106-
decl.swiftDemangledMangledName = name.descriptiveName
107103
}
108104

109105
return decl

Tests/JExtractSwiftTests/FuncImportTests.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ final class MethodImportTests: XCTestCase {
6060

6161
try await st.analyze(swiftInterfacePath: "/fake/Fake.swiftinterface", text: class_interfaceFile)
6262

63-
let funcDecl = st.importedGlobalFuncs.first { $0.identifier == "helloWorld" }!
63+
let funcDecl = st.importedGlobalFuncs.first { $0.baseIdentifier == "helloWorld" }!
6464

6565
let output = CodePrinter.toString { printer in
6666
st.printFuncDowncallMethod(&printer, decl: funcDecl, selfVariant: nil)
@@ -100,7 +100,7 @@ final class MethodImportTests: XCTestCase {
100100
try await st.analyze(swiftInterfacePath: "/fake/__FakeModule/SwiftFile.swiftinterface", text: class_interfaceFile)
101101

102102
let funcDecl = st.importedGlobalFuncs.first {
103-
$0.identifier == "globalTakeInt"
103+
$0.baseIdentifier == "globalTakeInt"
104104
}!
105105

106106
let output = CodePrinter.toString { printer in
@@ -141,7 +141,7 @@ final class MethodImportTests: XCTestCase {
141141
try await st.analyze(swiftInterfacePath: "/fake/__FakeModule/SwiftFile.swiftinterface", text: class_interfaceFile)
142142

143143
let funcDecl = st.importedGlobalFuncs.first {
144-
$0.identifier == "globalTakeIntLongString"
144+
$0.baseIdentifier == "globalTakeIntLongString"
145145
}!
146146

147147
let output = CodePrinter.toString { printer in
@@ -184,7 +184,7 @@ final class MethodImportTests: XCTestCase {
184184
let funcDecl: ImportedFunc = st.importedTypes.first {
185185
$0.name.javaClassName == "MySwiftClass"
186186
}!.methods.first {
187-
$0.identifier == "helloMemberFunction"
187+
$0.baseIdentifier == "helloMemberFunction"
188188
}!
189189

190190
let output = CodePrinter.toString { printer in
@@ -227,7 +227,7 @@ final class MethodImportTests: XCTestCase {
227227
let funcDecl: ImportedFunc = st.importedTypes.first {
228228
$0.name.javaClassName == "MySwiftClass"
229229
}!.methods.first {
230-
$0.identifier == "helloMemberFunction"
230+
$0.baseIdentifier == "helloMemberFunction"
231231
}!
232232

233233
let output = CodePrinter.toString { printer in
@@ -270,7 +270,7 @@ final class MethodImportTests: XCTestCase {
270270
let funcDecl: ImportedFunc = st.importedTypes.first {
271271
$0.name.javaClassName == "MySwiftClass"
272272
}!.methods.first {
273-
$0.identifier == "helloMemberFunction"
273+
$0.baseIdentifier == "helloMemberFunction"
274274
}!
275275

276276
let output = CodePrinter.toString { printer in
@@ -305,7 +305,7 @@ final class MethodImportTests: XCTestCase {
305305
let funcDecl: ImportedFunc = st.importedTypes.first {
306306
$0.name.javaClassName == "MySwiftClass"
307307
}!.methods.first {
308-
$0.identifier == "makeInt"
308+
$0.baseIdentifier == "makeInt"
309309
}!
310310

311311
let output = CodePrinter.toString { printer in

Tests/JExtractSwiftTests/FunctionDescriptorImportTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ final class FunctionDescriptorTests: XCTestCase {
6363
try await st.analyze(swiftInterfacePath: "/fake/Sample.swiftinterface", text: interfaceFile)
6464

6565
let funcDecl = st.importedGlobalFuncs.first {
66-
$0.identifier == methodIdentifier
66+
$0.baseIdentifier == methodIdentifier
6767
}!
6868

6969
let output = CodePrinter.toString { printer in

0 commit comments

Comments
 (0)