Skip to content

Commit da427ad

Browse files
committed
ASTGen: Translate inheritance clauses
1 parent 345a7f8 commit da427ad

File tree

6 files changed

+111
-85
lines changed

6 files changed

+111
-85
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -354,40 +354,37 @@ void *TypeAliasDecl_create(
354354
void IterableDeclContext_setParsedMembers(BridgedArrayRef members,
355355
void *opaqueDecl);
356356

357-
BridgedDeclContextAndDecl
358-
StructDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
359-
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
360-
BridgedSourceLoc cNameLoc,
361-
void *_Nullable opaqueGenericParamList,
362-
BridgedSourceRange cBraceRange);
363-
364-
BridgedDeclContextAndDecl
365-
ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
366-
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
367-
BridgedSourceLoc cNameLoc,
368-
void *_Nullable opaqueGenericParamList,
369-
BridgedSourceRange cBraceRange);
370-
371-
BridgedDeclContextAndDecl
372-
ProtocolDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
373-
BridgedSourceLoc cProtocolKeywordLoc,
374-
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
375-
BridgedArrayRef cPrimaryAssociatedTypeNames,
376-
BridgedSourceRange cBraceRange);
357+
BridgedDeclContextAndDecl StructDecl_create(
358+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
359+
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
360+
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
361+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
362+
363+
BridgedDeclContextAndDecl ClassDecl_create(
364+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
365+
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
366+
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
367+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
368+
369+
BridgedDeclContextAndDecl ProtocolDecl_create(
370+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
371+
BridgedSourceLoc cProtocolKeywordLoc, BridgedIdentifier cName,
372+
BridgedSourceLoc cNameLoc, BridgedArrayRef cPrimaryAssociatedTypeNames,
373+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
377374

378375
void *GenericParamList_create(BridgedASTContext cContext,
379376
BridgedSourceLoc cLAngleLoc,
380377
BridgedArrayRef params,
381378
BridgedSourceLoc cWhereLoc, BridgedArrayRef reqs,
382379
BridgedSourceLoc cRAngleLoc);
380+
383381
void *GenericTypeParamDecl_create(BridgedASTContext cContext,
384382
BridgedDeclContext cDeclContext,
385-
BridgedIdentifier name,
383+
BridgedSourceLoc cEachLoc,
384+
BridgedIdentifier cName,
386385
BridgedSourceLoc cNameLoc,
387-
BridgedSourceLoc cEachLoc, SwiftInt index,
388-
_Bool isParameterPack);
389-
void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext,
390-
void *Param, void *ty);
386+
void *_Nullable opaqueInheritedType,
387+
SwiftInt index);
391388

392389
void *ParameterList_create(BridgedASTContext cContext,
393390
BridgedSourceLoc cLeftParenLoc,

lib/AST/CASTBridging.cpp

Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -555,48 +555,57 @@ void IterableDeclContext_setParsedMembers(BridgedArrayRef bridgedMembers,
555555
FingerprintAndMembers{llvm::None, ctx.AllocateCopy(members)});
556556
}
557557

558-
BridgedDeclContextAndDecl
559-
StructDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
560-
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
561-
BridgedSourceLoc cNameLoc,
562-
void *_Nullable opaqueGenericParamList,
563-
BridgedSourceRange cBraceRange) {
564-
ASTContext &context = convertASTContext(cContext);
565-
566-
auto *decl = new (context)
567-
StructDecl(convertSourceLoc(cStructKeywordLoc), convertIdentifier(cName),
568-
convertSourceLoc(cNameLoc), {},
569-
static_cast<GenericParamList *>(opaqueGenericParamList),
570-
convertDeclContext(cDeclContext));
558+
static SmallVector<InheritedEntry>
559+
convertToInheritedEntries(BridgedArrayRef cInheritedTypes) {
560+
SmallVector<InheritedEntry> inheritedEntries;
561+
for (auto &repr : convertArrayRef<TypeRepr *>(cInheritedTypes)) {
562+
inheritedEntries.emplace_back(repr);
563+
}
564+
565+
return inheritedEntries;
566+
}
567+
568+
BridgedDeclContextAndDecl StructDecl_create(
569+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
570+
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
571+
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
572+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
573+
ASTContext &context = convertASTContext(cContext);
574+
575+
auto *decl = new (context) StructDecl(
576+
convertSourceLoc(cStructKeywordLoc), convertIdentifier(cName),
577+
convertSourceLoc(cNameLoc),
578+
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
579+
static_cast<GenericParamList *>(opaqueGenericParamList),
580+
convertDeclContext(cDeclContext));
571581
decl->setBraces(convertSourceRange(cBraceRange));
572582

573583
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
574584
}
575585

576-
BridgedDeclContextAndDecl
577-
ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
578-
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
579-
BridgedSourceLoc cNameLoc,
580-
void *_Nullable opaqueGenericParamList,
581-
BridgedSourceRange cBraceRange) {
586+
BridgedDeclContextAndDecl ClassDecl_create(
587+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
588+
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
589+
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
590+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
582591
ASTContext &context = convertASTContext(cContext);
583592

584-
auto *decl = new (context)
585-
ClassDecl(convertSourceLoc(cClassKeywordLoc), convertIdentifier(cName),
586-
convertSourceLoc(cNameLoc), {},
587-
static_cast<GenericParamList *>(opaqueGenericParamList),
588-
convertDeclContext(cDeclContext), false);
593+
auto *decl = new (context) ClassDecl(
594+
convertSourceLoc(cClassKeywordLoc), convertIdentifier(cName),
595+
convertSourceLoc(cNameLoc),
596+
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
597+
static_cast<GenericParamList *>(opaqueGenericParamList),
598+
convertDeclContext(cDeclContext), false);
589599
decl->setBraces(convertSourceRange(cBraceRange));
590600

591601
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
592602
}
593603

594-
BridgedDeclContextAndDecl
595-
ProtocolDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
596-
BridgedSourceLoc cProtocolKeywordLoc,
597-
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
598-
BridgedArrayRef cPrimaryAssociatedTypeNames,
599-
BridgedSourceRange cBraceRange) {
604+
BridgedDeclContextAndDecl ProtocolDecl_create(
605+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
606+
BridgedSourceLoc cProtocolKeywordLoc, BridgedIdentifier cName,
607+
BridgedSourceLoc cNameLoc, BridgedArrayRef cPrimaryAssociatedTypeNames,
608+
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
600609
SmallVector<PrimaryAssociatedTypeName, 2> primaryAssociatedTypeNames;
601610
for (auto &pair : convertArrayRef<BridgedIdentifierAndSourceLoc>(
602611
cPrimaryAssociatedTypeNames)) {
@@ -608,7 +617,9 @@ ProtocolDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
608617
auto *decl = new (context) ProtocolDecl(
609618
convertDeclContext(cDeclContext), convertSourceLoc(cProtocolKeywordLoc),
610619
convertSourceLoc(cNameLoc), convertIdentifier(cName),
611-
context.AllocateCopy(primaryAssociatedTypeNames), {}, nullptr);
620+
context.AllocateCopy(primaryAssociatedTypeNames),
621+
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
622+
nullptr);
612623
decl->setBraces(convertSourceRange(cBraceRange));
613624

614625
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
@@ -862,22 +873,24 @@ void *GenericParamList_create(BridgedASTContext cContext,
862873

863874
void *GenericTypeParamDecl_create(BridgedASTContext cContext,
864875
BridgedDeclContext cDeclContext,
865-
BridgedIdentifier name,
876+
BridgedSourceLoc cEachLoc,
877+
BridgedIdentifier cName,
866878
BridgedSourceLoc cNameLoc,
867-
BridgedSourceLoc cEachLoc, SwiftInt index,
868-
bool isParameterPack) {
869-
return GenericTypeParamDecl::createParsed(
870-
convertDeclContext(cDeclContext), convertIdentifier(name),
871-
convertSourceLoc(cNameLoc), convertSourceLoc(cEachLoc),
872-
/*index*/ index, isParameterPack);
873-
}
879+
void *_Nullable opaqueInheritedType,
880+
SwiftInt index) {
881+
auto eachLoc = convertSourceLoc(cEachLoc);
882+
auto *decl = GenericTypeParamDecl::createParsed(
883+
convertDeclContext(cDeclContext), convertIdentifier(cName),
884+
convertSourceLoc(cNameLoc), eachLoc, index,
885+
/*isParameterPack*/ eachLoc.isValid());
886+
887+
if (opaqueInheritedType) {
888+
auto entry = InheritedEntry(static_cast<TypeRepr *>(opaqueInheritedType));
889+
ASTContext &context = convertASTContext(cContext);
890+
decl->setInherited(context.AllocateCopy(llvm::makeArrayRef(entry)));
891+
}
874892

875-
void GenericTypeParamDecl_setInheritedType(BridgedASTContext cContext,
876-
void *param, void *ty) {
877-
ASTContext &context = convertASTContext(cContext);
878-
auto entries = context.AllocateCopy(
879-
ArrayRef<InheritedEntry>{InheritedEntry{(TypeRepr *)ty}});
880-
((GenericTypeParamDecl *)param)->setInherited(entries);
893+
return decl;
881894
}
882895

883896
void *ParameterList_create(BridgedASTContext cContext,

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ extension ASTGenVisitor {
215215

216216
return self.visit(node)
217217
}
218+
219+
@inline(__always)
220+
func visit(_ node: InheritedTypeListSyntax?) -> BridgedArrayRef {
221+
guard let node else {
222+
return .init()
223+
}
224+
225+
return self.visit(node)
226+
}
218227
}
219228

220229
extension Collection {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ extension ASTGenVisitor {
3434
name,
3535
nameLoc,
3636
self.visit(node.genericParameterClause)?.rawValue,
37+
self.visit(node.inheritanceClause?.inheritedTypes),
3738
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
3839
)
3940

@@ -54,6 +55,7 @@ extension ASTGenVisitor {
5455
name,
5556
nameLoc,
5657
self.visit(node.genericParameterClause)?.rawValue,
58+
self.visit(node.inheritanceClause?.inheritedTypes),
5759
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
5860
)
5961

@@ -77,6 +79,7 @@ extension ASTGenVisitor {
7779
name,
7880
nameLoc,
7981
primaryAssociatedTypeNames.bridgedArray(in: self),
82+
self.visit(node.inheritanceClause?.inheritedTypes),
8083
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
8184
)
8285

@@ -144,4 +147,9 @@ extension ASTGenVisitor {
144147
func visit(_ node: MemberBlockItemListSyntax) -> BridgedArrayRef {
145148
node.lazy.map { self.visit($0).rawValue }.bridgedArray(in: self)
146149
}
150+
151+
@inline(__always)
152+
func visit(_ node: InheritedTypeListSyntax) -> BridgedArrayRef {
153+
node.lazy.map { self.visit($0.type).rawValue }.bridgedArray(in: self)
154+
}
147155
}

lib/ASTGen/Sources/ASTGen/Generics.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ extension ASTGenVisitor {
1818

1919
func visit(_ node: GenericParameterSyntax) -> ASTNode {
2020
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
21-
let eachLoc = bridgedSourceLoc(for: node.eachKeyword)
2221

2322
var genericParameterIndex: Int?
2423
for (index, sibling) in (node.parent?.as(GenericParameterListSyntax.self) ?? []).enumerated() {
@@ -33,8 +32,15 @@ extension ASTGenVisitor {
3332

3433
return .decl(
3534
GenericTypeParamDecl_create(
36-
self.ctx, self.declContext, name, nameLoc, eachLoc, SwiftInt(genericParameterIndex),
37-
eachLoc.raw != nil))
35+
self.ctx,
36+
self.declContext,
37+
self.bridgedSourceLoc(for: node.eachKeyword),
38+
name,
39+
nameLoc,
40+
self.visit(node.inheritedType)?.rawValue,
41+
SwiftInt(genericParameterIndex)
42+
)
43+
)
3844
}
3945
}
4046

@@ -43,16 +49,7 @@ extension ASTGenVisitor {
4349
_ node: GenericParameterClauseSyntax,
4450
action: (BridgedArrayRef, BridgedArrayRef) -> T
4551
) -> T {
46-
let parameters = node.parameters.lazy.map {
47-
let loweredParameter = self.visit($0).rawValue
48-
49-
if let inheritedType = $0.inheritedType {
50-
let loweredInheritedType = self.visit(inheritedType).rawValue
51-
GenericTypeParamDecl_setInheritedType(self.ctx, loweredParameter, loweredInheritedType)
52-
}
53-
54-
return loweredParameter
55-
}
52+
let parameters = node.parameters.lazy.map { self.visit($0).rawValue }
5653

5754
let requirements = node.genericWhereClause?.requirements.lazy.map {
5855
switch $0.requirement {

test/ASTGen/verify-parse.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ protocol Proto1 {}
8585
protocol Proto2 {}
8686

8787
protocol
88-
Proto3 {
88+
Proto3: Proto1 {
8989
func method(_ b: Bool)
9090
}
9191

@@ -102,11 +102,13 @@ Struct
102102
T2:
103103
Proto2
104104
>
105+
:
106+
Proto1, Proto2, @unchecked Sendable
105107
{
106108
/*static*/ func method(_ b: Bool) {}
107109
}
108110

109111
class
110-
Class<T> {
112+
Class<T>: Proto1 {
111113
func method(_ b: Bool) {}
112114
}

0 commit comments

Comments
 (0)