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/Module.h"
19 #include "clang/Basic/SourceLocation.h"
20 #include "clang/Serialization/ASTBitCodes.h"
21 #include "clang/Serialization/ContinuousRangeMap.h"
22 #include "clang/Serialization/ModuleFileExtension.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/ADT/PointerIntPair.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringRef.h"
28 #include "llvm/Bitcode/BitstreamReader.h"
29 #include "llvm/Support/Endian.h"
40 namespace serialization {
42 /// Specifies the kind of module that has been loaded.
44 /// File is an implicitly-loaded module.
47 /// File is an explicitly-loaded module.
50 /// File is a PCH file treated as such.
53 /// File is a PCH file treated as the preamble.
56 /// File is a PCH file treated as the actual main file.
59 /// File is from a prebuilt module path.
63 /// 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;
75 InputFile() = default;
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 /// Information about a module that has been loaded by the ASTReader.
103 /// Each instance of the Module class corresponds to a single AST file, which
104 /// may be a precompiled header, precompiled preamble, a module, or an AST file
105 /// of some sort loaded as the main file, all of which are specific formulations
106 /// of the general notion of a "module". A module may depend on any number of
110 ModuleFile(ModuleKind Kind, unsigned Generation)
111 : Kind(Kind), Generation(Generation) {}
114 // === General information ===
116 /// The index of this module in the list of modules.
119 /// The type of this module.
122 /// The file name of the module file.
123 std::string FileName;
125 /// The name of the module.
126 std::string ModuleName;
128 /// The base directory of the module.
129 std::string BaseDirectory;
131 std::string getTimestampFilename() const {
132 return FileName + ".timestamp";
135 /// 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 /// The actual original source file name that was used to
141 /// build this AST file.
142 std::string ActualOriginalSourceFileName;
144 /// The file ID for the original source file that was used to
145 /// build this AST file.
146 FileID OriginalSourceFileID;
148 /// 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 /// Whether this precompiled header is a relocatable PCH file.
155 bool RelocatablePCH = false;
157 /// Whether timestamps are included in this module file.
158 bool HasTimestamps = false;
160 /// Whether the PCH has a corresponding object file.
161 bool PCHHasObjectFile = false;
163 /// The file entry for the module file.
164 const FileEntry *File = nullptr;
166 /// The signature of the module file, which may be used instead of the size
167 /// and modification time to identify this particular file.
168 ASTFileSignature Signature;
170 /// Whether this module has been directly imported by the
172 bool DirectlyImported = false;
174 /// The generation of which this module file is a part.
177 /// The memory buffer that stores the data associated with
178 /// this AST file, owned by the PCMCache in the ModuleManager.
179 llvm::MemoryBuffer *Buffer;
181 /// The size of this file, in bits.
182 uint64_t SizeInBits = 0;
184 /// The global bit offset (or base) of this module
185 uint64_t GlobalBitOffset = 0;
187 /// The serialized bitstream data for this file.
190 /// The main bitstream cursor for the main block.
191 llvm::BitstreamCursor Stream;
193 /// The source location where the module was explicitly or implicitly
194 /// imported in the local translation unit.
196 /// If module A depends on and imports module B, both modules will have the
197 /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
198 /// source location inside module A).
200 /// WARNING: This is largely useless. It doesn't tell you when a module was
201 /// made visible, just when the first submodule of that module was imported.
202 SourceLocation DirectImportLoc;
204 /// The source location where this module was first imported.
205 SourceLocation ImportLoc;
207 /// The first source location in this module.
208 SourceLocation FirstLoc;
210 /// The list of extension readers that are attached to this module
212 std::vector<std::unique_ptr<ModuleFileExtensionReader>> ExtensionReaders;
214 /// The module offset map data for this file. If non-empty, the various
215 /// ContinuousRangeMaps described below have not yet been populated.
216 StringRef ModuleOffsetMap;
218 // === Input Files ===
220 /// The cursor to the start of the input-files block.
221 llvm::BitstreamCursor InputFilesCursor;
223 /// Offsets for all of the input file entries in the AST file.
224 const llvm::support::unaligned_uint64_t *InputFileOffsets = nullptr;
226 /// The input files that have been loaded from this AST file.
227 std::vector<InputFile> InputFilesLoaded;
229 // All user input files reside at the index range [0, NumUserInputFiles), and
230 // system input files reside at [NumUserInputFiles, InputFilesLoaded.size()).
231 unsigned NumUserInputFiles = 0;
233 /// If non-zero, specifies the time when we last validated input
234 /// files. Zero means we never validated them.
236 /// The time is specified in seconds since the start of the Epoch.
237 uint64_t InputFilesValidationTimestamp = 0;
239 // === Source Locations ===
241 /// Cursor used to read source location entries.
242 llvm::BitstreamCursor SLocEntryCursor;
244 /// The number of source location entries in this AST file.
245 unsigned LocalNumSLocEntries = 0;
247 /// The base ID in the source manager's view of this module.
248 int SLocEntryBaseID = 0;
250 /// The base offset in the source manager's view of this module.
251 unsigned SLocEntryBaseOffset = 0;
253 /// Offsets for all of the source location entries in the
255 const uint32_t *SLocEntryOffsets = nullptr;
257 /// SLocEntries that we're going to preload.
258 SmallVector<uint64_t, 4> PreloadSLocEntries;
260 /// Remapping table for source locations in this module.
261 ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
263 // === Identifiers ===
265 /// The number of identifiers in this AST file.
266 unsigned LocalNumIdentifiers = 0;
268 /// Offsets into the identifier table data.
270 /// This array is indexed by the identifier ID (-1), and provides
271 /// the offset into IdentifierTableData where the string data is
273 const uint32_t *IdentifierOffsets = nullptr;
275 /// Base identifier ID for identifiers local to this module.
276 serialization::IdentID BaseIdentifierID = 0;
278 /// Remapping table for identifier IDs in this module.
279 ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
281 /// Actual data for the on-disk hash table of identifiers.
283 /// This pointer points into a memory buffer, where the on-disk hash
284 /// table for identifiers actually lives.
285 const char *IdentifierTableData = nullptr;
287 /// A pointer to an on-disk hash table of opaque type
288 /// IdentifierHashTable.
289 void *IdentifierLookupTable = nullptr;
291 /// Offsets of identifiers that we're going to preload within
292 /// IdentifierTableData.
293 std::vector<unsigned> PreloadIdentifierOffsets;
297 /// The cursor to the start of the preprocessor block, which stores
298 /// all of the macro definitions.
299 llvm::BitstreamCursor MacroCursor;
301 /// The number of macros in this AST file.
302 unsigned LocalNumMacros = 0;
304 /// Offsets of macros in the preprocessor block.
306 /// This array is indexed by the macro ID (-1), and provides
307 /// the offset into the preprocessor block where macro definitions are
309 const uint32_t *MacroOffsets = nullptr;
311 /// Base macro ID for macros local to this module.
312 serialization::MacroID BaseMacroID = 0;
314 /// Remapping table for macro IDs in this module.
315 ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
317 /// The offset of the start of the set of defined macros.
318 uint64_t MacroStartOffset = 0;
320 // === Detailed PreprocessingRecord ===
322 /// The cursor to the start of the (optional) detailed preprocessing
324 llvm::BitstreamCursor PreprocessorDetailCursor;
326 /// The offset of the start of the preprocessor detail cursor.
327 uint64_t PreprocessorDetailStartOffset = 0;
329 /// Base preprocessed entity ID for preprocessed entities local to
331 serialization::PreprocessedEntityID BasePreprocessedEntityID = 0;
333 /// Remapping table for preprocessed entity IDs in this module.
334 ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
336 const PPEntityOffset *PreprocessedEntityOffsets = nullptr;
337 unsigned NumPreprocessedEntities = 0;
339 /// Base ID for preprocessed skipped ranges local to this module.
340 unsigned BasePreprocessedSkippedRangeID = 0;
342 const PPSkippedRange *PreprocessedSkippedRangeOffsets = nullptr;
343 unsigned NumPreprocessedSkippedRanges = 0;
345 // === Header search information ===
347 /// The number of local HeaderFileInfo structures.
348 unsigned LocalNumHeaderFileInfos = 0;
350 /// Actual data for the on-disk hash table of header file
353 /// This pointer points into a memory buffer, where the on-disk hash
354 /// table for header file information actually lives.
355 const char *HeaderFileInfoTableData = nullptr;
357 /// The on-disk hash table that contains information about each of
358 /// the header files.
359 void *HeaderFileInfoTable = nullptr;
361 // === Submodule information ===
363 /// The number of submodules in this module.
364 unsigned LocalNumSubmodules = 0;
366 /// Base submodule ID for submodules local to this module.
367 serialization::SubmoduleID BaseSubmoduleID = 0;
369 /// Remapping table for submodule IDs in this module.
370 ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
374 /// The number of selectors new to this file.
376 /// This is the number of entries in SelectorOffsets.
377 unsigned LocalNumSelectors = 0;
379 /// Offsets into the selector lookup table's data array
380 /// where each selector resides.
381 const uint32_t *SelectorOffsets = nullptr;
383 /// Base selector ID for selectors local to this module.
384 serialization::SelectorID BaseSelectorID = 0;
386 /// Remapping table for selector IDs in this module.
387 ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
389 /// A pointer to the character data that comprises the selector table
391 /// The SelectorOffsets table refers into this memory.
392 const unsigned char *SelectorLookupTableData = nullptr;
394 /// A pointer to an on-disk hash table of opaque type
395 /// ASTSelectorLookupTable.
397 /// This hash table provides the IDs of all selectors, and the associated
398 /// instance and factory methods.
399 void *SelectorLookupTable = nullptr;
401 // === Declarations ===
403 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
404 /// has read all the abbreviations at the start of the block and is ready to
405 /// jump around with these in context.
406 llvm::BitstreamCursor DeclsCursor;
408 /// The number of declarations in this AST file.
409 unsigned LocalNumDecls = 0;
411 /// Offset of each declaration within the bitstream, indexed
412 /// by the declaration ID (-1).
413 const DeclOffset *DeclOffsets = nullptr;
415 /// Base declaration ID for declarations local to this module.
416 serialization::DeclID BaseDeclID = 0;
418 /// Remapping table for declaration IDs in this module.
419 ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
421 /// Mapping from the module files that this module file depends on
422 /// to the base declaration ID for that module as it is understood within this
425 /// This is effectively a reverse global-to-local mapping for declaration
426 /// IDs, so that we can interpret a true global ID (for this translation unit)
427 /// as a local ID (for this module file).
428 llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
430 /// Array of file-level DeclIDs sorted by file.
431 const serialization::DeclID *FileSortedDecls = nullptr;
432 unsigned NumFileSortedDecls = 0;
434 /// Array of category list location information within this
435 /// module file, sorted by the definition ID.
436 const serialization::ObjCCategoriesInfo *ObjCCategoriesMap = nullptr;
438 /// The number of redeclaration info entries in ObjCCategoriesMap.
439 unsigned LocalNumObjCCategoriesInMap = 0;
441 /// The Objective-C category lists for categories known to this
443 SmallVector<uint64_t, 1> ObjCCategories;
447 /// The number of types in this AST file.
448 unsigned LocalNumTypes = 0;
450 /// Offset of each type within the bitstream, indexed by the
451 /// type ID, or the representation of a Type*.
452 const uint32_t *TypeOffsets = nullptr;
454 /// Base type ID for types local to this module as represented in
455 /// the global type ID space.
456 serialization::TypeID BaseTypeIndex = 0;
458 /// Remapping table for type IDs in this module.
459 ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
461 // === Miscellaneous ===
463 /// Diagnostic IDs and their mappings that the user changed.
464 SmallVector<uint64_t, 8> PragmaDiagMappings;
466 /// List of modules which depend on this module
467 llvm::SetVector<ModuleFile *> ImportedBy;
469 /// List of modules which this module depends on
470 llvm::SetVector<ModuleFile *> Imports;
472 /// Determine whether this module was directly imported at
473 /// any point during translation.
474 bool isDirectlyImported() const { return DirectlyImported; }
476 /// Is this a module file for a module (rather than a PCH or similar).
477 bool isModule() const {
478 return Kind == MK_ImplicitModule || Kind == MK_ExplicitModule ||
479 Kind == MK_PrebuiltModule;
482 /// Dump debugging output for this module.
486 } // namespace serialization
490 #endif // LLVM_CLANG_SERIALIZATION_MODULE_H