1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
|
struct ModuleTranslation {
let diagnostics: DiagnosticCollection
let typeMapping: TypeMapping
var builder: WITBuilder
mutating func translate(sourceSummary: SwiftSourceSummary) {
for (_, typeSource) in sourceSummary.typesByWITName {
translateType(source: typeSource)
}
for (_, functionSource) in sourceSummary.functionsByWITName {
translateFunction(source: functionSource)
}
}
mutating func translateType(source: SwiftTypeSource) {
switch source {
case .structType(let source):
translateStruct(source: source)
case .enumType(let source):
translateEnum(source: source)
}
}
mutating func translateStruct(source: SwiftStructSource) {
guard let witTypeName = typeMapping.lookupWITType(byUsr: source.usr) else {
return
}
var fields: [WITRecord.Field] = []
for field in source.fields {
let fieldName = ConvertCase.witIdentifier(identifier: field.name)
guard let fieldWITType = typeMapping.lookupWITType(byNode: field.type, diagnostics: diagnostics) else {
diagnostics.add(
.skipField(
context: source.node.parent.printedName,
field: field.name,
missingType: field.type.parent.parent.printedName
)
)
continue
}
fields.append(WITRecord.Field(name: fieldName, type: fieldWITType))
}
builder.define(
record: WITRecord(name: witTypeName, fields: fields)
)
}
mutating func translateEnum(source: SwiftEnumSource) {
guard let witTypeName = typeMapping.lookupWITType(byUsr: source.usr) else {
return
}
var cases: [(name: String, type: String?)] = []
var hasPayload = false
for enumCase in source.cases {
let caseName = ConvertCase.witIdentifier(identifier: enumCase.name)
var payloadWITType: String?
if let payloadTypeNode = enumCase.payloadType {
hasPayload = true
payloadWITType = typeMapping.lookupWITType(byNode: payloadTypeNode, diagnostics: diagnostics)
if payloadWITType == nil {
diagnostics.add(
.skipField(
context: source.node.parent.printedName,
field: enumCase.name,
missingType: payloadTypeNode.parent.parent.printedName
)
)
}
}
cases.append((caseName, payloadWITType))
}
// If the given Swift enum has at least one case with payload, turn it into variant,
// otherwise optimize to be enum.
if hasPayload {
builder.define(variant: WITVariant(name: witTypeName, cases: cases.map { WITVariant.Case(name: $0, type: $1) }))
} else {
builder.define(enum: WITEnum(name: witTypeName, cases: cases.map { name, _ in name }))
}
}
mutating func translateFunction(source: SwiftFunctionSource) {
let witName = ConvertCase.witIdentifier(identifier: source.name)
let witResultTypeNames = source.results.compactMap { result -> String? in
guard let singleResult = typeMapping.lookupWITType(byNode: result.type, diagnostics: diagnostics) else {
diagnostics.add(
.skipField(
context: source.name,
field: "result",
missingType: result.type.parent.parent.printedName
)
)
return nil
}
return singleResult
}
guard witResultTypeNames.count == source.results.count else {
// Skip emitting if there is any missing WIT type
return
}
let witResults: WITFunction.Results
switch source.results.count {
case 0:
witResults = .named([])
case 1:
witResults = .anon(witResultTypeNames[0])
default:
var resultNames = AlphabeticalIterator()
witResults = .named(
zip(source.results, witResultTypeNames).map { source, witType in
WITFunction.Parameter(name: source.name ?? resultNames.next(), type: witType)
})
}
var parameterNames = AlphabeticalIterator()
let witParameters = source.parameters.compactMap { param -> WITFunction.Parameter? in
let name = parameterNames.next()
guard let type = typeMapping.lookupWITType(byNode: param.type, diagnostics: diagnostics) else {
diagnostics.add(
.skipField(
context: source.name,
field: param.name ?? "parameter",
missingType: param.type.parent.parent.printedName
)
)
return nil
}
return WITFunction.Parameter(name: name, type: type)
}
guard witParameters.count == source.parameters.count else {
// Skip emitting if there is any missing WIT type
return
}
builder.define(
function: WITFunction(
name: witName,
parameters: witParameters,
results: witResults
)
)
}
}
private struct AlphabeticalIterator {
var index: Int = 0
mutating func next() -> String {
let chars = Array("abcdefghijklmnopqrstuvwxyz")
var buffer: [Character] = []
var tmpIndex = index
while tmpIndex >= chars.count {
buffer.append(chars[tmpIndex % chars.count])
tmpIndex /= chars.count
tmpIndex -= 1
}
buffer.append(chars[tmpIndex])
index += 1
return String(buffer)
}
}
|