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