@@ -211,11 +211,52 @@ public func expandFreestandingMacro(
211
211
/// - Returns: A list of expanded source text. Upon failure (i.e.
212
212
/// `definition.expansion()` throws) returns `nil`, and the diagnostics
213
213
/// representing the `Error` are guaranteed to be added to context.
214
+ @available ( * , deprecated, message: " Change the 'declarationNode' argument label to 'node' " )
214
215
public func expandAttachedMacroWithoutCollapsing< Context: MacroExpansionContext > (
215
216
definition: Macro . Type ,
216
217
macroRole: MacroRole ,
217
218
attributeNode: AttributeSyntax ,
218
- declarationNode: some SyntaxProtocol ,
219
+ declarationNode: DeclSyntax ,
220
+ parentDeclNode: DeclSyntax ? ,
221
+ extendedType: TypeSyntax ? ,
222
+ conformanceList: InheritedTypeListSyntax ? ,
223
+ in context: Context ,
224
+ 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 ,
219
260
parentDeclNode: DeclSyntax ? ,
220
261
extendedType: TypeSyntax ? ,
221
262
conformanceList: InheritedTypeListSyntax ? ,
@@ -225,7 +266,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
225
266
do {
226
267
switch ( definition, macroRole) {
227
268
case ( let attachedMacro as AccessorMacro . Type , . accessor) :
228
- let declarationNode = declarationNode . cast ( DeclSyntax . self)
269
+ let declarationNode = node . cast ( DeclSyntax . self)
229
270
let accessors = try attachedMacro. expansion (
230
271
of: attributeNode,
231
272
providingAccessorsOf: declarationNode,
@@ -236,7 +277,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
236
277
}
237
278
238
279
case ( let attachedMacro as MemberAttributeMacro . Type , . memberAttribute) :
239
- let declarationNode = declarationNode . cast ( DeclSyntax . self)
280
+ let declarationNode = node . cast ( DeclSyntax . self)
240
281
guard
241
282
let parentDeclGroup = parentDeclNode? . asProtocol ( DeclGroupSyntax . self)
242
283
else {
@@ -257,7 +298,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
257
298
}
258
299
259
300
case ( let attachedMacro as MemberMacro . Type , . member) :
260
- guard let declGroup = declarationNode . asProtocol ( DeclGroupSyntax . self)
301
+ guard let declGroup = node . asProtocol ( DeclGroupSyntax . self)
261
302
else {
262
303
// Compiler error: declNode for member macro must be DeclGroupSyntax.
263
304
throw MacroExpansionError . declarationNotDeclGroup
@@ -276,7 +317,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
276
317
}
277
318
278
319
case ( let attachedMacro as PeerMacro . Type , . peer) :
279
- let declarationNode = declarationNode . cast ( DeclSyntax . self)
320
+ let declarationNode = node . cast ( DeclSyntax . self)
280
321
let peers = try attachedMacro. expansion (
281
322
of: attributeNode,
282
323
providingPeersOf: declarationNode,
@@ -289,15 +330,15 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
289
330
}
290
331
291
332
case ( let attachedMacro as ExtensionMacro . Type , . extension) :
292
- guard let declGroup = declarationNode . asProtocol ( DeclGroupSyntax . self) else {
333
+ guard let declGroup = node . asProtocol ( DeclGroupSyntax . self) else {
293
334
// Compiler error: type mismatch.
294
335
throw MacroExpansionError . declarationNotDeclGroup
295
336
}
296
337
297
338
let extensionOf : TypeSyntax
298
339
if let extendedType {
299
340
extensionOf = extendedType
300
- } else if let identified = declarationNode . asProtocol ( NamedDeclSyntax . self) {
341
+ } else if let identified = node . asProtocol ( NamedDeclSyntax . self) {
301
342
// Fallback for old compilers with a new plugin, where
302
343
extensionOf = TypeSyntax ( IdentifierTypeSyntax ( name: identified. name) )
303
344
} else {
@@ -321,7 +362,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
321
362
322
363
case ( let attachedMacro as PreambleMacro . Type , . preamble) :
323
364
guard
324
- let declToPass = Syntax ( declarationNode ) . asProtocol ( SyntaxProtocol . self)
365
+ let declToPass = Syntax ( node ) . asProtocol ( SyntaxProtocol . self)
325
366
as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
326
367
else {
327
368
// Compiler error: declaration must have a body.
@@ -339,13 +380,13 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
339
380
340
381
case ( let attachedMacro as BodyMacro . Type , . body) :
341
382
let body : [ CodeBlockItemSyntax ]
342
- if let closureSyntax = declarationNode . as ( ClosureExprSyntax . self) {
383
+ if let closureSyntax = node . as ( ClosureExprSyntax . self) {
343
384
body = try attachedMacro. expansion (
344
385
of: attributeNode,
345
386
providingBodyFor: closureSyntax,
346
387
in: context
347
388
)
348
- } else if let declToPass = Syntax ( declarationNode ) . asProtocol ( SyntaxProtocol . self)
389
+ } else if let declToPass = Syntax ( node ) . asProtocol ( SyntaxProtocol . self)
349
390
as? ( DeclSyntaxProtocol & WithOptionalCodeBlockSyntax )
350
391
{
351
392
body = try attachedMacro. expansion (
@@ -386,11 +427,52 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
386
427
/// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
387
428
/// throws) returns `nil`, and the diagnostics representing the `Error` are
388
429
/// guaranteed to be added to context.
430
+ @available ( * , deprecated, message: " Change the 'declarationNode' argument label to 'node' " )
431
+ public func expandAttachedMacro< Context: MacroExpansionContext > (
432
+ definition: Macro . Type ,
433
+ macroRole: MacroRole ,
434
+ attributeNode: AttributeSyntax ,
435
+ declarationNode: DeclSyntax ,
436
+ parentDeclNode: DeclSyntax ? ,
437
+ extendedType: TypeSyntax ? ,
438
+ conformanceList: InheritedTypeListSyntax ? ,
439
+ in context: Context ,
440
+ 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.
389
471
public func expandAttachedMacro< Context: MacroExpansionContext > (
390
472
definition: Macro . Type ,
391
473
macroRole: MacroRole ,
392
474
attributeNode: AttributeSyntax ,
393
- declarationNode : some SyntaxProtocol ,
475
+ node : some SyntaxProtocol ,
394
476
parentDeclNode: DeclSyntax ? ,
395
477
extendedType: TypeSyntax ? ,
396
478
conformanceList: InheritedTypeListSyntax ? ,
@@ -401,7 +483,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
401
483
definition: definition,
402
484
macroRole: macroRole,
403
485
attributeNode: attributeNode,
404
- declarationNode : declarationNode ,
486
+ node : node ,
405
487
parentDeclNode: parentDeclNode,
406
488
extendedType: extendedType,
407
489
conformanceList: conformanceList,
@@ -421,7 +503,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
421
503
return collapse (
422
504
expansions: expandedSources,
423
505
for: macroRole,
424
- attachedTo: declarationNode ,
506
+ attachedTo: node ,
425
507
indentationWidth: collapseIndentationWidth
426
508
)
427
509
}
0 commit comments