Add Missing Definitions of canDerive{En,De}codable

This commit is contained in:
Robert Widmann
2021-02-19 16:03:32 -08:00
parent 55fd87900a
commit 6df3fcf3ad
3 changed files with 45 additions and 25 deletions

View File

@@ -1071,10 +1071,35 @@ static bool canSynthesize(DerivedConformance &derived, ValueDecl *requirement) {
LLVM_FALLTHROUGH;
case CodingKeysClassification::Valid:
return true;
static bool canDeriveCodable(NominalTypeDecl *NTD,
KnownProtocolKind Kind) {
assert(Kind == KnownProtocolKind::Encodable ||
Kind == KnownProtocolKind::Decodable);
// Structs and classes can explicitly derive Encodable and Decodable
// conformance (explicitly meaning we can synthesize an implementation if
// a type conforms manually).
// FIXME: Enums too!
if (!isa<StructDecl>(NTD) && !isa<ClassDecl>(NTD)) {
return false;
}
auto *PD = NTD->getASTContext().getProtocol(Kind);
if (!PD) {
return false;
}
return true;
}
bool DerivedConformance::canDeriveDecodable(NominalTypeDecl *NTD) {
return canDeriveCodable(NTD, KnownProtocolKind::Decodable);
}
bool DerivedConformance::canDeriveEncodable(NominalTypeDecl *NTD) {
return canDeriveCodable(NTD, KnownProtocolKind::Encodable);
}
ValueDecl *DerivedConformance::deriveEncodable(ValueDecl *requirement) {
// We can only synthesize Encodable for structs and classes.
if (!isa<StructDecl>(Nominal) && !isa<ClassDecl>(Nominal))