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 { LinkLibraryLinkLibrary258 LinkLibrary() : IsFramework(false) { } LinkLibraryLinkLibrary259 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. isAvailable()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. isSubModule()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. isPartOfFramework()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. isSubFramework()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 Retrieve the top-level module for this (sub)module, which may 360 /// be this module. getTopLevelModule()361 Module *getTopLevelModule() { 362 return const_cast<Module *>( 363 const_cast<const Module *>(this)->getTopLevelModule()); 364 } 365 366 /// \brief Retrieve the top-level module for this (sub)module, which may 367 /// be this module. 368 const Module *getTopLevelModule() const; 369 370 /// \brief Retrieve the name of the top-level module. 371 /// getTopLevelModuleName()372 StringRef getTopLevelModuleName() const { 373 return getTopLevelModule()->Name; 374 } 375 376 /// \brief The serialized AST file for this module, if one was created. getASTFile()377 const FileEntry *getASTFile() const { 378 return getTopLevelModule()->ASTFile; 379 } 380 381 /// \brief Set the serialized AST file for the top-level module of this module. setASTFile(const FileEntry * File)382 void setASTFile(const FileEntry *File) { 383 assert((File == nullptr || getASTFile() == nullptr || 384 getASTFile() == File) && "file path changed"); 385 getTopLevelModule()->ASTFile = File; 386 } 387 388 /// \brief Retrieve the directory for which this module serves as the 389 /// umbrella. 390 DirectoryName getUmbrellaDir() const; 391 392 /// \brief Retrieve the header that serves as the umbrella header for this 393 /// module. getUmbrellaHeader()394 Header getUmbrellaHeader() const { 395 if (auto *E = Umbrella.dyn_cast<const FileEntry *>()) 396 return Header{UmbrellaAsWritten, E}; 397 return Header{}; 398 } 399 400 /// \brief Determine whether this module has an umbrella directory that is 401 /// not based on an umbrella header. hasUmbrellaDir()402 bool hasUmbrellaDir() const { 403 return Umbrella && Umbrella.is<const DirectoryEntry *>(); 404 } 405 406 /// \brief Add a top-level header associated with this module. addTopHeader(const FileEntry * File)407 void addTopHeader(const FileEntry *File) { 408 assert(File); 409 TopHeaders.insert(File); 410 } 411 412 /// \brief Add a top-level header filename associated with this module. addTopHeaderFilename(StringRef Filename)413 void addTopHeaderFilename(StringRef Filename) { 414 TopHeaderNames.push_back(Filename); 415 } 416 417 /// \brief The top-level headers associated with this module. 418 ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr); 419 420 /// \brief Determine whether this module has declared its intention to 421 /// directly use another module. 422 bool directlyUses(const Module *Requested) const; 423 424 /// \brief Add the given feature requirement to the list of features 425 /// required by this module. 426 /// 427 /// \param Feature The feature that is required by this module (and 428 /// its submodules). 429 /// 430 /// \param RequiredState The required state of this feature: \c true 431 /// if it must be present, \c false if it must be absent. 432 /// 433 /// \param LangOpts The set of language options that will be used to 434 /// evaluate the availability of this feature. 435 /// 436 /// \param Target The target options that will be used to evaluate the 437 /// availability of this feature. 438 void addRequirement(StringRef Feature, bool RequiredState, 439 const LangOptions &LangOpts, 440 const TargetInfo &Target); 441 442 /// \brief Mark this module and all of its submodules as unavailable. 443 void markUnavailable(bool MissingRequirement = false); 444 445 /// \brief Find the submodule with the given name. 446 /// 447 /// \returns The submodule if found, or NULL otherwise. 448 Module *findSubmodule(StringRef Name) const; 449 450 /// \brief Determine whether the specified module would be visible to 451 /// a lookup at the end of this module. 452 /// 453 /// FIXME: This may return incorrect results for (submodules of) the 454 /// module currently being built, if it's queried before we see all 455 /// of its imports. isModuleVisible(const Module * M)456 bool isModuleVisible(const Module *M) const { 457 if (VisibleModulesCache.empty()) 458 buildVisibleModulesCache(); 459 return VisibleModulesCache.count(M); 460 } 461 getVisibilityID()462 unsigned getVisibilityID() const { return VisibilityID; } 463 464 typedef std::vector<Module *>::iterator submodule_iterator; 465 typedef std::vector<Module *>::const_iterator submodule_const_iterator; 466 submodule_begin()467 submodule_iterator submodule_begin() { return SubModules.begin(); } submodule_begin()468 submodule_const_iterator submodule_begin() const {return SubModules.begin();} submodule_end()469 submodule_iterator submodule_end() { return SubModules.end(); } submodule_end()470 submodule_const_iterator submodule_end() const { return SubModules.end(); } 471 472 /// \brief Appends this module's list of exported modules to \p Exported. 473 /// 474 /// This provides a subset of immediately imported modules (the ones that are 475 /// directly exported), not the complete set of exported modules. 476 void getExportedModules(SmallVectorImpl<Module *> &Exported) const; 477 getModuleInputBufferName()478 static StringRef getModuleInputBufferName() { 479 return "<module-includes>"; 480 } 481 482 /// \brief Print the module map for this module to the given stream. 483 /// 484 void print(raw_ostream &OS, unsigned Indent = 0) const; 485 486 /// \brief Dump the contents of this module to the given output stream. 487 void dump() const; 488 489 private: 490 void buildVisibleModulesCache() const; 491 }; 492 493 /// \brief A set of visible modules. 494 class VisibleModuleSet { 495 public: VisibleModuleSet()496 VisibleModuleSet() : Generation(0) {} VisibleModuleSet(VisibleModuleSet && O)497 VisibleModuleSet(VisibleModuleSet &&O) 498 : ImportLocs(std::move(O.ImportLocs)), Generation(O.Generation ? 1 : 0) { 499 O.ImportLocs.clear(); 500 ++O.Generation; 501 } 502 503 /// Move from another visible modules set. Guaranteed to leave the source 504 /// empty and bump the generation on both. 505 VisibleModuleSet &operator=(VisibleModuleSet &&O) { 506 ImportLocs = std::move(O.ImportLocs); 507 O.ImportLocs.clear(); 508 ++O.Generation; 509 ++Generation; 510 return *this; 511 } 512 513 /// \brief Get the current visibility generation. Incremented each time the 514 /// set of visible modules changes in any way. getGeneration()515 unsigned getGeneration() const { return Generation; } 516 517 /// \brief Determine whether a module is visible. isVisible(const Module * M)518 bool isVisible(const Module *M) const { 519 return getImportLoc(M).isValid(); 520 } 521 522 /// \brief Get the location at which the import of a module was triggered. getImportLoc(const Module * M)523 SourceLocation getImportLoc(const Module *M) const { 524 return M->getVisibilityID() < ImportLocs.size() 525 ? ImportLocs[M->getVisibilityID()] 526 : SourceLocation(); 527 } 528 529 /// \brief A callback to call when a module is made visible (directly or 530 /// indirectly) by a call to \ref setVisible. 531 typedef llvm::function_ref<void(Module *M)> VisibleCallback; 532 /// \brief A callback to call when a module conflict is found. \p Path 533 /// consists of a sequence of modules from the conflicting module to the one 534 /// made visible, where each was exported by the next. 535 typedef llvm::function_ref<void(ArrayRef<Module *> Path, 536 Module *Conflict, StringRef Message)> 537 ConflictCallback; 538 /// \brief Make a specific module visible. 539 void setVisible(Module *M, SourceLocation Loc, 540 VisibleCallback Vis = [](Module *) {}, 541 ConflictCallback Cb = [](ArrayRef<Module *>, Module *, 542 StringRef) {}); 543 544 private: 545 /// Import locations for each visible module. Indexed by the module's 546 /// VisibilityID. 547 std::vector<SourceLocation> ImportLocs; 548 /// Visibility generation, bumped every time the visibility state changes. 549 unsigned Generation; 550 }; 551 552 } // end namespace clang 553 554 555 #endif // LLVM_CLANG_BASIC_MODULE_H 556