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 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
16 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
18 #include "clang/AST/ASTMutationListener.h"
19 #include "clang/AST/Decl.h"
20 #include "clang/AST/DeclarationName.h"
21 #include "clang/AST/NestedNameSpecifier.h"
22 #include "clang/AST/TemplateBase.h"
23 #include "clang/AST/TemplateName.h"
24 #include "clang/AST/Type.h"
25 #include "clang/AST/TypeLoc.h"
26 #include "clang/Basic/LLVM.h"
27 #include "clang/Basic/SourceLocation.h"
28 #include "clang/Frontend/PCHContainerOperations.h"
29 #include "clang/Sema/SemaConsumer.h"
30 #include "clang/Serialization/ASTBitCodes.h"
31 #include "clang/Serialization/ASTDeserializationListener.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/SetVector.h"
37 #include "llvm/ADT/SmallVector.h"
38 #include "llvm/ADT/StringRef.h"
39 #include "llvm/Bitcode/BitstreamWriter.h"
62 class ASTUnresolvedSet;
64 class CXXBaseSpecifier;
65 class CXXCtorInitializer;
72 class HeaderSearchOptions;
73 class IdentifierResolver;
75 class MacroDefinitionRecord;
77 class MemoryBufferCache;
79 class ModuleFileExtension;
80 class ModuleFileExtensionWriter;
82 class NestedNameSpecifier;
83 class ObjCInterfaceDecl;
84 class PreprocessingRecord;
91 struct StoredDeclsList;
93 class TemplateParameterList;
97 /// Writes an AST file containing the contents of a translation unit.
99 /// The ASTWriter class produces a bitstream containing the serialized
100 /// representation of a given abstract syntax tree and its supporting
101 /// data structures. This bitstream can be de-serialized via an
102 /// instance of the ASTReader class.
103 class ASTWriter : public ASTDeserializationListener,
104 public ASTMutationListener {
106 friend class ASTDeclWriter;
107 friend class ASTRecordWriter;
108 friend class ASTStmtWriter;
109 friend class ASTTypeWriter;
111 using RecordData = SmallVector<uint64_t, 64>;
112 using RecordDataImpl = SmallVectorImpl<uint64_t>;
113 using RecordDataRef = ArrayRef<uint64_t>;
116 /// Map that provides the ID numbers of each type within the
117 /// output stream, plus those deserialized from a chained PCH.
119 /// The ID numbers of types are consecutive (in order of discovery)
120 /// and start at 1. 0 is reserved for NULL. When types are actually
121 /// stored in the stream, the ID number is shifted by 2 bits to
122 /// allow for the const/volatile qualifiers.
124 /// Keys in the map never have const/volatile qualifiers.
125 using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
126 serialization::UnsafeQualTypeDenseMapInfo>;
128 /// The bitstream writer used to emit this precompiled header.
129 llvm::BitstreamWriter &Stream;
131 /// The buffer associated with the bitstream.
132 const SmallVectorImpl<char> &Buffer;
134 /// The PCM manager which manages memory buffers for pcm files.
135 MemoryBufferCache &PCMCache;
137 /// The ASTContext we're writing.
138 ASTContext *Context = nullptr;
140 /// The preprocessor we're writing.
141 Preprocessor *PP = nullptr;
143 /// The reader of existing AST files, if we're chaining.
144 ASTReader *Chain = nullptr;
146 /// The module we're currently writing, if any.
147 Module *WritingModule = nullptr;
149 /// The base directory for any relative paths we emit.
150 std::string BaseDirectory;
152 /// Indicates whether timestamps should be written to the produced
153 /// module file. This is the case for files implicitly written to the
154 /// module cache, where we need the timestamps to determine if the module
155 /// file is up to date, but not otherwise.
156 bool IncludeTimestamps;
158 /// Indicates when the AST writing is actively performing
159 /// serialization, rather than just queueing updates.
160 bool WritingAST = false;
162 /// Indicates that we are done serializing the collection of decls
163 /// and types to emit.
164 bool DoneWritingDeclsAndTypes = false;
166 /// Indicates that the AST contained compiler errors.
167 bool ASTHasCompilerErrors = false;
169 /// Mapping from input file entries to the index into the
170 /// offset table where information about that input file is stored.
171 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
173 /// Stores a declaration or a type to be written to the AST file.
176 DeclOrType(Decl *D) : Stored(D), IsType(false) {}
177 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
179 bool isType() const { return IsType; }
180 bool isDecl() const { return !IsType; }
182 QualType getType() const {
183 assert(isType() && "Not a type!");
184 return QualType::getFromOpaquePtr(Stored);
187 Decl *getDecl() const {
188 assert(isDecl() && "Not a decl!");
189 return static_cast<Decl *>(Stored);
197 /// The declarations and types to emit.
198 std::queue<DeclOrType> DeclTypesToEmit;
200 /// The first ID number we can use for our own declarations.
201 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
203 /// The decl ID that will be assigned to the next new decl.
204 serialization::DeclID NextDeclID = FirstDeclID;
206 /// Map that provides the ID numbers of each declaration within
207 /// the output stream, as well as those deserialized from a chained PCH.
209 /// The ID numbers of declarations are consecutive (in order of
210 /// discovery) and start at 2. 1 is reserved for the translation
211 /// unit, while 0 is reserved for NULL.
212 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
214 /// Offset of each declaration in the bitstream, indexed by
215 /// the declaration's ID.
216 std::vector<serialization::DeclOffset> DeclOffsets;
218 /// Sorted (by file offset) vector of pairs of file offset/DeclID.
220 SmallVector<std::pair<unsigned, serialization::DeclID>, 64>;
221 struct DeclIDInFileInfo {
222 LocDeclIDsTy DeclIDs;
224 /// Set when the DeclIDs vectors from all files are joined, this
225 /// indicates the index that this particular vector has in the global one.
226 unsigned FirstDeclIndex;
228 using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>;
230 /// Map from file SLocEntries to info about the file-level declarations
231 /// that it contains.
232 FileDeclIDsTy FileDeclIDs;
234 void associateDeclWithFile(const Decl *D, serialization::DeclID);
236 /// The first ID number we can use for our own types.
237 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
239 /// The type ID that will be assigned to the next new type.
240 serialization::TypeID NextTypeID = FirstTypeID;
242 /// Map that provides the ID numbers of each type within the
243 /// output stream, plus those deserialized from a chained PCH.
245 /// The ID numbers of types are consecutive (in order of discovery)
246 /// and start at 1. 0 is reserved for NULL. When types are actually
247 /// stored in the stream, the ID number is shifted by 2 bits to
248 /// allow for the const/volatile qualifiers.
250 /// Keys in the map never have const/volatile qualifiers.
253 /// Offset of each type in the bitstream, indexed by
255 std::vector<uint32_t> TypeOffsets;
257 /// The first ID number we can use for our own identifiers.
258 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
260 /// The identifier ID that will be assigned to the next new identifier.
261 serialization::IdentID NextIdentID = FirstIdentID;
263 /// Map that provides the ID numbers of each identifier in
264 /// the output stream.
266 /// The ID numbers for identifiers are consecutive (in order of
267 /// discovery), starting at 1. An ID of zero refers to a NULL
269 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
271 /// The first ID number we can use for our own macros.
272 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
274 /// The identifier ID that will be assigned to the next new identifier.
275 serialization::MacroID NextMacroID = FirstMacroID;
277 /// Map that provides the ID numbers of each macro.
278 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
280 struct MacroInfoToEmitData {
281 const IdentifierInfo *Name;
283 serialization::MacroID ID;
286 /// The macro infos to emit.
287 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
289 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
291 /// @name FlushStmt Caches
294 /// Set of parent Stmts for the currently serializing sub-stmt.
295 llvm::DenseSet<Stmt *> ParentStmts;
297 /// Offsets of sub-stmts already serialized. The offset points
298 /// just after the stmt record.
299 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
303 /// Offsets of each of the identifier IDs into the identifier
305 std::vector<uint32_t> IdentifierOffsets;
307 /// The first ID number we can use for our own submodules.
308 serialization::SubmoduleID FirstSubmoduleID =
309 serialization::NUM_PREDEF_SUBMODULE_IDS;
311 /// The submodule ID that will be assigned to the next new submodule.
312 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
314 /// The first ID number we can use for our own selectors.
315 serialization::SelectorID FirstSelectorID =
316 serialization::NUM_PREDEF_SELECTOR_IDS;
318 /// The selector ID that will be assigned to the next new selector.
319 serialization::SelectorID NextSelectorID = FirstSelectorID;
321 /// Map that provides the ID numbers of each Selector.
322 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
324 /// Offset of each selector within the method pool/selector
325 /// table, indexed by the Selector ID (-1).
326 std::vector<uint32_t> SelectorOffsets;
328 /// Mapping from macro definitions (as they occur in the preprocessing
329 /// record) to the macro IDs.
330 llvm::DenseMap<const MacroDefinitionRecord *,
331 serialization::PreprocessedEntityID> MacroDefinitions;
333 /// Cache of indices of anonymous declarations within their lexical
335 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
337 /// An update to a Decl.
339 /// A DeclUpdateKind.
347 const Attr *Attribute;
351 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
352 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
353 DeclUpdate(unsigned Kind, QualType Type)
354 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
355 DeclUpdate(unsigned Kind, SourceLocation Loc)
356 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
357 DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
358 DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
359 DeclUpdate(unsigned Kind, const Attr *Attribute)
360 : Kind(Kind), Attribute(Attribute) {}
362 unsigned getKind() const { return Kind; }
363 const Decl *getDecl() const { return Dcl; }
364 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
366 SourceLocation getLoc() const {
367 return SourceLocation::getFromRawEncoding(Loc);
370 unsigned getNumber() const { return Val; }
371 Module *getModule() const { return Mod; }
372 const Attr *getAttr() const { return Attribute; }
375 using UpdateRecord = SmallVector<DeclUpdate, 1>;
376 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
378 /// Mapping from declarations that came from a chained PCH to the
379 /// record containing modifications to them.
380 DeclUpdateMap DeclUpdates;
382 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
384 /// Map of first declarations from a chained PCH that point to the
385 /// most recent declarations in another PCH.
386 FirstLatestDeclMap FirstLatestDecls;
388 /// Declarations encountered that might be external
391 /// We keep track of external definitions and other 'interesting' declarations
392 /// as we are emitting declarations to the AST file. The AST file contains a
393 /// separate record for these declarations, which are provided to the AST
394 /// consumer by the AST reader. This is behavior is required to properly cope with,
395 /// e.g., tentative variable definitions that occur within
396 /// headers. The declarations themselves are stored as declaration
397 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
399 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
400 SmallVector<uint64_t, 16> ModularCodegenDecls;
402 /// DeclContexts that have received extensions since their serialized
405 /// For namespaces, when we're chaining and encountering a namespace, we check
406 /// if its primary namespace comes from the chain. If it does, we add the
407 /// primary to this set, so that we can write out lexical content updates for
409 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
411 /// Keeps track of declarations that we must emit, even though we're
412 /// not guaranteed to be able to find them by walking the AST starting at the
413 /// translation unit.
414 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
416 /// The set of Objective-C class that have categories we
417 /// should serialize.
418 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
420 /// The set of declarations that may have redeclaration chains that
421 /// need to be serialized.
422 llvm::SmallVector<const Decl *, 16> Redeclarations;
424 /// A cache of the first local declaration for "interesting"
425 /// redeclaration chains.
426 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
428 /// Mapping from SwitchCase statements to IDs.
429 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
431 /// The number of statements written to the AST file.
432 unsigned NumStatements = 0;
434 /// The number of macros written to the AST file.
435 unsigned NumMacros = 0;
437 /// The number of lexical declcontexts written to the AST
439 unsigned NumLexicalDeclContexts = 0;
441 /// The number of visible declcontexts written to the AST
443 unsigned NumVisibleDeclContexts = 0;
445 /// A mapping from each known submodule to its ID number, which will
446 /// be a positive integer.
447 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
449 /// A list of the module file extension writers.
450 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
451 ModuleFileExtensionWriters;
453 /// Retrieve or create a submodule ID for this module.
454 unsigned getSubmoduleID(Module *Mod);
456 /// Write the given subexpression to the bitstream.
457 void WriteSubStmt(Stmt *S);
459 void WriteBlockInfoBlock();
460 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
461 StringRef isysroot, const std::string &OutputFile);
463 /// Write out the signature and diagnostic options, and return the signature.
464 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
465 ASTContext &Context);
467 /// Calculate hash of the pcm content.
468 static ASTFileSignature createSignature(StringRef Bytes);
470 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
472 void WriteSourceManagerBlock(SourceManager &SourceMgr,
473 const Preprocessor &PP);
474 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
475 void WriteHeaderSearch(const HeaderSearch &HS);
476 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
477 void WriteSubmodules(Module *WritingModule);
479 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
482 unsigned TypeExtQualAbbrev = 0;
483 unsigned TypeFunctionProtoAbbrev = 0;
484 void WriteTypeAbbrevs();
485 void WriteType(QualType T);
487 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
488 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
490 void GenerateNameLookupTable(const DeclContext *DC,
491 llvm::SmallVectorImpl<char> &LookupTable);
492 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
493 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
494 void WriteTypeDeclOffsets();
495 void WriteFileDeclIDsMap();
496 void WriteComments();
497 void WriteSelectors(Sema &SemaRef);
498 void WriteReferencedSelectorsPool(Sema &SemaRef);
499 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
501 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
502 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
503 void WriteFPPragmaOptions(const FPOptions &Opts);
504 void WriteOpenCLExtensions(Sema &SemaRef);
505 void WriteOpenCLExtensionTypes(Sema &SemaRef);
506 void WriteOpenCLExtensionDecls(Sema &SemaRef);
507 void WriteCUDAPragmas(Sema &SemaRef);
508 void WriteObjCCategories();
509 void WriteLateParsedTemplates(Sema &SemaRef);
510 void WriteOptimizePragmaOptions(Sema &SemaRef);
511 void WriteMSStructPragmaOptions(Sema &SemaRef);
512 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
513 void WritePackPragmaOptions(Sema &SemaRef);
514 void WriteModuleFileExtension(Sema &SemaRef,
515 ModuleFileExtensionWriter &Writer);
517 unsigned DeclParmVarAbbrev = 0;
518 unsigned DeclContextLexicalAbbrev = 0;
519 unsigned DeclContextVisibleLookupAbbrev = 0;
520 unsigned UpdateVisibleAbbrev = 0;
521 unsigned DeclRecordAbbrev = 0;
522 unsigned DeclTypedefAbbrev = 0;
523 unsigned DeclVarAbbrev = 0;
524 unsigned DeclFieldAbbrev = 0;
525 unsigned DeclEnumAbbrev = 0;
526 unsigned DeclObjCIvarAbbrev = 0;
527 unsigned DeclCXXMethodAbbrev = 0;
529 unsigned DeclRefExprAbbrev = 0;
530 unsigned CharacterLiteralAbbrev = 0;
531 unsigned IntegerLiteralAbbrev = 0;
532 unsigned ExprImplicitCastAbbrev = 0;
534 void WriteDeclAbbrevs();
535 void WriteDecl(ASTContext &Context, Decl *D);
537 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
538 const std::string &OutputFile,
539 Module *WritingModule);
542 /// Create a new precompiled header writer that outputs to
543 /// the given bitstream.
544 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
545 MemoryBufferCache &PCMCache,
546 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
547 bool IncludeTimestamps = true);
548 ~ASTWriter() override;
550 const LangOptions &getLangOpts() const;
552 /// Get a timestamp for output into the AST file. The actual timestamp
553 /// of the specified file may be ignored if we have been instructed to not
554 /// include timestamps in the output file.
555 time_t getTimestampForOutput(const FileEntry *E) const;
557 /// Write a precompiled header for the given semantic analysis.
559 /// \param SemaRef a reference to the semantic analysis object that processed
560 /// the AST to be written into the precompiled header.
562 /// \param WritingModule The module that we are writing. If null, we are
563 /// writing a precompiled header.
565 /// \param isysroot if non-empty, write a relocatable file whose headers
566 /// are relative to the given system root. If we're writing a module, its
567 /// build directory will be used in preference to this if both are available.
569 /// \return the module signature, which eventually will be a hash of
570 /// the module but currently is merely a random 32-bit number.
571 ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
572 Module *WritingModule, StringRef isysroot,
573 bool hasErrors = false);
576 void AddToken(const Token &Tok, RecordDataImpl &Record);
578 /// Emit a source location.
579 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
581 /// Emit a source range.
582 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
584 /// Emit a reference to an identifier.
585 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
587 /// Get the unique number used to refer to the given selector.
588 serialization::SelectorID getSelectorRef(Selector Sel);
590 /// Get the unique number used to refer to the given identifier.
591 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
593 /// Get the unique number used to refer to the given macro.
594 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
596 /// Determine the ID of an already-emitted macro.
597 serialization::MacroID getMacroID(MacroInfo *MI);
599 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
601 /// Emit a reference to a type.
602 void AddTypeRef(QualType T, RecordDataImpl &Record);
604 /// Force a type to be emitted and get its ID.
605 serialization::TypeID GetOrCreateTypeID(QualType T);
607 /// Determine the type ID of an already-emitted type.
608 serialization::TypeID getTypeID(QualType T) const;
610 /// Find the first local declaration of a given local redeclarable
612 const Decl *getFirstLocalDecl(const Decl *D);
614 /// Is this a local declaration (that is, one that will be written to
615 /// our AST file)? This is the case for declarations that are neither imported
616 /// from another AST file nor predefined.
617 bool IsLocalDecl(const Decl *D) {
618 if (D->isFromASTFile())
620 auto I = DeclIDs.find(D);
621 return (I == DeclIDs.end() ||
622 I->second >= serialization::NUM_PREDEF_DECL_IDS);
625 /// Emit a reference to a declaration.
626 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
628 /// Force a declaration to be emitted and get its ID.
629 serialization::DeclID GetDeclRef(const Decl *D);
631 /// Determine the declaration ID of an already-emitted
633 serialization::DeclID getDeclID(const Decl *D);
635 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
637 /// Add a string to the given record.
638 void AddString(StringRef Str, RecordDataImpl &Record);
640 /// Convert a path from this build process into one that is appropriate
641 /// for emission in the module file.
642 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
644 /// Add a path to the given record.
645 void AddPath(StringRef Path, RecordDataImpl &Record);
647 /// Emit the current record with the given path as a blob.
648 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
651 /// Add a version tuple to the given record
652 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
654 /// Retrieve or create a submodule ID for this module, or return 0 if
655 /// the submodule is neither local (a submodle of the currently-written module)
656 /// nor from an imported module.
657 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
659 /// Note that the identifier II occurs at the given offset
660 /// within the identifier table.
661 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
663 /// Note that the selector Sel occurs at the given offset
664 /// within the method pool/selector table.
665 void SetSelectorOffset(Selector Sel, uint32_t Offset);
667 /// Record an ID for the given switch-case statement.
668 unsigned RecordSwitchCaseID(SwitchCase *S);
670 /// Retrieve the ID for the given switch-case statement.
671 unsigned getSwitchCaseID(SwitchCase *S);
673 void ClearSwitchCaseIDs();
675 unsigned getTypeExtQualAbbrev() const {
676 return TypeExtQualAbbrev;
679 unsigned getTypeFunctionProtoAbbrev() const {
680 return TypeFunctionProtoAbbrev;
683 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
684 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
685 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
686 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
687 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
688 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
689 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
690 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
692 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
693 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
694 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
695 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
697 bool hasChain() const { return Chain; }
698 ASTReader *getChain() const { return Chain; }
701 // ASTDeserializationListener implementation
702 void ReaderInitialized(ASTReader *Reader) override;
703 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
704 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
705 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
706 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
707 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
708 MacroDefinitionRecord *MD) override;
709 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
711 // ASTMutationListener implementation.
712 void CompletedTagDefinition(const TagDecl *D) override;
713 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
714 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
715 void AddedCXXTemplateSpecialization(
716 const ClassTemplateDecl *TD,
717 const ClassTemplateSpecializationDecl *D) override;
718 void AddedCXXTemplateSpecialization(
719 const VarTemplateDecl *TD,
720 const VarTemplateSpecializationDecl *D) override;
721 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
722 const FunctionDecl *D) override;
723 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
724 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
725 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
726 const FunctionDecl *Delete,
727 Expr *ThisArg) override;
728 void CompletedImplicitDefinition(const FunctionDecl *D) override;
729 void InstantiationRequested(const ValueDecl *D) override;
730 void VariableDefinitionInstantiated(const VarDecl *D) override;
731 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
732 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
733 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
734 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
735 const ObjCInterfaceDecl *IFD) override;
736 void DeclarationMarkedUsed(const Decl *D) override;
737 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
738 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
739 const Attr *Attr) override;
740 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
741 void AddedAttributeToRecord(const Attr *Attr,
742 const RecordDecl *Record) override;
745 /// An object for streaming information to a record.
746 class ASTRecordWriter {
748 ASTWriter::RecordDataImpl *Record;
750 /// Statements that we've encountered while serializing a
751 /// declaration or type.
752 SmallVector<Stmt *, 16> StmtsToEmit;
754 /// Indices of record elements that describe offsets within the
755 /// bitcode. These will be converted to offsets relative to the current
756 /// record when emitted.
757 SmallVector<unsigned, 8> OffsetIndices;
759 /// Flush all of the statements and expressions that have
760 /// been added to the queue via AddStmt().
762 void FlushSubStmts();
764 void PrepareToEmit(uint64_t MyOffset) {
765 // Convert offsets into relative form.
766 for (unsigned I : OffsetIndices) {
767 auto &StoredOffset = (*Record)[I];
768 assert(StoredOffset < MyOffset && "invalid offset");
770 StoredOffset = MyOffset - StoredOffset;
772 OffsetIndices.clear();
776 /// Construct a ASTRecordWriter that uses the default encoding scheme.
777 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
778 : Writer(&Writer), Record(&Record) {}
780 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
782 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
783 : Writer(Parent.Writer), Record(&Record) {}
785 /// Copying an ASTRecordWriter is almost certainly a bug.
786 ASTRecordWriter(const ASTRecordWriter &) = delete;
787 ASTRecordWriter &operator=(const ASTRecordWriter &) = delete;
789 /// Extract the underlying record storage.
790 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
792 /// Minimal vector-like interface.
794 void push_back(uint64_t N) { Record->push_back(N); }
795 template<typename InputIterator>
796 void append(InputIterator begin, InputIterator end) {
797 Record->append(begin, end);
799 bool empty() const { return Record->empty(); }
800 size_t size() const { return Record->size(); }
801 uint64_t &operator[](size_t N) { return (*Record)[N]; }
804 /// Emit the record to the stream, followed by its substatements, and
805 /// return its offset.
806 // FIXME: Allow record producers to suggest Abbrevs.
807 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
808 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
809 PrepareToEmit(Offset);
810 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
815 /// Emit the record to the stream, preceded by its substatements.
816 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
818 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
819 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
820 return Writer->Stream.GetCurrentBitNo();
823 /// Add a bit offset into the record. This will be converted into an
824 /// offset relative to the current record when emitted.
825 void AddOffset(uint64_t BitOffset) {
826 OffsetIndices.push_back(Record->size());
827 Record->push_back(BitOffset);
830 /// Add the given statement or expression to the queue of
831 /// statements to emit.
833 /// This routine should be used when emitting types and declarations
834 /// that have expressions as part of their formulation. Once the
835 /// type or declaration has been written, Emit() will write
836 /// the corresponding statements just after the record.
837 void AddStmt(Stmt *S) {
838 StmtsToEmit.push_back(S);
841 /// Add a definition for the given function to the queue of statements
843 void AddFunctionDefinition(const FunctionDecl *FD);
845 /// Emit a source location.
846 void AddSourceLocation(SourceLocation Loc) {
847 return Writer->AddSourceLocation(Loc, *Record);
850 /// Emit a source range.
851 void AddSourceRange(SourceRange Range) {
852 return Writer->AddSourceRange(Range, *Record);
855 /// Emit an integral value.
856 void AddAPInt(const llvm::APInt &Value);
858 /// Emit a signed integral value.
859 void AddAPSInt(const llvm::APSInt &Value);
861 /// Emit a floating-point value.
862 void AddAPFloat(const llvm::APFloat &Value);
864 /// Emit a reference to an identifier.
865 void AddIdentifierRef(const IdentifierInfo *II) {
866 return Writer->AddIdentifierRef(II, *Record);
869 /// Emit a Selector (which is a smart pointer reference).
870 void AddSelectorRef(Selector S);
872 /// Emit a CXXTemporary.
873 void AddCXXTemporary(const CXXTemporary *Temp);
875 /// Emit a C++ base specifier.
876 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
878 /// Emit a set of C++ base specifiers.
879 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
881 /// Emit a reference to a type.
882 void AddTypeRef(QualType T) {
883 return Writer->AddTypeRef(T, *Record);
886 /// Emits a reference to a declarator info.
887 void AddTypeSourceInfo(TypeSourceInfo *TInfo);
889 /// Emits source location information for a type. Does not emit the type.
890 void AddTypeLoc(TypeLoc TL);
892 /// Emits a template argument location info.
893 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
894 const TemplateArgumentLocInfo &Arg);
896 /// Emits a template argument location.
897 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
899 /// Emits an AST template argument list info.
900 void AddASTTemplateArgumentListInfo(
901 const ASTTemplateArgumentListInfo *ASTTemplArgList);
903 /// Emit a reference to a declaration.
904 void AddDeclRef(const Decl *D) {
905 return Writer->AddDeclRef(D, *Record);
908 /// Emit a declaration name.
909 void AddDeclarationName(DeclarationName Name);
911 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
912 DeclarationName Name);
913 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
915 void AddQualifierInfo(const QualifierInfo &Info);
917 /// Emit a nested name specifier.
918 void AddNestedNameSpecifier(NestedNameSpecifier *NNS);
920 /// Emit a nested name specifier with source-location information.
921 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
923 /// Emit a template name.
924 void AddTemplateName(TemplateName Name);
926 /// Emit a template argument.
927 void AddTemplateArgument(const TemplateArgument &Arg);
929 /// Emit a template parameter list.
930 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
932 /// Emit a template argument list.
933 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
935 /// Emit a UnresolvedSet structure.
936 void AddUnresolvedSet(const ASTUnresolvedSet &Set);
938 /// Emit a CXXCtorInitializer array.
939 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits);
941 void AddCXXDefinitionData(const CXXRecordDecl *D);
944 void AddString(StringRef Str) {
945 return Writer->AddString(Str, *Record);
949 void AddPath(StringRef Path) {
950 return Writer->AddPath(Path, *Record);
953 /// Emit a version tuple.
954 void AddVersionTuple(const VersionTuple &Version) {
955 return Writer->AddVersionTuple(Version, *Record);
958 /// Emit a list of attributes.
959 void AddAttributes(ArrayRef<const Attr*> Attrs);
962 /// AST and semantic-analysis consumer that generates a
963 /// precompiled header from the parsed source code.
964 class PCHGenerator : public SemaConsumer {
965 const Preprocessor &PP;
966 std::string OutputFile;
967 std::string isysroot;
969 std::shared_ptr<PCHBuffer> Buffer;
970 llvm::BitstreamWriter Stream;
972 bool AllowASTWithErrors;
975 ASTWriter &getWriter() { return Writer; }
976 const ASTWriter &getWriter() const { return Writer; }
977 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
980 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
981 std::shared_ptr<PCHBuffer> Buffer,
982 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
983 bool AllowASTWithErrors = false, bool IncludeTimestamps = true);
984 ~PCHGenerator() override;
986 void InitializeSema(Sema &S) override { SemaPtr = &S; }
987 void HandleTranslationUnit(ASTContext &Ctx) override;
988 ASTMutationListener *GetASTMutationListener() override;
989 ASTDeserializationListener *GetASTDeserializationListener() override;
990 bool hasEmittedPCH() const { return Buffer->IsComplete; }
995 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H