Skip to content

Requestify Operator Precedence Groups and Precedence Group Lookup #27351

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 4 commits into from
Sep 25, 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
2 changes: 2 additions & 0 deletions include/swift/AST/ASTTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,14 @@ SWIFT_TYPEID_NAMED(Decl *, Decl)
SWIFT_TYPEID_NAMED(GenericParamList *, GenericParamList)
SWIFT_TYPEID_NAMED(GenericSignature *, GenericSignature)
SWIFT_TYPEID_NAMED(GenericTypeParamType *, GenericTypeParamType)
SWIFT_TYPEID_NAMED(InfixOperatorDecl *, InfixOperatorDecl)
SWIFT_TYPEID_NAMED(IterableDeclContext *, IterableDeclContext)
SWIFT_TYPEID_NAMED(ModuleDecl *, ModuleDecl)
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
SWIFT_TYPEID_NAMED(OperatorDecl *, OperatorDecl)
SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>,
PropertyWrapperMutability)
SWIFT_TYPEID_NAMED(PrecedenceGroupDecl *, PrecedenceGroupDecl)
SWIFT_TYPEID_NAMED(ProtocolDecl *, ProtocolDecl)
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
SWIFT_TYPEID_NAMED(ValueDecl *, ValueDecl)
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/ASTTypeIDs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class Decl;
class GenericParamList;
class GenericSignature;
class GenericTypeParamType;
class InfixOperatorDecl;
class IterableDeclContext;
class ModuleDecl;
class NominalTypeDecl;
class OperatorDecl;
class PrecedenceGroupDecl;
struct PropertyWrapperBackingPropertyInfo;
struct PropertyWrapperTypeInfo;
enum class CtorInitializerKind;
Expand Down
14 changes: 1 addition & 13 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6909,7 +6909,6 @@ class OperatorDecl : public Decl {
/// \endcode
class InfixOperatorDecl : public OperatorDecl {
SourceLoc ColonLoc;
PrecedenceGroupDecl *PrecedenceGroup = nullptr;

public:
InfixOperatorDecl(DeclContext *DC, SourceLoc operatorLoc, Identifier name,
Expand All @@ -6920,14 +6919,6 @@ class InfixOperatorDecl : public OperatorDecl {
identifiers, identifierLocs),
ColonLoc(colonLoc) {}

InfixOperatorDecl(DeclContext *DC, SourceLoc operatorLoc, Identifier name,
SourceLoc nameLoc, SourceLoc colonLoc,
PrecedenceGroupDecl *precedenceGroup,
ArrayRef<NominalTypeDecl *> designatedNominalTypes)
: OperatorDecl(DeclKind::InfixOperator, DC, operatorLoc, name, nameLoc,
designatedNominalTypes),
ColonLoc(colonLoc), PrecedenceGroup(precedenceGroup) {}

SourceLoc getEndLoc() const {
auto identifierLocs = getIdentifierLocs();
if (identifierLocs.empty())
Expand All @@ -6942,10 +6933,7 @@ class InfixOperatorDecl : public OperatorDecl {

SourceLoc getColonLoc() const { return ColonLoc; }

PrecedenceGroupDecl *getPrecedenceGroup() const { return PrecedenceGroup; }
void setPrecedenceGroup(PrecedenceGroupDecl *PGD) {
PrecedenceGroup = PGD;
}
PrecedenceGroupDecl *getPrecedenceGroup() const;

/// True if this decl's attributes conflict with those declared by another
/// operator.
Expand Down
51 changes: 51 additions & 0 deletions include/swift/AST/NameLookupRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "swift/AST/SimpleRequest.h"
#include "swift/AST/ASTTypeIDs.h"
#include "swift/Basic/Statistic.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/TinyPtrVector.h"

namespace swift {
Expand Down Expand Up @@ -273,6 +274,56 @@ class GenericParamListRequest :
void cacheResult(GenericParamList *value) const;
};

struct PrecedenceGroupDescriptor {
DeclContext *dc;
Identifier ident;
SourceLoc nameLoc;

SourceLoc getLoc() const;

friend llvm::hash_code hash_value(const PrecedenceGroupDescriptor &owner) {
return hash_combine(llvm::hash_value(owner.dc),
llvm::hash_value(owner.ident.getAsOpaquePointer()),
llvm::hash_value(owner.nameLoc.getOpaquePointerValue()));
}

friend bool operator==(const PrecedenceGroupDescriptor &lhs,
const PrecedenceGroupDescriptor &rhs) {
return lhs.dc == rhs.dc &&
lhs.ident == rhs.ident &&
lhs.nameLoc == rhs.nameLoc;
}

friend bool operator!=(const PrecedenceGroupDescriptor &lhs,
const PrecedenceGroupDescriptor &rhs) {
return !(lhs == rhs);
}
};

void simple_display(llvm::raw_ostream &out, const PrecedenceGroupDescriptor &d);

class LookupPrecedenceGroupRequest
: public SimpleRequest<LookupPrecedenceGroupRequest,
PrecedenceGroupDecl *(PrecedenceGroupDescriptor),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<PrecedenceGroupDecl *>
evaluate(Evaluator &evaluator, PrecedenceGroupDescriptor descriptor) const;

public:
// Source location
SourceLoc getNearestLoc() const;

// Separate caching.
bool isCached() const { return true; }
};

#define SWIFT_TYPEID_ZONE NameLookup
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
#include "swift/Basic/DefineTypeIDZone.h"
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/NameLookupTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ SWIFT_REQUEST(NameLookup, InheritedDeclsReferencedRequest,
DirectlyReferencedTypeDecls(
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned),
Uncached, HasNearestLocation)
SWIFT_REQUEST(NameLookup, LookupPrecedenceGroupRequest,
PrecedenceGroupDecl *(DeclContext *, Identifier, SourceLoc),
Cached, NoLocationInfo)
SWIFT_REQUEST(NameLookup, SelfBoundsFromWhereClauseRequest,
SelfBounds(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>),
Uncached, NoLocationInfo)
Expand Down
20 changes: 20 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class AbstractStorageDecl;
class AccessorDecl;
enum class AccessorKind;
class GenericParamList;
class PrecedenceGroupDecl;
struct PropertyWrapperBackingPropertyInfo;
struct PropertyWrapperMutability;
class RequirementRepr;
Expand Down Expand Up @@ -1209,6 +1210,25 @@ class UnderlyingTypeRequest :
void cacheResult(Type value) const;
};

class OperatorPrecedenceGroupRequest
: public SimpleRequest<OperatorPrecedenceGroupRequest,
PrecedenceGroupDecl *(InfixOperatorDecl *),
CacheKind::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
llvm::Expected<PrecedenceGroupDecl *>
evaluate(Evaluator &evaluator, InfixOperatorDecl *PGD) const;

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

// Allow AnyValue to compare two Type values, even though Type doesn't
// support ==.
template<>
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ SWIFT_REQUEST(TypeChecker, MangleLocalTypeDeclRequest,
SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
OpaqueReadOwnership(AbstractStorageDecl *), SeparatelyCached,
NoLocationInfo)
SWIFT_REQUEST(TypeChecker, OperatorPrecedenceGroupRequest,
PrecedenceGroupDecl *(PrecedenceGroupDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, OverriddenDeclsRequest,
llvm::TinyPtrVector<ValueDecl *>(ValueDecl *), SeparatelyCached,
NoLocationInfo)
Expand Down
22 changes: 22 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7471,6 +7471,28 @@ PrecedenceGroupDecl::PrecedenceGroupDecl(DeclContext *dc,
lowerThan.size() * sizeof(Relation));
}

llvm::Expected<PrecedenceGroupDecl *> LookupPrecedenceGroupRequest::evaluate(
Evaluator &eval, PrecedenceGroupDescriptor descriptor) const {
auto *dc = descriptor.dc;
PrecedenceGroupDecl *group = nullptr;
if (auto sf = dc->getParentSourceFile()) {
bool cascading = dc->isCascadingContextForLookup(false);
group = sf->lookupPrecedenceGroup(descriptor.ident, cascading,
descriptor.nameLoc);
} else {
group = dc->getParentModule()->lookupPrecedenceGroup(descriptor.ident,
descriptor.nameLoc);
}
return group;
}

PrecedenceGroupDecl *InfixOperatorDecl::getPrecedenceGroup() const {
return evaluateOrDefault(
getASTContext().evaluator,
OperatorPrecedenceGroupRequest{const_cast<InfixOperatorDecl *>(this)},
nullptr);
}

bool FuncDecl::isDeferBody() const {
return getName() == getASTContext().getIdentifier("$defer");
}
Expand Down
19 changes: 19 additions & 0 deletions lib/AST/NameLookupRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,25 @@ void GenericParamListRequest::cacheResult(GenericParamList *params) const {
}


//----------------------------------------------------------------------------//
// LookupPrecedenceGroupRequest computation.
//----------------------------------------------------------------------------//

SourceLoc LookupPrecedenceGroupRequest::getNearestLoc() const {
auto &desc = std::get<0>(getStorage());
return desc.getLoc();
}

SourceLoc PrecedenceGroupDescriptor::getLoc() const {
return nameLoc;
}

void swift::simple_display(llvm::raw_ostream &out,
const PrecedenceGroupDescriptor &desc) {
out << "precedence group " << desc.ident << " at ";
desc.nameLoc.print(out, desc.dc->getASTContext().SourceMgr);
}

// Define request evaluation functions for each of the name lookup requests.
static AbstractRequestFunction *nameLookupRequestFunctions[] = {
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,7 @@ void GenericSignatureRequest::cacheResult(GenericSignature *value) const {
}

//----------------------------------------------------------------------------//
// GenericSignatureRequest computation.
// InferredGenericSignatureRequest computation.
//----------------------------------------------------------------------------//

void InferredGenericSignatureRequest::noteCycleStep(DiagnosticEngine &d) const {
Expand Down
18 changes: 9 additions & 9 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7342,7 +7342,7 @@ bool swift::exprNeedsParensInsideFollowingOperator(
TypeChecker &TC, DeclContext *DC, Expr *expr,
PrecedenceGroupDecl *followingPG) {
if (expr->isInfixOperator()) {
auto exprPG = TC.lookupPrecedenceGroupForInfixOperator(DC, expr);
auto exprPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC, expr);
if (!exprPG) return true;

return TC.Context.associateInfixOperators(exprPG, followingPG)
Expand Down Expand Up @@ -7376,7 +7376,8 @@ bool swift::exprNeedsParensOutsideFollowingOperator(
return false;

if (parent->isInfixOperator()) {
auto parentPG = TC.lookupPrecedenceGroupForInfixOperator(DC, parent);
auto parentPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC,
parent);
if (!parentPG) return true;

// If the index is 0, this is on the LHS of the parent.
Expand All @@ -7395,20 +7396,19 @@ bool swift::exprNeedsParensOutsideFollowingOperator(
bool swift::exprNeedsParensBeforeAddingNilCoalescing(TypeChecker &TC,
DeclContext *DC,
Expr *expr) {
auto asPG =
TC.lookupPrecedenceGroup(DC, DC->getASTContext().Id_NilCoalescingPrecedence,
SourceLoc());
if (!asPG) return true;
auto asPG = TypeChecker::lookupPrecedenceGroup(
DC, DC->getASTContext().Id_NilCoalescingPrecedence, SourceLoc());
if (!asPG)
return true;
return exprNeedsParensInsideFollowingOperator(TC, DC, expr, asPG);
}

bool swift::exprNeedsParensAfterAddingNilCoalescing(TypeChecker &TC,
DeclContext *DC,
Expr *expr,
Expr *rootExpr) {
auto asPG =
TC.lookupPrecedenceGroup(DC, DC->getASTContext().Id_NilCoalescingPrecedence,
SourceLoc());
auto asPG = TypeChecker::lookupPrecedenceGroup(
DC, DC->getASTContext().Id_NilCoalescingPrecedence, SourceLoc());
if (!asPG) return true;
return exprNeedsParensOutsideFollowingOperator(TC, DC, expr, rootExpr, asPG);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Sema/CSDiagnostics.h
Original file line number Diff line number Diff line change
Expand Up @@ -794,7 +794,7 @@ class MissingExplicitConversionFailure final : public ContextualFailure {
auto *DC = getDC();
auto &TC = getTypeChecker();

auto asPG = TC.lookupPrecedenceGroup(
auto asPG = TypeChecker::lookupPrecedenceGroup(
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc());
if (!asPG)
return true;
Expand All @@ -805,7 +805,7 @@ class MissingExplicitConversionFailure final : public ContextualFailure {
auto *DC = getDC();
auto &TC = getTypeChecker();

auto asPG = TC.lookupPrecedenceGroup(
auto asPG = TypeChecker::lookupPrecedenceGroup(
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc());
if (!asPG)
return true;
Expand Down
Loading