]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - contrib/llvm/tools/clang/lib/Serialization/ASTReaderDecl.cpp
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / contrib / llvm / tools / clang / lib / Serialization / ASTReaderDecl.cpp
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->setCompleteDefinition(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
486   MD->IsRedeclaration = Record[Idx++];
487   MD->HasRedeclaration = Record[Idx++];
488   if (MD->HasRedeclaration)
489     Reader.getContext().setObjCMethodRedeclaration(MD,
490                                        ReadDeclAs<ObjCMethodDecl>(Record, Idx));
491
492   MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
493   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
494   MD->SetRelatedResultType(Record[Idx++]);
495   MD->setResultType(Reader.readType(F, Record, Idx));
496   MD->setResultTypeSourceInfo(GetTypeSourceInfo(Record, Idx));
497   MD->setEndLoc(ReadSourceLocation(Record, Idx));
498   unsigned NumParams = Record[Idx++];
499   SmallVector<ParmVarDecl *, 16> Params;
500   Params.reserve(NumParams);
501   for (unsigned I = 0; I != NumParams; ++I)
502     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
503
504   MD->SelLocsKind = Record[Idx++];
505   unsigned NumStoredSelLocs = Record[Idx++];
506   SmallVector<SourceLocation, 16> SelLocs;
507   SelLocs.reserve(NumStoredSelLocs);
508   for (unsigned i = 0; i != NumStoredSelLocs; ++i)
509     SelLocs.push_back(ReadSourceLocation(Record, Idx));
510
511   MD->setParamsAndSelLocs(Reader.getContext(), Params, SelLocs);
512 }
513
514 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
515   VisitNamedDecl(CD);
516   CD->setAtStartLoc(ReadSourceLocation(Record, Idx));
517   CD->setAtEndRange(ReadSourceRange(Record, Idx));
518 }
519
520 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
521   VisitObjCContainerDecl(ID);
522   ID->setTypeForDecl(Reader.readType(F, Record, Idx).getTypePtrOrNull());
523   ID->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
524   
525   // Read the directly referenced protocols and their SourceLocations.
526   unsigned NumProtocols = Record[Idx++];
527   SmallVector<ObjCProtocolDecl *, 16> Protocols;
528   Protocols.reserve(NumProtocols);
529   for (unsigned I = 0; I != NumProtocols; ++I)
530     Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
531   SmallVector<SourceLocation, 16> ProtoLocs;
532   ProtoLocs.reserve(NumProtocols);
533   for (unsigned I = 0; I != NumProtocols; ++I)
534     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
535   ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
536                       Reader.getContext());
537   
538   // Read the transitive closure of protocols referenced by this class.
539   NumProtocols = Record[Idx++];
540   Protocols.clear();
541   Protocols.reserve(NumProtocols);
542   for (unsigned I = 0; I != NumProtocols; ++I)
543     Protocols.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
544   ID->AllReferencedProtocols.set(Protocols.data(), NumProtocols,
545                                  Reader.getContext());
546   
547   // Read the ivars.
548   unsigned NumIvars = Record[Idx++];
549   SmallVector<ObjCIvarDecl *, 16> IVars;
550   IVars.reserve(NumIvars);
551   for (unsigned I = 0; I != NumIvars; ++I)
552     IVars.push_back(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
553   ID->setCategoryList(ReadDeclAs<ObjCCategoryDecl>(Record, Idx));
554   
555   // We will rebuild this list lazily.
556   ID->setIvarList(0);
557   ID->setForwardDecl(Record[Idx++]);
558   ID->setImplicitInterfaceDecl(Record[Idx++]);
559   ID->setSuperClassLoc(ReadSourceLocation(Record, Idx));
560   ID->setLocEnd(ReadSourceLocation(Record, Idx));
561 }
562
563 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
564   VisitFieldDecl(IVD);
565   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
566   // This field will be built lazily.
567   IVD->setNextIvar(0);
568   bool synth = Record[Idx++];
569   IVD->setSynthesize(synth);
570 }
571
572 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
573   VisitObjCContainerDecl(PD);
574   PD->setForwardDecl(Record[Idx++]);
575   PD->setLocEnd(ReadSourceLocation(Record, Idx));
576   unsigned NumProtoRefs = Record[Idx++];
577   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
578   ProtoRefs.reserve(NumProtoRefs);
579   for (unsigned I = 0; I != NumProtoRefs; ++I)
580     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
581   SmallVector<SourceLocation, 16> ProtoLocs;
582   ProtoLocs.reserve(NumProtoRefs);
583   for (unsigned I = 0; I != NumProtoRefs; ++I)
584     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
585   PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
586                       Reader.getContext());
587 }
588
589 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
590   VisitFieldDecl(FD);
591 }
592
593 void ASTDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
594   VisitDecl(CD);
595   ObjCInterfaceDecl *ClassRef = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
596   SourceLocation SLoc = ReadSourceLocation(Record, Idx);
597   CD->setClass(Reader.getContext(), ClassRef, SLoc);
598 }
599
600 void ASTDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
601   VisitDecl(FPD);
602   unsigned NumProtoRefs = Record[Idx++];
603   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
604   ProtoRefs.reserve(NumProtoRefs);
605   for (unsigned I = 0; I != NumProtoRefs; ++I)
606     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
607   SmallVector<SourceLocation, 16> ProtoLocs;
608   ProtoLocs.reserve(NumProtoRefs);
609   for (unsigned I = 0; I != NumProtoRefs; ++I)
610     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
611   FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
612                        Reader.getContext());
613 }
614
615 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
616   VisitObjCContainerDecl(CD);
617   CD->ClassInterface = ReadDeclAs<ObjCInterfaceDecl>(Record, Idx);
618   unsigned NumProtoRefs = Record[Idx++];
619   SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
620   ProtoRefs.reserve(NumProtoRefs);
621   for (unsigned I = 0; I != NumProtoRefs; ++I)
622     ProtoRefs.push_back(ReadDeclAs<ObjCProtocolDecl>(Record, Idx));
623   SmallVector<SourceLocation, 16> ProtoLocs;
624   ProtoLocs.reserve(NumProtoRefs);
625   for (unsigned I = 0; I != NumProtoRefs; ++I)
626     ProtoLocs.push_back(ReadSourceLocation(Record, Idx));
627   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
628                       Reader.getContext());
629   CD->NextClassCategory = ReadDeclAs<ObjCCategoryDecl>(Record, Idx);
630   CD->setHasSynthBitfield(Record[Idx++]);
631   CD->setCategoryNameLoc(ReadSourceLocation(Record, Idx));
632 }
633
634 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
635   VisitNamedDecl(CAD);
636   CAD->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
637 }
638
639 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
640   VisitNamedDecl(D);
641   D->setAtLoc(ReadSourceLocation(Record, Idx));
642   D->setType(GetTypeSourceInfo(Record, Idx));
643   // FIXME: stable encoding
644   D->setPropertyAttributes(
645                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
646   D->setPropertyAttributesAsWritten(
647                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
648   // FIXME: stable encoding
649   D->setPropertyImplementation(
650                             (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
651   D->setGetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
652   D->setSetterName(Reader.ReadDeclarationName(F,Record, Idx).getObjCSelector());
653   D->setGetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
654   D->setSetterMethodDecl(ReadDeclAs<ObjCMethodDecl>(Record, Idx));
655   D->setPropertyIvarDecl(ReadDeclAs<ObjCIvarDecl>(Record, Idx));
656 }
657
658 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
659   VisitObjCContainerDecl(D);
660   D->setClassInterface(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
661 }
662
663 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
664   VisitObjCImplDecl(D);
665   D->setIdentifier(Reader.GetIdentifierInfo(F, Record, Idx));
666 }
667
668 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
669   VisitObjCImplDecl(D);
670   D->setSuperClass(ReadDeclAs<ObjCInterfaceDecl>(Record, Idx));
671   llvm::tie(D->IvarInitializers, D->NumIvarInitializers)
672       = Reader.ReadCXXCtorInitializers(F, Record, Idx);
673   D->setHasSynthBitfield(Record[Idx++]);
674 }
675
676
677 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
678   VisitDecl(D);
679   D->setAtLoc(ReadSourceLocation(Record, Idx));
680   D->setPropertyDecl(ReadDeclAs<ObjCPropertyDecl>(Record, Idx));
681   D->PropertyIvarDecl = ReadDeclAs<ObjCIvarDecl>(Record, Idx);
682   D->IvarLoc = ReadSourceLocation(Record, Idx);
683   D->setGetterCXXConstructor(Reader.ReadExpr(F));
684   D->setSetterCXXAssignment(Reader.ReadExpr(F));
685 }
686
687 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
688   VisitDeclaratorDecl(FD);
689   FD->setMutable(Record[Idx++]);
690   int BitWidthOrInitializer = Record[Idx++];
691   if (BitWidthOrInitializer == 1)
692     FD->setBitWidth(Reader.ReadExpr(F));
693   else if (BitWidthOrInitializer == 2)
694     FD->setInClassInitializer(Reader.ReadExpr(F));
695   if (!FD->getDeclName()) {
696     if (FieldDecl *Tmpl = ReadDeclAs<FieldDecl>(Record, Idx))
697       Reader.getContext().setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
698   }
699 }
700
701 void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) {
702   VisitValueDecl(FD);
703
704   FD->ChainingSize = Record[Idx++];
705   assert(FD->ChainingSize >= 2 && "Anonymous chaining must be >= 2");
706   FD->Chaining = new (Reader.getContext())NamedDecl*[FD->ChainingSize];
707
708   for (unsigned I = 0; I != FD->ChainingSize; ++I)
709     FD->Chaining[I] = ReadDeclAs<NamedDecl>(Record, Idx);
710 }
711
712 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
713   VisitDeclaratorDecl(VD);
714   VisitRedeclarable(VD);
715   VD->VarDeclBits.SClass = (StorageClass)Record[Idx++];
716   VD->VarDeclBits.SClassAsWritten = (StorageClass)Record[Idx++];
717   VD->VarDeclBits.ThreadSpecified = Record[Idx++];
718   VD->VarDeclBits.HasCXXDirectInit = Record[Idx++];
719   VD->VarDeclBits.ExceptionVar = Record[Idx++];
720   VD->VarDeclBits.NRVOVariable = Record[Idx++];
721   VD->VarDeclBits.CXXForRangeDecl = Record[Idx++];
722   VD->VarDeclBits.ARCPseudoStrong = Record[Idx++];
723   if (Record[Idx++])
724     VD->setInit(Reader.ReadExpr(F));
725
726   if (Record[Idx++]) { // HasMemberSpecializationInfo.
727     VarDecl *Tmpl = ReadDeclAs<VarDecl>(Record, Idx);
728     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
729     SourceLocation POI = ReadSourceLocation(Record, Idx);
730     Reader.getContext().setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
731   }
732 }
733
734 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
735   VisitVarDecl(PD);
736 }
737
738 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
739   VisitVarDecl(PD);
740   unsigned isObjCMethodParam = Record[Idx++];
741   unsigned scopeDepth = Record[Idx++];
742   unsigned scopeIndex = Record[Idx++];
743   unsigned declQualifier = Record[Idx++];
744   if (isObjCMethodParam) {
745     assert(scopeDepth == 0);
746     PD->setObjCMethodScopeInfo(scopeIndex);
747     PD->ParmVarDeclBits.ScopeDepthOrObjCQuals = declQualifier;
748   } else {
749     PD->setScopeInfo(scopeDepth, scopeIndex);
750   }
751   PD->ParmVarDeclBits.IsKNRPromoted = Record[Idx++];
752   PD->ParmVarDeclBits.HasInheritedDefaultArg = Record[Idx++];
753   if (Record[Idx++]) // hasUninstantiatedDefaultArg.
754     PD->setUninstantiatedDefaultArg(Reader.ReadExpr(F));
755 }
756
757 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
758   VisitDecl(AD);
759   AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(F)));
760   AD->setRParenLoc(ReadSourceLocation(Record, Idx));
761 }
762
763 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
764   VisitDecl(BD);
765   BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(F)));
766   BD->setSignatureAsWritten(GetTypeSourceInfo(Record, Idx));
767   unsigned NumParams = Record[Idx++];
768   SmallVector<ParmVarDecl *, 16> Params;
769   Params.reserve(NumParams);
770   for (unsigned I = 0; I != NumParams; ++I)
771     Params.push_back(ReadDeclAs<ParmVarDecl>(Record, Idx));
772   BD->setParams(Params);
773
774   bool capturesCXXThis = Record[Idx++];
775   unsigned numCaptures = Record[Idx++];
776   SmallVector<BlockDecl::Capture, 16> captures;
777   captures.reserve(numCaptures);
778   for (unsigned i = 0; i != numCaptures; ++i) {
779     VarDecl *decl = ReadDeclAs<VarDecl>(Record, Idx);
780     unsigned flags = Record[Idx++];
781     bool byRef = (flags & 1);
782     bool nested = (flags & 2);
783     Expr *copyExpr = ((flags & 4) ? Reader.ReadExpr(F) : 0);
784
785     captures.push_back(BlockDecl::Capture(decl, byRef, nested, copyExpr));
786   }
787   BD->setCaptures(Reader.getContext(), captures.begin(),
788                   captures.end(), capturesCXXThis);
789 }
790
791 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
792   VisitDecl(D);
793   D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
794   D->setExternLoc(ReadSourceLocation(Record, Idx));
795   D->setRBraceLoc(ReadSourceLocation(Record, Idx));
796 }
797
798 void ASTDeclReader::VisitLabelDecl(LabelDecl *D) {
799   VisitNamedDecl(D);
800   D->setLocStart(ReadSourceLocation(Record, Idx));
801 }
802
803
804 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
805   VisitNamedDecl(D);
806   D->IsInline = Record[Idx++];
807   D->LocStart = ReadSourceLocation(Record, Idx);
808   D->RBraceLoc = ReadSourceLocation(Record, Idx);
809   D->NextNamespace = Record[Idx++];
810
811   bool IsOriginal = Record[Idx++];
812   // FIXME: Modules will likely have trouble with pointing directly at
813   // the original namespace.
814   D->OrigOrAnonNamespace.setInt(IsOriginal);
815   D->OrigOrAnonNamespace.setPointer(ReadDeclAs<NamespaceDecl>(Record, Idx));
816 }
817
818 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
819   VisitNamedDecl(D);
820   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
821   D->IdentLoc = ReadSourceLocation(Record, Idx);
822   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
823   D->Namespace = ReadDeclAs<NamedDecl>(Record, Idx);
824 }
825
826 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
827   VisitNamedDecl(D);
828   D->setUsingLocation(ReadSourceLocation(Record, Idx));
829   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
830   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
831   D->FirstUsingShadow = ReadDeclAs<UsingShadowDecl>(Record, Idx);
832   D->setTypeName(Record[Idx++]);
833   if (NamedDecl *Pattern = ReadDeclAs<NamedDecl>(Record, Idx))
834     Reader.getContext().setInstantiatedFromUsingDecl(D, Pattern);
835 }
836
837 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
838   VisitNamedDecl(D);
839   D->setTargetDecl(ReadDeclAs<NamedDecl>(Record, Idx));
840   D->UsingOrNextShadow = ReadDeclAs<NamedDecl>(Record, Idx);
841   UsingShadowDecl *Pattern = ReadDeclAs<UsingShadowDecl>(Record, Idx);
842   if (Pattern)
843     Reader.getContext().setInstantiatedFromUsingShadowDecl(D, Pattern);
844 }
845
846 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
847   VisitNamedDecl(D);
848   D->UsingLoc = ReadSourceLocation(Record, Idx);
849   D->NamespaceLoc = ReadSourceLocation(Record, Idx);
850   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
851   D->NominatedNamespace = ReadDeclAs<NamedDecl>(Record, Idx);
852   D->CommonAncestor = ReadDeclAs<DeclContext>(Record, Idx);
853 }
854
855 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
856   VisitValueDecl(D);
857   D->setUsingLoc(ReadSourceLocation(Record, Idx));
858   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
859   ReadDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record, Idx);
860 }
861
862 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
863                                                UnresolvedUsingTypenameDecl *D) {
864   VisitTypeDecl(D);
865   D->TypenameLocation = ReadSourceLocation(Record, Idx);
866   D->QualifierLoc = Reader.ReadNestedNameSpecifierLoc(F, Record, Idx);
867 }
868
869 void ASTDeclReader::ReadCXXDefinitionData(
870                                    struct CXXRecordDecl::DefinitionData &Data,
871                                    const RecordData &Record, unsigned &Idx) {
872   Data.UserDeclaredConstructor = Record[Idx++];
873   Data.UserDeclaredCopyConstructor = Record[Idx++];
874   Data.UserDeclaredMoveConstructor = Record[Idx++];
875   Data.UserDeclaredCopyAssignment = Record[Idx++];
876   Data.UserDeclaredMoveAssignment = Record[Idx++];
877   Data.UserDeclaredDestructor = Record[Idx++];
878   Data.Aggregate = Record[Idx++];
879   Data.PlainOldData = Record[Idx++];
880   Data.Empty = Record[Idx++];
881   Data.Polymorphic = Record[Idx++];
882   Data.Abstract = Record[Idx++];
883   Data.IsStandardLayout = Record[Idx++];
884   Data.HasNoNonEmptyBases = Record[Idx++];
885   Data.HasPrivateFields = Record[Idx++];
886   Data.HasProtectedFields = Record[Idx++];
887   Data.HasPublicFields = Record[Idx++];
888   Data.HasMutableFields = Record[Idx++];
889   Data.HasTrivialDefaultConstructor = Record[Idx++];
890   Data.HasConstexprNonCopyMoveConstructor = Record[Idx++];
891   Data.HasTrivialCopyConstructor = Record[Idx++];
892   Data.HasTrivialMoveConstructor = Record[Idx++];
893   Data.HasTrivialCopyAssignment = Record[Idx++];
894   Data.HasTrivialMoveAssignment = Record[Idx++];
895   Data.HasTrivialDestructor = Record[Idx++];
896   Data.HasNonLiteralTypeFieldsOrBases = Record[Idx++];
897   Data.ComputedVisibleConversions = Record[Idx++];
898   Data.UserProvidedDefaultConstructor = Record[Idx++];
899   Data.DeclaredDefaultConstructor = Record[Idx++];
900   Data.DeclaredCopyConstructor = Record[Idx++];
901   Data.DeclaredMoveConstructor = Record[Idx++];
902   Data.DeclaredCopyAssignment = Record[Idx++];
903   Data.DeclaredMoveAssignment = Record[Idx++];
904   Data.DeclaredDestructor = Record[Idx++];
905   Data.FailedImplicitMoveConstructor = Record[Idx++];
906   Data.FailedImplicitMoveAssignment = Record[Idx++];
907
908   Data.NumBases = Record[Idx++];
909   if (Data.NumBases)
910     Data.Bases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
911   Data.NumVBases = Record[Idx++];
912   if (Data.NumVBases)
913     Data.VBases = Reader.readCXXBaseSpecifiers(F, Record, Idx);
914   
915   Reader.ReadUnresolvedSet(F, Data.Conversions, Record, Idx);
916   Reader.ReadUnresolvedSet(F, Data.VisibleConversions, Record, Idx);
917   assert(Data.Definition && "Data.Definition should be already set!");
918   Data.FirstFriend = ReadDeclAs<FriendDecl>(Record, Idx);
919 }
920
921 void ASTDeclReader::InitializeCXXDefinitionData(CXXRecordDecl *D,
922                                                 CXXRecordDecl *DefinitionDecl,
923                                                 const RecordData &Record,
924                                                 unsigned &Idx) {
925   ASTContext &C = Reader.getContext();
926
927   if (D == DefinitionDecl) {
928     D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
929     ReadCXXDefinitionData(*D->DefinitionData, Record, Idx);
930     // We read the definition info. Check if there are pending forward
931     // references that need to point to this DefinitionData pointer.
932     ASTReader::PendingForwardRefsMap::iterator
933         FindI = Reader.PendingForwardRefs.find(D);
934     if (FindI != Reader.PendingForwardRefs.end()) {
935       ASTReader::ForwardRefs &Refs = FindI->second;
936       for (ASTReader::ForwardRefs::iterator
937              I = Refs.begin(), E = Refs.end(); I != E; ++I)
938         (*I)->DefinitionData = D->DefinitionData;
939 #ifndef NDEBUG
940       // We later check whether PendingForwardRefs is empty to make sure all
941       // pending references were linked.
942       Reader.PendingForwardRefs.erase(D);
943 #endif
944     }
945   } else if (DefinitionDecl) {
946     if (DefinitionDecl->DefinitionData) {
947       D->DefinitionData = DefinitionDecl->DefinitionData;
948     } else {
949       // The definition is still initializing.
950       Reader.PendingForwardRefs[DefinitionDecl].push_back(D);
951     }
952   }
953 }
954
955 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
956   VisitRecordDecl(D);
957
958   CXXRecordDecl *DefinitionDecl = ReadDeclAs<CXXRecordDecl>(Record, Idx);
959   InitializeCXXDefinitionData(D, DefinitionDecl, Record, Idx);
960
961   ASTContext &C = Reader.getContext();
962
963   enum CXXRecKind {
964     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
965   };
966   switch ((CXXRecKind)Record[Idx++]) {
967   default:
968     llvm_unreachable("Out of sync with ASTDeclWriter::VisitCXXRecordDecl?");
969   case CXXRecNotTemplate:
970     break;
971   case CXXRecTemplate:
972     D->TemplateOrInstantiation = ReadDeclAs<ClassTemplateDecl>(Record, Idx);
973     break;
974   case CXXRecMemberSpecialization: {
975     CXXRecordDecl *RD = ReadDeclAs<CXXRecordDecl>(Record, Idx);
976     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
977     SourceLocation POI = ReadSourceLocation(Record, Idx);
978     MemberSpecializationInfo *MSI = new (C) MemberSpecializationInfo(RD, TSK);
979     MSI->setPointOfInstantiation(POI);
980     D->TemplateOrInstantiation = MSI;
981     break;
982   }
983   }
984
985   // Load the key function to avoid deserializing every method so we can
986   // compute it.
987   if (D->IsCompleteDefinition) {
988     if (CXXMethodDecl *Key = ReadDeclAs<CXXMethodDecl>(Record, Idx))
989       C.KeyFunctions[D] = Key;
990   }
991 }
992
993 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
994   VisitFunctionDecl(D);
995   unsigned NumOverridenMethods = Record[Idx++];
996   while (NumOverridenMethods--) {
997     // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
998     // MD may be initializing.
999     if (CXXMethodDecl *MD = ReadDeclAs<CXXMethodDecl>(Record, Idx))
1000       Reader.getContext().addOverriddenMethod(D, MD);
1001   }
1002 }
1003
1004 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1005   VisitCXXMethodDecl(D);
1006   
1007   D->IsExplicitSpecified = Record[Idx++];
1008   D->ImplicitlyDefined = Record[Idx++];
1009   llvm::tie(D->CtorInitializers, D->NumCtorInitializers)
1010       = Reader.ReadCXXCtorInitializers(F, Record, Idx);
1011 }
1012
1013 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1014   VisitCXXMethodDecl(D);
1015
1016   D->ImplicitlyDefined = Record[Idx++];
1017   D->OperatorDelete = ReadDeclAs<FunctionDecl>(Record, Idx);
1018 }
1019
1020 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
1021   VisitCXXMethodDecl(D);
1022   D->IsExplicitSpecified = Record[Idx++];
1023 }
1024
1025 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
1026   VisitDecl(D);
1027   D->setColonLoc(ReadSourceLocation(Record, Idx));
1028 }
1029
1030 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
1031   VisitDecl(D);
1032   if (Record[Idx++])
1033     D->Friend = GetTypeSourceInfo(Record, Idx);
1034   else
1035     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1036   D->NextFriend = Record[Idx++];
1037   D->UnsupportedFriend = (Record[Idx++] != 0);
1038   D->FriendLoc = ReadSourceLocation(Record, Idx);
1039 }
1040
1041 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1042   VisitDecl(D);
1043   unsigned NumParams = Record[Idx++];
1044   D->NumParams = NumParams;
1045   D->Params = new TemplateParameterList*[NumParams];
1046   for (unsigned i = 0; i != NumParams; ++i)
1047     D->Params[i] = Reader.ReadTemplateParameterList(F, Record, Idx);
1048   if (Record[Idx++]) // HasFriendDecl
1049     D->Friend = ReadDeclAs<NamedDecl>(Record, Idx);
1050   else
1051     D->Friend = GetTypeSourceInfo(Record, Idx);
1052   D->FriendLoc = ReadSourceLocation(Record, Idx);
1053 }
1054
1055 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
1056   VisitNamedDecl(D);
1057
1058   NamedDecl *TemplatedDecl = ReadDeclAs<NamedDecl>(Record, Idx);
1059   TemplateParameterList* TemplateParams
1060       = Reader.ReadTemplateParameterList(F, Record, Idx); 
1061   D->init(TemplatedDecl, TemplateParams);
1062 }
1063
1064 void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1065   // Initialize CommonOrPrev before VisitTemplateDecl so that getCommonPtr()
1066   // can be used while this is still initializing.
1067
1068   assert(D->CommonOrPrev.isNull() && "getCommonPtr was called earlier on this");
1069   DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1070   DeclID FirstDeclID =  PreviousDeclID ? ReadDeclID(Record, Idx) : 0;
1071   // We delay loading of the redeclaration chain to avoid deeply nested calls.
1072   // We temporarily set the first (canonical) declaration as the previous one
1073   // which is the one that matters and mark the real previous DeclID to be
1074   // loaded & attached later on.
1075   RedeclarableTemplateDecl *FirstDecl =
1076       cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(FirstDeclID));
1077   assert((FirstDecl == 0 || FirstDecl->getKind() == D->getKind()) &&
1078          "FirstDecl kind mismatch");
1079   if (FirstDecl) {
1080     D->CommonOrPrev = FirstDecl;
1081     // Mark the real previous DeclID to be loaded & attached later on.
1082     if (PreviousDeclID != FirstDeclID)
1083       Reader.PendingPreviousDecls.push_back(std::make_pair(D, PreviousDeclID));
1084   } else {
1085     D->CommonOrPrev = D->newCommon(Reader.getContext());
1086     if (RedeclarableTemplateDecl *RTD
1087           = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx)) {
1088       assert(RTD->getKind() == D->getKind() &&
1089              "InstantiatedFromMemberTemplate kind mismatch");
1090       D->setInstantiatedFromMemberTemplateImpl(RTD);
1091       if (Record[Idx++])
1092         D->setMemberSpecialization();
1093     }
1094
1095     RedeclarableTemplateDecl *LatestDecl
1096       = ReadDeclAs<RedeclarableTemplateDecl>(Record, Idx);
1097   
1098     // This decl is a first one and the latest declaration that it points to is
1099     // in the same AST file. However, if this actually needs to point to a
1100     // redeclaration in another AST file, we need to update it by checking
1101     // the FirstLatestDeclIDs map which tracks this kind of decls.
1102     assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
1103     ASTReader::FirstLatestDeclIDMap::iterator I
1104         = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1105     if (I != Reader.FirstLatestDeclIDs.end()) {
1106       if (Decl *NewLatest = Reader.GetDecl(I->second))
1107         LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
1108     }
1109
1110     assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
1111     D->getCommonPtr()->Latest = LatestDecl;
1112   }
1113
1114   VisitTemplateDecl(D);
1115   D->IdentifierNamespace = Record[Idx++];
1116 }
1117
1118 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1119   VisitRedeclarableTemplateDecl(D);
1120
1121   if (D->getPreviousDeclaration() == 0) {
1122     // This ClassTemplateDecl owns a CommonPtr; read it to keep track of all of
1123     // the specializations.
1124     SmallVector<serialization::DeclID, 2> SpecIDs;
1125     SpecIDs.push_back(0);
1126     
1127     // Specializations.
1128     unsigned Size = Record[Idx++];
1129     SpecIDs[0] += Size;
1130     for (unsigned I = 0; I != Size; ++I)
1131       SpecIDs.push_back(ReadDeclID(Record, Idx));
1132
1133     // Partial specializations.
1134     Size = Record[Idx++];
1135     SpecIDs[0] += Size;
1136     for (unsigned I = 0; I != Size; ++I)
1137       SpecIDs.push_back(ReadDeclID(Record, Idx));
1138
1139     if (SpecIDs[0]) {
1140       typedef serialization::DeclID DeclID;
1141       
1142       ClassTemplateDecl::Common *CommonPtr = D->getCommonPtr();
1143       CommonPtr->LazySpecializations
1144         = new (Reader.getContext()) DeclID [SpecIDs.size()];
1145       memcpy(CommonPtr->LazySpecializations, SpecIDs.data(), 
1146              SpecIDs.size() * sizeof(DeclID));
1147     }
1148     
1149     // InjectedClassNameType is computed.
1150   }
1151 }
1152
1153 void ASTDeclReader::VisitClassTemplateSpecializationDecl(
1154                                            ClassTemplateSpecializationDecl *D) {
1155   VisitCXXRecordDecl(D);
1156   
1157   ASTContext &C = Reader.getContext();
1158   if (Decl *InstD = ReadDecl(Record, Idx)) {
1159     if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
1160       D->SpecializedTemplate = CTD;
1161     } else {
1162       SmallVector<TemplateArgument, 8> TemplArgs;
1163       Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1164       TemplateArgumentList *ArgList
1165         = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 
1166                                            TemplArgs.size());
1167       ClassTemplateSpecializationDecl::SpecializedPartialSpecialization *PS
1168           = new (C) ClassTemplateSpecializationDecl::
1169                                              SpecializedPartialSpecialization();
1170       PS->PartialSpecialization
1171           = cast<ClassTemplatePartialSpecializationDecl>(InstD);
1172       PS->TemplateArgs = ArgList;
1173       D->SpecializedTemplate = PS;
1174     }
1175   }
1176
1177   // Explicit info.
1178   if (TypeSourceInfo *TyInfo = GetTypeSourceInfo(Record, Idx)) {
1179     ClassTemplateSpecializationDecl::ExplicitSpecializationInfo *ExplicitInfo
1180         = new (C) ClassTemplateSpecializationDecl::ExplicitSpecializationInfo;
1181     ExplicitInfo->TypeAsWritten = TyInfo;
1182     ExplicitInfo->ExternLoc = ReadSourceLocation(Record, Idx);
1183     ExplicitInfo->TemplateKeywordLoc = ReadSourceLocation(Record, Idx);
1184     D->ExplicitInfo = ExplicitInfo;
1185   }
1186
1187   SmallVector<TemplateArgument, 8> TemplArgs;
1188   Reader.ReadTemplateArgumentList(TemplArgs, F, Record, Idx);
1189   D->TemplateArgs = TemplateArgumentList::CreateCopy(C, TemplArgs.data(), 
1190                                                      TemplArgs.size());
1191   D->PointOfInstantiation = ReadSourceLocation(Record, Idx);
1192   D->SpecializationKind = (TemplateSpecializationKind)Record[Idx++];
1193   
1194   if (D->isCanonicalDecl()) { // It's kept in the folding set.
1195     ClassTemplateDecl *CanonPattern = ReadDeclAs<ClassTemplateDecl>(Record,Idx);
1196     if (ClassTemplatePartialSpecializationDecl *Partial
1197                        = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
1198       CanonPattern->getCommonPtr()->PartialSpecializations.InsertNode(Partial);
1199     } else {
1200       CanonPattern->getCommonPtr()->Specializations.InsertNode(D);
1201     }
1202   }
1203 }
1204
1205 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
1206                                     ClassTemplatePartialSpecializationDecl *D) {
1207   VisitClassTemplateSpecializationDecl(D);
1208
1209   ASTContext &C = Reader.getContext();
1210   D->TemplateParams = Reader.ReadTemplateParameterList(F, Record, Idx);
1211
1212   unsigned NumArgs = Record[Idx++];
1213   if (NumArgs) {
1214     D->NumArgsAsWritten = NumArgs;
1215     D->ArgsAsWritten = new (C) TemplateArgumentLoc[NumArgs];
1216     for (unsigned i=0; i != NumArgs; ++i)
1217       D->ArgsAsWritten[i] = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1218   }
1219
1220   D->SequenceNumber = Record[Idx++];
1221
1222   // These are read/set from/to the first declaration.
1223   if (D->getPreviousDeclaration() == 0) {
1224     D->InstantiatedFromMember.setPointer(
1225       ReadDeclAs<ClassTemplatePartialSpecializationDecl>(Record, Idx));
1226     D->InstantiatedFromMember.setInt(Record[Idx++]);
1227   }
1228 }
1229
1230 void ASTDeclReader::VisitClassScopeFunctionSpecializationDecl(
1231                                     ClassScopeFunctionSpecializationDecl *D) {
1232   VisitDecl(D);
1233   D->Specialization = ReadDeclAs<CXXMethodDecl>(Record, Idx);
1234 }
1235
1236 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1237   VisitRedeclarableTemplateDecl(D);
1238
1239   if (D->getPreviousDeclaration() == 0) {
1240     // This FunctionTemplateDecl owns a CommonPtr; read it.
1241
1242     // Read the function specialization declarations.
1243     // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1244     // when reading the specialized FunctionDecl.
1245     unsigned NumSpecs = Record[Idx++];
1246     while (NumSpecs--)
1247       (void)ReadDecl(Record, Idx);
1248   }
1249 }
1250
1251 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1252   VisitTypeDecl(D);
1253
1254   D->setDeclaredWithTypename(Record[Idx++]);
1255
1256   bool Inherited = Record[Idx++];
1257   TypeSourceInfo *DefArg = GetTypeSourceInfo(Record, Idx);
1258   D->setDefaultArgument(DefArg, Inherited);
1259 }
1260
1261 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1262   VisitDeclaratorDecl(D);
1263   // TemplateParmPosition.
1264   D->setDepth(Record[Idx++]);
1265   D->setPosition(Record[Idx++]);
1266   if (D->isExpandedParameterPack()) {
1267     void **Data = reinterpret_cast<void **>(D + 1);
1268     for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1269       Data[2*I] = Reader.readType(F, Record, Idx).getAsOpaquePtr();
1270       Data[2*I + 1] = GetTypeSourceInfo(Record, Idx);
1271     }
1272   } else {
1273     // Rest of NonTypeTemplateParmDecl.
1274     D->ParameterPack = Record[Idx++];
1275     if (Record[Idx++]) {
1276       Expr *DefArg = Reader.ReadExpr(F);
1277       bool Inherited = Record[Idx++];
1278       D->setDefaultArgument(DefArg, Inherited);
1279    }
1280   }
1281 }
1282
1283 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1284   VisitTemplateDecl(D);
1285   // TemplateParmPosition.
1286   D->setDepth(Record[Idx++]);
1287   D->setPosition(Record[Idx++]);
1288   // Rest of TemplateTemplateParmDecl.
1289   TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(F, Record, Idx);
1290   bool IsInherited = Record[Idx++];
1291   D->setDefaultArgument(Arg, IsInherited);
1292   D->ParameterPack = Record[Idx++];
1293 }
1294
1295 void ASTDeclReader::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1296   VisitRedeclarableTemplateDecl(D);
1297 }
1298
1299 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
1300   VisitDecl(D);
1301   D->AssertExpr = Reader.ReadExpr(F);
1302   D->Message = cast<StringLiteral>(Reader.ReadExpr(F));
1303   D->RParenLoc = ReadSourceLocation(Record, Idx);
1304 }
1305
1306 std::pair<uint64_t, uint64_t>
1307 ASTDeclReader::VisitDeclContext(DeclContext *DC) {
1308   uint64_t LexicalOffset = Record[Idx++];
1309   uint64_t VisibleOffset = Record[Idx++];
1310   return std::make_pair(LexicalOffset, VisibleOffset);
1311 }
1312
1313 template <typename T>
1314 void ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1315   enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1316   RedeclKind Kind = (RedeclKind)Record[Idx++];
1317   switch (Kind) {
1318   default:
1319     llvm_unreachable("Out of sync with ASTDeclWriter::VisitRedeclarable or"
1320                      " messed up reading");
1321   case NoRedeclaration:
1322     break;
1323   case PointsToPrevious: {
1324     DeclID PreviousDeclID = ReadDeclID(Record, Idx);
1325     DeclID FirstDeclID = ReadDeclID(Record, Idx);
1326     // We delay loading of the redeclaration chain to avoid deeply nested calls.
1327     // We temporarily set the first (canonical) declaration as the previous one
1328     // which is the one that matters and mark the real previous DeclID to be
1329     // loaded & attached later on.
1330     D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1331                                 cast_or_null<T>(Reader.GetDecl(FirstDeclID)));
1332     if (PreviousDeclID != FirstDeclID)
1333       Reader.PendingPreviousDecls.push_back(std::make_pair(static_cast<T*>(D),
1334                                                            PreviousDeclID));
1335     break;
1336   }
1337   case PointsToLatest:
1338     D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1339                                                    ReadDeclAs<T>(Record, Idx));
1340     break;
1341   }
1342
1343   assert(!(Kind == PointsToPrevious &&
1344            Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1345                Reader.FirstLatestDeclIDs.end()) &&
1346          "This decl is not first, it should not be in the map");
1347   if (Kind == PointsToPrevious)
1348     return;
1349
1350   // This decl is a first one and the latest declaration that it points to is in
1351   // the same AST file. However, if this actually needs to point to a
1352   // redeclaration in another AST file, we need to update it by checking the
1353   // FirstLatestDeclIDs map which tracks this kind of decls.
1354   assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1355          "Invalid ThisDeclID ?");
1356   ASTReader::FirstLatestDeclIDMap::iterator I
1357       = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1358   if (I != Reader.FirstLatestDeclIDs.end()) {
1359     Decl *NewLatest = Reader.GetDecl(I->second);
1360     D->RedeclLink
1361         = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1362   }
1363 }
1364
1365 //===----------------------------------------------------------------------===//
1366 // Attribute Reading
1367 //===----------------------------------------------------------------------===//
1368
1369 /// \brief Reads attributes from the current stream position.
1370 void ASTReader::ReadAttributes(Module &F, AttrVec &Attrs,
1371                                const RecordData &Record, unsigned &Idx) {
1372   for (unsigned i = 0, e = Record[Idx++]; i != e; ++i) {
1373     Attr *New = 0;
1374     attr::Kind Kind = (attr::Kind)Record[Idx++];
1375     SourceRange Range = ReadSourceRange(F, Record, Idx);
1376
1377 #include "clang/Serialization/AttrPCHRead.inc"
1378
1379     assert(New && "Unable to decode attribute?");
1380     Attrs.push_back(New);
1381   }
1382 }
1383
1384 //===----------------------------------------------------------------------===//
1385 // ASTReader Implementation
1386 //===----------------------------------------------------------------------===//
1387
1388 /// \brief Note that we have loaded the declaration with the given
1389 /// Index.
1390 ///
1391 /// This routine notes that this declaration has already been loaded,
1392 /// so that future GetDecl calls will return this declaration rather
1393 /// than trying to load a new declaration.
1394 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
1395   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1396   DeclsLoaded[Index] = D;
1397 }
1398
1399
1400 /// \brief Determine whether the consumer will be interested in seeing
1401 /// this declaration (via HandleTopLevelDecl).
1402 ///
1403 /// This routine should return true for anything that might affect
1404 /// code generation, e.g., inline function definitions, Objective-C
1405 /// declarations with metadata, etc.
1406 static bool isConsumerInterestedIn(Decl *D) {
1407   // An ObjCMethodDecl is never considered as "interesting" because its
1408   // implementation container always is.
1409
1410   if (isa<FileScopeAsmDecl>(D) || 
1411       isa<ObjCProtocolDecl>(D) || 
1412       isa<ObjCImplDecl>(D))
1413     return true;
1414   if (VarDecl *Var = dyn_cast<VarDecl>(D))
1415     return Var->isFileVarDecl() &&
1416            Var->isThisDeclarationADefinition() == VarDecl::Definition;
1417   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1418     return Func->doesThisDeclarationHaveABody();
1419   
1420   return false;
1421 }
1422
1423 /// \brief Get the correct cursor and offset for loading a declaration.
1424 ASTReader::RecordLocation
1425 ASTReader::DeclCursorForID(DeclID ID) {
1426   // See if there's an override.
1427   DeclReplacementMap::iterator It = ReplacedDecls.find(ID);
1428   if (It != ReplacedDecls.end())
1429     return RecordLocation(It->second.first, It->second.second);
1430
1431   GlobalDeclMapType::iterator I = GlobalDeclMap.find(ID);
1432   assert(I != GlobalDeclMap.end() && "Corrupted global declaration map");
1433   Module *M = I->second;
1434   return RecordLocation(M, 
1435            M->DeclOffsets[ID - M->BaseDeclID - NUM_PREDEF_DECL_IDS]);
1436 }
1437
1438 ASTReader::RecordLocation ASTReader::getLocalBitOffset(uint64_t GlobalOffset) {
1439   ContinuousRangeMap<uint64_t, Module*, 4>::iterator I
1440     = GlobalBitOffsetsMap.find(GlobalOffset);
1441
1442   assert(I != GlobalBitOffsetsMap.end() && "Corrupted global bit offsets map");
1443   return RecordLocation(I->second, GlobalOffset - I->second->GlobalBitOffset);
1444 }
1445
1446 uint64_t ASTReader::getGlobalBitOffset(Module &M, uint32_t LocalOffset) {
1447   return LocalOffset + M.GlobalBitOffset;
1448 }
1449
1450 void ASTDeclReader::attachPreviousDecl(Decl *D, Decl *previous) {
1451   assert(D && previous);
1452   if (TagDecl *TD = dyn_cast<TagDecl>(D)) {
1453     TD->RedeclLink.setPointer(cast<TagDecl>(previous));
1454   } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1455     FD->RedeclLink.setPointer(cast<FunctionDecl>(previous));
1456   } else if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
1457     VD->RedeclLink.setPointer(cast<VarDecl>(previous));
1458   } else {
1459     RedeclarableTemplateDecl *TD = cast<RedeclarableTemplateDecl>(D);
1460     TD->CommonOrPrev = cast<RedeclarableTemplateDecl>(previous);
1461   }
1462 }
1463
1464 void ASTReader::loadAndAttachPreviousDecl(Decl *D, serialization::DeclID ID) {
1465   Decl *previous = GetDecl(ID);
1466   ASTDeclReader::attachPreviousDecl(D, previous);
1467 }
1468
1469 /// \brief Read the declaration at the given offset from the AST file.
1470 Decl *ASTReader::ReadDeclRecord(DeclID ID) {
1471   unsigned Index = ID - NUM_PREDEF_DECL_IDS;
1472   RecordLocation Loc = DeclCursorForID(ID);
1473   llvm::BitstreamCursor &DeclsCursor = Loc.F->DeclsCursor;
1474   // Keep track of where we are in the stream, then jump back there
1475   // after reading this declaration.
1476   SavedStreamPosition SavedPosition(DeclsCursor);
1477
1478   ReadingKindTracker ReadingKind(Read_Decl, *this);
1479
1480   // Note that we are loading a declaration record.
1481   Deserializing ADecl(this);
1482
1483   DeclsCursor.JumpToBit(Loc.Offset);
1484   RecordData Record;
1485   unsigned Code = DeclsCursor.ReadCode();
1486   unsigned Idx = 0;
1487   ASTDeclReader Reader(*this, *Loc.F, DeclsCursor, ID, Record, Idx);
1488
1489   Decl *D = 0;
1490   switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
1491   case DECL_CONTEXT_LEXICAL:
1492   case DECL_CONTEXT_VISIBLE:
1493     llvm_unreachable("Record cannot be de-serialized with ReadDeclRecord");
1494   case DECL_TYPEDEF:
1495     D = TypedefDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1496                             0, 0);
1497     break;
1498   case DECL_TYPEALIAS:
1499     D = TypeAliasDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1500                               0, 0);
1501     break;
1502   case DECL_ENUM:
1503     D = EnumDecl::Create(Context, Decl::EmptyShell());
1504     break;
1505   case DECL_RECORD:
1506     D = RecordDecl::Create(Context, Decl::EmptyShell());
1507     break;
1508   case DECL_ENUM_CONSTANT:
1509     D = EnumConstantDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1510                                  0, llvm::APSInt());
1511     break;
1512   case DECL_FUNCTION:
1513     D = FunctionDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1514                              DeclarationName(), QualType(), 0);
1515     break;
1516   case DECL_LINKAGE_SPEC:
1517     D = LinkageSpecDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1518                                 (LinkageSpecDecl::LanguageIDs)0,
1519                                 SourceLocation());
1520     break;
1521   case DECL_LABEL:
1522     D = LabelDecl::Create(Context, 0, SourceLocation(), 0);
1523     break;
1524   case DECL_NAMESPACE:
1525     D = NamespaceDecl::Create(Context, 0, SourceLocation(),
1526                               SourceLocation(), 0);
1527     break;
1528   case DECL_NAMESPACE_ALIAS:
1529     D = NamespaceAliasDecl::Create(Context, 0, SourceLocation(),
1530                                    SourceLocation(), 0, 
1531                                    NestedNameSpecifierLoc(),
1532                                    SourceLocation(), 0);
1533     break;
1534   case DECL_USING:
1535     D = UsingDecl::Create(Context, 0, SourceLocation(),
1536                           NestedNameSpecifierLoc(), DeclarationNameInfo(), 
1537                           false);
1538     break;
1539   case DECL_USING_SHADOW:
1540     D = UsingShadowDecl::Create(Context, 0, SourceLocation(), 0, 0);
1541     break;
1542   case DECL_USING_DIRECTIVE:
1543     D = UsingDirectiveDecl::Create(Context, 0, SourceLocation(),
1544                                    SourceLocation(), NestedNameSpecifierLoc(),
1545                                    SourceLocation(), 0, 0);
1546     break;
1547   case DECL_UNRESOLVED_USING_VALUE:
1548     D = UnresolvedUsingValueDecl::Create(Context, 0, SourceLocation(),
1549                                          NestedNameSpecifierLoc(), 
1550                                          DeclarationNameInfo());
1551     break;
1552   case DECL_UNRESOLVED_USING_TYPENAME:
1553     D = UnresolvedUsingTypenameDecl::Create(Context, 0, SourceLocation(),
1554                                             SourceLocation(), 
1555                                             NestedNameSpecifierLoc(),
1556                                             SourceLocation(),
1557                                             DeclarationName());
1558     break;
1559   case DECL_CXX_RECORD:
1560     D = CXXRecordDecl::Create(Context, Decl::EmptyShell());
1561     break;
1562   case DECL_CXX_METHOD:
1563     D = CXXMethodDecl::Create(Context, 0, SourceLocation(),
1564                               DeclarationNameInfo(), QualType(), 0,
1565                               false, SC_None, false, false, SourceLocation());
1566     break;
1567   case DECL_CXX_CONSTRUCTOR:
1568     D = CXXConstructorDecl::Create(Context, Decl::EmptyShell());
1569     break;
1570   case DECL_CXX_DESTRUCTOR:
1571     D = CXXDestructorDecl::Create(Context, Decl::EmptyShell());
1572     break;
1573   case DECL_CXX_CONVERSION:
1574     D = CXXConversionDecl::Create(Context, Decl::EmptyShell());
1575     break;
1576   case DECL_ACCESS_SPEC:
1577     D = AccessSpecDecl::Create(Context, Decl::EmptyShell());
1578     break;
1579   case DECL_FRIEND:
1580     D = FriendDecl::Create(Context, Decl::EmptyShell());
1581     break;
1582   case DECL_FRIEND_TEMPLATE:
1583     D = FriendTemplateDecl::Create(Context, Decl::EmptyShell());
1584     break;
1585   case DECL_CLASS_TEMPLATE:
1586     D = ClassTemplateDecl::Create(Context, Decl::EmptyShell());
1587     break;
1588   case DECL_CLASS_TEMPLATE_SPECIALIZATION:
1589     D = ClassTemplateSpecializationDecl::Create(Context, Decl::EmptyShell());
1590     break;
1591   case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
1592     D = ClassTemplatePartialSpecializationDecl::Create(Context,
1593                                                        Decl::EmptyShell());
1594     break;
1595   case DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION:
1596     D = ClassScopeFunctionSpecializationDecl::Create(Context,
1597                                                      Decl::EmptyShell());
1598     break;
1599   case DECL_FUNCTION_TEMPLATE:
1600       D = FunctionTemplateDecl::Create(Context, Decl::EmptyShell());
1601     break;
1602   case DECL_TEMPLATE_TYPE_PARM:
1603     D = TemplateTypeParmDecl::Create(Context, Decl::EmptyShell());
1604     break;
1605   case DECL_NON_TYPE_TEMPLATE_PARM:
1606     D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1607                                         SourceLocation(), 0, 0, 0, QualType(),
1608                                         false, 0);
1609     break;
1610   case DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK:
1611     D = NonTypeTemplateParmDecl::Create(Context, 0, SourceLocation(),
1612                                         SourceLocation(), 0, 0, 0, QualType(),
1613                                         0, 0, Record[Idx++], 0);
1614     break;
1615   case DECL_TEMPLATE_TEMPLATE_PARM:
1616     D = TemplateTemplateParmDecl::Create(Context, 0, SourceLocation(), 0, 0,
1617                                          false, 0, 0);
1618     break;
1619   case DECL_TYPE_ALIAS_TEMPLATE:
1620     D = TypeAliasTemplateDecl::Create(Context, Decl::EmptyShell());
1621     break;
1622   case DECL_STATIC_ASSERT:
1623     D = StaticAssertDecl::Create(Context, 0, SourceLocation(), 0, 0,
1624                                  SourceLocation());
1625     break;
1626
1627   case DECL_OBJC_METHOD:
1628     D = ObjCMethodDecl::Create(Context, SourceLocation(), SourceLocation(),
1629                                Selector(), QualType(), 0, 0);
1630     break;
1631   case DECL_OBJC_INTERFACE:
1632     D = ObjCInterfaceDecl::Create(Context, 0, SourceLocation(), 0);
1633     break;
1634   case DECL_OBJC_IVAR:
1635     D = ObjCIvarDecl::Create(Context, 0, SourceLocation(), SourceLocation(),
1636                              0, QualType(), 0, ObjCIvarDecl::None);
1637     break;
1638   case DECL_OBJC_PROTOCOL:
1639     D = ObjCProtocolDecl::Create(Context, 0, 0, SourceLocation(),
1640                                  SourceLocation());
1641     break;
1642   case DECL_OBJC_AT_DEFS_FIELD:
1643     D = ObjCAtDefsFieldDecl::Create(Context, 0, SourceLocation(),
1644                                     SourceLocation(), 0, QualType(), 0);
1645     break;
1646   case DECL_OBJC_CLASS:
1647     D = ObjCClassDecl::Create(Context, 0, SourceLocation());
1648     break;
1649   case DECL_OBJC_FORWARD_PROTOCOL:
1650     D = ObjCForwardProtocolDecl::Create(Context, 0, SourceLocation());
1651     break;
1652   case DECL_OBJC_CATEGORY:
1653     D = ObjCCategoryDecl::Create(Context, Decl::EmptyShell());
1654     break;
1655   case DECL_OBJC_CATEGORY_IMPL:
1656     D = ObjCCategoryImplDecl::Create(Context, 0, 0, 0, SourceLocation(),
1657                                      SourceLocation());
1658     break;
1659   case DECL_OBJC_IMPLEMENTATION:
1660     D = ObjCImplementationDecl::Create(Context, 0, 0, 0, SourceLocation(),
1661                                        SourceLocation());
1662     break;
1663   case DECL_OBJC_COMPATIBLE_ALIAS:
1664     D = ObjCCompatibleAliasDecl::Create(Context, 0, SourceLocation(), 0, 0);
1665     break;
1666   case DECL_OBJC_PROPERTY:
1667     D = ObjCPropertyDecl::Create(Context, 0, SourceLocation(), 0, SourceLocation(),
1668                                  0);
1669     break;
1670   case DECL_OBJC_PROPERTY_IMPL:
1671     D = ObjCPropertyImplDecl::Create(Context, 0, SourceLocation(),
1672                                      SourceLocation(), 0,
1673                                      ObjCPropertyImplDecl::Dynamic, 0,
1674                                      SourceLocation());
1675     break;
1676   case DECL_FIELD:
1677     D = FieldDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1678                           QualType(), 0, 0, false, false);
1679     break;
1680   case DECL_INDIRECTFIELD:
1681     D = IndirectFieldDecl::Create(Context, 0, SourceLocation(), 0, QualType(),
1682                                   0, 0);
1683     break;
1684   case DECL_VAR:
1685     D = VarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1686                         QualType(), 0, SC_None, SC_None);
1687     break;
1688
1689   case DECL_IMPLICIT_PARAM:
1690     D = ImplicitParamDecl::Create(Context, 0, SourceLocation(), 0, QualType());
1691     break;
1692
1693   case DECL_PARM_VAR:
1694     D = ParmVarDecl::Create(Context, 0, SourceLocation(), SourceLocation(), 0,
1695                             QualType(), 0, SC_None, SC_None, 0);
1696     break;
1697   case DECL_FILE_SCOPE_ASM:
1698     D = FileScopeAsmDecl::Create(Context, 0, 0, SourceLocation(),
1699                                  SourceLocation());
1700     break;
1701   case DECL_BLOCK:
1702     D = BlockDecl::Create(Context, 0, SourceLocation());
1703     break;
1704   case DECL_CXX_BASE_SPECIFIERS:
1705     Error("attempt to read a C++ base-specifier record as a declaration");
1706     return 0;
1707   }
1708
1709   assert(D && "Unknown declaration reading AST file");
1710   LoadedDecl(Index, D);
1711   Reader.Visit(D);
1712
1713   // If this declaration is also a declaration context, get the
1714   // offsets for its tables of lexical and visible declarations.
1715   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1716     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1717     if (Offsets.first || Offsets.second) {
1718       if (Offsets.first != 0)
1719         DC->setHasExternalLexicalStorage(true);
1720       if (Offsets.second != 0)
1721         DC->setHasExternalVisibleStorage(true);
1722       if (ReadDeclContextStorage(*Loc.F, DeclsCursor, Offsets, 
1723                                  Loc.F->DeclContextInfos[DC]))
1724         return 0;
1725     }
1726
1727     // Now add the pending visible updates for this decl context, if it has any.
1728     DeclContextVisibleUpdatesPending::iterator I =
1729         PendingVisibleUpdates.find(ID);
1730     if (I != PendingVisibleUpdates.end()) {
1731       // There are updates. This means the context has external visible
1732       // storage, even if the original stored version didn't.
1733       DC->setHasExternalVisibleStorage(true);
1734       DeclContextVisibleUpdates &U = I->second;
1735       for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end();
1736            UI != UE; ++UI) {
1737         UI->second->DeclContextInfos[DC].NameLookupTableData = UI->first;
1738       }
1739       PendingVisibleUpdates.erase(I);
1740     }
1741   }
1742   assert(Idx == Record.size());
1743
1744   // Load any relevant update records.
1745   loadDeclUpdateRecords(ID, D);
1746   
1747   if (ObjCChainedCategoriesInterfaces.count(ID))
1748     loadObjCChainedCategories(ID, cast<ObjCInterfaceDecl>(D));
1749   
1750   // If we have deserialized a declaration that has a definition the
1751   // AST consumer might need to know about, queue it.
1752   // We don't pass it to the consumer immediately because we may be in recursive
1753   // loading, and some declarations may still be initializing.
1754   if (isConsumerInterestedIn(D))
1755       InterestingDecls.push_back(D);
1756   
1757   return D;
1758 }
1759
1760 void ASTReader::loadDeclUpdateRecords(serialization::DeclID ID, Decl *D) {
1761   // The declaration may have been modified by files later in the chain.
1762   // If this is the case, read the record containing the updates from each file
1763   // and pass it to ASTDeclReader to make the modifications.
1764   DeclUpdateOffsetsMap::iterator UpdI = DeclUpdateOffsets.find(ID);
1765   if (UpdI != DeclUpdateOffsets.end()) {
1766     FileOffsetsTy &UpdateOffsets = UpdI->second;
1767     for (FileOffsetsTy::iterator
1768          I = UpdateOffsets.begin(), E = UpdateOffsets.end(); I != E; ++I) {
1769       Module *F = I->first;
1770       uint64_t Offset = I->second;
1771       llvm::BitstreamCursor &Cursor = F->DeclsCursor;
1772       SavedStreamPosition SavedPosition(Cursor);
1773       Cursor.JumpToBit(Offset);
1774       RecordData Record;
1775       unsigned Code = Cursor.ReadCode();
1776       unsigned RecCode = Cursor.ReadRecord(Code, Record);
1777       (void)RecCode;
1778       assert(RecCode == DECL_UPDATES && "Expected DECL_UPDATES record!");
1779       
1780       unsigned Idx = 0;
1781       ASTDeclReader Reader(*this, *F, Cursor, ID, Record, Idx);
1782       Reader.UpdateDecl(D, *F, Record);
1783     }
1784   }
1785 }
1786
1787 namespace {
1788   /// \brief Given an ObjC interface, goes through the modules and links to the
1789   /// interface all the categories for it.
1790   class ObjCChainedCategoriesVisitor {
1791     ASTReader &Reader;
1792     serialization::GlobalDeclID InterfaceID;
1793     ObjCInterfaceDecl *Interface;
1794     ObjCCategoryDecl *GlobHeadCat, *GlobTailCat;
1795     llvm::DenseMap<DeclarationName, ObjCCategoryDecl *> NameCategoryMap;
1796
1797   public:
1798     ObjCChainedCategoriesVisitor(ASTReader &Reader,
1799                                  serialization::GlobalDeclID InterfaceID,
1800                                  ObjCInterfaceDecl *Interface)
1801       : Reader(Reader), InterfaceID(InterfaceID), Interface(Interface),
1802         GlobHeadCat(0), GlobTailCat(0) { }
1803
1804     static bool visit(Module &M, void *UserData) {
1805       return static_cast<ObjCChainedCategoriesVisitor *>(UserData)->visit(M);
1806     }
1807
1808     bool visit(Module &M) {
1809       if (Reader.isDeclIDFromModule(InterfaceID, M))
1810         return true; // We reached the module where the interface originated
1811                     // from. Stop traversing the imported modules.
1812
1813       Module::ChainedObjCCategoriesMap::iterator
1814         I = M.ChainedObjCCategories.find(InterfaceID);
1815       if (I == M.ChainedObjCCategories.end())
1816         return false;
1817
1818       ObjCCategoryDecl *
1819         HeadCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.first);
1820       ObjCCategoryDecl *
1821         TailCat = Reader.GetLocalDeclAs<ObjCCategoryDecl>(M, I->second.second);
1822
1823       addCategories(HeadCat, TailCat);
1824       return false;
1825     }
1826
1827     void addCategories(ObjCCategoryDecl *HeadCat,
1828                        ObjCCategoryDecl *TailCat = 0) {
1829       if (!HeadCat) {
1830         assert(!TailCat);
1831         return;
1832       }
1833
1834       if (!TailCat) {
1835         TailCat = HeadCat;
1836         while (TailCat->getNextClassCategory())
1837           TailCat = TailCat->getNextClassCategory();
1838       }
1839
1840       if (!GlobHeadCat) {
1841         GlobHeadCat = HeadCat;
1842         GlobTailCat = TailCat;
1843       } else {
1844         ASTDeclReader::setNextObjCCategory(GlobTailCat, HeadCat);
1845         GlobTailCat = TailCat;
1846       }
1847
1848       llvm::DenseSet<DeclarationName> Checked;
1849       for (ObjCCategoryDecl *Cat = HeadCat,
1850                             *CatEnd = TailCat->getNextClassCategory();
1851              Cat != CatEnd; Cat = Cat->getNextClassCategory()) {
1852         if (Checked.count(Cat->getDeclName()))
1853           continue;
1854         Checked.insert(Cat->getDeclName());
1855         checkForDuplicate(Cat);
1856       }
1857     }
1858
1859     /// \brief Warns for duplicate categories that come from different modules.
1860     void checkForDuplicate(ObjCCategoryDecl *Cat) {
1861       DeclarationName Name = Cat->getDeclName();
1862       // Find the top category with the same name. We do not want to warn for
1863       // duplicates along the established chain because there were already
1864       // warnings for them when the module was created. We only want to warn for
1865       // duplicates between non-dependent modules:
1866       //
1867       //   MT     //
1868       //  /  \    //
1869       // ML  MR   //
1870       //
1871       // We want to warn for duplicates between ML and MR,not between ML and MT.
1872       //
1873       // FIXME: We should not warn for duplicates in diamond:
1874       //
1875       //   MT     //
1876       //  /  \    //
1877       // ML  MR   //
1878       //  \  /    //
1879       //   MB     //
1880       //
1881       // If there are duplicates in ML/MR, there will be warning when creating
1882       // MB *and* when importing MB. We should not warn when importing.
1883       for (ObjCCategoryDecl *Next = Cat->getNextClassCategory(); Next;
1884              Next = Next->getNextClassCategory()) {
1885         if (Next->getDeclName() == Name)
1886           Cat = Next;
1887       }
1888
1889       ObjCCategoryDecl *&PrevCat = NameCategoryMap[Name];
1890       if (!PrevCat)
1891         PrevCat = Cat;
1892
1893       if (PrevCat != Cat) {
1894         Reader.Diag(Cat->getLocation(), diag::warn_dup_category_def)
1895           << Interface->getDeclName() << Name;
1896         Reader.Diag(PrevCat->getLocation(), diag::note_previous_definition);
1897       }
1898     }
1899
1900     ObjCCategoryDecl *getHeadCategory() const { return GlobHeadCat; }
1901   };
1902 }
1903
1904 void ASTReader::loadObjCChainedCategories(serialization::GlobalDeclID ID,
1905                                           ObjCInterfaceDecl *D) {
1906   ObjCChainedCategoriesVisitor Visitor(*this, ID, D);
1907   ModuleMgr.visit(ObjCChainedCategoriesVisitor::visit, &Visitor);
1908   // Also add the categories that the interface already links to.
1909   Visitor.addCategories(D->getCategoryList());
1910   D->setCategoryList(Visitor.getHeadCategory());
1911 }
1912
1913 void ASTDeclReader::UpdateDecl(Decl *D, Module &Module,
1914                                const RecordData &Record) {
1915   unsigned Idx = 0;
1916   while (Idx < Record.size()) {
1917     switch ((DeclUpdateKind)Record[Idx++]) {
1918     case UPD_CXX_SET_DEFINITIONDATA: {
1919       CXXRecordDecl *RD = cast<CXXRecordDecl>(D);
1920       CXXRecordDecl *DefinitionDecl
1921         = Reader.ReadDeclAs<CXXRecordDecl>(Module, Record, Idx);
1922       assert(!RD->DefinitionData && "DefinitionData is already set!");
1923       InitializeCXXDefinitionData(RD, DefinitionDecl, Record, Idx);
1924       break;
1925     }
1926
1927     case UPD_CXX_ADDED_IMPLICIT_MEMBER:
1928       cast<CXXRecordDecl>(D)->addedMember(Reader.ReadDecl(Module, Record, Idx));
1929       break;
1930
1931     case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
1932       // It will be added to the template's specializations set when loaded.
1933       (void)Reader.ReadDecl(Module, Record, Idx);
1934       break;
1935
1936     case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE: {
1937       NamespaceDecl *Anon
1938         = Reader.ReadDeclAs<NamespaceDecl>(Module, Record, Idx);
1939       // Guard against these being loaded out of original order. Don't use
1940       // getNextNamespace(), since it tries to access the context and can't in
1941       // the middle of deserialization.
1942       if (!Anon->NextNamespace) {
1943         if (TranslationUnitDecl *TU = dyn_cast<TranslationUnitDecl>(D))
1944           TU->setAnonymousNamespace(Anon);
1945         else
1946           cast<NamespaceDecl>(D)->OrigOrAnonNamespace.setPointer(Anon);
1947       }
1948       break;
1949     }
1950
1951     case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
1952       cast<VarDecl>(D)->getMemberSpecializationInfo()->setPointOfInstantiation(
1953           Reader.ReadSourceLocation(Module, Record, Idx));
1954       break;
1955     }
1956   }
1957 }