Skip to content

AST: Refactor SelfReferenceKind in preparation for #33767 #34249

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 2 commits into from
Oct 13, 2020
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
110 changes: 62 additions & 48 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -3741,62 +3741,79 @@ class ClassDecl final : public NominalTypeDecl {
}
};

/// A convenience wrapper around the \c SelfReferencePosition::Kind enum.
struct SelfReferencePosition final {
enum Kind : uint8_t { None, Covariant, Contravariant, Invariant };

/// Describes whether a requirement refers to 'Self', for use in the
/// is-inheritable and is-available-existential checks.
struct SelfReferenceKind {
bool result;
bool parameter;
bool requirement;
bool other;
private:
Kind kind;

/// The type does not refer to 'Self' at all.
static SelfReferenceKind None() {
return SelfReferenceKind(false, false, false, false);
public:
SelfReferencePosition(Kind kind) : kind(kind) {}

SelfReferencePosition flipped() const {
switch (kind) {
case None:
case Invariant:
return *this;
case Covariant:
return Contravariant;
case Contravariant:
return Covariant;
}
}

/// The type refers to 'Self', but only as the type of a property or
/// the result type of a method/subscript.
static SelfReferenceKind Result() {
return SelfReferenceKind(true, false, false, false);
}
explicit operator bool() const { return kind > None; }

/// The type refers to 'Self', but only as the parameter type
/// of a method/subscript.
static SelfReferenceKind Parameter() {
return SelfReferenceKind(false, true, false, false);
}
operator Kind() const { return kind; }
};

/// The type refers to 'Self' within a same-type requiement.
static SelfReferenceKind Requirement() {
return SelfReferenceKind(false, false, true, false);
}
/// Describes the least favorable positions at which a requirement refers
/// to 'Self' in terms of variance, for use in the is-inheritable and
/// is-available-existential checks.
struct SelfReferenceInfo final {
using Position = SelfReferencePosition;

bool hasCovariantSelfResult;
Position selfRef;
Position assocTypeRef;

/// The type refers to 'Self' in a position that is invariant.
static SelfReferenceKind Other() {
return SelfReferenceKind(false, false, false, true);
/// A reference to 'Self'.
static SelfReferenceInfo forSelfRef(Position position) {
assert(position);
return SelfReferenceInfo(false, position, Position::None);
}

SelfReferenceKind flip() const {
return SelfReferenceKind(parameter, result, requirement, other);
/// A reference to 'Self' through an associated type.
static SelfReferenceInfo forAssocTypeRef(Position position) {
assert(position);
return SelfReferenceInfo(false, Position::None, position);
}

SelfReferenceKind operator|=(SelfReferenceKind kind) {
result |= kind.result;
requirement |= kind.requirement;
parameter |= kind.parameter;
other |= kind.other;
SelfReferenceInfo operator|=(const SelfReferenceInfo &pos) {
hasCovariantSelfResult |= pos.hasCovariantSelfResult;
if (pos.selfRef > selfRef) {
selfRef = pos.selfRef;
}
if (pos.assocTypeRef > assocTypeRef) {
assocTypeRef = pos.assocTypeRef;
}
return *this;
}

operator bool() const {
return result || parameter || requirement || other;
explicit operator bool() const {
return hasCovariantSelfResult || selfRef || assocTypeRef;
}

SelfReferenceInfo()
: hasCovariantSelfResult(false), selfRef(Position::None),
assocTypeRef(Position::None) {}

private:
SelfReferenceKind(bool result, bool parameter, bool requirement, bool other)
: result(result), parameter(parameter), requirement(requirement),
other(other) { }
SelfReferenceInfo(bool hasCovariantSelfResult, Position selfRef,
Position assocTypeRef)
: hasCovariantSelfResult(hasCovariantSelfResult), selfRef(selfRef),
assocTypeRef(assocTypeRef) {}
};

/// The set of known protocols for which derived conformances are supported.
Expand Down Expand Up @@ -3978,15 +3995,12 @@ class ProtocolDecl final : public NominalTypeDecl {

/// Find direct Self references within the given requirement.
///
/// \param allowCovariantParameters If true, 'Self' is assumed to be
/// covariant anywhere; otherwise, only in the return type of the top-level
/// function type.
///
/// \param skipAssocTypes If true, associated types of 'Self' are ignored;
/// otherwise, they count as an 'other' usage of 'Self'.
SelfReferenceKind findProtocolSelfReferences(const ValueDecl *decl,
bool allowCovariantParameters,
bool skipAssocTypes) const;
/// \param treatNonResultCovariantSelfAsInvariant If true, 'Self' is only
/// assumed to be covariant in a top-level non-function type, or in the
/// eventual result type of a top-level function type.
SelfReferenceInfo
findProtocolSelfReferences(const ValueDecl *decl,
bool treatNonResultCovariantSelfAsInvariant) const;

/// Determine whether we are allowed to refer to an existential type
/// conforming to this protocol. This is only permitted if the type of
Expand Down
Loading