• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 //  This file defines the ModuleManager class, which manages a set of loaded
11 //  modules for the ASTReader.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
16 #define LLVM_CLANG_SERIALIZATION_MODULEMANAGER_H
17 
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 
23 namespace clang {
24 
25 class GlobalModuleIndex;
26 class ModuleMap;
27 
28 namespace serialization {
29 
30 /// \brief Manages the set of modules loaded by an AST reader.
31 class ModuleManager {
32   /// \brief The chain of AST files. The first entry is the one named by the
33   /// user, the last one is the one that doesn't depend on anything further.
34   SmallVector<ModuleFile *, 2> Chain;
35 
36   /// \brief All loaded modules, indexed by name.
37   llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
38 
39   typedef llvm::SetVector<const FileEntry *> AdditionalKnownModuleFileSet;
40 
41   /// \brief Additional module files that are known but not loaded. Tracked
42   /// here so that we can re-export them if necessary.
43   AdditionalKnownModuleFileSet AdditionalKnownModuleFiles;
44 
45   /// \brief FileManager that handles translating between filenames and
46   /// FileEntry *.
47   FileManager &FileMgr;
48 
49   /// \brief A lookup of in-memory (virtual file) buffers
50   llvm::DenseMap<const FileEntry *, std::unique_ptr<llvm::MemoryBuffer>>
51       InMemoryBuffers;
52 
53   /// \brief The visitation order.
54   SmallVector<ModuleFile *, 4> VisitOrder;
55 
56   /// \brief The list of module files that both we and the global module index
57   /// know about.
58   ///
59   /// Either the global index or the module manager may have modules that the
60   /// other does not know about, because the global index can be out-of-date
61   /// (in which case the module manager could have modules it does not) and
62   /// this particular translation unit might not have loaded all of the modules
63   /// known to the global index.
64   SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
65 
66   /// \brief The global module index, if one is attached.
67   ///
68   /// The global module index will actually be owned by the ASTReader; this is
69   /// just an non-owning pointer.
70   GlobalModuleIndex *GlobalIndex;
71 
72   /// \brief State used by the "visit" operation to avoid malloc traffic in
73   /// calls to visit().
74   struct VisitState {
VisitStateVisitState75     explicit VisitState(unsigned N)
76       : VisitNumber(N, 0), NextVisitNumber(1), NextState(nullptr)
77     {
78       Stack.reserve(N);
79     }
80 
~VisitStateVisitState81     ~VisitState() {
82       delete NextState;
83     }
84 
85     /// \brief The stack used when marking the imports of a particular module
86     /// as not-to-be-visited.
87     SmallVector<ModuleFile *, 4> Stack;
88 
89     /// \brief The visit number of each module file, which indicates when
90     /// this module file was last visited.
91     SmallVector<unsigned, 4> VisitNumber;
92 
93     /// \brief The next visit number to use to mark visited module files.
94     unsigned NextVisitNumber;
95 
96     /// \brief The next visit state.
97     VisitState *NextState;
98   };
99 
100   /// \brief The first visit() state in the chain.
101   VisitState *FirstVisitState;
102 
103   VisitState *allocateVisitState();
104   void returnVisitState(VisitState *State);
105 
106 public:
107   typedef SmallVectorImpl<ModuleFile*>::iterator ModuleIterator;
108   typedef SmallVectorImpl<ModuleFile*>::const_iterator ModuleConstIterator;
109   typedef SmallVectorImpl<ModuleFile*>::reverse_iterator ModuleReverseIterator;
110   typedef std::pair<uint32_t, StringRef> ModuleOffset;
111 
112   explicit ModuleManager(FileManager &FileMgr);
113   ~ModuleManager();
114 
115   /// \brief Forward iterator to traverse all loaded modules.  This is reverse
116   /// source-order.
begin()117   ModuleIterator begin() { return Chain.begin(); }
118   /// \brief Forward iterator end-point to traverse all loaded modules
end()119   ModuleIterator end() { return Chain.end(); }
120 
121   /// \brief Const forward iterator to traverse all loaded modules.  This is
122   /// in reverse source-order.
begin()123   ModuleConstIterator begin() const { return Chain.begin(); }
124   /// \brief Const forward iterator end-point to traverse all loaded modules
end()125   ModuleConstIterator end() const { return Chain.end(); }
126 
127   /// \brief Reverse iterator to traverse all loaded modules.  This is in
128   /// source order.
rbegin()129   ModuleReverseIterator rbegin() { return Chain.rbegin(); }
130   /// \brief Reverse iterator end-point to traverse all loaded modules.
rend()131   ModuleReverseIterator rend() { return Chain.rend(); }
132 
133   /// \brief Returns the primary module associated with the manager, that is,
134   /// the first module loaded
getPrimaryModule()135   ModuleFile &getPrimaryModule() { return *Chain[0]; }
136 
137   /// \brief Returns the primary module associated with the manager, that is,
138   /// the first module loaded.
getPrimaryModule()139   ModuleFile &getPrimaryModule() const { return *Chain[0]; }
140 
141   /// \brief Returns the module associated with the given index
142   ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
143 
144   /// \brief Returns the module associated with the given name
145   ModuleFile *lookup(StringRef Name);
146 
147   /// \brief Returns the module associated with the given module file.
148   ModuleFile *lookup(const FileEntry *File);
149 
150   /// \brief Returns the in-memory (virtual file) buffer with the given name
151   std::unique_ptr<llvm::MemoryBuffer> lookupBuffer(StringRef Name);
152 
153   /// \brief Number of modules loaded
size()154   unsigned size() const { return Chain.size(); }
155 
156   /// \brief The result of attempting to add a new module.
157   enum AddModuleResult {
158     /// \brief The module file had already been loaded.
159     AlreadyLoaded,
160     /// \brief The module file was just loaded in response to this call.
161     NewlyLoaded,
162     /// \brief The module file is missing.
163     Missing,
164     /// \brief The module file is out-of-date.
165     OutOfDate
166   };
167 
168   typedef ASTFileSignature(*ASTFileSignatureReader)(llvm::BitstreamReader &);
169 
170   /// \brief Attempts to create a new module and add it to the list of known
171   /// modules.
172   ///
173   /// \param FileName The file name of the module to be loaded.
174   ///
175   /// \param Type The kind of module being loaded.
176   ///
177   /// \param ImportLoc The location at which the module is imported.
178   ///
179   /// \param ImportedBy The module that is importing this module, or NULL if
180   /// this module is imported directly by the user.
181   ///
182   /// \param Generation The generation in which this module was loaded.
183   ///
184   /// \param ExpectedSize The expected size of the module file, used for
185   /// validation. This will be zero if unknown.
186   ///
187   /// \param ExpectedModTime The expected modification time of the module
188   /// file, used for validation. This will be zero if unknown.
189   ///
190   /// \param ExpectedSignature The expected signature of the module file, used
191   /// for validation. This will be zero if unknown.
192   ///
193   /// \param ReadSignature Reads the signature from an AST file without actually
194   /// loading it.
195   ///
196   /// \param Module A pointer to the module file if the module was successfully
197   /// loaded.
198   ///
199   /// \param ErrorStr Will be set to a non-empty string if any errors occurred
200   /// while trying to load the module.
201   ///
202   /// \return A pointer to the module that corresponds to this file name,
203   /// and a value indicating whether the module was loaded.
204   AddModuleResult addModule(StringRef FileName, ModuleKind Type,
205                             SourceLocation ImportLoc,
206                             ModuleFile *ImportedBy, unsigned Generation,
207                             off_t ExpectedSize, time_t ExpectedModTime,
208                             ASTFileSignature ExpectedSignature,
209                             ASTFileSignatureReader ReadSignature,
210                             ModuleFile *&Module,
211                             std::string &ErrorStr);
212 
213   /// \brief Remove the given set of modules.
214   void removeModules(ModuleIterator first, ModuleIterator last,
215                      llvm::SmallPtrSetImpl<ModuleFile *> &LoadedSuccessfully,
216                      ModuleMap *modMap);
217 
218   /// \brief Add an in-memory buffer the list of known buffers
219   void addInMemoryBuffer(StringRef FileName,
220                          std::unique_ptr<llvm::MemoryBuffer> Buffer);
221 
222   /// \brief Set the global module index.
223   void setGlobalIndex(GlobalModuleIndex *Index);
224 
225   /// \brief Notification from the AST reader that the given module file
226   /// has been "accepted", and will not (can not) be unloaded.
227   void moduleFileAccepted(ModuleFile *MF);
228 
229   /// \brief Notification from the frontend that the given module file is
230   /// part of this compilation (even if not imported) and, if this compilation
231   /// is exported, should be made available to importers of it.
232   bool addKnownModuleFile(StringRef FileName);
233 
234   /// \brief Get a list of additional module files that are not currently
235   /// loaded but are considered to be part of the current compilation.
236   llvm::iterator_range<AdditionalKnownModuleFileSet::const_iterator>
getAdditionalKnownModuleFiles()237   getAdditionalKnownModuleFiles() {
238     return llvm::make_range(AdditionalKnownModuleFiles.begin(),
239                             AdditionalKnownModuleFiles.end());
240   }
241 
242   /// \brief Visit each of the modules.
243   ///
244   /// This routine visits each of the modules, starting with the
245   /// "root" modules that no other loaded modules depend on, and
246   /// proceeding to the leaf modules, visiting each module only once
247   /// during the traversal.
248   ///
249   /// This traversal is intended to support various "lookup"
250   /// operations that can find data in any of the loaded modules.
251   ///
252   /// \param Visitor A visitor function that will be invoked with each
253   /// module and the given user data pointer. The return value must be
254   /// convertible to bool; when false, the visitation continues to
255   /// modules that the current module depends on. When true, the
256   /// visitation skips any modules that the current module depends on.
257   ///
258   /// \param UserData User data associated with the visitor object, which
259   /// will be passed along to the visitor.
260   ///
261   /// \param ModuleFilesHit If non-NULL, contains the set of module files
262   /// that we know we need to visit because the global module index told us to.
263   /// Any module that is known to both the global module index and the module
264   /// manager that is *not* in this set can be skipped.
265   void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
266              llvm::SmallPtrSetImpl<ModuleFile *> *ModuleFilesHit = nullptr);
267 
268   /// \brief Visit each of the modules with a depth-first traversal.
269   ///
270   /// This routine visits each of the modules known to the module
271   /// manager using a depth-first search, starting with the first
272   /// loaded module. The traversal invokes the callback both before
273   /// traversing the children (preorder traversal) and after
274   /// traversing the children (postorder traversal).
275   ///
276   /// \param Visitor A visitor function that will be invoked with each
277   /// module and given a \c Preorder flag that indicates whether we're
278   /// visiting the module before or after visiting its children.  The
279   /// visitor may return true at any time to abort the depth-first
280   /// visitation.
281   ///
282   /// \param UserData User data ssociated with the visitor object,
283   /// which will be passed along to the user.
284   void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
285                                        void *UserData),
286                        void *UserData);
287 
288   /// \brief Attempt to resolve the given module file name to a file entry.
289   ///
290   /// \param FileName The name of the module file.
291   ///
292   /// \param ExpectedSize The size that the module file is expected to have.
293   /// If the actual size differs, the resolver should return \c true.
294   ///
295   /// \param ExpectedModTime The modification time that the module file is
296   /// expected to have. If the actual modification time differs, the resolver
297   /// should return \c true.
298   ///
299   /// \param File Will be set to the file if there is one, or null
300   /// otherwise.
301   ///
302   /// \returns True if a file exists but does not meet the size/
303   /// modification time criteria, false if the file is either available and
304   /// suitable, or is missing.
305   bool lookupModuleFile(StringRef FileName,
306                         off_t ExpectedSize,
307                         time_t ExpectedModTime,
308                         const FileEntry *&File);
309 
310   /// \brief View the graphviz representation of the module graph.
311   void viewGraph();
312 };
313 
314 } } // end namespace clang::serialization
315 
316 #endif
317