blob: 28a10ad6b0a4542ff304207ffe2c5afcd002e475 [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
92 // FIXME: Put in the same order is DeclNodes.def?
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);
108 };
109}
110
111void PCHDeclWriter::VisitDecl(Decl *D) {
112 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
113 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
114 Writer.AddSourceLocation(D->getLocation(), Record);
115 Record.push_back(D->isInvalidDecl());
116 Record.push_back(D->hasAttrs());
117 Record.push_back(D->isImplicit());
Douglas Gregore0762c92009-06-19 23:52:42 +0000118 Record.push_back(D->isUsed());
Chris Lattner12b1c762009-04-27 06:16:06 +0000119 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000120 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +0000121}
122
123void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
124 VisitDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000125 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000126 Code = pch::DECL_TRANSLATION_UNIT;
127}
128
129void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
130 VisitDecl(D);
131 Writer.AddDeclarationName(D->getDeclName(), Record);
132}
133
134void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
135 VisitNamedDecl(D);
136 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
137}
138
139void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
140 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000141 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000142 Code = pch::DECL_TYPEDEF;
143}
144
145void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
146 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000147 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000148 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
149 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000150 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000151 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000152 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000153 // FIXME: maybe write optional qualifier and its range.
154 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000155}
156
157void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
158 VisitTagDecl(D);
159 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000160 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall1b5a6182010-05-06 08:49:23 +0000161 Record.push_back(D->getNumPositiveBits());
162 Record.push_back(D->getNumNegativeBits());
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000163 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000164 Code = pch::DECL_ENUM;
165}
166
167void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
168 VisitTagDecl(D);
169 Record.push_back(D->hasFlexibleArrayMember());
170 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000171 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000172 Code = pch::DECL_RECORD;
173}
174
175void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
176 VisitNamedDecl(D);
177 Writer.AddTypeRef(D->getType(), Record);
178}
179
180void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
181 VisitValueDecl(D);
182 Record.push_back(D->getInitExpr()? 1 : 0);
183 if (D->getInitExpr())
184 Writer.AddStmt(D->getInitExpr());
185 Writer.AddAPSInt(D->getInitVal(), Record);
186 Code = pch::DECL_ENUM_CONSTANT;
187}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000188
189void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
190 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000191 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000192 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000193}
Chris Lattner12b1c762009-04-27 06:16:06 +0000194
195void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000196 VisitDeclaratorDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000197
Chris Lattner12b1c762009-04-27 06:16:06 +0000198 Record.push_back(D->isThisDeclarationADefinition());
199 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000200 Writer.AddStmt(D->getBody());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000201
Chris Lattner12b1c762009-04-27 06:16:06 +0000202 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
203 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000204 Record.push_back(D->getStorageClassAsWritten());
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000205 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000206 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000207 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000208 Record.push_back(D->hasInheritedPrototype());
209 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000210 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000211 Record.push_back(D->isTrivial());
212 Record.push_back(D->isCopyAssignment());
213 Record.push_back(D->hasImplicitReturnZero());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000214 // FIXME: C++ TemplateOrInstantiation???
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000215 Writer.AddSourceLocation(D->getLocEnd(), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000216
Chris Lattner12b1c762009-04-27 06:16:06 +0000217 Record.push_back(D->param_size());
218 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
219 P != PEnd; ++P)
220 Writer.AddDeclRef(*P, Record);
221 Code = pch::DECL_FUNCTION;
222}
223
224void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
225 VisitNamedDecl(D);
226 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000227 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000228 Record.push_back(D->getBody() != 0);
229 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000230 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000231 Writer.AddDeclRef(D->getSelfDecl(), Record);
232 Writer.AddDeclRef(D->getCmdDecl(), Record);
233 }
234 Record.push_back(D->isInstanceMethod());
235 Record.push_back(D->isVariadic());
236 Record.push_back(D->isSynthesized());
237 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000238 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000239 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000240 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000241 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000242 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000243 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000244 Writer.AddSourceLocation(D->getLocEnd(), Record);
245 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000246 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000247 PEnd = D->param_end(); P != PEnd; ++P)
248 Writer.AddDeclRef(*P, Record);
249 Code = pch::DECL_OBJC_METHOD;
250}
251
252void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
253 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000254 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000255 // Abstract class (no need to define a stable pch::DECL code).
256}
257
258void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
259 VisitObjCContainerDecl(D);
260 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
261 Writer.AddDeclRef(D->getSuperClass(), Record);
262 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000263 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000264 PEnd = D->protocol_end();
265 P != PEnd; ++P)
266 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000267 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
268 PLEnd = D->protocol_loc_end();
269 PL != PLEnd; ++PL)
270 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000271 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000272 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000273 IEnd = D->ivar_end(); I != IEnd; ++I)
274 Writer.AddDeclRef(*I, Record);
275 Writer.AddDeclRef(D->getCategoryList(), Record);
276 Record.push_back(D->isForwardDecl());
277 Record.push_back(D->isImplicitInterfaceDecl());
278 Writer.AddSourceLocation(D->getClassLoc(), Record);
279 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
280 Writer.AddSourceLocation(D->getLocEnd(), Record);
281 Code = pch::DECL_OBJC_INTERFACE;
282}
283
284void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
285 VisitFieldDecl(D);
286 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000287 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000288 Code = pch::DECL_OBJC_IVAR;
289}
290
291void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
292 VisitObjCContainerDecl(D);
293 Record.push_back(D->isForwardDecl());
294 Writer.AddSourceLocation(D->getLocEnd(), Record);
295 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000296 for (ObjCProtocolDecl::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 (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
300 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_PROTOCOL;
304}
305
306void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
307 VisitFieldDecl(D);
308 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
309}
310
311void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
312 VisitDecl(D);
313 Record.push_back(D->size());
314 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000315 Writer.AddDeclRef(I->getInterface(), Record);
316 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
317 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000318 Code = pch::DECL_OBJC_CLASS;
319}
320
321void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
322 VisitDecl(D);
323 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000324 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000325 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
326 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000327 for (ObjCForwardProtocolDecl::protocol_loc_iterator
328 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
329 PL != PLEnd; ++PL)
330 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000331 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
332}
333
334void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
335 VisitObjCContainerDecl(D);
336 Writer.AddDeclRef(D->getClassInterface(), Record);
337 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000338 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000339 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
340 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000341 for (ObjCCategoryDecl::protocol_loc_iterator
342 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
343 PL != PLEnd; ++PL)
344 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000345 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000346 Writer.AddSourceLocation(D->getAtLoc(), Record);
347 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000348 Code = pch::DECL_OBJC_CATEGORY;
349}
350
351void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
352 VisitNamedDecl(D);
353 Writer.AddDeclRef(D->getClassInterface(), Record);
354 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
355}
356
357void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
358 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000359 Writer.AddSourceLocation(D->getAtLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000360 Writer.AddTypeRef(D->getType(), Record);
361 // FIXME: stable encoding
362 Record.push_back((unsigned)D->getPropertyAttributes());
363 // FIXME: stable encoding
364 Record.push_back((unsigned)D->getPropertyImplementation());
365 Writer.AddDeclarationName(D->getGetterName(), Record);
366 Writer.AddDeclarationName(D->getSetterName(), Record);
367 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
368 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
369 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
370 Code = pch::DECL_OBJC_PROPERTY;
371}
372
373void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000374 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000375 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000376 // Abstract class (no need to define a stable pch::DECL code).
377}
378
379void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
380 VisitObjCImplDecl(D);
381 Writer.AddIdentifierRef(D->getIdentifier(), Record);
382 Code = pch::DECL_OBJC_CATEGORY_IMPL;
383}
384
385void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
386 VisitObjCImplDecl(D);
387 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000388 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000389 Code = pch::DECL_OBJC_IMPLEMENTATION;
390}
391
392void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
393 VisitDecl(D);
394 Writer.AddSourceLocation(D->getLocStart(), Record);
395 Writer.AddDeclRef(D->getPropertyDecl(), Record);
396 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000397 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000398 Code = pch::DECL_OBJC_PROPERTY_IMPL;
399}
400
401void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000402 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000403 Record.push_back(D->isMutable());
404 Record.push_back(D->getBitWidth()? 1 : 0);
405 if (D->getBitWidth())
406 Writer.AddStmt(D->getBitWidth());
407 Code = pch::DECL_FIELD;
408}
409
410void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000411 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000412 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000413 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000414 Record.push_back(D->isThreadSpecified());
415 Record.push_back(D->hasCXXDirectInitializer());
416 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000417 Record.push_back(D->isExceptionVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000418 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000419 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000420 if (D->getInit())
421 Writer.AddStmt(D->getInit());
422 Code = pch::DECL_VAR;
423}
424
425void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
426 VisitVarDecl(D);
427 Code = pch::DECL_IMPLICIT_PARAM;
428}
429
430void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
431 VisitVarDecl(D);
432 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000433 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000434 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000435
Chris Lattnerea5ce472009-04-27 07:35:58 +0000436 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
437 // we dynamically check for the properties that we optimize for, but don't
438 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000439 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000440 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000441 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000442 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000443 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000444 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000445 D->getStorageClass() == 0 &&
446 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000447 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000448 !D->hasInheritedDefaultArg() &&
449 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000450 AbbrevToUse = Writer.getParmVarDeclAbbrev();
451
452 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
453 // just us assuming it.
454 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
455 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
456 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
457 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000458 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000459 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000460}
461
Chris Lattner12b1c762009-04-27 06:16:06 +0000462void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
463 VisitDecl(D);
464 Writer.AddStmt(D->getAsmString());
465 Code = pch::DECL_FILE_SCOPE_ASM;
466}
467
468void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
469 VisitDecl(D);
470 Writer.AddStmt(D->getBody());
471 Record.push_back(D->param_size());
472 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
473 P != PEnd; ++P)
474 Writer.AddDeclRef(*P, Record);
475 Code = pch::DECL_BLOCK;
476}
477
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000478void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
479 VisitDecl(D);
480 // FIXME: It might be nice to serialize the brace locations for this
481 // declaration, which don't seem to be readily available in the AST.
482 Record.push_back(D->getLanguage());
483 Record.push_back(D->hasBraces());
484 Code = pch::DECL_LINKAGE_SPEC;
485}
486
487void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
488 VisitNamedDecl(D);
489 Writer.AddSourceLocation(D->getLBracLoc(), Record);
490 Writer.AddSourceLocation(D->getRBracLoc(), Record);
491 Writer.AddDeclRef(D->getNextNamespace(), Record);
492
493 // Only write one reference--original or anonymous
494 Record.push_back(D->isOriginalNamespace());
495 if (D->isOriginalNamespace())
496 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
497 else
498 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
499 Code = pch::DECL_NAMESPACE;
500}
501
502void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
503 VisitNamedDecl(D);
504 Writer.AddSourceLocation(D->getAliasLoc(), Record);
505 Writer.AddSourceRange(D->getQualifierRange(), Record);
506 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
507 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
508 Writer.AddDeclRef(D->getNamespace(), Record);
509 Code = pch::DECL_NAMESPACE_ALIAS;
510}
511
512void PCHDeclWriter::VisitUsing(UsingDecl *D) {
513 VisitNamedDecl(D);
514 Writer.AddSourceRange(D->getNestedNameRange(), Record);
515 Writer.AddSourceLocation(D->getUsingLocation(), Record);
516 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
517 Record.push_back(D->getNumShadowDecls());
518 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
519 PEnd = D->shadow_end(); P != PEnd; ++P)
520 Writer.AddDeclRef(*P, Record);
521 Record.push_back(D->isTypeName());
522 Code = pch::DECL_USING;
523}
524
525void PCHDeclWriter::VisitUsingShadow(UsingShadowDecl *D) {
526 VisitNamedDecl(D);
527 Writer.AddDeclRef(D->getTargetDecl(), Record);
528 Writer.AddDeclRef(D->getUsingDecl(), Record);
529 Code = pch::DECL_USING_SHADOW;
530}
531
532void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
533 VisitNamedDecl(D);
534 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
535 Writer.AddSourceRange(D->getQualifierRange(), Record);
536 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
537 Writer.AddSourceLocation(D->getIdentLocation(), Record);
538 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
539 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
540 Code = pch::DECL_USING_DIRECTIVE;
541}
542
543void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) {
544 VisitValueDecl(D);
545 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
546 Writer.AddSourceLocation(D->getUsingLoc(), Record);
547 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
548 Code = pch::DECL_UNRESOLVED_USING_VALUE;
549}
550
551void PCHDeclWriter::VisitUnresolvedUsingTypename(
552 UnresolvedUsingTypenameDecl *D) {
553 VisitTypeDecl(D);
554 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
555 Writer.AddSourceLocation(D->getUsingLoc(), Record);
556 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
557 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
558 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
559}
560
561void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
562 // assert(false && "cannot write CXXRecordDecl");
563 VisitRecordDecl(D);
564 Code = pch::DECL_CXX_RECORD;
565}
566
567void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
568 // assert(false && "cannot write CXXMethodDecl");
569 VisitFunctionDecl(D);
570 Code = pch::DECL_CXX_METHOD;
571}
572
573void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
574 // assert(false && "cannot write CXXConstructorDecl");
575 VisitCXXMethodDecl(D);
576 Code = pch::DECL_CXX_CONSTRUCTOR;
577}
578
579void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
580 // assert(false && "cannot write CXXDestructorDecl");
581 VisitCXXMethodDecl(D);
582 Code = pch::DECL_CXX_DESTRUCTOR;
583}
584
585void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
586 // assert(false && "cannot write CXXConversionDecl");
587 VisitCXXMethodDecl(D);
588 Code = pch::DECL_CXX_CONVERSION;
589}
590
591void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
592 assert(false && "cannot write FriendTemplateDecl");
593}
594
595void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
596 assert(false && "cannot write TemplateDecl");
597}
598
599void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
600 assert(false && "cannot write ClassTemplateDecl");
601}
602
603void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
604 ClassTemplateSpecializationDecl *D) {
605 assert(false && "cannot write ClassTemplateSpecializationDecl");
606}
607
608void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
609 ClassTemplatePartialSpecializationDecl *D) {
610 assert(false && "cannot write ClassTemplatePartialSpecializationDecl");
611}
612
613void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) {
614 assert(false && "cannot write FunctionTemplateDecl");
615}
616
617void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
618 assert(false && "cannot write TemplateTypeParmDecl");
619}
620
621void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
622 assert(false && "cannot write NonTypeTemplateParmDecl");
623}
624
625void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
626 assert(false && "cannot write TemplateTemplateParmDecl");
627}
628
629void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
630 assert(false && "cannot write StaticAssertDecl");
631}
632
Chris Lattner12b1c762009-04-27 06:16:06 +0000633/// \brief Emit the DeclContext part of a declaration context decl.
634///
635/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
636/// block for this declaration context is stored. May be 0 to indicate
637/// that there are no declarations stored within this context.
638///
639/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
640/// block for this declaration context is stored. May be 0 to indicate
641/// that there are no declarations visible from this context. Note
642/// that this value will not be emitted for non-primary declaration
643/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000644void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000645 uint64_t VisibleOffset) {
646 Record.push_back(LexicalOffset);
647 Record.push_back(VisibleOffset);
648}
649
650
651//===----------------------------------------------------------------------===//
652// PCHWriter Implementation
653//===----------------------------------------------------------------------===//
654
Chris Lattnerea5ce472009-04-27 07:35:58 +0000655void PCHWriter::WriteDeclsBlockAbbrevs() {
656 using namespace llvm;
657 // Abbreviation for DECL_PARM_VAR.
658 BitCodeAbbrev *Abv = new BitCodeAbbrev();
659 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
660
661 // Decl
662 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
663 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
664 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
665 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
666 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
667 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000668 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000669 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000670 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattnerea5ce472009-04-27 07:35:58 +0000672 // NamedDecl
673 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
674 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
675 // ValueDecl
676 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000677 // DeclaratorDecl
678 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000679 // VarDecl
680 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000681 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000682 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
683 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
684 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000685 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000686 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000687 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
688 // ParmVarDecl
689 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000690 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000691
Chris Lattnerea5ce472009-04-27 07:35:58 +0000692 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
693}
694
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000695/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
696/// consumers of the AST.
697///
698/// Such decls will always be deserialized from the PCH file, so we would like
699/// this to be as restrictive as possible. Currently the predicate is driven by
700/// code generation requirements, if other clients have a different notion of
701/// what is "required" then we may have to consider an alternate scheme where
702/// clients can iterate over the top-level decls and get information on them,
703/// without necessary deserializing them. We could explicitly require such
704/// clients to use a separate API call to "realize" the decl. This should be
705/// relatively painless since they would presumably only do it for top-level
706/// decls.
707//
708// FIXME: This predicate is essentially IRgen's predicate to determine whether a
709// declaration can be deferred. Merge them somehow.
710static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
711 // File scoped assembly must be seen.
712 if (isa<FileScopeAsmDecl>(D))
713 return true;
714
715 // Otherwise if this isn't a function or a file scoped variable it doesn't
716 // need to be seen.
717 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
718 if (!VD->isFileVarDecl())
719 return false;
720 } else if (!isa<FunctionDecl>(D))
721 return false;
722
723 // Aliases and used decls must be seen.
724 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
725 return true;
726
727 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
728 // Forward declarations don't need to be seen.
729 if (!FD->isThisDeclarationADefinition())
730 return false;
731
732 // Constructors and destructors must be seen.
733 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
734 return true;
735
736 // Otherwise, this is required unless it is static.
737 //
738 // FIXME: Inlines.
739 return FD->getStorageClass() != FunctionDecl::Static;
740 } else {
741 const VarDecl *VD = cast<VarDecl>(D);
742
743 // In C++, this doesn't need to be seen if it is marked "extern".
744 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
745 (VD->getStorageClass() == VarDecl::Extern ||
746 VD->isExternC()))
747 return false;
748
749 // In C, this doesn't need to be seen unless it is a definition.
750 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
751 return false;
752
753 // Otherwise, this is required unless it is static.
754 return VD->getStorageClass() != VarDecl::Static;
755 }
756}
757
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000758void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000759 RecordData Record;
760 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000761
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000762 // If this declaration is also a DeclContext, write blocks for the
763 // declarations that lexically stored inside its context and those
764 // declarations that are visible from its context. These blocks
765 // are written before the declaration itself so that we can put
766 // their offsets into the record for the declaration.
767 uint64_t LexicalOffset = 0;
768 uint64_t VisibleOffset = 0;
769 DeclContext *DC = dyn_cast<DeclContext>(D);
770 if (DC) {
771 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
772 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000773 }
774
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000775 // Determine the ID for this declaration
776 pch::DeclID &ID = DeclIDs[D];
777 if (ID == 0)
778 ID = DeclIDs.size();
779
780 unsigned Index = ID - 1;
781
782 // Record the offset for this declaration
783 if (DeclOffsets.size() == Index)
784 DeclOffsets.push_back(Stream.GetCurrentBitNo());
785 else if (DeclOffsets.size() < Index) {
786 DeclOffsets.resize(Index+1);
787 DeclOffsets[Index] = Stream.GetCurrentBitNo();
788 }
789
790 // Build and emit a record for this declaration
791 Record.clear();
792 W.Code = (pch::DeclCode)0;
793 W.AbbrevToUse = 0;
794 W.Visit(D);
795 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
796
Daniel Dunbar33671982009-12-03 09:13:36 +0000797 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000798 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000799 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000800 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
801
802 // If the declaration had any attributes, write them now.
803 if (D->hasAttrs())
804 WriteAttributeRecord(D->getAttrs());
805
806 // Flush any expressions that were written as part of this declaration.
807 FlushStmts();
808
809 // Note "external" declarations so that we can add them to a record in the
810 // PCH file later.
811 //
812 // FIXME: This should be renamed, the predicate is much more complicated.
813 if (isRequiredDecl(D, Context))
814 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000815}