|
| 1 | +//===--- ModuleDependencies.h - Module Dependencies -------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | +// |
| 13 | +// This file defines data structures for capturing all of the source files |
| 14 | +// and modules on which a given module depends, forming a graph of all of the |
| 15 | +// modules that need to be present for a given module to be built. |
| 16 | +// |
| 17 | +//===----------------------------------------------------------------------===// |
| 18 | +#ifndef SWIFT_AST_MODULE_DEPENDENCIES_H |
| 19 | +#define SWIFT_AST_MODULE_DEPENDENCIES_H |
| 20 | + |
| 21 | +#include "swift/Basic/LLVM.h" |
| 22 | +#include "llvm/ADT/ArrayRef.h" |
| 23 | +#include "llvm/ADT/DenseMap.h" |
| 24 | +#include "llvm/ADT/Optional.h" |
| 25 | +#include "llvm/ADT/StringSet.h" |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | +namespace swift { |
| 30 | + |
| 31 | +class ClangModuleDependenciesCacheImpl; |
| 32 | +class SourceFile; |
| 33 | + |
| 34 | +/// Which kind of module dependencies we are looking for. |
| 35 | +enum class ModuleDependenciesKind : int8_t { |
| 36 | + Swift, |
| 37 | + Clang, |
| 38 | +}; |
| 39 | + |
| 40 | +/// Base class for the variant storage of ModuleDependencies. |
| 41 | +/// |
| 42 | +/// This class is mostly an implementation detail for \c ModuleDependencies. |
| 43 | +class ModuleDependenciesStorageBase { |
| 44 | +public: |
| 45 | + const bool isSwiftModule; |
| 46 | + |
| 47 | + ModuleDependenciesStorageBase(bool isSwiftModule, |
| 48 | + const std::string &compiledModulePath) |
| 49 | + : isSwiftModule(isSwiftModule), |
| 50 | + compiledModulePath(compiledModulePath) { } |
| 51 | + |
| 52 | + virtual ModuleDependenciesStorageBase *clone() const = 0; |
| 53 | + |
| 54 | + virtual ~ModuleDependenciesStorageBase(); |
| 55 | + |
| 56 | + /// The path to the compiled module file. |
| 57 | + const std::string compiledModulePath; |
| 58 | + |
| 59 | + /// The set of modules on which this module depends. |
| 60 | + std::vector<std::string> moduleDependencies; |
| 61 | +}; |
| 62 | + |
| 63 | +/// Describes the dependencies of a Swift module. |
| 64 | +/// |
| 65 | +/// This class is mostly an implementation detail for \c ModuleDependencies. |
| 66 | +class SwiftModuleDependenciesStorage : public ModuleDependenciesStorageBase { |
| 67 | +public: |
| 68 | + /// The Swift interface file, if it can be used to generate the module file. |
| 69 | + const Optional<std::string> swiftInterfaceFile; |
| 70 | + |
| 71 | + /// Bridging header file, if there is one. |
| 72 | + Optional<std::string> bridgingHeaderFile; |
| 73 | + |
| 74 | + /// Swift source files that are part of the Swift module, when known. |
| 75 | + std::vector<std::string> sourceFiles; |
| 76 | + |
| 77 | + /// Source files on which the bridging header depends. |
| 78 | + std::vector<std::string> bridgingSourceFiles; |
| 79 | + |
| 80 | + /// (Clang) modules on which the bridging header depends. |
| 81 | + std::vector<std::string> bridgingModuleDependencies; |
| 82 | + |
| 83 | + SwiftModuleDependenciesStorage( |
| 84 | + const std::string &compiledModulePath, |
| 85 | + const Optional<std::string> &swiftInterfaceFile |
| 86 | + ) : ModuleDependenciesStorageBase(/*isSwiftModule=*/true, compiledModulePath), |
| 87 | + swiftInterfaceFile(swiftInterfaceFile) { } |
| 88 | + |
| 89 | + ModuleDependenciesStorageBase *clone() const override { |
| 90 | + return new SwiftModuleDependenciesStorage(*this); |
| 91 | + } |
| 92 | + |
| 93 | + static bool classof(const ModuleDependenciesStorageBase *base) { |
| 94 | + return base->isSwiftModule; |
| 95 | + } |
| 96 | +}; |
| 97 | + |
| 98 | +/// Describes the dependencies of a Clang module. |
| 99 | +/// |
| 100 | +/// This class is mostly an implementation detail for \c ModuleDependencies. |
| 101 | +class ClangModuleDependenciesStorage : public ModuleDependenciesStorageBase { |
| 102 | +public: |
| 103 | + /// The module map file used to generate the Clang module. |
| 104 | + const std::string moduleMapFile; |
| 105 | + |
| 106 | + /// The context hash describing the configuration options for this module. |
| 107 | + const std::string contextHash; |
| 108 | + |
| 109 | + /// Partial (Clang) command line that can be used to build this module. |
| 110 | + const std::vector<std::string> nonPathCommandLine; |
| 111 | + |
| 112 | + /// The file dependencies |
| 113 | + const std::vector<std::string> fileDependencies; |
| 114 | + |
| 115 | + ClangModuleDependenciesStorage( |
| 116 | + const std::string &compiledModulePath, |
| 117 | + const std::string &moduleMapFile, |
| 118 | + const std::string &contextHash, |
| 119 | + const std::vector<std::string> &nonPathCommandLine, |
| 120 | + const std::vector<std::string> &fileDependencies |
| 121 | + ) : ModuleDependenciesStorageBase(/*isSwiftModule=*/false, |
| 122 | + compiledModulePath), |
| 123 | + moduleMapFile(moduleMapFile), |
| 124 | + contextHash(contextHash), |
| 125 | + nonPathCommandLine(nonPathCommandLine), |
| 126 | + fileDependencies(fileDependencies) { } |
| 127 | + |
| 128 | + ModuleDependenciesStorageBase *clone() const override { |
| 129 | + return new ClangModuleDependenciesStorage(*this); |
| 130 | + } |
| 131 | + |
| 132 | + static bool classof(const ModuleDependenciesStorageBase *base) { |
| 133 | + return !base->isSwiftModule; |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +/// Describes the dependencies of a given module. |
| 138 | +/// |
| 139 | +/// The dependencies of a module include all of the source files that go |
| 140 | +/// into that module, as well as any modules that are directly imported |
| 141 | +/// into the module. |
| 142 | +class ModuleDependencies { |
| 143 | +private: |
| 144 | + std::unique_ptr<ModuleDependenciesStorageBase> storage; |
| 145 | + |
| 146 | + ModuleDependencies(std::unique_ptr<ModuleDependenciesStorageBase> &&storage) |
| 147 | + : storage(std::move(storage)) { } |
| 148 | + |
| 149 | +public: |
| 150 | + ModuleDependencies(const ModuleDependencies &other) |
| 151 | + : storage(other.storage->clone()) { } |
| 152 | + ModuleDependencies(ModuleDependencies &&other) = default; |
| 153 | + |
| 154 | + ModuleDependencies &operator=(const ModuleDependencies &other) { |
| 155 | + storage.reset(other.storage->clone()); |
| 156 | + return *this; |
| 157 | + } |
| 158 | + |
| 159 | + ModuleDependencies &operator=(ModuleDependencies &&other) = default; |
| 160 | + |
| 161 | + /// Describe the module dependencies for a Swift module that can be |
| 162 | + /// built from a Swift interface file (\c .swiftinterface). |
| 163 | + static ModuleDependencies forSwiftInterface( |
| 164 | + const std::string &compiledModulePath, |
| 165 | + const std::string &swiftInterfaceFile) { |
| 166 | + return ModuleDependencies( |
| 167 | + std::make_unique<SwiftModuleDependenciesStorage>( |
| 168 | + compiledModulePath, swiftInterfaceFile)); |
| 169 | + } |
| 170 | + |
| 171 | + /// Describe the module dependencies for a serialized or parsed Swift module. |
| 172 | + static ModuleDependencies forSwiftModule( |
| 173 | + const std::string &compiledModulePath) { |
| 174 | + return ModuleDependencies( |
| 175 | + std::make_unique<SwiftModuleDependenciesStorage>( |
| 176 | + compiledModulePath, None)); |
| 177 | + } |
| 178 | + |
| 179 | + /// Describe the module dependencies for a Clang module that can be |
| 180 | + /// built from a module map and headers. |
| 181 | + static ModuleDependencies forClangModule( |
| 182 | + const std::string &compiledModulePath, |
| 183 | + const std::string &moduleMapFile, |
| 184 | + const std::string &contextHash, |
| 185 | + const std::vector<std::string> &nonPathCommandLine, |
| 186 | + const std::vector<std::string> &fileDependencies) { |
| 187 | + return ModuleDependencies( |
| 188 | + std::make_unique<ClangModuleDependenciesStorage>( |
| 189 | + compiledModulePath, moduleMapFile, contextHash, nonPathCommandLine, |
| 190 | + fileDependencies)); |
| 191 | + } |
| 192 | + |
| 193 | + /// Retrieve the path to the compiled module. |
| 194 | + const std::string getCompiledModulePath() const { |
| 195 | + return storage->compiledModulePath; |
| 196 | + } |
| 197 | + |
| 198 | + /// Retrieve the module-level dependencies. |
| 199 | + ArrayRef<std::string> getModuleDependencies() const { |
| 200 | + return storage->moduleDependencies; |
| 201 | + } |
| 202 | + |
| 203 | + /// Whether the dependencies are for a Swift module. |
| 204 | + bool isSwiftModule() const; |
| 205 | + |
| 206 | + ModuleDependenciesKind getKind() const { |
| 207 | + return isSwiftModule() ? ModuleDependenciesKind::Swift |
| 208 | + : ModuleDependenciesKind::Clang; |
| 209 | + } |
| 210 | + /// Retrieve the dependencies for a Swift module. |
| 211 | + const SwiftModuleDependenciesStorage *getAsSwiftModule() const; |
| 212 | + |
| 213 | + /// Retrieve the dependencies for a Clang module. |
| 214 | + const ClangModuleDependenciesStorage *getAsClangModule() const; |
| 215 | + |
| 216 | + /// Add a dependency on the given module, if it was not already in the set. |
| 217 | + void addModuleDependency(StringRef module, |
| 218 | + llvm::StringSet<> &alreadyAddedModules); |
| 219 | + |
| 220 | + /// Add all of the module dependencies for the imports in the given source |
| 221 | + /// file to the set of module dependencies. |
| 222 | + void addModuleDependencies(const SourceFile &sf, |
| 223 | + llvm::StringSet<> &alreadyAddedModules); |
| 224 | + |
| 225 | + /// Get the bridging header. |
| 226 | + Optional<std::string> getBridgingHeader() const; |
| 227 | + |
| 228 | + /// Add a bridging header to a Swift module's dependencies. |
| 229 | + void addBridgingHeader(StringRef bridgingHeader); |
| 230 | + |
| 231 | + /// Add source files that the bridging header depends on. |
| 232 | + void addBridgingSourceFile(StringRef bridgingSourceFile); |
| 233 | + |
| 234 | + /// Add (Clang) module on which the bridging header depends. |
| 235 | + void addBridgingModuleDependency(StringRef module, |
| 236 | + llvm::StringSet<> &alreadyAddedModules); |
| 237 | +}; |
| 238 | + |
| 239 | +using ModuleDependencyID = std::pair<std::string, ModuleDependenciesKind>; |
| 240 | + |
| 241 | +/// A cache describing the set of module dependencies that has been queried |
| 242 | +/// thus far. |
| 243 | +class ModuleDependenciesCache { |
| 244 | + /// All cached module dependencies, in the order in which they were |
| 245 | + /// encountered. |
| 246 | + std::vector<ModuleDependencyID> AllModules; |
| 247 | + |
| 248 | + /// Dependencies for Swift modules that have already been computed. |
| 249 | + llvm::StringMap<ModuleDependencies> SwiftModuleDependencies; |
| 250 | + |
| 251 | + /// Dependencies for Clang modules that have already been computed. |
| 252 | + llvm::StringMap<ModuleDependencies> ClangModuleDependencies; |
| 253 | + |
| 254 | + /// Additional information needed for Clang dependency scanning. |
| 255 | + ClangModuleDependenciesCacheImpl *clangImpl = nullptr; |
| 256 | + |
| 257 | + /// Function that will delete \c clangImpl properly. |
| 258 | + void (*clangImplDeleter)(ClangModuleDependenciesCacheImpl *) = nullptr; |
| 259 | + |
| 260 | + /// Free up the storage associated with the Clang implementation. |
| 261 | + void destroyClangImpl() { |
| 262 | + if (this->clangImplDeleter) |
| 263 | + this->clangImplDeleter(this->clangImpl); |
| 264 | + } |
| 265 | + |
| 266 | + /// Retrieve the dependencies map that corresponds to the given dependency |
| 267 | + /// kind. |
| 268 | + llvm::StringMap<ModuleDependencies> &getDependenciesMap( |
| 269 | + ModuleDependenciesKind kind); |
| 270 | + const llvm::StringMap<ModuleDependencies> &getDependenciesMap( |
| 271 | + ModuleDependenciesKind kind) const; |
| 272 | + |
| 273 | +public: |
| 274 | + ModuleDependenciesCache() { } |
| 275 | + |
| 276 | + ModuleDependenciesCache(const ModuleDependenciesCache &) = delete; |
| 277 | + ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete; |
| 278 | + |
| 279 | + ~ModuleDependenciesCache() { |
| 280 | + destroyClangImpl(); |
| 281 | + } |
| 282 | + |
| 283 | + /// Set the Clang-specific implementation data. |
| 284 | + void setClangImpl( |
| 285 | + ClangModuleDependenciesCacheImpl *clangImpl, |
| 286 | + void (*clangImplDeleter)(ClangModuleDependenciesCacheImpl *)) { |
| 287 | + destroyClangImpl(); |
| 288 | + |
| 289 | + this->clangImpl = clangImpl; |
| 290 | + this->clangImplDeleter = clangImplDeleter; |
| 291 | + } |
| 292 | + |
| 293 | + /// Retrieve the Clang-specific implementation data; |
| 294 | + ClangModuleDependenciesCacheImpl *getClangImpl() const { |
| 295 | + return clangImpl; |
| 296 | + } |
| 297 | + |
| 298 | + /// Whether we have cached dependency information for the given module. |
| 299 | + bool hasDependencies(StringRef moduleName, |
| 300 | + Optional<ModuleDependenciesKind> kind) const; |
| 301 | + |
| 302 | + /// Look for module dependencies for a module with the given name. |
| 303 | + /// |
| 304 | + /// \returns the cached result, or \c None if there is no cached entry. |
| 305 | + Optional<ModuleDependencies> findDependencies( |
| 306 | + StringRef moduleName, |
| 307 | + Optional<ModuleDependenciesKind> kind) const; |
| 308 | + |
| 309 | + /// Record dependencies for the given module. |
| 310 | + void recordDependencies(StringRef moduleName, |
| 311 | + ModuleDependencies dependencies, |
| 312 | + ModuleDependenciesKind kind); |
| 313 | + |
| 314 | + /// Update stored dependencies for the given module. |
| 315 | + void updateDependencies(ModuleDependencyID moduleID, |
| 316 | + ModuleDependencies dependencies); |
| 317 | + |
| 318 | + /// Reference the list of all module dependencies. |
| 319 | + const std::vector<ModuleDependencyID> &getAllModules() const { |
| 320 | + return AllModules; |
| 321 | + } |
| 322 | +}; |
| 323 | + |
| 324 | +} |
| 325 | + |
| 326 | +#endif /* SWIFT_AST_MODULE_DEPENDENCIES_H */ |
0 commit comments