Skip to content

Commit 4ad75a8

Browse files
authored
Merge pull request #1011 from DougGregor/macro-type-information
2 parents e621e8a + b61c0e9 commit 4ad75a8

File tree

6 files changed

+99
-0
lines changed

6 files changed

+99
-0
lines changed

Sources/SwiftSyntaxBuilder/gyb_generated/SyntaxExpressibleByStringInterpolationConformances.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,12 @@ extension CatchClauseSyntax: SyntaxExpressibleByStringInterpolation {
332332

333333
extension PoundAssertStmtSyntax: SyntaxExpressibleByStringInterpolation {}
334334

335+
extension GenericParameterClauseSyntax: SyntaxExpressibleByStringInterpolation {
336+
public static func parse(from parser: inout Parser) -> Self {
337+
return parser.parseGenericParameters().syntax
338+
}
339+
}
340+
335341
extension SimpleTypeIdentifierSyntax: SyntaxExpressibleByStringInterpolation {}
336342

337343
extension MemberTypeIdentifierSyntax: SyntaxExpressibleByStringInterpolation {}

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> {

Sources/_SwiftSyntaxMacros/MacroSystem.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ public struct MacroSystem {
5252

5353
macros[macro.name] = macro
5454
}
55+
56+
/// Look for a macro with the given name.
57+
public func lookup(_ macroName: String) -> Macro.Type? {
58+
return macros[macroName]
59+
}
5560
}
5661

5762
/// Syntax rewriter that evaluates any macros encountered along the way.

gyb_syntax_support/GenericNodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
# generic-parameter-clause -> '<' generic-parameter-list generic-where-clause? '>'
103103
Node('GenericParameterClause', name_for_diagnostics='generic parameter clause',
104104
kind='Syntax',
105+
parser_function='parseGenericParameters',
105106
children=[
106107
Child('LeftAngleBracket', kind='LeftAngleToken'),
107108
Child('GenericParameterList', kind='GenericParameterList',

0 commit comments

Comments
 (0)