Go to the documentation of this file.
14 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
15 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
27 #include "llvm/ADT/ArrayRef.h"
28 #include "llvm/ADT/DenseMap.h"
29 #include "llvm/ADT/DenseSet.h"
30 #include "llvm/ADT/MapVector.h"
31 #include "llvm/ADT/STLExtras.h"
32 #include "llvm/ADT/SetVector.h"
33 #include "llvm/ADT/SmallVector.h"
34 #include "llvm/ADT/StringRef.h"
35 #include "llvm/Bitstream/BitstreamWriter.h"
58 class ASTUnresolvedSet;
60 class CXXBaseSpecifier;
61 class CXXCtorInitializer;
66 class FPOptionsOverride;
69 class HeaderSearchOptions;
70 class IdentifierResolver;
72 class MacroDefinitionRecord;
75 class InMemoryModuleCache;
76 class ModuleFileExtension;
77 class ModuleFileExtensionWriter;
79 class ObjCInterfaceDecl;
80 class PreprocessingRecord;
87 class StoredDeclsList;
89 class TemplateParameterList;
123 llvm::BitstreamWriter &Stream;
141 Module *WritingModule =
nullptr;
144 uint64_t ASTBlockStartOffset = 0;
147 std::pair<uint64_t, uint64_t> ASTBlockRange;
156 bool IncludeTimestamps;
160 bool WritingAST =
false;
164 bool DoneWritingDeclsAndTypes =
false;
167 bool ASTHasCompilerErrors =
false;
171 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
176 DeclOrType(
Decl *D) : Stored(D), IsType(
false) {}
179 bool isType()
const {
return IsType; }
180 bool isDecl()
const {
return !IsType; }
183 assert(isType() &&
"Not a type!");
187 Decl *getDecl()
const {
188 assert(isDecl() &&
"Not a decl!");
189 return static_cast<Decl *
>(Stored);
198 std::queue<DeclOrType> DeclTypesToEmit;
212 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
216 std::vector<serialization::DeclOffset> DeclOffsets;
220 uint64_t DeclTypesBlockStartOffset = 0;
225 struct DeclIDInFileInfo {
226 LocDeclIDsTy DeclIDs;
230 unsigned FirstDeclIndex;
232 using FileDeclIDsTy =
233 llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
237 FileDeclIDsTy FileDeclIDs;
260 std::vector<serialization::UnderalignedInt64> TypeOffsets;
274 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
283 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
285 struct MacroInfoToEmitData {
286 const IdentifierInfo *Name;
292 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
294 llvm::DenseMap<const IdentifierInfo *, uint32_t>
295 IdentMacroDirectivesOffsetMap;
305 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
311 std::vector<uint32_t> IdentifierOffsets;
328 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
332 std::vector<uint32_t> SelectorOffsets;
336 llvm::DenseMap<
const MacroDefinitionRecord *,
341 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
353 const Attr *Attribute;
358 DeclUpdate(
unsigned Kind,
const Decl *Dcl) :
Kind(
Kind), Dcl(Dcl) {}
359 DeclUpdate(
unsigned Kind, QualType Type)
361 DeclUpdate(
unsigned Kind, SourceLocation Loc)
362 :
Kind(
Kind), Loc(Loc.getRawEncoding()) {}
363 DeclUpdate(
unsigned Kind,
unsigned Val) :
Kind(
Kind), Val(Val) {}
364 DeclUpdate(
unsigned Kind, Module *M) :
Kind(
Kind), Mod(M) {}
365 DeclUpdate(
unsigned Kind,
const Attr *Attribute)
366 :
Kind(
Kind), Attribute(Attribute) {}
369 const Decl *getDecl()
const {
return Dcl; }
372 SourceLocation getLoc()
const {
376 unsigned getNumber()
const {
return Val; }
377 Module *getModule()
const {
return Mod; }
378 const Attr *getAttr()
const {
return Attribute; }
381 using UpdateRecord = SmallVector<DeclUpdate, 1>;
382 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
386 DeclUpdateMap DeclUpdates;
388 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
392 FirstLatestDeclMap FirstLatestDecls;
405 SmallVector<uint64_t, 16> EagerlyDeserializedDecls;
406 SmallVector<uint64_t, 16> ModularCodegenDecls;
420 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
424 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
432 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
435 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
438 unsigned NumStatements = 0;
441 unsigned NumMacros = 0;
445 unsigned NumLexicalDeclContexts = 0;
449 unsigned NumVisibleDeclContexts = 0;
453 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
456 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
457 ModuleFileExtensionWriters;
460 unsigned getSubmoduleID(Module *Mod);
463 void WriteSubStmt(Stmt *S);
465 void WriteBlockInfoBlock();
466 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
467 StringRef isysroot,
const std::string &OutputFile);
470 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
471 ASTContext &Context);
474 static std::pair<ASTFileSignature, ASTFileSignature>
475 createSignature(StringRef AllBytes, StringRef ASTBlockBytes);
477 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts,
479 void WriteSourceManagerBlock(SourceManager &SourceMgr,
480 const Preprocessor &PP);
481 void WritePreprocessor(
const Preprocessor &PP,
bool IsModule);
482 void WriteHeaderSearch(
const HeaderSearch &HS);
483 void WritePreprocessorDetail(PreprocessingRecord &PPRec,
484 uint64_t MacroOffsetsBase);
485 void WriteSubmodules(Module *WritingModule);
487 void WritePragmaDiagnosticMappings(
const DiagnosticsEngine &
Diag,
490 unsigned TypeExtQualAbbrev = 0;
491 unsigned TypeFunctionProtoAbbrev = 0;
492 void WriteTypeAbbrevs();
493 void WriteType(QualType T);
495 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC);
496 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC);
498 void GenerateNameLookupTable(
const DeclContext *DC,
500 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
501 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
502 void WriteTypeDeclOffsets();
503 void WriteFileDeclIDsMap();
504 void WriteComments();
505 void WriteSelectors(Sema &SemaRef);
506 void WriteReferencedSelectorsPool(Sema &SemaRef);
507 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
510 void WriteDeclContextVisibleUpdate(
const DeclContext *DC);
511 void WriteFPPragmaOptions(
const FPOptionsOverride &Opts);
512 void WriteOpenCLExtensions(Sema &SemaRef);
513 void WriteOpenCLExtensionTypes(Sema &SemaRef);
514 void WriteOpenCLExtensionDecls(Sema &SemaRef);
515 void WriteCUDAPragmas(Sema &SemaRef);
516 void WriteObjCCategories();
517 void WriteLateParsedTemplates(Sema &SemaRef);
518 void WriteOptimizePragmaOptions(Sema &SemaRef);
519 void WriteMSStructPragmaOptions(Sema &SemaRef);
520 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
521 void WritePackPragmaOptions(Sema &SemaRef);
522 void WriteFloatControlPragmaOptions(Sema &SemaRef);
523 void WriteModuleFileExtension(Sema &SemaRef,
524 ModuleFileExtensionWriter &Writer);
526 unsigned DeclParmVarAbbrev = 0;
527 unsigned DeclContextLexicalAbbrev = 0;
528 unsigned DeclContextVisibleLookupAbbrev = 0;
529 unsigned UpdateVisibleAbbrev = 0;
530 unsigned DeclRecordAbbrev = 0;
531 unsigned DeclTypedefAbbrev = 0;
532 unsigned DeclVarAbbrev = 0;
533 unsigned DeclFieldAbbrev = 0;
534 unsigned DeclEnumAbbrev = 0;
535 unsigned DeclObjCIvarAbbrev = 0;
536 unsigned DeclCXXMethodAbbrev = 0;
538 unsigned DeclRefExprAbbrev = 0;
539 unsigned CharacterLiteralAbbrev = 0;
540 unsigned IntegerLiteralAbbrev = 0;
541 unsigned ExprImplicitCastAbbrev = 0;
543 void WriteDeclAbbrevs();
544 void WriteDecl(ASTContext &Context, Decl *D);
546 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
548 Module *WritingModule);
553 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
554 InMemoryModuleCache &ModuleCache,
555 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
556 bool IncludeTimestamps =
true);
560 assert(Context &&
"requested AST context when not writing AST");
586 Module *WritingModule, StringRef isysroot,
587 bool hasErrors =
false,
588 bool ShouldCacheASTInMemory =
false);
639 auto I = DeclIDs.find(D);
640 return (I == DeclIDs.end() ||
695 return TypeExtQualAbbrev;
699 return TypeFunctionProtoAbbrev;
721 void ReaderInitialized(
ASTReader *Reader)
override;
731 void CompletedTagDefinition(
const TagDecl *D)
override;
734 void AddedCXXTemplateSpecialization(
737 void AddedCXXTemplateSpecialization(
742 void ResolvedExceptionSpec(
const FunctionDecl *FD)
override;
746 Expr *ThisArg)
override;
747 void CompletedImplicitDefinition(
const FunctionDecl *D)
override;
748 void InstantiationRequested(
const ValueDecl *D)
override;
749 void VariableDefinitionInstantiated(
const VarDecl *D)
override;
750 void FunctionDefinitionInstantiated(
const FunctionDecl *D)
override;
751 void DefaultArgumentInstantiated(
const ParmVarDecl *D)
override;
752 void DefaultMemberInitializerInstantiated(
const FieldDecl *D)
override;
755 void DeclarationMarkedUsed(
const Decl *D)
override;
756 void DeclarationMarkedOpenMPThreadPrivate(
const Decl *D)
override;
757 void DeclarationMarkedOpenMPDeclareTarget(
const Decl *D,
759 void DeclarationMarkedOpenMPAllocate(
const Decl *D,
const Attr *A)
override;
760 void RedefinedHiddenDefinition(
const NamedDecl *D,
Module *M)
override;
761 void AddedAttributeToRecord(
const Attr *
Attr,
772 std::shared_ptr<PCHBuffer> Buffer;
773 llvm::BitstreamWriter Stream;
775 bool AllowASTWithErrors;
776 bool ShouldCacheASTInMemory;
785 StringRef OutputFile, StringRef isysroot,
786 std::shared_ptr<PCHBuffer> Buffer,
787 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
788 bool AllowASTWithErrors =
false,
bool IncludeTimestamps =
true,
789 bool ShouldCacheASTInMemory =
false);
801 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H
unsigned getCharacterLiteralAbbrev() const
Represents an ObjC class declaration.
Specialize PointerLikeTypeTraits to allow LazyGenerationalUpdatePtr to be placed into a PointerUnion.
unsigned getDeclRefExprAbbrev() const
A type index; the type ID with the qualifier bits removed.
const unsigned int NUM_PREDEF_SELECTOR_IDS
The number of predefined selector IDs.
A trivial tuple used to represent a source range.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
unsigned getSwitchCaseID(SwitchCase *S)
Retrieve the ID for the given switch-case statement.
static DiagnosticBuilder Diag(DiagnosticsEngine *Diags, const LangOptions &Features, FullSourceLoc TokLoc, const char *TokBegin, const char *TokRangeBegin, const char *TokRangeEnd, unsigned DiagID)
Produce a diagnostic highlighting some portion of a literal.
Encodes a location in the source.
This represents a decl that may have a name.
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
const Decl * getFirstLocalDecl(const Decl *D)
Find the first local declaration of a given local redeclarable decl.
A (possibly-)qualified type.
Represents a member of a struct/union/class.
Represents a parameter to a function.
bool PreparePathForOutput(SmallVectorImpl< char > &Path)
Convert a path from this build process into one that is appropriate for emission in the module file.
Encapsulates the data about a macro definition (e.g.
serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name)
Get the unique number used to refer to the given macro.
Represents a variable template specialization, which refers to a variable template with a given set o...
Token - This structure provides full information about a lexed token.
bool IsLocalDecl(const Decl *D)
Is this a local declaration (that is, one that will be written to our AST file)? This is the case for...
uint32_t MacroID
An ID number that refers to a macro in an AST file.
const ASTWriter & getWriter() const
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
unsigned getDeclVarAbbrev() const
Declaration of a template function.
unsigned getTypeFunctionProtoAbbrev() const
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
An abstract interface that should be implemented by clients that read ASTs and then require further s...
PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, StringRef OutputFile, StringRef isysroot, std::shared_ptr< PCHBuffer > Buffer, ArrayRef< std::shared_ptr< ModuleFileExtension >> Extensions, bool AllowASTWithErrors=false, bool IncludeTimestamps=true, bool ShouldCacheASTInMemory=false)
Describes a module or submodule.
static SourceLocation getFromRawEncoding(unsigned Encoding)
Turn a raw encoding of a SourceLocation object into a real SourceLocation.
const unsigned int NUM_PREDEF_DECL_IDS
The number of declaration IDs that are predefined.
serialization::TypeID getTypeID(QualType T) const
Determine the type ID of an already-emitted type.
void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, StringRef Path)
Emit the current record with the given path as a blob.
A structure for putting "fast"-unqualified QualTypes into a DenseMap.
unsigned getLocalOrImportedSubmoduleID(Module *Mod)
Retrieve or create a submodule ID for this module, or return 0 if the submodule is neither local (a s...
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
serialization::TypeID GetOrCreateTypeID(QualType T)
Force a type to be emitted and get its ID.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
unsigned getExprImplicitCastAbbrev() const
ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl< char > &Buffer, InMemoryModuleCache &ModuleCache, ArrayRef< std::shared_ptr< ModuleFileExtension >> Extensions, bool IncludeTimestamps=true)
Create a new precompiled header writer that outputs to the given bitstream.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
const unsigned NUM_PREDEF_TYPE_IDS
The number of predefined type IDs that are reserved for the PREDEF_TYPE_* constants.
const unsigned int NUM_PREDEF_SUBMODULE_IDS
The number of predefined submodule IDs.
Represents a C++ destructor within a class.
Record the location of a macro definition.
void ClearSwitchCaseIDs()
Represents a variable declaration or definition.
Represents the declaration of a struct/union/class/enum.
unsigned RecordSwitchCaseID(SwitchCase *S)
Record an ID for the given switch-case statement.
uint32_t TypeID
An ID number that refers to a type in an AST file.
const unsigned int NUM_PREDEF_IDENT_IDS
The number of predefined identifier IDs.
The signature of a module, which is a hash of the AST content.
unsigned getAnonymousDeclarationNumber(const NamedDecl *D)
Writes an AST file containing the contents of a translation unit.
unsigned getDeclParmVarAbbrev() const
static Decl::Kind getKind(const Decl *D)
Cached information about one file (either on disk or in the virtual file system).
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
unsigned getTypeExtQualAbbrev() const
ObjCCategoryDecl - Represents a category declaration.
ASTContext & getASTContext() const
Represents a C++ struct/union/class.
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
unsigned getIntegerLiteralAbbrev() const
Declaration of a class template.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
SmallVectorImpl< uint64_t > RecordDataImpl
serialization::DeclID getDeclID(const Decl *D)
Determine the declaration ID of an already-emitted declaration.
void AddAlignPackInfo(const Sema::AlignPackInfo &Info, RecordDataImpl &Record)
Emit a AlignPackInfo.
SmallVectorImpl< char > & getPCH() const
Decl - This represents one declaration (or definition), e.g.
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record)
Emit a source location.
serialization::IdentID getIdentifierRef(const IdentifierInfo *II)
Get the unique number used to refer to the given identifier.
Sema - This implements semantic analysis and AST building for C.
const unsigned int NUM_PREDEF_MACRO_IDS
The number of predefined macro IDs.
unsigned getDeclObjCIvarAbbrev() const
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
An object for streaming information to a record.
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
serialization::SelectorID getSelectorRef(Selector Sel)
Get the unique number used to refer to the given selector.
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
One of these records is kept for each identifier that is lexed.
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name)
const LangOptions & getLangOpts() const
static QualType getFromOpaquePtr(const void *Ptr)
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false, bool ShouldCacheASTInMemory=false)
Write a precompiled header for the given semantic analysis.
Reads an AST files chain containing the contents of a translation unit.
time_t getTimestampForOutput(const FileEntry *E) const
Get a timestamp for output into the AST file.
serialization::MacroID getMacroID(MacroInfo *MI)
Determine the ID of an already-emitted macro.
ASTReader * getChain() const
Dataflow Directional Tag Classes.
Smart pointer class that efficiently represents Objective-C method names.
void SetSelectorOffset(Selector Sel, uint32_t Offset)
Note that the selector Sel occurs at the given offset within the method pool/selector table.
ArrayRef< uint64_t > RecordDataRef
unsigned getDeclRecordAbbrev() const
void AddTypeRef(QualType T, RecordDataImpl &Record)
Emit a reference to a type.
Attr - This represents one attribute.
In-memory cache for modules.
serialization::DeclID GetDeclRef(const Decl *D)
Force a declaration to be emitted and get its ID.
void AddSourceRange(SourceRange Range, RecordDataImpl &Record)
Emit a source range.
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
void * getAsOpaquePtr() const
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
unsigned getDeclEnumAbbrev() const
Declaration of a variable template.
bool hasEmittedPCH() const
This represents one expression.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
unsigned getDeclFieldAbbrev() const
Represents a class template specialization, which refers to a class template with a given set of temp...
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
void AddToken(const Token &Tok, RecordDataImpl &Record)
Emit a token.
Represents a function declaration or definition.
Represents a struct/union/class.
unsigned getDeclCXXMethodAbbrev() const
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset)
Note that the identifier II occurs at the given offset within the identifier table.
unsigned getDeclTypedefAbbrev() const