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