15 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H 16 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H 32 #include "llvm/ADT/ArrayRef.h" 33 #include "llvm/ADT/DenseMap.h" 34 #include "llvm/ADT/DenseSet.h" 35 #include "llvm/ADT/MapVector.h" 36 #include "llvm/ADT/SetVector.h" 37 #include "llvm/ADT/SmallVector.h" 38 #include "llvm/ADT/StringRef.h" 39 #include "llvm/Bitcode/BitstreamWriter.h" 62 class ASTUnresolvedSet;
64 class CXXBaseSpecifier;
65 class CXXCtorInitializer;
72 class HeaderSearchOptions;
73 class IdentifierResolver;
75 class MacroDefinitionRecord;
77 class MemoryBufferCache;
79 class ModuleFileExtension;
80 class ModuleFileExtensionWriter;
82 class NestedNameSpecifier;
83 class ObjCInterfaceDecl;
84 class PreprocessingRecord;
91 struct StoredDeclsList;
93 class TemplateParameterList;
129 llvm::BitstreamWriter &Stream;
147 Module *WritingModule =
nullptr;
150 std::string BaseDirectory;
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) {}
177 DeclOrType(QualType T) : Stored(T.
getAsOpaquePtr()), IsType(
true) {}
179 bool isType()
const {
return IsType; }
180 bool isDecl()
const {
return !IsType; }
182 QualType getType()
const {
183 assert(isType() &&
"Not a type!");
184 return QualType::getFromOpaquePtr(Stored);
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;
221 struct DeclIDInFileInfo {
222 LocDeclIDsTy DeclIDs;
226 unsigned FirstDeclIndex;
228 using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>;
232 FileDeclIDsTy FileDeclIDs;
255 std::vector<uint32_t> TypeOffsets;
269 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
278 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs;
280 struct MacroInfoToEmitData {
287 std::vector<MacroInfoToEmitData> MacroInfosToEmit;
289 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap;
299 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
305 std::vector<uint32_t> IdentifierOffsets;
322 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs;
326 std::vector<uint32_t> SelectorOffsets;
335 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers;
347 const Attr *Attribute;
351 DeclUpdate(
unsigned Kind) :
Kind(Kind), Dcl(
nullptr) {}
352 DeclUpdate(
unsigned Kind,
const Decl *Dcl) :
Kind(Kind), Dcl(Dcl) {}
353 DeclUpdate(
unsigned Kind, QualType
Type)
357 DeclUpdate(
unsigned Kind,
unsigned Val) :
Kind(Kind), Val(Val) {}
358 DeclUpdate(
unsigned Kind,
Module *M) :
Kind(Kind), Mod(M) {}
359 DeclUpdate(
unsigned Kind,
const Attr *Attribute)
360 :
Kind(Kind), Attribute(Attribute) {}
363 const Decl *getDecl()
const {
return Dcl; }
364 QualType getType()
const {
return QualType::getFromOpaquePtr(Type); }
367 return SourceLocation::getFromRawEncoding(Loc);
370 unsigned getNumber()
const {
return Val; }
371 Module *getModule()
const {
return Mod; }
372 const Attr *getAttr()
const {
return Attribute; }
376 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>;
380 DeclUpdateMap DeclUpdates;
382 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>;
386 FirstLatestDeclMap FirstLatestDecls;
418 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
426 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache;
429 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
432 unsigned NumStatements = 0;
435 unsigned NumMacros = 0;
439 unsigned NumLexicalDeclContexts = 0;
443 unsigned NumVisibleDeclContexts = 0;
447 llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
450 std::vector<std::unique_ptr<ModuleFileExtensionWriter>>
451 ModuleFileExtensionWriters;
454 unsigned getSubmoduleID(
Module *Mod);
457 void WriteSubStmt(
Stmt *S);
459 void WriteBlockInfoBlock();
461 StringRef isysroot,
const std::string &OutputFile);
474 void WritePreprocessor(
const Preprocessor &PP,
bool IsModule);
477 void WriteSubmodules(
Module *WritingModule);
482 unsigned TypeExtQualAbbrev = 0;
483 unsigned TypeFunctionProtoAbbrev = 0;
484 void WriteTypeAbbrevs();
485 void WriteType(QualType T);
490 void GenerateNameLookupTable(
const DeclContext *DC,
494 void WriteTypeDeclOffsets();
495 void WriteFileDeclIDsMap();
496 void WriteComments();
497 void WriteSelectors(
Sema &SemaRef);
498 void WriteReferencedSelectorsPool(
Sema &SemaRef);
502 void WriteDeclContextVisibleUpdate(
const DeclContext *DC);
503 void WriteFPPragmaOptions(
const FPOptions &Opts);
504 void WriteOpenCLExtensions(
Sema &SemaRef);
505 void WriteOpenCLExtensionTypes(
Sema &SemaRef);
506 void WriteOpenCLExtensionDecls(
Sema &SemaRef);
507 void WriteCUDAPragmas(
Sema &SemaRef);
508 void WriteObjCCategories();
509 void WriteLateParsedTemplates(
Sema &SemaRef);
510 void WriteOptimizePragmaOptions(
Sema &SemaRef);
511 void WriteMSStructPragmaOptions(
Sema &SemaRef);
512 void WriteMSPointersToMembersPragmaOptions(
Sema &SemaRef);
513 void WritePackPragmaOptions(
Sema &SemaRef);
514 void WriteModuleFileExtension(
Sema &SemaRef,
517 unsigned DeclParmVarAbbrev = 0;
518 unsigned DeclContextLexicalAbbrev = 0;
519 unsigned DeclContextVisibleLookupAbbrev = 0;
520 unsigned UpdateVisibleAbbrev = 0;
521 unsigned DeclRecordAbbrev = 0;
522 unsigned DeclTypedefAbbrev = 0;
523 unsigned DeclVarAbbrev = 0;
524 unsigned DeclFieldAbbrev = 0;
525 unsigned DeclEnumAbbrev = 0;
526 unsigned DeclObjCIvarAbbrev = 0;
527 unsigned DeclCXXMethodAbbrev = 0;
529 unsigned DeclRefExprAbbrev = 0;
530 unsigned CharacterLiteralAbbrev = 0;
531 unsigned IntegerLiteralAbbrev = 0;
532 unsigned ExprImplicitCastAbbrev = 0;
534 void WriteDeclAbbrevs();
538 const std::string &OutputFile,
546 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
547 bool IncludeTimestamps =
true);
555 time_t getTimestampForOutput(
const FileEntry *E)
const;
572 Module *WritingModule, StringRef isysroot,
573 bool hasErrors =
false);
612 const Decl *getFirstLocalDecl(
const Decl *D);
620 auto I = DeclIDs.find(D);
621 return (I == DeclIDs.end() ||
635 unsigned getAnonymousDeclarationNumber(
const NamedDecl *D);
648 void EmitRecordWithPath(
unsigned Abbrev,
RecordDataRef Record,
652 void AddVersionTuple(
const VersionTuple &Version,
RecordDataImpl &Record);
657 unsigned getLocalOrImportedSubmoduleID(
Module *Mod);
665 void SetSelectorOffset(
Selector Sel, uint32_t Offset);
673 void ClearSwitchCaseIDs();
676 return TypeExtQualAbbrev;
680 return TypeFunctionProtoAbbrev;
702 void ReaderInitialized(
ASTReader *Reader)
override;
705 void TypeRead(serialization::TypeIdx Idx, QualType T)
override;
708 MacroDefinitionRecord *MD)
override;
712 void CompletedTagDefinition(
const TagDecl *D)
override;
715 void AddedCXXTemplateSpecialization(
718 void AddedCXXTemplateSpecialization(
723 void ResolvedExceptionSpec(
const FunctionDecl *FD)
override;
724 void DeducedReturnType(
const FunctionDecl *FD, QualType ReturnType)
override;
727 Expr *ThisArg)
override;
728 void CompletedImplicitDefinition(
const FunctionDecl *D)
override;
729 void InstantiationRequested(
const ValueDecl *D)
override;
730 void VariableDefinitionInstantiated(
const VarDecl *D)
override;
731 void FunctionDefinitionInstantiated(
const FunctionDecl *D)
override;
732 void DefaultArgumentInstantiated(
const ParmVarDecl *D)
override;
733 void DefaultMemberInitializerInstantiated(
const FieldDecl *D)
override;
736 void DeclarationMarkedUsed(
const Decl *D)
override;
737 void DeclarationMarkedOpenMPThreadPrivate(
const Decl *D)
override;
738 void DeclarationMarkedOpenMPDeclareTarget(
const Decl *D,
740 void RedefinedHiddenDefinition(
const NamedDecl *D,
Module *M)
override;
741 void AddedAttributeToRecord(
const Attr *Attr,
762 void FlushSubStmts();
764 void PrepareToEmit(uint64_t MyOffset) {
766 for (
unsigned I : OffsetIndices) {
767 auto &StoredOffset = (*Record)[I];
768 assert(StoredOffset < MyOffset &&
"invalid offset");
770 StoredOffset = MyOffset - StoredOffset;
772 OffsetIndices.clear();
778 : Writer(&Writer), Record(&Record) {}
783 : Writer(Parent.Writer), Record(&Record) {}
795 template<
typename InputIterator>
796 void append(InputIterator begin, InputIterator end) {
797 Record->append(begin, end);
799 bool empty()
const {
return Record->empty(); }
800 size_t size()
const {
return Record->size(); }
807 uint64_t
Emit(
unsigned Code,
unsigned Abbrev = 0) {
808 uint64_t
Offset = Writer->Stream.GetCurrentBitNo();
809 PrepareToEmit(Offset);
810 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
816 uint64_t
EmitStmt(
unsigned Code,
unsigned Abbrev = 0) {
818 PrepareToEmit(Writer->Stream.GetCurrentBitNo());
819 Writer->Stream.EmitRecord(Code, *Record, Abbrev);
820 return Writer->Stream.GetCurrentBitNo();
826 OffsetIndices.push_back(Record->size());
827 Record->push_back(BitOffset);
838 StmtsToEmit.push_back(S);
856 void AddAPInt(
const llvm::APInt &
Value);
859 void AddAPSInt(
const llvm::APSInt &Value);
862 void AddAPFloat(
const llvm::APFloat &Value);
900 void AddASTTemplateArgumentListInfo(
950 return Writer->
AddPath(Path, *Record);
966 std::string OutputFile;
967 std::string isysroot;
969 std::shared_ptr<PCHBuffer> Buffer;
970 llvm::BitstreamWriter Stream;
972 bool AllowASTWithErrors;
981 std::shared_ptr<PCHBuffer> Buffer,
982 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions,
983 bool AllowASTWithErrors =
false,
bool IncludeTimestamps =
true);
987 void HandleTranslationUnit(
ASTContext &Ctx)
override;
995 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H unsigned getDeclEnumAbbrev() const
Represents a function declaration or definition.
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.
Smart pointer class that efficiently represents Objective-C method names.
unsigned getRawEncoding() const
When a SourceLocation itself cannot be used, this returns an (opaque) 32-bit integer encoding for it...
A (possibly-)qualified type.
DominatorTree GraphTraits specialization so the DominatorTree can be iterable by generic graph iterat...
A structure for putting "fast"-unqualified QualTypes into a DenseMap.
const unsigned NUM_PREDEF_TYPE_IDS
The number of predefined type IDs that are reserved for the PREDEF_TYPE_* constants.
Stmt - This represents one statement.
C Language Family Type Representation.
void InitializeSema(Sema &S) override
Initialize the semantic consumer with the Sema instance being used to perform semantic analysis on th...
const unsigned int NUM_PREDEF_SELECTOR_IDS
The number of predefined selector IDs.
Decl - This represents one declaration (or definition), e.g.
uint64_t & operator[](size_t N)
The base class of the type hierarchy.
Declaration of a variable template.
A container of type source information.
Floating point control options.
void AddTypeRef(QualType T, RecordDataImpl &Record)
Emit a reference to a type.
unsigned getDeclRefExprAbbrev() const
uint64_t Emit(unsigned Code, unsigned Abbrev=0)
Emit the record to the stream, followed by its substatements, and return its offset.
Represents a variable declaration or definition.
Manage memory buffers across multiple users.
const unsigned int NUM_PREDEF_DECL_IDS
The number of declaration IDs that are predefined.
Represents a variable template specialization, which refers to a variable template with a given set o...
Represents an explicit template argument list in C++, e.g., the "<int>" in "sort<int>".
Stores a list of template parameters for a TemplateDecl and its derived classes.
SmallVectorImpl< char > & getPCH() const
Represents a parameter to a function.
const ASTWriter & getWriter() const
void AddString(StringRef Str)
Emit a string.
void AddSourceRange(SourceRange Range)
Emit a source range.
void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record)
Emit a reference to an identifier.
bool hasEmittedPCH() const
Base wrapper for a particular "section" of type source info.
Represents a struct/union/class.
Represents a class template specialization, which refers to a class template with a given set of temp...
One of these records is kept for each identifier that is lexed.
ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the same encoding scheme as another ASTRecordWriter.
void AddTypeRef(QualType T)
Emit a reference to a type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
A C++ nested-name-specifier augmented with source location information.
Record the location of a macro definition.
Represents a member of a struct/union/class.
Token - This structure provides full information about a lexed token.
ASTReader * getChain() const
uint32_t MacroID
An ID number that refers to a macro in an AST file.
The signature of a module, which is a hash of the AST content.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
Describes a module or submodule.
unsigned getDeclFieldAbbrev() const
void AddIdentifierRef(const IdentifierInfo *II)
Emit a reference to an identifier.
An abstract interface that should be implemented by clients that read ASTs and then require further s...
ASTWriter::RecordDataImpl & getRecordData() const
Extract the underlying record storage.
void append(InputIterator begin, InputIterator end)
unsigned getIntegerLiteralAbbrev() const
A record of the steps taken while preprocessing a source file, including the various preprocessing di...
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
Concrete class used by the front-end to report problems and issues.
void AddSourceRange(SourceRange Range, RecordDataImpl &Record)
Emit a source range.
unsigned getCharacterLiteralAbbrev() const
void * getAsOpaquePtr() const
Represents an ObjC class declaration.
unsigned getTypeFunctionProtoAbbrev() const
Sema - This implements semantic analysis and AST building for C.
void AddPath(StringRef Path, RecordDataImpl &Record)
Add a path to the given record.
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Expr - This represents one expression.
Represents a C++ destructor within a class.
ArgKind
The kind of template argument we're storing.
unsigned getDeclRecordAbbrev() const
IdentifierResolver - Keeps track of shadowed decls on enclosing scopes.
An abstract interface that should be implemented by listeners that want to be notified when an AST en...
unsigned getDeclTypedefAbbrev() const
Represents a C++ template name within the type system.
Defines the clang::TypeLoc interface and its subclasses.
void AddString(StringRef Str, RecordDataImpl &Record)
Add a string to the given record.
void AddPath(StringRef Path)
Emit a path.
void AddDeclRef(const Decl *D)
Emit a reference to a declaration.
void AddStmt(Stmt *S)
Add the given statement or expression to the queue of statements to emit.
Abstract base class that writes a module file extension block into a module file. ...
void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record)
Emit a source location.
void push_back(uint64_t N)
Minimal vector-like interface.
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.
A struct with extended info about a syntactic name qualifier, to be used for the case of out-of-line ...
Encodes a location in the source.
unsigned getDeclVarAbbrev() const
Represents a C++ temporary.
void AddOffset(uint64_t BitOffset)
Add a bit offset into the record.
Represents the declaration of a struct/union/class/enum.
Cached information about one file (either on disk or in the virtual file system). ...
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
ObjCCategoryDecl - Represents a category declaration.
unsigned getDeclCXXMethodAbbrev() const
bool isFromASTFile() const
Determine whether this declaration came from an AST file (such as a precompiled header or module) rat...
uint32_t TypeID
An ID number that refers to a type in an AST file.
void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record)
Add a version tuple to the given record.
void AddSourceLocation(SourceLocation Loc)
Emit a source location.
void AddDeclRef(const Decl *D, RecordDataImpl &Record)
Emit a reference to a declaration.
An array of decls optimized for the common case of only containing one entry.
uint32_t PreprocessedEntityID
An ID number that refers to an entity in the detailed preprocessing record.
unsigned getExprImplicitCastAbbrev() const
unsigned getTypeExtQualAbbrev() const
DeclarationNameLoc - Additional source/type location info for a declaration name. ...
Represents a template argument.
uint32_t SelectorID
An ID number that refers to an ObjC selector in an AST file.
Dataflow Directional Tag Classes.
DeclContext - This is used only as base class of specific decl types that can act as declaration cont...
Reads an AST files chain containing the contents of a translation unit.
DeclarationName - The name of a declaration.
DeclarationNameInfo - A collector data type for bundling together a DeclarationName and the correspnd...
unsigned getDeclParmVarAbbrev() const
uint32_t DeclID
An ID number that refers to a declaration in an AST file.
void AddVersionTuple(const VersionTuple &Version)
Emit a version tuple.
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...
Encapsulates the data about a macro definition (e.g.
Location wrapper for a TemplateArgument.
uint32_t IdentID
An ID number that refers to an identifier in an AST file.
An UnresolvedSet-like class which uses the ASTContext's allocator.
const unsigned int NUM_PREDEF_MACRO_IDS
The number of predefined macro IDs.
uint64_t EmitStmt(unsigned Code, unsigned Abbrev=0)
Emit the record to the stream, preceded by its substatements.
Represents a base class of a C++ class.
A template argument list.
Defines the clang::SourceLocation class and associated facilities.
Represents a C++ struct/union/class.
An object for streaming information to a record.
Location information for a TemplateArgument.
Declaration of a class template.
Writes an AST file containing the contents of a translation unit.
static Decl::Kind getKind(const Decl *D)
AST and semantic-analysis consumer that generates a precompiled header from the parsed source code...
A trivial tuple used to represent a source range.
This represents a decl that may have a name.
unsigned getDeclObjCIvarAbbrev() const
ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
Construct a ASTRecordWriter that uses the default encoding scheme.
This class handles loading and caching of source files into memory.
Declaration of a template function.
A type index; the type ID with the qualifier bits removed.
Attr - This represents one attribute.
const unsigned int NUM_PREDEF_SUBMODULE_IDS
The number of predefined submodule IDs.
Engages in a tight little dance with the lexer to efficiently preprocess tokens.