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