1 //===--- ASTReaderDecl.cpp - Decl Deserialization ---------------*- C++ -*-===//
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 the ASTReader::ReadDeclRecord method, which is the
11 // entrypoint for loading a decl.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "ASTCommon.h"
16 #include "clang/Serialization/ASTReader.h"
17 #include "clang/Sema/SemaDiagnostic.h"
18 #include "clang/AST/ASTConsumer.h"
19 #include "clang/AST/ASTContext.h"
20 #include "clang/AST/DeclVisitor.h"
21 #include "clang/AST/DeclGroup.h"
22 #include "clang/AST/DeclCXX.h"
23 #include "clang/AST/DeclTemplate.h"
24 #include "clang/AST/Expr.h"
25 using namespace clang;
26 using namespace clang::serialization;
27 
28 //===----------------------------------------------------------------------===//
29 // Declaration deserialization
30 //===----------------------------------------------------------------------===//
31 
32 namespace clang {
33   class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
34     ASTReader &Reader;
35     Module &F;
36     llvm::BitstreamCursor &Cursor;
37     const DeclID ThisDeclID;
38     typedef ASTReader::RecordData RecordData;
39     const RecordData &Record;
40     unsigned &Idx;
41     TypeID TypeIDForTypeDecl;
42 
43     DeclID DeclContextIDForTemplateParmDecl;
44     DeclID LexicalDeclContextIDForTemplateParmDecl;
45 
46     uint64_t GetCurrentCursorOffset();
47 
48     SourceLocation ReadSourceLocation(const RecordData &R, unsigned &I) {
49       return Reader.ReadSourceLocation(F, R, I);
50     }
51 
52     SourceRange ReadSourceRange(const RecordData &R, unsigned &I) {
53       return Reader.ReadSourceRange(F, R, I);
54     }
55 
56     TypeSourceInfo *GetTypeSourceInfo(const RecordData &R, unsigned &I) {
57       return Reader.GetTypeSourceInfo(F, R, I);
58     }
59 
60     serialization::DeclID ReadDeclID(const RecordData &R, unsigned &I) {
61       return Reader.ReadDeclID(F, R, I);
62     }
63 
64     Decl *ReadDecl(const RecordData &R, unsigned &I) {
65       return Reader.ReadDecl(F, R, I);
66     }
67 
68     template<typename T>
69     T *ReadDeclAs(const RecordData &R, unsigned &I) {
70       return Reader.ReadDeclAs<T>(F, R, I);
71     }
72 
73     void ReadQualifierInfo(QualifierInfo &Info,
74                            const RecordData &R, unsigned &I) {
75       Reader.ReadQualifierInfo(F, Info, R, I);
76     }
77 
78     void ReadDeclarationNameLoc(DeclarationNameLoc &DNLoc, DeclarationName Name,
79                                 const RecordData &R, unsigned &I) {
80       Reader.ReadDeclarationNameLoc(F, DNLoc, Name, R, I);
81     }
82 
83     void ReadDeclarationNameInfo(DeclarationNameInfo &NameInfo,
84                                 const RecordData &R, unsigned &I) {
85       Reader.ReadDeclarationNameInfo(F, NameInfo, R, I);
86     }
87 
88     void ReadCXXDefinitionData(struct CXXRecordDecl::DefinitionData &Data,
89                                const RecordData &R, unsigned &I);
90 
91     void InitializeCXXDefinitionData(CXXRecordDecl *D,
92                                      CXXRecordDecl *DefinitionDecl,
93                                      const RecordData &Record, unsigned &Idx);
94   public:
95     ASTDeclReader(ASTReader &Reader, Module &F,
96                   llvm::BitstreamCursor &Cursor, DeclID thisDeclID,
97                   const RecordData &Record, unsigned &Idx)
98       : Reader(Reader), F(F), Cursor(Cursor), ThisDeclID(thisDeclID),
99         Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { }
100 
101     static void attachPreviousDecl(Decl *D, Decl *previous);
102 
103     void Visit(Decl *D);
104 
105     void UpdateDecl(Decl *D, Module &Module,
106                     const RecordData &Record);
107 
108     static void setNextObjCCategory(ObjCCategoryDecl *Cat,
109                                     ObjCCategoryDecl *Next) {
110       Cat->NextClassCategory = Next;
111     }
112 
113     void VisitDecl(Decl *D);
114     void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
115     void VisitNamedDecl(NamedDecl *ND);
116     void VisitLabelDecl(LabelDecl *LD);
117     void VisitNamespaceDecl(NamespaceDecl *D);
118     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
119     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
120     void VisitTypeDecl(TypeDecl *TD);
121     void VisitTypedefDecl(TypedefDecl *TD);
122     void VisitTypeAliasDecl(TypeAliasDecl *TD);
123     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
124     void VisitTagDecl(TagDecl *TD);
125     void VisitEnumDecl(EnumDecl *ED);
126     void VisitRecordDecl(RecordDecl *RD);
127     void VisitCXXRecordDecl(CXXRecordDecl *D);
128     void VisitClassTemplateSpecializationDecl(
129                                             ClassTemplateSpecializationDecl *D);
130     void VisitClassTemplatePartialSpecializationDecl(
131                                      ClassTemplatePartialSpecializationDecl *D);
132     void VisitClassScopeFunctionSpecializationDecl(
133                                        ClassScopeFunctionSpecializationDecl *D);
134     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
135     void VisitValueDecl(ValueDecl *VD);
136     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
137     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
138     void VisitDeclaratorDecl(DeclaratorDecl *DD);
139     void VisitFunctionDecl(FunctionDecl *FD);
140     void VisitCXXMethodDecl(CXXMethodDecl *D);
141     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
142     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
143     void VisitCXXConversionDecl(CXXConversionDecl *D);
144     void VisitFieldDecl(FieldDecl *FD);
145     void VisitIndirectFieldDecl(IndirectFieldDecl *FD);
146     void VisitVarDecl(VarDecl *VD);
147     void VisitImplicitParamDecl(ImplicitParamDecl *PD);
148     void VisitParmVarDecl(ParmVarDecl *PD);
149     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
150     void VisitTemplateDecl(TemplateDecl *D);
151     void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
152     void VisitClassTemplateDecl(ClassTemplateDecl *D);
153     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
154     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
155     void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
156     void VisitUsingDecl(UsingDecl *D);
157     void VisitUsingShadowDecl(UsingShadowDecl *D);
158     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
159     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
160     void VisitAccessSpecDecl(AccessSpecDecl *D);
161     void VisitFriendDecl(FriendDecl *D);
162     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
163     void VisitStaticAssertDecl(StaticAssertDecl *D);
164     void VisitBlockDecl(BlockDecl *BD);
165 
166     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
167     template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
168 
169     // FIXME: Reorder according to DeclNodes.td?
170     void VisitObjCMethodDecl(ObjCMethodDecl *D);
171     void VisitObjCContainerDecl(ObjCContainerDecl *D);
172     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
173     void VisitObjCIvarDecl(ObjCIvarDecl *D);
174     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
175     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
176     void VisitObjCClassDecl(ObjCClassDecl *D);
177     void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
178     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
179     void VisitObjCImplDecl(ObjCImplDecl *D);
180     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
181     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
182     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
183     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
184     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
185   };
186 }
187 
188 uint64_t ASTDeclReader::GetCurrentCursorOffset() {
189   return F.DeclsCursor.GetCurrentBitNo() + F.GlobalBitOffset;
190 }
191 
192 void ASTDeclReader::Visit(Decl *D) {
193   DeclVisitor<ASTDeclReader, void>::Visit(D);
194 
195   if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)) {
196     if (DD->DeclInfo) {
197       DeclaratorDecl::ExtInfo *Info =
198           DD->DeclInfo.get<DeclaratorDecl::ExtInfo *>();
199       Info->TInfo =
200           GetTypeSourceInfo(Record, Idx);
201     }
202     else {
203       DD->DeclInfo = GetTypeSourceInfo(Record, Idx);
204     }
205   }
206 
207   if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
208     // if we have a fully initialized TypeDecl, we can safely read its type now.
209     TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtrOrNull());
210   } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
211     // FunctionDecl's body was written last after all other Stmts/Exprs.
212     if (Record[Idx++])
213       FD->setLazyBody(GetCurrentCursorOffset());
214   } else if (D->isTemplateParameter()) {
215     // If we have a fully initialized template parameter, we can now
216     // set its DeclContext.
217     D->setDeclContext(
218           cast_or_null<DeclContext>(
219                             Reader.GetDecl(DeclContextIDForTemplateParmDecl)));
220     D->setLexicalDeclContext(
221           cast_or_null<DeclContext>(
222                       Reader.GetDecl(LexicalDeclContextIDForTemplateParmDecl)));
223   }
224 }
225 
226 void ASTDeclReader::VisitDecl(Decl *D) {
227   if (D->isTemplateParameter()) {
228     // We don't want to deserialize the DeclContext of a template
229     // parameter immediately, because the template parameter might be
230     // used in the formulation of its DeclContext. Use the translation
231     // unit DeclContext as a placeholder.
232     DeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx);
233     LexicalDeclContextIDForTemplateParmDecl = ReadDeclID(Record, Idx);
234     D->setDeclContext(Reader.getContext().getTranslationUnitDecl());
235   } else {
236     D->setDeclContext(ReadDeclAs<DeclContext>(Record, Idx));
237     D->setLexicalDeclContext(ReadDeclAs<DeclContext>(Record, Idx));
238   }
239   D->setLocation(ReadSourceLocation(Record, Idx));
240   D->setInvalidDecl(Record[Idx++]);
241   if (Record[Idx++]) { // hasAttrs
242     AttrVec Attrs;
243     Reader.ReadAttributes(F, Attrs, Record, Idx);
244     D->setAttrs(Attrs);
245   }
246   D->setImplicit(Record[Idx++]);
247   D->setUsed(Record[Idx++]);
248   D->setReferenced(Record[Idx++]);
249   D->setAccess((AccessSpecifier)Record[Idx++]);
250   D->FromASTFile = true;
251   D->ModulePrivate = Record[Idx++];
252 }
253 
254 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
255   llvm_unreachable("Translation units are not serialized");
256 }
257 
258 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
259   VisitDecl(ND);
260   ND->setDeclName(Reader.ReadDeclarationName(F, Record, Idx));
261 }
262 
263 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
264   VisitNamedDecl(TD);
265   TD->setLocStart(ReadSourceLocation(Record, Idx));
266   // Delay type reading until after we have fully initialized the decl.
267   TypeIDForTypeDecl = Reader.getGlobalTypeID(F, Record[Idx++]);
268 }
269 
270 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
271   VisitTypeDecl(TD);
272   TD->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
273 }
274 
275 void ASTDeclReader::VisitTypeAliasDecl(TypeAliasDecl *TD) {
276   VisitTypeDecl(TD);
277   TD->setTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
278 }
279 
280 void ASTDeclReader::VisitTagDecl(TagDecl *TD) {
281   VisitTypeDecl(TD);
282   VisitRedeclarable(TD);
283   TD->IdentifierNamespace = Record[Idx++];
284   TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
285   TD->setDefinition(Record[Idx++]);
286   TD->setEmbeddedInDeclarator(Record[Idx++]);
287   TD->setFreeStanding(Record[Idx++]);
288   TD->setRBraceLoc(ReadSourceLocation(Record, Idx));
289   if (Record[Idx++]) { // hasExtInfo
290     TagDecl::ExtInfo *Info = new (Reader.getContext()) TagDecl::ExtInfo();
291     ReadQualifierInfo(*Info, Record, Idx);
292     TD->TypedefNameDeclOrQualifier = Info;
293   } else
294     TD->setTypedefNameForAnonDecl(ReadDeclAs<TypedefNameDecl>(Record, Idx));
295 }
296 
297 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
298   VisitTagDecl(ED);
299   if (TypeSourceInfo *TI = Reader.GetTypeSourceInfo(F, Record, Idx))
300     ED->setIntegerTypeSourceInfo(TI);
301   else
302     ED->setIntegerType(Reader.readType(F, Record, Idx));
303   ED->setPromotionType(Reader.readType(F, Record, Idx));
304   ED->setNumPositiveBits(Record[Idx++]);
305   ED->setNumNegativeBits(Record[Idx++]);
306   ED->IsScoped = Record[Idx++];
307   ED->IsScopedUsingClassTag = Record[Idx++];
308   ED->IsFixed = Record[Idx++];
309   ED->setInstantiationOfMemberEnum(ReadDeclAs<EnumDecl>(Record, Idx));
310 }
311 
312 void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
313   VisitTagDecl(RD);
314   RD->setHasFlexibleArrayMember(Record[Idx++]);
315   RD->setAnonymousStructOrUnion(Record[Idx++]);
316   RD->setHasObjectMember(Record[Idx++]);
317 }
318 
319 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
320   VisitNamedDecl(VD);
321   VD->setType(Reader.readType(F, Record, Idx));
322 }
323 
324 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
325   VisitValueDecl(ECD);
326   if (Record[Idx++])
327     ECD->setInitExpr(Reader.ReadExpr(F));
328   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
329 }
330 
331 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
332   VisitValueDecl(DD);
333   DD->setInnerLocStart(ReadSourceLocation(Record, Idx));
334   if (Record[Idx++]) { // hasExtInfo
335     DeclaratorDecl::ExtInfo *Info
336         = new (Reader.getContext()) DeclaratorDecl::ExtInfo();
337     ReadQualifierInfo(*Info, Record, Idx);
338     DD->DeclInfo = Info;
339   }
340 }
341 
342 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
343   VisitDeclaratorDecl(FD);
344   VisitRedeclarable(FD);
345 
346   ReadDeclarationNameLoc(FD->DNLoc, FD->getDeclName(), Record, Idx);
347   FD->IdentifierNamespace = Record[Idx++];
348   switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
349   default: llvm_unreachable("Unhandled TemplatedKind!");
350   case FunctionDecl::TK_NonTemplate:
351     break;
352   case FunctionDecl::TK_FunctionTemplate:
353     FD->setDescribedFunctionTemplate(ReadDeclAs<FunctionTemplateDecl>(Record,
354                                                                       Idx));
355     break;
356   case FunctionDecl::TK_MemberSpecialization: {
357     FunctionDecl *InstFD = ReadDeclAs<FunctionDecl>(Record, Idx);
358     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
359     SourceLocation POI = ReadSourceLocation(Record, Idx);
360     FD->setInstantiationOfMemberFunction(Reader.getContext(), InstFD, TSK);
361     FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
362     break;
363   }
364   case FunctionDecl::TK_FunctionTemplateSpecialization: {
365     FunctionTemplateDecl *Template = ReadDeclAs<FunctionTemplateDecl>(Record,
366                                                                       Idx);
367     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
368 
369     // Template arguments.
370     SmallVector<TemplateArgument, 8> TemplArgs;
371     Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
372 
373     // Template args as written.
374     SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
375     SourceLocation LAngleLoc, RAngleLoc;
376     bool HasTemplateArgumentsAsWritten = Record[Idx++];
377     if (HasTemplateArgumentsAsWritten) {
378       unsigned NumTemplateArgLocs = Record[Idx++];
379       TemplArgLocs.reserve(NumTemplateArgLocs);
380       for (unsigned i=0; i != NumTemplateArgLocs; ++i)
381         TemplArgLocs.push_back(
382             Reader.ReadTemplateArgumentLoc(F, Record, Idx));
383 
384       LAngleLoc = ReadSourceLocation(Record, Idx);
385       RAngleLoc = ReadSourceLocation(Record, Idx);
386     }
387 
388     SourceLocation POI = ReadSourceLocation(Record, Idx);
389 
390     ASTContext &C = Reader.getContext();
391     TemplateArgumentList *TemplArgList
392       = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), TemplArgs.size());
393     TemplateArgumentListInfo TemplArgsInfo(LAngleLoc, RAngleLoc);
394     for (unsigned i=0, e = TemplArgLocs.size(); i != e; ++i)
395       TemplArgsInfo.addArgument(TemplArgLocs[i]);
396     FunctionTemplateSpecializationInfo *FTInfo
397         = FunctionTemplateSpecializationInfo::Create(C, FD, Template, TSK,
398                                                      TemplArgList,
399                              HasTemplateArgumentsAsWritten ? &TemplArgsInfo : 0,
400                                                      POI);
401     FD->TemplateOrSpecialization = FTInfo;
402 
403     if (FD->isCanonicalDecl()) { // if canonical add to template's set.
404       // The template that contains the specializations set. It's not safe to
405       // use getCanonicalDecl on Template since it may still be initializing.
406       FunctionTemplateDecl *CanonTemplate
407         = ReadDeclAs<FunctionTemplateDecl>(Record, Idx);
408       // Get the InsertPos by FindNodeOrInsertPos() instead of calling
409       // InsertNode(FTInfo) directly to avoid the getASTContext() call in
410       // FunctionTemplateSpecializationInfo's Profile().
411       // We avoid getASTContext because a decl in the parent hierarchy may
412       // be initializing.
413       llvm::FoldingSetNodeID ID;
414       FunctionTemplateSpecializationInfo::Profile(ID, TemplArgs.data(),
415                                                   TemplArgs.size(), C);
416       void *InsertPos = 0;
417       CanonTemplate->getSpecializations().FindNodeOrInsertPos(ID, InsertPos);
418       assert(InsertPos && "Another specialization already inserted!");
419       CanonTemplate->getSpecializations().InsertNode(FTInfo, InsertPos);
420     }
421     break;
422   }
423   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
424     // Templates.
425     UnresolvedSet<8> TemplDecls;
426     unsigned NumTemplates = Record[Idx++];
427     while (NumTemplates--)
428       TemplDecls.addDecl(ReadDeclAs<NamedDecl>(Record, Idx));
429 
430     // Templates args.
431     TemplateArgumentListInfo TemplArgs;
432     unsigned NumArgs = Record[Idx++];
433     while (NumArgs--)
434       TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(F, Record, Idx));
435     TemplArgs.setLAngleLoc(ReadSourceLocation(Record, Idx));
436     TemplArgs.setRAngleLoc(ReadSourceLocation(Record, Idx));
437 
438     FD->setDependentTemplateSpecialization(Reader.getContext(),
439                                            TemplDecls, TemplArgs);
440     break;
441   }
442   }
443 
444   // FunctionDecl's body is handled last at ASTDeclReader::Visit,
445   // after everything else is read.
446 
447   FD->SClass = (StorageClass)Record[Idx++];
448   FD->SClassAsWritten = (StorageClass)Record[Idx++];
449   FD->IsInline = Record[Idx++];
450   FD->IsInlineSpecified = Record[Idx++];
451   FD->IsVirtualAsWritten = Record[Idx++];
452   FD->IsPure = Record[Idx++];
453   FD->HasInheritedPrototype = Record[Idx++];
454   FD->HasWrittenPrototype = Record[Idx++];
455   FD->IsDeleted = Record[Idx++];
456   FD->IsTrivial = Record[Idx++];
457   FD->IsDefaulted = Record[Idx++];
458   FD->IsExplicitlyDefaulted = Record[Idx++];
459   FD->HasImplicitReturnZero = Record[Idx++];
460   FD->IsConstexpr = Record[Idx++];
461   FD->EndRangeLoc = ReadSourceLocation(Record, Idx);
462 
463   // Read in the parameters.
464   unsigned NumParams = Record[Idx++];
465   SmallVector<ParmVarDecl *, 16> Params;
466   Params.reserve(NumParams);
467   for (unsigned I = 0; I != NumParams; ++I)
468     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
469   FD->setParams(Reader.getContext(), Params);
470 }
471 
472 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
473   VisitNamedDecl(MD);
474   if (Record[Idx++]) {
475     // In practice, this won't be executed (since method definitions
476     // don't occur in header files).
477     MD->setBody(Reader.ReadStmt(F));
478     MD->setSelfDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
479     MD->setCmdDecl(ReadDeclAs<ImplicitParamDecl>(Record, Idx));
480   }
481   MD->setInstanceMethod(Record[Idx++]);
482   MD->setVariadic(Record[Idx++]);
483   MD->setSynthesized(Record[Idx++]);
484   MD->setDefined(Record[Idx++]);
485   MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
486   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
487   MD->SetRelatedResultType(Record[Idx++]);
488   MD->setResultType(Reader.readType(F, Record, Idx));
489   MD->setResultTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
490   MD->setEndLoc(ReadSourceLocation(Record, Idx));
491   unsigned NumParams = Record[Idx++];
492   SmallVector<ParmVarDecl *, 16> Params;
493   Params.reserve(NumParams);
494   for (unsigned I = 0; I != NumParams; ++I)
495     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
496 
497   MD->SelLocsKind = Record[Idx++];
498   unsigned NumStoredSelLocs = Record[Idx++];
499   SmallVector<SourceLocation, 16> SelLocs;
500   SelLocs.reserve(NumStoredSelLocs);
501   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
502     SelLocs.push_back(ReadSourceLocation(Record, Idx));
503 
504   MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
505 }
506 
507 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
508   VisitNamedDecl(CD);
509   SourceLocation A = ReadSourceLocation(Record, Idx);
510   SourceLocation B = ReadSourceLocation(Record, Idx);
511   CD->setAtEndRange(SourceRange(A, B));
512 }
513 
514 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
515   VisitObjCContainerDecl(ID);
516   ID->setTypeForDecl(Reader.readType(F, Record, Idx).getTypePtrOrNull());
517   ID->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
518 
519   // Read the directly referenced protocols and their SourceLocations.
520   unsigned NumProtocols = Record[Idx++];
521   SmallVector<ObjCProtocolDecl *, 16> Protocols;
522   Protocols.reserve(NumProtocols);
523   for (unsigned I = 0; I != NumProtocols; ++I)
524     Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
525   SmallVector<SourceLocation, 16> ProtoLocs;
526   ProtoLocs.reserve(NumProtocols);
527   for (unsigned I = 0; I != NumProtocols; ++I)
528     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
529   ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
530                       Reader.getContext());
531 
532   // Read the transitive closure of protocols referenced by this class.
533   NumProtocols = Record[Idx++];
534   Protocols.clear();
535   Protocols.reserve(NumProtocols);
536   for (unsigned I = 0; I != NumProtocols; ++I)
537     Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
538   ID->AllReferencedProtocols.set(Protocols.data(), NumProtocols,
539                                  Reader.getContext());
540 
541   // Read the ivars.
542   unsigned NumIvars = Record[Idx++];
543   SmallVector<ObjCIvarDecl *, 16> IVars;
544   IVars.reserve(NumIvars);
545   for (unsigned I = 0; I != NumIvars; ++I)
546     IVars.push_back(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
547   ID->setCategoryList(ReadDeclAs<ObjCCategoryDecl>(Record, Idx));
548 
549   // We will rebuild this list lazily.
550   ID->setIvarList(0);
551   ID->setForwardDecl(Record[Idx++]);
552   ID->setImplicitInterfaceDecl(Record[Idx++]);
553   ID->setClassLoc(ReadSourceLocation(Record, Idx));
554   ID->setSuperClassLoc(ReadSourceLocation(Record, Idx));
555   ID->setLocEnd(ReadSourceLocation(Record, Idx));
556 }
557 
558 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
559   VisitFieldDecl(IVD);
560   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
561   // This field will be built lazily.
562   IVD->setNextIvar(0);
563   bool synth = Record[Idx++];
564   IVD->setSynthesize(synth);
565 }
566 
567 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
568   VisitObjCContainerDecl(PD);
569   PD->setForwardDecl(Record[Idx++]);
570   PD->setLocEnd(ReadSourceLocation(Record, Idx));
571   unsigned NumProtoRefs = Record[Idx++];
572   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
573   ProtoRefs.reserve(NumProtoRefs);
574   for (unsigned I = 0; I != NumProtoRefs; ++I)
575     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
576   SmallVector<SourceLocation, 16> ProtoLocs;
577   ProtoLocs.reserve(NumProtoRefs);
578   for (unsigned I = 0; I != NumProtoRefs; ++I)
579     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
580   PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
581                       Reader.getContext());
582 }
583 
584 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
585   VisitFieldDecl(FD);
586 }
587 
588 void ASTDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
589   VisitDecl(CD);
590   ObjCInterfaceDecl *ClassRef = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
591   SourceLocation SLoc = ReadSourceLocation(Record, Idx);
592   CD->setClass(Reader.getContext(), ClassRef, SLoc);
593 }
594 
595 void ASTDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
596   VisitDecl(FPD);
597   unsigned NumProtoRefs = Record[Idx++];
598   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
599   ProtoRefs.reserve(NumProtoRefs);
600   for (unsigned I = 0; I != NumProtoRefs; ++I)
601     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
602   SmallVector<SourceLocation, 16> ProtoLocs;
603   ProtoLocs.reserve(NumProtoRefs);
604   for (unsigned I = 0; I != NumProtoRefs; ++I)
605     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
606   FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
607                        Reader.getContext());
608 }
609 
610 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
611   VisitObjCContainerDecl(CD);
612   CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
613   unsigned NumProtoRefs = Record[Idx++];
614   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
615   ProtoRefs.reserve(NumProtoRefs);
616   for (unsigned I = 0; I != NumProtoRefs; ++I)
617     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
618   SmallVector<SourceLocation, 16> ProtoLocs;
619   ProtoLocs.reserve(NumProtoRefs);
620   for (unsigned I = 0; I != NumProtoRefs; ++I)
621     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
622   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
623                       Reader.getContext());
624   CD->NextClassCategory = ReadDeclAs<ObjCCategoryDecl>(Record, Idx);
625   CD->setHasSynthBitfield(Record[Idx++]);
626   CD->setAtLoc(ReadSourceLocation(Record, Idx));
627   CD->setCategoryNameLoc(ReadSourceLocation(Record, Idx));
628 }
629 
630 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
631   VisitNamedDecl(CAD);
632   CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
633 }
634 
635 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
636   VisitNamedDecl(D);
637   D->setAtLoc(ReadSourceLocation(Record, Idx));
638   D->setType(GetTypeSourceInfo(Record, Idx));
639   // FIXME: stable encoding
640   D->setPropertyAttributes(
641                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
642   D->setPropertyAttributesAsWritten(
643                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
644   // FIXME: stable encoding
645   D->setPropertyImplementation(
646                             (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
647   D->setGetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
648   D->setSetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
649   D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
650   D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
651   D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
652 }
653 
654 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
655   VisitObjCContainerDecl(D);
656   D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
657 }
658 
659 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
660   VisitObjCImplDecl(D);
661   D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx));
662 }
663 
664 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
665   VisitObjCImplDecl(D);
666   D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
667   llvm::tie(D->IvarInitializers, D->NumIvarInitializers)
668       = Reader.ReadCXXCtorInitializers(F, Record, Idx);
669   D->setHasSynthBitfield(Record[Idx++]);
670 }
671 
672 
673 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
674   VisitDecl(D);
675   D->setAtLoc(ReadSourceLocation(Record, Idx));
676   D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>(Record, Idx));
677   D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(Record, Idx);
678   D->IvarLoc = ReadSourceLocation(Record, Idx);
679   D->setGetterCXXConstructor(Reader.ReadExpr(F));
680   D->setSetterCXXAssignment(Reader.ReadExpr(F));
681 }
682 
683 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
684   VisitDeclaratorDecl(FD);
685   FD->setMutable(Record[Idx++]);
686   int BitWidthOrInitializer = Record[Idx++];
687   if (BitWidthOrInitializer == 1)
688     FD->setBitWidth(Reader.ReadExpr(F));
689   else if (BitWidthOrInitializer == 2)
690     FD->setInClassInitializer(Reader.ReadExpr(F));
691   if (!FD->getDeclName()) {
692     if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>(Record, Idx))
693       Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
694   }
695 }
696 
697 void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
698   VisitValueDecl(FD);
699 
700   FD->ChainingSize = Record[Idx++];
701   assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
702   FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
703 
704   for (unsigned I = 0; I != FD->ChainingSize; ++I)
705     FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx);
706 }
707 
708 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
709   VisitDeclaratorDecl(VD);
710   VisitRedeclarable(VD);
711   VD->VarDeclBits.SClass = (StorageClass)Record[Idx++];
712   VD->VarDeclBits.SClassAsWritten = (StorageClass)Record[Idx++];
713   VD->VarDeclBits.ThreadSpecified = Record[Idx++];
714   VD->VarDeclBits.HasCXXDirectInit = Record[Idx++];
715   VD->VarDeclBits.ExceptionVar = Record[Idx++];
716   VD->VarDeclBits.NRVOVariable = Record[Idx++];
717   VD->VarDeclBits.CXXForRangeDecl = Record[Idx++];
718   VD->VarDeclBits.ARCPseudoStrong = Record[Idx++];
719   if (Record[Idx++])
720     VD->setInit(Reader.ReadExpr(F));
721 
722   if (Record[Idx++]) { // HasMemberSpecializationInfo.
723     VarDecl *Tmpl = ReadDeclAs<VarDecl>(Record, Idx);
724     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
725     SourceLocation POI = ReadSourceLocation(Record, Idx);
726     Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
727   }
728 }
729 
730 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
731   VisitVarDecl(PD);
732 }
733 
734 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
735   VisitVarDecl(PD);
736   unsigned isObjCMethodParam = Record[Idx++];
737   unsigned scopeDepth = Record[Idx++];
738   unsigned scopeIndex = Record[Idx++];
739   unsigned declQualifier = Record[Idx++];
740   if (isObjCMethodParam) {
741     assert(scopeDepth == 0);
742     PD->setObjCMethodScopeInfo(scopeIndex);
743     PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
744   } else {
745     PD->setScopeInfo(scopeDepth, scopeIndex);
746   }
747   PD->ParmVarDeclBits.IsKNRPromoted = Record[Idx++];
748   PD->ParmVarDeclBits.HasInheritedDefaultArg = Record[Idx++];
749   if (Record[Idx++]) // hasUninstantiatedDefaultArg.
750     PD->setUninstantiatedDefaultArg(Reader.ReadExpr(F));
751 }
752 
753 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
754   VisitDecl(AD);
755   AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(F)));
756   AD->setRParenLoc(ReadSourceLocation(Record, Idx));
757 }
758 
759 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
760   VisitDecl(BD);
761   BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(F)));
762   BD->setSignatureAsWritten(GetTypeSourceInfo(Record, Idx));
763   unsigned NumParams = Record[Idx++];
764   SmallVector<ParmVarDecl *, 16> Params;
765   Params.reserve(NumParams);
766   for (unsigned I = 0; I != NumParams; ++I)
767     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
768   BD->setParams(Params);
769 
770   bool capturesCXXThis = Record[Idx++];
771   unsigned numCaptures = Record[Idx++];
772   SmallVector<BlockDecl::Capture, 16> captures;
773   captures.reserve(numCaptures);
774   for (unsigned i = 0; i != numCaptures; ++i) {
775     VarDecl *decl = ReadDeclAs<VarDecl>(Record, Idx);
776     unsigned flags = Record[Idx++];
777     bool byRef = (flags & 1);
778     bool nested = (flags & 2);
779     Expr *copyExpr = ((flags & 4) ? Reader.ReadExpr(F) : 0);
780 
781     captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
782   }
783   BD->setCaptures(Reader.getContext(), captures.begin(),
784                   captures.end(), capturesCXXThis);
785 }
786 
787 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
788   VisitDecl(D);
789   D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
790   D->setExternLoc(ReadSourceLocation(Record, Idx));
791   D->setRBraceLoc(ReadSourceLocation(Record, Idx));
792 }
793 
794 void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
795   VisitNamedDecl(D);
796   D->setLocStart(ReadSourceLocation(Record, Idx));
797 }
798 
799 
800 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
801   VisitNamedDecl(D);
802   D->IsInline = Record[Idx++];
803   D->LocStart = ReadSourceLocation(Record, Idx);
804   D->RBraceLoc = ReadSourceLocation(Record, Idx);
805   D->NextNamespace = Record[Idx++];
806 
807   bool IsOriginal = Record[Idx++];
808   // FIXME: Modules will likely have trouble with pointing directly at
809   // the original namespace.
810   D->OrigOrAnonNamespace.setInt(IsOriginal);
811   D->OrigOrAnonNamespace.setPointer(ReadDeclAs<NamespaceDecl>(Record, Idx));
812 }
813 
814 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
815   VisitNamedDecl(D);
816   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
817   D->IdentLoc = ReadSourceLocation(Record, Idx);
818   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
819   D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx);
820 }
821 
822 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
823   VisitNamedDecl(D);
824   D->setUsingLocation(ReadSourceLocation(Record, Idx));
825   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
826   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
827   D->FirstUsingShadow = ReadDeclAs<UsingShadowDecl>(Record, Idx);
828   D->setTypeName(Record[Idx++]);
829   if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx))
830     Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
831 }
832 
833 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
834   VisitNamedDecl(D);
835   D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx));
836   D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx);
837   UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx);
838   if (Pattern)
839     Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
840 }
841 
842 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
843   VisitNamedDecl(D);
844   D->UsingLoc = ReadSourceLocation(Record, Idx);
845   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
846   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
847   D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx);
848   D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx);
849 }
850 
851 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
852   VisitValueDecl(D);
853   D->setUsingLoc(ReadSourceLocation(Record, Idx));
854   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
855   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
856 }
857 
858 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
859                                                UnresolvedUsingTypenameDecl *D) {
860   VisitTypeDecl(D);
861   D->TypenameLocation = ReadSourceLocation(Record, Idx);
862   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
863 }
864 
865 void ASTDeclReader::ReadCXXDefinitionData(
866                                    struct CXXRecordDecl::DefinitionData &Data,
867                                    const RecordData &Record, unsigned &Idx) {
868   Data.UserDeclaredConstructor = Record[Idx++];
869   Data.UserDeclaredCopyConstructor = Record[Idx++];
870   Data.UserDeclaredMoveConstructor = Record[Idx++];
871   Data.UserDeclaredCopyAssignment = Record[Idx++];
872   Data.UserDeclaredMoveAssignment = Record[Idx++];
873   Data.UserDeclaredDestructor = Record[Idx++];
874   Data.Aggregate = Record[Idx++];
875   Data.PlainOldData = Record[Idx++];
876   Data.Empty = Record[Idx++];
877   Data.Polymorphic = Record[Idx++];
878   Data.Abstract = Record[Idx++];
879   Data.IsStandardLayout = Record[Idx++];
880   Data.HasNoNonEmptyBases = Record[Idx++];
881   Data.HasPrivateFields = Record[Idx++];
882   Data.HasProtectedFields = Record[Idx++];
883   Data.HasPublicFields = Record[Idx++];
884   Data.HasMutableFields = Record[Idx++];
885   Data.HasTrivialDefaultConstructor = Record[Idx++];
886   Data.HasConstexprNonCopyMoveConstructor = Record[Idx++];
887   Data.HasTrivialCopyConstructor = Record[Idx++];
888   Data.HasTrivialMoveConstructor = Record[Idx++];
889   Data.HasTrivialCopyAssignment = Record[Idx++];
890   Data.HasTrivialMoveAssignment = Record[Idx++];
891   Data.HasTrivialDestructor = Record[Idx++];
892   Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++];
893   Data.ComputedVisibleConversions = Record[Idx++];
894   Data.UserProvidedDefaultConstructor = Record[Idx++];
895   Data.DeclaredDefaultConstructor = Record[Idx++];
896   Data.DeclaredCopyConstructor = Record[Idx++];
897   Data.DeclaredMoveConstructor = Record[Idx++];
898   Data.DeclaredCopyAssignment = Record[Idx++];
899   Data.DeclaredMoveAssignment = Record[Idx++];
900   Data.DeclaredDestructor = Record[Idx++];
901   Data.FailedImplicitMoveConstructor = Record[Idx++];
902   Data.FailedImplicitMoveAssignment = Record[Idx++];
903 
904   Data.NumBases = Record[Idx++];
905   if (Data.NumBases)
906     Data.Bases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
907   Data.NumVBases = Record[Idx++];
908   if (Data.NumVBases)
909     Data.VBases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
910 
911   Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx);
912   Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx);
913   assert(Data.Definition && "Data.Definition should be already set!");
914   Data.FirstFriend = ReadDeclAs<FriendDecl>(Record, Idx);
915 }
916 
917 void ASTDeclReader::InitializeCXXDefinitionData(CXXRecordDecl *D,
918                                                 CXXRecordDecl *DefinitionDecl,
919                                                 const RecordData &Record,
920                                                 unsigned &Idx) {
921   ASTContext &C = Reader.getContext();
922 
923   if (D == DefinitionDecl) {
924     D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
925     ReadCXXDefinitionData(*D->DefinitionData, Record, Idx);
926     // We read the definition info. Check if there are pending forward
927     // references that need to point to this DefinitionData pointer.
928     ASTReader::PendingForwardRefsMap::iterator
929         FindI = Reader.PendingForwardRefs.find(D);
930     if (FindI != Reader.PendingForwardRefs.end()) {
931       ASTReader::ForwardRefs &Refs = FindI->second;
932       for (ASTReader::ForwardRefs::iterator
933              I = Refs.begin(), E = Refs.end(); I != E; ++I)
934         (*I)->DefinitionData = D->DefinitionData;
935 #ifndef NDEBUG
936       // We later check whether PendingForwardRefs is empty to make sure all
937       // pending references were linked.
938       Reader.PendingForwardRefs.erase(D);
939 #endif
940     }
941   } else if (DefinitionDecl) {
942     if (DefinitionDecl->DefinitionData) {
943       D->DefinitionData = DefinitionDecl->DefinitionData;
944     } else {
945       // The definition is still initializing.
946       Reader.PendingForwardRefs[DefinitionDecl].push_back(D);
947     }
948   }
949 }
950 
951 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
952   VisitRecordDecl(D);
953 
954   CXXRecordDecl *DefinitionDecl = ReadDeclAs<CXXRecordDecl>(Record, Idx);
955   InitializeCXXDefinitionData(D, DefinitionDecl, Record, Idx);
956 
957   ASTContext &C = Reader.getContext();
958 
959   enum CXXRecKind {
960     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
961   };
962   switch ((CXXRecKind)Record[Idx++]) {
963   default:
964     llvm_unreachable("Out of sync with ASTDeclWriter::VisitCXXRecordDecl?");
965   case CXXRecNotTemplate:
966     break;
967   case CXXRecTemplate:
968     D->TemplateOrInstantiation = ReadDeclAs<ClassTemplateDecl>(Record, Idx);
969     break;
970   case CXXRecMemberSpecialization: {
971     CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx);
972     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
973     SourceLocation POI = ReadSourceLocation(Record, Idx);
974     MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
975     MSI->setPointOfInstantiation(POI);
976     D->TemplateOrInstantiation = MSI;
977     break;
978   }
979   }
980 
981   // Load the key function to avoid deserializing every method so we can
982   // compute it.
983   if (D->IsDefinition) {
984     if (CXXMethodDecl *Key = ReadDeclAs<CXXMethodDecl>(Record, Idx))
985       C.KeyFunctions[D] = Key;
986   }
987 }
988 
989 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
990   VisitFunctionDecl(D);
991   unsigned NumOverridenMethods = Record[Idx++];
992   while (NumOverridenMethods--) {
993     // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
994     // MD may be initializing.
995     if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx))
996       Reader.getContext().addOverriddenMethod(D, MD);
997   }
998 }
999 
1000 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1001   VisitCXXMethodDecl(D);
1002 
1003   D->IsExplicitSpecified = Record[Idx++];
1004   D->ImplicitlyDefined = Record[Idx++];
1005   llvm::tie(D->CtorInitializers, D->NumCtorInitializers)
1006       = Reader.ReadCXXCtorInitializers(F, Record, Idx);
1007 }
1008 
1009 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1010   VisitCXXMethodDecl(D);
1011 
1012   D->ImplicitlyDefined = Record[Idx++];
1013   D->OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx);
1014 }
1015 
1016 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
1017   VisitCXXMethodDecl(D);
1018   D->IsExplicitSpecified = Record[Idx++];
1019 }
1020 
1021 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
1022   VisitDecl(D);
1023   D->setColonLoc(ReadSourceLocation(Record, Idx));
1024 }
1025 
1026 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
1027   VisitDecl(D);
1028   if (Record[Idx++])
1029     D->Friend = GetTypeSourceInfo(Record, Idx);
1030   else
1031     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1032   D->NextFriend = Record[Idx++];
1033   D->UnsupportedFriend = (Record[Idx++] != 0);
1034   D->FriendLoc = ReadSourceLocation(Record, Idx);
1035 }
1036 
1037 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1038   VisitDecl(D);
1039   unsigned NumParams = Record[Idx++];
1040   D->NumParams = NumParams;
1041   D->Params = new TemplateParameterList*[NumParams];
1042   for (unsigned i = 0; i != NumParams; ++i)
1043     D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx);
1044   if (Record[Idx++]) // HasFriendDecl
1045     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1046   else
1047     D->Friend = GetTypeSourceInfo(Record, Idx);
1048   D->FriendLoc = ReadSourceLocation(Record, Idx);
1049 }
1050 
1051 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
1052   VisitNamedDecl(D);
1053 
1054   NamedDecl *TemplatedDecl = ReadDeclAs<NamedDecl>(Record, Idx);
1055   TemplateParameterList* TemplateParams
1056       = Reader.ReadTemplateParameterList(F, Record, Idx);
1057   D->init(TemplatedDecl, TemplateParams);
1058 }
1059 
1060 void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1061   // Initialize CommonOrPrev before VisitTemplateDecl so that getCommonPtr()
1062   // can be used while this is still initializing.
1063 
1064   assert(D->CommonOrPrev.isNull() && "getCommonPtr was called earlier on this");
1065   DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1066   DeclID FirstDeclID =  PreviousDeclID ? ReadDeclID(Record, Idx) : 0;
1067   // We delay loading of the redeclaration chain to avoid deeply nested calls.
1068   // We temporarily set the first (canonical) declaration as the previous one
1069   // which is the one that matters and mark the real previous DeclID to be
1070   // loaded & attached later on.
1071   RedeclarableTemplateDecl *FirstDecl =
1072       cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(FirstDeclID));
1073   assert((FirstDecl == 0 || FirstDecl->getKind() == D->getKind()) &&
1074          "FirstDecl kind mismatch");
1075   if (FirstDecl) {
1076     D->CommonOrPrev = FirstDecl;
1077     // Mark the real previous DeclID to be loaded & attached later on.
1078     if (PreviousDeclID != FirstDeclID)
1079       Reader.PendingPreviousDecls.push_back(std::make_pair(D, PreviousDeclID));
1080   } else {
1081     D->CommonOrPrev = D->newCommon(Reader.getContext());
1082     if (RedeclarableTemplateDecl *RTD
1083           = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) {
1084       assert(RTD->getKind() == D->getKind() &&
1085              "InstantiatedFromMemberTemplate kind mismatch");
1086       D->setInstantiatedFromMemberTemplateImpl(RTD);
1087       if (Record[Idx++])
1088         D->setMemberSpecialization();
1089     }
1090 
1091     RedeclarableTemplateDecl *LatestDecl
1092       = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx);
1093 
1094     // This decl is a first one and the latest declaration that it points to is
1095     // in the same AST file. However, if this actually needs to point to a
1096     // redeclaration in another AST file, we need to update it by checking
1097     // the FirstLatestDeclIDs map which tracks this kind of decls.
1098     assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
1099     ASTReader::FirstLatestDeclIDMap::iterator I
1100         = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1101     if (I != Reader.FirstLatestDeclIDs.end()) {
1102       if (Decl *NewLatest = Reader.GetDecl(I->second))
1103         LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
1104     }
1105 
1106     assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
1107     D->getCommonPtr()->Latest = LatestDecl;
1108   }
1109 
1110   VisitTemplateDecl(D);
1111   D->IdentifierNamespace = Record[Idx++];
1112 }
1113 
1114 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1115   VisitRedeclarableTemplateDecl(D);
1116 
1117   if (D->getPreviousDeclaration() == 0) {
1118     // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1119     // the specializations.
1120     SmallVector<serialization::DeclID, 2> SpecIDs;
1121     SpecIDs.push_back(0);
1122 
1123     // Specializations.
1124     unsigned Size = Record[Idx++];
1125     SpecIDs[0] += Size;
1126     for (unsigned I = 0; I != Size; ++I)
1127       SpecIDs.push_back(ReadDeclID(Record, Idx));
1128 
1129     // Partial specializations.
1130     Size = Record[Idx++];
1131     SpecIDs[0] += Size;
1132     for (unsigned I = 0; I != Size; ++I)
1133       SpecIDs.push_back(ReadDeclID(Record, Idx));
1134 
1135     if (SpecIDs[0]) {
1136       typedef serialization::DeclID DeclID;
1137 
1138       ClassTemplateDecl::Common *CommonPtr = D->getCommonPtr();
1139       CommonPtr->LazySpecializations
1140         = new (Reader.getContext()) DeclID [SpecIDs.size()];
1141       memcpy(CommonPtr->LazySpecializations, SpecIDs.data(),
1142              SpecIDs.size() * sizeof(DeclID));
1143     }
1144 
1145     // InjectedClassNameType is computed.
1146   }
1147 }
1148 
1149 void ASTDeclReader::VisitClassTemplateSpecializationDecl(
1150                                            ClassTemplateSpecializationDecl *D) {
1151   VisitCXXRecordDecl(D);
1152 
1153   ASTContext &C = Reader.getContext();
1154   if (Decl *InstD = ReadDecl(Record, Idx)) {
1155     if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
1156       D->SpecializedTemplate = CTD;
1157     } else {
1158       SmallVector<TemplateArgument, 8> TemplArgs;
1159       Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1160       TemplateArgumentList *ArgList
1161         = TemplateArgumentList::CreateCopy(C, TemplArgs.data(),
1162                                            TemplArgs.size());
1163       ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
1164           = new (C) ClassTemplateSpecializationDecl::
1165                                              SpecializedPartialSpecialization();
1166       PS->PartialSpecialization
1167           = cast<ClassTemplatePartialSpecializationDecl>(InstD);
1168       PS->TemplateArgs = ArgList;
1169       D->SpecializedTemplate = PS;
1170     }
1171   }
1172 
1173   // Explicit info.
1174   if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) {
1175     ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
1176         = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
1177     ExplicitInfo->TypeAsWritten = TyInfo;
1178     ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx);
1179     ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx);
1180     D->ExplicitInfo = ExplicitInfo;
1181   }
1182 
1183   SmallVector<TemplateArgument, 8> TemplArgs;
1184   Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1185   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs.data(),
1186                                                      TemplArgs.size());
1187   D->PointOfInstantiation = ReadSourceLocation(Record, Idx);
1188   D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++];
1189 
1190   if (D->isCanonicalDecl()) { // It's kept in the folding set.
1191     ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx);
1192     if (ClassTemplatePartialSpecializationDecl *Partial
1193                        = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
1194       CanonPattern->getCommonPtr()->PartialSpecializations.InsertNode(Partial);
1195     } else {
1196       CanonPattern->getCommonPtr()->Specializations.InsertNode(D);
1197     }
1198   }
1199 }
1200 
1201 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
1202                                     ClassTemplatePartialSpecializationDecl *D) {
1203   VisitClassTemplateSpecializationDecl(D);
1204 
1205   ASTContext &C = Reader.getContext();
1206   D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx);
1207 
1208   unsigned NumArgs = Record[Idx++];
1209   if (NumArgs) {
1210     D->NumArgsAsWritten = NumArgs;
1211     D->ArgsAsWritten = new (C) TemplateArgumentLoc[NumArgs];
1212     for (unsigned i=0; i != NumArgs; ++i)
1213       D->ArgsAsWritten[i] = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1214   }
1215 
1216   D->SequenceNumber = Record[Idx++];
1217 
1218   // These are read/set from/to the first declaration.
1219   if (D->getPreviousDeclaration() == 0) {
1220     D->InstantiatedFromMember.setPointer(
1221       ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx));
1222     D->InstantiatedFromMember.setInt(Record[Idx++]);
1223   }
1224 }
1225 
1226 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
1227                                     ClassScopeFunctionSpecializationDecl *D) {
1228   VisitDecl(D);
1229   D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx);
1230 }
1231 
1232 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1233   VisitRedeclarableTemplateDecl(D);
1234 
1235   if (D->getPreviousDeclaration() == 0) {
1236     // This FunctionTemplateDecl owns a CommonPtr; read it.
1237 
1238     // Read the function specialization declarations.
1239     // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1240     // when reading the specialized FunctionDecl.
1241     unsigned NumSpecs = Record[Idx++];
1242     while (NumSpecs--)
1243       (void)ReadDecl(Record, Idx);
1244   }
1245 }
1246 
1247 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1248   VisitTypeDecl(D);
1249 
1250   D->setDeclaredWithTypename(Record[Idx++]);
1251 
1252   bool Inherited = Record[Idx++];
1253   TypeSourceInfo *DefArg = GetTypeSourceInfo(Record, Idx);
1254   D->setDefaultArgument(DefArg, Inherited);
1255 }
1256 
1257 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1258   VisitDeclaratorDecl(D);
1259   // TemplateParmPosition.
1260   D->setDepth(Record[Idx++]);
1261   D->setPosition(Record[Idx++]);
1262   if (D->isExpandedParameterPack()) {
1263     void **Data = reinterpret_cast<void **>(D + 1);
1264     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1265       Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr();
1266       Data[2*I + 1] = GetTypeSourceInfo(Record, Idx);
1267     }
1268   } else {
1269     // Rest of NonTypeTemplateParmDecl.
1270     D->ParameterPack = Record[Idx++];
1271     if (Record[Idx++]) {
1272       Expr *DefArg = Reader.ReadExpr(F);
1273       bool Inherited = Record[Idx++];
1274       D->setDefaultArgument(DefArg, Inherited);
1275    }
1276   }
1277 }
1278 
1279 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1280   VisitTemplateDecl(D);
1281   // TemplateParmPosition.
1282   D->setDepth(Record[Idx++]);
1283   D->setPosition(Record[Idx++]);
1284   // Rest of TemplateTemplateParmDecl.
1285   TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1286   bool IsInherited = Record[Idx++];
1287   D->setDefaultArgument(Arg, IsInherited);
1288   D->ParameterPack = Record[Idx++];
1289 }
1290 
1291 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1292   VisitRedeclarableTemplateDecl(D);
1293 }
1294 
1295 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
1296   VisitDecl(D);
1297   D->AssertExpr = Reader.ReadExpr(F);
1298   D->Message = cast<StringLiteral>(Reader.ReadExpr(F));
1299   D->RParenLoc = ReadSourceLocation(Record, Idx);
1300 }
1301 
1302 std::pair<uint64_t, uint64_t>
1303 ASTDeclReader::VisitDeclContext(DeclContext *DC) {
1304   uint64_t LexicalOffset = Record[Idx++];
1305   uint64_t VisibleOffset = Record[Idx++];
1306   return std::make_pair(LexicalOffset, VisibleOffset);
1307 }
1308 
1309 template <typename T>
1310 void ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1311   enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1312   RedeclKind Kind = (RedeclKind)Record[Idx++];
1313   switch (Kind) {
1314   default:
1315     llvm_unreachable("Out of sync with ASTDeclWriter::VisitRedeclarable or"
1316                      " messed up reading");
1317   case NoRedeclaration:
1318     break;
1319   case PointsToPrevious: {
1320     DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1321     DeclID FirstDeclID = ReadDeclID(Record, Idx);
1322     // We delay loading of the redeclaration chain to avoid deeply nested calls.
1323     // We temporarily set the first (canonical) declaration as the previous one
1324     // which is the one that matters and mark the real previous DeclID to be
1325     // loaded & attached later on.
1326     D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1327                                 cast_or_null<T>(Reader.GetDecl(FirstDeclID)));
1328     if (PreviousDeclID != FirstDeclID)
1329       Reader.PendingPreviousDecls.push_back(std::make_pair(static_cast<T*>(D),
1330                                                            PreviousDeclID));
1331     break;
1332   }
1333   case PointsToLatest:
1334     D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1335                                                    ReadDeclAs<T>(Record, Idx));
1336     break;
1337   }
1338 
1339   assert(!(Kind == PointsToPrevious &&
1340            Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1341                Reader.FirstLatestDeclIDs.end()) &&
1342          "This decl is not first, it should not be in the map");
1343   if (Kind == PointsToPrevious)
1344     return;
1345 
1346   // This decl is a first one and the latest declaration that it points to is in
1347   // the same AST file. However, if this actually needs to point to a
1348   // redeclaration in another AST file, we need to update it by checking the
1349   // FirstLatestDeclIDs map which tracks this kind of decls.
1350   assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1351          "Invalid ThisDeclID ?");
1352   ASTReader::FirstLatestDeclIDMap::iterator I
1353       = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1354   if (I != Reader.FirstLatestDeclIDs.end()) {
1355     Decl *NewLatest = Reader.GetDecl(I->second);
1356     D->RedeclLink
1357         = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1358   }
1359 }
1360 
1361 //===----------------------------------------------------------------------===//
1362 // Attribute Reading
1363 //===----------------------------------------------------------------------===//
1364 
1365 /// \brief Reads attributes from the current stream position.
1366 void ASTReader::ReadAttributes(Module &F, AttrVec &Attrs,
1367                                const RecordData &Record, unsigned &Idx) {
1368   for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) {
1369     Attr *New = 0;
1370     attr::Kind Kind = (attr::Kind)Record[Idx++];
1371     SourceRange Range = ReadSourceRange(F, Record, Idx);
1372 
1373 #include "clang/Serialization/AttrPCHRead.inc"
1374 
1375     assert(New && "Unable to decode attribute?");
1376     Attrs.push_back(New);
1377   }
1378 }
1379 
1380 //===----------------------------------------------------------------------===//
1381 // ASTReader Implementation
1382 //===----------------------------------------------------------------------===//
1383 
1384 /// \brief Note that we have loaded the declaration with the given
1385 /// Index.
1386 ///
1387 /// This routine notes that this declaration has already been loaded,
1388 /// so that future GetDecl calls will return this declaration rather
1389 /// than trying to load a new declaration.
1390 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
1391   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1392   DeclsLoaded[Index] = D;
1393 }
1394 
1395 
1396 /// \brief Determine whether the consumer will be interested in seeing
1397 /// this declaration (via HandleTopLevelDecl).
1398 ///
1399 /// This routine should return true for anything that might affect
1400 /// code generation, e.g., inline function definitions, Objective-C
1401 /// declarations with metadata, etc.
1402 static bool isConsumerInterestedIn(Decl *D) {
1403   // An ObjCMethodDecl is never considered as "interesting" because its
1404   // implementation container always is.
1405 
1406   if (isa<FileScopeAsmDecl>(D) ||
1407       isa<ObjCProtocolDecl>(D) ||
1408       isa<ObjCImplDecl>(D))
1409     return true;
1410   if (VarDecl *Var = dyn_cast<VarDecl>(D))
1411     return Var->isFileVarDecl() &&
1412            Var->isThisDeclarationADefinition() == VarDecl::Definition;
1413   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1414     return Func->doesThisDeclarationHaveABody();
1415 
1416   return false;
1417 }
1418 
1419 /// \brief Get the correct cursor and offset for loading a declaration.
1420 ASTReader::RecordLocation
1421 ASTReader::DeclCursorForID(DeclID ID) {
1422   // See if there's an override.
1423   DeclReplacementMap::iterator It = ReplacedDecls.find(ID);
1424   if (It != ReplacedDecls.end())
1425     return RecordLocation(It->second.first, It->second.second);
1426 
1427   GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
1428   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
1429   Module *M = I->second;
1430   return RecordLocation(M,
1431            M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]);
1432 }
1433 
1434 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
1435   ContinuousRangeMap<uint64_t, Module*, 4>::iterator I
1436     = GlobalBitOffsetsMap.find(GlobalOffset);
1437 
1438   assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
1439   return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
1440 }
1441 
1442 uint64_t ASTReader::getGlobalBitOffset(Module &M, uint32_t LocalOffset) {
1443   return LocalOffset + M.GlobalBitOffset;
1444 }
1445 
1446 void ASTDeclReader::attachPreviousDecl(Decl *D, Decl *previous) {
1447   assert(D && previous);
1448   if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1449     TD->RedeclLink.setPointer(cast<TagDecl>(previous));
1450   } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1451     FD->RedeclLink.setPointer(cast<FunctionDecl>(previous));
1452   } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1453     VD->RedeclLink.setPointer(cast<VarDecl>(previous));
1454   } else {
1455     RedeclarableTemplateDecl *TD = cast<RedeclarableTemplateDecl>(D);
1456     TD->CommonOrPrev = cast<RedeclarableTemplateDecl>(previous);
1457   }
1458 }
1459 
1460 void ASTReader::loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID) {
1461   Decl *previous = GetDecl(ID);
1462   ASTDeclReader::attachPreviousDecl(D, previous);
1463 }
1464 
1465 /// \brief Read the declaration at the given offset from the AST file.
1466 Decl *ASTReader::ReadDeclRecord(DeclID ID) {
1467   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
1468   RecordLocation Loc = DeclCursorForID(ID);
1469   llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
1470   // Keep track of where we are in the stream, then jump back there
1471   // after reading this declaration.
1472   SavedStreamPosition SavedPosition(DeclsCursor);
1473 
1474   ReadingKindTracker ReadingKind(Read_Decl, *this);
1475 
1476   // Note that we are loading a declaration record.
1477   Deserializing ADecl(this);
1478 
1479   DeclsCursor.JumpToBit(Loc.Offset);
1480   RecordData Record;
1481   unsigned Code = DeclsCursor.ReadCode();
1482   unsigned Idx = 0;
1483   ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, Record, Idx);
1484 
1485   Decl *D = 0;
1486   switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
1487   case DECL_CONTEXT_LEXICAL:
1488   case DECL_CONTEXT_VISIBLE:
1489     llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
1490   case DECL_TYPEDEF:
1491     D = TypedefDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1492                             0, 0);
1493     break;
1494   case DECL_TYPEALIAS:
1495     D = TypeAliasDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1496                               0, 0);
1497     break;
1498   case DECL_ENUM:
1499     D = EnumDecl::Create(Context, Decl::EmptyShell());
1500     break;
1501   case DECL_RECORD:
1502     D = RecordDecl::Create(Context, Decl::EmptyShell());
1503     break;
1504   case DECL_ENUM_CONSTANT:
1505     D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1506                                  0, llvm::APSInt());
1507     break;
1508   case DECL_FUNCTION:
1509     D = FunctionDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1510                              DeclarationName(), QualType(), 0);
1511     break;
1512   case DECL_LINKAGE_SPEC:
1513     D = LinkageSpecDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1514                                 (LinkageSpecDecl::LanguageIDs)0,
1515                                 SourceLocation());
1516     break;
1517   case DECL_LABEL:
1518     D = LabelDecl::Create(Context, 0, SourceLocation(), 0);
1519     break;
1520   case DECL_NAMESPACE:
1521     D = NamespaceDecl::Create(Context, 0, SourceLocation(),
1522                               SourceLocation(), 0);
1523     break;
1524   case DECL_NAMESPACE_ALIAS:
1525     D = NamespaceAliasDecl::Create(Context, 0, SourceLocation(),
1526                                    SourceLocation(), 0,
1527                                    NestedNameSpecifierLoc(),
1528                                    SourceLocation(), 0);
1529     break;
1530   case DECL_USING:
1531     D = UsingDecl::Create(Context, 0, SourceLocation(),
1532                           NestedNameSpecifierLoc(), DeclarationNameInfo(),
1533                           false);
1534     break;
1535   case DECL_USING_SHADOW:
1536     D = UsingShadowDecl::Create(Context, 0, SourceLocation(), 0, 0);
1537     break;
1538   case DECL_USING_DIRECTIVE:
1539     D = UsingDirectiveDecl::Create(Context, 0, SourceLocation(),
1540                                    SourceLocation(), NestedNameSpecifierLoc(),
1541                                    SourceLocation(), 0, 0);
1542     break;
1543   case DECL_UNRESOLVED_USING_VALUE:
1544     D = UnresolvedUsingValueDecl::Create(Context, 0, SourceLocation(),
1545                                          NestedNameSpecifierLoc(),
1546                                          DeclarationNameInfo());
1547     break;
1548   case DECL_UNRESOLVED_USING_TYPENAME:
1549     D = UnresolvedUsingTypenameDecl::Create(Context, 0, SourceLocation(),
1550                                             SourceLocation(),
1551                                             NestedNameSpecifierLoc(),
1552                                             SourceLocation(),
1553                                             DeclarationName());
1554     break;
1555   case DECL_CXX_RECORD:
1556     D = CXXRecordDecl::Create(Context, Decl::EmptyShell());
1557     break;
1558   case DECL_CXX_METHOD:
1559     D = CXXMethodDecl::Create(Context, 0, SourceLocation(),
1560                               DeclarationNameInfo(), QualType(), 0,
1561                               false, SC_None, false, false, SourceLocation());
1562     break;
1563   case DECL_CXX_CONSTRUCTOR:
1564     D = CXXConstructorDecl::Create(Context, Decl::EmptyShell());
1565     break;
1566   case DECL_CXX_DESTRUCTOR:
1567     D = CXXDestructorDecl::Create(Context, Decl::EmptyShell());
1568     break;
1569   case DECL_CXX_CONVERSION:
1570     D = CXXConversionDecl::Create(Context, Decl::EmptyShell());
1571     break;
1572   case DECL_ACCESS_SPEC:
1573     D = AccessSpecDecl::Create(Context, Decl::EmptyShell());
1574     break;
1575   case DECL_FRIEND:
1576     D = FriendDecl::Create(Context, Decl::EmptyShell());
1577     break;
1578   case DECL_FRIEND_TEMPLATE:
1579     D = FriendTemplateDecl::Create(Context, Decl::EmptyShell());
1580     break;
1581   case DECL_CLASS_TEMPLATE:
1582     D = ClassTemplateDecl::Create(Context, Decl::EmptyShell());
1583     break;
1584   case DECL_CLASS_TEMPLATE_SPECIALIZATION:
1585     D = ClassTemplateSpecializationDecl::Create(Context, Decl::EmptyShell());
1586     break;
1587   case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
1588     D = ClassTemplatePartialSpecializationDecl::Create(Context,
1589                                                        Decl::EmptyShell());
1590     break;
1591   case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
1592     D = ClassScopeFunctionSpecializationDecl::Create(Context,
1593                                                      Decl::EmptyShell());
1594     break;
1595   case DECL_FUNCTION_TEMPLATE:
1596       D = FunctionTemplateDecl::Create(Context, Decl::EmptyShell());
1597     break;
1598   case DECL_TEMPLATE_TYPE_PARM:
1599     D = TemplateTypeParmDecl::Create(Context, Decl::EmptyShell());
1600     break;
1601   case DECL_NON_TYPE_TEMPLATE_PARM:
1602     D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1603                                         SourceLocation(), 0, 0, 0, QualType(),
1604                                         false, 0);
1605     break;
1606   case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
1607     D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1608                                         SourceLocation(), 0, 0, 0, QualType(),
1609                                         0, 0, Record[Idx++], 0);
1610     break;
1611   case DECL_TEMPLATE_TEMPLATE_PARM:
1612     D = TemplateTemplateParmDecl::Create(Context, 0, SourceLocation(), 0, 0,
1613                                          false, 0, 0);
1614     break;
1615   case DECL_TYPE_ALIAS_TEMPLATE:
1616     D = TypeAliasTemplateDecl::Create(Context, Decl::EmptyShell());
1617     break;
1618   case DECL_STATIC_ASSERT:
1619     D = StaticAssertDecl::Create(Context, 0, SourceLocation(), 0, 0,
1620                                  SourceLocation());
1621     break;
1622 
1623   case DECL_OBJC_METHOD:
1624     D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1625                                Selector(), QualType(), 0, 0);
1626     break;
1627   case DECL_OBJC_INTERFACE:
1628     D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
1629     break;
1630   case DECL_OBJC_IVAR:
1631     D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1632                              0, QualType(), 0, ObjCIvarDecl::None);
1633     break;
1634   case DECL_OBJC_PROTOCOL:
1635     D = ObjCProtocolDecl::Create(Context, 0, SourceLocation(), 0);
1636     break;
1637   case DECL_OBJC_AT_DEFS_FIELD:
1638     D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(),
1639                                     SourceLocation(), 0, QualType(), 0);
1640     break;
1641   case DECL_OBJC_CLASS:
1642     D = ObjCClassDecl::Create(Context, 0, SourceLocation());
1643     break;
1644   case DECL_OBJC_FORWARD_PROTOCOL:
1645     D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
1646     break;
1647   case DECL_OBJC_CATEGORY:
1648     D = ObjCCategoryDecl::Create(Context, Decl::EmptyShell());
1649     break;
1650   case DECL_OBJC_CATEGORY_IMPL:
1651     D = ObjCCategoryImplDecl::Create(Context, 0, SourceLocation(), 0, 0);
1652     break;
1653   case DECL_OBJC_IMPLEMENTATION:
1654     D = ObjCImplementationDecl::Create(Context, 0, SourceLocation(), 0, 0);
1655     break;
1656   case DECL_OBJC_COMPATIBLE_ALIAS:
1657     D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
1658     break;
1659   case DECL_OBJC_PROPERTY:
1660     D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, SourceLocation(),
1661                                  0);
1662     break;
1663   case DECL_OBJC_PROPERTY_IMPL:
1664     D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
1665                                      SourceLocation(), 0,
1666                                      ObjCPropertyImplDecl::Dynamic, 0,
1667                                      SourceLocation());
1668     break;
1669   case DECL_FIELD:
1670     D = FieldDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1671                           QualType(), 0, 0, false, false);
1672     break;
1673   case DECL_INDIRECTFIELD:
1674     D = IndirectFieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1675                                   0, 0);
1676     break;
1677   case DECL_VAR:
1678     D = VarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1679                         QualType(), 0, SC_None, SC_None);
1680     break;
1681 
1682   case DECL_IMPLICIT_PARAM:
1683     D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType());
1684     break;
1685 
1686   case DECL_PARM_VAR:
1687     D = ParmVarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1688                             QualType(), 0, SC_None, SC_None, 0);
1689     break;
1690   case DECL_FILE_SCOPE_ASM:
1691     D = FileScopeAsmDecl::Create(Context, 0, 0, SourceLocation(),
1692                                  SourceLocation());
1693     break;
1694   case DECL_BLOCK:
1695     D = BlockDecl::Create(Context, 0, SourceLocation());
1696     break;
1697   case DECL_CXX_BASE_SPECIFIERS:
1698     Error("attempt to read a C++ base-specifier record as a declaration");
1699     return 0;
1700   }
1701 
1702   assert(D && "Unknown declaration reading AST file");
1703   LoadedDecl(Index, D);
1704   Reader.Visit(D);
1705 
1706   // If this declaration is also a declaration context, get the
1707   // offsets for its tables of lexical and visible declarations.
1708   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1709     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1710     if (Offsets.first || Offsets.second) {
1711       if (Offsets.first != 0)
1712         DC->setHasExternalLexicalStorage(true);
1713       if (Offsets.second != 0)
1714         DC->setHasExternalVisibleStorage(true);
1715       if (ReadDeclContextStorage(*Loc.F, DeclsCursor, Offsets,
1716                                  Loc.F->DeclContextInfos[DC]))
1717         return 0;
1718     }
1719 
1720     // Now add the pending visible updates for this decl context, if it has any.
1721     DeclContextVisibleUpdatesPending::iterator I =
1722         PendingVisibleUpdates.find(ID);
1723     if (I != PendingVisibleUpdates.end()) {
1724       // There are updates. This means the context has external visible
1725       // storage, even if the original stored version didn't.
1726       DC->setHasExternalVisibleStorage(true);
1727       DeclContextVisibleUpdates &U = I->second;
1728       for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end();
1729            UI != UE; ++UI) {
1730         UI->second->DeclContextInfos[DC].NameLookupTableData = UI->first;
1731       }
1732       PendingVisibleUpdates.erase(I);
1733     }
1734   }
1735   assert(Idx == Record.size());
1736 
1737   // Load any relevant update records.
1738   loadDeclUpdateRecords(ID, D);
1739 
1740   if (ObjCChainedCategoriesInterfaces.count(ID))
1741     loadObjCChainedCategories(ID, cast<ObjCInterfaceDecl>(D));
1742 
1743   // If we have deserialized a declaration that has a definition the
1744   // AST consumer might need to know about, queue it.
1745   // We don't pass it to the consumer immediately because we may be in recursive
1746   // loading, and some declarations may still be initializing.
1747   if (isConsumerInterestedIn(D))
1748       InterestingDecls.push_back(D);
1749 
1750   return D;
1751 }
1752 
1753 void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
1754   // The declaration may have been modified by files later in the chain.
1755   // If this is the case, read the record containing the updates from each file
1756   // and pass it to ASTDeclReader to make the modifications.
1757   DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
1758   if (UpdI != DeclUpdateOffsets.end()) {
1759     FileOffsetsTy &UpdateOffsets = UpdI->second;
1760     for (FileOffsetsTy::iterator
1761          I = UpdateOffsets.begin(), E = UpdateOffsets.end(); I != E; ++I) {
1762       Module *F = I->first;
1763       uint64_t Offset = I->second;
1764       llvm::BitstreamCursor &Cursor = F->DeclsCursor;
1765       SavedStreamPosition SavedPosition(Cursor);
1766       Cursor.JumpToBit(Offset);
1767       RecordData Record;
1768       unsigned Code = Cursor.ReadCode();
1769       unsigned RecCode = Cursor.ReadRecord(Code, Record);
1770       (void)RecCode;
1771       assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
1772 
1773       unsigned Idx = 0;
1774       ASTDeclReader Reader(*this, *F, Cursor, ID, Record, Idx);
1775       Reader.UpdateDecl(D, *F, Record);
1776     }
1777   }
1778 }
1779 
1780 namespace {
1781   /// \brief Given an ObjC interface, goes through the modules and links to the
1782   /// interface all the categories for it.
1783   class ObjCChainedCategoriesVisitor {
1784     ASTReader &Reader;
1785     serialization::GlobalDeclID InterfaceID;
1786     ObjCInterfaceDecl *Interface;
1787     ObjCCategoryDecl *GlobHeadCat, *GlobTailCat;
1788     llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
1789 
1790   public:
1791     ObjCChainedCategoriesVisitor(ASTReader &Reader,
1792                                  serialization::GlobalDeclID InterfaceID,
1793                                  ObjCInterfaceDecl *Interface)
1794       : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface),
1795         GlobHeadCat(0), GlobTailCat(0) { }
1796 
1797     static bool visit(Module &M, void *UserData) {
1798       return static_cast<ObjCChainedCategoriesVisitor *>(UserData)->visit(M);
1799     }
1800 
1801     bool visit(Module &M) {
1802       if (Reader.isDeclIDFromModule(InterfaceID, M))
1803         return true; // We reached the module where the interface originated
1804                     // from. Stop traversing the imported modules.
1805 
1806       Module::ChainedObjCCategoriesMap::iterator
1807         I = M.ChainedObjCCategories.find(InterfaceID);
1808       if (I == M.ChainedObjCCategories.end())
1809         return false;
1810 
1811       ObjCCategoryDecl *
1812         HeadCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.first);
1813       ObjCCategoryDecl *
1814         TailCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.second);
1815 
1816       addCategories(HeadCat, TailCat);
1817       return false;
1818     }
1819 
1820     void addCategories(ObjCCategoryDecl *HeadCat,
1821                        ObjCCategoryDecl *TailCat = 0) {
1822       if (!HeadCat) {
1823         assert(!TailCat);
1824         return;
1825       }
1826 
1827       if (!TailCat) {
1828         TailCat = HeadCat;
1829         while (TailCat->getNextClassCategory())
1830           TailCat = TailCat->getNextClassCategory();
1831       }
1832 
1833       if (!GlobHeadCat) {
1834         GlobHeadCat = HeadCat;
1835         GlobTailCat = TailCat;
1836       } else {
1837         ASTDeclReader::setNextObjCCategory(GlobTailCat, HeadCat);
1838         GlobTailCat = TailCat;
1839       }
1840 
1841       llvm::DenseSet<DeclarationName> Checked;
1842       for (ObjCCategoryDecl *Cat = HeadCat,
1843                             *CatEnd = TailCat->getNextClassCategory();
1844              Cat != CatEnd; Cat = Cat->getNextClassCategory()) {
1845         if (Checked.count(Cat->getDeclName()))
1846           continue;
1847         Checked.insert(Cat->getDeclName());
1848         checkForDuplicate(Cat);
1849       }
1850     }
1851 
1852     /// \brief Warns for duplicate categories that come from different modules.
1853     void checkForDuplicate(ObjCCategoryDecl *Cat) {
1854       DeclarationName Name = Cat->getDeclName();
1855       // Find the top category with the same name. We do not want to warn for
1856       // duplicates along the established chain because there were already
1857       // warnings for them when the module was created. We only want to warn for
1858       // duplicates between non-dependent modules:
1859       //
1860       //   MT     //
1861       //  /  \    //
1862       // ML  MR   //
1863       //
1864       // We want to warn for duplicates between ML and MR,not between ML and MT.
1865       //
1866       // FIXME: We should not warn for duplicates in diamond:
1867       //
1868       //   MT     //
1869       //  /  \    //
1870       // ML  MR   //
1871       //  \  /    //
1872       //   MB     //
1873       //
1874       // If there are duplicates in ML/MR, there will be warning when creating
1875       // MB *and* when importing MB. We should not warn when importing.
1876       for (ObjCCategoryDecl *Next = Cat->getNextClassCategory(); Next;
1877              Next = Next->getNextClassCategory()) {
1878         if (Next->getDeclName() == Name)
1879           Cat = Next;
1880       }
1881 
1882       ObjCCategoryDecl *&PrevCat = NameCategoryMap[Name];
1883       if (!PrevCat)
1884         PrevCat = Cat;
1885 
1886       if (PrevCat != Cat) {
1887         Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
1888           << Interface->getDeclName() << Name;
1889         Reader.Diag(PrevCat->getLocation(), diag::note_previous_definition);
1890       }
1891     }
1892 
1893     ObjCCategoryDecl *getHeadCategory() const { return GlobHeadCat; }
1894   };
1895 }
1896 
1897 void ASTReader::loadObjCChainedCategories(serialization::GlobalDeclID ID,
1898                                           ObjCInterfaceDecl *D) {
1899   ObjCChainedCategoriesVisitor Visitor(*this, ID, D);
1900   ModuleMgr.visit(ObjCChainedCategoriesVisitor::visit, &Visitor);
1901   // Also add the categories that the interface already links to.
1902   Visitor.addCategories(D->getCategoryList());
1903   D->setCategoryList(Visitor.getHeadCategory());
1904 }
1905 
1906 void ASTDeclReader::UpdateDecl(Decl *D, Module &Module,
1907                                const RecordData &Record) {
1908   unsigned Idx = 0;
1909   while (Idx < Record.size()) {
1910     switch ((DeclUpdateKind)Record[Idx++]) {
1911     case UPD_CXX_SET_DEFINITIONDATA: {
1912       CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1913       CXXRecordDecl *DefinitionDecl
1914         = Reader.ReadDeclAs<CXXRecordDecl>(Module, Record, Idx);
1915       assert(!RD->DefinitionData && "DefinitionData is already set!");
1916       InitializeCXXDefinitionData(RD, DefinitionDecl, Record, Idx);
1917       break;
1918     }
1919 
1920     case UPD_CXX_ADDED_IMPLICIT_MEMBER:
1921       cast<CXXRecordDecl>(D)->addedMember(Reader.ReadDecl(Module, Record, Idx));
1922       break;
1923 
1924     case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
1925       // It will be added to the template's specializations set when loaded.
1926       (void)Reader.ReadDecl(Module, Record, Idx);
1927       break;
1928 
1929     case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
1930       NamespaceDecl *Anon
1931         = Reader.ReadDeclAs<NamespaceDecl>(Module, Record, Idx);
1932       // Guard against these being loaded out of original order. Don't use
1933       // getNextNamespace(), since it tries to access the context and can't in
1934       // the middle of deserialization.
1935       if (!Anon->NextNamespace) {
1936         if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
1937           TU->setAnonymousNamespace(Anon);
1938         else
1939           cast<NamespaceDecl>(D)->OrigOrAnonNamespace.setPointer(Anon);
1940       }
1941       break;
1942     }
1943 
1944     case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
1945       cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
1946           Reader.ReadSourceLocation(Module, Record, Idx));
1947       break;
1948     }
1949   }
1950 }
1951