Skip to content

Commit 034a39a

Browse files
committed
ASTGen: Set brace locations for iterable declaration contexts
1 parent 1291333 commit 034a39a

File tree

5 files changed

+50
-14
lines changed

5 files changed

+50
-14
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ typedef struct BridgedSourceLoc {
4747
const void *_Nullable raw;
4848
} BridgedSourceLoc;
4949

50+
typedef struct {
51+
BridgedSourceLoc startLoc;
52+
BridgedSourceLoc endLoc;
53+
} BridgedSourceRange;
54+
5055
typedef struct BridgedIdentifier {
5156
const void *_Nullable raw;
5257
} BridgedIdentifier;
@@ -344,13 +349,13 @@ BridgedDeclContextAndDecl
344349
StructDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
345350
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
346351
BridgedSourceLoc cNameLoc,
347-
void *_Nullable opaqueGenericParamList);
352+
void *_Nullable opaqueGenericParamList,
353+
BridgedSourceRange cBraceRange);
348354

349-
BridgedDeclContextAndDecl ClassDecl_create(BridgedASTContext cContext,
350-
BridgedDeclContext cDeclContext,
351-
BridgedSourceLoc cClassKeywordLoc,
352-
BridgedIdentifier cName,
353-
BridgedSourceLoc cNameLoc);
355+
BridgedDeclContextAndDecl
356+
ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
357+
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
358+
BridgedSourceLoc cNameLoc, BridgedSourceRange cBraceRange);
354359

355360
void *GenericParamList_create(BridgedASTContext cContext,
356361
BridgedSourceLoc cLAngleLoc,

lib/AST/CASTBridging.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ static inline SourceLoc convertSourceLoc(BridgedSourceLoc cLoc) {
7070
return SourceLoc(smLoc);
7171
}
7272

73+
static inline SourceRange convertSourceRange(BridgedSourceRange cRange) {
74+
return SourceRange(convertSourceLoc(cRange.startLoc),
75+
convertSourceLoc(cRange.endLoc));
76+
}
77+
7378
static inline Identifier convertIdentifier(BridgedIdentifier cIdentifier) {
7479
return Identifier::getFromOpaquePointer(const_cast<void *>(cIdentifier.raw));
7580
}
@@ -533,29 +538,31 @@ BridgedDeclContextAndDecl
533538
StructDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
534539
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
535540
BridgedSourceLoc cNameLoc,
536-
void *_Nullable opaqueGenericParamList) {
541+
void *_Nullable opaqueGenericParamList,
542+
BridgedSourceRange cBraceRange) {
537543
ASTContext &context = convertASTContext(cContext);
538544

539545
auto *decl = new (context)
540546
StructDecl(convertSourceLoc(cStructKeywordLoc), convertIdentifier(cName),
541547
convertSourceLoc(cNameLoc), {},
542548
static_cast<GenericParamList *>(opaqueGenericParamList),
543549
convertDeclContext(cDeclContext));
550+
decl->setBraces(convertSourceRange(cBraceRange));
544551

545552
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
546553
}
547554

548-
BridgedDeclContextAndDecl ClassDecl_create(BridgedASTContext cContext,
549-
BridgedDeclContext cDeclContext,
550-
BridgedSourceLoc cClassKeywordLoc,
551-
BridgedIdentifier cName,
552-
BridgedSourceLoc cNameLoc) {
555+
BridgedDeclContextAndDecl
556+
ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
557+
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
558+
BridgedSourceLoc cNameLoc, BridgedSourceRange cBraceRange) {
553559
ASTContext &context = convertASTContext(cContext);
554560

555561
auto *decl = new (context)
556562
ClassDecl(convertSourceLoc(cClassKeywordLoc), convertIdentifier(cName),
557563
convertSourceLoc(cNameLoc), {}, nullptr,
558564
convertDeclContext(cDeclContext), false);
565+
decl->setBraces(convertSourceRange(cBraceRange));
559566

560567
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
561568
}

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ extension BridgedSourceLoc {
4141
}
4242
}
4343

44+
extension BridgedSourceRange {
45+
@inline(__always)
46+
init(startToken: TokenSyntax, endToken: TokenSyntax, in astgen: ASTGenVisitor) {
47+
self.init(startLoc: astgen.bridgedSourceLoc(for: startToken), endLoc: astgen.bridgedSourceLoc(for: endToken))
48+
}
49+
}
50+
4451
extension String {
4552
mutating func withBridgedString<R>(_ body: (BridgedString) throws -> R) rethrows -> R {
4653
try withUTF8 { buffer in

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ extension ASTGenVisitor {
3333
self.bridgedSourceLoc(for: node.structKeyword),
3434
name,
3535
nameLoc,
36-
self.visit(node.genericParameterClause)?.rawValue
36+
self.visit(node.genericParameterClause)?.rawValue,
37+
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
3738
)
3839

3940
self.withDeclContext(decl.asDeclContext) {
@@ -51,7 +52,8 @@ extension ASTGenVisitor {
5152
self.declContext,
5253
self.bridgedSourceLoc(for: node.classKeyword),
5354
name,
54-
nameLoc
55+
nameLoc,
56+
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
5557
)
5658

5759
self.withDeclContext(decl.asDeclContext) {

test/ASTGen/verify-parse.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,18 @@ typealias
8383
Alias<T>
8484
=
8585
String
86+
87+
struct
88+
Struct
89+
<
90+
T1,
91+
T2
92+
>
93+
{
94+
/*static*/ func method(_ b: Bool) {}
95+
}
96+
97+
class
98+
Class {
99+
func method(_ b: Bool) {}
100+
}

0 commit comments

Comments
 (0)