Last active
September 21, 2018 19:09
-
-
Save rockbruno/410a4ce94383b27f6d48aaaeb12b0b4b to your computer and use it in GitHub Desktop.
CaseIterable + first property
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ValueDecl *DerivedConformance::deriveCaseIterable(ValueDecl *requirement) { | |
// Conformance can't be synthesized in an extension. | |
if (checkAndDiagnoseDisallowedContext(requirement)) | |
return nullptr; | |
// Check that we can actually derive CaseIterable for this type. | |
if (!canDeriveConformance(Nominal)) | |
return nullptr; | |
Type returnTy; | |
Identifier propertyId; | |
if (requirement->getBaseName() == TC.Context.Id_allCases) { | |
returnTy = computeAllCasesType(Nominal); | |
propertyId = TC.Context.Id_allCases; | |
} else if (requirement->getBaseName() == TC.Context.Id_first) { | |
returnTy = Nominal->getDeclaredInterfaceType(); | |
propertyId = TC.Context.Id_first; | |
} else { | |
TC.diagnose(requirement->getLoc(), diag::broken_case_iterable_requirement); | |
return nullptr; | |
} | |
VarDecl *propDecl; | |
PatternBindingDecl *pbDecl; | |
std::tie(propDecl, pbDecl) = | |
declareDerivedProperty(propertyId, returnTy, returnTy, | |
/*isStatic=*/true, /*isFinal=*/true); | |
if (requirement->getBaseName() == TC.Context.Id_allCases) { | |
getterDecl->setBodySynthesizer(&deriveCaseIterable_enum_getter); | |
} else { | |
getterDecl->setBodySynthesizer(&deriveCaseIterable_first); | |
} | |
addMembersToConformanceContext({getterDecl, propDecl, pbDecl}); | |
return propDecl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment