Skip to content

Commit f4dc888

Browse files
author
David Ungar
committed
Further reorganization.
1 parent 314d1e1 commit f4dc888

File tree

4 files changed

+619
-574
lines changed

4 files changed

+619
-574
lines changed

include/swift/AST/SourceFileDepGraphConstructor.h

Lines changed: 114 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -17,124 +17,153 @@
1717

1818
namespace swift {
1919
namespace fine_grained_dependencies {
20-
/// Reads the information provided by the frontend and builds the
21-
/// SourceFileDepGraph
20+
/// Abstract class for building a \c SourceFileDepGraph from either a real
21+
/// \c SourceFile or a unit test
2222
class SourceFileDepGraphConstructor {
23+
protected:
2324
/// To match the existing system, set this to false.
2425
/// To include even private entities and get intra-file info, set to true.
2526
const bool includePrivateDeps;
2627

2728
/// If there was an error, cannot get accurate info.
2829
const bool hadCompilationError;
2930

30-
public:
31-
/// A lambda which invokes its parameter for every unconstrained \c Decl used
32-
/// in the source file, passing in the base name, and whether this use
33-
/// cascades..
34-
using ForEachUnconstrainedDeclUsed =
35-
function_ref<void(function_ref<void(StringRef, bool)>)>;
36-
37-
/// A lambda which invokes its parameter for every constrained \c Decl used in
38-
/// the source file, passing in the manged type name of the holder, the member
39-
/// base name, whether the holder is private to its source file, and whether
40-
/// the use cascades. If the baseName is empty, this entry will be a \c
41-
/// potentialMember dependency.
42-
using ForEachConstrainedDeclUsed =
43-
function_ref<void(function_ref<void(StringRef, StringRef, bool, bool)>)>;
44-
45-
/// Adds nodes to the graph corresponding to a defined \c Decl.
46-
/// Parameters are an interface key (implementation node will also be added)
47-
/// and the fingerprint if any.
48-
using AddDefinedDecl =
49-
function_ref<void(const DependencyKey &, Optional<StringRef>)>;
50-
51-
/// A lambda which invokes its parameter for every mock \c Decl "defined" in
52-
/// the source file, passing in to its argument the \c NodeKind, \c context,
53-
/// and \c name fields of resulting \c DependencyKey pair, and also the
54-
/// fingerprint (if present) of the resulting node pair.
55-
using ForEachDefinedDecl = function_ref<void(AddDefinedDecl)>;
56-
57-
/// A lambda which invokes its parameter for every use of a \c Decl in the
58-
/// source file, passing in to its argument the \c DependencyKey used, and the
59-
/// \c DependencyKey doing the using. (A.k.a. a "def" and an a "use".
60-
using ForEachUsedDecl = function_ref<void(
61-
function_ref<void(const DependencyKey &, const DependencyKey &)>)>;
31+
/// The name of the swiftDeps file.
32+
const std::string swiftDeps;
33+
34+
/// The fingerprint of the whole file
35+
const std::string fileFingerprint;
36+
37+
/// For debugging
38+
const bool emitDotFileAfterConstruction;
39+
40+
DiagnosticEngine &diags;
6241

63-
private:
6442
/// Graph under construction
6543
SourceFileDepGraph g;
6644

6745
public:
6846
/// Expose this layer to enable faking up a constructor for testing.
6947
/// See the instance variable comments for explanation.
70-
// clang-format off
71-
SourceFileDepGraphConstructor(
72-
bool includePrivateDeps,
73-
bool hadCompilationError
74-
) :
75-
includePrivateDeps(includePrivateDeps),
76-
hadCompilationError(hadCompilationError)
77-
{} // clang-format on
48+
SourceFileDepGraphConstructor(bool includePrivateDeps,
49+
bool hadCompilationError, StringRef swiftDeps,
50+
StringRef fileFingerprint,
51+
bool emitDotFileAfterConstruction,
52+
DiagnosticEngine &diags);
7853

79-
/// Create a SourceFileDepGraph.
80-
///
81-
/// \param swiftDeps The "output path name" and \c sourceFileProvide node
82-
/// names \param interfaceHash The fingerprint for the \c sourceFileProvide
83-
/// nodes. \param forEachMockDefinedDecl Iterator for "provides", i.e.
84-
/// definitions in this file \param forEachMockUsedDecl Iterator for
85-
/// "depends", i.e. used definitions in this file
86-
SourceFileDepGraph construct(StringRef swiftDeps, StringRef interfaceHash,
87-
ForEachDefinedDecl forEachDefinedDecl,
88-
ForEachUsedDecl forEachUsedDecl) {
89-
90-
return addSourceFileNodesAndThen(swiftDeps, interfaceHash, [&] {
91-
addAllDefinedDecls(forEachDefinedDecl);
92-
addAllUsedDecls(forEachUsedDecl);
93-
});
94-
}
95-
96-
/// Centralize the invariant that the fingerprint of the whole file is the
97-
/// interface hash
98-
static std::string getFingerprint(SourceFile *SF);
54+
virtual ~SourceFileDepGraphConstructor() = default;
9955

100-
void enumerateDefinedDecls(SourceFile *SF, AddDefinedDecl addDefinedDeclFn);
56+
/// Create a SourceFileDepGraph.
57+
SourceFileDepGraph construct();
10158

10259
private:
103-
/// Add the first two nodes in the graph and then, if there is no compilation
104-
/// error, do rest of the work to build the graph.
105-
SourceFileDepGraph addSourceFileNodesAndThen(StringRef name,
106-
StringRef fingerprint,
107-
function_ref<void()> doTheRest);
60+
void addSourceFileNodesToGraph();
10861

10962
/// Add the "provides" nodes when mocking up a graph
110-
void addAllDefinedDecls(ForEachDefinedDecl forEachDefinedDecl);
63+
virtual void addAllDefinedDecls() = 0;
11164

11265
/// Add the "depends" nodes and arcs when mocking a graph
113-
void addAllUsedDecls(ForEachUsedDecl forEachUsedDecl);
66+
virtual void addAllUsedDecls() = 0;
11467

115-
/// At present, only nominals, protocols, and extensions have (body)
116-
/// fingerprints
117-
static Optional<std::string>
118-
getFingerprintIfAny(std::pair<const NominalTypeDecl *, const ValueDecl *>);
68+
protected:
69+
/// Add an pair of interface, implementation nodes to the graph, which
70+
/// represent some \c Decl defined in this source file. \param key the
71+
/// interface key of the pair
72+
void addADefinedDecl(const DependencyKey &key,
73+
Optional<StringRef> fingerprint);
11974

120-
static Optional<std::string> getFingerprintIfAny(const Decl *d);
75+
void addAUsedDecl(const DependencyKey &def, const DependencyKey &use);
76+
};
77+
78+
/// Constructs a SourceFileDepGraph from a *real* \c SourceFile
79+
/// Reads the information provided by the frontend and builds the
80+
/// SourceFileDepGraph
81+
82+
class RealSourceFileDepGraphConstructor : public SourceFileDepGraphConstructor {
83+
SourceFile *const SF;
84+
const DependencyTracker &depTracker;
85+
86+
public:
87+
RealSourceFileDepGraphConstructor(SourceFile *SF, StringRef outputPath,
88+
const DependencyTracker &depTracker,
89+
bool alsoEmitDotFile);
12190

91+
~RealSourceFileDepGraphConstructor() override = default;
92+
93+
private:
94+
static std::string getFingerprint(SourceFile *SF);
95+
96+
static bool computeIncludePrivateDeps(SourceFile *SF);
12297
static std::string getInterfaceHash(SourceFile *SF);
12398

124-
void addSourceFileNodesToGraph(StringRef swiftDeps, StringRef fingerprint);
99+
void addAllDefinedDecls() override;
100+
void addAllUsedDecls() override;
125101

126102
/// Given an array of Decls or pairs of them in \p declsOrPairs
127103
/// create node pairs for context and name
128104
template <NodeKind kind, typename ContentsT>
129-
void
130-
enumerateAllProviderNodesOfAGivenType(std::vector<ContentsT> &contentsVec,
131-
AddDefinedDecl addDefinedDeclFn);
105+
void addAllDefinedDeclsOfAGivenType(std::vector<ContentsT> &contentsVec);
132106

133-
/// Add an pair of interface, implementation nodes to the graph, which
134-
/// represent some \c Decl defined in this source file. \param key the
135-
/// interface key of the pair
136-
void addDefinedDecl(const DependencyKey &key,
137-
Optional<StringRef> fingerprint);
107+
/// At present, only nominals, protocols, and extensions have (body)
108+
/// fingerprints
109+
static Optional<std::string>
110+
getFingerprintIfAny(std::pair<const NominalTypeDecl *, const ValueDecl *>);
111+
static Optional<std::string> getFingerprintIfAny(const Decl *d);
112+
};
113+
114+
using DependencyDescriptions =
115+
std::unordered_multimap<NodeKind, std::vector<std::string>>;
116+
117+
class MockSourceFileDepGraphConstructor : public SourceFileDepGraphConstructor {
118+
const DependencyDescriptions dependencyDescriptions;
119+
120+
public:
121+
MockSourceFileDepGraphConstructor(
122+
bool includePrivateDeps, bool hadCompilationError, StringRef swiftDeps,
123+
StringRef fileFingerprint, bool emitDotFileAfterConstruction,
124+
const DependencyDescriptions &dependencyDescriptions,
125+
DiagnosticEngine &diags)
126+
: SourceFileDepGraphConstructor(includePrivateDeps, hadCompilationError,
127+
swiftDeps, fileFingerprint,
128+
emitDotFileAfterConstruction, diags),
129+
dependencyDescriptions(dependencyDescriptions) {}
130+
131+
~MockSourceFileDepGraphConstructor() override = default;
132+
133+
private:
134+
void addAllDefinedDecls() override;
135+
void addAllUsedDecls() override;
136+
137+
/// For brevity, unit tests specify dependencies by NodeKind,
138+
/// but for processing, the kind is needed for each entry.
139+
void forEachEntry(function_ref<void(NodeKind kind, StringRef entry)> fn);
140+
141+
static const char *defUseSeparator;
142+
static bool isADefinedDecl(StringRef s);
143+
144+
void addADefinedDecl(StringRef s, NodeKind kind);
145+
void addAUsedDecl(StringRef s, NodeKind kind);
146+
147+
Optional<std::pair<DependencyKey, DependencyKey>> parseAUsedDecl(StringRef s,
148+
NodeKind);
149+
150+
/// Parse and return an interface \c DependencyKey
151+
Optional<DependencyKey> parseADefinedDecl(StringRef s, NodeKind, DeclAspect);
152+
153+
DependencyKey computeUseKey(StringRef s, bool isCascadingUse);
154+
155+
/// Return true if when the name appears in a unit test, it represents a
156+
/// context, not a baseName. Return false if a single name is a baseName,
157+
/// without context Return None if there shoud be two names
158+
static Optional<bool> singleNameIsContext(NodeKind kind);
159+
160+
static constexpr char nameContextSeparator = ',';
161+
162+
static constexpr char fingerprintSeparator = '@';
163+
164+
static std::string parseContext(const StringRef s, const NodeKind kind);
165+
166+
static std::string parseName(const StringRef s, const NodeKind kind);
138167
};
139168

140169
} // namespace fine_grained_dependencies

0 commit comments

Comments
 (0)