@@ -466,92 +466,6 @@ lookupVisibleMemberDeclsImpl(Type BaseTy, VisibleDeclConsumer &Consumer,
466
466
GenericSignatureBuilder *GSB,
467
467
VisitedSet &Visited);
468
468
469
- // Filters out restated declarations from a protocol hierarchy
470
- // or equivalent requirements from protocol composition types.
471
- class RestateFilteringConsumer : public VisibleDeclConsumer {
472
- LazyResolver *resolver;
473
-
474
- using FoundDecl = std::pair<ValueDecl*, DeclVisibilityKind>;
475
- using NameAndType = std::pair<DeclName, CanType>;
476
-
477
- llvm::DenseMap<DeclName, FoundDecl> foundVars;
478
- llvm::DenseMap<NameAndType, FoundDecl> foundFuncs;
479
- llvm::MapVector<ValueDecl*, DeclVisibilityKind> declsToReport;
480
-
481
- template <typename K>
482
- void addDecl (llvm::DenseMap<K, FoundDecl> &Map, K Key, FoundDecl FD) {
483
- // Add the declaration if we haven't found an equivalent yet, otherwise
484
- // replace the equivalent if the found decl has a higher access level.
485
- auto existingDecl = Map.find (Key);
486
-
487
- if ((existingDecl == Map.end ()) ||
488
- (Map[Key].first ->getFormalAccess () < FD.first ->getFormalAccess ())) {
489
- if (existingDecl != Map.end ())
490
- declsToReport.erase ({existingDecl->getSecond ().first });
491
- Map[Key] = FD;
492
- declsToReport.insert (FD);
493
- }
494
- }
495
-
496
- CanType stripSelfRequirementsIfNeeded (ValueDecl *VD,
497
- GenericFunctionType *GFT) const {
498
- // Preserve the generic signature if this is a subscript, which are uncurried,
499
- // or if we have generic params other than Self. Otherwise, use
500
- // the resultType of the curried function type.
501
- // When we keep the generic signature, we remove the requirements
502
- // from Self to make sure they don't prevent us from recognizing restatements.
503
- auto params = GFT->getGenericParams ();
504
- if (params.size () == 1 && !isa<SubscriptDecl>(VD)) {
505
- return GFT->getResult ()->getCanonicalType ();
506
- }
507
- auto Self = VD->getDeclContext ()->getSelfInterfaceType ();
508
- SmallVector<Requirement, 4 > newReqs;
509
- for (auto req: GFT->getRequirements ()) {
510
- if (!Self->isEqual (req.getFirstType ()))
511
- newReqs.push_back (req);
512
- }
513
- auto newSig = GenericSignature::get (params, newReqs, false );
514
-
515
- return GenericFunctionType::get (newSig, GFT->getParams (),
516
- GFT->getResult (), GFT->getExtInfo ())
517
- ->getCanonicalType ();
518
- }
519
-
520
- public:
521
- RestateFilteringConsumer (Type baseTy, const DeclContext *DC,
522
- LazyResolver *resolver)
523
- : resolver(resolver) {
524
- assert (DC && baseTy && !baseTy->hasLValueType ());
525
- }
526
-
527
- void foundDecl (ValueDecl *VD, DeclVisibilityKind Reason) override {
528
- assert (VD);
529
- // If this isn't a protocol context, don't look further into the decl.
530
- if (!isa<ProtocolDecl>(VD->getDeclContext ())) {
531
- declsToReport.insert ({VD, Reason});
532
- return ;
533
- }
534
- if (resolver)
535
- resolver->resolveDeclSignature (VD);
536
-
537
- if (!VD->hasInterfaceType ()) {
538
- declsToReport.insert ({VD, Reason});
539
- return ;
540
- }
541
- if (auto GFT = VD->getInterfaceType ()->getAs <GenericFunctionType>()) {
542
- auto type = stripSelfRequirementsIfNeeded (VD, GFT);
543
- addDecl (foundFuncs, {VD->getFullName (), type}, {VD, Reason});
544
- return ;
545
- }
546
- addDecl (foundVars, VD->getFullName (), {VD, Reason});
547
- }
548
-
549
- void feedResultsToConsumer (VisibleDeclConsumer &Consumer) const {
550
- for (const auto entry: declsToReport)
551
- Consumer.foundDecl (entry.first , entry.second );
552
- }
553
- };
554
-
555
469
static void
556
470
lookupVisibleProtocolMemberDecls (Type BaseTy, ProtocolType *PT,
557
471
VisibleDeclConsumer &Consumer,
@@ -637,12 +551,11 @@ static void lookupVisibleMemberDeclsImpl(
637
551
for (auto Proto : Archetype->getConformsTo ())
638
552
lookupVisibleProtocolMemberDecls (
639
553
BaseTy, Proto->getDeclaredType (), Consumer, CurrDC, LS,
640
- getReasonForSuper ( Reason) , TypeResolver, GSB, Visited);
554
+ Reason, TypeResolver, GSB, Visited);
641
555
642
556
if (auto superclass = Archetype->getSuperclass ())
643
557
lookupVisibleMemberDeclsImpl (superclass, Consumer, CurrDC, LS,
644
- getReasonForSuper (Reason), TypeResolver,
645
- GSB, Visited);
558
+ Reason, TypeResolver, GSB, Visited);
646
559
return ;
647
560
}
648
561
@@ -763,17 +676,6 @@ template <> struct DenseMapInfo<FoundDeclTy> {
763
676
764
677
namespace {
765
678
766
- // / Hack to guess at whether substituting into the type of a declaration will
767
- // / be okay.
768
- // / FIXME: This is awful. We should either have Type::subst() work for
769
- // / GenericFunctionType, or we should kill it outright.
770
- static bool shouldSubstIntoDeclType (Type type) {
771
- auto genericFnType = type->getAs <GenericFunctionType>();
772
- if (!genericFnType) return true ;
773
-
774
- return false ;
775
- }
776
-
777
679
class OverrideFilteringConsumer : public VisibleDeclConsumer {
778
680
public:
779
681
std::set<ValueDecl *> AllFoundDecls;
@@ -782,16 +684,12 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
782
684
Type BaseTy;
783
685
const DeclContext *DC;
784
686
LazyResolver *TypeResolver;
785
- bool IsTypeLookup = false ;
786
687
787
688
OverrideFilteringConsumer (Type BaseTy, const DeclContext *DC,
788
689
LazyResolver *resolver)
789
- : BaseTy(BaseTy), DC(DC), TypeResolver(resolver) {
690
+ : BaseTy(BaseTy->getMetatypeInstanceType ()),
691
+ DC(DC), TypeResolver(resolver) {
790
692
assert (!BaseTy->hasLValueType ());
791
- if (auto *MetaTy = BaseTy->getAs <AnyMetatypeType>()) {
792
- BaseTy = MetaTy->getInstanceType ();
793
- IsTypeLookup = true ;
794
- }
795
693
assert (DC && BaseTy);
796
694
}
797
695
@@ -801,7 +699,9 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
801
699
802
700
// If this kind of declaration doesn't participate in overriding, there's
803
701
// no filtering to do here.
804
- if (!isa<AbstractFunctionDecl>(VD) && !isa<AbstractStorageDecl>(VD)) {
702
+ if (!isa<AbstractFunctionDecl>(VD) &&
703
+ !isa<AbstractStorageDecl>(VD) &&
704
+ !isa<AssociatedTypeDecl>(VD)) {
805
705
DeclsToReport.insert (FoundDeclTy (VD, Reason));
806
706
return ;
807
707
}
@@ -852,7 +752,8 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
852
752
// don't substitute either.
853
753
bool shouldSubst = (!BaseTy->isAnyObject () &&
854
754
!BaseTy->hasTypeVariable () &&
855
- BaseTy->getNominalOrBoundGenericNominal () &&
755
+ (BaseTy->getNominalOrBoundGenericNominal () ||
756
+ BaseTy->is <ArchetypeType>()) &&
856
757
VD->getDeclContext ()->isTypeContext ());
857
758
ModuleDecl *M = DC->getParentModule ();
858
759
@@ -865,8 +766,7 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
865
766
866
767
auto FoundSignature = VD->getOverloadSignature ();
867
768
auto FoundSignatureType = VD->getOverloadSignatureType ();
868
- if (FoundSignatureType && shouldSubst &&
869
- shouldSubstIntoDeclType (FoundSignatureType)) {
769
+ if (FoundSignatureType && shouldSubst) {
870
770
auto subs = BaseTy->getMemberSubstitutionMap (M, VD);
871
771
if (auto CT = FoundSignatureType.subst (subs))
872
772
FoundSignatureType = CT->getCanonicalType ();
@@ -883,8 +783,7 @@ class OverrideFilteringConsumer : public VisibleDeclConsumer {
883
783
884
784
auto OtherSignature = OtherVD->getOverloadSignature ();
885
785
auto OtherSignatureType = OtherVD->getOverloadSignatureType ();
886
- if (OtherSignatureType && shouldSubst &&
887
- shouldSubstIntoDeclType (OtherSignatureType)) {
786
+ if (OtherSignatureType && shouldSubst) {
888
787
auto subs = BaseTy->getMemberSubstitutionMap (M, OtherVD);
889
788
if (auto CT = OtherSignatureType.subst (subs))
890
789
OtherSignatureType = CT->getCanonicalType ();
@@ -926,13 +825,11 @@ static void lookupVisibleMemberDecls(
926
825
LookupState LS, DeclVisibilityKind Reason, LazyResolver *TypeResolver,
927
826
GenericSignatureBuilder *GSB) {
928
827
OverrideFilteringConsumer overrideConsumer (BaseTy, CurrDC, TypeResolver);
929
- RestateFilteringConsumer restateConsumer (BaseTy, CurrDC, TypeResolver);
930
828
VisitedSet Visited;
931
- lookupVisibleMemberDeclsImpl (BaseTy, restateConsumer , CurrDC, LS, Reason,
829
+ lookupVisibleMemberDeclsImpl (BaseTy, overrideConsumer , CurrDC, LS, Reason,
932
830
TypeResolver, GSB, Visited);
933
831
934
832
// Report the declarations we found to the real consumer.
935
- restateConsumer.feedResultsToConsumer (overrideConsumer);
936
833
for (const auto &DeclAndReason : overrideConsumer.DeclsToReport )
937
834
Consumer.foundDecl (DeclAndReason.D , DeclAndReason.Reason );
938
835
}
0 commit comments