|
19 | 19 | #include "swift/Basic/FileTypes.h" |
20 | 20 | #include "swift/Frontend/ModuleInterfaceLoader.h" |
21 | 21 | #include "swift/Serialization/SerializedModuleLoader.h" |
| 22 | +#include "swift/Serialization/ModuleDependencyScanner.h" |
22 | 23 | #include "swift/Subsystems.h" |
23 | 24 | using namespace swift; |
24 | 25 | using llvm::ErrorOr; |
25 | 26 |
|
26 | | -namespace { |
27 | 27 |
|
28 | | -/// A module "loader" that looks for .swiftinterface and .swiftmodule files |
29 | | -/// for the purpose of determining dependencies, but does not attempt to |
30 | | -/// load the module files. |
31 | | -class ModuleDependencyScanner : public SerializedModuleLoaderBase { |
32 | | - /// The module we're scanning dependencies of. |
33 | | - Identifier moduleName; |
34 | | - |
35 | | - /// Scan the given interface file to determine dependencies. |
36 | | - ErrorOr<ModuleDependencies> scanInterfaceFile( |
37 | | - Twine moduleInterfacePath, bool isFramework); |
38 | | - |
39 | | - InterfaceSubContextDelegate &astDelegate; |
40 | | -public: |
41 | | - Optional<ModuleDependencies> dependencies; |
42 | | - |
43 | | - /// Describes the kind of dependencies this scanner is able to identify |
44 | | - ModuleDependenciesKind dependencyKind; |
45 | | - |
46 | | - ModuleDependencyScanner( |
47 | | - ASTContext &ctx, ModuleLoadingMode LoadMode, Identifier moduleName, |
48 | | - InterfaceSubContextDelegate &astDelegate, |
49 | | - ModuleDependenciesKind dependencyKind = ModuleDependenciesKind::Swift) |
50 | | - : SerializedModuleLoaderBase(ctx, nullptr, LoadMode, |
51 | | - /*IgnoreSwiftSourceInfoFile=*/true), |
52 | | - moduleName(moduleName), astDelegate(astDelegate), |
53 | | - dependencyKind(dependencyKind) {} |
54 | | - |
55 | | - virtual std::error_code findModuleFilesInDirectory( |
56 | | - AccessPathElem ModuleID, |
57 | | - const SerializedModuleBaseName &BaseName, |
58 | | - SmallVectorImpl<char> *ModuleInterfacePath, |
59 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, |
60 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, |
61 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer, |
62 | | - bool IsFramework) override { |
63 | | - using namespace llvm::sys; |
64 | | - |
65 | | - auto &fs = *Ctx.SourceMgr.getFileSystem(); |
66 | | - |
67 | | - auto ModPath = BaseName.getName(file_types::TY_SwiftModuleFile); |
68 | | - auto InPath = BaseName.getName(file_types::TY_SwiftModuleInterfaceFile); |
69 | | - |
70 | | - if (LoadMode == ModuleLoadingMode::OnlySerialized || !fs.exists(InPath)) { |
71 | | - if (fs.exists(ModPath)) { |
72 | | - // The module file will be loaded directly. |
73 | | - auto dependencies = scanModuleFile(ModPath); |
74 | | - if (dependencies) { |
75 | | - this->dependencies = std::move(dependencies.get()); |
76 | | - return std::error_code(); |
77 | | - } |
78 | | - return dependencies.getError(); |
79 | | - } else { |
80 | | - return std::make_error_code(std::errc::no_such_file_or_directory); |
| 28 | +std::error_code ModuleDependencyScanner::findModuleFilesInDirectory( |
| 29 | + AccessPathElem ModuleID, |
| 30 | + const SerializedModuleBaseName &BaseName, |
| 31 | + SmallVectorImpl<char> *ModuleInterfacePath, |
| 32 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, |
| 33 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, |
| 34 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer, |
| 35 | + bool IsFramework) { |
| 36 | + using namespace llvm::sys; |
| 37 | + |
| 38 | + auto &fs = *Ctx.SourceMgr.getFileSystem(); |
| 39 | + |
| 40 | + auto ModPath = BaseName.getName(file_types::TY_SwiftModuleFile); |
| 41 | + auto InPath = BaseName.getName(file_types::TY_SwiftModuleInterfaceFile); |
| 42 | + |
| 43 | + if (LoadMode == ModuleLoadingMode::OnlySerialized || !fs.exists(InPath)) { |
| 44 | + if (fs.exists(ModPath)) { |
| 45 | + // The module file will be loaded directly. |
| 46 | + auto dependencies = scanModuleFile(ModPath); |
| 47 | + if (dependencies) { |
| 48 | + this->dependencies = std::move(dependencies.get()); |
| 49 | + return std::error_code(); |
81 | 50 | } |
| 51 | + return dependencies.getError(); |
| 52 | + } else { |
| 53 | + return std::make_error_code(std::errc::no_such_file_or_directory); |
82 | 54 | } |
83 | | - assert(fs.exists(InPath)); |
84 | | - // Use the private interface file if exits. |
85 | | - auto PrivateInPath = |
86 | | - BaseName.getName(file_types::TY_PrivateSwiftModuleInterfaceFile); |
87 | | - if (fs.exists(PrivateInPath)) { |
88 | | - InPath = PrivateInPath; |
89 | | - } |
90 | | - auto dependencies = scanInterfaceFile(InPath, IsFramework); |
91 | | - if (dependencies) { |
92 | | - this->dependencies = std::move(dependencies.get()); |
93 | | - return std::error_code(); |
94 | | - } |
95 | | - |
96 | | - return dependencies.getError(); |
97 | 55 | } |
98 | | - |
99 | | - virtual void collectVisibleTopLevelModuleNames( |
100 | | - SmallVectorImpl<Identifier> &names) const override { |
101 | | - llvm_unreachable("Not used"); |
| 56 | + assert(fs.exists(InPath)); |
| 57 | + // Use the private interface file if exits. |
| 58 | + auto PrivateInPath = |
| 59 | + BaseName.getName(file_types::TY_PrivateSwiftModuleInterfaceFile); |
| 60 | + if (fs.exists(PrivateInPath)) { |
| 61 | + InPath = PrivateInPath; |
102 | 62 | } |
103 | | -}; |
104 | | - |
105 | | -/// A ModuleLoader that loads placeholder dependency module stubs specified in |
106 | | -/// -placeholder-dependency-module-map-file |
107 | | -/// This loader is used only in dependency scanning to inform the scanner that a |
108 | | -/// set of modules constitute placeholder dependencies that are not visible to the |
109 | | -/// scanner but will nevertheless be provided by the scanner's clients. |
110 | | -/// This "loader" will not attempt to load any module files. |
111 | | -class PlaceholderSwiftModuleScanner : public ModuleDependencyScanner { |
112 | | - /// Scan the given placeholder module map |
113 | | - void parsePlaceholderModuleMap(StringRef fileName) { |
114 | | - ExplicitModuleMapParser parser(Allocator); |
115 | | - auto result = |
116 | | - parser.parseSwiftExplicitModuleMap(fileName, PlaceholderDependencyModuleMap); |
117 | | - if (result == std::errc::invalid_argument) { |
118 | | - Ctx.Diags.diagnose(SourceLoc(), |
119 | | - diag::placeholder_dependency_module_map_corrupted, |
120 | | - fileName); |
121 | | - } |
122 | | - else if (result == std::errc::no_such_file_or_directory) { |
123 | | - Ctx.Diags.diagnose(SourceLoc(), |
124 | | - diag::placeholder_dependency_module_map_missing, |
125 | | - fileName); |
126 | | - } |
| 63 | + auto dependencies = scanInterfaceFile(InPath, IsFramework); |
| 64 | + if (dependencies) { |
| 65 | + this->dependencies = std::move(dependencies.get()); |
| 66 | + return std::error_code(); |
127 | 67 | } |
128 | 68 |
|
129 | | - llvm::StringMap<ExplicitModuleInfo> PlaceholderDependencyModuleMap; |
130 | | - llvm::BumpPtrAllocator Allocator; |
131 | | - |
132 | | -public: |
133 | | - PlaceholderSwiftModuleScanner(ASTContext &ctx, ModuleLoadingMode LoadMode, |
134 | | - Identifier moduleName, |
135 | | - StringRef PlaceholderDependencyModuleMap, |
136 | | - InterfaceSubContextDelegate &astDelegate) |
137 | | - : ModuleDependencyScanner(ctx, LoadMode, moduleName, astDelegate, |
138 | | - ModuleDependenciesKind::SwiftPlaceholder) { |
139 | | - |
140 | | - // FIXME: Find a better place for this map to live, to avoid |
141 | | - // doing the parsing on every module. |
142 | | - if (!PlaceholderDependencyModuleMap.empty()) { |
143 | | - parsePlaceholderModuleMap(PlaceholderDependencyModuleMap); |
144 | | - } |
145 | | - } |
| 69 | + return dependencies.getError(); |
| 70 | +} |
146 | 71 |
|
147 | | - std::error_code findModuleFilesInDirectory( |
148 | | - AccessPathElem ModuleID, const SerializedModuleBaseName &BaseName, |
149 | | - SmallVectorImpl<char> *ModuleInterfacePath, |
150 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, |
151 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, |
152 | | - std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer, |
153 | | - bool IsFramework) override { |
154 | | - StringRef moduleName = ModuleID.Item.str(); |
155 | | - auto it = PlaceholderDependencyModuleMap.find(moduleName); |
156 | | - // If no placeholder module stub path is given matches the name, return with an |
157 | | - // error code. |
158 | | - if (it == PlaceholderDependencyModuleMap.end()) { |
159 | | - return std::make_error_code(std::errc::not_supported); |
160 | | - } |
161 | | - auto &moduleInfo = it->getValue(); |
162 | | - assert(!moduleInfo.moduleBuffer && |
163 | | - "Placeholder dependency module stubs cannot have an associated buffer"); |
164 | 72 |
|
165 | | - auto dependencies = ModuleDependencies::forPlaceholderSwiftModuleStub( |
166 | | - moduleInfo.modulePath, moduleInfo.moduleDocPath, |
167 | | - moduleInfo.moduleSourceInfoPath); |
168 | | - this->dependencies = std::move(dependencies); |
169 | | - return std::error_code{}; |
| 73 | +std::error_code PlaceholderSwiftModuleScanner::findModuleFilesInDirectory( |
| 74 | + AccessPathElem ModuleID, const SerializedModuleBaseName &BaseName, |
| 75 | + SmallVectorImpl<char> *ModuleInterfacePath, |
| 76 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer, |
| 77 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer, |
| 78 | + std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer, |
| 79 | + bool IsFramework) { |
| 80 | + StringRef moduleName = ModuleID.Item.str(); |
| 81 | + auto it = PlaceholderDependencyModuleMap.find(moduleName); |
| 82 | + // If no placeholder module stub path is given matches the name, return with an |
| 83 | + // error code. |
| 84 | + if (it == PlaceholderDependencyModuleMap.end()) { |
| 85 | + return std::make_error_code(std::errc::not_supported); |
170 | 86 | } |
171 | | -}; |
172 | | -} // namespace |
| 87 | + auto &moduleInfo = it->getValue(); |
| 88 | + assert(!moduleInfo.moduleBuffer && |
| 89 | + "Placeholder dependency module stubs cannot have an associated buffer"); |
| 90 | + |
| 91 | + auto dependencies = ModuleDependencies::forPlaceholderSwiftModuleStub( |
| 92 | + moduleInfo.modulePath, moduleInfo.moduleDocPath, |
| 93 | + moduleInfo.moduleSourceInfoPath); |
| 94 | + this->dependencies = std::move(dependencies); |
| 95 | + return std::error_code{}; |
| 96 | +} |
173 | 97 |
|
174 | 98 | static std::vector<std::string> getCompiledCandidates(ASTContext &ctx, |
175 | 99 | StringRef moduleName, |
|
0 commit comments