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