-
Notifications
You must be signed in to change notification settings - Fork 433
/
Copy pathOptionSetMacro.swift
220 lines (191 loc) · 6.84 KB
/
OptionSetMacro.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
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
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
import SwiftDiagnostics
import SwiftSyntax
import SwiftSyntaxBuilder
import SwiftSyntaxMacros
enum OptionSetMacroDiagnostic {
case requiresStruct
case requiresStringLiteral(String)
case requiresOptionsEnum(String)
case requiresOptionsEnumRawType
}
extension OptionSetMacroDiagnostic: DiagnosticMessage {
func diagnose(at node: some SyntaxProtocol) -> Diagnostic {
Diagnostic(node: Syntax(node), message: self)
}
var message: String {
switch self {
case .requiresStruct:
return "'OptionSet' macro can only be applied to a struct"
case .requiresStringLiteral(let name):
return "'OptionSet' macro argument \(name) must be a string literal"
case .requiresOptionsEnum(let name):
return "'OptionSet' macro requires nested options enum '\(name)'"
case .requiresOptionsEnumRawType:
return "'OptionSet' macro requires a raw type"
}
}
var severity: DiagnosticSeverity { .error }
var diagnosticID: MessageID {
MessageID(domain: "Swift", id: "OptionSet.\(self)")
}
}
/// The label used for the OptionSet macro argument that provides the name of
/// the nested options enum.
private let optionsEnumNameArgumentLabel = "optionsName"
/// The default name used for the nested "Options" enum. This should
/// eventually be overridable.
private let defaultOptionsEnumName = "Options"
extension LabeledExprListSyntax {
/// Retrieve the first element with the given label.
func first(labeled name: String) -> Element? {
return first { element in
if let label = element.label, label.text == name {
return true
}
return false
}
}
}
public struct OptionSetMacro {
/// Decodes the arguments to the macro expansion.
///
/// - Returns: the important arguments used by the various roles of this
/// macro inhabits, or nil if an error occurred.
static func decodeExpansion(
of attribute: AttributeSyntax,
attachedTo decl: some DeclGroupSyntax,
in context: some MacroExpansionContext,
emitDiagnostics: Bool
) -> (StructDeclSyntax, EnumDeclSyntax, TypeSyntax)? {
// Determine the name of the options enum.
let optionsEnumName: String
if case .argumentList(let arguments) = attribute.arguments,
let optionEnumNameArg = arguments.first(labeled: optionsEnumNameArgumentLabel)
{
// We have a options name; make sure it is a string literal.
guard let stringLiteral = optionEnumNameArg.expression.as(StringLiteralExprSyntax.self),
stringLiteral.segments.count == 1,
case .stringSegment(let optionsEnumNameString)? = stringLiteral.segments.first
else {
if emitDiagnostics {
context.diagnose(
OptionSetMacroDiagnostic.requiresStringLiteral(optionsEnumNameArgumentLabel).diagnose(
at: optionEnumNameArg.expression
)
)
}
return nil
}
optionsEnumName = optionsEnumNameString.content.text
} else {
optionsEnumName = defaultOptionsEnumName
}
// Only apply to structs.
guard let structDecl = decl.as(StructDeclSyntax.self) else {
if emitDiagnostics {
context.diagnose(OptionSetMacroDiagnostic.requiresStruct.diagnose(at: decl))
}
return nil
}
// Find the option enum within the struct.
guard
let optionsEnum = decl.memberBlock.members.compactMap({ member in
if let enumDecl = member.decl.as(EnumDeclSyntax.self),
enumDecl.name.text == optionsEnumName
{
return enumDecl
}
return nil
}).first
else {
if emitDiagnostics {
context.diagnose(OptionSetMacroDiagnostic.requiresOptionsEnum(optionsEnumName).diagnose(at: decl))
}
return nil
}
// Retrieve the raw type from the attribute.
guard let genericArgs = attribute.attributeName.as(IdentifierTypeSyntax.self)?.genericArgumentClause,
let rawType = genericArgs.arguments.first?.argument
else {
if emitDiagnostics {
context.diagnose(OptionSetMacroDiagnostic.requiresOptionsEnumRawType.diagnose(at: attribute))
}
return nil
}
return (structDecl, optionsEnum, rawType)
}
}
extension OptionSetMacro: ExtensionMacro {
public static func expansion(
of node: AttributeSyntax,
attachedTo declaration: some DeclGroupSyntax,
providingExtensionsOf type: some TypeSyntaxProtocol,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [ExtensionDeclSyntax] {
// Decode the expansion arguments.
guard
let (structDecl, _, _) = decodeExpansion(of: node, attachedTo: declaration, in: context, emitDiagnostics: false)
else {
return []
}
// If there is an explicit conformance to OptionSet already, don't add one.
if let inheritedTypes = structDecl.inheritanceClause?.inheritedTypes,
inheritedTypes.contains(where: { inherited in inherited.type.trimmedDescription == "OptionSet" })
{
return []
}
return [try ExtensionDeclSyntax("extension \(type): OptionSet {}")]
}
}
extension OptionSetMacro: MemberMacro {
public static func expansion(
of attribute: AttributeSyntax,
providingMembersOf decl: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
// Decode the expansion arguments.
guard
let (_, optionsEnum, rawType) = decodeExpansion(
of: attribute,
attachedTo: decl,
in: context,
emitDiagnostics: true
)
else {
return []
}
// Find all of the case elements.
let caseElements: [EnumCaseElementSyntax] = optionsEnum.memberBlock.members.flatMap { member in
guard let caseDecl = member.decl.as(EnumCaseDeclSyntax.self) else {
return Array<EnumCaseElementSyntax>()
}
return Array(caseDecl.elements)
}
// Dig out the access control keyword we need.
let access = decl.modifiers.first(where: \.isNeededAccessLevelModifier)
let staticVars = caseElements.map { (element) -> DeclSyntax in
"""
\(access) static let \(element.name): Self =
Self(rawValue: 1 << \(optionsEnum.name).\(element.name).rawValue)
"""
}
return [
"\(access)typealias RawValue = \(rawType)",
"\(access)var rawValue: RawValue",
"\(access)init() { self.rawValue = 0 }",
"\(access)init(rawValue: RawValue) { self.rawValue = rawValue }",
] + staticVars
}
}