clang API Documentation
00001 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the ModuleManager class, which manages a set of loaded 00011 // modules for the ASTReader. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 00016 #define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H 00017 00018 #include "clang/Serialization/Module.h" 00019 #include "clang/Basic/FileManager.h" 00020 #include "llvm/ADT/DenseMap.h" 00021 00022 namespace clang { 00023 00024 namespace serialization { 00025 00026 /// \brief Manages the set of modules loaded by an AST reader. 00027 class ModuleManager { 00028 /// \brief The chain of AST files. The first entry is the one named by the 00029 /// user, the last one is the one that doesn't depend on anything further. 00030 llvm::SmallVector<ModuleFile*, 2> Chain; 00031 00032 /// \brief All loaded modules, indexed by name. 00033 llvm::DenseMap<const FileEntry *, ModuleFile *> Modules; 00034 00035 /// \brief FileManager that handles translating between filenames and 00036 /// FileEntry *. 00037 FileManager FileMgr; 00038 00039 /// \brief A lookup of in-memory (virtual file) buffers 00040 llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers; 00041 00042 public: 00043 typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator; 00044 typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator; 00045 typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator; 00046 typedef std::pair<uint32_t, StringRef> ModuleOffset; 00047 00048 ModuleManager(const FileSystemOptions &FSO); 00049 ~ModuleManager(); 00050 00051 /// \brief Forward iterator to traverse all loaded modules. This is reverse 00052 /// source-order. 00053 ModuleIterator begin() { return Chain.begin(); } 00054 /// \brief Forward iterator end-point to traverse all loaded modules 00055 ModuleIterator end() { return Chain.end(); } 00056 00057 /// \brief Const forward iterator to traverse all loaded modules. This is 00058 /// in reverse source-order. 00059 ModuleConstIterator begin() const { return Chain.begin(); } 00060 /// \brief Const forward iterator end-point to traverse all loaded modules 00061 ModuleConstIterator end() const { return Chain.end(); } 00062 00063 /// \brief Reverse iterator to traverse all loaded modules. This is in 00064 /// source order. 00065 ModuleReverseIterator rbegin() { return Chain.rbegin(); } 00066 /// \brief Reverse iterator end-point to traverse all loaded modules. 00067 ModuleReverseIterator rend() { return Chain.rend(); } 00068 00069 /// \brief Returns the primary module associated with the manager, that is, 00070 /// the first module loaded 00071 ModuleFile &getPrimaryModule() { return *Chain[0]; } 00072 00073 /// \brief Returns the primary module associated with the manager, that is, 00074 /// the first module loaded. 00075 ModuleFile &getPrimaryModule() const { return *Chain[0]; } 00076 00077 /// \brief Returns the module associated with the given index 00078 ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; } 00079 00080 /// \brief Returns the module associated with the given name 00081 ModuleFile *lookup(StringRef Name); 00082 00083 /// \brief Returns the in-memory (virtual file) buffer with the given name 00084 llvm::MemoryBuffer *lookupBuffer(StringRef Name); 00085 00086 /// \brief Number of modules loaded 00087 unsigned size() const { return Chain.size(); } 00088 /// \brief Attempts to create a new module and add it to the list of known 00089 /// modules. 00090 /// 00091 /// \param FileName The file name of the module to be loaded. 00092 /// 00093 /// \param Type The kind of module being loaded. 00094 /// 00095 /// \param ImportedBy The module that is importing this module, or NULL if 00096 /// this module is imported directly by the user. 00097 /// 00098 /// \param Generation The generation in which this module was loaded. 00099 /// 00100 /// \param ErrorStr Will be set to a non-empty string if any errors occurred 00101 /// while trying to load the module. 00102 /// 00103 /// \return A pointer to the module that corresponds to this file name, 00104 /// and a boolean indicating whether the module was newly added. 00105 std::pair<ModuleFile *, bool> 00106 addModule(StringRef FileName, ModuleKind Type, ModuleFile *ImportedBy, 00107 unsigned Generation, std::string &ErrorStr); 00108 00109 /// \brief Add an in-memory buffer the list of known buffers 00110 void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer); 00111 00112 /// \brief Visit each of the modules. 00113 /// 00114 /// This routine visits each of the modules, starting with the 00115 /// "root" modules that no other loaded modules depend on, and 00116 /// proceeding to the leaf modules, visiting each module only once 00117 /// during the traversal. 00118 /// 00119 /// This traversal is intended to support various "lookup" 00120 /// operations that can find data in any of the loaded modules. 00121 /// 00122 /// \param Visitor A visitor function that will be invoked with each 00123 /// module and the given user data pointer. The return value must be 00124 /// convertible to bool; when false, the visitation continues to 00125 /// modules that the current module depends on. When true, the 00126 /// visitation skips any modules that the current module depends on. 00127 /// 00128 /// \param UserData User data associated with the visitor object, which 00129 /// will be passed along to the visitor. 00130 void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData); 00131 00132 /// \brief Visit each of the modules with a depth-first traversal. 00133 /// 00134 /// This routine visits each of the modules known to the module 00135 /// manager using a depth-first search, starting with the first 00136 /// loaded module. The traversal invokes the callback both before 00137 /// traversing the children (preorder traversal) and after 00138 /// traversing the children (postorder traversal). 00139 /// 00140 /// \param Visitor A visitor function that will be invoked with each 00141 /// module and given a \c Preorder flag that indicates whether we're 00142 /// visiting the module before or after visiting its children. The 00143 /// visitor may return true at any time to abort the depth-first 00144 /// visitation. 00145 /// 00146 /// \param UserData User data ssociated with the visitor object, 00147 /// which will be passed along to the user. 00148 void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 00149 void *UserData), 00150 void *UserData); 00151 00152 /// \brief View the graphviz representation of the module graph. 00153 void viewGraph(); 00154 }; 00155 00156 } } // end namespace clang::serialization 00157 00158 #endif