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/AST/TemplateBase.h"
21 #include "clang/Sema/SemaConsumer.h"
22 #include "clang/Serialization/ASTBitCodes.h"
23 #include "clang/Serialization/ASTDeserializationListener.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/DenseSet.h"
26 #include "llvm/ADT/MapVector.h"
27 #include "llvm/ADT/SetVector.h"
28 #include "llvm/ADT/SmallPtrSet.h"
29 #include "llvm/ADT/SmallVector.h"
30 #include "llvm/Bitcode/BitstreamWriter.h"
38 class BitstreamWriter;
44 class NestedNameSpecifier;
45 class CXXBaseSpecifier;
46 class CXXCtorInitializer;
50 class HeaderSearchOptions;
51 class IdentifierResolver;
52 class MacroDefinition;
55 class OpaqueValueExpr;
59 class PreprocessedEntity;
60 class PreprocessingRecord;
68 class ASTUnresolvedSet;
70 namespace SrcMgr { class SLocEntry; }
72 /// \brief Writes an AST file containing the contents of a translation unit.
74 /// The ASTWriter class produces a bitstream containing the serialized
75 /// representation of a given abstract syntax tree and its supporting
76 /// data structures. This bitstream can be de-serialized via an
77 /// instance of the ASTReader class.
78 class ASTWriter : public ASTDeserializationListener,
79 public ASTMutationListener {
81 typedef SmallVector<uint64_t, 64> RecordData;
82 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
84 friend class ASTDeclWriter;
85 friend class ASTStmtWriter;
87 /// \brief Map that provides the ID numbers of each type within the
88 /// output stream, plus those deserialized from a chained PCH.
90 /// The ID numbers of types are consecutive (in order of discovery)
91 /// and start at 1. 0 is reserved for NULL. When types are actually
92 /// stored in the stream, the ID number is shifted by 2 bits to
93 /// allow for the const/volatile qualifiers.
95 /// Keys in the map never have const/volatile qualifiers.
96 typedef llvm::DenseMap<QualType, serialization::TypeIdx,
97 serialization::UnsafeQualTypeDenseMapInfo>
100 /// \brief The bitstream writer used to emit this precompiled header.
101 llvm::BitstreamWriter &Stream;
103 /// \brief The ASTContext we're writing.
106 /// \brief The preprocessor we're writing.
109 /// \brief The reader of existing AST files, if we're chaining.
112 /// \brief The module we're currently writing, if any.
113 Module *WritingModule;
115 /// \brief The base directory for any relative paths we emit.
116 std::string BaseDirectory;
118 /// \brief Indicates when the AST writing is actively performing
119 /// serialization, rather than just queueing updates.
122 /// \brief Indicates that we are done serializing the collection of decls
123 /// and types to emit.
124 bool DoneWritingDeclsAndTypes;
126 /// \brief Indicates that the AST contained compiler errors.
127 bool ASTHasCompilerErrors;
129 /// \brief Mapping from input file entries to the index into the
130 /// offset table where information about that input file is stored.
131 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
133 /// \brief Stores a declaration or a type to be written to the AST file.
136 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
137 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
139 bool isType() const { return IsType; }
140 bool isDecl() const { return !IsType; }
142 QualType getType() const {
143 assert(isType() && "Not a type!");
144 return QualType::getFromOpaquePtr(Stored);
147 Decl *getDecl() const {
148 assert(isDecl() && "Not a decl!");
149 return static_cast<Decl *>(Stored);
157 /// \brief The declarations and types to emit.
158 std::queue<DeclOrType> DeclTypesToEmit;
160 /// \brief The first ID number we can use for our own declarations.
161 serialization::DeclID FirstDeclID;
163 /// \brief The decl ID that will be assigned to the next new decl.
164 serialization::DeclID NextDeclID;
166 /// \brief Map that provides the ID numbers of each declaration within
167 /// the output stream, as well as those deserialized from a chained PCH.
169 /// The ID numbers of declarations are consecutive (in order of
170 /// discovery) and start at 2. 1 is reserved for the translation
171 /// unit, while 0 is reserved for NULL.
172 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
174 /// \brief Offset of each declaration in the bitstream, indexed by
175 /// the declaration's ID.
176 std::vector<serialization::DeclOffset> DeclOffsets;
178 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
179 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
181 struct DeclIDInFileInfo {
182 LocDeclIDsTy DeclIDs;
183 /// \brief Set when the DeclIDs vectors from all files are joined, this
184 /// indicates the index that this particular vector has in the global one.
185 unsigned FirstDeclIndex;
187 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
189 /// \brief Map from file SLocEntries to info about the file-level declarations
190 /// that it contains.
191 FileDeclIDsTy FileDeclIDs;
193 void associateDeclWithFile(const Decl *D, serialization::DeclID);
195 /// \brief The first ID number we can use for our own types.
196 serialization::TypeID FirstTypeID;
198 /// \brief The type ID that will be assigned to the next new type.
199 serialization::TypeID NextTypeID;
201 /// \brief Map that provides the ID numbers of each type within the
202 /// output stream, plus those deserialized from a chained PCH.
204 /// The ID numbers of types are consecutive (in order of discovery)
205 /// and start at 1. 0 is reserved for NULL. When types are actually
206 /// stored in the stream, the ID number is shifted by 2 bits to
207 /// allow for the const/volatile qualifiers.
209 /// Keys in the map never have const/volatile qualifiers.
212 /// \brief Offset of each type in the bitstream, indexed by
214 std::vector<uint32_t> TypeOffsets;
216 /// \brief The first ID number we can use for our own identifiers.
217 serialization::IdentID FirstIdentID;
219 /// \brief The identifier ID that will be assigned to the next new identifier.
220 serialization::IdentID NextIdentID;
222 /// \brief Map that provides the ID numbers of each identifier in
223 /// the output stream.
225 /// The ID numbers for identifiers are consecutive (in order of
226 /// discovery), starting at 1. An ID of zero refers to a NULL
228 llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
230 /// \brief The first ID number we can use for our own macros.
231 serialization::MacroID FirstMacroID;
233 /// \brief The identifier ID that will be assigned to the next new identifier.
234 serialization::MacroID NextMacroID;
236 /// \brief Map that provides the ID numbers of each macro.
237 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
239 struct MacroInfoToEmitData {
240 const IdentifierInfo *Name;
242 serialization::MacroID ID;
244 /// \brief The macro infos to emit.
245 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
247 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
249 /// @name FlushStmt Caches
252 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
253 llvm::DenseSet<Stmt *> ParentStmts;
255 /// \brief Offsets of sub-stmts already serialized. The offset points
256 /// just after the stmt record.
257 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
261 /// \brief Offsets of each of the identifier IDs into the identifier
263 std::vector<uint32_t> IdentifierOffsets;
265 /// \brief The first ID number we can use for our own submodules.
266 serialization::SubmoduleID FirstSubmoduleID;
268 /// \brief The submodule ID that will be assigned to the next new submodule.
269 serialization::SubmoduleID NextSubmoduleID;
271 /// \brief The first ID number we can use for our own selectors.
272 serialization::SelectorID FirstSelectorID;
274 /// \brief The selector ID that will be assigned to the next new selector.
275 serialization::SelectorID NextSelectorID;
277 /// \brief Map that provides the ID numbers of each Selector.
278 llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
280 /// \brief Offset of each selector within the method pool/selector
281 /// table, indexed by the Selector ID (-1).
282 std::vector<uint32_t> SelectorOffsets;
284 /// \brief Mapping from macro definitions (as they occur in the preprocessing
285 /// record) to the macro IDs.
286 llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID>
289 /// \brief Cache of indices of anonymous declarations within their lexical
291 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
293 /// An update to a Decl.
295 /// A DeclUpdateKind.
305 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
306 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
307 DeclUpdate(unsigned Kind, QualType Type)
308 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
309 DeclUpdate(unsigned Kind, SourceLocation Loc)
310 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
311 DeclUpdate(unsigned Kind, unsigned Val)
312 : Kind(Kind), Val(Val) {}
314 unsigned getKind() const { return Kind; }
315 const Decl *getDecl() const { return Dcl; }
316 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
317 SourceLocation getLoc() const {
318 return SourceLocation::getFromRawEncoding(Loc);
320 unsigned getNumber() const { return Val; }
323 typedef SmallVector<DeclUpdate, 1> UpdateRecord;
324 typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
325 /// \brief Mapping from declarations that came from a chained PCH to the
326 /// record containing modifications to them.
327 DeclUpdateMap DeclUpdates;
329 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
330 /// \brief Map of first declarations from a chained PCH that point to the
331 /// most recent declarations in another PCH.
332 FirstLatestDeclMap FirstLatestDecls;
334 /// \brief Declarations encountered that might be external
337 /// We keep track of external definitions and other 'interesting' declarations
338 /// as we are emitting declarations to the AST file. The AST file contains a
339 /// separate record for these declarations, which are provided to the AST
340 /// consumer by the AST reader. This is behavior is required to properly cope with,
341 /// e.g., tentative variable definitions that occur within
342 /// headers. The declarations themselves are stored as declaration
343 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
345 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
347 /// \brief DeclContexts that have received extensions since their serialized
350 /// For namespaces, when we're chaining and encountering a namespace, we check
351 /// if its primary namespace comes from the chain. If it does, we add the
352 /// primary to this set, so that we can write out lexical content updates for
354 llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
356 /// \brief Keeps track of visible decls that were added in DeclContexts
357 /// coming from another AST file.
358 SmallVector<const Decl *, 16> UpdatingVisibleDecls;
360 typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
361 /// \brief Decls that will be replaced in the current dependent AST file.
362 DeclsToRewriteTy DeclsToRewrite;
364 /// \brief The set of Objective-C class that have categories we
365 /// should serialize.
366 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
368 struct ReplacedDeclInfo {
369 serialization::DeclID ID;
373 ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
374 ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
376 : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
379 /// \brief Decls that have been replaced in the current dependent AST file.
381 /// When a decl changes fundamentally after being deserialized (this shouldn't
382 /// happen, but the ObjC AST nodes are designed this way), it will be
383 /// serialized again. In this case, it is registered here, so that the reader
384 /// knows to read the updated version.
385 SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
387 /// \brief The set of declarations that may have redeclaration chains that
388 /// need to be serialized.
389 llvm::SetVector<Decl *, SmallVector<Decl *, 4>,
390 llvm::SmallPtrSet<Decl *, 4> > Redeclarations;
392 /// \brief Statements that we've encountered while serializing a
393 /// declaration or type.
394 SmallVector<Stmt *, 16> StmtsToEmit;
396 /// \brief Statements collection to use for ASTWriter::AddStmt().
397 /// It will point to StmtsToEmit unless it is overriden.
398 SmallVector<Stmt *, 16> *CollectedStmts;
400 /// \brief Mapping from SwitchCase statements to IDs.
401 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
403 /// \brief The number of statements written to the AST file.
404 unsigned NumStatements;
406 /// \brief The number of macros written to the AST file.
409 /// \brief The number of lexical declcontexts written to the AST
411 unsigned NumLexicalDeclContexts;
413 /// \brief The number of visible declcontexts written to the AST
415 unsigned NumVisibleDeclContexts;
417 /// \brief The offset of each CXXBaseSpecifier set within the AST.
418 SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
420 /// \brief The first ID number we can use for our own base specifiers.
421 serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
423 /// \brief The base specifiers ID that will be assigned to the next new
424 /// set of C++ base specifiers.
425 serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
427 /// \brief A set of C++ base specifiers that is queued to be written into the
429 struct QueuedCXXBaseSpecifiers {
430 QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
432 QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
433 CXXBaseSpecifier const *Bases,
434 CXXBaseSpecifier const *BasesEnd)
435 : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
437 serialization::CXXBaseSpecifiersID ID;
438 CXXBaseSpecifier const * Bases;
439 CXXBaseSpecifier const * BasesEnd;
442 /// \brief Queue of C++ base specifiers to be written to the AST file,
443 /// in the order they should be written.
444 SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
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 Retrieve or create a submodule ID for this module.
451 unsigned getSubmoduleID(Module *Mod);
453 /// \brief Write the given subexpression to the bitstream.
454 void WriteSubStmt(Stmt *S,
455 llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
456 llvm::DenseSet<Stmt *> &ParentStmts);
458 void WriteBlockInfoBlock();
459 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
460 StringRef isysroot, const std::string &OutputFile);
461 void WriteInputFiles(SourceManager &SourceMgr,
462 HeaderSearchOptions &HSOpts,
464 void WriteSourceManagerBlock(SourceManager &SourceMgr,
465 const Preprocessor &PP);
466 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
467 void WriteHeaderSearch(const HeaderSearch &HS);
468 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
469 void WriteSubmodules(Module *WritingModule);
471 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
473 void WriteCXXBaseSpecifiersOffsets();
475 unsigned TypeExtQualAbbrev;
476 unsigned TypeFunctionProtoAbbrev;
477 void WriteTypeAbbrevs();
478 void WriteType(QualType T);
480 uint32_t GenerateNameLookupTable(const DeclContext *DC,
481 llvm::SmallVectorImpl<char> &LookupTable);
482 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
483 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
484 void WriteTypeDeclOffsets();
485 void WriteFileDeclIDsMap();
486 void WriteComments();
487 void WriteSelectors(Sema &SemaRef);
488 void WriteReferencedSelectorsPool(Sema &SemaRef);
489 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
491 void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record);
492 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
493 void WriteDeclReplacementsBlock();
494 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
495 void WriteFPPragmaOptions(const FPOptions &Opts);
496 void WriteOpenCLExtensions(Sema &SemaRef);
497 void WriteObjCCategories();
498 void WriteRedeclarations();
499 void WriteMergedDecls();
500 void WriteLateParsedTemplates(Sema &SemaRef);
501 void WriteOptimizePragmaOptions(Sema &SemaRef);
503 unsigned DeclParmVarAbbrev;
504 unsigned DeclContextLexicalAbbrev;
505 unsigned DeclContextVisibleLookupAbbrev;
506 unsigned UpdateVisibleAbbrev;
507 unsigned DeclRecordAbbrev;
508 unsigned DeclTypedefAbbrev;
509 unsigned DeclVarAbbrev;
510 unsigned DeclFieldAbbrev;
511 unsigned DeclEnumAbbrev;
512 unsigned DeclObjCIvarAbbrev;
513 unsigned DeclCXXMethodAbbrev;
515 unsigned DeclRefExprAbbrev;
516 unsigned CharacterLiteralAbbrev;
517 unsigned IntegerLiteralAbbrev;
518 unsigned ExprImplicitCastAbbrev;
520 void WriteDeclAbbrevs();
521 void WriteDecl(ASTContext &Context, Decl *D);
522 void AddFunctionDefinition(const FunctionDecl *FD, RecordData &Record);
524 void WriteASTCore(Sema &SemaRef,
525 StringRef isysroot, const std::string &OutputFile,
526 Module *WritingModule);
529 /// \brief Create a new precompiled header writer that outputs to
530 /// the given bitstream.
531 ASTWriter(llvm::BitstreamWriter &Stream);
534 /// \brief Write a precompiled header for the given semantic analysis.
536 /// \param SemaRef a reference to the semantic analysis object that processed
537 /// the AST to be written into the precompiled header.
539 /// \param WritingModule The module that we are writing. If null, we are
540 /// writing a precompiled header.
542 /// \param isysroot if non-empty, write a relocatable file whose headers
543 /// are relative to the given system root. If we're writing a module, its
544 /// build directory will be used in preference to this if both are available.
545 void WriteAST(Sema &SemaRef,
546 const std::string &OutputFile,
547 Module *WritingModule, StringRef isysroot,
548 bool hasErrors = false);
550 /// \brief Emit a token.
551 void AddToken(const Token &Tok, RecordDataImpl &Record);
553 /// \brief Emit a source location.
554 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
556 /// \brief Emit a source range.
557 void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
559 /// \brief Emit an integral value.
560 void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
562 /// \brief Emit a signed integral value.
563 void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
565 /// \brief Emit a floating-point value.
566 void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
568 /// \brief Emit a reference to an identifier.
569 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
571 /// \brief Emit a Selector (which is a smart pointer reference).
572 void AddSelectorRef(Selector, RecordDataImpl &Record);
574 /// \brief Emit a CXXTemporary.
575 void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
577 /// \brief Emit a set of C++ base specifiers to the record.
578 void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
579 CXXBaseSpecifier const *BasesEnd,
580 RecordDataImpl &Record);
582 /// \brief Get the unique number used to refer to the given selector.
583 serialization::SelectorID getSelectorRef(Selector Sel);
585 /// \brief Get the unique number used to refer to the given identifier.
586 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
588 /// \brief Get the unique number used to refer to the given macro.
589 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
591 /// \brief Determine the ID of an already-emitted macro.
592 serialization::MacroID getMacroID(MacroInfo *MI);
594 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
596 /// \brief Emit a reference to a type.
597 void AddTypeRef(QualType T, RecordDataImpl &Record);
599 /// \brief Force a type to be emitted and get its ID.
600 serialization::TypeID GetOrCreateTypeID(QualType T);
602 /// \brief Determine the type ID of an already-emitted type.
603 serialization::TypeID getTypeID(QualType T) const;
605 /// \brief Force a type to be emitted and get its index.
606 serialization::TypeIdx GetOrCreateTypeIdx( QualType T);
608 /// \brief Determine the type index of an already-emitted type.
609 serialization::TypeIdx getTypeIdx(QualType T) const;
611 /// \brief Emits a reference to a declarator info.
612 void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
614 /// \brief Emits a type with source-location information.
615 void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
617 /// \brief Emits a template argument location info.
618 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
619 const TemplateArgumentLocInfo &Arg,
620 RecordDataImpl &Record);
622 /// \brief Emits a template argument location.
623 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
624 RecordDataImpl &Record);
626 /// \brief Emits an AST template argument list info.
627 void AddASTTemplateArgumentListInfo(
628 const ASTTemplateArgumentListInfo *ASTTemplArgList,
629 RecordDataImpl &Record);
631 /// \brief Emit a reference to a declaration.
632 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
635 /// \brief Force a declaration to be emitted and get its ID.
636 serialization::DeclID GetDeclRef(const Decl *D);
638 /// \brief Determine the declaration ID of an already-emitted
640 serialization::DeclID getDeclID(const Decl *D);
642 /// \brief Emit a declaration name.
643 void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
644 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
645 DeclarationName Name, RecordDataImpl &Record);
646 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
647 RecordDataImpl &Record);
648 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
650 void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
652 /// \brief Emit a nested name specifier.
653 void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
655 /// \brief Emit a nested name specifier with source-location information.
656 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
657 RecordDataImpl &Record);
659 /// \brief Emit a template name.
660 void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
662 /// \brief Emit a template argument.
663 void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
665 /// \brief Emit a template parameter list.
666 void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
667 RecordDataImpl &Record);
669 /// \brief Emit a template argument list.
670 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
671 RecordDataImpl &Record);
673 /// \brief Emit a UnresolvedSet structure.
674 void AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record);
676 /// \brief Emit a C++ base specifier.
677 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
678 RecordDataImpl &Record);
680 /// \brief Emit a CXXCtorInitializer array.
681 void AddCXXCtorInitializers(
682 const CXXCtorInitializer * const *CtorInitializers,
683 unsigned NumCtorInitializers,
684 RecordDataImpl &Record);
686 void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
688 /// \brief Add a string to the given record.
689 void AddString(StringRef Str, RecordDataImpl &Record);
691 /// \brief Convert a path from this build process into one that is appropriate
692 /// for emission in the module file.
693 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
695 /// \brief Add a path to the given record.
696 void AddPath(StringRef Path, RecordDataImpl &Record);
698 /// \brief Emit the current record with the given path as a blob.
699 void EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record,
702 /// \brief Add a version tuple to the given record
703 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
705 /// \brief Mark a declaration context as needing an update.
706 void AddUpdatedDeclContext(const DeclContext *DC);
708 void RewriteDecl(const Decl *D) {
709 DeclsToRewrite.insert(D);
712 bool isRewritten(const Decl *D) const {
713 return DeclsToRewrite.count(D);
716 /// \brief Infer the submodule ID that contains an entity at the given
718 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
720 /// \brief Retrieve a submodule ID for this module.
721 /// Returns 0 If no ID has been associated with the module.
722 unsigned getExistingSubmoduleID(Module *Mod) const;
724 /// \brief Note that the identifier II occurs at the given offset
725 /// within the identifier table.
726 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
728 /// \brief Note that the selector Sel occurs at the given offset
729 /// within the method pool/selector table.
730 void SetSelectorOffset(Selector Sel, uint32_t Offset);
732 /// \brief Add the given statement or expression to the queue of
733 /// statements to emit.
735 /// This routine should be used when emitting types and declarations
736 /// that have expressions as part of their formulation. Once the
737 /// type or declaration has been written, call FlushStmts() to write
738 /// the corresponding statements just after the type or
740 void AddStmt(Stmt *S) {
741 CollectedStmts->push_back(S);
744 /// \brief Flush all of the statements and expressions that have
745 /// been added to the queue via AddStmt().
748 /// \brief Flush all of the C++ base specifier sets that have been added
749 /// via \c AddCXXBaseSpecifiersRef().
750 void FlushCXXBaseSpecifiers();
752 /// \brief Record an ID for the given switch-case statement.
753 unsigned RecordSwitchCaseID(SwitchCase *S);
755 /// \brief Retrieve the ID for the given switch-case statement.
756 unsigned getSwitchCaseID(SwitchCase *S);
758 void ClearSwitchCaseIDs();
760 unsigned getTypeExtQualAbbrev() const {
761 return TypeExtQualAbbrev;
763 unsigned getTypeFunctionProtoAbbrev() const {
764 return TypeFunctionProtoAbbrev;
767 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
768 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
769 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
770 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
771 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
772 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
773 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
774 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
776 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
777 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
778 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
779 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
781 bool hasChain() const { return Chain; }
783 // ASTDeserializationListener implementation
784 void ReaderInitialized(ASTReader *Reader) override;
785 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
786 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
787 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
788 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
789 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
790 MacroDefinition *MD) override;
791 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
793 // ASTMutationListener implementation.
794 void CompletedTagDefinition(const TagDecl *D) override;
795 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
796 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
797 void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
798 const ClassTemplateSpecializationDecl *D) override;
799 void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD,
800 const VarTemplateSpecializationDecl *D) override;
801 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
802 const FunctionDecl *D) override;
803 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
804 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
805 void CompletedImplicitDefinition(const FunctionDecl *D) override;
806 void StaticDataMemberInstantiated(const VarDecl *D) override;
807 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
808 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
809 const ObjCInterfaceDecl *IFD) override;
810 void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
811 const ObjCPropertyDecl *OrigProp,
812 const ObjCCategoryDecl *ClassExt) override;
813 void DeclarationMarkedUsed(const Decl *D) override;
814 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
817 /// \brief AST and semantic-analysis consumer that generates a
818 /// precompiled header from the parsed source code.
819 class PCHGenerator : public SemaConsumer {
820 const Preprocessor &PP;
821 std::string OutputFile;
822 clang::Module *Module;
823 std::string isysroot;
826 SmallVector<char, 128> Buffer;
827 llvm::BitstreamWriter Stream;
829 bool AllowASTWithErrors;
833 ASTWriter &getWriter() { return Writer; }
834 const ASTWriter &getWriter() const { return Writer; }
837 PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
838 clang::Module *Module,
839 StringRef isysroot, raw_ostream *Out,
840 bool AllowASTWithErrors = false);
842 void InitializeSema(Sema &S) override { SemaPtr = &S; }
843 void HandleTranslationUnit(ASTContext &Ctx) override;
844 ASTMutationListener *GetASTMutationListener() override;
845 ASTDeserializationListener *GetASTDeserializationListener() override;
847 bool hasEmittedPCH() const { return HasEmittedPCH; }
850 } // end namespace clang