blob: 799c77b03fc18fba083c49c8407470c3d525e97e [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"
17#include "llvm/Bitcode/BitstreamWriter.h"
18using namespace clang;
19
20//===----------------------------------------------------------------------===//
21// Declaration serialization
22//===----------------------------------------------------------------------===//
23
24namespace {
25 class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> {
26
27 PCHWriter &Writer;
28 ASTContext &Context;
29 PCHWriter::RecordData &Record;
30
31 public:
32 pch::DeclCode Code;
Chris Lattnerea5ce472009-04-27 07:35:58 +000033 unsigned AbbrevToUse;
Chris Lattner12b1c762009-04-27 06:16:06 +000034
35 PCHDeclWriter(PCHWriter &Writer, ASTContext &Context,
36 PCHWriter::RecordData &Record)
Chris Lattnerea5ce472009-04-27 07:35:58 +000037 : Writer(Writer), Context(Context), Record(Record) {
38 }
Chris Lattner12b1c762009-04-27 06:16:06 +000039
40 void VisitDecl(Decl *D);
41 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
42 void VisitNamedDecl(NamedDecl *D);
43 void VisitTypeDecl(TypeDecl *D);
44 void VisitTypedefDecl(TypedefDecl *D);
45 void VisitTagDecl(TagDecl *D);
46 void VisitEnumDecl(EnumDecl *D);
47 void VisitRecordDecl(RecordDecl *D);
48 void VisitValueDecl(ValueDecl *D);
49 void VisitEnumConstantDecl(EnumConstantDecl *D);
50 void VisitFunctionDecl(FunctionDecl *D);
51 void VisitFieldDecl(FieldDecl *D);
52 void VisitVarDecl(VarDecl *D);
53 void VisitImplicitParamDecl(ImplicitParamDecl *D);
54 void VisitParmVarDecl(ParmVarDecl *D);
55 void VisitOriginalParmVarDecl(OriginalParmVarDecl *D);
56 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
57 void VisitBlockDecl(BlockDecl *D);
58 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
59 uint64_t VisibleOffset);
60 void VisitObjCMethodDecl(ObjCMethodDecl *D);
61 void VisitObjCContainerDecl(ObjCContainerDecl *D);
62 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
63 void VisitObjCIvarDecl(ObjCIvarDecl *D);
64 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
65 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
66 void VisitObjCClassDecl(ObjCClassDecl *D);
67 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
68 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
69 void VisitObjCImplDecl(ObjCImplDecl *D);
70 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
71 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
72 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
73 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
74 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
75 };
76}
77
78void PCHDeclWriter::VisitDecl(Decl *D) {
79 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
80 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
81 Writer.AddSourceLocation(D->getLocation(), Record);
82 Record.push_back(D->isInvalidDecl());
83 Record.push_back(D->hasAttrs());
84 Record.push_back(D->isImplicit());
85 Record.push_back(D->getAccess());
86}
87
88void PCHDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
89 VisitDecl(D);
90 Code = pch::DECL_TRANSLATION_UNIT;
91}
92
93void PCHDeclWriter::VisitNamedDecl(NamedDecl *D) {
94 VisitDecl(D);
95 Writer.AddDeclarationName(D->getDeclName(), Record);
96}
97
98void PCHDeclWriter::VisitTypeDecl(TypeDecl *D) {
99 VisitNamedDecl(D);
100 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
101}
102
103void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
104 VisitTypeDecl(D);
105 Writer.AddTypeRef(D->getUnderlyingType(), Record);
106 Code = pch::DECL_TYPEDEF;
107}
108
109void PCHDeclWriter::VisitTagDecl(TagDecl *D) {
110 VisitTypeDecl(D);
111 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
112 Record.push_back(D->isDefinition());
113 Writer.AddDeclRef(D->getTypedefForAnonDecl(), Record);
114}
115
116void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) {
117 VisitTagDecl(D);
118 Writer.AddTypeRef(D->getIntegerType(), Record);
119 Code = pch::DECL_ENUM;
120}
121
122void PCHDeclWriter::VisitRecordDecl(RecordDecl *D) {
123 VisitTagDecl(D);
124 Record.push_back(D->hasFlexibleArrayMember());
125 Record.push_back(D->isAnonymousStructOrUnion());
126 Code = pch::DECL_RECORD;
127}
128
129void PCHDeclWriter::VisitValueDecl(ValueDecl *D) {
130 VisitNamedDecl(D);
131 Writer.AddTypeRef(D->getType(), Record);
132}
133
134void PCHDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
135 VisitValueDecl(D);
136 Record.push_back(D->getInitExpr()? 1 : 0);
137 if (D->getInitExpr())
138 Writer.AddStmt(D->getInitExpr());
139 Writer.AddAPSInt(D->getInitVal(), Record);
140 Code = pch::DECL_ENUM_CONSTANT;
141}
142
143void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
144 VisitValueDecl(D);
145 Record.push_back(D->isThisDeclarationADefinition());
146 if (D->isThisDeclarationADefinition())
147 Writer.AddStmt(D->getBody(Context));
148 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
149 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
150 Record.push_back(D->isInline());
151 Record.push_back(D->isC99InlineDefinition());
152 Record.push_back(D->isVirtual());
153 Record.push_back(D->isPure());
154 Record.push_back(D->inheritedPrototype());
155 Record.push_back(D->hasPrototype() && !D->inheritedPrototype());
156 Record.push_back(D->isDeleted());
157 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
158 Record.push_back(D->param_size());
159 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
160 P != PEnd; ++P)
161 Writer.AddDeclRef(*P, Record);
162 Code = pch::DECL_FUNCTION;
163}
164
165void PCHDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
166 VisitNamedDecl(D);
167 // FIXME: convert to LazyStmtPtr?
168 // Unlike C/C++, method bodies will never be in header files.
169 Record.push_back(D->getBody() != 0);
170 if (D->getBody() != 0) {
171 Writer.AddStmt(D->getBody(Context));
172 Writer.AddDeclRef(D->getSelfDecl(), Record);
173 Writer.AddDeclRef(D->getCmdDecl(), Record);
174 }
175 Record.push_back(D->isInstanceMethod());
176 Record.push_back(D->isVariadic());
177 Record.push_back(D->isSynthesized());
178 // FIXME: stable encoding for @required/@optional
179 Record.push_back(D->getImplementationControl());
180 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
181 Record.push_back(D->getObjCDeclQualifier());
182 Writer.AddTypeRef(D->getResultType(), Record);
183 Writer.AddSourceLocation(D->getLocEnd(), Record);
184 Record.push_back(D->param_size());
185 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
186 PEnd = D->param_end(); P != PEnd; ++P)
187 Writer.AddDeclRef(*P, Record);
188 Code = pch::DECL_OBJC_METHOD;
189}
190
191void PCHDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
192 VisitNamedDecl(D);
193 Writer.AddSourceLocation(D->getAtEndLoc(), Record);
194 // Abstract class (no need to define a stable pch::DECL code).
195}
196
197void PCHDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
198 VisitObjCContainerDecl(D);
199 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
200 Writer.AddDeclRef(D->getSuperClass(), Record);
201 Record.push_back(D->protocol_size());
202 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
203 PEnd = D->protocol_end();
204 P != PEnd; ++P)
205 Writer.AddDeclRef(*P, Record);
206 Record.push_back(D->ivar_size());
207 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
208 IEnd = D->ivar_end(); I != IEnd; ++I)
209 Writer.AddDeclRef(*I, Record);
210 Writer.AddDeclRef(D->getCategoryList(), Record);
211 Record.push_back(D->isForwardDecl());
212 Record.push_back(D->isImplicitInterfaceDecl());
213 Writer.AddSourceLocation(D->getClassLoc(), Record);
214 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
215 Writer.AddSourceLocation(D->getLocEnd(), Record);
216 Code = pch::DECL_OBJC_INTERFACE;
217}
218
219void PCHDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
220 VisitFieldDecl(D);
221 // FIXME: stable encoding for @public/@private/@protected/@package
222 Record.push_back(D->getAccessControl());
223 Code = pch::DECL_OBJC_IVAR;
224}
225
226void PCHDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
227 VisitObjCContainerDecl(D);
228 Record.push_back(D->isForwardDecl());
229 Writer.AddSourceLocation(D->getLocEnd(), Record);
230 Record.push_back(D->protocol_size());
231 for (ObjCProtocolDecl::protocol_iterator
232 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
233 Writer.AddDeclRef(*I, Record);
234 Code = pch::DECL_OBJC_PROTOCOL;
235}
236
237void PCHDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
238 VisitFieldDecl(D);
239 Code = pch::DECL_OBJC_AT_DEFS_FIELD;
240}
241
242void PCHDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
243 VisitDecl(D);
244 Record.push_back(D->size());
245 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
246 Writer.AddDeclRef(*I, Record);
247 Code = pch::DECL_OBJC_CLASS;
248}
249
250void PCHDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
251 VisitDecl(D);
252 Record.push_back(D->protocol_size());
253 for (ObjCProtocolDecl::protocol_iterator
254 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
255 Writer.AddDeclRef(*I, Record);
256 Code = pch::DECL_OBJC_FORWARD_PROTOCOL;
257}
258
259void PCHDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
260 VisitObjCContainerDecl(D);
261 Writer.AddDeclRef(D->getClassInterface(), Record);
262 Record.push_back(D->protocol_size());
263 for (ObjCProtocolDecl::protocol_iterator
264 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
265 Writer.AddDeclRef(*I, Record);
266 Writer.AddDeclRef(D->getNextClassCategory(), Record);
267 Writer.AddSourceLocation(D->getLocEnd(), Record);
268 Code = pch::DECL_OBJC_CATEGORY;
269}
270
271void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
272 VisitNamedDecl(D);
273 Writer.AddDeclRef(D->getClassInterface(), Record);
274 Code = pch::DECL_OBJC_COMPATIBLE_ALIAS;
275}
276
277void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
278 VisitNamedDecl(D);
279 Writer.AddTypeRef(D->getType(), Record);
280 // FIXME: stable encoding
281 Record.push_back((unsigned)D->getPropertyAttributes());
282 // FIXME: stable encoding
283 Record.push_back((unsigned)D->getPropertyImplementation());
284 Writer.AddDeclarationName(D->getGetterName(), Record);
285 Writer.AddDeclarationName(D->getSetterName(), Record);
286 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
287 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
288 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
289 Code = pch::DECL_OBJC_PROPERTY;
290}
291
292void PCHDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
293 VisitNamedDecl(D);
294 Writer.AddDeclRef(D->getClassInterface(), Record);
295 Writer.AddSourceLocation(D->getLocEnd(), Record);
296 // Abstract class (no need to define a stable pch::DECL code).
297}
298
299void PCHDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
300 VisitObjCImplDecl(D);
301 Writer.AddIdentifierRef(D->getIdentifier(), Record);
302 Code = pch::DECL_OBJC_CATEGORY_IMPL;
303}
304
305void PCHDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
306 VisitObjCImplDecl(D);
307 Writer.AddDeclRef(D->getSuperClass(), Record);
308 Code = pch::DECL_OBJC_IMPLEMENTATION;
309}
310
311void PCHDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
312 VisitDecl(D);
313 Writer.AddSourceLocation(D->getLocStart(), Record);
314 Writer.AddDeclRef(D->getPropertyDecl(), Record);
315 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
316 Code = pch::DECL_OBJC_PROPERTY_IMPL;
317}
318
319void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) {
320 VisitValueDecl(D);
321 Record.push_back(D->isMutable());
322 Record.push_back(D->getBitWidth()? 1 : 0);
323 if (D->getBitWidth())
324 Writer.AddStmt(D->getBitWidth());
325 Code = pch::DECL_FIELD;
326}
327
328void PCHDeclWriter::VisitVarDecl(VarDecl *D) {
329 VisitValueDecl(D);
330 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
331 Record.push_back(D->isThreadSpecified());
332 Record.push_back(D->hasCXXDirectInitializer());
333 Record.push_back(D->isDeclaredInCondition());
334 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
335 Writer.AddSourceLocation(D->getTypeSpecStartLoc(), Record);
336 Record.push_back(D->getInit()? 1 : 0);
337 if (D->getInit())
338 Writer.AddStmt(D->getInit());
339 Code = pch::DECL_VAR;
340}
341
342void PCHDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
343 VisitVarDecl(D);
344 Code = pch::DECL_IMPLICIT_PARAM;
345}
346
347void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
348 VisitVarDecl(D);
349 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
350 // FIXME: emit default argument (C++)
351 // FIXME: why isn't the "default argument" just stored as the initializer
352 // in VarDecl?
353 Code = pch::DECL_PARM_VAR;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000354
355
356 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
357 // we dynamically check for the properties that we optimize for, but don't
358 // know are true of all PARM_VAR_DECLs.
359 if (!D->hasAttrs() &&
360 !D->isImplicit() &&
361 D->getAccess() == AS_none &&
362 D->getStorageClass() == 0 &&
363 !D->hasCXXDirectInitializer() && // Can params have this ever?
364 D->getObjCDeclQualifier() == 0)
365 AbbrevToUse = Writer.getParmVarDeclAbbrev();
366
367 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
368 // just us assuming it.
369 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
370 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
371 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
372 assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition");
373 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
374 assert(D->getInit() == 0 && "PARM_VAR_DECL never has init");
Chris Lattner12b1c762009-04-27 06:16:06 +0000375}
376
377void PCHDeclWriter::VisitOriginalParmVarDecl(OriginalParmVarDecl *D) {
378 VisitParmVarDecl(D);
379 Writer.AddTypeRef(D->getOriginalType(), Record);
380 Code = pch::DECL_ORIGINAL_PARM_VAR;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000381 AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000382}
383
384void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
385 VisitDecl(D);
386 Writer.AddStmt(D->getAsmString());
387 Code = pch::DECL_FILE_SCOPE_ASM;
388}
389
390void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) {
391 VisitDecl(D);
392 Writer.AddStmt(D->getBody());
393 Record.push_back(D->param_size());
394 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
395 P != PEnd; ++P)
396 Writer.AddDeclRef(*P, Record);
397 Code = pch::DECL_BLOCK;
398}
399
400/// \brief Emit the DeclContext part of a declaration context decl.
401///
402/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
403/// block for this declaration context is stored. May be 0 to indicate
404/// that there are no declarations stored within this context.
405///
406/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
407/// block for this declaration context is stored. May be 0 to indicate
408/// that there are no declarations visible from this context. Note
409/// that this value will not be emitted for non-primary declaration
410/// contexts.
411void PCHDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
412 uint64_t VisibleOffset) {
413 Record.push_back(LexicalOffset);
414 Record.push_back(VisibleOffset);
415}
416
417
418//===----------------------------------------------------------------------===//
419// PCHWriter Implementation
420//===----------------------------------------------------------------------===//
421
Chris Lattnerea5ce472009-04-27 07:35:58 +0000422void PCHWriter::WriteDeclsBlockAbbrevs() {
423 using namespace llvm;
424 // Abbreviation for DECL_PARM_VAR.
425 BitCodeAbbrev *Abv = new BitCodeAbbrev();
426 Abv->Add(BitCodeAbbrevOp(pch::DECL_PARM_VAR));
427
428 // Decl
429 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
430 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
431 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
432 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
433 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
434 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
435 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
436
437 // NamedDecl
438 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
439 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
440 // ValueDecl
441 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
442 // VarDecl
443 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
444 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
445 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
446 Abv->Add(BitCodeAbbrevOp(0)); // isDeclaredInCondition
447 Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl
448 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeSpecStartLoc
449 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
450 // ParmVarDecl
451 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
452
453 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
454}
455
Chris Lattner12b1c762009-04-27 06:16:06 +0000456/// \brief Write a block containing all of the declarations.
457void PCHWriter::WriteDeclsBlock(ASTContext &Context) {
458 // Enter the declarations block.
Chris Lattnerea5ce472009-04-27 07:35:58 +0000459 Stream.EnterSubblock(pch::DECLS_BLOCK_ID, 3);
Chris Lattner12b1c762009-04-27 06:16:06 +0000460
Chris Lattnerea5ce472009-04-27 07:35:58 +0000461 // Output the abbreviations that we will use in this block.
462 WriteDeclsBlockAbbrevs();
463
Chris Lattner12b1c762009-04-27 06:16:06 +0000464 // Emit all of the declarations.
465 RecordData Record;
466 PCHDeclWriter W(*this, Context, Record);
467 while (!DeclsToEmit.empty()) {
468 // Pull the next declaration off the queue
469 Decl *D = DeclsToEmit.front();
470 DeclsToEmit.pop();
471
472 // If this declaration is also a DeclContext, write blocks for the
473 // declarations that lexically stored inside its context and those
474 // declarations that are visible from its context. These blocks
475 // are written before the declaration itself so that we can put
476 // their offsets into the record for the declaration.
477 uint64_t LexicalOffset = 0;
478 uint64_t VisibleOffset = 0;
479 DeclContext *DC = dyn_cast<DeclContext>(D);
480 if (DC) {
481 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
482 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
483 }
484
485 // Determine the ID for this declaration
486 pch::DeclID ID = DeclIDs[D];
487 if (ID == 0)
488 ID = DeclIDs.size();
489
490 unsigned Index = ID - 1;
491
492 // Record the offset for this declaration
493 if (DeclOffsets.size() == Index)
494 DeclOffsets.push_back(Stream.GetCurrentBitNo());
495 else if (DeclOffsets.size() < Index) {
496 DeclOffsets.resize(Index+1);
497 DeclOffsets[Index] = Stream.GetCurrentBitNo();
498 }
499
500 // Build and emit a record for this declaration
501 Record.clear();
502 W.Code = (pch::DeclCode)0;
Chris Lattnerea5ce472009-04-27 07:35:58 +0000503 W.AbbrevToUse = 0;
Chris Lattner12b1c762009-04-27 06:16:06 +0000504 W.Visit(D);
505 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
506
507 if (!W.Code) {
508 fprintf(stderr, "Cannot serialize declaration of kind %s\n",
509 D->getDeclKindName());
510 assert(false && "Unhandled declaration kind while generating PCH");
511 exit(-1);
512 }
Chris Lattnerea5ce472009-04-27 07:35:58 +0000513 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
514
Chris Lattner12b1c762009-04-27 06:16:06 +0000515 // If the declaration had any attributes, write them now.
516 if (D->hasAttrs())
517 WriteAttributeRecord(D->getAttrs());
518
519 // Flush any expressions that were written as part of this declaration.
520 FlushStmts();
521
522 // Note external declarations so that we can add them to a record
523 // in the PCH file later.
524 if (isa<FileScopeAsmDecl>(D))
525 ExternalDefinitions.push_back(ID);
526 }
527
528 // Exit the declarations block
529 Stream.ExitBlock();
530}