|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2022-2023 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 SwiftSyntax |
| 14 | +import SwiftSyntaxMacros |
| 15 | +import SwiftDiagnostics |
| 16 | + |
| 17 | +// TODO: docs |
| 18 | +public enum TaskLocalMacro {} |
| 19 | + |
| 20 | +extension TaskLocalMacro: PeerMacro { |
| 21 | + public static func expansion( |
| 22 | + of node: AttributeSyntax, |
| 23 | + providingPeersOf declaration: some DeclSyntaxProtocol, |
| 24 | + in context: some MacroExpansionContext |
| 25 | + ) throws -> [DeclSyntax] { |
| 26 | + guard let varDecl = try requireVar(declaration, diagnose: false) else { |
| 27 | + return [] |
| 28 | + } |
| 29 | + guard try requireModifier(varDecl, .static, diagnose: false) else { |
| 30 | + return [] |
| 31 | + } |
| 32 | + |
| 33 | + guard let firstBinding = varDecl.bindings.first else { |
| 34 | + return [] // TODO: make error |
| 35 | + } |
| 36 | + |
| 37 | + guard let name = firstBinding.pattern.as(IdentifierPatternSyntax.self)?.identifier else { |
| 38 | + return [] // TODO: make error |
| 39 | + } |
| 40 | + |
| 41 | + let type = firstBinding.typeAnnotation?.type |
| 42 | + let explicitType: String = |
| 43 | + if let type { |
| 44 | + ": TaskLocal<\(type.trimmed)>" |
| 45 | + } else { |
| 46 | + "" |
| 47 | + } |
| 48 | + |
| 49 | + let initialValue: Any |
| 50 | + if let initializerValue = firstBinding.initializer?.value { |
| 51 | + initialValue = initializerValue |
| 52 | + } else if let type, type.isOptional { |
| 53 | + initialValue = "nil" |
| 54 | + } else { |
| 55 | + throw DiagnosticsError( |
| 56 | + syntax: declaration, |
| 57 | + message: "'@TaskLocal' property must have default value, or be optional", id: .mustBeVar) |
| 58 | + } |
| 59 | + |
| 60 | + return [ |
| 61 | + """ |
| 62 | + static let $\(name)\(raw: explicitType) = TaskLocal(wrappedValue: \(raw: initialValue)) |
| 63 | + """ |
| 64 | + ] |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +extension TaskLocalMacro: AccessorMacro { |
| 69 | + public static func expansion( |
| 70 | + of node: AttributeSyntax, |
| 71 | + providingAccessorsOf declaration: some DeclSyntaxProtocol, |
| 72 | + in context: some MacroExpansionContext |
| 73 | + ) throws -> [AccessorDeclSyntax] { |
| 74 | + // We very specifically have to fail and diagnose in the accessor macro, |
| 75 | + // rather than in the peer macro, since returning [] from the accessor |
| 76 | + // macro adds another series of errors about it missing to emit a decl. |
| 77 | + guard let varDecl = try requireVar(declaration) else { |
| 78 | + return [] |
| 79 | + } |
| 80 | + try requireModifier(varDecl, .static) |
| 81 | + |
| 82 | + guard let firstBinding = varDecl.bindings.first else { |
| 83 | + return [] // TODO: make error |
| 84 | + } |
| 85 | + |
| 86 | + guard let name = firstBinding.pattern.as(IdentifierPatternSyntax.self)?.identifier else { |
| 87 | + return [] // TODO: make error |
| 88 | + } |
| 89 | + |
| 90 | + return ["get { $\(name).get() }"] |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +@discardableResult |
| 95 | +private func requireVar(_ decl: some DeclSyntaxProtocol, |
| 96 | + diagnose: Bool = true) throws -> VariableDeclSyntax? { |
| 97 | + if let varDecl = decl.as(VariableDeclSyntax.self) { |
| 98 | + return varDecl |
| 99 | + } |
| 100 | + if diagnose { |
| 101 | + throw DiagnosticsError( |
| 102 | + syntax: decl, |
| 103 | + message: "'@TaskLocal' can only be applied to properties", id: .mustBeVar) |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +@discardableResult |
| 110 | +private func requireModifier(_ decl: VariableDeclSyntax, |
| 111 | + _ keyword: Keyword, |
| 112 | + diagnose: Bool = true) throws -> Bool { |
| 113 | + let isStatic = decl.modifiers.contains { modifier in |
| 114 | + modifier.name.text == "\(keyword)" |
| 115 | + } |
| 116 | + |
| 117 | + if !isStatic { |
| 118 | + if diagnose { |
| 119 | + throw DiagnosticsError( |
| 120 | + syntax: decl, |
| 121 | + message: "'@TaskLocal' can only be applied to 'static' property", id: .mustBeStatic) |
| 122 | + } else { |
| 123 | + return false |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + return true |
| 128 | +} |
| 129 | + |
| 130 | +extension TypeSyntax { |
| 131 | + // This isn't great since we can't handle type aliases since the macro |
| 132 | + // has no type information, but at least for the common case for Optional<T> |
| 133 | + // and T? we can detect the optional. |
| 134 | + fileprivate var isOptional: Bool { |
| 135 | + let strRepr = "\(self)" |
| 136 | + return strRepr.last == "?" || |
| 137 | + strRepr.starts(with: "Optional<") || |
| 138 | + strRepr.starts(with: "Swift.Optional<") |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +struct TaskLocalMacroDiagnostic: DiagnosticMessage { |
| 143 | + enum ID: String { |
| 144 | + case mustBeStatic = "must be static" |
| 145 | + case mustBeVar = "must be var" |
| 146 | + } |
| 147 | + |
| 148 | + var message: String |
| 149 | + var diagnosticID: MessageID |
| 150 | + var severity: DiagnosticSeverity |
| 151 | + |
| 152 | + init(message: String, diagnosticID: SwiftDiagnostics.MessageID, severity: SwiftDiagnostics.DiagnosticSeverity = .error) { |
| 153 | + self.message = message |
| 154 | + self.diagnosticID = diagnosticID |
| 155 | + self.severity = severity |
| 156 | + } |
| 157 | + |
| 158 | + init(message: String, domain: String, id: ID, severity: SwiftDiagnostics.DiagnosticSeverity = .error) { |
| 159 | + self.message = message |
| 160 | + self.diagnosticID = MessageID(domain: domain, id: id.rawValue) |
| 161 | + self.severity = severity |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +extension DiagnosticsError { |
| 166 | + init<S: SyntaxProtocol>( |
| 167 | + syntax: S, |
| 168 | + message: String, |
| 169 | + domain: String = "Swift", |
| 170 | + id: TaskLocalMacroDiagnostic.ID, |
| 171 | + severity: SwiftDiagnostics.DiagnosticSeverity = .error) { |
| 172 | + self.init(diagnostics: [ |
| 173 | + Diagnostic( |
| 174 | + node: Syntax(syntax), |
| 175 | + message: TaskLocalMacroDiagnostic( |
| 176 | + message: message, |
| 177 | + domain: domain, |
| 178 | + id: id, |
| 179 | + severity: severity)) |
| 180 | + ]) |
| 181 | + } |
| 182 | +} |
0 commit comments