forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDerivedConformanceCodable.cpp
1224 lines (1057 loc) · 49.8 KB
/
DerivedConformanceCodable.cpp
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//===--- DerivedConformanceCodable.cpp - Derived Codable ------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements explicit derivation of the Encodable and Decodable
// protocols for a struct or class.
//
//===----------------------------------------------------------------------===//
#include "TypeChecker.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Expr.h"
#include "swift/AST/Module.h"
#include "swift/AST/ParameterList.h"
#include "swift/AST/Pattern.h"
#include "swift/AST/Stmt.h"
#include "swift/AST/Types.h"
#include "DerivedConformances.h"
using namespace swift;
/// Returns whether the type represented by the given ClassDecl inherits from a
/// type which conforms to the given protocol.
///
/// \param target The \c ClassDecl whose superclass to look up.
///
/// \param proto The protocol to check conformance for.
static bool inheritsConformanceTo(ClassDecl *target, ProtocolDecl *proto) {
if (!target->hasSuperclass())
return false;
auto *superclassDecl = target->getSuperclassDecl();
auto *superclassModule = superclassDecl->getModuleContext();
return (bool)superclassModule->lookupConformance(target->getSuperclass(),
proto);
}
/// Returns whether the superclass of the given class conforms to Encodable.
///
/// \param target The \c ClassDecl whose superclass to check.
static bool superclassIsEncodable(ClassDecl *target) {
auto &C = target->getASTContext();
return inheritsConformanceTo(target,
C.getProtocol(KnownProtocolKind::Encodable));
}
/// Returns whether the superclass of the given class conforms to Decodable.
///
/// \param target The \c ClassDecl whose superclass to check.
static bool superclassIsDecodable(ClassDecl *target) {
auto &C = target->getASTContext();
return inheritsConformanceTo(target,
C.getProtocol(KnownProtocolKind::Decodable));
}
/// Represents the possible outcomes of checking whether a decl conforms to
/// Encodable or Decodable.
enum CodableConformanceType {
TypeNotValidated,
DoesNotConform,
Conforms
};
/// Returns whether the given type conforms to the given {En,De}codable
/// protocol.
///
/// \param tc The typechecker to use in validating {En,De}codable conformance.
///
/// \param context The \c DeclContext the var declarations belong to.
///
/// \param target The \c Type to validate.
///
/// \param proto The \c ProtocolDecl to check conformance to.
static CodableConformanceType typeConformsToCodable(TypeChecker &tc,
DeclContext *context,
Type target, bool isIUO,
ProtocolDecl *proto) {
target = context->mapTypeIntoContext(target->mapTypeOutOfContext());
// Some generic types need to be introspected to get at their "true" Codable
// conformance.
if (auto referenceType = target->getAs<ReferenceStorageType>()) {
// This is a weak/unowned/unmanaged var. Get the inner type before checking
// conformance.
target = referenceType->getReferentType();
}
if (isIUO)
return typeConformsToCodable(tc, context, target->getOptionalObjectType(),
false, proto);
return tc.conformsToProtocol(target, proto, context,
ConformanceCheckFlags::Used) ? Conforms
: DoesNotConform;
}
/// Returns whether the given variable conforms to the given {En,De}codable
/// protocol.
///
/// \param tc The typechecker to use in validating {En,De}codable conformance.
///
/// \param context The \c DeclContext in which to check conformance.
///
/// \param varDecl The \c VarDecl to validate.
///
/// \param proto The \c ProtocolDecl to check conformance to.
static CodableConformanceType varConformsToCodable(TypeChecker &tc,
DeclContext *context,
VarDecl *varDecl,
ProtocolDecl *proto) {
// If the decl doesn't yet have a type, we may be seeing it before the type
// checker has gotten around to evaluating its type. For example:
//
// func foo() {
// let b = Bar(from: decoder) // <- evaluates Bar conformance to Codable,
// // forcing derivation
// }
//
// struct Bar : Codable {
// var x: Int // <- we get to valuate x's var decl here, but its type
// // hasn't yet been evaluated
// }
//
// Validate the decl eagerly.
if (!varDecl->hasType())
tc.validateDecl(varDecl);
// If the var decl didn't validate, it may still not have a type; confirm it
// has a type before ensuring the type conforms to Codable.
if (!varDecl->hasType())
return TypeNotValidated;
bool isIUO =
varDecl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>();
return typeConformsToCodable(tc, context, varDecl->getType(), isIUO, proto);
}
/// Validates the given CodingKeys enum decl by ensuring its cases are a 1-to-1
/// match with the stored vars of the given type.
///
/// \param codingKeysDecl The \c CodingKeys enum decl to validate.
static bool validateCodingKeysEnum(DerivedConformance &derived,
EnumDecl *codingKeysDecl) {
auto &tc = derived.TC;
auto conformanceDC = derived.getConformanceContext();
// Look through all var decls in the given type.
// * Filter out lazy/computed vars.
// * Filter out ones which are present in the given decl (by name).
//
// If any of the entries in the CodingKeys decl are not present in the type
// by name, then this decl doesn't match.
// If there are any vars left in the type which don't have a default value
// (for Decodable), then this decl doesn't match.
// Here we'll hold on to properties by name -- when we've validated a property
// against its CodingKey entry, it will get removed.
llvm::SmallDenseMap<Identifier, VarDecl *, 8> properties;
for (auto *varDecl :
derived.Nominal->getStoredProperties(/*skipInaccessible=*/true)) {
if (varDecl->getAttrs().hasAttribute<LazyAttr>())
continue;
properties[varDecl->getName()] = varDecl;
}
bool propertiesAreValid = true;
for (auto elt : codingKeysDecl->getAllElements()) {
auto it = properties.find(elt->getName());
if (it == properties.end()) {
tc.diagnose(elt->getLoc(), diag::codable_extraneous_codingkey_case_here,
elt->getName());
// TODO: Investigate typo-correction here; perhaps the case name was
// misspelled and we can provide a fix-it.
propertiesAreValid = false;
continue;
}
// We have a property to map to. Ensure it's {En,De}codable.
auto conformance =
varConformsToCodable(tc, conformanceDC, it->second, derived.Protocol);
switch (conformance) {
case Conforms:
// The property was valid. Remove it from the list.
properties.erase(it);
break;
case DoesNotConform:
tc.diagnose(it->second->getLoc(),
diag::codable_non_conforming_property_here,
derived.getProtocolType(), it->second->getType());
LLVM_FALLTHROUGH;
case TypeNotValidated:
// We don't produce a diagnostic for a type which failed to validate.
// This will produce a diagnostic elsewhere anyway.
propertiesAreValid = false;
continue;
}
}
if (!propertiesAreValid)
return false;
// If there are any remaining properties which the CodingKeys did not cover,
// we can skip them on encode. On decode, though, we can only skip them if
// they have a default value.
if (!properties.empty() &&
derived.Protocol->isSpecificProtocol(KnownProtocolKind::Decodable)) {
for (auto it = properties.begin(); it != properties.end(); ++it) {
// If the var is default initializable, then it need not have an explicit
// initial value.
auto *varDecl = it->second;
if (auto pbd = varDecl->getParentPatternBinding()) {
if (pbd->isDefaultInitializable())
continue;
}
if (varDecl->getParentInitializer())
continue;
// The var was not default initializable, and did not have an explicit
// initial value.
propertiesAreValid = false;
tc.diagnose(it->second->getLoc(), diag::codable_non_decoded_property_here,
derived.getProtocolType(), it->first);
}
}
return propertiesAreValid;
}
/// A type which has information about the validity of an encountered
/// CodingKeys type.
struct CodingKeysValidity {
bool hasType;
bool isValid;
CodingKeysValidity(bool ht, bool iv) : hasType(ht), isValid(iv) {}
};
/// Returns whether the given type has a valid nested \c CodingKeys enum.
///
/// If the type has an invalid \c CodingKeys entity, produces diagnostics to
/// complain about the error. In this case, the error result will be true -- in
/// the case where we don't have a valid CodingKeys enum and have produced
/// diagnostics here, we don't want to then attempt to synthesize a CodingKeys
/// enum.
///
/// \returns A \c CodingKeysValidity value representing the result of the check.
static CodingKeysValidity hasValidCodingKeysEnum(DerivedConformance &derived) {
auto &tc = derived.TC;
auto &C = tc.Context;
auto codingKeysDecls =
derived.Nominal->lookupDirect(DeclName(C.Id_CodingKeys));
if (codingKeysDecls.empty())
return CodingKeysValidity(/*hasType=*/false, /*isValid=*/true);
// Only ill-formed code would produce multiple results for this lookup.
// This would get diagnosed later anyway, so we're free to only look at the
// first result here.
auto result = codingKeysDecls.front();
auto *codingKeysTypeDecl = dyn_cast<TypeDecl>(result);
if (!codingKeysTypeDecl) {
tc.diagnose(result->getLoc(),
diag::codable_codingkeys_type_is_not_an_enum_here,
derived.getProtocolType());
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
}
// If the decl hasn't been validated yet, do so.
tc.validateDecl(codingKeysTypeDecl);
// CodingKeys may be a typealias. If so, follow the alias to its canonical
// type.
auto codingKeysType = codingKeysTypeDecl->getDeclaredInterfaceType();
if (isa<TypeAliasDecl>(codingKeysTypeDecl))
codingKeysTypeDecl = codingKeysType->getAnyNominal();
// Ensure that the type we found conforms to the CodingKey protocol.
auto *codingKeyProto = C.getProtocol(KnownProtocolKind::CodingKey);
if (!tc.conformsToProtocol(codingKeysType, codingKeyProto,
derived.getConformanceContext(),
ConformanceCheckFlags::Used)) {
// If CodingKeys is a typealias which doesn't point to a valid nominal type,
// codingKeysTypeDecl will be nullptr here. In that case, we need to warn on
// the location of the usage, since there isn't an underlying type to
// diagnose on.
SourceLoc loc = codingKeysTypeDecl ?
codingKeysTypeDecl->getLoc() :
cast<TypeDecl>(result)->getLoc();
tc.diagnose(loc, diag::codable_codingkeys_type_does_not_conform_here,
derived.getProtocolType());
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
}
// CodingKeys must be an enum for synthesized conformance.
auto *codingKeysEnum = dyn_cast<EnumDecl>(codingKeysTypeDecl);
if (!codingKeysEnum) {
tc.diagnose(codingKeysTypeDecl->getLoc(),
diag::codable_codingkeys_type_is_not_an_enum_here,
derived.getProtocolType());
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/false);
}
bool valid = validateCodingKeysEnum(derived, codingKeysEnum);
return CodingKeysValidity(/*hasType=*/true, /*isValid=*/valid);
}
/// Synthesizes a new \c CodingKeys enum based on the {En,De}codable members of
/// the given type (\c nullptr if unable to synthesize).
///
/// If able to synthesize the enum, adds it directly to \c derived.Nominal.
static EnumDecl *synthesizeCodingKeysEnum(DerivedConformance &derived) {
auto &tc = derived.TC;
auto &C = tc.Context;
// Create CodingKeys in the parent type always, because both
// Encodable and Decodable might want to use it, and they may have
// different conditional bounds. CodingKeys is simple and can't
// depend on those bounds.
auto target = derived.Nominal;
// We want to look through all the var declarations of this type to create
// enum cases based on those var names.
auto *codingKeyProto = C.getProtocol(KnownProtocolKind::CodingKey);
auto *codingKeyType = codingKeyProto->getDeclaredType();
TypeLoc protoTypeLoc[1] = {TypeLoc::withoutLoc(codingKeyType)};
MutableArrayRef<TypeLoc> inherited = C.AllocateCopy(protoTypeLoc);
auto *enumDecl = new (C) EnumDecl(SourceLoc(), C.Id_CodingKeys, SourceLoc(),
inherited, nullptr, target);
enumDecl->setImplicit();
enumDecl->setAccess(AccessLevel::Private);
// For classes which inherit from something Encodable or Decodable, we
// provide case `super` as the first key (to be used in encoding super).
auto *classDecl = dyn_cast<ClassDecl>(target);
if (classDecl &&
(superclassIsEncodable(classDecl) || superclassIsDecodable(classDecl))) {
// TODO: Ensure the class doesn't already have or inherit a variable named
// "`super`"; otherwise we will generate an invalid enum. In that case,
// diagnose and bail.
auto *super = new (C) EnumElementDecl(SourceLoc(), C.Id_super, nullptr,
SourceLoc(), nullptr, enumDecl);
super->setImplicit();
enumDecl->addMember(super);
}
// Each of these vars needs a case in the enum. For each var decl, if the type
// conforms to {En,De}codable, add it to the enum.
bool allConform = true;
for (auto *varDecl : target->getStoredProperties(/*skipInaccessible=*/true)) {
if (varDecl->getAttrs().hasAttribute<LazyAttr>())
continue;
// Despite creating the enum in the context of the type, we're
// concurrently checking the variables for the current protocol
// conformance being synthesized, for which we use the conformance
// context, not the type.
auto conformance = varConformsToCodable(tc, derived.getConformanceContext(),
varDecl, derived.Protocol);
switch (conformance) {
case Conforms:
{
auto *elt = new (C) EnumElementDecl(SourceLoc(), varDecl->getName(),
nullptr, SourceLoc(), nullptr,
enumDecl);
elt->setImplicit();
enumDecl->addMember(elt);
break;
}
case DoesNotConform:
tc.diagnose(varDecl->getLoc(),
diag::codable_non_conforming_property_here,
derived.getProtocolType(), varDecl->getType());
LLVM_FALLTHROUGH;
case TypeNotValidated:
// We don't produce a diagnostic for a type which failed to validate.
// This will produce a diagnostic elsewhere anyway.
allConform = false;
continue;
}
}
if (!allConform)
return nullptr;
// Forcibly derive conformance to CodingKey.
tc.checkConformancesInContext(enumDecl, enumDecl);
// Add to the type.
target->addMember(enumDecl);
return enumDecl;
}
/// Fetches the \c CodingKeys enum nested in \c target, potentially reaching
/// through a typealias if the "CodingKeys" entity is a typealias.
///
/// This is only useful once a \c CodingKeys enum has been validated (via \c
/// hasValidCodingKeysEnum) or synthesized (via \c synthesizeCodingKeysEnum).
///
/// \param C The \c ASTContext to perform the lookup in.
///
/// \param target The target type to look in.
///
/// \return A retrieved canonical \c CodingKeys enum if \c target has a valid
/// one; \c nullptr otherwise.
static EnumDecl *lookupEvaluatedCodingKeysEnum(ASTContext &C,
NominalTypeDecl *target) {
auto codingKeyDecls = target->lookupDirect(DeclName(C.Id_CodingKeys));
if (codingKeyDecls.empty())
return nullptr;
auto *codingKeysDecl = codingKeyDecls.front();
if (auto *typealiasDecl = dyn_cast<TypeAliasDecl>(codingKeysDecl))
codingKeysDecl = typealiasDecl->getDeclaredInterfaceType()->getAnyNominal();
return dyn_cast<EnumDecl>(codingKeysDecl);
}
/// Creates a new var decl representing
///
/// var/let container : containerBase<keyType>
///
/// \c containerBase is the name of the type to use as the base (either
/// \c KeyedEncodingContainer or \c KeyedDecodingContainer).
///
/// \param C The AST context to create the decl in.
///
/// \param DC The \c DeclContext to create the decl in.
///
/// \param keyedContainerDecl The generic type to bind the key type in.
///
/// \param keyType The key type to bind to the container type.
///
/// \param spec Whether to declare the variable as immutable.
static VarDecl *createKeyedContainer(ASTContext &C, DeclContext *DC,
NominalTypeDecl *keyedContainerDecl,
Type keyType, VarDecl::Specifier spec) {
// Bind Keyed*Container to Keyed*Container<KeyType>
Type boundType[1] = {keyType};
auto containerType = BoundGenericType::get(keyedContainerDecl, Type(),
C.AllocateCopy(boundType));
// let container : Keyed*Container<KeyType>
auto *containerDecl = new (C) VarDecl(/*IsStatic=*/false, spec,
/*IsCaptureList=*/false, SourceLoc(),
C.Id_container, DC);
containerDecl->setImplicit();
containerDecl->setInterfaceType(containerType);
return containerDecl;
}
/// Creates a new \c CallExpr representing
///
/// base.container(keyedBy: CodingKeys.self)
///
/// \param C The AST context to create the expression in.
///
/// \param DC The \c DeclContext to create any decls in.
///
/// \param base The base expression to make the call on.
///
/// \param returnType The return type of the call.
///
/// \param param The parameter to the call.
static CallExpr *createContainerKeyedByCall(ASTContext &C, DeclContext *DC,
Expr *base, Type returnType,
NominalTypeDecl *param) {
// (keyedBy:)
auto *keyedByDecl = new (C)
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(),
C.Id_keyedBy, SourceLoc(), C.Id_keyedBy, DC);
keyedByDecl->setImplicit();
keyedByDecl->setInterfaceType(returnType);
// container(keyedBy:) method name
auto *paramList = ParameterList::createWithoutLoc(keyedByDecl);
DeclName callName(C, C.Id_container, paramList);
// base.container(keyedBy:) expr
auto *unboundCall = new (C) UnresolvedDotExpr(base, SourceLoc(), callName,
DeclNameLoc(),
/*Implicit=*/true);
// CodingKeys.self expr
auto *codingKeysExpr = TypeExpr::createForDecl(SourceLoc(),
param,
param->getDeclContext(),
/*Implicit=*/true);
auto *codingKeysMetaTypeExpr = new (C) DotSelfExpr(codingKeysExpr,
SourceLoc(), SourceLoc());
// Full bound base.container(keyedBy: CodingKeys.self) call
Expr *args[1] = {codingKeysMetaTypeExpr};
Identifier argLabels[1] = {C.Id_keyedBy};
return CallExpr::createImplicit(C, unboundCall, C.AllocateCopy(args),
C.AllocateCopy(argLabels));
}
/// Synthesizes the body for `func encode(to encoder: Encoder) throws`.
///
/// \param encodeDecl The function decl whose body to synthesize.
static void deriveBodyEncodable_encode(AbstractFunctionDecl *encodeDecl) {
// struct Foo : Codable {
// var x: Int
// var y: String
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case x
// case y
// }
//
// @derived func encode(to encoder: Encoder) throws {
// var container = encoder.container(keyedBy: CodingKeys.self)
// try container.encode(x, forKey: .x)
// try container.encode(y, forKey: .y)
// }
// }
// The enclosing type decl.
auto *targetDecl = encodeDecl->getDeclContext()->getSelfNominalTypeDecl();
auto *funcDC = cast<DeclContext>(encodeDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, targetDecl);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
SmallVector<ASTNode, 5> statements;
// Generate a reference to containerExpr ahead of time in case there are no
// properties to encode or decode, but the type is a class which inherits from
// something Codable and needs to encode super.
// let container : KeyedEncodingContainer<CodingKeys>
auto codingKeysType = codingKeysEnum->getDeclaredType();
auto *containerDecl = createKeyedContainer(C, funcDC,
C.getKeyedEncodingContainerDecl(),
codingKeysType,
VarDecl::Specifier::Var);
auto *containerExpr = new (C) DeclRefExpr(ConcreteDeclRef(containerDecl),
DeclNameLoc(), /*Implicit=*/true,
AccessSemantics::DirectToStorage);
// Need to generate
// `let container = encoder.container(keyedBy: CodingKeys.self)`
// This is unconditional because a type with no properties should encode as an
// empty container.
//
// `let container` (containerExpr) is generated above.
// encoder
auto encoderParam = encodeDecl->getParameters()->get(0);
auto *encoderExpr = new (C) DeclRefExpr(ConcreteDeclRef(encoderParam),
DeclNameLoc(), /*Implicit=*/true);
// Bound encoder.container(keyedBy: CodingKeys.self) call
auto containerType = containerDecl->getInterfaceType();
auto *callExpr = createContainerKeyedByCall(C, funcDC, encoderExpr,
containerType, codingKeysEnum);
// Full `let container = encoder.container(keyedBy: CodingKeys.self)`
// binding.
auto *containerPattern = new (C) NamedPattern(containerDecl,
/*implicit=*/true);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern, callExpr, funcDC);
statements.push_back(bindingDecl);
statements.push_back(containerDecl);
// Now need to generate `try container.encode(x, forKey: .x)` for all
// existing properties. Optional properties get `encodeIfPresent`.
for (auto *elt : codingKeysEnum->getAllElements()) {
VarDecl *varDecl = nullptr;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName()))) {
if (auto *vd = dyn_cast<VarDecl>(decl)) {
if (!vd->isStatic()) {
varDecl = vd;
break;
}
}
}
assert(varDecl && "Should have found at least 1 var decl");
// self.x
auto *selfRef = DerivedConformance::createSelfDeclRef(encodeDecl);
auto *varExpr = new (C) MemberRefExpr(selfRef, SourceLoc(),
ConcreteDeclRef(varDecl),
DeclNameLoc(), /*Implicit=*/true);
// CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
auto *metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
// encode(_:forKey:)/encodeIfPresent(_:forKey:)
auto methodName = C.Id_encode;
auto varType = varDecl->getType();
if (auto referenceType = varType->getAs<ReferenceStorageType>()) {
// This is a weak/unowned/unmanaged var. Get the inner type before
// checking optionality.
varType = referenceType->getReferentType();
}
if (varType->getAnyNominal() == C.getOptionalDecl())
methodName = C.Id_encodeIfPresent;
SmallVector<Identifier, 2> argNames{Identifier(), C.Id_forKey};
DeclName name(C, methodName, argNames);
auto *encodeCall = new (C) UnresolvedDotExpr(containerExpr, SourceLoc(),
name, DeclNameLoc(),
/*Implicit=*/true);
// container.encode(self.x, forKey: CodingKeys.x)
Expr *args[2] = {varExpr, keyExpr};
auto *callExpr = CallExpr::createImplicit(C, encodeCall,
C.AllocateCopy(args),
C.AllocateCopy(argNames));
// try container.encode(self.x, forKey: CodingKeys.x)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(tryExpr);
}
// Classes which inherit from something Codable should encode super as well.
auto *classDecl = dyn_cast<ClassDecl>(targetDecl);
if (classDecl && superclassIsEncodable(classDecl)) {
// Need to generate `try super.encode(to: container.superEncoder())`
// superEncoder()
auto *method = new (C) UnresolvedDeclRefExpr(DeclName(C.Id_superEncoder),
DeclRefKind::Ordinary,
DeclNameLoc());
// container.superEncoder()
auto *superEncoderRef = new (C) DotSyntaxCallExpr(containerExpr,
SourceLoc(), method);
// encode(to:) expr
auto *encodeDeclRef = new (C) DeclRefExpr(ConcreteDeclRef(encodeDecl),
DeclNameLoc(), /*Implicit=*/true);
// super
auto *superRef = new (C) SuperRefExpr(encodeDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.encode(to:)
auto *encodeCall = new (C) DotSyntaxCallExpr(superRef, SourceLoc(),
encodeDeclRef);
// super.encode(to: container.superEncoder())
Expr *args[1] = {superEncoderRef};
Identifier argLabels[1] = {C.Id_to};
auto *callExpr = CallExpr::createImplicit(C, encodeCall,
C.AllocateCopy(args),
C.AllocateCopy(argLabels));
// try super.encode(to: container.superEncoder())
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(tryExpr);
}
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
encodeDecl->setBody(body);
}
/// Synthesizes a function declaration for `encode(to: Encoder) throws` with a
/// lazily synthesized body for the given type.
///
/// Adds the function declaration to the given type before returning it.
static FuncDecl *deriveEncodable_encode(DerivedConformance &derived) {
auto &C = derived.TC.Context;
auto conformanceDC = derived.getConformanceContext();
// Expected type: (Self) -> (Encoder) throws -> ()
// Constructed as: func type
// input: Self
// throws
// output: function type
// input: Encoder
// output: ()
// Create from the inside out:
auto encoderType = C.getEncoderDecl()->getDeclaredInterfaceType();
auto returnType = TupleType::getEmpty(C);
// Params: (Encoder)
auto *encoderParam = new (C)
ParamDecl(VarDecl::Specifier::Default, SourceLoc(), SourceLoc(), C.Id_to,
SourceLoc(), C.Id_encoder, conformanceDC);
encoderParam->setInterfaceType(encoderType);
ParameterList *params = ParameterList::createWithoutLoc(encoderParam);
// Func name: encode(to: Encoder)
DeclName name(C, C.Id_encode, params);
auto *encodeDecl = FuncDecl::create(
C, SourceLoc(), StaticSpellingKind::None, SourceLoc(), name, SourceLoc(),
/*Throws=*/true, SourceLoc(), nullptr, params,
TypeLoc::withoutLoc(returnType), conformanceDC);
encodeDecl->setImplicit();
encodeDecl->setSynthesized();
encodeDecl->setBodySynthesizer(deriveBodyEncodable_encode);
// This method should be marked as 'override' for classes inheriting Encodable
// conformance from a parent class.
auto *classDecl = dyn_cast<ClassDecl>(derived.Nominal);
if (classDecl && superclassIsEncodable(classDecl)) {
auto *attr = new (C) OverrideAttr(/*IsImplicit=*/true);
encodeDecl->getAttrs().add(attr);
}
if (auto env = conformanceDC->getGenericEnvironmentOfContext())
encodeDecl->setGenericEnvironment(env);
encodeDecl->computeType(FunctionType::ExtInfo().withThrows());
encodeDecl->setValidationToChecked();
encodeDecl->copyFormalAccessFrom(derived.Nominal,
/*sourceIsParentContext*/ true);
C.addSynthesizedDecl(encodeDecl);
derived.addMembersToConformanceContext({encodeDecl});
return encodeDecl;
}
/// Synthesizes the body for `init(from decoder: Decoder) throws`.
///
/// \param initDecl The function decl whose body to synthesize.
static void deriveBodyDecodable_init(AbstractFunctionDecl *initDecl) {
// struct Foo : Codable {
// var x: Int
// var y: String
//
// // Already derived by this point if possible.
// @derived enum CodingKeys : CodingKey {
// case x
// case y
// }
//
// @derived init(from decoder: Decoder) throws {
// let container = try decoder.container(keyedBy: CodingKeys.self)
// x = try container.decode(Type.self, forKey: .x)
// y = try container.decode(Type.self, forKey: .y)
// }
// }
// The enclosing type decl.
auto conformanceDC = initDecl->getDeclContext();
auto *targetDecl = conformanceDC->getSelfNominalTypeDecl();
auto *funcDC = cast<DeclContext>(initDecl);
auto &C = funcDC->getASTContext();
// We'll want the CodingKeys enum for this type, potentially looking through
// a typealias.
auto *codingKeysEnum = lookupEvaluatedCodingKeysEnum(C, targetDecl);
// We should have bailed already if:
// a) The type does not have CodingKeys
// b) The type is not an enum
assert(codingKeysEnum && "Missing CodingKeys decl.");
// Generate a reference to containerExpr ahead of time in case there are no
// properties to encode or decode, but the type is a class which inherits from
// something Codable and needs to decode super.
// let container : KeyedDecodingContainer<CodingKeys>
auto codingKeysType = codingKeysEnum->getDeclaredType();
auto *containerDecl = createKeyedContainer(C, funcDC,
C.getKeyedDecodingContainerDecl(),
codingKeysType,
VarDecl::Specifier::Let);
auto *containerExpr = new (C) DeclRefExpr(ConcreteDeclRef(containerDecl),
DeclNameLoc(), /*Implicit=*/true,
AccessSemantics::DirectToStorage);
SmallVector<ASTNode, 5> statements;
auto enumElements = codingKeysEnum->getAllElements();
if (!enumElements.empty()) {
// Need to generate
// `let container = try decoder.container(keyedBy: CodingKeys.self)`
// `let container` (containerExpr) is generated above.
// decoder
auto decoderParam = initDecl->getParameters()->get(0);
auto *decoderExpr = new (C) DeclRefExpr(ConcreteDeclRef(decoderParam),
DeclNameLoc(), /*Implicit=*/true);
// Bound decoder.container(keyedBy: CodingKeys.self) call
auto containerType = containerDecl->getInterfaceType();
auto *callExpr = createContainerKeyedByCall(C, funcDC, decoderExpr,
containerType, codingKeysEnum);
// try decoder.container(keyedBy: CodingKeys.self)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*implicit=*/true);
// Full `let container = decoder.container(keyedBy: CodingKeys.self)`
// binding.
auto *containerPattern = new (C) NamedPattern(containerDecl,
/*implicit=*/true);
auto *bindingDecl = PatternBindingDecl::createImplicit(
C, StaticSpellingKind::None, containerPattern, tryExpr, funcDC);
statements.push_back(bindingDecl);
statements.push_back(containerDecl);
// Now need to generate `x = try container.decode(Type.self, forKey: .x)`
// for all existing properties. Optional properties get `decodeIfPresent`.
for (auto *elt : enumElements) {
VarDecl *varDecl;
for (auto decl : targetDecl->lookupDirect(DeclName(elt->getName())))
if ((varDecl = dyn_cast<VarDecl>(decl)))
break;
// Don't output a decode statement for a var let with a default value.
if (varDecl->isLet() && varDecl->getParentInitializer() != nullptr)
continue;
// Potentially unwrap a layer of optionality from the var type. If the var
// is Optional<T>, we want to decodeIfPresent(T.self, forKey: ...);
// otherwise, we can just decode(T.self, forKey: ...).
// This is also true if the type is an ImplicitlyUnwrappedOptional.
auto varType = conformanceDC->mapTypeIntoContext(
varDecl->getInterfaceType());
auto methodName = C.Id_decode;
if (auto referenceType = varType->getAs<ReferenceStorageType>()) {
// This is a weak/unowned/unmanaged var. Get the inner type before
// checking optionality.
varType = referenceType->getReferentType();
}
if (varType->getAnyNominal() == C.getOptionalDecl()) {
methodName = C.Id_decodeIfPresent;
// The type we request out of decodeIfPresent needs to be unwrapped
// one level.
// e.g. String? => decodeIfPresent(String.self, forKey: ...), not
// decodeIfPresent(String?.self, forKey: ...)
auto boundOptionalType =
dyn_cast<BoundGenericType>(varType->getCanonicalType());
varType = boundOptionalType->getGenericArgs()[0];
}
// Type.self (where Type === type(of: x))
// Calculating the metatype needs to happen after potential Optional
// unwrapping above.
auto *metaTyRef = TypeExpr::createImplicit(varType, C);
auto *targetExpr = new (C) DotSelfExpr(metaTyRef, SourceLoc(),
SourceLoc(), varType);
// CodingKeys.x
auto *eltRef = new (C) DeclRefExpr(elt, DeclNameLoc(), /*implicit=*/true);
metaTyRef = TypeExpr::createImplicit(codingKeysType, C);
auto *keyExpr = new (C) DotSyntaxCallExpr(eltRef, SourceLoc(), metaTyRef);
// decode(_:forKey:)/decodeIfPresent(_:forKey:)
SmallVector<Identifier, 2> argNames{Identifier(), C.Id_forKey};
DeclName name(C, methodName, argNames);
auto *decodeCall = new (C) UnresolvedDotExpr(containerExpr, SourceLoc(),
name, DeclNameLoc(),
/*Implicit=*/true);
// container.decode(Type.self, forKey: CodingKeys.x)
Expr *args[2] = {targetExpr, keyExpr};
auto *callExpr = CallExpr::createImplicit(C, decodeCall,
C.AllocateCopy(args),
C.AllocateCopy(argNames));
// try container.decode(Type.self, forKey: CodingKeys.x)
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
auto *selfRef = DerivedConformance::createSelfDeclRef(initDecl);
auto *varExpr = new (C) UnresolvedDotExpr(selfRef, SourceLoc(),
DeclName(varDecl->getName()),
DeclNameLoc(),
/*implicit=*/true);
auto *assignExpr = new (C) AssignExpr(varExpr, SourceLoc(), tryExpr,
/*Implicit=*/true);
statements.push_back(assignExpr);
}
}
// Classes which have a superclass must call super.init(from:) if the
// superclass is Decodable, or super.init() if it is not.
if (auto *classDecl = dyn_cast<ClassDecl>(targetDecl)) {
if (auto *superclassDecl = classDecl->getSuperclassDecl()) {
if (superclassIsDecodable(classDecl)) {
// Need to generate `try super.init(from: container.superDecoder())`
// container.superDecoder
auto *superDecoderRef =
new (C) UnresolvedDotExpr(containerExpr, SourceLoc(),
DeclName(C.Id_superDecoder),
DeclNameLoc(), /*Implicit=*/true);
// container.superDecoder()
auto *superDecoderCall =
CallExpr::createImplicit(C, superDecoderRef, ArrayRef<Expr *>(),
ArrayRef<Identifier>());
// super
auto *superRef = new (C) SuperRefExpr(initDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.init(from:)
auto initName = DeclName(C, DeclBaseName::createConstructor(), C.Id_from);
auto *initCall = new (C) UnresolvedDotExpr(superRef, SourceLoc(),
initName, DeclNameLoc(),
/*Implicit=*/true);
// super.decode(from: container.superDecoder())
Expr *args[1] = {superDecoderCall};
Identifier argLabels[1] = {C.Id_from};
auto *callExpr = CallExpr::createImplicit(C, initCall,
C.AllocateCopy(args),
C.AllocateCopy(argLabels));
// try super.init(from: container.superDecoder())
auto *tryExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(tryExpr);
} else {
// The explicit constructor name is a compound name taking no arguments.
DeclName initName(C, DeclBaseName::createConstructor(), ArrayRef<Identifier>());
// We need to look this up in the superclass to see if it throws.
auto result = superclassDecl->lookupDirect(initName);
// We should have bailed one level up if this were not available.
assert(!result.empty());
// If the init is failable, we should have already bailed one level
// above.
ConstructorDecl *superInitDecl = cast<ConstructorDecl>(result.front());
assert(superInitDecl->getFailability() == OTK_None);
// super
auto *superRef = new (C) SuperRefExpr(initDecl->getImplicitSelfDecl(),
SourceLoc(), /*Implicit=*/true);
// super.init()
auto *superInitRef = new (C) UnresolvedDotExpr(superRef, SourceLoc(),
initName, DeclNameLoc(),
/*Implicit=*/true);
// super.init() call
Expr *callExpr = CallExpr::createImplicit(C, superInitRef,
ArrayRef<Expr *>(),
ArrayRef<Identifier>());
// If super.init throws, try super.init()
if (superInitDecl->hasThrows())
callExpr = new (C) TryExpr(SourceLoc(), callExpr, Type(),
/*Implicit=*/true);
statements.push_back(callExpr);
}
}
}
auto *body = BraceStmt::create(C, SourceLoc(), statements, SourceLoc(),
/*implicit=*/true);
initDecl->setBody(body);
}
/// Synthesizes a function declaration for `init(from: Decoder) throws` with a
/// lazily synthesized body for the given type.
///
/// Adds the function declaration to the given type before returning it.
static ValueDecl *deriveDecodable_init(DerivedConformance &derived) {
auto &C = derived.TC.Context;
auto classDecl = dyn_cast<ClassDecl>(derived.Nominal);
auto conformanceDC = derived.getConformanceContext();
// Expected type: (Self) -> (Decoder) throws -> (Self)
// Constructed as: func type