ModuleManager.h revision 87e2cfcec7231daaa3f367dc32df74b411251e46
198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// 398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// The LLVM Compiler Infrastructure 498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// 598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// This file is distributed under the University of Illinois Open Source 698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// License. See LICENSE.TXT for details. 798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// 898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===// 998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// 1098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// This file defines the ModuleManager class, which manages a set of loaded 1198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// modules for the ASTReader. 1298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// 1398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===// 1498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 1598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 1698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 1798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 1898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "clang/Serialization/Module.h" 1998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "clang/Basic/FileManager.h" 2098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "llvm/ADT/DenseMap.h" 2198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 2298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregornamespace clang { 2398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 2498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregornamespace serialization { 2598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 2698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor/// \brief Manages the set of modules loaded by an AST reader. 2798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorclass ModuleManager { 2898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief The chain of AST files. The first entry is the one named by the 2998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// user, the last one is the one that doesn't depend on anything further. 301a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor llvm::SmallVector<ModuleFile*, 2> Chain; 3198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 3298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief All loaded modules, indexed by name. 331a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 3498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 3598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief FileManager that handles translating between filenames and 3698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// FileEntry *. 37d64c26f6676eef69d1713f353ca8a3c2fe963f17Argyrios Kyrtzidis FileManager &FileMgr; 3898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 3998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief A lookup of in-memory (virtual file) buffers 4098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 4198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 4298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorpublic: 431a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 441a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 451a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 4698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor typedef std::pair<uint32_t, StringRef> ModuleOffset; 4798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 48d64c26f6676eef69d1713f353ca8a3c2fe963f17Argyrios Kyrtzidis explicit ModuleManager(FileManager &FileMgr); 4998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ~ModuleManager(); 5098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 5198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Forward iterator to traverse all loaded modules. This is reverse 5298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// source-order. 5398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleIterator begin() { return Chain.begin(); } 5498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Forward iterator end-point to traverse all loaded modules 5598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleIterator end() { return Chain.end(); } 5698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 5798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Const forward iterator to traverse all loaded modules. This is 5898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// in reverse source-order. 5998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleConstIterator begin() const { return Chain.begin(); } 6098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Const forward iterator end-point to traverse all loaded modules 6198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleConstIterator end() const { return Chain.end(); } 6298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 6398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Reverse iterator to traverse all loaded modules. This is in 6498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// source order. 6598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleReverseIterator rbegin() { return Chain.rbegin(); } 6698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Reverse iterator end-point to traverse all loaded modules. 6798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor ModuleReverseIterator rend() { return Chain.rend(); } 6898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 6998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Returns the primary module associated with the manager, that is, 7098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// the first module loaded 711a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor ModuleFile &getPrimaryModule() { return *Chain[0]; } 7298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 7398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Returns the primary module associated with the manager, that is, 7498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// the first module loaded. 751a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor ModuleFile &getPrimaryModule() const { return *Chain[0]; } 7698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 7798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Returns the module associated with the given index 781a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 7998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 8098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Returns the module associated with the given name 811a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor ModuleFile *lookup(StringRef Name); 8298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 8398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Returns the in-memory (virtual file) buffer with the given name 8498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor llvm::MemoryBuffer *lookupBuffer(StringRef Name); 8598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 8698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Number of modules loaded 8798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor unsigned size() const { return Chain.size(); } 8898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Attempts to create a new module and add it to the list of known 8998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// modules. 9098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 9198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param FileName The file name of the module to be loaded. 9298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 9398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param Type The kind of module being loaded. 9498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 9587e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor /// \param ImportLoc The location at which the module is imported. 9687e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor /// 9798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param ImportedBy The module that is importing this module, or NULL if 9898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// this module is imported directly by the user. 9998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 100057df20b3107cef764052d271c89b8591b98b3ceDouglas Gregor /// \param Generation The generation in which this module was loaded. 101057df20b3107cef764052d271c89b8591b98b3ceDouglas Gregor /// 10298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param ErrorStr Will be set to a non-empty string if any errors occurred 10398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// while trying to load the module. 10498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 10598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \return A pointer to the module that corresponds to this file name, 10698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// and a boolean indicating whether the module was newly added. 1071a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor std::pair<ModuleFile *, bool> 10887e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc, 10987e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor ModuleFile *ImportedBy, unsigned Generation, 11087e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor std::string &ErrorStr); 1117cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor 1127cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor /// \brief Remove the given set of modules. 1137cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor void removeModules(ModuleIterator first, ModuleIterator last); 1147cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor 11598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Add an in-memory buffer the list of known buffers 11698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 11798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 11898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Visit each of the modules. 11998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 12098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// This routine visits each of the modules, starting with the 12198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// "root" modules that no other loaded modules depend on, and 12298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// proceeding to the leaf modules, visiting each module only once 12398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// during the traversal. 12498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 12598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// This traversal is intended to support various "lookup" 12698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// operations that can find data in any of the loaded modules. 12798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 12898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param Visitor A visitor function that will be invoked with each 12998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// module and the given user data pointer. The return value must be 13098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// convertible to bool; when false, the visitation continues to 13198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// modules that the current module depends on. When true, the 13298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// visitation skips any modules that the current module depends on. 13398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 13498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param UserData User data associated with the visitor object, which 13598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// will be passed along to the visitor. 1361a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData); 13798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 13898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \brief Visit each of the modules with a depth-first traversal. 13998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 14098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// This routine visits each of the modules known to the module 14198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// manager using a depth-first search, starting with the first 14298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// loaded module. The traversal invokes the callback both before 14398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// traversing the children (preorder traversal) and after 14498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// traversing the children (postorder traversal). 14598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 14698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param Visitor A visitor function that will be invoked with each 14798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// module and given a \c Preorder flag that indicates whether we're 14898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// visiting the module before or after visiting its children. The 14998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// visitor may return true at any time to abort the depth-first 15098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// visitation. 15198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// 15298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// \param UserData User data ssociated with the visitor object, 15398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor /// which will be passed along to the user. 1541a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 15598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor void *UserData), 15698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor void *UserData); 1572492c89882b5c5ce03afb4704fee67b7eff8f5eeDouglas Gregor 1582492c89882b5c5ce03afb4704fee67b7eff8f5eeDouglas Gregor /// \brief View the graphviz representation of the module graph. 1592492c89882b5c5ce03afb4704fee67b7eff8f5eeDouglas Gregor void viewGraph(); 16098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor}; 16198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 16298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor} } // end namespace clang::serialization 16398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor 16498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#endif 165