@@ -93,14 +93,20 @@ template <typename KeyT, typename ValueT> class Memoizer {
93
93
public:
94
94
Memoizer () = default ;
95
95
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
+
96
103
// / \p createFn must create a \ref ValueT that corresponds to the \ref KeyT
97
104
// / passed into it.
98
105
ValueT
99
106
findExistingOrCreateIfNew (KeyT key,
100
107
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 ();
104
110
ValueT v = createFn (key);
105
111
(void )insert (key, v);
106
112
return v;
@@ -372,6 +378,7 @@ const std::string NodeKindNames[]{
372
378
" topLevel" , " nominal" , " potentialMember" , " member" ,
373
379
" dynamicLookup" , " externalDepend" , " sourceFileProvide" };
374
380
381
+
375
382
// / Instead of the status quo scheme of two kinds of "Depends", cascading and
376
383
// / non-cascading this code represents each entity ("Provides" in the status
377
384
// / quo), by a pair of nodes. One node represents the "implementation." If the
@@ -391,6 +398,7 @@ template <typename FnT> void forEachAspect(FnT fn) {
391
398
fn (DeclAspect (i));
392
399
}
393
400
401
+
394
402
// / A pair of nodes that represent the two aspects of a given entity.
395
403
// / Templated in order to serve for either SourceFileDepGraphNodes or
396
404
// / ModuleDepGraphNodes.
@@ -499,10 +507,23 @@ class DependencyKey {
499
507
}
500
508
bool isInterface () const { return getAspect () == DeclAspect::interface; }
501
509
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
+
502
515
// / Given some type of provided entity compute the context field of the key.
503
516
template <NodeKind kind, typename Entity>
504
517
static std::string computeContextForProvidedEntity (Entity);
505
518
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
+
506
527
// / Given some type of provided entity compute the name field of the key.
507
528
template <NodeKind kind, typename Entity>
508
529
static std::string computeNameForProvidedEntity (Entity);
@@ -514,7 +535,8 @@ class DependencyKey {
514
535
template <NodeKind kind>
515
536
static DependencyKey createDependedUponKey (StringRef);
516
537
517
- static DependencyKey createKeyForWholeSourceFile (StringRef swiftDeps);
538
+ static DependencyKey createKeyForWholeSourceFile (DeclAspect,
539
+ StringRef swiftDeps);
518
540
519
541
std::string humanReadableName () const ;
520
542
@@ -555,6 +577,14 @@ struct std::hash<typename swift::fine_grained_dependencies::DeclAspect> {
555
577
return size_t (aspect);
556
578
}
557
579
};
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
+
558
588
559
589
namespace swift {
560
590
namespace fine_grained_dependencies {
@@ -616,6 +646,10 @@ class DepGraphNode {
616
646
// / See SourceFileDepGraphNode::SourceFileDepGraphNode(...) and
617
647
// / ModuleDepGraphNode::ModuleDepGraphNode(...) Don't set swiftDeps on
618
648
// / 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
+
619
653
DepGraphNode (DependencyKey key, Optional<std::string> fingerprint)
620
654
: key(key), fingerprint(fingerprint) {}
621
655
DepGraphNode (const DepGraphNode &other) = default;
@@ -627,8 +661,12 @@ class DepGraphNode {
627
661
628
662
const DependencyKey &getKey () const { return key; }
629
663
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
+ }
632
670
// / When driver reads a SourceFileDepGraphNode, it may be a node that was
633
671
// / created to represent a name-lookup (a.k.a a "depend") in the frontend. In
634
672
// / that case, the node represents an entity that resides in some other file
@@ -637,7 +675,9 @@ class DepGraphNode {
637
675
// / (someday) have a fingerprint. In order to preserve the
638
676
// / ModuleDepGraphNode's identity but bring its fingerprint up to date, it
639
677
// / 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
+ }
641
681
642
682
SWIFT_DEBUG_DUMP;
643
683
void dump (llvm::raw_ostream &os) const ;
@@ -684,7 +724,7 @@ class SourceFileDepGraphNode : public DepGraphNode {
684
724
SourceFileDepGraphNode () : DepGraphNode(), sequenceNumber(~0 ) {}
685
725
686
726
// / Used by the frontend to build nodes.
687
- SourceFileDepGraphNode (DependencyKey key, Optional<std::string > fingerprint,
727
+ SourceFileDepGraphNode (DependencyKey key, Optional<StringRef > fingerprint,
688
728
bool isProvides)
689
729
: DepGraphNode(key, fingerprint), isProvides(isProvides) {
690
730
assert (key.verify ());
@@ -780,34 +820,6 @@ class SourceFileDepGraph {
780
820
SourceFileDepGraph (const SourceFileDepGraph &g) = delete ;
781
821
SourceFileDepGraph (SourceFileDepGraph &&g) = default ;
782
822
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
-
811
823
// / Nodes are owned by the graph.
812
824
~SourceFileDepGraph () {
813
825
forEachNode ([&](SourceFileDepGraphNode *n) { delete n; });
@@ -851,12 +863,15 @@ class SourceFileDepGraph {
851
863
// / The frontend creates a pair of nodes for every tracked Decl and the source
852
864
// / file itself.
853
865
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);
856
871
857
872
SourceFileDepGraphNode *
858
- findExistingNodeOrCreateIfNew (DependencyKey key,
859
- const Optional<std::string> & fingerprint,
873
+ findExistingNodeOrCreateIfNew (const DependencyKey & key,
874
+ const Optional<StringRef> fingerprint,
860
875
bool isProvides);
861
876
862
877
// / \p Use is the Node that must be rebuilt when \p def changes.
0 commit comments