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