Skip to content

Commit 875bcdb

Browse files
committed
[Macros] Rename macros to match recent pitches
1 parent c2741e4 commit 875bcdb

12 files changed

+75
-27
lines changed

Sources/_SwiftSyntaxMacros/AccessorDeclarationMacro.swift renamed to Sources/_SwiftSyntaxMacros/AccessorMacro.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import SwiftSyntax
1111

1212
/// Describes a macro that adds accessors to a given declaration.
13-
public protocol AccessorDeclarationMacro: DeclarationMacro {
13+
public protocol AccessorMacro: AttachedMacro {
1414
/// Expand a macro that's expressed as a custom attribute attached to
1515
/// the given declaration. The result is a set of accessors for the
1616
/// declaration.
@@ -20,3 +20,6 @@ public protocol AccessorDeclarationMacro: DeclarationMacro {
2020
in context: inout MacroExpansionContext
2121
) throws -> [AccessorDeclSyntax]
2222
}
23+
24+
@available(*, deprecated, renamed: "AccessorMacro")
25+
public typealias AccessorDeclarationMacro = AccessorMacro
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
3+
// Licensed under Apache License v2.0 with Runtime Library Exception
4+
//
5+
// See https://swift.org/LICENSE.txt for license information
6+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
/// Describes a macro that is attached, meaning that it is used with
11+
/// custom attribute syntax and attached to another entity.
12+
public protocol AttachedMacro: Macro {
13+
}

Sources/_SwiftSyntaxMacros/CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,18 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_swift_host_library(_SwiftSyntaxMacros
10-
AccessorDeclarationMacro.swift
10+
AccessorMacro.swift
11+
AttachedMacro.swift
12+
CodeItemMacro.swift
1113
DeclarationMacro.swift
1214
ExpressionMacro.swift
13-
FreestandingDeclarationMacro.swift
15+
FreestandingMacro.swift
1416
Macro.swift
1517
MacroExpansionContext.swift
1618
MacroSystem.swift
1719
MemberAttributeMacro.swift
18-
MemberDeclarationMacro.swift
19-
PeerDeclarationMacro.swift
20+
MemberMacro.swift
21+
PeerMacro.swift
2022
Syntax+MacroEvaluation.swift
2123
)
2224

Sources/_SwiftSyntaxMacros/FreestandingDeclarationMacro.swift renamed to Sources/_SwiftSyntaxMacros/CodeItemMacro.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
import SwiftSyntax
1111

12-
/// Describes a macro that forms declarations.
13-
public protocol FreestandingDeclarationMacro: DeclarationMacro {
12+
/// Describes a macro that forms code items in a function or closure body.
13+
public protocol CodeItemMacro: FreestandingMacro {
1414
/// Expand a macro described by the given freestanding macro expansion
1515
/// declaration within the given context to produce a set of declarations.
1616
static func expansion(
1717
of node: MacroExpansionDeclSyntax,
1818
in context: inout MacroExpansionContext
19-
) throws -> [DeclSyntax]
19+
) throws -> [CodeBlockItemSyntax]
2020
}

Sources/_SwiftSyntaxMacros/DeclarationMacro.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10+
import SwiftSyntax
11+
1012
/// Describes a macro that forms declarations.
11-
public protocol DeclarationMacro: Macro {
13+
public protocol DeclarationMacro: FreestandingMacro {
14+
/// Expand a macro described by the given freestanding macro expansion
15+
/// declaration within the given context to produce a set of declarations.
16+
static func expansion(
17+
of node: MacroExpansionDeclSyntax,
18+
in context: inout MacroExpansionContext
19+
) throws -> [DeclSyntax]
1220
}
21+
22+
@available(*, deprecated, renamed: "DeclarationMacro")
23+
public typealias FreestandingDeclarationMacro = DeclarationMacro

Sources/_SwiftSyntaxMacros/ExpressionMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SwiftSyntax
1414

1515
/// Describes a macro that is explicitly expanded as an expression.
16-
public protocol ExpressionMacro: Macro {
16+
public protocol ExpressionMacro: FreestandingMacro {
1717
/// Expand a macro described by the given macro expansion expression
1818
/// within the given context to produce a replacement expression.
1919
static func expansion(
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
3+
// Licensed under Apache License v2.0 with Runtime Library Exception
4+
//
5+
// See https://swift.org/LICENSE.txt for license information
6+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
7+
//
8+
//===----------------------------------------------------------------------===//
9+
10+
/// Describes a macro that is freestanding, meaning that it is used with the
11+
/// `#` syntax.
12+
public protocol FreestandingMacro: Macro {
13+
}

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class MacroApplication: SyntaxRewriter {
109109
return true
110110
}
111111

112-
return !(macro is PeerDeclarationMacro.Type || macro is MemberDeclarationMacro.Type || macro is AccessorDeclarationMacro.Type || macro is MemberAttributeMacro.Type)
112+
return !(macro is PeerMacro.Type || macro is MemberMacro.Type || macro is AccessorMacro.Type || macro is MemberAttributeMacro.Type)
113113
}
114114

115115
if newAttributes.isEmpty {
@@ -130,7 +130,7 @@ class MacroApplication: SyntaxRewriter {
130130
if case let .expr(exprItem) = item.item,
131131
let exprExpansion = exprItem.as(MacroExpansionExprSyntax.self),
132132
let macro = macroSystem.macros[exprExpansion.macro.text],
133-
let freestandingMacro = macro as? FreestandingDeclarationMacro.Type
133+
let freestandingMacro = macro as? DeclarationMacro.Type
134134
{
135135
do {
136136
let expandedDecls = try freestandingMacro.expansion(
@@ -180,7 +180,7 @@ class MacroApplication: SyntaxRewriter {
180180
// Expand declaration macros, which produce zero or more declarations.
181181
if let declExpansion = item.decl.as(MacroExpansionDeclSyntax.self),
182182
let macro = macroSystem.macros[declExpansion.macro.text],
183-
let freestandingMacro = macro as? FreestandingDeclarationMacro.Type
183+
let freestandingMacro = macro as? DeclarationMacro.Type
184184
{
185185
do {
186186
let expandedDecls = try freestandingMacro.expansion(
@@ -293,7 +293,7 @@ class MacroApplication: SyntaxRewriter {
293293

294294
var accessors: [AccessorDeclSyntax] = []
295295

296-
let accessorMacroAttributes = getMacroAttributes(attachedTo: DeclSyntax(node), ofType: AccessorDeclarationMacro.Type.self)
296+
let accessorMacroAttributes = getMacroAttributes(attachedTo: DeclSyntax(node), ofType: AccessorMacro.Type.self)
297297
for (accessorAttr, accessorMacro) in accessorMacroAttributes {
298298
do {
299299
let newAccessors = try accessorMacro.expansion(
@@ -362,7 +362,7 @@ extension MacroApplication {
362362
// set of peer declarations.
363363
private func expandPeers(of decl: DeclSyntax) -> [DeclSyntax] {
364364
var peers: [DeclSyntax] = []
365-
let macroAttributes = getMacroAttributes(attachedTo: decl, ofType: PeerDeclarationMacro.Type.self)
365+
let macroAttributes = getMacroAttributes(attachedTo: decl, ofType: PeerMacro.Type.self)
366366
for (attribute, peerMacro) in macroAttributes {
367367
do {
368368
let newPeers = try peerMacro.expansion(of: attribute, attachedTo: decl, in: &context)
@@ -387,7 +387,7 @@ extension MacroApplication {
387387
of decl: Decl
388388
) -> Decl {
389389
var newMembers: [DeclSyntax] = []
390-
let macroAttributes = getMacroAttributes(attachedTo: DeclSyntax(decl), ofType: MemberDeclarationMacro.Type.self)
390+
let macroAttributes = getMacroAttributes(attachedTo: DeclSyntax(decl), ofType: MemberMacro.Type.self)
391391
for (attribute, memberMacro) in macroAttributes {
392392
do {
393393
try newMembers.append(

Sources/_SwiftSyntaxMacros/MemberAttributeMacro.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import SwiftSyntax
1414

1515
/// Describes a macro that can add attributes to the members inside the
1616
/// declaration it's attached to.
17-
public protocol MemberAttributeMacro: DeclarationMacro {
17+
public protocol MemberAttributeMacro: AttachedMacro {
1818
/// Expand an attached declaration macro to produce an attribute list for
1919
/// a given member.
2020
///

Sources/_SwiftSyntaxMacros/MemberDeclarationMacro.swift renamed to Sources/_SwiftSyntaxMacros/MemberMacro.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
import SwiftSyntax
1414

1515
/// Describes a macro that can add members to the declaration it's attached to.
16-
public protocol MemberDeclarationMacro: DeclarationMacro {
16+
public protocol MemberMacro: AttachedMacro {
1717
/// Expand an attached declaration macro to produce a set of members.
1818
///
1919
/// - Parameters:
@@ -29,3 +29,6 @@ public protocol MemberDeclarationMacro: DeclarationMacro {
2929
in context: inout MacroExpansionContext
3030
) throws -> [DeclSyntax]
3131
}
32+
33+
@available(*, deprecated, renamed: "MemberMacro")
34+
public typealias MemberDeclarationMacro = MemberMacro

Sources/_SwiftSyntaxMacros/PeerDeclarationMacro.swift renamed to Sources/_SwiftSyntaxMacros/PeerMacro.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
import SwiftSyntax
1111

12-
public protocol PeerDeclarationMacro: DeclarationMacro {
12+
public protocol PeerMacro: AttachedMacro {
1313
/// Expand a macro described by the given custom attribute and
1414
/// attached to the given declaration and evaluated within a
1515
/// particular expansion context.
@@ -22,3 +22,6 @@ public protocol PeerDeclarationMacro: DeclarationMacro {
2222
in context: inout MacroExpansionContext
2323
) throws -> [DeclSyntax]
2424
}
25+
26+
@available(*, deprecated, renamed: "PeerMacro")
27+
public typealias PeerDeclarationMacro = PeerMacro

Tests/SwiftSyntaxMacrosTest/MacroSystemTests.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ extension SimpleDiagnosticMessage: FixItMessage {
154154
var fixItID: MessageID { diagnosticID }
155155
}
156156

157-
public struct ErrorMacro: FreestandingDeclarationMacro {
157+
public struct ErrorMacro: DeclarationMacro {
158158
public static func expansion(
159159
of node: MacroExpansionDeclSyntax,
160160
in context: inout MacroExpansionContext
@@ -183,7 +183,7 @@ public struct ErrorMacro: FreestandingDeclarationMacro {
183183
}
184184
}
185185

186-
struct DefineBitwidthNumberedStructsMacro: FreestandingDeclarationMacro {
186+
struct DefineBitwidthNumberedStructsMacro: DeclarationMacro {
187187
static func expansion(
188188
of node: MacroExpansionDeclSyntax,
189189
in context: inout MacroExpansionContext
@@ -210,7 +210,7 @@ struct DefineBitwidthNumberedStructsMacro: FreestandingDeclarationMacro {
210210

211211
public struct PropertyWrapper {}
212212

213-
extension PropertyWrapper: AccessorDeclarationMacro {
213+
extension PropertyWrapper: AccessorMacro {
214214
public static func expansion(
215215
of node: AttributeSyntax,
216216
attachedTo declaration: DeclSyntax,
@@ -241,7 +241,7 @@ extension PropertyWrapper: AccessorDeclarationMacro {
241241
}
242242
}
243243

244-
extension PropertyWrapper: PeerDeclarationMacro {
244+
extension PropertyWrapper: PeerMacro {
245245
public static func expansion(
246246
of node: AttributeSyntax,
247247
attachedTo declaration: DeclSyntax,
@@ -278,7 +278,7 @@ extension PropertyWrapper: PeerDeclarationMacro {
278278
}
279279
}
280280

281-
public struct AddCompletionHandler: PeerDeclarationMacro {
281+
public struct AddCompletionHandler: PeerMacro {
282282
public static func expansion(
283283
of node: AttributeSyntax,
284284
attachedTo declaration: DeclSyntax,
@@ -389,7 +389,7 @@ public struct AddCompletionHandler: PeerDeclarationMacro {
389389
}
390390
}
391391

392-
public struct AddBackingStorage: MemberDeclarationMacro {
392+
public struct AddBackingStorage: MemberMacro {
393393
public static func expansion(
394394
of node: AttributeSyntax,
395395
attachedTo decl: DeclSyntax,
@@ -470,7 +470,7 @@ public struct WrapStoredProperties: MemberAttributeMacro {
470470

471471
struct CustomTypeWrapperMacro {}
472472

473-
extension CustomTypeWrapperMacro: MemberDeclarationMacro {
473+
extension CustomTypeWrapperMacro: MemberMacro {
474474
static func expansion(
475475
of node: AttributeSyntax,
476476
attachedTo declaration: DeclSyntax,
@@ -503,7 +503,7 @@ extension CustomTypeWrapperMacro: MemberAttributeMacro {
503503
}
504504
}
505505

506-
extension CustomTypeWrapperMacro: AccessorDeclarationMacro {
506+
extension CustomTypeWrapperMacro: AccessorMacro {
507507
static func expansion(
508508
of node: AttributeSyntax,
509509
attachedTo declaration: DeclSyntax,

0 commit comments

Comments
 (0)