Skip to content

Commit a502246

Browse files
committed
[AST] Add OperatorFixity
Use this instead of DeclKind in a few places.
1 parent e5952ab commit a502246

File tree

10 files changed

+60
-22
lines changed

10 files changed

+60
-22
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,9 @@ class LoadedFile : public FileUnit {
346346
///
347347
/// \param name The operator name ("+", ">>", etc.)
348348
///
349-
/// \param fixity One of PrefixOperator, InfixOperator, or PostfixOperator.
350-
virtual OperatorDecl *lookupOperator(Identifier name, DeclKind fixity) const {
349+
/// \param fixity One of Prefix, Infix, or Postfix.
350+
virtual OperatorDecl *lookupOperator(Identifier name,
351+
OperatorFixity fixity) const {
351352
return nullptr;
352353
}
353354

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ class SerializedASTFile final : public LoadedFile {
329329
const NominalTypeDecl *parent) const override;
330330

331331
virtual OperatorDecl *lookupOperator(Identifier name,
332-
DeclKind fixity) const override;
332+
OperatorFixity fixity) const override;
333333

334334
virtual PrecedenceGroupDecl *
335335
lookupPrecedenceGroup(Identifier name) const override;

lib/AST/Module.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ namespace {
948948
template <typename T>
949949
static PrefixOperatorDecl *lookup(T &container, Identifier name) {
950950
return cast_or_null<PrefixOperatorDecl>(
951-
container.lookupOperator(name, DeclKind::PrefixOperator));
951+
container.lookupOperator(name, OperatorFixity::Prefix));
952952
}
953953
};
954954

@@ -958,7 +958,7 @@ namespace {
958958
template <typename T>
959959
static InfixOperatorDecl *lookup(T &container, Identifier name) {
960960
return cast_or_null<InfixOperatorDecl>(
961-
container.lookupOperator(name, DeclKind::InfixOperator));
961+
container.lookupOperator(name, OperatorFixity::Infix));
962962
}
963963
};
964964

@@ -968,7 +968,7 @@ namespace {
968968
template <typename T>
969969
static PostfixOperatorDecl *lookup(T &container, Identifier name) {
970970
return cast_or_null<PostfixOperatorDecl>(
971-
container.lookupOperator(name, DeclKind::PostfixOperator));
971+
container.lookupOperator(name, OperatorFixity::Postfix));
972972
}
973973
};
974974

lib/Serialization/Deserialization.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1654,7 +1654,7 @@ ModuleFile::resolveCrossReference(ModuleID MID, uint32_t pathLen) {
16541654
return true;
16551655
if (!fn->getOperatorDecl())
16561656
return true;
1657-
if (getStableFixity(fn->getOperatorDecl()->getKind()) != rawKind)
1657+
if (getStableFixity(fn->getOperatorDecl()->getFixity()) != rawKind)
16581658
return true;
16591659
return false;
16601660
});

lib/Serialization/ModuleFile.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2156,7 +2156,8 @@ TypeDecl *ModuleFile::lookupNestedType(Identifier name,
21562156
return nullptr;
21572157
}
21582158

2159-
OperatorDecl *ModuleFile::lookupOperator(Identifier name, DeclKind fixity) {
2159+
OperatorDecl *ModuleFile::lookupOperator(Identifier name,
2160+
OperatorFixity fixity) {
21602161
PrettyStackTraceModuleFile stackEntry(*this);
21612162

21622163
if (!OperatorDecls)

lib/Serialization/ModuleFile.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ class ModuleFile
731731
/// Searches the module's operators for one with the given name and fixity.
732732
///
733733
/// If none is found, returns null.
734-
OperatorDecl *lookupOperator(Identifier name, DeclKind fixity);
734+
OperatorDecl *lookupOperator(Identifier name, OperatorFixity fixity);
735735

736736
/// Searches the module's precedence groups for one with the given
737737
/// name and fixity.

lib/Serialization/ModuleFormat.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,18 @@ enum class SelfAccessKind : uint8_t {
385385
};
386386
using SelfAccessKindField = BCFixed<2>;
387387

388-
/// Translates an operator DeclKind to a Serialization fixity, whose values are
389-
/// guaranteed to be stable.
390-
static inline OperatorKind getStableFixity(DeclKind kind) {
391-
switch (kind) {
392-
case DeclKind::PrefixOperator:
388+
/// Translates an operator decl fixity to a Serialization fixity, whose values
389+
/// are guaranteed to be stable.
390+
static inline OperatorKind getStableFixity(OperatorFixity fixity) {
391+
switch (fixity) {
392+
case OperatorFixity::Prefix:
393393
return Prefix;
394-
case DeclKind::PostfixOperator:
394+
case OperatorFixity::Postfix:
395395
return Postfix;
396-
case DeclKind::InfixOperator:
396+
case OperatorFixity::Infix:
397397
return Infix;
398-
default:
399-
llvm_unreachable("unknown operator fixity");
400398
}
399+
llvm_unreachable("Unhandled case in switch");
401400
}
402401

403402
// These IDs must \em not be renumbered or reordered without incrementing

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1730,7 +1730,7 @@ void Serializer::writeCrossReference(const DeclContext *DC, uint32_t pathLen) {
17301730
assert(op);
17311731
abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
17321732
auto emptyID = addDeclBaseNameRef(Identifier());
1733-
auto fixity = getStableFixity(op->getKind());
1733+
auto fixity = getStableFixity(op->getFixity());
17341734
XRefOperatorOrAccessorPathPieceLayout::emitRecord(Out, ScratchRecord,
17351735
abbrCode, emptyID,
17361736
fixity);
@@ -1750,7 +1750,7 @@ void Serializer::writeCrossReference(const Decl *D) {
17501750

17511751
abbrCode = DeclTypeAbbrCodes[XRefOperatorOrAccessorPathPieceLayout::Code];
17521752
auto nameID = addDeclBaseNameRef(op->getName());
1753-
auto fixity = getStableFixity(op->getKind());
1753+
auto fixity = getStableFixity(op->getFixity());
17541754
XRefOperatorOrAccessorPathPieceLayout::emitRecord(Out, ScratchRecord,
17551755
abbrCode, nameID,
17561756
fixity);
@@ -4924,7 +4924,7 @@ void Serializer::writeAST(ModuleOrSourceFile DC) {
49244924
.push_back({ extendedNominal, addDeclRef(D) });
49254925
} else if (auto OD = dyn_cast<OperatorDecl>(D)) {
49264926
operatorDecls[OD->getName()]
4927-
.push_back({ getStableFixity(OD->getKind()), addDeclRef(D) });
4927+
.push_back({ getStableFixity(OD->getFixity()), addDeclRef(D) });
49284928
} else if (auto PGD = dyn_cast<PrecedenceGroupDecl>(D)) {
49294929
precedenceGroupDecls[PGD->getName()]
49304930
.push_back({ decls_block::PRECEDENCE_GROUP_DECL, addDeclRef(D) });

lib/Serialization/SerializedModuleLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1085,7 +1085,7 @@ SerializedASTFile::lookupNestedType(Identifier name,
10851085
}
10861086

10871087
OperatorDecl *SerializedASTFile::lookupOperator(Identifier name,
1088-
DeclKind fixity) const {
1088+
OperatorFixity fixity) const {
10891089
return File.lookupOperator(name, fixity);
10901090
}
10911091

0 commit comments

Comments
 (0)