Skip to content

[Macros] Support user-defined macros as compiler plugins #1022

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 1 commit into from
Oct 31, 2022
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
58 changes: 58 additions & 0 deletions Sources/_SwiftSyntaxMacros/ExpressionMacro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
//===----------------------------------------------------------------------===//

import SwiftSyntax
import SwiftParser
#if canImport(_CompilerPluginSupport)
import _CompilerPluginSupport
#endif

/// Describes a macro that is explicitly expanded as an expression.
public protocol ExpressionMacro: Macro {
Expand All @@ -20,3 +24,57 @@ public protocol ExpressionMacro: Macro {
_ macro: MacroExpansionExprSyntax, in context: MacroEvaluationContext
) -> MacroResult<ExprSyntax>
}

#if canImport(_CompilerPluginSupport)
extension ExpressionMacro {
public static func _kind() -> _CompilerPluginKind {
.expressionMacro
}

public static func _rewrite(
targetModuleName: UnsafePointer<UInt8>,
targetModuleNameCount: Int,
filePath: UnsafePointer<UInt8>,
filePathCount: Int,
sourceFileText: UnsafePointer<UInt8>,
sourceFileTextCount: Int,
localSourceText: UnsafePointer<UInt8>,
localSourceTextCount: Int
) -> (UnsafePointer<UInt8>?, count: Int) {
let targetModuleNameBuffer = UnsafeBufferPointer(
start: filePath, count: targetModuleNameCount)
let targetModuleName = String(
decoding: targetModuleNameBuffer, as: UTF8.self)
let filePathBuffer = UnsafeBufferPointer(
start: filePath, count: filePathCount)
let filePath = String(decoding: filePathBuffer, as: UTF8.self)
let sourceFileTextBuffer = UnsafeBufferPointer(
start: sourceFileText, count: sourceFileTextCount)
let sourceFileString = String(decoding: sourceFileTextBuffer, as: UTF8.self)
let sourceFileSyntax = Parser.parse(source: sourceFileString)
let converter = SourceLocationConverter(
file: filePath, tree: sourceFileSyntax)
let context = MacroEvaluationContext(
moduleName: targetModuleName, sourceLocationConverter: converter)
let meePosition = AbsolutePosition(
utf8Offset: localSourceText.distance(to: localSourceText))
guard let meeStartToken = sourceFileSyntax.token(at: meePosition),
let mee = meeStartToken.parent?.as(MacroExpansionExprSyntax.self)
else {
fatalError("Unable to locate 'MacroExpansionExprSyntax'")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It makes me pretty nervous to have a fatalError in something that will be in the compiler process. In some follow-up PR, can you find a way to report the failure back out of this API?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I'll find a way to allow diagnostics to be reported back

}

// Evaluate the macro.
let evalResult = apply(mee, in: context)

var resultString = "\(evalResult.rewritten)"
return resultString.withUTF8 { buffer in
let result = UnsafeMutableBufferPointer<UInt8>.allocate(
capacity: buffer.count + 1)
_ = result.initialize(from: buffer)
result[buffer.count] = 0
return (UnsafePointer(result.baseAddress), buffer.count)
}
}
}
#endif
21 changes: 20 additions & 1 deletion Sources/_SwiftSyntaxMacros/Macro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,16 @@
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import SwiftSyntax
#if canImport(_CompilerPluginSupport)
import _CompilerPluginSupport
#else
public typealias _CompilerPlugin = Any
#endif

/// Describes a macro.
public protocol Macro {
public protocol Macro: _CompilerPlugin {
/// The name of this macro.
static var name: String { get }

Expand All @@ -39,3 +45,16 @@ extension Macro {
/// Default, empty documentation string for macros.
public static var documentation: String { "" }
}

#if canImport(_CompilerPluginSupport)
extension Macro {
public static func _name() -> (UnsafePointer<UInt8>, count: Int) {
var name = name
return name.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