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