Skip to content

[Macros] Add notion of owning module and supplemental modules for macros #1071

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Sources/_SwiftSyntaxMacros/Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,29 @@ public protocol Macro: _CompilerPlugin {
/// as `#line`) that does not. This is a syntactic distinction, not a
/// semantic one.
static var signature: TypeSyntax { get }

/// The module that "owns" this macro.
///
/// This module must be imported by any code that wishes to use the macro.
static var owningModule: String { get }

/// Additional imports requires to describe the signature of the macro.
///
/// For example, if your macro is owned by module A, but its signature also
/// contains types from another module B that is used by A, then the module
/// B should be
static var supplementalSignatureModules: [String] { get }
}

extension Macro {
/// Default, empty documentation string for macros.
public static var documentation: String { "" }

/// Default, empty set of supplemental signature modules.
///
/// Many macros won't need any supplemental signature modules beyond the
/// default "Swift" import.
public static var supplementalSignatureModules: [String] { [] }
}

#if canImport(_CompilerPluginSupport)
Expand Down Expand Up @@ -77,5 +95,24 @@ extension Macro {
return (UnsafePointer(result), count: buffer.count)
}
}

public static func _owningModule() -> (UnsafePointer<UInt8>, count: Int) {
var module = "\(owningModule)"
return module.withUTF8 { buffer in
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
result.initialize(from: buffer.baseAddress!, count: buffer.count)
return (UnsafePointer(result), count: buffer.count)
}
}

public static func _supplementalSignatureModules()
-> (UnsafePointer<UInt8>, count: Int) {
var allModulesJoined = supplementalSignatureModules.joined(separator:";")
return allModulesJoined.withUTF8 { buffer in
let result = UnsafeMutablePointer<UInt8>.allocate(capacity: buffer.count)
result.initialize(from: buffer.baseAddress!, count: buffer.count)
return (UnsafePointer(result), count: buffer.count)
}
}
}
#endif
21 changes: 21 additions & 0 deletions Sources/_SwiftSyntaxMacros/MacroSystem+Builtin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ struct ColumnMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand All @@ -51,6 +53,8 @@ struct LineMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -87,6 +91,8 @@ struct FunctionMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

/// Form a function name.
private static func formFunctionName(
_ baseName: String, _ parameters: ParameterClauseSyntax?,
Expand Down Expand Up @@ -221,6 +227,10 @@ struct ColorLiteralMacro: ExpressionMacro {
) -> T
"""

// FIXME: Not entirely correct, should use _ColorLiteralType from
// appropriate place.
static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -250,6 +260,8 @@ struct FileLiteralMacro: ExpressionMacro {
static var signature: TypeSyntax =
"(resourceName path: String) -> T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -279,6 +291,9 @@ struct ImageLiteralMacro: ExpressionMacro {
static var signature: TypeSyntax =
"(resourceName path: String) -> T"

// FIXME: Not really correct, use _ImageLiteralType
static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -307,6 +322,8 @@ struct FilePathMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -335,6 +352,8 @@ struct FileIDMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down Expand Up @@ -369,6 +388,8 @@ struct FileMacro: ExpressionMacro {

static var signature: TypeSyntax = "T"

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down
2 changes: 2 additions & 0 deletions Sources/_SwiftSyntaxMacros/MacroSystem+Examples.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ struct StringifyMacro: ExpressionMacro {

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

static var owningModule: String = "Swift"

static func apply(
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax> {
Expand Down