Skip to content

Commit fa1c9fd

Browse files
committed
ASTGen: Translate enum case declarations
1 parent 4a5325a commit fa1c9fd

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,17 @@ BridgedDeclContextAndDecl EnumDecl_create(
365365
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
366366
BridgedSourceRange cBraceRange);
367367

368+
void *EnumCaseDecl_create(BridgedDeclContext cDeclContext,
369+
BridgedSourceLoc cCaseKeywordLoc,
370+
BridgedArrayRef cElements);
371+
372+
void *EnumElementDecl_create(BridgedASTContext cContext,
373+
BridgedDeclContext cDeclContext,
374+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
375+
void *_Nullable opaqueParameterList,
376+
BridgedSourceLoc cEqualsLoc,
377+
void *_Nullable opaqueRawValue);
378+
368379
BridgedDeclContextAndDecl StructDecl_create(
369380
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
370381
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,

lib/AST/CASTBridging.cpp

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,18 @@ void IterableDeclContext_setParsedMembers(BridgedArrayRef bridgedMembers,
556556
void *opaqueDecl) {
557557
auto *decl = static_cast<Decl *>(opaqueDecl);
558558
auto &ctx = decl->getASTContext();
559-
auto members = convertArrayRef<Decl *>(bridgedMembers);
559+
560+
SmallVector<Decl *> members;
561+
for (auto *decl : convertArrayRef<Decl *>(bridgedMembers)) {
562+
members.push_back(decl);
563+
// Each enum case element is also part of the members list according to the
564+
// legacy parser.
565+
if (auto *ECD = dyn_cast<EnumCaseDecl>(decl)) {
566+
for (auto *EED : ECD->getElements()) {
567+
members.push_back(EED);
568+
}
569+
}
570+
}
560571

561572
ctx.evaluator.cacheOutput(
562573
ParseMembersRequest{cast<IterableDeclContext>(decl)},
@@ -594,6 +605,42 @@ BridgedDeclContextAndDecl EnumDecl_create(
594605
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
595606
}
596607

608+
void *EnumCaseDecl_create(BridgedDeclContext cDeclContext,
609+
BridgedSourceLoc cCaseKeywordLoc,
610+
BridgedArrayRef cElements) {
611+
auto *decl =
612+
EnumCaseDecl::create(convertSourceLoc(cCaseKeywordLoc),
613+
convertArrayRef<EnumElementDecl *>(cElements),
614+
convertDeclContext(cDeclContext));
615+
616+
return static_cast<Decl *>(decl);
617+
}
618+
619+
void *EnumElementDecl_create(BridgedASTContext cContext,
620+
BridgedDeclContext cDeclContext,
621+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
622+
void *_Nullable opaqueParameterList,
623+
BridgedSourceLoc cEqualsLoc,
624+
void *_Nullable opaqueRawValue) {
625+
ASTContext &context = convertASTContext(cContext);
626+
627+
auto *parameterList = static_cast<ParameterList *>(opaqueParameterList);
628+
DeclName declName;
629+
{
630+
auto identifier = convertIdentifier(cName);
631+
if (parameterList) {
632+
declName = DeclName(context, identifier, parameterList);
633+
} else {
634+
declName = identifier;
635+
}
636+
}
637+
638+
return new (context) EnumElementDecl(
639+
convertSourceLoc(cNameLoc), declName, parameterList,
640+
convertSourceLoc(cEqualsLoc), static_cast<LiteralExpr *>(opaqueRawValue),
641+
convertDeclContext(cDeclContext));
642+
}
643+
597644
BridgedDeclContextAndDecl StructDecl_create(
598645
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
599646
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,15 @@ extension ASTGenVisitor {
225225
return self.visit(node)
226226
}
227227

228+
@inline(__always)
229+
func visit(_ node: EnumCaseParameterClauseSyntax?) -> ASTNode? {
230+
guard let node else {
231+
return nil
232+
}
233+
234+
return self.visit(node)
235+
}
236+
228237
@inline(__always)
229238
func visit(_ node: InheritedTypeListSyntax?) -> BridgedArrayRef {
230239
guard let node else {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,36 @@ extension ASTGenVisitor {
158158
}
159159
}
160160

161+
// MARK: - EnumCaseDecl
162+
163+
extension ASTGenVisitor {
164+
func visit(_ node: EnumCaseElementSyntax) -> ASTNode {
165+
let (name, nameLoc) = node.name.bridgedIdentifierAndSourceLoc(in: self)
166+
167+
return .decl(
168+
EnumElementDecl_create(
169+
self.ctx,
170+
self.declContext,
171+
name,
172+
nameLoc,
173+
self.visit(node.parameterClause)?.rawValue,
174+
self.bridgedSourceLoc(for: node.rawValue?.equal),
175+
self.visit(node.rawValue?.value)?.rawValue
176+
)
177+
)
178+
}
179+
180+
func visit(_ node: EnumCaseDeclSyntax) -> ASTNode {
181+
.decl(
182+
EnumCaseDecl_create(
183+
self.declContext,
184+
self.bridgedSourceLoc(for: node.caseKeyword),
185+
node.elements.lazy.map { self.visit($0).rawValue }.bridgedArray(in: self)
186+
)
187+
)
188+
}
189+
}
190+
161191
extension ASTGenVisitor {
162192
public func visit(_ node: VariableDeclSyntax) -> ASTNode {
163193
let pattern = visit(node.bindings.first!.pattern).rawValue

test/ASTGen/verify-parse.swift

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,14 @@ Alias<T>
105105
String where T: Proto1
106106

107107
enum
108-
Enum<T>: Proto1 where T: Proto1 {
108+
Enum<T>: Int, Proto1 where T: Proto1 {
109+
case
110+
a
111+
=
112+
1
113+
case b = 2,
114+
c
115+
109116
func method(_ b: Bool) {}
110117
}
111118

0 commit comments

Comments
 (0)