1226586Sdim//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 2226586Sdim// 3226586Sdim// The LLVM Compiler Infrastructure 4226586Sdim// 5226586Sdim// This file is distributed under the University of Illinois Open Source 6226586Sdim// License. See LICENSE.TXT for details. 7226586Sdim// 8226586Sdim//===----------------------------------------------------------------------===// 9226586Sdim// 10226586Sdim// This file defines the ModuleManager class, which manages a set of loaded 11226586Sdim// modules for the ASTReader. 12226586Sdim// 13226586Sdim//===----------------------------------------------------------------------===// 14226586Sdim 15226586Sdim#ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 16226586Sdim#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 17226586Sdim 18249423Sdim#include "clang/Basic/FileManager.h" 19226586Sdim#include "clang/Serialization/Module.h" 20226586Sdim#include "llvm/ADT/DenseMap.h" 21226586Sdim 22226586Sdimnamespace clang { 23226586Sdim 24249423Sdimclass GlobalModuleIndex; 25249423Sdimclass ModuleMap; 26249423Sdim 27226586Sdimnamespace serialization { 28249423Sdim 29226586Sdim/// \brief Manages the set of modules loaded by an AST reader. 30226586Sdimclass ModuleManager { 31226586Sdim /// \brief The chain of AST files. The first entry is the one named by the 32226586Sdim /// user, the last one is the one that doesn't depend on anything further. 33249423Sdim SmallVector<ModuleFile *, 2> Chain; 34226586Sdim 35226586Sdim /// \brief All loaded modules, indexed by name. 36234353Sdim llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 37226586Sdim 38226586Sdim /// \brief FileManager that handles translating between filenames and 39226586Sdim /// FileEntry *. 40243830Sdim FileManager &FileMgr; 41226586Sdim 42226586Sdim /// \brief A lookup of in-memory (virtual file) buffers 43226586Sdim llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 44249423Sdim 45249423Sdim /// \brief The visitation order. 46249423Sdim SmallVector<ModuleFile *, 4> VisitOrder; 47249423Sdim 48249423Sdim /// \brief The list of module files that both we and the global module index 49249423Sdim /// know about. 50249423Sdim /// 51249423Sdim /// Either the global index or the module manager may have modules that the 52249423Sdim /// other does not know about, because the global index can be out-of-date 53249423Sdim /// (in which case the module manager could have modules it does not) and 54249423Sdim /// this particular translation unit might not have loaded all of the modules 55249423Sdim /// known to the global index. 56249423Sdim SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex; 57249423Sdim 58249423Sdim /// \brief The global module index, if one is attached. 59249423Sdim /// 60249423Sdim /// The global module index will actually be owned by the ASTReader; this is 61249423Sdim /// just an non-owning pointer. 62249423Sdim GlobalModuleIndex *GlobalIndex; 63249423Sdim 64249423Sdim /// \brief State used by the "visit" operation to avoid malloc traffic in 65249423Sdim /// calls to visit(). 66249423Sdim struct VisitState { 67249423Sdim explicit VisitState(unsigned N) 68249423Sdim : VisitNumber(N, 0), NextVisitNumber(1), NextState(0) 69249423Sdim { 70249423Sdim Stack.reserve(N); 71249423Sdim } 72249423Sdim 73249423Sdim ~VisitState() { 74249423Sdim delete NextState; 75249423Sdim } 76249423Sdim 77249423Sdim /// \brief The stack used when marking the imports of a particular module 78249423Sdim /// as not-to-be-visited. 79249423Sdim SmallVector<ModuleFile *, 4> Stack; 80249423Sdim 81249423Sdim /// \brief The visit number of each module file, which indicates when 82249423Sdim /// this module file was last visited. 83249423Sdim SmallVector<unsigned, 4> VisitNumber; 84249423Sdim 85249423Sdim /// \brief The next visit number to use to mark visited module files. 86249423Sdim unsigned NextVisitNumber; 87249423Sdim 88249423Sdim /// \brief The next visit state. 89249423Sdim VisitState *NextState; 90249423Sdim }; 91249423Sdim 92249423Sdim /// \brief The first visit() state in the chain. 93249423Sdim VisitState *FirstVisitState; 94249423Sdim 95249423Sdim VisitState *allocateVisitState(); 96249423Sdim void returnVisitState(VisitState *State); 97249423Sdim 98226586Sdimpublic: 99263508Sdim typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator; 100263508Sdim typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator; 101263508Sdim typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator; 102226586Sdim typedef std::pair<uint32_t, StringRef> ModuleOffset; 103226586Sdim 104243830Sdim explicit ModuleManager(FileManager &FileMgr); 105226586Sdim ~ModuleManager(); 106226586Sdim 107226586Sdim /// \brief Forward iterator to traverse all loaded modules. This is reverse 108226586Sdim /// source-order. 109226586Sdim ModuleIterator begin() { return Chain.begin(); } 110226586Sdim /// \brief Forward iterator end-point to traverse all loaded modules 111226586Sdim ModuleIterator end() { return Chain.end(); } 112226586Sdim 113226586Sdim /// \brief Const forward iterator to traverse all loaded modules. This is 114226586Sdim /// in reverse source-order. 115226586Sdim ModuleConstIterator begin() const { return Chain.begin(); } 116226586Sdim /// \brief Const forward iterator end-point to traverse all loaded modules 117226586Sdim ModuleConstIterator end() const { return Chain.end(); } 118226586Sdim 119226586Sdim /// \brief Reverse iterator to traverse all loaded modules. This is in 120226586Sdim /// source order. 121226586Sdim ModuleReverseIterator rbegin() { return Chain.rbegin(); } 122226586Sdim /// \brief Reverse iterator end-point to traverse all loaded modules. 123226586Sdim ModuleReverseIterator rend() { return Chain.rend(); } 124226586Sdim 125226586Sdim /// \brief Returns the primary module associated with the manager, that is, 126226586Sdim /// the first module loaded 127234353Sdim ModuleFile &getPrimaryModule() { return *Chain[0]; } 128226586Sdim 129226586Sdim /// \brief Returns the primary module associated with the manager, that is, 130226586Sdim /// the first module loaded. 131234353Sdim ModuleFile &getPrimaryModule() const { return *Chain[0]; } 132226586Sdim 133226586Sdim /// \brief Returns the module associated with the given index 134234353Sdim ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 135226586Sdim 136226586Sdim /// \brief Returns the module associated with the given name 137234353Sdim ModuleFile *lookup(StringRef Name); 138249423Sdim 139249423Sdim /// \brief Returns the module associated with the given module file. 140249423Sdim ModuleFile *lookup(const FileEntry *File); 141249423Sdim 142226586Sdim /// \brief Returns the in-memory (virtual file) buffer with the given name 143226586Sdim llvm::MemoryBuffer *lookupBuffer(StringRef Name); 144226586Sdim 145226586Sdim /// \brief Number of modules loaded 146226586Sdim unsigned size() const { return Chain.size(); } 147249423Sdim 148249423Sdim /// \brief The result of attempting to add a new module. 149249423Sdim enum AddModuleResult { 150249423Sdim /// \brief The module file had already been loaded. 151249423Sdim AlreadyLoaded, 152249423Sdim /// \brief The module file was just loaded in response to this call. 153249423Sdim NewlyLoaded, 154249423Sdim /// \brief The module file is missing. 155249423Sdim Missing, 156249423Sdim /// \brief The module file is out-of-date. 157249423Sdim OutOfDate 158249423Sdim }; 159249423Sdim 160226586Sdim /// \brief Attempts to create a new module and add it to the list of known 161226586Sdim /// modules. 162226586Sdim /// 163226586Sdim /// \param FileName The file name of the module to be loaded. 164226586Sdim /// 165226586Sdim /// \param Type The kind of module being loaded. 166226586Sdim /// 167249423Sdim /// \param ImportLoc The location at which the module is imported. 168249423Sdim /// 169226586Sdim /// \param ImportedBy The module that is importing this module, or NULL if 170226586Sdim /// this module is imported directly by the user. 171226586Sdim /// 172234353Sdim /// \param Generation The generation in which this module was loaded. 173234353Sdim /// 174249423Sdim /// \param ExpectedSize The expected size of the module file, used for 175249423Sdim /// validation. This will be zero if unknown. 176249423Sdim /// 177249423Sdim /// \param ExpectedModTime The expected modification time of the module 178249423Sdim /// file, used for validation. This will be zero if unknown. 179249423Sdim /// 180249423Sdim /// \param Module A pointer to the module file if the module was successfully 181249423Sdim /// loaded. 182249423Sdim /// 183226586Sdim /// \param ErrorStr Will be set to a non-empty string if any errors occurred 184226586Sdim /// while trying to load the module. 185226586Sdim /// 186226586Sdim /// \return A pointer to the module that corresponds to this file name, 187249423Sdim /// and a value indicating whether the module was loaded. 188249423Sdim AddModuleResult addModule(StringRef FileName, ModuleKind Type, 189249423Sdim SourceLocation ImportLoc, 190249423Sdim ModuleFile *ImportedBy, unsigned Generation, 191249423Sdim off_t ExpectedSize, time_t ExpectedModTime, 192249423Sdim ModuleFile *&Module, 193249423Sdim std::string &ErrorStr); 194243830Sdim 195243830Sdim /// \brief Remove the given set of modules. 196249423Sdim void removeModules(ModuleIterator first, ModuleIterator last, 197249423Sdim ModuleMap *modMap); 198243830Sdim 199226586Sdim /// \brief Add an in-memory buffer the list of known buffers 200226586Sdim void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 201249423Sdim 202249423Sdim /// \brief Set the global module index. 203249423Sdim void setGlobalIndex(GlobalModuleIndex *Index); 204249423Sdim 205249423Sdim /// \brief Notification from the AST reader that the given module file 206249423Sdim /// has been "accepted", and will not (can not) be unloaded. 207249423Sdim void moduleFileAccepted(ModuleFile *MF); 208249423Sdim 209226586Sdim /// \brief Visit each of the modules. 210226586Sdim /// 211226586Sdim /// This routine visits each of the modules, starting with the 212226586Sdim /// "root" modules that no other loaded modules depend on, and 213226586Sdim /// proceeding to the leaf modules, visiting each module only once 214226586Sdim /// during the traversal. 215226586Sdim /// 216226586Sdim /// This traversal is intended to support various "lookup" 217226586Sdim /// operations that can find data in any of the loaded modules. 218226586Sdim /// 219226586Sdim /// \param Visitor A visitor function that will be invoked with each 220226586Sdim /// module and the given user data pointer. The return value must be 221226586Sdim /// convertible to bool; when false, the visitation continues to 222226586Sdim /// modules that the current module depends on. When true, the 223226586Sdim /// visitation skips any modules that the current module depends on. 224226586Sdim /// 225226586Sdim /// \param UserData User data associated with the visitor object, which 226226586Sdim /// will be passed along to the visitor. 227249423Sdim /// 228249423Sdim /// \param ModuleFilesHit If non-NULL, contains the set of module files 229249423Sdim /// that we know we need to visit because the global module index told us to. 230249423Sdim /// Any module that is known to both the global module index and the module 231249423Sdim /// manager that is *not* in this set can be skipped. 232249423Sdim void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData, 233249423Sdim llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit = 0); 234226586Sdim 235226586Sdim /// \brief Visit each of the modules with a depth-first traversal. 236226586Sdim /// 237226586Sdim /// This routine visits each of the modules known to the module 238226586Sdim /// manager using a depth-first search, starting with the first 239226586Sdim /// loaded module. The traversal invokes the callback both before 240226586Sdim /// traversing the children (preorder traversal) and after 241226586Sdim /// traversing the children (postorder traversal). 242226586Sdim /// 243226586Sdim /// \param Visitor A visitor function that will be invoked with each 244226586Sdim /// module and given a \c Preorder flag that indicates whether we're 245226586Sdim /// visiting the module before or after visiting its children. The 246226586Sdim /// visitor may return true at any time to abort the depth-first 247226586Sdim /// visitation. 248226586Sdim /// 249226586Sdim /// \param UserData User data ssociated with the visitor object, 250226586Sdim /// which will be passed along to the user. 251234353Sdim void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 252226586Sdim void *UserData), 253226586Sdim void *UserData); 254249423Sdim 255249423Sdim /// \brief Attempt to resolve the given module file name to a file entry. 256249423Sdim /// 257249423Sdim /// \param FileName The name of the module file. 258249423Sdim /// 259249423Sdim /// \param ExpectedSize The size that the module file is expected to have. 260249423Sdim /// If the actual size differs, the resolver should return \c true. 261249423Sdim /// 262249423Sdim /// \param ExpectedModTime The modification time that the module file is 263249423Sdim /// expected to have. If the actual modification time differs, the resolver 264249423Sdim /// should return \c true. 265249423Sdim /// 266249423Sdim /// \param File Will be set to the file if there is one, or null 267249423Sdim /// otherwise. 268249423Sdim /// 269249423Sdim /// \returns True if a file exists but does not meet the size/ 270249423Sdim /// modification time criteria, false if the file is either available and 271249423Sdim /// suitable, or is missing. 272249423Sdim bool lookupModuleFile(StringRef FileName, 273249423Sdim off_t ExpectedSize, 274249423Sdim time_t ExpectedModTime, 275249423Sdim const FileEntry *&File); 276249423Sdim 277226586Sdim /// \brief View the graphviz representation of the module graph. 278226586Sdim void viewGraph(); 279226586Sdim}; 280226586Sdim 281226586Sdim} } // end namespace clang::serialization 282226586Sdim 283226586Sdim#endif 284