ASTWriter.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTWriter.h - AST File Writer ----------------------------*- 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 ASTWriter class, which writes an AST file
  15. // containing a serialized representation of a translation unit.
  16. //
  17. //===----------------------------------------------------------------------===//
  18. #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
  19. #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
  20. #include "clang/AST/ASTMutationListener.h"
  21. #include "clang/AST/Decl.h"
  22. #include "clang/AST/Type.h"
  23. #include "clang/Basic/LLVM.h"
  24. #include "clang/Basic/Module.h"
  25. #include "clang/Basic/SourceLocation.h"
  26. #include "clang/Sema/Sema.h"
  27. #include "clang/Sema/SemaConsumer.h"
  28. #include "clang/Serialization/ASTBitCodes.h"
  29. #include "clang/Serialization/ASTDeserializationListener.h"
  30. #include "clang/Serialization/PCHContainerOperations.h"
  31. #include "clang/Serialization/SourceLocationEncoding.h"
  32. #include "llvm/ADT/ArrayRef.h"
  33. #include "llvm/ADT/DenseMap.h"
  34. #include "llvm/ADT/DenseSet.h"
  35. #include "llvm/ADT/MapVector.h"
  36. #include "llvm/ADT/STLExtras.h"
  37. #include "llvm/ADT/SetVector.h"
  38. #include "llvm/ADT/SmallVector.h"
  39. #include "llvm/ADT/StringRef.h"
  40. #include "llvm/Bitstream/BitstreamWriter.h"
  41. #include <cassert>
  42. #include <cstddef>
  43. #include <cstdint>
  44. #include <ctime>
  45. #include <memory>
  46. #include <queue>
  47. #include <string>
  48. #include <utility>
  49. #include <vector>
  50. namespace clang {
  51. class ASTContext;
  52. class ASTReader;
  53. class Attr;
  54. class CXXRecordDecl;
  55. class FileEntry;
  56. class FPOptionsOverride;
  57. class FunctionDecl;
  58. class HeaderSearch;
  59. class HeaderSearchOptions;
  60. class IdentifierResolver;
  61. class LangOptions;
  62. class MacroDefinitionRecord;
  63. class MacroInfo;
  64. class Module;
  65. class InMemoryModuleCache;
  66. class ModuleFileExtension;
  67. class ModuleFileExtensionWriter;
  68. class NamedDecl;
  69. class ObjCInterfaceDecl;
  70. class PreprocessingRecord;
  71. class Preprocessor;
  72. class RecordDecl;
  73. class Sema;
  74. class SourceManager;
  75. class Stmt;
  76. class StoredDeclsList;
  77. class SwitchCase;
  78. class Token;
  79. /// Writes an AST file containing the contents of a translation unit.
  80. ///
  81. /// The ASTWriter class produces a bitstream containing the serialized
  82. /// representation of a given abstract syntax tree and its supporting
  83. /// data structures. This bitstream can be de-serialized via an
  84. /// instance of the ASTReader class.
  85. class ASTWriter : public ASTDeserializationListener,
  86. public ASTMutationListener {
  87. public:
  88. friend class ASTDeclWriter;
  89. friend class ASTRecordWriter;
  90. using RecordData = SmallVector<uint64_t, 64>;
  91. using RecordDataImpl = SmallVectorImpl<uint64_t>;
  92. using RecordDataRef = ArrayRef<uint64_t>;
  93. private:
  94. /// Map that provides the ID numbers of each type within the
  95. /// output stream, plus those deserialized from a chained PCH.
  96. ///
  97. /// The ID numbers of types are consecutive (in order of discovery)
  98. /// and start at 1. 0 is reserved for NULL. When types are actually
  99. /// stored in the stream, the ID number is shifted by 2 bits to
  100. /// allow for the const/volatile qualifiers.
  101. ///
  102. /// Keys in the map never have const/volatile qualifiers.
  103. using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
  104. serialization::UnsafeQualTypeDenseMapInfo>;
  105. using LocSeq = SourceLocationSequence;
  106. /// The bitstream writer used to emit this precompiled header.
  107. llvm::BitstreamWriter &Stream;
  108. /// The buffer associated with the bitstream.
  109. const SmallVectorImpl<char> &Buffer;
  110. /// The PCM manager which manages memory buffers for pcm files.
  111. InMemoryModuleCache &ModuleCache;
  112. /// The ASTContext we're writing.
  113. ASTContext *Context = nullptr;
  114. /// The preprocessor we're writing.
  115. Preprocessor *PP = nullptr;
  116. /// The reader of existing AST files, if we're chaining.
  117. ASTReader *Chain = nullptr;
  118. /// The module we're currently writing, if any.
  119. Module *WritingModule = nullptr;
  120. /// The offset of the first bit inside the AST_BLOCK.
  121. uint64_t ASTBlockStartOffset = 0;
  122. /// The range representing all the AST_BLOCK.
  123. std::pair<uint64_t, uint64_t> ASTBlockRange;
  124. /// The base directory for any relative paths we emit.
  125. std::string BaseDirectory;
  126. /// Indicates whether timestamps should be written to the produced
  127. /// module file. This is the case for files implicitly written to the
  128. /// module cache, where we need the timestamps to determine if the module
  129. /// file is up to date, but not otherwise.
  130. bool IncludeTimestamps;
  131. /// Indicates when the AST writing is actively performing
  132. /// serialization, rather than just queueing updates.
  133. bool WritingAST = false;
  134. /// Indicates that we are done serializing the collection of decls
  135. /// and types to emit.
  136. bool DoneWritingDeclsAndTypes = false;
  137. /// Indicates that the AST contained compiler errors.
  138. bool ASTHasCompilerErrors = false;
  139. /// Mapping from input file entries to the index into the
  140. /// offset table where information about that input file is stored.
  141. llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
  142. /// Stores a declaration or a type to be written to the AST file.
  143. class DeclOrType {
  144. public:
  145. DeclOrType(Decl *D) : Stored(D), IsType(false) {}
  146. DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
  147. bool isType() const { return IsType; }
  148. bool isDecl() const { return !IsType; }
  149. QualType getType() const {
  150. assert(isType() && "Not a type!");
  151. return QualType::getFromOpaquePtr(Stored);
  152. }
  153. Decl *getDecl() const {
  154. assert(isDecl() && "Not a decl!");
  155. return static_cast<Decl *>(Stored);
  156. }
  157. private:
  158. void *Stored;
  159. bool IsType;
  160. };
  161. /// The declarations and types to emit.
  162. std::queue<DeclOrType> DeclTypesToEmit;
  163. /// The first ID number we can use for our own declarations.
  164. serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
  165. /// The decl ID that will be assigned to the next new decl.
  166. serialization::DeclID NextDeclID = FirstDeclID;
  167. /// Map that provides the ID numbers of each declaration within
  168. /// the output stream, as well as those deserialized from a chained PCH.
  169. ///
  170. /// The ID numbers of declarations are consecutive (in order of
  171. /// discovery) and start at 2. 1 is reserved for the translation
  172. /// unit, while 0 is reserved for NULL.
  173. llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
  174. /// Offset of each declaration in the bitstream, indexed by
  175. /// the declaration's ID.
  176. std::vector<serialization::DeclOffset> DeclOffsets;
  177. /// The offset of the DECLTYPES_BLOCK. The offsets in DeclOffsets
  178. /// are relative to this value.
  179. uint64_t DeclTypesBlockStartOffset = 0;
  180. /// Sorted (by file offset) vector of pairs of file offset/DeclID.
  181. using LocDeclIDsTy =
  182. SmallVector<std::pair<unsigned, serialization::DeclID>, 64>;
  183. struct DeclIDInFileInfo {
  184. LocDeclIDsTy DeclIDs;
  185. /// Set when the DeclIDs vectors from all files are joined, this
  186. /// indicates the index that this particular vector has in the global one.
  187. unsigned FirstDeclIndex;
  188. };
  189. using FileDeclIDsTy =
  190. llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
  191. /// Map from file SLocEntries to info about the file-level declarations
  192. /// that it contains.
  193. FileDeclIDsTy FileDeclIDs;
  194. void associateDeclWithFile(const Decl *D, serialization::DeclID);
  195. /// The first ID number we can use for our own types.
  196. serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
  197. /// The type ID that will be assigned to the next new type.
  198. serialization::TypeID NextTypeID = FirstTypeID;
  199. /// Map that provides the ID numbers of each type within the
  200. /// output stream, plus those deserialized from a chained PCH.
  201. ///
  202. /// The ID numbers of types are consecutive (in order of discovery)
  203. /// and start at 1. 0 is reserved for NULL. When types are actually
  204. /// stored in the stream, the ID number is shifted by 2 bits to
  205. /// allow for the const/volatile qualifiers.
  206. ///
  207. /// Keys in the map never have const/volatile qualifiers.
  208. TypeIdxMap TypeIdxs;
  209. /// Offset of each type in the bitstream, indexed by
  210. /// the type's ID.
  211. std::vector<serialization::UnderalignedInt64> TypeOffsets;
  212. /// The first ID number we can use for our own identifiers.
  213. serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
  214. /// The identifier ID that will be assigned to the next new identifier.
  215. serialization::IdentID NextIdentID = FirstIdentID;
  216. /// Map that provides the ID numbers of each identifier in
  217. /// the output stream.
  218. ///
  219. /// The ID numbers for identifiers are consecutive (in order of
  220. /// discovery), starting at 1. An ID of zero refers to a NULL
  221. /// IdentifierInfo.
  222. llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
  223. /// The first ID number we can use for our own macros.
  224. serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
  225. /// The identifier ID that will be assigned to the next new identifier.
  226. serialization::MacroID NextMacroID = FirstMacroID;
  227. /// Map that provides the ID numbers of each macro.
  228. llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
  229. struct MacroInfoToEmitData {
  230. const IdentifierInfo *Name;
  231. MacroInfo *MI;
  232. serialization::MacroID ID;
  233. };
  234. /// The macro infos to emit.
  235. std::vector<MacroInfoToEmitData> MacroInfosToEmit;
  236. llvm::DenseMap<const IdentifierInfo *, uint32_t>
  237. IdentMacroDirectivesOffsetMap;
  238. /// @name FlushStmt Caches
  239. /// @{
  240. /// Set of parent Stmts for the currently serializing sub-stmt.
  241. llvm::DenseSet<Stmt *> ParentStmts;
  242. /// Offsets of sub-stmts already serialized. The offset points
  243. /// just after the stmt record.
  244. llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
  245. /// @}
  246. /// Offsets of each of the identifier IDs into the identifier
  247. /// table.
  248. std::vector<uint32_t> IdentifierOffsets;
  249. /// The first ID number we can use for our own submodules.
  250. serialization::SubmoduleID FirstSubmoduleID =
  251. serialization::NUM_PREDEF_SUBMODULE_IDS;
  252. /// The submodule ID that will be assigned to the next new submodule.
  253. serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
  254. /// The first ID number we can use for our own selectors.
  255. serialization::SelectorID FirstSelectorID =
  256. serialization::NUM_PREDEF_SELECTOR_IDS;
  257. /// The selector ID that will be assigned to the next new selector.
  258. serialization::SelectorID NextSelectorID = FirstSelectorID;
  259. /// Map that provides the ID numbers of each Selector.
  260. llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
  261. /// Offset of each selector within the method pool/selector
  262. /// table, indexed by the Selector ID (-1).
  263. std::vector<uint32_t> SelectorOffsets;
  264. /// Mapping from macro definitions (as they occur in the preprocessing
  265. /// record) to the macro IDs.
  266. llvm::DenseMap<const MacroDefinitionRecord *,
  267. serialization::PreprocessedEntityID> MacroDefinitions;
  268. /// Cache of indices of anonymous declarations within their lexical
  269. /// contexts.
  270. llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
  271. /// An update to a Decl.
  272. class DeclUpdate {
  273. /// A DeclUpdateKind.
  274. unsigned Kind;
  275. union {
  276. const Decl *Dcl;
  277. void *Type;
  278. SourceLocation::UIntTy Loc;
  279. unsigned Val;
  280. Module *Mod;
  281. const Attr *Attribute;
  282. };
  283. public:
  284. DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
  285. DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
  286. DeclUpdate(unsigned Kind, QualType Type)
  287. : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
  288. DeclUpdate(unsigned Kind, SourceLocation Loc)
  289. : Kind(Kind), Loc(Loc.getRawEncoding()) {}
  290. DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
  291. DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
  292. DeclUpdate(unsigned Kind, const Attr *Attribute)
  293. : Kind(Kind), Attribute(Attribute) {}
  294. unsigned getKind() const { return Kind; }
  295. const Decl *getDecl() const { return Dcl; }
  296. QualType getType() const { return QualType::getFromOpaquePtr(Type); }
  297. SourceLocation getLoc() const {
  298. return SourceLocation::getFromRawEncoding(Loc);
  299. }
  300. unsigned getNumber() const { return Val; }
  301. Module *getModule() const { return Mod; }
  302. const Attr *getAttr() const { return Attribute; }
  303. };
  304. using UpdateRecord = SmallVector<DeclUpdate, 1>;
  305. using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
  306. /// Mapping from declarations that came from a chained PCH to the
  307. /// record containing modifications to them.
  308. DeclUpdateMap DeclUpdates;
  309. using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
  310. /// Map of first declarations from a chained PCH that point to the
  311. /// most recent declarations in another PCH.
  312. FirstLatestDeclMap FirstLatestDecls;
  313. /// Declarations encountered that might be external
  314. /// definitions.
  315. ///
  316. /// We keep track of external definitions and other 'interesting' declarations
  317. /// as we are emitting declarations to the AST file. The AST file contains a
  318. /// separate record for these declarations, which are provided to the AST
  319. /// consumer by the AST reader. This is behavior is required to properly cope with,
  320. /// e.g., tentative variable definitions that occur within
  321. /// headers. The declarations themselves are stored as declaration
  322. /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
  323. /// record.
  324. SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls;
  325. SmallVector<serialization::DeclID, 16> ModularCodegenDecls;
  326. /// DeclContexts that have received extensions since their serialized
  327. /// form.
  328. ///
  329. /// For namespaces, when we're chaining and encountering a namespace, we check
  330. /// if its primary namespace comes from the chain. If it does, we add the
  331. /// primary to this set, so that we can write out lexical content updates for
  332. /// it.
  333. llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
  334. /// Keeps track of declarations that we must emit, even though we're
  335. /// not guaranteed to be able to find them by walking the AST starting at the
  336. /// translation unit.
  337. SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
  338. /// The set of Objective-C class that have categories we
  339. /// should serialize.
  340. llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
  341. /// The set of declarations that may have redeclaration chains that
  342. /// need to be serialized.
  343. llvm::SmallVector<const Decl *, 16> Redeclarations;
  344. /// A cache of the first local declaration for "interesting"
  345. /// redeclaration chains.
  346. llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
  347. /// Mapping from SwitchCase statements to IDs.
  348. llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
  349. /// The number of statements written to the AST file.
  350. unsigned NumStatements = 0;
  351. /// The number of macros written to the AST file.
  352. unsigned NumMacros = 0;
  353. /// The number of lexical declcontexts written to the AST
  354. /// file.
  355. unsigned NumLexicalDeclContexts = 0;
  356. /// The number of visible declcontexts written to the AST
  357. /// file.
  358. unsigned NumVisibleDeclContexts = 0;
  359. /// A mapping from each known submodule to its ID number, which will
  360. /// be a positive integer.
  361. llvm::DenseMap<const Module *, unsigned> SubmoduleIDs;
  362. /// A list of the module file extension writers.
  363. std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
  364. ModuleFileExtensionWriters;
  365. /// Mapping from a source location entry to whether it is affecting or not.
  366. llvm::BitVector IsSLocAffecting;
  367. /// Mapping from \c FileID to an index into the FileID adjustment table.
  368. std::vector<FileID> NonAffectingFileIDs;
  369. std::vector<unsigned> NonAffectingFileIDAdjustments;
  370. /// Mapping from an offset to an index into the offset adjustment table.
  371. std::vector<SourceRange> NonAffectingRanges;
  372. std::vector<SourceLocation::UIntTy> NonAffectingOffsetAdjustments;
  373. /// Collects input files that didn't affect compilation of the current module,
  374. /// and initializes data structures necessary for leaving those files out
  375. /// during \c SourceManager serialization.
  376. void collectNonAffectingInputFiles();
  377. /// Returns an adjusted \c FileID, accounting for any non-affecting input
  378. /// files.
  379. FileID getAdjustedFileID(FileID FID) const;
  380. /// Returns an adjusted number of \c FileIDs created within the specified \c
  381. /// FileID, accounting for any non-affecting input files.
  382. unsigned getAdjustedNumCreatedFIDs(FileID FID) const;
  383. /// Returns an adjusted \c SourceLocation, accounting for any non-affecting
  384. /// input files.
  385. SourceLocation getAdjustedLocation(SourceLocation Loc) const;
  386. /// Returns an adjusted \c SourceRange, accounting for any non-affecting input
  387. /// files.
  388. SourceRange getAdjustedRange(SourceRange Range) const;
  389. /// Returns an adjusted \c SourceLocation offset, accounting for any
  390. /// non-affecting input files.
  391. SourceLocation::UIntTy getAdjustedOffset(SourceLocation::UIntTy Offset) const;
  392. /// Returns an adjustment for offset into SourceManager, accounting for any
  393. /// non-affecting input files.
  394. SourceLocation::UIntTy getAdjustment(SourceLocation::UIntTy Offset) const;
  395. /// Retrieve or create a submodule ID for this module.
  396. unsigned getSubmoduleID(Module *Mod);
  397. /// Write the given subexpression to the bitstream.
  398. void WriteSubStmt(Stmt *S);
  399. void WriteBlockInfoBlock();
  400. void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
  401. StringRef isysroot);
  402. /// Write out the signature and diagnostic options, and return the signature.
  403. ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
  404. ASTContext &Context);
  405. /// Calculate hash of the pcm content.
  406. static std::pair<ASTFileSignature, ASTFileSignature>
  407. createSignature(StringRef AllBytes, StringRef ASTBlockBytes);
  408. void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
  409. void WriteSourceManagerBlock(SourceManager &SourceMgr,
  410. const Preprocessor &PP);
  411. void writeIncludedFiles(raw_ostream &Out, const Preprocessor &PP);
  412. void WritePreprocessor(const Preprocessor &PP, bool IsModule);
  413. void WriteHeaderSearch(const HeaderSearch &HS);
  414. void WritePreprocessorDetail(PreprocessingRecord &PPRec,
  415. uint64_t MacroOffsetsBase);
  416. void WriteSubmodules(Module *WritingModule);
  417. void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
  418. bool isModule);
  419. unsigned TypeExtQualAbbrev = 0;
  420. void WriteTypeAbbrevs();
  421. void WriteType(QualType T);
  422. bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
  423. bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
  424. void GenerateNameLookupTable(const DeclContext *DC,
  425. llvm::SmallVectorImpl<char> &LookupTable);
  426. uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
  427. uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
  428. void WriteTypeDeclOffsets();
  429. void WriteFileDeclIDsMap();
  430. void WriteComments();
  431. void WriteSelectors(Sema &SemaRef);
  432. void WriteReferencedSelectorsPool(Sema &SemaRef);
  433. void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
  434. bool IsModule);
  435. void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
  436. void WriteDeclContextVisibleUpdate(const DeclContext *DC);
  437. void WriteFPPragmaOptions(const FPOptionsOverride &Opts);
  438. void WriteOpenCLExtensions(Sema &SemaRef);
  439. void WriteCUDAPragmas(Sema &SemaRef);
  440. void WriteObjCCategories();
  441. void WriteLateParsedTemplates(Sema &SemaRef);
  442. void WriteOptimizePragmaOptions(Sema &SemaRef);
  443. void WriteMSStructPragmaOptions(Sema &SemaRef);
  444. void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
  445. void WritePackPragmaOptions(Sema &SemaRef);
  446. void WriteFloatControlPragmaOptions(Sema &SemaRef);
  447. void WriteModuleFileExtension(Sema &SemaRef,
  448. ModuleFileExtensionWriter &Writer);
  449. unsigned DeclParmVarAbbrev = 0;
  450. unsigned DeclContextLexicalAbbrev = 0;
  451. unsigned DeclContextVisibleLookupAbbrev = 0;
  452. unsigned UpdateVisibleAbbrev = 0;
  453. unsigned DeclRecordAbbrev = 0;
  454. unsigned DeclTypedefAbbrev = 0;
  455. unsigned DeclVarAbbrev = 0;
  456. unsigned DeclFieldAbbrev = 0;
  457. unsigned DeclEnumAbbrev = 0;
  458. unsigned DeclObjCIvarAbbrev = 0;
  459. unsigned DeclCXXMethodAbbrev = 0;
  460. unsigned DeclRefExprAbbrev = 0;
  461. unsigned CharacterLiteralAbbrev = 0;
  462. unsigned IntegerLiteralAbbrev = 0;
  463. unsigned ExprImplicitCastAbbrev = 0;
  464. void WriteDeclAbbrevs();
  465. void WriteDecl(ASTContext &Context, Decl *D);
  466. ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
  467. Module *WritingModule);
  468. public:
  469. /// Create a new precompiled header writer that outputs to
  470. /// the given bitstream.
  471. ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
  472. InMemoryModuleCache &ModuleCache,
  473. ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
  474. bool IncludeTimestamps = true);
  475. ~ASTWriter() override;
  476. ASTContext &getASTContext() const {
  477. assert(Context && "requested AST context when not writing AST");
  478. return *Context;
  479. }
  480. const LangOptions &getLangOpts() const;
  481. /// Get a timestamp for output into the AST file. The actual timestamp
  482. /// of the specified file may be ignored if we have been instructed to not
  483. /// include timestamps in the output file.
  484. time_t getTimestampForOutput(const FileEntry *E) const;
  485. /// Write a precompiled header for the given semantic analysis.
  486. ///
  487. /// \param SemaRef a reference to the semantic analysis object that processed
  488. /// the AST to be written into the precompiled header.
  489. ///
  490. /// \param WritingModule The module that we are writing. If null, we are
  491. /// writing a precompiled header.
  492. ///
  493. /// \param isysroot if non-empty, write a relocatable file whose headers
  494. /// are relative to the given system root. If we're writing a module, its
  495. /// build directory will be used in preference to this if both are available.
  496. ///
  497. /// \return the module signature, which eventually will be a hash of
  498. /// the module but currently is merely a random 32-bit number.
  499. ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile,
  500. Module *WritingModule, StringRef isysroot,
  501. bool hasErrors = false,
  502. bool ShouldCacheASTInMemory = false);
  503. /// Emit a token.
  504. void AddToken(const Token &Tok, RecordDataImpl &Record);
  505. /// Emit a AlignPackInfo.
  506. void AddAlignPackInfo(const Sema::AlignPackInfo &Info,
  507. RecordDataImpl &Record);
  508. /// Emit a FileID.
  509. void AddFileID(FileID FID, RecordDataImpl &Record);
  510. /// Emit a source location.
  511. void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record,
  512. LocSeq *Seq = nullptr);
  513. /// Emit a source range.
  514. void AddSourceRange(SourceRange Range, RecordDataImpl &Record,
  515. LocSeq *Seq = nullptr);
  516. /// Emit a reference to an identifier.
  517. void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
  518. /// Get the unique number used to refer to the given selector.
  519. serialization::SelectorID getSelectorRef(Selector Sel);
  520. /// Get the unique number used to refer to the given identifier.
  521. serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
  522. /// Get the unique number used to refer to the given macro.
  523. serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
  524. /// Determine the ID of an already-emitted macro.
  525. serialization::MacroID getMacroID(MacroInfo *MI);
  526. uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name);
  527. /// Emit a reference to a type.
  528. void AddTypeRef(QualType T, RecordDataImpl &Record);
  529. /// Force a type to be emitted and get its ID.
  530. serialization::TypeID GetOrCreateTypeID(QualType T);
  531. /// Determine the type ID of an already-emitted type.
  532. serialization::TypeID getTypeID(QualType T) const;
  533. /// Find the first local declaration of a given local redeclarable
  534. /// decl.
  535. const Decl *getFirstLocalDecl(const Decl *D);
  536. /// Is this a local declaration (that is, one that will be written to
  537. /// our AST file)? This is the case for declarations that are neither imported
  538. /// from another AST file nor predefined.
  539. bool IsLocalDecl(const Decl *D) {
  540. if (D->isFromASTFile())
  541. return false;
  542. auto I = DeclIDs.find(D);
  543. return (I == DeclIDs.end() ||
  544. I->second >= serialization::NUM_PREDEF_DECL_IDS);
  545. };
  546. /// Emit a reference to a declaration.
  547. void AddDeclRef(const Decl *D, RecordDataImpl &Record);
  548. /// Force a declaration to be emitted and get its ID.
  549. serialization::DeclID GetDeclRef(const Decl *D);
  550. /// Determine the declaration ID of an already-emitted
  551. /// declaration.
  552. serialization::DeclID getDeclID(const Decl *D);
  553. unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
  554. /// Add a string to the given record.
  555. void AddString(StringRef Str, RecordDataImpl &Record);
  556. /// Convert a path from this build process into one that is appropriate
  557. /// for emission in the module file.
  558. bool PreparePathForOutput(SmallVectorImpl<char> &Path);
  559. /// Add a path to the given record.
  560. void AddPath(StringRef Path, RecordDataImpl &Record);
  561. /// Emit the current record with the given path as a blob.
  562. void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
  563. StringRef Path);
  564. /// Add a version tuple to the given record
  565. void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
  566. /// Retrieve or create a submodule ID for this module, or return 0 if
  567. /// the submodule is neither local (a submodle of the currently-written module)
  568. /// nor from an imported module.
  569. unsigned getLocalOrImportedSubmoduleID(const Module *Mod);
  570. /// Note that the identifier II occurs at the given offset
  571. /// within the identifier table.
  572. void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
  573. /// Note that the selector Sel occurs at the given offset
  574. /// within the method pool/selector table.
  575. void SetSelectorOffset(Selector Sel, uint32_t Offset);
  576. /// Record an ID for the given switch-case statement.
  577. unsigned RecordSwitchCaseID(SwitchCase *S);
  578. /// Retrieve the ID for the given switch-case statement.
  579. unsigned getSwitchCaseID(SwitchCase *S);
  580. void ClearSwitchCaseIDs();
  581. unsigned getTypeExtQualAbbrev() const {
  582. return TypeExtQualAbbrev;
  583. }
  584. unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
  585. unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
  586. unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
  587. unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
  588. unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
  589. unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
  590. unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
  591. unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
  592. unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
  593. unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
  594. unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
  595. unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
  596. bool hasChain() const { return Chain; }
  597. ASTReader *getChain() const { return Chain; }
  598. bool isWritingStdCXXNamedModules() const {
  599. return WritingModule && WritingModule->isModulePurview();
  600. }
  601. private:
  602. // ASTDeserializationListener implementation
  603. void ReaderInitialized(ASTReader *Reader) override;
  604. void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
  605. void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
  606. void TypeRead(serialization::TypeIdx Idx, QualType T) override;
  607. void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
  608. void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
  609. MacroDefinitionRecord *MD) override;
  610. void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
  611. // ASTMutationListener implementation.
  612. void CompletedTagDefinition(const TagDecl *D) override;
  613. void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
  614. void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
  615. void AddedCXXTemplateSpecialization(
  616. const ClassTemplateDecl *TD,
  617. const ClassTemplateSpecializationDecl *D) override;
  618. void AddedCXXTemplateSpecialization(
  619. const VarTemplateDecl *TD,
  620. const VarTemplateSpecializationDecl *D) override;
  621. void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
  622. const FunctionDecl *D) override;
  623. void ResolvedExceptionSpec(const FunctionDecl *FD) override;
  624. void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
  625. void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
  626. const FunctionDecl *Delete,
  627. Expr *ThisArg) override;
  628. void CompletedImplicitDefinition(const FunctionDecl *D) override;
  629. void InstantiationRequested(const ValueDecl *D) override;
  630. void VariableDefinitionInstantiated(const VarDecl *D) override;
  631. void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
  632. void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
  633. void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
  634. void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
  635. const ObjCInterfaceDecl *IFD) override;
  636. void DeclarationMarkedUsed(const Decl *D) override;
  637. void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
  638. void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
  639. const Attr *Attr) override;
  640. void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override;
  641. void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
  642. void AddedAttributeToRecord(const Attr *Attr,
  643. const RecordDecl *Record) override;
  644. };
  645. /// AST and semantic-analysis consumer that generates a
  646. /// precompiled header from the parsed source code.
  647. class PCHGenerator : public SemaConsumer {
  648. const Preprocessor &PP;
  649. std::string OutputFile;
  650. std::string isysroot;
  651. Sema *SemaPtr;
  652. std::shared_ptr<PCHBuffer> Buffer;
  653. llvm::BitstreamWriter Stream;
  654. ASTWriter Writer;
  655. bool AllowASTWithErrors;
  656. bool ShouldCacheASTInMemory;
  657. protected:
  658. ASTWriter &getWriter() { return Writer; }
  659. const ASTWriter &getWriter() const { return Writer; }
  660. SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
  661. public:
  662. PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache,
  663. StringRef OutputFile, StringRef isysroot,
  664. std::shared_ptr<PCHBuffer> Buffer,
  665. ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
  666. bool AllowASTWithErrors = false, bool IncludeTimestamps = true,
  667. bool ShouldCacheASTInMemory = false);
  668. ~PCHGenerator() override;
  669. void InitializeSema(Sema &S) override { SemaPtr = &S; }
  670. void HandleTranslationUnit(ASTContext &Ctx) override;
  671. ASTMutationListener *GetASTMutationListener() override;
  672. ASTDeserializationListener *GetASTDeserializationListener() override;
  673. bool hasEmittedPCH() const { return Buffer->IsComplete; }
  674. };
  675. } // namespace clang
  676. #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
  677. #ifdef __GNUC__
  678. #pragma GCC diagnostic pop
  679. #endif