Skip to content

Commit 6ee1976

Browse files
committed
ASTGen: Translate initializer declarations
1 parent 3ca622b commit 6ee1976

File tree

4 files changed

+82
-0
lines changed

4 files changed

+82
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,18 @@ BridgedDeclContextAndDecl FuncDecl_create(
359359
void *_Nullable opaqueReturnType,
360360
void *_Nullable opaqueGenericWhereClause);
361361

362+
BridgedDeclContextAndDecl ConstructorDecl_create(
363+
BridgedASTContext cContext,
364+
BridgedDeclContext cDeclContext,
365+
BridgedSourceLoc cInitKeywordLoc,
366+
BridgedSourceLoc cFailabilityMarkLoc,
367+
_Bool isIUO,
368+
void *_Nullable opaqueGenericParams,
369+
void *opaqueParameterList,
370+
BridgedSourceLoc cAsyncLoc,
371+
BridgedSourceLoc cThrowsLoc,
372+
void *_Nullable opaqueGenericWhereClause);
373+
362374
BridgedDeclContextAndDecl DestructorDecl_create(
363375
BridgedASTContext cContext,
364376
BridgedDeclContext cDeclContext,

lib/AST/CASTBridging.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,47 @@ BridgedDeclContextAndDecl FuncDecl_create(
481481
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
482482
}
483483

484+
BridgedDeclContextAndDecl ConstructorDecl_create(
485+
BridgedASTContext cContext,
486+
BridgedDeclContext cDeclContext,
487+
BridgedSourceLoc cInitKeywordLoc,
488+
BridgedSourceLoc cFailabilityMarkLoc,
489+
bool isIUO,
490+
void *_Nullable opaqueGenericParams,
491+
void *opaqueParameterList,
492+
BridgedSourceLoc cAsyncLoc,
493+
BridgedSourceLoc cThrowsLoc,
494+
void *_Nullable opaqueGenericWhereClause) {
495+
assert((bool)cFailabilityMarkLoc.raw || !isIUO);
496+
497+
ASTContext &context = convertASTContext(cContext);
498+
499+
auto *parameterList = static_cast<ParameterList *>(opaqueParameterList);
500+
auto declName =
501+
DeclName(context, DeclBaseName::createConstructor(), parameterList);
502+
auto asyncLoc = convertSourceLoc(cAsyncLoc);
503+
auto throwsLoc = convertSourceLoc(cThrowsLoc);
504+
auto failabilityMarkLoc = convertSourceLoc(cFailabilityMarkLoc);
505+
506+
auto *decl = new (context) ConstructorDecl(
507+
declName,
508+
convertSourceLoc(cInitKeywordLoc),
509+
failabilityMarkLoc.isValid(),
510+
failabilityMarkLoc,
511+
asyncLoc.isValid(),
512+
asyncLoc,
513+
throwsLoc.isValid(),
514+
throwsLoc,
515+
parameterList,
516+
static_cast<GenericParamList *>(opaqueGenericParams),
517+
convertDeclContext(cDeclContext));
518+
decl->setTrailingWhereClause(
519+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
520+
decl->setImplicitlyUnwrappedOptional(isIUO);
521+
522+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
523+
}
524+
484525
BridgedDeclContextAndDecl DestructorDecl_create(
485526
BridgedASTContext cContext,
486527
BridgedDeclContext cDeclContext,

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,29 @@ extension ASTGenVisitor {
296296
return .decl(declAndDeclContext.decl)
297297
}
298298

299+
func visit(_ node: InitializerDeclSyntax) -> ASTNode {
300+
let isIUO = node.optionalMark?.tokenKind == .exclamationMark
301+
302+
let declAndDeclContext = ConstructorDecl_create(
303+
self.ctx,
304+
self.declContext,
305+
self.bridgedSourceLoc(for: node.initKeyword),
306+
self.bridgedSourceLoc(for: node.optionalMark),
307+
isIUO,
308+
node.genericParameterClause.map(self.visit)?.rawValue,
309+
self.visit(node.signature.input).rawValue,
310+
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.asyncSpecifier),
311+
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.throwsSpecifier),
312+
node.genericWhereClause.map(self.visit)?.rawValue
313+
)
314+
315+
if let body = node.body {
316+
self.visitFunctionCodeBlock(body, declContextAndDecl: declAndDeclContext)
317+
}
318+
319+
return .decl(declAndDeclContext.decl)
320+
}
321+
299322
func visit(_ node: DeinitializerDeclSyntax) -> ASTNode {
300323
let declAndDeclContext = DestructorDecl_create(
301324
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)