brintos

brintos / llvm-project-archived public Read only

0
0
Text · 121.3 KiB · c9f8797 Raw
3109 lines · cpp
1//===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9//  This file implements serialization for Declarations.10//11//===----------------------------------------------------------------------===//12 13#include "ASTCommon.h"14#include "clang/AST/Attr.h"15#include "clang/AST/DeclCXX.h"16#include "clang/AST/DeclTemplate.h"17#include "clang/AST/DeclVisitor.h"18#include "clang/AST/Expr.h"19#include "clang/AST/OpenMPClause.h"20#include "clang/AST/PrettyDeclStackTrace.h"21#include "clang/Basic/SourceManager.h"22#include "clang/Serialization/ASTReader.h"23#include "clang/Serialization/ASTRecordWriter.h"24#include "llvm/Bitstream/BitstreamWriter.h"25#include "llvm/Support/ErrorHandling.h"26using namespace clang;27using namespace serialization;28 29//===----------------------------------------------------------------------===//30// Utility functions31//===----------------------------------------------------------------------===//32 33namespace {34 35// Helper function that returns true if the decl passed in the argument is36// a defintion in dependent contxt.37template <typename DT> bool isDefinitionInDependentContext(DT *D) {38  return D->isDependentContext() && D->isThisDeclarationADefinition();39}40 41} // namespace42 43//===----------------------------------------------------------------------===//44// Declaration serialization45//===----------------------------------------------------------------------===//46 47namespace clang {48  class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {49    ASTWriter &Writer;50    ASTRecordWriter Record;51 52    serialization::DeclCode Code;53    unsigned AbbrevToUse;54 55    bool GeneratingReducedBMI = false;56 57  public:58    ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,59                  ASTWriter::RecordDataImpl &Record, bool GeneratingReducedBMI)60        : Writer(Writer), Record(Context, Writer, Record),61          Code((serialization::DeclCode)0), AbbrevToUse(0),62          GeneratingReducedBMI(GeneratingReducedBMI) {}63 64    uint64_t Emit(Decl *D) {65      if (!Code)66        llvm::report_fatal_error(StringRef("unexpected declaration kind '") +67            D->getDeclKindName() + "'");68      return Record.Emit(Code, AbbrevToUse);69    }70 71    void Visit(Decl *D);72 73    void VisitDecl(Decl *D);74    void VisitPragmaCommentDecl(PragmaCommentDecl *D);75    void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);76    void VisitTranslationUnitDecl(TranslationUnitDecl *D);77    void VisitNamedDecl(NamedDecl *D);78    void VisitLabelDecl(LabelDecl *LD);79    void VisitNamespaceDecl(NamespaceDecl *D);80    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);81    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);82    void VisitTypeDecl(TypeDecl *D);83    void VisitTypedefNameDecl(TypedefNameDecl *D);84    void VisitTypedefDecl(TypedefDecl *D);85    void VisitTypeAliasDecl(TypeAliasDecl *D);86    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);87    void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);88    void VisitTagDecl(TagDecl *D);89    void VisitEnumDecl(EnumDecl *D);90    void VisitRecordDecl(RecordDecl *D);91    void VisitCXXRecordDecl(CXXRecordDecl *D);92    void VisitClassTemplateSpecializationDecl(93                                            ClassTemplateSpecializationDecl *D);94    void VisitClassTemplatePartialSpecializationDecl(95                                     ClassTemplatePartialSpecializationDecl *D);96    void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);97    void VisitVarTemplatePartialSpecializationDecl(98        VarTemplatePartialSpecializationDecl *D);99    void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);100    void VisitValueDecl(ValueDecl *D);101    void VisitEnumConstantDecl(EnumConstantDecl *D);102    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);103    void VisitDeclaratorDecl(DeclaratorDecl *D);104    void VisitFunctionDecl(FunctionDecl *D);105    void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);106    void VisitCXXMethodDecl(CXXMethodDecl *D);107    void VisitCXXConstructorDecl(CXXConstructorDecl *D);108    void VisitCXXDestructorDecl(CXXDestructorDecl *D);109    void VisitCXXConversionDecl(CXXConversionDecl *D);110    void VisitFieldDecl(FieldDecl *D);111    void VisitMSPropertyDecl(MSPropertyDecl *D);112    void VisitMSGuidDecl(MSGuidDecl *D);113    void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);114    void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);115    void VisitIndirectFieldDecl(IndirectFieldDecl *D);116    void VisitVarDecl(VarDecl *D);117    void VisitImplicitParamDecl(ImplicitParamDecl *D);118    void VisitParmVarDecl(ParmVarDecl *D);119    void VisitDecompositionDecl(DecompositionDecl *D);120    void VisitBindingDecl(BindingDecl *D);121    void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);122    void VisitTemplateDecl(TemplateDecl *D);123    void VisitConceptDecl(ConceptDecl *D);124    void VisitImplicitConceptSpecializationDecl(125        ImplicitConceptSpecializationDecl *D);126    void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);127    void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);128    void VisitClassTemplateDecl(ClassTemplateDecl *D);129    void VisitVarTemplateDecl(VarTemplateDecl *D);130    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);131    void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);132    void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);133    void VisitUsingDecl(UsingDecl *D);134    void VisitUsingEnumDecl(UsingEnumDecl *D);135    void VisitUsingPackDecl(UsingPackDecl *D);136    void VisitUsingShadowDecl(UsingShadowDecl *D);137    void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);138    void VisitLinkageSpecDecl(LinkageSpecDecl *D);139    void VisitExportDecl(ExportDecl *D);140    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);141    void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);142    void VisitImportDecl(ImportDecl *D);143    void VisitAccessSpecDecl(AccessSpecDecl *D);144    void VisitFriendDecl(FriendDecl *D);145    void VisitFriendTemplateDecl(FriendTemplateDecl *D);146    void VisitStaticAssertDecl(StaticAssertDecl *D);147    void VisitBlockDecl(BlockDecl *D);148    void VisitOutlinedFunctionDecl(OutlinedFunctionDecl *D);149    void VisitCapturedDecl(CapturedDecl *D);150    void VisitEmptyDecl(EmptyDecl *D);151    void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);152    void VisitDeclContext(DeclContext *DC);153    template <typename T> void VisitRedeclarable(Redeclarable<T> *D);154    void VisitHLSLBufferDecl(HLSLBufferDecl *D);155 156    // FIXME: Put in the same order is DeclNodes.td?157    void VisitObjCMethodDecl(ObjCMethodDecl *D);158    void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);159    void VisitObjCContainerDecl(ObjCContainerDecl *D);160    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);161    void VisitObjCIvarDecl(ObjCIvarDecl *D);162    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);163    void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);164    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);165    void VisitObjCImplDecl(ObjCImplDecl *D);166    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);167    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);168    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);169    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);170    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);171    void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);172    void VisitOMPAllocateDecl(OMPAllocateDecl *D);173    void VisitOMPRequiresDecl(OMPRequiresDecl *D);174    void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);175    void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);176    void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);177 178    void VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D);179    void VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D);180 181    /// Add an Objective-C type parameter list to the given record.182    void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {183      // Empty type parameter list.184      if (!typeParams) {185        Record.push_back(0);186        return;187      }188 189      Record.push_back(typeParams->size());190      for (auto *typeParam : *typeParams) {191        Record.AddDeclRef(typeParam);192      }193      Record.AddSourceLocation(typeParams->getLAngleLoc());194      Record.AddSourceLocation(typeParams->getRAngleLoc());195    }196 197    /// Collect the first declaration from each module file that provides a198    /// declaration of D.199    void CollectFirstDeclFromEachModule(200        const Decl *D, bool IncludeLocal,201        llvm::MapVector<ModuleFile *, const Decl *> &Firsts) {202 203      // FIXME: We can skip entries that we know are implied by others.204      for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {205        if (R->isFromASTFile())206          Firsts[Writer.Chain->getOwningModuleFile(R)] = R;207        else if (IncludeLocal)208          Firsts[nullptr] = R;209      }210    }211 212    /// Add to the record the first declaration from each module file that213    /// provides a declaration of D. The intent is to provide a sufficient214    /// set such that reloading this set will load all current redeclarations.215    void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {216      llvm::MapVector<ModuleFile *, const Decl *> Firsts;217      CollectFirstDeclFromEachModule(D, IncludeLocal, Firsts);218 219      for (const auto &F : Firsts)220        Record.AddDeclRef(F.second);221    }222 223    template <typename T> bool shouldSkipWritingSpecializations(T *Spec) {224      // Now we will only avoid writing specializations if we're generating225      // reduced BMI.226      if (!GeneratingReducedBMI)227        return false;228 229      assert((isa<FunctionDecl, ClassTemplateSpecializationDecl,230                  VarTemplateSpecializationDecl>(Spec)));231 232      ArrayRef<TemplateArgument> Args;233      if (auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(Spec))234        Args = CTSD->getTemplateArgs().asArray();235      else if (auto *VTSD = dyn_cast<VarTemplateSpecializationDecl>(Spec))236        Args = VTSD->getTemplateArgs().asArray();237      else238        Args = cast<FunctionDecl>(Spec)239                   ->getTemplateSpecializationArgs()240                   ->asArray();241 242      // If there is any template argument is TULocal, we can avoid writing the243      // specialization since the consumers of reduced BMI won't get the244      // specialization anyway.245      for (const TemplateArgument &TA : Args) {246        switch (TA.getKind()) {247        case TemplateArgument::Type: {248          Linkage L = TA.getAsType()->getLinkage();249          if (!isExternallyVisible(L))250            return true;251          break;252        }253        case TemplateArgument::Declaration:254          if (!TA.getAsDecl()->isExternallyVisible())255            return true;256          break;257        default:258          break;259        }260      }261 262      return false;263    }264 265    /// Add to the record the first template specialization from each module266    /// file that provides a declaration of D. We store the DeclId and an267    /// ODRHash of the template arguments of D which should provide enough268    /// information to load D only if the template instantiator needs it.269    void AddFirstSpecializationDeclFromEachModule(270        const Decl *D, llvm::SmallVectorImpl<const Decl *> &SpecsInMap,271        llvm::SmallVectorImpl<const Decl *> &PartialSpecsInMap) {272      assert((isa<ClassTemplateSpecializationDecl>(D) ||273              isa<VarTemplateSpecializationDecl>(D) || isa<FunctionDecl>(D)) &&274             "Must not be called with other decls");275      llvm::MapVector<ModuleFile *, const Decl *> Firsts;276      CollectFirstDeclFromEachModule(D, /*IncludeLocal*/ true, Firsts);277 278      for (const auto &F : Firsts) {279        if (shouldSkipWritingSpecializations(F.second))280          continue;281 282        if (isa<ClassTemplatePartialSpecializationDecl,283                VarTemplatePartialSpecializationDecl>(F.second))284          PartialSpecsInMap.push_back(F.second);285        else286          SpecsInMap.push_back(F.second);287      }288    }289 290    /// Get the specialization decl from an entry in the specialization list.291    template <typename EntryType>292    typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *293    getSpecializationDecl(EntryType &T) {294      return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);295    }296 297    /// Get the list of partial specializations from a template's common ptr.298    template<typename T>299    decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {300      return Common->PartialSpecializations;301    }302    MutableArrayRef<FunctionTemplateSpecializationInfo>303    getPartialSpecializations(FunctionTemplateDecl::Common *) {304      return {};305    }306 307    template<typename DeclTy>308    void AddTemplateSpecializations(DeclTy *D) {309      auto *Common = D->getCommonPtr();310 311      // If we have any lazy specializations, and the external AST source is312      // our chained AST reader, we can just write out the DeclIDs. Otherwise,313      // we need to resolve them to actual declarations.314      if (Writer.Chain != Record.getASTContext().getExternalSource() &&315          Writer.Chain && Writer.Chain->haveUnloadedSpecializations(D)) {316        D->LoadLazySpecializations();317        assert(!Writer.Chain->haveUnloadedSpecializations(D));318      }319 320      // AddFirstSpecializationDeclFromEachModule might trigger deserialization,321      // invalidating *Specializations iterators.322      llvm::SmallVector<const Decl *, 16> AllSpecs;323      for (auto &Entry : Common->Specializations)324        AllSpecs.push_back(getSpecializationDecl(Entry));325      for (auto &Entry : getPartialSpecializations(Common))326        AllSpecs.push_back(getSpecializationDecl(Entry));327 328      llvm::SmallVector<const Decl *, 16> Specs;329      llvm::SmallVector<const Decl *, 16> PartialSpecs;330      for (auto *D : AllSpecs) {331        assert(D->isCanonicalDecl() && "non-canonical decl in set");332        AddFirstSpecializationDeclFromEachModule(D, Specs, PartialSpecs);333      }334 335      Record.AddOffset(Writer.WriteSpecializationInfoLookupTable(336          D, Specs, /*IsPartial=*/false));337 338      // Function Template Decl doesn't have partial decls.339      if (isa<FunctionTemplateDecl>(D)) {340        assert(PartialSpecs.empty());341        return;342      }343 344      Record.AddOffset(Writer.WriteSpecializationInfoLookupTable(345          D, PartialSpecs, /*IsPartial=*/true));346    }347 348    /// Ensure that this template specialization is associated with the specified349    /// template on reload.350    void RegisterTemplateSpecialization(const Decl *Template,351                                        const Decl *Specialization) {352      Template = Template->getCanonicalDecl();353 354      // If the canonical template is local, we'll write out this specialization355      // when we emit it.356      // FIXME: We can do the same thing if there is any local declaration of357      // the template, to avoid emitting an update record.358      if (!Template->isFromASTFile())359        return;360 361      // We only need to associate the first local declaration of the362      // specialization. The other declarations will get pulled in by it.363      if (Writer.getFirstLocalDecl(Specialization) != Specialization)364        return;365 366      if (isa<ClassTemplatePartialSpecializationDecl,367              VarTemplatePartialSpecializationDecl>(Specialization))368        Writer.PartialSpecializationsUpdates[cast<NamedDecl>(Template)]369            .push_back(cast<NamedDecl>(Specialization));370      else371        Writer.SpecializationsUpdates[cast<NamedDecl>(Template)].push_back(372            cast<NamedDecl>(Specialization));373    }374  };375}376 377// When building a C++20 module interface unit or a partition unit, a378// strong definition in the module interface is provided by the379// compilation of that unit, not by its users. (Inline variables are still380// emitted in module users.)381static bool shouldVarGenerateHereOnly(const VarDecl *VD) {382  if (VD->getStorageDuration() != SD_Static)383    return false;384 385  if (VD->getDescribedVarTemplate())386    return false;387 388  Module *M = VD->getOwningModule();389  if (!M)390    return false;391 392  M = M->getTopLevelModule();393  ASTContext &Ctx = VD->getASTContext();394  if (!M->isInterfaceOrPartition() &&395      (!VD->hasAttr<DLLExportAttr>() ||396       !Ctx.getLangOpts().BuildingPCHWithObjectFile))397    return false;398 399  return Ctx.GetGVALinkageForVariable(VD) >= GVA_StrongExternal;400}401 402static bool shouldFunctionGenerateHereOnly(const FunctionDecl *FD) {403  if (FD->isDependentContext())404    return false;405 406  ASTContext &Ctx = FD->getASTContext();407  auto Linkage = Ctx.GetGVALinkageForFunction(FD);408  if (Ctx.getLangOpts().ModulesCodegen ||409      (FD->hasAttr<DLLExportAttr>() &&410       Ctx.getLangOpts().BuildingPCHWithObjectFile))411    // Under -fmodules-codegen, codegen is performed for all non-internal,412    // non-always_inline functions, unless they are available elsewhere.413    if (!FD->hasAttr<AlwaysInlineAttr>() && Linkage != GVA_Internal &&414        Linkage != GVA_AvailableExternally)415      return true;416 417  Module *M = FD->getOwningModule();418  if (!M)419    return false;420 421  M = M->getTopLevelModule();422  if (M->isInterfaceOrPartition())423    if (Linkage >= GVA_StrongExternal)424      return true;425 426  return false;427}428 429bool clang::CanElideDeclDef(const Decl *D) {430  if (auto *FD = dyn_cast<FunctionDecl>(D)) {431    if (FD->isInlined() || FD->isConstexpr() || FD->isConsteval())432      return false;433 434    // If the function should be generated somewhere else, we shouldn't elide435    // it.436    if (!shouldFunctionGenerateHereOnly(FD))437      return false;438  }439 440  if (auto *VD = dyn_cast<VarDecl>(D)) {441    if (VD->getDeclContext()->isDependentContext())442      return false;443 444    // Constant initialized variable may not affect the ABI, but they445    // may be used in constant evaluation in the frontend, so we have446    // to remain them.447    if (VD->hasConstantInitialization() || VD->isConstexpr())448      return false;449 450    // If the variable should be generated somewhere else, we shouldn't elide451    // it.452    if (!shouldVarGenerateHereOnly(VD))453      return false;454  }455 456  return true;457}458 459void ASTDeclWriter::Visit(Decl *D) {460  DeclVisitor<ASTDeclWriter>::Visit(D);461 462  // Source locations require array (variable-length) abbreviations.  The463  // abbreviation infrastructure requires that arrays are encoded last, so464  // we handle it here in the case of those classes derived from DeclaratorDecl465  if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {466    if (auto *TInfo = DD->getTypeSourceInfo())467      Record.AddTypeLoc(TInfo->getTypeLoc());468  }469 470  // Handle FunctionDecl's body here and write it after all other Stmts/Exprs471  // have been written. We want it last because we will not read it back when472  // retrieving it from the AST, we'll just lazily set the offset.473  if (auto *FD = dyn_cast<FunctionDecl>(D)) {474    if (!GeneratingReducedBMI || !CanElideDeclDef(FD)) {475      Record.push_back(FD->doesThisDeclarationHaveABody());476      if (FD->doesThisDeclarationHaveABody())477        Record.AddFunctionDefinition(FD);478    } else479      Record.push_back(0);480  }481 482  // Similar to FunctionDecls, handle VarDecl's initializer here and write it483  // after all other Stmts/Exprs. We will not read the initializer until after484  // we have finished recursive deserialization, because it can recursively485  // refer back to the variable.486  if (auto *VD = dyn_cast<VarDecl>(D)) {487    if (!GeneratingReducedBMI || !CanElideDeclDef(VD))488      Record.AddVarDeclInit(VD);489    else490      Record.push_back(0);491  }492 493  // And similarly for FieldDecls. We already serialized whether there is a494  // default member initializer.495  if (auto *FD = dyn_cast<FieldDecl>(D)) {496    if (FD->hasInClassInitializer()) {497      if (Expr *Init = FD->getInClassInitializer()) {498        Record.push_back(1);499        Record.AddStmt(Init);500      } else {501        Record.push_back(0);502        // Initializer has not been instantiated yet.503      }504    }505  }506 507  // If this declaration is also a DeclContext, write blocks for the508  // declarations that lexically stored inside its context and those509  // declarations that are visible from its context.510  if (auto *DC = dyn_cast<DeclContext>(D))511    VisitDeclContext(DC);512}513 514void ASTDeclWriter::VisitDecl(Decl *D) {515  BitsPacker DeclBits;516 517  // The order matters here. It will be better to put the bit with higher518  // probability to be 0 in the end of the bits.519  //520  // Since we're using VBR6 format to store it.521  // It will be pretty effient if all the higher bits are 0.522  // For example, if we need to pack 8 bits into a value and the stored value523  // is 0xf0, the actual stored value will be 0b000111'110000, which takes 12524  // bits actually. However, if we changed the order to be 0x0f, then we can525  // store it as 0b001111, which takes 6 bits only now.526  DeclBits.addBits((uint64_t)D->getModuleOwnershipKind(), /*BitWidth=*/3);527  DeclBits.addBit(D->isThisDeclarationReferenced());528  DeclBits.addBit(D->isUsed(false));529  DeclBits.addBits(D->getAccess(), /*BitWidth=*/2);530  DeclBits.addBit(D->isImplicit());531  DeclBits.addBit(D->getDeclContext() != D->getLexicalDeclContext());532  DeclBits.addBit(D->hasAttrs());533  DeclBits.addBit(D->isTopLevelDeclInObjCContainer());534  DeclBits.addBit(D->isInvalidDecl());535  Record.push_back(DeclBits);536 537  Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));538  if (D->getDeclContext() != D->getLexicalDeclContext())539    Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));540 541  if (D->hasAttrs())542    Record.AddAttributes(D->getAttrs());543 544  Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));545 546  // If this declaration injected a name into a context different from its547  // lexical context, and that context is an imported namespace, we need to548  // update its visible declarations to include this name.549  //550  // This happens when we instantiate a class with a friend declaration or a551  // function with a local extern declaration, for instance.552  //553  // FIXME: Can we handle this in AddedVisibleDecl instead?554  if (D->isOutOfLine()) {555    auto *DC = D->getDeclContext();556    while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {557      if (!NS->isFromASTFile())558        break;559      Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());560      if (!NS->isInlineNamespace())561        break;562      DC = NS->getParent();563    }564  }565}566 567void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {568  StringRef Arg = D->getArg();569  Record.push_back(Arg.size());570  VisitDecl(D);571  Record.AddSourceLocation(D->getBeginLoc());572  Record.push_back(D->getCommentKind());573  Record.AddString(Arg);574  Code = serialization::DECL_PRAGMA_COMMENT;575}576 577void ASTDeclWriter::VisitPragmaDetectMismatchDecl(578    PragmaDetectMismatchDecl *D) {579  StringRef Name = D->getName();580  StringRef Value = D->getValue();581  Record.push_back(Name.size() + 1 + Value.size());582  VisitDecl(D);583  Record.AddSourceLocation(D->getBeginLoc());584  Record.AddString(Name);585  Record.AddString(Value);586  Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;587}588 589void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {590  llvm_unreachable("Translation units aren't directly serialized");591}592 593void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {594  VisitDecl(D);595  Record.AddDeclarationName(D->getDeclName());596  Record.push_back(needsAnonymousDeclarationNumber(D)597                       ? Writer.getAnonymousDeclarationNumber(D)598                       : 0);599}600 601void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {602  VisitNamedDecl(D);603  Record.AddSourceLocation(D->getBeginLoc());604  if (!isa<TagDecl, TypedefDecl, TypeAliasDecl>(D))605    Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));606}607 608void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {609  VisitRedeclarable(D);610  VisitTypeDecl(D);611  Record.AddTypeSourceInfo(D->getTypeSourceInfo());612  Record.push_back(D->isModed());613  if (D->isModed())614    Record.AddTypeRef(D->getUnderlyingType());615  Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));616}617 618void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {619  VisitTypedefNameDecl(D);620  if (D->getDeclContext() == D->getLexicalDeclContext() &&621      !D->hasAttrs() &&622      !D->isImplicit() &&623      D->getFirstDecl() == D->getMostRecentDecl() &&624      !D->isInvalidDecl() &&625      !D->isTopLevelDeclInObjCContainer() &&626      !D->isModulePrivate() &&627      !needsAnonymousDeclarationNumber(D) &&628      D->getDeclName().getNameKind() == DeclarationName::Identifier)629    AbbrevToUse = Writer.getDeclTypedefAbbrev();630 631  Code = serialization::DECL_TYPEDEF;632}633 634void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {635  VisitTypedefNameDecl(D);636  Record.AddDeclRef(D->getDescribedAliasTemplate());637  Code = serialization::DECL_TYPEALIAS;638}639 640void ASTDeclWriter::VisitTagDecl(TagDecl *D) {641  static_assert(DeclContext::NumTagDeclBits == 23,642                "You need to update the serializer after you change the "643                "TagDeclBits");644 645  VisitRedeclarable(D);646  VisitTypeDecl(D);647  Record.push_back(D->getIdentifierNamespace());648 649  BitsPacker TagDeclBits;650  TagDeclBits.addBits(llvm::to_underlying(D->getTagKind()), /*BitWidth=*/3);651  TagDeclBits.addBit(!isa<CXXRecordDecl>(D) ? D->isCompleteDefinition() : 0);652  TagDeclBits.addBit(D->isEmbeddedInDeclarator());653  TagDeclBits.addBit(D->isFreeStanding());654  TagDeclBits.addBit(D->isCompleteDefinitionRequired());655  TagDeclBits.addBits(656      D->hasExtInfo() ? 1 : (D->getTypedefNameForAnonDecl() ? 2 : 0),657      /*BitWidth=*/2);658  Record.push_back(TagDeclBits);659 660  Record.AddSourceRange(D->getBraceRange());661 662  if (D->hasExtInfo()) {663    Record.AddQualifierInfo(*D->getExtInfo());664  } else if (auto *TD = D->getTypedefNameForAnonDecl()) {665    Record.AddDeclRef(TD);666    Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());667  }668}669 670void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {671  static_assert(DeclContext::NumEnumDeclBits == 43,672                "You need to update the serializer after you change the "673                "EnumDeclBits");674 675  VisitTagDecl(D);676  Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());677  if (!D->getIntegerTypeSourceInfo())678    Record.AddTypeRef(D->getIntegerType());679  Record.AddTypeRef(D->getPromotionType());680 681  BitsPacker EnumDeclBits;682  EnumDeclBits.addBits(D->getNumPositiveBits(), /*BitWidth=*/8);683  EnumDeclBits.addBits(D->getNumNegativeBits(), /*BitWidth=*/8);684  EnumDeclBits.addBit(D->isScoped());685  EnumDeclBits.addBit(D->isScopedUsingClassTag());686  EnumDeclBits.addBit(D->isFixed());687  Record.push_back(EnumDeclBits);688 689  Record.push_back(D->getODRHash());690 691  if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {692    Record.AddDeclRef(MemberInfo->getInstantiatedFrom());693    Record.push_back(MemberInfo->getTemplateSpecializationKind());694    Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());695  } else {696    Record.AddDeclRef(nullptr);697  }698 699  if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&700      !D->isInvalidDecl() && !D->isImplicit() && !D->hasExtInfo() &&701      !D->getTypedefNameForAnonDecl() &&702      D->getFirstDecl() == D->getMostRecentDecl() &&703      !D->isTopLevelDeclInObjCContainer() &&704      !CXXRecordDecl::classofKind(D->getKind()) &&705      !D->getIntegerTypeSourceInfo() && !D->getMemberSpecializationInfo() &&706      !needsAnonymousDeclarationNumber(D) &&707      D->getDeclName().getNameKind() == DeclarationName::Identifier)708    AbbrevToUse = Writer.getDeclEnumAbbrev();709 710  Code = serialization::DECL_ENUM;711}712 713void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {714  static_assert(DeclContext::NumRecordDeclBits == 64,715                "You need to update the serializer after you change the "716                "RecordDeclBits");717 718  VisitTagDecl(D);719 720  BitsPacker RecordDeclBits;721  RecordDeclBits.addBit(D->hasFlexibleArrayMember());722  RecordDeclBits.addBit(D->isAnonymousStructOrUnion());723  RecordDeclBits.addBit(D->hasObjectMember());724  RecordDeclBits.addBit(D->hasVolatileMember());725  RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDefaultInitialize());726  RecordDeclBits.addBit(D->isNonTrivialToPrimitiveCopy());727  RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDestroy());728  RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());729  RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDestructCUnion());730  RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveCopyCUnion());731  RecordDeclBits.addBit(D->hasUninitializedExplicitInitFields());732  RecordDeclBits.addBit(D->isParamDestroyedInCallee());733  RecordDeclBits.addBits(llvm::to_underlying(D->getArgPassingRestrictions()), 2);734  Record.push_back(RecordDeclBits);735 736  // Only compute this for C/Objective-C, in C++ this is computed as part737  // of CXXRecordDecl.738  if (!isa<CXXRecordDecl>(D))739    Record.push_back(D->getODRHash());740 741  if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&742      !D->isImplicit() && !D->isInvalidDecl() && !D->hasExtInfo() &&743      !D->getTypedefNameForAnonDecl() &&744      D->getFirstDecl() == D->getMostRecentDecl() &&745      !D->isTopLevelDeclInObjCContainer() &&746      !CXXRecordDecl::classofKind(D->getKind()) &&747      !needsAnonymousDeclarationNumber(D) &&748      D->getDeclName().getNameKind() == DeclarationName::Identifier)749    AbbrevToUse = Writer.getDeclRecordAbbrev();750 751  Code = serialization::DECL_RECORD;752}753 754void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {755  VisitNamedDecl(D);756  Record.AddTypeRef(D->getType());757}758 759void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {760  VisitValueDecl(D);761  Record.push_back(D->getInitExpr()? 1 : 0);762  if (D->getInitExpr())763    Record.AddStmt(D->getInitExpr());764  Record.AddAPSInt(D->getInitVal());765 766  Code = serialization::DECL_ENUM_CONSTANT;767}768 769void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {770  VisitValueDecl(D);771  Record.AddSourceLocation(D->getInnerLocStart());772  Record.push_back(D->hasExtInfo());773  if (D->hasExtInfo()) {774    DeclaratorDecl::ExtInfo *Info = D->getExtInfo();775    Record.AddQualifierInfo(*Info);776    Record.AddStmt(777        const_cast<Expr *>(Info->TrailingRequiresClause.ConstraintExpr));778    Record.writeUnsignedOrNone(Info->TrailingRequiresClause.ArgPackSubstIndex);779  }780  // The location information is deferred until the end of the record.781  Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()782                                           : QualType());783}784 785void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {786  static_assert(DeclContext::NumFunctionDeclBits == 45,787                "You need to update the serializer after you change the "788                "FunctionDeclBits");789 790  VisitRedeclarable(D);791 792  Record.push_back(D->getTemplatedKind());793  switch (D->getTemplatedKind()) {794  case FunctionDecl::TK_NonTemplate:795    break;796  case FunctionDecl::TK_DependentNonTemplate:797    Record.AddDeclRef(D->getInstantiatedFromDecl());798    break;799  case FunctionDecl::TK_FunctionTemplate:800    Record.AddDeclRef(D->getDescribedFunctionTemplate());801    break;802  case FunctionDecl::TK_MemberSpecialization: {803    MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();804    Record.AddDeclRef(MemberInfo->getInstantiatedFrom());805    Record.push_back(MemberInfo->getTemplateSpecializationKind());806    Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());807    break;808  }809  case FunctionDecl::TK_FunctionTemplateSpecialization: {810    FunctionTemplateSpecializationInfo *811      FTSInfo = D->getTemplateSpecializationInfo();812 813    RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);814 815    Record.AddDeclRef(FTSInfo->getTemplate());816    Record.push_back(FTSInfo->getTemplateSpecializationKind());817 818    // Template arguments.819    Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);820 821    // Template args as written.822    Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);823    if (FTSInfo->TemplateArgumentsAsWritten)824      Record.AddASTTemplateArgumentListInfo(825          FTSInfo->TemplateArgumentsAsWritten);826 827    Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());828 829    if (MemberSpecializationInfo *MemberInfo =830        FTSInfo->getMemberSpecializationInfo()) {831      Record.push_back(1);832      Record.AddDeclRef(MemberInfo->getInstantiatedFrom());833      Record.push_back(MemberInfo->getTemplateSpecializationKind());834      Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());835    } else {836      Record.push_back(0);837    }838 839    if (D->isCanonicalDecl()) {840      // Write the template that contains the specializations set. We will841      // add a FunctionTemplateSpecializationInfo to it when reading.842      Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());843    }844    break;845  }846  case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {847    DependentFunctionTemplateSpecializationInfo *848      DFTSInfo = D->getDependentSpecializationInfo();849 850    // Candidates.851    Record.push_back(DFTSInfo->getCandidates().size());852    for (FunctionTemplateDecl *FTD : DFTSInfo->getCandidates())853      Record.AddDeclRef(FTD);854 855    // Templates args.856    Record.push_back(DFTSInfo->TemplateArgumentsAsWritten != nullptr);857    if (DFTSInfo->TemplateArgumentsAsWritten)858      Record.AddASTTemplateArgumentListInfo(859          DFTSInfo->TemplateArgumentsAsWritten);860    break;861  }862  }863 864  VisitDeclaratorDecl(D);865  Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());866  Record.push_back(D->getIdentifierNamespace());867 868  // The order matters here. It will be better to put the bit with higher869  // probability to be 0 in the end of the bits. See the comments in VisitDecl870  // for details.871  BitsPacker FunctionDeclBits;872  // FIXME: stable encoding873  FunctionDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()), 3);874  FunctionDeclBits.addBits((uint32_t)D->getStorageClass(), /*BitWidth=*/3);875  FunctionDeclBits.addBit(D->isInlineSpecified());876  FunctionDeclBits.addBit(D->isInlined());877  FunctionDeclBits.addBit(D->hasSkippedBody());878  FunctionDeclBits.addBit(D->isVirtualAsWritten());879  FunctionDeclBits.addBit(D->isPureVirtual());880  FunctionDeclBits.addBit(D->hasInheritedPrototype());881  FunctionDeclBits.addBit(D->hasWrittenPrototype());882  FunctionDeclBits.addBit(D->isDeletedBit());883  FunctionDeclBits.addBit(D->isTrivial());884  FunctionDeclBits.addBit(D->isTrivialForCall());885  FunctionDeclBits.addBit(D->isDefaulted());886  FunctionDeclBits.addBit(D->isExplicitlyDefaulted());887  FunctionDeclBits.addBit(D->isIneligibleOrNotSelected());888  FunctionDeclBits.addBits((uint64_t)(D->getConstexprKind()), /*BitWidth=*/2);889  FunctionDeclBits.addBit(D->hasImplicitReturnZero());890  FunctionDeclBits.addBit(D->isMultiVersion());891  FunctionDeclBits.addBit(D->isLateTemplateParsed());892  FunctionDeclBits.addBit(D->isInstantiatedFromMemberTemplate());893  FunctionDeclBits.addBit(D->FriendConstraintRefersToEnclosingTemplate());894  FunctionDeclBits.addBit(D->usesSEHTry());895  FunctionDeclBits.addBit(D->isDestroyingOperatorDelete());896  FunctionDeclBits.addBit(D->isTypeAwareOperatorNewOrDelete());897  Record.push_back(FunctionDeclBits);898 899  Record.AddSourceLocation(D->getEndLoc());900  if (D->isExplicitlyDefaulted())901    Record.AddSourceLocation(D->getDefaultLoc());902 903  Record.push_back(D->getODRHash());904 905  if (D->isDefaulted() || D->isDeletedAsWritten()) {906    if (auto *FDI = D->getDefaultedOrDeletedInfo()) {907      // Store both that there is an DefaultedOrDeletedInfo and whether it908      // contains a DeletedMessage.909      StringLiteral *DeletedMessage = FDI->getDeletedMessage();910      Record.push_back(1 | (DeletedMessage ? 2 : 0));911      if (DeletedMessage)912        Record.AddStmt(DeletedMessage);913 914      Record.push_back(FDI->getUnqualifiedLookups().size());915      for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {916        Record.AddDeclRef(P.getDecl());917        Record.push_back(P.getAccess());918      }919    } else {920      Record.push_back(0);921    }922  }923 924  if (D->getFriendObjectKind()) {925    // For a friend function defined inline within a class template, we have to926    // force the definition to be the one inside the definition of the template927    // class. Remember this relation to deserialize them together.928    if (auto *RD = dyn_cast<CXXRecordDecl>(D->getLexicalParent());929        RD && isDefinitionInDependentContext(RD)) {930      Writer.RelatedDeclsMap[Writer.GetDeclRef(RD)].push_back(931          Writer.GetDeclRef(D));932    }933  }934 935  Record.push_back(D->param_size());936  for (auto *P : D->parameters())937    Record.AddDeclRef(P);938  Code = serialization::DECL_FUNCTION;939}940 941static void addExplicitSpecifier(ExplicitSpecifier ES,942                                 ASTRecordWriter &Record) {943  uint64_t Kind = static_cast<uint64_t>(ES.getKind());944  Kind = Kind << 1 | static_cast<bool>(ES.getExpr());945  Record.push_back(Kind);946  if (ES.getExpr()) {947    Record.AddStmt(ES.getExpr());948  }949}950 951void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {952  addExplicitSpecifier(D->getExplicitSpecifier(), Record);953  Record.AddDeclRef(D->Ctor);954  VisitFunctionDecl(D);955  Record.push_back(static_cast<unsigned char>(D->getDeductionCandidateKind()));956  Record.AddDeclRef(D->getSourceDeductionGuide());957  Record.push_back(958      static_cast<unsigned char>(D->getSourceDeductionGuideKind()));959  Code = serialization::DECL_CXX_DEDUCTION_GUIDE;960}961 962void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {963  static_assert(DeclContext::NumObjCMethodDeclBits == 37,964                "You need to update the serializer after you change the "965                "ObjCMethodDeclBits");966 967  VisitNamedDecl(D);968  // FIXME: convert to LazyStmtPtr?969  // Unlike C/C++, method bodies will never be in header files.970  bool HasBodyStuff = D->getBody() != nullptr;971  Record.push_back(HasBodyStuff);972  if (HasBodyStuff) {973    Record.AddStmt(D->getBody());974  }975  Record.AddDeclRef(D->getSelfDecl());976  Record.AddDeclRef(D->getCmdDecl());977  Record.push_back(D->isInstanceMethod());978  Record.push_back(D->isVariadic());979  Record.push_back(D->isPropertyAccessor());980  Record.push_back(D->isSynthesizedAccessorStub());981  Record.push_back(D->isDefined());982  Record.push_back(D->isOverriding());983  Record.push_back(D->hasSkippedBody());984 985  Record.push_back(D->isRedeclaration());986  Record.push_back(D->hasRedeclaration());987  if (D->hasRedeclaration()) {988    assert(Record.getASTContext().getObjCMethodRedeclaration(D));989    Record.AddDeclRef(Record.getASTContext().getObjCMethodRedeclaration(D));990  }991 992  // FIXME: stable encoding for @required/@optional993  Record.push_back(llvm::to_underlying(D->getImplementationControl()));994  // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability995  Record.push_back(D->getObjCDeclQualifier());996  Record.push_back(D->hasRelatedResultType());997  Record.AddTypeRef(D->getReturnType());998  Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());999  Record.AddSourceLocation(D->getEndLoc());1000  Record.push_back(D->param_size());1001  for (const auto *P : D->parameters())1002    Record.AddDeclRef(P);1003 1004  Record.push_back(D->getSelLocsKind());1005  unsigned NumStoredSelLocs = D->getNumStoredSelLocs();1006  SourceLocation *SelLocs = D->getStoredSelLocs();1007  Record.push_back(NumStoredSelLocs);1008  for (unsigned i = 0; i != NumStoredSelLocs; ++i)1009    Record.AddSourceLocation(SelLocs[i]);1010 1011  Code = serialization::DECL_OBJC_METHOD;1012}1013 1014void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {1015  VisitTypedefNameDecl(D);1016  Record.push_back(D->Variance);1017  Record.push_back(D->Index);1018  Record.AddSourceLocation(D->VarianceLoc);1019  Record.AddSourceLocation(D->ColonLoc);1020 1021  Code = serialization::DECL_OBJC_TYPE_PARAM;1022}1023 1024void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {1025  static_assert(DeclContext::NumObjCContainerDeclBits == 64,1026                "You need to update the serializer after you change the "1027                "ObjCContainerDeclBits");1028 1029  VisitNamedDecl(D);1030  Record.AddSourceLocation(D->getAtStartLoc());1031  Record.AddSourceRange(D->getAtEndRange());1032  // Abstract class (no need to define a stable serialization::DECL code).1033}1034 1035void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {1036  VisitRedeclarable(D);1037  VisitObjCContainerDecl(D);1038  Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));1039  AddObjCTypeParamList(D->TypeParamList);1040 1041  Record.push_back(D->isThisDeclarationADefinition());1042  if (D->isThisDeclarationADefinition()) {1043    // Write the DefinitionData1044    ObjCInterfaceDecl::DefinitionData &Data = D->data();1045 1046    Record.AddTypeSourceInfo(D->getSuperClassTInfo());1047    Record.AddSourceLocation(D->getEndOfDefinitionLoc());1048    Record.push_back(Data.HasDesignatedInitializers);1049    Record.push_back(D->getODRHash());1050 1051    // Write out the protocols that are directly referenced by the @interface.1052    Record.push_back(Data.ReferencedProtocols.size());1053    for (const auto *P : D->protocols())1054      Record.AddDeclRef(P);1055    for (const auto &PL : D->protocol_locs())1056      Record.AddSourceLocation(PL);1057 1058    // Write out the protocols that are transitively referenced.1059    Record.push_back(Data.AllReferencedProtocols.size());1060    for (ObjCList<ObjCProtocolDecl>::iterator1061              P = Data.AllReferencedProtocols.begin(),1062           PEnd = Data.AllReferencedProtocols.end();1063         P != PEnd; ++P)1064      Record.AddDeclRef(*P);1065 1066 1067    if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {1068      // Ensure that we write out the set of categories for this class.1069      Writer.ObjCClassesWithCategories.insert(D);1070 1071      // Make sure that the categories get serialized.1072      for (; Cat; Cat = Cat->getNextClassCategoryRaw())1073        (void)Writer.GetDeclRef(Cat);1074    }1075  }1076 1077  Code = serialization::DECL_OBJC_INTERFACE;1078}1079 1080void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {1081  VisitFieldDecl(D);1082  // FIXME: stable encoding for @public/@private/@protected/@package1083  Record.push_back(D->getAccessControl());1084  Record.push_back(D->getSynthesize());1085 1086  if (D->getDeclContext() == D->getLexicalDeclContext() &&1087      !D->hasAttrs() &&1088      !D->isImplicit() &&1089      !D->isUsed(false) &&1090      !D->isInvalidDecl() &&1091      !D->isReferenced() &&1092      !D->isModulePrivate() &&1093      !D->getBitWidth() &&1094      !D->hasExtInfo() &&1095      D->getDeclName())1096    AbbrevToUse = Writer.getDeclObjCIvarAbbrev();1097 1098  Code = serialization::DECL_OBJC_IVAR;1099}1100 1101void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {1102  VisitRedeclarable(D);1103  VisitObjCContainerDecl(D);1104 1105  Record.push_back(D->isThisDeclarationADefinition());1106  if (D->isThisDeclarationADefinition()) {1107    Record.push_back(D->protocol_size());1108    for (const auto *I : D->protocols())1109      Record.AddDeclRef(I);1110    for (const auto &PL : D->protocol_locs())1111      Record.AddSourceLocation(PL);1112    Record.push_back(D->getODRHash());1113  }1114 1115  Code = serialization::DECL_OBJC_PROTOCOL;1116}1117 1118void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {1119  VisitFieldDecl(D);1120  Code = serialization::DECL_OBJC_AT_DEFS_FIELD;1121}1122 1123void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {1124  VisitObjCContainerDecl(D);1125  Record.AddSourceLocation(D->getCategoryNameLoc());1126  Record.AddSourceLocation(D->getIvarLBraceLoc());1127  Record.AddSourceLocation(D->getIvarRBraceLoc());1128  Record.AddDeclRef(D->getClassInterface());1129  AddObjCTypeParamList(D->TypeParamList);1130  Record.push_back(D->protocol_size());1131  for (const auto *I : D->protocols())1132    Record.AddDeclRef(I);1133  for (const auto &PL : D->protocol_locs())1134    Record.AddSourceLocation(PL);1135  Code = serialization::DECL_OBJC_CATEGORY;1136}1137 1138void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {1139  VisitNamedDecl(D);1140  Record.AddDeclRef(D->getClassInterface());1141  Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;1142}1143 1144void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {1145  VisitNamedDecl(D);1146  Record.AddSourceLocation(D->getAtLoc());1147  Record.AddSourceLocation(D->getLParenLoc());1148  Record.AddTypeRef(D->getType());1149  Record.AddTypeSourceInfo(D->getTypeSourceInfo());1150  // FIXME: stable encoding1151  Record.push_back((unsigned)D->getPropertyAttributes());1152  Record.push_back((unsigned)D->getPropertyAttributesAsWritten());1153  // FIXME: stable encoding1154  Record.push_back((unsigned)D->getPropertyImplementation());1155  Record.AddDeclarationName(D->getGetterName());1156  Record.AddSourceLocation(D->getGetterNameLoc());1157  Record.AddDeclarationName(D->getSetterName());1158  Record.AddSourceLocation(D->getSetterNameLoc());1159  Record.AddDeclRef(D->getGetterMethodDecl());1160  Record.AddDeclRef(D->getSetterMethodDecl());1161  Record.AddDeclRef(D->getPropertyIvarDecl());1162  Code = serialization::DECL_OBJC_PROPERTY;1163}1164 1165void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {1166  VisitObjCContainerDecl(D);1167  Record.AddDeclRef(D->getClassInterface());1168  // Abstract class (no need to define a stable serialization::DECL code).1169}1170 1171void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {1172  VisitObjCImplDecl(D);1173  Record.AddSourceLocation(D->getCategoryNameLoc());1174  Code = serialization::DECL_OBJC_CATEGORY_IMPL;1175}1176 1177void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {1178  VisitObjCImplDecl(D);1179  Record.AddDeclRef(D->getSuperClass());1180  Record.AddSourceLocation(D->getSuperClassLoc());1181  Record.AddSourceLocation(D->getIvarLBraceLoc());1182  Record.AddSourceLocation(D->getIvarRBraceLoc());1183  Record.push_back(D->hasNonZeroConstructors());1184  Record.push_back(D->hasDestructors());1185  Record.push_back(D->NumIvarInitializers);1186  if (D->NumIvarInitializers)1187    Record.AddCXXCtorInitializers(1188        llvm::ArrayRef(D->init_begin(), D->init_end()));1189  Code = serialization::DECL_OBJC_IMPLEMENTATION;1190}1191 1192void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {1193  VisitDecl(D);1194  Record.AddSourceLocation(D->getBeginLoc());1195  Record.AddDeclRef(D->getPropertyDecl());1196  Record.AddDeclRef(D->getPropertyIvarDecl());1197  Record.AddSourceLocation(D->getPropertyIvarDeclLoc());1198  Record.AddDeclRef(D->getGetterMethodDecl());1199  Record.AddDeclRef(D->getSetterMethodDecl());1200  Record.AddStmt(D->getGetterCXXConstructor());1201  Record.AddStmt(D->getSetterCXXAssignment());1202  Code = serialization::DECL_OBJC_PROPERTY_IMPL;1203}1204 1205void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {1206  VisitDeclaratorDecl(D);1207  Record.push_back(D->isMutable());1208 1209  Record.push_back((D->StorageKind << 1) | D->BitField);1210  if (D->StorageKind == FieldDecl::ISK_CapturedVLAType)1211    Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));1212  else if (D->BitField)1213    Record.AddStmt(D->getBitWidth());1214 1215  if (!D->getDeclName() || D->isPlaceholderVar(Writer.getLangOpts()))1216    Record.AddDeclRef(1217        Record.getASTContext().getInstantiatedFromUnnamedFieldDecl(D));1218 1219  if (D->getDeclContext() == D->getLexicalDeclContext() &&1220      !D->hasAttrs() &&1221      !D->isImplicit() &&1222      !D->isUsed(false) &&1223      !D->isInvalidDecl() &&1224      !D->isReferenced() &&1225      !D->isTopLevelDeclInObjCContainer() &&1226      !D->isModulePrivate() &&1227      !D->getBitWidth() &&1228      !D->hasInClassInitializer() &&1229      !D->hasCapturedVLAType() &&1230      !D->hasExtInfo() &&1231      !ObjCIvarDecl::classofKind(D->getKind()) &&1232      !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&1233      D->getDeclName())1234    AbbrevToUse = Writer.getDeclFieldAbbrev();1235 1236  Code = serialization::DECL_FIELD;1237}1238 1239void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {1240  VisitDeclaratorDecl(D);1241  Record.AddIdentifierRef(D->getGetterId());1242  Record.AddIdentifierRef(D->getSetterId());1243  Code = serialization::DECL_MS_PROPERTY;1244}1245 1246void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {1247  VisitValueDecl(D);1248  MSGuidDecl::Parts Parts = D->getParts();1249  Record.push_back(Parts.Part1);1250  Record.push_back(Parts.Part2);1251  Record.push_back(Parts.Part3);1252  Record.append(std::begin(Parts.Part4And5), std::end(Parts.Part4And5));1253  Code = serialization::DECL_MS_GUID;1254}1255 1256void ASTDeclWriter::VisitUnnamedGlobalConstantDecl(1257    UnnamedGlobalConstantDecl *D) {1258  VisitValueDecl(D);1259  Record.AddAPValue(D->getValue());1260  Code = serialization::DECL_UNNAMED_GLOBAL_CONSTANT;1261}1262 1263void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {1264  VisitValueDecl(D);1265  Record.AddAPValue(D->getValue());1266  Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;1267}1268 1269void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {1270  VisitValueDecl(D);1271  Record.push_back(D->getChainingSize());1272 1273  for (const auto *P : D->chain())1274    Record.AddDeclRef(P);1275  Code = serialization::DECL_INDIRECTFIELD;1276}1277 1278void ASTDeclWriter::VisitVarDecl(VarDecl *D) {1279  VisitRedeclarable(D);1280  VisitDeclaratorDecl(D);1281 1282  // The order matters here. It will be better to put the bit with higher1283  // probability to be 0 in the end of the bits. See the comments in VisitDecl1284  // for details.1285  BitsPacker VarDeclBits;1286  VarDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()),1287                      /*BitWidth=*/3);1288 1289  bool ModulesCodegen = shouldVarGenerateHereOnly(D);1290  VarDeclBits.addBit(ModulesCodegen);1291 1292  VarDeclBits.addBits(D->getStorageClass(), /*BitWidth=*/3);1293  VarDeclBits.addBits(D->getTSCSpec(), /*BitWidth=*/2);1294  VarDeclBits.addBits(D->getInitStyle(), /*BitWidth=*/2);1295  VarDeclBits.addBit(D->isARCPseudoStrong());1296 1297  bool HasDeducedType = false;1298  if (!isa<ParmVarDecl>(D)) {1299    VarDeclBits.addBit(D->isThisDeclarationADemotedDefinition());1300    VarDeclBits.addBit(D->isExceptionVariable());1301    VarDeclBits.addBit(D->isNRVOVariable());1302    VarDeclBits.addBit(D->isCXXForRangeDecl());1303 1304    VarDeclBits.addBit(D->isInline());1305    VarDeclBits.addBit(D->isInlineSpecified());1306    VarDeclBits.addBit(D->isConstexpr());1307    VarDeclBits.addBit(D->isInitCapture());1308    VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());1309 1310    VarDeclBits.addBit(D->isEscapingByref());1311    HasDeducedType = D->getType()->getContainedDeducedType();1312    VarDeclBits.addBit(HasDeducedType);1313 1314    if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))1315      VarDeclBits.addBits(llvm::to_underlying(IPD->getParameterKind()),1316                          /*Width=*/3);1317    else1318      VarDeclBits.addBits(0, /*Width=*/3);1319 1320    VarDeclBits.addBit(D->isObjCForDecl());1321    VarDeclBits.addBit(D->isCXXForRangeImplicitVar());1322  }1323 1324  Record.push_back(VarDeclBits);1325 1326  if (ModulesCodegen)1327    Writer.AddDeclRef(D, Writer.ModularCodegenDecls);1328 1329  if (D->hasAttr<BlocksAttr>()) {1330    BlockVarCopyInit Init = Record.getASTContext().getBlockVarCopyInit(D);1331    Record.AddStmt(Init.getCopyExpr());1332    if (Init.getCopyExpr())1333      Record.push_back(Init.canThrow());1334  }1335 1336  enum {1337    VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization1338  };1339  if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {1340    Record.push_back(VarTemplate);1341    Record.AddDeclRef(TemplD);1342  } else if (MemberSpecializationInfo *SpecInfo1343               = D->getMemberSpecializationInfo()) {1344    Record.push_back(StaticDataMemberSpecialization);1345    Record.AddDeclRef(SpecInfo->getInstantiatedFrom());1346    Record.push_back(SpecInfo->getTemplateSpecializationKind());1347    Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());1348  } else {1349    Record.push_back(VarNotTemplate);1350  }1351 1352  if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&1353      !D->isTopLevelDeclInObjCContainer() &&1354      !needsAnonymousDeclarationNumber(D) &&1355      D->getDeclName().getNameKind() == DeclarationName::Identifier &&1356      !D->hasExtInfo() && D->getFirstDecl() == D->getMostRecentDecl() &&1357      D->getKind() == Decl::Var && !D->isInline() && !D->isConstexpr() &&1358      !D->isInitCapture() && !D->isPreviousDeclInSameBlockScope() &&1359      !D->hasInitWithSideEffects() && !D->isEscapingByref() &&1360      !HasDeducedType && D->getStorageDuration() != SD_Static &&1361      !D->getDescribedVarTemplate() && !D->getMemberSpecializationInfo() &&1362      !D->isObjCForDecl() && !isa<ImplicitParamDecl>(D) &&1363      !D->isEscapingByref())1364    AbbrevToUse = Writer.getDeclVarAbbrev();1365 1366  Code = serialization::DECL_VAR;1367}1368 1369void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {1370  VisitVarDecl(D);1371  Code = serialization::DECL_IMPLICIT_PARAM;1372}1373 1374void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {1375  VisitVarDecl(D);1376 1377  // See the implementation of `ParmVarDecl::getParameterIndex()`, which may1378  // exceed the size of the normal bitfield. So it may be better to not pack1379  // these bits.1380  Record.push_back(D->getFunctionScopeIndex());1381 1382  BitsPacker ParmVarDeclBits;1383  ParmVarDeclBits.addBit(D->isObjCMethodParameter());1384  ParmVarDeclBits.addBits(D->getFunctionScopeDepth(), /*BitsWidth=*/7);1385  // FIXME: stable encoding1386  ParmVarDeclBits.addBits(D->getObjCDeclQualifier(), /*BitsWidth=*/7);1387  ParmVarDeclBits.addBit(D->isKNRPromoted());1388  ParmVarDeclBits.addBit(D->hasInheritedDefaultArg());1389  ParmVarDeclBits.addBit(D->hasUninstantiatedDefaultArg());1390  ParmVarDeclBits.addBit(D->getExplicitObjectParamThisLoc().isValid());1391  Record.push_back(ParmVarDeclBits);1392 1393  if (D->hasUninstantiatedDefaultArg())1394    Record.AddStmt(D->getUninstantiatedDefaultArg());1395  if (D->getExplicitObjectParamThisLoc().isValid())1396    Record.AddSourceLocation(D->getExplicitObjectParamThisLoc());1397  Code = serialization::DECL_PARM_VAR;1398 1399  // If the assumptions about the DECL_PARM_VAR abbrev are true, use it.  Here1400  // we dynamically check for the properties that we optimize for, but don't1401  // know are true of all PARM_VAR_DECLs.1402  if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&1403      !D->hasExtInfo() && D->getStorageClass() == 0 && !D->isInvalidDecl() &&1404      !D->isTopLevelDeclInObjCContainer() &&1405      D->getInitStyle() == VarDecl::CInit && // Can params have anything else?1406      D->getInit() == nullptr)               // No default expr.1407    AbbrevToUse = Writer.getDeclParmVarAbbrev();1408 1409  // Check things we know are true of *every* PARM_VAR_DECL, which is more than1410  // just us assuming it.1411  assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");1412  assert(!D->isThisDeclarationADemotedDefinition()1413         && "PARM_VAR_DECL can't be demoted definition.");1414  assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");1415  assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");1416  assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");1417  assert(!D->isStaticDataMember() &&1418         "PARM_VAR_DECL can't be static data member");1419}1420 1421void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {1422  // Record the number of bindings first to simplify deserialization.1423  Record.push_back(D->bindings().size());1424 1425  VisitVarDecl(D);1426  for (auto *B : D->bindings())1427    Record.AddDeclRef(B);1428  Code = serialization::DECL_DECOMPOSITION;1429}1430 1431void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {1432  VisitValueDecl(D);1433  Record.AddStmt(D->getBinding());1434  Code = serialization::DECL_BINDING;1435}1436 1437void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {1438  VisitDecl(D);1439  Record.AddStmt(D->getAsmStringExpr());1440  Record.AddSourceLocation(D->getRParenLoc());1441  Code = serialization::DECL_FILE_SCOPE_ASM;1442}1443 1444void ASTDeclWriter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {1445  VisitDecl(D);1446  Record.AddStmt(D->getStmt());1447  Code = serialization::DECL_TOP_LEVEL_STMT_DECL;1448}1449 1450void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {1451  VisitDecl(D);1452  Code = serialization::DECL_EMPTY;1453}1454 1455void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(1456    LifetimeExtendedTemporaryDecl *D) {1457  VisitDecl(D);1458  Record.AddDeclRef(D->getExtendingDecl());1459  Record.AddStmt(D->getTemporaryExpr());1460  Record.push_back(static_cast<bool>(D->getValue()));1461  if (D->getValue())1462    Record.AddAPValue(*D->getValue());1463  Record.push_back(D->getManglingNumber());1464  Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;1465}1466void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {1467  VisitDecl(D);1468  Record.AddStmt(D->getBody());1469  Record.AddTypeSourceInfo(D->getSignatureAsWritten());1470  Record.push_back(D->param_size());1471  for (ParmVarDecl *P : D->parameters())1472    Record.AddDeclRef(P);1473  Record.push_back(D->isVariadic());1474  Record.push_back(D->blockMissingReturnType());1475  Record.push_back(D->isConversionFromLambda());1476  Record.push_back(D->doesNotEscape());1477  Record.push_back(D->canAvoidCopyToHeap());1478  Record.push_back(D->capturesCXXThis());1479  Record.push_back(D->getNumCaptures());1480  for (const auto &capture : D->captures()) {1481    Record.AddDeclRef(capture.getVariable());1482 1483    unsigned flags = 0;1484    if (capture.isByRef()) flags |= 1;1485    if (capture.isNested()) flags |= 2;1486    if (capture.hasCopyExpr()) flags |= 4;1487    Record.push_back(flags);1488 1489    if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());1490  }1491 1492  Code = serialization::DECL_BLOCK;1493}1494 1495void ASTDeclWriter::VisitOutlinedFunctionDecl(OutlinedFunctionDecl *D) {1496  Record.push_back(D->getNumParams());1497  VisitDecl(D);1498  for (unsigned I = 0; I < D->getNumParams(); ++I)1499    Record.AddDeclRef(D->getParam(I));1500  Record.push_back(D->isNothrow() ? 1 : 0);1501  Record.AddStmt(D->getBody());1502  Code = serialization::DECL_OUTLINEDFUNCTION;1503}1504 1505void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {1506  Record.push_back(CD->getNumParams());1507  VisitDecl(CD);1508  Record.push_back(CD->getContextParamPosition());1509  Record.push_back(CD->isNothrow() ? 1 : 0);1510  // Body is stored by VisitCapturedStmt.1511  for (unsigned I = 0; I < CD->getNumParams(); ++I)1512    Record.AddDeclRef(CD->getParam(I));1513  Code = serialization::DECL_CAPTURED;1514}1515 1516void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {1517  static_assert(DeclContext::NumLinkageSpecDeclBits == 17,1518                "You need to update the serializer after you change the"1519                "LinkageSpecDeclBits");1520 1521  VisitDecl(D);1522  Record.push_back(llvm::to_underlying(D->getLanguage()));1523  Record.AddSourceLocation(D->getExternLoc());1524  Record.AddSourceLocation(D->getRBraceLoc());1525  Code = serialization::DECL_LINKAGE_SPEC;1526}1527 1528void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {1529  VisitDecl(D);1530  Record.AddSourceLocation(D->getRBraceLoc());1531  Code = serialization::DECL_EXPORT;1532}1533 1534void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {1535  VisitNamedDecl(D);1536  Record.AddSourceLocation(D->getBeginLoc());1537  Code = serialization::DECL_LABEL;1538}1539 1540 1541void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {1542  VisitRedeclarable(D);1543  VisitNamedDecl(D);1544 1545  BitsPacker NamespaceDeclBits;1546  NamespaceDeclBits.addBit(D->isInline());1547  NamespaceDeclBits.addBit(D->isNested());1548  Record.push_back(NamespaceDeclBits);1549 1550  Record.AddSourceLocation(D->getBeginLoc());1551  Record.AddSourceLocation(D->getRBraceLoc());1552 1553  if (D->isFirstDecl())1554    Record.AddDeclRef(D->getAnonymousNamespace());1555  Code = serialization::DECL_NAMESPACE;1556 1557  if (Writer.hasChain() && D->isAnonymousNamespace() &&1558      D == D->getMostRecentDecl()) {1559    // This is a most recent reopening of the anonymous namespace. If its parent1560    // is in a previous PCH (or is the TU), mark that parent for update, because1561    // the original namespace always points to the latest re-opening of its1562    // anonymous namespace.1563    Decl *Parent = cast<Decl>(1564        D->getParent()->getRedeclContext()->getPrimaryContext());1565    if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {1566      Writer.DeclUpdates[Parent].push_back(1567          ASTWriter::DeclUpdate(DeclUpdateKind::CXXAddedAnonymousNamespace, D));1568    }1569  }1570}1571 1572void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {1573  VisitRedeclarable(D);1574  VisitNamedDecl(D);1575  Record.AddSourceLocation(D->getNamespaceLoc());1576  Record.AddSourceLocation(D->getTargetNameLoc());1577  Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1578  Record.AddDeclRef(D->getNamespace());1579  Code = serialization::DECL_NAMESPACE_ALIAS;1580}1581 1582void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {1583  VisitNamedDecl(D);1584  Record.AddSourceLocation(D->getUsingLoc());1585  Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1586  Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());1587  Record.AddDeclRef(D->FirstUsingShadow.getPointer());1588  Record.push_back(D->hasTypename());1589  Record.AddDeclRef(Record.getASTContext().getInstantiatedFromUsingDecl(D));1590  Code = serialization::DECL_USING;1591}1592 1593void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {1594  VisitNamedDecl(D);1595  Record.AddSourceLocation(D->getUsingLoc());1596  Record.AddSourceLocation(D->getEnumLoc());1597  Record.AddTypeSourceInfo(D->getEnumType());1598  Record.AddDeclRef(D->FirstUsingShadow.getPointer());1599  Record.AddDeclRef(Record.getASTContext().getInstantiatedFromUsingEnumDecl(D));1600  Code = serialization::DECL_USING_ENUM;1601}1602 1603void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {1604  Record.push_back(D->NumExpansions);1605  VisitNamedDecl(D);1606  Record.AddDeclRef(D->getInstantiatedFromUsingDecl());1607  for (auto *E : D->expansions())1608    Record.AddDeclRef(E);1609  Code = serialization::DECL_USING_PACK;1610}1611 1612void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {1613  VisitRedeclarable(D);1614  VisitNamedDecl(D);1615  Record.AddDeclRef(D->getTargetDecl());1616  Record.push_back(D->getIdentifierNamespace());1617  Record.AddDeclRef(D->UsingOrNextShadow);1618  Record.AddDeclRef(1619      Record.getASTContext().getInstantiatedFromUsingShadowDecl(D));1620 1621  if (D->getDeclContext() == D->getLexicalDeclContext() &&1622      D->getFirstDecl() == D->getMostRecentDecl() && !D->hasAttrs() &&1623      !needsAnonymousDeclarationNumber(D) &&1624      D->getDeclName().getNameKind() == DeclarationName::Identifier)1625    AbbrevToUse = Writer.getDeclUsingShadowAbbrev();1626 1627  Code = serialization::DECL_USING_SHADOW;1628}1629 1630void ASTDeclWriter::VisitConstructorUsingShadowDecl(1631    ConstructorUsingShadowDecl *D) {1632  VisitUsingShadowDecl(D);1633  Record.AddDeclRef(D->NominatedBaseClassShadowDecl);1634  Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);1635  Record.push_back(D->IsVirtual);1636  Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;1637}1638 1639void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {1640  VisitNamedDecl(D);1641  Record.AddSourceLocation(D->getUsingLoc());1642  Record.AddSourceLocation(D->getNamespaceKeyLocation());1643  Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1644  Record.AddDeclRef(D->getNominatedNamespace());1645  Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));1646  Code = serialization::DECL_USING_DIRECTIVE;1647}1648 1649void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {1650  VisitValueDecl(D);1651  Record.AddSourceLocation(D->getUsingLoc());1652  Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1653  Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());1654  Record.AddSourceLocation(D->getEllipsisLoc());1655  Code = serialization::DECL_UNRESOLVED_USING_VALUE;1656}1657 1658void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(1659                                               UnresolvedUsingTypenameDecl *D) {1660  VisitTypeDecl(D);1661  Record.AddSourceLocation(D->getTypenameLoc());1662  Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1663  Record.AddSourceLocation(D->getEllipsisLoc());1664  Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;1665}1666 1667void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(1668    UnresolvedUsingIfExistsDecl *D) {1669  VisitNamedDecl(D);1670  Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;1671}1672 1673void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {1674  VisitRecordDecl(D);1675 1676  enum {1677    CXXRecNotTemplate = 0,1678    CXXRecTemplate,1679    CXXRecMemberSpecialization,1680    CXXLambda1681  };1682  if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {1683    Record.push_back(CXXRecTemplate);1684    Record.AddDeclRef(TemplD);1685  } else if (MemberSpecializationInfo *MSInfo1686               = D->getMemberSpecializationInfo()) {1687    Record.push_back(CXXRecMemberSpecialization);1688    Record.AddDeclRef(MSInfo->getInstantiatedFrom());1689    Record.push_back(MSInfo->getTemplateSpecializationKind());1690    Record.AddSourceLocation(MSInfo->getPointOfInstantiation());1691  } else if (D->isLambda()) {1692    // For a lambda, we need some information early for merging.1693    Record.push_back(CXXLambda);1694    if (auto *Context = D->getLambdaContextDecl()) {1695      Record.AddDeclRef(Context);1696      Record.push_back(D->getLambdaIndexInContext());1697    } else {1698      Record.push_back(0);1699    }1700    // For lambdas inside template functions, remember the mapping to1701    // deserialize them together.1702    if (auto *FD = llvm::dyn_cast_or_null<FunctionDecl>(D->getDeclContext());1703        FD && isDefinitionInDependentContext(FD)) {1704      Writer.RelatedDeclsMap[Writer.GetDeclRef(FD)].push_back(1705          Writer.GetDeclRef(D->getLambdaCallOperator()));1706    }1707  } else {1708    Record.push_back(CXXRecNotTemplate);1709  }1710 1711  Record.push_back(D->isThisDeclarationADefinition());1712  if (D->isThisDeclarationADefinition())1713    Record.AddCXXDefinitionData(D);1714 1715  if (D->isCompleteDefinition() && D->isInNamedModule())1716    Writer.AddDeclRef(D, Writer.ModularCodegenDecls);1717 1718  // Store (what we currently believe to be) the key function to avoid1719  // deserializing every method so we can compute it.1720  //1721  // FIXME: Avoid adding the key function if the class is defined in1722  // module purview since in that case the key function is meaningless.1723  if (D->isCompleteDefinition())1724    Record.AddDeclRef(Record.getASTContext().getCurrentKeyFunction(D));1725 1726  Code = serialization::DECL_CXX_RECORD;1727}1728 1729void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {1730  VisitFunctionDecl(D);1731  if (D->isCanonicalDecl()) {1732    Record.push_back(D->size_overridden_methods());1733    for (const CXXMethodDecl *MD : D->overridden_methods())1734      Record.AddDeclRef(MD);1735  } else {1736    // We only need to record overridden methods once for the canonical decl.1737    Record.push_back(0);1738  }1739 1740  if (D->getDeclContext() == D->getLexicalDeclContext() &&1741      D->getFirstDecl() == D->getMostRecentDecl() && !D->isInvalidDecl() &&1742      !D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() &&1743      D->getDeclName().getNameKind() == DeclarationName::Identifier &&1744      !D->hasExtInfo() && !D->isExplicitlyDefaulted()) {1745    if (D->getTemplatedKind() == FunctionDecl::TK_NonTemplate ||1746        D->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate ||1747        D->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||1748        D->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)1749      AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1750    else if (D->getTemplatedKind() ==1751             FunctionDecl::TK_FunctionTemplateSpecialization) {1752      FunctionTemplateSpecializationInfo *FTSInfo =1753          D->getTemplateSpecializationInfo();1754 1755      if (FTSInfo->TemplateArguments->size() == 1) {1756        const TemplateArgument &TA = FTSInfo->TemplateArguments->get(0);1757        if (TA.getKind() == TemplateArgument::Type &&1758            !FTSInfo->TemplateArgumentsAsWritten &&1759            !FTSInfo->getMemberSpecializationInfo())1760          AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1761      }1762    } else if (D->getTemplatedKind() ==1763               FunctionDecl::TK_DependentFunctionTemplateSpecialization) {1764      DependentFunctionTemplateSpecializationInfo *DFTSInfo =1765          D->getDependentSpecializationInfo();1766      if (!DFTSInfo->TemplateArgumentsAsWritten)1767        AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1768    }1769  }1770 1771  Code = serialization::DECL_CXX_METHOD;1772}1773 1774void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {1775  static_assert(DeclContext::NumCXXConstructorDeclBits == 64,1776                "You need to update the serializer after you change the "1777                "CXXConstructorDeclBits");1778 1779  Record.push_back(D->getTrailingAllocKind());1780  addExplicitSpecifier(D->getExplicitSpecifierInternal(), Record);1781  if (auto Inherited = D->getInheritedConstructor()) {1782    Record.AddDeclRef(Inherited.getShadowDecl());1783    Record.AddDeclRef(Inherited.getConstructor());1784  }1785 1786  VisitCXXMethodDecl(D);1787  Code = serialization::DECL_CXX_CONSTRUCTOR;1788}1789 1790void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {1791  VisitCXXMethodDecl(D);1792 1793  Record.AddDeclRef(D->getOperatorDelete());1794  if (D->getOperatorDelete())1795    Record.AddStmt(D->getOperatorDeleteThisArg());1796  Record.AddDeclRef(D->getOperatorGlobalDelete());1797 1798  Code = serialization::DECL_CXX_DESTRUCTOR;1799}1800 1801void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {1802  addExplicitSpecifier(D->getExplicitSpecifier(), Record);1803  VisitCXXMethodDecl(D);1804  Code = serialization::DECL_CXX_CONVERSION;1805}1806 1807void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {1808  VisitDecl(D);1809  Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));1810  ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();1811  Record.push_back(!IdentifierLocs.empty());1812  if (IdentifierLocs.empty()) {1813    Record.AddSourceLocation(D->getEndLoc());1814    Record.push_back(1);1815  } else {1816    for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)1817      Record.AddSourceLocation(IdentifierLocs[I]);1818    Record.push_back(IdentifierLocs.size());1819  }1820  // Note: the number of source locations must always be the last element in1821  // the record.1822  Code = serialization::DECL_IMPORT;1823}1824 1825void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {1826  VisitDecl(D);1827  Record.AddSourceLocation(D->getColonLoc());1828  Code = serialization::DECL_ACCESS_SPEC;1829}1830 1831void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {1832  // Record the number of friend type template parameter lists here1833  // so as to simplify memory allocation during deserialization.1834  Record.push_back(D->NumTPLists);1835  VisitDecl(D);1836  bool hasFriendDecl = isa<NamedDecl *>(D->Friend);1837  Record.push_back(hasFriendDecl);1838  if (hasFriendDecl)1839    Record.AddDeclRef(D->getFriendDecl());1840  else1841    Record.AddTypeSourceInfo(D->getFriendType());1842  for (unsigned i = 0; i < D->NumTPLists; ++i)1843    Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));1844  Record.AddDeclRef(D->getNextFriend());1845  Record.push_back(D->UnsupportedFriend);1846  Record.AddSourceLocation(D->FriendLoc);1847  Record.AddSourceLocation(D->EllipsisLoc);1848  Code = serialization::DECL_FRIEND;1849}1850 1851void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {1852  VisitDecl(D);1853  Record.push_back(D->getNumTemplateParameters());1854  for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)1855    Record.AddTemplateParameterList(D->getTemplateParameterList(i));1856  Record.push_back(D->getFriendDecl() != nullptr);1857  if (D->getFriendDecl())1858    Record.AddDeclRef(D->getFriendDecl());1859  else1860    Record.AddTypeSourceInfo(D->getFriendType());1861  Record.AddSourceLocation(D->getFriendLoc());1862  Code = serialization::DECL_FRIEND_TEMPLATE;1863}1864 1865void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {1866  VisitNamedDecl(D);1867 1868  Record.AddTemplateParameterList(D->getTemplateParameters());1869  Record.AddDeclRef(D->getTemplatedDecl());1870}1871 1872void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {1873  VisitTemplateDecl(D);1874  Record.AddStmt(D->getConstraintExpr());1875  Code = serialization::DECL_CONCEPT;1876}1877 1878void ASTDeclWriter::VisitImplicitConceptSpecializationDecl(1879    ImplicitConceptSpecializationDecl *D) {1880  Record.push_back(D->getTemplateArguments().size());1881  VisitDecl(D);1882  for (const TemplateArgument &Arg : D->getTemplateArguments())1883    Record.AddTemplateArgument(Arg);1884  Code = serialization::DECL_IMPLICIT_CONCEPT_SPECIALIZATION;1885}1886 1887void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {1888  Code = serialization::DECL_REQUIRES_EXPR_BODY;1889}1890 1891void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {1892  VisitRedeclarable(D);1893 1894  // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that1895  // getCommonPtr() can be used while this is still initializing.1896  if (D->isFirstDecl()) {1897    // This declaration owns the 'common' pointer, so serialize that data now.1898    Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());1899    if (D->getInstantiatedFromMemberTemplate())1900      Record.push_back(D->isMemberSpecialization());1901  }1902 1903  VisitTemplateDecl(D);1904  Record.push_back(D->getIdentifierNamespace());1905}1906 1907void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {1908  VisitRedeclarableTemplateDecl(D);1909 1910  if (D->isFirstDecl())1911    AddTemplateSpecializations(D);1912 1913  // Force emitting the corresponding deduction guide in reduced BMI mode.1914  // Otherwise, the deduction guide may be optimized out incorrectly.1915  if (Writer.isGeneratingReducedBMI()) {1916    auto Name =1917        Record.getASTContext().DeclarationNames.getCXXDeductionGuideName(D);1918    for (auto *DG : D->getDeclContext()->noload_lookup(Name))1919      Writer.GetDeclRef(DG->getCanonicalDecl());1920  }1921 1922  Code = serialization::DECL_CLASS_TEMPLATE;1923}1924 1925void ASTDeclWriter::VisitClassTemplateSpecializationDecl(1926                                           ClassTemplateSpecializationDecl *D) {1927  RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);1928 1929  VisitCXXRecordDecl(D);1930 1931  llvm::PointerUnion<ClassTemplateDecl *,1932                     ClassTemplatePartialSpecializationDecl *> InstFrom1933    = D->getSpecializedTemplateOrPartial();1934  if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {1935    Record.AddDeclRef(InstFromD);1936  } else {1937    Record.AddDeclRef(cast<ClassTemplatePartialSpecializationDecl *>(InstFrom));1938    Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());1939  }1940 1941  Record.AddTemplateArgumentList(&D->getTemplateArgs());1942  Record.AddSourceLocation(D->getPointOfInstantiation());1943  Record.push_back(D->getSpecializationKind());1944  Record.push_back(D->hasStrictPackMatch());1945  Record.push_back(D->isCanonicalDecl());1946 1947  if (D->isCanonicalDecl()) {1948    // When reading, we'll add it to the folding set of the following template.1949    Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());1950  }1951 1952  bool ExplicitInstantiation =1953      D->getTemplateSpecializationKind() ==1954          TSK_ExplicitInstantiationDeclaration ||1955      D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;1956  Record.push_back(ExplicitInstantiation);1957  if (ExplicitInstantiation) {1958    Record.AddSourceLocation(D->getExternKeywordLoc());1959    Record.AddSourceLocation(D->getTemplateKeywordLoc());1960  }1961 1962  const ASTTemplateArgumentListInfo *ArgsWritten =1963      D->getTemplateArgsAsWritten();1964  Record.push_back(!!ArgsWritten);1965  if (ArgsWritten)1966    Record.AddASTTemplateArgumentListInfo(ArgsWritten);1967 1968  // Mention the implicitly generated C++ deduction guide to make sure the1969  // deduction guide will be rewritten as expected.1970  //1971  // FIXME: Would it be more efficient to add a callback register function1972  // in sema to register the deduction guide?1973  if (Writer.isWritingStdCXXNamedModules()) {1974    auto Name =1975        Record.getASTContext().DeclarationNames.getCXXDeductionGuideName(1976            D->getSpecializedTemplate());1977    for (auto *DG : D->getDeclContext()->noload_lookup(Name))1978      Writer.GetDeclRef(DG->getCanonicalDecl());1979  }1980 1981  Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;1982}1983 1984void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(1985                                    ClassTemplatePartialSpecializationDecl *D) {1986  Record.AddTemplateParameterList(D->getTemplateParameters());1987 1988  VisitClassTemplateSpecializationDecl(D);1989 1990  // These are read/set from/to the first declaration.1991  if (D->getPreviousDecl() == nullptr) {1992    Record.AddDeclRef(D->getInstantiatedFromMember());1993    Record.push_back(D->isMemberSpecialization());1994  }1995 1996  Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;1997}1998 1999void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {2000  VisitRedeclarableTemplateDecl(D);2001 2002  if (D->isFirstDecl())2003    AddTemplateSpecializations(D);2004  Code = serialization::DECL_VAR_TEMPLATE;2005}2006 2007void ASTDeclWriter::VisitVarTemplateSpecializationDecl(2008    VarTemplateSpecializationDecl *D) {2009  RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);2010 2011  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>2012  InstFrom = D->getSpecializedTemplateOrPartial();2013  if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {2014    Record.AddDeclRef(InstFromD);2015  } else {2016    Record.AddDeclRef(cast<VarTemplatePartialSpecializationDecl *>(InstFrom));2017    Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());2018  }2019 2020  bool ExplicitInstantiation =2021      D->getTemplateSpecializationKind() ==2022          TSK_ExplicitInstantiationDeclaration ||2023      D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;2024  Record.push_back(ExplicitInstantiation);2025  if (ExplicitInstantiation) {2026    Record.AddSourceLocation(D->getExternKeywordLoc());2027    Record.AddSourceLocation(D->getTemplateKeywordLoc());2028  }2029 2030  const ASTTemplateArgumentListInfo *ArgsWritten =2031      D->getTemplateArgsAsWritten();2032  Record.push_back(!!ArgsWritten);2033  if (ArgsWritten)2034    Record.AddASTTemplateArgumentListInfo(ArgsWritten);2035 2036  Record.AddTemplateArgumentList(&D->getTemplateArgs());2037  Record.AddSourceLocation(D->getPointOfInstantiation());2038  Record.push_back(D->getSpecializationKind());2039  Record.push_back(D->IsCompleteDefinition);2040 2041  VisitVarDecl(D);2042 2043  Record.push_back(D->isCanonicalDecl());2044 2045  if (D->isCanonicalDecl()) {2046    // When reading, we'll add it to the folding set of the following template.2047    Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());2048  }2049 2050  Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;2051}2052 2053void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(2054    VarTemplatePartialSpecializationDecl *D) {2055  Record.AddTemplateParameterList(D->getTemplateParameters());2056 2057  VisitVarTemplateSpecializationDecl(D);2058 2059  // These are read/set from/to the first declaration.2060  if (D->getPreviousDecl() == nullptr) {2061    Record.AddDeclRef(D->getInstantiatedFromMember());2062    Record.push_back(D->isMemberSpecialization());2063  }2064 2065  Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;2066}2067 2068void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {2069  VisitRedeclarableTemplateDecl(D);2070 2071  if (D->isFirstDecl())2072    AddTemplateSpecializations(D);2073  Code = serialization::DECL_FUNCTION_TEMPLATE;2074}2075 2076void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {2077  Record.push_back(D->hasTypeConstraint());2078  VisitTypeDecl(D);2079 2080  Record.push_back(D->wasDeclaredWithTypename());2081 2082  const TypeConstraint *TC = D->getTypeConstraint();2083  if (D->hasTypeConstraint())2084    Record.push_back(/*TypeConstraintInitialized=*/TC != nullptr);2085  if (TC) {2086    auto *CR = TC->getConceptReference();2087    Record.push_back(CR != nullptr);2088    if (CR)2089      Record.AddConceptReference(CR);2090    Record.AddStmt(TC->getImmediatelyDeclaredConstraint());2091    Record.writeUnsignedOrNone(TC->getArgPackSubstIndex());2092    Record.writeUnsignedOrNone(D->getNumExpansionParameters());2093  }2094 2095  bool OwnsDefaultArg = D->hasDefaultArgument() &&2096                        !D->defaultArgumentWasInherited();2097  Record.push_back(OwnsDefaultArg);2098  if (OwnsDefaultArg)2099    Record.AddTemplateArgumentLoc(D->getDefaultArgument());2100 2101  if (!D->hasTypeConstraint() && !OwnsDefaultArg &&2102      D->getDeclContext() == D->getLexicalDeclContext() &&2103      !D->isInvalidDecl() && !D->hasAttrs() &&2104      !D->isTopLevelDeclInObjCContainer() && !D->isImplicit() &&2105      D->getDeclName().getNameKind() == DeclarationName::Identifier)2106    AbbrevToUse = Writer.getDeclTemplateTypeParmAbbrev();2107 2108  Code = serialization::DECL_TEMPLATE_TYPE_PARM;2109}2110 2111void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {2112  // For an expanded parameter pack, record the number of expansion types here2113  // so that it's easier for deserialization to allocate the right amount of2114  // memory.2115  Record.push_back(D->hasPlaceholderTypeConstraint());2116  if (D->isExpandedParameterPack())2117    Record.push_back(D->getNumExpansionTypes());2118 2119  VisitDeclaratorDecl(D);2120  // TemplateParmPosition.2121  Record.push_back(D->getDepth());2122  Record.push_back(D->getPosition());2123 2124  if (D->hasPlaceholderTypeConstraint())2125    Record.AddStmt(D->getPlaceholderTypeConstraint());2126 2127  if (D->isExpandedParameterPack()) {2128    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {2129      Record.AddTypeRef(D->getExpansionType(I));2130      Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));2131    }2132 2133    Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;2134  } else {2135    // Rest of NonTypeTemplateParmDecl.2136    Record.push_back(D->isParameterPack());2137    bool OwnsDefaultArg = D->hasDefaultArgument() &&2138                          !D->defaultArgumentWasInherited();2139    Record.push_back(OwnsDefaultArg);2140    if (OwnsDefaultArg)2141      Record.AddTemplateArgumentLoc(D->getDefaultArgument());2142    Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;2143  }2144}2145 2146void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {2147  // For an expanded parameter pack, record the number of expansion types here2148  // so that it's easier for deserialization to allocate the right amount of2149  // memory.2150  if (D->isExpandedParameterPack())2151    Record.push_back(D->getNumExpansionTemplateParameters());2152 2153  VisitTemplateDecl(D);2154  Record.push_back(D->templateParameterKind());2155  Record.push_back(D->wasDeclaredWithTypename());2156  // TemplateParmPosition.2157  Record.push_back(D->getDepth());2158  Record.push_back(D->getPosition());2159 2160  if (D->isExpandedParameterPack()) {2161    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();2162         I != N; ++I)2163      Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));2164    Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;2165  } else {2166    // Rest of TemplateTemplateParmDecl.2167    Record.push_back(D->isParameterPack());2168    bool OwnsDefaultArg = D->hasDefaultArgument() &&2169                          !D->defaultArgumentWasInherited();2170    Record.push_back(OwnsDefaultArg);2171    if (OwnsDefaultArg)2172      Record.AddTemplateArgumentLoc(D->getDefaultArgument());2173    Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;2174  }2175}2176 2177void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {2178  VisitRedeclarableTemplateDecl(D);2179  Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;2180}2181 2182void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {2183  VisitDecl(D);2184  Record.AddStmt(D->getAssertExpr());2185  Record.push_back(D->isFailed());2186  Record.AddStmt(D->getMessage());2187  Record.AddSourceLocation(D->getRParenLoc());2188  Code = serialization::DECL_STATIC_ASSERT;2189}2190 2191/// Emit the DeclContext part of a declaration context decl.2192void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {2193  static_assert(DeclContext::NumDeclContextBits == 13,2194                "You need to update the serializer after you change the "2195                "DeclContextBits");2196  LookupBlockOffsets Offsets;2197 2198  if (Writer.isGeneratingReducedBMI() && isa<NamespaceDecl>(DC) &&2199      cast<NamespaceDecl>(DC)->isFromExplicitGlobalModule()) {2200    // In reduced BMI, delay writing lexical and visible block for namespace2201    // in the global module fragment. See the comments of DelayedNamespace for2202    // details.2203    Writer.DelayedNamespace.push_back(cast<NamespaceDecl>(DC));2204  } else {2205    Offsets.LexicalOffset =2206        Writer.WriteDeclContextLexicalBlock(Record.getASTContext(), DC);2207    Writer.WriteDeclContextVisibleBlock(Record.getASTContext(), DC, Offsets);2208  }2209 2210  Record.AddLookupOffsets(Offsets);2211}2212 2213const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {2214  assert(IsLocalDecl(D) && "expected a local declaration");2215 2216  const Decl *Canon = D->getCanonicalDecl();2217  if (IsLocalDecl(Canon))2218    return Canon;2219 2220  const Decl *&CacheEntry = FirstLocalDeclCache[Canon];2221  if (CacheEntry)2222    return CacheEntry;2223 2224  for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())2225    if (IsLocalDecl(Redecl))2226      D = Redecl;2227  return CacheEntry = D;2228}2229 2230template <typename T>2231void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {2232  T *First = D->getFirstDecl();2233  T *MostRecent = First->getMostRecentDecl();2234  T *DAsT = static_cast<T *>(D);2235  if (MostRecent != First) {2236    assert(isRedeclarableDeclKind(DAsT->getKind()) &&2237           "Not considered redeclarable?");2238 2239    Record.AddDeclRef(First);2240 2241    // Write out a list of local redeclarations of this declaration if it's the2242    // first local declaration in the chain.2243    const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);2244    if (DAsT == FirstLocal) {2245      // Emit a list of all imported first declarations so that we can be sure2246      // that all redeclarations visible to this module are before D in the2247      // redecl chain.2248      unsigned I = Record.size();2249      Record.push_back(0);2250      if (Writer.Chain)2251        AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);2252      // This is the number of imported first declarations + 1.2253      Record[I] = Record.size() - I;2254 2255      // Collect the set of local redeclarations of this declaration, from2256      // newest to oldest.2257      ASTWriter::RecordData LocalRedecls;2258      ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);2259      for (const Decl *Prev = FirstLocal->getMostRecentDecl();2260           Prev != FirstLocal; Prev = Prev->getPreviousDecl())2261        if (!Prev->isFromASTFile())2262          LocalRedeclWriter.AddDeclRef(Prev);2263 2264      // If we have any redecls, write them now as a separate record preceding2265      // the declaration itself.2266      if (LocalRedecls.empty())2267        Record.push_back(0);2268      else2269        Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));2270    } else {2271      Record.push_back(0);2272      Record.AddDeclRef(FirstLocal);2273    }2274 2275    // Make sure that we serialize both the previous and the most-recent2276    // declarations, which (transitively) ensures that all declarations in the2277    // chain get serialized.2278    //2279    // FIXME: This is not correct; when we reach an imported declaration we2280    // won't emit its previous declaration.2281    (void)Writer.GetDeclRef(D->getPreviousDecl());2282    (void)Writer.GetDeclRef(MostRecent);2283  } else {2284    // We use the sentinel value 0 to indicate an only declaration.2285    Record.push_back(0);2286  }2287}2288 2289void ASTDeclWriter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {2290  VisitNamedDecl(D);2291  VisitDeclContext(D);2292  Record.push_back(D->isCBuffer());2293  Record.AddSourceLocation(D->getLocStart());2294  Record.AddSourceLocation(D->getLBraceLoc());2295  Record.AddSourceLocation(D->getRBraceLoc());2296 2297  Code = serialization::DECL_HLSL_BUFFER;2298}2299 2300void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {2301  Record.writeOMPChildren(D->Data);2302  VisitDecl(D);2303  Code = serialization::DECL_OMP_THREADPRIVATE;2304}2305 2306void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {2307  Record.writeOMPChildren(D->Data);2308  VisitDecl(D);2309  Code = serialization::DECL_OMP_ALLOCATE;2310}2311 2312void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {2313  Record.writeOMPChildren(D->Data);2314  VisitDecl(D);2315  Code = serialization::DECL_OMP_REQUIRES;2316}2317 2318void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {2319  static_assert(DeclContext::NumOMPDeclareReductionDeclBits == 15,2320                "You need to update the serializer after you change the "2321                "NumOMPDeclareReductionDeclBits");2322 2323  VisitValueDecl(D);2324  Record.AddSourceLocation(D->getBeginLoc());2325  Record.AddStmt(D->getCombinerIn());2326  Record.AddStmt(D->getCombinerOut());2327  Record.AddStmt(D->getCombiner());2328  Record.AddStmt(D->getInitOrig());2329  Record.AddStmt(D->getInitPriv());2330  Record.AddStmt(D->getInitializer());2331  Record.push_back(llvm::to_underlying(D->getInitializerKind()));2332  Record.AddDeclRef(D->getPrevDeclInScope());2333  Code = serialization::DECL_OMP_DECLARE_REDUCTION;2334}2335 2336void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {2337  Record.writeOMPChildren(D->Data);2338  VisitValueDecl(D);2339  Record.AddDeclarationName(D->getVarName());2340  Record.AddDeclRef(D->getPrevDeclInScope());2341  Code = serialization::DECL_OMP_DECLARE_MAPPER;2342}2343 2344void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {2345  VisitVarDecl(D);2346  Code = serialization::DECL_OMP_CAPTUREDEXPR;2347}2348 2349void ASTDeclWriter::VisitOpenACCDeclareDecl(OpenACCDeclareDecl *D) {2350  Record.writeUInt32(D->clauses().size());2351  VisitDecl(D);2352  Record.writeEnum(D->DirKind);2353  Record.AddSourceLocation(D->DirectiveLoc);2354  Record.AddSourceLocation(D->EndLoc);2355  Record.writeOpenACCClauseList(D->clauses());2356  Code = serialization::DECL_OPENACC_DECLARE;2357}2358void ASTDeclWriter::VisitOpenACCRoutineDecl(OpenACCRoutineDecl *D) {2359  Record.writeUInt32(D->clauses().size());2360  VisitDecl(D);2361  Record.writeEnum(D->DirKind);2362  Record.AddSourceLocation(D->DirectiveLoc);2363  Record.AddSourceLocation(D->EndLoc);2364  Record.AddSourceRange(D->ParensLoc);2365  Record.AddStmt(D->FuncRef);2366  Record.writeOpenACCClauseList(D->clauses());2367  Code = serialization::DECL_OPENACC_ROUTINE;2368}2369 2370//===----------------------------------------------------------------------===//2371// ASTWriter Implementation2372//===----------------------------------------------------------------------===//2373 2374namespace {2375template <FunctionDecl::TemplatedKind Kind>2376std::shared_ptr<llvm::BitCodeAbbrev>2377getFunctionDeclAbbrev(serialization::DeclCode Code) {2378  using namespace llvm;2379 2380  auto Abv = std::make_shared<BitCodeAbbrev>();2381  Abv->Add(BitCodeAbbrevOp(Code));2382  // RedeclarableDecl2383  Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl2384  Abv->Add(BitCodeAbbrevOp(Kind));2385  if constexpr (Kind == FunctionDecl::TK_NonTemplate) {2386 2387  } else if constexpr (Kind == FunctionDecl::TK_FunctionTemplate) {2388    // DescribedFunctionTemplate2389    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2390  } else if constexpr (Kind == FunctionDecl::TK_DependentNonTemplate) {2391    // Instantiated From Decl2392    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2393  } else if constexpr (Kind == FunctionDecl::TK_MemberSpecialization) {2394    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedFrom2395    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2396                             3)); // TemplateSpecializationKind2397    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Specialized Location2398  } else if constexpr (Kind ==2399                       FunctionDecl::TK_FunctionTemplateSpecialization) {2400    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template2401    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2402                             3)); // TemplateSpecializationKind2403    Abv->Add(BitCodeAbbrevOp(1)); // Template Argument Size2404    Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); // Template Argument Kind2405    Abv->Add(2406        BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template Argument Type2407    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is Defaulted2408    Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten2409    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2410    Abv->Add(BitCodeAbbrevOp(0));2411    Abv->Add(2412        BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Canonical Decl of template2413  } else if constexpr (Kind == FunctionDecl::2414                                   TK_DependentFunctionTemplateSpecialization) {2415    // Candidates of specialization2416    Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2417    Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten2418  } else {2419    llvm_unreachable("Unknown templated kind?");2420  }2421  // Decl2422  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2423                           8)); // Packed DeclBits: ModuleOwnershipKind,2424                                // isUsed, isReferenced,  AccessSpecifier,2425                                // isImplicit2426                                //2427                                // The following bits should be 0:2428                                // HasStandaloneLexicalDC, HasAttrs,2429                                // TopLevelDeclInObjCContainer,2430                                // isInvalidDecl2431  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2432  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2433  // NamedDecl2434  Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind2435  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));     // Identifier2436  Abv->Add(BitCodeAbbrevOp(0));                           // AnonDeclNumber2437  // ValueDecl2438  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2439  // DeclaratorDecl2440  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerLocStart2441  Abv->Add(BitCodeAbbrevOp(0));                       // HasExtInfo2442  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2443  // FunctionDecl2444  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS2445  Abv->Add(BitCodeAbbrevOp(2446      BitCodeAbbrevOp::Fixed,2447      28)); // Packed Function Bits: StorageClass, Inline, InlineSpecified,2448            // VirtualAsWritten, Pure, HasInheritedProto, HasWrittenProto,2449            // Deleted, Trivial, TrivialForCall, Defaulted, ExplicitlyDefaulted,2450            // IsIneligibleOrNotSelected, ImplicitReturnZero, Constexpr,2451            // UsesSEHTry, SkippedBody, MultiVersion, LateParsed,2452            // FriendConstraintRefersToEnclosingTemplate, Linkage,2453            // ShouldSkipCheckingODR2454  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // LocEnd2455  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash2456  // This Array slurps the rest of the record. Fortunately we want to encode2457  // (nearly) all the remaining (variable number of) fields in the same way.2458  //2459  // This is:2460  //         NumParams and Params[] from FunctionDecl, and2461  //         NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.2462  //2463  //  Add an AbbrevOp for 'size then elements' and use it here.2464  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2465  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2466  return Abv;2467}2468 2469template <FunctionDecl::TemplatedKind Kind>2470std::shared_ptr<llvm::BitCodeAbbrev> getCXXMethodAbbrev() {2471  return getFunctionDeclAbbrev<Kind>(serialization::DECL_CXX_METHOD);2472}2473} // namespace2474 2475void ASTWriter::WriteDeclAbbrevs() {2476  using namespace llvm;2477 2478  std::shared_ptr<BitCodeAbbrev> Abv;2479 2480  // Abbreviation for DECL_FIELD2481  Abv = std::make_shared<BitCodeAbbrev>();2482  Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));2483  // Decl2484  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2485                           7)); // Packed DeclBits: ModuleOwnershipKind,2486                                // isUsed, isReferenced,  AccessSpecifier,2487                                //2488                                // The following bits should be 0:2489                                // isImplicit, HasStandaloneLexicalDC, HasAttrs,2490                                // TopLevelDeclInObjCContainer,2491                                // isInvalidDecl2492  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2493  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2494  // NamedDecl2495  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2496  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2497  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2498  // ValueDecl2499  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2500  // DeclaratorDecl2501  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2502  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo2503  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2504  // FieldDecl2505  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable2506  Abv->Add(BitCodeAbbrevOp(0));                       // StorageKind2507  // Type Source Info2508  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2509  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2510  DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));2511 2512  // Abbreviation for DECL_OBJC_IVAR2513  Abv = std::make_shared<BitCodeAbbrev>();2514  Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));2515  // Decl2516  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2517                           12)); // Packed DeclBits: HasStandaloneLexicalDC,2518                                 // isInvalidDecl, HasAttrs, isImplicit, isUsed,2519                                 // isReferenced, TopLevelDeclInObjCContainer,2520                                 // AccessSpecifier, ModuleOwnershipKind2521  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2522  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2523  // NamedDecl2524  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2525  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2526  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2527  // ValueDecl2528  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2529  // DeclaratorDecl2530  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2531  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo2532  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2533  // FieldDecl2534  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable2535  Abv->Add(BitCodeAbbrevOp(0));                       // InitStyle2536  // ObjC Ivar2537  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl2538  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize2539  // Type Source Info2540  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2541  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2542  DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));2543 2544  // Abbreviation for DECL_ENUM2545  Abv = std::make_shared<BitCodeAbbrev>();2546  Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));2547  // Redeclarable2548  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration2549  // Decl2550  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2551                           7)); // Packed DeclBits: ModuleOwnershipKind,2552                                // isUsed, isReferenced,  AccessSpecifier,2553                                //2554                                // The following bits should be 0:2555                                // isImplicit, HasStandaloneLexicalDC, HasAttrs,2556                                // TopLevelDeclInObjCContainer,2557                                // isInvalidDecl2558  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2559  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2560  // NamedDecl2561  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2562  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2563  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2564  // TypeDecl2565  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2566  // TagDecl2567  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace2568  Abv->Add(BitCodeAbbrevOp(2569      BitCodeAbbrevOp::Fixed,2570      9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,2571           // EmbeddedInDeclarator, IsFreeStanding,2572           // isCompleteDefinitionRequired, ExtInfoKind2573  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation2574  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation2575  // EnumDecl2576  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddTypeRef2577  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IntegerType2578  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getPromotionType2579  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 20)); // Enum Decl Bits2580  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash2581  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // InstantiatedMembEnum2582  // DC2583  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset2584  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset2585  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // ModuleLocalOffset2586  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TULocalOffset2587  DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));2588 2589  // Abbreviation for DECL_RECORD2590  Abv = std::make_shared<BitCodeAbbrev>();2591  Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));2592  // Redeclarable2593  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration2594  // Decl2595  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2596                           7)); // Packed DeclBits: ModuleOwnershipKind,2597                                // isUsed, isReferenced,  AccessSpecifier,2598                                //2599                                // The following bits should be 0:2600                                // isImplicit, HasStandaloneLexicalDC, HasAttrs,2601                                // TopLevelDeclInObjCContainer,2602                                // isInvalidDecl2603  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2604  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2605  // NamedDecl2606  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2607  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2608  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2609  // TypeDecl2610  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2611  // TagDecl2612  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace2613  Abv->Add(BitCodeAbbrevOp(2614      BitCodeAbbrevOp::Fixed,2615      9)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,2616           // EmbeddedInDeclarator, IsFreeStanding,2617           // isCompleteDefinitionRequired, ExtInfoKind2618  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation2619  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation2620  // RecordDecl2621  Abv->Add(BitCodeAbbrevOp(2622      BitCodeAbbrevOp::Fixed,2623      14)); // Packed Record Decl Bits: FlexibleArrayMember,2624            // AnonymousStructUnion, hasObjectMember, hasVolatileMember,2625            // isNonTrivialToPrimitiveDefaultInitialize,2626            // isNonTrivialToPrimitiveCopy, isNonTrivialToPrimitiveDestroy,2627            // hasNonTrivialToPrimitiveDefaultInitializeCUnion,2628            // hasNonTrivialToPrimitiveDestructCUnion,2629            // hasNonTrivialToPrimitiveCopyCUnion,2630            // hasUninitializedExplicitInitFields, isParamDestroyedInCallee,2631            // getArgPassingRestrictions2632  // ODRHash2633  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26));2634 2635  // DC2636  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset2637  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset2638  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // ModuleLocalOffset2639  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TULocalOffset2640  DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));2641 2642  // Abbreviation for DECL_PARM_VAR2643  Abv = std::make_shared<BitCodeAbbrev>();2644  Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));2645  // Redeclarable2646  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration2647  // Decl2648  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2649                           8)); // Packed DeclBits: ModuleOwnershipKind, isUsed,2650                                // isReferenced, AccessSpecifier,2651                                // HasStandaloneLexicalDC, HasAttrs, isImplicit,2652                                // TopLevelDeclInObjCContainer,2653                                // isInvalidDecl,2654  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2655  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2656  // NamedDecl2657  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2658  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2659  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2660  // ValueDecl2661  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2662  // DeclaratorDecl2663  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2664  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo2665  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2666  // VarDecl2667  Abv->Add(2668      BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2669                      12)); // Packed Var Decl bits: SClass, TSCSpec, InitStyle,2670                            // isARCPseudoStrong, Linkage, ModulesCodegen2671  Abv->Add(BitCodeAbbrevOp(0));                          // VarKind (local enum)2672  // ParmVarDecl2673  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex2674  Abv->Add(BitCodeAbbrevOp(2675      BitCodeAbbrevOp::Fixed,2676      19)); // Packed Parm Var Decl bits: IsObjCMethodParameter, ScopeDepth,2677            // ObjCDeclQualifier, KNRPromoted,2678            // HasInheritedDefaultArg, HasUninstantiatedDefaultArg2679  // Type Source Info2680  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2681  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2682  DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));2683 2684  // Abbreviation for DECL_TYPEDEF2685  Abv = std::make_shared<BitCodeAbbrev>();2686  Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));2687  // Redeclarable2688  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration2689  // Decl2690  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2691                           7)); // Packed DeclBits: ModuleOwnershipKind,2692                                // isReferenced, isUsed, AccessSpecifier. Other2693                                // higher bits should be 0: isImplicit,2694                                // HasStandaloneLexicalDC, HasAttrs,2695                                // TopLevelDeclInObjCContainer, isInvalidDecl2696  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2697  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2698  // NamedDecl2699  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2700  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2701  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2702  // TypeDecl2703  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2704  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2705  // TypedefDecl2706  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2707  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2708  DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));2709 2710  // Abbreviation for DECL_VAR2711  Abv = std::make_shared<BitCodeAbbrev>();2712  Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));2713  // Redeclarable2714  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration2715  // Decl2716  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2717                           12)); // Packed DeclBits: HasStandaloneLexicalDC,2718                                 // isInvalidDecl, HasAttrs, isImplicit, isUsed,2719                                 // isReferenced, TopLevelDeclInObjCContainer,2720                                 // AccessSpecifier, ModuleOwnershipKind2721  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2722  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2723  // NamedDecl2724  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2725  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2726  Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber2727  // ValueDecl2728  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2729  // DeclaratorDecl2730  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2731  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo2732  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2733  // VarDecl2734  Abv->Add(BitCodeAbbrevOp(2735      BitCodeAbbrevOp::Fixed,2736      22)); // Packed Var Decl bits:  Linkage, ModulesCodegen,2737            // SClass, TSCSpec, InitStyle,2738            // isARCPseudoStrong, IsThisDeclarationADemotedDefinition,2739            // isExceptionVariable, isNRVOVariable, isCXXForRangeDecl,2740            // isInline, isInlineSpecified, isConstexpr,2741            // isInitCapture, isPrevDeclInSameScope, hasInitWithSideEffects,2742            // EscapingByref, HasDeducedType, ImplicitParamKind, isObjCForDecl2743            // IsCXXForRangeImplicitVar2744  Abv->Add(BitCodeAbbrevOp(0));                         // VarKind (local enum)2745  // Type Source Info2746  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2747  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2748  DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));2749 2750  // Abbreviation for DECL_CXX_METHOD2751  DeclCXXMethodAbbrev =2752      Stream.EmitAbbrev(getCXXMethodAbbrev<FunctionDecl::TK_NonTemplate>());2753  DeclTemplateCXXMethodAbbrev = Stream.EmitAbbrev(2754      getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplate>());2755  DeclDependentNonTemplateCXXMethodAbbrev = Stream.EmitAbbrev(2756      getCXXMethodAbbrev<FunctionDecl::TK_DependentNonTemplate>());2757  DeclMemberSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(2758      getCXXMethodAbbrev<FunctionDecl::TK_MemberSpecialization>());2759  DeclTemplateSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(2760      getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplateSpecialization>());2761  DeclDependentSpecializationCXXMethodAbbrev = Stream.EmitAbbrev(2762      getCXXMethodAbbrev<2763          FunctionDecl::TK_DependentFunctionTemplateSpecialization>());2764 2765  // Abbreviation for DECL_TEMPLATE_TYPE_PARM2766  Abv = std::make_shared<BitCodeAbbrev>();2767  Abv->Add(BitCodeAbbrevOp(serialization::DECL_TEMPLATE_TYPE_PARM));2768  Abv->Add(BitCodeAbbrevOp(0)); // hasTypeConstraint2769  // Decl2770  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2771                           7)); // Packed DeclBits: ModuleOwnershipKind,2772                                // isReferenced, isUsed, AccessSpecifier. Other2773                                // higher bits should be 0: isImplicit,2774                                // HasStandaloneLexicalDC, HasAttrs,2775                                // TopLevelDeclInObjCContainer, isInvalidDecl2776  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2777  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2778  // NamedDecl2779  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2780  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2781  Abv->Add(BitCodeAbbrevOp(0));2782  // TypeDecl2783  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2784  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2785  // TemplateTypeParmDecl2786  Abv->Add(2787      BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // wasDeclaredWithTypename2788  Abv->Add(BitCodeAbbrevOp(0));                    // OwnsDefaultArg2789  DeclTemplateTypeParmAbbrev = Stream.EmitAbbrev(std::move(Abv));2790 2791  // Abbreviation for DECL_USING_SHADOW2792  Abv = std::make_shared<BitCodeAbbrev>();2793  Abv->Add(BitCodeAbbrevOp(serialization::DECL_USING_SHADOW));2794  // Redeclarable2795  Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2796  // Decl2797  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,2798                           12)); // Packed DeclBits: HasStandaloneLexicalDC,2799                                 // isInvalidDecl, HasAttrs, isImplicit, isUsed,2800                                 // isReferenced, TopLevelDeclInObjCContainer,2801                                 // AccessSpecifier, ModuleOwnershipKind2802  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2803  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2804  // NamedDecl2805  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier2806  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2807  Abv->Add(BitCodeAbbrevOp(0));2808  // UsingShadowDecl2809  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // TargetDecl2810  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS2811  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));    // UsingOrNextShadow2812  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR,2813                           6)); // InstantiatedFromUsingShadowDecl2814  DeclUsingShadowAbbrev = Stream.EmitAbbrev(std::move(Abv));2815 2816  // Abbreviation for EXPR_DECL_REF2817  Abv = std::make_shared<BitCodeAbbrev>();2818  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));2819  // Stmt2820  //  Expr2821  //  PackingBits: DependenceKind, ValueKind. ObjectKind should be 0.2822  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));2823  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2824  // DeclRefExpr2825  // Packing Bits: , HadMultipleCandidates, RefersToEnclosingVariableOrCapture,2826  // IsImmediateEscalating, NonOdrUseReason.2827  // GetDeclFound, HasQualifier and ExplicitTemplateArgs should be 0.2828  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2829  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef2830  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2831  DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));2832 2833  // Abbreviation for EXPR_INTEGER_LITERAL2834  Abv = std::make_shared<BitCodeAbbrev>();2835  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));2836  //Stmt2837  // Expr2838  // DependenceKind, ValueKind, ObjectKind2839  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2840  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2841  // Integer Literal2842  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2843  Abv->Add(BitCodeAbbrevOp(32));                      // Bit Width2844  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value2845  IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));2846 2847  // Abbreviation for EXPR_CHARACTER_LITERAL2848  Abv = std::make_shared<BitCodeAbbrev>();2849  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));2850  //Stmt2851  // Expr2852  // DependenceKind, ValueKind, ObjectKind2853  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2854  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2855  // Character Literal2856  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue2857  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2858  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind2859  CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));2860 2861  // Abbreviation for EXPR_IMPLICIT_CAST2862  Abv = std::make_shared<BitCodeAbbrev>();2863  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));2864  // Stmt2865  // Expr2866  // Packing Bits: DependenceKind, ValueKind, ObjectKind,2867  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2868  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2869  // CastExpr2870  Abv->Add(BitCodeAbbrevOp(0)); // PathSize2871  // Packing Bits: CastKind, StoredFPFeatures, isPartOfExplicitCast2872  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 9));2873  // ImplicitCastExpr2874  ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));2875 2876  // Abbreviation for EXPR_BINARY_OPERATOR2877  Abv = std::make_shared<BitCodeAbbrev>();2878  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_BINARY_OPERATOR));2879  // Stmt2880  // Expr2881  // Packing Bits: DependenceKind. ValueKind and ObjectKind should2882  // be 0 in this case.2883  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2884  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2885  // BinaryOperator2886  Abv->Add(2887      BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures2888  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2889  BinaryOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));2890 2891  // Abbreviation for EXPR_COMPOUND_ASSIGN_OPERATOR2892  Abv = std::make_shared<BitCodeAbbrev>();2893  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_COMPOUND_ASSIGN_OPERATOR));2894  // Stmt2895  // Expr2896  // Packing Bits: DependenceKind. ValueKind and ObjectKind should2897  // be 0 in this case.2898  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2899  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2900  // BinaryOperator2901  // Packing Bits: OpCode. The HasFPFeatures bit should be 02902  Abv->Add(2903      BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures2904  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2905  // CompoundAssignOperator2906  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHSType2907  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Result Type2908  CompoundAssignOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));2909 2910  // Abbreviation for EXPR_CALL2911  Abv = std::make_shared<BitCodeAbbrev>();2912  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CALL));2913  // Stmt2914  // Expr2915  // Packing Bits: DependenceKind, ValueKind, ObjectKind,2916  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2917  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2918  // CallExpr2919  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2920  Abv->Add(BitCodeAbbrevOp(0));                       // ADLCallKind2921  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2922  CallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));2923 2924  // Abbreviation for EXPR_CXX_OPERATOR_CALL2925  Abv = std::make_shared<BitCodeAbbrev>();2926  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_OPERATOR_CALL));2927  // Stmt2928  // Expr2929  // Packing Bits: DependenceKind, ValueKind, ObjectKind,2930  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2931  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2932  // CallExpr2933  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2934  Abv->Add(BitCodeAbbrevOp(0));                       // ADLCallKind2935  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2936  // CXXOperatorCallExpr2937  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Operator Kind2938  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2939  CXXOperatorCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));2940 2941  // Abbreviation for EXPR_CXX_MEMBER_CALL2942  Abv = std::make_shared<BitCodeAbbrev>();2943  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_MEMBER_CALL));2944  // Stmt2945  // Expr2946  // Packing Bits: DependenceKind, ValueKind, ObjectKind,2947  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2948  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2949  // CallExpr2950  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2951  Abv->Add(BitCodeAbbrevOp(0));                       // ADLCallKind2952  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2953  // CXXMemberCallExpr2954  CXXMemberCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));2955 2956  // Abbreviation for STMT_COMPOUND2957  Abv = std::make_shared<BitCodeAbbrev>();2958  Abv->Add(BitCodeAbbrevOp(serialization::STMT_COMPOUND));2959  // Stmt2960  // CompoundStmt2961  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Num Stmts2962  Abv->Add(BitCodeAbbrevOp(0));                       // hasStoredFPFeatures2963  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2964  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2965  CompoundStmtAbbrev = Stream.EmitAbbrev(std::move(Abv));2966 2967  Abv = std::make_shared<BitCodeAbbrev>();2968  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));2969  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2970  DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));2971 2972  Abv = std::make_shared<BitCodeAbbrev>();2973  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));2974  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2975  DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));2976 2977  Abv = std::make_shared<BitCodeAbbrev>();2978  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_MODULE_LOCAL_VISIBLE));2979  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2980  DeclModuleLocalVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));2981 2982  Abv = std::make_shared<BitCodeAbbrev>();2983  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_TU_LOCAL_VISIBLE));2984  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2985  DeclTULocalLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));2986 2987  Abv = std::make_shared<BitCodeAbbrev>();2988  Abv->Add(BitCodeAbbrevOp(serialization::DECL_SPECIALIZATIONS));2989  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2990  DeclSpecializationsAbbrev = Stream.EmitAbbrev(std::move(Abv));2991 2992  Abv = std::make_shared<BitCodeAbbrev>();2993  Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARTIAL_SPECIALIZATIONS));2994  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2995  DeclPartialSpecializationsAbbrev = Stream.EmitAbbrev(std::move(Abv));2996}2997 2998/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by2999/// consumers of the AST.3000///3001/// Such decls will always be deserialized from the AST file, so we would like3002/// this to be as restrictive as possible. Currently the predicate is driven by3003/// code generation requirements, if other clients have a different notion of3004/// what is "required" then we may have to consider an alternate scheme where3005/// clients can iterate over the top-level decls and get information on them,3006/// without necessary deserializing them. We could explicitly require such3007/// clients to use a separate API call to "realize" the decl. This should be3008/// relatively painless since they would presumably only do it for top-level3009/// decls.3010static bool isRequiredDecl(const Decl *D, ASTContext &Context,3011                           Module *WritingModule) {3012  // Named modules have different semantics than header modules. Every named3013  // module units owns a translation unit. So the importer of named modules3014  // doesn't need to deserilize everything ahead of time.3015  if (WritingModule && WritingModule->isNamedModule()) {3016    // The PragmaCommentDecl and PragmaDetectMismatchDecl are MSVC's extension.3017    // And the behavior of MSVC for such cases will leak this to the module3018    // users. Given pragma is not a standard thing, the compiler has the space3019    // to do their own decision. Let's follow MSVC here.3020    if (isa<PragmaCommentDecl, PragmaDetectMismatchDecl>(D))3021      return true;3022    return false;3023  }3024 3025  // An ObjCMethodDecl is never considered as "required" because its3026  // implementation container always is.3027 3028  // File scoped assembly or obj-c or OMP declare target implementation must be3029  // seen.3030  if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCImplDecl>(D))3031    return true;3032 3033  if (WritingModule && isPartOfPerModuleInitializer(D)) {3034    // These declarations are part of the module initializer, and are emitted3035    // if and when the module is imported, rather than being emitted eagerly.3036    return false;3037  }3038 3039  return Context.DeclMustBeEmitted(D);3040}3041 3042void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {3043  PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),3044                                      "serializing");3045 3046  // Determine the ID for this declaration.3047  LocalDeclID ID;3048  assert(!D->isFromASTFile() && "should not be emitting imported decl");3049  LocalDeclID &IDR = DeclIDs[D];3050  if (IDR.isInvalid())3051    IDR = NextDeclID++;3052 3053  ID = IDR;3054 3055  assert(ID >= FirstDeclID && "invalid decl ID");3056 3057  RecordData Record;3058  ASTDeclWriter W(*this, Context, Record, GeneratingReducedBMI);3059 3060  // Build a record for this declaration3061  W.Visit(D);3062 3063  // Emit this declaration to the bitstream.3064  uint64_t Offset = W.Emit(D);3065 3066  // Record the offset for this declaration3067  SourceLocation Loc = D->getLocation();3068  SourceLocationEncoding::RawLocEncoding RawLoc =3069      getRawSourceLocationEncoding(getAdjustedLocation(Loc));3070 3071  unsigned Index = ID.getRawValue() - FirstDeclID.getRawValue();3072  if (DeclOffsets.size() == Index)3073    DeclOffsets.emplace_back(RawLoc, Offset, DeclTypesBlockStartOffset);3074  else if (DeclOffsets.size() < Index) {3075    // FIXME: Can/should this happen?3076    DeclOffsets.resize(Index+1);3077    DeclOffsets[Index].setRawLoc(RawLoc);3078    DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);3079  } else {3080    llvm_unreachable("declarations should be emitted in ID order");3081  }3082 3083  SourceManager &SM = Context.getSourceManager();3084  if (Loc.isValid() && SM.isLocalSourceLocation(Loc))3085    associateDeclWithFile(D, ID);3086 3087  // Note declarations that should be deserialized eagerly so that we can add3088  // them to a record in the AST file later.3089  if (isRequiredDecl(D, Context, WritingModule))3090    AddDeclRef(D, EagerlyDeserializedDecls);3091}3092 3093void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {3094  // Switch case IDs are per function body.3095  Writer->ClearSwitchCaseIDs();3096 3097  assert(FD->doesThisDeclarationHaveABody());3098  bool ModulesCodegen = shouldFunctionGenerateHereOnly(FD);3099  Record->push_back(ModulesCodegen);3100  if (ModulesCodegen)3101    Writer->AddDeclRef(FD, Writer->ModularCodegenDecls);3102  if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {3103    Record->push_back(CD->getNumCtorInitializers());3104    if (CD->getNumCtorInitializers())3105      AddCXXCtorInitializers(llvm::ArrayRef(CD->init_begin(), CD->init_end()));3106  }3107  AddStmt(FD->getBody());3108}3109