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 "clang/AST/DeclVisitor.h"
16 #include "clang/AST/DeclCXX.h"
17 #include "clang/AST/DeclTemplate.h"
18 #include "clang/AST/Expr.h"
19 #include "clang/AST/DeclContextInternals.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/Bitcode/BitstreamWriter.h"
22 #include "llvm/Support/ErrorHandling.h"
23 using namespace clang
;
25 //===----------------------------------------------------------------------===//
26 // Declaration serialization
27 //===----------------------------------------------------------------------===//
30 class ASTDeclWriter
: public DeclVisitor
<ASTDeclWriter
, void> {
34 typedef ASTWriter::RecordData RecordData
;
38 serialization::DeclCode Code
;
41 ASTDeclWriter(ASTWriter
&Writer
, ASTContext
&Context
, RecordData
&Record
)
42 : Writer(Writer
), Context(Context
), Record(Record
) {
47 void VisitDecl(Decl
*D
);
48 void VisitTranslationUnitDecl(TranslationUnitDecl
*D
);
49 void VisitNamedDecl(NamedDecl
*D
);
50 void VisitLabelDecl(LabelDecl
*LD
);
51 void VisitNamespaceDecl(NamespaceDecl
*D
);
52 void VisitUsingDirectiveDecl(UsingDirectiveDecl
*D
);
53 void VisitNamespaceAliasDecl(NamespaceAliasDecl
*D
);
54 void VisitTypeDecl(TypeDecl
*D
);
55 void VisitTypedefDecl(TypedefDecl
*D
);
56 void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl
*D
);
57 void VisitTagDecl(TagDecl
*D
);
58 void VisitEnumDecl(EnumDecl
*D
);
59 void VisitRecordDecl(RecordDecl
*D
);
60 void VisitCXXRecordDecl(CXXRecordDecl
*D
);
61 void VisitClassTemplateSpecializationDecl(
62 ClassTemplateSpecializationDecl
*D
);
63 void VisitClassTemplatePartialSpecializationDecl(
64 ClassTemplatePartialSpecializationDecl
*D
);
65 void VisitTemplateTypeParmDecl(TemplateTypeParmDecl
*D
);
66 void VisitValueDecl(ValueDecl
*D
);
67 void VisitEnumConstantDecl(EnumConstantDecl
*D
);
68 void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl
*D
);
69 void VisitDeclaratorDecl(DeclaratorDecl
*D
);
70 void VisitFunctionDecl(FunctionDecl
*D
);
71 void VisitCXXMethodDecl(CXXMethodDecl
*D
);
72 void VisitCXXConstructorDecl(CXXConstructorDecl
*D
);
73 void VisitCXXDestructorDecl(CXXDestructorDecl
*D
);
74 void VisitCXXConversionDecl(CXXConversionDecl
*D
);
75 void VisitFieldDecl(FieldDecl
*D
);
76 void VisitIndirectFieldDecl(IndirectFieldDecl
*D
);
77 void VisitVarDecl(VarDecl
*D
);
78 void VisitImplicitParamDecl(ImplicitParamDecl
*D
);
79 void VisitParmVarDecl(ParmVarDecl
*D
);
80 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl
*D
);
81 void VisitTemplateDecl(TemplateDecl
*D
);
82 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl
*D
);
83 void VisitClassTemplateDecl(ClassTemplateDecl
*D
);
84 void VisitFunctionTemplateDecl(FunctionTemplateDecl
*D
);
85 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl
*D
);
86 void VisitUsingDecl(UsingDecl
*D
);
87 void VisitUsingShadowDecl(UsingShadowDecl
*D
);
88 void VisitLinkageSpecDecl(LinkageSpecDecl
*D
);
89 void VisitFileScopeAsmDecl(FileScopeAsmDecl
*D
);
90 void VisitAccessSpecDecl(AccessSpecDecl
*D
);
91 void VisitFriendDecl(FriendDecl
*D
);
92 void VisitFriendTemplateDecl(FriendTemplateDecl
*D
);
93 void VisitStaticAssertDecl(StaticAssertDecl
*D
);
94 void VisitBlockDecl(BlockDecl
*D
);
96 void VisitDeclContext(DeclContext
*DC
, uint64_t LexicalOffset
,
97 uint64_t VisibleOffset
);
98 template <typename T
> void VisitRedeclarable(Redeclarable
<T
> *D
);
101 // FIXME: Put in the same order is DeclNodes.td?
102 void VisitObjCMethodDecl(ObjCMethodDecl
*D
);
103 void VisitObjCContainerDecl(ObjCContainerDecl
*D
);
104 void VisitObjCInterfaceDecl(ObjCInterfaceDecl
*D
);
105 void VisitObjCIvarDecl(ObjCIvarDecl
*D
);
106 void VisitObjCProtocolDecl(ObjCProtocolDecl
*D
);
107 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl
*D
);
108 void VisitObjCClassDecl(ObjCClassDecl
*D
);
109 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl
*D
);
110 void VisitObjCCategoryDecl(ObjCCategoryDecl
*D
);
111 void VisitObjCImplDecl(ObjCImplDecl
*D
);
112 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl
*D
);
113 void VisitObjCImplementationDecl(ObjCImplementationDecl
*D
);
114 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl
*D
);
115 void VisitObjCPropertyDecl(ObjCPropertyDecl
*D
);
116 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl
*D
);
120 void ASTDeclWriter::Visit(Decl
*D
) {
121 DeclVisitor
<ASTDeclWriter
>::Visit(D
);
123 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
124 // have been written. We want it last because we will not read it back when
125 // retrieving it from the AST, we'll just lazily set the offset.
126 if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
)) {
127 Record
.push_back(FD
->isThisDeclarationADefinition());
128 if (FD
->isThisDeclarationADefinition())
129 Writer
.AddStmt(FD
->getBody());
133 void ASTDeclWriter::VisitDecl(Decl
*D
) {
134 Writer
.AddDeclRef(cast_or_null
<Decl
>(D
->getDeclContext()), Record
);
135 Writer
.AddDeclRef(cast_or_null
<Decl
>(D
->getLexicalDeclContext()), Record
);
136 Writer
.AddSourceLocation(D
->getLocation(), Record
);
137 Record
.push_back(D
->isInvalidDecl());
138 Record
.push_back(D
->hasAttrs());
140 Writer
.WriteAttributes(D
->getAttrs(), Record
);
141 Record
.push_back(D
->isImplicit());
142 Record
.push_back(D
->isUsed(false));
143 Record
.push_back(D
->getAccess());
144 Record
.push_back(D
->getPCHLevel());
147 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl
*D
) {
149 Writer
.AddDeclRef(D
->getAnonymousNamespace(), Record
);
150 Code
= serialization::DECL_TRANSLATION_UNIT
;
153 void ASTDeclWriter::VisitNamedDecl(NamedDecl
*D
) {
155 Writer
.AddDeclarationName(D
->getDeclName(), Record
);
158 void ASTDeclWriter::VisitTypeDecl(TypeDecl
*D
) {
160 Writer
.AddTypeRef(QualType(D
->getTypeForDecl(), 0), Record
);
163 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl
*D
) {
165 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
166 Code
= serialization::DECL_TYPEDEF
;
169 void ASTDeclWriter::VisitTagDecl(TagDecl
*D
) {
171 VisitRedeclarable(D
);
172 Record
.push_back(D
->getIdentifierNamespace());
173 Record
.push_back((unsigned)D
->getTagKind()); // FIXME: stable encoding
174 Record
.push_back(D
->isDefinition());
175 Record
.push_back(D
->isEmbeddedInDeclarator());
176 Writer
.AddSourceLocation(D
->getRBraceLoc(), Record
);
177 Writer
.AddSourceLocation(D
->getTagKeywordLoc(), Record
);
178 Record
.push_back(D
->hasExtInfo());
180 Writer
.AddQualifierInfo(*D
->getExtInfo(), Record
);
182 Writer
.AddDeclRef(D
->getTypedefForAnonDecl(), Record
);
185 void ASTDeclWriter::VisitEnumDecl(EnumDecl
*D
) {
187 Writer
.AddTypeSourceInfo(D
->getIntegerTypeSourceInfo(), Record
);
188 if (!D
->getIntegerTypeSourceInfo())
189 Writer
.AddTypeRef(D
->getIntegerType(), Record
);
190 Writer
.AddTypeRef(D
->getPromotionType(), Record
);
191 Record
.push_back(D
->getNumPositiveBits());
192 Record
.push_back(D
->getNumNegativeBits());
193 Record
.push_back(D
->isScoped());
194 Record
.push_back(D
->isScopedUsingClassTag());
195 Record
.push_back(D
->isFixed());
196 Writer
.AddDeclRef(D
->getInstantiatedFromMemberEnum(), Record
);
197 Code
= serialization::DECL_ENUM
;
200 void ASTDeclWriter::VisitRecordDecl(RecordDecl
*D
) {
202 Record
.push_back(D
->hasFlexibleArrayMember());
203 Record
.push_back(D
->isAnonymousStructOrUnion());
204 Record
.push_back(D
->hasObjectMember());
205 Code
= serialization::DECL_RECORD
;
208 void ASTDeclWriter::VisitValueDecl(ValueDecl
*D
) {
210 Writer
.AddTypeRef(D
->getType(), Record
);
213 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl
*D
) {
215 Record
.push_back(D
->getInitExpr()? 1 : 0);
216 if (D
->getInitExpr())
217 Writer
.AddStmt(D
->getInitExpr());
218 Writer
.AddAPSInt(D
->getInitVal(), Record
);
219 Code
= serialization::DECL_ENUM_CONSTANT
;
222 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl
*D
) {
224 Record
.push_back(D
->hasExtInfo());
226 Writer
.AddQualifierInfo(*D
->getExtInfo(), Record
);
227 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
230 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl
*D
) {
231 VisitDeclaratorDecl(D
);
232 VisitRedeclarable(D
);
234 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
235 Record
.push_back(D
->getIdentifierNamespace());
236 Record
.push_back(D
->getTemplatedKind());
237 switch (D
->getTemplatedKind()) {
238 default: assert(false && "Unhandled TemplatedKind!");
240 case FunctionDecl::TK_NonTemplate
:
242 case FunctionDecl::TK_FunctionTemplate
:
243 Writer
.AddDeclRef(D
->getDescribedFunctionTemplate(), Record
);
245 case FunctionDecl::TK_MemberSpecialization
: {
246 MemberSpecializationInfo
*MemberInfo
= D
->getMemberSpecializationInfo();
247 Writer
.AddDeclRef(MemberInfo
->getInstantiatedFrom(), Record
);
248 Record
.push_back(MemberInfo
->getTemplateSpecializationKind());
249 Writer
.AddSourceLocation(MemberInfo
->getPointOfInstantiation(), Record
);
252 case FunctionDecl::TK_FunctionTemplateSpecialization
: {
253 FunctionTemplateSpecializationInfo
*
254 FTSInfo
= D
->getTemplateSpecializationInfo();
255 Writer
.AddDeclRef(FTSInfo
->getTemplate(), Record
);
256 Record
.push_back(FTSInfo
->getTemplateSpecializationKind());
258 // Template arguments.
259 Writer
.AddTemplateArgumentList(FTSInfo
->TemplateArguments
, Record
);
261 // Template args as written.
262 Record
.push_back(FTSInfo
->TemplateArgumentsAsWritten
!= 0);
263 if (FTSInfo
->TemplateArgumentsAsWritten
) {
264 Record
.push_back(FTSInfo
->TemplateArgumentsAsWritten
->size());
265 for (int i
=0, e
= FTSInfo
->TemplateArgumentsAsWritten
->size(); i
!=e
; ++i
)
266 Writer
.AddTemplateArgumentLoc((*FTSInfo
->TemplateArgumentsAsWritten
)[i
],
268 Writer
.AddSourceLocation(FTSInfo
->TemplateArgumentsAsWritten
->getLAngleLoc(),
270 Writer
.AddSourceLocation(FTSInfo
->TemplateArgumentsAsWritten
->getRAngleLoc(),
274 Writer
.AddSourceLocation(FTSInfo
->getPointOfInstantiation(), Record
);
276 if (D
->isCanonicalDecl()) {
277 // Write the template that contains the specializations set. We will
278 // add a FunctionTemplateSpecializationInfo to it when reading.
279 Writer
.AddDeclRef(FTSInfo
->getTemplate()->getCanonicalDecl(), Record
);
283 case FunctionDecl::TK_DependentFunctionTemplateSpecialization
: {
284 DependentFunctionTemplateSpecializationInfo
*
285 DFTSInfo
= D
->getDependentSpecializationInfo();
288 Record
.push_back(DFTSInfo
->getNumTemplates());
289 for (int i
=0, e
= DFTSInfo
->getNumTemplates(); i
!= e
; ++i
)
290 Writer
.AddDeclRef(DFTSInfo
->getTemplate(i
), Record
);
293 Record
.push_back(DFTSInfo
->getNumTemplateArgs());
294 for (int i
=0, e
= DFTSInfo
->getNumTemplateArgs(); i
!= e
; ++i
)
295 Writer
.AddTemplateArgumentLoc(DFTSInfo
->getTemplateArg(i
), Record
);
296 Writer
.AddSourceLocation(DFTSInfo
->getLAngleLoc(), Record
);
297 Writer
.AddSourceLocation(DFTSInfo
->getRAngleLoc(), Record
);
302 // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
303 // after everything else is written.
305 Record
.push_back(D
->getStorageClass()); // FIXME: stable encoding
306 Record
.push_back(D
->getStorageClassAsWritten());
307 Record
.push_back(D
->IsInline
);
308 Record
.push_back(D
->isInlineSpecified());
309 Record
.push_back(D
->isVirtualAsWritten());
310 Record
.push_back(D
->isPure());
311 Record
.push_back(D
->hasInheritedPrototype());
312 Record
.push_back(D
->hasWrittenPrototype());
313 Record
.push_back(D
->isDeleted());
314 Record
.push_back(D
->isTrivial());
315 Record
.push_back(D
->hasImplicitReturnZero());
316 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
318 Record
.push_back(D
->param_size());
319 for (FunctionDecl::param_iterator P
= D
->param_begin(), PEnd
= D
->param_end();
321 Writer
.AddDeclRef(*P
, Record
);
322 Code
= serialization::DECL_FUNCTION
;
325 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl
*D
) {
327 // FIXME: convert to LazyStmtPtr?
328 // Unlike C/C++, method bodies will never be in header files.
329 bool HasBodyStuff
= D
->getBody() != 0 ||
330 D
->getSelfDecl() != 0 || D
->getCmdDecl() != 0;
331 Record
.push_back(HasBodyStuff
);
333 Writer
.AddStmt(D
->getBody());
334 Writer
.AddDeclRef(D
->getSelfDecl(), Record
);
335 Writer
.AddDeclRef(D
->getCmdDecl(), Record
);
337 Record
.push_back(D
->isInstanceMethod());
338 Record
.push_back(D
->isVariadic());
339 Record
.push_back(D
->isSynthesized());
340 Record
.push_back(D
->isDefined());
341 // FIXME: stable encoding for @required/@optional
342 Record
.push_back(D
->getImplementationControl());
343 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
344 Record
.push_back(D
->getObjCDeclQualifier());
345 Record
.push_back(D
->getNumSelectorArgs());
346 Writer
.AddTypeRef(D
->getResultType(), Record
);
347 Writer
.AddTypeSourceInfo(D
->getResultTypeSourceInfo(), Record
);
348 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
349 Record
.push_back(D
->param_size());
350 for (ObjCMethodDecl::param_iterator P
= D
->param_begin(),
351 PEnd
= D
->param_end(); P
!= PEnd
; ++P
)
352 Writer
.AddDeclRef(*P
, Record
);
353 Code
= serialization::DECL_OBJC_METHOD
;
356 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl
*D
) {
358 Writer
.AddSourceRange(D
->getAtEndRange(), Record
);
359 // Abstract class (no need to define a stable serialization::DECL code).
362 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl
*D
) {
363 VisitObjCContainerDecl(D
);
364 Writer
.AddTypeRef(QualType(D
->getTypeForDecl(), 0), Record
);
365 Writer
.AddDeclRef(D
->getSuperClass(), Record
);
367 // Write out the protocols that are directly referenced by the @interface.
368 Record
.push_back(D
->ReferencedProtocols
.size());
369 for (ObjCInterfaceDecl::protocol_iterator P
= D
->protocol_begin(),
370 PEnd
= D
->protocol_end();
372 Writer
.AddDeclRef(*P
, Record
);
373 for (ObjCInterfaceDecl::protocol_loc_iterator PL
= D
->protocol_loc_begin(),
374 PLEnd
= D
->protocol_loc_end();
376 Writer
.AddSourceLocation(*PL
, Record
);
378 // Write out the protocols that are transitively referenced.
379 Record
.push_back(D
->AllReferencedProtocols
.size());
380 for (ObjCList
<ObjCProtocolDecl
>::iterator
381 P
= D
->AllReferencedProtocols
.begin(),
382 PEnd
= D
->AllReferencedProtocols
.end();
384 Writer
.AddDeclRef(*P
, Record
);
386 // Write out the ivars.
387 Record
.push_back(D
->ivar_size());
388 for (ObjCInterfaceDecl::ivar_iterator I
= D
->ivar_begin(),
389 IEnd
= D
->ivar_end(); I
!= IEnd
; ++I
)
390 Writer
.AddDeclRef(*I
, Record
);
391 Writer
.AddDeclRef(D
->getCategoryList(), Record
);
392 Record
.push_back(D
->isForwardDecl());
393 Record
.push_back(D
->isImplicitInterfaceDecl());
394 Writer
.AddSourceLocation(D
->getClassLoc(), Record
);
395 Writer
.AddSourceLocation(D
->getSuperClassLoc(), Record
);
396 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
397 Code
= serialization::DECL_OBJC_INTERFACE
;
400 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl
*D
) {
402 // FIXME: stable encoding for @public/@private/@protected/@package
403 Record
.push_back(D
->getAccessControl());
404 Record
.push_back(D
->getSynthesize());
405 Code
= serialization::DECL_OBJC_IVAR
;
408 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl
*D
) {
409 VisitObjCContainerDecl(D
);
410 Record
.push_back(D
->isForwardDecl());
411 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
412 Record
.push_back(D
->protocol_size());
413 for (ObjCProtocolDecl::protocol_iterator
414 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
415 Writer
.AddDeclRef(*I
, Record
);
416 for (ObjCProtocolDecl::protocol_loc_iterator PL
= D
->protocol_loc_begin(),
417 PLEnd
= D
->protocol_loc_end();
419 Writer
.AddSourceLocation(*PL
, Record
);
420 Code
= serialization::DECL_OBJC_PROTOCOL
;
423 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl
*D
) {
425 Code
= serialization::DECL_OBJC_AT_DEFS_FIELD
;
428 void ASTDeclWriter::VisitObjCClassDecl(ObjCClassDecl
*D
) {
430 Record
.push_back(D
->size());
431 for (ObjCClassDecl::iterator I
= D
->begin(), IEnd
= D
->end(); I
!= IEnd
; ++I
)
432 Writer
.AddDeclRef(I
->getInterface(), Record
);
433 for (ObjCClassDecl::iterator I
= D
->begin(), IEnd
= D
->end(); I
!= IEnd
; ++I
)
434 Writer
.AddSourceLocation(I
->getLocation(), Record
);
435 Code
= serialization::DECL_OBJC_CLASS
;
438 void ASTDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl
*D
) {
440 Record
.push_back(D
->protocol_size());
441 for (ObjCForwardProtocolDecl::protocol_iterator
442 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
443 Writer
.AddDeclRef(*I
, Record
);
444 for (ObjCForwardProtocolDecl::protocol_loc_iterator
445 PL
= D
->protocol_loc_begin(), PLEnd
= D
->protocol_loc_end();
447 Writer
.AddSourceLocation(*PL
, Record
);
448 Code
= serialization::DECL_OBJC_FORWARD_PROTOCOL
;
451 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl
*D
) {
452 VisitObjCContainerDecl(D
);
453 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
454 Record
.push_back(D
->protocol_size());
455 for (ObjCCategoryDecl::protocol_iterator
456 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
457 Writer
.AddDeclRef(*I
, Record
);
458 for (ObjCCategoryDecl::protocol_loc_iterator
459 PL
= D
->protocol_loc_begin(), PLEnd
= D
->protocol_loc_end();
461 Writer
.AddSourceLocation(*PL
, Record
);
462 Writer
.AddDeclRef(D
->getNextClassCategory(), Record
);
463 Record
.push_back(D
->hasSynthBitfield());
464 Writer
.AddSourceLocation(D
->getAtLoc(), Record
);
465 Writer
.AddSourceLocation(D
->getCategoryNameLoc(), Record
);
466 Code
= serialization::DECL_OBJC_CATEGORY
;
469 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl
*D
) {
471 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
472 Code
= serialization::DECL_OBJC_COMPATIBLE_ALIAS
;
475 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl
*D
) {
477 Writer
.AddSourceLocation(D
->getAtLoc(), Record
);
478 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
479 // FIXME: stable encoding
480 Record
.push_back((unsigned)D
->getPropertyAttributes());
481 Record
.push_back((unsigned)D
->getPropertyAttributesAsWritten());
482 // FIXME: stable encoding
483 Record
.push_back((unsigned)D
->getPropertyImplementation());
484 Writer
.AddDeclarationName(D
->getGetterName(), Record
);
485 Writer
.AddDeclarationName(D
->getSetterName(), Record
);
486 Writer
.AddDeclRef(D
->getGetterMethodDecl(), Record
);
487 Writer
.AddDeclRef(D
->getSetterMethodDecl(), Record
);
488 Writer
.AddDeclRef(D
->getPropertyIvarDecl(), Record
);
489 Code
= serialization::DECL_OBJC_PROPERTY
;
492 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl
*D
) {
493 VisitObjCContainerDecl(D
);
494 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
495 // Abstract class (no need to define a stable serialization::DECL code).
498 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl
*D
) {
499 VisitObjCImplDecl(D
);
500 Writer
.AddIdentifierRef(D
->getIdentifier(), Record
);
501 Code
= serialization::DECL_OBJC_CATEGORY_IMPL
;
504 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl
*D
) {
505 VisitObjCImplDecl(D
);
506 Writer
.AddDeclRef(D
->getSuperClass(), Record
);
507 Writer
.AddCXXCtorInitializers(D
->IvarInitializers
, D
->NumIvarInitializers
,
509 Record
.push_back(D
->hasSynthBitfield());
510 Code
= serialization::DECL_OBJC_IMPLEMENTATION
;
513 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl
*D
) {
515 Writer
.AddSourceLocation(D
->getLocStart(), Record
);
516 Writer
.AddDeclRef(D
->getPropertyDecl(), Record
);
517 Writer
.AddDeclRef(D
->getPropertyIvarDecl(), Record
);
518 Writer
.AddSourceLocation(D
->getPropertyIvarDeclLoc(), Record
);
519 Writer
.AddStmt(D
->getGetterCXXConstructor());
520 Writer
.AddStmt(D
->getSetterCXXAssignment());
521 Code
= serialization::DECL_OBJC_PROPERTY_IMPL
;
524 void ASTDeclWriter::VisitFieldDecl(FieldDecl
*D
) {
525 VisitDeclaratorDecl(D
);
526 Record
.push_back(D
->isMutable());
527 Record
.push_back(D
->getBitWidth()? 1 : 0);
528 if (D
->getBitWidth())
529 Writer
.AddStmt(D
->getBitWidth());
530 if (!D
->getDeclName())
531 Writer
.AddDeclRef(Context
.getInstantiatedFromUnnamedFieldDecl(D
), Record
);
532 Code
= serialization::DECL_FIELD
;
535 void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl
*D
) {
537 Record
.push_back(D
->getChainingSize());
539 for (IndirectFieldDecl::chain_iterator
540 P
= D
->chain_begin(),
541 PEnd
= D
->chain_end(); P
!= PEnd
; ++P
)
542 Writer
.AddDeclRef(*P
, Record
);
543 Code
= serialization::DECL_INDIRECTFIELD
;
546 void ASTDeclWriter::VisitVarDecl(VarDecl
*D
) {
547 VisitDeclaratorDecl(D
);
548 VisitRedeclarable(D
);
549 Record
.push_back(D
->getStorageClass()); // FIXME: stable encoding
550 Record
.push_back(D
->getStorageClassAsWritten());
551 Record
.push_back(D
->isThreadSpecified());
552 Record
.push_back(D
->hasCXXDirectInitializer());
553 Record
.push_back(D
->isExceptionVariable());
554 Record
.push_back(D
->isNRVOVariable());
555 Record
.push_back(D
->getInit() ? 1 : 0);
557 Writer
.AddStmt(D
->getInit());
559 MemberSpecializationInfo
*SpecInfo
560 = D
->isStaticDataMember() ? D
->getMemberSpecializationInfo() : 0;
561 Record
.push_back(SpecInfo
!= 0);
563 Writer
.AddDeclRef(SpecInfo
->getInstantiatedFrom(), Record
);
564 Record
.push_back(SpecInfo
->getTemplateSpecializationKind());
565 Writer
.AddSourceLocation(SpecInfo
->getPointOfInstantiation(), Record
);
568 Code
= serialization::DECL_VAR
;
571 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl
*D
) {
573 Code
= serialization::DECL_IMPLICIT_PARAM
;
576 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl
*D
) {
578 Record
.push_back(D
->getObjCDeclQualifier()); // FIXME: stable encoding
579 Record
.push_back(D
->hasInheritedDefaultArg());
580 Record
.push_back(D
->hasUninstantiatedDefaultArg());
581 if (D
->hasUninstantiatedDefaultArg())
582 Writer
.AddStmt(D
->getUninstantiatedDefaultArg());
583 Code
= serialization::DECL_PARM_VAR
;
585 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
586 // we dynamically check for the properties that we optimize for, but don't
587 // know are true of all PARM_VAR_DECLs.
588 if (!D
->getTypeSourceInfo() &&
592 D
->getAccess() == AS_none
&&
593 D
->getPCHLevel() == 0 &&
594 D
->getStorageClass() == 0 &&
595 !D
->hasCXXDirectInitializer() && // Can params have this ever?
596 D
->getObjCDeclQualifier() == 0 &&
597 !D
->hasInheritedDefaultArg() &&
599 !D
->hasUninstantiatedDefaultArg()) // No default expr.
600 AbbrevToUse
= Writer
.getParmVarDeclAbbrev();
602 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
603 // just us assuming it.
604 assert(!D
->isInvalidDecl() && "Shouldn't emit invalid decls");
605 assert(!D
->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
606 assert(D
->getAccess() == AS_none
&& "PARM_VAR_DECL can't be public/private");
607 assert(!D
->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
608 assert(D
->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
609 assert(!D
->isStaticDataMember() &&
610 "PARM_VAR_DECL can't be static data member");
613 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl
*D
) {
615 Writer
.AddStmt(D
->getAsmString());
616 Code
= serialization::DECL_FILE_SCOPE_ASM
;
619 void ASTDeclWriter::VisitBlockDecl(BlockDecl
*D
) {
621 Writer
.AddStmt(D
->getBody());
622 Writer
.AddTypeSourceInfo(D
->getSignatureAsWritten(), Record
);
623 Record
.push_back(D
->param_size());
624 for (FunctionDecl::param_iterator P
= D
->param_begin(), PEnd
= D
->param_end();
626 Writer
.AddDeclRef(*P
, Record
);
627 Record
.push_back(D
->capturesCXXThis());
628 Record
.push_back(D
->getNumCaptures());
629 for (BlockDecl::capture_iterator
630 i
= D
->capture_begin(), e
= D
->capture_end(); i
!= e
; ++i
) {
631 const BlockDecl::Capture
&capture
= *i
;
632 Writer
.AddDeclRef(capture
.getVariable(), Record
);
635 if (capture
.isByRef()) flags
|= 1;
636 if (capture
.isNested()) flags
|= 2;
637 if (capture
.hasCopyExpr()) flags
|= 4;
638 Record
.push_back(flags
);
640 if (capture
.hasCopyExpr()) Writer
.AddStmt(capture
.getCopyExpr());
643 Code
= serialization::DECL_BLOCK
;
646 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl
*D
) {
648 // FIXME: It might be nice to serialize the brace locations for this
649 // declaration, which don't seem to be readily available in the AST.
650 Record
.push_back(D
->getLanguage());
651 Record
.push_back(D
->hasBraces());
652 Code
= serialization::DECL_LINKAGE_SPEC
;
655 void ASTDeclWriter::VisitLabelDecl(LabelDecl
*D
) {
657 Code
= serialization::DECL_LABEL
;
661 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl
*D
) {
663 Record
.push_back(D
->isInline());
664 Writer
.AddSourceLocation(D
->getLBracLoc(), Record
);
665 Writer
.AddSourceLocation(D
->getRBracLoc(), Record
);
666 Writer
.AddDeclRef(D
->getNextNamespace(), Record
);
668 // Only write one reference--original or anonymous
669 Record
.push_back(D
->isOriginalNamespace());
670 if (D
->isOriginalNamespace())
671 Writer
.AddDeclRef(D
->getAnonymousNamespace(), Record
);
673 Writer
.AddDeclRef(D
->getOriginalNamespace(), Record
);
674 Code
= serialization::DECL_NAMESPACE
;
676 if (Writer
.hasChain() && !D
->isOriginalNamespace() &&
677 D
->getOriginalNamespace()->getPCHLevel() > 0) {
678 NamespaceDecl
*NS
= D
->getOriginalNamespace();
679 Writer
.AddUpdatedDeclContext(NS
);
681 // Make sure all visible decls are written. They will be recorded later.
682 NS
->lookup(DeclarationName());
683 StoredDeclsMap
*Map
= static_cast<StoredDeclsMap
*>(NS
->getLookupPtr());
685 for (StoredDeclsMap::iterator D
= Map
->begin(), DEnd
= Map
->end();
687 DeclContext::lookup_result Result
= D
->second
.getLookupResult();
688 while (Result
.first
!= Result
.second
) {
689 Writer
.GetDeclRef(*Result
.first
);
697 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl
*D
) {
699 Writer
.AddSourceLocation(D
->getNamespaceLoc(), Record
);
700 Writer
.AddSourceRange(D
->getQualifierRange(), Record
);
701 Writer
.AddNestedNameSpecifier(D
->getQualifier(), Record
);
702 Writer
.AddSourceLocation(D
->getTargetNameLoc(), Record
);
703 Writer
.AddDeclRef(D
->getNamespace(), Record
);
704 Code
= serialization::DECL_NAMESPACE_ALIAS
;
707 void ASTDeclWriter::VisitUsingDecl(UsingDecl
*D
) {
709 Writer
.AddSourceRange(D
->getNestedNameRange(), Record
);
710 Writer
.AddSourceLocation(D
->getUsingLocation(), Record
);
711 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameDecl(), Record
);
712 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
713 Writer
.AddDeclRef(D
->FirstUsingShadow
, Record
);
714 Record
.push_back(D
->isTypeName());
715 Writer
.AddDeclRef(Context
.getInstantiatedFromUsingDecl(D
), Record
);
716 Code
= serialization::DECL_USING
;
719 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl
*D
) {
721 Writer
.AddDeclRef(D
->getTargetDecl(), Record
);
722 Writer
.AddDeclRef(D
->UsingOrNextShadow
, Record
);
723 Writer
.AddDeclRef(Context
.getInstantiatedFromUsingShadowDecl(D
), Record
);
724 Code
= serialization::DECL_USING_SHADOW
;
727 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl
*D
) {
729 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
730 Writer
.AddSourceLocation(D
->getNamespaceKeyLocation(), Record
);
731 Writer
.AddSourceRange(D
->getQualifierRange(), Record
);
732 Writer
.AddNestedNameSpecifier(D
->getQualifier(), Record
);
733 Writer
.AddDeclRef(D
->getNominatedNamespace(), Record
);
734 Writer
.AddDeclRef(dyn_cast
<Decl
>(D
->getCommonAncestor()), Record
);
735 Code
= serialization::DECL_USING_DIRECTIVE
;
738 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl
*D
) {
740 Writer
.AddSourceRange(D
->getTargetNestedNameRange(), Record
);
741 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
742 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameSpecifier(), Record
);
743 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
744 Code
= serialization::DECL_UNRESOLVED_USING_VALUE
;
747 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
748 UnresolvedUsingTypenameDecl
*D
) {
750 Writer
.AddSourceRange(D
->getTargetNestedNameRange(), Record
);
751 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
752 Writer
.AddSourceLocation(D
->getTypenameLoc(), Record
);
753 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameSpecifier(), Record
);
754 Code
= serialization::DECL_UNRESOLVED_USING_TYPENAME
;
757 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl
*D
) {
760 CXXRecordDecl
*DefinitionDecl
= 0;
761 if (D
->DefinitionData
)
762 DefinitionDecl
= D
->DefinitionData
->Definition
;
763 Writer
.AddDeclRef(DefinitionDecl
, Record
);
764 if (D
== DefinitionDecl
)
765 Writer
.AddCXXDefinitionData(D
, Record
);
768 CXXRecNotTemplate
= 0, CXXRecTemplate
, CXXRecMemberSpecialization
770 if (ClassTemplateDecl
*TemplD
= D
->getDescribedClassTemplate()) {
771 Record
.push_back(CXXRecTemplate
);
772 Writer
.AddDeclRef(TemplD
, Record
);
773 } else if (MemberSpecializationInfo
*MSInfo
774 = D
->getMemberSpecializationInfo()) {
775 Record
.push_back(CXXRecMemberSpecialization
);
776 Writer
.AddDeclRef(MSInfo
->getInstantiatedFrom(), Record
);
777 Record
.push_back(MSInfo
->getTemplateSpecializationKind());
778 Writer
.AddSourceLocation(MSInfo
->getPointOfInstantiation(), Record
);
780 Record
.push_back(CXXRecNotTemplate
);
783 // Store the key function to avoid deserializing every method so we can
786 Writer
.AddDeclRef(Context
.getKeyFunction(D
), Record
);
788 Code
= serialization::DECL_CXX_RECORD
;
791 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl
*D
) {
792 VisitFunctionDecl(D
);
793 Record
.push_back(D
->size_overridden_methods());
794 for (CXXMethodDecl::method_iterator
795 I
= D
->begin_overridden_methods(), E
= D
->end_overridden_methods();
797 Writer
.AddDeclRef(*I
, Record
);
798 Code
= serialization::DECL_CXX_METHOD
;
801 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl
*D
) {
802 VisitCXXMethodDecl(D
);
804 Record
.push_back(D
->IsExplicitSpecified
);
805 Record
.push_back(D
->ImplicitlyDefined
);
806 Writer
.AddCXXCtorInitializers(D
->CtorInitializers
, D
->NumCtorInitializers
,
809 Code
= serialization::DECL_CXX_CONSTRUCTOR
;
812 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl
*D
) {
813 VisitCXXMethodDecl(D
);
815 Record
.push_back(D
->ImplicitlyDefined
);
816 Writer
.AddDeclRef(D
->OperatorDelete
, Record
);
818 Code
= serialization::DECL_CXX_DESTRUCTOR
;
821 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl
*D
) {
822 VisitCXXMethodDecl(D
);
823 Record
.push_back(D
->IsExplicitSpecified
);
824 Code
= serialization::DECL_CXX_CONVERSION
;
827 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl
*D
) {
829 Writer
.AddSourceLocation(D
->getColonLoc(), Record
);
830 Code
= serialization::DECL_ACCESS_SPEC
;
833 void ASTDeclWriter::VisitFriendDecl(FriendDecl
*D
) {
835 Record
.push_back(D
->Friend
.is
<TypeSourceInfo
*>());
836 if (D
->Friend
.is
<TypeSourceInfo
*>())
837 Writer
.AddTypeSourceInfo(D
->Friend
.get
<TypeSourceInfo
*>(), Record
);
839 Writer
.AddDeclRef(D
->Friend
.get
<NamedDecl
*>(), Record
);
840 Writer
.AddDeclRef(D
->getNextFriend(), Record
);
841 Record
.push_back(D
->UnsupportedFriend
);
842 Writer
.AddSourceLocation(D
->FriendLoc
, Record
);
843 Code
= serialization::DECL_FRIEND
;
846 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl
*D
) {
848 Record
.push_back(D
->getNumTemplateParameters());
849 for (unsigned i
= 0, e
= D
->getNumTemplateParameters(); i
!= e
; ++i
)
850 Writer
.AddTemplateParameterList(D
->getTemplateParameterList(i
), Record
);
851 Record
.push_back(D
->getFriendDecl() != 0);
852 if (D
->getFriendDecl())
853 Writer
.AddDeclRef(D
->getFriendDecl(), Record
);
855 Writer
.AddTypeSourceInfo(D
->getFriendType(), Record
);
856 Writer
.AddSourceLocation(D
->getFriendLoc(), Record
);
857 Code
= serialization::DECL_FRIEND_TEMPLATE
;
860 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl
*D
) {
863 Writer
.AddDeclRef(D
->getTemplatedDecl(), Record
);
864 Writer
.AddTemplateParameterList(D
->getTemplateParameters(), Record
);
867 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl
*D
) {
868 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
869 // getCommonPtr() can be used while this is still initializing.
871 Writer
.AddDeclRef(D
->getPreviousDeclaration(), Record
);
872 if (D
->getPreviousDeclaration())
873 Writer
.AddDeclRef(D
->getFirstDeclaration(), Record
);
875 if (D
->getPreviousDeclaration() == 0) {
876 // This TemplateDecl owns the CommonPtr; write it.
877 assert(D
->isCanonicalDecl());
879 Writer
.AddDeclRef(D
->getInstantiatedFromMemberTemplate(), Record
);
880 if (D
->getInstantiatedFromMemberTemplate())
881 Record
.push_back(D
->isMemberSpecialization());
883 Writer
.AddDeclRef(D
->getCommonPtr()->Latest
, Record
);
885 RedeclarableTemplateDecl
*First
= D
->getFirstDeclaration();
887 // If this is a most recent redeclaration that is pointed to by a first decl
888 // in a chained PCH, keep track of the association with the map so we can
889 // update the first decl during AST reading.
890 if (First
->getMostRecentDeclaration() == D
&&
891 First
->getPCHLevel() > D
->getPCHLevel()) {
892 assert(Writer
.FirstLatestDecls
.find(First
)==Writer
.FirstLatestDecls
.end()
893 && "The latest is already set");
894 Writer
.FirstLatestDecls
[First
] = D
;
898 VisitTemplateDecl(D
);
899 Record
.push_back(D
->getIdentifierNamespace());
902 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl
*D
) {
903 VisitRedeclarableTemplateDecl(D
);
905 if (D
->getPreviousDeclaration() == 0) {
906 typedef llvm::FoldingSet
<ClassTemplateSpecializationDecl
> CTSDSetTy
;
907 CTSDSetTy
&CTSDSet
= D
->getSpecializations();
908 Record
.push_back(CTSDSet
.size());
909 for (CTSDSetTy::iterator I
=CTSDSet
.begin(), E
= CTSDSet
.end(); I
!=E
; ++I
) {
910 assert(I
->isCanonicalDecl() && "Expected only canonical decls in set");
911 Writer
.AddDeclRef(&*I
, Record
);
914 typedef llvm::FoldingSet
<ClassTemplatePartialSpecializationDecl
> CTPSDSetTy
;
915 CTPSDSetTy
&CTPSDSet
= D
->getPartialSpecializations();
916 Record
.push_back(CTPSDSet
.size());
917 for (CTPSDSetTy::iterator I
=CTPSDSet
.begin(), E
=CTPSDSet
.end(); I
!=E
; ++I
) {
918 assert(I
->isCanonicalDecl() && "Expected only canonical decls in set");
919 Writer
.AddDeclRef(&*I
, Record
);
922 // InjectedClassNameType is computed, no need to write it.
924 Code
= serialization::DECL_CLASS_TEMPLATE
;
927 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
928 ClassTemplateSpecializationDecl
*D
) {
929 VisitCXXRecordDecl(D
);
931 llvm::PointerUnion
<ClassTemplateDecl
*,
932 ClassTemplatePartialSpecializationDecl
*> InstFrom
933 = D
->getSpecializedTemplateOrPartial();
935 if (InstFrom
.is
<ClassTemplateDecl
*>()) {
936 InstFromD
= InstFrom
.get
<ClassTemplateDecl
*>();
937 Writer
.AddDeclRef(InstFromD
, Record
);
939 InstFromD
= InstFrom
.get
<ClassTemplatePartialSpecializationDecl
*>();
940 Writer
.AddDeclRef(InstFromD
, Record
);
941 Writer
.AddTemplateArgumentList(&D
->getTemplateInstantiationArgs(), Record
);
942 InstFromD
= cast
<ClassTemplatePartialSpecializationDecl
>(InstFromD
)->
943 getSpecializedTemplate();
947 Writer
.AddTypeSourceInfo(D
->getTypeAsWritten(), Record
);
948 if (D
->getTypeAsWritten()) {
949 Writer
.AddSourceLocation(D
->getExternLoc(), Record
);
950 Writer
.AddSourceLocation(D
->getTemplateKeywordLoc(), Record
);
953 Writer
.AddTemplateArgumentList(&D
->getTemplateArgs(), Record
);
954 Writer
.AddSourceLocation(D
->getPointOfInstantiation(), Record
);
955 Record
.push_back(D
->getSpecializationKind());
957 if (D
->isCanonicalDecl()) {
958 // When reading, we'll add it to the folding set of the following template.
959 Writer
.AddDeclRef(D
->getSpecializedTemplate()->getCanonicalDecl(), Record
);
962 Code
= serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION
;
965 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
966 ClassTemplatePartialSpecializationDecl
*D
) {
967 VisitClassTemplateSpecializationDecl(D
);
969 Writer
.AddTemplateParameterList(D
->getTemplateParameters(), Record
);
971 Record
.push_back(D
->getNumTemplateArgsAsWritten());
972 for (int i
= 0, e
= D
->getNumTemplateArgsAsWritten(); i
!= e
; ++i
)
973 Writer
.AddTemplateArgumentLoc(D
->getTemplateArgsAsWritten()[i
], Record
);
975 Record
.push_back(D
->getSequenceNumber());
977 // These are read/set from/to the first declaration.
978 if (D
->getPreviousDeclaration() == 0) {
979 Writer
.AddDeclRef(D
->getInstantiatedFromMember(), Record
);
980 Record
.push_back(D
->isMemberSpecialization());
983 Code
= serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
;
986 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl
*D
) {
987 VisitRedeclarableTemplateDecl(D
);
989 if (D
->getPreviousDeclaration() == 0) {
990 // This FunctionTemplateDecl owns the CommonPtr; write it.
992 // Write the function specialization declarations.
993 Record
.push_back(D
->getSpecializations().size());
994 for (llvm::FoldingSet
<FunctionTemplateSpecializationInfo
>::iterator
995 I
= D
->getSpecializations().begin(),
996 E
= D
->getSpecializations().end() ; I
!= E
; ++I
) {
997 assert(I
->Function
->isCanonicalDecl() &&
998 "Expected only canonical decls in set");
999 Writer
.AddDeclRef(I
->Function
, Record
);
1002 Code
= serialization::DECL_FUNCTION_TEMPLATE
;
1005 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl
*D
) {
1008 Record
.push_back(D
->wasDeclaredWithTypename());
1009 Record
.push_back(D
->isParameterPack());
1010 Record
.push_back(D
->defaultArgumentWasInherited());
1011 Writer
.AddTypeSourceInfo(D
->getDefaultArgumentInfo(), Record
);
1013 Code
= serialization::DECL_TEMPLATE_TYPE_PARM
;
1016 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl
*D
) {
1017 // For an expanded parameter pack, record the number of expansion types here
1018 // so that it's easier for
1019 if (D
->isExpandedParameterPack())
1020 Record
.push_back(D
->getNumExpansionTypes());
1022 VisitDeclaratorDecl(D
);
1023 // TemplateParmPosition.
1024 Record
.push_back(D
->getDepth());
1025 Record
.push_back(D
->getPosition());
1027 if (D
->isExpandedParameterPack()) {
1028 for (unsigned I
= 0, N
= D
->getNumExpansionTypes(); I
!= N
; ++I
) {
1029 Writer
.AddTypeRef(D
->getExpansionType(I
), Record
);
1030 Writer
.AddTypeSourceInfo(D
->getExpansionTypeSourceInfo(I
), Record
);
1033 Code
= serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK
;
1035 // Rest of NonTypeTemplateParmDecl.
1036 Record
.push_back(D
->isParameterPack());
1037 Record
.push_back(D
->getDefaultArgument() != 0);
1038 if (D
->getDefaultArgument()) {
1039 Writer
.AddStmt(D
->getDefaultArgument());
1040 Record
.push_back(D
->defaultArgumentWasInherited());
1042 Code
= serialization::DECL_NON_TYPE_TEMPLATE_PARM
;
1046 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl
*D
) {
1047 VisitTemplateDecl(D
);
1048 // TemplateParmPosition.
1049 Record
.push_back(D
->getDepth());
1050 Record
.push_back(D
->getPosition());
1051 // Rest of TemplateTemplateParmDecl.
1052 Writer
.AddTemplateArgumentLoc(D
->getDefaultArgument(), Record
);
1053 Record
.push_back(D
->defaultArgumentWasInherited());
1054 Record
.push_back(D
->isParameterPack());
1055 Code
= serialization::DECL_TEMPLATE_TEMPLATE_PARM
;
1058 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl
*D
) {
1060 Writer
.AddStmt(D
->getAssertExpr());
1061 Writer
.AddStmt(D
->getMessage());
1062 Code
= serialization::DECL_STATIC_ASSERT
;
1065 /// \brief Emit the DeclContext part of a declaration context decl.
1067 /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
1068 /// block for this declaration context is stored. May be 0 to indicate
1069 /// that there are no declarations stored within this context.
1071 /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
1072 /// block for this declaration context is stored. May be 0 to indicate
1073 /// that there are no declarations visible from this context. Note
1074 /// that this value will not be emitted for non-primary declaration
1076 void ASTDeclWriter::VisitDeclContext(DeclContext
*DC
, uint64_t LexicalOffset
,
1077 uint64_t VisibleOffset
) {
1078 Record
.push_back(LexicalOffset
);
1079 Record
.push_back(VisibleOffset
);
1082 template <typename T
>
1083 void ASTDeclWriter::VisitRedeclarable(Redeclarable
<T
> *D
) {
1084 enum { NoRedeclaration
= 0, PointsToPrevious
, PointsToLatest
};
1085 if (D
->RedeclLink
.getNext() == D
) {
1086 Record
.push_back(NoRedeclaration
);
1088 if (D
->RedeclLink
.NextIsPrevious()) {
1089 Record
.push_back(PointsToPrevious
);
1090 Writer
.AddDeclRef(D
->getPreviousDeclaration(), Record
);
1091 Writer
.AddDeclRef(D
->getFirstDeclaration(), Record
);
1093 Record
.push_back(PointsToLatest
);
1094 Writer
.AddDeclRef(D
->RedeclLink
.getPointer(), Record
);
1098 T
*First
= D
->getFirstDeclaration();
1099 T
*ThisDecl
= static_cast<T
*>(D
);
1100 // If this is a most recent redeclaration that is pointed to by a first decl
1101 // in a chained PCH, keep track of the association with the map so we can
1102 // update the first decl during AST reading.
1103 if (ThisDecl
!= First
&& First
->getMostRecentDeclaration() == ThisDecl
&&
1104 First
->getPCHLevel() > ThisDecl
->getPCHLevel()) {
1105 assert(Writer
.FirstLatestDecls
.find(First
) == Writer
.FirstLatestDecls
.end()
1106 && "The latest is already set");
1107 Writer
.FirstLatestDecls
[First
] = ThisDecl
;
1111 //===----------------------------------------------------------------------===//
1112 // ASTWriter Implementation
1113 //===----------------------------------------------------------------------===//
1115 void ASTWriter::WriteDeclsBlockAbbrevs() {
1116 using namespace llvm
;
1117 // Abbreviation for DECL_PARM_VAR.
1118 BitCodeAbbrev
*Abv
= new BitCodeAbbrev();
1119 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR
));
1122 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // DeclContext
1123 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // LexicalDeclContext
1124 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Location
1125 Abv
->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
1126 Abv
->Add(BitCodeAbbrevOp(0)); // HasAttrs
1127 Abv
->Add(BitCodeAbbrevOp(0)); // isImplicit
1128 Abv
->Add(BitCodeAbbrevOp(0)); // isUsed
1129 Abv
->Add(BitCodeAbbrevOp(AS_none
)); // C++ AccessSpecifier
1130 Abv
->Add(BitCodeAbbrevOp(0)); // PCH level
1133 Abv
->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
1134 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Name
1136 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Type
1138 Abv
->Add(BitCodeAbbrevOp(0)); // hasExtInfo
1139 Abv
->Add(BitCodeAbbrevOp(serialization::PREDEF_TYPE_NULL_ID
)); // InfoType
1141 Abv
->Add(BitCodeAbbrevOp(0)); // No redeclaration
1142 Abv
->Add(BitCodeAbbrevOp(0)); // StorageClass
1143 Abv
->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
1144 Abv
->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
1145 Abv
->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
1146 Abv
->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
1147 Abv
->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
1148 Abv
->Add(BitCodeAbbrevOp(0)); // HasInit
1149 Abv
->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo
1151 Abv
->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
1152 Abv
->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
1153 Abv
->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg
1155 ParmVarDeclAbbrev
= Stream
.EmitAbbrev(Abv
);
1157 Abv
= new BitCodeAbbrev();
1158 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL
));
1159 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1160 DeclContextLexicalAbbrev
= Stream
.EmitAbbrev(Abv
);
1162 Abv
= new BitCodeAbbrev();
1163 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE
));
1164 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
1165 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1166 DeclContextVisibleLookupAbbrev
= Stream
.EmitAbbrev(Abv
);
1169 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
1170 /// consumers of the AST.
1172 /// Such decls will always be deserialized from the AST file, so we would like
1173 /// this to be as restrictive as possible. Currently the predicate is driven by
1174 /// code generation requirements, if other clients have a different notion of
1175 /// what is "required" then we may have to consider an alternate scheme where
1176 /// clients can iterate over the top-level decls and get information on them,
1177 /// without necessary deserializing them. We could explicitly require such
1178 /// clients to use a separate API call to "realize" the decl. This should be
1179 /// relatively painless since they would presumably only do it for top-level
1181 static bool isRequiredDecl(const Decl
*D
, ASTContext
&Context
) {
1182 // File scoped assembly or obj-c implementation must be seen.
1183 if (isa
<FileScopeAsmDecl
>(D
) || isa
<ObjCImplementationDecl
>(D
))
1186 return Context
.DeclMustBeEmitted(D
);
1189 void ASTWriter::WriteDecl(ASTContext
&Context
, Decl
*D
) {
1190 // Switch case IDs are per Decl.
1191 ClearSwitchCaseIDs();
1194 ASTDeclWriter
W(*this, Context
, Record
);
1196 // If this declaration is also a DeclContext, write blocks for the
1197 // declarations that lexically stored inside its context and those
1198 // declarations that are visible from its context. These blocks
1199 // are written before the declaration itself so that we can put
1200 // their offsets into the record for the declaration.
1201 uint64_t LexicalOffset
= 0;
1202 uint64_t VisibleOffset
= 0;
1203 DeclContext
*DC
= dyn_cast
<DeclContext
>(D
);
1205 LexicalOffset
= WriteDeclContextLexicalBlock(Context
, DC
);
1206 VisibleOffset
= WriteDeclContextVisibleBlock(Context
, DC
);
1209 // Determine the ID for this declaration
1210 serialization::DeclID
&IDR
= DeclIDs
[D
];
1213 serialization::DeclID ID
= IDR
;
1215 if (ID
< FirstDeclID
) {
1216 // We're replacing a decl in a previous file.
1217 ReplacedDecls
.push_back(std::make_pair(ID
, Stream
.GetCurrentBitNo()));
1219 unsigned Index
= ID
- FirstDeclID
;
1221 // Record the offset for this declaration
1222 if (DeclOffsets
.size() == Index
)
1223 DeclOffsets
.push_back(Stream
.GetCurrentBitNo());
1224 else if (DeclOffsets
.size() < Index
) {
1225 DeclOffsets
.resize(Index
+1);
1226 DeclOffsets
[Index
] = Stream
.GetCurrentBitNo();
1230 // Build and emit a record for this declaration
1232 W
.Code
= (serialization::DeclCode
)0;
1235 if (DC
) W
.VisitDeclContext(DC
, LexicalOffset
, VisibleOffset
);
1238 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
1239 D
->getDeclKindName() + "'");
1240 Stream
.EmitRecord(W
.Code
, Record
, W
.AbbrevToUse
);
1242 // Flush any expressions that were written as part of this declaration.
1245 // Flush C++ base specifiers, if there are any.
1246 FlushCXXBaseSpecifiers();
1248 // Note "external" declarations so that we can add them to a record in the
1251 // FIXME: This should be renamed, the predicate is much more complicated.
1252 if (isRequiredDecl(D
, Context
))
1253 ExternalDefinitions
.push_back(ID
);