Skip to content

Commit 92e70ef

Browse files
committed
[AST] Remove DeclAttrKind::Count
Introduce NumDeclAttrKinds for number of enum values. Use optional for invalid attribute kind. This align with `TypeAttrKind`.
1 parent 8fdc4cc commit 92e70ef

File tree

12 files changed

+68
-88
lines changed

12 files changed

+68
-88
lines changed

include/swift/AST/ASTBridging.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
423423

424424
enum ENUM_EXTENSIBILITY_ATTR(closed) BridgedDeclAttrKind {
425425
#define DECL_ATTR(_, CLASS, ...) BridgedDeclAttrKind##CLASS,
426-
#include "swift/AST/DelAttr.def"
426+
#include "swift/AST/DeclAttr.def"
427427
BridgedDeclAttrKindNone,
428428
};
429429

include/swift/AST/ASTVisitor.h

-3
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,6 @@ class ASTVisitor {
142142
return static_cast<ImplClass *>(this)->visit##CLASS##Attr( \
143143
static_cast<CLASS##Attr *>(A), ::std::forward<Args>(AA)...);
144144
#include "swift/AST/DeclAttr.def"
145-
146-
case DeclAttrKind::Count:
147-
llvm_unreachable("Not an attribute kind");
148145
}
149146
}
150147

include/swift/AST/Attr.h

+2-3
Original file line numberDiff line numberDiff line change
@@ -470,10 +470,9 @@ class DeclAttribute : public AttributeBase {
470470
/// of the 'unowned(unsafe)' attribute, the string passed in is 'unowned'.
471471
///
472472
/// Also note that this recognizes both attributes like '@inline' (with no @)
473-
/// and decl modifiers like 'final'. This returns DeclAttrKind::Count on
474-
/// failure.
473+
/// and decl modifiers like 'final'.
475474
///
476-
static DeclAttrKind getAttrKindFromString(StringRef Str);
475+
static llvm::Optional<DeclAttrKind> getAttrKindFromString(StringRef Str);
477476

478477
static DeclAttribute *createSimple(const ASTContext &context,
479478
DeclAttrKind kind, SourceLoc atLoc,

include/swift/AST/AttrKind.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,14 @@ enum : unsigned { NumExternKindBits =
133133
enum class DeclAttrKind : unsigned {
134134
#define DECL_ATTR(_, CLASS, ...) CLASS,
135135
#include "swift/AST/DeclAttr.def"
136-
Count
137136
};
138137

139138
enum : unsigned {
140-
NumDeclAttrKindBits =
141-
countBitsUsed(static_cast<unsigned>(DeclAttrKind::Count) - 1)
139+
#define DECL_ATTR(_, C, ...) _counting_DAK_##C,
140+
#include "swift/AST/DeclAttr.def"
141+
NumDeclAttrKinds,
142+
143+
NumDeclAttrKindBits = countBitsUsed(NumDeclAttrKinds - 1)
142144
};
143145

144146
// Define enumerators for each type attribute, e.g. TypeAttrKind::Weak.

include/swift/AST/PrintOptions.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ class AnyAttrKind {
9393
static_assert(NumTypeAttrKinds < UINT_MAX, "TypeAttrKind is > 31 bits");
9494
}
9595
AnyAttrKind(DeclAttrKind K) : kind(static_cast<unsigned>(K)), isType(0) {
96-
static_assert(static_cast<unsigned>(DeclAttrKind::Count) < UINT_MAX,
97-
"DeclAttrKind is > 31 bits");
96+
static_assert(NumDeclAttrKinds < UINT_MAX, "DeclAttrKind is > 31 bits");
9897
}
9998
AnyAttrKind() : kind(NumTypeAttrKinds), isType(1) {}
10099

@@ -103,10 +102,11 @@ class AnyAttrKind {
103102
if (!isType || kind == NumTypeAttrKinds) return {};
104103
return static_cast<TypeAttrKind>(kind);
105104
}
106-
/// Returns the DeclAttrKind, or DeclAttrKind::Count if this is not a decl
107-
/// attribute.
108-
DeclAttrKind decl() const {
109-
return isType ? DeclAttrKind::Count : static_cast<DeclAttrKind>(kind);
105+
/// Returns the DeclAttrKind.
106+
llvm::Optional<DeclAttrKind> decl() const {
107+
if (isType || kind == NumDeclAttrKinds)
108+
return {};
109+
return static_cast<DeclAttrKind>(kind);
110110
}
111111

112112
bool operator==(AnyAttrKind K) const {

lib/APIDigester/ModuleAnalyzerNodes.cpp

+10-13
Original file line numberDiff line numberDiff line change
@@ -724,19 +724,18 @@ SDKNode* SDKNode::constructSDKNode(SDKContext &Ctx,
724724
}
725725
case KeyKind::KK_declAttributes: {
726726
auto *Seq = cast<llvm::yaml::SequenceNode>(Pair.getValue());
727-
std::transform(
728-
Seq->begin(), Seq->end(), std::back_inserter(Info.DeclAttrs),
729-
[&](llvm::yaml::Node &N) {
730-
auto Result =
731-
llvm::StringSwitch<DeclAttrKind>(GetScalarString(&N))
727+
for (auto &N : *Seq) {
728+
auto Result = llvm::StringSwitch<llvm::Optional<DeclAttrKind>>(
729+
GetScalarString(&N))
732730
#define DECL_ATTR(_, NAME, ...) .Case(#NAME, DeclAttrKind::NAME)
733731
#include "swift/AST/DeclAttr.def"
734-
.Default(DeclAttrKind::Count);
735-
if (Result == DeclAttrKind::Count)
736-
Ctx.diagnose(&N, diag::sdk_node_unrecognized_decl_attr_kind,
737-
GetScalarString(&N));
738-
return Result;
739-
});
732+
.Default(llvm::None);
733+
if (!Result)
734+
Ctx.diagnose(&N, diag::sdk_node_unrecognized_decl_attr_kind,
735+
GetScalarString(&N));
736+
else
737+
Info.DeclAttrs.push_back(*Result);
738+
}
740739
break;
741740
}
742741
case KeyKind::KK_accessors: {
@@ -2781,8 +2780,6 @@ static StringRef getAttrName(DeclAttrKind Kind) {
27812780
return DeclAttribute::isDeclModifier(DeclAttrKind::CLASS) ? #NAME \
27822781
: "@" #NAME;
27832782
#include "swift/AST/DeclAttr.def"
2784-
case DeclAttrKind::Count:
2785-
llvm_unreachable("unrecognized attribute kind.");
27862783
}
27872784
llvm_unreachable("covered switch");
27882785
}

lib/AST/ASTBridging.cpp

+10-8
Original file line numberDiff line numberDiff line change
@@ -352,33 +352,35 @@ BridgedDeclContext BridgedPatternBindingInitializer_asDeclContext(
352352
//===----------------------------------------------------------------------===//
353353

354354
BridgedDeclAttrKind BridgedDeclAttrKind_fromString(BridgedStringRef cStr) {
355-
auto kind = DeclAttribute::getAttrKindFromString(cStr.unbridged());
356-
switch (kind) {
355+
auto optKind = DeclAttribute::getAttrKindFromString(cStr.unbridged());
356+
if (!optKind)
357+
return BridgedDeclAttrKindNone;
358+
switch (*optKind) {
357359
#define DECL_ATTR(_, CLASS, ...) \
358360
case DeclAttrKind::CLASS: \
359361
return BridgedDeclAttrKind##CLASS;
360362
#include "swift/AST/DeclAttr.def"
361-
case DeclAttrKind::Count:
362-
return BridgedDeclAttrKindNone;
363363
}
364364
}
365365

366-
DeclAttrKind unbridged(BridgedDeclAttrKind kind) {
366+
llvm::Optional<DeclAttrKind> unbridged(BridgedDeclAttrKind kind) {
367367
switch (kind) {
368368
#define DECL_ATTR(_, CLASS, ...) \
369369
case BridgedDeclAttrKind##CLASS: \
370370
return DeclAttrKind::CLASS;
371371
#include "swift/AST/DeclAttr.def"
372372
case BridgedDeclAttrKindNone:
373-
return DeclAttrKind::Count;
373+
return llvm::None;
374374
}
375+
llvm_unreachable("unhandled enum value");
375376
}
376377

377378
BridgedDeclAttribute BridgedDeclAttribute_createSimple(
378379
BridgedASTContext cContext, BridgedDeclAttrKind cKind,
379380
BridgedSourceLoc cAtLoc, BridgedSourceLoc cAttrLoc) {
380-
auto kind = unbridged(cKind);
381-
return DeclAttribute::createSimple(cContext.unbridged(), kind,
381+
auto optKind = unbridged(cKind);
382+
assert(optKind && "creating attribute of invalid kind?");
383+
return DeclAttribute::createSimple(cContext.unbridged(), *optKind,
382384
cAtLoc.unbridged(), cAttrLoc.unbridged());
383385
}
384386

lib/AST/Attr.cpp

+13-21
Original file line numberDiff line numberDiff line change
@@ -251,16 +251,16 @@ void PackElementTypeAttr::printImpl(ASTPrinter &printer,
251251
/// of the 'unowned(unsafe)' attribute, the string passed in is 'unowned'.
252252
///
253253
/// Also note that this recognizes both attributes like '@inline' (with no @)
254-
/// and decl modifiers like 'final'. This returns DeclAttrKind::Count on
255-
/// failure.
254+
/// and decl modifiers like 'final'.
256255
///
257-
DeclAttrKind DeclAttribute::getAttrKindFromString(StringRef Str) {
258-
return llvm::StringSwitch<DeclAttrKind>(Str)
256+
llvm::Optional<DeclAttrKind>
257+
DeclAttribute::getAttrKindFromString(StringRef Str) {
258+
return llvm::StringSwitch<llvm::Optional<DeclAttrKind>>(Str)
259259
#define DECL_ATTR(X, CLASS, ...) .Case(#X, DeclAttrKind::CLASS)
260260
#define DECL_ATTR_ALIAS(X, CLASS) .Case(#X, DeclAttrKind::CLASS)
261261
#include "swift/AST/DeclAttr.def"
262262
.Case(SPI_AVAILABLE_ATTRNAME, DeclAttrKind::Available)
263-
.Default(DeclAttrKind::Count);
263+
.Default(llvm::None);
264264
}
265265

266266
DeclAttribute *DeclAttribute::createSimple(const ASTContext &context,
@@ -277,8 +277,6 @@ DeclAttribute *DeclAttribute::createSimple(const ASTContext &context,
277277
case DeclAttrKind::CLASS: \
278278
return new (context) CLASS##Attr(atLoc, attrLoc);
279279
#include "swift/AST/DeclAttr.def"
280-
case DeclAttrKind::Count:
281-
llvm_unreachable("bad decl attribute kind");
282280
}
283281
llvm_unreachable("bad decl attribute kind");
284282
}
@@ -1703,9 +1701,6 @@ bool DeclAttribute::printImpl(ASTPrinter &Printer, const PrintOptions &Options,
17031701
break;
17041702
}
17051703

1706-
case DeclAttrKind::Count:
1707-
llvm_unreachable("exceed declaration attribute kinds");
1708-
17091704
#define SIMPLE_DECL_ATTR(X, CLASS, ...) case DeclAttrKind::CLASS:
17101705
#include "swift/AST/DeclAttr.def"
17111706
llvm_unreachable("handled above");
@@ -1737,8 +1732,6 @@ void DeclAttribute::print(llvm::raw_ostream &OS, const Decl *D) const {
17371732

17381733
uint64_t DeclAttribute::getOptions(DeclAttrKind DK) {
17391734
switch (DK) {
1740-
case DeclAttrKind::Count:
1741-
llvm_unreachable("getOptions needs a valid attribute");
17421735
#define DECL_ATTR(_, CLASS, OPTIONS, ...) \
17431736
case DeclAttrKind::CLASS: \
17441737
return OPTIONS;
@@ -1749,8 +1742,6 @@ uint64_t DeclAttribute::getOptions(DeclAttrKind DK) {
17491742

17501743
StringRef DeclAttribute::getAttrName() const {
17511744
switch (getKind()) {
1752-
case DeclAttrKind::Count:
1753-
llvm_unreachable("getAttrName needs a valid attribute");
17541745
#define SIMPLE_DECL_ATTR(NAME, CLASS, ...) \
17551746
case DeclAttrKind::CLASS: \
17561747
return #NAME;
@@ -2890,19 +2881,20 @@ void swift::simple_display(llvm::raw_ostream &out, const DeclAttribute *attr) {
28902881

28912882
bool swift::hasAttribute(
28922883
const LangOptions &langOpts, llvm::StringRef attributeName) {
2893-
DeclAttrKind kind = DeclAttribute::getAttrKindFromString(attributeName);
2894-
if (kind == DeclAttrKind::Count)
2884+
llvm::Optional<DeclAttrKind> kind =
2885+
DeclAttribute::getAttrKindFromString(attributeName);
2886+
if (!kind)
28952887
return false;
28962888

2897-
if (DeclAttribute::isUserInaccessible(kind))
2889+
if (DeclAttribute::isUserInaccessible(*kind))
28982890
return false;
2899-
if (DeclAttribute::isDeclModifier(kind))
2891+
if (DeclAttribute::isDeclModifier(*kind))
29002892
return false;
2901-
if (DeclAttribute::shouldBeRejectedByParser(kind))
2893+
if (DeclAttribute::shouldBeRejectedByParser(*kind))
29022894
return false;
2903-
if (DeclAttribute::isSilOnly(kind))
2895+
if (DeclAttribute::isSilOnly(*kind))
29042896
return false;
2905-
if (DeclAttribute::isConcurrencyOnly(kind))
2897+
if (DeclAttribute::isConcurrencyOnly(*kind))
29062898
return false;
29072899

29082900
return true;

lib/IDE/SyntaxModel.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ SyntaxModelContext::SyntaxModelContext(SourceFile &SrcFile)
135135
// This token is following @, see if it's a known attribute name.
136136
// Type attribute, decl attribute, or '@unknown' for swift case statement.
137137
if (TypeAttribute::getAttrKindFromString(Tok.getText()).has_value() ||
138-
DeclAttribute::getAttrKindFromString(Tok.getText()) !=
139-
DeclAttrKind::Count ||
138+
DeclAttribute::getAttrKindFromString(Tok.getText()).has_value() ||
140139
Tok.getText() == "unknown") {
141140
// It's a known attribute, so treat it as a syntactic attribute node for
142141
// syntax coloring. If swift gets user attributes then all identifiers

0 commit comments

Comments
 (0)