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