Skip to content

Commit f4343bb

Browse files
committed
Merge branch 'main' into wip-converge-cli
2 parents 65400da + 6267c8f commit f4343bb

14 files changed

+351
-215
lines changed

Makefile

Lines changed: 0 additions & 110 deletions
This file was deleted.

Sources/JExtractSwiftLib/CDeclLowering/Swift2JavaTranslator+FunctionLowering.swift

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extension Swift2JavaTranslator {
2929
enclosingType: try enclosingType.map { try SwiftType($0, symbolTable: symbolTable) },
3030
symbolTable: symbolTable
3131
)
32-
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature, apiKind: .function)
32+
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature)
3333
}
3434

3535
/// Lower the given initializer to a C-compatible entrypoint,
@@ -46,7 +46,7 @@ extension Swift2JavaTranslator {
4646
symbolTable: symbolTable
4747
)
4848

49-
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature, apiKind: .initializer)
49+
return try CdeclLowering(swiftStdlibTypes: swiftStdlibTypes).lowerFunctionSignature(signature)
5050
}
5151

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

@@ -82,8 +82,7 @@ struct CdeclLowering {
8282
///
8383
/// Throws an error if this function cannot be lowered for any reason.
8484
func lowerFunctionSignature(
85-
_ signature: SwiftFunctionSignature,
86-
apiKind: SwiftAPIKind
85+
_ signature: SwiftFunctionSignature
8786
) throws -> LoweredFunctionSignature {
8887
// Lower the self parameter.
8988
let loweredSelf: LoweredParameter? = switch signature.selfParameter {
@@ -111,7 +110,6 @@ struct CdeclLowering {
111110

112111
return LoweredFunctionSignature(
113112
original: signature,
114-
apiKind: apiKind,
115113
selfParameter: loweredSelf,
116114
parameters: loweredParameters,
117115
result: loweredResult
@@ -436,13 +434,6 @@ struct CdeclLowering {
436434
}
437435
}
438436

439-
package enum SwiftAPIKind {
440-
case function
441-
case initializer
442-
case getter
443-
case setter
444-
}
445-
446437
/// Represent a Swift parameter in the cdecl thunk.
447438
struct LoweredParameter: Equatable {
448439
/// Lowered parameters in cdecl thunk.
@@ -487,8 +478,6 @@ extension LoweredResult {
487478
public struct LoweredFunctionSignature: Equatable {
488479
var original: SwiftFunctionSignature
489480

490-
var apiKind: SwiftAPIKind
491-
492481
var selfParameter: LoweredParameter?
493482
var parameters: [LoweredParameter]
494483
var result: LoweredResult
@@ -520,9 +509,10 @@ public struct LoweredFunctionSignature: Equatable {
520509
extension LoweredFunctionSignature {
521510
/// Produce the `@_cdecl` thunk for this lowered function signature that will
522511
/// call into the original function.
523-
public func cdeclThunk(
512+
package func cdeclThunk(
524513
cName: String,
525514
swiftAPIName: String,
515+
as apiKind: SwiftAPIKind,
526516
stdlibTypes: SwiftStandardLibraryTypes
527517
) -> FunctionDeclSyntax {
528518

@@ -563,7 +553,7 @@ extension LoweredFunctionSignature {
563553

564554
// Build callee expression.
565555
let callee: ExprSyntax = if let selfExpr {
566-
if case .initializer = self.apiKind {
556+
if case .initializer = apiKind {
567557
// Don't bother to create explicit ${Self}.init expression.
568558
selfExpr
569559
} else {

Sources/JExtractSwiftLib/Convenience/SwiftSyntax+Extensions.swift

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -177,17 +177,19 @@ extension DeclSyntaxProtocol {
177177
var signatureString: String {
178178
return switch DeclSyntax(self.detached).as(DeclSyntaxEnum.self) {
179179
case .functionDecl(let node):
180-
node.with(\.body, nil).trimmedDescription
180+
node.with(\.body, nil).triviaSanitizedDescription
181181
case .initializerDecl(let node):
182-
node.with(\.body, nil).trimmedDescription
182+
node.with(\.body, nil).triviaSanitizedDescription
183183
case .classDecl(let node):
184-
node.with(\.memberBlock, "").trimmedDescription
184+
node.with(\.memberBlock, "").triviaSanitizedDescription
185185
case .structDecl(let node):
186-
node.with(\.memberBlock, "").trimmedDescription
186+
node.with(\.memberBlock, "").triviaSanitizedDescription
187187
case .protocolDecl(let node):
188-
node.with(\.memberBlock, "").trimmedDescription
188+
node.with(\.memberBlock, "").triviaSanitizedDescription
189189
case .accessorDecl(let node):
190-
node.with(\.body, nil).trimmedDescription
190+
node.with(\.body, nil).triviaSanitizedDescription
191+
case .subscriptDecl(let node):
192+
node.with(\.accessorBlock, nil).triviaSanitizedDescription
191193
case .variableDecl(let node):
192194
node
193195
.with(\.bindings, PatternBindingListSyntax(
@@ -197,9 +199,47 @@ extension DeclSyntaxProtocol {
197199
.with(\.initializer, nil)
198200
}
199201
))
200-
.trimmedDescription
202+
.triviaSanitizedDescription
201203
default:
202204
fatalError("unimplemented \(self.kind)")
203205
}
204206
}
207+
208+
/// Syntax text but without unnecessary trivia.
209+
///
210+
/// Connective trivia are condensed into a single whitespace, but no space
211+
/// after opening or before closing parentheses
212+
var triviaSanitizedDescription: String {
213+
let visitor = TriviaSanitizingDescriptionVisitor(viewMode: .sourceAccurate)
214+
visitor.walk(self.trimmed)
215+
return visitor.result
216+
}
217+
}
218+
219+
class TriviaSanitizingDescriptionVisitor: SyntaxVisitor {
220+
var result: String = ""
221+
222+
var prevTokenKind: TokenKind = .endOfFile
223+
var prevHadTrailingSpace: Bool = false
224+
225+
override func visit(_ node: TokenSyntax) -> SyntaxVisitorContinueKind {
226+
let tokenKind = node.tokenKind
227+
switch (prevTokenKind, tokenKind) {
228+
case
229+
// No whitespace after open parentheses.
230+
(.leftAngle, _), (.leftParen, _), (.leftSquare, _), (.endOfFile, _),
231+
// No whitespace before close parentheses.
232+
(_, .rightAngle), (_, .rightParen), (_, .rightSquare):
233+
break
234+
default:
235+
if prevHadTrailingSpace || !node.leadingTrivia.isEmpty {
236+
result += " "
237+
}
238+
}
239+
result += node.text
240+
prevTokenKind = tokenKind
241+
prevHadTrailingSpace = !node.trailingTrivia.isEmpty
242+
243+
return .skipChildren
244+
}
205245
}

Sources/JExtractSwiftLib/ImportedDecls.swift

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,12 @@ import SwiftSyntax
1717
/// Any imported (Swift) declaration
1818
protocol ImportedDecl: AnyObject {}
1919

20-
public typealias JavaPackage = String
20+
package enum SwiftAPIKind {
21+
case function
22+
case initializer
23+
case getter
24+
case setter
25+
}
2126

2227
/// Describes a Swift nominal type (e.g., a class, struct, enum) that has been
2328
/// imported and is being translated into Java.
@@ -47,32 +52,17 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
4752

4853
public var swiftDecl: any DeclSyntaxProtocol
4954

50-
var translatedSignature: TranslatedFunctionSignature
55+
package var apiKind: SwiftAPIKind
56+
57+
var functionSignature: SwiftFunctionSignature
5158

5259
public var signatureString: String {
53-
// FIXME: Remove comments and normalize trivia.
5460
self.swiftDecl.signatureString
5561
}
5662

57-
var loweredSignature: LoweredFunctionSignature {
58-
translatedSignature.loweredSignature
59-
}
60-
61-
var swiftSignature: SwiftFunctionSignature {
62-
loweredSignature.original
63-
}
64-
65-
package func cFunctionDecl(cName: String) -> CFunction {
66-
// 'try!' because we know 'loweredSignature' can be described with C.
67-
try! loweredSignature.cFunctionDecl(cName: cName)
68-
}
69-
70-
package var kind: SwiftAPIKind {
71-
loweredSignature.apiKind
72-
}
7363

7464
var parentType: SwiftType? {
75-
guard let selfParameter = swiftSignature.selfParameter else {
65+
guard let selfParameter = functionSignature.selfParameter else {
7666
return nil
7767
}
7868
switch selfParameter {
@@ -89,12 +79,12 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
8979
/// this will contain that declaration's imported name.
9080
///
9181
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
92-
public var hasParent: Bool { translatedSignature.selfParameter != nil }
82+
public var hasParent: Bool { functionSignature.selfParameter != nil }
9383

9484
/// A display name to use to refer to the Swift declaration with its
9585
/// enclosing type, if there is one.
9686
public var displayName: String {
97-
let prefix = switch self.kind {
87+
let prefix = switch self.apiKind {
9888
case .getter: "getter:"
9989
case .setter: "setter:"
10090
case .function, .initializer: ""
@@ -113,18 +103,20 @@ public final class ImportedFunc: ImportedDecl, CustomStringConvertible {
113103
module: String,
114104
swiftDecl: any DeclSyntaxProtocol,
115105
name: String,
116-
translatedSignature: TranslatedFunctionSignature
106+
apiKind: SwiftAPIKind,
107+
functionSignature: SwiftFunctionSignature
117108
) {
118109
self.module = module
119110
self.name = name
120111
self.swiftDecl = swiftDecl
121-
self.translatedSignature = translatedSignature
112+
self.apiKind = apiKind
113+
self.functionSignature = functionSignature
122114
}
123115

124116
public var description: String {
125117
"""
126118
ImportedFunc {
127-
kind: \(kind)
119+
apiKind: \(apiKind)
128120
module: \(module)
129121
name: \(name)
130122
signature: \(self.swiftDecl.signatureString)

0 commit comments

Comments
 (0)