Skip to content

Commit 6f4cc1d

Browse files
committed
Add async overloads of expansion functions to macro protocols
1 parent 276c8d4 commit 6f4cc1d

File tree

10 files changed

+259
-0
lines changed

10 files changed

+259
-0
lines changed

Sources/SwiftSyntaxMacros/MacroProtocols/AccessorMacro.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,23 @@ public protocol AccessorMacro: AttachedMacro {
2424
providingAccessorsOf declaration: some DeclSyntaxProtocol,
2525
in context: some MacroExpansionContext
2626
) throws -> [AccessorDeclSyntax]
27+
28+
/// Expand a macro that's expressed as a custom attribute attached to
29+
/// the given declaration. The result is a set of accessors for the
30+
/// declaration.
31+
static func expansion(
32+
of node: AttributeSyntax,
33+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
34+
in context: some MacroExpansionContext
35+
) async throws -> [AccessorDeclSyntax]
36+
}
37+
38+
extension AccessorMacro {
39+
public static func expansion(
40+
of node: AttributeSyntax,
41+
providingAccessorsOf declaration: some DeclSyntaxProtocol,
42+
in context: some MacroExpansionContext
43+
) async throws -> [AccessorDeclSyntax] {
44+
return try { try self.expansion(of: node, providingAccessorsOf: declaration, in: context) }()
45+
}
2746
}

Sources/SwiftSyntaxMacros/MacroProtocols/BodyMacro.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,25 @@ public protocol BodyMacro: AttachedMacro {
2727
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
2828
in context: some MacroExpansionContext
2929
) throws -> [CodeBlockItemSyntax]
30+
31+
/// Expand a macro described by the given custom attribute and
32+
/// attached to the given declaration and evaluated within a
33+
/// particular expansion context.
34+
///
35+
/// The macro expansion can introduce a body for the given function.
36+
static func expansion(
37+
of node: AttributeSyntax,
38+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
39+
in context: some MacroExpansionContext
40+
) async throws -> [CodeBlockItemSyntax]
41+
}
42+
43+
extension BodyMacro {
44+
public static func expansion(
45+
of node: AttributeSyntax,
46+
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
47+
in context: some MacroExpansionContext
48+
) async throws -> [CodeBlockItemSyntax] {
49+
return try { try self.expansion(of: node, providingBodyFor: declaration, in: context) }()
50+
}
3051
}

Sources/SwiftSyntaxMacros/MacroProtocols/CodeItemMacro.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,20 @@ public protocol CodeItemMacro: FreestandingMacro {
2222
of node: some FreestandingMacroExpansionSyntax,
2323
in context: some MacroExpansionContext
2424
) throws -> [CodeBlockItemSyntax]
25+
26+
/// Expand a macro described by the given freestanding macro expansion
27+
/// declaration within the given context to produce a set of declarations.
28+
static func expansion(
29+
of node: some FreestandingMacroExpansionSyntax,
30+
in context: some MacroExpansionContext
31+
) async throws -> [CodeBlockItemSyntax]
32+
}
33+
34+
extension CodeItemMacro {
35+
public static func expansion(
36+
of node: some FreestandingMacroExpansionSyntax,
37+
in context: some MacroExpansionContext
38+
) async throws -> [CodeBlockItemSyntax] {
39+
return try { try expansion(of: node, in: context) }()
40+
}
2541
}

Sources/SwiftSyntaxMacros/MacroProtocols/DeclarationMacro.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ public protocol DeclarationMacro: FreestandingMacro {
2323
in context: some MacroExpansionContext
2424
) throws -> [DeclSyntax]
2525

26+
/// Expand a macro described by the given freestanding macro expansion
27+
/// declaration within the given context to produce a set of declarations.
28+
static func expansion(
29+
of node: some FreestandingMacroExpansionSyntax,
30+
in context: some MacroExpansionContext
31+
) async throws -> [DeclSyntax]
32+
2633
/// Whether to copy attributes on the expansion syntax to expanded declarations,
2734
/// 'true' by default.
2835
static var propagateFreestandingMacroAttributes: Bool { get }
@@ -32,6 +39,13 @@ public protocol DeclarationMacro: FreestandingMacro {
3239
}
3340

3441
extension DeclarationMacro {
42+
public static func expansion(
43+
of node: some FreestandingMacroExpansionSyntax,
44+
in context: some MacroExpansionContext
45+
) async throws -> [DeclSyntax] {
46+
return try { try self.expansion(of: node, in: context) }()
47+
}
48+
3549
public static var propagateFreestandingMacroAttributes: Bool { true }
3650
public static var propagateFreestandingMacroModifiers: Bool { true }
3751
}

Sources/SwiftSyntaxMacros/MacroProtocols/ExpressionMacro.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,20 @@ public protocol ExpressionMacro: FreestandingMacro {
2424
of node: some FreestandingMacroExpansionSyntax,
2525
in context: some MacroExpansionContext
2626
) throws -> ExprSyntax
27+
28+
/// Expand a macro described by the given freestanding macro expansion
29+
/// within the given context to produce a replacement expression.
30+
static func expansion(
31+
of node: some FreestandingMacroExpansionSyntax,
32+
in context: some MacroExpansionContext
33+
) async throws -> ExprSyntax
34+
}
35+
36+
extension ExpressionMacro {
37+
public static func expansion(
38+
of node: some FreestandingMacroExpansionSyntax,
39+
in context: some MacroExpansionContext
40+
) async throws -> ExprSyntax {
41+
return try { try self.expansion(of: node, in: context) }()
42+
}
2743
}

Sources/SwiftSyntaxMacros/MacroProtocols/ExtensionMacro.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,46 @@ public protocol ExtensionMacro: AttachedMacro {
4040
conformingTo protocols: [TypeSyntax],
4141
in context: some MacroExpansionContext
4242
) throws -> [ExtensionDeclSyntax]
43+
44+
/// Expand an attached extension macro to produce a set of extensions.
45+
///
46+
/// - Parameters:
47+
/// - node: The custom attribute describing the attached macro.
48+
/// - declaration: The declaration the macro attribute is attached to.
49+
/// - type: The type to provide extensions of.
50+
/// - protocols: The list of protocols to add conformances to. These will
51+
/// always be protocols that `type` does not already state a conformance
52+
/// to.
53+
/// - context: The context in which to perform the macro expansion.
54+
///
55+
/// - Returns: the set of extension declarations introduced by the macro,
56+
/// which are always inserted at top-level scope. Each extension must extend
57+
/// the `type` parameter.
58+
static func expansion(
59+
of node: AttributeSyntax,
60+
attachedTo declaration: some DeclGroupSyntax,
61+
providingExtensionsOf type: some TypeSyntaxProtocol,
62+
conformingTo protocols: [TypeSyntax],
63+
in context: some MacroExpansionContext
64+
) async throws -> [ExtensionDeclSyntax]
65+
}
66+
67+
extension ExtensionMacro {
68+
public static func expansion(
69+
of node: AttributeSyntax,
70+
attachedTo declaration: some DeclGroupSyntax,
71+
providingExtensionsOf type: some TypeSyntaxProtocol,
72+
conformingTo protocols: [TypeSyntax],
73+
in context: some MacroExpansionContext
74+
) async throws -> [ExtensionDeclSyntax] {
75+
return try {
76+
try self.expansion(
77+
of: node,
78+
attachedTo: declaration,
79+
providingExtensionsOf: type,
80+
conformingTo: protocols,
81+
in: context
82+
)
83+
}()
84+
}
4385
}

Sources/SwiftSyntaxMacros/MacroProtocols/MemberAttributeMacro.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,32 @@ public protocol MemberAttributeMacro: AttachedMacro {
3535
providingAttributesFor member: some DeclSyntaxProtocol,
3636
in context: some MacroExpansionContext
3737
) throws -> [AttributeSyntax]
38+
39+
/// Expand an attached declaration macro to produce an attribute list for
40+
/// a given member.
41+
///
42+
/// - Parameters:
43+
/// - node: The custom attribute describing the attached macro.
44+
/// - declaration: The declaration the macro attribute is attached to.
45+
/// - member: The member declaration to attach the resulting attributes to.
46+
/// - context: The context in which to perform the macro expansion.
47+
///
48+
/// - Returns: the set of attributes to apply to the given member.
49+
static func expansion(
50+
of node: AttributeSyntax,
51+
attachedTo declaration: some DeclGroupSyntax,
52+
providingAttributesFor member: some DeclSyntaxProtocol,
53+
in context: some MacroExpansionContext
54+
) async throws -> [AttributeSyntax]
55+
}
56+
57+
extension MemberAttributeMacro {
58+
public static func expansion(
59+
of node: AttributeSyntax,
60+
attachedTo declaration: some DeclGroupSyntax,
61+
providingAttributesFor member: some DeclSyntaxProtocol,
62+
in context: some MacroExpansionContext
63+
) async throws -> [AttributeSyntax] {
64+
return try { try self.expansion(of: node, attachedTo: declaration, providingAttributesFor: member, in: context) }()
65+
}
3866
}

Sources/SwiftSyntaxMacros/MacroProtocols/MemberMacro.swift

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,47 @@ public protocol MemberMacro: AttachedMacro {
5858
conformingTo protocols: [TypeSyntax],
5959
in context: some MacroExpansionContext
6060
) throws -> [DeclSyntax]
61+
62+
/// Expand an attached declaration macro to produce a set of members.
63+
///
64+
/// - Parameters:
65+
/// - node: The custom attribute describing the attached macro.
66+
/// - declaration: The declaration the macro attribute is attached to.
67+
/// - context: The context in which to perform the macro expansion.
68+
///
69+
/// - Returns: the set of member declarations introduced by this macro, which
70+
/// are nested inside the `attachedTo` declaration.
71+
///
72+
/// - Warning: This is the legacy `expansion` function of `MemberMacro` that is provided for backwards-compatiblity.
73+
/// Use ``expansion(of:providingMembersOf:conformingTo:in:)-1sxoe`` instead.
74+
static func expansion(
75+
of node: AttributeSyntax,
76+
providingMembersOf declaration: some DeclGroupSyntax,
77+
in context: some MacroExpansionContext
78+
) async throws -> [DeclSyntax]
79+
80+
/// Expand an attached declaration macro to produce a set of members.
81+
///
82+
/// - Parameters:
83+
/// - node: The custom attribute describing the attached macro.
84+
/// - declaration: The declaration the macro attribute is attached to.
85+
/// - conformingTo: The set of protocols that were declared
86+
/// in the set of conformances for the macro and to which the declaration
87+
/// does not explicitly conform. The member macro itself cannot declare
88+
/// conformances to these protocols (only an extension macro can do that),
89+
/// but can provide supporting declarations, such as a required
90+
/// initializer or stored property, that cannot be written in an
91+
/// extension.
92+
/// - context: The context in which to perform the macro expansion.
93+
///
94+
/// - Returns: the set of member declarations introduced by this macro, which
95+
/// are nested inside the `attachedTo` declaration.
96+
static func expansion(
97+
of node: AttributeSyntax,
98+
providingMembersOf declaration: some DeclGroupSyntax,
99+
conformingTo protocols: [TypeSyntax],
100+
in context: some MacroExpansionContext
101+
) async throws -> [DeclSyntax]
61102
}
62103

63104
private struct UnimplementedExpansionMethodError: Error, CustomStringConvertible {
@@ -80,6 +121,14 @@ extension MemberMacro {
80121
throw UnimplementedExpansionMethodError()
81122
}
82123

124+
public static func expansion(
125+
of node: AttributeSyntax,
126+
providingMembersOf declaration: some DeclGroupSyntax,
127+
in context: some MacroExpansionContext
128+
) async throws -> [DeclSyntax] {
129+
return try { try self.expansion(of: node, providingMembersOf: declaration, in: context) }()
130+
}
131+
83132
/// Default implementation that ignores the unhandled conformances.
84133
@available(
85134
*,
@@ -94,4 +143,13 @@ extension MemberMacro {
94143
) throws -> [DeclSyntax] {
95144
return try expansion(of: node, providingMembersOf: declaration, in: context)
96145
}
146+
147+
public static func expansion(
148+
of node: AttributeSyntax,
149+
providingMembersOf declaration: some DeclGroupSyntax,
150+
conformingTo protocols: [TypeSyntax],
151+
in context: some MacroExpansionContext
152+
) async throws -> [DeclSyntax] {
153+
return try { try self.expansion(of: node, providingMembersOf: declaration, conformingTo: protocols, in: context) }()
154+
}
97155
}

Sources/SwiftSyntaxMacros/MacroProtocols/PeerMacro.swift

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,26 @@ public protocol PeerMacro: AttachedMacro {
2626
providingPeersOf declaration: some DeclSyntaxProtocol,
2727
in context: some MacroExpansionContext
2828
) throws -> [DeclSyntax]
29+
30+
/// Expand a macro described by the given custom attribute and
31+
/// attached to the given declaration and evaluated within a
32+
/// particular expansion context.
33+
///
34+
/// The macro expansion can introduce "peer" declarations that sit alongside
35+
/// the given declaration.
36+
static func expansion(
37+
of node: AttributeSyntax,
38+
providingPeersOf declaration: some DeclSyntaxProtocol,
39+
in context: some MacroExpansionContext
40+
) async throws -> [DeclSyntax]
41+
}
42+
43+
extension PeerMacro {
44+
public static func expansion(
45+
of node: AttributeSyntax,
46+
providingPeersOf declaration: some DeclSyntaxProtocol,
47+
in context: some MacroExpansionContext
48+
) async throws -> [DeclSyntax] {
49+
return try { try self.expansion(of: node, providingPeersOf: declaration, in: context) }()
50+
}
2951
}

Sources/SwiftSyntaxMacros/MacroProtocols/PreambleMacro.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,27 @@ public protocol PreambleMacro: AttachedMacro {
3030
providingPreambleFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
3131
in context: some MacroExpansionContext
3232
) throws -> [CodeBlockItemSyntax]
33+
34+
/// Expand a macro described by the given custom attribute and
35+
/// attached to the given declaration and evaluated within a
36+
/// particular expansion context.
37+
///
38+
/// The macro expansion can introduce code items that form a preamble to
39+
/// the body of the given function. The code items produced by this macro
40+
/// expansion will be inserted at the beginning of the function body.
41+
static func expansion(
42+
of node: AttributeSyntax,
43+
providingPreambleFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
44+
in context: some MacroExpansionContext
45+
) async throws -> [CodeBlockItemSyntax]
46+
}
47+
48+
extension PreambleMacro {
49+
public static func expansion(
50+
of node: AttributeSyntax,
51+
providingPreambleFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
52+
in context: some MacroExpansionContext
53+
) async throws -> [CodeBlockItemSyntax] {
54+
return try { try expansion(of: node, providingPreambleFor: declaration, in: context) }()
55+
}
3356
}

0 commit comments

Comments
 (0)