File: Macros.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (789 lines) | stat: -rw-r--r-- 26,895 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import ASTBridging
import BasicBridging
@_spi(PluginMessage) @_spi(ExperimentalLanguageFeature) import SwiftCompilerPluginMessageHandling
import SwiftDiagnostics
import SwiftParser
import SwiftSyntax
@_spi(ExperimentalLanguageFeature) @_spi(Compiler) import SwiftSyntaxMacroExpansion
import swiftASTGen

struct ExportedExternalMacro {
  var moduleName: String
  var typeName: String
  var plugin: CompilerPlugin
}

extension MacroRole {
  init(rawMacroRole: UInt8) {
    switch rawMacroRole {
    case 0: self = .expression
    case 1: self = .declaration
    case 2: self = .accessor
    case 3: self = .memberAttribute
    case 4: self = .member
    case 5: self = .peer
    case 6: self = .conformance
    case 7: self = .codeItem
    case 8: self = .`extension`
    case 9: self = .preamble
    case 10: self = .body

    default: fatalError("unknown macro role")
    }
  }
}

@_cdecl("swift_Macros_resolveExternalMacro")
public func resolveExternalMacro(
  moduleName: UnsafePointer<CChar>,
  typeName: UnsafePointer<CChar>,
  pluginOpaqueHandle: UnsafeMutableRawPointer
) -> UnsafeRawPointer {
  // NOTE: This doesn't actually resolve anything.
  // Executable plugins is "trusted" to have the macro implementation. If not,
  // the actual expansion fails.
  let exportedPtr = UnsafeMutablePointer<ExportedExternalMacro>.allocate(capacity: 1)
  exportedPtr.initialize(
    to: .init(
      moduleName: String(cString: moduleName),
      typeName: String(cString: typeName),
      plugin: CompilerPlugin(opaqueHandle: pluginOpaqueHandle)
    )
  )
  return UnsafeRawPointer(exportedPtr)
}

@_cdecl("swift_Macros_destroyExternalMacro")
public func destroyExternalMacro(
  macroPtr: UnsafeMutableRawPointer
) {
  let macroPtr = macroPtr.assumingMemoryBound(to: ExportedExternalMacro.self)
  macroPtr.deinitialize(count: 1)
  macroPtr.deallocate()
}

/// Diagnostics produced here.
enum ASTGenMacroDiagnostic: DiagnosticMessage, FixItMessage {
  case thrownError(Error)
  case oldStyleExternalMacro
  case useExternalMacro
  case unknownBuiltin(String)
  case notStringLiteralArgument(String)

  var message: String {
    switch self {
    case .thrownError(let error):
      return String(describing: error)

    case .oldStyleExternalMacro:
      return "external macro definitions are now written using #externalMacro"

    case .useExternalMacro:
      return "use '#externalMacro'"

    case .unknownBuiltin(let type):
      return "ignoring definition of unknown builtin macro \(type)"

    case .notStringLiteralArgument(let kind):
      return "argument to `#externalMacro` must be a string literal naming the external macro's \(kind)"
    }
  }

  var severity: DiagnosticSeverity {
    switch self {
    case .thrownError, .notStringLiteralArgument:
      return .error

    case .oldStyleExternalMacro, .unknownBuiltin:
      return .warning

    case .useExternalMacro:
      return .note
    }
  }

  var diagnosticID: MessageID {
    .init(domain: "Swift", id: "\(self)")
  }

  var fixItID: MessageID { diagnosticID }
}

/// Treat the given expression as a string literal, which should contain a
/// single identifier.
fileprivate func identifierFromStringLiteral(_ node: ExprSyntax) -> String? {
  guard let stringLiteral = node.as(StringLiteralExprSyntax.self),
    stringLiteral.segments.count == 1,
    let segment = stringLiteral.segments.first,
    case .stringSegment(let stringSegment) = segment
  else {
    return nil
  }

  return stringSegment.content.text
}

/// Checks if the macro expression used as an default argument has any issues.
///
/// - Returns: `true` if all restrictions are satisfied, `false` if diagnostics
/// are emitted.
@_cdecl("swift_Macros_checkDefaultArgumentMacroExpression")
func checkDefaultArgumentMacroExpression(
  diagEnginePtr: UnsafeMutableRawPointer,
  sourceFilePtr: UnsafeRawPointer,
  macroLocationPtr: UnsafePointer<UInt8>
) -> Bool {
  let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)

  // Find the macro expression.
  guard
    let macroExpr = findSyntaxNodeInSourceFile(
      sourceFilePtr: sourceFilePtr,
      sourceLocationPtr: macroLocationPtr,
      type: MacroExpansionExprSyntax.self
    )
  else {
    // FIXME: Produce an error
    return false
  }

  do {
    try macroExpr.checkDefaultArgumentMacroExpression()
    return true
  } catch let errDiags as DiagnosticsError {
    let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
    srcMgr.insert(sourceFilePtr)
    for diag in errDiags.diagnostics {
      srcMgr.diagnose(diagnostic: diag)
    }
    return false
  } catch let error {
    let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
    srcMgr.insert(sourceFilePtr)
    srcMgr.diagnose(
      diagnostic: .init(
        node: macroExpr,
        message: ASTGenMacroDiagnostic.thrownError(error)
      )
    )
    return false
  }
}

/// Check a macro definition, producing a description of that macro definition
/// for use in macro expansion.
///
/// When the resulting macro requires expansion, the result will come in
/// two parts:
///
/// - Returns: -1 on failure, BridgedMacroDefinitionKind on success. When the
/// successful result is "expanded macro", `replacementsPtr` will point to a
/// number of "replacements" to perform when expanding that macro. Each
/// replacement is a textual replacement of use of a macro parameter with the
/// source text of the corresponding argument, and is represented as a triple
/// (start offset, end offset, parameter index): the [start offset, end offset)
/// range in the macro expansion expression should be replaced with the
/// argument matching the corresponding parameter.
@_cdecl("swift_Macros_checkMacroDefinition")
func checkMacroDefinition(
  diagEnginePtr: UnsafeMutableRawPointer,
  sourceFileBuffer: BridgedStringRef,
  macroDeclText: BridgedStringRef,
  externalMacroOutPtr: UnsafeMutablePointer<BridgedStringRef>,
  replacementsPtr: UnsafeMutablePointer<UnsafeMutablePointer<Int>?>,
  numReplacementsPtr: UnsafeMutablePointer<Int>,
  genericReplacementsPtr: UnsafeMutablePointer<UnsafeMutablePointer<Int>?>,
  numGenericReplacementsPtr: UnsafeMutablePointer<Int>
) -> Int {
  // Assert "out" parameters are initialized.
  assert(externalMacroOutPtr.pointee.isEmpty)
  assert(replacementsPtr.pointee == nil && numReplacementsPtr.pointee == 0)

  // Parse 'macro' decl.
  // FIXME: Use 'ExportedSourceFile' when C++ parser is replaced.
  let textBuffer = UnsafeBufferPointer<UInt8>(start: macroDeclText.data, count: macroDeclText.count)
  var parser = Parser(textBuffer)
  guard let macroDecl = DeclSyntax.parse(from: &parser).as(MacroDeclSyntax.self) else {
    // FIXME: Produce an error
    return -1
  }

  func diagnose(diagnostic: Diagnostic) {
    emitDiagnostic(
      diagnosticEngine: BridgedDiagnosticEngine(raw: diagEnginePtr),
      sourceFileBuffer: UnsafeBufferPointer(start: sourceFileBuffer.data, count: sourceFileBuffer.count),
      sourceFileBufferOffset: macroDeclText.data! - sourceFileBuffer.data!,
      diagnostic: diagnostic,
      diagnosticSeverity: diagnostic.diagMessage.severity
    )
  }

  // Check the definition
  do {
    let definition = try macroDecl.checkDefinition()
    switch definition {
    case let .deprecatedExternal(node: node, module: module, type: type):
      // Check for known builtins.
      if module == "Builtin" {
        switch type {
        case "ExternalMacro":
          return Int(BridgedMacroDefinitionKind.builtinExternalMacro.rawValue)

        case "IsolationMacro":
          return Int(BridgedMacroDefinitionKind.builtinIsolationMacro.rawValue)

        // These builtins don't exist, but are put into the standard library at
        // least for documentation purposes right now. Don't emit a warning for
        // them, but do fail operation.
        case "FileIDMacro",
          "FilePathMacro",
          "FileMacro",
          "FunctionMacro",
          "LineMacro",
          "ColumnMacro",
          "DSOHandleMacro",
          "WarningMacro",
          "ErrorMacro":
          return -1

        default:
          // Warn about the unknown builtin.
          diagnose(
            diagnostic: .init(
              node: node,
              message: ASTGenMacroDiagnostic.unknownBuiltin(type)
            )
          )

          return -1
        }
      }

      // Form the "ModuleName.TypeName" result string.
      externalMacroOutPtr.pointee =
        allocateBridgedString("\(module).\(type)")

      // Translate this into a use of #externalMacro.
      let expansionSourceSyntax: ExprSyntax =
        "#externalMacro(module: \(literal: module), type: \(literal: type))"

      // Warn about the use of old-style external macro syntax here.
      diagnose(
        diagnostic: .init(
          node: node,
          message: ASTGenMacroDiagnostic.oldStyleExternalMacro,
          fixIts: [
            FixIt(
              message: ASTGenMacroDiagnostic.useExternalMacro,
              changes: [
                FixIt.Change.replace(
                  oldNode: node,
                  newNode: Syntax(expansionSourceSyntax)
                )
              ]
            )
          ]
        )
      )
      return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)

    case let .expansion(expansionSyntax, replacements: _, genericReplacements: _)
    where expansionSyntax.macroName.text == "externalMacro":
      // Extract the identifier from the "module" argument.
      guard let firstArg = expansionSyntax.arguments.first,
        let firstArgLabel = firstArg.label?.text,
        firstArgLabel == "module",
        let module = identifierFromStringLiteral(firstArg.expression)
      else {
        diagnose(
          diagnostic: .init(
            node: Syntax(expansionSyntax),
            message: ASTGenMacroDiagnostic.notStringLiteralArgument("module")
          )
        )
        return -1
      }

      // Extract the identifier from the "type" argument.
      guard let secondArg = expansionSyntax.arguments.dropFirst().first,
        let secondArgLabel = secondArg.label?.text,
        secondArgLabel == "type",
        let type = identifierFromStringLiteral(secondArg.expression)
      else {
        diagnose(
          diagnostic: .init(
            node: Syntax(expansionSyntax),
            message: ASTGenMacroDiagnostic.notStringLiteralArgument("type")
          )
        )
        return -1
      }

      // Form the "ModuleName.TypeName" result string.
      externalMacroOutPtr.pointee =
        allocateBridgedString("\(module).\(type)")
      return Int(BridgedMacroDefinitionKind.externalMacro.rawValue)

    case let .expansion(expansionSyntax,
      replacements: replacements, genericReplacements: genericReplacements):
      // Provide the expansion syntax.
      externalMacroOutPtr.pointee =
        allocateBridgedString(expansionSyntax.trimmedDescription)

      // If there are no replacements, we're done.
      let totalReplacementsCount = replacements.count + genericReplacements.count
      guard totalReplacementsCount > 0 else {
        return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
      }

      // The replacements are triples: (startOffset, endOffset, parameter index).
      let replacementBuffer = UnsafeMutableBufferPointer<Int>.allocate(capacity: 3 * replacements.count)
      for (index, replacement) in replacements.enumerated() {
        let expansionStart = expansionSyntax.positionAfterSkippingLeadingTrivia.utf8Offset

        replacementBuffer[index * 3] =
          replacement.reference.positionAfterSkippingLeadingTrivia.utf8Offset - expansionStart
        replacementBuffer[index * 3 + 1] =
          replacement.reference.endPositionBeforeTrailingTrivia.utf8Offset - expansionStart
        replacementBuffer[index * 3 + 2] = replacement.parameterIndex
      }
      replacementsPtr.pointee = replacementBuffer.baseAddress
      numReplacementsPtr.pointee = replacements.count

      // The replacements are triples: (startOffset, endOffset, parameter index).
      let genericReplacementBuffer = UnsafeMutableBufferPointer<Int>.allocate(capacity: 3 * genericReplacements.count)
      for (index, genericReplacement) in genericReplacements.enumerated() {
        let expansionStart = expansionSyntax.positionAfterSkippingLeadingTrivia.utf8Offset

        genericReplacementBuffer[index * 3] =
          genericReplacement.reference.positionAfterSkippingLeadingTrivia.utf8Offset - expansionStart
        genericReplacementBuffer[index * 3 + 1] =
          genericReplacement.reference.endPositionBeforeTrailingTrivia.utf8Offset - expansionStart
        genericReplacementBuffer[index * 3 + 2] =
          genericReplacement.parameterIndex
      }
      genericReplacementsPtr.pointee = genericReplacementBuffer.baseAddress
      numGenericReplacementsPtr.pointee = genericReplacements.count

      return Int(BridgedMacroDefinitionKind.expandedMacro.rawValue)
    }
  } catch let errDiags as DiagnosticsError {
    for diag in errDiags.diagnostics {
      diagnose(diagnostic: diag)
    }
    return -1
  } catch let error {
    diagnose(
      diagnostic: .init(
        node: Syntax(macroDecl),
        message: ASTGenMacroDiagnostic.thrownError(error)
      )
    )
    return -1
  }
}

@_cdecl("swift_Macros_freeExpansionReplacements")
public func freeExpansionReplacements(
  pointer: UnsafeMutablePointer<Int>?,
  numReplacements: Int
) {
  UnsafeMutableBufferPointer(start: pointer, count: numReplacements).deallocate()
}

// Make an expansion result for '@_cdecl' function caller.
func makeExpansionOutputResult(
  expandedSource: String?,
  outputPointer: UnsafeMutablePointer<BridgedStringRef>
) -> Int {
  guard let expandedSource = expandedSource else {
    outputPointer.pointee = BridgedStringRef()
    return -1
  }
  outputPointer.pointee = allocateBridgedString(expandedSource)
  return 0
}

@_cdecl("swift_Macros_expandFreestandingMacro")
@usableFromInline
func expandFreestandingMacro(
  diagEnginePtr: UnsafeMutableRawPointer,
  macroPtr: UnsafeRawPointer,
  discriminatorText: UnsafePointer<CChar>,
  rawMacroRole: UInt8,
  sourceFilePtr: UnsafeRawPointer,
  sourceLocationPtr: UnsafePointer<UInt8>?,
  expandedSourceOutPtr: UnsafeMutablePointer<BridgedStringRef>
) -> Int {
  // We didn't expand anything so far.
  assert(expandedSourceOutPtr.pointee.isEmpty)

  guard let sourceLocationPtr = sourceLocationPtr else {
    print("NULL source location")
    return -1
  }

  let sourceFilePtr = sourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)

  guard
    let macroSyntax = findSyntaxNodeInSourceFile(
      sourceFilePtr: sourceFilePtr,
      sourceLocationPtr: sourceLocationPtr,
      type: Syntax.self
    )
  else {
    return 1
  }
  guard
    let expansion = macroSyntax.asProtocol(
      FreestandingMacroExpansionSyntax.self
    )
  else {
    print("not on a macro expansion node: \(macroSyntax.debugDescription)")
    return 1
  }

  let discriminator = String(cString: discriminatorText)

  let macroRole = MacroRole(rawMacroRole: rawMacroRole)
  let expandedSource: String? = expandFreestandingMacroImpl(
    macroPtr: macroPtr,
    macroRole: macroRole,
    diagEnginePtr: diagEnginePtr,
    expansionSyntax: expansion,
    sourceFilePtr: sourceFilePtr,
    discriminator: discriminator
  )

  return makeExpansionOutputResult(
    expandedSource: expandedSource,
    outputPointer: expandedSourceOutPtr
  )
}

func expandFreestandingMacroImpl(
  macroPtr: UnsafeRawPointer,
  macroRole: MacroRole,
  diagEnginePtr: UnsafeMutableRawPointer,
  expansionSyntax: FreestandingMacroExpansionSyntax,
  sourceFilePtr: UnsafePointer<ExportedSourceFile>,
  discriminator: String
) -> String? {

  let macroName: String
  if let exprSyntax = expansionSyntax.as(MacroExpansionExprSyntax.self) {
    macroName = exprSyntax.macroName.text
  } else if let declSyntax = expansionSyntax.as(MacroExpansionDeclSyntax.self) {
    macroName = declSyntax.macroName.text
  } else {
    fatalError("unknown syntax")
  }

  let macro = macroPtr.assumingMemoryBound(to: ExportedExternalMacro.self).pointee

  // Map the macro role.
  let pluginMacroRole: PluginMessage.MacroRole
  switch macroRole {
  case .accessor, .member, .memberAttribute, .peer, .conformance, .extension, .preamble, .body:
    preconditionFailure("unhandled macro role for freestanding macro")

  case .expression: pluginMacroRole = .expression
  case .declaration: pluginMacroRole = .declaration
  case .codeItem: pluginMacroRole = .codeItem
  }

  // Send the message.
  let message = HostToPluginMessage.expandFreestandingMacro(
    macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
    macroRole: pluginMacroRole,
    discriminator: discriminator,
    syntax: PluginMessage.Syntax(syntax: Syntax(expansionSyntax), in: sourceFilePtr)!,
    lexicalContext: pluginLexicalContext(of: expansionSyntax)
  )
  do {
    let result = try macro.plugin.sendMessageAndWait(message)
    let expandedSource: String?
    let diagnostics: [PluginMessage.Diagnostic]
    switch result {
    case .expandMacroResult(let _expandedSource, let _diagnostics),
      .expandFreestandingMacroResult(let _expandedSource, let _diagnostics):
      expandedSource = _expandedSource
      diagnostics = _diagnostics
    default:
      throw PluginError.invalidReponseKind
    }

    // Process the result.
    if !diagnostics.isEmpty {
      let diagEngine = PluginDiagnosticsEngine(cxxDiagnosticEngine: diagEnginePtr)
      diagEngine.add(exportedSourceFile: sourceFilePtr)
      diagEngine.emit(diagnostics, messageSuffix: " (from macro '\(macroName)')")
    }
    return expandedSource

  } catch let error {
    let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
    srcMgr.insert(sourceFilePtr)
    srcMgr.diagnose(
      diagnostic: .init(
        node: Syntax(expansionSyntax),
        // FIXME: This is probably a plugin communication error.
        // The error might not be relevant as the diagnostic message.
        message: ASTGenMacroDiagnostic.thrownError(error)
      ),
      messageSuffix: " (from macro '\(macroName)')"
    )
    return nil
  }
}

@_cdecl("swift_Macros_expandAttachedMacro")
@usableFromInline
func expandAttachedMacro(
  diagEnginePtr: UnsafeMutableRawPointer,
  macroPtr: UnsafeRawPointer,
  discriminatorText: UnsafePointer<CChar>,
  qualifiedTypeText: UnsafePointer<CChar>,
  conformanceListText: UnsafePointer<CChar>,
  rawMacroRole: UInt8,
  customAttrSourceFilePtr: UnsafeRawPointer,
  customAttrSourceLocPointer: UnsafePointer<UInt8>?,
  declarationSourceFilePtr: UnsafeRawPointer,
  attachedTo declarationSourceLocPointer: UnsafePointer<UInt8>?,
  parentDeclSourceFilePtr: UnsafeRawPointer?,
  parentDeclSourceLocPointer: UnsafePointer<UInt8>?,
  expandedSourceOutPtr: UnsafeMutablePointer<BridgedStringRef>
) -> Int {
  // We didn't expand anything so far.
  assert(expandedSourceOutPtr.pointee.isEmpty)

  // Dig out the custom attribute for the attached macro declarations.
  guard
    let customAttrNode = findSyntaxNodeInSourceFile(
      sourceFilePtr: customAttrSourceFilePtr,
      sourceLocationPtr: customAttrSourceLocPointer,
      type: AttributeSyntax.self
    )
  else {
    return 1
  }

  // Dig out the node for the declaration to which the custom attribute is
  // attached.
  guard
    let declarationNode = findSyntaxNodeInSourceFile(
      sourceFilePtr: declarationSourceFilePtr,
      sourceLocationPtr: declarationSourceLocPointer,
      type: DeclSyntax.self
    )
  else {
    return 1
  }

  var parentDeclNode: DeclSyntax?
  if let parentDeclSourceFilePtr = parentDeclSourceFilePtr {
    parentDeclNode = findSyntaxNodeInSourceFile(
      sourceFilePtr: parentDeclSourceFilePtr,
      sourceLocationPtr: parentDeclSourceLocPointer,
      type: DeclSyntax.self
    )
  }

  let customAttrSourceFilePtr = customAttrSourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)
  let declarationSourceFilePtr = declarationSourceFilePtr.bindMemory(to: ExportedSourceFile.self, capacity: 1)
  let parentDeclSourceFilePtr = parentDeclSourceFilePtr?.bindMemory(to: ExportedSourceFile.self, capacity: 1)

  let discriminator = String(cString: discriminatorText)
  let qualifiedType = String(cString: qualifiedTypeText)
  let conformanceList = String(cString: conformanceListText)

  let expandedSource: String? = expandAttachedMacroImpl(
    diagEnginePtr: diagEnginePtr,
    macroPtr: macroPtr,
    rawMacroRole: rawMacroRole,
    discriminator: discriminator,
    qualifiedType: qualifiedType,
    conformanceList: conformanceList,
    customAttrSourceFilePtr: customAttrSourceFilePtr,
    customAttrNode: customAttrNode,
    declarationSourceFilePtr: declarationSourceFilePtr,
    attachedTo: declarationNode,
    parentDeclSourceFilePtr: parentDeclSourceFilePtr,
    parentDeclNode: parentDeclNode
  )

  return makeExpansionOutputResult(
    expandedSource: expandedSource,
    outputPointer: expandedSourceOutPtr
  )
}

/// Produce the full lexical context of the given node to pass along to
/// macro expansion.
private func lexicalContext(of node: some SyntaxProtocol) -> [Syntax] {
  // FIXME: Should we query the source manager to get the macro expansion
  // information?
  node.allMacroLexicalContexts()
}

/// Produce the full lexical context of the given node to pass along to
/// macro expansion.
private func pluginLexicalContext(of node: some SyntaxProtocol) -> [PluginMessage.Syntax] {
  lexicalContext(of: node).compactMap { .init(syntax: $0) }
}

func expandAttachedMacroImpl(
  diagEnginePtr: UnsafeMutableRawPointer,
  macroPtr: UnsafeRawPointer,
  rawMacroRole: UInt8,
  discriminator: String,
  qualifiedType: String,
  conformanceList: String,
  customAttrSourceFilePtr: UnsafePointer<ExportedSourceFile>,
  customAttrNode: AttributeSyntax,
  declarationSourceFilePtr: UnsafePointer<ExportedSourceFile>,
  attachedTo declarationNode: DeclSyntax,
  parentDeclSourceFilePtr: UnsafePointer<ExportedSourceFile>?,
  parentDeclNode: DeclSyntax?
) -> String? {
  let macroName: String = customAttrNode.attributeName.description
  let macro = macroPtr.assumingMemoryBound(to: ExportedExternalMacro.self).pointee

  // Map the macro role.
  let macroRole: PluginMessage.MacroRole
  switch MacroRole(rawMacroRole: rawMacroRole) {
  case .accessor: macroRole = .accessor
  case .member: macroRole = .member
  case .memberAttribute: macroRole = .memberAttribute
  case .peer: macroRole = .peer
  case .conformance: macroRole = .conformance
  case .extension: macroRole = .`extension`
  case .preamble: macroRole = .preamble
  case .body: macroRole = .body

  case .expression,
    .declaration,
    .codeItem:
    preconditionFailure("unhandled macro role for attached macro")
  }

  // Prepare syntax nodes to transfer.
  let customAttributeSyntax = PluginMessage.Syntax(
    syntax: Syntax(customAttrNode),
    in: customAttrSourceFilePtr
  )!

  let declSyntax = PluginMessage.Syntax(
    syntax: Syntax(declarationNode),
    in: declarationSourceFilePtr
  )!

  let parentDeclSyntax: PluginMessage.Syntax?
  if parentDeclNode != nil {
    parentDeclSyntax = .init(syntax: Syntax(parentDeclNode!), in: parentDeclSourceFilePtr!)!
  } else {
    parentDeclSyntax = nil
  }

  let extendedTypeSyntax: PluginMessage.Syntax?
  if (!qualifiedType.isEmpty) {
    let typeSyntax: TypeSyntax = "\(raw: qualifiedType)"
    extendedTypeSyntax = .init(syntax: Syntax(typeSyntax))!
  } else {
    extendedTypeSyntax = nil
  }

  let conformanceListSyntax: PluginMessage.Syntax?
  if (conformanceList.isEmpty) {
    conformanceListSyntax = nil
  } else {
    let placeholderDecl: DeclSyntax =
      """
      struct Placeholder: \(raw: conformanceList) {}
      """
    conformanceListSyntax = .init(syntax: Syntax(placeholderDecl))!
  }


  // Send the message.
  let message = HostToPluginMessage.expandAttachedMacro(
    macro: .init(moduleName: macro.moduleName, typeName: macro.typeName, name: macroName),
    macroRole: macroRole,
    discriminator: discriminator,
    attributeSyntax: customAttributeSyntax,
    declSyntax: declSyntax,
    parentDeclSyntax: parentDeclSyntax,
    extendedTypeSyntax: extendedTypeSyntax,
    conformanceListSyntax: conformanceListSyntax,
    lexicalContext: pluginLexicalContext(of: declarationNode)
  )
  do {
    let expandedSource: String?
    let diagnostics: [PluginMessage.Diagnostic]
    switch try macro.plugin.sendMessageAndWait(message) {
    case .expandMacroResult(let _expandedSource, let _diagnostics):
      expandedSource = _expandedSource
      diagnostics = _diagnostics

    // Handle legacy result message.
    case .expandAttachedMacroResult(let _expandedSources, let _diagnostics):
      if let _expandedSources = _expandedSources {
        expandedSource = SwiftSyntaxMacroExpansion.collapse(
          expansions: _expandedSources,
          for: MacroRole(rawMacroRole: rawMacroRole),
          attachedTo: declarationNode
        )
      } else {
        expandedSource = nil
      }
      diagnostics = _diagnostics
      break
    default:
      throw PluginError.invalidReponseKind
    }

    // Process the result.
    if !diagnostics.isEmpty {
      let diagEngine = PluginDiagnosticsEngine(cxxDiagnosticEngine: diagEnginePtr)
      diagEngine.add(exportedSourceFile: customAttrSourceFilePtr)
      diagEngine.add(exportedSourceFile: declarationSourceFilePtr)
      if let parentDeclSourceFilePtr = parentDeclSourceFilePtr {
        diagEngine.add(exportedSourceFile: parentDeclSourceFilePtr)
      }
      diagEngine.emit(diagnostics, messageSuffix: " (from macro '\(macroName)')")
    }
    return expandedSource

  } catch let error {
    let srcMgr = SourceManager(cxxDiagnosticEngine: diagEnginePtr)
    srcMgr.insert(customAttrSourceFilePtr)
    srcMgr.insert(declarationSourceFilePtr)
    if let parentDeclSourceFilePtr = parentDeclSourceFilePtr {
      srcMgr.insert(parentDeclSourceFilePtr)
    }
    // FIXME: Need to decide where to diagnose the error:
    srcMgr.diagnose(
      diagnostic: .init(
        node: Syntax(declarationNode),
        // FIXME: This is probably a plugin communication error.
        // The error might not be relevant as the diagnostic message.
        message: ASTGenMacroDiagnostic.thrownError(error)
      ),
      messageSuffix: " (from macro '\(macroName)')"
    )
    return nil
  }
}