1 //===--- ASTWriter.h - AST File Writer --------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ASTWriter class, which writes an AST file
11 // containing a serialized representation of a translation unit.
13 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
17 #include "clang/AST/ASTMutationListener.h"
18 #include "clang/AST/Decl.h"
19 #include "clang/AST/TemplateBase.h"
20 #include "clang/Frontend/PCHContainerOperations.h"
21 #include "clang/Sema/SemaConsumer.h"
22 #include "clang/Serialization/ASTBitCodes.h"
23 #include "clang/Serialization/ASTDeserializationListener.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/SmallVector.h"
29 #include "llvm/Bitcode/BitstreamWriter.h"
40 class DeclarationName;
43 class NestedNameSpecifier;
44 class CXXBaseSpecifier;
45 class CXXCtorInitializer;
49 class HeaderSearchOptions;
50 class IdentifierResolver;
51 class MacroDefinitionRecord;
54 class OpaqueValueExpr;
57 class MemoryBufferCache;
59 class ModuleFileExtension;
60 class ModuleFileExtensionWriter;
61 class PreprocessedEntity;
62 class PreprocessingRecord;
67 struct StoredDeclsList;
72 class ASTUnresolvedSet;
74 namespace SrcMgr { class SLocEntry; }
76 /// \brief Writes an AST file containing the contents of a translation unit.
78 /// The ASTWriter class produces a bitstream containing the serialized
79 /// representation of a given abstract syntax tree and its supporting
80 /// data structures. This bitstream can be de-serialized via an
81 /// instance of the ASTReader class.
82 class ASTWriter : public ASTDeserializationListener,
83 public ASTMutationListener {
85 typedef SmallVector<uint64_t, 64> RecordData;
86 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
87 typedef ArrayRef<uint64_t> RecordDataRef;
89 friend class ASTDeclWriter;
90 friend class ASTStmtWriter;
91 friend class ASTTypeWriter;
92 friend class ASTRecordWriter;
94 /// \brief Map that provides the ID numbers of each type within the
95 /// output stream, plus those deserialized from a chained PCH.
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.
102 /// Keys in the map never have const/volatile qualifiers.
103 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
104 serialization::UnsafeQualTypeDenseMapInfo>
107 /// \brief The bitstream writer used to emit this precompiled header.
108 llvm::BitstreamWriter &Stream;
110 /// The buffer associated with the bitstream.
111 const SmallVectorImpl<char> &Buffer;
113 /// \brief The PCM manager which manages memory buffers for pcm files.
114 MemoryBufferCache &PCMCache;
116 /// \brief The ASTContext we're writing.
117 ASTContext *Context = nullptr;
119 /// \brief The preprocessor we're writing.
120 Preprocessor *PP = nullptr;
122 /// \brief The reader of existing AST files, if we're chaining.
123 ASTReader *Chain = nullptr;
125 /// \brief The module we're currently writing, if any.
126 Module *WritingModule = nullptr;
128 /// \brief The base directory for any relative paths we emit.
129 std::string BaseDirectory;
131 /// \brief Indicates whether timestamps should be written to the produced
132 /// module file. This is the case for files implicitly written to the
133 /// module cache, where we need the timestamps to determine if the module
134 /// file is up to date, but not otherwise.
135 bool IncludeTimestamps;
137 /// \brief Indicates when the AST writing is actively performing
138 /// serialization, rather than just queueing updates.
139 bool WritingAST = false;
141 /// \brief Indicates that we are done serializing the collection of decls
142 /// and types to emit.
143 bool DoneWritingDeclsAndTypes = false;
145 /// \brief Indicates that the AST contained compiler errors.
146 bool ASTHasCompilerErrors = false;
148 /// \brief Mapping from input file entries to the index into the
149 /// offset table where information about that input file is stored.
150 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
152 /// \brief Stores a declaration or a type to be written to the AST file.
155 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
156 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
158 bool isType() const { return IsType; }
159 bool isDecl() const { return !IsType; }
161 QualType getType() const {
162 assert(isType() && "Not a type!");
163 return QualType::getFromOpaquePtr(Stored);
166 Decl *getDecl() const {
167 assert(isDecl() && "Not a decl!");
168 return static_cast<Decl *>(Stored);
176 /// \brief The declarations and types to emit.
177 std::queue<DeclOrType> DeclTypesToEmit;
179 /// \brief The first ID number we can use for our own declarations.
180 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
182 /// \brief The decl ID that will be assigned to the next new decl.
183 serialization::DeclID NextDeclID = FirstDeclID;
185 /// \brief Map that provides the ID numbers of each declaration within
186 /// the output stream, as well as those deserialized from a chained PCH.
188 /// The ID numbers of declarations are consecutive (in order of
189 /// discovery) and start at 2. 1 is reserved for the translation
190 /// unit, while 0 is reserved for NULL.
191 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
193 /// \brief Offset of each declaration in the bitstream, indexed by
194 /// the declaration's ID.
195 std::vector<serialization::DeclOffset> DeclOffsets;
197 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
198 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
200 struct DeclIDInFileInfo {
201 LocDeclIDsTy DeclIDs;
202 /// \brief Set when the DeclIDs vectors from all files are joined, this
203 /// indicates the index that this particular vector has in the global one.
204 unsigned FirstDeclIndex;
206 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
208 /// \brief Map from file SLocEntries to info about the file-level declarations
209 /// that it contains.
210 FileDeclIDsTy FileDeclIDs;
212 void associateDeclWithFile(const Decl *D, serialization::DeclID);
214 /// \brief The first ID number we can use for our own types.
215 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
217 /// \brief The type ID that will be assigned to the next new type.
218 serialization::TypeID NextTypeID = FirstTypeID;
220 /// \brief Map that provides the ID numbers of each type within the
221 /// output stream, plus those deserialized from a chained PCH.
223 /// The ID numbers of types are consecutive (in order of discovery)
224 /// and start at 1. 0 is reserved for NULL. When types are actually
225 /// stored in the stream, the ID number is shifted by 2 bits to
226 /// allow for the const/volatile qualifiers.
228 /// Keys in the map never have const/volatile qualifiers.
231 /// \brief Offset of each type in the bitstream, indexed by
233 std::vector<uint32_t> TypeOffsets;
235 /// \brief The first ID number we can use for our own identifiers.
236 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
238 /// \brief The identifier ID that will be assigned to the next new identifier.
239 serialization::IdentID NextIdentID = FirstIdentID;
241 /// \brief Map that provides the ID numbers of each identifier in
242 /// the output stream.
244 /// The ID numbers for identifiers are consecutive (in order of
245 /// discovery), starting at 1. An ID of zero refers to a NULL
247 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
249 /// \brief The first ID number we can use for our own macros.
250 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
252 /// \brief The identifier ID that will be assigned to the next new identifier.
253 serialization::MacroID NextMacroID = FirstMacroID;
255 /// \brief Map that provides the ID numbers of each macro.
256 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
258 struct MacroInfoToEmitData {
259 const IdentifierInfo *Name;
261 serialization::MacroID ID;
263 /// \brief The macro infos to emit.
264 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
266 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
268 /// @name FlushStmt Caches
271 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
272 llvm::DenseSet<Stmt *> ParentStmts;
274 /// \brief Offsets of sub-stmts already serialized. The offset points
275 /// just after the stmt record.
276 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
280 /// \brief Offsets of each of the identifier IDs into the identifier
282 std::vector<uint32_t> IdentifierOffsets;
284 /// \brief The first ID number we can use for our own submodules.
285 serialization::SubmoduleID FirstSubmoduleID =
286 serialization::NUM_PREDEF_SUBMODULE_IDS;
288 /// \brief The submodule ID that will be assigned to the next new submodule.
289 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
291 /// \brief The first ID number we can use for our own selectors.
292 serialization::SelectorID FirstSelectorID =
293 serialization::NUM_PREDEF_SELECTOR_IDS;
295 /// \brief The selector ID that will be assigned to the next new selector.
296 serialization::SelectorID NextSelectorID = FirstSelectorID;
298 /// \brief Map that provides the ID numbers of each Selector.
299 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
301 /// \brief Offset of each selector within the method pool/selector
302 /// table, indexed by the Selector ID (-1).
303 std::vector<uint32_t> SelectorOffsets;
305 /// \brief Mapping from macro definitions (as they occur in the preprocessing
306 /// record) to the macro IDs.
307 llvm::DenseMap<const MacroDefinitionRecord *,
308 serialization::PreprocessedEntityID> MacroDefinitions;
310 /// \brief Cache of indices of anonymous declarations within their lexical
312 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
314 /// An update to a Decl.
316 /// A DeclUpdateKind.
324 const Attr *Attribute;
328 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
329 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
330 DeclUpdate(unsigned Kind, QualType Type)
331 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
332 DeclUpdate(unsigned Kind, SourceLocation Loc)
333 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
334 DeclUpdate(unsigned Kind, unsigned Val)
335 : Kind(Kind), Val(Val) {}
336 DeclUpdate(unsigned Kind, Module *M)
337 : Kind(Kind), Mod(M) {}
338 DeclUpdate(unsigned Kind, const Attr *Attribute)
339 : Kind(Kind), Attribute(Attribute) {}
341 unsigned getKind() const { return Kind; }
342 const Decl *getDecl() const { return Dcl; }
343 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
344 SourceLocation getLoc() const {
345 return SourceLocation::getFromRawEncoding(Loc);
347 unsigned getNumber() const { return Val; }
348 Module *getModule() const { return Mod; }
349 const Attr *getAttr() const { return Attribute; }
352 typedef SmallVector<DeclUpdate, 1> UpdateRecord;
353 typedef llvm::MapVector<const Decl *, UpdateRecord> DeclUpdateMap;
354 /// \brief Mapping from declarations that came from a chained PCH to the
355 /// record containing modifications to them.
356 DeclUpdateMap DeclUpdates;
358 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
359 /// \brief Map of first declarations from a chained PCH that point to the
360 /// most recent declarations in another PCH.
361 FirstLatestDeclMap FirstLatestDecls;
363 /// \brief Declarations encountered that might be external
366 /// We keep track of external definitions and other 'interesting' declarations
367 /// as we are emitting declarations to the AST file. The AST file contains a
368 /// separate record for these declarations, which are provided to the AST
369 /// consumer by the AST reader. This is behavior is required to properly cope with,
370 /// e.g., tentative variable definitions that occur within
371 /// headers. The declarations themselves are stored as declaration
372 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
374 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
375 SmallVector<uint64_t, 16> ModularCodegenDecls;
377 /// \brief DeclContexts that have received extensions since their serialized
380 /// For namespaces, when we're chaining and encountering a namespace, we check
381 /// if its primary namespace comes from the chain. If it does, we add the
382 /// primary to this set, so that we can write out lexical content updates for
384 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
386 /// \brief Keeps track of declarations that we must emit, even though we're
387 /// not guaranteed to be able to find them by walking the AST starting at the
388 /// translation unit.
389 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
391 /// \brief The set of Objective-C class that have categories we
392 /// should serialize.
393 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
395 /// \brief The set of declarations that may have redeclaration chains that
396 /// need to be serialized.
397 llvm::SmallVector<const Decl *, 16> Redeclarations;
399 /// \brief A cache of the first local declaration for "interesting"
400 /// redeclaration chains.
401 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
403 /// \brief Mapping from SwitchCase statements to IDs.
404 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
406 /// \brief The number of statements written to the AST file.
407 unsigned NumStatements = 0;
409 /// \brief The number of macros written to the AST file.
410 unsigned NumMacros = 0;
412 /// \brief The number of lexical declcontexts written to the AST
414 unsigned NumLexicalDeclContexts = 0;
416 /// \brief The number of visible declcontexts written to the AST
418 unsigned NumVisibleDeclContexts = 0;
420 /// \brief A mapping from each known submodule to its ID number, which will
421 /// be a positive integer.
422 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
424 /// \brief A list of the module file extension writers.
425 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
426 ModuleFileExtensionWriters;
428 /// \brief Retrieve or create a submodule ID for this module.
429 unsigned getSubmoduleID(Module *Mod);
431 /// \brief Write the given subexpression to the bitstream.
432 void WriteSubStmt(Stmt *S);
434 void WriteBlockInfoBlock();
435 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
436 StringRef isysroot, const std::string &OutputFile);
438 /// Write out the signature and diagnostic options, and return the signature.
439 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
440 ASTContext &Context);
442 /// Calculate hash of the pcm content.
443 static ASTFileSignature createSignature(StringRef Bytes);
445 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
447 void WriteSourceManagerBlock(SourceManager &SourceMgr,
448 const Preprocessor &PP);
449 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
450 void WriteHeaderSearch(const HeaderSearch &HS);
451 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
452 void WriteSubmodules(Module *WritingModule);
454 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
457 unsigned TypeExtQualAbbrev = 0;
458 unsigned TypeFunctionProtoAbbrev = 0;
459 void WriteTypeAbbrevs();
460 void WriteType(QualType T);
462 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
463 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
465 void GenerateNameLookupTable(const DeclContext *DC,
466 llvm::SmallVectorImpl<char> &LookupTable);
467 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
468 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
469 void WriteTypeDeclOffsets();
470 void WriteFileDeclIDsMap();
471 void WriteComments();
472 void WriteSelectors(Sema &SemaRef);
473 void WriteReferencedSelectorsPool(Sema &SemaRef);
474 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
476 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
477 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
478 void WriteFPPragmaOptions(const FPOptions &Opts);
479 void WriteOpenCLExtensions(Sema &SemaRef);
480 void WriteOpenCLExtensionTypes(Sema &SemaRef);
481 void WriteOpenCLExtensionDecls(Sema &SemaRef);
482 void WriteCUDAPragmas(Sema &SemaRef);
483 void WriteObjCCategories();
484 void WriteLateParsedTemplates(Sema &SemaRef);
485 void WriteOptimizePragmaOptions(Sema &SemaRef);
486 void WriteMSStructPragmaOptions(Sema &SemaRef);
487 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
488 void WritePackPragmaOptions(Sema &SemaRef);
489 void WriteModuleFileExtension(Sema &SemaRef,
490 ModuleFileExtensionWriter &Writer);
492 unsigned DeclParmVarAbbrev = 0;
493 unsigned DeclContextLexicalAbbrev = 0;
494 unsigned DeclContextVisibleLookupAbbrev = 0;
495 unsigned UpdateVisibleAbbrev = 0;
496 unsigned DeclRecordAbbrev = 0;
497 unsigned DeclTypedefAbbrev = 0;
498 unsigned DeclVarAbbrev = 0;
499 unsigned DeclFieldAbbrev = 0;
500 unsigned DeclEnumAbbrev = 0;
501 unsigned DeclObjCIvarAbbrev = 0;
502 unsigned DeclCXXMethodAbbrev = 0;
504 unsigned DeclRefExprAbbrev = 0;
505 unsigned CharacterLiteralAbbrev = 0;
506 unsigned IntegerLiteralAbbrev = 0;
507 unsigned ExprImplicitCastAbbrev = 0;
509 void WriteDeclAbbrevs();
510 void WriteDecl(ASTContext &Context, Decl *D);
512 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
513 const std::string &OutputFile,
514 Module *WritingModule);
517 /// \brief Create a new precompiled header writer that outputs to
518 /// the given bitstream.
519 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
520 MemoryBufferCache &PCMCache,
521 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
522 bool IncludeTimestamps = true);
523 ~ASTWriter() override;
525 const LangOptions &getLangOpts() const;
527 /// \brief Get a timestamp for output into the AST file. The actual timestamp
528 /// of the specified file may be ignored if we have been instructed to not
529 /// include timestamps in the output file.
530 time_t getTimestampForOutput(const FileEntry *E) const;
532 /// \brief Write a precompiled header for the given semantic analysis.
534 /// \param SemaRef a reference to the semantic analysis object that processed
535 /// the AST to be written into the precompiled header.
537 /// \param WritingModule The module that we are writing. If null, we are
538 /// writing a precompiled header.
540 /// \param isysroot if non-empty, write a relocatable file whose headers
541 /// are relative to the given system root. If we're writing a module, its
542 /// build directory will be used in preference to this if both are available.
544 /// \return the module signature, which eventually will be a hash of
545 /// the module but currently is merely a random 32-bit number.
546 ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
547 Module *WritingModule, StringRef isysroot,
548 bool hasErrors = false);
550 /// \brief Emit a token.
551 void AddToken(const Token &Tok, RecordDataImpl &Record);
553 /// \brief Emit a source location.
554 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
556 /// \brief Emit a source range.
557 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
559 /// \brief Emit a reference to an identifier.
560 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
562 /// \brief Get the unique number used to refer to the given selector.
563 serialization::SelectorID getSelectorRef(Selector Sel);
565 /// \brief Get the unique number used to refer to the given identifier.
566 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
568 /// \brief Get the unique number used to refer to the given macro.
569 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
571 /// \brief Determine the ID of an already-emitted macro.
572 serialization::MacroID getMacroID(MacroInfo *MI);
574 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
576 /// \brief Emit a reference to a type.
577 void AddTypeRef(QualType T, RecordDataImpl &Record);
579 /// \brief Force a type to be emitted and get its ID.
580 serialization::TypeID GetOrCreateTypeID(QualType T);
582 /// \brief Determine the type ID of an already-emitted type.
583 serialization::TypeID getTypeID(QualType T) const;
585 /// \brief Find the first local declaration of a given local redeclarable
587 const Decl *getFirstLocalDecl(const Decl *D);
589 /// \brief Is this a local declaration (that is, one that will be written to
590 /// our AST file)? This is the case for declarations that are neither imported
591 /// from another AST file nor predefined.
592 bool IsLocalDecl(const Decl *D) {
593 if (D->isFromASTFile())
595 auto I = DeclIDs.find(D);
596 return (I == DeclIDs.end() ||
597 I->second >= serialization::NUM_PREDEF_DECL_IDS);
600 /// \brief Emit a reference to a declaration.
601 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
604 /// \brief Force a declaration to be emitted and get its ID.
605 serialization::DeclID GetDeclRef(const Decl *D);
607 /// \brief Determine the declaration ID of an already-emitted
609 serialization::DeclID getDeclID(const Decl *D);
611 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
613 /// \brief Add a string to the given record.
614 void AddString(StringRef Str, RecordDataImpl &Record);
616 /// \brief Convert a path from this build process into one that is appropriate
617 /// for emission in the module file.
618 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
620 /// \brief Add a path to the given record.
621 void AddPath(StringRef Path, RecordDataImpl &Record);
623 /// \brief Emit the current record with the given path as a blob.
624 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
627 /// \brief Add a version tuple to the given record
628 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
630 /// \brief Retrieve or create a submodule ID for this module, or return 0 if
631 /// the submodule is neither local (a submodle of the currently-written module)
632 /// nor from an imported module.
633 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
635 /// \brief Note that the identifier II occurs at the given offset
636 /// within the identifier table.
637 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
639 /// \brief Note that the selector Sel occurs at the given offset
640 /// within the method pool/selector table.
641 void SetSelectorOffset(Selector Sel, uint32_t Offset);
643 /// \brief Record an ID for the given switch-case statement.
644 unsigned RecordSwitchCaseID(SwitchCase *S);
646 /// \brief Retrieve the ID for the given switch-case statement.
647 unsigned getSwitchCaseID(SwitchCase *S);
649 void ClearSwitchCaseIDs();
651 unsigned getTypeExtQualAbbrev() const {
652 return TypeExtQualAbbrev;
654 unsigned getTypeFunctionProtoAbbrev() const {
655 return TypeFunctionProtoAbbrev;
658 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
659 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
660 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
661 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
662 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
663 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
664 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
665 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
667 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
668 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
669 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
670 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
672 bool hasChain() const { return Chain; }
673 ASTReader *getChain() const { return Chain; }
676 // ASTDeserializationListener implementation
677 void ReaderInitialized(ASTReader *Reader) override;
678 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
679 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
680 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
681 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
682 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
683 MacroDefinitionRecord *MD) override;
684 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
686 // ASTMutationListener implementation.
687 void CompletedTagDefinition(const TagDecl *D) override;
688 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
689 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
690 void AddedCXXTemplateSpecialization(
691 const ClassTemplateDecl *TD,
692 const ClassTemplateSpecializationDecl *D) override;
693 void AddedCXXTemplateSpecialization(
694 const VarTemplateDecl *TD,
695 const VarTemplateSpecializationDecl *D) override;
696 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
697 const FunctionDecl *D) override;
698 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
699 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
700 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
701 const FunctionDecl *Delete) override;
702 void CompletedImplicitDefinition(const FunctionDecl *D) override;
703 void StaticDataMemberInstantiated(const VarDecl *D) override;
704 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
705 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
706 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
707 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
708 const ObjCInterfaceDecl *IFD) override;
709 void DeclarationMarkedUsed(const Decl *D) override;
710 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
711 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
712 const Attr *Attr) override;
713 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
714 void AddedAttributeToRecord(const Attr *Attr,
715 const RecordDecl *Record) override;
718 /// \brief An object for streaming information to a record.
719 class ASTRecordWriter {
721 ASTWriter::RecordDataImpl *Record;
723 /// \brief Statements that we've encountered while serializing a
724 /// declaration or type.
725 SmallVector<Stmt *, 16> StmtsToEmit;
727 /// \brief Indices of record elements that describe offsets within the
728 /// bitcode. These will be converted to offsets relative to the current
729 /// record when emitted.
730 SmallVector<unsigned, 8> OffsetIndices;
732 /// \brief Flush all of the statements and expressions that have
733 /// been added to the queue via AddStmt().
735 void FlushSubStmts();
737 void PrepareToEmit(uint64_t MyOffset) {
738 // Convert offsets into relative form.
739 for (unsigned I : OffsetIndices) {
740 auto &StoredOffset = (*Record)[I];
741 assert(StoredOffset < MyOffset && "invalid offset");
743 StoredOffset = MyOffset - StoredOffset;
745 OffsetIndices.clear();
749 /// Construct a ASTRecordWriter that uses the default encoding scheme.
750 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
751 : Writer(&Writer), Record(&Record) {}
753 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
755 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
756 : Writer(Parent.Writer), Record(&Record) {}
758 /// Copying an ASTRecordWriter is almost certainly a bug.
759 ASTRecordWriter(const ASTRecordWriter&) = delete;
760 void operator=(const ASTRecordWriter&) = delete;
762 /// \brief Extract the underlying record storage.
763 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
765 /// \brief Minimal vector-like interface.
767 void push_back(uint64_t N) { Record->push_back(N); }
768 template<typename InputIterator>
769 void append(InputIterator begin, InputIterator end) {
770 Record->append(begin, end);
772 bool empty() const { return Record->empty(); }
773 size_t size() const { return Record->size(); }
774 uint64_t &operator[](size_t N) { return (*Record)[N]; }
777 /// \brief Emit the record to the stream, followed by its substatements, and
778 /// return its offset.
779 // FIXME: Allow record producers to suggest Abbrevs.
780 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
781 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
782 PrepareToEmit(Offset);
783 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
788 /// \brief Emit the record to the stream, preceded by its substatements.
789 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
791 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
792 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
793 return Writer->Stream.GetCurrentBitNo();
796 /// \brief Add a bit offset into the record. This will be converted into an
797 /// offset relative to the current record when emitted.
798 void AddOffset(uint64_t BitOffset) {
799 OffsetIndices.push_back(Record->size());
800 Record->push_back(BitOffset);
803 /// \brief Add the given statement or expression to the queue of
804 /// statements to emit.
806 /// This routine should be used when emitting types and declarations
807 /// that have expressions as part of their formulation. Once the
808 /// type or declaration has been written, Emit() will write
809 /// the corresponding statements just after the record.
810 void AddStmt(Stmt *S) {
811 StmtsToEmit.push_back(S);
814 /// \brief Add a definition for the given function to the queue of statements
816 void AddFunctionDefinition(const FunctionDecl *FD);
818 /// \brief Emit a source location.
819 void AddSourceLocation(SourceLocation Loc) {
820 return Writer->AddSourceLocation(Loc, *Record);
823 /// \brief Emit a source range.
824 void AddSourceRange(SourceRange Range) {
825 return Writer->AddSourceRange(Range, *Record);
828 /// \brief Emit an integral value.
829 void AddAPInt(const llvm::APInt &Value);
831 /// \brief Emit a signed integral value.
832 void AddAPSInt(const llvm::APSInt &Value);
834 /// \brief Emit a floating-point value.
835 void AddAPFloat(const llvm::APFloat &Value);
837 /// \brief Emit a reference to an identifier.
838 void AddIdentifierRef(const IdentifierInfo *II) {
839 return Writer->AddIdentifierRef(II, *Record);
842 /// \brief Emit a Selector (which is a smart pointer reference).
843 void AddSelectorRef(Selector S);
845 /// \brief Emit a CXXTemporary.
846 void AddCXXTemporary(const CXXTemporary *Temp);
848 /// \brief Emit a C++ base specifier.
849 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
851 /// \brief Emit a set of C++ base specifiers.
852 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
854 /// \brief Emit a reference to a type.
855 void AddTypeRef(QualType T) {
856 return Writer->AddTypeRef(T, *Record);
859 /// \brief Emits a reference to a declarator info.
860 void AddTypeSourceInfo(TypeSourceInfo *TInfo);
862 /// \brief Emits a type with source-location information.
863 void AddTypeLoc(TypeLoc TL);
865 /// \brief Emits a template argument location info.
866 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
867 const TemplateArgumentLocInfo &Arg);
869 /// \brief Emits a template argument location.
870 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
872 /// \brief Emits an AST template argument list info.
873 void AddASTTemplateArgumentListInfo(
874 const ASTTemplateArgumentListInfo *ASTTemplArgList);
876 /// \brief Emit a reference to a declaration.
877 void AddDeclRef(const Decl *D) {
878 return Writer->AddDeclRef(D, *Record);
881 /// \brief Emit a declaration name.
882 void AddDeclarationName(DeclarationName Name);
884 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
885 DeclarationName Name);
886 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
888 void AddQualifierInfo(const QualifierInfo &Info);
890 /// \brief Emit a nested name specifier.
891 void AddNestedNameSpecifier(NestedNameSpecifier *NNS);
893 /// \brief Emit a nested name specifier with source-location information.
894 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
896 /// \brief Emit a template name.
897 void AddTemplateName(TemplateName Name);
899 /// \brief Emit a template argument.
900 void AddTemplateArgument(const TemplateArgument &Arg);
902 /// \brief Emit a template parameter list.
903 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
905 /// \brief Emit a template argument list.
906 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
908 /// \brief Emit a UnresolvedSet structure.
909 void AddUnresolvedSet(const ASTUnresolvedSet &Set);
911 /// \brief Emit a CXXCtorInitializer array.
912 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer*> CtorInits);
914 void AddCXXDefinitionData(const CXXRecordDecl *D);
916 /// \brief Emit a string.
917 void AddString(StringRef Str) {
918 return Writer->AddString(Str, *Record);
921 /// \brief Emit a path.
922 void AddPath(StringRef Path) {
923 return Writer->AddPath(Path, *Record);
926 /// \brief Emit a version tuple.
927 void AddVersionTuple(const VersionTuple &Version) {
928 return Writer->AddVersionTuple(Version, *Record);
931 /// \brief Emit a list of attributes.
932 void AddAttributes(ArrayRef<const Attr*> Attrs);
935 /// \brief AST and semantic-analysis consumer that generates a
936 /// precompiled header from the parsed source code.
937 class PCHGenerator : public SemaConsumer {
938 const Preprocessor &PP;
939 std::string OutputFile;
940 std::string isysroot;
942 std::shared_ptr<PCHBuffer> Buffer;
943 llvm::BitstreamWriter Stream;
945 bool AllowASTWithErrors;
948 ASTWriter &getWriter() { return Writer; }
949 const ASTWriter &getWriter() const { return Writer; }
950 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
953 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
954 std::shared_ptr<PCHBuffer> Buffer,
955 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
956 bool AllowASTWithErrors = false, bool IncludeTimestamps = true);
957 ~PCHGenerator() override;
958 void InitializeSema(Sema &S) override { SemaPtr = &S; }
959 void HandleTranslationUnit(ASTContext &Ctx) override;
960 ASTMutationListener *GetASTMutationListener() override;
961 ASTDeserializationListener *GetASTDeserializationListener() override;
962 bool hasEmittedPCH() const { return Buffer->IsComplete; }
965 } // end namespace clang