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
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#if swift(>=6)
internal import SwiftDiagnostics
public import SwiftSyntax
internal import SwiftSyntaxBuilder
#else
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxBuilder
#endif
enum MacroExpanderError: DiagnosticMessage {
case undefined
case definitionNotMacroExpansion
case nonParameterReference(TokenSyntax)
case nonTypeReference(TokenSyntax)
case nonLiteralOrParameter
var message: String {
switch self {
case .undefined:
return "macro expansion requires a definition"
case .definitionNotMacroExpansion:
return "macro must itself be defined by a macro expansion expression (starting with '#')"
case .nonParameterReference(let name):
return "reference to value '\(name.text)' that is not a macro parameter in expansion"
case .nonTypeReference(let name):
return "reference to type '\(name)' that is not a macro type parameter in expansion"
case .nonLiteralOrParameter:
return "only literals and macro parameters are permitted in expansion"
}
}
var diagnosticID: MessageID {
.init(domain: "SwiftMacros", id: "\(self)")
}
var severity: DiagnosticSeverity {
.error
}
}
/// Provide the definition of a macro
public enum MacroDefinition {
/// An externally-defined macro, known by its type name and the module in
/// which that type resides, which uses the deprecated syntax `A.B`.
case deprecatedExternal(node: Syntax, module: String, type: String)
/// A macro that is defined by expansion of another macro.
///
/// The definition has the macro expansion expression itself, along with
/// sequence of replacements for subtrees that refer to parameters of the
/// defining macro. These subtrees will need to be replaced with the text of
/// the corresponding argument to the macro, which can be accomplished with
/// `MacroDeclSyntax.expandDefinition`.
case expansion(
MacroExpansionExprSyntax,
replacements: [Replacement],
genericReplacements: [GenericArgumentReplacement]
)
}
extension MacroDefinition {
/// Best effort compatibility shim, the case has gained additional parameters.
@available(*, deprecated, message: "Use the expansion case with three associated values instead")
public func expansion(_ node: MacroExpansionExprSyntax, replacements: [Replacement]) -> Self {
.expansion(node, replacements: replacements, genericReplacements: [])
}
}
extension MacroDefinition {
/// A replacement that occurs as part of an expanded macro definition.
public struct Replacement {
/// A reference to a parameter as it occurs in the macro expansion expression.
public let reference: DeclReferenceExprSyntax
/// The index of the parameter in the defining macro.
public let parameterIndex: Int
}
/// A replacement that occurs as part of an expanded macro definition.
public struct GenericArgumentReplacement {
/// A reference to a parameter as it occurs in the macro expansion expression.
public let reference: GenericArgumentSyntax
/// The index of the parameter in the defining macro.
public let parameterIndex: Int
}
}
fileprivate class ParameterReplacementVisitor: OnlyLiteralExprChecker {
let macro: MacroDeclSyntax
var replacements: [MacroDefinition.Replacement] = []
var genericReplacements: [MacroDefinition.GenericArgumentReplacement] = []
init(macro: MacroDeclSyntax) {
self.macro = macro
super.init()
}
// References to declarations. Only accept those that refer to a parameter
// of a macro.
override func visit(_ node: DeclReferenceExprSyntax) -> SyntaxVisitorContinueKind {
let baseName = node.baseName
let signature = macro.signature
let matchedParameter = signature.parameterClause.parameters.enumerated().first { (index, parameter) in
if baseName.text == "_" {
return false
}
guard let parameterName = parameter.parameterName else {
return false
}
return baseName.text == parameterName.text
}
guard let (parameterIndex, _) = matchedParameter else {
// We have a reference to something that isn't a parameter of the macro.
diagnostics.append(
Diagnostic(
node: Syntax(baseName),
message: MacroExpanderError.nonParameterReference(baseName)
)
)
return .visitChildren
}
replacements.append(.init(reference: node, parameterIndex: parameterIndex))
return .visitChildren
}
override func visit(_ node: GenericArgumentClauseSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}
override func visit(_ node: GenericArgumentListSyntax) -> SyntaxVisitorContinueKind {
return .visitChildren
}
override func visit(_ node: GenericArgumentSyntax) -> SyntaxVisitorContinueKind {
guard let baseName = node.argument.as(IdentifierTypeSyntax.self)?.name else {
return .skipChildren
}
guard let genericParameterClause = macro.genericParameterClause else {
return .skipChildren
}
let matchedParameter = genericParameterClause.parameters.enumerated().first { (index, parameter) in
return parameter.name.text == baseName.text
}
guard let (parameterIndex, _) = matchedParameter else {
// We have a reference to something that isn't a parameter of the macro.
diagnostics.append(
Diagnostic(
node: Syntax(baseName),
message: MacroExpanderError.nonTypeReference(baseName)
)
)
return .visitChildren
}
genericReplacements.append(.init(reference: node, parameterIndex: parameterIndex))
return .visitChildren
}
override func diagnoseNonLiteral(_ node: some SyntaxProtocol) -> SyntaxVisitorContinueKind {
diagnostics.append(
Diagnostic(
node: node,
message: MacroExpanderError.nonLiteralOrParameter
)
)
return .skipChildren
}
}
extension MacroDeclSyntax {
/// Check the definition of the given macro.
///
/// Macros are defined by an expression, which must itself be a macro
/// expansion. Check the definition and produce a semantic representation of
/// it or one of the "builtin"
///
/// Compute the sequence of parameter replacements required when expanding
/// the definition of a non-external macro.
///
/// If there are an errors that prevent expansion, the diagnostics will be
/// wrapped into an error that prevents expansion, that error is thrown.
public func checkDefinition() throws -> MacroDefinition {
// Cannot compute replacements for an undefined macro.
guard let originalDefinition = definition?.value else {
let undefinedDiag = Diagnostic(
node: Syntax(self),
message: MacroExpanderError.undefined
)
throw DiagnosticsError(diagnostics: [undefinedDiag])
}
/// Recognize the deprecated syntax A.B. Clients will need to
/// handle this themselves.
if let memberAccess = originalDefinition.as(MemberAccessExprSyntax.self),
let base = memberAccess.base,
let baseName = base.as(DeclReferenceExprSyntax.self)?.baseName
{
let memberName = memberAccess.declName.baseName
return .deprecatedExternal(
node: Syntax(memberAccess),
module: baseName.trimmedDescription,
type: memberName.trimmedDescription
)
}
// Make sure we have a macro expansion expression.
guard let definition = originalDefinition.as(MacroExpansionExprSyntax.self) else {
let badDefinitionDiag =
Diagnostic(
node: Syntax(originalDefinition),
message: MacroExpanderError.definitionNotMacroExpansion
)
throw DiagnosticsError(diagnostics: [badDefinitionDiag])
}
let visitor = ParameterReplacementVisitor(macro: self)
visitor.walk(definition)
if !visitor.diagnostics.isEmpty {
throw DiagnosticsError(diagnostics: visitor.diagnostics)
}
return .expansion(definition, replacements: visitor.replacements, genericReplacements: visitor.genericReplacements)
}
}
/// Syntax rewrite that performs macro expansion by textually replacing
/// uses of macro parameters with their corresponding arguments.
private final class MacroExpansionRewriter: SyntaxRewriter {
let parameterReplacements: [DeclReferenceExprSyntax: Int]
let arguments: [ExprSyntax]
let genericParameterReplacements: [GenericArgumentSyntax: Int]
let genericArguments: [TypeSyntax]
init(
parameterReplacements: [DeclReferenceExprSyntax: Int],
arguments: [ExprSyntax],
genericReplacements: [GenericArgumentSyntax: Int],
genericArguments: [TypeSyntax]
) {
self.parameterReplacements = parameterReplacements
self.arguments = arguments
self.genericParameterReplacements = genericReplacements
self.genericArguments = genericArguments
super.init(viewMode: .sourceAccurate)
}
override func visit(_ node: DeclReferenceExprSyntax) -> ExprSyntax {
guard let parameterIndex = parameterReplacements[node] else {
return super.visit(node)
}
// Swap in the argument for this parameter
return arguments[parameterIndex].trimmed
}
override func visit(_ node: GenericArgumentSyntax) -> GenericArgumentSyntax {
guard let parameterIndex = genericParameterReplacements[node] else {
return super.visit(node)
}
guard parameterIndex < genericArguments.count else {
return super.visit(node)
}
// Swap in the argument for type parameter
var node = node
node.argument = genericArguments[parameterIndex].trimmed
return node
}
}
extension MacroDeclSyntax {
/// Expand the definition of this macro when provided with the given
/// argument list.
private func expand(
argumentList: LabeledExprListSyntax?,
genericArgumentList: GenericArgumentClauseSyntax?,
definition: MacroExpansionExprSyntax,
replacements: [MacroDefinition.Replacement],
genericReplacements: [MacroDefinition.GenericArgumentReplacement] = []
) -> ExprSyntax {
// FIXME: Do real call-argument matching between the argument list and the
// macro parameter list, porting over from the compiler.
let parameterReplacements = Dictionary(
replacements.map { replacement in
(replacement.reference, replacement.parameterIndex)
},
uniquingKeysWith: { l, r in l }
)
let arguments: [ExprSyntax] =
argumentList?.map { element in
element.expression
} ?? []
let genericReplacements = Dictionary(
genericReplacements.map { replacement in
(replacement.reference, replacement.parameterIndex)
},
uniquingKeysWith: { l, r in l }
)
let genericArguments: [TypeSyntax] =
genericArgumentList?.arguments.map { $0.argument } ?? []
let rewriter = MacroExpansionRewriter(
parameterReplacements: parameterReplacements,
arguments: arguments,
genericReplacements: genericReplacements,
genericArguments: genericArguments
)
return rewriter.visit(definition)
}
/// Given a freestanding macro expansion syntax node that references this
/// macro declaration, expand the macro by substituting the arguments from
/// the macro expansion into the parameters that are used in the definition.
public func expand(
_ node: some FreestandingMacroExpansionSyntax,
definition: MacroExpansionExprSyntax,
replacements: [MacroDefinition.Replacement],
genericReplacements: [MacroDefinition.GenericArgumentReplacement] = []
) -> ExprSyntax {
return expand(
argumentList: node.arguments,
genericArgumentList: node.genericArgumentClause,
definition: definition,
replacements: replacements,
genericReplacements: genericReplacements
)
}
/// Given an attached macro expansion syntax node that references this
/// macro declaration, expand the macro by substituting the arguments from
/// the expansion into the parameters that are used in the definition.
public func expand(
_ node: AttributeSyntax,
definition: MacroExpansionExprSyntax,
replacements: [MacroDefinition.Replacement],
genericReplacements: [MacroDefinition.GenericArgumentReplacement] = []
) -> ExprSyntax {
// Dig out the argument list.
let argumentList: LabeledExprListSyntax?
if case let .argumentList(argList) = node.arguments {
argumentList = argList
} else {
argumentList = nil
}
return expand(
argumentList: argumentList,
genericArgumentList: .init(arguments: []),
definition: definition,
replacements: replacements,
genericReplacements: genericReplacements
)
}
}
|