Skip to content

Commit 7ab4003

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 71ecdfc commit 7ab4003

File tree

13 files changed

+261
-281
lines changed

13 files changed

+261
-281
lines changed

include/swift/AST/ASTBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ inline const void *_Nullable BridgedIdentifier_raw(BridgedIdentifier ident) {
6565
return ident.Raw;
6666
}
6767

68-
struct BridgedIdentifierAndSourceLoc {
68+
struct BridgedLocatedIdentifier {
6969
SWIFT_NAME("name")
7070
BridgedIdentifier Name;
7171

lib/AST/ASTBridging.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -672,8 +672,7 @@ BridgedNominalTypeDecl BridgedProtocolDecl_createParsed(
672672

673673
auto primaryAssociatedTypeNames =
674674
context.AllocateTransform<PrimaryAssociatedTypeName>(
675-
cPrimaryAssociatedTypeNames
676-
.unbridged<BridgedIdentifierAndSourceLoc>(),
675+
cPrimaryAssociatedTypeNames.unbridged<BridgedLocatedIdentifier>(),
677676
[](auto &e) -> PrimaryAssociatedTypeName {
678677
return {e.Name.unbridged(), e.NameLoc.unbridged()};
679678
});
@@ -772,15 +771,13 @@ BridgedPrecedenceGroupDecl BridgedPrecedenceGroupDecl_createParsed(
772771
BridgedSourceLoc cRightBraceLoc) {
773772

774773
SmallVector<PrecedenceGroupDecl::Relation, 2> higherThanNames;
775-
for (auto &pair :
776-
cHigherThanNames.unbridged<BridgedIdentifierAndSourceLoc>()) {
774+
for (auto &pair : cHigherThanNames.unbridged<BridgedLocatedIdentifier>()) {
777775
higherThanNames.push_back(
778776
{pair.NameLoc.unbridged(), pair.Name.unbridged(), nullptr});
779777
}
780778

781779
SmallVector<PrecedenceGroupDecl::Relation, 2> lowerThanNames;
782-
for (auto &pair :
783-
cLowerThanNames.unbridged<BridgedIdentifierAndSourceLoc>()) {
780+
for (auto &pair : cLowerThanNames.unbridged<BridgedLocatedIdentifier>()) {
784781
lowerThanNames.push_back(
785782
{pair.NameLoc.unbridged(), pair.Name.unbridged(), nullptr});
786783
}
@@ -802,7 +799,7 @@ BridgedImportDecl BridgedImportDecl_createParsed(
802799
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements) {
803800
ImportPath::Builder builder;
804801
for (auto &element :
805-
cImportPathElements.unbridged<BridgedIdentifierAndSourceLoc>()) {
802+
cImportPathElements.unbridged<BridgedLocatedIdentifier>()) {
806803
builder.push_back(element.Name.unbridged(), element.NameLoc.unbridged());
807804
}
808805

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 78 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.generateSourceLoc(element)
110110
let swiftASTNodes = generate(codeBlockItem: element)
111111
switch swiftASTNodes {
112112
case .decl(let d):
@@ -138,6 +138,82 @@ 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+
@inline(__always)
147+
func generateIdentifier(_ token: TokenSyntax) -> BridgedIdentifier {
148+
var text = token.rawText
149+
// FIXME: Maybe `TokenSyntax.tokenView.rawKind == .wildcard`, or expose it as `.rawTokenKind`.
150+
if text == "_" {
151+
return nil
152+
}
153+
if text.count > 2 && text.hasPrefix("`") && text.hasSuffix("`") {
154+
text = .init(rebasing: text.dropFirst().dropLast())
155+
}
156+
return self.ctx.getIdentifier(text.bridged)
157+
}
158+
159+
/// Obtains a bridged, `ASTContext`-owned "identifier".
160+
///
161+
/// If the `token` text is `nil`, return an empty identifier.
162+
@inline(__always)
163+
func generateIdentifier(_ token: TokenSyntax?) -> BridgedIdentifier {
164+
token.map(generateIdentifier(_:)) ?? nil
165+
}
166+
167+
/// Obtains the start location of the node excluding leading trivia in the
168+
/// source buffer.
169+
@inline(__always)
170+
func generateSourceLoc(_ node: some SyntaxProtocol) -> BridgedSourceLoc {
171+
BridgedSourceLoc(at: node.positionAfterSkippingLeadingTrivia, in: self.base)
172+
}
173+
174+
/// Obtains the start location of the node excluding leading trivia in the
175+
/// source buffer. If the `node` is nil returns an invalid source location.
176+
@inline(__always)
177+
func generateSourceLoc(_ node: (some SyntaxProtocol)?) -> BridgedSourceLoc {
178+
node.map(generateSourceLoc(_:)) ?? nil
179+
}
180+
181+
/// Obtains a pair of bridged identifier and the bridged source location.
182+
@inline(__always)
183+
func generateIdentifierAndSourceLoc(_ token: TokenSyntax) -> (identifier: BridgedIdentifier, sourceLoc: BridgedSourceLoc) {
184+
return (
185+
self.generateIdentifier(token),
186+
self.generateSourceLoc(token)
187+
)
188+
}
189+
190+
/// Obtains a pair of bridged identifier and the bridged source location.
191+
/// If `token` is `nil`, returns a pair of an empty identifier and an invalid
192+
/// source location.
193+
@inline(__always)
194+
func generateIdentifierAndSourceLoc(_ token: TokenSyntax?) -> (identifier: BridgedIdentifier, sourceLoc: BridgedSourceLoc) {
195+
token.map(generateIdentifierAndSourceLoc(_:)) ?? (nil, nil)
196+
}
197+
198+
/// Obtains a pair of bridged identifier and the bridged source location.
199+
@inline(__always)
200+
func generateLocatedIdentifier(_ token: TokenSyntax) -> BridgedLocatedIdentifier {
201+
BridgedLocatedIdentifier(
202+
name: self.generateIdentifier(token),
203+
nameLoc: self.generateSourceLoc(token)
204+
)
205+
}
206+
207+
/// Obtains bridged token source range from a pair of token nodes.
208+
@inline(__always)
209+
func generateSourceRange(start: TokenSyntax, end: TokenSyntax) -> BridgedSourceRange {
210+
BridgedSourceRange(
211+
start: self.generateSourceLoc(start),
212+
end: self.generateSourceLoc(end)
213+
)
214+
}
215+
}
216+
141217
extension ASTGenVisitor {
142218
/// Replaces the current declaration context with `declContext` for the duration of its execution, and calls `body`.
143219
@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)