blob: 9cc7b4833d633b86318feca15502699b8163d22e [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);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +000078 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000079 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());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000224 Writer.AddSourceLocation(D->getLocEnd(), Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000225
226 Record.push_back(D->getTemplatedKind());
227 switch (D->getTemplatedKind()) {
228 case FunctionDecl::TK_NonTemplate:
229 break;
230 case FunctionDecl::TK_FunctionTemplate:
231 Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
232 break;
233 case FunctionDecl::TK_MemberSpecialization: {
234 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
235 Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
236 Record.push_back(MemberInfo->getTemplateSpecializationKind());
237 Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
238 break;
239 }
240 case FunctionDecl::TK_FunctionTemplateSpecialization: {
241 FunctionTemplateSpecializationInfo *
242 FTSInfo = D->getTemplateSpecializationInfo();
243 Writer.AddDeclRef(FTSInfo->getTemplate(), Record);
244 Record.push_back(FTSInfo->getTemplateSpecializationKind());
245
246 // Template arguments.
247 assert(FTSInfo->TemplateArguments && "No template args!");
248 Record.push_back(FTSInfo->TemplateArguments->flat_size());
249 for (int i=0, e = FTSInfo->TemplateArguments->flat_size(); i != e; ++i)
250 Writer.AddTemplateArgument(FTSInfo->TemplateArguments->get(i), Record);
251
252 // Template args as written.
253 if (FTSInfo->TemplateArgumentsAsWritten) {
254 Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size());
255 for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i)
256 Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
257 Record);
258 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(),
259 Record);
260 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(),
261 Record);
262 } else {
263 Record.push_back(0);
264 }
265 }
266 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
267 DependentFunctionTemplateSpecializationInfo *
268 DFTSInfo = D->getDependentSpecializationInfo();
269
270 // Templates.
271 Record.push_back(DFTSInfo->getNumTemplates());
272 for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
273 Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
274
275 // Templates args.
276 Record.push_back(DFTSInfo->getNumTemplateArgs());
277 for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
278 Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
279 }
280 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000281
Chris Lattner12b1c762009-04-27 06:16:06 +0000282 Record.push_back(D->param_size());
283 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
284 P != PEnd; ++P)
285 Writer.AddDeclRef(*P, Record);
286 Code = pch::DECL_FUNCTION;
287}
288
289void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
290 VisitNamedDecl(D);
291 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000292 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000293 Record.push_back(D->getBody() != 0);
294 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000295 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000296 Writer.AddDeclRef(D->getSelfDecl(), Record);
297 Writer.AddDeclRef(D->getCmdDecl(), Record);
298 }
299 Record.push_back(D->isInstanceMethod());
300 Record.push_back(D->isVariadic());
301 Record.push_back(D->isSynthesized());
302 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000303 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000304 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000305 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000306 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000307 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000308 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000309 Writer.AddSourceLocation(D->getLocEnd(), Record);
310 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000311 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000312 PEnd = D->param_end(); P != PEnd; ++P)
313 Writer.AddDeclRef(*P, Record);
314 Code = pch::DECL_OBJC_METHOD;
315}
316
317void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
318 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000319 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000320 // Abstract class (no need to define a stable pch::DECL code).
321}
322
323void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
324 VisitObjCContainerDecl(D);
325 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
326 Writer.AddDeclRef(D->getSuperClass(), Record);
327 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000328 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000329 PEnd = D->protocol_end();
330 P != PEnd; ++P)
331 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000332 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
333 PLEnd = D->protocol_loc_end();
334 PL != PLEnd; ++PL)
335 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000336 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000337 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000338 IEnd = D->ivar_end(); I != IEnd; ++I)
339 Writer.AddDeclRef(*I, Record);
340 Writer.AddDeclRef(D->getCategoryList(), Record);
341 Record.push_back(D->isForwardDecl());
342 Record.push_back(D->isImplicitInterfaceDecl());
343 Writer.AddSourceLocation(D->getClassLoc(), Record);
344 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
345 Writer.AddSourceLocation(D->getLocEnd(), Record);
346 Code = pch::DECL_OBJC_INTERFACE;
347}
348
349void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
350 VisitFieldDecl(D);
351 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000352 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000353 Code = pch::DECL_OBJC_IVAR;
354}
355
356void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
357 VisitObjCContainerDecl(D);
358 Record.push_back(D->isForwardDecl());
359 Writer.AddSourceLocation(D->getLocEnd(), Record);
360 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000361 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000362 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
363 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000364 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
365 PLEnd = D->protocol_loc_end();
366 PL != PLEnd; ++PL)
367 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000368 Code = pch::DECL_OBJC_PROTOCOL;
369}
370
371void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
372 VisitFieldDecl(D);
373 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
374}
375
376void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
377 VisitDecl(D);
378 Record.push_back(D->size());
379 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000380 Writer.AddDeclRef(I->getInterface(), Record);
381 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
382 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000383 Code = pch::DECL_OBJC_CLASS;
384}
385
386void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
387 VisitDecl(D);
388 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000389 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000390 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
391 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000392 for (ObjCForwardProtocolDecl::protocol_loc_iterator
393 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
394 PL != PLEnd; ++PL)
395 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000396 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
397}
398
399void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
400 VisitObjCContainerDecl(D);
401 Writer.AddDeclRef(D->getClassInterface(), Record);
402 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000403 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000404 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
405 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000406 for (ObjCCategoryDecl::protocol_loc_iterator
407 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
408 PL != PLEnd; ++PL)
409 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000410 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000411 Writer.AddSourceLocation(D->getAtLoc(), Record);
412 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000413 Code = pch::DECL_OBJC_CATEGORY;
414}
415
416void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
417 VisitNamedDecl(D);
418 Writer.AddDeclRef(D->getClassInterface(), Record);
419 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
420}
421
422void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
423 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000424 Writer.AddSourceLocation(D->getAtLoc(), Record);
John McCall83a230c2010-06-04 20:50:08 +0000425 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000426 // FIXME: stable encoding
427 Record.push_back((unsigned)D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000428 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000429 // FIXME: stable encoding
430 Record.push_back((unsigned)D->getPropertyImplementation());
431 Writer.AddDeclarationName(D->getGetterName(), Record);
432 Writer.AddDeclarationName(D->getSetterName(), Record);
433 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
434 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
435 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
436 Code = pch::DECL_OBJC_PROPERTY;
437}
438
439void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000440 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000441 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000442 // Abstract class (no need to define a stable pch::DECL code).
443}
444
445void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
446 VisitObjCImplDecl(D);
447 Writer.AddIdentifierRef(D->getIdentifier(), Record);
448 Code = pch::DECL_OBJC_CATEGORY_IMPL;
449}
450
451void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
452 VisitObjCImplDecl(D);
453 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000454 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000455 Code = pch::DECL_OBJC_IMPLEMENTATION;
456}
457
458void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
459 VisitDecl(D);
460 Writer.AddSourceLocation(D->getLocStart(), Record);
461 Writer.AddDeclRef(D->getPropertyDecl(), Record);
462 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000463 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000464 Code = pch::DECL_OBJC_PROPERTY_IMPL;
465}
466
467void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000468 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000469 Record.push_back(D->isMutable());
470 Record.push_back(D->getBitWidth()? 1 : 0);
471 if (D->getBitWidth())
472 Writer.AddStmt(D->getBitWidth());
473 Code = pch::DECL_FIELD;
474}
475
476void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000477 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000478 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000479 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000480 Record.push_back(D->isThreadSpecified());
481 Record.push_back(D->hasCXXDirectInitializer());
482 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000483 Record.push_back(D->isExceptionVariable());
Douglas Gregor5077c382010-05-15 06:01:05 +0000484 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000485 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000486 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000487 if (D->getInit())
488 Writer.AddStmt(D->getInit());
489 Code = pch::DECL_VAR;
490}
491
492void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
493 VisitVarDecl(D);
494 Code = pch::DECL_IMPLICIT_PARAM;
495}
496
497void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
498 VisitVarDecl(D);
499 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000500 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000501 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000502
Chris Lattnerea5ce472009-04-27 07:35:58 +0000503 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
504 // we dynamically check for the properties that we optimize for, but don't
505 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000506 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000507 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000508 !D->isImplicit() &&
Douglas Gregorc070cc62010-06-17 23:14:26 +0000509 !D->isUsed(false) &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000510 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000511 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000512 D->getStorageClass() == 0 &&
513 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000514 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000515 !D->hasInheritedDefaultArg() &&
516 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000517 AbbrevToUse = Writer.getParmVarDeclAbbrev();
518
519 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
520 // just us assuming it.
521 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
522 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
523 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
524 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000525 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000526 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000527}
528
Chris Lattner12b1c762009-04-27 06:16:06 +0000529void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
530 VisitDecl(D);
531 Writer.AddStmt(D->getAsmString());
532 Code = pch::DECL_FILE_SCOPE_ASM;
533}
534
535void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
536 VisitDecl(D);
537 Writer.AddStmt(D->getBody());
John McCall82dc0092010-06-04 11:21:44 +0000538 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000539 Record.push_back(D->param_size());
540 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
541 P != PEnd; ++P)
542 Writer.AddDeclRef(*P, Record);
543 Code = pch::DECL_BLOCK;
544}
545
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000546void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
547 VisitDecl(D);
548 // FIXME: It might be nice to serialize the brace locations for this
549 // declaration, which don't seem to be readily available in the AST.
550 Record.push_back(D->getLanguage());
551 Record.push_back(D->hasBraces());
552 Code = pch::DECL_LINKAGE_SPEC;
553}
554
555void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
556 VisitNamedDecl(D);
557 Writer.AddSourceLocation(D->getLBracLoc(), Record);
558 Writer.AddSourceLocation(D->getRBracLoc(), Record);
559 Writer.AddDeclRef(D->getNextNamespace(), Record);
560
561 // Only write one reference--original or anonymous
562 Record.push_back(D->isOriginalNamespace());
563 if (D->isOriginalNamespace())
564 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
565 else
566 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
567 Code = pch::DECL_NAMESPACE;
568}
569
570void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
571 VisitNamedDecl(D);
572 Writer.AddSourceLocation(D->getAliasLoc(), Record);
573 Writer.AddSourceRange(D->getQualifierRange(), Record);
574 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
575 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
576 Writer.AddDeclRef(D->getNamespace(), Record);
577 Code = pch::DECL_NAMESPACE_ALIAS;
578}
579
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000580void PCHDeclWriter::VisitUsingDecl(UsingDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000581 VisitNamedDecl(D);
582 Writer.AddSourceRange(D->getNestedNameRange(), Record);
583 Writer.AddSourceLocation(D->getUsingLocation(), Record);
584 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
585 Record.push_back(D->getNumShadowDecls());
586 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
587 PEnd = D->shadow_end(); P != PEnd; ++P)
588 Writer.AddDeclRef(*P, Record);
589 Record.push_back(D->isTypeName());
590 Code = pch::DECL_USING;
591}
592
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000593void PCHDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000594 VisitNamedDecl(D);
595 Writer.AddDeclRef(D->getTargetDecl(), Record);
596 Writer.AddDeclRef(D->getUsingDecl(), Record);
597 Code = pch::DECL_USING_SHADOW;
598}
599
600void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
601 VisitNamedDecl(D);
602 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
603 Writer.AddSourceRange(D->getQualifierRange(), Record);
604 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
605 Writer.AddSourceLocation(D->getIdentLocation(), Record);
606 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
607 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
608 Code = pch::DECL_USING_DIRECTIVE;
609}
610
611void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) {
612 VisitValueDecl(D);
613 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
614 Writer.AddSourceLocation(D->getUsingLoc(), Record);
615 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
616 Code = pch::DECL_UNRESOLVED_USING_VALUE;
617}
618
619void PCHDeclWriter::VisitUnresolvedUsingTypename(
620 UnresolvedUsingTypenameDecl *D) {
621 VisitTypeDecl(D);
622 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
623 Writer.AddSourceLocation(D->getUsingLoc(), Record);
624 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
625 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
626 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
627}
628
John McCall5250f272010-06-03 19:28:45 +0000629void PCHDeclWriter::WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base) {
630 Record.push_back(Base->isVirtual());
631 Record.push_back(Base->isBaseOfClass());
632 Record.push_back(Base->getAccessSpecifierAsWritten());
633 Writer.AddTypeRef(Base->getType(), Record);
634 Writer.AddSourceRange(Base->getSourceRange(), Record);
635}
636
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000637void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
638 // assert(false && "cannot write CXXRecordDecl");
639 VisitRecordDecl(D);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000640
641 enum {
642 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
643 };
644 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
645 Record.push_back(CXXRecTemplate);
646 Writer.AddDeclRef(TemplD, Record);
647 } else if (MemberSpecializationInfo *MSInfo
648 = D->getMemberSpecializationInfo()) {
649 Record.push_back(CXXRecMemberSpecialization);
650 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
651 Record.push_back(MSInfo->getTemplateSpecializationKind());
652 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
653 } else {
654 Record.push_back(CXXRecNotTemplate);
655 }
656
Argyrios Kyrtzidis28d16d72010-06-19 19:29:21 +0000657 // FIXME: Hack. See PCHDeclWriter::VisitTypeDecl.
658 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
659
John McCall5250f272010-06-03 19:28:45 +0000660 if (D->isDefinition()) {
661 unsigned NumBases = D->getNumBases();
662 Record.push_back(NumBases);
663 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
664 E = D->bases_end(); I != E; ++I)
665 WriteCXXBaseSpecifier(&*I);
666 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000667 Code = pch::DECL_CXX_RECORD;
668}
669
670void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
671 // assert(false && "cannot write CXXMethodDecl");
672 VisitFunctionDecl(D);
673 Code = pch::DECL_CXX_METHOD;
674}
675
676void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
677 // assert(false && "cannot write CXXConstructorDecl");
678 VisitCXXMethodDecl(D);
679 Code = pch::DECL_CXX_CONSTRUCTOR;
680}
681
682void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
683 // assert(false && "cannot write CXXDestructorDecl");
684 VisitCXXMethodDecl(D);
685 Code = pch::DECL_CXX_DESTRUCTOR;
686}
687
688void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
689 // assert(false && "cannot write CXXConversionDecl");
690 VisitCXXMethodDecl(D);
691 Code = pch::DECL_CXX_CONVERSION;
692}
693
Abramo Bagnara6206d532010-06-05 05:09:32 +0000694void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
695 VisitDecl(D);
696 Writer.AddSourceLocation(D->getColonLoc(), Record);
697 Code = pch::DECL_ACCESS_SPEC;
698}
699
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000700void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
701 assert(false && "cannot write FriendTemplateDecl");
702}
703
704void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000705 VisitNamedDecl(D);
706
707 Writer.AddDeclRef(D->getTemplatedDecl(), Record);
708 {
709 // TemplateParams.
710 TemplateParameterList *TPL = D->getTemplateParameters();
711 assert(TPL && "No TemplateParameters!");
712 Writer.AddSourceLocation(TPL->getTemplateLoc(), Record);
713 Writer.AddSourceLocation(TPL->getLAngleLoc(), Record);
714 Writer.AddSourceLocation(TPL->getRAngleLoc(), Record);
715 Record.push_back(TPL->size());
716 for (TemplateParameterList::iterator P = TPL->begin(), PEnd = TPL->end();
717 P != PEnd; ++P)
718 Writer.AddDeclRef(*P, Record);
719 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000720}
721
722void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000723 VisitTemplateDecl(D);
724
725 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
726 if (D->getPreviousDeclaration() == 0) {
727 // This ClassTemplateDecl owns the CommonPtr; write it.
728
729 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy;
730 CTSDSetTy &CTSDSet = D->getSpecializations();
731 Record.push_back(CTSDSet.size());
732 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I)
733 Writer.AddDeclRef(&*I, Record);
734
735 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy;
736 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
737 Record.push_back(CTPSDSet.size());
738 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E = CTPSDSet.end(); I!=E; ++I)
739 Writer.AddDeclRef(&*I, Record);
740
741 // InjectedClassNameType is computed, no need to write it.
742
743 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
744 if (D->getInstantiatedFromMemberTemplate())
745 Record.push_back(D->isMemberSpecialization());
746 }
747 Code = pch::DECL_CLASS_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000748}
749
750void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
751 ClassTemplateSpecializationDecl *D) {
752 assert(false && "cannot write ClassTemplateSpecializationDecl");
753}
754
755void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
756 ClassTemplatePartialSpecializationDecl *D) {
757 assert(false && "cannot write ClassTemplatePartialSpecializationDecl");
758}
759
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000760void PCHDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
761 VisitTemplateDecl(D);
762
763 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
764 if (D->getPreviousDeclaration() == 0) {
765 // This FunctionTemplateDecl owns the CommonPtr; write it.
766
767 // FunctionTemplateSpecializationInfos are filled through the
768 // templated FunctionDecl's setFunctionTemplateSpecialization, no need to
769 // write them here.
770
771 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
772 if (D->getInstantiatedFromMemberTemplate())
773 Record.push_back(D->isMemberSpecialization());
774 }
775 Code = pch::DECL_FUNCTION_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000776}
777
778void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000779 VisitTypeDecl(D);
780
781 Record.push_back(D->wasDeclaredWithTypename());
782 Record.push_back(D->isParameterPack());
783 Record.push_back(D->defaultArgumentWasInherited());
784 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
785
786 Code = pch::DECL_TEMPLATE_TYPE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000787}
788
789void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
790 assert(false && "cannot write NonTypeTemplateParmDecl");
791}
792
793void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
794 assert(false && "cannot write TemplateTemplateParmDecl");
795}
796
797void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
798 assert(false && "cannot write StaticAssertDecl");
799}
800
Chris Lattner12b1c762009-04-27 06:16:06 +0000801/// \brief Emit the DeclContext part of a declaration context decl.
802///
803/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
804/// block for this declaration context is stored. May be 0 to indicate
805/// that there are no declarations stored within this context.
806///
807/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
808/// block for this declaration context is stored. May be 0 to indicate
809/// that there are no declarations visible from this context. Note
810/// that this value will not be emitted for non-primary declaration
811/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000812void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000813 uint64_t VisibleOffset) {
814 Record.push_back(LexicalOffset);
815 Record.push_back(VisibleOffset);
816}
817
818
819//===----------------------------------------------------------------------===//
820// PCHWriter Implementation
821//===----------------------------------------------------------------------===//
822
Chris Lattnerea5ce472009-04-27 07:35:58 +0000823void PCHWriter::WriteDeclsBlockAbbrevs() {
824 using namespace llvm;
825 // Abbreviation for DECL_PARM_VAR.
826 BitCodeAbbrev *Abv = new BitCodeAbbrev();
827 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
828
829 // Decl
830 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
831 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
832 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
833 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
834 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
835 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000836 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000837 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000838 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattnerea5ce472009-04-27 07:35:58 +0000840 // NamedDecl
841 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
842 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
843 // ValueDecl
844 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000845 // DeclaratorDecl
846 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000847 // VarDecl
848 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000849 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000850 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
851 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
852 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000853 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +0000854 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000855 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000856 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
857 // ParmVarDecl
858 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000859 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000860
Chris Lattnerea5ce472009-04-27 07:35:58 +0000861 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
862}
863
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000864/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
865/// consumers of the AST.
866///
867/// Such decls will always be deserialized from the PCH file, so we would like
868/// this to be as restrictive as possible. Currently the predicate is driven by
869/// code generation requirements, if other clients have a different notion of
870/// what is "required" then we may have to consider an alternate scheme where
871/// clients can iterate over the top-level decls and get information on them,
872/// without necessary deserializing them. We could explicitly require such
873/// clients to use a separate API call to "realize" the decl. This should be
874/// relatively painless since they would presumably only do it for top-level
875/// decls.
876//
877// FIXME: This predicate is essentially IRgen's predicate to determine whether a
878// declaration can be deferred. Merge them somehow.
879static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
880 // File scoped assembly must be seen.
881 if (isa<FileScopeAsmDecl>(D))
882 return true;
883
884 // Otherwise if this isn't a function or a file scoped variable it doesn't
885 // need to be seen.
886 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
887 if (!VD->isFileVarDecl())
888 return false;
889 } else if (!isa<FunctionDecl>(D))
890 return false;
891
892 // Aliases and used decls must be seen.
893 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
894 return true;
895
896 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
897 // Forward declarations don't need to be seen.
898 if (!FD->isThisDeclarationADefinition())
899 return false;
900
901 // Constructors and destructors must be seen.
902 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
903 return true;
904
905 // Otherwise, this is required unless it is static.
906 //
907 // FIXME: Inlines.
908 return FD->getStorageClass() != FunctionDecl::Static;
909 } else {
910 const VarDecl *VD = cast<VarDecl>(D);
911
912 // In C++, this doesn't need to be seen if it is marked "extern".
913 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
914 (VD->getStorageClass() == VarDecl::Extern ||
915 VD->isExternC()))
916 return false;
917
918 // In C, this doesn't need to be seen unless it is a definition.
919 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
920 return false;
921
922 // Otherwise, this is required unless it is static.
923 return VD->getStorageClass() != VarDecl::Static;
924 }
925}
926
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000927void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +0000928 RecordData Record;
929 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000930
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000931 // If this declaration is also a DeclContext, write blocks for the
932 // declarations that lexically stored inside its context and those
933 // declarations that are visible from its context. These blocks
934 // are written before the declaration itself so that we can put
935 // their offsets into the record for the declaration.
936 uint64_t LexicalOffset = 0;
937 uint64_t VisibleOffset = 0;
938 DeclContext *DC = dyn_cast<DeclContext>(D);
939 if (DC) {
940 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
941 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +0000942 }
943
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000944 // Determine the ID for this declaration
945 pch::DeclID &ID = DeclIDs[D];
946 if (ID == 0)
947 ID = DeclIDs.size();
948
949 unsigned Index = ID - 1;
950
951 // Record the offset for this declaration
952 if (DeclOffsets.size() == Index)
953 DeclOffsets.push_back(Stream.GetCurrentBitNo());
954 else if (DeclOffsets.size() < Index) {
955 DeclOffsets.resize(Index+1);
956 DeclOffsets[Index] = Stream.GetCurrentBitNo();
957 }
958
959 // Build and emit a record for this declaration
960 Record.clear();
961 W.Code = (pch::DeclCode)0;
962 W.AbbrevToUse = 0;
963 W.Visit(D);
964 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
965
Daniel Dunbar33671982009-12-03 09:13:36 +0000966 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +0000967 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +0000968 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +0000969 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
970
971 // If the declaration had any attributes, write them now.
972 if (D->hasAttrs())
973 WriteAttributeRecord(D->getAttrs());
974
975 // Flush any expressions that were written as part of this declaration.
976 FlushStmts();
977
978 // Note "external" declarations so that we can add them to a record in the
979 // PCH file later.
980 //
981 // FIXME: This should be renamed, the predicate is much more complicated.
982 if (isRequiredDecl(D, Context))
983 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +0000984}