@@ -13,11 +13,23 @@ public enum MacroRole {
13
13
}
14
14
15
15
/// Simple diagnostic message
16
- private struct MacroExpansionError : Error , CustomStringConvertible {
17
- let description : String
16
+ private enum MacroExpansionError : String , Error , CustomStringConvertible {
17
+ case unmathedMacroRole = " macro doesn't conform to required macro role "
18
+ case parentDeclGroupNil = " parent decl group is nil "
19
+ case declarationNotDeclGroup = " declaration is not a decl group syntax "
20
+ case declarationNotIdentified = " declaration is not a 'Identified' syntax "
21
+ var description : String { self . rawValue }
18
22
}
19
23
20
24
/// Expand `@freestanding(XXX)` macros.
25
+ ///
26
+ /// - Parameters:
27
+ /// - definition: a type conforms to one of freestanding `Macro` protocol.
28
+ /// - node: macro expansion syntax node (e.g. `#macroName(argument)`).
29
+ /// - in: context of the expansion.
30
+ /// - Returns: expanded source text. Upon failure (i.e. `defintion.expansion()`
31
+ /// throws) returns `nil`, and the diagnostics representing the `Error` are
32
+ /// guaranteed to be added to context.
21
33
public func expandFreestandingMacro(
22
34
definition: Macro . Type ,
23
35
node: FreestandingMacroExpansionSyntax ,
@@ -51,7 +63,7 @@ public func expandFreestandingMacro(
51
63
expandedSyntax = Syntax ( CodeBlockItemListSyntax ( rewritten) )
52
64
53
65
default :
54
- throw MacroExpansionError ( description : " macro doesn't conform to required macro role " )
66
+ throw MacroExpansionError . unmathedMacroRole
55
67
}
56
68
return expandedSyntax. formattedExpansion ( definition. formatMode)
57
69
}
@@ -63,6 +75,18 @@ public func expandFreestandingMacro(
63
75
}
64
76
65
77
/// Expand `@attached(XXX)` macros.
78
+ ///
79
+ /// - Parameters:
80
+ /// - definition: a type that conforms to one or more attached `Macro` protocols.
81
+ /// - macroRole: indicates which `Macro` protocol expansion should be performed
82
+ /// - attributeNode: attribute syntax node (e.g. `@macroName(argument)`).
83
+ /// - declarationNode: target declaration syntax node to apply the expansion.
84
+ /// - parentDeclNode: Only used for `MacroRole.memberAttribute`. The parent
85
+ /// context node of `declarationNode`.
86
+ /// - in: context of the expansion.
87
+ /// - Returns: A list of expanded source text. Upon failure (i.e.
88
+ /// `defintion.expansion()` throws) returns `nil`, and the diagnostics
89
+ /// representing the `Error` are guaranteed to be added to context.
66
90
public func expandAttachedMacro< Context: MacroExpansionContext > (
67
91
definition: Macro . Type ,
68
92
macroRole: MacroRole ,
@@ -88,7 +112,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
88
112
let parentDeclGroup = parentDeclNode? . asProtocol ( DeclGroupSyntax . self)
89
113
else {
90
114
// Compiler error: 'parentDecl' is mandatory for MemberAttributeMacro.
91
- throw MacroExpansionError ( description : " parent decl group is nil " )
115
+ throw MacroExpansionError . parentDeclGroupNil
92
116
}
93
117
94
118
// Local function to expand a member attribute macro once we've opened up
@@ -118,7 +142,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
118
142
guard let declGroup = declarationNode. asProtocol ( DeclGroupSyntax . self)
119
143
else {
120
144
// Compiler error: declNode for member macro must be DeclGroupSyntax.
121
- throw MacroExpansionError ( description : " declaration is not a decl group syntax " )
145
+ throw MacroExpansionError . declarationNotDeclGroup
122
146
}
123
147
124
148
// Local function to expand a member macro once we've opened up
@@ -151,12 +175,14 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
151
175
}
152
176
153
177
case ( let attachedMacro as ConformanceMacro . Type , . conformance) :
154
- guard
155
- let declGroup = declarationNode. asProtocol ( DeclGroupSyntax . self) ,
156
- let identified = declarationNode. asProtocol ( IdentifiedDeclSyntax . self)
178
+ guard let declGroup = declarationNode. asProtocol ( DeclGroupSyntax . self) else {
179
+ // Compiler error: type mismatch.
180
+ throw MacroExpansionError . declarationNotDeclGroup
181
+ }
182
+ guard let identified = declarationNode. asProtocol ( IdentifiedDeclSyntax . self)
157
183
else {
158
184
// Compiler error: type mismatch.
159
- throw MacroExpansionError ( description : " declaration is not a identified decl group " )
185
+ throw MacroExpansionError . declarationNotIdentified
160
186
}
161
187
162
188
// Local function to expand a conformance macro once we've opened up
@@ -185,7 +211,7 @@ public func expandAttachedMacro<Context: MacroExpansionContext>(
185
211
}
186
212
187
213
default :
188
- throw MacroExpansionError ( description : " macro doesn't conform to required macro role " )
214
+ throw MacroExpansionError . unmathedMacroRole
189
215
}
190
216
} catch {
191
217
context. addDiagnostics ( from: error, node: attributeNode)
0 commit comments