Module.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/DenseSet.h"
  19. #include "llvm/ADT/PointerIntPair.h"
  20. #include "llvm/ADT/PointerUnion.h"
  21. #include "llvm/ADT/SetVector.h"
  22. #include "llvm/ADT/SmallVector.h"
  23. #include "llvm/ADT/StringMap.h"
  24. #include "llvm/ADT/StringRef.h"
  25. #include <string>
  26. #include <utility>
  27. #include <vector>
  28. namespace llvm {
  29. class raw_ostream;
  30. }
  31. namespace clang {
  32. class DirectoryEntry;
  33. class FileEntry;
  34. class FileManager;
  35. class LangOptions;
  36. class TargetInfo;
  37. class IdentifierInfo;
  38. /// \brief Describes the name of a module.
  39. typedef SmallVector<std::pair<std::string, SourceLocation>, 2> ModuleId;
  40. /// \brief Describes a module or submodule.
  41. class Module {
  42. public:
  43. /// \brief The name of this module.
  44. std::string Name;
  45. /// \brief The location of the module definition.
  46. SourceLocation DefinitionLoc;
  47. /// \brief The parent of this module. This will be NULL for the top-level
  48. /// module.
  49. Module *Parent;
  50. /// \brief The module map file that (along with the module name) uniquely
  51. /// identifies this module.
  52. ///
  53. /// The particular module that \c Name refers to may depend on how the module
  54. /// was found in header search. However, the combination of \c Name and
  55. /// \c ModuleMap will be globally unique for top-level modules. In the case of
  56. /// inferred modules, \c ModuleMap will contain the module map that allowed
  57. /// the inference (e.g. contained 'Module *') rather than the virtual
  58. /// inferred module map file.
  59. const FileEntry *ModuleMap;
  60. /// \brief The umbrella header or directory.
  61. llvm::PointerUnion<const DirectoryEntry *, const FileEntry *> Umbrella;
  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. public:
  78. /// \brief The headers that are part of this module.
  79. SmallVector<const FileEntry *, 2> NormalHeaders;
  80. /// \brief The headers that are explicitly excluded from this module.
  81. SmallVector<const FileEntry *, 2> ExcludedHeaders;
  82. /// \brief The headers that are private to this module.
  83. SmallVector<const FileEntry *, 2> PrivateHeaders;
  84. /// \brief Information about a header directive as found in the module map
  85. /// file.
  86. struct HeaderDirective {
  87. SourceLocation FileNameLoc;
  88. std::string FileName;
  89. bool IsUmbrella;
  90. };
  91. /// \brief Headers that are mentioned in the module map file but could not be
  92. /// found on the file system.
  93. SmallVector<HeaderDirective, 1> MissingHeaders;
  94. /// \brief An individual requirement: a feature name and a flag indicating
  95. /// the required state of that feature.
  96. typedef std::pair<std::string, bool> Requirement;
  97. /// \brief The set of language features required to use this module.
  98. ///
  99. /// If any of these requirements are not available, the \c IsAvailable bit
  100. /// will be false to indicate that this (sub)module is not available.
  101. SmallVector<Requirement, 2> Requirements;
  102. /// \brief Whether this module is missing a feature from \c Requirements.
  103. unsigned IsMissingRequirement : 1;
  104. /// \brief Whether this module is available in the current translation unit.
  105. ///
  106. /// If the module is missing headers or does not meet all requirements then
  107. /// this bit will be 0.
  108. unsigned IsAvailable : 1;
  109. /// \brief Whether this module was loaded from a module file.
  110. unsigned IsFromModuleFile : 1;
  111. /// \brief Whether this is a framework module.
  112. unsigned IsFramework : 1;
  113. /// \brief Whether this is an explicit submodule.
  114. unsigned IsExplicit : 1;
  115. /// \brief Whether this is a "system" module (which assumes that all
  116. /// headers in it are system headers).
  117. unsigned IsSystem : 1;
  118. /// \brief Whether this is an 'extern "C"' module (which implicitly puts all
  119. /// headers in it within an 'extern "C"' block, and allows the module to be
  120. /// imported within such a block).
  121. unsigned IsExternC : 1;
  122. /// \brief Whether we should infer submodules for this module based on
  123. /// the headers.
  124. ///
  125. /// Submodules can only be inferred for modules with an umbrella header.
  126. unsigned InferSubmodules : 1;
  127. /// \brief Whether, when inferring submodules, the inferred submodules
  128. /// should be explicit.
  129. unsigned InferExplicitSubmodules : 1;
  130. /// \brief Whether, when inferring submodules, the inferr submodules should
  131. /// export all modules they import (e.g., the equivalent of "export *").
  132. unsigned InferExportWildcard : 1;
  133. /// \brief Whether the set of configuration macros is exhaustive.
  134. ///
  135. /// When the set of configuration macros is exhaustive, meaning
  136. /// that no identifier not in this list should affect how the module is
  137. /// built.
  138. unsigned ConfigMacrosExhaustive : 1;
  139. /// \brief Describes the visibility of the various names within a
  140. /// particular module.
  141. enum NameVisibilityKind {
  142. /// \brief All of the names in this module are hidden.
  143. ///
  144. Hidden,
  145. /// \brief Only the macro names in this module are visible.
  146. MacrosVisible,
  147. /// \brief All of the names in this module are visible.
  148. AllVisible
  149. };
  150. /// \brief The visibility of names within this particular module.
  151. NameVisibilityKind NameVisibility;
  152. /// \brief The location at which macros within this module became visible.
  153. SourceLocation MacroVisibilityLoc;
  154. /// \brief The location of the inferred submodule.
  155. SourceLocation InferredSubmoduleLoc;
  156. /// \brief The set of modules imported by this module, and on which this
  157. /// module depends.
  158. SmallVector<Module *, 2> Imports;
  159. /// \brief Describes an exported module.
  160. ///
  161. /// The pointer is the module being re-exported, while the bit will be true
  162. /// to indicate that this is a wildcard export.
  163. typedef llvm::PointerIntPair<Module *, 1, bool> ExportDecl;
  164. /// \brief The set of export declarations.
  165. SmallVector<ExportDecl, 2> Exports;
  166. /// \brief Describes an exported module that has not yet been resolved
  167. /// (perhaps because the module it refers to has not yet been loaded).
  168. struct UnresolvedExportDecl {
  169. /// \brief The location of the 'export' keyword in the module map file.
  170. SourceLocation ExportLoc;
  171. /// \brief The name of the module.
  172. ModuleId Id;
  173. /// \brief Whether this export declaration ends in a wildcard, indicating
  174. /// that all of its submodules should be exported (rather than the named
  175. /// module itself).
  176. bool Wildcard;
  177. };
  178. /// \brief The set of export declarations that have yet to be resolved.
  179. SmallVector<UnresolvedExportDecl, 2> UnresolvedExports;
  180. /// \brief The directly used modules.
  181. SmallVector<Module *, 2> DirectUses;
  182. /// \brief The set of use declarations that have yet to be resolved.
  183. SmallVector<ModuleId, 2> UnresolvedDirectUses;
  184. /// \brief A library or framework to link against when an entity from this
  185. /// module is used.
  186. struct LinkLibrary {
  187. LinkLibrary() : IsFramework(false) { }
  188. LinkLibrary(const std::string &Library, bool IsFramework)
  189. : Library(Library), IsFramework(IsFramework) { }
  190. /// \brief The library to link against.
  191. ///
  192. /// This will typically be a library or framework name, but can also
  193. /// be an absolute path to the library or framework.
  194. std::string Library;
  195. /// \brief Whether this is a framework rather than a library.
  196. bool IsFramework;
  197. };
  198. /// \brief The set of libraries or frameworks to link against when
  199. /// an entity from this module is used.
  200. llvm::SmallVector<LinkLibrary, 2> LinkLibraries;
  201. /// \brief The set of "configuration macros", which are macros that
  202. /// (intentionally) change how this module is built.
  203. std::vector<std::string> ConfigMacros;
  204. /// \brief An unresolved conflict with another module.
  205. struct UnresolvedConflict {
  206. /// \brief The (unresolved) module id.
  207. ModuleId Id;
  208. /// \brief The message provided to the user when there is a conflict.
  209. std::string Message;
  210. };
  211. /// \brief The list of conflicts for which the module-id has not yet been
  212. /// resolved.
  213. std::vector<UnresolvedConflict> UnresolvedConflicts;
  214. /// \brief A conflict between two modules.
  215. struct Conflict {
  216. /// \brief The module that this module conflicts with.
  217. Module *Other;
  218. /// \brief The message provided to the user when there is a conflict.
  219. std::string Message;
  220. };
  221. /// \brief The list of conflicts.
  222. std::vector<Conflict> Conflicts;
  223. /// \brief Construct a new module or submodule.
  224. ///
  225. /// For an explanation of \p ModuleMap, see Module::ModuleMap.
  226. Module(StringRef Name, SourceLocation DefinitionLoc, Module *Parent,
  227. const FileEntry *ModuleMap, bool IsFramework, bool IsExplicit);
  228. ~Module();
  229. /// \brief Determine whether this module is available for use within the
  230. /// current translation unit.
  231. bool isAvailable() const { return IsAvailable; }
  232. /// \brief Determine whether this module is available for use within the
  233. /// current translation unit.
  234. ///
  235. /// \param LangOpts The language options used for the current
  236. /// translation unit.
  237. ///
  238. /// \param Target The target options used for the current translation unit.
  239. ///
  240. /// \param Req If this module is unavailable, this parameter
  241. /// will be set to one of the requirements that is not met for use of
  242. /// this module.
  243. bool isAvailable(const LangOptions &LangOpts,
  244. const TargetInfo &Target,
  245. Requirement &Req,
  246. HeaderDirective &MissingHeader) const;
  247. /// \brief Determine whether this module is a submodule.
  248. bool isSubModule() const { return Parent != 0; }
  249. /// \brief Determine whether this module is a submodule of the given other
  250. /// module.
  251. bool isSubModuleOf(const Module *Other) const;
  252. /// \brief Determine whether this module is a part of a framework,
  253. /// either because it is a framework module or because it is a submodule
  254. /// of a framework module.
  255. bool isPartOfFramework() const {
  256. for (const Module *Mod = this; Mod; Mod = Mod->Parent)
  257. if (Mod->IsFramework)
  258. return true;
  259. return false;
  260. }
  261. /// \brief Determine whether this module is a subframework of another
  262. /// framework.
  263. bool isSubFramework() const {
  264. return IsFramework && Parent && Parent->isPartOfFramework();
  265. }
  266. /// \brief Retrieve the full name of this module, including the path from
  267. /// its top-level module.
  268. std::string getFullModuleName() const;
  269. /// \brief Retrieve the top-level module for this (sub)module, which may
  270. /// be this module.
  271. Module *getTopLevelModule() {
  272. return const_cast<Module *>(
  273. const_cast<const Module *>(this)->getTopLevelModule());
  274. }
  275. /// \brief Retrieve the top-level module for this (sub)module, which may
  276. /// be this module.
  277. const Module *getTopLevelModule() const;
  278. /// \brief Retrieve the name of the top-level module.
  279. ///
  280. StringRef getTopLevelModuleName() const {
  281. return getTopLevelModule()->Name;
  282. }
  283. /// \brief The serialized AST file for this module, if one was created.
  284. const FileEntry *getASTFile() const {
  285. return getTopLevelModule()->ASTFile;
  286. }
  287. /// \brief Set the serialized AST file for the top-level module of this module.
  288. void setASTFile(const FileEntry *File) {
  289. assert((File == 0 || getASTFile() == 0 || getASTFile() == File) &&
  290. "file path changed");
  291. getTopLevelModule()->ASTFile = File;
  292. }
  293. /// \brief Retrieve the directory for which this module serves as the
  294. /// umbrella.
  295. const DirectoryEntry *getUmbrellaDir() const;
  296. /// \brief Retrieve the header that serves as the umbrella header for this
  297. /// module.
  298. const FileEntry *getUmbrellaHeader() const {
  299. return Umbrella.dyn_cast<const FileEntry *>();
  300. }
  301. /// \brief Determine whether this module has an umbrella directory that is
  302. /// not based on an umbrella header.
  303. bool hasUmbrellaDir() const {
  304. return Umbrella && Umbrella.is<const DirectoryEntry *>();
  305. }
  306. /// \brief Add a top-level header associated with this module.
  307. void addTopHeader(const FileEntry *File) {
  308. assert(File);
  309. TopHeaders.insert(File);
  310. }
  311. /// \brief Add a top-level header filename associated with this module.
  312. void addTopHeaderFilename(StringRef Filename) {
  313. TopHeaderNames.push_back(Filename);
  314. }
  315. /// \brief The top-level headers associated with this module.
  316. ArrayRef<const FileEntry *> getTopHeaders(FileManager &FileMgr);
  317. /// \brief Add the given feature requirement to the list of features
  318. /// required by this module.
  319. ///
  320. /// \param Feature The feature that is required by this module (and
  321. /// its submodules).
  322. ///
  323. /// \param RequiredState The required state of this feature: \c true
  324. /// if it must be present, \c false if it must be absent.
  325. ///
  326. /// \param LangOpts The set of language options that will be used to
  327. /// evaluate the availability of this feature.
  328. ///
  329. /// \param Target The target options that will be used to evaluate the
  330. /// availability of this feature.
  331. void addRequirement(StringRef Feature, bool RequiredState,
  332. const LangOptions &LangOpts,
  333. const TargetInfo &Target);
  334. /// \brief Mark this module and all of its submodules as unavailable.
  335. void markUnavailable();
  336. /// \brief Find the submodule with the given name.
  337. ///
  338. /// \returns The submodule if found, or NULL otherwise.
  339. Module *findSubmodule(StringRef Name) const;
  340. /// \brief Determine whether the specified module would be visible to
  341. /// a lookup at the end of this module.
  342. bool isModuleVisible(const Module *M) const {
  343. if (VisibleModulesCache.empty())
  344. buildVisibleModulesCache();
  345. return VisibleModulesCache.count(M);
  346. }
  347. typedef std::vector<Module *>::iterator submodule_iterator;
  348. typedef std::vector<Module *>::const_iterator submodule_const_iterator;
  349. submodule_iterator submodule_begin() { return SubModules.begin(); }
  350. submodule_const_iterator submodule_begin() const {return SubModules.begin();}
  351. submodule_iterator submodule_end() { return SubModules.end(); }
  352. submodule_const_iterator submodule_end() const { return SubModules.end(); }
  353. /// \brief Appends this module's list of exported modules to \p Exported.
  354. ///
  355. /// This provides a subset of immediately imported modules (the ones that are
  356. /// directly exported), not the complete set of exported modules.
  357. void getExportedModules(SmallVectorImpl<Module *> &Exported) const;
  358. static StringRef getModuleInputBufferName() {
  359. return "<module-includes>";
  360. }
  361. /// \brief Print the module map for this module to the given stream.
  362. ///
  363. void print(raw_ostream &OS, unsigned Indent = 0) const;
  364. /// \brief Dump the contents of this module to the given output stream.
  365. void dump() const;
  366. private:
  367. void buildVisibleModulesCache() const;
  368. };
  369. } // end namespace clang
  370. #endif // LLVM_CLANG_BASIC_MODULE_H