Skip to content

Commit 5e46243

Browse files
committed
ASTGen: Translate initializer declarations
1 parent 58ce5d1 commit 5e46243

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,13 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
337337
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType,
338338
void *_Nullable opaqueGenericWhereClause);
339339

340+
BridgedDeclContextAndDecl ConstructorDecl_create(
341+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
342+
BridgedSourceLoc cInitKeywordLoc, BridgedSourceLoc cFailabilityMarkLoc,
343+
_Bool isIUO, void *_Nullable opaqueGenericParams, void *opaqueParameterList,
344+
BridgedSourceLoc cAsyncLoc, BridgedSourceLoc cThrowsLoc,
345+
void *_Nullable opaqueGenericWhereClause);
346+
340347
BridgedDeclContextAndDecl
341348
DestructorDecl_create(BridgedASTContext cContext,
342349
BridgedDeclContext cDeclContext,

lib/AST/CASTBridging.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,36 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
477477
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
478478
}
479479

480+
BridgedDeclContextAndDecl ConstructorDecl_create(
481+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
482+
BridgedSourceLoc cInitKeywordLoc, BridgedSourceLoc cFailabilityMarkLoc,
483+
bool isIUO, void *_Nullable opaqueGenericParams, void *opaqueParameterList,
484+
BridgedSourceLoc cAsyncLoc, BridgedSourceLoc cThrowsLoc,
485+
void *_Nullable opaqueGenericWhereClause) {
486+
assert((bool)cFailabilityMarkLoc.raw || !isIUO);
487+
488+
ASTContext &context = convertASTContext(cContext);
489+
490+
auto *parameterList = static_cast<ParameterList *>(opaqueParameterList);
491+
auto declName =
492+
DeclName(context, DeclBaseName::createConstructor(), parameterList);
493+
auto asyncLoc = convertSourceLoc(cAsyncLoc);
494+
auto throwsLoc = convertSourceLoc(cThrowsLoc);
495+
auto failabilityMarkLoc = convertSourceLoc(cFailabilityMarkLoc);
496+
497+
auto *decl = new (context) ConstructorDecl(
498+
declName, convertSourceLoc(cInitKeywordLoc), failabilityMarkLoc.isValid(),
499+
failabilityMarkLoc, asyncLoc.isValid(), asyncLoc, throwsLoc.isValid(),
500+
throwsLoc, parameterList,
501+
static_cast<GenericParamList *>(opaqueGenericParams),
502+
convertDeclContext(cDeclContext));
503+
decl->setTrailingWhereClause(
504+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
505+
decl->setImplicitlyUnwrappedOptional(isIUO);
506+
507+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
508+
}
509+
480510
BridgedDeclContextAndDecl
481511
DestructorDecl_create(BridgedASTContext cContext,
482512
BridgedDeclContext cDeclContext,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,29 @@ extension ASTGenVisitor {
268268
return .decl(decl.asDecl)
269269
}
270270

271+
func visit(_ node: InitializerDeclSyntax) -> ASTNode {
272+
let decl = ConstructorDecl_create(
273+
self.ctx,
274+
self.declContext,
275+
self.bridgedSourceLoc(for: node.initKeyword),
276+
self.bridgedSourceLoc(for: node.optionalMark),
277+
node.optionalMark?.tokenKind == .exclamationMark,
278+
self.visit(node.genericParameterClause)?.rawValue,
279+
self.visit(node.signature.parameterClause).rawValue,
280+
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.asyncSpecifier),
281+
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.throwsSpecifier),
282+
self.visit(node.genericWhereClause)?.rawValue
283+
)
284+
285+
if let body = node.body {
286+
self.withDeclContext(decl.asDeclContext) {
287+
AbstractFunctionDecl_setBody(self.visit(body).rawValue, decl.asDecl)
288+
}
289+
}
290+
291+
return .decl(decl.asDecl)
292+
}
293+
271294
func visit(_ node: DeinitializerDeclSyntax) -> ASTNode {
272295
let decl = DestructorDecl_create(
273296
self.ctx,

test/ASTGen/verify-parse.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ Class<T>: Proto1 where T: Proto3 {
152152
deinit {
153153
if true {}
154154
}
155+
156+
init?<U>(_ u: U) where U: Proto1 {
157+
if true {}
158+
}
159+
160+
init!(i: Int) {}
155161
}
156162

157163
actor

0 commit comments

Comments
 (0)