xref: /freebsd/contrib/llvm-project/clang/lib/Serialization/ASTWriterDecl.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 //  This file implements serialization for Declarations.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "ASTCommon.h"
14 #include "clang/AST/Attr.h"
15 #include "clang/AST/DeclCXX.h"
16 #include "clang/AST/DeclTemplate.h"
17 #include "clang/AST/DeclVisitor.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/OpenMPClause.h"
20 #include "clang/AST/PrettyDeclStackTrace.h"
21 #include "clang/Basic/SourceManager.h"
22 #include "clang/Serialization/ASTReader.h"
23 #include "clang/Serialization/ASTRecordWriter.h"
24 #include "llvm/Bitstream/BitstreamWriter.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include <optional>
27 using namespace clang;
28 using namespace serialization;
29 
30 //===----------------------------------------------------------------------===//
31 // Declaration serialization
32 //===----------------------------------------------------------------------===//
33 
34 namespace clang {
35   class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
36     ASTWriter &Writer;
37     ASTContext &Context;
38     ASTRecordWriter Record;
39 
40     serialization::DeclCode Code;
41     unsigned AbbrevToUse;
42 
43   public:
44     ASTDeclWriter(ASTWriter &Writer, ASTContext &Context,
45                   ASTWriter::RecordDataImpl &Record)
46         : Writer(Writer), Context(Context), Record(Writer, Record),
47           Code((serialization::DeclCode)0), AbbrevToUse(0) {}
48 
49     uint64_t Emit(Decl *D) {
50       if (!Code)
51         llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
52             D->getDeclKindName() + "'");
53       return Record.Emit(Code, AbbrevToUse);
54     }
55 
56     void Visit(Decl *D);
57 
58     void VisitDecl(Decl *D);
59     void VisitPragmaCommentDecl(PragmaCommentDecl *D);
60     void VisitPragmaDetectMismatchDecl(PragmaDetectMismatchDecl *D);
61     void VisitTranslationUnitDecl(TranslationUnitDecl *D);
62     void VisitNamedDecl(NamedDecl *D);
63     void VisitLabelDecl(LabelDecl *LD);
64     void VisitNamespaceDecl(NamespaceDecl *D);
65     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
66     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
67     void VisitTypeDecl(TypeDecl *D);
68     void VisitTypedefNameDecl(TypedefNameDecl *D);
69     void VisitTypedefDecl(TypedefDecl *D);
70     void VisitTypeAliasDecl(TypeAliasDecl *D);
71     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
72     void VisitUnresolvedUsingIfExistsDecl(UnresolvedUsingIfExistsDecl *D);
73     void VisitTagDecl(TagDecl *D);
74     void VisitEnumDecl(EnumDecl *D);
75     void VisitRecordDecl(RecordDecl *D);
76     void VisitCXXRecordDecl(CXXRecordDecl *D);
77     void VisitClassTemplateSpecializationDecl(
78                                             ClassTemplateSpecializationDecl *D);
79     void VisitClassTemplatePartialSpecializationDecl(
80                                      ClassTemplatePartialSpecializationDecl *D);
81     void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
82     void VisitVarTemplatePartialSpecializationDecl(
83         VarTemplatePartialSpecializationDecl *D);
84     void VisitClassScopeFunctionSpecializationDecl(
85                                        ClassScopeFunctionSpecializationDecl *D);
86     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
87     void VisitValueDecl(ValueDecl *D);
88     void VisitEnumConstantDecl(EnumConstantDecl *D);
89     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
90     void VisitDeclaratorDecl(DeclaratorDecl *D);
91     void VisitFunctionDecl(FunctionDecl *D);
92     void VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D);
93     void VisitCXXMethodDecl(CXXMethodDecl *D);
94     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
95     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
96     void VisitCXXConversionDecl(CXXConversionDecl *D);
97     void VisitFieldDecl(FieldDecl *D);
98     void VisitMSPropertyDecl(MSPropertyDecl *D);
99     void VisitMSGuidDecl(MSGuidDecl *D);
100     void VisitUnnamedGlobalConstantDecl(UnnamedGlobalConstantDecl *D);
101     void VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D);
102     void VisitIndirectFieldDecl(IndirectFieldDecl *D);
103     void VisitVarDecl(VarDecl *D);
104     void VisitImplicitParamDecl(ImplicitParamDecl *D);
105     void VisitParmVarDecl(ParmVarDecl *D);
106     void VisitDecompositionDecl(DecompositionDecl *D);
107     void VisitBindingDecl(BindingDecl *D);
108     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
109     void VisitTemplateDecl(TemplateDecl *D);
110     void VisitConceptDecl(ConceptDecl *D);
111     void VisitImplicitConceptSpecializationDecl(
112         ImplicitConceptSpecializationDecl *D);
113     void VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D);
114     void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
115     void VisitClassTemplateDecl(ClassTemplateDecl *D);
116     void VisitVarTemplateDecl(VarTemplateDecl *D);
117     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
118     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
119     void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
120     void VisitUsingDecl(UsingDecl *D);
121     void VisitUsingEnumDecl(UsingEnumDecl *D);
122     void VisitUsingPackDecl(UsingPackDecl *D);
123     void VisitUsingShadowDecl(UsingShadowDecl *D);
124     void VisitConstructorUsingShadowDecl(ConstructorUsingShadowDecl *D);
125     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
126     void VisitExportDecl(ExportDecl *D);
127     void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
128     void VisitTopLevelStmtDecl(TopLevelStmtDecl *D);
129     void VisitImportDecl(ImportDecl *D);
130     void VisitAccessSpecDecl(AccessSpecDecl *D);
131     void VisitFriendDecl(FriendDecl *D);
132     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
133     void VisitStaticAssertDecl(StaticAssertDecl *D);
134     void VisitBlockDecl(BlockDecl *D);
135     void VisitCapturedDecl(CapturedDecl *D);
136     void VisitEmptyDecl(EmptyDecl *D);
137     void VisitLifetimeExtendedTemporaryDecl(LifetimeExtendedTemporaryDecl *D);
138     void VisitDeclContext(DeclContext *DC);
139     template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
140     void VisitHLSLBufferDecl(HLSLBufferDecl *D);
141 
142     // FIXME: Put in the same order is DeclNodes.td?
143     void VisitObjCMethodDecl(ObjCMethodDecl *D);
144     void VisitObjCTypeParamDecl(ObjCTypeParamDecl *D);
145     void VisitObjCContainerDecl(ObjCContainerDecl *D);
146     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
147     void VisitObjCIvarDecl(ObjCIvarDecl *D);
148     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
149     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
150     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
151     void VisitObjCImplDecl(ObjCImplDecl *D);
152     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
153     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
154     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
155     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
156     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
157     void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
158     void VisitOMPAllocateDecl(OMPAllocateDecl *D);
159     void VisitOMPRequiresDecl(OMPRequiresDecl *D);
160     void VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D);
161     void VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D);
162     void VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D);
163 
164     /// Add an Objective-C type parameter list to the given record.
165     void AddObjCTypeParamList(ObjCTypeParamList *typeParams) {
166       // Empty type parameter list.
167       if (!typeParams) {
168         Record.push_back(0);
169         return;
170       }
171 
172       Record.push_back(typeParams->size());
173       for (auto *typeParam : *typeParams) {
174         Record.AddDeclRef(typeParam);
175       }
176       Record.AddSourceLocation(typeParams->getLAngleLoc());
177       Record.AddSourceLocation(typeParams->getRAngleLoc());
178     }
179 
180     /// Add to the record the first declaration from each module file that
181     /// provides a declaration of D. The intent is to provide a sufficient
182     /// set such that reloading this set will load all current redeclarations.
183     void AddFirstDeclFromEachModule(const Decl *D, bool IncludeLocal) {
184       llvm::MapVector<ModuleFile*, const Decl*> Firsts;
185       // FIXME: We can skip entries that we know are implied by others.
186       for (const Decl *R = D->getMostRecentDecl(); R; R = R->getPreviousDecl()) {
187         if (R->isFromASTFile())
188           Firsts[Writer.Chain->getOwningModuleFile(R)] = R;
189         else if (IncludeLocal)
190           Firsts[nullptr] = R;
191       }
192       for (const auto &F : Firsts)
193         Record.AddDeclRef(F.second);
194     }
195 
196     /// Get the specialization decl from an entry in the specialization list.
197     template <typename EntryType>
198     typename RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::DeclType *
199     getSpecializationDecl(EntryType &T) {
200       return RedeclarableTemplateDecl::SpecEntryTraits<EntryType>::getDecl(&T);
201     }
202 
203     /// Get the list of partial specializations from a template's common ptr.
204     template<typename T>
205     decltype(T::PartialSpecializations) &getPartialSpecializations(T *Common) {
206       return Common->PartialSpecializations;
207     }
208     ArrayRef<Decl> getPartialSpecializations(FunctionTemplateDecl::Common *) {
209       return std::nullopt;
210     }
211 
212     template<typename DeclTy>
213     void AddTemplateSpecializations(DeclTy *D) {
214       auto *Common = D->getCommonPtr();
215 
216       // If we have any lazy specializations, and the external AST source is
217       // our chained AST reader, we can just write out the DeclIDs. Otherwise,
218       // we need to resolve them to actual declarations.
219       if (Writer.Chain != Writer.Context->getExternalSource() &&
220           Common->LazySpecializations) {
221         D->LoadLazySpecializations();
222         assert(!Common->LazySpecializations);
223       }
224 
225       ArrayRef<DeclID> LazySpecializations;
226       if (auto *LS = Common->LazySpecializations)
227         LazySpecializations = llvm::ArrayRef(LS + 1, LS[0]);
228 
229       // Add a slot to the record for the number of specializations.
230       unsigned I = Record.size();
231       Record.push_back(0);
232 
233       // AddFirstDeclFromEachModule might trigger deserialization, invalidating
234       // *Specializations iterators.
235       llvm::SmallVector<const Decl*, 16> Specs;
236       for (auto &Entry : Common->Specializations)
237         Specs.push_back(getSpecializationDecl(Entry));
238       for (auto &Entry : getPartialSpecializations(Common))
239         Specs.push_back(getSpecializationDecl(Entry));
240 
241       for (auto *D : Specs) {
242         assert(D->isCanonicalDecl() && "non-canonical decl in set");
243         AddFirstDeclFromEachModule(D, /*IncludeLocal*/true);
244       }
245       Record.append(LazySpecializations.begin(), LazySpecializations.end());
246 
247       // Update the size entry we added earlier.
248       Record[I] = Record.size() - I - 1;
249     }
250 
251     /// Ensure that this template specialization is associated with the specified
252     /// template on reload.
253     void RegisterTemplateSpecialization(const Decl *Template,
254                                         const Decl *Specialization) {
255       Template = Template->getCanonicalDecl();
256 
257       // If the canonical template is local, we'll write out this specialization
258       // when we emit it.
259       // FIXME: We can do the same thing if there is any local declaration of
260       // the template, to avoid emitting an update record.
261       if (!Template->isFromASTFile())
262         return;
263 
264       // We only need to associate the first local declaration of the
265       // specialization. The other declarations will get pulled in by it.
266       if (Writer.getFirstLocalDecl(Specialization) != Specialization)
267         return;
268 
269       Writer.DeclUpdates[Template].push_back(ASTWriter::DeclUpdate(
270           UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION, Specialization));
271     }
272   };
273 }
274 
275 void ASTDeclWriter::Visit(Decl *D) {
276   DeclVisitor<ASTDeclWriter>::Visit(D);
277 
278   // Source locations require array (variable-length) abbreviations.  The
279   // abbreviation infrastructure requires that arrays are encoded last, so
280   // we handle it here in the case of those classes derived from DeclaratorDecl
281   if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
282     if (auto *TInfo = DD->getTypeSourceInfo())
283       Record.AddTypeLoc(TInfo->getTypeLoc());
284   }
285 
286   // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
287   // have been written. We want it last because we will not read it back when
288   // retrieving it from the AST, we'll just lazily set the offset.
289   if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
290     Record.push_back(FD->doesThisDeclarationHaveABody());
291     if (FD->doesThisDeclarationHaveABody())
292       Record.AddFunctionDefinition(FD);
293   }
294 
295   // If this declaration is also a DeclContext, write blocks for the
296   // declarations that lexically stored inside its context and those
297   // declarations that are visible from its context.
298   if (DeclContext *DC = dyn_cast<DeclContext>(D))
299     VisitDeclContext(DC);
300 }
301 
302 void ASTDeclWriter::VisitDecl(Decl *D) {
303   Record.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()));
304   if (D->getDeclContext() != D->getLexicalDeclContext())
305     Record.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()));
306   else
307     Record.push_back(0);
308   Record.push_back(D->isInvalidDecl());
309   Record.push_back(D->hasAttrs());
310   if (D->hasAttrs())
311     Record.AddAttributes(D->getAttrs());
312   Record.push_back(D->isImplicit());
313   Record.push_back(D->isUsed(false));
314   Record.push_back(D->isReferenced());
315   Record.push_back(D->isTopLevelDeclInObjCContainer());
316   Record.push_back(D->getAccess());
317   Record.push_back((uint64_t)D->getModuleOwnershipKind());
318   Record.push_back(Writer.getSubmoduleID(D->getOwningModule()));
319 
320   // If this declaration injected a name into a context different from its
321   // lexical context, and that context is an imported namespace, we need to
322   // update its visible declarations to include this name.
323   //
324   // This happens when we instantiate a class with a friend declaration or a
325   // function with a local extern declaration, for instance.
326   //
327   // FIXME: Can we handle this in AddedVisibleDecl instead?
328   if (D->isOutOfLine()) {
329     auto *DC = D->getDeclContext();
330     while (auto *NS = dyn_cast<NamespaceDecl>(DC->getRedeclContext())) {
331       if (!NS->isFromASTFile())
332         break;
333       Writer.UpdatedDeclContexts.insert(NS->getPrimaryContext());
334       if (!NS->isInlineNamespace())
335         break;
336       DC = NS->getParent();
337     }
338   }
339 }
340 
341 void ASTDeclWriter::VisitPragmaCommentDecl(PragmaCommentDecl *D) {
342   StringRef Arg = D->getArg();
343   Record.push_back(Arg.size());
344   VisitDecl(D);
345   Record.AddSourceLocation(D->getBeginLoc());
346   Record.push_back(D->getCommentKind());
347   Record.AddString(Arg);
348   Code = serialization::DECL_PRAGMA_COMMENT;
349 }
350 
351 void ASTDeclWriter::VisitPragmaDetectMismatchDecl(
352     PragmaDetectMismatchDecl *D) {
353   StringRef Name = D->getName();
354   StringRef Value = D->getValue();
355   Record.push_back(Name.size() + 1 + Value.size());
356   VisitDecl(D);
357   Record.AddSourceLocation(D->getBeginLoc());
358   Record.AddString(Name);
359   Record.AddString(Value);
360   Code = serialization::DECL_PRAGMA_DETECT_MISMATCH;
361 }
362 
363 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
364   llvm_unreachable("Translation units aren't directly serialized");
365 }
366 
367 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
368   VisitDecl(D);
369   Record.AddDeclarationName(D->getDeclName());
370   Record.push_back(needsAnonymousDeclarationNumber(D)
371                        ? Writer.getAnonymousDeclarationNumber(D)
372                        : 0);
373 }
374 
375 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
376   VisitNamedDecl(D);
377   Record.AddSourceLocation(D->getBeginLoc());
378   Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
379 }
380 
381 void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
382   VisitRedeclarable(D);
383   VisitTypeDecl(D);
384   Record.AddTypeSourceInfo(D->getTypeSourceInfo());
385   Record.push_back(D->isModed());
386   if (D->isModed())
387     Record.AddTypeRef(D->getUnderlyingType());
388   Record.AddDeclRef(D->getAnonDeclWithTypedefName(false));
389 }
390 
391 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
392   VisitTypedefNameDecl(D);
393   if (D->getDeclContext() == D->getLexicalDeclContext() &&
394       !D->hasAttrs() &&
395       !D->isImplicit() &&
396       D->getFirstDecl() == D->getMostRecentDecl() &&
397       !D->isInvalidDecl() &&
398       !D->isTopLevelDeclInObjCContainer() &&
399       !D->isModulePrivate() &&
400       !needsAnonymousDeclarationNumber(D) &&
401       D->getDeclName().getNameKind() == DeclarationName::Identifier)
402     AbbrevToUse = Writer.getDeclTypedefAbbrev();
403 
404   Code = serialization::DECL_TYPEDEF;
405 }
406 
407 void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
408   VisitTypedefNameDecl(D);
409   Record.AddDeclRef(D->getDescribedAliasTemplate());
410   Code = serialization::DECL_TYPEALIAS;
411 }
412 
413 void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
414   VisitRedeclarable(D);
415   VisitTypeDecl(D);
416   Record.push_back(D->getIdentifierNamespace());
417   Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
418   if (!isa<CXXRecordDecl>(D))
419     Record.push_back(D->isCompleteDefinition());
420   Record.push_back(D->isEmbeddedInDeclarator());
421   Record.push_back(D->isFreeStanding());
422   Record.push_back(D->isCompleteDefinitionRequired());
423   Record.AddSourceRange(D->getBraceRange());
424 
425   if (D->hasExtInfo()) {
426     Record.push_back(1);
427     Record.AddQualifierInfo(*D->getExtInfo());
428   } else if (auto *TD = D->getTypedefNameForAnonDecl()) {
429     Record.push_back(2);
430     Record.AddDeclRef(TD);
431     Record.AddIdentifierRef(TD->getDeclName().getAsIdentifierInfo());
432   } else {
433     Record.push_back(0);
434   }
435 }
436 
437 void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
438   VisitTagDecl(D);
439   Record.AddTypeSourceInfo(D->getIntegerTypeSourceInfo());
440   if (!D->getIntegerTypeSourceInfo())
441     Record.AddTypeRef(D->getIntegerType());
442   Record.AddTypeRef(D->getPromotionType());
443   Record.push_back(D->getNumPositiveBits());
444   Record.push_back(D->getNumNegativeBits());
445   Record.push_back(D->isScoped());
446   Record.push_back(D->isScopedUsingClassTag());
447   Record.push_back(D->isFixed());
448   Record.push_back(D->getODRHash());
449 
450   if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
451     Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
452     Record.push_back(MemberInfo->getTemplateSpecializationKind());
453     Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
454   } else {
455     Record.AddDeclRef(nullptr);
456   }
457 
458   if (D->getDeclContext() == D->getLexicalDeclContext() &&
459       !D->hasAttrs() &&
460       !D->isImplicit() &&
461       !D->isUsed(false) &&
462       !D->hasExtInfo() &&
463       !D->getTypedefNameForAnonDecl() &&
464       D->getFirstDecl() == D->getMostRecentDecl() &&
465       !D->isInvalidDecl() &&
466       !D->isReferenced() &&
467       !D->isTopLevelDeclInObjCContainer() &&
468       D->getAccess() == AS_none &&
469       !D->isModulePrivate() &&
470       !CXXRecordDecl::classofKind(D->getKind()) &&
471       !D->getIntegerTypeSourceInfo() &&
472       !D->getMemberSpecializationInfo() &&
473       !needsAnonymousDeclarationNumber(D) &&
474       D->getDeclName().getNameKind() == DeclarationName::Identifier)
475     AbbrevToUse = Writer.getDeclEnumAbbrev();
476 
477   Code = serialization::DECL_ENUM;
478 }
479 
480 void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
481   VisitTagDecl(D);
482   Record.push_back(D->hasFlexibleArrayMember());
483   Record.push_back(D->isAnonymousStructOrUnion());
484   Record.push_back(D->hasObjectMember());
485   Record.push_back(D->hasVolatileMember());
486   Record.push_back(D->isNonTrivialToPrimitiveDefaultInitialize());
487   Record.push_back(D->isNonTrivialToPrimitiveCopy());
488   Record.push_back(D->isNonTrivialToPrimitiveDestroy());
489   Record.push_back(D->hasNonTrivialToPrimitiveDefaultInitializeCUnion());
490   Record.push_back(D->hasNonTrivialToPrimitiveDestructCUnion());
491   Record.push_back(D->hasNonTrivialToPrimitiveCopyCUnion());
492   Record.push_back(D->isParamDestroyedInCallee());
493   Record.push_back(D->getArgPassingRestrictions());
494   // Only compute this for C/Objective-C, in C++ this is computed as part
495   // of CXXRecordDecl.
496   if (!isa<CXXRecordDecl>(D))
497     Record.push_back(D->getODRHash());
498 
499   if (D->getDeclContext() == D->getLexicalDeclContext() &&
500       !D->hasAttrs() &&
501       !D->isImplicit() &&
502       !D->isUsed(false) &&
503       !D->hasExtInfo() &&
504       !D->getTypedefNameForAnonDecl() &&
505       D->getFirstDecl() == D->getMostRecentDecl() &&
506       !D->isInvalidDecl() &&
507       !D->isReferenced() &&
508       !D->isTopLevelDeclInObjCContainer() &&
509       D->getAccess() == AS_none &&
510       !D->isModulePrivate() &&
511       !CXXRecordDecl::classofKind(D->getKind()) &&
512       !needsAnonymousDeclarationNumber(D) &&
513       D->getDeclName().getNameKind() == DeclarationName::Identifier)
514     AbbrevToUse = Writer.getDeclRecordAbbrev();
515 
516   Code = serialization::DECL_RECORD;
517 }
518 
519 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
520   VisitNamedDecl(D);
521   Record.AddTypeRef(D->getType());
522 }
523 
524 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
525   VisitValueDecl(D);
526   Record.push_back(D->getInitExpr()? 1 : 0);
527   if (D->getInitExpr())
528     Record.AddStmt(D->getInitExpr());
529   Record.AddAPSInt(D->getInitVal());
530 
531   Code = serialization::DECL_ENUM_CONSTANT;
532 }
533 
534 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
535   VisitValueDecl(D);
536   Record.AddSourceLocation(D->getInnerLocStart());
537   Record.push_back(D->hasExtInfo());
538   if (D->hasExtInfo()) {
539     DeclaratorDecl::ExtInfo *Info = D->getExtInfo();
540     Record.AddQualifierInfo(*Info);
541     Record.AddStmt(Info->TrailingRequiresClause);
542   }
543   // The location information is deferred until the end of the record.
544   Record.AddTypeRef(D->getTypeSourceInfo() ? D->getTypeSourceInfo()->getType()
545                                            : QualType());
546 }
547 
548 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
549   VisitRedeclarable(D);
550 
551   Record.push_back(D->getTemplatedKind());
552   switch (D->getTemplatedKind()) {
553   case FunctionDecl::TK_NonTemplate:
554     break;
555   case FunctionDecl::TK_DependentNonTemplate:
556     Record.AddDeclRef(D->getInstantiatedFromDecl());
557     break;
558   case FunctionDecl::TK_FunctionTemplate:
559     Record.AddDeclRef(D->getDescribedFunctionTemplate());
560     break;
561   case FunctionDecl::TK_MemberSpecialization: {
562     MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
563     Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
564     Record.push_back(MemberInfo->getTemplateSpecializationKind());
565     Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
566     break;
567   }
568   case FunctionDecl::TK_FunctionTemplateSpecialization: {
569     FunctionTemplateSpecializationInfo *
570       FTSInfo = D->getTemplateSpecializationInfo();
571 
572     RegisterTemplateSpecialization(FTSInfo->getTemplate(), D);
573 
574     Record.AddDeclRef(FTSInfo->getTemplate());
575     Record.push_back(FTSInfo->getTemplateSpecializationKind());
576 
577     // Template arguments.
578     Record.AddTemplateArgumentList(FTSInfo->TemplateArguments);
579 
580     // Template args as written.
581     Record.push_back(FTSInfo->TemplateArgumentsAsWritten != nullptr);
582     if (FTSInfo->TemplateArgumentsAsWritten) {
583       Record.push_back(FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs);
584       for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs;
585              i!=e; ++i)
586         Record.AddTemplateArgumentLoc(
587             (*FTSInfo->TemplateArgumentsAsWritten)[i]);
588       Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc);
589       Record.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc);
590     }
591 
592     Record.AddSourceLocation(FTSInfo->getPointOfInstantiation());
593 
594     if (MemberSpecializationInfo *MemberInfo =
595         FTSInfo->getMemberSpecializationInfo()) {
596       Record.push_back(1);
597       Record.AddDeclRef(MemberInfo->getInstantiatedFrom());
598       Record.push_back(MemberInfo->getTemplateSpecializationKind());
599       Record.AddSourceLocation(MemberInfo->getPointOfInstantiation());
600     } else {
601       Record.push_back(0);
602     }
603 
604     if (D->isCanonicalDecl()) {
605       // Write the template that contains the specializations set. We will
606       // add a FunctionTemplateSpecializationInfo to it when reading.
607       Record.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl());
608     }
609     break;
610   }
611   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
612     DependentFunctionTemplateSpecializationInfo *
613       DFTSInfo = D->getDependentSpecializationInfo();
614 
615     // Templates.
616     Record.push_back(DFTSInfo->getNumTemplates());
617     for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
618       Record.AddDeclRef(DFTSInfo->getTemplate(i));
619 
620     // Templates args.
621     Record.push_back(DFTSInfo->getNumTemplateArgs());
622     for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
623       Record.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i));
624     Record.AddSourceLocation(DFTSInfo->getLAngleLoc());
625     Record.AddSourceLocation(DFTSInfo->getRAngleLoc());
626     break;
627   }
628   }
629 
630   VisitDeclaratorDecl(D);
631   Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
632   Record.push_back(D->getIdentifierNamespace());
633 
634   // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
635   // after everything else is written.
636   Record.push_back(
637       static_cast<int>(D->getStorageClass())); // FIXME: stable encoding
638   Record.push_back(D->isInlineSpecified());
639   Record.push_back(D->isInlined());
640   Record.push_back(D->isVirtualAsWritten());
641   Record.push_back(D->isPure());
642   Record.push_back(D->hasInheritedPrototype());
643   Record.push_back(D->hasWrittenPrototype());
644   Record.push_back(D->isDeletedBit());
645   Record.push_back(D->isTrivial());
646   Record.push_back(D->isTrivialForCall());
647   Record.push_back(D->isDefaulted());
648   Record.push_back(D->isExplicitlyDefaulted());
649   Record.push_back(D->isIneligibleOrNotSelected());
650   Record.push_back(D->hasImplicitReturnZero());
651   Record.push_back(static_cast<uint64_t>(D->getConstexprKind()));
652   Record.push_back(D->usesSEHTry());
653   Record.push_back(D->hasSkippedBody());
654   Record.push_back(D->isMultiVersion());
655   Record.push_back(D->isLateTemplateParsed());
656   Record.push_back(D->FriendConstraintRefersToEnclosingTemplate());
657   Record.push_back(D->getLinkageInternal());
658   Record.AddSourceLocation(D->getEndLoc());
659   Record.AddSourceLocation(D->getDefaultLoc());
660 
661   Record.push_back(D->getODRHash());
662 
663   if (D->isDefaulted()) {
664     if (auto *FDI = D->getDefaultedFunctionInfo()) {
665       Record.push_back(FDI->getUnqualifiedLookups().size());
666       for (DeclAccessPair P : FDI->getUnqualifiedLookups()) {
667         Record.AddDeclRef(P.getDecl());
668         Record.push_back(P.getAccess());
669       }
670     } else {
671       Record.push_back(0);
672     }
673   }
674 
675   Record.push_back(D->param_size());
676   for (auto *P : D->parameters())
677     Record.AddDeclRef(P);
678   Code = serialization::DECL_FUNCTION;
679 }
680 
681 static void addExplicitSpecifier(ExplicitSpecifier ES,
682                                  ASTRecordWriter &Record) {
683   uint64_t Kind = static_cast<uint64_t>(ES.getKind());
684   Kind = Kind << 1 | static_cast<bool>(ES.getExpr());
685   Record.push_back(Kind);
686   if (ES.getExpr()) {
687     Record.AddStmt(ES.getExpr());
688   }
689 }
690 
691 void ASTDeclWriter::VisitCXXDeductionGuideDecl(CXXDeductionGuideDecl *D) {
692   addExplicitSpecifier(D->getExplicitSpecifier(), Record);
693   Record.AddDeclRef(D->Ctor);
694   VisitFunctionDecl(D);
695   Record.push_back(D->isCopyDeductionCandidate());
696   Code = serialization::DECL_CXX_DEDUCTION_GUIDE;
697 }
698 
699 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
700   VisitNamedDecl(D);
701   // FIXME: convert to LazyStmtPtr?
702   // Unlike C/C++, method bodies will never be in header files.
703   bool HasBodyStuff = D->getBody() != nullptr;
704   Record.push_back(HasBodyStuff);
705   if (HasBodyStuff) {
706     Record.AddStmt(D->getBody());
707   }
708   Record.AddDeclRef(D->getSelfDecl());
709   Record.AddDeclRef(D->getCmdDecl());
710   Record.push_back(D->isInstanceMethod());
711   Record.push_back(D->isVariadic());
712   Record.push_back(D->isPropertyAccessor());
713   Record.push_back(D->isSynthesizedAccessorStub());
714   Record.push_back(D->isDefined());
715   Record.push_back(D->isOverriding());
716   Record.push_back(D->hasSkippedBody());
717 
718   Record.push_back(D->isRedeclaration());
719   Record.push_back(D->hasRedeclaration());
720   if (D->hasRedeclaration()) {
721     assert(Context.getObjCMethodRedeclaration(D));
722     Record.AddDeclRef(Context.getObjCMethodRedeclaration(D));
723   }
724 
725   // FIXME: stable encoding for @required/@optional
726   Record.push_back(D->getImplementationControl());
727   // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway/nullability
728   Record.push_back(D->getObjCDeclQualifier());
729   Record.push_back(D->hasRelatedResultType());
730   Record.AddTypeRef(D->getReturnType());
731   Record.AddTypeSourceInfo(D->getReturnTypeSourceInfo());
732   Record.AddSourceLocation(D->getEndLoc());
733   Record.push_back(D->param_size());
734   for (const auto *P : D->parameters())
735     Record.AddDeclRef(P);
736 
737   Record.push_back(D->getSelLocsKind());
738   unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
739   SourceLocation *SelLocs = D->getStoredSelLocs();
740   Record.push_back(NumStoredSelLocs);
741   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
742     Record.AddSourceLocation(SelLocs[i]);
743 
744   Code = serialization::DECL_OBJC_METHOD;
745 }
746 
747 void ASTDeclWriter::VisitObjCTypeParamDecl(ObjCTypeParamDecl *D) {
748   VisitTypedefNameDecl(D);
749   Record.push_back(D->Variance);
750   Record.push_back(D->Index);
751   Record.AddSourceLocation(D->VarianceLoc);
752   Record.AddSourceLocation(D->ColonLoc);
753 
754   Code = serialization::DECL_OBJC_TYPE_PARAM;
755 }
756 
757 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
758   VisitNamedDecl(D);
759   Record.AddSourceLocation(D->getAtStartLoc());
760   Record.AddSourceRange(D->getAtEndRange());
761   // Abstract class (no need to define a stable serialization::DECL code).
762 }
763 
764 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
765   VisitRedeclarable(D);
766   VisitObjCContainerDecl(D);
767   Record.AddTypeRef(QualType(D->getTypeForDecl(), 0));
768   AddObjCTypeParamList(D->TypeParamList);
769 
770   Record.push_back(D->isThisDeclarationADefinition());
771   if (D->isThisDeclarationADefinition()) {
772     // Write the DefinitionData
773     ObjCInterfaceDecl::DefinitionData &Data = D->data();
774 
775     Record.AddTypeSourceInfo(D->getSuperClassTInfo());
776     Record.AddSourceLocation(D->getEndOfDefinitionLoc());
777     Record.push_back(Data.HasDesignatedInitializers);
778     Record.push_back(D->getODRHash());
779 
780     // Write out the protocols that are directly referenced by the @interface.
781     Record.push_back(Data.ReferencedProtocols.size());
782     for (const auto *P : D->protocols())
783       Record.AddDeclRef(P);
784     for (const auto &PL : D->protocol_locs())
785       Record.AddSourceLocation(PL);
786 
787     // Write out the protocols that are transitively referenced.
788     Record.push_back(Data.AllReferencedProtocols.size());
789     for (ObjCList<ObjCProtocolDecl>::iterator
790               P = Data.AllReferencedProtocols.begin(),
791            PEnd = Data.AllReferencedProtocols.end();
792          P != PEnd; ++P)
793       Record.AddDeclRef(*P);
794 
795 
796     if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
797       // Ensure that we write out the set of categories for this class.
798       Writer.ObjCClassesWithCategories.insert(D);
799 
800       // Make sure that the categories get serialized.
801       for (; Cat; Cat = Cat->getNextClassCategoryRaw())
802         (void)Writer.GetDeclRef(Cat);
803     }
804   }
805 
806   Code = serialization::DECL_OBJC_INTERFACE;
807 }
808 
809 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
810   VisitFieldDecl(D);
811   // FIXME: stable encoding for @public/@private/@protected/@package
812   Record.push_back(D->getAccessControl());
813   Record.push_back(D->getSynthesize());
814 
815   if (D->getDeclContext() == D->getLexicalDeclContext() &&
816       !D->hasAttrs() &&
817       !D->isImplicit() &&
818       !D->isUsed(false) &&
819       !D->isInvalidDecl() &&
820       !D->isReferenced() &&
821       !D->isModulePrivate() &&
822       !D->getBitWidth() &&
823       !D->hasExtInfo() &&
824       D->getDeclName())
825     AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
826 
827   Code = serialization::DECL_OBJC_IVAR;
828 }
829 
830 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
831   VisitRedeclarable(D);
832   VisitObjCContainerDecl(D);
833 
834   Record.push_back(D->isThisDeclarationADefinition());
835   if (D->isThisDeclarationADefinition()) {
836     Record.push_back(D->protocol_size());
837     for (const auto *I : D->protocols())
838       Record.AddDeclRef(I);
839     for (const auto &PL : D->protocol_locs())
840       Record.AddSourceLocation(PL);
841     Record.push_back(D->getODRHash());
842   }
843 
844   Code = serialization::DECL_OBJC_PROTOCOL;
845 }
846 
847 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
848   VisitFieldDecl(D);
849   Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
850 }
851 
852 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
853   VisitObjCContainerDecl(D);
854   Record.AddSourceLocation(D->getCategoryNameLoc());
855   Record.AddSourceLocation(D->getIvarLBraceLoc());
856   Record.AddSourceLocation(D->getIvarRBraceLoc());
857   Record.AddDeclRef(D->getClassInterface());
858   AddObjCTypeParamList(D->TypeParamList);
859   Record.push_back(D->protocol_size());
860   for (const auto *I : D->protocols())
861     Record.AddDeclRef(I);
862   for (const auto &PL : D->protocol_locs())
863     Record.AddSourceLocation(PL);
864   Code = serialization::DECL_OBJC_CATEGORY;
865 }
866 
867 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
868   VisitNamedDecl(D);
869   Record.AddDeclRef(D->getClassInterface());
870   Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
871 }
872 
873 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
874   VisitNamedDecl(D);
875   Record.AddSourceLocation(D->getAtLoc());
876   Record.AddSourceLocation(D->getLParenLoc());
877   Record.AddTypeRef(D->getType());
878   Record.AddTypeSourceInfo(D->getTypeSourceInfo());
879   // FIXME: stable encoding
880   Record.push_back((unsigned)D->getPropertyAttributes());
881   Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
882   // FIXME: stable encoding
883   Record.push_back((unsigned)D->getPropertyImplementation());
884   Record.AddDeclarationName(D->getGetterName());
885   Record.AddSourceLocation(D->getGetterNameLoc());
886   Record.AddDeclarationName(D->getSetterName());
887   Record.AddSourceLocation(D->getSetterNameLoc());
888   Record.AddDeclRef(D->getGetterMethodDecl());
889   Record.AddDeclRef(D->getSetterMethodDecl());
890   Record.AddDeclRef(D->getPropertyIvarDecl());
891   Code = serialization::DECL_OBJC_PROPERTY;
892 }
893 
894 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
895   VisitObjCContainerDecl(D);
896   Record.AddDeclRef(D->getClassInterface());
897   // Abstract class (no need to define a stable serialization::DECL code).
898 }
899 
900 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
901   VisitObjCImplDecl(D);
902   Record.AddSourceLocation(D->getCategoryNameLoc());
903   Code = serialization::DECL_OBJC_CATEGORY_IMPL;
904 }
905 
906 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
907   VisitObjCImplDecl(D);
908   Record.AddDeclRef(D->getSuperClass());
909   Record.AddSourceLocation(D->getSuperClassLoc());
910   Record.AddSourceLocation(D->getIvarLBraceLoc());
911   Record.AddSourceLocation(D->getIvarRBraceLoc());
912   Record.push_back(D->hasNonZeroConstructors());
913   Record.push_back(D->hasDestructors());
914   Record.push_back(D->NumIvarInitializers);
915   if (D->NumIvarInitializers)
916     Record.AddCXXCtorInitializers(
917         llvm::ArrayRef(D->init_begin(), D->init_end()));
918   Code = serialization::DECL_OBJC_IMPLEMENTATION;
919 }
920 
921 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
922   VisitDecl(D);
923   Record.AddSourceLocation(D->getBeginLoc());
924   Record.AddDeclRef(D->getPropertyDecl());
925   Record.AddDeclRef(D->getPropertyIvarDecl());
926   Record.AddSourceLocation(D->getPropertyIvarDeclLoc());
927   Record.AddDeclRef(D->getGetterMethodDecl());
928   Record.AddDeclRef(D->getSetterMethodDecl());
929   Record.AddStmt(D->getGetterCXXConstructor());
930   Record.AddStmt(D->getSetterCXXAssignment());
931   Code = serialization::DECL_OBJC_PROPERTY_IMPL;
932 }
933 
934 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
935   VisitDeclaratorDecl(D);
936   Record.push_back(D->isMutable());
937 
938   FieldDecl::InitStorageKind ISK = D->InitStorage.getInt();
939   Record.push_back(ISK);
940   if (ISK == FieldDecl::ISK_CapturedVLAType)
941     Record.AddTypeRef(QualType(D->getCapturedVLAType(), 0));
942   else if (ISK)
943     Record.AddStmt(D->getInClassInitializer());
944 
945   Record.AddStmt(D->getBitWidth());
946 
947   if (!D->getDeclName())
948     Record.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D));
949 
950   if (D->getDeclContext() == D->getLexicalDeclContext() &&
951       !D->hasAttrs() &&
952       !D->isImplicit() &&
953       !D->isUsed(false) &&
954       !D->isInvalidDecl() &&
955       !D->isReferenced() &&
956       !D->isTopLevelDeclInObjCContainer() &&
957       !D->isModulePrivate() &&
958       !D->getBitWidth() &&
959       !D->hasInClassInitializer() &&
960       !D->hasCapturedVLAType() &&
961       !D->hasExtInfo() &&
962       !ObjCIvarDecl::classofKind(D->getKind()) &&
963       !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
964       D->getDeclName())
965     AbbrevToUse = Writer.getDeclFieldAbbrev();
966 
967   Code = serialization::DECL_FIELD;
968 }
969 
970 void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
971   VisitDeclaratorDecl(D);
972   Record.AddIdentifierRef(D->getGetterId());
973   Record.AddIdentifierRef(D->getSetterId());
974   Code = serialization::DECL_MS_PROPERTY;
975 }
976 
977 void ASTDeclWriter::VisitMSGuidDecl(MSGuidDecl *D) {
978   VisitValueDecl(D);
979   MSGuidDecl::Parts Parts = D->getParts();
980   Record.push_back(Parts.Part1);
981   Record.push_back(Parts.Part2);
982   Record.push_back(Parts.Part3);
983   Record.append(std::begin(Parts.Part4And5), std::end(Parts.Part4And5));
984   Code = serialization::DECL_MS_GUID;
985 }
986 
987 void ASTDeclWriter::VisitUnnamedGlobalConstantDecl(
988     UnnamedGlobalConstantDecl *D) {
989   VisitValueDecl(D);
990   Record.AddAPValue(D->getValue());
991   Code = serialization::DECL_UNNAMED_GLOBAL_CONSTANT;
992 }
993 
994 void ASTDeclWriter::VisitTemplateParamObjectDecl(TemplateParamObjectDecl *D) {
995   VisitValueDecl(D);
996   Record.AddAPValue(D->getValue());
997   Code = serialization::DECL_TEMPLATE_PARAM_OBJECT;
998 }
999 
1000 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
1001   VisitValueDecl(D);
1002   Record.push_back(D->getChainingSize());
1003 
1004   for (const auto *P : D->chain())
1005     Record.AddDeclRef(P);
1006   Code = serialization::DECL_INDIRECTFIELD;
1007 }
1008 
1009 void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
1010   VisitRedeclarable(D);
1011   VisitDeclaratorDecl(D);
1012   Record.push_back(D->getStorageClass());
1013   Record.push_back(D->getTSCSpec());
1014   Record.push_back(D->getInitStyle());
1015   Record.push_back(D->isARCPseudoStrong());
1016   if (!isa<ParmVarDecl>(D)) {
1017     Record.push_back(D->isThisDeclarationADemotedDefinition());
1018     Record.push_back(D->isExceptionVariable());
1019     Record.push_back(D->isNRVOVariable());
1020     Record.push_back(D->isCXXForRangeDecl());
1021     Record.push_back(D->isObjCForDecl());
1022     Record.push_back(D->isInline());
1023     Record.push_back(D->isInlineSpecified());
1024     Record.push_back(D->isConstexpr());
1025     Record.push_back(D->isInitCapture());
1026     Record.push_back(D->isPreviousDeclInSameBlockScope());
1027     if (const auto *IPD = dyn_cast<ImplicitParamDecl>(D))
1028       Record.push_back(static_cast<unsigned>(IPD->getParameterKind()));
1029     else
1030       Record.push_back(0);
1031     Record.push_back(D->isEscapingByref());
1032   }
1033   Record.push_back(D->getLinkageInternal());
1034 
1035   Record.AddVarDeclInit(D);
1036 
1037   if (D->hasAttr<BlocksAttr>() && D->getType()->getAsCXXRecordDecl()) {
1038     BlockVarCopyInit Init = Writer.Context->getBlockVarCopyInit(D);
1039     Record.AddStmt(Init.getCopyExpr());
1040     if (Init.getCopyExpr())
1041       Record.push_back(Init.canThrow());
1042   }
1043 
1044   if (D->getStorageDuration() == SD_Static) {
1045     bool ModulesCodegen = false;
1046     if (Writer.WritingModule &&
1047         !D->getDescribedVarTemplate() && !D->getMemberSpecializationInfo()) {
1048       // When building a C++20 module interface unit or a partition unit, a
1049       // strong definition in the module interface is provided by the
1050       // compilation of that unit, not by its users. (Inline variables are still
1051       // emitted in module users.)
1052       ModulesCodegen =
1053           (Writer.WritingModule->isInterfaceOrPartition() ||
1054            (D->hasAttr<DLLExportAttr>() &&
1055             Writer.Context->getLangOpts().BuildingPCHWithObjectFile)) &&
1056           Writer.Context->GetGVALinkageForVariable(D) == GVA_StrongExternal;
1057     }
1058     Record.push_back(ModulesCodegen);
1059     if (ModulesCodegen)
1060       Writer.ModularCodegenDecls.push_back(Writer.GetDeclRef(D));
1061   }
1062 
1063   enum {
1064     VarNotTemplate = 0, VarTemplate, StaticDataMemberSpecialization
1065   };
1066   if (VarTemplateDecl *TemplD = D->getDescribedVarTemplate()) {
1067     Record.push_back(VarTemplate);
1068     Record.AddDeclRef(TemplD);
1069   } else if (MemberSpecializationInfo *SpecInfo
1070                = D->getMemberSpecializationInfo()) {
1071     Record.push_back(StaticDataMemberSpecialization);
1072     Record.AddDeclRef(SpecInfo->getInstantiatedFrom());
1073     Record.push_back(SpecInfo->getTemplateSpecializationKind());
1074     Record.AddSourceLocation(SpecInfo->getPointOfInstantiation());
1075   } else {
1076     Record.push_back(VarNotTemplate);
1077   }
1078 
1079   if (D->getDeclContext() == D->getLexicalDeclContext() &&
1080       !D->hasAttrs() &&
1081       !D->isImplicit() &&
1082       !D->isUsed(false) &&
1083       !D->isInvalidDecl() &&
1084       !D->isReferenced() &&
1085       !D->isTopLevelDeclInObjCContainer() &&
1086       D->getAccess() == AS_none &&
1087       !D->isModulePrivate() &&
1088       !needsAnonymousDeclarationNumber(D) &&
1089       D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1090       !D->hasExtInfo() &&
1091       D->getFirstDecl() == D->getMostRecentDecl() &&
1092       D->getKind() == Decl::Var &&
1093       !D->isInline() &&
1094       !D->isConstexpr() &&
1095       !D->isInitCapture() &&
1096       !D->isPreviousDeclInSameBlockScope() &&
1097       !(D->hasAttr<BlocksAttr>() && D->getType()->getAsCXXRecordDecl()) &&
1098       !D->isEscapingByref() &&
1099       D->getStorageDuration() != SD_Static &&
1100       !D->getMemberSpecializationInfo())
1101     AbbrevToUse = Writer.getDeclVarAbbrev();
1102 
1103   Code = serialization::DECL_VAR;
1104 }
1105 
1106 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
1107   VisitVarDecl(D);
1108   Code = serialization::DECL_IMPLICIT_PARAM;
1109 }
1110 
1111 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
1112   VisitVarDecl(D);
1113   Record.push_back(D->isObjCMethodParameter());
1114   Record.push_back(D->getFunctionScopeDepth());
1115   Record.push_back(D->getFunctionScopeIndex());
1116   Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
1117   Record.push_back(D->isKNRPromoted());
1118   Record.push_back(D->hasInheritedDefaultArg());
1119   Record.push_back(D->hasUninstantiatedDefaultArg());
1120   if (D->hasUninstantiatedDefaultArg())
1121     Record.AddStmt(D->getUninstantiatedDefaultArg());
1122   Code = serialization::DECL_PARM_VAR;
1123 
1124   // If the assumptions about the DECL_PARM_VAR abbrev are true, use it.  Here
1125   // we dynamically check for the properties that we optimize for, but don't
1126   // know are true of all PARM_VAR_DECLs.
1127   if (D->getDeclContext() == D->getLexicalDeclContext() &&
1128       !D->hasAttrs() &&
1129       !D->hasExtInfo() &&
1130       !D->isImplicit() &&
1131       !D->isUsed(false) &&
1132       !D->isInvalidDecl() &&
1133       !D->isReferenced() &&
1134       D->getAccess() == AS_none &&
1135       !D->isModulePrivate() &&
1136       D->getStorageClass() == 0 &&
1137       D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
1138       D->getFunctionScopeDepth() == 0 &&
1139       D->getObjCDeclQualifier() == 0 &&
1140       !D->isKNRPromoted() &&
1141       !D->hasInheritedDefaultArg() &&
1142       D->getInit() == nullptr &&
1143       !D->hasUninstantiatedDefaultArg())  // No default expr.
1144     AbbrevToUse = Writer.getDeclParmVarAbbrev();
1145 
1146   // Check things we know are true of *every* PARM_VAR_DECL, which is more than
1147   // just us assuming it.
1148   assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
1149   assert(!D->isThisDeclarationADemotedDefinition()
1150          && "PARM_VAR_DECL can't be demoted definition.");
1151   assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
1152   assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
1153   assert(D->getPreviousDecl() == nullptr && "PARM_VAR_DECL can't be redecl");
1154   assert(!D->isStaticDataMember() &&
1155          "PARM_VAR_DECL can't be static data member");
1156 }
1157 
1158 void ASTDeclWriter::VisitDecompositionDecl(DecompositionDecl *D) {
1159   // Record the number of bindings first to simplify deserialization.
1160   Record.push_back(D->bindings().size());
1161 
1162   VisitVarDecl(D);
1163   for (auto *B : D->bindings())
1164     Record.AddDeclRef(B);
1165   Code = serialization::DECL_DECOMPOSITION;
1166 }
1167 
1168 void ASTDeclWriter::VisitBindingDecl(BindingDecl *D) {
1169   VisitValueDecl(D);
1170   Record.AddStmt(D->getBinding());
1171   Code = serialization::DECL_BINDING;
1172 }
1173 
1174 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
1175   VisitDecl(D);
1176   Record.AddStmt(D->getAsmString());
1177   Record.AddSourceLocation(D->getRParenLoc());
1178   Code = serialization::DECL_FILE_SCOPE_ASM;
1179 }
1180 
1181 void ASTDeclWriter::VisitTopLevelStmtDecl(TopLevelStmtDecl *D) {
1182   VisitDecl(D);
1183   Record.AddStmt(D->getStmt());
1184   Code = serialization::DECL_TOP_LEVEL_STMT_DECL;
1185 }
1186 
1187 void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
1188   VisitDecl(D);
1189   Code = serialization::DECL_EMPTY;
1190 }
1191 
1192 void ASTDeclWriter::VisitLifetimeExtendedTemporaryDecl(
1193     LifetimeExtendedTemporaryDecl *D) {
1194   VisitDecl(D);
1195   Record.AddDeclRef(D->getExtendingDecl());
1196   Record.AddStmt(D->getTemporaryExpr());
1197   Record.push_back(static_cast<bool>(D->getValue()));
1198   if (D->getValue())
1199     Record.AddAPValue(*D->getValue());
1200   Record.push_back(D->getManglingNumber());
1201   Code = serialization::DECL_LIFETIME_EXTENDED_TEMPORARY;
1202 }
1203 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
1204   VisitDecl(D);
1205   Record.AddStmt(D->getBody());
1206   Record.AddTypeSourceInfo(D->getSignatureAsWritten());
1207   Record.push_back(D->param_size());
1208   for (ParmVarDecl *P : D->parameters())
1209     Record.AddDeclRef(P);
1210   Record.push_back(D->isVariadic());
1211   Record.push_back(D->blockMissingReturnType());
1212   Record.push_back(D->isConversionFromLambda());
1213   Record.push_back(D->doesNotEscape());
1214   Record.push_back(D->canAvoidCopyToHeap());
1215   Record.push_back(D->capturesCXXThis());
1216   Record.push_back(D->getNumCaptures());
1217   for (const auto &capture : D->captures()) {
1218     Record.AddDeclRef(capture.getVariable());
1219 
1220     unsigned flags = 0;
1221     if (capture.isByRef()) flags |= 1;
1222     if (capture.isNested()) flags |= 2;
1223     if (capture.hasCopyExpr()) flags |= 4;
1224     Record.push_back(flags);
1225 
1226     if (capture.hasCopyExpr()) Record.AddStmt(capture.getCopyExpr());
1227   }
1228 
1229   Code = serialization::DECL_BLOCK;
1230 }
1231 
1232 void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
1233   Record.push_back(CD->getNumParams());
1234   VisitDecl(CD);
1235   Record.push_back(CD->getContextParamPosition());
1236   Record.push_back(CD->isNothrow() ? 1 : 0);
1237   // Body is stored by VisitCapturedStmt.
1238   for (unsigned I = 0; I < CD->getNumParams(); ++I)
1239     Record.AddDeclRef(CD->getParam(I));
1240   Code = serialization::DECL_CAPTURED;
1241 }
1242 
1243 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
1244   VisitDecl(D);
1245   Record.push_back(D->getLanguage());
1246   Record.AddSourceLocation(D->getExternLoc());
1247   Record.AddSourceLocation(D->getRBraceLoc());
1248   Code = serialization::DECL_LINKAGE_SPEC;
1249 }
1250 
1251 void ASTDeclWriter::VisitExportDecl(ExportDecl *D) {
1252   VisitDecl(D);
1253   Record.AddSourceLocation(D->getRBraceLoc());
1254   Code = serialization::DECL_EXPORT;
1255 }
1256 
1257 void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
1258   VisitNamedDecl(D);
1259   Record.AddSourceLocation(D->getBeginLoc());
1260   Code = serialization::DECL_LABEL;
1261 }
1262 
1263 
1264 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
1265   VisitRedeclarable(D);
1266   VisitNamedDecl(D);
1267   Record.push_back(D->isInline());
1268   Record.push_back(D->isNested());
1269   Record.AddSourceLocation(D->getBeginLoc());
1270   Record.AddSourceLocation(D->getRBraceLoc());
1271 
1272   if (D->isOriginalNamespace())
1273     Record.AddDeclRef(D->getAnonymousNamespace());
1274   Code = serialization::DECL_NAMESPACE;
1275 
1276   if (Writer.hasChain() && D->isAnonymousNamespace() &&
1277       D == D->getMostRecentDecl()) {
1278     // This is a most recent reopening of the anonymous namespace. If its parent
1279     // is in a previous PCH (or is the TU), mark that parent for update, because
1280     // the original namespace always points to the latest re-opening of its
1281     // anonymous namespace.
1282     Decl *Parent = cast<Decl>(
1283         D->getParent()->getRedeclContext()->getPrimaryContext());
1284     if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
1285       Writer.DeclUpdates[Parent].push_back(
1286           ASTWriter::DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, D));
1287     }
1288   }
1289 }
1290 
1291 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
1292   VisitRedeclarable(D);
1293   VisitNamedDecl(D);
1294   Record.AddSourceLocation(D->getNamespaceLoc());
1295   Record.AddSourceLocation(D->getTargetNameLoc());
1296   Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1297   Record.AddDeclRef(D->getNamespace());
1298   Code = serialization::DECL_NAMESPACE_ALIAS;
1299 }
1300 
1301 void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
1302   VisitNamedDecl(D);
1303   Record.AddSourceLocation(D->getUsingLoc());
1304   Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1305   Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1306   Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1307   Record.push_back(D->hasTypename());
1308   Record.AddDeclRef(Context.getInstantiatedFromUsingDecl(D));
1309   Code = serialization::DECL_USING;
1310 }
1311 
1312 void ASTDeclWriter::VisitUsingEnumDecl(UsingEnumDecl *D) {
1313   VisitNamedDecl(D);
1314   Record.AddSourceLocation(D->getUsingLoc());
1315   Record.AddSourceLocation(D->getEnumLoc());
1316   Record.AddTypeSourceInfo(D->getEnumType());
1317   Record.AddDeclRef(D->FirstUsingShadow.getPointer());
1318   Record.AddDeclRef(Context.getInstantiatedFromUsingEnumDecl(D));
1319   Code = serialization::DECL_USING_ENUM;
1320 }
1321 
1322 void ASTDeclWriter::VisitUsingPackDecl(UsingPackDecl *D) {
1323   Record.push_back(D->NumExpansions);
1324   VisitNamedDecl(D);
1325   Record.AddDeclRef(D->getInstantiatedFromUsingDecl());
1326   for (auto *E : D->expansions())
1327     Record.AddDeclRef(E);
1328   Code = serialization::DECL_USING_PACK;
1329 }
1330 
1331 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
1332   VisitRedeclarable(D);
1333   VisitNamedDecl(D);
1334   Record.AddDeclRef(D->getTargetDecl());
1335   Record.push_back(D->getIdentifierNamespace());
1336   Record.AddDeclRef(D->UsingOrNextShadow);
1337   Record.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D));
1338   Code = serialization::DECL_USING_SHADOW;
1339 }
1340 
1341 void ASTDeclWriter::VisitConstructorUsingShadowDecl(
1342     ConstructorUsingShadowDecl *D) {
1343   VisitUsingShadowDecl(D);
1344   Record.AddDeclRef(D->NominatedBaseClassShadowDecl);
1345   Record.AddDeclRef(D->ConstructedBaseClassShadowDecl);
1346   Record.push_back(D->IsVirtual);
1347   Code = serialization::DECL_CONSTRUCTOR_USING_SHADOW;
1348 }
1349 
1350 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
1351   VisitNamedDecl(D);
1352   Record.AddSourceLocation(D->getUsingLoc());
1353   Record.AddSourceLocation(D->getNamespaceKeyLocation());
1354   Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1355   Record.AddDeclRef(D->getNominatedNamespace());
1356   Record.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()));
1357   Code = serialization::DECL_USING_DIRECTIVE;
1358 }
1359 
1360 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
1361   VisitValueDecl(D);
1362   Record.AddSourceLocation(D->getUsingLoc());
1363   Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1364   Record.AddDeclarationNameLoc(D->DNLoc, D->getDeclName());
1365   Record.AddSourceLocation(D->getEllipsisLoc());
1366   Code = serialization::DECL_UNRESOLVED_USING_VALUE;
1367 }
1368 
1369 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
1370                                                UnresolvedUsingTypenameDecl *D) {
1371   VisitTypeDecl(D);
1372   Record.AddSourceLocation(D->getTypenameLoc());
1373   Record.AddNestedNameSpecifierLoc(D->getQualifierLoc());
1374   Record.AddSourceLocation(D->getEllipsisLoc());
1375   Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
1376 }
1377 
1378 void ASTDeclWriter::VisitUnresolvedUsingIfExistsDecl(
1379     UnresolvedUsingIfExistsDecl *D) {
1380   VisitNamedDecl(D);
1381   Code = serialization::DECL_UNRESOLVED_USING_IF_EXISTS;
1382 }
1383 
1384 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
1385   VisitRecordDecl(D);
1386 
1387   enum {
1388     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
1389   };
1390   if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
1391     Record.push_back(CXXRecTemplate);
1392     Record.AddDeclRef(TemplD);
1393   } else if (MemberSpecializationInfo *MSInfo
1394                = D->getMemberSpecializationInfo()) {
1395     Record.push_back(CXXRecMemberSpecialization);
1396     Record.AddDeclRef(MSInfo->getInstantiatedFrom());
1397     Record.push_back(MSInfo->getTemplateSpecializationKind());
1398     Record.AddSourceLocation(MSInfo->getPointOfInstantiation());
1399   } else {
1400     Record.push_back(CXXRecNotTemplate);
1401   }
1402 
1403   Record.push_back(D->isThisDeclarationADefinition());
1404   if (D->isThisDeclarationADefinition())
1405     Record.AddCXXDefinitionData(D);
1406 
1407   // Store (what we currently believe to be) the key function to avoid
1408   // deserializing every method so we can compute it.
1409   if (D->isCompleteDefinition())
1410     Record.AddDeclRef(Context.getCurrentKeyFunction(D));
1411 
1412   Code = serialization::DECL_CXX_RECORD;
1413 }
1414 
1415 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
1416   VisitFunctionDecl(D);
1417   if (D->isCanonicalDecl()) {
1418     Record.push_back(D->size_overridden_methods());
1419     for (const CXXMethodDecl *MD : D->overridden_methods())
1420       Record.AddDeclRef(MD);
1421   } else {
1422     // We only need to record overridden methods once for the canonical decl.
1423     Record.push_back(0);
1424   }
1425 
1426   if (D->getDeclContext() == D->getLexicalDeclContext() &&
1427       D->getFirstDecl() == D->getMostRecentDecl() && !D->isInvalidDecl() &&
1428       !D->hasAttrs() && !D->isTopLevelDeclInObjCContainer() &&
1429       D->getDeclName().getNameKind() == DeclarationName::Identifier &&
1430       !D->hasExtInfo() && !D->hasInheritedPrototype() &&
1431       D->hasWrittenPrototype() &&
1432       D->getTemplatedKind() == FunctionDecl::TK_NonTemplate)
1433     AbbrevToUse = Writer.getDeclCXXMethodAbbrev();
1434 
1435   Code = serialization::DECL_CXX_METHOD;
1436 }
1437 
1438 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1439   Record.push_back(D->getTrailingAllocKind());
1440   addExplicitSpecifier(D->getExplicitSpecifier(), Record);
1441   if (auto Inherited = D->getInheritedConstructor()) {
1442     Record.AddDeclRef(Inherited.getShadowDecl());
1443     Record.AddDeclRef(Inherited.getConstructor());
1444   }
1445 
1446   VisitCXXMethodDecl(D);
1447   Code = serialization::DECL_CXX_CONSTRUCTOR;
1448 }
1449 
1450 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1451   VisitCXXMethodDecl(D);
1452 
1453   Record.AddDeclRef(D->getOperatorDelete());
1454   if (D->getOperatorDelete())
1455     Record.AddStmt(D->getOperatorDeleteThisArg());
1456 
1457   Code = serialization::DECL_CXX_DESTRUCTOR;
1458 }
1459 
1460 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1461   addExplicitSpecifier(D->getExplicitSpecifier(), Record);
1462   VisitCXXMethodDecl(D);
1463   Code = serialization::DECL_CXX_CONVERSION;
1464 }
1465 
1466 void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
1467   VisitDecl(D);
1468   Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
1469   ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
1470   Record.push_back(!IdentifierLocs.empty());
1471   if (IdentifierLocs.empty()) {
1472     Record.AddSourceLocation(D->getEndLoc());
1473     Record.push_back(1);
1474   } else {
1475     for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
1476       Record.AddSourceLocation(IdentifierLocs[I]);
1477     Record.push_back(IdentifierLocs.size());
1478   }
1479   // Note: the number of source locations must always be the last element in
1480   // the record.
1481   Code = serialization::DECL_IMPORT;
1482 }
1483 
1484 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1485   VisitDecl(D);
1486   Record.AddSourceLocation(D->getColonLoc());
1487   Code = serialization::DECL_ACCESS_SPEC;
1488 }
1489 
1490 void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
1491   // Record the number of friend type template parameter lists here
1492   // so as to simplify memory allocation during deserialization.
1493   Record.push_back(D->NumTPLists);
1494   VisitDecl(D);
1495   bool hasFriendDecl = D->Friend.is<NamedDecl*>();
1496   Record.push_back(hasFriendDecl);
1497   if (hasFriendDecl)
1498     Record.AddDeclRef(D->getFriendDecl());
1499   else
1500     Record.AddTypeSourceInfo(D->getFriendType());
1501   for (unsigned i = 0; i < D->NumTPLists; ++i)
1502     Record.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i));
1503   Record.AddDeclRef(D->getNextFriend());
1504   Record.push_back(D->UnsupportedFriend);
1505   Record.AddSourceLocation(D->FriendLoc);
1506   Code = serialization::DECL_FRIEND;
1507 }
1508 
1509 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1510   VisitDecl(D);
1511   Record.push_back(D->getNumTemplateParameters());
1512   for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
1513     Record.AddTemplateParameterList(D->getTemplateParameterList(i));
1514   Record.push_back(D->getFriendDecl() != nullptr);
1515   if (D->getFriendDecl())
1516     Record.AddDeclRef(D->getFriendDecl());
1517   else
1518     Record.AddTypeSourceInfo(D->getFriendType());
1519   Record.AddSourceLocation(D->getFriendLoc());
1520   Code = serialization::DECL_FRIEND_TEMPLATE;
1521 }
1522 
1523 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
1524   VisitNamedDecl(D);
1525 
1526   Record.AddTemplateParameterList(D->getTemplateParameters());
1527   Record.AddDeclRef(D->getTemplatedDecl());
1528 }
1529 
1530 void ASTDeclWriter::VisitConceptDecl(ConceptDecl *D) {
1531   VisitTemplateDecl(D);
1532   Record.AddStmt(D->getConstraintExpr());
1533   Code = serialization::DECL_CONCEPT;
1534 }
1535 
1536 void ASTDeclWriter::VisitImplicitConceptSpecializationDecl(
1537     ImplicitConceptSpecializationDecl *D) {
1538   Record.push_back(D->getTemplateArguments().size());
1539   VisitDecl(D);
1540   for (const TemplateArgument &Arg : D->getTemplateArguments())
1541     Record.AddTemplateArgument(Arg);
1542   Code = serialization::DECL_IMPLICIT_CONCEPT_SPECIALIZATION;
1543 }
1544 
1545 void ASTDeclWriter::VisitRequiresExprBodyDecl(RequiresExprBodyDecl *D) {
1546   Code = serialization::DECL_REQUIRES_EXPR_BODY;
1547 }
1548 
1549 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1550   VisitRedeclarable(D);
1551 
1552   // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
1553   // getCommonPtr() can be used while this is still initializing.
1554   if (D->isFirstDecl()) {
1555     // This declaration owns the 'common' pointer, so serialize that data now.
1556     Record.AddDeclRef(D->getInstantiatedFromMemberTemplate());
1557     if (D->getInstantiatedFromMemberTemplate())
1558       Record.push_back(D->isMemberSpecialization());
1559   }
1560 
1561   VisitTemplateDecl(D);
1562   Record.push_back(D->getIdentifierNamespace());
1563 }
1564 
1565 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1566   VisitRedeclarableTemplateDecl(D);
1567 
1568   if (D->isFirstDecl())
1569     AddTemplateSpecializations(D);
1570   Code = serialization::DECL_CLASS_TEMPLATE;
1571 }
1572 
1573 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
1574                                            ClassTemplateSpecializationDecl *D) {
1575   RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
1576 
1577   VisitCXXRecordDecl(D);
1578 
1579   llvm::PointerUnion<ClassTemplateDecl *,
1580                      ClassTemplatePartialSpecializationDecl *> InstFrom
1581     = D->getSpecializedTemplateOrPartial();
1582   if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
1583     Record.AddDeclRef(InstFromD);
1584   } else {
1585     Record.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>());
1586     Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
1587   }
1588 
1589   Record.AddTemplateArgumentList(&D->getTemplateArgs());
1590   Record.AddSourceLocation(D->getPointOfInstantiation());
1591   Record.push_back(D->getSpecializationKind());
1592   Record.push_back(D->isCanonicalDecl());
1593 
1594   if (D->isCanonicalDecl()) {
1595     // When reading, we'll add it to the folding set of the following template.
1596     Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
1597   }
1598 
1599   // Explicit info.
1600   Record.AddTypeSourceInfo(D->getTypeAsWritten());
1601   if (D->getTypeAsWritten()) {
1602     Record.AddSourceLocation(D->getExternLoc());
1603     Record.AddSourceLocation(D->getTemplateKeywordLoc());
1604   }
1605 
1606   Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
1607 }
1608 
1609 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
1610                                     ClassTemplatePartialSpecializationDecl *D) {
1611   Record.AddTemplateParameterList(D->getTemplateParameters());
1612   Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
1613 
1614   VisitClassTemplateSpecializationDecl(D);
1615 
1616   // These are read/set from/to the first declaration.
1617   if (D->getPreviousDecl() == nullptr) {
1618     Record.AddDeclRef(D->getInstantiatedFromMember());
1619     Record.push_back(D->isMemberSpecialization());
1620   }
1621 
1622   Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
1623 }
1624 
1625 void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
1626   VisitRedeclarableTemplateDecl(D);
1627 
1628   if (D->isFirstDecl())
1629     AddTemplateSpecializations(D);
1630   Code = serialization::DECL_VAR_TEMPLATE;
1631 }
1632 
1633 void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
1634     VarTemplateSpecializationDecl *D) {
1635   RegisterTemplateSpecialization(D->getSpecializedTemplate(), D);
1636 
1637   llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
1638   InstFrom = D->getSpecializedTemplateOrPartial();
1639   if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
1640     Record.AddDeclRef(InstFromD);
1641   } else {
1642     Record.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>());
1643     Record.AddTemplateArgumentList(&D->getTemplateInstantiationArgs());
1644   }
1645 
1646   // Explicit info.
1647   Record.AddTypeSourceInfo(D->getTypeAsWritten());
1648   if (D->getTypeAsWritten()) {
1649     Record.AddSourceLocation(D->getExternLoc());
1650     Record.AddSourceLocation(D->getTemplateKeywordLoc());
1651   }
1652 
1653   Record.AddTemplateArgumentList(&D->getTemplateArgs());
1654   Record.AddSourceLocation(D->getPointOfInstantiation());
1655   Record.push_back(D->getSpecializationKind());
1656   Record.push_back(D->IsCompleteDefinition);
1657 
1658   VisitVarDecl(D);
1659 
1660   Record.push_back(D->isCanonicalDecl());
1661 
1662   if (D->isCanonicalDecl()) {
1663     // When reading, we'll add it to the folding set of the following template.
1664     Record.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl());
1665   }
1666 
1667   Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
1668 }
1669 
1670 void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
1671     VarTemplatePartialSpecializationDecl *D) {
1672   Record.AddTemplateParameterList(D->getTemplateParameters());
1673   Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
1674 
1675   VisitVarTemplateSpecializationDecl(D);
1676 
1677   // These are read/set from/to the first declaration.
1678   if (D->getPreviousDecl() == nullptr) {
1679     Record.AddDeclRef(D->getInstantiatedFromMember());
1680     Record.push_back(D->isMemberSpecialization());
1681   }
1682 
1683   Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
1684 }
1685 
1686 void ASTDeclWriter::VisitClassScopeFunctionSpecializationDecl(
1687                                     ClassScopeFunctionSpecializationDecl *D) {
1688   VisitDecl(D);
1689   Record.AddDeclRef(D->getSpecialization());
1690   Record.push_back(D->hasExplicitTemplateArgs());
1691   if (D->hasExplicitTemplateArgs())
1692     Record.AddASTTemplateArgumentListInfo(D->getTemplateArgsAsWritten());
1693   Code = serialization::DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION;
1694 }
1695 
1696 
1697 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1698   VisitRedeclarableTemplateDecl(D);
1699 
1700   if (D->isFirstDecl())
1701     AddTemplateSpecializations(D);
1702   Code = serialization::DECL_FUNCTION_TEMPLATE;
1703 }
1704 
1705 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1706   Record.push_back(D->hasTypeConstraint());
1707   VisitTypeDecl(D);
1708 
1709   Record.push_back(D->wasDeclaredWithTypename());
1710 
1711   const TypeConstraint *TC = D->getTypeConstraint();
1712   Record.push_back(TC != nullptr);
1713   if (TC) {
1714     Record.AddNestedNameSpecifierLoc(TC->getNestedNameSpecifierLoc());
1715     Record.AddDeclarationNameInfo(TC->getConceptNameInfo());
1716     Record.AddDeclRef(TC->getNamedConcept());
1717     Record.push_back(TC->getTemplateArgsAsWritten() != nullptr);
1718     if (TC->getTemplateArgsAsWritten())
1719       Record.AddASTTemplateArgumentListInfo(TC->getTemplateArgsAsWritten());
1720     Record.AddStmt(TC->getImmediatelyDeclaredConstraint());
1721     Record.push_back(D->isExpandedParameterPack());
1722     if (D->isExpandedParameterPack())
1723       Record.push_back(D->getNumExpansionParameters());
1724   }
1725 
1726   bool OwnsDefaultArg = D->hasDefaultArgument() &&
1727                         !D->defaultArgumentWasInherited();
1728   Record.push_back(OwnsDefaultArg);
1729   if (OwnsDefaultArg)
1730     Record.AddTypeSourceInfo(D->getDefaultArgumentInfo());
1731 
1732   Code = serialization::DECL_TEMPLATE_TYPE_PARM;
1733 }
1734 
1735 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1736   // For an expanded parameter pack, record the number of expansion types here
1737   // so that it's easier for deserialization to allocate the right amount of
1738   // memory.
1739   Expr *TypeConstraint = D->getPlaceholderTypeConstraint();
1740   Record.push_back(!!TypeConstraint);
1741   if (D->isExpandedParameterPack())
1742     Record.push_back(D->getNumExpansionTypes());
1743 
1744   VisitDeclaratorDecl(D);
1745   // TemplateParmPosition.
1746   Record.push_back(D->getDepth());
1747   Record.push_back(D->getPosition());
1748   if (TypeConstraint)
1749     Record.AddStmt(TypeConstraint);
1750 
1751   if (D->isExpandedParameterPack()) {
1752     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1753       Record.AddTypeRef(D->getExpansionType(I));
1754       Record.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I));
1755     }
1756 
1757     Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
1758   } else {
1759     // Rest of NonTypeTemplateParmDecl.
1760     Record.push_back(D->isParameterPack());
1761     bool OwnsDefaultArg = D->hasDefaultArgument() &&
1762                           !D->defaultArgumentWasInherited();
1763     Record.push_back(OwnsDefaultArg);
1764     if (OwnsDefaultArg)
1765       Record.AddStmt(D->getDefaultArgument());
1766     Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
1767   }
1768 }
1769 
1770 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1771   // For an expanded parameter pack, record the number of expansion types here
1772   // so that it's easier for deserialization to allocate the right amount of
1773   // memory.
1774   if (D->isExpandedParameterPack())
1775     Record.push_back(D->getNumExpansionTemplateParameters());
1776 
1777   VisitTemplateDecl(D);
1778   // TemplateParmPosition.
1779   Record.push_back(D->getDepth());
1780   Record.push_back(D->getPosition());
1781 
1782   if (D->isExpandedParameterPack()) {
1783     for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1784          I != N; ++I)
1785       Record.AddTemplateParameterList(D->getExpansionTemplateParameters(I));
1786     Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
1787   } else {
1788     // Rest of TemplateTemplateParmDecl.
1789     Record.push_back(D->isParameterPack());
1790     bool OwnsDefaultArg = D->hasDefaultArgument() &&
1791                           !D->defaultArgumentWasInherited();
1792     Record.push_back(OwnsDefaultArg);
1793     if (OwnsDefaultArg)
1794       Record.AddTemplateArgumentLoc(D->getDefaultArgument());
1795     Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
1796   }
1797 }
1798 
1799 void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1800   VisitRedeclarableTemplateDecl(D);
1801   Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
1802 }
1803 
1804 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1805   VisitDecl(D);
1806   Record.AddStmt(D->getAssertExpr());
1807   Record.push_back(D->isFailed());
1808   Record.AddStmt(D->getMessage());
1809   Record.AddSourceLocation(D->getRParenLoc());
1810   Code = serialization::DECL_STATIC_ASSERT;
1811 }
1812 
1813 /// Emit the DeclContext part of a declaration context decl.
1814 void ASTDeclWriter::VisitDeclContext(DeclContext *DC) {
1815   Record.AddOffset(Writer.WriteDeclContextLexicalBlock(Context, DC));
1816   Record.AddOffset(Writer.WriteDeclContextVisibleBlock(Context, DC));
1817 }
1818 
1819 const Decl *ASTWriter::getFirstLocalDecl(const Decl *D) {
1820   assert(IsLocalDecl(D) && "expected a local declaration");
1821 
1822   const Decl *Canon = D->getCanonicalDecl();
1823   if (IsLocalDecl(Canon))
1824     return Canon;
1825 
1826   const Decl *&CacheEntry = FirstLocalDeclCache[Canon];
1827   if (CacheEntry)
1828     return CacheEntry;
1829 
1830   for (const Decl *Redecl = D; Redecl; Redecl = Redecl->getPreviousDecl())
1831     if (IsLocalDecl(Redecl))
1832       D = Redecl;
1833   return CacheEntry = D;
1834 }
1835 
1836 template <typename T>
1837 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
1838   T *First = D->getFirstDecl();
1839   T *MostRecent = First->getMostRecentDecl();
1840   T *DAsT = static_cast<T *>(D);
1841   if (MostRecent != First) {
1842     assert(isRedeclarableDeclKind(DAsT->getKind()) &&
1843            "Not considered redeclarable?");
1844 
1845     Record.AddDeclRef(First);
1846 
1847     // Write out a list of local redeclarations of this declaration if it's the
1848     // first local declaration in the chain.
1849     const Decl *FirstLocal = Writer.getFirstLocalDecl(DAsT);
1850     if (DAsT == FirstLocal) {
1851       // Emit a list of all imported first declarations so that we can be sure
1852       // that all redeclarations visible to this module are before D in the
1853       // redecl chain.
1854       unsigned I = Record.size();
1855       Record.push_back(0);
1856       if (Writer.Chain)
1857         AddFirstDeclFromEachModule(DAsT, /*IncludeLocal*/false);
1858       // This is the number of imported first declarations + 1.
1859       Record[I] = Record.size() - I;
1860 
1861       // Collect the set of local redeclarations of this declaration, from
1862       // newest to oldest.
1863       ASTWriter::RecordData LocalRedecls;
1864       ASTRecordWriter LocalRedeclWriter(Record, LocalRedecls);
1865       for (const Decl *Prev = FirstLocal->getMostRecentDecl();
1866            Prev != FirstLocal; Prev = Prev->getPreviousDecl())
1867         if (!Prev->isFromASTFile())
1868           LocalRedeclWriter.AddDeclRef(Prev);
1869 
1870       // If we have any redecls, write them now as a separate record preceding
1871       // the declaration itself.
1872       if (LocalRedecls.empty())
1873         Record.push_back(0);
1874       else
1875         Record.AddOffset(LocalRedeclWriter.Emit(LOCAL_REDECLARATIONS));
1876     } else {
1877       Record.push_back(0);
1878       Record.AddDeclRef(FirstLocal);
1879     }
1880 
1881     // Make sure that we serialize both the previous and the most-recent
1882     // declarations, which (transitively) ensures that all declarations in the
1883     // chain get serialized.
1884     //
1885     // FIXME: This is not correct; when we reach an imported declaration we
1886     // won't emit its previous declaration.
1887     (void)Writer.GetDeclRef(D->getPreviousDecl());
1888     (void)Writer.GetDeclRef(MostRecent);
1889   } else {
1890     // We use the sentinel value 0 to indicate an only declaration.
1891     Record.push_back(0);
1892   }
1893 }
1894 
1895 void ASTDeclWriter::VisitHLSLBufferDecl(HLSLBufferDecl *D) {
1896   VisitNamedDecl(D);
1897   VisitDeclContext(D);
1898   Record.push_back(D->isCBuffer());
1899   Record.AddSourceLocation(D->getLocStart());
1900   Record.AddSourceLocation(D->getLBraceLoc());
1901   Record.AddSourceLocation(D->getRBraceLoc());
1902 
1903   Code = serialization::DECL_HLSL_BUFFER;
1904 }
1905 
1906 void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1907   Record.writeOMPChildren(D->Data);
1908   VisitDecl(D);
1909   Code = serialization::DECL_OMP_THREADPRIVATE;
1910 }
1911 
1912 void ASTDeclWriter::VisitOMPAllocateDecl(OMPAllocateDecl *D) {
1913   Record.writeOMPChildren(D->Data);
1914   VisitDecl(D);
1915   Code = serialization::DECL_OMP_ALLOCATE;
1916 }
1917 
1918 void ASTDeclWriter::VisitOMPRequiresDecl(OMPRequiresDecl *D) {
1919   Record.writeOMPChildren(D->Data);
1920   VisitDecl(D);
1921   Code = serialization::DECL_OMP_REQUIRES;
1922 }
1923 
1924 void ASTDeclWriter::VisitOMPDeclareReductionDecl(OMPDeclareReductionDecl *D) {
1925   VisitValueDecl(D);
1926   Record.AddSourceLocation(D->getBeginLoc());
1927   Record.AddStmt(D->getCombinerIn());
1928   Record.AddStmt(D->getCombinerOut());
1929   Record.AddStmt(D->getCombiner());
1930   Record.AddStmt(D->getInitOrig());
1931   Record.AddStmt(D->getInitPriv());
1932   Record.AddStmt(D->getInitializer());
1933   Record.push_back(D->getInitializerKind());
1934   Record.AddDeclRef(D->getPrevDeclInScope());
1935   Code = serialization::DECL_OMP_DECLARE_REDUCTION;
1936 }
1937 
1938 void ASTDeclWriter::VisitOMPDeclareMapperDecl(OMPDeclareMapperDecl *D) {
1939   Record.writeOMPChildren(D->Data);
1940   VisitValueDecl(D);
1941   Record.AddDeclarationName(D->getVarName());
1942   Record.AddDeclRef(D->getPrevDeclInScope());
1943   Code = serialization::DECL_OMP_DECLARE_MAPPER;
1944 }
1945 
1946 void ASTDeclWriter::VisitOMPCapturedExprDecl(OMPCapturedExprDecl *D) {
1947   VisitVarDecl(D);
1948   Code = serialization::DECL_OMP_CAPTUREDEXPR;
1949 }
1950 
1951 //===----------------------------------------------------------------------===//
1952 // ASTWriter Implementation
1953 //===----------------------------------------------------------------------===//
1954 
1955 void ASTWriter::WriteDeclAbbrevs() {
1956   using namespace llvm;
1957 
1958   std::shared_ptr<BitCodeAbbrev> Abv;
1959 
1960   // Abbreviation for DECL_FIELD
1961   Abv = std::make_shared<BitCodeAbbrev>();
1962   Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
1963   // Decl
1964   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1965   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
1966   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1967   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1968   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1969   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1970   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1971   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1972   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
1973   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));  // ModuleOwnershipKind
1974   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1975   // NamedDecl
1976   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1977   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1978   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
1979   // ValueDecl
1980   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1981   // DeclaratorDecl
1982   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1983   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
1984   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
1985   // FieldDecl
1986   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
1987   Abv->Add(BitCodeAbbrevOp(0));                       // InitStyle
1988   // Type Source Info
1989   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1990   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1991   DeclFieldAbbrev = Stream.EmitAbbrev(std::move(Abv));
1992 
1993   // Abbreviation for DECL_OBJC_IVAR
1994   Abv = std::make_shared<BitCodeAbbrev>();
1995   Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
1996   // Decl
1997   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1998   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
1999   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2000   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2001   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2002   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
2003   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
2004   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2005   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
2006   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3));  // ModuleOwnershipKind
2007   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2008   // NamedDecl
2009   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2010   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2011   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2012   // ValueDecl
2013   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2014   // DeclaratorDecl
2015   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2016   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
2017   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2018   // FieldDecl
2019   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
2020   Abv->Add(BitCodeAbbrevOp(0));                       // InitStyle
2021   // ObjC Ivar
2022   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
2023   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
2024   // Type Source Info
2025   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2026   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2027   DeclObjCIvarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2028 
2029   // Abbreviation for DECL_ENUM
2030   Abv = std::make_shared<BitCodeAbbrev>();
2031   Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
2032   // Redeclarable
2033   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
2034   // Decl
2035   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2036   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
2037   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2038   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2039   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2040   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
2041   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
2042   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2043   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
2044   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2045   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2046   // NamedDecl
2047   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2048   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2049   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2050   // TypeDecl
2051   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2052   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2053   // TagDecl
2054   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
2055   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
2056   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
2057   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
2058   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
2059   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
2060   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
2061   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
2062   Abv->Add(BitCodeAbbrevOp(0));                         // ExtInfoKind
2063   // EnumDecl
2064   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddTypeRef
2065   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IntegerType
2066   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getPromotionType
2067   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumPositiveBits
2068   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumNegativeBits
2069   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScoped
2070   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScopedUsingClassTag
2071   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isFixed
2072   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));// ODRHash
2073   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // InstantiatedMembEnum
2074   // DC
2075   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
2076   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
2077   DeclEnumAbbrev = Stream.EmitAbbrev(std::move(Abv));
2078 
2079   // Abbreviation for DECL_RECORD
2080   Abv = std::make_shared<BitCodeAbbrev>();
2081   Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
2082   // Redeclarable
2083   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
2084   // Decl
2085   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2086   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
2087   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2088   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2089   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2090   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
2091   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
2092   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2093   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
2094   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2095   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2096   // NamedDecl
2097   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2098   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2099   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2100   // TypeDecl
2101   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2102   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2103   // TagDecl
2104   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
2105   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
2106   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
2107   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
2108   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
2109   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
2110   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
2111   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
2112   Abv->Add(BitCodeAbbrevOp(0));                         // ExtInfoKind
2113   // RecordDecl
2114   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FlexibleArrayMember
2115   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // AnonymousStructUnion
2116   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasObjectMember
2117   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasVolatileMember
2118 
2119   // isNonTrivialToPrimitiveDefaultInitialize
2120   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2121   // isNonTrivialToPrimitiveCopy
2122   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2123   // isNonTrivialToPrimitiveDestroy
2124   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2125   // hasNonTrivialToPrimitiveDefaultInitializeCUnion
2126   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2127   // hasNonTrivialToPrimitiveDestructCUnion
2128   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2129   // hasNonTrivialToPrimitiveCopyCUnion
2130   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2131   // isParamDestroyedInCallee
2132   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1));
2133   // getArgPassingRestrictions
2134   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));
2135   // ODRHash
2136   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 26));
2137 
2138   // DC
2139   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
2140   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
2141   DeclRecordAbbrev = Stream.EmitAbbrev(std::move(Abv));
2142 
2143   // Abbreviation for DECL_PARM_VAR
2144   Abv = std::make_shared<BitCodeAbbrev>();
2145   Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
2146   // Redeclarable
2147   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
2148   // Decl
2149   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2150   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
2151   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2152   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2153   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2154   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
2155   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
2156   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2157   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
2158   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2159   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2160   // NamedDecl
2161   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2162   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2163   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2164   // ValueDecl
2165   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2166   // DeclaratorDecl
2167   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2168   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
2169   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2170   // VarDecl
2171   Abv->Add(BitCodeAbbrevOp(0));                       // SClass
2172   Abv->Add(BitCodeAbbrevOp(0));                       // TSCSpec
2173   Abv->Add(BitCodeAbbrevOp(0));                       // InitStyle
2174   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
2175   Abv->Add(BitCodeAbbrevOp(0));                       // Linkage
2176   Abv->Add(BitCodeAbbrevOp(0));                       // HasInit
2177   Abv->Add(BitCodeAbbrevOp(0));                   // HasMemberSpecializationInfo
2178   // ParmVarDecl
2179   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsObjCMethodParameter
2180   Abv->Add(BitCodeAbbrevOp(0));                       // ScopeDepth
2181   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
2182   Abv->Add(BitCodeAbbrevOp(0));                       // ObjCDeclQualifier
2183   Abv->Add(BitCodeAbbrevOp(0));                       // KNRPromoted
2184   Abv->Add(BitCodeAbbrevOp(0));                       // HasInheritedDefaultArg
2185   Abv->Add(BitCodeAbbrevOp(0));                   // HasUninstantiatedDefaultArg
2186   // Type Source Info
2187   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2188   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2189   DeclParmVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2190 
2191   // Abbreviation for DECL_TYPEDEF
2192   Abv = std::make_shared<BitCodeAbbrev>();
2193   Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
2194   // Redeclarable
2195   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
2196   // Decl
2197   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2198   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
2199   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2200   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2201   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2202   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isUsed
2203   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isReferenced
2204   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2205   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // C++ AccessSpecifier
2206   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2207   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2208   // NamedDecl
2209   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2210   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2211   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2212   // TypeDecl
2213   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
2214   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
2215   // TypedefDecl
2216   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2217   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2218   DeclTypedefAbbrev = Stream.EmitAbbrev(std::move(Abv));
2219 
2220   // Abbreviation for DECL_VAR
2221   Abv = std::make_shared<BitCodeAbbrev>();
2222   Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
2223   // Redeclarable
2224   Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
2225   // Decl
2226   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
2227   Abv->Add(BitCodeAbbrevOp(0));                       // LexicalDeclContext
2228   Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
2229   Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
2230   Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
2231   Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
2232   Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
2233   Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
2234   Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
2235   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2236   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
2237   // NamedDecl
2238   Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
2239   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
2240   Abv->Add(BitCodeAbbrevOp(0));                       // AnonDeclNumber
2241   // ValueDecl
2242   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2243   // DeclaratorDecl
2244   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
2245   Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
2246   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TSIType
2247   // VarDecl
2248   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // SClass
2249   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // TSCSpec
2250   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // InitStyle
2251   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
2252   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsThisDeclarationADemotedDefinition
2253   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isExceptionVariable
2254   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isNRVOVariable
2255   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCXXForRangeDecl
2256   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isObjCForDecl
2257   Abv->Add(BitCodeAbbrevOp(0));                         // isInline
2258   Abv->Add(BitCodeAbbrevOp(0));                         // isInlineSpecified
2259   Abv->Add(BitCodeAbbrevOp(0));                         // isConstexpr
2260   Abv->Add(BitCodeAbbrevOp(0));                         // isInitCapture
2261   Abv->Add(BitCodeAbbrevOp(0));                         // isPrevDeclInSameScope
2262   Abv->Add(BitCodeAbbrevOp(0));                         // ImplicitParamKind
2263   Abv->Add(BitCodeAbbrevOp(0));                         // EscapingByref
2264   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
2265   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // HasConstant*
2266   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // VarKind (local enum)
2267   // Type Source Info
2268   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2269   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
2270   DeclVarAbbrev = Stream.EmitAbbrev(std::move(Abv));
2271 
2272   // Abbreviation for DECL_CXX_METHOD
2273   Abv = std::make_shared<BitCodeAbbrev>();
2274   Abv->Add(BitCodeAbbrevOp(serialization::DECL_CXX_METHOD));
2275   // RedeclarableDecl
2276   Abv->Add(BitCodeAbbrevOp(0));                         // CanonicalDecl
2277   // FIXME: Implement abbreviation for other template kinds.
2278   Abv->Add(BitCodeAbbrevOp(FunctionDecl::TK_NonTemplate)); // TemplateKind
2279   // Decl
2280   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // DeclContext
2281   Abv->Add(BitCodeAbbrevOp(0));                         // LexicalDeclContext
2282   Abv->Add(BitCodeAbbrevOp(0));                         // Invalid
2283   Abv->Add(BitCodeAbbrevOp(0));                         // HasAttrs
2284   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Implicit
2285   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Used
2286   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Referenced
2287   Abv->Add(BitCodeAbbrevOp(0));                         // InObjCContainer
2288   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Access
2289   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // ModuleOwnershipKind
2290   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SubmoduleID
2291   // NamedDecl
2292   Abv->Add(BitCodeAbbrevOp(DeclarationName::Identifier)); // NameKind
2293   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Identifier
2294   Abv->Add(BitCodeAbbrevOp(0));                         // AnonDeclNumber
2295   // ValueDecl
2296   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
2297   // DeclaratorDecl
2298   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // InnerLocStart
2299   Abv->Add(BitCodeAbbrevOp(0));                         // HasExtInfo
2300   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TSIType
2301   // FunctionDecl
2302   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 11)); // IDNS
2303   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // StorageClass
2304   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Inline
2305   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InlineSpecified
2306   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // VirtualAsWritten
2307   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Pure
2308   Abv->Add(BitCodeAbbrevOp(0));                         // HasInheritedProto
2309   Abv->Add(BitCodeAbbrevOp(1));                         // HasWrittenProto
2310   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Deleted
2311   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Trivial
2312   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // TrivialForCall
2313   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Defaulted
2314   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ExplicitlyDefaulted
2315   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsIneligibleOrNotSelected
2316   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ImplicitReturnZero
2317   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Constexpr
2318   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // UsesSEHTry
2319   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // SkippedBody
2320   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // MultiVersion
2321   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // LateParsed
2322   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FriendConstraintRefersToEnclosingTemplate
2323   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
2324   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LocEnd
2325   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Default
2326   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // ODRHash
2327   // This Array slurps the rest of the record. Fortunately we want to encode
2328   // (nearly) all the remaining (variable number of) fields in the same way.
2329   //
2330   // This is:
2331   //         NumParams and Params[] from FunctionDecl, and
2332   //         NumOverriddenMethods, OverriddenMethods[] from CXXMethodDecl.
2333   //
2334   //  Add an AbbrevOp for 'size then elements' and use it here.
2335   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
2336   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
2337   DeclCXXMethodAbbrev = Stream.EmitAbbrev(std::move(Abv));
2338 
2339   unsigned ExprDependenceBits = llvm::BitWidth<ExprDependence>;
2340   // Abbreviation for EXPR_DECL_REF
2341   Abv = std::make_shared<BitCodeAbbrev>();
2342   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
2343   //Stmt
2344   // Expr
2345   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2346   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, ExprDependenceBits));
2347   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
2348   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
2349   //DeclRefExpr
2350   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HasQualifier
2351   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //GetDeclFound
2352   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ExplicitTemplateArgs
2353   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HadMultipleCandidates
2354   Abv->Add(BitCodeAbbrevOp(0)); // RefersToEnclosingVariableOrCapture
2355   Abv->Add(BitCodeAbbrevOp(0)); // NonOdrUseReason
2356   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
2357   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2358   DeclRefExprAbbrev = Stream.EmitAbbrev(std::move(Abv));
2359 
2360   // Abbreviation for EXPR_INTEGER_LITERAL
2361   Abv = std::make_shared<BitCodeAbbrev>();
2362   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
2363   //Stmt
2364   // Expr
2365   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2366   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, ExprDependenceBits));
2367   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
2368   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
2369   //Integer Literal
2370   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2371   Abv->Add(BitCodeAbbrevOp(32));                      // Bit Width
2372   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
2373   IntegerLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2374 
2375   // Abbreviation for EXPR_CHARACTER_LITERAL
2376   Abv = std::make_shared<BitCodeAbbrev>();
2377   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
2378   //Stmt
2379   // Expr
2380   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2381   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, ExprDependenceBits));
2382   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
2383   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
2384   //Character Literal
2385   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
2386   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
2387   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // getKind
2388   CharacterLiteralAbbrev = Stream.EmitAbbrev(std::move(Abv));
2389 
2390   // Abbreviation for EXPR_IMPLICIT_CAST
2391   Abv = std::make_shared<BitCodeAbbrev>();
2392   Abv->Add(BitCodeAbbrevOp(serialization::EXPR_IMPLICIT_CAST));
2393   // Stmt
2394   // Expr
2395   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
2396   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, ExprDependenceBits));
2397   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
2398   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
2399   // CastExpr
2400   Abv->Add(BitCodeAbbrevOp(0)); // PathSize
2401   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasFPFeatures
2402   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 6)); // CastKind
2403   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // PartOfExplicitCast
2404   // ImplicitCastExpr
2405   ExprImplicitCastAbbrev = Stream.EmitAbbrev(std::move(Abv));
2406 
2407   Abv = std::make_shared<BitCodeAbbrev>();
2408   Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
2409   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2410   DeclContextLexicalAbbrev = Stream.EmitAbbrev(std::move(Abv));
2411 
2412   Abv = std::make_shared<BitCodeAbbrev>();
2413   Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
2414   Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2415   DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(std::move(Abv));
2416 }
2417 
2418 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
2419 /// consumers of the AST.
2420 ///
2421 /// Such decls will always be deserialized from the AST file, so we would like
2422 /// this to be as restrictive as possible. Currently the predicate is driven by
2423 /// code generation requirements, if other clients have a different notion of
2424 /// what is "required" then we may have to consider an alternate scheme where
2425 /// clients can iterate over the top-level decls and get information on them,
2426 /// without necessary deserializing them. We could explicitly require such
2427 /// clients to use a separate API call to "realize" the decl. This should be
2428 /// relatively painless since they would presumably only do it for top-level
2429 /// decls.
2430 static bool isRequiredDecl(const Decl *D, ASTContext &Context,
2431                            bool WritingModule) {
2432   // An ObjCMethodDecl is never considered as "required" because its
2433   // implementation container always is.
2434 
2435   // File scoped assembly or obj-c or OMP declare target implementation must be
2436   // seen.
2437   if (isa<FileScopeAsmDecl, TopLevelStmtDecl, ObjCImplDecl>(D))
2438     return true;
2439 
2440   if (WritingModule && isPartOfPerModuleInitializer(D)) {
2441     // These declarations are part of the module initializer, and are emitted
2442     // if and when the module is imported, rather than being emitted eagerly.
2443     return false;
2444   }
2445 
2446   return Context.DeclMustBeEmitted(D);
2447 }
2448 
2449 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
2450   PrettyDeclStackTraceEntry CrashInfo(Context, D, SourceLocation(),
2451                                       "serializing");
2452 
2453   // Determine the ID for this declaration.
2454   serialization::DeclID ID;
2455   assert(!D->isFromASTFile() && "should not be emitting imported decl");
2456   serialization::DeclID &IDR = DeclIDs[D];
2457   if (IDR == 0)
2458     IDR = NextDeclID++;
2459 
2460   ID = IDR;
2461 
2462   assert(ID >= FirstDeclID && "invalid decl ID");
2463 
2464   RecordData Record;
2465   ASTDeclWriter W(*this, Context, Record);
2466 
2467   // Build a record for this declaration
2468   W.Visit(D);
2469 
2470   // Emit this declaration to the bitstream.
2471   uint64_t Offset = W.Emit(D);
2472 
2473   // Record the offset for this declaration
2474   SourceLocation Loc = D->getLocation();
2475   unsigned Index = ID - FirstDeclID;
2476   if (DeclOffsets.size() == Index)
2477     DeclOffsets.emplace_back(getAdjustedLocation(Loc), Offset,
2478                              DeclTypesBlockStartOffset);
2479   else if (DeclOffsets.size() < Index) {
2480     // FIXME: Can/should this happen?
2481     DeclOffsets.resize(Index+1);
2482     DeclOffsets[Index].setLocation(getAdjustedLocation(Loc));
2483     DeclOffsets[Index].setBitOffset(Offset, DeclTypesBlockStartOffset);
2484   } else {
2485     llvm_unreachable("declarations should be emitted in ID order");
2486   }
2487 
2488   SourceManager &SM = Context.getSourceManager();
2489   if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
2490     associateDeclWithFile(D, ID);
2491 
2492   // Note declarations that should be deserialized eagerly so that we can add
2493   // them to a record in the AST file later.
2494   if (isRequiredDecl(D, Context, WritingModule))
2495     EagerlyDeserializedDecls.push_back(ID);
2496 }
2497 
2498 void ASTRecordWriter::AddFunctionDefinition(const FunctionDecl *FD) {
2499   // Switch case IDs are per function body.
2500   Writer->ClearSwitchCaseIDs();
2501 
2502   assert(FD->doesThisDeclarationHaveABody());
2503   bool ModulesCodegen = false;
2504   if (!FD->isDependentContext()) {
2505     std::optional<GVALinkage> Linkage;
2506     if (Writer->WritingModule &&
2507         Writer->WritingModule->isInterfaceOrPartition()) {
2508       // When building a C++20 module interface unit or a partition unit, a
2509       // strong definition in the module interface is provided by the
2510       // compilation of that unit, not by its users. (Inline functions are still
2511       // emitted in module users.)
2512       Linkage = Writer->Context->GetGVALinkageForFunction(FD);
2513       ModulesCodegen = *Linkage == GVA_StrongExternal;
2514     }
2515     if (Writer->Context->getLangOpts().ModulesCodegen ||
2516         (FD->hasAttr<DLLExportAttr>() &&
2517          Writer->Context->getLangOpts().BuildingPCHWithObjectFile)) {
2518 
2519       // Under -fmodules-codegen, codegen is performed for all non-internal,
2520       // non-always_inline functions, unless they are available elsewhere.
2521       if (!FD->hasAttr<AlwaysInlineAttr>()) {
2522         if (!Linkage)
2523           Linkage = Writer->Context->GetGVALinkageForFunction(FD);
2524         ModulesCodegen =
2525             *Linkage != GVA_Internal && *Linkage != GVA_AvailableExternally;
2526       }
2527     }
2528   }
2529   Record->push_back(ModulesCodegen);
2530   if (ModulesCodegen)
2531     Writer->ModularCodegenDecls.push_back(Writer->GetDeclRef(FD));
2532   if (auto *CD = dyn_cast<CXXConstructorDecl>(FD)) {
2533     Record->push_back(CD->getNumCtorInitializers());
2534     if (CD->getNumCtorInitializers())
2535       AddCXXCtorInitializers(llvm::ArrayRef(CD->init_begin(), CD->init_end()));
2536   }
2537   AddStmt(FD->getBody());
2538 }
2539