1 //===- ASTWriter.h - AST File Writer ----------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the ASTWriter class, which writes an AST file 11 // containing a serialized representation of a translation unit. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #ifndef LLVM_CLANG_SERIALIZATION_ASTWRITER_H 16 #define LLVM_CLANG_SERIALIZATION_ASTWRITER_H 17 18 #include "clang/AST/ASTMutationListener.h" 19 #include "clang/AST/Decl.h" 20 #include "clang/AST/DeclarationName.h" 21 #include "clang/AST/NestedNameSpecifier.h" 22 #include "clang/AST/OpenMPClause.h" 23 #include "clang/AST/TemplateBase.h" 24 #include "clang/AST/TemplateName.h" 25 #include "clang/AST/Type.h" 26 #include "clang/AST/TypeLoc.h" 27 #include "clang/Basic/LLVM.h" 28 #include "clang/Basic/SourceLocation.h" 29 #include "clang/Sema/SemaConsumer.h" 30 #include "clang/Serialization/ASTBitCodes.h" 31 #include "clang/Serialization/ASTDeserializationListener.h" 32 #include "clang/Serialization/PCHContainerOperations.h" 33 #include "llvm/ADT/ArrayRef.h" 34 #include "llvm/ADT/DenseMap.h" 35 #include "llvm/ADT/DenseSet.h" 36 #include "llvm/ADT/MapVector.h" 37 #include "llvm/ADT/SetVector.h" 38 #include "llvm/ADT/SmallVector.h" 39 #include "llvm/ADT/StringRef.h" 40 #include "llvm/Bitcode/BitstreamWriter.h" 41 #include <cassert> 42 #include <cstddef> 43 #include <cstdint> 44 #include <ctime> 45 #include <memory> 46 #include <queue> 47 #include <string> 48 #include <utility> 49 #include <vector> 50 51 namespace llvm { 52 53 class APFloat; 54 class APInt; 55 class APSInt; 56 57 } // namespace llvm 58 59 namespace clang { 60 61 class ASTContext; 62 class ASTReader; 63 class ASTUnresolvedSet; 64 class Attr; 65 class CXXBaseSpecifier; 66 class CXXCtorInitializer; 67 class CXXRecordDecl; 68 class CXXTemporary; 69 class FileEntry; 70 class FPOptions; 71 class FunctionDecl; 72 class HeaderSearch; 73 class HeaderSearchOptions; 74 class IdentifierResolver; 75 class LangOptions; 76 class MacroDefinitionRecord; 77 class MacroInfo; 78 class MemoryBufferCache; 79 class Module; 80 class ModuleFileExtension; 81 class ModuleFileExtensionWriter; 82 class NamedDecl; 83 class NestedNameSpecifier; 84 class ObjCInterfaceDecl; 85 class PreprocessingRecord; 86 class Preprocessor; 87 struct QualifierInfo; 88 class RecordDecl; 89 class Sema; 90 class SourceManager; 91 class Stmt; 92 struct StoredDeclsList; 93 class SwitchCase; 94 class TemplateParameterList; 95 class Token; 96 class TypeSourceInfo; 97 98 /// Writes an AST file containing the contents of a translation unit. 99 /// 100 /// The ASTWriter class produces a bitstream containing the serialized 101 /// representation of a given abstract syntax tree and its supporting 102 /// data structures. This bitstream can be de-serialized via an 103 /// instance of the ASTReader class. 104 class ASTWriter : public ASTDeserializationListener, 105 public ASTMutationListener { 106 public: 107 friend class ASTDeclWriter; 108 friend class ASTRecordWriter; 109 friend class ASTStmtWriter; 110 friend class ASTTypeWriter; 111 112 using RecordData = SmallVector<uint64_t, 64>; 113 using RecordDataImpl = SmallVectorImpl<uint64_t>; 114 using RecordDataRef = ArrayRef<uint64_t>; 115 116 private: 117 /// Map that provides the ID numbers of each type within the 118 /// output stream, plus those deserialized from a chained PCH. 119 /// 120 /// The ID numbers of types are consecutive (in order of discovery) 121 /// and start at 1. 0 is reserved for NULL. When types are actually 122 /// stored in the stream, the ID number is shifted by 2 bits to 123 /// allow for the const/volatile qualifiers. 124 /// 125 /// Keys in the map never have const/volatile qualifiers. 126 using TypeIdxMap = llvm::DenseMap<QualType, serialization::TypeIdx, 127 serialization::UnsafeQualTypeDenseMapInfo>; 128 129 /// The bitstream writer used to emit this precompiled header. 130 llvm::BitstreamWriter &Stream; 131 132 /// The buffer associated with the bitstream. 133 const SmallVectorImpl<char> &Buffer; 134 135 /// The PCM manager which manages memory buffers for pcm files. 136 MemoryBufferCache &PCMCache; 137 138 /// The ASTContext we're writing. 139 ASTContext *Context = nullptr; 140 141 /// The preprocessor we're writing. 142 Preprocessor *PP = nullptr; 143 144 /// The reader of existing AST files, if we're chaining. 145 ASTReader *Chain = nullptr; 146 147 /// The module we're currently writing, if any. 148 Module *WritingModule = nullptr; 149 150 /// The base directory for any relative paths we emit. 151 std::string BaseDirectory; 152 153 /// Indicates whether timestamps should be written to the produced 154 /// module file. This is the case for files implicitly written to the 155 /// module cache, where we need the timestamps to determine if the module 156 /// file is up to date, but not otherwise. 157 bool IncludeTimestamps; 158 159 /// Indicates when the AST writing is actively performing 160 /// serialization, rather than just queueing updates. 161 bool WritingAST = false; 162 163 /// Indicates that we are done serializing the collection of decls 164 /// and types to emit. 165 bool DoneWritingDeclsAndTypes = false; 166 167 /// Indicates that the AST contained compiler errors. 168 bool ASTHasCompilerErrors = false; 169 170 /// Mapping from input file entries to the index into the 171 /// offset table where information about that input file is stored. 172 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs; 173 174 /// Stores a declaration or a type to be written to the AST file. 175 class DeclOrType { 176 public: DeclOrType(Decl * D)177 DeclOrType(Decl *D) : Stored(D), IsType(false) {} DeclOrType(QualType T)178 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) {} 179 isType()180 bool isType() const { return IsType; } isDecl()181 bool isDecl() const { return !IsType; } 182 getType()183 QualType getType() const { 184 assert(isType() && "Not a type!"); 185 return QualType::getFromOpaquePtr(Stored); 186 } 187 getDecl()188 Decl *getDecl() const { 189 assert(isDecl() && "Not a decl!"); 190 return static_cast<Decl *>(Stored); 191 } 192 193 private: 194 void *Stored; 195 bool IsType; 196 }; 197 198 /// The declarations and types to emit. 199 std::queue<DeclOrType> DeclTypesToEmit; 200 201 /// The first ID number we can use for our own declarations. 202 serialization::DeclID FirstDeclID = serialization::NUM_PREDEF_DECL_IDS; 203 204 /// The decl ID that will be assigned to the next new decl. 205 serialization::DeclID NextDeclID = FirstDeclID; 206 207 /// Map that provides the ID numbers of each declaration within 208 /// the output stream, as well as those deserialized from a chained PCH. 209 /// 210 /// The ID numbers of declarations are consecutive (in order of 211 /// discovery) and start at 2. 1 is reserved for the translation 212 /// unit, while 0 is reserved for NULL. 213 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs; 214 215 /// Offset of each declaration in the bitstream, indexed by 216 /// the declaration's ID. 217 std::vector<serialization::DeclOffset> DeclOffsets; 218 219 /// Sorted (by file offset) vector of pairs of file offset/DeclID. 220 using LocDeclIDsTy = 221 SmallVector<std::pair<unsigned, serialization::DeclID>, 64>; 222 struct DeclIDInFileInfo { 223 LocDeclIDsTy DeclIDs; 224 225 /// Set when the DeclIDs vectors from all files are joined, this 226 /// indicates the index that this particular vector has in the global one. 227 unsigned FirstDeclIndex; 228 }; 229 using FileDeclIDsTy = llvm::DenseMap<FileID, DeclIDInFileInfo *>; 230 231 /// Map from file SLocEntries to info about the file-level declarations 232 /// that it contains. 233 FileDeclIDsTy FileDeclIDs; 234 235 void associateDeclWithFile(const Decl *D, serialization::DeclID); 236 237 /// The first ID number we can use for our own types. 238 serialization::TypeID FirstTypeID = serialization::NUM_PREDEF_TYPE_IDS; 239 240 /// The type ID that will be assigned to the next new type. 241 serialization::TypeID NextTypeID = FirstTypeID; 242 243 /// Map that provides the ID numbers of each type within the 244 /// output stream, plus those deserialized from a chained PCH. 245 /// 246 /// The ID numbers of types are consecutive (in order of discovery) 247 /// and start at 1. 0 is reserved for NULL. When types are actually 248 /// stored in the stream, the ID number is shifted by 2 bits to 249 /// allow for the const/volatile qualifiers. 250 /// 251 /// Keys in the map never have const/volatile qualifiers. 252 TypeIdxMap TypeIdxs; 253 254 /// Offset of each type in the bitstream, indexed by 255 /// the type's ID. 256 std::vector<uint32_t> TypeOffsets; 257 258 /// The first ID number we can use for our own identifiers. 259 serialization::IdentID FirstIdentID = serialization::NUM_PREDEF_IDENT_IDS; 260 261 /// The identifier ID that will be assigned to the next new identifier. 262 serialization::IdentID NextIdentID = FirstIdentID; 263 264 /// Map that provides the ID numbers of each identifier in 265 /// the output stream. 266 /// 267 /// The ID numbers for identifiers are consecutive (in order of 268 /// discovery), starting at 1. An ID of zero refers to a NULL 269 /// IdentifierInfo. 270 llvm::MapVector<const IdentifierInfo *, serialization::IdentID> IdentifierIDs; 271 272 /// The first ID number we can use for our own macros. 273 serialization::MacroID FirstMacroID = serialization::NUM_PREDEF_MACRO_IDS; 274 275 /// The identifier ID that will be assigned to the next new identifier. 276 serialization::MacroID NextMacroID = FirstMacroID; 277 278 /// Map that provides the ID numbers of each macro. 279 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs; 280 281 struct MacroInfoToEmitData { 282 const IdentifierInfo *Name; 283 MacroInfo *MI; 284 serialization::MacroID ID; 285 }; 286 287 /// The macro infos to emit. 288 std::vector<MacroInfoToEmitData> MacroInfosToEmit; 289 290 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap; 291 292 /// @name FlushStmt Caches 293 /// @{ 294 295 /// Set of parent Stmts for the currently serializing sub-stmt. 296 llvm::DenseSet<Stmt *> ParentStmts; 297 298 /// Offsets of sub-stmts already serialized. The offset points 299 /// just after the stmt record. 300 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries; 301 302 /// @} 303 304 /// Offsets of each of the identifier IDs into the identifier 305 /// table. 306 std::vector<uint32_t> IdentifierOffsets; 307 308 /// The first ID number we can use for our own submodules. 309 serialization::SubmoduleID FirstSubmoduleID = 310 serialization::NUM_PREDEF_SUBMODULE_IDS; 311 312 /// The submodule ID that will be assigned to the next new submodule. 313 serialization::SubmoduleID NextSubmoduleID = FirstSubmoduleID; 314 315 /// The first ID number we can use for our own selectors. 316 serialization::SelectorID FirstSelectorID = 317 serialization::NUM_PREDEF_SELECTOR_IDS; 318 319 /// The selector ID that will be assigned to the next new selector. 320 serialization::SelectorID NextSelectorID = FirstSelectorID; 321 322 /// Map that provides the ID numbers of each Selector. 323 llvm::MapVector<Selector, serialization::SelectorID> SelectorIDs; 324 325 /// Offset of each selector within the method pool/selector 326 /// table, indexed by the Selector ID (-1). 327 std::vector<uint32_t> SelectorOffsets; 328 329 /// Mapping from macro definitions (as they occur in the preprocessing 330 /// record) to the macro IDs. 331 llvm::DenseMap<const MacroDefinitionRecord *, 332 serialization::PreprocessedEntityID> MacroDefinitions; 333 334 /// Cache of indices of anonymous declarations within their lexical 335 /// contexts. 336 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers; 337 338 /// An update to a Decl. 339 class DeclUpdate { 340 /// A DeclUpdateKind. 341 unsigned Kind; 342 union { 343 const Decl *Dcl; 344 void *Type; 345 unsigned Loc; 346 unsigned Val; 347 Module *Mod; 348 const Attr *Attribute; 349 }; 350 351 public: DeclUpdate(unsigned Kind)352 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {} DeclUpdate(unsigned Kind,const Decl * Dcl)353 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {} DeclUpdate(unsigned Kind,QualType Type)354 DeclUpdate(unsigned Kind, QualType Type) 355 : Kind(Kind), Type(Type.getAsOpaquePtr()) {} DeclUpdate(unsigned Kind,SourceLocation Loc)356 DeclUpdate(unsigned Kind, SourceLocation Loc) 357 : Kind(Kind), Loc(Loc.getRawEncoding()) {} DeclUpdate(unsigned Kind,unsigned Val)358 DeclUpdate(unsigned Kind, unsigned Val) : Kind(Kind), Val(Val) {} DeclUpdate(unsigned Kind,Module * M)359 DeclUpdate(unsigned Kind, Module *M) : Kind(Kind), Mod(M) {} DeclUpdate(unsigned Kind,const Attr * Attribute)360 DeclUpdate(unsigned Kind, const Attr *Attribute) 361 : Kind(Kind), Attribute(Attribute) {} 362 getKind()363 unsigned getKind() const { return Kind; } getDecl()364 const Decl *getDecl() const { return Dcl; } getType()365 QualType getType() const { return QualType::getFromOpaquePtr(Type); } 366 getLoc()367 SourceLocation getLoc() const { 368 return SourceLocation::getFromRawEncoding(Loc); 369 } 370 getNumber()371 unsigned getNumber() const { return Val; } getModule()372 Module *getModule() const { return Mod; } getAttr()373 const Attr *getAttr() const { return Attribute; } 374 }; 375 376 using UpdateRecord = SmallVector<DeclUpdate, 1>; 377 using DeclUpdateMap = llvm::MapVector<const Decl *, UpdateRecord>; 378 379 /// Mapping from declarations that came from a chained PCH to the 380 /// record containing modifications to them. 381 DeclUpdateMap DeclUpdates; 382 383 using FirstLatestDeclMap = llvm::DenseMap<Decl *, Decl *>; 384 385 /// Map of first declarations from a chained PCH that point to the 386 /// most recent declarations in another PCH. 387 FirstLatestDeclMap FirstLatestDecls; 388 389 /// Declarations encountered that might be external 390 /// definitions. 391 /// 392 /// We keep track of external definitions and other 'interesting' declarations 393 /// as we are emitting declarations to the AST file. The AST file contains a 394 /// separate record for these declarations, which are provided to the AST 395 /// consumer by the AST reader. This is behavior is required to properly cope with, 396 /// e.g., tentative variable definitions that occur within 397 /// headers. The declarations themselves are stored as declaration 398 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS 399 /// record. 400 SmallVector<uint64_t, 16> EagerlyDeserializedDecls; 401 SmallVector<uint64_t, 16> ModularCodegenDecls; 402 403 /// DeclContexts that have received extensions since their serialized 404 /// form. 405 /// 406 /// For namespaces, when we're chaining and encountering a namespace, we check 407 /// if its primary namespace comes from the chain. If it does, we add the 408 /// primary to this set, so that we can write out lexical content updates for 409 /// it. 410 llvm::SmallSetVector<const DeclContext *, 16> UpdatedDeclContexts; 411 412 /// Keeps track of declarations that we must emit, even though we're 413 /// not guaranteed to be able to find them by walking the AST starting at the 414 /// translation unit. 415 SmallVector<const Decl *, 16> DeclsToEmitEvenIfUnreferenced; 416 417 /// The set of Objective-C class that have categories we 418 /// should serialize. 419 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories; 420 421 /// The set of declarations that may have redeclaration chains that 422 /// need to be serialized. 423 llvm::SmallVector<const Decl *, 16> Redeclarations; 424 425 /// A cache of the first local declaration for "interesting" 426 /// redeclaration chains. 427 llvm::DenseMap<const Decl *, const Decl *> FirstLocalDeclCache; 428 429 /// Mapping from SwitchCase statements to IDs. 430 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs; 431 432 /// The number of statements written to the AST file. 433 unsigned NumStatements = 0; 434 435 /// The number of macros written to the AST file. 436 unsigned NumMacros = 0; 437 438 /// The number of lexical declcontexts written to the AST 439 /// file. 440 unsigned NumLexicalDeclContexts = 0; 441 442 /// The number of visible declcontexts written to the AST 443 /// file. 444 unsigned NumVisibleDeclContexts = 0; 445 446 /// A mapping from each known submodule to its ID number, which will 447 /// be a positive integer. 448 llvm::DenseMap<Module *, unsigned> SubmoduleIDs; 449 450 /// A list of the module file extension writers. 451 std::vector<std::unique_ptr<ModuleFileExtensionWriter>> 452 ModuleFileExtensionWriters; 453 454 /// Retrieve or create a submodule ID for this module. 455 unsigned getSubmoduleID(Module *Mod); 456 457 /// Write the given subexpression to the bitstream. 458 void WriteSubStmt(Stmt *S); 459 460 void WriteBlockInfoBlock(); 461 void WriteControlBlock(Preprocessor &PP, ASTContext &Context, 462 StringRef isysroot, const std::string &OutputFile); 463 464 /// Write out the signature and diagnostic options, and return the signature. 465 ASTFileSignature writeUnhashedControlBlock(Preprocessor &PP, 466 ASTContext &Context); 467 468 /// Calculate hash of the pcm content. 469 static ASTFileSignature createSignature(StringRef Bytes); 470 471 void WriteInputFiles(SourceManager &SourceMgr, HeaderSearchOptions &HSOpts, 472 bool Modules); 473 void WriteSourceManagerBlock(SourceManager &SourceMgr, 474 const Preprocessor &PP); 475 void WritePreprocessor(const Preprocessor &PP, bool IsModule); 476 void WriteHeaderSearch(const HeaderSearch &HS); 477 void WritePreprocessorDetail(PreprocessingRecord &PPRec); 478 void WriteSubmodules(Module *WritingModule); 479 480 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, 481 bool isModule); 482 483 unsigned TypeExtQualAbbrev = 0; 484 unsigned TypeFunctionProtoAbbrev = 0; 485 void WriteTypeAbbrevs(); 486 void WriteType(QualType T); 487 488 bool isLookupResultExternal(StoredDeclsList &Result, DeclContext *DC); 489 bool isLookupResultEntirelyExternal(StoredDeclsList &Result, DeclContext *DC); 490 491 void GenerateNameLookupTable(const DeclContext *DC, 492 llvm::SmallVectorImpl<char> &LookupTable); 493 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC); 494 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC); 495 void WriteTypeDeclOffsets(); 496 void WriteFileDeclIDsMap(); 497 void WriteComments(); 498 void WriteSelectors(Sema &SemaRef); 499 void WriteReferencedSelectorsPool(Sema &SemaRef); 500 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver, 501 bool IsModule); 502 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord); 503 void WriteDeclContextVisibleUpdate(const DeclContext *DC); 504 void WriteFPPragmaOptions(const FPOptions &Opts); 505 void WriteOpenCLExtensions(Sema &SemaRef); 506 void WriteOpenCLExtensionTypes(Sema &SemaRef); 507 void WriteOpenCLExtensionDecls(Sema &SemaRef); 508 void WriteCUDAPragmas(Sema &SemaRef); 509 void WriteObjCCategories(); 510 void WriteLateParsedTemplates(Sema &SemaRef); 511 void WriteOptimizePragmaOptions(Sema &SemaRef); 512 void WriteMSStructPragmaOptions(Sema &SemaRef); 513 void WriteMSPointersToMembersPragmaOptions(Sema &SemaRef); 514 void WritePackPragmaOptions(Sema &SemaRef); 515 void WriteModuleFileExtension(Sema &SemaRef, 516 ModuleFileExtensionWriter &Writer); 517 518 unsigned DeclParmVarAbbrev = 0; 519 unsigned DeclContextLexicalAbbrev = 0; 520 unsigned DeclContextVisibleLookupAbbrev = 0; 521 unsigned UpdateVisibleAbbrev = 0; 522 unsigned DeclRecordAbbrev = 0; 523 unsigned DeclTypedefAbbrev = 0; 524 unsigned DeclVarAbbrev = 0; 525 unsigned DeclFieldAbbrev = 0; 526 unsigned DeclEnumAbbrev = 0; 527 unsigned DeclObjCIvarAbbrev = 0; 528 unsigned DeclCXXMethodAbbrev = 0; 529 530 unsigned DeclRefExprAbbrev = 0; 531 unsigned CharacterLiteralAbbrev = 0; 532 unsigned IntegerLiteralAbbrev = 0; 533 unsigned ExprImplicitCastAbbrev = 0; 534 535 void WriteDeclAbbrevs(); 536 void WriteDecl(ASTContext &Context, Decl *D); 537 538 ASTFileSignature WriteASTCore(Sema &SemaRef, StringRef isysroot, 539 const std::string &OutputFile, 540 Module *WritingModule); 541 542 public: 543 /// Create a new precompiled header writer that outputs to 544 /// the given bitstream. 545 ASTWriter(llvm::BitstreamWriter &Stream, SmallVectorImpl<char> &Buffer, 546 MemoryBufferCache &PCMCache, 547 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 548 bool IncludeTimestamps = true); 549 ~ASTWriter() override; 550 551 const LangOptions &getLangOpts() const; 552 553 /// Get a timestamp for output into the AST file. The actual timestamp 554 /// of the specified file may be ignored if we have been instructed to not 555 /// include timestamps in the output file. 556 time_t getTimestampForOutput(const FileEntry *E) const; 557 558 /// Write a precompiled header for the given semantic analysis. 559 /// 560 /// \param SemaRef a reference to the semantic analysis object that processed 561 /// the AST to be written into the precompiled header. 562 /// 563 /// \param WritingModule The module that we are writing. If null, we are 564 /// writing a precompiled header. 565 /// 566 /// \param isysroot if non-empty, write a relocatable file whose headers 567 /// are relative to the given system root. If we're writing a module, its 568 /// build directory will be used in preference to this if both are available. 569 /// 570 /// \return the module signature, which eventually will be a hash of 571 /// the module but currently is merely a random 32-bit number. 572 ASTFileSignature WriteAST(Sema &SemaRef, const std::string &OutputFile, 573 Module *WritingModule, StringRef isysroot, 574 bool hasErrors = false); 575 576 /// Emit a token. 577 void AddToken(const Token &Tok, RecordDataImpl &Record); 578 579 /// Emit a source location. 580 void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record); 581 582 /// Emit a source range. 583 void AddSourceRange(SourceRange Range, RecordDataImpl &Record); 584 585 /// Emit a reference to an identifier. 586 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); 587 588 /// Get the unique number used to refer to the given selector. 589 serialization::SelectorID getSelectorRef(Selector Sel); 590 591 /// Get the unique number used to refer to the given identifier. 592 serialization::IdentID getIdentifierRef(const IdentifierInfo *II); 593 594 /// Get the unique number used to refer to the given macro. 595 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name); 596 597 /// Determine the ID of an already-emitted macro. 598 serialization::MacroID getMacroID(MacroInfo *MI); 599 600 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name); 601 602 /// Emit a reference to a type. 603 void AddTypeRef(QualType T, RecordDataImpl &Record); 604 605 /// Force a type to be emitted and get its ID. 606 serialization::TypeID GetOrCreateTypeID(QualType T); 607 608 /// Determine the type ID of an already-emitted type. 609 serialization::TypeID getTypeID(QualType T) const; 610 611 /// Find the first local declaration of a given local redeclarable 612 /// decl. 613 const Decl *getFirstLocalDecl(const Decl *D); 614 615 /// Is this a local declaration (that is, one that will be written to 616 /// our AST file)? This is the case for declarations that are neither imported 617 /// from another AST file nor predefined. IsLocalDecl(const Decl * D)618 bool IsLocalDecl(const Decl *D) { 619 if (D->isFromASTFile()) 620 return false; 621 auto I = DeclIDs.find(D); 622 return (I == DeclIDs.end() || 623 I->second >= serialization::NUM_PREDEF_DECL_IDS); 624 }; 625 626 /// Emit a reference to a declaration. 627 void AddDeclRef(const Decl *D, RecordDataImpl &Record); 628 629 /// Force a declaration to be emitted and get its ID. 630 serialization::DeclID GetDeclRef(const Decl *D); 631 632 /// Determine the declaration ID of an already-emitted 633 /// declaration. 634 serialization::DeclID getDeclID(const Decl *D); 635 636 unsigned getAnonymousDeclarationNumber(const NamedDecl *D); 637 638 /// Add a string to the given record. 639 void AddString(StringRef Str, RecordDataImpl &Record); 640 641 /// Convert a path from this build process into one that is appropriate 642 /// for emission in the module file. 643 bool PreparePathForOutput(SmallVectorImpl<char> &Path); 644 645 /// Add a path to the given record. 646 void AddPath(StringRef Path, RecordDataImpl &Record); 647 648 /// Emit the current record with the given path as a blob. 649 void EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record, 650 StringRef Path); 651 652 /// Add a version tuple to the given record 653 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record); 654 655 /// Retrieve or create a submodule ID for this module, or return 0 if 656 /// the submodule is neither local (a submodle of the currently-written module) 657 /// nor from an imported module. 658 unsigned getLocalOrImportedSubmoduleID(Module *Mod); 659 660 /// Note that the identifier II occurs at the given offset 661 /// within the identifier table. 662 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset); 663 664 /// Note that the selector Sel occurs at the given offset 665 /// within the method pool/selector table. 666 void SetSelectorOffset(Selector Sel, uint32_t Offset); 667 668 /// Record an ID for the given switch-case statement. 669 unsigned RecordSwitchCaseID(SwitchCase *S); 670 671 /// Retrieve the ID for the given switch-case statement. 672 unsigned getSwitchCaseID(SwitchCase *S); 673 674 void ClearSwitchCaseIDs(); 675 getTypeExtQualAbbrev()676 unsigned getTypeExtQualAbbrev() const { 677 return TypeExtQualAbbrev; 678 } 679 getTypeFunctionProtoAbbrev()680 unsigned getTypeFunctionProtoAbbrev() const { 681 return TypeFunctionProtoAbbrev; 682 } 683 getDeclParmVarAbbrev()684 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; } getDeclRecordAbbrev()685 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; } getDeclTypedefAbbrev()686 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; } getDeclVarAbbrev()687 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; } getDeclFieldAbbrev()688 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; } getDeclEnumAbbrev()689 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; } getDeclObjCIvarAbbrev()690 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; } getDeclCXXMethodAbbrev()691 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; } 692 getDeclRefExprAbbrev()693 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; } getCharacterLiteralAbbrev()694 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; } getIntegerLiteralAbbrev()695 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; } getExprImplicitCastAbbrev()696 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; } 697 hasChain()698 bool hasChain() const { return Chain; } getChain()699 ASTReader *getChain() const { return Chain; } 700 701 private: 702 // ASTDeserializationListener implementation 703 void ReaderInitialized(ASTReader *Reader) override; 704 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override; 705 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override; 706 void TypeRead(serialization::TypeIdx Idx, QualType T) override; 707 void SelectorRead(serialization::SelectorID ID, Selector Sel) override; 708 void MacroDefinitionRead(serialization::PreprocessedEntityID ID, 709 MacroDefinitionRecord *MD) override; 710 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override; 711 712 // ASTMutationListener implementation. 713 void CompletedTagDefinition(const TagDecl *D) override; 714 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override; 715 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override; 716 void AddedCXXTemplateSpecialization( 717 const ClassTemplateDecl *TD, 718 const ClassTemplateSpecializationDecl *D) override; 719 void AddedCXXTemplateSpecialization( 720 const VarTemplateDecl *TD, 721 const VarTemplateSpecializationDecl *D) override; 722 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 723 const FunctionDecl *D) override; 724 void ResolvedExceptionSpec(const FunctionDecl *FD) override; 725 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override; 726 void ResolvedOperatorDelete(const CXXDestructorDecl *DD, 727 const FunctionDecl *Delete, 728 Expr *ThisArg) override; 729 void CompletedImplicitDefinition(const FunctionDecl *D) override; 730 void InstantiationRequested(const ValueDecl *D) override; 731 void VariableDefinitionInstantiated(const VarDecl *D) override; 732 void FunctionDefinitionInstantiated(const FunctionDecl *D) override; 733 void DefaultArgumentInstantiated(const ParmVarDecl *D) override; 734 void DefaultMemberInitializerInstantiated(const FieldDecl *D) override; 735 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 736 const ObjCInterfaceDecl *IFD) override; 737 void DeclarationMarkedUsed(const Decl *D) override; 738 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override; 739 void DeclarationMarkedOpenMPDeclareTarget(const Decl *D, 740 const Attr *Attr) override; 741 void RedefinedHiddenDefinition(const NamedDecl *D, Module *M) override; 742 void AddedAttributeToRecord(const Attr *Attr, 743 const RecordDecl *Record) override; 744 }; 745 746 /// An object for streaming information to a record. 747 class ASTRecordWriter { 748 ASTWriter *Writer; 749 ASTWriter::RecordDataImpl *Record; 750 751 /// Statements that we've encountered while serializing a 752 /// declaration or type. 753 SmallVector<Stmt *, 16> StmtsToEmit; 754 755 /// Indices of record elements that describe offsets within the 756 /// bitcode. These will be converted to offsets relative to the current 757 /// record when emitted. 758 SmallVector<unsigned, 8> OffsetIndices; 759 760 /// Flush all of the statements and expressions that have 761 /// been added to the queue via AddStmt(). 762 void FlushStmts(); 763 void FlushSubStmts(); 764 PrepareToEmit(uint64_t MyOffset)765 void PrepareToEmit(uint64_t MyOffset) { 766 // Convert offsets into relative form. 767 for (unsigned I : OffsetIndices) { 768 auto &StoredOffset = (*Record)[I]; 769 assert(StoredOffset < MyOffset && "invalid offset"); 770 if (StoredOffset) 771 StoredOffset = MyOffset - StoredOffset; 772 } 773 OffsetIndices.clear(); 774 } 775 776 public: 777 /// Construct a ASTRecordWriter that uses the default encoding scheme. ASTRecordWriter(ASTWriter & Writer,ASTWriter::RecordDataImpl & Record)778 ASTRecordWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record) 779 : Writer(&Writer), Record(&Record) {} 780 781 /// Construct a ASTRecordWriter that uses the same encoding scheme as another 782 /// ASTRecordWriter. ASTRecordWriter(ASTRecordWriter & Parent,ASTWriter::RecordDataImpl & Record)783 ASTRecordWriter(ASTRecordWriter &Parent, ASTWriter::RecordDataImpl &Record) 784 : Writer(Parent.Writer), Record(&Record) {} 785 786 /// Copying an ASTRecordWriter is almost certainly a bug. 787 ASTRecordWriter(const ASTRecordWriter &) = delete; 788 ASTRecordWriter &operator=(const ASTRecordWriter &) = delete; 789 790 /// Extract the underlying record storage. getRecordData()791 ASTWriter::RecordDataImpl &getRecordData() const { return *Record; } 792 793 /// Minimal vector-like interface. 794 /// @{ push_back(uint64_t N)795 void push_back(uint64_t N) { Record->push_back(N); } 796 template<typename InputIterator> append(InputIterator begin,InputIterator end)797 void append(InputIterator begin, InputIterator end) { 798 Record->append(begin, end); 799 } empty()800 bool empty() const { return Record->empty(); } size()801 size_t size() const { return Record->size(); } 802 uint64_t &operator[](size_t N) { return (*Record)[N]; } 803 /// @} 804 805 /// Emit the record to the stream, followed by its substatements, and 806 /// return its offset. 807 // FIXME: Allow record producers to suggest Abbrevs. 808 uint64_t Emit(unsigned Code, unsigned Abbrev = 0) { 809 uint64_t Offset = Writer->Stream.GetCurrentBitNo(); 810 PrepareToEmit(Offset); 811 Writer->Stream.EmitRecord(Code, *Record, Abbrev); 812 FlushStmts(); 813 return Offset; 814 } 815 816 /// Emit the record to the stream, preceded by its substatements. 817 uint64_t EmitStmt(unsigned Code, unsigned Abbrev = 0) { 818 FlushSubStmts(); 819 PrepareToEmit(Writer->Stream.GetCurrentBitNo()); 820 Writer->Stream.EmitRecord(Code, *Record, Abbrev); 821 return Writer->Stream.GetCurrentBitNo(); 822 } 823 824 /// Add a bit offset into the record. This will be converted into an 825 /// offset relative to the current record when emitted. AddOffset(uint64_t BitOffset)826 void AddOffset(uint64_t BitOffset) { 827 OffsetIndices.push_back(Record->size()); 828 Record->push_back(BitOffset); 829 } 830 831 /// Add the given statement or expression to the queue of 832 /// statements to emit. 833 /// 834 /// This routine should be used when emitting types and declarations 835 /// that have expressions as part of their formulation. Once the 836 /// type or declaration has been written, Emit() will write 837 /// the corresponding statements just after the record. AddStmt(Stmt * S)838 void AddStmt(Stmt *S) { 839 StmtsToEmit.push_back(S); 840 } 841 842 /// Add a definition for the given function to the queue of statements 843 /// to emit. 844 void AddFunctionDefinition(const FunctionDecl *FD); 845 846 /// Emit a source location. AddSourceLocation(SourceLocation Loc)847 void AddSourceLocation(SourceLocation Loc) { 848 return Writer->AddSourceLocation(Loc, *Record); 849 } 850 851 /// Emit a source range. AddSourceRange(SourceRange Range)852 void AddSourceRange(SourceRange Range) { 853 return Writer->AddSourceRange(Range, *Record); 854 } 855 856 /// Emit an integral value. 857 void AddAPInt(const llvm::APInt &Value); 858 859 /// Emit a signed integral value. 860 void AddAPSInt(const llvm::APSInt &Value); 861 862 /// Emit a floating-point value. 863 void AddAPFloat(const llvm::APFloat &Value); 864 865 /// Emit a reference to an identifier. AddIdentifierRef(const IdentifierInfo * II)866 void AddIdentifierRef(const IdentifierInfo *II) { 867 return Writer->AddIdentifierRef(II, *Record); 868 } 869 870 /// Emit a Selector (which is a smart pointer reference). 871 void AddSelectorRef(Selector S); 872 873 /// Emit a CXXTemporary. 874 void AddCXXTemporary(const CXXTemporary *Temp); 875 876 /// Emit a C++ base specifier. 877 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base); 878 879 /// Emit a set of C++ base specifiers. 880 void AddCXXBaseSpecifiers(ArrayRef<CXXBaseSpecifier> Bases); 881 882 /// Emit a reference to a type. AddTypeRef(QualType T)883 void AddTypeRef(QualType T) { 884 return Writer->AddTypeRef(T, *Record); 885 } 886 887 /// Emits a reference to a declarator info. 888 void AddTypeSourceInfo(TypeSourceInfo *TInfo); 889 890 /// Emits source location information for a type. Does not emit the type. 891 void AddTypeLoc(TypeLoc TL); 892 893 /// Emits a template argument location info. 894 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 895 const TemplateArgumentLocInfo &Arg); 896 897 /// Emits a template argument location. 898 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg); 899 900 /// Emits an AST template argument list info. 901 void AddASTTemplateArgumentListInfo( 902 const ASTTemplateArgumentListInfo *ASTTemplArgList); 903 904 /// Emit a reference to a declaration. AddDeclRef(const Decl * D)905 void AddDeclRef(const Decl *D) { 906 return Writer->AddDeclRef(D, *Record); 907 } 908 909 /// Emit a declaration name. 910 void AddDeclarationName(DeclarationName Name); 911 912 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 913 DeclarationName Name); 914 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo); 915 916 void AddQualifierInfo(const QualifierInfo &Info); 917 918 /// Emit a nested name specifier. 919 void AddNestedNameSpecifier(NestedNameSpecifier *NNS); 920 921 /// Emit a nested name specifier with source-location information. 922 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS); 923 924 /// Emit a template name. 925 void AddTemplateName(TemplateName Name); 926 927 /// Emit a template argument. 928 void AddTemplateArgument(const TemplateArgument &Arg); 929 930 /// Emit a template parameter list. 931 void AddTemplateParameterList(const TemplateParameterList *TemplateParams); 932 933 /// Emit a template argument list. 934 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs); 935 936 /// Emit a UnresolvedSet structure. 937 void AddUnresolvedSet(const ASTUnresolvedSet &Set); 938 939 /// Emit a CXXCtorInitializer array. 940 void AddCXXCtorInitializers(ArrayRef<CXXCtorInitializer *> CtorInits); 941 942 void AddCXXDefinitionData(const CXXRecordDecl *D); 943 944 /// Emit a string. AddString(StringRef Str)945 void AddString(StringRef Str) { 946 return Writer->AddString(Str, *Record); 947 } 948 949 /// Emit a path. AddPath(StringRef Path)950 void AddPath(StringRef Path) { 951 return Writer->AddPath(Path, *Record); 952 } 953 954 /// Emit a version tuple. AddVersionTuple(const VersionTuple & Version)955 void AddVersionTuple(const VersionTuple &Version) { 956 return Writer->AddVersionTuple(Version, *Record); 957 } 958 959 // Emit an attribute. 960 void AddAttr(const Attr *A); 961 962 /// Emit a list of attributes. 963 void AddAttributes(ArrayRef<const Attr*> Attrs); 964 }; 965 966 /// AST and semantic-analysis consumer that generates a 967 /// precompiled header from the parsed source code. 968 class PCHGenerator : public SemaConsumer { 969 const Preprocessor &PP; 970 std::string OutputFile; 971 std::string isysroot; 972 Sema *SemaPtr; 973 std::shared_ptr<PCHBuffer> Buffer; 974 llvm::BitstreamWriter Stream; 975 ASTWriter Writer; 976 bool AllowASTWithErrors; 977 978 protected: getWriter()979 ASTWriter &getWriter() { return Writer; } getWriter()980 const ASTWriter &getWriter() const { return Writer; } getPCH()981 SmallVectorImpl<char> &getPCH() const { return Buffer->Data; } 982 983 public: 984 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, StringRef isysroot, 985 std::shared_ptr<PCHBuffer> Buffer, 986 ArrayRef<std::shared_ptr<ModuleFileExtension>> Extensions, 987 bool AllowASTWithErrors = false, bool IncludeTimestamps = true); 988 ~PCHGenerator() override; 989 InitializeSema(Sema & S)990 void InitializeSema(Sema &S) override { SemaPtr = &S; } 991 void HandleTranslationUnit(ASTContext &Ctx) override; 992 ASTMutationListener *GetASTMutationListener() override; 993 ASTDeserializationListener *GetASTDeserializationListener() override; hasEmittedPCH()994 bool hasEmittedPCH() const { return Buffer->IsComplete; } 995 }; 996 997 class OMPClauseWriter : public OMPClauseVisitor<OMPClauseWriter> { 998 ASTRecordWriter &Record; 999 1000 public: OMPClauseWriter(ASTRecordWriter & Record)1001 OMPClauseWriter(ASTRecordWriter &Record) : Record(Record) {} 1002 #define OPENMP_CLAUSE(Name, Class) void Visit##Class(Class *S); 1003 #include "clang/Basic/OpenMPKinds.def" 1004 void writeClause(OMPClause *C); 1005 void VisitOMPClauseWithPreInit(OMPClauseWithPreInit *C); 1006 void VisitOMPClauseWithPostUpdate(OMPClauseWithPostUpdate *C); 1007 }; 1008 1009 } // namespace clang 1010 1011 #endif // LLVM_CLANG_SERIALIZATION_ASTWRITER_H 1012