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"
27 template <typename Info> class OnDiskChainedHashTable;
28 template <typename Info> class OnDiskIterableChainedHashTable;
37 namespace serialization {
40 class ASTDeclContextNameLookupTrait;
43 /// \brief Specifies the kind of module that has been loaded.
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.
52 /// \brief Information about the contents of a DeclContext.
53 struct DeclContextInfo {
55 : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
57 llvm::OnDiskIterableChainedHashTable<reader::ASTDeclContextNameLookupTrait>
58 *NameLookupTableData; // an ASTDeclContextNameLookupTable.
59 const KindDeclIDPair *LexicalDecls;
60 unsigned NumLexicalDecls;
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.
72 llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
76 InputFile(const FileEntry *File,
77 bool isOverridden = false, bool isOutOfDate = false) {
78 assert(!(isOverridden && isOutOfDate) &&
79 "an overridden cannot be out-of-date");
85 Val.setPointerAndInt(File, intVal);
88 static InputFile getNotFound() {
90 File.Val.setInt(NotFound);
94 const FileEntry *getFile() const { return Val.getPointer(); }
95 bool isOverridden() const { return Val.getInt() == Overridden; }
96 bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
97 bool isNotFound() const { return Val.getInt() == NotFound; }
100 typedef unsigned ASTFileSignature;
102 /// \brief Information about a module that has been loaded by the ASTReader.
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
111 ModuleFile(ModuleKind Kind, unsigned Generation);
114 // === General information ===
116 /// \brief The index of this module in the list of modules.
119 /// \brief The type of this module.
122 /// \brief The file name of the module file.
123 std::string FileName;
125 /// \brief The name of the module.
126 std::string ModuleName;
128 /// \brief The base directory of the module.
129 std::string BaseDirectory;
131 std::string getTimestampFilename() const {
132 return FileName + ".timestamp";
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;
140 /// \brief The actual original source file name that was used to
141 /// build this AST file.
142 std::string ActualOriginalSourceFileName;
144 /// \brief The file ID for the original source file that was used to
145 /// build this AST file.
146 FileID OriginalSourceFileID;
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;
152 std::string ModuleMapPath;
154 /// \brief Whether this precompiled header is a relocatable PCH file.
157 /// \brief The file entry for the module file.
158 const FileEntry *File;
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;
164 /// \brief Whether this module has been directly imported by the
166 bool DirectlyImported;
168 /// \brief The generation of which this module file is a part.
171 /// \brief The memory buffer that stores the data associated with
173 std::unique_ptr<llvm::MemoryBuffer> Buffer;
175 /// \brief The size of this file, in bits.
178 /// \brief The global bit offset (or base) of this module
179 uint64_t GlobalBitOffset;
181 /// \brief The bitstream reader from which we'll read the AST file.
182 llvm::BitstreamReader StreamFile;
184 /// \brief The main bitstream cursor for the main block.
185 llvm::BitstreamCursor Stream;
187 /// \brief The source location where the module was explicitly or implicitly
188 /// imported in the local translation unit.
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).
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;
198 /// \brief The source location where this module was first imported.
199 SourceLocation ImportLoc;
201 /// \brief The first source location in this module.
202 SourceLocation FirstLoc;
204 // === Input Files ===
205 /// \brief The cursor to the start of the input-files block.
206 llvm::BitstreamCursor InputFilesCursor;
208 /// \brief Offsets for all of the input file entries in the AST file.
209 const uint32_t *InputFileOffsets;
211 /// \brief The input files that have been loaded from this AST file.
212 std::vector<InputFile> InputFilesLoaded;
214 /// \brief If non-zero, specifies the time when we last validated input
215 /// files. Zero means we never validated them.
217 /// The time is specified in seconds since the start of the Epoch.
218 uint64_t InputFilesValidationTimestamp;
220 // === Source Locations ===
222 /// \brief Cursor used to read source location entries.
223 llvm::BitstreamCursor SLocEntryCursor;
225 /// \brief The number of source location entries in this AST file.
226 unsigned LocalNumSLocEntries;
228 /// \brief The base ID in the source manager's view of this module.
231 /// \brief The base offset in the source manager's view of this module.
232 unsigned SLocEntryBaseOffset;
234 /// \brief Offsets for all of the source location entries in the
236 const uint32_t *SLocEntryOffsets;
238 /// \brief SLocEntries that we're going to preload.
239 SmallVector<uint64_t, 4> PreloadSLocEntries;
241 /// \brief Remapping table for source locations in this module.
242 ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
244 // === Identifiers ===
246 /// \brief The number of identifiers in this AST file.
247 unsigned LocalNumIdentifiers;
249 /// \brief Offsets into the identifier table data.
251 /// This array is indexed by the identifier ID (-1), and provides
252 /// the offset into IdentifierTableData where the string data is
254 const uint32_t *IdentifierOffsets;
256 /// \brief Base identifier ID for identifiers local to this module.
257 serialization::IdentID BaseIdentifierID;
259 /// \brief Remapping table for identifier IDs in this module.
260 ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
262 /// \brief Actual data for the on-disk hash table of identifiers.
264 /// This pointer points into a memory buffer, where the on-disk hash
265 /// table for identifiers actually lives.
266 const char *IdentifierTableData;
268 /// \brief A pointer to an on-disk hash table of opaque type
269 /// IdentifierHashTable.
270 void *IdentifierLookupTable;
274 /// \brief The cursor to the start of the preprocessor block, which stores
275 /// all of the macro definitions.
276 llvm::BitstreamCursor MacroCursor;
278 /// \brief The number of macros in this AST file.
279 unsigned LocalNumMacros;
281 /// \brief Offsets of macros in the preprocessor block.
283 /// This array is indexed by the macro ID (-1), and provides
284 /// the offset into the preprocessor block where macro definitions are
286 const uint32_t *MacroOffsets;
288 /// \brief Base macro ID for macros local to this module.
289 serialization::MacroID BaseMacroID;
291 /// \brief Remapping table for macro IDs in this module.
292 ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
294 /// \brief The offset of the start of the set of defined macros.
295 uint64_t MacroStartOffset;
297 // === Detailed PreprocessingRecord ===
299 /// \brief The cursor to the start of the (optional) detailed preprocessing
301 llvm::BitstreamCursor PreprocessorDetailCursor;
303 /// \brief The offset of the start of the preprocessor detail cursor.
304 uint64_t PreprocessorDetailStartOffset;
306 /// \brief Base preprocessed entity ID for preprocessed entities local to
308 serialization::PreprocessedEntityID BasePreprocessedEntityID;
310 /// \brief Remapping table for preprocessed entity IDs in this module.
311 ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
313 const PPEntityOffset *PreprocessedEntityOffsets;
314 unsigned NumPreprocessedEntities;
316 // === Header search information ===
318 /// \brief The number of local HeaderFileInfo structures.
319 unsigned LocalNumHeaderFileInfos;
321 /// \brief Actual data for the on-disk hash table of header file
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;
328 /// \brief The on-disk hash table that contains information about each of
329 /// the header files.
330 void *HeaderFileInfoTable;
332 // === Submodule information ===
333 /// \brief The number of submodules in this module.
334 unsigned LocalNumSubmodules;
336 /// \brief Base submodule ID for submodules local to this module.
337 serialization::SubmoduleID BaseSubmoduleID;
339 /// \brief Remapping table for submodule IDs in this module.
340 ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
344 /// \brief The number of selectors new to this file.
346 /// This is the number of entries in SelectorOffsets.
347 unsigned LocalNumSelectors;
349 /// \brief Offsets into the selector lookup table's data array
350 /// where each selector resides.
351 const uint32_t *SelectorOffsets;
353 /// \brief Base selector ID for selectors local to this module.
354 serialization::SelectorID BaseSelectorID;
356 /// \brief Remapping table for selector IDs in this module.
357 ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
359 /// \brief A pointer to the character data that comprises the selector table
361 /// The SelectorOffsets table refers into this memory.
362 const unsigned char *SelectorLookupTableData;
364 /// \brief A pointer to an on-disk hash table of opaque type
365 /// ASTSelectorLookupTable.
367 /// This hash table provides the IDs of all selectors, and the associated
368 /// instance and factory methods.
369 void *SelectorLookupTable;
371 // === Declarations ===
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;
378 /// \brief The number of declarations in this AST file.
379 unsigned LocalNumDecls;
381 /// \brief Offset of each declaration within the bitstream, indexed
382 /// by the declaration ID (-1).
383 const DeclOffset *DeclOffsets;
385 /// \brief Base declaration ID for declarations local to this module.
386 serialization::DeclID BaseDeclID;
388 /// \brief Remapping table for declaration IDs in this module.
389 ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
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
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;
400 /// \brief The number of C++ base specifier sets in this AST file.
401 unsigned LocalNumCXXBaseSpecifiers;
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;
407 typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
410 /// \brief Information about the lexical and visible declarations
411 /// for each DeclContext.
412 DeclContextInfosMap DeclContextInfos;
414 /// \brief Array of file-level DeclIDs sorted by file.
415 const serialization::DeclID *FileSortedDecls;
416 unsigned NumFileSortedDecls;
418 /// \brief Array of redeclaration chain location information within this
419 /// module file, sorted by the first declaration ID.
420 const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
422 /// \brief The number of redeclaration info entries in RedeclarationsMap.
423 unsigned LocalNumRedeclarationsInMap;
425 /// \brief The redeclaration chains for declarations local to this
427 SmallVector<uint64_t, 1> RedeclarationChains;
429 /// \brief Array of category list location information within this
430 /// module file, sorted by the definition ID.
431 const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
433 /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
434 unsigned LocalNumObjCCategoriesInMap;
436 /// \brief The Objective-C category lists for categories known to this
438 SmallVector<uint64_t, 1> ObjCCategories;
442 /// \brief The number of types in this AST file.
443 unsigned LocalNumTypes;
445 /// \brief Offset of each type within the bitstream, indexed by the
446 /// type ID, or the representation of a Type*.
447 const uint32_t *TypeOffsets;
449 /// \brief Base type ID for types local to this module as represented in
450 /// the global type ID space.
451 serialization::TypeID BaseTypeIndex;
453 /// \brief Remapping table for type IDs in this module.
454 ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
456 // === Miscellaneous ===
458 /// \brief Diagnostic IDs and their mappings that the user changed.
459 SmallVector<uint64_t, 8> PragmaDiagMappings;
461 /// \brief List of modules which depend on this module
462 llvm::SetVector<ModuleFile *> ImportedBy;
464 /// \brief List of modules which this module depends on
465 llvm::SetVector<ModuleFile *> Imports;
467 /// \brief Determine whether this module was directly imported at
468 /// any point during translation.
469 bool isDirectlyImported() const { return DirectlyImported; }
471 /// \brief Dump debugging output for this module.
475 } // end namespace serialization
477 } // end namespace clang