clang API Documentation

ModuleManager.h
Go to the documentation of this file.
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_MODULEMANAGER_H
00016 #define LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
00017 
00018 #include "clang/Basic/FileManager.h"
00019 #include "clang/Serialization/Module.h"
00020 #include "llvm/ADT/DenseMap.h"
00021 #include "llvm/ADT/SmallPtrSet.h"
00022 
00023 namespace clang { 
00024 
00025 class GlobalModuleIndex;
00026 class ModuleMap;
00027 
00028 namespace serialization {
00029 
00030 /// \brief Manages the set of modules loaded by an AST reader.
00031 class ModuleManager {
00032   /// \brief The chain of AST files. The first entry is the one named by the
00033   /// user, the last one is the one that doesn't depend on anything further.
00034   SmallVector<ModuleFile *, 2> Chain;
00035   
00036   /// \brief All loaded modules, indexed by name.
00037   llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
00038   
00039   /// \brief FileManager that handles translating between filenames and
00040   /// FileEntry *.
00041   FileManager &FileMgr;
00042   
00043   /// \brief A lookup of in-memory (virtual file) buffers
00044   llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
00045       InMemoryBuffers;
00046 
00047   /// \brief The visitation order.
00048   SmallVector<ModuleFile *, 4> VisitOrder;
00049       
00050   /// \brief The list of module files that both we and the global module index
00051   /// know about.
00052   ///
00053   /// Either the global index or the module manager may have modules that the
00054   /// other does not know about, because the global index can be out-of-date
00055   /// (in which case the module manager could have modules it does not) and
00056   /// this particular translation unit might not have loaded all of the modules
00057   /// known to the global index.
00058   SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
00059 
00060   /// \brief The global module index, if one is attached.
00061   ///
00062   /// The global module index will actually be owned by the ASTReader; this is
00063   /// just an non-owning pointer.
00064   GlobalModuleIndex *GlobalIndex;
00065 
00066   /// \brief State used by the "visit" operation to avoid malloc traffic in
00067   /// calls to visit().
00068   struct VisitState {
00069     explicit VisitState(unsigned N)
00070       : VisitNumber(N, 0), NextVisitNumber(1), NextState(nullptr)
00071     {
00072       Stack.reserve(N);
00073     }
00074 
00075     ~VisitState() {
00076       delete NextState;
00077     }
00078 
00079     /// \brief The stack used when marking the imports of a particular module
00080     /// as not-to-be-visited.
00081     SmallVector<ModuleFile *, 4> Stack;
00082 
00083     /// \brief The visit number of each module file, which indicates when
00084     /// this module file was last visited.
00085     SmallVector<unsigned, 4> VisitNumber;
00086 
00087     /// \brief The next visit number to use to mark visited module files.
00088     unsigned NextVisitNumber;
00089 
00090     /// \brief The next visit state.
00091     VisitState *NextState;
00092   };
00093 
00094   /// \brief The first visit() state in the chain.
00095   VisitState *FirstVisitState;
00096 
00097   VisitState *allocateVisitState();
00098   void returnVisitState(VisitState *State);
00099 
00100 public:
00101   typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator;
00102   typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator;
00103   typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator;
00104   typedef std::pair<uint32_t, StringRef> ModuleOffset;
00105   
00106   explicit ModuleManager(FileManager &FileMgr);
00107   ~ModuleManager();
00108   
00109   /// \brief Forward iterator to traverse all loaded modules.  This is reverse
00110   /// source-order.
00111   ModuleIterator begin() { return Chain.begin(); }
00112   /// \brief Forward iterator end-point to traverse all loaded modules
00113   ModuleIterator end() { return Chain.end(); }
00114   
00115   /// \brief Const forward iterator to traverse all loaded modules.  This is 
00116   /// in reverse source-order.
00117   ModuleConstIterator begin() const { return Chain.begin(); }
00118   /// \brief Const forward iterator end-point to traverse all loaded modules
00119   ModuleConstIterator end() const { return Chain.end(); }
00120   
00121   /// \brief Reverse iterator to traverse all loaded modules.  This is in 
00122   /// source order.
00123   ModuleReverseIterator rbegin() { return Chain.rbegin(); }
00124   /// \brief Reverse iterator end-point to traverse all loaded modules.
00125   ModuleReverseIterator rend() { return Chain.rend(); }
00126   
00127   /// \brief Returns the primary module associated with the manager, that is,
00128   /// the first module loaded
00129   ModuleFile &getPrimaryModule() { return *Chain[0]; }
00130   
00131   /// \brief Returns the primary module associated with the manager, that is,
00132   /// the first module loaded.
00133   ModuleFile &getPrimaryModule() const { return *Chain[0]; }
00134   
00135   /// \brief Returns the module associated with the given index
00136   ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
00137   
00138   /// \brief Returns the module associated with the given name
00139   ModuleFile *lookup(StringRef Name);
00140 
00141   /// \brief Returns the module associated with the given module file.
00142   ModuleFile *lookup(const FileEntry *File);
00143 
00144   /// \brief Returns the in-memory (virtual file) buffer with the given name
00145   std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
00146   
00147   /// \brief Number of modules loaded
00148   unsigned size() const { return Chain.size(); }
00149 
00150   /// \brief The result of attempting to add a new module.
00151   enum AddModuleResult {
00152     /// \brief The module file had already been loaded.
00153     AlreadyLoaded,
00154     /// \brief The module file was just loaded in response to this call.
00155     NewlyLoaded,
00156     /// \brief The module file is missing.
00157     Missing,
00158     /// \brief The module file is out-of-date.
00159     OutOfDate
00160   };
00161 
00162   /// \brief Attempts to create a new module and add it to the list of known
00163   /// modules.
00164   ///
00165   /// \param FileName The file name of the module to be loaded.
00166   ///
00167   /// \param Type The kind of module being loaded.
00168   ///
00169   /// \param ImportLoc The location at which the module is imported.
00170   ///
00171   /// \param ImportedBy The module that is importing this module, or NULL if
00172   /// this module is imported directly by the user.
00173   ///
00174   /// \param Generation The generation in which this module was loaded.
00175   ///
00176   /// \param ExpectedSize The expected size of the module file, used for
00177   /// validation. This will be zero if unknown.
00178   ///
00179   /// \param ExpectedModTime The expected modification time of the module
00180   /// file, used for validation. This will be zero if unknown.
00181   ///
00182   /// \param ExpectedSignature The expected signature of the module file, used
00183   /// for validation. This will be zero if unknown.
00184   ///
00185   /// \param ReadSignature Reads the signature from an AST file without actually
00186   /// loading it.
00187   ///
00188   /// \param Module A pointer to the module file if the module was successfully
00189   /// loaded.
00190   ///
00191   /// \param ErrorStr Will be set to a non-empty string if any errors occurred
00192   /// while trying to load the module.
00193   ///
00194   /// \return A pointer to the module that corresponds to this file name,
00195   /// and a value indicating whether the module was loaded.
00196   AddModuleResult addModule(StringRef FileName, ModuleKind Type,
00197                             SourceLocation ImportLoc,
00198                             ModuleFile *ImportedBy, unsigned Generation,
00199                             off_t ExpectedSize, time_t ExpectedModTime,
00200                             ASTFileSignature ExpectedSignature,
00201                             std::function<ASTFileSignature(llvm::BitstreamReader &)>
00202                                 ReadSignature,
00203                             ModuleFile *&Module,
00204                             std::string &ErrorStr);
00205 
00206   /// \brief Remove the given set of modules.
00207   void removeModules(ModuleIterator first, ModuleIterator last,
00208                      llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
00209                      ModuleMap *modMap);
00210 
00211   /// \brief Add an in-memory buffer the list of known buffers
00212   void addInMemoryBuffer(StringRef FileName,
00213                          std::unique_ptr<llvm::MemoryBuffer> Buffer);
00214 
00215   /// \brief Set the global module index.
00216   void setGlobalIndex(GlobalModuleIndex *Index);
00217 
00218   /// \brief Notification from the AST reader that the given module file
00219   /// has been "accepted", and will not (can not) be unloaded.
00220   void moduleFileAccepted(ModuleFile *MF);
00221 
00222   /// \brief Visit each of the modules.
00223   ///
00224   /// This routine visits each of the modules, starting with the
00225   /// "root" modules that no other loaded modules depend on, and
00226   /// proceeding to the leaf modules, visiting each module only once
00227   /// during the traversal.
00228   ///
00229   /// This traversal is intended to support various "lookup"
00230   /// operations that can find data in any of the loaded modules.
00231   ///
00232   /// \param Visitor A visitor function that will be invoked with each
00233   /// module and the given user data pointer. The return value must be
00234   /// convertible to bool; when false, the visitation continues to
00235   /// modules that the current module depends on. When true, the
00236   /// visitation skips any modules that the current module depends on.
00237   ///
00238   /// \param UserData User data associated with the visitor object, which
00239   /// will be passed along to the visitor.
00240   ///
00241   /// \param ModuleFilesHit If non-NULL, contains the set of module files
00242   /// that we know we need to visit because the global module index told us to.
00243   /// Any module that is known to both the global module index and the module
00244   /// manager that is *not* in this set can be skipped.
00245   void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
00246              llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit = nullptr);
00247   
00248   /// \brief Visit each of the modules with a depth-first traversal.
00249   ///
00250   /// This routine visits each of the modules known to the module
00251   /// manager using a depth-first search, starting with the first
00252   /// loaded module. The traversal invokes the callback both before
00253   /// traversing the children (preorder traversal) and after
00254   /// traversing the children (postorder traversal).
00255   ///
00256   /// \param Visitor A visitor function that will be invoked with each
00257   /// module and given a \c Preorder flag that indicates whether we're
00258   /// visiting the module before or after visiting its children.  The
00259   /// visitor may return true at any time to abort the depth-first
00260   /// visitation.
00261   ///
00262   /// \param UserData User data ssociated with the visitor object,
00263   /// which will be passed along to the user.
00264   void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder, 
00265                                        void *UserData), 
00266                        void *UserData);
00267 
00268   /// \brief Attempt to resolve the given module file name to a file entry.
00269   ///
00270   /// \param FileName The name of the module file.
00271   ///
00272   /// \param ExpectedSize The size that the module file is expected to have.
00273   /// If the actual size differs, the resolver should return \c true.
00274   ///
00275   /// \param ExpectedModTime The modification time that the module file is
00276   /// expected to have. If the actual modification time differs, the resolver
00277   /// should return \c true.
00278   ///
00279   /// \param File Will be set to the file if there is one, or null
00280   /// otherwise.
00281   ///
00282   /// \returns True if a file exists but does not meet the size/
00283   /// modification time criteria, false if the file is either available and
00284   /// suitable, or is missing.
00285   bool lookupModuleFile(StringRef FileName,
00286                         off_t ExpectedSize,
00287                         time_t ExpectedModTime,
00288                         const FileEntry *&File);
00289 
00290   /// \brief View the graphviz representation of the module graph.
00291   void viewGraph();
00292 };
00293 
00294 } } // end namespace clang::serialization
00295 
00296 #endif