Skip to content

Instantly share code, notes, and snippets.

@rockbruno
Last active September 21, 2018 19:09
Show Gist options
  • Save rockbruno/410a4ce94383b27f6d48aaaeb12b0b4b to your computer and use it in GitHub Desktop.
Save rockbruno/410a4ce94383b27f6d48aaaeb12b0b4b to your computer and use it in GitHub Desktop.
CaseIterable + first property
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