Skip to content

Commit 6297a59

Browse files
committed
Requestify Class Ancestry Flags
1 parent b1b0fbe commit 6297a59

File tree

6 files changed

+69
-20
lines changed

6 files changed

+69
-20
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ SWIFT_TYPEID(ResilienceExpansion)
2828
SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>, PropertyWrapperMutability)
2929
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
3030
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
31+
SWIFT_TYPEID(AncestryFlags)

include/swift/AST/ASTTypeIDs.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class VarDecl;
3131
class TypeAliasDecl;
3232
class Type;
3333
struct TypePair;
34+
enum class AncestryFlags : uint8_t;
3435

3536
#define SWIFT_AST_TYPEID_ZONE 1
3637

include/swift/AST/Decl.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -554,13 +554,7 @@ class alignas(1 << DeclAlignInBits) Decl {
554554
RawForeignKind : 2,
555555

556556
/// \see ClassDecl::getEmittedMembers()
557-
HasForcedEmittedMembers : 1,
558-
559-
/// Information about the class's ancestry.
560-
Ancestry : 7,
561-
562-
/// Whether we have computed the above field or not.
563-
AncestryComputed : 1,
557+
HasForcedEmittedMembers : 1,
564558

565559
HasMissingDesignatedInitializers : 1,
566560
HasMissingVTableEntries : 1,

include/swift/AST/TypeCheckRequests.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,6 +1052,27 @@ class IsImplicitlyUnwrappedOptionalRequest :
10521052
void cacheResult(bool value) const;
10531053
};
10541054

1055+
class ClassAncestryFlagsRequest :
1056+
public SimpleRequest<ClassAncestryFlagsRequest,
1057+
AncestryFlags (ClassDecl *),
1058+
CacheKind::Cached> {
1059+
public:
1060+
using SimpleRequest::SimpleRequest;
1061+
1062+
private:
1063+
friend SimpleRequest;
1064+
1065+
// Evaluation.
1066+
llvm::Expected<AncestryFlags>
1067+
evaluate(Evaluator &evaluator, ClassDecl *value) const;
1068+
1069+
public:
1070+
// Caching.
1071+
bool isCached() const { return true; }
1072+
};
1073+
1074+
void simple_display(llvm::raw_ostream &out, AncestryFlags value);
1075+
10551076
// Allow AnyValue to compare two Type values, even though Type doesn't
10561077
// support ==.
10571078
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,5 @@ SWIFT_TYPEID(RequiresOpaqueModifyCoroutineRequest)
5555
SWIFT_TYPEID(IsAccessorTransparentRequest)
5656
SWIFT_TYPEID(SynthesizeAccessorRequest)
5757
SWIFT_TYPEID(EmittedMembersRequest)
58-
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
58+
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
59+
SWIFT_TYPEID(ClassAncestryFlagsRequest)

lib/AST/Decl.cpp

Lines changed: 43 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3820,8 +3820,6 @@ ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
38203820
= static_cast<unsigned>(CircularityCheck::Unchecked);
38213821
Bits.ClassDecl.InheritsSuperclassInits = 0;
38223822
Bits.ClassDecl.RawForeignKind = 0;
3823-
Bits.ClassDecl.Ancestry = 0;
3824-
Bits.ClassDecl.AncestryComputed = 0;
38253823
Bits.ClassDecl.HasMissingDesignatedInitializers = 0;
38263824
Bits.ClassDecl.HasMissingVTableEntries = 0;
38273825
Bits.ClassDecl.IsIncompatibleWithWeakReferences = 0;
@@ -3956,16 +3954,20 @@ bool ClassDecl::inheritsSuperclassInitializers() {
39563954
return Bits.ClassDecl.InheritsSuperclassInits;
39573955
}
39583956

3959-
AncestryOptions ClassDecl::checkAncestry() const {
3960-
// See if we've already computed this.
3961-
if (Bits.ClassDecl.AncestryComputed)
3962-
return AncestryOptions(Bits.ClassDecl.Ancestry);
39633957

3958+
AncestryOptions ClassDecl::checkAncestry() const {
3959+
return AncestryOptions(evaluateOrDefault(getASTContext().evaluator,
3960+
ClassAncestryFlagsRequest{const_cast<ClassDecl *>(this)},
3961+
AncestryFlags()));
3962+
}
3963+
3964+
llvm::Expected<AncestryFlags>
3965+
ClassAncestryFlagsRequest::evaluate(Evaluator &evaluator, ClassDecl *value) const {
39643966
llvm::SmallPtrSet<const ClassDecl *, 8> visited;
39653967

39663968
AncestryOptions result;
3967-
const ClassDecl *CD = this;
3968-
auto *M = getParentModule();
3969+
const ClassDecl *CD = value;
3970+
auto *M = value->getParentModule();
39693971

39703972
do {
39713973
// If we hit circularity, we will diagnose at some point in typeCheckDecl().
@@ -4000,10 +4002,39 @@ AncestryOptions ClassDecl::checkAncestry() const {
40004002
CD = CD->getSuperclassDecl();
40014003
} while (CD != nullptr);
40024004

4003-
// Save the result for later.
4004-
const_cast<ClassDecl *>(this)->Bits.ClassDecl.Ancestry = result.toRaw();
4005-
const_cast<ClassDecl *>(this)->Bits.ClassDecl.AncestryComputed = 1;
4006-
return result;
4005+
return AncestryFlags(result.toRaw());
4006+
}
4007+
4008+
void swift::simple_display(llvm::raw_ostream &out, AncestryFlags value) {
4009+
AncestryOptions opts(value);
4010+
out << "{ ";
4011+
// If we have more than one bit set, we need to print the separator.
4012+
bool wantsSeparator = false;
4013+
auto printBit = [&wantsSeparator, &out](bool val, StringRef name) {
4014+
if (wantsSeparator) {
4015+
out << ", ";
4016+
}
4017+
4018+
if (!wantsSeparator) {
4019+
wantsSeparator = true;
4020+
}
4021+
4022+
out << name;
4023+
if (val) {
4024+
out << " = true";
4025+
} else {
4026+
out << " = false";
4027+
}
4028+
};
4029+
printBit(opts.contains(AncestryFlags::ObjC), "ObjC");
4030+
printBit(opts.contains(AncestryFlags::ObjCMembers), "ObjCMembers");
4031+
printBit(opts.contains(AncestryFlags::Generic), "Generic");
4032+
printBit(opts.contains(AncestryFlags::Resilient), "Resilient");
4033+
printBit(opts.contains(AncestryFlags::ResilientOther), "ResilientOther");
4034+
printBit(opts.contains(AncestryFlags::ClangImported), "ClangImported");
4035+
printBit(opts.contains(AncestryFlags::RequiresStoredPropertyInits),
4036+
"RequiresStoredPropertyInits");
4037+
out << " }";
40074038
}
40084039

40094040
bool ClassDecl::isSuperclassOf(ClassDecl *other) const {

0 commit comments

Comments
 (0)