@@ -67,6 +67,8 @@ extension MacroRole {
6767 case 0x10 : self = . member
6868 case 0x20 : self = . peer
6969 case 0x40 : self = . conformance
70+ case 0x80 : self = . codeItem
71+
7072 default : fatalError ( " unknown macro role " )
7173 }
7274 }
@@ -414,6 +416,7 @@ func expandFreestandingMacro(
414416 macroKind: UInt8 ,
415417 discriminatorText: UnsafePointer < UInt8 > ,
416418 discriminatorTextLength: Int ,
419+ rawMacroRole: UInt8 ,
417420 sourceFilePtr: UnsafeRawPointer ,
418421 sourceLocationPtr: UnsafePointer < UInt8 > ? ,
419422 expandedSourcePointer: UnsafeMutablePointer < UnsafePointer < UInt8 > ? > ,
@@ -446,18 +449,21 @@ func expandFreestandingMacro(
446449 )
447450 let discriminator = String ( decoding: discriminatorBuffer, as: UTF8 . self)
448451
452+ let macroRole = MacroRole ( rawMacroRole: rawMacroRole)
449453 let expandedSource : String ?
450454 switch MacroPluginKind ( rawValue: macroKind) ! {
451455 case . InProcess:
452456 expandedSource = expandFreestandingMacroInProcess (
453457 macroPtr: macroPtr,
458+ macroRole: macroRole,
454459 diagEnginePtr: diagEnginePtr,
455460 expansionSyntax: expansion,
456461 sourceFilePtr: sourceFilePtr,
457462 discriminator: discriminator)
458463 case . Executable:
459464 expandedSource = expandFreestandingMacroIPC (
460465 macroPtr: macroPtr,
466+ macroRole: macroRole,
461467 diagEnginePtr: diagEnginePtr,
462468 expansionSyntax: expansion,
463469 sourceFilePtr: sourceFilePtr,
@@ -485,6 +491,7 @@ func expandFreestandingMacro(
485491
486492func expandFreestandingMacroIPC(
487493 macroPtr: UnsafeRawPointer ,
494+ macroRole: MacroRole ,
488495 diagEnginePtr: UnsafeMutablePointer < UInt8 > ,
489496 expansionSyntax: FreestandingMacroExpansionSyntax ,
490497 sourceFilePtr: UnsafePointer < ExportedSourceFile > ,
@@ -502,9 +509,21 @@ func expandFreestandingMacroIPC(
502509
503510 let macro = macroPtr. assumingMemoryBound ( to: ExportedExecutableMacro . self) . pointee
504511
512+ // Map the macro role.
513+ let pluginMacroRole : PluginMessage . MacroRole
514+ switch macroRole {
515+ case . accessor, . member, . memberAttribute, . peer, . conformance:
516+ preconditionFailure ( " unhandled macro role for freestanding macro " )
517+
518+ case . expression: pluginMacroRole = . expression
519+ case . declaration: pluginMacroRole = . freeStandingDeclaration
520+ case . codeItem: pluginMacroRole = . codeItem
521+ }
522+
505523 // Send the message.
506524 let message = HostToPluginMessage . expandFreestandingMacro (
507525 macro: . init( moduleName: macro. moduleName, typeName: macro. typeName, name: macroName) ,
526+ macroRole: pluginMacroRole,
508527 discriminator: discriminator,
509528 syntax: PluginMessage . Syntax ( syntax: Syntax ( expansionSyntax) , in: sourceFilePtr) !)
510529 do {
@@ -541,6 +560,7 @@ func expandFreestandingMacroIPC(
541560
542561func expandFreestandingMacroInProcess(
543562 macroPtr: UnsafeRawPointer ,
563+ macroRole: MacroRole ,
544564 diagEnginePtr: UnsafeMutablePointer < UInt8 > ,
545565 expansionSyntax: FreestandingMacroExpansionSyntax ,
546566 sourceFilePtr: UnsafePointer < ExportedSourceFile > ,
@@ -580,6 +600,7 @@ func expandFreestandingMacroInProcess(
580600
581601 return SwiftSyntaxMacroExpansion . expandFreestandingMacro (
582602 definition: macro,
603+ macroRole: macroRole,
583604 node: node,
584605 in: context
585606 )
0 commit comments