@@ -211,6 +211,7 @@ public func expandFreestandingMacro(
211211/// - Returns: A list of expanded source text. Upon failure (i.e.
212212/// `definition.expansion()` throws) returns `nil`, and the diagnostics
213213/// representing the `Error` are guaranteed to be added to context.
214+ @available ( * , deprecated, message: " Change the 'declarationNode' argument label to 'node' " )
214215public func expandAttachedMacroWithoutCollapsing< Context: MacroExpansionContext > (
215216 definition: Macro . Type ,
216217 macroRole: MacroRole ,
@@ -221,10 +222,51 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
221222 conformanceList: InheritedTypeListSyntax ? ,
222223 in context: Context ,
223224 indentationWidth: Trivia ? = nil
225+ ) -> [ String ] ? {
226+ expandAttachedMacroWithoutCollapsing (
227+ definition: definition,
228+ macroRole: macroRole,
229+ attributeNode: attributeNode,
230+ node: declarationNode,
231+ parentDeclNode: parentDeclNode,
232+ extendedType: extendedType,
233+ conformanceList: conformanceList,
234+ in: context,
235+ indentationWidth: indentationWidth
236+ )
237+ }
238+
239+ /// Expand `@attached(XXX)` macros.
240+ ///
241+ /// - Parameters:
242+ /// - definition: a type that conforms to one or more attached `Macro` protocols.
243+ /// - macroRole: indicates which `Macro` protocol expansion should be performed
244+ /// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
245+ /// - node: target syntax node to apply the expansion. This is either a declaration
246+ /// or a closure syntax node.
247+ /// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
248+ /// context node of `declarationNode`.
249+ /// - context: context of the expansion.
250+ /// - indentationWidth: The indentation that should be added for each additional
251+ /// nesting level
252+ /// - Returns: A list of expanded source text. Upon failure (i.e.
253+ /// `definition.expansion()` throws) returns `nil`, and the diagnostics
254+ /// representing the `Error` are guaranteed to be added to context.
255+ public func expandAttachedMacroWithoutCollapsing< Context: MacroExpansionContext > (
256+ definition: Macro . Type ,
257+ macroRole: MacroRole ,
258+ attributeNode: AttributeSyntax ,
259+ node: some SyntaxProtocol ,
260+ parentDeclNode: DeclSyntax ? ,
261+ extendedType: TypeSyntax ? ,
262+ conformanceList: InheritedTypeListSyntax ? ,
263+ in context: Context ,
264+ indentationWidth: Trivia ? = nil
224265) -> [ String ] ? {
225266 do {
226267 switch ( definition, macroRole) {
227268 case ( let attachedMacro as AccessorMacro . Type , . accessor) :
269+ let declarationNode = node. cast ( DeclSyntax . self)
228270 let accessors = try attachedMacro. expansion (
229271 of: attributeNode,
230272 providingAccessorsOf: declarationNode,
@@ -235,6 +277,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
235277 }
236278
237279 case ( let attachedMacro as MemberAttributeMacro . Type , . memberAttribute) :
280+ let declarationNode = node. cast ( DeclSyntax . self)
238281 guard
239282 let parentDeclGroup = parentDeclNode? . asProtocol ( DeclGroupSyntax . self)
240283 else {
@@ -255,7 +298,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
255298 }
256299
257300 case ( let attachedMacro as MemberMacro . Type , . member) :
258- guard let declGroup = declarationNode . asProtocol ( DeclGroupSyntax . self)
301+ guard let declGroup = node . asProtocol ( DeclGroupSyntax . self)
259302 else {
260303 // Compiler error: declNode for member macro must be DeclGroupSyntax.
261304 throw MacroExpansionError . declarationNotDeclGroup
@@ -274,6 +317,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
274317 }
275318
276319 case ( let attachedMacro as PeerMacro . Type , . peer) :
320+ let declarationNode = node. cast ( DeclSyntax . self)
277321 let peers = try attachedMacro. expansion (
278322 of: attributeNode,
279323 providingPeersOf: declarationNode,
@@ -286,15 +330,15 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
286330 }
287331
288332 case ( let attachedMacro as ExtensionMacro . Type , . extension) :
289- guard let declGroup = declarationNode . asProtocol ( DeclGroupSyntax . self) else {
333+ guard let declGroup = node . asProtocol ( DeclGroupSyntax . self) else {
290334 // Compiler error: type mismatch.
291335 throw MacroExpansionError . declarationNotDeclGroup
292336 }
293337
294338 let extensionOf : TypeSyntax
295339 if let extendedType {
296340 extensionOf = extendedType
297- } else if let identified = declarationNode . asProtocol ( NamedDeclSyntax . self) {
341+ } else if let identified = node . asProtocol ( NamedDeclSyntax . self) {
298342 // Fallback for old compilers with a new plugin, where
299343 extensionOf = TypeSyntax ( IdentifierTypeSyntax ( name: identified. name) )
300344 } else {
@@ -318,7 +362,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
318362
319363 case ( let attachedMacro as PreambleMacro . Type , . preamble) :
320364 guard
321- let declToPass = Syntax ( declarationNode ) . asProtocol ( SyntaxProtocol . self)
365+ let declToPass = Syntax ( node ) . asProtocol ( SyntaxProtocol . self)
322366 as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
323367 else {
324368 // Compiler error: declaration must have a body.
@@ -335,19 +379,26 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
335379 }
336380
337381 case ( let attachedMacro as BodyMacro . Type , . body) :
338- guard
339- let declToPass = Syntax ( declarationNode) . asProtocol ( SyntaxProtocol . self)
340- as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
341- else {
382+ let body : [ CodeBlockItemSyntax ]
383+ if let closureSyntax = node. as ( ClosureExprSyntax . self) {
384+ body = try attachedMacro. expansion (
385+ of: attributeNode,
386+ providingBodyFor: closureSyntax,
387+ in: context
388+ )
389+ } else if let declToPass = Syntax ( node) . asProtocol ( SyntaxProtocol . self)
390+ as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
391+ {
392+ body = try attachedMacro. expansion (
393+ of: attributeNode,
394+ providingBodyFor: declToPass,
395+ in: context
396+ )
397+ } else {
342398 // Compiler error: declaration must have a body.
343399 throw MacroExpansionError . declarationHasNoBody
344400 }
345401
346- let body = try attachedMacro. expansion (
347- of: attributeNode,
348- providingBodyFor: declToPass,
349- in: context
350- )
351402 return body. map {
352403 $0. formattedExpansion ( definition. formatMode, indentationWidth: indentationWidth)
353404 }
@@ -376,6 +427,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
376427/// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
377428/// throws) returns `nil`, and the diagnostics representing the `Error` are
378429/// guaranteed to be added to context.
430+ @available ( * , deprecated, message: " Change the 'declarationNode' argument label to 'node' " )
379431public func expandAttachedMacro< Context: MacroExpansionContext > (
380432 definition: Macro . Type ,
381433 macroRole: MacroRole ,
@@ -386,12 +438,52 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
386438 conformanceList: InheritedTypeListSyntax ? ,
387439 in context: Context ,
388440 indentationWidth: Trivia ? = nil
441+ ) -> String ? {
442+ expandAttachedMacro (
443+ definition: definition,
444+ macroRole: macroRole,
445+ attributeNode: attributeNode,
446+ node: declarationNode,
447+ parentDeclNode: parentDeclNode,
448+ extendedType: extendedType,
449+ conformanceList: conformanceList,
450+ in: context,
451+ indentationWidth: indentationWidth
452+ )
453+ }
454+
455+ /// Expand `@attached(XXX)` macros.
456+ ///
457+ /// - Parameters:
458+ /// - definition: a type that conforms to one or more attached `Macro` protocols.
459+ /// - macroRole: indicates which `Macro` protocol expansion should be performed
460+ /// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
461+ /// - node: target declaration syntax node to apply the expansion. This is either
462+ /// a declaration or a closure syntax node.
463+ /// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
464+ /// context node of `declarationNode`.
465+ /// - context: context of the expansion.
466+ /// - indentationWidth: The indentation that should be added for each additional
467+ /// nesting level
468+ /// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
469+ /// throws) returns `nil`, and the diagnostics representing the `Error` are
470+ /// guaranteed to be added to context.
471+ public func expandAttachedMacro< Context: MacroExpansionContext > (
472+ definition: Macro . Type ,
473+ macroRole: MacroRole ,
474+ attributeNode: AttributeSyntax ,
475+ node: some SyntaxProtocol ,
476+ parentDeclNode: DeclSyntax ? ,
477+ extendedType: TypeSyntax ? ,
478+ conformanceList: InheritedTypeListSyntax ? ,
479+ in context: Context ,
480+ indentationWidth: Trivia ? = nil
389481) -> String ? {
390482 let expandedSources = expandAttachedMacroWithoutCollapsing (
391483 definition: definition,
392484 macroRole: macroRole,
393485 attributeNode: attributeNode,
394- declarationNode : declarationNode ,
486+ node : node ,
395487 parentDeclNode: parentDeclNode,
396488 extendedType: extendedType,
397489 conformanceList: conformanceList,
@@ -411,7 +503,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
411503 return collapse (
412504 expansions: expandedSources,
413505 for: macroRole,
414- attachedTo: declarationNode ,
506+ attachedTo: node ,
415507 indentationWidth: collapseIndentationWidth
416508 )
417509 }
0 commit comments