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/DeclarationName.h" 20 #include "clang/AST/TemplateBase.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/SmallPtrSet.h" 29 #include "llvm/ADT/SmallVector.h" 30 #include "llvm/Bitcode/BitstreamWriter.h" 31 #include <map> 32 #include <queue> 33 #include <vector> 34 35 namespace llvm { 36 class APFloat; 37 class APInt; 38 class BitstreamWriter; 39 } 40 41 namespace clang { 42 43 class ASTContext; 44 class NestedNameSpecifier; 45 class CXXBaseSpecifier; 46 class CXXCtorInitializer; 47 class FileEntry; 48 class FPOptions; 49 class HeaderSearch; 50 class HeaderSearchOptions; 51 class IdentifierResolver; 52 class MacroDefinition; 53 class MacroDirective; 54 class MacroInfo; 55 class OpaqueValueExpr; 56 class OpenCLOptions; 57 class ASTReader; 58 class Module; 59 class PreprocessedEntity; 60 class PreprocessingRecord; 61 class Preprocessor; 62 class Sema; 63 class SourceManager; 64 class SwitchCase; 65 class TargetInfo; 66 class Token; 67 class VersionTuple; 68 class ASTUnresolvedSet; 69 70 namespace SrcMgr { class SLocEntry; } 71 72 /// \brief Writes an AST file containing the contents of a translation unit. 73 /// 74 /// The ASTWriter class produces a bitstream containing the serialized 75 /// representation of a given abstract syntax tree and its supporting 76 /// data structures. This bitstream can be de-serialized via an 77 /// instance of the ASTReader class. 78 class ASTWriter : public ASTDeserializationListener, 79 public ASTMutationListener { 80 public: 81 typedef SmallVector<uint64_t, 64> RecordData; 82 typedef SmallVectorImpl<uint64_t> RecordDataImpl; 83 84 friend class ASTDeclWriter; 85 friend class ASTStmtWriter; 86 private: 87 /// \brief Map that provides the ID numbers of each type within the 88 /// output stream, plus those deserialized from a chained PCH. 89 /// 90 /// The ID numbers of types are consecutive (in order of discovery) 91 /// and start at 1. 0 is reserved for NULL. When types are actually 92 /// stored in the stream, the ID number is shifted by 2 bits to 93 /// allow for the const/volatile qualifiers. 94 /// 95 /// Keys in the map never have const/volatile qualifiers. 96 typedef llvm::DenseMap<QualType, serialization::TypeIdx, 97 serialization::UnsafeQualTypeDenseMapInfo> 98 TypeIdxMap; 99 100 /// \brief The bitstream writer used to emit this precompiled header. 101 llvm::BitstreamWriter &Stream; 102 103 /// \brief The ASTContext we're writing. 104 ASTContext *Context; 105 106 /// \brief The preprocessor we're writing. 107 Preprocessor *PP; 108 109 /// \brief The reader of existing AST files, if we're chaining. 110 ASTReader *Chain; 111 112 /// \brief The module we're currently writing, if any. 113 Module *WritingModule; 114 115 /// \brief The base directory for any relative paths we emit. 116 std::string BaseDirectory; 117 118 /// \brief Indicates when the AST writing is actively performing 119 /// serialization, rather than just queueing updates. 120 bool WritingAST; 121 122 /// \brief Indicates that we are done serializing the collection of decls 123 /// and types to emit. 124 bool DoneWritingDeclsAndTypes; 125 126 /// \brief Indicates that the AST contained compiler errors. 127 bool ASTHasCompilerErrors; 128 129 /// \brief Mapping from input file entries to the index into the 130 /// offset table where information about that input file is stored. 131 llvm::DenseMap<const FileEntry *, uint32_t> InputFileIDs; 132 133 /// \brief Stores a declaration or a type to be written to the AST file. 134 class DeclOrType { 135 public: DeclOrType(Decl * D)136 DeclOrType(Decl *D) : Stored(D), IsType(false) { } DeclOrType(QualType T)137 DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { } 138 isType()139 bool isType() const { return IsType; } isDecl()140 bool isDecl() const { return !IsType; } 141 getType()142 QualType getType() const { 143 assert(isType() && "Not a type!"); 144 return QualType::getFromOpaquePtr(Stored); 145 } 146 getDecl()147 Decl *getDecl() const { 148 assert(isDecl() && "Not a decl!"); 149 return static_cast<Decl *>(Stored); 150 } 151 152 private: 153 void *Stored; 154 bool IsType; 155 }; 156 157 /// \brief The declarations and types to emit. 158 std::queue<DeclOrType> DeclTypesToEmit; 159 160 /// \brief The first ID number we can use for our own declarations. 161 serialization::DeclID FirstDeclID; 162 163 /// \brief The decl ID that will be assigned to the next new decl. 164 serialization::DeclID NextDeclID; 165 166 /// \brief Map that provides the ID numbers of each declaration within 167 /// the output stream, as well as those deserialized from a chained PCH. 168 /// 169 /// The ID numbers of declarations are consecutive (in order of 170 /// discovery) and start at 2. 1 is reserved for the translation 171 /// unit, while 0 is reserved for NULL. 172 llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs; 173 174 /// \brief Offset of each declaration in the bitstream, indexed by 175 /// the declaration's ID. 176 std::vector<serialization::DeclOffset> DeclOffsets; 177 178 /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID. 179 typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64> 180 LocDeclIDsTy; 181 struct DeclIDInFileInfo { 182 LocDeclIDsTy DeclIDs; 183 /// \brief Set when the DeclIDs vectors from all files are joined, this 184 /// indicates the index that this particular vector has in the global one. 185 unsigned FirstDeclIndex; 186 }; 187 typedef llvm::DenseMap<FileID, DeclIDInFileInfo *> FileDeclIDsTy; 188 189 /// \brief Map from file SLocEntries to info about the file-level declarations 190 /// that it contains. 191 FileDeclIDsTy FileDeclIDs; 192 193 void associateDeclWithFile(const Decl *D, serialization::DeclID); 194 195 /// \brief The first ID number we can use for our own types. 196 serialization::TypeID FirstTypeID; 197 198 /// \brief The type ID that will be assigned to the next new type. 199 serialization::TypeID NextTypeID; 200 201 /// \brief Map that provides the ID numbers of each type within the 202 /// output stream, plus those deserialized from a chained PCH. 203 /// 204 /// The ID numbers of types are consecutive (in order of discovery) 205 /// and start at 1. 0 is reserved for NULL. When types are actually 206 /// stored in the stream, the ID number is shifted by 2 bits to 207 /// allow for the const/volatile qualifiers. 208 /// 209 /// Keys in the map never have const/volatile qualifiers. 210 TypeIdxMap TypeIdxs; 211 212 /// \brief Offset of each type in the bitstream, indexed by 213 /// the type's ID. 214 std::vector<uint32_t> TypeOffsets; 215 216 /// \brief The first ID number we can use for our own identifiers. 217 serialization::IdentID FirstIdentID; 218 219 /// \brief The identifier ID that will be assigned to the next new identifier. 220 serialization::IdentID NextIdentID; 221 222 /// \brief Map that provides the ID numbers of each identifier in 223 /// the output stream. 224 /// 225 /// The ID numbers for identifiers are consecutive (in order of 226 /// discovery), starting at 1. An ID of zero refers to a NULL 227 /// IdentifierInfo. 228 llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs; 229 230 /// \brief The first ID number we can use for our own macros. 231 serialization::MacroID FirstMacroID; 232 233 /// \brief The identifier ID that will be assigned to the next new identifier. 234 serialization::MacroID NextMacroID; 235 236 /// \brief Map that provides the ID numbers of each macro. 237 llvm::DenseMap<MacroInfo *, serialization::MacroID> MacroIDs; 238 239 struct MacroInfoToEmitData { 240 const IdentifierInfo *Name; 241 MacroInfo *MI; 242 serialization::MacroID ID; 243 }; 244 /// \brief The macro infos to emit. 245 std::vector<MacroInfoToEmitData> MacroInfosToEmit; 246 247 llvm::DenseMap<const IdentifierInfo *, uint64_t> IdentMacroDirectivesOffsetMap; 248 249 /// @name FlushStmt Caches 250 /// @{ 251 252 /// \brief Set of parent Stmts for the currently serializing sub-stmt. 253 llvm::DenseSet<Stmt *> ParentStmts; 254 255 /// \brief Offsets of sub-stmts already serialized. The offset points 256 /// just after the stmt record. 257 llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries; 258 259 /// @} 260 261 /// \brief Offsets of each of the identifier IDs into the identifier 262 /// table. 263 std::vector<uint32_t> IdentifierOffsets; 264 265 /// \brief The first ID number we can use for our own submodules. 266 serialization::SubmoduleID FirstSubmoduleID; 267 268 /// \brief The submodule ID that will be assigned to the next new submodule. 269 serialization::SubmoduleID NextSubmoduleID; 270 271 /// \brief The first ID number we can use for our own selectors. 272 serialization::SelectorID FirstSelectorID; 273 274 /// \brief The selector ID that will be assigned to the next new selector. 275 serialization::SelectorID NextSelectorID; 276 277 /// \brief Map that provides the ID numbers of each Selector. 278 llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs; 279 280 /// \brief Offset of each selector within the method pool/selector 281 /// table, indexed by the Selector ID (-1). 282 std::vector<uint32_t> SelectorOffsets; 283 284 /// \brief Mapping from macro definitions (as they occur in the preprocessing 285 /// record) to the macro IDs. 286 llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID> 287 MacroDefinitions; 288 289 /// \brief Cache of indices of anonymous declarations within their lexical 290 /// contexts. 291 llvm::DenseMap<const Decl *, unsigned> AnonymousDeclarationNumbers; 292 293 /// An update to a Decl. 294 class DeclUpdate { 295 /// A DeclUpdateKind. 296 unsigned Kind; 297 union { 298 const Decl *Dcl; 299 void *Type; 300 unsigned Loc; 301 unsigned Val; 302 }; 303 304 public: DeclUpdate(unsigned Kind)305 DeclUpdate(unsigned Kind) : Kind(Kind), Dcl(nullptr) {} DeclUpdate(unsigned Kind,const Decl * Dcl)306 DeclUpdate(unsigned Kind, const Decl *Dcl) : Kind(Kind), Dcl(Dcl) {} DeclUpdate(unsigned Kind,QualType Type)307 DeclUpdate(unsigned Kind, QualType Type) 308 : Kind(Kind), Type(Type.getAsOpaquePtr()) {} DeclUpdate(unsigned Kind,SourceLocation Loc)309 DeclUpdate(unsigned Kind, SourceLocation Loc) 310 : Kind(Kind), Loc(Loc.getRawEncoding()) {} DeclUpdate(unsigned Kind,unsigned Val)311 DeclUpdate(unsigned Kind, unsigned Val) 312 : Kind(Kind), Val(Val) {} 313 getKind()314 unsigned getKind() const { return Kind; } getDecl()315 const Decl *getDecl() const { return Dcl; } getType()316 QualType getType() const { return QualType::getFromOpaquePtr(Type); } getLoc()317 SourceLocation getLoc() const { 318 return SourceLocation::getFromRawEncoding(Loc); 319 } getNumber()320 unsigned getNumber() const { return Val; } 321 }; 322 323 typedef SmallVector<DeclUpdate, 1> UpdateRecord; 324 typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap; 325 /// \brief Mapping from declarations that came from a chained PCH to the 326 /// record containing modifications to them. 327 DeclUpdateMap DeclUpdates; 328 329 typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap; 330 /// \brief Map of first declarations from a chained PCH that point to the 331 /// most recent declarations in another PCH. 332 FirstLatestDeclMap FirstLatestDecls; 333 334 /// \brief Declarations encountered that might be external 335 /// definitions. 336 /// 337 /// We keep track of external definitions and other 'interesting' declarations 338 /// as we are emitting declarations to the AST file. The AST file contains a 339 /// separate record for these declarations, which are provided to the AST 340 /// consumer by the AST reader. This is behavior is required to properly cope with, 341 /// e.g., tentative variable definitions that occur within 342 /// headers. The declarations themselves are stored as declaration 343 /// IDs, since they will be written out to an EAGERLY_DESERIALIZED_DECLS 344 /// record. 345 SmallVector<uint64_t, 16> EagerlyDeserializedDecls; 346 347 /// \brief DeclContexts that have received extensions since their serialized 348 /// form. 349 /// 350 /// For namespaces, when we're chaining and encountering a namespace, we check 351 /// if its primary namespace comes from the chain. If it does, we add the 352 /// primary to this set, so that we can write out lexical content updates for 353 /// it. 354 llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts; 355 356 /// \brief Keeps track of visible decls that were added in DeclContexts 357 /// coming from another AST file. 358 SmallVector<const Decl *, 16> UpdatingVisibleDecls; 359 360 typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy; 361 /// \brief Decls that will be replaced in the current dependent AST file. 362 DeclsToRewriteTy DeclsToRewrite; 363 364 /// \brief The set of Objective-C class that have categories we 365 /// should serialize. 366 llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories; 367 368 struct ReplacedDeclInfo { 369 serialization::DeclID ID; 370 uint64_t Offset; 371 unsigned Loc; 372 ReplacedDeclInfoReplacedDeclInfo373 ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {} ReplacedDeclInfoReplacedDeclInfo374 ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset, 375 SourceLocation Loc) 376 : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {} 377 }; 378 379 /// \brief Decls that have been replaced in the current dependent AST file. 380 /// 381 /// When a decl changes fundamentally after being deserialized (this shouldn't 382 /// happen, but the ObjC AST nodes are designed this way), it will be 383 /// serialized again. In this case, it is registered here, so that the reader 384 /// knows to read the updated version. 385 SmallVector<ReplacedDeclInfo, 16> ReplacedDecls; 386 387 /// \brief The set of declarations that may have redeclaration chains that 388 /// need to be serialized. 389 llvm::SetVector<Decl *, SmallVector<Decl *, 4>, 390 llvm::SmallPtrSet<Decl *, 4> > Redeclarations; 391 392 /// \brief Statements that we've encountered while serializing a 393 /// declaration or type. 394 SmallVector<Stmt *, 16> StmtsToEmit; 395 396 /// \brief Statements collection to use for ASTWriter::AddStmt(). 397 /// It will point to StmtsToEmit unless it is overriden. 398 SmallVector<Stmt *, 16> *CollectedStmts; 399 400 /// \brief Mapping from SwitchCase statements to IDs. 401 llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs; 402 403 /// \brief The number of statements written to the AST file. 404 unsigned NumStatements; 405 406 /// \brief The number of macros written to the AST file. 407 unsigned NumMacros; 408 409 /// \brief The number of lexical declcontexts written to the AST 410 /// file. 411 unsigned NumLexicalDeclContexts; 412 413 /// \brief The number of visible declcontexts written to the AST 414 /// file. 415 unsigned NumVisibleDeclContexts; 416 417 /// \brief The offset of each CXXBaseSpecifier set within the AST. 418 SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets; 419 420 /// \brief The first ID number we can use for our own base specifiers. 421 serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID; 422 423 /// \brief The base specifiers ID that will be assigned to the next new 424 /// set of C++ base specifiers. 425 serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID; 426 427 /// \brief A set of C++ base specifiers that is queued to be written into the 428 /// AST file. 429 struct QueuedCXXBaseSpecifiers { QueuedCXXBaseSpecifiersQueuedCXXBaseSpecifiers430 QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { } 431 QueuedCXXBaseSpecifiersQueuedCXXBaseSpecifiers432 QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID, 433 CXXBaseSpecifier const *Bases, 434 CXXBaseSpecifier const *BasesEnd) 435 : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { } 436 437 serialization::CXXBaseSpecifiersID ID; 438 CXXBaseSpecifier const * Bases; 439 CXXBaseSpecifier const * BasesEnd; 440 }; 441 442 /// \brief Queue of C++ base specifiers to be written to the AST file, 443 /// in the order they should be written. 444 SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite; 445 446 /// \brief 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 /// \brief Retrieve or create a submodule ID for this module. 451 unsigned getSubmoduleID(Module *Mod); 452 453 /// \brief Write the given subexpression to the bitstream. 454 void WriteSubStmt(Stmt *S, 455 llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries, 456 llvm::DenseSet<Stmt *> &ParentStmts); 457 458 void WriteBlockInfoBlock(); 459 void WriteControlBlock(Preprocessor &PP, ASTContext &Context, 460 StringRef isysroot, const std::string &OutputFile); 461 void WriteInputFiles(SourceManager &SourceMgr, 462 HeaderSearchOptions &HSOpts, 463 bool Modules); 464 void WriteSourceManagerBlock(SourceManager &SourceMgr, 465 const Preprocessor &PP); 466 void WritePreprocessor(const Preprocessor &PP, bool IsModule); 467 void WriteHeaderSearch(const HeaderSearch &HS); 468 void WritePreprocessorDetail(PreprocessingRecord &PPRec); 469 void WriteSubmodules(Module *WritingModule); 470 471 void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag, 472 bool isModule); 473 void WriteCXXBaseSpecifiersOffsets(); 474 475 unsigned TypeExtQualAbbrev; 476 unsigned TypeFunctionProtoAbbrev; 477 void WriteTypeAbbrevs(); 478 void WriteType(QualType T); 479 480 uint32_t GenerateNameLookupTable(const DeclContext *DC, 481 llvm::SmallVectorImpl<char> &LookupTable); 482 uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC); 483 uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC); 484 void WriteTypeDeclOffsets(); 485 void WriteFileDeclIDsMap(); 486 void WriteComments(); 487 void WriteSelectors(Sema &SemaRef); 488 void WriteReferencedSelectorsPool(Sema &SemaRef); 489 void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver, 490 bool IsModule); 491 void WriteAttributes(ArrayRef<const Attr*> Attrs, RecordDataImpl &Record); 492 void WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord); 493 void WriteDeclReplacementsBlock(); 494 void WriteDeclContextVisibleUpdate(const DeclContext *DC); 495 void WriteFPPragmaOptions(const FPOptions &Opts); 496 void WriteOpenCLExtensions(Sema &SemaRef); 497 void WriteObjCCategories(); 498 void WriteRedeclarations(); 499 void WriteMergedDecls(); 500 void WriteLateParsedTemplates(Sema &SemaRef); 501 void WriteOptimizePragmaOptions(Sema &SemaRef); 502 503 unsigned DeclParmVarAbbrev; 504 unsigned DeclContextLexicalAbbrev; 505 unsigned DeclContextVisibleLookupAbbrev; 506 unsigned UpdateVisibleAbbrev; 507 unsigned DeclRecordAbbrev; 508 unsigned DeclTypedefAbbrev; 509 unsigned DeclVarAbbrev; 510 unsigned DeclFieldAbbrev; 511 unsigned DeclEnumAbbrev; 512 unsigned DeclObjCIvarAbbrev; 513 unsigned DeclCXXMethodAbbrev; 514 515 unsigned DeclRefExprAbbrev; 516 unsigned CharacterLiteralAbbrev; 517 unsigned IntegerLiteralAbbrev; 518 unsigned ExprImplicitCastAbbrev; 519 520 void WriteDeclAbbrevs(); 521 void WriteDecl(ASTContext &Context, Decl *D); 522 void AddFunctionDefinition(const FunctionDecl *FD, RecordData &Record); 523 524 void WriteASTCore(Sema &SemaRef, 525 StringRef isysroot, const std::string &OutputFile, 526 Module *WritingModule); 527 528 public: 529 /// \brief Create a new precompiled header writer that outputs to 530 /// the given bitstream. 531 ASTWriter(llvm::BitstreamWriter &Stream); 532 ~ASTWriter(); 533 534 /// \brief Write a precompiled header for the given semantic analysis. 535 /// 536 /// \param SemaRef a reference to the semantic analysis object that processed 537 /// the AST to be written into the precompiled header. 538 /// 539 /// \param WritingModule The module that we are writing. If null, we are 540 /// writing a precompiled header. 541 /// 542 /// \param isysroot if non-empty, write a relocatable file whose headers 543 /// are relative to the given system root. If we're writing a module, its 544 /// build directory will be used in preference to this if both are available. 545 void WriteAST(Sema &SemaRef, 546 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 an integral value. 560 void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record); 561 562 /// \brief Emit a signed integral value. 563 void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record); 564 565 /// \brief Emit a floating-point value. 566 void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record); 567 568 /// \brief Emit a reference to an identifier. 569 void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record); 570 571 /// \brief Emit a Selector (which is a smart pointer reference). 572 void AddSelectorRef(Selector, RecordDataImpl &Record); 573 574 /// \brief Emit a CXXTemporary. 575 void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record); 576 577 /// \brief Emit a set of C++ base specifiers to the record. 578 void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases, 579 CXXBaseSpecifier const *BasesEnd, 580 RecordDataImpl &Record); 581 582 /// \brief Get the unique number used to refer to the given selector. 583 serialization::SelectorID getSelectorRef(Selector Sel); 584 585 /// \brief Get the unique number used to refer to the given identifier. 586 serialization::IdentID getIdentifierRef(const IdentifierInfo *II); 587 588 /// \brief Get the unique number used to refer to the given macro. 589 serialization::MacroID getMacroRef(MacroInfo *MI, const IdentifierInfo *Name); 590 591 /// \brief Determine the ID of an already-emitted macro. 592 serialization::MacroID getMacroID(MacroInfo *MI); 593 594 uint64_t getMacroDirectivesOffset(const IdentifierInfo *Name); 595 596 /// \brief Emit a reference to a type. 597 void AddTypeRef(QualType T, RecordDataImpl &Record); 598 599 /// \brief Force a type to be emitted and get its ID. 600 serialization::TypeID GetOrCreateTypeID(QualType T); 601 602 /// \brief Determine the type ID of an already-emitted type. 603 serialization::TypeID getTypeID(QualType T) const; 604 605 /// \brief Force a type to be emitted and get its index. 606 serialization::TypeIdx GetOrCreateTypeIdx( QualType T); 607 608 /// \brief Determine the type index of an already-emitted type. 609 serialization::TypeIdx getTypeIdx(QualType T) const; 610 611 /// \brief Emits a reference to a declarator info. 612 void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record); 613 614 /// \brief Emits a type with source-location information. 615 void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record); 616 617 /// \brief Emits a template argument location info. 618 void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, 619 const TemplateArgumentLocInfo &Arg, 620 RecordDataImpl &Record); 621 622 /// \brief Emits a template argument location. 623 void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, 624 RecordDataImpl &Record); 625 626 /// \brief Emits an AST template argument list info. 627 void AddASTTemplateArgumentListInfo( 628 const ASTTemplateArgumentListInfo *ASTTemplArgList, 629 RecordDataImpl &Record); 630 631 /// \brief Emit a reference to a declaration. 632 void AddDeclRef(const Decl *D, RecordDataImpl &Record); 633 634 635 /// \brief Force a declaration to be emitted and get its ID. 636 serialization::DeclID GetDeclRef(const Decl *D); 637 638 /// \brief Determine the declaration ID of an already-emitted 639 /// declaration. 640 serialization::DeclID getDeclID(const Decl *D); 641 642 /// \brief Emit a declaration name. 643 void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record); 644 void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc, 645 DeclarationName Name, RecordDataImpl &Record); 646 void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo, 647 RecordDataImpl &Record); 648 unsigned getAnonymousDeclarationNumber(const NamedDecl *D); 649 650 void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record); 651 652 /// \brief Emit a nested name specifier. 653 void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record); 654 655 /// \brief Emit a nested name specifier with source-location information. 656 void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS, 657 RecordDataImpl &Record); 658 659 /// \brief Emit a template name. 660 void AddTemplateName(TemplateName Name, RecordDataImpl &Record); 661 662 /// \brief Emit a template argument. 663 void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record); 664 665 /// \brief Emit a template parameter list. 666 void AddTemplateParameterList(const TemplateParameterList *TemplateParams, 667 RecordDataImpl &Record); 668 669 /// \brief Emit a template argument list. 670 void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, 671 RecordDataImpl &Record); 672 673 /// \brief Emit a UnresolvedSet structure. 674 void AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record); 675 676 /// \brief Emit a C++ base specifier. 677 void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, 678 RecordDataImpl &Record); 679 680 /// \brief Emit a CXXCtorInitializer array. 681 void AddCXXCtorInitializers( 682 const CXXCtorInitializer * const *CtorInitializers, 683 unsigned NumCtorInitializers, 684 RecordDataImpl &Record); 685 686 void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record); 687 688 /// \brief Add a string to the given record. 689 void AddString(StringRef Str, RecordDataImpl &Record); 690 691 /// \brief Convert a path from this build process into one that is appropriate 692 /// for emission in the module file. 693 bool PreparePathForOutput(SmallVectorImpl<char> &Path); 694 695 /// \brief Add a path to the given record. 696 void AddPath(StringRef Path, RecordDataImpl &Record); 697 698 /// \brief Emit the current record with the given path as a blob. 699 void EmitRecordWithPath(unsigned Abbrev, RecordDataImpl &Record, 700 StringRef Path); 701 702 /// \brief Add a version tuple to the given record 703 void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record); 704 705 /// \brief Mark a declaration context as needing an update. 706 void AddUpdatedDeclContext(const DeclContext *DC); 707 RewriteDecl(const Decl * D)708 void RewriteDecl(const Decl *D) { 709 DeclsToRewrite.insert(D); 710 } 711 isRewritten(const Decl * D)712 bool isRewritten(const Decl *D) const { 713 return DeclsToRewrite.count(D); 714 } 715 716 /// \brief Infer the submodule ID that contains an entity at the given 717 /// source location. 718 serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc); 719 720 /// \brief Retrieve a submodule ID for this module. 721 /// Returns 0 If no ID has been associated with the module. 722 unsigned getExistingSubmoduleID(Module *Mod) const; 723 724 /// \brief Note that the identifier II occurs at the given offset 725 /// within the identifier table. 726 void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset); 727 728 /// \brief Note that the selector Sel occurs at the given offset 729 /// within the method pool/selector table. 730 void SetSelectorOffset(Selector Sel, uint32_t Offset); 731 732 /// \brief Add the given statement or expression to the queue of 733 /// statements to emit. 734 /// 735 /// This routine should be used when emitting types and declarations 736 /// that have expressions as part of their formulation. Once the 737 /// type or declaration has been written, call FlushStmts() to write 738 /// the corresponding statements just after the type or 739 /// declaration. AddStmt(Stmt * S)740 void AddStmt(Stmt *S) { 741 CollectedStmts->push_back(S); 742 } 743 744 /// \brief Flush all of the statements and expressions that have 745 /// been added to the queue via AddStmt(). 746 void FlushStmts(); 747 748 /// \brief Flush all of the C++ base specifier sets that have been added 749 /// via \c AddCXXBaseSpecifiersRef(). 750 void FlushCXXBaseSpecifiers(); 751 752 /// \brief Record an ID for the given switch-case statement. 753 unsigned RecordSwitchCaseID(SwitchCase *S); 754 755 /// \brief Retrieve the ID for the given switch-case statement. 756 unsigned getSwitchCaseID(SwitchCase *S); 757 758 void ClearSwitchCaseIDs(); 759 getTypeExtQualAbbrev()760 unsigned getTypeExtQualAbbrev() const { 761 return TypeExtQualAbbrev; 762 } getTypeFunctionProtoAbbrev()763 unsigned getTypeFunctionProtoAbbrev() const { 764 return TypeFunctionProtoAbbrev; 765 } 766 getDeclParmVarAbbrev()767 unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; } getDeclRecordAbbrev()768 unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; } getDeclTypedefAbbrev()769 unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; } getDeclVarAbbrev()770 unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; } getDeclFieldAbbrev()771 unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; } getDeclEnumAbbrev()772 unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; } getDeclObjCIvarAbbrev()773 unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; } getDeclCXXMethodAbbrev()774 unsigned getDeclCXXMethodAbbrev() const { return DeclCXXMethodAbbrev; } 775 getDeclRefExprAbbrev()776 unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; } getCharacterLiteralAbbrev()777 unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; } getIntegerLiteralAbbrev()778 unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; } getExprImplicitCastAbbrev()779 unsigned getExprImplicitCastAbbrev() const { return ExprImplicitCastAbbrev; } 780 hasChain()781 bool hasChain() const { return Chain; } 782 783 // ASTDeserializationListener implementation 784 void ReaderInitialized(ASTReader *Reader) override; 785 void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II) override; 786 void MacroRead(serialization::MacroID ID, MacroInfo *MI) override; 787 void TypeRead(serialization::TypeIdx Idx, QualType T) override; 788 void SelectorRead(serialization::SelectorID ID, Selector Sel) override; 789 void MacroDefinitionRead(serialization::PreprocessedEntityID ID, 790 MacroDefinition *MD) override; 791 void ModuleRead(serialization::SubmoduleID ID, Module *Mod) override; 792 793 // ASTMutationListener implementation. 794 void CompletedTagDefinition(const TagDecl *D) override; 795 void AddedVisibleDecl(const DeclContext *DC, const Decl *D) override; 796 void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) override; 797 void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD, 798 const ClassTemplateSpecializationDecl *D) override; 799 void AddedCXXTemplateSpecialization(const VarTemplateDecl *TD, 800 const VarTemplateSpecializationDecl *D) override; 801 void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD, 802 const FunctionDecl *D) override; 803 void ResolvedExceptionSpec(const FunctionDecl *FD) override; 804 void DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) override; 805 void CompletedImplicitDefinition(const FunctionDecl *D) override; 806 void StaticDataMemberInstantiated(const VarDecl *D) override; 807 void FunctionDefinitionInstantiated(const FunctionDecl *D) override; 808 void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD, 809 const ObjCInterfaceDecl *IFD) override; 810 void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop, 811 const ObjCPropertyDecl *OrigProp, 812 const ObjCCategoryDecl *ClassExt) override; 813 void DeclarationMarkedUsed(const Decl *D) override; 814 void DeclarationMarkedOpenMPThreadPrivate(const Decl *D) override; 815 }; 816 817 /// \brief AST and semantic-analysis consumer that generates a 818 /// precompiled header from the parsed source code. 819 class PCHGenerator : public SemaConsumer { 820 const Preprocessor &PP; 821 std::string OutputFile; 822 clang::Module *Module; 823 std::string isysroot; 824 raw_ostream *Out; 825 Sema *SemaPtr; 826 SmallVector<char, 128> Buffer; 827 llvm::BitstreamWriter Stream; 828 ASTWriter Writer; 829 bool AllowASTWithErrors; 830 bool HasEmittedPCH; 831 832 protected: getWriter()833 ASTWriter &getWriter() { return Writer; } getWriter()834 const ASTWriter &getWriter() const { return Writer; } 835 836 public: 837 PCHGenerator(const Preprocessor &PP, StringRef OutputFile, 838 clang::Module *Module, 839 StringRef isysroot, raw_ostream *Out, 840 bool AllowASTWithErrors = false); 841 ~PCHGenerator(); InitializeSema(Sema & S)842 void InitializeSema(Sema &S) override { SemaPtr = &S; } 843 void HandleTranslationUnit(ASTContext &Ctx) override; 844 ASTMutationListener *GetASTMutationListener() override; 845 ASTDeserializationListener *GetASTDeserializationListener() override; 846 hasEmittedPCH()847 bool hasEmittedPCH() const { return HasEmittedPCH; } 848 }; 849 850 } // end namespace clang 851 852 #endif 853