Skip to content

Commit 3ca622b

Browse files
committed
ASTGen: Translate deinitializer declarations
1 parent 1d969c0 commit 3ca622b

File tree

4 files changed

+62
-24
lines changed

4 files changed

+62
-24
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,9 @@ void *ParamDecl_create(
343343
void *_Nullable opaqueType,
344344
void *_Nullable opaqueDefaultArgumentExpr);
345345

346-
struct BridgedFuncDecl FuncDecl_create(
346+
void AbstractFunctionDecl_setBody(void *opaqueDecl, void *opaqueBody);
347+
348+
BridgedDeclContextAndDecl FuncDecl_create(
347349
BridgedASTContext cContext,
348350
BridgedDeclContext cDeclContext,
349351
BridgedSourceLoc cStaticLoc,
@@ -356,7 +358,11 @@ struct BridgedFuncDecl FuncDecl_create(
356358
BridgedSourceLoc cThrowsLoc,
357359
void *_Nullable opaqueReturnType,
358360
void *_Nullable opaqueGenericWhereClause);
359-
void FuncDecl_setBody(void *fn, void *body);
361+
362+
BridgedDeclContextAndDecl DestructorDecl_create(
363+
BridgedASTContext cContext,
364+
BridgedDeclContext cDeclContext,
365+
BridgedSourceLoc cDeinitKeywordLoc);
360366

361367
void *SimpleIdentTypeRepr_create(BridgedASTContext cContext,
362368
BridgedSourceLoc cLoc, BridgedIdentifier id);

lib/AST/CASTBridging.cpp

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,14 @@ void *ParamDecl_create(
433433
return paramDecl;
434434
}
435435

436-
BridgedFuncDecl FuncDecl_create(
436+
void AbstractFunctionDecl_setBody(void *opaqueDecl, void *opaqueBody) {
437+
auto *decl = static_cast<Decl *>(opaqueDecl);
438+
439+
cast<AbstractFunctionDecl>(decl)->setBody(
440+
static_cast<BraceStmt *>(opaqueBody), FuncDecl::BodyKind::Parsed);
441+
}
442+
443+
BridgedDeclContextAndDecl FuncDecl_create(
437444
BridgedASTContext cContext,
438445
BridgedDeclContext cDeclContext,
439446
BridgedSourceLoc cStaticLoc,
@@ -453,7 +460,7 @@ BridgedFuncDecl FuncDecl_create(
453460
auto asyncLoc = convertSourceLoc(cAsyncLoc);
454461
auto throwsLoc = convertSourceLoc(cThrowsLoc);
455462

456-
auto *out = FuncDecl::create(
463+
auto *decl = FuncDecl::create(
457464
context,
458465
convertSourceLoc(cStaticLoc),
459466
StaticSpellingKind::None,
@@ -468,17 +475,21 @@ BridgedFuncDecl FuncDecl_create(
468475
paramList,
469476
static_cast<TypeRepr *>(opaqueReturnType),
470477
convertDeclContext(cDeclContext));
471-
out->setTrailingWhereClause(
478+
decl->setTrailingWhereClause(
472479
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
473480

474-
return {
475-
bridgeDeclContext(out),
476-
static_cast<FuncDecl *>(out),
477-
static_cast<Decl *>(out)};
481+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
478482
}
479483

480-
void FuncDecl_setBody(void *fn, void *body) {
481-
((FuncDecl *)fn)->setBody((BraceStmt *)body, FuncDecl::BodyKind::Parsed);
484+
BridgedDeclContextAndDecl DestructorDecl_create(
485+
BridgedASTContext cContext,
486+
BridgedDeclContext cDeclContext,
487+
BridgedSourceLoc cDeinitKeywordLoc) {
488+
ASTContext &context = convertASTContext(cContext);
489+
auto *decl = new (context) DestructorDecl(
490+
convertSourceLoc(cDeinitKeywordLoc), convertDeclContext(cDeclContext));
491+
492+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
482493
}
483494

484495
void *SimpleIdentTypeRepr_create(BridgedASTContext cContext,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,8 @@ extension ASTGenVisitor {
240240
}
241241
}
242242

243+
// MARK: - AbstractStorageDecl
244+
243245
extension ASTGenVisitor {
244246
public func visit(_ node: VariableDeclSyntax) -> ASTNode {
245247
let pattern = visit(node.bindings.first!.pattern).rawValue
@@ -261,14 +263,18 @@ extension ASTGenVisitor {
261263
)
262264
)
263265
}
266+
}
267+
268+
// MARK: - AbstractFunctionDecl
264269

270+
extension ASTGenVisitor {
265271
public func visit(_ node: FunctionDeclSyntax) -> ASTNode {
266272
// FIXME: Compute this location
267273
let staticLoc: BridgedSourceLoc = nil
268274

269275
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(for: node.identifier)
270276

271-
let out = FuncDecl_create(
277+
let declAndDeclContext = FuncDecl_create(
272278
self.ctx,
273279
self.declContext,
274280
staticLoc,
@@ -283,22 +289,33 @@ extension ASTGenVisitor {
283289
node.genericWhereClause.map(self.visit)?.rawValue
284290
)
285291

286-
let oldDeclContext = declContext
287-
declContext = out.declContext
288-
defer { declContext = oldDeclContext }
289-
290-
let body: ASTNode?
291-
if let nodeBody = node.body {
292-
body = visit(nodeBody)
293-
} else {
294-
body = nil
292+
if let body = node.body {
293+
self.visitFunctionCodeBlock(body, declContextAndDecl: declAndDeclContext)
295294
}
296295

297-
if let body = body {
298-
FuncDecl_setBody(out.funcDecl, body.rawValue)
296+
return .decl(declAndDeclContext.decl)
297+
}
298+
299+
func visit(_ node: DeinitializerDeclSyntax) -> ASTNode {
300+
let declAndDeclContext = DestructorDecl_create(
301+
self.ctx,
302+
self.declContext,
303+
self.bridgedSourceLoc(for: node.deinitKeyword)
304+
)
305+
306+
if let body = node.body {
307+
self.visitFunctionCodeBlock(body, declContextAndDecl: declAndDeclContext)
299308
}
300309

301-
return .decl(out.decl)
310+
return .decl(declAndDeclContext.decl)
311+
}
312+
313+
private func visitFunctionCodeBlock(_ block: CodeBlockSyntax, declContextAndDecl: BridgedDeclContextAndDecl) {
314+
let oldDeclContext = declContext
315+
declContext = declContextAndDecl.declContext
316+
defer { declContext = oldDeclContext }
317+
318+
AbstractFunctionDecl_setBody(declContextAndDecl.decl, self.visit(block).rawValue)
302319
}
303320
}
304321

test/ASTGen/verify-parse.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,10 @@ where
148148
class
149149
Class<T>: Proto1 where T: Proto3 {
150150
func method(_ b: Bool) {}
151+
152+
deinit {
153+
if true {}
154+
}
151155
}
152156

153157
actor

0 commit comments

Comments
 (0)