blob: 8997e661a5a1d20b1c07fe6d1eb12cd0a7c26f25 [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"
17#include "llvm/Bitcode/BitstreamWriter.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000018#include <cstdio>
19
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 McCallba6a9bd2009-10-24 08:00:42 +0000109 Writer.AddDeclaratorInfo(D->getTypeDeclaratorInfo(), 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());
118 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000119 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000120 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000121}
122
123void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
124 VisitTagDecl(D);
125 Writer.AddTypeRef(D->getIntegerType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000126 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000127 Code = pch::DECL_ENUM;
128}
129
130void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
131 VisitTagDecl(D);
132 Record.push_back(D->hasFlexibleArrayMember());
133 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000134 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000135 Code = pch::DECL_RECORD;
136}
137
138void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
139 VisitNamedDecl(D);
140 Writer.AddTypeRef(D->getType(), Record);
141}
142
143void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
144 VisitValueDecl(D);
145 Record.push_back(D->getInitExpr()? 1 : 0);
146 if (D->getInitExpr())
147 Writer.AddStmt(D->getInitExpr());
148 Writer.AddAPSInt(D->getInitVal(), Record);
149 Code = pch::DECL_ENUM_CONSTANT;
150}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000151
152void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
153 VisitValueDecl(D);
John McCalla1ee0c52009-10-16 21:56:05 +0000154 Writer.AddDeclaratorInfo(D->getDeclaratorInfo(), Record);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000155}
Chris Lattner12b1c762009-04-27 06:16:06 +0000156
157void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000158 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000159 Record.push_back(D->isThisDeclarationADefinition());
160 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000161 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000162 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
163 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000164 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000165 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000166 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000167 Record.push_back(D->hasInheritedPrototype());
168 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000169 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000170 Record.push_back(D->isTrivial());
171 Record.push_back(D->isCopyAssignment());
172 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000173 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000174 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000175 Record.push_back(D->param_size());
176 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
177 P != PEnd; ++P)
178 Writer.AddDeclRef(*P, Record);
179 Code = pch::DECL_FUNCTION;
180}
181
182void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
183 VisitNamedDecl(D);
184 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000185 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000186 Record.push_back(D->getBody() != 0);
187 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000188 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000189 Writer.AddDeclRef(D->getSelfDecl(), Record);
190 Writer.AddDeclRef(D->getCmdDecl(), Record);
191 }
192 Record.push_back(D->isInstanceMethod());
193 Record.push_back(D->isVariadic());
194 Record.push_back(D->isSynthesized());
195 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000196 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000197 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000198 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000199 Writer.AddTypeRef(D->getResultType(), Record);
200 Writer.AddSourceLocation(D->getLocEnd(), Record);
201 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000202 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000203 PEnd = D->param_end(); P != PEnd; ++P)
204 Writer.AddDeclRef(*P, Record);
205 Code = pch::DECL_OBJC_METHOD;
206}
207
208void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
209 VisitNamedDecl(D);
210 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
211 // Abstract class (no need to define a stable pch::DECL code).
212}
213
214void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
215 VisitObjCContainerDecl(D);
216 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
217 Writer.AddDeclRef(D->getSuperClass(), Record);
218 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000219 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000220 PEnd = D->protocol_end();
221 P != PEnd; ++P)
222 Writer.AddDeclRef(*P, Record);
223 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000224 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000225 IEnd = D->ivar_end(); I != IEnd; ++I)
226 Writer.AddDeclRef(*I, Record);
227 Writer.AddDeclRef(D->getCategoryList(), Record);
228 Record.push_back(D->isForwardDecl());
229 Record.push_back(D->isImplicitInterfaceDecl());
230 Writer.AddSourceLocation(D->getClassLoc(), Record);
231 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
232 Writer.AddSourceLocation(D->getLocEnd(), Record);
233 Code = pch::DECL_OBJC_INTERFACE;
234}
235
236void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
237 VisitFieldDecl(D);
238 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000239 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000240 Code = pch::DECL_OBJC_IVAR;
241}
242
243void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
244 VisitObjCContainerDecl(D);
245 Record.push_back(D->isForwardDecl());
246 Writer.AddSourceLocation(D->getLocEnd(), Record);
247 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000248 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000249 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
250 Writer.AddDeclRef(*I, Record);
251 Code = pch::DECL_OBJC_PROTOCOL;
252}
253
254void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
255 VisitFieldDecl(D);
256 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
257}
258
259void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
260 VisitDecl(D);
261 Record.push_back(D->size());
262 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
263 Writer.AddDeclRef(*I, Record);
264 Code = pch::DECL_OBJC_CLASS;
265}
266
267void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
268 VisitDecl(D);
269 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000270 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000271 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
272 Writer.AddDeclRef(*I, Record);
273 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
274}
275
276void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
277 VisitObjCContainerDecl(D);
278 Writer.AddDeclRef(D->getClassInterface(), Record);
279 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000280 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000281 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
282 Writer.AddDeclRef(*I, Record);
283 Writer.AddDeclRef(D->getNextClassCategory(), Record);
284 Writer.AddSourceLocation(D->getLocEnd(), Record);
285 Code = pch::DECL_OBJC_CATEGORY;
286}
287
288void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
289 VisitNamedDecl(D);
290 Writer.AddDeclRef(D->getClassInterface(), Record);
291 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
292}
293
294void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
295 VisitNamedDecl(D);
296 Writer.AddTypeRef(D->getType(), Record);
297 // FIXME: stable encoding
298 Record.push_back((unsigned)D->getPropertyAttributes());
299 // FIXME: stable encoding
300 Record.push_back((unsigned)D->getPropertyImplementation());
301 Writer.AddDeclarationName(D->getGetterName(), Record);
302 Writer.AddDeclarationName(D->getSetterName(), Record);
303 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
304 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
305 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
306 Code = pch::DECL_OBJC_PROPERTY;
307}
308
309void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000310 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000311 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000312 // Abstract class (no need to define a stable pch::DECL code).
313}
314
315void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
316 VisitObjCImplDecl(D);
317 Writer.AddIdentifierRef(D->getIdentifier(), Record);
318 Code = pch::DECL_OBJC_CATEGORY_IMPL;
319}
320
321void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
322 VisitObjCImplDecl(D);
323 Writer.AddDeclRef(D->getSuperClass(), Record);
324 Code = pch::DECL_OBJC_IMPLEMENTATION;
325}
326
327void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
328 VisitDecl(D);
329 Writer.AddSourceLocation(D->getLocStart(), Record);
330 Writer.AddDeclRef(D->getPropertyDecl(), Record);
331 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
332 Code = pch::DECL_OBJC_PROPERTY_IMPL;
333}
334
335void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000336 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000337 Record.push_back(D->isMutable());
338 Record.push_back(D->getBitWidth()? 1 : 0);
339 if (D->getBitWidth())
340 Writer.AddStmt(D->getBitWidth());
341 Code = pch::DECL_FIELD;
342}
343
344void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000345 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000346 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
347 Record.push_back(D->isThreadSpecified());
348 Record.push_back(D->hasCXXDirectInitializer());
349 Record.push_back(D->isDeclaredInCondition());
350 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000351 Record.push_back(D->getInit()? 1 : 0);
352 if (D->getInit())
353 Writer.AddStmt(D->getInit());
354 Code = pch::DECL_VAR;
355}
356
357void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
358 VisitVarDecl(D);
359 Code = pch::DECL_IMPLICIT_PARAM;
360}
361
362void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
363 VisitVarDecl(D);
364 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000365 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000366
367
Chris Lattnerea5ce472009-04-27 07:35:58 +0000368 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
369 // we dynamically check for the properties that we optimize for, but don't
370 // know are true of all PARM_VAR_DECLs.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000371 if (!D->getDeclaratorInfo() &&
372 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000373 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000374 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000375 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000376 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000377 D->getStorageClass() == 0 &&
378 !D->hasCXXDirectInitializer() && // Can params have this ever?
379 D->getObjCDeclQualifier() == 0)
380 AbbrevToUse = Writer.getParmVarDeclAbbrev();
381
382 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
383 // just us assuming it.
384 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
385 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
386 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
387 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
388 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
389 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000390}
391
Chris Lattner12b1c762009-04-27 06:16:06 +0000392void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
393 VisitDecl(D);
394 Writer.AddStmt(D->getAsmString());
395 Code = pch::DECL_FILE_SCOPE_ASM;
396}
397
398void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
399 VisitDecl(D);
400 Writer.AddStmt(D->getBody());
401 Record.push_back(D->param_size());
402 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
403 P != PEnd; ++P)
404 Writer.AddDeclRef(*P, Record);
405 Code = pch::DECL_BLOCK;
406}
407
408/// \brief Emit the DeclContext part of a declaration context decl.
409///
410/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
411/// block for this declaration context is stored. May be 0 to indicate
412/// that there are no declarations stored within this context.
413///
414/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
415/// block for this declaration context is stored. May be 0 to indicate
416/// that there are no declarations visible from this context. Note
417/// that this value will not be emitted for non-primary declaration
418/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000419void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000420 uint64_t VisibleOffset) {
421 Record.push_back(LexicalOffset);
422 Record.push_back(VisibleOffset);
423}
424
425
426//===----------------------------------------------------------------------===//
427// PCHWriter Implementation
428//===----------------------------------------------------------------------===//
429
Chris Lattnerea5ce472009-04-27 07:35:58 +0000430void PCHWriter::WriteDeclsBlockAbbrevs() {
431 using namespace llvm;
432 // Abbreviation for DECL_PARM_VAR.
433 BitCodeAbbrev *Abv = new BitCodeAbbrev();
434 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
435
436 // Decl
437 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
438 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
439 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
440 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
441 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
442 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000443 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000444 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000445 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnerea5ce472009-04-27 07:35:58 +0000447 // NamedDecl
448 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
449 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
450 // ValueDecl
451 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000452 // DeclaratorDecl
453 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000454 // VarDecl
455 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
456 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
457 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
458 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
459 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000460 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
461 // ParmVarDecl
462 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000463
Chris Lattnerea5ce472009-04-27 07:35:58 +0000464 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
465}
466
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000467/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
468/// consumers of the AST.
469///
470/// Such decls will always be deserialized from the PCH file, so we would like
471/// this to be as restrictive as possible. Currently the predicate is driven by
472/// code generation requirements, if other clients have a different notion of
473/// what is "required" then we may have to consider an alternate scheme where
474/// clients can iterate over the top-level decls and get information on them,
475/// without necessary deserializing them. We could explicitly require such
476/// clients to use a separate API call to "realize" the decl. This should be
477/// relatively painless since they would presumably only do it for top-level
478/// decls.
479//
480// FIXME: This predicate is essentially IRgen's predicate to determine whether a
481// declaration can be deferred. Merge them somehow.
482static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
483 // File scoped assembly must be seen.
484 if (isa<FileScopeAsmDecl>(D))
485 return true;
486
487 // Otherwise if this isn't a function or a file scoped variable it doesn't
488 // need to be seen.
489 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
490 if (!VD->isFileVarDecl())
491 return false;
492 } else if (!isa<FunctionDecl>(D))
493 return false;
494
495 // Aliases and used decls must be seen.
496 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
497 return true;
498
499 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
500 // Forward declarations don't need to be seen.
501 if (!FD->isThisDeclarationADefinition())
502 return false;
503
504 // Constructors and destructors must be seen.
505 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
506 return true;
507
508 // Otherwise, this is required unless it is static.
509 //
510 // FIXME: Inlines.
511 return FD->getStorageClass() != FunctionDecl::Static;
512 } else {
513 const VarDecl *VD = cast<VarDecl>(D);
514
515 // In C++, this doesn't need to be seen if it is marked "extern".
516 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
517 (VD->getStorageClass() == VarDecl::Extern ||
518 VD->isExternC()))
519 return false;
520
521 // In C, this doesn't need to be seen unless it is a definition.
522 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
523 return false;
524
525 // Otherwise, this is required unless it is static.
526 return VD->getStorageClass() != VarDecl::Static;
527 }
528}
529
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000530void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000531 RecordData Record;
532 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000533
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000534 // If this declaration is also a DeclContext, write blocks for the
535 // declarations that lexically stored inside its context and those
536 // declarations that are visible from its context. These blocks
537 // are written before the declaration itself so that we can put
538 // their offsets into the record for the declaration.
539 uint64_t LexicalOffset = 0;
540 uint64_t VisibleOffset = 0;
541 DeclContext *DC = dyn_cast<DeclContext>(D);
542 if (DC) {
543 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
544 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000545 }
546
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000547 // Determine the ID for this declaration
548 pch::DeclID &ID = DeclIDs[D];
549 if (ID == 0)
550 ID = DeclIDs.size();
551
552 unsigned Index = ID - 1;
553
554 // Record the offset for this declaration
555 if (DeclOffsets.size() == Index)
556 DeclOffsets.push_back(Stream.GetCurrentBitNo());
557 else if (DeclOffsets.size() < Index) {
558 DeclOffsets.resize(Index+1);
559 DeclOffsets[Index] = Stream.GetCurrentBitNo();
560 }
561
562 // Build and emit a record for this declaration
563 Record.clear();
564 W.Code = (pch::DeclCode)0;
565 W.AbbrevToUse = 0;
566 W.Visit(D);
567 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
568
569 if (!W.Code) {
570 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
571 D->getDeclKindName());
572 assert(false && "Unhandled declaration kind while generating PCH");
573 exit(-1);
574 }
575 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
576
577 // If the declaration had any attributes, write them now.
578 if (D->hasAttrs())
579 WriteAttributeRecord(D->getAttrs());
580
581 // Flush any expressions that were written as part of this declaration.
582 FlushStmts();
583
584 // Note "external" declarations so that we can add them to a record in the
585 // PCH file later.
586 //
587 // FIXME: This should be renamed, the predicate is much more complicated.
588 if (isRequiredDecl(D, Context))
589 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000590}