19
19
#include " clang/AST/DeclObjC.h"
20
20
#include " swift/AST/ASTContext.h"
21
21
#include " swift/AST/ClangModuleLoader.h"
22
+ #include " swift/AST/GenericEnvironment.h"
22
23
#include " swift/AST/GenericSignature.h"
23
24
#include " swift/AST/ImportCache.h"
24
25
#include " swift/AST/Initializer.h"
@@ -238,15 +239,13 @@ static void collectVisibleMemberDecls(const DeclContext *CurrDC, LookupState LS,
238
239
// / Lookup members in extensions of \p LookupType, using \p BaseType as the
239
240
// / underlying type when checking any constraints on the extensions.
240
241
static void doGlobalExtensionLookup (Type BaseType,
241
- Type LookupType,
242
+ NominalTypeDecl * LookupType,
242
243
SmallVectorImpl<ValueDecl *> &FoundDecls,
243
244
const DeclContext *CurrDC,
244
245
LookupState LS,
245
246
DeclVisibilityKind Reason) {
246
- auto nominal = LookupType->getAnyNominal ();
247
-
248
247
// Look in each extension of this type.
249
- for (auto extension : nominal ->getExtensions ()) {
248
+ for (auto extension : LookupType ->getExtensions ()) {
250
249
if (!evaluateOrDefault (CurrDC->getASTContext ().evaluator ,
251
250
IsDeclApplicableRequest (DeclApplicabilityOwner (CurrDC, BaseType,
252
251
extension)), false ))
@@ -265,17 +264,17 @@ static void doGlobalExtensionLookup(Type BaseType,
265
264
// / Don't do lookup into superclasses or implemented protocols. Uses
266
265
// / \p BaseType as the underlying type when checking any constraints on the
267
266
// / extensions.
268
- static void lookupTypeMembers (Type BaseType, Type LookupType,
267
+ static void lookupTypeMembers (Type BaseType, NominalTypeDecl * LookupType,
269
268
VisibleDeclConsumer &Consumer,
270
269
const DeclContext *CurrDC, LookupState LS,
271
270
DeclVisibilityKind Reason) {
272
- NominalTypeDecl *D = LookupType-> getAnyNominal ( );
273
- assert (D && " should have a nominal type" );
271
+ assert (!BaseType-> hasTypeParameter () );
272
+ assert (LookupType && " should have a nominal type" );
274
273
275
- Consumer.onLookupNominalTypeMembers (D , Reason);
274
+ Consumer.onLookupNominalTypeMembers (LookupType , Reason);
276
275
277
276
SmallVector<ValueDecl*, 2 > FoundDecls;
278
- collectVisibleMemberDecls (CurrDC, LS, BaseType, D , FoundDecls);
277
+ collectVisibleMemberDecls (CurrDC, LS, BaseType, LookupType , FoundDecls);
279
278
280
279
doGlobalExtensionLookup (BaseType, LookupType, FoundDecls, CurrDC, LS, Reason);
281
280
@@ -527,7 +526,7 @@ static void lookupDeclsFromProtocolsBeingConformedTo(
527
526
// Add members from any extensions.
528
527
if (LS.isIncludingProtocolExtensionMembers ()) {
529
528
SmallVector<ValueDecl *, 2 > FoundDecls;
530
- doGlobalExtensionLookup (BaseTy, Proto-> getDeclaredInterfaceType () ,
529
+ doGlobalExtensionLookup (BaseTy, Proto,
531
530
FoundDecls, FromContext, LS,
532
531
ReasonForThisProtocol);
533
532
for (auto *VD : FoundDecls)
@@ -537,29 +536,20 @@ static void lookupDeclsFromProtocolsBeingConformedTo(
537
536
}
538
537
539
538
static void
540
- lookupVisibleMemberDeclsImpl (Type BaseTy, VisibleDeclConsumer &Consumer,
541
- const DeclContext *CurrDC, LookupState LS,
542
- DeclVisibilityKind Reason,
543
- GenericSignature Sig,
544
- VisitedSet &Visited);
545
-
546
- static void
547
- lookupVisibleProtocolMemberDecls (Type BaseTy, const ProtocolDecl *PD,
539
+ lookupVisibleProtocolMemberDecls (Type BaseTy, ProtocolDecl *PD,
548
540
VisibleDeclConsumer &Consumer,
549
541
const DeclContext *CurrDC, LookupState LS,
550
542
DeclVisibilityKind Reason,
551
- GenericSignature Sig,
552
543
VisitedSet &Visited) {
553
544
if (!Visited.insert (PD).second )
554
545
return ;
555
546
556
- lookupTypeMembers (BaseTy, PD->getDeclaredInterfaceType (), Consumer, CurrDC,
557
- LS, Reason);
547
+ lookupTypeMembers (BaseTy, PD, Consumer, CurrDC, LS, Reason);
558
548
559
549
// Collect members from the inherited protocols.
560
550
for (auto Proto : PD->getInheritedProtocols ())
561
551
lookupVisibleProtocolMemberDecls (BaseTy, Proto, Consumer, CurrDC, LS,
562
- getReasonForSuper (Reason), Sig, Visited);
552
+ getReasonForSuper (Reason), Visited);
563
553
}
564
554
565
555
static void lookupVisibleCxxNamespaceMemberDecls (
@@ -621,10 +611,10 @@ static void lookupVisibleCxxNamespaceMemberDecls(
621
611
622
612
static void lookupVisibleMemberDeclsImpl (
623
613
Type BaseTy, VisibleDeclConsumer &Consumer, const DeclContext *CurrDC,
624
- LookupState LS, DeclVisibilityKind Reason, GenericSignature Sig,
625
- VisitedSet &Visited) {
614
+ LookupState LS, DeclVisibilityKind Reason, VisitedSet &Visited) {
626
615
// Just look through l-valueness. It doesn't affect name lookup.
627
616
assert (BaseTy && " lookup into null type" );
617
+ assert (!BaseTy->hasTypeParameter ());
628
618
assert (!BaseTy->hasLValueType ());
629
619
630
620
// Handle metatype references, as in "some_type.some_member". These are
@@ -651,8 +641,7 @@ static void lookupVisibleMemberDeclsImpl(
651
641
// anything else. For example, type SomeTy.SomeMember can look up static
652
642
// functions, and can even look up non-static functions as well (thus
653
643
// getting the address of the member).
654
- lookupVisibleMemberDeclsImpl (Ty, Consumer, CurrDC, subLS, Reason,
655
- Sig, Visited);
644
+ lookupVisibleMemberDeclsImpl (Ty, Consumer, CurrDC, subLS, Reason, Visited);
656
645
return ;
657
646
}
658
647
@@ -675,23 +664,22 @@ static void lookupVisibleMemberDeclsImpl(
675
664
// If the base is a protocol, enumerate its members.
676
665
if (ProtocolType *PT = BaseTy->getAs <ProtocolType>()) {
677
666
lookupVisibleProtocolMemberDecls (BaseTy, PT->getDecl (),
678
- Consumer, CurrDC, LS, Reason,
679
- Sig, Visited);
667
+ Consumer, CurrDC, LS, Reason, Visited);
680
668
return ;
681
669
}
682
670
683
671
// If the base is a protocol composition, enumerate members of the protocols.
684
672
if (auto PC = BaseTy->getAs <ProtocolCompositionType>()) {
685
673
for (auto Member : PC->getMembers ())
686
674
lookupVisibleMemberDeclsImpl (Member, Consumer, CurrDC, LS, Reason,
687
- Sig, Visited);
675
+ Visited);
688
676
return ;
689
677
}
690
678
691
679
if (auto *existential = BaseTy->getAs <ExistentialType>()) {
692
680
auto constraint = existential->getConstraintType ();
693
681
lookupVisibleMemberDeclsImpl (constraint, Consumer, CurrDC, LS, Reason,
694
- Sig, Visited);
682
+ Visited);
695
683
return ;
696
684
}
697
685
@@ -700,11 +688,11 @@ static void lookupVisibleMemberDeclsImpl(
700
688
for (auto Proto : Archetype->getConformsTo ())
701
689
lookupVisibleProtocolMemberDecls (
702
690
BaseTy, Proto, Consumer, CurrDC, LS,
703
- Reason, Sig, Visited);
691
+ Reason, Visited);
704
692
705
693
if (auto superclass = Archetype->getSuperclass ())
706
694
lookupVisibleMemberDeclsImpl (superclass, Consumer, CurrDC, LS,
707
- Reason, Sig, Visited);
695
+ Reason, Visited);
708
696
return ;
709
697
}
710
698
@@ -718,88 +706,53 @@ static void lookupVisibleMemberDeclsImpl(
718
706
}
719
707
}
720
708
721
- // If we're looking into a type parameter and we have a GenericSignature,
722
- // query the signature to resolve where we should look.
723
- if (BaseTy->isTypeParameter () && Sig) {
724
- // The type might be fully concrete via a same-type requirement.
725
- if (auto ConcreteTy = Sig->getConcreteType (BaseTy)) {
726
- BaseTy = ConcreteTy;
727
- } else {
728
- // Look into protocols of conformance requirements
729
- for (const auto *Proto : Sig->getRequiredProtocols (BaseTy)) {
730
- lookupVisibleProtocolMemberDecls (
731
- BaseTy, Proto, Consumer, CurrDC,
732
- LS, getReasonForSuper (Reason), Sig, Visited);
733
- }
734
-
735
- // Look into the superclass requirement type, if there is one.
736
- if (auto SuperclassTy = Sig->getSuperclassBound (BaseTy)) {
737
- lookupVisibleMemberDeclsImpl (SuperclassTy, Consumer, CurrDC,
738
- LS, getReasonForSuper (Reason),
739
- Sig, Visited);
740
- }
741
-
742
- return ;
743
- }
744
- }
745
-
746
709
// The members of a dynamic 'Self' type are the members of its static
747
710
// class type.
748
711
if (auto *const DS = BaseTy->getAs <DynamicSelfType>()) {
749
712
BaseTy = DS->getSelfType ();
750
713
}
751
714
752
- auto lookupTy = BaseTy;
715
+ auto *NTD = BaseTy->getAnyNominal ();
716
+ if (NTD == nullptr )
717
+ return ;
753
718
754
- llvm::SmallPtrSet<ClassDecl *, 8 > Ancestors;
755
- {
756
- const auto NTD = BaseTy->getAnyNominal ();
757
- if (NTD == nullptr )
758
- return ;
719
+ lookupTypeMembers (BaseTy, NTD, Consumer, CurrDC, LS, Reason);
759
720
760
- lookupTypeMembers (BaseTy, lookupTy, Consumer, CurrDC, LS, Reason);
721
+ // Look into protocols only on the current nominal to avoid repeatedly
722
+ // visiting inherited conformances.
723
+ lookupDeclsFromProtocolsBeingConformedTo (BaseTy, Consumer, LS, CurrDC,
724
+ Reason, Visited);
761
725
762
- // Look into protocols only on the current nominal to avoid repeatedly
763
- // visiting inherited conformances.
764
- lookupDeclsFromProtocolsBeingConformedTo (BaseTy, Consumer, LS, CurrDC,
765
- Reason, Visited);
726
+ auto *CD = dyn_cast<ClassDecl>(NTD);
766
727
767
- const auto CD = dyn_cast<ClassDecl>(NTD);
728
+ if (!CD || !CD->hasSuperclass ())
729
+ return ;
768
730
769
- // FIXME: We check `getSuperclass()` here because we'll be using the
770
- // superclass Type below, and in ill-formed code `hasSuperclass()` could
771
- // be true while `getSuperclass()` returns null, because the latter
772
- // looks for a declaration.
773
- if (!CD || !CD->getSuperclass ())
774
- return ;
731
+ // We have a superclass; switch state and look into the inheritance chain.
732
+ llvm::SmallPtrSet<ClassDecl *, 8 > Ancestors;
733
+ Ancestors.insert (CD);
775
734
776
- // We have a superclass; switch state and look into the inheritance chain.
777
- Ancestors.insert (CD);
735
+ Reason = getReasonForSuper (Reason);
778
736
779
- Reason = getReasonForSuper (Reason);
780
- lookupTy = CD->getSuperclass ();
737
+ LS = LS.withOnSuperclass ();
738
+ if (CD->inheritsSuperclassInitializers ())
739
+ LS = LS.withInheritsSuperclassInitializers ();
781
740
782
- LS = LS.withOnSuperclass ();
783
- if (CD->inheritsSuperclassInitializers ())
784
- LS = LS.withInheritsSuperclassInitializers ();
785
- }
741
+ CD = CD->getSuperclassDecl ();
786
742
787
743
// Look into the inheritance chain.
788
744
do {
789
- const auto CurClass = lookupTy->getClassOrBoundGenericClass ();
790
-
791
745
// FIXME: This path is no substitute for an actual circularity check.
792
746
// The real fix is to check that the superclass doesn't introduce a
793
747
// circular reference before it's written into the AST.
794
- if (!Ancestors.insert (CurClass ).second )
748
+ if (!Ancestors.insert (CD ).second )
795
749
break ;
796
750
797
- lookupTypeMembers (BaseTy, lookupTy , Consumer, CurrDC, LS, Reason);
751
+ lookupTypeMembers (BaseTy, CD , Consumer, CurrDC, LS, Reason);
798
752
799
- lookupTy = CurClass->getSuperclass ();
800
- if (!CurClass->inheritsSuperclassInitializers ())
753
+ if (!CD->inheritsSuperclassInitializers ())
801
754
LS = LS.withoutInheritsSuperclassInitializers ();
802
- } while (lookupTy );
755
+ } while ((CD = CD-> getSuperclassDecl ()) );
803
756
}
804
757
805
758
swift::DynamicLookupInfo::DynamicLookupInfo (
@@ -1121,8 +1074,7 @@ struct KeyPathDynamicMemberConsumer : public VisibleDeclConsumer {
1121
1074
static void lookupVisibleDynamicMemberLookupDecls (
1122
1075
Type baseType, SourceLoc loc, KeyPathDynamicMemberConsumer &consumer,
1123
1076
const DeclContext *dc, LookupState LS, DeclVisibilityKind reason,
1124
- GenericSignature Sig, VisitedSet &visited,
1125
- llvm::DenseSet<TypeBase *> &seenDynamicLookup);
1077
+ VisitedSet &visited, llvm::DenseSet<TypeBase *> &seenDynamicLookup);
1126
1078
1127
1079
// / Enumerates all members of \c baseType, including both directly visible and
1128
1080
// / members visible by keypath dynamic member lookup.
@@ -1132,11 +1084,12 @@ static void lookupVisibleDynamicMemberLookupDecls(
1132
1084
static void lookupVisibleMemberAndDynamicMemberDecls (
1133
1085
Type baseType, SourceLoc loc, VisibleDeclConsumer &consumer,
1134
1086
KeyPathDynamicMemberConsumer &dynamicMemberConsumer, const DeclContext *DC,
1135
- LookupState LS, DeclVisibilityKind reason, GenericSignature Sig,
1087
+ LookupState LS, DeclVisibilityKind reason,
1136
1088
VisitedSet &visited, llvm::DenseSet<TypeBase *> &seenDynamicLookup) {
1137
- lookupVisibleMemberDeclsImpl (baseType, consumer, DC, LS, reason, Sig, visited);
1089
+
1090
+ lookupVisibleMemberDeclsImpl (baseType, consumer, DC, LS, reason, visited);
1138
1091
lookupVisibleDynamicMemberLookupDecls (baseType, loc, dynamicMemberConsumer,
1139
- DC, LS, reason, Sig, visited,
1092
+ DC, LS, reason, visited,
1140
1093
seenDynamicLookup);
1141
1094
}
1142
1095
@@ -1149,8 +1102,7 @@ static void lookupVisibleMemberAndDynamicMemberDecls(
1149
1102
static void lookupVisibleDynamicMemberLookupDecls (
1150
1103
Type baseType, SourceLoc loc, KeyPathDynamicMemberConsumer &consumer,
1151
1104
const DeclContext *dc, LookupState LS, DeclVisibilityKind reason,
1152
- GenericSignature Sig, VisitedSet &visited,
1153
- llvm::DenseSet<TypeBase *> &seenDynamicLookup) {
1105
+ VisitedSet &visited, llvm::DenseSet<TypeBase *> &seenDynamicLookup) {
1154
1106
if (!seenDynamicLookup.insert (baseType.getPointer ()).second )
1155
1107
return ;
1156
1108
@@ -1187,7 +1139,7 @@ static void lookupVisibleDynamicMemberLookupDecls(
1187
1139
baseType);
1188
1140
1189
1141
lookupVisibleMemberAndDynamicMemberDecls (memberType, loc, consumer,
1190
- consumer, dc, LS, reason, Sig,
1142
+ consumer, dc, LS, reason,
1191
1143
visited, seenDynamicLookup);
1192
1144
}
1193
1145
}
@@ -1201,7 +1153,9 @@ static void lookupVisibleDynamicMemberLookupDecls(
1201
1153
static void lookupVisibleMemberDecls (
1202
1154
Type BaseTy, SourceLoc loc, VisibleDeclConsumer &Consumer,
1203
1155
const DeclContext *CurrDC, LookupState LS,
1204
- DeclVisibilityKind Reason, GenericSignature Sig) {
1156
+ DeclVisibilityKind Reason) {
1157
+ assert (!BaseTy->hasTypeParameter ());
1158
+
1205
1159
OverrideFilteringConsumer overrideConsumer (BaseTy, CurrDC);
1206
1160
KeyPathDynamicMemberConsumer dynamicConsumer (
1207
1161
Consumer,
@@ -1211,7 +1165,7 @@ static void lookupVisibleMemberDecls(
1211
1165
llvm::DenseSet<TypeBase *> seenDynamicLookup;
1212
1166
lookupVisibleMemberAndDynamicMemberDecls (
1213
1167
BaseTy, loc, overrideConsumer, dynamicConsumer, CurrDC, LS, Reason,
1214
- Sig, Visited, seenDynamicLookup);
1168
+ Visited, seenDynamicLookup);
1215
1169
1216
1170
// Report the declarations we found to the real consumer.
1217
1171
overrideConsumer.filterDecls (Consumer);
@@ -1325,7 +1279,7 @@ static void lookupVisibleDeclsImpl(VisibleDeclConsumer &Consumer,
1325
1279
1326
1280
if (ExtendedType) {
1327
1281
::lookupVisibleMemberDecls (ExtendedType, Loc, Consumer, DC, LS,
1328
- MemberReason, nullptr );
1282
+ MemberReason);
1329
1283
1330
1284
// Going outside the current type context.
1331
1285
MemberReason = DeclVisibilityKind::MemberOfOutsideNominal;
@@ -1386,6 +1340,13 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
1386
1340
bool includeDerivedRequirements,
1387
1341
bool includeProtocolExtensionMembers,
1388
1342
GenericSignature Sig) {
1343
+ // If we have an interface type, map it into its primary environment before
1344
+ // doing anything else.
1345
+ if (BaseTy->hasTypeParameter ()) {
1346
+ assert (Sig);
1347
+ BaseTy = Sig.getGenericEnvironment ()->mapTypeIntoContext (BaseTy);
1348
+ }
1349
+
1389
1350
assert (CurrDC);
1390
1351
LookupState ls = LookupState::makeQualified ();
1391
1352
if (includeInstanceMembers) {
@@ -1399,6 +1360,5 @@ void swift::lookupVisibleMemberDecls(VisibleDeclConsumer &Consumer, Type BaseTy,
1399
1360
}
1400
1361
1401
1362
::lookupVisibleMemberDecls (BaseTy, loc, Consumer, CurrDC, ls,
1402
- DeclVisibilityKind::MemberOfCurrentNominal,
1403
- Sig);
1363
+ DeclVisibilityKind::MemberOfCurrentNominal);
1404
1364
}
0 commit comments