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