Skip to content

Commit

Permalink
Merge pull request swiftlang#61834 from zoecarver/astgen-array-literal
Browse files Browse the repository at this point in the history
  • Loading branch information
zoecarver authored Nov 1, 2022
2 parents 8ca67c7 + 0801d47 commit 60c8b4b
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
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 @@ -135,6 +135,14 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
IntegerLiteralExpr(stringRef, 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)
}
}

0 comments on commit 60c8b4b

Please sign in to comment.