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