Skip to content

Commit 7b4e58a

Browse files
authored
Merge pull request #70220 from rintaro/astgen-getidentifier
[ASTGen] Move logic in `BridgedASTContext.getIdentifier()` to ASTGen
2 parents db8ca19 + 2f46e6f commit 7b4e58a

File tree

13 files changed

+284
-227
lines changed

13 files changed

+284
-227
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: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,7 @@ BridgedDeclNameLoc_createParsed(BridgedSourceLoc cBaseNameLoc) {
110110

111111
BridgedIdentifier BridgedASTContext_getIdentifier(BridgedASTContext cContext,
112112
BridgedStringRef cStr) {
113-
StringRef str = cStr.unbridged();
114-
if (str.size() == 1 && str.front() == '_')
115-
return BridgedIdentifier();
116-
117-
// If this was a back-ticked identifier, drop the back-ticks.
118-
if (str.size() >= 2 && str.front() == '`' && str.back() == '`') {
119-
str = str.drop_front().drop_back();
120-
}
121-
122-
return cContext.unbridged().getIdentifier(str);
113+
return cContext.unbridged().getIdentifier(cStr.unbridged());
123114
}
124115

125116
bool BridgedASTContext_langOptsHasFeature(BridgedASTContext cContext,
@@ -681,8 +672,7 @@ BridgedNominalTypeDecl BridgedProtocolDecl_createParsed(
681672

682673
auto primaryAssociatedTypeNames =
683674
context.AllocateTransform<PrimaryAssociatedTypeName>(
684-
cPrimaryAssociatedTypeNames
685-
.unbridged<BridgedIdentifierAndSourceLoc>(),
675+
cPrimaryAssociatedTypeNames.unbridged<BridgedLocatedIdentifier>(),
686676
[](auto &e) -> PrimaryAssociatedTypeName {
687677
return {e.Name.unbridged(), e.NameLoc.unbridged()};
688678
});
@@ -781,15 +771,13 @@ BridgedPrecedenceGroupDecl BridgedPrecedenceGroupDecl_createParsed(
781771
BridgedSourceLoc cRightBraceLoc) {
782772

783773
SmallVector<PrecedenceGroupDecl::Relation, 2> higherThanNames;
784-
for (auto &pair :
785-
cHigherThanNames.unbridged<BridgedIdentifierAndSourceLoc>()) {
774+
for (auto &pair : cHigherThanNames.unbridged<BridgedLocatedIdentifier>()) {
786775
higherThanNames.push_back(
787776
{pair.NameLoc.unbridged(), pair.Name.unbridged(), nullptr});
788777
}
789778

790779
SmallVector<PrecedenceGroupDecl::Relation, 2> lowerThanNames;
791-
for (auto &pair :
792-
cLowerThanNames.unbridged<BridgedIdentifierAndSourceLoc>()) {
780+
for (auto &pair : cLowerThanNames.unbridged<BridgedLocatedIdentifier>()) {
793781
lowerThanNames.push_back(
794782
{pair.NameLoc.unbridged(), pair.Name.unbridged(), nullptr});
795783
}
@@ -811,7 +799,7 @@ BridgedImportDecl BridgedImportDecl_createParsed(
811799
BridgedSourceLoc cImportKindLoc, BridgedArrayRef cImportPathElements) {
812800
ImportPath::Builder builder;
813801
for (auto &element :
814-
cImportPathElements.unbridged<BridgedIdentifierAndSourceLoc>()) {
802+
cImportPathElements.unbridged<BridgedLocatedIdentifier>()) {
815803
builder.push_back(element.Name.unbridged(), element.NameLoc.unbridged());
816804
}
817805

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: 22 additions & 30 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(
@@ -141,81 +134,80 @@ extension SyntaxProtocol {
141134
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`
142135
///
143136
/// - Parameter astgen: The visitor providing the source buffer.
137+
@available(*, deprecated, message: "use ASTContext.bridgedSourceLoc(syntax:)")
144138
@inline(__always)
145139
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
146-
return BridgedSourceLoc(at: self.positionAfterSkippingLeadingTrivia, in: astgen.base)
140+
astgen.generateSourceLoc(self)
147141
}
148142
}
149143

150144
extension Optional where Wrapped: SyntaxProtocol {
151145
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`.
152146
///
153147
/// - Parameter astgen: The visitor providing the source buffer.
148+
@available(*, deprecated, message: "use ASTContext.bridgedSourceLoc(syntax:)")
154149
@inline(__always)
155150
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
156-
guard let self else {
157-
return nil
158-
}
159-
160-
return self.bridgedSourceLoc(in: astgen)
151+
astgen.generateSourceLoc(self)
161152
}
162153
}
163154

164155
extension TokenSyntax {
165156
/// Obtains a bridged, `ASTContext`-owned copy of this token's text.
166157
///
167158
/// - Parameter astgen: The visitor providing the `ASTContext`.
159+
@available(*, deprecated, message: "use ASTContext.bridgedIdentifier(token:)")
168160
@inline(__always)
169161
func bridgedIdentifier(in astgen: ASTGenVisitor) -> BridgedIdentifier {
170-
var text = self.text
171-
return text.withBridgedString { bridged in
172-
astgen.ctx.getIdentifier(bridged)
173-
}
162+
astgen.generateIdentifier(self)
174163
}
175164

176165
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
177166
/// source buffer provided by `astgen`.
178167
///
179168
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
169+
@available(*, deprecated, message: "use ASTContext.bridgedIdentifierAndSourceLoc(token:)")
180170
@inline(__always)
181171
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
182-
return (self.bridgedIdentifier(in: astgen), self.bridgedSourceLoc(in: astgen))
172+
astgen.generateIdentifierAndSourceLoc(self)
183173
}
184174

185175
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
186176
/// source buffer provided by `astgen`.
187177
///
188178
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
179+
@available(*, deprecated, message: "use ASTContext.bridgedIdentifierAndSourceLoc(token:)")
189180
@inline(__always)
190-
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> BridgedIdentifierAndSourceLoc {
191-
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(in: astgen)
192-
return .init(name: name, nameLoc: nameLoc)
181+
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> BridgedLocatedIdentifier {
182+
astgen.generateLocatedIdentifier(self)
193183
}
194184
}
195185

196186
extension Optional<TokenSyntax> {
197187
/// Obtains a bridged, `ASTContext`-owned copy of this token's text.
198188
///
199189
/// - Parameter astgen: The visitor providing the `ASTContext`.
190+
@available(*, deprecated, message: "use ASTContext.bridgedIdentifier(token:)")
200191
@inline(__always)
201192
func bridgedIdentifier(in astgen: ASTGenVisitor) -> BridgedIdentifier {
202-
guard let self else {
203-
return nil
204-
}
205-
206-
return self.bridgedIdentifier(in: astgen)
193+
astgen.generateIdentifier(self)
207194
}
208195

209196
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
210197
/// source buffer provided by `astgen` excluding leading trivia.
211198
///
212199
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
200+
@available(*, deprecated, message: "use ASTContext.bridgedIdentifierAndSourceLoc(token:)")
213201
@inline(__always)
214202
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
215-
guard let self else {
216-
return (nil, nil)
217-
}
203+
astgen.generateIdentifierAndSourceLoc(self)
204+
}
205+
}
218206

219-
return self.bridgedIdentifierAndSourceLoc(in: astgen)
207+
extension BridgedSourceRange {
208+
@available(*, deprecated, message: "use ASTContext.bridgedSourceRange(startToken:endToken:)")
209+
@inline(__always)
210+
init(startToken: TokenSyntax, endToken: TokenSyntax, in astgen: ASTGenVisitor) {
211+
self = astgen.generateSourceRange(start: startToken, end: endToken)
220212
}
221213
}

0 commit comments

Comments
 (0)