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