Skip to content

Commit d4a0b67

Browse files
authored
Merge pull request #30547 from CodaFi/a-quick-game-of-operation
[NFC] Remove SourceFile's Operator Lookup Entrypoints
2 parents 09aee22 + 425bd50 commit d4a0b67

File tree

4 files changed

+52
-66
lines changed

4 files changed

+52
-66
lines changed

include/swift/AST/SourceFile.h

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,25 +461,6 @@ class SourceFile final : public FileUnit {
461461

462462
virtual bool walk(ASTWalker &walker) override;
463463

464-
/// @{
465-
466-
/// Look up the given operator in this file.
467-
///
468-
/// The file must be name-bound already. If the operator is not found, or if
469-
/// there is an ambiguity, returns null.
470-
///
471-
/// \param isCascading If true, the lookup of this operator may affect
472-
/// downstream files.
473-
InfixOperatorDecl *lookupInfixOperator(Identifier name, bool isCascading,
474-
SourceLoc diagLoc = {});
475-
PrefixOperatorDecl *lookupPrefixOperator(Identifier name, bool isCascading,
476-
SourceLoc diagLoc = {});
477-
PostfixOperatorDecl *lookupPostfixOperator(Identifier name, bool isCascading,
478-
SourceLoc diagLoc = {});
479-
PrecedenceGroupDecl *lookupPrecedenceGroup(Identifier name, bool isCascading,
480-
SourceLoc diagLoc = {});
481-
/// @}
482-
483464
ReferencedNameTracker *getReferencedNameTracker() {
484465
return ReferencedNames ? ReferencedNames.getPointer() : nullptr;
485466
}

lib/AST/Module.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1014,8 +1014,7 @@ void SourceFile::setSyntaxRoot(syntax::SourceFileSyntax &&Root) {
10141014

10151015
template<typename OP_DECL>
10161016
static Optional<OP_DECL *>
1017-
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
1018-
OperatorMap<OP_DECL *> SourceFile::*OP_MAP);
1017+
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name);
10191018

10201019
template<typename OP_DECL>
10211020
using ImportedOperatorsMap = llvm::SmallDenseMap<OP_DECL*, bool, 16>;
@@ -1066,8 +1065,7 @@ checkOperatorConflicts(const SourceFile &SF, SourceLoc loc,
10661065
template <typename OP_DECL>
10671066
static Optional<OP_DECL *>
10681067
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
1069-
Identifier Name, bool includePrivate,
1070-
OperatorMap<OP_DECL *> SourceFile::*OP_MAP) {
1068+
Identifier Name, bool includePrivate) {
10711069
switch (File.getKind()) {
10721070
case FileUnitKind::Builtin:
10731071
// The Builtin module declares no operators.
@@ -1084,6 +1082,7 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
10841082
assert(SF.ASTStage >= SourceFile::NameBound);
10851083

10861084
// Look for an operator declaration in the current module.
1085+
const auto OP_MAP = OperatorLookup<OP_DECL>::map_ptr;
10871086
auto found = (SF.*OP_MAP).find(Name);
10881087
if (found != (SF.*OP_MAP).end() && (includePrivate || found->second.getInt()))
10891088
return found->second.getPointer();
@@ -1105,7 +1104,7 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
11051104
continue;
11061105

11071106
Optional<OP_DECL *> maybeOp =
1108-
lookupOperatorDeclForName(imported.module.second, Loc, Name, OP_MAP);
1107+
lookupOperatorDeclForName<OP_DECL>(imported.module.second, Loc, Name);
11091108
if (!maybeOp)
11101109
return None;
11111110

@@ -1138,12 +1137,10 @@ lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
11381137

11391138
template<typename OP_DECL>
11401139
static Optional<OP_DECL *>
1141-
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
1142-
OperatorMap<OP_DECL *> SourceFile::*OP_MAP)
1143-
{
1140+
lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name) {
11441141
OP_DECL *result = nullptr;
11451142
for (const FileUnit *File : M->getFiles()) {
1146-
auto next = lookupOperatorDeclForName(*File, Loc, Name, false, OP_MAP);
1143+
auto next = lookupOperatorDeclForName<OP_DECL>(*File, Loc, Name, false);
11471144
if (!next.hasValue())
11481145
return next;
11491146

@@ -1159,9 +1156,9 @@ lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
11591156
template <typename OperatorType>
11601157
llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
11611158
Evaluator &evaluator, OperatorLookupDescriptor desc) const {
1162-
auto result = lookupOperatorDeclForName(*desc.SF, desc.diagLoc, desc.name,
1163-
/*includePrivate*/ true,
1164-
OperatorLookup<OperatorType>::map_ptr);
1159+
auto result = lookupOperatorDeclForName<OperatorType>(*desc.SF, desc.diagLoc,
1160+
desc.name,
1161+
/*includePrivate*/ true);
11651162
if (!result.hasValue())
11661163
return nullptr;
11671164
if (auto *tracker = desc.SF->getReferencedNameTracker()) {
@@ -1172,9 +1169,9 @@ llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
11721169
}
11731170
}
11741171
if (!result.getValue()) {
1175-
result = lookupOperatorDeclForName(desc.SF->getParentModule(), desc.diagLoc,
1176-
desc.name,
1177-
OperatorLookup<OperatorType>::map_ptr);
1172+
result = lookupOperatorDeclForName<OperatorType>(desc.SF->getParentModule(),
1173+
desc.diagLoc,
1174+
desc.name);
11781175
}
11791176
return result.hasValue() ? result.getValue() : nullptr;
11801177
}
@@ -1183,15 +1180,9 @@ llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
11831180
#define LOOKUP_OPERATOR(Kind) \
11841181
Kind##Decl *ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
11851182
auto result = \
1186-
lookupOperatorDeclForName(this, loc, name, &SourceFile::Kind##s); \
1183+
lookupOperatorDeclForName<Kind##Decl>(this, loc, name); \
11871184
return result ? *result : nullptr; \
11881185
} \
1189-
Kind##Decl *SourceFile::lookup##Kind(Identifier name, bool cascades, \
1190-
SourceLoc loc) { \
1191-
return evaluateOrDefault( \
1192-
getASTContext().evaluator, \
1193-
Lookup##Kind##Request{{this, name, cascades, loc}}, nullptr); \
1194-
} \
11951186
template llvm::Expected<Kind##Decl *> \
11961187
LookupOperatorRequest<Kind##Decl>::evaluate(Evaluator &e, \
11971188
OperatorLookupDescriptor d) const;

lib/Sema/TypeCheckDecl.cpp

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,9 +1253,14 @@ static PrecedenceGroupDecl *
12531253
lookupPrecedenceGroup(const PrecedenceGroupDescriptor &descriptor) {
12541254
auto *dc = descriptor.dc;
12551255
if (auto sf = dc->getParentSourceFile()) {
1256-
bool cascading = dc->isCascadingContextForLookup(false);
1257-
return sf->lookupPrecedenceGroup(descriptor.ident, cascading,
1258-
descriptor.nameLoc);
1256+
OperatorLookupDescriptor desc{
1257+
sf,
1258+
descriptor.ident,
1259+
dc->isCascadingContextForLookup(false),
1260+
descriptor.nameLoc
1261+
};
1262+
return evaluateOrDefault(sf->getASTContext().evaluator,
1263+
LookupPrecedenceGroupRequest{desc}, nullptr);
12591264
} else {
12601265
return dc->getParentModule()->lookupPrecedenceGroup(descriptor.ident,
12611266
descriptor.nameLoc);
@@ -1725,26 +1730,27 @@ FunctionOperatorRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
17251730
FD->diagnose(diag::operator_in_local_scope);
17261731
}
17271732

1733+
OperatorLookupDescriptor desc{
1734+
FD->getDeclContext()->getParentSourceFile(),
1735+
operatorName,
1736+
FD->isCascadingContextForLookup(false),
1737+
FD->getLoc()
1738+
};
17281739
OperatorDecl *op = nullptr;
1729-
SourceFile &SF = *FD->getDeclContext()->getParentSourceFile();
17301740
if (FD->isUnaryOperator()) {
17311741
if (FD->getAttrs().hasAttribute<PrefixAttr>()) {
1732-
op = SF.lookupPrefixOperator(operatorName,
1733-
FD->isCascadingContextForLookup(false),
1734-
FD->getLoc());
1742+
op = evaluateOrDefault(evaluator,
1743+
LookupPrefixOperatorRequest{desc}, nullptr);
17351744
} else if (FD->getAttrs().hasAttribute<PostfixAttr>()) {
1736-
op = SF.lookupPostfixOperator(operatorName,
1737-
FD->isCascadingContextForLookup(false),
1738-
FD->getLoc());
1745+
op = evaluateOrDefault(evaluator,
1746+
LookupPostfixOperatorRequest{desc}, nullptr);
17391747
} else {
1740-
auto prefixOp =
1741-
SF.lookupPrefixOperator(operatorName,
1742-
FD->isCascadingContextForLookup(false),
1743-
FD->getLoc());
1744-
auto postfixOp =
1745-
SF.lookupPostfixOperator(operatorName,
1746-
FD->isCascadingContextForLookup(false),
1747-
FD->getLoc());
1748+
auto prefixOp = evaluateOrDefault(evaluator,
1749+
LookupPrefixOperatorRequest{desc},
1750+
nullptr);
1751+
auto postfixOp = evaluateOrDefault(evaluator,
1752+
LookupPostfixOperatorRequest{desc},
1753+
nullptr);
17481754

17491755
// If we found both prefix and postfix, or neither prefix nor postfix,
17501756
// complain. We can't fix this situation.
@@ -1790,9 +1796,9 @@ FunctionOperatorRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
17901796
static_cast<bool>(postfixOp));
17911797
}
17921798
} else if (FD->isBinaryOperator()) {
1793-
op = SF.lookupInfixOperator(operatorName,
1794-
FD->isCascadingContextForLookup(false),
1795-
FD->getLoc());
1799+
op = evaluateOrDefault(evaluator,
1800+
LookupInfixOperatorRequest{desc},
1801+
nullptr);
17961802
} else {
17971803
diags.diagnose(FD, diag::invalid_arg_count_for_operator);
17981804
return nullptr;

lib/Sema/TypeCheckExpr.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "TypeChecker.h"
1919
#include "swift/AST/NameLookup.h"
20+
#include "swift/AST/NameLookupRequests.h"
2021
#include "swift/AST/Decl.h"
2122
#include "swift/AST/Initializer.h"
2223
#include "swift/AST/ParameterList.h"
@@ -129,12 +130,19 @@ Expr *TypeChecker::substituteInputSugarTypeForResult(ApplyExpr *E) {
129130
static PrecedenceGroupDecl *lookupPrecedenceGroupForOperator(DeclContext *DC,
130131
Identifier name,
131132
SourceLoc loc) {
132-
SourceFile *SF = DC->getParentSourceFile();
133-
bool isCascading = DC->isCascadingContextForLookup(true);
134-
if (auto op = SF->lookupInfixOperator(name, isCascading, loc)) {
133+
OperatorLookupDescriptor desc{
134+
DC->getParentSourceFile(),
135+
name,
136+
DC->isCascadingContextForLookup(true),
137+
loc
138+
};
139+
auto &Ctx = DC->getASTContext();
140+
if (auto op = evaluateOrDefault(Ctx.evaluator,
141+
LookupInfixOperatorRequest{desc},
142+
nullptr)) {
135143
return op->getPrecedenceGroup();
136144
} else {
137-
DC->getASTContext().Diags.diagnose(loc, diag::unknown_binop);
145+
Ctx.Diags.diagnose(loc, diag::unknown_binop);
138146
}
139147
return nullptr;
140148
}

0 commit comments

Comments
 (0)