ModuleManager.h revision 677e15ffee2ecc9c1c8f46fd77cab4b5afb59640
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/GlobalModuleIndex.h" 20#include "clang/Serialization/Module.h" 21#include "llvm/ADT/DenseMap.h" 22 23namespace clang { 24 25class ModuleMap; 26 27namespace serialization { 28 29/// \brief Manages the set of modules loaded by an AST reader. 30class ModuleManager : public ModuleFileNameResolver { 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 Update the set of modules files we know about known to the global index. 65 void updateModulesInCommonWithGlobalIndex(); 66 67 /// \brief State used by the "visit" operation to avoid malloc traffic in 68 /// calls to visit(). 69 struct VisitState { 70 explicit VisitState(unsigned N) 71 : VisitNumber(N, 0), NextVisitNumber(1), NextState(0) 72 { 73 Stack.reserve(N); 74 } 75 76 ~VisitState() { 77 delete NextState; 78 } 79 80 /// \brief The stack used when marking the imports of a particular module 81 /// as not-to-be-visited. 82 SmallVector<ModuleFile *, 4> Stack; 83 84 /// \brief The visit number of each module file, which indicates when 85 /// this module file was last visited. 86 SmallVector<unsigned, 4> VisitNumber; 87 88 /// \brief The next visit number to use to mark visited module files. 89 unsigned NextVisitNumber; 90 91 /// \brief The next visit state. 92 VisitState *NextState; 93 }; 94 95 /// \brief The first visit() state in the chain. 96 VisitState *FirstVisitState; 97 98 VisitState *allocateVisitState(); 99 void returnVisitState(VisitState *State); 100 101public: 102 typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 103 typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 104 typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 105 typedef std::pair<uint32_t, StringRef> ModuleOffset; 106 107 explicit ModuleManager(FileManager &FileMgr); 108 ~ModuleManager(); 109 110 /// \brief Forward iterator to traverse all loaded modules. This is reverse 111 /// source-order. 112 ModuleIterator begin() { return Chain.begin(); } 113 /// \brief Forward iterator end-point to traverse all loaded modules 114 ModuleIterator end() { return Chain.end(); } 115 116 /// \brief Const forward iterator to traverse all loaded modules. This is 117 /// in reverse source-order. 118 ModuleConstIterator begin() const { return Chain.begin(); } 119 /// \brief Const forward iterator end-point to traverse all loaded modules 120 ModuleConstIterator end() const { return Chain.end(); } 121 122 /// \brief Reverse iterator to traverse all loaded modules. This is in 123 /// source order. 124 ModuleReverseIterator rbegin() { return Chain.rbegin(); } 125 /// \brief Reverse iterator end-point to traverse all loaded modules. 126 ModuleReverseIterator rend() { return Chain.rend(); } 127 128 /// \brief Returns the primary module associated with the manager, that is, 129 /// the first module loaded 130 ModuleFile &getPrimaryModule() { return *Chain[0]; } 131 132 /// \brief Returns the primary module associated with the manager, that is, 133 /// the first module loaded. 134 ModuleFile &getPrimaryModule() const { return *Chain[0]; } 135 136 /// \brief Returns the module associated with the given index 137 ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 138 139 /// \brief Returns the module associated with the given name 140 ModuleFile *lookup(StringRef Name); 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 Visit each of the modules. 206 /// 207 /// This routine visits each of the modules, starting with the 208 /// "root" modules that no other loaded modules depend on, and 209 /// proceeding to the leaf modules, visiting each module only once 210 /// during the traversal. 211 /// 212 /// This traversal is intended to support various "lookup" 213 /// operations that can find data in any of the loaded modules. 214 /// 215 /// \param Visitor A visitor function that will be invoked with each 216 /// module and the given user data pointer. The return value must be 217 /// convertible to bool; when false, the visitation continues to 218 /// modules that the current module depends on. When true, the 219 /// visitation skips any modules that the current module depends on. 220 /// 221 /// \param UserData User data associated with the visitor object, which 222 /// will be passed along to the visitor. 223 /// 224 /// \param ModuleFilesHit If non-NULL, contains the set of module files 225 /// that we know we need to visit because the global module index told us to. 226 /// Any module that is known to both the global module index and the module 227 /// manager that is *not* in this set can be skipped. 228 void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData, 229 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit = 0); 230 231 /// \brief Visit each of the modules with a depth-first traversal. 232 /// 233 /// This routine visits each of the modules known to the module 234 /// manager using a depth-first search, starting with the first 235 /// loaded module. The traversal invokes the callback both before 236 /// traversing the children (preorder traversal) and after 237 /// traversing the children (postorder traversal). 238 /// 239 /// \param Visitor A visitor function that will be invoked with each 240 /// module and given a \c Preorder flag that indicates whether we're 241 /// visiting the module before or after visiting its children. The 242 /// visitor may return true at any time to abort the depth-first 243 /// visitation. 244 /// 245 /// \param UserData User data ssociated with the visitor object, 246 /// which will be passed along to the user. 247 void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 248 void *UserData), 249 void *UserData); 250 251 /// \brief Attempt to resolve the given module file name to a file entry. 252 /// 253 /// \param FileName The name of the module file. 254 /// 255 /// \param ExpectedSize The size that the module file is expected to have. 256 /// If the actual size differs, the resolver should return \c true. 257 /// 258 /// \param ExpectedModTime The modification time that the module file is 259 /// expected to have. If the actual modification time differs, the resolver 260 /// should return \c true. 261 /// 262 /// \param File Will be set to the file if there is one, or null 263 /// otherwise. 264 /// 265 /// \returns True if a file exists but does not meet the size/ 266 /// modification time criteria, false if the file is either available and 267 /// suitable, or is missing. 268 bool lookupModuleFile(StringRef FileName, 269 off_t ExpectedSize, 270 time_t ExpectedModTime, 271 const FileEntry *&File); 272 273 virtual bool resolveModuleFileName(StringRef FileName, 274 off_t ExpectedSize, 275 time_t ExpectedModTime, 276 ModuleFile *&File); 277 278 /// \brief View the graphviz representation of the module graph. 279 void viewGraph(); 280}; 281 282} } // end namespace clang::serialization 283 284#endif 285