blob: d105382b4354b3c84fde0a72b7e0c3eba4048521 [file] [log] [blame]
Chris Lattner12b1c762009-04-27 06:16:06 +00001//===--- PCHWriterDecl.cpp - Declaration Serialization --------------------===//
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 serialization for Declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Frontend/PCHWriter.h"
15#include "clang/AST/DeclVisitor.h"
16#include "clang/AST/Expr.h"
Daniel Dunbar33671982009-12-03 09:13:36 +000017#include "llvm/ADT/Twine.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000018#include "llvm/Bitcode/BitstreamWriter.h"
Daniel Dunbar33671982009-12-03 09:13:36 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Declaration serialization
24//===----------------------------------------------------------------------===//
25
26namespace {
27 class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
28
29 PCHWriter &Writer;
30 ASTContext &Context;
31 PCHWriter::RecordData &Record;
32
33 public:
34 pch::DeclCode Code;
Chris Lattnerea5ce472009-04-27 07:35:58 +000035 unsigned AbbrevToUse;
Chris Lattner12b1c762009-04-27 06:16:06 +000036
Mike Stump1eb44332009-09-09 15:08:12 +000037 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
38 PCHWriter::RecordData &Record)
Chris Lattnerea5ce472009-04-27 07:35:58 +000039 : Writer(Writer), Context(Context), Record(Record) {
40 }
Chris Lattner12b1c762009-04-27 06:16:06 +000041
42 void VisitDecl(Decl *D);
43 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
44 void VisitNamedDecl(NamedDecl *D);
45 void VisitTypeDecl(TypeDecl *D);
46 void VisitTypedefDecl(TypedefDecl *D);
47 void VisitTagDecl(TagDecl *D);
48 void VisitEnumDecl(EnumDecl *D);
49 void VisitRecordDecl(RecordDecl *D);
50 void VisitValueDecl(ValueDecl *D);
51 void VisitEnumConstantDecl(EnumConstantDecl *D);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +000052 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000053 void VisitFunctionDecl(FunctionDecl *D);
54 void VisitFieldDecl(FieldDecl *D);
55 void VisitVarDecl(VarDecl *D);
56 void VisitImplicitParamDecl(ImplicitParamDecl *D);
57 void VisitParmVarDecl(ParmVarDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000058 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
59 void VisitBlockDecl(BlockDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000060 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000061 uint64_t VisibleOffset);
62 void VisitObjCMethodDecl(ObjCMethodDecl *D);
63 void VisitObjCContainerDecl(ObjCContainerDecl *D);
64 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
65 void VisitObjCIvarDecl(ObjCIvarDecl *D);
66 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
67 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
68 void VisitObjCClassDecl(ObjCClassDecl *D);
69 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
70 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
71 void VisitObjCImplDecl(ObjCImplDecl *D);
72 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
73 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
74 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
75 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
76 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
77 };
78}
79
80void PCHDeclWriter::VisitDecl(Decl *D) {
81 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
82 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
83 Writer.AddSourceLocation(D->getLocation(), Record);
84 Record.push_back(D->isInvalidDecl());
85 Record.push_back(D->hasAttrs());
86 Record.push_back(D->isImplicit());
Douglas Gregore0762c92009-06-19 23:52:42 +000087 Record.push_back(D->isUsed());
Chris Lattner12b1c762009-04-27 06:16:06 +000088 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000089 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +000090}
91
92void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
93 VisitDecl(D);
94 Code = pch::DECL_TRANSLATION_UNIT;
95}
96
97void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
98 VisitDecl(D);
99 Writer.AddDeclarationName(D->getDeclName(), Record);
100}
101
102void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
103 VisitNamedDecl(D);
104 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
105}
106
107void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
108 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000109 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000110 Code = pch::DECL_TYPEDEF;
111}
112
113void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
114 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000115 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000116 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
117 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000118 Record.push_back(D->isEmbeddedInDeclarator());
Chris Lattner12b1c762009-04-27 06:16:06 +0000119 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000120 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000121 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000122}
123
124void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
125 VisitTagDecl(D);
126 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000127 Writer.AddTypeRef(D->getPromotionType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000128 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000129 Code = pch::DECL_ENUM;
130}
131
132void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
133 VisitTagDecl(D);
134 Record.push_back(D->hasFlexibleArrayMember());
135 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000136 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000137 Code = pch::DECL_RECORD;
138}
139
140void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
141 VisitNamedDecl(D);
142 Writer.AddTypeRef(D->getType(), Record);
143}
144
145void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
146 VisitValueDecl(D);
147 Record.push_back(D->getInitExpr()? 1 : 0);
148 if (D->getInitExpr())
149 Writer.AddStmt(D->getInitExpr());
150 Writer.AddAPSInt(D->getInitVal(), Record);
151 Code = pch::DECL_ENUM_CONSTANT;
152}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000153
154void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
155 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000156 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000157}
Chris Lattner12b1c762009-04-27 06:16:06 +0000158
159void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000160 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000161 Record.push_back(D->isThisDeclarationADefinition());
162 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000163 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000164 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
165 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000166 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000167 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000168 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000169 Record.push_back(D->hasInheritedPrototype());
170 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000171 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000172 Record.push_back(D->isTrivial());
173 Record.push_back(D->isCopyAssignment());
174 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000175 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000176 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000177 Record.push_back(D->param_size());
178 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
179 P != PEnd; ++P)
180 Writer.AddDeclRef(*P, Record);
181 Code = pch::DECL_FUNCTION;
182}
183
184void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
185 VisitNamedDecl(D);
186 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000187 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000188 Record.push_back(D->getBody() != 0);
189 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000190 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000191 Writer.AddDeclRef(D->getSelfDecl(), Record);
192 Writer.AddDeclRef(D->getCmdDecl(), Record);
193 }
194 Record.push_back(D->isInstanceMethod());
195 Record.push_back(D->isVariadic());
196 Record.push_back(D->isSynthesized());
197 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000198 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000199 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000200 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000201 Writer.AddTypeRef(D->getResultType(), Record);
202 Writer.AddSourceLocation(D->getLocEnd(), Record);
203 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000204 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000205 PEnd = D->param_end(); P != PEnd; ++P)
206 Writer.AddDeclRef(*P, Record);
207 Code = pch::DECL_OBJC_METHOD;
208}
209
210void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
211 VisitNamedDecl(D);
Ted Kremenek782f2f52010-01-07 01:20:12 +0000212 SourceRange R = D->getAtEndRange();
213 Writer.AddSourceLocation(R.getBegin(), Record);
214 Writer.AddSourceLocation(R.getEnd(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000215 // Abstract class (no need to define a stable pch::DECL code).
216}
217
218void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
219 VisitObjCContainerDecl(D);
220 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
221 Writer.AddDeclRef(D->getSuperClass(), Record);
222 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000223 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000224 PEnd = D->protocol_end();
225 P != PEnd; ++P)
226 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000227 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
228 PLEnd = D->protocol_loc_end();
229 PL != PLEnd; ++PL)
230 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000231 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000232 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000233 IEnd = D->ivar_end(); I != IEnd; ++I)
234 Writer.AddDeclRef(*I, Record);
235 Writer.AddDeclRef(D->getCategoryList(), Record);
236 Record.push_back(D->isForwardDecl());
237 Record.push_back(D->isImplicitInterfaceDecl());
238 Writer.AddSourceLocation(D->getClassLoc(), Record);
239 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
240 Writer.AddSourceLocation(D->getLocEnd(), Record);
241 Code = pch::DECL_OBJC_INTERFACE;
242}
243
244void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
245 VisitFieldDecl(D);
246 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000247 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000248 Code = pch::DECL_OBJC_IVAR;
249}
250
251void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
252 VisitObjCContainerDecl(D);
253 Record.push_back(D->isForwardDecl());
254 Writer.AddSourceLocation(D->getLocEnd(), Record);
255 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000256 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000257 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
258 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000259 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
260 PLEnd = D->protocol_loc_end();
261 PL != PLEnd; ++PL)
262 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000263 Code = pch::DECL_OBJC_PROTOCOL;
264}
265
266void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
267 VisitFieldDecl(D);
268 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
269}
270
271void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
272 VisitDecl(D);
273 Record.push_back(D->size());
274 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000275 Writer.AddDeclRef(I->getInterface(), Record);
276 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
277 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000278 Code = pch::DECL_OBJC_CLASS;
279}
280
281void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
282 VisitDecl(D);
283 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000284 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000285 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
286 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000287 for (ObjCForwardProtocolDecl::protocol_loc_iterator
288 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
289 PL != PLEnd; ++PL)
290 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000291 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
292}
293
294void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
295 VisitObjCContainerDecl(D);
296 Writer.AddDeclRef(D->getClassInterface(), Record);
297 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000298 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000299 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
300 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000301 for (ObjCCategoryDecl::protocol_loc_iterator
302 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
303 PL != PLEnd; ++PL)
304 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000305 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000306 Writer.AddSourceLocation(D->getAtLoc(), Record);
307 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000308 Code = pch::DECL_OBJC_CATEGORY;
309}
310
311void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
312 VisitNamedDecl(D);
313 Writer.AddDeclRef(D->getClassInterface(), Record);
314 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
315}
316
317void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
318 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000319 Writer.AddSourceLocation(D->getAtLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000320 Writer.AddTypeRef(D->getType(), Record);
321 // FIXME: stable encoding
322 Record.push_back((unsigned)D->getPropertyAttributes());
323 // FIXME: stable encoding
324 Record.push_back((unsigned)D->getPropertyImplementation());
325 Writer.AddDeclarationName(D->getGetterName(), Record);
326 Writer.AddDeclarationName(D->getSetterName(), Record);
327 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
328 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
329 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
330 Code = pch::DECL_OBJC_PROPERTY;
331}
332
333void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000334 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000335 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000336 // Abstract class (no need to define a stable pch::DECL code).
337}
338
339void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
340 VisitObjCImplDecl(D);
341 Writer.AddIdentifierRef(D->getIdentifier(), Record);
342 Code = pch::DECL_OBJC_CATEGORY_IMPL;
343}
344
345void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
346 VisitObjCImplDecl(D);
347 Writer.AddDeclRef(D->getSuperClass(), Record);
348 Code = pch::DECL_OBJC_IMPLEMENTATION;
349}
350
351void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
352 VisitDecl(D);
353 Writer.AddSourceLocation(D->getLocStart(), Record);
354 Writer.AddDeclRef(D->getPropertyDecl(), Record);
355 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
356 Code = pch::DECL_OBJC_PROPERTY_IMPL;
357}
358
359void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000360 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000361 Record.push_back(D->isMutable());
362 Record.push_back(D->getBitWidth()? 1 : 0);
363 if (D->getBitWidth())
364 Writer.AddStmt(D->getBitWidth());
365 Code = pch::DECL_FIELD;
366}
367
368void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000369 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000370 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
371 Record.push_back(D->isThreadSpecified());
372 Record.push_back(D->hasCXXDirectInitializer());
373 Record.push_back(D->isDeclaredInCondition());
374 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000375 Record.push_back(D->getInit()? 1 : 0);
376 if (D->getInit())
377 Writer.AddStmt(D->getInit());
378 Code = pch::DECL_VAR;
379}
380
381void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
382 VisitVarDecl(D);
383 Code = pch::DECL_IMPLICIT_PARAM;
384}
385
386void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
387 VisitVarDecl(D);
388 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000389 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000390
391
Chris Lattnerea5ce472009-04-27 07:35:58 +0000392 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
393 // we dynamically check for the properties that we optimize for, but don't
394 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000395 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000396 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000397 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000398 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000399 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000400 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000401 D->getStorageClass() == 0 &&
402 !D->hasCXXDirectInitializer() && // Can params have this ever?
403 D->getObjCDeclQualifier() == 0)
404 AbbrevToUse = Writer.getParmVarDeclAbbrev();
405
406 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
407 // just us assuming it.
408 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
409 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
410 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
411 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
412 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
413 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000414}
415
Chris Lattner12b1c762009-04-27 06:16:06 +0000416void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
417 VisitDecl(D);
418 Writer.AddStmt(D->getAsmString());
419 Code = pch::DECL_FILE_SCOPE_ASM;
420}
421
422void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
423 VisitDecl(D);
424 Writer.AddStmt(D->getBody());
425 Record.push_back(D->param_size());
426 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
427 P != PEnd; ++P)
428 Writer.AddDeclRef(*P, Record);
429 Code = pch::DECL_BLOCK;
430}
431
432/// \brief Emit the DeclContext part of a declaration context decl.
433///
434/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
435/// block for this declaration context is stored. May be 0 to indicate
436/// that there are no declarations stored within this context.
437///
438/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
439/// block for this declaration context is stored. May be 0 to indicate
440/// that there are no declarations visible from this context. Note
441/// that this value will not be emitted for non-primary declaration
442/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000443void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000444 uint64_t VisibleOffset) {
445 Record.push_back(LexicalOffset);
446 Record.push_back(VisibleOffset);
447}
448
449
450//===----------------------------------------------------------------------===//
451// PCHWriter Implementation
452//===----------------------------------------------------------------------===//
453
Chris Lattnerea5ce472009-04-27 07:35:58 +0000454void PCHWriter::WriteDeclsBlockAbbrevs() {
455 using namespace llvm;
456 // Abbreviation for DECL_PARM_VAR.
457 BitCodeAbbrev *Abv = new BitCodeAbbrev();
458 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
459
460 // Decl
461 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
462 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
463 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
464 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
465 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
466 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000467 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000468 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000469 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000470
Chris Lattnerea5ce472009-04-27 07:35:58 +0000471 // NamedDecl
472 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
473 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
474 // ValueDecl
475 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000476 // DeclaratorDecl
477 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000478 // VarDecl
479 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
480 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
481 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
482 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
483 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000484 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
485 // ParmVarDecl
486 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Chris Lattnerea5ce472009-04-27 07:35:58 +0000488 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
489}
490
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000491/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
492/// consumers of the AST.
493///
494/// Such decls will always be deserialized from the PCH file, so we would like
495/// this to be as restrictive as possible. Currently the predicate is driven by
496/// code generation requirements, if other clients have a different notion of
497/// what is "required" then we may have to consider an alternate scheme where
498/// clients can iterate over the top-level decls and get information on them,
499/// without necessary deserializing them. We could explicitly require such
500/// clients to use a separate API call to "realize" the decl. This should be
501/// relatively painless since they would presumably only do it for top-level
502/// decls.
503//
504// FIXME: This predicate is essentially IRgen's predicate to determine whether a
505// declaration can be deferred. Merge them somehow.
506static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
507 // File scoped assembly must be seen.
508 if (isa<FileScopeAsmDecl>(D))
509 return true;
510
511 // Otherwise if this isn't a function or a file scoped variable it doesn't
512 // need to be seen.
513 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
514 if (!VD->isFileVarDecl())
515 return false;
516 } else if (!isa<FunctionDecl>(D))
517 return false;
518
519 // Aliases and used decls must be seen.
520 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
521 return true;
522
523 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
524 // Forward declarations don't need to be seen.
525 if (!FD->isThisDeclarationADefinition())
526 return false;
527
528 // Constructors and destructors must be seen.
529 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
530 return true;
531
532 // Otherwise, this is required unless it is static.
533 //
534 // FIXME: Inlines.
535 return FD->getStorageClass() != FunctionDecl::Static;
536 } else {
537 const VarDecl *VD = cast<VarDecl>(D);
538
539 // In C++, this doesn't need to be seen if it is marked "extern".
540 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
541 (VD->getStorageClass() == VarDecl::Extern ||
542 VD->isExternC()))
543 return false;
544
545 // In C, this doesn't need to be seen unless it is a definition.
546 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
547 return false;
548
549 // Otherwise, this is required unless it is static.
550 return VD->getStorageClass() != VarDecl::Static;
551 }
552}
553
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000554void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000555 RecordData Record;
556 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000557
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000558 // If this declaration is also a DeclContext, write blocks for the
559 // declarations that lexically stored inside its context and those
560 // declarations that are visible from its context. These blocks
561 // are written before the declaration itself so that we can put
562 // their offsets into the record for the declaration.
563 uint64_t LexicalOffset = 0;
564 uint64_t VisibleOffset = 0;
565 DeclContext *DC = dyn_cast<DeclContext>(D);
566 if (DC) {
567 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
568 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000569 }
570
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000571 // Determine the ID for this declaration
572 pch::DeclID &ID = DeclIDs[D];
573 if (ID == 0)
574 ID = DeclIDs.size();
575
576 unsigned Index = ID - 1;
577
578 // Record the offset for this declaration
579 if (DeclOffsets.size() == Index)
580 DeclOffsets.push_back(Stream.GetCurrentBitNo());
581 else if (DeclOffsets.size() < Index) {
582 DeclOffsets.resize(Index+1);
583 DeclOffsets[Index] = Stream.GetCurrentBitNo();
584 }
585
586 // Build and emit a record for this declaration
587 Record.clear();
588 W.Code = (pch::DeclCode)0;
589 W.AbbrevToUse = 0;
590 W.Visit(D);
591 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
592
Daniel Dunbar33671982009-12-03 09:13:36 +0000593 if (!W.Code)
594 llvm::llvm_report_error(llvm::StringRef("unexpected declaration kind '") +
595 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000596 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
597
598 // If the declaration had any attributes, write them now.
599 if (D->hasAttrs())
600 WriteAttributeRecord(D->getAttrs());
601
602 // Flush any expressions that were written as part of this declaration.
603 FlushStmts();
604
605 // Note "external" declarations so that we can add them to a record in the
606 // PCH file later.
607 //
608 // FIXME: This should be renamed, the predicate is much more complicated.
609 if (isRequiredDecl(D, Context))
610 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000611}