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/OwningPtr.h"
22 #include "llvm/ADT/SetVector.h"
23 #include "llvm/Bitcode/BitstreamReader.h"
31 template<typename Info> class OnDiskChainedHashTable;
33 namespace serialization {
36 class ASTDeclContextNameLookupTrait;
39 /// \brief Specifies the kind of module that has been loaded.
41 MK_Module, ///< File is a module proper.
42 MK_PCH, ///< File is a PCH file treated as such.
43 MK_Preamble, ///< File is a PCH file treated as the preamble.
44 MK_MainFile ///< File is a PCH file treated as the actual main file.
47 /// \brief Information about the contents of a DeclContext.
48 struct DeclContextInfo {
50 : NameLookupTableData(), LexicalDecls(), NumLexicalDecls() {}
52 OnDiskChainedHashTable<reader::ASTDeclContextNameLookupTrait>
53 *NameLookupTableData; // an ASTDeclContextNameLookupTable.
54 const KindDeclIDPair *LexicalDecls;
55 unsigned NumLexicalDecls;
58 /// \brief The input file that has been loaded from this AST file, along with
59 /// bools indicating whether this was an overridden buffer or if it was
66 llvm::PointerIntPair<const FileEntry *, 2, unsigned> Val;
70 InputFile(const FileEntry *File,
71 bool isOverridden = false, bool isOutOfDate = false) {
72 assert(!(isOverridden && isOutOfDate) &&
73 "an overridden cannot be out-of-date");
79 Val.setPointerAndInt(File, intVal);
82 const FileEntry *getFile() const { return Val.getPointer(); }
83 bool isOverridden() const { return Val.getInt() == Overridden; }
84 bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
87 /// \brief Information about a module that has been loaded by the ASTReader.
89 /// Each instance of the Module class corresponds to a single AST file, which
90 /// may be a precompiled header, precompiled preamble, a module, or an AST file
91 /// of some sort loaded as the main file, all of which are specific formulations
92 /// of the general notion of a "module". A module may depend on any number of
96 ModuleFile(ModuleKind Kind, unsigned Generation);
99 // === General information ===
101 /// \brief The index of this module in the list of modules.
104 /// \brief The type of this module.
107 /// \brief The file name of the module file.
108 std::string FileName;
110 /// \brief The original source file name that was used to build the
111 /// primary AST file, which may have been modified for
112 /// relocatable-pch support.
113 std::string OriginalSourceFileName;
115 /// \brief The actual original source file name that was used to
116 /// build this AST file.
117 std::string ActualOriginalSourceFileName;
119 /// \brief The file ID for the original source file that was used to
120 /// build this AST file.
121 FileID OriginalSourceFileID;
123 /// \brief The directory that the PCH was originally created in. Used to
124 /// allow resolving headers even after headers+PCH was moved to a new path.
125 std::string OriginalDir;
127 /// \brief Whether this precompiled header is a relocatable PCH file.
130 /// \brief The file entry for the module file.
131 const FileEntry *File;
133 /// \brief Whether this module has been directly imported by the
135 bool DirectlyImported;
137 /// \brief The generation of which this module file is a part.
140 /// \brief The memory buffer that stores the data associated with
142 OwningPtr<llvm::MemoryBuffer> Buffer;
144 /// \brief The size of this file, in bits.
147 /// \brief The global bit offset (or base) of this module
148 uint64_t GlobalBitOffset;
150 /// \brief The bitstream reader from which we'll read the AST file.
151 llvm::BitstreamReader StreamFile;
153 /// \brief The main bitstream cursor for the main block.
154 llvm::BitstreamCursor Stream;
156 /// \brief The source location where the module was explicitly or implicitly
157 /// imported in the local translation unit.
159 /// If module A depends on and imports module B, both modules will have the
160 /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
161 /// source location inside module A).
162 SourceLocation DirectImportLoc;
164 /// \brief The source location where this module was first imported.
165 SourceLocation ImportLoc;
167 /// \brief The first source location in this module.
168 SourceLocation FirstLoc;
170 // === Input Files ===
171 /// \brief The cursor to the start of the input-files block.
172 llvm::BitstreamCursor InputFilesCursor;
174 /// \brief Offsets for all of the input file entries in the AST file.
175 const uint32_t *InputFileOffsets;
177 /// \brief The input files that have been loaded from this AST file.
178 std::vector<InputFile> InputFilesLoaded;
180 // === Source Locations ===
182 /// \brief Cursor used to read source location entries.
183 llvm::BitstreamCursor SLocEntryCursor;
185 /// \brief The number of source location entries in this AST file.
186 unsigned LocalNumSLocEntries;
188 /// \brief The base ID in the source manager's view of this module.
191 /// \brief The base offset in the source manager's view of this module.
192 unsigned SLocEntryBaseOffset;
194 /// \brief Offsets for all of the source location entries in the
196 const uint32_t *SLocEntryOffsets;
198 /// \brief SLocEntries that we're going to preload.
199 SmallVector<uint64_t, 4> PreloadSLocEntries;
201 /// \brief Remapping table for source locations in this module.
202 ContinuousRangeMap<uint32_t, int, 2> SLocRemap;
204 // === Identifiers ===
206 /// \brief The number of identifiers in this AST file.
207 unsigned LocalNumIdentifiers;
209 /// \brief Offsets into the identifier table data.
211 /// This array is indexed by the identifier ID (-1), and provides
212 /// the offset into IdentifierTableData where the string data is
214 const uint32_t *IdentifierOffsets;
216 /// \brief Base identifier ID for identifiers local to this module.
217 serialization::IdentID BaseIdentifierID;
219 /// \brief Remapping table for identifier IDs in this module.
220 ContinuousRangeMap<uint32_t, int, 2> IdentifierRemap;
222 /// \brief Actual data for the on-disk hash table of identifiers.
224 /// This pointer points into a memory buffer, where the on-disk hash
225 /// table for identifiers actually lives.
226 const char *IdentifierTableData;
228 /// \brief A pointer to an on-disk hash table of opaque type
229 /// IdentifierHashTable.
230 void *IdentifierLookupTable;
234 /// \brief The cursor to the start of the preprocessor block, which stores
235 /// all of the macro definitions.
236 llvm::BitstreamCursor MacroCursor;
238 /// \brief The number of macros in this AST file.
239 unsigned LocalNumMacros;
241 /// \brief Offsets of macros in the preprocessor block.
243 /// This array is indexed by the macro ID (-1), and provides
244 /// the offset into the preprocessor block where macro definitions are
246 const uint32_t *MacroOffsets;
248 /// \brief Base macro ID for macros local to this module.
249 serialization::MacroID BaseMacroID;
251 /// \brief Remapping table for macro IDs in this module.
252 ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
254 /// \brief The offset of the start of the set of defined macros.
255 uint64_t MacroStartOffset;
257 // === Detailed PreprocessingRecord ===
259 /// \brief The cursor to the start of the (optional) detailed preprocessing
261 llvm::BitstreamCursor PreprocessorDetailCursor;
263 /// \brief The offset of the start of the preprocessor detail cursor.
264 uint64_t PreprocessorDetailStartOffset;
266 /// \brief Base preprocessed entity ID for preprocessed entities local to
268 serialization::PreprocessedEntityID BasePreprocessedEntityID;
270 /// \brief Remapping table for preprocessed entity IDs in this module.
271 ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
273 const PPEntityOffset *PreprocessedEntityOffsets;
274 unsigned NumPreprocessedEntities;
276 // === Header search information ===
278 /// \brief The number of local HeaderFileInfo structures.
279 unsigned LocalNumHeaderFileInfos;
281 /// \brief Actual data for the on-disk hash table of header file
284 /// This pointer points into a memory buffer, where the on-disk hash
285 /// table for header file information actually lives.
286 const char *HeaderFileInfoTableData;
288 /// \brief The on-disk hash table that contains information about each of
289 /// the header files.
290 void *HeaderFileInfoTable;
292 // === Submodule information ===
293 /// \brief The number of submodules in this module.
294 unsigned LocalNumSubmodules;
296 /// \brief Base submodule ID for submodules local to this module.
297 serialization::SubmoduleID BaseSubmoduleID;
299 /// \brief Remapping table for submodule IDs in this module.
300 ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
304 /// \brief The number of selectors new to this file.
306 /// This is the number of entries in SelectorOffsets.
307 unsigned LocalNumSelectors;
309 /// \brief Offsets into the selector lookup table's data array
310 /// where each selector resides.
311 const uint32_t *SelectorOffsets;
313 /// \brief Base selector ID for selectors local to this module.
314 serialization::SelectorID BaseSelectorID;
316 /// \brief Remapping table for selector IDs in this module.
317 ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
319 /// \brief A pointer to the character data that comprises the selector table
321 /// The SelectorOffsets table refers into this memory.
322 const unsigned char *SelectorLookupTableData;
324 /// \brief A pointer to an on-disk hash table of opaque type
325 /// ASTSelectorLookupTable.
327 /// This hash table provides the IDs of all selectors, and the associated
328 /// instance and factory methods.
329 void *SelectorLookupTable;
331 // === Declarations ===
333 /// DeclsCursor - This is a cursor to the start of the DECLS_BLOCK block. It
334 /// has read all the abbreviations at the start of the block and is ready to
335 /// jump around with these in context.
336 llvm::BitstreamCursor DeclsCursor;
338 /// \brief The number of declarations in this AST file.
339 unsigned LocalNumDecls;
341 /// \brief Offset of each declaration within the bitstream, indexed
342 /// by the declaration ID (-1).
343 const DeclOffset *DeclOffsets;
345 /// \brief Base declaration ID for declarations local to this module.
346 serialization::DeclID BaseDeclID;
348 /// \brief Remapping table for declaration IDs in this module.
349 ContinuousRangeMap<uint32_t, int, 2> DeclRemap;
351 /// \brief Mapping from the module files that this module file depends on
352 /// to the base declaration ID for that module as it is understood within this
355 /// This is effectively a reverse global-to-local mapping for declaration
356 /// IDs, so that we can interpret a true global ID (for this translation unit)
357 /// as a local ID (for this module file).
358 llvm::DenseMap<ModuleFile *, serialization::DeclID> GlobalToLocalDeclIDs;
360 /// \brief The number of C++ base specifier sets in this AST file.
361 unsigned LocalNumCXXBaseSpecifiers;
363 /// \brief Offset of each C++ base specifier set within the bitstream,
364 /// indexed by the C++ base specifier set ID (-1).
365 const uint32_t *CXXBaseSpecifiersOffsets;
367 typedef llvm::DenseMap<const DeclContext *, DeclContextInfo>
370 /// \brief Information about the lexical and visible declarations
371 /// for each DeclContext.
372 DeclContextInfosMap DeclContextInfos;
374 /// \brief Array of file-level DeclIDs sorted by file.
375 const serialization::DeclID *FileSortedDecls;
376 unsigned NumFileSortedDecls;
378 /// \brief Array of redeclaration chain location information within this
379 /// module file, sorted by the first declaration ID.
380 const serialization::LocalRedeclarationsInfo *RedeclarationsMap;
382 /// \brief The number of redeclaration info entries in RedeclarationsMap.
383 unsigned LocalNumRedeclarationsInMap;
385 /// \brief The redeclaration chains for declarations local to this
387 SmallVector<uint64_t, 1> RedeclarationChains;
389 /// \brief Array of category list location information within this
390 /// module file, sorted by the definition ID.
391 const serialization::ObjCCategoriesInfo *ObjCCategoriesMap;
393 /// \brief The number of redeclaration info entries in ObjCCategoriesMap.
394 unsigned LocalNumObjCCategoriesInMap;
396 /// \brief The Objective-C category lists for categories known to this
398 SmallVector<uint64_t, 1> ObjCCategories;
402 /// \brief The number of types in this AST file.
403 unsigned LocalNumTypes;
405 /// \brief Offset of each type within the bitstream, indexed by the
406 /// type ID, or the representation of a Type*.
407 const uint32_t *TypeOffsets;
409 /// \brief Base type ID for types local to this module as represented in
410 /// the global type ID space.
411 serialization::TypeID BaseTypeIndex;
413 /// \brief Remapping table for type IDs in this module.
414 ContinuousRangeMap<uint32_t, int, 2> TypeRemap;
416 // === Miscellaneous ===
418 /// \brief Diagnostic IDs and their mappings that the user changed.
419 SmallVector<uint64_t, 8> PragmaDiagMappings;
421 /// \brief List of modules which depend on this module
422 llvm::SetVector<ModuleFile *> ImportedBy;
424 /// \brief List of modules which this module depends on
425 llvm::SetVector<ModuleFile *> Imports;
427 /// \brief Determine whether this module was directly imported at
428 /// any point during translation.
429 bool isDirectlyImported() const { return DirectlyImported; }
431 /// \brief Dump debugging output for this module.
435 } // end namespace serialization
437 } // end namespace clang