blob: 06266d2282d5158e40afb4a28a06ed026983d9f8 [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 }
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +000043
44 void Visit(Decl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000045
46 void VisitDecl(Decl *D);
47 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
48 void VisitNamedDecl(NamedDecl *D);
Douglas Gregor0cef4832010-02-21 18:22:14 +000049 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000050 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
51 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000052 void VisitTypeDecl(TypeDecl *D);
53 void VisitTypedefDecl(TypedefDecl *D);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000054 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000055 void VisitTagDecl(TagDecl *D);
56 void VisitEnumDecl(EnumDecl *D);
57 void VisitRecordDecl(RecordDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000058 void VisitCXXRecordDecl(CXXRecordDecl *D);
59 void VisitClassTemplateSpecializationDecl(
60 ClassTemplateSpecializationDecl *D);
61 void VisitClassTemplatePartialSpecializationDecl(
62 ClassTemplatePartialSpecializationDecl *D);
63 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000064 void VisitValueDecl(ValueDecl *D);
65 void VisitEnumConstantDecl(EnumConstantDecl *D);
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +000066 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +000067 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000068 void VisitFunctionDecl(FunctionDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000069 void VisitCXXMethodDecl(CXXMethodDecl *D);
70 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
71 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
72 void VisitCXXConversionDecl(CXXConversionDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000073 void VisitFieldDecl(FieldDecl *D);
74 void VisitVarDecl(VarDecl *D);
75 void VisitImplicitParamDecl(ImplicitParamDecl *D);
76 void VisitParmVarDecl(ParmVarDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000077 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
78 void VisitTemplateDecl(TemplateDecl *D);
79 void VisitClassTemplateDecl(ClassTemplateDecl *D);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +000080 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000081 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +000082 void VisitUsingDecl(UsingDecl *D);
83 void VisitUsingShadowDecl(UsingShadowDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000084 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000085 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
Abramo Bagnara6206d532010-06-05 05:09:32 +000086 void VisitAccessSpecDecl(AccessSpecDecl *D);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +000087 void VisitFriendDecl(FriendDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000088 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
89 void VisitStaticAssertDecl(StaticAssertDecl *D);
Chris Lattner12b1c762009-04-27 06:16:06 +000090 void VisitBlockDecl(BlockDecl *D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000091
Mike Stump1eb44332009-09-09 15:08:12 +000092 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000093 uint64_t VisibleOffset);
Chris Lattner6ad9ac02010-05-07 21:43:38 +000094
95
Sean Hunt9a555912010-05-30 07:21:58 +000096 // FIXME: Put in the same order is DeclNodes.td?
Chris Lattner12b1c762009-04-27 06:16:06 +000097 void VisitObjCMethodDecl(ObjCMethodDecl *D);
98 void VisitObjCContainerDecl(ObjCContainerDecl *D);
99 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
100 void VisitObjCIvarDecl(ObjCIvarDecl *D);
101 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
102 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
103 void VisitObjCClassDecl(ObjCClassDecl *D);
104 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
105 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
106 void VisitObjCImplDecl(ObjCImplDecl *D);
107 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
108 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
109 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
110 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
111 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
112 };
113}
114
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000115void PCHDeclWriter::Visit(Decl *D) {
116 DeclVisitor<PCHDeclWriter>::Visit(D);
117
118 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
119 // have been written. We want it last because we will not read it back when
120 // retrieving it from the PCH, we'll just lazily set the offset.
121 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
122 Record.push_back(FD->isThisDeclarationADefinition());
123 if (FD->isThisDeclarationADefinition())
124 Writer.AddStmt(FD->getBody());
125 }
126}
127
Chris Lattner12b1c762009-04-27 06:16:06 +0000128void PCHDeclWriter::VisitDecl(Decl *D) {
129 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
130 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
131 Writer.AddSourceLocation(D->getLocation(), Record);
132 Record.push_back(D->isInvalidDecl());
133 Record.push_back(D->hasAttrs());
134 Record.push_back(D->isImplicit());
Douglas Gregorc070cc62010-06-17 23:14:26 +0000135 Record.push_back(D->isUsed(false));
Chris Lattner12b1c762009-04-27 06:16:06 +0000136 Record.push_back(D->getAccess());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000137 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +0000138}
139
140void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
141 VisitDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000142 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000143 Code = pch::DECL_TRANSLATION_UNIT;
144}
145
146void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
147 VisitDecl(D);
148 Writer.AddDeclarationName(D->getDeclName(), Record);
149}
150
151void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
152 VisitNamedDecl(D);
Argyrios Kyrtzidisd8a0c6f2010-07-02 11:55:01 +0000153 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000154}
155
156void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
157 VisitTypeDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000158 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000159 Code = pch::DECL_TYPEDEF;
160}
161
162void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
163 VisitTypeDecl(D);
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000164 Record.push_back(D->getIdentifierNamespace());
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000165 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000166 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
167 Record.push_back(D->isDefinition());
Douglas Gregorb37b6482010-02-12 17:40:34 +0000168 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000169 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000170 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCallb6217662010-03-15 10:12:16 +0000171 // FIXME: maybe write optional qualifier and its range.
172 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000173}
174
175void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
176 VisitTagDecl(D);
177 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall842aef82009-12-09 09:09:27 +0000178 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall1b5a6182010-05-06 08:49:23 +0000179 Record.push_back(D->getNumPositiveBits());
180 Record.push_back(D->getNumNegativeBits());
Argyrios Kyrtzidis74228272010-07-06 15:36:48 +0000181 Writer.AddDeclRef(D->getInstantiatedFromMemberEnum(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000182 Code = pch::DECL_ENUM;
183}
184
185void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
186 VisitTagDecl(D);
187 Record.push_back(D->hasFlexibleArrayMember());
188 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000189 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000190 Code = pch::DECL_RECORD;
191}
192
193void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
194 VisitNamedDecl(D);
195 Writer.AddTypeRef(D->getType(), Record);
196}
197
198void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
199 VisitValueDecl(D);
200 Record.push_back(D->getInitExpr()? 1 : 0);
201 if (D->getInitExpr())
202 Writer.AddStmt(D->getInitExpr());
203 Writer.AddAPSInt(D->getInitVal(), Record);
204 Code = pch::DECL_ENUM_CONSTANT;
205}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000206
207void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
208 VisitValueDecl(D);
John McCalla93c9342009-12-07 02:54:59 +0000209 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCallb6217662010-03-15 10:12:16 +0000210 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000211}
Chris Lattner12b1c762009-04-27 06:16:06 +0000212
213void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000214 VisitDeclaratorDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000215
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000216 Record.push_back(D->getIdentifierNamespace());
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000217 Record.push_back(D->getTemplatedKind());
218 switch (D->getTemplatedKind()) {
Argyrios Kyrtzidisdc767e32010-06-28 09:31:34 +0000219 default: assert(false && "Unhandled TemplatedKind!");
220 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000221 case FunctionDecl::TK_NonTemplate:
222 break;
223 case FunctionDecl::TK_FunctionTemplate:
224 Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
225 break;
226 case FunctionDecl::TK_MemberSpecialization: {
227 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
228 Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
229 Record.push_back(MemberInfo->getTemplateSpecializationKind());
230 Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
231 break;
232 }
233 case FunctionDecl::TK_FunctionTemplateSpecialization: {
234 FunctionTemplateSpecializationInfo *
235 FTSInfo = D->getTemplateSpecializationInfo();
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000236 // We want it canonical to guarantee that it has a Common*.
237 Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000238 Record.push_back(FTSInfo->getTemplateSpecializationKind());
239
240 // Template arguments.
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000241 Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000242
243 // Template args as written.
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000244 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000245 if (FTSInfo->TemplateArgumentsAsWritten) {
246 Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size());
247 for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i)
248 Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
249 Record);
250 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(),
251 Record);
252 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(),
253 Record);
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000254 }
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +0000255
256 Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record);
Argyrios Kyrtzidisa56b0492010-06-25 16:24:51 +0000257 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000258 }
259 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
260 DependentFunctionTemplateSpecializationInfo *
261 DFTSInfo = D->getDependentSpecializationInfo();
262
263 // Templates.
264 Record.push_back(DFTSInfo->getNumTemplates());
265 for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
266 Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
267
268 // Templates args.
269 Record.push_back(DFTSInfo->getNumTemplateArgs());
270 for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
271 Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
Argyrios Kyrtzidis7b081c82010-07-05 10:37:55 +0000272 Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record);
273 Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record);
Argyrios Kyrtzidisa56b0492010-06-25 16:24:51 +0000274 break;
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000275 }
276 }
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000277
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000278 // FunctionDecl's body is handled last at PCHWriterDecl::Visit,
279 // after everything else is written.
Argyrios Kyrtzidis5efb06f2010-07-02 11:55:40 +0000280
281 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
282 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
283 Record.push_back(D->getStorageClassAsWritten());
284 Record.push_back(D->isInlineSpecified());
285 Record.push_back(D->isVirtualAsWritten());
286 Record.push_back(D->isPure());
287 Record.push_back(D->hasInheritedPrototype());
288 Record.push_back(D->hasWrittenPrototype());
289 Record.push_back(D->isDeleted());
290 Record.push_back(D->isTrivial());
291 Record.push_back(D->isCopyAssignment());
292 Record.push_back(D->hasImplicitReturnZero());
293 Writer.AddSourceLocation(D->getLocEnd(), Record);
294
Chris Lattner12b1c762009-04-27 06:16:06 +0000295 Record.push_back(D->param_size());
296 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
297 P != PEnd; ++P)
298 Writer.AddDeclRef(*P, Record);
299 Code = pch::DECL_FUNCTION;
300}
301
302void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
303 VisitNamedDecl(D);
304 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000305 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000306 Record.push_back(D->getBody() != 0);
307 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000308 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000309 Writer.AddDeclRef(D->getSelfDecl(), Record);
310 Writer.AddDeclRef(D->getCmdDecl(), Record);
311 }
312 Record.push_back(D->isInstanceMethod());
313 Record.push_back(D->isVariadic());
314 Record.push_back(D->isSynthesized());
Fariborz Jahanian3fe10412010-07-22 18:24:20 +0000315 Record.push_back(D->isDefined());
Chris Lattner12b1c762009-04-27 06:16:06 +0000316 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000317 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000318 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000319 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahanian7732cc92010-04-08 21:29:11 +0000320 Record.push_back(D->getNumSelectorArgs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000321 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor4bc1cb62010-03-08 14:59:44 +0000322 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000323 Writer.AddSourceLocation(D->getLocEnd(), Record);
324 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000325 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000326 PEnd = D->param_end(); P != PEnd; ++P)
327 Writer.AddDeclRef(*P, Record);
328 Code = pch::DECL_OBJC_METHOD;
329}
330
331void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
332 VisitNamedDecl(D);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000333 Writer.AddSourceRange(D->getAtEndRange(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000334 // Abstract class (no need to define a stable pch::DECL code).
335}
336
337void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
338 VisitObjCContainerDecl(D);
339 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
340 Writer.AddDeclRef(D->getSuperClass(), Record);
341 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000342 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000343 PEnd = D->protocol_end();
344 P != PEnd; ++P)
345 Writer.AddDeclRef(*P, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000346 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
347 PLEnd = D->protocol_loc_end();
348 PL != PLEnd; ++PL)
349 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000350 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000351 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000352 IEnd = D->ivar_end(); I != IEnd; ++I)
353 Writer.AddDeclRef(*I, Record);
354 Writer.AddDeclRef(D->getCategoryList(), Record);
355 Record.push_back(D->isForwardDecl());
356 Record.push_back(D->isImplicitInterfaceDecl());
357 Writer.AddSourceLocation(D->getClassLoc(), Record);
358 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
359 Writer.AddSourceLocation(D->getLocEnd(), Record);
360 Code = pch::DECL_OBJC_INTERFACE;
361}
362
363void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
364 VisitFieldDecl(D);
365 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000366 Record.push_back(D->getAccessControl());
Fariborz Jahanianac0021b2010-07-17 18:35:47 +0000367 Record.push_back(D->getSynthesize());
Chris Lattner12b1c762009-04-27 06:16:06 +0000368 Code = pch::DECL_OBJC_IVAR;
369}
370
371void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
372 VisitObjCContainerDecl(D);
373 Record.push_back(D->isForwardDecl());
374 Writer.AddSourceLocation(D->getLocEnd(), Record);
375 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000376 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000377 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
378 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000379 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
380 PLEnd = D->protocol_loc_end();
381 PL != PLEnd; ++PL)
382 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000383 Code = pch::DECL_OBJC_PROTOCOL;
384}
385
386void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
387 VisitFieldDecl(D);
388 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
389}
390
391void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
392 VisitDecl(D);
393 Record.push_back(D->size());
394 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek321c22f2009-11-18 00:28:11 +0000395 Writer.AddDeclRef(I->getInterface(), Record);
396 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
397 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000398 Code = pch::DECL_OBJC_CLASS;
399}
400
401void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
402 VisitDecl(D);
403 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000404 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000405 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
406 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000407 for (ObjCForwardProtocolDecl::protocol_loc_iterator
408 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
409 PL != PLEnd; ++PL)
410 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000411 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
412}
413
414void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
415 VisitObjCContainerDecl(D);
416 Writer.AddDeclRef(D->getClassInterface(), Record);
417 Record.push_back(D->protocol_size());
Douglas Gregor18df52b2010-01-16 15:02:53 +0000418 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000419 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
420 Writer.AddDeclRef(*I, Record);
Douglas Gregor18df52b2010-01-16 15:02:53 +0000421 for (ObjCCategoryDecl::protocol_loc_iterator
422 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
423 PL != PLEnd; ++PL)
424 Writer.AddSourceLocation(*PL, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000425 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor3db211b2010-01-16 16:38:58 +0000426 Writer.AddSourceLocation(D->getAtLoc(), Record);
427 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000428 Code = pch::DECL_OBJC_CATEGORY;
429}
430
431void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
432 VisitNamedDecl(D);
433 Writer.AddDeclRef(D->getClassInterface(), Record);
434 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
435}
436
437void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
438 VisitNamedDecl(D);
Fariborz Jahaniand0502402010-01-21 17:36:00 +0000439 Writer.AddSourceLocation(D->getAtLoc(), Record);
John McCall83a230c2010-06-04 20:50:08 +0000440 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000441 // FIXME: stable encoding
442 Record.push_back((unsigned)D->getPropertyAttributes());
Fariborz Jahanian80aa1cd2010-06-22 23:20:40 +0000443 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000444 // FIXME: stable encoding
445 Record.push_back((unsigned)D->getPropertyImplementation());
446 Writer.AddDeclarationName(D->getGetterName(), Record);
447 Writer.AddDeclarationName(D->getSetterName(), Record);
448 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
449 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
450 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
451 Code = pch::DECL_OBJC_PROPERTY;
452}
453
454void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000455 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000456 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000457 // Abstract class (no need to define a stable pch::DECL code).
458}
459
460void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
461 VisitObjCImplDecl(D);
462 Writer.AddIdentifierRef(D->getIdentifier(), Record);
463 Code = pch::DECL_OBJC_CATEGORY_IMPL;
464}
465
466void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
467 VisitObjCImplDecl(D);
468 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahaniane4498c62010-04-28 16:11:27 +0000469 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner12b1c762009-04-27 06:16:06 +0000470 Code = pch::DECL_OBJC_IMPLEMENTATION;
471}
472
473void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
474 VisitDecl(D);
475 Writer.AddSourceLocation(D->getLocStart(), Record);
476 Writer.AddDeclRef(D->getPropertyDecl(), Record);
477 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian17cb3262010-05-05 21:52:17 +0000478 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner12b1c762009-04-27 06:16:06 +0000479 Code = pch::DECL_OBJC_PROPERTY_IMPL;
480}
481
482void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000483 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000484 Record.push_back(D->isMutable());
485 Record.push_back(D->getBitWidth()? 1 : 0);
486 if (D->getBitWidth())
487 Writer.AddStmt(D->getBitWidth());
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000488 if (!D->getDeclName())
489 Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000490 Code = pch::DECL_FIELD;
491}
492
493void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000494 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000495 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregor16573fa2010-04-19 22:54:31 +0000496 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000497 Record.push_back(D->isThreadSpecified());
498 Record.push_back(D->hasCXXDirectInitializer());
499 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor324b54d2010-05-03 18:51:14 +0000500 Record.push_back(D->isExceptionVariable());
Douglas Gregor5077c382010-05-15 06:01:05 +0000501 Record.push_back(D->isNRVOVariable());
Chris Lattner12b1c762009-04-27 06:16:06 +0000502 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner030854b2010-05-09 06:40:08 +0000503 Record.push_back(D->getInit() ? 1 : 0);
Chris Lattner12b1c762009-04-27 06:16:06 +0000504 if (D->getInit())
505 Writer.AddStmt(D->getInit());
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000506
507 MemberSpecializationInfo *SpecInfo
508 = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0;
509 Record.push_back(SpecInfo != 0);
510 if (SpecInfo) {
511 Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record);
512 Record.push_back(SpecInfo->getTemplateSpecializationKind());
513 Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record);
514 }
515
Chris Lattner12b1c762009-04-27 06:16:06 +0000516 Code = pch::DECL_VAR;
517}
518
519void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
520 VisitVarDecl(D);
521 Code = pch::DECL_IMPLICIT_PARAM;
522}
523
524void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
525 VisitVarDecl(D);
526 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallbf73b352010-03-12 18:31:32 +0000527 Record.push_back(D->hasInheritedDefaultArg());
Argyrios Kyrtzidis691d77f2010-07-04 21:44:07 +0000528 Record.push_back(D->hasUninstantiatedDefaultArg());
529 if (D->hasUninstantiatedDefaultArg())
530 Writer.AddStmt(D->getUninstantiatedDefaultArg());
Chris Lattner12b1c762009-04-27 06:16:06 +0000531 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattnerea5ce472009-04-27 07:35:58 +0000533 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
534 // we dynamically check for the properties that we optimize for, but don't
535 // know are true of all PARM_VAR_DECLs.
John McCalla93c9342009-12-07 02:54:59 +0000536 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000537 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000538 !D->isImplicit() &&
Douglas Gregorc070cc62010-06-17 23:14:26 +0000539 !D->isUsed(false) &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000540 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000541 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000542 D->getStorageClass() == 0 &&
543 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallbf73b352010-03-12 18:31:32 +0000544 D->getObjCDeclQualifier() == 0 &&
Chris Lattner030854b2010-05-09 06:40:08 +0000545 !D->hasInheritedDefaultArg() &&
Argyrios Kyrtzidis691d77f2010-07-04 21:44:07 +0000546 D->getInit() == 0 &&
547 !D->hasUninstantiatedDefaultArg()) // No default expr.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000548 AbbrevToUse = Writer.getParmVarDeclAbbrev();
549
550 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
551 // just us assuming it.
552 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
553 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
554 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
555 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor324b54d2010-05-03 18:51:14 +0000556 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattnerea5ce472009-04-27 07:35:58 +0000557 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +0000558 assert(!D->isStaticDataMember() &&
559 "PARM_VAR_DECL can't be static data member");
Chris Lattner12b1c762009-04-27 06:16:06 +0000560}
561
Chris Lattner12b1c762009-04-27 06:16:06 +0000562void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
563 VisitDecl(D);
564 Writer.AddStmt(D->getAsmString());
565 Code = pch::DECL_FILE_SCOPE_ASM;
566}
567
568void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
569 VisitDecl(D);
570 Writer.AddStmt(D->getBody());
John McCall82dc0092010-06-04 11:21:44 +0000571 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000572 Record.push_back(D->param_size());
573 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
574 P != PEnd; ++P)
575 Writer.AddDeclRef(*P, Record);
576 Code = pch::DECL_BLOCK;
577}
578
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000579void PCHDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
580 VisitDecl(D);
581 // FIXME: It might be nice to serialize the brace locations for this
582 // declaration, which don't seem to be readily available in the AST.
583 Record.push_back(D->getLanguage());
584 Record.push_back(D->hasBraces());
585 Code = pch::DECL_LINKAGE_SPEC;
586}
587
588void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
589 VisitNamedDecl(D);
590 Writer.AddSourceLocation(D->getLBracLoc(), Record);
591 Writer.AddSourceLocation(D->getRBracLoc(), Record);
592 Writer.AddDeclRef(D->getNextNamespace(), Record);
593
594 // Only write one reference--original or anonymous
595 Record.push_back(D->isOriginalNamespace());
596 if (D->isOriginalNamespace())
597 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
598 else
599 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
600 Code = pch::DECL_NAMESPACE;
601}
602
603void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
604 VisitNamedDecl(D);
605 Writer.AddSourceLocation(D->getAliasLoc(), Record);
606 Writer.AddSourceRange(D->getQualifierRange(), Record);
607 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
608 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
609 Writer.AddDeclRef(D->getNamespace(), Record);
610 Code = pch::DECL_NAMESPACE_ALIAS;
611}
612
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000613void PCHDeclWriter::VisitUsingDecl(UsingDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000614 VisitNamedDecl(D);
615 Writer.AddSourceRange(D->getNestedNameRange(), Record);
616 Writer.AddSourceLocation(D->getUsingLocation(), Record);
617 Writer.AddNestedNameSpecifier(D->getTargetNestedNameDecl(), Record);
618 Record.push_back(D->getNumShadowDecls());
619 for (UsingDecl::shadow_iterator P = D->shadow_begin(),
620 PEnd = D->shadow_end(); P != PEnd; ++P)
621 Writer.AddDeclRef(*P, Record);
622 Record.push_back(D->isTypeName());
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000623 Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000624 Code = pch::DECL_USING;
625}
626
Argyrios Kyrtzidisb01a5522010-06-20 14:40:59 +0000627void PCHDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000628 VisitNamedDecl(D);
629 Writer.AddDeclRef(D->getTargetDecl(), Record);
630 Writer.AddDeclRef(D->getUsingDecl(), Record);
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000631 Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000632 Code = pch::DECL_USING_SHADOW;
633}
634
635void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
636 VisitNamedDecl(D);
637 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
638 Writer.AddSourceRange(D->getQualifierRange(), Record);
639 Writer.AddNestedNameSpecifier(D->getQualifier(), Record);
640 Writer.AddSourceLocation(D->getIdentLocation(), Record);
641 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
642 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
643 Code = pch::DECL_USING_DIRECTIVE;
644}
645
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000646void PCHDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000647 VisitValueDecl(D);
648 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
649 Writer.AddSourceLocation(D->getUsingLoc(), Record);
650 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
651 Code = pch::DECL_UNRESOLVED_USING_VALUE;
652}
653
Argyrios Kyrtzidis8f4eae92010-06-30 08:49:30 +0000654void PCHDeclWriter::VisitUnresolvedUsingTypenameDecl(
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000655 UnresolvedUsingTypenameDecl *D) {
656 VisitTypeDecl(D);
657 Writer.AddSourceRange(D->getTargetNestedNameRange(), Record);
658 Writer.AddSourceLocation(D->getUsingLoc(), Record);
659 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
660 Writer.AddNestedNameSpecifier(D->getTargetNestedNameSpecifier(), Record);
661 Code = pch::DECL_UNRESOLVED_USING_TYPENAME;
662}
663
664void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000665 // See comments at PCHDeclReader::VisitCXXRecordDecl about why this happens
666 // before VisitRecordDecl.
667 enum { Data_NoDefData, Data_Owner, Data_NotOwner };
668 bool OwnsDefinitionData = false;
669 if (D->DefinitionData) {
670 assert(D->DefinitionData->Definition &&
671 "DefinitionData don't point to a definition decl!");
672 OwnsDefinitionData = D->DefinitionData->Definition == D;
673 if (OwnsDefinitionData) {
674 Record.push_back(Data_Owner);
675 } else {
676 Record.push_back(Data_NotOwner);
677 Writer.AddDeclRef(D->DefinitionData->Definition, Record);
678 }
679 } else
680 Record.push_back(Data_NoDefData);
681
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000682 VisitRecordDecl(D);
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000683
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000684 if (OwnsDefinitionData) {
685 assert(D->DefinitionData);
686 struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData;
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000687
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000688 Record.push_back(Data.UserDeclaredConstructor);
689 Record.push_back(Data.UserDeclaredCopyConstructor);
690 Record.push_back(Data.UserDeclaredCopyAssignment);
691 Record.push_back(Data.UserDeclaredDestructor);
692 Record.push_back(Data.Aggregate);
693 Record.push_back(Data.PlainOldData);
694 Record.push_back(Data.Empty);
695 Record.push_back(Data.Polymorphic);
696 Record.push_back(Data.Abstract);
697 Record.push_back(Data.HasTrivialConstructor);
698 Record.push_back(Data.HasTrivialCopyConstructor);
699 Record.push_back(Data.HasTrivialCopyAssignment);
700 Record.push_back(Data.HasTrivialDestructor);
701 Record.push_back(Data.ComputedVisibleConversions);
702 Record.push_back(Data.DeclaredDefaultConstructor);
703 Record.push_back(Data.DeclaredCopyConstructor);
704 Record.push_back(Data.DeclaredCopyAssignment);
705 Record.push_back(Data.DeclaredDestructor);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000706
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000707 Record.push_back(D->getNumBases());
708 for (CXXRecordDecl::base_class_iterator I = D->bases_begin(),
709 E = D->bases_end(); I != E; ++I)
710 Writer.AddCXXBaseSpecifier(*I, Record);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000711
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000712 // FIXME: Make VBases lazily computed when needed to avoid storing them.
713 Record.push_back(D->getNumVBases());
714 for (CXXRecordDecl::base_class_iterator I = D->vbases_begin(),
715 E = D->vbases_end(); I != E; ++I)
716 Writer.AddCXXBaseSpecifier(*I, Record);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000717
Argyrios Kyrtzidis0f47bb92010-07-06 15:36:58 +0000718 Writer.AddUnresolvedSet(Data.Conversions, Record);
719 Writer.AddUnresolvedSet(Data.VisibleConversions, Record);
720 // Data.Definition is written at the top.
721 Writer.AddDeclRef(Data.FirstFriend, Record);
Argyrios Kyrtzidis37ffed32010-07-02 11:55:32 +0000722 }
723
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000724 enum {
725 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
726 };
727 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
728 Record.push_back(CXXRecTemplate);
729 Writer.AddDeclRef(TemplD, Record);
730 } else if (MemberSpecializationInfo *MSInfo
731 = D->getMemberSpecializationInfo()) {
732 Record.push_back(CXXRecMemberSpecialization);
733 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
734 Record.push_back(MSInfo->getTemplateSpecializationKind());
735 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
736 } else {
737 Record.push_back(CXXRecNotTemplate);
738 }
739
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000740 Code = pch::DECL_CXX_RECORD;
741}
742
743void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000744 VisitFunctionDecl(D);
Argyrios Kyrtzidisc91e9f42010-07-04 21:44:35 +0000745 Record.push_back(D->size_overridden_methods());
746 for (CXXMethodDecl::method_iterator
747 I = D->begin_overridden_methods(), E = D->end_overridden_methods();
748 I != E; ++I)
749 Writer.AddDeclRef(*I, Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000750 Code = pch::DECL_CXX_METHOD;
751}
752
753void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000754 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000755
756 Record.push_back(D->IsExplicitSpecified);
757 Record.push_back(D->ImplicitlyDefined);
758
759 Record.push_back(D->NumBaseOrMemberInitializers);
760 for (unsigned i=0; i != D->NumBaseOrMemberInitializers; ++i) {
761 CXXBaseOrMemberInitializer *Init = D->BaseOrMemberInitializers[i];
762
763 Record.push_back(Init->isBaseInitializer());
764 if (Init->isBaseInitializer()) {
765 Writer.AddTypeSourceInfo(Init->getBaseClassInfo(), Record);
766 Record.push_back(Init->isBaseVirtual());
767 } else {
768 Writer.AddDeclRef(Init->getMember(), Record);
769 }
770 Writer.AddSourceLocation(Init->getMemberLocation(), Record);
771 Writer.AddStmt(Init->getInit());
772 Writer.AddDeclRef(Init->getAnonUnionMember(), Record);
773 Writer.AddSourceLocation(Init->getLParenLoc(), Record);
774 Writer.AddSourceLocation(Init->getRParenLoc(), Record);
775 Record.push_back(Init->isWritten());
776 if (Init->isWritten()) {
777 Record.push_back(Init->getSourceOrder());
778 } else {
779 Record.push_back(Init->getNumArrayIndices());
780 for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
781 Writer.AddDeclRef(Init->getArrayIndex(i), Record);
782 }
783 }
784
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000785 Code = pch::DECL_CXX_CONSTRUCTOR;
786}
787
788void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000789 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000790
791 Record.push_back(D->ImplicitlyDefined);
792 Writer.AddDeclRef(D->OperatorDelete, Record);
793
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000794 Code = pch::DECL_CXX_DESTRUCTOR;
795}
796
797void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000798 VisitCXXMethodDecl(D);
Argyrios Kyrtzidis91468322010-07-02 15:58:43 +0000799 Record.push_back(D->IsExplicitSpecified);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000800 Code = pch::DECL_CXX_CONVERSION;
801}
802
Abramo Bagnara6206d532010-06-05 05:09:32 +0000803void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
804 VisitDecl(D);
805 Writer.AddSourceLocation(D->getColonLoc(), Record);
806 Code = pch::DECL_ACCESS_SPEC;
807}
808
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000809void PCHDeclWriter::VisitFriendDecl(FriendDecl *D) {
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000810 VisitDecl(D);
Argyrios Kyrtzidis67643342010-06-29 22:47:00 +0000811 Record.push_back(D->Friend.is<TypeSourceInfo*>());
812 if (D->Friend.is<TypeSourceInfo*>())
813 Writer.AddTypeSourceInfo(D->Friend.get<TypeSourceInfo*>(), Record);
814 else
815 Writer.AddDeclRef(D->Friend.get<NamedDecl*>(), Record);
816 Writer.AddDeclRef(D->NextFriend, Record);
817 Writer.AddSourceLocation(D->FriendLoc, Record);
818 Code = pch::DECL_FRIEND;
819}
820
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000821void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
Argyrios Kyrtzidis554e6aa2010-07-22 16:04:10 +0000822 VisitDecl(D);
823 Record.push_back(D->getNumTemplateParameters());
824 for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
825 Writer.AddTemplateParameterList(D->getTemplateParameterList(i), Record);
826 Record.push_back(D->getFriendDecl() != 0);
827 if (D->getFriendDecl())
828 Writer.AddDeclRef(D->getFriendDecl(), Record);
829 else
830 Writer.AddTypeSourceInfo(D->getFriendType(), Record);
831 Writer.AddSourceLocation(D->getFriendLoc(), Record);
832 Code = pch::DECL_FRIEND_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000833}
834
835void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000836 VisitNamedDecl(D);
837
838 Writer.AddDeclRef(D->getTemplatedDecl(), Record);
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000839 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000840}
841
842void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000843 VisitTemplateDecl(D);
844
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000845 Record.push_back(D->getIdentifierNamespace());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000846 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
847 if (D->getPreviousDeclaration() == 0) {
848 // This ClassTemplateDecl owns the CommonPtr; write it.
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000849 assert(D->isCanonicalDecl());
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000850
851 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy;
852 CTSDSetTy &CTSDSet = D->getSpecializations();
853 Record.push_back(CTSDSet.size());
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000854 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) {
Argyrios Kyrtzidisbc5ab7c2010-07-20 13:59:40 +0000855 assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000856 Writer.AddDeclRef(&*I, Record);
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000857 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000858
859 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy;
860 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
861 Record.push_back(CTPSDSet.size());
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000862 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) {
Argyrios Kyrtzidisbc5ab7c2010-07-20 13:59:40 +0000863 assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000864 Writer.AddDeclRef(&*I, Record);
Argyrios Kyrtzidis7d530482010-07-02 11:55:37 +0000865 }
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000866
867 // InjectedClassNameType is computed, no need to write it.
868
869 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
870 if (D->getInstantiatedFromMemberTemplate())
871 Record.push_back(D->isMemberSpecialization());
872 }
873 Code = pch::DECL_CLASS_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000874}
875
876void PCHDeclWriter::VisitClassTemplateSpecializationDecl(
877 ClassTemplateSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000878 VisitCXXRecordDecl(D);
879
880 llvm::PointerUnion<ClassTemplateDecl *,
881 ClassTemplatePartialSpecializationDecl *> InstFrom
882 = D->getSpecializedTemplateOrPartial();
883 if (InstFrom.is<ClassTemplateDecl *>()) {
884 Writer.AddDeclRef(InstFrom.get<ClassTemplateDecl *>(), Record);
885 } else {
886 Writer.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>(),
887 Record);
888 Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
889 }
890
891 // Explicit info.
892 Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
893 if (D->getTypeAsWritten()) {
894 Writer.AddSourceLocation(D->getExternLoc(), Record);
895 Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
896 }
897
898 Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
899 Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
900 Record.push_back(D->getSpecializationKind());
901
Argyrios Kyrtzidisbc5ab7c2010-07-20 13:59:40 +0000902 if (D->isCanonicalDecl()) {
903 // When reading, we'll add it to the folding set of the following template.
Argyrios Kyrtzidisecf966e2010-07-09 21:11:43 +0000904 Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
905 }
906
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000907 Code = pch::DECL_CLASS_TEMPLATE_SPECIALIZATION;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000908}
909
910void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl(
911 ClassTemplatePartialSpecializationDecl *D) {
Argyrios Kyrtzidisdd41c142010-06-23 13:48:30 +0000912 VisitClassTemplateSpecializationDecl(D);
913
914 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
915
916 Record.push_back(D->getNumTemplateArgsAsWritten());
917 for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i)
918 Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record);
919
920 Record.push_back(D->getSequenceNumber());
921
922 // These are read/set from/to the first declaration.
923 if (D->getPreviousDeclaration() == 0) {
924 Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
925 Record.push_back(D->isMemberSpecialization());
926 }
927
928 Code = pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000929}
930
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000931void PCHDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
932 VisitTemplateDecl(D);
933
Argyrios Kyrtzidisc8f9af22010-07-05 10:38:01 +0000934 Record.push_back(D->getIdentifierNamespace());
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000935 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
936 if (D->getPreviousDeclaration() == 0) {
937 // This FunctionTemplateDecl owns the CommonPtr; write it.
938
Argyrios Kyrtzidis057d9af2010-07-06 15:37:09 +0000939 // Write the function specialization declarations.
940 Record.push_back(D->getSpecializations().size());
941 for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator
942 I = D->getSpecializations().begin(),
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000943 E = D->getSpecializations().end() ; I != E; ++I) {
944 assert(I->Function->isCanonicalDecl() &&
945 "Expected only canonical decls in set");
Argyrios Kyrtzidis057d9af2010-07-06 15:37:09 +0000946 Writer.AddDeclRef(I->Function, Record);
Argyrios Kyrtzidis2c853e42010-07-20 13:59:58 +0000947 }
Argyrios Kyrtzidisf511ba62010-06-22 09:55:07 +0000948
949 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
950 if (D->getInstantiatedFromMemberTemplate())
951 Record.push_back(D->isMemberSpecialization());
952 }
953 Code = pch::DECL_FUNCTION_TEMPLATE;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000954}
955
956void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
Argyrios Kyrtzidis90b715e2010-06-19 19:28:53 +0000957 VisitTypeDecl(D);
958
959 Record.push_back(D->wasDeclaredWithTypename());
960 Record.push_back(D->isParameterPack());
961 Record.push_back(D->defaultArgumentWasInherited());
962 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
963
964 Code = pch::DECL_TEMPLATE_TYPE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000965}
966
967void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
Argyrios Kyrtzidisb24e1992010-06-25 16:25:09 +0000968 VisitVarDecl(D);
969 // TemplateParmPosition.
970 Record.push_back(D->getDepth());
971 Record.push_back(D->getPosition());
972 // Rest of NonTypeTemplateParmDecl.
973 Record.push_back(D->getDefaultArgument() != 0);
974 if (D->getDefaultArgument()) {
975 Writer.AddStmt(D->getDefaultArgument());
976 Record.push_back(D->defaultArgumentWasInherited());
977 }
978 Code = pch::DECL_NON_TYPE_TEMPLATE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000979}
980
981void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
Argyrios Kyrtzidisbfcc92c2010-07-08 17:12:57 +0000982 VisitTemplateDecl(D);
983 // TemplateParmPosition.
984 Record.push_back(D->getDepth());
985 Record.push_back(D->getPosition());
986 // Rest of TemplateTemplateParmDecl.
987 Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record);
988 Record.push_back(D->defaultArgumentWasInherited());
989 Code = pch::DECL_TEMPLATE_TEMPLATE_PARM;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000990}
991
992void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
Argyrios Kyrtzidis0d396892010-07-22 17:28:12 +0000993 VisitDecl(D);
994 Writer.AddStmt(D->getAssertExpr());
995 Writer.AddStmt(D->getMessage());
996 Code = pch::DECL_STATIC_ASSERT;
Chris Lattner6ad9ac02010-05-07 21:43:38 +0000997}
998
Chris Lattner12b1c762009-04-27 06:16:06 +0000999/// \brief Emit the DeclContext part of a declaration context decl.
1000///
1001/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
1002/// block for this declaration context is stored. May be 0 to indicate
1003/// that there are no declarations stored within this context.
1004///
1005/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
1006/// block for this declaration context is stored. May be 0 to indicate
1007/// that there are no declarations visible from this context. Note
1008/// that this value will not be emitted for non-primary declaration
1009/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +00001010void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +00001011 uint64_t VisibleOffset) {
1012 Record.push_back(LexicalOffset);
1013 Record.push_back(VisibleOffset);
1014}
1015
1016
1017//===----------------------------------------------------------------------===//
1018// PCHWriter Implementation
1019//===----------------------------------------------------------------------===//
1020
Chris Lattnerea5ce472009-04-27 07:35:58 +00001021void PCHWriter::WriteDeclsBlockAbbrevs() {
1022 using namespace llvm;
1023 // Abbreviation for DECL_PARM_VAR.
1024 BitCodeAbbrev *Abv = new BitCodeAbbrev();
1025 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
1026
1027 // Decl
1028 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1029 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1030 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
1031 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
1032 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
1033 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +00001034 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +00001035 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +00001036 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattnerea5ce472009-04-27 07:35:58 +00001038 // NamedDecl
1039 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
1040 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1041 // ValueDecl
1042 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +00001043 // DeclaratorDecl
1044 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +00001045 // VarDecl
1046 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregor16573fa2010-04-19 22:54:31 +00001047 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattnerea5ce472009-04-27 07:35:58 +00001048 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
1049 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
1050 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor324b54d2010-05-03 18:51:14 +00001051 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Douglas Gregor5077c382010-05-15 06:01:05 +00001052 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
Chris Lattnerea5ce472009-04-27 07:35:58 +00001053 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +00001054 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
Argyrios Kyrtzidis9421adc2010-07-04 21:44:00 +00001055 Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo
Chris Lattnerea5ce472009-04-27 07:35:58 +00001056 // ParmVarDecl
1057 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallbf73b352010-03-12 18:31:32 +00001058 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Argyrios Kyrtzidis691d77f2010-07-04 21:44:07 +00001059 Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg
Mike Stump1eb44332009-09-09 15:08:12 +00001060
Chris Lattnerea5ce472009-04-27 07:35:58 +00001061 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
Sebastian Redl681d7232010-07-27 00:17:23 +00001062
1063 Abv = new BitCodeAbbrev();
1064 Abv->Add(BitCodeAbbrevOp(pch::DECL_CONTEXT_LEXICAL));
1065 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1066 DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv);
Chris Lattnerea5ce472009-04-27 07:35:58 +00001067}
1068
Daniel Dunbare24d38f2009-09-17 03:06:51 +00001069/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
1070/// consumers of the AST.
1071///
1072/// Such decls will always be deserialized from the PCH file, so we would like
1073/// this to be as restrictive as possible. Currently the predicate is driven by
1074/// code generation requirements, if other clients have a different notion of
1075/// what is "required" then we may have to consider an alternate scheme where
1076/// clients can iterate over the top-level decls and get information on them,
1077/// without necessary deserializing them. We could explicitly require such
1078/// clients to use a separate API call to "realize" the decl. This should be
1079/// relatively painless since they would presumably only do it for top-level
1080/// decls.
Argyrios Kyrtzidisa6d6af32010-07-27 22:37:14 +00001081//
1082// FIXME: This predicate is essentially IRgen's predicate to determine whether a
1083// declaration can be deferred. Merge them somehow.
Daniel Dunbare24d38f2009-09-17 03:06:51 +00001084static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
1085 // File scoped assembly must be seen.
1086 if (isa<FileScopeAsmDecl>(D))
1087 return true;
1088
Argyrios Kyrtzidisa6d6af32010-07-27 22:37:14 +00001089 // Otherwise if this isn't a function or a file scoped variable it doesn't
1090 // need to be seen.
1091 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1092 if (!VD->isFileVarDecl())
1093 return false;
1094 } else if (!isa<FunctionDecl>(D))
1095 return false;
1096
1097 // Aliases and used decls must be seen.
1098 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
1099 return true;
1100
1101 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
1102 // Forward declarations don't need to be seen.
1103 if (!FD->isThisDeclarationADefinition())
1104 return false;
1105
1106 // Constructors and destructors must be seen.
1107 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
1108 return true;
1109
1110 // Otherwise, this is required unless it is static.
1111 //
1112 // FIXME: Inlines.
1113 return FD->getStorageClass() != FunctionDecl::Static;
1114 } else {
1115 const VarDecl *VD = cast<VarDecl>(D);
1116
1117 // Structs that have non-trivial constructors or destructors must be seen.
1118 if (const RecordType *RT = VD->getType()->getAs<RecordType>()) {
1119 if (const CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(RT->getDecl())) {
1120 if (!RD->hasTrivialConstructor() || !RD->hasTrivialDestructor())
1121 return true;
1122 }
1123 }
1124
1125 // In C++, this doesn't need to be seen if it is marked "extern".
1126 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
1127 (VD->getStorageClass() == VarDecl::Extern ||
1128 VD->isExternC()))
1129 return false;
1130
1131 // In C, this doesn't need to be seen unless it is a definition.
1132 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
1133 return false;
1134
1135 // Otherwise, this is required unless it is static.
1136 return VD->getStorageClass() != VarDecl::Static;
1137 }
Daniel Dunbare24d38f2009-09-17 03:06:51 +00001138}
1139
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001140void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner12b1c762009-04-27 06:16:06 +00001141 RecordData Record;
1142 PCHDeclWriter W(*this, Context, Record);
Chris Lattner12b1c762009-04-27 06:16:06 +00001143
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001144 // If this declaration is also a DeclContext, write blocks for the
1145 // declarations that lexically stored inside its context and those
1146 // declarations that are visible from its context. These blocks
1147 // are written before the declaration itself so that we can put
1148 // their offsets into the record for the declaration.
1149 uint64_t LexicalOffset = 0;
1150 uint64_t VisibleOffset = 0;
1151 DeclContext *DC = dyn_cast<DeclContext>(D);
1152 if (DC) {
1153 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1154 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner12b1c762009-04-27 06:16:06 +00001155 }
1156
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001157 // Determine the ID for this declaration
1158 pch::DeclID &ID = DeclIDs[D];
1159 if (ID == 0)
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001160 ID = NextDeclID++;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001161
Sebastian Redlf2f0f032010-07-23 23:49:55 +00001162 unsigned Index = ID - FirstDeclID;
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001163
1164 // Record the offset for this declaration
1165 if (DeclOffsets.size() == Index)
1166 DeclOffsets.push_back(Stream.GetCurrentBitNo());
1167 else if (DeclOffsets.size() < Index) {
1168 DeclOffsets.resize(Index+1);
1169 DeclOffsets[Index] = Stream.GetCurrentBitNo();
1170 }
1171
1172 // Build and emit a record for this declaration
1173 Record.clear();
1174 W.Code = (pch::DeclCode)0;
1175 W.AbbrevToUse = 0;
1176 W.Visit(D);
1177 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
1178
Daniel Dunbar33671982009-12-03 09:13:36 +00001179 if (!W.Code)
Chris Lattner83e7a782010-04-07 22:58:06 +00001180 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbar33671982009-12-03 09:13:36 +00001181 D->getDeclKindName() + "'");
Douglas Gregor61d60ee2009-10-17 00:13:19 +00001182 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
1183
1184 // If the declaration had any attributes, write them now.
1185 if (D->hasAttrs())
1186 WriteAttributeRecord(D->getAttrs());
1187
1188 // Flush any expressions that were written as part of this declaration.
1189 FlushStmts();
1190
1191 // Note "external" declarations so that we can add them to a record in the
1192 // PCH file later.
1193 //
1194 // FIXME: This should be renamed, the predicate is much more complicated.
1195 if (isRequiredDecl(D, Context))
1196 ExternalDefinitions.push_back(Index + 1);
Chris Lattner12b1c762009-04-27 06:16:06 +00001197}