ModuleManager.h revision 87e2cfcec7231daaa3f367dc32df74b411251e46
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The LLVM Compiler Infrastructure 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file is distributed under the University of Illinois Open Source 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// License. See LICENSE.TXT for details. 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 8868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)//===----------------------------------------------------------------------===// 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file defines the ModuleManager class, which manages a set of loaded 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// modules for the ASTReader. 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//===----------------------------------------------------------------------===// 14c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 162a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 172a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "clang/Serialization/Module.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "clang/Basic/FileManager.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "llvm/ADT/DenseMap.h" 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace clang { 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace serialization { 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/// \brief Manages the set of modules loaded by an AST reader. 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class ModuleManager { 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief The chain of AST files. The first entry is the one named by the 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// user, the last one is the one that doesn't depend on anything further. 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm::SmallVector<ModuleFile*, 2> Chain; 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief All loaded modules, indexed by name. 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief FileManager that handles translating between filenames and 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// FileEntry *. 372a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) FileManager &FileMgr; 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief A lookup of in-memory (virtual file) buffers 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)public: 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 46a93a17c8d99d686bd4a1511e5504e5e6cc9fcadfTorne (Richard Coles) typedef std::pair<uint32_t, StringRef> ModuleOffset; 47c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit ModuleManager(FileManager &FileMgr); 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ~ModuleManager(); 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Forward iterator to traverse all loaded modules. This is reverse 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// source-order. 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ModuleIterator begin() { return Chain.begin(); } 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Forward iterator end-point to traverse all loaded modules 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ModuleIterator end() { return Chain.end(); } 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Const forward iterator to traverse all loaded modules. This is 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// in reverse source-order. 592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ModuleConstIterator begin() const { return Chain.begin(); } 602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \brief Const forward iterator end-point to traverse all loaded modules 61c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleConstIterator end() const { return Chain.end(); } 62c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 63c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Reverse iterator to traverse all loaded modules. This is in 64c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// source order. 65c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleReverseIterator rbegin() { return Chain.rbegin(); } 66c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Reverse iterator end-point to traverse all loaded modules. 67c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleReverseIterator rend() { return Chain.rend(); } 68c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 69c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Returns the primary module associated with the manager, that is, 70c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// the first module loaded 71c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleFile &getPrimaryModule() { return *Chain[0]; } 72c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 73c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Returns the primary module associated with the manager, that is, 74c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// the first module loaded. 75c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleFile &getPrimaryModule() const { return *Chain[0]; } 76c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 77c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Returns the module associated with the given index 78c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 79c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 80c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Returns the module associated with the given name 81c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) ModuleFile *lookup(StringRef Name); 82c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 83c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Returns the in-memory (virtual file) buffer with the given name 84c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) llvm::MemoryBuffer *lookupBuffer(StringRef Name); 85c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) 86c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Number of modules loaded 87c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) unsigned size() const { return Chain.size(); } 88c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// \brief Attempts to create a new module and add it to the list of known 89c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// modules. 90c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles) /// 912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \param FileName The file name of the module to be loaded. 922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param Type The kind of module being loaded. 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param ImportLoc The location at which the module is imported. 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param ImportedBy The module that is importing this module, or NULL if 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// this module is imported directly by the user. 992a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 1002a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \param Generation The generation in which this module was loaded. 1012a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \param ErrorStr Will be set to a non-empty string if any errors occurred 1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// while trying to load the module. 1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 1052a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \return A pointer to the module that corresponds to this file name, 1062a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// and a boolean indicating whether the module was newly added. 1072a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) std::pair<ModuleFile *, bool> 1082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc, 1092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) ModuleFile *ImportedBy, unsigned Generation, 1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string &ErrorStr); 1112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Remove the given set of modules. 1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void removeModules(ModuleIterator first, ModuleIterator last); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Add an in-memory buffer the list of known buffers 1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Visit each of the modules. 1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// This routine visits each of the modules, starting with the 12190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) /// "root" modules that no other loaded modules depend on, and 1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// proceeding to the leaf modules, visiting each module only once 1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// during the traversal. 1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// This traversal is intended to support various "lookup" 1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// operations that can find data in any of the loaded modules. 1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param Visitor A visitor function that will be invoked with each 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// module and the given user data pointer. The return value must be 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// convertible to bool; when false, the visitation continues to 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// modules that the current module depends on. When true, the 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// visitation skips any modules that the current module depends on. 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param UserData User data associated with the visitor object, which 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// will be passed along to the visitor. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData); 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \brief Visit each of the modules with a depth-first traversal. 1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// This routine visits each of the modules known to the module 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// manager using a depth-first search, starting with the first 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// loaded module. The traversal invokes the callback both before 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// traversing the children (preorder traversal) and after 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// traversing the children (postorder traversal). 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// \param Visitor A visitor function that will be invoked with each 1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// module and given a \c Preorder flag that indicates whether we're 1485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// visiting the module before or after visiting its children. The 1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) /// visitor may return true at any time to abort the depth-first 1502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// visitation. 1512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// 1522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \param UserData User data ssociated with the visitor object, 1532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// which will be passed along to the user. 1542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 1552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void *UserData), 1562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void *UserData); 1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) /// \brief View the graphviz representation of the module graph. 1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) void viewGraph(); 1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)}; 1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)} } // end namespace clang::serialization 1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 1642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#endif 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)