ModuleManager.h revision d07865b42dcb32154c75134fded51b38cc55a0c4
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
24namespace serialization {
25
26/// \brief Manages the set of modules loaded by an AST reader.
27class ModuleManager {
28  /// \brief The chain of AST files. The first entry is the one named by the
29  /// user, the last one is the one that doesn't depend on anything further.
30  SmallVector<ModuleFile *, 2> Chain;
31
32  /// \brief All loaded modules, indexed by name.
33  llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
34
35  /// \brief FileManager that handles translating between filenames and
36  /// FileEntry *.
37  FileManager &FileMgr;
38
39  /// \brief A lookup of in-memory (virtual file) buffers
40  llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers;
41
42  /// \brief The visitation order.
43  SmallVector<ModuleFile *, 4> VisitOrder;
44
45public:
46  typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator;
47  typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator;
48  typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator;
49  typedef std::pair<uint32_t, StringRef> ModuleOffset;
50
51  explicit ModuleManager(FileManager &FileMgr);
52  ~ModuleManager();
53
54  /// \brief Forward iterator to traverse all loaded modules.  This is reverse
55  /// source-order.
56  ModuleIterator begin() { return Chain.begin(); }
57  /// \brief Forward iterator end-point to traverse all loaded modules
58  ModuleIterator end() { return Chain.end(); }
59
60  /// \brief Const forward iterator to traverse all loaded modules.  This is
61  /// in reverse source-order.
62  ModuleConstIterator begin() const { return Chain.begin(); }
63  /// \brief Const forward iterator end-point to traverse all loaded modules
64  ModuleConstIterator end() const { return Chain.end(); }
65
66  /// \brief Reverse iterator to traverse all loaded modules.  This is in
67  /// source order.
68  ModuleReverseIterator rbegin() { return Chain.rbegin(); }
69  /// \brief Reverse iterator end-point to traverse all loaded modules.
70  ModuleReverseIterator rend() { return Chain.rend(); }
71
72  /// \brief Returns the primary module associated with the manager, that is,
73  /// the first module loaded
74  ModuleFile &getPrimaryModule() { return *Chain[0]; }
75
76  /// \brief Returns the primary module associated with the manager, that is,
77  /// the first module loaded.
78  ModuleFile &getPrimaryModule() const { return *Chain[0]; }
79
80  /// \brief Returns the module associated with the given index
81  ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
82
83  /// \brief Returns the module associated with the given name
84  ModuleFile *lookup(StringRef Name);
85
86  /// \brief Returns the in-memory (virtual file) buffer with the given name
87  llvm::MemoryBuffer *lookupBuffer(StringRef Name);
88
89  /// \brief Number of modules loaded
90  unsigned size() const { return Chain.size(); }
91  /// \brief Attempts to create a new module and add it to the list of known
92  /// modules.
93  ///
94  /// \param FileName The file name of the module to be loaded.
95  ///
96  /// \param Type The kind of module being loaded.
97  ///
98  /// \param ImportLoc The location at which the module is imported.
99  ///
100  /// \param ImportedBy The module that is importing this module, or NULL if
101  /// this module is imported directly by the user.
102  ///
103  /// \param Generation The generation in which this module was loaded.
104  ///
105  /// \param ErrorStr Will be set to a non-empty string if any errors occurred
106  /// while trying to load the module.
107  ///
108  /// \return A pointer to the module that corresponds to this file name,
109  /// and a boolean indicating whether the module was newly added.
110  std::pair<ModuleFile *, bool>
111  addModule(StringRef FileName, ModuleKind Type, SourceLocation ImportLoc,
112            ModuleFile *ImportedBy, unsigned Generation,
113            std::string &ErrorStr);
114
115  /// \brief Remove the given set of modules.
116  void removeModules(ModuleIterator first, ModuleIterator last);
117
118  /// \brief Add an in-memory buffer the list of known buffers
119  void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
120
121  /// \brief Visit each of the modules.
122  ///
123  /// This routine visits each of the modules, starting with the
124  /// "root" modules that no other loaded modules depend on, and
125  /// proceeding to the leaf modules, visiting each module only once
126  /// during the traversal.
127  ///
128  /// This traversal is intended to support various "lookup"
129  /// operations that can find data in any of the loaded modules.
130  ///
131  /// \param Visitor A visitor function that will be invoked with each
132  /// module and the given user data pointer. The return value must be
133  /// convertible to bool; when false, the visitation continues to
134  /// modules that the current module depends on. When true, the
135  /// visitation skips any modules that the current module depends on.
136  ///
137  /// \param UserData User data associated with the visitor object, which
138  /// will be passed along to the visitor.
139  void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData);
140
141  /// \brief Visit each of the modules with a depth-first traversal.
142  ///
143  /// This routine visits each of the modules known to the module
144  /// manager using a depth-first search, starting with the first
145  /// loaded module. The traversal invokes the callback both before
146  /// traversing the children (preorder traversal) and after
147  /// traversing the children (postorder traversal).
148  ///
149  /// \param Visitor A visitor function that will be invoked with each
150  /// module and given a \c Preorder flag that indicates whether we're
151  /// visiting the module before or after visiting its children.  The
152  /// visitor may return true at any time to abort the depth-first
153  /// visitation.
154  ///
155  /// \param UserData User data ssociated with the visitor object,
156  /// which will be passed along to the user.
157  void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
158                                       void *UserData),
159                       void *UserData);
160
161  /// \brief View the graphviz representation of the module graph.
162  void viewGraph();
163};
164
165} } // end namespace clang::serialization
166
167#endif
168