1//===--- ASTWriterDecl.cpp - Declaration Serialization --------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements serialization for Declarations.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTWriter.h"
15#include "ASTCommon.h"
16#include "clang/AST/DeclCXX.h"
17#include "clang/AST/DeclContextInternals.h"
18#include "clang/AST/DeclTemplate.h"
19#include "clang/AST/DeclVisitor.h"
20#include "clang/AST/Expr.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Serialization/ASTReader.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/Bitcode/BitstreamWriter.h"
25#include "llvm/Support/ErrorHandling.h"
26using namespace clang;
27using namespace serialization;
28
29//===----------------------------------------------------------------------===//
30// Declaration serialization
31//===----------------------------------------------------------------------===//
32
33namespace clang {
34  class ASTDeclWriter : public DeclVisitor<ASTDeclWriter, void> {
35
36    ASTWriter &Writer;
37    ASTContext &Context;
38    typedef ASTWriter::RecordData RecordData;
39    RecordData &Record;
40
41  public:
42    serialization::DeclCode Code;
43    unsigned AbbrevToUse;
44
45    ASTDeclWriter(ASTWriter &Writer, ASTContext &Context, RecordData &Record)
46      : Writer(Writer), Context(Context), Record(Record) {
47    }
48
49    void Visit(Decl *D);
50
51    void VisitDecl(Decl *D);
52    void VisitTranslationUnitDecl(TranslationUnitDecl *D);
53    void VisitNamedDecl(NamedDecl *D);
54    void VisitLabelDecl(LabelDecl *LD);
55    void VisitNamespaceDecl(NamespaceDecl *D);
56    void VisitUsingDirectiveDecl(UsingDirectiveDecl *D);
57    void VisitNamespaceAliasDecl(NamespaceAliasDecl *D);
58    void VisitTypeDecl(TypeDecl *D);
59    void VisitTypedefNameDecl(TypedefNameDecl *D);
60    void VisitTypedefDecl(TypedefDecl *D);
61    void VisitTypeAliasDecl(TypeAliasDecl *D);
62    void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D);
63    void VisitTagDecl(TagDecl *D);
64    void VisitEnumDecl(EnumDecl *D);
65    void VisitRecordDecl(RecordDecl *D);
66    void VisitCXXRecordDecl(CXXRecordDecl *D);
67    void VisitClassTemplateSpecializationDecl(
68                                            ClassTemplateSpecializationDecl *D);
69    void VisitClassTemplatePartialSpecializationDecl(
70                                     ClassTemplatePartialSpecializationDecl *D);
71    void VisitVarTemplateSpecializationDecl(VarTemplateSpecializationDecl *D);
72    void VisitVarTemplatePartialSpecializationDecl(
73        VarTemplatePartialSpecializationDecl *D);
74    void VisitClassScopeFunctionSpecializationDecl(
75                                       ClassScopeFunctionSpecializationDecl *D);
76    void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D);
77    void VisitValueDecl(ValueDecl *D);
78    void VisitEnumConstantDecl(EnumConstantDecl *D);
79    void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D);
80    void VisitDeclaratorDecl(DeclaratorDecl *D);
81    void VisitFunctionDecl(FunctionDecl *D);
82    void VisitCXXMethodDecl(CXXMethodDecl *D);
83    void VisitCXXConstructorDecl(CXXConstructorDecl *D);
84    void VisitCXXDestructorDecl(CXXDestructorDecl *D);
85    void VisitCXXConversionDecl(CXXConversionDecl *D);
86    void VisitFieldDecl(FieldDecl *D);
87    void VisitMSPropertyDecl(MSPropertyDecl *D);
88    void VisitIndirectFieldDecl(IndirectFieldDecl *D);
89    void VisitVarDecl(VarDecl *D);
90    void VisitImplicitParamDecl(ImplicitParamDecl *D);
91    void VisitParmVarDecl(ParmVarDecl *D);
92    void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D);
93    void VisitTemplateDecl(TemplateDecl *D);
94    void VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D);
95    void VisitClassTemplateDecl(ClassTemplateDecl *D);
96    void VisitVarTemplateDecl(VarTemplateDecl *D);
97    void VisitFunctionTemplateDecl(FunctionTemplateDecl *D);
98    void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D);
99    void VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D);
100    void VisitUsingDecl(UsingDecl *D);
101    void VisitUsingShadowDecl(UsingShadowDecl *D);
102    void VisitLinkageSpecDecl(LinkageSpecDecl *D);
103    void VisitFileScopeAsmDecl(FileScopeAsmDecl *D);
104    void VisitImportDecl(ImportDecl *D);
105    void VisitAccessSpecDecl(AccessSpecDecl *D);
106    void VisitFriendDecl(FriendDecl *D);
107    void VisitFriendTemplateDecl(FriendTemplateDecl *D);
108    void VisitStaticAssertDecl(StaticAssertDecl *D);
109    void VisitBlockDecl(BlockDecl *D);
110    void VisitCapturedDecl(CapturedDecl *D);
111    void VisitEmptyDecl(EmptyDecl *D);
112
113    void VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
114                          uint64_t VisibleOffset);
115    template <typename T> void VisitRedeclarable(Redeclarable<T> *D);
116
117
118    // FIXME: Put in the same order is DeclNodes.td?
119    void VisitObjCMethodDecl(ObjCMethodDecl *D);
120    void VisitObjCContainerDecl(ObjCContainerDecl *D);
121    void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D);
122    void VisitObjCIvarDecl(ObjCIvarDecl *D);
123    void VisitObjCProtocolDecl(ObjCProtocolDecl *D);
124    void VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D);
125    void VisitObjCCategoryDecl(ObjCCategoryDecl *D);
126    void VisitObjCImplDecl(ObjCImplDecl *D);
127    void VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D);
128    void VisitObjCImplementationDecl(ObjCImplementationDecl *D);
129    void VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D);
130    void VisitObjCPropertyDecl(ObjCPropertyDecl *D);
131    void VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D);
132    void VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D);
133  };
134}
135
136void ASTDeclWriter::Visit(Decl *D) {
137  DeclVisitor<ASTDeclWriter>::Visit(D);
138
139  // Source locations require array (variable-length) abbreviations.  The
140  // abbreviation infrastructure requires that arrays are encoded last, so
141  // we handle it here in the case of those classes derived from DeclaratorDecl
142  if (DeclaratorDecl *DD = dyn_cast<DeclaratorDecl>(D)){
143    Writer.AddTypeSourceInfo(DD->getTypeSourceInfo(), Record);
144  }
145
146  // Handle FunctionDecl's body here and write it after all other Stmts/Exprs
147  // have been written. We want it last because we will not read it back when
148  // retrieving it from the AST, we'll just lazily set the offset.
149  if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
150    Record.push_back(FD->doesThisDeclarationHaveABody());
151    if (FD->doesThisDeclarationHaveABody())
152      Writer.AddStmt(FD->getBody());
153  }
154}
155
156void ASTDeclWriter::VisitDecl(Decl *D) {
157  Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record);
158  Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record);
159  Record.push_back(D->isInvalidDecl());
160  Record.push_back(D->hasAttrs());
161  if (D->hasAttrs())
162    Writer.WriteAttributes(ArrayRef<const Attr*>(D->getAttrs().begin(),
163                                                 D->getAttrs().size()), Record);
164  Record.push_back(D->isImplicit());
165  Record.push_back(D->isUsed(false));
166  Record.push_back(D->isReferenced());
167  Record.push_back(D->isTopLevelDeclInObjCContainer());
168  Record.push_back(D->getAccess());
169  Record.push_back(D->isModulePrivate());
170  Record.push_back(Writer.inferSubmoduleIDFromLocation(D->getLocation()));
171}
172
173void ASTDeclWriter::VisitTranslationUnitDecl(TranslationUnitDecl *D) {
174  llvm_unreachable("Translation units aren't directly serialized");
175}
176
177void ASTDeclWriter::VisitNamedDecl(NamedDecl *D) {
178  VisitDecl(D);
179  Writer.AddDeclarationName(D->getDeclName(), Record);
180}
181
182void ASTDeclWriter::VisitTypeDecl(TypeDecl *D) {
183  VisitNamedDecl(D);
184  Writer.AddSourceLocation(D->getLocStart(), Record);
185  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
186}
187
188void ASTDeclWriter::VisitTypedefNameDecl(TypedefNameDecl *D) {
189  VisitRedeclarable(D);
190  VisitTypeDecl(D);
191  Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
192  Record.push_back(D->isModed());
193  if (D->isModed())
194    Writer.AddTypeRef(D->getUnderlyingType(), Record);
195}
196
197void ASTDeclWriter::VisitTypedefDecl(TypedefDecl *D) {
198  VisitTypedefNameDecl(D);
199  if (!D->hasAttrs() &&
200      !D->isImplicit() &&
201      !D->isUsed(false) &&
202      D->getFirstDeclaration() == D->getMostRecentDecl() &&
203      !D->isInvalidDecl() &&
204      !D->isReferenced() &&
205      !D->isTopLevelDeclInObjCContainer() &&
206      D->getAccess() == AS_none &&
207      !D->isModulePrivate() &&
208      D->getDeclName().getNameKind() == DeclarationName::Identifier)
209    AbbrevToUse = Writer.getDeclTypedefAbbrev();
210
211  Code = serialization::DECL_TYPEDEF;
212}
213
214void ASTDeclWriter::VisitTypeAliasDecl(TypeAliasDecl *D) {
215  VisitTypedefNameDecl(D);
216  Code = serialization::DECL_TYPEALIAS;
217}
218
219void ASTDeclWriter::VisitTagDecl(TagDecl *D) {
220  VisitRedeclarable(D);
221  VisitTypeDecl(D);
222  Record.push_back(D->getIdentifierNamespace());
223  Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding
224  Record.push_back(D->isCompleteDefinition());
225  Record.push_back(D->isEmbeddedInDeclarator());
226  Record.push_back(D->isFreeStanding());
227  Record.push_back(D->isCompleteDefinitionRequired());
228  Writer.AddSourceLocation(D->getRBraceLoc(), Record);
229  Record.push_back(D->hasExtInfo());
230  if (D->hasExtInfo())
231    Writer.AddQualifierInfo(*D->getExtInfo(), Record);
232  else
233    Writer.AddDeclRef(D->getTypedefNameForAnonDecl(), Record);
234}
235
236void ASTDeclWriter::VisitEnumDecl(EnumDecl *D) {
237  VisitTagDecl(D);
238  Writer.AddTypeSourceInfo(D->getIntegerTypeSourceInfo(), Record);
239  if (!D->getIntegerTypeSourceInfo())
240    Writer.AddTypeRef(D->getIntegerType(), Record);
241  Writer.AddTypeRef(D->getPromotionType(), Record);
242  Record.push_back(D->getNumPositiveBits());
243  Record.push_back(D->getNumNegativeBits());
244  Record.push_back(D->isScoped());
245  Record.push_back(D->isScopedUsingClassTag());
246  Record.push_back(D->isFixed());
247  if (MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo()) {
248    Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
249    Record.push_back(MemberInfo->getTemplateSpecializationKind());
250    Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
251  } else {
252    Writer.AddDeclRef(0, Record);
253  }
254
255  if (!D->hasAttrs() &&
256      !D->isImplicit() &&
257      !D->isUsed(false) &&
258      !D->hasExtInfo() &&
259      D->getFirstDeclaration() == D->getMostRecentDecl() &&
260      !D->isInvalidDecl() &&
261      !D->isReferenced() &&
262      !D->isTopLevelDeclInObjCContainer() &&
263      D->getAccess() == AS_none &&
264      !D->isModulePrivate() &&
265      !CXXRecordDecl::classofKind(D->getKind()) &&
266      !D->getIntegerTypeSourceInfo() &&
267      !D->getMemberSpecializationInfo() &&
268      D->getDeclName().getNameKind() == DeclarationName::Identifier)
269    AbbrevToUse = Writer.getDeclEnumAbbrev();
270
271  Code = serialization::DECL_ENUM;
272}
273
274void ASTDeclWriter::VisitRecordDecl(RecordDecl *D) {
275  VisitTagDecl(D);
276  Record.push_back(D->hasFlexibleArrayMember());
277  Record.push_back(D->isAnonymousStructOrUnion());
278  Record.push_back(D->hasObjectMember());
279  Record.push_back(D->hasVolatileMember());
280
281  if (!D->hasAttrs() &&
282      !D->isImplicit() &&
283      !D->isUsed(false) &&
284      !D->hasExtInfo() &&
285      D->getFirstDeclaration() == D->getMostRecentDecl() &&
286      !D->isInvalidDecl() &&
287      !D->isReferenced() &&
288      !D->isTopLevelDeclInObjCContainer() &&
289      D->getAccess() == AS_none &&
290      !D->isModulePrivate() &&
291      !CXXRecordDecl::classofKind(D->getKind()) &&
292      D->getDeclName().getNameKind() == DeclarationName::Identifier)
293    AbbrevToUse = Writer.getDeclRecordAbbrev();
294
295  Code = serialization::DECL_RECORD;
296}
297
298void ASTDeclWriter::VisitValueDecl(ValueDecl *D) {
299  VisitNamedDecl(D);
300  Writer.AddTypeRef(D->getType(), Record);
301}
302
303void ASTDeclWriter::VisitEnumConstantDecl(EnumConstantDecl *D) {
304  VisitValueDecl(D);
305  Record.push_back(D->getInitExpr()? 1 : 0);
306  if (D->getInitExpr())
307    Writer.AddStmt(D->getInitExpr());
308  Writer.AddAPSInt(D->getInitVal(), Record);
309
310  Code = serialization::DECL_ENUM_CONSTANT;
311}
312
313void ASTDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) {
314  VisitValueDecl(D);
315  Writer.AddSourceLocation(D->getInnerLocStart(), Record);
316  Record.push_back(D->hasExtInfo());
317  if (D->hasExtInfo())
318    Writer.AddQualifierInfo(*D->getExtInfo(), Record);
319}
320
321void ASTDeclWriter::VisitFunctionDecl(FunctionDecl *D) {
322  VisitRedeclarable(D);
323  VisitDeclaratorDecl(D);
324
325  Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
326  Record.push_back(D->getIdentifierNamespace());
327
328  // FunctionDecl's body is handled last at ASTWriterDecl::Visit,
329  // after everything else is written.
330
331  Record.push_back(D->getStorageClass()); // FIXME: stable encoding
332  Record.push_back(D->IsInline);
333  Record.push_back(D->isInlineSpecified());
334  Record.push_back(D->isVirtualAsWritten());
335  Record.push_back(D->isPure());
336  Record.push_back(D->hasInheritedPrototype());
337  Record.push_back(D->hasWrittenPrototype());
338  Record.push_back(D->isDeletedAsWritten());
339  Record.push_back(D->isTrivial());
340  Record.push_back(D->isDefaulted());
341  Record.push_back(D->isExplicitlyDefaulted());
342  Record.push_back(D->hasImplicitReturnZero());
343  Record.push_back(D->isConstexpr());
344  Record.push_back(D->HasSkippedBody);
345  Record.push_back(D->getLinkageInternal());
346  Writer.AddSourceLocation(D->getLocEnd(), Record);
347
348  Record.push_back(D->getTemplatedKind());
349  switch (D->getTemplatedKind()) {
350  case FunctionDecl::TK_NonTemplate:
351    break;
352  case FunctionDecl::TK_FunctionTemplate:
353    Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record);
354    break;
355  case FunctionDecl::TK_MemberSpecialization: {
356    MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo();
357    Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record);
358    Record.push_back(MemberInfo->getTemplateSpecializationKind());
359    Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record);
360    break;
361  }
362  case FunctionDecl::TK_FunctionTemplateSpecialization: {
363    FunctionTemplateSpecializationInfo *
364      FTSInfo = D->getTemplateSpecializationInfo();
365    Writer.AddDeclRef(FTSInfo->getTemplate(), Record);
366    Record.push_back(FTSInfo->getTemplateSpecializationKind());
367
368    // Template arguments.
369    Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record);
370
371    // Template args as written.
372    Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0);
373    if (FTSInfo->TemplateArgumentsAsWritten) {
374      Record.push_back(FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs);
375      for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->NumTemplateArgs;
376             i!=e; ++i)
377        Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i],
378                                      Record);
379      Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->LAngleLoc,
380                               Record);
381      Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->RAngleLoc,
382                               Record);
383    }
384
385    Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record);
386
387    if (D->isCanonicalDecl()) {
388      // Write the template that contains the specializations set. We will
389      // add a FunctionTemplateSpecializationInfo to it when reading.
390      Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record);
391    }
392    break;
393  }
394  case FunctionDecl::TK_DependentFunctionTemplateSpecialization: {
395    DependentFunctionTemplateSpecializationInfo *
396      DFTSInfo = D->getDependentSpecializationInfo();
397
398    // Templates.
399    Record.push_back(DFTSInfo->getNumTemplates());
400    for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i)
401      Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record);
402
403    // Templates args.
404    Record.push_back(DFTSInfo->getNumTemplateArgs());
405    for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i)
406      Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record);
407    Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record);
408    Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record);
409    break;
410  }
411  }
412
413  Record.push_back(D->param_size());
414  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
415       P != PEnd; ++P)
416    Writer.AddDeclRef(*P, Record);
417  Code = serialization::DECL_FUNCTION;
418}
419
420void ASTDeclWriter::VisitObjCMethodDecl(ObjCMethodDecl *D) {
421  VisitNamedDecl(D);
422  // FIXME: convert to LazyStmtPtr?
423  // Unlike C/C++, method bodies will never be in header files.
424  bool HasBodyStuff = D->getBody() != 0     ||
425                      D->getSelfDecl() != 0 || D->getCmdDecl() != 0;
426  Record.push_back(HasBodyStuff);
427  if (HasBodyStuff) {
428    Writer.AddStmt(D->getBody());
429    Writer.AddDeclRef(D->getSelfDecl(), Record);
430    Writer.AddDeclRef(D->getCmdDecl(), Record);
431  }
432  Record.push_back(D->isInstanceMethod());
433  Record.push_back(D->isVariadic());
434  Record.push_back(D->isPropertyAccessor());
435  Record.push_back(D->isDefined());
436  Record.push_back(D->IsOverriding);
437  Record.push_back(D->HasSkippedBody);
438
439  Record.push_back(D->IsRedeclaration);
440  Record.push_back(D->HasRedeclaration);
441  if (D->HasRedeclaration) {
442    assert(Context.getObjCMethodRedeclaration(D));
443    Writer.AddDeclRef(Context.getObjCMethodRedeclaration(D), Record);
444  }
445
446  // FIXME: stable encoding for @required/@optional
447  Record.push_back(D->getImplementationControl());
448  // FIXME: stable encoding for in/out/inout/bycopy/byref/oneway
449  Record.push_back(D->getObjCDeclQualifier());
450  Record.push_back(D->hasRelatedResultType());
451  Writer.AddTypeRef(D->getResultType(), Record);
452  Writer.AddTypeSourceInfo(D->getResultTypeSourceInfo(), Record);
453  Writer.AddSourceLocation(D->getLocEnd(), Record);
454  Record.push_back(D->param_size());
455  for (ObjCMethodDecl::param_iterator P = D->param_begin(),
456                                   PEnd = D->param_end(); P != PEnd; ++P)
457    Writer.AddDeclRef(*P, Record);
458
459  Record.push_back(D->SelLocsKind);
460  unsigned NumStoredSelLocs = D->getNumStoredSelLocs();
461  SourceLocation *SelLocs = D->getStoredSelLocs();
462  Record.push_back(NumStoredSelLocs);
463  for (unsigned i = 0; i != NumStoredSelLocs; ++i)
464    Writer.AddSourceLocation(SelLocs[i], Record);
465
466  Code = serialization::DECL_OBJC_METHOD;
467}
468
469void ASTDeclWriter::VisitObjCContainerDecl(ObjCContainerDecl *D) {
470  VisitNamedDecl(D);
471  Writer.AddSourceLocation(D->getAtStartLoc(), Record);
472  Writer.AddSourceRange(D->getAtEndRange(), Record);
473  // Abstract class (no need to define a stable serialization::DECL code).
474}
475
476void ASTDeclWriter::VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
477  VisitRedeclarable(D);
478  VisitObjCContainerDecl(D);
479  Writer.AddTypeRef(QualType(D->getTypeForDecl(), 0), Record);
480
481  Record.push_back(D->isThisDeclarationADefinition());
482  if (D->isThisDeclarationADefinition()) {
483    // Write the DefinitionData
484    ObjCInterfaceDecl::DefinitionData &Data = D->data();
485
486    Writer.AddDeclRef(D->getSuperClass(), Record);
487    Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
488    Writer.AddSourceLocation(D->getEndOfDefinitionLoc(), Record);
489
490    // Write out the protocols that are directly referenced by the @interface.
491    Record.push_back(Data.ReferencedProtocols.size());
492    for (ObjCInterfaceDecl::protocol_iterator P = D->protocol_begin(),
493                                           PEnd = D->protocol_end();
494         P != PEnd; ++P)
495      Writer.AddDeclRef(*P, Record);
496    for (ObjCInterfaceDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
497         PLEnd = D->protocol_loc_end();
498         PL != PLEnd; ++PL)
499      Writer.AddSourceLocation(*PL, Record);
500
501    // Write out the protocols that are transitively referenced.
502    Record.push_back(Data.AllReferencedProtocols.size());
503    for (ObjCList<ObjCProtocolDecl>::iterator
504              P = Data.AllReferencedProtocols.begin(),
505           PEnd = Data.AllReferencedProtocols.end();
506         P != PEnd; ++P)
507      Writer.AddDeclRef(*P, Record);
508
509
510    if (ObjCCategoryDecl *Cat = D->getCategoryListRaw()) {
511      // Ensure that we write out the set of categories for this class.
512      Writer.ObjCClassesWithCategories.insert(D);
513
514      // Make sure that the categories get serialized.
515      for (; Cat; Cat = Cat->getNextClassCategoryRaw())
516        (void)Writer.GetDeclRef(Cat);
517    }
518  }
519
520  Code = serialization::DECL_OBJC_INTERFACE;
521}
522
523void ASTDeclWriter::VisitObjCIvarDecl(ObjCIvarDecl *D) {
524  VisitFieldDecl(D);
525  // FIXME: stable encoding for @public/@private/@protected/@package
526  Record.push_back(D->getAccessControl());
527  Record.push_back(D->getSynthesize());
528
529  if (!D->hasAttrs() &&
530      !D->isImplicit() &&
531      !D->isUsed(false) &&
532      !D->isInvalidDecl() &&
533      !D->isReferenced() &&
534      !D->isModulePrivate() &&
535      !D->getBitWidth() &&
536      !D->hasExtInfo() &&
537      D->getDeclName())
538    AbbrevToUse = Writer.getDeclObjCIvarAbbrev();
539
540  Code = serialization::DECL_OBJC_IVAR;
541}
542
543void ASTDeclWriter::VisitObjCProtocolDecl(ObjCProtocolDecl *D) {
544  VisitRedeclarable(D);
545  VisitObjCContainerDecl(D);
546
547  Record.push_back(D->isThisDeclarationADefinition());
548  if (D->isThisDeclarationADefinition()) {
549    Record.push_back(D->protocol_size());
550    for (ObjCProtocolDecl::protocol_iterator
551         I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
552      Writer.AddDeclRef(*I, Record);
553    for (ObjCProtocolDecl::protocol_loc_iterator PL = D->protocol_loc_begin(),
554           PLEnd = D->protocol_loc_end();
555         PL != PLEnd; ++PL)
556      Writer.AddSourceLocation(*PL, Record);
557  }
558
559  Code = serialization::DECL_OBJC_PROTOCOL;
560}
561
562void ASTDeclWriter::VisitObjCAtDefsFieldDecl(ObjCAtDefsFieldDecl *D) {
563  VisitFieldDecl(D);
564  Code = serialization::DECL_OBJC_AT_DEFS_FIELD;
565}
566
567void ASTDeclWriter::VisitObjCCategoryDecl(ObjCCategoryDecl *D) {
568  VisitObjCContainerDecl(D);
569  Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
570  Writer.AddSourceLocation(D->getIvarLBraceLoc(), Record);
571  Writer.AddSourceLocation(D->getIvarRBraceLoc(), Record);
572  Writer.AddDeclRef(D->getClassInterface(), Record);
573  Record.push_back(D->protocol_size());
574  for (ObjCCategoryDecl::protocol_iterator
575       I = D->protocol_begin(), IEnd = D->protocol_end(); I != IEnd; ++I)
576    Writer.AddDeclRef(*I, Record);
577  for (ObjCCategoryDecl::protocol_loc_iterator
578         PL = D->protocol_loc_begin(), PLEnd = D->protocol_loc_end();
579       PL != PLEnd; ++PL)
580    Writer.AddSourceLocation(*PL, Record);
581  Code = serialization::DECL_OBJC_CATEGORY;
582}
583
584void ASTDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) {
585  VisitNamedDecl(D);
586  Writer.AddDeclRef(D->getClassInterface(), Record);
587  Code = serialization::DECL_OBJC_COMPATIBLE_ALIAS;
588}
589
590void ASTDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) {
591  VisitNamedDecl(D);
592  Writer.AddSourceLocation(D->getAtLoc(), Record);
593  Writer.AddSourceLocation(D->getLParenLoc(), Record);
594  Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record);
595  // FIXME: stable encoding
596  Record.push_back((unsigned)D->getPropertyAttributes());
597  Record.push_back((unsigned)D->getPropertyAttributesAsWritten());
598  // FIXME: stable encoding
599  Record.push_back((unsigned)D->getPropertyImplementation());
600  Writer.AddDeclarationName(D->getGetterName(), Record);
601  Writer.AddDeclarationName(D->getSetterName(), Record);
602  Writer.AddDeclRef(D->getGetterMethodDecl(), Record);
603  Writer.AddDeclRef(D->getSetterMethodDecl(), Record);
604  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
605  Code = serialization::DECL_OBJC_PROPERTY;
606}
607
608void ASTDeclWriter::VisitObjCImplDecl(ObjCImplDecl *D) {
609  VisitObjCContainerDecl(D);
610  Writer.AddDeclRef(D->getClassInterface(), Record);
611  // Abstract class (no need to define a stable serialization::DECL code).
612}
613
614void ASTDeclWriter::VisitObjCCategoryImplDecl(ObjCCategoryImplDecl *D) {
615  VisitObjCImplDecl(D);
616  Writer.AddIdentifierRef(D->getIdentifier(), Record);
617  Writer.AddSourceLocation(D->getCategoryNameLoc(), Record);
618  Code = serialization::DECL_OBJC_CATEGORY_IMPL;
619}
620
621void ASTDeclWriter::VisitObjCImplementationDecl(ObjCImplementationDecl *D) {
622  VisitObjCImplDecl(D);
623  Writer.AddDeclRef(D->getSuperClass(), Record);
624  Writer.AddSourceLocation(D->getSuperClassLoc(), Record);
625  Writer.AddSourceLocation(D->getIvarLBraceLoc(), Record);
626  Writer.AddSourceLocation(D->getIvarRBraceLoc(), Record);
627  Record.push_back(D->hasNonZeroConstructors());
628  Record.push_back(D->hasDestructors());
629  Writer.AddCXXCtorInitializers(D->IvarInitializers, D->NumIvarInitializers,
630                                Record);
631  Code = serialization::DECL_OBJC_IMPLEMENTATION;
632}
633
634void ASTDeclWriter::VisitObjCPropertyImplDecl(ObjCPropertyImplDecl *D) {
635  VisitDecl(D);
636  Writer.AddSourceLocation(D->getLocStart(), Record);
637  Writer.AddDeclRef(D->getPropertyDecl(), Record);
638  Writer.AddDeclRef(D->getPropertyIvarDecl(), Record);
639  Writer.AddSourceLocation(D->getPropertyIvarDeclLoc(), Record);
640  Writer.AddStmt(D->getGetterCXXConstructor());
641  Writer.AddStmt(D->getSetterCXXAssignment());
642  Code = serialization::DECL_OBJC_PROPERTY_IMPL;
643}
644
645void ASTDeclWriter::VisitFieldDecl(FieldDecl *D) {
646  VisitDeclaratorDecl(D);
647  Record.push_back(D->isMutable());
648  if (D->InitializerOrBitWidth.getInt() != ICIS_NoInit ||
649      D->InitializerOrBitWidth.getPointer()) {
650    Record.push_back(D->InitializerOrBitWidth.getInt() + 1);
651    Writer.AddStmt(D->InitializerOrBitWidth.getPointer());
652  } else {
653    Record.push_back(0);
654  }
655  if (!D->getDeclName())
656    Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record);
657
658  if (!D->hasAttrs() &&
659      !D->isImplicit() &&
660      !D->isUsed(false) &&
661      !D->isInvalidDecl() &&
662      !D->isReferenced() &&
663      !D->isTopLevelDeclInObjCContainer() &&
664      !D->isModulePrivate() &&
665      !D->getBitWidth() &&
666      !D->hasInClassInitializer() &&
667      !D->hasExtInfo() &&
668      !ObjCIvarDecl::classofKind(D->getKind()) &&
669      !ObjCAtDefsFieldDecl::classofKind(D->getKind()) &&
670      D->getDeclName())
671    AbbrevToUse = Writer.getDeclFieldAbbrev();
672
673  Code = serialization::DECL_FIELD;
674}
675
676void ASTDeclWriter::VisitMSPropertyDecl(MSPropertyDecl *D) {
677  VisitDeclaratorDecl(D);
678  Writer.AddIdentifierRef(D->getGetterId(), Record);
679  Writer.AddIdentifierRef(D->getSetterId(), Record);
680  Code = serialization::DECL_MS_PROPERTY;
681}
682
683void ASTDeclWriter::VisitIndirectFieldDecl(IndirectFieldDecl *D) {
684  VisitValueDecl(D);
685  Record.push_back(D->getChainingSize());
686
687  for (IndirectFieldDecl::chain_iterator
688       P = D->chain_begin(),
689       PEnd = D->chain_end(); P != PEnd; ++P)
690    Writer.AddDeclRef(*P, Record);
691  Code = serialization::DECL_INDIRECTFIELD;
692}
693
694void ASTDeclWriter::VisitVarDecl(VarDecl *D) {
695  VisitRedeclarable(D);
696  VisitDeclaratorDecl(D);
697  Record.push_back(D->getStorageClass());
698  Record.push_back(D->getTSCSpec());
699  Record.push_back(D->getInitStyle());
700  Record.push_back(D->isExceptionVariable());
701  Record.push_back(D->isNRVOVariable());
702  Record.push_back(D->isCXXForRangeDecl());
703  Record.push_back(D->isARCPseudoStrong());
704  Record.push_back(D->isConstexpr());
705  Record.push_back(D->getLinkageInternal());
706
707  if (D->getInit()) {
708    Record.push_back(!D->isInitKnownICE() ? 1 : (D->isInitICE() ? 3 : 2));
709    Writer.AddStmt(D->getInit());
710  } else {
711    Record.push_back(0);
712  }
713
714  MemberSpecializationInfo *SpecInfo
715    = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0;
716  Record.push_back(SpecInfo != 0);
717  if (SpecInfo) {
718    Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record);
719    Record.push_back(SpecInfo->getTemplateSpecializationKind());
720    Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record);
721  }
722
723  if (!D->hasAttrs() &&
724      !D->isImplicit() &&
725      !D->isUsed(false) &&
726      !D->isInvalidDecl() &&
727      !D->isReferenced() &&
728      !D->isTopLevelDeclInObjCContainer() &&
729      D->getAccess() == AS_none &&
730      !D->isModulePrivate() &&
731      D->getDeclName().getNameKind() == DeclarationName::Identifier &&
732      !D->hasExtInfo() &&
733      D->getFirstDeclaration() == D->getMostRecentDecl() &&
734      D->getInitStyle() == VarDecl::CInit &&
735      D->getInit() == 0 &&
736      !isa<ParmVarDecl>(D) &&
737      !D->isConstexpr() &&
738      !SpecInfo)
739    AbbrevToUse = Writer.getDeclVarAbbrev();
740
741  Code = serialization::DECL_VAR;
742}
743
744void ASTDeclWriter::VisitImplicitParamDecl(ImplicitParamDecl *D) {
745  VisitVarDecl(D);
746  Code = serialization::DECL_IMPLICIT_PARAM;
747}
748
749void ASTDeclWriter::VisitParmVarDecl(ParmVarDecl *D) {
750  VisitVarDecl(D);
751  Record.push_back(D->isObjCMethodParameter());
752  Record.push_back(D->getFunctionScopeDepth());
753  Record.push_back(D->getFunctionScopeIndex());
754  Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding
755  Record.push_back(D->isKNRPromoted());
756  Record.push_back(D->hasInheritedDefaultArg());
757  Record.push_back(D->hasUninstantiatedDefaultArg());
758  if (D->hasUninstantiatedDefaultArg())
759    Writer.AddStmt(D->getUninstantiatedDefaultArg());
760  Code = serialization::DECL_PARM_VAR;
761
762  assert(!D->isARCPseudoStrong()); // can be true of ImplicitParamDecl
763
764  // If the assumptions about the DECL_PARM_VAR abbrev are true, use it.  Here
765  // we dynamically check for the properties that we optimize for, but don't
766  // know are true of all PARM_VAR_DECLs.
767  if (!D->hasAttrs() &&
768      !D->hasExtInfo() &&
769      !D->isImplicit() &&
770      !D->isUsed(false) &&
771      !D->isInvalidDecl() &&
772      !D->isReferenced() &&
773      D->getAccess() == AS_none &&
774      !D->isModulePrivate() &&
775      D->getStorageClass() == 0 &&
776      D->getInitStyle() == VarDecl::CInit && // Can params have anything else?
777      D->getFunctionScopeDepth() == 0 &&
778      D->getObjCDeclQualifier() == 0 &&
779      !D->isKNRPromoted() &&
780      !D->hasInheritedDefaultArg() &&
781      D->getInit() == 0 &&
782      !D->hasUninstantiatedDefaultArg())  // No default expr.
783    AbbrevToUse = Writer.getDeclParmVarAbbrev();
784
785  // Check things we know are true of *every* PARM_VAR_DECL, which is more than
786  // just us assuming it.
787  assert(!D->getTSCSpec() && "PARM_VAR_DECL can't use TLS");
788  assert(D->getAccess() == AS_none && "PARM_VAR_DECL can't be public/private");
789  assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var");
790  assert(D->getPreviousDecl() == 0 && "PARM_VAR_DECL can't be redecl");
791  assert(!D->isStaticDataMember() &&
792         "PARM_VAR_DECL can't be static data member");
793}
794
795void ASTDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) {
796  VisitDecl(D);
797  Writer.AddStmt(D->getAsmString());
798  Writer.AddSourceLocation(D->getRParenLoc(), Record);
799  Code = serialization::DECL_FILE_SCOPE_ASM;
800}
801
802void ASTDeclWriter::VisitEmptyDecl(EmptyDecl *D) {
803  VisitDecl(D);
804  Code = serialization::DECL_EMPTY;
805}
806
807void ASTDeclWriter::VisitBlockDecl(BlockDecl *D) {
808  VisitDecl(D);
809  Writer.AddStmt(D->getBody());
810  Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record);
811  Record.push_back(D->param_size());
812  for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end();
813       P != PEnd; ++P)
814    Writer.AddDeclRef(*P, Record);
815  Record.push_back(D->isVariadic());
816  Record.push_back(D->blockMissingReturnType());
817  Record.push_back(D->isConversionFromLambda());
818  Record.push_back(D->capturesCXXThis());
819  Record.push_back(D->getNumCaptures());
820  for (BlockDecl::capture_iterator
821         i = D->capture_begin(), e = D->capture_end(); i != e; ++i) {
822    const BlockDecl::Capture &capture = *i;
823    Writer.AddDeclRef(capture.getVariable(), Record);
824
825    unsigned flags = 0;
826    if (capture.isByRef()) flags |= 1;
827    if (capture.isNested()) flags |= 2;
828    if (capture.hasCopyExpr()) flags |= 4;
829    Record.push_back(flags);
830
831    if (capture.hasCopyExpr()) Writer.AddStmt(capture.getCopyExpr());
832  }
833
834  Code = serialization::DECL_BLOCK;
835}
836
837void ASTDeclWriter::VisitCapturedDecl(CapturedDecl *CD) {
838  Record.push_back(CD->getNumParams());
839  VisitDecl(CD);
840  // Body is stored by VisitCapturedStmt.
841  for (unsigned i = 0; i < CD->getNumParams(); ++i)
842    Writer.AddDeclRef(CD->getParam(i), Record);
843  Code = serialization::DECL_CAPTURED;
844}
845
846void ASTDeclWriter::VisitLinkageSpecDecl(LinkageSpecDecl *D) {
847  VisitDecl(D);
848  Record.push_back(D->getLanguage());
849  Writer.AddSourceLocation(D->getExternLoc(), Record);
850  Writer.AddSourceLocation(D->getRBraceLoc(), Record);
851  Code = serialization::DECL_LINKAGE_SPEC;
852}
853
854void ASTDeclWriter::VisitLabelDecl(LabelDecl *D) {
855  VisitNamedDecl(D);
856  Writer.AddSourceLocation(D->getLocStart(), Record);
857  Code = serialization::DECL_LABEL;
858}
859
860
861void ASTDeclWriter::VisitNamespaceDecl(NamespaceDecl *D) {
862  VisitRedeclarable(D);
863  VisitNamedDecl(D);
864  Record.push_back(D->isInline());
865  Writer.AddSourceLocation(D->getLocStart(), Record);
866  Writer.AddSourceLocation(D->getRBraceLoc(), Record);
867
868  if (D->isOriginalNamespace())
869    Writer.AddDeclRef(D->getAnonymousNamespace(), Record);
870  Code = serialization::DECL_NAMESPACE;
871
872  if (Writer.hasChain() && !D->isOriginalNamespace() &&
873      D->getOriginalNamespace()->isFromASTFile()) {
874    NamespaceDecl *NS = D->getOriginalNamespace();
875    Writer.AddUpdatedDeclContext(NS);
876
877    // Make sure all visible decls are written. They will be recorded later.
878    if (StoredDeclsMap *Map = NS->buildLookup()) {
879      for (StoredDeclsMap::iterator D = Map->begin(), DEnd = Map->end();
880           D != DEnd; ++D) {
881        DeclContext::lookup_result R = D->second.getLookupResult();
882        for (DeclContext::lookup_iterator I = R.begin(), E = R.end(); I != E;
883             ++I)
884          Writer.GetDeclRef(*I);
885      }
886    }
887  }
888
889  if (Writer.hasChain() && D->isAnonymousNamespace() &&
890      D == D->getMostRecentDecl()) {
891    // This is a most recent reopening of the anonymous namespace. If its parent
892    // is in a previous PCH (or is the TU), mark that parent for update, because
893    // the original namespace always points to the latest re-opening of its
894    // anonymous namespace.
895    Decl *Parent = cast<Decl>(
896        D->getParent()->getRedeclContext()->getPrimaryContext());
897    if (Parent->isFromASTFile() || isa<TranslationUnitDecl>(Parent)) {
898      ASTWriter::UpdateRecord &Record = Writer.DeclUpdates[Parent];
899      Record.push_back(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE);
900      Writer.AddDeclRef(D, Record);
901    }
902  }
903}
904
905void ASTDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) {
906  VisitNamedDecl(D);
907  Writer.AddSourceLocation(D->getNamespaceLoc(), Record);
908  Writer.AddSourceLocation(D->getTargetNameLoc(), Record);
909  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
910  Writer.AddDeclRef(D->getNamespace(), Record);
911  Code = serialization::DECL_NAMESPACE_ALIAS;
912}
913
914void ASTDeclWriter::VisitUsingDecl(UsingDecl *D) {
915  VisitNamedDecl(D);
916  Writer.AddSourceLocation(D->getUsingLoc(), Record);
917  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
918  Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
919  Writer.AddDeclRef(D->FirstUsingShadow.getPointer(), Record);
920  Record.push_back(D->hasTypename());
921  Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record);
922  Code = serialization::DECL_USING;
923}
924
925void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) {
926  VisitNamedDecl(D);
927  Writer.AddDeclRef(D->getTargetDecl(), Record);
928  Writer.AddDeclRef(D->UsingOrNextShadow, Record);
929  Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record);
930  Code = serialization::DECL_USING_SHADOW;
931}
932
933void ASTDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) {
934  VisitNamedDecl(D);
935  Writer.AddSourceLocation(D->getUsingLoc(), Record);
936  Writer.AddSourceLocation(D->getNamespaceKeyLocation(), Record);
937  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
938  Writer.AddDeclRef(D->getNominatedNamespace(), Record);
939  Writer.AddDeclRef(dyn_cast<Decl>(D->getCommonAncestor()), Record);
940  Code = serialization::DECL_USING_DIRECTIVE;
941}
942
943void ASTDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) {
944  VisitValueDecl(D);
945  Writer.AddSourceLocation(D->getUsingLoc(), Record);
946  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
947  Writer.AddDeclarationNameLoc(D->DNLoc, D->getDeclName(), Record);
948  Code = serialization::DECL_UNRESOLVED_USING_VALUE;
949}
950
951void ASTDeclWriter::VisitUnresolvedUsingTypenameDecl(
952                                               UnresolvedUsingTypenameDecl *D) {
953  VisitTypeDecl(D);
954  Writer.AddSourceLocation(D->getTypenameLoc(), Record);
955  Writer.AddNestedNameSpecifierLoc(D->getQualifierLoc(), Record);
956  Code = serialization::DECL_UNRESOLVED_USING_TYPENAME;
957}
958
959void ASTDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) {
960  VisitRecordDecl(D);
961  Record.push_back(D->isThisDeclarationADefinition());
962  if (D->isThisDeclarationADefinition())
963    Writer.AddCXXDefinitionData(D, Record);
964
965  enum {
966    CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization
967  };
968  if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) {
969    Record.push_back(CXXRecTemplate);
970    Writer.AddDeclRef(TemplD, Record);
971  } else if (MemberSpecializationInfo *MSInfo
972               = D->getMemberSpecializationInfo()) {
973    Record.push_back(CXXRecMemberSpecialization);
974    Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record);
975    Record.push_back(MSInfo->getTemplateSpecializationKind());
976    Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
977  } else {
978    Record.push_back(CXXRecNotTemplate);
979  }
980
981  // Store (what we currently believe to be) the key function to avoid
982  // deserializing every method so we can compute it.
983  if (D->IsCompleteDefinition)
984    Writer.AddDeclRef(Context.getCurrentKeyFunction(D), Record);
985
986  Code = serialization::DECL_CXX_RECORD;
987}
988
989void ASTDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) {
990  VisitFunctionDecl(D);
991  if (D->isCanonicalDecl()) {
992    Record.push_back(D->size_overridden_methods());
993    for (CXXMethodDecl::method_iterator
994           I = D->begin_overridden_methods(), E = D->end_overridden_methods();
995           I != E; ++I)
996      Writer.AddDeclRef(*I, Record);
997  } else {
998    // We only need to record overridden methods once for the canonical decl.
999    Record.push_back(0);
1000  }
1001  Code = serialization::DECL_CXX_METHOD;
1002}
1003
1004void ASTDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) {
1005  VisitCXXMethodDecl(D);
1006
1007  Record.push_back(D->IsExplicitSpecified);
1008  Writer.AddCXXCtorInitializers(D->CtorInitializers, D->NumCtorInitializers,
1009                                Record);
1010
1011  Code = serialization::DECL_CXX_CONSTRUCTOR;
1012}
1013
1014void ASTDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) {
1015  VisitCXXMethodDecl(D);
1016
1017  Writer.AddDeclRef(D->OperatorDelete, Record);
1018
1019  Code = serialization::DECL_CXX_DESTRUCTOR;
1020}
1021
1022void ASTDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) {
1023  VisitCXXMethodDecl(D);
1024  Record.push_back(D->IsExplicitSpecified);
1025  Code = serialization::DECL_CXX_CONVERSION;
1026}
1027
1028void ASTDeclWriter::VisitImportDecl(ImportDecl *D) {
1029  VisitDecl(D);
1030  Record.push_back(Writer.getSubmoduleID(D->getImportedModule()));
1031  ArrayRef<SourceLocation> IdentifierLocs = D->getIdentifierLocs();
1032  Record.push_back(!IdentifierLocs.empty());
1033  if (IdentifierLocs.empty()) {
1034    Writer.AddSourceLocation(D->getLocEnd(), Record);
1035    Record.push_back(1);
1036  } else {
1037    for (unsigned I = 0, N = IdentifierLocs.size(); I != N; ++I)
1038      Writer.AddSourceLocation(IdentifierLocs[I], Record);
1039    Record.push_back(IdentifierLocs.size());
1040  }
1041  // Note: the number of source locations must always be the last element in
1042  // the record.
1043  Code = serialization::DECL_IMPORT;
1044}
1045
1046void ASTDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) {
1047  VisitDecl(D);
1048  Writer.AddSourceLocation(D->getColonLoc(), Record);
1049  Code = serialization::DECL_ACCESS_SPEC;
1050}
1051
1052void ASTDeclWriter::VisitFriendDecl(FriendDecl *D) {
1053  // Record the number of friend type template parameter lists here
1054  // so as to simplify memory allocation during deserialization.
1055  Record.push_back(D->NumTPLists);
1056  VisitDecl(D);
1057  bool hasFriendDecl = D->Friend.is<NamedDecl*>();
1058  Record.push_back(hasFriendDecl);
1059  if (hasFriendDecl)
1060    Writer.AddDeclRef(D->getFriendDecl(), Record);
1061  else
1062    Writer.AddTypeSourceInfo(D->getFriendType(), Record);
1063  for (unsigned i = 0; i < D->NumTPLists; ++i)
1064    Writer.AddTemplateParameterList(D->getFriendTypeTemplateParameterList(i),
1065                                    Record);
1066  Writer.AddDeclRef(D->getNextFriend(), Record);
1067  Record.push_back(D->UnsupportedFriend);
1068  Writer.AddSourceLocation(D->FriendLoc, Record);
1069  Code = serialization::DECL_FRIEND;
1070}
1071
1072void ASTDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) {
1073  VisitDecl(D);
1074  Record.push_back(D->getNumTemplateParameters());
1075  for (unsigned i = 0, e = D->getNumTemplateParameters(); i != e; ++i)
1076    Writer.AddTemplateParameterList(D->getTemplateParameterList(i), Record);
1077  Record.push_back(D->getFriendDecl() != 0);
1078  if (D->getFriendDecl())
1079    Writer.AddDeclRef(D->getFriendDecl(), Record);
1080  else
1081    Writer.AddTypeSourceInfo(D->getFriendType(), Record);
1082  Writer.AddSourceLocation(D->getFriendLoc(), Record);
1083  Code = serialization::DECL_FRIEND_TEMPLATE;
1084}
1085
1086void ASTDeclWriter::VisitTemplateDecl(TemplateDecl *D) {
1087  VisitNamedDecl(D);
1088
1089  Writer.AddDeclRef(D->getTemplatedDecl(), Record);
1090  Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
1091}
1092
1093void ASTDeclWriter::VisitRedeclarableTemplateDecl(RedeclarableTemplateDecl *D) {
1094  VisitRedeclarable(D);
1095
1096  // Emit data to initialize CommonOrPrev before VisitTemplateDecl so that
1097  // getCommonPtr() can be used while this is still initializing.
1098  if (D->isFirstDeclaration()) {
1099    // This declaration owns the 'common' pointer, so serialize that data now.
1100    Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record);
1101    if (D->getInstantiatedFromMemberTemplate())
1102      Record.push_back(D->isMemberSpecialization());
1103  }
1104
1105  VisitTemplateDecl(D);
1106  Record.push_back(D->getIdentifierNamespace());
1107}
1108
1109void ASTDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) {
1110  VisitRedeclarableTemplateDecl(D);
1111
1112  if (D->isFirstDeclaration()) {
1113    typedef llvm::FoldingSetVector<ClassTemplateSpecializationDecl> CTSDSetTy;
1114    CTSDSetTy &CTSDSet = D->getSpecializations();
1115    Record.push_back(CTSDSet.size());
1116    for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) {
1117      assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
1118      Writer.AddDeclRef(&*I, Record);
1119    }
1120
1121    typedef llvm::FoldingSetVector<ClassTemplatePartialSpecializationDecl>
1122      CTPSDSetTy;
1123    CTPSDSetTy &CTPSDSet = D->getPartialSpecializations();
1124    Record.push_back(CTPSDSet.size());
1125    for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) {
1126      assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
1127      Writer.AddDeclRef(&*I, Record);
1128    }
1129
1130    Writer.AddTypeRef(D->getCommonPtr()->InjectedClassNameType, Record);
1131  }
1132  Code = serialization::DECL_CLASS_TEMPLATE;
1133}
1134
1135void ASTDeclWriter::VisitClassTemplateSpecializationDecl(
1136                                           ClassTemplateSpecializationDecl *D) {
1137  VisitCXXRecordDecl(D);
1138
1139  llvm::PointerUnion<ClassTemplateDecl *,
1140                     ClassTemplatePartialSpecializationDecl *> InstFrom
1141    = D->getSpecializedTemplateOrPartial();
1142  if (Decl *InstFromD = InstFrom.dyn_cast<ClassTemplateDecl *>()) {
1143    Writer.AddDeclRef(InstFromD, Record);
1144  } else {
1145    Writer.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>(),
1146                      Record);
1147    Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
1148  }
1149
1150  // Explicit info.
1151  Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
1152  if (D->getTypeAsWritten()) {
1153    Writer.AddSourceLocation(D->getExternLoc(), Record);
1154    Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
1155  }
1156
1157  Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
1158  Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
1159  Record.push_back(D->getSpecializationKind());
1160  Record.push_back(D->isCanonicalDecl());
1161
1162  if (D->isCanonicalDecl()) {
1163    // When reading, we'll add it to the folding set of the following template.
1164    Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
1165  }
1166
1167  Code = serialization::DECL_CLASS_TEMPLATE_SPECIALIZATION;
1168}
1169
1170void ASTDeclWriter::VisitClassTemplatePartialSpecializationDecl(
1171                                    ClassTemplatePartialSpecializationDecl *D) {
1172  VisitClassTemplateSpecializationDecl(D);
1173
1174  Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
1175
1176  Record.push_back(D->getNumTemplateArgsAsWritten());
1177  for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i)
1178    Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record);
1179
1180  // These are read/set from/to the first declaration.
1181  if (D->getPreviousDecl() == 0) {
1182    Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
1183    Record.push_back(D->isMemberSpecialization());
1184  }
1185
1186  Code = serialization::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION;
1187}
1188
1189void ASTDeclWriter::VisitVarTemplateDecl(VarTemplateDecl *D) {
1190  VisitRedeclarableTemplateDecl(D);
1191
1192  if (D->isFirstDeclaration()) {
1193    typedef llvm::FoldingSetVector<VarTemplateSpecializationDecl> VTSDSetTy;
1194    VTSDSetTy &VTSDSet = D->getSpecializations();
1195    Record.push_back(VTSDSet.size());
1196    for (VTSDSetTy::iterator I = VTSDSet.begin(), E = VTSDSet.end(); I != E;
1197         ++I) {
1198      assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
1199      Writer.AddDeclRef(&*I, Record);
1200    }
1201
1202    typedef llvm::FoldingSetVector<VarTemplatePartialSpecializationDecl>
1203    VTPSDSetTy;
1204    VTPSDSetTy &VTPSDSet = D->getPartialSpecializations();
1205    Record.push_back(VTPSDSet.size());
1206    for (VTPSDSetTy::iterator I = VTPSDSet.begin(), E = VTPSDSet.end(); I != E;
1207         ++I) {
1208      assert(I->isCanonicalDecl() && "Expected only canonical decls in set");
1209      Writer.AddDeclRef(&*I, Record);
1210    }
1211  }
1212  Code = serialization::DECL_VAR_TEMPLATE;
1213}
1214
1215void ASTDeclWriter::VisitVarTemplateSpecializationDecl(
1216    VarTemplateSpecializationDecl *D) {
1217  VisitVarDecl(D);
1218
1219  llvm::PointerUnion<VarTemplateDecl *, VarTemplatePartialSpecializationDecl *>
1220  InstFrom = D->getSpecializedTemplateOrPartial();
1221  if (Decl *InstFromD = InstFrom.dyn_cast<VarTemplateDecl *>()) {
1222    Writer.AddDeclRef(InstFromD, Record);
1223  } else {
1224    Writer.AddDeclRef(InstFrom.get<VarTemplatePartialSpecializationDecl *>(),
1225                      Record);
1226    Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record);
1227  }
1228
1229  // Explicit info.
1230  Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record);
1231  if (D->getTypeAsWritten()) {
1232    Writer.AddSourceLocation(D->getExternLoc(), Record);
1233    Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record);
1234  }
1235
1236  Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record);
1237  Writer.AddSourceLocation(D->getPointOfInstantiation(), Record);
1238  Record.push_back(D->getSpecializationKind());
1239  Record.push_back(D->isCanonicalDecl());
1240
1241  if (D->isCanonicalDecl()) {
1242    // When reading, we'll add it to the folding set of the following template.
1243    Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record);
1244  }
1245
1246  Code = serialization::DECL_VAR_TEMPLATE_SPECIALIZATION;
1247}
1248
1249void ASTDeclWriter::VisitVarTemplatePartialSpecializationDecl(
1250    VarTemplatePartialSpecializationDecl *D) {
1251  VisitVarTemplateSpecializationDecl(D);
1252
1253  Writer.AddTemplateParameterList(D->getTemplateParameters(), Record);
1254
1255  Record.push_back(D->getNumTemplateArgsAsWritten());
1256  for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i)
1257    Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record);
1258
1259  Record.push_back(D->getSequenceNumber());
1260
1261  // These are read/set from/to the first declaration.
1262  if (D->getPreviousDecl() == 0) {
1263    Writer.AddDeclRef(D->getInstantiatedFromMember(), Record);
1264    Record.push_back(D->isMemberSpecialization());
1265  }
1266
1267  Code = serialization::DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION;
1268}
1269
1270void ASTDeclWriter::VisitClassScopeFunctionSpecializationDecl(
1271                                    ClassScopeFunctionSpecializationDecl *D) {
1272  VisitDecl(D);
1273  Writer.AddDeclRef(D->getSpecialization(), Record);
1274  Code = serialization::DECL_CLASS_SCOPE_FUNCTION_SPECIALIZATION;
1275}
1276
1277
1278void ASTDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) {
1279  VisitRedeclarableTemplateDecl(D);
1280
1281  if (D->isFirstDeclaration()) {
1282    // This FunctionTemplateDecl owns the CommonPtr; write it.
1283
1284    // Write the function specialization declarations.
1285    Record.push_back(D->getSpecializations().size());
1286    for (llvm::FoldingSetVector<FunctionTemplateSpecializationInfo>::iterator
1287           I = D->getSpecializations().begin(),
1288           E = D->getSpecializations().end()   ; I != E; ++I) {
1289      assert(I->Function->isCanonicalDecl() &&
1290             "Expected only canonical decls in set");
1291      Writer.AddDeclRef(I->Function, Record);
1292    }
1293  }
1294  Code = serialization::DECL_FUNCTION_TEMPLATE;
1295}
1296
1297void ASTDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) {
1298  VisitTypeDecl(D);
1299
1300  Record.push_back(D->wasDeclaredWithTypename());
1301  Record.push_back(D->defaultArgumentWasInherited());
1302  Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record);
1303
1304  Code = serialization::DECL_TEMPLATE_TYPE_PARM;
1305}
1306
1307void ASTDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) {
1308  // For an expanded parameter pack, record the number of expansion types here
1309  // so that it's easier for deserialization to allocate the right amount of
1310  // memory.
1311  if (D->isExpandedParameterPack())
1312    Record.push_back(D->getNumExpansionTypes());
1313
1314  VisitDeclaratorDecl(D);
1315  // TemplateParmPosition.
1316  Record.push_back(D->getDepth());
1317  Record.push_back(D->getPosition());
1318
1319  if (D->isExpandedParameterPack()) {
1320    for (unsigned I = 0, N = D->getNumExpansionTypes(); I != N; ++I) {
1321      Writer.AddTypeRef(D->getExpansionType(I), Record);
1322      Writer.AddTypeSourceInfo(D->getExpansionTypeSourceInfo(I), Record);
1323    }
1324
1325    Code = serialization::DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK;
1326  } else {
1327    // Rest of NonTypeTemplateParmDecl.
1328    Record.push_back(D->isParameterPack());
1329    Record.push_back(D->getDefaultArgument() != 0);
1330    if (D->getDefaultArgument()) {
1331      Writer.AddStmt(D->getDefaultArgument());
1332      Record.push_back(D->defaultArgumentWasInherited());
1333    }
1334    Code = serialization::DECL_NON_TYPE_TEMPLATE_PARM;
1335  }
1336}
1337
1338void ASTDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) {
1339  // For an expanded parameter pack, record the number of expansion types here
1340  // so that it's easier for deserialization to allocate the right amount of
1341  // memory.
1342  if (D->isExpandedParameterPack())
1343    Record.push_back(D->getNumExpansionTemplateParameters());
1344
1345  VisitTemplateDecl(D);
1346  // TemplateParmPosition.
1347  Record.push_back(D->getDepth());
1348  Record.push_back(D->getPosition());
1349
1350  if (D->isExpandedParameterPack()) {
1351    for (unsigned I = 0, N = D->getNumExpansionTemplateParameters();
1352         I != N; ++I)
1353      Writer.AddTemplateParameterList(D->getExpansionTemplateParameters(I),
1354                                      Record);
1355    Code = serialization::DECL_EXPANDED_TEMPLATE_TEMPLATE_PARM_PACK;
1356  } else {
1357    // Rest of TemplateTemplateParmDecl.
1358    Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record);
1359    Record.push_back(D->defaultArgumentWasInherited());
1360    Record.push_back(D->isParameterPack());
1361    Code = serialization::DECL_TEMPLATE_TEMPLATE_PARM;
1362  }
1363}
1364
1365void ASTDeclWriter::VisitTypeAliasTemplateDecl(TypeAliasTemplateDecl *D) {
1366  VisitRedeclarableTemplateDecl(D);
1367  Code = serialization::DECL_TYPE_ALIAS_TEMPLATE;
1368}
1369
1370void ASTDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) {
1371  VisitDecl(D);
1372  Writer.AddStmt(D->getAssertExpr());
1373  Record.push_back(D->isFailed());
1374  Writer.AddStmt(D->getMessage());
1375  Writer.AddSourceLocation(D->getRParenLoc(), Record);
1376  Code = serialization::DECL_STATIC_ASSERT;
1377}
1378
1379/// \brief Emit the DeclContext part of a declaration context decl.
1380///
1381/// \param LexicalOffset the offset at which the DECL_CONTEXT_LEXICAL
1382/// block for this declaration context is stored. May be 0 to indicate
1383/// that there are no declarations stored within this context.
1384///
1385/// \param VisibleOffset the offset at which the DECL_CONTEXT_VISIBLE
1386/// block for this declaration context is stored. May be 0 to indicate
1387/// that there are no declarations visible from this context. Note
1388/// that this value will not be emitted for non-primary declaration
1389/// contexts.
1390void ASTDeclWriter::VisitDeclContext(DeclContext *DC, uint64_t LexicalOffset,
1391                                     uint64_t VisibleOffset) {
1392  Record.push_back(LexicalOffset);
1393  Record.push_back(VisibleOffset);
1394}
1395
1396template <typename T>
1397void ASTDeclWriter::VisitRedeclarable(Redeclarable<T> *D) {
1398  T *First = D->getFirstDeclaration();
1399  if (First->getMostRecentDecl() != First) {
1400    assert(isRedeclarableDeclKind(static_cast<T *>(D)->getKind()) &&
1401           "Not considered redeclarable?");
1402
1403    // There is more than one declaration of this entity, so we will need to
1404    // write a redeclaration chain.
1405    Writer.AddDeclRef(First, Record);
1406    Writer.Redeclarations.insert(First);
1407
1408    // Make sure that we serialize both the previous and the most-recent
1409    // declarations, which (transitively) ensures that all declarations in the
1410    // chain get serialized.
1411    (void)Writer.GetDeclRef(D->getPreviousDecl());
1412    (void)Writer.GetDeclRef(First->getMostRecentDecl());
1413  } else {
1414    // We use the sentinel value 0 to indicate an only declaration.
1415    Record.push_back(0);
1416  }
1417
1418}
1419
1420void ASTDeclWriter::VisitOMPThreadPrivateDecl(OMPThreadPrivateDecl *D) {
1421  Record.push_back(D->varlist_size());
1422  VisitDecl(D);
1423  for (OMPThreadPrivateDecl::varlist_iterator I = D->varlist_begin(),
1424                                              E = D->varlist_end();
1425       I != E; ++I)
1426    Writer.AddStmt(*I);
1427  Code = serialization::DECL_OMP_THREADPRIVATE;
1428}
1429
1430//===----------------------------------------------------------------------===//
1431// ASTWriter Implementation
1432//===----------------------------------------------------------------------===//
1433
1434void ASTWriter::WriteDeclsBlockAbbrevs() {
1435  using namespace llvm;
1436
1437  BitCodeAbbrev *Abv;
1438
1439  // Abbreviation for DECL_FIELD
1440  Abv = new BitCodeAbbrev();
1441  Abv->Add(BitCodeAbbrevOp(serialization::DECL_FIELD));
1442  // Decl
1443  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1444  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1445  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1446  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1447  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1448  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1449  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1450  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1451  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
1452  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1453  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1454  // NamedDecl
1455  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1456  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1457  // ValueDecl
1458  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1459  // DeclaratorDecl
1460  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1461  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
1462  // FieldDecl
1463  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
1464  Abv->Add(BitCodeAbbrevOp(0));                       //getBitWidth
1465  // Type Source Info
1466  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1467  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1468  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1469  DeclFieldAbbrev = Stream.EmitAbbrev(Abv);
1470
1471  // Abbreviation for DECL_OBJC_IVAR
1472  Abv = new BitCodeAbbrev();
1473  Abv->Add(BitCodeAbbrevOp(serialization::DECL_OBJC_IVAR));
1474  // Decl
1475  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1476  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1477  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1478  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1479  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1480  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1481  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1482  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1483  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2));  // AccessSpecifier
1484  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1485  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1486  // NamedDecl
1487  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1488  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1489  // ValueDecl
1490  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1491  // DeclaratorDecl
1492  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1493  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
1494  // FieldDecl
1495  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isMutable
1496  Abv->Add(BitCodeAbbrevOp(0));                       //getBitWidth
1497  // ObjC Ivar
1498  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getAccessControl
1499  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getSynthesize
1500  // Type Source Info
1501  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1502  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1503  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1504  DeclObjCIvarAbbrev = Stream.EmitAbbrev(Abv);
1505
1506  // Abbreviation for DECL_ENUM
1507  Abv = new BitCodeAbbrev();
1508  Abv->Add(BitCodeAbbrevOp(serialization::DECL_ENUM));
1509  // Redeclarable
1510  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
1511  // Decl
1512  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1513  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1514  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1515  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1516  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1517  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1518  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1519  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1520  Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
1521  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1522  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1523  // NamedDecl
1524  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1525  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1526  // TypeDecl
1527  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
1528  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
1529  // TagDecl
1530  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
1531  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
1532  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
1533  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
1534  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
1535  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
1536  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
1537  Abv->Add(BitCodeAbbrevOp(0));                         // hasExtInfo
1538  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TypedefNameAnonDecl
1539  // EnumDecl
1540  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // AddTypeRef
1541  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IntegerType
1542  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getPromotionType
1543  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumPositiveBits
1544  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getNumNegativeBits
1545  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScoped
1546  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isScopedUsingClassTag
1547  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isFixed
1548  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // InstantiatedMembEnum
1549  // DC
1550  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
1551  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
1552  DeclEnumAbbrev = Stream.EmitAbbrev(Abv);
1553
1554  // Abbreviation for DECL_RECORD
1555  Abv = new BitCodeAbbrev();
1556  Abv->Add(BitCodeAbbrevOp(serialization::DECL_RECORD));
1557  // Redeclarable
1558  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
1559  // Decl
1560  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1561  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1562  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1563  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1564  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1565  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1566  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1567  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1568  Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
1569  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1570  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1571  // NamedDecl
1572  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1573  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1574  // TypeDecl
1575  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
1576  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
1577  // TagDecl
1578  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // IdentifierNamespace
1579  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // getTagKind
1580  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCompleteDefinition
1581  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // EmbeddedInDeclarator
1582  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFreeStanding
1583  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsCompleteDefinitionRequired
1584  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // SourceLocation
1585  Abv->Add(BitCodeAbbrevOp(0));                         // hasExtInfo
1586  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // TypedefNameAnonDecl
1587  // RecordDecl
1588  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // FlexibleArrayMember
1589  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // AnonymousStructUnion
1590  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasObjectMember
1591  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // hasVolatileMember
1592  // DC
1593  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // LexicalOffset
1594  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // VisibleOffset
1595  DeclRecordAbbrev = Stream.EmitAbbrev(Abv);
1596
1597  // Abbreviation for DECL_PARM_VAR
1598  Abv = new BitCodeAbbrev();
1599  Abv->Add(BitCodeAbbrevOp(serialization::DECL_PARM_VAR));
1600  // Redeclarable
1601  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
1602  // Decl
1603  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1604  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1605  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1606  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1607  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1608  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1609  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1610  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1611  Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
1612  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1613  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1614  // NamedDecl
1615  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1616  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1617  // ValueDecl
1618  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1619  // DeclaratorDecl
1620  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1621  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
1622  // VarDecl
1623  Abv->Add(BitCodeAbbrevOp(0));                       // StorageClass
1624  Abv->Add(BitCodeAbbrevOp(0));                       // getTSCSpec
1625  Abv->Add(BitCodeAbbrevOp(0));                       // hasCXXDirectInitializer
1626  Abv->Add(BitCodeAbbrevOp(0));                       // isExceptionVariable
1627  Abv->Add(BitCodeAbbrevOp(0));                       // isNRVOVariable
1628  Abv->Add(BitCodeAbbrevOp(0));                       // isCXXForRangeDecl
1629  Abv->Add(BitCodeAbbrevOp(0));                       // isARCPseudoStrong
1630  Abv->Add(BitCodeAbbrevOp(0));                       // isConstexpr
1631  Abv->Add(BitCodeAbbrevOp(0));                       // Linkage
1632  Abv->Add(BitCodeAbbrevOp(0));                       // HasInit
1633  Abv->Add(BitCodeAbbrevOp(0));                   // HasMemberSpecializationInfo
1634  // ParmVarDecl
1635  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsObjCMethodParameter
1636  Abv->Add(BitCodeAbbrevOp(0));                       // ScopeDepth
1637  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ScopeIndex
1638  Abv->Add(BitCodeAbbrevOp(0));                       // ObjCDeclQualifier
1639  Abv->Add(BitCodeAbbrevOp(0));                       // KNRPromoted
1640  Abv->Add(BitCodeAbbrevOp(0));                       // HasInheritedDefaultArg
1641  Abv->Add(BitCodeAbbrevOp(0));                   // HasUninstantiatedDefaultArg
1642  // Type Source Info
1643  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1644  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1645  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1646  DeclParmVarAbbrev = Stream.EmitAbbrev(Abv);
1647
1648  // Abbreviation for DECL_TYPEDEF
1649  Abv = new BitCodeAbbrev();
1650  Abv->Add(BitCodeAbbrevOp(serialization::DECL_TYPEDEF));
1651  // Redeclarable
1652  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
1653  // Decl
1654  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1655  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1656  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1657  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1658  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1659  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1660  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1661  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1662  Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
1663  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1664  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1665  // NamedDecl
1666  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1667  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1668  // TypeDecl
1669  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Source Location
1670  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type Ref
1671  // TypedefDecl
1672  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1673  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1674  DeclTypedefAbbrev = Stream.EmitAbbrev(Abv);
1675
1676  // Abbreviation for DECL_VAR
1677  Abv = new BitCodeAbbrev();
1678  Abv->Add(BitCodeAbbrevOp(serialization::DECL_VAR));
1679  // Redeclarable
1680  Abv->Add(BitCodeAbbrevOp(0));                       // No redeclaration
1681  // Decl
1682  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclContext
1683  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // LexicalDeclContext
1684  Abv->Add(BitCodeAbbrevOp(0));                       // isInvalidDecl
1685  Abv->Add(BitCodeAbbrevOp(0));                       // HasAttrs
1686  Abv->Add(BitCodeAbbrevOp(0));                       // isImplicit
1687  Abv->Add(BitCodeAbbrevOp(0));                       // isUsed
1688  Abv->Add(BitCodeAbbrevOp(0));                       // isReferenced
1689  Abv->Add(BitCodeAbbrevOp(0));                   // TopLevelDeclInObjCContainer
1690  Abv->Add(BitCodeAbbrevOp(AS_none));                 // C++ AccessSpecifier
1691  Abv->Add(BitCodeAbbrevOp(0));                       // ModulePrivate
1692  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // SubmoduleID
1693  // NamedDecl
1694  Abv->Add(BitCodeAbbrevOp(0));                       // NameKind = Identifier
1695  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Name
1696  // ValueDecl
1697  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1698  // DeclaratorDecl
1699  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // InnerStartLoc
1700  Abv->Add(BitCodeAbbrevOp(0));                       // hasExtInfo
1701  // VarDecl
1702  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // StorageClass
1703  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // getTSCSpec
1704  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // CXXDirectInitializer
1705  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isExceptionVariable
1706  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isNRVOVariable
1707  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isCXXForRangeDecl
1708  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // isARCPseudoStrong
1709  Abv->Add(BitCodeAbbrevOp(0));                         // isConstexpr
1710  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); // Linkage
1711  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasInit
1712  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // HasMemberSpecInfo
1713  // Type Source Info
1714  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
1715  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
1716  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // TypeLoc
1717  DeclVarAbbrev = Stream.EmitAbbrev(Abv);
1718
1719  // Abbreviation for EXPR_DECL_REF
1720  Abv = new BitCodeAbbrev();
1721  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_DECL_REF));
1722  //Stmt
1723  //Expr
1724  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1725  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
1726  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
1727  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
1728  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
1729  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
1730  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
1731  //DeclRefExpr
1732  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HasQualifier
1733  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //GetDeclFound
1734  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ExplicitTemplateArgs
1735  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //HadMultipleCandidates
1736  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //RefersToEnclosingLocal
1737  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // DeclRef
1738  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
1739  DeclRefExprAbbrev = Stream.EmitAbbrev(Abv);
1740
1741  // Abbreviation for EXPR_INTEGER_LITERAL
1742  Abv = new BitCodeAbbrev();
1743  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_INTEGER_LITERAL));
1744  //Stmt
1745  //Expr
1746  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1747  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
1748  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
1749  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
1750  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
1751  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
1752  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
1753  //Integer Literal
1754  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
1755  Abv->Add(BitCodeAbbrevOp(32));                      // Bit Width
1756  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Value
1757  IntegerLiteralAbbrev = Stream.EmitAbbrev(Abv);
1758
1759  // Abbreviation for EXPR_CHARACTER_LITERAL
1760  Abv = new BitCodeAbbrev();
1761  Abv->Add(BitCodeAbbrevOp(serialization::EXPR_CHARACTER_LITERAL));
1762  //Stmt
1763  //Expr
1764  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Type
1765  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //TypeDependent
1766  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //ValueDependent
1767  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //InstantiationDependent
1768  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); //UnexpandedParamPack
1769  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetValueKind
1770  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 3)); //GetObjectKind
1771  //Character Literal
1772  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // getValue
1773  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Location
1774  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // getKind
1775  CharacterLiteralAbbrev = Stream.EmitAbbrev(Abv);
1776
1777  Abv = new BitCodeAbbrev();
1778  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_LEXICAL));
1779  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1780  DeclContextLexicalAbbrev = Stream.EmitAbbrev(Abv);
1781
1782  Abv = new BitCodeAbbrev();
1783  Abv->Add(BitCodeAbbrevOp(serialization::DECL_CONTEXT_VISIBLE));
1784  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1785  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1786  DeclContextVisibleLookupAbbrev = Stream.EmitAbbrev(Abv);
1787}
1788
1789/// isRequiredDecl - Check if this is a "required" Decl, which must be seen by
1790/// consumers of the AST.
1791///
1792/// Such decls will always be deserialized from the AST file, so we would like
1793/// this to be as restrictive as possible. Currently the predicate is driven by
1794/// code generation requirements, if other clients have a different notion of
1795/// what is "required" then we may have to consider an alternate scheme where
1796/// clients can iterate over the top-level decls and get information on them,
1797/// without necessary deserializing them. We could explicitly require such
1798/// clients to use a separate API call to "realize" the decl. This should be
1799/// relatively painless since they would presumably only do it for top-level
1800/// decls.
1801static bool isRequiredDecl(const Decl *D, ASTContext &Context) {
1802  // An ObjCMethodDecl is never considered as "required" because its
1803  // implementation container always is.
1804
1805  // File scoped assembly or obj-c implementation must be seen.
1806  if (isa<FileScopeAsmDecl>(D) || isa<ObjCImplDecl>(D))
1807    return true;
1808
1809  return Context.DeclMustBeEmitted(D);
1810}
1811
1812void ASTWriter::WriteDecl(ASTContext &Context, Decl *D) {
1813  // Switch case IDs are per Decl.
1814  ClearSwitchCaseIDs();
1815
1816  RecordData Record;
1817  ASTDeclWriter W(*this, Context, Record);
1818
1819  // Determine the ID for this declaration.
1820  serialization::DeclID ID;
1821  if (D->isFromASTFile())
1822    ID = getDeclID(D);
1823  else {
1824    serialization::DeclID &IDR = DeclIDs[D];
1825    if (IDR == 0)
1826      IDR = NextDeclID++;
1827
1828    ID= IDR;
1829  }
1830
1831  bool isReplacingADecl = ID < FirstDeclID;
1832
1833  // If this declaration is also a DeclContext, write blocks for the
1834  // declarations that lexically stored inside its context and those
1835  // declarations that are visible from its context. These blocks
1836  // are written before the declaration itself so that we can put
1837  // their offsets into the record for the declaration.
1838  uint64_t LexicalOffset = 0;
1839  uint64_t VisibleOffset = 0;
1840  DeclContext *DC = dyn_cast<DeclContext>(D);
1841  if (DC) {
1842    if (isReplacingADecl) {
1843      // It is replacing a decl from a chained PCH; make sure that the
1844      // DeclContext is fully loaded.
1845      if (DC->hasExternalLexicalStorage())
1846        DC->LoadLexicalDeclsFromExternalStorage();
1847      if (DC->hasExternalVisibleStorage())
1848        Chain->completeVisibleDeclsMap(DC);
1849    }
1850    LexicalOffset = WriteDeclContextLexicalBlock(Context, DC);
1851    VisibleOffset = WriteDeclContextVisibleBlock(Context, DC);
1852  }
1853
1854  if (isReplacingADecl) {
1855    // We're replacing a decl in a previous file.
1856    ReplacedDecls.push_back(ReplacedDeclInfo(ID, Stream.GetCurrentBitNo(),
1857                                             D->getLocation()));
1858  } else {
1859    unsigned Index = ID - FirstDeclID;
1860
1861    // Record the offset for this declaration
1862    SourceLocation Loc = D->getLocation();
1863    if (DeclOffsets.size() == Index)
1864      DeclOffsets.push_back(DeclOffset(Loc, Stream.GetCurrentBitNo()));
1865    else if (DeclOffsets.size() < Index) {
1866      DeclOffsets.resize(Index+1);
1867      DeclOffsets[Index].setLocation(Loc);
1868      DeclOffsets[Index].BitOffset = Stream.GetCurrentBitNo();
1869    }
1870
1871    SourceManager &SM = Context.getSourceManager();
1872    if (Loc.isValid() && SM.isLocalSourceLocation(Loc))
1873      associateDeclWithFile(D, ID);
1874  }
1875
1876  // Build and emit a record for this declaration
1877  Record.clear();
1878  W.Code = (serialization::DeclCode)0;
1879  W.AbbrevToUse = 0;
1880  W.Visit(D);
1881  if (DC) W.VisitDeclContext(DC, LexicalOffset, VisibleOffset);
1882
1883  if (!W.Code)
1884    llvm::report_fatal_error(StringRef("unexpected declaration kind '") +
1885                            D->getDeclKindName() + "'");
1886  Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
1887
1888  // Flush any expressions that were written as part of this declaration.
1889  FlushStmts();
1890
1891  // Flush C++ base specifiers, if there are any.
1892  FlushCXXBaseSpecifiers();
1893
1894  // Note "external" declarations so that we can add them to a record in the
1895  // AST file later.
1896  //
1897  // FIXME: This should be renamed, the predicate is much more complicated.
1898  if (isRequiredDecl(D, Context))
1899    ExternalDefinitions.push_back(ID);
1900}
1901