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