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