ASTWriter.h revision 360784
1//===- ASTWriter.h - AST File Writer ----------------------------*- C++ -*-===// 2// 3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4// See https://llvm.org/LICENSE.txt for license information. 5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6// 7//===----------------------------------------------------------------------===// 8// 9// This file defines the ASTWriter class, which writes an AST file 10// containing a serialized representation of a translation unit. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H 15#define LLVM_CLANG_SERIALIZATION_ASTWRITER_H 16 17#include "clang/AST/ASTMutationListener.h" 18#include "clang/AST/Decl.h" 19#include "clang/AST/Type.h" 20#include "clang/Basic/LLVM.h" 21#include "clang/Basic/SourceLocation.h" 22#include "clang/Sema/SemaConsumer.h" 23#include "clang/Serialization/ASTBitCodes.h" 24#include "clang/Serialization/ASTDeserializationListener.h" 25#include "clang/Serialization/PCHContainerOperations.h" 26#include "llvm/ADT/ArrayRef.h" 27#include "llvm/ADT/DenseMap.h" 28#include "llvm/ADT/DenseSet.h" 29#include "llvm/ADT/MapVector.h" 30#include "llvm/ADT/SetVector.h" 31#include "llvm/ADT/SmallVector.h" 32#include "llvm/ADT/StringRef.h" 33#include "llvm/Bitstream/BitstreamWriter.h" 34#include <cassert> 35#include <cstddef> 36#include <cstdint> 37#include <ctime> 38#include <memory> 39#include <queue> 40#include <string> 41#include <utility> 42#include <vector> 43 44namespace llvm { 45 46class APFloat; 47class APInt; 48class APSInt; 49 50} // namespace llvm 51 52namespace clang { 53 54class ASTContext; 55class ASTReader; 56class ASTUnresolvedSet; 57class Attr; 58class CXXBaseSpecifier; 59class CXXCtorInitializer; 60class CXXRecordDecl; 61class CXXTemporary; 62class FileEntry; 63class FPOptions; 64class FunctionDecl; 65class HeaderSearch; 66class HeaderSearchOptions; 67class IdentifierResolver; 68class LangOptions; 69class MacroDefinitionRecord; 70class MacroInfo; 71class Module; 72class InMemoryModuleCache; 73class ModuleFileExtension; 74class ModuleFileExtensionWriter; 75class NamedDecl; 76class ObjCInterfaceDecl; 77class PreprocessingRecord; 78class Preprocessor; 79struct QualifierInfo; 80class RecordDecl; 81class Sema; 82class SourceManager; 83class Stmt; 84struct StoredDeclsList; 85class SwitchCase; 86class TemplateParameterList; 87class Token; 88class TypeSourceInfo; 89 90/// Writes an AST file containing the contents of a translation unit. 91/// 92/// The ASTWriter class produces a bitstream containing the serialized 93/// representation of a given abstract syntax tree and its supporting 94/// data structures. This bitstream can be de-serialized via an 95/// instance of the ASTReader class. 96class ASTWriter : public ASTDeserializationListener, 97 public ASTMutationListener { 98public: 99 friend class ASTDeclWriter; 100 friend class ASTRecordWriter; 101 102 using RecordData = SmallVector<uint64_t, 64>; 103 using RecordDataImpl = SmallVectorImpl<uint64_t>; 104 using RecordDataRef = ArrayRef<uint64_t>; 105 106private: 107 /// Map that provides the ID numbers of each type within the 108 /// output stream, plus those deserialized from a chained PCH. 109 /// 110 /// The ID numbers of types are consecutive (in order of discovery) 111 /// and start at 1. 0 is reserved for NULL. When types are actually 112 /// stored in the stream, the ID number is shifted by 2 bits to 113 /// allow for the const/volatile qualifiers. 114 /// 115 /// Keys in the map never have const/volatile qualifiers. 116 using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx, 117 serialization::UnsafeQualTypeDenseMapInfo>; 118 119 /// The bitstream writer used to emit this precompiled header. 120 llvm::BitstreamWriter &Stream; 121 122 /// The buffer associated with the bitstream. 123 const SmallVectorImpl<char> &Buffer; 124 125 /// The PCM manager which manages memory buffers for pcm files. 126 InMemoryModuleCache &ModuleCache; 127 128 /// The ASTContext we're writing. 129 ASTContext *Context = nullptr; 130 131 /// The preprocessor we're writing. 132 Preprocessor *PP = nullptr; 133 134 /// The reader of existing AST files, if we're chaining. 135 ASTReader *Chain = nullptr; 136 137 /// The module we're currently writing, if any. 138 Module *WritingModule = nullptr; 139 140 /// The base directory for any relative paths we emit. 141 std::string BaseDirectory; 142 143 /// Indicates whether timestamps should be written to the produced 144 /// module file. This is the case for files implicitly written to the 145 /// module cache, where we need the timestamps to determine if the module 146 /// file is up to date, but not otherwise. 147 bool IncludeTimestamps; 148 149 /// Indicates when the AST writing is actively performing 150 /// serialization, rather than just queueing updates. 151 bool WritingAST = false; 152 153 /// Indicates that we are done serializing the collection of decls 154 /// and types to emit. 155 bool DoneWritingDeclsAndTypes = false; 156 157 /// Indicates that the AST contained compiler errors. 158 bool ASTHasCompilerErrors = false; 159 160 /// Mapping from input file entries to the index into the 161 /// offset table where information about that input file is stored. 162 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs; 163 164 /// Stores a declaration or a type to be written to the AST file. 165 class DeclOrType { 166 public: 167 DeclOrType(Decl *D) : Stored(D), IsType(false) {} 168 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {} 169 170 bool isType() const { return IsType; } 171 bool isDecl() const { return !IsType; } 172 173 QualType getType() const { 174 assert(isType() && "Not a type!"); 175 return QualType::getFromOpaquePtr(Stored); 176 } 177 178 Decl *getDecl() const { 179 assert(isDecl() && "Not a decl!"); 180 return static_cast<Decl *>(Stored); 181 } 182 183 private: 184 void *Stored; 185 bool IsType; 186 }; 187 188 /// The declarations and types to emit. 189 std::queue<DeclOrType> DeclTypesToEmit; 190 191 /// The first ID number we can use for our own declarations. 192 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS; 193 194 /// The decl ID that will be assigned to the next new decl. 195 serialization::DeclID NextDeclID = FirstDeclID; 196 197 /// Map that provides the ID numbers of each declaration within 198 /// the output stream, as well as those deserialized from a chained PCH. 199 /// 200 /// The ID numbers of declarations are consecutive (in order of 201 /// discovery) and start at 2. 1 is reserved for the translation 202 /// unit, while 0 is reserved for NULL. 203 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs; 204 205 /// Offset of each declaration in the bitstream, indexed by 206 /// the declaration's ID. 207 std::vector<serialization::DeclOffset> DeclOffsets; 208 209 /// Sorted (by file offset) vector of pairs of file offset/DeclID. 210 using LocDeclIDsTy = 211 SmallVector<std::pair<unsigned, serialization::DeclID>, 64>; 212 struct DeclIDInFileInfo { 213 LocDeclIDsTy DeclIDs; 214 215 /// Set when the DeclIDs vectors from all files are joined, this 216 /// indicates the index that this particular vector has in the global one. 217 unsigned FirstDeclIndex; 218 }; 219 using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>; 220 221 /// Map from file SLocEntries to info about the file-level declarations 222 /// that it contains. 223 FileDeclIDsTy FileDeclIDs; 224 225 void associateDeclWithFile(const Decl *D, serialization::DeclID); 226 227 /// The first ID number we can use for our own types. 228 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS; 229 230 /// The type ID that will be assigned to the next new type. 231 serialization::TypeID NextTypeID = FirstTypeID; 232 233 /// Map that provides the ID numbers of each type within the 234 /// output stream, plus those deserialized from a chained PCH. 235 /// 236 /// The ID numbers of types are consecutive (in order of discovery) 237 /// and start at 1. 0 is reserved for NULL. When types are actually 238 /// stored in the stream, the ID number is shifted by 2 bits to 239 /// allow for the const/volatile qualifiers. 240 /// 241 /// Keys in the map never have const/volatile qualifiers. 242 TypeIdxMap TypeIdxs; 243 244 /// Offset of each type in the bitstream, indexed by 245 /// the type's ID. 246 std::vector<uint32_t> TypeOffsets; 247 248 /// The first ID number we can use for our own identifiers. 249 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS; 250 251 /// The identifier ID that will be assigned to the next new identifier. 252 serialization::IdentID NextIdentID = FirstIdentID; 253 254 /// Map that provides the ID numbers of each identifier in 255 /// the output stream. 256 /// 257 /// The ID numbers for identifiers are consecutive (in order of 258 /// discovery), starting at 1. An ID of zero refers to a NULL 259 /// IdentifierInfo. 260 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs; 261 262 /// The first ID number we can use for our own macros. 263 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS; 264 265 /// The identifier ID that will be assigned to the next new identifier. 266 serialization::MacroID NextMacroID = FirstMacroID; 267 268 /// Map that provides the ID numbers of each macro. 269 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs; 270 271 struct MacroInfoToEmitData { 272 const IdentifierInfo *Name; 273 MacroInfo *MI; 274 serialization::MacroID ID; 275 }; 276 277 /// The macro infos to emit. 278 std::vector<MacroInfoToEmitData> MacroInfosToEmit; 279 280 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap; 281 282 /// @name FlushStmt Caches 283 /// @{ 284 285 /// Set of parent Stmts for the currently serializing sub-stmt. 286 llvm::DenseSet<Stmt *> ParentStmts; 287 288 /// Offsets of sub-stmts already serialized. The offset points 289 /// just after the stmt record. 290 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries; 291 292 /// @} 293 294 /// Offsets of each of the identifier IDs into the identifier 295 /// table. 296 std::vector<uint32_t> IdentifierOffsets; 297 298 /// The first ID number we can use for our own submodules. 299 serialization::SubmoduleID FirstSubmoduleID = 300 serialization::NUM_PREDEF_SUBMODULE_IDS; 301 302 /// The submodule ID that will be assigned to the next new submodule. 303 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID; 304 305 /// The first ID number we can use for our own selectors. 306 serialization::SelectorID FirstSelectorID = 307 serialization::NUM_PREDEF_SELECTOR_IDS; 308 309 /// The selector ID that will be assigned to the next new selector. 310 serialization::SelectorID NextSelectorID = FirstSelectorID; 311 312 /// Map that provides the ID numbers of each Selector. 313 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs; 314 315 /// Offset of each selector within the method pool/selector 316 /// table, indexed by the Selector ID (-1). 317 std::vector<uint32_t> SelectorOffsets; 318 319 /// Mapping from macro definitions (as they occur in the preprocessing 320 /// record) to the macro IDs. 321 llvm::DenseMap<const MacroDefinitionRecord *, 322 serialization::PreprocessedEntityID> MacroDefinitions; 323 324 /// Cache of indices of anonymous declarations within their lexical 325 /// contexts. 326 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers; 327 328 /// An update to a Decl. 329 class DeclUpdate { 330 /// A DeclUpdateKind. 331 unsigned Kind; 332 union { 333 const Decl *Dcl; 334 void *Type; 335 unsigned Loc; 336 unsigned Val; 337 Module *Mod; 338 const Attr *Attribute; 339 }; 340 341 public: 342 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {} 343 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {} 344 DeclUpdate(unsigned Kind, QualType Type) 345 : Kind(Kind), Type(Type.getAsOpaquePtr()) {} 346 DeclUpdate(unsigned Kind, SourceLocation Loc) 347 : Kind(Kind), Loc(Loc.getRawEncoding()) {} 348 DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {} 349 DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {} 350 DeclUpdate(unsigned Kind, const Attr *Attribute) 351 : Kind(Kind), Attribute(Attribute) {} 352 353 unsigned getKind() const { return Kind; } 354 const Decl *getDecl() const { return Dcl; } 355 QualType getType() const { return QualType::getFromOpaquePtr(Type); } 356 357 SourceLocation getLoc() const { 358 return SourceLocation::getFromRawEncoding(Loc); 359 } 360 361 unsigned getNumber() const { return Val; } 362 Module *getModule() const { return Mod; } 363 const Attr *getAttr() const { return Attribute; } 364 }; 365 366 using UpdateRecord = SmallVector<DeclUpdate, 1>; 367 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>; 368 369 /// Mapping from declarations that came from a chained PCH to the 370 /// record containing modifications to them. 371 DeclUpdateMap DeclUpdates; 372 373 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>; 374 375 /// Map of first declarations from a chained PCH that point to the 376 /// most recent declarations in another PCH. 377 FirstLatestDeclMap FirstLatestDecls; 378 379 /// Declarations encountered that might be external 380 /// definitions. 381 /// 382 /// We keep track of external definitions and other 'interesting' declarations 383 /// as we are emitting declarations to the AST file. The AST file contains a 384 /// separate record for these declarations, which are provided to the AST 385 /// consumer by the AST reader. This is behavior is required to properly cope with, 386 /// e.g., tentative variable definitions that occur within 387 /// headers. The declarations themselves are stored as declaration 388 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS 389 /// record. 390 SmallVector<uint64_t, 16> EagerlyDeserializedDecls; 391 SmallVector<uint64_t, 16> ModularCodegenDecls; 392 393 /// DeclContexts that have received extensions since their serialized 394 /// form. 395 /// 396 /// For namespaces, when we're chaining and encountering a namespace, we check 397 /// if its primary namespace comes from the chain. If it does, we add the 398 /// primary to this set, so that we can write out lexical content updates for 399 /// it. 400 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts; 401 402 /// Keeps track of declarations that we must emit, even though we're 403 /// not guaranteed to be able to find them by walking the AST starting at the 404 /// translation unit. 405 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced; 406 407 /// The set of Objective-C class that have categories we 408 /// should serialize. 409 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories; 410 411 /// The set of declarations that may have redeclaration chains that 412 /// need to be serialized. 413 llvm::SmallVector<const Decl *, 16> Redeclarations; 414 415 /// A cache of the first local declaration for "interesting" 416 /// redeclaration chains. 417 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache; 418 419 /// Mapping from SwitchCase statements to IDs. 420 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs; 421 422 /// The number of statements written to the AST file. 423 unsigned NumStatements = 0; 424 425 /// The number of macros written to the AST file. 426 unsigned NumMacros = 0; 427 428 /// The number of lexical declcontexts written to the AST 429 /// file. 430 unsigned NumLexicalDeclContexts = 0; 431 432 /// The number of visible declcontexts written to the AST 433 /// file. 434 unsigned NumVisibleDeclContexts = 0; 435 436 /// A mapping from each known submodule to its ID number, which will 437 /// be a positive integer. 438 llvm::DenseMap<Module *, unsigned> SubmoduleIDs; 439 440 /// A list of the module file extension writers. 441 std::vector<std::unique_ptr<ModuleFileExtensionWriter>> 442 ModuleFileExtensionWriters; 443 444 /// Retrieve or create a submodule ID for this module. 445 unsigned getSubmoduleID(Module *Mod); 446 447 /// Write the given subexpression to the bitstream. 448 void WriteSubStmt(Stmt *S); 449 450 void WriteBlockInfoBlock(); 451 void WriteControlBlock(Preprocessor &PP, ASTContext &Context, 452 StringRef isysroot, const std::string &OutputFile); 453 454 /// Write out the signature and diagnostic options, and return the signature. 455 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP, 456 ASTContext &Context); 457 458 /// Calculate hash of the pcm content. 459 static ASTFileSignature createSignature(StringRef Bytes); 460 461 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, 462 bool Modules); 463 void WriteSourceManagerBlock(SourceManager &SourceMgr, 464 const Preprocessor &PP); 465 void WritePreprocessor(const Preprocessor &PP, bool IsModule); 466 void WriteHeaderSearch(const HeaderSearch &HS); 467 void WritePreprocessorDetail(PreprocessingRecord &PPRec); 468 void WriteSubmodules(Module *WritingModule); 469 470 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, 471 bool isModule); 472 473 unsigned TypeExtQualAbbrev = 0; 474 unsigned TypeFunctionProtoAbbrev = 0; 475 void WriteTypeAbbrevs(); 476 void WriteType(QualType T); 477 478 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC); 479 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC); 480 481 void GenerateNameLookupTable(const DeclContext *DC, 482 llvm::SmallVectorImpl<char> &LookupTable); 483 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC); 484 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC); 485 void WriteTypeDeclOffsets(); 486 void WriteFileDeclIDsMap(); 487 void WriteComments(); 488 void WriteSelectors(Sema &SemaRef); 489 void WriteReferencedSelectorsPool(Sema &SemaRef); 490 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver, 491 bool IsModule); 492 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord); 493 void WriteDeclContextVisibleUpdate(const DeclContext *DC); 494 void WriteFPPragmaOptions(const FPOptions &Opts); 495 void WriteOpenCLExtensions(Sema &SemaRef); 496 void WriteOpenCLExtensionTypes(Sema &SemaRef); 497 void WriteOpenCLExtensionDecls(Sema &SemaRef); 498 void WriteCUDAPragmas(Sema &SemaRef); 499 void WriteObjCCategories(); 500 void WriteLateParsedTemplates(Sema &SemaRef); 501 void WriteOptimizePragmaOptions(Sema &SemaRef); 502 void WriteMSStructPragmaOptions(Sema &SemaRef); 503 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef); 504 void WritePackPragmaOptions(Sema &SemaRef); 505 void WriteModuleFileExtension(Sema &SemaRef, 506 ModuleFileExtensionWriter &Writer); 507 508 unsigned DeclParmVarAbbrev = 0; 509 unsigned DeclContextLexicalAbbrev = 0; 510 unsigned DeclContextVisibleLookupAbbrev = 0; 511 unsigned UpdateVisibleAbbrev = 0; 512 unsigned DeclRecordAbbrev = 0; 513 unsigned DeclTypedefAbbrev = 0; 514 unsigned DeclVarAbbrev = 0; 515 unsigned DeclFieldAbbrev = 0; 516 unsigned DeclEnumAbbrev = 0; 517 unsigned DeclObjCIvarAbbrev = 0; 518 unsigned DeclCXXMethodAbbrev = 0; 519 520 unsigned DeclRefExprAbbrev = 0; 521 unsigned CharacterLiteralAbbrev = 0; 522 unsigned IntegerLiteralAbbrev = 0; 523 unsigned ExprImplicitCastAbbrev = 0; 524 525 void WriteDeclAbbrevs(); 526 void WriteDecl(ASTContext &Context, Decl *D); 527 528 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot, 529 const std::string &OutputFile, 530 Module *WritingModule); 531 532public: 533 /// Create a new precompiled header writer that outputs to 534 /// the given bitstream. 535 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer, 536 InMemoryModuleCache &ModuleCache, 537 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 538 bool IncludeTimestamps = true); 539 ~ASTWriter() override; 540 541 const LangOptions &getLangOpts() const; 542 543 /// Get a timestamp for output into the AST file. The actual timestamp 544 /// of the specified file may be ignored if we have been instructed to not 545 /// include timestamps in the output file. 546 time_t getTimestampForOutput(const FileEntry *E) const; 547 548 /// Write a precompiled header for the given semantic analysis. 549 /// 550 /// \param SemaRef a reference to the semantic analysis object that processed 551 /// the AST to be written into the precompiled header. 552 /// 553 /// \param WritingModule The module that we are writing. If null, we are 554 /// writing a precompiled header. 555 /// 556 /// \param isysroot if non-empty, write a relocatable file whose headers 557 /// are relative to the given system root. If we're writing a module, its 558 /// build directory will be used in preference to this if both are available. 559 /// 560 /// \return the module signature, which eventually will be a hash of 561 /// the module but currently is merely a random 32-bit number. 562 ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, 563 Module *WritingModule, StringRef isysroot, 564 bool hasErrors = false, 565 bool ShouldCacheASTInMemory = false); 566 567 /// Emit a token. 568 void AddToken(const Token &Tok, RecordDataImpl &Record); 569 570 /// Emit a source location. 571 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record); 572 573 /// Emit a source range. 574 void AddSourceRange(SourceRange Range, RecordDataImpl &Record); 575 576 /// Emit a reference to an identifier. 577 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); 578 579 /// Get the unique number used to refer to the given selector. 580 serialization::SelectorID getSelectorRef(Selector Sel); 581 582 /// Get the unique number used to refer to the given identifier. 583 serialization::IdentID getIdentifierRef(const IdentifierInfo *II); 584 585 /// Get the unique number used to refer to the given macro. 586 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name); 587 588 /// Determine the ID of an already-emitted macro. 589 serialization::MacroID getMacroID(MacroInfo *MI); 590 591 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name); 592 593 /// Emit a reference to a type. 594 void AddTypeRef(QualType T, RecordDataImpl &Record); 595 596 /// Force a type to be emitted and get its ID. 597 serialization::TypeID GetOrCreateTypeID(QualType T); 598 599 /// Determine the type ID of an already-emitted type. 600 serialization::TypeID getTypeID(QualType T) const; 601 602 /// Find the first local declaration of a given local redeclarable 603 /// decl. 604 const Decl *getFirstLocalDecl(const Decl *D); 605 606 /// Is this a local declaration (that is, one that will be written to 607 /// our AST file)? This is the case for declarations that are neither imported 608 /// from another AST file nor predefined. 609 bool IsLocalDecl(const Decl *D) { 610 if (D->isFromASTFile()) 611 return false; 612 auto I = DeclIDs.find(D); 613 return (I == DeclIDs.end() || 614 I->second >= serialization::NUM_PREDEF_DECL_IDS); 615 }; 616 617 /// Emit a reference to a declaration. 618 void AddDeclRef(const Decl *D, RecordDataImpl &Record); 619 620 /// Force a declaration to be emitted and get its ID. 621 serialization::DeclID GetDeclRef(const Decl *D); 622 623 /// Determine the declaration ID of an already-emitted 624 /// declaration. 625 serialization::DeclID getDeclID(const Decl *D); 626 627 unsigned getAnonymousDeclarationNumber(const NamedDecl *D); 628 629 /// Add a string to the given record. 630 void AddString(StringRef Str, RecordDataImpl &Record); 631 632 /// Convert a path from this build process into one that is appropriate 633 /// for emission in the module file. 634 bool PreparePathForOutput(SmallVectorImpl<char> &Path); 635 636 /// Add a path to the given record. 637 void AddPath(StringRef Path, RecordDataImpl &Record); 638 639 /// Emit the current record with the given path as a blob. 640 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, 641 StringRef Path); 642 643 /// Add a version tuple to the given record 644 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record); 645 646 /// Retrieve or create a submodule ID for this module, or return 0 if 647 /// the submodule is neither local (a submodle of the currently-written module) 648 /// nor from an imported module. 649 unsigned getLocalOrImportedSubmoduleID(Module *Mod); 650 651 /// Note that the identifier II occurs at the given offset 652 /// within the identifier table. 653 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset); 654 655 /// Note that the selector Sel occurs at the given offset 656 /// within the method pool/selector table. 657 void SetSelectorOffset(Selector Sel, uint32_t Offset); 658 659 /// Record an ID for the given switch-case statement. 660 unsigned RecordSwitchCaseID(SwitchCase *S); 661 662 /// Retrieve the ID for the given switch-case statement. 663 unsigned getSwitchCaseID(SwitchCase *S); 664 665 void ClearSwitchCaseIDs(); 666 667 unsigned getTypeExtQualAbbrev() const { 668 return TypeExtQualAbbrev; 669 } 670 671 unsigned getTypeFunctionProtoAbbrev() const { 672 return TypeFunctionProtoAbbrev; 673 } 674 675 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; } 676 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; } 677 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; } 678 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; } 679 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; } 680 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; } 681 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; } 682 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; } 683 684 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; } 685 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; } 686 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; } 687 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; } 688 689 bool hasChain() const { return Chain; } 690 ASTReader *getChain() const { return Chain; } 691 692private: 693 // ASTDeserializationListener implementation 694 void ReaderInitialized(ASTReader *Reader) override; 695 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override; 696 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override; 697 void TypeRead(serialization::TypeIdx Idx, QualType T) override; 698 void SelectorRead(serialization::SelectorID ID, Selector Sel) override; 699 void MacroDefinitionRead(serialization::PreprocessedEntityID ID, 700 MacroDefinitionRecord *MD) override; 701 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override; 702 703 // ASTMutationListener implementation. 704 void CompletedTagDefinition(const TagDecl *D) override; 705 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override; 706 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override; 707 void AddedCXXTemplateSpecialization( 708 const ClassTemplateDecl *TD, 709 const ClassTemplateSpecializationDecl *D) override; 710 void AddedCXXTemplateSpecialization( 711 const VarTemplateDecl *TD, 712 const VarTemplateSpecializationDecl *D) override; 713 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 714 const FunctionDecl *D) override; 715 void ResolvedExceptionSpec(const FunctionDecl *FD) override; 716 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override; 717 void ResolvedOperatorDelete(const CXXDestructorDecl *DD, 718 const FunctionDecl *Delete, 719 Expr *ThisArg) override; 720 void CompletedImplicitDefinition(const FunctionDecl *D) override; 721 void InstantiationRequested(const ValueDecl *D) override; 722 void VariableDefinitionInstantiated(const VarDecl *D) override; 723 void FunctionDefinitionInstantiated(const FunctionDecl *D) override; 724 void DefaultArgumentInstantiated(const ParmVarDecl *D) override; 725 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override; 726 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 727 const ObjCInterfaceDecl *IFD) override; 728 void DeclarationMarkedUsed(const Decl *D) override; 729 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override; 730 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, 731 const Attr *Attr) override; 732 void DeclarationMarkedOpenMPAllocate(const Decl *D, const Attr *A) override; 733 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override; 734 void AddedAttributeToRecord(const Attr *Attr, 735 const RecordDecl *Record) override; 736}; 737 738/// AST and semantic-analysis consumer that generates a 739/// precompiled header from the parsed source code. 740class PCHGenerator : public SemaConsumer { 741 const Preprocessor &PP; 742 std::string OutputFile; 743 std::string isysroot; 744 Sema *SemaPtr; 745 std::shared_ptr<PCHBuffer> Buffer; 746 llvm::BitstreamWriter Stream; 747 ASTWriter Writer; 748 bool AllowASTWithErrors; 749 bool ShouldCacheASTInMemory; 750 751protected: 752 ASTWriter &getWriter() { return Writer; } 753 const ASTWriter &getWriter() const { return Writer; } 754 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; } 755 756public: 757 PCHGenerator(const Preprocessor &PP, InMemoryModuleCache &ModuleCache, 758 StringRef OutputFile, StringRef isysroot, 759 std::shared_ptr<PCHBuffer> Buffer, 760 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 761 bool AllowASTWithErrors = false, bool IncludeTimestamps = true, 762 bool ShouldCacheASTInMemory = false); 763 ~PCHGenerator() override; 764 765 void InitializeSema(Sema &S) override { SemaPtr = &S; } 766 void HandleTranslationUnit(ASTContext &Ctx) override; 767 ASTMutationListener *GetASTMutationListener() override; 768 ASTDeserializationListener *GetASTDeserializationListener() override; 769 bool hasEmittedPCH() const { return Buffer->IsComplete; } 770}; 771 772} // namespace clang 773 774#endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H 775