blob: ef7c5ec4b1a69bb69326cadb26755165e1de3616 [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"
16#include "clang/AST/Expr.h"
Argyrios Kyrtzidis0c411802009-09-29 21:27:32 +000017#include "clang/AST/TypeLocVisitor.h"
Chris Lattner12b1c762009-04-27 06:16:06 +000018#include "llvm/Bitcode/BitstreamWriter.h"
Torok Edwinf42e4a62009-08-24 13:25:12 +000019#include <cstdio>
20
Chris Lattner12b1c762009-04-27 06:16:06 +000021using namespace clang;
22
23//===----------------------------------------------------------------------===//
24// Declaration serialization
25//===----------------------------------------------------------------------===//
26
27namespace {
28 class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
29
30 PCHWriter &Writer;
31 ASTContext &Context;
32 PCHWriter::RecordData &Record;
33
34 public:
35 pch::DeclCode Code;
Chris Lattnerea5ce472009-04-27 07:35:58 +000036 unsigned AbbrevToUse;
Chris Lattner12b1c762009-04-27 06:16:06 +000037
Mike Stump1eb44332009-09-09 15:08:12 +000038 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
39 PCHWriter::RecordData &Record)
Chris Lattnerea5ce472009-04-27 07:35:58 +000040 : Writer(Writer), Context(Context), Record(Record) {
41 }
Chris Lattner12b1c762009-04-27 06:16:06 +000042
43 void VisitDecl(Decl *D);
44 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
45 void VisitNamedDecl(NamedDecl *D);
46 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 Kyrtzidisd4a7e542009-08-19 01:28:35 +000053 void VisitDeclaratorDecl(DeclaratorDecl *D);
Chris Lattner12b1c762009-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);
59 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
60 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
61 void VisitBlockDecl(BlockDecl *D);
Mike Stump1eb44332009-09-09 15:08:12 +000062 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +000063 uint64_t VisibleOffset);
64 void VisitObjCMethodDecl(ObjCMethodDecl *D);
65 void VisitObjCContainerDecl(ObjCContainerDecl *D);
66 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
67 void VisitObjCIvarDecl(ObjCIvarDecl *D);
68 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
69 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
70 void VisitObjCClassDecl(ObjCClassDecl *D);
71 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
72 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
73 void VisitObjCImplDecl(ObjCImplDecl *D);
74 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
75 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
76 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
77 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
78 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
79 };
80}
81
82void PCHDeclWriter::VisitDecl(Decl *D) {
83 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
84 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
85 Writer.AddSourceLocation(D->getLocation(), Record);
86 Record.push_back(D->isInvalidDecl());
87 Record.push_back(D->hasAttrs());
88 Record.push_back(D->isImplicit());
Douglas Gregore0762c92009-06-19 23:52:42 +000089 Record.push_back(D->isUsed());
Chris Lattner12b1c762009-04-27 06:16:06 +000090 Record.push_back(D->getAccess());
91}
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
103void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
104 VisitNamedDecl(D);
105 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
106}
107
108void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
109 VisitTypeDecl(D);
110 Writer.AddTypeRef(D->getUnderlyingType(), Record);
111 Code = pch::DECL_TYPEDEF;
112}
113
114void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
115 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000116 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000117 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
118 Record.push_back(D->isDefinition());
119 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000120 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000121 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000122}
123
124void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
125 VisitTagDecl(D);
126 Writer.AddTypeRef(D->getIntegerType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000127 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000128 Code = pch::DECL_ENUM;
129}
130
131void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
132 VisitTagDecl(D);
133 Record.push_back(D->hasFlexibleArrayMember());
134 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000135 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000136 Code = pch::DECL_RECORD;
137}
138
139void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
140 VisitNamedDecl(D);
141 Writer.AddTypeRef(D->getType(), Record);
142}
143
144void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
145 VisitValueDecl(D);
146 Record.push_back(D->getInitExpr()? 1 : 0);
147 if (D->getInitExpr())
148 Writer.AddStmt(D->getInitExpr());
149 Writer.AddAPSInt(D->getInitVal(), Record);
150 Code = pch::DECL_ENUM_CONSTANT;
151}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000152namespace {
153
154class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
155 PCHWriter &Writer;
156 PCHWriter::RecordData &Record;
157
158public:
159 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
160 : Writer(Writer), Record(Record) { }
161
162#define ABSTRACT_TYPELOC(CLASS)
John McCall34a04472009-10-15 03:50:32 +0000163#define TYPELOC(CLASS, PARENT) \
Mike Stump1eb44332009-09-09 15:08:12 +0000164 void Visit##CLASS(CLASS TyLoc);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000165#include "clang/AST/TypeLocNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000166
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000167 void VisitTypeLoc(TypeLoc TyLoc) {
168 assert(0 && "A type loc wrapper was not handled!");
169 }
170};
171
172}
173
John McCall34a04472009-10-15 03:50:32 +0000174void TypeLocWriter::VisitQualifiedLoc(QualifiedLoc TyLoc) {
175 // nothing to do here
176}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000177void TypeLocWriter::VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) {
178 Writer.AddSourceLocation(TyLoc.getStartLoc(), Record);
179}
180void TypeLocWriter::VisitTypedefLoc(TypedefLoc TyLoc) {
181 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
182}
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +0000183void TypeLocWriter::VisitObjCInterfaceLoc(ObjCInterfaceLoc TyLoc) {
184 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
185}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000186void TypeLocWriter::VisitObjCProtocolListLoc(ObjCProtocolListLoc TyLoc) {
187 Writer.AddSourceLocation(TyLoc.getLAngleLoc(), Record);
188 Writer.AddSourceLocation(TyLoc.getRAngleLoc(), Record);
189 for (unsigned i = 0, e = TyLoc.getNumProtocols(); i != e; ++i)
190 Writer.AddSourceLocation(TyLoc.getProtocolLoc(i), Record);
191}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000192void TypeLocWriter::VisitPointerLoc(PointerLoc TyLoc) {
193 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
194}
195void TypeLocWriter::VisitBlockPointerLoc(BlockPointerLoc TyLoc) {
196 Writer.AddSourceLocation(TyLoc.getCaretLoc(), Record);
197}
198void TypeLocWriter::VisitMemberPointerLoc(MemberPointerLoc TyLoc) {
199 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
200}
201void TypeLocWriter::VisitReferenceLoc(ReferenceLoc TyLoc) {
202 Writer.AddSourceLocation(TyLoc.getAmpLoc(), Record);
203}
204void TypeLocWriter::VisitFunctionLoc(FunctionLoc TyLoc) {
205 Writer.AddSourceLocation(TyLoc.getLParenLoc(), Record);
206 Writer.AddSourceLocation(TyLoc.getRParenLoc(), Record);
207 for (unsigned i = 0, e = TyLoc.getNumArgs(); i != e; ++i)
208 Writer.AddDeclRef(TyLoc.getArg(i), Record);
209}
210void TypeLocWriter::VisitArrayLoc(ArrayLoc TyLoc) {
211 Writer.AddSourceLocation(TyLoc.getLBracketLoc(), Record);
212 Writer.AddSourceLocation(TyLoc.getRBracketLoc(), Record);
213 Record.push_back(TyLoc.getSizeExpr() ? 1 : 0);
214 if (TyLoc.getSizeExpr())
215 Writer.AddStmt(TyLoc.getSizeExpr());
216}
217
218void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
219 VisitValueDecl(D);
220 DeclaratorInfo *DInfo = D->getDeclaratorInfo();
221 if (DInfo == 0) {
222 Writer.AddTypeRef(QualType(), Record);
223 return;
224 }
Mike Stump1eb44332009-09-09 15:08:12 +0000225
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000226 Writer.AddTypeRef(DInfo->getTypeLoc().getSourceType(), Record);
227 TypeLocWriter TLW(Writer, Record);
228 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
229 TLW.Visit(TL);
230}
Chris Lattner12b1c762009-04-27 06:16:06 +0000231
232void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000233 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000234 Record.push_back(D->isThisDeclarationADefinition());
235 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000236 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000237 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
238 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
239 Record.push_back(D->isInline());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000240 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000241 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000242 Record.push_back(D->hasInheritedPrototype());
243 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000244 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000245 Record.push_back(D->isTrivial());
246 Record.push_back(D->isCopyAssignment());
247 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000248 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000249 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000250 Record.push_back(D->param_size());
251 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
252 P != PEnd; ++P)
253 Writer.AddDeclRef(*P, Record);
254 Code = pch::DECL_FUNCTION;
255}
256
257void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
258 VisitNamedDecl(D);
259 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000260 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000261 Record.push_back(D->getBody() != 0);
262 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000263 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000264 Writer.AddDeclRef(D->getSelfDecl(), Record);
265 Writer.AddDeclRef(D->getCmdDecl(), Record);
266 }
267 Record.push_back(D->isInstanceMethod());
268 Record.push_back(D->isVariadic());
269 Record.push_back(D->isSynthesized());
270 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000271 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000272 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000273 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000274 Writer.AddTypeRef(D->getResultType(), Record);
275 Writer.AddSourceLocation(D->getLocEnd(), Record);
276 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000277 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000278 PEnd = D->param_end(); P != PEnd; ++P)
279 Writer.AddDeclRef(*P, Record);
280 Code = pch::DECL_OBJC_METHOD;
281}
282
283void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
284 VisitNamedDecl(D);
285 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
286 // Abstract class (no need to define a stable pch::DECL code).
287}
288
289void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
290 VisitObjCContainerDecl(D);
291 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
292 Writer.AddDeclRef(D->getSuperClass(), Record);
293 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000294 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000295 PEnd = D->protocol_end();
296 P != PEnd; ++P)
297 Writer.AddDeclRef(*P, Record);
298 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000299 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000300 IEnd = D->ivar_end(); I != IEnd; ++I)
301 Writer.AddDeclRef(*I, Record);
302 Writer.AddDeclRef(D->getCategoryList(), Record);
303 Record.push_back(D->isForwardDecl());
304 Record.push_back(D->isImplicitInterfaceDecl());
305 Writer.AddSourceLocation(D->getClassLoc(), Record);
306 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
307 Writer.AddSourceLocation(D->getLocEnd(), Record);
308 Code = pch::DECL_OBJC_INTERFACE;
309}
310
311void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
312 VisitFieldDecl(D);
313 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000314 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000315 Code = pch::DECL_OBJC_IVAR;
316}
317
318void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
319 VisitObjCContainerDecl(D);
320 Record.push_back(D->isForwardDecl());
321 Writer.AddSourceLocation(D->getLocEnd(), Record);
322 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000323 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000324 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
325 Writer.AddDeclRef(*I, Record);
326 Code = pch::DECL_OBJC_PROTOCOL;
327}
328
329void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
330 VisitFieldDecl(D);
331 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
332}
333
334void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
335 VisitDecl(D);
336 Record.push_back(D->size());
337 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
338 Writer.AddDeclRef(*I, Record);
339 Code = pch::DECL_OBJC_CLASS;
340}
341
342void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
343 VisitDecl(D);
344 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000345 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000346 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
347 Writer.AddDeclRef(*I, Record);
348 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
349}
350
351void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
352 VisitObjCContainerDecl(D);
353 Writer.AddDeclRef(D->getClassInterface(), Record);
354 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000355 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000356 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
357 Writer.AddDeclRef(*I, Record);
358 Writer.AddDeclRef(D->getNextClassCategory(), Record);
359 Writer.AddSourceLocation(D->getLocEnd(), Record);
360 Code = pch::DECL_OBJC_CATEGORY;
361}
362
363void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
364 VisitNamedDecl(D);
365 Writer.AddDeclRef(D->getClassInterface(), Record);
366 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
367}
368
369void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
370 VisitNamedDecl(D);
371 Writer.AddTypeRef(D->getType(), Record);
372 // FIXME: stable encoding
373 Record.push_back((unsigned)D->getPropertyAttributes());
374 // FIXME: stable encoding
375 Record.push_back((unsigned)D->getPropertyImplementation());
376 Writer.AddDeclarationName(D->getGetterName(), Record);
377 Writer.AddDeclarationName(D->getSetterName(), Record);
378 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
379 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
380 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
381 Code = pch::DECL_OBJC_PROPERTY;
382}
383
384void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000385 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000386 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000387 // Abstract class (no need to define a stable pch::DECL code).
388}
389
390void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
391 VisitObjCImplDecl(D);
392 Writer.AddIdentifierRef(D->getIdentifier(), Record);
393 Code = pch::DECL_OBJC_CATEGORY_IMPL;
394}
395
396void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
397 VisitObjCImplDecl(D);
398 Writer.AddDeclRef(D->getSuperClass(), Record);
399 Code = pch::DECL_OBJC_IMPLEMENTATION;
400}
401
402void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
403 VisitDecl(D);
404 Writer.AddSourceLocation(D->getLocStart(), Record);
405 Writer.AddDeclRef(D->getPropertyDecl(), Record);
406 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
407 Code = pch::DECL_OBJC_PROPERTY_IMPL;
408}
409
410void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000411 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000412 Record.push_back(D->isMutable());
413 Record.push_back(D->getBitWidth()? 1 : 0);
414 if (D->getBitWidth())
415 Writer.AddStmt(D->getBitWidth());
416 Code = pch::DECL_FIELD;
417}
418
419void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000420 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000421 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
422 Record.push_back(D->isThreadSpecified());
423 Record.push_back(D->hasCXXDirectInitializer());
424 Record.push_back(D->isDeclaredInCondition());
425 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000426 Record.push_back(D->getInit()? 1 : 0);
427 if (D->getInit())
428 Writer.AddStmt(D->getInit());
429 Code = pch::DECL_VAR;
430}
431
432void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
433 VisitVarDecl(D);
434 Code = pch::DECL_IMPLICIT_PARAM;
435}
436
437void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
438 VisitVarDecl(D);
439 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000440 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000441
442
Chris Lattnerea5ce472009-04-27 07:35:58 +0000443 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
444 // we dynamically check for the properties that we optimize for, but don't
445 // know are true of all PARM_VAR_DECLs.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000446 if (!D->getDeclaratorInfo() &&
447 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000448 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000449 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000450 D->getAccess() == AS_none &&
451 D->getStorageClass() == 0 &&
452 !D->hasCXXDirectInitializer() && // Can params have this ever?
453 D->getObjCDeclQualifier() == 0)
454 AbbrevToUse = Writer.getParmVarDeclAbbrev();
455
456 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
457 // just us assuming it.
458 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
459 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
460 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
461 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
462 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
463 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000464}
465
466void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
467 VisitParmVarDecl(D);
468 Writer.AddTypeRef(D->getOriginalType(), Record);
469 Code = pch::DECL_ORIGINAL_PARM_VAR;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000470 AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000471}
472
473void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
474 VisitDecl(D);
475 Writer.AddStmt(D->getAsmString());
476 Code = pch::DECL_FILE_SCOPE_ASM;
477}
478
479void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
480 VisitDecl(D);
481 Writer.AddStmt(D->getBody());
482 Record.push_back(D->param_size());
483 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
484 P != PEnd; ++P)
485 Writer.AddDeclRef(*P, Record);
486 Code = pch::DECL_BLOCK;
487}
488
489/// \brief Emit the DeclContext part of a declaration context decl.
490///
491/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
492/// block for this declaration context is stored. May be 0 to indicate
493/// that there are no declarations stored within this context.
494///
495/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
496/// block for this declaration context is stored. May be 0 to indicate
497/// that there are no declarations visible from this context. Note
498/// that this value will not be emitted for non-primary declaration
499/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000500void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000501 uint64_t VisibleOffset) {
502 Record.push_back(LexicalOffset);
503 Record.push_back(VisibleOffset);
504}
505
506
507//===----------------------------------------------------------------------===//
508// PCHWriter Implementation
509//===----------------------------------------------------------------------===//
510
Chris Lattnerea5ce472009-04-27 07:35:58 +0000511void PCHWriter::WriteDeclsBlockAbbrevs() {
512 using namespace llvm;
513 // Abbreviation for DECL_PARM_VAR.
514 BitCodeAbbrev *Abv = new BitCodeAbbrev();
515 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
516
517 // Decl
518 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
519 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
520 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
521 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
522 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
523 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000524 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000525 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Mike Stump1eb44332009-09-09 15:08:12 +0000526
Chris Lattnerea5ce472009-04-27 07:35:58 +0000527 // NamedDecl
528 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
529 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
530 // ValueDecl
531 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000532 // DeclaratorDecl
533 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000534 // VarDecl
535 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
536 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
537 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
538 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
539 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000540 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
541 // ParmVarDecl
542 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000543
Chris Lattnerea5ce472009-04-27 07:35:58 +0000544 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
545}
546
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000547/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
548/// consumers of the AST.
549///
550/// Such decls will always be deserialized from the PCH file, so we would like
551/// this to be as restrictive as possible. Currently the predicate is driven by
552/// code generation requirements, if other clients have a different notion of
553/// what is "required" then we may have to consider an alternate scheme where
554/// clients can iterate over the top-level decls and get information on them,
555/// without necessary deserializing them. We could explicitly require such
556/// clients to use a separate API call to "realize" the decl. This should be
557/// relatively painless since they would presumably only do it for top-level
558/// decls.
559//
560// FIXME: This predicate is essentially IRgen's predicate to determine whether a
561// declaration can be deferred. Merge them somehow.
562static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
563 // File scoped assembly must be seen.
564 if (isa<FileScopeAsmDecl>(D))
565 return true;
566
567 // Otherwise if this isn't a function or a file scoped variable it doesn't
568 // need to be seen.
569 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
570 if (!VD->isFileVarDecl())
571 return false;
572 } else if (!isa<FunctionDecl>(D))
573 return false;
574
575 // Aliases and used decls must be seen.
576 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
577 return true;
578
579 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
580 // Forward declarations don't need to be seen.
581 if (!FD->isThisDeclarationADefinition())
582 return false;
583
584 // Constructors and destructors must be seen.
585 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
586 return true;
587
588 // Otherwise, this is required unless it is static.
589 //
590 // FIXME: Inlines.
591 return FD->getStorageClass() != FunctionDecl::Static;
592 } else {
593 const VarDecl *VD = cast<VarDecl>(D);
594
595 // In C++, this doesn't need to be seen if it is marked "extern".
596 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
597 (VD->getStorageClass() == VarDecl::Extern ||
598 VD->isExternC()))
599 return false;
600
601 // In C, this doesn't need to be seen unless it is a definition.
602 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
603 return false;
604
605 // Otherwise, this is required unless it is static.
606 return VD->getStorageClass() != VarDecl::Static;
607 }
608}
609
Chris Lattner12b1c762009-04-27 06:16:06 +0000610/// \brief Write a block containing all of the declarations.
611void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
612 // Enter the declarations block.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000613 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3);
Chris Lattner12b1c762009-04-27 06:16:06 +0000614
Chris Lattnerea5ce472009-04-27 07:35:58 +0000615 // Output the abbreviations that we will use in this block.
616 WriteDeclsBlockAbbrevs();
Mike Stump1eb44332009-09-09 15:08:12 +0000617
Chris Lattner12b1c762009-04-27 06:16:06 +0000618 // Emit all of the declarations.
619 RecordData Record;
620 PCHDeclWriter W(*this, Context, Record);
621 while (!DeclsToEmit.empty()) {
622 // Pull the next declaration off the queue
623 Decl *D = DeclsToEmit.front();
624 DeclsToEmit.pop();
625
626 // If this declaration is also a DeclContext, write blocks for the
627 // declarations that lexically stored inside its context and those
628 // declarations that are visible from its context. These blocks
629 // are written before the declaration itself so that we can put
630 // their offsets into the record for the declaration.
631 uint64_t LexicalOffset = 0;
632 uint64_t VisibleOffset = 0;
633 DeclContext *DC = dyn_cast<DeclContext>(D);
634 if (DC) {
635 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
636 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
637 }
638
639 // Determine the ID for this declaration
Zhongxing Xu44dfc982009-06-01 00:50:23 +0000640 pch::DeclID &ID = DeclIDs[D];
Chris Lattner12b1c762009-04-27 06:16:06 +0000641 if (ID == 0)
642 ID = DeclIDs.size();
643
644 unsigned Index = ID - 1;
645
646 // Record the offset for this declaration
647 if (DeclOffsets.size() == Index)
648 DeclOffsets.push_back(Stream.GetCurrentBitNo());
649 else if (DeclOffsets.size() < Index) {
650 DeclOffsets.resize(Index+1);
651 DeclOffsets[Index] = Stream.GetCurrentBitNo();
652 }
653
654 // Build and emit a record for this declaration
655 Record.clear();
656 W.Code = (pch::DeclCode)0;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000657 W.AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000658 W.Visit(D);
659 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
660
661 if (!W.Code) {
662 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
663 D->getDeclKindName());
664 assert(false && "Unhandled declaration kind while generating PCH");
665 exit(-1);
666 }
Chris Lattnerea5ce472009-04-27 07:35:58 +0000667 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Mike Stump1eb44332009-09-09 15:08:12 +0000668
Chris Lattner12b1c762009-04-27 06:16:06 +0000669 // If the declaration had any attributes, write them now.
670 if (D->hasAttrs())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000671 WriteAttributeRecord(D->getAttrs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000672
673 // Flush any expressions that were written as part of this declaration.
674 FlushStmts();
Mike Stump1eb44332009-09-09 15:08:12 +0000675
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000676 // Note "external" declarations so that we can add them to a record in the
677 // PCH file later.
678 //
679 // FIXME: This should be renamed, the predicate is much more complicated.
680 if (isRequiredDecl(D, Context))
Chris Lattner12b1c762009-04-27 06:16:06 +0000681 ExternalDefinitions.push_back(ID);
682 }
683
684 // Exit the declarations block
685 Stream.ExitBlock();
686}