blob: 35850934382666e82ed1c475271dee146bbe7312 [file] [log] [blame]
Chris Lattner7099dbc2009-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"
16#include "clang/AST/Expr.h"
Daniel Dunbarf5bda7b2009-12-03 09:13:36 +000017#include "llvm/ADT/Twine.h"
Chris Lattner7099dbc2009-04-27 06:16:06 +000018#include "llvm/Bitcode/BitstreamWriter.h"
Daniel Dunbarf5bda7b2009-12-03 09:13:36 +000019#include "llvm/Support/ErrorHandling.h"
Chris Lattner7099dbc2009-04-27 06:16:06 +000020using namespace clang;
21
22//===----------------------------------------------------------------------===//
23// Declaration serialization
24//===----------------------------------------------------------------------===//
25
26namespace {
27 class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
28
29 PCHWriter &Writer;
30 ASTContext &Context;
31 PCHWriter::RecordData &Record;
32
33 public:
34 pch::DeclCode Code;
Chris Lattner258172e2009-04-27 07:35:58 +000035 unsigned AbbrevToUse;
Chris Lattner7099dbc2009-04-27 06:16:06 +000036
Mike Stump11289f42009-09-09 15:08:12 +000037 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
38 PCHWriter::RecordData &Record)
Chris Lattner258172e2009-04-27 07:35:58 +000039 : Writer(Writer), Context(Context), Record(Record) {
40 }
Chris Lattner7099dbc2009-04-27 06:16:06 +000041
42 void VisitDecl(Decl *D);
43 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
44 void VisitNamedDecl(NamedDecl *D);
Douglas Gregore31bbd92010-02-21 18:22:14 +000045 void VisitNamespaceDecl(NamespaceDecl *D);
Chris Lattner7099dbc2009-04-27 06:16:06 +000046 void VisitTypeDecl(TypeDecl *D);
47 void VisitTypedefDecl(TypedefDecl *D);
48 void VisitTagDecl(TagDecl *D);
49 void VisitEnumDecl(EnumDecl *D);
50 void VisitRecordDecl(RecordDecl *D);
51 void VisitValueDecl(ValueDecl *D);
52 void VisitEnumConstantDecl(EnumConstantDecl *D);
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +000053 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner7099dbc2009-04-27 06:16:06 +000054 void VisitFunctionDecl(FunctionDecl *D);
55 void VisitFieldDecl(FieldDecl *D);
56 void VisitVarDecl(VarDecl *D);
57 void VisitImplicitParamDecl(ImplicitParamDecl *D);
58 void VisitParmVarDecl(ParmVarDecl *D);
Chris Lattner7099dbc2009-04-27 06:16:06 +000059 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
60 void VisitBlockDecl(BlockDecl *D);
Mike Stump11289f42009-09-09 15:08:12 +000061 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner7099dbc2009-04-27 06:16:06 +000062 uint64_t VisibleOffset);
63 void VisitObjCMethodDecl(ObjCMethodDecl *D);
64 void VisitObjCContainerDecl(ObjCContainerDecl *D);
65 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
66 void VisitObjCIvarDecl(ObjCIvarDecl *D);
67 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
68 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
69 void VisitObjCClassDecl(ObjCClassDecl *D);
70 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
71 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
72 void VisitObjCImplDecl(ObjCImplDecl *D);
73 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
74 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
75 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
76 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
77 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
78 };
79}
80
81void PCHDeclWriter::VisitDecl(Decl *D) {
82 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
83 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
84 Writer.AddSourceLocation(D->getLocation(), Record);
85 Record.push_back(D->isInvalidDecl());
86 Record.push_back(D->hasAttrs());
87 Record.push_back(D->isImplicit());
Douglas Gregorc9c02ed2009-06-19 23:52:42 +000088 Record.push_back(D->isUsed());
Chris Lattner7099dbc2009-04-27 06:16:06 +000089 Record.push_back(D->getAccess());
Douglas Gregor16bef852009-10-16 20:01:17 +000090 Record.push_back(D->getPCHLevel());
Chris Lattner7099dbc2009-04-27 06:16:06 +000091}
92
93void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
94 VisitDecl(D);
95 Code = pch::DECL_TRANSLATION_UNIT;
96}
97
98void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
99 VisitDecl(D);
100 Writer.AddDeclarationName(D->getDeclName(), Record);
101}
102
Douglas Gregore31bbd92010-02-21 18:22:14 +0000103void PCHDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
104 VisitNamedDecl(D);
105 Writer.AddSourceLocation(D->getLBracLoc(), Record);
106 Writer.AddSourceLocation(D->getRBracLoc(), Record);
107 Writer.AddDeclRef(D->getNextNamespace(), Record);
108 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
109 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
110 Code = pch::DECL_NAMESPACE;
111}
112
Chris Lattner7099dbc2009-04-27 06:16:06 +0000113void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
114 VisitNamedDecl(D);
115 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
116}
117
118void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
119 VisitTypeDecl(D);
John McCallbcd03502009-12-07 02:54:59 +0000120 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000121 Code = pch::DECL_TYPEDEF;
122}
123
124void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
125 VisitTypeDecl(D);
Douglas Gregorb6b8f9e2009-07-29 23:36:44 +0000126 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000127 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
128 Record.push_back(D->isDefinition());
Douglas Gregor5089c762010-02-12 17:40:34 +0000129 Record.push_back(D->isEmbeddedInDeclarator());
Argyrios Kyrtzidis664b6902009-07-14 03:18:02 +0000130 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor82fe3e32009-07-21 14:46:17 +0000131 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
John McCall3e11ebe2010-03-15 10:12:16 +0000132 // FIXME: maybe write optional qualifier and its range.
133 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000134}
135
136void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
137 VisitTagDecl(D);
138 Writer.AddTypeRef(D->getIntegerType(), Record);
John McCall56774992009-12-09 09:09:27 +0000139 Writer.AddTypeRef(D->getPromotionType(), Record);
John McCall9aa35be2010-05-06 08:49:23 +0000140 Record.push_back(D->getNumPositiveBits());
141 Record.push_back(D->getNumNegativeBits());
Douglas Gregor7a749382009-05-27 17:20:35 +0000142 // FIXME: C++ InstantiatedFrom
Chris Lattner7099dbc2009-04-27 06:16:06 +0000143 Code = pch::DECL_ENUM;
144}
145
146void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
147 VisitTagDecl(D);
148 Record.push_back(D->hasFlexibleArrayMember());
149 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian8e0d0422009-07-08 16:37:44 +0000150 Record.push_back(D->hasObjectMember());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000151 Code = pch::DECL_RECORD;
152}
153
154void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
155 VisitNamedDecl(D);
156 Writer.AddTypeRef(D->getType(), Record);
157}
158
159void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
160 VisitValueDecl(D);
161 Record.push_back(D->getInitExpr()? 1 : 0);
162 if (D->getInitExpr())
163 Writer.AddStmt(D->getInitExpr());
164 Writer.AddAPSInt(D->getInitVal(), Record);
165 Code = pch::DECL_ENUM_CONSTANT;
166}
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000167
168void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
169 VisitValueDecl(D);
John McCallbcd03502009-12-07 02:54:59 +0000170 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
John McCall3e11ebe2010-03-15 10:12:16 +0000171 // FIXME: write optional qualifier and its range.
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000172}
Chris Lattner7099dbc2009-04-27 06:16:06 +0000173
174void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000175 VisitDeclaratorDecl(D);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000176 Record.push_back(D->isThisDeclarationADefinition());
177 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000178 Writer.AddStmt(D->getBody());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000179 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
180 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc4df4072010-04-19 22:54:31 +0000181 Record.push_back(D->getStorageClassAsWritten());
Douglas Gregor35b57532009-10-27 21:01:01 +0000182 Record.push_back(D->isInlineSpecified());
Anders Carlsson0a7c01f2009-05-14 22:15:41 +0000183 Record.push_back(D->isVirtualAsWritten());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000184 Record.push_back(D->isPure());
Anders Carlssone0dd1d52009-05-14 21:46:00 +0000185 Record.push_back(D->hasInheritedPrototype());
186 Record.push_back(D->hasWrittenPrototype());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000187 Record.push_back(D->isDeleted());
Daniel Dunbarb45012d2009-09-22 05:38:14 +0000188 Record.push_back(D->isTrivial());
189 Record.push_back(D->isCopyAssignment());
190 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis1ead0b42009-06-20 08:09:34 +0000191 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor24c332b2009-05-14 21:06:31 +0000192 // FIXME: C++ TemplateOrInstantiation
Chris Lattner7099dbc2009-04-27 06:16:06 +0000193 Record.push_back(D->param_size());
194 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
195 P != PEnd; ++P)
196 Writer.AddDeclRef(*P, Record);
197 Code = pch::DECL_FUNCTION;
198}
199
200void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
201 VisitNamedDecl(D);
202 // FIXME: convert to LazyStmtPtr?
Mike Stump11289f42009-09-09 15:08:12 +0000203 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner7099dbc2009-04-27 06:16:06 +0000204 Record.push_back(D->getBody() != 0);
205 if (D->getBody() != 0) {
Argyrios Kyrtzidisddcd1322009-06-30 02:35:26 +0000206 Writer.AddStmt(D->getBody());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000207 Writer.AddDeclRef(D->getSelfDecl(), Record);
208 Writer.AddDeclRef(D->getCmdDecl(), Record);
209 }
210 Record.push_back(D->isInstanceMethod());
211 Record.push_back(D->isVariadic());
212 Record.push_back(D->isSynthesized());
213 // FIXME: stable encoding for @required/@optional
Mike Stump11289f42009-09-09 15:08:12 +0000214 Record.push_back(D->getImplementationControl());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000215 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump11289f42009-09-09 15:08:12 +0000216 Record.push_back(D->getObjCDeclQualifier());
Fariborz Jahaniand9235db2010-04-08 21:29:11 +0000217 Record.push_back(D->getNumSelectorArgs());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000218 Writer.AddTypeRef(D->getResultType(), Record);
Douglas Gregor12852d92010-03-08 14:59:44 +0000219 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000220 Writer.AddSourceLocation(D->getLocEnd(), Record);
221 Record.push_back(D->param_size());
Mike Stump11289f42009-09-09 15:08:12 +0000222 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner7099dbc2009-04-27 06:16:06 +0000223 PEnd = D->param_end(); P != PEnd; ++P)
224 Writer.AddDeclRef(*P, Record);
225 Code = pch::DECL_OBJC_METHOD;
226}
227
228void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
229 VisitNamedDecl(D);
Ted Kremenekc7c64312010-01-07 01:20:12 +0000230 SourceRange R = D->getAtEndRange();
231 Writer.AddSourceLocation(R.getBegin(), Record);
232 Writer.AddSourceLocation(R.getEnd(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000233 // Abstract class (no need to define a stable pch::DECL code).
234}
235
236void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
237 VisitObjCContainerDecl(D);
238 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
239 Writer.AddDeclRef(D->getSuperClass(), Record);
240 Record.push_back(D->protocol_size());
Mike Stump11289f42009-09-09 15:08:12 +0000241 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner7099dbc2009-04-27 06:16:06 +0000242 PEnd = D->protocol_end();
243 P != PEnd; ++P)
244 Writer.AddDeclRef(*P, Record);
Douglas Gregor002b6712010-01-16 15:02:53 +0000245 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
246 PLEnd = D->protocol_loc_end();
247 PL != PLEnd; ++PL)
248 Writer.AddSourceLocation(*PL, Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000249 Record.push_back(D->ivar_size());
Mike Stump11289f42009-09-09 15:08:12 +0000250 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner7099dbc2009-04-27 06:16:06 +0000251 IEnd = D->ivar_end(); I != IEnd; ++I)
252 Writer.AddDeclRef(*I, Record);
253 Writer.AddDeclRef(D->getCategoryList(), Record);
254 Record.push_back(D->isForwardDecl());
255 Record.push_back(D->isImplicitInterfaceDecl());
256 Writer.AddSourceLocation(D->getClassLoc(), Record);
257 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
258 Writer.AddSourceLocation(D->getLocEnd(), Record);
259 Code = pch::DECL_OBJC_INTERFACE;
260}
261
262void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
263 VisitFieldDecl(D);
264 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump11289f42009-09-09 15:08:12 +0000265 Record.push_back(D->getAccessControl());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000266 Code = pch::DECL_OBJC_IVAR;
267}
268
269void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
270 VisitObjCContainerDecl(D);
271 Record.push_back(D->isForwardDecl());
272 Writer.AddSourceLocation(D->getLocEnd(), Record);
273 Record.push_back(D->protocol_size());
Mike Stump11289f42009-09-09 15:08:12 +0000274 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner7099dbc2009-04-27 06:16:06 +0000275 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
276 Writer.AddDeclRef(*I, Record);
Douglas Gregor002b6712010-01-16 15:02:53 +0000277 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
278 PLEnd = D->protocol_loc_end();
279 PL != PLEnd; ++PL)
280 Writer.AddSourceLocation(*PL, Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000281 Code = pch::DECL_OBJC_PROTOCOL;
282}
283
284void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
285 VisitFieldDecl(D);
286 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
287}
288
289void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
290 VisitDecl(D);
291 Record.push_back(D->size());
292 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
Ted Kremenek9b124e12009-11-18 00:28:11 +0000293 Writer.AddDeclRef(I->getInterface(), Record);
294 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
295 Writer.AddSourceLocation(I->getLocation(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000296 Code = pch::DECL_OBJC_CLASS;
297}
298
299void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
300 VisitDecl(D);
301 Record.push_back(D->protocol_size());
Douglas Gregor002b6712010-01-16 15:02:53 +0000302 for (ObjCForwardProtocolDecl::protocol_iterator
Chris Lattner7099dbc2009-04-27 06:16:06 +0000303 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
304 Writer.AddDeclRef(*I, Record);
Douglas Gregor002b6712010-01-16 15:02:53 +0000305 for (ObjCForwardProtocolDecl::protocol_loc_iterator
306 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
307 PL != PLEnd; ++PL)
308 Writer.AddSourceLocation(*PL, Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000309 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
310}
311
312void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
313 VisitObjCContainerDecl(D);
314 Writer.AddDeclRef(D->getClassInterface(), Record);
315 Record.push_back(D->protocol_size());
Douglas Gregor002b6712010-01-16 15:02:53 +0000316 for (ObjCCategoryDecl::protocol_iterator
Chris Lattner7099dbc2009-04-27 06:16:06 +0000317 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
318 Writer.AddDeclRef(*I, Record);
Douglas Gregor002b6712010-01-16 15:02:53 +0000319 for (ObjCCategoryDecl::protocol_loc_iterator
320 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
321 PL != PLEnd; ++PL)
322 Writer.AddSourceLocation(*PL, Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000323 Writer.AddDeclRef(D->getNextClassCategory(), Record);
Douglas Gregor071676f2010-01-16 16:38:58 +0000324 Writer.AddSourceLocation(D->getAtLoc(), Record);
325 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000326 Code = pch::DECL_OBJC_CATEGORY;
327}
328
329void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
330 VisitNamedDecl(D);
331 Writer.AddDeclRef(D->getClassInterface(), Record);
332 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
333}
334
335void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
336 VisitNamedDecl(D);
Fariborz Jahanianda8ec2b2010-01-21 17:36:00 +0000337 Writer.AddSourceLocation(D->getAtLoc(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000338 Writer.AddTypeRef(D->getType(), Record);
339 // FIXME: stable encoding
340 Record.push_back((unsigned)D->getPropertyAttributes());
341 // FIXME: stable encoding
342 Record.push_back((unsigned)D->getPropertyImplementation());
343 Writer.AddDeclarationName(D->getGetterName(), Record);
344 Writer.AddDeclarationName(D->getSetterName(), Record);
345 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
346 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
347 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
348 Code = pch::DECL_OBJC_PROPERTY;
349}
350
351void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidis067c4072009-07-27 19:04:32 +0000352 VisitObjCContainerDecl(D);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000353 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000354 // Abstract class (no need to define a stable pch::DECL code).
355}
356
357void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
358 VisitObjCImplDecl(D);
359 Writer.AddIdentifierRef(D->getIdentifier(), Record);
360 Code = pch::DECL_OBJC_CATEGORY_IMPL;
361}
362
363void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
364 VisitObjCImplDecl(D);
365 Writer.AddDeclRef(D->getSuperClass(), Record);
Fariborz Jahanianc83726e2010-04-28 16:11:27 +0000366 // FIXME add writing of IvarInitializers and NumIvarInitializers.
Chris Lattner7099dbc2009-04-27 06:16:06 +0000367 Code = pch::DECL_OBJC_IMPLEMENTATION;
368}
369
370void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
371 VisitDecl(D);
372 Writer.AddSourceLocation(D->getLocStart(), Record);
373 Writer.AddDeclRef(D->getPropertyDecl(), Record);
374 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
Fariborz Jahanian25491a22010-05-05 21:52:17 +0000375 // FIXME. write GetterCXXConstructor and SetterCXXAssignment.
Chris Lattner7099dbc2009-04-27 06:16:06 +0000376 Code = pch::DECL_OBJC_PROPERTY_IMPL;
377}
378
379void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000380 VisitDeclaratorDecl(D);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000381 Record.push_back(D->isMutable());
382 Record.push_back(D->getBitWidth()? 1 : 0);
383 if (D->getBitWidth())
384 Writer.AddStmt(D->getBitWidth());
385 Code = pch::DECL_FIELD;
386}
387
388void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000389 VisitDeclaratorDecl(D);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000390 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
Douglas Gregorc4df4072010-04-19 22:54:31 +0000391 Record.push_back(D->getStorageClassAsWritten());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000392 Record.push_back(D->isThreadSpecified());
393 Record.push_back(D->hasCXXDirectInitializer());
394 Record.push_back(D->isDeclaredInCondition());
Douglas Gregor3f324d562010-05-03 18:51:14 +0000395 Record.push_back(D->isExceptionVariable());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000396 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000397 Record.push_back(D->getInit()? 1 : 0);
398 if (D->getInit())
399 Writer.AddStmt(D->getInit());
400 Code = pch::DECL_VAR;
401}
402
403void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
404 VisitVarDecl(D);
405 Code = pch::DECL_IMPLICIT_PARAM;
406}
407
408void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
409 VisitVarDecl(D);
410 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
John McCallf3cd6652010-03-12 18:31:32 +0000411 Record.push_back(D->hasInheritedDefaultArg());
Chris Lattner7099dbc2009-04-27 06:16:06 +0000412 Code = pch::DECL_PARM_VAR;
Mike Stump11289f42009-09-09 15:08:12 +0000413
414
Chris Lattner258172e2009-04-27 07:35:58 +0000415 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
416 // we dynamically check for the properties that we optimize for, but don't
417 // know are true of all PARM_VAR_DECLs.
John McCallbcd03502009-12-07 02:54:59 +0000418 if (!D->getTypeSourceInfo() &&
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000419 !D->hasAttrs() &&
Chris Lattner258172e2009-04-27 07:35:58 +0000420 !D->isImplicit() &&
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000421 !D->isUsed() &&
Chris Lattner258172e2009-04-27 07:35:58 +0000422 D->getAccess() == AS_none &&
Douglas Gregor16bef852009-10-16 20:01:17 +0000423 D->getPCHLevel() == 0 &&
Chris Lattner258172e2009-04-27 07:35:58 +0000424 D->getStorageClass() == 0 &&
425 !D->hasCXXDirectInitializer() && // Can params have this ever?
John McCallf3cd6652010-03-12 18:31:32 +0000426 D->getObjCDeclQualifier() == 0 &&
427 !D->hasInheritedDefaultArg())
Chris Lattner258172e2009-04-27 07:35:58 +0000428 AbbrevToUse = Writer.getParmVarDeclAbbrev();
429
430 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
431 // just us assuming it.
432 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
433 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
434 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
435 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
Douglas Gregor3f324d562010-05-03 18:51:14 +0000436 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
Chris Lattner258172e2009-04-27 07:35:58 +0000437 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
438 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner7099dbc2009-04-27 06:16:06 +0000439}
440
Chris Lattner7099dbc2009-04-27 06:16:06 +0000441void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
442 VisitDecl(D);
443 Writer.AddStmt(D->getAsmString());
444 Code = pch::DECL_FILE_SCOPE_ASM;
445}
446
447void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
448 VisitDecl(D);
449 Writer.AddStmt(D->getBody());
450 Record.push_back(D->param_size());
451 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
452 P != PEnd; ++P)
453 Writer.AddDeclRef(*P, Record);
454 Code = pch::DECL_BLOCK;
455}
456
457/// \brief Emit the DeclContext part of a declaration context decl.
458///
459/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
460/// block for this declaration context is stored. May be 0 to indicate
461/// that there are no declarations stored within this context.
462///
463/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
464/// block for this declaration context is stored. May be 0 to indicate
465/// that there are no declarations visible from this context. Note
466/// that this value will not be emitted for non-primary declaration
467/// contexts.
Mike Stump11289f42009-09-09 15:08:12 +0000468void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner7099dbc2009-04-27 06:16:06 +0000469 uint64_t VisibleOffset) {
470 Record.push_back(LexicalOffset);
471 Record.push_back(VisibleOffset);
472}
473
474
475//===----------------------------------------------------------------------===//
476// PCHWriter Implementation
477//===----------------------------------------------------------------------===//
478
Chris Lattner258172e2009-04-27 07:35:58 +0000479void PCHWriter::WriteDeclsBlockAbbrevs() {
480 using namespace llvm;
481 // Abbreviation for DECL_PARM_VAR.
482 BitCodeAbbrev *Abv = new BitCodeAbbrev();
483 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
484
485 // Decl
486 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
487 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
488 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
489 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
490 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
491 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregorc9c02ed2009-06-19 23:52:42 +0000492 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattner258172e2009-04-27 07:35:58 +0000493 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor16bef852009-10-16 20:01:17 +0000494 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump11289f42009-09-09 15:08:12 +0000495
Chris Lattner258172e2009-04-27 07:35:58 +0000496 // NamedDecl
497 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
498 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
499 // ValueDecl
500 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidis560ac972009-08-19 01:28:35 +0000501 // DeclaratorDecl
502 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattner258172e2009-04-27 07:35:58 +0000503 // VarDecl
504 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
Douglas Gregorc4df4072010-04-19 22:54:31 +0000505 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
Chris Lattner258172e2009-04-27 07:35:58 +0000506 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
507 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
508 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
Douglas Gregor3f324d562010-05-03 18:51:14 +0000509 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
Chris Lattner258172e2009-04-27 07:35:58 +0000510 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattner258172e2009-04-27 07:35:58 +0000511 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
512 // ParmVarDecl
513 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
John McCallf3cd6652010-03-12 18:31:32 +0000514 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
Mike Stump11289f42009-09-09 15:08:12 +0000515
Chris Lattner258172e2009-04-27 07:35:58 +0000516 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
517}
518
Daniel Dunbar5bb5ec52009-09-17 03:06:51 +0000519/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
520/// consumers of the AST.
521///
522/// Such decls will always be deserialized from the PCH file, so we would like
523/// this to be as restrictive as possible. Currently the predicate is driven by
524/// code generation requirements, if other clients have a different notion of
525/// what is "required" then we may have to consider an alternate scheme where
526/// clients can iterate over the top-level decls and get information on them,
527/// without necessary deserializing them. We could explicitly require such
528/// clients to use a separate API call to "realize" the decl. This should be
529/// relatively painless since they would presumably only do it for top-level
530/// decls.
531//
532// FIXME: This predicate is essentially IRgen's predicate to determine whether a
533// declaration can be deferred. Merge them somehow.
534static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
535 // File scoped assembly must be seen.
536 if (isa<FileScopeAsmDecl>(D))
537 return true;
538
539 // Otherwise if this isn't a function or a file scoped variable it doesn't
540 // need to be seen.
541 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
542 if (!VD->isFileVarDecl())
543 return false;
544 } else if (!isa<FunctionDecl>(D))
545 return false;
546
547 // Aliases and used decls must be seen.
548 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
549 return true;
550
551 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
552 // Forward declarations don't need to be seen.
553 if (!FD->isThisDeclarationADefinition())
554 return false;
555
556 // Constructors and destructors must be seen.
557 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
558 return true;
559
560 // Otherwise, this is required unless it is static.
561 //
562 // FIXME: Inlines.
563 return FD->getStorageClass() != FunctionDecl::Static;
564 } else {
565 const VarDecl *VD = cast<VarDecl>(D);
566
567 // In C++, this doesn't need to be seen if it is marked "extern".
568 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
569 (VD->getStorageClass() == VarDecl::Extern ||
570 VD->isExternC()))
571 return false;
572
573 // In C, this doesn't need to be seen unless it is a definition.
574 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
575 return false;
576
577 // Otherwise, this is required unless it is static.
578 return VD->getStorageClass() != VarDecl::Static;
579 }
580}
581
Douglas Gregor12bfa382009-10-17 00:13:19 +0000582void PCHWriter::WriteDecl(ASTContext &Context, Decl *D) {
Chris Lattner7099dbc2009-04-27 06:16:06 +0000583 RecordData Record;
584 PCHDeclWriter W(*this, Context, Record);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000585
Douglas Gregor12bfa382009-10-17 00:13:19 +0000586 // If this declaration is also a DeclContext, write blocks for the
587 // declarations that lexically stored inside its context and those
588 // declarations that are visible from its context. These blocks
589 // are written before the declaration itself so that we can put
590 // their offsets into the record for the declaration.
591 uint64_t LexicalOffset = 0;
592 uint64_t VisibleOffset = 0;
593 DeclContext *DC = dyn_cast<DeclContext>(D);
594 if (DC) {
595 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
596 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000597 }
598
Douglas Gregor12bfa382009-10-17 00:13:19 +0000599 // Determine the ID for this declaration
600 pch::DeclID &ID = DeclIDs[D];
601 if (ID == 0)
602 ID = DeclIDs.size();
603
604 unsigned Index = ID - 1;
605
606 // Record the offset for this declaration
607 if (DeclOffsets.size() == Index)
608 DeclOffsets.push_back(Stream.GetCurrentBitNo());
609 else if (DeclOffsets.size() < Index) {
610 DeclOffsets.resize(Index+1);
611 DeclOffsets[Index] = Stream.GetCurrentBitNo();
612 }
613
614 // Build and emit a record for this declaration
615 Record.clear();
616 W.Code = (pch::DeclCode)0;
617 W.AbbrevToUse = 0;
618 W.Visit(D);
619 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
620
Daniel Dunbarf5bda7b2009-12-03 09:13:36 +0000621 if (!W.Code)
Chris Lattner4b73cfa2010-04-07 22:58:06 +0000622 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
Daniel Dunbarf5bda7b2009-12-03 09:13:36 +0000623 D->getDeclKindName() + "'");
Douglas Gregor12bfa382009-10-17 00:13:19 +0000624 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
625
626 // If the declaration had any attributes, write them now.
627 if (D->hasAttrs())
628 WriteAttributeRecord(D->getAttrs());
629
630 // Flush any expressions that were written as part of this declaration.
631 FlushStmts();
632
633 // Note "external" declarations so that we can add them to a record in the
634 // PCH file later.
635 //
636 // FIXME: This should be renamed, the predicate is much more complicated.
637 if (isRequiredDecl(D, Context))
638 ExternalDefinitions.push_back(Index + 1);
Chris Lattner7099dbc2009-04-27 06:16:06 +0000639}