Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- Module.h - Describe a module -----------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | /// \file |
| 11 | /// \brief Defines the clang::Module class, which describes a module in the |
| 12 | /// source code. |
| 13 | // |
| 14 | //===----------------------------------------------------------------------===// |
| 15 | |
| 16 | #ifndef LLVM_CLANG_BASIC_MODULE_H |
| 17 | #define LLVM_CLANG_BASIC_MODULE_H |
| 18 | |
| 19 | #include "clang/Basic/FileManager.h" |
| 20 | #include "clang/Basic/SourceLocation.h" |
| 21 | #include "llvm/ADT/ArrayRef.h" |
| 22 | #include "llvm/ADT/DenseSet.h" |
| 23 | #include "llvm/ADT/Optional.h" |
| 24 | #include "llvm/ADT/PointerIntPair.h" |
| 25 | #include "llvm/ADT/PointerUnion.h" |
| 26 | #include "llvm/ADT/SetVector.h" |
| 27 | #include "llvm/ADT/SmallVector.h" |
| 28 | #include "llvm/ADT/STLExtras.h" |
| 29 | #include "llvm/ADT/StringMap.h" |
| 30 | #include "llvm/ADT/StringRef.h" |
| 31 | #include "llvm/ADT/iterator_range.h" |
| 32 | #include <array> |
| 33 | #include <cassert> |
| 34 | #include <cstdint> |
| 35 | #include <ctime> |
| 36 | #include <string> |
| 37 | #include <utility> |
| 38 | #include <vector> |
| 39 | |
| 40 | namespace llvm { |
| 41 | |
| 42 | class raw_ostream; |
| 43 | |
| 44 | } // namespace llvm |
| 45 | |
| 46 | namespace clang { |
| 47 | |
| 48 | class LangOptions; |
| 49 | class TargetInfo; |
| 50 | |
| 51 | /// \brief Describes the name of a module. |
| 52 | using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>; |
| 53 | |
| 54 | /// The signature of a module, which is a hash of the AST content. |
| 55 | struct ASTFileSignature : std::array<uint32_t, 5> { |
| 56 | ASTFileSignature(std::array<uint32_t, 5> S = {{0}}) |
| 57 | : std::array<uint32_t, 5>(std::move(S)) {} |
| 58 | |
| 59 | explicit operator bool() const { |
| 60 | return *this != std::array<uint32_t, 5>({{0}}); |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | /// \brief Describes a module or submodule. |
| 65 | class Module { |
| 66 | public: |
| 67 | /// \brief The name of this module. |
| 68 | std::string Name; |
| 69 | |
| 70 | /// \brief The location of the module definition. |
| 71 | SourceLocation DefinitionLoc; |
| 72 | |
| 73 | enum ModuleKind { |
| 74 | /// \brief This is a module that was defined by a module map and built out |
| 75 | /// of header files. |
| 76 | ModuleMapModule, |
| 77 | |
| 78 | /// \brief This is a C++ Modules TS module interface unit. |
| 79 | ModuleInterfaceUnit, |
| 80 | |
| 81 | /// \brief This is a fragment of the global module within some C++ Modules |
| 82 | /// TS module. |
| 83 | GlobalModuleFragment, |
| 84 | }; |
| 85 | |
| 86 | /// \brief The kind of this module. |
| 87 | ModuleKind Kind = ModuleMapModule; |
| 88 | |
| 89 | /// \brief The parent of this module. This will be NULL for the top-level |
| 90 | /// module. |
| 91 | Module *Parent; |
| 92 | |
| 93 | /// \brief The build directory of this module. This is the directory in |
| 94 | /// which the module is notionally built, and relative to which its headers |
| 95 | /// are found. |
| 96 | const DirectoryEntry *Directory = nullptr; |
| 97 | |
| 98 | /// \brief The presumed file name for the module map defining this module. |
| 99 | /// Only non-empty when building from preprocessed source. |
| 100 | std::string PresumedModuleMapFile; |
| 101 | |
| 102 | /// \brief The umbrella header or directory. |
| 103 | llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella; |
| 104 | |
| 105 | /// \brief The module signature. |
| 106 | ASTFileSignature Signature; |
| 107 | |
| 108 | /// \brief The name of the umbrella entry, as written in the module map. |
| 109 | std::string UmbrellaAsWritten; |
| 110 | |
| 111 | /// \brief The module through which entities defined in this module will |
| 112 | /// eventually be exposed, for use in "private" modules. |
| 113 | std::string ExportAsModule; |
| 114 | |
| 115 | private: |
| 116 | /// \brief The submodules of this module, indexed by name. |
| 117 | std::vector<Module *> SubModules; |
| 118 | |
| 119 | /// \brief A mapping from the submodule name to the index into the |
| 120 | /// \c SubModules vector at which that submodule resides. |
| 121 | llvm::StringMap<unsigned> SubModuleIndex; |
| 122 | |
| 123 | /// \brief The AST file if this is a top-level module which has a |
| 124 | /// corresponding serialized AST file, or null otherwise. |
| 125 | const FileEntry *ASTFile = nullptr; |
| 126 | |
| 127 | /// \brief The top-level headers associated with this module. |
| 128 | llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; |
| 129 | |
| 130 | /// \brief top-level header filenames that aren't resolved to FileEntries yet. |
| 131 | std::vector<std::string> TopHeaderNames; |
| 132 | |
| 133 | /// \brief Cache of modules visible to lookup in this module. |
| 134 | mutable llvm::DenseSet<const Module*> VisibleModulesCache; |
| 135 | |
| 136 | /// The ID used when referencing this module within a VisibleModuleSet. |
| 137 | unsigned VisibilityID; |
| 138 | |
| 139 | public: |
| 140 | enum HeaderKind { |
| 141 | HK_Normal, |
| 142 | HK_Textual, |
| 143 | HK_Private, |
| 144 | HK_PrivateTextual, |
| 145 | HK_Excluded |
| 146 | }; |
| 147 | static const int NumHeaderKinds = HK_Excluded + 1; |
| 148 | |
| 149 | /// \brief Information about a header directive as found in the module map |
| 150 | /// file. |
| 151 | struct Header { |
| 152 | std::string NameAsWritten; |
| 153 | const FileEntry *Entry; |
| 154 | |
| 155 | explicit operator bool() { return Entry; } |
| 156 | }; |
| 157 | |
| 158 | /// \brief Information about a directory name as found in the module map |
| 159 | /// file. |
| 160 | struct DirectoryName { |
| 161 | std::string NameAsWritten; |
| 162 | const DirectoryEntry *Entry; |
| 163 | |
| 164 | explicit operator bool() { return Entry; } |
| 165 | }; |
| 166 | |
| 167 | /// \brief The headers that are part of this module. |
| 168 | SmallVector<Header, 2> Headers[5]; |
| 169 | |
| 170 | /// \brief Stored information about a header directive that was found in the |
| 171 | /// module map file but has not been resolved to a file. |
| 172 | struct UnresolvedHeaderDirective { |
| 173 | HeaderKind Kind = HK_Normal; |
| 174 | SourceLocation FileNameLoc; |
| 175 | std::string FileName; |
| 176 | bool IsUmbrella = false; |
| 177 | bool HasBuiltinHeader = false; |
| 178 | Optional<off_t> Size; |
| 179 | Optional<time_t> ModTime; |
| 180 | }; |
| 181 | |
| 182 | /// Headers that are mentioned in the module map file but that we have not |
| 183 | /// yet attempted to resolve to a file on the file system. |
| 184 | SmallVector<UnresolvedHeaderDirective, 1> UnresolvedHeaders; |
| 185 | |
| 186 | /// \brief Headers that are mentioned in the module map file but could not be |
| 187 | /// found on the file system. |
| 188 | SmallVector<UnresolvedHeaderDirective, 1> MissingHeaders; |
| 189 | |
| 190 | /// \brief An individual requirement: a feature name and a flag indicating |
| 191 | /// the required state of that feature. |
| 192 | using Requirement = std::pair<std::string, bool>; |
| 193 | |
| 194 | /// \brief The set of language features required to use this module. |
| 195 | /// |
| 196 | /// If any of these requirements are not available, the \c IsAvailable bit |
| 197 | /// will be false to indicate that this (sub)module is not available. |
| 198 | SmallVector<Requirement, 2> Requirements; |
| 199 | |
| 200 | /// \brief A module with the same name that shadows this module. |
| 201 | Module *ShadowingModule = nullptr; |
| 202 | |
| 203 | /// \brief Whether this module is missing a feature from \c Requirements. |
| 204 | unsigned IsMissingRequirement : 1; |
| 205 | |
| 206 | /// \brief Whether we tried and failed to load a module file for this module. |
| 207 | unsigned HasIncompatibleModuleFile : 1; |
| 208 | |
| 209 | /// \brief Whether this module is available in the current translation unit. |
| 210 | /// |
| 211 | /// If the module is missing headers or does not meet all requirements then |
| 212 | /// this bit will be 0. |
| 213 | unsigned IsAvailable : 1; |
| 214 | |
| 215 | /// \brief Whether this module was loaded from a module file. |
| 216 | unsigned IsFromModuleFile : 1; |
| 217 | |
| 218 | /// \brief Whether this is a framework module. |
| 219 | unsigned IsFramework : 1; |
| 220 | |
| 221 | /// \brief Whether this is an explicit submodule. |
| 222 | unsigned IsExplicit : 1; |
| 223 | |
| 224 | /// \brief Whether this is a "system" module (which assumes that all |
| 225 | /// headers in it are system headers). |
| 226 | unsigned IsSystem : 1; |
| 227 | |
| 228 | /// \brief Whether this is an 'extern "C"' module (which implicitly puts all |
| 229 | /// headers in it within an 'extern "C"' block, and allows the module to be |
| 230 | /// imported within such a block). |
| 231 | unsigned IsExternC : 1; |
| 232 | |
| 233 | /// \brief Whether this is an inferred submodule (module * { ... }). |
| 234 | unsigned IsInferred : 1; |
| 235 | |
| 236 | /// \brief Whether we should infer submodules for this module based on |
| 237 | /// the headers. |
| 238 | /// |
| 239 | /// Submodules can only be inferred for modules with an umbrella header. |
| 240 | unsigned InferSubmodules : 1; |
| 241 | |
| 242 | /// \brief Whether, when inferring submodules, the inferred submodules |
| 243 | /// should be explicit. |
| 244 | unsigned InferExplicitSubmodules : 1; |
| 245 | |
| 246 | /// \brief Whether, when inferring submodules, the inferr submodules should |
| 247 | /// export all modules they import (e.g., the equivalent of "export *"). |
| 248 | unsigned InferExportWildcard : 1; |
| 249 | |
| 250 | /// \brief Whether the set of configuration macros is exhaustive. |
| 251 | /// |
| 252 | /// When the set of configuration macros is exhaustive, meaning |
| 253 | /// that no identifier not in this list should affect how the module is |
| 254 | /// built. |
| 255 | unsigned ConfigMacrosExhaustive : 1; |
| 256 | |
| 257 | /// \brief Whether files in this module can only include non-modular headers |
| 258 | /// and headers from used modules. |
| 259 | unsigned NoUndeclaredIncludes : 1; |
| 260 | |
| 261 | /// \brief Describes the visibility of the various names within a |
| 262 | /// particular module. |
| 263 | enum NameVisibilityKind { |
| 264 | /// \brief All of the names in this module are hidden. |
| 265 | Hidden, |
| 266 | /// \brief All of the names in this module are visible. |
| 267 | AllVisible |
| 268 | }; |
| 269 | |
| 270 | /// \brief The visibility of names within this particular module. |
| 271 | NameVisibilityKind NameVisibility; |
| 272 | |
| 273 | /// \brief The location of the inferred submodule. |
| 274 | SourceLocation InferredSubmoduleLoc; |
| 275 | |
| 276 | /// \brief The set of modules imported by this module, and on which this |
| 277 | /// module depends. |
| 278 | llvm::SmallSetVector<Module *, 2> Imports; |
| 279 | |
| 280 | /// \brief Describes an exported module. |
| 281 | /// |
| 282 | /// The pointer is the module being re-exported, while the bit will be true |
| 283 | /// to indicate that this is a wildcard export. |
| 284 | using ExportDecl = llvm::PointerIntPair<Module *, 1, bool>; |
| 285 | |
| 286 | /// \brief The set of export declarations. |
| 287 | SmallVector<ExportDecl, 2> Exports; |
| 288 | |
| 289 | /// \brief Describes an exported module that has not yet been resolved |
| 290 | /// (perhaps because the module it refers to has not yet been loaded). |
| 291 | struct UnresolvedExportDecl { |
| 292 | /// \brief The location of the 'export' keyword in the module map file. |
| 293 | SourceLocation ExportLoc; |
| 294 | |
| 295 | /// \brief The name of the module. |
| 296 | ModuleId Id; |
| 297 | |
| 298 | /// \brief Whether this export declaration ends in a wildcard, indicating |
| 299 | /// that all of its submodules should be exported (rather than the named |
| 300 | /// module itself). |
| 301 | bool Wildcard; |
| 302 | }; |
| 303 | |
| 304 | /// \brief The set of export declarations that have yet to be resolved. |
| 305 | SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; |
| 306 | |
| 307 | /// \brief The directly used modules. |
| 308 | SmallVector<Module *, 2> DirectUses; |
| 309 | |
| 310 | /// \brief The set of use declarations that have yet to be resolved. |
| 311 | SmallVector<ModuleId, 2> UnresolvedDirectUses; |
| 312 | |
| 313 | /// \brief A library or framework to link against when an entity from this |
| 314 | /// module is used. |
| 315 | struct LinkLibrary { |
| 316 | LinkLibrary() = default; |
| 317 | LinkLibrary(const std::string &Library, bool IsFramework) |
| 318 | : Library(Library), IsFramework(IsFramework) {} |
| 319 | |
| 320 | /// \brief The library to link against. |
| 321 | /// |
| 322 | /// This will typically be a library or framework name, but can also |
| 323 | /// be an absolute path to the library or framework. |
| 324 | std::string Library; |
| 325 | |
| 326 | /// \brief Whether this is a framework rather than a library. |
| 327 | bool IsFramework = false; |
| 328 | }; |
| 329 | |
| 330 | /// \brief The set of libraries or frameworks to link against when |
| 331 | /// an entity from this module is used. |
| 332 | llvm::SmallVector<LinkLibrary, 2> LinkLibraries; |
| 333 | |
| 334 | /// \brief The set of "configuration macros", which are macros that |
| 335 | /// (intentionally) change how this module is built. |
| 336 | std::vector<std::string> ConfigMacros; |
| 337 | |
| 338 | /// \brief An unresolved conflict with another module. |
| 339 | struct UnresolvedConflict { |
| 340 | /// \brief The (unresolved) module id. |
| 341 | ModuleId Id; |
| 342 | |
| 343 | /// \brief The message provided to the user when there is a conflict. |
| 344 | std::string Message; |
| 345 | }; |
| 346 | |
| 347 | /// \brief The list of conflicts for which the module-id has not yet been |
| 348 | /// resolved. |
| 349 | std::vector<UnresolvedConflict> UnresolvedConflicts; |
| 350 | |
| 351 | /// \brief A conflict between two modules. |
| 352 | struct Conflict { |
| 353 | /// \brief The module that this module conflicts with. |
| 354 | Module *Other; |
| 355 | |
| 356 | /// \brief The message provided to the user when there is a conflict. |
| 357 | std::string Message; |
| 358 | }; |
| 359 | |
| 360 | /// \brief The list of conflicts. |
| 361 | std::vector<Conflict> Conflicts; |
| 362 | |
| 363 | /// \brief Construct a new module or submodule. |
| 364 | Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, |
| 365 | bool IsFramework, bool IsExplicit, unsigned VisibilityID); |
| 366 | |
| 367 | ~Module(); |
| 368 | |
| 369 | /// \brief Determine whether this module is available for use within the |
| 370 | /// current translation unit. |
| 371 | bool isAvailable() const { return IsAvailable; } |
| 372 | |
| 373 | /// \brief Determine whether this module is available for use within the |
| 374 | /// current translation unit. |
| 375 | /// |
| 376 | /// \param LangOpts The language options used for the current |
| 377 | /// translation unit. |
| 378 | /// |
| 379 | /// \param Target The target options used for the current translation unit. |
| 380 | /// |
| 381 | /// \param Req If this module is unavailable because of a missing requirement, |
| 382 | /// this parameter will be set to one of the requirements that is not met for |
| 383 | /// use of this module. |
| 384 | /// |
| 385 | /// \param MissingHeader If this module is unavailable because of a missing |
| 386 | /// header, this parameter will be set to one of the missing headers. |
| 387 | /// |
| 388 | /// \param ShadowingModule If this module is unavailable because it is |
| 389 | /// shadowed, this parameter will be set to the shadowing module. |
| 390 | bool isAvailable(const LangOptions &LangOpts, |
| 391 | const TargetInfo &Target, |
| 392 | Requirement &Req, |
| 393 | UnresolvedHeaderDirective &MissingHeader, |
| 394 | Module *&ShadowingModule) const; |
| 395 | |
| 396 | /// \brief Determine whether this module is a submodule. |
| 397 | bool isSubModule() const { return Parent != nullptr; } |
| 398 | |
| 399 | /// \brief Determine whether this module is a submodule of the given other |
| 400 | /// module. |
| 401 | bool isSubModuleOf(const Module *Other) const; |
| 402 | |
| 403 | /// \brief Determine whether this module is a part of a framework, |
| 404 | /// either because it is a framework module or because it is a submodule |
| 405 | /// of a framework module. |
| 406 | bool isPartOfFramework() const { |
| 407 | for (const Module *Mod = this; Mod; Mod = Mod->Parent) |
| 408 | if (Mod->IsFramework) |
| 409 | return true; |
| 410 | |
| 411 | return false; |
| 412 | } |
| 413 | |
| 414 | /// \brief Determine whether this module is a subframework of another |
| 415 | /// framework. |
| 416 | bool isSubFramework() const { |
| 417 | return IsFramework && Parent && Parent->isPartOfFramework(); |
| 418 | } |
| 419 | |
| 420 | /// Set the parent of this module. This should only be used if the parent |
| 421 | /// could not be set during module creation. |
| 422 | void setParent(Module *M) { |
| 423 | assert(!Parent); |
| 424 | Parent = M; |
| 425 | Parent->SubModuleIndex[Name] = Parent->SubModules.size(); |
| 426 | Parent->SubModules.push_back(this); |
| 427 | } |
| 428 | |
| 429 | /// \brief Retrieve the full name of this module, including the path from |
| 430 | /// its top-level module. |
| 431 | /// \param AllowStringLiterals If \c true, components that might not be |
| 432 | /// lexically valid as identifiers will be emitted as string literals. |
| 433 | std::string getFullModuleName(bool AllowStringLiterals = false) const; |
| 434 | |
| 435 | /// \brief Whether the full name of this module is equal to joining |
| 436 | /// \p nameParts with "."s. |
| 437 | /// |
| 438 | /// This is more efficient than getFullModuleName(). |
| 439 | bool fullModuleNameIs(ArrayRef<StringRef> nameParts) const; |
| 440 | |
| 441 | /// \brief Retrieve the top-level module for this (sub)module, which may |
| 442 | /// be this module. |
| 443 | Module *getTopLevelModule() { |
| 444 | return const_cast<Module *>( |
| 445 | const_cast<const Module *>(this)->getTopLevelModule()); |
| 446 | } |
| 447 | |
| 448 | /// \brief Retrieve the top-level module for this (sub)module, which may |
| 449 | /// be this module. |
| 450 | const Module *getTopLevelModule() const; |
| 451 | |
| 452 | /// \brief Retrieve the name of the top-level module. |
| 453 | StringRef getTopLevelModuleName() const { |
| 454 | return getTopLevelModule()->Name; |
| 455 | } |
| 456 | |
| 457 | /// \brief The serialized AST file for this module, if one was created. |
| 458 | const FileEntry *getASTFile() const { |
| 459 | return getTopLevelModule()->ASTFile; |
| 460 | } |
| 461 | |
| 462 | /// \brief Set the serialized AST file for the top-level module of this module. |
| 463 | void setASTFile(const FileEntry *File) { |
| 464 | assert((File == nullptr || getASTFile() == nullptr || |
| 465 | getASTFile() == File) && "file path changed"); |
| 466 | getTopLevelModule()->ASTFile = File; |
| 467 | } |
| 468 | |
| 469 | /// \brief Retrieve the directory for which this module serves as the |
| 470 | /// umbrella. |
| 471 | DirectoryName getUmbrellaDir() const; |
| 472 | |
| 473 | /// \brief Retrieve the header that serves as the umbrella header for this |
| 474 | /// module. |
| 475 | Header getUmbrellaHeader() const { |
| 476 | if (auto *E = Umbrella.dyn_cast<const FileEntry *>()) |
| 477 | return Header{UmbrellaAsWritten, E}; |
| 478 | return Header{}; |
| 479 | } |
| 480 | |
| 481 | /// \brief Determine whether this module has an umbrella directory that is |
| 482 | /// not based on an umbrella header. |
| 483 | bool hasUmbrellaDir() const { |
| 484 | return Umbrella && Umbrella.is<const DirectoryEntry *>(); |
| 485 | } |
| 486 | |
| 487 | /// \brief Add a top-level header associated with this module. |
| 488 | void addTopHeader(const FileEntry *File) { |
| 489 | assert(File); |
| 490 | TopHeaders.insert(File); |
| 491 | } |
| 492 | |
| 493 | /// \brief Add a top-level header filename associated with this module. |
| 494 | void addTopHeaderFilename(StringRef Filename) { |
| 495 | TopHeaderNames.push_back(Filename); |
| 496 | } |
| 497 | |
| 498 | /// \brief The top-level headers associated with this module. |
| 499 | ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); |
| 500 | |
| 501 | /// \brief Determine whether this module has declared its intention to |
| 502 | /// directly use another module. |
| 503 | bool directlyUses(const Module *Requested) const; |
| 504 | |
| 505 | /// \brief Add the given feature requirement to the list of features |
| 506 | /// required by this module. |
| 507 | /// |
| 508 | /// \param Feature The feature that is required by this module (and |
| 509 | /// its submodules). |
| 510 | /// |
| 511 | /// \param RequiredState The required state of this feature: \c true |
| 512 | /// if it must be present, \c false if it must be absent. |
| 513 | /// |
| 514 | /// \param LangOpts The set of language options that will be used to |
| 515 | /// evaluate the availability of this feature. |
| 516 | /// |
| 517 | /// \param Target The target options that will be used to evaluate the |
| 518 | /// availability of this feature. |
| 519 | void addRequirement(StringRef Feature, bool RequiredState, |
| 520 | const LangOptions &LangOpts, |
| 521 | const TargetInfo &Target); |
| 522 | |
| 523 | /// \brief Mark this module and all of its submodules as unavailable. |
| 524 | void markUnavailable(bool MissingRequirement = false); |
| 525 | |
| 526 | /// \brief Find the submodule with the given name. |
| 527 | /// |
| 528 | /// \returns The submodule if found, or NULL otherwise. |
| 529 | Module *findSubmodule(StringRef Name) const; |
| 530 | |
| 531 | /// \brief Determine whether the specified module would be visible to |
| 532 | /// a lookup at the end of this module. |
| 533 | /// |
| 534 | /// FIXME: This may return incorrect results for (submodules of) the |
| 535 | /// module currently being built, if it's queried before we see all |
| 536 | /// of its imports. |
| 537 | bool isModuleVisible(const Module *M) const { |
| 538 | if (VisibleModulesCache.empty()) |
| 539 | buildVisibleModulesCache(); |
| 540 | return VisibleModulesCache.count(M); |
| 541 | } |
| 542 | |
| 543 | unsigned getVisibilityID() const { return VisibilityID; } |
| 544 | |
| 545 | using submodule_iterator = std::vector<Module *>::iterator; |
| 546 | using submodule_const_iterator = std::vector<Module *>::const_iterator; |
| 547 | |
| 548 | submodule_iterator submodule_begin() { return SubModules.begin(); } |
| 549 | submodule_const_iterator submodule_begin() const {return SubModules.begin();} |
| 550 | submodule_iterator submodule_end() { return SubModules.end(); } |
| 551 | submodule_const_iterator submodule_end() const { return SubModules.end(); } |
| 552 | |
| 553 | llvm::iterator_range<submodule_iterator> submodules() { |
| 554 | return llvm::make_range(submodule_begin(), submodule_end()); |
| 555 | } |
| 556 | llvm::iterator_range<submodule_const_iterator> submodules() const { |
| 557 | return llvm::make_range(submodule_begin(), submodule_end()); |
| 558 | } |
| 559 | |
| 560 | /// \brief Appends this module's list of exported modules to \p Exported. |
| 561 | /// |
| 562 | /// This provides a subset of immediately imported modules (the ones that are |
| 563 | /// directly exported), not the complete set of exported modules. |
| 564 | void getExportedModules(SmallVectorImpl<Module *> &Exported) const; |
| 565 | |
| 566 | static StringRef getModuleInputBufferName() { |
| 567 | return "<module-includes>"; |
| 568 | } |
| 569 | |
| 570 | /// \brief Print the module map for this module to the given stream. |
| 571 | void print(raw_ostream &OS, unsigned Indent = 0) const; |
| 572 | |
| 573 | /// \brief Dump the contents of this module to the given output stream. |
| 574 | void dump() const; |
| 575 | |
| 576 | private: |
| 577 | void buildVisibleModulesCache() const; |
| 578 | }; |
| 579 | |
| 580 | /// \brief A set of visible modules. |
| 581 | class VisibleModuleSet { |
| 582 | public: |
| 583 | VisibleModuleSet() = default; |
| 584 | VisibleModuleSet(VisibleModuleSet &&O) |
| 585 | : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) { |
| 586 | O.ImportLocs.clear(); |
| 587 | ++O.Generation; |
| 588 | } |
| 589 | |
| 590 | /// Move from another visible modules set. Guaranteed to leave the source |
| 591 | /// empty and bump the generation on both. |
| 592 | VisibleModuleSet &operator=(VisibleModuleSet &&O) { |
| 593 | ImportLocs = std::move(O.ImportLocs); |
| 594 | O.ImportLocs.clear(); |
| 595 | ++O.Generation; |
| 596 | ++Generation; |
| 597 | return *this; |
| 598 | } |
| 599 | |
| 600 | /// \brief Get the current visibility generation. Incremented each time the |
| 601 | /// set of visible modules changes in any way. |
| 602 | unsigned getGeneration() const { return Generation; } |
| 603 | |
| 604 | /// \brief Determine whether a module is visible. |
| 605 | bool isVisible(const Module *M) const { |
| 606 | return getImportLoc(M).isValid(); |
| 607 | } |
| 608 | |
| 609 | /// \brief Get the location at which the import of a module was triggered. |
| 610 | SourceLocation getImportLoc(const Module *M) const { |
| 611 | return M->getVisibilityID() < ImportLocs.size() |
| 612 | ? ImportLocs[M->getVisibilityID()] |
| 613 | : SourceLocation(); |
| 614 | } |
| 615 | |
| 616 | /// \brief A callback to call when a module is made visible (directly or |
| 617 | /// indirectly) by a call to \ref setVisible. |
| 618 | using VisibleCallback = llvm::function_ref<void(Module *M)>; |
| 619 | |
| 620 | /// \brief A callback to call when a module conflict is found. \p Path |
| 621 | /// consists of a sequence of modules from the conflicting module to the one |
| 622 | /// made visible, where each was exported by the next. |
| 623 | using ConflictCallback = |
| 624 | llvm::function_ref<void(ArrayRef<Module *> Path, Module *Conflict, |
| 625 | StringRef Message)>; |
| 626 | |
| 627 | /// \brief Make a specific module visible. |
| 628 | void setVisible(Module *M, SourceLocation Loc, |
| 629 | VisibleCallback Vis = [](Module *) {}, |
| 630 | ConflictCallback Cb = [](ArrayRef<Module *>, Module *, |
| 631 | StringRef) {}); |
| 632 | |
| 633 | private: |
| 634 | /// Import locations for each visible module. Indexed by the module's |
| 635 | /// VisibilityID. |
| 636 | std::vector<SourceLocation> ImportLocs; |
| 637 | |
| 638 | /// Visibility generation, bumped every time the visibility state changes. |
| 639 | unsigned Generation = 0; |
| 640 | }; |
| 641 | |
| 642 | } // namespace clang |
| 643 | |
| 644 | #endif // LLVM_CLANG_BASIC_MODULE_H |