1 //===--- Module.h - Module description --------------------------*- 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 Module class, which describes a module that has
11 //  been loaded from an AST file.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_H
16 #define LLVM_CLANG_SERIALIZATION_MODULE_H
17 
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Serialization/ASTBitCodes.h"
20 #include "clang/Serialization/ContinuousRangeMap.h"
21 #include "llvm/ADT/SetVector.h"
22 #include "llvm/Bitcode/BitstreamReader.h"
23 #include <memory>
24 #include <string>
25 
26 namespace llvm {
27 template <typename Info> class OnDiskChainedHashTable;
28 template <typename Info> class OnDiskIterableChainedHashTable;
29 }
30 
31 namespace clang {
32 
33 class FileEntry;
34 class DeclContext;
35 class Module;
36 
37 namespace serialization {
38 
39 namespace reader {
40   class ASTDeclContextNameLookupTrait;
41 }
42 
43 /// \brief Specifies the kind of module that has been loaded.
44 enum ModuleKind {
45   MK_ImplicitModule, ///< File is an implicitly-loaded module.
46   MK_ExplicitModule, ///< File is an explicitly-loaded module.
47   MK_PCH,            ///< File is a PCH file treated as such.
48   MK_Preamble,       ///< File is a PCH file treated as the preamble.
49   MK_MainFile        ///< File is a PCH file treated as the actual main file.
50 };
51 
52 /// \brief Information about the contents of a DeclContext.
53 struct DeclContextInfo {
DeclContextInfoDeclContextInfo54   DeclContextInfo()
55     : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
56 
57   llvm::OnDiskIterableChainedHashTable<reader::ASTDeclContextNameLookupTrait>
58     *NameLookupTableData; // an ASTDeclContextNameLookupTable.
59   const KindDeclIDPair *LexicalDecls;
60   unsigned NumLexicalDecls;
61 };
62 
63 /// \brief The input file that has been loaded from this AST file, along with
64 /// bools indicating whether this was an overridden buffer or if it was
65 /// out-of-date or not-found.
66 class InputFile {
67   enum {
68     Overridden = 1,
69     OutOfDate = 2,
70     NotFound = 3
71   };
72   llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
73 
74 public:
InputFile()75   InputFile() {}
76   InputFile(const FileEntry *File,
77             bool isOverridden = false, bool isOutOfDate = false) {
78     assert(!(isOverridden && isOutOfDate) &&
79            "an overridden cannot be out-of-date");
80     unsigned intVal = 0;
81     if (isOverridden)
82       intVal = Overridden;
83     else if (isOutOfDate)
84       intVal = OutOfDate;
85     Val.setPointerAndInt(File, intVal);
86   }
87 
getNotFound()88   static InputFile getNotFound() {
89     InputFile File;
90     File.Val.setInt(NotFound);
91     return File;
92   }
93 
getFile()94   const FileEntry *getFile() const { return Val.getPointer(); }
isOverridden()95   bool isOverridden() const { return Val.getInt() == Overridden; }
isOutOfDate()96   bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
isNotFound()97   bool isNotFound() const { return Val.getInt() == NotFound; }
98 };
99 
100 typedef unsigned ASTFileSignature;
101 
102 /// \brief Information about a module that has been loaded by the ASTReader.
103 ///
104 /// Each instance of the Module class corresponds to a single AST file, which
105 /// may be a precompiled header, precompiled preamble, a module, or an AST file
106 /// of some sort loaded as the main file, all of which are specific formulations
107 /// of the general notion of a "module". A module may depend on any number of
108 /// other modules.
109 class ModuleFile {
110 public:
111   ModuleFile(ModuleKind Kind, unsigned Generation);
112   ~ModuleFile();
113 
114   // === General information ===
115 
116   /// \brief The index of this module in the list of modules.
117   unsigned Index;
118 
119   /// \brief The type of this module.
120   ModuleKind Kind;
121 
122   /// \brief The file name of the module file.
123   std::string FileName;
124 
125   /// \brief The name of the module.
126   std::string ModuleName;
127 
128   /// \brief The base directory of the module.
129   std::string BaseDirectory;
130 
getTimestampFilename()131   std::string getTimestampFilename() const {
132     return FileName + ".timestamp";
133   }
134 
135   /// \brief The original source file name that was used to build the
136   /// primary AST file, which may have been modified for
137   /// relocatable-pch support.
138   std::string OriginalSourceFileName;
139 
140   /// \brief The actual original source file name that was used to
141   /// build this AST file.
142   std::string ActualOriginalSourceFileName;
143 
144   /// \brief The file ID for the original source file that was used to
145   /// build this AST file.
146   FileID OriginalSourceFileID;
147 
148   /// \brief The directory that the PCH was originally created in. Used to
149   /// allow resolving headers even after headers+PCH was moved to a new path.
150   std::string OriginalDir;
151 
152   std::string ModuleMapPath;
153 
154   /// \brief Whether this precompiled header is a relocatable PCH file.
155   bool RelocatablePCH;
156 
157   /// \brief The file entry for the module file.
158   const FileEntry *File;
159 
160   /// \brief The signature of the module file, which may be used along with size
161   /// and modification time to identify this particular file.
162   ASTFileSignature Signature;
163 
164   /// \brief Whether this module has been directly imported by the
165   /// user.
166   bool DirectlyImported;
167 
168   /// \brief The generation of which this module file is a part.
169   unsigned Generation;
170 
171   /// \brief The memory buffer that stores the data associated with
172   /// this AST file.
173   std::unique_ptr<llvm::MemoryBuffer> Buffer;
174 
175   /// \brief The size of this file, in bits.
176   uint64_t SizeInBits;
177 
178   /// \brief The global bit offset (or base) of this module
179   uint64_t GlobalBitOffset;
180 
181   /// \brief The bitstream reader from which we'll read the AST file.
182   llvm::BitstreamReader StreamFile;
183 
184   /// \brief The main bitstream cursor for the main block.
185   llvm::BitstreamCursor Stream;
186 
187   /// \brief The source location where the module was explicitly or implicitly
188   /// imported in the local translation unit.
189   ///
190   /// If module A depends on and imports module B, both modules will have the
191   /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
192   /// source location inside module A).
193   ///
194   /// WARNING: This is largely useless. It doesn't tell you when a module was
195   /// made visible, just when the first submodule of that module was imported.
196   SourceLocation DirectImportLoc;
197 
198   /// \brief The source location where this module was first imported.
199   SourceLocation ImportLoc;
200 
201   /// \brief The first source location in this module.
202   SourceLocation FirstLoc;
203 
204   // === Input Files ===
205   /// \brief The cursor to the start of the input-files block.
206   llvm::BitstreamCursor InputFilesCursor;
207 
208   /// \brief Offsets for all of the input file entries in the AST file.
209   const uint64_t *InputFileOffsets;
210 
211   /// \brief The input files that have been loaded from this AST file.
212   std::vector<InputFile> InputFilesLoaded;
213 
214   /// \brief If non-zero, specifies the time when we last validated input
215   /// files.  Zero means we never validated them.
216   ///
217   /// The time is specified in seconds since the start of the Epoch.
218   uint64_t InputFilesValidationTimestamp;
219 
220   // === Source Locations ===
221 
222   /// \brief Cursor used to read source location entries.
223   llvm::BitstreamCursor SLocEntryCursor;
224 
225   /// \brief The number of source location entries in this AST file.
226   unsigned LocalNumSLocEntries;
227 
228   /// \brief The base ID in the source manager's view of this module.
229   int SLocEntryBaseID;
230 
231   /// \brief The base offset in the source manager's view of this module.
232   unsigned SLocEntryBaseOffset;
233 
234   /// \brief Offsets for all of the source location entries in the
235   /// AST file.
236   const uint32_t *SLocEntryOffsets;
237 
238   /// \brief SLocEntries that we're going to preload.
239   SmallVector<uint64_t, 4> PreloadSLocEntries;
240 
241   /// \brief Remapping table for source locations in this module.
242   ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
243 
244   // === Identifiers ===
245 
246   /// \brief The number of identifiers in this AST file.
247   unsigned LocalNumIdentifiers;
248 
249   /// \brief Offsets into the identifier table data.
250   ///
251   /// This array is indexed by the identifier ID (-1), and provides
252   /// the offset into IdentifierTableData where the string data is
253   /// stored.
254   const uint32_t *IdentifierOffsets;
255 
256   /// \brief Base identifier ID for identifiers local to this module.
257   serialization::IdentID BaseIdentifierID;
258 
259   /// \brief Remapping table for identifier IDs in this module.
260   ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
261 
262   /// \brief Actual data for the on-disk hash table of identifiers.
263   ///
264   /// This pointer points into a memory buffer, where the on-disk hash
265   /// table for identifiers actually lives.
266   const char *IdentifierTableData;
267 
268   /// \brief A pointer to an on-disk hash table of opaque type
269   /// IdentifierHashTable.
270   void *IdentifierLookupTable;
271 
272   // === Macros ===
273 
274   /// \brief The cursor to the start of the preprocessor block, which stores
275   /// all of the macro definitions.
276   llvm::BitstreamCursor MacroCursor;
277 
278   /// \brief The number of macros in this AST file.
279   unsigned LocalNumMacros;
280 
281   /// \brief Offsets of macros in the preprocessor block.
282   ///
283   /// This array is indexed by the macro ID (-1), and provides
284   /// the offset into the preprocessor block where macro definitions are
285   /// stored.
286   const uint32_t *MacroOffsets;
287 
288   /// \brief Base macro ID for macros local to this module.
289   serialization::MacroID BaseMacroID;
290 
291   /// \brief Remapping table for macro IDs in this module.
292   ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
293 
294   /// \brief The offset of the start of the set of defined macros.
295   uint64_t MacroStartOffset;
296 
297   // === Detailed PreprocessingRecord ===
298 
299   /// \brief The cursor to the start of the (optional) detailed preprocessing
300   /// record block.
301   llvm::BitstreamCursor PreprocessorDetailCursor;
302 
303   /// \brief The offset of the start of the preprocessor detail cursor.
304   uint64_t PreprocessorDetailStartOffset;
305 
306   /// \brief Base preprocessed entity ID for preprocessed entities local to
307   /// this module.
308   serialization::PreprocessedEntityID BasePreprocessedEntityID;
309 
310   /// \brief Remapping table for preprocessed entity IDs in this module.
311   ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
312 
313   const PPEntityOffset *PreprocessedEntityOffsets;
314   unsigned NumPreprocessedEntities;
315 
316   // === Header search information ===
317 
318   /// \brief The number of local HeaderFileInfo structures.
319   unsigned LocalNumHeaderFileInfos;
320 
321   /// \brief Actual data for the on-disk hash table of header file
322   /// information.
323   ///
324   /// This pointer points into a memory buffer, where the on-disk hash
325   /// table for header file information actually lives.
326   const char *HeaderFileInfoTableData;
327 
328   /// \brief The on-disk hash table that contains information about each of
329   /// the header files.
330   void *HeaderFileInfoTable;
331 
332   // === Submodule information ===
333   /// \brief The number of submodules in this module.
334   unsigned LocalNumSubmodules;
335 
336   /// \brief Base submodule ID for submodules local to this module.
337   serialization::SubmoduleID BaseSubmoduleID;
338 
339   /// \brief Remapping table for submodule IDs in this module.
340   ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
341 
342   // === Selectors ===
343 
344   /// \brief The number of selectors new to this file.
345   ///
346   /// This is the number of entries in SelectorOffsets.
347   unsigned LocalNumSelectors;
348 
349   /// \brief Offsets into the selector lookup table's data array
350   /// where each selector resides.
351   const uint32_t *SelectorOffsets;
352 
353   /// \brief Base selector ID for selectors local to this module.
354   serialization::SelectorID BaseSelectorID;
355 
356   /// \brief Remapping table for selector IDs in this module.
357   ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
358 
359   /// \brief A pointer to the character data that comprises the selector table
360   ///
361   /// The SelectorOffsets table refers into this memory.
362   const unsigned char *SelectorLookupTableData;
363 
364   /// \brief A pointer to an on-disk hash table of opaque type
365   /// ASTSelectorLookupTable.
366   ///
367   /// This hash table provides the IDs of all selectors, and the associated
368   /// instance and factory methods.
369   void *SelectorLookupTable;
370 
371   // === Declarations ===
372 
373   /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
374   /// has read all the abbreviations at the start of the block and is ready to
375   /// jump around with these in context.
376   llvm::BitstreamCursor DeclsCursor;
377 
378   /// \brief The number of declarations in this AST file.
379   unsigned LocalNumDecls;
380 
381   /// \brief Offset of each declaration within the bitstream, indexed
382   /// by the declaration ID (-1).
383   const DeclOffset *DeclOffsets;
384 
385   /// \brief Base declaration ID for declarations local to this module.
386   serialization::DeclID BaseDeclID;
387 
388   /// \brief Remapping table for declaration IDs in this module.
389   ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
390 
391   /// \brief Mapping from the module files that this module file depends on
392   /// to the base declaration ID for that module as it is understood within this
393   /// module.
394   ///
395   /// This is effectively a reverse global-to-local mapping for declaration
396   /// IDs, so that we can interpret a true global ID (for this translation unit)
397   /// as a local ID (for this module file).
398   llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
399 
400   /// \brief The number of C++ base specifier sets in this AST file.
401   unsigned LocalNumCXXBaseSpecifiers;
402 
403   /// \brief Offset of each C++ base specifier set within the bitstream,
404   /// indexed by the C++ base specifier set ID (-1).
405   const uint32_t *CXXBaseSpecifiersOffsets;
406 
407   /// \brief The number of C++ ctor initializer lists in this AST file.
408   unsigned LocalNumCXXCtorInitializers;
409 
410   /// \brief Offset of each C++ ctor initializer list within the bitstream,
411   /// indexed by the C++ ctor initializer list ID minus 1.
412   const uint32_t *CXXCtorInitializersOffsets;
413 
414   typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
415   DeclContextInfosMap;
416 
417   /// \brief Information about the lexical and visible declarations
418   /// for each DeclContext.
419   DeclContextInfosMap DeclContextInfos;
420 
421   /// \brief Array of file-level DeclIDs sorted by file.
422   const serialization::DeclID *FileSortedDecls;
423   unsigned NumFileSortedDecls;
424 
425   /// \brief Array of redeclaration chain location information within this
426   /// module file, sorted by the first declaration ID.
427   const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
428 
429   /// \brief The number of redeclaration info entries in RedeclarationsMap.
430   unsigned LocalNumRedeclarationsInMap;
431 
432   /// \brief The redeclaration chains for declarations local to this
433   /// module file.
434   SmallVector<uint64_t, 1> RedeclarationChains;
435 
436   /// \brief Array of category list location information within this
437   /// module file, sorted by the definition ID.
438   const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
439 
440   /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
441   unsigned LocalNumObjCCategoriesInMap;
442 
443   /// \brief The Objective-C category lists for categories known to this
444   /// module.
445   SmallVector<uint64_t, 1> ObjCCategories;
446 
447   // === Types ===
448 
449   /// \brief The number of types in this AST file.
450   unsigned LocalNumTypes;
451 
452   /// \brief Offset of each type within the bitstream, indexed by the
453   /// type ID, or the representation of a Type*.
454   const uint32_t *TypeOffsets;
455 
456   /// \brief Base type ID for types local to this module as represented in
457   /// the global type ID space.
458   serialization::TypeID BaseTypeIndex;
459 
460   /// \brief Remapping table for type IDs in this module.
461   ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
462 
463   // === Miscellaneous ===
464 
465   /// \brief Diagnostic IDs and their mappings that the user changed.
466   SmallVector<uint64_t, 8> PragmaDiagMappings;
467 
468   /// \brief List of modules which depend on this module
469   llvm::SetVector<ModuleFile *> ImportedBy;
470 
471   /// \brief List of modules which this module depends on
472   llvm::SetVector<ModuleFile *> Imports;
473 
474   /// \brief Determine whether this module was directly imported at
475   /// any point during translation.
isDirectlyImported()476   bool isDirectlyImported() const { return DirectlyImported; }
477 
478   /// \brief Dump debugging output for this module.
479   void dump();
480 };
481 
482 } // end namespace serialization
483 
484 } // end namespace clang
485 
486 #endif
487