ModuleManager.h revision 234353
1299425Smm//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 2299425Smm// 3299425Smm// The LLVM Compiler Infrastructure 4299425Smm// 5299425Smm// This file is distributed under the University of Illinois Open Source 6299425Smm// License. See LICENSE.TXT for details. 7299425Smm// 8299425Smm//===----------------------------------------------------------------------===// 9299425Smm// 10299425Smm// This file defines the ModuleManager class, which manages a set of loaded 11299425Smm// modules for the ASTReader. 12299425Smm// 13299425Smm//===----------------------------------------------------------------------===// 14299425Smm 15299425Smm#ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 16299425Smm#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 17299425Smm 18299425Smm#include "clang/Serialization/Module.h" 19299425Smm#include "clang/Basic/FileManager.h" 20299425Smm#include "llvm/ADT/DenseMap.h" 21299425Smm 22299425Smmnamespace clang { 23299425Smm 24299425Smmnamespace serialization { 25299425Smm 26299425Smm/// \brief Manages the set of modules loaded by an AST reader. 27299425Smmclass ModuleManager { 28299425Smm /// \brief The chain of AST files. The first entry is the one named by the 29299425Smm /// user, the last one is the one that doesn't depend on anything further. 30299425Smm llvm::SmallVector<ModuleFile*, 2> Chain; 31299425Smm 32299425Smm /// \brief All loaded modules, indexed by name. 33299425Smm llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 34299425Smm 35299425Smm /// \brief FileManager that handles translating between filenames and 36299425Smm /// FileEntry *. 37299425Smm FileManager FileMgr; 38299425Smm 39299425Smm /// \brief A lookup of in-memory (virtual file) buffers 40299425Smm llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 41299425Smm 42299425Smmpublic: 43299425Smm typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 44299425Smm typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 45299425Smm typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 46299425Smm typedef std::pair<uint32_t, StringRef> ModuleOffset; 47299425Smm 48299425Smm ModuleManager(const FileSystemOptions &FSO); 49299425Smm ~ModuleManager(); 50299425Smm 51299425Smm /// \brief Forward iterator to traverse all loaded modules. This is reverse 52299425Smm /// source-order. 53299425Smm ModuleIterator begin() { return Chain.begin(); } 54299425Smm /// \brief Forward iterator end-point to traverse all loaded modules 55299425Smm ModuleIterator end() { return Chain.end(); } 56299425Smm 57299425Smm /// \brief Const forward iterator to traverse all loaded modules. This is 58299425Smm /// in reverse source-order. 59299425Smm ModuleConstIterator begin() const { return Chain.begin(); } 60358088Smm /// \brief Const forward iterator end-point to traverse all loaded modules 61358088Smm ModuleConstIterator end() const { return Chain.end(); } 62358088Smm 63358088Smm /// \brief Reverse iterator to traverse all loaded modules. This is in 64338795Smm /// source order. 65358088Smm ModuleReverseIterator rbegin() { return Chain.rbegin(); } 66338795Smm /// \brief Reverse iterator end-point to traverse all loaded modules. 67299425Smm ModuleReverseIterator rend() { return Chain.rend(); } 68299425Smm 69299425Smm /// \brief Returns the primary module associated with the manager, that is, 70299425Smm /// the first module loaded 71299425Smm ModuleFile &getPrimaryModule() { return *Chain[0]; } 72299425Smm 73299425Smm /// \brief Returns the primary module associated with the manager, that is, 74299425Smm /// the first module loaded. 75299425Smm ModuleFile &getPrimaryModule() const { return *Chain[0]; } 76299425Smm 77299425Smm /// \brief Returns the module associated with the given index 78299425Smm ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 79299425Smm 80299425Smm /// \brief Returns the module associated with the given name 81299425Smm ModuleFile *lookup(StringRef Name); 82299425Smm 83299425Smm /// \brief Returns the in-memory (virtual file) buffer with the given name 84299425Smm llvm::MemoryBuffer *lookupBuffer(StringRef Name); 85299425Smm 86299425Smm /// \brief Number of modules loaded 87299425Smm unsigned size() const { return Chain.size(); } 88299425Smm /// \brief Attempts to create a new module and add it to the list of known 89299425Smm /// modules. 90299425Smm /// 91299425Smm /// \param FileName The file name of the module to be loaded. 92299425Smm /// 93299425Smm /// \param Type The kind of module being loaded. 94299425Smm /// 95299425Smm /// \param ImportedBy The module that is importing this module, or NULL if 96299425Smm /// this module is imported directly by the user. 97299425Smm /// 98299425Smm /// \param Generation The generation in which this module was loaded. 99299425Smm /// 100299425Smm /// \param ErrorStr Will be set to a non-empty string if any errors occurred 101299425Smm /// while trying to load the module. 102299425Smm /// 103299425Smm /// \return A pointer to the module that corresponds to this file name, 104299425Smm /// and a boolean indicating whether the module was newly added. 105299425Smm std::pair<ModuleFile *, bool> 106299425Smm addModule(StringRef FileName, ModuleKind Type, ModuleFile *ImportedBy, 107299425Smm unsigned Generation, std::string &ErrorStr); 108299425Smm 109299425Smm /// \brief Add an in-memory buffer the list of known buffers 110299425Smm void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 111299425Smm 112299425Smm /// \brief Visit each of the modules. 113299425Smm /// 114299425Smm /// This routine visits each of the modules, starting with the 115299425Smm /// "root" modules that no other loaded modules depend on, and 116299425Smm /// proceeding to the leaf modules, visiting each module only once 117299425Smm /// during the traversal. 118299425Smm /// 119299425Smm /// This traversal is intended to support various "lookup" 120299425Smm /// operations that can find data in any of the loaded modules. 121299425Smm /// 122299425Smm /// \param Visitor A visitor function that will be invoked with each 123299425Smm /// module and the given user data pointer. The return value must be 124299425Smm /// convertible to bool; when false, the visitation continues to 125299425Smm /// modules that the current module depends on. When true, the 126299425Smm /// visitation skips any modules that the current module depends on. 127299425Smm /// 128299425Smm /// \param UserData User data associated with the visitor object, which 129299425Smm /// will be passed along to the visitor. 130299425Smm void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData); 131299425Smm 132299425Smm /// \brief Visit each of the modules with a depth-first traversal. 133299425Smm /// 134299425Smm /// This routine visits each of the modules known to the module 135299425Smm /// manager using a depth-first search, starting with the first 136299425Smm /// loaded module. The traversal invokes the callback both before 137299425Smm /// traversing the children (preorder traversal) and after 138299425Smm /// traversing the children (postorder traversal). 139299425Smm /// 140299425Smm /// \param Visitor A visitor function that will be invoked with each 141299425Smm /// module and given a \c Preorder flag that indicates whether we're 142299425Smm /// visiting the module before or after visiting its children. The 143299425Smm /// visitor may return true at any time to abort the depth-first 144299425Smm /// visitation. 145299425Smm /// 146299425Smm /// \param UserData User data ssociated with the visitor object, 147299425Smm /// which will be passed along to the user. 148299425Smm void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 149299425Smm void *UserData), 150299425Smm void *UserData); 151299425Smm 152299425Smm /// \brief View the graphviz representation of the module graph. 153299425Smm void viewGraph(); 154299425Smm}; 155299425Smm 156299425Smm} } // end namespace clang::serialization 157299425Smm 158299425Smm#endif 159299425Smm