14#ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H
15#define LLVM_CLANG_SERIALIZATION_ASTWRITER_H
29#include "llvm/ADT/ArrayRef.h"
30#include "llvm/ADT/DenseMap.h"
31#include "llvm/ADT/DenseSet.h"
32#include "llvm/ADT/MapVector.h"
33#include "llvm/ADT/STLExtras.h"
34#include "llvm/ADT/SetVector.h"
35#include "llvm/ADT/SmallVector.h"
36#include "llvm/ADT/StringRef.h"
37#include "llvm/Bitstream/BitstreamWriter.h"
55class FPOptionsOverride;
58class HeaderSearchOptions;
59class IdentifierResolver;
61class MacroDefinitionRecord;
64class InMemoryModuleCache;
65class ModuleFileExtension;
66class ModuleFileExtensionWriter;
68class ObjCInterfaceDecl;
69class PreprocessingRecord;
111 llvm::BitstreamWriter &Stream;
129 Module *WritingModule =
nullptr;
132 uint64_t ASTBlockStartOffset = 0;
135 std::pair<uint64_t, uint64_t> ASTBlockRange;
138 std::string BaseDirectory;
144 bool IncludeTimestamps;
148 bool WritingAST =
false;
152 bool DoneWritingDeclsAndTypes =
false;
155 bool ASTHasCompilerErrors =
false;
159 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs;
164 DeclOrType(
Decl *D) : Stored(D), IsType(
false) {}
167 bool isType()
const {
return IsType; }
168 bool isDecl()
const {
return !IsType; }
171 assert(isType() &&
"Not a type!");
175 Decl *getDecl()
const {
176 assert(isDecl() &&
"Not a decl!");
177 return static_cast<Decl *
>(Stored);
186 std::queue<DeclOrType> DeclTypesToEmit;
200 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
204 std::vector<serialization::DeclOffset> DeclOffsets;
208 uint64_t DeclTypesBlockStartOffset = 0;
213 struct DeclIDInFileInfo {
214 LocDeclIDsTy DeclIDs;
218 unsigned FirstDeclIndex;
220 using FileDeclIDsTy =
221 llvm::DenseMap<FileID, std::unique_ptr<DeclIDInFileInfo>>;
225 FileDeclIDsTy FileDeclIDs;
248 std::vector<serialization::UnderalignedInt64> TypeOffsets;
262 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
271 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
273 struct MacroInfoToEmitData {
274 const IdentifierInfo *Name;
280 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
282 llvm::DenseMap<const IdentifierInfo *, uint32_t>
283 IdentMacroDirectivesOffsetMap;
293 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
299 std::vector<uint32_t> IdentifierOffsets;
316 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
320 std::vector<uint32_t> SelectorOffsets;
324 llvm::DenseMap<
const MacroDefinitionRecord *,
329 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
341 const Attr *Attribute;
345 DeclUpdate(
unsigned Kind) :
Kind(
Kind), Dcl(nullptr) {}
346 DeclUpdate(
unsigned Kind,
const Decl *Dcl) :
Kind(
Kind), Dcl(Dcl) {}
347 DeclUpdate(
unsigned Kind, QualType Type)
349 DeclUpdate(
unsigned Kind, SourceLocation Loc)
350 :
Kind(
Kind), Loc(Loc.getRawEncoding()) {}
351 DeclUpdate(
unsigned Kind,
unsigned Val) :
Kind(
Kind), Val(Val) {}
352 DeclUpdate(
unsigned Kind, Module *M) :
Kind(
Kind), Mod(M) {}
353 DeclUpdate(
unsigned Kind,
const Attr *Attribute)
354 :
Kind(
Kind), Attribute(Attribute) {}
356 unsigned getKind()
const {
return Kind; }
357 const Decl *getDecl()
const {
return Dcl; }
360 SourceLocation getLoc()
const {
364 unsigned getNumber()
const {
return Val; }
365 Module *getModule()
const {
return Mod; }
366 const Attr *getAttr()
const {
return Attribute; }
369 using UpdateRecord = SmallVector<DeclUpdate, 1>;
370 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
374 DeclUpdateMap DeclUpdates;
376 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
380 FirstLatestDeclMap FirstLatestDecls;
393 SmallVector<serialization::DeclID, 16> EagerlyDeserializedDecls;
394 SmallVector<serialization::DeclID, 16> ModularCodegenDecls;
408 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced;
412 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
420 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
423 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
426 unsigned NumStatements = 0;
429 unsigned NumMacros = 0;
433 unsigned NumLexicalDeclContexts = 0;
437 unsigned NumVisibleDeclContexts = 0;
441 llvm::DenseMap<const Module *, unsigned> SubmoduleIDs;
444 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
445 ModuleFileExtensionWriters;
448 llvm::BitVector IsSLocAffecting;
451 std::vector<FileID> NonAffectingFileIDs;
452 std::vector<unsigned> NonAffectingFileIDAdjustments;
455 std::vector<SourceRange> NonAffectingRanges;
456 std::vector<SourceLocation::UIntTy> NonAffectingOffsetAdjustments;
461 void collectNonAffectingInputFiles();
465 FileID getAdjustedFileID(FileID FID)
const;
468 unsigned getAdjustedNumCreatedFIDs(FileID FID)
const;
471 SourceLocation getAdjustedLocation(SourceLocation Loc)
const;
474 SourceRange getAdjustedRange(SourceRange Range)
const;
483 unsigned getSubmoduleID(Module *Mod);
486 void WriteSubStmt(Stmt *S);
488 void WriteBlockInfoBlock();
489 void WriteControlBlock(Preprocessor &PP, ASTContext &Context,
493 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP,
494 ASTContext &Context);
497 static std::pair<ASTFileSignature, ASTFileSignature>
498 createSignature(StringRef AllBytes, StringRef ASTBlockBytes);
500 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts);
501 void WriteSourceManagerBlock(SourceManager &SourceMgr,
502 const Preprocessor &PP);
503 void writeIncludedFiles(raw_ostream &Out,
const Preprocessor &PP);
504 void WritePreprocessor(
const Preprocessor &PP,
bool IsModule);
505 void WriteHeaderSearch(
const HeaderSearch &HS);
506 void WritePreprocessorDetail(PreprocessingRecord &PPRec,
507 uint64_t MacroOffsetsBase);
508 void WriteSubmodules(Module *WritingModule);
510 void WritePragmaDiagnosticMappings(
const DiagnosticsEngine &
Diag,
513 unsigned TypeExtQualAbbrev = 0;
514 void WriteTypeAbbrevs();
515 void WriteType(QualType T);
517 bool isLookupResultExternal(StoredDeclsList &
Result, DeclContext *DC);
518 bool isLookupResultEntirelyExternal(StoredDeclsList &
Result, DeclContext *DC);
520 void GenerateNameLookupTable(
const DeclContext *DC,
522 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
523 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
524 void WriteTypeDeclOffsets();
525 void WriteFileDeclIDsMap();
526 void WriteComments();
527 void WriteSelectors(Sema &SemaRef);
528 void WriteReferencedSelectorsPool(Sema &SemaRef);
529 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
532 void WriteDeclContextVisibleUpdate(
const DeclContext *DC);
533 void WriteFPPragmaOptions(
const FPOptionsOverride &Opts);
534 void WriteOpenCLExtensions(Sema &SemaRef);
535 void WriteCUDAPragmas(Sema &SemaRef);
536 void WriteObjCCategories();
537 void WriteLateParsedTemplates(Sema &SemaRef);
538 void WriteOptimizePragmaOptions(Sema &SemaRef);
539 void WriteMSStructPragmaOptions(Sema &SemaRef);
540 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef);
541 void WritePackPragmaOptions(Sema &SemaRef);
542 void WriteFloatControlPragmaOptions(Sema &SemaRef);
543 void WriteModuleFileExtension(Sema &SemaRef,
544 ModuleFileExtensionWriter &Writer);
546 unsigned DeclParmVarAbbrev = 0;
547 unsigned DeclContextLexicalAbbrev = 0;
548 unsigned DeclContextVisibleLookupAbbrev = 0;
549 unsigned UpdateVisibleAbbrev = 0;
550 unsigned DeclRecordAbbrev = 0;
551 unsigned DeclTypedefAbbrev = 0;
552 unsigned DeclVarAbbrev = 0;
553 unsigned DeclFieldAbbrev = 0;
554 unsigned DeclEnumAbbrev = 0;
555 unsigned DeclObjCIvarAbbrev = 0;
556 unsigned DeclCXXMethodAbbrev = 0;
558 unsigned DeclRefExprAbbrev = 0;
559 unsigned CharacterLiteralAbbrev = 0;
560 unsigned IntegerLiteralAbbrev = 0;
561 unsigned ExprImplicitCastAbbrev = 0;
563 void WriteDeclAbbrevs();
564 void WriteDecl(ASTContext &Context, Decl *D);
566 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot,
567 Module *WritingModule);
572 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer,
573 InMemoryModuleCache &ModuleCache,
574 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
575 bool IncludeTimestamps =
true);
579 assert(Context &&
"requested AST context when not writing AST");
605 Module *WritingModule, StringRef isysroot,
606 bool hasErrors =
false,
607 bool ShouldCacheASTInMemory =
false);
621 LocSeq *Seq =
nullptr);
625 LocSeq *Seq =
nullptr);
663 auto I = DeclIDs.find(D);
664 return (I == DeclIDs.end() ||
719 return TypeExtQualAbbrev;
745 void ReaderInitialized(
ASTReader *Reader)
override;
755 void CompletedTagDefinition(
const TagDecl *D)
override;
758 void AddedCXXTemplateSpecialization(
761 void AddedCXXTemplateSpecialization(
766 void ResolvedExceptionSpec(
const FunctionDecl *FD)
override;
770 Expr *ThisArg)
override;
771 void CompletedImplicitDefinition(
const FunctionDecl *D)
override;
772 void InstantiationRequested(
const ValueDecl *D)
override;
773 void VariableDefinitionInstantiated(
const VarDecl *D)
override;
774 void FunctionDefinitionInstantiated(
const FunctionDecl *D)
override;
775 void DefaultArgumentInstantiated(
const ParmVarDecl *D)
override;
776 void DefaultMemberInitializerInstantiated(
const FieldDecl *D)
override;
779 void DeclarationMarkedUsed(
const Decl *D)
override;
780 void DeclarationMarkedOpenMPThreadPrivate(
const Decl *D)
override;
781 void DeclarationMarkedOpenMPDeclareTarget(
const Decl *D,
783 void DeclarationMarkedOpenMPAllocate(
const Decl *D,
const Attr *A)
override;
784 void RedefinedHiddenDefinition(
const NamedDecl *D,
Module *M)
override;
785 void AddedAttributeToRecord(
const Attr *
Attr,
793 std::string OutputFile;
794 std::string isysroot;
796 std::shared_ptr<PCHBuffer> Buffer;
797 llvm::BitstreamWriter Stream;
799 bool AllowASTWithErrors;
800 bool ShouldCacheASTInMemory;
809 StringRef OutputFile, StringRef isysroot,
810 std::shared_ptr<PCHBuffer> Buffer,
811 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
812 bool AllowASTWithErrors =
false,
bool IncludeTimestamps =
true,
813 bool ShouldCacheASTInMemory =
false);
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified.
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.
Defines the clang::Module class, which describes a module in the source code.
Defines the clang::SourceLocation class and associated facilities.
C Language Family Type Representation.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
Reads an AST files chain containing the contents of a translation unit.
An object for streaming information to a record.
Writes an AST file containing the contents of a translation unit.
serialization::MacroID getMacroID(MacroInfo *MI)
Determine the ID of an already-emitted macro.
serialization::TypeID getTypeID(QualType T) const
Determine the type ID of an already-emitted type.
unsigned getDeclParmVarAbbrev() const
void AddSourceRange(SourceRange Range, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source range.
bool isWritingStdCXXNamedModules() const
ArrayRef< uint64_t > RecordDataRef
void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, StringRef Path)
Emit the current record with the given path as a blob.
void AddFileID(FileID FID, RecordDataImpl &Record)
Emit a FileID.
unsigned getDeclObjCIvarAbbrev() const
unsigned getExprImplicitCastAbbrev() const
serialization::DeclID GetDeclRef(const Decl *D)
Force a declaration to be emitted and get its ID.
unsigned getDeclTypedefAbbrev() const
unsigned getSwitchCaseID(SwitchCase *S)
Retrieve the ID for the given switch-case statement.
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
SmallVectorImpl< uint64_t > RecordDataImpl
unsigned getTypeExtQualAbbrev() const
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
uint32_t getMacroDirectivesOffset(const IdentifierInfo *Name)
unsigned getDeclVarAbbrev() const
unsigned getDeclEnumAbbrev() const
void AddAlignPackInfo(const Sema::AlignPackInfo &Info, RecordDataImpl &Record)
Emit a AlignPackInfo.
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...
unsigned getDeclRefExprAbbrev() 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.
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
time_t getTimestampForOutput(const FileEntry *E) const
Get a timestamp for output into the AST file.
void ClearSwitchCaseIDs()
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
serialization::TypeID GetOrCreateTypeID(QualType T)
Force a type to be emitted and get its ID.
serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name)
Get the unique number used to refer to the given macro.
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record, LocSeq *Seq=nullptr)
Emit a source location.
void AddTypeRef(QualType T, RecordDataImpl &Record)
Emit a reference to a type.
ASTContext & getASTContext() const
ASTFileSignature WriteAST(Sema &SemaRef, StringRef OutputFile, Module *WritingModule, StringRef isysroot, bool hasErrors=false, bool ShouldCacheASTInMemory=false)
Write a precompiled header for the given semantic analysis.
unsigned getDeclCXXMethodAbbrev() const
ASTReader * getChain() const
unsigned RecordSwitchCaseID(SwitchCase *S)
Record an ID for the given switch-case statement.
serialization::DeclID getDeclID(const Decl *D)
Determine the declaration ID of an already-emitted declaration.
unsigned getCharacterLiteralAbbrev() const
unsigned getLocalOrImportedSubmoduleID(const Module *Mod)
Retrieve or create a submodule ID for this module, or return 0 if the submodule is neither local (a s...
const Decl * getFirstLocalDecl(const Decl *D)
Find the first local declaration of a given local redeclarable decl.
void AddToken(const Token &Tok, RecordDataImpl &Record)
Emit a token.
serialization::SelectorID getSelectorRef(Selector Sel)
Get the unique number used to refer to the given selector.
unsigned getAnonymousDeclarationNumber(const NamedDecl *D)
unsigned getDeclFieldAbbrev() const
serialization::IdentID getIdentifierRef(const IdentifierInfo *II)
Get the unique number used to refer to the given identifier.
const LangOptions & getLangOpts() const
void SetSelectorOffset(Selector Sel, uint32_t Offset)
Note that the selector Sel occurs at the given offset within the method pool/selector table.
bool PreparePathForOutput(SmallVectorImpl< char > &Path)
Convert a path from this build process into one that is appropriate for emission in the module file.
void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset)
Note that the identifier II occurs at the given offset within the identifier table.
unsigned getDeclRecordAbbrev() const
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
unsigned getIntegerLiteralAbbrev() const
Attr - This represents one attribute.
Represents a C++ destructor within a class.
Represents a C++ struct/union/class.
Declaration of a class template.
Represents a class template specialization, which refers to a class template with a given set of temp...
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Decl - This represents one declaration (or definition), e.g.
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
This represents one expression.
Represents a member of a struct/union/class.
Cached information about one file (either on disk or in the virtual file system).
An opaque identifier used by SourceManager which refers to a source file (MemoryBuffer) along with it...
Represents a function declaration or definition.
Declaration of a template function.
One of these records is kept for each identifier that is lexed.
In-memory cache for modules.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Record the location of a macro definition.
Encapsulates the data about a macro definition (e.g.
Describes a module or submodule.
bool isModulePurview() const
Does this Module scope describe part of the purview of a standard named C++ module?
This represents a decl that may have a name.
ObjCCategoryDecl - Represents a category declaration.
Represents an ObjC class declaration.
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code.
ASTMutationListener * GetASTMutationListener() override
If the consumer is interested in entities getting modified after their initial creation,...
SmallVectorImpl< char > & getPCH() const
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
ASTDeserializationListener * GetASTDeserializationListener() override
If the consumer is interested in entities being deserialized from AST files, it should return a point...
const ASTWriter & getWriter() const
void HandleTranslationUnit(ASTContext &Ctx) override
HandleTranslationUnit - This method is called when the ASTs for entire translation unit have been par...
bool hasEmittedPCH() const
Represents a parameter to a function.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.
A (possibly-)qualified type.
static QualType getFromOpaquePtr(const void *Ptr)
void * getAsOpaquePtr() const
Represents a struct/union/class.
Smart pointer class that efficiently represents Objective-C method names.
An abstract interface that should be implemented by clients that read ASTs and then require further s...
Sema - This implements semantic analysis and AST building for C.
Serialized encoding of a sequence of SourceLocations.
Encodes a location in the source.
static SourceLocation getFromRawEncoding(UIntTy Encoding)
Turn a raw encoding of a SourceLocation object into a real SourceLocation.
A trivial tuple used to represent a source range.
Represents the declaration of a struct/union/class/enum.
Token - This structure provides full information about a lexed token.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Represents a variable declaration or definition.
Declaration of a variable template.
Represents a variable template specialization, which refers to a variable template with a given set o...
A type index; the type ID with the qualifier bits removed.
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_DECL_IDS
The number of declaration IDs that are predefined.
@ Decl
The l-value was an access to a declared entity or something equivalently strong, like the address of ...
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.
uint32_t SubmoduleID
An ID number that refers to a submodule in a module file.
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
const unsigned int NUM_PREDEF_SUBMODULE_IDS
The number of predefined submodule IDs.
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
const unsigned int NUM_PREDEF_SELECTOR_IDS
The number of predefined selector IDs.
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
const unsigned int NUM_PREDEF_MACRO_IDS
The number of predefined macro IDs.
uint32_t MacroID
An ID number that refers to a macro in an AST file.
@ Result
The result type of a method or function.
The signature of a module, which is a hash of the AST content.
A structure for putting "fast"-unqualified QualTypes into a DenseMap.