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