blob: 21beb42e93e5c653b2fe93a5c52f4add92137356 [file] [log] [blame]
Chris Lattner487412d2009-04-27 05:27:42 +00001//===--- 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"
Chris Lattnerca025db2010-05-07 21:43:38 +000020#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclTemplate.h"
Chris Lattner487412d2009-04-27 05:27:42 +000022#include "clang/AST/Expr.h"
23using namespace clang;
24
Chris Lattner487412d2009-04-27 05:27:42 +000025
26//===----------------------------------------------------------------------===//
27// Declaration deserialization
28//===----------------------------------------------------------------------===//
29
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000030namespace clang {
Chris Lattner487412d2009-04-27 05:27:42 +000031 class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> {
32 PCHReader &Reader;
Sebastian Redlc67764e2010-07-22 22:43:28 +000033 llvm::BitstreamCursor &Cursor;
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +000034 const pch::DeclID ThisDeclID;
Chris Lattner487412d2009-04-27 05:27:42 +000035 const PCHReader::RecordData &Record;
36 unsigned &Idx;
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +000037 pch::TypeID TypeIDForTypeDecl;
Chris Lattner487412d2009-04-27 05:27:42 +000038
Sebastian Redlc67764e2010-07-22 22:43:28 +000039 uint64_t GetCurrentCursorOffset();
40
Chris Lattner487412d2009-04-27 05:27:42 +000041 public:
Sebastian Redlc67764e2010-07-22 22:43:28 +000042 PCHDeclReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +000043 pch::DeclID thisDeclID, const PCHReader::RecordData &Record,
44 unsigned &Idx)
45 : Reader(Reader), Cursor(Cursor), ThisDeclID(thisDeclID), Record(Record),
46 Idx(Idx), TypeIDForTypeDecl(0) { }
Chris Lattner487412d2009-04-27 05:27:42 +000047
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +000048 void Visit(Decl *D);
49
Chris Lattner487412d2009-04-27 05:27:42 +000050 void VisitDecl(Decl *D);
51 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52 void VisitNamedDecl(NamedDecl *ND);
Douglas Gregore31bbd92010-02-21 18:22:14 +000053 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000054 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
55 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000056 void VisitTypeDecl(TypeDecl *TD);
57 void VisitTypedefDecl(TypedefDecl *TD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +000058 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000059 void VisitTagDecl(TagDecl *TD);
60 void VisitEnumDecl(EnumDecl *ED);
61 void VisitRecordDecl(RecordDecl *RD);
Chris Lattnerca025db2010-05-07 21:43:38 +000062 void VisitCXXRecordDecl(CXXRecordDecl *D);
63 void VisitClassTemplateSpecializationDecl(
64 ClassTemplateSpecializationDecl *D);
65 void VisitClassTemplatePartialSpecializationDecl(
66 ClassTemplatePartialSpecializationDecl *D);
67 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000068 void VisitValueDecl(ValueDecl *VD);
69 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +000070 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +000071 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner487412d2009-04-27 05:27:42 +000072 void VisitFunctionDecl(FunctionDecl *FD);
Chris Lattnerca025db2010-05-07 21:43:38 +000073 void VisitCXXMethodDecl(CXXMethodDecl *D);
74 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
75 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
76 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000077 void VisitFieldDecl(FieldDecl *FD);
78 void VisitVarDecl(VarDecl *VD);
79 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
80 void VisitParmVarDecl(ParmVarDecl *PD);
Chris Lattnerca025db2010-05-07 21:43:38 +000081 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
82 void VisitTemplateDecl(TemplateDecl *D);
Peter Collingbourne91b25b72010-07-29 16:11:51 +000083 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000084 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +000085 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000086 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +000087 void VisitUsingDecl(UsingDecl *D);
88 void VisitUsingShadowDecl(UsingShadowDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000089 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000090 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Abramo Bagnarad7340582010-06-05 05:09:32 +000091 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +000092 void VisitFriendDecl(FriendDecl *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000093 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
94 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner487412d2009-04-27 05:27:42 +000095 void VisitBlockDecl(BlockDecl *BD);
Chris Lattnerca025db2010-05-07 21:43:38 +000096
Chris Lattner487412d2009-04-27 05:27:42 +000097 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +000098 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
Chris Lattnerca025db2010-05-07 21:43:38 +000099
Alexis Hunted053252010-05-30 07:21:58 +0000100 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner487412d2009-04-27 05:27:42 +0000101 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
Sebastian Redlc67764e2010-07-22 22:43:28 +0000119uint64_t PCHDeclReader::GetCurrentCursorOffset() {
120 uint64_t Off = 0;
121 for (unsigned I = 0, N = Reader.Chain.size(); I != N; ++I) {
122 PCHReader::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
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000132void PCHDeclReader::Visit(Decl *D) {
133 DeclVisitor<PCHDeclReader, void>::Visit(D);
134
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000135 if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) {
136 // if we have a fully initialized TypeDecl, we can safely read its type now.
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000137 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr());
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000138 } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
139 // FunctionDecl's body was written last after all other Stmts/Exprs.
140 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000141 FD->setLazyBody(GetCurrentCursorOffset());
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000142 }
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000143}
144
Chris Lattner487412d2009-04-27 05:27:42 +0000145void PCHDeclReader::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++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000152 D->initAttrs(Reader.ReadAttributes(Cursor));
Chris Lattner487412d2009-04-27 05:27:42 +0000153 D->setImplicit(Record[Idx++]);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000154 D->setUsed(Record[Idx++]);
Chris Lattner487412d2009-04-27 05:27:42 +0000155 D->setAccess((AccessSpecifier)Record[Idx++]);
Douglas Gregor16bef852009-10-16 20:01:17 +0000156 D->setPCHLevel(Record[Idx++] + 1);
Chris Lattner487412d2009-04-27 05:27:42 +0000157}
158
159void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
160 VisitDecl(TU);
Chris Lattnerca025db2010-05-07 21:43:38 +0000161 TU->setAnonymousNamespace(
162 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner487412d2009-04-27 05:27:42 +0000163}
164
165void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
166 VisitDecl(ND);
Mike Stump11289f42009-09-09 15:08:12 +0000167 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
Chris Lattner487412d2009-04-27 05:27:42 +0000168}
169
170void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
171 VisitNamedDecl(TD);
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000172 // Delay type reading until after we have fully initialized the decl.
173 TypeIDForTypeDecl = Record[Idx++];
Chris Lattner487412d2009-04-27 05:27:42 +0000174}
175
176void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Argyrios Kyrtzidis318b0e72010-07-02 11:55:01 +0000177 VisitTypeDecl(TD);
Sebastian Redlc67764e2010-07-22 22:43:28 +0000178 TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner487412d2009-04-27 05:27:42 +0000179}
180
181void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
182 VisitTypeDecl(TD);
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +0000183 TD->IdentifierNamespace = Record[Idx++];
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +0000184 VisitRedeclarable(TD);
Chris Lattner487412d2009-04-27 05:27:42 +0000185 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
186 TD->setDefinition(Record[Idx++]);
Douglas Gregor5089c762010-02-12 17:40:34 +0000187 TD->setEmbeddedInDeclarator(Record[Idx++]);
Argyrios Kyrtzidis664b6902009-07-14 03:18:02 +0000188 TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000189 TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCall3e11ebe2010-03-15 10:12:16 +0000190 // FIXME: maybe read optional qualifier and its range.
191 TD->setTypedefForAnonDecl(
192 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner487412d2009-04-27 05:27:42 +0000193}
194
195void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
196 VisitTagDecl(ED);
197 ED->setIntegerType(Reader.GetType(Record[Idx++]));
John McCall56774992009-12-09 09:09:27 +0000198 ED->setPromotionType(Reader.GetType(Record[Idx++]));
John McCall9aa35be2010-05-06 08:49:23 +0000199 ED->setNumPositiveBits(Record[Idx++]);
200 ED->setNumNegativeBits(Record[Idx++]);
Argyrios Kyrtzidis282b36b2010-07-06 15:36:48 +0000201 ED->setInstantiationOfMemberEnum(
202 cast_or_null<EnumDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner487412d2009-04-27 05:27:42 +0000203}
204
205void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
206 VisitTagDecl(RD);
207 RD->setHasFlexibleArrayMember(Record[Idx++]);
208 RD->setAnonymousStructOrUnion(Record[Idx++]);
Fariborz Jahanian8e0d0422009-07-08 16:37:44 +0000209 RD->setHasObjectMember(Record[Idx++]);
Chris Lattner487412d2009-04-27 05:27:42 +0000210}
211
212void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
213 VisitNamedDecl(VD);
214 VD->setType(Reader.GetType(Record[Idx++]));
215}
216
217void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
218 VisitValueDecl(ECD);
219 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000220 ECD->setInitExpr(Reader.ReadExpr(Cursor));
Chris Lattner487412d2009-04-27 05:27:42 +0000221 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
222}
223
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000224void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
225 VisitValueDecl(DD);
Sebastian Redlc67764e2010-07-22 22:43:28 +0000226 TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
John McCallbcd03502009-12-07 02:54:59 +0000227 if (TInfo)
228 DD->setTypeSourceInfo(TInfo);
John McCall3e11ebe2010-03-15 10:12:16 +0000229 // FIXME: read optional qualifier and its range.
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000230}
231
Chris Lattner487412d2009-04-27 05:27:42 +0000232void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000233 VisitDeclaratorDecl(FD);
Chris Lattnerca025db2010-05-07 21:43:38 +0000234
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +0000235 FD->IdentifierNamespace = Record[Idx++];
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000236 switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000237 default: assert(false && "Unhandled TemplatedKind!");
238 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000239 case FunctionDecl::TK_NonTemplate:
240 break;
241 case FunctionDecl::TK_FunctionTemplate:
242 FD->setDescribedFunctionTemplate(
243 cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++])));
244 break;
245 case FunctionDecl::TK_MemberSpecialization: {
246 FunctionDecl *InstFD = cast<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
247 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
248 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
249 FD->setInstantiationOfMemberFunction(InstFD, TSK);
250 FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
251 break;
252 }
253 case FunctionDecl::TK_FunctionTemplateSpecialization: {
254 FunctionTemplateDecl *Template
255 = cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++]));
256 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
257
258 // Template arguments.
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000259 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000260 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000261
262 // Template args as written.
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000263 llvm::SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000264 SourceLocation LAngleLoc, RAngleLoc;
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000265 if (Record[Idx++]) { // TemplateArgumentsAsWritten != 0
266 unsigned NumTemplateArgLocs = Record[Idx++];
267 TemplArgLocs.reserve(NumTemplateArgLocs);
268 for (unsigned i=0; i != NumTemplateArgLocs; ++i)
Sebastian Redlc67764e2010-07-22 22:43:28 +0000269 TemplArgLocs.push_back(
270 Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000271
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000272 LAngleLoc = Reader.ReadSourceLocation(Record, Idx);
273 RAngleLoc = Reader.ReadSourceLocation(Record, Idx);
274 }
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +0000275
276 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000277
Argyrios Kyrtzidisdde57902010-07-20 13:59:58 +0000278 if (FD->isCanonicalDecl()) // if canonical add to template's set.
279 FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(),
280 TemplArgs.data(), TSK,
281 TemplArgLocs.size(),
282 TemplArgLocs.data(),
283 LAngleLoc, RAngleLoc, POI);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000284 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000285 }
286 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
287 // Templates.
288 UnresolvedSet<8> TemplDecls;
289 unsigned NumTemplates = Record[Idx++];
290 while (NumTemplates--)
291 TemplDecls.addDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
292
293 // Templates args.
294 TemplateArgumentListInfo TemplArgs;
295 unsigned NumArgs = Record[Idx++];
296 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +0000297 TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Cursor,Record, Idx));
Argyrios Kyrtzidis927d8e02010-07-05 10:37:55 +0000298 TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
299 TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000300
301 FD->setDependentTemplateSpecialization(*Reader.getContext(),
302 TemplDecls, TemplArgs);
Argyrios Kyrtzidis0b0369a2010-06-28 09:31:34 +0000303 break;
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +0000304 }
305 }
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000306
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000307 // FunctionDecl's body is handled last at PCHReaderDecl::Visit,
308 // after everything else is read.
309
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +0000310 VisitRedeclarable(FD);
Argyrios Kyrtzidis373a83a2010-07-02 11:55:40 +0000311 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
312 FD->setStorageClassAsWritten((FunctionDecl::StorageClass)Record[Idx++]);
313 FD->setInlineSpecified(Record[Idx++]);
314 FD->setVirtualAsWritten(Record[Idx++]);
315 FD->setPure(Record[Idx++]);
316 FD->setHasInheritedPrototype(Record[Idx++]);
317 FD->setHasWrittenPrototype(Record[Idx++]);
318 FD->setDeleted(Record[Idx++]);
319 FD->setTrivial(Record[Idx++]);
320 FD->setCopyAssignment(Record[Idx++]);
321 FD->setHasImplicitReturnZero(Record[Idx++]);
322 FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
323
Chris Lattnerca025db2010-05-07 21:43:38 +0000324 // Read in the parameters.
Chris Lattner487412d2009-04-27 05:27:42 +0000325 unsigned NumParams = Record[Idx++];
326 llvm::SmallVector<ParmVarDecl *, 16> Params;
327 Params.reserve(NumParams);
328 for (unsigned I = 0; I != NumParams; ++I)
329 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregord5058122010-02-11 01:19:42 +0000330 FD->setParams(Params.data(), NumParams);
Chris Lattner487412d2009-04-27 05:27:42 +0000331}
332
333void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
334 VisitNamedDecl(MD);
335 if (Record[Idx++]) {
336 // In practice, this won't be executed (since method definitions
337 // don't occur in header files).
Sebastian Redlc67764e2010-07-22 22:43:28 +0000338 MD->setBody(Reader.ReadStmt(Cursor));
Chris Lattner487412d2009-04-27 05:27:42 +0000339 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
340 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
341 }
342 MD->setInstanceMethod(Record[Idx++]);
343 MD->setVariadic(Record[Idx++]);
344 MD->setSynthesized(Record[Idx++]);
Fariborz Jahanian6e7e8cc2010-07-22 18:24:20 +0000345 MD->setDefined(Record[Idx++]);
Chris Lattner487412d2009-04-27 05:27:42 +0000346 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
347 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000348 MD->setNumSelectorArgs(unsigned(Record[Idx++]));
Chris Lattner487412d2009-04-27 05:27:42 +0000349 MD->setResultType(Reader.GetType(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +0000350 MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner487412d2009-04-27 05:27:42 +0000351 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 unsigned NumParams = Record[Idx++];
353 llvm::SmallVector<ParmVarDecl *, 16> Params;
354 Params.reserve(NumParams);
355 for (unsigned I = 0; I != NumParams; ++I)
356 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Fariborz Jahaniancdabb312010-04-09 15:40:42 +0000357 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams,
358 NumParams);
Chris Lattner487412d2009-04-27 05:27:42 +0000359}
360
361void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
362 VisitNamedDecl(CD);
Ted Kremenekc7c64312010-01-07 01:20:12 +0000363 SourceLocation A = SourceLocation::getFromRawEncoding(Record[Idx++]);
364 SourceLocation B = SourceLocation::getFromRawEncoding(Record[Idx++]);
365 CD->setAtEndRange(SourceRange(A, B));
Chris Lattner487412d2009-04-27 05:27:42 +0000366}
367
368void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
369 VisitObjCContainerDecl(ID);
370 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
371 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
372 (Reader.GetDecl(Record[Idx++])));
373 unsigned NumProtocols = Record[Idx++];
374 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
375 Protocols.reserve(NumProtocols);
376 for (unsigned I = 0; I != NumProtocols; ++I)
377 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor002b6712010-01-16 15:02:53 +0000378 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
379 ProtoLocs.reserve(NumProtocols);
380 for (unsigned I = 0; I != NumProtocols; ++I)
381 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
382 ID->setProtocolList(Protocols.data(), NumProtocols, ProtoLocs.data(),
383 *Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +0000384 unsigned NumIvars = Record[Idx++];
385 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
386 IVars.reserve(NumIvars);
387 for (unsigned I = 0; I != NumIvars; ++I)
388 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner487412d2009-04-27 05:27:42 +0000389 ID->setCategoryList(
390 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
391 ID->setForwardDecl(Record[Idx++]);
392 ID->setImplicitInterfaceDecl(Record[Idx++]);
393 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
394 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Argyrios Kyrtzidisea72f422009-07-18 00:33:23 +0000395 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner487412d2009-04-27 05:27:42 +0000396}
397
398void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
399 VisitFieldDecl(IVD);
400 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
Fariborz Jahanianaea8e1e2010-07-17 18:35:47 +0000401 bool synth = Record[Idx++];
402 IVD->setSynthesize(synth);
Chris Lattner487412d2009-04-27 05:27:42 +0000403}
404
405void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
406 VisitObjCContainerDecl(PD);
407 PD->setForwardDecl(Record[Idx++]);
408 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
409 unsigned NumProtoRefs = Record[Idx++];
410 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
411 ProtoRefs.reserve(NumProtoRefs);
412 for (unsigned I = 0; I != NumProtoRefs; ++I)
413 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor002b6712010-01-16 15:02:53 +0000414 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
415 ProtoLocs.reserve(NumProtoRefs);
416 for (unsigned I = 0; I != NumProtoRefs; ++I)
417 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
418 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
419 *Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +0000420}
421
422void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
423 VisitFieldDecl(FD);
424}
425
426void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
427 VisitDecl(CD);
428 unsigned NumClassRefs = Record[Idx++];
429 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
430 ClassRefs.reserve(NumClassRefs);
431 for (unsigned I = 0; I != NumClassRefs; ++I)
432 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Ted Kremenek9b124e12009-11-18 00:28:11 +0000433 llvm::SmallVector<SourceLocation, 16> SLocs;
434 SLocs.reserve(NumClassRefs);
435 for (unsigned I = 0; I != NumClassRefs; ++I)
436 SLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
437 CD->setClassList(*Reader.getContext(), ClassRefs.data(), SLocs.data(),
438 NumClassRefs);
Chris Lattner487412d2009-04-27 05:27:42 +0000439}
440
441void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
442 VisitDecl(FPD);
443 unsigned NumProtoRefs = Record[Idx++];
444 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
445 ProtoRefs.reserve(NumProtoRefs);
446 for (unsigned I = 0; I != NumProtoRefs; ++I)
447 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor002b6712010-01-16 15:02:53 +0000448 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
449 ProtoLocs.reserve(NumProtoRefs);
450 for (unsigned I = 0; I != NumProtoRefs; ++I)
451 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
452 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
453 *Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +0000454}
455
456void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
457 VisitObjCContainerDecl(CD);
458 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
459 unsigned NumProtoRefs = Record[Idx++];
460 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
461 ProtoRefs.reserve(NumProtoRefs);
462 for (unsigned I = 0; I != NumProtoRefs; ++I)
463 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor002b6712010-01-16 15:02:53 +0000464 llvm::SmallVector<SourceLocation, 16> ProtoLocs;
465 ProtoLocs.reserve(NumProtoRefs);
466 for (unsigned I = 0; I != NumProtoRefs; ++I)
467 ProtoLocs.push_back(SourceLocation::getFromRawEncoding(Record[Idx++]));
468 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, ProtoLocs.data(),
469 *Reader.getContext());
Chris Lattner487412d2009-04-27 05:27:42 +0000470 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor071676f2010-01-16 16:38:58 +0000471 CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
472 CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner487412d2009-04-27 05:27:42 +0000473}
474
475void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
476 VisitNamedDecl(CAD);
477 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
478}
479
480void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
481 VisitNamedDecl(D);
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000482 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redlc67764e2010-07-22 22:43:28 +0000483 D->setType(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner487412d2009-04-27 05:27:42 +0000484 // FIXME: stable encoding
485 D->setPropertyAttributes(
486 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
Fariborz Jahanian3bf0ded2010-06-22 23:20:40 +0000487 D->setPropertyAttributesAsWritten(
488 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
Chris Lattner487412d2009-04-27 05:27:42 +0000489 // FIXME: stable encoding
490 D->setPropertyImplementation(
491 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
492 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
493 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
494 D->setGetterMethodDecl(
495 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
496 D->setSetterMethodDecl(
497 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
498 D->setPropertyIvarDecl(
499 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
500}
501
502void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +0000503 VisitObjCContainerDecl(D);
Chris Lattner487412d2009-04-27 05:27:42 +0000504 D->setClassInterface(
505 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner487412d2009-04-27 05:27:42 +0000506}
507
508void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
509 VisitObjCImplDecl(D);
510 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
511}
512
513void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
514 VisitObjCImplDecl(D);
515 D->setSuperClass(
516 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Fariborz Jahanianc83726e2010-04-28 16:11:27 +0000517 // FIXME. Add reading of IvarInitializers and NumIvarInitializers.
Chris Lattner487412d2009-04-27 05:27:42 +0000518}
519
520
521void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
522 VisitDecl(D);
523 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
524 D->setPropertyDecl(
525 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
526 D->setPropertyIvarDecl(
527 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000528 // FIXME. read GetterCXXConstructor and SetterCXXAssignment
Chris Lattner487412d2009-04-27 05:27:42 +0000529}
530
531void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000532 VisitDeclaratorDecl(FD);
Chris Lattner487412d2009-04-27 05:27:42 +0000533 FD->setMutable(Record[Idx++]);
534 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000535 FD->setBitWidth(Reader.ReadExpr(Cursor));
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000536 if (!FD->getDeclName()) {
537 FieldDecl *Tmpl = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]));
538 if (Tmpl)
539 Reader.getContext()->setInstantiatedFromUnnamedFieldDecl(FD, Tmpl);
540 }
Chris Lattner487412d2009-04-27 05:27:42 +0000541}
542
543void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000544 VisitDeclaratorDecl(VD);
Chris Lattner487412d2009-04-27 05:27:42 +0000545 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
Douglas Gregorc4df4072010-04-19 22:54:31 +0000546 VD->setStorageClassAsWritten((VarDecl::StorageClass)Record[Idx++]);
Chris Lattner487412d2009-04-27 05:27:42 +0000547 VD->setThreadSpecified(Record[Idx++]);
548 VD->setCXXDirectInitializer(Record[Idx++]);
549 VD->setDeclaredInCondition(Record[Idx++]);
Douglas Gregor3f324d562010-05-03 18:51:14 +0000550 VD->setExceptionVariable(Record[Idx++]);
Douglas Gregor6fd1b182010-05-15 06:01:05 +0000551 VD->setNRVOVariable(Record[Idx++]);
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +0000552 VisitRedeclarable(VD);
Chris Lattner487412d2009-04-27 05:27:42 +0000553 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000554 VD->setInit(Reader.ReadExpr(Cursor));
Argyrios Kyrtzidiscdb8b3f2010-07-04 21:44:00 +0000555
556 if (Record[Idx++]) { // HasMemberSpecializationInfo.
557 VarDecl *Tmpl = cast<VarDecl>(Reader.GetDecl(Record[Idx++]));
558 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
559 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
560 Reader.getContext()->setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
561 }
Chris Lattner487412d2009-04-27 05:27:42 +0000562}
563
564void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
565 VisitVarDecl(PD);
566}
567
568void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
569 VisitVarDecl(PD);
570 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
John McCallf3cd6652010-03-12 18:31:32 +0000571 PD->setHasInheritedDefaultArg(Record[Idx++]);
Argyrios Kyrtzidisccde6a02010-07-04 21:44:07 +0000572 if (Record[Idx++]) // hasUninstantiatedDefaultArg.
Sebastian Redlc67764e2010-07-22 22:43:28 +0000573 PD->setUninstantiatedDefaultArg(Reader.ReadExpr(Cursor));
Chris Lattner487412d2009-04-27 05:27:42 +0000574}
575
Chris Lattner487412d2009-04-27 05:27:42 +0000576void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
577 VisitDecl(AD);
Sebastian Redlc67764e2010-07-22 22:43:28 +0000578 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(Cursor)));
Chris Lattner487412d2009-04-27 05:27:42 +0000579}
580
581void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
582 VisitDecl(BD);
Sebastian Redlc67764e2010-07-22 22:43:28 +0000583 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(Cursor)));
584 BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner487412d2009-04-27 05:27:42 +0000585 unsigned NumParams = Record[Idx++];
586 llvm::SmallVector<ParmVarDecl *, 16> Params;
587 Params.reserve(NumParams);
588 for (unsigned I = 0; I != NumParams; ++I)
589 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregord5058122010-02-11 01:19:42 +0000590 BD->setParams(Params.data(), NumParams);
Chris Lattner487412d2009-04-27 05:27:42 +0000591}
592
Chris Lattnerca025db2010-05-07 21:43:38 +0000593void PCHDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
594 VisitDecl(D);
595 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
596 D->setHasBraces(Record[Idx++]);
597}
598
599void PCHDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
600 VisitNamedDecl(D);
601 D->setLBracLoc(Reader.ReadSourceLocation(Record, Idx));
602 D->setRBracLoc(Reader.ReadSourceLocation(Record, Idx));
603 D->setNextNamespace(
604 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
605
Chris Lattnerca025db2010-05-07 21:43:38 +0000606 bool IsOriginal = Record[Idx++];
Argyrios Kyrtzidisdae2a162010-07-03 07:57:53 +0000607 D->OrigOrAnonNamespace.setInt(IsOriginal);
608 D->OrigOrAnonNamespace.setPointer(
Chris Lattnerca025db2010-05-07 21:43:38 +0000609 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
610}
611
612void PCHDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
613 VisitNamedDecl(D);
614
615 D->setAliasLoc(Reader.ReadSourceLocation(Record, Idx));
616 D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
617 D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
618 D->setTargetNameLoc(Reader.ReadSourceLocation(Record, Idx));
619 D->setAliasedNamespace(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
620}
621
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000622void PCHDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000623 VisitNamedDecl(D);
624 D->setUsingLocation(Reader.ReadSourceLocation(Record, Idx));
625 D->setNestedNameRange(Reader.ReadSourceRange(Record, Idx));
626 D->setTargetNestedNameDecl(Reader.ReadNestedNameSpecifier(Record, Idx));
627
628 // FIXME: It would probably be more efficient to read these into a vector
629 // and then re-cosntruct the shadow decl set over that vector since it
630 // would avoid existence checks.
631 unsigned NumShadows = Record[Idx++];
632 for(unsigned I = 0; I != NumShadows; ++I) {
Argyrios Kyrtzidis00dda6a2010-07-08 13:09:41 +0000633 // Avoid invariant checking of UsingDecl::addShadowDecl, the decl may still
634 // be initializing.
635 D->Shadows.insert(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerca025db2010-05-07 21:43:38 +0000636 }
637 D->setTypeName(Record[Idx++]);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000638 NamedDecl *Pattern = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
639 if (Pattern)
640 Reader.getContext()->setInstantiatedFromUsingDecl(D, Pattern);
Chris Lattnerca025db2010-05-07 21:43:38 +0000641}
642
Argyrios Kyrtzidis41d45622010-06-20 14:40:59 +0000643void PCHDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000644 VisitNamedDecl(D);
645 D->setTargetDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
646 D->setUsingDecl(cast<UsingDecl>(Reader.GetDecl(Record[Idx++])));
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000647 UsingShadowDecl *Pattern
648 = cast_or_null<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]));
649 if (Pattern)
650 Reader.getContext()->setInstantiatedFromUsingShadowDecl(D, Pattern);
Chris Lattnerca025db2010-05-07 21:43:38 +0000651}
652
653void PCHDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
654 VisitNamedDecl(D);
655 D->setNamespaceKeyLocation(Reader.ReadSourceLocation(Record, Idx));
656 D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
657 D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
658 D->setIdentLocation(Reader.ReadSourceLocation(Record, Idx));
659 D->setNominatedNamespace(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
660 D->setCommonAncestor(cast_or_null<DeclContext>(
661 Reader.GetDecl(Record[Idx++])));
662}
663
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000664void PCHDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000665 VisitValueDecl(D);
666 D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
667 D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
668 D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
669}
670
Argyrios Kyrtzidisbd8ac8c2010-06-30 08:49:30 +0000671void PCHDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattnerca025db2010-05-07 21:43:38 +0000672 UnresolvedUsingTypenameDecl *D) {
673 VisitTypeDecl(D);
674 D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
675 D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
676 D->setTypenameLoc(Reader.ReadSourceLocation(Record, Idx));
677 D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
678}
679
680void PCHDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000681 ASTContext &C = *Reader.getContext();
682
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000683 // We need to allocate the DefinitionData struct ahead of VisitRecordDecl
684 // so that the other CXXRecordDecls can get a pointer even when the owner
685 // is still initializing.
686 bool OwnsDefinitionData = false;
687 enum DataOwnership { Data_NoDefData, Data_Owner, Data_NotOwner };
688 switch ((DataOwnership)Record[Idx++]) {
689 default:
690 assert(0 && "Out of sync with PCHDeclWriter or messed up reading");
691 case Data_NoDefData:
692 break;
693 case Data_Owner:
694 OwnsDefinitionData = true;
695 D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
696 break;
697 case Data_NotOwner:
698 D->DefinitionData
699 = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))->DefinitionData;
700 break;
701 }
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000702
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000703 VisitRecordDecl(D);
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000704
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000705 if (OwnsDefinitionData) {
706 assert(D->DefinitionData);
707 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000708
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000709 Data.UserDeclaredConstructor = Record[Idx++];
710 Data.UserDeclaredCopyConstructor = Record[Idx++];
711 Data.UserDeclaredCopyAssignment = Record[Idx++];
712 Data.UserDeclaredDestructor = Record[Idx++];
713 Data.Aggregate = Record[Idx++];
714 Data.PlainOldData = Record[Idx++];
715 Data.Empty = Record[Idx++];
716 Data.Polymorphic = Record[Idx++];
717 Data.Abstract = Record[Idx++];
718 Data.HasTrivialConstructor = Record[Idx++];
719 Data.HasTrivialCopyConstructor = Record[Idx++];
720 Data.HasTrivialCopyAssignment = Record[Idx++];
721 Data.HasTrivialDestructor = Record[Idx++];
722 Data.ComputedVisibleConversions = Record[Idx++];
723 Data.DeclaredDefaultConstructor = Record[Idx++];
724 Data.DeclaredCopyConstructor = Record[Idx++];
725 Data.DeclaredCopyAssignment = Record[Idx++];
726 Data.DeclaredDestructor = Record[Idx++];
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000727
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000728 // setBases() is unsuitable since it may try to iterate the bases of an
Nick Lewycky19b9f952010-07-26 16:56:01 +0000729 // uninitialized base.
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000730 Data.NumBases = Record[Idx++];
731 Data.Bases = new(C) CXXBaseSpecifier [Data.NumBases];
732 for (unsigned i = 0; i != Data.NumBases; ++i)
Nick Lewycky19b9f952010-07-26 16:56:01 +0000733 Data.Bases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000734
735 // FIXME: Make VBases lazily computed when needed to avoid storing them.
736 Data.NumVBases = Record[Idx++];
737 Data.VBases = new(C) CXXBaseSpecifier [Data.NumVBases];
738 for (unsigned i = 0; i != Data.NumVBases; ++i)
Nick Lewycky19b9f952010-07-26 16:56:01 +0000739 Data.VBases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
Argyrios Kyrtzidis181431c2010-07-06 15:36:58 +0000740
741 Reader.ReadUnresolvedSet(Data.Conversions, Record, Idx);
742 Reader.ReadUnresolvedSet(Data.VisibleConversions, Record, Idx);
743 assert(Data.Definition && "Data.Definition should be already set!");
744 Data.FirstFriend
745 = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis2c2167a2010-07-02 11:55:32 +0000746 }
747
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000748 enum CXXRecKind {
749 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
750 };
751 switch ((CXXRecKind)Record[Idx++]) {
752 default:
753 assert(false && "Out of sync with PCHDeclWriter::VisitCXXRecordDecl?");
754 case CXXRecNotTemplate:
755 break;
756 case CXXRecTemplate:
757 D->setDescribedClassTemplate(
758 cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])));
759 break;
760 case CXXRecMemberSpecialization: {
761 CXXRecordDecl *RD = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]));
762 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
763 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
764 D->setInstantiationOfMemberClass(RD, TSK);
765 D->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
766 break;
767 }
768 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000769}
770
771void PCHDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000772 VisitFunctionDecl(D);
Argyrios Kyrtzidis6685e8a2010-07-04 21:44:35 +0000773 unsigned NumOverridenMethods = Record[Idx++];
774 while (NumOverridenMethods--) {
775 CXXMethodDecl *MD = cast<CXXMethodDecl>(Reader.GetDecl(Record[Idx++]));
776 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
777 // MD may be initializing.
778 Reader.getContext()->addOverriddenMethod(D, MD);
779 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000780}
781
782void PCHDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000783 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000784
785 D->IsExplicitSpecified = Record[Idx++];
786 D->ImplicitlyDefined = Record[Idx++];
787
788 unsigned NumInitializers = Record[Idx++];
789 D->NumBaseOrMemberInitializers = NumInitializers;
790 if (NumInitializers) {
791 ASTContext &C = *Reader.getContext();
792
793 D->BaseOrMemberInitializers
794 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
795 for (unsigned i=0; i != NumInitializers; ++i) {
Duncan Sands16143962010-07-06 18:19:40 +0000796 TypeSourceInfo *BaseClassInfo = 0;
797 bool IsBaseVirtual = false;
798 FieldDecl *Member = 0;
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000799
800 bool IsBaseInitializer = Record[Idx++];
801 if (IsBaseInitializer) {
Sebastian Redlc67764e2010-07-22 22:43:28 +0000802 BaseClassInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000803 IsBaseVirtual = Record[Idx++];
804 } else {
805 Member = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
806 }
807 SourceLocation MemberLoc = Reader.ReadSourceLocation(Record, Idx);
Sebastian Redlc67764e2010-07-22 22:43:28 +0000808 Expr *Init = Reader.ReadExpr(Cursor);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000809 FieldDecl *AnonUnionMember
810 = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]));
811 SourceLocation LParenLoc = Reader.ReadSourceLocation(Record, Idx);
812 SourceLocation RParenLoc = Reader.ReadSourceLocation(Record, Idx);
813 bool IsWritten = Record[Idx++];
814 unsigned SourceOrderOrNumArrayIndices;
815 llvm::SmallVector<VarDecl *, 8> Indices;
816 if (IsWritten) {
817 SourceOrderOrNumArrayIndices = Record[Idx++];
818 } else {
819 SourceOrderOrNumArrayIndices = Record[Idx++];
820 Indices.reserve(SourceOrderOrNumArrayIndices);
821 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
822 Indices.push_back(cast<VarDecl>(Reader.GetDecl(Record[Idx++])));
823 }
824
825 CXXBaseOrMemberInitializer *BOMInit;
826 if (IsBaseInitializer) {
827 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
828 IsBaseVirtual, LParenLoc,
829 Init, RParenLoc);
830 } else if (IsWritten) {
831 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
832 LParenLoc, Init, RParenLoc);
833 } else {
834 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
835 LParenLoc, Init, RParenLoc,
836 Indices.data(),
837 Indices.size());
838 }
839
840 BOMInit->setAnonUnionMember(AnonUnionMember);
841 D->BaseOrMemberInitializers[i] = BOMInit;
842 }
843 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000844}
845
846void PCHDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000847 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000848
849 D->ImplicitlyDefined = Record[Idx++];
850 D->OperatorDelete = cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
Chris Lattnerca025db2010-05-07 21:43:38 +0000851}
852
853void PCHDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattnerca025db2010-05-07 21:43:38 +0000854 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis33575162010-07-02 15:58:43 +0000855 D->IsExplicitSpecified = Record[Idx++];
Chris Lattnerca025db2010-05-07 21:43:38 +0000856}
857
Abramo Bagnarad7340582010-06-05 05:09:32 +0000858void PCHDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
859 VisitDecl(D);
860 D->setColonLoc(Reader.ReadSourceLocation(Record, Idx));
861}
862
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000863void PCHDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +0000864 VisitDecl(D);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000865 if (Record[Idx++])
Sebastian Redlc67764e2010-07-22 22:43:28 +0000866 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +0000867 else
868 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
869 D->NextFriend = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
870 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
871}
872
Chris Lattnerca025db2010-05-07 21:43:38 +0000873void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000874 VisitDecl(D);
875 unsigned NumParams = Record[Idx++];
876 D->NumParams = NumParams;
877 D->Params = new TemplateParameterList*[NumParams];
878 for (unsigned i = 0; i != NumParams; ++i)
879 D->Params[i] = Reader.ReadTemplateParameterList(Record, Idx);
880 if (Record[Idx++]) // HasFriendDecl
881 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
882 else
Sebastian Redlc67764e2010-07-22 22:43:28 +0000883 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +0000884 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
Chris Lattnerca025db2010-05-07 21:43:38 +0000885}
886
887void PCHDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000888 VisitNamedDecl(D);
889
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +0000890 NamedDecl *TemplatedDecl
891 = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +0000892 TemplateParameterList* TemplateParams
893 = Reader.ReadTemplateParameterList(Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000894 D->init(TemplatedDecl, TemplateParams);
Chris Lattnerca025db2010-05-07 21:43:38 +0000895}
896
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000897void PCHDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000898 VisitTemplateDecl(D);
899
Argyrios Kyrtzidisa95d0192010-07-05 10:38:01 +0000900 D->IdentifierNamespace = Record[Idx++];
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000901 RedeclarableTemplateDecl *PrevDecl =
902 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
903 assert((PrevDecl == 0 || PrevDecl->getKind() == D->getKind()) &&
904 "PrevDecl kind mismatch");
905 if (PrevDecl)
906 D->CommonOrPrev = PrevDecl;
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000907 if (PrevDecl == 0) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000908 if (RedeclarableTemplateDecl *RTD
909 = cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]))) {
910 assert(RTD->getKind() == D->getKind() &&
911 "InstantiatedFromMemberTemplate kind mismatch");
912 D->setInstantiatedFromMemberTemplateImpl(RTD);
913 if (Record[Idx++])
914 D->setMemberSpecialization();
915 }
Peter Collingbourne2bf3d242010-07-29 16:12:01 +0000916
917 RedeclarableTemplateDecl *LatestDecl =
918 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +0000919
920 // This decl is a first one and the latest declaration that it points to is
921 // in the same PCH. However, if this actually needs to point to a
922 // redeclaration in another chained PCH, we need to update it by checking
923 // the FirstLatestDeclIDs map which tracks this kind of decls.
924 assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
925 PCHReader::FirstLatestDeclIDMap::iterator I
926 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
927 if (I != Reader.FirstLatestDeclIDs.end()) {
928 Decl *NewLatest = Reader.GetDecl(I->second);
929 assert((LatestDecl->getLocation().isInvalid() ||
930 NewLatest->getLocation().isInvalid() ||
931 Reader.SourceMgr.isBeforeInTranslationUnit(
932 LatestDecl->getLocation(),
933 NewLatest->getLocation())) &&
934 "The new latest is supposed to come after the previous latest");
935 LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
936 }
937
Peter Collingbourne2bf3d242010-07-29 16:12:01 +0000938 assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
939 D->getCommonPtr()->Latest = LatestDecl;
Peter Collingbourne91b25b72010-07-29 16:11:51 +0000940 }
941}
942
943void PCHDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
944 VisitRedeclarableTemplateDecl(D);
945
946 if (D->getPreviousDeclaration() == 0) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000947 // This ClassTemplateDecl owns a CommonPtr; read it.
948
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000949 // FoldingSets are filled in VisitClassTemplateSpecializationDecl.
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000950 unsigned size = Record[Idx++];
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000951 while (size--)
952 cast<ClassTemplateSpecializationDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000953
954 size = Record[Idx++];
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000955 while (size--)
956 cast<ClassTemplatePartialSpecializationDecl>(
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000957 Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000958
959 // InjectedClassNameType is computed.
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +0000960 }
Chris Lattnerca025db2010-05-07 21:43:38 +0000961}
962
963void PCHDeclReader::VisitClassTemplateSpecializationDecl(
964 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +0000965 VisitCXXRecordDecl(D);
966
967 if (Decl *InstD = Reader.GetDecl(Record[Idx++])) {
968 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
969 D->setInstantiationOf(CTD);
970 } else {
971 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000972 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +0000973 D->setInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(InstD),
974 TemplArgs.data(), TemplArgs.size());
975 }
976 }
977
978 // Explicit info.
Sebastian Redlc67764e2010-07-22 22:43:28 +0000979 if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx)) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +0000980 D->setTypeAsWritten(TyInfo);
981 D->setExternLoc(Reader.ReadSourceLocation(Record, Idx));
982 D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx));
983 }
984
985 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redlc67764e2010-07-22 22:43:28 +0000986 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +0000987 D->initTemplateArgs(TemplArgs.data(), TemplArgs.size());
988 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
989 if (POI.isValid())
990 D->setPointOfInstantiation(POI);
991 D->setSpecializationKind((TemplateSpecializationKind)Record[Idx++]);
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000992
Argyrios Kyrtzidisc1624e92010-07-20 13:59:40 +0000993 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000994 ClassTemplateDecl *CanonPattern
995 = cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]));
996 if (ClassTemplatePartialSpecializationDecl *Partial
997 = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Argyrios Kyrtzidisf4cc7dc2010-07-12 21:41:31 +0000998 CanonPattern->getPartialSpecializations().InsertNode(Partial);
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +0000999 } else {
Argyrios Kyrtzidisf4cc7dc2010-07-12 21:41:31 +00001000 CanonPattern->getSpecializations().InsertNode(D);
Argyrios Kyrtzidis87040572010-07-09 21:11:43 +00001001 }
1002 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001003}
1004
1005void PCHDeclReader::VisitClassTemplatePartialSpecializationDecl(
1006 ClassTemplatePartialSpecializationDecl *D) {
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001007 VisitClassTemplateSpecializationDecl(D);
1008
1009 D->initTemplateParameters(Reader.ReadTemplateParameterList(Record, Idx));
1010
1011 TemplateArgumentListInfo ArgInfos;
1012 unsigned NumArgs = Record[Idx++];
1013 while (NumArgs--)
Sebastian Redlc67764e2010-07-22 22:43:28 +00001014 ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001015 D->initTemplateArgsAsWritten(ArgInfos);
1016
1017 D->setSequenceNumber(Record[Idx++]);
1018
1019 // These are read/set from/to the first declaration.
1020 if (D->getPreviousDeclaration() == 0) {
1021 D->setInstantiatedFromMember(
1022 cast_or_null<ClassTemplatePartialSpecializationDecl>(
1023 Reader.GetDecl(Record[Idx++])));
1024 if (Record[Idx++])
Argyrios Kyrtzidisdd2061b2010-07-09 21:11:35 +00001025 D->setMemberSpecialization();
Argyrios Kyrtzidis818c5db2010-06-23 13:48:30 +00001026 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001027}
1028
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001029void PCHDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001030 VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001031
Peter Collingbourne91b25b72010-07-29 16:11:51 +00001032 if (D->getPreviousDeclaration() == 0) {
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001033 // This FunctionTemplateDecl owns a CommonPtr; read it.
1034
Argyrios Kyrtzidis39fdf812010-07-06 15:37:09 +00001035 // Read the function specialization declarations.
1036 // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1037 // through the specialized FunctionDecl's setFunctionTemplateSpecialization.
1038 unsigned NumSpecs = Record[Idx++];
1039 while (NumSpecs--)
1040 Reader.GetDecl(Record[Idx++]);
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001041 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001042}
1043
1044void PCHDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001045 VisitTypeDecl(D);
1046
1047 D->setDeclaredWithTypename(Record[Idx++]);
1048 D->setParameterPack(Record[Idx++]);
1049
1050 bool Inherited = Record[Idx++];
Sebastian Redlc67764e2010-07-22 22:43:28 +00001051 TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis95c04ca2010-06-19 19:29:09 +00001052 D->setDefaultArgument(DefArg, Inherited);
Chris Lattnerca025db2010-05-07 21:43:38 +00001053}
1054
1055void PCHDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00001056 VisitVarDecl(D);
1057 // TemplateParmPosition.
1058 D->setDepth(Record[Idx++]);
1059 D->setPosition(Record[Idx++]);
1060 // Rest of NonTypeTemplateParmDecl.
1061 if (Record[Idx++]) {
Sebastian Redlc67764e2010-07-22 22:43:28 +00001062 Expr *DefArg = Reader.ReadExpr(Cursor);
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00001063 bool Inherited = Record[Idx++];
1064 D->setDefaultArgument(DefArg, Inherited);
1065 }
Chris Lattnerca025db2010-05-07 21:43:38 +00001066}
1067
1068void PCHDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00001069 VisitTemplateDecl(D);
1070 // TemplateParmPosition.
1071 D->setDepth(Record[Idx++]);
1072 D->setPosition(Record[Idx++]);
1073 // Rest of TemplateTemplateParmDecl.
Sebastian Redlc67764e2010-07-22 22:43:28 +00001074 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx);
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00001075 bool IsInherited = Record[Idx++];
1076 D->setDefaultArgument(Arg, IsInherited);
Chris Lattnerca025db2010-05-07 21:43:38 +00001077}
1078
1079void PCHDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00001080 VisitDecl(D);
Sebastian Redlc67764e2010-07-22 22:43:28 +00001081 D->AssertExpr = Reader.ReadExpr(Cursor);
1082 D->Message = cast<StringLiteral>(Reader.ReadExpr(Cursor));
Chris Lattnerca025db2010-05-07 21:43:38 +00001083}
1084
Mike Stump11289f42009-09-09 15:08:12 +00001085std::pair<uint64_t, uint64_t>
Chris Lattner487412d2009-04-27 05:27:42 +00001086PCHDeclReader::VisitDeclContext(DeclContext *DC) {
1087 uint64_t LexicalOffset = Record[Idx++];
1088 uint64_t VisibleOffset = Record[Idx++];
1089 return std::make_pair(LexicalOffset, VisibleOffset);
1090}
1091
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001092template <typename T>
1093void PCHDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1094 enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1095 RedeclKind Kind = (RedeclKind)Record[Idx++];
1096 switch (Kind) {
1097 default:
1098 assert(0 && "Out of sync with PCHDeclWriter::VisitRedeclarable or messed up"
1099 " reading");
1100 case NoRedeclaration:
1101 break;
1102 case PointsToPrevious:
1103 D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1104 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1105 break;
1106 case PointsToLatest:
1107 D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1108 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1109 break;
1110 }
1111
1112 assert(!(Kind == PointsToPrevious &&
1113 Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1114 Reader.FirstLatestDeclIDs.end()) &&
1115 "This decl is not first, it should not be in the map");
1116 if (Kind == PointsToPrevious)
1117 return;
1118
1119 // This decl is a first one and the latest declaration that it points to is in
1120 // the same PCH. However, if this actually needs to point to a redeclaration
1121 // in another chained PCH, we need to update it by checking the
1122 // FirstLatestDeclIDs map which tracks this kind of decls.
1123 assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1124 "Invalid ThisDeclID ?");
1125 PCHReader::FirstLatestDeclIDMap::iterator I
1126 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1127 if (I != Reader.FirstLatestDeclIDs.end()) {
1128 Decl *NewLatest = Reader.GetDecl(I->second);
1129 assert((D->getMostRecentDeclaration()->getLocation().isInvalid() ||
1130 NewLatest->getLocation().isInvalid() ||
1131 Reader.SourceMgr.isBeforeInTranslationUnit(
1132 D->getMostRecentDeclaration()->getLocation(),
1133 NewLatest->getLocation())) &&
1134 "The new latest is supposed to come after the previous latest");
1135 D->RedeclLink
1136 = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1137 }
1138}
1139
Chris Lattner487412d2009-04-27 05:27:42 +00001140//===----------------------------------------------------------------------===//
Chris Lattner8f63ab52009-04-27 06:01:06 +00001141// Attribute Reading
Chris Lattner487412d2009-04-27 05:27:42 +00001142//===----------------------------------------------------------------------===//
1143
Chris Lattner8f63ab52009-04-27 06:01:06 +00001144/// \brief Reads attributes from the current stream position.
Sebastian Redlc67764e2010-07-22 22:43:28 +00001145Attr *PCHReader::ReadAttributes(llvm::BitstreamCursor &DeclsCursor) {
Chris Lattner8f63ab52009-04-27 06:01:06 +00001146 unsigned Code = DeclsCursor.ReadCode();
Mike Stump11289f42009-09-09 15:08:12 +00001147 assert(Code == llvm::bitc::UNABBREV_RECORD &&
Chris Lattner8f63ab52009-04-27 06:01:06 +00001148 "Expected unabbreviated record"); (void)Code;
Mike Stump11289f42009-09-09 15:08:12 +00001149
Chris Lattner8f63ab52009-04-27 06:01:06 +00001150 RecordData Record;
1151 unsigned Idx = 0;
1152 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Mike Stump11289f42009-09-09 15:08:12 +00001153 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
Chris Lattner8f63ab52009-04-27 06:01:06 +00001154 (void)RecCode;
1155
1156#define SIMPLE_ATTR(Name) \
Alexis Hunt344393e2010-06-16 23:43:53 +00001157 case attr::Name: \
Chris Lattner8575daa2009-04-27 21:45:14 +00001158 New = ::new (*Context) Name##Attr(); \
Chris Lattner8f63ab52009-04-27 06:01:06 +00001159 break
1160
1161#define STRING_ATTR(Name) \
Alexis Hunt344393e2010-06-16 23:43:53 +00001162 case attr::Name: \
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001163 New = ::new (*Context) Name##Attr(*Context, ReadString(Record, Idx)); \
Chris Lattner8f63ab52009-04-27 06:01:06 +00001164 break
1165
1166#define UNSIGNED_ATTR(Name) \
Alexis Hunt344393e2010-06-16 23:43:53 +00001167 case attr::Name: \
Chris Lattner8575daa2009-04-27 21:45:14 +00001168 New = ::new (*Context) Name##Attr(Record[Idx++]); \
Chris Lattner8f63ab52009-04-27 06:01:06 +00001169 break
1170
1171 Attr *Attrs = 0;
1172 while (Idx < Record.size()) {
1173 Attr *New = 0;
Alexis Hunt344393e2010-06-16 23:43:53 +00001174 attr::Kind Kind = (attr::Kind)Record[Idx++];
Chris Lattner8f63ab52009-04-27 06:01:06 +00001175 bool IsInherited = Record[Idx++];
1176
1177 switch (Kind) {
Anton Korobeynikov55bcea12010-01-10 12:58:08 +00001178 default:
1179 assert(0 && "Unknown attribute!");
1180 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001181 STRING_ATTR(Alias);
Daniel Dunbarfc6507e2010-05-27 02:25:39 +00001182 SIMPLE_ATTR(AlignMac68k);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001183 UNSIGNED_ATTR(Aligned);
1184 SIMPLE_ATTR(AlwaysInline);
1185 SIMPLE_ATTR(AnalyzerNoReturn);
1186 STRING_ATTR(Annotate);
1187 STRING_ATTR(AsmLabel);
Alexis Hunt54a02542009-11-25 04:20:27 +00001188 SIMPLE_ATTR(BaseCheck);
Mike Stump11289f42009-09-09 15:08:12 +00001189
Alexis Hunt344393e2010-06-16 23:43:53 +00001190 case attr::Blocks:
Chris Lattner8575daa2009-04-27 21:45:14 +00001191 New = ::new (*Context) BlocksAttr(
Chris Lattner8f63ab52009-04-27 06:01:06 +00001192 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1193 break;
Mike Stump11289f42009-09-09 15:08:12 +00001194
Eli Friedmane4310c82009-11-09 18:38:53 +00001195 SIMPLE_ATTR(CDecl);
1196
Alexis Hunt344393e2010-06-16 23:43:53 +00001197 case attr::Cleanup:
Chris Lattner8575daa2009-04-27 21:45:14 +00001198 New = ::new (*Context) CleanupAttr(
Chris Lattner8f63ab52009-04-27 06:01:06 +00001199 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1200 break;
1201
1202 SIMPLE_ATTR(Const);
1203 UNSIGNED_ATTR(Constructor);
1204 SIMPLE_ATTR(DLLExport);
1205 SIMPLE_ATTR(DLLImport);
1206 SIMPLE_ATTR(Deprecated);
1207 UNSIGNED_ATTR(Destructor);
1208 SIMPLE_ATTR(FastCall);
Alexis Hunt96d5c762009-11-21 08:43:09 +00001209 SIMPLE_ATTR(Final);
Mike Stump11289f42009-09-09 15:08:12 +00001210
Alexis Hunt344393e2010-06-16 23:43:53 +00001211 case attr::Format: {
Chris Lattner8f63ab52009-04-27 06:01:06 +00001212 std::string Type = ReadString(Record, Idx);
1213 unsigned FormatIdx = Record[Idx++];
1214 unsigned FirstArg = Record[Idx++];
Ted Kremenek7f4945a2010-02-11 05:28:37 +00001215 New = ::new (*Context) FormatAttr(*Context, Type, FormatIdx, FirstArg);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001216 break;
1217 }
Mike Stump11289f42009-09-09 15:08:12 +00001218
Alexis Hunt344393e2010-06-16 23:43:53 +00001219 case attr::FormatArg: {
Fariborz Jahanianf1c25022009-05-20 17:41:43 +00001220 unsigned FormatIdx = Record[Idx++];
1221 New = ::new (*Context) FormatArgAttr(FormatIdx);
1222 break;
1223 }
Mike Stump11289f42009-09-09 15:08:12 +00001224
Alexis Hunt344393e2010-06-16 23:43:53 +00001225 case attr::Sentinel: {
Fariborz Jahanian027b8862009-05-13 18:09:35 +00001226 int sentinel = Record[Idx++];
1227 int nullPos = Record[Idx++];
1228 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
1229 break;
1230 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00001231
1232 SIMPLE_ATTR(GNUInline);
Alexis Hunt54a02542009-11-25 04:20:27 +00001233 SIMPLE_ATTR(Hiding);
Mike Stump11289f42009-09-09 15:08:12 +00001234
Alexis Hunt344393e2010-06-16 23:43:53 +00001235 case attr::IBAction:
Ted Kremenek06be9682010-02-17 02:37:45 +00001236 New = ::new (*Context) IBActionAttr();
1237 break;
1238
Alexis Hunt344393e2010-06-16 23:43:53 +00001239 case attr::IBOutlet:
Chris Lattner8575daa2009-04-27 21:45:14 +00001240 New = ::new (*Context) IBOutletAttr();
Chris Lattner8f63ab52009-04-27 06:01:06 +00001241 break;
1242
Alexis Hunt344393e2010-06-16 23:43:53 +00001243 case attr::IBOutletCollection: {
Ted Kremenek26bde772010-05-19 17:38:06 +00001244 ObjCInterfaceDecl *D =
1245 cast_or_null<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
1246 New = ::new (*Context) IBOutletCollectionAttr(D);
1247 break;
1248 }
1249
Ryan Flynn1f1fdc02009-08-09 20:07:29 +00001250 SIMPLE_ATTR(Malloc);
Mike Stump3722f582009-08-26 22:31:08 +00001251 SIMPLE_ATTR(NoDebug);
1252 SIMPLE_ATTR(NoInline);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001253 SIMPLE_ATTR(NoReturn);
1254 SIMPLE_ATTR(NoThrow);
Mike Stump11289f42009-09-09 15:08:12 +00001255
Alexis Hunt344393e2010-06-16 23:43:53 +00001256 case attr::NonNull: {
Chris Lattner8f63ab52009-04-27 06:01:06 +00001257 unsigned Size = Record[Idx++];
1258 llvm::SmallVector<unsigned, 16> ArgNums;
1259 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1260 Idx += Size;
Ted Kremenek510ee252010-02-11 07:31:47 +00001261 New = ::new (*Context) NonNullAttr(*Context, ArgNums.data(), Size);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001262 break;
1263 }
Mike Stump11289f42009-09-09 15:08:12 +00001264
Alexis Hunt344393e2010-06-16 23:43:53 +00001265 case attr::ReqdWorkGroupSize: {
Nate Begemanf2758702009-06-26 06:32:41 +00001266 unsigned X = Record[Idx++];
1267 unsigned Y = Record[Idx++];
1268 unsigned Z = Record[Idx++];
1269 New = ::new (*Context) ReqdWorkGroupSizeAttr(X, Y, Z);
1270 break;
1271 }
Chris Lattner8f63ab52009-04-27 06:01:06 +00001272
1273 SIMPLE_ATTR(ObjCException);
1274 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekd9c66632010-02-18 00:05:45 +00001275 SIMPLE_ATTR(CFReturnsNotRetained);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001276 SIMPLE_ATTR(CFReturnsRetained);
Ted Kremenekd9c66632010-02-18 00:05:45 +00001277 SIMPLE_ATTR(NSReturnsNotRetained);
Ted Kremenek9ecdfaf2009-05-09 02:44:38 +00001278 SIMPLE_ATTR(NSReturnsRetained);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001279 SIMPLE_ATTR(Overloadable);
Alexis Hunt54a02542009-11-25 04:20:27 +00001280 SIMPLE_ATTR(Override);
Anders Carlsson68e0b682009-08-08 18:23:56 +00001281 SIMPLE_ATTR(Packed);
Daniel Dunbar40130442010-05-27 01:12:46 +00001282 UNSIGNED_ATTR(MaxFieldAlignment);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001283 SIMPLE_ATTR(Pure);
1284 UNSIGNED_ATTR(Regparm);
1285 STRING_ATTR(Section);
1286 SIMPLE_ATTR(StdCall);
Douglas Gregora941dca2010-05-18 16:57:00 +00001287 SIMPLE_ATTR(ThisCall);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001288 SIMPLE_ATTR(TransparentUnion);
1289 SIMPLE_ATTR(Unavailable);
1290 SIMPLE_ATTR(Unused);
1291 SIMPLE_ATTR(Used);
Mike Stump11289f42009-09-09 15:08:12 +00001292
Alexis Hunt344393e2010-06-16 23:43:53 +00001293 case attr::Visibility:
Chris Lattner8575daa2009-04-27 21:45:14 +00001294 New = ::new (*Context) VisibilityAttr(
Chris Lattner8f63ab52009-04-27 06:01:06 +00001295 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
1296 break;
1297
1298 SIMPLE_ATTR(WarnUnusedResult);
1299 SIMPLE_ATTR(Weak);
Rafael Espindolac18086a2010-02-23 22:00:30 +00001300 SIMPLE_ATTR(WeakRef);
Chris Lattner8f63ab52009-04-27 06:01:06 +00001301 SIMPLE_ATTR(WeakImport);
1302 }
1303
1304 assert(New && "Unable to decode attribute?");
1305 New->setInherited(IsInherited);
1306 New->setNext(Attrs);
1307 Attrs = New;
1308 }
1309#undef UNSIGNED_ATTR
1310#undef STRING_ATTR
1311#undef SIMPLE_ATTR
1312
1313 // The list of attributes was built backwards. Reverse the list
1314 // before returning it.
1315 Attr *PrevAttr = 0, *NextAttr = 0;
1316 while (Attrs) {
1317 NextAttr = Attrs->getNext();
1318 Attrs->setNext(PrevAttr);
1319 PrevAttr = Attrs;
1320 Attrs = NextAttr;
1321 }
1322
1323 return PrevAttr;
1324}
1325
1326//===----------------------------------------------------------------------===//
1327// PCHReader Implementation
1328//===----------------------------------------------------------------------===//
Chris Lattner487412d2009-04-27 05:27:42 +00001329
1330/// \brief Note that we have loaded the declaration with the given
1331/// Index.
Mike Stump11289f42009-09-09 15:08:12 +00001332///
Chris Lattner487412d2009-04-27 05:27:42 +00001333/// This routine notes that this declaration has already been loaded,
1334/// so that future GetDecl calls will return this declaration rather
1335/// than trying to load a new declaration.
1336inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
1337 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
1338 DeclsLoaded[Index] = D;
1339}
1340
1341
1342/// \brief Determine whether the consumer will be interested in seeing
1343/// this declaration (via HandleTopLevelDecl).
1344///
1345/// This routine should return true for anything that might affect
1346/// code generation, e.g., inline function definitions, Objective-C
1347/// declarations with metadata, etc.
1348static bool isConsumerInterestedIn(Decl *D) {
Daniel Dunbar865c2a72009-09-17 03:06:44 +00001349 if (isa<FileScopeAsmDecl>(D))
1350 return true;
Chris Lattner487412d2009-04-27 05:27:42 +00001351 if (VarDecl *Var = dyn_cast<VarDecl>(D))
1352 return Var->isFileVarDecl() && Var->getInit();
1353 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1354 return Func->isThisDeclarationADefinition();
1355 return isa<ObjCProtocolDecl>(D);
1356}
1357
Sebastian Redl34627792010-07-20 22:46:15 +00001358/// \brief Get the correct cursor and offset for loading a type.
1359PCHReader::RecordLocation PCHReader::DeclCursorForIndex(unsigned Index) {
1360 PerFileData *F = 0;
1361 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1362 F = Chain[N - I - 1];
1363 if (Index < F->LocalNumDecls)
1364 break;
1365 Index -= F->LocalNumDecls;
1366 }
1367 assert(F && F->LocalNumDecls > Index && "Broken chain");
Sebastian Redlb2831db2010-07-20 22:55:31 +00001368 return RecordLocation(&F->DeclsCursor, F->DeclOffsets[Index]);
Sebastian Redl34627792010-07-20 22:46:15 +00001369}
1370
Chris Lattner487412d2009-04-27 05:27:42 +00001371/// \brief Read the declaration at the given offset from the PCH file.
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001372Decl *PCHReader::ReadDeclRecord(unsigned Index, pch::DeclID ID) {
Sebastian Redl34627792010-07-20 22:46:15 +00001373 RecordLocation Loc = DeclCursorForIndex(Index);
Sebastian Redlb2831db2010-07-20 22:55:31 +00001374 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Chris Lattner487412d2009-04-27 05:27:42 +00001375 // Keep track of where we are in the stream, then jump back there
1376 // after reading this declaration.
Chris Lattner1de76db2009-04-27 05:58:23 +00001377 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner487412d2009-04-27 05:27:42 +00001378
Argyrios Kyrtzidisd0795b22010-06-28 22:28:35 +00001379 ReadingKindTracker ReadingKind(Read_Decl, *this);
1380
Douglas Gregor1342e842009-07-06 18:54:52 +00001381 // Note that we are loading a declaration record.
Argyrios Kyrtzidisb24355a2010-07-30 10:03:16 +00001382 Deserializing ADecl(this);
Mike Stump11289f42009-09-09 15:08:12 +00001383
Sebastian Redl34627792010-07-20 22:46:15 +00001384 DeclsCursor.JumpToBit(Loc.second);
Chris Lattner487412d2009-04-27 05:27:42 +00001385 RecordData Record;
Chris Lattner1de76db2009-04-27 05:58:23 +00001386 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner487412d2009-04-27 05:27:42 +00001387 unsigned Idx = 0;
Argyrios Kyrtzidis839bbac2010-08-03 17:30:10 +00001388 PCHDeclReader Reader(*this, DeclsCursor, ID, Record, Idx);
Chris Lattner487412d2009-04-27 05:27:42 +00001389
Chris Lattner1de76db2009-04-27 05:58:23 +00001390 Decl *D = 0;
1391 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
Chris Lattner487412d2009-04-27 05:27:42 +00001392 case pch::DECL_ATTR:
1393 case pch::DECL_CONTEXT_LEXICAL:
1394 case pch::DECL_CONTEXT_VISIBLE:
1395 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1396 break;
Chris Lattner487412d2009-04-27 05:27:42 +00001397 case pch::DECL_TRANSLATION_UNIT:
1398 assert(Index == 0 && "Translation unit must be at index 0");
Chris Lattner8575daa2009-04-27 21:45:14 +00001399 D = Context->getTranslationUnitDecl();
Chris Lattner487412d2009-04-27 05:27:42 +00001400 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001401 case pch::DECL_TYPEDEF:
John McCall703a3f82009-10-24 08:00:42 +00001402 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001403 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001404 case pch::DECL_ENUM:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001405 D = EnumDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner487412d2009-04-27 05:27:42 +00001406 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001407 case pch::DECL_RECORD:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001408 D = RecordDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner487412d2009-04-27 05:27:42 +00001409 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001410 case pch::DECL_ENUM_CONSTANT:
Chris Lattner8575daa2009-04-27 21:45:14 +00001411 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner487412d2009-04-27 05:27:42 +00001412 0, llvm::APSInt());
1413 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001414 case pch::DECL_FUNCTION:
Mike Stump11289f42009-09-09 15:08:12 +00001415 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001416 QualType(), 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001417 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00001418 case pch::DECL_LINKAGE_SPEC:
1419 D = LinkageSpecDecl::Create(*Context, 0, SourceLocation(),
1420 (LinkageSpecDecl::LanguageIDs)0,
1421 false);
1422 break;
1423 case pch::DECL_NAMESPACE:
1424 D = NamespaceDecl::Create(*Context, 0, SourceLocation(), 0);
1425 break;
1426 case pch::DECL_NAMESPACE_ALIAS:
1427 D = NamespaceAliasDecl::Create(*Context, 0, SourceLocation(),
1428 SourceLocation(), 0, SourceRange(), 0,
1429 SourceLocation(), 0);
1430 break;
1431 case pch::DECL_USING:
1432 D = UsingDecl::Create(*Context, 0, SourceLocation(), SourceRange(),
1433 SourceLocation(), 0, DeclarationName(), false);
1434 break;
1435 case pch::DECL_USING_SHADOW:
1436 D = UsingShadowDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1437 break;
1438 case pch::DECL_USING_DIRECTIVE:
1439 D = UsingDirectiveDecl::Create(*Context, 0, SourceLocation(),
1440 SourceLocation(), SourceRange(), 0,
1441 SourceLocation(), 0, 0);
1442 break;
1443 case pch::DECL_UNRESOLVED_USING_VALUE:
1444 D = UnresolvedUsingValueDecl::Create(*Context, 0, SourceLocation(),
1445 SourceRange(), 0, SourceLocation(),
1446 DeclarationName());
1447 break;
1448 case pch::DECL_UNRESOLVED_USING_TYPENAME:
1449 D = UnresolvedUsingTypenameDecl::Create(*Context, 0, SourceLocation(),
1450 SourceLocation(), SourceRange(),
1451 0, SourceLocation(),
1452 DeclarationName());
1453 break;
1454 case pch::DECL_CXX_RECORD:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001455 D = CXXRecordDecl::Create(*Context, Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001456 break;
1457 case pch::DECL_CXX_METHOD:
1458 D = CXXMethodDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
1459 QualType(), 0);
1460 break;
1461 case pch::DECL_CXX_CONSTRUCTOR:
1462 D = CXXConstructorDecl::Create(*Context, Decl::EmptyShell());
1463 break;
1464 case pch::DECL_CXX_DESTRUCTOR:
1465 D = CXXDestructorDecl::Create(*Context, Decl::EmptyShell());
1466 break;
1467 case pch::DECL_CXX_CONVERSION:
1468 D = CXXConversionDecl::Create(*Context, Decl::EmptyShell());
1469 break;
Abramo Bagnarad7340582010-06-05 05:09:32 +00001470 case pch::DECL_ACCESS_SPEC:
1471 D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(),
1472 SourceLocation());
1473 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00001474 case pch::DECL_FRIEND:
Argyrios Kyrtzidis74d28bd2010-06-29 22:47:00 +00001475 D = FriendDecl::Create(*Context, Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001476 break;
1477 case pch::DECL_FRIEND_TEMPLATE:
Argyrios Kyrtzidis165b5812010-07-22 16:04:10 +00001478 D = FriendTemplateDecl::Create(*Context, Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001479 break;
Chris Lattnerca025db2010-05-07 21:43:38 +00001480 case pch::DECL_CLASS_TEMPLATE:
Argyrios Kyrtzidisa35c8e42010-06-21 10:57:41 +00001481 D = ClassTemplateDecl::Create(*Context, 0, SourceLocation(),
1482 DeclarationName(), 0, 0, 0);
Chris Lattnerca025db2010-05-07 21:43:38 +00001483 break;
1484 case pch::DECL_CLASS_TEMPLATE_SPECIALIZATION:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001485 D = ClassTemplateSpecializationDecl::Create(*Context, Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001486 break;
1487 case pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001488 D = ClassTemplatePartialSpecializationDecl::Create(*Context,
1489 Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001490 break;
1491 case pch::DECL_FUNCTION_TEMPLATE:
Argyrios Kyrtzidis69da4a82010-06-22 09:55:07 +00001492 D = FunctionTemplateDecl::Create(*Context, 0, SourceLocation(),
1493 DeclarationName(), 0, 0);
Chris Lattnerca025db2010-05-07 21:43:38 +00001494 break;
1495 case pch::DECL_TEMPLATE_TYPE_PARM:
Argyrios Kyrtzidis39f0e302010-07-02 11:54:55 +00001496 D = TemplateTypeParmDecl::Create(*Context, Decl::EmptyShell());
Chris Lattnerca025db2010-05-07 21:43:38 +00001497 break;
1498 case pch::DECL_NON_TYPE_TEMPLATE_PARM:
Argyrios Kyrtzidisb1d38e32010-06-25 16:25:09 +00001499 D = NonTypeTemplateParmDecl::Create(*Context, 0, SourceLocation(), 0,0,0,
1500 QualType(),0);
Chris Lattnerca025db2010-05-07 21:43:38 +00001501 break;
1502 case pch::DECL_TEMPLATE_TEMPLATE_PARM:
Argyrios Kyrtzidis9f2d24a2010-07-08 17:12:57 +00001503 D = TemplateTemplateParmDecl::Create(*Context, 0, SourceLocation(),0,0,0,0);
Chris Lattnerca025db2010-05-07 21:43:38 +00001504 break;
1505 case pch::DECL_STATIC_ASSERT:
Argyrios Kyrtzidis2d8891c2010-07-22 17:28:12 +00001506 D = StaticAssertDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattnerca025db2010-05-07 21:43:38 +00001507 break;
1508
Chris Lattner8f63ab52009-04-27 06:01:06 +00001509 case pch::DECL_OBJC_METHOD:
Mike Stump11289f42009-09-09 15:08:12 +00001510 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
Douglas Gregor12852d92010-03-08 14:59:44 +00001511 Selector(), QualType(), 0, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001512 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001513 case pch::DECL_OBJC_INTERFACE:
Chris Lattner8575daa2009-04-27 21:45:14 +00001514 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001515 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001516 case pch::DECL_OBJC_IVAR:
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001517 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Chris Lattner487412d2009-04-27 05:27:42 +00001518 ObjCIvarDecl::None);
1519 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001520 case pch::DECL_OBJC_PROTOCOL:
Chris Lattner8575daa2009-04-27 21:45:14 +00001521 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001522 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001523 case pch::DECL_OBJC_AT_DEFS_FIELD:
Mike Stump11289f42009-09-09 15:08:12 +00001524 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner487412d2009-04-27 05:27:42 +00001525 QualType(), 0);
1526 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001527 case pch::DECL_OBJC_CLASS:
Chris Lattner8575daa2009-04-27 21:45:14 +00001528 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001529 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001530 case pch::DECL_OBJC_FORWARD_PROTOCOL:
Chris Lattner8575daa2009-04-27 21:45:14 +00001531 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001532 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001533 case pch::DECL_OBJC_CATEGORY:
Douglas Gregor071676f2010-01-16 16:38:58 +00001534 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(),
1535 SourceLocation(), SourceLocation(), 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001536 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001537 case pch::DECL_OBJC_CATEGORY_IMPL:
Chris Lattner8575daa2009-04-27 21:45:14 +00001538 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001539 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001540 case pch::DECL_OBJC_IMPLEMENTATION:
Chris Lattner8575daa2009-04-27 21:45:14 +00001541 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001542 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001543 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
Chris Lattner8575daa2009-04-27 21:45:14 +00001544 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001545 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001546 case pch::DECL_OBJC_PROPERTY:
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +00001547 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
John McCall339bb662010-06-04 20:50:08 +00001548 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001549 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001550 case pch::DECL_OBJC_PROPERTY_IMPL:
Chris Lattner8575daa2009-04-27 21:45:14 +00001551 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
Mike Stump11289f42009-09-09 15:08:12 +00001552 SourceLocation(), 0,
Chris Lattner487412d2009-04-27 05:27:42 +00001553 ObjCPropertyImplDecl::Dynamic, 0);
1554 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001555 case pch::DECL_FIELD:
Mike Stump11289f42009-09-09 15:08:12 +00001556 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
Argyrios Kyrtzidis6032ef12009-08-21 00:31:54 +00001557 false);
Chris Lattner487412d2009-04-27 05:27:42 +00001558 break;
Chris Lattner487412d2009-04-27 05:27:42 +00001559 case pch::DECL_VAR:
Argyrios Kyrtzidis60ed5602009-08-19 01:27:57 +00001560 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001561 VarDecl::None, VarDecl::None);
Chris Lattner487412d2009-04-27 05:27:42 +00001562 break;
1563
1564 case pch::DECL_IMPLICIT_PARAM:
Chris Lattner8575daa2009-04-27 21:45:14 +00001565 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner487412d2009-04-27 05:27:42 +00001566 break;
1567
Chris Lattner8f63ab52009-04-27 06:01:06 +00001568 case pch::DECL_PARM_VAR:
Mike Stump11289f42009-09-09 15:08:12 +00001569 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Douglas Gregorc4df4072010-04-19 22:54:31 +00001570 VarDecl::None, VarDecl::None, 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001571 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001572 case pch::DECL_FILE_SCOPE_ASM:
Chris Lattner8575daa2009-04-27 21:45:14 +00001573 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner487412d2009-04-27 05:27:42 +00001574 break;
Chris Lattner8f63ab52009-04-27 06:01:06 +00001575 case pch::DECL_BLOCK:
Chris Lattner8575daa2009-04-27 21:45:14 +00001576 D = BlockDecl::Create(*Context, 0, SourceLocation());
Chris Lattner487412d2009-04-27 05:27:42 +00001577 break;
1578 }
Chris Lattner487412d2009-04-27 05:27:42 +00001579
1580 assert(D && "Unknown declaration reading PCH file");
Chris Lattner8f63ab52009-04-27 06:01:06 +00001581 LoadedDecl(Index, D);
1582 Reader.Visit(D);
Chris Lattner487412d2009-04-27 05:27:42 +00001583
1584 // If this declaration is also a declaration context, get the
1585 // offsets for its tables of lexical and visible declarations.
1586 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1587 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1588 if (Offsets.first || Offsets.second) {
1589 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1590 DC->setHasExternalVisibleStorage(Offsets.second != 0);
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001591 DeclContextInfo Info;
1592 if (ReadDeclContextStorage(DeclsCursor, Offsets, Info))
1593 return 0;
Sebastian Redl4b1f4902010-07-27 18:24:41 +00001594 DeclContextInfos &Infos = DeclContextOffsets[DC];
1595 // Reading the TU will happen after reading its update blocks, so we need
1596 // to make sure we insert in front. For all other contexts, the vector
1597 // is empty here anyway, so there's no loss in efficiency.
1598 Infos.insert(Infos.begin(), Info);
Chris Lattner487412d2009-04-27 05:27:42 +00001599 }
1600 }
1601 assert(Idx == Record.size());
1602
1603 // If we have deserialized a declaration that has a definition the
Argyrios Kyrtzidis903ccd62010-07-07 15:46:26 +00001604 // AST consumer might need to know about, queue it.
1605 // We don't pass it to the consumer immediately because we may be in recursive
1606 // loading, and some declarations may still be initializing.
1607 if (isConsumerInterestedIn(D))
1608 InterestingDecls.push_back(D);
Chris Lattner487412d2009-04-27 05:27:42 +00001609
1610 return D;
1611}
Sebastian Redl66c5eef2010-07-27 00:17:23 +00001612
1613bool PCHReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
1614 const std::pair<uint64_t, uint64_t> &Offsets,
1615 DeclContextInfo &Info) {
1616 SavedStreamPosition SavedPosition(Cursor);
1617 // First the lexical decls.
1618 if (Offsets.first != 0) {
1619 Cursor.JumpToBit(Offsets.first);
1620
1621 RecordData Record;
1622 const char *Blob;
1623 unsigned BlobLen;
1624 unsigned Code = Cursor.ReadCode();
1625 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
1626 if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
1627 Error("Expected lexical block");
1628 return true;
1629 }
1630
1631 Info.LexicalDecls = reinterpret_cast<const pch::DeclID*>(Blob);
1632 Info.NumLexicalDecls = BlobLen / sizeof(pch::DeclID);
1633 } else {
1634 Info.LexicalDecls = 0;
1635 Info.NumLexicalDecls = 0;
1636 }
1637
1638 // Now the visible decls.
1639 Info.Stream = &Cursor;
1640 Info.OffsetToVisibleDecls = Offsets.second;
1641
1642 return false;
1643}