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