Skip to content

Commit 101dd2d

Browse files
authored
Merge pull request swiftlang#30167 from davidungar/ref-name-tracker-factor-3-2-20
[NFC, Incremental] Factor used Decl enumeration in preparation for dependency verifier
2 parents 0b2089d + 85d70b5 commit 101dd2d

File tree

9 files changed

+279
-230
lines changed

9 files changed

+279
-230
lines changed

include/swift/AST/Decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -920,6 +920,9 @@ class alignas(1 << DeclAlignInBits) Decl {
920920
/// If this returns true, the decl can be safely casted to ValueDecl.
921921
bool isPotentiallyOverridable() const;
922922

923+
/// Returns true if this Decl cannot be seen by any other source file
924+
bool isPrivateToEnclosingFile() const;
925+
923926
/// If an alternative module name is specified for this decl, e.g. using
924927
/// @_originalDefinedIn attribute, this function returns this module name.
925928
StringRef getAlternateModuleName() const;

include/swift/AST/FineGrainedDependencies.h

Lines changed: 1 addition & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/Basic/LLVM.h"
1818
#include "swift/Basic/NullablePtr.h"
1919
#include "swift/Basic/Range.h"
20+
#include "swift/Basic/ReferenceDependencyKeys.h"
2021
#include "llvm/ADT/Hashing.h"
2122
#include "llvm/Support/MD5.h"
2223
#include "llvm/Support/MemoryBuffer.h"
@@ -350,33 +351,6 @@ bool emitReferenceDependencies(DiagnosticEngine &diags, SourceFile *SF,
350351
// MARK: Enums
351352
//==============================================================================
352353

353-
/// Encode the current sorts of dependencies as kinds of nodes in the dependency
354-
/// graph, splitting the current *member* into \ref member and \ref
355-
/// potentialMember and adding \ref sourceFileProvide.
356-
357-
enum class NodeKind {
358-
topLevel,
359-
nominal,
360-
/// In the status quo scheme, *member* dependencies could have blank names
361-
/// for the member, to indicate that the provider might add members.
362-
/// This code uses a separate kind, \ref potentialMember. The holder field is
363-
/// unused.
364-
potentialMember,
365-
/// Corresponding to the status quo *member* dependency with a non-blank
366-
/// member.
367-
member,
368-
dynamicLookup,
369-
externalDepend,
370-
sourceFileProvide,
371-
/// For iterating through the NodeKinds.
372-
kindCount
373-
};
374-
375-
/// Used for printing out NodeKinds to dot files, and dumping nodes for
376-
/// debugging.
377-
const std::string NodeKindNames[]{
378-
"topLevel", "nominal", "potentialMember", "member",
379-
"dynamicLookup", "externalDepend", "sourceFileProvide"};
380354

381355
/// Instead of the status quo scheme of two kinds of "Depends", cascading and
382356
/// non-cascading this code represents each entity ("Provides" in the status

include/swift/AST/ReferencedNameTracker.h

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,33 @@
1414
#define SWIFT_REFERENCEDNAMETRACKER_H
1515

1616
#include "swift/AST/Identifier.h"
17+
#include "swift/Basic/ReferenceDependencyKeys.h"
1718
#include "llvm/ADT/DenseMap.h"
1819

20+
#include <unordered_set>
21+
1922
namespace swift {
2023

2124
class NominalTypeDecl;
25+
class DependencyTracker;
2226

2327
class ReferencedNameTracker {
24-
#define TRACKED_SET(KIND, NAME) \
25-
private: \
26-
llvm::DenseMap<KIND, bool> NAME##s; \
27-
public: \
28-
void add##NAME(KIND new##NAME, bool isCascadingUse) { \
29-
NAME##s[new##NAME] |= isCascadingUse; \
30-
} \
31-
const decltype(NAME##s) &get##NAME##s() const { \
32-
return NAME##s; \
33-
}
28+
public:
29+
using EnumerateUsedDecl = function_ref<void(
30+
fine_grained_dependencies::NodeKind kind, StringRef context,
31+
StringRef name, bool isCascadingUse)>;
32+
33+
private:
34+
#define TRACKED_SET(KIND, NAME) \
35+
private: \
36+
llvm::DenseMap<KIND, bool> NAME##s; \
37+
\
38+
public: \
39+
void add##NAME(KIND new##NAME, bool isCascadingUse) { \
40+
NAME##s[new##NAME] |= isCascadingUse; \
41+
} \
42+
/* make private once ReferenceDependencies.cpp is gone */ \
43+
const decltype(NAME##s) &get##NAME##s() const { return NAME##s; }
3444

3545
TRACKED_SET(DeclBaseName, TopLevelName)
3646
TRACKED_SET(DeclBaseName, DynamicLookupName)
@@ -39,6 +49,33 @@ public: \
3949
TRACKED_SET(MemberPair, UsedMember)
4050

4151
#undef TRACKED_SET
52+
public:
53+
// Pushing the DependencyTracker through unifies external dependency
54+
// enumeration.
55+
void enumerateAllUses(bool includeIntrafileDeps,
56+
const DependencyTracker &depTracker,
57+
EnumerateUsedDecl enumerateUsedDecl) const;
58+
59+
private:
60+
template <fine_grained_dependencies::NodeKind kind>
61+
void enumerateSimpleUses(llvm::DenseMap<DeclBaseName, bool> cascadesByName,
62+
EnumerateUsedDecl enumerateUsedDecl) const;
63+
64+
void enumerateExternalUses(const DependencyTracker &,
65+
EnumerateUsedDecl enumerateUsedDecl) const;
66+
67+
void enumerateCompoundUses(bool includeIntrafileDeps,
68+
EnumerateUsedDecl enumerateUsedDecl) const;
69+
70+
std::unordered_set<std::string>
71+
computeHoldersOfCascadingMembers(bool includeIntrafileDeps) const;
72+
73+
void enumerateNominalUses(
74+
bool includeIntrafileDeps,
75+
const std::unordered_set<std::string> &&holdersOfCascadingMembers,
76+
EnumerateUsedDecl enumerateUsedDecl) const;
77+
78+
void enumerateMemberUses(EnumerateUsedDecl enumerateUsedDecl) const;
4279
};
4380

4481
} // end namespace swift

include/swift/Basic/ReferenceDependencyKeys.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,36 @@ static constexpr StringLiteral dependsExternal("depends-external");
3333

3434
static constexpr StringLiteral interfaceHash("interface-hash");
3535
} // end namespace reference_dependency_keys
36+
37+
namespace fine_grained_dependencies {
38+
/// Encode the current sorts of dependencies as kinds of nodes in the dependency
39+
/// graph, splitting the current *member* into \ref member and \ref
40+
/// potentialMember and adding \ref sourceFileProvide.
41+
42+
enum class NodeKind {
43+
topLevel,
44+
nominal,
45+
/// In the status quo scheme, *member* dependencies could have blank names
46+
/// for the member, to indicate that the provider might add members.
47+
/// This code uses a separate kind, \ref potentialMember. The holder field is
48+
/// unused.
49+
potentialMember,
50+
/// Corresponding to the status quo *member* dependency with a non-blank
51+
/// member.
52+
member,
53+
dynamicLookup,
54+
externalDepend,
55+
sourceFileProvide,
56+
/// For iterating through the NodeKinds.
57+
kindCount
58+
};
59+
60+
/// Used for printing out NodeKinds to dot files, and dumping nodes for
61+
/// debugging.
62+
const std::string NodeKindNames[]{
63+
"topLevel", "nominal", "potentialMember", "member",
64+
"dynamicLookup", "externalDepend", "sourceFileProvide"};
65+
} // end namespace fine_grained_dependencies
3666
} // end namespace swift
3767

3868
#endif

lib/AST/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ add_swift_host_library(swiftAST STATIC
6666
PrettyStackTrace.cpp
6767
ProtocolConformance.cpp
6868
RawComment.cpp
69+
ReferencedNameTracker.cpp
6970
RequirementEnvironment.cpp
7071
SyntaxASTMap.cpp
7172
SILLayout.cpp

lib/AST/Decl.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8068,3 +8068,26 @@ void swift::simple_display(llvm::raw_ostream &out, AnyFunctionRef fn) {
80688068
else
80698069
out << "closure";
80708070
}
8071+
8072+
bool Decl::isPrivateToEnclosingFile() const {
8073+
if (auto *VD = dyn_cast<ValueDecl>(this))
8074+
return VD->getFormalAccess() <= AccessLevel::FilePrivate;
8075+
switch (getKind()) {
8076+
case DeclKind::Import:
8077+
case DeclKind::PatternBinding:
8078+
case DeclKind::EnumCase:
8079+
case DeclKind::TopLevelCode:
8080+
case DeclKind::IfConfig:
8081+
case DeclKind::PoundDiagnostic:
8082+
return true;
8083+
8084+
case DeclKind::Extension:
8085+
case DeclKind::InfixOperator:
8086+
case DeclKind::PrefixOperator:
8087+
case DeclKind::PostfixOperator:
8088+
return false;
8089+
8090+
default:
8091+
llvm_unreachable("everything else is a ValueDecl");
8092+
}
8093+
}

0 commit comments

Comments
 (0)