ModuleManager.h revision 188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15
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
66public:
67  typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator;
68  typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator;
69  typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator;
70  typedef std::pair<uint32_t, StringRef> ModuleOffset;
71
72  explicit ModuleManager(FileManager &FileMgr);
73  ~ModuleManager();
74
75  /// \brief Forward iterator to traverse all loaded modules.  This is reverse
76  /// source-order.
77  ModuleIterator begin() { return Chain.begin(); }
78  /// \brief Forward iterator end-point to traverse all loaded modules
79  ModuleIterator end() { return Chain.end(); }
80
81  /// \brief Const forward iterator to traverse all loaded modules.  This is
82  /// in reverse source-order.
83  ModuleConstIterator begin() const { return Chain.begin(); }
84  /// \brief Const forward iterator end-point to traverse all loaded modules
85  ModuleConstIterator end() const { return Chain.end(); }
86
87  /// \brief Reverse iterator to traverse all loaded modules.  This is in
88  /// source order.
89  ModuleReverseIterator rbegin() { return Chain.rbegin(); }
90  /// \brief Reverse iterator end-point to traverse all loaded modules.
91  ModuleReverseIterator rend() { return Chain.rend(); }
92
93  /// \brief Returns the primary module associated with the manager, that is,
94  /// the first module loaded
95  ModuleFile &getPrimaryModule() { return *Chain[0]; }
96
97  /// \brief Returns the primary module associated with the manager, that is,
98  /// the first module loaded.
99  ModuleFile &getPrimaryModule() const { return *Chain[0]; }
100
101  /// \brief Returns the module associated with the given index
102  ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
103
104  /// \brief Returns the module associated with the given name
105  ModuleFile *lookup(StringRef Name);
106
107  /// \brief Returns the in-memory (virtual file) buffer with the given name
108  llvm::MemoryBuffer *lookupBuffer(StringRef Name);
109
110  /// \brief Number of modules loaded
111  unsigned size() const { return Chain.size(); }
112  /// \brief Attempts to create a new module and add it to the list of known
113  /// modules.
114  ///
115  /// \param FileName The file name of the module to be loaded.
116  ///
117  /// \param Type The kind of module being loaded.
118  ///
119  /// \param ImportLoc The location at which the module is imported.
120  ///
121  /// \param ImportedBy The module that is importing this module, or NULL if
122  /// this module is imported directly by the user.
123  ///
124  /// \param Generation The generation in which this module was loaded.
125  ///
126  /// \param ErrorStr Will be set to a non-empty string if any errors occurred
127  /// while trying to load the module.
128  ///
129  /// \return A pointer to the module that corresponds to this file name,
130  /// and a boolean indicating whether the module was newly added.
131  std::pair<ModuleFile *, bool>
132  addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc,
133            ModuleFile *ImportedBy, unsigned Generation,
134            std::string &ErrorStr);
135
136  /// \brief Remove the given set of modules.
137  void removeModules(ModuleIterator first, ModuleIterator last);
138
139  /// \brief Add an in-memory buffer the list of known buffers
140  void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
141
142  /// \brief Set the global module index.
143  void setGlobalIndex(GlobalModuleIndex *Index);
144
145  /// \brief Visit each of the modules.
146  ///
147  /// This routine visits each of the modules, starting with the
148  /// "root" modules that no other loaded modules depend on, and
149  /// proceeding to the leaf modules, visiting each module only once
150  /// during the traversal.
151  ///
152  /// This traversal is intended to support various "lookup"
153  /// operations that can find data in any of the loaded modules.
154  ///
155  /// \param Visitor A visitor function that will be invoked with each
156  /// module and the given user data pointer. The return value must be
157  /// convertible to bool; when false, the visitation continues to
158  /// modules that the current module depends on. When true, the
159  /// visitation skips any modules that the current module depends on.
160  ///
161  /// \param UserData User data associated with the visitor object, which
162  /// will be passed along to the visitor.
163  ///
164  /// \param ModuleFilesHit If non-NULL, contains the set of module files
165  /// that we know we need to visit because the global module index told us to.
166  /// Any module that is known to both the global module index and the module
167  /// manager that is *not* in this set can be skipped.
168  void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
169             llvm::SmallPtrSet<const FileEntry *, 4> *ModuleFilesHit = 0);
170
171  /// \brief Visit each of the modules with a depth-first traversal.
172  ///
173  /// This routine visits each of the modules known to the module
174  /// manager using a depth-first search, starting with the first
175  /// loaded module. The traversal invokes the callback both before
176  /// traversing the children (preorder traversal) and after
177  /// traversing the children (postorder traversal).
178  ///
179  /// \param Visitor A visitor function that will be invoked with each
180  /// module and given a \c Preorder flag that indicates whether we're
181  /// visiting the module before or after visiting its children.  The
182  /// visitor may return true at any time to abort the depth-first
183  /// visitation.
184  ///
185  /// \param UserData User data ssociated with the visitor object,
186  /// which will be passed along to the user.
187  void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
188                                       void *UserData),
189                       void *UserData);
190
191  /// \brief View the graphviz representation of the module graph.
192  void viewGraph();
193};
194
195} } // end namespace clang::serialization
196
197#endif
198