Module.h 19 KB

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