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/TemplateBase.h"
20 #include "clang/Frontend/PCHContainerOperations.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/SmallVector.h"
29 #include "llvm/Bitcode/BitstreamWriter.h"
40 class DeclarationName;
43 class NestedNameSpecifier;
44 class CXXBaseSpecifier;
45 class CXXCtorInitializer;
49 class HeaderSearchOptions;
50 class IdentifierResolver;
51 class MacroDefinitionRecord;
54 class OpaqueValueExpr;
57 class MemoryBufferCache;
59 class ModuleFileExtension;
60 class ModuleFileExtensionWriter;
61 class PreprocessedEntity;
62 class PreprocessingRecord;
67 struct StoredDeclsList;
72 class ASTUnresolvedSet;
74 namespace SrcMgr { class SLocEntry; }
76 /// \brief Writes an AST file containing the contents of a translation unit.
78 /// The ASTWriter class produces a bitstream containing the serialized
79 /// representation of a given abstract syntax tree and its supporting
80 /// data structures. This bitstream can be de-serialized via an
81 /// instance of the ASTReader class.
82 class ASTWriter : public ASTDeserializationListener,
83 public ASTMutationListener {
85 typedef SmallVector<uint64_t, 64> RecordData;
86 typedef SmallVectorImpl<uint64_t> RecordDataImpl;
87 typedef ArrayRef<uint64_t> RecordDataRef;
89 friend class ASTDeclWriter;
90 friend class ASTStmtWriter;
91 friend class ASTTypeWriter;
92 friend class ASTRecordWriter;
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 /// The buffer associated with the bitstream.
111 const SmallVectorImpl<char> &Buffer;
113 /// \brief The PCM manager which manages memory buffers for pcm files.
114 MemoryBufferCache &PCMCache;
116 /// \brief The ASTContext we're writing.
117 ASTContext *Context = nullptr;
119 /// \brief The preprocessor we're writing.
120 Preprocessor *PP = nullptr;
122 /// \brief The reader of existing AST files, if we're chaining.
123 ASTReader *Chain = nullptr;
125 /// \brief The module we're currently writing, if any.
126 Module *WritingModule = nullptr;
128 /// \brief The base directory for any relative paths we emit.
129 std::string BaseDirectory;
131 /// \brief Indicates whether timestamps should be written to the produced
132 /// module file. This is the case for files implicitly written to the
133 /// module cache, where we need the timestamps to determine if the module
134 /// file is up to date, but not otherwise.
135 bool IncludeTimestamps;
137 /// \brief Indicates when the AST writing is actively performing
138 /// serialization, rather than just queueing updates.
139 bool WritingAST = false;
141 /// \brief Indicates that we are done serializing the collection of decls
142 /// and types to emit.
143 bool DoneWritingDeclsAndTypes = false;
145 /// \brief Indicates that the AST contained compiler errors.
146 bool ASTHasCompilerErrors = false;
148 /// \brief Mapping from input file entries to the index into the
149 /// offset table where information about that input file is stored.
150 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
152 /// \brief Stores a declaration or a type to be written to the AST file.
155 DeclOrType(Decl *D) : Stored(D), IsType(false) { }
156 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
158 bool isType() const { return IsType; }
159 bool isDecl() const { return !IsType; }
161 QualType getType() const {
162 assert(isType() && "Not a type!");
163 return QualType::getFromOpaquePtr(Stored);
166 Decl *getDecl() const {
167 assert(isDecl() && "Not a decl!");
168 return static_cast<Decl *>(Stored);
176 /// \brief The declarations and types to emit.
177 std::queue<DeclOrType> DeclTypesToEmit;
179 /// \brief The first ID number we can use for our own declarations.
180 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS;
182 /// \brief The decl ID that will be assigned to the next new decl.
183 serialization::DeclID NextDeclID = FirstDeclID;
185 /// \brief Map that provides the ID numbers of each declaration within
186 /// the output stream, as well as those deserialized from a chained PCH.
188 /// The ID numbers of declarations are consecutive (in order of
189 /// discovery) and start at 2. 1 is reserved for the translation
190 /// unit, while 0 is reserved for NULL.
191 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
193 /// \brief Offset of each declaration in the bitstream, indexed by
194 /// the declaration's ID.
195 std::vector<serialization::DeclOffset> DeclOffsets;
197 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
198 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
200 struct DeclIDInFileInfo {
201 LocDeclIDsTy DeclIDs;
202 /// \brief Set when the DeclIDs vectors from all files are joined, this
203 /// indicates the index that this particular vector has in the global one.
204 unsigned FirstDeclIndex;
206 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy;
208 /// \brief Map from file SLocEntries to info about the file-level declarations
209 /// that it contains.
210 FileDeclIDsTy FileDeclIDs;
212 void associateDeclWithFile(const Decl *D, serialization::DeclID);
214 /// \brief The first ID number we can use for our own types.
215 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS;
217 /// \brief The type ID that will be assigned to the next new type.
218 serialization::TypeID NextTypeID = FirstTypeID;
220 /// \brief Map that provides the ID numbers of each type within the
221 /// output stream, plus those deserialized from a chained PCH.
223 /// The ID numbers of types are consecutive (in order of discovery)
224 /// and start at 1. 0 is reserved for NULL. When types are actually
225 /// stored in the stream, the ID number is shifted by 2 bits to
226 /// allow for the const/volatile qualifiers.
228 /// Keys in the map never have const/volatile qualifiers.
231 /// \brief Offset of each type in the bitstream, indexed by
233 std::vector<uint32_t> TypeOffsets;
235 /// \brief The first ID number we can use for our own identifiers.
236 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS;
238 /// \brief The identifier ID that will be assigned to the next new identifier.
239 serialization::IdentID NextIdentID = FirstIdentID;
241 /// \brief Map that provides the ID numbers of each identifier in
242 /// the output stream.
244 /// The ID numbers for identifiers are consecutive (in order of
245 /// discovery), starting at 1. An ID of zero refers to a NULL
247 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
249 /// \brief The first ID number we can use for our own macros.
250 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS;
252 /// \brief The identifier ID that will be assigned to the next new identifier.
253 serialization::MacroID NextMacroID = FirstMacroID;
255 /// \brief Map that provides the ID numbers of each macro.
256 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
258 struct MacroInfoToEmitData {
259 const IdentifierInfo *Name;
261 serialization::MacroID ID;
263 /// \brief The macro infos to emit.
264 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
266 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
268 /// @name FlushStmt Caches
271 /// \brief Set of parent Stmts for the currently serializing sub-stmt.
272 llvm::DenseSet<Stmt *> ParentStmts;
274 /// \brief Offsets of sub-stmts already serialized. The offset points
275 /// just after the stmt record.
276 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
280 /// \brief Offsets of each of the identifier IDs into the identifier
282 std::vector<uint32_t> IdentifierOffsets;
284 /// \brief The first ID number we can use for our own submodules.
285 serialization::SubmoduleID FirstSubmoduleID =
286 serialization::NUM_PREDEF_SUBMODULE_IDS;
288 /// \brief The submodule ID that will be assigned to the next new submodule.
289 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID;
291 /// \brief The first ID number we can use for our own selectors.
292 serialization::SelectorID FirstSelectorID =
293 serialization::NUM_PREDEF_SELECTOR_IDS;
295 /// \brief The selector ID that will be assigned to the next new selector.
296 serialization::SelectorID NextSelectorID = FirstSelectorID;
298 /// \brief Map that provides the ID numbers of each Selector.
299 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
301 /// \brief Offset of each selector within the method pool/selector
302 /// table, indexed by the Selector ID (-1).
303 std::vector<uint32_t> SelectorOffsets;
305 /// \brief Mapping from macro definitions (as they occur in the preprocessing
306 /// record) to the macro IDs.
307 llvm::DenseMap<const MacroDefinitionRecord *,
308 serialization::PreprocessedEntityID> MacroDefinitions;
310 /// \brief Cache of indices of anonymous declarations within their lexical
312 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
314 /// An update to a Decl.
316 /// A DeclUpdateKind.
324 const Attr *Attribute;
328 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {}
329 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {}
330 DeclUpdate(unsigned Kind, QualType Type)
331 : Kind(Kind), Type(Type.getAsOpaquePtr()) {}
332 DeclUpdate(unsigned Kind, SourceLocation Loc)
333 : Kind(Kind), Loc(Loc.getRawEncoding()) {}
334 DeclUpdate(unsigned Kind, unsigned Val)
335 : Kind(Kind), Val(Val) {}
336 DeclUpdate(unsigned Kind, Module *M)
337 : Kind(Kind), Mod(M) {}
338 DeclUpdate(unsigned Kind, const Attr *Attribute)
339 : Kind(Kind), Attribute(Attribute) {}
341 unsigned getKind() const { return Kind; }
342 const Decl *getDecl() const { return Dcl; }
343 QualType getType() const { return QualType::getFromOpaquePtr(Type); }
344 SourceLocation getLoc() const {
345 return SourceLocation::getFromRawEncoding(Loc);
347 unsigned getNumber() const { return Val; }
348 Module *getModule() const { return Mod; }
349 const Attr *getAttr() const { return Attribute; }
352 typedef SmallVector<DeclUpdate, 1> UpdateRecord;
353 typedef llvm::MapVector<const Decl *, UpdateRecord> DeclUpdateMap;
354 /// \brief Mapping from declarations that came from a chained PCH to the
355 /// record containing modifications to them.
356 DeclUpdateMap DeclUpdates;
358 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
359 /// \brief Map of first declarations from a chained PCH that point to the
360 /// most recent declarations in another PCH.
361 FirstLatestDeclMap FirstLatestDecls;
363 /// \brief Declarations encountered that might be external
366 /// We keep track of external definitions and other 'interesting' declarations
367 /// as we are emitting declarations to the AST file. The AST file contains a
368 /// separate record for these declarations, which are provided to the AST
369 /// consumer by the AST reader. This is behavior is required to properly cope with,
370 /// e.g., tentative variable definitions that occur within
371 /// headers. The declarations themselves are stored as declaration
372 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS
374 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
375 SmallVector<uint64_t, 16> ModularCodegenDecls;
377 /// \brief DeclContexts that have received extensions since their serialized
380 /// For namespaces, when we're chaining and encountering a namespace, we check
381 /// if its primary namespace comes from the chain. If it does, we add the
382 /// primary to this set, so that we can write out lexical content updates for
384 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts;
386 /// \brief Keeps track of declarations that we must emit, even though we're
387 /// not guaranteed to be able to find them by walking the AST starting at the
388 /// translation unit.
389 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
391 /// \brief The set of Objective-C class that have categories we
392 /// should serialize.
393 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
395 /// \brief The set of declarations that may have redeclaration chains that
396 /// need to be serialized.
397 llvm::SmallVector<const Decl *, 16> Redeclarations;
399 /// \brief A cache of the first local declaration for "interesting"
400 /// redeclaration chains.
401 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
403 /// \brief Mapping from SwitchCase statements to IDs.
404 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
406 /// \brief The number of statements written to the AST file.
407 unsigned NumStatements = 0;
409 /// \brief The number of macros written to the AST file.
410 unsigned NumMacros = 0;
412 /// \brief The number of lexical declcontexts written to the AST
414 unsigned NumLexicalDeclContexts = 0;
416 /// \brief The number of visible declcontexts written to the AST
418 unsigned NumVisibleDeclContexts = 0;
420 /// \brief A mapping from each known submodule to its ID number, which will
421 /// be a positive integer.
422 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
424 /// \brief A list of the module file extension writers.
425 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
426 ModuleFileExtensionWriters;
428 /// \brief Retrieve or create a submodule ID for this module.
429 unsigned getSubmoduleID(Module *Mod);
431 /// \brief Write the given subexpression to the bitstream.
432 void WriteSubStmt(Stmt *S);
434 void WriteBlockInfoBlock();
435 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
436 StringRef isysroot, const std::string &OutputFile);
438 /// Write out the signature and diagnostic options, and return the signature.
439 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
440 ASTContext &Context);
442 /// Calculate hash of the pcm content.
443 static ASTFileSignature createSignature(StringRef Bytes);
445 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
447 void WriteSourceManagerBlock(SourceManager &SourceMgr,
448 const Preprocessor &PP);
449 void WritePreprocessor(const Preprocessor &PP, bool IsModule);
450 void WriteHeaderSearch(const HeaderSearch &HS);
451 void WritePreprocessorDetail(PreprocessingRecord &PPRec);
452 void WriteSubmodules(Module *WritingModule);
454 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
457 unsigned TypeExtQualAbbrev = 0;
458 unsigned TypeFunctionProtoAbbrev = 0;
459 void WriteTypeAbbrevs();
460 void WriteType(QualType T);
462 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
463 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
465 void GenerateNameLookupTable(const DeclContext *DC,
466 llvm::SmallVectorImpl<char> &LookupTable);
467 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
468 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
469 void WriteTypeDeclOffsets();
470 void WriteFileDeclIDsMap();
471 void WriteComments();
472 void WriteSelectors(Sema &SemaRef);
473 void WriteReferencedSelectorsPool(Sema &SemaRef);
474 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
476 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord);
477 void WriteDeclContextVisibleUpdate(const DeclContext *DC);
478 void WriteFPPragmaOptions(const FPOptions &Opts);
479 void WriteOpenCLExtensions(Sema &SemaRef);
480 void WriteOpenCLExtensionTypes(Sema &SemaRef);
481 void WriteOpenCLExtensionDecls(Sema &SemaRef);
482 void WriteCUDAPragmas(Sema &SemaRef);
483 void WriteObjCCategories();
484 void WriteLateParsedTemplates(Sema &SemaRef);
485 void WriteOptimizePragmaOptions(Sema &SemaRef);
486 void WriteMSStructPragmaOptions(Sema &SemaRef);
487 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
488 void WritePackPragmaOptions(Sema &SemaRef);
489 void WriteModuleFileExtension(Sema &SemaRef,
490 ModuleFileExtensionWriter &Writer);
492 unsigned DeclParmVarAbbrev = 0;
493 unsigned DeclContextLexicalAbbrev = 0;
494 unsigned DeclContextVisibleLookupAbbrev = 0;
495 unsigned UpdateVisibleAbbrev = 0;
496 unsigned DeclRecordAbbrev = 0;
497 unsigned DeclTypedefAbbrev = 0;
498 unsigned DeclVarAbbrev = 0;
499 unsigned DeclFieldAbbrev = 0;
500 unsigned DeclEnumAbbrev = 0;
501 unsigned DeclObjCIvarAbbrev = 0;
502 unsigned DeclCXXMethodAbbrev = 0;
504 unsigned DeclRefExprAbbrev = 0;
505 unsigned CharacterLiteralAbbrev = 0;
506 unsigned IntegerLiteralAbbrev = 0;
507 unsigned ExprImplicitCastAbbrev = 0;
509 void WriteDeclAbbrevs();
510 void WriteDecl(ASTContext &Context, Decl *D);
512 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
513 const std::string &OutputFile,
514 Module *WritingModule);
517 /// \brief Create a new precompiled header writer that outputs to
518 /// the given bitstream.
519 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
520 MemoryBufferCache &PCMCache,
521 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
522 bool IncludeTimestamps = true);
523 ~ASTWriter() override;
525 const LangOptions &getLangOpts() const;
527 /// \brief Get a timestamp for output into the AST file. The actual timestamp
528 /// of the specified file may be ignored if we have been instructed to not
529 /// include timestamps in the output file.
530 time_t getTimestampForOutput(const FileEntry *E) const;
532 /// \brief Write a precompiled header for the given semantic analysis.
534 /// \param SemaRef a reference to the semantic analysis object that processed
535 /// the AST to be written into the precompiled header.
537 /// \param WritingModule The module that we are writing. If null, we are
538 /// writing a precompiled header.
540 /// \param isysroot if non-empty, write a relocatable file whose headers
541 /// are relative to the given system root. If we're writing a module, its
542 /// build directory will be used in preference to this if both are available.
544 /// \return the module signature, which eventually will be a hash of
545 /// the module but currently is merely a random 32-bit number.
546 ASTFileSignature WriteAST(Sema &SemaRef, 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 a reference to an identifier.
560 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
562 /// \brief Get the unique number used to refer to the given selector.
563 serialization::SelectorID getSelectorRef(Selector Sel);
565 /// \brief Get the unique number used to refer to the given identifier.
566 serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
568 /// \brief Get the unique number used to refer to the given macro.
569 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name);
571 /// \brief Determine the ID of an already-emitted macro.
572 serialization::MacroID getMacroID(MacroInfo *MI);
574 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name);
576 /// \brief Emit a reference to a type.
577 void AddTypeRef(QualType T, RecordDataImpl &Record);
579 /// \brief Force a type to be emitted and get its ID.
580 serialization::TypeID GetOrCreateTypeID(QualType T);
582 /// \brief Determine the type ID of an already-emitted type.
583 serialization::TypeID getTypeID(QualType T) const;
585 /// \brief Find the first local declaration of a given local redeclarable
587 const Decl *getFirstLocalDecl(const Decl *D);
589 /// \brief Is this a local declaration (that is, one that will be written to
590 /// our AST file)? This is the case for declarations that are neither imported
591 /// from another AST file nor predefined.
592 bool IsLocalDecl(const Decl *D) {
593 if (D->isFromASTFile())
595 auto I = DeclIDs.find(D);
596 return (I == DeclIDs.end() ||
597 I->second >= serialization::NUM_PREDEF_DECL_IDS);
600 /// \brief Emit a reference to a declaration.
601 void AddDeclRef(const Decl *D, RecordDataImpl &Record);
604 /// \brief Force a declaration to be emitted and get its ID.
605 serialization::DeclID GetDeclRef(const Decl *D);
607 /// \brief Determine the declaration ID of an already-emitted
609 serialization::DeclID getDeclID(const Decl *D);
611 unsigned getAnonymousDeclarationNumber(const NamedDecl *D);
613 /// \brief Add a string to the given record.
614 void AddString(StringRef Str, RecordDataImpl &Record);
616 /// \brief Convert a path from this build process into one that is appropriate
617 /// for emission in the module file.
618 bool PreparePathForOutput(SmallVectorImpl<char> &Path);
620 /// \brief Add a path to the given record.
621 void AddPath(StringRef Path, RecordDataImpl &Record);
623 /// \brief Emit the current record with the given path as a blob.
624 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
627 /// \brief Add a version tuple to the given record
628 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
630 /// \brief Infer the submodule ID that contains an entity at the given
632 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
634 /// \brief Retrieve or create a submodule ID for this module, or return 0 if
635 /// the submodule is neither local (a submodle of the currently-written module)
636 /// nor from an imported module.
637 unsigned getLocalOrImportedSubmoduleID(Module *Mod);
639 /// \brief Note that the identifier II occurs at the given offset
640 /// within the identifier table.
641 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
643 /// \brief Note that the selector Sel occurs at the given offset
644 /// within the method pool/selector table.
645 void SetSelectorOffset(Selector Sel, uint32_t Offset);
647 /// \brief Record an ID for the given switch-case statement.
648 unsigned RecordSwitchCaseID(SwitchCase *S);
650 /// \brief Retrieve the ID for the given switch-case statement.
651 unsigned getSwitchCaseID(SwitchCase *S);
653 void ClearSwitchCaseIDs();
655 unsigned getTypeExtQualAbbrev() const {
656 return TypeExtQualAbbrev;
658 unsigned getTypeFunctionProtoAbbrev() const {
659 return TypeFunctionProtoAbbrev;
662 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
663 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
664 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
665 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
666 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
667 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
668 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
669 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; }
671 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
672 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
673 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
674 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; }
676 bool hasChain() const { return Chain; }
677 ASTReader *getChain() const { return Chain; }
680 // ASTDeserializationListener implementation
681 void ReaderInitialized(ASTReader *Reader) override;
682 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override;
683 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override;
684 void TypeRead(serialization::TypeIdx Idx, QualType T) override;
685 void SelectorRead(serialization::SelectorID ID, Selector Sel) override;
686 void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
687 MacroDefinitionRecord *MD) override;
688 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override;
690 // ASTMutationListener implementation.
691 void CompletedTagDefinition(const TagDecl *D) override;
692 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override;
693 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override;
694 void AddedCXXTemplateSpecialization(
695 const ClassTemplateDecl *TD,
696 const ClassTemplateSpecializationDecl *D) override;
697 void AddedCXXTemplateSpecialization(
698 const VarTemplateDecl *TD,
699 const VarTemplateSpecializationDecl *D) override;
700 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
701 const FunctionDecl *D) override;
702 void ResolvedExceptionSpec(const FunctionDecl *FD) override;
703 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override;
704 void ResolvedOperatorDelete(const CXXDestructorDecl *DD,
705 const FunctionDecl *Delete) override;
706 void CompletedImplicitDefinition(const FunctionDecl *D) override;
707 void StaticDataMemberInstantiated(const VarDecl *D) override;
708 void DefaultArgumentInstantiated(const ParmVarDecl *D) override;
709 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override;
710 void FunctionDefinitionInstantiated(const FunctionDecl *D) override;
711 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
712 const ObjCInterfaceDecl *IFD) override;
713 void DeclarationMarkedUsed(const Decl *D) override;
714 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override;
715 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D,
716 const Attr *Attr) override;
717 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override;
718 void AddedAttributeToRecord(const Attr *Attr,
719 const RecordDecl *Record) override;
722 /// \brief An object for streaming information to a record.
723 class ASTRecordWriter {
725 ASTWriter::RecordDataImpl *Record;
727 /// \brief Statements that we've encountered while serializing a
728 /// declaration or type.
729 SmallVector<Stmt *, 16> StmtsToEmit;
731 /// \brief Indices of record elements that describe offsets within the
732 /// bitcode. These will be converted to offsets relative to the current
733 /// record when emitted.
734 SmallVector<unsigned, 8> OffsetIndices;
736 /// \brief Flush all of the statements and expressions that have
737 /// been added to the queue via AddStmt().
739 void FlushSubStmts();
741 void PrepareToEmit(uint64_t MyOffset) {
742 // Convert offsets into relative form.
743 for (unsigned I : OffsetIndices) {
744 auto &StoredOffset = (*Record)[I];
745 assert(StoredOffset < MyOffset && "invalid offset");
747 StoredOffset = MyOffset - StoredOffset;
749 OffsetIndices.clear();
753 /// Construct a ASTRecordWriter that uses the default encoding scheme.
754 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
755 : Writer(&Writer), Record(&Record) {}
757 /// Construct a ASTRecordWriter that uses the same encoding scheme as another
759 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
760 : Writer(Parent.Writer), Record(&Record) {}
762 /// Copying an ASTRecordWriter is almost certainly a bug.
763 ASTRecordWriter(const ASTRecordWriter&) = delete;
764 void operator=(const ASTRecordWriter&) = delete;
766 /// \brief Extract the underlying record storage.
767 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; }
769 /// \brief Minimal vector-like interface.
771 void push_back(uint64_t N) { Record->push_back(N); }
772 template<typename InputIterator>
773 void append(InputIterator begin, InputIterator end) {
774 Record->append(begin, end);
776 bool empty() const { return Record->empty(); }
777 size_t size() const { return Record->size(); }
778 uint64_t &operator[](size_t N) { return (*Record)[N]; }
781 /// \brief Emit the record to the stream, followed by its substatements, and
782 /// return its offset.
783 // FIXME: Allow record producers to suggest Abbrevs.
784 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) {
785 uint64_t Offset = Writer->Stream.GetCurrentBitNo();
786 PrepareToEmit(Offset);
787 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
792 /// \brief Emit the record to the stream, preceded by its substatements.
793 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) {
795 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
796 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
797 return Writer->Stream.GetCurrentBitNo();
800 /// \brief Add a bit offset into the record. This will be converted into an
801 /// offset relative to the current record when emitted.
802 void AddOffset(uint64_t BitOffset) {
803 OffsetIndices.push_back(Record->size());
804 Record->push_back(BitOffset);
807 /// \brief Add the given statement or expression to the queue of
808 /// statements to emit.
810 /// This routine should be used when emitting types and declarations
811 /// that have expressions as part of their formulation. Once the
812 /// type or declaration has been written, Emit() will write
813 /// the corresponding statements just after the record.
814 void AddStmt(Stmt *S) {
815 StmtsToEmit.push_back(S);
818 /// \brief Add a definition for the given function to the queue of statements
820 void AddFunctionDefinition(const FunctionDecl *FD);
822 /// \brief Emit a source location.
823 void AddSourceLocation(SourceLocation Loc) {
824 return Writer->AddSourceLocation(Loc, *Record);
827 /// \brief Emit a source range.
828 void AddSourceRange(SourceRange Range) {
829 return Writer->AddSourceRange(Range, *Record);
832 /// \brief Emit an integral value.
833 void AddAPInt(const llvm::APInt &Value);
835 /// \brief Emit a signed integral value.
836 void AddAPSInt(const llvm::APSInt &Value);
838 /// \brief Emit a floating-point value.
839 void AddAPFloat(const llvm::APFloat &Value);
841 /// \brief Emit a reference to an identifier.
842 void AddIdentifierRef(const IdentifierInfo *II) {
843 return Writer->AddIdentifierRef(II, *Record);
846 /// \brief Emit a Selector (which is a smart pointer reference).
847 void AddSelectorRef(Selector S);
849 /// \brief Emit a CXXTemporary.
850 void AddCXXTemporary(const CXXTemporary *Temp);
852 /// \brief Emit a C++ base specifier.
853 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base);
855 /// \brief Emit a set of C++ base specifiers.
856 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases);
858 /// \brief Emit a reference to a type.
859 void AddTypeRef(QualType T) {
860 return Writer->AddTypeRef(T, *Record);
863 /// \brief Emits a reference to a declarator info.
864 void AddTypeSourceInfo(TypeSourceInfo *TInfo);
866 /// \brief Emits a type with source-location information.
867 void AddTypeLoc(TypeLoc TL);
869 /// \brief Emits a template argument location info.
870 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
871 const TemplateArgumentLocInfo &Arg);
873 /// \brief Emits a template argument location.
874 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg);
876 /// \brief Emits an AST template argument list info.
877 void AddASTTemplateArgumentListInfo(
878 const ASTTemplateArgumentListInfo *ASTTemplArgList);
880 /// \brief Emit a reference to a declaration.
881 void AddDeclRef(const Decl *D) {
882 return Writer->AddDeclRef(D, *Record);
885 /// \brief Emit a declaration name.
886 void AddDeclarationName(DeclarationName Name);
888 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
889 DeclarationName Name);
890 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo);
892 void AddQualifierInfo(const QualifierInfo &Info);
894 /// \brief Emit a nested name specifier.
895 void AddNestedNameSpecifier(NestedNameSpecifier *NNS);
897 /// \brief Emit a nested name specifier with source-location information.
898 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS);
900 /// \brief Emit a template name.
901 void AddTemplateName(TemplateName Name);
903 /// \brief Emit a template argument.
904 void AddTemplateArgument(const TemplateArgument &Arg);
906 /// \brief Emit a template parameter list.
907 void AddTemplateParameterList(const TemplateParameterList *TemplateParams);
909 /// \brief Emit a template argument list.
910 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs);
912 /// \brief Emit a UnresolvedSet structure.
913 void AddUnresolvedSet(const ASTUnresolvedSet &Set);
915 /// \brief Emit a CXXCtorInitializer array.
916 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer*> CtorInits);
918 void AddCXXDefinitionData(const CXXRecordDecl *D);
920 /// \brief Emit a string.
921 void AddString(StringRef Str) {
922 return Writer->AddString(Str, *Record);
925 /// \brief Emit a path.
926 void AddPath(StringRef Path) {
927 return Writer->AddPath(Path, *Record);
930 /// \brief Emit a version tuple.
931 void AddVersionTuple(const VersionTuple &Version) {
932 return Writer->AddVersionTuple(Version, *Record);
935 /// \brief Emit a list of attributes.
936 void AddAttributes(ArrayRef<const Attr*> Attrs);
939 /// \brief AST and semantic-analysis consumer that generates a
940 /// precompiled header from the parsed source code.
941 class PCHGenerator : public SemaConsumer {
942 const Preprocessor &PP;
943 std::string OutputFile;
944 std::string isysroot;
946 std::shared_ptr<PCHBuffer> Buffer;
947 llvm::BitstreamWriter Stream;
949 bool AllowASTWithErrors;
952 ASTWriter &getWriter() { return Writer; }
953 const ASTWriter &getWriter() const { return Writer; }
954 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; }
957 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot,
958 std::shared_ptr<PCHBuffer> Buffer,
959 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
960 bool AllowASTWithErrors = false, bool IncludeTimestamps = true);
961 ~PCHGenerator() override;
962 void InitializeSema(Sema &S) override { SemaPtr = &S; }
963 void HandleTranslationUnit(ASTContext &Ctx) override;
964 ASTMutationListener *GetASTMutationListener() override;
965 ASTDeserializationListener *GetASTDeserializationListener() override;
966 bool hasEmittedPCH() const { return Buffer->IsComplete; }
969 } // end namespace clang