11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import SwiftSyntax
14
+ import SwiftParser
15
+ #if canImport(_CompilerPluginSupport)
16
+ import _CompilerPluginSupport
17
+ #endif
14
18
15
19
/// Describes a macro that is explicitly expanded as an expression.
16
20
public protocol ExpressionMacro : Macro {
@@ -20,3 +24,54 @@ public protocol ExpressionMacro: Macro {
20
24
_ macro: MacroExpansionExprSyntax , in context: MacroEvaluationContext
21
25
) -> MacroResult < ExprSyntax >
22
26
}
27
+
28
+ #if canImport(_CompilerPluginSupport)
29
+ extension ExpressionMacro {
30
+ public static func _kind( ) -> _CompilerPluginKind {
31
+ . expressionMacro
32
+ }
33
+
34
+ public static func _rewrite(
35
+ targetModuleName: UnsafePointer < UInt8 > ,
36
+ targetModuleNameCount: Int ,
37
+ filePath: UnsafePointer < UInt8 > ,
38
+ filePathCount: Int ,
39
+ sourceFileText: UnsafePointer < UInt8 > ,
40
+ sourceFileTextCount: Int ,
41
+ localSourceText: UnsafePointer < UInt8 > ,
42
+ localSourceTextCount: Int
43
+ ) -> ( UnsafePointer < UInt8 > ? , count: Int ) {
44
+ let targetModuleNameBuffer = UnsafeBufferPointer (
45
+ start: filePath, count: targetModuleNameCount)
46
+ let targetModuleName = String (
47
+ decoding: targetModuleNameBuffer, as: UTF8 . self)
48
+ let filePathBuffer = UnsafeBufferPointer (
49
+ start: filePath, count: filePathCount)
50
+ let filePath = String ( decoding: filePathBuffer, as: UTF8 . self)
51
+ let sourceFileTextBuffer = UnsafeBufferPointer (
52
+ start: sourceFileText, count: sourceFileTextCount)
53
+ let sourceFileText = String ( decoding: sourceFileTextBuffer, as: UTF8 . self)
54
+ // TODO: `SourceLocationConverter.init(file:source:)` should be able to
55
+ // accept a buffer.
56
+ let converter = SourceLocationConverter (
57
+ file: filePath, source: sourceFileText)
58
+ let context = MacroEvaluationContext (
59
+ moduleName: targetModuleName, sourceLocationConverter: converter)
60
+ let meeTextBuffer = UnsafeBufferPointer (
61
+ start: localSourceText, count: localSourceTextCount)
62
+ var parser = Parser ( meeTextBuffer)
63
+ let mee = try ! MacroExpansionExprSyntax . parse ( from: & parser)
64
+
65
+ // Evaluate the macro.
66
+ let evalResult = apply ( mee, in: context)
67
+
68
+ var resultString = " \( evalResult. rewritten) "
69
+ return resultString. withUTF8 { buffer in
70
+ let result = UnsafeMutableBufferPointer< UInt8> . allocate(
71
+ capacity: buffer. count)
72
+ _ = result. initialize ( from: buffer)
73
+ return ( UnsafePointer ( result. baseAddress) , result. count)
74
+ }
75
+ }
76
+ }
77
+ #endif
0 commit comments