blob: 4ea764d91920a8c2010af720e9236d51883fa274 [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);
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +000080 void VisitUsingDecl(UsingDecl *D);
81 void VisitUsingShadowDecl(UsingShadowDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000082 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000083 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Abramo Bagnara6206d532010-06-05 05:09:32 +000084 void VisitAccessSpecDecl(AccessSpecDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000085 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
86 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000087 void VisitBlockDecl(BlockDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000088
Mike Stump1eb44332009-09-09 15:08:12 +000089 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000090 uint64_t VisibleOffset);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000091
92
Sean Hunt9a555912010-05-30 07:21:58 +000093 // FIXME: Put in the same order is DeclNodes.td?
Chris Lattner12b1c762009-04-27 06:16:06 +000094 void VisitObjCMethodDecl(ObjCMethodDecl *D);
95 void VisitObjCContainerDecl(ObjCContainerDecl *D);
96 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
97 void VisitObjCIvarDecl(ObjCIvarDecl *D);
98 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
99 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
100 void VisitObjCClassDecl(ObjCClassDecl *D);
101 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
102 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
103 void VisitObjCImplDecl(ObjCImplDecl *D);
104 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
105 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
106 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
107 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
108 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall5250f272010-06-03 19:28:45 +0000109
110 void WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base);
Chris Lattner12b1c762009-04-27 06:16:06 +0000111 };
112}
113
114void PCHDeclWriter::VisitDecl(Decl *D) {
115 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
116 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
117 Writer.AddSourceLocation(D->getLocation(), Record);
118 Record.push_back(D->isInvalidDecl());
119 Record.push_back(D->hasAttrs());
120 Record.push_back(D->isImplicit());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000121 Record.push_back(D->isUsed(false));
Chris Lattner12b1c762009-04-27 06:16:06 +0000122 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000123 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +0000124}
125
126void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
127 VisitDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000128 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000129 Code = pch::DECL_TRANSLATION_UNIT;
130}
131
132void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
133 VisitDecl(D);
134 Writer.AddDeclarationName(D->getDeclName(), Record);
135}
136
137void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
138 VisitNamedDecl(D);
Argyrios Kyrtzidis28d16d72010-06-19 19:29:21 +0000139 if (isa<CXXRecordDecl>(D)) {
140 // FIXME: Hack. To read a templated CXXRecordDecl from PCH, we need an
141 // initialized CXXRecordDecl before creating an InjectedClassNameType.
142 // Delay emitting/reading CXXRecordDecl's TypeForDecl until when we handle
143 // CXXRecordDecl emitting/initialization.
144 Writer.AddTypeRef(QualType(), Record);
145 } else
146 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000147}
148
149void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
150 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000151 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000152 Code = pch::DECL_TYPEDEF;
153}
154
155void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
156 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000157 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000158 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
159 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000160 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000161 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000162 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000163 // FIXME: maybe write optional qualifier and its range.
164 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000165}
166
167void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
168 VisitTagDecl(D);
169 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000170 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall1b5a6182010-05-06 08:49:23 +0000171 Record.push_back(D->getNumPositiveBits());
172 Record.push_back(D->getNumNegativeBits());
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000173 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000174 Code = pch::DECL_ENUM;
175}
176
177void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
178 VisitTagDecl(D);
179 Record.push_back(D->hasFlexibleArrayMember());
180 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000181 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000182 Code = pch::DECL_RECORD;
183}
184
185void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
186 VisitNamedDecl(D);
187 Writer.AddTypeRef(D->getType(), Record);
188}
189
190void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
191 VisitValueDecl(D);
192 Record.push_back(D->getInitExpr()? 1 : 0);
193 if (D->getInitExpr())
194 Writer.AddStmt(D->getInitExpr());
195 Writer.AddAPSInt(D->getInitVal(), Record);
196 Code = pch::DECL_ENUM_CONSTANT;
197}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000198
199void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
200 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000201 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000202 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000203}
Chris Lattner12b1c762009-04-27 06:16:06 +0000204
205void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000206 VisitDeclaratorDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000207
Chris Lattner12b1c762009-04-27 06:16:06 +0000208 Record.push_back(D->isThisDeclarationADefinition());
209 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000210 Writer.AddStmt(D->getBody());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000211
Chris Lattner12b1c762009-04-27 06:16:06 +0000212 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
213 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000214 Record.push_back(D->getStorageClassAsWritten());
Douglas Gregor0130f3c2009-10-27 21:01:01 +0000215 Record.push_back(D->isInlineSpecified());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000216 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000217 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000218 Record.push_back(D->hasInheritedPrototype());
219 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000220 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000221 Record.push_back(D->isTrivial());
222 Record.push_back(D->isCopyAssignment());
223 Record.push_back(D->hasImplicitReturnZero());
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000224 // FIXME: C++ TemplateOrInstantiation???
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000225 Writer.AddSourceLocation(D->getLocEnd(), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000226
Chris Lattner12b1c762009-04-27 06:16:06 +0000227 Record.push_back(D->param_size());
228 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
229 P != PEnd; ++P)
230 Writer.AddDeclRef(*P, Record);
231 Code = pch::DECL_FUNCTION;
232}
233
234void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
235 VisitNamedDecl(D);
236 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000237 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000238 Record.push_back(D->getBody() != 0);
239 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000240 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000241 Writer.AddDeclRef(D->getSelfDecl(), Record);
242 Writer.AddDeclRef(D->getCmdDecl(), Record);
243 }
244 Record.push_back(D->isInstanceMethod());
245 Record.push_back(D->isVariadic());
246 Record.push_back(D->isSynthesized());
247 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000248 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000249 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000250 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000251 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000252 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000253 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000254 Writer.AddSourceLocation(D->getLocEnd(), Record);
255 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000256 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000257 PEnd = D->param_end(); P != PEnd; ++P)
258 Writer.AddDeclRef(*P, Record);
259 Code = pch::DECL_OBJC_METHOD;
260}
261
262void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
263 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000264 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000265 // Abstract class (no need to define a stable pch::DECL code).
266}
267
268void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
269 VisitObjCContainerDecl(D);
270 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
271 Writer.AddDeclRef(D->getSuperClass(), Record);
272 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000273 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000274 PEnd = D->protocol_end();
275 P != PEnd; ++P)
276 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000277 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
278 PLEnd = D->protocol_loc_end();
279 PL != PLEnd; ++PL)
280 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000281 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000282 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000283 IEnd = D->ivar_end(); I != IEnd; ++I)
284 Writer.AddDeclRef(*I, Record);
285 Writer.AddDeclRef(D->getCategoryList(), Record);
286 Record.push_back(D->isForwardDecl());
287 Record.push_back(D->isImplicitInterfaceDecl());
288 Writer.AddSourceLocation(D->getClassLoc(), Record);
289 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
290 Writer.AddSourceLocation(D->getLocEnd(), Record);
291 Code = pch::DECL_OBJC_INTERFACE;
292}
293
294void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
295 VisitFieldDecl(D);
296 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000297 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000298 Code = pch::DECL_OBJC_IVAR;
299}
300
301void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
302 VisitObjCContainerDecl(D);
303 Record.push_back(D->isForwardDecl());
304 Writer.AddSourceLocation(D->getLocEnd(), Record);
305 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000306 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000307 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
308 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000309 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
310 PLEnd = D->protocol_loc_end();
311 PL != PLEnd; ++PL)
312 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000313 Code = pch::DECL_OBJC_PROTOCOL;
314}
315
316void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
317 VisitFieldDecl(D);
318 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
319}
320
321void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
322 VisitDecl(D);
323 Record.push_back(D->size());
324 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000325 Writer.AddDeclRef(I->getInterface(), Record);
326 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
327 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000328 Code = pch::DECL_OBJC_CLASS;
329}
330
331void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
332 VisitDecl(D);
333 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000334 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000335 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
336 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000337 for (ObjCForwardProtocolDecl::protocol_loc_iterator
338 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
339 PL != PLEnd; ++PL)
340 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000341 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
342}
343
344void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
345 VisitObjCContainerDecl(D);
346 Writer.AddDeclRef(D->getClassInterface(), Record);
347 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000348 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000349 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
350 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000351 for (ObjCCategoryDecl::protocol_loc_iterator
352 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
353 PL != PLEnd; ++PL)
354 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000355 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000356 Writer.AddSourceLocation(D->getAtLoc(), Record);
357 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000358 Code = pch::DECL_OBJC_CATEGORY;
359}
360
361void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
362 VisitNamedDecl(D);
363 Writer.AddDeclRef(D->getClassInterface(), Record);
364 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
365}
366
367void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
368 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000369 Writer.AddSourceLocation(D->getAtLoc(), Record);
John McCall83a230c2010-06-04 20:50:08 +0000370 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000371 // FIXME: stable encoding
372 Record.push_back((unsigned)D->getPropertyAttributes());
373 // FIXME: stable encoding
374 Record.push_back((unsigned)D->getPropertyImplementation());
375 Writer.AddDeclarationName(D->getGetterName(), Record);
376 Writer.AddDeclarationName(D->getSetterName(), Record);
377 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
378 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
379 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
380 Code = pch::DECL_OBJC_PROPERTY;
381}
382
383void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000384 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000385 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000386 // Abstract class (no need to define a stable pch::DECL code).
387}
388
389void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
390 VisitObjCImplDecl(D);
391 Writer.AddIdentifierRef(D->getIdentifier(), Record);
392 Code = pch::DECL_OBJC_CATEGORY_IMPL;
393}
394
395void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
396 VisitObjCImplDecl(D);
397 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000398 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000399 Code = pch::DECL_OBJC_IMPLEMENTATION;
400}
401
402void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
403 VisitDecl(D);
404 Writer.AddSourceLocation(D->getLocStart(), Record);
405 Writer.AddDeclRef(D->getPropertyDecl(), Record);
406 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000407 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000408 Code = pch::DECL_OBJC_PROPERTY_IMPL;
409}
410
411void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000412 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000413 Record.push_back(D->isMutable());
414 Record.push_back(D->getBitWidth()? 1 : 0);
415 if (D->getBitWidth())
416 Writer.AddStmt(D->getBitWidth());
417 Code = pch::DECL_FIELD;
418}
419
420void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000421 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000422 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000423 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000424 Record.push_back(D->isThreadSpecified());
425 Record.push_back(D->hasCXXDirectInitializer());
426 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000427 Record.push_back(D->isExceptionVariable());
Douglas Gregor5077c382010-05-15 06:01:05 +0000428 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000429 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000430 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000431 if (D->getInit())
432 Writer.AddStmt(D->getInit());
433 Code = pch::DECL_VAR;
434}
435
436void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
437 VisitVarDecl(D);
438 Code = pch::DECL_IMPLICIT_PARAM;
439}
440
441void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
442 VisitVarDecl(D);
443 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000444 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000445 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000446
Chris Lattnerea5ce472009-04-27 07:35:58 +0000447 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
448 // we dynamically check for the properties that we optimize for, but don't
449 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000450 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000451 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000452 !D->isImplicit() &&
Douglas Gregorc070cc62010-06-17 23:14:26 +0000453 !D->isUsed(false) &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000454 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000455 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000456 D->getStorageClass() == 0 &&
457 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000458 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000459 !D->hasInheritedDefaultArg() &&
460 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000461 AbbrevToUse = Writer.getParmVarDeclAbbrev();
462
463 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
464 // just us assuming it.
465 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
466 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
467 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
468 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000469 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000470 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000471}
472
Chris Lattner12b1c762009-04-27 06:16:06 +0000473void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
474 VisitDecl(D);
475 Writer.AddStmt(D->getAsmString());
476 Code = pch::DECL_FILE_SCOPE_ASM;
477}
478
479void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
480 VisitDecl(D);
481 Writer.AddStmt(D->getBody());
John McCall82dc0092010-06-04 11:21:44 +0000482 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000483 Record.push_back(D->param_size());
484 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
485 P != PEnd; ++P)
486 Writer.AddDeclRef(*P, Record);
487 Code = pch::DECL_BLOCK;
488}
489
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000490void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
491 VisitDecl(D);
492 // FIXME: It might be nice to serialize the brace locations for this
493 // declaration, which don't seem to be readily available in the AST.
494 Record.push_back(D->getLanguage());
495 Record.push_back(D->hasBraces());
496 Code = pch::DECL_LINKAGE_SPEC;
497}
498
499void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
500 VisitNamedDecl(D);
501 Writer.AddSourceLocation(D->getLBracLoc(), Record);
502 Writer.AddSourceLocation(D->getRBracLoc(), Record);
503 Writer.AddDeclRef(D->getNextNamespace(), Record);
504
505 // Only write one reference--original or anonymous
506 Record.push_back(D->isOriginalNamespace());
507 if (D->isOriginalNamespace())
508 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
509 else
510 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
511 Code = pch::DECL_NAMESPACE;
512}
513
514void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
515 VisitNamedDecl(D);
516 Writer.AddSourceLocation(D->getAliasLoc(), Record);
517 Writer.AddSourceRange(D->getQualifierRange(), Record);
518 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
519 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
520 Writer.AddDeclRef(D->getNamespace(), Record);
521 Code = pch::DECL_NAMESPACE_ALIAS;
522}
523
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000524void PCHDeclWriter::VisitUsingDecl(UsingDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000525 VisitNamedDecl(D);
526 Writer.AddSourceRange(D->getNestedNameRange(), Record);
527 Writer.AddSourceLocation(D->getUsingLocation(), Record);
528 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
529 Record.push_back(D->getNumShadowDecls());
530 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
531 PEnd = D->shadow_end(); P != PEnd; ++P)
532 Writer.AddDeclRef(*P, Record);
533 Record.push_back(D->isTypeName());
534 Code = pch::DECL_USING;
535}
536
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000537void PCHDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000538 VisitNamedDecl(D);
539 Writer.AddDeclRef(D->getTargetDecl(), Record);
540 Writer.AddDeclRef(D->getUsingDecl(), Record);
541 Code = pch::DECL_USING_SHADOW;
542}
543
544void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
545 VisitNamedDecl(D);
546 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
547 Writer.AddSourceRange(D->getQualifierRange(), Record);
548 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
549 Writer.AddSourceLocation(D->getIdentLocation(), Record);
550 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
551 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
552 Code = pch::DECL_USING_DIRECTIVE;
553}
554
555void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) {
556 VisitValueDecl(D);
557 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
558 Writer.AddSourceLocation(D->getUsingLoc(), Record);
559 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
560 Code = pch::DECL_UNRESOLVED_USING_VALUE;
561}
562
563void PCHDeclWriter::VisitUnresolvedUsingTypename(
564 UnresolvedUsingTypenameDecl *D) {
565 VisitTypeDecl(D);
566 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
567 Writer.AddSourceLocation(D->getUsingLoc(), Record);
568 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
569 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
570 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
571}
572
John McCall5250f272010-06-03 19:28:45 +0000573void PCHDeclWriter::WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base) {
574 Record.push_back(Base->isVirtual());
575 Record.push_back(Base->isBaseOfClass());
576 Record.push_back(Base->getAccessSpecifierAsWritten());
577 Writer.AddTypeRef(Base->getType(), Record);
578 Writer.AddSourceRange(Base->getSourceRange(), Record);
579}
580
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000581void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
582 // assert(false && "cannot write CXXRecordDecl");
583 VisitRecordDecl(D);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000584
585 enum {
586 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
587 };
588 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
589 Record.push_back(CXXRecTemplate);
590 Writer.AddDeclRef(TemplD, Record);
591 } else if (MemberSpecializationInfo *MSInfo
592 = D->getMemberSpecializationInfo()) {
593 Record.push_back(CXXRecMemberSpecialization);
594 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
595 Record.push_back(MSInfo->getTemplateSpecializationKind());
596 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
597 } else {
598 Record.push_back(CXXRecNotTemplate);
599 }
600
Argyrios Kyrtzidis28d16d72010-06-19 19:29:21 +0000601 // FIXME: Hack. See PCHDeclWriter::VisitTypeDecl.
602 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
603
John McCall5250f272010-06-03 19:28:45 +0000604 if (D->isDefinition()) {
605 unsigned NumBases = D->getNumBases();
606 Record.push_back(NumBases);
607 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
608 E = D->bases_end(); I != E; ++I)
609 WriteCXXBaseSpecifier(&*I);
610 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000611 Code = pch::DECL_CXX_RECORD;
612}
613
614void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
615 // assert(false && "cannot write CXXMethodDecl");
616 VisitFunctionDecl(D);
617 Code = pch::DECL_CXX_METHOD;
618}
619
620void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
621 // assert(false && "cannot write CXXConstructorDecl");
622 VisitCXXMethodDecl(D);
623 Code = pch::DECL_CXX_CONSTRUCTOR;
624}
625
626void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
627 // assert(false && "cannot write CXXDestructorDecl");
628 VisitCXXMethodDecl(D);
629 Code = pch::DECL_CXX_DESTRUCTOR;
630}
631
632void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
633 // assert(false && "cannot write CXXConversionDecl");
634 VisitCXXMethodDecl(D);
635 Code = pch::DECL_CXX_CONVERSION;
636}
637
Abramo Bagnara6206d532010-06-05 05:09:32 +0000638void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
639 VisitDecl(D);
640 Writer.AddSourceLocation(D->getColonLoc(), Record);
641 Code = pch::DECL_ACCESS_SPEC;
642}
643
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000644void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
645 assert(false && "cannot write FriendTemplateDecl");
646}
647
648void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000649 VisitNamedDecl(D);
650
651 Writer.AddDeclRef(D->getTemplatedDecl(), Record);
652 {
653 // TemplateParams.
654 TemplateParameterList *TPL = D->getTemplateParameters();
655 assert(TPL && "No TemplateParameters!");
656 Writer.AddSourceLocation(TPL->getTemplateLoc(), Record);
657 Writer.AddSourceLocation(TPL->getLAngleLoc(), Record);
658 Writer.AddSourceLocation(TPL->getRAngleLoc(), Record);
659 Record.push_back(TPL->size());
660 for (TemplateParameterList::iterator P = TPL->begin(), PEnd = TPL->end();
661 P != PEnd; ++P)
662 Writer.AddDeclRef(*P, Record);
663 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000664}
665
666void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000667 VisitTemplateDecl(D);
668
669 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
670 if (D->getPreviousDeclaration() == 0) {
671 // This ClassTemplateDecl owns the CommonPtr; write it.
672
673 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy;
674 CTSDSetTy &CTSDSet = D->getSpecializations();
675 Record.push_back(CTSDSet.size());
676 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I)
677 Writer.AddDeclRef(&*I, Record);
678
679 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy;
680 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
681 Record.push_back(CTPSDSet.size());
682 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E = CTPSDSet.end(); I!=E; ++I)
683 Writer.AddDeclRef(&*I, Record);
684
685 // InjectedClassNameType is computed, no need to write it.
686
687 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
688 if (D->getInstantiatedFromMemberTemplate())
689 Record.push_back(D->isMemberSpecialization());
690 }
691 Code = pch::DECL_CLASS_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000692}
693
694void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
695 ClassTemplateSpecializationDecl *D) {
696 assert(false && "cannot write ClassTemplateSpecializationDecl");
697}
698
699void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
700 ClassTemplatePartialSpecializationDecl *D) {
701 assert(false && "cannot write ClassTemplatePartialSpecializationDecl");
702}
703
704void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) {
705 assert(false && "cannot write FunctionTemplateDecl");
706}
707
708void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000709 VisitTypeDecl(D);
710
711 Record.push_back(D->wasDeclaredWithTypename());
712 Record.push_back(D->isParameterPack());
713 Record.push_back(D->defaultArgumentWasInherited());
714 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
715
716 Code = pch::DECL_TEMPLATE_TYPE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000717}
718
719void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
720 assert(false && "cannot write NonTypeTemplateParmDecl");
721}
722
723void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
724 assert(false && "cannot write TemplateTemplateParmDecl");
725}
726
727void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
728 assert(false && "cannot write StaticAssertDecl");
729}
730
Chris Lattner12b1c762009-04-27 06:16:06 +0000731/// \brief Emit the DeclContext part of a declaration context decl.
732///
733/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
734/// block for this declaration context is stored. May be 0 to indicate
735/// that there are no declarations stored within this context.
736///
737/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
738/// block for this declaration context is stored. May be 0 to indicate
739/// that there are no declarations visible from this context. Note
740/// that this value will not be emitted for non-primary declaration
741/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000742void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000743 uint64_t VisibleOffset) {
744 Record.push_back(LexicalOffset);
745 Record.push_back(VisibleOffset);
746}
747
748
749//===----------------------------------------------------------------------===//
750// PCHWriter Implementation
751//===----------------------------------------------------------------------===//
752
Chris Lattnerea5ce472009-04-27 07:35:58 +0000753void PCHWriter::WriteDeclsBlockAbbrevs() {
754 using namespace llvm;
755 // Abbreviation for DECL_PARM_VAR.
756 BitCodeAbbrev *Abv = new BitCodeAbbrev();
757 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
758
759 // Decl
760 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
761 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
762 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
763 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
764 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
765 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000766 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000767 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000768 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chris Lattnerea5ce472009-04-27 07:35:58 +0000770 // NamedDecl
771 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
772 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
773 // ValueDecl
774 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000775 // DeclaratorDecl
776 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000777 // VarDecl
778 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000779 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000780 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
781 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
782 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000783 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +0000784 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000785 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000786 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
787 // ParmVarDecl
788 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000789 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattnerea5ce472009-04-27 07:35:58 +0000791 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
792}
793
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000794/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
795/// consumers of the AST.
796///
797/// Such decls will always be deserialized from the PCH file, so we would like
798/// this to be as restrictive as possible. Currently the predicate is driven by
799/// code generation requirements, if other clients have a different notion of
800/// what is "required" then we may have to consider an alternate scheme where
801/// clients can iterate over the top-level decls and get information on them,
802/// without necessary deserializing them. We could explicitly require such
803/// clients to use a separate API call to "realize" the decl. This should be
804/// relatively painless since they would presumably only do it for top-level
805/// decls.
806//
807// FIXME: This predicate is essentially IRgen's predicate to determine whether a
808// declaration can be deferred. Merge them somehow.
809static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
810 // File scoped assembly must be seen.
811 if (isa<FileScopeAsmDecl>(D))
812 return true;
813
814 // Otherwise if this isn't a function or a file scoped variable it doesn't
815 // need to be seen.
816 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
817 if (!VD->isFileVarDecl())
818 return false;
819 } else if (!isa<FunctionDecl>(D))
820 return false;
821
822 // Aliases and used decls must be seen.
823 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
824 return true;
825
826 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
827 // Forward declarations don't need to be seen.
828 if (!FD->isThisDeclarationADefinition())
829 return false;
830
831 // Constructors and destructors must be seen.
832 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
833 return true;
834
835 // Otherwise, this is required unless it is static.
836 //
837 // FIXME: Inlines.
838 return FD->getStorageClass() != FunctionDecl::Static;
839 } else {
840 const VarDecl *VD = cast<VarDecl>(D);
841
842 // In C++, this doesn't need to be seen if it is marked "extern".
843 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
844 (VD->getStorageClass() == VarDecl::Extern ||
845 VD->isExternC()))
846 return false;
847
848 // In C, this doesn't need to be seen unless it is a definition.
849 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
850 return false;
851
852 // Otherwise, this is required unless it is static.
853 return VD->getStorageClass() != VarDecl::Static;
854 }
855}
856
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000857void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000858 RecordData Record;
859 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000860
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000861 // If this declaration is also a DeclContext, write blocks for the
862 // declarations that lexically stored inside its context and those
863 // declarations that are visible from its context. These blocks
864 // are written before the declaration itself so that we can put
865 // their offsets into the record for the declaration.
866 uint64_t LexicalOffset = 0;
867 uint64_t VisibleOffset = 0;
868 DeclContext *DC = dyn_cast<DeclContext>(D);
869 if (DC) {
870 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
871 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000872 }
873
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000874 // Determine the ID for this declaration
875 pch::DeclID &ID = DeclIDs[D];
876 if (ID == 0)
877 ID = DeclIDs.size();
878
879 unsigned Index = ID - 1;
880
881 // Record the offset for this declaration
882 if (DeclOffsets.size() == Index)
883 DeclOffsets.push_back(Stream.GetCurrentBitNo());
884 else if (DeclOffsets.size() < Index) {
885 DeclOffsets.resize(Index+1);
886 DeclOffsets[Index] = Stream.GetCurrentBitNo();
887 }
888
889 // Build and emit a record for this declaration
890 Record.clear();
891 W.Code = (pch::DeclCode)0;
892 W.AbbrevToUse = 0;
893 W.Visit(D);
894 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
895
Daniel Dunbar33671982009-12-03 09:13:36 +0000896 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000897 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000898 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000899 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
900
901 // If the declaration had any attributes, write them now.
902 if (D->hasAttrs())
903 WriteAttributeRecord(D->getAttrs());
904
905 // Flush any expressions that were written as part of this declaration.
906 FlushStmts();
907
908 // Note "external" declarations so that we can add them to a record in the
909 // PCH file later.
910 //
911 // FIXME: This should be renamed, the predicate is much more complicated.
912 if (isRequiredDecl(D, Context))
913 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000914}