Skip to content

Commit c236c30

Browse files
committed
Sema: Refactor away computeDesignatedInitOverrideSignature()
1 parent 2dfb7dc commit c236c30

File tree

1 file changed

+46
-66
lines changed

1 file changed

+46
-66
lines changed

lib/Sema/CodeSynthesis.cpp

Lines changed: 46 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -421,60 +421,37 @@ synthesizeStubBody(AbstractFunctionDecl *fn, void *) {
421421
/*isTypeChecked=*/true };
422422
}
423423

424-
namespace {
425-
struct DesignatedInitOverrideInfo {
426-
GenericSignature GenericSig;
427-
GenericParamList *GenericParams;
428-
SubstitutionMap OverrideSubMap;
429-
};
430-
}
431-
432-
static DesignatedInitOverrideInfo
433-
computeDesignatedInitOverrideSignature(ASTContext &ctx,
434-
ClassDecl *classDecl,
435-
Type superclassTy,
436-
ConstructorDecl *superclassCtor) {
424+
/// Clone the base class initializer's generic parameter list, but change the
425+
/// depth of the generic parameters to be one greater than the depth of the
426+
/// subclass.
427+
static GenericParamList *
428+
createDesignatedInitOverrideGenericParams(ASTContext &ctx,
429+
ClassDecl *classDecl,
430+
ConstructorDecl *superclassCtor) {
437431
auto *genericParams = superclassCtor->getGenericParams();
438432

439433
// If genericParams is non-null, the base class initializer has its own
440434
// generic parameters. Otherwise, it is non-generic with a contextual
441435
// 'where' clause.
442-
if (genericParams) {
443-
// First, clone the base class initializer's generic parameter list,
444-
// but change the depth of the generic parameters to be one greater
445-
// than the depth of the subclass.
446-
unsigned depth = 0;
447-
if (auto classSig = classDecl->getGenericSignature())
448-
depth = classSig.getGenericParams().back()->getDepth() + 1;
449-
450-
SmallVector<GenericTypeParamDecl *, 4> newParams;
451-
for (auto *param : genericParams->getParams()) {
452-
auto *newParam = GenericTypeParamDecl::create(
453-
classDecl, param->getName(), SourceLoc(), param->isTypeSequence(),
454-
depth, param->getIndex(), param->isOpaqueType(),
455-
/*opaqueTypeRepr=*/nullptr);
456-
newParams.push_back(newParam);
457-
}
436+
if (genericParams == nullptr)
437+
return nullptr;
458438

459-
// We don't have to clone the RequirementReprs, because they're not
460-
// used for anything other than SIL mode.
461-
genericParams = GenericParamList::create(ctx,
462-
SourceLoc(),
463-
newParams,
464-
SourceLoc(),
465-
ArrayRef<RequirementRepr>(),
466-
SourceLoc());
439+
unsigned depth = 0;
440+
if (auto classSig = classDecl->getGenericSignature())
441+
depth = classSig.getGenericParams().back()->getDepth() + 1;
442+
443+
SmallVector<GenericTypeParamDecl *, 4> newParams;
444+
for (auto *param : genericParams->getParams()) {
445+
auto *newParam = GenericTypeParamDecl::create(
446+
classDecl, param->getName(), SourceLoc(), param->isTypeSequence(),
447+
depth, param->getIndex(), param->isOpaqueType(),
448+
/*opaqueTypeRepr=*/nullptr);
449+
newParams.push_back(newParam);
467450
}
468451

469-
auto *superclassDecl = superclassTy->getClassOrBoundGenericClass();
470-
auto superclassCtorSig = superclassCtor->getGenericSignature();
471-
472-
auto subMap = SubstitutionMap::getOverrideSubstitutions(
473-
superclassDecl, classDecl, superclassCtorSig, genericParams);
474-
auto genericSig = ctx.getOverrideGenericSignature(
475-
superclassDecl, classDecl, superclassCtorSig, genericParams);
476-
477-
return DesignatedInitOverrideInfo{genericSig, genericParams, subMap};
452+
return GenericParamList::create(ctx, SourceLoc(),
453+
newParams, SourceLoc(),
454+
ArrayRef<RequirementRepr>(), SourceLoc());
478455
}
479456

480457
static void
@@ -651,22 +628,25 @@ createDesignatedInitOverride(ClassDecl *classDecl,
651628
//
652629
// FIXME: Remove this when lookup of initializers becomes restricted to our
653630
// immediate superclass.
654-
auto *superclassCtorDecl =
655-
superclassCtor->getDeclContext()->getSelfNominalTypeDecl();
656-
Type superclassTy = classDecl->getSuperclass();
657-
NominalTypeDecl *superclassDecl = superclassTy->getAnyNominal();
658-
if (superclassCtorDecl != superclassDecl) {
631+
auto *superclassDecl = superclassCtor->getDeclContext()->getSelfClassDecl();
632+
if (classDecl->getSuperclassDecl() != superclassDecl)
659633
return nullptr;
660-
}
661634

662-
auto overrideInfo =
663-
computeDesignatedInitOverrideSignature(ctx,
664-
classDecl,
665-
superclassTy,
666-
superclassCtor);
635+
auto *genericParams = createDesignatedInitOverrideGenericParams(
636+
ctx, classDecl, superclassCtor);
637+
638+
auto superclassCtorSig = superclassCtor->getGenericSignature();
639+
640+
// Compute a generic signature for the initializer, and a substitution map
641+
// from the superclass initializer signature to the initializer generic
642+
// signature.
643+
auto subMap = SubstitutionMap::getOverrideSubstitutions(
644+
superclassDecl, classDecl, superclassCtorSig, genericParams);
645+
auto genericSig = ctx.getOverrideGenericSignature(
646+
superclassDecl, classDecl, superclassCtorSig, genericParams);
667647

668-
if (auto superclassCtorSig = superclassCtor->getGenericSignature()) {
669-
auto *genericEnv = overrideInfo.GenericSig.getGenericEnvironment();
648+
if (superclassCtorSig) {
649+
auto *genericEnv = genericSig.getGenericEnvironment();
670650

671651
// If the base class initializer has a 'where' clause, it might impose
672652
// requirements on the base class's own generic parameters that are not
@@ -676,15 +656,15 @@ createDesignatedInitOverride(ClassDecl *classDecl,
676656
classDecl->getParentModule(),
677657
superclassCtorSig.getRequirements(),
678658
[&](Type type) -> Type {
679-
auto substType = type.subst(overrideInfo.OverrideSubMap);
680-
return GenericEnvironment::mapTypeIntoContext(
681-
genericEnv, substType);
659+
auto substType = type.subst(subMap);
660+
return GenericEnvironment::mapTypeIntoContext(genericEnv, substType);
682661
});
683662
if (checkResult != CheckGenericArgumentsResult::Success)
684663
return nullptr;
685664
}
686665

687-
// Create the initializer parameter patterns.
666+
// Create the initializer parameter list by cloning the superclass initializer
667+
// parameter list and applying the substitution map.
688668
OptionSet<ParameterList::CloneFlags> options
689669
= (ParameterList::Implicit |
690670
ParameterList::Inherited |
@@ -702,7 +682,7 @@ createDesignatedInitOverride(ClassDecl *classDecl,
702682
auto *bodyParam = bodyParams->get(idx);
703683

704684
auto paramTy = superclassParam->getInterfaceType();
705-
auto substTy = paramTy.subst(overrideInfo.OverrideSubMap);
685+
auto substTy = paramTy.subst(subMap);
706686

707687
bodyParam->setInterfaceType(substTy);
708688
}
@@ -718,13 +698,13 @@ createDesignatedInitOverride(ClassDecl *classDecl,
718698
/*AsyncLoc=*/SourceLoc(),
719699
/*Throws=*/superclassCtor->hasThrows(),
720700
/*ThrowsLoc=*/SourceLoc(),
721-
bodyParams, overrideInfo.GenericParams,
701+
bodyParams, genericParams,
722702
classDecl);
723703

724704
ctor->setImplicit();
725705

726706
// Set the interface type of the initializer.
727-
ctor->setGenericSignature(overrideInfo.GenericSig);
707+
ctor->setGenericSignature(genericSig);
728708

729709
ctor->setImplicitlyUnwrappedOptional(
730710
superclassCtor->isImplicitlyUnwrappedOptional());

0 commit comments

Comments
 (0)