|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import SwiftDiagnostics |
| 14 | +import SwiftParser |
| 15 | +import SwiftSyntax |
| 16 | +import SwiftSyntaxBuilder |
| 17 | +import SwiftSyntaxMacros |
| 18 | + |
| 19 | +extension MacroExpansionContext { |
| 20 | + func diagnose( |
| 21 | + _ diag: TaskMacroDiagnostic, |
| 22 | + at node: some SyntaxProtocol |
| 23 | + ) { |
| 24 | + diagnose(Diagnostic( |
| 25 | + node: Syntax(node), |
| 26 | + message: diag |
| 27 | + )) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +enum TaskMacroDiagnostic: String, DiagnosticMessage { |
| 32 | + case noImplementation |
| 33 | + = "'@Task' macro can only be used on functions with an implementation" |
| 34 | + case unsupportedGlobalActor |
| 35 | + = "'@Task' global actor must be written 'GlobalActorType'.shared" |
| 36 | + |
| 37 | + var message: String { rawValue } |
| 38 | + |
| 39 | + var severity: DiagnosticSeverity { .error } |
| 40 | + |
| 41 | + var diagnosticID: MessageID { |
| 42 | + MessageID(domain: "_Concurrency", id: "TaskMacro.\(self)") |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +public struct TaskMacro: BodyMacro { |
| 48 | + public static func expansion( |
| 49 | + of node: AttributeSyntax, |
| 50 | + statements: CodeBlockItemListSyntax, |
| 51 | + in context: some MacroExpansionContext |
| 52 | + ) throws -> [CodeBlockItemSyntax] { |
| 53 | + var globalActor: TokenSyntax? = nil |
| 54 | + var argumentList: LabeledExprListSyntax = [] |
| 55 | + if case .argumentList(let arguments) = node.arguments { |
| 56 | + if let actor = arguments.first(labeled: "on") { |
| 57 | + guard let member = actor.expression.as(MemberAccessExprSyntax.self), |
| 58 | + let declRef = member.base?.as(DeclReferenceExprSyntax.self) else { |
| 59 | + context.diagnose(.unsupportedGlobalActor, at: actor) |
| 60 | + return [] |
| 61 | + } |
| 62 | + |
| 63 | + argumentList = LabeledExprListSyntax(arguments.dropFirst()) |
| 64 | + globalActor = declRef.baseName |
| 65 | + } else { |
| 66 | + argumentList = arguments |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + let signature: ClosureSignatureSyntax? = |
| 71 | + if let globalActor { |
| 72 | + .init(attributes: "@\(globalActor) ") |
| 73 | + } else { |
| 74 | + nil |
| 75 | + } |
| 76 | + |
| 77 | + let parens: (left: TokenSyntax, right: TokenSyntax)? = |
| 78 | + if !argumentList.isEmpty { |
| 79 | + (.leftParenToken(), .rightParenToken()) |
| 80 | + } else { |
| 81 | + nil |
| 82 | + } |
| 83 | + |
| 84 | + let taskInit = FunctionCallExprSyntax( |
| 85 | + calledExpression: DeclReferenceExprSyntax( |
| 86 | + baseName: "Task" |
| 87 | + ), |
| 88 | + leftParen: parens?.left, |
| 89 | + arguments: argumentList, |
| 90 | + rightParen: parens?.right, |
| 91 | + trailingClosure: ClosureExprSyntax( |
| 92 | + signature: signature, |
| 93 | + statements: statements |
| 94 | + ) |
| 95 | + ) |
| 96 | + |
| 97 | + return ["\(taskInit)"] |
| 98 | + } |
| 99 | + |
| 100 | + public static func expansion( |
| 101 | + of node: AttributeSyntax, |
| 102 | + providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax, |
| 103 | + in context: some MacroExpansionContext |
| 104 | + ) throws -> [CodeBlockItemSyntax] { |
| 105 | + guard let taskBody = declaration.body else { |
| 106 | + context.diagnose(.noImplementation, at: node) |
| 107 | + return [] |
| 108 | + } |
| 109 | + |
| 110 | + return try expansion( |
| 111 | + of: node, |
| 112 | + statements: taskBody.statements, |
| 113 | + in: context) |
| 114 | + } |
| 115 | + |
| 116 | + public static func expansion( |
| 117 | + of node: AttributeSyntax, |
| 118 | + providingBodyFor closure: ClosureExprSyntax, |
| 119 | + in context: some MacroExpansionContext |
| 120 | + ) throws -> [CodeBlockItemSyntax] { |
| 121 | + try expansion( |
| 122 | + of: node, |
| 123 | + statements: closure.statements, |
| 124 | + in: context) |
| 125 | + } |
| 126 | +} |
0 commit comments