Skip to content

Commit 9e2cdf4

Browse files
authored
Merge pull request #31506 from hamishknight/hello-operator
2 parents 4eb5689 + e7cb6bb commit 9e2cdf4

31 files changed

+905
-481
lines changed

include/swift/AST/Decl.h

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7054,6 +7054,10 @@ class PrecedenceGroupDecl : public Decl {
70547054
return Name;
70557055
}
70567056

7057+
// This is needed to allow templated code to work with both ValueDecls and
7058+
// PrecedenceGroupDecls.
7059+
DeclBaseName getBaseName() const { return Name; }
7060+
70577061
SourceLoc getLBraceLoc() const { return LBraceLoc; }
70587062
SourceLoc getRBraceLoc() const { return RBraceLoc; }
70597063

@@ -7211,6 +7215,10 @@ class OperatorDecl : public Decl {
72117215
SourceLoc getNameLoc() const { return NameLoc; }
72127216
Identifier getName() const { return name; }
72137217

7218+
// This is needed to allow templated code to work with both ValueDecls and
7219+
// OperatorDecls.
7220+
DeclBaseName getBaseName() const { return name; }
7221+
72147222
/// Get the list of identifiers after the colon in the operator declaration.
72157223
///
72167224
/// This list includes the names of designated types. For infix operators, the
@@ -7271,12 +7279,6 @@ class InfixOperatorDecl : public OperatorDecl {
72717279

72727280
PrecedenceGroupDecl *getPrecedenceGroup() const;
72737281

7274-
/// True if this decl's attributes conflict with those declared by another
7275-
/// operator.
7276-
bool conflictsWith(InfixOperatorDecl *other) {
7277-
return getPrecedenceGroup() != other->getPrecedenceGroup();
7278-
}
7279-
72807282
static bool classof(const Decl *D) {
72817283
return D->getKind() == DeclKind::InfixOperator;
72827284
}
@@ -7305,12 +7307,6 @@ class PrefixOperatorDecl : public OperatorDecl {
73057307
return { getOperatorLoc(), getNameLoc() };
73067308
}
73077309

7308-
/// True if this decl's attributes conflict with those declared by another
7309-
/// PrefixOperatorDecl.
7310-
bool conflictsWith(PrefixOperatorDecl *other) {
7311-
return false;
7312-
}
7313-
73147310
static bool classof(const Decl *D) {
73157311
return D->getKind() == DeclKind::PrefixOperator;
73167312
}
@@ -7338,12 +7334,6 @@ class PostfixOperatorDecl : public OperatorDecl {
73387334
SourceRange getSourceRange() const {
73397335
return { getOperatorLoc(), getNameLoc() };
73407336
}
7341-
7342-
/// True if this decl's attributes conflict with those declared by another
7343-
/// PostfixOperatorDecl.
7344-
bool conflictsWith(PostfixOperatorDecl *other) {
7345-
return false;
7346-
}
73477337

73487338
static bool classof(const Decl *D) {
73497339
return D->getKind() == DeclKind::PostfixOperator;

include/swift/AST/DeclContext.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,19 @@ namespace swift {
5555
class GenericSignature;
5656
class GenericTypeParamDecl;
5757
class GenericTypeParamType;
58+
class InfixOperatorDecl;
59+
class InfixOperatorLookupResult;
60+
class PrecedenceGroupDecl;
5861
class ProtocolDecl;
5962
class Requirement;
6063
class SourceFile;
6164
class Type;
6265
class ModuleDecl;
6366
class GenericTypeDecl;
6467
class NominalTypeDecl;
68+
class PrecedenceGroupLookupResult;
69+
class PostfixOperatorDecl;
70+
class PrefixOperatorDecl;
6571
class ProtocolConformance;
6672
class ValueDecl;
6773
class Initializer;
@@ -562,6 +568,29 @@ class alignas(1 << DeclContextAlignInBits) DeclContext {
562568
ObjCSelector selector,
563569
SmallVectorImpl<AbstractFunctionDecl *> &results) const;
564570

571+
/// Looks up an infix operator with a given \p name.
572+
///
573+
/// This returns a vector of results, as it's possible to find multiple infix
574+
/// operators with different precedence groups.
575+
InfixOperatorLookupResult lookupInfixOperator(Identifier name) const;
576+
577+
/// Looks up an prefix operator with a given \p name.
578+
///
579+
/// If multiple results are found, one is chosen in a stable manner, as
580+
/// prefix operator decls cannot differ other than in name. If no results are
581+
/// found, returns \c nullptr.
582+
PrefixOperatorDecl *lookupPrefixOperator(Identifier name) const;
583+
584+
/// Looks up an postfix operator with a given \p name.
585+
///
586+
/// If multiple results are found, one is chosen in a stable manner, as
587+
/// postfix operator decls cannot differ other than in name. If no results are
588+
/// found, returns \c nullptr.
589+
PostfixOperatorDecl *lookupPostfixOperator(Identifier name) const;
590+
591+
/// Looks up a precedence group with a given \p name.
592+
PrecedenceGroupLookupResult lookupPrecedenceGroup(Identifier name) const;
593+
565594
/// Return the ASTContext for a specified DeclContext by
566595
/// walking up to the enclosing module and returning its ASTContext.
567596
LLVM_READONLY

include/swift/AST/Module.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -590,21 +590,6 @@ class ModuleDecl : public DeclContext, public TypeDecl {
590590
/// FIXME: Remove the integrated REPL.
591591
void clearLookupCache();
592592

593-
/// @{
594-
595-
/// Look up the given operator in this module.
596-
///
597-
/// If the operator is not found, or if there is an ambiguity, returns null.
598-
InfixOperatorDecl *lookupInfixOperator(Identifier name,
599-
SourceLoc diagLoc = {});
600-
PrefixOperatorDecl *lookupPrefixOperator(Identifier name,
601-
SourceLoc diagLoc = {});
602-
PostfixOperatorDecl *lookupPostfixOperator(Identifier name,
603-
SourceLoc diagLoc = {});
604-
PrecedenceGroupDecl *lookupPrecedenceGroup(Identifier name,
605-
SourceLoc diagLoc = {});
606-
/// @}
607-
608593
/// Finds all class members defined in this module.
609594
///
610595
/// This does a simple local lookup, not recursively looking through imports.

include/swift/AST/NameLookup.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,26 @@ bool removeOverriddenDecls(SmallVectorImpl<ValueDecl*> &decls);
455455
bool removeShadowedDecls(SmallVectorImpl<ValueDecl*> &decls,
456456
const DeclContext *dc);
457457

458+
/// Remove any operators in the given set that are shadowed by
459+
/// other operators in that set.
460+
///
461+
/// \param decls The set of operators being considered.
462+
/// \param dc The DeclContext from which the lookup was performed.
463+
///
464+
/// \returns true if any shadowed declarations were removed.
465+
bool removeShadowedDecls(TinyPtrVector<OperatorDecl *> &decls,
466+
const DeclContext *dc);
467+
468+
/// Remove any precedence groups in the given set that are shadowed by
469+
/// other precedence groups in that set.
470+
///
471+
/// \param decls The set of precedence groups being considered.
472+
/// \param dc The DeclContext from which the lookup was performed.
473+
///
474+
/// \returns true if any shadowed declarations were removed.
475+
bool removeShadowedDecls(TinyPtrVector<PrecedenceGroupDecl *> &decls,
476+
const DeclContext *dc);
477+
458478
/// Finds decls visible in the given context and feeds them to the given
459479
/// VisibleDeclConsumer. If the current DeclContext is nested in a function,
460480
/// the SourceLoc is used to determine which declarations in that function

include/swift/AST/NameLookupRequests.h

Lines changed: 95 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,10 @@ class OperatorLookupDescriptor final {
594594
using Storage = llvm::PointerUnion<FileUnit *, ModuleDecl *>;
595595
Storage fileOrModule;
596596
Identifier name;
597-
bool isCascading;
598-
SourceLoc diagLoc;
599597

600598
private:
601-
OperatorLookupDescriptor(Storage fileOrModule, Identifier name,
602-
bool isCascading, SourceLoc diagLoc)
603-
: fileOrModule(fileOrModule), name(name), isCascading(isCascading),
604-
diagLoc(diagLoc) {}
599+
OperatorLookupDescriptor(Storage fileOrModule, Identifier name)
600+
: fileOrModule(fileOrModule), name(name) {}
605601

606602
public:
607603
/// Retrieves the files to perform lookup in.
@@ -613,66 +609,43 @@ class OperatorLookupDescriptor final {
613609
return fileOrModule.dyn_cast<ModuleDecl *>();
614610
}
615611

612+
/// Retrieve the file or module for the lookup, as a DeclContext.
613+
DeclContext *getDC() const {
614+
if (auto *module = getModule())
615+
return module;
616+
return fileOrModule.get<FileUnit *>();
617+
}
618+
616619
friend llvm::hash_code hash_value(const OperatorLookupDescriptor &desc) {
617-
return llvm::hash_combine(desc.fileOrModule, desc.name, desc.isCascading);
620+
return llvm::hash_combine(desc.fileOrModule, desc.name);
618621
}
619622

620623
friend bool operator==(const OperatorLookupDescriptor &lhs,
621624
const OperatorLookupDescriptor &rhs) {
622-
return lhs.fileOrModule == rhs.fileOrModule && lhs.name == rhs.name &&
623-
lhs.isCascading == rhs.isCascading;
625+
return lhs.fileOrModule == rhs.fileOrModule && lhs.name == rhs.name;
624626
}
625627

626628
friend bool operator!=(const OperatorLookupDescriptor &lhs,
627629
const OperatorLookupDescriptor &rhs) {
628630
return !(lhs == rhs);
629631
}
630632

631-
static OperatorLookupDescriptor forFile(FileUnit *file, Identifier name,
632-
bool isCascading, SourceLoc diagLoc) {
633-
return OperatorLookupDescriptor(file, name, isCascading, diagLoc);
633+
static OperatorLookupDescriptor forFile(FileUnit *file, Identifier name) {
634+
return OperatorLookupDescriptor(file, name);
634635
}
635636

636-
static OperatorLookupDescriptor forModule(ModuleDecl *mod, Identifier name,
637-
bool isCascading,
638-
SourceLoc diagLoc) {
639-
return OperatorLookupDescriptor(mod, name, isCascading, diagLoc);
637+
static OperatorLookupDescriptor forModule(ModuleDecl *mod, Identifier name) {
638+
return OperatorLookupDescriptor(mod, name);
640639
}
640+
641+
static OperatorLookupDescriptor forDC(const DeclContext *DC, Identifier name);
641642
};
642643

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

646647
SourceLoc extractNearestSourceLoc(const OperatorLookupDescriptor &desc);
647648

648-
template <typename OperatorType>
649-
class LookupOperatorRequest
650-
: public SimpleRequest<LookupOperatorRequest<OperatorType>,
651-
OperatorType *(OperatorLookupDescriptor),
652-
RequestFlags::Cached> {
653-
using SimpleRequest<LookupOperatorRequest<OperatorType>,
654-
OperatorType *(OperatorLookupDescriptor),
655-
RequestFlags::Cached>::SimpleRequest;
656-
657-
private:
658-
friend SimpleRequest<LookupOperatorRequest<OperatorType>,
659-
OperatorType *(OperatorLookupDescriptor),
660-
RequestFlags::Cached>;
661-
662-
// Evaluation.
663-
OperatorType *evaluate(Evaluator &evaluator,
664-
OperatorLookupDescriptor desc) const;
665-
666-
public:
667-
// Cached.
668-
bool isCached() const { return true; }
669-
};
670-
671-
using LookupPrefixOperatorRequest = LookupOperatorRequest<PrefixOperatorDecl>;
672-
using LookupInfixOperatorRequest = LookupOperatorRequest<InfixOperatorDecl>;
673-
using LookupPostfixOperatorRequest = LookupOperatorRequest<PostfixOperatorDecl>;
674-
using LookupPrecedenceGroupRequest = LookupOperatorRequest<PrecedenceGroupDecl>;
675-
676649
/// Looks up an operator in a given file or module without looking through
677650
/// imports.
678651
class DirectOperatorLookupRequest
@@ -770,6 +743,84 @@ class LookupConformanceInModuleRequest
770743
ProtocolConformanceRef result) const;
771744
};
772745

746+
/// Look up an 'infix operator' decl by name.
747+
class LookupInfixOperatorRequest
748+
: public SimpleRequest<LookupInfixOperatorRequest,
749+
TinyPtrVector<InfixOperatorDecl *>(
750+
OperatorLookupDescriptor),
751+
RequestFlags::Cached> {
752+
public:
753+
using SimpleRequest::SimpleRequest;
754+
755+
private:
756+
friend SimpleRequest;
757+
758+
TinyPtrVector<InfixOperatorDecl *>
759+
evaluate(Evaluator &evaluator, OperatorLookupDescriptor desc) const;
760+
761+
public:
762+
// Cached.
763+
bool isCached() const { return true; }
764+
};
765+
766+
/// Look up an 'prefix operator' decl by name.
767+
class LookupPrefixOperatorRequest
768+
: public SimpleRequest<LookupPrefixOperatorRequest,
769+
PrefixOperatorDecl *(OperatorLookupDescriptor),
770+
RequestFlags::Cached> {
771+
public:
772+
using SimpleRequest::SimpleRequest;
773+
774+
private:
775+
friend SimpleRequest;
776+
777+
PrefixOperatorDecl *evaluate(Evaluator &evaluator,
778+
OperatorLookupDescriptor desc) const;
779+
780+
public:
781+
// Cached.
782+
bool isCached() const { return true; }
783+
};
784+
785+
/// Look up an 'postfix operator' decl by name.
786+
class LookupPostfixOperatorRequest
787+
: public SimpleRequest<LookupPostfixOperatorRequest,
788+
PostfixOperatorDecl *(OperatorLookupDescriptor),
789+
RequestFlags::Cached> {
790+
public:
791+
using SimpleRequest::SimpleRequest;
792+
793+
private:
794+
friend SimpleRequest;
795+
796+
PostfixOperatorDecl *evaluate(Evaluator &evaluator,
797+
OperatorLookupDescriptor desc) const;
798+
799+
public:
800+
// Cached.
801+
bool isCached() const { return true; }
802+
};
803+
804+
/// Look up a precedencegroup decl by name.
805+
class LookupPrecedenceGroupRequest
806+
: public SimpleRequest<LookupPrecedenceGroupRequest,
807+
TinyPtrVector<PrecedenceGroupDecl *>(
808+
OperatorLookupDescriptor),
809+
RequestFlags::Cached> {
810+
public:
811+
using SimpleRequest::SimpleRequest;
812+
813+
private:
814+
friend SimpleRequest;
815+
816+
TinyPtrVector<PrecedenceGroupDecl *>
817+
evaluate(Evaluator &evaluator, OperatorLookupDescriptor descriptor) const;
818+
819+
public:
820+
// Cached.
821+
bool isCached() const { return true; }
822+
};
823+
773824
#define SWIFT_TYPEID_ZONE NameLookup
774825
#define SWIFT_TYPEID_HEADER "swift/AST/NameLookupTypeIDZone.def"
775826
#include "swift/Basic/DefineTypeIDZone.h"

include/swift/AST/NameLookupTypeIDZone.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,11 @@ SWIFT_REQUEST(NameLookup, LookupPrefixOperatorRequest,
8989
PrefixOperatorDecl *(OperatorLookupDescriptor),
9090
Cached, NoLocationInfo)
9191
SWIFT_REQUEST(NameLookup, LookupInfixOperatorRequest,
92-
InfixOperatorDecl *(OperatorLookupDescriptor),
92+
TinyPtrVector<InfixOperatorDecl *>(OperatorLookupDescriptor),
9393
Cached, NoLocationInfo)
9494
SWIFT_REQUEST(NameLookup, LookupPostfixOperatorRequest,
9595
PostfixOperatorDecl *(OperatorLookupDescriptor),
9696
Cached, NoLocationInfo)
9797
SWIFT_REQUEST(NameLookup, LookupPrecedenceGroupRequest,
98-
PrecedenceGroupDecl *(OperatorLookupDescriptor),
98+
TinyPtrVector<PrecedenceGroupDecl *>(OperatorLookupDescriptor),
9999
Cached, NoLocationInfo)

0 commit comments

Comments
 (0)