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/Serialization/Module.h"
19 #include "clang/Basic/FileManager.h"
20 #include "llvm/ADT/DenseMap.h"
24 namespace serialization {
26 /// \brief Manages the set of modules loaded by an AST reader.
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 llvm::SmallVector<Module*, 2> Chain;
32 /// \brief All loaded modules, indexed by name.
33 llvm::DenseMap<const FileEntry *, Module *> Modules;
35 /// \brief FileManager that handles translating between filenames and
39 /// \brief A lookup of in-memory (virtual file) buffers
40 llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers;
43 typedef SmallVector<Module*, 2>::iterator ModuleIterator;
44 typedef SmallVector<Module*, 2>::const_iterator ModuleConstIterator;
45 typedef SmallVector<Module*, 2>::reverse_iterator ModuleReverseIterator;
46 typedef std::pair<uint32_t, StringRef> ModuleOffset;
48 ModuleManager(const FileSystemOptions &FSO);
51 /// \brief Forward iterator to traverse all loaded modules. This is reverse
53 ModuleIterator begin() { return Chain.begin(); }
54 /// \brief Forward iterator end-point to traverse all loaded modules
55 ModuleIterator end() { return Chain.end(); }
57 /// \brief Const forward iterator to traverse all loaded modules. This is
58 /// in reverse source-order.
59 ModuleConstIterator begin() const { return Chain.begin(); }
60 /// \brief Const forward iterator end-point to traverse all loaded modules
61 ModuleConstIterator end() const { return Chain.end(); }
63 /// \brief Reverse iterator to traverse all loaded modules. This is in
65 ModuleReverseIterator rbegin() { return Chain.rbegin(); }
66 /// \brief Reverse iterator end-point to traverse all loaded modules.
67 ModuleReverseIterator rend() { return Chain.rend(); }
69 /// \brief Returns the primary module associated with the manager, that is,
70 /// the first module loaded
71 Module &getPrimaryModule() { return *Chain[0]; }
73 /// \brief Returns the primary module associated with the manager, that is,
74 /// the first module loaded.
75 Module &getPrimaryModule() const { return *Chain[0]; }
77 /// \brief Returns the module associated with the given index
78 Module &operator[](unsigned Index) const { return *Chain[Index]; }
80 /// \brief Returns the module associated with the given name
81 Module *lookup(StringRef Name);
83 /// \brief Returns the in-memory (virtual file) buffer with the given name
84 llvm::MemoryBuffer *lookupBuffer(StringRef Name);
86 /// \brief Number of modules loaded
87 unsigned size() const { return Chain.size(); }
88 /// \brief Attempts to create a new module and add it to the list of known
91 /// \param FileName The file name of the module to be loaded.
93 /// \param Type The kind of module being loaded.
95 /// \param ImportedBy The module that is importing this module, or NULL if
96 /// this module is imported directly by the user.
98 /// \param ErrorStr Will be set to a non-empty string if any errors occurred
99 /// while trying to load the module.
101 /// \return A pointer to the module that corresponds to this file name,
102 /// and a boolean indicating whether the module was newly added.
103 std::pair<Module *, bool>
104 addModule(StringRef FileName, ModuleKind Type, Module *ImportedBy,
105 std::string &ErrorStr);
107 /// \brief Add an in-memory buffer the list of known buffers
108 void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
110 /// \brief Visit each of the modules.
112 /// This routine visits each of the modules, starting with the
113 /// "root" modules that no other loaded modules depend on, and
114 /// proceeding to the leaf modules, visiting each module only once
115 /// during the traversal.
117 /// This traversal is intended to support various "lookup"
118 /// operations that can find data in any of the loaded modules.
120 /// \param Visitor A visitor function that will be invoked with each
121 /// module and the given user data pointer. The return value must be
122 /// convertible to bool; when false, the visitation continues to
123 /// modules that the current module depends on. When true, the
124 /// visitation skips any modules that the current module depends on.
126 /// \param UserData User data associated with the visitor object, which
127 /// will be passed along to the visitor.
128 void visit(bool (*Visitor)(Module &M, void *UserData), void *UserData);
130 /// \brief Visit each of the modules with a depth-first traversal.
132 /// This routine visits each of the modules known to the module
133 /// manager using a depth-first search, starting with the first
134 /// loaded module. The traversal invokes the callback both before
135 /// traversing the children (preorder traversal) and after
136 /// traversing the children (postorder traversal).
138 /// \param Visitor A visitor function that will be invoked with each
139 /// module and given a \c Preorder flag that indicates whether we're
140 /// visiting the module before or after visiting its children. The
141 /// visitor may return true at any time to abort the depth-first
144 /// \param UserData User data ssociated with the visitor object,
145 /// which will be passed along to the user.
146 void visitDepthFirst(bool (*Visitor)(Module &M, bool Preorder,
150 /// \brief View the graphviz representation of the module graph.
154 } } // end namespace clang::serialization