Skip to content

Commit d5047ee

Browse files
author
David Ungar
committed
Use isPrivateToEnclosingFile
1 parent f6f086f commit d5047ee

File tree

2 files changed

+16
-68
lines changed

2 files changed

+16
-68
lines changed

lib/AST/FrontendSourceFileDepGraphFactory.cpp

Lines changed: 12 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -72,40 +72,12 @@ static std::string mangleTypeAsContext(const NominalTypeDecl *NTD) {
7272
// MARK: Privacy queries
7373
//==============================================================================
7474

75-
static bool declIsPrivate(const ValueDecl *VD) {
76-
return VD->getFormalAccess() <= AccessLevel::FilePrivate;
77-
}
78-
79-
/// Return true if \param D cannot affect other files.
80-
static bool declIsPrivate(const Decl *D) {
81-
if (auto *VD = dyn_cast<ValueDecl>(D))
82-
return declIsPrivate(VD);
83-
switch (D->getKind()) {
84-
case DeclKind::Import:
85-
case DeclKind::PatternBinding:
86-
case DeclKind::EnumCase:
87-
case DeclKind::TopLevelCode:
88-
case DeclKind::IfConfig:
89-
case DeclKind::PoundDiagnostic:
90-
return true;
91-
92-
case DeclKind::Extension:
93-
case DeclKind::InfixOperator:
94-
case DeclKind::PrefixOperator:
95-
case DeclKind::PostfixOperator:
96-
return false;
97-
98-
default:
99-
llvm_unreachable("everything else is a ValueDecl");
100-
}
101-
}
102-
10375
/// Return true if \ref ED does not contain a member that can affect other
10476
/// files.
10577
static bool allMembersArePrivate(const ExtensionDecl *ED) {
106-
return std::all_of(ED->getMembers().begin(), ED->getMembers().end(),
107-
[](const Decl *d) { return declIsPrivate(d); });
108-
// declIsPrivate);
78+
return std::all_of(
79+
ED->getMembers().begin(), ED->getMembers().end(),
80+
[](const Decl *d) { return d->isPrivateToEnclosingFile(); });
10981
}
11082

11183
/// \ref inheritedType, an inherited protocol, return true if this inheritance
@@ -125,7 +97,7 @@ static bool extendedTypeIsPrivate(TypeLoc inheritedType) {
12597
"Should not have a subclass existential "
12698
"in the inheritance clause of an extension");
12799
for (auto protoTy : layout.getProtocols()) {
128-
if (!declIsPrivate(protoTy->getDecl()))
100+
if (!protoTy->getDecl()->isPrivateToEnclosingFile())
129101
return false;
130102
}
131103

@@ -448,8 +420,9 @@ struct SourceFileDeclFinder {
448420
const bool exposedProtocolIsExtended =
449421
ED && !allInheritedProtocolsArePrivate(ED);
450422
if (ED && !includePrivateDecls && !exposedProtocolIsExtended &&
451-
std::all_of(ED->getMembers().begin(), ED->getMembers().end(),
452-
[&](const Decl *D) { return declIsPrivate(D); })) {
423+
std::all_of(
424+
ED->getMembers().begin(), ED->getMembers().end(),
425+
[&](const Decl *D) { return D->isPrivateToEnclosingFile(); })) {
453426
return;
454427
}
455428
if (includePrivateDecls || !ED || exposedProtocolIsExtended)
@@ -485,7 +458,8 @@ struct SourceFileDeclFinder {
485458
continue;
486459
for (const auto *member : ED->getMembers())
487460
if (const auto *VD = dyn_cast<ValueDecl>(member))
488-
if (VD->hasName() && (includePrivateDecls || !declIsPrivate(VD))) {
461+
if (VD->hasName() &&
462+
(includePrivateDecls || !VD->isPrivateToEnclosingFile())) {
489463
const auto *const NTD = ED->getExtendedNominal();
490464
if (NTD)
491465
valuesInExtensions.push_back(std::make_pair(NTD, VD));
@@ -534,7 +508,7 @@ struct SourceFileDeclFinder {
534508

535509
/// Return true if \param D should be excluded on privacy grounds.
536510
bool excludeIfPrivate(const Decl *const D) {
537-
return !includePrivateDecls && declIsPrivate(D);
511+
return !includePrivateDecls && D->isPrivateToEnclosingFile();
538512
}
539513
};
540514
} // namespace
@@ -642,7 +616,7 @@ class UsedDeclEnumerator {
642616
std::unordered_set<std::string> holdersOfCascadingMembers;
643617
for (const auto &p : SF->getReferencedNameTracker()->getUsedMembers()) {
644618
{
645-
bool isPrivate = declIsPrivate(p.getFirst().first);
619+
bool isPrivate = p.getFirst().first->isPrivateToEnclosingFile();
646620
if (isPrivate && !includeIntrafileDeps)
647621
continue;
648622
}
@@ -660,7 +634,7 @@ class UsedDeclEnumerator {
660634
const std::unordered_set<std::string> &&holdersOfCascadingMembers) {
661635
for (const auto &p : SF->getReferencedNameTracker()->getUsedMembers()) {
662636
{
663-
bool isPrivate = declIsPrivate(p.getFirst().first);
637+
bool isPrivate = p.getFirst().first->isPrivateToEnclosingFile();
664638
if (isPrivate && !includeIntrafileDeps)
665639
continue;
666640
}

lib/FrontendTool/ReferenceDependencies.cpp

Lines changed: 4 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ class ProvidesEmitter {
124124
void emitValueDecl(const ValueDecl *VD) const;
125125

126126
static bool extendedTypeIsPrivate(TypeLoc inheritedType);
127-
static bool declIsPrivate(const Decl *member);
128127
};
129128

130129
/// Emit the depended-upon declartions.
@@ -334,8 +333,9 @@ void ProvidesEmitter::emitExtensionDecl(const ExtensionDecl *const ED,
334333
std::all_of(ED->getInherited().begin(), ED->getInherited().end(),
335334
extendedTypeIsPrivate);
336335
if (justMembers) {
337-
if (std::all_of(ED->getMembers().begin(), ED->getMembers().end(),
338-
declIsPrivate)) {
336+
if (std::all_of(
337+
ED->getMembers().begin(), ED->getMembers().end(),
338+
[](const Decl *D) { return D->isPrivateToEnclosingFile(); })) {
339339
return;
340340
}
341341
cpd.extensionsWithJustMembers.push_back(ED);
@@ -474,39 +474,13 @@ bool ProvidesEmitter::extendedTypeIsPrivate(TypeLoc inheritedType) {
474474
assert(!layout.explicitSuperclass && "Should not have a subclass existential "
475475
"in the inheritance clause of an extension");
476476
for (auto protoTy : layout.getProtocols()) {
477-
if (!declIsPrivate(protoTy->getDecl()))
477+
if (!protoTy->getDecl()->isPrivateToEnclosingFile())
478478
return false;
479479
}
480480

481481
return true;
482482
}
483483

484-
bool ProvidesEmitter::declIsPrivate(const Decl *member) {
485-
auto *VD = dyn_cast<ValueDecl>(member);
486-
if (!VD) {
487-
switch (member->getKind()) {
488-
case DeclKind::Import:
489-
case DeclKind::PatternBinding:
490-
case DeclKind::EnumCase:
491-
case DeclKind::TopLevelCode:
492-
case DeclKind::IfConfig:
493-
case DeclKind::PoundDiagnostic:
494-
return true;
495-
496-
case DeclKind::Extension:
497-
case DeclKind::InfixOperator:
498-
case DeclKind::PrefixOperator:
499-
case DeclKind::PostfixOperator:
500-
return false;
501-
502-
default:
503-
llvm_unreachable("everything else is a ValueDecl");
504-
}
505-
}
506-
507-
return VD->getFormalAccess() <= AccessLevel::FilePrivate;
508-
}
509-
510484
void DependsEmitter::emit(const SourceFile *SF,
511485
const DependencyTracker &depTracker,
512486
llvm::raw_ostream &out) {

0 commit comments

Comments
 (0)