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