1313import SwiftSyntax
1414
1515/// An entity that is implicitly declared based on the syntactic structure of the program.
16- @ _spi ( Experimental ) public enum ImplicitDecl {
16+ public enum ImplicitDecl {
1717 /// `self` keyword representing object instance.
18- /// Could be associated with type declaration, extension,
19- /// or closure captures. Introduced at function edge.
20- case `self`( DeclSyntaxProtocol )
18+ /// Introduced at member boundary.
19+ /// Associated syntax node could be: `FunctionDeclSyntax`,
20+ /// `AccessorDeclSyntax`, `SubscriptDeclSyntax`,
21+ /// `DeinitializerDeclSyntax`, or `InitializerDeclSyntax`.
22+ case `self`( DeclSyntax )
2123 /// `Self` keyword representing object type.
22- /// Could be associated with type declaration or extension.
23- case `Self`( DeclSyntaxProtocol )
24- /// `error` value caught by a `catch`
24+ /// Associated syntax node could be: `ExtensionDeclSyntax`,
25+ /// or `ProtocolDeclSyntax`.
26+ case `Self`( DeclSyntax )
27+ /// `error` available by default inside `catch`
2528 /// block that does not specify a catch pattern.
2629 case error( CatchClauseSyntax )
2730 /// `newValue` available by default inside `set` and `willSet`.
@@ -30,7 +33,7 @@ import SwiftSyntax
3033 case oldValue( AccessorDeclSyntax )
3134
3235 /// Syntax associated with this name.
33- @ _spi ( Experimental ) public var syntax : SyntaxProtocol {
36+ public var syntax : SyntaxProtocol {
3437 switch self {
3538 case . self ( let syntax) :
3639 return syntax
@@ -46,7 +49,7 @@ import SwiftSyntax
4649 }
4750
4851 /// The name of the implicit declaration.
49- private var name : StaticString {
52+ public var name : StaticString {
5053 switch self {
5154 case . self :
5255 return " self "
@@ -85,12 +88,12 @@ import SwiftSyntax
8588 /// ```
8689 /// `self` and `Self` identifers override implicit `self` and `Self` introduced by
8790 /// the `Foo` class declaration.
88- var identifier : Identifier {
91+ public var identifier : Identifier {
8992 Identifier ( canonicalName: name)
9093 }
9194
9295 /// Position of this implicit name.
93- @ _spi ( Experimental ) public var position : AbsolutePosition {
96+ public var position : AbsolutePosition {
9497 switch self {
9598 case . self ( let declSyntax) :
9699 switch Syntax ( declSyntax) . as ( SyntaxEnum . self) {
@@ -128,17 +131,15 @@ import SwiftSyntax
128131 }
129132}
130133
131- @ _spi ( Experimental ) public enum LookupName {
134+ public enum LookupName {
132135 /// Identifier associated with the name.
133136 /// Could be an identifier of a variable, function or closure parameter and more.
134- case identifier( IdentifiableSyntax , accessibleAfter: AbsolutePosition ? )
137+ case identifier( Syntax , accessibleAfter: AbsolutePosition ? )
135138 /// Declaration associated with the name.
136139 /// Could be class, struct, actor, protocol, function and more.
137- case declaration( NamedDeclSyntax )
140+ case declaration( Syntax )
138141 /// Name introduced implicitly by certain syntax nodes.
139142 case implicit( ImplicitDecl )
140- /// Dollar identifier introduced by a closure without parameters.
141- case dollarIdentifier( ClosureExprSyntax , strRepresentation: String )
142143 /// Represents equivalent names grouped together.
143144 /// - Important: The array should be non-empty.
144145 ///
@@ -154,32 +155,28 @@ import SwiftSyntax
154155 case equivalentNames( [ LookupName ] )
155156
156157 /// Syntax associated with this name.
157- @ _spi ( Experimental ) public var syntax : SyntaxProtocol {
158+ public var syntax : SyntaxProtocol {
158159 switch self {
159160 case . identifier( let syntax, _) :
160161 return syntax
161162 case . declaration( let syntax) :
162163 return syntax
163164 case . implicit( let implicitName) :
164165 return implicitName. syntax
165- case . dollarIdentifier( let closureExpr, _) :
166- return closureExpr
167166 case . equivalentNames( let names) :
168167 return names. first!. syntax
169168 }
170169 }
171170
172171 /// Identifier used for name comparison.
173- @ _spi ( Experimental ) public var identifier : Identifier ? {
172+ public var identifier : Identifier {
174173 switch self {
175174 case . identifier( let syntax, _) :
176- return Identifier ( syntax. identifier)
175+ return Identifier ( ( syntax. asProtocol ( SyntaxProtocol . self ) as! IdentifiableSyntax ) . identifier) !
177176 case . declaration( let syntax) :
178- return Identifier ( syntax. name)
177+ return Identifier ( ( syntax. asProtocol ( SyntaxProtocol . self ) as! NamedDeclSyntax ) . name) !
179178 case . implicit( let kind) :
180179 return kind. identifier
181- case . dollarIdentifier( _, strRepresentation: _) :
182- return nil
183180 case . equivalentNames( let names) :
184181 return names. first!. identifier
185182 }
@@ -192,16 +189,15 @@ import SwiftSyntax
192189 /// Such cases are function parameters (as they can
193190 /// contain two identifiers) and function declarations (where name
194191 /// is precided by access modifiers and `func` keyword).
195- @ _spi ( Experimental ) public var position : AbsolutePosition {
192+ public var position : AbsolutePosition {
196193 switch self {
197194 case . identifier( let syntax, _) :
198- return syntax. identifier. positionAfterSkippingLeadingTrivia
195+ return ( syntax. asProtocol ( SyntaxProtocol . self) as! IdentifiableSyntax ) . identifier
196+ . positionAfterSkippingLeadingTrivia
199197 case . declaration( let syntax) :
200- return syntax. name. positionAfterSkippingLeadingTrivia
198+ return ( syntax. asProtocol ( SyntaxProtocol . self ) as! NamedDeclSyntax ) . name. positionAfterSkippingLeadingTrivia
201199 case . implicit( let implicitName) :
202200 return implicitName. position
203- case . dollarIdentifier( let closureExpr, _) :
204- return closureExpr. positionAfterSkippingLeadingTrivia
205201 case . equivalentNames( let names) :
206202 return names. first!. position
207203 }
@@ -226,13 +222,7 @@ import SwiftSyntax
226222
227223 func refersTo( _ otherIdentifier: Identifier ? ) -> Bool {
228224 guard let otherIdentifier else { return true }
229-
230- switch self {
231- case . dollarIdentifier( _, let strRepresentation) :
232- return strRepresentation == otherIdentifier. name
233- default :
234- return identifier == otherIdentifier
235- }
225+ return identifier == otherIdentifier
236226 }
237227
238228 /// Extracts names introduced by the given `syntax` structure.
@@ -305,22 +295,22 @@ import SwiftSyntax
305295 return [ ]
306296 }
307297
308- return [ . identifier( identifiable, accessibleAfter: accessibleAfter) ]
298+ return [ . identifier( Syntax ( identifiable) , accessibleAfter: accessibleAfter) ]
309299 }
310300
311301 /// Extracts name introduced by `NamedDeclSyntax` node.
312302 private static func handle(
313303 namedDecl: NamedDeclSyntax ,
314304 accessibleAfter: AbsolutePosition ? = nil
315305 ) -> [ LookupName ] {
316- [ . declaration( namedDecl) ]
306+ [ . declaration( Syntax ( namedDecl) ) ]
317307 }
318308
319309 /// Debug description of this lookup name.
320- @ _spi ( Experimental ) public var debugDescription : String {
310+ public var debugDescription : String {
321311 let sourceLocationConverter = SourceLocationConverter ( fileName: " " , tree: syntax. root)
322312 let location = sourceLocationConverter. location ( for: position)
323- let strName = ( identifier? . name ?? " NO-NAME " ) + " at: \( location. line) : \( location. column) "
313+ let strName = identifier. name + " at: \( location. line) : \( location. column) "
324314
325315 switch self {
326316 case . identifier:
@@ -336,8 +326,6 @@ import SwiftSyntax
336326 return " declaration: \( strName) "
337327 case . implicit:
338328 return " implicit: \( strName) "
339- case . dollarIdentifier( _, strRepresentation: let str) :
340- return " dollarIdentifier: \( str) "
341329 case . equivalentNames( let names) :
342330 return " Composite name: [ \( names. map ( \. debugDescription) . joined ( separator: " , " ) ) ] "
343331 }
0 commit comments