|
| 1 | +// |
| 2 | +// This source file is part of the Swift.org open source project |
| 3 | +// |
| 4 | +// Copyright (c) 2024 Apple Inc. and the Swift project authors |
| 5 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 6 | +// |
| 7 | +// See https://swift.org/LICENSE.txt for license information |
| 8 | +// See https://swift.org/CONTRIBUTORS.txt for Swift project authors |
| 9 | +// |
| 10 | + |
| 11 | +import SwiftSyntax |
| 12 | +import SwiftSyntaxBuilder |
| 13 | +import SwiftSyntaxMacros |
| 14 | + |
| 15 | +// MARK: - Finding effect keywords |
| 16 | + |
| 17 | +/// A syntax visitor class that looks for effectful keywords in a given |
| 18 | +/// expression. |
| 19 | +private final class _EffectFinder: SyntaxAnyVisitor { |
| 20 | + /// The effect keywords discovered so far. |
| 21 | + var effectKeywords: Set<Keyword> = [] |
| 22 | + |
| 23 | + override func visitAny(_ node: Syntax) -> SyntaxVisitorContinueKind { |
| 24 | + switch node.kind { |
| 25 | + case .tryExpr: |
| 26 | + effectKeywords.insert(.try) |
| 27 | + case .awaitExpr: |
| 28 | + effectKeywords.insert(.await) |
| 29 | + case .consumeExpr: |
| 30 | + effectKeywords.insert(.consume) |
| 31 | + case .borrowExpr: |
| 32 | + effectKeywords.insert(.borrow) |
| 33 | + case .unsafeExpr: |
| 34 | + effectKeywords.insert(.unsafe) |
| 35 | + case .closureExpr, .functionDecl: |
| 36 | + // Do not delve into closures or function declarations. |
| 37 | + return .skipChildren |
| 38 | + case .variableDecl: |
| 39 | + // Delve into variable declarations. |
| 40 | + return .visitChildren |
| 41 | + default: |
| 42 | + // Do not delve into declarations other than variables. |
| 43 | + if node.isProtocol((any DeclSyntaxProtocol).self) { |
| 44 | + return .skipChildren |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + // Recurse into everything else. |
| 49 | + return .visitChildren |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +/// Find effectful keywords in a syntax node. |
| 54 | +/// |
| 55 | +/// - Parameters: |
| 56 | +/// - node: The node to inspect. |
| 57 | +/// - context: The macro context in which the expression is being parsed. |
| 58 | +/// |
| 59 | +/// - Returns: A set of effectful keywords such as `await` that are present in |
| 60 | +/// `node`. |
| 61 | +/// |
| 62 | +/// This function does not descend into function declarations or closure |
| 63 | +/// expressions because they represent distinct lexical contexts and their |
| 64 | +/// effects are uninteresting in the context of `node` unless they are called. |
| 65 | +func findEffectKeywords(in node: some SyntaxProtocol, context: some MacroExpansionContext) -> Set<Keyword> { |
| 66 | + // TODO: gather any effects from the lexical context once swift-syntax-#3037 and related PRs land |
| 67 | + let effectFinder = _EffectFinder(viewMode: .sourceAccurate) |
| 68 | + effectFinder.walk(node) |
| 69 | + return effectFinder.effectKeywords |
| 70 | +} |
| 71 | + |
| 72 | +// MARK: - Inserting effect keywords/thunks |
| 73 | + |
| 74 | +/// Make a function call expression to an effectful thunk function provided by |
| 75 | +/// the testing library. |
| 76 | +/// |
| 77 | +/// - Parameters: |
| 78 | +/// - thunkName: The unqualified name of the thunk function to call. This |
| 79 | +/// token must be the name of a function in the `Testing` module. |
| 80 | +/// - expr: The expression to thunk. |
| 81 | +/// |
| 82 | +/// - Returns: An expression representing a call to the function named |
| 83 | +/// `thunkName`, passing `expr`. |
| 84 | +private func _makeCallToEffectfulThunk(_ thunkName: TokenSyntax, passing expr: some ExprSyntaxProtocol) -> ExprSyntax { |
| 85 | + ExprSyntax( |
| 86 | + FunctionCallExprSyntax( |
| 87 | + calledExpression: MemberAccessExprSyntax( |
| 88 | + base: DeclReferenceExprSyntax(baseName: .identifier("Testing")), |
| 89 | + declName: DeclReferenceExprSyntax(baseName: thunkName) |
| 90 | + ), |
| 91 | + leftParen: .leftParenToken(), |
| 92 | + rightParen: .rightParenToken() |
| 93 | + ) { |
| 94 | + LabeledExprSyntax(expression: expr.trimmed) |
| 95 | + } |
| 96 | + ) |
| 97 | +} |
| 98 | + |
| 99 | +/// Apply the given effectful keywords (i.e. `try` and `await`) to an expression |
| 100 | +/// using thunk functions provided by the testing library. |
| 101 | +/// |
| 102 | +/// - Parameters: |
| 103 | +/// - effectfulKeywords: The effectful keywords to apply. |
| 104 | +/// - expr: The expression to apply the keywords and thunk functions to. |
| 105 | +/// |
| 106 | +/// - Returns: A copy of `expr` if no changes are needed, or an expression that |
| 107 | +/// adds the keywords in `effectfulKeywords` to `expr`. |
| 108 | +func applyEffectfulKeywords(_ effectfulKeywords: Set<Keyword>, to expr: some ExprSyntaxProtocol) -> ExprSyntax { |
| 109 | + let originalExpr = expr |
| 110 | + var expr = ExprSyntax(expr) |
| 111 | + |
| 112 | + let needAwait = effectfulKeywords.contains(.await) && !expr.is(AwaitExprSyntax.self) |
| 113 | + let needTry = effectfulKeywords.contains(.try) && !expr.is(TryExprSyntax.self) |
| 114 | + let needUnsafe = effectfulKeywords.contains(.unsafe) && !expr.is(UnsafeExprSyntax.self) |
| 115 | + |
| 116 | + // First, add thunk function calls. |
| 117 | + if needAwait { |
| 118 | + expr = _makeCallToEffectfulThunk(.identifier("__requiringAwait"), passing: expr) |
| 119 | + } |
| 120 | + if needTry { |
| 121 | + expr = _makeCallToEffectfulThunk(.identifier("__requiringTry"), passing: expr) |
| 122 | + } |
| 123 | + if needUnsafe { |
| 124 | + expr = _makeCallToEffectfulThunk(.identifier("__requiringUnsafe"), passing: expr) |
| 125 | + } |
| 126 | + |
| 127 | + // Then add keyword expressions. (We do this separately so we end up writing |
| 128 | + // `try await __r(__r(self))` instead of `try __r(await __r(self))` which is |
| 129 | + // less accepted by the compiler.) |
| 130 | + if needAwait { |
| 131 | + expr = ExprSyntax( |
| 132 | + AwaitExprSyntax( |
| 133 | + awaitKeyword: .keyword(.await).with(\.trailingTrivia, .space), |
| 134 | + expression: expr |
| 135 | + ) |
| 136 | + ) |
| 137 | + } |
| 138 | + if needTry { |
| 139 | + expr = ExprSyntax( |
| 140 | + TryExprSyntax( |
| 141 | + tryKeyword: .keyword(.try).with(\.trailingTrivia, .space), |
| 142 | + expression: expr |
| 143 | + ) |
| 144 | + ) |
| 145 | + } |
| 146 | + if needUnsafe { |
| 147 | + expr = ExprSyntax( |
| 148 | + UnsafeExprSyntax( |
| 149 | + unsafeKeyword: .keyword(.unsafe).with(\.trailingTrivia, .space), |
| 150 | + expression: expr |
| 151 | + ) |
| 152 | + ) |
| 153 | + } |
| 154 | + |
| 155 | + expr.leadingTrivia = originalExpr.leadingTrivia |
| 156 | + expr.trailingTrivia = originalExpr.trailingTrivia |
| 157 | + |
| 158 | + return expr |
| 159 | +} |
0 commit comments