Home | History | Annotate | Download | only in Serialization
      1 //===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//
      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 implements serialization for Declarations.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "clang/Serialization/ASTWriter.h"
     15 #include "ASTCommon.h"
     16 #include "clang/AST/DeclCXX.h"
     17 #include "clang/AST/DeclContextInternals.h"
     18 #include "clang/AST/DeclTemplate.h"
     19 #include "clang/AST/DeclVisitor.h"
     20 #include "clang/AST/Expr.h"
     21 #include "clang/Basic/SourceManager.h"
     22 #include "clang/Serialization/ASTReader.h"
     23 #include "llvm/ADT/Twine.h"
     24 #include "llvm/Bitcode/BitstreamWriter.h"
     25 #include "llvm/Support/ErrorHandling.h"
     26 using namespace clang;
     27 using namespace serialization;
     28 
     29 //===----------------------------------------------------------------------===//
     30 // Declaration serialization
     31 //===----------------------------------------------------------------------===//
     32 
     33 namespace clang {
     34   class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
     35 
     36     ASTWriter &Writer;
     37     ASTContext &Context;
     38     typedef ASTWriter::RecordData RecordData;
     39     RecordData &Record;
     40 
     41   public:
     42     serialization::DeclCode Code;
     43     unsigned AbbrevToUse;
     44 
     45     ASTDeclWriter(ASTWriter &Writer, ASTContext &Context, RecordData &Record)
     46       : Writer(Writer), Context(Context), Record(Record) {
     47     }
     48 
     49     void Visit(Decl *D);
     50 
     51     void VisitDecl(Decl *D);
     52     void VisitTranslationUnitDecl(TranslationUnitDecl *D);
     53     void VisitNamedDecl(NamedDecl *D);
     54     void VisitLabelDecl(LabelDecl *LD);
     55     void VisitNamespaceDecl(NamespaceDecl *D);
     56     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
     57     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
     58     void VisitTypeDecl(TypeDecl *D);
     59     void VisitTypedefNameDecl(TypedefNameDecl *D);
     60     void VisitTypedefDecl(TypedefDecl *D);
     61     void VisitTypeAliasDecl(TypeAliasDecl *D);
     62     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
     63     void VisitTagDecl(TagDecl *D);
     64     void VisitEnumDecl(EnumDecl *D);
     65     void VisitRecordDecl(RecordDecl *D);
     66     void VisitCXXRecordDecl(CXXRecordDecl *D);
     67     void VisitClassTemplateSpecializationDecl(
     68                                             ClassTemplateSpecializationDecl *D);
     69     void VisitClassTemplatePartialSpecializationDecl(
     70                                      ClassTemplatePartialSpecializationDecl *D);
     71     void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
     72     void VisitVarTemplatePartialSpecializationDecl(
     73         VarTemplatePartialSpecializationDecl *D);
     74     void VisitClassScopeFunctionSpecializationDecl(
     75                                        ClassScopeFunctionSpecializationDecl *D);
     76     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
     77     void VisitValueDecl(ValueDecl *D);
     78     void VisitEnumConstantDecl(EnumConstantDecl *D);
     79     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
     80     void VisitDeclaratorDecl(DeclaratorDecl *D);
     81     void VisitFunctionDecl(FunctionDecl *D);
     82     void VisitCXXMethodDecl(CXXMethodDecl *D);
     83     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
     84     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
     85     void VisitCXXConversionDecl(CXXConversionDecl *D);
     86     void VisitFieldDecl(FieldDecl *D);
     87     void VisitMSPropertyDecl(MSPropertyDecl *D);
     88     void VisitIndirectFieldDecl(IndirectFieldDecl *D);
     89     void VisitVarDecl(VarDecl *D);
     90     void VisitImplicitParamDecl(ImplicitParamDecl *D);
     91     void VisitParmVarDecl(ParmVarDecl *D);
     92     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
     93     void VisitTemplateDecl(TemplateDecl *D);
     94     void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
     95     void VisitClassTemplateDecl(ClassTemplateDecl *D);
     96     void VisitVarTemplateDecl(VarTemplateDecl *D);
     97     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
     98     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
     99     void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
    100     void VisitUsingDecl(UsingDecl *D);
    101     void VisitUsingShadowDecl(UsingShadowDecl *D);
    102     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
    103     void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
    104     void VisitImportDecl(ImportDecl *D);
    105     void VisitAccessSpecDecl(AccessSpecDecl *D);
    106     void VisitFriendDecl(FriendDecl *D);
    107     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
    108     void VisitStaticAssertDecl(StaticAssertDecl *D);
    109     void VisitBlockDecl(BlockDecl *D);
    110     void VisitCapturedDecl(CapturedDecl *D);
    111     void VisitEmptyDecl(EmptyDecl *D);
    112 
    113     void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
    114                           uint64_t VisibleOffset);
    115     template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
    116 
    117 
    118     // FIXME: Put in the same order is DeclNodes.td?
    119     void VisitObjCMethodDecl(ObjCMethodDecl *D);
    120     void VisitObjCContainerDecl(ObjCContainerDecl *D);
    121     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
    122     void VisitObjCIvarDecl(ObjCIvarDecl *D);
    123     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
    124     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
    125     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
    126     void VisitObjCImplDecl(ObjCImplDecl *D);
    127     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
    128     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
    129     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
    130     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
    131     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
    132     void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
    133 
    134     void AddFunctionDefinition(const FunctionDecl *FD) {
    135       assert(FD->doesThisDeclarationHaveABody());
    136       if (auto *CD = dyn_cast<CXXConstructorDecl>(FD))
    137         Writer.AddCXXCtorInitializers(CD->CtorInitializers,
    138                                       CD->NumCtorInitializers, Record);
    139       Writer.AddStmt(FD->getBody());
    140     }
    141   };
    142 }
    143 
    144 void ASTDeclWriter::Visit(Decl *D) {
    145   DeclVisitor<ASTDeclWriter>::Visit(D);
    146 
    147   // Source locations require array (variable-length) abbreviations.  The
    148   // abbreviation infrastructure requires that arrays are encoded last, so
    149   // we handle it here in the case of those classes derived from DeclaratorDecl
    150   if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)){
    151     Writer.AddTypeSourceInfo(DD->getTypeSourceInfo(), Record);
    152   }
    153 
    154   // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
    155   // have been written. We want it last because we will not read it back when
    156   // retrieving it from the AST, we'll just lazily set the offset.
    157   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
    158     Record.push_back(FD->doesThisDeclarationHaveABody());
    159     if (FD->doesThisDeclarationHaveABody())
    160       Writer.AddStmt(FD->getBody());
    161   }
    162 }
    163 
    164 void ASTDeclWriter::VisitDecl(Decl *D) {
    165   Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
    166   Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
    167   Record.push_back(D->isInvalidDecl());
    168   Record.push_back(D->hasAttrs());
    169   if (D->hasAttrs())
    170     Writer.WriteAttributes(ArrayRef<const Attr*>(D->getAttrs().begin(),
    171                                                  D->getAttrs().size()), Record);
    172   Record.push_back(D->isImplicit());
    173   Record.push_back(D->isUsed(false));
    174   Record.push_back(D->isReferenced());
    175   Record.push_back(D->isTopLevelDeclInObjCContainer());
    176   Record.push_back(D->getAccess());
    177   Record.push_back(D->isModulePrivate());
    178   Record.push_back(Writer.inferSubmoduleIDFromLocation(D->getLocation()));
    179 
    180   // If this declaration injected a name into a context different from its
    181   // lexical context, and that context is an imported namespace, we need to
    182   // update its visible declarations to include this name.
    183   //
    184   // This happens when we instantiate a class with a friend declaration or a
    185   // function with a local extern declaration, for instance.
    186   if (D->isOutOfLine()) {
    187     auto *DC = D->getDeclContext();
    188     while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {
    189       if (!NS->isFromASTFile())
    190         break;
    191       Writer.AddUpdatedDeclContext(NS->getPrimaryContext());
    192       if (!NS->isInlineNamespace())
    193         break;
    194       DC = NS->getParent();
    195     }
    196   }
    197 }
    198 
    199 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
    200   llvm_unreachable("Translation units aren't directly serialized");
    201 }
    202 
    203 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
    204   VisitDecl(D);
    205   Writer.AddDeclarationName(D->getDeclName(), Record);
    206 }
    207 
    208 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
    209   VisitNamedDecl(D);
    210   Writer.AddSourceLocation(D->getLocStart(), Record);
    211   Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
    212 }
    213 
    214 void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
    215   VisitRedeclarable(D);
    216   VisitTypeDecl(D);
    217   Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
    218   Record.push_back(D->isModed());
    219   if (D->isModed())
    220     Writer.AddTypeRef(D->getUnderlyingType(), Record);
    221 }
    222 
    223 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
    224   VisitTypedefNameDecl(D);
    225   if (!D->hasAttrs() &&
    226       !D->isImplicit() &&
    227       !D->isUsed(false) &&
    228       D->getFirstDecl() == D->getMostRecentDecl() &&
    229       !D->isInvalidDecl() &&
    230       !D->isReferenced() &&
    231       !D->isTopLevelDeclInObjCContainer() &&
    232       D->getAccess() == AS_none &&
    233       !D->isModulePrivate() &&
    234       D->getDeclName().getNameKind() == DeclarationName::Identifier)
    235     AbbrevToUse = Writer.getDeclTypedefAbbrev();
    236 
    237   Code = serialization::DECL_TYPEDEF;
    238 }
    239 
    240 void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
    241   VisitTypedefNameDecl(D);
    242   Code = serialization::DECL_TYPEALIAS;
    243 }
    244 
    245 void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
    246   VisitRedeclarable(D);
    247   VisitTypeDecl(D);
    248   Record.push_back(D->getIdentifierNamespace());
    249   Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
    250   Record.push_back(D->isCompleteDefinition());
    251   Record.push_back(D->isEmbeddedInDeclarator());
    252   Record.push_back(D->isFreeStanding());
    253   Record.push_back(D->isCompleteDefinitionRequired());
    254   Writer.AddSourceLocation(D->getRBraceLoc(), Record);
    255   Record.push_back(D->hasExtInfo());
    256   if (D->hasExtInfo())
    257     Writer.AddQualifierInfo(*D->getExtInfo(), Record);
    258   else if (D->hasDeclaratorForAnonDecl())
    259     Writer.AddDeclRef(D->getDeclaratorForAnonDecl(), Record);
    260   else
    261     Writer.AddDeclRef(D->getTypedefNameForAnonDecl(), Record);
    262 }
    263 
    264 void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
    265   VisitTagDecl(D);
    266   Writer.AddTypeSourceInfo(D->getIntegerTypeSourceInfo(), Record);
    267   if (!D->getIntegerTypeSourceInfo())
    268     Writer.AddTypeRef(D->getIntegerType(), Record);
    269   Writer.AddTypeRef(D->getPromotionType(), Record);
    270   Record.push_back(D->getNumPositiveBits());
    271   Record.push_back(D->getNumNegativeBits());
    272   Record.push_back(D->isScoped());
    273   Record.push_back(D->isScopedUsingClassTag());
    274   Record.push_back(D->isFixed());
    275   if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
    276     Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
    277     Record.push_back(MemberInfo->getTemplateSpecializationKind());
    278     Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
    279   } else {
    280     Writer.AddDeclRef(nullptr, Record);
    281   }
    282 
    283   if (!D->hasAttrs() &&
    284       !D->isImplicit() &&
    285       !D->isUsed(false) &&
    286       !D->hasExtInfo() &&
    287       D->getFirstDecl() == D->getMostRecentDecl() &&
    288       !D->isInvalidDecl() &&
    289       !D->isReferenced() &&
    290       !D->isTopLevelDeclInObjCContainer() &&
    291       D->getAccess() == AS_none &&
    292       !D->isModulePrivate() &&
    293       !CXXRecordDecl::classofKind(D->getKind()) &&
    294       !D->getIntegerTypeSourceInfo() &&
    295       !D->getMemberSpecializationInfo() &&
    296       D->getDeclName().getNameKind() == DeclarationName::Identifier)
    297     AbbrevToUse = Writer.getDeclEnumAbbrev();
    298 
    299   Code = serialization::DECL_ENUM;
    300 }
    301 
    302 void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
    303   VisitTagDecl(D);
    304   Record.push_back(D->hasFlexibleArrayMember());
    305   Record.push_back(D->isAnonymousStructOrUnion());
    306   Record.push_back(D->hasObjectMember());
    307   Record.push_back(D->hasVolatileMember());
    308 
    309   if (!D->hasAttrs() &&
    310       !D->isImplicit() &&
    311       !D->isUsed(false) &&
    312       !D->hasExtInfo() &&
    313       D->getFirstDecl() == D->getMostRecentDecl() &&
    314       !D->isInvalidDecl() &&
    315       !D->isReferenced() &&
    316       !D->isTopLevelDeclInObjCContainer() &&
    317       D->getAccess() == AS_none &&
    318       !D->isModulePrivate() &&
    319       !CXXRecordDecl::classofKind(D->getKind()) &&
    320       D->getDeclName().getNameKind() == DeclarationName::Identifier)
    321     AbbrevToUse = Writer.getDeclRecordAbbrev();
    322 
    323   Code = serialization::DECL_RECORD;
    324 }
    325 
    326 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
    327   VisitNamedDecl(D);
    328   Writer.AddTypeRef(D->getType(), Record);
    329 }
    330 
    331 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
    332   VisitValueDecl(D);
    333   Record.push_back(D->getInitExpr()? 1 : 0);
    334   if (D->getInitExpr())
    335     Writer.AddStmt(D->getInitExpr());
    336   Writer.AddAPSInt(D->getInitVal(), Record);
    337 
    338   Code = serialization::DECL_ENUM_CONSTANT;
    339 }
    340 
    341 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
    342   VisitValueDecl(D);
    343   Writer.AddSourceLocation(D->getInnerLocStart(), Record);
    344   Record.push_back(D->hasExtInfo());
    345   if (D->hasExtInfo())
    346     Writer.AddQualifierInfo(*D->getExtInfo(), Record);
    347 }
    348 
    349 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
    350   VisitRedeclarable(D);
    351   VisitDeclaratorDecl(D);
    352 
    353   Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
    354   Record.push_back(D->getIdentifierNamespace());
    355 
    356   // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
    357   // after everything else is written.
    358 
    359   Record.push_back(D->getStorageClass()); // FIXME: stable encoding
    360   Record.push_back(D->IsInline);
    361   Record.push_back(D->isInlineSpecified());
    362   Record.push_back(D->isVirtualAsWritten());
    363   Record.push_back(D->isPure());
    364   Record.push_back(D->hasInheritedPrototype());
    365   Record.push_back(D->hasWrittenPrototype());
    366   Record.push_back(D->isDeletedAsWritten());
    367   Record.push_back(D->isTrivial());
    368   Record.push_back(D->isDefaulted());
    369   Record.push_back(D->isExplicitlyDefaulted());
    370   Record.push_back(D->hasImplicitReturnZero());
    371   Record.push_back(D->isConstexpr());
    372   Record.push_back(D->HasSkippedBody);
    373   Record.push_back(D->isLateTemplateParsed());
    374   Record.push_back(D->getLinkageInternal());
    375   Writer.AddSourceLocation(D->getLocEnd(), Record);
    376 
    377   Record.push_back(D->getTemplatedKind());
    378   switch (D->getTemplatedKind()) {
    379   case FunctionDecl::TK_NonTemplate:
    380     break;
    381   case FunctionDecl::TK_FunctionTemplate:
    382     Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
    383     break;
    384   case FunctionDecl::TK_MemberSpecialization: {
    385     MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
    386     Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
    387     Record.push_back(MemberInfo->getTemplateSpecializationKind());
    388     Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
    389     break;
    390   }
    391   case FunctionDecl::TK_FunctionTemplateSpecialization: {
    392     FunctionTemplateSpecializationInfo *
    393       FTSInfo = D->getTemplateSpecializationInfo();
    394     Writer.AddDeclRef(FTSInfo->getTemplate(), Record);
    395     Record.push_back(FTSInfo->getTemplateSpecializationKind());
    396 
    397     // Template arguments.
    398     Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record);
    399 
    400     // Template args as written.
    401     Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);
    402     if (FTSInfo->TemplateArgumentsAsWritten) {
    403       Record.push_back(FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs);
    404       for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs;
    405              i!=e; ++i)
    406         Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
    407                                       Record);
    408       Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc,
    409                                Record);
    410       Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc,
    411                                Record);
    412     }
    413 
    414     Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record);
    415 
    416     if (D->isCanonicalDecl()) {
    417       // Write the template that contains the specializations set. We will
    418       // add a FunctionTemplateSpecializationInfo to it when reading.
    419       Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record);
    420     }
    421     break;
    422   }
    423   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
    424     DependentFunctionTemplateSpecializationInfo *
    425       DFTSInfo = D->getDependentSpecializationInfo();
    426 
    427     // Templates.
    428     Record.push_back(DFTSInfo->getNumTemplates());
    429     for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
    430       Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
    431 
    432     // Templates args.
    433     Record.push_back(DFTSInfo->getNumTemplateArgs());
    434     for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
    435       Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
    436     Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record);
    437     Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record);
    438     break;
    439   }
    440   }
    441 
    442   Record.push_back(D->param_size());
    443   for (auto P : D->params())
    444     Writer.AddDeclRef(P, Record);
    445   Code = serialization::DECL_FUNCTION;
    446 }
    447 
    448 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
    449   VisitNamedDecl(D);
    450   // FIXME: convert to LazyStmtPtr?
    451   // Unlike C/C++, method bodies will never be in header files.
    452   bool HasBodyStuff = D->getBody() != nullptr     ||
    453                       D->getSelfDecl() != nullptr || D->getCmdDecl() != nullptr;
    454   Record.push_back(HasBodyStuff);
    455   if (HasBodyStuff) {
    456     Writer.AddStmt(D->getBody());
    457     Writer.AddDeclRef(D->getSelfDecl(), Record);
    458     Writer.AddDeclRef(D->getCmdDecl(), Record);
    459   }
    460   Record.push_back(D->isInstanceMethod());
    461   Record.push_back(D->isVariadic());
    462   Record.push_back(D->isPropertyAccessor());
    463   Record.push_back(D->isDefined());
    464   Record.push_back(D->IsOverriding);
    465   Record.push_back(D->HasSkippedBody);
    466 
    467   Record.push_back(D->IsRedeclaration);
    468   Record.push_back(D->HasRedeclaration);
    469   if (D->HasRedeclaration) {
    470     assert(Context.getObjCMethodRedeclaration(D));
    471     Writer.AddDeclRef(Context.getObjCMethodRedeclaration(D), Record);
    472   }
    473 
    474   // FIXME: stable encoding for @required/@optional
    475   Record.push_back(D->getImplementationControl());
    476   // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
    477   Record.push_back(D->getObjCDeclQualifier());
    478   Record.push_back(D->hasRelatedResultType());
    479   Writer.AddTypeRef(D->getReturnType(), Record);
    480   Writer.AddTypeSourceInfo(D->getReturnTypeSourceInfo(), Record);
    481   Writer.AddSourceLocation(D->getLocEnd(), Record);
    482   Record.push_back(D->param_size());
    483   for (const auto *P : D->params())
    484     Writer.AddDeclRef(P, Record);
    485 
    486   Record.push_back(D->SelLocsKind);
    487   unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
    488   SourceLocation *SelLocs = D->getStoredSelLocs();
    489   Record.push_back(NumStoredSelLocs);
    490   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
    491     Writer.AddSourceLocation(SelLocs[i], Record);
    492 
    493   Code = serialization::DECL_OBJC_METHOD;
    494 }
    495 
    496 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
    497   VisitNamedDecl(D);
    498   Writer.AddSourceLocation(D->getAtStartLoc(), Record);
    499   Writer.AddSourceRange(D->getAtEndRange(), Record);
    500   // Abstract class (no need to define a stable serialization::DECL code).
    501 }
    502 
    503 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
    504   VisitRedeclarable(D);
    505   VisitObjCContainerDecl(D);
    506   Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
    507 
    508   Record.push_back(D->isThisDeclarationADefinition());
    509   if (D->isThisDeclarationADefinition()) {
    510     // Write the DefinitionData
    511     ObjCInterfaceDecl::DefinitionData &Data = D->data();
    512 
    513     Writer.AddDeclRef(D->getSuperClass(), Record);
    514     Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
    515     Writer.AddSourceLocation(D->getEndOfDefinitionLoc(), Record);
    516     Record.push_back(Data.HasDesignatedInitializers);
    517 
    518     // Write out the protocols that are directly referenced by the @interface.
    519     Record.push_back(Data.ReferencedProtocols.size());
    520     for (const auto *P : D->protocols())
    521       Writer.AddDeclRef(P, Record);
    522     for (const auto &PL : D->protocol_locs())
    523       Writer.AddSourceLocation(PL, Record);
    524 
    525     // Write out the protocols that are transitively referenced.
    526     Record.push_back(Data.AllReferencedProtocols.size());
    527     for (ObjCList<ObjCProtocolDecl>::iterator
    528               P = Data.AllReferencedProtocols.begin(),
    529            PEnd = Data.AllReferencedProtocols.end();
    530          P != PEnd; ++P)
    531       Writer.AddDeclRef(*P, Record);
    532 
    533 
    534     if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
    535       // Ensure that we write out the set of categories for this class.
    536       Writer.ObjCClassesWithCategories.insert(D);
    537 
    538       // Make sure that the categories get serialized.
    539       for (; Cat; Cat = Cat->getNextClassCategoryRaw())
    540         (void)Writer.GetDeclRef(Cat);
    541     }
    542   }
    543 
    544   Code = serialization::DECL_OBJC_INTERFACE;
    545 }
    546 
    547 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
    548   VisitFieldDecl(D);
    549   // FIXME: stable encoding for @public/@private/@protected/@package
    550   Record.push_back(D->getAccessControl());
    551   Record.push_back(D->getSynthesize());
    552 
    553   if (!D->hasAttrs() &&
    554       !D->isImplicit() &&
    555       !D->isUsed(false) &&
    556       !D->isInvalidDecl() &&
    557       !D->isReferenced() &&
    558       !D->isModulePrivate() &&
    559       !D->getBitWidth() &&
    560       !D->hasExtInfo() &&
    561       D->getDeclName())
    562     AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
    563 
    564   Code = serialization::DECL_OBJC_IVAR;
    565 }
    566 
    567 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
    568   VisitRedeclarable(D);
    569   VisitObjCContainerDecl(D);
    570 
    571   Record.push_back(D->isThisDeclarationADefinition());
    572   if (D->isThisDeclarationADefinition()) {
    573     Record.push_back(D->protocol_size());
    574     for (const auto *I : D->protocols())
    575       Writer.AddDeclRef(I, Record);
    576     for (const auto &PL : D->protocol_locs())
    577       Writer.AddSourceLocation(PL, Record);
    578   }
    579 
    580   Code = serialization::DECL_OBJC_PROTOCOL;
    581 }
    582 
    583 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
    584   VisitFieldDecl(D);
    585   Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
    586 }
    587 
    588 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
    589   VisitObjCContainerDecl(D);
    590   Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
    591   Writer.AddSourceLocation(D->getIvarLBraceLoc(), Record);
    592   Writer.AddSourceLocation(D->getIvarRBraceLoc(), Record);
    593   Writer.AddDeclRef(D->getClassInterface(), Record);
    594   Record.push_back(D->protocol_size());
    595   for (const auto *I : D->protocols())
    596     Writer.AddDeclRef(I, Record);
    597   for (const auto &PL : D->protocol_locs())
    598     Writer.AddSourceLocation(PL, Record);
    599   Code = serialization::DECL_OBJC_CATEGORY;
    600 }
    601 
    602 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
    603   VisitNamedDecl(D);
    604   Writer.AddDeclRef(D->getClassInterface(), Record);
    605   Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
    606 }
    607 
    608 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
    609   VisitNamedDecl(D);
    610   Writer.AddSourceLocation(D->getAtLoc(), Record);
    611   Writer.AddSourceLocation(D->getLParenLoc(), Record);
    612   Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
    613   // FIXME: stable encoding
    614   Record.push_back((unsigned)D->getPropertyAttributes());
    615   Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
    616   // FIXME: stable encoding
    617   Record.push_back((unsigned)D->getPropertyImplementation());
    618   Writer.AddDeclarationName(D->getGetterName(), Record);
    619   Writer.AddDeclarationName(D->getSetterName(), Record);
    620   Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
    621   Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
    622   Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
    623   Code = serialization::DECL_OBJC_PROPERTY;
    624 }
    625 
    626 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
    627   VisitObjCContainerDecl(D);
    628   Writer.AddDeclRef(D->getClassInterface(), Record);
    629   // Abstract class (no need to define a stable serialization::DECL code).
    630 }
    631 
    632 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
    633   VisitObjCImplDecl(D);
    634   Writer.AddIdentifierRef(D->getIdentifier(), Record);
    635   Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
    636   Code = serialization::DECL_OBJC_CATEGORY_IMPL;
    637 }
    638 
    639 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
    640   VisitObjCImplDecl(D);
    641   Writer.AddDeclRef(D->getSuperClass(), Record);
    642   Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
    643   Writer.AddSourceLocation(D->getIvarLBraceLoc(), Record);
    644   Writer.AddSourceLocation(D->getIvarRBraceLoc(), Record);
    645   Record.push_back(D->hasNonZeroConstructors());
    646   Record.push_back(D->hasDestructors());
    647   Writer.AddCXXCtorInitializers(D->IvarInitializers, D->NumIvarInitializers,
    648                                 Record);
    649   Code = serialization::DECL_OBJC_IMPLEMENTATION;
    650 }
    651 
    652 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
    653   VisitDecl(D);
    654   Writer.AddSourceLocation(D->getLocStart(), Record);
    655   Writer.AddDeclRef(D->getPropertyDecl(), Record);
    656   Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
    657   Writer.AddSourceLocation(D->getPropertyIvarDeclLoc(), Record);
    658   Writer.AddStmt(D->getGetterCXXConstructor());
    659   Writer.AddStmt(D->getSetterCXXAssignment());
    660   Code = serialization::DECL_OBJC_PROPERTY_IMPL;
    661 }
    662 
    663 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
    664   VisitDeclaratorDecl(D);
    665   Record.push_back(D->isMutable());
    666   if (D->InitializerOrBitWidth.getInt() != ICIS_NoInit ||
    667       D->InitializerOrBitWidth.getPointer()) {
    668     Record.push_back(D->InitializerOrBitWidth.getInt() + 1);
    669     Writer.AddStmt(D->InitializerOrBitWidth.getPointer());
    670   } else {
    671     Record.push_back(0);
    672   }
    673   if (!D->getDeclName())
    674     Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record);
    675 
    676   if (!D->hasAttrs() &&
    677       !D->isImplicit() &&
    678       !D->isUsed(false) &&
    679       !D->isInvalidDecl() &&
    680       !D->isReferenced() &&
    681       !D->isTopLevelDeclInObjCContainer() &&
    682       !D->isModulePrivate() &&
    683       !D->getBitWidth() &&
    684       !D->hasInClassInitializer() &&
    685       !D->hasExtInfo() &&
    686       !ObjCIvarDecl::classofKind(D->getKind()) &&
    687       !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
    688       D->getDeclName())
    689     AbbrevToUse = Writer.getDeclFieldAbbrev();
    690 
    691   Code = serialization::DECL_FIELD;
    692 }
    693 
    694 void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
    695   VisitDeclaratorDecl(D);
    696   Writer.AddIdentifierRef(D->getGetterId(), Record);
    697   Writer.AddIdentifierRef(D->getSetterId(), Record);
    698   Code = serialization::DECL_MS_PROPERTY;
    699 }
    700 
    701 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
    702   VisitValueDecl(D);
    703   Record.push_back(D->getChainingSize());
    704 
    705   for (const auto *P : D->chain())
    706     Writer.AddDeclRef(P, Record);
    707   Code = serialization::DECL_INDIRECTFIELD;
    708 }
    709 
    710 void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
    711   VisitRedeclarable(D);
    712   VisitDeclaratorDecl(D);
    713   Record.push_back(D->getStorageClass());
    714   Record.push_back(D->getTSCSpec());
    715   Record.push_back(D->getInitStyle());
    716   Record.push_back(D->isExceptionVariable());
    717   Record.push_back(D->isNRVOVariable());
    718   Record.push_back(D->isCXXForRangeDecl());
    719   Record.push_back(D->isARCPseudoStrong());
    720   Record.push_back(D->isConstexpr());
    721   Record.push_back(D->isInitCapture());
    722   Record.push_back(D->isPreviousDeclInSameBlockScope());
    723   Record.push_back(D->getLinkageInternal());
    724 
    725   if (D->getInit()) {
    726     Record.push_back(!D->isInitKnownICE() ? 1 : (D->isInitICE() ? 3 : 2));
    727     Writer.AddStmt(D->getInit());
    728   } else {
    729     Record.push_back(0);
    730   }
    731 
    732   enum {
    733     VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
    734   };
    735   if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {
    736     Record.push_back(VarTemplate);
    737     Writer.AddDeclRef(TemplD, Record);
    738   } else if (MemberSpecializationInfo *SpecInfo
    739                = D->getMemberSpecializationInfo()) {
    740     Record.push_back(StaticDataMemberSpecialization);
    741     Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record);
    742     Record.push_back(SpecInfo->getTemplateSpecializationKind());
    743     Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record);
    744   } else {
    745     Record.push_back(VarNotTemplate);
    746   }
    747 
    748   if (!D->hasAttrs() &&
    749       !D->isImplicit() &&
    750       !D->isUsed(false) &&
    751       !D->isInvalidDecl() &&
    752       !D->isReferenced() &&
    753       !D->isTopLevelDeclInObjCContainer() &&
    754       D->getAccess() == AS_none &&
    755       !D->isModulePrivate() &&
    756       D->getDeclName().getNameKind() == DeclarationName::Identifier &&
    757       !D->hasExtInfo() &&
    758       D->getFirstDecl() == D->getMostRecentDecl() &&
    759       D->getInitStyle() == VarDecl::CInit &&
    760       D->getInit() == nullptr &&
    761       !isa<ParmVarDecl>(D) &&
    762       !isa<VarTemplateSpecializationDecl>(D) &&
    763       !D->isConstexpr() &&
    764       !D->isInitCapture() &&
    765       !D->isPreviousDeclInSameBlockScope() &&
    766       !D->getMemberSpecializationInfo())
    767     AbbrevToUse = Writer.getDeclVarAbbrev();
    768 
    769   Code = serialization::DECL_VAR;
    770 }
    771 
    772 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
    773   VisitVarDecl(D);
    774   Code = serialization::DECL_IMPLICIT_PARAM;
    775 }
    776 
    777 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
    778   VisitVarDecl(D);
    779   Record.push_back(D->isObjCMethodParameter());
    780   Record.push_back(D->getFunctionScopeDepth());
    781   Record.push_back(D->getFunctionScopeIndex());
    782   Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
    783   Record.push_back(D->isKNRPromoted());
    784   Record.push_back(D->hasInheritedDefaultArg());
    785   Record.push_back(D->hasUninstantiatedDefaultArg());
    786   if (D->hasUninstantiatedDefaultArg())
    787     Writer.AddStmt(D->getUninstantiatedDefaultArg());
    788   Code = serialization::DECL_PARM_VAR;
    789 
    790   assert(!D->isARCPseudoStrong()); // can be true of ImplicitParamDecl
    791 
    792   // If the assumptions about the DECL_PARM_VAR abbrev are true, use it.  Here
    793   // we dynamically check for the properties that we optimize for, but don't
    794   // know are true of all PARM_VAR_DECLs.
    795   if (!D->hasAttrs() &&
    796       !D->hasExtInfo() &&
    797       !D->isImplicit() &&
    798       !D->isUsed(false) &&
    799       !D->isInvalidDecl() &&
    800       !D->isReferenced() &&
    801       D->getAccess() == AS_none &&
    802       !D->isModulePrivate() &&
    803       D->getStorageClass() == 0 &&
    804       D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
    805       D->getFunctionScopeDepth() == 0 &&
    806       D->getObjCDeclQualifier() == 0 &&
    807       !D->isKNRPromoted() &&
    808       !D->hasInheritedDefaultArg() &&
    809       D->getInit() == nullptr &&
    810       !D->hasUninstantiatedDefaultArg())  // No default expr.
    811     AbbrevToUse = Writer.getDeclParmVarAbbrev();
    812 
    813   // Check things we know are true of *every* PARM_VAR_DECL, which is more than
    814   // just us assuming it.
    815   assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
    816   assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
    817   assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
    818   assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");
    819   assert(!D->isStaticDataMember() &&
    820          "PARM_VAR_DECL can't be static data member");
    821 }
    822 
    823 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
    824   VisitDecl(D);
    825   Writer.AddStmt(D->getAsmString());
    826   Writer.AddSourceLocation(D->getRParenLoc(), Record);
    827   Code = serialization::DECL_FILE_SCOPE_ASM;
    828 }
    829 
    830 void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
    831   VisitDecl(D);
    832   Code = serialization::DECL_EMPTY;
    833 }
    834 
    835 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
    836   VisitDecl(D);
    837   Writer.AddStmt(D->getBody());
    838   Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
    839   Record.push_back(D->param_size());
    840   for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
    841        P != PEnd; ++P)
    842     Writer.AddDeclRef(*P, Record);
    843   Record.push_back(D->isVariadic());
    844   Record.push_back(D->blockMissingReturnType());
    845   Record.push_back(D->isConversionFromLambda());
    846   Record.push_back(D->capturesCXXThis());
    847   Record.push_back(D->getNumCaptures());
    848   for (const auto &capture : D->captures()) {
    849     Writer.AddDeclRef(capture.getVariable(), Record);
    850 
    851     unsigned flags = 0;
    852     if (capture.isByRef()) flags |= 1;
    853     if (capture.isNested()) flags |= 2;
    854     if (capture.hasCopyExpr()) flags |= 4;
    855     Record.push_back(flags);
    856 
    857     if (capture.hasCopyExpr()) Writer.AddStmt(capture.getCopyExpr());
    858   }
    859 
    860   Code = serialization::DECL_BLOCK;
    861 }
    862 
    863 void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
    864   Record.push_back(CD->getNumParams());
    865   VisitDecl(CD);
    866   Record.push_back(CD->getContextParamPosition());
    867   Record.push_back(CD->isNothrow() ? 1 : 0);
    868   // Body is stored by VisitCapturedStmt.
    869   for (unsigned I = 0; I < CD->getNumParams(); ++I)
    870     Writer.AddDeclRef(CD->getParam(I), Record);
    871   Code = serialization::DECL_CAPTURED;
    872 }
    873 
    874 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
    875   VisitDecl(D);
    876   Record.push_back(D->getLanguage());
    877   Writer.AddSourceLocation(D->getExternLoc(), Record);
    878   Writer.AddSourceLocation(D->getRBraceLoc(), Record);
    879   Code = serialization::DECL_LINKAGE_SPEC;
    880 }
    881 
    882 void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
    883   VisitNamedDecl(D);
    884   Writer.AddSourceLocation(D->getLocStart(), Record);
    885   Code = serialization::DECL_LABEL;
    886 }
    887 
    888 
    889 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
    890   VisitRedeclarable(D);
    891   VisitNamedDecl(D);
    892   Record.push_back(D->isInline());
    893   Writer.AddSourceLocation(D->getLocStart(), Record);
    894   Writer.AddSourceLocation(D->getRBraceLoc(), Record);
    895 
    896   if (D->isOriginalNamespace())
    897     Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
    898   Code = serialization::DECL_NAMESPACE;
    899 
    900   if (Writer.hasChain() && !D->isOriginalNamespace() &&
    901       D->getOriginalNamespace()->isFromASTFile()) {
    902     NamespaceDecl *NS = D->getOriginalNamespace();
    903     Writer.AddUpdatedDeclContext(NS);
    904 
    905     // Make sure all visible decls are written. They will be recorded later.
    906     if (StoredDeclsMap *Map = NS->buildLookup()) {
    907       for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
    908            D != DEnd; ++D) {
    909         DeclContext::lookup_result R = D->second.getLookupResult();
    910         for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
    911              ++I)
    912           Writer.GetDeclRef(*I);
    913       }
    914     }
    915   }
    916 
    917   if (Writer.hasChain() && D->isAnonymousNamespace() &&
    918       D == D->getMostRecentDecl()) {
    919     // This is a most recent reopening of the anonymous namespace. If its parent
    920     // is in a previous PCH (or is the TU), mark that parent for update, because
    921     // the original namespace always points to the latest re-opening of its
    922     // anonymous namespace.
    923     Decl *Parent = cast<Decl>(
    924         D->getParent()->getRedeclContext()->getPrimaryContext());
    925     if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
    926       Writer.DeclUpdates[Parent].push_back(
    927           ASTWriter::DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, D));
    928     }
    929   }
    930 }
    931 
    932 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
    933   VisitNamedDecl(D);
    934   Writer.AddSourceLocation(D->getNamespaceLoc(), Record);
    935   Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
    936   Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
    937   Writer.AddDeclRef(D->getNamespace(), Record);
    938   Code = serialization::DECL_NAMESPACE_ALIAS;
    939 }
    940 
    941 void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
    942   VisitNamedDecl(D);
    943   Writer.AddSourceLocation(D->getUsingLoc(), Record);
    944   Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
    945   Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
    946   Writer.AddDeclRef(D->FirstUsingShadow.getPointer(), Record);
    947   Record.push_back(D->hasTypename());
    948   Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record);
    949   Code = serialization::DECL_USING;
    950 }
    951 
    952 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
    953   VisitRedeclarable(D);
    954   VisitNamedDecl(D);
    955   Writer.AddDeclRef(D->getTargetDecl(), Record);
    956   Writer.AddDeclRef(D->UsingOrNextShadow, Record);
    957   Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record);
    958   Code = serialization::DECL_USING_SHADOW;
    959 }
    960 
    961 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
    962   VisitNamedDecl(D);
    963   Writer.AddSourceLocation(D->getUsingLoc(), Record);
    964   Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
    965   Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
    966   Writer.AddDeclRef(D->getNominatedNamespace(), Record);
    967   Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
    968   Code = serialization::DECL_USING_DIRECTIVE;
    969 }
    970 
    971 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
    972   VisitValueDecl(D);
    973   Writer.AddSourceLocation(D->getUsingLoc(), Record);
    974   Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
    975   Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
    976   Code = serialization::DECL_UNRESOLVED_USING_VALUE;
    977 }
    978 
    979 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
    980                                                UnresolvedUsingTypenameDecl *D) {
    981   VisitTypeDecl(D);
    982   Writer.AddSourceLocation(D->getTypenameLoc(), Record);
    983   Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
    984   Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
    985 }
    986 
    987 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
    988   VisitRecordDecl(D);
    989 
    990   enum {
    991     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
    992   };
    993   if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
    994     Record.push_back(CXXRecTemplate);
    995     Writer.AddDeclRef(TemplD, Record);
    996   } else if (MemberSpecializationInfo *MSInfo
    997                = D->getMemberSpecializationInfo()) {
    998     Record.push_back(CXXRecMemberSpecialization);
    999     Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
   1000     Record.push_back(MSInfo->getTemplateSpecializationKind());
   1001     Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
   1002   } else {
   1003     Record.push_back(CXXRecNotTemplate);
   1004   }
   1005 
   1006   Record.push_back(D->isThisDeclarationADefinition());
   1007   if (D->isThisDeclarationADefinition())
   1008     Writer.AddCXXDefinitionData(D, Record);
   1009 
   1010   // Store (what we currently believe to be) the key function to avoid
   1011   // deserializing every method so we can compute it.
   1012   if (D->IsCompleteDefinition)
   1013     Writer.AddDeclRef(Context.getCurrentKeyFunction(D), Record);
   1014 
   1015   Code = serialization::DECL_CXX_RECORD;
   1016 }
   1017 
   1018 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
   1019   VisitFunctionDecl(D);
   1020   if (D->isCanonicalDecl()) {
   1021     Record.push_back(D->size_overridden_methods());
   1022     for (CXXMethodDecl::method_iterator
   1023            I = D->begin_overridden_methods(), E = D->end_overridden_methods();
   1024            I != E; ++I)
   1025       Writer.AddDeclRef(*I, Record);
   1026   } else {
   1027     // We only need to record overridden methods once for the canonical decl.
   1028     Record.push_back(0);
   1029   }
   1030   Code = serialization::DECL_CXX_METHOD;
   1031 }
   1032 
   1033 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
   1034   VisitCXXMethodDecl(D);
   1035 
   1036   Writer.AddDeclRef(D->getInheritedConstructor(), Record);
   1037   Record.push_back(D->IsExplicitSpecified);
   1038   Writer.AddCXXCtorInitializers(D->CtorInitializers, D->NumCtorInitializers,
   1039                                 Record);
   1040 
   1041   Code = serialization::DECL_CXX_CONSTRUCTOR;
   1042 }
   1043 
   1044 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
   1045   VisitCXXMethodDecl(D);
   1046 
   1047   Writer.AddDeclRef(D->OperatorDelete, Record);
   1048 
   1049   Code = serialization::DECL_CXX_DESTRUCTOR;
   1050 }
   1051 
   1052 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
   1053   VisitCXXMethodDecl(D);
   1054   Record.push_back(D->IsExplicitSpecified);
   1055   Code = serialization::DECL_CXX_CONVERSION;
   1056 }
   1057 
   1058 void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
   1059   VisitDecl(D);
   1060   Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
   1061   ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
   1062   Record.push_back(!IdentifierLocs.empty());
   1063   if (IdentifierLocs.empty()) {
   1064     Writer.AddSourceLocation(D->getLocEnd(), Record);
   1065     Record.push_back(1);
   1066   } else {
   1067     for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
   1068       Writer.AddSourceLocation(IdentifierLocs[I], Record);
   1069     Record.push_back(IdentifierLocs.size());
   1070   }
   1071   // Note: the number of source locations must always be the last element in
   1072   // the record.
   1073   Code = serialization::DECL_IMPORT;
   1074 }
   1075 
   1076 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
   1077   VisitDecl(D);
   1078   Writer.AddSourceLocation(D->getColonLoc(), Record);
   1079   Code = serialization::DECL_ACCESS_SPEC;
   1080 }
   1081 
   1082 void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
   1083   // Record the number of friend type template parameter lists here
   1084   // so as to simplify memory allocation during deserialization.
   1085   Record.push_back(D->NumTPLists);
   1086   VisitDecl(D);
   1087   bool hasFriendDecl = D->Friend.is<NamedDecl*>();
   1088   Record.push_back(hasFriendDecl);
   1089   if (hasFriendDecl)
   1090     Writer.AddDeclRef(D->getFriendDecl(), Record);
   1091   else
   1092     Writer.AddTypeSourceInfo(D->getFriendType(), Record);
   1093   for (unsigned i = 0; i < D->NumTPLists; ++i)
   1094     Writer.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i),
   1095                                     Record);
   1096   Writer.AddDeclRef(D->getNextFriend(), Record);
   1097   Record.push_back(D->UnsupportedFriend);
   1098   Writer.AddSourceLocation(D->FriendLoc, Record);
   1099   Code = serialization::DECL_FRIEND;
   1100 }
   1101 
   1102 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
   1103   VisitDecl(D);
   1104   Record.push_back(D->getNumTemplateParameters());
   1105   for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
   1106     Writer.AddTemplateParameterList(D->getTemplateParameterList(i), Record);
   1107   Record.push_back(D->getFriendDecl() != nullptr);
   1108   if (D->getFriendDecl())
   1109     Writer.AddDeclRef(D->getFriendDecl(), Record);
   1110   else
   1111     Writer.AddTypeSourceInfo(D->getFriendType(), Record);
   1112   Writer.AddSourceLocation(D->getFriendLoc(), Record);
   1113   Code = serialization::DECL_FRIEND_TEMPLATE;
   1114 }
   1115 
   1116 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
   1117   VisitNamedDecl(D);
   1118 
   1119   Writer.AddDeclRef(D->getTemplatedDecl(), Record);
   1120   Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
   1121 }
   1122 
   1123 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
   1124   VisitRedeclarable(D);
   1125 
   1126   // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
   1127   // getCommonPtr() can be used while this is still initializing.
   1128   if (D->isFirstDecl()) {
   1129     // This declaration owns the 'common' pointer, so serialize that data now.
   1130     Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
   1131     if (D->getInstantiatedFromMemberTemplate())
   1132       Record.push_back(D->isMemberSpecialization());
   1133   }
   1134 
   1135   VisitTemplateDecl(D);
   1136   Record.push_back(D->getIdentifierNamespace());
   1137 }
   1138 
   1139 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
   1140   VisitRedeclarableTemplateDecl(D);
   1141 
   1142   if (D->isFirstDecl()) {
   1143     typedef llvm::FoldingSetVector<ClassTemplateSpecializationDecl> CTSDSetTy;
   1144     CTSDSetTy &CTSDSet = D->getSpecializations();
   1145     Record.push_back(CTSDSet.size());
   1146     for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) {
   1147       assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
   1148       Writer.AddDeclRef(&*I, Record);
   1149     }
   1150 
   1151     typedef llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
   1152       CTPSDSetTy;
   1153     CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
   1154     Record.push_back(CTPSDSet.size());
   1155     for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) {
   1156       assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
   1157       Writer.AddDeclRef(&*I, Record);
   1158     }
   1159   }
   1160   Code = serialization::DECL_CLASS_TEMPLATE;
   1161 }
   1162 
   1163 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
   1164                                            ClassTemplateSpecializationDecl *D) {
   1165   VisitCXXRecordDecl(D);
   1166 
   1167   llvm::PointerUnion<ClassTemplateDecl *,
   1168                      ClassTemplatePartialSpecializationDecl *> InstFrom
   1169     = D->getSpecializedTemplateOrPartial();
   1170   if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
   1171     Writer.AddDeclRef(InstFromD, Record);
   1172   } else {
   1173     Writer.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>(),
   1174                       Record);
   1175     Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
   1176   }
   1177 
   1178   Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
   1179   Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
   1180   Record.push_back(D->getSpecializationKind());
   1181   Record.push_back(D->isCanonicalDecl());
   1182 
   1183   if (D->isCanonicalDecl()) {
   1184     // When reading, we'll add it to the folding set of the following template.
   1185     Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
   1186   }
   1187 
   1188   // Explicit info.
   1189   Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
   1190   if (D->getTypeAsWritten()) {
   1191     Writer.AddSourceLocation(D->getExternLoc(), Record);
   1192     Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
   1193   }
   1194 
   1195   Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
   1196 }
   1197 
   1198 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
   1199                                     ClassTemplatePartialSpecializationDecl *D) {
   1200   VisitClassTemplateSpecializationDecl(D);
   1201 
   1202   Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
   1203   Writer.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten(), Record);
   1204 
   1205   // These are read/set from/to the first declaration.
   1206   if (D->getPreviousDecl() == nullptr) {
   1207     Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
   1208     Record.push_back(D->isMemberSpecialization());
   1209   }
   1210 
   1211   Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
   1212 }
   1213 
   1214 void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
   1215   VisitRedeclarableTemplateDecl(D);
   1216 
   1217   if (D->isFirstDecl()) {
   1218     typedef llvm::FoldingSetVector<VarTemplateSpecializationDecl> VTSDSetTy;
   1219     VTSDSetTy &VTSDSet = D->getSpecializations();
   1220     Record.push_back(VTSDSet.size());
   1221     for (VTSDSetTy::iterator I = VTSDSet.begin(), E = VTSDSet.end(); I != E;
   1222          ++I) {
   1223       assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
   1224       Writer.AddDeclRef(&*I, Record);
   1225     }
   1226 
   1227     typedef llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
   1228     VTPSDSetTy;
   1229     VTPSDSetTy &VTPSDSet = D->getPartialSpecializations();
   1230     Record.push_back(VTPSDSet.size());
   1231     for (VTPSDSetTy::iterator I = VTPSDSet.begin(), E = VTPSDSet.end(); I != E;
   1232          ++I) {
   1233       assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
   1234       Writer.AddDeclRef(&*I, Record);
   1235     }
   1236   }
   1237   Code = serialization::DECL_VAR_TEMPLATE;
   1238 }
   1239 
   1240 void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
   1241     VarTemplateSpecializationDecl *D) {
   1242   VisitVarDecl(D);
   1243 
   1244   llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
   1245   InstFrom = D->getSpecializedTemplateOrPartial();
   1246   if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
   1247     Writer.AddDeclRef(InstFromD, Record);
   1248   } else {
   1249     Writer.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>(),
   1250                       Record);
   1251     Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
   1252   }
   1253 
   1254   // Explicit info.
   1255   Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
   1256   if (D->getTypeAsWritten()) {
   1257     Writer.AddSourceLocation(D->getExternLoc(), Record);
   1258     Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
   1259   }
   1260 
   1261   Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
   1262   Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
   1263   Record.push_back(D->getSpecializationKind());
   1264   Record.push_back(D->isCanonicalDecl());
   1265 
   1266   if (D->isCanonicalDecl()) {
   1267     // When reading, we'll add it to the folding set of the following template.
   1268     Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
   1269   }
   1270 
   1271   Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
   1272 }
   1273 
   1274 void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
   1275     VarTemplatePartialSpecializationDecl *D) {
   1276   VisitVarTemplateSpecializationDecl(D);
   1277 
   1278   Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
   1279   Writer.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten(), Record);
   1280 
   1281   // These are read/set from/to the first declaration.
   1282   if (D->getPreviousDecl() == nullptr) {
   1283     Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
   1284     Record.push_back(D->isMemberSpecialization());
   1285   }
   1286 
   1287   Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
   1288 }
   1289 
   1290 void ASTDeclWriter::VisitClassScopeFunctionSpecializationDecl(
   1291                                     ClassScopeFunctionSpecializationDecl *D) {
   1292   VisitDecl(D);
   1293   Writer.AddDeclRef(D->getSpecialization(), Record);
   1294   Code = serialization::DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION;
   1295 }
   1296 
   1297 
   1298 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
   1299   VisitRedeclarableTemplateDecl(D);
   1300 
   1301   if (D->isFirstDecl()) {
   1302     // This FunctionTemplateDecl owns the CommonPtr; write it.
   1303 
   1304     // Write the function specialization declarations.
   1305     Record.push_back(D->getSpecializations().size());
   1306     for (llvm::FoldingSetVector<FunctionTemplateSpecializationInfo>::iterator
   1307            I = D->getSpecializations().begin(),
   1308            E = D->getSpecializations().end()   ; I != E; ++I) {
   1309       assert(I->Function->isCanonicalDecl() &&
   1310              "Expected only canonical decls in set");
   1311       Writer.AddDeclRef(I->Function, Record);
   1312     }
   1313   }
   1314   Code = serialization::DECL_FUNCTION_TEMPLATE;
   1315 }
   1316 
   1317 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
   1318   VisitTypeDecl(D);
   1319 
   1320   Record.push_back(D->wasDeclaredWithTypename());
   1321   Record.push_back(D->defaultArgumentWasInherited());
   1322   Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
   1323 
   1324   Code = serialization::DECL_TEMPLATE_TYPE_PARM;
   1325 }
   1326 
   1327 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
   1328   // For an expanded parameter pack, record the number of expansion types here
   1329   // so that it's easier for deserialization to allocate the right amount of
   1330   // memory.
   1331   if (D->isExpandedParameterPack())
   1332     Record.push_back(D->getNumExpansionTypes());
   1333 
   1334   VisitDeclaratorDecl(D);
   1335   // TemplateParmPosition.
   1336   Record.push_back(D->getDepth());
   1337   Record.push_back(D->getPosition());
   1338 
   1339   if (D->isExpandedParameterPack()) {
   1340     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
   1341       Writer.AddTypeRef(D->getExpansionType(I), Record);
   1342       Writer.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I), Record);
   1343     }
   1344 
   1345     Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
   1346   } else {
   1347     // Rest of NonTypeTemplateParmDecl.
   1348     Record.push_back(D->isParameterPack());
   1349     Record.push_back(D->getDefaultArgument() != nullptr);
   1350     if (D->getDefaultArgument()) {
   1351       Writer.AddStmt(D->getDefaultArgument());
   1352       Record.push_back(D->defaultArgumentWasInherited());
   1353     }
   1354     Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
   1355   }
   1356 }
   1357 
   1358 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
   1359   // For an expanded parameter pack, record the number of expansion types here
   1360   // so that it's easier for deserialization to allocate the right amount of
   1361   // memory.
   1362   if (D->isExpandedParameterPack())
   1363     Record.push_back(D->getNumExpansionTemplateParameters());
   1364 
   1365   VisitTemplateDecl(D);
   1366   // TemplateParmPosition.
   1367   Record.push_back(D->getDepth());
   1368   Record.push_back(D->getPosition());
   1369 
   1370   if (D->isExpandedParameterPack()) {
   1371     for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
   1372          I != N; ++I)
   1373       Writer.AddTemplateParameterList(D->getExpansionTemplateParameters(I),
   1374                                       Record);
   1375     Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
   1376   } else {
   1377     // Rest of TemplateTemplateParmDecl.
   1378     Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record);
   1379     Record.push_back(D->defaultArgumentWasInherited());
   1380     Record.push_back(D->isParameterPack());
   1381     Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
   1382   }
   1383 }
   1384 
   1385 void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
   1386   VisitRedeclarableTemplateDecl(D);
   1387   Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
   1388 }
   1389 
   1390 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
   1391   VisitDecl(D);
   1392   Writer.AddStmt(D->getAssertExpr());
   1393   Record.push_back(D->isFailed());
   1394   Writer.AddStmt(D->getMessage());
   1395   Writer.AddSourceLocation(D->getRParenLoc(), Record);
   1396   Code = serialization::DECL_STATIC_ASSERT;
   1397 }
   1398 
   1399 /// \brief Emit the DeclContext part of a declaration context decl.
   1400 ///
   1401 /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
   1402 /// block for this declaration context is stored. May be 0 to indicate
   1403 /// that there are no declarations stored within this context.
   1404 ///
   1405 /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
   1406 /// block for this declaration context is stored. May be 0 to indicate
   1407 /// that there are no declarations visible from this context. Note
   1408 /// that this value will not be emitted for non-primary declaration
   1409 /// contexts.
   1410 void ASTDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
   1411                                      uint64_t VisibleOffset) {
   1412   Record.push_back(LexicalOffset);
   1413   Record.push_back(VisibleOffset);
   1414 }
   1415 
   1416 template <typename T>
   1417 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
   1418   T *First = D->getFirstDecl();
   1419   if (First->getMostRecentDecl() != First) {
   1420     assert(isRedeclarableDeclKind(static_cast<T *>(D)->getKind()) &&
   1421            "Not considered redeclarable?");
   1422 
   1423     // There is more than one declaration of this entity, so we will need to
   1424     // write a redeclaration chain.
   1425     Writer.AddDeclRef(First, Record);
   1426     Writer.Redeclarations.insert(First);
   1427 
   1428     // Make sure that we serialize both the previous and the most-recent
   1429     // declarations, which (transitively) ensures that all declarations in the
   1430     // chain get serialized.
   1431     (void)Writer.GetDeclRef(D->getPreviousDecl());
   1432     (void)Writer.GetDeclRef(First->getMostRecentDecl());
   1433   } else {
   1434     // We use the sentinel value 0 to indicate an only declaration.
   1435     Record.push_back(0);
   1436   }
   1437 
   1438 }
   1439 
   1440 void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
   1441   Record.push_back(D->varlist_size());
   1442   VisitDecl(D);
   1443   for (auto *I : D->varlists())
   1444     Writer.AddStmt(I);
   1445   Code = serialization::DECL_OMP_THREADPRIVATE;
   1446 }
   1447 
   1448 //===----------------------------------------------------------------------===//
   1449 // ASTWriter Implementation
   1450 //===----------------------------------------------------------------------===//
   1451 
   1452 void ASTWriter::WriteDeclsBlockAbbrevs() {
   1453   using namespace llvm;
   1454 
   1455   BitCodeAbbrev *Abv;
   1456 
   1457   // Abbreviation for DECL_FIELD
   1458   Abv = new BitCodeAbbrev();
   1459   Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
   1460   // Decl
   1461   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1462   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1463   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1464   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1465   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1466   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1467   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1468   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1469   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
   1470   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1471   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1472   // NamedDecl
   1473   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1474   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1475   // ValueDecl
   1476   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1477   // DeclaratorDecl
   1478   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
   1479   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
   1480   // FieldDecl
   1481   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
   1482   Abv->Add(BitCodeAbbrevOp(0));                       //getBitWidth
   1483   // Type Source Info
   1484   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1485   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1486   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
   1487   DeclFieldAbbrev = Stream.EmitAbbrev(Abv);
   1488 
   1489   // Abbreviation for DECL_OBJC_IVAR
   1490   Abv = new BitCodeAbbrev();
   1491   Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
   1492   // Decl
   1493   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1494   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1495   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1496   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1497   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1498   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1499   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1500   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1501   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
   1502   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1503   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1504   // NamedDecl
   1505   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1506   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1507   // ValueDecl
   1508   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1509   // DeclaratorDecl
   1510   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
   1511   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
   1512   // FieldDecl
   1513   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
   1514   Abv->Add(BitCodeAbbrevOp(0));                       //getBitWidth
   1515   // ObjC Ivar
   1516   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
   1517   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
   1518   // Type Source Info
   1519   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1520   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1521   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
   1522   DeclObjCIvarAbbrev = Stream.EmitAbbrev(Abv);
   1523 
   1524   // Abbreviation for DECL_ENUM
   1525   Abv = new BitCodeAbbrev();
   1526   Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
   1527   // Redeclarable
   1528   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
   1529   // Decl
   1530   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1531   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1532   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1533   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1534   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1535   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1536   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1537   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1538   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
   1539   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1540   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1541   // NamedDecl
   1542   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1543   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1544   // TypeDecl
   1545   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
   1546   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
   1547   // TagDecl
   1548   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
   1549   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
   1550   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
   1551   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
   1552   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
   1553   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
   1554   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
   1555   Abv->Add(BitCodeAbbrevOp(0));                         // hasExtInfo
   1556   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TypedefNameAnonDecl
   1557   // EnumDecl
   1558   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddTypeRef
   1559   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IntegerType
   1560   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getPromotionType
   1561   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumPositiveBits
   1562   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumNegativeBits
   1563   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScoped
   1564   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScopedUsingClassTag
   1565   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isFixed
   1566   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // InstantiatedMembEnum
   1567   // DC
   1568   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
   1569   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
   1570   DeclEnumAbbrev = Stream.EmitAbbrev(Abv);
   1571 
   1572   // Abbreviation for DECL_RECORD
   1573   Abv = new BitCodeAbbrev();
   1574   Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
   1575   // Redeclarable
   1576   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
   1577   // Decl
   1578   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1579   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1580   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1581   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1582   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1583   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1584   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1585   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1586   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
   1587   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1588   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1589   // NamedDecl
   1590   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1591   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1592   // TypeDecl
   1593   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
   1594   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
   1595   // TagDecl
   1596   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
   1597   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
   1598   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
   1599   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
   1600   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
   1601   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
   1602   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
   1603   Abv->Add(BitCodeAbbrevOp(0));                         // hasExtInfo
   1604   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TypedefNameAnonDecl
   1605   // RecordDecl
   1606   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FlexibleArrayMember
   1607   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // AnonymousStructUnion
   1608   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasObjectMember
   1609   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasVolatileMember
   1610   // DC
   1611   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
   1612   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
   1613   DeclRecordAbbrev = Stream.EmitAbbrev(Abv);
   1614 
   1615   // Abbreviation for DECL_PARM_VAR
   1616   Abv = new BitCodeAbbrev();
   1617   Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
   1618   // Redeclarable
   1619   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
   1620   // Decl
   1621   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1622   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1623   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1624   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1625   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1626   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1627   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1628   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1629   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
   1630   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1631   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1632   // NamedDecl
   1633   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1634   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1635   // ValueDecl
   1636   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1637   // DeclaratorDecl
   1638   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
   1639   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
   1640   // VarDecl
   1641   Abv->Add(BitCodeAbbrevOp(0));                       // StorageClass
   1642   Abv->Add(BitCodeAbbrevOp(0));                       // getTSCSpec
   1643   Abv->Add(BitCodeAbbrevOp(0));                       // hasCXXDirectInitializer
   1644   Abv->Add(BitCodeAbbrevOp(0));                       // isExceptionVariable
   1645   Abv->Add(BitCodeAbbrevOp(0));                       // isNRVOVariable
   1646   Abv->Add(BitCodeAbbrevOp(0));                       // isCXXForRangeDecl
   1647   Abv->Add(BitCodeAbbrevOp(0));                       // isARCPseudoStrong
   1648   Abv->Add(BitCodeAbbrevOp(0));                       // isConstexpr
   1649   Abv->Add(BitCodeAbbrevOp(0));                       // isInitCapture
   1650   Abv->Add(BitCodeAbbrevOp(0));                       // isPrevDeclInSameScope
   1651   Abv->Add(BitCodeAbbrevOp(0));                       // Linkage
   1652   Abv->Add(BitCodeAbbrevOp(0));                       // HasInit
   1653   Abv->Add(BitCodeAbbrevOp(0));                   // HasMemberSpecializationInfo
   1654   // ParmVarDecl
   1655   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsObjCMethodParameter
   1656   Abv->Add(BitCodeAbbrevOp(0));                       // ScopeDepth
   1657   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
   1658   Abv->Add(BitCodeAbbrevOp(0));                       // ObjCDeclQualifier
   1659   Abv->Add(BitCodeAbbrevOp(0));                       // KNRPromoted
   1660   Abv->Add(BitCodeAbbrevOp(0));                       // HasInheritedDefaultArg
   1661   Abv->Add(BitCodeAbbrevOp(0));                   // HasUninstantiatedDefaultArg
   1662   // Type Source Info
   1663   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1664   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1665   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
   1666   DeclParmVarAbbrev = Stream.EmitAbbrev(Abv);
   1667 
   1668   // Abbreviation for DECL_TYPEDEF
   1669   Abv = new BitCodeAbbrev();
   1670   Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
   1671   // Redeclarable
   1672   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
   1673   // Decl
   1674   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1675   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1676   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1677   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1678   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1679   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1680   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1681   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1682   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
   1683   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1684   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1685   // NamedDecl
   1686   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1687   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1688   // TypeDecl
   1689   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
   1690   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
   1691   // TypedefDecl
   1692   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1693   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
   1694   DeclTypedefAbbrev = Stream.EmitAbbrev(Abv);
   1695 
   1696   // Abbreviation for DECL_VAR
   1697   Abv = new BitCodeAbbrev();
   1698   Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
   1699   // Redeclarable
   1700   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
   1701   // Decl
   1702   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
   1703   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
   1704   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
   1705   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
   1706   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
   1707   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
   1708   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
   1709   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
   1710   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
   1711   Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
   1712   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
   1713   // NamedDecl
   1714   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
   1715   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
   1716   // ValueDecl
   1717   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1718   // DeclaratorDecl
   1719   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
   1720   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
   1721   // VarDecl
   1722   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // StorageClass
   1723   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // getTSCSpec
   1724   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // CXXDirectInitializer
   1725   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isExceptionVariable
   1726   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isNRVOVariable
   1727   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCXXForRangeDecl
   1728   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
   1729   Abv->Add(BitCodeAbbrevOp(0));                         // isConstexpr
   1730   Abv->Add(BitCodeAbbrevOp(0));                         // isInitCapture
   1731   Abv->Add(BitCodeAbbrevOp(0));                         // isPrevDeclInSameScope
   1732   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
   1733   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasInit
   1734   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasMemberSpecInfo
   1735   // Type Source Info
   1736   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
   1737   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
   1738   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
   1739   DeclVarAbbrev = Stream.EmitAbbrev(Abv);
   1740 
   1741   // Abbreviation for EXPR_DECL_REF
   1742   Abv = new BitCodeAbbrev();
   1743   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
   1744   //Stmt
   1745   //Expr
   1746   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1747   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
   1748   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
   1749   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
   1750   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
   1751   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
   1752   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
   1753   //DeclRefExpr
   1754   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HasQualifier
   1755   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //GetDeclFound
   1756   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ExplicitTemplateArgs
   1757   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HadMultipleCandidates
   1758   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //RefersToEnclosingLocal
   1759   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
   1760   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
   1761   DeclRefExprAbbrev = Stream.EmitAbbrev(Abv);
   1762 
   1763   // Abbreviation for EXPR_INTEGER_LITERAL
   1764   Abv = new BitCodeAbbrev();
   1765   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
   1766   //Stmt
   1767   //Expr
   1768   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1769   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
   1770   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
   1771   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
   1772   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
   1773   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
   1774   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
   1775   //Integer Literal
   1776   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
   1777   Abv->Add(BitCodeAbbrevOp(32));                      // Bit Width
   1778   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
   1779   IntegerLiteralAbbrev = Stream.EmitAbbrev(Abv);
   1780 
   1781   // Abbreviation for EXPR_CHARACTER_LITERAL
   1782   Abv = new BitCodeAbbrev();
   1783   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
   1784   //Stmt
   1785   //Expr
   1786   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
   1787   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
   1788   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
   1789   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
   1790   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
   1791   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
   1792   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
   1793   //Character Literal
   1794   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
   1795   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
   1796   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // getKind
   1797   CharacterLiteralAbbrev = Stream.EmitAbbrev(Abv);
   1798 
   1799   Abv = new BitCodeAbbrev();
   1800   Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
   1801   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
   1802   DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv);
   1803 
   1804   Abv = new BitCodeAbbrev();
   1805   Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
   1806   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
   1807   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
   1808   DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(Abv);
   1809 }
   1810 
   1811 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
   1812 /// consumers of the AST.
   1813 ///
   1814 /// Such decls will always be deserialized from the AST file, so we would like
   1815 /// this to be as restrictive as possible. Currently the predicate is driven by
   1816 /// code generation requirements, if other clients have a different notion of
   1817 /// what is "required" then we may have to consider an alternate scheme where
   1818 /// clients can iterate over the top-level decls and get information on them,
   1819 /// without necessary deserializing them. We could explicitly require such
   1820 /// clients to use a separate API call to "realize" the decl. This should be
   1821 /// relatively painless since they would presumably only do it for top-level
   1822 /// decls.
   1823 static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
   1824   // An ObjCMethodDecl is never considered as "required" because its
   1825   // implementation container always is.
   1826 
   1827   // File scoped assembly or obj-c implementation must be seen. ImportDecl is
   1828   // used by codegen to determine the set of imported modules to search for
   1829   // inputs for automatic linking.
   1830   if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplDecl>(D) || isa<ImportDecl>(D))
   1831     return true;
   1832 
   1833   return Context.DeclMustBeEmitted(D);
   1834 }
   1835 
   1836 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
   1837   // Switch case IDs are per Decl.
   1838   ClearSwitchCaseIDs();
   1839 
   1840   RecordData Record;
   1841   ASTDeclWriter W(*this, Context, Record);
   1842 
   1843   // Determine the ID for this declaration.
   1844   serialization::DeclID ID;
   1845   if (D->isFromASTFile())
   1846     ID = getDeclID(D);
   1847   else {
   1848     serialization::DeclID &IDR = DeclIDs[D];
   1849     if (IDR == 0)
   1850       IDR = NextDeclID++;
   1851 
   1852     ID= IDR;
   1853   }
   1854 
   1855   bool isReplacingADecl = ID < FirstDeclID;
   1856 
   1857   // If this declaration is also a DeclContext, write blocks for the
   1858   // declarations that lexically stored inside its context and those
   1859   // declarations that are visible from its context. These blocks
   1860   // are written before the declaration itself so that we can put
   1861   // their offsets into the record for the declaration.
   1862   uint64_t LexicalOffset = 0;
   1863   uint64_t VisibleOffset = 0;
   1864   DeclContext *DC = dyn_cast<DeclContext>(D);
   1865   if (DC) {
   1866     if (isReplacingADecl) {
   1867       // It is replacing a decl from a chained PCH; make sure that the
   1868       // DeclContext is fully loaded.
   1869       if (DC->hasExternalLexicalStorage())
   1870         DC->LoadLexicalDeclsFromExternalStorage();
   1871       if (DC->hasExternalVisibleStorage())
   1872         Chain->completeVisibleDeclsMap(DC);
   1873     }
   1874     LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
   1875     VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
   1876   }
   1877 
   1878   if (isReplacingADecl) {
   1879     // We're replacing a decl in a previous file.
   1880     ReplacedDecls.push_back(ReplacedDeclInfo(ID, Stream.GetCurrentBitNo(),
   1881                                              D->getLocation()));
   1882   } else {
   1883     unsigned Index = ID - FirstDeclID;
   1884 
   1885     // Record the offset for this declaration
   1886     SourceLocation Loc = D->getLocation();
   1887     if (DeclOffsets.size() == Index)
   1888       DeclOffsets.push_back(DeclOffset(Loc, Stream.GetCurrentBitNo()));
   1889     else if (DeclOffsets.size() < Index) {
   1890       DeclOffsets.resize(Index+1);
   1891       DeclOffsets[Index].setLocation(Loc);
   1892       DeclOffsets[Index].BitOffset = Stream.GetCurrentBitNo();
   1893     }
   1894 
   1895     SourceManager &SM = Context.getSourceManager();
   1896     if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
   1897       associateDeclWithFile(D, ID);
   1898   }
   1899 
   1900   // Build and emit a record for this declaration
   1901   Record.clear();
   1902   W.Code = (serialization::DeclCode)0;
   1903   W.AbbrevToUse = 0;
   1904   W.Visit(D);
   1905   if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
   1906 
   1907   if (!W.Code)
   1908     llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
   1909                             D->getDeclKindName() + "'");
   1910   Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
   1911 
   1912   // Flush any expressions that were written as part of this declaration.
   1913   FlushStmts();
   1914 
   1915   // Flush C++ base specifiers, if there are any.
   1916   FlushCXXBaseSpecifiers();
   1917 
   1918   // Note declarations that should be deserialized eagerly so that we can add
   1919   // them to a record in the AST file later.
   1920   if (isRequiredDecl(D, Context))
   1921     EagerlyDeserializedDecls.push_back(ID);
   1922 }
   1923 
   1924 void ASTWriter::AddFunctionDefinition(const FunctionDecl *FD,
   1925                                       RecordData &Record) {
   1926   ClearSwitchCaseIDs();
   1927 
   1928   ASTDeclWriter W(*this, FD->getASTContext(), Record);
   1929   W.AddFunctionDefinition(FD);
   1930 }
   1931