ModuleManager.h revision 234353
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 18226586Sdim#include "clang/Serialization/Module.h" 19226586Sdim#include "clang/Basic/FileManager.h" 20226586Sdim#include "llvm/ADT/DenseMap.h" 21226586Sdim 22226586Sdimnamespace clang { 23226586Sdim 24226586Sdimnamespace serialization { 25226586Sdim 26226586Sdim/// \brief Manages the set of modules loaded by an AST reader. 27226586Sdimclass ModuleManager { 28226586Sdim /// \brief The chain of AST files. The first entry is the one named by the 29226586Sdim /// user, the last one is the one that doesn't depend on anything further. 30234353Sdim llvm::SmallVector<ModuleFile*, 2> Chain; 31226586Sdim 32226586Sdim /// \brief All loaded modules, indexed by name. 33234353Sdim llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 34226586Sdim 35226586Sdim /// \brief FileManager that handles translating between filenames and 36226586Sdim /// FileEntry *. 37226586Sdim FileManager FileMgr; 38226586Sdim 39226586Sdim /// \brief A lookup of in-memory (virtual file) buffers 40226586Sdim llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 41226586Sdim 42226586Sdimpublic: 43234353Sdim typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 44234353Sdim typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 45234353Sdim typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 46226586Sdim typedef std::pair<uint32_t, StringRef> ModuleOffset; 47226586Sdim 48226586Sdim ModuleManager(const FileSystemOptions &FSO); 49226586Sdim ~ModuleManager(); 50226586Sdim 51226586Sdim /// \brief Forward iterator to traverse all loaded modules. This is reverse 52226586Sdim /// source-order. 53226586Sdim ModuleIterator begin() { return Chain.begin(); } 54226586Sdim /// \brief Forward iterator end-point to traverse all loaded modules 55226586Sdim ModuleIterator end() { return Chain.end(); } 56226586Sdim 57226586Sdim /// \brief Const forward iterator to traverse all loaded modules. This is 58226586Sdim /// in reverse source-order. 59226586Sdim ModuleConstIterator begin() const { return Chain.begin(); } 60226586Sdim /// \brief Const forward iterator end-point to traverse all loaded modules 61226586Sdim ModuleConstIterator end() const { return Chain.end(); } 62226586Sdim 63226586Sdim /// \brief Reverse iterator to traverse all loaded modules. This is in 64226586Sdim /// source order. 65226586Sdim ModuleReverseIterator rbegin() { return Chain.rbegin(); } 66226586Sdim /// \brief Reverse iterator end-point to traverse all loaded modules. 67226586Sdim ModuleReverseIterator rend() { return Chain.rend(); } 68226586Sdim 69226586Sdim /// \brief Returns the primary module associated with the manager, that is, 70226586Sdim /// the first module loaded 71234353Sdim ModuleFile &getPrimaryModule() { return *Chain[0]; } 72226586Sdim 73226586Sdim /// \brief Returns the primary module associated with the manager, that is, 74226586Sdim /// the first module loaded. 75234353Sdim ModuleFile &getPrimaryModule() const { return *Chain[0]; } 76226586Sdim 77226586Sdim /// \brief Returns the module associated with the given index 78234353Sdim ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 79226586Sdim 80226586Sdim /// \brief Returns the module associated with the given name 81234353Sdim ModuleFile *lookup(StringRef Name); 82226586Sdim 83226586Sdim /// \brief Returns the in-memory (virtual file) buffer with the given name 84226586Sdim llvm::MemoryBuffer *lookupBuffer(StringRef Name); 85226586Sdim 86226586Sdim /// \brief Number of modules loaded 87226586Sdim unsigned size() const { return Chain.size(); } 88226586Sdim /// \brief Attempts to create a new module and add it to the list of known 89226586Sdim /// modules. 90226586Sdim /// 91226586Sdim /// \param FileName The file name of the module to be loaded. 92226586Sdim /// 93226586Sdim /// \param Type The kind of module being loaded. 94226586Sdim /// 95226586Sdim /// \param ImportedBy The module that is importing this module, or NULL if 96226586Sdim /// this module is imported directly by the user. 97226586Sdim /// 98234353Sdim /// \param Generation The generation in which this module was loaded. 99234353Sdim /// 100226586Sdim /// \param ErrorStr Will be set to a non-empty string if any errors occurred 101226586Sdim /// while trying to load the module. 102226586Sdim /// 103226586Sdim /// \return A pointer to the module that corresponds to this file name, 104226586Sdim /// and a boolean indicating whether the module was newly added. 105234353Sdim std::pair<ModuleFile *, bool> 106234353Sdim addModule(StringRef FileName, ModuleKind Type, ModuleFile *ImportedBy, 107234353Sdim unsigned Generation, std::string &ErrorStr); 108226586Sdim 109226586Sdim /// \brief Add an in-memory buffer the list of known buffers 110226586Sdim void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 111226586Sdim 112226586Sdim /// \brief Visit each of the modules. 113226586Sdim /// 114226586Sdim /// This routine visits each of the modules, starting with the 115226586Sdim /// "root" modules that no other loaded modules depend on, and 116226586Sdim /// proceeding to the leaf modules, visiting each module only once 117226586Sdim /// during the traversal. 118226586Sdim /// 119226586Sdim /// This traversal is intended to support various "lookup" 120226586Sdim /// operations that can find data in any of the loaded modules. 121226586Sdim /// 122226586Sdim /// \param Visitor A visitor function that will be invoked with each 123226586Sdim /// module and the given user data pointer. The return value must be 124226586Sdim /// convertible to bool; when false, the visitation continues to 125226586Sdim /// modules that the current module depends on. When true, the 126226586Sdim /// visitation skips any modules that the current module depends on. 127226586Sdim /// 128226586Sdim /// \param UserData User data associated with the visitor object, which 129226586Sdim /// will be passed along to the visitor. 130234353Sdim void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData); 131226586Sdim 132226586Sdim /// \brief Visit each of the modules with a depth-first traversal. 133226586Sdim /// 134226586Sdim /// This routine visits each of the modules known to the module 135226586Sdim /// manager using a depth-first search, starting with the first 136226586Sdim /// loaded module. The traversal invokes the callback both before 137226586Sdim /// traversing the children (preorder traversal) and after 138226586Sdim /// traversing the children (postorder traversal). 139226586Sdim /// 140226586Sdim /// \param Visitor A visitor function that will be invoked with each 141226586Sdim /// module and given a \c Preorder flag that indicates whether we're 142226586Sdim /// visiting the module before or after visiting its children. The 143226586Sdim /// visitor may return true at any time to abort the depth-first 144226586Sdim /// visitation. 145226586Sdim /// 146226586Sdim /// \param UserData User data ssociated with the visitor object, 147226586Sdim /// which will be passed along to the user. 148234353Sdim void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 149226586Sdim void *UserData), 150226586Sdim void *UserData); 151226586Sdim 152226586Sdim /// \brief View the graphviz representation of the module graph. 153226586Sdim void viewGraph(); 154226586Sdim}; 155226586Sdim 156226586Sdim} } // end namespace clang::serialization 157226586Sdim 158226586Sdim#endif 159