Skip to content

Commit 882a89b

Browse files
Merge pull request swiftlang#31415 from AnthonyLatsis/se-0267-ast-dump
ASTDumper: print contextual 'where' clauses where necessary
2 parents 70b3f01 + 5fe21dc commit 882a89b

File tree

1 file changed

+51
-42
lines changed

1 file changed

+51
-42
lines changed

lib/AST/ASTDumper.cpp

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -544,13 +544,33 @@ namespace {
544544

545545
explicit PrintDecl(raw_ostream &os, unsigned indent = 0)
546546
: OS(os), Indent(indent) { }
547-
547+
548+
private:
548549
void printRec(Decl *D) { PrintDecl(OS, Indent + 2).visit(D); }
549550
void printRec(Expr *E) { E->dump(OS, Indent+2); }
550551
void printRec(Stmt *S, const ASTContext &Ctx) { S->dump(OS, &Ctx, Indent+2); }
551552
void printRec(Pattern *P) { PrintPattern(OS, Indent+2).visit(P); }
552553
void printRec(TypeRepr *T);
553554

555+
void printWhereRequirements(
556+
PointerUnion<const AssociatedTypeDecl *, const GenericContext *> Owner)
557+
const {
558+
const auto printWhere = [&](const TrailingWhereClause *Where) {
559+
if (Where) {
560+
OS << " where requirements: ";
561+
Where->print(OS, /*printWhereKeyword*/ false);
562+
}
563+
};
564+
565+
if (const auto GC = Owner.dyn_cast<const GenericContext *>()) {
566+
if (!GC->isGeneric() || isa<ProtocolDecl>(GC))
567+
printWhere(GC->getTrailingWhereClause());
568+
} else {
569+
const auto ATD = Owner.get<const AssociatedTypeDecl *>();
570+
printWhere(ATD->getTrailingWhereClause());
571+
}
572+
}
573+
554574
// Print a field with a value.
555575
template<typename T>
556576
raw_ostream &printField(StringRef name, const T &value) {
@@ -588,6 +608,7 @@ namespace {
588608
[&] { OS << ", "; });
589609
}
590610

611+
public:
591612
void visitImportDecl(ImportDecl *ID) {
592613
printCommon(ID, "import_decl");
593614

@@ -610,12 +631,7 @@ namespace {
610631
printCommon(ED, "extension_decl", ExtensionColor);
611632
OS << ' ';
612633
ED->getExtendedType().print(OS);
613-
printInherited(ED->getInherited());
614-
for (Decl *Member : ED->getMembers()) {
615-
OS << '\n';
616-
printRec(Member);
617-
}
618-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
634+
printCommonPost(ED);
619635
}
620636

621637
void printDeclName(const ValueDecl *D) {
@@ -637,6 +653,7 @@ namespace {
637653
} else {
638654
PrintWithColorRAII(OS, TypeColor) << "<<<unresolved>>>";
639655
}
656+
printWhereRequirements(TAD);
640657
PrintWithColorRAII(OS, ParenthesisColor) << ')';
641658
}
642659

@@ -680,10 +697,7 @@ namespace {
680697
OS << " default=";
681698
defaultDef.print(OS);
682699
}
683-
if (auto whereClause = decl->getTrailingWhereClause()) {
684-
OS << " where requirements: ";
685-
whereClause->print(OS, /*printWhereKeyword*/false);
686-
}
700+
printWhereRequirements(decl);
687701
if (decl->overriddenDeclsComputed()) {
688702
OS << " overridden=";
689703
interleave(decl->getOverriddenDecls(),
@@ -708,19 +722,7 @@ namespace {
708722
} else {
709723
OS << "<null>";
710724
}
711-
printInherited(PD->getInherited());
712-
if (auto whereClause = PD->getTrailingWhereClause()) {
713-
OS << " where requirements: ";
714-
interleave(whereClause->getRequirements(),
715-
[&](const RequirementRepr &req) { req.print(OS); },
716-
[&] { OS << ", "; });
717-
}
718-
719-
for (auto VD : PD->getMembers()) {
720-
OS << '\n';
721-
printRec(VD);
722-
}
723-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
725+
printCommonPost(PD);
724726
}
725727

726728
void printCommon(ValueDecl *VD, const char *Name,
@@ -796,6 +798,28 @@ namespace {
796798
}
797799
}
798800

801+
void printCommonPost(const IterableDeclContext *IDC) {
802+
switch (IDC->getIterableContextKind()) {
803+
case IterableDeclContextKind::NominalTypeDecl: {
804+
const auto NTD = cast<NominalTypeDecl>(IDC);
805+
printInherited(NTD->getInherited());
806+
printWhereRequirements(NTD);
807+
break;
808+
}
809+
case IterableDeclContextKind::ExtensionDecl:
810+
const auto ED = cast<ExtensionDecl>(IDC);
811+
printInherited(ED->getInherited());
812+
printWhereRequirements(ED);
813+
break;
814+
}
815+
816+
for (Decl *D : IDC->getMembers()) {
817+
OS << '\n';
818+
printRec(D);
819+
}
820+
PrintWithColorRAII(OS, ParenthesisColor) << ')';
821+
}
822+
799823
void visitSourceFile(const SourceFile &SF) {
800824
OS.indent(Indent);
801825
PrintWithColorRAII(OS, ParenthesisColor) << '(';
@@ -870,12 +894,7 @@ namespace {
870894

871895
void visitEnumDecl(EnumDecl *ED) {
872896
printCommon(ED, "enum_decl");
873-
printInherited(ED->getInherited());
874-
for (Decl *D : ED->getMembers()) {
875-
OS << '\n';
876-
printRec(D);
877-
}
878-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
897+
printCommonPost(ED);
879898
}
880899

881900
void visitEnumElementDecl(EnumElementDecl *EED) {
@@ -885,24 +904,14 @@ namespace {
885904

886905
void visitStructDecl(StructDecl *SD) {
887906
printCommon(SD, "struct_decl");
888-
printInherited(SD->getInherited());
889-
for (Decl *D : SD->getMembers()) {
890-
OS << '\n';
891-
printRec(D);
892-
}
893-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
907+
printCommonPost(SD);
894908
}
895909

896910
void visitClassDecl(ClassDecl *CD) {
897911
printCommon(CD, "class_decl");
898912
if (CD->getAttrs().hasAttribute<StaticInitializeObjCMetadataAttr>())
899913
OS << " @_staticInitializeObjCMetadata";
900-
printInherited(CD->getInherited());
901-
for (Decl *D : CD->getMembers()) {
902-
OS << '\n';
903-
printRec(D);
904-
}
905-
PrintWithColorRAII(OS, ParenthesisColor) << ')';
914+
printCommonPost(CD);
906915
}
907916

908917
void visitPatternBindingDecl(PatternBindingDecl *PBD) {

0 commit comments

Comments
 (0)