-
Notifications
You must be signed in to change notification settings - Fork 19
/
ClassGen.pkl
373 lines (339 loc) · 13.3 KB
/
ClassGen.pkl
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
// ===----------------------------------------------------------------------===//
// Copyright © 2024 Apple Inc. and the Pkl project authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ===----------------------------------------------------------------------===//
@Unlisted
module pkl.swift.internal.ClassGen
extends "Gen.pkl"
import "pkl:reflect"
import "SwiftMapping.pkl"
import "utils.pkl"
import "Type.pkl"
import "typegen.pkl"
clazz: reflect.Class = mapping.source as reflect.Class
classInfo: SwiftMapping.Class = mapping as SwiftMapping.Class
structs: Mapping<String, String>
topLevelContents = protocol
contents = new Listing {
when (classInfo.protocol != null) {
"public typealias \(classInfo.protocol.name) = \(classInfo.protocol.fullName)"
}
when (!isAbstract) {
when (classInfo.protocol != null) {
""
}
struct
}
}.join("\n")
local isAbstract: Boolean = clazz.modifiers.contains("abstract")
local superClass: SwiftMapping.Class? = mappings.findOrNull((c) -> c is SwiftMapping.Class && c.clazz == clazz.superclass) as SwiftMapping.Class?
local properties: Map<String, Property> = getProperties(clazz, mappings)
local imports: List<String> =
properties.values
.flatMap((f) -> f.type.imports)
.filter((i) -> i != classInfo.swiftModuleName).distinct
+ (if (superClass != null && superClass.swiftModuleName != classInfo.swiftModuleName) List(superClass.swiftModuleName) else List())
+ (if (isModule && !isAbstract) List("PklSwift") else List())
local isModule: Boolean = clazz.enclosingDeclaration.moduleClass == clazz
local function getAllProperties(clazz: reflect.Class?): List<reflect.Property> =
if (clazz == null) List()
else if (doesNotInherit(clazz)) clazz.properties.values
else clazz.properties.values + getAllProperties(clazz.superclass!!)
local function isSameType(typeA: reflect.Type, typeB: reflect.Type) =
if (typeA is reflect.DeclaredType && typeB is reflect.DeclaredType)
typeA.referent.reflectee == typeB.referent.reflectee
else if (typeA is reflect.NullableType && typeB is reflect.NullableType)
isSameType(typeA.member, typeB.member)
else if (typeA is reflect.NothingType && typeB is reflect.NothingType)
true
else if (typeA is reflect.UnknownType && typeB is reflect.UnknownType)
true
else if (typeA is reflect.StringLiteralType && typeB is reflect.StringLiteralType)
typeA.value == typeB.value
// union types turn into Swift `AnyHashable?`, so we can say that this is always fine.
else if (typeA is reflect.UnionType && typeB is reflect.UnionType)
true
// remaining types: `FunctionType`, `TypeParameter`, `ModuleType`.
// we can actually check if `ModuleType` refers to the same type by checking if the enclosing declaration is the same,
// but we will pretend it is always false for now.
else false
// visible for testing
function getProperties(
clazz: reflect.Class,
mappings: List<SwiftMapping>
): Map<String, Property> =
let (isSuperOpenOrAbstract: Boolean =
clazz.superclass.modifiers.contains("open") || clazz.superclass.modifiers.contains("abstract"))
// add the properties of the superclass as fields unless it is an open class (because its struct gets embedded),
// or the class does not inherit.
let (superFields: Map<String, Property> =
if (doesNotInherit(clazz)) Map()
else getProperties(clazz.superclass!!, mappings)
)
let (superProperties = getAllProperties(clazz.superclass))
clazz.properties
.filter((propName, prop: reflect.Property) ->
let (superProp = superProperties.findOrNull((it) -> it.name == prop.name))
// don't render hidden members
if (prop.modifiers.contains("hidden")) false
// Okay if there is no property override, or if the super property has the same type.
else if (superProp == null || isSameType(superProp.type, prop.type)) true
// Okay if the property is overridden but does not define a type, but don't render as its own field.
// E.g. `class Foo extends Bar { bar = "mybar" }`
else if (prop.type is reflect.UnknownType) !isSuperOpenOrAbstract
// Otherwise, the property's type has been overridden and this is possible to
// represent in Swift, but makes it harder to generate common getters for enums.
else throw("""
Illegal: Class `\(clazz.reflectee)` overrides property `\(propName)`. This is not supported when generating Swift.
\(prop.location.displayUri)
""")
)
.mapValues((_, prop: reflect.Property) ->
new Property {
isInherited = false
type = typegen.generateType(prop.type, clazz, mappings)
docComment = prop.docComment
name = utils.toSwiftName(prop)
property = prop
}
) + superFields.mapValues((_, field) -> (field) { isInherited = true })
local function doesNotInherit(clazz: reflect.Class?) =
clazz.superclass == null
|| clazz.superclass.reflectee == Typed
local structSuperclasses: String =
if (classInfo.protocol != null)
classInfo.protocol.name
else if (superClass != null)
if (classInfo.namespaceName == superClass.namespaceName)
superClass.name
else
"\(superClass.namespaceName).\(superClass.name)"
else
"PklRegisteredType, Decodable, Hashable"
local struct: String = new Listing {
when (clazz.docComment != null) {
utils.renderDocComment(clazz.docComment!!, "")
"\n"
}
"public struct \(classInfo.struct.name): "
structSuperclasses
" {\n"
"\(module.indent)public static let registeredIdentifier: String = \(utils.toSwiftString(classInfo.source.reflectee.toString()))\n\n"
when (!properties.isEmpty) {
for (pklPropertyName, field in properties) {
when (pklPropertyName != properties.keys.first) {
"\n"
}
renderProperty(field)
}
"\n"
}
synthesisedInit
when (properties.values.any((p) -> p.isPolymorphic)) {
// need to implement ==, hash and both inits
"\n\n"
synthesisedEqualsEquals
"\n\n"
synthesisedHash
"\n\n"
synthesisedInitDecoder
} else {
when (properties.values.any((p) -> p.isAny)) {
// need to implement both inits
"\n\n"
synthesisedInitDecoder
}
}
when (codingKeys != null) {
"\n\n"
codingKeys
}
"\n}"
}.join("")
local synthesisedEqualsEquals =
new Listing {
"\(module.indent)public static func ==(lhs: \(classInfo.struct.name), rhs: \(classInfo.struct.name)) -> Bool {\n"
for (prop, field in properties) {
when (prop != properties.keys.first) {
"\n\(module.indent.repeat(2))&& "
} else {
module.indent.repeat(2)
}
when (field.isPolymorphic) {
if (field.type is Type.Array)
"arrayEquals(arr1: lhs.\(prop), arr2: rhs.\(prop))"
else if (field.type is Type.Dictionary)
"mapEquals(map1: lhs.\(prop), map2: rhs.\(prop))"
else if (field.type is Type.Nullable)
"((lhs.\(prop) == nil && rhs.\(prop) == nil) || lhs.\(prop)?.isDynamicallyEqual(to: rhs.\(prop)) ?? false)"
else
"lhs.\(prop).isDynamicallyEqual(to: rhs.\(prop))"
} else {
"lhs.\(prop) == rhs.\(prop)"
}
}
"\n"
"\(module.indent)}"
}.join("")
local synthesisedHash =
new Listing {
"\(module.indent)public func hash(into hasher: inout Hasher) {\n"
properties.keys.map((pklPropertyName) ->
let (field = properties[pklPropertyName])
if (field.isPolymorphic)
if (field.type is Type.Array)
"""
\(module.indent.repeat(2))for x in self.\(field.name) {
\(module.indent.repeat(3))hasher.combine(x)
\(module.indent.repeat(2))}
"""
else if (field.type is Type.Dictionary)
"""
\(module.indent.repeat(2))for (k, v) in self.\(field.name) {
\(module.indent.repeat(3))hasher.combine(k)
\(module.indent.repeat(3))hasher.combine(v)
\(module.indent.repeat(2))}
"""
else if (field.type is Type.Nullable)
"""
\(module.indent.repeat(2))if let \(field.name) {
\(module.indent.repeat(3))hasher.combine(\(field.name))
\(module.indent.repeat(2))}
"""
else
"\(module.indent.repeat(2))hasher.combine(\(field.name))"
else
"\(module.indent.repeat(2))hasher.combine(\(field.name))"
).join("\n")
"\n"
"\(module.indent)}"
}.join("")
local synthesisedInit =
if (properties.isEmpty) "\(module.indent)public init() {}"
else
let (renderedProperties = properties.keys.map((name) -> renderPropertyBase(properties[name])))
let (renderedPropertiesLength = renderedProperties.toList().map((it) -> it.length + 2).fold(0, (a, b) -> a + b))
new Listing {
"\(module.indent)public init("
when (renderedPropertiesLength > 80) {
"\n"
for (renderedProp in renderedProperties) {
module.indent.repeat(2)
renderedProp
when (renderedProp != renderedProperties.last) {
",\n"
} else {
"\n"
}
}
"\(module.indent)) {\n"
} else {
renderedProperties.join(", ")
") {\n"
}
properties.values.map((prop) ->
"\(module.indent.repeat(2))self.\(prop.name) = \(prop.name)"
).join("\n")
"\n"
"\(module.indent)}"
}.join("")
local synthesisedInitDecoder =
new Listing {
"\(module.indent)public init(from decoder: Decoder) throws {\n"
"\(module.indent.repeat(2))let dec = try decoder.container(keyedBy: PklCodingKey.self)\n"
for (pklPropertyName, field in properties) {
"\(module.indent.repeat(2))let \(field.name) = try dec.decode("
when (field.isPolymorphic || field.isAny) {
field.type.renderGeneric(classInfo.namespaceName)
} else {
field.type.render(classInfo.namespaceName)
}
".self, forKey: PklCodingKey(string: \"\(pklPropertyName)\"))"
when (field.isPolymorphic || field.isAny) {
if (field.type is Type.Array)
"\n\(module.indent.repeat(4)).map { $0.value as! \((field.type as Type.Array).elem.render(classInfo.namespaceName)) }"
else if (field.type is Type.Dictionary)
"\n\(module.indent.repeat(4)).mapValues { $0.value as! \((field.type as Type.Dictionary).elem.render(classInfo.namespaceName)) }"
else if (field.type == typegen.anyType)
".value"
else
"\n\(module.indent.repeat(4)).value as! \(field.type.render(classInfo.namespaceName))"
}
"\n"
}
"\(module.indent.repeat(2))self = \(classInfo.struct.name)("
properties.values.map((prop) ->
"\(prop.name): \(prop.name)"
).join(", ")
")\n\(module.indent)}"
}.join("")
local function renderPropertyBase(property: Property): String =
let (type = property.type.render(classInfo.namespaceName))
"\(property.name): \(type)"
local function renderProperty(property: Property) = new Listing {
when (property.docComment != null) {
utils.renderDocComment(property.docComment!!, module.indent)
"\n"
}
"\(module.indent)public var "
renderPropertyBase(property)
"\n"
}.join("")
local codingKeys: String? =
if (properties.every((pklName, prop) -> pklName == prop.name)) null
else
new Listing {
"\(module.indent)enum CodingKeys: String, CodingKey {"
for (pklName, prop in properties) {
"\(module.indent.repeat(2))case \(prop.name) = \(utils.toSwiftString(pklName))"
}
"\(module.indent)}"
}.join("\n")
local protocol: String? =
if (classInfo.protocol != null)
// Only generate interface methods for properties that don't exist on the superclass.
// Properties on the superclass are handled via interface embedding.
let (methodsToGenerate = properties.filter((_, field) -> !field.isInherited))
new Listing {
"public protocol \(classInfo.protocol.fullName): "
when (superClass != null) {
"\(superClass.protocol.fullName) {\n"
} else {
"PklRegisteredType, DynamicallyEquatable, Hashable {\n"
}
for (key, field in methodsToGenerate) {
when (key != methodsToGenerate.keys.first) {
"\n"
}
"\(module.indent)var \(field.name): \(field.type.render(classInfo.swiftModuleName)) { get }\n"
}
"}\n"
}.join("")
else null
local class Property {
/// Is this property inherited from a parent?
isInherited: Boolean
/// The name of the property
name: String
/// The Swift type associated with this field
type: Type
/// The doc comments on the field
docComment: String?
/// The Pkl property behind the field
property: reflect.Property
/// True if the field is not a concrete type
isPolymorphic: Boolean = type.isPolymorphic
/// True if this field represents the Pkl type Any
isAny: Boolean = type.isAny
}