]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Serialization/ASTReaderDecl.cpp
Merge ACPICA 20110112. Switch to BSD/GPLv2 dual license[1].
[FreeBSD/FreeBSD.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 "clang/Serialization/ASTReader.h"
16 #include "clang/AST/ASTConsumer.h"
17 #include "clang/AST/ASTContext.h"
18 #include "clang/AST/DeclVisitor.h"
19 #include "clang/AST/DeclGroup.h"
20 #include "clang/AST/DeclCXX.h"
21 #include "clang/AST/DeclTemplate.h"
22 #include "clang/AST/Expr.h"
23 using namespace clang;
24 using namespace clang::serialization;
25
26 //===----------------------------------------------------------------------===//
27 // Declaration deserialization
28 //===----------------------------------------------------------------------===//
29
30 namespace clang {
31   class ASTDeclReader : public DeclVisitor<ASTDeclReader, void> {
32     ASTReader &Reader;
33     llvm::BitstreamCursor &Cursor;
34     const DeclID ThisDeclID;
35     const ASTReader::RecordData &Record;
36     unsigned &Idx;
37     TypeID TypeIDForTypeDecl;
38
39     uint64_t GetCurrentCursorOffset();
40
41   public:
42     ASTDeclReader(ASTReader &Reader, llvm::BitstreamCursor &Cursor,
43                   DeclID thisDeclID, const ASTReader::RecordData &Record,
44                   unsigned &Idx)
45       : Reader(Reader), Cursor(Cursor), ThisDeclID(thisDeclID), Record(Record),
46         Idx(Idx), TypeIDForTypeDecl(0) { }
47
48     void Visit(Decl *D);
49
50     void VisitDecl(Decl *D);
51     void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52     void VisitNamedDecl(NamedDecl *ND);
53     void VisitNamespaceDecl(NamespaceDecl *D);
54     void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
55     void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
56     void VisitTypeDecl(TypeDecl *TD);
57     void VisitTypedefDecl(TypedefDecl *TD);
58     void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
59     void VisitTagDecl(TagDecl *TD);
60     void VisitEnumDecl(EnumDecl *ED);
61     void VisitRecordDecl(RecordDecl *RD);
62     void VisitCXXRecordDecl(CXXRecordDecl *D);
63     void VisitClassTemplateSpecializationDecl(
64                                             ClassTemplateSpecializationDecl *D);
65     void VisitClassTemplatePartialSpecializationDecl(
66                                      ClassTemplatePartialSpecializationDecl *D);
67     void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
68     void VisitValueDecl(ValueDecl *VD);
69     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
70     void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
71     void VisitDeclaratorDecl(DeclaratorDecl *DD);
72     void VisitFunctionDecl(FunctionDecl *FD);
73     void VisitCXXMethodDecl(CXXMethodDecl *D);
74     void VisitCXXConstructorDecl(CXXConstructorDecl *D);
75     void VisitCXXDestructorDecl(CXXDestructorDecl *D);
76     void VisitCXXConversionDecl(CXXConversionDecl *D);
77     void VisitFieldDecl(FieldDecl *FD);
78     void VisitVarDecl(VarDecl *VD);
79     void VisitImplicitParamDecl(ImplicitParamDecl *PD);
80     void VisitParmVarDecl(ParmVarDecl *PD);
81     void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
82     void VisitTemplateDecl(TemplateDecl *D);
83     void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
84     void VisitClassTemplateDecl(ClassTemplateDecl *D);
85     void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
86     void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
87     void VisitUsingDecl(UsingDecl *D);
88     void VisitUsingShadowDecl(UsingShadowDecl *D);
89     void VisitLinkageSpecDecl(LinkageSpecDecl *D);
90     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
91     void VisitAccessSpecDecl(AccessSpecDecl *D);
92     void VisitFriendDecl(FriendDecl *D);
93     void VisitFriendTemplateDecl(FriendTemplateDecl *D);
94     void VisitStaticAssertDecl(StaticAssertDecl *D);
95     void VisitBlockDecl(BlockDecl *BD);
96
97     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
98     template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
99
100     // FIXME: Reorder according to DeclNodes.td?
101     void VisitObjCMethodDecl(ObjCMethodDecl *D);
102     void VisitObjCContainerDecl(ObjCContainerDecl *D);
103     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
104     void VisitObjCIvarDecl(ObjCIvarDecl *D);
105     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
106     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
107     void VisitObjCClassDecl(ObjCClassDecl *D);
108     void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
109     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
110     void VisitObjCImplDecl(ObjCImplDecl *D);
111     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
112     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
113     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
114     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
115     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
116   };
117 }
118
119 uint64_t ASTDeclReader::GetCurrentCursorOffset() {
120   uint64_t Off = 0;
121   for (unsigned I = 0, N = Reader.Chain.size(); I != N; ++I) {
122     ASTReader::PerFileData &F = *Reader.Chain[N - I - 1];
123     if (&Cursor == &F.DeclsCursor) {
124       Off += F.DeclsCursor.GetCurrentBitNo();
125       break;
126     }
127     Off += F.SizeInBits;
128   }
129   return Off;
130 }
131
132 void ASTDeclReader::Visit(Decl *D) {
133   DeclVisitor<ASTDeclReader, void>::Visit(D);
134
135   if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
136     // if we have a fully initialized TypeDecl, we can safely read its type now.
137     TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr());
138   } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
139     // FunctionDecl's body was written last after all other Stmts/Exprs.
140     if (Record[Idx++])
141       FD->setLazyBody(GetCurrentCursorOffset());
142   }
143 }
144
145 void ASTDeclReader::VisitDecl(Decl *D) {
146   D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
147   D->setLexicalDeclContext(
148                      cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
149   D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
150   D->setInvalidDecl(Record[Idx++]);
151   if (Record[Idx++]) {
152     AttrVec Attrs;
153     Reader.ReadAttributes(Cursor, Attrs);
154     D->setAttrs(Attrs);
155   }
156   D->setImplicit(Record[Idx++]);
157   D->setUsed(Record[Idx++]);
158   D->setAccess((AccessSpecifier)Record[Idx++]);
159   D->setPCHLevel(Record[Idx++] + 1);
160 }
161
162 void ASTDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
163   VisitDecl(TU);
164   TU->setAnonymousNamespace(
165                     cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
166 }
167
168 void ASTDeclReader::VisitNamedDecl(NamedDecl *ND) {
169   VisitDecl(ND);
170   ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
171 }
172
173 void ASTDeclReader::VisitTypeDecl(TypeDecl *TD) {
174   VisitNamedDecl(TD);
175   // Delay type reading until after we have fully initialized the decl.
176   TypeIDForTypeDecl = Record[Idx++];
177 }
178
179 void ASTDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
180   VisitTypeDecl(TD);
181   TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
182 }
183
184 void ASTDeclReader::VisitTagDecl(TagDecl *TD) {
185   VisitTypeDecl(TD);
186   TD->IdentifierNamespace = Record[Idx++];
187   VisitRedeclarable(TD);
188   TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
189   TD->setDefinition(Record[Idx++]);
190   TD->setEmbeddedInDeclarator(Record[Idx++]);
191   TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
192   TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
193   // FIXME: maybe read optional qualifier and its range.
194   TD->setTypedefForAnonDecl(
195                     cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
196 }
197
198 void ASTDeclReader::VisitEnumDecl(EnumDecl *ED) {
199   VisitTagDecl(ED);
200   ED->setIntegerType(Reader.GetType(Record[Idx++]));
201   ED->setPromotionType(Reader.GetType(Record[Idx++]));
202   ED->setNumPositiveBits(Record[Idx++]);
203   ED->setNumNegativeBits(Record[Idx++]);
204   ED->setInstantiationOfMemberEnum(
205                          cast_or_null<EnumDecl>(Reader.GetDecl(Record[Idx++])));
206 }
207
208 void ASTDeclReader::VisitRecordDecl(RecordDecl *RD) {
209   VisitTagDecl(RD);
210   RD->setHasFlexibleArrayMember(Record[Idx++]);
211   RD->setAnonymousStructOrUnion(Record[Idx++]);
212   RD->setHasObjectMember(Record[Idx++]);
213 }
214
215 void ASTDeclReader::VisitValueDecl(ValueDecl *VD) {
216   VisitNamedDecl(VD);
217   VD->setType(Reader.GetType(Record[Idx++]));
218 }
219
220 void ASTDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
221   VisitValueDecl(ECD);
222   if (Record[Idx++])
223     ECD->setInitExpr(Reader.ReadExpr(Cursor));
224   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
225 }
226
227 void ASTDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
228   VisitValueDecl(DD);
229   TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
230   if (TInfo)
231     DD->setTypeSourceInfo(TInfo);
232   // FIXME: read optional qualifier and its range.
233 }
234
235 void ASTDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
236   VisitDeclaratorDecl(FD);
237   // FIXME: read DeclarationNameLoc.
238
239   FD->IdentifierNamespace = Record[Idx++];
240   switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
241   default: assert(false && "Unhandled TemplatedKind!");
242     break;
243   case FunctionDecl::TK_NonTemplate:
244     break;
245   case FunctionDecl::TK_FunctionTemplate:
246     FD->setDescribedFunctionTemplate(
247                      cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++])));
248     break;
249   case FunctionDecl::TK_MemberSpecialization: {
250     FunctionDecl *InstFD = cast<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
251     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
252     SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
253     FD->setInstantiationOfMemberFunction(InstFD, TSK);
254     FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
255     break;
256   }
257   case FunctionDecl::TK_FunctionTemplateSpecialization: {
258     FunctionTemplateDecl *Template
259       = cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++]));
260     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
261     
262     // Template arguments.
263     llvm::SmallVector<TemplateArgument, 8> TemplArgs;
264     Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
265     
266     // Template args as written.
267     llvm::SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
268     SourceLocation LAngleLoc, RAngleLoc;
269     if (Record[Idx++]) {  // TemplateArgumentsAsWritten != 0
270       unsigned NumTemplateArgLocs = Record[Idx++];
271       TemplArgLocs.reserve(NumTemplateArgLocs);
272       for (unsigned i=0; i != NumTemplateArgLocs; ++i)
273         TemplArgLocs.push_back(
274             Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
275   
276       LAngleLoc = Reader.ReadSourceLocation(Record, Idx);
277       RAngleLoc = Reader.ReadSourceLocation(Record, Idx);
278     }
279     
280     SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
281
282     if (FD->isCanonicalDecl()) // if canonical add to template's set.
283       FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(),
284                                             TemplArgs.data(), TSK,
285                                             TemplArgLocs.size(),
286                                             TemplArgLocs.data(),
287                                             LAngleLoc, RAngleLoc, POI);
288     break;
289   }
290   case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
291     // Templates.
292     UnresolvedSet<8> TemplDecls;
293     unsigned NumTemplates = Record[Idx++];
294     while (NumTemplates--)
295       TemplDecls.addDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
296     
297     // Templates args.
298     TemplateArgumentListInfo TemplArgs;
299     unsigned NumArgs = Record[Idx++];
300     while (NumArgs--)
301       TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Cursor,Record, Idx));
302     TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
303     TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
304     
305     FD->setDependentTemplateSpecialization(*Reader.getContext(),
306                                            TemplDecls, TemplArgs);
307     break;
308   }
309   }
310
311   // FunctionDecl's body is handled last at ASTDeclReader::Visit,
312   // after everything else is read.
313
314   VisitRedeclarable(FD);
315   FD->setStorageClass((StorageClass)Record[Idx++]);
316   FD->setStorageClassAsWritten((StorageClass)Record[Idx++]);
317   FD->setInlineSpecified(Record[Idx++]);
318   FD->setVirtualAsWritten(Record[Idx++]);
319   FD->setPure(Record[Idx++]);
320   FD->setHasInheritedPrototype(Record[Idx++]);
321   FD->setHasWrittenPrototype(Record[Idx++]);
322   FD->setDeleted(Record[Idx++]);
323   FD->setTrivial(Record[Idx++]);
324   FD->setCopyAssignment(Record[Idx++]);
325   FD->setHasImplicitReturnZero(Record[Idx++]);
326   FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
327
328   // Read in the parameters.
329   unsigned NumParams = Record[Idx++];
330   llvm::SmallVector<ParmVarDecl *, 16> Params;
331   Params.reserve(NumParams);
332   for (unsigned I = 0; I != NumParams; ++I)
333     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
334   FD->setParams(Params.data(), NumParams);
335 }
336
337 void ASTDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
338   VisitNamedDecl(MD);
339   if (Record[Idx++]) {
340     // In practice, this won't be executed (since method definitions
341     // don't occur in header files).
342     MD->setBody(Reader.ReadStmt(Cursor));
343     MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
344     MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
345   }
346   MD->setInstanceMethod(Record[Idx++]);
347   MD->setVariadic(Record[Idx++]);
348   MD->setSynthesized(Record[Idx++]);
349   MD->setDefined(Record[Idx++]);
350   MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
351   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
352   MD->setNumSelectorArgs(unsigned(Record[Idx++]));
353   MD->setResultType(Reader.GetType(Record[Idx++]));
354   MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
355   MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
356   unsigned NumParams = Record[Idx++];
357   llvm::SmallVector<ParmVarDecl *, 16> Params;
358   Params.reserve(NumParams);
359   for (unsigned I = 0; I != NumParams; ++I)
360     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
361   MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams,
362                       NumParams);
363 }
364
365 void ASTDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
366   VisitNamedDecl(CD);
367   SourceLocation A = SourceLocation::getFromRawEncoding(Record[Idx++]);
368   SourceLocation B = SourceLocation::getFromRawEncoding(Record[Idx++]);
369   CD->setAtEndRange(SourceRange(A, B));
370 }
371
372 void ASTDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
373   VisitObjCContainerDecl(ID);
374   ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
375   ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
376                        (Reader.GetDecl(Record[Idx++])));
377   
378   // Read the directly referenced protocols and their SourceLocations.
379   unsigned NumProtocols = Record[Idx++];
380   llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
381   Protocols.reserve(NumProtocols);
382   for (unsigned I = 0; I != NumProtocols; ++I)
383     Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
384   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
385   ProtoLocs.reserve(NumProtocols);
386   for (unsigned I = 0; I != NumProtocols; ++I)
387     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
388   ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
389                       *Reader.getContext());
390   
391   // Read the transitive closure of protocols referenced by this class.
392   NumProtocols = Record[Idx++];
393   Protocols.clear();
394   Protocols.reserve(NumProtocols);
395   for (unsigned I = 0; I != NumProtocols; ++I)
396     Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
397   ID->AllReferencedProtocols.set(Protocols.data(), NumProtocols,
398                                  *Reader.getContext());
399   
400   // Read the ivars.
401   unsigned NumIvars = Record[Idx++];
402   llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
403   IVars.reserve(NumIvars);
404   for (unsigned I = 0; I != NumIvars; ++I)
405     IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
406   ID->setCategoryList(
407                cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
408   // We will rebuild this list lazily.
409   ID->setIvarList(0);
410   ID->setForwardDecl(Record[Idx++]);
411   ID->setImplicitInterfaceDecl(Record[Idx++]);
412   ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
413   ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
414   ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
415 }
416
417 void ASTDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
418   VisitFieldDecl(IVD);
419   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
420   // This field will be built lazily.
421   IVD->setNextIvar(0);
422   bool synth = Record[Idx++];
423   IVD->setSynthesize(synth);
424 }
425
426 void ASTDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
427   VisitObjCContainerDecl(PD);
428   PD->setForwardDecl(Record[Idx++]);
429   PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
430   unsigned NumProtoRefs = Record[Idx++];
431   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
432   ProtoRefs.reserve(NumProtoRefs);
433   for (unsigned I = 0; I != NumProtoRefs; ++I)
434     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
435   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
436   ProtoLocs.reserve(NumProtoRefs);
437   for (unsigned I = 0; I != NumProtoRefs; ++I)
438     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
439   PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
440                       *Reader.getContext());
441 }
442
443 void ASTDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
444   VisitFieldDecl(FD);
445 }
446
447 void ASTDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
448   VisitDecl(CD);
449   unsigned NumClassRefs = Record[Idx++];
450   llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
451   ClassRefs.reserve(NumClassRefs);
452   for (unsigned I = 0; I != NumClassRefs; ++I)
453     ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
454   llvm::SmallVector<SourceLocation, 16> SLocs;
455   SLocs.reserve(NumClassRefs);
456   for (unsigned I = 0; I != NumClassRefs; ++I)
457     SLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
458   CD->setClassList(*Reader.getContext(), ClassRefs.data(), SLocs.data(),
459                    NumClassRefs);
460 }
461
462 void ASTDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
463   VisitDecl(FPD);
464   unsigned NumProtoRefs = Record[Idx++];
465   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
466   ProtoRefs.reserve(NumProtoRefs);
467   for (unsigned I = 0; I != NumProtoRefs; ++I)
468     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
469   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
470   ProtoLocs.reserve(NumProtoRefs);
471   for (unsigned I = 0; I != NumProtoRefs; ++I)
472     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
473   FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
474                        *Reader.getContext());
475 }
476
477 void ASTDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
478   VisitObjCContainerDecl(CD);
479   CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
480   unsigned NumProtoRefs = Record[Idx++];
481   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
482   ProtoRefs.reserve(NumProtoRefs);
483   for (unsigned I = 0; I != NumProtoRefs; ++I)
484     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
485   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
486   ProtoLocs.reserve(NumProtoRefs);
487   for (unsigned I = 0; I != NumProtoRefs; ++I)
488     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
489   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
490                       *Reader.getContext());
491   CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
492   CD->setHasSynthBitfield(Record[Idx++]);
493   CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
494   CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
495 }
496
497 void ASTDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
498   VisitNamedDecl(CAD);
499   CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
500 }
501
502 void ASTDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
503   VisitNamedDecl(D);
504   D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
505   D->setType(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
506   // FIXME: stable encoding
507   D->setPropertyAttributes(
508                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
509   D->setPropertyAttributesAsWritten(
510                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
511   // FIXME: stable encoding
512   D->setPropertyImplementation(
513                             (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
514   D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
515   D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
516   D->setGetterMethodDecl(
517                  cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
518   D->setSetterMethodDecl(
519                  cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
520   D->setPropertyIvarDecl(
521                    cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
522 }
523
524 void ASTDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
525   VisitObjCContainerDecl(D);
526   D->setClassInterface(
527               cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
528 }
529
530 void ASTDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
531   VisitObjCImplDecl(D);
532   D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
533 }
534
535 void ASTDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
536   VisitObjCImplDecl(D);
537   D->setSuperClass(
538               cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
539   llvm::tie(D->IvarInitializers, D->NumIvarInitializers)
540       = Reader.ReadCXXBaseOrMemberInitializers(Cursor, Record, Idx);
541   D->setHasSynthBitfield(Record[Idx++]);
542 }
543
544
545 void ASTDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
546   VisitDecl(D);
547   D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
548   D->setPropertyDecl(
549                cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
550   D->setPropertyIvarDecl(
551                    cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
552   D->setGetterCXXConstructor(Reader.ReadExpr(Cursor));
553   D->setSetterCXXAssignment(Reader.ReadExpr(Cursor));
554 }
555
556 void ASTDeclReader::VisitFieldDecl(FieldDecl *FD) {
557   VisitDeclaratorDecl(FD);
558   FD->setMutable(Record[Idx++]);
559   if (Record[Idx++])
560     FD->setBitWidth(Reader.ReadExpr(Cursor));
561   if (!FD->getDeclName()) {
562     FieldDecl *Tmpl = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]));
563     if (Tmpl)
564       Reader.getContext()->setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
565   }
566 }
567
568 void ASTDeclReader::VisitVarDecl(VarDecl *VD) {
569   VisitDeclaratorDecl(VD);
570   VD->setStorageClass((StorageClass)Record[Idx++]);
571   VD->setStorageClassAsWritten((StorageClass)Record[Idx++]);
572   VD->setThreadSpecified(Record[Idx++]);
573   VD->setCXXDirectInitializer(Record[Idx++]);
574   VD->setExceptionVariable(Record[Idx++]);
575   VD->setNRVOVariable(Record[Idx++]);
576   VisitRedeclarable(VD);
577   if (Record[Idx++])
578     VD->setInit(Reader.ReadExpr(Cursor));
579
580   if (Record[Idx++]) { // HasMemberSpecializationInfo.
581     VarDecl *Tmpl = cast<VarDecl>(Reader.GetDecl(Record[Idx++]));
582     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
583     SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
584     Reader.getContext()->setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
585   }
586 }
587
588 void ASTDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
589   VisitVarDecl(PD);
590 }
591
592 void ASTDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
593   VisitVarDecl(PD);
594   PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
595   PD->setHasInheritedDefaultArg(Record[Idx++]);
596   if (Record[Idx++]) // hasUninstantiatedDefaultArg.
597     PD->setUninstantiatedDefaultArg(Reader.ReadExpr(Cursor));
598 }
599
600 void ASTDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
601   VisitDecl(AD);
602   AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(Cursor)));
603 }
604
605 void ASTDeclReader::VisitBlockDecl(BlockDecl *BD) {
606   VisitDecl(BD);
607   BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(Cursor)));
608   BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
609   unsigned NumParams = Record[Idx++];
610   llvm::SmallVector<ParmVarDecl *, 16> Params;
611   Params.reserve(NumParams);
612   for (unsigned I = 0; I != NumParams; ++I)
613     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
614   BD->setParams(Params.data(), NumParams);
615 }
616
617 void ASTDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
618   VisitDecl(D);
619   D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
620   D->setHasBraces(Record[Idx++]);
621 }
622
623 void ASTDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
624   VisitNamedDecl(D);
625   D->setLBracLoc(Reader.ReadSourceLocation(Record, Idx));
626   D->setRBracLoc(Reader.ReadSourceLocation(Record, Idx));
627   D->setNextNamespace(
628                     cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
629
630   bool IsOriginal = Record[Idx++];
631   D->OrigOrAnonNamespace.setInt(IsOriginal);
632   D->OrigOrAnonNamespace.setPointer(
633                     cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
634 }
635
636 void ASTDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
637   VisitNamedDecl(D);
638   D->NamespaceLoc = Reader.ReadSourceLocation(Record, Idx);
639   D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
640   D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
641   D->IdentLoc = Reader.ReadSourceLocation(Record, Idx);
642   D->Namespace = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
643 }
644
645 void ASTDeclReader::VisitUsingDecl(UsingDecl *D) {
646   VisitNamedDecl(D);
647   D->setUsingLocation(Reader.ReadSourceLocation(Record, Idx));
648   D->setNestedNameRange(Reader.ReadSourceRange(Record, Idx));
649   D->setTargetNestedNameDecl(Reader.ReadNestedNameSpecifier(Record, Idx));
650   // FIXME: read the DNLoc component.
651
652   // FIXME: It would probably be more efficient to read these into a vector
653   // and then re-cosntruct the shadow decl set over that vector since it
654   // would avoid existence checks.
655   unsigned NumShadows = Record[Idx++];
656   for(unsigned I = 0; I != NumShadows; ++I) {
657     // Avoid invariant checking of UsingDecl::addShadowDecl, the decl may still
658     // be initializing.
659     D->Shadows.insert(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++])));
660   }
661   D->setTypeName(Record[Idx++]);
662   NamedDecl *Pattern = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
663   if (Pattern)
664     Reader.getContext()->setInstantiatedFromUsingDecl(D, Pattern);
665 }
666
667 void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
668   VisitNamedDecl(D);
669   D->setTargetDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
670   D->setUsingDecl(cast<UsingDecl>(Reader.GetDecl(Record[Idx++])));
671   UsingShadowDecl *Pattern
672       = cast_or_null<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]));
673   if (Pattern)
674     Reader.getContext()->setInstantiatedFromUsingShadowDecl(D, Pattern);
675 }
676
677 void ASTDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
678   VisitNamedDecl(D);
679   D->UsingLoc = Reader.ReadSourceLocation(Record, Idx);
680   D->NamespaceLoc = Reader.ReadSourceLocation(Record, Idx);
681   D->QualifierRange = Reader.ReadSourceRange(Record, Idx);
682   D->Qualifier = Reader.ReadNestedNameSpecifier(Record, Idx);
683   D->NominatedNamespace = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
684   D->CommonAncestor = cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]));
685 }
686
687 void ASTDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
688   VisitValueDecl(D);
689   D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
690   D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
691   D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
692   // FIXME: read the DNLoc component.
693 }
694
695 void ASTDeclReader::VisitUnresolvedUsingTypenameDecl(
696                                                UnresolvedUsingTypenameDecl *D) {
697   VisitTypeDecl(D);
698   D->TargetNestedNameRange = Reader.ReadSourceRange(Record, Idx);
699   D->UsingLocation = Reader.ReadSourceLocation(Record, Idx);
700   D->TypenameLocation = Reader.ReadSourceLocation(Record, Idx);
701   D->TargetNestedNameSpecifier = Reader.ReadNestedNameSpecifier(Record, Idx);
702 }
703
704 void ASTDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
705   ASTContext &C = *Reader.getContext();
706
707   // We need to allocate the DefinitionData struct ahead of VisitRecordDecl
708   // so that the other CXXRecordDecls can get a pointer even when the owner
709   // is still initializing.
710   bool OwnsDefinitionData = false;
711   enum DataOwnership { Data_NoDefData, Data_Owner, Data_NotOwner };
712   switch ((DataOwnership)Record[Idx++]) {
713   default:
714     assert(0 && "Out of sync with ASTDeclWriter or messed up reading");
715   case Data_NoDefData:
716     break;
717   case Data_Owner:
718     OwnsDefinitionData = true;
719     D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
720     break;
721   case Data_NotOwner:
722     D->DefinitionData
723         = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))->DefinitionData;
724     break;
725   }
726
727   VisitRecordDecl(D);
728
729   if (OwnsDefinitionData) {
730     assert(D->DefinitionData);
731     struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
732
733     Data.UserDeclaredConstructor = Record[Idx++];
734     Data.UserDeclaredCopyConstructor = Record[Idx++];
735     Data.UserDeclaredCopyAssignment = Record[Idx++];
736     Data.UserDeclaredDestructor = Record[Idx++];
737     Data.Aggregate = Record[Idx++];
738     Data.PlainOldData = Record[Idx++];
739     Data.Empty = Record[Idx++];
740     Data.Polymorphic = Record[Idx++];
741     Data.Abstract = Record[Idx++];
742     Data.HasTrivialConstructor = Record[Idx++];
743     Data.HasTrivialCopyConstructor = Record[Idx++];
744     Data.HasTrivialCopyAssignment = Record[Idx++];
745     Data.HasTrivialDestructor = Record[Idx++];
746     Data.ComputedVisibleConversions = Record[Idx++];
747     Data.DeclaredDefaultConstructor = Record[Idx++];
748     Data.DeclaredCopyConstructor = Record[Idx++];
749     Data.DeclaredCopyAssignment = Record[Idx++];
750     Data.DeclaredDestructor = Record[Idx++];
751
752     // setBases() is unsuitable since it may try to iterate the bases of an
753     // uninitialized base.
754     Data.NumBases = Record[Idx++];
755     Data.Bases = new(C) CXXBaseSpecifier [Data.NumBases];
756     for (unsigned i = 0; i != Data.NumBases; ++i)
757       Data.Bases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
758
759     // FIXME: Make VBases lazily computed when needed to avoid storing them.
760     Data.NumVBases = Record[Idx++];
761     Data.VBases = new(C) CXXBaseSpecifier [Data.NumVBases];
762     for (unsigned i = 0; i != Data.NumVBases; ++i)
763       Data.VBases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
764
765     Reader.ReadUnresolvedSet(Data.Conversions, Record, Idx);
766     Reader.ReadUnresolvedSet(Data.VisibleConversions, Record, Idx);
767     assert(Data.Definition && "Data.Definition should be already set!");
768     Data.FirstFriend
769         = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
770   }
771
772   enum CXXRecKind {
773     CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
774   };
775   switch ((CXXRecKind)Record[Idx++]) {
776   default:
777     assert(false && "Out of sync with ASTDeclWriter::VisitCXXRecordDecl?");
778   case CXXRecNotTemplate:
779     break;
780   case CXXRecTemplate:
781     D->setDescribedClassTemplate(
782                         cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])));
783     break;
784   case CXXRecMemberSpecialization: {
785     CXXRecordDecl *RD = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]));
786     TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
787     SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
788     D->setInstantiationOfMemberClass(RD, TSK);
789     D->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
790     break;
791   }
792   }
793 }
794
795 void ASTDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
796   VisitFunctionDecl(D);
797   unsigned NumOverridenMethods = Record[Idx++];
798   while (NumOverridenMethods--) {
799     CXXMethodDecl *MD = cast<CXXMethodDecl>(Reader.GetDecl(Record[Idx++]));
800     // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
801     // MD may be initializing.
802     Reader.getContext()->addOverriddenMethod(D, MD);
803   }
804 }
805
806 void ASTDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
807   VisitCXXMethodDecl(D);
808   
809   D->IsExplicitSpecified = Record[Idx++];
810   D->ImplicitlyDefined = Record[Idx++];
811   llvm::tie(D->BaseOrMemberInitializers, D->NumBaseOrMemberInitializers)
812       = Reader.ReadCXXBaseOrMemberInitializers(Cursor, Record, Idx);
813 }
814
815 void ASTDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
816   VisitCXXMethodDecl(D);
817
818   D->ImplicitlyDefined = Record[Idx++];
819   D->OperatorDelete = cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
820 }
821
822 void ASTDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
823   VisitCXXMethodDecl(D);
824   D->IsExplicitSpecified = Record[Idx++];
825 }
826
827 void ASTDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
828   VisitDecl(D);
829   D->setColonLoc(Reader.ReadSourceLocation(Record, Idx));
830 }
831
832 void ASTDeclReader::VisitFriendDecl(FriendDecl *D) {
833   VisitDecl(D);
834   if (Record[Idx++])
835     D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
836   else
837     D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
838   D->NextFriend = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
839   D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
840 }
841
842 void ASTDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
843   VisitDecl(D);
844   unsigned NumParams = Record[Idx++];
845   D->NumParams = NumParams;
846   D->Params = new TemplateParameterList*[NumParams];
847   for (unsigned i = 0; i != NumParams; ++i)
848     D->Params[i] = Reader.ReadTemplateParameterList(Record, Idx);
849   if (Record[Idx++]) // HasFriendDecl
850     D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
851   else
852     D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
853   D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
854 }
855
856 void ASTDeclReader::VisitTemplateDecl(TemplateDecl *D) {
857   VisitNamedDecl(D);
858
859   NamedDecl *TemplatedDecl
860     = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
861   TemplateParameterList* TemplateParams
862       = Reader.ReadTemplateParameterList(Record, Idx); 
863   D->init(TemplatedDecl, TemplateParams);
864 }
865
866 void ASTDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
867   VisitTemplateDecl(D);
868
869   D->IdentifierNamespace = Record[Idx++];
870   RedeclarableTemplateDecl *PrevDecl =
871       cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
872   assert((PrevDecl == 0 || PrevDecl->getKind() == D->getKind()) &&
873          "PrevDecl kind mismatch");
874   if (PrevDecl)
875     D->CommonOrPrev = PrevDecl;
876   if (PrevDecl == 0) {
877     if (RedeclarableTemplateDecl *RTD
878           = cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]))) {
879       assert(RTD->getKind() == D->getKind() &&
880              "InstantiatedFromMemberTemplate kind mismatch");
881       D->setInstantiatedFromMemberTemplateImpl(RTD);
882       if (Record[Idx++])
883         D->setMemberSpecialization();
884     }
885
886     RedeclarableTemplateDecl *LatestDecl = 
887         cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
888   
889     // This decl is a first one and the latest declaration that it points to is
890     // in the same AST file. However, if this actually needs to point to a
891     // redeclaration in another AST file, we need to update it by checking
892     // the FirstLatestDeclIDs map which tracks this kind of decls.
893     assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
894     ASTReader::FirstLatestDeclIDMap::iterator I
895         = Reader.FirstLatestDeclIDs.find(ThisDeclID);
896     if (I != Reader.FirstLatestDeclIDs.end()) {
897       Decl *NewLatest = Reader.GetDecl(I->second);
898       assert((LatestDecl->getLocation().isInvalid() ||
899               NewLatest->getLocation().isInvalid()  ||
900               Reader.SourceMgr.isBeforeInTranslationUnit(
901                                                    LatestDecl->getLocation(),
902                                                    NewLatest->getLocation())) &&
903              "The new latest is supposed to come after the previous latest");
904       LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
905     }
906
907     assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
908     D->getCommonPtr()->Latest = LatestDecl;
909   }
910 }
911
912 void ASTDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
913   VisitRedeclarableTemplateDecl(D);
914
915   if (D->getPreviousDeclaration() == 0) {
916     // This ClassTemplateDecl owns a CommonPtr; read it.
917
918     // FoldingSets are filled in VisitClassTemplateSpecializationDecl.
919     unsigned size = Record[Idx++];
920     while (size--)
921       cast<ClassTemplateSpecializationDecl>(Reader.GetDecl(Record[Idx++]));
922
923     size = Record[Idx++];
924     while (size--)
925       cast<ClassTemplatePartialSpecializationDecl>(
926                                                  Reader.GetDecl(Record[Idx++]));
927
928     // InjectedClassNameType is computed.
929   }
930 }
931
932 void ASTDeclReader::VisitClassTemplateSpecializationDecl(
933                                            ClassTemplateSpecializationDecl *D) {
934   VisitCXXRecordDecl(D);
935
936   if (Decl *InstD = Reader.GetDecl(Record[Idx++])) {
937     if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
938       D->setInstantiationOf(CTD);
939     } else {
940       llvm::SmallVector<TemplateArgument, 8> TemplArgs;
941       Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
942       D->setInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(InstD),
943                             TemplArgs.data(), TemplArgs.size());
944     }
945   }
946
947   // Explicit info.
948   if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx)) {
949     D->setTypeAsWritten(TyInfo);
950     D->setExternLoc(Reader.ReadSourceLocation(Record, Idx));
951     D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx));
952   }
953
954   llvm::SmallVector<TemplateArgument, 8> TemplArgs;
955   Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
956   D->initTemplateArgs(TemplArgs.data(), TemplArgs.size());
957   SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
958   if (POI.isValid())
959     D->setPointOfInstantiation(POI);
960   D->setSpecializationKind((TemplateSpecializationKind)Record[Idx++]);
961
962   if (D->isCanonicalDecl()) { // It's kept in the folding set.
963     ClassTemplateDecl *CanonPattern
964                        = cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]));
965     if (ClassTemplatePartialSpecializationDecl *Partial
966             = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
967       CanonPattern->getPartialSpecializations().InsertNode(Partial);
968     } else {
969       CanonPattern->getSpecializations().InsertNode(D);
970     }
971   }
972 }
973
974 void ASTDeclReader::VisitClassTemplatePartialSpecializationDecl(
975                                     ClassTemplatePartialSpecializationDecl *D) {
976   VisitClassTemplateSpecializationDecl(D);
977
978   D->initTemplateParameters(Reader.ReadTemplateParameterList(Record, Idx));
979   
980   TemplateArgumentListInfo ArgInfos;
981   unsigned NumArgs = Record[Idx++];
982   while (NumArgs--)
983     ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
984   D->initTemplateArgsAsWritten(ArgInfos);
985   
986   D->setSequenceNumber(Record[Idx++]);
987
988   // These are read/set from/to the first declaration.
989   if (D->getPreviousDeclaration() == 0) {
990     D->setInstantiatedFromMember(
991         cast_or_null<ClassTemplatePartialSpecializationDecl>(
992                                                 Reader.GetDecl(Record[Idx++])));
993     if (Record[Idx++])
994       D->setMemberSpecialization();
995   }
996 }
997
998 void ASTDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
999   VisitRedeclarableTemplateDecl(D);
1000
1001   if (D->getPreviousDeclaration() == 0) {
1002     // This FunctionTemplateDecl owns a CommonPtr; read it.
1003
1004     // Read the function specialization declarations.
1005     // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1006     // through the specialized FunctionDecl's setFunctionTemplateSpecialization.
1007     unsigned NumSpecs = Record[Idx++];
1008     while (NumSpecs--)
1009       Reader.GetDecl(Record[Idx++]);
1010   }
1011 }
1012
1013 void ASTDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1014   VisitTypeDecl(D);
1015
1016   D->setDeclaredWithTypename(Record[Idx++]);
1017   D->setParameterPack(Record[Idx++]);
1018
1019   bool Inherited = Record[Idx++];
1020   TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
1021   D->setDefaultArgument(DefArg, Inherited);
1022 }
1023
1024 void ASTDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1025   VisitVarDecl(D);
1026   // TemplateParmPosition.
1027   D->setDepth(Record[Idx++]);
1028   D->setPosition(Record[Idx++]);
1029   // Rest of NonTypeTemplateParmDecl.
1030   if (Record[Idx++]) {
1031     Expr *DefArg = Reader.ReadExpr(Cursor);
1032     bool Inherited = Record[Idx++];
1033     D->setDefaultArgument(DefArg, Inherited);
1034  }
1035 }
1036
1037 void ASTDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1038   VisitTemplateDecl(D);
1039   // TemplateParmPosition.
1040   D->setDepth(Record[Idx++]);
1041   D->setPosition(Record[Idx++]);
1042   // Rest of TemplateTemplateParmDecl.
1043   TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx);
1044   bool IsInherited = Record[Idx++];
1045   D->setDefaultArgument(Arg, IsInherited);
1046 }
1047
1048 void ASTDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
1049   VisitDecl(D);
1050   D->AssertExpr = Reader.ReadExpr(Cursor);
1051   D->Message = cast<StringLiteral>(Reader.ReadExpr(Cursor));
1052 }
1053
1054 std::pair<uint64_t, uint64_t>
1055 ASTDeclReader::VisitDeclContext(DeclContext *DC) {
1056   uint64_t LexicalOffset = Record[Idx++];
1057   uint64_t VisibleOffset = Record[Idx++];
1058   return std::make_pair(LexicalOffset, VisibleOffset);
1059 }
1060
1061 template <typename T>
1062 void ASTDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1063   enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1064   RedeclKind Kind = (RedeclKind)Record[Idx++];
1065   switch (Kind) {
1066   default:
1067     assert(0 && "Out of sync with ASTDeclWriter::VisitRedeclarable or messed up"
1068                 " reading");
1069   case NoRedeclaration:
1070     break;
1071   case PointsToPrevious:
1072     D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1073                                 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1074     break;
1075   case PointsToLatest:
1076     D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1077                                 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1078     break;
1079   }
1080
1081   assert(!(Kind == PointsToPrevious &&
1082            Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1083                Reader.FirstLatestDeclIDs.end()) &&
1084          "This decl is not first, it should not be in the map");
1085   if (Kind == PointsToPrevious)
1086     return;
1087
1088   // This decl is a first one and the latest declaration that it points to is in
1089   // the same AST file. However, if this actually needs to point to a
1090   // redeclaration in another AST file, we need to update it by checking the
1091   // FirstLatestDeclIDs map which tracks this kind of decls.
1092   assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1093          "Invalid ThisDeclID ?");
1094   ASTReader::FirstLatestDeclIDMap::iterator I
1095       = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1096   if (I != Reader.FirstLatestDeclIDs.end()) {
1097     Decl *NewLatest = Reader.GetDecl(I->second);
1098     assert((D->getMostRecentDeclaration()->getLocation().isInvalid() ||
1099             NewLatest->getLocation().isInvalid() ||
1100             Reader.SourceMgr.isBeforeInTranslationUnit(
1101                                    D->getMostRecentDeclaration()->getLocation(),
1102                                    NewLatest->getLocation())) &&
1103            "The new latest is supposed to come after the previous latest");
1104     D->RedeclLink
1105         = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1106   }
1107 }
1108
1109 //===----------------------------------------------------------------------===//
1110 // Attribute Reading
1111 //===----------------------------------------------------------------------===//
1112
1113 /// \brief Reads attributes from the current stream position.
1114 void ASTReader::ReadAttributes(llvm::BitstreamCursor &DeclsCursor,
1115                                AttrVec &Attrs) {
1116   unsigned Code = DeclsCursor.ReadCode();
1117   assert(Code == llvm::bitc::UNABBREV_RECORD &&
1118          "Expected unabbreviated record"); (void)Code;
1119
1120   RecordData Record;
1121   unsigned Idx = 0;
1122   unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
1123   assert(RecCode == DECL_ATTR && "Expected attribute record");
1124   (void)RecCode;
1125
1126   while (Idx < Record.size()) {
1127     Attr *New = 0;
1128     attr::Kind Kind = (attr::Kind)Record[Idx++];
1129     SourceLocation Loc = SourceLocation::getFromRawEncoding(Record[Idx++]);
1130     bool isInherited = Record[Idx++];
1131
1132 #include "clang/Serialization/AttrPCHRead.inc"
1133
1134     assert(New && "Unable to decode attribute?");
1135     New->setInherited(isInherited);
1136     Attrs.push_back(New);
1137   }
1138 }
1139
1140 //===----------------------------------------------------------------------===//
1141 // ASTReader Implementation
1142 //===----------------------------------------------------------------------===//
1143
1144 /// \brief Note that we have loaded the declaration with the given
1145 /// Index.
1146 ///
1147 /// This routine notes that this declaration has already been loaded,
1148 /// so that future GetDecl calls will return this declaration rather
1149 /// than trying to load a new declaration.
1150 inline void ASTReader::LoadedDecl(unsigned Index, Decl *D) {
1151   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1152   DeclsLoaded[Index] = D;
1153 }
1154
1155
1156 /// \brief Determine whether the consumer will be interested in seeing
1157 /// this declaration (via HandleTopLevelDecl).
1158 ///
1159 /// This routine should return true for anything that might affect
1160 /// code generation, e.g., inline function definitions, Objective-C
1161 /// declarations with metadata, etc.
1162 static bool isConsumerInterestedIn(Decl *D) {
1163   if (isa<FileScopeAsmDecl>(D))
1164     return true;
1165   if (VarDecl *Var = dyn_cast<VarDecl>(D))
1166     return Var->isFileVarDecl() &&
1167            Var->isThisDeclarationADefinition() == VarDecl::Definition;
1168   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1169     return Func->isThisDeclarationADefinition();
1170   return isa<ObjCProtocolDecl>(D) || isa<ObjCImplementationDecl>(D);
1171 }
1172
1173 /// \brief Get the correct cursor and offset for loading a type.
1174 ASTReader::RecordLocation
1175 ASTReader::DeclCursorForIndex(unsigned Index, DeclID ID) {
1176   // See if there's an override.
1177   DeclReplacementMap::iterator It = ReplacedDecls.find(ID);
1178   if (It != ReplacedDecls.end())
1179     return RecordLocation(&It->second.first->DeclsCursor, It->second.second);
1180
1181   PerFileData *F = 0;
1182   for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1183     F = Chain[N - I - 1];
1184     if (Index < F->LocalNumDecls)
1185       break;
1186     Index -= F->LocalNumDecls;
1187   }
1188   assert(F && F->LocalNumDecls > Index && "Broken chain");
1189   return RecordLocation(&F->DeclsCursor, F->DeclOffsets[Index]);
1190 }
1191
1192 /// \brief Read the declaration at the given offset from the AST file.
1193 Decl *ASTReader::ReadDeclRecord(unsigned Index, DeclID ID) {
1194   RecordLocation Loc = DeclCursorForIndex(Index, ID);
1195   llvm::BitstreamCursor &DeclsCursor = *Loc.first;
1196   // Keep track of where we are in the stream, then jump back there
1197   // after reading this declaration.
1198   SavedStreamPosition SavedPosition(DeclsCursor);
1199
1200   ReadingKindTracker ReadingKind(Read_Decl, *this);
1201
1202   // Note that we are loading a declaration record.
1203   Deserializing ADecl(this);
1204
1205   DeclsCursor.JumpToBit(Loc.second);
1206   RecordData Record;
1207   unsigned Code = DeclsCursor.ReadCode();
1208   unsigned Idx = 0;
1209   ASTDeclReader Reader(*this, DeclsCursor, ID, Record, Idx);
1210
1211   Decl *D = 0;
1212   switch ((DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
1213   case DECL_ATTR:
1214   case DECL_CONTEXT_LEXICAL:
1215   case DECL_CONTEXT_VISIBLE:
1216     assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1217     break;
1218   case DECL_TRANSLATION_UNIT:
1219     assert(Index == 0 && "Translation unit must be at index 0");
1220     D = Context->getTranslationUnitDecl();
1221     break;
1222   case DECL_TYPEDEF:
1223     D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1224     break;
1225   case DECL_ENUM:
1226     D = EnumDecl::Create(*Context, Decl::EmptyShell());
1227     break;
1228   case DECL_RECORD:
1229     D = RecordDecl::Create(*Context, Decl::EmptyShell());
1230     break;
1231   case DECL_ENUM_CONSTANT:
1232     D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
1233                                  0, llvm::APSInt());
1234     break;
1235   case DECL_FUNCTION:
1236     D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
1237                              QualType(), 0);
1238     break;
1239   case DECL_LINKAGE_SPEC:
1240     D = LinkageSpecDecl::Create(*Context, 0, SourceLocation(),
1241                                 (LinkageSpecDecl::LanguageIDs)0,
1242                                 false);
1243     break;
1244   case DECL_NAMESPACE:
1245     D = NamespaceDecl::Create(*Context, 0, SourceLocation(), 0);
1246     break;
1247   case DECL_NAMESPACE_ALIAS:
1248     D = NamespaceAliasDecl::Create(*Context, 0, SourceLocation(),
1249                                    SourceLocation(), 0, SourceRange(), 0,
1250                                    SourceLocation(), 0);
1251     break;
1252   case DECL_USING:
1253     D = UsingDecl::Create(*Context, 0, SourceRange(), SourceLocation(),
1254                           0, DeclarationNameInfo(), false);
1255     break;
1256   case DECL_USING_SHADOW:
1257     D = UsingShadowDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1258     break;
1259   case DECL_USING_DIRECTIVE:
1260     D = UsingDirectiveDecl::Create(*Context, 0, SourceLocation(),
1261                                    SourceLocation(), SourceRange(), 0,
1262                                    SourceLocation(), 0, 0);
1263     break;
1264   case DECL_UNRESOLVED_USING_VALUE:
1265     D = UnresolvedUsingValueDecl::Create(*Context, 0, SourceLocation(),
1266                                          SourceRange(), 0,
1267                                          DeclarationNameInfo());
1268     break;
1269   case DECL_UNRESOLVED_USING_TYPENAME:
1270     D = UnresolvedUsingTypenameDecl::Create(*Context, 0, SourceLocation(),
1271                                             SourceLocation(), SourceRange(),
1272                                             0, SourceLocation(),
1273                                             DeclarationName());
1274     break;
1275   case DECL_CXX_RECORD:
1276     D = CXXRecordDecl::Create(*Context, Decl::EmptyShell());
1277     break;
1278   case DECL_CXX_METHOD:
1279     D = CXXMethodDecl::Create(*Context, 0, DeclarationNameInfo(),
1280                               QualType(), 0);
1281     break;
1282   case DECL_CXX_CONSTRUCTOR:
1283     D = CXXConstructorDecl::Create(*Context, Decl::EmptyShell());
1284     break;
1285   case DECL_CXX_DESTRUCTOR:
1286     D = CXXDestructorDecl::Create(*Context, Decl::EmptyShell());
1287     break;
1288   case DECL_CXX_CONVERSION:
1289     D = CXXConversionDecl::Create(*Context, Decl::EmptyShell());
1290     break;
1291   case DECL_ACCESS_SPEC:
1292     D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(),
1293                                SourceLocation());
1294     break;
1295   case DECL_FRIEND:
1296     D = FriendDecl::Create(*Context, Decl::EmptyShell());
1297     break;
1298   case DECL_FRIEND_TEMPLATE:
1299     D = FriendTemplateDecl::Create(*Context, Decl::EmptyShell());
1300     break;
1301   case DECL_CLASS_TEMPLATE:
1302     D = ClassTemplateDecl::Create(*Context, 0, SourceLocation(),
1303                                   DeclarationName(), 0, 0, 0);
1304     break;
1305   case DECL_CLASS_TEMPLATE_SPECIALIZATION:
1306     D = ClassTemplateSpecializationDecl::Create(*Context, Decl::EmptyShell());
1307     break;
1308   case DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
1309     D = ClassTemplatePartialSpecializationDecl::Create(*Context,
1310                                                             Decl::EmptyShell());
1311     break;
1312   case DECL_FUNCTION_TEMPLATE:
1313     D = FunctionTemplateDecl::Create(*Context, 0, SourceLocation(),
1314                                      DeclarationName(), 0, 0);
1315     break;
1316   case DECL_TEMPLATE_TYPE_PARM:
1317     D = TemplateTypeParmDecl::Create(*Context, Decl::EmptyShell());
1318     break;
1319   case DECL_NON_TYPE_TEMPLATE_PARM:
1320     D = NonTypeTemplateParmDecl::Create(*Context, 0, SourceLocation(), 0,0,0,
1321                                         QualType(),0);
1322     break;
1323   case DECL_TEMPLATE_TEMPLATE_PARM:
1324     D = TemplateTemplateParmDecl::Create(*Context, 0, SourceLocation(),0,0,0,0);
1325     break;
1326   case DECL_STATIC_ASSERT:
1327     D = StaticAssertDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1328     break;
1329
1330   case DECL_OBJC_METHOD:
1331     D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
1332                                Selector(), QualType(), 0, 0);
1333     break;
1334   case DECL_OBJC_INTERFACE:
1335     D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
1336     break;
1337   case DECL_OBJC_IVAR:
1338     D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1339                              ObjCIvarDecl::None);
1340     break;
1341   case DECL_OBJC_PROTOCOL:
1342     D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
1343     break;
1344   case DECL_OBJC_AT_DEFS_FIELD:
1345     D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
1346                                     QualType(), 0);
1347     break;
1348   case DECL_OBJC_CLASS:
1349     D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
1350     break;
1351   case DECL_OBJC_FORWARD_PROTOCOL:
1352     D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
1353     break;
1354   case DECL_OBJC_CATEGORY:
1355     D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(), 
1356                                  SourceLocation(), SourceLocation(), 0);
1357     break;
1358   case DECL_OBJC_CATEGORY_IMPL:
1359     D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1360     break;
1361   case DECL_OBJC_IMPLEMENTATION:
1362     D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1363     break;
1364   case DECL_OBJC_COMPATIBLE_ALIAS:
1365     D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1366     break;
1367   case DECL_OBJC_PROPERTY:
1368     D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
1369                                  0);
1370     break;
1371   case DECL_OBJC_PROPERTY_IMPL:
1372     D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
1373                                      SourceLocation(), 0,
1374                                      ObjCPropertyImplDecl::Dynamic, 0);
1375     break;
1376   case DECL_FIELD:
1377     D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
1378                           false);
1379     break;
1380   case DECL_VAR:
1381     D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1382                         SC_None, SC_None);
1383     break;
1384
1385   case DECL_IMPLICIT_PARAM:
1386     D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
1387     break;
1388
1389   case DECL_PARM_VAR:
1390     D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
1391                             SC_None, SC_None, 0);
1392     break;
1393   case DECL_FILE_SCOPE_ASM:
1394     D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
1395     break;
1396   case DECL_BLOCK:
1397     D = BlockDecl::Create(*Context, 0, SourceLocation());
1398     break;
1399   }
1400
1401   assert(D && "Unknown declaration reading AST file");
1402   LoadedDecl(Index, D);
1403   Reader.Visit(D);
1404
1405   // If this declaration is also a declaration context, get the
1406   // offsets for its tables of lexical and visible declarations.
1407   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1408     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1409     if (Offsets.first || Offsets.second) {
1410       DC->setHasExternalLexicalStorage(Offsets.first != 0);
1411       DC->setHasExternalVisibleStorage(Offsets.second != 0);
1412       DeclContextInfo Info;
1413       if (ReadDeclContextStorage(DeclsCursor, Offsets, Info))
1414         return 0;
1415       DeclContextInfos &Infos = DeclContextOffsets[DC];
1416       // Reading the TU will happen after reading its lexical update blocks,
1417       // so we need to make sure we insert in front. For all other contexts,
1418       // the vector is empty here anyway, so there's no loss in efficiency.
1419       Infos.insert(Infos.begin(), Info);
1420
1421       // Now add the pending visible updates for this decl context, if it has
1422       // any.
1423       DeclContextVisibleUpdatesPending::iterator I =
1424           PendingVisibleUpdates.find(ID);
1425       if (I != PendingVisibleUpdates.end()) {
1426         DeclContextVisibleUpdates &U = I->second;
1427         Info.LexicalDecls = 0;
1428         Info.NumLexicalDecls = 0;
1429         for (DeclContextVisibleUpdates::iterator UI = U.begin(), UE = U.end();
1430              UI != UE; ++UI) {
1431           Info.NameLookupTableData = *UI;
1432           Infos.push_back(Info);
1433         }
1434         PendingVisibleUpdates.erase(I);
1435       }
1436     }
1437   }
1438
1439   // If this is a template, read additional specializations that may be in a
1440   // different part of the chain.
1441   if (isa<RedeclarableTemplateDecl>(D)) {
1442     AdditionalTemplateSpecializationsMap::iterator F =
1443         AdditionalTemplateSpecializationsPending.find(ID);
1444     if (F != AdditionalTemplateSpecializationsPending.end()) {
1445       for (AdditionalTemplateSpecializations::iterator I = F->second.begin(),
1446                                                        E = F->second.end();
1447            I != E; ++I)
1448         GetDecl(*I);
1449       AdditionalTemplateSpecializationsPending.erase(F);
1450     }
1451   }
1452   assert(Idx == Record.size());
1453
1454   // If we have deserialized a declaration that has a definition the
1455   // AST consumer might need to know about, queue it.
1456   // We don't pass it to the consumer immediately because we may be in recursive
1457   // loading, and some declarations may still be initializing.
1458   if (isConsumerInterestedIn(D))
1459     InterestingDecls.push_back(D);
1460
1461   return D;
1462 }