blob: 42756c35cc33a2e53afb62e89b64f2f054444ca5 [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);
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());
Douglas Gregor5077c382010-05-15 06:01:05 +0000418 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000419 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000420 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000421 if (D->getInit())
422 Writer.AddStmt(D->getInit());
423 Code = pch::DECL_VAR;
424}
425
426void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
427 VisitVarDecl(D);
428 Code = pch::DECL_IMPLICIT_PARAM;
429}
430
431void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
432 VisitVarDecl(D);
433 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000434 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000435 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattnerea5ce472009-04-27 07:35:58 +0000437 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
438 // we dynamically check for the properties that we optimize for, but don't
439 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000440 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000441 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000442 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000443 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000444 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000445 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000446 D->getStorageClass() == 0 &&
447 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000448 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000449 !D->hasInheritedDefaultArg() &&
450 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000451 AbbrevToUse = Writer.getParmVarDeclAbbrev();
452
453 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
454 // just us assuming it.
455 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
456 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
457 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
458 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000459 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000460 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000461}
462
Chris Lattner12b1c762009-04-27 06:16:06 +0000463void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
464 VisitDecl(D);
465 Writer.AddStmt(D->getAsmString());
466 Code = pch::DECL_FILE_SCOPE_ASM;
467}
468
469void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
470 VisitDecl(D);
471 Writer.AddStmt(D->getBody());
472 Record.push_back(D->param_size());
473 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
474 P != PEnd; ++P)
475 Writer.AddDeclRef(*P, Record);
476 Code = pch::DECL_BLOCK;
477}
478
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000479void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
480 VisitDecl(D);
481 // FIXME: It might be nice to serialize the brace locations for this
482 // declaration, which don't seem to be readily available in the AST.
483 Record.push_back(D->getLanguage());
484 Record.push_back(D->hasBraces());
485 Code = pch::DECL_LINKAGE_SPEC;
486}
487
488void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
489 VisitNamedDecl(D);
490 Writer.AddSourceLocation(D->getLBracLoc(), Record);
491 Writer.AddSourceLocation(D->getRBracLoc(), Record);
492 Writer.AddDeclRef(D->getNextNamespace(), Record);
493
494 // Only write one reference--original or anonymous
495 Record.push_back(D->isOriginalNamespace());
496 if (D->isOriginalNamespace())
497 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
498 else
499 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
500 Code = pch::DECL_NAMESPACE;
501}
502
503void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
504 VisitNamedDecl(D);
505 Writer.AddSourceLocation(D->getAliasLoc(), Record);
506 Writer.AddSourceRange(D->getQualifierRange(), Record);
507 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
508 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
509 Writer.AddDeclRef(D->getNamespace(), Record);
510 Code = pch::DECL_NAMESPACE_ALIAS;
511}
512
513void PCHDeclWriter::VisitUsing(UsingDecl *D) {
514 VisitNamedDecl(D);
515 Writer.AddSourceRange(D->getNestedNameRange(), Record);
516 Writer.AddSourceLocation(D->getUsingLocation(), Record);
517 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
518 Record.push_back(D->getNumShadowDecls());
519 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
520 PEnd = D->shadow_end(); P != PEnd; ++P)
521 Writer.AddDeclRef(*P, Record);
522 Record.push_back(D->isTypeName());
523 Code = pch::DECL_USING;
524}
525
526void PCHDeclWriter::VisitUsingShadow(UsingShadowDecl *D) {
527 VisitNamedDecl(D);
528 Writer.AddDeclRef(D->getTargetDecl(), Record);
529 Writer.AddDeclRef(D->getUsingDecl(), Record);
530 Code = pch::DECL_USING_SHADOW;
531}
532
533void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
534 VisitNamedDecl(D);
535 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
536 Writer.AddSourceRange(D->getQualifierRange(), Record);
537 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
538 Writer.AddSourceLocation(D->getIdentLocation(), Record);
539 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
540 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
541 Code = pch::DECL_USING_DIRECTIVE;
542}
543
544void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) {
545 VisitValueDecl(D);
546 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
547 Writer.AddSourceLocation(D->getUsingLoc(), Record);
548 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
549 Code = pch::DECL_UNRESOLVED_USING_VALUE;
550}
551
552void PCHDeclWriter::VisitUnresolvedUsingTypename(
553 UnresolvedUsingTypenameDecl *D) {
554 VisitTypeDecl(D);
555 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
556 Writer.AddSourceLocation(D->getUsingLoc(), Record);
557 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
558 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
559 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
560}
561
562void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
563 // assert(false && "cannot write CXXRecordDecl");
564 VisitRecordDecl(D);
565 Code = pch::DECL_CXX_RECORD;
566}
567
568void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
569 // assert(false && "cannot write CXXMethodDecl");
570 VisitFunctionDecl(D);
571 Code = pch::DECL_CXX_METHOD;
572}
573
574void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
575 // assert(false && "cannot write CXXConstructorDecl");
576 VisitCXXMethodDecl(D);
577 Code = pch::DECL_CXX_CONSTRUCTOR;
578}
579
580void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
581 // assert(false && "cannot write CXXDestructorDecl");
582 VisitCXXMethodDecl(D);
583 Code = pch::DECL_CXX_DESTRUCTOR;
584}
585
586void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
587 // assert(false && "cannot write CXXConversionDecl");
588 VisitCXXMethodDecl(D);
589 Code = pch::DECL_CXX_CONVERSION;
590}
591
592void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
593 assert(false && "cannot write FriendTemplateDecl");
594}
595
596void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
597 assert(false && "cannot write TemplateDecl");
598}
599
600void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
601 assert(false && "cannot write ClassTemplateDecl");
602}
603
604void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
605 ClassTemplateSpecializationDecl *D) {
606 assert(false && "cannot write ClassTemplateSpecializationDecl");
607}
608
609void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
610 ClassTemplatePartialSpecializationDecl *D) {
611 assert(false && "cannot write ClassTemplatePartialSpecializationDecl");
612}
613
614void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) {
615 assert(false && "cannot write FunctionTemplateDecl");
616}
617
618void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
619 assert(false && "cannot write TemplateTypeParmDecl");
620}
621
622void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
623 assert(false && "cannot write NonTypeTemplateParmDecl");
624}
625
626void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
627 assert(false && "cannot write TemplateTemplateParmDecl");
628}
629
630void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
631 assert(false && "cannot write StaticAssertDecl");
632}
633
Chris Lattner12b1c762009-04-27 06:16:06 +0000634/// \brief Emit the DeclContext part of a declaration context decl.
635///
636/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
637/// block for this declaration context is stored. May be 0 to indicate
638/// that there are no declarations stored within this context.
639///
640/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
641/// block for this declaration context is stored. May be 0 to indicate
642/// that there are no declarations visible from this context. Note
643/// that this value will not be emitted for non-primary declaration
644/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000645void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000646 uint64_t VisibleOffset) {
647 Record.push_back(LexicalOffset);
648 Record.push_back(VisibleOffset);
649}
650
651
652//===----------------------------------------------------------------------===//
653// PCHWriter Implementation
654//===----------------------------------------------------------------------===//
655
Chris Lattnerea5ce472009-04-27 07:35:58 +0000656void PCHWriter::WriteDeclsBlockAbbrevs() {
657 using namespace llvm;
658 // Abbreviation for DECL_PARM_VAR.
659 BitCodeAbbrev *Abv = new BitCodeAbbrev();
660 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
661
662 // Decl
663 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
664 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
665 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
666 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
667 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
668 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000669 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000670 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000671 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000672
Chris Lattnerea5ce472009-04-27 07:35:58 +0000673 // NamedDecl
674 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
675 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
676 // ValueDecl
677 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000678 // DeclaratorDecl
679 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000680 // VarDecl
681 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000682 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000683 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
684 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
685 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000686 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +0000687 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000688 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000689 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
690 // ParmVarDecl
691 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000692 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000693
Chris Lattnerea5ce472009-04-27 07:35:58 +0000694 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
695}
696
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000697/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
698/// consumers of the AST.
699///
700/// Such decls will always be deserialized from the PCH file, so we would like
701/// this to be as restrictive as possible. Currently the predicate is driven by
702/// code generation requirements, if other clients have a different notion of
703/// what is "required" then we may have to consider an alternate scheme where
704/// clients can iterate over the top-level decls and get information on them,
705/// without necessary deserializing them. We could explicitly require such
706/// clients to use a separate API call to "realize" the decl. This should be
707/// relatively painless since they would presumably only do it for top-level
708/// decls.
709//
710// FIXME: This predicate is essentially IRgen's predicate to determine whether a
711// declaration can be deferred. Merge them somehow.
712static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
713 // File scoped assembly must be seen.
714 if (isa<FileScopeAsmDecl>(D))
715 return true;
716
717 // Otherwise if this isn't a function or a file scoped variable it doesn't
718 // need to be seen.
719 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
720 if (!VD->isFileVarDecl())
721 return false;
722 } else if (!isa<FunctionDecl>(D))
723 return false;
724
725 // Aliases and used decls must be seen.
726 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
727 return true;
728
729 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
730 // Forward declarations don't need to be seen.
731 if (!FD->isThisDeclarationADefinition())
732 return false;
733
734 // Constructors and destructors must be seen.
735 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
736 return true;
737
738 // Otherwise, this is required unless it is static.
739 //
740 // FIXME: Inlines.
741 return FD->getStorageClass() != FunctionDecl::Static;
742 } else {
743 const VarDecl *VD = cast<VarDecl>(D);
744
745 // In C++, this doesn't need to be seen if it is marked "extern".
746 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
747 (VD->getStorageClass() == VarDecl::Extern ||
748 VD->isExternC()))
749 return false;
750
751 // In C, this doesn't need to be seen unless it is a definition.
752 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
753 return false;
754
755 // Otherwise, this is required unless it is static.
756 return VD->getStorageClass() != VarDecl::Static;
757 }
758}
759
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000760void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000761 RecordData Record;
762 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000763
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000764 // If this declaration is also a DeclContext, write blocks for the
765 // declarations that lexically stored inside its context and those
766 // declarations that are visible from its context. These blocks
767 // are written before the declaration itself so that we can put
768 // their offsets into the record for the declaration.
769 uint64_t LexicalOffset = 0;
770 uint64_t VisibleOffset = 0;
771 DeclContext *DC = dyn_cast<DeclContext>(D);
772 if (DC) {
773 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
774 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000775 }
776
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000777 // Determine the ID for this declaration
778 pch::DeclID &ID = DeclIDs[D];
779 if (ID == 0)
780 ID = DeclIDs.size();
781
782 unsigned Index = ID - 1;
783
784 // Record the offset for this declaration
785 if (DeclOffsets.size() == Index)
786 DeclOffsets.push_back(Stream.GetCurrentBitNo());
787 else if (DeclOffsets.size() < Index) {
788 DeclOffsets.resize(Index+1);
789 DeclOffsets[Index] = Stream.GetCurrentBitNo();
790 }
791
792 // Build and emit a record for this declaration
793 Record.clear();
794 W.Code = (pch::DeclCode)0;
795 W.AbbrevToUse = 0;
796 W.Visit(D);
797 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
798
Daniel Dunbar33671982009-12-03 09:13:36 +0000799 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000800 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000801 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000802 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
803
804 // If the declaration had any attributes, write them now.
805 if (D->hasAttrs())
806 WriteAttributeRecord(D->getAttrs());
807
808 // Flush any expressions that were written as part of this declaration.
809 FlushStmts();
810
811 // Note "external" declarations so that we can add them to a record in the
812 // PCH file later.
813 //
814 // FIXME: This should be renamed, the predicate is much more complicated.
815 if (isRequiredDecl(D, Context))
816 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000817}