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