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