Skip to content

Commit 182950b

Browse files
committed
Update to account for SwiftSyntaxMacros module rename and overhaul
1 parent f558447 commit 182950b

File tree

8 files changed

+176
-156
lines changed

8 files changed

+176
-156
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,7 @@ Entities
396396
macro-discriminator-list ::= macro-discriminator-list? 'fM' macro-expansion-operator INDEX
397397

398398
macro-expansion-operator ::= 'f' // freestanding macro
399+
macro-expansion-operator ::= 'u' // uniquely-named entity
399400

400401
file-discriminator ::= identifier 'Ll' // anonymous file-discriminated declaration
401402

lib/ASTGen/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ if (SWIFT_SWIFT_PARSER)
7575
SwiftSyntax
7676
SwiftOperators
7777
SwiftSyntaxBuilder
78-
_SwiftSyntaxMacros
78+
SwiftSyntaxMacros
7979
)
8080

8181
# Compute the list of SwiftSyntax targets

lib/ASTGen/Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let package = Package(
2727
.product(name: "SwiftDiagnostics", package: "swift-syntax"),
2828
.product(name: "SwiftSyntax", package: "swift-syntax"),
2929
.product(name: "SwiftParser", package: "swift-syntax"),
30-
.product(name: "_SwiftSyntaxMacros", package: "swift-syntax"),
30+
.product(name: "SwiftSyntaxMacros", package: "swift-syntax"),
3131
],
3232
path: ".",
3333
exclude: ["CMakeLists.txt"],

lib/ASTGen/Sources/ASTGen/Macros.swift

Lines changed: 52 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import SwiftDiagnostics
22
import SwiftParser
33
import SwiftSyntax
4-
import _SwiftSyntaxMacros
4+
import SwiftSyntaxMacros
55

66
extension SyntaxProtocol {
77
func token(at position: AbsolutePosition) -> TokenSyntax? {
@@ -117,29 +117,6 @@ fileprivate struct ThrownErrorDiagnostic: DiagnosticMessage {
117117
}
118118
}
119119

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-
143120
@_cdecl("swift_ASTGen_evaluateMacro")
144121
@usableFromInline
145122
func evaluateMacro(
@@ -174,9 +151,13 @@ func evaluateMacro(
174151
return -1
175152
}
176153

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+
]
180161
)
181162

182163
guard let parentSyntax = token.parent else {
@@ -192,26 +173,31 @@ func evaluateMacro(
192173
switch macroPtr.pointee.macro {
193174
// Handle expression macro.
194175
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 {
201179
print("not on a macro expansion node: \(parentSyntax.recursiveDescription)")
202180
return -1
203181
}
204182

205183
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+
)
207190

208191
// Handle expression macro. The resulting decls are wrapped in a `CodeBlockItemListSyntax`.
209192
case let declMacro as DeclarationMacro.Type:
210193
guard let parentExpansion = parentSyntax.as(MacroExpansionDeclSyntax.self) else {
211194
print("not on a macro expansion node: \(token.recursiveDescription)")
212195
return -1
213196
}
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+
)
215201
macroName = parentExpansion.macro.text
216202
evaluatedSyntax = Syntax(CodeBlockItemListSyntax(
217203
decls.map { CodeBlockItemSyntax(item: .decl($0)) }))
@@ -337,22 +323,34 @@ func expandAttachedMacro(
337323
let macro = macroPtr.pointee.macro
338324
let macroRole = MacroRole(rawValue: rawMacroRole)
339325

340-
// FIXME: Which source file? I don't know! This should go.
326+
let attributeSourceFile = customAttrSourceFilePtr.bindMemory(
327+
to: ExportedSourceFile.self, capacity: 1
328+
)
341329
let declarationSourceFilePtr = declarationSourceFilePtr.bindMemory(
342330
to: ExportedSourceFile.self, capacity: 1
343331
)
344332

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(
346340
moduleName: declarationSourceFilePtr.pointee.moduleName,
347-
fileName: declarationSourceFilePtr.pointee.fileName.withoutPath()
341+
fullFilePath: declarationSourceFilePtr.pointee.fileName
348342
)
349343

344+
let context = BasicMacroExpansionContext(sourceFiles: sourceFiles)
345+
350346
var evaluatedSyntaxStr: String
351347
do {
352348
switch (macro, macroRole) {
353349
case (let attachedMacro as AccessorMacro.Type, .Accessor):
354350
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
356354
)
357355

358356
// Form a buffer of accessor declarations to return to the caller.
@@ -367,15 +365,17 @@ func expandAttachedMacro(
367365
sourceFilePtr: parentDeclSourceFilePtr,
368366
sourceLocationPtr: parentDeclSourceLocPointer,
369367
type: DeclSyntax.self
370-
) else {
368+
),
369+
let parentDeclGroup = parentDeclNode.asProtocol(DeclGroupSyntax.self)
370+
else {
371371
return 1
372372
}
373373

374374
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
379379
)
380380

381381
// Form a buffer containing an attribute list to return to the caller.
@@ -384,10 +384,15 @@ func expandAttachedMacro(
384384
}.joined(separator: " ")
385385

386386
case (let attachedMacro as MemberMacro.Type, .Member):
387+
guard let declGroup = declarationNode.asProtocol(DeclGroupSyntax.self)
388+
else {
389+
return 1
390+
}
391+
387392
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
391396
)
392397

393398
// Form a buffer of member declarations to return to the caller.

lib/Parse/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ if (SWIFT_SWIFT_PARSER)
4545
SwiftSyntax::SwiftSyntax
4646
SwiftSyntax::SwiftOperators
4747
SwiftSyntax::SwiftSyntaxBuilder
48-
SwiftSyntax::_SwiftSyntaxMacros
48+
SwiftSyntax::SwiftSyntaxMacros
4949
$<TARGET_OBJECTS:swiftASTGen>
5050
)
5151

@@ -57,7 +57,7 @@ if (SWIFT_SWIFT_PARSER)
5757
SwiftSyntax::SwiftSyntax
5858
SwiftSyntax::SwiftOperators
5959
SwiftSyntax::SwiftSyntaxBuilder
60-
SwiftSyntax::_SwiftSyntaxMacros
60+
SwiftSyntax::SwiftSyntaxMacros
6161
swiftASTGen
6262
)
6363

0 commit comments

Comments
 (0)