Skip to content

Commit beac603

Browse files
authored
Merge pull request #61899 from zoecarver/astgen-fixes
A few more ASTGen fixes.
2 parents 4e36e21 + 673ccd1 commit beac603

File tree

7 files changed

+52
-7
lines changed

7 files changed

+52
-7
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ void *ReturnStmt_create(void *ctx, void *loc, void *_Nullable expr);
110110
void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs);
111111

112112
void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
113+
BridgedArrayRef names,
114+
BridgedArrayRef nameLocs,
113115
void *rparen);
114116

115117
void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args);

lib/AST/CASTBridging.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,14 @@ void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs) {
8787
}
8888

8989
void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
90+
BridgedArrayRef names,
91+
BridgedArrayRef nameLocs,
9092
void *rparen) {
93+
auto &Context = *static_cast<ASTContext *>(ctx);
9194
return TupleExpr::create(
92-
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(lparen),
93-
getArrayRef<Expr *>(subs), {}, {}, getSourceLocFromPointer(rparen),
95+
Context, getSourceLocFromPointer(lparen),
96+
getArrayRef<Expr *>(subs), getArrayRef<Identifier>(names),
97+
getArrayRef<SourceLoc>(nameLocs), getSourceLocFromPointer(rparen),
9498
/*Implicit*/ false);
9599
}
96100

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
8787
}
8888

8989
public func visit(_ node: SourceFileSyntax) -> [UnsafeMutableRawPointer] {
90-
let loc = self.base.advanced(by: node.position.utf8Offset).raw
9190
var out = [UnsafeMutableRawPointer]()
9291

9392
for element in node.statements {
93+
let loc = self.base.advanced(by: element.position.utf8Offset).raw
9494
let swiftASTNodes = visit(element)
9595
switch swiftASTNodes {
9696
case .decl(let d):

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ extension ASTGenVisitor {
9191
let secondName: UnsafeMutableRawPointer?
9292
let type: UnsafeMutableRawPointer?
9393

94-
if let nodeFirstName = node.firstName {
94+
if let nodeFirstName = node.firstName,
95+
// Swift AST represnts "_" as nil.
96+
nodeFirstName.text != "_" {
9597
var text = nodeFirstName.text
9698
firstName = text.withUTF8 { buf in
9799
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)

lib/ASTGen/Sources/ASTGen/Exprs.swift

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ extension ASTGenVisitor {
2424
}
2525

2626
let args = visit(node.argumentList).rawValue
27-
// TODO: hack
2827
let callee = visit(node.calledExpression).rawValue
2928

3029
return .expr(SwiftFunctionCallExpr_create(self.ctx, callee, args))
@@ -65,13 +64,30 @@ extension ASTGenVisitor {
6564

6665
public func visit(_ node: TupleExprElementListSyntax) -> ASTNode {
6766
let elements = node.map { self.visit($0).rawValue }
67+
let labels: [BridgedIdentifier?] = node.map {
68+
guard var name = $0.label?.text else {
69+
return nil
70+
}
71+
return name.withUTF8 { buf in
72+
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
73+
}
74+
}
75+
let labelLocs: [UnsafeMutableRawPointer] = node.map {
76+
let pos = $0.label?.position ?? $0.position
77+
return base.advanced(by: pos.utf8Offset).raw
78+
}
6879

6980
let lParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
7081
let rParenLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw
7182

7283
return .expr(
7384
elements.withBridgedArrayRef { elementsRef in
74-
SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, rParenLoc)
85+
labels.withBridgedArrayRef { labelsRef in
86+
labelLocs.withBridgedArrayRef { labelLocRef in
87+
SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, labelsRef,
88+
labelLocRef, rParenLoc)
89+
}
90+
}
7591
})
7692
}
7793
}

lib/Parse/ParseDecl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ extern "C" void parseTopLevelSwift(const char *buffer,
167167
void (*)(void *, void *));
168168

169169
static void appendToVector(void *declPtr, void *vecPtr) {
170-
auto vec = static_cast<SmallVectorImpl<Decl *> *>(vecPtr);
170+
auto vec = static_cast<SmallVectorImpl<ASTNode> *>(vecPtr);
171171
auto decl = static_cast<Decl *>(declPtr);
172172

173173
vec->push_back(decl);

test/ASTGen/verify-parse.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %target-run-simple-swift(-enable-experimental-feature SwiftParser -enable-experimental-feature ParserASTGen)
2+
3+
func test1(x: Int, fn: (Int) -> Int) -> Int {
4+
let xx = fn(42)
5+
return fn(x)
6+
}
7+
8+
func test2(b: Bool) {
9+
if b {
10+
print("TRUE")
11+
} else {
12+
print("FALSE")
13+
}
14+
15+
let x = true
16+
}
17+
18+
func test3(y: Int) -> Int {
19+
let x = y
20+
return x
21+
}

0 commit comments

Comments
 (0)