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