1 //===- Module.h - Describe a module -----------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// Defines the clang::Module class, which describes a module in the 11 /// source code. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_BASIC_MODULE_H 16 #define LLVM_CLANG_BASIC_MODULE_H 17 18 #include "clang/Basic/DirectoryEntry.h" 19 #include "clang/Basic/FileEntry.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/STLExtras.h" 26 #include "llvm/ADT/SetVector.h" 27 #include "llvm/ADT/SmallVector.h" 28 #include "llvm/ADT/StringMap.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/ADT/iterator_range.h" 31 #include <array> 32 #include <cassert> 33 #include <cstdint> 34 #include <ctime> 35 #include <iterator> 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 FileManager; 49 class LangOptions; 50 class TargetInfo; 51 52 /// Describes the name of a module. 53 using ModuleId = SmallVector<std::pair<std::string, SourceLocation>, 2>; 54 55 /// The signature of a module, which is a hash of the AST content. 56 struct ASTFileSignature : std::array<uint8_t, 20> { 57 using BaseT = std::array<uint8_t, 20>; 58 59 static constexpr size_t size = std::tuple_size<BaseT>::value; 60 61 ASTFileSignature(BaseT S = {{0}}) : BaseT(std::move(S)) {} 62 63 explicit operator bool() const { return *this != BaseT({{0}}); } 64 65 /// Returns the value truncated to the size of an uint64_t. 66 uint64_t truncatedValue() const { 67 uint64_t Value = 0; 68 static_assert(sizeof(*this) >= sizeof(uint64_t), "No need to truncate."); 69 for (unsigned I = 0; I < sizeof(uint64_t); ++I) 70 Value |= static_cast<uint64_t>((*this)[I]) << (I * 8); 71 return Value; 72 } 73 74 static ASTFileSignature create(std::array<uint8_t, 20> Bytes) { 75 return ASTFileSignature(std::move(Bytes)); 76 } 77 78 static ASTFileSignature createDISentinel() { 79 ASTFileSignature Sentinel; 80 Sentinel.fill(0xFF); 81 return Sentinel; 82 } 83 84 template <typename InputIt> 85 static ASTFileSignature create(InputIt First, InputIt Last) { 86 assert(std::distance(First, Last) == size && 87 "Wrong amount of bytes to create an ASTFileSignature"); 88 89 ASTFileSignature Signature; 90 std::copy(First, Last, Signature.begin()); 91 return Signature; 92 } 93 }; 94 95 /// Describes a module or submodule. 96 class Module { 97 public: 98 /// The name of this module. 99 std::string Name; 100 101 /// The location of the module definition. 102 SourceLocation DefinitionLoc; 103 104 enum ModuleKind { 105 /// This is a module that was defined by a module map and built out 106 /// of header files. 107 ModuleMapModule, 108 109 /// This is a C++20 module interface unit. 110 ModuleInterfaceUnit, 111 112 /// This is a C++ 20 header unit. 113 ModuleHeaderUnit, 114 115 /// This is a C++ 20 module partition interface. 116 ModulePartitionInterface, 117 118 /// This is a C++ 20 module partition implementation. 119 ModulePartitionImplementation, 120 121 /// This is a fragment of the global module within some C++ module. 122 GlobalModuleFragment, 123 124 /// This is the private module fragment within some C++ module. 125 PrivateModuleFragment, 126 }; 127 128 /// The kind of this module. 129 ModuleKind Kind = ModuleMapModule; 130 131 /// The parent of this module. This will be NULL for the top-level 132 /// module. 133 Module *Parent; 134 135 /// The build directory of this module. This is the directory in 136 /// which the module is notionally built, and relative to which its headers 137 /// are found. 138 const DirectoryEntry *Directory = nullptr; 139 140 /// The presumed file name for the module map defining this module. 141 /// Only non-empty when building from preprocessed source. 142 std::string PresumedModuleMapFile; 143 144 /// The umbrella header or directory. 145 llvm::PointerUnion<const FileEntry *, const DirectoryEntry *> Umbrella; 146 147 /// The module signature. 148 ASTFileSignature Signature; 149 150 /// The name of the umbrella entry, as written in the module map. 151 std::string UmbrellaAsWritten; 152 153 // The path to the umbrella entry relative to the root module's \c Directory. 154 std::string UmbrellaRelativeToRootModuleDirectory; 155 156 /// The module through which entities defined in this module will 157 /// eventually be exposed, for use in "private" modules. 158 std::string ExportAsModule; 159 160 /// Does this Module scope describe part of the purview of a named C++ module? 161 bool isModulePurview() const { 162 return Kind == ModuleInterfaceUnit || Kind == ModulePartitionInterface || 163 Kind == ModulePartitionImplementation || 164 Kind == PrivateModuleFragment; 165 } 166 167 /// Does this Module scope describe a fragment of the global module within 168 /// some C++ module. 169 bool isGlobalModule() const { return Kind == GlobalModuleFragment; } 170 171 bool isPrivateModule() const { return Kind == PrivateModuleFragment; } 172 173 private: 174 /// The submodules of this module, indexed by name. 175 std::vector<Module *> SubModules; 176 177 /// A mapping from the submodule name to the index into the 178 /// \c SubModules vector at which that submodule resides. 179 llvm::StringMap<unsigned> SubModuleIndex; 180 181 /// The AST file if this is a top-level module which has a 182 /// corresponding serialized AST file, or null otherwise. 183 Optional<FileEntryRef> ASTFile; 184 185 /// The top-level headers associated with this module. 186 llvm::SmallSetVector<const FileEntry *, 2> TopHeaders; 187 188 /// top-level header filenames that aren't resolved to FileEntries yet. 189 std::vector<std::string> TopHeaderNames; 190 191 /// Cache of modules visible to lookup in this module. 192 mutable llvm::DenseSet<const Module*> VisibleModulesCache; 193 194 /// The ID used when referencing this module within a VisibleModuleSet. 195 unsigned VisibilityID; 196 197 public: 198 enum HeaderKind { 199 HK_Normal, 200 HK_Textual, 201 HK_Private, 202 HK_PrivateTextual, 203 HK_Excluded 204 }; 205 static const int NumHeaderKinds = HK_Excluded + 1; 206 207 /// Information about a header directive as found in the module map 208 /// file. 209 struct Header { 210 std::string NameAsWritten; 211 std::string PathRelativeToRootModuleDirectory; 212 const FileEntry *Entry; 213 214 explicit operator bool() { return Entry; } 215 }; 216 217 /// Information about a directory name as found in the module map 218 /// file. 219 struct DirectoryName { 220 std::string NameAsWritten; 221 std::string PathRelativeToRootModuleDirectory; 222 const DirectoryEntry *Entry; 223 224 explicit operator bool() { return Entry; } 225 }; 226 227 /// The headers that are part of this module. 228 SmallVector<Header, 2> Headers[5]; 229 230 /// Stored information about a header directive that was found in the 231 /// module map file but has not been resolved to a file. 232 struct UnresolvedHeaderDirective { 233 HeaderKind Kind = HK_Normal; 234 SourceLocation FileNameLoc; 235 std::string FileName; 236 bool IsUmbrella = false; 237 bool HasBuiltinHeader = false; 238 Optional<off_t> Size; 239 Optional<time_t> ModTime; 240 }; 241 242 /// Headers that are mentioned in the module map file but that we have not 243 /// yet attempted to resolve to a file on the file system. 244 SmallVector<UnresolvedHeaderDirective, 1> UnresolvedHeaders; 245 246 /// Headers that are mentioned in the module map file but could not be 247 /// found on the file system. 248 SmallVector<UnresolvedHeaderDirective, 1> MissingHeaders; 249 250 /// An individual requirement: a feature name and a flag indicating 251 /// the required state of that feature. 252 using Requirement = std::pair<std::string, bool>; 253 254 /// The set of language features required to use this module. 255 /// 256 /// If any of these requirements are not available, the \c IsAvailable bit 257 /// will be false to indicate that this (sub)module is not available. 258 SmallVector<Requirement, 2> Requirements; 259 260 /// A module with the same name that shadows this module. 261 Module *ShadowingModule = nullptr; 262 263 /// Whether this module has declared itself unimportable, either because 264 /// it's missing a requirement from \p Requirements or because it's been 265 /// shadowed by another module. 266 unsigned IsUnimportable : 1; 267 268 /// Whether we tried and failed to load a module file for this module. 269 unsigned HasIncompatibleModuleFile : 1; 270 271 /// Whether this module is available in the current translation unit. 272 /// 273 /// If the module is missing headers or does not meet all requirements then 274 /// this bit will be 0. 275 unsigned IsAvailable : 1; 276 277 /// Whether this module was loaded from a module file. 278 unsigned IsFromModuleFile : 1; 279 280 /// Whether this is a framework module. 281 unsigned IsFramework : 1; 282 283 /// Whether this is an explicit submodule. 284 unsigned IsExplicit : 1; 285 286 /// Whether this is a "system" module (which assumes that all 287 /// headers in it are system headers). 288 unsigned IsSystem : 1; 289 290 /// Whether this is an 'extern "C"' module (which implicitly puts all 291 /// headers in it within an 'extern "C"' block, and allows the module to be 292 /// imported within such a block). 293 unsigned IsExternC : 1; 294 295 /// Whether this is an inferred submodule (module * { ... }). 296 unsigned IsInferred : 1; 297 298 /// Whether we should infer submodules for this module based on 299 /// the headers. 300 /// 301 /// Submodules can only be inferred for modules with an umbrella header. 302 unsigned InferSubmodules : 1; 303 304 /// Whether, when inferring submodules, the inferred submodules 305 /// should be explicit. 306 unsigned InferExplicitSubmodules : 1; 307 308 /// Whether, when inferring submodules, the inferr submodules should 309 /// export all modules they import (e.g., the equivalent of "export *"). 310 unsigned InferExportWildcard : 1; 311 312 /// Whether the set of configuration macros is exhaustive. 313 /// 314 /// When the set of configuration macros is exhaustive, meaning 315 /// that no identifier not in this list should affect how the module is 316 /// built. 317 unsigned ConfigMacrosExhaustive : 1; 318 319 /// Whether files in this module can only include non-modular headers 320 /// and headers from used modules. 321 unsigned NoUndeclaredIncludes : 1; 322 323 /// Whether this module came from a "private" module map, found next 324 /// to a regular (public) module map. 325 unsigned ModuleMapIsPrivate : 1; 326 327 /// Describes the visibility of the various names within a 328 /// particular module. 329 enum NameVisibilityKind { 330 /// All of the names in this module are hidden. 331 Hidden, 332 /// All of the names in this module are visible. 333 AllVisible 334 }; 335 336 /// The visibility of names within this particular module. 337 NameVisibilityKind NameVisibility; 338 339 /// The location of the inferred submodule. 340 SourceLocation InferredSubmoduleLoc; 341 342 /// The set of modules imported by this module, and on which this 343 /// module depends. 344 llvm::SmallSetVector<Module *, 2> Imports; 345 346 /// Describes an exported module. 347 /// 348 /// The pointer is the module being re-exported, while the bit will be true 349 /// to indicate that this is a wildcard export. 350 using ExportDecl = llvm::PointerIntPair<Module *, 1, bool>; 351 352 /// The set of export declarations. 353 SmallVector<ExportDecl, 2> Exports; 354 355 /// Describes an exported module that has not yet been resolved 356 /// (perhaps because the module it refers to has not yet been loaded). 357 struct UnresolvedExportDecl { 358 /// The location of the 'export' keyword in the module map file. 359 SourceLocation ExportLoc; 360 361 /// The name of the module. 362 ModuleId Id; 363 364 /// Whether this export declaration ends in a wildcard, indicating 365 /// that all of its submodules should be exported (rather than the named 366 /// module itself). 367 bool Wildcard; 368 }; 369 370 /// The set of export declarations that have yet to be resolved. 371 SmallVector<UnresolvedExportDecl, 2> UnresolvedExports; 372 373 /// The directly used modules. 374 SmallVector<Module *, 2> DirectUses; 375 376 /// The set of use declarations that have yet to be resolved. 377 SmallVector<ModuleId, 2> UnresolvedDirectUses; 378 379 /// When \c NoUndeclaredIncludes is true, the set of modules this module tried 380 /// to import but didn't because they are not direct uses. 381 llvm::SmallSetVector<const Module *, 2> UndeclaredUses; 382 383 /// A library or framework to link against when an entity from this 384 /// module is used. 385 struct LinkLibrary { 386 LinkLibrary() = default; 387 LinkLibrary(const std::string &Library, bool IsFramework) 388 : Library(Library), IsFramework(IsFramework) {} 389 390 /// The library to link against. 391 /// 392 /// This will typically be a library or framework name, but can also 393 /// be an absolute path to the library or framework. 394 std::string Library; 395 396 /// Whether this is a framework rather than a library. 397 bool IsFramework = false; 398 }; 399 400 /// The set of libraries or frameworks to link against when 401 /// an entity from this module is used. 402 llvm::SmallVector<LinkLibrary, 2> LinkLibraries; 403 404 /// Autolinking uses the framework name for linking purposes 405 /// when this is false and the export_as name otherwise. 406 bool UseExportAsModuleLinkName = false; 407 408 /// The set of "configuration macros", which are macros that 409 /// (intentionally) change how this module is built. 410 std::vector<std::string> ConfigMacros; 411 412 /// An unresolved conflict with another module. 413 struct UnresolvedConflict { 414 /// The (unresolved) module id. 415 ModuleId Id; 416 417 /// The message provided to the user when there is a conflict. 418 std::string Message; 419 }; 420 421 /// The list of conflicts for which the module-id has not yet been 422 /// resolved. 423 std::vector<UnresolvedConflict> UnresolvedConflicts; 424 425 /// A conflict between two modules. 426 struct Conflict { 427 /// The module that this module conflicts with. 428 Module *Other; 429 430 /// The message provided to the user when there is a conflict. 431 std::string Message; 432 }; 433 434 /// The list of conflicts. 435 std::vector<Conflict> Conflicts; 436 437 /// Construct a new module or submodule. 438 Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent, 439 bool IsFramework, bool IsExplicit, unsigned VisibilityID); 440 441 ~Module(); 442 443 /// Determine whether this module has been declared unimportable. 444 bool isUnimportable() const { return IsUnimportable; } 445 446 /// Determine whether this module has been declared unimportable. 447 /// 448 /// \param LangOpts The language options used for the current 449 /// translation unit. 450 /// 451 /// \param Target The target options used for the current translation unit. 452 /// 453 /// \param Req If this module is unimportable because of a missing 454 /// requirement, this parameter will be set to one of the requirements that 455 /// is not met for use of this module. 456 /// 457 /// \param ShadowingModule If this module is unimportable because it is 458 /// shadowed, this parameter will be set to the shadowing module. 459 bool isUnimportable(const LangOptions &LangOpts, const TargetInfo &Target, 460 Requirement &Req, Module *&ShadowingModule) const; 461 462 /// Determine whether this module is available for use within the 463 /// current translation unit. 464 bool isAvailable() const { return IsAvailable; } 465 466 /// Determine whether this module is available for use within the 467 /// current translation unit. 468 /// 469 /// \param LangOpts The language options used for the current 470 /// translation unit. 471 /// 472 /// \param Target The target options used for the current translation unit. 473 /// 474 /// \param Req If this module is unavailable because of a missing requirement, 475 /// this parameter will be set to one of the requirements that is not met for 476 /// use of this module. 477 /// 478 /// \param MissingHeader If this module is unavailable because of a missing 479 /// header, this parameter will be set to one of the missing headers. 480 /// 481 /// \param ShadowingModule If this module is unavailable because it is 482 /// shadowed, this parameter will be set to the shadowing module. 483 bool isAvailable(const LangOptions &LangOpts, 484 const TargetInfo &Target, 485 Requirement &Req, 486 UnresolvedHeaderDirective &MissingHeader, 487 Module *&ShadowingModule) const; 488 489 /// Determine whether this module is a submodule. 490 bool isSubModule() const { return Parent != nullptr; } 491 492 /// Check if this module is a (possibly transitive) submodule of \p Other. 493 /// 494 /// The 'A is a submodule of B' relation is a partial order based on the 495 /// the parent-child relationship between individual modules. 496 /// 497 /// Returns \c false if \p Other is \c nullptr. 498 bool isSubModuleOf(const Module *Other) const; 499 500 /// Determine whether this module is a part of a framework, 501 /// either because it is a framework module or because it is a submodule 502 /// of a framework module. 503 bool isPartOfFramework() const { 504 for (const Module *Mod = this; Mod; Mod = Mod->Parent) 505 if (Mod->IsFramework) 506 return true; 507 508 return false; 509 } 510 511 /// Determine whether this module is a subframework of another 512 /// framework. 513 bool isSubFramework() const { 514 return IsFramework && Parent && Parent->isPartOfFramework(); 515 } 516 517 /// Set the parent of this module. This should only be used if the parent 518 /// could not be set during module creation. 519 void setParent(Module *M) { 520 assert(!Parent); 521 Parent = M; 522 Parent->SubModuleIndex[Name] = Parent->SubModules.size(); 523 Parent->SubModules.push_back(this); 524 } 525 526 /// Is this a module partition. 527 bool isModulePartition() const { 528 return Kind == ModulePartitionInterface || 529 Kind == ModulePartitionImplementation; 530 } 531 532 /// Is this module a header unit. 533 bool isHeaderUnit() const { return Kind == ModuleHeaderUnit; } 534 // Is this a C++20 module interface or a partition. 535 bool isInterfaceOrPartition() const { 536 return Kind == ModuleInterfaceUnit || isModulePartition(); 537 } 538 539 /// Get the primary module interface name from a partition. 540 StringRef getPrimaryModuleInterfaceName() const { 541 // Technically, global module fragment belongs to global module. And global 542 // module has no name: [module.unit]p6: 543 // The global module has no name, no module interface unit, and is not 544 // introduced by any module-declaration. 545 // 546 // <global> is the default name showed in module map. 547 if (isGlobalModule()) 548 return "<global>"; 549 550 if (isModulePartition()) { 551 auto pos = Name.find(':'); 552 return StringRef(Name.data(), pos); 553 } 554 555 if (isPrivateModule()) 556 return getTopLevelModuleName(); 557 558 return Name; 559 } 560 561 /// Retrieve the full name of this module, including the path from 562 /// its top-level module. 563 /// \param AllowStringLiterals If \c true, components that might not be 564 /// lexically valid as identifiers will be emitted as string literals. 565 std::string getFullModuleName(bool AllowStringLiterals = false) const; 566 567 /// Whether the full name of this module is equal to joining 568 /// \p nameParts with "."s. 569 /// 570 /// This is more efficient than getFullModuleName(). 571 bool fullModuleNameIs(ArrayRef<StringRef> nameParts) const; 572 573 /// Retrieve the top-level module for this (sub)module, which may 574 /// be this module. 575 Module *getTopLevelModule() { 576 return const_cast<Module *>( 577 const_cast<const Module *>(this)->getTopLevelModule()); 578 } 579 580 /// Retrieve the top-level module for this (sub)module, which may 581 /// be this module. 582 const Module *getTopLevelModule() const; 583 584 /// Retrieve the name of the top-level module. 585 StringRef getTopLevelModuleName() const { 586 return getTopLevelModule()->Name; 587 } 588 589 /// The serialized AST file for this module, if one was created. 590 OptionalFileEntryRefDegradesToFileEntryPtr getASTFile() const { 591 return getTopLevelModule()->ASTFile; 592 } 593 594 /// Set the serialized AST file for the top-level module of this module. 595 void setASTFile(Optional<FileEntryRef> File) { 596 assert((!File || !getASTFile() || getASTFile() == File) && 597 "file path changed"); 598 getTopLevelModule()->ASTFile = File; 599 } 600 601 /// Retrieve the directory for which this module serves as the 602 /// umbrella. 603 DirectoryName getUmbrellaDir() const; 604 605 /// Retrieve the header that serves as the umbrella header for this 606 /// module. 607 Header getUmbrellaHeader() const { 608 if (auto *FE = Umbrella.dyn_cast<const FileEntry *>()) 609 return Header{UmbrellaAsWritten, UmbrellaRelativeToRootModuleDirectory, 610 FE}; 611 return Header{}; 612 } 613 614 /// Determine whether this module has an umbrella directory that is 615 /// not based on an umbrella header. 616 bool hasUmbrellaDir() const { 617 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 618 } 619 620 /// Add a top-level header associated with this module. 621 void addTopHeader(const FileEntry *File); 622 623 /// Add a top-level header filename associated with this module. 624 void addTopHeaderFilename(StringRef Filename) { 625 TopHeaderNames.push_back(std::string(Filename)); 626 } 627 628 /// The top-level headers associated with this module. 629 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); 630 631 /// Determine whether this module has declared its intention to 632 /// directly use another module. 633 bool directlyUses(const Module *Requested); 634 635 /// Add the given feature requirement to the list of features 636 /// required by this module. 637 /// 638 /// \param Feature The feature that is required by this module (and 639 /// its submodules). 640 /// 641 /// \param RequiredState The required state of this feature: \c true 642 /// if it must be present, \c false if it must be absent. 643 /// 644 /// \param LangOpts The set of language options that will be used to 645 /// evaluate the availability of this feature. 646 /// 647 /// \param Target The target options that will be used to evaluate the 648 /// availability of this feature. 649 void addRequirement(StringRef Feature, bool RequiredState, 650 const LangOptions &LangOpts, 651 const TargetInfo &Target); 652 653 /// Mark this module and all of its submodules as unavailable. 654 void markUnavailable(bool Unimportable); 655 656 /// Find the submodule with the given name. 657 /// 658 /// \returns The submodule if found, or NULL otherwise. 659 Module *findSubmodule(StringRef Name) const; 660 Module *findOrInferSubmodule(StringRef Name); 661 662 /// Determine whether the specified module would be visible to 663 /// a lookup at the end of this module. 664 /// 665 /// FIXME: This may return incorrect results for (submodules of) the 666 /// module currently being built, if it's queried before we see all 667 /// of its imports. 668 bool isModuleVisible(const Module *M) const { 669 if (VisibleModulesCache.empty()) 670 buildVisibleModulesCache(); 671 return VisibleModulesCache.count(M); 672 } 673 674 unsigned getVisibilityID() const { return VisibilityID; } 675 676 using submodule_iterator = std::vector<Module *>::iterator; 677 using submodule_const_iterator = std::vector<Module *>::const_iterator; 678 679 submodule_iterator submodule_begin() { return SubModules.begin(); } 680 submodule_const_iterator submodule_begin() const {return SubModules.begin();} 681 submodule_iterator submodule_end() { return SubModules.end(); } 682 submodule_const_iterator submodule_end() const { return SubModules.end(); } 683 684 llvm::iterator_range<submodule_iterator> submodules() { 685 return llvm::make_range(submodule_begin(), submodule_end()); 686 } 687 llvm::iterator_range<submodule_const_iterator> submodules() const { 688 return llvm::make_range(submodule_begin(), submodule_end()); 689 } 690 691 /// Appends this module's list of exported modules to \p Exported. 692 /// 693 /// This provides a subset of immediately imported modules (the ones that are 694 /// directly exported), not the complete set of exported modules. 695 void getExportedModules(SmallVectorImpl<Module *> &Exported) const; 696 697 static StringRef getModuleInputBufferName() { 698 return "<module-includes>"; 699 } 700 701 /// Print the module map for this module to the given stream. 702 void print(raw_ostream &OS, unsigned Indent = 0, bool Dump = false) const; 703 704 /// Dump the contents of this module to the given output stream. 705 void dump() const; 706 707 private: 708 void buildVisibleModulesCache() const; 709 }; 710 711 /// A set of visible modules. 712 class VisibleModuleSet { 713 public: 714 VisibleModuleSet() = default; 715 VisibleModuleSet(VisibleModuleSet &&O) 716 : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) { 717 O.ImportLocs.clear(); 718 ++O.Generation; 719 } 720 721 /// Move from another visible modules set. Guaranteed to leave the source 722 /// empty and bump the generation on both. 723 VisibleModuleSet &operator=(VisibleModuleSet &&O) { 724 ImportLocs = std::move(O.ImportLocs); 725 O.ImportLocs.clear(); 726 ++O.Generation; 727 ++Generation; 728 return *this; 729 } 730 731 /// Get the current visibility generation. Incremented each time the 732 /// set of visible modules changes in any way. 733 unsigned getGeneration() const { return Generation; } 734 735 /// Determine whether a module is visible. 736 bool isVisible(const Module *M) const { 737 return getImportLoc(M).isValid(); 738 } 739 740 /// Get the location at which the import of a module was triggered. 741 SourceLocation getImportLoc(const Module *M) const { 742 return M->getVisibilityID() < ImportLocs.size() 743 ? ImportLocs[M->getVisibilityID()] 744 : SourceLocation(); 745 } 746 747 /// A callback to call when a module is made visible (directly or 748 /// indirectly) by a call to \ref setVisible. 749 using VisibleCallback = llvm::function_ref<void(Module *M)>; 750 751 /// A callback to call when a module conflict is found. \p Path 752 /// consists of a sequence of modules from the conflicting module to the one 753 /// made visible, where each was exported by the next. 754 using ConflictCallback = 755 llvm::function_ref<void(ArrayRef<Module *> Path, Module *Conflict, 756 StringRef Message)>; 757 758 /// Make a specific module visible. 759 void setVisible(Module *M, SourceLocation Loc, 760 VisibleCallback Vis = [](Module *) {}, 761 ConflictCallback Cb = [](ArrayRef<Module *>, Module *, 762 StringRef) {}); 763 764 private: 765 /// Import locations for each visible module. Indexed by the module's 766 /// VisibilityID. 767 std::vector<SourceLocation> ImportLocs; 768 769 /// Visibility generation, bumped every time the visibility state changes. 770 unsigned Generation = 0; 771 }; 772 773 /// Abstracts clang modules and precompiled header files and holds 774 /// everything needed to generate debug info for an imported module 775 /// or PCH. 776 class ASTSourceDescriptor { 777 StringRef PCHModuleName; 778 StringRef Path; 779 StringRef ASTFile; 780 ASTFileSignature Signature; 781 Module *ClangModule = nullptr; 782 783 public: 784 ASTSourceDescriptor() = default; 785 ASTSourceDescriptor(StringRef Name, StringRef Path, StringRef ASTFile, 786 ASTFileSignature Signature) 787 : PCHModuleName(std::move(Name)), Path(std::move(Path)), 788 ASTFile(std::move(ASTFile)), Signature(Signature) {} 789 ASTSourceDescriptor(Module &M); 790 791 std::string getModuleName() const; 792 StringRef getPath() const { return Path; } 793 StringRef getASTFile() const { return ASTFile; } 794 ASTFileSignature getSignature() const { return Signature; } 795 Module *getModuleOrNull() const { return ClangModule; } 796 }; 797 798 799 } // namespace clang 800 801 #endif // LLVM_CLANG_BASIC_MODULE_H 802