blob: 3dd84c7b08ad50292d8025ac4b67ae18410ae591 [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++])
Douglas Gregor68584ed2009-06-18 16:11:24 +000083 D->addAttr(*Reader.getContext(), 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);
115 TD->setTagKind((TagDecl::TagKind)Record[Idx++]);
116 TD->setDefinition(Record[Idx++]);
117 TD->setTypedefForAnonDecl(
118 cast_or_null<TypedefDecl>(Reader.GetDecl(Record[Idx++])));
119}
120
121void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) {
122 VisitTagDecl(ED);
123 ED->setIntegerType(Reader.GetType(Record[Idx++]));
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000124 // FIXME: C++ InstantiatedFrom
Chris Lattner698f9252009-04-27 05:27:42 +0000125}
126
127void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) {
128 VisitTagDecl(RD);
129 RD->setHasFlexibleArrayMember(Record[Idx++]);
130 RD->setAnonymousStructOrUnion(Record[Idx++]);
131}
132
133void PCHDeclReader::VisitValueDecl(ValueDecl *VD) {
134 VisitNamedDecl(VD);
135 VD->setType(Reader.GetType(Record[Idx++]));
136}
137
138void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) {
139 VisitValueDecl(ECD);
140 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000141 ECD->setInitExpr(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000142 ECD->setInitVal(Reader.ReadAPSInt(Record, Idx));
143}
144
145void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) {
146 VisitValueDecl(FD);
147 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000148 FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo());
Chris Lattner698f9252009-04-27 05:27:42 +0000149 FD->setPreviousDeclaration(
150 cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])));
151 FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]);
152 FD->setInline(Record[Idx++]);
153 FD->setC99InlineDefinition(Record[Idx++]);
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000154 FD->setVirtualAsWritten(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000155 FD->setPure(Record[Idx++]);
Anders Carlssona75e8532009-05-14 21:46:00 +0000156 FD->setHasInheritedPrototype(Record[Idx++]);
157 FD->setHasWrittenPrototype(Record[Idx++]);
Chris Lattner698f9252009-04-27 05:27:42 +0000158 FD->setDeleted(Record[Idx++]);
159 FD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000160 FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000161 // FIXME: C++ TemplateOrInstantiation
Chris Lattner698f9252009-04-27 05:27:42 +0000162 unsigned NumParams = Record[Idx++];
163 llvm::SmallVector<ParmVarDecl *, 16> Params;
164 Params.reserve(NumParams);
165 for (unsigned I = 0; I != NumParams; ++I)
166 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000167 FD->setParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000168}
169
170void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) {
171 VisitNamedDecl(MD);
172 if (Record[Idx++]) {
173 // In practice, this won't be executed (since method definitions
174 // don't occur in header files).
Chris Lattnerda930612009-04-27 05:58:23 +0000175 MD->setBody(Reader.ReadDeclStmt());
Chris Lattner698f9252009-04-27 05:27:42 +0000176 MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
177 MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++])));
178 }
179 MD->setInstanceMethod(Record[Idx++]);
180 MD->setVariadic(Record[Idx++]);
181 MD->setSynthesized(Record[Idx++]);
182 MD->setDeclImplementation((ObjCMethodDecl::ImplementationControl)Record[Idx++]);
183 MD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
184 MD->setResultType(Reader.GetType(Record[Idx++]));
185 MD->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
186 unsigned NumParams = Record[Idx++];
187 llvm::SmallVector<ParmVarDecl *, 16> Params;
188 Params.reserve(NumParams);
189 for (unsigned I = 0; I != NumParams; ++I)
190 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000191 MD->setMethodParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000192}
193
194void PCHDeclReader::VisitObjCContainerDecl(ObjCContainerDecl *CD) {
195 VisitNamedDecl(CD);
196 CD->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
197}
198
199void PCHDeclReader::VisitObjCInterfaceDecl(ObjCInterfaceDecl *ID) {
200 VisitObjCContainerDecl(ID);
201 ID->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr());
202 ID->setSuperClass(cast_or_null<ObjCInterfaceDecl>
203 (Reader.GetDecl(Record[Idx++])));
204 unsigned NumProtocols = Record[Idx++];
205 llvm::SmallVector<ObjCProtocolDecl *, 16> Protocols;
206 Protocols.reserve(NumProtocols);
207 for (unsigned I = 0; I != NumProtocols; ++I)
208 Protocols.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000209 ID->setProtocolList(Protocols.data(), NumProtocols, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000210 unsigned NumIvars = Record[Idx++];
211 llvm::SmallVector<ObjCIvarDecl *, 16> IVars;
212 IVars.reserve(NumIvars);
213 for (unsigned I = 0; I != NumIvars; ++I)
214 IVars.push_back(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000215 ID->setIVarList(IVars.data(), NumIvars, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000216 ID->setCategoryList(
217 cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
218 ID->setForwardDecl(Record[Idx++]);
219 ID->setImplicitInterfaceDecl(Record[Idx++]);
220 ID->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
221 ID->setSuperClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
222 ID->setAtEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
223}
224
225void PCHDeclReader::VisitObjCIvarDecl(ObjCIvarDecl *IVD) {
226 VisitFieldDecl(IVD);
227 IVD->setAccessControl((ObjCIvarDecl::AccessControl)Record[Idx++]);
228}
229
230void PCHDeclReader::VisitObjCProtocolDecl(ObjCProtocolDecl *PD) {
231 VisitObjCContainerDecl(PD);
232 PD->setForwardDecl(Record[Idx++]);
233 PD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
234 unsigned NumProtoRefs = Record[Idx++];
235 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
236 ProtoRefs.reserve(NumProtoRefs);
237 for (unsigned I = 0; I != NumProtoRefs; ++I)
238 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Jay Foadbeaaccd2009-05-21 09:52:38 +0000239 PD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000240}
241
242void PCHDeclReader::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *FD) {
243 VisitFieldDecl(FD);
244}
245
246void PCHDeclReader::VisitObjCClassDecl(ObjCClassDecl *CD) {
247 VisitDecl(CD);
248 unsigned NumClassRefs = Record[Idx++];
249 llvm::SmallVector<ObjCInterfaceDecl *, 16> ClassRefs;
250 ClassRefs.reserve(NumClassRefs);
251 for (unsigned I = 0; I != NumClassRefs; ++I)
252 ClassRefs.push_back(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000253 CD->setClassList(*Reader.getContext(), ClassRefs.data(), NumClassRefs);
Chris Lattner698f9252009-04-27 05:27:42 +0000254}
255
256void PCHDeclReader::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *FPD) {
257 VisitDecl(FPD);
258 unsigned NumProtoRefs = Record[Idx++];
259 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
260 ProtoRefs.reserve(NumProtoRefs);
261 for (unsigned I = 0; I != NumProtoRefs; ++I)
262 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000263 FPD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000264}
265
266void PCHDeclReader::VisitObjCCategoryDecl(ObjCCategoryDecl *CD) {
267 VisitObjCContainerDecl(CD);
268 CD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
269 unsigned NumProtoRefs = Record[Idx++];
270 llvm::SmallVector<ObjCProtocolDecl *, 16> ProtoRefs;
271 ProtoRefs.reserve(NumProtoRefs);
272 for (unsigned I = 0; I != NumProtoRefs; ++I)
273 ProtoRefs.push_back(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++])));
Ted Kremenek66ef1112009-05-22 22:34:23 +0000274 CD->setProtocolList(ProtoRefs.data(), NumProtoRefs, *Reader.getContext());
Chris Lattner698f9252009-04-27 05:27:42 +0000275 CD->setNextClassCategory(cast_or_null<ObjCCategoryDecl>(Reader.GetDecl(Record[Idx++])));
276 CD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
277}
278
279void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) {
280 VisitNamedDecl(CAD);
281 CAD->setClassInterface(cast<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
282}
283
284void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
285 VisitNamedDecl(D);
286 D->setType(Reader.GetType(Record[Idx++]));
287 // FIXME: stable encoding
288 D->setPropertyAttributes(
289 (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]);
290 // FIXME: stable encoding
291 D->setPropertyImplementation(
292 (ObjCPropertyDecl::PropertyControl)Record[Idx++]);
293 D->setGetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
294 D->setSetterName(Reader.ReadDeclarationName(Record, Idx).getObjCSelector());
295 D->setGetterMethodDecl(
296 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
297 D->setSetterMethodDecl(
298 cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++])));
299 D->setPropertyIvarDecl(
300 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
301}
302
303void PCHDeclReader::VisitObjCImplDecl(ObjCImplDecl *D) {
304 VisitNamedDecl(D);
305 D->setClassInterface(
306 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
307 D->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++]));
308}
309
310void PCHDeclReader::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
311 VisitObjCImplDecl(D);
312 D->setIdentifier(Reader.GetIdentifierInfo(Record, Idx));
313}
314
315void PCHDeclReader::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
316 VisitObjCImplDecl(D);
317 D->setSuperClass(
318 cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++])));
319}
320
321
322void PCHDeclReader::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
323 VisitDecl(D);
324 D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
325 D->setPropertyDecl(
326 cast_or_null<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++])));
327 D->setPropertyIvarDecl(
328 cast_or_null<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++])));
329}
330
331void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) {
332 VisitValueDecl(FD);
333 FD->setMutable(Record[Idx++]);
334 if (Record[Idx++])
Chris Lattnerda930612009-04-27 05:58:23 +0000335 FD->setBitWidth(Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000336}
337
338void PCHDeclReader::VisitVarDecl(VarDecl *VD) {
339 VisitValueDecl(VD);
340 VD->setStorageClass((VarDecl::StorageClass)Record[Idx++]);
341 VD->setThreadSpecified(Record[Idx++]);
342 VD->setCXXDirectInitializer(Record[Idx++]);
343 VD->setDeclaredInCondition(Record[Idx++]);
344 VD->setPreviousDeclaration(
345 cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++])));
346 VD->setTypeSpecStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++]));
347 if (Record[Idx++])
Douglas Gregor78d15832009-05-26 18:54:04 +0000348 VD->setInit(*Reader.getContext(), Reader.ReadDeclExpr());
Chris Lattner698f9252009-04-27 05:27:42 +0000349}
350
351void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) {
352 VisitVarDecl(PD);
353}
354
355void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) {
356 VisitVarDecl(PD);
357 PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]);
358 // FIXME: default argument (C++ only)
359}
360
361void PCHDeclReader::VisitOriginalParmVarDecl(OriginalParmVarDecl *PD) {
362 VisitParmVarDecl(PD);
363 PD->setOriginalType(Reader.GetType(Record[Idx++]));
364}
365
366void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) {
367 VisitDecl(AD);
Chris Lattnerda930612009-04-27 05:58:23 +0000368 AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr()));
Chris Lattner698f9252009-04-27 05:27:42 +0000369}
370
371void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) {
372 VisitDecl(BD);
Chris Lattnerda930612009-04-27 05:58:23 +0000373 BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt()));
Chris Lattner698f9252009-04-27 05:27:42 +0000374 unsigned NumParams = Record[Idx++];
375 llvm::SmallVector<ParmVarDecl *, 16> Params;
376 Params.reserve(NumParams);
377 for (unsigned I = 0; I != NumParams; ++I)
378 Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++])));
Douglas Gregor75fdb232009-05-22 22:45:36 +0000379 BD->setParams(*Reader.getContext(), Params.data(), NumParams);
Chris Lattner698f9252009-04-27 05:27:42 +0000380}
381
382std::pair<uint64_t, uint64_t>
383PCHDeclReader::VisitDeclContext(DeclContext *DC) {
384 uint64_t LexicalOffset = Record[Idx++];
385 uint64_t VisibleOffset = Record[Idx++];
386 return std::make_pair(LexicalOffset, VisibleOffset);
387}
388
389//===----------------------------------------------------------------------===//
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000390// Attribute Reading
Chris Lattner698f9252009-04-27 05:27:42 +0000391//===----------------------------------------------------------------------===//
392
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000393/// \brief Reads attributes from the current stream position.
394Attr *PCHReader::ReadAttributes() {
395 unsigned Code = DeclsCursor.ReadCode();
396 assert(Code == llvm::bitc::UNABBREV_RECORD &&
397 "Expected unabbreviated record"); (void)Code;
398
399 RecordData Record;
400 unsigned Idx = 0;
401 unsigned RecCode = DeclsCursor.ReadRecord(Code, Record);
402 assert(RecCode == pch::DECL_ATTR && "Expected attribute record");
403 (void)RecCode;
404
405#define SIMPLE_ATTR(Name) \
406 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000407 New = ::new (*Context) Name##Attr(); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000408 break
409
410#define STRING_ATTR(Name) \
411 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000412 New = ::new (*Context) Name##Attr(ReadString(Record, Idx)); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000413 break
414
415#define UNSIGNED_ATTR(Name) \
416 case Attr::Name: \
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000417 New = ::new (*Context) Name##Attr(Record[Idx++]); \
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000418 break
419
420 Attr *Attrs = 0;
421 while (Idx < Record.size()) {
422 Attr *New = 0;
423 Attr::Kind Kind = (Attr::Kind)Record[Idx++];
424 bool IsInherited = Record[Idx++];
425
426 switch (Kind) {
427 STRING_ATTR(Alias);
428 UNSIGNED_ATTR(Aligned);
429 SIMPLE_ATTR(AlwaysInline);
430 SIMPLE_ATTR(AnalyzerNoReturn);
431 STRING_ATTR(Annotate);
432 STRING_ATTR(AsmLabel);
433
434 case Attr::Blocks:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000435 New = ::new (*Context) BlocksAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000436 (BlocksAttr::BlocksAttrTypes)Record[Idx++]);
437 break;
438
439 case Attr::Cleanup:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000440 New = ::new (*Context) CleanupAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000441 cast<FunctionDecl>(GetDecl(Record[Idx++])));
442 break;
443
444 SIMPLE_ATTR(Const);
445 UNSIGNED_ATTR(Constructor);
446 SIMPLE_ATTR(DLLExport);
447 SIMPLE_ATTR(DLLImport);
448 SIMPLE_ATTR(Deprecated);
449 UNSIGNED_ATTR(Destructor);
450 SIMPLE_ATTR(FastCall);
451
452 case Attr::Format: {
453 std::string Type = ReadString(Record, Idx);
454 unsigned FormatIdx = Record[Idx++];
455 unsigned FirstArg = Record[Idx++];
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000456 New = ::new (*Context) FormatAttr(Type, FormatIdx, FirstArg);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000457 break;
458 }
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000459
Fariborz Jahanian5b160922009-05-20 17:41:43 +0000460 case Attr::FormatArg: {
461 unsigned FormatIdx = Record[Idx++];
462 New = ::new (*Context) FormatArgAttr(FormatIdx);
463 break;
464 }
465
Fariborz Jahanian5b530052009-05-13 18:09:35 +0000466 case Attr::Sentinel: {
467 int sentinel = Record[Idx++];
468 int nullPos = Record[Idx++];
469 New = ::new (*Context) SentinelAttr(sentinel, nullPos);
470 break;
471 }
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000472
473 SIMPLE_ATTR(GNUInline);
474
475 case Attr::IBOutletKind:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000476 New = ::new (*Context) IBOutletAttr();
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000477 break;
478
479 SIMPLE_ATTR(NoReturn);
480 SIMPLE_ATTR(NoThrow);
481 SIMPLE_ATTR(Nodebug);
482 SIMPLE_ATTR(Noinline);
483
484 case Attr::NonNull: {
485 unsigned Size = Record[Idx++];
486 llvm::SmallVector<unsigned, 16> ArgNums;
487 ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size);
488 Idx += Size;
Douglas Gregor75fdb232009-05-22 22:45:36 +0000489 New = ::new (*Context) NonNullAttr(ArgNums.data(), Size);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000490 break;
491 }
492
493 SIMPLE_ATTR(ObjCException);
494 SIMPLE_ATTR(ObjCNSObject);
Ted Kremenekb71368d2009-05-09 02:44:38 +0000495 SIMPLE_ATTR(CFReturnsRetained);
496 SIMPLE_ATTR(NSReturnsRetained);
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000497 SIMPLE_ATTR(Overloadable);
498 UNSIGNED_ATTR(Packed);
499 SIMPLE_ATTR(Pure);
500 UNSIGNED_ATTR(Regparm);
501 STRING_ATTR(Section);
502 SIMPLE_ATTR(StdCall);
503 SIMPLE_ATTR(TransparentUnion);
504 SIMPLE_ATTR(Unavailable);
505 SIMPLE_ATTR(Unused);
506 SIMPLE_ATTR(Used);
507
508 case Attr::Visibility:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000509 New = ::new (*Context) VisibilityAttr(
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000510 (VisibilityAttr::VisibilityTypes)Record[Idx++]);
511 break;
512
513 SIMPLE_ATTR(WarnUnusedResult);
514 SIMPLE_ATTR(Weak);
515 SIMPLE_ATTR(WeakImport);
516 }
517
518 assert(New && "Unable to decode attribute?");
519 New->setInherited(IsInherited);
520 New->setNext(Attrs);
521 Attrs = New;
522 }
523#undef UNSIGNED_ATTR
524#undef STRING_ATTR
525#undef SIMPLE_ATTR
526
527 // The list of attributes was built backwards. Reverse the list
528 // before returning it.
529 Attr *PrevAttr = 0, *NextAttr = 0;
530 while (Attrs) {
531 NextAttr = Attrs->getNext();
532 Attrs->setNext(PrevAttr);
533 PrevAttr = Attrs;
534 Attrs = NextAttr;
535 }
536
537 return PrevAttr;
538}
539
540//===----------------------------------------------------------------------===//
541// PCHReader Implementation
542//===----------------------------------------------------------------------===//
Chris Lattner698f9252009-04-27 05:27:42 +0000543
544/// \brief Note that we have loaded the declaration with the given
545/// Index.
546///
547/// This routine notes that this declaration has already been loaded,
548/// so that future GetDecl calls will return this declaration rather
549/// than trying to load a new declaration.
550inline void PCHReader::LoadedDecl(unsigned Index, Decl *D) {
551 assert(!DeclsLoaded[Index] && "Decl loaded twice?");
552 DeclsLoaded[Index] = D;
553}
554
555
556/// \brief Determine whether the consumer will be interested in seeing
557/// this declaration (via HandleTopLevelDecl).
558///
559/// This routine should return true for anything that might affect
560/// code generation, e.g., inline function definitions, Objective-C
561/// declarations with metadata, etc.
562static bool isConsumerInterestedIn(Decl *D) {
563 if (VarDecl *Var = dyn_cast<VarDecl>(D))
564 return Var->isFileVarDecl() && Var->getInit();
565 if (FunctionDecl *Func = dyn_cast<FunctionDecl>(D))
566 return Func->isThisDeclarationADefinition();
567 return isa<ObjCProtocolDecl>(D);
568}
569
570/// \brief Read the declaration at the given offset from the PCH file.
571Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) {
572 // Keep track of where we are in the stream, then jump back there
573 // after reading this declaration.
Chris Lattnerda930612009-04-27 05:58:23 +0000574 SavedStreamPosition SavedPosition(DeclsCursor);
Chris Lattner698f9252009-04-27 05:27:42 +0000575
Chris Lattnerda930612009-04-27 05:58:23 +0000576 DeclsCursor.JumpToBit(Offset);
Chris Lattner698f9252009-04-27 05:27:42 +0000577 RecordData Record;
Chris Lattnerda930612009-04-27 05:58:23 +0000578 unsigned Code = DeclsCursor.ReadCode();
Chris Lattner698f9252009-04-27 05:27:42 +0000579 unsigned Idx = 0;
580 PCHDeclReader Reader(*this, Record, Idx);
581
Chris Lattnerda930612009-04-27 05:58:23 +0000582 Decl *D = 0;
583 switch ((pch::DeclCode)DeclsCursor.ReadRecord(Code, Record)) {
Chris Lattner698f9252009-04-27 05:27:42 +0000584 case pch::DECL_ATTR:
585 case pch::DECL_CONTEXT_LEXICAL:
586 case pch::DECL_CONTEXT_VISIBLE:
587 assert(false && "Record cannot be de-serialized with ReadDeclRecord");
588 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000589 case pch::DECL_TRANSLATION_UNIT:
590 assert(Index == 0 && "Translation unit must be at index 0");
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000591 D = Context->getTranslationUnitDecl();
Chris Lattner698f9252009-04-27 05:27:42 +0000592 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000593 case pch::DECL_TYPEDEF:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000594 D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000595 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000596 case pch::DECL_ENUM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000597 D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000598 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000599 case pch::DECL_RECORD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000600 D = RecordDecl::Create(*Context, TagDecl::TK_struct, 0, SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000601 0, 0);
602 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000603 case pch::DECL_ENUM_CONSTANT:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000604 D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000605 0, llvm::APSInt());
606 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000607 case pch::DECL_FUNCTION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000608 D = FunctionDecl::Create(*Context, 0, SourceLocation(), DeclarationName(),
Chris Lattner698f9252009-04-27 05:27:42 +0000609 QualType());
610 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000611 case pch::DECL_OBJC_METHOD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000612 D = ObjCMethodDecl::Create(*Context, SourceLocation(), SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000613 Selector(), QualType(), 0);
614 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000615 case pch::DECL_OBJC_INTERFACE:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000616 D = ObjCInterfaceDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000617 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000618 case pch::DECL_OBJC_IVAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000619 D = ObjCIvarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000620 ObjCIvarDecl::None);
621 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000622 case pch::DECL_OBJC_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000623 D = ObjCProtocolDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000624 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000625 case pch::DECL_OBJC_AT_DEFS_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000626 D = ObjCAtDefsFieldDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +0000627 QualType(), 0);
628 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000629 case pch::DECL_OBJC_CLASS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000630 D = ObjCClassDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000631 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000632 case pch::DECL_OBJC_FORWARD_PROTOCOL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000633 D = ObjCForwardProtocolDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000634 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000635 case pch::DECL_OBJC_CATEGORY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000636 D = ObjCCategoryDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000637 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000638 case pch::DECL_OBJC_CATEGORY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000639 D = ObjCCategoryImplDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000640 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000641 case pch::DECL_OBJC_IMPLEMENTATION:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000642 D = ObjCImplementationDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000643 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000644 case pch::DECL_OBJC_COMPATIBLE_ALIAS:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000645 D = ObjCCompatibleAliasDecl::Create(*Context, 0, SourceLocation(), 0, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000646 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000647 case pch::DECL_OBJC_PROPERTY:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000648 D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000649 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000650 case pch::DECL_OBJC_PROPERTY_IMPL:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000651 D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(),
Chris Lattner698f9252009-04-27 05:27:42 +0000652 SourceLocation(), 0,
653 ObjCPropertyImplDecl::Dynamic, 0);
654 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000655 case pch::DECL_FIELD:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000656 D = FieldDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), 0,
Chris Lattner698f9252009-04-27 05:27:42 +0000657 false);
658 break;
Chris Lattner698f9252009-04-27 05:27:42 +0000659 case pch::DECL_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000660 D = VarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000661 VarDecl::None, SourceLocation());
662 break;
663
664 case pch::DECL_IMPLICIT_PARAM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000665 D = ImplicitParamDecl::Create(*Context, 0, SourceLocation(), 0, QualType());
Chris Lattner698f9252009-04-27 05:27:42 +0000666 break;
667
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000668 case pch::DECL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000669 D = ParmVarDecl::Create(*Context, 0, SourceLocation(), 0, QualType(),
Chris Lattner698f9252009-04-27 05:27:42 +0000670 VarDecl::None, 0);
671 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000672 case pch::DECL_ORIGINAL_PARM_VAR:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000673 D = OriginalParmVarDecl::Create(*Context, 0, SourceLocation(), 0,
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000674 QualType(), QualType(), VarDecl::None, 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000675 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000676 case pch::DECL_FILE_SCOPE_ASM:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000677 D = FileScopeAsmDecl::Create(*Context, 0, SourceLocation(), 0);
Chris Lattner698f9252009-04-27 05:27:42 +0000678 break;
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000679 case pch::DECL_BLOCK:
Chris Lattnerd1d64a02009-04-27 21:45:14 +0000680 D = BlockDecl::Create(*Context, 0, SourceLocation());
Chris Lattner698f9252009-04-27 05:27:42 +0000681 break;
682 }
Chris Lattner698f9252009-04-27 05:27:42 +0000683
684 assert(D && "Unknown declaration reading PCH file");
Chris Lattner4e3fcc82009-04-27 06:01:06 +0000685 LoadedDecl(Index, D);
686 Reader.Visit(D);
Chris Lattner698f9252009-04-27 05:27:42 +0000687
688 // If this declaration is also a declaration context, get the
689 // offsets for its tables of lexical and visible declarations.
690 if (DeclContext *DC = dyn_cast<DeclContext>(D)) {
691 std::pair<uint64_t, uint64_t> Offsets = Reader.VisitDeclContext(DC);
692 if (Offsets.first || Offsets.second) {
693 DC->setHasExternalLexicalStorage(Offsets.first != 0);
694 DC->setHasExternalVisibleStorage(Offsets.second != 0);
695 DeclContextOffsets[DC] = Offsets;
696 }
697 }
698 assert(Idx == Record.size());
699
700 // If we have deserialized a declaration that has a definition the
701 // AST consumer might need to know about, notify the consumer
702 // about that definition now or queue it for later.
703 if (isConsumerInterestedIn(D)) {
704 if (Consumer) {
705 DeclGroupRef DG(D);
706 Consumer->HandleTopLevelDecl(DG);
707 } else {
708 InterestingDecls.push_back(D);
709 }
710 }
711
712 return D;
713}
714