@@ -14,7 +14,7 @@ import ASTBridging
14
14
import BasicBridging
15
15
import ParseBridging
16
16
// Needed to use BumpPtrAllocator
17
- @_spi ( BumpPtrAllocator) import SwiftSyntax
17
+ @_spi ( BumpPtrAllocator) @ _spi ( RawSyntax ) import SwiftSyntax
18
18
19
19
import struct SwiftDiagnostics. Diagnostic
20
20
@@ -106,7 +106,7 @@ struct ASTGenVisitor {
106
106
var out = [ BridgedDecl] ( )
107
107
108
108
for element in node. statements {
109
- let loc = element . bridgedSourceLoc ( in : self )
109
+ let loc = self . bridgedSourceLoc ( syntax : element )
110
110
let swiftASTNodes = generate ( codeBlockItem: element)
111
111
switch swiftASTNodes {
112
112
case . decl( let d) :
@@ -138,6 +138,73 @@ struct ASTGenVisitor {
138
138
}
139
139
}
140
140
141
+ extension ASTGenVisitor {
142
+ /// Obtains a bridged, `ASTContext`-owned "identifier".
143
+ ///
144
+ /// If the token text is `_`, return an empty identifier. If the token is an
145
+ /// escaped identifier, backticks are stripped.
146
+ func bridgedIdentifier( token: TokenSyntax ) -> BridgedIdentifier {
147
+ var text = token. rawText
148
+ if text == " _ " {
149
+ return nil
150
+ }
151
+ if text. count > 2 && text. hasPrefix ( " ` " ) && text. hasSuffix ( " ` " ) {
152
+ text = . init( rebasing: text. dropFirst ( ) . dropLast ( ) )
153
+ }
154
+ return self . ctx. getIdentifier ( text. bridged)
155
+ }
156
+
157
+ /// Obtains a bridged, `ASTContext`-owned "identifier".
158
+ ///
159
+ /// If the `token` text is `nil`, return an empty identifier.
160
+ func bridgedIdentifier( token: TokenSyntax ? ) -> BridgedIdentifier {
161
+ token. map ( bridgedIdentifier ( token: ) ) ?? nil
162
+ }
163
+
164
+ /// Obtains the start location of the node excluding leading trivia in the
165
+ /// source buffer.
166
+ func bridgedSourceLoc( syntax node: some SyntaxProtocol ) -> BridgedSourceLoc {
167
+ BridgedSourceLoc ( at: node. positionAfterSkippingLeadingTrivia, in: self . base)
168
+ }
169
+
170
+ /// Obtains the start location of the node excluding leading trivia in the
171
+ /// source buffer. If the `node` is nil returns an invalid source location.
172
+ func bridgedSourceLoc( syntax node: ( some SyntaxProtocol ) ? ) -> BridgedSourceLoc {
173
+ node. map ( bridgedSourceLoc ( syntax: ) ) ?? nil
174
+ }
175
+
176
+ /// Obtains a pair of bridged identifier and the bridged source location.
177
+ func bridgedIdentifierAndSourceLoc( token: TokenSyntax ) -> ( identifier: BridgedIdentifier , sourceLoc: BridgedSourceLoc ) {
178
+ return (
179
+ self . bridgedIdentifier ( token: token) ,
180
+ self . bridgedSourceLoc ( syntax: token)
181
+ )
182
+ }
183
+
184
+ /// Obtains a pair of bridged identifier and the bridged source location.
185
+ /// If `token` is `nil`, returns a pair of an empty identifier and an invalid
186
+ /// source location.
187
+ func bridgedIdentifierAndSourceLoc( token: TokenSyntax ? ) -> ( identifier: BridgedIdentifier , sourceLoc: BridgedSourceLoc ) {
188
+ token. map ( bridgedIdentifierAndSourceLoc ( token: ) ) ?? ( nil , nil )
189
+ }
190
+
191
+ /// Obtains a pair of bridged identifier and the bridged source location.
192
+ func bridgedIdentifierAndSourceLoc( token: TokenSyntax ) -> BridgedIdentifierAndSourceLoc {
193
+ BridgedIdentifierAndSourceLoc (
194
+ name: self . bridgedIdentifier ( token: token) ,
195
+ nameLoc: self . bridgedSourceLoc ( syntax: token)
196
+ )
197
+ }
198
+
199
+ /// Obtains bridged token source range from a pair of token nodes.
200
+ func bridgedSourceRange( startToken: TokenSyntax , endToken: TokenSyntax ) -> BridgedSourceRange {
201
+ BridgedSourceRange (
202
+ start: self . bridgedSourceLoc ( syntax: startToken) ,
203
+ end: self . bridgedSourceLoc ( syntax: endToken)
204
+ )
205
+ }
206
+ }
207
+
141
208
extension ASTGenVisitor {
142
209
/// Replaces the current declaration context with `declContext` for the duration of its execution, and calls `body`.
143
210
@inline ( __always)
0 commit comments