Skip to content

Commit 2b092ab

Browse files
committed
Extend macro protocols with information about the macro types.
Macros are intended to have typed inputs and outputs, so express those via a signature and (optional) generic parameter clause to help express that signature. This puts the entirety of the macro's "declaration" in the conformance, so it can be queried by the macro system.
1 parent b8b64e2 commit 2b092ab

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

Sources/_SwiftSyntaxMacros/Macro.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,22 @@
99
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
1010
//
1111
//===----------------------------------------------------------------------===//
12+
import SwiftSyntax
1213

1314
/// Describes a macro.
1415
public protocol Macro {
1516
/// The name of this macro.
1617
static var name: String { get }
18+
19+
/// The generic signature to use when describing the type of this macro.
20+
static var genericSignature: GenericParameterClauseSyntax? { get }
21+
22+
/// The type signature for this macro.
23+
///
24+
/// A function type indicates a function-like macro (such as
25+
/// `#colorLiteral(red: r, green: b, blue: b, alpha: a)`) that takes
26+
/// arguments, whereas any other type indicates a value-like macro (such
27+
/// as `#line`) that does not. This is a syntactic distinction, not a
28+
/// semantic one.
29+
static var signature: TypeSyntax { get }
1730
}

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,13 @@ import SwiftSyntaxBuilder
1616
struct ColumnMacro: ExpressionMacro {
1717
static var name: String { "column" }
1818

19+
static var genericSignature: GenericParameterClauseSyntax? =
20+
"""
21+
<T: ExpressibleByIntegerLiteral>
22+
"""
23+
24+
static var signature: TypeSyntax = "T"
25+
1926
static func apply(
2027
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
2128
) -> MacroResult<ExprSyntax> {
@@ -29,6 +36,13 @@ struct ColumnMacro: ExpressionMacro {
2936
struct LineMacro: ExpressionMacro {
3037
static var name: String { "line" }
3138

39+
static var genericSignature: GenericParameterClauseSyntax? =
40+
"""
41+
<T: ExpressibleByIntegerLiteral>
42+
"""
43+
44+
static var signature: TypeSyntax = "T"
45+
3246
static func apply(
3347
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
3448
) -> MacroResult<ExprSyntax> {
@@ -54,6 +68,13 @@ extension PatternBindingSyntax {
5468
struct FunctionMacro: ExpressionMacro {
5569
static var name: String { "function" }
5670

71+
static var genericSignature: GenericParameterClauseSyntax? =
72+
"""
73+
<T: ExpressibleByStringLiteral>
74+
"""
75+
76+
static var signature: TypeSyntax = "T"
77+
5778
/// Form a function name.
5879
private static func formFunctionName(
5980
_ baseName: String, _ parameters: ParameterClauseSyntax?,
@@ -159,6 +180,18 @@ struct FunctionMacro: ExpressionMacro {
159180
struct ColorLiteralMacro: ExpressionMacro {
160181
static var name: String { "colorLiteral" }
161182

183+
static var genericSignature: GenericParameterClauseSyntax? =
184+
"""
185+
<T: ExpressibleByColorLiteral>
186+
"""
187+
188+
static var signature: TypeSyntax =
189+
"""
190+
(
191+
_colorLiteralRed red: Float, green: Float, blue: Float, alpha: Float
192+
) -> T
193+
"""
194+
162195
static func apply(
163196
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
164197
) -> MacroResult<ExprSyntax> {
@@ -173,6 +206,14 @@ struct ColorLiteralMacro: ExpressionMacro {
173206
struct FileLiteralMacro: ExpressionMacro {
174207
static var name: String { "fileLiteral" }
175208

209+
static var genericSignature: GenericParameterClauseSyntax? =
210+
"""
211+
<T: ExpressibleByFileReferenceLiteral>
212+
"""
213+
214+
static var signature: TypeSyntax =
215+
"(fileReferenceLiteralResourceName path: String) -> T"
216+
176217
static func apply(
177218
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
178219
) -> MacroResult<ExprSyntax> {
@@ -187,6 +228,14 @@ struct FileLiteralMacro: ExpressionMacro {
187228
struct ImageLiteralMacro: ExpressionMacro {
188229
static var name: String { "imageLiteral" }
189230

231+
static var genericSignature: GenericParameterClauseSyntax? =
232+
"""
233+
<T: ExpressibleByImageLiteral>
234+
"""
235+
236+
static var signature: TypeSyntax =
237+
"(imageLiteralResourceName path: String) -> T"
238+
190239
static func apply(
191240
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
192241
) -> MacroResult<ExprSyntax> {
@@ -201,6 +250,13 @@ struct ImageLiteralMacro: ExpressionMacro {
201250
struct FilePathMacro: ExpressionMacro {
202251
static var name: String { "filePath" }
203252

253+
static var genericSignature: GenericParameterClauseSyntax? =
254+
"""
255+
<T: ExpressibleByStringLiteral>
256+
"""
257+
258+
static var signature: TypeSyntax = "T"
259+
204260
static func apply(
205261
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
206262
) -> MacroResult<ExprSyntax> {
@@ -218,6 +274,13 @@ struct FilePathMacro: ExpressionMacro {
218274
struct FileIDMacro: ExpressionMacro {
219275
static var name: String { "fileID" }
220276

277+
static var genericSignature: GenericParameterClauseSyntax? =
278+
"""
279+
<T: ExpressibleByStringLiteral>
280+
"""
281+
282+
static var signature: TypeSyntax = "T"
283+
221284
static func apply(
222285
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
223286
) -> MacroResult<ExprSyntax> {
@@ -241,6 +304,13 @@ struct FileIDMacro: ExpressionMacro {
241304
struct FileMacro: ExpressionMacro {
242305
static var name: String { "file" }
243306

307+
static var genericSignature: GenericParameterClauseSyntax? =
308+
"""
309+
<T: ExpressibleByStringLiteral>
310+
"""
311+
312+
static var signature: TypeSyntax = "T"
313+
244314
static func apply(
245315
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
246316
) -> MacroResult<ExprSyntax> {

Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ import SwiftSyntaxBuilder
1717
struct StringifyMacro: ExpressionMacro {
1818
static var name: String { "stringify" }
1919

20+
static var genericSignature: GenericParameterClauseSyntax? = "<T>"
21+
22+
static var signature: TypeSyntax = "(T) -> (T, String)"
23+
2024
static func apply(
2125
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
2226
) -> MacroResult<ExprSyntax> {

0 commit comments

Comments
 (0)