blob: 0774797463e06f97b5e46faa03b2058a1c7d275a [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);
Douglas Gregor0cef4832010-02-21 18:22:14 +000045 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000046 void VisitTypeDecl(TypeDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitTagDecl(TagDecl *D);
49 void VisitEnumDecl(EnumDecl *D);
50 void VisitRecordDecl(RecordDecl *D);
51 void VisitValueDecl(ValueDecl *D);
52 void VisitEnumConstantDecl(EnumConstantDecl *D);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +000053 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000054 void VisitFunctionDecl(FunctionDecl *D);
55 void VisitFieldDecl(FieldDecl *D);
56 void VisitVarDecl(VarDecl *D);
57 void VisitImplicitParamDecl(ImplicitParamDecl *D);
58 void VisitParmVarDecl(ParmVarDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000059 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
60 void VisitBlockDecl(BlockDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000061 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000062 uint64_t VisibleOffset);
63 void VisitObjCMethodDecl(ObjCMethodDecl *D);
64 void VisitObjCContainerDecl(ObjCContainerDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCIvarDecl(ObjCIvarDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
69 void VisitObjCClassDecl(ObjCClassDecl *D);
70 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
71 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
72 void VisitObjCImplDecl(ObjCImplDecl *D);
73 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
74 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
75 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
76 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
77 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
78 };
79}
80
81void PCHDeclWriter::VisitDecl(Decl *D) {
82 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
83 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
84 Writer.AddSourceLocation(D->getLocation(), Record);
85 Record.push_back(D->isInvalidDecl());
86 Record.push_back(D->hasAttrs());
87 Record.push_back(D->isImplicit());
Douglas Gregore0762c92009-06-19 23:52:42 +000088 Record.push_back(D->isUsed());
Chris Lattner12b1c762009-04-27 06:16:06 +000089 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000090 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +000091}
92
93void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
94 VisitDecl(D);
95 Code = pch::DECL_TRANSLATION_UNIT;
96}
97
98void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
99 VisitDecl(D);
100 Writer.AddDeclarationName(D->getDeclName(), Record);
101}
102
Douglas Gregor0cef4832010-02-21 18:22:14 +0000103void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
104 VisitNamedDecl(D);
105 Writer.AddSourceLocation(D->getLBracLoc(), Record);
106 Writer.AddSourceLocation(D->getRBracLoc(), Record);
107 Writer.AddDeclRef(D->getNextNamespace(), Record);
108 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
109 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
110 Code = pch::DECL_NAMESPACE;
111}
112
Chris Lattner12b1c762009-04-27 06:16:06 +0000113void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
114 VisitNamedDecl(D);
115 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
116}
117
118void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
119 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000120 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000121 Code = pch::DECL_TYPEDEF;
122}
123
124void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
125 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000126 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000127 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
128 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000129 Record.push_back(D->isEmbeddedInDeclarator());
Chris Lattner12b1c762009-04-27 06:16:06 +0000130 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000131 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000132 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000133}
134
135void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
136 VisitTagDecl(D);
137 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000138 Writer.AddTypeRef(D->getPromotionType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000139 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000140 Code = pch::DECL_ENUM;
141}
142
143void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
144 VisitTagDecl(D);
145 Record.push_back(D->hasFlexibleArrayMember());
146 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000147 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000148 Code = pch::DECL_RECORD;
149}
150
151void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
152 VisitNamedDecl(D);
153 Writer.AddTypeRef(D->getType(), Record);
154}
155
156void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
157 VisitValueDecl(D);
158 Record.push_back(D->getInitExpr()? 1 : 0);
159 if (D->getInitExpr())
160 Writer.AddStmt(D->getInitExpr());
161 Writer.AddAPSInt(D->getInitVal(), Record);
162 Code = pch::DECL_ENUM_CONSTANT;
163}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000164
165void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
166 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000167 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000168}
Chris Lattner12b1c762009-04-27 06:16:06 +0000169
170void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000171 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000172 Record.push_back(D->isThisDeclarationADefinition());
173 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000174 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000175 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
176 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000177 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000178 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000179 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000180 Record.push_back(D->hasInheritedPrototype());
181 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000182 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000183 Record.push_back(D->isTrivial());
184 Record.push_back(D->isCopyAssignment());
185 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000186 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000187 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000188 Record.push_back(D->param_size());
189 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
190 P != PEnd; ++P)
191 Writer.AddDeclRef(*P, Record);
192 Code = pch::DECL_FUNCTION;
193}
194
195void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
196 VisitNamedDecl(D);
197 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000198 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000199 Record.push_back(D->getBody() != 0);
200 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000201 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000202 Writer.AddDeclRef(D->getSelfDecl(), Record);
203 Writer.AddDeclRef(D->getCmdDecl(), Record);
204 }
205 Record.push_back(D->isInstanceMethod());
206 Record.push_back(D->isVariadic());
207 Record.push_back(D->isSynthesized());
208 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000209 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000210 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000211 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000212 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000213 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000214 Writer.AddSourceLocation(D->getLocEnd(), Record);
215 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000216 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000217 PEnd = D->param_end(); P != PEnd; ++P)
218 Writer.AddDeclRef(*P, Record);
219 Code = pch::DECL_OBJC_METHOD;
220}
221
222void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
223 VisitNamedDecl(D);
Ted Kremenek782f2f52010-01-07 01:20:12 +0000224 SourceRange R = D->getAtEndRange();
225 Writer.AddSourceLocation(R.getBegin(), Record);
226 Writer.AddSourceLocation(R.getEnd(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000227 // Abstract class (no need to define a stable pch::DECL code).
228}
229
230void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
231 VisitObjCContainerDecl(D);
232 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
233 Writer.AddDeclRef(D->getSuperClass(), Record);
234 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000235 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000236 PEnd = D->protocol_end();
237 P != PEnd; ++P)
238 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000239 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
240 PLEnd = D->protocol_loc_end();
241 PL != PLEnd; ++PL)
242 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000243 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000244 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000245 IEnd = D->ivar_end(); I != IEnd; ++I)
246 Writer.AddDeclRef(*I, Record);
247 Writer.AddDeclRef(D->getCategoryList(), Record);
248 Record.push_back(D->isForwardDecl());
249 Record.push_back(D->isImplicitInterfaceDecl());
250 Writer.AddSourceLocation(D->getClassLoc(), Record);
251 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
252 Writer.AddSourceLocation(D->getLocEnd(), Record);
253 Code = pch::DECL_OBJC_INTERFACE;
254}
255
256void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
257 VisitFieldDecl(D);
258 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000259 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000260 Code = pch::DECL_OBJC_IVAR;
261}
262
263void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
264 VisitObjCContainerDecl(D);
265 Record.push_back(D->isForwardDecl());
266 Writer.AddSourceLocation(D->getLocEnd(), Record);
267 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000268 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000269 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
270 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000271 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
272 PLEnd = D->protocol_loc_end();
273 PL != PLEnd; ++PL)
274 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000275 Code = pch::DECL_OBJC_PROTOCOL;
276}
277
278void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
279 VisitFieldDecl(D);
280 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
281}
282
283void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
284 VisitDecl(D);
285 Record.push_back(D->size());
286 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000287 Writer.AddDeclRef(I->getInterface(), Record);
288 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
289 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000290 Code = pch::DECL_OBJC_CLASS;
291}
292
293void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
294 VisitDecl(D);
295 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000296 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000297 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
298 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000299 for (ObjCForwardProtocolDecl::protocol_loc_iterator
300 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
301 PL != PLEnd; ++PL)
302 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000303 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
304}
305
306void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
307 VisitObjCContainerDecl(D);
308 Writer.AddDeclRef(D->getClassInterface(), Record);
309 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000310 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000311 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
312 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000313 for (ObjCCategoryDecl::protocol_loc_iterator
314 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
315 PL != PLEnd; ++PL)
316 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000317 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000318 Writer.AddSourceLocation(D->getAtLoc(), Record);
319 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000320 Code = pch::DECL_OBJC_CATEGORY;
321}
322
323void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
324 VisitNamedDecl(D);
325 Writer.AddDeclRef(D->getClassInterface(), Record);
326 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
327}
328
329void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
330 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000331 Writer.AddSourceLocation(D->getAtLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000332 Writer.AddTypeRef(D->getType(), Record);
333 // FIXME: stable encoding
334 Record.push_back((unsigned)D->getPropertyAttributes());
335 // FIXME: stable encoding
336 Record.push_back((unsigned)D->getPropertyImplementation());
337 Writer.AddDeclarationName(D->getGetterName(), Record);
338 Writer.AddDeclarationName(D->getSetterName(), Record);
339 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
340 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
341 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
342 Code = pch::DECL_OBJC_PROPERTY;
343}
344
345void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000346 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000347 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000348 // Abstract class (no need to define a stable pch::DECL code).
349}
350
351void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
352 VisitObjCImplDecl(D);
353 Writer.AddIdentifierRef(D->getIdentifier(), Record);
354 Code = pch::DECL_OBJC_CATEGORY_IMPL;
355}
356
357void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
358 VisitObjCImplDecl(D);
359 Writer.AddDeclRef(D->getSuperClass(), Record);
360 Code = pch::DECL_OBJC_IMPLEMENTATION;
361}
362
363void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
364 VisitDecl(D);
365 Writer.AddSourceLocation(D->getLocStart(), Record);
366 Writer.AddDeclRef(D->getPropertyDecl(), Record);
367 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
368 Code = pch::DECL_OBJC_PROPERTY_IMPL;
369}
370
371void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000372 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000373 Record.push_back(D->isMutable());
374 Record.push_back(D->getBitWidth()? 1 : 0);
375 if (D->getBitWidth())
376 Writer.AddStmt(D->getBitWidth());
377 Code = pch::DECL_FIELD;
378}
379
380void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000381 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000382 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
383 Record.push_back(D->isThreadSpecified());
384 Record.push_back(D->hasCXXDirectInitializer());
385 Record.push_back(D->isDeclaredInCondition());
386 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000387 Record.push_back(D->getInit()? 1 : 0);
388 if (D->getInit())
389 Writer.AddStmt(D->getInit());
390 Code = pch::DECL_VAR;
391}
392
393void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
394 VisitVarDecl(D);
395 Code = pch::DECL_IMPLICIT_PARAM;
396}
397
398void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
399 VisitVarDecl(D);
400 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000401 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000402
403
Chris Lattnerea5ce472009-04-27 07:35:58 +0000404 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
405 // we dynamically check for the properties that we optimize for, but don't
406 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000407 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000408 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000409 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000410 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000411 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000412 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000413 D->getStorageClass() == 0 &&
414 !D->hasCXXDirectInitializer() && // Can params have this ever?
415 D->getObjCDeclQualifier() == 0)
416 AbbrevToUse = Writer.getParmVarDeclAbbrev();
417
418 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
419 // just us assuming it.
420 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
421 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
422 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
423 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
424 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
425 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000426}
427
Chris Lattner12b1c762009-04-27 06:16:06 +0000428void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
429 VisitDecl(D);
430 Writer.AddStmt(D->getAsmString());
431 Code = pch::DECL_FILE_SCOPE_ASM;
432}
433
434void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
435 VisitDecl(D);
436 Writer.AddStmt(D->getBody());
437 Record.push_back(D->param_size());
438 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
439 P != PEnd; ++P)
440 Writer.AddDeclRef(*P, Record);
441 Code = pch::DECL_BLOCK;
442}
443
444/// \brief Emit the DeclContext part of a declaration context decl.
445///
446/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
447/// block for this declaration context is stored. May be 0 to indicate
448/// that there are no declarations stored within this context.
449///
450/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
451/// block for this declaration context is stored. May be 0 to indicate
452/// that there are no declarations visible from this context. Note
453/// that this value will not be emitted for non-primary declaration
454/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000455void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000456 uint64_t VisibleOffset) {
457 Record.push_back(LexicalOffset);
458 Record.push_back(VisibleOffset);
459}
460
461
462//===----------------------------------------------------------------------===//
463// PCHWriter Implementation
464//===----------------------------------------------------------------------===//
465
Chris Lattnerea5ce472009-04-27 07:35:58 +0000466void PCHWriter::WriteDeclsBlockAbbrevs() {
467 using namespace llvm;
468 // Abbreviation for DECL_PARM_VAR.
469 BitCodeAbbrev *Abv = new BitCodeAbbrev();
470 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
471
472 // Decl
473 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
474 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
475 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
476 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
477 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
478 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000479 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000480 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000481 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000482
Chris Lattnerea5ce472009-04-27 07:35:58 +0000483 // NamedDecl
484 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
485 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
486 // ValueDecl
487 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000488 // DeclaratorDecl
489 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000490 // VarDecl
491 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
492 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
493 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
494 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
495 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000496 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
497 // ParmVarDecl
498 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattnerea5ce472009-04-27 07:35:58 +0000500 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
501}
502
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000503/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
504/// consumers of the AST.
505///
506/// Such decls will always be deserialized from the PCH file, so we would like
507/// this to be as restrictive as possible. Currently the predicate is driven by
508/// code generation requirements, if other clients have a different notion of
509/// what is "required" then we may have to consider an alternate scheme where
510/// clients can iterate over the top-level decls and get information on them,
511/// without necessary deserializing them. We could explicitly require such
512/// clients to use a separate API call to "realize" the decl. This should be
513/// relatively painless since they would presumably only do it for top-level
514/// decls.
515//
516// FIXME: This predicate is essentially IRgen's predicate to determine whether a
517// declaration can be deferred. Merge them somehow.
518static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
519 // File scoped assembly must be seen.
520 if (isa<FileScopeAsmDecl>(D))
521 return true;
522
523 // Otherwise if this isn't a function or a file scoped variable it doesn't
524 // need to be seen.
525 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
526 if (!VD->isFileVarDecl())
527 return false;
528 } else if (!isa<FunctionDecl>(D))
529 return false;
530
531 // Aliases and used decls must be seen.
532 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
533 return true;
534
535 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
536 // Forward declarations don't need to be seen.
537 if (!FD->isThisDeclarationADefinition())
538 return false;
539
540 // Constructors and destructors must be seen.
541 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
542 return true;
543
544 // Otherwise, this is required unless it is static.
545 //
546 // FIXME: Inlines.
547 return FD->getStorageClass() != FunctionDecl::Static;
548 } else {
549 const VarDecl *VD = cast<VarDecl>(D);
550
551 // In C++, this doesn't need to be seen if it is marked "extern".
552 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
553 (VD->getStorageClass() == VarDecl::Extern ||
554 VD->isExternC()))
555 return false;
556
557 // In C, this doesn't need to be seen unless it is a definition.
558 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
559 return false;
560
561 // Otherwise, this is required unless it is static.
562 return VD->getStorageClass() != VarDecl::Static;
563 }
564}
565
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000566void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000567 RecordData Record;
568 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000569
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000570 // If this declaration is also a DeclContext, write blocks for the
571 // declarations that lexically stored inside its context and those
572 // declarations that are visible from its context. These blocks
573 // are written before the declaration itself so that we can put
574 // their offsets into the record for the declaration.
575 uint64_t LexicalOffset = 0;
576 uint64_t VisibleOffset = 0;
577 DeclContext *DC = dyn_cast<DeclContext>(D);
578 if (DC) {
579 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
580 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000581 }
582
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000583 // Determine the ID for this declaration
584 pch::DeclID &ID = DeclIDs[D];
585 if (ID == 0)
586 ID = DeclIDs.size();
587
588 unsigned Index = ID - 1;
589
590 // Record the offset for this declaration
591 if (DeclOffsets.size() == Index)
592 DeclOffsets.push_back(Stream.GetCurrentBitNo());
593 else if (DeclOffsets.size() < Index) {
594 DeclOffsets.resize(Index+1);
595 DeclOffsets[Index] = Stream.GetCurrentBitNo();
596 }
597
598 // Build and emit a record for this declaration
599 Record.clear();
600 W.Code = (pch::DeclCode)0;
601 W.AbbrevToUse = 0;
602 W.Visit(D);
603 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
604
Daniel Dunbar33671982009-12-03 09:13:36 +0000605 if (!W.Code)
606 llvm::llvm_report_error(llvm::StringRef("unexpected declaration kind '") +
607 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000608 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
609
610 // If the declaration had any attributes, write them now.
611 if (D->hasAttrs())
612 WriteAttributeRecord(D->getAttrs());
613
614 // Flush any expressions that were written as part of this declaration.
615 FlushStmts();
616
617 // Note "external" declarations so that we can add them to a record in the
618 // PCH file later.
619 //
620 // FIXME: This should be renamed, the predicate is much more complicated.
621 if (isRequiredDecl(D, Context))
622 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000623}