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