Skip to content

Requestify Class Ancestry Flags #26794

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/ASTTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ SWIFT_TYPEID(ResilienceExpansion)
SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>, PropertyWrapperMutability)
SWIFT_TYPEID_NAMED(CustomAttr *, CustomAttr)
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
SWIFT_TYPEID(AncestryFlags)
1 change: 1 addition & 0 deletions include/swift/AST/ASTTypeIDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class VarDecl;
class TypeAliasDecl;
class Type;
struct TypePair;
enum class AncestryFlags : uint8_t;

#define SWIFT_AST_TYPEID_ZONE 1

Expand Down
8 changes: 1 addition & 7 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -554,13 +554,7 @@ class alignas(1 << DeclAlignInBits) Decl {
RawForeignKind : 2,

/// \see ClassDecl::getEmittedMembers()
HasForcedEmittedMembers : 1,

/// Information about the class's ancestry.
Ancestry : 7,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since these are used rather frequently, I would imagine making this request ExternallyCached would still be profitable. Or maybe not...


/// Whether we have computed the above field or not.
AncestryComputed : 1,
HasForcedEmittedMembers : 1,

HasMissingDesignatedInitializers : 1,
HasMissingVTableEntries : 1,
Expand Down
21 changes: 21 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,27 @@ class IsImplicitlyUnwrappedOptionalRequest :
void cacheResult(bool value) const;
};

class ClassAncestryFlagsRequest :
public SimpleRequest<ClassAncestryFlagsRequest,
AncestryFlags (ClassDecl *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<AncestryFlags>
evaluate(Evaluator &evaluator, ClassDecl *value) const;

public:
// Caching.
bool isCached() const { return true; }
};

void simple_display(llvm::raw_ostream &out, AncestryFlags value);

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
3 changes: 2 additions & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ SWIFT_TYPEID(RequiresOpaqueModifyCoroutineRequest)
SWIFT_TYPEID(IsAccessorTransparentRequest)
SWIFT_TYPEID(SynthesizeAccessorRequest)
SWIFT_TYPEID(EmittedMembersRequest)
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
SWIFT_TYPEID(IsImplicitlyUnwrappedOptionalRequest)
SWIFT_TYPEID(ClassAncestryFlagsRequest)
55 changes: 43 additions & 12 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3820,8 +3820,6 @@ ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
= static_cast<unsigned>(CircularityCheck::Unchecked);
Bits.ClassDecl.InheritsSuperclassInits = 0;
Bits.ClassDecl.RawForeignKind = 0;
Bits.ClassDecl.Ancestry = 0;
Bits.ClassDecl.AncestryComputed = 0;
Bits.ClassDecl.HasMissingDesignatedInitializers = 0;
Bits.ClassDecl.HasMissingVTableEntries = 0;
Bits.ClassDecl.IsIncompatibleWithWeakReferences = 0;
Expand Down Expand Up @@ -3956,16 +3954,20 @@ bool ClassDecl::inheritsSuperclassInitializers() {
return Bits.ClassDecl.InheritsSuperclassInits;
}

AncestryOptions ClassDecl::checkAncestry() const {
// See if we've already computed this.
if (Bits.ClassDecl.AncestryComputed)
return AncestryOptions(Bits.ClassDecl.Ancestry);

AncestryOptions ClassDecl::checkAncestry() const {
return AncestryOptions(evaluateOrDefault(getASTContext().evaluator,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you have the request return AncestryOptions instead of AncestryFlags to avoid the coercion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OptionSet doesn’t have an equality operator for good reason (see the file). I’m sure Sufficiently Magic Template Nonsense will let us do this eventually.

ClassAncestryFlagsRequest{const_cast<ClassDecl *>(this)},
AncestryFlags()));
}

llvm::Expected<AncestryFlags>
ClassAncestryFlagsRequest::evaluate(Evaluator &evaluator, ClassDecl *value) const {
llvm::SmallPtrSet<const ClassDecl *, 8> visited;

AncestryOptions result;
const ClassDecl *CD = this;
auto *M = getParentModule();
const ClassDecl *CD = value;
auto *M = value->getParentModule();

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

// Save the result for later.
const_cast<ClassDecl *>(this)->Bits.ClassDecl.Ancestry = result.toRaw();
const_cast<ClassDecl *>(this)->Bits.ClassDecl.AncestryComputed = 1;
return result;
return AncestryFlags(result.toRaw());
}

void swift::simple_display(llvm::raw_ostream &out, AncestryFlags value) {
AncestryOptions opts(value);
out << "{ ";
// If we have more than one bit set, we need to print the separator.
bool wantsSeparator = false;
auto printBit = [&wantsSeparator, &out](bool val, StringRef name) {
if (wantsSeparator) {
out << ", ";
}

if (!wantsSeparator) {
wantsSeparator = true;
}

out << name;
if (val) {
out << " = true";
} else {
out << " = false";
}
};
printBit(opts.contains(AncestryFlags::ObjC), "ObjC");
printBit(opts.contains(AncestryFlags::ObjCMembers), "ObjCMembers");
printBit(opts.contains(AncestryFlags::Generic), "Generic");
printBit(opts.contains(AncestryFlags::Resilient), "Resilient");
printBit(opts.contains(AncestryFlags::ResilientOther), "ResilientOther");
printBit(opts.contains(AncestryFlags::ClangImported), "ClangImported");
printBit(opts.contains(AncestryFlags::RequiresStoredPropertyInits),
"RequiresStoredPropertyInits");
out << " }";
}

bool ClassDecl::isSuperclassOf(ClassDecl *other) const {
Expand Down