ModuleManager.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. //===--- ModuleManager.cpp - Module Manager ---------------------*- 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. // This file defines the ModuleManager class, which manages a set of loaded
  11. // modules for the ASTReader.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
  15. #define LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
  16. #include "clang/Basic/FileManager.h"
  17. #include "clang/Serialization/Module.h"
  18. #include "llvm/ADT/DenseMap.h"
  19. #include "llvm/ADT/SmallPtrSet.h"
  20. namespace clang {
  21. class GlobalModuleIndex;
  22. class ModuleMap;
  23. class PCHContainerReader;
  24. namespace serialization {
  25. /// \brief Manages the set of modules loaded by an AST reader.
  26. class ModuleManager {
  27. /// \brief The chain of AST files, in the order in which we started to load
  28. /// them (this order isn't really useful for anything).
  29. SmallVector<ModuleFile *, 2> Chain;
  30. /// \brief The chain of non-module PCH files. The first entry is the one named
  31. /// by the user, the last one is the one that doesn't depend on anything
  32. /// further.
  33. SmallVector<ModuleFile *, 2> PCHChain;
  34. // \brief The roots of the dependency DAG of AST files. This is used
  35. // to implement short-circuiting logic when running DFS over the dependencies.
  36. SmallVector<ModuleFile *, 2> Roots;
  37. /// \brief All loaded modules, indexed by name.
  38. llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
  39. /// \brief FileManager that handles translating between filenames and
  40. /// FileEntry *.
  41. FileManager &FileMgr;
  42. /// \brief Knows how to unwrap module containers.
  43. const PCHContainerReader &PCHContainerRdr;
  44. /// \brief A lookup of in-memory (virtual file) buffers
  45. llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
  46. InMemoryBuffers;
  47. /// \brief The visitation order.
  48. SmallVector<ModuleFile *, 4> VisitOrder;
  49. /// \brief The list of module files that both we and the global module index
  50. /// know about.
  51. ///
  52. /// Either the global index or the module manager may have modules that the
  53. /// other does not know about, because the global index can be out-of-date
  54. /// (in which case the module manager could have modules it does not) and
  55. /// this particular translation unit might not have loaded all of the modules
  56. /// known to the global index.
  57. SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
  58. /// \brief The global module index, if one is attached.
  59. ///
  60. /// The global module index will actually be owned by the ASTReader; this is
  61. /// just an non-owning pointer.
  62. GlobalModuleIndex *GlobalIndex;
  63. /// \brief State used by the "visit" operation to avoid malloc traffic in
  64. /// calls to visit().
  65. struct VisitState {
  66. explicit VisitState(unsigned N)
  67. : VisitNumber(N, 0), NextVisitNumber(1), NextState(nullptr)
  68. {
  69. Stack.reserve(N);
  70. }
  71. ~VisitState() {
  72. delete NextState;
  73. }
  74. /// \brief The stack used when marking the imports of a particular module
  75. /// as not-to-be-visited.
  76. SmallVector<ModuleFile *, 4> Stack;
  77. /// \brief The visit number of each module file, which indicates when
  78. /// this module file was last visited.
  79. SmallVector<unsigned, 4> VisitNumber;
  80. /// \brief The next visit number to use to mark visited module files.
  81. unsigned NextVisitNumber;
  82. /// \brief The next visit state.
  83. VisitState *NextState;
  84. };
  85. /// \brief The first visit() state in the chain.
  86. VisitState *FirstVisitState;
  87. VisitState *allocateVisitState();
  88. void returnVisitState(VisitState *State);
  89. public:
  90. typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator;
  91. typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator;
  92. typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator;
  93. typedef std::pair<uint32_t, StringRef> ModuleOffset;
  94. explicit ModuleManager(FileManager &FileMgr,
  95. const PCHContainerReader &PCHContainerRdr);
  96. ~ModuleManager();
  97. /// \brief Forward iterator to traverse all loaded modules.
  98. ModuleIterator begin() { return Chain.begin(); }
  99. /// \brief Forward iterator end-point to traverse all loaded modules
  100. ModuleIterator end() { return Chain.end(); }
  101. /// \brief Const forward iterator to traverse all loaded modules.
  102. ModuleConstIterator begin() const { return Chain.begin(); }
  103. /// \brief Const forward iterator end-point to traverse all loaded modules
  104. ModuleConstIterator end() const { return Chain.end(); }
  105. /// \brief Reverse iterator to traverse all loaded modules.
  106. ModuleReverseIterator rbegin() { return Chain.rbegin(); }
  107. /// \brief Reverse iterator end-point to traverse all loaded modules.
  108. ModuleReverseIterator rend() { return Chain.rend(); }
  109. /// \brief A range covering the PCH and preamble module files loaded.
  110. llvm::iterator_range<ModuleConstIterator> pch_modules() const {
  111. return llvm::make_range(PCHChain.begin(), PCHChain.end());
  112. }
  113. /// \brief Returns the primary module associated with the manager, that is,
  114. /// the first module loaded
  115. ModuleFile &getPrimaryModule() { return *Chain[0]; }
  116. /// \brief Returns the primary module associated with the manager, that is,
  117. /// the first module loaded.
  118. ModuleFile &getPrimaryModule() const { return *Chain[0]; }
  119. /// \brief Returns the module associated with the given index
  120. ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
  121. /// \brief Returns the module associated with the given name
  122. ModuleFile *lookup(StringRef Name);
  123. /// \brief Returns the module associated with the given module file.
  124. ModuleFile *lookup(const FileEntry *File);
  125. /// \brief Returns the in-memory (virtual file) buffer with the given name
  126. std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
  127. /// \brief Number of modules loaded
  128. unsigned size() const { return Chain.size(); }
  129. /// \brief The result of attempting to add a new module.
  130. enum AddModuleResult {
  131. /// \brief The module file had already been loaded.
  132. AlreadyLoaded,
  133. /// \brief The module file was just loaded in response to this call.
  134. NewlyLoaded,
  135. /// \brief The module file is missing.
  136. Missing,
  137. /// \brief The module file is out-of-date.
  138. OutOfDate
  139. };
  140. typedef ASTFileSignature(*ASTFileSignatureReader)(StringRef);
  141. /// \brief Attempts to create a new module and add it to the list of known
  142. /// modules.
  143. ///
  144. /// \param FileName The file name of the module to be loaded.
  145. ///
  146. /// \param Type The kind of module being loaded.
  147. ///
  148. /// \param ImportLoc The location at which the module is imported.
  149. ///
  150. /// \param ImportedBy The module that is importing this module, or NULL if
  151. /// this module is imported directly by the user.
  152. ///
  153. /// \param Generation The generation in which this module was loaded.
  154. ///
  155. /// \param ExpectedSize The expected size of the module file, used for
  156. /// validation. This will be zero if unknown.
  157. ///
  158. /// \param ExpectedModTime The expected modification time of the module
  159. /// file, used for validation. This will be zero if unknown.
  160. ///
  161. /// \param ExpectedSignature The expected signature of the module file, used
  162. /// for validation. This will be zero if unknown.
  163. ///
  164. /// \param ReadSignature Reads the signature from an AST file without actually
  165. /// loading it.
  166. ///
  167. /// \param Module A pointer to the module file if the module was successfully
  168. /// loaded.
  169. ///
  170. /// \param ErrorStr Will be set to a non-empty string if any errors occurred
  171. /// while trying to load the module.
  172. ///
  173. /// \return A pointer to the module that corresponds to this file name,
  174. /// and a value indicating whether the module was loaded.
  175. AddModuleResult addModule(StringRef FileName, ModuleKind Type,
  176. SourceLocation ImportLoc,
  177. ModuleFile *ImportedBy, unsigned Generation,
  178. off_t ExpectedSize, time_t ExpectedModTime,
  179. ASTFileSignature ExpectedSignature,
  180. ASTFileSignatureReader ReadSignature,
  181. ModuleFile *&Module,
  182. std::string &ErrorStr);
  183. /// \brief Remove the given set of modules.
  184. void removeModules(ModuleIterator first, ModuleIterator last,
  185. llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
  186. ModuleMap *modMap);
  187. /// \brief Add an in-memory buffer the list of known buffers
  188. void addInMemoryBuffer(StringRef FileName,
  189. std::unique_ptr<llvm::MemoryBuffer> Buffer);
  190. /// \brief Set the global module index.
  191. void setGlobalIndex(GlobalModuleIndex *Index);
  192. /// \brief Notification from the AST reader that the given module file
  193. /// has been "accepted", and will not (can not) be unloaded.
  194. void moduleFileAccepted(ModuleFile *MF);
  195. /// \brief Visit each of the modules.
  196. ///
  197. /// This routine visits each of the modules, starting with the
  198. /// "root" modules that no other loaded modules depend on, and
  199. /// proceeding to the leaf modules, visiting each module only once
  200. /// during the traversal.
  201. ///
  202. /// This traversal is intended to support various "lookup"
  203. /// operations that can find data in any of the loaded modules.
  204. ///
  205. /// \param Visitor A visitor function that will be invoked with each
  206. /// module. The return value must be convertible to bool; when false, the
  207. /// visitation continues to modules that the current module depends on. When
  208. /// true, the visitation skips any modules that the current module depends on.
  209. ///
  210. /// \param ModuleFilesHit If non-NULL, contains the set of module files
  211. /// that we know we need to visit because the global module index told us to.
  212. /// Any module that is known to both the global module index and the module
  213. /// manager that is *not* in this set can be skipped.
  214. void visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
  215. llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit = nullptr);
  216. /// \brief Attempt to resolve the given module file name to a file entry.
  217. ///
  218. /// \param FileName The name of the module file.
  219. ///
  220. /// \param ExpectedSize The size that the module file is expected to have.
  221. /// If the actual size differs, the resolver should return \c true.
  222. ///
  223. /// \param ExpectedModTime The modification time that the module file is
  224. /// expected to have. If the actual modification time differs, the resolver
  225. /// should return \c true.
  226. ///
  227. /// \param File Will be set to the file if there is one, or null
  228. /// otherwise.
  229. ///
  230. /// \returns True if a file exists but does not meet the size/
  231. /// modification time criteria, false if the file is either available and
  232. /// suitable, or is missing.
  233. bool lookupModuleFile(StringRef FileName,
  234. off_t ExpectedSize,
  235. time_t ExpectedModTime,
  236. const FileEntry *&File);
  237. /// \brief View the graphviz representation of the module graph.
  238. void viewGraph();
  239. };
  240. } } // end namespace clang::serialization
  241. #endif