Skip to content

Commit 58ce5d1

Browse files
committed
ASTGen: Translate deinitializer declarations
1 parent 96f13f5 commit 58ce5d1

File tree

4 files changed

+58
-13
lines changed

4 files changed

+58
-13
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,15 +326,21 @@ ParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
326326
BridgedSourceLoc cSecondNameLoc, void *_Nullable opaqueType,
327327
void *_Nullable opaqueDefaultValue);
328328

329-
struct BridgedFuncDecl
329+
void AbstractFunctionDecl_setBody(void *opaqueBody, void *opaqueDecl);
330+
331+
struct BridgedDeclContextAndDecl
330332
FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
331333
BridgedSourceLoc cStaticLoc, BridgedSourceLoc cFuncKeywordLoc,
332334
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
333335
void *_Nullable opaqueGenericParamList,
334336
void *opaqueParameterList, BridgedSourceLoc cAsyncLoc,
335337
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType,
336338
void *_Nullable opaqueGenericWhereClause);
337-
void FuncDecl_setBody(void *fn, void *body);
339+
340+
BridgedDeclContextAndDecl
341+
DestructorDecl_create(BridgedASTContext cContext,
342+
BridgedDeclContext cDeclContext,
343+
BridgedSourceLoc cDeinitKeywordLoc);
338344

339345
void *SimpleIdentTypeRepr_create(BridgedASTContext cContext,
340346
BridgedSourceLoc cLoc, BridgedIdentifier id);

lib/AST/CASTBridging.cpp

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,14 @@ ParamDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
442442
return paramDecl;
443443
}
444444

445-
BridgedFuncDecl
445+
void AbstractFunctionDecl_setBody(void *opaqueBody, void *opaqueDecl) {
446+
auto *decl = static_cast<Decl *>(opaqueDecl);
447+
448+
cast<AbstractFunctionDecl>(decl)->setBody(
449+
static_cast<BraceStmt *>(opaqueBody), FuncDecl::BodyKind::Parsed);
450+
}
451+
452+
BridgedDeclContextAndDecl
446453
FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
447454
BridgedSourceLoc cStaticLoc, BridgedSourceLoc cFuncKeywordLoc,
448455
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
@@ -457,22 +464,28 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
457464
auto asyncLoc = convertSourceLoc(cAsyncLoc);
458465
auto throwsLoc = convertSourceLoc(cThrowsLoc);
459466

460-
auto *out = FuncDecl::create(
467+
auto *decl = FuncDecl::create(
461468
context, convertSourceLoc(cStaticLoc), StaticSpellingKind::None,
462469
convertSourceLoc(cFuncKeywordLoc), declName, convertSourceLoc(cNameLoc),
463470
asyncLoc.isValid(), asyncLoc, throwsLoc.isValid(), throwsLoc,
464471
static_cast<GenericParamList *>(opaqueGenericParamList), paramList,
465472
static_cast<TypeRepr *>(opaqueReturnType),
466473
convertDeclContext(cDeclContext));
467-
out->setTrailingWhereClause(
474+
decl->setTrailingWhereClause(
468475
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
469476

470-
return {bridgeDeclContext(out), static_cast<FuncDecl *>(out),
471-
static_cast<Decl *>(out)};
477+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
472478
}
473479

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

478491
void *SimpleIdentTypeRepr_create(BridgedASTContext cContext,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,8 @@ extension ASTGenVisitor {
211211
}
212212
}
213213

214+
// MARK: - AbstractStorageDecl
215+
214216
extension ASTGenVisitor {
215217
public func visit(_ node: VariableDeclSyntax) -> ASTNode {
216218
let pattern = visit(node.bindings.first!.pattern).rawValue
@@ -231,14 +233,18 @@ extension ASTGenVisitor {
231233
)
232234
)
233235
}
236+
}
234237

238+
// MARK: - AbstractFunctionDecl
239+
240+
extension ASTGenVisitor {
235241
public func visit(_ node: FunctionDeclSyntax) -> ASTNode {
236242
// FIXME: Compute this location
237243
let staticLoc: BridgedSourceLoc = nil
238244

239245
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
240246

241-
let out = FuncDecl_create(
247+
let decl = FuncDecl_create(
242248
self.ctx,
243249
self.declContext,
244250
staticLoc,
@@ -254,12 +260,28 @@ extension ASTGenVisitor {
254260
)
255261

256262
if let body = node.body {
257-
self.withDeclContext(out.declContext) {
258-
FuncDecl_setBody(out.funcDecl, self.visit(body).rawValue)
263+
self.withDeclContext(decl.asDeclContext) {
264+
AbstractFunctionDecl_setBody(self.visit(body).rawValue, decl.asDecl)
265+
}
266+
}
267+
268+
return .decl(decl.asDecl)
269+
}
270+
271+
func visit(_ node: DeinitializerDeclSyntax) -> ASTNode {
272+
let decl = DestructorDecl_create(
273+
self.ctx,
274+
self.declContext,
275+
self.bridgedSourceLoc(for: node.deinitKeyword)
276+
)
277+
278+
if let body = node.body {
279+
self.withDeclContext(decl.asDeclContext) {
280+
AbstractFunctionDecl_setBody(self.visit(body).rawValue, decl.asDecl)
259281
}
260282
}
261283

262-
return .decl(out.decl)
284+
return .decl(decl.asDecl)
263285
}
264286
}
265287

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)