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;
94 /// \brief Map that provides the ID numbers of each type within the
95 /// output stream, plus those deserialized from a chained PCH.
97 /// The ID numbers of types are consecutive (in order of discovery)
98 /// and start at 1. 0 is reserved for NULL. When types are actually
99 /// stored in the stream, the ID number is shifted by 2 bits to
100 /// allow for the const/volatile qualifiers.
102 /// Keys in the map never have const/volatile qualifiers.
103 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
104 serialization::UnsafeQualTypeDenseMapInfo>
107 /// \brief The bitstream writer used to emit this precompiled header.
108 llvm::BitstreamWriter &Stream;
110 /// \brief The ASTContext we're writing.
113 /// \brief The preprocessor we're writing.
116 /// \brief The reader of existing AST files, if we're chaining.
119 /// \brief The module we're currently writing, if any.
120 Module *WritingModule;
122 /// \brief The base directory for any relative paths we emit.
123 std::string BaseDirectory;
125 /// \brief Indicates whether timestamps should be written to the produced
126 /// module file. This is the case for files implicitly written to the
127 /// module cache, where we need the timestamps to determine if the module
128 /// file is up to date, but not otherwise.
129 bool IncludeTimestamps;
131 /// \brief Indicates when the AST writing is actively performing
132 /// serialization, rather than just queueing updates.
135 /// \brief Indicates that we are done serializing the collection of decls
136 /// and types to emit.
137 bool DoneWritingDeclsAndTypes;
139 /// \brief Indicates that the AST contained compiler errors.
140 bool ASTHasCompilerErrors;
142 /// \brief Mapping from input file entries to the index into the
143 /// offset table where information about that input file is stored.
144 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
146 /// \brief Stores a declaration or a type to be written to the AST file.
149 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
150 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
152 bool isType() const { return IsType; }
153 bool isDecl() const { return !IsType; }
155 QualType getType() const {
156 assert(isType() && "Not a type!");
157 return QualType::getFromOpaquePtr(Stored);
160 Decl *getDecl() const {
161 assert(isDecl() && "Not a decl!");
162 return static_cast<Decl *>(Stored);
170 /// \brief The declarations and types to emit.
171 std::queue<DeclOrType> DeclTypesToEmit;
173 /// \brief The first ID number we can use for our own declarations.
174 serialization::DeclID FirstDeclID;
176 /// \brief The decl ID that will be assigned to the next new decl.
177 serialization::DeclID NextDeclID;
179 /// \brief Map that provides the ID numbers of each declaration within
180 /// the output stream, as well as those deserialized from a chained PCH.
182 /// The ID numbers of declarations are consecutive (in order of
183 /// discovery) and start at 2. 1 is reserved for the translation
184 /// unit, while 0 is reserved for NULL.
185 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
187 /// \brief Offset of each declaration in the bitstream, indexed by
188 /// the declaration's ID.
189 std::vector<serialization::DeclOffset> DeclOffsets;
191 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
192 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
194 struct DeclIDInFileInfo {
195 LocDeclIDsTy DeclIDs;
196 /// \brief Set when the DeclIDs vectors from all files are joined, this
197 /// indicates the index that this particular vector has in the global one.
198 unsigned FirstDeclIndex;
200 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
202 /// \brief Map from file SLocEntries to info about the file-level declarations
203 /// that it contains.
204 FileDeclIDsTy FileDeclIDs;
206 void associateDeclWithFile(const Decl *D, serialization::DeclID);
208 /// \brief The first ID number we can use for our own types.
209 serialization::TypeID FirstTypeID;
211 /// \brief The type ID that will be assigned to the next new type.
212 serialization::TypeID NextTypeID;
214 /// \brief Map that provides the ID numbers of each type within the
215 /// output stream, plus those deserialized from a chained PCH.
217 /// The ID numbers of types are consecutive (in order of discovery)
218 /// and start at 1. 0 is reserved for NULL. When types are actually
219 /// stored in the stream, the ID number is shifted by 2 bits to
220 /// allow for the const/volatile qualifiers.
222 /// Keys in the map never have const/volatile qualifiers.
225 /// \brief Offset of each type in the bitstream, indexed by
227 std::vector<uint32_t> TypeOffsets;
229 /// \brief The first ID number we can use for our own identifiers.
230 serialization::IdentID FirstIdentID;
232 /// \brief The identifier ID that will be assigned to the next new identifier.
233 serialization::IdentID NextIdentID;
235 /// \brief Map that provides the ID numbers of each identifier in
236 /// the output stream.
238 /// The ID numbers for identifiers are consecutive (in order of
239 /// discovery), starting at 1. An ID of zero refers to a NULL
241 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
243 /// \brief The first ID number we can use for our own macros.
244 serialization::MacroID FirstMacroID;
246 /// \brief The identifier ID that will be assigned to the next new identifier.
247 serialization::MacroID NextMacroID;
249 /// \brief Map that provides the ID numbers of each macro.
250 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
252 struct MacroInfoToEmitData {
253 const IdentifierInfo *Name;
255 serialization::MacroID ID;
257 /// \brief The macro infos to emit.
258 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
260 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
262 /// @name FlushStmt Caches
265 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
266 llvm::DenseSet<Stmt *> ParentStmts;
268 /// \brief Offsets of sub-stmts already serialized. The offset points
269 /// just after the stmt record.
270 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
274 /// \brief Offsets of each of the identifier IDs into the identifier
276 std::vector<uint32_t> IdentifierOffsets;
278 /// \brief The first ID number we can use for our own submodules.
279 serialization::SubmoduleID FirstSubmoduleID;
281 /// \brief The submodule ID that will be assigned to the next new submodule.
282 serialization::SubmoduleID NextSubmoduleID;
284 /// \brief The first ID number we can use for our own selectors.
285 serialization::SelectorID FirstSelectorID;
287 /// \brief The selector ID that will be assigned to the next new selector.
288 serialization::SelectorID NextSelectorID;
290 /// \brief Map that provides the ID numbers of each Selector.
291 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
293 /// \brief Offset of each selector within the method pool/selector
294 /// table, indexed by the Selector ID (-1).
295 std::vector<uint32_t> SelectorOffsets;
297 /// \brief Mapping from macro definitions (as they occur in the preprocessing
298 /// record) to the macro IDs.
299 llvm::DenseMap<const MacroDefinitionRecord *,
300 serialization::PreprocessedEntityID> MacroDefinitions;
302 /// \brief Cache of indices of anonymous declarations within their lexical
304 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
306 /// An update to a Decl.
308 /// A DeclUpdateKind.
316 const Attr *Attribute;
320 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
321 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
322 DeclUpdate(unsigned Kind, QualType Type)
323 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
324 DeclUpdate(unsigned Kind, SourceLocation Loc)
325 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
326 DeclUpdate(unsigned Kind, unsigned Val)
327 : Kind(Kind), Val(Val) {}
328 DeclUpdate(unsigned Kind, Module *M)
329 : Kind(Kind), Mod(M) {}
330 DeclUpdate(unsigned Kind, const Attr *Attribute)
331 : Kind(Kind), Attribute(Attribute) {}
333 unsigned getKind() const { return Kind; }
334 const Decl *getDecl() const { return Dcl; }
335 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
336 SourceLocation getLoc() const {
337 return SourceLocation::getFromRawEncoding(Loc);
339 unsigned getNumber() const { return Val; }
340 Module *getModule() const { return Mod; }
341 const Attr *getAttr() const { return Attribute; }
344 typedef SmallVector<DeclUpdate, 1> UpdateRecord;
345 typedef llvm::MapVector<const Decl *, UpdateRecord> DeclUpdateMap;
346 /// \brief Mapping from declarations that came from a chained PCH to the
347 /// record containing modifications to them.
348 DeclUpdateMap DeclUpdates;
350 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
351 /// \brief Map of first declarations from a chained PCH that point to the
352 /// most recent declarations in another PCH.
353 FirstLatestDeclMap FirstLatestDecls;
355 /// \brief Declarations encountered that might be external
358 /// We keep track of external definitions and other 'interesting' declarations
359 /// as we are emitting declarations to the AST file. The AST file contains a
360 /// separate record for these declarations, which are provided to the AST
361 /// consumer by the AST reader. This is behavior is required to properly cope with,
362 /// e.g., tentative variable definitions that occur within
363 /// headers. The declarations themselves are stored as declaration
364 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
366 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
368 /// \brief DeclContexts that have received extensions since their serialized
371 /// For namespaces, when we're chaining and encountering a namespace, we check
372 /// if its primary namespace comes from the chain. If it does, we add the
373 /// primary to this set, so that we can write out lexical content updates for
375 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
377 /// \brief Keeps track of visible decls that were added in DeclContexts
378 /// coming from another AST file.
379 SmallVector<const Decl *, 16> UpdatingVisibleDecls;
381 /// \brief The set of Objective-C class that have categories we
382 /// should serialize.
383 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
385 struct ReplacedDeclInfo {
386 serialization::DeclID ID;
390 ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
391 ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
393 : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
396 /// \brief Decls that have been replaced in the current dependent AST file.
398 /// When a decl changes fundamentally after being deserialized (this shouldn't
399 /// happen, but the ObjC AST nodes are designed this way), it will be
400 /// serialized again. In this case, it is registered here, so that the reader
401 /// knows to read the updated version.
402 SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
404 /// \brief The set of declarations that may have redeclaration chains that
405 /// need to be serialized.
406 llvm::SmallVector<const Decl *, 16> Redeclarations;
408 /// \brief A cache of the first local declaration for "interesting"
409 /// redeclaration chains.
410 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
412 /// \brief Statements that we've encountered while serializing a
413 /// declaration or type.
414 SmallVector<Stmt *, 16> StmtsToEmit;
416 /// \brief Statements collection to use for ASTWriter::AddStmt().
417 /// It will point to StmtsToEmit unless it is overriden.
418 SmallVector<Stmt *, 16> *CollectedStmts;
420 /// \brief Mapping from SwitchCase statements to IDs.
421 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
423 /// \brief The number of statements written to the AST file.
424 unsigned NumStatements;
426 /// \brief The number of macros written to the AST file.
429 /// \brief The number of lexical declcontexts written to the AST
431 unsigned NumLexicalDeclContexts;
433 /// \brief The number of visible declcontexts written to the AST
435 unsigned NumVisibleDeclContexts;
437 /// \brief The offset of each CXXBaseSpecifier set within the AST.
438 SmallVector<uint32_t, 16> CXXBaseSpecifiersOffsets;
440 /// \brief The first ID number we can use for our own base specifiers.
441 serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
443 /// \brief The base specifiers ID that will be assigned to the next new
444 /// set of C++ base specifiers.
445 serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
447 /// \brief A set of C++ base specifiers that is queued to be written into the
449 struct QueuedCXXBaseSpecifiers {
450 QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
452 QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
453 CXXBaseSpecifier const *Bases,
454 CXXBaseSpecifier const *BasesEnd)
455 : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
457 serialization::CXXBaseSpecifiersID ID;
458 CXXBaseSpecifier const * Bases;
459 CXXBaseSpecifier const * BasesEnd;
462 /// \brief Queue of C++ base specifiers to be written to the AST file,
463 /// in the order they should be written.
464 SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
466 /// \brief The offset of each CXXCtorInitializer list within the AST.
467 SmallVector<uint32_t, 16> CXXCtorInitializersOffsets;
469 /// \brief The first ID number we can use for our own ctor initializers.
470 serialization::CXXCtorInitializersID FirstCXXCtorInitializersID;
472 /// \brief The ctor initializers ID that will be assigned to the next new
473 /// list of C++ ctor initializers.
474 serialization::CXXCtorInitializersID NextCXXCtorInitializersID;
476 /// \brief A set of C++ ctor initializers that is queued to be written
477 /// into the AST file.
478 struct QueuedCXXCtorInitializers {
479 QueuedCXXCtorInitializers() : ID() {}
481 QueuedCXXCtorInitializers(serialization::CXXCtorInitializersID ID,
482 ArrayRef<CXXCtorInitializer*> Inits)
483 : ID(ID), Inits(Inits) {}
485 serialization::CXXCtorInitializersID ID;
486 ArrayRef<CXXCtorInitializer*> Inits;
489 /// \brief Queue of C++ ctor initializers to be written to the AST file,
490 /// in the order they should be written.
491 SmallVector<QueuedCXXCtorInitializers, 2> CXXCtorInitializersToWrite;
493 /// \brief A mapping from each known submodule to its ID number, which will
494 /// be a positive integer.
495 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
497 /// \brief A list of the module file extension writers.
498 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
499 ModuleFileExtensionWriters;
501 /// \brief Retrieve or create a submodule ID for this module.
502 unsigned getSubmoduleID(Module *Mod);
504 /// \brief Write the given subexpression to the bitstream.
505 void WriteSubStmt(Stmt *S,
506 llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
507 llvm::DenseSet<Stmt *> &ParentStmts);
509 void WriteBlockInfoBlock();
510 uint64_t WriteControlBlock(Preprocessor &PP, ASTContext &Context,
511 StringRef isysroot, const std::string &OutputFile);
512 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
514 void WriteSourceManagerBlock(SourceManager &SourceMgr,
515 const Preprocessor &PP);
516 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
517 void WriteHeaderSearch(const HeaderSearch &HS);
518 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
519 void WriteSubmodules(Module *WritingModule);
521 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
523 void WriteCXXBaseSpecifiersOffsets();
524 void WriteCXXCtorInitializersOffsets();
526 unsigned TypeExtQualAbbrev;
527 unsigned TypeFunctionProtoAbbrev;
528 void WriteTypeAbbrevs();
529 void WriteType(QualType T);
531 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
532 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
534 void GenerateNameLookupTable(const DeclContext *DC,
535 llvm::SmallVectorImpl<char> &LookupTable);
536 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
537 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
538 void WriteTypeDeclOffsets();
539 void WriteFileDeclIDsMap();
540 void WriteComments();
541 void WriteSelectors(Sema &SemaRef);
542 void WriteReferencedSelectorsPool(Sema &SemaRef);
543 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
545 void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
546 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
547 void WriteDeclReplacementsBlock();
548 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
549 void WriteFPPragmaOptions(const FPOptions &Opts);
550 void WriteOpenCLExtensions(Sema &SemaRef);
551 void WriteObjCCategories();
552 void WriteLateParsedTemplates(Sema &SemaRef);
553 void WriteOptimizePragmaOptions(Sema &SemaRef);
554 void WriteModuleFileExtension(Sema &SemaRef,
555 ModuleFileExtensionWriter &Writer);
557 unsigned DeclParmVarAbbrev;
558 unsigned DeclContextLexicalAbbrev;
559 unsigned DeclContextVisibleLookupAbbrev;
560 unsigned UpdateVisibleAbbrev;
561 unsigned DeclRecordAbbrev;
562 unsigned DeclTypedefAbbrev;
563 unsigned DeclVarAbbrev;
564 unsigned DeclFieldAbbrev;
565 unsigned DeclEnumAbbrev;
566 unsigned DeclObjCIvarAbbrev;
567 unsigned DeclCXXMethodAbbrev;
569 unsigned DeclRefExprAbbrev;
570 unsigned CharacterLiteralAbbrev;
571 unsigned IntegerLiteralAbbrev;
572 unsigned ExprImplicitCastAbbrev;
574 void WriteDeclAbbrevs();
575 void WriteDecl(ASTContext &Context, Decl *D);
576 void AddFunctionDefinition(const FunctionDecl *FD, RecordData &Record);
578 uint64_t WriteASTCore(Sema &SemaRef,
579 StringRef isysroot, const std::string &OutputFile,
580 Module *WritingModule);
583 /// \brief Create a new precompiled header writer that outputs to
584 /// the given bitstream.
585 ASTWriter(llvm::BitstreamWriter &Stream,
586 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
587 bool IncludeTimestamps = true);
588 ~ASTWriter() override;
590 const LangOptions &getLangOpts() const;
592 /// \brief Get a timestamp for output into the AST file. The actual timestamp
593 /// of the specified file may be ignored if we have been instructed to not
594 /// include timestamps in the output file.
595 time_t getTimestampForOutput(const FileEntry *E) const;
597 /// \brief Write a precompiled header for the given semantic analysis.
599 /// \param SemaRef a reference to the semantic analysis object that processed
600 /// the AST to be written into the precompiled header.
602 /// \param WritingModule The module that we are writing. If null, we are
603 /// writing a precompiled header.
605 /// \param isysroot if non-empty, write a relocatable file whose headers
606 /// are relative to the given system root. If we're writing a module, its
607 /// build directory will be used in preference to this if both are available.
609 /// \return the module signature, which eventually will be a hash of
610 /// the module but currently is merely a random 32-bit number.
611 uint64_t WriteAST(Sema &SemaRef, const std::string &OutputFile,
612 Module *WritingModule, StringRef isysroot,
613 bool hasErrors = false);
615 /// \brief Emit a token.
616 void AddToken(const Token &Tok, RecordDataImpl &Record);
618 /// \brief Emit a source location.
619 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
621 /// \brief Emit a source range.
622 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
624 /// \brief Emit an integral value.
625 void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
627 /// \brief Emit a signed integral value.
628 void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
630 /// \brief Emit a floating-point value.
631 void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
633 /// \brief Emit a reference to an identifier.
634 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
636 /// \brief Emit a Selector (which is a smart pointer reference).
637 void AddSelectorRef(Selector, RecordDataImpl &Record);
639 /// \brief Emit a CXXTemporary.
640 void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
642 /// \brief Emit a set of C++ base specifiers to the record.
643 void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
644 CXXBaseSpecifier const *BasesEnd,
645 RecordDataImpl &Record);
647 /// \brief Get the unique number used to refer to the given selector.
648 serialization::SelectorID getSelectorRef(Selector Sel);
650 /// \brief Get the unique number used to refer to the given identifier.
651 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
653 /// \brief Get the unique number used to refer to the given macro.
654 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
656 /// \brief Determine the ID of an already-emitted macro.
657 serialization::MacroID getMacroID(MacroInfo *MI);
659 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
661 /// \brief Emit a reference to a type.
662 void AddTypeRef(QualType T, RecordDataImpl &Record);
664 /// \brief Force a type to be emitted and get its ID.
665 serialization::TypeID GetOrCreateTypeID(QualType T);
667 /// \brief Determine the type ID of an already-emitted type.
668 serialization::TypeID getTypeID(QualType T) const;
670 /// \brief Emits a reference to a declarator info.
671 void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
673 /// \brief Emits a type with source-location information.
674 void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
676 /// \brief Emits a template argument location info.
677 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
678 const TemplateArgumentLocInfo &Arg,
679 RecordDataImpl &Record);
681 /// \brief Emits a template argument location.
682 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
683 RecordDataImpl &Record);
685 /// \brief Emits an AST template argument list info.
686 void AddASTTemplateArgumentListInfo(
687 const ASTTemplateArgumentListInfo *ASTTemplArgList,
688 RecordDataImpl &Record);
690 /// \brief Find the first local declaration of a given local redeclarable
692 const Decl *getFirstLocalDecl(const Decl *D);
694 /// \brief Emit a reference to a declaration.
695 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
698 /// \brief Force a declaration to be emitted and get its ID.
699 serialization::DeclID GetDeclRef(const Decl *D);
701 /// \brief Determine the declaration ID of an already-emitted
703 serialization::DeclID getDeclID(const Decl *D);
705 /// \brief Emit a declaration name.
706 void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
707 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
708 DeclarationName Name, RecordDataImpl &Record);
709 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
710 RecordDataImpl &Record);
711 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
713 void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
715 /// \brief Emit a nested name specifier.
716 void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
718 /// \brief Emit a nested name specifier with source-location information.
719 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
720 RecordDataImpl &Record);
722 /// \brief Emit a template name.
723 void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
725 /// \brief Emit a template argument.
726 void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
728 /// \brief Emit a template parameter list.
729 void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
730 RecordDataImpl &Record);
732 /// \brief Emit a template argument list.
733 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
734 RecordDataImpl &Record);
736 /// \brief Emit a UnresolvedSet structure.
737 void AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record);
739 /// \brief Emit a C++ base specifier.
740 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
741 RecordDataImpl &Record);
743 /// \brief Emit the ID for a CXXCtorInitializer array and register the array
744 /// for later serialization.
745 void AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
746 RecordDataImpl &Record);
748 /// \brief Emit a CXXCtorInitializer array.
749 void AddCXXCtorInitializers(
750 const CXXCtorInitializer * const *CtorInitializers,
751 unsigned NumCtorInitializers,
752 RecordDataImpl &Record);
754 void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
756 /// \brief Add a string to the given record.
757 void AddString(StringRef Str, RecordDataImpl &Record);
759 /// \brief Convert a path from this build process into one that is appropriate
760 /// for emission in the module file.
761 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
763 /// \brief Add a path to the given record.
764 void AddPath(StringRef Path, RecordDataImpl &Record);
766 /// \brief Emit the current record with the given path as a blob.
767 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
770 /// \brief Add a version tuple to the given record
771 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
773 /// \brief Infer the submodule ID that contains an entity at the given
775 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
777 /// \brief Retrieve or create a submodule ID for this module, or return 0 if
778 /// the submodule is neither local (a submodle of the currently-written module)
779 /// nor from an imported module.
780 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
782 /// \brief Note that the identifier II occurs at the given offset
783 /// within the identifier table.
784 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
786 /// \brief Note that the selector Sel occurs at the given offset
787 /// within the method pool/selector table.
788 void SetSelectorOffset(Selector Sel, uint32_t Offset);
790 /// \brief Add the given statement or expression to the queue of
791 /// statements to emit.
793 /// This routine should be used when emitting types and declarations
794 /// that have expressions as part of their formulation. Once the
795 /// type or declaration has been written, call FlushStmts() to write
796 /// the corresponding statements just after the type or
798 void AddStmt(Stmt *S) {
799 CollectedStmts->push_back(S);
802 /// \brief Flush all of the statements and expressions that have
803 /// been added to the queue via AddStmt().
806 /// \brief Flush all of the C++ base specifier sets that have been added
807 /// via \c AddCXXBaseSpecifiersRef().
808 void FlushCXXBaseSpecifiers();
810 /// \brief Flush all of the C++ constructor initializer lists that have been
811 /// added via \c AddCXXCtorInitializersRef().
812 void FlushCXXCtorInitializers();
814 /// \brief Flush all pending records that are tacked onto the end of
815 /// decl and decl update records.
816 void FlushPendingAfterDecl() {
818 FlushCXXBaseSpecifiers();
819 FlushCXXCtorInitializers();
822 /// \brief Record an ID for the given switch-case statement.
823 unsigned RecordSwitchCaseID(SwitchCase *S);
825 /// \brief Retrieve the ID for the given switch-case statement.
826 unsigned getSwitchCaseID(SwitchCase *S);
828 void ClearSwitchCaseIDs();
830 unsigned getTypeExtQualAbbrev() const {
831 return TypeExtQualAbbrev;
833 unsigned getTypeFunctionProtoAbbrev() const {
834 return TypeFunctionProtoAbbrev;
837 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
838 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
839 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
840 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
841 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
842 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
843 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
844 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
846 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
847 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
848 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
849 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
851 bool hasChain() const { return Chain; }
852 ASTReader *getChain() const { return Chain; }
854 // ASTDeserializationListener implementation
855 void ReaderInitialized(ASTReader *Reader) override;
856 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
857 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
858 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
859 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
860 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
861 MacroDefinitionRecord *MD) override;
862 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
864 // ASTMutationListener implementation.
865 void CompletedTagDefinition(const TagDecl *D) override;
866 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
867 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
868 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
869 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
870 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
871 const FunctionDecl *Delete) override;
872 void CompletedImplicitDefinition(const FunctionDecl *D) override;
873 void StaticDataMemberInstantiated(const VarDecl *D) override;
874 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
875 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
876 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
877 const ObjCInterfaceDecl *IFD) override;
878 void DeclarationMarkedUsed(const Decl *D) override;
879 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
880 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
881 void AddedAttributeToRecord(const Attr *Attr,
882 const RecordDecl *Record) override;
885 /// \brief AST and semantic-analysis consumer that generates a
886 /// precompiled header from the parsed source code.
887 class PCHGenerator : public SemaConsumer {
888 const Preprocessor &PP;
889 std::string OutputFile;
890 clang::Module *Module;
891 std::string isysroot;
893 std::shared_ptr<PCHBuffer> Buffer;
894 llvm::BitstreamWriter Stream;
896 bool AllowASTWithErrors;
899 ASTWriter &getWriter() { return Writer; }
900 const ASTWriter &getWriter() const { return Writer; }
901 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
905 const Preprocessor &PP, StringRef OutputFile,
906 clang::Module *Module, StringRef isysroot,
907 std::shared_ptr<PCHBuffer> Buffer,
908 ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
909 bool AllowASTWithErrors = false,
910 bool IncludeTimestamps = true);
911 ~PCHGenerator() override;
912 void InitializeSema(Sema &S) override { SemaPtr = &S; }
913 void HandleTranslationUnit(ASTContext &Ctx) override;
914 ASTMutationListener *GetASTMutationListener() override;
915 ASTDeserializationListener *GetASTDeserializationListener() override;
916 bool hasEmittedPCH() const { return Buffer->IsComplete; }
919 } // end namespace clang