blob: 4b6bde7b19dc18bf67d6069a78994ef8fa8b6613 [file] [log] [blame]
Chris Lattner698f9252009-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 Lattner6ad9ac02010-05-07 21:43:38 +000020#include "clang/AST/DeclCXX.h"
21#include "clang/AST/DeclTemplate.h"
Chris Lattner698f9252009-04-27 05:27:42 +000022#include "clang/AST/Expr.h"
23using namespace clang;
24
Chris Lattner698f9252009-04-27 05:27:42 +000025
26//===----------------------------------------------------------------------===//
27// Declaration deserialization
28//===----------------------------------------------------------------------===//
29
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +000030namespace clang {
Chris Lattner698f9252009-04-27 05:27:42 +000031 class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> {
32 PCHReader &Reader;
Sebastian Redl577d4792010-07-22 22:43:28 +000033 llvm::BitstreamCursor &Cursor;
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +000034 const pch::DeclID ThisDeclID;
Chris Lattner698f9252009-04-27 05:27:42 +000035 const PCHReader::RecordData &Record;
36 unsigned &Idx;
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +000037 pch::TypeID TypeIDForTypeDecl;
Chris Lattner698f9252009-04-27 05:27:42 +000038
Sebastian Redl577d4792010-07-22 22:43:28 +000039 uint64_t GetCurrentCursorOffset();
40
Chris Lattner698f9252009-04-27 05:27:42 +000041 public:
Sebastian Redl577d4792010-07-22 22:43:28 +000042 PCHDeclReader(PCHReader &Reader, llvm::BitstreamCursor &Cursor,
Argyrios Kyrtzidisa8650052010-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 Lattner698f9252009-04-27 05:27:42 +000047
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +000048 void Visit(Decl *D);
49
Chris Lattner698f9252009-04-27 05:27:42 +000050 void VisitDecl(Decl *D);
51 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
52 void VisitNamedDecl(NamedDecl *ND);
Douglas Gregor0cef4832010-02-21 18:22:14 +000053 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000054 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
55 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner698f9252009-04-27 05:27:42 +000056 void VisitTypeDecl(TypeDecl *TD);
57 void VisitTypedefDecl(TypedefDecl *TD);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000058 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Chris Lattner698f9252009-04-27 05:27:42 +000059 void VisitTagDecl(TagDecl *TD);
60 void VisitEnumDecl(EnumDecl *ED);
61 void VisitRecordDecl(RecordDecl *RD);
Chris Lattner6ad9ac02010-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 Lattner698f9252009-04-27 05:27:42 +000068 void VisitValueDecl(ValueDecl *VD);
69 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000070 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +000071 void VisitDeclaratorDecl(DeclaratorDecl *DD);
Chris Lattner698f9252009-04-27 05:27:42 +000072 void VisitFunctionDecl(FunctionDecl *FD);
Chris Lattner6ad9ac02010-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 Lattner698f9252009-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 Lattner6ad9ac02010-05-07 21:43:38 +000081 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
82 void VisitTemplateDecl(TemplateDecl *D);
Peter Collingbourne9eabeba2010-07-29 16:11:51 +000083 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000084 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +000085 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000086 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +000087 void VisitUsingDecl(UsingDecl *D);
88 void VisitUsingShadowDecl(UsingShadowDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000089 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Chris Lattner698f9252009-04-27 05:27:42 +000090 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
Abramo Bagnara6206d532010-06-05 05:09:32 +000091 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +000092 void VisitFriendDecl(FriendDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000093 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
94 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner698f9252009-04-27 05:27:42 +000095 void VisitBlockDecl(BlockDecl *BD);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000096
Chris Lattner698f9252009-04-27 05:27:42 +000097 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +000098 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000099
Sean Hunt9a555912010-05-30 07:21:58 +0000100 // FIXME: Reorder according to DeclNodes.td?
Chris Lattner698f9252009-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 Redl577d4792010-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 Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000132void PCHDeclReader::Visit(Decl *D) {
133 DeclVisitor<PCHDeclReader, void>::Visit(D);
134
Argyrios Kyrtzidis91468322010-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 Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000137 TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr());
Argyrios Kyrtzidis91468322010-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 Redl577d4792010-07-22 22:43:28 +0000141 FD->setLazyBody(GetCurrentCursorOffset());
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000142 }
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000143}
144
Chris Lattner698f9252009-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 Redl577d4792010-07-22 22:43:28 +0000152 D->initAttrs(Reader.ReadAttributes(Cursor));
Chris Lattner698f9252009-04-27 05:27:42 +0000153 D->setImplicit(Record[Idx++]);
Douglas Gregore0762c92009-06-19 23:52:42 +0000154 D->setUsed(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000155 D->setAccess((AccessSpecifier)Record[Idx++]);
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000156 D->setPCHLevel(Record[Idx++] + 1);
Chris Lattner698f9252009-04-27 05:27:42 +0000157}
158
159void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
160 VisitDecl(TU);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000161 TU->setAnonymousNamespace(
162 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner698f9252009-04-27 05:27:42 +0000163}
164
165void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
166 VisitDecl(ND);
Mike Stump1eb44332009-09-09 15:08:12 +0000167 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
Chris Lattner698f9252009-04-27 05:27:42 +0000168}
169
170void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
171 VisitNamedDecl(TD);
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000172 // Delay type reading until after we have fully initialized the decl.
173 TypeIDForTypeDecl = Record[Idx++];
Chris Lattner698f9252009-04-27 05:27:42 +0000174}
175
176void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000177 VisitTypeDecl(TD);
Sebastian Redl577d4792010-07-22 22:43:28 +0000178 TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner698f9252009-04-27 05:27:42 +0000179}
180
181void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
182 VisitTypeDecl(TD);
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000183 TD->IdentifierNamespace = Record[Idx++];
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +0000184 VisitRedeclarable(TD);
Chris Lattner698f9252009-04-27 05:27:42 +0000185 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
186 TD->setDefinition(Record[Idx++]);
Douglas Gregorb37b6482010-02-12 17:40:34 +0000187 TD->setEmbeddedInDeclarator(Record[Idx++]);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000188 TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000189 TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
John McCallb6217662010-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 Lattner698f9252009-04-27 05:27:42 +0000193}
194
195void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
196 VisitTagDecl(ED);
197 ED->setIntegerType(Reader.GetType(Record[Idx++]));
John McCall842aef82009-12-09 09:09:27 +0000198 ED->setPromotionType(Reader.GetType(Record[Idx++]));
John McCall1b5a6182010-05-06 08:49:23 +0000199 ED->setNumPositiveBits(Record[Idx++]);
200 ED->setNumNegativeBits(Record[Idx++]);
Argyrios Kyrtzidis74228272010-07-06 15:36:48 +0000201 ED->setInstantiationOfMemberEnum(
202 cast_or_null<EnumDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner698f9252009-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 Jahanian643b7df2009-07-08 16:37:44 +0000209 RD->setHasObjectMember(Record[Idx++]);
Chris Lattner698f9252009-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 Redl577d4792010-07-22 22:43:28 +0000220 ECD->setInitExpr(Reader.ReadExpr(Cursor));
Chris Lattner698f9252009-04-27 05:27:42 +0000221 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
222}
223
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000224void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) {
225 VisitValueDecl(DD);
Sebastian Redl577d4792010-07-22 22:43:28 +0000226 TypeSourceInfo *TInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
John McCalla93c9342009-12-07 02:54:59 +0000227 if (TInfo)
228 DD->setTypeSourceInfo(TInfo);
John McCallb6217662010-03-15 10:12:16 +0000229 // FIXME: read optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000230}
231
Chris Lattner698f9252009-04-27 05:27:42 +0000232void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000233 VisitDeclaratorDecl(FD);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000234
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000235 FD->IdentifierNamespace = Record[Idx++];
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000236 switch ((FunctionDecl::TemplatedKind)Record[Idx++]) {
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +0000237 default: assert(false && "Unhandled TemplatedKind!");
238 break;
Argyrios Kyrtzidisf511ba62010-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 Kyrtzidisf511ba62010-06-22 09:55:07 +0000259 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redl577d4792010-07-22 22:43:28 +0000260 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000261
262 // Template args as written.
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000263 llvm::SmallVector<TemplateArgumentLoc, 8> TemplArgLocs;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000264 SourceLocation LAngleLoc, RAngleLoc;
Argyrios Kyrtzidis5efb06f2010-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 Redl577d4792010-07-22 22:43:28 +0000269 TemplArgLocs.push_back(
270 Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000271
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000272 LAngleLoc = Reader.ReadSourceLocation(Record, Idx);
273 RAngleLoc = Reader.ReadSourceLocation(Record, Idx);
274 }
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +0000275
276 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000277
Argyrios Kyrtzidis2c853e42010-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 Kyrtzidisdc767e32010-06-28 09:31:34 +0000284 break;
Argyrios Kyrtzidisf511ba62010-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 Redl577d4792010-07-22 22:43:28 +0000297 TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Cursor,Record, Idx));
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +0000298 TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx));
299 TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx));
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000300
301 FD->setDependentTemplateSpecialization(*Reader.getContext(),
302 TemplDecls, TemplArgs);
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +0000303 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000304 }
305 }
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000306
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000307 // FunctionDecl's body is handled last at PCHReaderDecl::Visit,
308 // after everything else is read.
309
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +0000310 VisitRedeclarable(FD);
Argyrios Kyrtzidis5efb06f2010-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 Lattner6ad9ac02010-05-07 21:43:38 +0000324 // Read in the parameters.
Chris Lattner698f9252009-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 Gregor838db382010-02-11 01:19:42 +0000330 FD->setParams(Params.data(), NumParams);
Chris Lattner698f9252009-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 Redl577d4792010-07-22 22:43:28 +0000338 MD->setBody(Reader.ReadStmt(Cursor));
Chris Lattner698f9252009-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 Jahanian3fe10412010-07-22 18:24:20 +0000345 MD->setDefined(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000346 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
347 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000348 MD->setNumSelectorArgs(unsigned(Record[Idx++]));
Chris Lattner698f9252009-04-27 05:27:42 +0000349 MD->setResultType(Reader.GetType(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +0000350 MD->setResultTypeSourceInfo(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner698f9252009-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 Jahanian4ecb25f2010-04-09 15:40:42 +0000357 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams,
358 NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000359}
360
361void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
362 VisitNamedDecl(CD);
Ted Kremenek782f2f52010-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 Lattner698f9252009-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 Gregor18df52b2010-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 Lattner698f9252009-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 Lattner698f9252009-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 Kyrtzidisc999f1f2009-07-18 00:33:23 +0000395 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner698f9252009-04-27 05:27:42 +0000396}
397
398void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
399 VisitFieldDecl(IVD);
400 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
Fariborz Jahanianac0021b2010-07-17 18:35:47 +0000401 bool synth = Record[Idx++];
402 IVD->setSynthesize(synth);
Chris Lattner698f9252009-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 Gregor18df52b2010-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 Lattner698f9252009-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 Kremenek321c22f2009-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 Lattner698f9252009-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 Gregor18df52b2010-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 Lattner698f9252009-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 Gregor18df52b2010-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 Lattner698f9252009-04-27 05:27:42 +0000470 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor3db211b2010-01-16 16:38:58 +0000471 CD->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
472 CD->setCategoryNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner698f9252009-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 Jahaniand0502402010-01-21 17:36:00 +0000482 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Sebastian Redl577d4792010-07-22 22:43:28 +0000483 D->setType(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner698f9252009-04-27 05:27:42 +0000484 // FIXME: stable encoding
485 D->setPropertyAttributes(
486 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000487 D->setPropertyAttributesAsWritten(
488 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
Chris Lattner698f9252009-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 Kyrtzidisaecae622009-07-27 19:04:32 +0000503 VisitObjCContainerDecl(D);
Chris Lattner698f9252009-04-27 05:27:42 +0000504 D->setClassInterface(
505 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner698f9252009-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 Jahaniane4498c62010-04-28 16:11:27 +0000517 // FIXME. Add reading of IvarInitializers and NumIvarInitializers.
Chris Lattner698f9252009-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 Jahanian17cb3262010-05-05 21:52:17 +0000528 // FIXME. read GetterCXXConstructor and SetterCXXAssignment
Chris Lattner698f9252009-04-27 05:27:42 +0000529}
530
531void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000532 VisitDeclaratorDecl(FD);
Chris Lattner698f9252009-04-27 05:27:42 +0000533 FD->setMutable(Record[Idx++]);
534 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +0000535 FD->setBitWidth(Reader.ReadExpr(Cursor));
Argyrios Kyrtzidisc91e9f42010-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 Lattner698f9252009-04-27 05:27:42 +0000541}
542
543void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000544 VisitDeclaratorDecl(VD);
Chris Lattner698f9252009-04-27 05:27:42 +0000545 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
Douglas Gregor16573fa2010-04-19 22:54:31 +0000546 VD->setStorageClassAsWritten((VarDecl::StorageClass)Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000547 VD->setThreadSpecified(Record[Idx++]);
548 VD->setCXXDirectInitializer(Record[Idx++]);
Douglas Gregor324b54d2010-05-03 18:51:14 +0000549 VD->setExceptionVariable(Record[Idx++]);
Douglas Gregor5077c382010-05-15 06:01:05 +0000550 VD->setNRVOVariable(Record[Idx++]);
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +0000551 VisitRedeclarable(VD);
Chris Lattner698f9252009-04-27 05:27:42 +0000552 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +0000553 VD->setInit(Reader.ReadExpr(Cursor));
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000554
555 if (Record[Idx++]) { // HasMemberSpecializationInfo.
556 VarDecl *Tmpl = cast<VarDecl>(Reader.GetDecl(Record[Idx++]));
557 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
558 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
559 Reader.getContext()->setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI);
560 }
Chris Lattner698f9252009-04-27 05:27:42 +0000561}
562
563void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
564 VisitVarDecl(PD);
565}
566
567void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
568 VisitVarDecl(PD);
569 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
John McCallbf73b352010-03-12 18:31:32 +0000570 PD->setHasInheritedDefaultArg(Record[Idx++]);
Argyrios Kyrtzidis691d77f2010-07-04 21:44:07 +0000571 if (Record[Idx++]) // hasUninstantiatedDefaultArg.
Sebastian Redl577d4792010-07-22 22:43:28 +0000572 PD->setUninstantiatedDefaultArg(Reader.ReadExpr(Cursor));
Chris Lattner698f9252009-04-27 05:27:42 +0000573}
574
Chris Lattner698f9252009-04-27 05:27:42 +0000575void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
576 VisitDecl(AD);
Sebastian Redl577d4792010-07-22 22:43:28 +0000577 AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr(Cursor)));
Chris Lattner698f9252009-04-27 05:27:42 +0000578}
579
580void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
581 VisitDecl(BD);
Sebastian Redl577d4792010-07-22 22:43:28 +0000582 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt(Cursor)));
583 BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Cursor, Record, Idx));
Chris Lattner698f9252009-04-27 05:27:42 +0000584 unsigned NumParams = Record[Idx++];
585 llvm::SmallVector<ParmVarDecl *, 16> Params;
586 Params.reserve(NumParams);
587 for (unsigned I = 0; I != NumParams; ++I)
588 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor838db382010-02-11 01:19:42 +0000589 BD->setParams(Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000590}
591
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000592void PCHDeclReader::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
593 VisitDecl(D);
594 D->setLanguage((LinkageSpecDecl::LanguageIDs)Record[Idx++]);
595 D->setHasBraces(Record[Idx++]);
596}
597
598void PCHDeclReader::VisitNamespaceDecl(NamespaceDecl *D) {
599 VisitNamedDecl(D);
600 D->setLBracLoc(Reader.ReadSourceLocation(Record, Idx));
601 D->setRBracLoc(Reader.ReadSourceLocation(Record, Idx));
602 D->setNextNamespace(
603 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
604
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000605 bool IsOriginal = Record[Idx++];
Argyrios Kyrtzidisa038c1d2010-07-03 07:57:53 +0000606 D->OrigOrAnonNamespace.setInt(IsOriginal);
607 D->OrigOrAnonNamespace.setPointer(
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000608 cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++])));
609}
610
611void PCHDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
612 VisitNamedDecl(D);
613
614 D->setAliasLoc(Reader.ReadSourceLocation(Record, Idx));
615 D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
616 D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
617 D->setTargetNameLoc(Reader.ReadSourceLocation(Record, Idx));
618 D->setAliasedNamespace(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
619}
620
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000621void PCHDeclReader::VisitUsingDecl(UsingDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000622 VisitNamedDecl(D);
623 D->setUsingLocation(Reader.ReadSourceLocation(Record, Idx));
624 D->setNestedNameRange(Reader.ReadSourceRange(Record, Idx));
625 D->setTargetNestedNameDecl(Reader.ReadNestedNameSpecifier(Record, Idx));
626
627 // FIXME: It would probably be more efficient to read these into a vector
628 // and then re-cosntruct the shadow decl set over that vector since it
629 // would avoid existence checks.
630 unsigned NumShadows = Record[Idx++];
631 for(unsigned I = 0; I != NumShadows; ++I) {
Argyrios Kyrtzidis82f8e792010-07-08 13:09:41 +0000632 // Avoid invariant checking of UsingDecl::addShadowDecl, the decl may still
633 // be initializing.
634 D->Shadows.insert(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000635 }
636 D->setTypeName(Record[Idx++]);
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000637 NamedDecl *Pattern = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
638 if (Pattern)
639 Reader.getContext()->setInstantiatedFromUsingDecl(D, Pattern);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000640}
641
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000642void PCHDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000643 VisitNamedDecl(D);
644 D->setTargetDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
645 D->setUsingDecl(cast<UsingDecl>(Reader.GetDecl(Record[Idx++])));
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000646 UsingShadowDecl *Pattern
647 = cast_or_null<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]));
648 if (Pattern)
649 Reader.getContext()->setInstantiatedFromUsingShadowDecl(D, Pattern);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000650}
651
652void PCHDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
653 VisitNamedDecl(D);
654 D->setNamespaceKeyLocation(Reader.ReadSourceLocation(Record, Idx));
655 D->setQualifierRange(Reader.ReadSourceRange(Record, Idx));
656 D->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx));
657 D->setIdentLocation(Reader.ReadSourceLocation(Record, Idx));
658 D->setNominatedNamespace(cast<NamedDecl>(Reader.GetDecl(Record[Idx++])));
659 D->setCommonAncestor(cast_or_null<DeclContext>(
660 Reader.GetDecl(Record[Idx++])));
661}
662
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000663void PCHDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000664 VisitValueDecl(D);
665 D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
666 D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
667 D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
668}
669
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000670void PCHDeclReader::VisitUnresolvedUsingTypenameDecl(
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000671 UnresolvedUsingTypenameDecl *D) {
672 VisitTypeDecl(D);
673 D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx));
674 D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx));
675 D->setTypenameLoc(Reader.ReadSourceLocation(Record, Idx));
676 D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx));
677}
678
679void PCHDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) {
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000680 ASTContext &C = *Reader.getContext();
681
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000682 // We need to allocate the DefinitionData struct ahead of VisitRecordDecl
683 // so that the other CXXRecordDecls can get a pointer even when the owner
684 // is still initializing.
685 bool OwnsDefinitionData = false;
686 enum DataOwnership { Data_NoDefData, Data_Owner, Data_NotOwner };
687 switch ((DataOwnership)Record[Idx++]) {
688 default:
689 assert(0 && "Out of sync with PCHDeclWriter or messed up reading");
690 case Data_NoDefData:
691 break;
692 case Data_Owner:
693 OwnsDefinitionData = true;
694 D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D);
695 break;
696 case Data_NotOwner:
697 D->DefinitionData
698 = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))->DefinitionData;
699 break;
700 }
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000701
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000702 VisitRecordDecl(D);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000703
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000704 if (OwnsDefinitionData) {
705 assert(D->DefinitionData);
706 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000707
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000708 Data.UserDeclaredConstructor = Record[Idx++];
709 Data.UserDeclaredCopyConstructor = Record[Idx++];
710 Data.UserDeclaredCopyAssignment = Record[Idx++];
711 Data.UserDeclaredDestructor = Record[Idx++];
712 Data.Aggregate = Record[Idx++];
713 Data.PlainOldData = Record[Idx++];
714 Data.Empty = Record[Idx++];
715 Data.Polymorphic = Record[Idx++];
716 Data.Abstract = Record[Idx++];
717 Data.HasTrivialConstructor = Record[Idx++];
718 Data.HasTrivialCopyConstructor = Record[Idx++];
719 Data.HasTrivialCopyAssignment = Record[Idx++];
720 Data.HasTrivialDestructor = Record[Idx++];
721 Data.ComputedVisibleConversions = Record[Idx++];
722 Data.DeclaredDefaultConstructor = Record[Idx++];
723 Data.DeclaredCopyConstructor = Record[Idx++];
724 Data.DeclaredCopyAssignment = Record[Idx++];
725 Data.DeclaredDestructor = Record[Idx++];
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000726
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000727 // setBases() is unsuitable since it may try to iterate the bases of an
Nick Lewycky56062202010-07-26 16:56:01 +0000728 // uninitialized base.
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000729 Data.NumBases = Record[Idx++];
730 Data.Bases = new(C) CXXBaseSpecifier [Data.NumBases];
731 for (unsigned i = 0; i != Data.NumBases; ++i)
Nick Lewycky56062202010-07-26 16:56:01 +0000732 Data.Bases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000733
734 // FIXME: Make VBases lazily computed when needed to avoid storing them.
735 Data.NumVBases = Record[Idx++];
736 Data.VBases = new(C) CXXBaseSpecifier [Data.NumVBases];
737 for (unsigned i = 0; i != Data.NumVBases; ++i)
Nick Lewycky56062202010-07-26 16:56:01 +0000738 Data.VBases[i] = Reader.ReadCXXBaseSpecifier(Cursor, Record, Idx);
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000739
740 Reader.ReadUnresolvedSet(Data.Conversions, Record, Idx);
741 Reader.ReadUnresolvedSet(Data.VisibleConversions, Record, Idx);
742 assert(Data.Definition && "Data.Definition should be already set!");
743 Data.FirstFriend
744 = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000745 }
746
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000747 enum CXXRecKind {
748 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
749 };
750 switch ((CXXRecKind)Record[Idx++]) {
751 default:
752 assert(false && "Out of sync with PCHDeclWriter::VisitCXXRecordDecl?");
753 case CXXRecNotTemplate:
754 break;
755 case CXXRecTemplate:
756 D->setDescribedClassTemplate(
757 cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])));
758 break;
759 case CXXRecMemberSpecialization: {
760 CXXRecordDecl *RD = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]));
761 TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++];
762 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
763 D->setInstantiationOfMemberClass(RD, TSK);
764 D->getMemberSpecializationInfo()->setPointOfInstantiation(POI);
765 break;
766 }
767 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000768}
769
770void PCHDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000771 VisitFunctionDecl(D);
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000772 unsigned NumOverridenMethods = Record[Idx++];
773 while (NumOverridenMethods--) {
774 CXXMethodDecl *MD = cast<CXXMethodDecl>(Reader.GetDecl(Record[Idx++]));
775 // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod,
776 // MD may be initializing.
777 Reader.getContext()->addOverriddenMethod(D, MD);
778 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000779}
780
781void PCHDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000782 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000783
784 D->IsExplicitSpecified = Record[Idx++];
785 D->ImplicitlyDefined = Record[Idx++];
786
787 unsigned NumInitializers = Record[Idx++];
788 D->NumBaseOrMemberInitializers = NumInitializers;
789 if (NumInitializers) {
790 ASTContext &C = *Reader.getContext();
791
792 D->BaseOrMemberInitializers
793 = new (C) CXXBaseOrMemberInitializer*[NumInitializers];
794 for (unsigned i=0; i != NumInitializers; ++i) {
Duncan Sands1f377b12010-07-06 18:19:40 +0000795 TypeSourceInfo *BaseClassInfo = 0;
796 bool IsBaseVirtual = false;
797 FieldDecl *Member = 0;
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000798
799 bool IsBaseInitializer = Record[Idx++];
800 if (IsBaseInitializer) {
Sebastian Redl577d4792010-07-22 22:43:28 +0000801 BaseClassInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000802 IsBaseVirtual = Record[Idx++];
803 } else {
804 Member = cast<FieldDecl>(Reader.GetDecl(Record[Idx++]));
805 }
806 SourceLocation MemberLoc = Reader.ReadSourceLocation(Record, Idx);
Sebastian Redl577d4792010-07-22 22:43:28 +0000807 Expr *Init = Reader.ReadExpr(Cursor);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000808 FieldDecl *AnonUnionMember
809 = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]));
810 SourceLocation LParenLoc = Reader.ReadSourceLocation(Record, Idx);
811 SourceLocation RParenLoc = Reader.ReadSourceLocation(Record, Idx);
812 bool IsWritten = Record[Idx++];
813 unsigned SourceOrderOrNumArrayIndices;
814 llvm::SmallVector<VarDecl *, 8> Indices;
815 if (IsWritten) {
816 SourceOrderOrNumArrayIndices = Record[Idx++];
817 } else {
818 SourceOrderOrNumArrayIndices = Record[Idx++];
819 Indices.reserve(SourceOrderOrNumArrayIndices);
820 for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i)
821 Indices.push_back(cast<VarDecl>(Reader.GetDecl(Record[Idx++])));
822 }
823
824 CXXBaseOrMemberInitializer *BOMInit;
825 if (IsBaseInitializer) {
826 BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo,
827 IsBaseVirtual, LParenLoc,
828 Init, RParenLoc);
829 } else if (IsWritten) {
830 BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc,
831 LParenLoc, Init, RParenLoc);
832 } else {
833 BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc,
834 LParenLoc, Init, RParenLoc,
835 Indices.data(),
836 Indices.size());
837 }
838
839 BOMInit->setAnonUnionMember(AnonUnionMember);
840 D->BaseOrMemberInitializers[i] = BOMInit;
841 }
842 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000843}
844
845void PCHDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000846 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000847
848 D->ImplicitlyDefined = Record[Idx++];
849 D->OperatorDelete = cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]));
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000850}
851
852void PCHDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000853 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000854 D->IsExplicitSpecified = Record[Idx++];
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000855}
856
Abramo Bagnara6206d532010-06-05 05:09:32 +0000857void PCHDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) {
858 VisitDecl(D);
859 D->setColonLoc(Reader.ReadSourceLocation(Record, Idx));
860}
861
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000862void PCHDeclReader::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000863 VisitDecl(D);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000864 if (Record[Idx++])
Sebastian Redl577d4792010-07-22 22:43:28 +0000865 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000866 else
867 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
868 D->NextFriend = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++]));
869 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
870}
871
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000872void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000873 VisitDecl(D);
874 unsigned NumParams = Record[Idx++];
875 D->NumParams = NumParams;
876 D->Params = new TemplateParameterList*[NumParams];
877 for (unsigned i = 0; i != NumParams; ++i)
878 D->Params[i] = Reader.ReadTemplateParameterList(Record, Idx);
879 if (Record[Idx++]) // HasFriendDecl
880 D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++]));
881 else
Sebastian Redl577d4792010-07-22 22:43:28 +0000882 D->Friend = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000883 D->FriendLoc = Reader.ReadSourceLocation(Record, Idx);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000884}
885
886void PCHDeclReader::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000887 VisitNamedDecl(D);
888
Argyrios Kyrtzidisbfcc92c2010-07-08 17:12:57 +0000889 NamedDecl *TemplatedDecl
890 = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000891 TemplateParameterList* TemplateParams
892 = Reader.ReadTemplateParameterList(Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000893 D->init(TemplatedDecl, TemplateParams);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000894}
895
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000896void PCHDeclReader::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000897 VisitTemplateDecl(D);
898
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000899 D->IdentifierNamespace = Record[Idx++];
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000900 RedeclarableTemplateDecl *PrevDecl =
901 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
902 assert((PrevDecl == 0 || PrevDecl->getKind() == D->getKind()) &&
903 "PrevDecl kind mismatch");
904 if (PrevDecl)
905 D->CommonOrPrev = PrevDecl;
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000906 if (PrevDecl == 0) {
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000907 if (RedeclarableTemplateDecl *RTD
908 = cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]))) {
909 assert(RTD->getKind() == D->getKind() &&
910 "InstantiatedFromMemberTemplate kind mismatch");
911 D->setInstantiatedFromMemberTemplateImpl(RTD);
912 if (Record[Idx++])
913 D->setMemberSpecialization();
914 }
Peter Collingbourne8a798a72010-07-29 16:12:01 +0000915
916 RedeclarableTemplateDecl *LatestDecl =
917 cast_or_null<RedeclarableTemplateDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +0000918
919 // This decl is a first one and the latest declaration that it points to is
920 // in the same PCH. However, if this actually needs to point to a
921 // redeclaration in another chained PCH, we need to update it by checking
922 // the FirstLatestDeclIDs map which tracks this kind of decls.
923 assert(Reader.GetDecl(ThisDeclID) == D && "Invalid ThisDeclID ?");
924 PCHReader::FirstLatestDeclIDMap::iterator I
925 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
926 if (I != Reader.FirstLatestDeclIDs.end()) {
927 Decl *NewLatest = Reader.GetDecl(I->second);
928 assert((LatestDecl->getLocation().isInvalid() ||
929 NewLatest->getLocation().isInvalid() ||
930 Reader.SourceMgr.isBeforeInTranslationUnit(
931 LatestDecl->getLocation(),
932 NewLatest->getLocation())) &&
933 "The new latest is supposed to come after the previous latest");
934 LatestDecl = cast<RedeclarableTemplateDecl>(NewLatest);
935 }
936
Peter Collingbourne8a798a72010-07-29 16:12:01 +0000937 assert(LatestDecl->getKind() == D->getKind() && "Latest kind mismatch");
938 D->getCommonPtr()->Latest = LatestDecl;
Peter Collingbourne9eabeba2010-07-29 16:11:51 +0000939 }
940}
941
942void PCHDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) {
943 VisitRedeclarableTemplateDecl(D);
944
945 if (D->getPreviousDeclaration() == 0) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000946 // This ClassTemplateDecl owns a CommonPtr; read it.
947
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000948 // FoldingSets are filled in VisitClassTemplateSpecializationDecl.
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000949 unsigned size = Record[Idx++];
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000950 while (size--)
951 cast<ClassTemplateSpecializationDecl>(Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000952
953 size = Record[Idx++];
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000954 while (size--)
955 cast<ClassTemplatePartialSpecializationDecl>(
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000956 Reader.GetDecl(Record[Idx++]));
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000957
958 // InjectedClassNameType is computed.
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +0000959 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000960}
961
962void PCHDeclReader::VisitClassTemplateSpecializationDecl(
963 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000964 VisitCXXRecordDecl(D);
965
966 if (Decl *InstD = Reader.GetDecl(Record[Idx++])) {
967 if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) {
968 D->setInstantiationOf(CTD);
969 } else {
970 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redl577d4792010-07-22 22:43:28 +0000971 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000972 D->setInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(InstD),
973 TemplArgs.data(), TemplArgs.size());
974 }
975 }
976
977 // Explicit info.
Sebastian Redl577d4792010-07-22 22:43:28 +0000978 if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Cursor, Record, Idx)) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000979 D->setTypeAsWritten(TyInfo);
980 D->setExternLoc(Reader.ReadSourceLocation(Record, Idx));
981 D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx));
982 }
983
984 llvm::SmallVector<TemplateArgument, 8> TemplArgs;
Sebastian Redl577d4792010-07-22 22:43:28 +0000985 Reader.ReadTemplateArgumentList(TemplArgs, Cursor, Record, Idx);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000986 D->initTemplateArgs(TemplArgs.data(), TemplArgs.size());
987 SourceLocation POI = Reader.ReadSourceLocation(Record, Idx);
988 if (POI.isValid())
989 D->setPointOfInstantiation(POI);
990 D->setSpecializationKind((TemplateSpecializationKind)Record[Idx++]);
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000991
Argyrios Kyrtzidisbc5ab7c2010-07-20 13:59:40 +0000992 if (D->isCanonicalDecl()) { // It's kept in the folding set.
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000993 ClassTemplateDecl *CanonPattern
994 = cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]));
995 if (ClassTemplatePartialSpecializationDecl *Partial
996 = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) {
Argyrios Kyrtzidis0a67edd2010-07-12 21:41:31 +0000997 CanonPattern->getPartialSpecializations().InsertNode(Partial);
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000998 } else {
Argyrios Kyrtzidis0a67edd2010-07-12 21:41:31 +0000999 CanonPattern->getSpecializations().InsertNode(D);
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +00001000 }
1001 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001002}
1003
1004void PCHDeclReader::VisitClassTemplatePartialSpecializationDecl(
1005 ClassTemplatePartialSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00001006 VisitClassTemplateSpecializationDecl(D);
1007
1008 D->initTemplateParameters(Reader.ReadTemplateParameterList(Record, Idx));
1009
1010 TemplateArgumentListInfo ArgInfos;
1011 unsigned NumArgs = Record[Idx++];
1012 while (NumArgs--)
Sebastian Redl577d4792010-07-22 22:43:28 +00001013 ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx));
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00001014 D->initTemplateArgsAsWritten(ArgInfos);
1015
1016 D->setSequenceNumber(Record[Idx++]);
1017
1018 // These are read/set from/to the first declaration.
1019 if (D->getPreviousDeclaration() == 0) {
1020 D->setInstantiatedFromMember(
1021 cast_or_null<ClassTemplatePartialSpecializationDecl>(
1022 Reader.GetDecl(Record[Idx++])));
1023 if (Record[Idx++])
Argyrios Kyrtzidis64a5e2c2010-07-09 21:11:35 +00001024 D->setMemberSpecialization();
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +00001025 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001026}
1027
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +00001028void PCHDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
Peter Collingbourne9eabeba2010-07-29 16:11:51 +00001029 VisitRedeclarableTemplateDecl(D);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +00001030
Peter Collingbourne9eabeba2010-07-29 16:11:51 +00001031 if (D->getPreviousDeclaration() == 0) {
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +00001032 // This FunctionTemplateDecl owns a CommonPtr; read it.
1033
Argyrios Kyrtzidis057d9af2010-07-06 15:37:09 +00001034 // Read the function specialization declarations.
1035 // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled
1036 // through the specialized FunctionDecl's setFunctionTemplateSpecialization.
1037 unsigned NumSpecs = Record[Idx++];
1038 while (NumSpecs--)
1039 Reader.GetDecl(Record[Idx++]);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +00001040 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001041}
1042
1043void PCHDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00001044 VisitTypeDecl(D);
1045
1046 D->setDeclaredWithTypename(Record[Idx++]);
1047 D->setParameterPack(Record[Idx++]);
1048
1049 bool Inherited = Record[Idx++];
Sebastian Redl577d4792010-07-22 22:43:28 +00001050 TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Cursor, Record, Idx);
Argyrios Kyrtzidis8731ca72010-06-19 19:29:09 +00001051 D->setDefaultArgument(DefArg, Inherited);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001052}
1053
1054void PCHDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisb24e1992010-06-25 16:25:09 +00001055 VisitVarDecl(D);
1056 // TemplateParmPosition.
1057 D->setDepth(Record[Idx++]);
1058 D->setPosition(Record[Idx++]);
1059 // Rest of NonTypeTemplateParmDecl.
1060 if (Record[Idx++]) {
Sebastian Redl577d4792010-07-22 22:43:28 +00001061 Expr *DefArg = Reader.ReadExpr(Cursor);
Argyrios Kyrtzidisb24e1992010-06-25 16:25:09 +00001062 bool Inherited = Record[Idx++];
1063 D->setDefaultArgument(DefArg, Inherited);
1064 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001065}
1066
1067void PCHDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisbfcc92c2010-07-08 17:12:57 +00001068 VisitTemplateDecl(D);
1069 // TemplateParmPosition.
1070 D->setDepth(Record[Idx++]);
1071 D->setPosition(Record[Idx++]);
1072 // Rest of TemplateTemplateParmDecl.
Sebastian Redl577d4792010-07-22 22:43:28 +00001073 TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Cursor, Record, Idx);
Argyrios Kyrtzidisbfcc92c2010-07-08 17:12:57 +00001074 bool IsInherited = Record[Idx++];
1075 D->setDefaultArgument(Arg, IsInherited);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001076}
1077
1078void PCHDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis0d396892010-07-22 17:28:12 +00001079 VisitDecl(D);
Sebastian Redl577d4792010-07-22 22:43:28 +00001080 D->AssertExpr = Reader.ReadExpr(Cursor);
1081 D->Message = cast<StringLiteral>(Reader.ReadExpr(Cursor));
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001082}
1083
Mike Stump1eb44332009-09-09 15:08:12 +00001084std::pair<uint64_t, uint64_t>
Chris Lattner698f9252009-04-27 05:27:42 +00001085PCHDeclReader::VisitDeclContext(DeclContext *DC) {
1086 uint64_t LexicalOffset = Record[Idx++];
1087 uint64_t VisibleOffset = Record[Idx++];
1088 return std::make_pair(LexicalOffset, VisibleOffset);
1089}
1090
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001091template <typename T>
1092void PCHDeclReader::VisitRedeclarable(Redeclarable<T> *D) {
1093 enum RedeclKind { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1094 RedeclKind Kind = (RedeclKind)Record[Idx++];
1095 switch (Kind) {
1096 default:
1097 assert(0 && "Out of sync with PCHDeclWriter::VisitRedeclarable or messed up"
1098 " reading");
1099 case NoRedeclaration:
1100 break;
1101 case PointsToPrevious:
1102 D->RedeclLink = typename Redeclarable<T>::PreviousDeclLink(
1103 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1104 break;
1105 case PointsToLatest:
1106 D->RedeclLink = typename Redeclarable<T>::LatestDeclLink(
1107 cast_or_null<T>(Reader.GetDecl(Record[Idx++])));
1108 break;
1109 }
1110
1111 assert(!(Kind == PointsToPrevious &&
1112 Reader.FirstLatestDeclIDs.find(ThisDeclID) !=
1113 Reader.FirstLatestDeclIDs.end()) &&
1114 "This decl is not first, it should not be in the map");
1115 if (Kind == PointsToPrevious)
1116 return;
1117
1118 // This decl is a first one and the latest declaration that it points to is in
1119 // the same PCH. However, if this actually needs to point to a redeclaration
1120 // in another chained PCH, we need to update it by checking the
1121 // FirstLatestDeclIDs map which tracks this kind of decls.
1122 assert(Reader.GetDecl(ThisDeclID) == static_cast<T*>(D) &&
1123 "Invalid ThisDeclID ?");
1124 PCHReader::FirstLatestDeclIDMap::iterator I
1125 = Reader.FirstLatestDeclIDs.find(ThisDeclID);
1126 if (I != Reader.FirstLatestDeclIDs.end()) {
1127 Decl *NewLatest = Reader.GetDecl(I->second);
1128 assert((D->getMostRecentDeclaration()->getLocation().isInvalid() ||
1129 NewLatest->getLocation().isInvalid() ||
1130 Reader.SourceMgr.isBeforeInTranslationUnit(
1131 D->getMostRecentDeclaration()->getLocation(),
1132 NewLatest->getLocation())) &&
1133 "The new latest is supposed to come after the previous latest");
1134 D->RedeclLink
1135 = typename Redeclarable<T>::LatestDeclLink(cast_or_null<T>(NewLatest));
1136 }
1137}
1138
Chris Lattner698f9252009-04-27 05:27:42 +00001139//===----------------------------------------------------------------------===//
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001140// Attribute Reading
Chris Lattner698f9252009-04-27 05:27:42 +00001141//===----------------------------------------------------------------------===//
1142
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001143/// \brief Reads attributes from the current stream position.
Sebastian Redl577d4792010-07-22 22:43:28 +00001144Attr *PCHReader::ReadAttributes(llvm::BitstreamCursor &DeclsCursor) {
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001145 unsigned Code = DeclsCursor.ReadCode();
Mike Stump1eb44332009-09-09 15:08:12 +00001146 assert(Code == llvm::bitc::UNABBREV_RECORD &&
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001147 "Expected unabbreviated record"); (void)Code;
Mike Stump1eb44332009-09-09 15:08:12 +00001148
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001149 RecordData Record;
1150 unsigned Idx = 0;
1151 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
Mike Stump1eb44332009-09-09 15:08:12 +00001152 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001153 (void)RecCode;
1154
1155#define SIMPLE_ATTR(Name) \
Sean Hunt387475d2010-06-16 23:43:53 +00001156 case attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001157 New = ::new (*Context) Name##Attr(); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001158 break
1159
1160#define STRING_ATTR(Name) \
Sean Hunt387475d2010-06-16 23:43:53 +00001161 case attr::Name: \
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001162 New = ::new (*Context) Name##Attr(*Context, ReadString(Record, Idx)); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001163 break
1164
1165#define UNSIGNED_ATTR(Name) \
Sean Hunt387475d2010-06-16 23:43:53 +00001166 case attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001167 New = ::new (*Context) Name##Attr(Record[Idx++]); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001168 break
1169
1170 Attr *Attrs = 0;
1171 while (Idx < Record.size()) {
1172 Attr *New = 0;
Sean Hunt387475d2010-06-16 23:43:53 +00001173 attr::Kind Kind = (attr::Kind)Record[Idx++];
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001174 bool IsInherited = Record[Idx++];
1175
1176 switch (Kind) {
Anton Korobeynikov82d0a412010-01-10 12:58:08 +00001177 default:
1178 assert(0 && "Unknown attribute!");
1179 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001180 STRING_ATTR(Alias);
Daniel Dunbar4e9255f2010-05-27 02:25:39 +00001181 SIMPLE_ATTR(AlignMac68k);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001182 UNSIGNED_ATTR(Aligned);
1183 SIMPLE_ATTR(AlwaysInline);
1184 SIMPLE_ATTR(AnalyzerNoReturn);
1185 STRING_ATTR(Annotate);
1186 STRING_ATTR(AsmLabel);
Sean Hunt7725e672009-11-25 04:20:27 +00001187 SIMPLE_ATTR(BaseCheck);
Mike Stump1eb44332009-09-09 15:08:12 +00001188
Sean Hunt387475d2010-06-16 23:43:53 +00001189 case attr::Blocks:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001190 New = ::new (*Context) BlocksAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001191 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
1192 break;
Mike Stump1eb44332009-09-09 15:08:12 +00001193
Eli Friedman8f4c59e2009-11-09 18:38:53 +00001194 SIMPLE_ATTR(CDecl);
1195
Sean Hunt387475d2010-06-16 23:43:53 +00001196 case attr::Cleanup:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001197 New = ::new (*Context) CleanupAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001198 cast<FunctionDecl>(GetDecl(Record[Idx++])));
1199 break;
1200
1201 SIMPLE_ATTR(Const);
1202 UNSIGNED_ATTR(Constructor);
1203 SIMPLE_ATTR(DLLExport);
1204 SIMPLE_ATTR(DLLImport);
1205 SIMPLE_ATTR(Deprecated);
1206 UNSIGNED_ATTR(Destructor);
1207 SIMPLE_ATTR(FastCall);
Sean Huntbbd37c62009-11-21 08:43:09 +00001208 SIMPLE_ATTR(Final);
Mike Stump1eb44332009-09-09 15:08:12 +00001209
Sean Hunt387475d2010-06-16 23:43:53 +00001210 case attr::Format: {
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001211 std::string Type = ReadString(Record, Idx);
1212 unsigned FormatIdx = Record[Idx++];
1213 unsigned FirstArg = Record[Idx++];
Ted Kremenek3d2c43e2010-02-11 05:28:37 +00001214 New = ::new (*Context) FormatAttr(*Context, Type, FormatIdx, FirstArg);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001215 break;
1216 }
Mike Stump1eb44332009-09-09 15:08:12 +00001217
Sean Hunt387475d2010-06-16 23:43:53 +00001218 case attr::FormatArg: {
Fariborz Jahanian5b160922009-05-20 17:41:43 +00001219 unsigned FormatIdx = Record[Idx++];
1220 New = ::new (*Context) FormatArgAttr(FormatIdx);
1221 break;
1222 }
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Sean Hunt387475d2010-06-16 23:43:53 +00001224 case attr::Sentinel: {
Fariborz Jahanian5b530052009-05-13 18:09:35 +00001225 int sentinel = Record[Idx++];
1226 int nullPos = Record[Idx++];
1227 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
1228 break;
1229 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001230
1231 SIMPLE_ATTR(GNUInline);
Sean Hunt7725e672009-11-25 04:20:27 +00001232 SIMPLE_ATTR(Hiding);
Mike Stump1eb44332009-09-09 15:08:12 +00001233
Sean Hunt387475d2010-06-16 23:43:53 +00001234 case attr::IBAction:
Ted Kremenekefbddd22010-02-17 02:37:45 +00001235 New = ::new (*Context) IBActionAttr();
1236 break;
1237
Sean Hunt387475d2010-06-16 23:43:53 +00001238 case attr::IBOutlet:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001239 New = ::new (*Context) IBOutletAttr();
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001240 break;
1241
Sean Hunt387475d2010-06-16 23:43:53 +00001242 case attr::IBOutletCollection: {
Ted Kremenek857e9182010-05-19 17:38:06 +00001243 ObjCInterfaceDecl *D =
1244 cast_or_null<ObjCInterfaceDecl>(GetDecl(Record[Idx++]));
1245 New = ::new (*Context) IBOutletCollectionAttr(D);
1246 break;
1247 }
1248
Ryan Flynn76168e22009-08-09 20:07:29 +00001249 SIMPLE_ATTR(Malloc);
Mike Stump1feade82009-08-26 22:31:08 +00001250 SIMPLE_ATTR(NoDebug);
1251 SIMPLE_ATTR(NoInline);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001252 SIMPLE_ATTR(NoReturn);
1253 SIMPLE_ATTR(NoThrow);
Mike Stump1eb44332009-09-09 15:08:12 +00001254
Sean Hunt387475d2010-06-16 23:43:53 +00001255 case attr::NonNull: {
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001256 unsigned Size = Record[Idx++];
1257 llvm::SmallVector<unsigned, 16> ArgNums;
1258 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
1259 Idx += Size;
Ted Kremenek59616112010-02-11 07:31:47 +00001260 New = ::new (*Context) NonNullAttr(*Context, ArgNums.data(), Size);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001261 break;
1262 }
Mike Stump1eb44332009-09-09 15:08:12 +00001263
Sean Hunt387475d2010-06-16 23:43:53 +00001264 case attr::ReqdWorkGroupSize: {
Nate Begeman6f3d8382009-06-26 06:32:41 +00001265 unsigned X = Record[Idx++];
1266 unsigned Y = Record[Idx++];
1267 unsigned Z = Record[Idx++];
1268 New = ::new (*Context) ReqdWorkGroupSizeAttr(X, Y, Z);
1269 break;
1270 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001271
1272 SIMPLE_ATTR(ObjCException);
1273 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenek31c780d2010-02-18 00:05:45 +00001274 SIMPLE_ATTR(CFReturnsNotRetained);
Ted Kremenekb71368d2009-05-09 02:44:38 +00001275 SIMPLE_ATTR(CFReturnsRetained);
Ted Kremenek31c780d2010-02-18 00:05:45 +00001276 SIMPLE_ATTR(NSReturnsNotRetained);
Ted Kremenekb71368d2009-05-09 02:44:38 +00001277 SIMPLE_ATTR(NSReturnsRetained);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001278 SIMPLE_ATTR(Overloadable);
Sean Hunt7725e672009-11-25 04:20:27 +00001279 SIMPLE_ATTR(Override);
Anders Carlssona860e752009-08-08 18:23:56 +00001280 SIMPLE_ATTR(Packed);
Daniel Dunbar8a2c92c2010-05-27 01:12:46 +00001281 UNSIGNED_ATTR(MaxFieldAlignment);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001282 SIMPLE_ATTR(Pure);
1283 UNSIGNED_ATTR(Regparm);
1284 STRING_ATTR(Section);
1285 SIMPLE_ATTR(StdCall);
Douglas Gregorf813a2c2010-05-18 16:57:00 +00001286 SIMPLE_ATTR(ThisCall);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001287 SIMPLE_ATTR(TransparentUnion);
1288 SIMPLE_ATTR(Unavailable);
1289 SIMPLE_ATTR(Unused);
1290 SIMPLE_ATTR(Used);
Mike Stump1eb44332009-09-09 15:08:12 +00001291
Sean Hunt387475d2010-06-16 23:43:53 +00001292 case attr::Visibility:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001293 New = ::new (*Context) VisibilityAttr(
Eli Friedmanaa8b0d12010-08-05 06:57:20 +00001294 (VisibilityAttr::VisibilityTypes)Record[Idx++],
1295 (bool)Record[Idx++]);
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001296 break;
1297
1298 SIMPLE_ATTR(WarnUnusedResult);
1299 SIMPLE_ATTR(Weak);
Rafael Espindola11e8ce72010-02-23 22:00:30 +00001300 SIMPLE_ATTR(WeakRef);
Chris Lattner4e3fcc82009-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 Lattner698f9252009-04-27 05:27:42 +00001329
1330/// \brief Note that we have loaded the declaration with the given
1331/// Index.
Mike Stump1eb44332009-09-09 15:08:12 +00001332///
Chris Lattner698f9252009-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 Dunbar04a0b502009-09-17 03:06:44 +00001349 if (isa<FileScopeAsmDecl>(D))
1350 return true;
Chris Lattner698f9252009-04-27 05:27:42 +00001351 if (VarDecl *Var = dyn_cast<VarDecl>(D))
Argyrios Kyrtzidis3f954772010-08-05 09:47:59 +00001352 return Var->isFileVarDecl() &&
1353 Var->isThisDeclarationADefinition() == VarDecl::Definition;
Chris Lattner698f9252009-04-27 05:27:42 +00001354 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
1355 return Func->isThisDeclarationADefinition();
1356 return isa<ObjCProtocolDecl>(D);
1357}
1358
Sebastian Redlcb526aa2010-07-20 22:46:15 +00001359/// \brief Get the correct cursor and offset for loading a type.
1360PCHReader::RecordLocation PCHReader::DeclCursorForIndex(unsigned Index) {
1361 PerFileData *F = 0;
1362 for (unsigned I = 0, N = Chain.size(); I != N; ++I) {
1363 F = Chain[N - I - 1];
1364 if (Index < F->LocalNumDecls)
1365 break;
1366 Index -= F->LocalNumDecls;
1367 }
1368 assert(F && F->LocalNumDecls > Index && "Broken chain");
Sebastian Redl971dd442010-07-20 22:55:31 +00001369 return RecordLocation(&F->DeclsCursor, F->DeclOffsets[Index]);
Sebastian Redlcb526aa2010-07-20 22:46:15 +00001370}
1371
Chris Lattner698f9252009-04-27 05:27:42 +00001372/// \brief Read the declaration at the given offset from the PCH file.
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001373Decl *PCHReader::ReadDeclRecord(unsigned Index, pch::DeclID ID) {
Sebastian Redlcb526aa2010-07-20 22:46:15 +00001374 RecordLocation Loc = DeclCursorForIndex(Index);
Sebastian Redl971dd442010-07-20 22:55:31 +00001375 llvm::BitstreamCursor &DeclsCursor = *Loc.first;
Chris Lattner698f9252009-04-27 05:27:42 +00001376 // Keep track of where we are in the stream, then jump back there
1377 // after reading this declaration.
Chris Lattnerda930612009-04-27 05:58:23 +00001378 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner698f9252009-04-27 05:27:42 +00001379
Argyrios Kyrtzidis919e6932010-06-28 22:28:35 +00001380 ReadingKindTracker ReadingKind(Read_Decl, *this);
1381
Douglas Gregord89275b2009-07-06 18:54:52 +00001382 // Note that we are loading a declaration record.
Argyrios Kyrtzidis29ee3a22010-07-30 10:03:16 +00001383 Deserializing ADecl(this);
Mike Stump1eb44332009-09-09 15:08:12 +00001384
Sebastian Redlcb526aa2010-07-20 22:46:15 +00001385 DeclsCursor.JumpToBit(Loc.second);
Chris Lattner698f9252009-04-27 05:27:42 +00001386 RecordData Record;
Chris Lattnerda930612009-04-27 05:58:23 +00001387 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner698f9252009-04-27 05:27:42 +00001388 unsigned Idx = 0;
Argyrios Kyrtzidisa8650052010-08-03 17:30:10 +00001389 PCHDeclReader Reader(*this, DeclsCursor, ID, Record, Idx);
Chris Lattner698f9252009-04-27 05:27:42 +00001390
Chris Lattnerda930612009-04-27 05:58:23 +00001391 Decl *D = 0;
1392 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
Chris Lattner698f9252009-04-27 05:27:42 +00001393 case pch::DECL_ATTR:
1394 case pch::DECL_CONTEXT_LEXICAL:
1395 case pch::DECL_CONTEXT_VISIBLE:
1396 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
1397 break;
Chris Lattner698f9252009-04-27 05:27:42 +00001398 case pch::DECL_TRANSLATION_UNIT:
1399 assert(Index == 0 && "Translation unit must be at index 0");
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001400 D = Context->getTranslationUnitDecl();
Chris Lattner698f9252009-04-27 05:27:42 +00001401 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001402 case pch::DECL_TYPEDEF:
John McCallba6a9bd2009-10-24 08:00:42 +00001403 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001404 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001405 case pch::DECL_ENUM:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001406 D = EnumDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner698f9252009-04-27 05:27:42 +00001407 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001408 case pch::DECL_RECORD:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001409 D = RecordDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner698f9252009-04-27 05:27:42 +00001410 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001411 case pch::DECL_ENUM_CONSTANT:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001412 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +00001413 0, llvm::APSInt());
1414 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001415 case pch::DECL_FUNCTION:
Mike Stump1eb44332009-09-09 15:08:12 +00001416 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001417 QualType(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001418 break;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001419 case pch::DECL_LINKAGE_SPEC:
1420 D = LinkageSpecDecl::Create(*Context, 0, SourceLocation(),
1421 (LinkageSpecDecl::LanguageIDs)0,
1422 false);
1423 break;
1424 case pch::DECL_NAMESPACE:
1425 D = NamespaceDecl::Create(*Context, 0, SourceLocation(), 0);
1426 break;
1427 case pch::DECL_NAMESPACE_ALIAS:
1428 D = NamespaceAliasDecl::Create(*Context, 0, SourceLocation(),
1429 SourceLocation(), 0, SourceRange(), 0,
1430 SourceLocation(), 0);
1431 break;
1432 case pch::DECL_USING:
1433 D = UsingDecl::Create(*Context, 0, SourceLocation(), SourceRange(),
1434 SourceLocation(), 0, DeclarationName(), false);
1435 break;
1436 case pch::DECL_USING_SHADOW:
1437 D = UsingShadowDecl::Create(*Context, 0, SourceLocation(), 0, 0);
1438 break;
1439 case pch::DECL_USING_DIRECTIVE:
1440 D = UsingDirectiveDecl::Create(*Context, 0, SourceLocation(),
1441 SourceLocation(), SourceRange(), 0,
1442 SourceLocation(), 0, 0);
1443 break;
1444 case pch::DECL_UNRESOLVED_USING_VALUE:
1445 D = UnresolvedUsingValueDecl::Create(*Context, 0, SourceLocation(),
1446 SourceRange(), 0, SourceLocation(),
1447 DeclarationName());
1448 break;
1449 case pch::DECL_UNRESOLVED_USING_TYPENAME:
1450 D = UnresolvedUsingTypenameDecl::Create(*Context, 0, SourceLocation(),
1451 SourceLocation(), SourceRange(),
1452 0, SourceLocation(),
1453 DeclarationName());
1454 break;
1455 case pch::DECL_CXX_RECORD:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001456 D = CXXRecordDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001457 break;
1458 case pch::DECL_CXX_METHOD:
1459 D = CXXMethodDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
1460 QualType(), 0);
1461 break;
1462 case pch::DECL_CXX_CONSTRUCTOR:
1463 D = CXXConstructorDecl::Create(*Context, Decl::EmptyShell());
1464 break;
1465 case pch::DECL_CXX_DESTRUCTOR:
1466 D = CXXDestructorDecl::Create(*Context, Decl::EmptyShell());
1467 break;
1468 case pch::DECL_CXX_CONVERSION:
1469 D = CXXConversionDecl::Create(*Context, Decl::EmptyShell());
1470 break;
Abramo Bagnara6206d532010-06-05 05:09:32 +00001471 case pch::DECL_ACCESS_SPEC:
1472 D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(),
1473 SourceLocation());
1474 break;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001475 case pch::DECL_FRIEND:
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +00001476 D = FriendDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001477 break;
1478 case pch::DECL_FRIEND_TEMPLATE:
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +00001479 D = FriendTemplateDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001480 break;
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001481 case pch::DECL_CLASS_TEMPLATE:
Argyrios Kyrtzidis5bf1bdc2010-06-21 10:57:41 +00001482 D = ClassTemplateDecl::Create(*Context, 0, SourceLocation(),
1483 DeclarationName(), 0, 0, 0);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001484 break;
1485 case pch::DECL_CLASS_TEMPLATE_SPECIALIZATION:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001486 D = ClassTemplateSpecializationDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001487 break;
1488 case pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001489 D = ClassTemplatePartialSpecializationDecl::Create(*Context,
1490 Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001491 break;
1492 case pch::DECL_FUNCTION_TEMPLATE:
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +00001493 D = FunctionTemplateDecl::Create(*Context, 0, SourceLocation(),
1494 DeclarationName(), 0, 0);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001495 break;
1496 case pch::DECL_TEMPLATE_TYPE_PARM:
Argyrios Kyrtzidisb8b03e62010-07-02 11:54:55 +00001497 D = TemplateTypeParmDecl::Create(*Context, Decl::EmptyShell());
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001498 break;
1499 case pch::DECL_NON_TYPE_TEMPLATE_PARM:
Argyrios Kyrtzidisb24e1992010-06-25 16:25:09 +00001500 D = NonTypeTemplateParmDecl::Create(*Context, 0, SourceLocation(), 0,0,0,
1501 QualType(),0);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001502 break;
1503 case pch::DECL_TEMPLATE_TEMPLATE_PARM:
Argyrios Kyrtzidisbfcc92c2010-07-08 17:12:57 +00001504 D = TemplateTemplateParmDecl::Create(*Context, 0, SourceLocation(),0,0,0,0);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001505 break;
1506 case pch::DECL_STATIC_ASSERT:
Argyrios Kyrtzidis0d396892010-07-22 17:28:12 +00001507 D = StaticAssertDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner6ad9ac02010-05-07 21:43:38 +00001508 break;
1509
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001510 case pch::DECL_OBJC_METHOD:
Mike Stump1eb44332009-09-09 15:08:12 +00001511 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
Douglas Gregor4bc1cb62010-03-08 14:59:44 +00001512 Selector(), QualType(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001513 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001514 case pch::DECL_OBJC_INTERFACE:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001515 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001516 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001517 case pch::DECL_OBJC_IVAR:
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001518 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +00001519 ObjCIvarDecl::None);
1520 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001521 case pch::DECL_OBJC_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001522 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001523 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001524 case pch::DECL_OBJC_AT_DEFS_FIELD:
Mike Stump1eb44332009-09-09 15:08:12 +00001525 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +00001526 QualType(), 0);
1527 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001528 case pch::DECL_OBJC_CLASS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001529 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +00001530 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001531 case pch::DECL_OBJC_FORWARD_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001532 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +00001533 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001534 case pch::DECL_OBJC_CATEGORY:
Douglas Gregor3db211b2010-01-16 16:38:58 +00001535 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(),
1536 SourceLocation(), SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001537 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001538 case pch::DECL_OBJC_CATEGORY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001539 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001540 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001541 case pch::DECL_OBJC_IMPLEMENTATION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001542 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001543 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001544 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001545 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001546 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001547 case pch::DECL_OBJC_PROPERTY:
Fariborz Jahaniand0502402010-01-21 17:36:00 +00001548 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(),
John McCall83a230c2010-06-04 20:50:08 +00001549 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001550 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001551 case pch::DECL_OBJC_PROPERTY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001552 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
Mike Stump1eb44332009-09-09 15:08:12 +00001553 SourceLocation(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +00001554 ObjCPropertyImplDecl::Dynamic, 0);
1555 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001556 case pch::DECL_FIELD:
Mike Stump1eb44332009-09-09 15:08:12 +00001557 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0, 0,
Argyrios Kyrtzidisa5d82002009-08-21 00:31:54 +00001558 false);
Chris Lattner698f9252009-04-27 05:27:42 +00001559 break;
Chris Lattner698f9252009-04-27 05:27:42 +00001560 case pch::DECL_VAR:
Argyrios Kyrtzidisa1d56622009-08-19 01:27:57 +00001561 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001562 VarDecl::None, VarDecl::None);
Chris Lattner698f9252009-04-27 05:27:42 +00001563 break;
1564
1565 case pch::DECL_IMPLICIT_PARAM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001566 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +00001567 break;
1568
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001569 case pch::DECL_PARM_VAR:
Mike Stump1eb44332009-09-09 15:08:12 +00001570 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Douglas Gregor16573fa2010-04-19 22:54:31 +00001571 VarDecl::None, VarDecl::None, 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001572 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001573 case pch::DECL_FILE_SCOPE_ASM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001574 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +00001575 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001576 case pch::DECL_BLOCK:
Chris Lattnerd1d64a02009-04-27 21:45:14 +00001577 D = BlockDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +00001578 break;
1579 }
Chris Lattner698f9252009-04-27 05:27:42 +00001580
1581 assert(D && "Unknown declaration reading PCH file");
Chris Lattner4e3fcc82009-04-27 06:01:06 +00001582 LoadedDecl(Index, D);
1583 Reader.Visit(D);
Chris Lattner698f9252009-04-27 05:27:42 +00001584
1585 // If this declaration is also a declaration context, get the
1586 // offsets for its tables of lexical and visible declarations.
1587 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
1588 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
1589 if (Offsets.first || Offsets.second) {
1590 DC->setHasExternalLexicalStorage(Offsets.first != 0);
1591 DC->setHasExternalVisibleStorage(Offsets.second != 0);
Sebastian Redl681d7232010-07-27 00:17:23 +00001592 DeclContextInfo Info;
1593 if (ReadDeclContextStorage(DeclsCursor, Offsets, Info))
1594 return 0;
Sebastian Redld692af72010-07-27 18:24:41 +00001595 DeclContextInfos &Infos = DeclContextOffsets[DC];
1596 // Reading the TU will happen after reading its update blocks, so we need
1597 // to make sure we insert in front. For all other contexts, the vector
1598 // is empty here anyway, so there's no loss in efficiency.
1599 Infos.insert(Infos.begin(), Info);
Chris Lattner698f9252009-04-27 05:27:42 +00001600 }
1601 }
1602 assert(Idx == Record.size());
1603
1604 // If we have deserialized a declaration that has a definition the
Argyrios Kyrtzidisbb80a8e2010-07-07 15:46:26 +00001605 // AST consumer might need to know about, queue it.
1606 // We don't pass it to the consumer immediately because we may be in recursive
1607 // loading, and some declarations may still be initializing.
1608 if (isConsumerInterestedIn(D))
1609 InterestingDecls.push_back(D);
Chris Lattner698f9252009-04-27 05:27:42 +00001610
1611 return D;
1612}
Sebastian Redl681d7232010-07-27 00:17:23 +00001613
1614bool PCHReader::ReadDeclContextStorage(llvm::BitstreamCursor &Cursor,
1615 const std::pair<uint64_t, uint64_t> &Offsets,
1616 DeclContextInfo &Info) {
1617 SavedStreamPosition SavedPosition(Cursor);
1618 // First the lexical decls.
1619 if (Offsets.first != 0) {
1620 Cursor.JumpToBit(Offsets.first);
1621
1622 RecordData Record;
1623 const char *Blob;
1624 unsigned BlobLen;
1625 unsigned Code = Cursor.ReadCode();
1626 unsigned RecCode = Cursor.ReadRecord(Code, Record, &Blob, &BlobLen);
1627 if (RecCode != pch::DECL_CONTEXT_LEXICAL) {
1628 Error("Expected lexical block");
1629 return true;
1630 }
1631
1632 Info.LexicalDecls = reinterpret_cast<const pch::DeclID*>(Blob);
1633 Info.NumLexicalDecls = BlobLen / sizeof(pch::DeclID);
1634 } else {
1635 Info.LexicalDecls = 0;
1636 Info.NumLexicalDecls = 0;
1637 }
1638
1639 // Now the visible decls.
1640 Info.Stream = &Cursor;
1641 Info.OffsetToVisibleDecls = Offsets.second;
1642
1643 return false;
1644}