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
;
37 void WriteCXXDefinitionData(struct CXXRecordDecl::DefinitionData
&Data
);
39 serialization::DeclCode Code
;
42 ASTDeclWriter(ASTWriter
&Writer
, ASTContext
&Context
, RecordData
&Record
)
43 : Writer(Writer
), Context(Context
), Record(Record
) {
48 void VisitDecl(Decl
*D
);
49 void VisitTranslationUnitDecl(TranslationUnitDecl
*D
);
50 void VisitNamedDecl(NamedDecl
*D
);
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 VisitVarDecl(VarDecl
*D
);
77 void VisitImplicitParamDecl(ImplicitParamDecl
*D
);
78 void VisitParmVarDecl(ParmVarDecl
*D
);
79 void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl
*D
);
80 void VisitTemplateDecl(TemplateDecl
*D
);
81 void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl
*D
);
82 void VisitClassTemplateDecl(ClassTemplateDecl
*D
);
83 void VisitFunctionTemplateDecl(FunctionTemplateDecl
*D
);
84 void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl
*D
);
85 void VisitUsingDecl(UsingDecl
*D
);
86 void VisitUsingShadowDecl(UsingShadowDecl
*D
);
87 void VisitLinkageSpecDecl(LinkageSpecDecl
*D
);
88 void VisitFileScopeAsmDecl(FileScopeAsmDecl
*D
);
89 void VisitAccessSpecDecl(AccessSpecDecl
*D
);
90 void VisitFriendDecl(FriendDecl
*D
);
91 void VisitFriendTemplateDecl(FriendTemplateDecl
*D
);
92 void VisitStaticAssertDecl(StaticAssertDecl
*D
);
93 void VisitBlockDecl(BlockDecl
*D
);
95 void VisitDeclContext(DeclContext
*DC
, uint64_t LexicalOffset
,
96 uint64_t VisibleOffset
);
97 template <typename T
> void VisitRedeclarable(Redeclarable
<T
> *D
);
100 // FIXME: Put in the same order is DeclNodes.td?
101 void VisitObjCMethodDecl(ObjCMethodDecl
*D
);
102 void VisitObjCContainerDecl(ObjCContainerDecl
*D
);
103 void VisitObjCInterfaceDecl(ObjCInterfaceDecl
*D
);
104 void VisitObjCIvarDecl(ObjCIvarDecl
*D
);
105 void VisitObjCProtocolDecl(ObjCProtocolDecl
*D
);
106 void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl
*D
);
107 void VisitObjCClassDecl(ObjCClassDecl
*D
);
108 void VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl
*D
);
109 void VisitObjCCategoryDecl(ObjCCategoryDecl
*D
);
110 void VisitObjCImplDecl(ObjCImplDecl
*D
);
111 void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl
*D
);
112 void VisitObjCImplementationDecl(ObjCImplementationDecl
*D
);
113 void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl
*D
);
114 void VisitObjCPropertyDecl(ObjCPropertyDecl
*D
);
115 void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl
*D
);
119 void ASTDeclWriter::Visit(Decl
*D
) {
120 DeclVisitor
<ASTDeclWriter
>::Visit(D
);
122 // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
123 // have been written. We want it last because we will not read it back when
124 // retrieving it from the AST, we'll just lazily set the offset.
125 if (FunctionDecl
*FD
= dyn_cast
<FunctionDecl
>(D
)) {
126 Record
.push_back(FD
->isThisDeclarationADefinition());
127 if (FD
->isThisDeclarationADefinition())
128 Writer
.AddStmt(FD
->getBody());
132 void ASTDeclWriter::VisitDecl(Decl
*D
) {
133 Writer
.AddDeclRef(cast_or_null
<Decl
>(D
->getDeclContext()), Record
);
134 Writer
.AddDeclRef(cast_or_null
<Decl
>(D
->getLexicalDeclContext()), Record
);
135 Writer
.AddSourceLocation(D
->getLocation(), Record
);
136 Record
.push_back(D
->isInvalidDecl());
137 Record
.push_back(D
->hasAttrs());
139 Writer
.WriteAttributes(D
->getAttrs(), Record
);
140 Record
.push_back(D
->isImplicit());
141 Record
.push_back(D
->isUsed(false));
142 Record
.push_back(D
->getAccess());
143 Record
.push_back(D
->getPCHLevel());
146 void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl
*D
) {
148 Writer
.AddDeclRef(D
->getAnonymousNamespace(), Record
);
149 Code
= serialization::DECL_TRANSLATION_UNIT
;
152 void ASTDeclWriter::VisitNamedDecl(NamedDecl
*D
) {
154 Writer
.AddDeclarationName(D
->getDeclName(), Record
);
157 void ASTDeclWriter::VisitTypeDecl(TypeDecl
*D
) {
159 Writer
.AddTypeRef(QualType(D
->getTypeForDecl(), 0), Record
);
162 void ASTDeclWriter::VisitTypedefDecl(TypedefDecl
*D
) {
164 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
165 Code
= serialization::DECL_TYPEDEF
;
168 void ASTDeclWriter::VisitTagDecl(TagDecl
*D
) {
170 VisitRedeclarable(D
);
171 Record
.push_back(D
->getIdentifierNamespace());
172 Record
.push_back((unsigned)D
->getTagKind()); // FIXME: stable encoding
173 Record
.push_back(D
->isDefinition());
174 Record
.push_back(D
->isEmbeddedInDeclarator());
175 Writer
.AddSourceLocation(D
->getRBraceLoc(), Record
);
176 Writer
.AddSourceLocation(D
->getTagKeywordLoc(), Record
);
177 Record
.push_back(D
->hasExtInfo());
179 Writer
.AddQualifierInfo(*D
->getExtInfo(), Record
);
181 Writer
.AddDeclRef(D
->getTypedefForAnonDecl(), Record
);
184 void ASTDeclWriter::VisitEnumDecl(EnumDecl
*D
) {
186 Writer
.AddTypeSourceInfo(D
->getIntegerTypeSourceInfo(), Record
);
187 if (!D
->getIntegerTypeSourceInfo())
188 Writer
.AddTypeRef(D
->getIntegerType(), Record
);
189 Writer
.AddTypeRef(D
->getPromotionType(), Record
);
190 Record
.push_back(D
->getNumPositiveBits());
191 Record
.push_back(D
->getNumNegativeBits());
192 Record
.push_back(D
->isScoped());
193 Record
.push_back(D
->isFixed());
194 Writer
.AddDeclRef(D
->getInstantiatedFromMemberEnum(), Record
);
195 Code
= serialization::DECL_ENUM
;
198 void ASTDeclWriter::VisitRecordDecl(RecordDecl
*D
) {
200 Record
.push_back(D
->hasFlexibleArrayMember());
201 Record
.push_back(D
->isAnonymousStructOrUnion());
202 Record
.push_back(D
->hasObjectMember());
203 Code
= serialization::DECL_RECORD
;
206 void ASTDeclWriter::VisitValueDecl(ValueDecl
*D
) {
208 Writer
.AddTypeRef(D
->getType(), Record
);
211 void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl
*D
) {
213 Record
.push_back(D
->getInitExpr()? 1 : 0);
214 if (D
->getInitExpr())
215 Writer
.AddStmt(D
->getInitExpr());
216 Writer
.AddAPSInt(D
->getInitVal(), Record
);
217 Code
= serialization::DECL_ENUM_CONSTANT
;
220 void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl
*D
) {
222 Record
.push_back(D
->hasExtInfo());
224 Writer
.AddQualifierInfo(*D
->getExtInfo(), Record
);
225 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
228 void ASTDeclWriter::VisitFunctionDecl(FunctionDecl
*D
) {
229 VisitDeclaratorDecl(D
);
230 VisitRedeclarable(D
);
232 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
233 Record
.push_back(D
->getIdentifierNamespace());
234 Record
.push_back(D
->getTemplatedKind());
235 switch (D
->getTemplatedKind()) {
236 default: assert(false && "Unhandled TemplatedKind!");
238 case FunctionDecl::TK_NonTemplate
:
240 case FunctionDecl::TK_FunctionTemplate
:
241 Writer
.AddDeclRef(D
->getDescribedFunctionTemplate(), Record
);
243 case FunctionDecl::TK_MemberSpecialization
: {
244 MemberSpecializationInfo
*MemberInfo
= D
->getMemberSpecializationInfo();
245 Writer
.AddDeclRef(MemberInfo
->getInstantiatedFrom(), Record
);
246 Record
.push_back(MemberInfo
->getTemplateSpecializationKind());
247 Writer
.AddSourceLocation(MemberInfo
->getPointOfInstantiation(), Record
);
250 case FunctionDecl::TK_FunctionTemplateSpecialization
: {
251 FunctionTemplateSpecializationInfo
*
252 FTSInfo
= D
->getTemplateSpecializationInfo();
253 Writer
.AddDeclRef(FTSInfo
->getTemplate(), Record
);
254 Record
.push_back(FTSInfo
->getTemplateSpecializationKind());
256 // Template arguments.
257 Writer
.AddTemplateArgumentList(FTSInfo
->TemplateArguments
, Record
);
259 // Template args as written.
260 Record
.push_back(FTSInfo
->TemplateArgumentsAsWritten
!= 0);
261 if (FTSInfo
->TemplateArgumentsAsWritten
) {
262 Record
.push_back(FTSInfo
->TemplateArgumentsAsWritten
->size());
263 for (int i
=0, e
= FTSInfo
->TemplateArgumentsAsWritten
->size(); i
!=e
; ++i
)
264 Writer
.AddTemplateArgumentLoc((*FTSInfo
->TemplateArgumentsAsWritten
)[i
],
266 Writer
.AddSourceLocation(FTSInfo
->TemplateArgumentsAsWritten
->getLAngleLoc(),
268 Writer
.AddSourceLocation(FTSInfo
->TemplateArgumentsAsWritten
->getRAngleLoc(),
272 Writer
.AddSourceLocation(FTSInfo
->getPointOfInstantiation(), Record
);
274 if (D
->isCanonicalDecl()) {
275 // Write the template that contains the specializations set. We will
276 // add a FunctionTemplateSpecializationInfo to it when reading.
277 Writer
.AddDeclRef(FTSInfo
->getTemplate()->getCanonicalDecl(), Record
);
281 case FunctionDecl::TK_DependentFunctionTemplateSpecialization
: {
282 DependentFunctionTemplateSpecializationInfo
*
283 DFTSInfo
= D
->getDependentSpecializationInfo();
286 Record
.push_back(DFTSInfo
->getNumTemplates());
287 for (int i
=0, e
= DFTSInfo
->getNumTemplates(); i
!= e
; ++i
)
288 Writer
.AddDeclRef(DFTSInfo
->getTemplate(i
), Record
);
291 Record
.push_back(DFTSInfo
->getNumTemplateArgs());
292 for (int i
=0, e
= DFTSInfo
->getNumTemplateArgs(); i
!= e
; ++i
)
293 Writer
.AddTemplateArgumentLoc(DFTSInfo
->getTemplateArg(i
), Record
);
294 Writer
.AddSourceLocation(DFTSInfo
->getLAngleLoc(), Record
);
295 Writer
.AddSourceLocation(DFTSInfo
->getRAngleLoc(), Record
);
300 // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
301 // after everything else is written.
303 Record
.push_back(D
->getStorageClass()); // FIXME: stable encoding
304 Record
.push_back(D
->getStorageClassAsWritten());
305 Record
.push_back(D
->isInlineSpecified());
306 Record
.push_back(D
->isVirtualAsWritten());
307 Record
.push_back(D
->isPure());
308 Record
.push_back(D
->hasInheritedPrototype());
309 Record
.push_back(D
->hasWrittenPrototype());
310 Record
.push_back(D
->isDeleted());
311 Record
.push_back(D
->isTrivial());
312 Record
.push_back(D
->hasImplicitReturnZero());
313 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
315 Record
.push_back(D
->param_size());
316 for (FunctionDecl::param_iterator P
= D
->param_begin(), PEnd
= D
->param_end();
318 Writer
.AddDeclRef(*P
, Record
);
319 Code
= serialization::DECL_FUNCTION
;
322 void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl
*D
) {
324 // FIXME: convert to LazyStmtPtr?
325 // Unlike C/C++, method bodies will never be in header files.
326 bool HasBodyStuff
= D
->getBody() != 0 ||
327 D
->getSelfDecl() != 0 || D
->getCmdDecl() != 0;
328 Record
.push_back(HasBodyStuff
);
330 Writer
.AddStmt(D
->getBody());
331 Writer
.AddDeclRef(D
->getSelfDecl(), Record
);
332 Writer
.AddDeclRef(D
->getCmdDecl(), Record
);
334 Record
.push_back(D
->isInstanceMethod());
335 Record
.push_back(D
->isVariadic());
336 Record
.push_back(D
->isSynthesized());
337 Record
.push_back(D
->isDefined());
338 // FIXME: stable encoding for @required/@optional
339 Record
.push_back(D
->getImplementationControl());
340 // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
341 Record
.push_back(D
->getObjCDeclQualifier());
342 Record
.push_back(D
->getNumSelectorArgs());
343 Writer
.AddTypeRef(D
->getResultType(), Record
);
344 Writer
.AddTypeSourceInfo(D
->getResultTypeSourceInfo(), Record
);
345 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
346 Record
.push_back(D
->param_size());
347 for (ObjCMethodDecl::param_iterator P
= D
->param_begin(),
348 PEnd
= D
->param_end(); P
!= PEnd
; ++P
)
349 Writer
.AddDeclRef(*P
, Record
);
350 Code
= serialization::DECL_OBJC_METHOD
;
353 void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl
*D
) {
355 Writer
.AddSourceRange(D
->getAtEndRange(), Record
);
356 // Abstract class (no need to define a stable serialization::DECL code).
359 void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl
*D
) {
360 VisitObjCContainerDecl(D
);
361 Writer
.AddTypeRef(QualType(D
->getTypeForDecl(), 0), Record
);
362 Writer
.AddDeclRef(D
->getSuperClass(), Record
);
364 // Write out the protocols that are directly referenced by the @interface.
365 Record
.push_back(D
->ReferencedProtocols
.size());
366 for (ObjCInterfaceDecl::protocol_iterator P
= D
->protocol_begin(),
367 PEnd
= D
->protocol_end();
369 Writer
.AddDeclRef(*P
, Record
);
370 for (ObjCInterfaceDecl::protocol_loc_iterator PL
= D
->protocol_loc_begin(),
371 PLEnd
= D
->protocol_loc_end();
373 Writer
.AddSourceLocation(*PL
, Record
);
375 // Write out the protocols that are transitively referenced.
376 Record
.push_back(D
->AllReferencedProtocols
.size());
377 for (ObjCList
<ObjCProtocolDecl
>::iterator
378 P
= D
->AllReferencedProtocols
.begin(),
379 PEnd
= D
->AllReferencedProtocols
.end();
381 Writer
.AddDeclRef(*P
, Record
);
383 // Write out the ivars.
384 Record
.push_back(D
->ivar_size());
385 for (ObjCInterfaceDecl::ivar_iterator I
= D
->ivar_begin(),
386 IEnd
= D
->ivar_end(); I
!= IEnd
; ++I
)
387 Writer
.AddDeclRef(*I
, Record
);
388 Writer
.AddDeclRef(D
->getCategoryList(), Record
);
389 Record
.push_back(D
->isForwardDecl());
390 Record
.push_back(D
->isImplicitInterfaceDecl());
391 Writer
.AddSourceLocation(D
->getClassLoc(), Record
);
392 Writer
.AddSourceLocation(D
->getSuperClassLoc(), Record
);
393 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
394 Code
= serialization::DECL_OBJC_INTERFACE
;
397 void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl
*D
) {
399 // FIXME: stable encoding for @public/@private/@protected/@package
400 Record
.push_back(D
->getAccessControl());
401 Record
.push_back(D
->getSynthesize());
402 Code
= serialization::DECL_OBJC_IVAR
;
405 void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl
*D
) {
406 VisitObjCContainerDecl(D
);
407 Record
.push_back(D
->isForwardDecl());
408 Writer
.AddSourceLocation(D
->getLocEnd(), Record
);
409 Record
.push_back(D
->protocol_size());
410 for (ObjCProtocolDecl::protocol_iterator
411 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
412 Writer
.AddDeclRef(*I
, Record
);
413 for (ObjCProtocolDecl::protocol_loc_iterator PL
= D
->protocol_loc_begin(),
414 PLEnd
= D
->protocol_loc_end();
416 Writer
.AddSourceLocation(*PL
, Record
);
417 Code
= serialization::DECL_OBJC_PROTOCOL
;
420 void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl
*D
) {
422 Code
= serialization::DECL_OBJC_AT_DEFS_FIELD
;
425 void ASTDeclWriter::VisitObjCClassDecl(ObjCClassDecl
*D
) {
427 Record
.push_back(D
->size());
428 for (ObjCClassDecl::iterator I
= D
->begin(), IEnd
= D
->end(); I
!= IEnd
; ++I
)
429 Writer
.AddDeclRef(I
->getInterface(), Record
);
430 for (ObjCClassDecl::iterator I
= D
->begin(), IEnd
= D
->end(); I
!= IEnd
; ++I
)
431 Writer
.AddSourceLocation(I
->getLocation(), Record
);
432 Code
= serialization::DECL_OBJC_CLASS
;
435 void ASTDeclWriter::VisitObjCForwardProtocolDecl(ObjCForwardProtocolDecl
*D
) {
437 Record
.push_back(D
->protocol_size());
438 for (ObjCForwardProtocolDecl::protocol_iterator
439 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
440 Writer
.AddDeclRef(*I
, Record
);
441 for (ObjCForwardProtocolDecl::protocol_loc_iterator
442 PL
= D
->protocol_loc_begin(), PLEnd
= D
->protocol_loc_end();
444 Writer
.AddSourceLocation(*PL
, Record
);
445 Code
= serialization::DECL_OBJC_FORWARD_PROTOCOL
;
448 void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl
*D
) {
449 VisitObjCContainerDecl(D
);
450 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
451 Record
.push_back(D
->protocol_size());
452 for (ObjCCategoryDecl::protocol_iterator
453 I
= D
->protocol_begin(), IEnd
= D
->protocol_end(); I
!= IEnd
; ++I
)
454 Writer
.AddDeclRef(*I
, Record
);
455 for (ObjCCategoryDecl::protocol_loc_iterator
456 PL
= D
->protocol_loc_begin(), PLEnd
= D
->protocol_loc_end();
458 Writer
.AddSourceLocation(*PL
, Record
);
459 Writer
.AddDeclRef(D
->getNextClassCategory(), Record
);
460 Record
.push_back(D
->hasSynthBitfield());
461 Writer
.AddSourceLocation(D
->getAtLoc(), Record
);
462 Writer
.AddSourceLocation(D
->getCategoryNameLoc(), Record
);
463 Code
= serialization::DECL_OBJC_CATEGORY
;
466 void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl
*D
) {
468 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
469 Code
= serialization::DECL_OBJC_COMPATIBLE_ALIAS
;
472 void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl
*D
) {
474 Writer
.AddSourceLocation(D
->getAtLoc(), Record
);
475 Writer
.AddTypeSourceInfo(D
->getTypeSourceInfo(), Record
);
476 // FIXME: stable encoding
477 Record
.push_back((unsigned)D
->getPropertyAttributes());
478 Record
.push_back((unsigned)D
->getPropertyAttributesAsWritten());
479 // FIXME: stable encoding
480 Record
.push_back((unsigned)D
->getPropertyImplementation());
481 Writer
.AddDeclarationName(D
->getGetterName(), Record
);
482 Writer
.AddDeclarationName(D
->getSetterName(), Record
);
483 Writer
.AddDeclRef(D
->getGetterMethodDecl(), Record
);
484 Writer
.AddDeclRef(D
->getSetterMethodDecl(), Record
);
485 Writer
.AddDeclRef(D
->getPropertyIvarDecl(), Record
);
486 Code
= serialization::DECL_OBJC_PROPERTY
;
489 void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl
*D
) {
490 VisitObjCContainerDecl(D
);
491 Writer
.AddDeclRef(D
->getClassInterface(), Record
);
492 // Abstract class (no need to define a stable serialization::DECL code).
495 void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl
*D
) {
496 VisitObjCImplDecl(D
);
497 Writer
.AddIdentifierRef(D
->getIdentifier(), Record
);
498 Code
= serialization::DECL_OBJC_CATEGORY_IMPL
;
501 void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl
*D
) {
502 VisitObjCImplDecl(D
);
503 Writer
.AddDeclRef(D
->getSuperClass(), Record
);
504 Writer
.AddCXXBaseOrMemberInitializers(D
->IvarInitializers
,
505 D
->NumIvarInitializers
, Record
);
506 Record
.push_back(D
->hasSynthBitfield());
507 Code
= serialization::DECL_OBJC_IMPLEMENTATION
;
510 void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl
*D
) {
512 Writer
.AddSourceLocation(D
->getLocStart(), Record
);
513 Writer
.AddDeclRef(D
->getPropertyDecl(), Record
);
514 Writer
.AddDeclRef(D
->getPropertyIvarDecl(), Record
);
515 Writer
.AddStmt(D
->getGetterCXXConstructor());
516 Writer
.AddStmt(D
->getSetterCXXAssignment());
517 Code
= serialization::DECL_OBJC_PROPERTY_IMPL
;
520 void ASTDeclWriter::VisitFieldDecl(FieldDecl
*D
) {
521 VisitDeclaratorDecl(D
);
522 Record
.push_back(D
->isMutable());
523 Record
.push_back(D
->getBitWidth()? 1 : 0);
524 if (D
->getBitWidth())
525 Writer
.AddStmt(D
->getBitWidth());
526 if (!D
->getDeclName())
527 Writer
.AddDeclRef(Context
.getInstantiatedFromUnnamedFieldDecl(D
), Record
);
528 Code
= serialization::DECL_FIELD
;
531 void ASTDeclWriter::VisitVarDecl(VarDecl
*D
) {
532 VisitDeclaratorDecl(D
);
533 VisitRedeclarable(D
);
534 Record
.push_back(D
->getStorageClass()); // FIXME: stable encoding
535 Record
.push_back(D
->getStorageClassAsWritten());
536 Record
.push_back(D
->isThreadSpecified());
537 Record
.push_back(D
->hasCXXDirectInitializer());
538 Record
.push_back(D
->isExceptionVariable());
539 Record
.push_back(D
->isNRVOVariable());
540 Record
.push_back(D
->getInit() ? 1 : 0);
542 Writer
.AddStmt(D
->getInit());
544 MemberSpecializationInfo
*SpecInfo
545 = D
->isStaticDataMember() ? D
->getMemberSpecializationInfo() : 0;
546 Record
.push_back(SpecInfo
!= 0);
548 Writer
.AddDeclRef(SpecInfo
->getInstantiatedFrom(), Record
);
549 Record
.push_back(SpecInfo
->getTemplateSpecializationKind());
550 Writer
.AddSourceLocation(SpecInfo
->getPointOfInstantiation(), Record
);
553 Code
= serialization::DECL_VAR
;
556 void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl
*D
) {
558 Code
= serialization::DECL_IMPLICIT_PARAM
;
561 void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl
*D
) {
563 Record
.push_back(D
->getObjCDeclQualifier()); // FIXME: stable encoding
564 Record
.push_back(D
->hasInheritedDefaultArg());
565 Record
.push_back(D
->hasUninstantiatedDefaultArg());
566 if (D
->hasUninstantiatedDefaultArg())
567 Writer
.AddStmt(D
->getUninstantiatedDefaultArg());
568 Code
= serialization::DECL_PARM_VAR
;
570 // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here
571 // we dynamically check for the properties that we optimize for, but don't
572 // know are true of all PARM_VAR_DECLs.
573 if (!D
->getTypeSourceInfo() &&
577 D
->getAccess() == AS_none
&&
578 D
->getPCHLevel() == 0 &&
579 D
->getStorageClass() == 0 &&
580 !D
->hasCXXDirectInitializer() && // Can params have this ever?
581 D
->getObjCDeclQualifier() == 0 &&
582 !D
->hasInheritedDefaultArg() &&
584 !D
->hasUninstantiatedDefaultArg()) // No default expr.
585 AbbrevToUse
= Writer
.getParmVarDeclAbbrev();
587 // Check things we know are true of *every* PARM_VAR_DECL, which is more than
588 // just us assuming it.
589 assert(!D
->isInvalidDecl() && "Shouldn't emit invalid decls");
590 assert(!D
->isThreadSpecified() && "PARM_VAR_DECL can't be __thread");
591 assert(D
->getAccess() == AS_none
&& "PARM_VAR_DECL can't be public/private");
592 assert(!D
->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
593 assert(D
->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl");
594 assert(!D
->isStaticDataMember() &&
595 "PARM_VAR_DECL can't be static data member");
598 void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl
*D
) {
600 Writer
.AddStmt(D
->getAsmString());
601 Code
= serialization::DECL_FILE_SCOPE_ASM
;
604 void ASTDeclWriter::VisitBlockDecl(BlockDecl
*D
) {
606 Writer
.AddStmt(D
->getBody());
607 Writer
.AddTypeSourceInfo(D
->getSignatureAsWritten(), Record
);
608 Record
.push_back(D
->param_size());
609 for (FunctionDecl::param_iterator P
= D
->param_begin(), PEnd
= D
->param_end();
611 Writer
.AddDeclRef(*P
, Record
);
612 Code
= serialization::DECL_BLOCK
;
615 void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl
*D
) {
617 // FIXME: It might be nice to serialize the brace locations for this
618 // declaration, which don't seem to be readily available in the AST.
619 Record
.push_back(D
->getLanguage());
620 Record
.push_back(D
->hasBraces());
621 Code
= serialization::DECL_LINKAGE_SPEC
;
624 void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl
*D
) {
626 Record
.push_back(D
->isInline());
627 Writer
.AddSourceLocation(D
->getLBracLoc(), Record
);
628 Writer
.AddSourceLocation(D
->getRBracLoc(), Record
);
629 Writer
.AddDeclRef(D
->getNextNamespace(), Record
);
631 // Only write one reference--original or anonymous
632 Record
.push_back(D
->isOriginalNamespace());
633 if (D
->isOriginalNamespace())
634 Writer
.AddDeclRef(D
->getAnonymousNamespace(), Record
);
636 Writer
.AddDeclRef(D
->getOriginalNamespace(), Record
);
637 Code
= serialization::DECL_NAMESPACE
;
639 if (Writer
.hasChain() && !D
->isOriginalNamespace() &&
640 D
->getOriginalNamespace()->getPCHLevel() > 0) {
641 NamespaceDecl
*NS
= D
->getOriginalNamespace();
642 Writer
.AddUpdatedNamespace(NS
);
644 // Make sure all visible decls are written. They will be recorded later.
645 NS
->lookup(DeclarationName());
646 StoredDeclsMap
*Map
= static_cast<StoredDeclsMap
*>(NS
->getLookupPtr());
648 for (StoredDeclsMap::iterator D
= Map
->begin(), DEnd
= Map
->end();
650 DeclarationName Name
= D
->first
;
651 DeclContext::lookup_result Result
= D
->second
.getLookupResult();
652 while (Result
.first
!= Result
.second
) {
653 Writer
.GetDeclRef(*Result
.first
);
661 void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl
*D
) {
663 Writer
.AddSourceLocation(D
->getNamespaceLoc(), Record
);
664 Writer
.AddSourceRange(D
->getQualifierRange(), Record
);
665 Writer
.AddNestedNameSpecifier(D
->getQualifier(), Record
);
666 Writer
.AddSourceLocation(D
->getTargetNameLoc(), Record
);
667 Writer
.AddDeclRef(D
->getNamespace(), Record
);
668 Code
= serialization::DECL_NAMESPACE_ALIAS
;
671 void ASTDeclWriter::VisitUsingDecl(UsingDecl
*D
) {
673 Writer
.AddSourceRange(D
->getNestedNameRange(), Record
);
674 Writer
.AddSourceLocation(D
->getUsingLocation(), Record
);
675 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameDecl(), Record
);
676 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
677 Record
.push_back(D
->getNumShadowDecls());
678 for (UsingDecl::shadow_iterator P
= D
->shadow_begin(),
679 PEnd
= D
->shadow_end(); P
!= PEnd
; ++P
)
680 Writer
.AddDeclRef(*P
, Record
);
681 Record
.push_back(D
->isTypeName());
682 Writer
.AddDeclRef(Context
.getInstantiatedFromUsingDecl(D
), Record
);
683 Code
= serialization::DECL_USING
;
686 void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl
*D
) {
688 Writer
.AddDeclRef(D
->getTargetDecl(), Record
);
689 Writer
.AddDeclRef(D
->getUsingDecl(), Record
);
690 Writer
.AddDeclRef(Context
.getInstantiatedFromUsingShadowDecl(D
), Record
);
691 Code
= serialization::DECL_USING_SHADOW
;
694 void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl
*D
) {
696 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
697 Writer
.AddSourceLocation(D
->getNamespaceKeyLocation(), Record
);
698 Writer
.AddSourceRange(D
->getQualifierRange(), Record
);
699 Writer
.AddNestedNameSpecifier(D
->getQualifier(), Record
);
700 Writer
.AddDeclRef(D
->getNominatedNamespace(), Record
);
701 Writer
.AddDeclRef(dyn_cast
<Decl
>(D
->getCommonAncestor()), Record
);
702 Code
= serialization::DECL_USING_DIRECTIVE
;
705 void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl
*D
) {
707 Writer
.AddSourceRange(D
->getTargetNestedNameRange(), Record
);
708 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
709 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameSpecifier(), Record
);
710 Writer
.AddDeclarationNameLoc(D
->DNLoc
, D
->getDeclName(), Record
);
711 Code
= serialization::DECL_UNRESOLVED_USING_VALUE
;
714 void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
715 UnresolvedUsingTypenameDecl
*D
) {
717 Writer
.AddSourceRange(D
->getTargetNestedNameRange(), Record
);
718 Writer
.AddSourceLocation(D
->getUsingLoc(), Record
);
719 Writer
.AddSourceLocation(D
->getTypenameLoc(), Record
);
720 Writer
.AddNestedNameSpecifier(D
->getTargetNestedNameSpecifier(), Record
);
721 Code
= serialization::DECL_UNRESOLVED_USING_TYPENAME
;
724 void ASTDeclWriter::WriteCXXDefinitionData(
725 struct CXXRecordDecl::DefinitionData
&Data
) {
726 Record
.push_back(Data
.UserDeclaredConstructor
);
727 Record
.push_back(Data
.UserDeclaredCopyConstructor
);
728 Record
.push_back(Data
.UserDeclaredCopyAssignment
);
729 Record
.push_back(Data
.UserDeclaredDestructor
);
730 Record
.push_back(Data
.Aggregate
);
731 Record
.push_back(Data
.PlainOldData
);
732 Record
.push_back(Data
.Empty
);
733 Record
.push_back(Data
.Polymorphic
);
734 Record
.push_back(Data
.Abstract
);
735 Record
.push_back(Data
.HasTrivialConstructor
);
736 Record
.push_back(Data
.HasTrivialCopyConstructor
);
737 Record
.push_back(Data
.HasTrivialCopyAssignment
);
738 Record
.push_back(Data
.HasTrivialDestructor
);
739 Record
.push_back(Data
.ComputedVisibleConversions
);
740 Record
.push_back(Data
.DeclaredDefaultConstructor
);
741 Record
.push_back(Data
.DeclaredCopyConstructor
);
742 Record
.push_back(Data
.DeclaredCopyAssignment
);
743 Record
.push_back(Data
.DeclaredDestructor
);
745 Record
.push_back(Data
.NumBases
);
746 for (unsigned i
= 0; i
!= Data
.NumBases
; ++i
)
747 Writer
.AddCXXBaseSpecifier(Data
.Bases
[i
], Record
);
749 // FIXME: Make VBases lazily computed when needed to avoid storing them.
750 Record
.push_back(Data
.NumVBases
);
751 for (unsigned i
= 0; i
!= Data
.NumVBases
; ++i
)
752 Writer
.AddCXXBaseSpecifier(Data
.VBases
[i
], Record
);
754 Writer
.AddUnresolvedSet(Data
.Conversions
, Record
);
755 Writer
.AddUnresolvedSet(Data
.VisibleConversions
, Record
);
756 // Data.Definition is the owning decl, no need to write it.
757 Writer
.AddDeclRef(Data
.FirstFriend
, Record
);
760 void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl
*D
) {
761 // See comments at ASTDeclReader::VisitCXXRecordDecl about why this happens
762 // before VisitRecordDecl.
763 enum { Data_NoDefData
, Data_Owner
, Data_NotOwner
};
764 bool OwnsDefinitionData
= false;
765 if (D
->DefinitionData
) {
766 assert(D
->DefinitionData
->Definition
&&
767 "DefinitionData don't point to a definition decl!");
768 OwnsDefinitionData
= D
->DefinitionData
->Definition
== D
;
769 if (OwnsDefinitionData
) {
770 Record
.push_back(Data_Owner
);
772 Record
.push_back(Data_NotOwner
);
773 Writer
.AddDeclRef(D
->DefinitionData
->Definition
, Record
);
776 Record
.push_back(Data_NoDefData
);
780 if (OwnsDefinitionData
) {
781 assert(D
->DefinitionData
);
782 WriteCXXDefinitionData(*D
->DefinitionData
);
786 CXXRecNotTemplate
= 0, CXXRecTemplate
, CXXRecMemberSpecialization
788 if (ClassTemplateDecl
*TemplD
= D
->getDescribedClassTemplate()) {
789 Record
.push_back(CXXRecTemplate
);
790 Writer
.AddDeclRef(TemplD
, Record
);
791 } else if (MemberSpecializationInfo
*MSInfo
792 = D
->getMemberSpecializationInfo()) {
793 Record
.push_back(CXXRecMemberSpecialization
);
794 Writer
.AddDeclRef(MSInfo
->getInstantiatedFrom(), Record
);
795 Record
.push_back(MSInfo
->getTemplateSpecializationKind());
796 Writer
.AddSourceLocation(MSInfo
->getPointOfInstantiation(), Record
);
798 Record
.push_back(CXXRecNotTemplate
);
801 // Store the key function to avoid deserializing every method so we can
804 Writer
.AddDeclRef(Context
.getKeyFunction(D
), Record
);
806 Code
= serialization::DECL_CXX_RECORD
;
809 void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl
*D
) {
810 VisitFunctionDecl(D
);
811 Record
.push_back(D
->size_overridden_methods());
812 for (CXXMethodDecl::method_iterator
813 I
= D
->begin_overridden_methods(), E
= D
->end_overridden_methods();
815 Writer
.AddDeclRef(*I
, Record
);
816 Code
= serialization::DECL_CXX_METHOD
;
819 void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl
*D
) {
820 VisitCXXMethodDecl(D
);
822 Record
.push_back(D
->IsExplicitSpecified
);
823 Record
.push_back(D
->ImplicitlyDefined
);
824 Writer
.AddCXXBaseOrMemberInitializers(D
->BaseOrMemberInitializers
,
825 D
->NumBaseOrMemberInitializers
, Record
);
827 Code
= serialization::DECL_CXX_CONSTRUCTOR
;
830 void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl
*D
) {
831 VisitCXXMethodDecl(D
);
833 Record
.push_back(D
->ImplicitlyDefined
);
834 Writer
.AddDeclRef(D
->OperatorDelete
, Record
);
836 Code
= serialization::DECL_CXX_DESTRUCTOR
;
839 void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl
*D
) {
840 VisitCXXMethodDecl(D
);
841 Record
.push_back(D
->IsExplicitSpecified
);
842 Code
= serialization::DECL_CXX_CONVERSION
;
845 void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl
*D
) {
847 Writer
.AddSourceLocation(D
->getColonLoc(), Record
);
848 Code
= serialization::DECL_ACCESS_SPEC
;
851 void ASTDeclWriter::VisitFriendDecl(FriendDecl
*D
) {
853 Record
.push_back(D
->Friend
.is
<TypeSourceInfo
*>());
854 if (D
->Friend
.is
<TypeSourceInfo
*>())
855 Writer
.AddTypeSourceInfo(D
->Friend
.get
<TypeSourceInfo
*>(), Record
);
857 Writer
.AddDeclRef(D
->Friend
.get
<NamedDecl
*>(), Record
);
858 Writer
.AddDeclRef(D
->NextFriend
, Record
);
859 Record
.push_back(D
->UnsupportedFriend
);
860 Writer
.AddSourceLocation(D
->FriendLoc
, Record
);
861 Code
= serialization::DECL_FRIEND
;
864 void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl
*D
) {
866 Record
.push_back(D
->getNumTemplateParameters());
867 for (unsigned i
= 0, e
= D
->getNumTemplateParameters(); i
!= e
; ++i
)
868 Writer
.AddTemplateParameterList(D
->getTemplateParameterList(i
), Record
);
869 Record
.push_back(D
->getFriendDecl() != 0);
870 if (D
->getFriendDecl())
871 Writer
.AddDeclRef(D
->getFriendDecl(), Record
);
873 Writer
.AddTypeSourceInfo(D
->getFriendType(), Record
);
874 Writer
.AddSourceLocation(D
->getFriendLoc(), Record
);
875 Code
= serialization::DECL_FRIEND_TEMPLATE
;
878 void ASTDeclWriter::VisitTemplateDecl(TemplateDecl
*D
) {
881 Writer
.AddDeclRef(D
->getTemplatedDecl(), Record
);
882 Writer
.AddTemplateParameterList(D
->getTemplateParameters(), Record
);
885 void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl
*D
) {
886 // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
887 // getCommonPtr() can be used while this is still initializing.
889 Writer
.AddDeclRef(D
->getPreviousDeclaration(), Record
);
890 if (D
->getPreviousDeclaration() == 0) {
891 // This TemplateDecl owns the CommonPtr; write it.
892 assert(D
->isCanonicalDecl());
894 Writer
.AddDeclRef(D
->getInstantiatedFromMemberTemplate(), Record
);
895 if (D
->getInstantiatedFromMemberTemplate())
896 Record
.push_back(D
->isMemberSpecialization());
898 Writer
.AddDeclRef(D
->getCommonPtr()->Latest
, Record
);
900 RedeclarableTemplateDecl
*First
= D
->getFirstDeclaration();
902 // If this is a most recent redeclaration that is pointed to by a first decl
903 // in a chained PCH, keep track of the association with the map so we can
904 // update the first decl during AST reading.
905 if (First
->getMostRecentDeclaration() == D
&&
906 First
->getPCHLevel() > D
->getPCHLevel()) {
907 assert(Writer
.FirstLatestDecls
.find(First
)==Writer
.FirstLatestDecls
.end()
908 && "The latest is already set");
909 Writer
.FirstLatestDecls
[First
] = D
;
913 VisitTemplateDecl(D
);
914 Record
.push_back(D
->getIdentifierNamespace());
917 void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl
*D
) {
918 VisitRedeclarableTemplateDecl(D
);
920 if (D
->getPreviousDeclaration() == 0) {
921 typedef llvm::FoldingSet
<ClassTemplateSpecializationDecl
> CTSDSetTy
;
922 CTSDSetTy
&CTSDSet
= D
->getSpecializations();
923 Record
.push_back(CTSDSet
.size());
924 for (CTSDSetTy::iterator I
=CTSDSet
.begin(), E
= CTSDSet
.end(); I
!=E
; ++I
) {
925 assert(I
->isCanonicalDecl() && "Expected only canonical decls in set");
926 Writer
.AddDeclRef(&*I
, Record
);
929 typedef llvm::FoldingSet
<ClassTemplatePartialSpecializationDecl
> CTPSDSetTy
;
930 CTPSDSetTy
&CTPSDSet
= D
->getPartialSpecializations();
931 Record
.push_back(CTPSDSet
.size());
932 for (CTPSDSetTy::iterator I
=CTPSDSet
.begin(), E
=CTPSDSet
.end(); I
!=E
; ++I
) {
933 assert(I
->isCanonicalDecl() && "Expected only canonical decls in set");
934 Writer
.AddDeclRef(&*I
, Record
);
937 // InjectedClassNameType is computed, no need to write it.
939 Code
= serialization::DECL_CLASS_TEMPLATE
;
942 void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
943 ClassTemplateSpecializationDecl
*D
) {
944 VisitCXXRecordDecl(D
);
946 llvm::PointerUnion
<ClassTemplateDecl
*,
947 ClassTemplatePartialSpecializationDecl
*> InstFrom
948 = D
->getSpecializedTemplateOrPartial();
950 if (InstFrom
.is
<ClassTemplateDecl
*>()) {
951 InstFromD
= InstFrom
.get
<ClassTemplateDecl
*>();
952 Writer
.AddDeclRef(InstFromD
, Record
);
954 InstFromD
= InstFrom
.get
<ClassTemplatePartialSpecializationDecl
*>();
955 Writer
.AddDeclRef(InstFromD
, Record
);
956 Writer
.AddTemplateArgumentList(&D
->getTemplateInstantiationArgs(), Record
);
957 InstFromD
= cast
<ClassTemplatePartialSpecializationDecl
>(InstFromD
)->
958 getSpecializedTemplate();
960 // Is this a specialization of an already-serialized template?
961 if (InstFromD
->getCanonicalDecl()->getPCHLevel() != 0)
962 Writer
.AddAdditionalTemplateSpecialization(Writer
.getDeclID(InstFromD
),
963 Writer
.getDeclID(D
));
966 Writer
.AddTypeSourceInfo(D
->getTypeAsWritten(), Record
);
967 if (D
->getTypeAsWritten()) {
968 Writer
.AddSourceLocation(D
->getExternLoc(), Record
);
969 Writer
.AddSourceLocation(D
->getTemplateKeywordLoc(), Record
);
972 Writer
.AddTemplateArgumentList(&D
->getTemplateArgs(), Record
);
973 Writer
.AddSourceLocation(D
->getPointOfInstantiation(), Record
);
974 Record
.push_back(D
->getSpecializationKind());
976 if (D
->isCanonicalDecl()) {
977 // When reading, we'll add it to the folding set of the following template.
978 Writer
.AddDeclRef(D
->getSpecializedTemplate()->getCanonicalDecl(), Record
);
981 Code
= serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION
;
984 void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
985 ClassTemplatePartialSpecializationDecl
*D
) {
986 VisitClassTemplateSpecializationDecl(D
);
988 Writer
.AddTemplateParameterList(D
->getTemplateParameters(), Record
);
990 Record
.push_back(D
->getNumTemplateArgsAsWritten());
991 for (int i
= 0, e
= D
->getNumTemplateArgsAsWritten(); i
!= e
; ++i
)
992 Writer
.AddTemplateArgumentLoc(D
->getTemplateArgsAsWritten()[i
], Record
);
994 Record
.push_back(D
->getSequenceNumber());
996 // These are read/set from/to the first declaration.
997 if (D
->getPreviousDeclaration() == 0) {
998 Writer
.AddDeclRef(D
->getInstantiatedFromMember(), Record
);
999 Record
.push_back(D
->isMemberSpecialization());
1002 Code
= serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION
;
1005 void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl
*D
) {
1006 VisitRedeclarableTemplateDecl(D
);
1008 if (D
->getPreviousDeclaration() == 0) {
1009 // This FunctionTemplateDecl owns the CommonPtr; write it.
1011 // Write the function specialization declarations.
1012 Record
.push_back(D
->getSpecializations().size());
1013 for (llvm::FoldingSet
<FunctionTemplateSpecializationInfo
>::iterator
1014 I
= D
->getSpecializations().begin(),
1015 E
= D
->getSpecializations().end() ; I
!= E
; ++I
) {
1016 assert(I
->Function
->isCanonicalDecl() &&
1017 "Expected only canonical decls in set");
1018 Writer
.AddDeclRef(I
->Function
, Record
);
1021 Code
= serialization::DECL_FUNCTION_TEMPLATE
;
1024 void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl
*D
) {
1027 Record
.push_back(D
->wasDeclaredWithTypename());
1028 Record
.push_back(D
->isParameterPack());
1029 Record
.push_back(D
->defaultArgumentWasInherited());
1030 Writer
.AddTypeSourceInfo(D
->getDefaultArgumentInfo(), Record
);
1032 Code
= serialization::DECL_TEMPLATE_TYPE_PARM
;
1035 void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl
*D
) {
1037 // TemplateParmPosition.
1038 Record
.push_back(D
->getDepth());
1039 Record
.push_back(D
->getPosition());
1040 // Rest of NonTypeTemplateParmDecl.
1041 Record
.push_back(D
->getDefaultArgument() != 0);
1042 if (D
->getDefaultArgument()) {
1043 Writer
.AddStmt(D
->getDefaultArgument());
1044 Record
.push_back(D
->defaultArgumentWasInherited());
1046 Code
= serialization::DECL_NON_TYPE_TEMPLATE_PARM
;
1049 void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl
*D
) {
1050 VisitTemplateDecl(D
);
1051 // TemplateParmPosition.
1052 Record
.push_back(D
->getDepth());
1053 Record
.push_back(D
->getPosition());
1054 // Rest of TemplateTemplateParmDecl.
1055 Writer
.AddTemplateArgumentLoc(D
->getDefaultArgument(), Record
);
1056 Record
.push_back(D
->defaultArgumentWasInherited());
1057 Code
= serialization::DECL_TEMPLATE_TEMPLATE_PARM
;
1060 void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl
*D
) {
1062 Writer
.AddStmt(D
->getAssertExpr());
1063 Writer
.AddStmt(D
->getMessage());
1064 Code
= serialization::DECL_STATIC_ASSERT
;
1067 /// \brief Emit the DeclContext part of a declaration context decl.
1069 /// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
1070 /// block for this declaration context is stored. May be 0 to indicate
1071 /// that there are no declarations stored within this context.
1073 /// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
1074 /// block for this declaration context is stored. May be 0 to indicate
1075 /// that there are no declarations visible from this context. Note
1076 /// that this value will not be emitted for non-primary declaration
1078 void ASTDeclWriter::VisitDeclContext(DeclContext
*DC
, uint64_t LexicalOffset
,
1079 uint64_t VisibleOffset
) {
1080 Record
.push_back(LexicalOffset
);
1081 Record
.push_back(VisibleOffset
);
1084 template <typename T
>
1085 void ASTDeclWriter::VisitRedeclarable(Redeclarable
<T
> *D
) {
1086 enum { NoRedeclaration
= 0, PointsToPrevious
, PointsToLatest
};
1087 if (D
->RedeclLink
.getNext() == D
) {
1088 Record
.push_back(NoRedeclaration
);
1090 Record
.push_back(D
->RedeclLink
.NextIsPrevious() ? PointsToPrevious
1092 Writer
.AddDeclRef(D
->RedeclLink
.getPointer(), Record
);
1095 T
*First
= D
->getFirstDeclaration();
1096 T
*ThisDecl
= static_cast<T
*>(D
);
1097 // If this is a most recent redeclaration that is pointed to by a first decl
1098 // in a chained PCH, keep track of the association with the map so we can
1099 // update the first decl during AST reading.
1100 if (ThisDecl
!= First
&& First
->getMostRecentDeclaration() == ThisDecl
&&
1101 First
->getPCHLevel() > ThisDecl
->getPCHLevel()) {
1102 assert(Writer
.FirstLatestDecls
.find(First
) == Writer
.FirstLatestDecls
.end()
1103 && "The latest is already set");
1104 Writer
.FirstLatestDecls
[First
] = ThisDecl
;
1108 //===----------------------------------------------------------------------===//
1109 // ASTWriter Implementation
1110 //===----------------------------------------------------------------------===//
1112 void ASTWriter::WriteDeclsBlockAbbrevs() {
1113 using namespace llvm
;
1114 // Abbreviation for DECL_PARM_VAR.
1115 BitCodeAbbrev
*Abv
= new BitCodeAbbrev();
1116 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR
));
1119 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // DeclContext
1120 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // LexicalDeclContext
1121 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Location
1122 Abv
->Add(BitCodeAbbrevOp(0)); // isInvalidDecl (!?)
1123 Abv
->Add(BitCodeAbbrevOp(0)); // HasAttrs
1124 Abv
->Add(BitCodeAbbrevOp(0)); // isImplicit
1125 Abv
->Add(BitCodeAbbrevOp(0)); // isUsed
1126 Abv
->Add(BitCodeAbbrevOp(AS_none
)); // C++ AccessSpecifier
1127 Abv
->Add(BitCodeAbbrevOp(0)); // PCH level
1130 Abv
->Add(BitCodeAbbrevOp(0)); // NameKind = Identifier
1131 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Name
1133 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR
, 6)); // Type
1135 Abv
->Add(BitCodeAbbrevOp(0)); // hasExtInfo
1136 Abv
->Add(BitCodeAbbrevOp(serialization::PREDEF_TYPE_NULL_ID
)); // InfoType
1138 Abv
->Add(BitCodeAbbrevOp(0)); // No redeclaration
1139 Abv
->Add(BitCodeAbbrevOp(0)); // StorageClass
1140 Abv
->Add(BitCodeAbbrevOp(0)); // StorageClassAsWritten
1141 Abv
->Add(BitCodeAbbrevOp(0)); // isThreadSpecified
1142 Abv
->Add(BitCodeAbbrevOp(0)); // hasCXXDirectInitializer
1143 Abv
->Add(BitCodeAbbrevOp(0)); // isExceptionVariable
1144 Abv
->Add(BitCodeAbbrevOp(0)); // isNRVOVariable
1145 Abv
->Add(BitCodeAbbrevOp(0)); // HasInit
1146 Abv
->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo
1148 Abv
->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier
1149 Abv
->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg
1150 Abv
->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg
1152 ParmVarDeclAbbrev
= Stream
.EmitAbbrev(Abv
);
1154 Abv
= new BitCodeAbbrev();
1155 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL
));
1156 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1157 DeclContextLexicalAbbrev
= Stream
.EmitAbbrev(Abv
);
1159 Abv
= new BitCodeAbbrev();
1160 Abv
->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE
));
1161 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed
, 32));
1162 Abv
->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob
));
1163 DeclContextVisibleLookupAbbrev
= Stream
.EmitAbbrev(Abv
);
1166 /// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
1167 /// consumers of the AST.
1169 /// Such decls will always be deserialized from the AST file, so we would like
1170 /// this to be as restrictive as possible. Currently the predicate is driven by
1171 /// code generation requirements, if other clients have a different notion of
1172 /// what is "required" then we may have to consider an alternate scheme where
1173 /// clients can iterate over the top-level decls and get information on them,
1174 /// without necessary deserializing them. We could explicitly require such
1175 /// clients to use a separate API call to "realize" the decl. This should be
1176 /// relatively painless since they would presumably only do it for top-level
1178 static bool isRequiredDecl(const Decl
*D
, ASTContext
&Context
) {
1179 // File scoped assembly or obj-c implementation must be seen.
1180 if (isa
<FileScopeAsmDecl
>(D
) || isa
<ObjCImplementationDecl
>(D
))
1183 return Context
.DeclMustBeEmitted(D
);
1186 void ASTWriter::WriteDecl(ASTContext
&Context
, Decl
*D
) {
1188 ASTDeclWriter
W(*this, Context
, Record
);
1190 // If this declaration is also a DeclContext, write blocks for the
1191 // declarations that lexically stored inside its context and those
1192 // declarations that are visible from its context. These blocks
1193 // are written before the declaration itself so that we can put
1194 // their offsets into the record for the declaration.
1195 uint64_t LexicalOffset
= 0;
1196 uint64_t VisibleOffset
= 0;
1197 DeclContext
*DC
= dyn_cast
<DeclContext
>(D
);
1199 LexicalOffset
= WriteDeclContextLexicalBlock(Context
, DC
);
1200 VisibleOffset
= WriteDeclContextVisibleBlock(Context
, DC
);
1203 // Determine the ID for this declaration
1204 serialization::DeclID
&IDR
= DeclIDs
[D
];
1207 serialization::DeclID ID
= IDR
;
1209 if (ID
< FirstDeclID
) {
1210 // We're replacing a decl in a previous file.
1211 ReplacedDecls
.push_back(std::make_pair(ID
, Stream
.GetCurrentBitNo()));
1213 unsigned Index
= ID
- FirstDeclID
;
1215 // Record the offset for this declaration
1216 if (DeclOffsets
.size() == Index
)
1217 DeclOffsets
.push_back(Stream
.GetCurrentBitNo());
1218 else if (DeclOffsets
.size() < Index
) {
1219 DeclOffsets
.resize(Index
+1);
1220 DeclOffsets
[Index
] = Stream
.GetCurrentBitNo();
1224 // Build and emit a record for this declaration
1226 W
.Code
= (serialization::DeclCode
)0;
1229 if (DC
) W
.VisitDeclContext(DC
, LexicalOffset
, VisibleOffset
);
1232 llvm::report_fatal_error(llvm::StringRef("unexpected declaration kind '") +
1233 D
->getDeclKindName() + "'");
1234 Stream
.EmitRecord(W
.Code
, Record
, W
.AbbrevToUse
);
1236 // Flush any expressions that were written as part of this declaration.
1239 // Note "external" declarations so that we can add them to a record in the
1242 // FIXME: This should be renamed, the predicate is much more complicated.
1243 if (isRequiredDecl(D
, Context
))
1244 ExternalDefinitions
.push_back(ID
);