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