-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathTaskMacro.swift
126 lines (110 loc) · 3.49 KB
/
TaskMacro.swift
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2025 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
//
//===----------------------------------------------------------------------===//
import SwiftDiagnostics
import SwiftParser
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
extension MacroExpansionContext {
func diagnose(
_ diag: TaskMacroDiagnostic,
at node: some SyntaxProtocol
) {
diagnose(Diagnostic(
node: Syntax(node),
message: diag
))
}
}
enum TaskMacroDiagnostic: String, DiagnosticMessage {
case noImplementation
= "'@Task' macro can only be used on functions with an implementation"
case unsupportedGlobalActor
= "'@Task' global actor must be written 'GlobalActorType'.shared"
var message: String { rawValue }
var severity: DiagnosticSeverity { .error }
var diagnosticID: MessageID {
MessageID(domain: "_Concurrency", id: "TaskMacro.\(self)")
}
}
public struct TaskMacro: BodyMacro {
public static func expansion(
of node: AttributeSyntax,
statements: CodeBlockItemListSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
var globalActor: TokenSyntax? = nil
var argumentList: LabeledExprListSyntax = []
if case .argumentList(let arguments) = node.arguments {
if let actor = arguments.first(labeled: "on") {
guard let member = actor.expression.as(MemberAccessExprSyntax.self),
let declRef = member.base?.as(DeclReferenceExprSyntax.self) else {
context.diagnose(.unsupportedGlobalActor, at: actor)
return []
}
argumentList = LabeledExprListSyntax(arguments.dropFirst())
globalActor = declRef.baseName
} else {
argumentList = arguments
}
}
let signature: ClosureSignatureSyntax? =
if let globalActor {
.init(attributes: "@\(globalActor) ")
} else {
nil
}
let parens: (left: TokenSyntax, right: TokenSyntax)? =
if !argumentList.isEmpty {
(.leftParenToken(), .rightParenToken())
} else {
nil
}
let taskInit = FunctionCallExprSyntax(
calledExpression: DeclReferenceExprSyntax(
baseName: "Task"
),
leftParen: parens?.left,
arguments: argumentList,
rightParen: parens?.right,
trailingClosure: ClosureExprSyntax(
signature: signature,
statements: statements
)
)
return ["\(taskInit)"]
}
public static func expansion(
of node: AttributeSyntax,
providingBodyFor declaration: some DeclSyntaxProtocol & WithOptionalCodeBlockSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
guard let taskBody = declaration.body else {
context.diagnose(.noImplementation, at: node)
return []
}
return try expansion(
of: node,
statements: taskBody.statements,
in: context)
}
public static func expansion(
of node: AttributeSyntax,
providingBodyFor closure: ClosureExprSyntax,
in context: some MacroExpansionContext
) throws -> [CodeBlockItemSyntax] {
try expansion(
of: node,
statements: closure.statements,
in: context)
}
}