blob: 7a15abf0a01c6074e7ce60ff51685a232c94a193 [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 Kyrtzidisd4a7e542009-08-19 01:28:35 +000017#include "clang/AST/TypeLoc.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)
163#define TYPELOC(CLASS, PARENT, TYPE) \
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
174void TypeLocWriter::VisitDefaultTypeSpecLoc(DefaultTypeSpecLoc TyLoc) {
175 Writer.AddSourceLocation(TyLoc.getStartLoc(), Record);
176}
177void TypeLocWriter::VisitTypedefLoc(TypedefLoc TyLoc) {
178 Writer.AddSourceLocation(TyLoc.getNameLoc(), Record);
179}
180void TypeLocWriter::VisitPointerLoc(PointerLoc TyLoc) {
181 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
182}
183void TypeLocWriter::VisitBlockPointerLoc(BlockPointerLoc TyLoc) {
184 Writer.AddSourceLocation(TyLoc.getCaretLoc(), Record);
185}
186void TypeLocWriter::VisitMemberPointerLoc(MemberPointerLoc TyLoc) {
187 Writer.AddSourceLocation(TyLoc.getStarLoc(), Record);
188}
189void TypeLocWriter::VisitReferenceLoc(ReferenceLoc TyLoc) {
190 Writer.AddSourceLocation(TyLoc.getAmpLoc(), Record);
191}
192void TypeLocWriter::VisitFunctionLoc(FunctionLoc TyLoc) {
193 Writer.AddSourceLocation(TyLoc.getLParenLoc(), Record);
194 Writer.AddSourceLocation(TyLoc.getRParenLoc(), Record);
195 for (unsigned i = 0, e = TyLoc.getNumArgs(); i != e; ++i)
196 Writer.AddDeclRef(TyLoc.getArg(i), Record);
197}
198void TypeLocWriter::VisitArrayLoc(ArrayLoc TyLoc) {
199 Writer.AddSourceLocation(TyLoc.getLBracketLoc(), Record);
200 Writer.AddSourceLocation(TyLoc.getRBracketLoc(), Record);
201 Record.push_back(TyLoc.getSizeExpr() ? 1 : 0);
202 if (TyLoc.getSizeExpr())
203 Writer.AddStmt(TyLoc.getSizeExpr());
204}
205
206void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
207 VisitValueDecl(D);
208 DeclaratorInfo *DInfo = D->getDeclaratorInfo();
209 if (DInfo == 0) {
210 Writer.AddTypeRef(QualType(), Record);
211 return;
212 }
Mike Stump1eb44332009-09-09 15:08:12 +0000213
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000214 Writer.AddTypeRef(DInfo->getTypeLoc().getSourceType(), Record);
215 TypeLocWriter TLW(Writer, Record);
216 for (TypeLoc TL = DInfo->getTypeLoc(); !TL.isNull(); TL = TL.getNextTypeLoc())
217 TLW.Visit(TL);
218}
Chris Lattner12b1c762009-04-27 06:16:06 +0000219
220void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000221 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000222 Record.push_back(D->isThisDeclarationADefinition());
223 if (D->isThisDeclarationADefinition())
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000224 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000225 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
226 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
227 Record.push_back(D->isInline());
228 Record.push_back(D->isC99InlineDefinition());
Anders Carlsson77b7f1d2009-05-14 22:15:41 +0000229 Record.push_back(D->isVirtualAsWritten());
Chris Lattner12b1c762009-04-27 06:16:06 +0000230 Record.push_back(D->isPure());
Anders Carlssona75e8532009-05-14 21:46:00 +0000231 Record.push_back(D->hasInheritedPrototype());
232 Record.push_back(D->hasWrittenPrototype());
Chris Lattner12b1c762009-04-27 06:16:06 +0000233 Record.push_back(D->isDeleted());
Argyrios Kyrtzidis8cff90e2009-06-20 08:09:34 +0000234 Writer.AddSourceLocation(D->getLocEnd(), Record);
Douglas Gregor1eee0e72009-05-14 21:06:31 +0000235 // FIXME: C++ TemplateOrInstantiation
Chris Lattner12b1c762009-04-27 06:16:06 +0000236 Record.push_back(D->param_size());
237 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
238 P != PEnd; ++P)
239 Writer.AddDeclRef(*P, Record);
240 Code = pch::DECL_FUNCTION;
241}
242
243void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
244 VisitNamedDecl(D);
245 // FIXME: convert to LazyStmtPtr?
Mike Stump1eb44332009-09-09 15:08:12 +0000246 // Unlike C/C++, method bodies will never be in header files.
Chris Lattner12b1c762009-04-27 06:16:06 +0000247 Record.push_back(D->getBody() != 0);
248 if (D->getBody() != 0) {
Argyrios Kyrtzidis6fb0aee2009-06-30 02:35:26 +0000249 Writer.AddStmt(D->getBody());
Chris Lattner12b1c762009-04-27 06:16:06 +0000250 Writer.AddDeclRef(D->getSelfDecl(), Record);
251 Writer.AddDeclRef(D->getCmdDecl(), Record);
252 }
253 Record.push_back(D->isInstanceMethod());
254 Record.push_back(D->isVariadic());
255 Record.push_back(D->isSynthesized());
256 // FIXME: stable encoding for @required/@optional
Mike Stump1eb44332009-09-09 15:08:12 +0000257 Record.push_back(D->getImplementationControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000258 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
Mike Stump1eb44332009-09-09 15:08:12 +0000259 Record.push_back(D->getObjCDeclQualifier());
Chris Lattner12b1c762009-04-27 06:16:06 +0000260 Writer.AddTypeRef(D->getResultType(), Record);
261 Writer.AddSourceLocation(D->getLocEnd(), Record);
262 Record.push_back(D->param_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000263 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000264 PEnd = D->param_end(); P != PEnd; ++P)
265 Writer.AddDeclRef(*P, Record);
266 Code = pch::DECL_OBJC_METHOD;
267}
268
269void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
270 VisitNamedDecl(D);
271 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
272 // Abstract class (no need to define a stable pch::DECL code).
273}
274
275void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
276 VisitObjCContainerDecl(D);
277 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
278 Writer.AddDeclRef(D->getSuperClass(), Record);
279 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000280 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000281 PEnd = D->protocol_end();
282 P != PEnd; ++P)
283 Writer.AddDeclRef(*P, Record);
284 Record.push_back(D->ivar_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000285 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
Chris Lattner12b1c762009-04-27 06:16:06 +0000286 IEnd = D->ivar_end(); I != IEnd; ++I)
287 Writer.AddDeclRef(*I, Record);
288 Writer.AddDeclRef(D->getCategoryList(), Record);
289 Record.push_back(D->isForwardDecl());
290 Record.push_back(D->isImplicitInterfaceDecl());
291 Writer.AddSourceLocation(D->getClassLoc(), Record);
292 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
293 Writer.AddSourceLocation(D->getLocEnd(), Record);
294 Code = pch::DECL_OBJC_INTERFACE;
295}
296
297void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
298 VisitFieldDecl(D);
299 // FIXME: stable encoding for @public/@private/@protected/@package
Mike Stump1eb44332009-09-09 15:08:12 +0000300 Record.push_back(D->getAccessControl());
Chris Lattner12b1c762009-04-27 06:16:06 +0000301 Code = pch::DECL_OBJC_IVAR;
302}
303
304void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
305 VisitObjCContainerDecl(D);
306 Record.push_back(D->isForwardDecl());
307 Writer.AddSourceLocation(D->getLocEnd(), Record);
308 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000309 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000310 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
311 Writer.AddDeclRef(*I, Record);
312 Code = pch::DECL_OBJC_PROTOCOL;
313}
314
315void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
316 VisitFieldDecl(D);
317 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
318}
319
320void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
321 VisitDecl(D);
322 Record.push_back(D->size());
323 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
324 Writer.AddDeclRef(*I, Record);
325 Code = pch::DECL_OBJC_CLASS;
326}
327
328void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
329 VisitDecl(D);
330 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000331 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000332 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
333 Writer.AddDeclRef(*I, Record);
334 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
335}
336
337void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
338 VisitObjCContainerDecl(D);
339 Writer.AddDeclRef(D->getClassInterface(), Record);
340 Record.push_back(D->protocol_size());
Mike Stump1eb44332009-09-09 15:08:12 +0000341 for (ObjCProtocolDecl::protocol_iterator
Chris Lattner12b1c762009-04-27 06:16:06 +0000342 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
343 Writer.AddDeclRef(*I, Record);
344 Writer.AddDeclRef(D->getNextClassCategory(), Record);
345 Writer.AddSourceLocation(D->getLocEnd(), Record);
346 Code = pch::DECL_OBJC_CATEGORY;
347}
348
349void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
350 VisitNamedDecl(D);
351 Writer.AddDeclRef(D->getClassInterface(), Record);
352 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
353}
354
355void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
356 VisitNamedDecl(D);
357 Writer.AddTypeRef(D->getType(), Record);
358 // FIXME: stable encoding
359 Record.push_back((unsigned)D->getPropertyAttributes());
360 // FIXME: stable encoding
361 Record.push_back((unsigned)D->getPropertyImplementation());
362 Writer.AddDeclarationName(D->getGetterName(), Record);
363 Writer.AddDeclarationName(D->getSetterName(), Record);
364 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
365 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
366 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
367 Code = pch::DECL_OBJC_PROPERTY;
368}
369
370void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
Argyrios Kyrtzidisaecae622009-07-27 19:04:32 +0000371 VisitObjCContainerDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000372 Writer.AddDeclRef(D->getClassInterface(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000373 // Abstract class (no need to define a stable pch::DECL code).
374}
375
376void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
377 VisitObjCImplDecl(D);
378 Writer.AddIdentifierRef(D->getIdentifier(), Record);
379 Code = pch::DECL_OBJC_CATEGORY_IMPL;
380}
381
382void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
383 VisitObjCImplDecl(D);
384 Writer.AddDeclRef(D->getSuperClass(), Record);
385 Code = pch::DECL_OBJC_IMPLEMENTATION;
386}
387
388void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
389 VisitDecl(D);
390 Writer.AddSourceLocation(D->getLocStart(), Record);
391 Writer.AddDeclRef(D->getPropertyDecl(), Record);
392 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
393 Code = pch::DECL_OBJC_PROPERTY_IMPL;
394}
395
396void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000397 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000398 Record.push_back(D->isMutable());
399 Record.push_back(D->getBitWidth()? 1 : 0);
400 if (D->getBitWidth())
401 Writer.AddStmt(D->getBitWidth());
402 Code = pch::DECL_FIELD;
403}
404
405void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000406 VisitDeclaratorDecl(D);
Chris Lattner12b1c762009-04-27 06:16:06 +0000407 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
408 Record.push_back(D->isThreadSpecified());
409 Record.push_back(D->hasCXXDirectInitializer());
410 Record.push_back(D->isDeclaredInCondition());
411 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
Chris Lattner12b1c762009-04-27 06:16:06 +0000412 Record.push_back(D->getInit()? 1 : 0);
413 if (D->getInit())
414 Writer.AddStmt(D->getInit());
415 Code = pch::DECL_VAR;
416}
417
418void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
419 VisitVarDecl(D);
420 Code = pch::DECL_IMPLICIT_PARAM;
421}
422
423void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
424 VisitVarDecl(D);
425 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
Chris Lattner12b1c762009-04-27 06:16:06 +0000426 Code = pch::DECL_PARM_VAR;
Mike Stump1eb44332009-09-09 15:08:12 +0000427
428
Chris Lattnerea5ce472009-04-27 07:35:58 +0000429 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
430 // we dynamically check for the properties that we optimize for, but don't
431 // know are true of all PARM_VAR_DECLs.
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000432 if (!D->getDeclaratorInfo() &&
433 !D->hasAttrs() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000434 !D->isImplicit() &&
Douglas Gregore0762c92009-06-19 23:52:42 +0000435 !D->isUsed() &&
Chris Lattnerea5ce472009-04-27 07:35:58 +0000436 D->getAccess() == AS_none &&
437 D->getStorageClass() == 0 &&
438 !D->hasCXXDirectInitializer() && // Can params have this ever?
439 D->getObjCDeclQualifier() == 0)
440 AbbrevToUse = Writer.getParmVarDeclAbbrev();
441
442 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
443 // just us assuming it.
444 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
445 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
446 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
447 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
448 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
449 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000450}
451
452void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
453 VisitParmVarDecl(D);
454 Writer.AddTypeRef(D->getOriginalType(), Record);
455 Code = pch::DECL_ORIGINAL_PARM_VAR;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000456 AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000457}
458
459void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
460 VisitDecl(D);
461 Writer.AddStmt(D->getAsmString());
462 Code = pch::DECL_FILE_SCOPE_ASM;
463}
464
465void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
466 VisitDecl(D);
467 Writer.AddStmt(D->getBody());
468 Record.push_back(D->param_size());
469 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
470 P != PEnd; ++P)
471 Writer.AddDeclRef(*P, Record);
472 Code = pch::DECL_BLOCK;
473}
474
475/// \brief Emit the DeclContext part of a declaration context decl.
476///
477/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
478/// block for this declaration context is stored. May be 0 to indicate
479/// that there are no declarations stored within this context.
480///
481/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
482/// block for this declaration context is stored. May be 0 to indicate
483/// that there are no declarations visible from this context. Note
484/// that this value will not be emitted for non-primary declaration
485/// contexts.
Mike Stump1eb44332009-09-09 15:08:12 +0000486void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
Chris Lattner12b1c762009-04-27 06:16:06 +0000487 uint64_t VisibleOffset) {
488 Record.push_back(LexicalOffset);
489 Record.push_back(VisibleOffset);
490}
491
492
493//===----------------------------------------------------------------------===//
494// PCHWriter Implementation
495//===----------------------------------------------------------------------===//
496
Chris Lattnerea5ce472009-04-27 07:35:58 +0000497void PCHWriter::WriteDeclsBlockAbbrevs() {
498 using namespace llvm;
499 // Abbreviation for DECL_PARM_VAR.
500 BitCodeAbbrev *Abv = new BitCodeAbbrev();
501 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
502
503 // Decl
504 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
505 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
506 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
507 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
508 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
509 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
Douglas Gregore0762c92009-06-19 23:52:42 +0000510 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
Chris Lattnerea5ce472009-04-27 07:35:58 +0000511 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
Mike Stump1eb44332009-09-09 15:08:12 +0000512
Chris Lattnerea5ce472009-04-27 07:35:58 +0000513 // NamedDecl
514 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
515 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
516 // ValueDecl
517 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
Argyrios Kyrtzidisd4a7e542009-08-19 01:28:35 +0000518 // DeclaratorDecl
519 Abv->Add(BitCodeAbbrevOp(pch::PREDEF_TYPE_NULL_ID)); // InfoType
Chris Lattnerea5ce472009-04-27 07:35:58 +0000520 // VarDecl
521 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
522 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
523 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
524 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
525 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
Chris Lattnerea5ce472009-04-27 07:35:58 +0000526 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
527 // ParmVarDecl
528 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
Mike Stump1eb44332009-09-09 15:08:12 +0000529
Chris Lattnerea5ce472009-04-27 07:35:58 +0000530 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
531}
532
Chris Lattner12b1c762009-04-27 06:16:06 +0000533/// \brief Write a block containing all of the declarations.
534void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
535 // Enter the declarations block.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000536 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3);
Chris Lattner12b1c762009-04-27 06:16:06 +0000537
Chris Lattnerea5ce472009-04-27 07:35:58 +0000538 // Output the abbreviations that we will use in this block.
539 WriteDeclsBlockAbbrevs();
Mike Stump1eb44332009-09-09 15:08:12 +0000540
Chris Lattner12b1c762009-04-27 06:16:06 +0000541 // Emit all of the declarations.
542 RecordData Record;
543 PCHDeclWriter W(*this, Context, Record);
544 while (!DeclsToEmit.empty()) {
545 // Pull the next declaration off the queue
546 Decl *D = DeclsToEmit.front();
547 DeclsToEmit.pop();
548
549 // If this declaration is also a DeclContext, write blocks for the
550 // declarations that lexically stored inside its context and those
551 // declarations that are visible from its context. These blocks
552 // are written before the declaration itself so that we can put
553 // their offsets into the record for the declaration.
554 uint64_t LexicalOffset = 0;
555 uint64_t VisibleOffset = 0;
556 DeclContext *DC = dyn_cast<DeclContext>(D);
557 if (DC) {
558 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
559 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
560 }
561
562 // Determine the ID for this declaration
Zhongxing Xu44dfc982009-06-01 00:50:23 +0000563 pch::DeclID &ID = DeclIDs[D];
Chris Lattner12b1c762009-04-27 06:16:06 +0000564 if (ID == 0)
565 ID = DeclIDs.size();
566
567 unsigned Index = ID - 1;
568
569 // Record the offset for this declaration
570 if (DeclOffsets.size() == Index)
571 DeclOffsets.push_back(Stream.GetCurrentBitNo());
572 else if (DeclOffsets.size() < Index) {
573 DeclOffsets.resize(Index+1);
574 DeclOffsets[Index] = Stream.GetCurrentBitNo();
575 }
576
577 // Build and emit a record for this declaration
578 Record.clear();
579 W.Code = (pch::DeclCode)0;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000580 W.AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000581 W.Visit(D);
582 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
583
584 if (!W.Code) {
585 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
586 D->getDeclKindName());
587 assert(false && "Unhandled declaration kind while generating PCH");
588 exit(-1);
589 }
Chris Lattnerea5ce472009-04-27 07:35:58 +0000590 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattner12b1c762009-04-27 06:16:06 +0000592 // If the declaration had any attributes, write them now.
593 if (D->hasAttrs())
Argyrios Kyrtzidis40b598e2009-06-30 02:34:44 +0000594 WriteAttributeRecord(D->getAttrs());
Chris Lattner12b1c762009-04-27 06:16:06 +0000595
596 // Flush any expressions that were written as part of this declaration.
597 FlushStmts();
Mike Stump1eb44332009-09-09 15:08:12 +0000598
Chris Lattner12b1c762009-04-27 06:16:06 +0000599 // Note external declarations so that we can add them to a record
600 // in the PCH file later.
601 if (isa<FileScopeAsmDecl>(D))
602 ExternalDefinitions.push_back(ID);
603 }
604
605 // Exit the declarations block
606 Stream.ExitBlock();
607}