Skip to content

Commit 135425c

Browse files
authored
Merge pull request #16014 from davezarzycki/nfc_metaprogram_DeclAttrOptions
[AST] NFC: Metaprogram DeclAttrOptions
2 parents 414d5ac + 0c4c02d commit 135425c

File tree

4 files changed

+52
-61
lines changed

4 files changed

+52
-61
lines changed

include/swift/AST/Attr.h

Lines changed: 39 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ enum class DeclKind : uint8_t;
182182
/// Represents one declaration attribute.
183183
class DeclAttribute : public AttributeBase {
184184
friend class DeclAttributes;
185+
185186
protected:
186187
union {
187188
uint64_t OpaqueBits;
@@ -261,84 +262,65 @@ class DeclAttribute : public AttributeBase {
261262
Bits.DeclAttribute.Invalid = false;
262263
}
263264

265+
private:
266+
// NOTE: We cannot use DeclKind due to layering. Even if we could, there is no
267+
// guarantee that the first DeclKind starts at zero. This is only used to
268+
// build "OnXYZ" flags.
269+
enum class DeclKindIndex : unsigned {
270+
#define DECL(Name, _) Name,
271+
#define LAST_DECL(Name) Last_Decl = Name
272+
#include "swift/AST/DeclNodes.def"
273+
};
274+
264275
public:
265-
enum DeclAttrOptions {
276+
enum DeclAttrOptions : uint64_t {
277+
// There is one entry for each DeclKind, and some higher level buckets
278+
// below. These are used in Attr.def to control which kinds of declarations
279+
// an attribute can be attached to.
280+
#define DECL(Name, _) On##Name = 1ull << unsigned(DeclKindIndex::Name),
281+
#include "swift/AST/DeclNodes.def"
282+
283+
// More coarse-grained aggregations for use in Attr.def.
284+
OnOperator = 0
285+
#define DECL(Name, _)
286+
#define OPERATOR_DECL(Name, _) |On##Name
287+
#include "swift/AST/DeclNodes.def"
288+
,
289+
290+
OnAnyDecl = 0
291+
#define DECL(Name, _) |On##Name
292+
#include "swift/AST/DeclNodes.def"
293+
,
294+
266295
/// True if multiple instances of this attribute are allowed on a single
267296
/// declaration.
268-
AllowMultipleAttributes = 1 << 0,
297+
AllowMultipleAttributes = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 1),
269298

270299
/// True if this is a decl modifier - i.e., that it should not be spelled
271300
/// with an @.
272-
DeclModifier = 1 << 1,
301+
DeclModifier = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 2),
273302

274303
/// True if this is a long attribute that should be printed on its own line.
275304
///
276305
/// Currently has no effect on DeclModifier attributes.
277-
LongAttribute = 1 << 2,
306+
LongAttribute = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 3),
278307

279308
/// True if this shouldn't be serialized.
280-
NotSerialized = 1 << 3,
309+
NotSerialized = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 4),
281310

282311
/// True if this attribute is only valid when parsing a .sil file.
283-
SILOnly = 1 << 4,
312+
SILOnly = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 5),
284313

285314
/// The attribute should be reported by parser as unknown.
286-
RejectByParser = 1 << 5,
315+
RejectByParser = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 6),
287316

288317
/// Whether client code cannot use the attribute.
289-
UserInaccessible = 1 << 6,
290-
291-
// There is one entry for each DeclKind here, and some higher level buckets
292-
// down below. These are used in Attr.def to control which kinds of
293-
// declarations an attribute can be attached to.
294-
OnPrecedenceGroup = 1 << 7,
295-
OnImport = 1 << 8,
296-
OnExtension = 1 << 9,
297-
OnPatternBinding = 1 << 10,
298-
OnEnumCase = 1 << 11,
299-
OnTopLevelCode = 1 << 12,
300-
OnIfConfig = 1 << 13,
301-
OnInfixOperator = 1 << 14, // "infix operator"
302-
OnPrefixOperator = 1 << 15, // "prefix operator"
303-
OnPostfixOperator = 1 << 16, // "postfix operator"
304-
305-
OnEnum = 1 << 17,
306-
OnStruct = 1 << 18,
307-
OnClass = 1 << 19,
308-
OnProtocol = 1 << 20,
309-
OnTypeAlias = 1 << 21,
310-
OnVar = 1 << 22,
311-
OnSubscript = 1 << 23,
312-
313-
OnConstructor = 1 << 24,
314-
OnDestructor = 1 << 25,
315-
OnFunc = 1 << 26,
316-
OnAccessor = OnFunc,
317-
OnEnumElement = 1 << 27,
318-
319-
OnGenericTypeParam = 1 << 28,
320-
OnAssociatedType = 1 << 29,
321-
OnParam = 1 << 30,
322-
OnModule = 1 << 31,
323-
324-
// Cannot have any attributes.
325-
OnMissingMember = 0,
326-
OnPoundDiagnostic = 0,
327-
328-
// More coarse-grained aggregations for use in Attr.def.
329-
OnOperator = OnInfixOperator|OnPrefixOperator|OnPostfixOperator,
330-
331-
OnAnyDecl = OnImport|OnExtension|OnPatternBinding|OnEnumCase|
332-
OnTopLevelCode|OnIfConfig|OnInfixOperator|OnPrefixOperator|
333-
OnPostfixOperator|OnEnum|OnStruct|OnClass|OnProtocol|
334-
OnTypeAlias|OnVar|OnSubscript|OnConstructor|OnDestructor|
335-
OnFunc|OnEnumElement|OnGenericTypeParam|OnAssociatedType|
336-
OnParam|OnPrecedenceGroup
318+
UserInaccessible = 1ull << (unsigned(DeclKindIndex::Last_Decl) + 7),
337319
};
338320

339-
static unsigned getOptions(DeclAttrKind DK);
321+
static uint64_t getOptions(DeclAttrKind DK);
340322

341-
unsigned getOptions() const {
323+
uint64_t getOptions() const {
342324
return getOptions(getKind());
343325
}
344326

include/swift/AST/DeclNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,5 +197,6 @@ LAST_DECL(PostfixOperator)
197197
#undef VALUE_DECL
198198
#undef DECL_RANGE
199199
#undef ABSTRACT_DECL
200+
#undef OPERATOR_DECL
200201
#undef DECL
201202
#undef LAST_DECL

lib/AST/Attr.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ bool DeclAttribute::canAttributeAppearOnDecl(DeclAttrKind DK, const Decl *D) {
7878
}
7979

8080
bool DeclAttribute::canAttributeAppearOnDeclKind(DeclAttrKind DAK, DeclKind DK) {
81-
unsigned Options = getOptions(DAK);
81+
auto Options = getOptions(DAK);
8282
switch (DK) {
8383
#define DECL(Id, Parent) case DeclKind::Id: return (Options & On##Id) != 0;
8484
#include "swift/AST/DeclNodes.def"
@@ -538,12 +538,17 @@ void DeclAttribute::print(llvm::raw_ostream &OS, const Decl *D) const {
538538
print(P, PrintOptions(), D);
539539
}
540540

541-
unsigned DeclAttribute::getOptions(DeclAttrKind DK) {
541+
uint64_t DeclAttribute::getOptions(DeclAttrKind DK) {
542+
// FIXME: Update Attr.def to use OnAccessor.
542543
switch (DK) {
543544
case DAK_Count:
544545
llvm_unreachable("getOptions needs a valid attribute");
545546
#define DECL_ATTR(_, CLASS, OPTIONS, ...)\
546-
case DAK_##CLASS: return OPTIONS;
547+
case DAK_##CLASS: { \
548+
uint64_t options = OPTIONS; \
549+
if (options & OnFunc) options |= OnAccessor; \
550+
return options; \
551+
}
547552
#include "swift/AST/Attr.def"
548553
}
549554
llvm_unreachable("bad DeclAttrKind");

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -738,7 +738,7 @@ void TypeChecker::checkDeclAttributesEarly(Decl *D) {
738738
// Otherwise, this attribute cannot be applied to this declaration. If the
739739
// attribute is only valid on one kind of declaration (which is pretty
740740
// common) give a specific helpful error.
741-
unsigned PossibleDeclKinds = attr->getOptions() & DeclAttribute::OnAnyDecl;
741+
auto PossibleDeclKinds = attr->getOptions() & DeclAttribute::OnAnyDecl;
742742
StringRef OnlyKind;
743743
switch (PossibleDeclKinds) {
744744
case DeclAttribute::OnImport:
@@ -747,7 +747,10 @@ void TypeChecker::checkDeclAttributesEarly(Decl *D) {
747747
case DeclAttribute::OnVar:
748748
OnlyKind = "var";
749749
break;
750+
// FIXME: Update Attr.def to use OnAccessor.
750751
case DeclAttribute::OnFunc:
752+
case DeclAttribute::OnAccessor:
753+
case DeclAttribute::OnFunc | DeclAttribute::OnAccessor:
751754
OnlyKind = "func";
752755
break;
753756
case DeclAttribute::OnClass:

0 commit comments

Comments
 (0)