blob: d3618df64ece482086650d1a0a909345c177a178 [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());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000130 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000131 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000132 // FIXME: maybe write optional qualifier and its range.
133 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000134}
135
136void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
137 VisitTagDecl(D);
138 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000139 Writer.AddTypeRef(D->getPromotionType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000140 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000141 Code = pch::DECL_ENUM;
142}
143
144void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
145 VisitTagDecl(D);
146 Record.push_back(D->hasFlexibleArrayMember());
147 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000148 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000149 Code = pch::DECL_RECORD;
150}
151
152void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
153 VisitNamedDecl(D);
154 Writer.AddTypeRef(D->getType(), Record);
155}
156
157void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
158 VisitValueDecl(D);
159 Record.push_back(D->getInitExpr()? 1 : 0);
160 if (D->getInitExpr())
161 Writer.AddStmt(D->getInitExpr());
162 Writer.AddAPSInt(D->getInitVal(), Record);
163 Code = pch::DECL_ENUM_CONSTANT;
164}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000165
166void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
167 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000168 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000169 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000170}
Chris Lattner12b1c762009-04-27 06:16:06 +0000171
172void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000173 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000174 Record.push_back(D->isThisDeclarationADefinition());
175 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000176 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000177 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
178 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000179 Record.push_back(D->getStorageClassAsWritten());
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000180 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000181 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000182 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000183 Record.push_back(D->hasInheritedPrototype());
184 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000185 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000186 Record.push_back(D->isTrivial());
187 Record.push_back(D->isCopyAssignment());
188 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000189 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000190 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000191 Record.push_back(D->param_size());
192 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
193 P != PEnd; ++P)
194 Writer.AddDeclRef(*P, Record);
195 Code = pch::DECL_FUNCTION;
196}
197
198void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
199 VisitNamedDecl(D);
200 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000201 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000202 Record.push_back(D->getBody() != 0);
203 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000204 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000205 Writer.AddDeclRef(D->getSelfDecl(), Record);
206 Writer.AddDeclRef(D->getCmdDecl(), Record);
207 }
208 Record.push_back(D->isInstanceMethod());
209 Record.push_back(D->isVariadic());
210 Record.push_back(D->isSynthesized());
211 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000212 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000213 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000214 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000215 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000216 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000217 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000218 Writer.AddSourceLocation(D->getLocEnd(), Record);
219 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000220 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000221 PEnd = D->param_end(); P != PEnd; ++P)
222 Writer.AddDeclRef(*P, Record);
223 Code = pch::DECL_OBJC_METHOD;
224}
225
226void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
227 VisitNamedDecl(D);
Ted Kremenek782f2f52010-01-07 01:20:12 +0000228 SourceRange R = D->getAtEndRange();
229 Writer.AddSourceLocation(R.getBegin(), Record);
230 Writer.AddSourceLocation(R.getEnd(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000231 // Abstract class (no need to define a stable pch::DECL code).
232}
233
234void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
235 VisitObjCContainerDecl(D);
236 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
237 Writer.AddDeclRef(D->getSuperClass(), Record);
238 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000239 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000240 PEnd = D->protocol_end();
241 P != PEnd; ++P)
242 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000243 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
244 PLEnd = D->protocol_loc_end();
245 PL != PLEnd; ++PL)
246 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000247 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000248 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000249 IEnd = D->ivar_end(); I != IEnd; ++I)
250 Writer.AddDeclRef(*I, Record);
251 Writer.AddDeclRef(D->getCategoryList(), Record);
252 Record.push_back(D->isForwardDecl());
253 Record.push_back(D->isImplicitInterfaceDecl());
254 Writer.AddSourceLocation(D->getClassLoc(), Record);
255 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
256 Writer.AddSourceLocation(D->getLocEnd(), Record);
257 Code = pch::DECL_OBJC_INTERFACE;
258}
259
260void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
261 VisitFieldDecl(D);
262 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000263 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000264 Code = pch::DECL_OBJC_IVAR;
265}
266
267void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
268 VisitObjCContainerDecl(D);
269 Record.push_back(D->isForwardDecl());
270 Writer.AddSourceLocation(D->getLocEnd(), Record);
271 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000272 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000273 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
274 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000275 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
276 PLEnd = D->protocol_loc_end();
277 PL != PLEnd; ++PL)
278 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000279 Code = pch::DECL_OBJC_PROTOCOL;
280}
281
282void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
283 VisitFieldDecl(D);
284 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
285}
286
287void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
288 VisitDecl(D);
289 Record.push_back(D->size());
290 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000291 Writer.AddDeclRef(I->getInterface(), Record);
292 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
293 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000294 Code = pch::DECL_OBJC_CLASS;
295}
296
297void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
298 VisitDecl(D);
299 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000300 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000301 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
302 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000303 for (ObjCForwardProtocolDecl::protocol_loc_iterator
304 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
305 PL != PLEnd; ++PL)
306 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000307 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
308}
309
310void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
311 VisitObjCContainerDecl(D);
312 Writer.AddDeclRef(D->getClassInterface(), Record);
313 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000314 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000315 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
316 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000317 for (ObjCCategoryDecl::protocol_loc_iterator
318 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
319 PL != PLEnd; ++PL)
320 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000321 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000322 Writer.AddSourceLocation(D->getAtLoc(), Record);
323 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000324 Code = pch::DECL_OBJC_CATEGORY;
325}
326
327void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
328 VisitNamedDecl(D);
329 Writer.AddDeclRef(D->getClassInterface(), Record);
330 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
331}
332
333void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
334 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000335 Writer.AddSourceLocation(D->getAtLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000336 Writer.AddTypeRef(D->getType(), Record);
337 // FIXME: stable encoding
338 Record.push_back((unsigned)D->getPropertyAttributes());
339 // FIXME: stable encoding
340 Record.push_back((unsigned)D->getPropertyImplementation());
341 Writer.AddDeclarationName(D->getGetterName(), Record);
342 Writer.AddDeclarationName(D->getSetterName(), Record);
343 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
344 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
345 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
346 Code = pch::DECL_OBJC_PROPERTY;
347}
348
349void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000350 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000351 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000352 // Abstract class (no need to define a stable pch::DECL code).
353}
354
355void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
356 VisitObjCImplDecl(D);
357 Writer.AddIdentifierRef(D->getIdentifier(), Record);
358 Code = pch::DECL_OBJC_CATEGORY_IMPL;
359}
360
361void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
362 VisitObjCImplDecl(D);
363 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000364 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000365 Code = pch::DECL_OBJC_IMPLEMENTATION;
366}
367
368void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
369 VisitDecl(D);
370 Writer.AddSourceLocation(D->getLocStart(), Record);
371 Writer.AddDeclRef(D->getPropertyDecl(), Record);
372 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000373 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000374 Code = pch::DECL_OBJC_PROPERTY_IMPL;
375}
376
377void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000378 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000379 Record.push_back(D->isMutable());
380 Record.push_back(D->getBitWidth()? 1 : 0);
381 if (D->getBitWidth())
382 Writer.AddStmt(D->getBitWidth());
383 Code = pch::DECL_FIELD;
384}
385
386void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000387 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000388 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000389 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000390 Record.push_back(D->isThreadSpecified());
391 Record.push_back(D->hasCXXDirectInitializer());
392 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000393 Record.push_back(D->isExceptionVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000394 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000395 Record.push_back(D->getInit()? 1 : 0);
396 if (D->getInit())
397 Writer.AddStmt(D->getInit());
398 Code = pch::DECL_VAR;
399}
400
401void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
402 VisitVarDecl(D);
403 Code = pch::DECL_IMPLICIT_PARAM;
404}
405
406void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
407 VisitVarDecl(D);
408 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000409 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000410 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000411
412
Chris Lattnerea5ce472009-04-27 07:35:58 +0000413 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
414 // we dynamically check for the properties that we optimize for, but don't
415 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000416 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000417 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000418 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000419 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000420 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000421 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000422 D->getStorageClass() == 0 &&
423 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000424 D->getObjCDeclQualifier() == 0 &&
425 !D->hasInheritedDefaultArg())
Chris Lattnerea5ce472009-04-27 07:35:58 +0000426 AbbrevToUse = Writer.getParmVarDeclAbbrev();
427
428 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
429 // just us assuming it.
430 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
431 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
432 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
433 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000434 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000435 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
436 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000437}
438
Chris Lattner12b1c762009-04-27 06:16:06 +0000439void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
440 VisitDecl(D);
441 Writer.AddStmt(D->getAsmString());
442 Code = pch::DECL_FILE_SCOPE_ASM;
443}
444
445void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
446 VisitDecl(D);
447 Writer.AddStmt(D->getBody());
448 Record.push_back(D->param_size());
449 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
450 P != PEnd; ++P)
451 Writer.AddDeclRef(*P, Record);
452 Code = pch::DECL_BLOCK;
453}
454
455/// \brief Emit the DeclContext part of a declaration context decl.
456///
457/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
458/// block for this declaration context is stored. May be 0 to indicate
459/// that there are no declarations stored within this context.
460///
461/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
462/// block for this declaration context is stored. May be 0 to indicate
463/// that there are no declarations visible from this context. Note
464/// that this value will not be emitted for non-primary declaration
465/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000466void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000467 uint64_t VisibleOffset) {
468 Record.push_back(LexicalOffset);
469 Record.push_back(VisibleOffset);
470}
471
472
473//===----------------------------------------------------------------------===//
474// PCHWriter Implementation
475//===----------------------------------------------------------------------===//
476
Chris Lattnerea5ce472009-04-27 07:35:58 +0000477void PCHWriter::WriteDeclsBlockAbbrevs() {
478 using namespace llvm;
479 // Abbreviation for DECL_PARM_VAR.
480 BitCodeAbbrev *Abv = new BitCodeAbbrev();
481 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
482
483 // Decl
484 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
485 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
486 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
487 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
488 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
489 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000490 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000491 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000492 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattnerea5ce472009-04-27 07:35:58 +0000494 // NamedDecl
495 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
496 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
497 // ValueDecl
498 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000499 // DeclaratorDecl
500 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000501 // VarDecl
502 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000503 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000504 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
505 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
506 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000507 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000508 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000509 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
510 // ParmVarDecl
511 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000512 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattnerea5ce472009-04-27 07:35:58 +0000514 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
515}
516
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000517/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
518/// consumers of the AST.
519///
520/// Such decls will always be deserialized from the PCH file, so we would like
521/// this to be as restrictive as possible. Currently the predicate is driven by
522/// code generation requirements, if other clients have a different notion of
523/// what is "required" then we may have to consider an alternate scheme where
524/// clients can iterate over the top-level decls and get information on them,
525/// without necessary deserializing them. We could explicitly require such
526/// clients to use a separate API call to "realize" the decl. This should be
527/// relatively painless since they would presumably only do it for top-level
528/// decls.
529//
530// FIXME: This predicate is essentially IRgen's predicate to determine whether a
531// declaration can be deferred. Merge them somehow.
532static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
533 // File scoped assembly must be seen.
534 if (isa<FileScopeAsmDecl>(D))
535 return true;
536
537 // Otherwise if this isn't a function or a file scoped variable it doesn't
538 // need to be seen.
539 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
540 if (!VD->isFileVarDecl())
541 return false;
542 } else if (!isa<FunctionDecl>(D))
543 return false;
544
545 // Aliases and used decls must be seen.
546 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
547 return true;
548
549 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
550 // Forward declarations don't need to be seen.
551 if (!FD->isThisDeclarationADefinition())
552 return false;
553
554 // Constructors and destructors must be seen.
555 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
556 return true;
557
558 // Otherwise, this is required unless it is static.
559 //
560 // FIXME: Inlines.
561 return FD->getStorageClass() != FunctionDecl::Static;
562 } else {
563 const VarDecl *VD = cast<VarDecl>(D);
564
565 // In C++, this doesn't need to be seen if it is marked "extern".
566 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
567 (VD->getStorageClass() == VarDecl::Extern ||
568 VD->isExternC()))
569 return false;
570
571 // In C, this doesn't need to be seen unless it is a definition.
572 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
573 return false;
574
575 // Otherwise, this is required unless it is static.
576 return VD->getStorageClass() != VarDecl::Static;
577 }
578}
579
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000580void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000581 RecordData Record;
582 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000583
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000584 // If this declaration is also a DeclContext, write blocks for the
585 // declarations that lexically stored inside its context and those
586 // declarations that are visible from its context. These blocks
587 // are written before the declaration itself so that we can put
588 // their offsets into the record for the declaration.
589 uint64_t LexicalOffset = 0;
590 uint64_t VisibleOffset = 0;
591 DeclContext *DC = dyn_cast<DeclContext>(D);
592 if (DC) {
593 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
594 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000595 }
596
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000597 // Determine the ID for this declaration
598 pch::DeclID &ID = DeclIDs[D];
599 if (ID == 0)
600 ID = DeclIDs.size();
601
602 unsigned Index = ID - 1;
603
604 // Record the offset for this declaration
605 if (DeclOffsets.size() == Index)
606 DeclOffsets.push_back(Stream.GetCurrentBitNo());
607 else if (DeclOffsets.size() < Index) {
608 DeclOffsets.resize(Index+1);
609 DeclOffsets[Index] = Stream.GetCurrentBitNo();
610 }
611
612 // Build and emit a record for this declaration
613 Record.clear();
614 W.Code = (pch::DeclCode)0;
615 W.AbbrevToUse = 0;
616 W.Visit(D);
617 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
618
Daniel Dunbar33671982009-12-03 09:13:36 +0000619 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000620 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000621 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000622 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
623
624 // If the declaration had any attributes, write them now.
625 if (D->hasAttrs())
626 WriteAttributeRecord(D->getAttrs());
627
628 // Flush any expressions that were written as part of this declaration.
629 FlushStmts();
630
631 // Note "external" declarations so that we can add them to a record in the
632 // PCH file later.
633 //
634 // FIXME: This should be renamed, the predicate is much more complicated.
635 if (isRequiredDecl(D, Context))
636 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000637}