Skip to content

Commit e540b7c

Browse files
committed
[Macros] Freestanding macros expand to CodeBlockItemList; parse top-level macro expansion decls
- Change the return type of `FreestandingDeclarationMacro.expanion(of:in:)` to ``CodeBlockItemListSyntax` to allow expressions, statements, and declarations. - Parse freestanding macro expansions as declarations rather than expressions when it's not part of a statement or expression (i.e. "top-level").
1 parent 017a1fd commit e540b7c

File tree

4 files changed

+22
-19
lines changed

4 files changed

+22
-19
lines changed

Sources/_SwiftSyntaxMacros/CodeItemMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ public protocol CodeItemMacro: FreestandingMacro {
1616
static func expansion(
1717
of node: MacroExpansionDeclSyntax,
1818
in context: inout MacroExpansionContext
19-
) throws -> [CodeBlockItemSyntax]
19+
) throws -> CodeBlockItemListSyntax
2020
}

Sources/_SwiftSyntaxMacros/DeclarationMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public protocol DeclarationMacro: FreestandingMacro {
1616
static func expansion(
1717
of node: MacroExpansionDeclSyntax,
1818
in context: inout MacroExpansionContext
19-
) throws -> [DeclSyntax]
19+
) throws -> CodeBlockItemListSyntax
2020
}
2121

2222
@available(*, deprecated, renamed: "DeclarationMacro")

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -134,18 +134,12 @@ class MacroApplication: SyntaxRewriter {
134134
do {
135135
if let macro = macro as? DeclarationMacro.Type {
136136
let expandedItemList = try macro.expansion(
137-
of: declExpansion,
138-
in: &context
139-
)
140-
newItems.append(
141-
contentsOf: expandedItemList.map {
142-
CodeBlockItemSyntax(item: .decl($0))
143-
}
137+
of: declExpansion, in: &context
144138
)
139+
newItems.append(contentsOf: expandedItemList)
145140
} else if let macro = macro as? ExpressionMacro.Type {
146141
let expandedExpr = try macro.expansion(
147-
of: declExpansion.asMacroExpansionExpr(),
148-
in: &context
142+
of: declExpansion.asMacroExpansionExpr(), in: &context
149143
)
150144
newItems.append(CodeBlockItemSyntax(item: .init(expandedExpr)))
151145
}
@@ -194,11 +188,15 @@ class MacroApplication: SyntaxRewriter {
194188
in: &context
195189
)
196190

197-
newItems.append(
198-
contentsOf: expandedList.map { decl in
199-
return MemberDeclListItemSyntax(decl: decl)
200-
}
201-
)
191+
newItems.append(contentsOf: expandedList.compactMap { item -> MemberDeclListItemSyntax? in
192+
guard let decl = item.item.as(DeclSyntax.self) else { return nil }
193+
return MemberDeclListItemSyntax(
194+
leadingTrivia: item.leadingTrivia,
195+
item.unexpectedBeforeItem,
196+
decl: decl,
197+
semicolon: item.semicolon,
198+
trailingTrivia: item.trailingTrivia)
199+
})
202200
} catch {
203201
// Record the error
204202
context.diagnose(

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public struct ErrorMacro: DeclarationMacro {
158158
public static func expansion(
159159
of node: MacroExpansionDeclSyntax,
160160
in context: inout MacroExpansionContext
161-
) throws -> [DeclSyntax] {
161+
) throws -> CodeBlockItemListSyntax {
162162
guard let firstElement = node.argumentList.first,
163163
let stringLiteral = firstElement.expression
164164
.as(StringLiteralExprSyntax.self),
@@ -187,7 +187,7 @@ struct DefineBitwidthNumberedStructsMacro: DeclarationMacro {
187187
static func expansion(
188188
of node: MacroExpansionDeclSyntax,
189189
in context: inout MacroExpansionContext
190-
) throws -> [DeclSyntax] {
190+
) throws -> CodeBlockItemListSyntax {
191191
guard let firstElement = node.argumentList.first,
192192
let stringLiteral = firstElement.expression
193193
.as(StringLiteralExprSyntax.self),
@@ -199,12 +199,17 @@ struct DefineBitwidthNumberedStructsMacro: DeclarationMacro {
199199
)
200200
}
201201

202-
return [8, 16, 32, 64].map { bitwidth in
202+
let decls: [DeclSyntax] = [8, 16, 32, 64].map { bitwidth in
203203
"""
204204
205205
struct \(raw: prefix)\(raw: String(bitwidth)) { }
206206
"""
207207
}
208+
// TODO: Interpolate to `CodeBlockItemList` directly when it supports
209+
// string interpolation.
210+
return CodeBlockItemListSyntax(decls.map {
211+
CodeBlockItemSyntax(item: .init($0))
212+
})
208213
}
209214
}
210215

0 commit comments

Comments
 (0)