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