Skip to content

Commit b54f58e

Browse files
committed
[Macros] Introduce a ConformanceMacro protocol.
1 parent 324166c commit b54f58e

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

Sources/SwiftSyntaxMacros/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ add_swift_host_library(SwiftSyntaxMacros
1010
MacroProtocols/AccessorMacro.swift
1111
MacroProtocols/AttachedMacro.swift
1212
MacroProtocols/CodeItemMacro.swift
13+
MacroProtocols/ConformanceMacro.swift
1314
MacroProtocols/DeclarationMacro.swift
1415
MacroProtocols/ExpressionMacro.swift
1516
MacroProtocols/FreestandingMacro.swift
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SwiftSyntax
14+
15+
/// Describes a macro that can add conformances to the declaration it's
16+
/// attached to.
17+
public protocol ConformanceMacro: AttachedMacro {
18+
/// Expand an attached conformance macro to produce a set of conformances.
19+
///
20+
/// - Parameters:
21+
/// - node: The custom attribute describing the attached macro.
22+
/// - declaration: The declaration the macro attribute is attached to.
23+
/// - context: The context in which to perform the macro expansion.
24+
///
25+
/// - Returns: the set of `(type, where-clause?)` pairs that each provide the
26+
/// protocol type to which the declared type conforms, along with
27+
/// an optional where clause.
28+
static func expansion<
29+
Declaration: DeclGroupSyntax,
30+
Context: MacroExpansionContext
31+
>(
32+
of node: AttributeSyntax,
33+
providingConformancesOf declaration: Declaration,
34+
in context: Context
35+
) throws -> [(TypeSyntax, WhereClauseSyntax?)]
36+
}

Sources/SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class MacroApplication<Context: MacroExpansionContext>: SyntaxRewriter {
109109
return true
110110
}
111111

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

115115
if newAttributes.isEmpty {

0 commit comments

Comments
 (0)