1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ModuleManager class, which manages a set of loaded
11 // modules for the ASTReader.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
16 #define LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Serialization/Module.h"
20 #include "llvm/ADT/DenseMap.h"
21 #include "llvm/ADT/SmallPtrSet.h"
22 #include "llvm/ADT/iterator.h"
26 class GlobalModuleIndex;
27 class MemoryBufferCache;
29 class PCHContainerReader;
31 namespace serialization {
33 /// \brief Manages the set of modules loaded by an AST reader.
35 /// \brief The chain of AST files, in the order in which we started to load
36 /// them (this order isn't really useful for anything).
37 SmallVector<std::unique_ptr<ModuleFile>, 2> Chain;
39 /// \brief The chain of non-module PCH files. The first entry is the one named
40 /// by the user, the last one is the one that doesn't depend on anything
42 SmallVector<ModuleFile *, 2> PCHChain;
44 // \brief The roots of the dependency DAG of AST files. This is used
45 // to implement short-circuiting logic when running DFS over the dependencies.
46 SmallVector<ModuleFile *, 2> Roots;
48 /// \brief All loaded modules, indexed by name.
49 llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
51 /// \brief FileManager that handles translating between filenames and
55 /// Cache of PCM files.
56 IntrusiveRefCntPtr<MemoryBufferCache> PCMCache;
58 /// \brief Knows how to unwrap module containers.
59 const PCHContainerReader &PCHContainerRdr;
61 /// \brief A lookup of in-memory (virtual file) buffers
62 llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
65 /// \brief The visitation order.
66 SmallVector<ModuleFile *, 4> VisitOrder;
68 /// \brief The list of module files that both we and the global module index
71 /// Either the global index or the module manager may have modules that the
72 /// other does not know about, because the global index can be out-of-date
73 /// (in which case the module manager could have modules it does not) and
74 /// this particular translation unit might not have loaded all of the modules
75 /// known to the global index.
76 SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
78 /// \brief The global module index, if one is attached.
80 /// The global module index will actually be owned by the ASTReader; this is
81 /// just an non-owning pointer.
82 GlobalModuleIndex *GlobalIndex;
84 /// \brief State used by the "visit" operation to avoid malloc traffic in
87 explicit VisitState(unsigned N)
88 : VisitNumber(N, 0), NextVisitNumber(1), NextState(nullptr)
97 /// \brief The stack used when marking the imports of a particular module
98 /// as not-to-be-visited.
99 SmallVector<ModuleFile *, 4> Stack;
101 /// \brief The visit number of each module file, which indicates when
102 /// this module file was last visited.
103 SmallVector<unsigned, 4> VisitNumber;
105 /// \brief The next visit number to use to mark visited module files.
106 unsigned NextVisitNumber;
108 /// \brief The next visit state.
109 VisitState *NextState;
112 /// \brief The first visit() state in the chain.
113 VisitState *FirstVisitState;
115 VisitState *allocateVisitState();
116 void returnVisitState(VisitState *State);
119 typedef llvm::pointee_iterator<
120 SmallVectorImpl<std::unique_ptr<ModuleFile>>::iterator>
122 typedef llvm::pointee_iterator<
123 SmallVectorImpl<std::unique_ptr<ModuleFile>>::const_iterator>
125 typedef llvm::pointee_iterator<
126 SmallVectorImpl<std::unique_ptr<ModuleFile>>::reverse_iterator>
127 ModuleReverseIterator;
128 typedef std::pair<uint32_t, StringRef> ModuleOffset;
130 explicit ModuleManager(FileManager &FileMgr, MemoryBufferCache &PCMCache,
131 const PCHContainerReader &PCHContainerRdr);
134 /// \brief Forward iterator to traverse all loaded modules.
135 ModuleIterator begin() { return Chain.begin(); }
136 /// \brief Forward iterator end-point to traverse all loaded modules
137 ModuleIterator end() { return Chain.end(); }
139 /// \brief Const forward iterator to traverse all loaded modules.
140 ModuleConstIterator begin() const { return Chain.begin(); }
141 /// \brief Const forward iterator end-point to traverse all loaded modules
142 ModuleConstIterator end() const { return Chain.end(); }
144 /// \brief Reverse iterator to traverse all loaded modules.
145 ModuleReverseIterator rbegin() { return Chain.rbegin(); }
146 /// \brief Reverse iterator end-point to traverse all loaded modules.
147 ModuleReverseIterator rend() { return Chain.rend(); }
149 /// \brief A range covering the PCH and preamble module files loaded.
150 llvm::iterator_range<SmallVectorImpl<ModuleFile *>::const_iterator>
151 pch_modules() const {
152 return llvm::make_range(PCHChain.begin(), PCHChain.end());
155 /// \brief Returns the primary module associated with the manager, that is,
156 /// the first module loaded
157 ModuleFile &getPrimaryModule() { return *Chain[0]; }
159 /// \brief Returns the primary module associated with the manager, that is,
160 /// the first module loaded.
161 ModuleFile &getPrimaryModule() const { return *Chain[0]; }
163 /// \brief Returns the module associated with the given index
164 ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
166 /// \brief Returns the module associated with the given name
167 ModuleFile *lookup(StringRef Name) const;
169 /// \brief Returns the module associated with the given module file.
170 ModuleFile *lookup(const FileEntry *File) const;
172 /// \brief Returns the in-memory (virtual file) buffer with the given name
173 std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
175 /// \brief Number of modules loaded
176 unsigned size() const { return Chain.size(); }
178 /// \brief The result of attempting to add a new module.
179 enum AddModuleResult {
180 /// \brief The module file had already been loaded.
182 /// \brief The module file was just loaded in response to this call.
184 /// \brief The module file is missing.
186 /// \brief The module file is out-of-date.
190 typedef ASTFileSignature(*ASTFileSignatureReader)(StringRef);
192 /// \brief Attempts to create a new module and add it to the list of known
195 /// \param FileName The file name of the module to be loaded.
197 /// \param Type The kind of module being loaded.
199 /// \param ImportLoc The location at which the module is imported.
201 /// \param ImportedBy The module that is importing this module, or NULL if
202 /// this module is imported directly by the user.
204 /// \param Generation The generation in which this module was loaded.
206 /// \param ExpectedSize The expected size of the module file, used for
207 /// validation. This will be zero if unknown.
209 /// \param ExpectedModTime The expected modification time of the module
210 /// file, used for validation. This will be zero if unknown.
212 /// \param ExpectedSignature The expected signature of the module file, used
213 /// for validation. This will be zero if unknown.
215 /// \param ReadSignature Reads the signature from an AST file without actually
218 /// \param Module A pointer to the module file if the module was successfully
221 /// \param ErrorStr Will be set to a non-empty string if any errors occurred
222 /// while trying to load the module.
224 /// \return A pointer to the module that corresponds to this file name,
225 /// and a value indicating whether the module was loaded.
226 AddModuleResult addModule(StringRef FileName, ModuleKind Type,
227 SourceLocation ImportLoc,
228 ModuleFile *ImportedBy, unsigned Generation,
229 off_t ExpectedSize, time_t ExpectedModTime,
230 ASTFileSignature ExpectedSignature,
231 ASTFileSignatureReader ReadSignature,
233 std::string &ErrorStr);
235 /// \brief Remove the modules starting from First (to the end).
236 void removeModules(ModuleIterator First,
237 llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
240 /// \brief Add an in-memory buffer the list of known buffers
241 void addInMemoryBuffer(StringRef FileName,
242 std::unique_ptr<llvm::MemoryBuffer> Buffer);
244 /// \brief Set the global module index.
245 void setGlobalIndex(GlobalModuleIndex *Index);
247 /// \brief Notification from the AST reader that the given module file
248 /// has been "accepted", and will not (can not) be unloaded.
249 void moduleFileAccepted(ModuleFile *MF);
251 /// \brief Visit each of the modules.
253 /// This routine visits each of the modules, starting with the
254 /// "root" modules that no other loaded modules depend on, and
255 /// proceeding to the leaf modules, visiting each module only once
256 /// during the traversal.
258 /// This traversal is intended to support various "lookup"
259 /// operations that can find data in any of the loaded modules.
261 /// \param Visitor A visitor function that will be invoked with each
262 /// module. The return value must be convertible to bool; when false, the
263 /// visitation continues to modules that the current module depends on. When
264 /// true, the visitation skips any modules that the current module depends on.
266 /// \param ModuleFilesHit If non-NULL, contains the set of module files
267 /// that we know we need to visit because the global module index told us to.
268 /// Any module that is known to both the global module index and the module
269 /// manager that is *not* in this set can be skipped.
270 void visit(llvm::function_ref<bool(ModuleFile &M)> Visitor,
271 llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit = nullptr);
273 /// \brief Attempt to resolve the given module file name to a file entry.
275 /// \param FileName The name of the module file.
277 /// \param ExpectedSize The size that the module file is expected to have.
278 /// If the actual size differs, the resolver should return \c true.
280 /// \param ExpectedModTime The modification time that the module file is
281 /// expected to have. If the actual modification time differs, the resolver
282 /// should return \c true.
284 /// \param File Will be set to the file if there is one, or null
287 /// \returns True if a file exists but does not meet the size/
288 /// modification time criteria, false if the file is either available and
289 /// suitable, or is missing.
290 bool lookupModuleFile(StringRef FileName,
292 time_t ExpectedModTime,
293 const FileEntry *&File);
295 /// \brief View the graphviz representation of the module graph.
298 MemoryBufferCache &getPCMCache() const { return *PCMCache; }
301 } } // end namespace clang::serialization