1
1
import SwiftDiagnostics
2
2
import SwiftParser
3
3
import SwiftSyntax
4
- import _SwiftSyntaxMacros
4
+ import SwiftSyntaxMacros
5
5
6
6
extension SyntaxProtocol {
7
7
func token( at position: AbsolutePosition ) -> TokenSyntax ? {
@@ -117,29 +117,6 @@ fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
117
117
}
118
118
}
119
119
120
- extension MacroExpansionDeclSyntax {
121
- func asMacroExpansionExpr( ) -> MacroExpansionExprSyntax {
122
- MacroExpansionExprSyntax (
123
- unexpectedBeforePoundToken,
124
- poundToken: poundToken,
125
- unexpectedBetweenPoundTokenAndMacro,
126
- macro: macro,
127
- genericArguments: genericArguments,
128
- unexpectedBetweenGenericArgumentsAndLeftParen,
129
- leftParen: leftParen,
130
- unexpectedBetweenLeftParenAndArgumentList,
131
- argumentList: argumentList,
132
- unexpectedBetweenArgumentListAndRightParen,
133
- rightParen: rightParen,
134
- unexpectedBetweenRightParenAndTrailingClosure,
135
- trailingClosure: trailingClosure,
136
- unexpectedBetweenTrailingClosureAndAdditionalTrailingClosures,
137
- additionalTrailingClosures: additionalTrailingClosures,
138
- unexpectedAfterAdditionalTrailingClosures
139
- )
140
- }
141
- }
142
-
143
120
@_cdecl ( " swift_ASTGen_evaluateMacro " )
144
121
@usableFromInline
145
122
func evaluateMacro(
@@ -174,9 +151,13 @@ func evaluateMacro(
174
151
return - 1
175
152
}
176
153
177
- var context = MacroExpansionContext (
178
- moduleName: sourceFilePtr. pointee. moduleName,
179
- fileName: sourceFilePtr. pointee. fileName. withoutPath ( )
154
+ let context = BasicMacroExpansionContext (
155
+ sourceFiles: [
156
+ sourceFilePtr. pointee. syntax : . init(
157
+ moduleName: sourceFilePtr. pointee. moduleName,
158
+ fullFilePath: sourceFilePtr. pointee. fileName
159
+ )
160
+ ]
180
161
)
181
162
182
163
guard let parentSyntax = token. parent else {
@@ -192,26 +173,31 @@ func evaluateMacro(
192
173
switch macroPtr. pointee. macro {
193
174
// Handle expression macro.
194
175
case let exprMacro as ExpressionMacro . Type :
195
- let parentExpansion : MacroExpansionExprSyntax
196
- if let expansionExpr = parentSyntax. as ( MacroExpansionExprSyntax . self) {
197
- parentExpansion = expansionExpr
198
- } else if let expansionDecl = parentSyntax. as ( MacroExpansionDeclSyntax . self) {
199
- parentExpansion = expansionDecl. asMacroExpansionExpr ( )
200
- } else {
176
+ guard let parentExpansion = parentSyntax. asProtocol (
177
+ FreestandingMacroExpansionSyntax . self
178
+ ) else {
201
179
print ( " not on a macro expansion node: \( parentSyntax. recursiveDescription) " )
202
180
return - 1
203
181
}
204
182
205
183
macroName = parentExpansion. macro. text
206
- evaluatedSyntax = Syntax ( try exprMacro. expansion ( of: parentExpansion, in: & context) )
184
+ evaluatedSyntax = Syntax (
185
+ try exprMacro. expansion (
186
+ of: context. detach ( parentExpansion) ,
187
+ in: context
188
+ )
189
+ )
207
190
208
191
// Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
209
192
case let declMacro as DeclarationMacro . Type :
210
193
guard let parentExpansion = parentSyntax. as ( MacroExpansionDeclSyntax . self) else {
211
194
print ( " not on a macro expansion node: \( token. recursiveDescription) " )
212
195
return - 1
213
196
}
214
- let decls = try declMacro. expansion ( of: parentExpansion, in: & context)
197
+ let decls = try declMacro. expansion (
198
+ of: context. detach ( parentExpansion) ,
199
+ in: context
200
+ )
215
201
macroName = parentExpansion. macro. text
216
202
evaluatedSyntax = Syntax ( CodeBlockItemListSyntax (
217
203
decls. map { CodeBlockItemSyntax ( item: . decl( $0) ) } ) )
@@ -337,22 +323,34 @@ func expandAttachedMacro(
337
323
let macro = macroPtr. pointee. macro
338
324
let macroRole = MacroRole ( rawValue: rawMacroRole)
339
325
340
- // FIXME: Which source file? I don't know! This should go.
326
+ let attributeSourceFile = customAttrSourceFilePtr. bindMemory (
327
+ to: ExportedSourceFile . self, capacity: 1
328
+ )
341
329
let declarationSourceFilePtr = declarationSourceFilePtr. bindMemory (
342
330
to: ExportedSourceFile . self, capacity: 1
343
331
)
344
332
345
- var context = MacroExpansionContext (
333
+ // Record the source file(s).
334
+ var sourceFiles : [ SourceFileSyntax : BasicMacroExpansionContext . KnownSourceFile ] = [ : ]
335
+ sourceFiles [ attributeSourceFile. pointee. syntax] = . init(
336
+ moduleName: attributeSourceFile. pointee. moduleName,
337
+ fullFilePath: attributeSourceFile. pointee. fileName
338
+ )
339
+ sourceFiles [ declarationSourceFilePtr. pointee. syntax] = . init(
346
340
moduleName: declarationSourceFilePtr. pointee. moduleName,
347
- fileName : declarationSourceFilePtr. pointee. fileName. withoutPath ( )
341
+ fullFilePath : declarationSourceFilePtr. pointee. fileName
348
342
)
349
343
344
+ let context = BasicMacroExpansionContext ( sourceFiles: sourceFiles)
345
+
350
346
var evaluatedSyntaxStr : String
351
347
do {
352
348
switch ( macro, macroRole) {
353
349
case ( let attachedMacro as AccessorMacro . Type , . Accessor) :
354
350
let accessors = try attachedMacro. expansion (
355
- of: customAttrNode, attachedTo: declarationNode, in: & context
351
+ of: context. detach ( customAttrNode) ,
352
+ providingAccessorsOf: context. detach ( declarationNode) ,
353
+ in: context
356
354
)
357
355
358
356
// Form a buffer of accessor declarations to return to the caller.
@@ -367,15 +365,17 @@ func expandAttachedMacro(
367
365
sourceFilePtr: parentDeclSourceFilePtr,
368
366
sourceLocationPtr: parentDeclSourceLocPointer,
369
367
type: DeclSyntax . self
370
- ) else {
368
+ ) ,
369
+ let parentDeclGroup = parentDeclNode. asProtocol ( DeclGroupSyntax . self)
370
+ else {
371
371
return 1
372
372
}
373
373
374
374
let attributes = try attachedMacro. expansion (
375
- of: customAttrNode,
376
- attachedTo: parentDeclNode ,
377
- annotating : declarationNode,
378
- in: & context
375
+ of: context . detach ( customAttrNode) ,
376
+ attachedTo: context . detach ( parentDeclGroup ) ,
377
+ providingAttributesFor : context . detach ( declarationNode) ,
378
+ in: context
379
379
)
380
380
381
381
// Form a buffer containing an attribute list to return to the caller.
@@ -384,10 +384,15 @@ func expandAttachedMacro(
384
384
} . joined ( separator: " " )
385
385
386
386
case ( let attachedMacro as MemberMacro . Type , . Member) :
387
+ guard let declGroup = declarationNode. asProtocol ( DeclGroupSyntax . self)
388
+ else {
389
+ return 1
390
+ }
391
+
387
392
let members = try attachedMacro. expansion (
388
- of: customAttrNode,
389
- attachedTo : declarationNode ,
390
- in: & context
393
+ of: context . detach ( customAttrNode) ,
394
+ providingMembersOf : context . detach ( declGroup ) ,
395
+ in: context
391
396
)
392
397
393
398
// Form a buffer of member declarations to return to the caller.
0 commit comments