1 //===--- Module.h - Module description --------------------------*- 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 Module class, which describes a module that has
11 // been loaded from an AST file.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SERIALIZATION_MODULE_H
16 #define LLVM_CLANG_SERIALIZATION_MODULE_H
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"
28 template <typename Info> class OnDiskChainedHashTable;
29 template <typename Info> class OnDiskIterableChainedHashTable;
38 namespace serialization {
41 class ASTDeclContextNameLookupTrait;
44 /// \brief Specifies the kind of module that has been loaded.
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.
53 /// \brief Information about the contents of a DeclContext.
54 struct DeclContextInfo {
56 : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
58 llvm::OnDiskIterableChainedHashTable<reader::ASTDeclContextNameLookupTrait>
59 *NameLookupTableData; // an ASTDeclContextNameLookupTable.
60 const KindDeclIDPair *LexicalDecls;
61 unsigned NumLexicalDecls;
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.
73 llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
77 InputFile(const FileEntry *File,
78 bool isOverridden = false, bool isOutOfDate = false) {
79 assert(!(isOverridden && isOutOfDate) &&
80 "an overridden cannot be out-of-date");
86 Val.setPointerAndInt(File, intVal);
89 static InputFile getNotFound() {
91 File.Val.setInt(NotFound);
95 const FileEntry *getFile() const { return Val.getPointer(); }
96 bool isOverridden() const { return Val.getInt() == Overridden; }
97 bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
98 bool isNotFound() const { return Val.getInt() == NotFound; }
101 typedef unsigned ASTFileSignature;
103 /// \brief Information about a module that has been loaded by the ASTReader.
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
112 ModuleFile(ModuleKind Kind, unsigned Generation);
115 // === General information ===
117 /// \brief The index of this module in the list of modules.
120 /// \brief The type of this module.
123 /// \brief The file name of the module file.
124 std::string FileName;
126 /// \brief The name of the module.
127 std::string ModuleName;
129 /// \brief The base directory of the module.
130 std::string BaseDirectory;
132 std::string getTimestampFilename() const {
133 return FileName + ".timestamp";
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;
141 /// \brief The actual original source file name that was used to
142 /// build this AST file.
143 std::string ActualOriginalSourceFileName;
145 /// \brief The file ID for the original source file that was used to
146 /// build this AST file.
147 FileID OriginalSourceFileID;
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;
153 std::string ModuleMapPath;
155 /// \brief Whether this precompiled header is a relocatable PCH file.
158 /// \brief The file entry for the module file.
159 const FileEntry *File;
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;
165 /// \brief Whether this module has been directly imported by the
167 bool DirectlyImported;
169 /// \brief The generation of which this module file is a part.
172 /// \brief The memory buffer that stores the data associated with
174 std::unique_ptr<llvm::MemoryBuffer> Buffer;
176 /// \brief The size of this file, in bits.
179 /// \brief The global bit offset (or base) of this module
180 uint64_t GlobalBitOffset;
182 /// \brief The bitstream reader from which we'll read the AST file.
183 llvm::BitstreamReader StreamFile;
185 /// \brief The main bitstream cursor for the main block.
186 llvm::BitstreamCursor Stream;
188 /// \brief The source location where the module was explicitly or implicitly
189 /// imported in the local translation unit.
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).
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;
199 /// \brief The source location where this module was first imported.
200 SourceLocation ImportLoc;
202 /// \brief The first source location in this module.
203 SourceLocation FirstLoc;
205 // === Input Files ===
206 /// \brief The cursor to the start of the input-files block.
207 llvm::BitstreamCursor InputFilesCursor;
209 /// \brief Offsets for all of the input file entries in the AST file.
210 const llvm::support::unaligned_uint64_t *InputFileOffsets;
212 /// \brief The input files that have been loaded from this AST file.
213 std::vector<InputFile> InputFilesLoaded;
215 /// \brief If non-zero, specifies the time when we last validated input
216 /// files. Zero means we never validated them.
218 /// The time is specified in seconds since the start of the Epoch.
219 uint64_t InputFilesValidationTimestamp;
221 // === Source Locations ===
223 /// \brief Cursor used to read source location entries.
224 llvm::BitstreamCursor SLocEntryCursor;
226 /// \brief The number of source location entries in this AST file.
227 unsigned LocalNumSLocEntries;
229 /// \brief The base ID in the source manager's view of this module.
232 /// \brief The base offset in the source manager's view of this module.
233 unsigned SLocEntryBaseOffset;
235 /// \brief Offsets for all of the source location entries in the
237 const uint32_t *SLocEntryOffsets;
239 /// \brief SLocEntries that we're going to preload.
240 SmallVector<uint64_t, 4> PreloadSLocEntries;
242 /// \brief Remapping table for source locations in this module.
243 ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
245 // === Identifiers ===
247 /// \brief The number of identifiers in this AST file.
248 unsigned LocalNumIdentifiers;
250 /// \brief Offsets into the identifier table data.
252 /// This array is indexed by the identifier ID (-1), and provides
253 /// the offset into IdentifierTableData where the string data is
255 const uint32_t *IdentifierOffsets;
257 /// \brief Base identifier ID for identifiers local to this module.
258 serialization::IdentID BaseIdentifierID;
260 /// \brief Remapping table for identifier IDs in this module.
261 ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
263 /// \brief Actual data for the on-disk hash table of identifiers.
265 /// This pointer points into a memory buffer, where the on-disk hash
266 /// table for identifiers actually lives.
267 const char *IdentifierTableData;
269 /// \brief A pointer to an on-disk hash table of opaque type
270 /// IdentifierHashTable.
271 void *IdentifierLookupTable;
275 /// \brief The cursor to the start of the preprocessor block, which stores
276 /// all of the macro definitions.
277 llvm::BitstreamCursor MacroCursor;
279 /// \brief The number of macros in this AST file.
280 unsigned LocalNumMacros;
282 /// \brief Offsets of macros in the preprocessor block.
284 /// This array is indexed by the macro ID (-1), and provides
285 /// the offset into the preprocessor block where macro definitions are
287 const uint32_t *MacroOffsets;
289 /// \brief Base macro ID for macros local to this module.
290 serialization::MacroID BaseMacroID;
292 /// \brief Remapping table for macro IDs in this module.
293 ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
295 /// \brief The offset of the start of the set of defined macros.
296 uint64_t MacroStartOffset;
298 // === Detailed PreprocessingRecord ===
300 /// \brief The cursor to the start of the (optional) detailed preprocessing
302 llvm::BitstreamCursor PreprocessorDetailCursor;
304 /// \brief The offset of the start of the preprocessor detail cursor.
305 uint64_t PreprocessorDetailStartOffset;
307 /// \brief Base preprocessed entity ID for preprocessed entities local to
309 serialization::PreprocessedEntityID BasePreprocessedEntityID;
311 /// \brief Remapping table for preprocessed entity IDs in this module.
312 ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
314 const PPEntityOffset *PreprocessedEntityOffsets;
315 unsigned NumPreprocessedEntities;
317 // === Header search information ===
319 /// \brief The number of local HeaderFileInfo structures.
320 unsigned LocalNumHeaderFileInfos;
322 /// \brief Actual data for the on-disk hash table of header file
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;
329 /// \brief The on-disk hash table that contains information about each of
330 /// the header files.
331 void *HeaderFileInfoTable;
333 // === Submodule information ===
334 /// \brief The number of submodules in this module.
335 unsigned LocalNumSubmodules;
337 /// \brief Base submodule ID for submodules local to this module.
338 serialization::SubmoduleID BaseSubmoduleID;
340 /// \brief Remapping table for submodule IDs in this module.
341 ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
345 /// \brief The number of selectors new to this file.
347 /// This is the number of entries in SelectorOffsets.
348 unsigned LocalNumSelectors;
350 /// \brief Offsets into the selector lookup table's data array
351 /// where each selector resides.
352 const uint32_t *SelectorOffsets;
354 /// \brief Base selector ID for selectors local to this module.
355 serialization::SelectorID BaseSelectorID;
357 /// \brief Remapping table for selector IDs in this module.
358 ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
360 /// \brief A pointer to the character data that comprises the selector table
362 /// The SelectorOffsets table refers into this memory.
363 const unsigned char *SelectorLookupTableData;
365 /// \brief A pointer to an on-disk hash table of opaque type
366 /// ASTSelectorLookupTable.
368 /// This hash table provides the IDs of all selectors, and the associated
369 /// instance and factory methods.
370 void *SelectorLookupTable;
372 // === Declarations ===
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;
379 /// \brief The number of declarations in this AST file.
380 unsigned LocalNumDecls;
382 /// \brief Offset of each declaration within the bitstream, indexed
383 /// by the declaration ID (-1).
384 const DeclOffset *DeclOffsets;
386 /// \brief Base declaration ID for declarations local to this module.
387 serialization::DeclID BaseDeclID;
389 /// \brief Remapping table for declaration IDs in this module.
390 ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
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
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;
401 /// \brief The number of C++ base specifier sets in this AST file.
402 unsigned LocalNumCXXBaseSpecifiers;
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;
408 /// \brief The number of C++ ctor initializer lists in this AST file.
409 unsigned LocalNumCXXCtorInitializers;
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;
415 typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
418 /// \brief Information about the lexical and visible declarations
419 /// for each DeclContext.
420 DeclContextInfosMap DeclContextInfos;
422 /// \brief Array of file-level DeclIDs sorted by file.
423 const serialization::DeclID *FileSortedDecls;
424 unsigned NumFileSortedDecls;
426 /// \brief Array of redeclaration chain location information within this
427 /// module file, sorted by the first declaration ID.
428 const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
430 /// \brief The number of redeclaration info entries in RedeclarationsMap.
431 unsigned LocalNumRedeclarationsInMap;
433 /// \brief The redeclaration chains for declarations local to this
435 SmallVector<uint64_t, 1> RedeclarationChains;
437 /// \brief Array of category list location information within this
438 /// module file, sorted by the definition ID.
439 const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
441 /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
442 unsigned LocalNumObjCCategoriesInMap;
444 /// \brief The Objective-C category lists for categories known to this
446 SmallVector<uint64_t, 1> ObjCCategories;
450 /// \brief The number of types in this AST file.
451 unsigned LocalNumTypes;
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;
457 /// \brief Base type ID for types local to this module as represented in
458 /// the global type ID space.
459 serialization::TypeID BaseTypeIndex;
461 /// \brief Remapping table for type IDs in this module.
462 ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
464 // === Miscellaneous ===
466 /// \brief Diagnostic IDs and their mappings that the user changed.
467 SmallVector<uint64_t, 8> PragmaDiagMappings;
469 /// \brief List of modules which depend on this module
470 llvm::SetVector<ModuleFile *> ImportedBy;
472 /// \brief List of modules which this module depends on
473 llvm::SetVector<ModuleFile *> Imports;
475 /// \brief Determine whether this module was directly imported at
476 /// any point during translation.
477 bool isDirectlyImported() const { return DirectlyImported; }
479 /// \brief Dump debugging output for this module.
483 } // end namespace serialization
485 } // end namespace clang