Skip to content

Commit f87dd0e

Browse files
committed
Finish the migration from ImportedTypeName over to TranslatedType
TranslatedType captures more information about how the type is represented in Swift, Java, and the intermediate stages in between.
1 parent 1245f75 commit f87dd0e

File tree

5 files changed

+38
-139
lines changed

5 files changed

+38
-139
lines changed

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,12 @@ import Foundation
1515
import JavaTypes
1616
import SwiftSyntax
1717

18-
protocol ImportedDecl: Hashable {
18+
protocol ImportedDecl {
1919

2020
}
2121

2222
public typealias JavaPackage = String
2323

24-
public struct ImportedProtocol: ImportedDecl {
25-
public var identifier: String
26-
}
27-
2824
/// Describes a Swift nominal type (e.g., a class, struct, enum) that has been
2925
/// imported and is being translated into Java.
3026
public struct ImportedNominalType: ImportedDecl {
@@ -43,9 +39,12 @@ public struct ImportedNominalType: ImportedDecl {
4339
self.kind = kind
4440
}
4541

46-
var importedTypeName: ImportedTypeName {
47-
ImportedTypeName(
48-
swiftTypeName: swiftTypeName,
42+
var translatedType: TranslatedType {
43+
TranslatedType(
44+
cCompatibleConvention: .direct,
45+
originalSwiftType: "\(raw: swiftTypeName)",
46+
cCompatibleSwiftType: "\(raw: swiftTypeName)",
47+
cCompatibleJavaMemoryLayout: .heapObject,
4948
javaType: javaType
5049
)
5150
}
@@ -58,7 +57,7 @@ public enum NominalTypeKind {
5857
case `struct`
5958
}
6059

61-
public struct ImportedParam: Hashable {
60+
public struct ImportedParam {
6261
let param: FunctionParameterSyntax
6362

6463
var firstName: String? {
@@ -89,7 +88,7 @@ public struct ImportedParam: Hashable {
8988
}
9089

9190
// The mapped-to Java type of the above Java type, collections and optionals may be replaced with Java ones etc.
92-
var type: ImportedTypeName
91+
var type: TranslatedType
9392
}
9493

9594
extension ImportedParam {
@@ -102,30 +101,6 @@ extension ImportedParam {
102101
}
103102
}
104103

105-
public struct ImportedTypeName: Hashable {
106-
public var swiftTypeName: String
107-
108-
public var swiftMangledName: String = ""
109-
110-
public var javaType: JavaType
111-
112-
public var isVoid: Bool { javaType == .void }
113-
114-
public var fullyQualifiedName: String { javaType.description }
115-
116-
/// Retrieve the Java class name that this type describes, or nil if it
117-
/// doesn't represent a class at all.
118-
public var javaClassName: String? {
119-
javaType.className
120-
}
121-
122-
public init(swiftTypeName: String, javaType: JavaType, swiftMangledName: String? = nil) {
123-
self.swiftTypeName = swiftTypeName
124-
self.javaType = javaType
125-
self.swiftMangledName = swiftMangledName ?? ""
126-
}
127-
}
128-
129104
// TODO: this is used in different contexts and needs a cleanup
130105
public enum SelfParameterVariant {
131106
/// Make a method that accepts the raw memory pointer as a MemorySegment
@@ -141,7 +116,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
141116
/// this will contain that declaration's imported name.
142117
///
143118
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
144-
public var parentName: ImportedTypeName?
119+
var parentName: TranslatedType?
145120
public var hasParent: Bool { parentName != nil }
146121

147122
/// This is a full name such as init(cap:name:).
@@ -166,7 +141,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
166141
return identifier
167142
}
168143

169-
public var returnType: ImportedTypeName
144+
public var returnType: TranslatedType
170145
public var parameters: [ImportedParam]
171146

172147
public func effectiveParameters(selfVariant: SelfParameterVariant?) -> [ImportedParam] {
@@ -185,28 +160,18 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
185160
params.append(
186161
ImportedParam(
187162
param: selfParam,
188-
type: TranslatedType(
189-
cCompatibleConvention: .indirect,
190-
originalSwiftType: "\(raw: parentName.swiftTypeName)",
191-
cCompatibleSwiftType: "UnsafeRawPointer",
192-
cCompatibleJavaMemoryLayout: .heapObject,
193-
javaType: .javaForeignMemorySegment
194-
).importedTypeName
163+
type: parentName
195164
)
196165
)
197166

198167
case .memorySegment:
199168
let selfParam: FunctionParameterSyntax = "self$: $java_lang_foreign_MemorySegment"
169+
var parentForSelf = parentName
170+
parentForSelf.javaType = .javaForeignMemorySegment
200171
params.append(
201172
ImportedParam(
202173
param: selfParam,
203-
type: TranslatedType(
204-
cCompatibleConvention: .indirect,
205-
originalSwiftType: "\(raw: parentName.swiftTypeName)",
206-
cCompatibleSwiftType: "UnsafeRawPointer",
207-
cCompatibleJavaMemoryLayout: .heapObject,
208-
javaType: .javaForeignMemorySegment
209-
).importedTypeName
174+
type: parentForSelf
210175
)
211176
)
212177

@@ -230,9 +195,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
230195
public var isInit: Bool = false
231196

232197
public init(
233-
parentName: ImportedTypeName?,
198+
parentName: TranslatedType?,
234199
identifier: String,
235-
returnType: ImportedTypeName,
200+
returnType: TranslatedType,
236201
parameters: [ImportedParam]
237202
) {
238203
self.parentName = parentName

Sources/JExtractSwift/Swift2JavaTranslator+MemoryLayouts.swift

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import SwiftBasicFormat
1616
import SwiftParser
1717
import SwiftSyntax
1818

19-
let SWIFT_POINTER = "SWIFT_POINTER"
20-
2119
extension Swift2JavaTranslator {
2220
public func javaMemoryLayoutDescriptors(
2321
forParametersOf decl: ImportedFunc,
@@ -31,61 +29,9 @@ extension Swift2JavaTranslator {
3129
// decl.isInit ? nil : .wrapper
3230

3331
for param in decl.effectiveParameters(selfVariant: selfVariant) {
34-
if let paramLayout = javaMemoryLayoutDescriptor(param.type) {
35-
layouts.append(paramLayout)
36-
}
32+
layouts.append(param.type.foreignValueLayout)
3733
}
3834

3935
return layouts
4036
}
41-
42-
// This may reach for another types $layout I think
43-
public func javaMemoryLayoutDescriptor(_ ty: ImportedTypeName) -> ForeignValueLayout? {
44-
switch ty.swiftTypeName {
45-
case "Bool":
46-
return .SwiftBool
47-
case "Int":
48-
return .SwiftInt
49-
case "Int32":
50-
return .SwiftInt32
51-
case "Int64":
52-
return .SwiftInt64
53-
case "Float":
54-
return .SwiftFloat
55-
case "Double":
56-
return .SwiftDouble
57-
case "Void":
58-
return nil
59-
case "Never":
60-
return nil
61-
case "Swift.UnsafePointer<Swift.UInt8>":
62-
return .SwiftPointer
63-
default:
64-
break
65-
}
66-
67-
// not great?
68-
if ty.swiftTypeName == "Self.self" {
69-
return .SwiftPointer
70-
}
71-
72-
// not great?
73-
if ty.swiftTypeName == "(any Any.Type)?" {
74-
return .SwiftPointer
75-
}
76-
77-
if ty.swiftTypeName == "() -> ()" {
78-
return .SwiftPointer
79-
}
80-
81-
// TODO: Java has OptionalLong, OptionalInt, OptionalDouble types.
82-
// if ty.swiftTypeName.hasSuffix("?") {
83-
// if ty.swiftTypeName == "Int?" {
84-
// return JavaOptionalLong
85-
// } else ..
86-
// }
87-
88-
// Last fallback is to try to get the type's $layout()
89-
return ForeignValueLayout(inlineComment: ty.swiftTypeName, customType: ty.fullyQualifiedName)
90-
}
9137
}

Sources/JExtractSwift/Swift2JavaTranslator+Printing.swift

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -421,15 +421,15 @@ extension Swift2JavaTranslator {
421421
* \(decl.swiftDeclRaw ?? "")
422422
* }
423423
*/
424-
public static \(parentName.javaClassName!) init(\(renderJavaParamDecls(decl, selfVariant: .none))) {
424+
public static \(parentName.javaType) init(\(renderJavaParamDecls(decl, selfVariant: .none))) {
425425
var mh$ = \(descClassIdentifier).HANDLE;
426426
try {
427427
if (TRACE_DOWNCALLS) {
428428
traceDowncall(\(renderForwardParams(decl, selfVariant: nil)));
429429
}
430430
431431
var self = (MemorySegment) mh$.invokeExact(\(renderForwardParams(decl, selfVariant: nil)), TYPE_METADATA);
432-
return new \(parentName.javaClassName!)(self);
432+
return new \(parentName.javaType)(self);
433433
} catch (Throwable ex$) {
434434
throw new AssertionError("should not reach here", ex$);
435435
}
@@ -524,10 +524,10 @@ extension Swift2JavaTranslator {
524524
decl: ImportedFunc,
525525
selfVariant: SelfParameterVariant?
526526
) {
527-
let returnTy = decl.returnType.fullyQualifiedName
527+
let returnTy = decl.returnType.javaType
528528

529529
let maybeReturnCast: String
530-
if decl.returnType.isVoid {
530+
if decl.returnType.javaType == .void {
531531
maybeReturnCast = "" // nothing to return or cast to
532532
} else {
533533
maybeReturnCast = "return (\(returnTy))"
@@ -601,7 +601,7 @@ extension Swift2JavaTranslator {
601601
}
602602

603603
for p in decl.effectiveParameters(selfVariant: selfVariant) {
604-
let param = "\(p.type.fullyQualifiedName) \(p.effectiveName ?? nextUniqueParamName())"
604+
let param = "\(p.type.javaType.description) \(p.effectiveName ?? nextUniqueParamName())"
605605
ps.append(param)
606606
}
607607

@@ -641,7 +641,7 @@ extension Swift2JavaTranslator {
641641
selfVariant: .pointer
642642
)
643643

644-
if decl.returnType.isVoid {
644+
if decl.returnType.javaType == .void {
645645
printer.print("FunctionDescriptor.ofVoid(");
646646
printer.indent()
647647
} else {
@@ -655,10 +655,9 @@ extension Swift2JavaTranslator {
655655
// when initializing, we return a pointer to the newly created object
656656
printer.print("/* -> */\(ForeignValueLayout.SwiftPointer)", .parameterNewlineSeparator(returnTyIsLastTy))
657657
} else {
658-
if var returnDesc = javaMemoryLayoutDescriptor(decl.returnType) {
659-
returnDesc.inlineComment = " -> "
660-
printer.print(returnDesc, .parameterNewlineSeparator(returnTyIsLastTy))
661-
}
658+
var returnDesc = decl.returnType.foreignValueLayout
659+
returnDesc.inlineComment = " -> "
660+
printer.print(returnDesc, .parameterNewlineSeparator(returnTyIsLastTy))
662661
}
663662
}
664663

Sources/JExtractSwift/Swift2JavaVisitor.swift

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@ final class Swift2JavaVisitor: SyntaxVisitor {
120120
}
121121

122122
let params: [ImportedParam]
123-
let javaResultType: ImportedTypeName
123+
let javaResultType: TranslatedType
124124
do {
125125
params = try node.signature.parameterClause.parameters.map { param in
126126
// TODO: more robust parameter handling
127127
// TODO: More robust type handling
128128
return ImportedParam(
129129
param: param,
130-
type: try mapTypeToJava(name: param.type)
130+
type: try cCompatibleType(for: param.type)
131131
)
132132
}
133133

134-
javaResultType = try mapTypeToJava(name: returnTy)
134+
javaResultType = try cCompatibleType(for: returnTy)
135135
} catch {
136136
self.log.info("Unable to import function \(node.name) - \(error)")
137137
return .skipChildren
@@ -147,7 +147,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
147147
let fullName = "\(node.name.text)(\(argumentLabelsStr))"
148148

149149
var funcDecl = ImportedFunc(
150-
parentName: currentTypeName.map { translator.importedTypes[$0] }??.importedTypeName,
150+
parentName: currentTypeName.map { translator.importedTypes[$0] }??.translatedType,
151151
identifier: fullName,
152152
returnType: javaResultType,
153153
parameters: params
@@ -186,7 +186,7 @@ final class Swift2JavaVisitor: SyntaxVisitor {
186186
// TODO: More robust type handling
187187
return ImportedParam(
188188
param: param,
189-
type: try mapTypeToJava(name: param.type)
189+
type: try cCompatibleType(for: param.type)
190190
)
191191
}
192192
} catch {
@@ -198,9 +198,9 @@ final class Swift2JavaVisitor: SyntaxVisitor {
198198
"init(\(params.compactMap { $0.effectiveName ?? "_" }.joined(separator: ":")))"
199199

200200
var funcDecl = ImportedFunc(
201-
parentName: currentType.importedTypeName,
201+
parentName: currentType.translatedType,
202202
identifier: initIdentifier,
203-
returnType: currentType.importedTypeName,
203+
returnType: currentType.translatedType,
204204
parameters: params
205205
)
206206
funcDecl.isInit = true
@@ -254,13 +254,6 @@ extension FunctionDeclSyntax {
254254
}
255255
}
256256

257-
extension Swift2JavaVisitor {
258-
// TODO: this is more more complicated, we need to know our package and imports etc
259-
func mapTypeToJava(name swiftType: TypeSyntax) throws -> ImportedTypeName {
260-
return try cCompatibleType(for: swiftType).importedTypeName
261-
}
262-
}
263-
264257
private let mangledNameCommentPrefix = "MANGLED NAME: "
265258

266259
extension SyntaxProtocol {

Sources/JExtractSwift/TranslatedType.swift

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ enum ParameterConvention {
207207
case indirect
208208
}
209209

210-
struct TranslatedType {
210+
public struct TranslatedType {
211211
/// How a parameter of this type will be passed through C functions.
212212
var cCompatibleConvention: ParameterConvention
213213

@@ -224,14 +224,10 @@ struct TranslatedType {
224224

225225
/// The Java type that is used to present these values in Java.
226226
var javaType: JavaType
227-
}
228227

229-
extension TranslatedType {
230-
var importedTypeName: ImportedTypeName {
231-
ImportedTypeName(
232-
swiftTypeName: originalSwiftType.trimmedDescription,
233-
javaType: javaType
234-
)
228+
/// Produce a Swift type name to reference this type.
229+
var swiftTypeName: String {
230+
originalSwiftType.trimmedDescription
235231
}
236232
}
237233

0 commit comments

Comments
 (0)