Skip to content

Commit 9656a04

Browse files
committed
Allow OperatorLookupDescriptor to hold a file or module
1 parent a502246 commit 9656a04

File tree

5 files changed

+57
-35
lines changed

5 files changed

+57
-35
lines changed

include/swift/AST/NameLookupRequests.h

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
#include "swift/AST/SimpleRequest.h"
2020
#include "swift/AST/ASTTypeIDs.h"
21+
#include "swift/AST/FileUnit.h"
2122
#include "swift/AST/Identifier.h"
2223
#include "swift/Basic/Statistic.h"
2324
#include "llvm/ADT/Hashing.h"
@@ -518,29 +519,53 @@ class DirectLookupRequest
518519

519520
class OperatorLookupDescriptor final {
520521
public:
521-
SourceFile *SF;
522+
using Storage = llvm::PointerUnion<FileUnit *, ModuleDecl *>;
523+
Storage fileOrModule;
522524
Identifier name;
523525
bool isCascading;
524526
SourceLoc diagLoc;
525527

526-
OperatorLookupDescriptor(SourceFile *SF, Identifier name, bool isCascading,
527-
SourceLoc diagLoc)
528-
: SF(SF), name(name), isCascading(isCascading), diagLoc(diagLoc) {}
528+
private:
529+
OperatorLookupDescriptor(Storage fileOrModule, Identifier name,
530+
bool isCascading, SourceLoc diagLoc)
531+
: fileOrModule(fileOrModule), name(name), isCascading(isCascading),
532+
diagLoc(diagLoc) {}
533+
534+
public:
535+
/// Retrieves the files to perform lookup in.
536+
ArrayRef<FileUnit *> getFiles() const;
537+
538+
/// If this is for a module lookup, returns the module. Otherwise returns
539+
/// \c nullptr.
540+
ModuleDecl *getModule() const {
541+
return fileOrModule.dyn_cast<ModuleDecl *>();
542+
}
529543

530544
friend llvm::hash_code hash_value(const OperatorLookupDescriptor &desc) {
531-
return llvm::hash_combine(desc.SF, desc.name, desc.isCascading);
545+
return llvm::hash_combine(desc.fileOrModule, desc.name, desc.isCascading);
532546
}
533547

534548
friend bool operator==(const OperatorLookupDescriptor &lhs,
535549
const OperatorLookupDescriptor &rhs) {
536-
return lhs.SF == rhs.SF && lhs.name == rhs.name &&
550+
return lhs.fileOrModule == rhs.fileOrModule && lhs.name == rhs.name &&
537551
lhs.isCascading == rhs.isCascading;
538552
}
539553

540554
friend bool operator!=(const OperatorLookupDescriptor &lhs,
541555
const OperatorLookupDescriptor &rhs) {
542556
return !(lhs == rhs);
543557
}
558+
559+
static OperatorLookupDescriptor forFile(FileUnit *file, Identifier name,
560+
bool isCascading, SourceLoc diagLoc) {
561+
return OperatorLookupDescriptor(file, name, isCascading, diagLoc);
562+
}
563+
564+
static OperatorLookupDescriptor forModule(ModuleDecl *mod, Identifier name,
565+
bool isCascading,
566+
SourceLoc diagLoc) {
567+
return OperatorLookupDescriptor(mod, name, isCascading, diagLoc);
568+
}
544569
};
545570

546571
void simple_display(llvm::raw_ostream &out,

lib/AST/Module.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,22 +1154,20 @@ lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name) {
11541154
template <typename OperatorType>
11551155
llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
11561156
Evaluator &evaluator, OperatorLookupDescriptor desc) const {
1157-
auto result = lookupOperatorDeclForName<OperatorType>(*desc.SF, desc.diagLoc,
1157+
auto *file = desc.fileOrModule.get<FileUnit *>();
1158+
auto result = lookupOperatorDeclForName<OperatorType>(*file, desc.diagLoc,
11581159
desc.name,
11591160
/*includePrivate*/ true);
11601161
if (!result.hasValue())
11611162
return nullptr;
1162-
if (auto *tracker = desc.SF->getReferencedNameTracker()) {
1163-
if (!result.getValue() ||
1164-
result.getValue()->getDeclContext()->getModuleScopeContext() !=
1165-
desc.SF) {
1166-
tracker->addTopLevelName(desc.name, desc.isCascading);
1167-
}
1163+
1164+
if (!result.getValue() ||
1165+
result.getValue()->getDeclContext()->getModuleScopeContext() != file) {
1166+
namelookup::recordLookupOfTopLevelName(file, desc.name, desc.isCascading);
11681167
}
11691168
if (!result.getValue()) {
1170-
result = lookupOperatorDeclForName<OperatorType>(desc.SF->getParentModule(),
1171-
desc.diagLoc,
1172-
desc.name);
1169+
result = lookupOperatorDeclForName<OperatorType>(file->getParentModule(),
1170+
desc.diagLoc, desc.name);
11731171
}
11741172
return result.hasValue() ? result.getValue() : nullptr;
11751173
}

lib/AST/NameLookupRequests.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,12 +211,20 @@ SourceLoc swift::extractNearestSourceLoc(const DirectLookupDescriptor &desc) {
211211
// LookupOperatorRequest computation.
212212
//----------------------------------------------------------------------------//
213213

214+
ArrayRef<FileUnit *> OperatorLookupDescriptor::getFiles() const {
215+
if (auto *module = getModule())
216+
return module->getFiles();
217+
218+
// Return an ArrayRef pointing to the FileUnit in the union.
219+
return llvm::makeArrayRef(*fileOrModule.getAddrOfPtr1());
220+
}
221+
214222
void swift::simple_display(llvm::raw_ostream &out,
215223
const OperatorLookupDescriptor &desc) {
216224
out << "looking up operator ";
217225
simple_display(out, desc.name);
218226
out << " in ";
219-
simple_display(out, desc.SF);
227+
simple_display(out, desc.fileOrModule);
220228
}
221229

222230
SourceLoc swift::extractNearestSourceLoc(const OperatorLookupDescriptor &desc) {

lib/Sema/TypeCheckDecl.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,12 +1253,9 @@ static PrecedenceGroupDecl *
12531253
lookupPrecedenceGroup(const PrecedenceGroupDescriptor &descriptor) {
12541254
auto *dc = descriptor.dc;
12551255
if (auto sf = dc->getParentSourceFile()) {
1256-
OperatorLookupDescriptor desc{
1257-
sf,
1258-
descriptor.ident,
1259-
dc->isCascadingContextForLookup(false),
1260-
descriptor.nameLoc
1261-
};
1256+
auto desc = OperatorLookupDescriptor::forFile(
1257+
sf, descriptor.ident, dc->isCascadingContextForLookup(false),
1258+
descriptor.nameLoc);
12621259
return evaluateOrDefault(sf->getASTContext().evaluator,
12631260
LookupPrecedenceGroupRequest{desc}, nullptr);
12641261
} else {
@@ -1730,12 +1727,9 @@ FunctionOperatorRequest::evaluate(Evaluator &evaluator, FuncDecl *FD) const {
17301727
FD->diagnose(diag::operator_in_local_scope);
17311728
}
17321729

1733-
OperatorLookupDescriptor desc{
1734-
FD->getDeclContext()->getParentSourceFile(),
1735-
operatorName,
1736-
FD->isCascadingContextForLookup(false),
1737-
FD->getLoc()
1738-
};
1730+
auto desc = OperatorLookupDescriptor::forFile(
1731+
FD->getDeclContext()->getParentSourceFile(), operatorName,
1732+
FD->isCascadingContextForLookup(false), FD->getLoc());
17391733
OperatorDecl *op = nullptr;
17401734
if (FD->isUnaryOperator()) {
17411735
if (FD->getAttrs().hasAttribute<PrefixAttr>()) {

lib/Sema/TypeCheckExpr.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,9 @@ Expr *TypeChecker::substituteInputSugarTypeForResult(ApplyExpr *E) {
130130
static PrecedenceGroupDecl *lookupPrecedenceGroupForOperator(DeclContext *DC,
131131
Identifier name,
132132
SourceLoc loc) {
133-
OperatorLookupDescriptor desc{
134-
DC->getParentSourceFile(),
135-
name,
136-
DC->isCascadingContextForLookup(true),
137-
loc
138-
};
133+
auto desc = OperatorLookupDescriptor::forFile(
134+
DC->getParentSourceFile(), name, DC->isCascadingContextForLookup(true),
135+
loc);
139136
auto &Ctx = DC->getASTContext();
140137
if (auto op = evaluateOrDefault(Ctx.evaluator,
141138
LookupInfixOperatorRequest{desc},

0 commit comments

Comments
 (0)