Skip to content

Commit 55447e6

Browse files
Merge pull request #31645 from AnthonyLatsis/relocate-to-iterabledc
[NFC] AST: Relocate some conformance lookup client methods from DeclContext to IterableDeclContext
2 parents cb3db19 + bfe25fa commit 55447e6

19 files changed

+131
-120
lines changed

include/swift/AST/DeclContext.h

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ namespace swift {
5151
class Expr;
5252
class GenericParamList;
5353
class LazyMemberLoader;
54+
class GenericContext;
5455
class GenericSignature;
5556
class GenericTypeParamDecl;
5657
class GenericTypeParamType;
@@ -566,41 +567,6 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
566567
LLVM_READONLY
567568
ASTContext &getASTContext() const;
568569

569-
/// Retrieve the set of protocols whose conformances will be
570-
/// associated with this declaration context.
571-
///
572-
/// This function differs from \c getLocalConformances() in that it
573-
/// returns protocol declarations, not protocol conformances, and
574-
/// therefore does not require the protocol conformances to be
575-
/// formed.
576-
///
577-
/// \param lookupKind The kind of lookup to perform.
578-
///
579-
/// FIXME: This likely makes more sense on IterableDeclContext or
580-
/// something similar.
581-
SmallVector<ProtocolDecl *, 2>
582-
getLocalProtocols(ConformanceLookupKind lookupKind
583-
= ConformanceLookupKind::All) const;
584-
585-
/// Retrieve the set of protocol conformances associated with this
586-
/// declaration context.
587-
///
588-
/// \param lookupKind The kind of lookup to perform.
589-
///
590-
/// FIXME: This likely makes more sense on IterableDeclContext or
591-
/// something similar.
592-
SmallVector<ProtocolConformance *, 2>
593-
getLocalConformances(ConformanceLookupKind lookupKind
594-
= ConformanceLookupKind::All) const;
595-
596-
/// Retrieve diagnostics discovered while expanding conformances for this
597-
/// declaration context. This operation then removes those diagnostics from
598-
/// consideration, so subsequent calls to this function with the same
599-
/// declaration context that have not had any new extensions bound
600-
/// will see an empty array.
601-
SmallVector<ConformanceDiagnostic, 4>
602-
takeConformanceDiagnostics() const;
603-
604570
/// Retrieves a list of separately imported overlays which are shadowing
605571
/// \p declaring. If any \p overlays are returned, qualified lookups into
606572
/// \p declaring should be performed into \p overlays instead; since they
@@ -815,9 +781,40 @@ class IterableDeclContext {
815781
/// valid).
816782
bool wasDeserialized() const;
817783

784+
/// Retrieve the set of protocols whose conformances will be
785+
/// associated with this declaration context.
786+
///
787+
/// This function differs from \c getLocalConformances() in that it
788+
/// returns protocol declarations, not protocol conformances, and
789+
/// therefore does not require the protocol conformances to be
790+
/// formed.
791+
///
792+
/// \param lookupKind The kind of lookup to perform.
793+
SmallVector<ProtocolDecl *, 2>
794+
getLocalProtocols(ConformanceLookupKind lookupKind
795+
= ConformanceLookupKind::All) const;
796+
797+
/// Retrieve the set of protocol conformances associated with this
798+
/// declaration context.
799+
///
800+
/// \param lookupKind The kind of lookup to perform.
801+
SmallVector<ProtocolConformance *, 2>
802+
getLocalConformances(ConformanceLookupKind lookupKind
803+
= ConformanceLookupKind::All) const;
804+
805+
/// Retrieve diagnostics discovered while expanding conformances for this
806+
/// declaration context. This operation then removes those diagnostics from
807+
/// consideration, so subsequent calls to this function with the same
808+
/// declaration context that have not had any new extensions bound
809+
/// will see an empty array.
810+
SmallVector<ConformanceDiagnostic, 4> takeConformanceDiagnostics() const;
811+
818812
/// Return 'this' as a \c Decl.
819813
const Decl *getDecl() const;
820814

815+
/// Return 'this' as a \c GenericContext.
816+
const GenericContext *getAsGenericContext() const;
817+
821818
/// Get the DeclID this Decl was deserialized from.
822819
serialization::DeclID getDeclID() const {
823820
assert(wasDeserialized());

include/swift/AST/TypeCheckRequests.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,8 @@ void simple_display(llvm::raw_ostream &out, ConformanceLookupKind kind);
22592259
/// must also be reported so it can be checked as well.
22602260
class LookupAllConformancesInContextRequest
22612261
: public SimpleRequest<LookupAllConformancesInContextRequest,
2262-
ProtocolConformanceLookupResult(const DeclContext *),
2262+
ProtocolConformanceLookupResult(
2263+
const IterableDeclContext *),
22632264
RequestFlags::Uncached |
22642265
RequestFlags::DependencySink |
22652266
RequestFlags::DependencySource> {
@@ -2271,7 +2272,7 @@ class LookupAllConformancesInContextRequest
22712272

22722273
// Evaluation.
22732274
ProtocolConformanceLookupResult
2274-
evaluate(Evaluator &evaluator, const DeclContext *DC) const;
2275+
evaluate(Evaluator &evaluator, const IterableDeclContext *IDC) const;
22752276

22762277
public:
22772278
// Incremental dependencies

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ SWIFT_REQUEST(TypeChecker, ScopedImportLookupRequest,
256256
SWIFT_REQUEST(TypeChecker, ClosureHasExplicitResultRequest,
257257
bool(ClosureExpr *), Cached, NoLocationInfo)
258258
SWIFT_REQUEST(TypeChecker, LookupAllConformancesInContextRequest,
259-
ProtocolConformanceLookupResult(const DeclContext *),
259+
ProtocolConformanceLookupResult(const IterableDeclContext *),
260260
Uncached, NoLocationInfo)
261261
SWIFT_REQUEST(TypeChecker, SimpleDidSetRequest,
262262
bool(AccessorDecl *), Cached, NoLocationInfo)

lib/AST/DeclContext.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,16 @@ IterableDeclContext::getDecl() const {
773773
llvm_unreachable("Unhandled IterableDeclContextKind in switch.");
774774
}
775775

776+
const GenericContext *IterableDeclContext::getAsGenericContext() const {
777+
switch (getIterableContextKind()) {
778+
case IterableDeclContextKind::NominalTypeDecl:
779+
return cast<NominalTypeDecl>(this);
780+
case IterableDeclContextKind::ExtensionDecl:
781+
return cast<ExtensionDecl>(this);
782+
}
783+
llvm_unreachable("Unhandled IterableDeclContextKind in switch.");
784+
}
785+
776786
ASTContext &IterableDeclContext::getASTContext() const {
777787
return getDecl()->getASTContext();
778788
}
@@ -860,7 +870,7 @@ bool IterableDeclContext::hasUnparsedMembers() const {
860870
if (AddedParsedMembers)
861871
return false;
862872

863-
if (!getDecl()->getDeclContext()->getParentSourceFile()) {
873+
if (!getAsGenericContext()->getParentSourceFile()) {
864874
// There will never be any parsed members to add, so set the flag to say
865875
// we are done so we can short-circuit next time.
866876
const_cast<IterableDeclContext *>(this)->AddedParsedMembers = 1;
@@ -880,7 +890,7 @@ void IterableDeclContext::loadAllMembers() const {
880890
ASTContext &ctx = getASTContext();
881891

882892
// For contexts within a source file, get the list of parsed members.
883-
if (getDecl()->getDeclContext()->getParentSourceFile()) {
893+
if (getAsGenericContext()->getParentSourceFile()) {
884894
// Retrieve the parsed members. Even if we've already added the parsed
885895
// members to this context, this call is important for recording the
886896
// dependency edge.
@@ -918,7 +928,7 @@ void IterableDeclContext::loadAllMembers() const {
918928
}
919929

920930
bool IterableDeclContext::wasDeserialized() const {
921-
const DeclContext *DC = cast<DeclContext>(getDecl());
931+
const DeclContext *DC = getAsGenericContext();
922932
if (auto F = dyn_cast<FileUnit>(DC->getModuleScopeContext())) {
923933
return F->getKind() == FileUnitKind::SerializedAST;
924934
}
@@ -950,7 +960,7 @@ IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
950960

951961
Optional<std::string> IterableDeclContext::getBodyFingerprint() const {
952962
// Only makes sense for contexts in a source file
953-
if (!getDecl()->getDeclContext()->getParentSourceFile())
963+
if (!getAsGenericContext()->getParentSourceFile())
954964
return None;
955965
auto mutableThis = const_cast<IterableDeclContext *>(this);
956966
return evaluateOrDefault(getASTContext().evaluator,

lib/AST/ProtocolConformance.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1313,11 +1313,12 @@ NominalTypeDecl::getSatisfiedProtocolRequirementsForMember(
13131313
}
13141314

13151315
SmallVector<ProtocolDecl *, 2>
1316-
DeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
1316+
IterableDeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
13171317
SmallVector<ProtocolDecl *, 2> result;
13181318

13191319
// Dig out the nominal type.
1320-
NominalTypeDecl *nominal = getSelfNominalTypeDecl();
1320+
const auto dc = getAsGenericContext();
1321+
const auto nominal = dc->getSelfNominalTypeDecl();
13211322
if (!nominal) {
13221323
return result;
13231324
}
@@ -1326,7 +1327,7 @@ DeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
13261327
nominal->prepareConformanceTable();
13271328
nominal->ConformanceTable->lookupConformances(
13281329
nominal,
1329-
const_cast<DeclContext *>(this),
1330+
const_cast<GenericContext *>(dc),
13301331
lookupKind,
13311332
&result,
13321333
nullptr,
@@ -1336,11 +1337,13 @@ DeclContext::getLocalProtocols(ConformanceLookupKind lookupKind) const {
13361337
}
13371338

13381339
SmallVector<ProtocolConformance *, 2>
1339-
DeclContext::getLocalConformances(ConformanceLookupKind lookupKind) const {
1340+
IterableDeclContext::getLocalConformances(ConformanceLookupKind lookupKind)
1341+
const {
13401342
SmallVector<ProtocolConformance *, 2> result;
13411343

13421344
// Dig out the nominal type.
1343-
NominalTypeDecl *nominal = getSelfNominalTypeDecl();
1345+
const auto dc = getAsGenericContext();
1346+
const auto nominal = dc->getSelfNominalTypeDecl();
13441347
if (!nominal) {
13451348
return result;
13461349
}
@@ -1359,7 +1362,7 @@ DeclContext::getLocalConformances(ConformanceLookupKind lookupKind) const {
13591362
nominal->prepareConformanceTable();
13601363
nominal->ConformanceTable->lookupConformances(
13611364
nominal,
1362-
const_cast<DeclContext *>(this),
1365+
const_cast<GenericContext *>(dc),
13631366
lookupKind,
13641367
nullptr,
13651368
&result,
@@ -1369,25 +1372,27 @@ DeclContext::getLocalConformances(ConformanceLookupKind lookupKind) const {
13691372
}
13701373

13711374
SmallVector<ConformanceDiagnostic, 4>
1372-
DeclContext::takeConformanceDiagnostics() const {
1375+
IterableDeclContext::takeConformanceDiagnostics() const {
13731376
SmallVector<ConformanceDiagnostic, 4> result;
13741377

13751378
// Dig out the nominal type.
1376-
NominalTypeDecl *nominal = getSelfNominalTypeDecl();
1379+
const auto dc = getAsGenericContext();
1380+
const auto nominal = dc->getSelfNominalTypeDecl();
1381+
13771382
if (!nominal) {
1378-
return { };
1383+
return result;
13791384
}
13801385

13811386
// Protocols are not subject to the checks for supersession.
13821387
if (isa<ProtocolDecl>(nominal)) {
1383-
return { };
1388+
return result;
13841389
}
13851390

13861391
// Update to record all potential conformances.
13871392
nominal->prepareConformanceTable();
13881393
nominal->ConformanceTable->lookupConformances(
13891394
nominal,
1390-
const_cast<DeclContext *>(this),
1395+
const_cast<GenericContext *>(dc),
13911396
ConformanceLookupKind::All,
13921397
nullptr,
13931398
nullptr,

lib/AST/TypeCheckRequests.cpp

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1385,20 +1385,16 @@ void CheckRedeclarationRequest::writeDependencySink(
13851385
evaluator::DependencySource
13861386
LookupAllConformancesInContextRequest::readDependencySource(
13871387
const evaluator::DependencyCollector &collector) const {
1388-
auto *dc = std::get<0>(getStorage());
1389-
AccessLevel defaultAccess;
1390-
if (auto ext = dyn_cast<ExtensionDecl>(dc)) {
1391-
const NominalTypeDecl *nominal = ext->getExtendedNominal();
1392-
if (!nominal) {
1393-
return {collector.getActiveDependencySourceOrNull(),
1394-
evaluator::DependencyScope::Cascading};
1395-
}
1396-
defaultAccess = nominal->getFormalAccess();
1397-
} else {
1398-
defaultAccess = cast<NominalTypeDecl>(dc)->getFormalAccess();
1388+
const auto *nominal = std::get<0>(getStorage())
1389+
->getAsGenericContext()
1390+
->getSelfNominalTypeDecl();
1391+
if (!nominal) {
1392+
return {collector.getActiveDependencySourceOrNull(),
1393+
evaluator::DependencyScope::Cascading};
13991394
}
1395+
14001396
return {collector.getActiveDependencySourceOrNull(),
1401-
evaluator::getScopeForAccessLevel(defaultAccess)};
1397+
evaluator::getScopeForAccessLevel(nominal->getFormalAccess())};
14021398
}
14031399

14041400
void LookupAllConformancesInContextRequest::writeDependencySink(

lib/IDE/Refactoring.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2801,7 +2801,8 @@ bool RefactoringActionConvertToTernaryExpr::performChange() {
28012801
/// these stubs should be filled.
28022802
class FillProtocolStubContext {
28032803

2804-
std::vector<ValueDecl*> getUnsatisfiedRequirements(const DeclContext *DC);
2804+
std::vector<ValueDecl*>
2805+
getUnsatisfiedRequirements(const IterableDeclContext *IDC);
28052806

28062807
/// Context in which the content should be filled; this could be either a
28072808
/// nominal type declaraion or an extension declaration.
@@ -2872,12 +2873,12 @@ getContextFromCursorInfo(ResolvedCursorInfo CursorInfo) {
28722873
}
28732874

28742875
std::vector<ValueDecl*> FillProtocolStubContext::
2875-
getUnsatisfiedRequirements(const DeclContext *DC) {
2876+
getUnsatisfiedRequirements(const IterableDeclContext *IDC) {
28762877
// The results to return.
28772878
std::vector<ValueDecl*> NonWitnessedReqs;
28782879

28792880
// For each conformance of the extended nominal.
2880-
for(ProtocolConformance *Con : DC->getLocalConformances()) {
2881+
for(ProtocolConformance *Con : IDC->getLocalConformances()) {
28812882

28822883
// Collect non-witnessed requirements.
28832884
Con->forEachNonWitnessedRequirement(

lib/IRGen/GenClass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1116,9 +1116,9 @@ namespace {
11161116

11171117
/// Gather protocol records for all of the explicitly-specified Objective-C
11181118
/// protocol conformances.
1119-
void visitConformances(DeclContext *dc) {
1119+
void visitConformances(const IterableDeclContext *idc) {
11201120
llvm::SmallSetVector<ProtocolDecl *, 2> protocols;
1121-
for (auto conformance : dc->getLocalConformances(
1121+
for (auto conformance : idc->getLocalConformances(
11221122
ConformanceLookupKind::OnlyExplicit)) {
11231123
ProtocolDecl *proto = conformance->getProtocol();
11241124
getObjCProtocols(proto, protocols);

lib/IRGen/GenDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,9 +913,9 @@ IRGenModule::getConstantReferenceForProtocolDescriptor(ProtocolDecl *proto) {
913913
LinkEntity::forProtocolDescriptor(proto));
914914
}
915915

916-
void IRGenModule::addLazyConformances(DeclContext *dc) {
916+
void IRGenModule::addLazyConformances(const IterableDeclContext *idc) {
917917
for (const ProtocolConformance *conf :
918-
dc->getLocalConformances(ConformanceLookupKind::All)) {
918+
idc->getLocalConformances(ConformanceLookupKind::All)) {
919919
IRGen.addLazyWitnessTable(conf);
920920
}
921921
}

lib/IRGen/IRGenModule.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1594,8 +1594,9 @@ private: \
15941594
void addRuntimeResolvableType(GenericTypeDecl *nominal);
15951595
void maybeEmitOpaqueTypeDecl(OpaqueTypeDecl *opaque);
15961596

1597-
/// Add all conformances of the given \c DeclContext LazyWitnessTables.
1598-
void addLazyConformances(DeclContext *dc);
1597+
/// Add all conformances of the given \c IterableDeclContext
1598+
/// LazyWitnessTables.
1599+
void addLazyConformances(const IterableDeclContext *idc);
15991600

16001601
//--- Global context emission --------------------------------------------------
16011602
public:

lib/Index/Index.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -777,11 +777,12 @@ bool IndexSwiftASTWalker::visitImports(
777777
}
778778

779779
bool IndexSwiftASTWalker::handleWitnesses(Decl *D, SmallVectorImpl<IndexedWitness> &explicitWitnesses) {
780-
auto DC = dyn_cast<DeclContext>(D);
781-
if (!DC)
780+
const auto *const IDC = dyn_cast<IterableDeclContext>(D);
781+
if (!IDC)
782782
return true;
783783

784-
for (auto *conf : DC->getLocalConformances()) {
784+
const auto DC = IDC->getAsGenericContext();
785+
for (auto *conf : IDC->getLocalConformances()) {
785786
if (conf->isInvalid())
786787
continue;
787788

lib/Parse/ParseRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void swift::simple_display(llvm::raw_ostream &out,
4848
FingerprintAndMembers
4949
ParseMembersRequest::evaluate(Evaluator &evaluator,
5050
IterableDeclContext *idc) const {
51-
SourceFile &sf = *idc->getDecl()->getDeclContext()->getParentSourceFile();
51+
SourceFile &sf = *idc->getAsGenericContext()->getParentSourceFile();
5252
unsigned bufferID = *sf.getBufferID();
5353

5454
// Lexer diaganostics have been emitted during skipping, so we disable lexer's

lib/Sema/DerivedConformanceEquatableHashable.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,11 +1024,11 @@ getHashValueRequirement(ASTContext &C) {
10241024
}
10251025

10261026
static ProtocolConformance *
1027-
getHashableConformance(Decl *parentDecl) {
1027+
getHashableConformance(const Decl *parentDecl) {
10281028
ASTContext &C = parentDecl->getASTContext();
1029-
auto DC = cast<DeclContext>(parentDecl);
1029+
const auto IDC = cast<IterableDeclContext>(parentDecl);
10301030
auto hashableProto = C.getProtocol(KnownProtocolKind::Hashable);
1031-
for (auto conformance: DC->getLocalConformances()) {
1031+
for (auto conformance: IDC->getLocalConformances()) {
10321032
if (conformance->getProtocol() == hashableProto) {
10331033
return conformance;
10341034
}

lib/Sema/LookupVisibleDecls.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ static void
539539
// wrappers.
540540
static void
541541
synthesizePropertyWrapperStorageWrapperProperties(IterableDeclContext *IDC) {
542-
auto SF = IDC->getDecl()->getDeclContext()->getParentSourceFile();
542+
auto SF = IDC->getAsGenericContext()->getParentSourceFile();
543543
if (!SF || SF->Kind == SourceFileKind::Interface)
544544
return;
545545

0 commit comments

Comments
 (0)