1111//===----------------------------------------------------------------------===//
1212
1313import SwiftDiagnostics
14+ import SwiftOperators
1415@_spi ( MacroExpansion) import SwiftParser
1516import SwiftSyntax
1617import SwiftSyntaxBuilder
@@ -55,7 +56,7 @@ private func expandFreestandingMemberDeclList(
5556 let expanded = try expandFreestandingMacro (
5657 definition: definition,
5758 macroRole: inferFreestandingMacroRole ( definition: definition) ,
58- node: node. detach ( in: context) ,
59+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
5960 in: context,
6061 indentationWidth: indentationWidth
6162 )
@@ -80,7 +81,7 @@ private func expandFreestandingCodeItemList(
8081 let expanded = try expandFreestandingMacro (
8182 definition: definition,
8283 macroRole: inferFreestandingMacroRole ( definition: definition) ,
83- node: node. detach ( in: context) ,
84+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
8485 in: context,
8586 indentationWidth: indentationWidth
8687 )
@@ -108,7 +109,7 @@ private func expandFreestandingExpr(
108109 let expanded = expandFreestandingMacro (
109110 definition: definition,
110111 macroRole: . expression,
111- node: node. detach ( in: context) ,
112+ node: node. detach ( in: context, foldingWith : . standardOperators ) ,
112113 in: context,
113114 indentationWidth: indentationWidth
114115 )
@@ -134,7 +135,7 @@ private func expandMemberMacro(
134135 let expanded = expandAttachedMacro (
135136 definition: definition,
136137 macroRole: . member,
137- attributeNode: attributeNode. detach ( in: context) ,
138+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
138139 declarationNode: attachedTo. detach ( in: context) ,
139140 parentDeclNode: nil ,
140141 extendedType: nil ,
@@ -163,7 +164,7 @@ private func expandMemberAttributeMacro(
163164 let expanded = expandAttachedMacro (
164165 definition: definition,
165166 macroRole: . memberAttribute,
166- attributeNode: attributeNode. detach ( in: context) ,
167+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
167168 declarationNode: member. detach ( in: context) ,
168169 parentDeclNode: declaration. detach ( in: context) ,
169170 extendedType: nil ,
@@ -191,7 +192,7 @@ private func expandPeerMacroMember(
191192 let expanded = expandAttachedMacro (
192193 definition: definition,
193194 macroRole: . peer,
194- attributeNode: attributeNode. detach ( in: context) ,
195+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
195196 declarationNode: attachedTo. detach ( in: context) ,
196197 parentDeclNode: nil ,
197198 extendedType: nil ,
@@ -219,7 +220,7 @@ private func expandPeerMacroCodeItem(
219220 let expanded = expandAttachedMacro (
220221 definition: definition,
221222 macroRole: . peer,
222- attributeNode: attributeNode. detach ( in: context) ,
223+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
223224 declarationNode: attachedTo. detach ( in: context) ,
224225 parentDeclNode: nil ,
225226 extendedType: nil ,
@@ -251,7 +252,7 @@ private func expandAccessorMacroWithoutExistingAccessors(
251252 let expanded = expandAttachedMacro (
252253 definition: definition,
253254 macroRole: . accessor,
254- attributeNode: attributeNode. detach ( in: context) ,
255+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
255256 declarationNode: attachedTo. detach ( in: context) ,
256257 parentDeclNode: nil ,
257258 extendedType: nil ,
@@ -285,7 +286,7 @@ private func expandAccessorMacroWithExistingAccessors(
285286 let expanded = expandAttachedMacro (
286287 definition: definition,
287288 macroRole: . accessor,
288- attributeNode: attributeNode. detach ( in: context) ,
289+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
289290 declarationNode: attachedTo. detach ( in: context) ,
290291 parentDeclNode: nil ,
291292 extendedType: nil ,
@@ -322,7 +323,7 @@ private func expandExtensionMacro(
322323 let expanded = expandAttachedMacro (
323324 definition: definition,
324325 macroRole: . extension,
325- attributeNode: attributeNode. detach ( in: context) ,
326+ attributeNode: attributeNode. detach ( in: context, foldingWith : . standardOperators ) ,
326327 declarationNode: attachedTo. detach ( in: context) ,
327328 parentDeclNode: nil ,
328329 extendedType: extendedType. detach ( in: context) ,
@@ -1011,4 +1012,47 @@ private extension SyntaxProtocol {
10111012
10121013 return self . detached
10131014 }
1015+
1016+ /// Fold operators in this node using the given operator table, detach the
1017+ /// node and inform the macro expansion context, if it needs to know.
1018+ func detach(
1019+ in context: MacroExpansionContext ,
1020+ foldingWith operatorTable: OperatorTable ?
1021+ ) -> Syntax {
1022+ let folded : Syntax
1023+ if let operatorTable {
1024+ if let basicContext = context as? BasicMacroExpansionContext {
1025+ folded = basicContext. foldAllOperators ( of: self , with: operatorTable)
1026+ } else {
1027+ folded = operatorTable. foldAll ( self , errorHandler: { _ in /*ignore*/ } )
1028+ }
1029+ } else {
1030+ folded = Syntax ( self )
1031+ }
1032+ return folded. detach ( in: context)
1033+ }
1034+ }
1035+
1036+ private extension FreestandingMacroExpansionSyntax {
1037+ /// Same as `SyntaxProtocol.detach(in:foldingWith:)` but returns a node of type
1038+ /// `Self` since we know that operator folding doesn't change the type of any
1039+ /// `FreestandingMacroExpansionSyntax`.
1040+ func detach(
1041+ in context: MacroExpansionContext ,
1042+ foldingWith operatorTable: OperatorTable ?
1043+ ) -> Self {
1044+ return ( detach ( in: context, foldingWith: operatorTable) as Syntax ) . cast ( Self . self)
1045+ }
1046+ }
1047+
1048+ private extension AttributeSyntax {
1049+ /// Same as `SyntaxProtocol.detach(in:foldingWith:)` but returns a node of type
1050+ /// `Self` since we know that operator folding doesn't change the type of any
1051+ /// `AttributeSyntax`.
1052+ func detach(
1053+ in context: MacroExpansionContext ,
1054+ foldingWith operatorTable: OperatorTable ?
1055+ ) -> Self {
1056+ return ( detach ( in: context, foldingWith: operatorTable) as Syntax ) . cast ( Self . self)
1057+ }
10141058}
0 commit comments