xref: /minix3/external/bsd/llvm/dist/clang/include/clang/Serialization/GlobalModuleIndex.h (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- GlobalModuleIndex.h - Global Module Index --------------*- C++ -*-===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file defines the GlobalModuleIndex class, which manages a global index
11f4a2713aSLionel Sambuc // containing all of the identifiers known to the various modules within a given
12f4a2713aSLionel Sambuc // subdirectory of the module cache. It is used to improve the performance of
13f4a2713aSLionel Sambuc // queries such as "do any modules know about this identifier?"
14f4a2713aSLionel Sambuc //
15f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
16*0a6a1f1dSLionel Sambuc #ifndef LLVM_CLANG_SERIALIZATION_GLOBALMODULEINDEX_H
17*0a6a1f1dSLionel Sambuc #define LLVM_CLANG_SERIALIZATION_GLOBALMODULEINDEX_H
18f4a2713aSLionel Sambuc 
19f4a2713aSLionel Sambuc #include "llvm/ADT/DenseMap.h"
20f4a2713aSLionel Sambuc #include "llvm/ADT/SmallPtrSet.h"
21f4a2713aSLionel Sambuc #include "llvm/ADT/SmallVector.h"
22f4a2713aSLionel Sambuc #include "llvm/ADT/StringMap.h"
23f4a2713aSLionel Sambuc #include "llvm/ADT/StringRef.h"
24*0a6a1f1dSLionel Sambuc #include <memory>
25f4a2713aSLionel Sambuc #include <utility>
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc namespace llvm {
28f4a2713aSLionel Sambuc class BitstreamCursor;
29f4a2713aSLionel Sambuc class MemoryBuffer;
30f4a2713aSLionel Sambuc }
31f4a2713aSLionel Sambuc 
32f4a2713aSLionel Sambuc namespace clang {
33f4a2713aSLionel Sambuc 
34f4a2713aSLionel Sambuc class DirectoryEntry;
35f4a2713aSLionel Sambuc class FileEntry;
36f4a2713aSLionel Sambuc class FileManager;
37f4a2713aSLionel Sambuc class IdentifierIterator;
38f4a2713aSLionel Sambuc 
39f4a2713aSLionel Sambuc namespace serialization {
40f4a2713aSLionel Sambuc   class ModuleFile;
41f4a2713aSLionel Sambuc }
42f4a2713aSLionel Sambuc 
43f4a2713aSLionel Sambuc using llvm::SmallVector;
44f4a2713aSLionel Sambuc using llvm::SmallVectorImpl;
45f4a2713aSLionel Sambuc using llvm::StringRef;
46f4a2713aSLionel Sambuc using serialization::ModuleFile;
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc /// \brief A global index for a set of module files, providing information about
49f4a2713aSLionel Sambuc /// the identifiers within those module files.
50f4a2713aSLionel Sambuc ///
51f4a2713aSLionel Sambuc /// The global index is an aid for name lookup into modules, offering a central
52f4a2713aSLionel Sambuc /// place where one can look for identifiers determine which
53f4a2713aSLionel Sambuc /// module files contain any information about that identifier. This
54f4a2713aSLionel Sambuc /// allows the client to restrict the search to only those module files known
55f4a2713aSLionel Sambuc /// to have a information about that identifier, improving performance. Moreover,
56f4a2713aSLionel Sambuc /// the global module index may know about module files that have not been
57f4a2713aSLionel Sambuc /// imported, and can be queried to determine which modules the current
58f4a2713aSLionel Sambuc /// translation could or should load to fix a problem.
59f4a2713aSLionel Sambuc class GlobalModuleIndex {
60f4a2713aSLionel Sambuc   /// \brief Buffer containing the index file, which is lazily accessed so long
61f4a2713aSLionel Sambuc   /// as the global module index is live.
62*0a6a1f1dSLionel Sambuc   std::unique_ptr<llvm::MemoryBuffer> Buffer;
63f4a2713aSLionel Sambuc 
64f4a2713aSLionel Sambuc   /// \brief The hash table.
65f4a2713aSLionel Sambuc   ///
66f4a2713aSLionel Sambuc   /// This pointer actually points to a IdentifierIndexTable object,
67f4a2713aSLionel Sambuc   /// but that type is only accessible within the implementation of
68f4a2713aSLionel Sambuc   /// GlobalModuleIndex.
69f4a2713aSLionel Sambuc   void *IdentifierIndex;
70f4a2713aSLionel Sambuc 
71f4a2713aSLionel Sambuc   /// \brief Information about a given module file.
72f4a2713aSLionel Sambuc   struct ModuleInfo {
ModuleInfoModuleInfo73f4a2713aSLionel Sambuc     ModuleInfo() : File(), Size(), ModTime() { }
74f4a2713aSLionel Sambuc 
75f4a2713aSLionel Sambuc     /// \brief The module file, once it has been resolved.
76f4a2713aSLionel Sambuc     ModuleFile *File;
77f4a2713aSLionel Sambuc 
78f4a2713aSLionel Sambuc     /// \brief The module file name.
79f4a2713aSLionel Sambuc     std::string FileName;
80f4a2713aSLionel Sambuc 
81f4a2713aSLionel Sambuc     /// \brief Size of the module file at the time the global index was built.
82f4a2713aSLionel Sambuc     off_t Size;
83f4a2713aSLionel Sambuc 
84f4a2713aSLionel Sambuc     /// \brief Modification time of the module file at the time the global
85f4a2713aSLionel Sambuc     /// index was built.
86f4a2713aSLionel Sambuc     time_t ModTime;
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc     /// \brief The module IDs on which this module directly depends.
89f4a2713aSLionel Sambuc     /// FIXME: We don't really need a vector here.
90f4a2713aSLionel Sambuc     llvm::SmallVector<unsigned, 4> Dependencies;
91f4a2713aSLionel Sambuc   };
92f4a2713aSLionel Sambuc 
93f4a2713aSLionel Sambuc   /// \brief A mapping from module IDs to information about each module.
94f4a2713aSLionel Sambuc   ///
95f4a2713aSLionel Sambuc   /// This vector may have gaps, if module files have been removed or have
96f4a2713aSLionel Sambuc   /// been updated since the index was built. A gap is indicated by an empty
97f4a2713aSLionel Sambuc   /// file name.
98f4a2713aSLionel Sambuc   llvm::SmallVector<ModuleInfo, 16> Modules;
99f4a2713aSLionel Sambuc 
100f4a2713aSLionel Sambuc   /// \brief Lazily-populated mapping from module files to their
101f4a2713aSLionel Sambuc   /// corresponding index into the \c Modules vector.
102f4a2713aSLionel Sambuc   llvm::DenseMap<ModuleFile *, unsigned> ModulesByFile;
103f4a2713aSLionel Sambuc 
104f4a2713aSLionel Sambuc   /// \brief The set of modules that have not yet been resolved.
105f4a2713aSLionel Sambuc   ///
106f4a2713aSLionel Sambuc   /// The string is just the name of the module itself, which maps to the
107f4a2713aSLionel Sambuc   /// module ID.
108f4a2713aSLionel Sambuc   llvm::StringMap<unsigned> UnresolvedModules;
109f4a2713aSLionel Sambuc 
110f4a2713aSLionel Sambuc   /// \brief The number of identifier lookups we performed.
111f4a2713aSLionel Sambuc   unsigned NumIdentifierLookups;
112f4a2713aSLionel Sambuc 
113f4a2713aSLionel Sambuc   /// \brief The number of identifier lookup hits, where we recognize the
114f4a2713aSLionel Sambuc   /// identifier.
115f4a2713aSLionel Sambuc   unsigned NumIdentifierLookupHits;
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc   /// \brief Internal constructor. Use \c readIndex() to read an index.
118*0a6a1f1dSLionel Sambuc   explicit GlobalModuleIndex(std::unique_ptr<llvm::MemoryBuffer> Buffer,
119f4a2713aSLionel Sambuc                              llvm::BitstreamCursor Cursor);
120f4a2713aSLionel Sambuc 
121f4a2713aSLionel Sambuc   GlobalModuleIndex(const GlobalModuleIndex &) LLVM_DELETED_FUNCTION;
122f4a2713aSLionel Sambuc   GlobalModuleIndex &operator=(const GlobalModuleIndex &) LLVM_DELETED_FUNCTION;
123f4a2713aSLionel Sambuc 
124f4a2713aSLionel Sambuc public:
125f4a2713aSLionel Sambuc   ~GlobalModuleIndex();
126f4a2713aSLionel Sambuc 
127f4a2713aSLionel Sambuc   /// \brief An error code returned when trying to read an index.
128f4a2713aSLionel Sambuc   enum ErrorCode {
129f4a2713aSLionel Sambuc     /// \brief No error occurred.
130f4a2713aSLionel Sambuc     EC_None,
131f4a2713aSLionel Sambuc     /// \brief No index was found.
132f4a2713aSLionel Sambuc     EC_NotFound,
133f4a2713aSLionel Sambuc     /// \brief Some other process is currently building the index; it is not
134f4a2713aSLionel Sambuc     /// available yet.
135f4a2713aSLionel Sambuc     EC_Building,
136f4a2713aSLionel Sambuc     /// \brief There was an unspecified I/O error reading or writing the index.
137f4a2713aSLionel Sambuc     EC_IOError
138f4a2713aSLionel Sambuc   };
139f4a2713aSLionel Sambuc 
140f4a2713aSLionel Sambuc   /// \brief Read a global index file for the given directory.
141f4a2713aSLionel Sambuc   ///
142f4a2713aSLionel Sambuc   /// \param Path The path to the specific module cache where the module files
143f4a2713aSLionel Sambuc   /// for the intended configuration reside.
144f4a2713aSLionel Sambuc   ///
145f4a2713aSLionel Sambuc   /// \returns A pair containing the global module index (if it exists) and
146f4a2713aSLionel Sambuc   /// the error code.
147f4a2713aSLionel Sambuc   static std::pair<GlobalModuleIndex *, ErrorCode>
148f4a2713aSLionel Sambuc   readIndex(StringRef Path);
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc   /// \brief Returns an iterator for identifiers stored in the index table.
151f4a2713aSLionel Sambuc   ///
152f4a2713aSLionel Sambuc   /// The caller accepts ownership of the returned object.
153f4a2713aSLionel Sambuc   IdentifierIterator *createIdentifierIterator() const;
154f4a2713aSLionel Sambuc 
155f4a2713aSLionel Sambuc   /// \brief Retrieve the set of modules that have up-to-date indexes.
156f4a2713aSLionel Sambuc   ///
157f4a2713aSLionel Sambuc   /// \param ModuleFiles Will be populated with the set of module files that
158f4a2713aSLionel Sambuc   /// have been indexed.
159f4a2713aSLionel Sambuc   void getKnownModules(SmallVectorImpl<ModuleFile *> &ModuleFiles);
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc   /// \brief Retrieve the set of module files on which the given module file
162f4a2713aSLionel Sambuc   /// directly depends.
163f4a2713aSLionel Sambuc   void getModuleDependencies(ModuleFile *File,
164f4a2713aSLionel Sambuc                              SmallVectorImpl<ModuleFile *> &Dependencies);
165f4a2713aSLionel Sambuc 
166f4a2713aSLionel Sambuc   /// \brief A set of module files in which we found a result.
167f4a2713aSLionel Sambuc   typedef llvm::SmallPtrSet<ModuleFile *, 4> HitSet;
168f4a2713aSLionel Sambuc 
169f4a2713aSLionel Sambuc   /// \brief Look for all of the module files with information about the given
170f4a2713aSLionel Sambuc   /// identifier, e.g., a global function, variable, or type with that name.
171f4a2713aSLionel Sambuc   ///
172f4a2713aSLionel Sambuc   /// \param Name The identifier to look for.
173f4a2713aSLionel Sambuc   ///
174f4a2713aSLionel Sambuc   /// \param Hits Will be populated with the set of module files that have
175f4a2713aSLionel Sambuc   /// information about this name.
176f4a2713aSLionel Sambuc   ///
177f4a2713aSLionel Sambuc   /// \returns true if the identifier is known to the index, false otherwise.
178f4a2713aSLionel Sambuc   bool lookupIdentifier(StringRef Name, HitSet &Hits);
179f4a2713aSLionel Sambuc 
180f4a2713aSLionel Sambuc   /// \brief Note that the given module file has been loaded.
181f4a2713aSLionel Sambuc   ///
182f4a2713aSLionel Sambuc   /// \returns false if the global module index has information about this
183f4a2713aSLionel Sambuc   /// module file, and true otherwise.
184f4a2713aSLionel Sambuc   bool loadedModuleFile(ModuleFile *File);
185f4a2713aSLionel Sambuc 
186f4a2713aSLionel Sambuc   /// \brief Print statistics to standard error.
187f4a2713aSLionel Sambuc   void printStats();
188f4a2713aSLionel Sambuc 
189*0a6a1f1dSLionel Sambuc   /// \brief Print debugging view to standard error.
190*0a6a1f1dSLionel Sambuc   void dump();
191*0a6a1f1dSLionel Sambuc 
192f4a2713aSLionel Sambuc   /// \brief Write a global index into the given
193f4a2713aSLionel Sambuc   ///
194f4a2713aSLionel Sambuc   /// \param FileMgr The file manager to use to load module files.
195f4a2713aSLionel Sambuc   ///
196f4a2713aSLionel Sambuc   /// \param Path The path to the directory containing module files, into
197f4a2713aSLionel Sambuc   /// which the global index will be written.
198f4a2713aSLionel Sambuc   static ErrorCode writeIndex(FileManager &FileMgr, StringRef Path);
199f4a2713aSLionel Sambuc };
200f4a2713aSLionel Sambuc 
201f4a2713aSLionel Sambuc }
202f4a2713aSLionel Sambuc 
203f4a2713aSLionel Sambuc #endif
204