Skip to content

[AST] Cleanup AttrKind #71342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 2, 2024
Merged

[AST] Cleanup AttrKind #71342

merged 5 commits into from
Feb 2, 2024

Conversation

rintaro
Copy link
Member

@rintaro rintaro commented Feb 2, 2024

The main motivation is to make DeclAttrKind/TypeAttrKind align with other "kind" enums e.g. DeclKind.

  • In TypeAttrKind, use the class names instead of the spelling as the enum case names.
  • Use scoped enums i.e. DeclAttrKind::Available instead of DAK_Available
  • Split Attr.def to DeclAttr.def and TypeAttr.def. Nothing was handling both attribute kind at the same time.
  • Remove DAK_Count(DeclAttrKind::Count). Instead, introduce NumDeclAttrKinds for the number of the enum cases, and use optional to represent invalid attribute kind. (aligning with TypeAttrKind)
  • Introduce LAST_TYPE_ATTR metaprogramming macro to get the number of the enum cases, instead of using dummy _counting_TAK_* globally visible symbols. This aligns with other "kind" enums.

Align with other kind enum e.g. DeclKind.
Introduce NumDeclAttrKinds for number of enum values. Use optional for
invalid attribute kind. This align with `TypeAttrKind`.
Instead of using dummy `_counting_KAK_*` global symbols, use
`LAST_DECL_ATTR` metaprogramming technique to determine the number of
enum values. This align with other "kind" enum e.g. `DeclKind`.
@rintaro
Copy link
Member Author

rintaro commented Feb 2, 2024

@swift-ci Please smoke test

#define TYPE_ATTR(X, C) ExcludeAttrs.insert(std::make_pair("TAK_" #X, TAK_##X));
#include "swift/AST/Attr.def"
#define TYPE_ATTR(X, C) \
ExcludeAttrs.insert(std::make_pair("TypeAttrKind::" #C, TypeAttrKind::C));
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This string is currently only used as keys of the temporary map. Changing the string value doesn't affect the functionality.

#include "swift/AST/Attr.def"
// NOTE: For historical reasons. TypeAttribute uses the spelling, but
// DeclAttribute uses the kind name.
#define TYPE_ATTR(X, C) out.enumCase(value, #X, TypeAttrKind::C);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little unfortunate, but we can't change swift-api-digester results.

Copy link
Contributor

@bnbarham bnbarham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice cleanup 😍

#define DECL_ATTR(_, NAME, ...) DAK_##NAME,
#include "swift/AST/Attr.def"
DAK_Count
enum class DeclAttrKind : unsigned {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know if there's any reason DeclAttrKind has to be unsigned? If it's just for DAK_Count, NumDeclAttrKinds covers that now. Doesn't really matter either way though so 🤷 (just pointing it out since it seemed like part of the reason for this PR was to mostly "unify" decl/type attr kinds)

GetScalarString(&N));
return Result;
});
for (auto &N : *Seq) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice 👍 (though personally I'd type N since it's not super obvious what it is)

@rintaro rintaro merged commit b12370a into swiftlang:main Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants