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