blob: e2c1ae8715469b31ef69b79f6357b4354ffe89ac [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());
Douglas Gregor7d1d49d2009-10-16 20:01:17 +000091 Record.push_back(D->getPCHLevel());
Chris Lattner12b1c762009-04-27 06:16:06 +000092}
93
94void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
95 VisitDecl(D);
96 Code = pch::DECL_TRANSLATION_UNIT;
97}
98
99void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
100 VisitDecl(D);
101 Writer.AddDeclarationName(D->getDeclName(), Record);
102}
103
104void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
105 VisitNamedDecl(D);
106 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
107}
108
109void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
110 VisitTypeDecl(D);
111 Writer.AddTypeRef(D->getUnderlyingType(), Record);
112 Code = pch::DECL_TYPEDEF;
113}
114
115void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
116 VisitTypeDecl(D);
Douglas Gregor8e9e9ef2009-07-29 23:36:44 +0000117 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000118 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
119 Record.push_back(D->isDefinition());
120 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
Argyrios Kyrtzidisad93a742009-07-14 03:18:02 +0000121 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
Douglas Gregor741dd9a2009-07-21 14:46:17 +0000122 Writer.AddSourceLocation(D->getTagKeywordLoc(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000123}
124
125void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
126 VisitTagDecl(D);
127 Writer.AddTypeRef(D->getIntegerType(), Record);
Douglas Gregor8dbc3c62009-05-27 17:20:35 +0000128 // FIXME: C++ InstantiatedFrom
Chris Lattner12b1c762009-04-27 06:16:06 +0000129 Code = pch::DECL_ENUM;
130}
131
132void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
133 VisitTagDecl(D);
134 Record.push_back(D->hasFlexibleArrayMember());
135 Record.push_back(D->isAnonymousStructOrUnion());
Fariborz Jahanian643b7df2009-07-08 16:37:44 +0000136 Record.push_back(D->hasObjectMember());
Chris Lattner12b1c762009-04-27 06:16:06 +0000137 Code = pch::DECL_RECORD;
138}
139
140void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
141 VisitNamedDecl(D);
142 Writer.AddTypeRef(D->getType(), Record);
143}
144
145void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
146 VisitValueDecl(D);
147 Record.push_back(D->getInitExpr()? 1 : 0);
148 if (D->getInitExpr())
149 Writer.AddStmt(D->getInitExpr());
150 Writer.AddAPSInt(D->getInitVal(), Record);
151 Code = pch::DECL_ENUM_CONSTANT;
152}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000153namespace {
154
155class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
156 PCHWriter &Writer;
157 PCHWriter::RecordData &Record;
158
159public:
160 TypeLocWriter(PCHWriter &Writer, PCHWriter::RecordData &Record)
161 : Writer(Writer), Record(Record) { }
162
163#define ABSTRACT_TYPELOC(CLASS)
John McCall34a04472009-10-15 03:50:32 +0000164#define TYPELOC(CLASS, PARENT) \
Mike Stump1eb44332009-09-09 15:08:12 +0000165 void Visit##CLASS(CLASS TyLoc);
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000166#include "clang/AST/TypeLocNodes.def"
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000168 void VisitTypeLoc(TypeLoc TyLoc) {
169 assert(0 && "A type loc wrapper was not handled!");
170 }
171};
172
173}
174
John McCall34a04472009-10-15 03:50:32 +0000175void TypeLocWriter::VisitQualifiedLoc(QualifiedLoc TyLoc) {
176 // nothing to do here
177}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000178void TypeLocWriter::VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) {
179 Writer.AddSourceLocation(TyLoc.getStartLoc(), Record);
180}
181void TypeLocWriter::VisitTypedefLoc(TypedefLoc TyLoc) {
182 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
183}
Argyrios Kyrtzidiseb667592009-09-29 19:45:22 +0000184void TypeLocWriter::VisitObjCInterfaceLoc(ObjCInterfaceLoc TyLoc) {
185 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
186}
Argyrios Kyrtzidisf352bdd2009-09-29 19:43:35 +0000187void TypeLocWriter::VisitObjCProtocolListLoc(ObjCProtocolListLoc TyLoc) {
188 Writer.AddSourceLocation(TyLoc.getLAngleLoc(), Record);
189 Writer.AddSourceLocation(TyLoc.getRAngleLoc(), Record);
190 for (unsigned i = 0, e = TyLoc.getNumProtocols(); i != e; ++i)
191 Writer.AddSourceLocation(TyLoc.getProtocolLoc(i), Record);
192}
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000193void TypeLocWriter::VisitPointerLoc(PointerLoc TyLoc) {
194 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
195}
196void TypeLocWriter::VisitBlockPointerLoc(BlockPointerLoc TyLoc) {
197 Writer.AddSourceLocation(TyLoc.getCaretLoc(), Record);
198}
199void TypeLocWriter::VisitMemberPointerLoc(MemberPointerLoc TyLoc) {
200 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
201}
202void TypeLocWriter::VisitReferenceLoc(ReferenceLoc TyLoc) {
203 Writer.AddSourceLocation(TyLoc.getAmpLoc(), Record);
204}
205void TypeLocWriter::VisitFunctionLoc(FunctionLoc TyLoc) {
206 Writer.AddSourceLocation(TyLoc.getLParenLoc(), Record);
207 Writer.AddSourceLocation(TyLoc.getRParenLoc(), Record);
208 for (unsigned i = 0, e = TyLoc.getNumArgs(); i != e; ++i)
209 Writer.AddDeclRef(TyLoc.getArg(i), Record);
210}
211void TypeLocWriter::VisitArrayLoc(ArrayLoc TyLoc) {
212 Writer.AddSourceLocation(TyLoc.getLBracketLoc(), Record);
213 Writer.AddSourceLocation(TyLoc.getRBracketLoc(), Record);
214 Record.push_back(TyLoc.getSizeExpr() ? 1 : 0);
215 if (TyLoc.getSizeExpr())
216 Writer.AddStmt(TyLoc.getSizeExpr());
217}
218
219void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
220 VisitValueDecl(D);
221 DeclaratorInfo *DInfo = D->getDeclaratorInfo();
222 if (DInfo == 0) {
223 Writer.AddTypeRef(QualType(), Record);
224 return;
225 }
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000227 Writer.AddTypeRef(DInfo->getTypeLoc().getSourceType(), Record);
228 TypeLocWriter TLW(Writer, Record);
229 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
230 TLW.Visit(TL);
231}
Chris Lattner12b1c762009-04-27 06:16:06 +0000232
233void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000234 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000235 Record.push_back(D->isThisDeclarationADefinition());
236 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000237 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000238 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
239 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
240 Record.push_back(D->isInline());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000241 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000242 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000243 Record.push_back(D->hasInheritedPrototype());
244 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000245 Record.push_back(D->isDeleted());
Daniel Dunbar7f8b57a2009-09-22 05:38:14 +0000246 Record.push_back(D->isTrivial());
247 Record.push_back(D->isCopyAssignment());
248 Record.push_back(D->hasImplicitReturnZero());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000249 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000250 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000251 Record.push_back(D->param_size());
252 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
253 P != PEnd; ++P)
254 Writer.AddDeclRef(*P, Record);
255 Code = pch::DECL_FUNCTION;
256}
257
258void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
259 VisitNamedDecl(D);
260 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000261 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000262 Record.push_back(D->getBody() != 0);
263 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000264 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000265 Writer.AddDeclRef(D->getSelfDecl(), Record);
266 Writer.AddDeclRef(D->getCmdDecl(), Record);
267 }
268 Record.push_back(D->isInstanceMethod());
269 Record.push_back(D->isVariadic());
270 Record.push_back(D->isSynthesized());
271 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000272 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000273 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000274 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000275 Writer.AddTypeRef(D->getResultType(), Record);
276 Writer.AddSourceLocation(D->getLocEnd(), Record);
277 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000278 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000279 PEnd = D->param_end(); P != PEnd; ++P)
280 Writer.AddDeclRef(*P, Record);
281 Code = pch::DECL_OBJC_METHOD;
282}
283
284void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
285 VisitNamedDecl(D);
286 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
287 // Abstract class (no need to define a stable pch::DECL code).
288}
289
290void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
291 VisitObjCContainerDecl(D);
292 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
293 Writer.AddDeclRef(D->getSuperClass(), Record);
294 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000295 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000296 PEnd = D->protocol_end();
297 P != PEnd; ++P)
298 Writer.AddDeclRef(*P, Record);
299 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000300 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000301 IEnd = D->ivar_end(); I != IEnd; ++I)
302 Writer.AddDeclRef(*I, Record);
303 Writer.AddDeclRef(D->getCategoryList(), Record);
304 Record.push_back(D->isForwardDecl());
305 Record.push_back(D->isImplicitInterfaceDecl());
306 Writer.AddSourceLocation(D->getClassLoc(), Record);
307 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
308 Writer.AddSourceLocation(D->getLocEnd(), Record);
309 Code = pch::DECL_OBJC_INTERFACE;
310}
311
312void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
313 VisitFieldDecl(D);
314 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000315 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000316 Code = pch::DECL_OBJC_IVAR;
317}
318
319void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
320 VisitObjCContainerDecl(D);
321 Record.push_back(D->isForwardDecl());
322 Writer.AddSourceLocation(D->getLocEnd(), Record);
323 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000324 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000325 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
326 Writer.AddDeclRef(*I, Record);
327 Code = pch::DECL_OBJC_PROTOCOL;
328}
329
330void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
331 VisitFieldDecl(D);
332 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
333}
334
335void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
336 VisitDecl(D);
337 Record.push_back(D->size());
338 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
339 Writer.AddDeclRef(*I, Record);
340 Code = pch::DECL_OBJC_CLASS;
341}
342
343void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
344 VisitDecl(D);
345 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000346 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000347 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
348 Writer.AddDeclRef(*I, Record);
349 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
350}
351
352void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
353 VisitObjCContainerDecl(D);
354 Writer.AddDeclRef(D->getClassInterface(), Record);
355 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000356 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000357 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
358 Writer.AddDeclRef(*I, Record);
359 Writer.AddDeclRef(D->getNextClassCategory(), Record);
360 Writer.AddSourceLocation(D->getLocEnd(), Record);
361 Code = pch::DECL_OBJC_CATEGORY;
362}
363
364void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
365 VisitNamedDecl(D);
366 Writer.AddDeclRef(D->getClassInterface(), Record);
367 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
368}
369
370void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
371 VisitNamedDecl(D);
372 Writer.AddTypeRef(D->getType(), Record);
373 // FIXME: stable encoding
374 Record.push_back((unsigned)D->getPropertyAttributes());
375 // FIXME: stable encoding
376 Record.push_back((unsigned)D->getPropertyImplementation());
377 Writer.AddDeclarationName(D->getGetterName(), Record);
378 Writer.AddDeclarationName(D->getSetterName(), Record);
379 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
380 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
381 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
382 Code = pch::DECL_OBJC_PROPERTY;
383}
384
385void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000386 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000387 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000388 // Abstract class (no need to define a stable pch::DECL code).
389}
390
391void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
392 VisitObjCImplDecl(D);
393 Writer.AddIdentifierRef(D->getIdentifier(), Record);
394 Code = pch::DECL_OBJC_CATEGORY_IMPL;
395}
396
397void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
398 VisitObjCImplDecl(D);
399 Writer.AddDeclRef(D->getSuperClass(), Record);
400 Code = pch::DECL_OBJC_IMPLEMENTATION;
401}
402
403void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
404 VisitDecl(D);
405 Writer.AddSourceLocation(D->getLocStart(), Record);
406 Writer.AddDeclRef(D->getPropertyDecl(), Record);
407 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
408 Code = pch::DECL_OBJC_PROPERTY_IMPL;
409}
410
411void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000412 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000413 Record.push_back(D->isMutable());
414 Record.push_back(D->getBitWidth()? 1 : 0);
415 if (D->getBitWidth())
416 Writer.AddStmt(D->getBitWidth());
417 Code = pch::DECL_FIELD;
418}
419
420void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000421 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000422 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
423 Record.push_back(D->isThreadSpecified());
424 Record.push_back(D->hasCXXDirectInitializer());
425 Record.push_back(D->isDeclaredInCondition());
426 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000427 Record.push_back(D->getInit()? 1 : 0);
428 if (D->getInit())
429 Writer.AddStmt(D->getInit());
430 Code = pch::DECL_VAR;
431}
432
433void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
434 VisitVarDecl(D);
435 Code = pch::DECL_IMPLICIT_PARAM;
436}
437
438void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
439 VisitVarDecl(D);
440 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000441 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000442
443
Chris Lattnerea5ce472009-04-27 07:35:58 +0000444 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
445 // we dynamically check for the properties that we optimize for, but don't
446 // know are true of all PARM_VAR_DECLs.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000447 if (!D->getDeclaratorInfo() &&
448 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000449 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000450 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000451 D->getAccess() == AS_none &&
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000452 D->getPCHLevel() == 0 &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000453 D->getStorageClass() == 0 &&
454 !D->hasCXXDirectInitializer() && // Can params have this ever?
455 D->getObjCDeclQualifier() == 0)
456 AbbrevToUse = Writer.getParmVarDeclAbbrev();
457
458 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
459 // just us assuming it.
460 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
461 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
462 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
463 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
464 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
465 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000466}
467
468void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
469 VisitParmVarDecl(D);
470 Writer.AddTypeRef(D->getOriginalType(), Record);
471 Code = pch::DECL_ORIGINAL_PARM_VAR;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000472 AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000473}
474
475void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
476 VisitDecl(D);
477 Writer.AddStmt(D->getAsmString());
478 Code = pch::DECL_FILE_SCOPE_ASM;
479}
480
481void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
482 VisitDecl(D);
483 Writer.AddStmt(D->getBody());
484 Record.push_back(D->param_size());
485 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
486 P != PEnd; ++P)
487 Writer.AddDeclRef(*P, Record);
488 Code = pch::DECL_BLOCK;
489}
490
491/// \brief Emit the DeclContext part of a declaration context decl.
492///
493/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
494/// block for this declaration context is stored. May be 0 to indicate
495/// that there are no declarations stored within this context.
496///
497/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
498/// block for this declaration context is stored. May be 0 to indicate
499/// that there are no declarations visible from this context. Note
500/// that this value will not be emitted for non-primary declaration
501/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000502void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000503 uint64_t VisibleOffset) {
504 Record.push_back(LexicalOffset);
505 Record.push_back(VisibleOffset);
506}
507
508
509//===----------------------------------------------------------------------===//
510// PCHWriter Implementation
511//===----------------------------------------------------------------------===//
512
Chris Lattnerea5ce472009-04-27 07:35:58 +0000513void PCHWriter::WriteDeclsBlockAbbrevs() {
514 using namespace llvm;
515 // Abbreviation for DECL_PARM_VAR.
516 BitCodeAbbrev *Abv = new BitCodeAbbrev();
517 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
518
519 // Decl
520 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
521 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
522 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
523 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
524 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
525 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000526 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000527 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Douglas Gregor7d1d49d2009-10-16 20:01:17 +0000528 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnerea5ce472009-04-27 07:35:58 +0000530 // NamedDecl
531 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
532 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
533 // ValueDecl
534 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000535 // DeclaratorDecl
536 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000537 // VarDecl
538 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
539 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
540 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
541 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
542 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000543 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
544 // ParmVarDecl
545 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnerea5ce472009-04-27 07:35:58 +0000547 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
548}
549
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000550/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
551/// consumers of the AST.
552///
553/// Such decls will always be deserialized from the PCH file, so we would like
554/// this to be as restrictive as possible. Currently the predicate is driven by
555/// code generation requirements, if other clients have a different notion of
556/// what is "required" then we may have to consider an alternate scheme where
557/// clients can iterate over the top-level decls and get information on them,
558/// without necessary deserializing them. We could explicitly require such
559/// clients to use a separate API call to "realize" the decl. This should be
560/// relatively painless since they would presumably only do it for top-level
561/// decls.
562//
563// FIXME: This predicate is essentially IRgen's predicate to determine whether a
564// declaration can be deferred. Merge them somehow.
565static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
566 // File scoped assembly must be seen.
567 if (isa<FileScopeAsmDecl>(D))
568 return true;
569
570 // Otherwise if this isn't a function or a file scoped variable it doesn't
571 // need to be seen.
572 if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
573 if (!VD->isFileVarDecl())
574 return false;
575 } else if (!isa<FunctionDecl>(D))
576 return false;
577
578 // Aliases and used decls must be seen.
579 if (D->hasAttr<AliasAttr>() || D->hasAttr<UsedAttr>())
580 return true;
581
582 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
583 // Forward declarations don't need to be seen.
584 if (!FD->isThisDeclarationADefinition())
585 return false;
586
587 // Constructors and destructors must be seen.
588 if (FD->hasAttr<ConstructorAttr>() || FD->hasAttr<DestructorAttr>())
589 return true;
590
591 // Otherwise, this is required unless it is static.
592 //
593 // FIXME: Inlines.
594 return FD->getStorageClass() != FunctionDecl::Static;
595 } else {
596 const VarDecl *VD = cast<VarDecl>(D);
597
598 // In C++, this doesn't need to be seen if it is marked "extern".
599 if (Context.getLangOptions().CPlusPlus && !VD->getInit() &&
600 (VD->getStorageClass() == VarDecl::Extern ||
601 VD->isExternC()))
602 return false;
603
604 // In C, this doesn't need to be seen unless it is a definition.
605 if (!Context.getLangOptions().CPlusPlus && !VD->getInit())
606 return false;
607
608 // Otherwise, this is required unless it is static.
609 return VD->getStorageClass() != VarDecl::Static;
610 }
611}
612
Chris Lattner12b1c762009-04-27 06:16:06 +0000613/// \brief Write a block containing all of the declarations.
614void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
615 // Enter the declarations block.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000616 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3);
Chris Lattner12b1c762009-04-27 06:16:06 +0000617
Chris Lattnerea5ce472009-04-27 07:35:58 +0000618 // Output the abbreviations that we will use in this block.
619 WriteDeclsBlockAbbrevs();
Mike Stump1eb44332009-09-09 15:08:12 +0000620
Chris Lattner12b1c762009-04-27 06:16:06 +0000621 // Emit all of the declarations.
622 RecordData Record;
623 PCHDeclWriter W(*this, Context, Record);
624 while (!DeclsToEmit.empty()) {
625 // Pull the next declaration off the queue
626 Decl *D = DeclsToEmit.front();
627 DeclsToEmit.pop();
628
629 // If this declaration is also a DeclContext, write blocks for the
630 // declarations that lexically stored inside its context and those
631 // declarations that are visible from its context. These blocks
632 // are written before the declaration itself so that we can put
633 // their offsets into the record for the declaration.
634 uint64_t LexicalOffset = 0;
635 uint64_t VisibleOffset = 0;
636 DeclContext *DC = dyn_cast<DeclContext>(D);
637 if (DC) {
638 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
639 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
640 }
641
642 // Determine the ID for this declaration
Zhongxing Xu44dfc982009-06-01 00:50:23 +0000643 pch::DeclID &ID = DeclIDs[D];
Chris Lattner12b1c762009-04-27 06:16:06 +0000644 if (ID == 0)
645 ID = DeclIDs.size();
646
647 unsigned Index = ID - 1;
648
649 // Record the offset for this declaration
650 if (DeclOffsets.size() == Index)
651 DeclOffsets.push_back(Stream.GetCurrentBitNo());
652 else if (DeclOffsets.size() < Index) {
653 DeclOffsets.resize(Index+1);
654 DeclOffsets[Index] = Stream.GetCurrentBitNo();
655 }
656
657 // Build and emit a record for this declaration
658 Record.clear();
659 W.Code = (pch::DeclCode)0;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000660 W.AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000661 W.Visit(D);
662 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
663
664 if (!W.Code) {
665 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
666 D->getDeclKindName());
667 assert(false && "Unhandled declaration kind while generating PCH");
668 exit(-1);
669 }
Chris Lattnerea5ce472009-04-27 07:35:58 +0000670 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Mike Stump1eb44332009-09-09 15:08:12 +0000671
Chris Lattner12b1c762009-04-27 06:16:06 +0000672 // If the declaration had any attributes, write them now.
673 if (D->hasAttrs())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000674 WriteAttributeRecord(D->getAttrs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000675
676 // Flush any expressions that were written as part of this declaration.
677 FlushStmts();
Mike Stump1eb44332009-09-09 15:08:12 +0000678
Daniel Dunbare24d38f2009-09-17 03:06:51 +0000679 // Note "external" declarations so that we can add them to a record in the
680 // PCH file later.
681 //
682 // FIXME: This should be renamed, the predicate is much more complicated.
683 if (isRequiredDecl(D, Context))
Chris Lattner12b1c762009-04-27 06:16:06 +0000684 ExternalDefinitions.push_back(ID);
685 }
686
687 // Exit the declarations block
688 Stream.ExitBlock();
689}