ModuleManager.h revision d3cf5fba332fc77f7e72ef58077822606718671d
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_MODULE_MANAGER_H
16#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H
17
18#include "clang/Basic/FileManager.h"
19#include "clang/Serialization/Module.h"
20#include "llvm/ADT/DenseMap.h"
21
22namespace clang {
23
24class GlobalModuleIndex;
25
26namespace serialization {
27
28/// \brief Manages the set of modules loaded by an AST reader.
29class ModuleManager {
30  /// \brief The chain of AST files. The first entry is the one named by the
31  /// user, the last one is the one that doesn't depend on anything further.
32  SmallVector<ModuleFile *, 2> Chain;
33
34  /// \brief All loaded modules, indexed by name.
35  llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
36
37  /// \brief FileManager that handles translating between filenames and
38  /// FileEntry *.
39  FileManager &FileMgr;
40
41  /// \brief A lookup of in-memory (virtual file) buffers
42  llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers;
43
44  /// \brief The visitation order.
45  SmallVector<ModuleFile *, 4> VisitOrder;
46
47  /// \brief The list of module files that both we and the global module index
48  /// know about.
49  ///
50  /// Either the global index or the module manager may have modules that the
51  /// other does not know about, because the global index can be out-of-date
52  /// (in which case the module manager could have modules it does not) and
53  /// this particular translation unit might not have loaded all of the modules
54  /// known to the global index.
55  SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
56
57  /// \brief The global module index, if one is attached.
58  ///
59  /// The global module index will actually be owned by the ASTReader; this is
60  /// just an non-owning pointer.
61  GlobalModuleIndex *GlobalIndex;
62
63  /// \brief Update the set of modules files we know about known to the global index.
64  void updateModulesInCommonWithGlobalIndex();
65
66  /// \brief State used by the "visit" operation to avoid malloc traffic in
67  /// calls to visit().
68  struct VisitState {
69    explicit VisitState(unsigned N)
70      : VisitNumber(N, 0), NextVisitNumber(1), NextState(0)
71    {
72      Stack.reserve(N);
73    }
74
75    ~VisitState() {
76      delete NextState;
77    }
78
79    /// \brief The stack used when marking the imports of a particular module
80    /// as not-to-be-visited.
81    SmallVector<ModuleFile *, 4> Stack;
82
83    /// \brief The visit number of each module file, which indicates when
84    /// this module file was last visited.
85    SmallVector<unsigned, 4> VisitNumber;
86
87    /// \brief The next visit number to use to mark visited module files.
88    unsigned NextVisitNumber;
89
90    /// \brief The next visit state.
91    VisitState *NextState;
92  };
93
94  /// \brief The first visit() state in the chain.
95  VisitState *FirstVisitState;
96
97  VisitState *allocateVisitState();
98  void returnVisitState(VisitState *State);
99
100public:
101  typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator;
102  typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator;
103  typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator;
104  typedef std::pair<uint32_t, StringRef> ModuleOffset;
105
106  explicit ModuleManager(FileManager &FileMgr);
107  ~ModuleManager();
108
109  /// \brief Forward iterator to traverse all loaded modules.  This is reverse
110  /// source-order.
111  ModuleIterator begin() { return Chain.begin(); }
112  /// \brief Forward iterator end-point to traverse all loaded modules
113  ModuleIterator end() { return Chain.end(); }
114
115  /// \brief Const forward iterator to traverse all loaded modules.  This is
116  /// in reverse source-order.
117  ModuleConstIterator begin() const { return Chain.begin(); }
118  /// \brief Const forward iterator end-point to traverse all loaded modules
119  ModuleConstIterator end() const { return Chain.end(); }
120
121  /// \brief Reverse iterator to traverse all loaded modules.  This is in
122  /// source order.
123  ModuleReverseIterator rbegin() { return Chain.rbegin(); }
124  /// \brief Reverse iterator end-point to traverse all loaded modules.
125  ModuleReverseIterator rend() { return Chain.rend(); }
126
127  /// \brief Returns the primary module associated with the manager, that is,
128  /// the first module loaded
129  ModuleFile &getPrimaryModule() { return *Chain[0]; }
130
131  /// \brief Returns the primary module associated with the manager, that is,
132  /// the first module loaded.
133  ModuleFile &getPrimaryModule() const { return *Chain[0]; }
134
135  /// \brief Returns the module associated with the given index
136  ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
137
138  /// \brief Returns the module associated with the given name
139  ModuleFile *lookup(StringRef Name);
140
141  /// \brief Returns the in-memory (virtual file) buffer with the given name
142  llvm::MemoryBuffer *lookupBuffer(StringRef Name);
143
144  /// \brief Number of modules loaded
145  unsigned size() const { return Chain.size(); }
146  /// \brief Attempts to create a new module and add it to the list of known
147  /// modules.
148  ///
149  /// \param FileName The file name of the module to be loaded.
150  ///
151  /// \param Type The kind of module being loaded.
152  ///
153  /// \param ImportLoc The location at which the module is imported.
154  ///
155  /// \param ImportedBy The module that is importing this module, or NULL if
156  /// this module is imported directly by the user.
157  ///
158  /// \param Generation The generation in which this module was loaded.
159  ///
160  /// \param ErrorStr Will be set to a non-empty string if any errors occurred
161  /// while trying to load the module.
162  ///
163  /// \return A pointer to the module that corresponds to this file name,
164  /// and a boolean indicating whether the module was newly added.
165  std::pair<ModuleFile *, bool>
166  addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc,
167            ModuleFile *ImportedBy, unsigned Generation,
168            std::string &ErrorStr);
169
170  /// \brief Remove the given set of modules.
171  void removeModules(ModuleIterator first, ModuleIterator last);
172
173  /// \brief Add an in-memory buffer the list of known buffers
174  void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
175
176  /// \brief Set the global module index.
177  void setGlobalIndex(GlobalModuleIndex *Index);
178
179  /// \brief Visit each of the modules.
180  ///
181  /// This routine visits each of the modules, starting with the
182  /// "root" modules that no other loaded modules depend on, and
183  /// proceeding to the leaf modules, visiting each module only once
184  /// during the traversal.
185  ///
186  /// This traversal is intended to support various "lookup"
187  /// operations that can find data in any of the loaded modules.
188  ///
189  /// \param Visitor A visitor function that will be invoked with each
190  /// module and the given user data pointer. The return value must be
191  /// convertible to bool; when false, the visitation continues to
192  /// modules that the current module depends on. When true, the
193  /// visitation skips any modules that the current module depends on.
194  ///
195  /// \param UserData User data associated with the visitor object, which
196  /// will be passed along to the visitor.
197  ///
198  /// \param ModuleFilesHit If non-NULL, contains the set of module files
199  /// that we know we need to visit because the global module index told us to.
200  /// Any module that is known to both the global module index and the module
201  /// manager that is *not* in this set can be skipped.
202  void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
203             llvm::SmallPtrSet<const FileEntry *, 4> *ModuleFilesHit = 0);
204
205  /// \brief Visit each of the modules with a depth-first traversal.
206  ///
207  /// This routine visits each of the modules known to the module
208  /// manager using a depth-first search, starting with the first
209  /// loaded module. The traversal invokes the callback both before
210  /// traversing the children (preorder traversal) and after
211  /// traversing the children (postorder traversal).
212  ///
213  /// \param Visitor A visitor function that will be invoked with each
214  /// module and given a \c Preorder flag that indicates whether we're
215  /// visiting the module before or after visiting its children.  The
216  /// visitor may return true at any time to abort the depth-first
217  /// visitation.
218  ///
219  /// \param UserData User data ssociated with the visitor object,
220  /// which will be passed along to the user.
221  void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
222                                       void *UserData),
223                       void *UserData);
224
225  /// \brief View the graphviz representation of the module graph.
226  void viewGraph();
227};
228
229} } // end namespace clang::serialization
230
231#endif
232