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