blob: 32a1e384cc7d7dd563ba2a92108fe64c76b8fca9 [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"
20#include "clang/AST/Expr.h"
21using namespace clang;
22
Chris Lattner698f9252009-04-27 05:27:42 +000023
24//===----------------------------------------------------------------------===//
25// Declaration deserialization
26//===----------------------------------------------------------------------===//
27
28namespace {
29 class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> {
30 PCHReader &Reader;
31 const PCHReader::RecordData &Record;
32 unsigned &Idx;
33
34 public:
35 PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record,
36 unsigned &Idx)
37 : Reader(Reader), Record(Record), Idx(Idx) { }
38
39 void VisitDecl(Decl *D);
40 void VisitTranslationUnitDecl(TranslationUnitDecl *TU);
41 void VisitNamedDecl(NamedDecl *ND);
42 void VisitTypeDecl(TypeDecl *TD);
43 void VisitTypedefDecl(TypedefDecl *TD);
44 void VisitTagDecl(TagDecl *TD);
45 void VisitEnumDecl(EnumDecl *ED);
46 void VisitRecordDecl(RecordDecl *RD);
47 void VisitValueDecl(ValueDecl *VD);
48 void VisitEnumConstantDecl(EnumConstantDecl *ECD);
49 void VisitFunctionDecl(FunctionDecl *FD);
50 void VisitFieldDecl(FieldDecl *FD);
51 void VisitVarDecl(VarDecl *VD);
52 void VisitImplicitParamDecl(ImplicitParamDecl *PD);
53 void VisitParmVarDecl(ParmVarDecl *PD);
54 void VisitOriginalParmVarDecl(OriginalParmVarDecl *PD);
55 void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD);
56 void VisitBlockDecl(BlockDecl *BD);
57 std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC);
58 void VisitObjCMethodDecl(ObjCMethodDecl *D);
59 void VisitObjCContainerDecl(ObjCContainerDecl *D);
60 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
61 void VisitObjCIvarDecl(ObjCIvarDecl *D);
62 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
63 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
64 void VisitObjCClassDecl(ObjCClassDecl *D);
65 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
66 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
67 void VisitObjCImplDecl(ObjCImplDecl *D);
68 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
69 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
70 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
71 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
72 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
73 };
74}
75
76void PCHDeclReader::VisitDecl(Decl *D) {
77 D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
78 D->setLexicalDeclContext(
79 cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++])));
80 D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++]));
81 D->setInvalidDecl(Record[Idx++]);
82 if (Record[Idx++])
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +000083 D->addAttr(Reader.ReadAttributes());
Chris Lattner698f9252009-04-27 05:27:42 +000084 D->setImplicit(Record[Idx++]);
Douglas Gregore0762c92009-06-19 23:52:42 +000085 D->setUsed(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +000086 D->setAccess((AccessSpecifier)Record[Idx++]);
87}
88
89void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
90 VisitDecl(TU);
91}
92
93void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
94 VisitDecl(ND);
95 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
96}
97
98void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
99 VisitNamedDecl(TD);
100 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
101}
102
103void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
104 // Note that we cannot use VisitTypeDecl here, because we need to
105 // set the underlying type of the typedef *before* we try to read
106 // the type associated with the TypedefDecl.
107 VisitNamedDecl(TD);
108 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
109 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
110 Idx += 2;
111}
112
113void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
114 VisitTypeDecl(TD);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000115 TD->setPreviousDeclaration(
116 cast_or_null<TagDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner698f9252009-04-27 05:27:42 +0000117 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
118 TD->setDefinition(Record[Idx++]);
119 TD->setTypedefForAnonDecl(
120 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000121 TD->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000122 TD->setTagKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner698f9252009-04-27 05:27:42 +0000123}
124
125void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
126 VisitTagDecl(ED);
127 ED->setIntegerType(Reader.GetType(Record[Idx++]));
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000128 // FIXME: C++ InstantiatedFrom
Chris Lattner698f9252009-04-27 05:27:42 +0000129}
130
131void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
132 VisitTagDecl(RD);
133 RD->setHasFlexibleArrayMember(Record[Idx++]);
134 RD->setAnonymousStructOrUnion(Record[Idx++]);
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000135 RD->setHasObjectMember(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000136}
137
138void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
139 VisitNamedDecl(VD);
140 VD->setType(Reader.GetType(Record[Idx++]));
141}
142
143void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
144 VisitValueDecl(ECD);
145 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000146 ECD->setInitExpr(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000147 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
148}
149
150void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
151 VisitValueDecl(FD);
152 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000153 FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo());
Chris Lattner698f9252009-04-27 05:27:42 +0000154 FD->setPreviousDeclaration(
155 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
156 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
157 FD->setInline(Record[Idx++]);
158 FD->setC99InlineDefinition(Record[Idx++]);
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000159 FD->setVirtualAsWritten(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000160 FD->setPure(Record[Idx++]);
Anders Carlssona75e8532009-05-14 21:46:00 +0000161 FD->setHasInheritedPrototype(Record[Idx++]);
162 FD->setHasWrittenPrototype(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000163 FD->setDeleted(Record[Idx++]);
164 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000165 FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000166 // FIXME: C++ TemplateOrInstantiation
Chris Lattner698f9252009-04-27 05:27:42 +0000167 unsigned NumParams = Record[Idx++];
168 llvm::SmallVector<ParmVarDecl *, 16> Params;
169 Params.reserve(NumParams);
170 for (unsigned I = 0; I != NumParams; ++I)
171 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000172 FD->setParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000173}
174
175void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
176 VisitNamedDecl(MD);
177 if (Record[Idx++]) {
178 // In practice, this won't be executed (since method definitions
179 // don't occur in header files).
Chris Lattnerda930612009-04-27 05:58:23 +0000180 MD->setBody(Reader.ReadDeclStmt());
Chris Lattner698f9252009-04-27 05:27:42 +0000181 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
182 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
183 }
184 MD->setInstanceMethod(Record[Idx++]);
185 MD->setVariadic(Record[Idx++]);
186 MD->setSynthesized(Record[Idx++]);
187 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
188 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
189 MD->setResultType(Reader.GetType(Record[Idx++]));
190 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
191 unsigned NumParams = Record[Idx++];
192 llvm::SmallVector<ParmVarDecl *, 16> Params;
193 Params.reserve(NumParams);
194 for (unsigned I = 0; I != NumParams; ++I)
195 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000196 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000197}
198
199void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
200 VisitNamedDecl(CD);
201 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
202}
203
204void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
205 VisitObjCContainerDecl(ID);
206 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
207 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
208 (Reader.GetDecl(Record[Idx++])));
209 unsigned NumProtocols = Record[Idx++];
210 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
211 Protocols.reserve(NumProtocols);
212 for (unsigned I = 0; I != NumProtocols; ++I)
213 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000214 ID->setProtocolList(Protocols.data(), NumProtocols, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000215 unsigned NumIvars = Record[Idx++];
216 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
217 IVars.reserve(NumIvars);
218 for (unsigned I = 0; I != NumIvars; ++I)
219 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000220 ID->setIVarList(IVars.data(), NumIvars, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000221 ID->setCategoryList(
222 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
223 ID->setForwardDecl(Record[Idx++]);
224 ID->setImplicitInterfaceDecl(Record[Idx++]);
225 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
226 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Argyrios Kyrtzidisc999f1f2009-07-18 00:33:23 +0000227 ID->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner698f9252009-04-27 05:27:42 +0000228}
229
230void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
231 VisitFieldDecl(IVD);
232 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
233}
234
235void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
236 VisitObjCContainerDecl(PD);
237 PD->setForwardDecl(Record[Idx++]);
238 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
239 unsigned NumProtoRefs = Record[Idx++];
240 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
241 ProtoRefs.reserve(NumProtoRefs);
242 for (unsigned I = 0; I != NumProtoRefs; ++I)
243 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000244 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000245}
246
247void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
248 VisitFieldDecl(FD);
249}
250
251void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
252 VisitDecl(CD);
253 unsigned NumClassRefs = Record[Idx++];
254 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
255 ClassRefs.reserve(NumClassRefs);
256 for (unsigned I = 0; I != NumClassRefs; ++I)
257 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000258 CD->setClassList(*Reader.getContext(), ClassRefs.data(), NumClassRefs);
Chris Lattner698f9252009-04-27 05:27:42 +0000259}
260
261void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
262 VisitDecl(FPD);
263 unsigned NumProtoRefs = Record[Idx++];
264 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
265 ProtoRefs.reserve(NumProtoRefs);
266 for (unsigned I = 0; I != NumProtoRefs; ++I)
267 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000268 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000269}
270
271void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
272 VisitObjCContainerDecl(CD);
273 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
274 unsigned NumProtoRefs = Record[Idx++];
275 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
276 ProtoRefs.reserve(NumProtoRefs);
277 for (unsigned I = 0; I != NumProtoRefs; ++I)
278 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Ted Kremenek66ef1112009-05-22 22:34:23 +0000279 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000280 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
281 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
282}
283
284void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
285 VisitNamedDecl(CAD);
286 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
287}
288
289void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
290 VisitNamedDecl(D);
291 D->setType(Reader.GetType(Record[Idx++]));
292 // FIXME: stable encoding
293 D->setPropertyAttributes(
294 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
295 // FIXME: stable encoding
296 D->setPropertyImplementation(
297 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
298 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
299 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
300 D->setGetterMethodDecl(
301 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
302 D->setSetterMethodDecl(
303 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
304 D->setPropertyIvarDecl(
305 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
306}
307
308void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000309 VisitObjCContainerDecl(D);
Chris Lattner698f9252009-04-27 05:27:42 +0000310 D->setClassInterface(
311 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattner698f9252009-04-27 05:27:42 +0000312}
313
314void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
315 VisitObjCImplDecl(D);
316 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
317}
318
319void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
320 VisitObjCImplDecl(D);
321 D->setSuperClass(
322 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
323}
324
325
326void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
327 VisitDecl(D);
328 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
329 D->setPropertyDecl(
330 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
331 D->setPropertyIvarDecl(
332 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
333}
334
335void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
336 VisitValueDecl(FD);
337 FD->setMutable(Record[Idx++]);
Steve Naroffea218b82009-07-14 14:58:18 +0000338 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Chris Lattner698f9252009-04-27 05:27:42 +0000339 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000340 FD->setBitWidth(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000341}
342
343void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
344 VisitValueDecl(VD);
345 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
346 VD->setThreadSpecified(Record[Idx++]);
347 VD->setCXXDirectInitializer(Record[Idx++]);
348 VD->setDeclaredInCondition(Record[Idx++]);
349 VD->setPreviousDeclaration(
350 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
351 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
352 if (Record[Idx++])
Douglas Gregor78d15832009-05-26 18:54:04 +0000353 VD->setInit(*Reader.getContext(), Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000354}
355
356void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
357 VisitVarDecl(PD);
358}
359
360void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
361 VisitVarDecl(PD);
362 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000363}
364
365void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
366 VisitParmVarDecl(PD);
367 PD->setOriginalType(Reader.GetType(Record[Idx++]));
368}
369
370void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
371 VisitDecl(AD);
Chris Lattnerda930612009-04-27 05:58:23 +0000372 AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr()));
Chris Lattner698f9252009-04-27 05:27:42 +0000373}
374
375void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
376 VisitDecl(BD);
Chris Lattnerda930612009-04-27 05:58:23 +0000377 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt()));
Chris Lattner698f9252009-04-27 05:27:42 +0000378 unsigned NumParams = Record[Idx++];
379 llvm::SmallVector<ParmVarDecl *, 16> Params;
380 Params.reserve(NumParams);
381 for (unsigned I = 0; I != NumParams; ++I)
382 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000383 BD->setParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000384}
385
386std::pair<uint64_t, uint64_t>
387PCHDeclReader::VisitDeclContext(DeclContext *DC) {
388 uint64_t LexicalOffset = Record[Idx++];
389 uint64_t VisibleOffset = Record[Idx++];
390 return std::make_pair(LexicalOffset, VisibleOffset);
391}
392
393//===----------------------------------------------------------------------===//
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000394// Attribute Reading
Chris Lattner698f9252009-04-27 05:27:42 +0000395//===----------------------------------------------------------------------===//
396
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000397/// \brief Reads attributes from the current stream position.
398Attr *PCHReader::ReadAttributes() {
399 unsigned Code = DeclsCursor.ReadCode();
400 assert(Code == llvm::bitc::UNABBREV_RECORD &&
401 "Expected unabbreviated record"); (void)Code;
402
403 RecordData Record;
404 unsigned Idx = 0;
405 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
406 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
407 (void)RecCode;
408
409#define SIMPLE_ATTR(Name) \
410 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000411 New = ::new (*Context) Name##Attr(); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000412 break
413
414#define STRING_ATTR(Name) \
415 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000416 New = ::new (*Context) Name##Attr(ReadString(Record, Idx)); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000417 break
418
419#define UNSIGNED_ATTR(Name) \
420 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000421 New = ::new (*Context) Name##Attr(Record[Idx++]); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000422 break
423
424 Attr *Attrs = 0;
425 while (Idx < Record.size()) {
426 Attr *New = 0;
427 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
428 bool IsInherited = Record[Idx++];
429
430 switch (Kind) {
431 STRING_ATTR(Alias);
432 UNSIGNED_ATTR(Aligned);
433 SIMPLE_ATTR(AlwaysInline);
434 SIMPLE_ATTR(AnalyzerNoReturn);
435 STRING_ATTR(Annotate);
436 STRING_ATTR(AsmLabel);
437
438 case Attr::Blocks:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000439 New = ::new (*Context) BlocksAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000440 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
441 break;
442
443 case Attr::Cleanup:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000444 New = ::new (*Context) CleanupAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000445 cast<FunctionDecl>(GetDecl(Record[Idx++])));
446 break;
447
448 SIMPLE_ATTR(Const);
449 UNSIGNED_ATTR(Constructor);
450 SIMPLE_ATTR(DLLExport);
451 SIMPLE_ATTR(DLLImport);
452 SIMPLE_ATTR(Deprecated);
453 UNSIGNED_ATTR(Destructor);
454 SIMPLE_ATTR(FastCall);
455
456 case Attr::Format: {
457 std::string Type = ReadString(Record, Idx);
458 unsigned FormatIdx = Record[Idx++];
459 unsigned FirstArg = Record[Idx++];
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000460 New = ::new (*Context) FormatAttr(Type, FormatIdx, FirstArg);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000461 break;
462 }
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000463
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000464 case Attr::FormatArg: {
465 unsigned FormatIdx = Record[Idx++];
466 New = ::new (*Context) FormatArgAttr(FormatIdx);
467 break;
468 }
469
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000470 case Attr::Sentinel: {
471 int sentinel = Record[Idx++];
472 int nullPos = Record[Idx++];
473 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
474 break;
475 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000476
477 SIMPLE_ATTR(GNUInline);
478
479 case Attr::IBOutletKind:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000480 New = ::new (*Context) IBOutletAttr();
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000481 break;
482
Ryan Flynn76168e22009-08-09 20:07:29 +0000483 SIMPLE_ATTR(Malloc);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000484 SIMPLE_ATTR(NoReturn);
485 SIMPLE_ATTR(NoThrow);
486 SIMPLE_ATTR(Nodebug);
487 SIMPLE_ATTR(Noinline);
488
489 case Attr::NonNull: {
490 unsigned Size = Record[Idx++];
491 llvm::SmallVector<unsigned, 16> ArgNums;
492 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
493 Idx += Size;
Douglas Gregor75fdb232009-05-22 22:45:36 +0000494 New = ::new (*Context) NonNullAttr(ArgNums.data(), Size);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000495 break;
496 }
Nate Begeman6f3d8382009-06-26 06:32:41 +0000497
498 case Attr::ReqdWorkGroupSize: {
499 unsigned X = Record[Idx++];
500 unsigned Y = Record[Idx++];
501 unsigned Z = Record[Idx++];
502 New = ::new (*Context) ReqdWorkGroupSizeAttr(X, Y, Z);
503 break;
504 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000505
506 SIMPLE_ATTR(ObjCException);
507 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb71368d2009-05-09 02:44:38 +0000508 SIMPLE_ATTR(CFReturnsRetained);
509 SIMPLE_ATTR(NSReturnsRetained);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000510 SIMPLE_ATTR(Overloadable);
Anders Carlssona860e752009-08-08 18:23:56 +0000511 SIMPLE_ATTR(Packed);
512 UNSIGNED_ATTR(PragmaPack);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000513 SIMPLE_ATTR(Pure);
514 UNSIGNED_ATTR(Regparm);
515 STRING_ATTR(Section);
516 SIMPLE_ATTR(StdCall);
517 SIMPLE_ATTR(TransparentUnion);
518 SIMPLE_ATTR(Unavailable);
519 SIMPLE_ATTR(Unused);
520 SIMPLE_ATTR(Used);
521
522 case Attr::Visibility:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000523 New = ::new (*Context) VisibilityAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000524 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
525 break;
526
527 SIMPLE_ATTR(WarnUnusedResult);
528 SIMPLE_ATTR(Weak);
529 SIMPLE_ATTR(WeakImport);
530 }
531
532 assert(New && "Unable to decode attribute?");
533 New->setInherited(IsInherited);
534 New->setNext(Attrs);
535 Attrs = New;
536 }
537#undef UNSIGNED_ATTR
538#undef STRING_ATTR
539#undef SIMPLE_ATTR
540
541 // The list of attributes was built backwards. Reverse the list
542 // before returning it.
543 Attr *PrevAttr = 0, *NextAttr = 0;
544 while (Attrs) {
545 NextAttr = Attrs->getNext();
546 Attrs->setNext(PrevAttr);
547 PrevAttr = Attrs;
548 Attrs = NextAttr;
549 }
550
551 return PrevAttr;
552}
553
554//===----------------------------------------------------------------------===//
555// PCHReader Implementation
556//===----------------------------------------------------------------------===//
Chris Lattner698f9252009-04-27 05:27:42 +0000557
558/// \brief Note that we have loaded the declaration with the given
559/// Index.
560///
561/// This routine notes that this declaration has already been loaded,
562/// so that future GetDecl calls will return this declaration rather
563/// than trying to load a new declaration.
564inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
565 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
566 DeclsLoaded[Index] = D;
567}
568
569
570/// \brief Determine whether the consumer will be interested in seeing
571/// this declaration (via HandleTopLevelDecl).
572///
573/// This routine should return true for anything that might affect
574/// code generation, e.g., inline function definitions, Objective-C
575/// declarations with metadata, etc.
576static bool isConsumerInterestedIn(Decl *D) {
577 if (VarDecl *Var = dyn_cast<VarDecl>(D))
578 return Var->isFileVarDecl() && Var->getInit();
579 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
580 return Func->isThisDeclarationADefinition();
581 return isa<ObjCProtocolDecl>(D);
582}
583
584/// \brief Read the declaration at the given offset from the PCH file.
585Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
586 // Keep track of where we are in the stream, then jump back there
587 // after reading this declaration.
Chris Lattnerda930612009-04-27 05:58:23 +0000588 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner698f9252009-04-27 05:27:42 +0000589
Douglas Gregord89275b2009-07-06 18:54:52 +0000590 // Note that we are loading a declaration record.
591 LoadingTypeOrDecl Loading(*this);
592
Chris Lattnerda930612009-04-27 05:58:23 +0000593 DeclsCursor.JumpToBit(Offset);
Chris Lattner698f9252009-04-27 05:27:42 +0000594 RecordData Record;
Chris Lattnerda930612009-04-27 05:58:23 +0000595 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner698f9252009-04-27 05:27:42 +0000596 unsigned Idx = 0;
597 PCHDeclReader Reader(*this, Record, Idx);
598
Chris Lattnerda930612009-04-27 05:58:23 +0000599 Decl *D = 0;
600 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
Chris Lattner698f9252009-04-27 05:27:42 +0000601 case pch::DECL_ATTR:
602 case pch::DECL_CONTEXT_LEXICAL:
603 case pch::DECL_CONTEXT_VISIBLE:
604 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
605 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000606 case pch::DECL_TRANSLATION_UNIT:
607 assert(Index == 0 && "Translation unit must be at index 0");
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000608 D = Context->getTranslationUnitDecl();
Chris Lattner698f9252009-04-27 05:27:42 +0000609 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000610 case pch::DECL_TYPEDEF:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000611 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000612 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000613 case pch::DECL_ENUM:
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000614 D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000615 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000616 case pch::DECL_RECORD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000617 D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0, SourceLocation(),
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000618 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000619 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000620 case pch::DECL_ENUM_CONSTANT:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000621 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000622 0, llvm::APSInt());
623 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000624 case pch::DECL_FUNCTION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000625 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
Chris Lattner698f9252009-04-27 05:27:42 +0000626 QualType());
627 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000628 case pch::DECL_OBJC_METHOD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000629 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000630 Selector(), QualType(), 0);
631 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000632 case pch::DECL_OBJC_INTERFACE:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000633 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000634 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000635 case pch::DECL_OBJC_IVAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000636 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000637 ObjCIvarDecl::None);
638 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000639 case pch::DECL_OBJC_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000640 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000641 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000642 case pch::DECL_OBJC_AT_DEFS_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000643 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +0000644 QualType(), 0);
645 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000646 case pch::DECL_OBJC_CLASS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000647 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000648 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000649 case pch::DECL_OBJC_FORWARD_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000650 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000651 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000652 case pch::DECL_OBJC_CATEGORY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000653 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000654 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000655 case pch::DECL_OBJC_CATEGORY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000656 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000657 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000658 case pch::DECL_OBJC_IMPLEMENTATION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000659 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000660 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000661 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000662 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000663 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000664 case pch::DECL_OBJC_PROPERTY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000665 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000666 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000667 case pch::DECL_OBJC_PROPERTY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000668 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000669 SourceLocation(), 0,
670 ObjCPropertyImplDecl::Dynamic, 0);
671 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000672 case pch::DECL_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000673 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Steve Naroffea218b82009-07-14 14:58:18 +0000674 false, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000675 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000676 case pch::DECL_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000677 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000678 VarDecl::None, SourceLocation());
679 break;
680
681 case pch::DECL_IMPLICIT_PARAM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000682 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000683 break;
684
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000685 case pch::DECL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000686 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000687 VarDecl::None, 0);
688 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000689 case pch::DECL_ORIGINAL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000690 D = OriginalParmVarDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000691 QualType(), QualType(), VarDecl::None, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000692 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000693 case pch::DECL_FILE_SCOPE_ASM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000694 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000695 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000696 case pch::DECL_BLOCK:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000697 D = BlockDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000698 break;
699 }
Chris Lattner698f9252009-04-27 05:27:42 +0000700
701 assert(D && "Unknown declaration reading PCH file");
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000702 LoadedDecl(Index, D);
703 Reader.Visit(D);
Chris Lattner698f9252009-04-27 05:27:42 +0000704
705 // If this declaration is also a declaration context, get the
706 // offsets for its tables of lexical and visible declarations.
707 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
708 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
709 if (Offsets.first || Offsets.second) {
710 DC->setHasExternalLexicalStorage(Offsets.first != 0);
711 DC->setHasExternalVisibleStorage(Offsets.second != 0);
712 DeclContextOffsets[DC] = Offsets;
713 }
714 }
715 assert(Idx == Record.size());
716
717 // If we have deserialized a declaration that has a definition the
718 // AST consumer might need to know about, notify the consumer
719 // about that definition now or queue it for later.
720 if (isConsumerInterestedIn(D)) {
721 if (Consumer) {
722 DeclGroupRef DG(D);
723 Consumer->HandleTopLevelDecl(DG);
724 } else {
725 InterestingDecls.push_back(D);
726 }
727 }
728
729 return D;
730}
731