Skip to content

Commit de46690

Browse files
authored
Add direct operator lookup requests (swiftlang#30584)
Add direct operator lookup requests
2 parents b0ca53f + cc9c851 commit de46690

27 files changed

+610
-107
lines changed

include/swift/AST/Decl.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7063,6 +7063,28 @@ class PrecedenceGroupDecl : public Decl {
70637063
}
70647064
};
70657065

7066+
/// The fixity of an OperatorDecl.
7067+
enum class OperatorFixity : uint8_t {
7068+
Infix,
7069+
Prefix,
7070+
Postfix
7071+
};
7072+
7073+
inline void simple_display(llvm::raw_ostream &out, OperatorFixity fixity) {
7074+
switch (fixity) {
7075+
case OperatorFixity::Infix:
7076+
out << "infix";
7077+
return;
7078+
case OperatorFixity::Prefix:
7079+
out << "prefix";
7080+
return;
7081+
case OperatorFixity::Postfix:
7082+
out << "postfix";
7083+
return;
7084+
}
7085+
llvm_unreachable("Unhandled case in switch");
7086+
}
7087+
70667088
/// Abstract base class of operator declarations.
70677089
class OperatorDecl : public Decl {
70687090
SourceLoc OperatorLoc, NameLoc;
@@ -7088,6 +7110,21 @@ class OperatorDecl : public Decl {
70887110
: Decl(kind, DC), OperatorLoc(OperatorLoc), NameLoc(NameLoc), name(Name),
70897111
DesignatedNominalTypes(DesignatedNominalTypes) {}
70907112

7113+
/// Retrieve the operator's fixity, corresponding to the concrete subclass
7114+
/// of the OperatorDecl.
7115+
OperatorFixity getFixity() const {
7116+
switch (getKind()) {
7117+
#define DECL(Id, Name) case DeclKind::Id: llvm_unreachable("Not an operator!");
7118+
#define OPERATOR_DECL(Id, Name)
7119+
#include "swift/AST/DeclNodes.def"
7120+
case DeclKind::InfixOperator:
7121+
return OperatorFixity::Infix;
7122+
case DeclKind::PrefixOperator:
7123+
return OperatorFixity::Prefix;
7124+
case DeclKind::PostfixOperator:
7125+
return OperatorFixity::Postfix;
7126+
}
7127+
}
70917128

70927129
SourceLoc getOperatorLoc() const { return OperatorLoc; }
70937130
SourceLoc getNameLoc() const { return NameLoc; }

include/swift/AST/FileUnit.h

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ class FileUnit : public DeclContext {
3030
#pragma clang diagnostic pop
3131
virtual void anchor();
3232

33+
friend class DirectOperatorLookupRequest;
34+
friend class DirectPrecedenceGroupLookupRequest;
35+
3336
// FIXME: Stick this in a PointerIntPair.
3437
const FileUnitKind Kind;
3538

@@ -107,6 +110,25 @@ class FileUnit : public DeclContext {
107110
const ModuleDecl *importedModule,
108111
SmallVectorImpl<Identifier> &spiGroups) const {};
109112

113+
protected:
114+
/// Look up an operator declaration. Do not call directly, use
115+
/// \c DirectOperatorLookupRequest instead.
116+
///
117+
/// \param name The operator name ("+", ">>", etc.)
118+
///
119+
/// \param fixity One of Prefix, Infix, or Postfix.
120+
virtual void
121+
lookupOperatorDirect(Identifier name, OperatorFixity fixity,
122+
TinyPtrVector<OperatorDecl *> &results) const {}
123+
124+
/// Look up a precedence group. Do not call directly, use
125+
/// \c DirectPrecedenceGroupLookupRequest instead.
126+
///
127+
/// \param name The precedence group name.
128+
virtual void lookupPrecedenceGroupDirect(
129+
Identifier name, TinyPtrVector<PrecedenceGroupDecl *> &results) const {}
130+
131+
public:
110132
/// Returns the comment attached to the given declaration.
111133
///
112134
/// This function is an implementation detail for comment serialization.
@@ -342,22 +364,6 @@ class LoadedFile : public FileUnit {
342364
return StringRef();
343365
}
344366

345-
/// Look up an operator declaration.
346-
///
347-
/// \param name The operator name ("+", ">>", etc.)
348-
///
349-
/// \param fixity One of PrefixOperator, InfixOperator, or PostfixOperator.
350-
virtual OperatorDecl *lookupOperator(Identifier name, DeclKind fixity) const {
351-
return nullptr;
352-
}
353-
354-
/// Look up a precedence group.
355-
///
356-
/// \param name The precedence group name.
357-
virtual PrecedenceGroupDecl *lookupPrecedenceGroup(Identifier name) const {
358-
return nullptr;
359-
}
360-
361367
/// Returns the Swift module that overlays a Clang module.
362368
virtual ModuleDecl *getOverlayModule() const { return nullptr; }
363369

include/swift/AST/NameLookupRequests.h

Lines changed: 66 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,
@@ -572,6 +597,41 @@ using LookupInfixOperatorRequest = LookupOperatorRequest<InfixOperatorDecl>;
572597
using LookupPostfixOperatorRequest = LookupOperatorRequest<PostfixOperatorDecl>;
573598
using LookupPrecedenceGroupRequest = LookupOperatorRequest<PrecedenceGroupDecl>;
574599

600+
/// Looks up an operator in a given file or module without looking through
601+
/// imports.
602+
class DirectOperatorLookupRequest
603+
: public SimpleRequest<DirectOperatorLookupRequest,
604+
TinyPtrVector<OperatorDecl *>(
605+
OperatorLookupDescriptor, OperatorFixity),
606+
CacheKind::Uncached> {
607+
public:
608+
using SimpleRequest::SimpleRequest;
609+
610+
private:
611+
friend SimpleRequest;
612+
613+
llvm::Expected<TinyPtrVector<OperatorDecl *>>
614+
evaluate(Evaluator &evaluator, OperatorLookupDescriptor descriptor,
615+
OperatorFixity fixity) const;
616+
};
617+
618+
/// Looks up an precedencegroup in a given file or module without looking
619+
/// through imports.
620+
class DirectPrecedenceGroupLookupRequest
621+
: public SimpleRequest<DirectPrecedenceGroupLookupRequest,
622+
TinyPtrVector<PrecedenceGroupDecl *>(
623+
OperatorLookupDescriptor),
624+
CacheKind::Uncached> {
625+
public:
626+
using SimpleRequest::SimpleRequest;
627+
628+
private:
629+
friend SimpleRequest;
630+
631+
llvm::Expected<TinyPtrVector<PrecedenceGroupDecl *>>
632+
evaluate(Evaluator &evaluator, OperatorLookupDescriptor descriptor) const;
633+
};
634+
575635
#define SWIFT_TYPEID_ZONE NameLookup
576636
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
577637
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,13 @@ SWIFT_REQUEST(NameLookup, CustomAttrNominalRequest,
2424
SWIFT_REQUEST(NameLookup, DirectLookupRequest,
2525
TinyPtrVector<ValueDecl *>(DirectLookupDescriptor), Uncached,
2626
NoLocationInfo)
27+
SWIFT_REQUEST(NameLookup, DirectOperatorLookupRequest,
28+
TinyPtrVector<OperatorDecl *>(OperatorLookupDescriptor,
29+
OperatorFixity),
30+
Uncached, NoLocationInfo)
31+
SWIFT_REQUEST(NameLookup, DirectPrecedenceGroupLookupRequest,
32+
TinyPtrVector<PrecedenceGroupDecl *>(OperatorLookupDescriptor),
33+
Uncached, NoLocationInfo)
2734
SWIFT_REQUEST(NameLookup, ExpandASTScopeRequest,
2835
ast_scope::ASTScopeImpl* (ast_scope::ASTScopeImpl*, ast_scope::ScopeCreator*),
2936
SeparatelyCached,

include/swift/AST/SourceFile.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ class SourceFile final : public FileUnit {
436436
ObjCSelector selector,
437437
SmallVectorImpl<AbstractFunctionDecl *> &results) const override;
438438

439+
protected:
440+
virtual void
441+
lookupOperatorDirect(Identifier name, OperatorFixity fixity,
442+
TinyPtrVector<OperatorDecl *> &results) const override;
443+
444+
virtual void lookupPrecedenceGroupDirect(
445+
Identifier name,
446+
TinyPtrVector<PrecedenceGroupDecl *> &results) const override;
447+
448+
public:
439449
virtual void getTopLevelDecls(SmallVectorImpl<Decl*> &results) const override;
440450

441451
virtual void

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,12 +328,16 @@ class SerializedASTFile final : public LoadedFile {
328328
lookupNestedType(Identifier name,
329329
const NominalTypeDecl *parent) const override;
330330

331-
virtual OperatorDecl *lookupOperator(Identifier name,
332-
DeclKind fixity) const override;
331+
protected:
332+
virtual void
333+
lookupOperatorDirect(Identifier name, OperatorFixity fixity,
334+
TinyPtrVector<OperatorDecl *> &results) const override;
333335

334-
virtual PrecedenceGroupDecl *
335-
lookupPrecedenceGroup(Identifier name) const override;
336+
virtual void lookupPrecedenceGroupDirect(
337+
Identifier name,
338+
TinyPtrVector<PrecedenceGroupDecl *> &results) const override;
336339

340+
public:
337341
virtual void lookupVisibleDecls(ModuleDecl::AccessPathTy accessPath,
338342
VisibleDeclConsumer &consumer,
339343
NLKind lookupKind) const override;

0 commit comments

Comments
 (0)