Skip to content

[astgen] A number of small fixes. #61836

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 6 commits into from
Nov 1, 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
21 changes: 14 additions & 7 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,14 +154,20 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
void *_Nullable argName, void *_Nullable paramLoc,
void *_Nullable paramName, void *_Nullable type,
void *declContext);
struct FuncDeclBridged {
void *declContext;
void *funcDecl;
void *decl;
};

void *FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
void *_Nullable asyncLoc, _Bool throws,
void *_Nullable throwsLoc, void *paramLLoc,
BridgedArrayRef params, void *paramRLoc,
void *_Nullable body, void *_Nullable returnType,
void *declContext);
struct FuncDeclBridged
FuncDecl_create(void *ctx, void *staticLoc, _Bool isStatic, void *funcLoc,
BridgedIdentifier name, void *nameLoc, _Bool isAsync,
void *_Nullable asyncLoc, _Bool throws,
void *_Nullable throwsLoc, void *paramLLoc,
BridgedArrayRef params, void *paramRLoc,
void *_Nullable returnType, void *declContext);
void FuncDecl_setBody(void *fn, void *body);

void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id);

Expand Down Expand Up @@ -230,6 +236,7 @@ void TopLevelCodeDecl_dump(void *);
void Expr_dump(void *);
void Decl_dump(void *);
void Stmt_dump(void *);
void Type_dump(void *);

#ifdef __cplusplus
}
Expand Down
26 changes: 17 additions & 9 deletions lib/AST/CASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,13 @@ void *ParamDecl_create(void *ctx, void *loc, void *_Nullable argLoc,
return paramDecl;
}

void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
BridgedIdentifier name, void *nameLoc, bool isAsync,
void *_Nullable asyncLoc, bool throws,
void *_Nullable throwsLoc, void *paramLLoc,
BridgedArrayRef params, void *paramRLoc,
void *_Nullable body, void *_Nullable returnType,
void *declContext) {
struct FuncDeclBridged
FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
BridgedIdentifier name, void *nameLoc, bool isAsync,
void *_Nullable asyncLoc, bool throws,
void *_Nullable throwsLoc, void *paramLLoc,
BridgedArrayRef params, void *paramRLoc,
void *_Nullable returnType, void *declContext) {
auto *paramList = ParameterList::create(
*static_cast<ASTContext *>(ctx), getSourceLocFromPointer(paramLLoc),
getArrayRef<ParamDecl *>(params), getSourceLocFromPointer(paramRLoc));
Expand All @@ -240,9 +240,13 @@ void *FuncDecl_create(void *ctx, void *staticLoc, bool isStatic, void *funcLoc,
getSourceLocFromPointer(asyncLoc), throws,
getSourceLocFromPointer(throwsLoc), nullptr, paramList,
(TypeRepr *)returnType, (DeclContext *)declContext);
out->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);

return static_cast<Decl *>(out);
return {static_cast<DeclContext *>(out), static_cast<FuncDecl *>(out),
static_cast<Decl *>(out)};
}

void FuncDecl_setBody(void *fn, void *body) {
((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
}

void *SimpleIdentTypeRepr_create(void *ctx, void *loc, BridgedIdentifier id) {
Expand Down Expand Up @@ -283,10 +287,13 @@ void *ClosureExpr_create(void *ctx, void *body, void *dc) {
SourceLoc inLoc;

ASTContext &Context = *static_cast<ASTContext *>(ctx);
auto params = ParameterList::create(Context, inLoc, {}, inLoc);

auto *out = new (Context)
ClosureExpr(attributes, bracketRange, nullptr, nullptr, asyncLoc,
throwsLoc, arrowLoc, inLoc, nullptr, 0, (DeclContext *)dc);
out->setBody((BraceStmt *)body, true);
out->setParameterList(params);
return (Expr *)out;
}

Expand Down Expand Up @@ -505,3 +512,4 @@ void TopLevelCodeDecl_dump(void *decl) {
void Expr_dump(void *expr) { ((Expr *)expr)->dump(llvm::errs()); }
void Decl_dump(void *expr) { ((Decl *)expr)->dump(llvm::errs()); }
void Stmt_dump(void *expr) { ((Stmt *)expr)->dump(llvm::errs()); }
void Type_dump(void *expr) { ((TypeRepr *)expr)->dump(); }
40 changes: 27 additions & 13 deletions lib/ASTGen/Sources/ASTGen/Decls.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,33 +119,47 @@ extension ASTGenVisitor {
}

public func visit(_ node: FunctionDeclSyntax) -> ASTNode {
let loc = self.base.advanced(by: node.position.utf8Offset).raw
let staticLoc = self.base.advanced(by: node.position.utf8Offset).raw
let funcLoc = self.base.advanced(by: node.funcKeyword.position.utf8Offset).raw
let nameLoc = self.base.advanced(by: node.identifier.position.utf8Offset).raw
let rParamLoc = self.base.advanced(by: node.signature.input.leftParen.position.utf8Offset).raw
let lParamLoc = self.base.advanced(by: node.signature.input.rightParen.position.utf8Offset).raw

var nameText = node.identifier.text
let name = nameText.withUTF8 { buf in
return SwiftASTContext_getIdentifier(ctx, buf.baseAddress, buf.count)
}

let returnType: ASTNode?
if let output = node.signature.output {
returnType = visit(output.returnType)
} else {
returnType = nil
}

let params = node.signature.input.parameterList.map { visit($0).rawValue }
let out = params.withBridgedArrayRef { ref in
FuncDecl_create(
ctx, staticLoc, false, funcLoc, name, nameLoc, false, nil, false, nil, rParamLoc, ref,
lParamLoc,
returnType?.rawValue, declContext)
}

let oldDeclContext = declContext
declContext = out.declContext
defer { declContext = oldDeclContext }

let body: ASTNode?
if let nodeBody = node.body {
body = visit(nodeBody)
} else {
body = nil
}

let returnType: ASTNode?
if let output = node.signature.output {
returnType = visit(output.returnType)
} else {
returnType = nil
if let body = body {
FuncDecl_setBody(out.funcDecl, body.rawValue)
}

let params = node.signature.input.parameterList.map { visit($0) }
return .decl(
params.withBridgedArrayRef { ref in
FuncDecl_create(
ctx, loc, false, loc, name, loc, false, nil, false, nil, loc, ref, loc, body?.rawValue,
returnType?.rawValue, declContext)
})
return .decl(out.decl)
}
}
11 changes: 5 additions & 6 deletions lib/ASTGen/Sources/ASTGen/Exprs.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import SwiftSyntax
extension ASTGenVisitor {
public func visit(_ node: ClosureExprSyntax) -> ASTNode {
let statements = node.statements.map { self.visit($0).bridged() }
let loc = self.base.advanced(by: node.position.utf8Offset).raw

let body = statements.withBridgedArrayRef { ref in
BraceStmt_create(ctx, loc, ref, loc)
let body: UnsafeMutableRawPointer = statements.withBridgedArrayRef { ref in
let startLoc = self.base.advanced(by: node.leftBrace.position.utf8Offset).raw
let endLoc = self.base.advanced(by: node.rightBrace.position.utf8Offset).raw
return BraceStmt_create(ctx, startLoc, ref, endLoc)
}

return .expr(ClosureExpr_create(ctx, body, declContext))
Expand Down Expand Up @@ -66,9 +66,8 @@ extension ASTGenVisitor {
public func visit(_ node: TupleExprElementListSyntax) -> ASTNode {
let elements = node.map { self.visit($0).rawValue }

// TODO: find correct paren locs.
let lParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
let rParenLoc = self.base.advanced(by: node.position.utf8Offset).raw
let rParenLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw

return .expr(
elements.withBridgedArrayRef { elementsRef in
Expand Down
4 changes: 2 additions & 2 deletions lib/ASTGen/Sources/ASTGen/Stmts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import SwiftSyntax
extension ASTGenVisitor {
public func visit(_ node: CodeBlockSyntax) -> ASTNode {
let statements = node.statements.map { self.visit($0).bridged() }
let startLoc = self.base.advanced(by: node.position.utf8Offset).raw
let endLoc = self.base.advanced(by: node.endPosition.utf8Offset).raw
let startLoc = self.base.advanced(by: node.leftBrace.position.utf8Offset).raw
let endLoc = self.base.advanced(by: node.rightBrace.position.utf8Offset).raw

return .stmt(
statements.withBridgedArrayRef { ref in
Expand Down