13
13
import SwiftSyntax
14
14
import SwiftParser
15
15
import SwiftDiagnostics
16
- #if canImport(_CompilerPluginSupport)
17
- import _CompilerPluginSupport
18
- #endif
19
16
20
17
/// Describes a macro that is explicitly expanded as an expression.
21
18
public protocol ExpressionMacro : Macro {
@@ -26,80 +23,3 @@ public protocol ExpressionMacro: Macro {
26
23
in context: inout MacroExpansionContext
27
24
) -> ExprSyntax
28
25
}
29
-
30
- #if canImport(_CompilerPluginSupport)
31
- extension _CompilerPluginSupport . _DiagnosticSeverity {
32
- fileprivate init ( _ other: SwiftDiagnostics . DiagnosticSeverity ) {
33
- switch other {
34
- case . note: self = . note
35
- case . warning: self = . warning
36
- case . error: self = . error
37
- }
38
- }
39
- }
40
-
41
- extension ExpressionMacro {
42
- public static func _kind( ) -> _CompilerPluginKind {
43
- . expressionMacro
44
- }
45
-
46
- public static func _rewrite(
47
- targetModuleName: UnsafePointer < UInt8 > ,
48
- targetModuleNameCount: Int ,
49
- filePath: UnsafePointer < UInt8 > ,
50
- filePathCount: Int ,
51
- sourceFileText: UnsafePointer < UInt8 > ,
52
- sourceFileTextCount: Int ,
53
- localSourceText: UnsafePointer < UInt8 > ,
54
- localSourceTextCount: Int
55
- ) -> ( code: UnsafePointer < UInt8 > ? , codeLength: Int ,
56
- diagnostics: UnsafePointer < _Diagnostic > ? ,
57
- diagnosticCount: Int ) {
58
- let targetModuleNameBuffer = UnsafeBufferPointer (
59
- start: filePath, count: targetModuleNameCount)
60
- let targetModuleName = String (
61
- decoding: targetModuleNameBuffer, as: UTF8 . self)
62
- let filePathBuffer = UnsafeBufferPointer (
63
- start: filePath, count: filePathCount)
64
- let filePath = String ( decoding: filePathBuffer, as: UTF8 . self)
65
- let sourceFileTextBuffer = UnsafeBufferPointer (
66
- start: sourceFileText, count: sourceFileTextCount)
67
- let sourceFileSyntax = Parser . parse ( source: sourceFileTextBuffer)
68
- let converter = SourceLocationConverter (
69
- file: filePath, tree: sourceFileSyntax)
70
- let context = MacroExpansionContext (
71
- moduleName: targetModuleName, sourceLocationConverter: converter)
72
- let meePosition = AbsolutePosition (
73
- utf8Offset: sourceFileText. distance ( to: localSourceText) )
74
- guard let meeStartToken = sourceFileSyntax. token ( at: meePosition) ,
75
- let mee = meeStartToken. parent? . as ( MacroExpansionExprSyntax . self)
76
- else {
77
- fatalError ( " Unable to locate 'MacroExpansionExprSyntax' at offset \( meePosition) " )
78
- }
79
-
80
- // Evaluate the macro.
81
- let evalResult = expand ( mee, in: context)
82
-
83
- let rawDiags = UnsafeMutablePointer< _Diagnostic> . allocate(
84
- capacity: evalResult. diagnostics. count)
85
- for (i, diag) in evalResult. diagnostics. enumerated ( ) {
86
- rawDiags. advanced ( by: i) . initialize ( to: _makeDiagnostic (
87
- message: diag. message,
88
- position: diag. position. utf8Offset,
89
- severity: . init( diag. diagMessage. severity) ) )
90
- }
91
-
92
- var resultString = " \( evalResult. rewritten) "
93
- return resultString. withUTF8 { buffer in
94
- let result = UnsafeMutableBufferPointer< UInt8> . allocate(
95
- capacity: buffer. count + 1 )
96
- _ = result. initialize ( from: buffer)
97
- result [ buffer. count] = 0
98
- return (
99
- code: UnsafePointer ( result. baseAddress) , codeLength: buffer. count,
100
- diagnostics: UnsafePointer ? ( rawDiags) ,
101
- diagnosticCount: evalResult. diagnostics. count)
102
- }
103
- }
104
- }
105
- #endif
0 commit comments