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