Skip to content

Commit ffa5ba7

Browse files
committed
More macro metaprogramming for macro roles
1 parent b53026d commit ffa5ba7

File tree

5 files changed

+24
-65
lines changed

5 files changed

+24
-65
lines changed

lib/Parse/ParseDecl.cpp

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2464,15 +2464,9 @@ getMacroIntroducedDeclNameKind(Identifier name) {
24642464
llvm::Optional<MacroRole> getMacroRole(StringRef roleName) {
24652465
// Match the role string to the known set of roles.
24662466
return llvm::StringSwitch<llvm::Optional<MacroRole>>(roleName)
2467-
.Case("declaration", MacroRole::Declaration)
2468-
.Case("expression", MacroRole::Expression)
2469-
.Case("codeItem", MacroRole::CodeItem)
2470-
.Case("accessor", MacroRole::Accessor)
2471-
.Case("memberAttribute", MacroRole::MemberAttribute)
2472-
.Case("member", MacroRole::Member)
2473-
.Case("peer", MacroRole::Peer)
2474-
.Case("conformance", MacroRole::Conformance)
2475-
.Case("extension", MacroRole::Extension)
2467+
#define MACRO_ROLE(Name, Description) \
2468+
.Case(Description, MacroRole::Name)
2469+
#include "swift/Basic/MacroRoles.def"
24762470
.Default(llvm::None);
24772471
}
24782472

lib/Serialization/ModuleFormat.h

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -645,15 +645,8 @@ enum class GenericEnvironmentKind : uint8_t {
645645
// These IDs must \em not be renumbered or reordered without incrementing
646646
// the module version.
647647
enum class MacroRole : uint8_t {
648-
Expression,
649-
Declaration,
650-
Accessor,
651-
MemberAttribute,
652-
Member,
653-
Peer,
654-
Conformance,
655-
CodeItem,
656-
Extension,
648+
#define MACRO_ROLE(Name, Description) Name,
649+
#include "swift/Basic/MacroRoles.def"
657650
};
658651
using MacroRoleField = BCFixed<4>;
659652

tools/SourceKit/include/SourceKit/Core/LangSupport.h

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -222,17 +222,17 @@ struct RawCharSourceRange {
222222
unsigned Length;
223223
};
224224

225-
enum class MacroRole : uint32_t {
226-
// This should align with 'swift::MacroRole'.
227-
Expression = 0x01,
228-
Declaration = 0x02,
229-
Accessor = 0x04,
230-
MemberAttribute = 0x08,
231-
Member = 0x10,
232-
Peer = 0x20,
233-
Conformance = 0x40,
234-
CodeItem = 0x80,
235-
Extension = 0x100,
225+
enum class MacroRoleBits: uint8_t {
226+
#define MACRO_ROLE(Name, Description) Name,
227+
#include "swift/Basic/MacroRoles.def"
228+
};
229+
230+
/// The context in which a macro can be used, which determines the syntax it
231+
/// uses.
232+
enum class MacroRole: uint32_t {
233+
#define MACRO_ROLE(Name, Description) \
234+
Name = 1 << static_cast<uint8_t>(MacroRoleBits::Name),
235+
#include "swift/Basic/MacroRoles.def"
236236
};
237237
using MacroRoles = swift::OptionSet<MacroRole>;
238238

tools/SourceKit/lib/SwiftLang/SwiftSyntacticMacroExpansion.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -43,24 +43,10 @@ void SwiftLangSupport::expandMacroSyntactically(
4343
unsigned offset = req.offset;
4444

4545
swift::MacroRoles macroRoles;
46-
if (req.roles.contains(SourceKit::MacroRole::Expression))
47-
macroRoles |= swift::MacroRole::Expression;
48-
if (req.roles.contains(SourceKit::MacroRole::Declaration))
49-
macroRoles |= swift::MacroRole::Declaration;
50-
if (req.roles.contains(SourceKit::MacroRole::CodeItem))
51-
macroRoles |= swift::MacroRole::CodeItem;
52-
if (req.roles.contains(SourceKit::MacroRole::Accessor))
53-
macroRoles |= swift::MacroRole::Accessor;
54-
if (req.roles.contains(SourceKit::MacroRole::MemberAttribute))
55-
macroRoles |= swift::MacroRole::MemberAttribute;
56-
if (req.roles.contains(SourceKit::MacroRole::Member))
57-
macroRoles |= swift::MacroRole::Member;
58-
if (req.roles.contains(SourceKit::MacroRole::Peer))
59-
macroRoles |= swift::MacroRole::Peer;
60-
if (req.roles.contains(SourceKit::MacroRole::Conformance))
61-
macroRoles |= swift::MacroRole::Conformance;
62-
if (req.roles.contains(SourceKit::MacroRole::Extension))
63-
macroRoles |= swift::MacroRole::Extension;
46+
#define MACRO_ROLE(Name, Description) \
47+
if (req.roles.contains(SourceKit::MacroRole::Name)) \
48+
macroRoles |= swift::MacroRole::Name;
49+
#include "swift/Basic/MacroRoles.def"
6450

6551
MacroDefinition definition = [&] {
6652
if (auto *expanded =

tools/SourceKit/tools/sourcekitd/lib/Service/Requests.cpp

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1963,24 +1963,10 @@ static void handleRequestSyntacticMacroExpansion(
19631963
}
19641964
MacroRoles macroRoles;
19651965
for (auto uid : macroRoleUIDs) {
1966-
if (uid == KindMacroRoleExpression)
1967-
macroRoles |= MacroRole::Expression;
1968-
if (uid == KindMacroRoleDeclaration)
1969-
macroRoles |= MacroRole::Declaration;
1970-
if (uid == KindMacroRoleCodeItem)
1971-
macroRoles |= MacroRole::CodeItem;
1972-
if (uid == KindMacroRoleAccessor)
1973-
macroRoles |= MacroRole::Accessor;
1974-
if (uid == KindMacroRoleMemberAttribute)
1975-
macroRoles |= MacroRole::MemberAttribute;
1976-
if (uid == KindMacroRoleMember)
1977-
macroRoles |= MacroRole::Member;
1978-
if (uid == KindMacroRolePeer)
1979-
macroRoles |= MacroRole::Peer;
1980-
if (uid == KindMacroRoleConformance)
1981-
macroRoles |= MacroRole::Conformance;
1982-
if (uid == KindMacroRoleExtension)
1983-
macroRoles |= MacroRole::Extension;
1966+
#define MACRO_ROLE(Name, Description) \
1967+
if (uid == KindMacroRole##Name) \
1968+
macroRoles |= MacroRole::Name;
1969+
#include "swift/Basic/MacroRoles.def"
19841970
}
19851971

19861972
// definition.

0 commit comments

Comments
 (0)