blob: 3db00ec1b71f86dbd67abdcdc7e7e3e9ac0667fa [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++])
83 D->addAttr(Reader.ReadAttributes());
84 D->setImplicit(Record[Idx++]);
85 D->setAccess((AccessSpecifier)Record[Idx++]);
86}
87
88void PCHDeclReader::VisitTranslationUnitDecl(TranslationUnitDecl *TU) {
89 VisitDecl(TU);
90}
91
92void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) {
93 VisitDecl(ND);
94 ND->setDeclName(Reader.ReadDeclarationName(Record, Idx));
95}
96
97void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) {
98 VisitNamedDecl(TD);
99 TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
100}
101
102void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) {
103 // Note that we cannot use VisitTypeDecl here, because we need to
104 // set the underlying type of the typedef *before* we try to read
105 // the type associated with the TypedefDecl.
106 VisitNamedDecl(TD);
107 TD->setUnderlyingType(Reader.GetType(Record[Idx + 1]));
108 TD->setTypeForDecl(Reader.GetType(Record[Idx]).getTypePtr());
109 Idx += 2;
110}
111
112void PCHDeclReader::VisitTagDecl(TagDecl *TD) {
113 VisitTypeDecl(TD);
114 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
115 TD->setDefinition(Record[Idx++]);
116 TD->setTypedefForAnonDecl(
117 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
118}
119
120void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
121 VisitTagDecl(ED);
122 ED->setIntegerType(Reader.GetType(Record[Idx++]));
123}
124
125void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
126 VisitTagDecl(RD);
127 RD->setHasFlexibleArrayMember(Record[Idx++]);
128 RD->setAnonymousStructOrUnion(Record[Idx++]);
129}
130
131void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
132 VisitNamedDecl(VD);
133 VD->setType(Reader.GetType(Record[Idx++]));
134}
135
136void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
137 VisitValueDecl(ECD);
138 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000139 ECD->setInitExpr(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000140 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
141}
142
143void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
144 VisitValueDecl(FD);
145 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000146 FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo());
Chris Lattner698f9252009-04-27 05:27:42 +0000147 FD->setPreviousDeclaration(
148 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
149 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
150 FD->setInline(Record[Idx++]);
151 FD->setC99InlineDefinition(Record[Idx++]);
152 FD->setVirtual(Record[Idx++]);
153 FD->setPure(Record[Idx++]);
154 FD->setInheritedPrototype(Record[Idx++]);
155 FD->setHasPrototype(Record[Idx++]);
156 FD->setDeleted(Record[Idx++]);
157 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
158 unsigned NumParams = Record[Idx++];
159 llvm::SmallVector<ParmVarDecl *, 16> Params;
160 Params.reserve(NumParams);
161 for (unsigned I = 0; I != NumParams; ++I)
162 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000163 FD->setParams(*Reader.getContext(), &Params[0], NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000164}
165
166void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
167 VisitNamedDecl(MD);
168 if (Record[Idx++]) {
169 // In practice, this won't be executed (since method definitions
170 // don't occur in header files).
Chris Lattnerda930612009-04-27 05:58:23 +0000171 MD->setBody(Reader.ReadDeclStmt());
Chris Lattner698f9252009-04-27 05:27:42 +0000172 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
173 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
174 }
175 MD->setInstanceMethod(Record[Idx++]);
176 MD->setVariadic(Record[Idx++]);
177 MD->setSynthesized(Record[Idx++]);
178 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
179 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
180 MD->setResultType(Reader.GetType(Record[Idx++]));
181 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
182 unsigned NumParams = Record[Idx++];
183 llvm::SmallVector<ParmVarDecl *, 16> Params;
184 Params.reserve(NumParams);
185 for (unsigned I = 0; I != NumParams; ++I)
186 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000187 MD->setMethodParams(*Reader.getContext(), &Params[0], NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000188}
189
190void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
191 VisitNamedDecl(CD);
192 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
193}
194
195void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
196 VisitObjCContainerDecl(ID);
197 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
198 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
199 (Reader.GetDecl(Record[Idx++])));
200 unsigned NumProtocols = Record[Idx++];
201 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
202 Protocols.reserve(NumProtocols);
203 for (unsigned I = 0; I != NumProtocols; ++I)
204 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000205 ID->setProtocolList(&Protocols[0], NumProtocols, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000206 unsigned NumIvars = Record[Idx++];
207 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
208 IVars.reserve(NumIvars);
209 for (unsigned I = 0; I != NumIvars; ++I)
210 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000211 ID->setIVarList(&IVars[0], NumIvars, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000212 ID->setCategoryList(
213 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
214 ID->setForwardDecl(Record[Idx++]);
215 ID->setImplicitInterfaceDecl(Record[Idx++]);
216 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
217 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
218 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
219}
220
221void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
222 VisitFieldDecl(IVD);
223 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
224}
225
226void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
227 VisitObjCContainerDecl(PD);
228 PD->setForwardDecl(Record[Idx++]);
229 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
230 unsigned NumProtoRefs = Record[Idx++];
231 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
232 ProtoRefs.reserve(NumProtoRefs);
233 for (unsigned I = 0; I != NumProtoRefs; ++I)
234 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000235 PD->setProtocolList(&ProtoRefs[0], NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000236}
237
238void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
239 VisitFieldDecl(FD);
240}
241
242void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
243 VisitDecl(CD);
244 unsigned NumClassRefs = Record[Idx++];
245 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
246 ClassRefs.reserve(NumClassRefs);
247 for (unsigned I = 0; I != NumClassRefs; ++I)
248 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000249 CD->setClassList(*Reader.getContext(), &ClassRefs[0], NumClassRefs);
Chris Lattner698f9252009-04-27 05:27:42 +0000250}
251
252void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
253 VisitDecl(FPD);
254 unsigned NumProtoRefs = Record[Idx++];
255 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
256 ProtoRefs.reserve(NumProtoRefs);
257 for (unsigned I = 0; I != NumProtoRefs; ++I)
258 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000259 FPD->setProtocolList(&ProtoRefs[0], NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000260}
261
262void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
263 VisitObjCContainerDecl(CD);
264 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
265 unsigned NumProtoRefs = Record[Idx++];
266 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
267 ProtoRefs.reserve(NumProtoRefs);
268 for (unsigned I = 0; I != NumProtoRefs; ++I)
269 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000270 CD->setProtocolList(&ProtoRefs[0], NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000271 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
272 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
273}
274
275void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
276 VisitNamedDecl(CAD);
277 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
278}
279
280void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
281 VisitNamedDecl(D);
282 D->setType(Reader.GetType(Record[Idx++]));
283 // FIXME: stable encoding
284 D->setPropertyAttributes(
285 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
286 // FIXME: stable encoding
287 D->setPropertyImplementation(
288 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
289 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
290 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
291 D->setGetterMethodDecl(
292 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
293 D->setSetterMethodDecl(
294 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
295 D->setPropertyIvarDecl(
296 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
297}
298
299void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
300 VisitNamedDecl(D);
301 D->setClassInterface(
302 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
303 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
304}
305
306void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
307 VisitObjCImplDecl(D);
308 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
309}
310
311void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
312 VisitObjCImplDecl(D);
313 D->setSuperClass(
314 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
315}
316
317
318void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
319 VisitDecl(D);
320 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
321 D->setPropertyDecl(
322 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
323 D->setPropertyIvarDecl(
324 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
325}
326
327void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
328 VisitValueDecl(FD);
329 FD->setMutable(Record[Idx++]);
330 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000331 FD->setBitWidth(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000332}
333
334void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
335 VisitValueDecl(VD);
336 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
337 VD->setThreadSpecified(Record[Idx++]);
338 VD->setCXXDirectInitializer(Record[Idx++]);
339 VD->setDeclaredInCondition(Record[Idx++]);
340 VD->setPreviousDeclaration(
341 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
342 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
343 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000344 VD->setInit(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000345}
346
347void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
348 VisitVarDecl(PD);
349}
350
351void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
352 VisitVarDecl(PD);
353 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
354 // FIXME: default argument (C++ only)
355}
356
357void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
358 VisitParmVarDecl(PD);
359 PD->setOriginalType(Reader.GetType(Record[Idx++]));
360}
361
362void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
363 VisitDecl(AD);
Chris Lattnerda930612009-04-27 05:58:23 +0000364 AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr()));
Chris Lattner698f9252009-04-27 05:27:42 +0000365}
366
367void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
368 VisitDecl(BD);
Chris Lattnerda930612009-04-27 05:58:23 +0000369 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt()));
Chris Lattner698f9252009-04-27 05:27:42 +0000370 unsigned NumParams = Record[Idx++];
371 llvm::SmallVector<ParmVarDecl *, 16> Params;
372 Params.reserve(NumParams);
373 for (unsigned I = 0; I != NumParams; ++I)
374 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000375 BD->setParams(*Reader.getContext(), &Params[0], NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000376}
377
378std::pair<uint64_t, uint64_t>
379PCHDeclReader::VisitDeclContext(DeclContext *DC) {
380 uint64_t LexicalOffset = Record[Idx++];
381 uint64_t VisibleOffset = Record[Idx++];
382 return std::make_pair(LexicalOffset, VisibleOffset);
383}
384
385//===----------------------------------------------------------------------===//
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000386// Attribute Reading
Chris Lattner698f9252009-04-27 05:27:42 +0000387//===----------------------------------------------------------------------===//
388
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000389/// \brief Reads attributes from the current stream position.
390Attr *PCHReader::ReadAttributes() {
391 unsigned Code = DeclsCursor.ReadCode();
392 assert(Code == llvm::bitc::UNABBREV_RECORD &&
393 "Expected unabbreviated record"); (void)Code;
394
395 RecordData Record;
396 unsigned Idx = 0;
397 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
398 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
399 (void)RecCode;
400
401#define SIMPLE_ATTR(Name) \
402 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000403 New = ::new (*Context) Name##Attr(); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000404 break
405
406#define STRING_ATTR(Name) \
407 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000408 New = ::new (*Context) Name##Attr(ReadString(Record, Idx)); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000409 break
410
411#define UNSIGNED_ATTR(Name) \
412 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000413 New = ::new (*Context) Name##Attr(Record[Idx++]); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000414 break
415
416 Attr *Attrs = 0;
417 while (Idx < Record.size()) {
418 Attr *New = 0;
419 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
420 bool IsInherited = Record[Idx++];
421
422 switch (Kind) {
423 STRING_ATTR(Alias);
424 UNSIGNED_ATTR(Aligned);
425 SIMPLE_ATTR(AlwaysInline);
426 SIMPLE_ATTR(AnalyzerNoReturn);
427 STRING_ATTR(Annotate);
428 STRING_ATTR(AsmLabel);
429
430 case Attr::Blocks:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000431 New = ::new (*Context) BlocksAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000432 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
433 break;
434
435 case Attr::Cleanup:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000436 New = ::new (*Context) CleanupAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000437 cast<FunctionDecl>(GetDecl(Record[Idx++])));
438 break;
439
440 SIMPLE_ATTR(Const);
441 UNSIGNED_ATTR(Constructor);
442 SIMPLE_ATTR(DLLExport);
443 SIMPLE_ATTR(DLLImport);
444 SIMPLE_ATTR(Deprecated);
445 UNSIGNED_ATTR(Destructor);
446 SIMPLE_ATTR(FastCall);
447
448 case Attr::Format: {
449 std::string Type = ReadString(Record, Idx);
450 unsigned FormatIdx = Record[Idx++];
451 unsigned FirstArg = Record[Idx++];
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000452 New = ::new (*Context) FormatAttr(Type, FormatIdx, FirstArg);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000453 break;
454 }
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000455
456 case Attr::Sentinel: {
457 int sentinel = Record[Idx++];
458 int nullPos = Record[Idx++];
459 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
460 break;
461 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000462
463 SIMPLE_ATTR(GNUInline);
464
465 case Attr::IBOutletKind:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000466 New = ::new (*Context) IBOutletAttr();
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000467 break;
468
469 SIMPLE_ATTR(NoReturn);
470 SIMPLE_ATTR(NoThrow);
471 SIMPLE_ATTR(Nodebug);
472 SIMPLE_ATTR(Noinline);
473
474 case Attr::NonNull: {
475 unsigned Size = Record[Idx++];
476 llvm::SmallVector<unsigned, 16> ArgNums;
477 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
478 Idx += Size;
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000479 New = ::new (*Context) NonNullAttr(&ArgNums[0], Size);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000480 break;
481 }
482
483 SIMPLE_ATTR(ObjCException);
484 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb71368d2009-05-09 02:44:38 +0000485 SIMPLE_ATTR(CFReturnsRetained);
486 SIMPLE_ATTR(NSReturnsRetained);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000487 SIMPLE_ATTR(Overloadable);
488 UNSIGNED_ATTR(Packed);
489 SIMPLE_ATTR(Pure);
490 UNSIGNED_ATTR(Regparm);
491 STRING_ATTR(Section);
492 SIMPLE_ATTR(StdCall);
493 SIMPLE_ATTR(TransparentUnion);
494 SIMPLE_ATTR(Unavailable);
495 SIMPLE_ATTR(Unused);
496 SIMPLE_ATTR(Used);
497
498 case Attr::Visibility:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000499 New = ::new (*Context) VisibilityAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000500 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
501 break;
502
503 SIMPLE_ATTR(WarnUnusedResult);
504 SIMPLE_ATTR(Weak);
505 SIMPLE_ATTR(WeakImport);
506 }
507
508 assert(New && "Unable to decode attribute?");
509 New->setInherited(IsInherited);
510 New->setNext(Attrs);
511 Attrs = New;
512 }
513#undef UNSIGNED_ATTR
514#undef STRING_ATTR
515#undef SIMPLE_ATTR
516
517 // The list of attributes was built backwards. Reverse the list
518 // before returning it.
519 Attr *PrevAttr = 0, *NextAttr = 0;
520 while (Attrs) {
521 NextAttr = Attrs->getNext();
522 Attrs->setNext(PrevAttr);
523 PrevAttr = Attrs;
524 Attrs = NextAttr;
525 }
526
527 return PrevAttr;
528}
529
530//===----------------------------------------------------------------------===//
531// PCHReader Implementation
532//===----------------------------------------------------------------------===//
Chris Lattner698f9252009-04-27 05:27:42 +0000533
534/// \brief Note that we have loaded the declaration with the given
535/// Index.
536///
537/// This routine notes that this declaration has already been loaded,
538/// so that future GetDecl calls will return this declaration rather
539/// than trying to load a new declaration.
540inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
541 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
542 DeclsLoaded[Index] = D;
543}
544
545
546/// \brief Determine whether the consumer will be interested in seeing
547/// this declaration (via HandleTopLevelDecl).
548///
549/// This routine should return true for anything that might affect
550/// code generation, e.g., inline function definitions, Objective-C
551/// declarations with metadata, etc.
552static bool isConsumerInterestedIn(Decl *D) {
553 if (VarDecl *Var = dyn_cast<VarDecl>(D))
554 return Var->isFileVarDecl() && Var->getInit();
555 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
556 return Func->isThisDeclarationADefinition();
557 return isa<ObjCProtocolDecl>(D);
558}
559
560/// \brief Read the declaration at the given offset from the PCH file.
561Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
562 // Keep track of where we are in the stream, then jump back there
563 // after reading this declaration.
Chris Lattnerda930612009-04-27 05:58:23 +0000564 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner698f9252009-04-27 05:27:42 +0000565
Chris Lattnerda930612009-04-27 05:58:23 +0000566 DeclsCursor.JumpToBit(Offset);
Chris Lattner698f9252009-04-27 05:27:42 +0000567 RecordData Record;
Chris Lattnerda930612009-04-27 05:58:23 +0000568 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner698f9252009-04-27 05:27:42 +0000569 unsigned Idx = 0;
570 PCHDeclReader Reader(*this, Record, Idx);
571
Chris Lattnerda930612009-04-27 05:58:23 +0000572 Decl *D = 0;
573 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
Chris Lattner698f9252009-04-27 05:27:42 +0000574 case pch::DECL_ATTR:
575 case pch::DECL_CONTEXT_LEXICAL:
576 case pch::DECL_CONTEXT_VISIBLE:
577 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
578 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000579 case pch::DECL_TRANSLATION_UNIT:
580 assert(Index == 0 && "Translation unit must be at index 0");
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000581 D = Context->getTranslationUnitDecl();
Chris Lattner698f9252009-04-27 05:27:42 +0000582 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000583 case pch::DECL_TYPEDEF:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000584 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000585 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000586 case pch::DECL_ENUM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000587 D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000588 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000589 case pch::DECL_RECORD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000590 D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0, SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000591 0, 0);
592 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000593 case pch::DECL_ENUM_CONSTANT:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000594 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000595 0, llvm::APSInt());
596 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000597 case pch::DECL_FUNCTION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000598 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
Chris Lattner698f9252009-04-27 05:27:42 +0000599 QualType());
600 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000601 case pch::DECL_OBJC_METHOD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000602 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000603 Selector(), QualType(), 0);
604 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000605 case pch::DECL_OBJC_INTERFACE:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000606 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000607 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000608 case pch::DECL_OBJC_IVAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000609 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000610 ObjCIvarDecl::None);
611 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000612 case pch::DECL_OBJC_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000613 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000614 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000615 case pch::DECL_OBJC_AT_DEFS_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000616 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +0000617 QualType(), 0);
618 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000619 case pch::DECL_OBJC_CLASS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000620 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000621 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000622 case pch::DECL_OBJC_FORWARD_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000623 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000624 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000625 case pch::DECL_OBJC_CATEGORY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000626 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000627 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000628 case pch::DECL_OBJC_CATEGORY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000629 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000630 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000631 case pch::DECL_OBJC_IMPLEMENTATION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000632 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000633 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000634 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000635 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000636 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000637 case pch::DECL_OBJC_PROPERTY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000638 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000639 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000640 case pch::DECL_OBJC_PROPERTY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000641 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000642 SourceLocation(), 0,
643 ObjCPropertyImplDecl::Dynamic, 0);
644 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000645 case pch::DECL_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000646 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +0000647 false);
648 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000649 case pch::DECL_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000650 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000651 VarDecl::None, SourceLocation());
652 break;
653
654 case pch::DECL_IMPLICIT_PARAM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000655 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000656 break;
657
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000658 case pch::DECL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000659 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000660 VarDecl::None, 0);
661 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000662 case pch::DECL_ORIGINAL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000663 D = OriginalParmVarDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000664 QualType(), QualType(), VarDecl::None, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000665 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000666 case pch::DECL_FILE_SCOPE_ASM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000667 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000668 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000669 case pch::DECL_BLOCK:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000670 D = BlockDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000671 break;
672 }
Chris Lattner698f9252009-04-27 05:27:42 +0000673
674 assert(D && "Unknown declaration reading PCH file");
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000675 LoadedDecl(Index, D);
676 Reader.Visit(D);
Chris Lattner698f9252009-04-27 05:27:42 +0000677
678 // If this declaration is also a declaration context, get the
679 // offsets for its tables of lexical and visible declarations.
680 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
681 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
682 if (Offsets.first || Offsets.second) {
683 DC->setHasExternalLexicalStorage(Offsets.first != 0);
684 DC->setHasExternalVisibleStorage(Offsets.second != 0);
685 DeclContextOffsets[DC] = Offsets;
686 }
687 }
688 assert(Idx == Record.size());
689
690 // If we have deserialized a declaration that has a definition the
691 // AST consumer might need to know about, notify the consumer
692 // about that definition now or queue it for later.
693 if (isConsumerInterestedIn(D)) {
694 if (Consumer) {
695 DeclGroupRef DG(D);
696 Consumer->HandleTopLevelDecl(DG);
697 } else {
698 InterestingDecls.push_back(D);
699 }
700 }
701
702 return D;
703}
704