@@ -74,29 +74,44 @@ enum class ModuleDependencyKind : int8_t {
74
74
LastKind = SwiftPlaceholder + 1
75
75
};
76
76
77
+ using ModuleDependencyID = std::pair<std::string, ModuleDependencyKind>;
78
+ using ModuleDependencyIDSetVector =
79
+ llvm::SetVector<ModuleDependencyID, std::vector<ModuleDependencyID>,
80
+ std::set<ModuleDependencyID>>;
81
+
77
82
struct ModuleDependencyKindHash {
78
83
std::size_t operator ()(ModuleDependencyKind k) const {
79
84
using UnderlyingType = std::underlying_type<ModuleDependencyKind>::type;
80
85
return std::hash<UnderlyingType>{}(static_cast <UnderlyingType>(k));
81
86
}
82
87
};
83
88
89
+ namespace dependencies {
90
+ std::string createEncodedModuleKindAndName (ModuleDependencyID id);
91
+ }
92
+
84
93
// / Base class for the variant storage of ModuleDependencyInfo.
85
94
// /
86
95
// / This class is mostly an implementation detail for \c ModuleDependencyInfo.
87
96
class ModuleDependencyInfoStorageBase {
88
97
public:
89
98
const ModuleDependencyKind dependencyKind;
90
99
91
- ModuleDependencyInfoStorageBase (ModuleDependencyKind dependencyKind)
92
- : dependencyKind(dependencyKind) { }
100
+ ModuleDependencyInfoStorageBase (ModuleDependencyKind dependencyKind,
101
+ bool resolved = false )
102
+ : dependencyKind(dependencyKind), resolved(resolved) { }
93
103
94
104
virtual ModuleDependencyInfoStorageBase *clone () const = 0;
95
105
96
106
virtual ~ModuleDependencyInfoStorageBase ();
97
107
98
108
// / The set of modules on which this module depends.
99
- std::vector<std::string> moduleDependencies;
109
+ std::vector<std::string> moduleImports;
110
+
111
+ // / The set of modules on which this module depends, resolved
112
+ // / to Module IDs, qualified by module kind: Swift, Clang, etc.
113
+ std::vector<ModuleDependencyID> resolvedModuleDependencies;
114
+ bool resolved;
100
115
};
101
116
102
117
struct CommonSwiftTextualModuleDependencyDetails {
@@ -400,9 +415,29 @@ class ModuleDependencyInfo {
400
415
compiledModulePath, moduleDocPath, sourceInfoPath));
401
416
}
402
417
403
- // / Retrieve the module-level dependencies.
404
- ArrayRef<std::string> getModuleDependencies () const {
405
- return storage->moduleDependencies ;
418
+ // / Retrieve the module-level imports.
419
+ ArrayRef<std::string> getModuleImports () const {
420
+ return storage->moduleImports ;
421
+ }
422
+
423
+ // / Retreive the module-level dependencies.
424
+ const ArrayRef<ModuleDependencyID> getModuleDependencies () const {
425
+ assert (storage->resolved );
426
+ return storage->resolvedModuleDependencies ;
427
+ }
428
+
429
+ // / Resolve a dependency's set of `imports` with qualified Module IDs
430
+ void resolveDependencies (const std::vector<ModuleDependencyID> &dependencyIDs) {
431
+ assert (!storage->resolved && " Resolving an already-resolved dependency" );
432
+ storage->resolved = true ;
433
+ storage->resolvedModuleDependencies .assign (dependencyIDs.begin (), dependencyIDs.end ());
434
+ }
435
+
436
+ bool isResolved () const {
437
+ return storage->resolved ;
438
+ }
439
+ void setIsResolved (bool isResolved) {
440
+ storage->resolved = isResolved;
406
441
}
407
442
408
443
// / Whether the dependencies are for a Swift module: either Textual, Source, Binary, or Placeholder.
@@ -444,19 +479,22 @@ class ModuleDependencyInfo {
444
479
getAsPlaceholderDependencyModule () const ;
445
480
446
481
// / Add a dependency on the given module, if it was not already in the set.
447
- void addModuleDependency (StringRef module ,
448
- llvm::StringSet<> *alreadyAddedModules = nullptr );
482
+ void addModuleImport (StringRef module ,
483
+ llvm::StringSet<> *alreadyAddedModules = nullptr );
449
484
450
485
// / Add a dependency on the given module, if it was not already in the set.
451
- void addModuleDependency (ImportPath::Module module ,
452
- llvm::StringSet<> *alreadyAddedModules = nullptr ) {
453
- addModuleDependency (module .front ().Item .str (), alreadyAddedModules);
486
+ void addModuleImport (ImportPath::Module module ,
487
+ llvm::StringSet<> *alreadyAddedModules = nullptr ) {
488
+ addModuleImport (module .front ().Item .str (), alreadyAddedModules);
454
489
}
455
490
456
- // / Add all of the module dependencies for the imports in the given source
457
- // / file to the set of module dependencies.
458
- void addModuleDependencies (const SourceFile &sf,
459
- llvm::StringSet<> &alreadyAddedModules);
491
+ // / Add all of the module imports in the given source
492
+ // / file to the set of module imports.
493
+ void addModuleImport (const SourceFile &sf,
494
+ llvm::StringSet<> &alreadyAddedModules);
495
+ // / Add a kind-qualified module dependency ID to the set of
496
+ // / module dependencies.
497
+ void addModuleDependency (ModuleDependencyID dependencyID);
460
498
461
499
// / Get the bridging header.
462
500
Optional<std::string> getBridgingHeader () const ;
@@ -477,11 +515,9 @@ class ModuleDependencyInfo {
477
515
// / Collect a map from a secondary module name to a list of cross-import
478
516
// / overlays, when this current module serves as the primary module.
479
517
llvm::StringMap<llvm::SmallSetVector<Identifier, 4 >>
480
- collectCrossImportOverlayNames (ASTContext &ctx, StringRef moduleName);
518
+ collectCrossImportOverlayNames (ASTContext &ctx, StringRef moduleName) const ;
481
519
};
482
520
483
- using ModuleDependencyID = std::pair<std::string, ModuleDependencyKind>;
484
- using ModuleDependenciesVector = llvm::SmallVector<ModuleDependencyInfo, 1 >;
485
521
using ModuleNameToDependencyMap = llvm::StringMap<ModuleDependencyInfo>;
486
522
using ModuleDependenciesKindMap =
487
523
std::unordered_map<ModuleDependencyKind,
@@ -505,6 +541,9 @@ class SwiftDependencyScanningService {
505
541
// / encountered.
506
542
std::vector<ModuleDependencyID> AllModules;
507
543
544
+ // / Set containing all of the Clang modules that have already been seen.
545
+ llvm::StringSet<> alreadySeenClangModules;
546
+
508
547
// / Dependencies for modules that have already been computed.
509
548
// / This maps a dependency kind to a map of a module's name to a Dependency
510
549
// / object
@@ -565,6 +604,10 @@ class SwiftDependencyScanningService {
565
604
return *SharedFilesystemCache;
566
605
}
567
606
607
+ llvm::StringSet<>& getAlreadySeenClangModules () {
608
+ return getCurrentCache ()->alreadySeenClangModules ;
609
+ }
610
+
568
611
// / Wrap the filesystem on the specified `CompilerInstance` with a
569
612
// / caching `DependencyScanningWorkerFilesystem`
570
613
void overlaySharedFilesystemCacheForCompilation (CompilerInstance &Instance);
@@ -587,8 +630,8 @@ class SwiftDependencyScanningService {
587
630
}
588
631
589
632
// / Whether we have cached dependency information for the given module.
590
- bool hasDependencies (StringRef moduleName,
591
- Optional<ModuleDependencyKind> kind) const ;
633
+ bool hasDependency (StringRef moduleName,
634
+ Optional<ModuleDependencyKind> kind) const ;
592
635
593
636
// / Return a pointer to the context-specific cache state of the current
594
637
// / scanning action.
@@ -599,22 +642,22 @@ class SwiftDependencyScanningService {
599
642
getCacheForScanningContextHash (StringRef scanningContextHash) const ;
600
643
601
644
// / Look for source-based module dependency details
602
- Optional<ModuleDependencyInfo>
645
+ Optional<const ModuleDependencyInfo* >
603
646
findSourceModuleDependency (StringRef moduleName) const ;
604
647
605
648
// / Look for module dependencies for a module with the given name
606
649
// /
607
650
// / \returns the cached result, or \c None if there is no cached entry.
608
- Optional<ModuleDependencyInfo>
609
- findDependencies (StringRef moduleName,
610
- Optional<ModuleDependencyKind> kind) const ;
651
+ Optional<const ModuleDependencyInfo* >
652
+ findDependency (StringRef moduleName,
653
+ Optional<ModuleDependencyKind> kind) const ;
611
654
612
655
// / Record dependencies for the given module.
613
- const ModuleDependencyInfo *recordDependencies (StringRef moduleName,
656
+ const ModuleDependencyInfo *recordDependency (StringRef moduleName,
614
657
ModuleDependencyInfo dependencies);
615
658
616
659
// / Update stored dependencies for the given module.
617
- const ModuleDependencyInfo *updateDependencies (ModuleDependencyID moduleID,
660
+ const ModuleDependencyInfo *updateDependency (ModuleDependencyID moduleID,
618
661
ModuleDependencyInfo dependencies);
619
662
620
663
// / Reference the list of all module dependencies that are not source-based
@@ -642,23 +685,14 @@ class SwiftDependencyScanningService {
642
685
class ModuleDependenciesCache {
643
686
private:
644
687
SwiftDependencyScanningService &globalScanningService;
645
-
646
- // / References to data in `globalCache` for Swift dependencies and
647
- // / `clangModuleDependencies` for Clang dependencies accimulated during
648
- // / the current scanning action.
688
+ // / References to data in the `globalScanningService` for module dependencies
649
689
ModuleDependenciesKindRefMap ModuleDependenciesMap;
650
-
651
690
// / Name of the module under scan
652
691
std::string mainScanModuleName;
653
692
// / The context hash of the current scanning invocation
654
693
std::string scannerContextHash;
655
-
656
- // / Set containing all of the Clang modules that have already been seen.
657
- llvm::StringSet<> alreadySeenClangModules;
658
694
// / The Clang dependency scanner tool
659
695
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
660
- // / Discovered Clang modules are only cached locally.
661
- llvm::StringMap<ModuleDependenciesVector> clangModuleDependencies;
662
696
663
697
// / Retrieve the dependencies map that corresponds to the given dependency
664
698
// / kind.
@@ -667,17 +701,6 @@ class ModuleDependenciesCache {
667
701
const llvm::StringMap<const ModuleDependencyInfo *> &
668
702
getDependencyReferencesMap (ModuleDependencyKind kind) const ;
669
703
670
- // / Local cache results lookup, only for modules which were discovered during
671
- // / the current scanner invocation.
672
- bool hasDependenciesLocalOnly (StringRef moduleName,
673
- Optional<ModuleDependencyKind> kind) const ;
674
-
675
- // / Local cache results lookup, only for modules which were discovered during
676
- // / the current scanner invocation.
677
- Optional<const ModuleDependencyInfo *>
678
- findDependenciesLocalOnly (StringRef moduleName,
679
- Optional<ModuleDependencyKind> kind) const ;
680
-
681
704
public:
682
705
ModuleDependenciesCache (SwiftDependencyScanningService &globalScanningService,
683
706
std::string mainScanModuleName,
@@ -687,31 +710,36 @@ class ModuleDependenciesCache {
687
710
688
711
public:
689
712
// / Whether we have cached dependency information for the given module.
690
- bool hasDependencies (StringRef moduleName,
691
- Optional<ModuleDependencyKind> kind) const ;
713
+ bool hasDependency (StringRef moduleName,
714
+ Optional<ModuleDependencyKind> kind) const ;
692
715
693
716
// / Produce a reference to the Clang scanner tool associated with this cache
694
717
clang::tooling::dependencies::DependencyScanningTool& getClangScannerTool () {
695
718
return clangScanningTool;
696
719
}
697
720
llvm::StringSet<>& getAlreadySeenClangModules () {
698
- return alreadySeenClangModules ;
721
+ return globalScanningService. getAlreadySeenClangModules () ;
699
722
}
700
723
701
724
// / Look for module dependencies for a module with the given name
702
725
// /
703
726
// / \returns the cached result, or \c None if there is no cached entry.
704
- Optional<ModuleDependencyInfo>
705
- findDependencies (StringRef moduleName,
706
- Optional<ModuleDependencyKind> kind) const ;
727
+ Optional<const ModuleDependencyInfo* >
728
+ findDependency (StringRef moduleName,
729
+ Optional<ModuleDependencyKind> kind) const ;
707
730
708
731
// / Record dependencies for the given module.
709
- void recordDependencies (StringRef moduleName,
710
- ModuleDependencyInfo dependencies);
732
+ void recordDependency (StringRef moduleName,
733
+ ModuleDependencyInfo dependencies);
711
734
712
735
// / Update stored dependencies for the given module.
713
- void updateDependencies (ModuleDependencyID moduleID,
714
- ModuleDependencyInfo dependencies);
736
+ void updateDependency (ModuleDependencyID moduleID,
737
+ ModuleDependencyInfo dependencies);
738
+
739
+ // / Resolve a dependency module's set of imports
740
+ // / to a kind-qualified set of module IDs.
741
+ void resolveDependencyImports (ModuleDependencyID moduleID,
742
+ const std::vector<ModuleDependencyID> &dependencyIDs);
715
743
716
744
const std::vector<ModuleDependencyID> &getAllSourceModules () const {
717
745
return globalScanningService.getAllSourceModules ();
0 commit comments