Skip to content

A few more ASTGen fixes. #61899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ void *ReturnStmt_create(void *ctx, void *loc, void *_Nullable expr);
void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs);

void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
BridgedArrayRef names,
BridgedArrayRef nameLocs,
void *rparen);

void *SwiftFunctionCallExpr_create(void *ctx, void *fn, void *args);
Expand Down
8 changes: 6 additions & 2 deletions lib/AST/CASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,14 @@ void *SwiftSequenceExpr_create(void *ctx, BridgedArrayRef exprs) {
}

void *SwiftTupleExpr_create(void *ctx, void *lparen, BridgedArrayRef subs,
BridgedArrayRef names,
BridgedArrayRef nameLocs,
void *rparen) {
auto &Context = *static_cast<ASTContext *>(ctx);
return TupleExpr::create(
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(lparen),
getArrayRef<Expr *>(subs), {}, {}, getSourceLocFromPointer(rparen),
Context, getSourceLocFromPointer(lparen),
getArrayRef<Expr *>(subs), getArrayRef<Identifier>(names),
getArrayRef<SourceLoc>(nameLocs), getSourceLocFromPointer(rparen),
/*Implicit*/ false);
}

Expand Down
2 changes: 1 addition & 1 deletion lib/ASTGen/Sources/ASTGen/ASTGen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
}

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

for element in node.statements {
let loc = self.base.advanced(by: element.position.utf8Offset).raw
let swiftASTNodes = visit(element)
switch swiftASTNodes {
case .decl(let d):
Expand Down
4 changes: 3 additions & 1 deletion lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ extension ASTGenVisitor {
let secondName: UnsafeMutableRawPointer?
let type: UnsafeMutableRawPointer?

if let nodeFirstName = node.firstName {
if let nodeFirstName = node.firstName,
// Swift AST represnts "_" as nil.
nodeFirstName.text != "_" {
var text = nodeFirstName.text
firstName = text.withUTF8 { buf in
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
Expand Down
20 changes: 18 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ extension ASTGenVisitor {
}

let args = visit(node.argumentList).rawValue
// TODO: hack
let callee = visit(node.calledExpression).rawValue

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

public func visit(_ node: TupleExprElementListSyntax) -> ASTNode {
let elements = node.map { self.visit($0).rawValue }
let labels: [BridgedIdentifier?] = node.map {
guard var name = $0.label?.text else {
return nil
}
return name.withUTF8 { buf in
SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
}
}
let labelLocs: [UnsafeMutableRawPointer] = node.map {
let pos = $0.label?.position ?? $0.position
return base.advanced(by: pos.utf8Offset).raw
}

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

return .expr(
elements.withBridgedArrayRef { elementsRef in
SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, rParenLoc)
labels.withBridgedArrayRef { labelsRef in
labelLocs.withBridgedArrayRef { labelLocRef in
SwiftTupleExpr_create(self.ctx, lParenLoc, elementsRef, labelsRef,
labelLocRef, rParenLoc)
}
}
})
}
}
2 changes: 1 addition & 1 deletion lib/Parse/ParseDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ extern "C" void parseTopLevelSwift(const char *buffer,
void (*)(void *, void *));

static void appendToVector(void *declPtr, void *vecPtr) {
auto vec = static_cast<SmallVectorImpl<Decl *> *>(vecPtr);
auto vec = static_cast<SmallVectorImpl<ASTNode> *>(vecPtr);
auto decl = static_cast<Decl *>(declPtr);

vec->push_back(decl);
Expand Down
21 changes: 21 additions & 0 deletions test/ASTGen/verify-parse.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-run-simple-swift(-enable-experimental-feature SwiftParser -enable-experimental-feature ParserASTGen)

func test1(x: Int, fn: (Int) -> Int) -> Int {
let xx = fn(42)
return fn(x)
}

func test2(b: Bool) {
if b {
print("TRUE")
} else {
print("FALSE")
}

let x = true
}

func test3(y: Int) -> Int {
let x = y
return x
}