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