Skip to content

[astgen] Add support for ArrayExpr. #61834

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 1 commit 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
3 changes: 3 additions & 0 deletions include/swift/AST/CASTBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,

void *SwiftBooleanLiteralExpr_create(void *ctx, _Bool value, void *TokenLoc);

void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements,
BridgedArrayRef commas, void *rLoc);

void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
void *initExpr, void *loc, _Bool isStatic,
_Bool isLet, void *dc);
Expand Down
8 changes: 8 additions & 0 deletions lib/AST/CASTBridging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,14 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
getSourceLocFromPointer(TokenLoc));
}

void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements,
BridgedArrayRef commas, void *rLoc) {
ASTContext &Context = *static_cast<ASTContext *>(ctx);
return ArrayExpr::create(
Context, getSourceLocFromPointer(lLoc), getArrayRef<Expr *>(elements),
getArrayRef<SourceLoc>(commas), getSourceLocFromPointer(rLoc));
}

void *SwiftBooleanLiteralExpr_create(void *ctx, bool value, void *TokenLoc) {
ASTContext &Context = *static_cast<ASTContext *>(ctx);
return new (Context)
Expand Down
18 changes: 18 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Literals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,22 @@ extension ASTGenVisitor {
let value = node.booleanLiteral == .trueKeyword()
return .expr(SwiftBooleanLiteralExpr_create(ctx, value, loc))
}

public func visit(_ node: ArrayExprSyntax) -> ASTNode {
let lLoc = self.base.advanced(by: node.leftSquare.position.utf8Offset).raw
let rLoc = self.base.advanced(by: node.rightSquare.position.utf8Offset).raw

let elements = node.elements.map { self.visit($0).rawValue }
let commas = node.elements
.compactMap { $0.trailingComma }
.map {
self.base.advanced(by: $0.position.utf8Offset).raw
}

return elements.withBridgedArrayRef { elementsRef in
commas.withBridgedArrayRef { commasRef in
.expr(ArrayExpr_create(ctx, lLoc, elementsRef, commasRef, rLoc))
}
}
}
}
4 changes: 4 additions & 0 deletions lib/ASTGen/Sources/ASTGen/Misc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ extension ASTGenVisitor {
public func visit(_ node: CodeBlockItemSyntax) -> ASTNode {
visit(node.item)
}

public func visit(_ node: ArrayElementSyntax) -> ASTNode {
visit(node.expression)
}
}