Skip to content

Commit 076a247

Browse files
authored
Merge pull request #30544 from CodaFi/standard-operating-procedure
[NFC] Define LookupOperatorRequest and Friends
2 parents 6f0c54f + bfe23bb commit 076a247

11 files changed

+165
-55
lines changed

include/swift/AST/Identifier.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ class Identifier {
168168
/// Null identifiers come after all other identifiers.
169169
int compare(Identifier other) const;
170170

171+
friend llvm::hash_code hash_value(Identifier ident) {
172+
return llvm::hash_value(ident.getAsOpaquePointer());
173+
}
174+
171175
bool operator==(Identifier RHS) const { return Pointer == RHS.Pointer; }
172176
bool operator!=(Identifier RHS) const { return !(*this==RHS); }
173177

include/swift/AST/NameLookupRequests.h

Lines changed: 57 additions & 0 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/Identifier.h"
2122
#include "swift/Basic/Statistic.h"
2223
#include "llvm/ADT/Hashing.h"
2324
#include "llvm/ADT/TinyPtrVector.h"
@@ -515,6 +516,62 @@ class DirectLookupRequest
515516
evaluate(Evaluator &evaluator, DirectLookupDescriptor desc) const;
516517
};
517518

519+
class OperatorLookupDescriptor final {
520+
public:
521+
SourceFile *SF;
522+
Identifier name;
523+
bool isCascading;
524+
SourceLoc diagLoc;
525+
526+
OperatorLookupDescriptor(SourceFile *SF, Identifier name, bool isCascading,
527+
SourceLoc diagLoc)
528+
: SF(SF), name(name), isCascading(isCascading), diagLoc(diagLoc) {}
529+
530+
friend llvm::hash_code hash_value(const OperatorLookupDescriptor &desc) {
531+
return llvm::hash_combine(desc.SF, desc.name, desc.isCascading);
532+
}
533+
534+
friend bool operator==(const OperatorLookupDescriptor &lhs,
535+
const OperatorLookupDescriptor &rhs) {
536+
return lhs.SF == rhs.SF && lhs.name == rhs.name &&
537+
lhs.isCascading == rhs.isCascading;
538+
}
539+
540+
friend bool operator!=(const OperatorLookupDescriptor &lhs,
541+
const OperatorLookupDescriptor &rhs) {
542+
return !(lhs == rhs);
543+
}
544+
};
545+
546+
void simple_display(llvm::raw_ostream &out,
547+
const OperatorLookupDescriptor &desc);
548+
549+
SourceLoc extractNearestSourceLoc(const OperatorLookupDescriptor &desc);
550+
551+
template <typename OperatorType>
552+
class LookupOperatorRequest
553+
: public SimpleRequest<LookupOperatorRequest<OperatorType>,
554+
OperatorType *(OperatorLookupDescriptor),
555+
CacheKind::Uncached> {
556+
using SimpleRequest<LookupOperatorRequest<OperatorType>,
557+
OperatorType *(OperatorLookupDescriptor),
558+
CacheKind::Uncached>::SimpleRequest;
559+
560+
private:
561+
friend SimpleRequest<LookupOperatorRequest<OperatorType>,
562+
OperatorType *(OperatorLookupDescriptor),
563+
CacheKind::Uncached>;
564+
565+
// Evaluation.
566+
llvm::Expected<OperatorType *> evaluate(Evaluator &evaluator,
567+
OperatorLookupDescriptor desc) const;
568+
};
569+
570+
using LookupPrefixOperatorRequest = LookupOperatorRequest<PrefixOperatorDecl>;
571+
using LookupInfixOperatorRequest = LookupOperatorRequest<InfixOperatorDecl>;
572+
using LookupPostfixOperatorRequest = LookupOperatorRequest<PostfixOperatorDecl>;
573+
using LookupPrecedenceGroupRequest = LookupOperatorRequest<PrecedenceGroupDecl>;
574+
518575
#define SWIFT_TYPEID_ZONE NameLookup
519576
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
520577
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,16 @@ SWIFT_REQUEST(NameLookup, UnderlyingTypeDeclsReferencedRequest,
7171
SWIFT_REQUEST(NameLookup, UnqualifiedLookupRequest,
7272
LookupResult(UnqualifiedLookupDescriptor), Uncached,
7373
NoLocationInfo)
74+
75+
SWIFT_REQUEST(NameLookup, LookupPrefixOperatorRequest,
76+
PrefixOperatorDecl *(OperatorLookupDescriptor),
77+
Uncached, NoLocationInfo)
78+
SWIFT_REQUEST(NameLookup, LookupInfixOperatorRequest,
79+
InfixOperatorDecl *(OperatorLookupDescriptor),
80+
Uncached, NoLocationInfo)
81+
SWIFT_REQUEST(NameLookup, LookupPostfixOperatorRequest,
82+
PostfixOperatorDecl *(OperatorLookupDescriptor),
83+
Uncached, NoLocationInfo)
84+
SWIFT_REQUEST(NameLookup, LookupPrecedenceGroupRequest,
85+
PrecedenceGroupDecl *(OperatorLookupDescriptor),
86+
Uncached, NoLocationInfo)

include/swift/AST/TypeCheckRequests.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,8 +1495,8 @@ struct PrecedenceGroupDescriptor {
14951495

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

1498-
class LookupPrecedenceGroupRequest
1499-
: public SimpleRequest<LookupPrecedenceGroupRequest,
1498+
class ValidatePrecedenceGroupRequest
1499+
: public SimpleRequest<ValidatePrecedenceGroupRequest,
15001500
PrecedenceGroupDecl *(PrecedenceGroupDescriptor),
15011501
CacheKind::Cached> {
15021502
public:

include/swift/AST/TypeCheckerTypeIDZone.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ SWIFT_REQUEST(TypeChecker, IsSetterMutatingRequest, bool(AbstractStorageDecl *),
117117
SeparatelyCached, NoLocationInfo)
118118
SWIFT_REQUEST(TypeChecker, LazyStoragePropertyRequest, VarDecl *(VarDecl *),
119119
Cached, NoLocationInfo)
120-
SWIFT_REQUEST(TypeChecker, LookupPrecedenceGroupRequest,
120+
SWIFT_REQUEST(TypeChecker, ValidatePrecedenceGroupRequest,
121121
PrecedenceGroupDecl *(DeclContext *, Identifier, SourceLoc),
122122
Cached, NoLocationInfo)
123123
SWIFT_REQUEST(TypeChecker, MangleLocalTypeDeclRequest,

lib/AST/Module.cpp

Lines changed: 53 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "swift/AST/Module.h"
18-
#include "swift/AST/AccessScope.h"
1918
#include "swift/AST/ASTContext.h"
2019
#include "swift/AST/ASTMangler.h"
2120
#include "swift/AST/ASTPrinter.h"
2221
#include "swift/AST/ASTWalker.h"
22+
#include "swift/AST/AccessScope.h"
2323
#include "swift/AST/Builtins.h"
2424
#include "swift/AST/ClangModuleLoader.h"
2525
#include "swift/AST/DiagnosticsSema.h"
@@ -31,11 +31,12 @@
3131
#include "swift/AST/LinkLibrary.h"
3232
#include "swift/AST/ModuleLoader.h"
3333
#include "swift/AST/NameLookup.h"
34-
#include "swift/AST/ReferencedNameTracker.h"
34+
#include "swift/AST/NameLookupRequests.h"
3535
#include "swift/AST/ParseRequests.h"
3636
#include "swift/AST/PrettyStackTrace.h"
3737
#include "swift/AST/PrintOptions.h"
3838
#include "swift/AST/ProtocolConformance.h"
39+
#include "swift/AST/ReferencedNameTracker.h"
3940
#include "swift/AST/SourceFile.h"
4041
#include "swift/AST/TypeCheckRequests.h"
4142
#include "swift/Basic/Compiler.h"
@@ -48,15 +49,15 @@
4849
#include "clang/Basic/Module.h"
4950
#include "llvm/ADT/DenseMap.h"
5051
#include "llvm/ADT/DenseSet.h"
51-
#include "llvm/ADT/TinyPtrVector.h"
52+
#include "llvm/ADT/STLExtras.h"
5253
#include "llvm/ADT/SmallPtrSet.h"
5354
#include "llvm/ADT/StringExtras.h"
54-
#include "llvm/ADT/STLExtras.h"
55+
#include "llvm/ADT/TinyPtrVector.h"
5556
#include "llvm/Support/MD5.h"
5657
#include "llvm/Support/MemoryBuffer.h"
5758
#include "llvm/Support/Path.h"
58-
#include "llvm/Support/raw_ostream.h"
5959
#include "llvm/Support/SaveAndRestore.h"
60+
#include "llvm/Support/raw_ostream.h"
6061

6162
using namespace swift;
6263

@@ -943,6 +944,7 @@ namespace {
943944

944945
template <>
945946
struct OperatorLookup<PrefixOperatorDecl> {
947+
constexpr static auto map_ptr = &SourceFile::PrefixOperators;
946948
template <typename T>
947949
static PrefixOperatorDecl *lookup(T &container, Identifier name) {
948950
return cast_or_null<PrefixOperatorDecl>(
@@ -952,6 +954,7 @@ namespace {
952954

953955
template <>
954956
struct OperatorLookup<InfixOperatorDecl> {
957+
constexpr static auto map_ptr = &SourceFile::InfixOperators;
955958
template <typename T>
956959
static InfixOperatorDecl *lookup(T &container, Identifier name) {
957960
return cast_or_null<InfixOperatorDecl>(
@@ -961,6 +964,7 @@ namespace {
961964

962965
template <>
963966
struct OperatorLookup<PostfixOperatorDecl> {
967+
constexpr static auto map_ptr = &SourceFile::PostfixOperators;
964968
template <typename T>
965969
static PostfixOperatorDecl *lookup(T &container, Identifier name) {
966970
return cast_or_null<PostfixOperatorDecl>(
@@ -970,6 +974,7 @@ namespace {
970974

971975
template <>
972976
struct OperatorLookup<PrecedenceGroupDecl> {
977+
constexpr static auto map_ptr = &SourceFile::PrecedenceGroups;
973978
template <typename T>
974979
static PrecedenceGroupDecl *lookup(T &container, Identifier name) {
975980
return container.lookupPrecedenceGroup(name);
@@ -1058,12 +1063,11 @@ checkOperatorConflicts(const SourceFile &SF, SourceLoc loc,
10581063

10591064
// Returns None on error, Optional(nullptr) if no operator decl found, or
10601065
// Optional(decl) if decl was found.
1061-
template<typename OP_DECL>
1066+
template <typename OP_DECL>
10621067
static Optional<OP_DECL *>
1063-
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc, Identifier Name,
1064-
bool includePrivate,
1065-
OperatorMap<OP_DECL *> SourceFile::*OP_MAP)
1066-
{
1068+
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
1069+
Identifier Name, bool includePrivate,
1070+
OperatorMap<OP_DECL *> SourceFile::*OP_MAP) {
10671071
switch (File.getKind()) {
10681072
case FileUnitKind::Builtin:
10691073
// The Builtin module declares no operators.
@@ -1152,31 +1156,45 @@ lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
11521156
return result;
11531157
}
11541158

1155-
#define LOOKUP_OPERATOR(Kind) \
1156-
Kind##Decl * \
1157-
ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
1158-
auto result = lookupOperatorDeclForName(this, loc, name, \
1159-
&SourceFile::Kind##s); \
1160-
return result ? *result : nullptr; \
1161-
} \
1162-
Kind##Decl * \
1163-
SourceFile::lookup##Kind(Identifier name, bool isCascading, SourceLoc loc) { \
1164-
auto result = lookupOperatorDeclForName(*this, loc, name, true, \
1165-
&SourceFile::Kind##s); \
1166-
if (!result.hasValue()) \
1167-
return nullptr; \
1168-
if (ReferencedNames) {\
1169-
if (!result.getValue() || \
1170-
result.getValue()->getDeclContext()->getModuleScopeContext() != this) {\
1171-
ReferencedNames->addTopLevelName(name, isCascading); \
1172-
} \
1173-
} \
1174-
if (!result.getValue()) { \
1175-
result = lookupOperatorDeclForName(getParentModule(), loc, name, \
1176-
&SourceFile::Kind##s); \
1177-
} \
1178-
return result.hasValue() ? result.getValue() : nullptr; \
1179-
}
1159+
template <typename OperatorType>
1160+
llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
1161+
Evaluator &evaluator, OperatorLookupDescriptor desc) const {
1162+
auto result = lookupOperatorDeclForName(*desc.SF, desc.diagLoc, desc.name,
1163+
/*includePrivate*/ true,
1164+
OperatorLookup<OperatorType>::map_ptr);
1165+
if (!result.hasValue())
1166+
return nullptr;
1167+
if (auto *tracker = desc.SF->getReferencedNameTracker()) {
1168+
if (!result.getValue() ||
1169+
result.getValue()->getDeclContext()->getModuleScopeContext() !=
1170+
desc.SF) {
1171+
tracker->addTopLevelName(desc.name, desc.isCascading);
1172+
}
1173+
}
1174+
if (!result.getValue()) {
1175+
result = lookupOperatorDeclForName(desc.SF->getParentModule(), desc.diagLoc,
1176+
desc.name,
1177+
OperatorLookup<OperatorType>::map_ptr);
1178+
}
1179+
return result.hasValue() ? result.getValue() : nullptr;
1180+
}
1181+
1182+
1183+
#define LOOKUP_OPERATOR(Kind) \
1184+
Kind##Decl *ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
1185+
auto result = \
1186+
lookupOperatorDeclForName(this, loc, name, &SourceFile::Kind##s); \
1187+
return result ? *result : nullptr; \
1188+
} \
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+
} \
1195+
template llvm::Expected<Kind##Decl *> \
1196+
LookupOperatorRequest<Kind##Decl>::evaluate(Evaluator &e, \
1197+
OperatorLookupDescriptor d) const;
11801198

11811199
LOOKUP_OPERATOR(PrefixOperator)
11821200
LOOKUP_OPERATOR(InfixOperator)

lib/AST/ModuleNameLookup.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,9 @@ llvm::Expected<QualifiedLookupResult> LookupInModuleRequest::evaluate(
249249
const DeclContext *moduleScopeContext) const {
250250
assert(moduleScopeContext->isModuleScopeContext());
251251

252-
auto &ctx = moduleOrFile->getASTContext();
253-
FrontendStatsTracer tracer(ctx.Stats, "lookup-in-module");
254-
255252
QualifiedLookupResult decls;
256-
LookupByName lookup(ctx, resolutionKind, name, lookupKind);
253+
LookupByName lookup(moduleOrFile->getASTContext(), resolutionKind,
254+
name, lookupKind);
257255
lookup.lookupInModule(decls, moduleOrFile, {}, moduleScopeContext);
258256
return decls;
259257
}

lib/AST/NameLookup.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1609,7 +1609,6 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,
16091609

16101610
// Visit all of the nominal types we know about, discovering any others
16111611
// we need along the way.
1612-
auto &ctx = DC->getASTContext();
16131612
bool wantProtocolMembers = (options & NL_ProtocolMembers);
16141613
while (!stack.empty()) {
16151614
auto current = stack.back();

lib/AST/NameLookupRequests.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212

1313
#include "swift/AST/NameLookup.h"
1414
#include "swift/AST/NameLookupRequests.h"
15-
#include "swift/Subsystems.h"
1615
#include "swift/AST/ASTContext.h"
17-
#include "swift/AST/Evaluator.h"
1816
#include "swift/AST/Decl.h"
17+
#include "swift/AST/Evaluator.h"
1918
#include "swift/AST/Module.h"
19+
#include "swift/AST/SourceFile.h"
20+
#include "swift/Subsystems.h"
2021

2122
using namespace swift;
2223

@@ -206,6 +207,22 @@ SourceLoc swift::extractNearestSourceLoc(const DirectLookupDescriptor &desc) {
206207
return extractNearestSourceLoc(desc.DC);
207208
}
208209

210+
//----------------------------------------------------------------------------//
211+
// LookupOperatorRequest computation.
212+
//----------------------------------------------------------------------------//
213+
214+
void swift::simple_display(llvm::raw_ostream &out,
215+
const OperatorLookupDescriptor &desc) {
216+
out << "looking up operator ";
217+
simple_display(out, desc.name);
218+
out << " in ";
219+
simple_display(out, desc.SF);
220+
}
221+
222+
SourceLoc swift::extractNearestSourceLoc(const OperatorLookupDescriptor &desc) {
223+
return desc.diagLoc;
224+
}
225+
209226
// Define request evaluation functions for each of the name lookup requests.
210227
static AbstractRequestFunction *nameLookupRequestFunctions[] = {
211228
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \

lib/AST/TypeCheckRequests.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,15 +1017,16 @@ void InterfaceTypeRequest::cacheResult(Type type) const {
10171017
}
10181018

10191019
//----------------------------------------------------------------------------//
1020-
// LookupPrecedenceGroupRequest computation.
1020+
// ValidatePrecedenceGroupRequest computation.
10211021
//----------------------------------------------------------------------------//
10221022

1023-
SourceLoc LookupPrecedenceGroupRequest::getNearestLoc() const {
1023+
SourceLoc ValidatePrecedenceGroupRequest::getNearestLoc() const {
10241024
auto &desc = std::get<0>(getStorage());
10251025
return desc.getLoc();
10261026
}
10271027

1028-
void LookupPrecedenceGroupRequest::diagnoseCycle(DiagnosticEngine &diags) const {
1028+
void ValidatePrecedenceGroupRequest::diagnoseCycle(
1029+
DiagnosticEngine &diags) const {
10291030
auto &desc = std::get<0>(getStorage());
10301031
if (auto pathDir = desc.pathDirection) {
10311032
diags.diagnose(desc.nameLoc, diag::precedence_group_cycle, (bool)*pathDir);
@@ -1034,7 +1035,8 @@ void LookupPrecedenceGroupRequest::diagnoseCycle(DiagnosticEngine &diags) const
10341035
}
10351036
}
10361037

1037-
void LookupPrecedenceGroupRequest::noteCycleStep(DiagnosticEngine &diag) const {
1038+
void ValidatePrecedenceGroupRequest::noteCycleStep(
1039+
DiagnosticEngine &diag) const {
10381040
auto &desc = std::get<0>(getStorage());
10391041
diag.diagnose(desc.nameLoc,
10401042
diag::circular_reference_through_precedence_group, desc.ident);

0 commit comments

Comments
 (0)