Skip to content

Commit 345a7f8

Browse files
committed
ASTGen: Translate protocol declarations
1 parent a49bf19 commit 345a7f8

File tree

5 files changed

+80
-2
lines changed

5 files changed

+80
-2
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,11 @@ typedef struct BridgedTypeAttributes {
215215
void *raw;
216216
} BridgedTypeAttributes;
217217

218+
struct BridgedIdentifierAndSourceLoc {
219+
BridgedIdentifier name;
220+
BridgedSourceLoc nameLoc;
221+
};
222+
218223
#ifdef __cplusplus
219224
extern "C" {
220225

@@ -363,6 +368,13 @@ ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
363368
void *_Nullable opaqueGenericParamList,
364369
BridgedSourceRange cBraceRange);
365370

371+
BridgedDeclContextAndDecl
372+
ProtocolDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
373+
BridgedSourceLoc cProtocolKeywordLoc,
374+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
375+
BridgedArrayRef cPrimaryAssociatedTypeNames,
376+
BridgedSourceRange cBraceRange);
377+
366378
void *GenericParamList_create(BridgedASTContext cContext,
367379
BridgedSourceLoc cLAngleLoc,
368380
BridgedArrayRef params,

lib/AST/CASTBridging.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,29 @@ ClassDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
591591
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
592592
}
593593

594+
BridgedDeclContextAndDecl
595+
ProtocolDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
596+
BridgedSourceLoc cProtocolKeywordLoc,
597+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
598+
BridgedArrayRef cPrimaryAssociatedTypeNames,
599+
BridgedSourceRange cBraceRange) {
600+
SmallVector<PrimaryAssociatedTypeName, 2> primaryAssociatedTypeNames;
601+
for (auto &pair : convertArrayRef<BridgedIdentifierAndSourceLoc>(
602+
cPrimaryAssociatedTypeNames)) {
603+
primaryAssociatedTypeNames.emplace_back(convertIdentifier(pair.name),
604+
convertSourceLoc(pair.nameLoc));
605+
}
606+
607+
ASTContext &context = convertASTContext(cContext);
608+
auto *decl = new (context) ProtocolDecl(
609+
convertDeclContext(cDeclContext), convertSourceLoc(cProtocolKeywordLoc),
610+
convertSourceLoc(cNameLoc), convertIdentifier(cName),
611+
context.AllocateCopy(primaryAssociatedTypeNames), {}, nullptr);
612+
decl->setBraces(convertSourceRange(cBraceRange));
613+
614+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
615+
}
616+
594617
void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base,
595618
BridgedSourceLoc cQuestionLoc) {
596619
ASTContext &context = convertASTContext(cContext);

lib/ASTGen/Sources/ASTGen/Bridge.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,16 @@ extension TokenSyntax {
7676
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> (BridgedIdentifier, BridgedSourceLoc) {
7777
return (self.bridgedIdentifier(in: astgen), astgen.bridgedSourceLoc(for: self))
7878
}
79+
80+
/// Obtains a bridged, `ASTContext`-owned copy of this token's text, and its bridged start location in the
81+
/// source buffer provided by `astgen`.
82+
///
83+
/// - Parameter astgen: The visitor providing the `ASTContext` and source buffer.
84+
@inline(__always)
85+
func bridgedIdentifierAndSourceLoc(in astgen: ASTGenVisitor) -> BridgedIdentifierAndSourceLoc {
86+
let (name, nameLoc) = self.bridgedIdentifierAndSourceLoc(in: astgen)
87+
return .init(name: name, nameLoc: nameLoc)
88+
}
7989
}
8090

8191
extension Optional<TokenSyntax> {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,29 @@ extension ASTGenVisitor {
6363

6464
return .decl(decl.asDecl)
6565
}
66+
67+
func visit(_ node: ProtocolDeclSyntax) -> ASTNode {
68+
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
69+
let primaryAssociatedTypeNames = node.primaryAssociatedTypeClause?.primaryAssociatedTypes.lazy.map {
70+
$0.name.bridgedIdentifierAndSourceLoc(in: self) as BridgedIdentifierAndSourceLoc
71+
}
72+
73+
let decl = ProtocolDecl_create(
74+
self.ctx,
75+
self.declContext,
76+
self.bridgedSourceLoc(for: node.protocolKeyword),
77+
name,
78+
nameLoc,
79+
primaryAssociatedTypeNames.bridgedArray(in: self),
80+
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
81+
)
82+
83+
self.withDeclContext(decl.asDeclContext) {
84+
IterableDeclContext_setParsedMembers(self.visit(node.memberBlock.members), decl.asDecl)
85+
}
86+
87+
return .decl(decl.asDecl)
88+
}
6689
}
6790

6891
extension ASTGenVisitor {

test/ASTGen/verify-parse.swift

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ func test9(_ value: Any) { }
8181

8282
func test10<T>(t: T) {}
8383

84+
protocol Proto1 {}
85+
protocol Proto2 {}
86+
87+
protocol
88+
Proto3 {
89+
func method(_ b: Bool)
90+
}
91+
8492
typealias
8593
Alias<T>
8694
=
@@ -89,8 +97,10 @@ String
8997
struct
9098
Struct
9199
<
92-
T1,
93-
T2
100+
T1:
101+
Proto1,
102+
T2:
103+
Proto2
94104
>
95105
{
96106
/*static*/ func method(_ b: Bool) {}

0 commit comments

Comments
 (0)