]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Frontend/PCHReaderDecl.cpp
Update clang to r96341.
[FreeBSD/FreeBSD.git] / lib / Frontend / PCHReaderDecl.cpp
1 //===--- PCHReaderDecl.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 PCHReader::ReadDeclRecord method, which is the
11 // entrypoint for loading a decl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "clang/Frontend/PCHReader.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/Expr.h"
21 using namespace clang;
22
23
24 //===----------------------------------------------------------------------===//
25 // Declaration deserialization
26 //===----------------------------------------------------------------------===//
27
28 namespace {
29   class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> {
30     PCHReader &Reader;
31     const PCHReader::RecordData &Record;
32     unsigned &Idx;
33
34   public:
35     PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
36                   unsigned &Idx)
37       : Reader(Reader), Record(Record), Idx(Idx) { }
38
39     void VisitDecl(Decl *D);
40     void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
41     void VisitNamedDecl(NamedDecl *ND);
42     void VisitTypeDecl(TypeDecl *TD);
43     void VisitTypedefDecl(TypedefDecl *TD);
44     void VisitTagDecl(TagDecl *TD);
45     void VisitEnumDecl(EnumDecl *ED);
46     void VisitRecordDecl(RecordDecl *RD);
47     void VisitValueDecl(ValueDecl *VD);
48     void VisitEnumConstantDecl(EnumConstantDecl *ECD);
49     void VisitDeclaratorDecl(DeclaratorDecl *DD);
50     void VisitFunctionDecl(FunctionDecl *FD);
51     void VisitFieldDecl(FieldDecl *FD);
52     void VisitVarDecl(VarDecl *VD);
53     void VisitImplicitParamDecl(ImplicitParamDecl *PD);
54     void VisitParmVarDecl(ParmVarDecl *PD);
55     void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
56     void VisitBlockDecl(BlockDecl *BD);
57     std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
58     void VisitObjCMethodDecl(ObjCMethodDecl *D);
59     void VisitObjCContainerDecl(ObjCContainerDecl *D);
60     void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
61     void VisitObjCIvarDecl(ObjCIvarDecl *D);
62     void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
63     void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
64     void VisitObjCClassDecl(ObjCClassDecl *D);
65     void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
66     void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
67     void VisitObjCImplDecl(ObjCImplDecl *D);
68     void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69     void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
70     void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71     void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72     void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
73   };
74 }
75
76 void PCHDeclReader::VisitDecl(Decl *D) {
77   D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
78   D->setLexicalDeclContext(
79                      cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
80   D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
81   D->setInvalidDecl(Record[Idx++]);
82   if (Record[Idx++])
83     D->addAttr(Reader.ReadAttributes());
84   D->setImplicit(Record[Idx++]);
85   D->setUsed(Record[Idx++]);
86   D->setAccess((AccessSpecifier)Record[Idx++]);
87   D->setPCHLevel(Record[Idx++] + 1);
88 }
89
90 void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
91   VisitDecl(TU);
92 }
93
94 void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
95   VisitDecl(ND);
96   ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
97 }
98
99 void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
100   VisitNamedDecl(TD);
101   TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
102 }
103
104 void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
105   // Note that we cannot use VisitTypeDecl here, because we need to
106   // set the underlying type of the typedef *before* we try to read
107   // the type associated with the TypedefDecl.
108   VisitNamedDecl(TD);
109   uint64_t TypeData = Record[Idx++];
110   TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx));
111   TD->setTypeForDecl(Reader.GetType(TypeData).getTypePtr());
112 }
113
114 void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
115   VisitTypeDecl(TD);
116   TD->setPreviousDeclaration(
117                         cast_or_null<TagDecl>(Reader.GetDecl(Record[Idx++])));
118   TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
119   TD->setDefinition(Record[Idx++]);
120   TD->setEmbeddedInDeclarator(Record[Idx++]);
121   TD->setTypedefForAnonDecl(
122                     cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
123   TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
124   TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
125 }
126
127 void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
128   VisitTagDecl(ED);
129   ED->setIntegerType(Reader.GetType(Record[Idx++]));
130   ED->setPromotionType(Reader.GetType(Record[Idx++]));
131   // FIXME: C++ InstantiatedFrom
132 }
133
134 void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
135   VisitTagDecl(RD);
136   RD->setHasFlexibleArrayMember(Record[Idx++]);
137   RD->setAnonymousStructOrUnion(Record[Idx++]);
138   RD->setHasObjectMember(Record[Idx++]);
139 }
140
141 void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
142   VisitNamedDecl(VD);
143   VD->setType(Reader.GetType(Record[Idx++]));
144 }
145
146 void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
147   VisitValueDecl(ECD);
148   if (Record[Idx++])
149     ECD->setInitExpr(Reader.ReadDeclExpr());
150   ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
151 }
152
153 void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
154   VisitValueDecl(DD);
155   TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Record, Idx);
156   if (TInfo)
157     DD->setTypeSourceInfo(TInfo);
158 }
159
160 void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
161   VisitDeclaratorDecl(FD);
162   if (Record[Idx++])
163     FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo());
164   FD->setPreviousDeclaration(
165                    cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
166   FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
167   FD->setInlineSpecified(Record[Idx++]);
168   FD->setVirtualAsWritten(Record[Idx++]);
169   FD->setPure(Record[Idx++]);
170   FD->setHasInheritedPrototype(Record[Idx++]);
171   FD->setHasWrittenPrototype(Record[Idx++]);
172   FD->setDeleted(Record[Idx++]);
173   FD->setTrivial(Record[Idx++]);
174   FD->setCopyAssignment(Record[Idx++]);
175   FD->setHasImplicitReturnZero(Record[Idx++]);
176   FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
177   // FIXME: C++ TemplateOrInstantiation
178   unsigned NumParams = Record[Idx++];
179   llvm::SmallVector<ParmVarDecl *, 16> Params;
180   Params.reserve(NumParams);
181   for (unsigned I = 0; I != NumParams; ++I)
182     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
183   FD->setParams(Params.data(), NumParams);
184 }
185
186 void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
187   VisitNamedDecl(MD);
188   if (Record[Idx++]) {
189     // In practice, this won't be executed (since method definitions
190     // don't occur in header files).
191     MD->setBody(Reader.ReadDeclStmt());
192     MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
193     MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
194   }
195   MD->setInstanceMethod(Record[Idx++]);
196   MD->setVariadic(Record[Idx++]);
197   MD->setSynthesized(Record[Idx++]);
198   MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
199   MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
200   MD->setResultType(Reader.GetType(Record[Idx++]));
201   MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
202   unsigned NumParams = Record[Idx++];
203   llvm::SmallVector<ParmVarDecl *, 16> Params;
204   Params.reserve(NumParams);
205   for (unsigned I = 0; I != NumParams; ++I)
206     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
207   MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams);
208 }
209
210 void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
211   VisitNamedDecl(CD);
212   SourceLocation A = SourceLocation::getFromRawEncoding(Record[Idx++]);
213   SourceLocation B = SourceLocation::getFromRawEncoding(Record[Idx++]);
214   CD->setAtEndRange(SourceRange(A, B));
215 }
216
217 void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
218   VisitObjCContainerDecl(ID);
219   ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
220   ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
221                        (Reader.GetDecl(Record[Idx++])));
222   unsigned NumProtocols = Record[Idx++];
223   llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
224   Protocols.reserve(NumProtocols);
225   for (unsigned I = 0; I != NumProtocols; ++I)
226     Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
227   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
228   ProtoLocs.reserve(NumProtocols);
229   for (unsigned I = 0; I != NumProtocols; ++I)
230     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
231   ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
232                       *Reader.getContext());
233   unsigned NumIvars = Record[Idx++];
234   llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
235   IVars.reserve(NumIvars);
236   for (unsigned I = 0; I != NumIvars; ++I)
237     IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
238   ID->setIVarList(IVars.data(), NumIvars, *Reader.getContext());
239   ID->setCategoryList(
240                cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
241   ID->setForwardDecl(Record[Idx++]);
242   ID->setImplicitInterfaceDecl(Record[Idx++]);
243   ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
244   ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
245   ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
246 }
247
248 void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
249   VisitFieldDecl(IVD);
250   IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
251 }
252
253 void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
254   VisitObjCContainerDecl(PD);
255   PD->setForwardDecl(Record[Idx++]);
256   PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
257   unsigned NumProtoRefs = Record[Idx++];
258   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
259   ProtoRefs.reserve(NumProtoRefs);
260   for (unsigned I = 0; I != NumProtoRefs; ++I)
261     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
262   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
263   ProtoLocs.reserve(NumProtoRefs);
264   for (unsigned I = 0; I != NumProtoRefs; ++I)
265     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
266   PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
267                       *Reader.getContext());
268 }
269
270 void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
271   VisitFieldDecl(FD);
272 }
273
274 void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
275   VisitDecl(CD);
276   unsigned NumClassRefs = Record[Idx++];
277   llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
278   ClassRefs.reserve(NumClassRefs);
279   for (unsigned I = 0; I != NumClassRefs; ++I)
280     ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
281   llvm::SmallVector<SourceLocation, 16> SLocs;
282   SLocs.reserve(NumClassRefs);
283   for (unsigned I = 0; I != NumClassRefs; ++I)
284     SLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
285   CD->setClassList(*Reader.getContext(), ClassRefs.data(), SLocs.data(),
286                    NumClassRefs);
287 }
288
289 void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
290   VisitDecl(FPD);
291   unsigned NumProtoRefs = Record[Idx++];
292   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
293   ProtoRefs.reserve(NumProtoRefs);
294   for (unsigned I = 0; I != NumProtoRefs; ++I)
295     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
296   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
297   ProtoLocs.reserve(NumProtoRefs);
298   for (unsigned I = 0; I != NumProtoRefs; ++I)
299     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
300   FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
301                        *Reader.getContext());
302 }
303
304 void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
305   VisitObjCContainerDecl(CD);
306   CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
307   unsigned NumProtoRefs = Record[Idx++];
308   llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
309   ProtoRefs.reserve(NumProtoRefs);
310   for (unsigned I = 0; I != NumProtoRefs; ++I)
311     ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
312   llvm::SmallVector<SourceLocation, 16> ProtoLocs;
313   ProtoLocs.reserve(NumProtoRefs);
314   for (unsigned I = 0; I != NumProtoRefs; ++I)
315     ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
316   CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
317                       *Reader.getContext());
318   CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
319   CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
320   CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
321 }
322
323 void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
324   VisitNamedDecl(CAD);
325   CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
326 }
327
328 void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
329   VisitNamedDecl(D);
330   D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
331   D->setType(Reader.GetType(Record[Idx++]));
332   // FIXME: stable encoding
333   D->setPropertyAttributes(
334                       (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
335   // FIXME: stable encoding
336   D->setPropertyImplementation(
337                             (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
338   D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
339   D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
340   D->setGetterMethodDecl(
341                  cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
342   D->setSetterMethodDecl(
343                  cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
344   D->setPropertyIvarDecl(
345                    cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
346 }
347
348 void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
349   VisitObjCContainerDecl(D);
350   D->setClassInterface(
351               cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
352 }
353
354 void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
355   VisitObjCImplDecl(D);
356   D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
357 }
358
359 void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
360   VisitObjCImplDecl(D);
361   D->setSuperClass(
362               cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
363 }
364
365
366 void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
367   VisitDecl(D);
368   D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
369   D->setPropertyDecl(
370                cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
371   D->setPropertyIvarDecl(
372                    cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
373 }
374
375 void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
376   VisitDeclaratorDecl(FD);
377   FD->setMutable(Record[Idx++]);
378   if (Record[Idx++])
379     FD->setBitWidth(Reader.ReadDeclExpr());
380 }
381
382 void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
383   VisitDeclaratorDecl(VD);
384   VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
385   VD->setThreadSpecified(Record[Idx++]);
386   VD->setCXXDirectInitializer(Record[Idx++]);
387   VD->setDeclaredInCondition(Record[Idx++]);
388   VD->setPreviousDeclaration(
389                          cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
390   if (Record[Idx++])
391     VD->setInit(Reader.ReadDeclExpr());
392 }
393
394 void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
395   VisitVarDecl(PD);
396 }
397
398 void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
399   VisitVarDecl(PD);
400   PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
401 }
402
403 void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
404   VisitDecl(AD);
405   AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr()));
406 }
407
408 void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
409   VisitDecl(BD);
410   BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt()));
411   unsigned NumParams = Record[Idx++];
412   llvm::SmallVector<ParmVarDecl *, 16> Params;
413   Params.reserve(NumParams);
414   for (unsigned I = 0; I != NumParams; ++I)
415     Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
416   BD->setParams(Params.data(), NumParams);
417 }
418
419 std::pair<uint64_t, uint64_t>
420 PCHDeclReader::VisitDeclContext(DeclContext *DC) {
421   uint64_t LexicalOffset = Record[Idx++];
422   uint64_t VisibleOffset = Record[Idx++];
423   return std::make_pair(LexicalOffset, VisibleOffset);
424 }
425
426 //===----------------------------------------------------------------------===//
427 // Attribute Reading
428 //===----------------------------------------------------------------------===//
429
430 /// \brief Reads attributes from the current stream position.
431 Attr *PCHReader::ReadAttributes() {
432   unsigned Code = DeclsCursor.ReadCode();
433   assert(Code == llvm::bitc::UNABBREV_RECORD &&
434          "Expected unabbreviated record"); (void)Code;
435
436   RecordData Record;
437   unsigned Idx = 0;
438   unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
439   assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
440   (void)RecCode;
441
442 #define SIMPLE_ATTR(Name)                       \
443  case Attr::Name:                               \
444    New = ::new (*Context) Name##Attr();         \
445    break
446
447 #define STRING_ATTR(Name)                                       \
448  case Attr::Name:                                               \
449    New = ::new (*Context) Name##Attr(*Context, ReadString(Record, Idx));  \
450    break
451
452 #define UNSIGNED_ATTR(Name)                             \
453  case Attr::Name:                                       \
454    New = ::new (*Context) Name##Attr(Record[Idx++]);    \
455    break
456
457   Attr *Attrs = 0;
458   while (Idx < Record.size()) {
459     Attr *New = 0;
460     Attr::Kind Kind = (Attr::Kind)Record[Idx++];
461     bool IsInherited = Record[Idx++];
462
463     switch (Kind) {
464     default:
465       assert(0 && "Unknown attribute!");
466       break;
467     STRING_ATTR(Alias);
468     UNSIGNED_ATTR(Aligned);
469     SIMPLE_ATTR(AlwaysInline);
470     SIMPLE_ATTR(AnalyzerNoReturn);
471     STRING_ATTR(Annotate);
472     STRING_ATTR(AsmLabel);
473     SIMPLE_ATTR(BaseCheck);
474
475     case Attr::Blocks:
476       New = ::new (*Context) BlocksAttr(
477                                   (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
478       break;
479
480     SIMPLE_ATTR(CDecl);
481
482     case Attr::Cleanup:
483       New = ::new (*Context) CleanupAttr(
484                                   cast<FunctionDecl>(GetDecl(Record[Idx++])));
485       break;
486
487     SIMPLE_ATTR(Const);
488     UNSIGNED_ATTR(Constructor);
489     SIMPLE_ATTR(DLLExport);
490     SIMPLE_ATTR(DLLImport);
491     SIMPLE_ATTR(Deprecated);
492     UNSIGNED_ATTR(Destructor);
493     SIMPLE_ATTR(FastCall);
494     SIMPLE_ATTR(Final);
495
496     case Attr::Format: {
497       std::string Type = ReadString(Record, Idx);
498       unsigned FormatIdx = Record[Idx++];
499       unsigned FirstArg = Record[Idx++];
500       New = ::new (*Context) FormatAttr(*Context, Type, FormatIdx, FirstArg);
501       break;
502     }
503
504     case Attr::FormatArg: {
505       unsigned FormatIdx = Record[Idx++];
506       New = ::new (*Context) FormatArgAttr(FormatIdx);
507       break;
508     }
509
510     case Attr::Sentinel: {
511       int sentinel = Record[Idx++];
512       int nullPos = Record[Idx++];
513       New = ::new (*Context) SentinelAttr(sentinel, nullPos);
514       break;
515     }
516
517     SIMPLE_ATTR(GNUInline);
518     SIMPLE_ATTR(Hiding);
519
520     case Attr::IBOutletKind:
521       New = ::new (*Context) IBOutletAttr();
522       break;
523
524     SIMPLE_ATTR(Malloc);
525     SIMPLE_ATTR(NoDebug);
526     SIMPLE_ATTR(NoInline);
527     SIMPLE_ATTR(NoReturn);
528     SIMPLE_ATTR(NoThrow);
529
530     case Attr::NonNull: {
531       unsigned Size = Record[Idx++];
532       llvm::SmallVector<unsigned, 16> ArgNums;
533       ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
534       Idx += Size;
535       New = ::new (*Context) NonNullAttr(*Context, ArgNums.data(), Size);
536       break;
537     }
538
539     case Attr::ReqdWorkGroupSize: {
540       unsigned X = Record[Idx++];
541       unsigned Y = Record[Idx++];
542       unsigned Z = Record[Idx++];
543       New = ::new (*Context) ReqdWorkGroupSizeAttr(X, Y, Z);
544       break;
545     }
546
547     SIMPLE_ATTR(ObjCException);
548     SIMPLE_ATTR(ObjCNSObject);
549     SIMPLE_ATTR(CFReturnsRetained);
550     SIMPLE_ATTR(NSReturnsRetained);
551     SIMPLE_ATTR(Overloadable);
552     SIMPLE_ATTR(Override);
553     SIMPLE_ATTR(Packed);
554     UNSIGNED_ATTR(PragmaPack);
555     SIMPLE_ATTR(Pure);
556     UNSIGNED_ATTR(Regparm);
557     STRING_ATTR(Section);
558     SIMPLE_ATTR(StdCall);
559     SIMPLE_ATTR(TransparentUnion);
560     SIMPLE_ATTR(Unavailable);
561     SIMPLE_ATTR(Unused);
562     SIMPLE_ATTR(Used);
563
564     case Attr::Visibility:
565       New = ::new (*Context) VisibilityAttr(
566                               (VisibilityAttr::VisibilityTypes)Record[Idx++]);
567       break;
568
569     SIMPLE_ATTR(WarnUnusedResult);
570     SIMPLE_ATTR(Weak);
571     SIMPLE_ATTR(WeakImport);
572     }
573
574     assert(New && "Unable to decode attribute?");
575     New->setInherited(IsInherited);
576     New->setNext(Attrs);
577     Attrs = New;
578   }
579 #undef UNSIGNED_ATTR
580 #undef STRING_ATTR
581 #undef SIMPLE_ATTR
582
583   // The list of attributes was built backwards. Reverse the list
584   // before returning it.
585   Attr *PrevAttr = 0, *NextAttr = 0;
586   while (Attrs) {
587     NextAttr = Attrs->getNext();
588     Attrs->setNext(PrevAttr);
589     PrevAttr = Attrs;
590     Attrs = NextAttr;
591   }
592
593   return PrevAttr;
594 }
595
596 //===----------------------------------------------------------------------===//
597 // PCHReader Implementation
598 //===----------------------------------------------------------------------===//
599
600 /// \brief Note that we have loaded the declaration with the given
601 /// Index.
602 ///
603 /// This routine notes that this declaration has already been loaded,
604 /// so that future GetDecl calls will return this declaration rather
605 /// than trying to load a new declaration.
606 inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
607   assert(!DeclsLoaded[Index] && "Decl loaded twice?");
608   DeclsLoaded[Index] = D;
609 }
610
611
612 /// \brief Determine whether the consumer will be interested in seeing
613 /// this declaration (via HandleTopLevelDecl).
614 ///
615 /// This routine should return true for anything that might affect
616 /// code generation, e.g., inline function definitions, Objective-C
617 /// declarations with metadata, etc.
618 static bool isConsumerInterestedIn(Decl *D) {
619   if (isa<FileScopeAsmDecl>(D))
620     return true;
621   if (VarDecl *Var = dyn_cast<VarDecl>(D))
622     return Var->isFileVarDecl() && Var->getInit();
623   if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
624     return Func->isThisDeclarationADefinition();
625   return isa<ObjCProtocolDecl>(D);
626 }
627
628 /// \brief Read the declaration at the given offset from the PCH file.
629 Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
630   // Keep track of where we are in the stream, then jump back there
631   // after reading this declaration.
632   SavedStreamPosition SavedPosition(DeclsCursor);
633
634   // Note that we are loading a declaration record.
635   LoadingTypeOrDecl Loading(*this);
636
637   DeclsCursor.JumpToBit(Offset);
638   RecordData Record;
639   unsigned Code = DeclsCursor.ReadCode();
640   unsigned Idx = 0;
641   PCHDeclReader Reader(*this, Record, Idx);
642
643   Decl *D = 0;
644   switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
645   case pch::DECL_ATTR:
646   case pch::DECL_CONTEXT_LEXICAL:
647   case pch::DECL_CONTEXT_VISIBLE:
648     assert(false && "Record cannot be de-serialized with ReadDeclRecord");
649     break;
650   case pch::DECL_TRANSLATION_UNIT:
651     assert(Index == 0 && "Translation unit must be at index 0");
652     D = Context->getTranslationUnitDecl();
653     break;
654   case pch::DECL_TYPEDEF:
655     D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
656     break;
657   case pch::DECL_ENUM:
658     D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(), 0);
659     break;
660   case pch::DECL_RECORD:
661     D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0, SourceLocation(),
662                            0, SourceLocation(), 0);
663     break;
664   case pch::DECL_ENUM_CONSTANT:
665     D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
666                                  0, llvm::APSInt());
667     break;
668   case pch::DECL_FUNCTION:
669     D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
670                              QualType(), 0);
671     break;
672   case pch::DECL_OBJC_METHOD:
673     D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
674                                Selector(), QualType(), 0);
675     break;
676   case pch::DECL_OBJC_INTERFACE:
677     D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
678     break;
679   case pch::DECL_OBJC_IVAR:
680     D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
681                              ObjCIvarDecl::None);
682     break;
683   case pch::DECL_OBJC_PROTOCOL:
684     D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
685     break;
686   case pch::DECL_OBJC_AT_DEFS_FIELD:
687     D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
688                                     QualType(), 0);
689     break;
690   case pch::DECL_OBJC_CLASS:
691     D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
692     break;
693   case pch::DECL_OBJC_FORWARD_PROTOCOL:
694     D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
695     break;
696   case pch::DECL_OBJC_CATEGORY:
697     D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(), 
698                                  SourceLocation(), SourceLocation(), 0);
699     break;
700   case pch::DECL_OBJC_CATEGORY_IMPL:
701     D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
702     break;
703   case pch::DECL_OBJC_IMPLEMENTATION:
704     D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
705     break;
706   case pch::DECL_OBJC_COMPATIBLE_ALIAS:
707     D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
708     break;
709   case pch::DECL_OBJC_PROPERTY:
710     D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
711                                  QualType());
712     break;
713   case pch::DECL_OBJC_PROPERTY_IMPL:
714     D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
715                                      SourceLocation(), 0,
716                                      ObjCPropertyImplDecl::Dynamic, 0);
717     break;
718   case pch::DECL_FIELD:
719     D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
720                           false);
721     break;
722   case pch::DECL_VAR:
723     D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
724                         VarDecl::None);
725     break;
726
727   case pch::DECL_IMPLICIT_PARAM:
728     D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
729     break;
730
731   case pch::DECL_PARM_VAR:
732     D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
733                             VarDecl::None, 0);
734     break;
735   case pch::DECL_FILE_SCOPE_ASM:
736     D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
737     break;
738   case pch::DECL_BLOCK:
739     D = BlockDecl::Create(*Context, 0, SourceLocation());
740     break;
741   }
742
743   assert(D && "Unknown declaration reading PCH file");
744   LoadedDecl(Index, D);
745   Reader.Visit(D);
746
747   // If this declaration is also a declaration context, get the
748   // offsets for its tables of lexical and visible declarations.
749   if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
750     std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
751     if (Offsets.first || Offsets.second) {
752       DC->setHasExternalLexicalStorage(Offsets.first != 0);
753       DC->setHasExternalVisibleStorage(Offsets.second != 0);
754       DeclContextOffsets[DC] = Offsets;
755     }
756   }
757   assert(Idx == Record.size());
758
759   // If we have deserialized a declaration that has a definition the
760   // AST consumer might need to know about, notify the consumer
761   // about that definition now or queue it for later.
762   if (isConsumerInterestedIn(D)) {
763     if (Consumer) {
764       DeclGroupRef DG(D);
765       Consumer->HandleTopLevelDecl(DG);
766     } else {
767       InterestingDecls.push_back(D);
768     }
769   }
770
771   return D;
772 }