Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:37:06

0001 //===- ModuleFile.h - Module file description -------------------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 //  This file defines the Module class, which describes a module that has
0010 //  been loaded from an AST file.
0011 //
0012 //===----------------------------------------------------------------------===//
0013 
0014 #ifndef LLVM_CLANG_SERIALIZATION_MODULEFILE_H
0015 #define LLVM_CLANG_SERIALIZATION_MODULEFILE_H
0016 
0017 #include "clang/Basic/FileManager.h"
0018 #include "clang/Basic/LLVM.h"
0019 #include "clang/Basic/Module.h"
0020 #include "clang/Basic/SourceLocation.h"
0021 #include "clang/Serialization/ASTBitCodes.h"
0022 #include "clang/Serialization/ContinuousRangeMap.h"
0023 #include "clang/Serialization/ModuleFileExtension.h"
0024 #include "llvm/ADT/BitVector.h"
0025 #include "llvm/ADT/DenseMap.h"
0026 #include "llvm/ADT/PointerIntPair.h"
0027 #include "llvm/ADT/SetVector.h"
0028 #include "llvm/ADT/SmallVector.h"
0029 #include "llvm/ADT/StringRef.h"
0030 #include "llvm/Bitstream/BitstreamReader.h"
0031 #include "llvm/Support/Endian.h"
0032 #include <cassert>
0033 #include <cstdint>
0034 #include <memory>
0035 #include <string>
0036 #include <vector>
0037 
0038 namespace clang {
0039 
0040 namespace serialization {
0041 
0042 /// Specifies the kind of module that has been loaded.
0043 enum ModuleKind {
0044   /// File is an implicitly-loaded module.
0045   MK_ImplicitModule,
0046 
0047   /// File is an explicitly-loaded module.
0048   MK_ExplicitModule,
0049 
0050   /// File is a PCH file treated as such.
0051   MK_PCH,
0052 
0053   /// File is a PCH file treated as the preamble.
0054   MK_Preamble,
0055 
0056   /// File is a PCH file treated as the actual main file.
0057   MK_MainFile,
0058 
0059   /// File is from a prebuilt module path.
0060   MK_PrebuiltModule
0061 };
0062 
0063 /// The input file info that has been loaded from an AST file.
0064 struct InputFileInfo {
0065   StringRef UnresolvedImportedFilenameAsRequested;
0066   StringRef UnresolvedImportedFilename;
0067 
0068   uint64_t ContentHash;
0069   off_t StoredSize;
0070   time_t StoredTime;
0071   bool Overridden;
0072   bool Transient;
0073   bool TopLevel;
0074   bool ModuleMap;
0075 
0076   bool isValid() const {
0077     return !UnresolvedImportedFilenameAsRequested.empty();
0078   }
0079 };
0080 
0081 /// The input file that has been loaded from this AST file, along with
0082 /// bools indicating whether this was an overridden buffer or if it was
0083 /// out-of-date or not-found.
0084 class InputFile {
0085   enum {
0086     Overridden = 1,
0087     OutOfDate = 2,
0088     NotFound = 3
0089   };
0090   llvm::PointerIntPair<const FileEntryRef::MapEntry *, 2, unsigned> Val;
0091 
0092 public:
0093   InputFile() = default;
0094 
0095   InputFile(FileEntryRef File, bool isOverridden = false,
0096             bool isOutOfDate = false) {
0097     unsigned intVal = 0;
0098     // Make isOutOfDate with higher priority than isOverridden.
0099     // It is possible if the recorded hash value mismatches.
0100     if (isOutOfDate)
0101       intVal = OutOfDate;
0102     else if (isOverridden)
0103       intVal = Overridden;
0104     Val.setPointerAndInt(&File.getMapEntry(), intVal);
0105   }
0106 
0107   static InputFile getNotFound() {
0108     InputFile File;
0109     File.Val.setInt(NotFound);
0110     return File;
0111   }
0112 
0113   OptionalFileEntryRef getFile() const {
0114     if (auto *P = Val.getPointer())
0115       return FileEntryRef(*P);
0116     return std::nullopt;
0117   }
0118   bool isOverridden() const { return Val.getInt() == Overridden; }
0119   bool isOutOfDate() const { return Val.getInt() == OutOfDate; }
0120   bool isNotFound() const { return Val.getInt() == NotFound; }
0121 };
0122 
0123 /// Information about a module that has been loaded by the ASTReader.
0124 ///
0125 /// Each instance of the Module class corresponds to a single AST file, which
0126 /// may be a precompiled header, precompiled preamble, a module, or an AST file
0127 /// of some sort loaded as the main file, all of which are specific formulations
0128 /// of the general notion of a "module". A module may depend on any number of
0129 /// other modules.
0130 class ModuleFile {
0131 public:
0132   ModuleFile(ModuleKind Kind, FileEntryRef File, unsigned Generation)
0133       : Kind(Kind), File(File), Generation(Generation) {}
0134   ~ModuleFile();
0135 
0136   // === General information ===
0137 
0138   /// The index of this module in the list of modules.
0139   unsigned Index = 0;
0140 
0141   /// The type of this module.
0142   ModuleKind Kind;
0143 
0144   /// The file name of the module file.
0145   std::string FileName;
0146 
0147   /// The name of the module.
0148   std::string ModuleName;
0149 
0150   /// The base directory of the module.
0151   std::string BaseDirectory;
0152 
0153   static std::string getTimestampFilename(StringRef FileName) {
0154     return (FileName + ".timestamp").str();
0155   }
0156 
0157   /// The original source file name that was used to build the
0158   /// primary AST file, which may have been modified for
0159   /// relocatable-pch support.
0160   std::string OriginalSourceFileName;
0161 
0162   /// The actual original source file name that was used to
0163   /// build this AST file.
0164   std::string ActualOriginalSourceFileName;
0165 
0166   /// The file ID for the original source file that was used to
0167   /// build this AST file.
0168   FileID OriginalSourceFileID;
0169 
0170   std::string ModuleMapPath;
0171 
0172   /// Whether this precompiled header is a relocatable PCH file.
0173   bool RelocatablePCH = false;
0174 
0175   /// Whether this module file is a standard C++ module.
0176   bool StandardCXXModule = false;
0177 
0178   /// Whether timestamps are included in this module file.
0179   bool HasTimestamps = false;
0180 
0181   /// Whether the top-level module has been read from the AST file.
0182   bool DidReadTopLevelSubmodule = false;
0183 
0184   /// The file entry for the module file.
0185   FileEntryRef File;
0186 
0187   /// The signature of the module file, which may be used instead of the size
0188   /// and modification time to identify this particular file.
0189   ASTFileSignature Signature;
0190 
0191   /// The signature of the AST block of the module file, this can be used to
0192   /// unique module files based on AST contents.
0193   ASTFileSignature ASTBlockHash;
0194 
0195   /// The bit vector denoting usage of each header search entry (true = used).
0196   llvm::BitVector SearchPathUsage;
0197 
0198   /// The bit vector denoting usage of each VFS entry (true = used).
0199   llvm::BitVector VFSUsage;
0200 
0201   /// Whether this module has been directly imported by the
0202   /// user.
0203   bool DirectlyImported = false;
0204 
0205   /// The generation of which this module file is a part.
0206   unsigned Generation;
0207 
0208   /// The memory buffer that stores the data associated with
0209   /// this AST file, owned by the InMemoryModuleCache.
0210   llvm::MemoryBuffer *Buffer = nullptr;
0211 
0212   /// The size of this file, in bits.
0213   uint64_t SizeInBits = 0;
0214 
0215   /// The global bit offset (or base) of this module
0216   uint64_t GlobalBitOffset = 0;
0217 
0218   /// The bit offset of the AST block of this module.
0219   uint64_t ASTBlockStartOffset = 0;
0220 
0221   /// The serialized bitstream data for this file.
0222   StringRef Data;
0223 
0224   /// The main bitstream cursor for the main block.
0225   llvm::BitstreamCursor Stream;
0226 
0227   /// The source location where the module was explicitly or implicitly
0228   /// imported in the local translation unit.
0229   ///
0230   /// If module A depends on and imports module B, both modules will have the
0231   /// same DirectImportLoc, but different ImportLoc (B's ImportLoc will be a
0232   /// source location inside module A).
0233   ///
0234   /// WARNING: This is largely useless. It doesn't tell you when a module was
0235   /// made visible, just when the first submodule of that module was imported.
0236   SourceLocation DirectImportLoc;
0237 
0238   /// The source location where this module was first imported.
0239   SourceLocation ImportLoc;
0240 
0241   /// The first source location in this module.
0242   SourceLocation FirstLoc;
0243 
0244   /// The list of extension readers that are attached to this module
0245   /// file.
0246   std::vector<std::unique_ptr<ModuleFileExtensionReader>> ExtensionReaders;
0247 
0248   /// The module offset map data for this file. If non-empty, the various
0249   /// ContinuousRangeMaps described below have not yet been populated.
0250   StringRef ModuleOffsetMap;
0251 
0252   // === Input Files ===
0253 
0254   /// The cursor to the start of the input-files block.
0255   llvm::BitstreamCursor InputFilesCursor;
0256 
0257   /// Absolute offset of the start of the input-files block.
0258   uint64_t InputFilesOffsetBase = 0;
0259 
0260   /// Relative offsets for all of the input file entries in the AST file.
0261   const llvm::support::unaligned_uint64_t *InputFileOffsets = nullptr;
0262 
0263   /// The input files that have been loaded from this AST file.
0264   std::vector<InputFile> InputFilesLoaded;
0265 
0266   /// The input file infos that have been loaded from this AST file.
0267   std::vector<InputFileInfo> InputFileInfosLoaded;
0268 
0269   // All user input files reside at the index range [0, NumUserInputFiles), and
0270   // system input files reside at [NumUserInputFiles, InputFilesLoaded.size()).
0271   unsigned NumUserInputFiles = 0;
0272 
0273   /// If non-zero, specifies the time when we last validated input
0274   /// files.  Zero means we never validated them.
0275   ///
0276   /// The time is specified in seconds since the start of the Epoch.
0277   uint64_t InputFilesValidationTimestamp = 0;
0278 
0279   // === Source Locations ===
0280 
0281   /// Cursor used to read source location entries.
0282   llvm::BitstreamCursor SLocEntryCursor;
0283 
0284   /// The bit offset to the start of the SOURCE_MANAGER_BLOCK.
0285   uint64_t SourceManagerBlockStartOffset = 0;
0286 
0287   /// The number of source location entries in this AST file.
0288   unsigned LocalNumSLocEntries = 0;
0289 
0290   /// The base ID in the source manager's view of this module.
0291   int SLocEntryBaseID = 0;
0292 
0293   /// The base offset in the source manager's view of this module.
0294   SourceLocation::UIntTy SLocEntryBaseOffset = 0;
0295 
0296   /// Base file offset for the offsets in SLocEntryOffsets. Real file offset
0297   /// for the entry is SLocEntryOffsetsBase + SLocEntryOffsets[i].
0298   uint64_t SLocEntryOffsetsBase = 0;
0299 
0300   /// Offsets for all of the source location entries in the
0301   /// AST file.
0302   const uint32_t *SLocEntryOffsets = nullptr;
0303 
0304   // === Identifiers ===
0305 
0306   /// The number of identifiers in this AST file.
0307   unsigned LocalNumIdentifiers = 0;
0308 
0309   /// Offsets into the identifier table data.
0310   ///
0311   /// This array is indexed by the identifier ID (-1), and provides
0312   /// the offset into IdentifierTableData where the string data is
0313   /// stored.
0314   const uint32_t *IdentifierOffsets = nullptr;
0315 
0316   /// Base identifier ID for identifiers local to this module.
0317   serialization::IdentifierID BaseIdentifierID = 0;
0318 
0319   /// Actual data for the on-disk hash table of identifiers.
0320   ///
0321   /// This pointer points into a memory buffer, where the on-disk hash
0322   /// table for identifiers actually lives.
0323   const unsigned char *IdentifierTableData = nullptr;
0324 
0325   /// A pointer to an on-disk hash table of opaque type
0326   /// IdentifierHashTable.
0327   void *IdentifierLookupTable = nullptr;
0328 
0329   /// Offsets of identifiers that we're going to preload within
0330   /// IdentifierTableData.
0331   std::vector<unsigned> PreloadIdentifierOffsets;
0332 
0333   // === Macros ===
0334 
0335   /// The cursor to the start of the preprocessor block, which stores
0336   /// all of the macro definitions.
0337   llvm::BitstreamCursor MacroCursor;
0338 
0339   /// The number of macros in this AST file.
0340   unsigned LocalNumMacros = 0;
0341 
0342   /// Base file offset for the offsets in MacroOffsets. Real file offset for
0343   /// the entry is MacroOffsetsBase + MacroOffsets[i].
0344   uint64_t MacroOffsetsBase = 0;
0345 
0346   /// Offsets of macros in the preprocessor block.
0347   ///
0348   /// This array is indexed by the macro ID (-1), and provides
0349   /// the offset into the preprocessor block where macro definitions are
0350   /// stored.
0351   const uint32_t *MacroOffsets = nullptr;
0352 
0353   /// Base macro ID for macros local to this module.
0354   serialization::MacroID BaseMacroID = 0;
0355 
0356   /// Remapping table for macro IDs in this module.
0357   ContinuousRangeMap<uint32_t, int, 2> MacroRemap;
0358 
0359   /// The offset of the start of the set of defined macros.
0360   uint64_t MacroStartOffset = 0;
0361 
0362   // === Detailed PreprocessingRecord ===
0363 
0364   /// The cursor to the start of the (optional) detailed preprocessing
0365   /// record block.
0366   llvm::BitstreamCursor PreprocessorDetailCursor;
0367 
0368   /// The offset of the start of the preprocessor detail cursor.
0369   uint64_t PreprocessorDetailStartOffset = 0;
0370 
0371   /// Base preprocessed entity ID for preprocessed entities local to
0372   /// this module.
0373   serialization::PreprocessedEntityID BasePreprocessedEntityID = 0;
0374 
0375   /// Remapping table for preprocessed entity IDs in this module.
0376   ContinuousRangeMap<uint32_t, int, 2> PreprocessedEntityRemap;
0377 
0378   const PPEntityOffset *PreprocessedEntityOffsets = nullptr;
0379   unsigned NumPreprocessedEntities = 0;
0380 
0381   /// Base ID for preprocessed skipped ranges local to this module.
0382   unsigned BasePreprocessedSkippedRangeID = 0;
0383 
0384   const PPSkippedRange *PreprocessedSkippedRangeOffsets = nullptr;
0385   unsigned NumPreprocessedSkippedRanges = 0;
0386 
0387   // === Header search information ===
0388 
0389   /// The number of local HeaderFileInfo structures.
0390   unsigned LocalNumHeaderFileInfos = 0;
0391 
0392   /// Actual data for the on-disk hash table of header file
0393   /// information.
0394   ///
0395   /// This pointer points into a memory buffer, where the on-disk hash
0396   /// table for header file information actually lives.
0397   const char *HeaderFileInfoTableData = nullptr;
0398 
0399   /// The on-disk hash table that contains information about each of
0400   /// the header files.
0401   void *HeaderFileInfoTable = nullptr;
0402 
0403   // === Submodule information ===
0404 
0405   /// The number of submodules in this module.
0406   unsigned LocalNumSubmodules = 0;
0407 
0408   /// Base submodule ID for submodules local to this module.
0409   serialization::SubmoduleID BaseSubmoduleID = 0;
0410 
0411   /// Remapping table for submodule IDs in this module.
0412   ContinuousRangeMap<uint32_t, int, 2> SubmoduleRemap;
0413 
0414   // === Selectors ===
0415 
0416   /// The number of selectors new to this file.
0417   ///
0418   /// This is the number of entries in SelectorOffsets.
0419   unsigned LocalNumSelectors = 0;
0420 
0421   /// Offsets into the selector lookup table's data array
0422   /// where each selector resides.
0423   const uint32_t *SelectorOffsets = nullptr;
0424 
0425   /// Base selector ID for selectors local to this module.
0426   serialization::SelectorID BaseSelectorID = 0;
0427 
0428   /// Remapping table for selector IDs in this module.
0429   ContinuousRangeMap<uint32_t, int, 2> SelectorRemap;
0430 
0431   /// A pointer to the character data that comprises the selector table
0432   ///
0433   /// The SelectorOffsets table refers into this memory.
0434   const unsigned char *SelectorLookupTableData = nullptr;
0435 
0436   /// A pointer to an on-disk hash table of opaque type
0437   /// ASTSelectorLookupTable.
0438   ///
0439   /// This hash table provides the IDs of all selectors, and the associated
0440   /// instance and factory methods.
0441   void *SelectorLookupTable = nullptr;
0442 
0443   // === Declarations ===
0444 
0445   /// DeclsCursor - This is a cursor to the start of the DECLTYPES_BLOCK block.
0446   /// It has read all the abbreviations at the start of the block and is ready
0447   /// to jump around with these in context.
0448   llvm::BitstreamCursor DeclsCursor;
0449 
0450   /// The offset to the start of the DECLTYPES_BLOCK block.
0451   uint64_t DeclsBlockStartOffset = 0;
0452 
0453   /// The number of declarations in this AST file.
0454   unsigned LocalNumDecls = 0;
0455 
0456   /// Offset of each declaration within the bitstream, indexed
0457   /// by the declaration ID (-1).
0458   const DeclOffset *DeclOffsets = nullptr;
0459 
0460   /// Base declaration index in ASTReader for declarations local to this module.
0461   unsigned BaseDeclIndex = 0;
0462 
0463   /// Array of file-level DeclIDs sorted by file.
0464   const serialization::unaligned_decl_id_t *FileSortedDecls = nullptr;
0465   unsigned NumFileSortedDecls = 0;
0466 
0467   /// Array of category list location information within this
0468   /// module file, sorted by the definition ID.
0469   const serialization::ObjCCategoriesInfo *ObjCCategoriesMap = nullptr;
0470 
0471   /// The number of redeclaration info entries in ObjCCategoriesMap.
0472   unsigned LocalNumObjCCategoriesInMap = 0;
0473 
0474   /// The Objective-C category lists for categories known to this
0475   /// module.
0476   SmallVector<uint64_t, 1> ObjCCategories;
0477 
0478   // === Types ===
0479 
0480   /// The number of types in this AST file.
0481   unsigned LocalNumTypes = 0;
0482 
0483   /// Offset of each type within the bitstream, indexed by the
0484   /// type ID, or the representation of a Type*.
0485   const UnalignedUInt64 *TypeOffsets = nullptr;
0486 
0487   /// Base type ID for types local to this module as represented in
0488   /// the global type ID space.
0489   serialization::TypeID BaseTypeIndex = 0;
0490 
0491   // === Miscellaneous ===
0492 
0493   /// Diagnostic IDs and their mappings that the user changed.
0494   SmallVector<uint64_t, 8> PragmaDiagMappings;
0495 
0496   /// List of modules which depend on this module
0497   llvm::SetVector<ModuleFile *> ImportedBy;
0498 
0499   /// List of modules which this module directly imported
0500   llvm::SetVector<ModuleFile *> Imports;
0501 
0502   /// List of modules which this modules dependent on. Different
0503   /// from `Imports`, this includes indirectly imported modules too.
0504   /// The order of TransitiveImports is significant. It should keep
0505   /// the same order with that module file manager when we write
0506   /// the current module file. The value of the member will be initialized
0507   /// in `ASTReader::ReadModuleOffsetMap`.
0508   llvm::SmallVector<ModuleFile *, 16> TransitiveImports;
0509 
0510   /// Determine whether this module was directly imported at
0511   /// any point during translation.
0512   bool isDirectlyImported() const { return DirectlyImported; }
0513 
0514   /// Is this a module file for a module (rather than a PCH or similar).
0515   bool isModule() const {
0516     return Kind == MK_ImplicitModule || Kind == MK_ExplicitModule ||
0517            Kind == MK_PrebuiltModule;
0518   }
0519 
0520   /// Dump debugging output for this module.
0521   void dump();
0522 };
0523 
0524 } // namespace serialization
0525 
0526 } // namespace clang
0527 
0528 #endif // LLVM_CLANG_SERIALIZATION_MODULEFILE_H