Skip to content

Commit a27be39

Browse files
authored
Merge pull request #68460 from mininny/refactor-astgenvisitor-bridgedSourceLoc
[ASTGen] Refactor bridgedSourceLoc to SyntaxProtocol
2 parents d93f871 + 8169658 commit a27be39

File tree

9 files changed

+120
-108
lines changed

9 files changed

+120
-108
lines changed

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
105105
var out = [UnsafeMutableRawPointer]()
106106

107107
for element in node.statements {
108-
let loc = bridgedSourceLoc(for: element)
108+
let loc = element.bridgedSourceLoc(in: self)
109109
let swiftASTNodes = visit(element)
110110
switch swiftASTNodes {
111111
case .decl(let d):

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,6 @@ import CASTBridging
22
import CBasicBridging
33
import SwiftSyntax
44

5-
extension ASTGenVisitor {
6-
/// Obtains the bridged start location of the given node in the current file, excluding leading trivia.
7-
@inline(__always)
8-
func bridgedSourceLoc(for node: (some SyntaxProtocol)?) -> BridgedSourceLoc {
9-
guard let node else {
10-
return nil
11-
}
12-
13-
return BridgedSourceLoc(at: node.positionAfterSkippingLeadingTrivia, in: self.base)
14-
}
15-
}
16-
175
extension BridgedSourceLoc: ExpressibleByNilLiteral {
186
public init(nilLiteral: ()) {
197
self.init(raw: nil)
@@ -44,7 +32,7 @@ extension BridgedSourceLoc {
4432
extension BridgedSourceRange {
4533
@inline(__always)
4634
init(startToken: TokenSyntax, endToken: TokenSyntax, in astgen: ASTGenVisitor) {
47-
self.init(startLoc: astgen.bridgedSourceLoc(for: startToken), endLoc: astgen.bridgedSourceLoc(for: endToken))
35+
self.init(startLoc: startToken.bridgedSourceLoc(in: astgen), endLoc: endToken.bridgedSourceLoc(in: astgen))
4836
}
4937
}
5038

@@ -56,6 +44,30 @@ extension String {
5644
}
5745
}
5846

47+
extension SyntaxProtocol {
48+
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`
49+
///
50+
/// - Parameter astgen: The visitor providing the source buffer.
51+
@inline(__always)
52+
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
53+
return BridgedSourceLoc(at: self.positionAfterSkippingLeadingTrivia, in: astgen.base)
54+
}
55+
}
56+
57+
extension Optional where Wrapped: SyntaxProtocol {
58+
/// Obtains the bridged start location of the node excluding leading trivia in the source buffer provided by `astgen`.
59+
///
60+
/// - Parameter astgen: The visitor providing the source buffer.
61+
@inline(__always)
62+
func bridgedSourceLoc(in astgen: ASTGenVisitor) -> BridgedSourceLoc {
63+
guard let self else {
64+
return nil
65+
}
66+
67+
return self.bridgedSourceLoc(in: astgen)
68+
}
69+
}
70+
5971
extension TokenSyntax {
6072
/// Obtains a bridged, `ASTContext`-owned copy of this token's text.
6173
///
@@ -74,7 +86,7 @@ extension TokenSyntax {
7486
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
7587
@inline(__always)
7688
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
77-
return (self.bridgedIdentifier(in: astgen), astgen.bridgedSourceLoc(for: self))
89+
return (self.bridgedIdentifier(in: astgen), self.bridgedSourceLoc(in: astgen))
7890
}
7991

8092
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ extension ASTGenVisitor {
1515
TypeAliasDecl_create(
1616
astContext: self.ctx,
1717
declContext: self.declContext,
18-
typealiasKeywordLoc: self.bridgedSourceLoc(for: node.typealiasKeyword),
18+
typealiasKeywordLoc: node.typealiasKeyword.bridgedSourceLoc(in: self),
1919
name: name,
2020
nameLoc: nameLoc,
2121
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
22-
equalLoc: self.bridgedSourceLoc(for: node.initializer.equal),
22+
equalLoc: node.initializer.equal.bridgedSourceLoc(in: self),
2323
underlyingType: self.visit(node.initializer.value).rawValue,
2424
genericWhereClause: self.visit(node.genericWhereClause)?.rawValue
2525
)
@@ -32,7 +32,7 @@ extension ASTGenVisitor {
3232
let decl = EnumDecl_create(
3333
astContext: self.ctx,
3434
declContext: self.declContext,
35-
enumKeywordLoc: self.bridgedSourceLoc(for: node.enumKeyword),
35+
enumKeywordLoc: node.enumKeyword.bridgedSourceLoc(in: self),
3636
name: name,
3737
nameLoc: nameLoc,
3838
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
@@ -54,7 +54,7 @@ extension ASTGenVisitor {
5454
let decl = StructDecl_create(
5555
astContext: self.ctx,
5656
declContext: self.declContext,
57-
structKeywordLoc: self.bridgedSourceLoc(for: node.structKeyword),
57+
structKeywordLoc: node.structKeyword.bridgedSourceLoc(in: self),
5858
name: name,
5959
nameLoc: nameLoc,
6060
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
@@ -76,7 +76,7 @@ extension ASTGenVisitor {
7676
let decl = ClassDecl_create(
7777
astContext: self.ctx,
7878
declContext: self.declContext,
79-
classKeywordLoc: self.bridgedSourceLoc(for: node.classKeyword),
79+
classKeywordLoc: node.classKeyword.bridgedSourceLoc(in: self),
8080
name: name,
8181
nameLoc: nameLoc,
8282
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
@@ -99,7 +99,7 @@ extension ASTGenVisitor {
9999
let decl = ClassDecl_create(
100100
astContext: self.ctx,
101101
declContext: self.declContext,
102-
classKeywordLoc: self.bridgedSourceLoc(for: node.actorKeyword),
102+
classKeywordLoc: node.actorKeyword.bridgedSourceLoc(in: self),
103103
name: name,
104104
nameLoc: nameLoc,
105105
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
@@ -125,7 +125,7 @@ extension ASTGenVisitor {
125125
let decl = ProtocolDecl_create(
126126
astContext: self.ctx,
127127
declContext: self.declContext,
128-
protocolKeywordLoc: self.bridgedSourceLoc(for: node.protocolKeyword),
128+
protocolKeywordLoc: node.protocolKeyword.bridgedSourceLoc(in: self),
129129
name: name,
130130
nameLoc: nameLoc,
131131
primaryAssociatedTypeNames: primaryAssociatedTypeNames.bridgedArray(in: self),
@@ -148,7 +148,7 @@ extension ASTGenVisitor {
148148
AssociatedTypeDecl_create(
149149
astContext: self.ctx,
150150
declContext: self.declContext,
151-
associatedtypeKeywordLoc: self.bridgedSourceLoc(for: node.associatedtypeKeyword),
151+
associatedtypeKeywordLoc: node.associatedtypeKeyword.bridgedSourceLoc(in: self),
152152
name: name,
153153
nameLoc: nameLoc,
154154
inheritedTypes: self.visit(node.inheritanceClause?.inheritedTypes),
@@ -166,7 +166,7 @@ extension ASTGenVisitor {
166166
let decl = ExtensionDecl_create(
167167
astContext: self.ctx,
168168
declContext: self.declContext,
169-
extensionKeywordLoc: self.bridgedSourceLoc(for: node.extensionKeyword),
169+
extensionKeywordLoc: node.extensionKeyword.bridgedSourceLoc(in: self),
170170
extendedType: self.visit(node.extendedType).rawValue,
171171
inheritedTypes: self.visit(node.inheritanceClause?.inheritedTypes),
172172
genericWhereClause: self.visit(node.genericWhereClause)?.rawValue,
@@ -194,7 +194,7 @@ extension ASTGenVisitor {
194194
name: name,
195195
nameLoc: nameLoc,
196196
parameterList: self.visit(node.parameterClause)?.rawValue,
197-
equalsLoc: self.bridgedSourceLoc(for: node.rawValue?.equal),
197+
equalsLoc: (node.rawValue?.equal).bridgedSourceLoc(in: self),
198198
rawValue: self.visit(node.rawValue?.value)?.rawValue
199199
)
200200
)
@@ -204,7 +204,7 @@ extension ASTGenVisitor {
204204
.decl(
205205
EnumCaseDecl_create(
206206
declContext: self.declContext,
207-
caseKeywordLoc: self.bridgedSourceLoc(for: node.caseKeyword),
207+
caseKeywordLoc: node.caseKeyword.bridgedSourceLoc(in: self),
208208
elements: node.elements.lazy.map { self.visit($0).rawValue }.bridgedArray(in: self)
209209
)
210210
)
@@ -225,7 +225,7 @@ extension ASTGenVisitor {
225225
VarDecl_create(
226226
astContext: self.ctx,
227227
declContext: self.declContext,
228-
bindingKeywordLoc: self.bridgedSourceLoc(for: node.bindingSpecifier),
228+
bindingKeywordLoc: node.bindingSpecifier.bridgedSourceLoc(in: self),
229229
nameExpr: pattern,
230230
initializer: initializer,
231231
isStatic: isStatic,
@@ -248,13 +248,13 @@ extension ASTGenVisitor {
248248
astContext: self.ctx,
249249
declContext: self.declContext,
250250
staticLoc: staticLoc,
251-
funcKeywordLoc: self.bridgedSourceLoc(for: node.funcKeyword),
251+
funcKeywordLoc: node.funcKeyword.bridgedSourceLoc(in: self),
252252
name: name,
253253
nameLoc: nameLoc,
254254
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
255255
parameterList: self.visit(node.signature.parameterClause).rawValue,
256-
asyncSpecifierLoc: self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.asyncSpecifier),
257-
throwsSpecifierLoc: self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.throwsSpecifier),
256+
asyncSpecifierLoc: (node.signature.effectSpecifiers?.asyncSpecifier).bridgedSourceLoc(in: self),
257+
throwsSpecifierLoc: (node.signature.effectSpecifiers?.throwsSpecifier).bridgedSourceLoc(in: self),
258258
returnType: self.visit(node.signature.returnClause?.type)?.rawValue,
259259
genericWhereClause: self.visit(node.genericWhereClause)?.rawValue
260260
)
@@ -272,13 +272,13 @@ extension ASTGenVisitor {
272272
let decl = ConstructorDecl_create(
273273
astContext: self.ctx,
274274
declContext: self.declContext,
275-
initKeywordLoc: self.bridgedSourceLoc(for: node.initKeyword),
276-
failabilityMarkLoc: self.bridgedSourceLoc(for: node.optionalMark),
275+
initKeywordLoc: node.initKeyword.bridgedSourceLoc(in: self),
276+
failabilityMarkLoc: node.optionalMark.bridgedSourceLoc(in: self),
277277
isIUO: node.optionalMark?.tokenKind == .exclamationMark,
278278
genericParamList: self.visit(node.genericParameterClause)?.rawValue,
279279
parameterList: self.visit(node.signature.parameterClause).rawValue,
280-
asyncSpecifierLoc: self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.asyncSpecifier),
281-
throwsSpecifierLoc: self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.throwsSpecifier),
280+
asyncSpecifierLoc: (node.signature.effectSpecifiers?.asyncSpecifier).bridgedSourceLoc(in: self),
281+
throwsSpecifierLoc: (node.signature.effectSpecifiers?.throwsSpecifier).bridgedSourceLoc(in: self),
282282
genericWhereClause: self.visit(node.genericWhereClause)?.rawValue
283283
)
284284

@@ -295,7 +295,7 @@ extension ASTGenVisitor {
295295
let decl = DestructorDecl_create(
296296
astContext: self.ctx,
297297
declContext: self.declContext,
298-
deinitKeywordLoc: self.bridgedSourceLoc(for: node.deinitKeyword)
298+
deinitKeywordLoc: node.deinitKeyword.bridgedSourceLoc(in: self)
299299
)
300300

301301
if let body = node.body {
@@ -339,10 +339,10 @@ extension ASTGenVisitor {
339339
astContext: self.ctx,
340340
declContext: self.declContext,
341341
fixity: fixity,
342-
operatorKeywordLoc: self.bridgedSourceLoc(for: node.operatorKeyword),
342+
operatorKeywordLoc: node.operatorKeyword.bridgedSourceLoc(in: self),
343343
name: name,
344344
nameLoc: nameLoc,
345-
colonLoc: self.bridgedSourceLoc(for: node.operatorPrecedenceAndTypes?.colon),
345+
colonLoc: (node.operatorPrecedenceAndTypes?.colon).bridgedSourceLoc(in: self),
346346
precedenceGroupName: precedenceGroupName,
347347
PrecedenceGroupLoc: precedenceGroupLoc
348348
)
@@ -440,21 +440,21 @@ extension ASTGenVisitor {
440440
return .decl(
441441
PrecedenceGroupDecl_create(
442442
declContext: self.declContext,
443-
precedencegroupKeywordLoc: self.bridgedSourceLoc(for: node.precedencegroupKeyword),
443+
precedencegroupKeywordLoc: node.precedencegroupKeyword.bridgedSourceLoc(in: self),
444444
name: name,
445445
nameLoc: nameLoc,
446-
leftBraceLoc: self.bridgedSourceLoc(for: node.leftBrace),
447-
associativityLabelLoc: self.bridgedSourceLoc(for: body.associativity?.associativityLabel),
448-
associativityValueLoc: self.bridgedSourceLoc(for: body.associativity?.value),
446+
leftBraceLoc: node.leftBrace.bridgedSourceLoc(in: self),
447+
associativityLabelLoc: (body.associativity?.associativityLabel).bridgedSourceLoc(in: self),
448+
associativityValueLoc: (body.associativity?.value).bridgedSourceLoc(in: self),
449449
associativity: associativityValue,
450-
assignmentLabelLoc: self.bridgedSourceLoc(for: body.assignment?.assignmentLabel),
451-
assignmentValueLoc: self.bridgedSourceLoc(for: body.assignment?.value),
450+
assignmentLabelLoc: (body.assignment?.assignmentLabel).bridgedSourceLoc(in: self),
451+
assignmentValueLoc: (body.assignment?.value).bridgedSourceLoc(in: self),
452452
isAssignment: assignmentValue,
453-
higherThanKeywordLoc: self.bridgedSourceLoc(for: body.higherThanRelation?.higherThanOrLowerThanLabel),
453+
higherThanKeywordLoc: (body.higherThanRelation?.higherThanOrLowerThanLabel).bridgedSourceLoc(in: self),
454454
higherThanNames: self.visit(body.higherThanRelation?.precedenceGroups),
455-
lowerThanKeywordLoc: self.bridgedSourceLoc(for: body.lowerThanRelation?.higherThanOrLowerThanLabel),
455+
lowerThanKeywordLoc: (body.lowerThanRelation?.higherThanOrLowerThanLabel).bridgedSourceLoc(in: self),
456456
lowerThanNames: self.visit(body.lowerThanRelation?.precedenceGroups),
457-
rightBraceLoc: self.bridgedSourceLoc(for: node.rightBrace)
457+
rightBraceLoc: node.rightBrace.bridgedSourceLoc(in: self)
458458
)
459459
)
460460
}
@@ -495,9 +495,9 @@ extension ASTGenVisitor {
495495
ImportDecl_create(
496496
astContext: self.ctx,
497497
declContext: self.declContext,
498-
importKeywordLoc: self.bridgedSourceLoc(for: node.importKeyword),
498+
importKeywordLoc: node.importKeyword.bridgedSourceLoc(in: self),
499499
importKind: importKind,
500-
importKindLoc: self.bridgedSourceLoc(for: node.importKindSpecifier),
500+
importKindLoc: node.importKindSpecifier.bridgedSourceLoc(in: self),
501501
path: node.path.lazy.map {
502502
$0.name.bridgedIdentifierAndSourceLoc(in: self) as BridgedIdentifierAndSourceLoc
503503
}.bridgedArray(in: self)

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ extension ASTGenVisitor {
99
public func visit(_ node: ClosureExprSyntax) -> ASTNode {
1010
let body = BraceStmt_create(
1111
self.ctx,
12-
self.bridgedSourceLoc(for: node.leftBrace),
12+
node.leftBrace.bridgedSourceLoc(in: self),
1313
self.visit(node.statements),
14-
self.bridgedSourceLoc(for: node.rightBrace)
14+
node.rightBrace.bridgedSourceLoc(in: self)
1515
)
1616

1717
// FIXME: Translate the signature, capture list, 'in' location, etc.
@@ -59,7 +59,7 @@ extension ASTGenVisitor {
5959
}
6060

6161
public func visit(_ node: MemberAccessExprSyntax) -> ASTNode {
62-
let loc = bridgedSourceLoc(for: node)
62+
let loc = node.bridgedSourceLoc(in: self)
6363
let base = visit(node.base!).rawValue
6464
let name = node.declName.baseName.bridgedIdentifier(in: self)
6565

@@ -91,20 +91,20 @@ extension ASTGenVisitor {
9191
}
9292
let labelLocations = node.lazy.map {
9393
if let label = $0.label {
94-
return self.bridgedSourceLoc(for: label)
94+
return label.bridgedSourceLoc(in: self)
9595
}
9696

97-
return self.bridgedSourceLoc(for: $0)
97+
return $0.bridgedSourceLoc(in: self)
9898
}
9999

100100
return .expr(
101101
TupleExpr_create(
102102
self.ctx,
103-
self.bridgedSourceLoc(for: leftParen),
103+
leftParen.bridgedSourceLoc(in: self),
104104
expressions.bridgedArray(in: self),
105105
labels.bridgedArray(in: self),
106106
labelLocations.bridgedArray(in: self),
107-
self.bridgedSourceLoc(for: rightParen)
107+
rightParen.bridgedSourceLoc(in: self)
108108
)
109109
)
110110
}

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ extension ASTGenVisitor {
1010
.misc(
1111
GenericParamList_create(
1212
astContext: self.ctx,
13-
leftAngleLoc: bridgedSourceLoc(for: node.leftAngle),
13+
leftAngleLoc: node.leftAngle.bridgedSourceLoc(in: self),
1414
parameters: node.parameters.lazy.map { self.visit($0).rawValue }.bridgedArray(in: self),
1515
genericWhereClause: self.visit(node.genericWhereClause)?.rawValue,
16-
rightAngleLoc: bridgedSourceLoc(for: node.rightAngle)
16+
rightAngleLoc: node.rightAngle.bridgedSourceLoc(in: self)
1717
)
1818
)
1919
}
@@ -36,7 +36,7 @@ extension ASTGenVisitor {
3636
GenericTypeParamDecl_create(
3737
astContext: self.ctx,
3838
declContext: self.declContext,
39-
eachKeywordLoc: self.bridgedSourceLoc(for: node.eachKeyword),
39+
eachKeywordLoc: node.eachKeyword.bridgedSourceLoc(in: self),
4040
name: name,
4141
nameLoc: nameLoc,
4242
inheritedType: self.visit(node.inheritedType)?.rawValue,
@@ -50,14 +50,14 @@ extension ASTGenVisitor {
5050
switch $0.requirement {
5151
case .conformanceRequirement(let conformance):
5252
return BridgedRequirementRepr(
53-
SeparatorLoc: self.bridgedSourceLoc(for: conformance.colon),
53+
SeparatorLoc: conformance.colon.bridgedSourceLoc(in: self),
5454
Kind: .typeConstraint,
5555
FirstType: self.visit(conformance.leftType).rawValue,
5656
SecondType: self.visit(conformance.rightType).rawValue
5757
)
5858
case .sameTypeRequirement(let sameType):
5959
return BridgedRequirementRepr(
60-
SeparatorLoc: self.bridgedSourceLoc(for: sameType.equal),
60+
SeparatorLoc: sameType.equal.bridgedSourceLoc(in: self),
6161
Kind: .sameType,
6262
FirstType: self.visit(sameType.leftType).rawValue,
6363
SecondType: self.visit(sameType.rightType).rawValue
@@ -71,7 +71,7 @@ extension ASTGenVisitor {
7171
return .misc(
7272
TrailingWhereClause_create(
7373
astContext: self.ctx,
74-
whereKeywordLoc: self.bridgedSourceLoc(for: node.whereKeyword),
74+
whereKeywordLoc: node.whereKeyword.bridgedSourceLoc(in: self),
7575
requirements: requirements.bridgedArray(in: self)
7676
)
7777
)

0 commit comments

Comments
 (0)