Skip to content

Commit 80b68a5

Browse files
authored
Merge pull request #8 from DougGregor/translated-type
Finish the migration from ImportedTypeName over to TranslatedType
2 parents a6cf883 + 885b45a commit 80b68a5

11 files changed

+201
-210
lines changed

Sources/JExtractSwift/ImportedDecls.swift

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,39 @@ 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 {
31-
public var name: ImportedTypeName
27+
public let swiftTypeName: String
28+
public let javaType: JavaType
29+
public var swiftMangledName: String?
3230
public var kind: NominalTypeKind
3331

3432
public var initializers: [ImportedFunc] = []
3533
public var methods: [ImportedFunc] = []
3634

37-
public init(name: ImportedTypeName, kind: NominalTypeKind) {
38-
self.name = name
35+
public init(swiftTypeName: String, javaType: JavaType, swiftMangledName: String? = nil, kind: NominalTypeKind) {
36+
self.swiftTypeName = swiftTypeName
37+
self.javaType = javaType
38+
self.swiftMangledName = swiftMangledName
3939
self.kind = kind
4040
}
41+
42+
var translatedType: TranslatedType {
43+
TranslatedType(
44+
cCompatibleConvention: .direct,
45+
originalSwiftType: "\(raw: swiftTypeName)",
46+
cCompatibleSwiftType: "UnsafeRawPointer",
47+
cCompatibleJavaMemoryLayout: .heapObject,
48+
javaType: javaType
49+
)
50+
}
4151
}
4252

4353
public enum NominalTypeKind {
@@ -47,7 +57,7 @@ public enum NominalTypeKind {
4757
case `struct`
4858
}
4959

50-
public struct ImportedParam: Hashable {
60+
public struct ImportedParam {
5161
let param: FunctionParameterSyntax
5262

5363
var firstName: String? {
@@ -78,7 +88,7 @@ public struct ImportedParam: Hashable {
7888
}
7989

8090
// The mapped-to Java type of the above Java type, collections and optionals may be replaced with Java ones etc.
81-
var type: ImportedTypeName
91+
var type: TranslatedType
8292
}
8393

8494
extension ImportedParam {
@@ -91,30 +101,6 @@ extension ImportedParam {
91101
}
92102
}
93103

94-
public struct ImportedTypeName: Hashable {
95-
public var swiftTypeName: String
96-
97-
public var swiftMangledName: String = ""
98-
99-
public var javaType: JavaType
100-
101-
public var isVoid: Bool { javaType == .void }
102-
103-
public var fullyQualifiedName: String { javaType.description }
104-
105-
/// Retrieve the Java class name that this type describes, or nil if it
106-
/// doesn't represent a class at all.
107-
public var javaClassName: String? {
108-
javaType.className
109-
}
110-
111-
public init(swiftTypeName: String, javaType: JavaType, swiftMangledName: String? = nil) {
112-
self.swiftTypeName = swiftTypeName
113-
self.javaType = javaType
114-
self.swiftMangledName = swiftMangledName ?? ""
115-
}
116-
}
117-
118104
// TODO: this is used in different contexts and needs a cleanup
119105
public enum SelfParameterVariant {
120106
/// Make a method that accepts the raw memory pointer as a MemorySegment
@@ -130,7 +116,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
130116
/// this will contain that declaration's imported name.
131117
///
132118
/// This is necessary when rendering accessor Java code we need the type that "self" is expecting to have.
133-
public var parentName: ImportedTypeName?
119+
var parentName: TranslatedType?
134120
public var hasParent: Bool { parentName != nil }
135121

136122
/// This is a full name such as init(cap:name:).
@@ -155,7 +141,7 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
155141
return identifier
156142
}
157143

158-
public var returnType: ImportedTypeName
144+
public var returnType: TranslatedType
159145
public var parameters: [ImportedParam]
160146

161147
public func effectiveParameters(selfVariant: SelfParameterVariant?) -> [ImportedParam] {
@@ -172,13 +158,21 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
172158
case .pointer:
173159
let selfParam: FunctionParameterSyntax = "self$: $swift_pointer"
174160
params.append(
175-
ImportedParam(param: selfParam, type: java_lang_foreign_MemorySegment(swiftTypeName: "Self.self"))
161+
ImportedParam(
162+
param: selfParam,
163+
type: parentName
164+
)
176165
)
177166

178167
case .memorySegment:
179168
let selfParam: FunctionParameterSyntax = "self$: $java_lang_foreign_MemorySegment"
169+
var parentForSelf = parentName
170+
parentForSelf.javaType = .javaForeignMemorySegment
180171
params.append(
181-
ImportedParam(param: selfParam, type: java_lang_foreign_MemorySegment(swiftTypeName: ""))
172+
ImportedParam(
173+
param: selfParam,
174+
type: parentForSelf
175+
)
182176
)
183177

184178
case .wrapper:
@@ -201,9 +195,9 @@ public struct ImportedFunc: ImportedDecl, CustomStringConvertible {
201195
public var isInit: Bool = false
202196

203197
public init(
204-
parentName: ImportedTypeName?,
198+
parentName: TranslatedType?,
205199
identifier: String,
206-
returnType: ImportedTypeName,
200+
returnType: TranslatedType,
207201
parameters: [ImportedParam]
208202
) {
209203
self.parentName = parentName

Sources/JExtractSwift/JavaConstants/ForeignValueLayouts.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public struct ForeignValueLayout: CustomStringConvertible {
5454
}
5555

5656
extension ForeignValueLayout {
57-
public static let SwiftSelf = Self(inlineComment: "Self", javaConstant: "SWIFT_SELF")
5857
public static let SwiftPointer = Self(javaConstant: "SWIFT_POINTER")
5958

6059
public static let SwiftBool = Self(javaConstant: "SWIFT_BOOL")
@@ -63,6 +62,7 @@ extension ForeignValueLayout {
6362
public static let SwiftInt64 = Self(javaConstant: "SWIFT_INT64")
6463
public static let SwiftInt32 = Self(javaConstant: "SWIFT_INT32")
6564
public static let SwiftInt16 = Self(javaConstant: "SWIFT_INT16")
65+
public static let SwiftUInt16 = Self(javaConstant: "SWIFT_UINT16")
6666
public static let SwiftInt8 = Self(javaConstant: "SWIFT_INT8")
6767

6868
public static let SwiftFloat = Self(javaConstant: "SWIFT_FLOAT")

Sources/JExtractSwift/JavaTypes.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,6 @@
1313

1414
import JavaTypes
1515

16-
func java_lang_foreign_MemorySegment(swiftTypeName: String) -> ImportedTypeName {
17-
ImportedTypeName(
18-
swiftTypeName: swiftTypeName,
19-
javaType: .javaForeignMemorySegment
20-
)
21-
}
22-
2316
extension JavaType {
2417
/// The description of the type java.lang.foreign.MemorySegment.
2518
static var javaForeignMemorySegment: JavaType {

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: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ extension Swift2JavaTranslator {
2828
var printer = CodePrinter()
2929

3030
for (_, ty) in importedTypes.sorted(by: { (lhs, rhs) in lhs.key < rhs.key }) {
31-
let filename = "\(ty.name.javaClassName!).java"
31+
let filename = "\(ty.javaType).java"
3232
log.info("Printing contents: \(filename)")
3333
printImportedClass(&printer, ty)
3434

@@ -116,7 +116,7 @@ extension Swift2JavaTranslator {
116116
printer.print(
117117
"""
118118
// FIXME: this detecting is somewhat off
119-
public static final String TYPE_METADATA_NAME = "\(decl.name.swiftMangledName)";
119+
public static final String TYPE_METADATA_NAME = "\(decl.swiftMangledName!)";
120120
static final MemorySegment TYPE_METADATA = SwiftKit.getTypeByMangledNameInEnvironment(TYPE_METADATA_NAME);
121121
"""
122122
)
@@ -165,7 +165,7 @@ extension Swift2JavaTranslator {
165165
}
166166

167167
public func printClass(_ printer: inout CodePrinter, _ decl: ImportedNominalType, body: (inout CodePrinter) -> Void) {
168-
printer.printTypeDecl("public final class \(decl.name.javaClassName!)") { printer in
168+
printer.printTypeDecl("public final class \(decl.javaType)") { printer in
169169
// ==== Storage of the class
170170
// FIXME: implement the self storage for the memory address and accessors
171171
printClassSelfProperty(&printer, decl)
@@ -274,7 +274,7 @@ extension Swift2JavaTranslator {
274274
printer.print(
275275
"""
276276
/** Instances are created using static {@code init} methods rather than through the constructor directly. */
277-
private \(decl.name.javaClassName!)(MemorySegment selfMemorySegment) {
277+
private \(decl.javaType)(MemorySegment selfMemorySegment) {
278278
this.selfMemorySegment = selfMemorySegment;
279279
}
280280
"""
@@ -307,7 +307,7 @@ extension Swift2JavaTranslator {
307307
// SWIFT_INT.withName("heapObject"),
308308
// ...
309309
// SWIFT_INT.withName("cap")
310-
).withName("\(decl.name.javaClassName!)"); // TODO: is the name right?
310+
).withName("\(decl.javaType)"); // TODO: is the name right?
311311
312312
/**
313313
* When other types refer to this type, they refer to by a pointer,
@@ -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

0 commit comments

Comments
 (0)