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;
98 /// \brief Writes an AST file containing the contents of a translation unit.
100 /// The ASTWriter class produces a bitstream containing the serialized
101 /// representation of a given abstract syntax tree and its supporting
102 /// data structures. This bitstream can be de-serialized via an
103 /// instance of the ASTReader class.
104 class ASTWriter : public ASTDeserializationListener,
105 public ASTMutationListener {
107 friend class ASTDeclWriter;
108 friend class ASTRecordWriter;
109 friend class ASTStmtWriter;
110 friend class ASTTypeWriter;
112 using RecordData = SmallVector<uint64_t, 64>;
113 using RecordDataImpl = SmallVectorImpl<uint64_t>;
114 using RecordDataRef = ArrayRef<uint64_t>;
117 /// \brief Map that provides the ID numbers of each type within the
118 /// output stream, plus those deserialized from a chained PCH.
120 /// The ID numbers of types are consecutive (in order of discovery)
121 /// and start at 1. 0 is reserved for NULL. When types are actually
122 /// stored in the stream, the ID number is shifted by 2 bits to
123 /// allow for the const/volatile qualifiers.
125 /// Keys in the map never have const/volatile qualifiers.
126 using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx,
127 serialization::UnsafeQualTypeDenseMapInfo>;
129 /// \brief The bitstream writer used to emit this precompiled header.
130 llvm::BitstreamWriter &Stream;
132 /// The buffer associated with the bitstream.
133 const SmallVectorImpl<char> &Buffer;
135 /// \brief The PCM manager which manages memory buffers for pcm files.
136 MemoryBufferCache &PCMCache;
138 /// \brief The ASTContext we're writing.
139 ASTContext *Context = nullptr;
141 /// \brief The preprocessor we're writing.
142 Preprocessor *PP = nullptr;
144 /// \brief The reader of existing AST files, if we're chaining.
145 ASTReader *Chain = nullptr;
147 /// \brief The module we're currently writing, if any.
148 Module *WritingModule = nullptr;
150 /// \brief The base directory for any relative paths we emit.
151 std::string BaseDirectory;
153 /// \brief Indicates whether timestamps should be written to the produced
154 /// module file. This is the case for files implicitly written to the
155 /// module cache, where we need the timestamps to determine if the module
156 /// file is up to date, but not otherwise.
157 bool IncludeTimestamps;
159 /// \brief Indicates when the AST writing is actively performing
160 /// serialization, rather than just queueing updates.
161 bool WritingAST = false;
163 /// \brief Indicates that we are done serializing the collection of decls
164 /// and types to emit.
165 bool DoneWritingDeclsAndTypes = false;
167 /// \brief Indicates that the AST contained compiler errors.
168 bool ASTHasCompilerErrors = false;
170 /// \brief Mapping from input file entries to the index into the
171 /// offset table where information about that input file is stored.
172 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
174 /// \brief Stores a declaration or a type to be written to the AST file.
177 DeclOrType(Decl *D) : Stored(D), IsType(false) {}
178 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {}
180 bool isType() const { return IsType; }
181 bool isDecl() const { return !IsType; }
183 QualType getType() const {
184 assert(isType() && "Not a type!");
185 return QualType::getFromOpaquePtr(Stored);
188 Decl *getDecl() const {
189 assert(isDecl() && "Not a decl!");
190 return static_cast<Decl *>(Stored);
198 /// \brief The declarations and types to emit.
199 std::queue<DeclOrType> DeclTypesToEmit;
201 /// \brief The first ID number we can use for our own declarations.
202 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
204 /// \brief The decl ID that will be assigned to the next new decl.
205 serialization::DeclID NextDeclID = FirstDeclID;
207 /// \brief Map that provides the ID numbers of each declaration within
208 /// the output stream, as well as those deserialized from a chained PCH.
210 /// The ID numbers of declarations are consecutive (in order of
211 /// discovery) and start at 2. 1 is reserved for the translation
212 /// unit, while 0 is reserved for NULL.
213 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
215 /// \brief Offset of each declaration in the bitstream, indexed by
216 /// the declaration's ID.
217 std::vector<serialization::DeclOffset> DeclOffsets;
219 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
221 SmallVector<std::pair<unsigned, serialization::DeclID>, 64>;
222 struct DeclIDInFileInfo {
223 LocDeclIDsTy DeclIDs;
225 /// \brief Set when the DeclIDs vectors from all files are joined, this
226 /// indicates the index that this particular vector has in the global one.
227 unsigned FirstDeclIndex;
229 using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>;
231 /// \brief Map from file SLocEntries to info about the file-level declarations
232 /// that it contains.
233 FileDeclIDsTy FileDeclIDs;
235 void associateDeclWithFile(const Decl *D, serialization::DeclID);
237 /// \brief The first ID number we can use for our own types.
238 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
240 /// \brief The type ID that will be assigned to the next new type.
241 serialization::TypeID NextTypeID = FirstTypeID;
243 /// \brief Map that provides the ID numbers of each type within the
244 /// output stream, plus those deserialized from a chained PCH.
246 /// The ID numbers of types are consecutive (in order of discovery)
247 /// and start at 1. 0 is reserved for NULL. When types are actually
248 /// stored in the stream, the ID number is shifted by 2 bits to
249 /// allow for the const/volatile qualifiers.
251 /// Keys in the map never have const/volatile qualifiers.
254 /// \brief Offset of each type in the bitstream, indexed by
256 std::vector<uint32_t> TypeOffsets;
258 /// \brief The first ID number we can use for our own identifiers.
259 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
261 /// \brief The identifier ID that will be assigned to the next new identifier.
262 serialization::IdentID NextIdentID = FirstIdentID;
264 /// \brief Map that provides the ID numbers of each identifier in
265 /// the output stream.
267 /// The ID numbers for identifiers are consecutive (in order of
268 /// discovery), starting at 1. An ID of zero refers to a NULL
270 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
272 /// \brief The first ID number we can use for our own macros.
273 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
275 /// \brief The identifier ID that will be assigned to the next new identifier.
276 serialization::MacroID NextMacroID = FirstMacroID;
278 /// \brief Map that provides the ID numbers of each macro.
279 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
281 struct MacroInfoToEmitData {
282 const IdentifierInfo *Name;
284 serialization::MacroID ID;
287 /// \brief The macro infos to emit.
288 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
290 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
292 /// @name FlushStmt Caches
295 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
296 llvm::DenseSet<Stmt *> ParentStmts;
298 /// \brief Offsets of sub-stmts already serialized. The offset points
299 /// just after the stmt record.
300 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
304 /// \brief Offsets of each of the identifier IDs into the identifier
306 std::vector<uint32_t> IdentifierOffsets;
308 /// \brief The first ID number we can use for our own submodules.
309 serialization::SubmoduleID FirstSubmoduleID =
310 serialization::NUM_PREDEF_SUBMODULE_IDS;
312 /// \brief The submodule ID that will be assigned to the next new submodule.
313 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
315 /// \brief The first ID number we can use for our own selectors.
316 serialization::SelectorID FirstSelectorID =
317 serialization::NUM_PREDEF_SELECTOR_IDS;
319 /// \brief The selector ID that will be assigned to the next new selector.
320 serialization::SelectorID NextSelectorID = FirstSelectorID;
322 /// \brief Map that provides the ID numbers of each Selector.
323 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
325 /// \brief Offset of each selector within the method pool/selector
326 /// table, indexed by the Selector ID (-1).
327 std::vector<uint32_t> SelectorOffsets;
329 /// \brief Mapping from macro definitions (as they occur in the preprocessing
330 /// record) to the macro IDs.
331 llvm::DenseMap<const MacroDefinitionRecord *,
332 serialization::PreprocessedEntityID> MacroDefinitions;
334 /// \brief Cache of indices of anonymous declarations within their lexical
336 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
338 /// An update to a Decl.
340 /// A DeclUpdateKind.
348 const Attr *Attribute;
352 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
353 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
354 DeclUpdate(unsigned Kind, QualType Type)
355 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
356 DeclUpdate(unsigned Kind, SourceLocation Loc)
357 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
358 DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {}
359 DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {}
360 DeclUpdate(unsigned Kind, const Attr *Attribute)
361 : Kind(Kind), Attribute(Attribute) {}
363 unsigned getKind() const { return Kind; }
364 const Decl *getDecl() const { return Dcl; }
365 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
367 SourceLocation getLoc() const {
368 return SourceLocation::getFromRawEncoding(Loc);
371 unsigned getNumber() const { return Val; }
372 Module *getModule() const { return Mod; }
373 const Attr *getAttr() const { return Attribute; }
376 using UpdateRecord = SmallVector<DeclUpdate, 1>;
377 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
379 /// \brief Mapping from declarations that came from a chained PCH to the
380 /// record containing modifications to them.
381 DeclUpdateMap DeclUpdates;
383 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
385 /// \brief Map of first declarations from a chained PCH that point to the
386 /// most recent declarations in another PCH.
387 FirstLatestDeclMap FirstLatestDecls;
389 /// \brief Declarations encountered that might be external
392 /// We keep track of external definitions and other 'interesting' declarations
393 /// as we are emitting declarations to the AST file. The AST file contains a
394 /// separate record for these declarations, which are provided to the AST
395 /// consumer by the AST reader. This is behavior is required to properly cope with,
396 /// e.g., tentative variable definitions that occur within
397 /// headers. The declarations themselves are stored as declaration
398 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
400 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
401 SmallVector<uint64_t, 16> ModularCodegenDecls;
403 /// \brief DeclContexts that have received extensions since their serialized
406 /// For namespaces, when we're chaining and encountering a namespace, we check
407 /// if its primary namespace comes from the chain. If it does, we add the
408 /// primary to this set, so that we can write out lexical content updates for
410 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
412 /// \brief Keeps track of declarations that we must emit, even though we're
413 /// not guaranteed to be able to find them by walking the AST starting at the
414 /// translation unit.
415 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
417 /// \brief The set of Objective-C class that have categories we
418 /// should serialize.
419 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
421 /// \brief The set of declarations that may have redeclaration chains that
422 /// need to be serialized.
423 llvm::SmallVector<const Decl *, 16> Redeclarations;
425 /// \brief A cache of the first local declaration for "interesting"
426 /// redeclaration chains.
427 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
429 /// \brief Mapping from SwitchCase statements to IDs.
430 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
432 /// \brief The number of statements written to the AST file.
433 unsigned NumStatements = 0;
435 /// \brief The number of macros written to the AST file.
436 unsigned NumMacros = 0;
438 /// \brief The number of lexical declcontexts written to the AST
440 unsigned NumLexicalDeclContexts = 0;
442 /// \brief The number of visible declcontexts written to the AST
444 unsigned NumVisibleDeclContexts = 0;
446 /// \brief A mapping from each known submodule to its ID number, which will
447 /// be a positive integer.
448 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
450 /// \brief A list of the module file extension writers.
451 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
452 ModuleFileExtensionWriters;
454 /// \brief Retrieve or create a submodule ID for this module.
455 unsigned getSubmoduleID(Module *Mod);
457 /// \brief Write the given subexpression to the bitstream.
458 void WriteSubStmt(Stmt *S);
460 void WriteBlockInfoBlock();
461 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
462 StringRef isysroot, const std::string &OutputFile);
464 /// Write out the signature and diagnostic options, and return the signature.
465 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
466 ASTContext &Context);
468 /// Calculate hash of the pcm content.
469 static ASTFileSignature createSignature(StringRef Bytes);
471 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
473 void WriteSourceManagerBlock(SourceManager &SourceMgr,
474 const Preprocessor &PP);
475 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
476 void WriteHeaderSearch(const HeaderSearch &HS);
477 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
478 void WriteSubmodules(Module *WritingModule);
480 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
483 unsigned TypeExtQualAbbrev = 0;
484 unsigned TypeFunctionProtoAbbrev = 0;
485 void WriteTypeAbbrevs();
486 void WriteType(QualType T);
488 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
489 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
491 void GenerateNameLookupTable(const DeclContext *DC,
492 llvm::SmallVectorImpl<char> &LookupTable);
493 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
494 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
495 void WriteTypeDeclOffsets();
496 void WriteFileDeclIDsMap();
497 void WriteComments();
498 void WriteSelectors(Sema &SemaRef);
499 void WriteReferencedSelectorsPool(Sema &SemaRef);
500 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
502 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
503 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
504 void WriteFPPragmaOptions(const FPOptions &Opts);
505 void WriteOpenCLExtensions(Sema &SemaRef);
506 void WriteOpenCLExtensionTypes(Sema &SemaRef);
507 void WriteOpenCLExtensionDecls(Sema &SemaRef);
508 void WriteCUDAPragmas(Sema &SemaRef);
509 void WriteObjCCategories();
510 void WriteLateParsedTemplates(Sema &SemaRef);
511 void WriteOptimizePragmaOptions(Sema &SemaRef);
512 void WriteMSStructPragmaOptions(Sema &SemaRef);
513 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
514 void WritePackPragmaOptions(Sema &SemaRef);
515 void WriteModuleFileExtension(Sema &SemaRef,
516 ModuleFileExtensionWriter &Writer);
518 unsigned DeclParmVarAbbrev = 0;
519 unsigned DeclContextLexicalAbbrev = 0;
520 unsigned DeclContextVisibleLookupAbbrev = 0;
521 unsigned UpdateVisibleAbbrev = 0;
522 unsigned DeclRecordAbbrev = 0;
523 unsigned DeclTypedefAbbrev = 0;
524 unsigned DeclVarAbbrev = 0;
525 unsigned DeclFieldAbbrev = 0;
526 unsigned DeclEnumAbbrev = 0;
527 unsigned DeclObjCIvarAbbrev = 0;
528 unsigned DeclCXXMethodAbbrev = 0;
530 unsigned DeclRefExprAbbrev = 0;
531 unsigned CharacterLiteralAbbrev = 0;
532 unsigned IntegerLiteralAbbrev = 0;
533 unsigned ExprImplicitCastAbbrev = 0;
535 void WriteDeclAbbrevs();
536 void WriteDecl(ASTContext &Context, Decl *D);
538 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
539 const std::string &OutputFile,
540 Module *WritingModule);
543 /// \brief Create a new precompiled header writer that outputs to
544 /// the given bitstream.
545 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
546 MemoryBufferCache &PCMCache,
547 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
548 bool IncludeTimestamps = true);
549 ~ASTWriter() override;
551 const LangOptions &getLangOpts() const;
553 /// \brief Get a timestamp for output into the AST file. The actual timestamp
554 /// of the specified file may be ignored if we have been instructed to not
555 /// include timestamps in the output file.
556 time_t getTimestampForOutput(const FileEntry *E) const;
558 /// \brief Write a precompiled header for the given semantic analysis.
560 /// \param SemaRef a reference to the semantic analysis object that processed
561 /// the AST to be written into the precompiled header.
563 /// \param WritingModule The module that we are writing. If null, we are
564 /// writing a precompiled header.
566 /// \param isysroot if non-empty, write a relocatable file whose headers
567 /// are relative to the given system root. If we're writing a module, its
568 /// build directory will be used in preference to this if both are available.
570 /// \return the module signature, which eventually will be a hash of
571 /// the module but currently is merely a random 32-bit number.
572 ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile,
573 Module *WritingModule, StringRef isysroot,
574 bool hasErrors = false);
576 /// \brief Emit a token.
577 void AddToken(const Token &Tok, RecordDataImpl &Record);
579 /// \brief Emit a source location.
580 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
582 /// \brief Emit a source range.
583 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
585 /// \brief Emit a reference to an identifier.
586 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
588 /// \brief Get the unique number used to refer to the given selector.
589 serialization::SelectorID getSelectorRef(Selector Sel);
591 /// \brief Get the unique number used to refer to the given identifier.
592 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
594 /// \brief Get the unique number used to refer to the given macro.
595 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
597 /// \brief Determine the ID of an already-emitted macro.
598 serialization::MacroID getMacroID(MacroInfo *MI);
600 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
602 /// \brief Emit a reference to a type.
603 void AddTypeRef(QualType T, RecordDataImpl &Record);
605 /// \brief Force a type to be emitted and get its ID.
606 serialization::TypeID GetOrCreateTypeID(QualType T);
608 /// \brief Determine the type ID of an already-emitted type.
609 serialization::TypeID getTypeID(QualType T) const;
611 /// \brief Find the first local declaration of a given local redeclarable
613 const Decl *getFirstLocalDecl(const Decl *D);
615 /// \brief Is this a local declaration (that is, one that will be written to
616 /// our AST file)? This is the case for declarations that are neither imported
617 /// from another AST file nor predefined.
618 bool IsLocalDecl(const Decl *D) {
619 if (D->isFromASTFile())
621 auto I = DeclIDs.find(D);
622 return (I == DeclIDs.end() ||
623 I->second >= serialization::NUM_PREDEF_DECL_IDS);
626 /// \brief Emit a reference to a declaration.
627 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
629 /// \brief Force a declaration to be emitted and get its ID.
630 serialization::DeclID GetDeclRef(const Decl *D);
632 /// \brief Determine the declaration ID of an already-emitted
634 serialization::DeclID getDeclID(const Decl *D);
636 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
638 /// \brief Add a string to the given record.
639 void AddString(StringRef Str, RecordDataImpl &Record);
641 /// \brief Convert a path from this build process into one that is appropriate
642 /// for emission in the module file.
643 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
645 /// \brief Add a path to the given record.
646 void AddPath(StringRef Path, RecordDataImpl &Record);
648 /// \brief Emit the current record with the given path as a blob.
649 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
652 /// \brief Add a version tuple to the given record
653 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
655 /// \brief Retrieve or create a submodule ID for this module, or return 0 if
656 /// the submodule is neither local (a submodle of the currently-written module)
657 /// nor from an imported module.
658 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
660 /// \brief Note that the identifier II occurs at the given offset
661 /// within the identifier table.
662 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
664 /// \brief Note that the selector Sel occurs at the given offset
665 /// within the method pool/selector table.
666 void SetSelectorOffset(Selector Sel, uint32_t Offset);
668 /// \brief Record an ID for the given switch-case statement.
669 unsigned RecordSwitchCaseID(SwitchCase *S);
671 /// \brief Retrieve the ID for the given switch-case statement.
672 unsigned getSwitchCaseID(SwitchCase *S);
674 void ClearSwitchCaseIDs();
676 unsigned getTypeExtQualAbbrev() const {
677 return TypeExtQualAbbrev;
680 unsigned getTypeFunctionProtoAbbrev() const {
681 return TypeFunctionProtoAbbrev;
684 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
685 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
686 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
687 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
688 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
689 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
690 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
691 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
693 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
694 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
695 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
696 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
698 bool hasChain() const { return Chain; }
699 ASTReader *getChain() const { return Chain; }
702 // ASTDeserializationListener implementation
703 void ReaderInitialized(ASTReader *Reader) override;
704 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
705 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
706 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
707 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
708 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
709 MacroDefinitionRecord *MD) override;
710 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
712 // ASTMutationListener implementation.
713 void CompletedTagDefinition(const TagDecl *D) override;
714 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
715 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
716 void AddedCXXTemplateSpecialization(
717 const ClassTemplateDecl *TD,
718 const ClassTemplateSpecializationDecl *D) override;
719 void AddedCXXTemplateSpecialization(
720 const VarTemplateDecl *TD,
721 const VarTemplateSpecializationDecl *D) override;
722 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
723 const FunctionDecl *D) override;
724 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
725 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
726 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
727 const FunctionDecl *Delete,
728 Expr *ThisArg) override;
729 void CompletedImplicitDefinition(const FunctionDecl *D) override;
730 void InstantiationRequested(const ValueDecl *D) override;
731 void VariableDefinitionInstantiated(const VarDecl *D) override;
732 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
733 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
734 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
735 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
736 const ObjCInterfaceDecl *IFD) override;
737 void DeclarationMarkedUsed(const Decl *D) override;
738 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
739 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
740 const Attr *Attr) override;
741 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
742 void AddedAttributeToRecord(const Attr *Attr,
743 const RecordDecl *Record) override;
746 /// \brief An object for streaming information to a record.
747 class ASTRecordWriter {
749 ASTWriter::RecordDataImpl *Record;
751 /// \brief Statements that we've encountered while serializing a
752 /// declaration or type.
753 SmallVector<Stmt *, 16> StmtsToEmit;
755 /// \brief Indices of record elements that describe offsets within the
756 /// bitcode. These will be converted to offsets relative to the current
757 /// record when emitted.
758 SmallVector<unsigned, 8> OffsetIndices;
760 /// \brief Flush all of the statements and expressions that have
761 /// been added to the queue via AddStmt().
763 void FlushSubStmts();
765 void PrepareToEmit(uint64_t MyOffset) {
766 // Convert offsets into relative form.
767 for (unsigned I : OffsetIndices) {
768 auto &StoredOffset = (*Record)[I];
769 assert(StoredOffset < MyOffset && "invalid offset");
771 StoredOffset = MyOffset - StoredOffset;
773 OffsetIndices.clear();
777 /// Construct a ASTRecordWriter that uses the default encoding scheme.
778 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
779 : Writer(&Writer), Record(&Record) {}
781 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
783 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
784 : Writer(Parent.Writer), Record(&Record) {}
786 /// Copying an ASTRecordWriter is almost certainly a bug.
787 ASTRecordWriter(const ASTRecordWriter &) = delete;
788 ASTRecordWriter &operator=(const ASTRecordWriter &) = delete;
790 /// \brief Extract the underlying record storage.
791 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
793 /// \brief Minimal vector-like interface.
795 void push_back(uint64_t N) { Record->push_back(N); }
796 template<typename InputIterator>
797 void append(InputIterator begin, InputIterator end) {
798 Record->append(begin, end);
800 bool empty() const { return Record->empty(); }
801 size_t size() const { return Record->size(); }
802 uint64_t &operator[](size_t N) { return (*Record)[N]; }
805 /// \brief Emit the record to the stream, followed by its substatements, and
806 /// return its offset.
807 // FIXME: Allow record producers to suggest Abbrevs.
808 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
809 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
810 PrepareToEmit(Offset);
811 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
816 /// \brief Emit the record to the stream, preceded by its substatements.
817 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
819 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
820 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
821 return Writer->Stream.GetCurrentBitNo();
824 /// \brief Add a bit offset into the record. This will be converted into an
825 /// offset relative to the current record when emitted.
826 void AddOffset(uint64_t BitOffset) {
827 OffsetIndices.push_back(Record->size());
828 Record->push_back(BitOffset);
831 /// \brief Add the given statement or expression to the queue of
832 /// statements to emit.
834 /// This routine should be used when emitting types and declarations
835 /// that have expressions as part of their formulation. Once the
836 /// type or declaration has been written, Emit() will write
837 /// the corresponding statements just after the record.
838 void AddStmt(Stmt *S) {
839 StmtsToEmit.push_back(S);
842 /// \brief Add a definition for the given function to the queue of statements
844 void AddFunctionDefinition(const FunctionDecl *FD);
846 /// \brief Emit a source location.
847 void AddSourceLocation(SourceLocation Loc) {
848 return Writer->AddSourceLocation(Loc, *Record);
851 /// \brief Emit a source range.
852 void AddSourceRange(SourceRange Range) {
853 return Writer->AddSourceRange(Range, *Record);
856 /// \brief Emit an integral value.
857 void AddAPInt(const llvm::APInt &Value);
859 /// \brief Emit a signed integral value.
860 void AddAPSInt(const llvm::APSInt &Value);
862 /// \brief Emit a floating-point value.
863 void AddAPFloat(const llvm::APFloat &Value);
865 /// \brief Emit a reference to an identifier.
866 void AddIdentifierRef(const IdentifierInfo *II) {
867 return Writer->AddIdentifierRef(II, *Record);
870 /// \brief Emit a Selector (which is a smart pointer reference).
871 void AddSelectorRef(Selector S);
873 /// \brief Emit a CXXTemporary.
874 void AddCXXTemporary(const CXXTemporary *Temp);
876 /// \brief Emit a C++ base specifier.
877 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
879 /// \brief Emit a set of C++ base specifiers.
880 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
882 /// \brief Emit a reference to a type.
883 void AddTypeRef(QualType T) {
884 return Writer->AddTypeRef(T, *Record);
887 /// \brief Emits a reference to a declarator info.
888 void AddTypeSourceInfo(TypeSourceInfo *TInfo);
890 /// \brief Emits a type with source-location information.
891 void AddTypeLoc(TypeLoc TL);
893 /// \brief Emits a template argument location info.
894 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
895 const TemplateArgumentLocInfo &Arg);
897 /// \brief Emits a template argument location.
898 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
900 /// \brief Emits an AST template argument list info.
901 void AddASTTemplateArgumentListInfo(
902 const ASTTemplateArgumentListInfo *ASTTemplArgList);
904 /// \brief Emit a reference to a declaration.
905 void AddDeclRef(const Decl *D) {
906 return Writer->AddDeclRef(D, *Record);
909 /// \brief Emit a declaration name.
910 void AddDeclarationName(DeclarationName Name);
912 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
913 DeclarationName Name);
914 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
916 void AddQualifierInfo(const QualifierInfo &Info);
918 /// \brief Emit a nested name specifier.
919 void AddNestedNameSpecifier(NestedNameSpecifier *NNS);
921 /// \brief Emit a nested name specifier with source-location information.
922 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
924 /// \brief Emit a template name.
925 void AddTemplateName(TemplateName Name);
927 /// \brief Emit a template argument.
928 void AddTemplateArgument(const TemplateArgument &Arg);
930 /// \brief Emit a template parameter list.
931 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
933 /// \brief Emit a template argument list.
934 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
936 /// \brief Emit a UnresolvedSet structure.
937 void AddUnresolvedSet(const ASTUnresolvedSet &Set);
939 /// \brief Emit a CXXCtorInitializer array.
940 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits);
942 void AddCXXDefinitionData(const CXXRecordDecl *D);
944 /// \brief Emit a string.
945 void AddString(StringRef Str) {
946 return Writer->AddString(Str, *Record);
949 /// \brief Emit a path.
950 void AddPath(StringRef Path) {
951 return Writer->AddPath(Path, *Record);
954 /// \brief Emit a version tuple.
955 void AddVersionTuple(const VersionTuple &Version) {
956 return Writer->AddVersionTuple(Version, *Record);
959 /// \brief Emit a list of attributes.
960 void AddAttributes(ArrayRef<const Attr*> Attrs);
963 /// \brief AST and semantic-analysis consumer that generates a
964 /// precompiled header from the parsed source code.
965 class PCHGenerator : public SemaConsumer {
966 const Preprocessor &PP;
967 std::string OutputFile;
968 std::string isysroot;
970 std::shared_ptr<PCHBuffer> Buffer;
971 llvm::BitstreamWriter Stream;
973 bool AllowASTWithErrors;
976 ASTWriter &getWriter() { return Writer; }
977 const ASTWriter &getWriter() const { return Writer; }
978 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
981 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
982 std::shared_ptr<PCHBuffer> Buffer,
983 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
984 bool AllowASTWithErrors = false, bool IncludeTimestamps = true);
985 ~PCHGenerator() override;
987 void InitializeSema(Sema &S) override { SemaPtr = &S; }
988 void HandleTranslationUnit(ASTContext &Ctx) override;
989 ASTMutationListener *GetASTMutationListener() override;
990 ASTDeserializationListener *GetASTDeserializationListener() override;
991 bool hasEmittedPCH() const { return Buffer->IsComplete; }
996 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H