@@ -46,7 +46,7 @@ import SwiftSyntax
4646 }
4747
4848 /// The name of the implicit declaration.
49- private var name : String {
49+ private var name : StaticString {
5050 switch self {
5151 case . self :
5252 return " self "
@@ -86,17 +86,38 @@ import SwiftSyntax
8686 /// `self` and `Self` identifers override implicit `self` and `Self` introduced by
8787 /// the `Foo` class declaration.
8888 var identifier : Identifier {
89+ Identifier ( name)
90+ }
91+
92+ /// Position of this implicit name.
93+ @_spi ( Experimental) public var position : AbsolutePosition {
8994 switch self {
90- case . self :
91- return Identifier ( " self " )
92- case . Self:
93- return Identifier ( " Self " )
94- case . error:
95- return Identifier ( " error " )
96- case . newValue:
97- return Identifier ( " newValue " )
98- case . oldValue:
99- return Identifier ( " oldValue " )
95+ case . self ( let declSyntax) :
96+ switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
97+ case . functionDecl( let functionDecl) :
98+ return functionDecl. name. positionAfterSkippingLeadingTrivia
99+ case . initializerDecl( let initializerDecl) :
100+ return initializerDecl. initKeyword. positionAfterSkippingLeadingTrivia
101+ case . subscriptDecl( let subscriptDecl) :
102+ return subscriptDecl. accessorBlock? . positionAfterSkippingLeadingTrivia
103+ ?? subscriptDecl. endPositionBeforeTrailingTrivia
104+ case . variableDecl( let variableDecl) :
105+ return variableDecl. bindings. first? . accessorBlock? . positionAfterSkippingLeadingTrivia
106+ ?? variableDecl. endPosition
107+ default :
108+ return declSyntax. positionAfterSkippingLeadingTrivia
109+ }
110+ case . Self( let declSyntax) :
111+ switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
112+ case . protocolDecl( let protocolDecl) :
113+ return protocolDecl. name. positionAfterSkippingLeadingTrivia
114+ default :
115+ return declSyntax. positionAfterSkippingLeadingTrivia
116+ }
117+ case . error( let catchClause) :
118+ return catchClause. catchItems. positionAfterSkippingLeadingTrivia
119+ default :
120+ return syntax. positionAfterSkippingLeadingTrivia
100121 }
101122 }
102123}
@@ -110,6 +131,8 @@ import SwiftSyntax
110131 case declaration( NamedDeclSyntax )
111132 /// Name introduced implicitly by certain syntax nodes.
112133 case implicit( ImplicitDecl )
134+ /// Dollar identifier introduced by a closure without parameters.
135+ case dollarIdentifier( ClosureExprSyntax , strRepresentation: String )
113136
114137 /// Syntax associated with this name.
115138 @_spi ( Experimental) public var syntax : SyntaxProtocol {
@@ -120,6 +143,8 @@ import SwiftSyntax
120143 return syntax
121144 case . implicit( let implicitName) :
122145 return implicitName. syntax
146+ case . dollarIdentifier( let closureExpr, _) :
147+ return closureExpr
123148 }
124149 }
125150
@@ -132,6 +157,8 @@ import SwiftSyntax
132157 return Identifier ( syntax. name)
133158 case . implicit( let kind) :
134159 return kind. identifier
160+ case . dollarIdentifier( _, strRepresentation: _) :
161+ return nil
135162 }
136163 }
137164
@@ -149,34 +176,9 @@ import SwiftSyntax
149176 case . declaration( let syntax) :
150177 return syntax. name. positionAfterSkippingLeadingTrivia
151178 case . implicit( let implicitName) :
152- switch implicitName {
153- case . self ( let declSyntax) :
154- switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
155- case . functionDecl( let functionDecl) :
156- return functionDecl. name. positionAfterSkippingLeadingTrivia
157- case . initializerDecl( let initializerDecl) :
158- return initializerDecl. initKeyword. positionAfterSkippingLeadingTrivia
159- case . subscriptDecl( let subscriptDecl) :
160- return subscriptDecl. accessorBlock? . positionAfterSkippingLeadingTrivia
161- ?? subscriptDecl. endPositionBeforeTrailingTrivia
162- case . variableDecl( let variableDecl) :
163- return variableDecl. bindings. first? . accessorBlock? . positionAfterSkippingLeadingTrivia
164- ?? variableDecl. endPosition
165- default :
166- return declSyntax. positionAfterSkippingLeadingTrivia
167- }
168- case . Self( let declSyntax) :
169- switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
170- case . protocolDecl( let protocolDecl) :
171- return protocolDecl. name. positionAfterSkippingLeadingTrivia
172- default :
173- return declSyntax. positionAfterSkippingLeadingTrivia
174- }
175- case . error( let catchClause) :
176- return catchClause. body. positionAfterSkippingLeadingTrivia
177- default :
178- return implicitName. syntax. positionAfterSkippingLeadingTrivia
179- }
179+ return implicitName. position
180+ case . dollarIdentifier( let closureExpr, _) :
181+ return closureExpr. positionAfterSkippingLeadingTrivia
180182 }
181183 }
182184
@@ -197,6 +199,17 @@ import SwiftSyntax
197199 return accessibleAfter <= lookUpPosition
198200 }
199201
202+ func refersTo( _ otherIdentifier: Identifier ? ) -> Bool {
203+ guard let otherIdentifier else { return true }
204+
205+ switch self {
206+ case . dollarIdentifier( _, let strRepresentation) :
207+ return strRepresentation == otherIdentifier. name
208+ default :
209+ return identifier == otherIdentifier
210+ }
211+ }
212+
200213 /// Extracts names introduced by the given `syntax` structure.
201214 ///
202215 /// When e.g. looking up a variable declaration like `let a = a`,
@@ -221,6 +234,12 @@ import SwiftSyntax
221234 return tuplePattern. elements. flatMap { tupleElement in
222235 getNames ( from: tupleElement. pattern, accessibleAfter: accessibleAfter)
223236 }
237+ case . tupleExpr( let tupleExpr) :
238+ return tupleExpr. elements. flatMap { tupleElement in
239+ getNames ( from: tupleElement, accessibleAfter: accessibleAfter)
240+ }
241+ case . labeledExpr( let labeledExpr) :
242+ return getNames ( from: labeledExpr. expression, accessibleAfter: accessibleAfter)
224243 case . valueBindingPattern( let valueBindingPattern) :
225244 return getNames ( from: valueBindingPattern. pattern, accessibleAfter: accessibleAfter)
226245 case . expressionPattern( let expressionPattern) :
@@ -288,6 +307,8 @@ import SwiftSyntax
288307 return " declaration: \( strName) "
289308 case . implicit:
290309 return " implicit: \( strName) "
310+ case . dollarIdentifier( _, strRepresentation: let str) :
311+ return " dollarIdentifier: \( str) "
291312 }
292313 }
293314}
0 commit comments