clang API Documentation

ASTWriter.h
Go to the documentation of this file.
00001 //===--- ASTWriter.h - AST File Writer --------------------------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the ASTWriter class, which writes an AST file
00011 //  containing a serialized representation of a translation unit.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 #ifndef LLVM_CLANG_FRONTEND_AST_WRITER_H
00015 #define LLVM_CLANG_FRONTEND_AST_WRITER_H
00016 
00017 #include "clang/AST/Decl.h"
00018 #include "clang/AST/DeclarationName.h"
00019 #include "clang/AST/TemplateBase.h"
00020 #include "clang/AST/ASTMutationListener.h"
00021 #include "clang/Serialization/ASTBitCodes.h"
00022 #include "clang/Serialization/ASTDeserializationListener.h"
00023 #include "clang/Sema/SemaConsumer.h"
00024 #include "llvm/ADT/SmallPtrSet.h"
00025 #include "llvm/ADT/SmallVector.h"
00026 #include "llvm/ADT/DenseMap.h"
00027 #include "llvm/ADT/DenseSet.h"
00028 #include "llvm/ADT/SetVector.h"
00029 #include "llvm/Bitcode/BitstreamWriter.h"
00030 #include <map>
00031 #include <queue>
00032 #include <vector>
00033 
00034 namespace llvm {
00035   class APFloat;
00036   class APInt;
00037   class BitstreamWriter;
00038 }
00039 
00040 namespace clang {
00041 
00042 class ASTContext;
00043 class NestedNameSpecifier;
00044 class CXXBaseSpecifier;
00045 class CXXCtorInitializer;
00046 class FPOptions;
00047 class HeaderSearch;
00048 class IdentifierResolver;
00049 class MacroDefinition;
00050 class MemorizeStatCalls;
00051 class OpaqueValueExpr;
00052 class OpenCLOptions;
00053 class ASTReader;
00054 class Module;
00055 class PreprocessedEntity;
00056 class PreprocessingRecord;
00057 class Preprocessor;
00058 class Sema;
00059 class SourceManager;
00060 class SwitchCase;
00061 class TargetInfo;
00062 class VersionTuple;
00063 
00064 namespace SrcMgr { class SLocEntry; }
00065 
00066 /// \brief Writes an AST file containing the contents of a translation unit.
00067 ///
00068 /// The ASTWriter class produces a bitstream containing the serialized
00069 /// representation of a given abstract syntax tree and its supporting
00070 /// data structures. This bitstream can be de-serialized via an
00071 /// instance of the ASTReader class.
00072 class ASTWriter : public ASTDeserializationListener,
00073                   public ASTMutationListener {
00074 public:
00075   typedef SmallVector<uint64_t, 64> RecordData;
00076   typedef SmallVectorImpl<uint64_t> RecordDataImpl;
00077 
00078   friend class ASTDeclWriter;
00079   friend class ASTStmtWriter;
00080 private:
00081   /// \brief Map that provides the ID numbers of each type within the
00082   /// output stream, plus those deserialized from a chained PCH.
00083   ///
00084   /// The ID numbers of types are consecutive (in order of discovery)
00085   /// and start at 1. 0 is reserved for NULL. When types are actually
00086   /// stored in the stream, the ID number is shifted by 2 bits to
00087   /// allow for the const/volatile qualifiers.
00088   ///
00089   /// Keys in the map never have const/volatile qualifiers.
00090   typedef llvm::DenseMap<QualType, serialization::TypeIdx,
00091                          serialization::UnsafeQualTypeDenseMapInfo>
00092     TypeIdxMap;
00093 
00094   /// \brief The bitstream writer used to emit this precompiled header.
00095   llvm::BitstreamWriter &Stream;
00096 
00097   /// \brief The ASTContext we're writing.
00098   ASTContext *Context;
00099 
00100   /// \brief The preprocessor we're writing.
00101   Preprocessor *PP;
00102 
00103   /// \brief The reader of existing AST files, if we're chaining.
00104   ASTReader *Chain;
00105 
00106   /// \brief The module we're currently writing, if any.
00107   Module *WritingModule;
00108                     
00109   /// \brief Indicates when the AST writing is actively performing
00110   /// serialization, rather than just queueing updates.
00111   bool WritingAST;
00112 
00113   /// \brief Indicates that the AST contained compiler errors.
00114   bool ASTHasCompilerErrors;
00115 
00116   /// \brief Stores a declaration or a type to be written to the AST file.
00117   class DeclOrType {
00118   public:
00119     DeclOrType(Decl *D) : Stored(D), IsType(false) { }
00120     DeclOrType(QualType T) : Stored(T.getAsOpaquePtr()), IsType(true) { }
00121 
00122     bool isType() const { return IsType; }
00123     bool isDecl() const { return !IsType; }
00124 
00125     QualType getType() const {
00126       assert(isType() && "Not a type!");
00127       return QualType::getFromOpaquePtr(Stored);
00128     }
00129 
00130     Decl *getDecl() const {
00131       assert(isDecl() && "Not a decl!");
00132       return static_cast<Decl *>(Stored);
00133     }
00134 
00135   private:
00136     void *Stored;
00137     bool IsType;
00138   };
00139 
00140   /// \brief The declarations and types to emit.
00141   std::queue<DeclOrType> DeclTypesToEmit;
00142 
00143   /// \brief The first ID number we can use for our own declarations.
00144   serialization::DeclID FirstDeclID;
00145 
00146   /// \brief The decl ID that will be assigned to the next new decl.
00147   serialization::DeclID NextDeclID;
00148 
00149   /// \brief Map that provides the ID numbers of each declaration within
00150   /// the output stream, as well as those deserialized from a chained PCH.
00151   ///
00152   /// The ID numbers of declarations are consecutive (in order of
00153   /// discovery) and start at 2. 1 is reserved for the translation
00154   /// unit, while 0 is reserved for NULL.
00155   llvm::DenseMap<const Decl *, serialization::DeclID> DeclIDs;
00156 
00157   /// \brief Offset of each declaration in the bitstream, indexed by
00158   /// the declaration's ID.
00159   std::vector<serialization::DeclOffset> DeclOffsets;
00160 
00161   /// \brief Sorted (by file offset) vector of pairs of file offset/DeclID.
00162   typedef SmallVector<std::pair<unsigned, serialization::DeclID>, 64>
00163     LocDeclIDsTy;
00164   struct DeclIDInFileInfo {
00165     LocDeclIDsTy DeclIDs;
00166     /// \brief Set when the DeclIDs vectors from all files are joined, this
00167     /// indicates the index that this particular vector has in the global one.
00168     unsigned FirstDeclIndex;
00169   };
00170   typedef llvm::DenseMap<const SrcMgr::SLocEntry *,
00171                          DeclIDInFileInfo *> FileDeclIDsTy;
00172 
00173   /// \brief Map from file SLocEntries to info about the file-level declarations
00174   /// that it contains.
00175   FileDeclIDsTy FileDeclIDs;
00176 
00177   void associateDeclWithFile(const Decl *D, serialization::DeclID);
00178 
00179   /// \brief The first ID number we can use for our own types.
00180   serialization::TypeID FirstTypeID;
00181 
00182   /// \brief The type ID that will be assigned to the next new type.
00183   serialization::TypeID NextTypeID;
00184 
00185   /// \brief Map that provides the ID numbers of each type within the
00186   /// output stream, plus those deserialized from a chained PCH.
00187   ///
00188   /// The ID numbers of types are consecutive (in order of discovery)
00189   /// and start at 1. 0 is reserved for NULL. When types are actually
00190   /// stored in the stream, the ID number is shifted by 2 bits to
00191   /// allow for the const/volatile qualifiers.
00192   ///
00193   /// Keys in the map never have const/volatile qualifiers.
00194   TypeIdxMap TypeIdxs;
00195 
00196   /// \brief Offset of each type in the bitstream, indexed by
00197   /// the type's ID.
00198   std::vector<uint32_t> TypeOffsets;
00199 
00200   /// \brief The first ID number we can use for our own identifiers.
00201   serialization::IdentID FirstIdentID;
00202 
00203   /// \brief The identifier ID that will be assigned to the next new identifier.
00204   serialization::IdentID NextIdentID;
00205 
00206   /// \brief Map that provides the ID numbers of each identifier in
00207   /// the output stream.
00208   ///
00209   /// The ID numbers for identifiers are consecutive (in order of
00210   /// discovery), starting at 1. An ID of zero refers to a NULL
00211   /// IdentifierInfo.
00212   llvm::DenseMap<const IdentifierInfo *, serialization::IdentID> IdentifierIDs;
00213 
00214   /// @name FlushStmt Caches
00215   /// @{
00216 
00217   /// \brief Set of parent Stmts for the currently serializing sub stmt.
00218   llvm::DenseSet<Stmt *> ParentStmts;
00219 
00220   /// \brief Offsets of sub stmts already serialized. The offset points
00221   /// just after the stmt record.
00222   llvm::DenseMap<Stmt *, uint64_t> SubStmtEntries;
00223 
00224   /// @}
00225 
00226   /// \brief Offsets of each of the identifier IDs into the identifier
00227   /// table.
00228   std::vector<uint32_t> IdentifierOffsets;
00229 
00230   /// \brief The first ID number we can use for our own submodules.
00231   serialization::SubmoduleID FirstSubmoduleID;
00232   
00233   /// \brief The submodule ID that will be assigned to the next new submodule.
00234   serialization::SubmoduleID NextSubmoduleID;
00235 
00236   /// \brief The first ID number we can use for our own selectors.
00237   serialization::SelectorID FirstSelectorID;
00238 
00239   /// \brief The selector ID that will be assigned to the next new selector.
00240   serialization::SelectorID NextSelectorID;
00241 
00242   /// \brief Map that provides the ID numbers of each Selector.
00243   llvm::DenseMap<Selector, serialization::SelectorID> SelectorIDs;
00244 
00245   /// \brief Offset of each selector within the method pool/selector
00246   /// table, indexed by the Selector ID (-1).
00247   std::vector<uint32_t> SelectorOffsets;
00248 
00249   /// \brief Offsets of each of the macro identifiers into the
00250   /// bitstream.
00251   ///
00252   /// For each identifier that is associated with a macro, this map
00253   /// provides the offset into the bitstream where that macro is
00254   /// defined.
00255   llvm::DenseMap<const IdentifierInfo *, uint64_t> MacroOffsets;
00256 
00257   /// \brief The set of identifiers that had macro definitions at some point.
00258   std::vector<const IdentifierInfo *> DeserializedMacroNames;
00259 
00260   /// \brief Mapping from macro definitions (as they occur in the preprocessing
00261   /// record) to the macro IDs.
00262   llvm::DenseMap<const MacroDefinition *, serialization::PreprocessedEntityID>
00263       MacroDefinitions;
00264 
00265   typedef SmallVector<uint64_t, 2> UpdateRecord;
00266   typedef llvm::DenseMap<const Decl *, UpdateRecord> DeclUpdateMap;
00267   /// \brief Mapping from declarations that came from a chained PCH to the
00268   /// record containing modifications to them.
00269   DeclUpdateMap DeclUpdates;
00270 
00271   typedef llvm::DenseMap<Decl *, Decl *> FirstLatestDeclMap;
00272   /// \brief Map of first declarations from a chained PCH that point to the
00273   /// most recent declarations in another PCH.
00274   FirstLatestDeclMap FirstLatestDecls;
00275 
00276   /// \brief Declarations encountered that might be external
00277   /// definitions.
00278   ///
00279   /// We keep track of external definitions (as well as tentative
00280   /// definitions) as we are emitting declarations to the AST
00281   /// file. The AST file contains a separate record for these external
00282   /// definitions, which are provided to the AST consumer by the AST
00283   /// reader. This is behavior is required to properly cope with,
00284   /// e.g., tentative variable definitions that occur within
00285   /// headers. The declarations themselves are stored as declaration
00286   /// IDs, since they will be written out to an EXTERNAL_DEFINITIONS
00287   /// record.
00288   SmallVector<uint64_t, 16> ExternalDefinitions;
00289 
00290   /// \brief DeclContexts that have received extensions since their serialized
00291   /// form.
00292   ///
00293   /// For namespaces, when we're chaining and encountering a namespace, we check
00294   /// if its primary namespace comes from the chain. If it does, we add the
00295   /// primary to this set, so that we can write out lexical content updates for
00296   /// it.
00297   llvm::SmallPtrSet<const DeclContext *, 16> UpdatedDeclContexts;
00298 
00299   typedef llvm::SmallPtrSet<const Decl *, 16> DeclsToRewriteTy;
00300   /// \brief Decls that will be replaced in the current dependent AST file.
00301   DeclsToRewriteTy DeclsToRewrite;
00302 
00303   /// \brief The set of Objective-C class that have categories we
00304   /// should serialize.
00305   llvm::SetVector<ObjCInterfaceDecl *> ObjCClassesWithCategories;
00306                     
00307   struct ReplacedDeclInfo {
00308     serialization::DeclID ID;
00309     uint64_t Offset;
00310     unsigned Loc;
00311 
00312     ReplacedDeclInfo() : ID(0), Offset(0), Loc(0) {}
00313     ReplacedDeclInfo(serialization::DeclID ID, uint64_t Offset,
00314                      SourceLocation Loc)
00315       : ID(ID), Offset(Offset), Loc(Loc.getRawEncoding()) {}
00316   };
00317 
00318   /// \brief Decls that have been replaced in the current dependent AST file.
00319   ///
00320   /// When a decl changes fundamentally after being deserialized (this shouldn't
00321   /// happen, but the ObjC AST nodes are designed this way), it will be
00322   /// serialized again. In this case, it is registered here, so that the reader
00323   /// knows to read the updated version.
00324   SmallVector<ReplacedDeclInfo, 16> ReplacedDecls;
00325                  
00326   /// \brief The set of declarations that may have redeclaration chains that
00327   /// need to be serialized.
00328   llvm::SetVector<Decl *, llvm::SmallVector<Decl *, 4>, 
00329                   llvm::SmallPtrSet<Decl *, 4> > Redeclarations;
00330                                       
00331   /// \brief Statements that we've encountered while serializing a
00332   /// declaration or type.
00333   SmallVector<Stmt *, 16> StmtsToEmit;
00334 
00335   /// \brief Statements collection to use for ASTWriter::AddStmt().
00336   /// It will point to StmtsToEmit unless it is overriden.
00337   SmallVector<Stmt *, 16> *CollectedStmts;
00338 
00339   /// \brief Mapping from SwitchCase statements to IDs.
00340   llvm::DenseMap<SwitchCase *, unsigned> SwitchCaseIDs;
00341 
00342   /// \brief The number of statements written to the AST file.
00343   unsigned NumStatements;
00344 
00345   /// \brief The number of macros written to the AST file.
00346   unsigned NumMacros;
00347 
00348   /// \brief The number of lexical declcontexts written to the AST
00349   /// file.
00350   unsigned NumLexicalDeclContexts;
00351 
00352   /// \brief The number of visible declcontexts written to the AST
00353   /// file.
00354   unsigned NumVisibleDeclContexts;
00355 
00356   /// \brief The offset of each CXXBaseSpecifier set within the AST.
00357   SmallVector<uint32_t, 4> CXXBaseSpecifiersOffsets;
00358 
00359   /// \brief The first ID number we can use for our own base specifiers.
00360   serialization::CXXBaseSpecifiersID FirstCXXBaseSpecifiersID;
00361 
00362   /// \brief The base specifiers ID that will be assigned to the next new
00363   /// set of C++ base specifiers.
00364   serialization::CXXBaseSpecifiersID NextCXXBaseSpecifiersID;
00365 
00366   /// \brief A set of C++ base specifiers that is queued to be written into the
00367   /// AST file.
00368   struct QueuedCXXBaseSpecifiers {
00369     QueuedCXXBaseSpecifiers() : ID(), Bases(), BasesEnd() { }
00370 
00371     QueuedCXXBaseSpecifiers(serialization::CXXBaseSpecifiersID ID,
00372                             CXXBaseSpecifier const *Bases,
00373                             CXXBaseSpecifier const *BasesEnd)
00374       : ID(ID), Bases(Bases), BasesEnd(BasesEnd) { }
00375 
00376     serialization::CXXBaseSpecifiersID ID;
00377     CXXBaseSpecifier const * Bases;
00378     CXXBaseSpecifier const * BasesEnd;
00379   };
00380 
00381   /// \brief Queue of C++ base specifiers to be written to the AST file,
00382   /// in the order they should be written.
00383   SmallVector<QueuedCXXBaseSpecifiers, 2> CXXBaseSpecifiersToWrite;
00384 
00385   /// \brief A mapping from each known submodule to its ID number, which will
00386   /// be a positive integer.
00387   llvm::DenseMap<Module *, unsigned> SubmoduleIDs;
00388                     
00389   /// \brief Retrieve or create a submodule ID for this module.
00390   unsigned getSubmoduleID(Module *Mod);
00391                     
00392   /// \brief Write the given subexpression to the bitstream.
00393   void WriteSubStmt(Stmt *S,
00394                     llvm::DenseMap<Stmt *, uint64_t> &SubStmtEntries,
00395                     llvm::DenseSet<Stmt *> &ParentStmts);
00396 
00397   void WriteBlockInfoBlock();
00398   void WriteMetadata(ASTContext &Context, StringRef isysroot,
00399                      const std::string &OutputFile);
00400   void WriteLanguageOptions(const LangOptions &LangOpts);
00401   void WriteStatCache(MemorizeStatCalls &StatCalls);
00402   void WriteSourceManagerBlock(SourceManager &SourceMgr,
00403                                const Preprocessor &PP,
00404                                StringRef isysroot);
00405   void WritePreprocessor(const Preprocessor &PP, bool IsModule);
00406   void WriteHeaderSearch(const HeaderSearch &HS, StringRef isysroot);
00407   void WritePreprocessorDetail(PreprocessingRecord &PPRec);
00408   void WriteSubmodules(Module *WritingModule);
00409                                         
00410   void WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag);
00411   void WriteCXXBaseSpecifiersOffsets();
00412   void WriteType(QualType T);
00413   uint64_t WriteDeclContextLexicalBlock(ASTContext &Context, DeclContext *DC);
00414   uint64_t WriteDeclContextVisibleBlock(ASTContext &Context, DeclContext *DC);
00415   void WriteTypeDeclOffsets();
00416   void WriteFileDeclIDsMap();
00417   void WriteSelectors(Sema &SemaRef);
00418   void WriteReferencedSelectorsPool(Sema &SemaRef);
00419   void WriteIdentifierTable(Preprocessor &PP, IdentifierResolver &IdResolver,
00420                             bool IsModule);
00421   void WriteAttributes(const AttrVec &Attrs, RecordDataImpl &Record);
00422   void ResolveDeclUpdatesBlocks();
00423   void WriteDeclUpdatesBlocks();
00424   void WriteDeclReplacementsBlock();
00425   void WriteDeclContextVisibleUpdate(const DeclContext *DC);
00426   void WriteFPPragmaOptions(const FPOptions &Opts);
00427   void WriteOpenCLExtensions(Sema &SemaRef);
00428   void WriteObjCCategories();
00429   void WriteRedeclarations();
00430   void WriteMergedDecls();
00431                         
00432   unsigned DeclParmVarAbbrev;
00433   unsigned DeclContextLexicalAbbrev;
00434   unsigned DeclContextVisibleLookupAbbrev;
00435   unsigned UpdateVisibleAbbrev;
00436   unsigned DeclRefExprAbbrev;
00437   unsigned CharacterLiteralAbbrev;
00438   unsigned DeclRecordAbbrev;
00439   unsigned IntegerLiteralAbbrev;
00440   unsigned DeclTypedefAbbrev;
00441   unsigned DeclVarAbbrev;
00442   unsigned DeclFieldAbbrev;
00443   unsigned DeclEnumAbbrev;
00444   unsigned DeclObjCIvarAbbrev;
00445 
00446   void WriteDeclsBlockAbbrevs();
00447   void WriteDecl(ASTContext &Context, Decl *D);
00448 
00449   void WriteASTCore(Sema &SemaRef, MemorizeStatCalls *StatCalls,
00450                     StringRef isysroot, const std::string &OutputFile,
00451                     Module *WritingModule);
00452 
00453 public:
00454   /// \brief Create a new precompiled header writer that outputs to
00455   /// the given bitstream.
00456   ASTWriter(llvm::BitstreamWriter &Stream);
00457   ~ASTWriter();
00458 
00459   /// \brief Write a precompiled header for the given semantic analysis.
00460   ///
00461   /// \param SemaRef a reference to the semantic analysis object that processed
00462   /// the AST to be written into the precompiled header.
00463   ///
00464   /// \param StatCalls the object that cached all of the stat() calls made while
00465   /// searching for source files and headers.
00466   ///
00467   /// \param WritingModule The module that we are writing. If null, we are
00468   /// writing a precompiled header.
00469   ///
00470   /// \param isysroot if non-empty, write a relocatable file whose headers
00471   /// are relative to the given system root.
00472   void WriteAST(Sema &SemaRef, MemorizeStatCalls *StatCalls,
00473                 const std::string &OutputFile,
00474                 Module *WritingModule, StringRef isysroot,
00475                 bool hasErrors = false);
00476 
00477   /// \brief Emit a source location.
00478   void AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record);
00479 
00480   /// \brief Emit a source range.
00481   void AddSourceRange(SourceRange Range, RecordDataImpl &Record);
00482 
00483   /// \brief Emit an integral value.
00484   void AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record);
00485 
00486   /// \brief Emit a signed integral value.
00487   void AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record);
00488 
00489   /// \brief Emit a floating-point value.
00490   void AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record);
00491 
00492   /// \brief Emit a reference to an identifier.
00493   void AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record);
00494 
00495   /// \brief Emit a Selector (which is a smart pointer reference).
00496   void AddSelectorRef(Selector, RecordDataImpl &Record);
00497 
00498   /// \brief Emit a CXXTemporary.
00499   void AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record);
00500 
00501   /// \brief Emit a set of C++ base specifiers to the record.
00502   void AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
00503                                CXXBaseSpecifier const *BasesEnd,
00504                                RecordDataImpl &Record);
00505 
00506   /// \brief Get the unique number used to refer to the given selector.
00507   serialization::SelectorID getSelectorRef(Selector Sel);
00508 
00509   /// \brief Get the unique number used to refer to the given identifier.
00510   serialization::IdentID getIdentifierRef(const IdentifierInfo *II);
00511 
00512   /// \brief Retrieve the offset of the macro definition for the given
00513   /// identifier.
00514   ///
00515   /// The identifier must refer to a macro.
00516   uint64_t getMacroOffset(const IdentifierInfo *II) {
00517     assert(MacroOffsets.find(II) != MacroOffsets.end() &&
00518            "Identifier does not name a macro");
00519     return MacroOffsets[II];
00520   }
00521 
00522   /// \brief Emit a reference to a type.
00523   void AddTypeRef(QualType T, RecordDataImpl &Record);
00524 
00525   /// \brief Force a type to be emitted and get its ID.
00526   serialization::TypeID GetOrCreateTypeID(QualType T);
00527 
00528   /// \brief Determine the type ID of an already-emitted type.
00529   serialization::TypeID getTypeID(QualType T) const;
00530 
00531   /// \brief Force a type to be emitted and get its index.
00532   serialization::TypeIdx GetOrCreateTypeIdx( QualType T);
00533 
00534   /// \brief Determine the type index of an already-emitted type.
00535   serialization::TypeIdx getTypeIdx(QualType T) const;
00536 
00537   /// \brief Emits a reference to a declarator info.
00538   void AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordDataImpl &Record);
00539 
00540   /// \brief Emits a type with source-location information.
00541   void AddTypeLoc(TypeLoc TL, RecordDataImpl &Record);
00542 
00543   /// \brief Emits a template argument location info.
00544   void AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
00545                                   const TemplateArgumentLocInfo &Arg,
00546                                   RecordDataImpl &Record);
00547 
00548   /// \brief Emits a template argument location.
00549   void AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
00550                               RecordDataImpl &Record);
00551 
00552   /// \brief Emit a reference to a declaration.
00553   void AddDeclRef(const Decl *D, RecordDataImpl &Record);
00554 
00555 
00556   /// \brief Force a declaration to be emitted and get its ID.
00557   serialization::DeclID GetDeclRef(const Decl *D);
00558 
00559   /// \brief Determine the declaration ID of an already-emitted
00560   /// declaration.
00561   serialization::DeclID getDeclID(const Decl *D);
00562 
00563   /// \brief Emit a declaration name.
00564   void AddDeclarationName(DeclarationName Name, RecordDataImpl &Record);
00565   void AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
00566                              DeclarationName Name, RecordDataImpl &Record);
00567   void AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
00568                               RecordDataImpl &Record);
00569 
00570   void AddQualifierInfo(const QualifierInfo &Info, RecordDataImpl &Record);
00571 
00572   /// \brief Emit a nested name specifier.
00573   void AddNestedNameSpecifier(NestedNameSpecifier *NNS, RecordDataImpl &Record);
00574 
00575   /// \brief Emit a nested name specifier with source-location information.
00576   void AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
00577                                  RecordDataImpl &Record);
00578 
00579   /// \brief Emit a template name.
00580   void AddTemplateName(TemplateName Name, RecordDataImpl &Record);
00581 
00582   /// \brief Emit a template argument.
00583   void AddTemplateArgument(const TemplateArgument &Arg, RecordDataImpl &Record);
00584 
00585   /// \brief Emit a template parameter list.
00586   void AddTemplateParameterList(const TemplateParameterList *TemplateParams,
00587                                 RecordDataImpl &Record);
00588 
00589   /// \brief Emit a template argument list.
00590   void AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
00591                                 RecordDataImpl &Record);
00592 
00593   /// \brief Emit a UnresolvedSet structure.
00594   void AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordDataImpl &Record);
00595 
00596   /// \brief Emit a C++ base specifier.
00597   void AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
00598                            RecordDataImpl &Record);
00599 
00600   /// \brief Emit a CXXCtorInitializer array.
00601   void AddCXXCtorInitializers(
00602                              const CXXCtorInitializer * const *CtorInitializers,
00603                              unsigned NumCtorInitializers,
00604                              RecordDataImpl &Record);
00605 
00606   void AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record);
00607 
00608   /// \brief Add a string to the given record.
00609   void AddString(StringRef Str, RecordDataImpl &Record);
00610 
00611   /// \brief Add a version tuple to the given record
00612   void AddVersionTuple(const VersionTuple &Version, RecordDataImpl &Record);
00613 
00614   /// \brief Mark a declaration context as needing an update.
00615   void AddUpdatedDeclContext(const DeclContext *DC) {
00616     UpdatedDeclContexts.insert(DC);
00617   }
00618 
00619   void RewriteDecl(const Decl *D) {
00620     DeclsToRewrite.insert(D);
00621   }
00622 
00623   bool isRewritten(const Decl *D) const {
00624     return DeclsToRewrite.count(D);
00625   }
00626 
00627   /// \brief Infer the submodule ID that contains an entity at the given
00628   /// source location.
00629   serialization::SubmoduleID inferSubmoduleIDFromLocation(SourceLocation Loc);
00630 
00631   /// \brief Note that the identifier II occurs at the given offset
00632   /// within the identifier table.
00633   void SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset);
00634 
00635   /// \brief Note that the selector Sel occurs at the given offset
00636   /// within the method pool/selector table.
00637   void SetSelectorOffset(Selector Sel, uint32_t Offset);
00638 
00639   /// \brief Add the given statement or expression to the queue of
00640   /// statements to emit.
00641   ///
00642   /// This routine should be used when emitting types and declarations
00643   /// that have expressions as part of their formulation. Once the
00644   /// type or declaration has been written, call FlushStmts() to write
00645   /// the corresponding statements just after the type or
00646   /// declaration.
00647   void AddStmt(Stmt *S) {
00648       CollectedStmts->push_back(S);
00649   }
00650 
00651   /// \brief Flush all of the statements and expressions that have
00652   /// been added to the queue via AddStmt().
00653   void FlushStmts();
00654 
00655   /// \brief Flush all of the C++ base specifier sets that have been added
00656   /// via \c AddCXXBaseSpecifiersRef().
00657   void FlushCXXBaseSpecifiers();
00658 
00659   /// \brief Record an ID for the given switch-case statement.
00660   unsigned RecordSwitchCaseID(SwitchCase *S);
00661 
00662   /// \brief Retrieve the ID for the given switch-case statement.
00663   unsigned getSwitchCaseID(SwitchCase *S);
00664 
00665   void ClearSwitchCaseIDs();
00666 
00667   unsigned getDeclParmVarAbbrev() const { return DeclParmVarAbbrev; }
00668   unsigned getDeclRefExprAbbrev() const { return DeclRefExprAbbrev; }
00669   unsigned getCharacterLiteralAbbrev() const { return CharacterLiteralAbbrev; }
00670   unsigned getDeclRecordAbbrev() const { return DeclRecordAbbrev; }
00671   unsigned getIntegerLiteralAbbrev() const { return IntegerLiteralAbbrev; }
00672   unsigned getDeclTypedefAbbrev() const { return DeclTypedefAbbrev; }
00673   unsigned getDeclVarAbbrev() const { return DeclVarAbbrev; }
00674   unsigned getDeclFieldAbbrev() const { return DeclFieldAbbrev; }
00675   unsigned getDeclEnumAbbrev() const { return DeclEnumAbbrev; }
00676   unsigned getDeclObjCIvarAbbrev() const { return DeclObjCIvarAbbrev; }
00677 
00678   bool hasChain() const { return Chain; }
00679 
00680   // ASTDeserializationListener implementation
00681   void ReaderInitialized(ASTReader *Reader);
00682   void IdentifierRead(serialization::IdentID ID, IdentifierInfo *II);
00683   void TypeRead(serialization::TypeIdx Idx, QualType T);
00684   void SelectorRead(serialization::SelectorID ID, Selector Sel);
00685   void MacroDefinitionRead(serialization::PreprocessedEntityID ID,
00686                            MacroDefinition *MD);
00687   void MacroVisible(IdentifierInfo *II);
00688   void ModuleRead(serialization::SubmoduleID ID, Module *Mod);
00689                     
00690   // ASTMutationListener implementation.
00691   virtual void CompletedTagDefinition(const TagDecl *D);
00692   virtual void AddedVisibleDecl(const DeclContext *DC, const Decl *D);
00693   virtual void AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D);
00694   virtual void AddedCXXTemplateSpecialization(const ClassTemplateDecl *TD,
00695                                     const ClassTemplateSpecializationDecl *D);
00696   virtual void AddedCXXTemplateSpecialization(const FunctionTemplateDecl *TD,
00697                                               const FunctionDecl *D);
00698   virtual void CompletedImplicitDefinition(const FunctionDecl *D);
00699   virtual void StaticDataMemberInstantiated(const VarDecl *D);
00700   virtual void AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
00701                                             const ObjCInterfaceDecl *IFD);
00702   virtual void AddedObjCPropertyInClassExtension(const ObjCPropertyDecl *Prop,
00703                                             const ObjCPropertyDecl *OrigProp,
00704                                             const ObjCCategoryDecl *ClassExt);
00705 };
00706 
00707 /// \brief AST and semantic-analysis consumer that generates a
00708 /// precompiled header from the parsed source code.
00709 class PCHGenerator : public SemaConsumer {
00710   const Preprocessor &PP;
00711   std::string OutputFile;
00712   clang::Module *Module;
00713   std::string isysroot;
00714   raw_ostream *Out;
00715   Sema *SemaPtr;
00716   MemorizeStatCalls *StatCalls; // owned by the FileManager
00717   llvm::SmallVector<char, 128> Buffer;
00718   llvm::BitstreamWriter Stream;
00719   ASTWriter Writer;
00720 
00721 protected:
00722   ASTWriter &getWriter() { return Writer; }
00723   const ASTWriter &getWriter() const { return Writer; }
00724 
00725 public:
00726   PCHGenerator(const Preprocessor &PP, StringRef OutputFile,
00727                clang::Module *Module,
00728                StringRef isysroot, raw_ostream *Out);
00729   ~PCHGenerator();
00730   virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
00731   virtual void HandleTranslationUnit(ASTContext &Ctx);
00732   virtual ASTMutationListener *GetASTMutationListener();
00733   virtual ASTDeserializationListener *GetASTDeserializationListener();
00734 };
00735 
00736 } // end namespace clang
00737 
00738 #endif