Skip to content

Commit 3b7d3ad

Browse files
authored
Merge pull request #1071 from DougGregor/macros-owning-supplemental-modules
2 parents e4a3ce8 + afbca91 commit 3b7d3ad

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

Sources/_SwiftSyntaxMacros/Macro.swift

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,29 @@ public protocol Macro: _CompilerPlugin {
3939
/// as `#line`) that does not. This is a syntactic distinction, not a
4040
/// semantic one.
4141
static var signature: TypeSyntax { get }
42+
43+
/// The module that "owns" this macro.
44+
///
45+
/// This module must be imported by any code that wishes to use the macro.
46+
static var owningModule: String { get }
47+
48+
/// Additional imports requires to describe the signature of the macro.
49+
///
50+
/// For example, if your macro is owned by module A, but its signature also
51+
/// contains types from another module B that is used by A, then the module
52+
/// B should be
53+
static var supplementalSignatureModules: [String] { get }
4254
}
4355

4456
extension Macro {
4557
/// Default, empty documentation string for macros.
4658
public static var documentation: String { "" }
59+
60+
/// Default, empty set of supplemental signature modules.
61+
///
62+
/// Many macros won't need any supplemental signature modules beyond the
63+
/// default "Swift" import.
64+
public static var supplementalSignatureModules: [String] { [] }
4765
}
4866

4967
#if canImport(_CompilerPluginSupport)
@@ -77,5 +95,24 @@ extension Macro {
7795
return (UnsafePointer(result), count: buffer.count)
7896
}
7997
}
98+
99+
public static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
100+
var module = "\(owningModule)"
101+
return module.withUTF8 { buffer in
102+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
103+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
104+
return (UnsafePointer(result), count: buffer.count)
105+
}
106+
}
107+
108+
public static func _supplementalSignatureModules()
109+
-> (UnsafePointer<UInt8>, count: Int) {
110+
var allModulesJoined = supplementalSignatureModules.joined(separator:";")
111+
return allModulesJoined.withUTF8 { buffer in
112+
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
113+
result.initialize(from: buffer.baseAddress!, count: buffer.count)
114+
return (UnsafePointer(result), count: buffer.count)
115+
}
116+
}
80117
}
81118
#endif

Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ struct ColumnMacro: ExpressionMacro {
2727

2828
static var signature: TypeSyntax = "T"
2929

30+
static var owningModule: String = "Swift"
31+
3032
static func apply(
3133
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
3234
) -> MacroResult<ExprSyntax> {
@@ -51,6 +53,8 @@ struct LineMacro: ExpressionMacro {
5153

5254
static var signature: TypeSyntax = "T"
5355

56+
static var owningModule: String = "Swift"
57+
5458
static func apply(
5559
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
5660
) -> MacroResult<ExprSyntax> {
@@ -87,6 +91,8 @@ struct FunctionMacro: ExpressionMacro {
8791

8892
static var signature: TypeSyntax = "T"
8993

94+
static var owningModule: String = "Swift"
95+
9096
/// Form a function name.
9197
private static func formFunctionName(
9298
_ baseName: String, _ parameters: ParameterClauseSyntax?,
@@ -221,6 +227,10 @@ struct ColorLiteralMacro: ExpressionMacro {
221227
) -> T
222228
"""
223229

230+
// FIXME: Not entirely correct, should use _ColorLiteralType from
231+
// appropriate place.
232+
static var owningModule: String = "Swift"
233+
224234
static func apply(
225235
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
226236
) -> MacroResult<ExprSyntax> {
@@ -250,6 +260,8 @@ struct FileLiteralMacro: ExpressionMacro {
250260
static var signature: TypeSyntax =
251261
"(resourceName path: String) -> T"
252262

263+
static var owningModule: String = "Swift"
264+
253265
static func apply(
254266
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
255267
) -> MacroResult<ExprSyntax> {
@@ -279,6 +291,9 @@ struct ImageLiteralMacro: ExpressionMacro {
279291
static var signature: TypeSyntax =
280292
"(resourceName path: String) -> T"
281293

294+
// FIXME: Not really correct, use _ImageLiteralType
295+
static var owningModule: String = "Swift"
296+
282297
static func apply(
283298
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
284299
) -> MacroResult<ExprSyntax> {
@@ -307,6 +322,8 @@ struct FilePathMacro: ExpressionMacro {
307322

308323
static var signature: TypeSyntax = "T"
309324

325+
static var owningModule: String = "Swift"
326+
310327
static func apply(
311328
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
312329
) -> MacroResult<ExprSyntax> {
@@ -335,6 +352,8 @@ struct FileIDMacro: ExpressionMacro {
335352

336353
static var signature: TypeSyntax = "T"
337354

355+
static var owningModule: String = "Swift"
356+
338357
static func apply(
339358
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
340359
) -> MacroResult<ExprSyntax> {
@@ -369,6 +388,8 @@ struct FileMacro: ExpressionMacro {
369388

370389
static var signature: TypeSyntax = "T"
371390

391+
static var owningModule: String = "Swift"
392+
372393
static func apply(
373394
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
374395
) -> MacroResult<ExprSyntax> {

Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ struct StringifyMacro: ExpressionMacro {
2525

2626
static var signature: TypeSyntax = "(T) -> (T, String)"
2727

28+
static var owningModule: String = "Swift"
29+
2830
static func apply(
2931
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
3032
) -> MacroResult<ExprSyntax> {

0 commit comments

Comments
 (0)