Skip to content

Requestify whether a decl is an ABI compatible override #27524

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
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
20 changes: 2 additions & 18 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -340,12 +340,7 @@ class alignas(1 << DeclAlignInBits) Decl {
IsUserAccessible : 1
);

SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1+1+1,
/// Whether a keypath component can directly reference this storage,
/// or if it must use the overridden declaration instead.
HasComputedValidKeyPathComponent : 1,
ValidKeyPathComponent : 1,

SWIFT_INLINE_BITFIELD(AbstractStorageDecl, ValueDecl, 1,
/// Whether this property is a type property (currently unfortunately
/// called 'static').
IsStatic : 1
Expand Down Expand Up @@ -4535,8 +4530,6 @@ class AbstractStorageDecl : public ValueDecl {
Bits.AbstractStorageDecl.IsStatic = IsStatic;
}

void computeIsValidKeyPathComponent();

OpaqueTypeDecl *OpaqueReturn = nullptr;

public:
Expand Down Expand Up @@ -4779,18 +4772,9 @@ class AbstractStorageDecl : public ValueDecl {
/// property from the given module?
bool isResilient(ModuleDecl *M, ResilienceExpansion expansion) const;

void setIsValidKeyPathComponent(bool value) {
Bits.AbstractStorageDecl.HasComputedValidKeyPathComponent = true;
Bits.AbstractStorageDecl.ValidKeyPathComponent = value;
}

/// True if the storage can be referenced by a keypath directly.
/// Otherwise, its override must be referenced.
bool isValidKeyPathComponent() const {
if (!Bits.AbstractStorageDecl.HasComputedValidKeyPathComponent)
const_cast<AbstractStorageDecl *>(this)->computeIsValidKeyPathComponent();
return Bits.AbstractStorageDecl.ValidKeyPathComponent;
}
bool isValidKeyPathComponent() const;

/// True if the storage exports a property descriptor for key paths in
/// other modules.
Expand Down
17 changes: 17 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1255,6 +1255,23 @@ class EnumRawValuesRequest :
void cacheResult(bool value) const;
};

class IsABICompatibleOverrideRequest
: public SimpleRequest<IsABICompatibleOverrideRequest, bool(ValueDecl *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<bool> evaluate(Evaluator &evaluator, ValueDecl *decl) const;

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

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,5 @@ SWIFT_REQUEST(TypeChecker, UnderlyingTypeRequest, Type(TypeAliasDecl *),
SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, USRGenerationRequest, std::string(const ValueDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, IsABICompatibleOverrideRequest,
bool(ValueDecl *), Cached, NoLocationInfo)
59 changes: 19 additions & 40 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2142,32 +2142,15 @@ bool AbstractStorageDecl::isResilient(ModuleDecl *M,
llvm_unreachable("bad resilience expansion");
}

static bool isValidKeyPathComponent(AbstractStorageDecl *decl) {
// If this property or subscript is not an override, we can reference it
// from a keypath component.
auto base = decl->getOverriddenDecl();
if (!base)
return true;

// Otherwise, we can only reference it if the type is not ABI-compatible
// with the type of the base.
//
// If the type is ABI compatible with the type of the base, we have to
// reference the base instead.
auto baseInterfaceTy = base->getInterfaceType();
auto derivedInterfaceTy = decl->getInterfaceType();

auto selfInterfaceTy = decl->getDeclContext()->getDeclaredInterfaceType();

auto overrideInterfaceTy = selfInterfaceTy->adjustSuperclassMemberDeclType(
base, decl, baseInterfaceTy);

return !derivedInterfaceTy->matches(overrideInterfaceTy,
TypeMatchFlags::AllowABICompatible);
}

void AbstractStorageDecl::computeIsValidKeyPathComponent() {
setIsValidKeyPathComponent(::isValidKeyPathComponent(this));
bool AbstractStorageDecl::isValidKeyPathComponent() const {
// Check whether we're an ABI compatible override of another property. If we
// are, then the key path should refer to the base decl instead.
auto &ctx = getASTContext();
auto isABICompatibleOverride = evaluateOrDefault(
ctx.evaluator,
IsABICompatibleOverrideRequest{const_cast<AbstractStorageDecl *>(this)},
false);
return !isABICompatibleOverride;
}

bool AbstractStorageDecl::isGetterMutating() const {
Expand Down Expand Up @@ -6588,26 +6571,22 @@ static bool requiresNewVTableEntry(const AbstractFunctionDecl *decl) {
if (decl->isEffectiveLinkageMoreVisibleThan(base))
return true;

// If the method overrides something, we only need a new entry if the
// override has a more general AST type. However an abstraction
// change is OK; we don't want to add a whole new vtable entry just
// because an @in parameter because @owned, or whatever.
auto baseInterfaceTy = base->getInterfaceType();
auto derivedInterfaceTy = decl->getInterfaceType();

using Direction = ASTContext::OverrideGenericSignatureReqCheck;
if (!ctx.overrideGenericSignatureReqsSatisfied(
base, decl, Direction::BaseReqSatisfiedByDerived)) {
return true;
}

auto selfInterfaceTy = decl->getDeclContext()->getDeclaredInterfaceType();

auto overrideInterfaceTy = selfInterfaceTy->adjustSuperclassMemberDeclType(
base, decl, baseInterfaceTy);

return !derivedInterfaceTy->matches(overrideInterfaceTy,
TypeMatchFlags::AllowABICompatible);
// If this method is an ABI compatible override, then we don't need a new
// vtable entry. Otherwise, if it's not ABI compatible, for example if the
// base has a more general AST type, then we need a new entry. Note that an
// abstraction change is OK; we don't want to add a whole new vtable entry
// just because an @in parameter becomes @owned, or whatever.
auto isABICompatibleOverride = evaluateOrDefault(
ctx.evaluator,
IsABICompatibleOverrideRequest{const_cast<AbstractFunctionDecl *>(decl)},
false);
return !isABICompatibleOverride;
}

void AbstractFunctionDecl::computeNeedsNewVTableEntry() {
Expand Down
19 changes: 19 additions & 0 deletions lib/Sema/TypeCheckDeclOverride.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1954,3 +1954,22 @@ OverriddenDeclsRequest::evaluate(Evaluator &evaluator, ValueDecl *decl) const {
return matcher.checkPotentialOverrides(matches,
OverrideCheckingAttempt::PerfectMatch);
}

llvm::Expected<bool>
IsABICompatibleOverrideRequest::evaluate(Evaluator &evaluator,
ValueDecl *decl) const {
auto base = decl->getOverriddenDecl();
if (!base)
return false;

auto baseInterfaceTy = base->getInterfaceType();
auto derivedInterfaceTy = decl->getInterfaceType();

auto selfInterfaceTy = decl->getDeclContext()->getDeclaredInterfaceType();

auto overrideInterfaceTy = selfInterfaceTy->adjustSuperclassMemberDeclType(
base, decl, baseInterfaceTy);

return derivedInterfaceTy->matches(overrideInterfaceTy,
TypeMatchFlags::AllowABICompatible);
}