Skip to content

Commit de155c9

Browse files
committed
ASTGen: Translate extension declarations
1 parent 76e2cdc commit de155c9

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,12 @@ void *AssociatedTypeDecl_create(BridgedASTContext cContext,
406406
void *_Nullable opaqueDefaultType,
407407
void *_Nullable opaqueGenericWhereClause);
408408

409+
BridgedDeclContextAndDecl ExtensionDecl_create(
410+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
411+
BridgedSourceLoc cExtensionKeywordLoc, void *opaqueExtendedType,
412+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
413+
BridgedSourceRange cBraceRange);
414+
409415
void *GenericParamList_create(BridgedASTContext cContext,
410416
BridgedSourceLoc cLeftAngleLoc,
411417
BridgedArrayRef cParameters,

lib/AST/CASTBridging.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,24 @@ void *AssociatedTypeDecl_create(BridgedASTContext cContext,
729729
return static_cast<Decl *>(decl);
730730
}
731731

732+
BridgedDeclContextAndDecl ExtensionDecl_create(
733+
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
734+
BridgedSourceLoc cExtensionKeywordLoc, void *opaqueExtendedType,
735+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
736+
BridgedSourceRange cBraceRange) {
737+
ASTContext &context = convertASTContext(cContext);
738+
739+
auto *decl = ExtensionDecl::create(
740+
context, convertSourceLoc(cExtensionKeywordLoc),
741+
static_cast<TypeRepr *>(opaqueExtendedType),
742+
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
743+
convertDeclContext(cDeclContext),
744+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
745+
decl->setBraces(convertSourceRange(cBraceRange));
746+
747+
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
748+
}
749+
732750
void *OptionalTypeRepr_create(BridgedASTContext cContext, void *base,
733751
BridgedSourceLoc cQuestionLoc) {
734752
ASTContext &context = convertASTContext(cContext);

lib/ASTGen/Sources/ASTGen/Decls.swift

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

161+
// MARK: - ExtensionDecl
162+
163+
extension ASTGenVisitor {
164+
func visit(_ node: ExtensionDeclSyntax) -> ASTNode {
165+
let decl = ExtensionDecl_create(
166+
self.ctx,
167+
self.declContext,
168+
self.bridgedSourceLoc(for: node.extensionKeyword),
169+
self.visit(node.extendedType).rawValue,
170+
self.visit(node.inheritanceClause?.inheritedTypes),
171+
self.visit(node.genericWhereClause)?.rawValue,
172+
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
173+
)
174+
175+
self.withDeclContext(decl.asDeclContext) {
176+
IterableDeclContext_setParsedMembers(self.visit(node.memberBlock.members), decl.asDecl)
177+
}
178+
179+
return .decl(decl.asDecl)
180+
}
181+
}
182+
161183
// MARK: - EnumCaseDecl
162184

163185
extension ASTGenVisitor {

test/ASTGen/verify-parse.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,8 @@ actor
154154
Actor<T>: Proto1 where T: Proto1 {
155155
func method(_ b: Bool) {}
156156
}
157+
158+
extension
159+
Class: Proto2 where T: Proto1 {
160+
func method2(_ b: Bool) {}
161+
}

0 commit comments

Comments
 (0)