@@ -421,60 +421,37 @@ synthesizeStubBody(AbstractFunctionDecl *fn, void *) {
421
421
/* isTypeChecked=*/ true };
422
422
}
423
423
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) {
437
431
auto *genericParams = superclassCtor->getGenericParams ();
438
432
439
433
// If genericParams is non-null, the base class initializer has its own
440
434
// generic parameters. Otherwise, it is non-generic with a contextual
441
435
// '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 ;
458
438
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);
467
450
}
468
451
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 ());
478
455
}
479
456
480
457
static void
@@ -651,22 +628,25 @@ createDesignatedInitOverride(ClassDecl *classDecl,
651
628
//
652
629
// FIXME: Remove this when lookup of initializers becomes restricted to our
653
630
// 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)
659
633
return nullptr ;
660
- }
661
634
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);
667
647
668
- if (auto superclassCtorSig = superclassCtor-> getGenericSignature () ) {
669
- auto *genericEnv = overrideInfo. GenericSig .getGenericEnvironment ();
648
+ if (superclassCtorSig) {
649
+ auto *genericEnv = genericSig .getGenericEnvironment ();
670
650
671
651
// If the base class initializer has a 'where' clause, it might impose
672
652
// requirements on the base class's own generic parameters that are not
@@ -676,15 +656,15 @@ createDesignatedInitOverride(ClassDecl *classDecl,
676
656
classDecl->getParentModule (),
677
657
superclassCtorSig.getRequirements (),
678
658
[&](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);
682
661
});
683
662
if (checkResult != CheckGenericArgumentsResult::Success)
684
663
return nullptr ;
685
664
}
686
665
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.
688
668
OptionSet<ParameterList::CloneFlags> options
689
669
= (ParameterList::Implicit |
690
670
ParameterList::Inherited |
@@ -702,7 +682,7 @@ createDesignatedInitOverride(ClassDecl *classDecl,
702
682
auto *bodyParam = bodyParams->get (idx);
703
683
704
684
auto paramTy = superclassParam->getInterfaceType ();
705
- auto substTy = paramTy.subst (overrideInfo. OverrideSubMap );
685
+ auto substTy = paramTy.subst (subMap );
706
686
707
687
bodyParam->setInterfaceType (substTy);
708
688
}
@@ -718,13 +698,13 @@ createDesignatedInitOverride(ClassDecl *classDecl,
718
698
/* AsyncLoc=*/ SourceLoc (),
719
699
/* Throws=*/ superclassCtor->hasThrows (),
720
700
/* ThrowsLoc=*/ SourceLoc (),
721
- bodyParams, overrideInfo. GenericParams ,
701
+ bodyParams, genericParams ,
722
702
classDecl);
723
703
724
704
ctor->setImplicit ();
725
705
726
706
// Set the interface type of the initializer.
727
- ctor->setGenericSignature (overrideInfo. GenericSig );
707
+ ctor->setGenericSignature (genericSig );
728
708
729
709
ctor->setImplicitlyUnwrappedOptional (
730
710
superclassCtor->isImplicitlyUnwrappedOptional ());
0 commit comments