Skip to content

Commit 2ad1d3a

Browse files
authored
Merge pull request #11909 from slavapestov/divorce-clang-importer-from-sema-part-6
ClangImporter: Don't record redundant inheritance clause entries
2 parents 6d16044 + a722283 commit 2ad1d3a

File tree

11 files changed

+2312
-1906
lines changed

11 files changed

+2312
-1906
lines changed

lib/AST/ASTPrinter.cpp

Lines changed: 42 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -594,15 +594,12 @@ class PrintAST : public ASTVisitor<PrintAST> {
594594
bool openBracket = true, bool closeBracket = true);
595595
void printNominalDeclGenericParams(NominalTypeDecl *decl);
596596
void printNominalDeclGenericRequirements(NominalTypeDecl *decl);
597-
void printInherited(const Decl *decl,
598-
ArrayRef<TypeLoc> inherited,
599-
ArrayRef<ProtocolDecl *> protos,
600-
Type superclass = {});
601-
602-
void printInherited(const NominalTypeDecl *decl);
603-
void printInherited(const EnumDecl *D);
604-
void printInherited(const ExtensionDecl *decl);
597+
void printInherited(const Decl *decl, ArrayRef<TypeLoc> inherited);
598+
599+
void printInherited(const NominalTypeDecl *D);
600+
void printInherited(const ExtensionDecl *D);
605601
void printInherited(const GenericTypeParamDecl *D);
602+
void printInherited(const AssociatedTypeDecl *D);
606603

607604
void printEnumElement(EnumElementDecl *elt);
608605

@@ -1659,93 +1656,55 @@ void PrintAST::printNominalDeclGenericRequirements(NominalTypeDecl *decl) {
16591656
printGenericSignature(GenericSig, PrintRequirements | InnermostOnly);
16601657
}
16611658

1662-
void PrintAST::printInherited(const Decl *decl,
1663-
ArrayRef<TypeLoc> inherited,
1664-
ArrayRef<ProtocolDecl *> protos,
1665-
Type superclass) {
1666-
if (inherited.empty() && superclass.isNull()) {
1667-
if (protos.empty())
1668-
return;
1669-
}
1670-
1671-
if (inherited.empty()) {
1672-
bool PrintedColon = false;
1673-
bool PrintedInherited = false;
1674-
1675-
if (superclass) {
1676-
bool ShouldPrintSuper = true;
1677-
if (auto NTD = superclass->getAnyNominal()) {
1678-
ShouldPrintSuper = shouldPrint(NTD);
1679-
}
1680-
if (ShouldPrintSuper) {
1681-
Printer << " : ";
1682-
superclass.print(Printer, Options);
1683-
PrintedInherited = true;
1684-
}
1659+
void PrintAST::printInherited(const Decl *decl, ArrayRef<TypeLoc> inherited) {
1660+
SmallVector<TypeLoc, 6> TypesToPrint;
1661+
for (auto TL : inherited) {
1662+
if (auto Ty = TL.getType()) {
1663+
if (auto NTD = Ty->getAnyNominal())
1664+
if (!shouldPrint(NTD))
1665+
continue;
16851666
}
1667+
TypesToPrint.push_back(TL);
1668+
}
16861669

1687-
for (auto Proto : protos) {
1688-
if (!shouldPrint(Proto))
1670+
auto &ctx = decl->getASTContext();
1671+
for (auto attr : decl->getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
1672+
if (auto *proto = ctx.getProtocol(attr->getProtocolKind())) {
1673+
if (!shouldPrint(proto))
16891674
continue;
1690-
if (auto Enum = dyn_cast<EnumDecl>(decl)) {
1691-
// Conformance to RawRepresentable is implied by having a raw type.
1692-
if (Enum->hasRawType()
1693-
&& Proto->isSpecificProtocol(KnownProtocolKind::RawRepresentable))
1694-
continue;
1695-
// Conformance to Equatable and Hashable is implied by being a "simple"
1696-
// no-payload enum with cases.
1697-
if (Enum->hasCases()
1698-
&& Enum->hasOnlyCasesWithoutAssociatedValues()
1699-
&& (Proto->isSpecificProtocol(KnownProtocolKind::Equatable)
1700-
|| Proto->isSpecificProtocol(KnownProtocolKind::Hashable)))
1701-
continue;
1702-
}
1703-
1704-
if (PrintedInherited)
1705-
Printer << ", ";
1706-
else if (!PrintedColon)
1707-
Printer << " : ";
1708-
Proto->getDeclaredType()->print(Printer, Options);
1709-
PrintedInherited = true;
1710-
PrintedColon = true;
1711-
}
1712-
} else {
1713-
SmallVector<TypeLoc, 6> TypesToPrint;
1714-
for (auto TL : inherited) {
1715-
if (auto Ty = TL.getType()) {
1716-
if (auto NTD = Ty->getAnyNominal())
1717-
if (!shouldPrint(NTD))
1718-
continue;
1719-
}
1720-
TypesToPrint.push_back(TL);
1675+
if (attr->getProtocolKind() == KnownProtocolKind::RawRepresentable &&
1676+
isa<EnumDecl>(decl) &&
1677+
cast<EnumDecl>(decl)->hasRawType())
1678+
continue;
1679+
TypesToPrint.push_back(TypeLoc::withoutLoc(proto->getDeclaredType()));
17211680
}
1722-
if (TypesToPrint.empty())
1723-
return;
1681+
}
1682+
if (TypesToPrint.empty())
1683+
return;
17241684

1725-
Printer << " : ";
1685+
Printer << " : ";
17261686

1727-
interleave(TypesToPrint, [&](TypeLoc TL) {
1728-
printTypeLoc(TL);
1729-
}, [&]() {
1730-
Printer << ", ";
1731-
});
1732-
}
1687+
interleave(TypesToPrint, [&](TypeLoc TL) {
1688+
printTypeLoc(TL);
1689+
}, [&]() {
1690+
Printer << ", ";
1691+
});
17331692
}
17341693

1735-
void PrintAST::printInherited(const NominalTypeDecl *decl) {
1736-
printInherited(decl, decl->getInherited(), { }, nullptr);
1694+
void PrintAST::printInherited(const NominalTypeDecl *D) {
1695+
printInherited(D, D->getInherited());
17371696
}
17381697

1739-
void PrintAST::printInherited(const EnumDecl *decl) {
1740-
printInherited(decl, decl->getInherited(), { });
1698+
void PrintAST::printInherited(const ExtensionDecl *D) {
1699+
printInherited(D, D->getInherited());
17411700
}
17421701

1743-
void PrintAST::printInherited(const ExtensionDecl *decl) {
1744-
printInherited(decl, decl->getInherited(), { });
1702+
void PrintAST::printInherited(const GenericTypeParamDecl *D) {
1703+
printInherited(D, D->getInherited());
17451704
}
17461705

1747-
void PrintAST::printInherited(const GenericTypeParamDecl *D) {
1748-
printInherited(D, D->getInherited(), { });
1706+
void PrintAST::printInherited(const AssociatedTypeDecl *D) {
1707+
printInherited(D, D->getInherited());
17491708
}
17501709

17511710
static void getModuleEntities(const clang::Module *ClangMod,
@@ -2022,7 +1981,7 @@ void PrintAST::visitGenericTypeParamDecl(GenericTypeParamDecl *decl) {
20221981
Printer.printName(decl->getName(), PrintNameContext::GenericParameter);
20231982
});
20241983

2025-
printInherited(decl, decl->getInherited(), { });
1984+
printInherited(decl);
20261985
}
20271986

20281987
void PrintAST::visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
@@ -2039,7 +1998,7 @@ void PrintAST::visitAssociatedTypeDecl(AssociatedTypeDecl *decl) {
20391998
if (proto->isRequirementSignatureComputed()) {
20401999
printInheritedFromRequirementSignature(proto, decl);
20412000
} else {
2042-
printInherited(decl, decl->getInherited(), { });
2001+
printInherited(decl);
20432002
}
20442003

20452004
if (!decl->getDefaultDefinitionLoc().isNull()) {

lib/AST/ProtocolConformance.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -896,37 +896,37 @@ void NominalTypeDecl::prepareConformanceTable() const {
896896
return;
897897
}
898898

899-
// Add any synthesized conformances.
899+
SmallPtrSet<ProtocolDecl *, 2> protocols;
900+
901+
auto addSynthesized = [&](KnownProtocolKind kind) {
902+
if (auto *proto = getASTContext().getProtocol(kind)) {
903+
if (protocols.count(proto) == 0) {
904+
ConformanceTable->addSynthesizedConformance(mutableThis, proto);
905+
protocols.insert(proto);
906+
}
907+
}
908+
};
909+
910+
// Add protocols for any synthesized protocol attributes.
911+
for (auto attr : getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
912+
addSynthesized(attr->getProtocolKind());
913+
}
914+
915+
// Add any implicit conformances.
900916
if (auto theEnum = dyn_cast<EnumDecl>(mutableThis)) {
901917
if (theEnum->hasCases() && theEnum->hasOnlyCasesWithoutAssociatedValues()) {
902918
// Simple enumerations conform to Equatable.
903-
if (auto equatable = ctx.getProtocol(KnownProtocolKind::Equatable)) {
904-
ConformanceTable->addSynthesizedConformance(mutableThis, equatable);
905-
}
919+
addSynthesized(KnownProtocolKind::Equatable);
906920

907921
// Simple enumerations conform to Hashable.
908-
if (auto hashable = getASTContext().getProtocol(
909-
KnownProtocolKind::Hashable)) {
910-
ConformanceTable->addSynthesizedConformance(mutableThis, hashable);
911-
}
922+
addSynthesized(KnownProtocolKind::Hashable);
912923
}
913924

914925
// Enumerations with a raw type conform to RawRepresentable.
915926
if (resolver)
916927
resolver->resolveRawType(theEnum);
917928
if (theEnum->hasRawType()) {
918-
if (auto rawRepresentable =
919-
ctx.getProtocol(KnownProtocolKind::RawRepresentable)) {
920-
ConformanceTable->addSynthesizedConformance(mutableThis,
921-
rawRepresentable);
922-
}
923-
}
924-
}
925-
926-
// Add protocols for any synthesized protocol attributes.
927-
for (auto attr : getAttrs().getAttributes<SynthesizedProtocolAttr>()) {
928-
if (auto proto = getASTContext().getProtocol(attr->getProtocolKind())) {
929-
ConformanceTable->addSynthesizedConformance(mutableThis, proto);
929+
addSynthesized(KnownProtocolKind::RawRepresentable);
930930
}
931931
}
932932
}

0 commit comments

Comments
 (0)