|
| 1 | +import SwiftDiagnostics |
| 2 | +import SwiftSyntax |
| 3 | +import SwiftSyntaxMacros |
| 4 | + |
| 5 | +extension SourceManager { |
| 6 | + class MacroExpansionContext { |
| 7 | + /// The source manager. |
| 8 | + private let sourceManager: SourceManager |
| 9 | + |
| 10 | + /// The set of diagnostics that were emitted as part of expanding the |
| 11 | + /// macro. |
| 12 | + var diagnostics: [Diagnostic] = [] |
| 13 | + |
| 14 | + /// The macro expansion discriminator, which is used to form unique names |
| 15 | + /// when requested. |
| 16 | + /// |
| 17 | + /// The expansion discriminator is combined with the `uniqueNames` counters |
| 18 | + /// to produce unique names. |
| 19 | + private var discriminator: String |
| 20 | + |
| 21 | + /// Counter for each of the uniqued names. |
| 22 | + /// |
| 23 | + /// Used in conjunction with `expansionDiscriminator`. |
| 24 | + private var uniqueNames: [String: Int] = [:] |
| 25 | + |
| 26 | + init(sourceManager: SourceManager, discriminator: String) { |
| 27 | + self.sourceManager = sourceManager |
| 28 | + self.discriminator = discriminator |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + /// Create a new macro expansion context |
| 33 | + func createMacroExpansionContext( |
| 34 | + discriminator: String = "" |
| 35 | + ) -> MacroExpansionContext { |
| 36 | + return MacroExpansionContext( |
| 37 | + sourceManager: self, discriminator: discriminator |
| 38 | + ) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +extension String { |
| 43 | + /// Retrieve the base name of a string that represents a path, removing the |
| 44 | + /// directory. |
| 45 | + fileprivate var basename: String { |
| 46 | + guard let lastSlash = lastIndex(of: "/") else { |
| 47 | + return self |
| 48 | + } |
| 49 | + |
| 50 | + return String(self[index(after: lastSlash)...]) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +extension SourceManager.MacroExpansionContext: MacroExpansionContext { |
| 55 | + /// Generate a unique name for use in the macro. |
| 56 | + public func createUniqueName(_ providedName: String) -> TokenSyntax { |
| 57 | + // If provided with an empty name, substitute in something. |
| 58 | + let name = providedName.isEmpty ? "__local" : providedName |
| 59 | + |
| 60 | + // Grab a unique index value for this name. |
| 61 | + let uniqueIndex = uniqueNames[name, default: 0] |
| 62 | + uniqueNames[name] = uniqueIndex + 1 |
| 63 | + |
| 64 | + // Start with the discriminator. |
| 65 | + var resultString = discriminator |
| 66 | + |
| 67 | + // Mangle the name |
| 68 | + resultString += "\(name.count)\(name)" |
| 69 | + |
| 70 | + // Mangle the operator for unique macro names. |
| 71 | + resultString += "fMu" |
| 72 | + |
| 73 | + // Mangle the index. |
| 74 | + if uniqueIndex > 0 { |
| 75 | + resultString += "\(uniqueIndex - 1)" |
| 76 | + } |
| 77 | + resultString += "_" |
| 78 | + |
| 79 | + return TokenSyntax(.identifier(resultString), presence: .present) |
| 80 | + } |
| 81 | + |
| 82 | + /// Produce a diagnostic while expanding the macro. |
| 83 | + public func diagnose(_ diagnostic: Diagnostic) { |
| 84 | + diagnostics.append(diagnostic) |
| 85 | + } |
| 86 | + |
| 87 | + public func location<Node: SyntaxProtocol>( |
| 88 | + of node: Node, |
| 89 | + at position: PositionInSyntaxNode, |
| 90 | + filePathMode: SourceLocationFilePathMode |
| 91 | + ) -> SourceLocation? { |
| 92 | + guard let (sourceFile, rootPosition) = sourceManager.rootSourceFile(of: node), |
| 93 | + let exportedSourceFile = |
| 94 | + sourceManager.exportedSourceFilesBySyntax[sourceFile]?.pointee |
| 95 | + else { |
| 96 | + return nil |
| 97 | + } |
| 98 | + |
| 99 | + // Determine the filename to use in the resulting location. |
| 100 | + let fileName: String |
| 101 | + switch filePathMode { |
| 102 | + case .fileID: |
| 103 | + fileName = "\(exportedSourceFile.moduleName)/\(exportedSourceFile.fileName.basename)" |
| 104 | + |
| 105 | + case .filePath: |
| 106 | + fileName = exportedSourceFile.fileName |
| 107 | + } |
| 108 | + |
| 109 | + // Find the node's offset relative to its root. |
| 110 | + let rawPosition: AbsolutePosition |
| 111 | + switch position { |
| 112 | + case .beforeLeadingTrivia: |
| 113 | + rawPosition = node.position |
| 114 | + |
| 115 | + case .afterLeadingTrivia: |
| 116 | + rawPosition = node.positionAfterSkippingLeadingTrivia |
| 117 | + |
| 118 | + case .beforeTrailingTrivia: |
| 119 | + rawPosition = node.endPositionBeforeTrailingTrivia |
| 120 | + |
| 121 | + case .afterTrailingTrivia: |
| 122 | + rawPosition = node.endPosition |
| 123 | + } |
| 124 | + |
| 125 | + let offsetWithinSyntaxNode = |
| 126 | + rawPosition.utf8Offset - node.position.utf8Offset |
| 127 | + |
| 128 | + // Do the location lookup. |
| 129 | + let converter = SourceLocationConverter(file: fileName, tree: sourceFile) |
| 130 | + return converter.location(for: rootPosition.advanced(by: offsetWithinSyntaxNode)) |
| 131 | + } |
| 132 | +} |
0 commit comments