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