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