ModuleManager.h revision 677e15ffee2ecc9c1c8f46fd77cab4b5afb59640
198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===--- ModuleManager.cpp - Module Manager ---------------------*- C++ -*-===//
298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//                     The LLVM Compiler Infrastructure
498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// This file is distributed under the University of Illinois Open Source
698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor// License. See LICENSE.TXT for details.
798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===//
998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
1098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//  This file defines the ModuleManager class, which manages a set of loaded
1198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//  modules for the ASTReader.
1298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//
1398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor//===----------------------------------------------------------------------===//
1498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
1598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#ifndef LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H
1698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#define LLVM_CLANG_SERIALIZATION_MODULE_MANAGER_H
1798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
1898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "clang/Basic/FileManager.h"
19677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor#include "clang/Serialization/GlobalModuleIndex.h"
2030a2e16f6c27f888dd11eba6bbbae1e980078fcbChandler Carruth#include "clang/Serialization/Module.h"
2198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#include "llvm/ADT/DenseMap.h"
2298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
2398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregornamespace clang {
2498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
25677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregorclass ModuleMap;
26188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
2798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregornamespace serialization {
2898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
2998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor/// \brief Manages the set of modules loaded by an AST reader.
30677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregorclass ModuleManager : public ModuleFileNameResolver {
3198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief The chain of AST files. The first entry is the one named by the
3298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// user, the last one is the one that doesn't depend on anything further.
33cfa88f893915ceb8ae4ce2f17c46c24a4d67502fDmitri Gribenko  SmallVector<ModuleFile *, 2> Chain;
3498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
3598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief All loaded modules, indexed by name.
361a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  llvm::DenseMap<const FileEntry *, ModuleFile *> Modules;
3798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
3898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief FileManager that handles translating between filenames and
3998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// FileEntry *.
40d64c26f6676eef69d1713f353ca8a3c2fe963f17Argyrios Kyrtzidis  FileManager &FileMgr;
4198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
4298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief A lookup of in-memory (virtual file) buffers
4398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::DenseMap<const FileEntry *, llvm::MemoryBuffer *> InMemoryBuffers;
44d07865b42dcb32154c75134fded51b38cc55a0c4Douglas Gregor
45d07865b42dcb32154c75134fded51b38cc55a0c4Douglas Gregor  /// \brief The visitation order.
46d07865b42dcb32154c75134fded51b38cc55a0c4Douglas Gregor  SmallVector<ModuleFile *, 4> VisitOrder;
47d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
48188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// \brief The list of module files that both we and the global module index
49188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// know about.
50188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  ///
51188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// Either the global index or the module manager may have modules that the
52188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// other does not know about, because the global index can be out-of-date
53188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// (in which case the module manager could have modules it does not) and
54188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// this particular translation unit might not have loaded all of the modules
55188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// known to the global index.
56188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  SmallVector<ModuleFile *, 4> ModulesInCommonWithGlobalIndex;
57188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
58188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// \brief The global module index, if one is attached.
59188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  ///
60188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// The global module index will actually be owned by the ASTReader; this is
61188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// just an non-owning pointer.
62188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  GlobalModuleIndex *GlobalIndex;
63188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
64188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// \brief Update the set of modules files we know about known to the global index.
65188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  void updateModulesInCommonWithGlobalIndex();
66188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
67d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  /// \brief State used by the "visit" operation to avoid malloc traffic in
68d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  /// calls to visit().
69d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  struct VisitState {
70d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    explicit VisitState(unsigned N)
71d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor      : VisitNumber(N, 0), NextVisitNumber(1), NextState(0)
72d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    {
73d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor      Stack.reserve(N);
74d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    }
75d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
76d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    ~VisitState() {
77d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor      delete NextState;
78d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    }
79d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
80d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// \brief The stack used when marking the imports of a particular module
81d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// as not-to-be-visited.
82d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    SmallVector<ModuleFile *, 4> Stack;
83d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
84d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// \brief The visit number of each module file, which indicates when
85d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// this module file was last visited.
86d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    SmallVector<unsigned, 4> VisitNumber;
87d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
88d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// \brief The next visit number to use to mark visited module files.
89d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    unsigned NextVisitNumber;
90d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
91d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    /// \brief The next visit state.
92d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor    VisitState *NextState;
93d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  };
94d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
95d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  /// \brief The first visit() state in the chain.
96d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  VisitState *FirstVisitState;
97d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
98d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  VisitState *allocateVisitState();
99d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor  void returnVisitState(VisitState *State);
100d3cf5fba332fc77f7e72ef58077822606718671dDouglas Gregor
10198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregorpublic:
1021a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  typedef SmallVector<ModuleFile*, 2>::iterator ModuleIterator;
1031a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  typedef SmallVector<ModuleFile*, 2>::const_iterator ModuleConstIterator;
1041a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  typedef SmallVector<ModuleFile*, 2>::reverse_iterator ModuleReverseIterator;
10598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  typedef std::pair<uint32_t, StringRef> ModuleOffset;
10698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
107d64c26f6676eef69d1713f353ca8a3c2fe963f17Argyrios Kyrtzidis  explicit ModuleManager(FileManager &FileMgr);
10898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ~ModuleManager();
10998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
11098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Forward iterator to traverse all loaded modules.  This is reverse
11198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// source-order.
11298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleIterator begin() { return Chain.begin(); }
11398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Forward iterator end-point to traverse all loaded modules
11498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleIterator end() { return Chain.end(); }
11598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
11698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Const forward iterator to traverse all loaded modules.  This is
11798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// in reverse source-order.
11898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleConstIterator begin() const { return Chain.begin(); }
11998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Const forward iterator end-point to traverse all loaded modules
12098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleConstIterator end() const { return Chain.end(); }
12198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
12298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Reverse iterator to traverse all loaded modules.  This is in
12398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// source order.
12498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleReverseIterator rbegin() { return Chain.rbegin(); }
12598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Reverse iterator end-point to traverse all loaded modules.
12698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ModuleReverseIterator rend() { return Chain.rend(); }
12798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
12898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Returns the primary module associated with the manager, that is,
12998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// the first module loaded
1301a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  ModuleFile &getPrimaryModule() { return *Chain[0]; }
13198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
13298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Returns the primary module associated with the manager, that is,
13398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// the first module loaded.
1341a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  ModuleFile &getPrimaryModule() const { return *Chain[0]; }
13598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
13698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Returns the module associated with the given index
1371a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  ModuleFile &operator[](unsigned Index) const { return *Chain[Index]; }
13898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
13998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Returns the module associated with the given name
1401a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  ModuleFile *lookup(StringRef Name);
14198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
14298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Returns the in-memory (virtual file) buffer with the given name
14398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  llvm::MemoryBuffer *lookupBuffer(StringRef Name);
14498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
14598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Number of modules loaded
14698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  unsigned size() const { return Chain.size(); }
147677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor
148677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \brief The result of attempting to add a new module.
149677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  enum AddModuleResult {
150677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    /// \brief The module file had already been loaded.
151677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    AlreadyLoaded,
152677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    /// \brief The module file was just loaded in response to this call.
153677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    NewlyLoaded,
154677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    /// \brief The module file is missing.
155677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    Missing,
156677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    /// \brief The module file is out-of-date.
157677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor    OutOfDate
158677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  };
159677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor
16098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Attempts to create a new module and add it to the list of known
16198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// modules.
16298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
16398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param FileName The file name of the module to be loaded.
16498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
16598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param Type The kind of module being loaded.
16698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
16787e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor  /// \param ImportLoc The location at which the module is imported.
16887e2cfcec7231daaa3f367dc32df74b411251e46Douglas Gregor  ///
16998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param ImportedBy The module that is importing this module, or NULL if
17098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// this module is imported directly by the user.
17198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
172057df20b3107cef764052d271c89b8591b98b3ceDouglas Gregor  /// \param Generation The generation in which this module was loaded.
173057df20b3107cef764052d271c89b8591b98b3ceDouglas Gregor  ///
174677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param ExpectedSize The expected size of the module file, used for
175677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// validation. This will be zero if unknown.
176677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
177677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param ExpectedModTime The expected modification time of the module
178677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// file, used for validation. This will be zero if unknown.
179677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
180677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param Module A pointer to the module file if the module was successfully
181677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// loaded.
182677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
18398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param ErrorStr Will be set to a non-empty string if any errors occurred
18498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// while trying to load the module.
18598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
18698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \return A pointer to the module that corresponds to this file name,
187677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// and a value indicating whether the module was loaded.
188677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  AddModuleResult addModule(StringRef FileName, ModuleKind Type,
189677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                            SourceLocation ImportLoc,
190677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                            ModuleFile *ImportedBy, unsigned Generation,
191677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                            off_t ExpectedSize, time_t ExpectedModTime,
192677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                            ModuleFile *&Module,
193677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                            std::string &ErrorStr);
1947cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor
1957cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor  /// \brief Remove the given set of modules.
196677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  void removeModules(ModuleIterator first, ModuleIterator last,
197677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                     ModuleMap *modMap);
1987cdd28162dc7ade4b14bf237e87b4bbc17b2f023Douglas Gregor
19998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Add an in-memory buffer the list of known buffers
20098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  void addInMemoryBuffer(StringRef FileName, llvm::MemoryBuffer *Buffer);
201188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
202188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// \brief Set the global module index.
203188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  void setGlobalIndex(GlobalModuleIndex *Index);
204188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor
20598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Visit each of the modules.
20698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
20798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// This routine visits each of the modules, starting with the
20898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// "root" modules that no other loaded modules depend on, and
20998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// proceeding to the leaf modules, visiting each module only once
21098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// during the traversal.
21198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
21298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// This traversal is intended to support various "lookup"
21398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// operations that can find data in any of the loaded modules.
21498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
21598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param Visitor A visitor function that will be invoked with each
21698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// module and the given user data pointer. The return value must be
21798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// convertible to bool; when false, the visitation continues to
21898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// modules that the current module depends on. When true, the
21998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// visitation skips any modules that the current module depends on.
22098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
22198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param UserData User data associated with the visitor object, which
22298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// will be passed along to the visitor.
223188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  ///
224188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// \param ModuleFilesHit If non-NULL, contains the set of module files
225188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// that we know we need to visit because the global module index told us to.
226188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// Any module that is known to both the global module index and the module
227188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  /// manager that is *not* in this set can be skipped.
228188bdcd1aaf5e9f457cec6851707d7dc3e7bbb15Douglas Gregor  void visit(bool (*Visitor)(ModuleFile &M, void *UserData), void *UserData,
229677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor             llvm::SmallPtrSet<ModuleFile *, 4> *ModuleFilesHit = 0);
23098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
23198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \brief Visit each of the modules with a depth-first traversal.
23298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
23398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// This routine visits each of the modules known to the module
23498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// manager using a depth-first search, starting with the first
23598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// loaded module. The traversal invokes the callback both before
23698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// traversing the children (preorder traversal) and after
23798339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// traversing the children (postorder traversal).
23898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
23998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param Visitor A visitor function that will be invoked with each
24098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// module and given a \c Preorder flag that indicates whether we're
24198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// visiting the module before or after visiting its children.  The
24298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// visitor may return true at any time to abort the depth-first
24398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// visitation.
24498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  ///
24598339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// \param UserData User data ssociated with the visitor object,
24698339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor  /// which will be passed along to the user.
2471a4761edca58c6b559de825b9abfb66f7f1ba94aDouglas Gregor  void visitDepthFirst(bool (*Visitor)(ModuleFile &M, bool Preorder,
24898339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor                                       void *UserData),
24998339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor                       void *UserData);
250677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor
251677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \brief Attempt to resolve the given module file name to a file entry.
252677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
253677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param FileName The name of the module file.
254677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
255677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param ExpectedSize The size that the module file is expected to have.
256677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// If the actual size differs, the resolver should return \c true.
257677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
258677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param ExpectedModTime The modification time that the module file is
259677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// expected to have. If the actual modification time differs, the resolver
260677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// should return \c true.
261677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
262677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \param File Will be set to the file if there is one, or null
263677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// otherwise.
264677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  ///
265677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// \returns True if a file exists but does not meet the size/
266677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// modification time criteria, false if the file is either available and
267677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  /// suitable, or is missing.
268677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  bool lookupModuleFile(StringRef FileName,
269677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                        off_t ExpectedSize,
270677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                        time_t ExpectedModTime,
271677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                        const FileEntry *&File);
272677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor
273677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor  virtual bool resolveModuleFileName(StringRef FileName,
274677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                                     off_t ExpectedSize,
275677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                                     time_t ExpectedModTime,
276677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor                                     ModuleFile *&File);
277677e15ffee2ecc9c1c8f46fd77cab4b5afb59640Douglas Gregor
2782492c89882b5c5ce03afb4704fee67b7eff8f5eeDouglas Gregor  /// \brief View the graphviz representation of the module graph.
2792492c89882b5c5ce03afb4704fee67b7eff8f5eeDouglas Gregor  void viewGraph();
28098339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor};
28198339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
28298339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor} } // end namespace clang::serialization
28398339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor
28498339b96a8089a6da715487e432c5abfca0ca0dfDouglas Gregor#endif
285