blob: 499a16adb1eff1035847d108101978a795794e57 [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"
Chris Lattner6ad9ac02010-05-07 21:43:38 +000016#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclTemplate.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000018#include "clang/AST/Expr.h"
Daniel Dunbar33671982009-12-03 09:13:36 +000019#include "llvm/ADT/Twine.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000020#include "llvm/Bitcode/BitstreamWriter.h"
Daniel Dunbar33671982009-12-03 09:13:36 +000021#include "llvm/Support/ErrorHandling.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000022using namespace clang;
23
24//===----------------------------------------------------------------------===//
25// Declaration serialization
26//===----------------------------------------------------------------------===//
27
28namespace {
29 class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
30
31 PCHWriter &Writer;
32 ASTContext &Context;
33 PCHWriter::RecordData &Record;
34
35 public:
36 pch::DeclCode Code;
Chris Lattnerea5ce472009-04-27 07:35:58 +000037 unsigned AbbrevToUse;
Chris Lattner12b1c762009-04-27 06:16:06 +000038
Mike Stump1eb44332009-09-09 15:08:12 +000039 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
40 PCHWriter::RecordData &Record)
Chris Lattnerea5ce472009-04-27 07:35:58 +000041 : Writer(Writer), Context(Context), Record(Record) {
42 }
Chris Lattner12b1c762009-04-27 06:16:06 +000043
44 void VisitDecl(Decl *D);
45 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
46 void VisitNamedDecl(NamedDecl *D);
Douglas Gregor0cef4832010-02-21 18:22:14 +000047 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000048 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
49 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000050 void VisitTypeDecl(TypeDecl *D);
51 void VisitTypedefDecl(TypedefDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000052 void VisitUnresolvedUsingTypename(UnresolvedUsingTypenameDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000053 void VisitTagDecl(TagDecl *D);
54 void VisitEnumDecl(EnumDecl *D);
55 void VisitRecordDecl(RecordDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000056 void VisitCXXRecordDecl(CXXRecordDecl *D);
57 void VisitClassTemplateSpecializationDecl(
58 ClassTemplateSpecializationDecl *D);
59 void VisitClassTemplatePartialSpecializationDecl(
60 ClassTemplatePartialSpecializationDecl *D);
61 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000062 void VisitValueDecl(ValueDecl *D);
63 void VisitEnumConstantDecl(EnumConstantDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000064 void VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +000065 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000066 void VisitFunctionDecl(FunctionDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000067 void VisitCXXMethodDecl(CXXMethodDecl *D);
68 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
69 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
70 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000071 void VisitFieldDecl(FieldDecl *D);
72 void VisitVarDecl(VarDecl *D);
73 void VisitImplicitParamDecl(ImplicitParamDecl *D);
74 void VisitParmVarDecl(ParmVarDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000075 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
76 void VisitTemplateDecl(TemplateDecl *D);
77 void VisitClassTemplateDecl(ClassTemplateDecl *D);
78 void visitFunctionTemplateDecl(FunctionTemplateDecl *D);
79 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
80 void VisitUsing(UsingDecl *D);
81 void VisitUsingShadow(UsingShadowDecl *D);
82 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000083 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000084 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
85 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000086 void VisitBlockDecl(BlockDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000087
Mike Stump1eb44332009-09-09 15:08:12 +000088 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000089 uint64_t VisibleOffset);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000090
91
Sean Hunt9a555912010-05-30 07:21:58 +000092 // FIXME: Put in the same order is DeclNodes.td?
Chris Lattner12b1c762009-04-27 06:16:06 +000093 void VisitObjCMethodDecl(ObjCMethodDecl *D);
94 void VisitObjCContainerDecl(ObjCContainerDecl *D);
95 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
96 void VisitObjCIvarDecl(ObjCIvarDecl *D);
97 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
98 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
99 void VisitObjCClassDecl(ObjCClassDecl *D);
100 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
101 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
102 void VisitObjCImplDecl(ObjCImplDecl *D);
103 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
104 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
105 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
106 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
107 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall5250f272010-06-03 19:28:45 +0000108
109 void WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base);
Chris Lattner12b1c762009-04-27 06:16:06 +0000110 };
111}
112
113void PCHDeclWriter::VisitDecl(Decl *D) {
114 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
115 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
116 Writer.AddSourceLocation(D->getLocation(), Record);
117 Record.push_back(D->isInvalidDecl());
118 Record.push_back(D->hasAttrs());
119 Record.push_back(D->isImplicit());
Douglas Gregore0762c92009-06-19 23:52:42 +0000120 Record.push_back(D->isUsed());
Chris Lattner12b1c762009-04-27 06:16:06 +0000121 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000122 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +0000123}
124
125void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
126 VisitDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000127 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000128 Code = pch::DECL_TRANSLATION_UNIT;
129}
130
131void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
132 VisitDecl(D);
133 Writer.AddDeclarationName(D->getDeclName(), Record);
134}
135
136void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
137 VisitNamedDecl(D);
138 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
139}
140
141void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
142 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000143 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000144 Code = pch::DECL_TYPEDEF;
145}
146
147void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
148 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000149 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000150 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
151 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000152 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000153 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000154 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000155 // FIXME: maybe write optional qualifier and its range.
156 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000157}
158
159void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
160 VisitTagDecl(D);
161 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000162 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall1b5a6182010-05-06 08:49:23 +0000163 Record.push_back(D->getNumPositiveBits());
164 Record.push_back(D->getNumNegativeBits());
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000165 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000166 Code = pch::DECL_ENUM;
167}
168
169void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
170 VisitTagDecl(D);
171 Record.push_back(D->hasFlexibleArrayMember());
172 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000173 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000174 Code = pch::DECL_RECORD;
175}
176
177void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
178 VisitNamedDecl(D);
179 Writer.AddTypeRef(D->getType(), Record);
180}
181
182void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
183 VisitValueDecl(D);
184 Record.push_back(D->getInitExpr()? 1 : 0);
185 if (D->getInitExpr())
186 Writer.AddStmt(D->getInitExpr());
187 Writer.AddAPSInt(D->getInitVal(), Record);
188 Code = pch::DECL_ENUM_CONSTANT;
189}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000190
191void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
192 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000193 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000194 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000195}
Chris Lattner12b1c762009-04-27 06:16:06 +0000196
197void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000198 VisitDeclaratorDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000199
Chris Lattner12b1c762009-04-27 06:16:06 +0000200 Record.push_back(D->isThisDeclarationADefinition());
201 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000202 Writer.AddStmt(D->getBody());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000203
Chris Lattner12b1c762009-04-27 06:16:06 +0000204 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
205 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000206 Record.push_back(D->getStorageClassAsWritten());
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000207 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000208 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000209 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000210 Record.push_back(D->hasInheritedPrototype());
211 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000212 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000213 Record.push_back(D->isTrivial());
214 Record.push_back(D->isCopyAssignment());
215 Record.push_back(D->hasImplicitReturnZero());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000216 // FIXME: C++ TemplateOrInstantiation???
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000217 Writer.AddSourceLocation(D->getLocEnd(), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000218
Chris Lattner12b1c762009-04-27 06:16:06 +0000219 Record.push_back(D->param_size());
220 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
221 P != PEnd; ++P)
222 Writer.AddDeclRef(*P, Record);
223 Code = pch::DECL_FUNCTION;
224}
225
226void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
227 VisitNamedDecl(D);
228 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000229 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000230 Record.push_back(D->getBody() != 0);
231 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000232 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000233 Writer.AddDeclRef(D->getSelfDecl(), Record);
234 Writer.AddDeclRef(D->getCmdDecl(), Record);
235 }
236 Record.push_back(D->isInstanceMethod());
237 Record.push_back(D->isVariadic());
238 Record.push_back(D->isSynthesized());
239 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000240 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000241 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000242 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000243 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000244 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000245 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000246 Writer.AddSourceLocation(D->getLocEnd(), Record);
247 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000248 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000249 PEnd = D->param_end(); P != PEnd; ++P)
250 Writer.AddDeclRef(*P, Record);
251 Code = pch::DECL_OBJC_METHOD;
252}
253
254void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
255 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000256 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000257 // Abstract class (no need to define a stable pch::DECL code).
258}
259
260void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
261 VisitObjCContainerDecl(D);
262 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
263 Writer.AddDeclRef(D->getSuperClass(), Record);
264 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000265 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000266 PEnd = D->protocol_end();
267 P != PEnd; ++P)
268 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000269 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
270 PLEnd = D->protocol_loc_end();
271 PL != PLEnd; ++PL)
272 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000273 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000274 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000275 IEnd = D->ivar_end(); I != IEnd; ++I)
276 Writer.AddDeclRef(*I, Record);
277 Writer.AddDeclRef(D->getCategoryList(), Record);
278 Record.push_back(D->isForwardDecl());
279 Record.push_back(D->isImplicitInterfaceDecl());
280 Writer.AddSourceLocation(D->getClassLoc(), Record);
281 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
282 Writer.AddSourceLocation(D->getLocEnd(), Record);
283 Code = pch::DECL_OBJC_INTERFACE;
284}
285
286void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
287 VisitFieldDecl(D);
288 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000289 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000290 Code = pch::DECL_OBJC_IVAR;
291}
292
293void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
294 VisitObjCContainerDecl(D);
295 Record.push_back(D->isForwardDecl());
296 Writer.AddSourceLocation(D->getLocEnd(), Record);
297 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000298 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000299 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
300 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000301 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
302 PLEnd = D->protocol_loc_end();
303 PL != PLEnd; ++PL)
304 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000305 Code = pch::DECL_OBJC_PROTOCOL;
306}
307
308void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
309 VisitFieldDecl(D);
310 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
311}
312
313void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
314 VisitDecl(D);
315 Record.push_back(D->size());
316 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000317 Writer.AddDeclRef(I->getInterface(), Record);
318 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
319 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000320 Code = pch::DECL_OBJC_CLASS;
321}
322
323void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
324 VisitDecl(D);
325 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000326 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000327 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
328 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000329 for (ObjCForwardProtocolDecl::protocol_loc_iterator
330 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
331 PL != PLEnd; ++PL)
332 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000333 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
334}
335
336void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
337 VisitObjCContainerDecl(D);
338 Writer.AddDeclRef(D->getClassInterface(), Record);
339 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000340 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000341 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
342 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000343 for (ObjCCategoryDecl::protocol_loc_iterator
344 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
345 PL != PLEnd; ++PL)
346 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000347 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000348 Writer.AddSourceLocation(D->getAtLoc(), Record);
349 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000350 Code = pch::DECL_OBJC_CATEGORY;
351}
352
353void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
354 VisitNamedDecl(D);
355 Writer.AddDeclRef(D->getClassInterface(), Record);
356 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
357}
358
359void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
360 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000361 Writer.AddSourceLocation(D->getAtLoc(), Record);
John McCall83a230c2010-06-04 20:50:08 +0000362 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000363 // FIXME: stable encoding
364 Record.push_back((unsigned)D->getPropertyAttributes());
365 // FIXME: stable encoding
366 Record.push_back((unsigned)D->getPropertyImplementation());
367 Writer.AddDeclarationName(D->getGetterName(), Record);
368 Writer.AddDeclarationName(D->getSetterName(), Record);
369 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
370 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
371 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
372 Code = pch::DECL_OBJC_PROPERTY;
373}
374
375void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000376 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000377 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000378 // Abstract class (no need to define a stable pch::DECL code).
379}
380
381void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
382 VisitObjCImplDecl(D);
383 Writer.AddIdentifierRef(D->getIdentifier(), Record);
384 Code = pch::DECL_OBJC_CATEGORY_IMPL;
385}
386
387void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
388 VisitObjCImplDecl(D);
389 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000390 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000391 Code = pch::DECL_OBJC_IMPLEMENTATION;
392}
393
394void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
395 VisitDecl(D);
396 Writer.AddSourceLocation(D->getLocStart(), Record);
397 Writer.AddDeclRef(D->getPropertyDecl(), Record);
398 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000399 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000400 Code = pch::DECL_OBJC_PROPERTY_IMPL;
401}
402
403void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000404 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000405 Record.push_back(D->isMutable());
406 Record.push_back(D->getBitWidth()? 1 : 0);
407 if (D->getBitWidth())
408 Writer.AddStmt(D->getBitWidth());
409 Code = pch::DECL_FIELD;
410}
411
412void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000413 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000414 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000415 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000416 Record.push_back(D->isThreadSpecified());
417 Record.push_back(D->hasCXXDirectInitializer());
418 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000419 Record.push_back(D->isExceptionVariable());
Douglas Gregor5077c382010-05-15 06:01:05 +0000420 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000421 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000422 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000423 if (D->getInit())
424 Writer.AddStmt(D->getInit());
425 Code = pch::DECL_VAR;
426}
427
428void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
429 VisitVarDecl(D);
430 Code = pch::DECL_IMPLICIT_PARAM;
431}
432
433void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
434 VisitVarDecl(D);
435 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000436 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000437 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattnerea5ce472009-04-27 07:35:58 +0000439 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
440 // we dynamically check for the properties that we optimize for, but don't
441 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000442 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000443 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000444 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000445 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000446 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000447 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000448 D->getStorageClass() == 0 &&
449 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000450 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000451 !D->hasInheritedDefaultArg() &&
452 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000453 AbbrevToUse = Writer.getParmVarDeclAbbrev();
454
455 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
456 // just us assuming it.
457 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
458 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
459 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
460 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000461 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000462 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000463}
464
Chris Lattner12b1c762009-04-27 06:16:06 +0000465void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
466 VisitDecl(D);
467 Writer.AddStmt(D->getAsmString());
468 Code = pch::DECL_FILE_SCOPE_ASM;
469}
470
471void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
472 VisitDecl(D);
473 Writer.AddStmt(D->getBody());
John McCall82dc0092010-06-04 11:21:44 +0000474 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000475 Record.push_back(D->param_size());
476 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
477 P != PEnd; ++P)
478 Writer.AddDeclRef(*P, Record);
479 Code = pch::DECL_BLOCK;
480}
481
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000482void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
483 VisitDecl(D);
484 // FIXME: It might be nice to serialize the brace locations for this
485 // declaration, which don't seem to be readily available in the AST.
486 Record.push_back(D->getLanguage());
487 Record.push_back(D->hasBraces());
488 Code = pch::DECL_LINKAGE_SPEC;
489}
490
491void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
492 VisitNamedDecl(D);
493 Writer.AddSourceLocation(D->getLBracLoc(), Record);
494 Writer.AddSourceLocation(D->getRBracLoc(), Record);
495 Writer.AddDeclRef(D->getNextNamespace(), Record);
496
497 // Only write one reference--original or anonymous
498 Record.push_back(D->isOriginalNamespace());
499 if (D->isOriginalNamespace())
500 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
501 else
502 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
503 Code = pch::DECL_NAMESPACE;
504}
505
506void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
507 VisitNamedDecl(D);
508 Writer.AddSourceLocation(D->getAliasLoc(), Record);
509 Writer.AddSourceRange(D->getQualifierRange(), Record);
510 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
511 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
512 Writer.AddDeclRef(D->getNamespace(), Record);
513 Code = pch::DECL_NAMESPACE_ALIAS;
514}
515
516void PCHDeclWriter::VisitUsing(UsingDecl *D) {
517 VisitNamedDecl(D);
518 Writer.AddSourceRange(D->getNestedNameRange(), Record);
519 Writer.AddSourceLocation(D->getUsingLocation(), Record);
520 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
521 Record.push_back(D->getNumShadowDecls());
522 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
523 PEnd = D->shadow_end(); P != PEnd; ++P)
524 Writer.AddDeclRef(*P, Record);
525 Record.push_back(D->isTypeName());
526 Code = pch::DECL_USING;
527}
528
529void PCHDeclWriter::VisitUsingShadow(UsingShadowDecl *D) {
530 VisitNamedDecl(D);
531 Writer.AddDeclRef(D->getTargetDecl(), Record);
532 Writer.AddDeclRef(D->getUsingDecl(), Record);
533 Code = pch::DECL_USING_SHADOW;
534}
535
536void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
537 VisitNamedDecl(D);
538 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
539 Writer.AddSourceRange(D->getQualifierRange(), Record);
540 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
541 Writer.AddSourceLocation(D->getIdentLocation(), Record);
542 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
543 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
544 Code = pch::DECL_USING_DIRECTIVE;
545}
546
547void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) {
548 VisitValueDecl(D);
549 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
550 Writer.AddSourceLocation(D->getUsingLoc(), Record);
551 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
552 Code = pch::DECL_UNRESOLVED_USING_VALUE;
553}
554
555void PCHDeclWriter::VisitUnresolvedUsingTypename(
556 UnresolvedUsingTypenameDecl *D) {
557 VisitTypeDecl(D);
558 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
559 Writer.AddSourceLocation(D->getUsingLoc(), Record);
560 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
561 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
562 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
563}
564
John McCall5250f272010-06-03 19:28:45 +0000565void PCHDeclWriter::WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base) {
566 Record.push_back(Base->isVirtual());
567 Record.push_back(Base->isBaseOfClass());
568 Record.push_back(Base->getAccessSpecifierAsWritten());
569 Writer.AddTypeRef(Base->getType(), Record);
570 Writer.AddSourceRange(Base->getSourceRange(), Record);
571}
572
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000573void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
574 // assert(false && "cannot write CXXRecordDecl");
575 VisitRecordDecl(D);
John McCall5250f272010-06-03 19:28:45 +0000576 if (D->isDefinition()) {
577 unsigned NumBases = D->getNumBases();
578 Record.push_back(NumBases);
579 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
580 E = D->bases_end(); I != E; ++I)
581 WriteCXXBaseSpecifier(&*I);
582 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000583 Code = pch::DECL_CXX_RECORD;
584}
585
586void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
587 // assert(false && "cannot write CXXMethodDecl");
588 VisitFunctionDecl(D);
589 Code = pch::DECL_CXX_METHOD;
590}
591
592void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
593 // assert(false && "cannot write CXXConstructorDecl");
594 VisitCXXMethodDecl(D);
595 Code = pch::DECL_CXX_CONSTRUCTOR;
596}
597
598void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
599 // assert(false && "cannot write CXXDestructorDecl");
600 VisitCXXMethodDecl(D);
601 Code = pch::DECL_CXX_DESTRUCTOR;
602}
603
604void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
605 // assert(false && "cannot write CXXConversionDecl");
606 VisitCXXMethodDecl(D);
607 Code = pch::DECL_CXX_CONVERSION;
608}
609
610void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
611 assert(false && "cannot write FriendTemplateDecl");
612}
613
614void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
615 assert(false && "cannot write TemplateDecl");
616}
617
618void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
619 assert(false && "cannot write ClassTemplateDecl");
620}
621
622void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
623 ClassTemplateSpecializationDecl *D) {
624 assert(false && "cannot write ClassTemplateSpecializationDecl");
625}
626
627void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
628 ClassTemplatePartialSpecializationDecl *D) {
629 assert(false && "cannot write ClassTemplatePartialSpecializationDecl");
630}
631
632void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) {
633 assert(false && "cannot write FunctionTemplateDecl");
634}
635
636void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
637 assert(false && "cannot write TemplateTypeParmDecl");
638}
639
640void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
641 assert(false && "cannot write NonTypeTemplateParmDecl");
642}
643
644void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
645 assert(false && "cannot write TemplateTemplateParmDecl");
646}
647
648void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
649 assert(false && "cannot write StaticAssertDecl");
650}
651
Chris Lattner12b1c762009-04-27 06:16:06 +0000652/// \brief Emit the DeclContext part of a declaration context decl.
653///
654/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
655/// block for this declaration context is stored. May be 0 to indicate
656/// that there are no declarations stored within this context.
657///
658/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
659/// block for this declaration context is stored. May be 0 to indicate
660/// that there are no declarations visible from this context. Note
661/// that this value will not be emitted for non-primary declaration
662/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000663void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000664 uint64_t VisibleOffset) {
665 Record.push_back(LexicalOffset);
666 Record.push_back(VisibleOffset);
667}
668
669
670//===----------------------------------------------------------------------===//
671// PCHWriter Implementation
672//===----------------------------------------------------------------------===//
673
Chris Lattnerea5ce472009-04-27 07:35:58 +0000674void PCHWriter::WriteDeclsBlockAbbrevs() {
675 using namespace llvm;
676 // Abbreviation for DECL_PARM_VAR.
677 BitCodeAbbrev *Abv = new BitCodeAbbrev();
678 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
679
680 // Decl
681 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
682 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
683 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
684 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
685 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
686 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000687 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000688 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000689 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000690
Chris Lattnerea5ce472009-04-27 07:35:58 +0000691 // NamedDecl
692 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
693 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
694 // ValueDecl
695 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000696 // DeclaratorDecl
697 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000698 // VarDecl
699 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000700 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000701 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
702 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
703 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000704 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +0000705 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000706 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000707 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
708 // ParmVarDecl
709 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000710 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Chris Lattnerea5ce472009-04-27 07:35:58 +0000712 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
713}
714
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000715/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
716/// consumers of the AST.
717///
718/// Such decls will always be deserialized from the PCH file, so we would like
719/// this to be as restrictive as possible. Currently the predicate is driven by
720/// code generation requirements, if other clients have a different notion of
721/// what is "required" then we may have to consider an alternate scheme where
722/// clients can iterate over the top-level decls and get information on them,
723/// without necessary deserializing them. We could explicitly require such
724/// clients to use a separate API call to "realize" the decl. This should be
725/// relatively painless since they would presumably only do it for top-level
726/// decls.
727//
728// FIXME: This predicate is essentially IRgen's predicate to determine whether a
729// declaration can be deferred. Merge them somehow.
730static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
731 // File scoped assembly must be seen.
732 if (isa<FileScopeAsmDecl>(D))
733 return true;
734
735 // Otherwise if this isn't a function or a file scoped variable it doesn't
736 // need to be seen.
737 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
738 if (!VD->isFileVarDecl())
739 return false;
740 } else if (!isa<FunctionDecl>(D))
741 return false;
742
743 // Aliases and used decls must be seen.
744 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
745 return true;
746
747 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
748 // Forward declarations don't need to be seen.
749 if (!FD->isThisDeclarationADefinition())
750 return false;
751
752 // Constructors and destructors must be seen.
753 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
754 return true;
755
756 // Otherwise, this is required unless it is static.
757 //
758 // FIXME: Inlines.
759 return FD->getStorageClass() != FunctionDecl::Static;
760 } else {
761 const VarDecl *VD = cast<VarDecl>(D);
762
763 // In C++, this doesn't need to be seen if it is marked "extern".
764 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
765 (VD->getStorageClass() == VarDecl::Extern ||
766 VD->isExternC()))
767 return false;
768
769 // In C, this doesn't need to be seen unless it is a definition.
770 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
771 return false;
772
773 // Otherwise, this is required unless it is static.
774 return VD->getStorageClass() != VarDecl::Static;
775 }
776}
777
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000778void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000779 RecordData Record;
780 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000781
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000782 // If this declaration is also a DeclContext, write blocks for the
783 // declarations that lexically stored inside its context and those
784 // declarations that are visible from its context. These blocks
785 // are written before the declaration itself so that we can put
786 // their offsets into the record for the declaration.
787 uint64_t LexicalOffset = 0;
788 uint64_t VisibleOffset = 0;
789 DeclContext *DC = dyn_cast<DeclContext>(D);
790 if (DC) {
791 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
792 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000793 }
794
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000795 // Determine the ID for this declaration
796 pch::DeclID &ID = DeclIDs[D];
797 if (ID == 0)
798 ID = DeclIDs.size();
799
800 unsigned Index = ID - 1;
801
802 // Record the offset for this declaration
803 if (DeclOffsets.size() == Index)
804 DeclOffsets.push_back(Stream.GetCurrentBitNo());
805 else if (DeclOffsets.size() < Index) {
806 DeclOffsets.resize(Index+1);
807 DeclOffsets[Index] = Stream.GetCurrentBitNo();
808 }
809
810 // Build and emit a record for this declaration
811 Record.clear();
812 W.Code = (pch::DeclCode)0;
813 W.AbbrevToUse = 0;
814 W.Visit(D);
815 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
816
Daniel Dunbar33671982009-12-03 09:13:36 +0000817 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000818 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000819 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000820 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
821
822 // If the declaration had any attributes, write them now.
823 if (D->hasAttrs())
824 WriteAttributeRecord(D->getAttrs());
825
826 // Flush any expressions that were written as part of this declaration.
827 FlushStmts();
828
829 // Note "external" declarations so that we can add them to a record in the
830 // PCH file later.
831 //
832 // FIXME: This should be renamed, the predicate is much more complicated.
833 if (isRequiredDecl(D, Context))
834 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000835}