Skip to content

Commit fca6724

Browse files
committed
[NFC] AST: Requestify checkInheritanceClause.
1 parent 5a87696 commit fca6724

File tree

5 files changed

+141
-8
lines changed

5 files changed

+141
-8
lines changed

include/swift/AST/Decl.h

Lines changed: 42 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,7 +561,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
561561
HasStubImplementation : 1
562562
);
563563

564-
SWIFT_INLINE_BITFIELD_EMPTY(TypeDecl, ValueDecl);
564+
SWIFT_INLINE_BITFIELD(TypeDecl, ValueDecl, 1+1,
565+
/// Whether the inheritance clause has been checked.
566+
HasCheckedInheritanceClause : 1,
567+
568+
/// Whether the inheritance clause checked successfully.
569+
HasValidInheritanceClause : 1
570+
);
565571

566572
SWIFT_INLINE_BITFIELD_FULL(GenericTypeParamDecl, TypeDecl, 16+16+1+1,
567573
: NumPadBits,
@@ -774,7 +780,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
774780
NumPathElements : 8
775781
);
776782

777-
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1,
783+
SWIFT_INLINE_BITFIELD(ExtensionDecl, Decl, 4+1+1+1,
778784
/// An encoding of the default and maximum access level for this extension.
779785
/// The value 4 corresponds to AccessLevel::Public
780786
///
@@ -784,7 +790,13 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
784790
DefaultAndMaxAccessLevel : 4,
785791

786792
/// Whether there is are lazily-loaded conformances for this extension.
787-
HasLazyConformances : 1
793+
HasLazyConformances : 1,
794+
795+
/// Whether the inheritance clause has been checked.
796+
HasCheckedInheritanceClause : 1,
797+
798+
/// Whether the inheritance clause checked successfully.
799+
HasValidInheritanceClause : 1
788800
);
789801

790802
SWIFT_INLINE_BITFIELD(IfConfigDecl, Decl, 1,
@@ -1819,6 +1831,17 @@ class ExtensionDecl final : public GenericContext, public Decl,
18191831

18201832
void setInherited(ArrayRef<InheritedEntry> i) { Inherited = i; }
18211833

1834+
void setInheritanceClauseChecked(bool valid) {
1835+
assert(!Bits.ExtensionDecl.HasCheckedInheritanceClause);
1836+
Bits.ExtensionDecl.HasCheckedInheritanceClause = true;
1837+
Bits.ExtensionDecl.HasValidInheritanceClause = valid;
1838+
}
1839+
1840+
std::pair<bool, bool> hasCheckedInheritanceClause() const {
1841+
return {Bits.ExtensionDecl.HasCheckedInheritanceClause,
1842+
Bits.ExtensionDecl.HasValidInheritanceClause};
1843+
}
1844+
18221845
bool hasDefaultAccessLevel() const {
18231846
return Bits.ExtensionDecl.DefaultAndMaxAccessLevel != 0;
18241847
}
@@ -3233,8 +3256,11 @@ class TypeDecl : public ValueDecl {
32333256
protected:
32343257
TypeDecl(DeclKind K, llvm::PointerUnion<DeclContext *, ASTContext *> context,
32353258
Identifier name, SourceLoc NameLoc,
3236-
ArrayRef<InheritedEntry> inherited) :
3237-
ValueDecl(K, context, name, NameLoc), Inherited(inherited) {}
3259+
ArrayRef<InheritedEntry> inherited)
3260+
: ValueDecl(K, context, name, NameLoc), Inherited(inherited) {
3261+
Bits.TypeDecl.HasCheckedInheritanceClause = false;
3262+
Bits.TypeDecl.HasValidInheritanceClause = false;
3263+
}
32383264

32393265
friend class InheritedTypes;
32403266

@@ -3257,6 +3283,17 @@ class TypeDecl : public ValueDecl {
32573283

32583284
void setInherited(ArrayRef<InheritedEntry> i) { Inherited = i; }
32593285

3286+
void setInheritanceClauseChecked(bool valid) {
3287+
assert(!Bits.TypeDecl.HasCheckedInheritanceClause);
3288+
Bits.TypeDecl.HasCheckedInheritanceClause = true;
3289+
Bits.TypeDecl.HasValidInheritanceClause = valid;
3290+
}
3291+
3292+
std::pair<bool, bool> hasCheckedInheritanceClause() const {
3293+
return {Bits.TypeDecl.HasCheckedInheritanceClause,
3294+
Bits.TypeDecl.HasValidInheritanceClause};
3295+
}
3296+
32603297
struct CanBeInvertible {
32613298
/// Indicates how "strongly" a TypeDecl will conform to an invertible
32623299
/// protocol. Supports inequality comparisons and casts to bool.

include/swift/AST/TypeCheckRequests.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4866,6 +4866,7 @@ class ImportDeclRequest
48664866
bool isCached() const { return true; }
48674867
};
48684868

4869+
48694870
class LifetimeDependenceInfoRequest
48704871
: public SimpleRequest<LifetimeDependenceInfoRequest,
48714872
std::optional<LifetimeDependenceInfo>(
@@ -4885,6 +4886,31 @@ class LifetimeDependenceInfoRequest
48854886
bool isCached() const { return true; }
48864887
};
48874888

4889+
/// Type check an inheritance clause.
4890+
class InheritanceClauseRequest
4891+
: public SimpleRequest<
4892+
InheritanceClauseRequest,
4893+
ArrayRef<InheritedEntry>(
4894+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>),
4895+
RequestFlags::Cached> {
4896+
public:
4897+
using SimpleRequest::SimpleRequest;
4898+
4899+
private:
4900+
friend SimpleRequest;
4901+
4902+
ArrayRef<InheritedEntry>
4903+
evaluate(Evaluator &evaluator,
4904+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>
4905+
declUnion) const;
4906+
4907+
public:
4908+
// Cached in TypeDecl/ExtensionDecl.
4909+
bool isCached() const { return true; }
4910+
std::optional<ArrayRef<InheritedEntry>> getCachedResult() const;
4911+
void cacheResult(ArrayRef<InheritedEntry>) const;
4912+
};
4913+
48884914
#define SWIFT_TYPEID_ZONE TypeChecker
48894915
#define SWIFT_TYPEID_HEADER "swift/AST/TypeCheckerTypeIDZone.def"
48904916
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,9 @@ SWIFT_REQUEST(TypeChecker, InheritedTypeRequest,
205205
Type(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>,
206206
unsigned, TypeResolutionStage),
207207
SeparatelyCached, HasNearestLocation)
208+
SWIFT_REQUEST(TypeChecker, InheritanceClauseRequest,
209+
evaluator::SideEffect(llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *>),
210+
Cached, NoLocationInfo)
208211
SWIFT_REQUEST(TypeChecker, InheritsSuperclassInitializersRequest,
209212
bool(ClassDecl *), SeparatelyCached, NoLocationInfo)
210213
SWIFT_REQUEST(TypeChecker, InitKindRequest,

lib/AST/Decl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,8 @@ ExtensionDecl::ExtensionDecl(SourceLoc extensionLoc,
16861686
{
16871687
Bits.ExtensionDecl.DefaultAndMaxAccessLevel = 0;
16881688
Bits.ExtensionDecl.HasLazyConformances = false;
1689+
Bits.ExtensionDecl.HasCheckedInheritanceClause = false;
1690+
Bits.ExtensionDecl.HasValidInheritanceClause = false;
16891691
setTrailingWhereClause(trailingWhereClause);
16901692
}
16911693

lib/Sema/TypeCheckDeclPrimary.cpp

Lines changed: 68 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,34 @@ static Type containsParameterizedProtocolType(Type inheritedTy) {
8787
return Type();
8888
}
8989

90+
static ArrayRef<InheritedEntry> checkInheritanceClauseEntries(
91+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> declUnion) {
92+
const Decl *decl = nullptr;
93+
if (const auto *td = declUnion.dyn_cast<const TypeDecl *>()) {
94+
decl = td;
95+
} else {
96+
decl = declUnion.get<const ExtensionDecl *>();
97+
}
98+
ASTContext &ctx = decl->getASTContext();
99+
return evaluateOrDefault(ctx.evaluator, InheritanceClauseRequest{declUnion},
100+
{});
101+
}
102+
103+
static void checkInheritanceClause(
104+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> declUnion) {
105+
auto entries = checkInheritanceClauseEntries(declUnion);
106+
(void)entries;
107+
}
108+
90109
/// Check the inheritance clause of a type declaration or extension thereof.
91110
///
92111
/// This routine performs detailed checking of the inheritance clause of the
93112
/// given type or extension. It need only be called within the primary source
94113
/// file.
95-
static void checkInheritanceClause(
96-
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> declUnion) {
114+
ArrayRef<InheritedEntry> InheritanceClauseRequest::evaluate(
115+
Evaluator &evaluator,
116+
llvm::PointerUnion<const TypeDecl *, const ExtensionDecl *> declUnion)
117+
const {
97118
auto inheritedTypes = InheritedTypes(declUnion);
98119
auto inheritedClause = inheritedTypes.getEntries();
99120
const ExtensionDecl *ext = nullptr;
@@ -109,7 +130,7 @@ static void checkInheritanceClause(
109130
proto->getName())
110131
.highlight(SourceRange(inheritedClause.front().getSourceRange().Start,
111132
inheritedClause.back().getSourceRange().End));
112-
return;
133+
return {};
113134
}
114135
}
115136
} else {
@@ -341,6 +362,50 @@ static void checkInheritanceClause(
341362
inheritedTy);
342363
// FIXME: Note pointing to the declaration 'inheritedTy' references?
343364
}
365+
CheckSuppressions(declUnion, suppressedConformances, ctx).check();
366+
return inheritedTypes.getEntries();
367+
}
368+
369+
std::optional<ArrayRef<InheritedEntry>>
370+
InheritanceClauseRequest::getCachedResult() const {
371+
auto declUnion = std::get<0>(getStorage());
372+
if (const auto *td = declUnion.dyn_cast<const TypeDecl *>()) {
373+
auto checking = td->hasCheckedInheritanceClause();
374+
if (!checking.first)
375+
return std::nullopt;
376+
if (!checking.second)
377+
return {{}};
378+
} else {
379+
auto *ed = declUnion.get<const ExtensionDecl *>();
380+
auto checking = ed->hasCheckedInheritanceClause();
381+
if (!checking.first)
382+
return std::nullopt;
383+
if (!checking.second)
384+
return {{}};
385+
}
386+
return InheritedTypes(std::get<0>(getStorage())).getEntries();
387+
}
388+
389+
void InheritanceClauseRequest::cacheResult(
390+
ArrayRef<InheritedEntry> entries) const {
391+
auto declUnion = std::get<0>(getStorage());
392+
auto inheritanceClause = InheritedTypes(declUnion);
393+
if (inheritanceClause.size() == 0) {
394+
// Nothing to check, nothing to cache.
395+
assert(entries.size() == 0);
396+
return;
397+
}
398+
assert(entries.size() == 0 || entries.size() == inheritanceClause.size());
399+
// It's valid if it has entries.
400+
auto valid = entries.size() > 0;
401+
if (const auto *td = declUnion.dyn_cast<const TypeDecl *>()) {
402+
auto *mtd = const_cast<TypeDecl *>(td);
403+
mtd->setInheritanceClauseChecked(valid);
404+
} else {
405+
auto *ed = declUnion.get<const ExtensionDecl *>();
406+
auto *med = const_cast<ExtensionDecl *>(ed);
407+
med->setInheritanceClauseChecked(valid);
408+
}
344409
}
345410

346411
static void installCodingKeysIfNecessary(NominalTypeDecl *NTD) {

0 commit comments

Comments
 (0)