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