Path: blob/main/contrib/llvm-project/clang/lib/Serialization/ASTWriterDecl.cpp
35234 views
//===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7//8// This file implements serialization for Declarations.9//10//===----------------------------------------------------------------------===//1112#include "ASTCommon.h"13#include "clang/AST/Attr.h"14#include "clang/AST/DeclCXX.h"15#include "clang/AST/DeclTemplate.h"16#include "clang/AST/DeclVisitor.h"17#include "clang/AST/Expr.h"18#include "clang/AST/OpenMPClause.h"19#include "clang/AST/PrettyDeclStackTrace.h"20#include "clang/Basic/SourceManager.h"21#include "clang/Serialization/ASTReader.h"22#include "clang/Serialization/ASTRecordWriter.h"23#include "llvm/Bitstream/BitstreamWriter.h"24#include "llvm/Support/ErrorHandling.h"25#include <optional>26using namespace clang;27using namespace serialization;2829//===----------------------------------------------------------------------===//30// Declaration serialization31//===----------------------------------------------------------------------===//3233namespace clang {34class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {35ASTWriter &Writer;36ASTContext &Context;37ASTRecordWriter Record;3839serialization::DeclCode Code;40unsigned AbbrevToUse;4142bool GeneratingReducedBMI = false;4344public:45ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,46ASTWriter::RecordDataImpl &Record, bool GeneratingReducedBMI)47: Writer(Writer), Context(Context), Record(Writer, Record),48Code((serialization::DeclCode)0), AbbrevToUse(0),49GeneratingReducedBMI(GeneratingReducedBMI) {}5051uint64_t Emit(Decl *D) {52if (!Code)53llvm::report_fatal_error(StringRef("unexpected declaration kind '") +54D->getDeclKindName() + "'");55return Record.Emit(Code, AbbrevToUse);56}5758void Visit(Decl *D);5960void VisitDecl(Decl *D);61void VisitPragmaCommentDecl(PragmaCommentDecl *D);62void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);63void VisitTranslationUnitDecl(TranslationUnitDecl *D);64void VisitNamedDecl(NamedDecl *D);65void VisitLabelDecl(LabelDecl *LD);66void VisitNamespaceDecl(NamespaceDecl *D);67void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);68void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);69void VisitTypeDecl(TypeDecl *D);70void VisitTypedefNameDecl(TypedefNameDecl *D);71void VisitTypedefDecl(TypedefDecl *D);72void VisitTypeAliasDecl(TypeAliasDecl *D);73void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);74void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);75void VisitTagDecl(TagDecl *D);76void VisitEnumDecl(EnumDecl *D);77void VisitRecordDecl(RecordDecl *D);78void VisitCXXRecordDecl(CXXRecordDecl *D);79void VisitClassTemplateSpecializationDecl(80ClassTemplateSpecializationDecl *D);81void VisitClassTemplatePartialSpecializationDecl(82ClassTemplatePartialSpecializationDecl *D);83void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);84void VisitVarTemplatePartialSpecializationDecl(85VarTemplatePartialSpecializationDecl *D);86void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);87void VisitValueDecl(ValueDecl *D);88void VisitEnumConstantDecl(EnumConstantDecl *D);89void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);90void VisitDeclaratorDecl(DeclaratorDecl *D);91void VisitFunctionDecl(FunctionDecl *D);92void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);93void VisitCXXMethodDecl(CXXMethodDecl *D);94void VisitCXXConstructorDecl(CXXConstructorDecl *D);95void VisitCXXDestructorDecl(CXXDestructorDecl *D);96void VisitCXXConversionDecl(CXXConversionDecl *D);97void VisitFieldDecl(FieldDecl *D);98void VisitMSPropertyDecl(MSPropertyDecl *D);99void VisitMSGuidDecl(MSGuidDecl *D);100void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);101void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);102void VisitIndirectFieldDecl(IndirectFieldDecl *D);103void VisitVarDecl(VarDecl *D);104void VisitImplicitParamDecl(ImplicitParamDecl *D);105void VisitParmVarDecl(ParmVarDecl *D);106void VisitDecompositionDecl(DecompositionDecl *D);107void VisitBindingDecl(BindingDecl *D);108void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);109void VisitTemplateDecl(TemplateDecl *D);110void VisitConceptDecl(ConceptDecl *D);111void VisitImplicitConceptSpecializationDecl(112ImplicitConceptSpecializationDecl *D);113void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);114void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);115void VisitClassTemplateDecl(ClassTemplateDecl *D);116void VisitVarTemplateDecl(VarTemplateDecl *D);117void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);118void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);119void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);120void VisitUsingDecl(UsingDecl *D);121void VisitUsingEnumDecl(UsingEnumDecl *D);122void VisitUsingPackDecl(UsingPackDecl *D);123void VisitUsingShadowDecl(UsingShadowDecl *D);124void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);125void VisitLinkageSpecDecl(LinkageSpecDecl *D);126void VisitExportDecl(ExportDecl *D);127void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);128void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);129void VisitImportDecl(ImportDecl *D);130void VisitAccessSpecDecl(AccessSpecDecl *D);131void VisitFriendDecl(FriendDecl *D);132void VisitFriendTemplateDecl(FriendTemplateDecl *D);133void VisitStaticAssertDecl(StaticAssertDecl *D);134void VisitBlockDecl(BlockDecl *D);135void VisitCapturedDecl(CapturedDecl *D);136void VisitEmptyDecl(EmptyDecl *D);137void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);138void VisitDeclContext(DeclContext *DC);139template <typename T> void VisitRedeclarable(Redeclarable<T> *D);140void VisitHLSLBufferDecl(HLSLBufferDecl *D);141142// FIXME: Put in the same order is DeclNodes.td?143void VisitObjCMethodDecl(ObjCMethodDecl *D);144void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);145void VisitObjCContainerDecl(ObjCContainerDecl *D);146void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);147void VisitObjCIvarDecl(ObjCIvarDecl *D);148void VisitObjCProtocolDecl(ObjCProtocolDecl *D);149void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);150void VisitObjCCategoryDecl(ObjCCategoryDecl *D);151void VisitObjCImplDecl(ObjCImplDecl *D);152void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);153void VisitObjCImplementationDecl(ObjCImplementationDecl *D);154void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);155void VisitObjCPropertyDecl(ObjCPropertyDecl *D);156void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);157void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);158void VisitOMPAllocateDecl(OMPAllocateDecl *D);159void VisitOMPRequiresDecl(OMPRequiresDecl *D);160void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);161void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);162void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);163164/// Add an Objective-C type parameter list to the given record.165void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {166// Empty type parameter list.167if (!typeParams) {168Record.push_back(0);169return;170}171172Record.push_back(typeParams->size());173for (auto *typeParam : *typeParams) {174Record.AddDeclRef(typeParam);175}176Record.AddSourceLocation(typeParams->getLAngleLoc());177Record.AddSourceLocation(typeParams->getRAngleLoc());178}179180/// Add to the record the first declaration from each module file that181/// provides a declaration of D. The intent is to provide a sufficient182/// set such that reloading this set will load all current redeclarations.183void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {184llvm::MapVector<ModuleFile*, const Decl*> Firsts;185// FIXME: We can skip entries that we know are implied by others.186for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {187if (R->isFromASTFile())188Firsts[Writer.Chain->getOwningModuleFile(R)] = R;189else if (IncludeLocal)190Firsts[nullptr] = R;191}192for (const auto &F : Firsts)193Record.AddDeclRef(F.second);194}195196/// Get the specialization decl from an entry in the specialization list.197template <typename EntryType>198typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *199getSpecializationDecl(EntryType &T) {200return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);201}202203/// Get the list of partial specializations from a template's common ptr.204template<typename T>205decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {206return Common->PartialSpecializations;207}208ArrayRef<Decl> getPartialSpecializations(FunctionTemplateDecl::Common *) {209return std::nullopt;210}211212template<typename DeclTy>213void AddTemplateSpecializations(DeclTy *D) {214auto *Common = D->getCommonPtr();215216// If we have any lazy specializations, and the external AST source is217// our chained AST reader, we can just write out the DeclIDs. Otherwise,218// we need to resolve them to actual declarations.219if (Writer.Chain != Writer.Context->getExternalSource() &&220Common->LazySpecializations) {221D->LoadLazySpecializations();222assert(!Common->LazySpecializations);223}224225ArrayRef<GlobalDeclID> LazySpecializations;226if (auto *LS = Common->LazySpecializations)227LazySpecializations = llvm::ArrayRef(LS + 1, LS[0].getRawValue());228229// Add a slot to the record for the number of specializations.230unsigned I = Record.size();231Record.push_back(0);232233// AddFirstDeclFromEachModule might trigger deserialization, invalidating234// *Specializations iterators.235llvm::SmallVector<const Decl*, 16> Specs;236for (auto &Entry : Common->Specializations)237Specs.push_back(getSpecializationDecl(Entry));238for (auto &Entry : getPartialSpecializations(Common))239Specs.push_back(getSpecializationDecl(Entry));240241for (auto *D : Specs) {242assert(D->isCanonicalDecl() && "non-canonical decl in set");243AddFirstDeclFromEachModule(D, /*IncludeLocal*/true);244}245Record.append(246DeclIDIterator<GlobalDeclID, DeclID>(LazySpecializations.begin()),247DeclIDIterator<GlobalDeclID, DeclID>(LazySpecializations.end()));248249// Update the size entry we added earlier.250Record[I] = Record.size() - I - 1;251}252253/// Ensure that this template specialization is associated with the specified254/// template on reload.255void RegisterTemplateSpecialization(const Decl *Template,256const Decl *Specialization) {257Template = Template->getCanonicalDecl();258259// If the canonical template is local, we'll write out this specialization260// when we emit it.261// FIXME: We can do the same thing if there is any local declaration of262// the template, to avoid emitting an update record.263if (!Template->isFromASTFile())264return;265266// We only need to associate the first local declaration of the267// specialization. The other declarations will get pulled in by it.268if (Writer.getFirstLocalDecl(Specialization) != Specialization)269return;270271Writer.DeclUpdates[Template].push_back(ASTWriter::DeclUpdate(272UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, Specialization));273}274};275}276277bool clang::CanElideDeclDef(const Decl *D) {278if (auto *FD = dyn_cast<FunctionDecl>(D)) {279if (FD->isInlined() || FD->isConstexpr())280return false;281282if (FD->isDependentContext())283return false;284285if (FD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)286return false;287}288289if (auto *VD = dyn_cast<VarDecl>(D)) {290if (!VD->getDeclContext()->getRedeclContext()->isFileContext() ||291VD->isInline() || VD->isConstexpr() || isa<ParmVarDecl>(VD) ||292// Constant initialized variable may not affect the ABI, but they293// may be used in constant evaluation in the frontend, so we have294// to remain them.295VD->hasConstantInitialization())296return false;297298if (VD->getTemplateSpecializationKind() == TSK_ImplicitInstantiation)299return false;300}301302return true;303}304305void ASTDeclWriter::Visit(Decl *D) {306DeclVisitor<ASTDeclWriter>::Visit(D);307308// Source locations require array (variable-length) abbreviations. The309// abbreviation infrastructure requires that arrays are encoded last, so310// we handle it here in the case of those classes derived from DeclaratorDecl311if (auto *DD = dyn_cast<DeclaratorDecl>(D)) {312if (auto *TInfo = DD->getTypeSourceInfo())313Record.AddTypeLoc(TInfo->getTypeLoc());314}315316// Handle FunctionDecl's body here and write it after all other Stmts/Exprs317// have been written. We want it last because we will not read it back when318// retrieving it from the AST, we'll just lazily set the offset.319if (auto *FD = dyn_cast<FunctionDecl>(D)) {320if (!GeneratingReducedBMI || !CanElideDeclDef(FD)) {321Record.push_back(FD->doesThisDeclarationHaveABody());322if (FD->doesThisDeclarationHaveABody())323Record.AddFunctionDefinition(FD);324} else325Record.push_back(0);326}327328// Similar to FunctionDecls, handle VarDecl's initializer here and write it329// after all other Stmts/Exprs. We will not read the initializer until after330// we have finished recursive deserialization, because it can recursively331// refer back to the variable.332if (auto *VD = dyn_cast<VarDecl>(D)) {333if (!GeneratingReducedBMI || !CanElideDeclDef(VD))334Record.AddVarDeclInit(VD);335else336Record.push_back(0);337}338339// And similarly for FieldDecls. We already serialized whether there is a340// default member initializer.341if (auto *FD = dyn_cast<FieldDecl>(D)) {342if (FD->hasInClassInitializer()) {343if (Expr *Init = FD->getInClassInitializer()) {344Record.push_back(1);345Record.AddStmt(Init);346} else {347Record.push_back(0);348// Initializer has not been instantiated yet.349}350}351}352353// If this declaration is also a DeclContext, write blocks for the354// declarations that lexically stored inside its context and those355// declarations that are visible from its context.356if (auto *DC = dyn_cast<DeclContext>(D))357VisitDeclContext(DC);358}359360void ASTDeclWriter::VisitDecl(Decl *D) {361BitsPacker DeclBits;362363// The order matters here. It will be better to put the bit with higher364// probability to be 0 in the end of the bits.365//366// Since we're using VBR6 format to store it.367// It will be pretty effient if all the higher bits are 0.368// For example, if we need to pack 8 bits into a value and the stored value369// is 0xf0, the actual stored value will be 0b000111'110000, which takes 12370// bits actually. However, if we changed the order to be 0x0f, then we can371// store it as 0b001111, which takes 6 bits only now.372DeclBits.addBits((uint64_t)D->getModuleOwnershipKind(), /*BitWidth=*/3);373DeclBits.addBit(D->isReferenced());374DeclBits.addBit(D->isUsed(false));375DeclBits.addBits(D->getAccess(), /*BitWidth=*/2);376DeclBits.addBit(D->isImplicit());377DeclBits.addBit(D->getDeclContext() != D->getLexicalDeclContext());378DeclBits.addBit(D->hasAttrs());379DeclBits.addBit(D->isTopLevelDeclInObjCContainer());380DeclBits.addBit(D->isInvalidDecl());381Record.push_back(DeclBits);382383Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));384if (D->getDeclContext() != D->getLexicalDeclContext())385Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));386387if (D->hasAttrs())388Record.AddAttributes(D->getAttrs());389390Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));391392// If this declaration injected a name into a context different from its393// lexical context, and that context is an imported namespace, we need to394// update its visible declarations to include this name.395//396// This happens when we instantiate a class with a friend declaration or a397// function with a local extern declaration, for instance.398//399// FIXME: Can we handle this in AddedVisibleDecl instead?400if (D->isOutOfLine()) {401auto *DC = D->getDeclContext();402while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {403if (!NS->isFromASTFile())404break;405Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());406if (!NS->isInlineNamespace())407break;408DC = NS->getParent();409}410}411}412413void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {414StringRef Arg = D->getArg();415Record.push_back(Arg.size());416VisitDecl(D);417Record.AddSourceLocation(D->getBeginLoc());418Record.push_back(D->getCommentKind());419Record.AddString(Arg);420Code = serialization::DECL_PRAGMA_COMMENT;421}422423void ASTDeclWriter::VisitPragmaDetectMismatchDecl(424PragmaDetectMismatchDecl *D) {425StringRef Name = D->getName();426StringRef Value = D->getValue();427Record.push_back(Name.size() + 1 + Value.size());428VisitDecl(D);429Record.AddSourceLocation(D->getBeginLoc());430Record.AddString(Name);431Record.AddString(Value);432Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;433}434435void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {436llvm_unreachable("Translation units aren't directly serialized");437}438439void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {440VisitDecl(D);441Record.AddDeclarationName(D->getDeclName());442Record.push_back(needsAnonymousDeclarationNumber(D)443? Writer.getAnonymousDeclarationNumber(D)444: 0);445}446447void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {448VisitNamedDecl(D);449Record.AddSourceLocation(D->getBeginLoc());450Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));451}452453void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {454VisitRedeclarable(D);455VisitTypeDecl(D);456Record.AddTypeSourceInfo(D->getTypeSourceInfo());457Record.push_back(D->isModed());458if (D->isModed())459Record.AddTypeRef(D->getUnderlyingType());460Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));461}462463void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {464VisitTypedefNameDecl(D);465if (D->getDeclContext() == D->getLexicalDeclContext() &&466!D->hasAttrs() &&467!D->isImplicit() &&468D->getFirstDecl() == D->getMostRecentDecl() &&469!D->isInvalidDecl() &&470!D->isTopLevelDeclInObjCContainer() &&471!D->isModulePrivate() &&472!needsAnonymousDeclarationNumber(D) &&473D->getDeclName().getNameKind() == DeclarationName::Identifier)474AbbrevToUse = Writer.getDeclTypedefAbbrev();475476Code = serialization::DECL_TYPEDEF;477}478479void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {480VisitTypedefNameDecl(D);481Record.AddDeclRef(D->getDescribedAliasTemplate());482Code = serialization::DECL_TYPEALIAS;483}484485void ASTDeclWriter::VisitTagDecl(TagDecl *D) {486static_assert(DeclContext::NumTagDeclBits == 23,487"You need to update the serializer after you change the "488"TagDeclBits");489490VisitRedeclarable(D);491VisitTypeDecl(D);492Record.push_back(D->getIdentifierNamespace());493494BitsPacker TagDeclBits;495TagDeclBits.addBits(llvm::to_underlying(D->getTagKind()), /*BitWidth=*/3);496TagDeclBits.addBit(!isa<CXXRecordDecl>(D) ? D->isCompleteDefinition() : 0);497TagDeclBits.addBit(D->isEmbeddedInDeclarator());498TagDeclBits.addBit(D->isFreeStanding());499TagDeclBits.addBit(D->isCompleteDefinitionRequired());500TagDeclBits.addBits(501D->hasExtInfo() ? 1 : (D->getTypedefNameForAnonDecl() ? 2 : 0),502/*BitWidth=*/2);503Record.push_back(TagDeclBits);504505Record.AddSourceRange(D->getBraceRange());506507if (D->hasExtInfo()) {508Record.AddQualifierInfo(*D->getExtInfo());509} else if (auto *TD = D->getTypedefNameForAnonDecl()) {510Record.AddDeclRef(TD);511Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());512}513}514515void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {516static_assert(DeclContext::NumEnumDeclBits == 43,517"You need to update the serializer after you change the "518"EnumDeclBits");519520VisitTagDecl(D);521Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());522if (!D->getIntegerTypeSourceInfo())523Record.AddTypeRef(D->getIntegerType());524Record.AddTypeRef(D->getPromotionType());525526BitsPacker EnumDeclBits;527EnumDeclBits.addBits(D->getNumPositiveBits(), /*BitWidth=*/8);528EnumDeclBits.addBits(D->getNumNegativeBits(), /*BitWidth=*/8);529EnumDeclBits.addBit(D->isScoped());530EnumDeclBits.addBit(D->isScopedUsingClassTag());531EnumDeclBits.addBit(D->isFixed());532Record.push_back(EnumDeclBits);533534Record.push_back(D->getODRHash());535536if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {537Record.AddDeclRef(MemberInfo->getInstantiatedFrom());538Record.push_back(MemberInfo->getTemplateSpecializationKind());539Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());540} else {541Record.AddDeclRef(nullptr);542}543544if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&545!D->isInvalidDecl() && !D->isImplicit() && !D->hasExtInfo() &&546!D->getTypedefNameForAnonDecl() &&547D->getFirstDecl() == D->getMostRecentDecl() &&548!D->isTopLevelDeclInObjCContainer() &&549!CXXRecordDecl::classofKind(D->getKind()) &&550!D->getIntegerTypeSourceInfo() && !D->getMemberSpecializationInfo() &&551!needsAnonymousDeclarationNumber(D) &&552D->getDeclName().getNameKind() == DeclarationName::Identifier)553AbbrevToUse = Writer.getDeclEnumAbbrev();554555Code = serialization::DECL_ENUM;556}557558void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {559static_assert(DeclContext::NumRecordDeclBits == 64,560"You need to update the serializer after you change the "561"RecordDeclBits");562563VisitTagDecl(D);564565BitsPacker RecordDeclBits;566RecordDeclBits.addBit(D->hasFlexibleArrayMember());567RecordDeclBits.addBit(D->isAnonymousStructOrUnion());568RecordDeclBits.addBit(D->hasObjectMember());569RecordDeclBits.addBit(D->hasVolatileMember());570RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDefaultInitialize());571RecordDeclBits.addBit(D->isNonTrivialToPrimitiveCopy());572RecordDeclBits.addBit(D->isNonTrivialToPrimitiveDestroy());573RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());574RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveDestructCUnion());575RecordDeclBits.addBit(D->hasNonTrivialToPrimitiveCopyCUnion());576RecordDeclBits.addBit(D->isParamDestroyedInCallee());577RecordDeclBits.addBits(llvm::to_underlying(D->getArgPassingRestrictions()), 2);578Record.push_back(RecordDeclBits);579580// Only compute this for C/Objective-C, in C++ this is computed as part581// of CXXRecordDecl.582if (!isa<CXXRecordDecl>(D))583Record.push_back(D->getODRHash());584585if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&586!D->isImplicit() && !D->isInvalidDecl() && !D->hasExtInfo() &&587!D->getTypedefNameForAnonDecl() &&588D->getFirstDecl() == D->getMostRecentDecl() &&589!D->isTopLevelDeclInObjCContainer() &&590!CXXRecordDecl::classofKind(D->getKind()) &&591!needsAnonymousDeclarationNumber(D) &&592D->getDeclName().getNameKind() == DeclarationName::Identifier)593AbbrevToUse = Writer.getDeclRecordAbbrev();594595Code = serialization::DECL_RECORD;596}597598void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {599VisitNamedDecl(D);600Record.AddTypeRef(D->getType());601}602603void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {604VisitValueDecl(D);605Record.push_back(D->getInitExpr()? 1 : 0);606if (D->getInitExpr())607Record.AddStmt(D->getInitExpr());608Record.AddAPSInt(D->getInitVal());609610Code = serialization::DECL_ENUM_CONSTANT;611}612613void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {614VisitValueDecl(D);615Record.AddSourceLocation(D->getInnerLocStart());616Record.push_back(D->hasExtInfo());617if (D->hasExtInfo()) {618DeclaratorDecl::ExtInfo *Info = D->getExtInfo();619Record.AddQualifierInfo(*Info);620Record.AddStmt(Info->TrailingRequiresClause);621}622// The location information is deferred until the end of the record.623Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()624: QualType());625}626627void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {628static_assert(DeclContext::NumFunctionDeclBits == 44,629"You need to update the serializer after you change the "630"FunctionDeclBits");631632VisitRedeclarable(D);633634Record.push_back(D->getTemplatedKind());635switch (D->getTemplatedKind()) {636case FunctionDecl::TK_NonTemplate:637break;638case FunctionDecl::TK_DependentNonTemplate:639Record.AddDeclRef(D->getInstantiatedFromDecl());640break;641case FunctionDecl::TK_FunctionTemplate:642Record.AddDeclRef(D->getDescribedFunctionTemplate());643break;644case FunctionDecl::TK_MemberSpecialization: {645MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();646Record.AddDeclRef(MemberInfo->getInstantiatedFrom());647Record.push_back(MemberInfo->getTemplateSpecializationKind());648Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());649break;650}651case FunctionDecl::TK_FunctionTemplateSpecialization: {652FunctionTemplateSpecializationInfo *653FTSInfo = D->getTemplateSpecializationInfo();654655RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);656657Record.AddDeclRef(FTSInfo->getTemplate());658Record.push_back(FTSInfo->getTemplateSpecializationKind());659660// Template arguments.661Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);662663// Template args as written.664Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);665if (FTSInfo->TemplateArgumentsAsWritten)666Record.AddASTTemplateArgumentListInfo(667FTSInfo->TemplateArgumentsAsWritten);668669Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());670671if (MemberSpecializationInfo *MemberInfo =672FTSInfo->getMemberSpecializationInfo()) {673Record.push_back(1);674Record.AddDeclRef(MemberInfo->getInstantiatedFrom());675Record.push_back(MemberInfo->getTemplateSpecializationKind());676Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());677} else {678Record.push_back(0);679}680681if (D->isCanonicalDecl()) {682// Write the template that contains the specializations set. We will683// add a FunctionTemplateSpecializationInfo to it when reading.684Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());685}686break;687}688case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {689DependentFunctionTemplateSpecializationInfo *690DFTSInfo = D->getDependentSpecializationInfo();691692// Candidates.693Record.push_back(DFTSInfo->getCandidates().size());694for (FunctionTemplateDecl *FTD : DFTSInfo->getCandidates())695Record.AddDeclRef(FTD);696697// Templates args.698Record.push_back(DFTSInfo->TemplateArgumentsAsWritten != nullptr);699if (DFTSInfo->TemplateArgumentsAsWritten)700Record.AddASTTemplateArgumentListInfo(701DFTSInfo->TemplateArgumentsAsWritten);702break;703}704}705706VisitDeclaratorDecl(D);707Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());708Record.push_back(D->getIdentifierNamespace());709710// The order matters here. It will be better to put the bit with higher711// probability to be 0 in the end of the bits. See the comments in VisitDecl712// for details.713BitsPacker FunctionDeclBits;714// FIXME: stable encoding715FunctionDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()), 3);716FunctionDeclBits.addBits((uint32_t)D->getStorageClass(), /*BitWidth=*/3);717FunctionDeclBits.addBit(D->isInlineSpecified());718FunctionDeclBits.addBit(D->isInlined());719FunctionDeclBits.addBit(D->hasSkippedBody());720FunctionDeclBits.addBit(D->isVirtualAsWritten());721FunctionDeclBits.addBit(D->isPureVirtual());722FunctionDeclBits.addBit(D->hasInheritedPrototype());723FunctionDeclBits.addBit(D->hasWrittenPrototype());724FunctionDeclBits.addBit(D->isDeletedBit());725FunctionDeclBits.addBit(D->isTrivial());726FunctionDeclBits.addBit(D->isTrivialForCall());727FunctionDeclBits.addBit(D->isDefaulted());728FunctionDeclBits.addBit(D->isExplicitlyDefaulted());729FunctionDeclBits.addBit(D->isIneligibleOrNotSelected());730FunctionDeclBits.addBits((uint64_t)(D->getConstexprKind()), /*BitWidth=*/2);731FunctionDeclBits.addBit(D->hasImplicitReturnZero());732FunctionDeclBits.addBit(D->isMultiVersion());733FunctionDeclBits.addBit(D->isLateTemplateParsed());734FunctionDeclBits.addBit(D->FriendConstraintRefersToEnclosingTemplate());735FunctionDeclBits.addBit(D->usesSEHTry());736Record.push_back(FunctionDeclBits);737738Record.AddSourceLocation(D->getEndLoc());739if (D->isExplicitlyDefaulted())740Record.AddSourceLocation(D->getDefaultLoc());741742Record.push_back(D->getODRHash());743744if (D->isDefaulted() || D->isDeletedAsWritten()) {745if (auto *FDI = D->getDefalutedOrDeletedInfo()) {746// Store both that there is an DefaultedOrDeletedInfo and whether it747// contains a DeletedMessage.748StringLiteral *DeletedMessage = FDI->getDeletedMessage();749Record.push_back(1 | (DeletedMessage ? 2 : 0));750if (DeletedMessage)751Record.AddStmt(DeletedMessage);752753Record.push_back(FDI->getUnqualifiedLookups().size());754for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {755Record.AddDeclRef(P.getDecl());756Record.push_back(P.getAccess());757}758} else {759Record.push_back(0);760}761}762763Record.push_back(D->param_size());764for (auto *P : D->parameters())765Record.AddDeclRef(P);766Code = serialization::DECL_FUNCTION;767}768769static void addExplicitSpecifier(ExplicitSpecifier ES,770ASTRecordWriter &Record) {771uint64_t Kind = static_cast<uint64_t>(ES.getKind());772Kind = Kind << 1 | static_cast<bool>(ES.getExpr());773Record.push_back(Kind);774if (ES.getExpr()) {775Record.AddStmt(ES.getExpr());776}777}778779void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {780addExplicitSpecifier(D->getExplicitSpecifier(), Record);781Record.AddDeclRef(D->Ctor);782VisitFunctionDecl(D);783Record.push_back(static_cast<unsigned char>(D->getDeductionCandidateKind()));784Code = serialization::DECL_CXX_DEDUCTION_GUIDE;785}786787void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {788static_assert(DeclContext::NumObjCMethodDeclBits == 37,789"You need to update the serializer after you change the "790"ObjCMethodDeclBits");791792VisitNamedDecl(D);793// FIXME: convert to LazyStmtPtr?794// Unlike C/C++, method bodies will never be in header files.795bool HasBodyStuff = D->getBody() != nullptr;796Record.push_back(HasBodyStuff);797if (HasBodyStuff) {798Record.AddStmt(D->getBody());799}800Record.AddDeclRef(D->getSelfDecl());801Record.AddDeclRef(D->getCmdDecl());802Record.push_back(D->isInstanceMethod());803Record.push_back(D->isVariadic());804Record.push_back(D->isPropertyAccessor());805Record.push_back(D->isSynthesizedAccessorStub());806Record.push_back(D->isDefined());807Record.push_back(D->isOverriding());808Record.push_back(D->hasSkippedBody());809810Record.push_back(D->isRedeclaration());811Record.push_back(D->hasRedeclaration());812if (D->hasRedeclaration()) {813assert(Context.getObjCMethodRedeclaration(D));814Record.AddDeclRef(Context.getObjCMethodRedeclaration(D));815}816817// FIXME: stable encoding for @required/@optional818Record.push_back(llvm::to_underlying(D->getImplementationControl()));819// FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability820Record.push_back(D->getObjCDeclQualifier());821Record.push_back(D->hasRelatedResultType());822Record.AddTypeRef(D->getReturnType());823Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());824Record.AddSourceLocation(D->getEndLoc());825Record.push_back(D->param_size());826for (const auto *P : D->parameters())827Record.AddDeclRef(P);828829Record.push_back(D->getSelLocsKind());830unsigned NumStoredSelLocs = D->getNumStoredSelLocs();831SourceLocation *SelLocs = D->getStoredSelLocs();832Record.push_back(NumStoredSelLocs);833for (unsigned i = 0; i != NumStoredSelLocs; ++i)834Record.AddSourceLocation(SelLocs[i]);835836Code = serialization::DECL_OBJC_METHOD;837}838839void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {840VisitTypedefNameDecl(D);841Record.push_back(D->Variance);842Record.push_back(D->Index);843Record.AddSourceLocation(D->VarianceLoc);844Record.AddSourceLocation(D->ColonLoc);845846Code = serialization::DECL_OBJC_TYPE_PARAM;847}848849void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {850static_assert(DeclContext::NumObjCContainerDeclBits == 64,851"You need to update the serializer after you change the "852"ObjCContainerDeclBits");853854VisitNamedDecl(D);855Record.AddSourceLocation(D->getAtStartLoc());856Record.AddSourceRange(D->getAtEndRange());857// Abstract class (no need to define a stable serialization::DECL code).858}859860void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {861VisitRedeclarable(D);862VisitObjCContainerDecl(D);863Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));864AddObjCTypeParamList(D->TypeParamList);865866Record.push_back(D->isThisDeclarationADefinition());867if (D->isThisDeclarationADefinition()) {868// Write the DefinitionData869ObjCInterfaceDecl::DefinitionData &Data = D->data();870871Record.AddTypeSourceInfo(D->getSuperClassTInfo());872Record.AddSourceLocation(D->getEndOfDefinitionLoc());873Record.push_back(Data.HasDesignatedInitializers);874Record.push_back(D->getODRHash());875876// Write out the protocols that are directly referenced by the @interface.877Record.push_back(Data.ReferencedProtocols.size());878for (const auto *P : D->protocols())879Record.AddDeclRef(P);880for (const auto &PL : D->protocol_locs())881Record.AddSourceLocation(PL);882883// Write out the protocols that are transitively referenced.884Record.push_back(Data.AllReferencedProtocols.size());885for (ObjCList<ObjCProtocolDecl>::iterator886P = Data.AllReferencedProtocols.begin(),887PEnd = Data.AllReferencedProtocols.end();888P != PEnd; ++P)889Record.AddDeclRef(*P);890891892if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {893// Ensure that we write out the set of categories for this class.894Writer.ObjCClassesWithCategories.insert(D);895896// Make sure that the categories get serialized.897for (; Cat; Cat = Cat->getNextClassCategoryRaw())898(void)Writer.GetDeclRef(Cat);899}900}901902Code = serialization::DECL_OBJC_INTERFACE;903}904905void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {906VisitFieldDecl(D);907// FIXME: stable encoding for @public/@private/@protected/@package908Record.push_back(D->getAccessControl());909Record.push_back(D->getSynthesize());910911if (D->getDeclContext() == D->getLexicalDeclContext() &&912!D->hasAttrs() &&913!D->isImplicit() &&914!D->isUsed(false) &&915!D->isInvalidDecl() &&916!D->isReferenced() &&917!D->isModulePrivate() &&918!D->getBitWidth() &&919!D->hasExtInfo() &&920D->getDeclName())921AbbrevToUse = Writer.getDeclObjCIvarAbbrev();922923Code = serialization::DECL_OBJC_IVAR;924}925926void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {927VisitRedeclarable(D);928VisitObjCContainerDecl(D);929930Record.push_back(D->isThisDeclarationADefinition());931if (D->isThisDeclarationADefinition()) {932Record.push_back(D->protocol_size());933for (const auto *I : D->protocols())934Record.AddDeclRef(I);935for (const auto &PL : D->protocol_locs())936Record.AddSourceLocation(PL);937Record.push_back(D->getODRHash());938}939940Code = serialization::DECL_OBJC_PROTOCOL;941}942943void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {944VisitFieldDecl(D);945Code = serialization::DECL_OBJC_AT_DEFS_FIELD;946}947948void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {949VisitObjCContainerDecl(D);950Record.AddSourceLocation(D->getCategoryNameLoc());951Record.AddSourceLocation(D->getIvarLBraceLoc());952Record.AddSourceLocation(D->getIvarRBraceLoc());953Record.AddDeclRef(D->getClassInterface());954AddObjCTypeParamList(D->TypeParamList);955Record.push_back(D->protocol_size());956for (const auto *I : D->protocols())957Record.AddDeclRef(I);958for (const auto &PL : D->protocol_locs())959Record.AddSourceLocation(PL);960Code = serialization::DECL_OBJC_CATEGORY;961}962963void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {964VisitNamedDecl(D);965Record.AddDeclRef(D->getClassInterface());966Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;967}968969void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {970VisitNamedDecl(D);971Record.AddSourceLocation(D->getAtLoc());972Record.AddSourceLocation(D->getLParenLoc());973Record.AddTypeRef(D->getType());974Record.AddTypeSourceInfo(D->getTypeSourceInfo());975// FIXME: stable encoding976Record.push_back((unsigned)D->getPropertyAttributes());977Record.push_back((unsigned)D->getPropertyAttributesAsWritten());978// FIXME: stable encoding979Record.push_back((unsigned)D->getPropertyImplementation());980Record.AddDeclarationName(D->getGetterName());981Record.AddSourceLocation(D->getGetterNameLoc());982Record.AddDeclarationName(D->getSetterName());983Record.AddSourceLocation(D->getSetterNameLoc());984Record.AddDeclRef(D->getGetterMethodDecl());985Record.AddDeclRef(D->getSetterMethodDecl());986Record.AddDeclRef(D->getPropertyIvarDecl());987Code = serialization::DECL_OBJC_PROPERTY;988}989990void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {991VisitObjCContainerDecl(D);992Record.AddDeclRef(D->getClassInterface());993// Abstract class (no need to define a stable serialization::DECL code).994}995996void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {997VisitObjCImplDecl(D);998Record.AddSourceLocation(D->getCategoryNameLoc());999Code = serialization::DECL_OBJC_CATEGORY_IMPL;1000}10011002void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {1003VisitObjCImplDecl(D);1004Record.AddDeclRef(D->getSuperClass());1005Record.AddSourceLocation(D->getSuperClassLoc());1006Record.AddSourceLocation(D->getIvarLBraceLoc());1007Record.AddSourceLocation(D->getIvarRBraceLoc());1008Record.push_back(D->hasNonZeroConstructors());1009Record.push_back(D->hasDestructors());1010Record.push_back(D->NumIvarInitializers);1011if (D->NumIvarInitializers)1012Record.AddCXXCtorInitializers(1013llvm::ArrayRef(D->init_begin(), D->init_end()));1014Code = serialization::DECL_OBJC_IMPLEMENTATION;1015}10161017void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {1018VisitDecl(D);1019Record.AddSourceLocation(D->getBeginLoc());1020Record.AddDeclRef(D->getPropertyDecl());1021Record.AddDeclRef(D->getPropertyIvarDecl());1022Record.AddSourceLocation(D->getPropertyIvarDeclLoc());1023Record.AddDeclRef(D->getGetterMethodDecl());1024Record.AddDeclRef(D->getSetterMethodDecl());1025Record.AddStmt(D->getGetterCXXConstructor());1026Record.AddStmt(D->getSetterCXXAssignment());1027Code = serialization::DECL_OBJC_PROPERTY_IMPL;1028}10291030void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {1031VisitDeclaratorDecl(D);1032Record.push_back(D->isMutable());10331034Record.push_back((D->StorageKind << 1) | D->BitField);1035if (D->StorageKind == FieldDecl::ISK_CapturedVLAType)1036Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));1037else if (D->BitField)1038Record.AddStmt(D->getBitWidth());10391040if (!D->getDeclName())1041Record.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D));10421043if (D->getDeclContext() == D->getLexicalDeclContext() &&1044!D->hasAttrs() &&1045!D->isImplicit() &&1046!D->isUsed(false) &&1047!D->isInvalidDecl() &&1048!D->isReferenced() &&1049!D->isTopLevelDeclInObjCContainer() &&1050!D->isModulePrivate() &&1051!D->getBitWidth() &&1052!D->hasInClassInitializer() &&1053!D->hasCapturedVLAType() &&1054!D->hasExtInfo() &&1055!ObjCIvarDecl::classofKind(D->getKind()) &&1056!ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&1057D->getDeclName())1058AbbrevToUse = Writer.getDeclFieldAbbrev();10591060Code = serialization::DECL_FIELD;1061}10621063void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {1064VisitDeclaratorDecl(D);1065Record.AddIdentifierRef(D->getGetterId());1066Record.AddIdentifierRef(D->getSetterId());1067Code = serialization::DECL_MS_PROPERTY;1068}10691070void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {1071VisitValueDecl(D);1072MSGuidDecl::Parts Parts = D->getParts();1073Record.push_back(Parts.Part1);1074Record.push_back(Parts.Part2);1075Record.push_back(Parts.Part3);1076Record.append(std::begin(Parts.Part4And5), std::end(Parts.Part4And5));1077Code = serialization::DECL_MS_GUID;1078}10791080void ASTDeclWriter::VisitUnnamedGlobalConstantDecl(1081UnnamedGlobalConstantDecl *D) {1082VisitValueDecl(D);1083Record.AddAPValue(D->getValue());1084Code = serialization::DECL_UNNAMED_GLOBAL_CONSTANT;1085}10861087void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {1088VisitValueDecl(D);1089Record.AddAPValue(D->getValue());1090Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;1091}10921093void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {1094VisitValueDecl(D);1095Record.push_back(D->getChainingSize());10961097for (const auto *P : D->chain())1098Record.AddDeclRef(P);1099Code = serialization::DECL_INDIRECTFIELD;1100}11011102void ASTDeclWriter::VisitVarDecl(VarDecl *D) {1103VisitRedeclarable(D);1104VisitDeclaratorDecl(D);11051106// The order matters here. It will be better to put the bit with higher1107// probability to be 0 in the end of the bits. See the comments in VisitDecl1108// for details.1109BitsPacker VarDeclBits;1110VarDeclBits.addBits(llvm::to_underlying(D->getLinkageInternal()),1111/*BitWidth=*/3);11121113bool ModulesCodegen = false;1114if (Writer.WritingModule && D->getStorageDuration() == SD_Static &&1115!D->getDescribedVarTemplate()) {1116// When building a C++20 module interface unit or a partition unit, a1117// strong definition in the module interface is provided by the1118// compilation of that unit, not by its users. (Inline variables are still1119// emitted in module users.)1120ModulesCodegen =1121(Writer.WritingModule->isInterfaceOrPartition() ||1122(D->hasAttr<DLLExportAttr>() &&1123Writer.Context->getLangOpts().BuildingPCHWithObjectFile)) &&1124Writer.Context->GetGVALinkageForVariable(D) >= GVA_StrongExternal;1125}1126VarDeclBits.addBit(ModulesCodegen);11271128VarDeclBits.addBits(D->getStorageClass(), /*BitWidth=*/3);1129VarDeclBits.addBits(D->getTSCSpec(), /*BitWidth=*/2);1130VarDeclBits.addBits(D->getInitStyle(), /*BitWidth=*/2);1131VarDeclBits.addBit(D->isARCPseudoStrong());11321133bool HasDeducedType = false;1134if (!isa<ParmVarDecl>(D)) {1135VarDeclBits.addBit(D->isThisDeclarationADemotedDefinition());1136VarDeclBits.addBit(D->isExceptionVariable());1137VarDeclBits.addBit(D->isNRVOVariable());1138VarDeclBits.addBit(D->isCXXForRangeDecl());11391140VarDeclBits.addBit(D->isInline());1141VarDeclBits.addBit(D->isInlineSpecified());1142VarDeclBits.addBit(D->isConstexpr());1143VarDeclBits.addBit(D->isInitCapture());1144VarDeclBits.addBit(D->isPreviousDeclInSameBlockScope());11451146VarDeclBits.addBit(D->isEscapingByref());1147HasDeducedType = D->getType()->getContainedDeducedType();1148VarDeclBits.addBit(HasDeducedType);11491150if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))1151VarDeclBits.addBits(llvm::to_underlying(IPD->getParameterKind()),1152/*Width=*/3);1153else1154VarDeclBits.addBits(0, /*Width=*/3);11551156VarDeclBits.addBit(D->isObjCForDecl());1157}11581159Record.push_back(VarDeclBits);11601161if (ModulesCodegen)1162Writer.AddDeclRef(D, Writer.ModularCodegenDecls);11631164if (D->hasAttr<BlocksAttr>()) {1165BlockVarCopyInit Init = Writer.Context->getBlockVarCopyInit(D);1166Record.AddStmt(Init.getCopyExpr());1167if (Init.getCopyExpr())1168Record.push_back(Init.canThrow());1169}11701171enum {1172VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization1173};1174if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {1175Record.push_back(VarTemplate);1176Record.AddDeclRef(TemplD);1177} else if (MemberSpecializationInfo *SpecInfo1178= D->getMemberSpecializationInfo()) {1179Record.push_back(StaticDataMemberSpecialization);1180Record.AddDeclRef(SpecInfo->getInstantiatedFrom());1181Record.push_back(SpecInfo->getTemplateSpecializationKind());1182Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());1183} else {1184Record.push_back(VarNotTemplate);1185}11861187if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&1188!D->isTopLevelDeclInObjCContainer() &&1189!needsAnonymousDeclarationNumber(D) &&1190D->getDeclName().getNameKind() == DeclarationName::Identifier &&1191!D->hasExtInfo() && D->getFirstDecl() == D->getMostRecentDecl() &&1192D->getKind() == Decl::Var && !D->isInline() && !D->isConstexpr() &&1193!D->isInitCapture() && !D->isPreviousDeclInSameBlockScope() &&1194!D->isEscapingByref() && !HasDeducedType &&1195D->getStorageDuration() != SD_Static && !D->getDescribedVarTemplate() &&1196!D->getMemberSpecializationInfo() && !D->isObjCForDecl() &&1197!isa<ImplicitParamDecl>(D) && !D->isEscapingByref())1198AbbrevToUse = Writer.getDeclVarAbbrev();11991200Code = serialization::DECL_VAR;1201}12021203void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {1204VisitVarDecl(D);1205Code = serialization::DECL_IMPLICIT_PARAM;1206}12071208void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {1209VisitVarDecl(D);12101211// See the implementation of `ParmVarDecl::getParameterIndex()`, which may1212// exceed the size of the normal bitfield. So it may be better to not pack1213// these bits.1214Record.push_back(D->getFunctionScopeIndex());12151216BitsPacker ParmVarDeclBits;1217ParmVarDeclBits.addBit(D->isObjCMethodParameter());1218ParmVarDeclBits.addBits(D->getFunctionScopeDepth(), /*BitsWidth=*/7);1219// FIXME: stable encoding1220ParmVarDeclBits.addBits(D->getObjCDeclQualifier(), /*BitsWidth=*/7);1221ParmVarDeclBits.addBit(D->isKNRPromoted());1222ParmVarDeclBits.addBit(D->hasInheritedDefaultArg());1223ParmVarDeclBits.addBit(D->hasUninstantiatedDefaultArg());1224ParmVarDeclBits.addBit(D->getExplicitObjectParamThisLoc().isValid());1225Record.push_back(ParmVarDeclBits);12261227if (D->hasUninstantiatedDefaultArg())1228Record.AddStmt(D->getUninstantiatedDefaultArg());1229if (D->getExplicitObjectParamThisLoc().isValid())1230Record.AddSourceLocation(D->getExplicitObjectParamThisLoc());1231Code = serialization::DECL_PARM_VAR;12321233// If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here1234// we dynamically check for the properties that we optimize for, but don't1235// know are true of all PARM_VAR_DECLs.1236if (D->getDeclContext() == D->getLexicalDeclContext() && !D->hasAttrs() &&1237!D->hasExtInfo() && D->getStorageClass() == 0 && !D->isInvalidDecl() &&1238!D->isTopLevelDeclInObjCContainer() &&1239D->getInitStyle() == VarDecl::CInit && // Can params have anything else?1240D->getInit() == nullptr) // No default expr.1241AbbrevToUse = Writer.getDeclParmVarAbbrev();12421243// Check things we know are true of *every* PARM_VAR_DECL, which is more than1244// just us assuming it.1245assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");1246assert(!D->isThisDeclarationADemotedDefinition()1247&& "PARM_VAR_DECL can't be demoted definition.");1248assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");1249assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");1250assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");1251assert(!D->isStaticDataMember() &&1252"PARM_VAR_DECL can't be static data member");1253}12541255void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {1256// Record the number of bindings first to simplify deserialization.1257Record.push_back(D->bindings().size());12581259VisitVarDecl(D);1260for (auto *B : D->bindings())1261Record.AddDeclRef(B);1262Code = serialization::DECL_DECOMPOSITION;1263}12641265void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {1266VisitValueDecl(D);1267Record.AddStmt(D->getBinding());1268Code = serialization::DECL_BINDING;1269}12701271void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {1272VisitDecl(D);1273Record.AddStmt(D->getAsmString());1274Record.AddSourceLocation(D->getRParenLoc());1275Code = serialization::DECL_FILE_SCOPE_ASM;1276}12771278void ASTDeclWriter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {1279VisitDecl(D);1280Record.AddStmt(D->getStmt());1281Code = serialization::DECL_TOP_LEVEL_STMT_DECL;1282}12831284void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {1285VisitDecl(D);1286Code = serialization::DECL_EMPTY;1287}12881289void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(1290LifetimeExtendedTemporaryDecl *D) {1291VisitDecl(D);1292Record.AddDeclRef(D->getExtendingDecl());1293Record.AddStmt(D->getTemporaryExpr());1294Record.push_back(static_cast<bool>(D->getValue()));1295if (D->getValue())1296Record.AddAPValue(*D->getValue());1297Record.push_back(D->getManglingNumber());1298Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;1299}1300void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {1301VisitDecl(D);1302Record.AddStmt(D->getBody());1303Record.AddTypeSourceInfo(D->getSignatureAsWritten());1304Record.push_back(D->param_size());1305for (ParmVarDecl *P : D->parameters())1306Record.AddDeclRef(P);1307Record.push_back(D->isVariadic());1308Record.push_back(D->blockMissingReturnType());1309Record.push_back(D->isConversionFromLambda());1310Record.push_back(D->doesNotEscape());1311Record.push_back(D->canAvoidCopyToHeap());1312Record.push_back(D->capturesCXXThis());1313Record.push_back(D->getNumCaptures());1314for (const auto &capture : D->captures()) {1315Record.AddDeclRef(capture.getVariable());13161317unsigned flags = 0;1318if (capture.isByRef()) flags |= 1;1319if (capture.isNested()) flags |= 2;1320if (capture.hasCopyExpr()) flags |= 4;1321Record.push_back(flags);13221323if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());1324}13251326Code = serialization::DECL_BLOCK;1327}13281329void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {1330Record.push_back(CD->getNumParams());1331VisitDecl(CD);1332Record.push_back(CD->getContextParamPosition());1333Record.push_back(CD->isNothrow() ? 1 : 0);1334// Body is stored by VisitCapturedStmt.1335for (unsigned I = 0; I < CD->getNumParams(); ++I)1336Record.AddDeclRef(CD->getParam(I));1337Code = serialization::DECL_CAPTURED;1338}13391340void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {1341static_assert(DeclContext::NumLinkageSpecDeclBits == 17,1342"You need to update the serializer after you change the"1343"LinkageSpecDeclBits");13441345VisitDecl(D);1346Record.push_back(llvm::to_underlying(D->getLanguage()));1347Record.AddSourceLocation(D->getExternLoc());1348Record.AddSourceLocation(D->getRBraceLoc());1349Code = serialization::DECL_LINKAGE_SPEC;1350}13511352void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {1353VisitDecl(D);1354Record.AddSourceLocation(D->getRBraceLoc());1355Code = serialization::DECL_EXPORT;1356}13571358void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {1359VisitNamedDecl(D);1360Record.AddSourceLocation(D->getBeginLoc());1361Code = serialization::DECL_LABEL;1362}136313641365void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {1366VisitRedeclarable(D);1367VisitNamedDecl(D);13681369BitsPacker NamespaceDeclBits;1370NamespaceDeclBits.addBit(D->isInline());1371NamespaceDeclBits.addBit(D->isNested());1372Record.push_back(NamespaceDeclBits);13731374Record.AddSourceLocation(D->getBeginLoc());1375Record.AddSourceLocation(D->getRBraceLoc());13761377if (D->isFirstDecl())1378Record.AddDeclRef(D->getAnonymousNamespace());1379Code = serialization::DECL_NAMESPACE;13801381if (Writer.hasChain() && D->isAnonymousNamespace() &&1382D == D->getMostRecentDecl()) {1383// This is a most recent reopening of the anonymous namespace. If its parent1384// is in a previous PCH (or is the TU), mark that parent for update, because1385// the original namespace always points to the latest re-opening of its1386// anonymous namespace.1387Decl *Parent = cast<Decl>(1388D->getParent()->getRedeclContext()->getPrimaryContext());1389if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {1390Writer.DeclUpdates[Parent].push_back(1391ASTWriter::DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, D));1392}1393}1394}13951396void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {1397VisitRedeclarable(D);1398VisitNamedDecl(D);1399Record.AddSourceLocation(D->getNamespaceLoc());1400Record.AddSourceLocation(D->getTargetNameLoc());1401Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1402Record.AddDeclRef(D->getNamespace());1403Code = serialization::DECL_NAMESPACE_ALIAS;1404}14051406void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {1407VisitNamedDecl(D);1408Record.AddSourceLocation(D->getUsingLoc());1409Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1410Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());1411Record.AddDeclRef(D->FirstUsingShadow.getPointer());1412Record.push_back(D->hasTypename());1413Record.AddDeclRef(Context.getInstantiatedFromUsingDecl(D));1414Code = serialization::DECL_USING;1415}14161417void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {1418VisitNamedDecl(D);1419Record.AddSourceLocation(D->getUsingLoc());1420Record.AddSourceLocation(D->getEnumLoc());1421Record.AddTypeSourceInfo(D->getEnumType());1422Record.AddDeclRef(D->FirstUsingShadow.getPointer());1423Record.AddDeclRef(Context.getInstantiatedFromUsingEnumDecl(D));1424Code = serialization::DECL_USING_ENUM;1425}14261427void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {1428Record.push_back(D->NumExpansions);1429VisitNamedDecl(D);1430Record.AddDeclRef(D->getInstantiatedFromUsingDecl());1431for (auto *E : D->expansions())1432Record.AddDeclRef(E);1433Code = serialization::DECL_USING_PACK;1434}14351436void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {1437VisitRedeclarable(D);1438VisitNamedDecl(D);1439Record.AddDeclRef(D->getTargetDecl());1440Record.push_back(D->getIdentifierNamespace());1441Record.AddDeclRef(D->UsingOrNextShadow);1442Record.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D));14431444if (D->getDeclContext() == D->getLexicalDeclContext() &&1445D->getFirstDecl() == D->getMostRecentDecl() && !D->hasAttrs() &&1446!needsAnonymousDeclarationNumber(D) &&1447D->getDeclName().getNameKind() == DeclarationName::Identifier)1448AbbrevToUse = Writer.getDeclUsingShadowAbbrev();14491450Code = serialization::DECL_USING_SHADOW;1451}14521453void ASTDeclWriter::VisitConstructorUsingShadowDecl(1454ConstructorUsingShadowDecl *D) {1455VisitUsingShadowDecl(D);1456Record.AddDeclRef(D->NominatedBaseClassShadowDecl);1457Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);1458Record.push_back(D->IsVirtual);1459Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;1460}14611462void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {1463VisitNamedDecl(D);1464Record.AddSourceLocation(D->getUsingLoc());1465Record.AddSourceLocation(D->getNamespaceKeyLocation());1466Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1467Record.AddDeclRef(D->getNominatedNamespace());1468Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));1469Code = serialization::DECL_USING_DIRECTIVE;1470}14711472void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {1473VisitValueDecl(D);1474Record.AddSourceLocation(D->getUsingLoc());1475Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1476Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());1477Record.AddSourceLocation(D->getEllipsisLoc());1478Code = serialization::DECL_UNRESOLVED_USING_VALUE;1479}14801481void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(1482UnresolvedUsingTypenameDecl *D) {1483VisitTypeDecl(D);1484Record.AddSourceLocation(D->getTypenameLoc());1485Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());1486Record.AddSourceLocation(D->getEllipsisLoc());1487Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;1488}14891490void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(1491UnresolvedUsingIfExistsDecl *D) {1492VisitNamedDecl(D);1493Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;1494}14951496void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {1497VisitRecordDecl(D);14981499enum {1500CXXRecNotTemplate = 0,1501CXXRecTemplate,1502CXXRecMemberSpecialization,1503CXXLambda1504};1505if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {1506Record.push_back(CXXRecTemplate);1507Record.AddDeclRef(TemplD);1508} else if (MemberSpecializationInfo *MSInfo1509= D->getMemberSpecializationInfo()) {1510Record.push_back(CXXRecMemberSpecialization);1511Record.AddDeclRef(MSInfo->getInstantiatedFrom());1512Record.push_back(MSInfo->getTemplateSpecializationKind());1513Record.AddSourceLocation(MSInfo->getPointOfInstantiation());1514} else if (D->isLambda()) {1515// For a lambda, we need some information early for merging.1516Record.push_back(CXXLambda);1517if (auto *Context = D->getLambdaContextDecl()) {1518Record.AddDeclRef(Context);1519Record.push_back(D->getLambdaIndexInContext());1520} else {1521Record.push_back(0);1522}1523} else {1524Record.push_back(CXXRecNotTemplate);1525}15261527Record.push_back(D->isThisDeclarationADefinition());1528if (D->isThisDeclarationADefinition())1529Record.AddCXXDefinitionData(D);15301531if (D->isCompleteDefinition() && D->isInNamedModule())1532Writer.AddDeclRef(D, Writer.ModularCodegenDecls);15331534// Store (what we currently believe to be) the key function to avoid1535// deserializing every method so we can compute it.1536//1537// FIXME: Avoid adding the key function if the class is defined in1538// module purview since in that case the key function is meaningless.1539if (D->isCompleteDefinition())1540Record.AddDeclRef(Context.getCurrentKeyFunction(D));15411542Code = serialization::DECL_CXX_RECORD;1543}15441545void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {1546VisitFunctionDecl(D);1547if (D->isCanonicalDecl()) {1548Record.push_back(D->size_overridden_methods());1549for (const CXXMethodDecl *MD : D->overridden_methods())1550Record.AddDeclRef(MD);1551} else {1552// We only need to record overridden methods once for the canonical decl.1553Record.push_back(0);1554}15551556if (D->getDeclContext() == D->getLexicalDeclContext() &&1557D->getFirstDecl() == D->getMostRecentDecl() && !D->isInvalidDecl() &&1558!D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() &&1559D->getDeclName().getNameKind() == DeclarationName::Identifier &&1560!D->hasExtInfo() && !D->isExplicitlyDefaulted()) {1561if (D->getTemplatedKind() == FunctionDecl::TK_NonTemplate ||1562D->getTemplatedKind() == FunctionDecl::TK_FunctionTemplate ||1563D->getTemplatedKind() == FunctionDecl::TK_MemberSpecialization ||1564D->getTemplatedKind() == FunctionDecl::TK_DependentNonTemplate)1565AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1566else if (D->getTemplatedKind() ==1567FunctionDecl::TK_FunctionTemplateSpecialization) {1568FunctionTemplateSpecializationInfo *FTSInfo =1569D->getTemplateSpecializationInfo();15701571if (FTSInfo->TemplateArguments->size() == 1) {1572const TemplateArgument &TA = FTSInfo->TemplateArguments->get(0);1573if (TA.getKind() == TemplateArgument::Type &&1574!FTSInfo->TemplateArgumentsAsWritten &&1575!FTSInfo->getMemberSpecializationInfo())1576AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1577}1578} else if (D->getTemplatedKind() ==1579FunctionDecl::TK_DependentFunctionTemplateSpecialization) {1580DependentFunctionTemplateSpecializationInfo *DFTSInfo =1581D->getDependentSpecializationInfo();1582if (!DFTSInfo->TemplateArgumentsAsWritten)1583AbbrevToUse = Writer.getDeclCXXMethodAbbrev(D->getTemplatedKind());1584}1585}15861587Code = serialization::DECL_CXX_METHOD;1588}15891590void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {1591static_assert(DeclContext::NumCXXConstructorDeclBits == 64,1592"You need to update the serializer after you change the "1593"CXXConstructorDeclBits");15941595Record.push_back(D->getTrailingAllocKind());1596addExplicitSpecifier(D->getExplicitSpecifier(), Record);1597if (auto Inherited = D->getInheritedConstructor()) {1598Record.AddDeclRef(Inherited.getShadowDecl());1599Record.AddDeclRef(Inherited.getConstructor());1600}16011602VisitCXXMethodDecl(D);1603Code = serialization::DECL_CXX_CONSTRUCTOR;1604}16051606void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {1607VisitCXXMethodDecl(D);16081609Record.AddDeclRef(D->getOperatorDelete());1610if (D->getOperatorDelete())1611Record.AddStmt(D->getOperatorDeleteThisArg());16121613Code = serialization::DECL_CXX_DESTRUCTOR;1614}16151616void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {1617addExplicitSpecifier(D->getExplicitSpecifier(), Record);1618VisitCXXMethodDecl(D);1619Code = serialization::DECL_CXX_CONVERSION;1620}16211622void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {1623VisitDecl(D);1624Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));1625ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();1626Record.push_back(!IdentifierLocs.empty());1627if (IdentifierLocs.empty()) {1628Record.AddSourceLocation(D->getEndLoc());1629Record.push_back(1);1630} else {1631for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)1632Record.AddSourceLocation(IdentifierLocs[I]);1633Record.push_back(IdentifierLocs.size());1634}1635// Note: the number of source locations must always be the last element in1636// the record.1637Code = serialization::DECL_IMPORT;1638}16391640void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {1641VisitDecl(D);1642Record.AddSourceLocation(D->getColonLoc());1643Code = serialization::DECL_ACCESS_SPEC;1644}16451646void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {1647// Record the number of friend type template parameter lists here1648// so as to simplify memory allocation during deserialization.1649Record.push_back(D->NumTPLists);1650VisitDecl(D);1651bool hasFriendDecl = D->Friend.is<NamedDecl*>();1652Record.push_back(hasFriendDecl);1653if (hasFriendDecl)1654Record.AddDeclRef(D->getFriendDecl());1655else1656Record.AddTypeSourceInfo(D->getFriendType());1657for (unsigned i = 0; i < D->NumTPLists; ++i)1658Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));1659Record.AddDeclRef(D->getNextFriend());1660Record.push_back(D->UnsupportedFriend);1661Record.AddSourceLocation(D->FriendLoc);1662Code = serialization::DECL_FRIEND;1663}16641665void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {1666VisitDecl(D);1667Record.push_back(D->getNumTemplateParameters());1668for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)1669Record.AddTemplateParameterList(D->getTemplateParameterList(i));1670Record.push_back(D->getFriendDecl() != nullptr);1671if (D->getFriendDecl())1672Record.AddDeclRef(D->getFriendDecl());1673else1674Record.AddTypeSourceInfo(D->getFriendType());1675Record.AddSourceLocation(D->getFriendLoc());1676Code = serialization::DECL_FRIEND_TEMPLATE;1677}16781679void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {1680VisitNamedDecl(D);16811682Record.AddTemplateParameterList(D->getTemplateParameters());1683Record.AddDeclRef(D->getTemplatedDecl());1684}16851686void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {1687VisitTemplateDecl(D);1688Record.AddStmt(D->getConstraintExpr());1689Code = serialization::DECL_CONCEPT;1690}16911692void ASTDeclWriter::VisitImplicitConceptSpecializationDecl(1693ImplicitConceptSpecializationDecl *D) {1694Record.push_back(D->getTemplateArguments().size());1695VisitDecl(D);1696for (const TemplateArgument &Arg : D->getTemplateArguments())1697Record.AddTemplateArgument(Arg);1698Code = serialization::DECL_IMPLICIT_CONCEPT_SPECIALIZATION;1699}17001701void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {1702Code = serialization::DECL_REQUIRES_EXPR_BODY;1703}17041705void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {1706VisitRedeclarable(D);17071708// Emit data to initialize CommonOrPrev before VisitTemplateDecl so that1709// getCommonPtr() can be used while this is still initializing.1710if (D->isFirstDecl()) {1711// This declaration owns the 'common' pointer, so serialize that data now.1712Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());1713if (D->getInstantiatedFromMemberTemplate())1714Record.push_back(D->isMemberSpecialization());1715}17161717VisitTemplateDecl(D);1718Record.push_back(D->getIdentifierNamespace());1719}17201721void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {1722VisitRedeclarableTemplateDecl(D);17231724if (D->isFirstDecl())1725AddTemplateSpecializations(D);17261727// Force emitting the corresponding deduction guide in reduced BMI mode.1728// Otherwise, the deduction guide may be optimized out incorrectly.1729if (Writer.isGeneratingReducedBMI()) {1730auto Name = Context.DeclarationNames.getCXXDeductionGuideName(D);1731for (auto *DG : D->getDeclContext()->noload_lookup(Name))1732Writer.GetDeclRef(DG->getCanonicalDecl());1733}17341735Code = serialization::DECL_CLASS_TEMPLATE;1736}17371738void ASTDeclWriter::VisitClassTemplateSpecializationDecl(1739ClassTemplateSpecializationDecl *D) {1740RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);17411742VisitCXXRecordDecl(D);17431744llvm::PointerUnion<ClassTemplateDecl *,1745ClassTemplatePartialSpecializationDecl *> InstFrom1746= D->getSpecializedTemplateOrPartial();1747if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {1748Record.AddDeclRef(InstFromD);1749} else {1750Record.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>());1751Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());1752}17531754Record.AddTemplateArgumentList(&D->getTemplateArgs());1755Record.AddSourceLocation(D->getPointOfInstantiation());1756Record.push_back(D->getSpecializationKind());1757Record.push_back(D->isCanonicalDecl());17581759if (D->isCanonicalDecl()) {1760// When reading, we'll add it to the folding set of the following template.1761Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());1762}17631764bool ExplicitInstantiation =1765D->getTemplateSpecializationKind() ==1766TSK_ExplicitInstantiationDeclaration ||1767D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;1768Record.push_back(ExplicitInstantiation);1769if (ExplicitInstantiation) {1770Record.AddSourceLocation(D->getExternKeywordLoc());1771Record.AddSourceLocation(D->getTemplateKeywordLoc());1772}17731774const ASTTemplateArgumentListInfo *ArgsWritten =1775D->getTemplateArgsAsWritten();1776Record.push_back(!!ArgsWritten);1777if (ArgsWritten)1778Record.AddASTTemplateArgumentListInfo(ArgsWritten);17791780Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;1781}17821783void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(1784ClassTemplatePartialSpecializationDecl *D) {1785Record.AddTemplateParameterList(D->getTemplateParameters());17861787VisitClassTemplateSpecializationDecl(D);17881789// These are read/set from/to the first declaration.1790if (D->getPreviousDecl() == nullptr) {1791Record.AddDeclRef(D->getInstantiatedFromMember());1792Record.push_back(D->isMemberSpecialization());1793}17941795Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;1796}17971798void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {1799VisitRedeclarableTemplateDecl(D);18001801if (D->isFirstDecl())1802AddTemplateSpecializations(D);1803Code = serialization::DECL_VAR_TEMPLATE;1804}18051806void ASTDeclWriter::VisitVarTemplateSpecializationDecl(1807VarTemplateSpecializationDecl *D) {1808RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);18091810llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>1811InstFrom = D->getSpecializedTemplateOrPartial();1812if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {1813Record.AddDeclRef(InstFromD);1814} else {1815Record.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>());1816Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());1817}18181819bool ExplicitInstantiation =1820D->getTemplateSpecializationKind() ==1821TSK_ExplicitInstantiationDeclaration ||1822D->getTemplateSpecializationKind() == TSK_ExplicitInstantiationDefinition;1823Record.push_back(ExplicitInstantiation);1824if (ExplicitInstantiation) {1825Record.AddSourceLocation(D->getExternKeywordLoc());1826Record.AddSourceLocation(D->getTemplateKeywordLoc());1827}18281829const ASTTemplateArgumentListInfo *ArgsWritten =1830D->getTemplateArgsAsWritten();1831Record.push_back(!!ArgsWritten);1832if (ArgsWritten)1833Record.AddASTTemplateArgumentListInfo(ArgsWritten);18341835Record.AddTemplateArgumentList(&D->getTemplateArgs());1836Record.AddSourceLocation(D->getPointOfInstantiation());1837Record.push_back(D->getSpecializationKind());1838Record.push_back(D->IsCompleteDefinition);18391840VisitVarDecl(D);18411842Record.push_back(D->isCanonicalDecl());18431844if (D->isCanonicalDecl()) {1845// When reading, we'll add it to the folding set of the following template.1846Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());1847}18481849Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;1850}18511852void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(1853VarTemplatePartialSpecializationDecl *D) {1854Record.AddTemplateParameterList(D->getTemplateParameters());18551856VisitVarTemplateSpecializationDecl(D);18571858// These are read/set from/to the first declaration.1859if (D->getPreviousDecl() == nullptr) {1860Record.AddDeclRef(D->getInstantiatedFromMember());1861Record.push_back(D->isMemberSpecialization());1862}18631864Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;1865}18661867void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {1868VisitRedeclarableTemplateDecl(D);18691870if (D->isFirstDecl())1871AddTemplateSpecializations(D);1872Code = serialization::DECL_FUNCTION_TEMPLATE;1873}18741875void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {1876Record.push_back(D->hasTypeConstraint());1877VisitTypeDecl(D);18781879Record.push_back(D->wasDeclaredWithTypename());18801881const TypeConstraint *TC = D->getTypeConstraint();1882Record.push_back(/*TypeConstraintInitialized=*/TC != nullptr);1883if (TC) {1884auto *CR = TC->getConceptReference();1885Record.push_back(CR != nullptr);1886if (CR)1887Record.AddConceptReference(CR);1888Record.AddStmt(TC->getImmediatelyDeclaredConstraint());1889Record.push_back(D->isExpandedParameterPack());1890if (D->isExpandedParameterPack())1891Record.push_back(D->getNumExpansionParameters());1892}18931894bool OwnsDefaultArg = D->hasDefaultArgument() &&1895!D->defaultArgumentWasInherited();1896Record.push_back(OwnsDefaultArg);1897if (OwnsDefaultArg)1898Record.AddTemplateArgumentLoc(D->getDefaultArgument());18991900if (!D->hasTypeConstraint() && !OwnsDefaultArg &&1901D->getDeclContext() == D->getLexicalDeclContext() &&1902!D->isInvalidDecl() && !D->hasAttrs() &&1903!D->isTopLevelDeclInObjCContainer() && !D->isImplicit() &&1904D->getDeclName().getNameKind() == DeclarationName::Identifier)1905AbbrevToUse = Writer.getDeclTemplateTypeParmAbbrev();19061907Code = serialization::DECL_TEMPLATE_TYPE_PARM;1908}19091910void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {1911// For an expanded parameter pack, record the number of expansion types here1912// so that it's easier for deserialization to allocate the right amount of1913// memory.1914Expr *TypeConstraint = D->getPlaceholderTypeConstraint();1915Record.push_back(!!TypeConstraint);1916if (D->isExpandedParameterPack())1917Record.push_back(D->getNumExpansionTypes());19181919VisitDeclaratorDecl(D);1920// TemplateParmPosition.1921Record.push_back(D->getDepth());1922Record.push_back(D->getPosition());1923if (TypeConstraint)1924Record.AddStmt(TypeConstraint);19251926if (D->isExpandedParameterPack()) {1927for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {1928Record.AddTypeRef(D->getExpansionType(I));1929Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));1930}19311932Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;1933} else {1934// Rest of NonTypeTemplateParmDecl.1935Record.push_back(D->isParameterPack());1936bool OwnsDefaultArg = D->hasDefaultArgument() &&1937!D->defaultArgumentWasInherited();1938Record.push_back(OwnsDefaultArg);1939if (OwnsDefaultArg)1940Record.AddTemplateArgumentLoc(D->getDefaultArgument());1941Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;1942}1943}19441945void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {1946// For an expanded parameter pack, record the number of expansion types here1947// so that it's easier for deserialization to allocate the right amount of1948// memory.1949if (D->isExpandedParameterPack())1950Record.push_back(D->getNumExpansionTemplateParameters());19511952VisitTemplateDecl(D);1953Record.push_back(D->wasDeclaredWithTypename());1954// TemplateParmPosition.1955Record.push_back(D->getDepth());1956Record.push_back(D->getPosition());19571958if (D->isExpandedParameterPack()) {1959for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();1960I != N; ++I)1961Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));1962Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;1963} else {1964// Rest of TemplateTemplateParmDecl.1965Record.push_back(D->isParameterPack());1966bool OwnsDefaultArg = D->hasDefaultArgument() &&1967!D->defaultArgumentWasInherited();1968Record.push_back(OwnsDefaultArg);1969if (OwnsDefaultArg)1970Record.AddTemplateArgumentLoc(D->getDefaultArgument());1971Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;1972}1973}19741975void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {1976VisitRedeclarableTemplateDecl(D);1977Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;1978}19791980void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {1981VisitDecl(D);1982Record.AddStmt(D->getAssertExpr());1983Record.push_back(D->isFailed());1984Record.AddStmt(D->getMessage());1985Record.AddSourceLocation(D->getRParenLoc());1986Code = serialization::DECL_STATIC_ASSERT;1987}19881989/// Emit the DeclContext part of a declaration context decl.1990void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {1991static_assert(DeclContext::NumDeclContextBits == 13,1992"You need to update the serializer after you change the "1993"DeclContextBits");19941995uint64_t LexicalOffset = 0;1996uint64_t VisibleOffset = 0;19971998if (Writer.isGeneratingReducedBMI() && isa<NamespaceDecl>(DC) &&1999cast<NamespaceDecl>(DC)->isFromExplicitGlobalModule()) {2000// In reduced BMI, delay writing lexical and visible block for namespace2001// in the global module fragment. See the comments of DelayedNamespace for2002// details.2003Writer.DelayedNamespace.push_back(cast<NamespaceDecl>(DC));2004} else {2005LexicalOffset = Writer.WriteDeclContextLexicalBlock(Context, DC);2006VisibleOffset = Writer.WriteDeclContextVisibleBlock(Context, DC);2007}20082009Record.AddOffset(LexicalOffset);2010Record.AddOffset(VisibleOffset);2011}20122013const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {2014assert(IsLocalDecl(D) && "expected a local declaration");20152016const Decl *Canon = D->getCanonicalDecl();2017if (IsLocalDecl(Canon))2018return Canon;20192020const Decl *&CacheEntry = FirstLocalDeclCache[Canon];2021if (CacheEntry)2022return CacheEntry;20232024for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())2025if (IsLocalDecl(Redecl))2026D = Redecl;2027return CacheEntry = D;2028}20292030template <typename T>2031void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {2032T *First = D->getFirstDecl();2033T *MostRecent = First->getMostRecentDecl();2034T *DAsT = static_cast<T *>(D);2035if (MostRecent != First) {2036assert(isRedeclarableDeclKind(DAsT->getKind()) &&2037"Not considered redeclarable?");20382039Record.AddDeclRef(First);20402041// Write out a list of local redeclarations of this declaration if it's the2042// first local declaration in the chain.2043const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);2044if (DAsT == FirstLocal) {2045// Emit a list of all imported first declarations so that we can be sure2046// that all redeclarations visible to this module are before D in the2047// redecl chain.2048unsigned I = Record.size();2049Record.push_back(0);2050if (Writer.Chain)2051AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);2052// This is the number of imported first declarations + 1.2053Record[I] = Record.size() - I;20542055// Collect the set of local redeclarations of this declaration, from2056// newest to oldest.2057ASTWriter::RecordData LocalRedecls;2058ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);2059for (const Decl *Prev = FirstLocal->getMostRecentDecl();2060Prev != FirstLocal; Prev = Prev->getPreviousDecl())2061if (!Prev->isFromASTFile())2062LocalRedeclWriter.AddDeclRef(Prev);20632064// If we have any redecls, write them now as a separate record preceding2065// the declaration itself.2066if (LocalRedecls.empty())2067Record.push_back(0);2068else2069Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));2070} else {2071Record.push_back(0);2072Record.AddDeclRef(FirstLocal);2073}20742075// Make sure that we serialize both the previous and the most-recent2076// declarations, which (transitively) ensures that all declarations in the2077// chain get serialized.2078//2079// FIXME: This is not correct; when we reach an imported declaration we2080// won't emit its previous declaration.2081(void)Writer.GetDeclRef(D->getPreviousDecl());2082(void)Writer.GetDeclRef(MostRecent);2083} else {2084// We use the sentinel value 0 to indicate an only declaration.2085Record.push_back(0);2086}2087}20882089void ASTDeclWriter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {2090VisitNamedDecl(D);2091VisitDeclContext(D);2092Record.push_back(D->isCBuffer());2093Record.AddSourceLocation(D->getLocStart());2094Record.AddSourceLocation(D->getLBraceLoc());2095Record.AddSourceLocation(D->getRBraceLoc());20962097Code = serialization::DECL_HLSL_BUFFER;2098}20992100void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {2101Record.writeOMPChildren(D->Data);2102VisitDecl(D);2103Code = serialization::DECL_OMP_THREADPRIVATE;2104}21052106void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {2107Record.writeOMPChildren(D->Data);2108VisitDecl(D);2109Code = serialization::DECL_OMP_ALLOCATE;2110}21112112void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {2113Record.writeOMPChildren(D->Data);2114VisitDecl(D);2115Code = serialization::DECL_OMP_REQUIRES;2116}21172118void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {2119static_assert(DeclContext::NumOMPDeclareReductionDeclBits == 15,2120"You need to update the serializer after you change the "2121"NumOMPDeclareReductionDeclBits");21222123VisitValueDecl(D);2124Record.AddSourceLocation(D->getBeginLoc());2125Record.AddStmt(D->getCombinerIn());2126Record.AddStmt(D->getCombinerOut());2127Record.AddStmt(D->getCombiner());2128Record.AddStmt(D->getInitOrig());2129Record.AddStmt(D->getInitPriv());2130Record.AddStmt(D->getInitializer());2131Record.push_back(llvm::to_underlying(D->getInitializerKind()));2132Record.AddDeclRef(D->getPrevDeclInScope());2133Code = serialization::DECL_OMP_DECLARE_REDUCTION;2134}21352136void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {2137Record.writeOMPChildren(D->Data);2138VisitValueDecl(D);2139Record.AddDeclarationName(D->getVarName());2140Record.AddDeclRef(D->getPrevDeclInScope());2141Code = serialization::DECL_OMP_DECLARE_MAPPER;2142}21432144void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {2145VisitVarDecl(D);2146Code = serialization::DECL_OMP_CAPTUREDEXPR;2147}21482149//===----------------------------------------------------------------------===//2150// ASTWriter Implementation2151//===----------------------------------------------------------------------===//21522153namespace {2154template <FunctionDecl::TemplatedKind Kind>2155std::shared_ptr<llvm::BitCodeAbbrev>2156getFunctionDeclAbbrev(serialization::DeclCode Code) {2157using namespace llvm;21582159auto Abv = std::make_shared<BitCodeAbbrev>();2160Abv->Add(BitCodeAbbrevOp(Code));2161// RedeclarableDecl2162Abv->Add(BitCodeAbbrevOp(0)); // CanonicalDecl2163Abv->Add(BitCodeAbbrevOp(Kind));2164if constexpr (Kind == FunctionDecl::TK_NonTemplate) {21652166} else if constexpr (Kind == FunctionDecl::TK_FunctionTemplate) {2167// DescribedFunctionTemplate2168Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2169} else if constexpr (Kind == FunctionDecl::TK_DependentNonTemplate) {2170// Instantiated From Decl2171Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2172} else if constexpr (Kind == FunctionDecl::TK_MemberSpecialization) {2173Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedFrom2174Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,21753)); // TemplateSpecializationKind2176Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Specialized Location2177} else if constexpr (Kind ==2178FunctionDecl::TK_FunctionTemplateSpecialization) {2179Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template2180Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,21813)); // TemplateSpecializationKind2182Abv->Add(BitCodeAbbrevOp(1)); // Template Argument Size2183Abv->Add(BitCodeAbbrevOp(TemplateArgument::Type)); // Template Argument Kind2184Abv->Add(2185BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Template Argument Type2186Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Is Defaulted2187Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten2188Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2189Abv->Add(BitCodeAbbrevOp(0));2190Abv->Add(2191BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Canonical Decl of template2192} else if constexpr (Kind == FunctionDecl::2193TK_DependentFunctionTemplateSpecialization) {2194// Candidates of specialization2195Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2196Abv->Add(BitCodeAbbrevOp(0)); // TemplateArgumentsAsWritten2197} else {2198llvm_unreachable("Unknown templated kind?");2199}2200// Decl2201Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,22028)); // Packed DeclBits: ModuleOwnershipKind,2203// isUsed, isReferenced, AccessSpecifier,2204// isImplicit2205//2206// The following bits should be 0:2207// HasStandaloneLexicalDC, HasAttrs,2208// TopLevelDeclInObjCContainer,2209// isInvalidDecl2210Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2211Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2212// NamedDecl2213Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind2214Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Identifier2215Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2216// ValueDecl2217Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2218// DeclaratorDecl2219Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerLocStart2220Abv->Add(BitCodeAbbrevOp(0)); // HasExtInfo2221Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2222// FunctionDecl2223Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS2224Abv->Add(BitCodeAbbrevOp(2225BitCodeAbbrevOp::Fixed,222628)); // Packed Function Bits: StorageClass, Inline, InlineSpecified,2227// VirtualAsWritten, Pure, HasInheritedProto, HasWrittenProto,2228// Deleted, Trivial, TrivialForCall, Defaulted, ExplicitlyDefaulted,2229// IsIneligibleOrNotSelected, ImplicitReturnZero, Constexpr,2230// UsesSEHTry, SkippedBody, MultiVersion, LateParsed,2231// FriendConstraintRefersToEnclosingTemplate, Linkage,2232// ShouldSkipCheckingODR2233Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LocEnd2234Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash2235// This Array slurps the rest of the record. Fortunately we want to encode2236// (nearly) all the remaining (variable number of) fields in the same way.2237//2238// This is:2239// NumParams and Params[] from FunctionDecl, and2240// NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.2241//2242// Add an AbbrevOp for 'size then elements' and use it here.2243Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2244Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));2245return Abv;2246}22472248template <FunctionDecl::TemplatedKind Kind>2249std::shared_ptr<llvm::BitCodeAbbrev> getCXXMethodAbbrev() {2250return getFunctionDeclAbbrev<Kind>(serialization::DECL_CXX_METHOD);2251}2252} // namespace22532254void ASTWriter::WriteDeclAbbrevs() {2255using namespace llvm;22562257std::shared_ptr<BitCodeAbbrev> Abv;22582259// Abbreviation for DECL_FIELD2260Abv = std::make_shared<BitCodeAbbrev>();2261Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));2262// Decl2263Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,22647)); // Packed DeclBits: ModuleOwnershipKind,2265// isUsed, isReferenced, AccessSpecifier,2266//2267// The following bits should be 0:2268// isImplicit, HasStandaloneLexicalDC, HasAttrs,2269// TopLevelDeclInObjCContainer,2270// isInvalidDecl2271Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2272Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2273// NamedDecl2274Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2275Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2276Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2277// ValueDecl2278Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2279// DeclaratorDecl2280Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2281Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo2282Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2283// FieldDecl2284Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable2285Abv->Add(BitCodeAbbrevOp(0)); // StorageKind2286// Type Source Info2287Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2288Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2289DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));22902291// Abbreviation for DECL_OBJC_IVAR2292Abv = std::make_shared<BitCodeAbbrev>();2293Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));2294// Decl2295Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,229612)); // Packed DeclBits: HasStandaloneLexicalDC,2297// isInvalidDecl, HasAttrs, isImplicit, isUsed,2298// isReferenced, TopLevelDeclInObjCContainer,2299// AccessSpecifier, ModuleOwnershipKind2300Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2301Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2302// NamedDecl2303Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2304Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2305Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2306// ValueDecl2307Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2308// DeclaratorDecl2309Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2310Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo2311Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2312// FieldDecl2313Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable2314Abv->Add(BitCodeAbbrevOp(0)); // InitStyle2315// ObjC Ivar2316Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl2317Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize2318// Type Source Info2319Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2320Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2321DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));23222323// Abbreviation for DECL_ENUM2324Abv = std::make_shared<BitCodeAbbrev>();2325Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));2326// Redeclarable2327Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2328// Decl2329Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,23307)); // Packed DeclBits: ModuleOwnershipKind,2331// isUsed, isReferenced, AccessSpecifier,2332//2333// The following bits should be 0:2334// isImplicit, HasStandaloneLexicalDC, HasAttrs,2335// TopLevelDeclInObjCContainer,2336// isInvalidDecl2337Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2338Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2339// NamedDecl2340Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2341Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2342Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2343// TypeDecl2344Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2345Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2346// TagDecl2347Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace2348Abv->Add(BitCodeAbbrevOp(2349BitCodeAbbrevOp::Fixed,23509)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,2351// EmbeddedInDeclarator, IsFreeStanding,2352// isCompleteDefinitionRequired, ExtInfoKind2353Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2354Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2355// EnumDecl2356Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // AddTypeRef2357Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IntegerType2358Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getPromotionType2359Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 20)); // Enum Decl Bits2360Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash2361Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InstantiatedMembEnum2362// DC2363Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset2364Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset2365DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));23662367// Abbreviation for DECL_RECORD2368Abv = std::make_shared<BitCodeAbbrev>();2369Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));2370// Redeclarable2371Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2372// Decl2373Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,23747)); // Packed DeclBits: ModuleOwnershipKind,2375// isUsed, isReferenced, AccessSpecifier,2376//2377// The following bits should be 0:2378// isImplicit, HasStandaloneLexicalDC, HasAttrs,2379// TopLevelDeclInObjCContainer,2380// isInvalidDecl2381Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2382Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2383// NamedDecl2384Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2385Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2386Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2387// TypeDecl2388Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2389Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2390// TagDecl2391Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // IdentifierNamespace2392Abv->Add(BitCodeAbbrevOp(2393BitCodeAbbrevOp::Fixed,23949)); // Packed Tag Decl Bits: getTagKind, isCompleteDefinition,2395// EmbeddedInDeclarator, IsFreeStanding,2396// isCompleteDefinitionRequired, ExtInfoKind2397Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2398Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SourceLocation2399// RecordDecl2400Abv->Add(BitCodeAbbrevOp(2401BitCodeAbbrevOp::Fixed,240213)); // Packed Record Decl Bits: FlexibleArrayMember,2403// AnonymousStructUnion, hasObjectMember, hasVolatileMember,2404// isNonTrivialToPrimitiveDefaultInitialize,2405// isNonTrivialToPrimitiveCopy, isNonTrivialToPrimitiveDestroy,2406// hasNonTrivialToPrimitiveDefaultInitializeCUnion,2407// hasNonTrivialToPrimitiveDestructCUnion,2408// hasNonTrivialToPrimitiveCopyCUnion, isParamDestroyedInCallee,2409// getArgPassingRestrictions2410// ODRHash2411Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26));24122413// DC2414Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalOffset2415Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // VisibleOffset2416DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));24172418// Abbreviation for DECL_PARM_VAR2419Abv = std::make_shared<BitCodeAbbrev>();2420Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));2421// Redeclarable2422Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2423// Decl2424Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,24258)); // Packed DeclBits: ModuleOwnershipKind, isUsed,2426// isReferenced, AccessSpecifier,2427// HasStandaloneLexicalDC, HasAttrs, isImplicit,2428// TopLevelDeclInObjCContainer,2429// isInvalidDecl,2430Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2431Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2432// NamedDecl2433Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2434Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2435Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2436// ValueDecl2437Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2438// DeclaratorDecl2439Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2440Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo2441Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2442// VarDecl2443Abv->Add(2444BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,244512)); // Packed Var Decl bits: SClass, TSCSpec, InitStyle,2446// isARCPseudoStrong, Linkage, ModulesCodegen2447Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)2448// ParmVarDecl2449Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex2450Abv->Add(BitCodeAbbrevOp(2451BitCodeAbbrevOp::Fixed,245219)); // Packed Parm Var Decl bits: IsObjCMethodParameter, ScopeDepth,2453// ObjCDeclQualifier, KNRPromoted,2454// HasInheritedDefaultArg, HasUninstantiatedDefaultArg2455// Type Source Info2456Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2457Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2458DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));24592460// Abbreviation for DECL_TYPEDEF2461Abv = std::make_shared<BitCodeAbbrev>();2462Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));2463// Redeclarable2464Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2465// Decl2466Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,24677)); // Packed DeclBits: ModuleOwnershipKind,2468// isReferenced, isUsed, AccessSpecifier. Other2469// higher bits should be 0: isImplicit,2470// HasStandaloneLexicalDC, HasAttrs,2471// TopLevelDeclInObjCContainer, isInvalidDecl2472Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2473Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2474// NamedDecl2475Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2476Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2477Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2478// TypeDecl2479Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2480Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2481// TypedefDecl2482Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2483Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2484DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));24852486// Abbreviation for DECL_VAR2487Abv = std::make_shared<BitCodeAbbrev>();2488Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));2489// Redeclarable2490Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2491// Decl2492Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,249312)); // Packed DeclBits: HasStandaloneLexicalDC,2494// isInvalidDecl, HasAttrs, isImplicit, isUsed,2495// isReferenced, TopLevelDeclInObjCContainer,2496// AccessSpecifier, ModuleOwnershipKind2497Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2498Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2499// NamedDecl2500Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2501Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2502Abv->Add(BitCodeAbbrevOp(0)); // AnonDeclNumber2503// ValueDecl2504Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2505// DeclaratorDecl2506Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc2507Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo2508Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType2509// VarDecl2510Abv->Add(BitCodeAbbrevOp(2511BitCodeAbbrevOp::Fixed,251221)); // Packed Var Decl bits: Linkage, ModulesCodegen,2513// SClass, TSCSpec, InitStyle,2514// isARCPseudoStrong, IsThisDeclarationADemotedDefinition,2515// isExceptionVariable, isNRVOVariable, isCXXForRangeDecl,2516// isInline, isInlineSpecified, isConstexpr,2517// isInitCapture, isPrevDeclInSameScope,2518// EscapingByref, HasDeducedType, ImplicitParamKind, isObjCForDecl2519Abv->Add(BitCodeAbbrevOp(0)); // VarKind (local enum)2520// Type Source Info2521Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));2522Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc2523DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));25242525// Abbreviation for DECL_CXX_METHOD2526DeclCXXMethodAbbrev =2527Stream.EmitAbbrev(getCXXMethodAbbrev<FunctionDecl::TK_NonTemplate>());2528DeclTemplateCXXMethodAbbrev = Stream.EmitAbbrev(2529getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplate>());2530DeclDependentNonTemplateCXXMethodAbbrev = Stream.EmitAbbrev(2531getCXXMethodAbbrev<FunctionDecl::TK_DependentNonTemplate>());2532DeclMemberSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(2533getCXXMethodAbbrev<FunctionDecl::TK_MemberSpecialization>());2534DeclTemplateSpecializedCXXMethodAbbrev = Stream.EmitAbbrev(2535getCXXMethodAbbrev<FunctionDecl::TK_FunctionTemplateSpecialization>());2536DeclDependentSpecializationCXXMethodAbbrev = Stream.EmitAbbrev(2537getCXXMethodAbbrev<2538FunctionDecl::TK_DependentFunctionTemplateSpecialization>());25392540// Abbreviation for DECL_TEMPLATE_TYPE_PARM2541Abv = std::make_shared<BitCodeAbbrev>();2542Abv->Add(BitCodeAbbrevOp(serialization::DECL_TEMPLATE_TYPE_PARM));2543Abv->Add(BitCodeAbbrevOp(0)); // hasTypeConstraint2544// Decl2545Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,25467)); // Packed DeclBits: ModuleOwnershipKind,2547// isReferenced, isUsed, AccessSpecifier. Other2548// higher bits should be 0: isImplicit,2549// HasStandaloneLexicalDC, HasAttrs,2550// TopLevelDeclInObjCContainer, isInvalidDecl2551Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2552Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2553// NamedDecl2554Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2555Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2556Abv->Add(BitCodeAbbrevOp(0));2557// TypeDecl2558Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2559Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref2560// TemplateTypeParmDecl2561Abv->Add(2562BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // wasDeclaredWithTypename2563Abv->Add(BitCodeAbbrevOp(0)); // TypeConstraintInitialized2564Abv->Add(BitCodeAbbrevOp(0)); // OwnsDefaultArg2565DeclTemplateTypeParmAbbrev = Stream.EmitAbbrev(std::move(Abv));25662567// Abbreviation for DECL_USING_SHADOW2568Abv = std::make_shared<BitCodeAbbrev>();2569Abv->Add(BitCodeAbbrevOp(serialization::DECL_USING_SHADOW));2570// Redeclarable2571Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration2572// Decl2573Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed,257412)); // Packed DeclBits: HasStandaloneLexicalDC,2575// isInvalidDecl, HasAttrs, isImplicit, isUsed,2576// isReferenced, TopLevelDeclInObjCContainer,2577// AccessSpecifier, ModuleOwnershipKind2578Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext2579Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID2580// NamedDecl2581Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier2582Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name2583Abv->Add(BitCodeAbbrevOp(0));2584// UsingShadowDecl2585Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TargetDecl2586Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS2587Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // UsingOrNextShadow2588Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR,25896)); // InstantiatedFromUsingShadowDecl2590DeclUsingShadowAbbrev = Stream.EmitAbbrev(std::move(Abv));25912592// Abbreviation for EXPR_DECL_REF2593Abv = std::make_shared<BitCodeAbbrev>();2594Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));2595// Stmt2596// Expr2597// PackingBits: DependenceKind, ValueKind. ObjectKind should be 0.2598Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 7));2599Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2600// DeclRefExpr2601// Packing Bits: , HadMultipleCandidates, RefersToEnclosingVariableOrCapture,2602// IsImmediateEscalating, NonOdrUseReason.2603// GetDeclFound, HasQualifier and ExplicitTemplateArgs should be 0.2604Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2605Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef2606Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2607DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));26082609// Abbreviation for EXPR_INTEGER_LITERAL2610Abv = std::make_shared<BitCodeAbbrev>();2611Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));2612//Stmt2613// Expr2614// DependenceKind, ValueKind, ObjectKind2615Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2616Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2617// Integer Literal2618Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2619Abv->Add(BitCodeAbbrevOp(32)); // Bit Width2620Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value2621IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));26222623// Abbreviation for EXPR_CHARACTER_LITERAL2624Abv = std::make_shared<BitCodeAbbrev>();2625Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));2626//Stmt2627// Expr2628// DependenceKind, ValueKind, ObjectKind2629Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2630Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2631// Character Literal2632Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue2633Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location2634Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind2635CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));26362637// Abbreviation for EXPR_IMPLICIT_CAST2638Abv = std::make_shared<BitCodeAbbrev>();2639Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));2640// Stmt2641// Expr2642// Packing Bits: DependenceKind, ValueKind, ObjectKind,2643Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2644Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2645// CastExpr2646Abv->Add(BitCodeAbbrevOp(0)); // PathSize2647// Packing Bits: CastKind, StoredFPFeatures, isPartOfExplicitCast2648Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 9));2649// ImplicitCastExpr2650ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));26512652// Abbreviation for EXPR_BINARY_OPERATOR2653Abv = std::make_shared<BitCodeAbbrev>();2654Abv->Add(BitCodeAbbrevOp(serialization::EXPR_BINARY_OPERATOR));2655// Stmt2656// Expr2657// Packing Bits: DependenceKind. ValueKind and ObjectKind should2658// be 0 in this case.2659Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2660Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2661// BinaryOperator2662Abv->Add(2663BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures2664Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2665BinaryOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));26662667// Abbreviation for EXPR_COMPOUND_ASSIGN_OPERATOR2668Abv = std::make_shared<BitCodeAbbrev>();2669Abv->Add(BitCodeAbbrevOp(serialization::EXPR_COMPOUND_ASSIGN_OPERATOR));2670// Stmt2671// Expr2672// Packing Bits: DependenceKind. ValueKind and ObjectKind should2673// be 0 in this case.2674Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 5));2675Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2676// BinaryOperator2677// Packing Bits: OpCode. The HasFPFeatures bit should be 02678Abv->Add(2679BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // OpCode and HasFPFeatures2680Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2681// CompoundAssignOperator2682Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LHSType2683Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Result Type2684CompoundAssignOperatorAbbrev = Stream.EmitAbbrev(std::move(Abv));26852686// Abbreviation for EXPR_CALL2687Abv = std::make_shared<BitCodeAbbrev>();2688Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CALL));2689// Stmt2690// Expr2691// Packing Bits: DependenceKind, ValueKind, ObjectKind,2692Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2693Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2694// CallExpr2695Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2696Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind2697Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2698CallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));26992700// Abbreviation for EXPR_CXX_OPERATOR_CALL2701Abv = std::make_shared<BitCodeAbbrev>();2702Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_OPERATOR_CALL));2703// Stmt2704// Expr2705// Packing Bits: DependenceKind, ValueKind, ObjectKind,2706Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2707Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2708// CallExpr2709Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2710Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind2711Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2712// CXXOperatorCallExpr2713Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Operator Kind2714Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2715Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2716CXXOperatorCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));27172718// Abbreviation for EXPR_CXX_MEMBER_CALL2719Abv = std::make_shared<BitCodeAbbrev>();2720Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CXX_MEMBER_CALL));2721// Stmt2722// Expr2723// Packing Bits: DependenceKind, ValueKind, ObjectKind,2724Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 10));2725Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type2726// CallExpr2727Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // NumArgs2728Abv->Add(BitCodeAbbrevOp(0)); // ADLCallKind2729Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2730// CXXMemberCallExpr2731CXXMemberCallExprAbbrev = Stream.EmitAbbrev(std::move(Abv));27322733// Abbreviation for STMT_COMPOUND2734Abv = std::make_shared<BitCodeAbbrev>();2735Abv->Add(BitCodeAbbrevOp(serialization::STMT_COMPOUND));2736// Stmt2737// CompoundStmt2738Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Num Stmts2739Abv->Add(BitCodeAbbrevOp(0)); // hasStoredFPFeatures2740Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2741Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location2742CompoundStmtAbbrev = Stream.EmitAbbrev(std::move(Abv));27432744Abv = std::make_shared<BitCodeAbbrev>();2745Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));2746Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2747DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));27482749Abv = std::make_shared<BitCodeAbbrev>();2750Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));2751Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));2752DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));2753}27542755/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by2756/// consumers of the AST.2757///2758/// Such decls will always be deserialized from the AST file, so we would like2759/// this to be as restrictive as possible. Currently the predicate is driven by2760/// code generation requirements, if other clients have a different notion of2761/// what is "required" then we may have to consider an alternate scheme where2762/// clients can iterate over the top-level decls and get information on them,2763/// without necessary deserializing them. We could explicitly require such2764/// clients to use a separate API call to "realize" the decl. This should be2765/// relatively painless since they would presumably only do it for top-level2766/// decls.2767static bool isRequiredDecl(const Decl *D, ASTContext &Context,2768Module *WritingModule) {2769// Named modules have different semantics than header modules. Every named2770// module units owns a translation unit. So the importer of named modules2771// doesn't need to deserilize everything ahead of time.2772if (WritingModule && WritingModule->isNamedModule()) {2773// The PragmaCommentDecl and PragmaDetectMismatchDecl are MSVC's extension.2774// And the behavior of MSVC for such cases will leak this to the module2775// users. Given pragma is not a standard thing, the compiler has the space2776// to do their own decision. Let's follow MSVC here.2777if (isa<PragmaCommentDecl, PragmaDetectMismatchDecl>(D))2778return true;2779return false;2780}27812782// An ObjCMethodDecl is never considered as "required" because its2783// implementation container always is.27842785// File scoped assembly or obj-c or OMP declare target implementation must be2786// seen.2787if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCImplDecl>(D))2788return true;27892790if (WritingModule && isPartOfPerModuleInitializer(D)) {2791// These declarations are part of the module initializer, and are emitted2792// if and when the module is imported, rather than being emitted eagerly.2793return false;2794}27952796return Context.DeclMustBeEmitted(D);2797}27982799void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {2800PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),2801"serializing");28022803// Determine the ID for this declaration.2804LocalDeclID ID;2805assert(!D->isFromASTFile() && "should not be emitting imported decl");2806LocalDeclID &IDR = DeclIDs[D];2807if (IDR.isInvalid())2808IDR = NextDeclID++;28092810ID = IDR;28112812assert(ID >= FirstDeclID && "invalid decl ID");28132814RecordData Record;2815ASTDeclWriter W(*this, Context, Record, GeneratingReducedBMI);28162817// Build a record for this declaration2818W.Visit(D);28192820// Emit this declaration to the bitstream.2821uint64_t Offset = W.Emit(D);28222823// Record the offset for this declaration2824SourceLocation Loc = D->getLocation();2825SourceLocationEncoding::RawLocEncoding RawLoc =2826getRawSourceLocationEncoding(getAdjustedLocation(Loc));28272828unsigned Index = ID.getRawValue() - FirstDeclID.getRawValue();2829if (DeclOffsets.size() == Index)2830DeclOffsets.emplace_back(RawLoc, Offset, DeclTypesBlockStartOffset);2831else if (DeclOffsets.size() < Index) {2832// FIXME: Can/should this happen?2833DeclOffsets.resize(Index+1);2834DeclOffsets[Index].setRawLoc(RawLoc);2835DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);2836} else {2837llvm_unreachable("declarations should be emitted in ID order");2838}28392840SourceManager &SM = Context.getSourceManager();2841if (Loc.isValid() && SM.isLocalSourceLocation(Loc))2842associateDeclWithFile(D, ID);28432844// Note declarations that should be deserialized eagerly so that we can add2845// them to a record in the AST file later.2846if (isRequiredDecl(D, Context, WritingModule))2847AddDeclRef(D, EagerlyDeserializedDecls);2848}28492850void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {2851// Switch case IDs are per function body.2852Writer->ClearSwitchCaseIDs();28532854assert(FD->doesThisDeclarationHaveABody());2855bool ModulesCodegen = false;2856if (!FD->isDependentContext()) {2857std::optional<GVALinkage> Linkage;2858if (Writer->WritingModule &&2859Writer->WritingModule->isInterfaceOrPartition()) {2860// When building a C++20 module interface unit or a partition unit, a2861// strong definition in the module interface is provided by the2862// compilation of that unit, not by its users. (Inline functions are still2863// emitted in module users.)2864Linkage = Writer->Context->GetGVALinkageForFunction(FD);2865ModulesCodegen = *Linkage >= GVA_StrongExternal;2866}2867if (Writer->Context->getLangOpts().ModulesCodegen ||2868(FD->hasAttr<DLLExportAttr>() &&2869Writer->Context->getLangOpts().BuildingPCHWithObjectFile)) {28702871// Under -fmodules-codegen, codegen is performed for all non-internal,2872// non-always_inline functions, unless they are available elsewhere.2873if (!FD->hasAttr<AlwaysInlineAttr>()) {2874if (!Linkage)2875Linkage = Writer->Context->GetGVALinkageForFunction(FD);2876ModulesCodegen =2877*Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally;2878}2879}2880}2881Record->push_back(ModulesCodegen);2882if (ModulesCodegen)2883Writer->AddDeclRef(FD, Writer->ModularCodegenDecls);2884if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {2885Record->push_back(CD->getNumCtorInitializers());2886if (CD->getNumCtorInitializers())2887AddCXXCtorInitializers(llvm::ArrayRef(CD->init_begin(), CD->init_end()));2888}2889AddStmt(FD->getBody());2890}289128922893