Skip to content

Commit ca8f911

Browse files
committed
ASTGen: Translate generic where clauses
1 parent b6be6da commit ca8f911

File tree

6 files changed

+155
-76
lines changed

6 files changed

+155
-76
lines changed

include/swift/AST/CASTBridging.h

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,8 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
332332
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
333333
void *_Nullable opaqueGenericParamList,
334334
void *opaqueParameterList, BridgedSourceLoc cAsyncLoc,
335-
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType);
335+
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType,
336+
void *_Nullable opaqueGenericWhereClause);
336337
void FuncDecl_setBody(void *fn, void *body);
337338

338339
void *SimpleIdentTypeRepr_create(BridgedASTContext cContext,
@@ -345,11 +346,14 @@ void *UnresolvedDotExpr_create(BridgedASTContext cContext, void *base,
345346
void *ClosureExpr_create(BridgedASTContext cContext, void *body,
346347
BridgedDeclContext cDeclContext);
347348

348-
void *TypeAliasDecl_create(
349-
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
350-
BridgedSourceLoc cAliasKeywordLoc, BridgedIdentifier cName,
351-
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
352-
BridgedSourceLoc cEqualLoc, void *opaqueUnderlyingType);
349+
void *TypeAliasDecl_create(BridgedASTContext cContext,
350+
BridgedDeclContext cDeclContext,
351+
BridgedSourceLoc cAliasKeywordLoc,
352+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
353+
void *_Nullable opaqueGenericParamList,
354+
BridgedSourceLoc cEqualLoc,
355+
void *opaqueUnderlyingType,
356+
void *_Nullable opaqueGenericWhereClause);
353357

354358
void IterableDeclContext_setParsedMembers(BridgedArrayRef members,
355359
void *opaqueDecl);
@@ -358,33 +362,37 @@ BridgedDeclContextAndDecl StructDecl_create(
358362
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
359363
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
360364
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
361-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
365+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
366+
BridgedSourceRange cBraceRange);
362367

363368
BridgedDeclContextAndDecl ClassDecl_create(
364369
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
365370
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
366371
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
367-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
372+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
373+
BridgedSourceRange cBraceRange);
368374

369375
BridgedDeclContextAndDecl ProtocolDecl_create(
370376
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
371377
BridgedSourceLoc cProtocolKeywordLoc, BridgedIdentifier cName,
372378
BridgedSourceLoc cNameLoc, BridgedArrayRef cPrimaryAssociatedTypeNames,
373-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange);
379+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
380+
BridgedSourceRange cBraceRange);
374381

375382
void *AssociatedTypeDecl_create(BridgedASTContext cContext,
376383
BridgedDeclContext cDeclContext,
377384
BridgedSourceLoc cAssociatedtypeKeywordLoc,
378385
BridgedIdentifier cName,
379386
BridgedSourceLoc cNameLoc,
380387
BridgedArrayRef cInheritedTypes,
381-
void *_Nullable opaqueDefaultType);
388+
void *_Nullable opaqueDefaultType,
389+
void *_Nullable opaqueGenericWhereClause);
382390

383391
void *GenericParamList_create(BridgedASTContext cContext,
384-
BridgedSourceLoc cLAngleLoc,
385-
BridgedArrayRef params,
386-
BridgedSourceLoc cWhereLoc, BridgedArrayRef reqs,
387-
BridgedSourceLoc cRAngleLoc);
392+
BridgedSourceLoc cLeftAngleLoc,
393+
BridgedArrayRef cParameters,
394+
void *_Nullable opaqueGenericWhereClause,
395+
BridgedSourceLoc cRightAngleLoc);
388396

389397
void *GenericTypeParamDecl_create(BridgedASTContext cContext,
390398
BridgedDeclContext cDeclContext,
@@ -394,6 +402,10 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
394402
void *_Nullable opaqueInheritedType,
395403
SwiftInt index);
396404

405+
void *TrailingWhereClause_create(BridgedASTContext cContext,
406+
BridgedSourceLoc cWhereKeywordLoc,
407+
BridgedArrayRef cRequirements);
408+
397409
void *ParameterList_create(BridgedASTContext cContext,
398410
BridgedSourceLoc cLeftParenLoc,
399411
BridgedArrayRef cParameters,

lib/AST/CASTBridging.cpp

Lines changed: 79 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,8 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
448448
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
449449
void *_Nullable opaqueGenericParamList,
450450
void *opaqueParameterList, BridgedSourceLoc cAsyncLoc,
451-
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType) {
451+
BridgedSourceLoc cThrowsLoc, void *_Nullable opaqueReturnType,
452+
void *_Nullable opaqueGenericWhereClause) {
452453
ASTContext &context = convertASTContext(cContext);
453454

454455
auto *paramList = static_cast<ParameterList *>(opaqueParameterList);
@@ -463,6 +464,8 @@ FuncDecl_create(BridgedASTContext cContext, BridgedDeclContext cDeclContext,
463464
static_cast<GenericParamList *>(opaqueGenericParamList), paramList,
464465
static_cast<TypeRepr *>(opaqueReturnType),
465466
convertDeclContext(cDeclContext));
467+
out->setTrailingWhereClause(
468+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
466469

467470
return {bridgeDeclContext(out), static_cast<FuncDecl *>(out),
468471
static_cast<Decl *>(out)};
@@ -527,11 +530,14 @@ void *ClosureExpr_create(BridgedASTContext cContext, void *body,
527530
return (Expr *)out;
528531
}
529532

530-
void *TypeAliasDecl_create(
531-
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
532-
BridgedSourceLoc cAliasKeywordLoc, BridgedIdentifier cName,
533-
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
534-
BridgedSourceLoc cEqualLoc, void *opaqueUnderlyingType) {
533+
void *TypeAliasDecl_create(BridgedASTContext cContext,
534+
BridgedDeclContext cDeclContext,
535+
BridgedSourceLoc cAliasKeywordLoc,
536+
BridgedIdentifier cName, BridgedSourceLoc cNameLoc,
537+
void *_Nullable opaqueGenericParamList,
538+
BridgedSourceLoc cEqualLoc,
539+
void *opaqueUnderlyingType,
540+
void *_Nullable opaqueGenericWhereClause) {
535541
ASTContext &context = convertASTContext(cContext);
536542

537543
auto *decl = new (context) TypeAliasDecl(
@@ -540,6 +546,8 @@ void *TypeAliasDecl_create(
540546
static_cast<GenericParamList *>(opaqueGenericParamList),
541547
convertDeclContext(cDeclContext));
542548
decl->setUnderlyingTypeRepr(static_cast<TypeRepr *>(opaqueUnderlyingType));
549+
decl->setTrailingWhereClause(
550+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
543551

544552
return static_cast<Decl *>(decl);
545553
}
@@ -569,7 +577,8 @@ BridgedDeclContextAndDecl StructDecl_create(
569577
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
570578
BridgedSourceLoc cStructKeywordLoc, BridgedIdentifier cName,
571579
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
572-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
580+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
581+
BridgedSourceRange cBraceRange) {
573582
ASTContext &context = convertASTContext(cContext);
574583

575584
auto *decl = new (context) StructDecl(
@@ -578,6 +587,8 @@ BridgedDeclContextAndDecl StructDecl_create(
578587
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
579588
static_cast<GenericParamList *>(opaqueGenericParamList),
580589
convertDeclContext(cDeclContext));
590+
decl->setTrailingWhereClause(
591+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
581592
decl->setBraces(convertSourceRange(cBraceRange));
582593

583594
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
@@ -587,7 +598,8 @@ BridgedDeclContextAndDecl ClassDecl_create(
587598
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
588599
BridgedSourceLoc cClassKeywordLoc, BridgedIdentifier cName,
589600
BridgedSourceLoc cNameLoc, void *_Nullable opaqueGenericParamList,
590-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
601+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
602+
BridgedSourceRange cBraceRange) {
591603
ASTContext &context = convertASTContext(cContext);
592604

593605
auto *decl = new (context) ClassDecl(
@@ -596,6 +608,8 @@ BridgedDeclContextAndDecl ClassDecl_create(
596608
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
597609
static_cast<GenericParamList *>(opaqueGenericParamList),
598610
convertDeclContext(cDeclContext), false);
611+
decl->setTrailingWhereClause(
612+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
599613
decl->setBraces(convertSourceRange(cBraceRange));
600614

601615
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
@@ -605,7 +619,8 @@ BridgedDeclContextAndDecl ProtocolDecl_create(
605619
BridgedASTContext cContext, BridgedDeclContext cDeclContext,
606620
BridgedSourceLoc cProtocolKeywordLoc, BridgedIdentifier cName,
607621
BridgedSourceLoc cNameLoc, BridgedArrayRef cPrimaryAssociatedTypeNames,
608-
BridgedArrayRef cInheritedTypes, BridgedSourceRange cBraceRange) {
622+
BridgedArrayRef cInheritedTypes, void *_Nullable opaqueGenericWhereClause,
623+
BridgedSourceRange cBraceRange) {
609624
SmallVector<PrimaryAssociatedTypeName, 2> primaryAssociatedTypeNames;
610625
for (auto &pair : convertArrayRef<BridgedIdentifierAndSourceLoc>(
611626
cPrimaryAssociatedTypeNames)) {
@@ -619,7 +634,7 @@ BridgedDeclContextAndDecl ProtocolDecl_create(
619634
convertSourceLoc(cNameLoc), convertIdentifier(cName),
620635
context.AllocateCopy(primaryAssociatedTypeNames),
621636
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)),
622-
nullptr);
637+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
623638
decl->setBraces(convertSourceRange(cBraceRange));
624639

625640
return {bridgeDeclContext(decl), static_cast<Decl *>(decl)};
@@ -631,14 +646,15 @@ void *AssociatedTypeDecl_create(BridgedASTContext cContext,
631646
BridgedIdentifier cName,
632647
BridgedSourceLoc cNameLoc,
633648
BridgedArrayRef cInheritedTypes,
634-
void *_Nullable opaqueDefaultType) {
649+
void *_Nullable opaqueDefaultType,
650+
void *_Nullable opaqueGenericWhereClause) {
635651
ASTContext &context = convertASTContext(cContext);
636652

637653
auto *decl = AssociatedTypeDecl::createParsed(
638654
context, convertDeclContext(cDeclContext),
639655
convertSourceLoc(cAssociatedtypeKeywordLoc), convertIdentifier(cName),
640656
convertSourceLoc(cNameLoc), static_cast<TypeRepr *>(opaqueDefaultType),
641-
nullptr);
657+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause));
642658
decl->setInherited(
643659
context.AllocateCopy(convertToInheritedEntries(cInheritedTypes)));
644660

@@ -863,32 +879,22 @@ void *ExistentialTypeRepr_create(BridgedASTContext cContext,
863879
}
864880

865881
void *GenericParamList_create(BridgedASTContext cContext,
866-
BridgedSourceLoc cLAngleLoc,
867-
BridgedArrayRef params,
868-
BridgedSourceLoc cWhereLoc, BridgedArrayRef reqs,
869-
BridgedSourceLoc cRAngleLoc) {
870-
ASTContext &context = convertASTContext(cContext);
871-
SmallVector<RequirementRepr> requirements;
872-
for (auto req : convertArrayRef<BridgedRequirementRepr>(reqs)) {
873-
switch (req.Kind) {
874-
case BridgedRequirementReprKindTypeConstraint:
875-
requirements.push_back(RequirementRepr::getTypeConstraint(
876-
(TypeRepr *)req.FirstType, convertSourceLoc(req.SeparatorLoc),
877-
(TypeRepr *)req.SecondType, /*isExpansionPattern*/ false));
878-
break;
879-
case BridgedRequirementReprKindSameType:
880-
requirements.push_back(RequirementRepr::getSameType(
881-
(TypeRepr *)req.FirstType, convertSourceLoc(req.SeparatorLoc),
882-
(TypeRepr *)req.SecondType, /*isExpansionPattern*/ false));
883-
break;
884-
case BridgedRequirementReprKindLayoutConstraint:
885-
llvm_unreachable("cannot handle layout constraints!");
886-
}
882+
BridgedSourceLoc cLeftAngleLoc,
883+
BridgedArrayRef cParameters,
884+
void *_Nullable opaqueGenericWhereClause,
885+
BridgedSourceLoc cRightAngleLoc) {
886+
SourceLoc whereLoc;
887+
ArrayRef<RequirementRepr> requirements;
888+
if (auto *genericWhereClause =
889+
static_cast<TrailingWhereClause *>(opaqueGenericWhereClause)) {
890+
whereLoc = genericWhereClause->getWhereLoc();
891+
requirements = genericWhereClause->getRequirements();
887892
}
893+
888894
return GenericParamList::create(
889-
context, convertSourceLoc(cLAngleLoc),
890-
convertArrayRef<GenericTypeParamDecl *>(params),
891-
convertSourceLoc(cWhereLoc), requirements, convertSourceLoc(cRAngleLoc));
895+
convertASTContext(cContext), convertSourceLoc(cLeftAngleLoc),
896+
convertArrayRef<GenericTypeParamDecl *>(cParameters), whereLoc,
897+
requirements, convertSourceLoc(cRightAngleLoc));
892898
}
893899

894900
void *GenericTypeParamDecl_create(BridgedASTContext cContext,
@@ -913,6 +919,43 @@ void *GenericTypeParamDecl_create(BridgedASTContext cContext,
913919
return decl;
914920
}
915921

922+
void *TrailingWhereClause_create(BridgedASTContext cContext,
923+
BridgedSourceLoc cWhereKeywordLoc,
924+
BridgedArrayRef cRequirements) {
925+
SmallVector<RequirementRepr> requirements;
926+
for (auto &cReq : convertArrayRef<BridgedRequirementRepr>(cRequirements)) {
927+
switch (cReq.Kind) {
928+
case BridgedRequirementReprKindTypeConstraint:
929+
requirements.push_back(RequirementRepr::getTypeConstraint(
930+
static_cast<TypeRepr *>(cReq.FirstType),
931+
convertSourceLoc(cReq.SeparatorLoc),
932+
static_cast<TypeRepr *>(cReq.SecondType),
933+
/*isExpansionPattern*/ false));
934+
break;
935+
case BridgedRequirementReprKindSameType:
936+
requirements.push_back(
937+
RequirementRepr::getSameType(static_cast<TypeRepr *>(cReq.FirstType),
938+
convertSourceLoc(cReq.SeparatorLoc),
939+
static_cast<TypeRepr *>(cReq.SecondType),
940+
/*isExpansionPattern*/ false));
941+
break;
942+
case BridgedRequirementReprKindLayoutConstraint:
943+
llvm_unreachable("cannot handle layout constraints!");
944+
}
945+
}
946+
947+
SourceLoc whereKeywordLoc = convertSourceLoc(cWhereKeywordLoc);
948+
SourceLoc endLoc;
949+
if (requirements.empty()) {
950+
endLoc = whereKeywordLoc;
951+
} else {
952+
endLoc = requirements.back().getSourceRange().End;
953+
}
954+
955+
return TrailingWhereClause::create(convertASTContext(cContext),
956+
whereKeywordLoc, endLoc, requirements);
957+
}
958+
916959
void *ParameterList_create(BridgedASTContext cContext,
917960
BridgedSourceLoc cLeftParenLoc,
918961
BridgedArrayRef cParameters,

lib/ASTGen/Sources/ASTGen/ASTGen.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,15 @@ extension ASTGenVisitor {
216216
return self.visit(node)
217217
}
218218

219+
@inline(__always)
220+
func visit(_ node: GenericWhereClauseSyntax?) -> ASTNode? {
221+
guard let node else {
222+
return nil
223+
}
224+
225+
return self.visit(node)
226+
}
227+
219228
@inline(__always)
220229
func visit(_ node: InheritedTypeListSyntax?) -> BridgedArrayRef {
221230
guard let node else {

lib/ASTGen/Sources/ASTGen/Decls.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ extension ASTGenVisitor {
1919
nameLoc,
2020
self.visit(node.genericParameterClause)?.rawValue,
2121
self.bridgedSourceLoc(for: node.initializer.equal),
22-
self.visit(node.initializer.value).rawValue
22+
self.visit(node.initializer.value).rawValue,
23+
self.visit(node.genericWhereClause)?.rawValue
2324
)
2425
)
2526
}
@@ -35,6 +36,7 @@ extension ASTGenVisitor {
3536
nameLoc,
3637
self.visit(node.genericParameterClause)?.rawValue,
3738
self.visit(node.inheritanceClause?.inheritedTypes),
39+
self.visit(node.genericWhereClause)?.rawValue,
3840
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
3941
)
4042

@@ -56,6 +58,7 @@ extension ASTGenVisitor {
5658
nameLoc,
5759
self.visit(node.genericParameterClause)?.rawValue,
5860
self.visit(node.inheritanceClause?.inheritedTypes),
61+
self.visit(node.genericWhereClause)?.rawValue,
5962
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
6063
)
6164

@@ -80,6 +83,7 @@ extension ASTGenVisitor {
8083
nameLoc,
8184
primaryAssociatedTypeNames.bridgedArray(in: self),
8285
self.visit(node.inheritanceClause?.inheritedTypes),
86+
self.visit(node.genericWhereClause)?.rawValue,
8387
BridgedSourceRange(startToken: node.memberBlock.leftBrace, endToken: node.memberBlock.rightBrace, in: self)
8488
)
8589

@@ -101,7 +105,8 @@ extension ASTGenVisitor {
101105
name,
102106
nameLoc,
103107
self.visit(node.inheritanceClause?.inheritedTypes),
104-
self.visit(node.initializer?.value)?.rawValue
108+
self.visit(node.initializer?.value)?.rawValue,
109+
self.visit(node.genericWhereClause)?.rawValue
105110
)
106111
)
107112
}
@@ -145,7 +150,8 @@ extension ASTGenVisitor {
145150
self.visit(node.signature.parameterClause).rawValue,
146151
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.asyncSpecifier),
147152
self.bridgedSourceLoc(for: node.signature.effectSpecifiers?.throwsSpecifier),
148-
self.visit(node.signature.returnClause?.type)?.rawValue
153+
self.visit(node.signature.returnClause?.type)?.rawValue,
154+
self.visit(node.genericWhereClause)?.rawValue
149155
)
150156

151157
if let body = node.body {

0 commit comments

Comments
 (0)