1 //===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements serialization for Declarations.
12 //===----------------------------------------------------------------------===//
14 #include "clang/Serialization/ASTWriter.h"
15 #include "ASTCommon.h"
16 #include "clang/AST/DeclVisitor.h"
17 #include "clang/AST/DeclCXX.h"
18 #include "clang/AST/DeclTemplate.h"
19 #include "clang/AST/Expr.h"
20 #include "clang/AST/DeclContextInternals.h"
21 #include "llvm/ADT/Twine.h"
22 #include "llvm/Bitcode/BitstreamWriter.h"
23 #include "llvm/Support/ErrorHandling.h"
24 using namespace clang;
25 using namespace serialization;
27 //===----------------------------------------------------------------------===//
28 // Declaration serialization
29 //===----------------------------------------------------------------------===//
32 class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
36 typedef ASTWriter::RecordData RecordData;
40 serialization::DeclCode Code;
43 ASTDeclWriter(ASTWriter &Writer, ASTContext &Context, RecordData &Record)
44 : Writer(Writer), Context(Context), Record(Record) {
49 void VisitDecl(Decl *D);
50 void VisitTranslationUnitDecl(TranslationUnitDecl *D);
51 void VisitNamedDecl(NamedDecl *D);
52 void VisitLabelDecl(LabelDecl *LD);
53 void VisitNamespaceDecl(NamespaceDecl *D);
54 void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
55 void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
56 void VisitTypeDecl(TypeDecl *D);
57 void VisitTypedefDecl(TypedefDecl *D);
58 void VisitTypeAliasDecl(TypeAliasDecl *D);
59 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
60 void VisitTagDecl(TagDecl *D);
61 void VisitEnumDecl(EnumDecl *D);
62 void VisitRecordDecl(RecordDecl *D);
63 void VisitCXXRecordDecl(CXXRecordDecl *D);
64 void VisitClassTemplateSpecializationDecl(
65 ClassTemplateSpecializationDecl *D);
66 void VisitClassTemplatePartialSpecializationDecl(
67 ClassTemplatePartialSpecializationDecl *D);
68 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
69 void VisitValueDecl(ValueDecl *D);
70 void VisitEnumConstantDecl(EnumConstantDecl *D);
71 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
72 void VisitDeclaratorDecl(DeclaratorDecl *D);
73 void VisitFunctionDecl(FunctionDecl *D);
74 void VisitCXXMethodDecl(CXXMethodDecl *D);
75 void VisitCXXConstructorDecl(CXXConstructorDecl *D);
76 void VisitCXXDestructorDecl(CXXDestructorDecl *D);
77 void VisitCXXConversionDecl(CXXConversionDecl *D);
78 void VisitFieldDecl(FieldDecl *D);
79 void VisitIndirectFieldDecl(IndirectFieldDecl *D);
80 void VisitVarDecl(VarDecl *D);
81 void VisitImplicitParamDecl(ImplicitParamDecl *D);
82 void VisitParmVarDecl(ParmVarDecl *D);
83 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
84 void VisitTemplateDecl(TemplateDecl *D);
85 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
86 void VisitClassTemplateDecl(ClassTemplateDecl *D);
87 void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
88 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
89 void VisitUsingDecl(UsingDecl *D);
90 void VisitUsingShadowDecl(UsingShadowDecl *D);
91 void VisitLinkageSpecDecl(LinkageSpecDecl *D);
92 void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
93 void VisitAccessSpecDecl(AccessSpecDecl *D);
94 void VisitFriendDecl(FriendDecl *D);
95 void VisitFriendTemplateDecl(FriendTemplateDecl *D);
96 void VisitStaticAssertDecl(StaticAssertDecl *D);
97 void VisitBlockDecl(BlockDecl *D);
99 void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
100 uint64_t VisibleOffset);
101 template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
104 // FIXME: Put in the same order is DeclNodes.td?
105 void VisitObjCMethodDecl(ObjCMethodDecl *D);
106 void VisitObjCContainerDecl(ObjCContainerDecl *D);
107 void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
108 void VisitObjCIvarDecl(ObjCIvarDecl *D);
109 void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
110 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
111 void VisitObjCClassDecl(ObjCClassDecl *D);
112 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D);
113 void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
114 void VisitObjCImplDecl(ObjCImplDecl *D);
115 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
116 void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
117 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
118 void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
119 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
123 void ASTDeclWriter::Visit(Decl *D) {
124 DeclVisitor<ASTDeclWriter>::Visit(D);
126 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
127 // have been written. We want it last because we will not read it back when
128 // retrieving it from the AST, we'll just lazily set the offset.
129 if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
130 Record.push_back(FD->isThisDeclarationADefinition());
131 if (FD->isThisDeclarationADefinition())
132 Writer.AddStmt(FD->getBody());
136 void ASTDeclWriter::VisitDecl(Decl *D) {
137 Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
138 Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
139 Writer.AddSourceLocation(D->getLocation(), Record);
140 Record.push_back(D->isInvalidDecl());
141 Record.push_back(D->hasAttrs());
143 Writer.WriteAttributes(D->getAttrs(), Record);
144 Record.push_back(D->isImplicit());
145 Record.push_back(D->isUsed(false));
146 Record.push_back(D->isReferenced());
147 Record.push_back(D->getAccess());
148 Record.push_back(D->getPCHLevel());
151 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
153 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
154 Code = serialization::DECL_TRANSLATION_UNIT;
157 void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
159 Writer.AddDeclarationName(D->getDeclName(), Record);
162 void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
164 Writer.AddSourceLocation(D->getLocStart(), Record);
165 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
168 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
170 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
171 Code = serialization::DECL_TYPEDEF;
174 void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
176 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
177 Code = serialization::DECL_TYPEALIAS;
180 void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
182 VisitRedeclarable(D);
183 Record.push_back(D->getIdentifierNamespace());
184 Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
185 Record.push_back(D->isDefinition());
186 Record.push_back(D->isEmbeddedInDeclarator());
187 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
188 Record.push_back(D->hasExtInfo());
190 Writer.AddQualifierInfo(*D->getExtInfo(), Record);
192 Writer.AddDeclRef(D->getTypedefNameForAnonDecl(), Record);
195 void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
197 Writer.AddTypeSourceInfo(D->getIntegerTypeSourceInfo(), Record);
198 if (!D->getIntegerTypeSourceInfo())
199 Writer.AddTypeRef(D->getIntegerType(), Record);
200 Writer.AddTypeRef(D->getPromotionType(), Record);
201 Record.push_back(D->getNumPositiveBits());
202 Record.push_back(D->getNumNegativeBits());
203 Record.push_back(D->isScoped());
204 Record.push_back(D->isScopedUsingClassTag());
205 Record.push_back(D->isFixed());
206 Writer.AddDeclRef(D->getInstantiatedFromMemberEnum(), Record);
207 Code = serialization::DECL_ENUM;
210 void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
212 Record.push_back(D->hasFlexibleArrayMember());
213 Record.push_back(D->isAnonymousStructOrUnion());
214 Record.push_back(D->hasObjectMember());
215 Code = serialization::DECL_RECORD;
218 void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
220 Writer.AddTypeRef(D->getType(), Record);
223 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
225 Record.push_back(D->getInitExpr()? 1 : 0);
226 if (D->getInitExpr())
227 Writer.AddStmt(D->getInitExpr());
228 Writer.AddAPSInt(D->getInitVal(), Record);
229 Code = serialization::DECL_ENUM_CONSTANT;
232 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
234 Writer.AddSourceLocation(D->getInnerLocStart(), Record);
235 Record.push_back(D->hasExtInfo());
237 Writer.AddQualifierInfo(*D->getExtInfo(), Record);
238 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
241 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
242 VisitDeclaratorDecl(D);
243 VisitRedeclarable(D);
245 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
246 Record.push_back(D->getIdentifierNamespace());
247 Record.push_back(D->getTemplatedKind());
248 switch (D->getTemplatedKind()) {
249 default: assert(false && "Unhandled TemplatedKind!");
251 case FunctionDecl::TK_NonTemplate:
253 case FunctionDecl::TK_FunctionTemplate:
254 Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
256 case FunctionDecl::TK_MemberSpecialization: {
257 MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
258 Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
259 Record.push_back(MemberInfo->getTemplateSpecializationKind());
260 Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
263 case FunctionDecl::TK_FunctionTemplateSpecialization: {
264 FunctionTemplateSpecializationInfo *
265 FTSInfo = D->getTemplateSpecializationInfo();
266 Writer.AddDeclRef(FTSInfo->getTemplate(), Record);
267 Record.push_back(FTSInfo->getTemplateSpecializationKind());
269 // Template arguments.
270 Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record);
272 // Template args as written.
273 Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0);
274 if (FTSInfo->TemplateArgumentsAsWritten) {
275 Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size());
276 for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i)
277 Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
279 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(),
281 Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(),
285 Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record);
287 if (D->isCanonicalDecl()) {
288 // Write the template that contains the specializations set. We will
289 // add a FunctionTemplateSpecializationInfo to it when reading.
290 Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record);
294 case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
295 DependentFunctionTemplateSpecializationInfo *
296 DFTSInfo = D->getDependentSpecializationInfo();
299 Record.push_back(DFTSInfo->getNumTemplates());
300 for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
301 Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
304 Record.push_back(DFTSInfo->getNumTemplateArgs());
305 for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
306 Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
307 Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record);
308 Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record);
313 // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
314 // after everything else is written.
316 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
317 Record.push_back(D->getStorageClassAsWritten());
318 Record.push_back(D->IsInline);
319 Record.push_back(D->isInlineSpecified());
320 Record.push_back(D->isVirtualAsWritten());
321 Record.push_back(D->isPure());
322 Record.push_back(D->hasInheritedPrototype());
323 Record.push_back(D->hasWrittenPrototype());
324 Record.push_back(D->isDeleted());
325 Record.push_back(D->isTrivial());
326 Record.push_back(D->hasImplicitReturnZero());
327 Writer.AddSourceLocation(D->getLocEnd(), Record);
329 Record.push_back(D->param_size());
330 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
332 Writer.AddDeclRef(*P, Record);
333 Code = serialization::DECL_FUNCTION;
336 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
338 // FIXME: convert to LazyStmtPtr?
339 // Unlike C/C++, method bodies will never be in header files.
340 bool HasBodyStuff = D->getBody() != 0 ||
341 D->getSelfDecl() != 0 || D->getCmdDecl() != 0;
342 Record.push_back(HasBodyStuff);
344 Writer.AddStmt(D->getBody());
345 Writer.AddDeclRef(D->getSelfDecl(), Record);
346 Writer.AddDeclRef(D->getCmdDecl(), Record);
348 Record.push_back(D->isInstanceMethod());
349 Record.push_back(D->isVariadic());
350 Record.push_back(D->isSynthesized());
351 Record.push_back(D->isDefined());
352 // FIXME: stable encoding for @required/@optional
353 Record.push_back(D->getImplementationControl());
354 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
355 Record.push_back(D->getObjCDeclQualifier());
356 Record.push_back(D->getNumSelectorArgs());
357 Writer.AddTypeRef(D->getResultType(), Record);
358 Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
359 Writer.AddSourceLocation(D->getLocEnd(), Record);
360 Record.push_back(D->param_size());
361 for (ObjCMethodDecl::param_iterator P = D->param_begin(),
362 PEnd = D->param_end(); P != PEnd; ++P)
363 Writer.AddDeclRef(*P, Record);
364 Code = serialization::DECL_OBJC_METHOD;
367 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
369 Writer.AddSourceRange(D->getAtEndRange(), Record);
370 // Abstract class (no need to define a stable serialization::DECL code).
373 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
374 VisitObjCContainerDecl(D);
375 Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
376 Writer.AddDeclRef(D->getSuperClass(), Record);
378 // Write out the protocols that are directly referenced by the @interface.
379 Record.push_back(D->ReferencedProtocols.size());
380 for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
381 PEnd = D->protocol_end();
383 Writer.AddDeclRef(*P, Record);
384 for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
385 PLEnd = D->protocol_loc_end();
387 Writer.AddSourceLocation(*PL, Record);
389 // Write out the protocols that are transitively referenced.
390 Record.push_back(D->AllReferencedProtocols.size());
391 for (ObjCList<ObjCProtocolDecl>::iterator
392 P = D->AllReferencedProtocols.begin(),
393 PEnd = D->AllReferencedProtocols.end();
395 Writer.AddDeclRef(*P, Record);
397 // Write out the ivars.
398 Record.push_back(D->ivar_size());
399 for (ObjCInterfaceDecl::ivar_iterator I = D->ivar_begin(),
400 IEnd = D->ivar_end(); I != IEnd; ++I)
401 Writer.AddDeclRef(*I, Record);
402 Writer.AddDeclRef(D->getCategoryList(), Record);
403 Record.push_back(D->isForwardDecl());
404 Record.push_back(D->isImplicitInterfaceDecl());
405 Writer.AddSourceLocation(D->getClassLoc(), Record);
406 Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
407 Writer.AddSourceLocation(D->getLocEnd(), Record);
408 Code = serialization::DECL_OBJC_INTERFACE;
411 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
413 // FIXME: stable encoding for @public/@private/@protected/@package
414 Record.push_back(D->getAccessControl());
415 Record.push_back(D->getSynthesize());
416 Code = serialization::DECL_OBJC_IVAR;
419 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
420 VisitObjCContainerDecl(D);
421 Record.push_back(D->isForwardDecl());
422 Writer.AddSourceLocation(D->getLocEnd(), Record);
423 Record.push_back(D->protocol_size());
424 for (ObjCProtocolDecl::protocol_iterator
425 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
426 Writer.AddDeclRef(*I, Record);
427 for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
428 PLEnd = D->protocol_loc_end();
430 Writer.AddSourceLocation(*PL, Record);
431 Code = serialization::DECL_OBJC_PROTOCOL;
434 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
436 Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
439 void ASTDeclWriter::VisitObjCClassDecl(ObjCClassDecl *D) {
441 Record.push_back(D->size());
442 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
443 Writer.AddDeclRef(I->getInterface(), Record);
444 for (ObjCClassDecl::iterator I = D->begin(), IEnd = D->end(); I != IEnd; ++I)
445 Writer.AddSourceLocation(I->getLocation(), Record);
446 Code = serialization::DECL_OBJC_CLASS;
449 void ASTDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl *D) {
451 Record.push_back(D->protocol_size());
452 for (ObjCForwardProtocolDecl::protocol_iterator
453 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
454 Writer.AddDeclRef(*I, Record);
455 for (ObjCForwardProtocolDecl::protocol_loc_iterator
456 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
458 Writer.AddSourceLocation(*PL, Record);
459 Code = serialization::DECL_OBJC_FORWARD_PROTOCOL;
462 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
463 VisitObjCContainerDecl(D);
464 Writer.AddDeclRef(D->getClassInterface(), Record);
465 Record.push_back(D->protocol_size());
466 for (ObjCCategoryDecl::protocol_iterator
467 I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
468 Writer.AddDeclRef(*I, Record);
469 for (ObjCCategoryDecl::protocol_loc_iterator
470 PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
472 Writer.AddSourceLocation(*PL, Record);
473 Writer.AddDeclRef(D->getNextClassCategory(), Record);
474 Record.push_back(D->hasSynthBitfield());
475 Writer.AddSourceLocation(D->getAtLoc(), Record);
476 Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
477 Code = serialization::DECL_OBJC_CATEGORY;
480 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
482 Writer.AddDeclRef(D->getClassInterface(), Record);
483 Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
486 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
488 Writer.AddSourceLocation(D->getAtLoc(), Record);
489 Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
490 // FIXME: stable encoding
491 Record.push_back((unsigned)D->getPropertyAttributes());
492 Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
493 // FIXME: stable encoding
494 Record.push_back((unsigned)D->getPropertyImplementation());
495 Writer.AddDeclarationName(D->getGetterName(), Record);
496 Writer.AddDeclarationName(D->getSetterName(), Record);
497 Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
498 Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
499 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
500 Code = serialization::DECL_OBJC_PROPERTY;
503 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
504 VisitObjCContainerDecl(D);
505 Writer.AddDeclRef(D->getClassInterface(), Record);
506 // Abstract class (no need to define a stable serialization::DECL code).
509 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
510 VisitObjCImplDecl(D);
511 Writer.AddIdentifierRef(D->getIdentifier(), Record);
512 Code = serialization::DECL_OBJC_CATEGORY_IMPL;
515 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
516 VisitObjCImplDecl(D);
517 Writer.AddDeclRef(D->getSuperClass(), Record);
518 Writer.AddCXXCtorInitializers(D->IvarInitializers, D->NumIvarInitializers,
520 Record.push_back(D->hasSynthBitfield());
521 Code = serialization::DECL_OBJC_IMPLEMENTATION;
524 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
526 Writer.AddSourceLocation(D->getLocStart(), Record);
527 Writer.AddDeclRef(D->getPropertyDecl(), Record);
528 Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
529 Writer.AddSourceLocation(D->getPropertyIvarDeclLoc(), Record);
530 Writer.AddStmt(D->getGetterCXXConstructor());
531 Writer.AddStmt(D->getSetterCXXAssignment());
532 Code = serialization::DECL_OBJC_PROPERTY_IMPL;
535 void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
536 VisitDeclaratorDecl(D);
537 Record.push_back(D->isMutable());
538 Record.push_back(D->getBitWidth()? 1 : 0);
539 if (D->getBitWidth())
540 Writer.AddStmt(D->getBitWidth());
541 if (!D->getDeclName())
542 Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record);
543 Code = serialization::DECL_FIELD;
546 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
548 Record.push_back(D->getChainingSize());
550 for (IndirectFieldDecl::chain_iterator
551 P = D->chain_begin(),
552 PEnd = D->chain_end(); P != PEnd; ++P)
553 Writer.AddDeclRef(*P, Record);
554 Code = serialization::DECL_INDIRECTFIELD;
557 void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
558 VisitDeclaratorDecl(D);
559 VisitRedeclarable(D);
560 Record.push_back(D->getStorageClass()); // FIXME: stable encoding
561 Record.push_back(D->getStorageClassAsWritten());
562 Record.push_back(D->isThreadSpecified());
563 Record.push_back(D->hasCXXDirectInitializer());
564 Record.push_back(D->isExceptionVariable());
565 Record.push_back(D->isNRVOVariable());
566 Record.push_back(D->isCXXForRangeDecl());
567 Record.push_back(D->getInit() ? 1 : 0);
569 Writer.AddStmt(D->getInit());
571 MemberSpecializationInfo *SpecInfo
572 = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0;
573 Record.push_back(SpecInfo != 0);
575 Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record);
576 Record.push_back(SpecInfo->getTemplateSpecializationKind());
577 Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record);
580 Code = serialization::DECL_VAR;
583 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
585 Code = serialization::DECL_IMPLICIT_PARAM;
588 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
590 Record.push_back(D->isObjCMethodParameter());
591 Record.push_back(D->getFunctionScopeDepth());
592 Record.push_back(D->getFunctionScopeIndex());
593 Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
594 Record.push_back(D->isKNRPromoted());
595 Record.push_back(D->hasInheritedDefaultArg());
596 Record.push_back(D->hasUninstantiatedDefaultArg());
597 if (D->hasUninstantiatedDefaultArg())
598 Writer.AddStmt(D->getUninstantiatedDefaultArg());
599 Code = serialization::DECL_PARM_VAR;
601 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
602 // we dynamically check for the properties that we optimize for, but don't
603 // know are true of all PARM_VAR_DECLs.
604 if (!D->getTypeSourceInfo() &&
608 D->getAccess() == AS_none &&
609 D->getPCHLevel() == 0 &&
610 D->getStorageClass() == 0 &&
611 !D->hasCXXDirectInitializer() && // Can params have this ever?
612 D->getFunctionScopeDepth() == 0 &&
613 D->getObjCDeclQualifier() == 0 &&
614 !D->isKNRPromoted() &&
615 !D->hasInheritedDefaultArg() &&
617 !D->hasUninstantiatedDefaultArg()) // No default expr.
618 AbbrevToUse = Writer.getParmVarDeclAbbrev();
620 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
621 // just us assuming it.
622 assert(!D->isInvalidDecl() && "Shouldn't emit invalid decls");
623 assert(!D->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
624 assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
625 assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
626 assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
627 assert(!D->isStaticDataMember() &&
628 "PARM_VAR_DECL can't be static data member");
631 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
633 Writer.AddStmt(D->getAsmString());
634 Writer.AddSourceLocation(D->getRParenLoc(), Record);
635 Code = serialization::DECL_FILE_SCOPE_ASM;
638 void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
640 Writer.AddStmt(D->getBody());
641 Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
642 Record.push_back(D->param_size());
643 for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
645 Writer.AddDeclRef(*P, Record);
646 Record.push_back(D->capturesCXXThis());
647 Record.push_back(D->getNumCaptures());
648 for (BlockDecl::capture_iterator
649 i = D->capture_begin(), e = D->capture_end(); i != e; ++i) {
650 const BlockDecl::Capture &capture = *i;
651 Writer.AddDeclRef(capture.getVariable(), Record);
654 if (capture.isByRef()) flags |= 1;
655 if (capture.isNested()) flags |= 2;
656 if (capture.hasCopyExpr()) flags |= 4;
657 Record.push_back(flags);
659 if (capture.hasCopyExpr()) Writer.AddStmt(capture.getCopyExpr());
662 Code = serialization::DECL_BLOCK;
665 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
667 Record.push_back(D->getLanguage());
668 Writer.AddSourceLocation(D->getExternLoc(), Record);
669 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
670 Code = serialization::DECL_LINKAGE_SPEC;
673 void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
675 Writer.AddSourceLocation(D->getLocStart(), Record);
676 Code = serialization::DECL_LABEL;
680 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
682 Record.push_back(D->isInline());
683 Writer.AddSourceLocation(D->getLocStart(), Record);
684 Writer.AddSourceLocation(D->getRBraceLoc(), Record);
685 Writer.AddDeclRef(D->getNextNamespace(), Record);
687 // Only write one reference--original or anonymous
688 Record.push_back(D->isOriginalNamespace());
689 if (D->isOriginalNamespace())
690 Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
692 Writer.AddDeclRef(D->getOriginalNamespace(), Record);
693 Code = serialization::DECL_NAMESPACE;
695 if (Writer.hasChain() && !D->isOriginalNamespace() &&
696 D->getOriginalNamespace()->getPCHLevel() > 0) {
697 NamespaceDecl *NS = D->getOriginalNamespace();
698 Writer.AddUpdatedDeclContext(NS);
700 // Make sure all visible decls are written. They will be recorded later.
701 NS->lookup(DeclarationName());
702 StoredDeclsMap *Map = static_cast<StoredDeclsMap*>(NS->getLookupPtr());
704 for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
706 DeclContext::lookup_result Result = D->second.getLookupResult();
707 while (Result.first != Result.second) {
708 Writer.GetDeclRef(*Result.first);
715 if (Writer.hasChain() && D->isAnonymousNamespace() && !D->getNextNamespace()){
716 // This is a most recent reopening of the anonymous namespace. If its parent
717 // is in a previous PCH (or is the TU), mark that parent for update, because
718 // the original namespace always points to the latest re-opening of its
719 // anonymous namespace.
720 Decl *Parent = cast<Decl>(
721 D->getParent()->getRedeclContext()->getPrimaryContext());
722 if (Parent->getPCHLevel() > 0) {
723 ASTWriter::UpdateRecord &Record = Writer.DeclUpdates[Parent];
724 Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
725 Writer.AddDeclRef(D, Record);
730 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
732 Writer.AddSourceLocation(D->getNamespaceLoc(), Record);
733 Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
734 Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
735 Writer.AddDeclRef(D->getNamespace(), Record);
736 Code = serialization::DECL_NAMESPACE_ALIAS;
739 void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
741 Writer.AddSourceLocation(D->getUsingLocation(), Record);
742 Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
743 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
744 Writer.AddDeclRef(D->FirstUsingShadow, Record);
745 Record.push_back(D->isTypeName());
746 Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record);
747 Code = serialization::DECL_USING;
750 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
752 Writer.AddDeclRef(D->getTargetDecl(), Record);
753 Writer.AddDeclRef(D->UsingOrNextShadow, Record);
754 Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record);
755 Code = serialization::DECL_USING_SHADOW;
758 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
760 Writer.AddSourceLocation(D->getUsingLoc(), Record);
761 Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
762 Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
763 Writer.AddDeclRef(D->getNominatedNamespace(), Record);
764 Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
765 Code = serialization::DECL_USING_DIRECTIVE;
768 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
770 Writer.AddSourceLocation(D->getUsingLoc(), Record);
771 Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
772 Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
773 Code = serialization::DECL_UNRESOLVED_USING_VALUE;
776 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
777 UnresolvedUsingTypenameDecl *D) {
779 Writer.AddSourceLocation(D->getTypenameLoc(), Record);
780 Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
781 Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
784 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
787 CXXRecordDecl *DefinitionDecl = 0;
788 if (D->DefinitionData)
789 DefinitionDecl = D->DefinitionData->Definition;
790 Writer.AddDeclRef(DefinitionDecl, Record);
791 if (D == DefinitionDecl)
792 Writer.AddCXXDefinitionData(D, Record);
795 CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
797 if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
798 Record.push_back(CXXRecTemplate);
799 Writer.AddDeclRef(TemplD, Record);
800 } else if (MemberSpecializationInfo *MSInfo
801 = D->getMemberSpecializationInfo()) {
802 Record.push_back(CXXRecMemberSpecialization);
803 Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
804 Record.push_back(MSInfo->getTemplateSpecializationKind());
805 Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
807 Record.push_back(CXXRecNotTemplate);
810 // Store the key function to avoid deserializing every method so we can
813 Writer.AddDeclRef(Context.getKeyFunction(D), Record);
815 Code = serialization::DECL_CXX_RECORD;
818 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
819 VisitFunctionDecl(D);
820 Record.push_back(D->size_overridden_methods());
821 for (CXXMethodDecl::method_iterator
822 I = D->begin_overridden_methods(), E = D->end_overridden_methods();
824 Writer.AddDeclRef(*I, Record);
825 Code = serialization::DECL_CXX_METHOD;
828 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
829 VisitCXXMethodDecl(D);
831 Record.push_back(D->IsExplicitSpecified);
832 Record.push_back(D->ImplicitlyDefined);
833 Writer.AddCXXCtorInitializers(D->CtorInitializers, D->NumCtorInitializers,
836 Code = serialization::DECL_CXX_CONSTRUCTOR;
839 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
840 VisitCXXMethodDecl(D);
842 Record.push_back(D->ImplicitlyDefined);
843 Writer.AddDeclRef(D->OperatorDelete, Record);
845 Code = serialization::DECL_CXX_DESTRUCTOR;
848 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
849 VisitCXXMethodDecl(D);
850 Record.push_back(D->IsExplicitSpecified);
851 Code = serialization::DECL_CXX_CONVERSION;
854 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
856 Writer.AddSourceLocation(D->getColonLoc(), Record);
857 Code = serialization::DECL_ACCESS_SPEC;
860 void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
862 Record.push_back(D->Friend.is<TypeSourceInfo*>());
863 if (D->Friend.is<TypeSourceInfo*>())
864 Writer.AddTypeSourceInfo(D->Friend.get<TypeSourceInfo*>(), Record);
866 Writer.AddDeclRef(D->Friend.get<NamedDecl*>(), Record);
867 Writer.AddDeclRef(D->getNextFriend(), Record);
868 Record.push_back(D->UnsupportedFriend);
869 Writer.AddSourceLocation(D->FriendLoc, Record);
870 Code = serialization::DECL_FRIEND;
873 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
875 Record.push_back(D->getNumTemplateParameters());
876 for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
877 Writer.AddTemplateParameterList(D->getTemplateParameterList(i), Record);
878 Record.push_back(D->getFriendDecl() != 0);
879 if (D->getFriendDecl())
880 Writer.AddDeclRef(D->getFriendDecl(), Record);
882 Writer.AddTypeSourceInfo(D->getFriendType(), Record);
883 Writer.AddSourceLocation(D->getFriendLoc(), Record);
884 Code = serialization::DECL_FRIEND_TEMPLATE;
887 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
890 Writer.AddDeclRef(D->getTemplatedDecl(), Record);
891 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
894 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
895 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
896 // getCommonPtr() can be used while this is still initializing.
898 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
899 if (D->getPreviousDeclaration())
900 Writer.AddDeclRef(D->getFirstDeclaration(), Record);
902 if (D->getPreviousDeclaration() == 0) {
903 // This TemplateDecl owns the CommonPtr; write it.
904 assert(D->isCanonicalDecl());
906 Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
907 if (D->getInstantiatedFromMemberTemplate())
908 Record.push_back(D->isMemberSpecialization());
910 Writer.AddDeclRef(D->getCommonPtr()->Latest, Record);
912 RedeclarableTemplateDecl *First = D->getFirstDeclaration();
914 // If this is a most recent redeclaration that is pointed to by a first decl
915 // in a chained PCH, keep track of the association with the map so we can
916 // update the first decl during AST reading.
917 if (First->getMostRecentDeclaration() == D &&
918 First->getPCHLevel() > D->getPCHLevel()) {
919 assert(Writer.FirstLatestDecls.find(First)==Writer.FirstLatestDecls.end()
920 && "The latest is already set");
921 Writer.FirstLatestDecls[First] = D;
925 VisitTemplateDecl(D);
926 Record.push_back(D->getIdentifierNamespace());
929 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
930 VisitRedeclarableTemplateDecl(D);
932 if (D->getPreviousDeclaration() == 0) {
933 typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy;
934 CTSDSetTy &CTSDSet = D->getSpecializations();
935 Record.push_back(CTSDSet.size());
936 for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) {
937 assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
938 Writer.AddDeclRef(&*I, Record);
941 typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy;
942 CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
943 Record.push_back(CTPSDSet.size());
944 for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) {
945 assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
946 Writer.AddDeclRef(&*I, Record);
949 // InjectedClassNameType is computed, no need to write it.
951 Code = serialization::DECL_CLASS_TEMPLATE;
954 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
955 ClassTemplateSpecializationDecl *D) {
956 VisitCXXRecordDecl(D);
958 llvm::PointerUnion<ClassTemplateDecl *,
959 ClassTemplatePartialSpecializationDecl *> InstFrom
960 = D->getSpecializedTemplateOrPartial();
962 if (InstFrom.is<ClassTemplateDecl *>()) {
963 InstFromD = InstFrom.get<ClassTemplateDecl *>();
964 Writer.AddDeclRef(InstFromD, Record);
966 InstFromD = InstFrom.get<ClassTemplatePartialSpecializationDecl *>();
967 Writer.AddDeclRef(InstFromD, Record);
968 Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
969 InstFromD = cast<ClassTemplatePartialSpecializationDecl>(InstFromD)->
970 getSpecializedTemplate();
974 Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
975 if (D->getTypeAsWritten()) {
976 Writer.AddSourceLocation(D->getExternLoc(), Record);
977 Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
980 Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
981 Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
982 Record.push_back(D->getSpecializationKind());
984 if (D->isCanonicalDecl()) {
985 // When reading, we'll add it to the folding set of the following template.
986 Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
989 Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
992 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
993 ClassTemplatePartialSpecializationDecl *D) {
994 VisitClassTemplateSpecializationDecl(D);
996 Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
998 Record.push_back(D->getNumTemplateArgsAsWritten());
999 for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i)
1000 Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record);
1002 Record.push_back(D->getSequenceNumber());
1004 // These are read/set from/to the first declaration.
1005 if (D->getPreviousDeclaration() == 0) {
1006 Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
1007 Record.push_back(D->isMemberSpecialization());
1010 Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
1013 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1014 VisitRedeclarableTemplateDecl(D);
1016 if (D->getPreviousDeclaration() == 0) {
1017 // This FunctionTemplateDecl owns the CommonPtr; write it.
1019 // Write the function specialization declarations.
1020 Record.push_back(D->getSpecializations().size());
1021 for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator
1022 I = D->getSpecializations().begin(),
1023 E = D->getSpecializations().end() ; I != E; ++I) {
1024 assert(I->Function->isCanonicalDecl() &&
1025 "Expected only canonical decls in set");
1026 Writer.AddDeclRef(I->Function, Record);
1029 Code = serialization::DECL_FUNCTION_TEMPLATE;
1032 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1035 Record.push_back(D->wasDeclaredWithTypename());
1036 Record.push_back(D->defaultArgumentWasInherited());
1037 Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
1039 Code = serialization::DECL_TEMPLATE_TYPE_PARM;
1042 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1043 // For an expanded parameter pack, record the number of expansion types here
1044 // so that it's easier for
1045 if (D->isExpandedParameterPack())
1046 Record.push_back(D->getNumExpansionTypes());
1048 VisitDeclaratorDecl(D);
1049 // TemplateParmPosition.
1050 Record.push_back(D->getDepth());
1051 Record.push_back(D->getPosition());
1053 if (D->isExpandedParameterPack()) {
1054 for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1055 Writer.AddTypeRef(D->getExpansionType(I), Record);
1056 Writer.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I), Record);
1059 Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
1061 // Rest of NonTypeTemplateParmDecl.
1062 Record.push_back(D->isParameterPack());
1063 Record.push_back(D->getDefaultArgument() != 0);
1064 if (D->getDefaultArgument()) {
1065 Writer.AddStmt(D->getDefaultArgument());
1066 Record.push_back(D->defaultArgumentWasInherited());
1068 Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
1072 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1073 VisitTemplateDecl(D);
1074 // TemplateParmPosition.
1075 Record.push_back(D->getDepth());
1076 Record.push_back(D->getPosition());
1077 // Rest of TemplateTemplateParmDecl.
1078 Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record);
1079 Record.push_back(D->defaultArgumentWasInherited());
1080 Record.push_back(D->isParameterPack());
1081 Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
1084 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1086 Writer.AddStmt(D->getAssertExpr());
1087 Writer.AddStmt(D->getMessage());
1088 Writer.AddSourceLocation(D->getRParenLoc(), Record);
1089 Code = serialization::DECL_STATIC_ASSERT;
1092 /// \brief Emit the DeclContext part of a declaration context decl.
1094 /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
1095 /// block for this declaration context is stored. May be 0 to indicate
1096 /// that there are no declarations stored within this context.
1098 /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
1099 /// block for this declaration context is stored. May be 0 to indicate
1100 /// that there are no declarations visible from this context. Note
1101 /// that this value will not be emitted for non-primary declaration
1103 void ASTDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
1104 uint64_t VisibleOffset) {
1105 Record.push_back(LexicalOffset);
1106 Record.push_back(VisibleOffset);
1109 template <typename T>
1110 void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
1111 enum { NoRedeclaration = 0, PointsToPrevious, PointsToLatest };
1112 if (D->RedeclLink.getNext() == D) {
1113 Record.push_back(NoRedeclaration);
1115 if (D->RedeclLink.NextIsPrevious()) {
1116 Record.push_back(PointsToPrevious);
1117 Writer.AddDeclRef(D->getPreviousDeclaration(), Record);
1118 Writer.AddDeclRef(D->getFirstDeclaration(), Record);
1120 Record.push_back(PointsToLatest);
1121 Writer.AddDeclRef(D->RedeclLink.getPointer(), Record);
1125 T *First = D->getFirstDeclaration();
1126 T *ThisDecl = static_cast<T*>(D);
1127 // If this is a most recent redeclaration that is pointed to by a first decl
1128 // in a chained PCH, keep track of the association with the map so we can
1129 // update the first decl during AST reading.
1130 if (ThisDecl != First && First->getMostRecentDeclaration() == ThisDecl &&
1131 First->getPCHLevel() > ThisDecl->getPCHLevel()) {
1132 assert(Writer.FirstLatestDecls.find(First) == Writer.FirstLatestDecls.end()
1133 && "The latest is already set");
1134 Writer.FirstLatestDecls[First] = ThisDecl;
1138 //===----------------------------------------------------------------------===//
1139 // ASTWriter Implementation
1140 //===----------------------------------------------------------------------===//
1142 void ASTWriter::WriteDeclsBlockAbbrevs() {
1143 using namespace llvm;
1144 // Abbreviation for DECL_PARM_VAR.
1145 BitCodeAbbrev *Abv = new BitCodeAbbrev();
1146 Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
1149 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1150 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1151 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
1152 Abv->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
1153 Abv->Add(BitCodeAbbrevOp(0)); // HasAttrs
1154 Abv->Add(BitCodeAbbrevOp(0)); // isImplicit
1155 Abv->Add(BitCodeAbbrevOp(0)); // isUsed
1156 Abv->Add(BitCodeAbbrevOp(0)); // isReferenced
1157 Abv->Add(BitCodeAbbrevOp(AS_none)); // C++ AccessSpecifier
1158 Abv->Add(BitCodeAbbrevOp(0)); // PCH level
1161 Abv->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
1162 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1164 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1166 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1167 Abv->Add(BitCodeAbbrevOp(0)); // hasExtInfo
1168 Abv->Add(BitCodeAbbrevOp(serialization::PREDEF_TYPE_NULL_ID)); // InfoType
1170 Abv->Add(BitCodeAbbrevOp(0)); // No redeclaration
1171 Abv->Add(BitCodeAbbrevOp(0)); // StorageClass
1172 Abv->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
1173 Abv->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
1174 Abv->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
1175 Abv->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
1176 Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
1177 Abv->Add(BitCodeAbbrevOp(0)); // isCXXForRangeDecl
1178 Abv->Add(BitCodeAbbrevOp(0)); // HasInit
1179 Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo
1181 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsObjCMethodParameter
1182 Abv->Add(BitCodeAbbrevOp(0)); // ScopeDepth
1183 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
1184 Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
1185 Abv->Add(BitCodeAbbrevOp(0)); // KNRPromoted
1186 Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
1187 Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg
1189 ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv);
1191 Abv = new BitCodeAbbrev();
1192 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
1193 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1194 DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv);
1196 Abv = new BitCodeAbbrev();
1197 Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
1198 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1199 Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1200 DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(Abv);
1203 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
1204 /// consumers of the AST.
1206 /// Such decls will always be deserialized from the AST file, so we would like
1207 /// this to be as restrictive as possible. Currently the predicate is driven by
1208 /// code generation requirements, if other clients have a different notion of
1209 /// what is "required" then we may have to consider an alternate scheme where
1210 /// clients can iterate over the top-level decls and get information on them,
1211 /// without necessary deserializing them. We could explicitly require such
1212 /// clients to use a separate API call to "realize" the decl. This should be
1213 /// relatively painless since they would presumably only do it for top-level
1215 static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
1216 // File scoped assembly or obj-c implementation must be seen.
1217 if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplementationDecl>(D))
1220 return Context.DeclMustBeEmitted(D);
1223 void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
1224 // Switch case IDs are per Decl.
1225 ClearSwitchCaseIDs();
1228 ASTDeclWriter W(*this, Context, Record);
1230 // If this declaration is also a DeclContext, write blocks for the
1231 // declarations that lexically stored inside its context and those
1232 // declarations that are visible from its context. These blocks
1233 // are written before the declaration itself so that we can put
1234 // their offsets into the record for the declaration.
1235 uint64_t LexicalOffset = 0;
1236 uint64_t VisibleOffset = 0;
1237 DeclContext *DC = dyn_cast<DeclContext>(D);
1239 LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1240 VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1243 // Determine the ID for this declaration
1244 serialization::DeclID &IDR = DeclIDs[D];
1247 serialization::DeclID ID = IDR;
1249 if (ID < FirstDeclID) {
1250 // We're replacing a decl in a previous file.
1251 ReplacedDecls.push_back(std::make_pair(ID, Stream.GetCurrentBitNo()));
1253 unsigned Index = ID - FirstDeclID;
1255 // Record the offset for this declaration
1256 if (DeclOffsets.size() == Index)
1257 DeclOffsets.push_back(Stream.GetCurrentBitNo());
1258 else if (DeclOffsets.size() < Index) {
1259 DeclOffsets.resize(Index+1);
1260 DeclOffsets[Index] = Stream.GetCurrentBitNo();
1264 // Build and emit a record for this declaration
1266 W.Code = (serialization::DeclCode)0;
1269 if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
1272 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
1273 D->getDeclKindName() + "'");
1274 Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
1276 // Flush any expressions that were written as part of this declaration.
1279 // Flush C++ base specifiers, if there are any.
1280 FlushCXXBaseSpecifiers();
1282 // Note "external" declarations so that we can add them to a record in the
1285 // FIXME: This should be renamed, the predicate is much more complicated.
1286 if (isRequiredDecl(D, Context))
1287 ExternalDefinitions.push_back(ID);