blob: cea7ba0d9e1fcb1faacd57f39ac32e3feff8c24b [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
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +000028namespace clang {
Chris Lattner12b1c762009-04-27 06:16:06 +000029 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);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000052 void VisitUnresolvedUsingTypenameDecl(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);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000064 void VisitUnresolvedUsingValueDecl(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);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +000085 void VisitFriendDecl(FriendDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000086 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
87 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000088 void VisitBlockDecl(BlockDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000089
Mike Stump1eb44332009-09-09 15:08:12 +000090 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000091 uint64_t VisibleOffset);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000092
93
Sean Hunt9a555912010-05-30 07:21:58 +000094 // FIXME: Put in the same order is DeclNodes.td?
Chris Lattner12b1c762009-04-27 06:16:06 +000095 void VisitObjCMethodDecl(ObjCMethodDecl *D);
96 void VisitObjCContainerDecl(ObjCContainerDecl *D);
97 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
98 void VisitObjCIvarDecl(ObjCIvarDecl *D);
99 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
100 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
101 void VisitObjCClassDecl(ObjCClassDecl *D);
102 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
103 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
104 void VisitObjCImplDecl(ObjCImplDecl *D);
105 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
106 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
107 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
108 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
109 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
John McCall5250f272010-06-03 19:28:45 +0000110
111 void WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base);
Chris Lattner12b1c762009-04-27 06:16:06 +0000112 };
113}
114
115void PCHDeclWriter::VisitDecl(Decl *D) {
116 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
117 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
118 Writer.AddSourceLocation(D->getLocation(), Record);
119 Record.push_back(D->isInvalidDecl());
120 Record.push_back(D->hasAttrs());
121 Record.push_back(D->isImplicit());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000122 Record.push_back(D->isUsed(false));
Chris Lattner12b1c762009-04-27 06:16:06 +0000123 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000124 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +0000125}
126
127void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
128 VisitDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000129 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000130 Code = pch::DECL_TRANSLATION_UNIT;
131}
132
133void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
134 VisitDecl(D);
135 Writer.AddDeclarationName(D->getDeclName(), Record);
136}
137
138void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
139 VisitNamedDecl(D);
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000140 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000141}
142
143void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
144 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000145 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000146 Code = pch::DECL_TYPEDEF;
147}
148
149void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
150 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000151 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000152 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
153 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000154 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000155 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000156 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000157 // FIXME: maybe write optional qualifier and its range.
158 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000159}
160
161void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
162 VisitTagDecl(D);
163 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000164 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall1b5a6182010-05-06 08:49:23 +0000165 Record.push_back(D->getNumPositiveBits());
166 Record.push_back(D->getNumNegativeBits());
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000167 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000168 Code = pch::DECL_ENUM;
169}
170
171void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
172 VisitTagDecl(D);
173 Record.push_back(D->hasFlexibleArrayMember());
174 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000175 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000176 Code = pch::DECL_RECORD;
177}
178
179void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
180 VisitNamedDecl(D);
181 Writer.AddTypeRef(D->getType(), Record);
182}
183
184void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
185 VisitValueDecl(D);
186 Record.push_back(D->getInitExpr()? 1 : 0);
187 if (D->getInitExpr())
188 Writer.AddStmt(D->getInitExpr());
189 Writer.AddAPSInt(D->getInitVal(), Record);
190 Code = pch::DECL_ENUM_CONSTANT;
191}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000192
193void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
194 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000195 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000196 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000197}
Chris Lattner12b1c762009-04-27 06:16:06 +0000198
199void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000200 VisitDeclaratorDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000201
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000202 Record.push_back(D->getTemplatedKind());
203 switch (D->getTemplatedKind()) {
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +0000204 default: assert(false && "Unhandled TemplatedKind!");
205 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000206 case FunctionDecl::TK_NonTemplate:
207 break;
208 case FunctionDecl::TK_FunctionTemplate:
209 Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
210 break;
211 case FunctionDecl::TK_MemberSpecialization: {
212 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
213 Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
214 Record.push_back(MemberInfo->getTemplateSpecializationKind());
215 Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
216 break;
217 }
218 case FunctionDecl::TK_FunctionTemplateSpecialization: {
219 FunctionTemplateSpecializationInfo *
220 FTSInfo = D->getTemplateSpecializationInfo();
221 Writer.AddDeclRef(FTSInfo->getTemplate(), Record);
222 Record.push_back(FTSInfo->getTemplateSpecializationKind());
223
224 // Template arguments.
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000225 Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000226
227 // Template args as written.
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000228 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000229 if (FTSInfo->TemplateArgumentsAsWritten) {
230 Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size());
231 for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i)
232 Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
233 Record);
234 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(),
235 Record);
236 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(),
237 Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000238 }
Argyrios Kyrtzidisa56b0492010-06-25 16:24:51 +0000239 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000240 }
241 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
242 DependentFunctionTemplateSpecializationInfo *
243 DFTSInfo = D->getDependentSpecializationInfo();
244
245 // Templates.
246 Record.push_back(DFTSInfo->getNumTemplates());
247 for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
248 Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
249
250 // Templates args.
251 Record.push_back(DFTSInfo->getNumTemplateArgs());
252 for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
253 Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
Argyrios Kyrtzidisa56b0492010-06-25 16:24:51 +0000254 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000255 }
256 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000257
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000258 // Make sure no Exprs are emitted after the body, because when reading the
259 // function, the body doesn't get read so the cursor doesn't advance.
260 Record.push_back(D->isThisDeclarationADefinition());
261 if (D->isThisDeclarationADefinition())
262 Writer.AddStmt(D->getBody());
263
264 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
265 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
266 Record.push_back(D->getStorageClassAsWritten());
267 Record.push_back(D->isInlineSpecified());
268 Record.push_back(D->isVirtualAsWritten());
269 Record.push_back(D->isPure());
270 Record.push_back(D->hasInheritedPrototype());
271 Record.push_back(D->hasWrittenPrototype());
272 Record.push_back(D->isDeleted());
273 Record.push_back(D->isTrivial());
274 Record.push_back(D->isCopyAssignment());
275 Record.push_back(D->hasImplicitReturnZero());
276 Writer.AddSourceLocation(D->getLocEnd(), Record);
277
Chris Lattner12b1c762009-04-27 06:16:06 +0000278 Record.push_back(D->param_size());
279 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
280 P != PEnd; ++P)
281 Writer.AddDeclRef(*P, Record);
282 Code = pch::DECL_FUNCTION;
283}
284
285void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
286 VisitNamedDecl(D);
287 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000288 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000289 Record.push_back(D->getBody() != 0);
290 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000291 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000292 Writer.AddDeclRef(D->getSelfDecl(), Record);
293 Writer.AddDeclRef(D->getCmdDecl(), Record);
294 }
295 Record.push_back(D->isInstanceMethod());
296 Record.push_back(D->isVariadic());
297 Record.push_back(D->isSynthesized());
298 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000299 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000300 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000301 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000302 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000303 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000304 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000305 Writer.AddSourceLocation(D->getLocEnd(), Record);
306 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000307 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000308 PEnd = D->param_end(); P != PEnd; ++P)
309 Writer.AddDeclRef(*P, Record);
310 Code = pch::DECL_OBJC_METHOD;
311}
312
313void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
314 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000315 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000316 // Abstract class (no need to define a stable pch::DECL code).
317}
318
319void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
320 VisitObjCContainerDecl(D);
321 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
322 Writer.AddDeclRef(D->getSuperClass(), Record);
323 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000324 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000325 PEnd = D->protocol_end();
326 P != PEnd; ++P)
327 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000328 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
329 PLEnd = D->protocol_loc_end();
330 PL != PLEnd; ++PL)
331 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000332 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000333 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000334 IEnd = D->ivar_end(); I != IEnd; ++I)
335 Writer.AddDeclRef(*I, Record);
336 Writer.AddDeclRef(D->getCategoryList(), Record);
337 Record.push_back(D->isForwardDecl());
338 Record.push_back(D->isImplicitInterfaceDecl());
339 Writer.AddSourceLocation(D->getClassLoc(), Record);
340 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
341 Writer.AddSourceLocation(D->getLocEnd(), Record);
342 Code = pch::DECL_OBJC_INTERFACE;
343}
344
345void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
346 VisitFieldDecl(D);
347 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000348 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000349 Code = pch::DECL_OBJC_IVAR;
350}
351
352void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
353 VisitObjCContainerDecl(D);
354 Record.push_back(D->isForwardDecl());
355 Writer.AddSourceLocation(D->getLocEnd(), Record);
356 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000357 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000358 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
359 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000360 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
361 PLEnd = D->protocol_loc_end();
362 PL != PLEnd; ++PL)
363 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000364 Code = pch::DECL_OBJC_PROTOCOL;
365}
366
367void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
368 VisitFieldDecl(D);
369 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
370}
371
372void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
373 VisitDecl(D);
374 Record.push_back(D->size());
375 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000376 Writer.AddDeclRef(I->getInterface(), Record);
377 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
378 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000379 Code = pch::DECL_OBJC_CLASS;
380}
381
382void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
383 VisitDecl(D);
384 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000385 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000386 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
387 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000388 for (ObjCForwardProtocolDecl::protocol_loc_iterator
389 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
390 PL != PLEnd; ++PL)
391 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000392 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
393}
394
395void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
396 VisitObjCContainerDecl(D);
397 Writer.AddDeclRef(D->getClassInterface(), Record);
398 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000399 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000400 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
401 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000402 for (ObjCCategoryDecl::protocol_loc_iterator
403 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
404 PL != PLEnd; ++PL)
405 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000406 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000407 Writer.AddSourceLocation(D->getAtLoc(), Record);
408 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000409 Code = pch::DECL_OBJC_CATEGORY;
410}
411
412void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
413 VisitNamedDecl(D);
414 Writer.AddDeclRef(D->getClassInterface(), Record);
415 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
416}
417
418void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
419 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000420 Writer.AddSourceLocation(D->getAtLoc(), Record);
John McCall83a230c2010-06-04 20:50:08 +0000421 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000422 // FIXME: stable encoding
423 Record.push_back((unsigned)D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000424 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000425 // FIXME: stable encoding
426 Record.push_back((unsigned)D->getPropertyImplementation());
427 Writer.AddDeclarationName(D->getGetterName(), Record);
428 Writer.AddDeclarationName(D->getSetterName(), Record);
429 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
430 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
431 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
432 Code = pch::DECL_OBJC_PROPERTY;
433}
434
435void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000436 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000437 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000438 // Abstract class (no need to define a stable pch::DECL code).
439}
440
441void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
442 VisitObjCImplDecl(D);
443 Writer.AddIdentifierRef(D->getIdentifier(), Record);
444 Code = pch::DECL_OBJC_CATEGORY_IMPL;
445}
446
447void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
448 VisitObjCImplDecl(D);
449 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000450 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000451 Code = pch::DECL_OBJC_IMPLEMENTATION;
452}
453
454void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
455 VisitDecl(D);
456 Writer.AddSourceLocation(D->getLocStart(), Record);
457 Writer.AddDeclRef(D->getPropertyDecl(), Record);
458 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000459 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000460 Code = pch::DECL_OBJC_PROPERTY_IMPL;
461}
462
463void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000464 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000465 Record.push_back(D->isMutable());
466 Record.push_back(D->getBitWidth()? 1 : 0);
467 if (D->getBitWidth())
468 Writer.AddStmt(D->getBitWidth());
469 Code = pch::DECL_FIELD;
470}
471
472void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000473 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000474 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000475 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000476 Record.push_back(D->isThreadSpecified());
477 Record.push_back(D->hasCXXDirectInitializer());
478 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000479 Record.push_back(D->isExceptionVariable());
Douglas Gregor5077c382010-05-15 06:01:05 +0000480 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000481 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000482 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000483 if (D->getInit())
484 Writer.AddStmt(D->getInit());
485 Code = pch::DECL_VAR;
486}
487
488void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
489 VisitVarDecl(D);
490 Code = pch::DECL_IMPLICIT_PARAM;
491}
492
493void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
494 VisitVarDecl(D);
495 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000496 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000497 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattnerea5ce472009-04-27 07:35:58 +0000499 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
500 // we dynamically check for the properties that we optimize for, but don't
501 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000502 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000503 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000504 !D->isImplicit() &&
Douglas Gregorc070cc62010-06-17 23:14:26 +0000505 !D->isUsed(false) &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000506 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000507 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000508 D->getStorageClass() == 0 &&
509 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000510 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000511 !D->hasInheritedDefaultArg() &&
512 D->getInit() == 0) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000513 AbbrevToUse = Writer.getParmVarDeclAbbrev();
514
515 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
516 // just us assuming it.
517 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
518 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
519 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
520 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000521 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000522 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Chris Lattner12b1c762009-04-27 06:16:06 +0000523}
524
Chris Lattner12b1c762009-04-27 06:16:06 +0000525void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
526 VisitDecl(D);
527 Writer.AddStmt(D->getAsmString());
528 Code = pch::DECL_FILE_SCOPE_ASM;
529}
530
531void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
532 VisitDecl(D);
533 Writer.AddStmt(D->getBody());
John McCall82dc0092010-06-04 11:21:44 +0000534 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000535 Record.push_back(D->param_size());
536 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
537 P != PEnd; ++P)
538 Writer.AddDeclRef(*P, Record);
539 Code = pch::DECL_BLOCK;
540}
541
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000542void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
543 VisitDecl(D);
544 // FIXME: It might be nice to serialize the brace locations for this
545 // declaration, which don't seem to be readily available in the AST.
546 Record.push_back(D->getLanguage());
547 Record.push_back(D->hasBraces());
548 Code = pch::DECL_LINKAGE_SPEC;
549}
550
551void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
552 VisitNamedDecl(D);
553 Writer.AddSourceLocation(D->getLBracLoc(), Record);
554 Writer.AddSourceLocation(D->getRBracLoc(), Record);
555 Writer.AddDeclRef(D->getNextNamespace(), Record);
556
557 // Only write one reference--original or anonymous
558 Record.push_back(D->isOriginalNamespace());
559 if (D->isOriginalNamespace())
560 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
561 else
562 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
563 Code = pch::DECL_NAMESPACE;
564}
565
566void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
567 VisitNamedDecl(D);
568 Writer.AddSourceLocation(D->getAliasLoc(), Record);
569 Writer.AddSourceRange(D->getQualifierRange(), Record);
570 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
571 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
572 Writer.AddDeclRef(D->getNamespace(), Record);
573 Code = pch::DECL_NAMESPACE_ALIAS;
574}
575
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000576void PCHDeclWriter::VisitUsingDecl(UsingDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000577 VisitNamedDecl(D);
578 Writer.AddSourceRange(D->getNestedNameRange(), Record);
579 Writer.AddSourceLocation(D->getUsingLocation(), Record);
580 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
581 Record.push_back(D->getNumShadowDecls());
582 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
583 PEnd = D->shadow_end(); P != PEnd; ++P)
584 Writer.AddDeclRef(*P, Record);
585 Record.push_back(D->isTypeName());
586 Code = pch::DECL_USING;
587}
588
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000589void PCHDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000590 VisitNamedDecl(D);
591 Writer.AddDeclRef(D->getTargetDecl(), Record);
592 Writer.AddDeclRef(D->getUsingDecl(), Record);
593 Code = pch::DECL_USING_SHADOW;
594}
595
596void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
597 VisitNamedDecl(D);
598 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
599 Writer.AddSourceRange(D->getQualifierRange(), Record);
600 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
601 Writer.AddSourceLocation(D->getIdentLocation(), Record);
602 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
603 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
604 Code = pch::DECL_USING_DIRECTIVE;
605}
606
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000607void PCHDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000608 VisitValueDecl(D);
609 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
610 Writer.AddSourceLocation(D->getUsingLoc(), Record);
611 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
612 Code = pch::DECL_UNRESOLVED_USING_VALUE;
613}
614
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000615void PCHDeclWriter::VisitUnresolvedUsingTypenameDecl(
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000616 UnresolvedUsingTypenameDecl *D) {
617 VisitTypeDecl(D);
618 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
619 Writer.AddSourceLocation(D->getUsingLoc(), Record);
620 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
621 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
622 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
623}
624
John McCall5250f272010-06-03 19:28:45 +0000625void PCHDeclWriter::WriteCXXBaseSpecifier(const CXXBaseSpecifier *Base) {
626 Record.push_back(Base->isVirtual());
627 Record.push_back(Base->isBaseOfClass());
628 Record.push_back(Base->getAccessSpecifierAsWritten());
629 Writer.AddTypeRef(Base->getType(), Record);
630 Writer.AddSourceRange(Base->getSourceRange(), Record);
631}
632
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000633void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000634 VisitRecordDecl(D);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000635
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000636 if (D->isFirstDeclaration()) {
637 Record.push_back(D->DefinitionData != 0);
638 if (D->DefinitionData) {
639 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
640
641 Record.push_back(Data.UserDeclaredConstructor);
642 Record.push_back(Data.UserDeclaredCopyConstructor);
643 Record.push_back(Data.UserDeclaredCopyAssignment);
644 Record.push_back(Data.UserDeclaredDestructor);
645 Record.push_back(Data.Aggregate);
646 Record.push_back(Data.PlainOldData);
647 Record.push_back(Data.Empty);
648 Record.push_back(Data.Polymorphic);
649 Record.push_back(Data.Abstract);
650 Record.push_back(Data.HasTrivialConstructor);
651 Record.push_back(Data.HasTrivialCopyConstructor);
652 Record.push_back(Data.HasTrivialCopyAssignment);
653 Record.push_back(Data.HasTrivialDestructor);
654 Record.push_back(Data.ComputedVisibleConversions);
655
656 Record.push_back(D->getNumBases());
657 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
658 E = D->bases_end(); I != E; ++I)
659 WriteCXXBaseSpecifier(&*I);
660
661 // FIXME: Make VBases lazily computed when needed to avoid storing them.
662 Record.push_back(D->getNumVBases());
663 for (CXXRecordDecl::base_class_iterator I = D->vbases_begin(),
664 E = D->vbases_end(); I != E; ++I)
665 WriteCXXBaseSpecifier(&*I);
666
667 Writer.AddUnresolvedSet(Data.Conversions, Record);
668 Writer.AddUnresolvedSet(Data.VisibleConversions, Record);
669 Writer.AddDeclRef(Data.Definition, Record);
670 Writer.AddDeclRef(Data.FirstFriend, Record);
671 }
672 }
673
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000674 enum {
675 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
676 };
677 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
678 Record.push_back(CXXRecTemplate);
679 Writer.AddDeclRef(TemplD, Record);
680 } else if (MemberSpecializationInfo *MSInfo
681 = D->getMemberSpecializationInfo()) {
682 Record.push_back(CXXRecMemberSpecialization);
683 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
684 Record.push_back(MSInfo->getTemplateSpecializationKind());
685 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
686 } else {
687 Record.push_back(CXXRecNotTemplate);
688 }
689
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000690 Code = pch::DECL_CXX_RECORD;
691}
692
693void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
694 // assert(false && "cannot write CXXMethodDecl");
695 VisitFunctionDecl(D);
696 Code = pch::DECL_CXX_METHOD;
697}
698
699void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
700 // assert(false && "cannot write CXXConstructorDecl");
701 VisitCXXMethodDecl(D);
702 Code = pch::DECL_CXX_CONSTRUCTOR;
703}
704
705void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
706 // assert(false && "cannot write CXXDestructorDecl");
707 VisitCXXMethodDecl(D);
708 Code = pch::DECL_CXX_DESTRUCTOR;
709}
710
711void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
712 // assert(false && "cannot write CXXConversionDecl");
713 VisitCXXMethodDecl(D);
714 Code = pch::DECL_CXX_CONVERSION;
715}
716
Abramo Bagnara6206d532010-06-05 05:09:32 +0000717void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
718 VisitDecl(D);
719 Writer.AddSourceLocation(D->getColonLoc(), Record);
720 Code = pch::DECL_ACCESS_SPEC;
721}
722
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000723void PCHDeclWriter::VisitFriendDecl(FriendDecl *D) {
724 Record.push_back(D->Friend.is<TypeSourceInfo*>());
725 if (D->Friend.is<TypeSourceInfo*>())
726 Writer.AddTypeSourceInfo(D->Friend.get<TypeSourceInfo*>(), Record);
727 else
728 Writer.AddDeclRef(D->Friend.get<NamedDecl*>(), Record);
729 Writer.AddDeclRef(D->NextFriend, Record);
730 Writer.AddSourceLocation(D->FriendLoc, Record);
731 Code = pch::DECL_FRIEND;
732}
733
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000734void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
735 assert(false && "cannot write FriendTemplateDecl");
736}
737
738void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000739 VisitNamedDecl(D);
740
741 Writer.AddDeclRef(D->getTemplatedDecl(), Record);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000742 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000743}
744
745void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000746 VisitTemplateDecl(D);
747
748 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
749 if (D->getPreviousDeclaration() == 0) {
750 // This ClassTemplateDecl owns the CommonPtr; write it.
751
752 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy;
753 CTSDSetTy &CTSDSet = D->getSpecializations();
754 Record.push_back(CTSDSet.size());
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000755 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) {
756 ClassTemplateSpecializationDecl *CTSD = &*I;
757 Writer.AddDeclRef(CTSD, Record);
758 // Write the argument list here because we may get it uninitialized when
759 // reading it back.
760 Writer.AddTemplateArgumentList(&CTSD->getTemplateArgs(), Record);
761 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000762
763 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy;
764 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
765 Record.push_back(CTPSDSet.size());
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000766 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) {
767 ClassTemplatePartialSpecializationDecl *CTPSD = &*I;
768 Writer.AddDeclRef(CTPSD, Record);
769 // Write the argument list here because we may get it uninitialized when
770 // reading it back.
771 Writer.AddTemplateArgumentList(&CTPSD->getTemplateArgs(), Record);
772 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000773
774 // InjectedClassNameType is computed, no need to write it.
775
776 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
777 if (D->getInstantiatedFromMemberTemplate())
778 Record.push_back(D->isMemberSpecialization());
779 }
780 Code = pch::DECL_CLASS_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000781}
782
783void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
784 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000785 VisitCXXRecordDecl(D);
786
787 llvm::PointerUnion<ClassTemplateDecl *,
788 ClassTemplatePartialSpecializationDecl *> InstFrom
789 = D->getSpecializedTemplateOrPartial();
790 if (InstFrom.is<ClassTemplateDecl *>()) {
791 Writer.AddDeclRef(InstFrom.get<ClassTemplateDecl *>(), Record);
792 } else {
793 Writer.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>(),
794 Record);
795 Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
796 }
797
798 // Explicit info.
799 Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
800 if (D->getTypeAsWritten()) {
801 Writer.AddSourceLocation(D->getExternLoc(), Record);
802 Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
803 }
804
805 Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
806 Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
807 Record.push_back(D->getSpecializationKind());
808
809 Code = pch::DECL_CLASS_TEMPLATE_SPECIALIZATION;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000810}
811
812void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
813 ClassTemplatePartialSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000814 VisitClassTemplateSpecializationDecl(D);
815
816 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
817
818 Record.push_back(D->getNumTemplateArgsAsWritten());
819 for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i)
820 Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record);
821
822 Record.push_back(D->getSequenceNumber());
823
824 // These are read/set from/to the first declaration.
825 if (D->getPreviousDeclaration() == 0) {
826 Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
827 Record.push_back(D->isMemberSpecialization());
828 }
829
830 Code = pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000831}
832
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000833void PCHDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
834 VisitTemplateDecl(D);
835
836 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
837 if (D->getPreviousDeclaration() == 0) {
838 // This FunctionTemplateDecl owns the CommonPtr; write it.
839
840 // FunctionTemplateSpecializationInfos are filled through the
841 // templated FunctionDecl's setFunctionTemplateSpecialization, no need to
842 // write them here.
843
844 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
845 if (D->getInstantiatedFromMemberTemplate())
846 Record.push_back(D->isMemberSpecialization());
847 }
848 Code = pch::DECL_FUNCTION_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000849}
850
851void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000852 VisitTypeDecl(D);
853
854 Record.push_back(D->wasDeclaredWithTypename());
855 Record.push_back(D->isParameterPack());
856 Record.push_back(D->defaultArgumentWasInherited());
857 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
858
859 Code = pch::DECL_TEMPLATE_TYPE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000860}
861
862void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisb24e1992010-06-25 16:25:09 +0000863 VisitVarDecl(D);
864 // TemplateParmPosition.
865 Record.push_back(D->getDepth());
866 Record.push_back(D->getPosition());
867 // Rest of NonTypeTemplateParmDecl.
868 Record.push_back(D->getDefaultArgument() != 0);
869 if (D->getDefaultArgument()) {
870 Writer.AddStmt(D->getDefaultArgument());
871 Record.push_back(D->defaultArgumentWasInherited());
872 }
873 Code = pch::DECL_NON_TYPE_TEMPLATE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000874}
875
876void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
877 assert(false && "cannot write TemplateTemplateParmDecl");
878}
879
880void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
881 assert(false && "cannot write StaticAssertDecl");
882}
883
Chris Lattner12b1c762009-04-27 06:16:06 +0000884/// \brief Emit the DeclContext part of a declaration context decl.
885///
886/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
887/// block for this declaration context is stored. May be 0 to indicate
888/// that there are no declarations stored within this context.
889///
890/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
891/// block for this declaration context is stored. May be 0 to indicate
892/// that there are no declarations visible from this context. Note
893/// that this value will not be emitted for non-primary declaration
894/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000895void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000896 uint64_t VisibleOffset) {
897 Record.push_back(LexicalOffset);
898 Record.push_back(VisibleOffset);
899}
900
901
902//===----------------------------------------------------------------------===//
903// PCHWriter Implementation
904//===----------------------------------------------------------------------===//
905
Chris Lattnerea5ce472009-04-27 07:35:58 +0000906void PCHWriter::WriteDeclsBlockAbbrevs() {
907 using namespace llvm;
908 // Abbreviation for DECL_PARM_VAR.
909 BitCodeAbbrev *Abv = new BitCodeAbbrev();
910 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
911
912 // Decl
913 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
914 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
915 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
916 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
917 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
918 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000919 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000920 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000921 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Chris Lattnerea5ce472009-04-27 07:35:58 +0000923 // NamedDecl
924 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
925 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
926 // ValueDecl
927 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000928 // DeclaratorDecl
929 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000930 // VarDecl
931 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +0000932 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +0000933 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
934 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
935 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +0000936 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +0000937 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +0000938 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000939 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
940 // ParmVarDecl
941 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +0000942 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +0000943
Chris Lattnerea5ce472009-04-27 07:35:58 +0000944 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
945}
946
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000947/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
948/// consumers of the AST.
949///
950/// Such decls will always be deserialized from the PCH file, so we would like
951/// this to be as restrictive as possible. Currently the predicate is driven by
952/// code generation requirements, if other clients have a different notion of
953/// what is "required" then we may have to consider an alternate scheme where
954/// clients can iterate over the top-level decls and get information on them,
955/// without necessary deserializing them. We could explicitly require such
956/// clients to use a separate API call to "realize" the decl. This should be
957/// relatively painless since they would presumably only do it for top-level
958/// decls.
959//
960// FIXME: This predicate is essentially IRgen's predicate to determine whether a
961// declaration can be deferred. Merge them somehow.
962static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
963 // File scoped assembly must be seen.
964 if (isa<FileScopeAsmDecl>(D))
965 return true;
966
967 // Otherwise if this isn't a function or a file scoped variable it doesn't
968 // need to be seen.
969 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
970 if (!VD->isFileVarDecl())
971 return false;
972 } else if (!isa<FunctionDecl>(D))
973 return false;
974
975 // Aliases and used decls must be seen.
976 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
977 return true;
978
979 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
980 // Forward declarations don't need to be seen.
981 if (!FD->isThisDeclarationADefinition())
982 return false;
983
984 // Constructors and destructors must be seen.
985 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
986 return true;
987
988 // Otherwise, this is required unless it is static.
989 //
990 // FIXME: Inlines.
991 return FD->getStorageClass() != FunctionDecl::Static;
992 } else {
993 const VarDecl *VD = cast<VarDecl>(D);
994
995 // In C++, this doesn't need to be seen if it is marked "extern".
996 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
997 (VD->getStorageClass() == VarDecl::Extern ||
998 VD->isExternC()))
999 return false;
1000
1001 // In C, this doesn't need to be seen unless it is a definition.
1002 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
1003 return false;
1004
1005 // Otherwise, this is required unless it is static.
1006 return VD->getStorageClass() != VarDecl::Static;
1007 }
1008}
1009
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001010void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +00001011 RecordData Record;
1012 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +00001013
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001014 // If this declaration is also a DeclContext, write blocks for the
1015 // declarations that lexically stored inside its context and those
1016 // declarations that are visible from its context. These blocks
1017 // are written before the declaration itself so that we can put
1018 // their offsets into the record for the declaration.
1019 uint64_t LexicalOffset = 0;
1020 uint64_t VisibleOffset = 0;
1021 DeclContext *DC = dyn_cast<DeclContext>(D);
1022 if (DC) {
1023 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1024 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +00001025 }
1026
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001027 // Determine the ID for this declaration
1028 pch::DeclID &ID = DeclIDs[D];
1029 if (ID == 0)
1030 ID = DeclIDs.size();
1031
1032 unsigned Index = ID - 1;
1033
1034 // Record the offset for this declaration
1035 if (DeclOffsets.size() == Index)
1036 DeclOffsets.push_back(Stream.GetCurrentBitNo());
1037 else if (DeclOffsets.size() < Index) {
1038 DeclOffsets.resize(Index+1);
1039 DeclOffsets[Index] = Stream.GetCurrentBitNo();
1040 }
1041
1042 // Build and emit a record for this declaration
1043 Record.clear();
1044 W.Code = (pch::DeclCode)0;
1045 W.AbbrevToUse = 0;
1046 W.Visit(D);
1047 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
1048
Daniel Dunbar33671982009-12-03 09:13:36 +00001049 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +00001050 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +00001051 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001052 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
1053
1054 // If the declaration had any attributes, write them now.
1055 if (D->hasAttrs())
1056 WriteAttributeRecord(D->getAttrs());
1057
1058 // Flush any expressions that were written as part of this declaration.
1059 FlushStmts();
1060
1061 // Note "external" declarations so that we can add them to a record in the
1062 // PCH file later.
1063 //
1064 // FIXME: This should be renamed, the predicate is much more complicated.
1065 if (isRequiredDecl(D, Context))
1066 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +00001067}