blob: 6f4cd8f36177940a7108f0141b969dbc3cb1abd6 [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());
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);
John McCall842aef82009-12-09 09:09:27 +0000126 Writer.AddTypeRef(D->getPromotionType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000127 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000128 Code = pch::DECL_ENUM;
129}
130
131void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
132 VisitTagDecl(D);
133 Record.push_back(D->hasFlexibleArrayMember());
134 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000135 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000136 Code = pch::DECL_RECORD;
137}
138
139void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
140 VisitNamedDecl(D);
141 Writer.AddTypeRef(D->getType(), Record);
142}
143
144void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
145 VisitValueDecl(D);
146 Record.push_back(D->getInitExpr()? 1 : 0);
147 if (D->getInitExpr())
148 Writer.AddStmt(D->getInitExpr());
149 Writer.AddAPSInt(D->getInitVal(), Record);
150 Code = pch::DECL_ENUM_CONSTANT;
151}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000152
153void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
154 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000155 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000156}
Chris Lattner12b1c762009-04-27 06:16:06 +0000157
158void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000159 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000160 Record.push_back(D->isThisDeclarationADefinition());
161 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000162 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000163 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
164 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000165 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000166 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000167 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000168 Record.push_back(D->hasInheritedPrototype());
169 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000170 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000171 Record.push_back(D->isTrivial());
172 Record.push_back(D->isCopyAssignment());
173 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000174 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000175 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000176 Record.push_back(D->param_size());
177 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
178 P != PEnd; ++P)
179 Writer.AddDeclRef(*P, Record);
180 Code = pch::DECL_FUNCTION;
181}
182
183void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
184 VisitNamedDecl(D);
185 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000186 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000187 Record.push_back(D->getBody() != 0);
188 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000189 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000190 Writer.AddDeclRef(D->getSelfDecl(), Record);
191 Writer.AddDeclRef(D->getCmdDecl(), Record);
192 }
193 Record.push_back(D->isInstanceMethod());
194 Record.push_back(D->isVariadic());
195 Record.push_back(D->isSynthesized());
196 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000197 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000198 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000199 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000200 Writer.AddTypeRef(D->getResultType(), Record);
201 Writer.AddSourceLocation(D->getLocEnd(), Record);
202 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000203 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000204 PEnd = D->param_end(); P != PEnd; ++P)
205 Writer.AddDeclRef(*P, Record);
206 Code = pch::DECL_OBJC_METHOD;
207}
208
209void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
210 VisitNamedDecl(D);
Ted Kremenek782f2f52010-01-07 01:20:12 +0000211 SourceRange R = D->getAtEndRange();
212 Writer.AddSourceLocation(R.getBegin(), Record);
213 Writer.AddSourceLocation(R.getEnd(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000214 // Abstract class (no need to define a stable pch::DECL code).
215}
216
217void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
218 VisitObjCContainerDecl(D);
219 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
220 Writer.AddDeclRef(D->getSuperClass(), Record);
221 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000222 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000223 PEnd = D->protocol_end();
224 P != PEnd; ++P)
225 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000226 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
227 PLEnd = D->protocol_loc_end();
228 PL != PLEnd; ++PL)
229 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000230 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000231 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000232 IEnd = D->ivar_end(); I != IEnd; ++I)
233 Writer.AddDeclRef(*I, Record);
234 Writer.AddDeclRef(D->getCategoryList(), Record);
235 Record.push_back(D->isForwardDecl());
236 Record.push_back(D->isImplicitInterfaceDecl());
237 Writer.AddSourceLocation(D->getClassLoc(), Record);
238 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
239 Writer.AddSourceLocation(D->getLocEnd(), Record);
240 Code = pch::DECL_OBJC_INTERFACE;
241}
242
243void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
244 VisitFieldDecl(D);
245 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000246 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000247 Code = pch::DECL_OBJC_IVAR;
248}
249
250void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
251 VisitObjCContainerDecl(D);
252 Record.push_back(D->isForwardDecl());
253 Writer.AddSourceLocation(D->getLocEnd(), Record);
254 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000255 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000256 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
257 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000258 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
259 PLEnd = D->protocol_loc_end();
260 PL != PLEnd; ++PL)
261 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000262 Code = pch::DECL_OBJC_PROTOCOL;
263}
264
265void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
266 VisitFieldDecl(D);
267 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
268}
269
270void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
271 VisitDecl(D);
272 Record.push_back(D->size());
273 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000274 Writer.AddDeclRef(I->getInterface(), Record);
275 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
276 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000277 Code = pch::DECL_OBJC_CLASS;
278}
279
280void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
281 VisitDecl(D);
282 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000283 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000284 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
285 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000286 for (ObjCForwardProtocolDecl::protocol_loc_iterator
287 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
288 PL != PLEnd; ++PL)
289 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000290 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
291}
292
293void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
294 VisitObjCContainerDecl(D);
295 Writer.AddDeclRef(D->getClassInterface(), Record);
296 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000297 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000298 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
299 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000300 for (ObjCCategoryDecl::protocol_loc_iterator
301 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
302 PL != PLEnd; ++PL)
303 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000304 Writer.AddDeclRef(D->getNextClassCategory(), Record);
305 Writer.AddSourceLocation(D->getLocEnd(), Record);
306 Code = pch::DECL_OBJC_CATEGORY;
307}
308
309void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
310 VisitNamedDecl(D);
311 Writer.AddDeclRef(D->getClassInterface(), Record);
312 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
313}
314
315void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
316 VisitNamedDecl(D);
317 Writer.AddTypeRef(D->getType(), Record);
318 // FIXME: stable encoding
319 Record.push_back((unsigned)D->getPropertyAttributes());
320 // FIXME: stable encoding
321 Record.push_back((unsigned)D->getPropertyImplementation());
322 Writer.AddDeclarationName(D->getGetterName(), Record);
323 Writer.AddDeclarationName(D->getSetterName(), Record);
324 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
325 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
326 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
327 Code = pch::DECL_OBJC_PROPERTY;
328}
329
330void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000331 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000332 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000333 // Abstract class (no need to define a stable pch::DECL code).
334}
335
336void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
337 VisitObjCImplDecl(D);
338 Writer.AddIdentifierRef(D->getIdentifier(), Record);
339 Code = pch::DECL_OBJC_CATEGORY_IMPL;
340}
341
342void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
343 VisitObjCImplDecl(D);
344 Writer.AddDeclRef(D->getSuperClass(), Record);
345 Code = pch::DECL_OBJC_IMPLEMENTATION;
346}
347
348void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
349 VisitDecl(D);
350 Writer.AddSourceLocation(D->getLocStart(), Record);
351 Writer.AddDeclRef(D->getPropertyDecl(), Record);
352 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
353 Code = pch::DECL_OBJC_PROPERTY_IMPL;
354}
355
356void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000357 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000358 Record.push_back(D->isMutable());
359 Record.push_back(D->getBitWidth()? 1 : 0);
360 if (D->getBitWidth())
361 Writer.AddStmt(D->getBitWidth());
362 Code = pch::DECL_FIELD;
363}
364
365void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000366 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000367 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
368 Record.push_back(D->isThreadSpecified());
369 Record.push_back(D->hasCXXDirectInitializer());
370 Record.push_back(D->isDeclaredInCondition());
371 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000372 Record.push_back(D->getInit()? 1 : 0);
373 if (D->getInit())
374 Writer.AddStmt(D->getInit());
375 Code = pch::DECL_VAR;
376}
377
378void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
379 VisitVarDecl(D);
380 Code = pch::DECL_IMPLICIT_PARAM;
381}
382
383void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
384 VisitVarDecl(D);
385 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000386 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000387
388
Chris Lattnerea5ce472009-04-27 07:35:58 +0000389 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
390 // we dynamically check for the properties that we optimize for, but don't
391 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000392 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000393 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000394 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000395 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000396 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000397 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000398 D->getStorageClass() == 0 &&
399 !D->hasCXXDirectInitializer() && // Can params have this ever?
400 D->getObjCDeclQualifier() == 0)
401 AbbrevToUse = Writer.getParmVarDeclAbbrev();
402
403 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
404 // just us assuming it.
405 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
406 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
407 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
408 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
409 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
410 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000411}
412
Chris Lattner12b1c762009-04-27 06:16:06 +0000413void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
414 VisitDecl(D);
415 Writer.AddStmt(D->getAsmString());
416 Code = pch::DECL_FILE_SCOPE_ASM;
417}
418
419void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
420 VisitDecl(D);
421 Writer.AddStmt(D->getBody());
422 Record.push_back(D->param_size());
423 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
424 P != PEnd; ++P)
425 Writer.AddDeclRef(*P, Record);
426 Code = pch::DECL_BLOCK;
427}
428
429/// \brief Emit the DeclContext part of a declaration context decl.
430///
431/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
432/// block for this declaration context is stored. May be 0 to indicate
433/// that there are no declarations stored within this context.
434///
435/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
436/// block for this declaration context is stored. May be 0 to indicate
437/// that there are no declarations visible from this context. Note
438/// that this value will not be emitted for non-primary declaration
439/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000440void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000441 uint64_t VisibleOffset) {
442 Record.push_back(LexicalOffset);
443 Record.push_back(VisibleOffset);
444}
445
446
447//===----------------------------------------------------------------------===//
448// PCHWriter Implementation
449//===----------------------------------------------------------------------===//
450
Chris Lattnerea5ce472009-04-27 07:35:58 +0000451void PCHWriter::WriteDeclsBlockAbbrevs() {
452 using namespace llvm;
453 // Abbreviation for DECL_PARM_VAR.
454 BitCodeAbbrev *Abv = new BitCodeAbbrev();
455 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
456
457 // Decl
458 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
459 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
460 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
461 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
462 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
463 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000464 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000465 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000466 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000467
Chris Lattnerea5ce472009-04-27 07:35:58 +0000468 // NamedDecl
469 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
470 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
471 // ValueDecl
472 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000473 // DeclaratorDecl
474 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000475 // VarDecl
476 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
477 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
478 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
479 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
480 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000481 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
482 // ParmVarDecl
483 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Chris Lattnerea5ce472009-04-27 07:35:58 +0000485 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
486}
487
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000488/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
489/// consumers of the AST.
490///
491/// Such decls will always be deserialized from the PCH file, so we would like
492/// this to be as restrictive as possible. Currently the predicate is driven by
493/// code generation requirements, if other clients have a different notion of
494/// what is "required" then we may have to consider an alternate scheme where
495/// clients can iterate over the top-level decls and get information on them,
496/// without necessary deserializing them. We could explicitly require such
497/// clients to use a separate API call to "realize" the decl. This should be
498/// relatively painless since they would presumably only do it for top-level
499/// decls.
500//
501// FIXME: This predicate is essentially IRgen's predicate to determine whether a
502// declaration can be deferred. Merge them somehow.
503static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
504 // File scoped assembly must be seen.
505 if (isa<FileScopeAsmDecl>(D))
506 return true;
507
508 // Otherwise if this isn't a function or a file scoped variable it doesn't
509 // need to be seen.
510 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
511 if (!VD->isFileVarDecl())
512 return false;
513 } else if (!isa<FunctionDecl>(D))
514 return false;
515
516 // Aliases and used decls must be seen.
517 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
518 return true;
519
520 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
521 // Forward declarations don't need to be seen.
522 if (!FD->isThisDeclarationADefinition())
523 return false;
524
525 // Constructors and destructors must be seen.
526 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
527 return true;
528
529 // Otherwise, this is required unless it is static.
530 //
531 // FIXME: Inlines.
532 return FD->getStorageClass() != FunctionDecl::Static;
533 } else {
534 const VarDecl *VD = cast<VarDecl>(D);
535
536 // In C++, this doesn't need to be seen if it is marked "extern".
537 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
538 (VD->getStorageClass() == VarDecl::Extern ||
539 VD->isExternC()))
540 return false;
541
542 // In C, this doesn't need to be seen unless it is a definition.
543 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
544 return false;
545
546 // Otherwise, this is required unless it is static.
547 return VD->getStorageClass() != VarDecl::Static;
548 }
549}
550
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000551void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000552 RecordData Record;
553 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000554
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000555 // If this declaration is also a DeclContext, write blocks for the
556 // declarations that lexically stored inside its context and those
557 // declarations that are visible from its context. These blocks
558 // are written before the declaration itself so that we can put
559 // their offsets into the record for the declaration.
560 uint64_t LexicalOffset = 0;
561 uint64_t VisibleOffset = 0;
562 DeclContext *DC = dyn_cast<DeclContext>(D);
563 if (DC) {
564 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
565 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000566 }
567
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000568 // Determine the ID for this declaration
569 pch::DeclID &ID = DeclIDs[D];
570 if (ID == 0)
571 ID = DeclIDs.size();
572
573 unsigned Index = ID - 1;
574
575 // Record the offset for this declaration
576 if (DeclOffsets.size() == Index)
577 DeclOffsets.push_back(Stream.GetCurrentBitNo());
578 else if (DeclOffsets.size() < Index) {
579 DeclOffsets.resize(Index+1);
580 DeclOffsets[Index] = Stream.GetCurrentBitNo();
581 }
582
583 // Build and emit a record for this declaration
584 Record.clear();
585 W.Code = (pch::DeclCode)0;
586 W.AbbrevToUse = 0;
587 W.Visit(D);
588 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
589
Daniel Dunbar33671982009-12-03 09:13:36 +0000590 if (!W.Code)
591 llvm::llvm_report_error(llvm::StringRef("unexpected declaration kind '") +
592 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000593 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
594
595 // If the declaration had any attributes, write them now.
596 if (D->hasAttrs())
597 WriteAttributeRecord(D->getAttrs());
598
599 // Flush any expressions that were written as part of this declaration.
600 FlushStmts();
601
602 // Note "external" declarations so that we can add them to a record in the
603 // PCH file later.
604 //
605 // FIXME: This should be renamed, the predicate is much more complicated.
606 if (isRequiredDecl(D, Context))
607 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000608}