Skip to content

Commit 0801d47

Browse files
committed
[astgen] Add support for ArrayExpr.
1 parent 47b29b6 commit 0801d47

File tree

4 files changed

+33
-0
lines changed

4 files changed

+33
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
124124

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

127+
void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements,
128+
BridgedArrayRef commas, void *rLoc);
129+
127130
void *SwiftVarDecl_create(void *ctx, BridgedIdentifier _Nullable name,
128131
void *initExpr, void *loc, _Bool isStatic,
129132
_Bool isLet, void *dc);

lib/AST/CASTBridging.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,14 @@ void *SwiftIntegerLiteralExpr_create(void *ctx, const uint8_t *_Nullable string,
133133
getSourceLocFromPointer(TokenLoc));
134134
}
135135

136+
void *ArrayExpr_create(void *ctx, void *lLoc, BridgedArrayRef elements,
137+
BridgedArrayRef commas, void *rLoc) {
138+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
139+
return ArrayExpr::create(
140+
Context, getSourceLocFromPointer(lLoc), getArrayRef<Expr *>(elements),
141+
getArrayRef<SourceLoc>(commas), getSourceLocFromPointer(rLoc));
142+
}
143+
136144
void *SwiftBooleanLiteralExpr_create(void *ctx, bool value, void *TokenLoc) {
137145
ASTContext &Context = *static_cast<ASTContext *>(ctx);
138146
return new (Context)

lib/ASTGen/Sources/ASTGen/Literals.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,22 @@ extension ASTGenVisitor {
2626
let value = node.booleanLiteral == .trueKeyword()
2727
return .expr(SwiftBooleanLiteralExpr_create(ctx, value, loc))
2828
}
29+
30+
public func visit(_ node: ArrayExprSyntax) -> ASTNode {
31+
let lLoc = self.base.advanced(by: node.leftSquare.position.utf8Offset).raw
32+
let rLoc = self.base.advanced(by: node.rightSquare.position.utf8Offset).raw
33+
34+
let elements = node.elements.map { self.visit($0).rawValue }
35+
let commas = node.elements
36+
.compactMap { $0.trailingComma }
37+
.map {
38+
self.base.advanced(by: $0.position.utf8Offset).raw
39+
}
40+
41+
return elements.withBridgedArrayRef { elementsRef in
42+
commas.withBridgedArrayRef { commasRef in
43+
.expr(ArrayExpr_create(ctx, lLoc, elementsRef, commasRef, rLoc))
44+
}
45+
}
46+
}
2947
}

lib/ASTGen/Sources/ASTGen/Misc.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ extension ASTGenVisitor {
2222
public func visit(_ node: CodeBlockItemSyntax) -> ASTNode {
2323
visit(node.item)
2424
}
25+
26+
public func visit(_ node: ArrayElementSyntax) -> ASTNode {
27+
visit(node.expression)
28+
}
2529
}

0 commit comments

Comments
 (0)