Skip to content

Commit 39e9948

Browse files
committed
Make ASTGen a swift package
1 parent b7e88f4 commit 39e9948

File tree

9 files changed

+84
-3
lines changed

9 files changed

+84
-3
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ void *SimpleIdentTypeRepr_create(void *ctx, void *loc, const char *id);
126126

127127
void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *name, void *nameLoc);
128128

129+
void *ClosureExpr_create(void *ctx, void *body, void *dc);
130+
129131
void TopLevelCodeDecl_dump(void *);
130132
void Expr_dump(void *);
131133
void Decl_dump(void *);

lib/AST/CASTBridging.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,22 @@ void *UnresolvedDotExpr_create(void *ctx, void *base, void *dotLoc, const char *
195195
DeclNameLoc(*(SourceLoc *)&nameLoc), false);
196196
}
197197

198+
void *ClosureExpr_create(void *ctx, void *body, void *dc) {
199+
DeclAttributes attributes;
200+
SourceRange bracketRange;
201+
SourceLoc asyncLoc;
202+
SourceLoc throwsLoc;
203+
SourceLoc arrowLoc;
204+
SourceLoc inLoc;
205+
206+
ASTContext &Context = *static_cast<ASTContext *>(ctx);
207+
auto *out = new (Context) ClosureExpr(attributes, bracketRange, nullptr,
208+
nullptr, asyncLoc, throwsLoc, arrowLoc,
209+
inLoc, nullptr, 0, (DeclContext *)dc);
210+
out->setBody((BraceStmt *)body, true);
211+
return (Expr *)out;
212+
}
213+
198214
void TopLevelCodeDecl_dump(void *decl) { ((TopLevelCodeDecl *)decl)->dump(); }
199215

200216
void Expr_dump(void *expr) { ((Expr *)expr)->dump(); }

lib/ASTGen/.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ASTGen/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set(CMAKE_Swift_ARCHIVE_FINISH "")
44

55
if (SWIFT_SWIFT_PARSER)
66
add_library(ASTGen
7-
ASTGen.swift)
7+
Sources/ASTGen/ASTGen.swift)
88

99
target_link_libraries(ASTGen PUBLIC
1010
SwiftSyntax
@@ -18,12 +18,13 @@ if (SWIFT_SWIFT_PARSER)
1818
"${SWIFT_PATH_TO_EARLYSWIFTSYNTAX_BUILD_DIR}/swift")
1919
else()
2020
add_library(ASTGen
21-
DummyASTGen.swift)
21+
Sources/ASTGen/DummyASTGen.swift)
2222
endif()
2323

2424
set_target_properties(ASTGen PROPERTIES LINKER_LANGUAGE Swift)
2525

2626
target_compile_options(ASTGen PUBLIC
27-
"-emit-module-interface")
27+
"-emit-module-interface"
28+
"-g")
2829

2930
add_dependencies(ASTGen swiftAST)

lib/ASTGen/Package.resolved

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/ASTGen/Package.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// swift-tools-version: 5.6
2+
3+
import PackageDescription
4+
5+
let package = Package(
6+
name: "ASTGen",
7+
products: [
8+
.library(name: "ASTGen", targets: ["ASTGen"]),
9+
],
10+
dependencies: [
11+
.package(path: "../../../swift-syntax"),
12+
],
13+
targets: [
14+
.target(
15+
name: "ASTGen",
16+
dependencies: ["SwiftSyntax"]),
17+
]
18+
)

lib/ASTGen/ASTGen.swift renamed to lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,30 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
5757

5858
return out
5959
}
60+
61+
public func visit(_ node: ClosureExprSyntax) -> UnsafeMutableRawPointer {
62+
let statements = node.statements.map(self.visit)
63+
let loc = self.base.advanced(by: node.position.utf8Offset).raw
64+
65+
let body = statements.withBridgedArrayRef { ref in
66+
BraceStmt_create(ctx, loc, ref, loc)
67+
}
68+
69+
return ClosureExpr_create(ctx, body, declContext)
70+
}
6071

6172
public func visit(_ node: FunctionCallExprSyntax) -> UnsafeMutableRawPointer {
73+
// Transform the trailing closure into an argument.
74+
if let trailingClosure = node.trailingClosure {
75+
let tupleElement = TupleExprElementSyntax(label: nil, colon: nil, expression: ExprSyntax(trailingClosure), trailingComma: nil)
76+
77+
return visit(node.addArgument(tupleElement).withTrailingClosure(nil))
78+
}
79+
6280
let args = visit(node.argumentList)
6381
// TODO: hack
6482
let callee = visit(node.calledExpression)
83+
6584
return SwiftFunctionCallExpr_create(self.ctx, callee, args)
6685
}
6786

@@ -125,6 +144,10 @@ struct ASTGenVisitor: SyntaxTransformVisitor {
125144
}
126145
}
127146

147+
public func visit(_ node: InitializerClauseSyntax) -> UnsafeMutableRawPointer {
148+
visit(node.value)
149+
}
150+
128151
public func visit(_ node: VariableDeclSyntax) -> UnsafeMutableRawPointer {
129152
let pattern = visit(node.bindings.first!.pattern)
130153
let initializer = visit(node.bindings.first!.initializer!)

0 commit comments

Comments
 (0)