@@ -93,14 +93,20 @@ template <typename KeyT, typename ValueT> class Memoizer {
9393public:
9494 Memoizer () = default ;
9595
96+ Optional<ValueT> findExisting (KeyT key) {
97+ auto iter = memos.find (key);
98+ if (iter != memos.end ())
99+ return iter->second ;
100+ return None;
101+ }
102+
96103 // / \p createFn must create a \ref ValueT that corresponds to the \ref KeyT
97104 // / passed into it.
98105 ValueT
99106 findExistingOrCreateIfNew (KeyT key,
100107 function_ref<ValueT(const KeyT &)> createFn) {
101- auto iter = memos.find (key);
102- if (iter != memos.end ())
103- return iter->second ;
108+ if (auto existing = findExisting (key))
109+ return existing.getValue ();
104110 ValueT v = createFn (key);
105111 (void )insert (key, v);
106112 return v;
@@ -372,6 +378,7 @@ const std::string NodeKindNames[]{
372378 " topLevel" , " nominal" , " potentialMember" , " member" ,
373379 " dynamicLookup" , " externalDepend" , " sourceFileProvide" };
374380
381+
375382// / Instead of the status quo scheme of two kinds of "Depends", cascading and
376383// / non-cascading this code represents each entity ("Provides" in the status
377384// / quo), by a pair of nodes. One node represents the "implementation." If the
@@ -391,6 +398,7 @@ template <typename FnT> void forEachAspect(FnT fn) {
391398 fn (DeclAspect (i));
392399}
393400
401+
394402// / A pair of nodes that represent the two aspects of a given entity.
395403// / Templated in order to serve for either SourceFileDepGraphNodes or
396404// / ModuleDepGraphNodes.
@@ -499,10 +507,23 @@ class DependencyKey {
499507 }
500508 bool isInterface () const { return getAspect () == DeclAspect::interface; }
501509
510+ // / Create just the interface half of the keys for a provided Decl or Decl
511+ // / pair
512+ template <NodeKind kind, typename Entity>
513+ static DependencyKey createForProvidedEntityInterface (Entity);
514+
502515 // / Given some type of provided entity compute the context field of the key.
503516 template <NodeKind kind, typename Entity>
504517 static std::string computeContextForProvidedEntity (Entity);
505518
519+ DependencyKey correspondingImplementation () const {
520+ return withAspect (DeclAspect::implementation);
521+ }
522+
523+ DependencyKey withAspect (DeclAspect aspect) const {
524+ return DependencyKey (kind, aspect, context, name);
525+ }
526+
506527 // / Given some type of provided entity compute the name field of the key.
507528 template <NodeKind kind, typename Entity>
508529 static std::string computeNameForProvidedEntity (Entity);
@@ -514,7 +535,8 @@ class DependencyKey {
514535 template <NodeKind kind>
515536 static DependencyKey createDependedUponKey (StringRef);
516537
517- static DependencyKey createKeyForWholeSourceFile (StringRef swiftDeps);
538+ static DependencyKey createKeyForWholeSourceFile (DeclAspect,
539+ StringRef swiftDeps);
518540
519541 std::string humanReadableName () const ;
520542
@@ -555,6 +577,14 @@ struct std::hash<typename swift::fine_grained_dependencies::DeclAspect> {
555577 return size_t (aspect);
556578 }
557579};
580+ template <>
581+ struct std ::hash<typename swift::fine_grained_dependencies::NodeKind> {
582+ size_t
583+ operator ()(const swift::fine_grained_dependencies::NodeKind kind) const {
584+ return size_t (kind);
585+ }
586+ };
587+
558588
559589namespace swift {
560590namespace fine_grained_dependencies {
@@ -616,6 +646,10 @@ class DepGraphNode {
616646 // / See SourceFileDepGraphNode::SourceFileDepGraphNode(...) and
617647 // / ModuleDepGraphNode::ModuleDepGraphNode(...) Don't set swiftDeps on
618648 // / creation because this field can change if a node is moved.
649+ DepGraphNode (DependencyKey key, Optional<StringRef> fingerprint)
650+ : DepGraphNode(key, fingerprint ? fingerprint->str ()
651+ : Optional<std::string>()) {}
652+
619653 DepGraphNode (DependencyKey key, Optional<std::string> fingerprint)
620654 : key(key), fingerprint(fingerprint) {}
621655 DepGraphNode (const DepGraphNode &other) = default;
@@ -627,8 +661,12 @@ class DepGraphNode {
627661
628662 const DependencyKey &getKey () const { return key; }
629663
630- const Optional<std::string> &getFingerprint () const { return fingerprint; }
631-
664+ const Optional<StringRef> getFingerprint () const {
665+ if (fingerprint) {
666+ return StringRef (fingerprint.getValue ());
667+ }
668+ return None;
669+ }
632670 // / When driver reads a SourceFileDepGraphNode, it may be a node that was
633671 // / created to represent a name-lookup (a.k.a a "depend") in the frontend. In
634672 // / that case, the node represents an entity that resides in some other file
@@ -637,7 +675,9 @@ class DepGraphNode {
637675 // / (someday) have a fingerprint. In order to preserve the
638676 // / ModuleDepGraphNode's identity but bring its fingerprint up to date, it
639677 // / needs to set the fingerprint *after* the node has been created.
640- void setFingerprint (Optional<std::string> fp) { fingerprint = fp; }
678+ void setFingerprint (Optional<StringRef> fp) {
679+ fingerprint = fp ? fp->str () : Optional<std::string>();
680+ }
641681
642682 SWIFT_DEBUG_DUMP;
643683 void dump (llvm::raw_ostream &os) const ;
@@ -684,7 +724,7 @@ class SourceFileDepGraphNode : public DepGraphNode {
684724 SourceFileDepGraphNode () : DepGraphNode(), sequenceNumber(~0 ) {}
685725
686726 // / Used by the frontend to build nodes.
687- SourceFileDepGraphNode (DependencyKey key, Optional<std::string > fingerprint,
727+ SourceFileDepGraphNode (DependencyKey key, Optional<StringRef > fingerprint,
688728 bool isProvides)
689729 : DepGraphNode(key, fingerprint), isProvides(isProvides) {
690730 assert (key.verify ());
@@ -780,34 +820,6 @@ class SourceFileDepGraph {
780820 SourceFileDepGraph (const SourceFileDepGraph &g) = delete ;
781821 SourceFileDepGraph (SourceFileDepGraph &&g) = default ;
782822
783- // / Simulate loading for unit testing:
784- // / \param swiftDepsFileName The name of the swiftdeps file of the phony job
785- // / \param includePrivateDeps Whether the graph includes intra-file arcs
786- // / \param hadCompilationError Simulate a compilation error
787- // / \param interfaceHash The interface hash of the simulated graph
788- // / \param simpleNamesByRDK A map of vectors of names keyed by reference
789- // / dependency key \param compoundNamesByRDK A map of (mangledHolder,
790- // / baseName) pairs keyed by reference dependency key. For single-name
791- // / dependencies, an initial underscore indicates that the name does not
792- // / cascade. For compound names, it is the first name, the holder which
793- // / indicates non-cascading. For member names, an initial underscore indicates
794- // / file-privacy.
795- static SourceFileDepGraph
796- simulateLoad (std::string swiftDepsFileName, const bool includePrivateDeps,
797- const bool hadCompilationError, std::string interfaceHash,
798- llvm::StringMap<std::vector<std::string>> simpleNamesByRDK,
799- llvm::StringMap<std::vector<std::pair<std::string, std::string>>>
800- compoundNamesByRDK);
801-
802- static constexpr char noncascadingOrPrivatePrefix = ' #' ;
803- static constexpr char nameFingerprintSeparator = ' ,' ;
804-
805- static std::string noncascading (std::string name);
806-
807- LLVM_ATTRIBUTE_UNUSED
808- static std::string privatize (std::string name);
809-
810-
811823 // / Nodes are owned by the graph.
812824 ~SourceFileDepGraph () {
813825 forEachNode ([&](SourceFileDepGraphNode *n) { delete n; });
@@ -851,12 +863,15 @@ class SourceFileDepGraph {
851863 // / The frontend creates a pair of nodes for every tracked Decl and the source
852864 // / file itself.
853865 InterfaceAndImplementationPair<SourceFileDepGraphNode>
854- findExistingNodePairOrCreateAndAddIfNew (
855- NodeKind k, const ContextNameFingerprint &contextNameFingerprint);
866+ findExistingNodePairOrCreateAndAddIfNew (const DependencyKey &interfaceKey,
867+ Optional<StringRef> fingerprint);
868+
869+ NullablePtr<SourceFileDepGraphNode>
870+ findExistingNode (const DependencyKey &key);
856871
857872 SourceFileDepGraphNode *
858- findExistingNodeOrCreateIfNew (DependencyKey key,
859- const Optional<std::string> & fingerprint,
873+ findExistingNodeOrCreateIfNew (const DependencyKey & key,
874+ const Optional<StringRef> fingerprint,
860875 bool isProvides);
861876
862877 // / \p Use is the Node that must be rebuilt when \p def changes.
0 commit comments