Skip to content

Commit edcf764

Browse files
committed
[NFC] AST: Define and use IterableDeclContext::getAsGenericContext()
1 parent 7b787a6 commit edcf764

File tree

6 files changed

+47
-35
lines changed

6 files changed

+47
-35
lines changed

include/swift/AST/DeclContext.h

Lines changed: 4 additions & 0 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;
@@ -818,6 +819,9 @@ class IterableDeclContext {
818819
/// Return 'this' as a \c Decl.
819820
const Decl *getDecl() const;
820821

822+
/// Return 'this' as a \c GenericContext.
823+
const GenericContext *getAsGenericContext() const;
824+
821825
/// Get the DeclID this Decl was deserialized from.
822826
serialization::DeclID getDeclID() const {
823827
assert(wasDeserialized());

lib/AST/DeclContext.cpp

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

769+
const GenericContext *IterableDeclContext::getAsGenericContext() const {
770+
switch (getIterableContextKind()) {
771+
case IterableDeclContextKind::NominalTypeDecl:
772+
return cast<NominalTypeDecl>(this);
773+
case IterableDeclContextKind::ExtensionDecl:
774+
return cast<ExtensionDecl>(this);
775+
}
776+
llvm_unreachable("Unhandled IterableDeclContextKind in switch.");
777+
}
778+
769779
ASTContext &IterableDeclContext::getASTContext() const {
770780
return getDecl()->getASTContext();
771781
}
@@ -853,7 +863,7 @@ bool IterableDeclContext::hasUnparsedMembers() const {
853863
if (AddedParsedMembers)
854864
return false;
855865

856-
if (!getDecl()->getDeclContext()->getParentSourceFile()) {
866+
if (!getAsGenericContext()->getParentSourceFile()) {
857867
// There will never be any parsed members to add, so set the flag to say
858868
// we are done so we can short-circuit next time.
859869
const_cast<IterableDeclContext *>(this)->AddedParsedMembers = 1;
@@ -873,7 +883,7 @@ void IterableDeclContext::loadAllMembers() const {
873883
ASTContext &ctx = getASTContext();
874884

875885
// For contexts within a source file, get the list of parsed members.
876-
if (getDecl()->getDeclContext()->getParentSourceFile()) {
886+
if (getAsGenericContext()->getParentSourceFile()) {
877887
// Retrieve the parsed members. Even if we've already added the parsed
878888
// members to this context, this call is important for recording the
879889
// dependency edge.
@@ -911,7 +921,7 @@ void IterableDeclContext::loadAllMembers() const {
911921
}
912922

913923
bool IterableDeclContext::wasDeserialized() const {
914-
const DeclContext *DC = cast<DeclContext>(getDecl());
924+
const DeclContext *DC = getAsGenericContext();
915925
if (auto F = dyn_cast<FileUnit>(DC->getModuleScopeContext())) {
916926
return F->getKind() == FileUnitKind::SerializedAST;
917927
}
@@ -943,7 +953,7 @@ IterableDeclContext::castDeclToIterableDeclContext(const Decl *D) {
943953

944954
Optional<std::string> IterableDeclContext::getBodyFingerprint() const {
945955
// Only makes sense for contexts in a source file
946-
if (!getDecl()->getDeclContext()->getParentSourceFile())
956+
if (!getAsGenericContext()->getParentSourceFile())
947957
return None;
948958
auto mutableThis = const_cast<IterableDeclContext *>(this);
949959
return evaluateOrDefault(getASTContext().evaluator,

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/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

lib/SymbolGraphGen/Edge.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ void Edge::serialize(llvm::json::OStream &OS) const {
4242
}
4343

4444
if (ConformanceExtension) {
45-
if (const auto *Generics = ConformanceExtension->getAsGenericContext()) {
46-
SmallVector<Requirement, 4> FilteredRequirements;
47-
filterGenericRequirements(Generics->getGenericRequirements(),
48-
ConformanceExtension->getExtendedNominal()
49-
->getDeclContext()->getSelfNominalTypeDecl(),
50-
FilteredRequirements);
51-
if (!FilteredRequirements.empty()) {
52-
OS.attributeArray("swiftConstraints", [&](){
53-
for (const auto &Req :
54-
ConformanceExtension->getGenericRequirements()) {
55-
::serialize(Req, OS);
56-
}
57-
});
58-
}
45+
SmallVector<Requirement, 4> FilteredRequirements;
46+
filterGenericRequirements(
47+
ConformanceExtension->getGenericRequirements(),
48+
ConformanceExtension->getExtendedNominal()
49+
->getDeclContext()->getSelfNominalTypeDecl(),
50+
FilteredRequirements);
51+
if (!FilteredRequirements.empty()) {
52+
OS.attributeArray("swiftConstraints", [&](){
53+
for (const auto &Req :
54+
ConformanceExtension->getGenericRequirements()) {
55+
::serialize(Req, OS);
56+
}
57+
});
5958
}
6059
}
6160
});

lib/SymbolGraphGen/JSON.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,20 @@ void swift::symbolgraphgen::serialize(const ExtensionDecl *Extension,
6666
OS.attribute("extendedModule", ExtendedModule->getNameStr());
6767
}
6868
}
69-
if (const auto Generics = Extension->getAsGenericContext()) {
70-
SmallVector<Requirement, 4> FilteredRequirements;
71-
72-
filterGenericRequirements(Generics->getGenericRequirements(),
73-
Extension->getExtendedNominal()
74-
->getDeclContext()->getSelfNominalTypeDecl(),
75-
FilteredRequirements);
76-
77-
if (!FilteredRequirements.empty()) {
78-
OS.attributeArray("constraints", [&](){
79-
for (const auto &Requirement : FilteredRequirements) {
80-
serialize(Requirement, OS);
81-
}
82-
}); // end constraints:
83-
}
69+
70+
SmallVector<Requirement, 4> FilteredRequirements;
71+
72+
filterGenericRequirements(Extension->getGenericRequirements(),
73+
Extension->getExtendedNominal()
74+
->getDeclContext()->getSelfNominalTypeDecl(),
75+
FilteredRequirements);
76+
77+
if (!FilteredRequirements.empty()) {
78+
OS.attributeArray("constraints", [&](){
79+
for (const auto &Requirement : FilteredRequirements) {
80+
serialize(Requirement, OS);
81+
}
82+
}); // end constraints:
8483
}
8584
}); // end swiftExtension:
8685
}

0 commit comments

Comments
 (0)