Skip to content

[NFC] Define LookupOperatorRequest and Friends #30544

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
Mar 21, 2020
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
4 changes: 4 additions & 0 deletions include/swift/AST/Identifier.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ class Identifier {
/// Null identifiers come after all other identifiers.
int compare(Identifier other) const;

friend llvm::hash_code hash_value(Identifier ident) {
return llvm::hash_value(ident.getAsOpaquePointer());
}

bool operator==(Identifier RHS) const { return Pointer == RHS.Pointer; }
bool operator!=(Identifier RHS) const { return !(*this==RHS); }

Expand Down
57 changes: 57 additions & 0 deletions include/swift/AST/NameLookupRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include "swift/AST/SimpleRequest.h"
#include "swift/AST/ASTTypeIDs.h"
#include "swift/AST/Identifier.h"
#include "swift/Basic/Statistic.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/TinyPtrVector.h"
Expand Down Expand Up @@ -515,6 +516,62 @@ class DirectLookupRequest
evaluate(Evaluator &evaluator, DirectLookupDescriptor desc) const;
};

class OperatorLookupDescriptor final {
public:
SourceFile *SF;
Identifier name;
bool isCascading;
SourceLoc diagLoc;

OperatorLookupDescriptor(SourceFile *SF, Identifier name, bool isCascading,
SourceLoc diagLoc)
: SF(SF), name(name), isCascading(isCascading), diagLoc(diagLoc) {}

friend llvm::hash_code hash_value(const OperatorLookupDescriptor &desc) {
return llvm::hash_combine(desc.SF, desc.name, desc.isCascading);
}

friend bool operator==(const OperatorLookupDescriptor &lhs,
const OperatorLookupDescriptor &rhs) {
return lhs.SF == rhs.SF && lhs.name == rhs.name &&
lhs.isCascading == rhs.isCascading;
}

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

void simple_display(llvm::raw_ostream &out,
const OperatorLookupDescriptor &desc);

SourceLoc extractNearestSourceLoc(const OperatorLookupDescriptor &desc);

template <typename OperatorType>
class LookupOperatorRequest
: public SimpleRequest<LookupOperatorRequest<OperatorType>,
OperatorType *(OperatorLookupDescriptor),
CacheKind::Uncached> {
using SimpleRequest<LookupOperatorRequest<OperatorType>,
OperatorType *(OperatorLookupDescriptor),
CacheKind::Uncached>::SimpleRequest;

private:
friend SimpleRequest<LookupOperatorRequest<OperatorType>,
OperatorType *(OperatorLookupDescriptor),
CacheKind::Uncached>;

// Evaluation.
llvm::Expected<OperatorType *> evaluate(Evaluator &evaluator,
OperatorLookupDescriptor desc) const;
};

using LookupPrefixOperatorRequest = LookupOperatorRequest<PrefixOperatorDecl>;
using LookupInfixOperatorRequest = LookupOperatorRequest<InfixOperatorDecl>;
using LookupPostfixOperatorRequest = LookupOperatorRequest<PostfixOperatorDecl>;
using LookupPrecedenceGroupRequest = LookupOperatorRequest<PrecedenceGroupDecl>;

#define SWIFT_TYPEID_ZONE NameLookup
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
#include "swift/Basic/DefineTypeIDZone.h"
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/NameLookupTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,16 @@ SWIFT_REQUEST(NameLookup, UnderlyingTypeDeclsReferencedRequest,
SWIFT_REQUEST(NameLookup, UnqualifiedLookupRequest,
LookupResult(UnqualifiedLookupDescriptor), Uncached,
NoLocationInfo)

SWIFT_REQUEST(NameLookup, LookupPrefixOperatorRequest,
PrefixOperatorDecl *(OperatorLookupDescriptor),
Uncached, NoLocationInfo)
SWIFT_REQUEST(NameLookup, LookupInfixOperatorRequest,
InfixOperatorDecl *(OperatorLookupDescriptor),
Uncached, NoLocationInfo)
SWIFT_REQUEST(NameLookup, LookupPostfixOperatorRequest,
PostfixOperatorDecl *(OperatorLookupDescriptor),
Uncached, NoLocationInfo)
SWIFT_REQUEST(NameLookup, LookupPrecedenceGroupRequest,
PrecedenceGroupDecl *(OperatorLookupDescriptor),
Uncached, NoLocationInfo)
4 changes: 2 additions & 2 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1495,8 +1495,8 @@ struct PrecedenceGroupDescriptor {

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

class LookupPrecedenceGroupRequest
: public SimpleRequest<LookupPrecedenceGroupRequest,
class ValidatePrecedenceGroupRequest
: public SimpleRequest<ValidatePrecedenceGroupRequest,
PrecedenceGroupDecl *(PrecedenceGroupDescriptor),
CacheKind::Cached> {
public:
Expand Down
2 changes: 1 addition & 1 deletion include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ SWIFT_REQUEST(TypeChecker, IsSetterMutatingRequest, bool(AbstractStorageDecl *),
SeparatelyCached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, LazyStoragePropertyRequest, VarDecl *(VarDecl *),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, LookupPrecedenceGroupRequest,
SWIFT_REQUEST(TypeChecker, ValidatePrecedenceGroupRequest,
PrecedenceGroupDecl *(DeclContext *, Identifier, SourceLoc),
Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, MangleLocalTypeDeclRequest,
Expand Down
88 changes: 53 additions & 35 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
//===----------------------------------------------------------------------===//

#include "swift/AST/Module.h"
#include "swift/AST/AccessScope.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/ASTMangler.h"
#include "swift/AST/ASTPrinter.h"
#include "swift/AST/ASTWalker.h"
#include "swift/AST/AccessScope.h"
#include "swift/AST/Builtins.h"
#include "swift/AST/ClangModuleLoader.h"
#include "swift/AST/DiagnosticsSema.h"
Expand All @@ -31,11 +31,12 @@
#include "swift/AST/LinkLibrary.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/NameLookup.h"
#include "swift/AST/ReferencedNameTracker.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/ParseRequests.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/PrintOptions.h"
#include "swift/AST/ProtocolConformance.h"
#include "swift/AST/ReferencedNameTracker.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/Compiler.h"
Expand All @@ -48,15 +49,15 @@
#include "clang/Basic/Module.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/TinyPtrVector.h"
#include "llvm/Support/MD5.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Support/SaveAndRestore.h"
#include "llvm/Support/raw_ostream.h"

using namespace swift;

Expand Down Expand Up @@ -943,6 +944,7 @@ namespace {

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

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

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

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

// Returns None on error, Optional(nullptr) if no operator decl found, or
// Optional(decl) if decl was found.
template<typename OP_DECL>
template <typename OP_DECL>
static Optional<OP_DECL *>
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc, Identifier Name,
bool includePrivate,
OperatorMap<OP_DECL *> SourceFile::*OP_MAP)
{
lookupOperatorDeclForName(const FileUnit &File, SourceLoc Loc,
Identifier Name, bool includePrivate,
OperatorMap<OP_DECL *> SourceFile::*OP_MAP) {
switch (File.getKind()) {
case FileUnitKind::Builtin:
// The Builtin module declares no operators.
Expand Down Expand Up @@ -1152,31 +1156,45 @@ lookupOperatorDeclForName(ModuleDecl *M, SourceLoc Loc, Identifier Name,
return result;
}

#define LOOKUP_OPERATOR(Kind) \
Kind##Decl * \
ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
auto result = lookupOperatorDeclForName(this, loc, name, \
&SourceFile::Kind##s); \
return result ? *result : nullptr; \
} \
Kind##Decl * \
SourceFile::lookup##Kind(Identifier name, bool isCascading, SourceLoc loc) { \
auto result = lookupOperatorDeclForName(*this, loc, name, true, \
&SourceFile::Kind##s); \
if (!result.hasValue()) \
return nullptr; \
if (ReferencedNames) {\
if (!result.getValue() || \
result.getValue()->getDeclContext()->getModuleScopeContext() != this) {\
ReferencedNames->addTopLevelName(name, isCascading); \
} \
} \
if (!result.getValue()) { \
result = lookupOperatorDeclForName(getParentModule(), loc, name, \
&SourceFile::Kind##s); \
} \
return result.hasValue() ? result.getValue() : nullptr; \
}
template <typename OperatorType>
llvm::Expected<OperatorType *> LookupOperatorRequest<OperatorType>::evaluate(
Evaluator &evaluator, OperatorLookupDescriptor desc) const {
auto result = lookupOperatorDeclForName(*desc.SF, desc.diagLoc, desc.name,
/*includePrivate*/ true,
OperatorLookup<OperatorType>::map_ptr);
if (!result.hasValue())
return nullptr;
if (auto *tracker = desc.SF->getReferencedNameTracker()) {
if (!result.getValue() ||
result.getValue()->getDeclContext()->getModuleScopeContext() !=
desc.SF) {
tracker->addTopLevelName(desc.name, desc.isCascading);
}
}
if (!result.getValue()) {
result = lookupOperatorDeclForName(desc.SF->getParentModule(), desc.diagLoc,
desc.name,
OperatorLookup<OperatorType>::map_ptr);
}
return result.hasValue() ? result.getValue() : nullptr;
}


#define LOOKUP_OPERATOR(Kind) \
Kind##Decl *ModuleDecl::lookup##Kind(Identifier name, SourceLoc loc) { \
auto result = \
lookupOperatorDeclForName(this, loc, name, &SourceFile::Kind##s); \
return result ? *result : nullptr; \
} \
Kind##Decl *SourceFile::lookup##Kind(Identifier name, bool cascades, \
SourceLoc loc) { \
return evaluateOrDefault( \
getASTContext().evaluator, \
Lookup##Kind##Request{{this, name, cascades, loc}}, nullptr); \
} \
template llvm::Expected<Kind##Decl *> \
LookupOperatorRequest<Kind##Decl>::evaluate(Evaluator &e, \
OperatorLookupDescriptor d) const;

LOOKUP_OPERATOR(PrefixOperator)
LOOKUP_OPERATOR(InfixOperator)
Expand Down
6 changes: 2 additions & 4 deletions lib/AST/ModuleNameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,9 @@ llvm::Expected<QualifiedLookupResult> LookupInModuleRequest::evaluate(
const DeclContext *moduleScopeContext) const {
assert(moduleScopeContext->isModuleScopeContext());

auto &ctx = moduleOrFile->getASTContext();
FrontendStatsTracer tracer(ctx.Stats, "lookup-in-module");

QualifiedLookupResult decls;
LookupByName lookup(ctx, resolutionKind, name, lookupKind);
LookupByName lookup(moduleOrFile->getASTContext(), resolutionKind,
name, lookupKind);
lookup.lookupInModule(decls, moduleOrFile, {}, moduleScopeContext);
return decls;
}
Expand Down
1 change: 0 additions & 1 deletion lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,6 @@ QualifiedLookupRequest::evaluate(Evaluator &eval, const DeclContext *DC,

// Visit all of the nominal types we know about, discovering any others
// we need along the way.
auto &ctx = DC->getASTContext();
bool wantProtocolMembers = (options & NL_ProtocolMembers);
while (!stack.empty()) {
auto current = stack.back();
Expand Down
21 changes: 19 additions & 2 deletions lib/AST/NameLookupRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@

#include "swift/AST/NameLookup.h"
#include "swift/AST/NameLookupRequests.h"
#include "swift/Subsystems.h"
#include "swift/AST/ASTContext.h"
#include "swift/AST/Evaluator.h"
#include "swift/AST/Decl.h"
#include "swift/AST/Evaluator.h"
#include "swift/AST/Module.h"
#include "swift/AST/SourceFile.h"
#include "swift/Subsystems.h"

using namespace swift;

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

//----------------------------------------------------------------------------//
// LookupOperatorRequest computation.
//----------------------------------------------------------------------------//

void swift::simple_display(llvm::raw_ostream &out,
const OperatorLookupDescriptor &desc) {
out << "looking up operator ";
simple_display(out, desc.name);
out << " in ";
simple_display(out, desc.SF);
}

SourceLoc swift::extractNearestSourceLoc(const OperatorLookupDescriptor &desc) {
return desc.diagLoc;
}

// Define request evaluation functions for each of the name lookup requests.
static AbstractRequestFunction *nameLookupRequestFunctions[] = {
#define SWIFT_REQUEST(Zone, Name, Sig, Caching, LocOptions) \
Expand Down
10 changes: 6 additions & 4 deletions lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1017,15 +1017,16 @@ void InterfaceTypeRequest::cacheResult(Type type) const {
}

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

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

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

void LookupPrecedenceGroupRequest::noteCycleStep(DiagnosticEngine &diag) const {
void ValidatePrecedenceGroupRequest::noteCycleStep(
DiagnosticEngine &diag) const {
auto &desc = std::get<0>(getStorage());
diag.diagnose(desc.nameLoc,
diag::circular_reference_through_precedence_group, desc.ident);
Expand Down
Loading