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_MODULE_MANAGER_H
16 #define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H
18 #include "clang/Basic/FileManager.h"
19 #include "clang/Serialization/Module.h"
20 #include "llvm/ADT/DenseMap.h"
24 class GlobalModuleIndex;
27 namespace serialization {
29 /// \brief Manages the set of modules loaded by an AST reader.
31 /// \brief The chain of AST files. The first entry is the one named by the
32 /// user, the last one is the one that doesn't depend on anything further.
33 SmallVector<ModuleFile *, 2> Chain;
35 /// \brief All loaded modules, indexed by name.
36 llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
38 /// \brief FileManager that handles translating between filenames and
42 /// \brief A lookup of in-memory (virtual file) buffers
43 llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers;
45 /// \brief The visitation order.
46 SmallVector<ModuleFile *, 4> VisitOrder;
48 /// \brief The list of module files that both we and the global module index
51 /// Either the global index or the module manager may have modules that the
52 /// other does not know about, because the global index can be out-of-date
53 /// (in which case the module manager could have modules it does not) and
54 /// this particular translation unit might not have loaded all of the modules
55 /// known to the global index.
56 SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
58 /// \brief The global module index, if one is attached.
60 /// The global module index will actually be owned by the ASTReader; this is
61 /// just an non-owning pointer.
62 GlobalModuleIndex *GlobalIndex;
64 /// \brief State used by the "visit" operation to avoid malloc traffic in
67 explicit VisitState(unsigned N)
68 : VisitNumber(N, 0), NextVisitNumber(1), NextState(0)
77 /// \brief The stack used when marking the imports of a particular module
78 /// as not-to-be-visited.
79 SmallVector<ModuleFile *, 4> Stack;
81 /// \brief The visit number of each module file, which indicates when
82 /// this module file was last visited.
83 SmallVector<unsigned, 4> VisitNumber;
85 /// \brief The next visit number to use to mark visited module files.
86 unsigned NextVisitNumber;
88 /// \brief The next visit state.
89 VisitState *NextState;
92 /// \brief The first visit() state in the chain.
93 VisitState *FirstVisitState;
95 VisitState *allocateVisitState();
96 void returnVisitState(VisitState *State);
99 typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator;
100 typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator;
101 typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator;
102 typedef std::pair<uint32_t, StringRef> ModuleOffset;
104 explicit ModuleManager(FileManager &FileMgr);
107 /// \brief Forward iterator to traverse all loaded modules. This is reverse
109 ModuleIterator begin() { return Chain.begin(); }
110 /// \brief Forward iterator end-point to traverse all loaded modules
111 ModuleIterator end() { return Chain.end(); }
113 /// \brief Const forward iterator to traverse all loaded modules. This is
114 /// in reverse source-order.
115 ModuleConstIterator begin() const { return Chain.begin(); }
116 /// \brief Const forward iterator end-point to traverse all loaded modules
117 ModuleConstIterator end() const { return Chain.end(); }
119 /// \brief Reverse iterator to traverse all loaded modules. This is in
121 ModuleReverseIterator rbegin() { return Chain.rbegin(); }
122 /// \brief Reverse iterator end-point to traverse all loaded modules.
123 ModuleReverseIterator rend() { return Chain.rend(); }
125 /// \brief Returns the primary module associated with the manager, that is,
126 /// the first module loaded
127 ModuleFile &getPrimaryModule() { return *Chain[0]; }
129 /// \brief Returns the primary module associated with the manager, that is,
130 /// the first module loaded.
131 ModuleFile &getPrimaryModule() const { return *Chain[0]; }
133 /// \brief Returns the module associated with the given index
134 ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
136 /// \brief Returns the module associated with the given name
137 ModuleFile *lookup(StringRef Name);
139 /// \brief Returns the module associated with the given module file.
140 ModuleFile *lookup(const FileEntry *File);
142 /// \brief Returns the in-memory (virtual file) buffer with the given name
143 llvm::MemoryBuffer *lookupBuffer(StringRef Name);
145 /// \brief Number of modules loaded
146 unsigned size() const { return Chain.size(); }
148 /// \brief The result of attempting to add a new module.
149 enum AddModuleResult {
150 /// \brief The module file had already been loaded.
152 /// \brief The module file was just loaded in response to this call.
154 /// \brief The module file is missing.
156 /// \brief The module file is out-of-date.
160 /// \brief Attempts to create a new module and add it to the list of known
163 /// \param FileName The file name of the module to be loaded.
165 /// \param Type The kind of module being loaded.
167 /// \param ImportLoc The location at which the module is imported.
169 /// \param ImportedBy The module that is importing this module, or NULL if
170 /// this module is imported directly by the user.
172 /// \param Generation The generation in which this module was loaded.
174 /// \param ExpectedSize The expected size of the module file, used for
175 /// validation. This will be zero if unknown.
177 /// \param ExpectedModTime The expected modification time of the module
178 /// file, used for validation. This will be zero if unknown.
180 /// \param Module A pointer to the module file if the module was successfully
183 /// \param ErrorStr Will be set to a non-empty string if any errors occurred
184 /// while trying to load the module.
186 /// \return A pointer to the module that corresponds to this file name,
187 /// and a value indicating whether the module was loaded.
188 AddModuleResult addModule(StringRef FileName, ModuleKind Type,
189 SourceLocation ImportLoc,
190 ModuleFile *ImportedBy, unsigned Generation,
191 off_t ExpectedSize, time_t ExpectedModTime,
193 std::string &ErrorStr);
195 /// \brief Remove the given set of modules.
196 void removeModules(ModuleIterator first, ModuleIterator last,
199 /// \brief Add an in-memory buffer the list of known buffers
200 void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
202 /// \brief Set the global module index.
203 void setGlobalIndex(GlobalModuleIndex *Index);
205 /// \brief Notification from the AST reader that the given module file
206 /// has been "accepted", and will not (can not) be unloaded.
207 void moduleFileAccepted(ModuleFile *MF);
209 /// \brief Visit each of the modules.
211 /// This routine visits each of the modules, starting with the
212 /// "root" modules that no other loaded modules depend on, and
213 /// proceeding to the leaf modules, visiting each module only once
214 /// during the traversal.
216 /// This traversal is intended to support various "lookup"
217 /// operations that can find data in any of the loaded modules.
219 /// \param Visitor A visitor function that will be invoked with each
220 /// module and the given user data pointer. The return value must be
221 /// convertible to bool; when false, the visitation continues to
222 /// modules that the current module depends on. When true, the
223 /// visitation skips any modules that the current module depends on.
225 /// \param UserData User data associated with the visitor object, which
226 /// will be passed along to the visitor.
228 /// \param ModuleFilesHit If non-NULL, contains the set of module files
229 /// that we know we need to visit because the global module index told us to.
230 /// Any module that is known to both the global module index and the module
231 /// manager that is *not* in this set can be skipped.
232 void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
233 llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit = 0);
235 /// \brief Visit each of the modules with a depth-first traversal.
237 /// This routine visits each of the modules known to the module
238 /// manager using a depth-first search, starting with the first
239 /// loaded module. The traversal invokes the callback both before
240 /// traversing the children (preorder traversal) and after
241 /// traversing the children (postorder traversal).
243 /// \param Visitor A visitor function that will be invoked with each
244 /// module and given a \c Preorder flag that indicates whether we're
245 /// visiting the module before or after visiting its children. The
246 /// visitor may return true at any time to abort the depth-first
249 /// \param UserData User data ssociated with the visitor object,
250 /// which will be passed along to the user.
251 void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
255 /// \brief Attempt to resolve the given module file name to a file entry.
257 /// \param FileName The name of the module file.
259 /// \param ExpectedSize The size that the module file is expected to have.
260 /// If the actual size differs, the resolver should return \c true.
262 /// \param ExpectedModTime The modification time that the module file is
263 /// expected to have. If the actual modification time differs, the resolver
264 /// should return \c true.
266 /// \param File Will be set to the file if there is one, or null
269 /// \returns True if a file exists but does not meet the size/
270 /// modification time criteria, false if the file is either available and
271 /// suitable, or is missing.
272 bool lookupModuleFile(StringRef FileName,
274 time_t ExpectedModTime,
275 const FileEntry *&File);
277 /// \brief View the graphviz representation of the module graph.
281 } } // end namespace clang::serialization