11
11
//===----------------------------------------------------------------------===//
12
12
13
13
import SwiftDiagnostics
14
+ import SwiftOperators
14
15
import SwiftSyntax
15
16
import SwiftSyntaxBuilder
16
17
@_spi ( MacroExpansion) import SwiftParser
@@ -55,7 +56,7 @@ private func expandFreestandingMemberDeclList(
55
56
let expanded = try expandFreestandingMacro (
56
57
definition: definition,
57
58
macroRole: inferFreestandingMacroRole ( definition: definition) ,
58
- node: node. detach ( in: context) ,
59
+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
59
60
in: context,
60
61
indentationWidth: indentationWidth
61
62
)
@@ -80,7 +81,7 @@ private func expandFreestandingCodeItemList(
80
81
let expanded = try expandFreestandingMacro (
81
82
definition: definition,
82
83
macroRole: inferFreestandingMacroRole ( definition: definition) ,
83
- node: node. detach ( in: context) ,
84
+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
84
85
in: context,
85
86
indentationWidth: indentationWidth
86
87
)
@@ -108,7 +109,7 @@ private func expandFreestandingExpr(
108
109
let expanded = expandFreestandingMacro (
109
110
definition: definition,
110
111
macroRole: . expression,
111
- node: node. detach ( in: context) ,
112
+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
112
113
in: context,
113
114
indentationWidth: indentationWidth
114
115
)
@@ -134,7 +135,7 @@ private func expandMemberMacro(
134
135
let expanded = expandAttachedMacro (
135
136
definition: definition,
136
137
macroRole: . member,
137
- attributeNode: attributeNode. detach ( in: context) ,
138
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
138
139
declarationNode: attachedTo. detach ( in: context) ,
139
140
parentDeclNode: nil ,
140
141
extendedType: nil ,
@@ -163,7 +164,7 @@ private func expandMemberAttributeMacro(
163
164
let expanded = expandAttachedMacro (
164
165
definition: definition,
165
166
macroRole: . memberAttribute,
166
- attributeNode: attributeNode. detach ( in: context) ,
167
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
167
168
declarationNode: member. detach ( in: context) ,
168
169
parentDeclNode: declaration. detach ( in: context) ,
169
170
extendedType: nil ,
@@ -191,7 +192,7 @@ private func expandPeerMacroMember(
191
192
let expanded = expandAttachedMacro (
192
193
definition: definition,
193
194
macroRole: . peer,
194
- attributeNode: attributeNode. detach ( in: context) ,
195
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
195
196
declarationNode: attachedTo. detach ( in: context) ,
196
197
parentDeclNode: nil ,
197
198
extendedType: nil ,
@@ -219,7 +220,7 @@ private func expandPeerMacroCodeItem(
219
220
let expanded = expandAttachedMacro (
220
221
definition: definition,
221
222
macroRole: . peer,
222
- attributeNode: attributeNode. detach ( in: context) ,
223
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
223
224
declarationNode: attachedTo. detach ( in: context) ,
224
225
parentDeclNode: nil ,
225
226
extendedType: nil ,
@@ -251,7 +252,7 @@ private func expandAccessorMacroWithoutExistingAccessors(
251
252
let expanded = expandAttachedMacro (
252
253
definition: definition,
253
254
macroRole: . accessor,
254
- attributeNode: attributeNode. detach ( in: context) ,
255
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
255
256
declarationNode: attachedTo. detach ( in: context) ,
256
257
parentDeclNode: nil ,
257
258
extendedType: nil ,
@@ -285,7 +286,7 @@ private func expandAccessorMacroWithExistingAccessors(
285
286
let expanded = expandAttachedMacro (
286
287
definition: definition,
287
288
macroRole: . accessor,
288
- attributeNode: attributeNode. detach ( in: context) ,
289
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
289
290
declarationNode: attachedTo. detach ( in: context) ,
290
291
parentDeclNode: nil ,
291
292
extendedType: nil ,
@@ -322,7 +323,7 @@ private func expandExtensionMacro(
322
323
let expanded = expandAttachedMacro (
323
324
definition: definition,
324
325
macroRole: . extension,
325
- attributeNode: attributeNode. detach ( in: context) ,
326
+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
326
327
declarationNode: attachedTo. detach ( in: context) ,
327
328
parentDeclNode: nil ,
328
329
extendedType: extendedType. detach ( in: context) ,
@@ -1007,4 +1008,47 @@ private extension SyntaxProtocol {
1007
1008
1008
1009
return self . detached
1009
1010
}
1011
+
1012
+ /// Fold operators in this node using the given operator table, detach the
1013
+ /// node and inform the macro expansion context, if it needs to know.
1014
+ func detach(
1015
+ in context: MacroExpansionContext ,
1016
+ foldingWith operatorTable: OperatorTable ?
1017
+ ) -> Syntax {
1018
+ let folded : Syntax
1019
+ if let operatorTable {
1020
+ if let basicContext = context as? BasicMacroExpansionContext {
1021
+ folded = basicContext. foldAllOperators ( of: self , with: operatorTable)
1022
+ } else {
1023
+ folded = operatorTable. foldAll ( self , errorHandler: { _ in /*ignore*/ } )
1024
+ }
1025
+ } else {
1026
+ folded = Syntax ( self )
1027
+ }
1028
+ return folded. detach ( in: context)
1029
+ }
1030
+ }
1031
+
1032
+ private extension FreestandingMacroExpansionSyntax {
1033
+ /// Same as `SyntaxProtocol.detach(in:foldingWith:)` but returns a node of type
1034
+ /// `Self` since we know that operator folding doesn't change the type of any
1035
+ /// `FreestandingMacroExpansionSyntax`.
1036
+ func detach(
1037
+ in context: MacroExpansionContext ,
1038
+ foldingWith operatorTable: OperatorTable ?
1039
+ ) -> Self {
1040
+ return ( detach ( in: context, foldingWith: operatorTable) as Syntax ) . cast ( Self . self)
1041
+ }
1042
+ }
1043
+
1044
+ private extension AttributeSyntax {
1045
+ /// Same as `SyntaxProtocol.detach(in:foldingWith:)` but returns a node of type
1046
+ /// `Self` since we know that operator folding doesn't change the type of any
1047
+ /// `AttributeSyntax`.
1048
+ func detach(
1049
+ in context: MacroExpansionContext ,
1050
+ foldingWith operatorTable: OperatorTable ?
1051
+ ) -> Self {
1052
+ return ( detach ( in: context, foldingWith: operatorTable) as Syntax ) . cast ( Self . self)
1053
+ }
1010
1054
}
0 commit comments