Skip to content

Commit c5a27ef

Browse files
authored
Merge pull request #27351 from CodaFi/not-a-good-start-boris
Requestify Operator Precedence Groups and Precedence Group Lookup
2 parents dcf607d + d992ff3 commit c5a27ef

17 files changed

+291
-202
lines changed

include/swift/AST/ASTTypeIDZone.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ SWIFT_TYPEID_NAMED(Decl *, Decl)
2828
SWIFT_TYPEID_NAMED(GenericParamList *, GenericParamList)
2929
SWIFT_TYPEID_NAMED(GenericSignature *, GenericSignature)
3030
SWIFT_TYPEID_NAMED(GenericTypeParamType *, GenericTypeParamType)
31+
SWIFT_TYPEID_NAMED(InfixOperatorDecl *, InfixOperatorDecl)
3132
SWIFT_TYPEID_NAMED(IterableDeclContext *, IterableDeclContext)
3233
SWIFT_TYPEID_NAMED(ModuleDecl *, ModuleDecl)
3334
SWIFT_TYPEID_NAMED(NominalTypeDecl *, NominalTypeDecl)
3435
SWIFT_TYPEID_NAMED(OperatorDecl *, OperatorDecl)
3536
SWIFT_TYPEID_NAMED(Optional<PropertyWrapperMutability>,
3637
PropertyWrapperMutability)
38+
SWIFT_TYPEID_NAMED(PrecedenceGroupDecl *, PrecedenceGroupDecl)
3739
SWIFT_TYPEID_NAMED(ProtocolDecl *, ProtocolDecl)
3840
SWIFT_TYPEID_NAMED(TypeAliasDecl *, TypeAliasDecl)
3941
SWIFT_TYPEID_NAMED(ValueDecl *, ValueDecl)

include/swift/AST/ASTTypeIDs.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ class Decl;
2626
class GenericParamList;
2727
class GenericSignature;
2828
class GenericTypeParamType;
29+
class InfixOperatorDecl;
2930
class IterableDeclContext;
3031
class ModuleDecl;
3132
class NominalTypeDecl;
3233
class OperatorDecl;
34+
class PrecedenceGroupDecl;
3335
struct PropertyWrapperBackingPropertyInfo;
3436
struct PropertyWrapperTypeInfo;
3537
enum class CtorInitializerKind;

include/swift/AST/Decl.h

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6909,7 +6909,6 @@ class OperatorDecl : public Decl {
69096909
/// \endcode
69106910
class InfixOperatorDecl : public OperatorDecl {
69116911
SourceLoc ColonLoc;
6912-
PrecedenceGroupDecl *PrecedenceGroup = nullptr;
69136912

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

6923-
InfixOperatorDecl(DeclContext *DC, SourceLoc operatorLoc, Identifier name,
6924-
SourceLoc nameLoc, SourceLoc colonLoc,
6925-
PrecedenceGroupDecl *precedenceGroup,
6926-
ArrayRef<NominalTypeDecl *> designatedNominalTypes)
6927-
: OperatorDecl(DeclKind::InfixOperator, DC, operatorLoc, name, nameLoc,
6928-
designatedNominalTypes),
6929-
ColonLoc(colonLoc), PrecedenceGroup(precedenceGroup) {}
6930-
69316922
SourceLoc getEndLoc() const {
69326923
auto identifierLocs = getIdentifierLocs();
69336924
if (identifierLocs.empty())
@@ -6942,10 +6933,7 @@ class InfixOperatorDecl : public OperatorDecl {
69426933

69436934
SourceLoc getColonLoc() const { return ColonLoc; }
69446935

6945-
PrecedenceGroupDecl *getPrecedenceGroup() const { return PrecedenceGroup; }
6946-
void setPrecedenceGroup(PrecedenceGroupDecl *PGD) {
6947-
PrecedenceGroup = PGD;
6948-
}
6936+
PrecedenceGroupDecl *getPrecedenceGroup() const;
69496937

69506938
/// True if this decl's attributes conflict with those declared by another
69516939
/// operator.

include/swift/AST/NameLookupRequests.h

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "swift/AST/SimpleRequest.h"
2020
#include "swift/AST/ASTTypeIDs.h"
2121
#include "swift/Basic/Statistic.h"
22+
#include "llvm/ADT/Hashing.h"
2223
#include "llvm/ADT/TinyPtrVector.h"
2324

2425
namespace swift {
@@ -273,6 +274,56 @@ class GenericParamListRequest :
273274
void cacheResult(GenericParamList *value) const;
274275
};
275276

277+
struct PrecedenceGroupDescriptor {
278+
DeclContext *dc;
279+
Identifier ident;
280+
SourceLoc nameLoc;
281+
282+
SourceLoc getLoc() const;
283+
284+
friend llvm::hash_code hash_value(const PrecedenceGroupDescriptor &owner) {
285+
return hash_combine(llvm::hash_value(owner.dc),
286+
llvm::hash_value(owner.ident.getAsOpaquePointer()),
287+
llvm::hash_value(owner.nameLoc.getOpaquePointerValue()));
288+
}
289+
290+
friend bool operator==(const PrecedenceGroupDescriptor &lhs,
291+
const PrecedenceGroupDescriptor &rhs) {
292+
return lhs.dc == rhs.dc &&
293+
lhs.ident == rhs.ident &&
294+
lhs.nameLoc == rhs.nameLoc;
295+
}
296+
297+
friend bool operator!=(const PrecedenceGroupDescriptor &lhs,
298+
const PrecedenceGroupDescriptor &rhs) {
299+
return !(lhs == rhs);
300+
}
301+
};
302+
303+
void simple_display(llvm::raw_ostream &out, const PrecedenceGroupDescriptor &d);
304+
305+
class LookupPrecedenceGroupRequest
306+
: public SimpleRequest<LookupPrecedenceGroupRequest,
307+
PrecedenceGroupDecl *(PrecedenceGroupDescriptor),
308+
CacheKind::Cached> {
309+
public:
310+
using SimpleRequest::SimpleRequest;
311+
312+
private:
313+
friend SimpleRequest;
314+
315+
// Evaluation.
316+
llvm::Expected<PrecedenceGroupDecl *>
317+
evaluate(Evaluator &evaluator, PrecedenceGroupDescriptor descriptor) const;
318+
319+
public:
320+
// Source location
321+
SourceLoc getNearestLoc() const;
322+
323+
// Separate caching.
324+
bool isCached() const { return true; }
325+
};
326+
276327
#define SWIFT_TYPEID_ZONE NameLookup
277328
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
278329
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ SWIFT_REQUEST(NameLookup, InheritedDeclsReferencedRequest,
3030
DirectlyReferencedTypeDecls(
3131
llvm::PointerUnion<TypeDecl *, ExtensionDecl *>, unsigned),
3232
Uncached, HasNearestLocation)
33+
SWIFT_REQUEST(NameLookup, LookupPrecedenceGroupRequest,
34+
PrecedenceGroupDecl *(DeclContext *, Identifier, SourceLoc),
35+
Cached, NoLocationInfo)
3336
SWIFT_REQUEST(NameLookup, SelfBoundsFromWhereClauseRequest,
3437
SelfBounds(llvm::PointerUnion<TypeDecl *, ExtensionDecl *>),
3538
Uncached, NoLocationInfo)

include/swift/AST/TypeCheckRequests.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class AbstractStorageDecl;
3333
class AccessorDecl;
3434
enum class AccessorKind;
3535
class GenericParamList;
36+
class PrecedenceGroupDecl;
3637
struct PropertyWrapperBackingPropertyInfo;
3738
struct PropertyWrapperMutability;
3839
class RequirementRepr;
@@ -1209,6 +1210,25 @@ class UnderlyingTypeRequest :
12091210
void cacheResult(Type value) const;
12101211
};
12111212

1213+
class OperatorPrecedenceGroupRequest
1214+
: public SimpleRequest<OperatorPrecedenceGroupRequest,
1215+
PrecedenceGroupDecl *(InfixOperatorDecl *),
1216+
CacheKind::Cached> {
1217+
public:
1218+
using SimpleRequest::SimpleRequest;
1219+
1220+
private:
1221+
friend SimpleRequest;
1222+
1223+
// Evaluation.
1224+
llvm::Expected<PrecedenceGroupDecl *>
1225+
evaluate(Evaluator &evaluator, InfixOperatorDecl *PGD) const;
1226+
1227+
public:
1228+
// Separate caching.
1229+
bool isCached() const { return true; }
1230+
};
1231+
12121232
// Allow AnyValue to compare two Type values, even though Type doesn't
12131233
// support ==.
12141234
template<>

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ SWIFT_REQUEST(TypeChecker, MangleLocalTypeDeclRequest,
8585
SWIFT_REQUEST(TypeChecker, OpaqueReadOwnershipRequest,
8686
OpaqueReadOwnership(AbstractStorageDecl *), SeparatelyCached,
8787
NoLocationInfo)
88+
SWIFT_REQUEST(TypeChecker, OperatorPrecedenceGroupRequest,
89+
PrecedenceGroupDecl *(PrecedenceGroupDecl *),
90+
Cached, NoLocationInfo)
8891
SWIFT_REQUEST(TypeChecker, OverriddenDeclsRequest,
8992
llvm::TinyPtrVector<ValueDecl *>(ValueDecl *), SeparatelyCached,
9093
NoLocationInfo)

lib/AST/Decl.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7471,6 +7471,28 @@ PrecedenceGroupDecl::PrecedenceGroupDecl(DeclContext *dc,
74717471
lowerThan.size() * sizeof(Relation));
74727472
}
74737473

7474+
llvm::Expected<PrecedenceGroupDecl *> LookupPrecedenceGroupRequest::evaluate(
7475+
Evaluator &eval, PrecedenceGroupDescriptor descriptor) const {
7476+
auto *dc = descriptor.dc;
7477+
PrecedenceGroupDecl *group = nullptr;
7478+
if (auto sf = dc->getParentSourceFile()) {
7479+
bool cascading = dc->isCascadingContextForLookup(false);
7480+
group = sf->lookupPrecedenceGroup(descriptor.ident, cascading,
7481+
descriptor.nameLoc);
7482+
} else {
7483+
group = dc->getParentModule()->lookupPrecedenceGroup(descriptor.ident,
7484+
descriptor.nameLoc);
7485+
}
7486+
return group;
7487+
}
7488+
7489+
PrecedenceGroupDecl *InfixOperatorDecl::getPrecedenceGroup() const {
7490+
return evaluateOrDefault(
7491+
getASTContext().evaluator,
7492+
OperatorPrecedenceGroupRequest{const_cast<InfixOperatorDecl *>(this)},
7493+
nullptr);
7494+
}
7495+
74747496
bool FuncDecl::isDeferBody() const {
74757497
return getName() == getASTContext().getIdentifier("$defer");
74767498
}

lib/AST/NameLookupRequests.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,25 @@ void GenericParamListRequest::cacheResult(GenericParamList *params) const {
127127
}
128128

129129

130+
//----------------------------------------------------------------------------//
131+
// LookupPrecedenceGroupRequest computation.
132+
//----------------------------------------------------------------------------//
133+
134+
SourceLoc LookupPrecedenceGroupRequest::getNearestLoc() const {
135+
auto &desc = std::get<0>(getStorage());
136+
return desc.getLoc();
137+
}
138+
139+
SourceLoc PrecedenceGroupDescriptor::getLoc() const {
140+
return nameLoc;
141+
}
142+
143+
void swift::simple_display(llvm::raw_ostream &out,
144+
const PrecedenceGroupDescriptor &desc) {
145+
out << "precedence group " << desc.ident << " at ";
146+
desc.nameLoc.print(out, desc.dc->getASTContext().SourceMgr);
147+
}
148+
130149
// Define request evaluation functions for each of the name lookup requests.
131150
static AbstractRequestFunction *nameLookupRequestFunctions[] = {
132151
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \

lib/AST/TypeCheckRequests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,7 +827,7 @@ void GenericSignatureRequest::cacheResult(GenericSignature *value) const {
827827
}
828828

829829
//----------------------------------------------------------------------------//
830-
// GenericSignatureRequest computation.
830+
// InferredGenericSignatureRequest computation.
831831
//----------------------------------------------------------------------------//
832832

833833
void InferredGenericSignatureRequest::noteCycleStep(DiagnosticEngine &d) const {

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7342,7 +7342,7 @@ bool swift::exprNeedsParensInsideFollowingOperator(
73427342
TypeChecker &TC, DeclContext *DC, Expr *expr,
73437343
PrecedenceGroupDecl *followingPG) {
73447344
if (expr->isInfixOperator()) {
7345-
auto exprPG = TC.lookupPrecedenceGroupForInfixOperator(DC, expr);
7345+
auto exprPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC, expr);
73467346
if (!exprPG) return true;
73477347

73487348
return TC.Context.associateInfixOperators(exprPG, followingPG)
@@ -7376,7 +7376,8 @@ bool swift::exprNeedsParensOutsideFollowingOperator(
73767376
return false;
73777377

73787378
if (parent->isInfixOperator()) {
7379-
auto parentPG = TC.lookupPrecedenceGroupForInfixOperator(DC, parent);
7379+
auto parentPG = TypeChecker::lookupPrecedenceGroupForInfixOperator(DC,
7380+
parent);
73807381
if (!parentPG) return true;
73817382

73827383
// If the index is 0, this is on the LHS of the parent.
@@ -7395,20 +7396,19 @@ bool swift::exprNeedsParensOutsideFollowingOperator(
73957396
bool swift::exprNeedsParensBeforeAddingNilCoalescing(TypeChecker &TC,
73967397
DeclContext *DC,
73977398
Expr *expr) {
7398-
auto asPG =
7399-
TC.lookupPrecedenceGroup(DC, DC->getASTContext().Id_NilCoalescingPrecedence,
7400-
SourceLoc());
7401-
if (!asPG) return true;
7399+
auto asPG = TypeChecker::lookupPrecedenceGroup(
7400+
DC, DC->getASTContext().Id_NilCoalescingPrecedence, SourceLoc());
7401+
if (!asPG)
7402+
return true;
74027403
return exprNeedsParensInsideFollowingOperator(TC, DC, expr, asPG);
74037404
}
74047405

74057406
bool swift::exprNeedsParensAfterAddingNilCoalescing(TypeChecker &TC,
74067407
DeclContext *DC,
74077408
Expr *expr,
74087409
Expr *rootExpr) {
7409-
auto asPG =
7410-
TC.lookupPrecedenceGroup(DC, DC->getASTContext().Id_NilCoalescingPrecedence,
7411-
SourceLoc());
7410+
auto asPG = TypeChecker::lookupPrecedenceGroup(
7411+
DC, DC->getASTContext().Id_NilCoalescingPrecedence, SourceLoc());
74127412
if (!asPG) return true;
74137413
return exprNeedsParensOutsideFollowingOperator(TC, DC, expr, rootExpr, asPG);
74147414
}

lib/Sema/CSDiagnostics.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ class MissingExplicitConversionFailure final : public ContextualFailure {
794794
auto *DC = getDC();
795795
auto &TC = getTypeChecker();
796796

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

808-
auto asPG = TC.lookupPrecedenceGroup(
808+
auto asPG = TypeChecker::lookupPrecedenceGroup(
809809
DC, DC->getASTContext().Id_CastingPrecedence, SourceLoc());
810810
if (!asPG)
811811
return true;

0 commit comments

Comments
 (0)