ModuleFile.h 17 KB

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