|
17 | 17 |
|
18 | 18 | namespace swift {
|
19 | 19 | 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 |
22 | 22 | class SourceFileDepGraphConstructor {
|
| 23 | +protected: |
23 | 24 | /// To match the existing system, set this to false.
|
24 | 25 | /// To include even private entities and get intra-file info, set to true.
|
25 | 26 | const bool includePrivateDeps;
|
26 | 27 |
|
27 | 28 | /// If there was an error, cannot get accurate info.
|
28 | 29 | const bool hadCompilationError;
|
29 | 30 |
|
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; |
62 | 41 |
|
63 |
| -private: |
64 | 42 | /// Graph under construction
|
65 | 43 | SourceFileDepGraph g;
|
66 | 44 |
|
67 | 45 | public:
|
68 | 46 | /// Expose this layer to enable faking up a constructor for testing.
|
69 | 47 | /// 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); |
78 | 53 |
|
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; |
99 | 55 |
|
100 |
| - void enumerateDefinedDecls(SourceFile *SF, AddDefinedDecl addDefinedDeclFn); |
| 56 | + /// Create a SourceFileDepGraph. |
| 57 | + SourceFileDepGraph construct(); |
101 | 58 |
|
102 | 59 | 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(); |
108 | 61 |
|
109 | 62 | /// Add the "provides" nodes when mocking up a graph
|
110 |
| - void addAllDefinedDecls(ForEachDefinedDecl forEachDefinedDecl); |
| 63 | + virtual void addAllDefinedDecls() = 0; |
111 | 64 |
|
112 | 65 | /// Add the "depends" nodes and arcs when mocking a graph
|
113 |
| - void addAllUsedDecls(ForEachUsedDecl forEachUsedDecl); |
| 66 | + virtual void addAllUsedDecls() = 0; |
114 | 67 |
|
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); |
119 | 74 |
|
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); |
121 | 90 |
|
| 91 | + ~RealSourceFileDepGraphConstructor() override = default; |
| 92 | + |
| 93 | +private: |
| 94 | + static std::string getFingerprint(SourceFile *SF); |
| 95 | + |
| 96 | + static bool computeIncludePrivateDeps(SourceFile *SF); |
122 | 97 | static std::string getInterfaceHash(SourceFile *SF);
|
123 | 98 |
|
124 |
| - void addSourceFileNodesToGraph(StringRef swiftDeps, StringRef fingerprint); |
| 99 | + void addAllDefinedDecls() override; |
| 100 | + void addAllUsedDecls() override; |
125 | 101 |
|
126 | 102 | /// Given an array of Decls or pairs of them in \p declsOrPairs
|
127 | 103 | /// create node pairs for context and name
|
128 | 104 | template <NodeKind kind, typename ContentsT>
|
129 |
| - void |
130 |
| - enumerateAllProviderNodesOfAGivenType(std::vector<ContentsT> &contentsVec, |
131 |
| - AddDefinedDecl addDefinedDeclFn); |
| 105 | + void addAllDefinedDeclsOfAGivenType(std::vector<ContentsT> &contentsVec); |
132 | 106 |
|
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); |
138 | 167 | };
|
139 | 168 |
|
140 | 169 | } // namespace fine_grained_dependencies
|
|
0 commit comments