Skip to content

Commit 10c067f

Browse files
committed
[ASTGen] Move getIdentifier() etc to ASTGenVisitor
Now that `getIdentifier()` has some logic in it. It's not a simple bridging.
1 parent 84afc65 commit 10c067f

File tree

11 files changed

+241
-267
lines changed

11 files changed

+241
-267
lines changed

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import ASTBridging
1414
import BasicBridging
1515
import ParseBridging
1616
// Needed to use BumpPtrAllocator
17-
@_spi(BumpPtrAllocator) import SwiftSyntax
17+
@_spi(BumpPtrAllocator) @_spi(RawSyntax) import SwiftSyntax
1818

1919
import struct SwiftDiagnostics.Diagnostic
2020

@@ -106,7 +106,7 @@ struct ASTGenVisitor {
106106
var out = [BridgedDecl]()
107107

108108
for element in node.statements {
109-
let loc = element.bridgedSourceLoc(in: self)
109+
let loc = self.bridgedSourceLoc(syntax: element)
110110
let swiftASTNodes = generate(codeBlockItem: element)
111111
switch swiftASTNodes {
112112
case .decl(let d):
@@ -138,6 +138,73 @@ struct ASTGenVisitor {
138138
}
139139
}
140140

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+
141208
extension ASTGenVisitor {
142209
/// Replaces the current declaration context with `declContext` for the duration of its execution, and calls `body`.
143210
@inline(__always)

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 0 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,6 @@ public extension BridgedSourceLoc {
7878
}
7979
}
8080

81-
extension BridgedSourceRange {
82-
@inline(__always)
83-
init(startToken: TokenSyntax, endToken: TokenSyntax, in astgen: ASTGenVisitor) {
84-
self.init(start: startToken.bridgedSourceLoc(in: astgen), end: endToken.bridgedSourceLoc(in: astgen))
85-
}
86-
}
87-
8881
extension String {
8982
init(bridged: BridgedStringRef) {
9083
self.init(
@@ -136,90 +129,3 @@ extension BridgedStringRef {
136129
return self.data == nil && self.count == 0
137130
}
138131
}
139-
140-
extension SyntaxProtocol {
141-
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`
142-
///
143-
/// - Parameter astgen: The visitor providing the source buffer.
144-
@inline(__always)
145-
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
146-
return BridgedSourceLoc(at: self.positionAfterSkippingLeadingTrivia, in: astgen.base)
147-
}
148-
}
149-
150-
extension Optional where Wrapped: SyntaxProtocol {
151-
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`.
152-
///
153-
/// - Parameter astgen: The visitor providing the source buffer.
154-
@inline(__always)
155-
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
156-
guard let self else {
157-
return nil
158-
}
159-
160-
return self.bridgedSourceLoc(in: astgen)
161-
}
162-
}
163-
164-
extension TokenSyntax {
165-
/// Obtains a bridged, `ASTContext`-owned copy of this token's text.
166-
///
167-
/// - Parameter astgen: The visitor providing the `ASTContext`.
168-
@inline(__always)
169-
func bridgedIdentifier(in astgen: ASTGenVisitor) -> BridgedIdentifier {
170-
var text = self.rawText
171-
if rawText == "_" {
172-
return nil
173-
}
174-
if rawText.count > 2 && rawText.hasPrefix("`") && rawText.hasSuffix("`") {
175-
text = .init(rebasing: text.dropFirst().dropLast())
176-
}
177-
return astgen.ctx.getIdentifier(rawText.bridged)
178-
}
179-
180-
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
181-
/// source buffer provided by `astgen`.
182-
///
183-
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
184-
@inline(__always)
185-
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
186-
return (self.bridgedIdentifier(in: astgen), self.bridgedSourceLoc(in: astgen))
187-
}
188-
189-
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
190-
/// source buffer provided by `astgen`.
191-
///
192-
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
193-
@inline(__always)
194-
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> BridgedIdentifierAndSourceLoc {
195-
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(in: astgen)
196-
return .init(name: name, nameLoc: nameLoc)
197-
}
198-
}
199-
200-
extension Optional<TokenSyntax> {
201-
/// Obtains a bridged, `ASTContext`-owned copy of this token's text.
202-
///
203-
/// - Parameter astgen: The visitor providing the `ASTContext`.
204-
@inline(__always)
205-
func bridgedIdentifier(in astgen: ASTGenVisitor) -> BridgedIdentifier {
206-
guard let self else {
207-
return nil
208-
}
209-
210-
return self.bridgedIdentifier(in: astgen)
211-
}
212-
213-
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
214-
/// source buffer provided by `astgen` excluding leading trivia.
215-
///
216-
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
217-
@inline(__always)
218-
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
219-
guard let self else {
220-
return (nil, nil)
221-
}
222-
223-
return self.bridgedIdentifierAndSourceLoc(in: astgen)
224-
}
225-
}

0 commit comments

Comments
 (0)