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/DeclarationName.h"
20 #include "clang/Frontend/PCHContainerOperations.h"
21 #include "clang/AST/TemplateBase.h"
22 #include "clang/Sema/SemaConsumer.h"
23 #include "clang/Serialization/ASTBitCodes.h"
24 #include "clang/Serialization/ASTDeserializationListener.h"
25 #include "llvm/ADT/DenseMap.h"
26 #include "llvm/ADT/DenseSet.h"
27 #include "llvm/ADT/MapVector.h"
28 #include "llvm/ADT/SetVector.h"
29 #include "llvm/ADT/SmallPtrSet.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/Bitcode/BitstreamWriter.h"
39 class BitstreamWriter;
46 class NestedNameSpecifier;
47 class CXXBaseSpecifier;
48 class CXXCtorInitializer;
52 class HeaderSearchOptions;
53 class IdentifierResolver;
54 class MacroDefinitionRecord;
57 class OpaqueValueExpr;
61 class ModuleFileExtension;
62 class ModuleFileExtensionWriter;
63 class PreprocessedEntity;
64 class PreprocessingRecord;
69 struct StoredDeclsList;
74 class ASTUnresolvedSet;
76 namespace SrcMgr { class SLocEntry; }
78 /// \brief Writes an AST file containing the contents of a translation unit.
80 /// The ASTWriter class produces a bitstream containing the serialized
81 /// representation of a given abstract syntax tree and its supporting
82 /// data structures. This bitstream can be de-serialized via an
83 /// instance of the ASTReader class.
84 class ASTWriter : public ASTDeserializationListener,
85 public ASTMutationListener {
87 typedef SmallVector<uint64_t, 64> RecordData;
88 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
89 typedef ArrayRef<uint64_t> RecordDataRef;
91 friend class ASTDeclWriter;
92 friend class ASTStmtWriter;
93 friend class ASTTypeWriter;
94 friend class ASTRecordWriter;
96 /// \brief Map that provides the ID numbers of each type within the
97 /// output stream, plus those deserialized from a chained PCH.
99 /// The ID numbers of types are consecutive (in order of discovery)
100 /// and start at 1. 0 is reserved for NULL. When types are actually
101 /// stored in the stream, the ID number is shifted by 2 bits to
102 /// allow for the const/volatile qualifiers.
104 /// Keys in the map never have const/volatile qualifiers.
105 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
106 serialization::UnsafeQualTypeDenseMapInfo>
109 /// \brief The bitstream writer used to emit this precompiled header.
110 llvm::BitstreamWriter &Stream;
112 /// \brief The ASTContext we're writing.
115 /// \brief The preprocessor we're writing.
118 /// \brief The reader of existing AST files, if we're chaining.
121 /// \brief The module we're currently writing, if any.
122 Module *WritingModule;
124 /// \brief The base directory for any relative paths we emit.
125 std::string BaseDirectory;
127 /// \brief Indicates whether timestamps should be written to the produced
128 /// module file. This is the case for files implicitly written to the
129 /// module cache, where we need the timestamps to determine if the module
130 /// file is up to date, but not otherwise.
131 bool IncludeTimestamps;
133 /// \brief Indicates when the AST writing is actively performing
134 /// serialization, rather than just queueing updates.
137 /// \brief Indicates that we are done serializing the collection of decls
138 /// and types to emit.
139 bool DoneWritingDeclsAndTypes;
141 /// \brief Indicates that the AST contained compiler errors.
142 bool ASTHasCompilerErrors;
144 /// \brief Mapping from input file entries to the index into the
145 /// offset table where information about that input file is stored.
146 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
148 /// \brief Stores a declaration or a type to be written to the AST file.
151 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
152 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
154 bool isType() const { return IsType; }
155 bool isDecl() const { return !IsType; }
157 QualType getType() const {
158 assert(isType() && "Not a type!");
159 return QualType::getFromOpaquePtr(Stored);
162 Decl *getDecl() const {
163 assert(isDecl() && "Not a decl!");
164 return static_cast<Decl *>(Stored);
172 /// \brief The declarations and types to emit.
173 std::queue<DeclOrType> DeclTypesToEmit;
175 /// \brief The first ID number we can use for our own declarations.
176 serialization::DeclID FirstDeclID;
178 /// \brief The decl ID that will be assigned to the next new decl.
179 serialization::DeclID NextDeclID;
181 /// \brief Map that provides the ID numbers of each declaration within
182 /// the output stream, as well as those deserialized from a chained PCH.
184 /// The ID numbers of declarations are consecutive (in order of
185 /// discovery) and start at 2. 1 is reserved for the translation
186 /// unit, while 0 is reserved for NULL.
187 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
189 /// \brief Offset of each declaration in the bitstream, indexed by
190 /// the declaration's ID.
191 std::vector<serialization::DeclOffset> DeclOffsets;
193 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
194 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
196 struct DeclIDInFileInfo {
197 LocDeclIDsTy DeclIDs;
198 /// \brief Set when the DeclIDs vectors from all files are joined, this
199 /// indicates the index that this particular vector has in the global one.
200 unsigned FirstDeclIndex;
202 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
204 /// \brief Map from file SLocEntries to info about the file-level declarations
205 /// that it contains.
206 FileDeclIDsTy FileDeclIDs;
208 void associateDeclWithFile(const Decl *D, serialization::DeclID);
210 /// \brief The first ID number we can use for our own types.
211 serialization::TypeID FirstTypeID;
213 /// \brief The type ID that will be assigned to the next new type.
214 serialization::TypeID NextTypeID;
216 /// \brief Map that provides the ID numbers of each type within the
217 /// output stream, plus those deserialized from a chained PCH.
219 /// The ID numbers of types are consecutive (in order of discovery)
220 /// and start at 1. 0 is reserved for NULL. When types are actually
221 /// stored in the stream, the ID number is shifted by 2 bits to
222 /// allow for the const/volatile qualifiers.
224 /// Keys in the map never have const/volatile qualifiers.
227 /// \brief Offset of each type in the bitstream, indexed by
229 std::vector<uint32_t> TypeOffsets;
231 /// \brief The first ID number we can use for our own identifiers.
232 serialization::IdentID FirstIdentID;
234 /// \brief The identifier ID that will be assigned to the next new identifier.
235 serialization::IdentID NextIdentID;
237 /// \brief Map that provides the ID numbers of each identifier in
238 /// the output stream.
240 /// The ID numbers for identifiers are consecutive (in order of
241 /// discovery), starting at 1. An ID of zero refers to a NULL
243 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
245 /// \brief The first ID number we can use for our own macros.
246 serialization::MacroID FirstMacroID;
248 /// \brief The identifier ID that will be assigned to the next new identifier.
249 serialization::MacroID NextMacroID;
251 /// \brief Map that provides the ID numbers of each macro.
252 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
254 struct MacroInfoToEmitData {
255 const IdentifierInfo *Name;
257 serialization::MacroID ID;
259 /// \brief The macro infos to emit.
260 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
262 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
264 /// @name FlushStmt Caches
267 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
268 llvm::DenseSet<Stmt *> ParentStmts;
270 /// \brief Offsets of sub-stmts already serialized. The offset points
271 /// just after the stmt record.
272 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
276 /// \brief Offsets of each of the identifier IDs into the identifier
278 std::vector<uint32_t> IdentifierOffsets;
280 /// \brief The first ID number we can use for our own submodules.
281 serialization::SubmoduleID FirstSubmoduleID;
283 /// \brief The submodule ID that will be assigned to the next new submodule.
284 serialization::SubmoduleID NextSubmoduleID;
286 /// \brief The first ID number we can use for our own selectors.
287 serialization::SelectorID FirstSelectorID;
289 /// \brief The selector ID that will be assigned to the next new selector.
290 serialization::SelectorID NextSelectorID;
292 /// \brief Map that provides the ID numbers of each Selector.
293 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
295 /// \brief Offset of each selector within the method pool/selector
296 /// table, indexed by the Selector ID (-1).
297 std::vector<uint32_t> SelectorOffsets;
299 /// \brief Mapping from macro definitions (as they occur in the preprocessing
300 /// record) to the macro IDs.
301 llvm::DenseMap<const MacroDefinitionRecord *,
302 serialization::PreprocessedEntityID> MacroDefinitions;
304 /// \brief Cache of indices of anonymous declarations within their lexical
306 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
308 /// An update to a Decl.
310 /// A DeclUpdateKind.
318 const Attr *Attribute;
322 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
323 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
324 DeclUpdate(unsigned Kind, QualType Type)
325 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
326 DeclUpdate(unsigned Kind, SourceLocation Loc)
327 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
328 DeclUpdate(unsigned Kind, unsigned Val)
329 : Kind(Kind), Val(Val) {}
330 DeclUpdate(unsigned Kind, Module *M)
331 : Kind(Kind), Mod(M) {}
332 DeclUpdate(unsigned Kind, const Attr *Attribute)
333 : Kind(Kind), Attribute(Attribute) {}
335 unsigned getKind() const { return Kind; }
336 const Decl *getDecl() const { return Dcl; }
337 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
338 SourceLocation getLoc() const {
339 return SourceLocation::getFromRawEncoding(Loc);
341 unsigned getNumber() const { return Val; }
342 Module *getModule() const { return Mod; }
343 const Attr *getAttr() const { return Attribute; }
346 typedef SmallVector<DeclUpdate, 1> UpdateRecord;
347 typedef llvm::MapVector<const Decl *, UpdateRecord> DeclUpdateMap;
348 /// \brief Mapping from declarations that came from a chained PCH to the
349 /// record containing modifications to them.
350 DeclUpdateMap DeclUpdates;
352 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
353 /// \brief Map of first declarations from a chained PCH that point to the
354 /// most recent declarations in another PCH.
355 FirstLatestDeclMap FirstLatestDecls;
357 /// \brief Declarations encountered that might be external
360 /// We keep track of external definitions and other 'interesting' declarations
361 /// as we are emitting declarations to the AST file. The AST file contains a
362 /// separate record for these declarations, which are provided to the AST
363 /// consumer by the AST reader. This is behavior is required to properly cope with,
364 /// e.g., tentative variable definitions that occur within
365 /// headers. The declarations themselves are stored as declaration
366 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
368 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
370 /// \brief DeclContexts that have received extensions since their serialized
373 /// For namespaces, when we're chaining and encountering a namespace, we check
374 /// if its primary namespace comes from the chain. If it does, we add the
375 /// primary to this set, so that we can write out lexical content updates for
377 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
379 /// \brief Keeps track of visible decls that were added in DeclContexts
380 /// coming from another AST file.
381 SmallVector<const Decl *, 16> UpdatingVisibleDecls;
383 /// \brief The set of Objective-C class that have categories we
384 /// should serialize.
385 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
387 /// \brief The set of declarations that may have redeclaration chains that
388 /// need to be serialized.
389 llvm::SmallVector<const Decl *, 16> Redeclarations;
391 /// \brief A cache of the first local declaration for "interesting"
392 /// redeclaration chains.
393 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
395 /// \brief Mapping from SwitchCase statements to IDs.
396 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
398 /// \brief The number of statements written to the AST file.
399 unsigned NumStatements;
401 /// \brief The number of macros written to the AST file.
404 /// \brief The number of lexical declcontexts written to the AST
406 unsigned NumLexicalDeclContexts;
408 /// \brief The number of visible declcontexts written to the AST
410 unsigned NumVisibleDeclContexts;
412 /// \brief A mapping from each known submodule to its ID number, which will
413 /// be a positive integer.
414 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
416 /// \brief A list of the module file extension writers.
417 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
418 ModuleFileExtensionWriters;
420 /// \brief Retrieve or create a submodule ID for this module.
421 unsigned getSubmoduleID(Module *Mod);
423 /// \brief Write the given subexpression to the bitstream.
424 void WriteSubStmt(Stmt *S);
426 void WriteBlockInfoBlock();
427 uint64_t WriteControlBlock(Preprocessor &PP, ASTContext &Context,
428 StringRef isysroot, const std::string &OutputFile);
429 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
431 void WriteSourceManagerBlock(SourceManager &SourceMgr,
432 const Preprocessor &PP);
433 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
434 void WriteHeaderSearch(const HeaderSearch &HS);
435 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
436 void WriteSubmodules(Module *WritingModule);
438 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
441 unsigned TypeExtQualAbbrev;
442 unsigned TypeFunctionProtoAbbrev;
443 void WriteTypeAbbrevs();
444 void WriteType(QualType T);
446 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
447 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
449 void GenerateNameLookupTable(const DeclContext *DC,
450 llvm::SmallVectorImpl<char> &LookupTable);
451 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
452 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
453 void WriteTypeDeclOffsets();
454 void WriteFileDeclIDsMap();
455 void WriteComments();
456 void WriteSelectors(Sema &SemaRef);
457 void WriteReferencedSelectorsPool(Sema &SemaRef);
458 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
460 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
461 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
462 void WriteFPPragmaOptions(const FPOptions &Opts);
463 void WriteOpenCLExtensions(Sema &SemaRef);
464 void WriteObjCCategories();
465 void WriteLateParsedTemplates(Sema &SemaRef);
466 void WriteOptimizePragmaOptions(Sema &SemaRef);
467 void WriteMSStructPragmaOptions(Sema &SemaRef);
468 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
469 void WriteModuleFileExtension(Sema &SemaRef,
470 ModuleFileExtensionWriter &Writer);
472 unsigned DeclParmVarAbbrev;
473 unsigned DeclContextLexicalAbbrev;
474 unsigned DeclContextVisibleLookupAbbrev;
475 unsigned UpdateVisibleAbbrev;
476 unsigned DeclRecordAbbrev;
477 unsigned DeclTypedefAbbrev;
478 unsigned DeclVarAbbrev;
479 unsigned DeclFieldAbbrev;
480 unsigned DeclEnumAbbrev;
481 unsigned DeclObjCIvarAbbrev;
482 unsigned DeclCXXMethodAbbrev;
484 unsigned DeclRefExprAbbrev;
485 unsigned CharacterLiteralAbbrev;
486 unsigned IntegerLiteralAbbrev;
487 unsigned ExprImplicitCastAbbrev;
489 void WriteDeclAbbrevs();
490 void WriteDecl(ASTContext &Context, Decl *D);
492 uint64_t WriteASTCore(Sema &SemaRef,
493 StringRef isysroot, const std::string &OutputFile,
494 Module *WritingModule);
497 /// \brief Create a new precompiled header writer that outputs to
498 /// the given bitstream.
499 ASTWriter(llvm::BitstreamWriter &Stream,
500 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
501 bool IncludeTimestamps = true);
502 ~ASTWriter() override;
504 const LangOptions &getLangOpts() const;
506 /// \brief Get a timestamp for output into the AST file. The actual timestamp
507 /// of the specified file may be ignored if we have been instructed to not
508 /// include timestamps in the output file.
509 time_t getTimestampForOutput(const FileEntry *E) const;
511 /// \brief Write a precompiled header for the given semantic analysis.
513 /// \param SemaRef a reference to the semantic analysis object that processed
514 /// the AST to be written into the precompiled header.
516 /// \param WritingModule The module that we are writing. If null, we are
517 /// writing a precompiled header.
519 /// \param isysroot if non-empty, write a relocatable file whose headers
520 /// are relative to the given system root. If we're writing a module, its
521 /// build directory will be used in preference to this if both are available.
523 /// \return the module signature, which eventually will be a hash of
524 /// the module but currently is merely a random 32-bit number.
525 uint64_t WriteAST(Sema &SemaRef, const std::string &OutputFile,
526 Module *WritingModule, StringRef isysroot,
527 bool hasErrors = false);
529 /// \brief Emit a token.
530 void AddToken(const Token &Tok, RecordDataImpl &Record);
532 /// \brief Emit a source location.
533 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
535 /// \brief Emit a source range.
536 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
538 /// \brief Emit a reference to an identifier.
539 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
541 /// \brief Get the unique number used to refer to the given selector.
542 serialization::SelectorID getSelectorRef(Selector Sel);
544 /// \brief Get the unique number used to refer to the given identifier.
545 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
547 /// \brief Get the unique number used to refer to the given macro.
548 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
550 /// \brief Determine the ID of an already-emitted macro.
551 serialization::MacroID getMacroID(MacroInfo *MI);
553 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
555 /// \brief Emit a reference to a type.
556 void AddTypeRef(QualType T, RecordDataImpl &Record);
558 /// \brief Force a type to be emitted and get its ID.
559 serialization::TypeID GetOrCreateTypeID(QualType T);
561 /// \brief Determine the type ID of an already-emitted type.
562 serialization::TypeID getTypeID(QualType T) const;
564 /// \brief Find the first local declaration of a given local redeclarable
566 const Decl *getFirstLocalDecl(const Decl *D);
568 /// \brief Is this a local declaration (that is, one that will be written to
569 /// our AST file)? This is the case for declarations that are neither imported
570 /// from another AST file nor predefined.
571 bool IsLocalDecl(const Decl *D) {
572 if (D->isFromASTFile())
574 auto I = DeclIDs.find(D);
575 return (I == DeclIDs.end() ||
576 I->second >= serialization::NUM_PREDEF_DECL_IDS);
579 /// \brief Emit a reference to a declaration.
580 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
583 /// \brief Force a declaration to be emitted and get its ID.
584 serialization::DeclID GetDeclRef(const Decl *D);
586 /// \brief Determine the declaration ID of an already-emitted
588 serialization::DeclID getDeclID(const Decl *D);
590 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
592 /// \brief Add a string to the given record.
593 void AddString(StringRef Str, RecordDataImpl &Record);
595 /// \brief Convert a path from this build process into one that is appropriate
596 /// for emission in the module file.
597 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
599 /// \brief Add a path to the given record.
600 void AddPath(StringRef Path, RecordDataImpl &Record);
602 /// \brief Emit the current record with the given path as a blob.
603 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
606 /// \brief Add a version tuple to the given record
607 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
609 /// \brief Infer the submodule ID that contains an entity at the given
611 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
613 /// \brief Retrieve or create a submodule ID for this module, or return 0 if
614 /// the submodule is neither local (a submodle of the currently-written module)
615 /// nor from an imported module.
616 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
618 /// \brief Note that the identifier II occurs at the given offset
619 /// within the identifier table.
620 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
622 /// \brief Note that the selector Sel occurs at the given offset
623 /// within the method pool/selector table.
624 void SetSelectorOffset(Selector Sel, uint32_t Offset);
626 /// \brief Record an ID for the given switch-case statement.
627 unsigned RecordSwitchCaseID(SwitchCase *S);
629 /// \brief Retrieve the ID for the given switch-case statement.
630 unsigned getSwitchCaseID(SwitchCase *S);
632 void ClearSwitchCaseIDs();
634 unsigned getTypeExtQualAbbrev() const {
635 return TypeExtQualAbbrev;
637 unsigned getTypeFunctionProtoAbbrev() const {
638 return TypeFunctionProtoAbbrev;
641 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
642 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
643 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
644 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
645 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
646 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
647 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
648 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
650 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
651 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
652 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
653 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
655 bool hasChain() const { return Chain; }
656 ASTReader *getChain() const { return Chain; }
659 // ASTDeserializationListener implementation
660 void ReaderInitialized(ASTReader *Reader) override;
661 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
662 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
663 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
664 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
665 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
666 MacroDefinitionRecord *MD) override;
667 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
669 // ASTMutationListener implementation.
670 void CompletedTagDefinition(const TagDecl *D) override;
671 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
672 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
673 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
674 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
675 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
676 const FunctionDecl *Delete) override;
677 void CompletedImplicitDefinition(const FunctionDecl *D) override;
678 void StaticDataMemberInstantiated(const VarDecl *D) override;
679 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
680 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
681 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
682 const ObjCInterfaceDecl *IFD) override;
683 void DeclarationMarkedUsed(const Decl *D) override;
684 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
685 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
686 const Attr *Attr) override;
687 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
688 void AddedAttributeToRecord(const Attr *Attr,
689 const RecordDecl *Record) override;
692 /// \brief An object for streaming information to a record.
693 class ASTRecordWriter {
695 ASTWriter::RecordDataImpl *Record;
697 /// \brief Statements that we've encountered while serializing a
698 /// declaration or type.
699 SmallVector<Stmt *, 16> StmtsToEmit;
701 /// \brief Indices of record elements that describe offsets within the
702 /// bitcode. These will be converted to offsets relative to the current
703 /// record when emitted.
704 SmallVector<unsigned, 8> OffsetIndices;
706 /// \brief Flush all of the statements and expressions that have
707 /// been added to the queue via AddStmt().
709 void FlushSubStmts();
711 void PrepareToEmit(uint64_t MyOffset) {
712 // Convert offsets into relative form.
713 for (unsigned I : OffsetIndices) {
714 auto &StoredOffset = (*Record)[I];
715 assert(StoredOffset < MyOffset && "invalid offset");
717 StoredOffset = MyOffset - StoredOffset;
719 OffsetIndices.clear();
723 /// Construct a ASTRecordWriter that uses the default encoding scheme.
724 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
725 : Writer(&Writer), Record(&Record) {}
727 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
729 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
730 : Writer(Parent.Writer), Record(&Record) {}
732 /// Copying an ASTRecordWriter is almost certainly a bug.
733 ASTRecordWriter(const ASTRecordWriter&) = delete;
734 void operator=(const ASTRecordWriter&) = delete;
736 /// \brief Extract the underlying record storage.
737 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
739 /// \brief Minimal vector-like interface.
741 void push_back(uint64_t N) { Record->push_back(N); }
742 template<typename InputIterator>
743 void append(InputIterator begin, InputIterator end) {
744 Record->append(begin, end);
746 bool empty() const { return Record->empty(); }
747 size_t size() const { return Record->size(); }
748 uint64_t &operator[](size_t N) { return (*Record)[N]; }
751 /// \brief Emit the record to the stream, followed by its substatements, and
752 /// return its offset.
753 // FIXME: Allow record producers to suggest Abbrevs.
754 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
755 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
756 PrepareToEmit(Offset);
757 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
762 /// \brief Emit the record to the stream, preceded by its substatements.
763 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
765 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
766 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
767 return Writer->Stream.GetCurrentBitNo();
770 /// \brief Add a bit offset into the record. This will be converted into an
771 /// offset relative to the current record when emitted.
772 void AddOffset(uint64_t BitOffset) {
773 OffsetIndices.push_back(Record->size());
774 Record->push_back(BitOffset);
777 /// \brief Add the given statement or expression to the queue of
778 /// statements to emit.
780 /// This routine should be used when emitting types and declarations
781 /// that have expressions as part of their formulation. Once the
782 /// type or declaration has been written, Emit() will write
783 /// the corresponding statements just after the record.
784 void AddStmt(Stmt *S) {
785 StmtsToEmit.push_back(S);
788 /// \brief Add a definition for the given function to the queue of statements
790 void AddFunctionDefinition(const FunctionDecl *FD);
792 /// \brief Emit a source location.
793 void AddSourceLocation(SourceLocation Loc) {
794 return Writer->AddSourceLocation(Loc, *Record);
797 /// \brief Emit a source range.
798 void AddSourceRange(SourceRange Range) {
799 return Writer->AddSourceRange(Range, *Record);
802 /// \brief Emit an integral value.
803 void AddAPInt(const llvm::APInt &Value);
805 /// \brief Emit a signed integral value.
806 void AddAPSInt(const llvm::APSInt &Value);
808 /// \brief Emit a floating-point value.
809 void AddAPFloat(const llvm::APFloat &Value);
811 /// \brief Emit a reference to an identifier.
812 void AddIdentifierRef(const IdentifierInfo *II) {
813 return Writer->AddIdentifierRef(II, *Record);
816 /// \brief Emit a Selector (which is a smart pointer reference).
817 void AddSelectorRef(Selector S);
819 /// \brief Emit a CXXTemporary.
820 void AddCXXTemporary(const CXXTemporary *Temp);
822 /// \brief Emit a C++ base specifier.
823 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
825 /// \brief Emit a set of C++ base specifiers.
826 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
828 /// \brief Emit a reference to a type.
829 void AddTypeRef(QualType T) {
830 return Writer->AddTypeRef(T, *Record);
833 /// \brief Emits a reference to a declarator info.
834 void AddTypeSourceInfo(TypeSourceInfo *TInfo);
836 /// \brief Emits a type with source-location information.
837 void AddTypeLoc(TypeLoc TL);
839 /// \brief Emits a template argument location info.
840 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
841 const TemplateArgumentLocInfo &Arg);
843 /// \brief Emits a template argument location.
844 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
846 /// \brief Emits an AST template argument list info.
847 void AddASTTemplateArgumentListInfo(
848 const ASTTemplateArgumentListInfo *ASTTemplArgList);
850 /// \brief Emit a reference to a declaration.
851 void AddDeclRef(const Decl *D) {
852 return Writer->AddDeclRef(D, *Record);
855 /// \brief Emit a declaration name.
856 void AddDeclarationName(DeclarationName Name);
858 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
859 DeclarationName Name);
860 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
862 void AddQualifierInfo(const QualifierInfo &Info);
864 /// \brief Emit a nested name specifier.
865 void AddNestedNameSpecifier(NestedNameSpecifier *NNS);
867 /// \brief Emit a nested name specifier with source-location information.
868 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
870 /// \brief Emit a template name.
871 void AddTemplateName(TemplateName Name);
873 /// \brief Emit a template argument.
874 void AddTemplateArgument(const TemplateArgument &Arg);
876 /// \brief Emit a template parameter list.
877 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
879 /// \brief Emit a template argument list.
880 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
882 /// \brief Emit a UnresolvedSet structure.
883 void AddUnresolvedSet(const ASTUnresolvedSet &Set);
885 /// \brief Emit a CXXCtorInitializer array.
886 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer*> CtorInits);
888 void AddCXXDefinitionData(const CXXRecordDecl *D);
890 /// \brief Emit a string.
891 void AddString(StringRef Str) {
892 return Writer->AddString(Str, *Record);
895 /// \brief Emit a path.
896 void AddPath(StringRef Path) {
897 return Writer->AddPath(Path, *Record);
900 /// \brief Emit a version tuple.
901 void AddVersionTuple(const VersionTuple &Version) {
902 return Writer->AddVersionTuple(Version, *Record);
905 /// \brief Emit a list of attributes.
906 void AddAttributes(ArrayRef<const Attr*> Attrs);
909 /// \brief AST and semantic-analysis consumer that generates a
910 /// precompiled header from the parsed source code.
911 class PCHGenerator : public SemaConsumer {
912 const Preprocessor &PP;
913 std::string OutputFile;
914 clang::Module *Module;
915 std::string isysroot;
917 std::shared_ptr<PCHBuffer> Buffer;
918 llvm::BitstreamWriter Stream;
920 bool AllowASTWithErrors;
923 ASTWriter &getWriter() { return Writer; }
924 const ASTWriter &getWriter() const { return Writer; }
925 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
929 const Preprocessor &PP, StringRef OutputFile,
930 clang::Module *Module, StringRef isysroot,
931 std::shared_ptr<PCHBuffer> Buffer,
932 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
933 bool AllowASTWithErrors = false,
934 bool IncludeTimestamps = true);
935 ~PCHGenerator() override;
936 void InitializeSema(Sema &S) override { SemaPtr = &S; }
937 void HandleTranslationUnit(ASTContext &Ctx) override;
938 ASTMutationListener *GetASTMutationListener() override;
939 ASTDeserializationListener *GetASTDeserializationListener() override;
940 bool hasEmittedPCH() const { return Buffer->IsComplete; }
943 } // end namespace clang