1//===--- ASTWriter.cpp - AST File Writer ------------------------*- C++ -*-===//
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 defines the ASTWriter class, which writes AST files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Serialization/ASTWriter.h"
15#include "clang/Serialization/ModuleFileExtension.h"
16#include "ASTCommon.h"
17#include "ASTReaderInternals.h"
18#include "MultiOnDiskHashTable.h"
19#include "clang/AST/ASTContext.h"
20#include "clang/AST/Decl.h"
21#include "clang/AST/DeclContextInternals.h"
22#include "clang/AST/DeclFriend.h"
23#include "clang/AST/DeclLookups.h"
24#include "clang/AST/DeclTemplate.h"
25#include "clang/AST/Expr.h"
26#include "clang/AST/ExprCXX.h"
27#include "clang/AST/Type.h"
28#include "clang/AST/TypeLocVisitor.h"
29#include "clang/Basic/DiagnosticOptions.h"
30#include "clang/Basic/FileManager.h"
31#include "clang/Basic/FileSystemStatCache.h"
32#include "clang/Basic/SourceManager.h"
33#include "clang/Basic/SourceManagerInternals.h"
34#include "clang/Basic/TargetInfo.h"
35#include "clang/Basic/TargetOptions.h"
36#include "clang/Basic/Version.h"
37#include "clang/Basic/VersionTuple.h"
38#include "clang/Lex/HeaderSearch.h"
39#include "clang/Lex/HeaderSearchOptions.h"
40#include "clang/Lex/MacroInfo.h"
41#include "clang/Lex/PreprocessingRecord.h"
42#include "clang/Lex/Preprocessor.h"
43#include "clang/Lex/PreprocessorOptions.h"
44#include "clang/Sema/IdentifierResolver.h"
45#include "clang/Sema/Sema.h"
46#include "clang/Serialization/ASTReader.h"
47#include "clang/Serialization/SerializationDiagnostic.h"
48#include "llvm/ADT/APFloat.h"
49#include "llvm/ADT/APInt.h"
50#include "llvm/ADT/Hashing.h"
51#include "llvm/ADT/StringExtras.h"
52#include "llvm/Bitcode/BitstreamWriter.h"
53#include "llvm/Support/EndianStream.h"
54#include "llvm/Support/FileSystem.h"
55#include "llvm/Support/MemoryBuffer.h"
56#include "llvm/Support/OnDiskHashTable.h"
57#include "llvm/Support/Path.h"
58#include "llvm/Support/Process.h"
59#include <algorithm>
60#include <cstdio>
61#include <string.h>
62#include <utility>
63
64using namespace clang;
65using namespace clang::serialization;
66
67template <typename T, typename Allocator>
68static StringRef bytes(const std::vector<T, Allocator> &v) {
69  if (v.empty()) return StringRef();
70  return StringRef(reinterpret_cast<const char*>(&v[0]),
71                         sizeof(T) * v.size());
72}
73
74template <typename T>
75static StringRef bytes(const SmallVectorImpl<T> &v) {
76  return StringRef(reinterpret_cast<const char*>(v.data()),
77                         sizeof(T) * v.size());
78}
79
80//===----------------------------------------------------------------------===//
81// Type serialization
82//===----------------------------------------------------------------------===//
83
84namespace {
85  class ASTTypeWriter {
86    ASTWriter &Writer;
87    ASTWriter::RecordDataImpl &Record;
88
89  public:
90    /// \brief Type code that corresponds to the record generated.
91    TypeCode Code;
92    /// \brief Abbreviation to use for the record, if any.
93    unsigned AbbrevToUse;
94
95    ASTTypeWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
96      : Writer(Writer), Record(Record), Code(TYPE_EXT_QUAL) { }
97
98    void VisitArrayType(const ArrayType *T);
99    void VisitFunctionType(const FunctionType *T);
100    void VisitTagType(const TagType *T);
101
102#define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T);
103#define ABSTRACT_TYPE(Class, Base)
104#include "clang/AST/TypeNodes.def"
105  };
106} // end anonymous namespace
107
108void ASTTypeWriter::VisitBuiltinType(const BuiltinType *T) {
109  llvm_unreachable("Built-in types are never serialized");
110}
111
112void ASTTypeWriter::VisitComplexType(const ComplexType *T) {
113  Writer.AddTypeRef(T->getElementType(), Record);
114  Code = TYPE_COMPLEX;
115}
116
117void ASTTypeWriter::VisitPointerType(const PointerType *T) {
118  Writer.AddTypeRef(T->getPointeeType(), Record);
119  Code = TYPE_POINTER;
120}
121
122void ASTTypeWriter::VisitDecayedType(const DecayedType *T) {
123  Writer.AddTypeRef(T->getOriginalType(), Record);
124  Code = TYPE_DECAYED;
125}
126
127void ASTTypeWriter::VisitAdjustedType(const AdjustedType *T) {
128  Writer.AddTypeRef(T->getOriginalType(), Record);
129  Writer.AddTypeRef(T->getAdjustedType(), Record);
130  Code = TYPE_ADJUSTED;
131}
132
133void ASTTypeWriter::VisitBlockPointerType(const BlockPointerType *T) {
134  Writer.AddTypeRef(T->getPointeeType(), Record);
135  Code = TYPE_BLOCK_POINTER;
136}
137
138void ASTTypeWriter::VisitLValueReferenceType(const LValueReferenceType *T) {
139  Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
140  Record.push_back(T->isSpelledAsLValue());
141  Code = TYPE_LVALUE_REFERENCE;
142}
143
144void ASTTypeWriter::VisitRValueReferenceType(const RValueReferenceType *T) {
145  Writer.AddTypeRef(T->getPointeeTypeAsWritten(), Record);
146  Code = TYPE_RVALUE_REFERENCE;
147}
148
149void ASTTypeWriter::VisitMemberPointerType(const MemberPointerType *T) {
150  Writer.AddTypeRef(T->getPointeeType(), Record);
151  Writer.AddTypeRef(QualType(T->getClass(), 0), Record);
152  Code = TYPE_MEMBER_POINTER;
153}
154
155void ASTTypeWriter::VisitArrayType(const ArrayType *T) {
156  Writer.AddTypeRef(T->getElementType(), Record);
157  Record.push_back(T->getSizeModifier()); // FIXME: stable values
158  Record.push_back(T->getIndexTypeCVRQualifiers()); // FIXME: stable values
159}
160
161void ASTTypeWriter::VisitConstantArrayType(const ConstantArrayType *T) {
162  VisitArrayType(T);
163  Writer.AddAPInt(T->getSize(), Record);
164  Code = TYPE_CONSTANT_ARRAY;
165}
166
167void ASTTypeWriter::VisitIncompleteArrayType(const IncompleteArrayType *T) {
168  VisitArrayType(T);
169  Code = TYPE_INCOMPLETE_ARRAY;
170}
171
172void ASTTypeWriter::VisitVariableArrayType(const VariableArrayType *T) {
173  VisitArrayType(T);
174  Writer.AddSourceLocation(T->getLBracketLoc(), Record);
175  Writer.AddSourceLocation(T->getRBracketLoc(), Record);
176  Writer.AddStmt(T->getSizeExpr());
177  Code = TYPE_VARIABLE_ARRAY;
178}
179
180void ASTTypeWriter::VisitVectorType(const VectorType *T) {
181  Writer.AddTypeRef(T->getElementType(), Record);
182  Record.push_back(T->getNumElements());
183  Record.push_back(T->getVectorKind());
184  Code = TYPE_VECTOR;
185}
186
187void ASTTypeWriter::VisitExtVectorType(const ExtVectorType *T) {
188  VisitVectorType(T);
189  Code = TYPE_EXT_VECTOR;
190}
191
192void ASTTypeWriter::VisitFunctionType(const FunctionType *T) {
193  Writer.AddTypeRef(T->getReturnType(), Record);
194  FunctionType::ExtInfo C = T->getExtInfo();
195  Record.push_back(C.getNoReturn());
196  Record.push_back(C.getHasRegParm());
197  Record.push_back(C.getRegParm());
198  // FIXME: need to stabilize encoding of calling convention...
199  Record.push_back(C.getCC());
200  Record.push_back(C.getProducesResult());
201
202  if (C.getHasRegParm() || C.getRegParm() || C.getProducesResult())
203    AbbrevToUse = 0;
204}
205
206void ASTTypeWriter::VisitFunctionNoProtoType(const FunctionNoProtoType *T) {
207  VisitFunctionType(T);
208  Code = TYPE_FUNCTION_NO_PROTO;
209}
210
211static void addExceptionSpec(ASTWriter &Writer, const FunctionProtoType *T,
212                             ASTWriter::RecordDataImpl &Record) {
213  Record.push_back(T->getExceptionSpecType());
214  if (T->getExceptionSpecType() == EST_Dynamic) {
215    Record.push_back(T->getNumExceptions());
216    for (unsigned I = 0, N = T->getNumExceptions(); I != N; ++I)
217      Writer.AddTypeRef(T->getExceptionType(I), Record);
218  } else if (T->getExceptionSpecType() == EST_ComputedNoexcept) {
219    Writer.AddStmt(T->getNoexceptExpr());
220  } else if (T->getExceptionSpecType() == EST_Uninstantiated) {
221    Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
222    Writer.AddDeclRef(T->getExceptionSpecTemplate(), Record);
223  } else if (T->getExceptionSpecType() == EST_Unevaluated) {
224    Writer.AddDeclRef(T->getExceptionSpecDecl(), Record);
225  }
226}
227
228void ASTTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) {
229  VisitFunctionType(T);
230
231  Record.push_back(T->isVariadic());
232  Record.push_back(T->hasTrailingReturn());
233  Record.push_back(T->getTypeQuals());
234  Record.push_back(static_cast<unsigned>(T->getRefQualifier()));
235  addExceptionSpec(Writer, T, Record);
236
237  Record.push_back(T->getNumParams());
238  for (unsigned I = 0, N = T->getNumParams(); I != N; ++I)
239    Writer.AddTypeRef(T->getParamType(I), Record);
240
241  if (T->isVariadic() || T->hasTrailingReturn() || T->getTypeQuals() ||
242      T->getRefQualifier() || T->getExceptionSpecType() != EST_None)
243    AbbrevToUse = 0;
244
245  Code = TYPE_FUNCTION_PROTO;
246}
247
248void ASTTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) {
249  Writer.AddDeclRef(T->getDecl(), Record);
250  Code = TYPE_UNRESOLVED_USING;
251}
252
253void ASTTypeWriter::VisitTypedefType(const TypedefType *T) {
254  Writer.AddDeclRef(T->getDecl(), Record);
255  assert(!T->isCanonicalUnqualified() && "Invalid typedef ?");
256  Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record);
257  Code = TYPE_TYPEDEF;
258}
259
260void ASTTypeWriter::VisitTypeOfExprType(const TypeOfExprType *T) {
261  Writer.AddStmt(T->getUnderlyingExpr());
262  Code = TYPE_TYPEOF_EXPR;
263}
264
265void ASTTypeWriter::VisitTypeOfType(const TypeOfType *T) {
266  Writer.AddTypeRef(T->getUnderlyingType(), Record);
267  Code = TYPE_TYPEOF;
268}
269
270void ASTTypeWriter::VisitDecltypeType(const DecltypeType *T) {
271  Writer.AddTypeRef(T->getUnderlyingType(), Record);
272  Writer.AddStmt(T->getUnderlyingExpr());
273  Code = TYPE_DECLTYPE;
274}
275
276void ASTTypeWriter::VisitUnaryTransformType(const UnaryTransformType *T) {
277  Writer.AddTypeRef(T->getBaseType(), Record);
278  Writer.AddTypeRef(T->getUnderlyingType(), Record);
279  Record.push_back(T->getUTTKind());
280  Code = TYPE_UNARY_TRANSFORM;
281}
282
283void ASTTypeWriter::VisitAutoType(const AutoType *T) {
284  Writer.AddTypeRef(T->getDeducedType(), Record);
285  Record.push_back((unsigned)T->getKeyword());
286  if (T->getDeducedType().isNull())
287    Record.push_back(T->isDependentType());
288  Code = TYPE_AUTO;
289}
290
291void ASTTypeWriter::VisitTagType(const TagType *T) {
292  Record.push_back(T->isDependentType());
293  Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
294  assert(!T->isBeingDefined() &&
295         "Cannot serialize in the middle of a type definition");
296}
297
298void ASTTypeWriter::VisitRecordType(const RecordType *T) {
299  VisitTagType(T);
300  Code = TYPE_RECORD;
301}
302
303void ASTTypeWriter::VisitEnumType(const EnumType *T) {
304  VisitTagType(T);
305  Code = TYPE_ENUM;
306}
307
308void ASTTypeWriter::VisitAttributedType(const AttributedType *T) {
309  Writer.AddTypeRef(T->getModifiedType(), Record);
310  Writer.AddTypeRef(T->getEquivalentType(), Record);
311  Record.push_back(T->getAttrKind());
312  Code = TYPE_ATTRIBUTED;
313}
314
315void
316ASTTypeWriter::VisitSubstTemplateTypeParmType(
317                                        const SubstTemplateTypeParmType *T) {
318  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
319  Writer.AddTypeRef(T->getReplacementType(), Record);
320  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM;
321}
322
323void
324ASTTypeWriter::VisitSubstTemplateTypeParmPackType(
325                                      const SubstTemplateTypeParmPackType *T) {
326  Writer.AddTypeRef(QualType(T->getReplacedParameter(), 0), Record);
327  Writer.AddTemplateArgument(T->getArgumentPack(), Record);
328  Code = TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK;
329}
330
331void
332ASTTypeWriter::VisitTemplateSpecializationType(
333                                       const TemplateSpecializationType *T) {
334  Record.push_back(T->isDependentType());
335  Writer.AddTemplateName(T->getTemplateName(), Record);
336  Record.push_back(T->getNumArgs());
337  for (const auto &ArgI : *T)
338    Writer.AddTemplateArgument(ArgI, Record);
339  Writer.AddTypeRef(T->isTypeAlias() ? T->getAliasedType() :
340                    T->isCanonicalUnqualified() ? QualType()
341                                                : T->getCanonicalTypeInternal(),
342                    Record);
343  Code = TYPE_TEMPLATE_SPECIALIZATION;
344}
345
346void
347ASTTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) {
348  VisitArrayType(T);
349  Writer.AddStmt(T->getSizeExpr());
350  Writer.AddSourceRange(T->getBracketsRange(), Record);
351  Code = TYPE_DEPENDENT_SIZED_ARRAY;
352}
353
354void
355ASTTypeWriter::VisitDependentSizedExtVectorType(
356                                        const DependentSizedExtVectorType *T) {
357  // FIXME: Serialize this type (C++ only)
358  llvm_unreachable("Cannot serialize dependent sized extended vector types");
359}
360
361void
362ASTTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) {
363  Record.push_back(T->getDepth());
364  Record.push_back(T->getIndex());
365  Record.push_back(T->isParameterPack());
366  Writer.AddDeclRef(T->getDecl(), Record);
367  Code = TYPE_TEMPLATE_TYPE_PARM;
368}
369
370void
371ASTTypeWriter::VisitDependentNameType(const DependentNameType *T) {
372  Record.push_back(T->getKeyword());
373  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
374  Writer.AddIdentifierRef(T->getIdentifier(), Record);
375  Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType()
376                                                : T->getCanonicalTypeInternal(),
377                    Record);
378  Code = TYPE_DEPENDENT_NAME;
379}
380
381void
382ASTTypeWriter::VisitDependentTemplateSpecializationType(
383                                const DependentTemplateSpecializationType *T) {
384  Record.push_back(T->getKeyword());
385  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
386  Writer.AddIdentifierRef(T->getIdentifier(), Record);
387  Record.push_back(T->getNumArgs());
388  for (const auto &I : *T)
389    Writer.AddTemplateArgument(I, Record);
390  Code = TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION;
391}
392
393void ASTTypeWriter::VisitPackExpansionType(const PackExpansionType *T) {
394  Writer.AddTypeRef(T->getPattern(), Record);
395  if (Optional<unsigned> NumExpansions = T->getNumExpansions())
396    Record.push_back(*NumExpansions + 1);
397  else
398    Record.push_back(0);
399  Code = TYPE_PACK_EXPANSION;
400}
401
402void ASTTypeWriter::VisitParenType(const ParenType *T) {
403  Writer.AddTypeRef(T->getInnerType(), Record);
404  Code = TYPE_PAREN;
405}
406
407void ASTTypeWriter::VisitElaboratedType(const ElaboratedType *T) {
408  Record.push_back(T->getKeyword());
409  Writer.AddNestedNameSpecifier(T->getQualifier(), Record);
410  Writer.AddTypeRef(T->getNamedType(), Record);
411  Code = TYPE_ELABORATED;
412}
413
414void ASTTypeWriter::VisitInjectedClassNameType(const InjectedClassNameType *T) {
415  Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
416  Writer.AddTypeRef(T->getInjectedSpecializationType(), Record);
417  Code = TYPE_INJECTED_CLASS_NAME;
418}
419
420void ASTTypeWriter::VisitObjCInterfaceType(const ObjCInterfaceType *T) {
421  Writer.AddDeclRef(T->getDecl()->getCanonicalDecl(), Record);
422  Code = TYPE_OBJC_INTERFACE;
423}
424
425void ASTTypeWriter::VisitObjCObjectType(const ObjCObjectType *T) {
426  Writer.AddTypeRef(T->getBaseType(), Record);
427  Record.push_back(T->getTypeArgsAsWritten().size());
428  for (auto TypeArg : T->getTypeArgsAsWritten())
429    Writer.AddTypeRef(TypeArg, Record);
430  Record.push_back(T->getNumProtocols());
431  for (const auto *I : T->quals())
432    Writer.AddDeclRef(I, Record);
433  Record.push_back(T->isKindOfTypeAsWritten());
434  Code = TYPE_OBJC_OBJECT;
435}
436
437void
438ASTTypeWriter::VisitObjCObjectPointerType(const ObjCObjectPointerType *T) {
439  Writer.AddTypeRef(T->getPointeeType(), Record);
440  Code = TYPE_OBJC_OBJECT_POINTER;
441}
442
443void
444ASTTypeWriter::VisitAtomicType(const AtomicType *T) {
445  Writer.AddTypeRef(T->getValueType(), Record);
446  Code = TYPE_ATOMIC;
447}
448
449namespace {
450
451class TypeLocWriter : public TypeLocVisitor<TypeLocWriter> {
452  ASTWriter &Writer;
453  ASTWriter::RecordDataImpl &Record;
454
455public:
456  TypeLocWriter(ASTWriter &Writer, ASTWriter::RecordDataImpl &Record)
457    : Writer(Writer), Record(Record) { }
458
459#define ABSTRACT_TYPELOC(CLASS, PARENT)
460#define TYPELOC(CLASS, PARENT) \
461    void Visit##CLASS##TypeLoc(CLASS##TypeLoc TyLoc);
462#include "clang/AST/TypeLocNodes.def"
463
464  void VisitArrayTypeLoc(ArrayTypeLoc TyLoc);
465  void VisitFunctionTypeLoc(FunctionTypeLoc TyLoc);
466};
467
468} // end anonymous namespace
469
470void TypeLocWriter::VisitQualifiedTypeLoc(QualifiedTypeLoc TL) {
471  // nothing to do
472}
473void TypeLocWriter::VisitBuiltinTypeLoc(BuiltinTypeLoc TL) {
474  Writer.AddSourceLocation(TL.getBuiltinLoc(), Record);
475  if (TL.needsExtraLocalData()) {
476    Record.push_back(TL.getWrittenTypeSpec());
477    Record.push_back(TL.getWrittenSignSpec());
478    Record.push_back(TL.getWrittenWidthSpec());
479    Record.push_back(TL.hasModeAttr());
480  }
481}
482void TypeLocWriter::VisitComplexTypeLoc(ComplexTypeLoc TL) {
483  Writer.AddSourceLocation(TL.getNameLoc(), Record);
484}
485void TypeLocWriter::VisitPointerTypeLoc(PointerTypeLoc TL) {
486  Writer.AddSourceLocation(TL.getStarLoc(), Record);
487}
488void TypeLocWriter::VisitDecayedTypeLoc(DecayedTypeLoc TL) {
489  // nothing to do
490}
491void TypeLocWriter::VisitAdjustedTypeLoc(AdjustedTypeLoc TL) {
492  // nothing to do
493}
494void TypeLocWriter::VisitBlockPointerTypeLoc(BlockPointerTypeLoc TL) {
495  Writer.AddSourceLocation(TL.getCaretLoc(), Record);
496}
497void TypeLocWriter::VisitLValueReferenceTypeLoc(LValueReferenceTypeLoc TL) {
498  Writer.AddSourceLocation(TL.getAmpLoc(), Record);
499}
500void TypeLocWriter::VisitRValueReferenceTypeLoc(RValueReferenceTypeLoc TL) {
501  Writer.AddSourceLocation(TL.getAmpAmpLoc(), Record);
502}
503void TypeLocWriter::VisitMemberPointerTypeLoc(MemberPointerTypeLoc TL) {
504  Writer.AddSourceLocation(TL.getStarLoc(), Record);
505  Writer.AddTypeSourceInfo(TL.getClassTInfo(), Record);
506}
507void TypeLocWriter::VisitArrayTypeLoc(ArrayTypeLoc TL) {
508  Writer.AddSourceLocation(TL.getLBracketLoc(), Record);
509  Writer.AddSourceLocation(TL.getRBracketLoc(), Record);
510  Record.push_back(TL.getSizeExpr() ? 1 : 0);
511  if (TL.getSizeExpr())
512    Writer.AddStmt(TL.getSizeExpr());
513}
514void TypeLocWriter::VisitConstantArrayTypeLoc(ConstantArrayTypeLoc TL) {
515  VisitArrayTypeLoc(TL);
516}
517void TypeLocWriter::VisitIncompleteArrayTypeLoc(IncompleteArrayTypeLoc TL) {
518  VisitArrayTypeLoc(TL);
519}
520void TypeLocWriter::VisitVariableArrayTypeLoc(VariableArrayTypeLoc TL) {
521  VisitArrayTypeLoc(TL);
522}
523void TypeLocWriter::VisitDependentSizedArrayTypeLoc(
524                                            DependentSizedArrayTypeLoc TL) {
525  VisitArrayTypeLoc(TL);
526}
527void TypeLocWriter::VisitDependentSizedExtVectorTypeLoc(
528                                        DependentSizedExtVectorTypeLoc TL) {
529  Writer.AddSourceLocation(TL.getNameLoc(), Record);
530}
531void TypeLocWriter::VisitVectorTypeLoc(VectorTypeLoc TL) {
532  Writer.AddSourceLocation(TL.getNameLoc(), Record);
533}
534void TypeLocWriter::VisitExtVectorTypeLoc(ExtVectorTypeLoc TL) {
535  Writer.AddSourceLocation(TL.getNameLoc(), Record);
536}
537void TypeLocWriter::VisitFunctionTypeLoc(FunctionTypeLoc TL) {
538  Writer.AddSourceLocation(TL.getLocalRangeBegin(), Record);
539  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
540  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
541  Writer.AddSourceLocation(TL.getLocalRangeEnd(), Record);
542  for (unsigned i = 0, e = TL.getNumParams(); i != e; ++i)
543    Writer.AddDeclRef(TL.getParam(i), Record);
544}
545void TypeLocWriter::VisitFunctionProtoTypeLoc(FunctionProtoTypeLoc TL) {
546  VisitFunctionTypeLoc(TL);
547}
548void TypeLocWriter::VisitFunctionNoProtoTypeLoc(FunctionNoProtoTypeLoc TL) {
549  VisitFunctionTypeLoc(TL);
550}
551void TypeLocWriter::VisitUnresolvedUsingTypeLoc(UnresolvedUsingTypeLoc TL) {
552  Writer.AddSourceLocation(TL.getNameLoc(), Record);
553}
554void TypeLocWriter::VisitTypedefTypeLoc(TypedefTypeLoc TL) {
555  Writer.AddSourceLocation(TL.getNameLoc(), Record);
556}
557void TypeLocWriter::VisitTypeOfExprTypeLoc(TypeOfExprTypeLoc TL) {
558  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
559  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
560  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
561}
562void TypeLocWriter::VisitTypeOfTypeLoc(TypeOfTypeLoc TL) {
563  Writer.AddSourceLocation(TL.getTypeofLoc(), Record);
564  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
565  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
566  Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
567}
568void TypeLocWriter::VisitDecltypeTypeLoc(DecltypeTypeLoc TL) {
569  Writer.AddSourceLocation(TL.getNameLoc(), Record);
570}
571void TypeLocWriter::VisitUnaryTransformTypeLoc(UnaryTransformTypeLoc TL) {
572  Writer.AddSourceLocation(TL.getKWLoc(), Record);
573  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
574  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
575  Writer.AddTypeSourceInfo(TL.getUnderlyingTInfo(), Record);
576}
577void TypeLocWriter::VisitAutoTypeLoc(AutoTypeLoc TL) {
578  Writer.AddSourceLocation(TL.getNameLoc(), Record);
579}
580void TypeLocWriter::VisitRecordTypeLoc(RecordTypeLoc TL) {
581  Writer.AddSourceLocation(TL.getNameLoc(), Record);
582}
583void TypeLocWriter::VisitEnumTypeLoc(EnumTypeLoc TL) {
584  Writer.AddSourceLocation(TL.getNameLoc(), Record);
585}
586void TypeLocWriter::VisitAttributedTypeLoc(AttributedTypeLoc TL) {
587  Writer.AddSourceLocation(TL.getAttrNameLoc(), Record);
588  if (TL.hasAttrOperand()) {
589    SourceRange range = TL.getAttrOperandParensRange();
590    Writer.AddSourceLocation(range.getBegin(), Record);
591    Writer.AddSourceLocation(range.getEnd(), Record);
592  }
593  if (TL.hasAttrExprOperand()) {
594    Expr *operand = TL.getAttrExprOperand();
595    Record.push_back(operand ? 1 : 0);
596    if (operand) Writer.AddStmt(operand);
597  } else if (TL.hasAttrEnumOperand()) {
598    Writer.AddSourceLocation(TL.getAttrEnumOperandLoc(), Record);
599  }
600}
601void TypeLocWriter::VisitTemplateTypeParmTypeLoc(TemplateTypeParmTypeLoc TL) {
602  Writer.AddSourceLocation(TL.getNameLoc(), Record);
603}
604void TypeLocWriter::VisitSubstTemplateTypeParmTypeLoc(
605                                            SubstTemplateTypeParmTypeLoc TL) {
606  Writer.AddSourceLocation(TL.getNameLoc(), Record);
607}
608void TypeLocWriter::VisitSubstTemplateTypeParmPackTypeLoc(
609                                          SubstTemplateTypeParmPackTypeLoc TL) {
610  Writer.AddSourceLocation(TL.getNameLoc(), Record);
611}
612void TypeLocWriter::VisitTemplateSpecializationTypeLoc(
613                                           TemplateSpecializationTypeLoc TL) {
614  Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
615  Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
616  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
617  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
618  for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i)
619    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(),
620                                      TL.getArgLoc(i).getLocInfo(), Record);
621}
622void TypeLocWriter::VisitParenTypeLoc(ParenTypeLoc TL) {
623  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
624  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
625}
626void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) {
627  Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
628  Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
629}
630void TypeLocWriter::VisitInjectedClassNameTypeLoc(InjectedClassNameTypeLoc TL) {
631  Writer.AddSourceLocation(TL.getNameLoc(), Record);
632}
633void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) {
634  Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
635  Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
636  Writer.AddSourceLocation(TL.getNameLoc(), Record);
637}
638void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc(
639       DependentTemplateSpecializationTypeLoc TL) {
640  Writer.AddSourceLocation(TL.getElaboratedKeywordLoc(), Record);
641  Writer.AddNestedNameSpecifierLoc(TL.getQualifierLoc(), Record);
642  Writer.AddSourceLocation(TL.getTemplateKeywordLoc(), Record);
643  Writer.AddSourceLocation(TL.getTemplateNameLoc(), Record);
644  Writer.AddSourceLocation(TL.getLAngleLoc(), Record);
645  Writer.AddSourceLocation(TL.getRAngleLoc(), Record);
646  for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I)
647    Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(),
648                                      TL.getArgLoc(I).getLocInfo(), Record);
649}
650void TypeLocWriter::VisitPackExpansionTypeLoc(PackExpansionTypeLoc TL) {
651  Writer.AddSourceLocation(TL.getEllipsisLoc(), Record);
652}
653void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) {
654  Writer.AddSourceLocation(TL.getNameLoc(), Record);
655}
656void TypeLocWriter::VisitObjCObjectTypeLoc(ObjCObjectTypeLoc TL) {
657  Record.push_back(TL.hasBaseTypeAsWritten());
658  Writer.AddSourceLocation(TL.getTypeArgsLAngleLoc(), Record);
659  Writer.AddSourceLocation(TL.getTypeArgsRAngleLoc(), Record);
660  for (unsigned i = 0, e = TL.getNumTypeArgs(); i != e; ++i)
661    Writer.AddTypeSourceInfo(TL.getTypeArgTInfo(i), Record);
662  Writer.AddSourceLocation(TL.getProtocolLAngleLoc(), Record);
663  Writer.AddSourceLocation(TL.getProtocolRAngleLoc(), Record);
664  for (unsigned i = 0, e = TL.getNumProtocols(); i != e; ++i)
665    Writer.AddSourceLocation(TL.getProtocolLoc(i), Record);
666}
667void TypeLocWriter::VisitObjCObjectPointerTypeLoc(ObjCObjectPointerTypeLoc TL) {
668  Writer.AddSourceLocation(TL.getStarLoc(), Record);
669}
670void TypeLocWriter::VisitAtomicTypeLoc(AtomicTypeLoc TL) {
671  Writer.AddSourceLocation(TL.getKWLoc(), Record);
672  Writer.AddSourceLocation(TL.getLParenLoc(), Record);
673  Writer.AddSourceLocation(TL.getRParenLoc(), Record);
674}
675
676void ASTWriter::WriteTypeAbbrevs() {
677  using namespace llvm;
678
679  BitCodeAbbrev *Abv;
680
681  // Abbreviation for TYPE_EXT_QUAL
682  Abv = new BitCodeAbbrev();
683  Abv->Add(BitCodeAbbrevOp(serialization::TYPE_EXT_QUAL));
684  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Type
685  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 3));   // Quals
686  TypeExtQualAbbrev = Stream.EmitAbbrev(Abv);
687
688  // Abbreviation for TYPE_FUNCTION_PROTO
689  Abv = new BitCodeAbbrev();
690  Abv->Add(BitCodeAbbrevOp(serialization::TYPE_FUNCTION_PROTO));
691  // FunctionType
692  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // ReturnType
693  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // NoReturn
694  Abv->Add(BitCodeAbbrevOp(0));                         // HasRegParm
695  Abv->Add(BitCodeAbbrevOp(0));                         // RegParm
696  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 4)); // CC
697  Abv->Add(BitCodeAbbrevOp(0));                         // ProducesResult
698  // FunctionProtoType
699  Abv->Add(BitCodeAbbrevOp(0));                         // IsVariadic
700  Abv->Add(BitCodeAbbrevOp(0));                         // HasTrailingReturn
701  Abv->Add(BitCodeAbbrevOp(0));                         // TypeQuals
702  Abv->Add(BitCodeAbbrevOp(0));                         // RefQualifier
703  Abv->Add(BitCodeAbbrevOp(EST_None));                  // ExceptionSpec
704  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // NumParams
705  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Array));
706  Abv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));   // Params
707  TypeFunctionProtoAbbrev = Stream.EmitAbbrev(Abv);
708}
709
710//===----------------------------------------------------------------------===//
711// ASTWriter Implementation
712//===----------------------------------------------------------------------===//
713
714static void EmitBlockID(unsigned ID, const char *Name,
715                        llvm::BitstreamWriter &Stream,
716                        ASTWriter::RecordDataImpl &Record) {
717  Record.clear();
718  Record.push_back(ID);
719  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETBID, Record);
720
721  // Emit the block name if present.
722  if (!Name || Name[0] == 0)
723    return;
724  Record.clear();
725  while (*Name)
726    Record.push_back(*Name++);
727  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_BLOCKNAME, Record);
728}
729
730static void EmitRecordID(unsigned ID, const char *Name,
731                         llvm::BitstreamWriter &Stream,
732                         ASTWriter::RecordDataImpl &Record) {
733  Record.clear();
734  Record.push_back(ID);
735  while (*Name)
736    Record.push_back(*Name++);
737  Stream.EmitRecord(llvm::bitc::BLOCKINFO_CODE_SETRECORDNAME, Record);
738}
739
740static void AddStmtsExprs(llvm::BitstreamWriter &Stream,
741                          ASTWriter::RecordDataImpl &Record) {
742#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
743  RECORD(STMT_STOP);
744  RECORD(STMT_NULL_PTR);
745  RECORD(STMT_REF_PTR);
746  RECORD(STMT_NULL);
747  RECORD(STMT_COMPOUND);
748  RECORD(STMT_CASE);
749  RECORD(STMT_DEFAULT);
750  RECORD(STMT_LABEL);
751  RECORD(STMT_ATTRIBUTED);
752  RECORD(STMT_IF);
753  RECORD(STMT_SWITCH);
754  RECORD(STMT_WHILE);
755  RECORD(STMT_DO);
756  RECORD(STMT_FOR);
757  RECORD(STMT_GOTO);
758  RECORD(STMT_INDIRECT_GOTO);
759  RECORD(STMT_CONTINUE);
760  RECORD(STMT_BREAK);
761  RECORD(STMT_RETURN);
762  RECORD(STMT_DECL);
763  RECORD(STMT_GCCASM);
764  RECORD(STMT_MSASM);
765  RECORD(EXPR_PREDEFINED);
766  RECORD(EXPR_DECL_REF);
767  RECORD(EXPR_INTEGER_LITERAL);
768  RECORD(EXPR_FLOATING_LITERAL);
769  RECORD(EXPR_IMAGINARY_LITERAL);
770  RECORD(EXPR_STRING_LITERAL);
771  RECORD(EXPR_CHARACTER_LITERAL);
772  RECORD(EXPR_PAREN);
773  RECORD(EXPR_PAREN_LIST);
774  RECORD(EXPR_UNARY_OPERATOR);
775  RECORD(EXPR_SIZEOF_ALIGN_OF);
776  RECORD(EXPR_ARRAY_SUBSCRIPT);
777  RECORD(EXPR_CALL);
778  RECORD(EXPR_MEMBER);
779  RECORD(EXPR_BINARY_OPERATOR);
780  RECORD(EXPR_COMPOUND_ASSIGN_OPERATOR);
781  RECORD(EXPR_CONDITIONAL_OPERATOR);
782  RECORD(EXPR_IMPLICIT_CAST);
783  RECORD(EXPR_CSTYLE_CAST);
784  RECORD(EXPR_COMPOUND_LITERAL);
785  RECORD(EXPR_EXT_VECTOR_ELEMENT);
786  RECORD(EXPR_INIT_LIST);
787  RECORD(EXPR_DESIGNATED_INIT);
788  RECORD(EXPR_DESIGNATED_INIT_UPDATE);
789  RECORD(EXPR_IMPLICIT_VALUE_INIT);
790  RECORD(EXPR_NO_INIT);
791  RECORD(EXPR_VA_ARG);
792  RECORD(EXPR_ADDR_LABEL);
793  RECORD(EXPR_STMT);
794  RECORD(EXPR_CHOOSE);
795  RECORD(EXPR_GNU_NULL);
796  RECORD(EXPR_SHUFFLE_VECTOR);
797  RECORD(EXPR_BLOCK);
798  RECORD(EXPR_GENERIC_SELECTION);
799  RECORD(EXPR_OBJC_STRING_LITERAL);
800  RECORD(EXPR_OBJC_BOXED_EXPRESSION);
801  RECORD(EXPR_OBJC_ARRAY_LITERAL);
802  RECORD(EXPR_OBJC_DICTIONARY_LITERAL);
803  RECORD(EXPR_OBJC_ENCODE);
804  RECORD(EXPR_OBJC_SELECTOR_EXPR);
805  RECORD(EXPR_OBJC_PROTOCOL_EXPR);
806  RECORD(EXPR_OBJC_IVAR_REF_EXPR);
807  RECORD(EXPR_OBJC_PROPERTY_REF_EXPR);
808  RECORD(EXPR_OBJC_KVC_REF_EXPR);
809  RECORD(EXPR_OBJC_MESSAGE_EXPR);
810  RECORD(STMT_OBJC_FOR_COLLECTION);
811  RECORD(STMT_OBJC_CATCH);
812  RECORD(STMT_OBJC_FINALLY);
813  RECORD(STMT_OBJC_AT_TRY);
814  RECORD(STMT_OBJC_AT_SYNCHRONIZED);
815  RECORD(STMT_OBJC_AT_THROW);
816  RECORD(EXPR_OBJC_BOOL_LITERAL);
817  RECORD(STMT_CXX_CATCH);
818  RECORD(STMT_CXX_TRY);
819  RECORD(STMT_CXX_FOR_RANGE);
820  RECORD(EXPR_CXX_OPERATOR_CALL);
821  RECORD(EXPR_CXX_MEMBER_CALL);
822  RECORD(EXPR_CXX_CONSTRUCT);
823  RECORD(EXPR_CXX_TEMPORARY_OBJECT);
824  RECORD(EXPR_CXX_STATIC_CAST);
825  RECORD(EXPR_CXX_DYNAMIC_CAST);
826  RECORD(EXPR_CXX_REINTERPRET_CAST);
827  RECORD(EXPR_CXX_CONST_CAST);
828  RECORD(EXPR_CXX_FUNCTIONAL_CAST);
829  RECORD(EXPR_USER_DEFINED_LITERAL);
830  RECORD(EXPR_CXX_STD_INITIALIZER_LIST);
831  RECORD(EXPR_CXX_BOOL_LITERAL);
832  RECORD(EXPR_CXX_NULL_PTR_LITERAL);
833  RECORD(EXPR_CXX_TYPEID_EXPR);
834  RECORD(EXPR_CXX_TYPEID_TYPE);
835  RECORD(EXPR_CXX_THIS);
836  RECORD(EXPR_CXX_THROW);
837  RECORD(EXPR_CXX_DEFAULT_ARG);
838  RECORD(EXPR_CXX_DEFAULT_INIT);
839  RECORD(EXPR_CXX_BIND_TEMPORARY);
840  RECORD(EXPR_CXX_SCALAR_VALUE_INIT);
841  RECORD(EXPR_CXX_NEW);
842  RECORD(EXPR_CXX_DELETE);
843  RECORD(EXPR_CXX_PSEUDO_DESTRUCTOR);
844  RECORD(EXPR_EXPR_WITH_CLEANUPS);
845  RECORD(EXPR_CXX_DEPENDENT_SCOPE_MEMBER);
846  RECORD(EXPR_CXX_DEPENDENT_SCOPE_DECL_REF);
847  RECORD(EXPR_CXX_UNRESOLVED_CONSTRUCT);
848  RECORD(EXPR_CXX_UNRESOLVED_MEMBER);
849  RECORD(EXPR_CXX_UNRESOLVED_LOOKUP);
850  RECORD(EXPR_CXX_EXPRESSION_TRAIT);
851  RECORD(EXPR_CXX_NOEXCEPT);
852  RECORD(EXPR_OPAQUE_VALUE);
853  RECORD(EXPR_BINARY_CONDITIONAL_OPERATOR);
854  RECORD(EXPR_TYPE_TRAIT);
855  RECORD(EXPR_ARRAY_TYPE_TRAIT);
856  RECORD(EXPR_PACK_EXPANSION);
857  RECORD(EXPR_SIZEOF_PACK);
858  RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM);
859  RECORD(EXPR_SUBST_NON_TYPE_TEMPLATE_PARM_PACK);
860  RECORD(EXPR_FUNCTION_PARM_PACK);
861  RECORD(EXPR_MATERIALIZE_TEMPORARY);
862  RECORD(EXPR_CUDA_KERNEL_CALL);
863  RECORD(EXPR_CXX_UUIDOF_EXPR);
864  RECORD(EXPR_CXX_UUIDOF_TYPE);
865  RECORD(EXPR_LAMBDA);
866#undef RECORD
867}
868
869void ASTWriter::WriteBlockInfoBlock() {
870  RecordData Record;
871  Stream.EnterSubblock(llvm::bitc::BLOCKINFO_BLOCK_ID, 3);
872
873#define BLOCK(X) EmitBlockID(X ## _ID, #X, Stream, Record)
874#define RECORD(X) EmitRecordID(X, #X, Stream, Record)
875
876  // Control Block.
877  BLOCK(CONTROL_BLOCK);
878  RECORD(METADATA);
879  RECORD(SIGNATURE);
880  RECORD(MODULE_NAME);
881  RECORD(MODULE_DIRECTORY);
882  RECORD(MODULE_MAP_FILE);
883  RECORD(IMPORTS);
884  RECORD(ORIGINAL_FILE);
885  RECORD(ORIGINAL_PCH_DIR);
886  RECORD(ORIGINAL_FILE_ID);
887  RECORD(INPUT_FILE_OFFSETS);
888
889  BLOCK(OPTIONS_BLOCK);
890  RECORD(LANGUAGE_OPTIONS);
891  RECORD(TARGET_OPTIONS);
892  RECORD(DIAGNOSTIC_OPTIONS);
893  RECORD(FILE_SYSTEM_OPTIONS);
894  RECORD(HEADER_SEARCH_OPTIONS);
895  RECORD(PREPROCESSOR_OPTIONS);
896
897  BLOCK(INPUT_FILES_BLOCK);
898  RECORD(INPUT_FILE);
899
900  // AST Top-Level Block.
901  BLOCK(AST_BLOCK);
902  RECORD(TYPE_OFFSET);
903  RECORD(DECL_OFFSET);
904  RECORD(IDENTIFIER_OFFSET);
905  RECORD(IDENTIFIER_TABLE);
906  RECORD(EAGERLY_DESERIALIZED_DECLS);
907  RECORD(SPECIAL_TYPES);
908  RECORD(STATISTICS);
909  RECORD(TENTATIVE_DEFINITIONS);
910  RECORD(SELECTOR_OFFSETS);
911  RECORD(METHOD_POOL);
912  RECORD(PP_COUNTER_VALUE);
913  RECORD(SOURCE_LOCATION_OFFSETS);
914  RECORD(SOURCE_LOCATION_PRELOADS);
915  RECORD(EXT_VECTOR_DECLS);
916  RECORD(UNUSED_FILESCOPED_DECLS);
917  RECORD(PPD_ENTITIES_OFFSETS);
918  RECORD(VTABLE_USES);
919  RECORD(REFERENCED_SELECTOR_POOL);
920  RECORD(TU_UPDATE_LEXICAL);
921  RECORD(SEMA_DECL_REFS);
922  RECORD(WEAK_UNDECLARED_IDENTIFIERS);
923  RECORD(PENDING_IMPLICIT_INSTANTIATIONS);
924  RECORD(DECL_REPLACEMENTS);
925  RECORD(UPDATE_VISIBLE);
926  RECORD(DECL_UPDATE_OFFSETS);
927  RECORD(DECL_UPDATES);
928  RECORD(CXX_BASE_SPECIFIER_OFFSETS);
929  RECORD(DIAG_PRAGMA_MAPPINGS);
930  RECORD(CUDA_SPECIAL_DECL_REFS);
931  RECORD(HEADER_SEARCH_TABLE);
932  RECORD(FP_PRAGMA_OPTIONS);
933  RECORD(OPENCL_EXTENSIONS);
934  RECORD(DELEGATING_CTORS);
935  RECORD(KNOWN_NAMESPACES);
936  RECORD(MODULE_OFFSET_MAP);
937  RECORD(SOURCE_MANAGER_LINE_TABLE);
938  RECORD(OBJC_CATEGORIES_MAP);
939  RECORD(FILE_SORTED_DECLS);
940  RECORD(IMPORTED_MODULES);
941  RECORD(OBJC_CATEGORIES);
942  RECORD(MACRO_OFFSET);
943  RECORD(INTERESTING_IDENTIFIERS);
944  RECORD(UNDEFINED_BUT_USED);
945  RECORD(LATE_PARSED_TEMPLATE);
946  RECORD(OPTIMIZE_PRAGMA_OPTIONS);
947  RECORD(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES);
948  RECORD(CXX_CTOR_INITIALIZERS_OFFSETS);
949  RECORD(DELETE_EXPRS_TO_ANALYZE);
950
951  // SourceManager Block.
952  BLOCK(SOURCE_MANAGER_BLOCK);
953  RECORD(SM_SLOC_FILE_ENTRY);
954  RECORD(SM_SLOC_BUFFER_ENTRY);
955  RECORD(SM_SLOC_BUFFER_BLOB);
956  RECORD(SM_SLOC_EXPANSION_ENTRY);
957
958  // Preprocessor Block.
959  BLOCK(PREPROCESSOR_BLOCK);
960  RECORD(PP_MACRO_DIRECTIVE_HISTORY);
961  RECORD(PP_MACRO_FUNCTION_LIKE);
962  RECORD(PP_MACRO_OBJECT_LIKE);
963  RECORD(PP_MODULE_MACRO);
964  RECORD(PP_TOKEN);
965
966  // Submodule Block.
967  BLOCK(SUBMODULE_BLOCK);
968  RECORD(SUBMODULE_METADATA);
969  RECORD(SUBMODULE_DEFINITION);
970  RECORD(SUBMODULE_UMBRELLA_HEADER);
971  RECORD(SUBMODULE_HEADER);
972  RECORD(SUBMODULE_TOPHEADER);
973  RECORD(SUBMODULE_UMBRELLA_DIR);
974  RECORD(SUBMODULE_IMPORTS);
975  RECORD(SUBMODULE_EXPORTS);
976  RECORD(SUBMODULE_REQUIRES);
977  RECORD(SUBMODULE_EXCLUDED_HEADER);
978  RECORD(SUBMODULE_LINK_LIBRARY);
979  RECORD(SUBMODULE_CONFIG_MACRO);
980  RECORD(SUBMODULE_CONFLICT);
981  RECORD(SUBMODULE_PRIVATE_HEADER);
982  RECORD(SUBMODULE_TEXTUAL_HEADER);
983  RECORD(SUBMODULE_PRIVATE_TEXTUAL_HEADER);
984
985  // Comments Block.
986  BLOCK(COMMENTS_BLOCK);
987  RECORD(COMMENTS_RAW_COMMENT);
988
989  // Decls and Types block.
990  BLOCK(DECLTYPES_BLOCK);
991  RECORD(TYPE_EXT_QUAL);
992  RECORD(TYPE_COMPLEX);
993  RECORD(TYPE_POINTER);
994  RECORD(TYPE_BLOCK_POINTER);
995  RECORD(TYPE_LVALUE_REFERENCE);
996  RECORD(TYPE_RVALUE_REFERENCE);
997  RECORD(TYPE_MEMBER_POINTER);
998  RECORD(TYPE_CONSTANT_ARRAY);
999  RECORD(TYPE_INCOMPLETE_ARRAY);
1000  RECORD(TYPE_VARIABLE_ARRAY);
1001  RECORD(TYPE_VECTOR);
1002  RECORD(TYPE_EXT_VECTOR);
1003  RECORD(TYPE_FUNCTION_NO_PROTO);
1004  RECORD(TYPE_FUNCTION_PROTO);
1005  RECORD(TYPE_TYPEDEF);
1006  RECORD(TYPE_TYPEOF_EXPR);
1007  RECORD(TYPE_TYPEOF);
1008  RECORD(TYPE_RECORD);
1009  RECORD(TYPE_ENUM);
1010  RECORD(TYPE_OBJC_INTERFACE);
1011  RECORD(TYPE_OBJC_OBJECT_POINTER);
1012  RECORD(TYPE_DECLTYPE);
1013  RECORD(TYPE_ELABORATED);
1014  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM);
1015  RECORD(TYPE_UNRESOLVED_USING);
1016  RECORD(TYPE_INJECTED_CLASS_NAME);
1017  RECORD(TYPE_OBJC_OBJECT);
1018  RECORD(TYPE_TEMPLATE_TYPE_PARM);
1019  RECORD(TYPE_TEMPLATE_SPECIALIZATION);
1020  RECORD(TYPE_DEPENDENT_NAME);
1021  RECORD(TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION);
1022  RECORD(TYPE_DEPENDENT_SIZED_ARRAY);
1023  RECORD(TYPE_PAREN);
1024  RECORD(TYPE_PACK_EXPANSION);
1025  RECORD(TYPE_ATTRIBUTED);
1026  RECORD(TYPE_SUBST_TEMPLATE_TYPE_PARM_PACK);
1027  RECORD(TYPE_AUTO);
1028  RECORD(TYPE_UNARY_TRANSFORM);
1029  RECORD(TYPE_ATOMIC);
1030  RECORD(TYPE_DECAYED);
1031  RECORD(TYPE_ADJUSTED);
1032  RECORD(LOCAL_REDECLARATIONS);
1033  RECORD(DECL_TYPEDEF);
1034  RECORD(DECL_TYPEALIAS);
1035  RECORD(DECL_ENUM);
1036  RECORD(DECL_RECORD);
1037  RECORD(DECL_ENUM_CONSTANT);
1038  RECORD(DECL_FUNCTION);
1039  RECORD(DECL_OBJC_METHOD);
1040  RECORD(DECL_OBJC_INTERFACE);
1041  RECORD(DECL_OBJC_PROTOCOL);
1042  RECORD(DECL_OBJC_IVAR);
1043  RECORD(DECL_OBJC_AT_DEFS_FIELD);
1044  RECORD(DECL_OBJC_CATEGORY);
1045  RECORD(DECL_OBJC_CATEGORY_IMPL);
1046  RECORD(DECL_OBJC_IMPLEMENTATION);
1047  RECORD(DECL_OBJC_COMPATIBLE_ALIAS);
1048  RECORD(DECL_OBJC_PROPERTY);
1049  RECORD(DECL_OBJC_PROPERTY_IMPL);
1050  RECORD(DECL_FIELD);
1051  RECORD(DECL_MS_PROPERTY);
1052  RECORD(DECL_VAR);
1053  RECORD(DECL_IMPLICIT_PARAM);
1054  RECORD(DECL_PARM_VAR);
1055  RECORD(DECL_FILE_SCOPE_ASM);
1056  RECORD(DECL_BLOCK);
1057  RECORD(DECL_CONTEXT_LEXICAL);
1058  RECORD(DECL_CONTEXT_VISIBLE);
1059  RECORD(DECL_NAMESPACE);
1060  RECORD(DECL_NAMESPACE_ALIAS);
1061  RECORD(DECL_USING);
1062  RECORD(DECL_USING_SHADOW);
1063  RECORD(DECL_USING_DIRECTIVE);
1064  RECORD(DECL_UNRESOLVED_USING_VALUE);
1065  RECORD(DECL_UNRESOLVED_USING_TYPENAME);
1066  RECORD(DECL_LINKAGE_SPEC);
1067  RECORD(DECL_CXX_RECORD);
1068  RECORD(DECL_CXX_METHOD);
1069  RECORD(DECL_CXX_CONSTRUCTOR);
1070  RECORD(DECL_CXX_DESTRUCTOR);
1071  RECORD(DECL_CXX_CONVERSION);
1072  RECORD(DECL_ACCESS_SPEC);
1073  RECORD(DECL_FRIEND);
1074  RECORD(DECL_FRIEND_TEMPLATE);
1075  RECORD(DECL_CLASS_TEMPLATE);
1076  RECORD(DECL_CLASS_TEMPLATE_SPECIALIZATION);
1077  RECORD(DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION);
1078  RECORD(DECL_VAR_TEMPLATE);
1079  RECORD(DECL_VAR_TEMPLATE_SPECIALIZATION);
1080  RECORD(DECL_VAR_TEMPLATE_PARTIAL_SPECIALIZATION);
1081  RECORD(DECL_FUNCTION_TEMPLATE);
1082  RECORD(DECL_TEMPLATE_TYPE_PARM);
1083  RECORD(DECL_NON_TYPE_TEMPLATE_PARM);
1084  RECORD(DECL_TEMPLATE_TEMPLATE_PARM);
1085  RECORD(DECL_STATIC_ASSERT);
1086  RECORD(DECL_CXX_BASE_SPECIFIERS);
1087  RECORD(DECL_INDIRECTFIELD);
1088  RECORD(DECL_EXPANDED_NON_TYPE_TEMPLATE_PARM_PACK);
1089
1090  // Statements and Exprs can occur in the Decls and Types block.
1091  AddStmtsExprs(Stream, Record);
1092
1093  BLOCK(PREPROCESSOR_DETAIL_BLOCK);
1094  RECORD(PPD_MACRO_EXPANSION);
1095  RECORD(PPD_MACRO_DEFINITION);
1096  RECORD(PPD_INCLUSION_DIRECTIVE);
1097
1098  // Decls and Types block.
1099  BLOCK(EXTENSION_BLOCK);
1100  RECORD(EXTENSION_METADATA);
1101
1102#undef RECORD
1103#undef BLOCK
1104  Stream.ExitBlock();
1105}
1106
1107/// \brief Prepares a path for being written to an AST file by converting it
1108/// to an absolute path and removing nested './'s.
1109///
1110/// \return \c true if the path was changed.
1111static bool cleanPathForOutput(FileManager &FileMgr,
1112                               SmallVectorImpl<char> &Path) {
1113  bool Changed = FileMgr.makeAbsolutePath(Path);
1114  return Changed | llvm::sys::path::remove_dots(Path);
1115}
1116
1117/// \brief Adjusts the given filename to only write out the portion of the
1118/// filename that is not part of the system root directory.
1119///
1120/// \param Filename the file name to adjust.
1121///
1122/// \param BaseDir When non-NULL, the PCH file is a relocatable AST file and
1123/// the returned filename will be adjusted by this root directory.
1124///
1125/// \returns either the original filename (if it needs no adjustment) or the
1126/// adjusted filename (which points into the @p Filename parameter).
1127static const char *
1128adjustFilenameForRelocatableAST(const char *Filename, StringRef BaseDir) {
1129  assert(Filename && "No file name to adjust?");
1130
1131  if (BaseDir.empty())
1132    return Filename;
1133
1134  // Verify that the filename and the system root have the same prefix.
1135  unsigned Pos = 0;
1136  for (; Filename[Pos] && Pos < BaseDir.size(); ++Pos)
1137    if (Filename[Pos] != BaseDir[Pos])
1138      return Filename; // Prefixes don't match.
1139
1140  // We hit the end of the filename before we hit the end of the system root.
1141  if (!Filename[Pos])
1142    return Filename;
1143
1144  // If there's not a path separator at the end of the base directory nor
1145  // immediately after it, then this isn't within the base directory.
1146  if (!llvm::sys::path::is_separator(Filename[Pos])) {
1147    if (!llvm::sys::path::is_separator(BaseDir.back()))
1148      return Filename;
1149  } else {
1150    // If the file name has a '/' at the current position, skip over the '/'.
1151    // We distinguish relative paths from absolute paths by the
1152    // absence of '/' at the beginning of relative paths.
1153    //
1154    // FIXME: This is wrong. We distinguish them by asking if the path is
1155    // absolute, which isn't the same thing. And there might be multiple '/'s
1156    // in a row. Use a better mechanism to indicate whether we have emitted an
1157    // absolute or relative path.
1158    ++Pos;
1159  }
1160
1161  return Filename + Pos;
1162}
1163
1164static ASTFileSignature getSignature() {
1165  while (1) {
1166    if (ASTFileSignature S = llvm::sys::Process::GetRandomNumber())
1167      return S;
1168    // Rely on GetRandomNumber to eventually return non-zero...
1169  }
1170}
1171
1172/// \brief Write the control block.
1173uint64_t ASTWriter::WriteControlBlock(Preprocessor &PP,
1174                                      ASTContext &Context,
1175                                      StringRef isysroot,
1176                                      const std::string &OutputFile) {
1177  ASTFileSignature Signature = 0;
1178
1179  using namespace llvm;
1180  Stream.EnterSubblock(CONTROL_BLOCK_ID, 5);
1181  RecordData Record;
1182
1183  // Metadata
1184  auto *MetadataAbbrev = new BitCodeAbbrev();
1185  MetadataAbbrev->Add(BitCodeAbbrevOp(METADATA));
1186  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Major
1187  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Minor
1188  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang maj.
1189  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang min.
1190  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable
1191  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Timestamps
1192  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Errors
1193  MetadataAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // SVN branch/tag
1194  unsigned MetadataAbbrevCode = Stream.EmitAbbrev(MetadataAbbrev);
1195  assert((!WritingModule || isysroot.empty()) &&
1196         "writing module as a relocatable PCH?");
1197  {
1198    RecordData::value_type Record[] = {METADATA, VERSION_MAJOR, VERSION_MINOR,
1199                                       CLANG_VERSION_MAJOR, CLANG_VERSION_MINOR,
1200                                       !isysroot.empty(), IncludeTimestamps,
1201                                       ASTHasCompilerErrors};
1202    Stream.EmitRecordWithBlob(MetadataAbbrevCode, Record,
1203                              getClangFullRepositoryVersion());
1204  }
1205  if (WritingModule) {
1206    // For implicit modules we output a signature that we can use to ensure
1207    // duplicate module builds don't collide in the cache as their output order
1208    // is non-deterministic.
1209    // FIXME: Remove this when output is deterministic.
1210    if (Context.getLangOpts().ImplicitModules) {
1211      Signature = getSignature();
1212      RecordData::value_type Record[] = {Signature};
1213      Stream.EmitRecord(SIGNATURE, Record);
1214    }
1215
1216    // Module name
1217    auto *Abbrev = new BitCodeAbbrev();
1218    Abbrev->Add(BitCodeAbbrevOp(MODULE_NAME));
1219    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
1220    unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1221    RecordData::value_type Record[] = {MODULE_NAME};
1222    Stream.EmitRecordWithBlob(AbbrevCode, Record, WritingModule->Name);
1223  }
1224
1225  if (WritingModule && WritingModule->Directory) {
1226    SmallString<128> BaseDir(WritingModule->Directory->getName());
1227    cleanPathForOutput(Context.getSourceManager().getFileManager(), BaseDir);
1228
1229    // If the home of the module is the current working directory, then we
1230    // want to pick up the cwd of the build process loading the module, not
1231    // our cwd, when we load this module.
1232    if (!PP.getHeaderSearchInfo()
1233             .getHeaderSearchOpts()
1234             .ModuleMapFileHomeIsCwd ||
1235        WritingModule->Directory->getName() != StringRef(".")) {
1236      // Module directory.
1237      auto *Abbrev = new BitCodeAbbrev();
1238      Abbrev->Add(BitCodeAbbrevOp(MODULE_DIRECTORY));
1239      Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Directory
1240      unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1241
1242      RecordData::value_type Record[] = {MODULE_DIRECTORY};
1243      Stream.EmitRecordWithBlob(AbbrevCode, Record, BaseDir);
1244    }
1245
1246    // Write out all other paths relative to the base directory if possible.
1247    BaseDirectory.assign(BaseDir.begin(), BaseDir.end());
1248  } else if (!isysroot.empty()) {
1249    // Write out paths relative to the sysroot if possible.
1250    BaseDirectory = isysroot;
1251  }
1252
1253  // Module map file
1254  if (WritingModule) {
1255    Record.clear();
1256
1257    auto &Map = PP.getHeaderSearchInfo().getModuleMap();
1258
1259    // Primary module map file.
1260    AddPath(Map.getModuleMapFileForUniquing(WritingModule)->getName(), Record);
1261
1262    // Additional module map files.
1263    if (auto *AdditionalModMaps =
1264            Map.getAdditionalModuleMapFiles(WritingModule)) {
1265      Record.push_back(AdditionalModMaps->size());
1266      for (const FileEntry *F : *AdditionalModMaps)
1267        AddPath(F->getName(), Record);
1268    } else {
1269      Record.push_back(0);
1270    }
1271
1272    Stream.EmitRecord(MODULE_MAP_FILE, Record);
1273  }
1274
1275  // Imports
1276  if (Chain) {
1277    serialization::ModuleManager &Mgr = Chain->getModuleManager();
1278    Record.clear();
1279
1280    for (auto *M : Mgr) {
1281      // Skip modules that weren't directly imported.
1282      if (!M->isDirectlyImported())
1283        continue;
1284
1285      Record.push_back((unsigned)M->Kind); // FIXME: Stable encoding
1286      AddSourceLocation(M->ImportLoc, Record);
1287      Record.push_back(M->File->getSize());
1288      Record.push_back(getTimestampForOutput(M->File));
1289      Record.push_back(M->Signature);
1290      AddPath(M->FileName, Record);
1291    }
1292    Stream.EmitRecord(IMPORTS, Record);
1293  }
1294
1295  // Write the options block.
1296  Stream.EnterSubblock(OPTIONS_BLOCK_ID, 4);
1297
1298  // Language options.
1299  Record.clear();
1300  const LangOptions &LangOpts = Context.getLangOpts();
1301#define LANGOPT(Name, Bits, Default, Description) \
1302  Record.push_back(LangOpts.Name);
1303#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
1304  Record.push_back(static_cast<unsigned>(LangOpts.get##Name()));
1305#include "clang/Basic/LangOptions.def"
1306#define SANITIZER(NAME, ID)                                                    \
1307  Record.push_back(LangOpts.Sanitize.has(SanitizerKind::ID));
1308#include "clang/Basic/Sanitizers.def"
1309
1310  Record.push_back(LangOpts.ModuleFeatures.size());
1311  for (StringRef Feature : LangOpts.ModuleFeatures)
1312    AddString(Feature, Record);
1313
1314  Record.push_back((unsigned) LangOpts.ObjCRuntime.getKind());
1315  AddVersionTuple(LangOpts.ObjCRuntime.getVersion(), Record);
1316
1317  AddString(LangOpts.CurrentModule, Record);
1318
1319  // Comment options.
1320  Record.push_back(LangOpts.CommentOpts.BlockCommandNames.size());
1321  for (const auto &I : LangOpts.CommentOpts.BlockCommandNames) {
1322    AddString(I, Record);
1323  }
1324  Record.push_back(LangOpts.CommentOpts.ParseAllComments);
1325
1326  Stream.EmitRecord(LANGUAGE_OPTIONS, Record);
1327
1328  // Target options.
1329  Record.clear();
1330  const TargetInfo &Target = Context.getTargetInfo();
1331  const TargetOptions &TargetOpts = Target.getTargetOpts();
1332  AddString(TargetOpts.Triple, Record);
1333  AddString(TargetOpts.CPU, Record);
1334  AddString(TargetOpts.ABI, Record);
1335  Record.push_back(TargetOpts.FeaturesAsWritten.size());
1336  for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size(); I != N; ++I) {
1337    AddString(TargetOpts.FeaturesAsWritten[I], Record);
1338  }
1339  Record.push_back(TargetOpts.Features.size());
1340  for (unsigned I = 0, N = TargetOpts.Features.size(); I != N; ++I) {
1341    AddString(TargetOpts.Features[I], Record);
1342  }
1343  Stream.EmitRecord(TARGET_OPTIONS, Record);
1344
1345  // Diagnostic options.
1346  Record.clear();
1347  const DiagnosticOptions &DiagOpts
1348    = Context.getDiagnostics().getDiagnosticOptions();
1349#define DIAGOPT(Name, Bits, Default) Record.push_back(DiagOpts.Name);
1350#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
1351  Record.push_back(static_cast<unsigned>(DiagOpts.get##Name()));
1352#include "clang/Basic/DiagnosticOptions.def"
1353  Record.push_back(DiagOpts.Warnings.size());
1354  for (unsigned I = 0, N = DiagOpts.Warnings.size(); I != N; ++I)
1355    AddString(DiagOpts.Warnings[I], Record);
1356  Record.push_back(DiagOpts.Remarks.size());
1357  for (unsigned I = 0, N = DiagOpts.Remarks.size(); I != N; ++I)
1358    AddString(DiagOpts.Remarks[I], Record);
1359  // Note: we don't serialize the log or serialization file names, because they
1360  // are generally transient files and will almost always be overridden.
1361  Stream.EmitRecord(DIAGNOSTIC_OPTIONS, Record);
1362
1363  // File system options.
1364  Record.clear();
1365  const FileSystemOptions &FSOpts =
1366      Context.getSourceManager().getFileManager().getFileSystemOpts();
1367  AddString(FSOpts.WorkingDir, Record);
1368  Stream.EmitRecord(FILE_SYSTEM_OPTIONS, Record);
1369
1370  // Header search options.
1371  Record.clear();
1372  const HeaderSearchOptions &HSOpts
1373    = PP.getHeaderSearchInfo().getHeaderSearchOpts();
1374  AddString(HSOpts.Sysroot, Record);
1375
1376  // Include entries.
1377  Record.push_back(HSOpts.UserEntries.size());
1378  for (unsigned I = 0, N = HSOpts.UserEntries.size(); I != N; ++I) {
1379    const HeaderSearchOptions::Entry &Entry = HSOpts.UserEntries[I];
1380    AddString(Entry.Path, Record);
1381    Record.push_back(static_cast<unsigned>(Entry.Group));
1382    Record.push_back(Entry.IsFramework);
1383    Record.push_back(Entry.IgnoreSysRoot);
1384  }
1385
1386  // System header prefixes.
1387  Record.push_back(HSOpts.SystemHeaderPrefixes.size());
1388  for (unsigned I = 0, N = HSOpts.SystemHeaderPrefixes.size(); I != N; ++I) {
1389    AddString(HSOpts.SystemHeaderPrefixes[I].Prefix, Record);
1390    Record.push_back(HSOpts.SystemHeaderPrefixes[I].IsSystemHeader);
1391  }
1392
1393  AddString(HSOpts.ResourceDir, Record);
1394  AddString(HSOpts.ModuleCachePath, Record);
1395  AddString(HSOpts.ModuleUserBuildPath, Record);
1396  Record.push_back(HSOpts.DisableModuleHash);
1397  Record.push_back(HSOpts.UseBuiltinIncludes);
1398  Record.push_back(HSOpts.UseStandardSystemIncludes);
1399  Record.push_back(HSOpts.UseStandardCXXIncludes);
1400  Record.push_back(HSOpts.UseLibcxx);
1401  // Write out the specific module cache path that contains the module files.
1402  AddString(PP.getHeaderSearchInfo().getModuleCachePath(), Record);
1403  Stream.EmitRecord(HEADER_SEARCH_OPTIONS, Record);
1404
1405  // Preprocessor options.
1406  Record.clear();
1407  const PreprocessorOptions &PPOpts = PP.getPreprocessorOpts();
1408
1409  // Macro definitions.
1410  Record.push_back(PPOpts.Macros.size());
1411  for (unsigned I = 0, N = PPOpts.Macros.size(); I != N; ++I) {
1412    AddString(PPOpts.Macros[I].first, Record);
1413    Record.push_back(PPOpts.Macros[I].second);
1414  }
1415
1416  // Includes
1417  Record.push_back(PPOpts.Includes.size());
1418  for (unsigned I = 0, N = PPOpts.Includes.size(); I != N; ++I)
1419    AddString(PPOpts.Includes[I], Record);
1420
1421  // Macro includes
1422  Record.push_back(PPOpts.MacroIncludes.size());
1423  for (unsigned I = 0, N = PPOpts.MacroIncludes.size(); I != N; ++I)
1424    AddString(PPOpts.MacroIncludes[I], Record);
1425
1426  Record.push_back(PPOpts.UsePredefines);
1427  // Detailed record is important since it is used for the module cache hash.
1428  Record.push_back(PPOpts.DetailedRecord);
1429  AddString(PPOpts.ImplicitPCHInclude, Record);
1430  AddString(PPOpts.ImplicitPTHInclude, Record);
1431  Record.push_back(static_cast<unsigned>(PPOpts.ObjCXXARCStandardLibrary));
1432  Stream.EmitRecord(PREPROCESSOR_OPTIONS, Record);
1433
1434  // Leave the options block.
1435  Stream.ExitBlock();
1436
1437  // Original file name and file ID
1438  SourceManager &SM = Context.getSourceManager();
1439  if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
1440    auto *FileAbbrev = new BitCodeAbbrev();
1441    FileAbbrev->Add(BitCodeAbbrevOp(ORIGINAL_FILE));
1442    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // File ID
1443    FileAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1444    unsigned FileAbbrevCode = Stream.EmitAbbrev(FileAbbrev);
1445
1446    Record.clear();
1447    Record.push_back(ORIGINAL_FILE);
1448    Record.push_back(SM.getMainFileID().getOpaqueValue());
1449    EmitRecordWithPath(FileAbbrevCode, Record, MainFile->getName());
1450  }
1451
1452  Record.clear();
1453  Record.push_back(SM.getMainFileID().getOpaqueValue());
1454  Stream.EmitRecord(ORIGINAL_FILE_ID, Record);
1455
1456  // Original PCH directory
1457  if (!OutputFile.empty() && OutputFile != "-") {
1458    auto *Abbrev = new BitCodeAbbrev();
1459    Abbrev->Add(BitCodeAbbrevOp(ORIGINAL_PCH_DIR));
1460    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1461    unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
1462
1463    SmallString<128> OutputPath(OutputFile);
1464
1465    SM.getFileManager().makeAbsolutePath(OutputPath);
1466    StringRef origDir = llvm::sys::path::parent_path(OutputPath);
1467
1468    RecordData::value_type Record[] = {ORIGINAL_PCH_DIR};
1469    Stream.EmitRecordWithBlob(AbbrevCode, Record, origDir);
1470  }
1471
1472  WriteInputFiles(Context.SourceMgr,
1473                  PP.getHeaderSearchInfo().getHeaderSearchOpts(),
1474                  PP.getLangOpts().Modules);
1475  Stream.ExitBlock();
1476  return Signature;
1477}
1478
1479namespace  {
1480  /// \brief An input file.
1481  struct InputFileEntry {
1482    const FileEntry *File;
1483    bool IsSystemFile;
1484    bool IsTransient;
1485    bool BufferOverridden;
1486  };
1487} // end anonymous namespace
1488
1489void ASTWriter::WriteInputFiles(SourceManager &SourceMgr,
1490                                HeaderSearchOptions &HSOpts,
1491                                bool Modules) {
1492  using namespace llvm;
1493  Stream.EnterSubblock(INPUT_FILES_BLOCK_ID, 4);
1494
1495  // Create input-file abbreviation.
1496  auto *IFAbbrev = new BitCodeAbbrev();
1497  IFAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE));
1498  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
1499  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 12)); // Size
1500  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 32)); // Modification time
1501  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Overridden
1502  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Transient
1503  IFAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // File name
1504  unsigned IFAbbrevCode = Stream.EmitAbbrev(IFAbbrev);
1505
1506  // Get all ContentCache objects for files, sorted by whether the file is a
1507  // system one or not. System files go at the back, users files at the front.
1508  std::deque<InputFileEntry> SortedFiles;
1509  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size(); I != N; ++I) {
1510    // Get this source location entry.
1511    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1512    assert(&SourceMgr.getSLocEntry(FileID::get(I)) == SLoc);
1513
1514    // We only care about file entries that were not overridden.
1515    if (!SLoc->isFile())
1516      continue;
1517    const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1518    if (!Cache->OrigEntry)
1519      continue;
1520
1521    InputFileEntry Entry;
1522    Entry.File = Cache->OrigEntry;
1523    Entry.IsSystemFile = Cache->IsSystemFile;
1524    Entry.IsTransient = Cache->IsTransient;
1525    Entry.BufferOverridden = Cache->BufferOverridden;
1526    if (Cache->IsSystemFile)
1527      SortedFiles.push_back(Entry);
1528    else
1529      SortedFiles.push_front(Entry);
1530  }
1531
1532  unsigned UserFilesNum = 0;
1533  // Write out all of the input files.
1534  std::vector<uint64_t> InputFileOffsets;
1535  for (const auto &Entry : SortedFiles) {
1536    uint32_t &InputFileID = InputFileIDs[Entry.File];
1537    if (InputFileID != 0)
1538      continue; // already recorded this file.
1539
1540    // Record this entry's offset.
1541    InputFileOffsets.push_back(Stream.GetCurrentBitNo());
1542
1543    InputFileID = InputFileOffsets.size();
1544
1545    if (!Entry.IsSystemFile)
1546      ++UserFilesNum;
1547
1548    // Emit size/modification time for this file.
1549    // And whether this file was overridden.
1550    RecordData::value_type Record[] = {
1551        INPUT_FILE,
1552        InputFileOffsets.size(),
1553        (uint64_t)Entry.File->getSize(),
1554        (uint64_t)getTimestampForOutput(Entry.File),
1555        Entry.BufferOverridden,
1556        Entry.IsTransient};
1557
1558    EmitRecordWithPath(IFAbbrevCode, Record, Entry.File->getName());
1559  }
1560
1561  Stream.ExitBlock();
1562
1563  // Create input file offsets abbreviation.
1564  auto *OffsetsAbbrev = new BitCodeAbbrev();
1565  OffsetsAbbrev->Add(BitCodeAbbrevOp(INPUT_FILE_OFFSETS));
1566  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # input files
1567  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # non-system
1568                                                                //   input files
1569  OffsetsAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));   // Array
1570  unsigned OffsetsAbbrevCode = Stream.EmitAbbrev(OffsetsAbbrev);
1571
1572  // Write input file offsets.
1573  RecordData::value_type Record[] = {INPUT_FILE_OFFSETS,
1574                                     InputFileOffsets.size(), UserFilesNum};
1575  Stream.EmitRecordWithBlob(OffsetsAbbrevCode, Record, bytes(InputFileOffsets));
1576}
1577
1578//===----------------------------------------------------------------------===//
1579// Source Manager Serialization
1580//===----------------------------------------------------------------------===//
1581
1582/// \brief Create an abbreviation for the SLocEntry that refers to a
1583/// file.
1584static unsigned CreateSLocFileAbbrev(llvm::BitstreamWriter &Stream) {
1585  using namespace llvm;
1586
1587  auto *Abbrev = new BitCodeAbbrev();
1588  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_FILE_ENTRY));
1589  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1590  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1591  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1592  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1593  // FileEntry fields.
1594  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Input File ID
1595  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumCreatedFIDs
1596  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 24)); // FirstDeclIndex
1597  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // NumDecls
1598  return Stream.EmitAbbrev(Abbrev);
1599}
1600
1601/// \brief Create an abbreviation for the SLocEntry that refers to a
1602/// buffer.
1603static unsigned CreateSLocBufferAbbrev(llvm::BitstreamWriter &Stream) {
1604  using namespace llvm;
1605
1606  auto *Abbrev = new BitCodeAbbrev();
1607  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_ENTRY));
1608  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1609  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Include location
1610  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // Characteristic
1611  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Line directives
1612  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Buffer name blob
1613  return Stream.EmitAbbrev(Abbrev);
1614}
1615
1616/// \brief Create an abbreviation for the SLocEntry that refers to a
1617/// buffer's blob.
1618static unsigned CreateSLocBufferBlobAbbrev(llvm::BitstreamWriter &Stream) {
1619  using namespace llvm;
1620
1621  auto *Abbrev = new BitCodeAbbrev();
1622  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_BUFFER_BLOB));
1623  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Blob
1624  return Stream.EmitAbbrev(Abbrev);
1625}
1626
1627/// \brief Create an abbreviation for the SLocEntry that refers to a macro
1628/// expansion.
1629static unsigned CreateSLocExpansionAbbrev(llvm::BitstreamWriter &Stream) {
1630  using namespace llvm;
1631
1632  auto *Abbrev = new BitCodeAbbrev();
1633  Abbrev->Add(BitCodeAbbrevOp(SM_SLOC_EXPANSION_ENTRY));
1634  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Offset
1635  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Spelling location
1636  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // Start location
1637  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8)); // End location
1638  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Token length
1639  return Stream.EmitAbbrev(Abbrev);
1640}
1641
1642namespace {
1643  // Trait used for the on-disk hash table of header search information.
1644  class HeaderFileInfoTrait {
1645    ASTWriter &Writer;
1646    const HeaderSearch &HS;
1647
1648    // Keep track of the framework names we've used during serialization.
1649    SmallVector<char, 128> FrameworkStringData;
1650    llvm::StringMap<unsigned> FrameworkNameOffset;
1651
1652  public:
1653    HeaderFileInfoTrait(ASTWriter &Writer, const HeaderSearch &HS)
1654      : Writer(Writer), HS(HS) { }
1655
1656    struct key_type {
1657      const FileEntry *FE;
1658      const char *Filename;
1659    };
1660    typedef const key_type &key_type_ref;
1661
1662    typedef HeaderFileInfo data_type;
1663    typedef const data_type &data_type_ref;
1664    typedef unsigned hash_value_type;
1665    typedef unsigned offset_type;
1666
1667    hash_value_type ComputeHash(key_type_ref key) {
1668      // The hash is based only on size/time of the file, so that the reader can
1669      // match even when symlinking or excess path elements ("foo/../", "../")
1670      // change the form of the name. However, complete path is still the key.
1671      return llvm::hash_combine(key.FE->getSize(),
1672                                Writer.getTimestampForOutput(key.FE));
1673    }
1674
1675    std::pair<unsigned,unsigned>
1676    EmitKeyDataLength(raw_ostream& Out, key_type_ref key, data_type_ref Data) {
1677      using namespace llvm::support;
1678      endian::Writer<little> LE(Out);
1679      unsigned KeyLen = strlen(key.Filename) + 1 + 8 + 8;
1680      LE.write<uint16_t>(KeyLen);
1681      unsigned DataLen = 1 + 2 + 4 + 4;
1682      for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE))
1683        if (Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule()))
1684          DataLen += 4;
1685      LE.write<uint8_t>(DataLen);
1686      return std::make_pair(KeyLen, DataLen);
1687    }
1688
1689    void EmitKey(raw_ostream& Out, key_type_ref key, unsigned KeyLen) {
1690      using namespace llvm::support;
1691      endian::Writer<little> LE(Out);
1692      LE.write<uint64_t>(key.FE->getSize());
1693      KeyLen -= 8;
1694      LE.write<uint64_t>(Writer.getTimestampForOutput(key.FE));
1695      KeyLen -= 8;
1696      Out.write(key.Filename, KeyLen);
1697    }
1698
1699    void EmitData(raw_ostream &Out, key_type_ref key,
1700                  data_type_ref Data, unsigned DataLen) {
1701      using namespace llvm::support;
1702      endian::Writer<little> LE(Out);
1703      uint64_t Start = Out.tell(); (void)Start;
1704
1705      unsigned char Flags = (Data.isImport << 4)
1706                          | (Data.isPragmaOnce << 3)
1707                          | (Data.DirInfo << 1)
1708                          | Data.IndexHeaderMapHeader;
1709      LE.write<uint8_t>(Flags);
1710      LE.write<uint16_t>(Data.NumIncludes);
1711
1712      if (!Data.ControllingMacro)
1713        LE.write<uint32_t>(Data.ControllingMacroID);
1714      else
1715        LE.write<uint32_t>(Writer.getIdentifierRef(Data.ControllingMacro));
1716
1717      unsigned Offset = 0;
1718      if (!Data.Framework.empty()) {
1719        // If this header refers into a framework, save the framework name.
1720        llvm::StringMap<unsigned>::iterator Pos
1721          = FrameworkNameOffset.find(Data.Framework);
1722        if (Pos == FrameworkNameOffset.end()) {
1723          Offset = FrameworkStringData.size() + 1;
1724          FrameworkStringData.append(Data.Framework.begin(),
1725                                     Data.Framework.end());
1726          FrameworkStringData.push_back(0);
1727
1728          FrameworkNameOffset[Data.Framework] = Offset;
1729        } else
1730          Offset = Pos->second;
1731      }
1732      LE.write<uint32_t>(Offset);
1733
1734      // FIXME: If the header is excluded, we should write out some
1735      // record of that fact.
1736      for (auto ModInfo : HS.getModuleMap().findAllModulesForHeader(key.FE)) {
1737        if (uint32_t ModID =
1738                Writer.getLocalOrImportedSubmoduleID(ModInfo.getModule())) {
1739          uint32_t Value = (ModID << 2) | (unsigned)ModInfo.getRole();
1740          assert((Value >> 2) == ModID && "overflow in header module info");
1741          LE.write<uint32_t>(Value);
1742        }
1743      }
1744
1745      assert(Out.tell() - Start == DataLen && "Wrong data length");
1746    }
1747
1748    const char *strings_begin() const { return FrameworkStringData.begin(); }
1749    const char *strings_end() const { return FrameworkStringData.end(); }
1750  };
1751} // end anonymous namespace
1752
1753/// \brief Write the header search block for the list of files that
1754///
1755/// \param HS The header search structure to save.
1756void ASTWriter::WriteHeaderSearch(const HeaderSearch &HS) {
1757  SmallVector<const FileEntry *, 16> FilesByUID;
1758  HS.getFileMgr().GetUniqueIDMapping(FilesByUID);
1759
1760  if (FilesByUID.size() > HS.header_file_size())
1761    FilesByUID.resize(HS.header_file_size());
1762
1763  HeaderFileInfoTrait GeneratorTrait(*this, HS);
1764  llvm::OnDiskChainedHashTableGenerator<HeaderFileInfoTrait> Generator;
1765  SmallVector<const char *, 4> SavedStrings;
1766  unsigned NumHeaderSearchEntries = 0;
1767  for (unsigned UID = 0, LastUID = FilesByUID.size(); UID != LastUID; ++UID) {
1768    const FileEntry *File = FilesByUID[UID];
1769    if (!File)
1770      continue;
1771
1772    // Get the file info. This will load info from the external source if
1773    // necessary. Skip emitting this file if we have no information on it
1774    // as a header file (in which case HFI will be null) or if it hasn't
1775    // changed since it was loaded. Also skip it if it's for a modular header
1776    // from a different module; in that case, we rely on the module(s)
1777    // containing the header to provide this information.
1778    const HeaderFileInfo *HFI =
1779        HS.getExistingFileInfo(File, /*WantExternal*/!Chain);
1780    if (!HFI || (HFI->isModuleHeader && !HFI->isCompilingModuleHeader))
1781      continue;
1782
1783    // Massage the file path into an appropriate form.
1784    const char *Filename = File->getName();
1785    SmallString<128> FilenameTmp(Filename);
1786    if (PreparePathForOutput(FilenameTmp)) {
1787      // If we performed any translation on the file name at all, we need to
1788      // save this string, since the generator will refer to it later.
1789      Filename = strdup(FilenameTmp.c_str());
1790      SavedStrings.push_back(Filename);
1791    }
1792
1793    HeaderFileInfoTrait::key_type key = { File, Filename };
1794    Generator.insert(key, *HFI, GeneratorTrait);
1795    ++NumHeaderSearchEntries;
1796  }
1797
1798  // Create the on-disk hash table in a buffer.
1799  SmallString<4096> TableData;
1800  uint32_t BucketOffset;
1801  {
1802    using namespace llvm::support;
1803    llvm::raw_svector_ostream Out(TableData);
1804    // Make sure that no bucket is at offset 0
1805    endian::Writer<little>(Out).write<uint32_t>(0);
1806    BucketOffset = Generator.Emit(Out, GeneratorTrait);
1807  }
1808
1809  // Create a blob abbreviation
1810  using namespace llvm;
1811
1812  auto *Abbrev = new BitCodeAbbrev();
1813  Abbrev->Add(BitCodeAbbrevOp(HEADER_SEARCH_TABLE));
1814  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1815  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1816  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
1817  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
1818  unsigned TableAbbrev = Stream.EmitAbbrev(Abbrev);
1819
1820  // Write the header search table
1821  RecordData::value_type Record[] = {HEADER_SEARCH_TABLE, BucketOffset,
1822                                     NumHeaderSearchEntries, TableData.size()};
1823  TableData.append(GeneratorTrait.strings_begin(),GeneratorTrait.strings_end());
1824  Stream.EmitRecordWithBlob(TableAbbrev, Record, TableData);
1825
1826  // Free all of the strings we had to duplicate.
1827  for (unsigned I = 0, N = SavedStrings.size(); I != N; ++I)
1828    free(const_cast<char *>(SavedStrings[I]));
1829}
1830
1831/// \brief Writes the block containing the serialized form of the
1832/// source manager.
1833///
1834/// TODO: We should probably use an on-disk hash table (stored in a
1835/// blob), indexed based on the file name, so that we only create
1836/// entries for files that we actually need. In the common case (no
1837/// errors), we probably won't have to create file entries for any of
1838/// the files in the AST.
1839void ASTWriter::WriteSourceManagerBlock(SourceManager &SourceMgr,
1840                                        const Preprocessor &PP) {
1841  RecordData Record;
1842
1843  // Enter the source manager block.
1844  Stream.EnterSubblock(SOURCE_MANAGER_BLOCK_ID, 3);
1845
1846  // Abbreviations for the various kinds of source-location entries.
1847  unsigned SLocFileAbbrv = CreateSLocFileAbbrev(Stream);
1848  unsigned SLocBufferAbbrv = CreateSLocBufferAbbrev(Stream);
1849  unsigned SLocBufferBlobAbbrv = CreateSLocBufferBlobAbbrev(Stream);
1850  unsigned SLocExpansionAbbrv = CreateSLocExpansionAbbrev(Stream);
1851
1852  // Write out the source location entry table. We skip the first
1853  // entry, which is always the same dummy entry.
1854  std::vector<uint32_t> SLocEntryOffsets;
1855  RecordData PreloadSLocs;
1856  SLocEntryOffsets.reserve(SourceMgr.local_sloc_entry_size() - 1);
1857  for (unsigned I = 1, N = SourceMgr.local_sloc_entry_size();
1858       I != N; ++I) {
1859    // Get this source location entry.
1860    const SrcMgr::SLocEntry *SLoc = &SourceMgr.getLocalSLocEntry(I);
1861    FileID FID = FileID::get(I);
1862    assert(&SourceMgr.getSLocEntry(FID) == SLoc);
1863
1864    // Record the offset of this source-location entry.
1865    SLocEntryOffsets.push_back(Stream.GetCurrentBitNo());
1866
1867    // Figure out which record code to use.
1868    unsigned Code;
1869    if (SLoc->isFile()) {
1870      const SrcMgr::ContentCache *Cache = SLoc->getFile().getContentCache();
1871      if (Cache->OrigEntry) {
1872        Code = SM_SLOC_FILE_ENTRY;
1873      } else
1874        Code = SM_SLOC_BUFFER_ENTRY;
1875    } else
1876      Code = SM_SLOC_EXPANSION_ENTRY;
1877    Record.clear();
1878    Record.push_back(Code);
1879
1880    // Starting offset of this entry within this module, so skip the dummy.
1881    Record.push_back(SLoc->getOffset() - 2);
1882    if (SLoc->isFile()) {
1883      const SrcMgr::FileInfo &File = SLoc->getFile();
1884      Record.push_back(File.getIncludeLoc().getRawEncoding());
1885      Record.push_back(File.getFileCharacteristic()); // FIXME: stable encoding
1886      Record.push_back(File.hasLineDirectives());
1887
1888      const SrcMgr::ContentCache *Content = File.getContentCache();
1889      if (Content->OrigEntry) {
1890        assert(Content->OrigEntry == Content->ContentsEntry &&
1891               "Writing to AST an overridden file is not supported");
1892
1893        // The source location entry is a file. Emit input file ID.
1894        assert(InputFileIDs[Content->OrigEntry] != 0 && "Missed file entry");
1895        Record.push_back(InputFileIDs[Content->OrigEntry]);
1896
1897        Record.push_back(File.NumCreatedFIDs);
1898
1899        FileDeclIDsTy::iterator FDI = FileDeclIDs.find(FID);
1900        if (FDI != FileDeclIDs.end()) {
1901          Record.push_back(FDI->second->FirstDeclIndex);
1902          Record.push_back(FDI->second->DeclIDs.size());
1903        } else {
1904          Record.push_back(0);
1905          Record.push_back(0);
1906        }
1907
1908        Stream.EmitRecordWithAbbrev(SLocFileAbbrv, Record);
1909
1910        if (Content->BufferOverridden || Content->IsTransient) {
1911          RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
1912          const llvm::MemoryBuffer *Buffer
1913            = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1914          Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1915                                    StringRef(Buffer->getBufferStart(),
1916                                              Buffer->getBufferSize() + 1));
1917        }
1918      } else {
1919        // The source location entry is a buffer. The blob associated
1920        // with this entry contains the contents of the buffer.
1921
1922        // We add one to the size so that we capture the trailing NULL
1923        // that is required by llvm::MemoryBuffer::getMemBuffer (on
1924        // the reader side).
1925        const llvm::MemoryBuffer *Buffer
1926          = Content->getBuffer(PP.getDiagnostics(), PP.getSourceManager());
1927        const char *Name = Buffer->getBufferIdentifier();
1928        Stream.EmitRecordWithBlob(SLocBufferAbbrv, Record,
1929                                  StringRef(Name, strlen(Name) + 1));
1930        RecordData::value_type Record[] = {SM_SLOC_BUFFER_BLOB};
1931        Stream.EmitRecordWithBlob(SLocBufferBlobAbbrv, Record,
1932                                  StringRef(Buffer->getBufferStart(),
1933                                                  Buffer->getBufferSize() + 1));
1934
1935        if (strcmp(Name, "<built-in>") == 0) {
1936          PreloadSLocs.push_back(SLocEntryOffsets.size());
1937        }
1938      }
1939    } else {
1940      // The source location entry is a macro expansion.
1941      const SrcMgr::ExpansionInfo &Expansion = SLoc->getExpansion();
1942      Record.push_back(Expansion.getSpellingLoc().getRawEncoding());
1943      Record.push_back(Expansion.getExpansionLocStart().getRawEncoding());
1944      Record.push_back(Expansion.isMacroArgExpansion() ? 0
1945                             : Expansion.getExpansionLocEnd().getRawEncoding());
1946
1947      // Compute the token length for this macro expansion.
1948      unsigned NextOffset = SourceMgr.getNextLocalOffset();
1949      if (I + 1 != N)
1950        NextOffset = SourceMgr.getLocalSLocEntry(I + 1).getOffset();
1951      Record.push_back(NextOffset - SLoc->getOffset() - 1);
1952      Stream.EmitRecordWithAbbrev(SLocExpansionAbbrv, Record);
1953    }
1954  }
1955
1956  Stream.ExitBlock();
1957
1958  if (SLocEntryOffsets.empty())
1959    return;
1960
1961  // Write the source-location offsets table into the AST block. This
1962  // table is used for lazily loading source-location information.
1963  using namespace llvm;
1964
1965  auto *Abbrev = new BitCodeAbbrev();
1966  Abbrev->Add(BitCodeAbbrevOp(SOURCE_LOCATION_OFFSETS));
1967  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // # of slocs
1968  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 16)); // total size
1969  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // offsets
1970  unsigned SLocOffsetsAbbrev = Stream.EmitAbbrev(Abbrev);
1971  {
1972    RecordData::value_type Record[] = {
1973        SOURCE_LOCATION_OFFSETS, SLocEntryOffsets.size(),
1974        SourceMgr.getNextLocalOffset() - 1 /* skip dummy */};
1975    Stream.EmitRecordWithBlob(SLocOffsetsAbbrev, Record,
1976                              bytes(SLocEntryOffsets));
1977  }
1978  // Write the source location entry preloads array, telling the AST
1979  // reader which source locations entries it should load eagerly.
1980  Stream.EmitRecord(SOURCE_LOCATION_PRELOADS, PreloadSLocs);
1981
1982  // Write the line table. It depends on remapping working, so it must come
1983  // after the source location offsets.
1984  if (SourceMgr.hasLineTable()) {
1985    LineTableInfo &LineTable = SourceMgr.getLineTable();
1986
1987    Record.clear();
1988
1989    // Emit the needed file names.
1990    llvm::DenseMap<int, int> FilenameMap;
1991    for (const auto &L : LineTable) {
1992      if (L.first.ID < 0)
1993        continue;
1994      for (auto &LE : L.second) {
1995        if (FilenameMap.insert(std::make_pair(LE.FilenameID,
1996                                              FilenameMap.size())).second)
1997          AddPath(LineTable.getFilename(LE.FilenameID), Record);
1998      }
1999    }
2000    Record.push_back(0);
2001
2002    // Emit the line entries
2003    for (const auto &L : LineTable) {
2004      // Only emit entries for local files.
2005      if (L.first.ID < 0)
2006        continue;
2007
2008      // Emit the file ID
2009      Record.push_back(L.first.ID);
2010
2011      // Emit the line entries
2012      Record.push_back(L.second.size());
2013      for (const auto &LE : L.second) {
2014        Record.push_back(LE.FileOffset);
2015        Record.push_back(LE.LineNo);
2016        Record.push_back(FilenameMap[LE.FilenameID]);
2017        Record.push_back((unsigned)LE.FileKind);
2018        Record.push_back(LE.IncludeOffset);
2019      }
2020    }
2021
2022    Stream.EmitRecord(SOURCE_MANAGER_LINE_TABLE, Record);
2023  }
2024}
2025
2026//===----------------------------------------------------------------------===//
2027// Preprocessor Serialization
2028//===----------------------------------------------------------------------===//
2029
2030static bool shouldIgnoreMacro(MacroDirective *MD, bool IsModule,
2031                              const Preprocessor &PP) {
2032  if (MacroInfo *MI = MD->getMacroInfo())
2033    if (MI->isBuiltinMacro())
2034      return true;
2035
2036  if (IsModule) {
2037    SourceLocation Loc = MD->getLocation();
2038    if (Loc.isInvalid())
2039      return true;
2040    if (PP.getSourceManager().getFileID(Loc) == PP.getPredefinesFileID())
2041      return true;
2042  }
2043
2044  return false;
2045}
2046
2047/// \brief Writes the block containing the serialized form of the
2048/// preprocessor.
2049///
2050void ASTWriter::WritePreprocessor(const Preprocessor &PP, bool IsModule) {
2051  PreprocessingRecord *PPRec = PP.getPreprocessingRecord();
2052  if (PPRec)
2053    WritePreprocessorDetail(*PPRec);
2054
2055  RecordData Record;
2056  RecordData ModuleMacroRecord;
2057
2058  // If the preprocessor __COUNTER__ value has been bumped, remember it.
2059  if (PP.getCounterValue() != 0) {
2060    RecordData::value_type Record[] = {PP.getCounterValue()};
2061    Stream.EmitRecord(PP_COUNTER_VALUE, Record);
2062  }
2063
2064  // Enter the preprocessor block.
2065  Stream.EnterSubblock(PREPROCESSOR_BLOCK_ID, 3);
2066
2067  // If the AST file contains __DATE__ or __TIME__ emit a warning about this.
2068  // FIXME: Include a location for the use, and say which one was used.
2069  if (PP.SawDateOrTime())
2070    PP.Diag(SourceLocation(), diag::warn_module_uses_date_time) << IsModule;
2071
2072  // Loop over all the macro directives that are live at the end of the file,
2073  // emitting each to the PP section.
2074
2075  // Construct the list of identifiers with macro directives that need to be
2076  // serialized.
2077  SmallVector<const IdentifierInfo *, 128> MacroIdentifiers;
2078  for (auto &Id : PP.getIdentifierTable())
2079    if (Id.second->hadMacroDefinition() &&
2080        (!Id.second->isFromAST() ||
2081         Id.second->hasChangedSinceDeserialization()))
2082      MacroIdentifiers.push_back(Id.second);
2083  // Sort the set of macro definitions that need to be serialized by the
2084  // name of the macro, to provide a stable ordering.
2085  std::sort(MacroIdentifiers.begin(), MacroIdentifiers.end(),
2086            llvm::less_ptr<IdentifierInfo>());
2087
2088  // Emit the macro directives as a list and associate the offset with the
2089  // identifier they belong to.
2090  for (const IdentifierInfo *Name : MacroIdentifiers) {
2091    MacroDirective *MD = PP.getLocalMacroDirectiveHistory(Name);
2092    auto StartOffset = Stream.GetCurrentBitNo();
2093
2094    // Emit the macro directives in reverse source order.
2095    for (; MD; MD = MD->getPrevious()) {
2096      // Once we hit an ignored macro, we're done: the rest of the chain
2097      // will all be ignored macros.
2098      if (shouldIgnoreMacro(MD, IsModule, PP))
2099        break;
2100
2101      AddSourceLocation(MD->getLocation(), Record);
2102      Record.push_back(MD->getKind());
2103      if (auto *DefMD = dyn_cast<DefMacroDirective>(MD)) {
2104        Record.push_back(getMacroRef(DefMD->getInfo(), Name));
2105      } else if (auto *VisMD = dyn_cast<VisibilityMacroDirective>(MD)) {
2106        Record.push_back(VisMD->isPublic());
2107      }
2108    }
2109
2110    // Write out any exported module macros.
2111    bool EmittedModuleMacros = false;
2112    if (IsModule) {
2113      auto Leafs = PP.getLeafModuleMacros(Name);
2114      SmallVector<ModuleMacro*, 8> Worklist(Leafs.begin(), Leafs.end());
2115      llvm::DenseMap<ModuleMacro*, unsigned> Visits;
2116      while (!Worklist.empty()) {
2117        auto *Macro = Worklist.pop_back_val();
2118
2119        // Emit a record indicating this submodule exports this macro.
2120        ModuleMacroRecord.push_back(
2121            getSubmoduleID(Macro->getOwningModule()));
2122        ModuleMacroRecord.push_back(getMacroRef(Macro->getMacroInfo(), Name));
2123        for (auto *M : Macro->overrides())
2124          ModuleMacroRecord.push_back(getSubmoduleID(M->getOwningModule()));
2125
2126        Stream.EmitRecord(PP_MODULE_MACRO, ModuleMacroRecord);
2127        ModuleMacroRecord.clear();
2128
2129        // Enqueue overridden macros once we've visited all their ancestors.
2130        for (auto *M : Macro->overrides())
2131          if (++Visits[M] == M->getNumOverridingMacros())
2132            Worklist.push_back(M);
2133
2134        EmittedModuleMacros = true;
2135      }
2136    }
2137
2138    if (Record.empty() && !EmittedModuleMacros)
2139      continue;
2140
2141    IdentMacroDirectivesOffsetMap[Name] = StartOffset;
2142    Stream.EmitRecord(PP_MACRO_DIRECTIVE_HISTORY, Record);
2143    Record.clear();
2144  }
2145
2146  /// \brief Offsets of each of the macros into the bitstream, indexed by
2147  /// the local macro ID
2148  ///
2149  /// For each identifier that is associated with a macro, this map
2150  /// provides the offset into the bitstream where that macro is
2151  /// defined.
2152  std::vector<uint32_t> MacroOffsets;
2153
2154  for (unsigned I = 0, N = MacroInfosToEmit.size(); I != N; ++I) {
2155    const IdentifierInfo *Name = MacroInfosToEmit[I].Name;
2156    MacroInfo *MI = MacroInfosToEmit[I].MI;
2157    MacroID ID = MacroInfosToEmit[I].ID;
2158
2159    if (ID < FirstMacroID) {
2160      assert(0 && "Loaded MacroInfo entered MacroInfosToEmit ?");
2161      continue;
2162    }
2163
2164    // Record the local offset of this macro.
2165    unsigned Index = ID - FirstMacroID;
2166    if (Index == MacroOffsets.size())
2167      MacroOffsets.push_back(Stream.GetCurrentBitNo());
2168    else {
2169      if (Index > MacroOffsets.size())
2170        MacroOffsets.resize(Index + 1);
2171
2172      MacroOffsets[Index] = Stream.GetCurrentBitNo();
2173    }
2174
2175    AddIdentifierRef(Name, Record);
2176    Record.push_back(inferSubmoduleIDFromLocation(MI->getDefinitionLoc()));
2177    AddSourceLocation(MI->getDefinitionLoc(), Record);
2178    AddSourceLocation(MI->getDefinitionEndLoc(), Record);
2179    Record.push_back(MI->isUsed());
2180    Record.push_back(MI->isUsedForHeaderGuard());
2181    unsigned Code;
2182    if (MI->isObjectLike()) {
2183      Code = PP_MACRO_OBJECT_LIKE;
2184    } else {
2185      Code = PP_MACRO_FUNCTION_LIKE;
2186
2187      Record.push_back(MI->isC99Varargs());
2188      Record.push_back(MI->isGNUVarargs());
2189      Record.push_back(MI->hasCommaPasting());
2190      Record.push_back(MI->getNumArgs());
2191      for (const IdentifierInfo *Arg : MI->args())
2192        AddIdentifierRef(Arg, Record);
2193    }
2194
2195    // If we have a detailed preprocessing record, record the macro definition
2196    // ID that corresponds to this macro.
2197    if (PPRec)
2198      Record.push_back(MacroDefinitions[PPRec->findMacroDefinition(MI)]);
2199
2200    Stream.EmitRecord(Code, Record);
2201    Record.clear();
2202
2203    // Emit the tokens array.
2204    for (unsigned TokNo = 0, e = MI->getNumTokens(); TokNo != e; ++TokNo) {
2205      // Note that we know that the preprocessor does not have any annotation
2206      // tokens in it because they are created by the parser, and thus can't
2207      // be in a macro definition.
2208      const Token &Tok = MI->getReplacementToken(TokNo);
2209      AddToken(Tok, Record);
2210      Stream.EmitRecord(PP_TOKEN, Record);
2211      Record.clear();
2212    }
2213    ++NumMacros;
2214  }
2215
2216  Stream.ExitBlock();
2217
2218  // Write the offsets table for macro IDs.
2219  using namespace llvm;
2220
2221  auto *Abbrev = new BitCodeAbbrev();
2222  Abbrev->Add(BitCodeAbbrevOp(MACRO_OFFSET));
2223  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of macros
2224  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
2225  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2226
2227  unsigned MacroOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2228  {
2229    RecordData::value_type Record[] = {MACRO_OFFSET, MacroOffsets.size(),
2230                                       FirstMacroID - NUM_PREDEF_MACRO_IDS};
2231    Stream.EmitRecordWithBlob(MacroOffsetAbbrev, Record, bytes(MacroOffsets));
2232  }
2233}
2234
2235void ASTWriter::WritePreprocessorDetail(PreprocessingRecord &PPRec) {
2236  if (PPRec.local_begin() == PPRec.local_end())
2237    return;
2238
2239  SmallVector<PPEntityOffset, 64> PreprocessedEntityOffsets;
2240
2241  // Enter the preprocessor block.
2242  Stream.EnterSubblock(PREPROCESSOR_DETAIL_BLOCK_ID, 3);
2243
2244  // If the preprocessor has a preprocessing record, emit it.
2245  unsigned NumPreprocessingRecords = 0;
2246  using namespace llvm;
2247
2248  // Set up the abbreviation for
2249  unsigned InclusionAbbrev = 0;
2250  {
2251    auto *Abbrev = new BitCodeAbbrev();
2252    Abbrev->Add(BitCodeAbbrevOp(PPD_INCLUSION_DIRECTIVE));
2253    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // filename length
2254    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // in quotes
2255    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 2)); // kind
2256    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // imported module
2257    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2258    InclusionAbbrev = Stream.EmitAbbrev(Abbrev);
2259  }
2260
2261  unsigned FirstPreprocessorEntityID
2262    = (Chain ? PPRec.getNumLoadedPreprocessedEntities() : 0)
2263    + NUM_PREDEF_PP_ENTITY_IDS;
2264  unsigned NextPreprocessorEntityID = FirstPreprocessorEntityID;
2265  RecordData Record;
2266  for (PreprocessingRecord::iterator E = PPRec.local_begin(),
2267                                  EEnd = PPRec.local_end();
2268       E != EEnd;
2269       (void)++E, ++NumPreprocessingRecords, ++NextPreprocessorEntityID) {
2270    Record.clear();
2271
2272    PreprocessedEntityOffsets.push_back(
2273        PPEntityOffset((*E)->getSourceRange(), Stream.GetCurrentBitNo()));
2274
2275    if (auto *MD = dyn_cast<MacroDefinitionRecord>(*E)) {
2276      // Record this macro definition's ID.
2277      MacroDefinitions[MD] = NextPreprocessorEntityID;
2278
2279      AddIdentifierRef(MD->getName(), Record);
2280      Stream.EmitRecord(PPD_MACRO_DEFINITION, Record);
2281      continue;
2282    }
2283
2284    if (auto *ME = dyn_cast<MacroExpansion>(*E)) {
2285      Record.push_back(ME->isBuiltinMacro());
2286      if (ME->isBuiltinMacro())
2287        AddIdentifierRef(ME->getName(), Record);
2288      else
2289        Record.push_back(MacroDefinitions[ME->getDefinition()]);
2290      Stream.EmitRecord(PPD_MACRO_EXPANSION, Record);
2291      continue;
2292    }
2293
2294    if (auto *ID = dyn_cast<InclusionDirective>(*E)) {
2295      Record.push_back(PPD_INCLUSION_DIRECTIVE);
2296      Record.push_back(ID->getFileName().size());
2297      Record.push_back(ID->wasInQuotes());
2298      Record.push_back(static_cast<unsigned>(ID->getKind()));
2299      Record.push_back(ID->importedModule());
2300      SmallString<64> Buffer;
2301      Buffer += ID->getFileName();
2302      // Check that the FileEntry is not null because it was not resolved and
2303      // we create a PCH even with compiler errors.
2304      if (ID->getFile())
2305        Buffer += ID->getFile()->getName();
2306      Stream.EmitRecordWithBlob(InclusionAbbrev, Record, Buffer);
2307      continue;
2308    }
2309
2310    llvm_unreachable("Unhandled PreprocessedEntity in ASTWriter");
2311  }
2312  Stream.ExitBlock();
2313
2314  // Write the offsets table for the preprocessing record.
2315  if (NumPreprocessingRecords > 0) {
2316    assert(PreprocessedEntityOffsets.size() == NumPreprocessingRecords);
2317
2318    // Write the offsets table for identifier IDs.
2319    using namespace llvm;
2320
2321    auto *Abbrev = new BitCodeAbbrev();
2322    Abbrev->Add(BitCodeAbbrevOp(PPD_ENTITIES_OFFSETS));
2323    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first pp entity
2324    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2325    unsigned PPEOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2326
2327    RecordData::value_type Record[] = {PPD_ENTITIES_OFFSETS,
2328                                       FirstPreprocessorEntityID -
2329                                           NUM_PREDEF_PP_ENTITY_IDS};
2330    Stream.EmitRecordWithBlob(PPEOffsetAbbrev, Record,
2331                              bytes(PreprocessedEntityOffsets));
2332  }
2333}
2334
2335unsigned ASTWriter::getLocalOrImportedSubmoduleID(Module *Mod) {
2336  if (!Mod)
2337    return 0;
2338
2339  llvm::DenseMap<Module *, unsigned>::iterator Known = SubmoduleIDs.find(Mod);
2340  if (Known != SubmoduleIDs.end())
2341    return Known->second;
2342
2343  if (Mod->getTopLevelModule() != WritingModule)
2344    return 0;
2345
2346  return SubmoduleIDs[Mod] = NextSubmoduleID++;
2347}
2348
2349unsigned ASTWriter::getSubmoduleID(Module *Mod) {
2350  // FIXME: This can easily happen, if we have a reference to a submodule that
2351  // did not result in us loading a module file for that submodule. For
2352  // instance, a cross-top-level-module 'conflict' declaration will hit this.
2353  unsigned ID = getLocalOrImportedSubmoduleID(Mod);
2354  assert((ID || !Mod) &&
2355         "asked for module ID for non-local, non-imported module");
2356  return ID;
2357}
2358
2359/// \brief Compute the number of modules within the given tree (including the
2360/// given module).
2361static unsigned getNumberOfModules(Module *Mod) {
2362  unsigned ChildModules = 0;
2363  for (auto Sub = Mod->submodule_begin(), SubEnd = Mod->submodule_end();
2364       Sub != SubEnd; ++Sub)
2365    ChildModules += getNumberOfModules(*Sub);
2366
2367  return ChildModules + 1;
2368}
2369
2370void ASTWriter::WriteSubmodules(Module *WritingModule) {
2371  // Enter the submodule description block.
2372  Stream.EnterSubblock(SUBMODULE_BLOCK_ID, /*bits for abbreviations*/5);
2373
2374  // Write the abbreviations needed for the submodules block.
2375  using namespace llvm;
2376
2377  auto *Abbrev = new BitCodeAbbrev();
2378  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_DEFINITION));
2379  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // ID
2380  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // Parent
2381  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2382  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExplicit
2383  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsSystem
2384  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsExternC
2385  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferSubmodules...
2386  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExplicit...
2387  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // InferExportWild...
2388  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // ConfigMacrosExh...
2389  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2390  unsigned DefinitionAbbrev = Stream.EmitAbbrev(Abbrev);
2391
2392  Abbrev = new BitCodeAbbrev();
2393  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_HEADER));
2394  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2395  unsigned UmbrellaAbbrev = Stream.EmitAbbrev(Abbrev);
2396
2397  Abbrev = new BitCodeAbbrev();
2398  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_HEADER));
2399  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2400  unsigned HeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2401
2402  Abbrev = new BitCodeAbbrev();
2403  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TOPHEADER));
2404  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2405  unsigned TopHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2406
2407  Abbrev = new BitCodeAbbrev();
2408  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_UMBRELLA_DIR));
2409  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2410  unsigned UmbrellaDirAbbrev = Stream.EmitAbbrev(Abbrev);
2411
2412  Abbrev = new BitCodeAbbrev();
2413  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_REQUIRES));
2414  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // State
2415  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Feature
2416  unsigned RequiresAbbrev = Stream.EmitAbbrev(Abbrev);
2417
2418  Abbrev = new BitCodeAbbrev();
2419  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_EXCLUDED_HEADER));
2420  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2421  unsigned ExcludedHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2422
2423  Abbrev = new BitCodeAbbrev();
2424  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_TEXTUAL_HEADER));
2425  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2426  unsigned TextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2427
2428  Abbrev = new BitCodeAbbrev();
2429  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_HEADER));
2430  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2431  unsigned PrivateHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2432
2433  Abbrev = new BitCodeAbbrev();
2434  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_PRIVATE_TEXTUAL_HEADER));
2435  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Name
2436  unsigned PrivateTextualHeaderAbbrev = Stream.EmitAbbrev(Abbrev);
2437
2438  Abbrev = new BitCodeAbbrev();
2439  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_LINK_LIBRARY));
2440  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // IsFramework
2441  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));     // Name
2442  unsigned LinkLibraryAbbrev = Stream.EmitAbbrev(Abbrev);
2443
2444  Abbrev = new BitCodeAbbrev();
2445  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFIG_MACRO));
2446  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Macro name
2447  unsigned ConfigMacroAbbrev = Stream.EmitAbbrev(Abbrev);
2448
2449  Abbrev = new BitCodeAbbrev();
2450  Abbrev->Add(BitCodeAbbrevOp(SUBMODULE_CONFLICT));
2451  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));  // Other module
2452  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));    // Message
2453  unsigned ConflictAbbrev = Stream.EmitAbbrev(Abbrev);
2454
2455  // Write the submodule metadata block.
2456  RecordData::value_type Record[] = {getNumberOfModules(WritingModule),
2457                                     FirstSubmoduleID -
2458                                         NUM_PREDEF_SUBMODULE_IDS};
2459  Stream.EmitRecord(SUBMODULE_METADATA, Record);
2460
2461  // Write all of the submodules.
2462  std::queue<Module *> Q;
2463  Q.push(WritingModule);
2464  while (!Q.empty()) {
2465    Module *Mod = Q.front();
2466    Q.pop();
2467    unsigned ID = getSubmoduleID(Mod);
2468
2469    uint64_t ParentID = 0;
2470    if (Mod->Parent) {
2471      assert(SubmoduleIDs[Mod->Parent] && "Submodule parent not written?");
2472      ParentID = SubmoduleIDs[Mod->Parent];
2473    }
2474
2475    // Emit the definition of the block.
2476    {
2477      RecordData::value_type Record[] = {
2478          SUBMODULE_DEFINITION, ID, ParentID, Mod->IsFramework, Mod->IsExplicit,
2479          Mod->IsSystem, Mod->IsExternC, Mod->InferSubmodules,
2480          Mod->InferExplicitSubmodules, Mod->InferExportWildcard,
2481          Mod->ConfigMacrosExhaustive};
2482      Stream.EmitRecordWithBlob(DefinitionAbbrev, Record, Mod->Name);
2483    }
2484
2485    // Emit the requirements.
2486    for (const auto &R : Mod->Requirements) {
2487      RecordData::value_type Record[] = {SUBMODULE_REQUIRES, R.second};
2488      Stream.EmitRecordWithBlob(RequiresAbbrev, Record, R.first);
2489    }
2490
2491    // Emit the umbrella header, if there is one.
2492    if (auto UmbrellaHeader = Mod->getUmbrellaHeader()) {
2493      RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_HEADER};
2494      Stream.EmitRecordWithBlob(UmbrellaAbbrev, Record,
2495                                UmbrellaHeader.NameAsWritten);
2496    } else if (auto UmbrellaDir = Mod->getUmbrellaDir()) {
2497      RecordData::value_type Record[] = {SUBMODULE_UMBRELLA_DIR};
2498      Stream.EmitRecordWithBlob(UmbrellaDirAbbrev, Record,
2499                                UmbrellaDir.NameAsWritten);
2500    }
2501
2502    // Emit the headers.
2503    struct {
2504      unsigned RecordKind;
2505      unsigned Abbrev;
2506      Module::HeaderKind HeaderKind;
2507    } HeaderLists[] = {
2508      {SUBMODULE_HEADER, HeaderAbbrev, Module::HK_Normal},
2509      {SUBMODULE_TEXTUAL_HEADER, TextualHeaderAbbrev, Module::HK_Textual},
2510      {SUBMODULE_PRIVATE_HEADER, PrivateHeaderAbbrev, Module::HK_Private},
2511      {SUBMODULE_PRIVATE_TEXTUAL_HEADER, PrivateTextualHeaderAbbrev,
2512        Module::HK_PrivateTextual},
2513      {SUBMODULE_EXCLUDED_HEADER, ExcludedHeaderAbbrev, Module::HK_Excluded}
2514    };
2515    for (auto &HL : HeaderLists) {
2516      RecordData::value_type Record[] = {HL.RecordKind};
2517      for (auto &H : Mod->Headers[HL.HeaderKind])
2518        Stream.EmitRecordWithBlob(HL.Abbrev, Record, H.NameAsWritten);
2519    }
2520
2521    // Emit the top headers.
2522    {
2523      auto TopHeaders = Mod->getTopHeaders(PP->getFileManager());
2524      RecordData::value_type Record[] = {SUBMODULE_TOPHEADER};
2525      for (auto *H : TopHeaders)
2526        Stream.EmitRecordWithBlob(TopHeaderAbbrev, Record, H->getName());
2527    }
2528
2529    // Emit the imports.
2530    if (!Mod->Imports.empty()) {
2531      RecordData Record;
2532      for (auto *I : Mod->Imports)
2533        Record.push_back(getSubmoduleID(I));
2534      Stream.EmitRecord(SUBMODULE_IMPORTS, Record);
2535    }
2536
2537    // Emit the exports.
2538    if (!Mod->Exports.empty()) {
2539      RecordData Record;
2540      for (const auto &E : Mod->Exports) {
2541        // FIXME: This may fail; we don't require that all exported modules
2542        // are local or imported.
2543        Record.push_back(getSubmoduleID(E.getPointer()));
2544        Record.push_back(E.getInt());
2545      }
2546      Stream.EmitRecord(SUBMODULE_EXPORTS, Record);
2547    }
2548
2549    //FIXME: How do we emit the 'use'd modules?  They may not be submodules.
2550    // Might be unnecessary as use declarations are only used to build the
2551    // module itself.
2552
2553    // Emit the link libraries.
2554    for (const auto &LL : Mod->LinkLibraries) {
2555      RecordData::value_type Record[] = {SUBMODULE_LINK_LIBRARY,
2556                                         LL.IsFramework};
2557      Stream.EmitRecordWithBlob(LinkLibraryAbbrev, Record, LL.Library);
2558    }
2559
2560    // Emit the conflicts.
2561    for (const auto &C : Mod->Conflicts) {
2562      // FIXME: This may fail; we don't require that all conflicting modules
2563      // are local or imported.
2564      RecordData::value_type Record[] = {SUBMODULE_CONFLICT,
2565                                         getSubmoduleID(C.Other)};
2566      Stream.EmitRecordWithBlob(ConflictAbbrev, Record, C.Message);
2567    }
2568
2569    // Emit the configuration macros.
2570    for (const auto &CM : Mod->ConfigMacros) {
2571      RecordData::value_type Record[] = {SUBMODULE_CONFIG_MACRO};
2572      Stream.EmitRecordWithBlob(ConfigMacroAbbrev, Record, CM);
2573    }
2574
2575    // Queue up the submodules of this module.
2576    for (auto *M : Mod->submodules())
2577      Q.push(M);
2578  }
2579
2580  Stream.ExitBlock();
2581
2582  assert((NextSubmoduleID - FirstSubmoduleID ==
2583          getNumberOfModules(WritingModule)) &&
2584         "Wrong # of submodules; found a reference to a non-local, "
2585         "non-imported submodule?");
2586}
2587
2588serialization::SubmoduleID
2589ASTWriter::inferSubmoduleIDFromLocation(SourceLocation Loc) {
2590  if (Loc.isInvalid() || !WritingModule)
2591    return 0; // No submodule
2592
2593  // Find the module that owns this location.
2594  ModuleMap &ModMap = PP->getHeaderSearchInfo().getModuleMap();
2595  Module *OwningMod
2596    = ModMap.inferModuleFromLocation(FullSourceLoc(Loc,PP->getSourceManager()));
2597  if (!OwningMod)
2598    return 0;
2599
2600  // Check whether this submodule is part of our own module.
2601  if (WritingModule != OwningMod && !OwningMod->isSubModuleOf(WritingModule))
2602    return 0;
2603
2604  return getSubmoduleID(OwningMod);
2605}
2606
2607void ASTWriter::WritePragmaDiagnosticMappings(const DiagnosticsEngine &Diag,
2608                                              bool isModule) {
2609  // Make sure set diagnostic pragmas don't affect the translation unit that
2610  // imports the module.
2611  // FIXME: Make diagnostic pragma sections work properly with modules.
2612  if (isModule)
2613    return;
2614
2615  llvm::SmallDenseMap<const DiagnosticsEngine::DiagState *, unsigned, 64>
2616      DiagStateIDMap;
2617  unsigned CurrID = 0;
2618  DiagStateIDMap[&Diag.DiagStates.front()] = ++CurrID; // the command-line one.
2619  RecordData Record;
2620  for (DiagnosticsEngine::DiagStatePointsTy::const_iterator
2621         I = Diag.DiagStatePoints.begin(), E = Diag.DiagStatePoints.end();
2622         I != E; ++I) {
2623    const DiagnosticsEngine::DiagStatePoint &point = *I;
2624    if (point.Loc.isInvalid())
2625      continue;
2626
2627    Record.push_back(point.Loc.getRawEncoding());
2628    unsigned &DiagStateID = DiagStateIDMap[point.State];
2629    Record.push_back(DiagStateID);
2630
2631    if (DiagStateID == 0) {
2632      DiagStateID = ++CurrID;
2633      for (const auto &I : *(point.State)) {
2634        if (I.second.isPragma()) {
2635          Record.push_back(I.first);
2636          Record.push_back((unsigned)I.second.getSeverity());
2637        }
2638      }
2639      Record.push_back(-1); // mark the end of the diag/map pairs for this
2640                            // location.
2641    }
2642  }
2643
2644  if (!Record.empty())
2645    Stream.EmitRecord(DIAG_PRAGMA_MAPPINGS, Record);
2646}
2647
2648void ASTWriter::WriteCXXCtorInitializersOffsets() {
2649  if (CXXCtorInitializersOffsets.empty())
2650    return;
2651
2652  // Create a blob abbreviation for the C++ ctor initializer offsets.
2653  using namespace llvm;
2654
2655  auto *Abbrev = new BitCodeAbbrev();
2656  Abbrev->Add(BitCodeAbbrevOp(CXX_CTOR_INITIALIZERS_OFFSETS));
2657  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2658  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2659  unsigned CtorInitializersOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2660
2661  // Write the base specifier offsets table.
2662  RecordData::value_type Record[] = {CXX_CTOR_INITIALIZERS_OFFSETS,
2663                                     CXXCtorInitializersOffsets.size()};
2664  Stream.EmitRecordWithBlob(CtorInitializersOffsetAbbrev, Record,
2665                            bytes(CXXCtorInitializersOffsets));
2666}
2667
2668void ASTWriter::WriteCXXBaseSpecifiersOffsets() {
2669  if (CXXBaseSpecifiersOffsets.empty())
2670    return;
2671
2672  // Create a blob abbreviation for the C++ base specifiers offsets.
2673  using namespace llvm;
2674
2675  auto *Abbrev = new BitCodeAbbrev();
2676  Abbrev->Add(BitCodeAbbrevOp(CXX_BASE_SPECIFIER_OFFSETS));
2677  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
2678  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2679  unsigned BaseSpecifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2680
2681  // Write the base specifier offsets table.
2682  RecordData::value_type Record[] = {CXX_BASE_SPECIFIER_OFFSETS,
2683                                     CXXBaseSpecifiersOffsets.size()};
2684  Stream.EmitRecordWithBlob(BaseSpecifierOffsetAbbrev, Record,
2685                            bytes(CXXBaseSpecifiersOffsets));
2686}
2687
2688//===----------------------------------------------------------------------===//
2689// Type Serialization
2690//===----------------------------------------------------------------------===//
2691
2692/// \brief Write the representation of a type to the AST stream.
2693void ASTWriter::WriteType(QualType T) {
2694  TypeIdx &Idx = TypeIdxs[T];
2695  if (Idx.getIndex() == 0) // we haven't seen this type before.
2696    Idx = TypeIdx(NextTypeID++);
2697
2698  assert(Idx.getIndex() >= FirstTypeID && "Re-writing a type from a prior AST");
2699
2700  // Record the offset for this type.
2701  unsigned Index = Idx.getIndex() - FirstTypeID;
2702  if (TypeOffsets.size() == Index)
2703    TypeOffsets.push_back(Stream.GetCurrentBitNo());
2704  else if (TypeOffsets.size() < Index) {
2705    TypeOffsets.resize(Index + 1);
2706    TypeOffsets[Index] = Stream.GetCurrentBitNo();
2707  }
2708
2709  RecordData Record;
2710
2711  // Emit the type's representation.
2712  ASTTypeWriter W(*this, Record);
2713  W.AbbrevToUse = 0;
2714
2715  if (T.hasLocalNonFastQualifiers()) {
2716    Qualifiers Qs = T.getLocalQualifiers();
2717    AddTypeRef(T.getLocalUnqualifiedType(), Record);
2718    Record.push_back(Qs.getAsOpaqueValue());
2719    W.Code = TYPE_EXT_QUAL;
2720    W.AbbrevToUse = TypeExtQualAbbrev;
2721  } else {
2722    switch (T->getTypeClass()) {
2723      // For all of the concrete, non-dependent types, call the
2724      // appropriate visitor function.
2725#define TYPE(Class, Base) \
2726    case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break;
2727#define ABSTRACT_TYPE(Class, Base)
2728#include "clang/AST/TypeNodes.def"
2729    }
2730  }
2731
2732  // Emit the serialized record.
2733  Stream.EmitRecord(W.Code, Record, W.AbbrevToUse);
2734
2735  // Flush any expressions that were written as part of this type.
2736  FlushStmts();
2737}
2738
2739//===----------------------------------------------------------------------===//
2740// Declaration Serialization
2741//===----------------------------------------------------------------------===//
2742
2743/// \brief Write the block containing all of the declaration IDs
2744/// lexically declared within the given DeclContext.
2745///
2746/// \returns the offset of the DECL_CONTEXT_LEXICAL block within the
2747/// bistream, or 0 if no block was written.
2748uint64_t ASTWriter::WriteDeclContextLexicalBlock(ASTContext &Context,
2749                                                 DeclContext *DC) {
2750  if (DC->decls_empty())
2751    return 0;
2752
2753  uint64_t Offset = Stream.GetCurrentBitNo();
2754  SmallVector<uint32_t, 128> KindDeclPairs;
2755  for (const auto *D : DC->decls()) {
2756    KindDeclPairs.push_back(D->getKind());
2757    KindDeclPairs.push_back(GetDeclRef(D));
2758  }
2759
2760  ++NumLexicalDeclContexts;
2761  RecordData::value_type Record[] = {DECL_CONTEXT_LEXICAL};
2762  Stream.EmitRecordWithBlob(DeclContextLexicalAbbrev, Record,
2763                            bytes(KindDeclPairs));
2764  return Offset;
2765}
2766
2767void ASTWriter::WriteTypeDeclOffsets() {
2768  using namespace llvm;
2769
2770  // Write the type offsets array
2771  auto *Abbrev = new BitCodeAbbrev();
2772  Abbrev->Add(BitCodeAbbrevOp(TYPE_OFFSET));
2773  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of types
2774  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base type index
2775  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // types block
2776  unsigned TypeOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2777  {
2778    RecordData::value_type Record[] = {TYPE_OFFSET, TypeOffsets.size(),
2779                                       FirstTypeID - NUM_PREDEF_TYPE_IDS};
2780    Stream.EmitRecordWithBlob(TypeOffsetAbbrev, Record, bytes(TypeOffsets));
2781  }
2782
2783  // Write the declaration offsets array
2784  Abbrev = new BitCodeAbbrev();
2785  Abbrev->Add(BitCodeAbbrevOp(DECL_OFFSET));
2786  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of declarations
2787  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // base decl ID
2788  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // declarations block
2789  unsigned DeclOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
2790  {
2791    RecordData::value_type Record[] = {DECL_OFFSET, DeclOffsets.size(),
2792                                       FirstDeclID - NUM_PREDEF_DECL_IDS};
2793    Stream.EmitRecordWithBlob(DeclOffsetAbbrev, Record, bytes(DeclOffsets));
2794  }
2795}
2796
2797void ASTWriter::WriteFileDeclIDsMap() {
2798  using namespace llvm;
2799
2800  SmallVector<std::pair<FileID, DeclIDInFileInfo *>, 64> SortedFileDeclIDs(
2801      FileDeclIDs.begin(), FileDeclIDs.end());
2802  std::sort(SortedFileDeclIDs.begin(), SortedFileDeclIDs.end(),
2803            llvm::less_first());
2804
2805  // Join the vectors of DeclIDs from all files.
2806  SmallVector<DeclID, 256> FileGroupedDeclIDs;
2807  for (auto &FileDeclEntry : SortedFileDeclIDs) {
2808    DeclIDInFileInfo &Info = *FileDeclEntry.second;
2809    Info.FirstDeclIndex = FileGroupedDeclIDs.size();
2810    for (auto &LocDeclEntry : Info.DeclIDs)
2811      FileGroupedDeclIDs.push_back(LocDeclEntry.second);
2812  }
2813
2814  auto *Abbrev = new BitCodeAbbrev();
2815  Abbrev->Add(BitCodeAbbrevOp(FILE_SORTED_DECLS));
2816  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
2817  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
2818  unsigned AbbrevCode = Stream.EmitAbbrev(Abbrev);
2819  RecordData::value_type Record[] = {FILE_SORTED_DECLS,
2820                                     FileGroupedDeclIDs.size()};
2821  Stream.EmitRecordWithBlob(AbbrevCode, Record, bytes(FileGroupedDeclIDs));
2822}
2823
2824void ASTWriter::WriteComments() {
2825  Stream.EnterSubblock(COMMENTS_BLOCK_ID, 3);
2826  ArrayRef<RawComment *> RawComments = Context->Comments.getComments();
2827  RecordData Record;
2828  for (const auto *I : RawComments) {
2829    Record.clear();
2830    AddSourceRange(I->getSourceRange(), Record);
2831    Record.push_back(I->getKind());
2832    Record.push_back(I->isTrailingComment());
2833    Record.push_back(I->isAlmostTrailingComment());
2834    Stream.EmitRecord(COMMENTS_RAW_COMMENT, Record);
2835  }
2836  Stream.ExitBlock();
2837}
2838
2839//===----------------------------------------------------------------------===//
2840// Global Method Pool and Selector Serialization
2841//===----------------------------------------------------------------------===//
2842
2843namespace {
2844// Trait used for the on-disk hash table used in the method pool.
2845class ASTMethodPoolTrait {
2846  ASTWriter &Writer;
2847
2848public:
2849  typedef Selector key_type;
2850  typedef key_type key_type_ref;
2851
2852  struct data_type {
2853    SelectorID ID;
2854    ObjCMethodList Instance, Factory;
2855  };
2856  typedef const data_type& data_type_ref;
2857
2858  typedef unsigned hash_value_type;
2859  typedef unsigned offset_type;
2860
2861  explicit ASTMethodPoolTrait(ASTWriter &Writer) : Writer(Writer) { }
2862
2863  static hash_value_type ComputeHash(Selector Sel) {
2864    return serialization::ComputeHash(Sel);
2865  }
2866
2867  std::pair<unsigned,unsigned>
2868    EmitKeyDataLength(raw_ostream& Out, Selector Sel,
2869                      data_type_ref Methods) {
2870    using namespace llvm::support;
2871    endian::Writer<little> LE(Out);
2872    unsigned KeyLen = 2 + (Sel.getNumArgs()? Sel.getNumArgs() * 4 : 4);
2873    LE.write<uint16_t>(KeyLen);
2874    unsigned DataLen = 4 + 2 + 2; // 2 bytes for each of the method counts
2875    for (const ObjCMethodList *Method = &Methods.Instance; Method;
2876         Method = Method->getNext())
2877      if (Method->getMethod())
2878        DataLen += 4;
2879    for (const ObjCMethodList *Method = &Methods.Factory; Method;
2880         Method = Method->getNext())
2881      if (Method->getMethod())
2882        DataLen += 4;
2883    LE.write<uint16_t>(DataLen);
2884    return std::make_pair(KeyLen, DataLen);
2885  }
2886
2887  void EmitKey(raw_ostream& Out, Selector Sel, unsigned) {
2888    using namespace llvm::support;
2889    endian::Writer<little> LE(Out);
2890    uint64_t Start = Out.tell();
2891    assert((Start >> 32) == 0 && "Selector key offset too large");
2892    Writer.SetSelectorOffset(Sel, Start);
2893    unsigned N = Sel.getNumArgs();
2894    LE.write<uint16_t>(N);
2895    if (N == 0)
2896      N = 1;
2897    for (unsigned I = 0; I != N; ++I)
2898      LE.write<uint32_t>(
2899          Writer.getIdentifierRef(Sel.getIdentifierInfoForSlot(I)));
2900  }
2901
2902  void EmitData(raw_ostream& Out, key_type_ref,
2903                data_type_ref Methods, unsigned DataLen) {
2904    using namespace llvm::support;
2905    endian::Writer<little> LE(Out);
2906    uint64_t Start = Out.tell(); (void)Start;
2907    LE.write<uint32_t>(Methods.ID);
2908    unsigned NumInstanceMethods = 0;
2909    for (const ObjCMethodList *Method = &Methods.Instance; Method;
2910         Method = Method->getNext())
2911      if (Method->getMethod())
2912        ++NumInstanceMethods;
2913
2914    unsigned NumFactoryMethods = 0;
2915    for (const ObjCMethodList *Method = &Methods.Factory; Method;
2916         Method = Method->getNext())
2917      if (Method->getMethod())
2918        ++NumFactoryMethods;
2919
2920    unsigned InstanceBits = Methods.Instance.getBits();
2921    assert(InstanceBits < 4);
2922    unsigned InstanceHasMoreThanOneDeclBit =
2923        Methods.Instance.hasMoreThanOneDecl();
2924    unsigned FullInstanceBits = (NumInstanceMethods << 3) |
2925                                (InstanceHasMoreThanOneDeclBit << 2) |
2926                                InstanceBits;
2927    unsigned FactoryBits = Methods.Factory.getBits();
2928    assert(FactoryBits < 4);
2929    unsigned FactoryHasMoreThanOneDeclBit =
2930        Methods.Factory.hasMoreThanOneDecl();
2931    unsigned FullFactoryBits = (NumFactoryMethods << 3) |
2932                               (FactoryHasMoreThanOneDeclBit << 2) |
2933                               FactoryBits;
2934    LE.write<uint16_t>(FullInstanceBits);
2935    LE.write<uint16_t>(FullFactoryBits);
2936    for (const ObjCMethodList *Method = &Methods.Instance; Method;
2937         Method = Method->getNext())
2938      if (Method->getMethod())
2939        LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
2940    for (const ObjCMethodList *Method = &Methods.Factory; Method;
2941         Method = Method->getNext())
2942      if (Method->getMethod())
2943        LE.write<uint32_t>(Writer.getDeclID(Method->getMethod()));
2944
2945    assert(Out.tell() - Start == DataLen && "Data length is wrong");
2946  }
2947};
2948} // end anonymous namespace
2949
2950/// \brief Write ObjC data: selectors and the method pool.
2951///
2952/// The method pool contains both instance and factory methods, stored
2953/// in an on-disk hash table indexed by the selector. The hash table also
2954/// contains an empty entry for every other selector known to Sema.
2955void ASTWriter::WriteSelectors(Sema &SemaRef) {
2956  using namespace llvm;
2957
2958  // Do we have to do anything at all?
2959  if (SemaRef.MethodPool.empty() && SelectorIDs.empty())
2960    return;
2961  unsigned NumTableEntries = 0;
2962  // Create and write out the blob that contains selectors and the method pool.
2963  {
2964    llvm::OnDiskChainedHashTableGenerator<ASTMethodPoolTrait> Generator;
2965    ASTMethodPoolTrait Trait(*this);
2966
2967    // Create the on-disk hash table representation. We walk through every
2968    // selector we've seen and look it up in the method pool.
2969    SelectorOffsets.resize(NextSelectorID - FirstSelectorID);
2970    for (auto &SelectorAndID : SelectorIDs) {
2971      Selector S = SelectorAndID.first;
2972      SelectorID ID = SelectorAndID.second;
2973      Sema::GlobalMethodPool::iterator F = SemaRef.MethodPool.find(S);
2974      ASTMethodPoolTrait::data_type Data = {
2975        ID,
2976        ObjCMethodList(),
2977        ObjCMethodList()
2978      };
2979      if (F != SemaRef.MethodPool.end()) {
2980        Data.Instance = F->second.first;
2981        Data.Factory = F->second.second;
2982      }
2983      // Only write this selector if it's not in an existing AST or something
2984      // changed.
2985      if (Chain && ID < FirstSelectorID) {
2986        // Selector already exists. Did it change?
2987        bool changed = false;
2988        for (ObjCMethodList *M = &Data.Instance;
2989             !changed && M && M->getMethod(); M = M->getNext()) {
2990          if (!M->getMethod()->isFromASTFile())
2991            changed = true;
2992        }
2993        for (ObjCMethodList *M = &Data.Factory; !changed && M && M->getMethod();
2994             M = M->getNext()) {
2995          if (!M->getMethod()->isFromASTFile())
2996            changed = true;
2997        }
2998        if (!changed)
2999          continue;
3000      } else if (Data.Instance.getMethod() || Data.Factory.getMethod()) {
3001        // A new method pool entry.
3002        ++NumTableEntries;
3003      }
3004      Generator.insert(S, Data, Trait);
3005    }
3006
3007    // Create the on-disk hash table in a buffer.
3008    SmallString<4096> MethodPool;
3009    uint32_t BucketOffset;
3010    {
3011      using namespace llvm::support;
3012      ASTMethodPoolTrait Trait(*this);
3013      llvm::raw_svector_ostream Out(MethodPool);
3014      // Make sure that no bucket is at offset 0
3015      endian::Writer<little>(Out).write<uint32_t>(0);
3016      BucketOffset = Generator.Emit(Out, Trait);
3017    }
3018
3019    // Create a blob abbreviation
3020    auto *Abbrev = new BitCodeAbbrev();
3021    Abbrev->Add(BitCodeAbbrevOp(METHOD_POOL));
3022    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3023    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3024    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3025    unsigned MethodPoolAbbrev = Stream.EmitAbbrev(Abbrev);
3026
3027    // Write the method pool
3028    {
3029      RecordData::value_type Record[] = {METHOD_POOL, BucketOffset,
3030                                         NumTableEntries};
3031      Stream.EmitRecordWithBlob(MethodPoolAbbrev, Record, MethodPool);
3032    }
3033
3034    // Create a blob abbreviation for the selector table offsets.
3035    Abbrev = new BitCodeAbbrev();
3036    Abbrev->Add(BitCodeAbbrevOp(SELECTOR_OFFSETS));
3037    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // size
3038    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3039    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3040    unsigned SelectorOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3041
3042    // Write the selector offsets table.
3043    {
3044      RecordData::value_type Record[] = {
3045          SELECTOR_OFFSETS, SelectorOffsets.size(),
3046          FirstSelectorID - NUM_PREDEF_SELECTOR_IDS};
3047      Stream.EmitRecordWithBlob(SelectorOffsetAbbrev, Record,
3048                                bytes(SelectorOffsets));
3049    }
3050  }
3051}
3052
3053/// \brief Write the selectors referenced in @selector expression into AST file.
3054void ASTWriter::WriteReferencedSelectorsPool(Sema &SemaRef) {
3055  using namespace llvm;
3056  if (SemaRef.ReferencedSelectors.empty())
3057    return;
3058
3059  RecordData Record;
3060
3061  // Note: this writes out all references even for a dependent AST. But it is
3062  // very tricky to fix, and given that @selector shouldn't really appear in
3063  // headers, probably not worth it. It's not a correctness issue.
3064  for (auto &SelectorAndLocation : SemaRef.ReferencedSelectors) {
3065    Selector Sel = SelectorAndLocation.first;
3066    SourceLocation Loc = SelectorAndLocation.second;
3067    AddSelectorRef(Sel, Record);
3068    AddSourceLocation(Loc, Record);
3069  }
3070  Stream.EmitRecord(REFERENCED_SELECTOR_POOL, Record);
3071}
3072
3073//===----------------------------------------------------------------------===//
3074// Identifier Table Serialization
3075//===----------------------------------------------------------------------===//
3076
3077/// Determine the declaration that should be put into the name lookup table to
3078/// represent the given declaration in this module. This is usually D itself,
3079/// but if D was imported and merged into a local declaration, we want the most
3080/// recent local declaration instead. The chosen declaration will be the most
3081/// recent declaration in any module that imports this one.
3082static NamedDecl *getDeclForLocalLookup(const LangOptions &LangOpts,
3083                                        NamedDecl *D) {
3084  if (!LangOpts.Modules || !D->isFromASTFile())
3085    return D;
3086
3087  if (Decl *Redecl = D->getPreviousDecl()) {
3088    // For Redeclarable decls, a prior declaration might be local.
3089    for (; Redecl; Redecl = Redecl->getPreviousDecl()) {
3090      if (!Redecl->isFromASTFile())
3091        return cast<NamedDecl>(Redecl);
3092      // If we find a decl from a (chained-)PCH stop since we won't find a
3093      // local one.
3094      if (D->getOwningModuleID() == 0)
3095        break;
3096    }
3097  } else if (Decl *First = D->getCanonicalDecl()) {
3098    // For Mergeable decls, the first decl might be local.
3099    if (!First->isFromASTFile())
3100      return cast<NamedDecl>(First);
3101  }
3102
3103  // All declarations are imported. Our most recent declaration will also be
3104  // the most recent one in anyone who imports us.
3105  return D;
3106}
3107
3108namespace {
3109class ASTIdentifierTableTrait {
3110  ASTWriter &Writer;
3111  Preprocessor &PP;
3112  IdentifierResolver &IdResolver;
3113  bool IsModule;
3114  bool NeedDecls;
3115  ASTWriter::RecordData *InterestingIdentifierOffsets;
3116
3117  /// \brief Determines whether this is an "interesting" identifier that needs a
3118  /// full IdentifierInfo structure written into the hash table. Notably, this
3119  /// doesn't check whether the name has macros defined; use PublicMacroIterator
3120  /// to check that.
3121  bool isInterestingIdentifier(const IdentifierInfo *II, uint64_t MacroOffset) {
3122    if (MacroOffset ||
3123        II->isPoisoned() ||
3124        (IsModule ? II->hasRevertedBuiltin() : II->getObjCOrBuiltinID()) ||
3125        II->hasRevertedTokenIDToIdentifier() ||
3126        (NeedDecls && II->getFETokenInfo<void>()))
3127      return true;
3128
3129    return false;
3130  }
3131
3132public:
3133  typedef IdentifierInfo* key_type;
3134  typedef key_type  key_type_ref;
3135
3136  typedef IdentID data_type;
3137  typedef data_type data_type_ref;
3138
3139  typedef unsigned hash_value_type;
3140  typedef unsigned offset_type;
3141
3142  ASTIdentifierTableTrait(ASTWriter &Writer, Preprocessor &PP,
3143                          IdentifierResolver &IdResolver, bool IsModule,
3144                          ASTWriter::RecordData *InterestingIdentifierOffsets)
3145      : Writer(Writer), PP(PP), IdResolver(IdResolver), IsModule(IsModule),
3146        NeedDecls(!IsModule || !Writer.getLangOpts().CPlusPlus),
3147        InterestingIdentifierOffsets(InterestingIdentifierOffsets) {}
3148
3149  static hash_value_type ComputeHash(const IdentifierInfo* II) {
3150    return llvm::HashString(II->getName());
3151  }
3152
3153  bool isInterestingIdentifier(const IdentifierInfo *II) {
3154    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3155    return isInterestingIdentifier(II, MacroOffset);
3156  }
3157  bool isInterestingNonMacroIdentifier(const IdentifierInfo *II) {
3158    return isInterestingIdentifier(II, 0);
3159  }
3160
3161  std::pair<unsigned,unsigned>
3162  EmitKeyDataLength(raw_ostream& Out, IdentifierInfo* II, IdentID ID) {
3163    unsigned KeyLen = II->getLength() + 1;
3164    unsigned DataLen = 4; // 4 bytes for the persistent ID << 1
3165    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3166    if (isInterestingIdentifier(II, MacroOffset)) {
3167      DataLen += 2; // 2 bytes for builtin ID
3168      DataLen += 2; // 2 bytes for flags
3169      if (MacroOffset)
3170        DataLen += 4; // MacroDirectives offset.
3171
3172      if (NeedDecls) {
3173        for (IdentifierResolver::iterator D = IdResolver.begin(II),
3174                                       DEnd = IdResolver.end();
3175             D != DEnd; ++D)
3176          DataLen += 4;
3177      }
3178    }
3179    using namespace llvm::support;
3180    endian::Writer<little> LE(Out);
3181
3182    assert((uint16_t)DataLen == DataLen && (uint16_t)KeyLen == KeyLen);
3183    LE.write<uint16_t>(DataLen);
3184    // We emit the key length after the data length so that every
3185    // string is preceded by a 16-bit length. This matches the PTH
3186    // format for storing identifiers.
3187    LE.write<uint16_t>(KeyLen);
3188    return std::make_pair(KeyLen, DataLen);
3189  }
3190
3191  void EmitKey(raw_ostream& Out, const IdentifierInfo* II,
3192               unsigned KeyLen) {
3193    // Record the location of the key data.  This is used when generating
3194    // the mapping from persistent IDs to strings.
3195    Writer.SetIdentifierOffset(II, Out.tell());
3196
3197    // Emit the offset of the key/data length information to the interesting
3198    // identifiers table if necessary.
3199    if (InterestingIdentifierOffsets && isInterestingIdentifier(II))
3200      InterestingIdentifierOffsets->push_back(Out.tell() - 4);
3201
3202    Out.write(II->getNameStart(), KeyLen);
3203  }
3204
3205  void EmitData(raw_ostream& Out, IdentifierInfo* II,
3206                IdentID ID, unsigned) {
3207    using namespace llvm::support;
3208    endian::Writer<little> LE(Out);
3209
3210    auto MacroOffset = Writer.getMacroDirectivesOffset(II);
3211    if (!isInterestingIdentifier(II, MacroOffset)) {
3212      LE.write<uint32_t>(ID << 1);
3213      return;
3214    }
3215
3216    LE.write<uint32_t>((ID << 1) | 0x01);
3217    uint32_t Bits = (uint32_t)II->getObjCOrBuiltinID();
3218    assert((Bits & 0xffff) == Bits && "ObjCOrBuiltinID too big for ASTReader.");
3219    LE.write<uint16_t>(Bits);
3220    Bits = 0;
3221    bool HadMacroDefinition = MacroOffset != 0;
3222    Bits = (Bits << 1) | unsigned(HadMacroDefinition);
3223    Bits = (Bits << 1) | unsigned(II->isExtensionToken());
3224    Bits = (Bits << 1) | unsigned(II->isPoisoned());
3225    Bits = (Bits << 1) | unsigned(II->hasRevertedBuiltin());
3226    Bits = (Bits << 1) | unsigned(II->hasRevertedTokenIDToIdentifier());
3227    Bits = (Bits << 1) | unsigned(II->isCPlusPlusOperatorKeyword());
3228    LE.write<uint16_t>(Bits);
3229
3230    if (HadMacroDefinition)
3231      LE.write<uint32_t>(MacroOffset);
3232
3233    if (NeedDecls) {
3234      // Emit the declaration IDs in reverse order, because the
3235      // IdentifierResolver provides the declarations as they would be
3236      // visible (e.g., the function "stat" would come before the struct
3237      // "stat"), but the ASTReader adds declarations to the end of the list
3238      // (so we need to see the struct "stat" before the function "stat").
3239      // Only emit declarations that aren't from a chained PCH, though.
3240      SmallVector<NamedDecl *, 16> Decls(IdResolver.begin(II),
3241                                         IdResolver.end());
3242      for (SmallVectorImpl<NamedDecl *>::reverse_iterator D = Decls.rbegin(),
3243                                                          DEnd = Decls.rend();
3244           D != DEnd; ++D)
3245        LE.write<uint32_t>(
3246            Writer.getDeclID(getDeclForLocalLookup(PP.getLangOpts(), *D)));
3247    }
3248  }
3249};
3250} // end anonymous namespace
3251
3252/// \brief Write the identifier table into the AST file.
3253///
3254/// The identifier table consists of a blob containing string data
3255/// (the actual identifiers themselves) and a separate "offsets" index
3256/// that maps identifier IDs to locations within the blob.
3257void ASTWriter::WriteIdentifierTable(Preprocessor &PP,
3258                                     IdentifierResolver &IdResolver,
3259                                     bool IsModule) {
3260  using namespace llvm;
3261
3262  RecordData InterestingIdents;
3263
3264  // Create and write out the blob that contains the identifier
3265  // strings.
3266  {
3267    llvm::OnDiskChainedHashTableGenerator<ASTIdentifierTableTrait> Generator;
3268    ASTIdentifierTableTrait Trait(
3269        *this, PP, IdResolver, IsModule,
3270        (getLangOpts().CPlusPlus && IsModule) ? &InterestingIdents : nullptr);
3271
3272    // Look for any identifiers that were named while processing the
3273    // headers, but are otherwise not needed. We add these to the hash
3274    // table to enable checking of the predefines buffer in the case
3275    // where the user adds new macro definitions when building the AST
3276    // file.
3277    SmallVector<const IdentifierInfo *, 128> IIs;
3278    for (const auto &ID : PP.getIdentifierTable())
3279      IIs.push_back(ID.second);
3280    // Sort the identifiers lexicographically before getting them references so
3281    // that their order is stable.
3282    std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
3283    for (const IdentifierInfo *II : IIs)
3284      if (Trait.isInterestingNonMacroIdentifier(II))
3285        getIdentifierRef(II);
3286
3287    // Create the on-disk hash table representation. We only store offsets
3288    // for identifiers that appear here for the first time.
3289    IdentifierOffsets.resize(NextIdentID - FirstIdentID);
3290    for (auto IdentIDPair : IdentifierIDs) {
3291      auto *II = const_cast<IdentifierInfo *>(IdentIDPair.first);
3292      IdentID ID = IdentIDPair.second;
3293      assert(II && "NULL identifier in identifier table");
3294      if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
3295        Generator.insert(II, ID, Trait);
3296    }
3297
3298    // Create the on-disk hash table in a buffer.
3299    SmallString<4096> IdentifierTable;
3300    uint32_t BucketOffset;
3301    {
3302      using namespace llvm::support;
3303      llvm::raw_svector_ostream Out(IdentifierTable);
3304      // Make sure that no bucket is at offset 0
3305      endian::Writer<little>(Out).write<uint32_t>(0);
3306      BucketOffset = Generator.Emit(Out, Trait);
3307    }
3308
3309    // Create a blob abbreviation
3310    auto *Abbrev = new BitCodeAbbrev();
3311    Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_TABLE));
3312    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32));
3313    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3314    unsigned IDTableAbbrev = Stream.EmitAbbrev(Abbrev);
3315
3316    // Write the identifier table
3317    RecordData::value_type Record[] = {IDENTIFIER_TABLE, BucketOffset};
3318    Stream.EmitRecordWithBlob(IDTableAbbrev, Record, IdentifierTable);
3319  }
3320
3321  // Write the offsets table for identifier IDs.
3322  auto *Abbrev = new BitCodeAbbrev();
3323  Abbrev->Add(BitCodeAbbrevOp(IDENTIFIER_OFFSET));
3324  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // # of identifiers
3325  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 32)); // first ID
3326  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3327  unsigned IdentifierOffsetAbbrev = Stream.EmitAbbrev(Abbrev);
3328
3329#ifndef NDEBUG
3330  for (unsigned I = 0, N = IdentifierOffsets.size(); I != N; ++I)
3331    assert(IdentifierOffsets[I] && "Missing identifier offset?");
3332#endif
3333
3334  RecordData::value_type Record[] = {IDENTIFIER_OFFSET,
3335                                     IdentifierOffsets.size(),
3336                                     FirstIdentID - NUM_PREDEF_IDENT_IDS};
3337  Stream.EmitRecordWithBlob(IdentifierOffsetAbbrev, Record,
3338                            bytes(IdentifierOffsets));
3339
3340  // In C++, write the list of interesting identifiers (those that are
3341  // defined as macros, poisoned, or similar unusual things).
3342  if (!InterestingIdents.empty())
3343    Stream.EmitRecord(INTERESTING_IDENTIFIERS, InterestingIdents);
3344}
3345
3346//===----------------------------------------------------------------------===//
3347// DeclContext's Name Lookup Table Serialization
3348//===----------------------------------------------------------------------===//
3349
3350namespace {
3351// Trait used for the on-disk hash table used in the method pool.
3352class ASTDeclContextNameLookupTrait {
3353  ASTWriter &Writer;
3354  llvm::SmallVector<DeclID, 64> DeclIDs;
3355
3356public:
3357  typedef DeclarationNameKey key_type;
3358  typedef key_type key_type_ref;
3359
3360  /// A start and end index into DeclIDs, representing a sequence of decls.
3361  typedef std::pair<unsigned, unsigned> data_type;
3362  typedef const data_type& data_type_ref;
3363
3364  typedef unsigned hash_value_type;
3365  typedef unsigned offset_type;
3366
3367  explicit ASTDeclContextNameLookupTrait(ASTWriter &Writer) : Writer(Writer) { }
3368
3369  template<typename Coll>
3370  data_type getData(const Coll &Decls) {
3371    unsigned Start = DeclIDs.size();
3372    for (NamedDecl *D : Decls) {
3373      DeclIDs.push_back(
3374          Writer.GetDeclRef(getDeclForLocalLookup(Writer.getLangOpts(), D)));
3375    }
3376    return std::make_pair(Start, DeclIDs.size());
3377  }
3378
3379  data_type ImportData(const reader::ASTDeclContextNameLookupTrait::data_type &FromReader) {
3380    unsigned Start = DeclIDs.size();
3381    for (auto ID : FromReader)
3382      DeclIDs.push_back(ID);
3383    return std::make_pair(Start, DeclIDs.size());
3384  }
3385
3386  static bool EqualKey(key_type_ref a, key_type_ref b) {
3387    return a == b;
3388  }
3389
3390  hash_value_type ComputeHash(DeclarationNameKey Name) {
3391    return Name.getHash();
3392  }
3393
3394  void EmitFileRef(raw_ostream &Out, ModuleFile *F) const {
3395    assert(Writer.hasChain() &&
3396           "have reference to loaded module file but no chain?");
3397
3398    using namespace llvm::support;
3399    endian::Writer<little>(Out)
3400        .write<uint32_t>(Writer.getChain()->getModuleFileID(F));
3401  }
3402
3403  std::pair<unsigned, unsigned> EmitKeyDataLength(raw_ostream &Out,
3404                                                  DeclarationNameKey Name,
3405                                                  data_type_ref Lookup) {
3406    using namespace llvm::support;
3407    endian::Writer<little> LE(Out);
3408    unsigned KeyLen = 1;
3409    switch (Name.getKind()) {
3410    case DeclarationName::Identifier:
3411    case DeclarationName::ObjCZeroArgSelector:
3412    case DeclarationName::ObjCOneArgSelector:
3413    case DeclarationName::ObjCMultiArgSelector:
3414    case DeclarationName::CXXLiteralOperatorName:
3415      KeyLen += 4;
3416      break;
3417    case DeclarationName::CXXOperatorName:
3418      KeyLen += 1;
3419      break;
3420    case DeclarationName::CXXConstructorName:
3421    case DeclarationName::CXXDestructorName:
3422    case DeclarationName::CXXConversionFunctionName:
3423    case DeclarationName::CXXUsingDirective:
3424      break;
3425    }
3426    LE.write<uint16_t>(KeyLen);
3427
3428    // 4 bytes for each DeclID.
3429    unsigned DataLen = 4 * (Lookup.second - Lookup.first);
3430    assert(uint16_t(DataLen) == DataLen &&
3431           "too many decls for serialized lookup result");
3432    LE.write<uint16_t>(DataLen);
3433
3434    return std::make_pair(KeyLen, DataLen);
3435  }
3436
3437  void EmitKey(raw_ostream &Out, DeclarationNameKey Name, unsigned) {
3438    using namespace llvm::support;
3439    endian::Writer<little> LE(Out);
3440    LE.write<uint8_t>(Name.getKind());
3441    switch (Name.getKind()) {
3442    case DeclarationName::Identifier:
3443    case DeclarationName::CXXLiteralOperatorName:
3444      LE.write<uint32_t>(Writer.getIdentifierRef(Name.getIdentifier()));
3445      return;
3446    case DeclarationName::ObjCZeroArgSelector:
3447    case DeclarationName::ObjCOneArgSelector:
3448    case DeclarationName::ObjCMultiArgSelector:
3449      LE.write<uint32_t>(Writer.getSelectorRef(Name.getSelector()));
3450      return;
3451    case DeclarationName::CXXOperatorName:
3452      assert(Name.getOperatorKind() < NUM_OVERLOADED_OPERATORS &&
3453             "Invalid operator?");
3454      LE.write<uint8_t>(Name.getOperatorKind());
3455      return;
3456    case DeclarationName::CXXConstructorName:
3457    case DeclarationName::CXXDestructorName:
3458    case DeclarationName::CXXConversionFunctionName:
3459    case DeclarationName::CXXUsingDirective:
3460      return;
3461    }
3462
3463    llvm_unreachable("Invalid name kind?");
3464  }
3465
3466  void EmitData(raw_ostream &Out, key_type_ref, data_type Lookup,
3467                unsigned DataLen) {
3468    using namespace llvm::support;
3469    endian::Writer<little> LE(Out);
3470    uint64_t Start = Out.tell(); (void)Start;
3471    for (unsigned I = Lookup.first, N = Lookup.second; I != N; ++I)
3472      LE.write<uint32_t>(DeclIDs[I]);
3473    assert(Out.tell() - Start == DataLen && "Data length is wrong");
3474  }
3475};
3476} // end anonymous namespace
3477
3478bool ASTWriter::isLookupResultExternal(StoredDeclsList &Result,
3479                                       DeclContext *DC) {
3480  return Result.hasExternalDecls() && DC->NeedToReconcileExternalVisibleStorage;
3481}
3482
3483bool ASTWriter::isLookupResultEntirelyExternal(StoredDeclsList &Result,
3484                                               DeclContext *DC) {
3485  for (auto *D : Result.getLookupResult())
3486    if (!getDeclForLocalLookup(getLangOpts(), D)->isFromASTFile())
3487      return false;
3488
3489  return true;
3490}
3491
3492void
3493ASTWriter::GenerateNameLookupTable(const DeclContext *ConstDC,
3494                                   llvm::SmallVectorImpl<char> &LookupTable) {
3495  assert(!ConstDC->HasLazyLocalLexicalLookups &&
3496         !ConstDC->HasLazyExternalLexicalLookups &&
3497         "must call buildLookups first");
3498
3499  // FIXME: We need to build the lookups table, which is logically const.
3500  auto *DC = const_cast<DeclContext*>(ConstDC);
3501  assert(DC == DC->getPrimaryContext() && "only primary DC has lookup table");
3502
3503  // Create the on-disk hash table representation.
3504  MultiOnDiskHashTableGenerator<reader::ASTDeclContextNameLookupTrait,
3505                                ASTDeclContextNameLookupTrait> Generator;
3506  ASTDeclContextNameLookupTrait Trait(*this);
3507
3508  // The first step is to collect the declaration names which we need to
3509  // serialize into the name lookup table, and to collect them in a stable
3510  // order.
3511  SmallVector<DeclarationName, 16> Names;
3512
3513  // We also build up small sets of the constructor and conversion function
3514  // names which are visible.
3515  llvm::SmallSet<DeclarationName, 8> ConstructorNameSet, ConversionNameSet;
3516
3517  for (auto &Lookup : *DC->buildLookup()) {
3518    auto &Name = Lookup.first;
3519    auto &Result = Lookup.second;
3520
3521    // If there are no local declarations in our lookup result, we
3522    // don't need to write an entry for the name at all. If we can't
3523    // write out a lookup set without performing more deserialization,
3524    // just skip this entry.
3525    if (isLookupResultExternal(Result, DC) &&
3526        isLookupResultEntirelyExternal(Result, DC))
3527      continue;
3528
3529    // We also skip empty results. If any of the results could be external and
3530    // the currently available results are empty, then all of the results are
3531    // external and we skip it above. So the only way we get here with an empty
3532    // results is when no results could have been external *and* we have
3533    // external results.
3534    //
3535    // FIXME: While we might want to start emitting on-disk entries for negative
3536    // lookups into a decl context as an optimization, today we *have* to skip
3537    // them because there are names with empty lookup results in decl contexts
3538    // which we can't emit in any stable ordering: we lookup constructors and
3539    // conversion functions in the enclosing namespace scope creating empty
3540    // results for them. This in almost certainly a bug in Clang's name lookup,
3541    // but that is likely to be hard or impossible to fix and so we tolerate it
3542    // here by omitting lookups with empty results.
3543    if (Lookup.second.getLookupResult().empty())
3544      continue;
3545
3546    switch (Lookup.first.getNameKind()) {
3547    default:
3548      Names.push_back(Lookup.first);
3549      break;
3550
3551    case DeclarationName::CXXConstructorName:
3552      assert(isa<CXXRecordDecl>(DC) &&
3553             "Cannot have a constructor name outside of a class!");
3554      ConstructorNameSet.insert(Name);
3555      break;
3556
3557    case DeclarationName::CXXConversionFunctionName:
3558      assert(isa<CXXRecordDecl>(DC) &&
3559             "Cannot have a conversion function name outside of a class!");
3560      ConversionNameSet.insert(Name);
3561      break;
3562    }
3563  }
3564
3565  // Sort the names into a stable order.
3566  std::sort(Names.begin(), Names.end());
3567
3568  if (auto *D = dyn_cast<CXXRecordDecl>(DC)) {
3569    // We need to establish an ordering of constructor and conversion function
3570    // names, and they don't have an intrinsic ordering.
3571
3572    // First we try the easy case by forming the current context's constructor
3573    // name and adding that name first. This is a very useful optimization to
3574    // avoid walking the lexical declarations in many cases, and it also
3575    // handles the only case where a constructor name can come from some other
3576    // lexical context -- when that name is an implicit constructor merged from
3577    // another declaration in the redecl chain. Any non-implicit constructor or
3578    // conversion function which doesn't occur in all the lexical contexts
3579    // would be an ODR violation.
3580    auto ImplicitCtorName = Context->DeclarationNames.getCXXConstructorName(
3581        Context->getCanonicalType(Context->getRecordType(D)));
3582    if (ConstructorNameSet.erase(ImplicitCtorName))
3583      Names.push_back(ImplicitCtorName);
3584
3585    // If we still have constructors or conversion functions, we walk all the
3586    // names in the decl and add the constructors and conversion functions
3587    // which are visible in the order they lexically occur within the context.
3588    if (!ConstructorNameSet.empty() || !ConversionNameSet.empty())
3589      for (Decl *ChildD : cast<CXXRecordDecl>(DC)->decls())
3590        if (auto *ChildND = dyn_cast<NamedDecl>(ChildD)) {
3591          auto Name = ChildND->getDeclName();
3592          switch (Name.getNameKind()) {
3593          default:
3594            continue;
3595
3596          case DeclarationName::CXXConstructorName:
3597            if (ConstructorNameSet.erase(Name))
3598              Names.push_back(Name);
3599            break;
3600
3601          case DeclarationName::CXXConversionFunctionName:
3602            if (ConversionNameSet.erase(Name))
3603              Names.push_back(Name);
3604            break;
3605          }
3606
3607          if (ConstructorNameSet.empty() && ConversionNameSet.empty())
3608            break;
3609        }
3610
3611    assert(ConstructorNameSet.empty() && "Failed to find all of the visible "
3612                                         "constructors by walking all the "
3613                                         "lexical members of the context.");
3614    assert(ConversionNameSet.empty() && "Failed to find all of the visible "
3615                                        "conversion functions by walking all "
3616                                        "the lexical members of the context.");
3617  }
3618
3619  // Next we need to do a lookup with each name into this decl context to fully
3620  // populate any results from external sources. We don't actually use the
3621  // results of these lookups because we only want to use the results after all
3622  // results have been loaded and the pointers into them will be stable.
3623  for (auto &Name : Names)
3624    DC->lookup(Name);
3625
3626  // Now we need to insert the results for each name into the hash table. For
3627  // constructor names and conversion function names, we actually need to merge
3628  // all of the results for them into one list of results each and insert
3629  // those.
3630  SmallVector<NamedDecl *, 8> ConstructorDecls;
3631  SmallVector<NamedDecl *, 8> ConversionDecls;
3632
3633  // Now loop over the names, either inserting them or appending for the two
3634  // special cases.
3635  for (auto &Name : Names) {
3636    DeclContext::lookup_result Result = DC->noload_lookup(Name);
3637
3638    switch (Name.getNameKind()) {
3639    default:
3640      Generator.insert(Name, Trait.getData(Result), Trait);
3641      break;
3642
3643    case DeclarationName::CXXConstructorName:
3644      ConstructorDecls.append(Result.begin(), Result.end());
3645      break;
3646
3647    case DeclarationName::CXXConversionFunctionName:
3648      ConversionDecls.append(Result.begin(), Result.end());
3649      break;
3650    }
3651  }
3652
3653  // Handle our two special cases if we ended up having any. We arbitrarily use
3654  // the first declaration's name here because the name itself isn't part of
3655  // the key, only the kind of name is used.
3656  if (!ConstructorDecls.empty())
3657    Generator.insert(ConstructorDecls.front()->getDeclName(),
3658                     Trait.getData(ConstructorDecls), Trait);
3659  if (!ConversionDecls.empty())
3660    Generator.insert(ConversionDecls.front()->getDeclName(),
3661                     Trait.getData(ConversionDecls), Trait);
3662
3663  // Create the on-disk hash table. Also emit the existing imported and
3664  // merged table if there is one.
3665  auto *Lookups = Chain ? Chain->getLoadedLookupTables(DC) : nullptr;
3666  Generator.emit(LookupTable, Trait, Lookups ? &Lookups->Table : nullptr);
3667}
3668
3669/// \brief Write the block containing all of the declaration IDs
3670/// visible from the given DeclContext.
3671///
3672/// \returns the offset of the DECL_CONTEXT_VISIBLE block within the
3673/// bitstream, or 0 if no block was written.
3674uint64_t ASTWriter::WriteDeclContextVisibleBlock(ASTContext &Context,
3675                                                 DeclContext *DC) {
3676  // If we imported a key declaration of this namespace, write the visible
3677  // lookup results as an update record for it rather than including them
3678  // on this declaration. We will only look at key declarations on reload.
3679  if (isa<NamespaceDecl>(DC) && Chain &&
3680      Chain->getKeyDeclaration(cast<Decl>(DC))->isFromASTFile()) {
3681    // Only do this once, for the first local declaration of the namespace.
3682    for (auto *Prev = cast<NamespaceDecl>(DC)->getPreviousDecl(); Prev;
3683         Prev = Prev->getPreviousDecl())
3684      if (!Prev->isFromASTFile())
3685        return 0;
3686
3687    // Note that we need to emit an update record for the primary context.
3688    UpdatedDeclContexts.insert(DC->getPrimaryContext());
3689
3690    // Make sure all visible decls are written. They will be recorded later. We
3691    // do this using a side data structure so we can sort the names into
3692    // a deterministic order.
3693    StoredDeclsMap *Map = DC->getPrimaryContext()->buildLookup();
3694    SmallVector<std::pair<DeclarationName, DeclContext::lookup_result>, 16>
3695        LookupResults;
3696    if (Map) {
3697      LookupResults.reserve(Map->size());
3698      for (auto &Entry : *Map)
3699        LookupResults.push_back(
3700            std::make_pair(Entry.first, Entry.second.getLookupResult()));
3701    }
3702
3703    std::sort(LookupResults.begin(), LookupResults.end(), llvm::less_first());
3704    for (auto &NameAndResult : LookupResults) {
3705      DeclarationName Name = NameAndResult.first;
3706      DeclContext::lookup_result Result = NameAndResult.second;
3707      if (Name.getNameKind() == DeclarationName::CXXConstructorName ||
3708          Name.getNameKind() == DeclarationName::CXXConversionFunctionName) {
3709        // We have to work around a name lookup bug here where negative lookup
3710        // results for these names get cached in namespace lookup tables (these
3711        // names should never be looked up in a namespace).
3712        assert(Result.empty() && "Cannot have a constructor or conversion "
3713                                 "function name in a namespace!");
3714        continue;
3715      }
3716
3717      for (NamedDecl *ND : Result)
3718        if (!ND->isFromASTFile())
3719          GetDeclRef(ND);
3720    }
3721
3722    return 0;
3723  }
3724
3725  if (DC->getPrimaryContext() != DC)
3726    return 0;
3727
3728  // Skip contexts which don't support name lookup.
3729  if (!DC->isLookupContext())
3730    return 0;
3731
3732  // If not in C++, we perform name lookup for the translation unit via the
3733  // IdentifierInfo chains, don't bother to build a visible-declarations table.
3734  if (DC->isTranslationUnit() && !Context.getLangOpts().CPlusPlus)
3735    return 0;
3736
3737  // Serialize the contents of the mapping used for lookup. Note that,
3738  // although we have two very different code paths, the serialized
3739  // representation is the same for both cases: a declaration name,
3740  // followed by a size, followed by references to the visible
3741  // declarations that have that name.
3742  uint64_t Offset = Stream.GetCurrentBitNo();
3743  StoredDeclsMap *Map = DC->buildLookup();
3744  if (!Map || Map->empty())
3745    return 0;
3746
3747  // Create the on-disk hash table in a buffer.
3748  SmallString<4096> LookupTable;
3749  GenerateNameLookupTable(DC, LookupTable);
3750
3751  // Write the lookup table
3752  RecordData::value_type Record[] = {DECL_CONTEXT_VISIBLE};
3753  Stream.EmitRecordWithBlob(DeclContextVisibleLookupAbbrev, Record,
3754                            LookupTable);
3755  ++NumVisibleDeclContexts;
3756  return Offset;
3757}
3758
3759/// \brief Write an UPDATE_VISIBLE block for the given context.
3760///
3761/// UPDATE_VISIBLE blocks contain the declarations that are added to an existing
3762/// DeclContext in a dependent AST file. As such, they only exist for the TU
3763/// (in C++), for namespaces, and for classes with forward-declared unscoped
3764/// enumeration members (in C++11).
3765void ASTWriter::WriteDeclContextVisibleUpdate(const DeclContext *DC) {
3766  StoredDeclsMap *Map = DC->getLookupPtr();
3767  if (!Map || Map->empty())
3768    return;
3769
3770  // Create the on-disk hash table in a buffer.
3771  SmallString<4096> LookupTable;
3772  GenerateNameLookupTable(DC, LookupTable);
3773
3774  // If we're updating a namespace, select a key declaration as the key for the
3775  // update record; those are the only ones that will be checked on reload.
3776  if (isa<NamespaceDecl>(DC))
3777    DC = cast<DeclContext>(Chain->getKeyDeclaration(cast<Decl>(DC)));
3778
3779  // Write the lookup table
3780  RecordData::value_type Record[] = {UPDATE_VISIBLE, getDeclID(cast<Decl>(DC))};
3781  Stream.EmitRecordWithBlob(UpdateVisibleAbbrev, Record, LookupTable);
3782}
3783
3784/// \brief Write an FP_PRAGMA_OPTIONS block for the given FPOptions.
3785void ASTWriter::WriteFPPragmaOptions(const FPOptions &Opts) {
3786  RecordData::value_type Record[] = {Opts.fp_contract};
3787  Stream.EmitRecord(FP_PRAGMA_OPTIONS, Record);
3788}
3789
3790/// \brief Write an OPENCL_EXTENSIONS block for the given OpenCLOptions.
3791void ASTWriter::WriteOpenCLExtensions(Sema &SemaRef) {
3792  if (!SemaRef.Context.getLangOpts().OpenCL)
3793    return;
3794
3795  const OpenCLOptions &Opts = SemaRef.getOpenCLOptions();
3796  RecordData Record;
3797#define OPENCLEXT(nm)  Record.push_back(Opts.nm);
3798#include "clang/Basic/OpenCLExtensions.def"
3799  Stream.EmitRecord(OPENCL_EXTENSIONS, Record);
3800}
3801
3802void ASTWriter::WriteObjCCategories() {
3803  SmallVector<ObjCCategoriesInfo, 2> CategoriesMap;
3804  RecordData Categories;
3805
3806  for (unsigned I = 0, N = ObjCClassesWithCategories.size(); I != N; ++I) {
3807    unsigned Size = 0;
3808    unsigned StartIndex = Categories.size();
3809
3810    ObjCInterfaceDecl *Class = ObjCClassesWithCategories[I];
3811
3812    // Allocate space for the size.
3813    Categories.push_back(0);
3814
3815    // Add the categories.
3816    for (ObjCInterfaceDecl::known_categories_iterator
3817           Cat = Class->known_categories_begin(),
3818           CatEnd = Class->known_categories_end();
3819         Cat != CatEnd; ++Cat, ++Size) {
3820      assert(getDeclID(*Cat) != 0 && "Bogus category");
3821      AddDeclRef(*Cat, Categories);
3822    }
3823
3824    // Update the size.
3825    Categories[StartIndex] = Size;
3826
3827    // Record this interface -> category map.
3828    ObjCCategoriesInfo CatInfo = { getDeclID(Class), StartIndex };
3829    CategoriesMap.push_back(CatInfo);
3830  }
3831
3832  // Sort the categories map by the definition ID, since the reader will be
3833  // performing binary searches on this information.
3834  llvm::array_pod_sort(CategoriesMap.begin(), CategoriesMap.end());
3835
3836  // Emit the categories map.
3837  using namespace llvm;
3838
3839  auto *Abbrev = new BitCodeAbbrev();
3840  Abbrev->Add(BitCodeAbbrevOp(OBJC_CATEGORIES_MAP));
3841  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6)); // # of entries
3842  Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
3843  unsigned AbbrevID = Stream.EmitAbbrev(Abbrev);
3844
3845  RecordData::value_type Record[] = {OBJC_CATEGORIES_MAP, CategoriesMap.size()};
3846  Stream.EmitRecordWithBlob(AbbrevID, Record,
3847                            reinterpret_cast<char *>(CategoriesMap.data()),
3848                            CategoriesMap.size() * sizeof(ObjCCategoriesInfo));
3849
3850  // Emit the category lists.
3851  Stream.EmitRecord(OBJC_CATEGORIES, Categories);
3852}
3853
3854void ASTWriter::WriteLateParsedTemplates(Sema &SemaRef) {
3855  Sema::LateParsedTemplateMapT &LPTMap = SemaRef.LateParsedTemplateMap;
3856
3857  if (LPTMap.empty())
3858    return;
3859
3860  RecordData Record;
3861  for (auto LPTMapEntry : LPTMap) {
3862    const FunctionDecl *FD = LPTMapEntry.first;
3863    LateParsedTemplate *LPT = LPTMapEntry.second;
3864    AddDeclRef(FD, Record);
3865    AddDeclRef(LPT->D, Record);
3866    Record.push_back(LPT->Toks.size());
3867
3868    for (const auto &Tok : LPT->Toks) {
3869      AddToken(Tok, Record);
3870    }
3871  }
3872  Stream.EmitRecord(LATE_PARSED_TEMPLATE, Record);
3873}
3874
3875/// \brief Write the state of 'pragma clang optimize' at the end of the module.
3876void ASTWriter::WriteOptimizePragmaOptions(Sema &SemaRef) {
3877  RecordData Record;
3878  SourceLocation PragmaLoc = SemaRef.getOptimizeOffPragmaLocation();
3879  AddSourceLocation(PragmaLoc, Record);
3880  Stream.EmitRecord(OPTIMIZE_PRAGMA_OPTIONS, Record);
3881}
3882
3883void ASTWriter::WriteModuleFileExtension(Sema &SemaRef,
3884                                         ModuleFileExtensionWriter &Writer) {
3885  // Enter the extension block.
3886  Stream.EnterSubblock(EXTENSION_BLOCK_ID, 4);
3887
3888  // Emit the metadata record abbreviation.
3889  auto *Abv = new llvm::BitCodeAbbrev();
3890  Abv->Add(llvm::BitCodeAbbrevOp(EXTENSION_METADATA));
3891  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3892  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3893  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3894  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
3895  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
3896  unsigned Abbrev = Stream.EmitAbbrev(Abv);
3897
3898  // Emit the metadata record.
3899  RecordData Record;
3900  auto Metadata = Writer.getExtension()->getExtensionMetadata();
3901  Record.push_back(EXTENSION_METADATA);
3902  Record.push_back(Metadata.MajorVersion);
3903  Record.push_back(Metadata.MinorVersion);
3904  Record.push_back(Metadata.BlockName.size());
3905  Record.push_back(Metadata.UserInfo.size());
3906  SmallString<64> Buffer;
3907  Buffer += Metadata.BlockName;
3908  Buffer += Metadata.UserInfo;
3909  Stream.EmitRecordWithBlob(Abbrev, Record, Buffer);
3910
3911  // Emit the contents of the extension block.
3912  Writer.writeExtensionContents(SemaRef, Stream);
3913
3914  // Exit the extension block.
3915  Stream.ExitBlock();
3916}
3917
3918//===----------------------------------------------------------------------===//
3919// General Serialization Routines
3920//===----------------------------------------------------------------------===//
3921
3922/// \brief Write a record containing the given attributes.
3923void ASTWriter::WriteAttributes(ArrayRef<const Attr*> Attrs,
3924                                RecordDataImpl &Record) {
3925  Record.push_back(Attrs.size());
3926  for (const auto *A : Attrs) {
3927    Record.push_back(A->getKind()); // FIXME: stable encoding, target attrs
3928    AddSourceRange(A->getRange(), Record);
3929
3930#include "clang/Serialization/AttrPCHWrite.inc"
3931
3932  }
3933}
3934
3935void ASTWriter::AddToken(const Token &Tok, RecordDataImpl &Record) {
3936  AddSourceLocation(Tok.getLocation(), Record);
3937  Record.push_back(Tok.getLength());
3938
3939  // FIXME: When reading literal tokens, reconstruct the literal pointer
3940  // if it is needed.
3941  AddIdentifierRef(Tok.getIdentifierInfo(), Record);
3942  // FIXME: Should translate token kind to a stable encoding.
3943  Record.push_back(Tok.getKind());
3944  // FIXME: Should translate token flags to a stable encoding.
3945  Record.push_back(Tok.getFlags());
3946}
3947
3948void ASTWriter::AddString(StringRef Str, RecordDataImpl &Record) {
3949  Record.push_back(Str.size());
3950  Record.insert(Record.end(), Str.begin(), Str.end());
3951}
3952
3953bool ASTWriter::PreparePathForOutput(SmallVectorImpl<char> &Path) {
3954  assert(Context && "should have context when outputting path");
3955
3956  bool Changed =
3957      cleanPathForOutput(Context->getSourceManager().getFileManager(), Path);
3958
3959  // Remove a prefix to make the path relative, if relevant.
3960  const char *PathBegin = Path.data();
3961  const char *PathPtr =
3962      adjustFilenameForRelocatableAST(PathBegin, BaseDirectory);
3963  if (PathPtr != PathBegin) {
3964    Path.erase(Path.begin(), Path.begin() + (PathPtr - PathBegin));
3965    Changed = true;
3966  }
3967
3968  return Changed;
3969}
3970
3971void ASTWriter::AddPath(StringRef Path, RecordDataImpl &Record) {
3972  SmallString<128> FilePath(Path);
3973  PreparePathForOutput(FilePath);
3974  AddString(FilePath, Record);
3975}
3976
3977void ASTWriter::EmitRecordWithPath(unsigned Abbrev, RecordDataRef Record,
3978                                   StringRef Path) {
3979  SmallString<128> FilePath(Path);
3980  PreparePathForOutput(FilePath);
3981  Stream.EmitRecordWithBlob(Abbrev, Record, FilePath);
3982}
3983
3984void ASTWriter::AddVersionTuple(const VersionTuple &Version,
3985                                RecordDataImpl &Record) {
3986  Record.push_back(Version.getMajor());
3987  if (Optional<unsigned> Minor = Version.getMinor())
3988    Record.push_back(*Minor + 1);
3989  else
3990    Record.push_back(0);
3991  if (Optional<unsigned> Subminor = Version.getSubminor())
3992    Record.push_back(*Subminor + 1);
3993  else
3994    Record.push_back(0);
3995}
3996
3997/// \brief Note that the identifier II occurs at the given offset
3998/// within the identifier table.
3999void ASTWriter::SetIdentifierOffset(const IdentifierInfo *II, uint32_t Offset) {
4000  IdentID ID = IdentifierIDs[II];
4001  // Only store offsets new to this AST file. Other identifier names are looked
4002  // up earlier in the chain and thus don't need an offset.
4003  if (ID >= FirstIdentID)
4004    IdentifierOffsets[ID - FirstIdentID] = Offset;
4005}
4006
4007/// \brief Note that the selector Sel occurs at the given offset
4008/// within the method pool/selector table.
4009void ASTWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) {
4010  unsigned ID = SelectorIDs[Sel];
4011  assert(ID && "Unknown selector");
4012  // Don't record offsets for selectors that are also available in a different
4013  // file.
4014  if (ID < FirstSelectorID)
4015    return;
4016  SelectorOffsets[ID - FirstSelectorID] = Offset;
4017}
4018
4019ASTWriter::ASTWriter(
4020  llvm::BitstreamWriter &Stream,
4021  ArrayRef<llvm::IntrusiveRefCntPtr<ModuleFileExtension>> Extensions,
4022  bool IncludeTimestamps)
4023    : Stream(Stream), Context(nullptr), PP(nullptr), Chain(nullptr),
4024      WritingModule(nullptr), IncludeTimestamps(IncludeTimestamps),
4025      WritingAST(false), DoneWritingDeclsAndTypes(false),
4026      ASTHasCompilerErrors(false), FirstDeclID(NUM_PREDEF_DECL_IDS),
4027      NextDeclID(FirstDeclID), FirstTypeID(NUM_PREDEF_TYPE_IDS),
4028      NextTypeID(FirstTypeID), FirstIdentID(NUM_PREDEF_IDENT_IDS),
4029      NextIdentID(FirstIdentID), FirstMacroID(NUM_PREDEF_MACRO_IDS),
4030      NextMacroID(FirstMacroID), FirstSubmoduleID(NUM_PREDEF_SUBMODULE_IDS),
4031      NextSubmoduleID(FirstSubmoduleID),
4032      FirstSelectorID(NUM_PREDEF_SELECTOR_IDS), NextSelectorID(FirstSelectorID),
4033      CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0),
4034      NumLexicalDeclContexts(0), NumVisibleDeclContexts(0),
4035      NextCXXBaseSpecifiersID(1), NextCXXCtorInitializersID(1),
4036      TypeExtQualAbbrev(0), TypeFunctionProtoAbbrev(0), DeclParmVarAbbrev(0),
4037      DeclContextLexicalAbbrev(0), DeclContextVisibleLookupAbbrev(0),
4038      UpdateVisibleAbbrev(0), DeclRecordAbbrev(0), DeclTypedefAbbrev(0),
4039      DeclVarAbbrev(0), DeclFieldAbbrev(0), DeclEnumAbbrev(0),
4040      DeclObjCIvarAbbrev(0), DeclCXXMethodAbbrev(0), DeclRefExprAbbrev(0),
4041      CharacterLiteralAbbrev(0), IntegerLiteralAbbrev(0),
4042      ExprImplicitCastAbbrev(0) {
4043  for (const auto &Ext : Extensions) {
4044    if (auto Writer = Ext->createExtensionWriter(*this))
4045      ModuleFileExtensionWriters.push_back(std::move(Writer));
4046  }
4047}
4048
4049ASTWriter::~ASTWriter() {
4050  llvm::DeleteContainerSeconds(FileDeclIDs);
4051}
4052
4053const LangOptions &ASTWriter::getLangOpts() const {
4054  assert(WritingAST && "can't determine lang opts when not writing AST");
4055  return Context->getLangOpts();
4056}
4057
4058time_t ASTWriter::getTimestampForOutput(const FileEntry *E) const {
4059  return IncludeTimestamps ? E->getModificationTime() : 0;
4060}
4061
4062uint64_t ASTWriter::WriteAST(Sema &SemaRef, const std::string &OutputFile,
4063                             Module *WritingModule, StringRef isysroot,
4064                             bool hasErrors) {
4065  WritingAST = true;
4066
4067  ASTHasCompilerErrors = hasErrors;
4068
4069  // Emit the file header.
4070  Stream.Emit((unsigned)'C', 8);
4071  Stream.Emit((unsigned)'P', 8);
4072  Stream.Emit((unsigned)'C', 8);
4073  Stream.Emit((unsigned)'H', 8);
4074
4075  WriteBlockInfoBlock();
4076
4077  Context = &SemaRef.Context;
4078  PP = &SemaRef.PP;
4079  this->WritingModule = WritingModule;
4080  ASTFileSignature Signature =
4081      WriteASTCore(SemaRef, isysroot, OutputFile, WritingModule);
4082  Context = nullptr;
4083  PP = nullptr;
4084  this->WritingModule = nullptr;
4085  this->BaseDirectory.clear();
4086
4087  WritingAST = false;
4088  return Signature;
4089}
4090
4091template<typename Vector>
4092static void AddLazyVectorDecls(ASTWriter &Writer, Vector &Vec,
4093                               ASTWriter::RecordData &Record) {
4094  for (typename Vector::iterator I = Vec.begin(nullptr, true), E = Vec.end();
4095       I != E; ++I) {
4096    Writer.AddDeclRef(*I, Record);
4097  }
4098}
4099
4100uint64_t ASTWriter::WriteASTCore(Sema &SemaRef, StringRef isysroot,
4101                                 const std::string &OutputFile,
4102                                 Module *WritingModule) {
4103  using namespace llvm;
4104
4105  bool isModule = WritingModule != nullptr;
4106
4107  // Make sure that the AST reader knows to finalize itself.
4108  if (Chain)
4109    Chain->finalizeForWriting();
4110
4111  ASTContext &Context = SemaRef.Context;
4112  Preprocessor &PP = SemaRef.PP;
4113
4114  // Set up predefined declaration IDs.
4115  auto RegisterPredefDecl = [&] (Decl *D, PredefinedDeclIDs ID) {
4116    if (D) {
4117      assert(D->isCanonicalDecl() && "predefined decl is not canonical");
4118      DeclIDs[D] = ID;
4119    }
4120  };
4121  RegisterPredefDecl(Context.getTranslationUnitDecl(),
4122                     PREDEF_DECL_TRANSLATION_UNIT_ID);
4123  RegisterPredefDecl(Context.ObjCIdDecl, PREDEF_DECL_OBJC_ID_ID);
4124  RegisterPredefDecl(Context.ObjCSelDecl, PREDEF_DECL_OBJC_SEL_ID);
4125  RegisterPredefDecl(Context.ObjCClassDecl, PREDEF_DECL_OBJC_CLASS_ID);
4126  RegisterPredefDecl(Context.ObjCProtocolClassDecl,
4127                     PREDEF_DECL_OBJC_PROTOCOL_ID);
4128  RegisterPredefDecl(Context.Int128Decl, PREDEF_DECL_INT_128_ID);
4129  RegisterPredefDecl(Context.UInt128Decl, PREDEF_DECL_UNSIGNED_INT_128_ID);
4130  RegisterPredefDecl(Context.ObjCInstanceTypeDecl,
4131                     PREDEF_DECL_OBJC_INSTANCETYPE_ID);
4132  RegisterPredefDecl(Context.BuiltinVaListDecl, PREDEF_DECL_BUILTIN_VA_LIST_ID);
4133  RegisterPredefDecl(Context.VaListTagDecl, PREDEF_DECL_VA_LIST_TAG);
4134  RegisterPredefDecl(Context.BuiltinMSVaListDecl,
4135                     PREDEF_DECL_BUILTIN_MS_VA_LIST_ID);
4136  RegisterPredefDecl(Context.ExternCContext, PREDEF_DECL_EXTERN_C_CONTEXT_ID);
4137  RegisterPredefDecl(Context.MakeIntegerSeqDecl,
4138                     PREDEF_DECL_MAKE_INTEGER_SEQ_ID);
4139
4140  // Build a record containing all of the tentative definitions in this file, in
4141  // TentativeDefinitions order.  Generally, this record will be empty for
4142  // headers.
4143  RecordData TentativeDefinitions;
4144  AddLazyVectorDecls(*this, SemaRef.TentativeDefinitions, TentativeDefinitions);
4145
4146  // Build a record containing all of the file scoped decls in this file.
4147  RecordData UnusedFileScopedDecls;
4148  if (!isModule)
4149    AddLazyVectorDecls(*this, SemaRef.UnusedFileScopedDecls,
4150                       UnusedFileScopedDecls);
4151
4152  // Build a record containing all of the delegating constructors we still need
4153  // to resolve.
4154  RecordData DelegatingCtorDecls;
4155  if (!isModule)
4156    AddLazyVectorDecls(*this, SemaRef.DelegatingCtorDecls, DelegatingCtorDecls);
4157
4158  // Write the set of weak, undeclared identifiers. We always write the
4159  // entire table, since later PCH files in a PCH chain are only interested in
4160  // the results at the end of the chain.
4161  RecordData WeakUndeclaredIdentifiers;
4162  for (auto &WeakUndeclaredIdentifier : SemaRef.WeakUndeclaredIdentifiers) {
4163    IdentifierInfo *II = WeakUndeclaredIdentifier.first;
4164    WeakInfo &WI = WeakUndeclaredIdentifier.second;
4165    AddIdentifierRef(II, WeakUndeclaredIdentifiers);
4166    AddIdentifierRef(WI.getAlias(), WeakUndeclaredIdentifiers);
4167    AddSourceLocation(WI.getLocation(), WeakUndeclaredIdentifiers);
4168    WeakUndeclaredIdentifiers.push_back(WI.getUsed());
4169  }
4170
4171  // Build a record containing all of the ext_vector declarations.
4172  RecordData ExtVectorDecls;
4173  AddLazyVectorDecls(*this, SemaRef.ExtVectorDecls, ExtVectorDecls);
4174
4175  // Build a record containing all of the VTable uses information.
4176  RecordData VTableUses;
4177  if (!SemaRef.VTableUses.empty()) {
4178    for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) {
4179      AddDeclRef(SemaRef.VTableUses[I].first, VTableUses);
4180      AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses);
4181      VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]);
4182    }
4183  }
4184
4185  // Build a record containing all of the UnusedLocalTypedefNameCandidates.
4186  RecordData UnusedLocalTypedefNameCandidates;
4187  for (const TypedefNameDecl *TD : SemaRef.UnusedLocalTypedefNameCandidates)
4188    AddDeclRef(TD, UnusedLocalTypedefNameCandidates);
4189
4190  // Build a record containing all of pending implicit instantiations.
4191  RecordData PendingInstantiations;
4192  for (const auto &I : SemaRef.PendingInstantiations) {
4193    AddDeclRef(I.first, PendingInstantiations);
4194    AddSourceLocation(I.second, PendingInstantiations);
4195  }
4196  assert(SemaRef.PendingLocalImplicitInstantiations.empty() &&
4197         "There are local ones at end of translation unit!");
4198
4199  // Build a record containing some declaration references.
4200  RecordData SemaDeclRefs;
4201  if (SemaRef.StdNamespace || SemaRef.StdBadAlloc) {
4202    AddDeclRef(SemaRef.getStdNamespace(), SemaDeclRefs);
4203    AddDeclRef(SemaRef.getStdBadAlloc(), SemaDeclRefs);
4204  }
4205
4206  RecordData CUDASpecialDeclRefs;
4207  if (Context.getcudaConfigureCallDecl()) {
4208    AddDeclRef(Context.getcudaConfigureCallDecl(), CUDASpecialDeclRefs);
4209  }
4210
4211  // Build a record containing all of the known namespaces.
4212  RecordData KnownNamespaces;
4213  for (const auto &I : SemaRef.KnownNamespaces) {
4214    if (!I.second)
4215      AddDeclRef(I.first, KnownNamespaces);
4216  }
4217
4218  // Build a record of all used, undefined objects that require definitions.
4219  RecordData UndefinedButUsed;
4220
4221  SmallVector<std::pair<NamedDecl *, SourceLocation>, 16> Undefined;
4222  SemaRef.getUndefinedButUsed(Undefined);
4223  for (const auto &I : Undefined) {
4224    AddDeclRef(I.first, UndefinedButUsed);
4225    AddSourceLocation(I.second, UndefinedButUsed);
4226  }
4227
4228  // Build a record containing all delete-expressions that we would like to
4229  // analyze later in AST.
4230  RecordData DeleteExprsToAnalyze;
4231
4232  for (const auto &DeleteExprsInfo :
4233       SemaRef.getMismatchingDeleteExpressions()) {
4234    AddDeclRef(DeleteExprsInfo.first, DeleteExprsToAnalyze);
4235    DeleteExprsToAnalyze.push_back(DeleteExprsInfo.second.size());
4236    for (const auto &DeleteLoc : DeleteExprsInfo.second) {
4237      AddSourceLocation(DeleteLoc.first, DeleteExprsToAnalyze);
4238      DeleteExprsToAnalyze.push_back(DeleteLoc.second);
4239    }
4240  }
4241
4242  // Write the control block
4243  uint64_t Signature = WriteControlBlock(PP, Context, isysroot, OutputFile);
4244
4245  // Write the remaining AST contents.
4246  Stream.EnterSubblock(AST_BLOCK_ID, 5);
4247
4248  // This is so that older clang versions, before the introduction
4249  // of the control block, can read and reject the newer PCH format.
4250  {
4251    RecordData Record = {VERSION_MAJOR};
4252    Stream.EmitRecord(METADATA_OLD_FORMAT, Record);
4253  }
4254
4255  // Create a lexical update block containing all of the declarations in the
4256  // translation unit that do not come from other AST files.
4257  const TranslationUnitDecl *TU = Context.getTranslationUnitDecl();
4258  SmallVector<uint32_t, 128> NewGlobalKindDeclPairs;
4259  for (const auto *D : TU->noload_decls()) {
4260    if (!D->isFromASTFile()) {
4261      NewGlobalKindDeclPairs.push_back(D->getKind());
4262      NewGlobalKindDeclPairs.push_back(GetDeclRef(D));
4263    }
4264  }
4265
4266  auto *Abv = new llvm::BitCodeAbbrev();
4267  Abv->Add(llvm::BitCodeAbbrevOp(TU_UPDATE_LEXICAL));
4268  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4269  unsigned TuUpdateLexicalAbbrev = Stream.EmitAbbrev(Abv);
4270  {
4271    RecordData::value_type Record[] = {TU_UPDATE_LEXICAL};
4272    Stream.EmitRecordWithBlob(TuUpdateLexicalAbbrev, Record,
4273                              bytes(NewGlobalKindDeclPairs));
4274  }
4275
4276  // And a visible updates block for the translation unit.
4277  Abv = new llvm::BitCodeAbbrev();
4278  Abv->Add(llvm::BitCodeAbbrevOp(UPDATE_VISIBLE));
4279  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::VBR, 6));
4280  Abv->Add(llvm::BitCodeAbbrevOp(llvm::BitCodeAbbrevOp::Blob));
4281  UpdateVisibleAbbrev = Stream.EmitAbbrev(Abv);
4282  WriteDeclContextVisibleUpdate(TU);
4283
4284  // If we have any extern "C" names, write out a visible update for them.
4285  if (Context.ExternCContext)
4286    WriteDeclContextVisibleUpdate(Context.ExternCContext);
4287
4288  // If the translation unit has an anonymous namespace, and we don't already
4289  // have an update block for it, write it as an update block.
4290  // FIXME: Why do we not do this if there's already an update block?
4291  if (NamespaceDecl *NS = TU->getAnonymousNamespace()) {
4292    ASTWriter::UpdateRecord &Record = DeclUpdates[TU];
4293    if (Record.empty())
4294      Record.push_back(DeclUpdate(UPD_CXX_ADDED_ANONYMOUS_NAMESPACE, NS));
4295  }
4296
4297  // Add update records for all mangling numbers and static local numbers.
4298  // These aren't really update records, but this is a convenient way of
4299  // tagging this rare extra data onto the declarations.
4300  for (const auto &Number : Context.MangleNumbers)
4301    if (!Number.first->isFromASTFile())
4302      DeclUpdates[Number.first].push_back(DeclUpdate(UPD_MANGLING_NUMBER,
4303                                                     Number.second));
4304  for (const auto &Number : Context.StaticLocalNumbers)
4305    if (!Number.first->isFromASTFile())
4306      DeclUpdates[Number.first].push_back(DeclUpdate(UPD_STATIC_LOCAL_NUMBER,
4307                                                     Number.second));
4308
4309  // Make sure visible decls, added to DeclContexts previously loaded from
4310  // an AST file, are registered for serialization.
4311  for (const auto *I : UpdatingVisibleDecls) {
4312    GetDeclRef(I);
4313  }
4314
4315  // Make sure all decls associated with an identifier are registered for
4316  // serialization, if we're storing decls with identifiers.
4317  if (!WritingModule || !getLangOpts().CPlusPlus) {
4318    llvm::SmallVector<const IdentifierInfo*, 256> IIs;
4319    for (const auto &ID : PP.getIdentifierTable()) {
4320      const IdentifierInfo *II = ID.second;
4321      if (!Chain || !II->isFromAST() || II->hasChangedSinceDeserialization())
4322        IIs.push_back(II);
4323    }
4324    // Sort the identifiers to visit based on their name.
4325    std::sort(IIs.begin(), IIs.end(), llvm::less_ptr<IdentifierInfo>());
4326    for (const IdentifierInfo *II : IIs) {
4327      for (IdentifierResolver::iterator D = SemaRef.IdResolver.begin(II),
4328                                     DEnd = SemaRef.IdResolver.end();
4329           D != DEnd; ++D) {
4330        GetDeclRef(*D);
4331      }
4332    }
4333  }
4334
4335  // Form the record of special types.
4336  RecordData SpecialTypes;
4337  AddTypeRef(Context.getRawCFConstantStringType(), SpecialTypes);
4338  AddTypeRef(Context.getFILEType(), SpecialTypes);
4339  AddTypeRef(Context.getjmp_bufType(), SpecialTypes);
4340  AddTypeRef(Context.getsigjmp_bufType(), SpecialTypes);
4341  AddTypeRef(Context.ObjCIdRedefinitionType, SpecialTypes);
4342  AddTypeRef(Context.ObjCClassRedefinitionType, SpecialTypes);
4343  AddTypeRef(Context.ObjCSelRedefinitionType, SpecialTypes);
4344  AddTypeRef(Context.getucontext_tType(), SpecialTypes);
4345
4346  if (Chain) {
4347    // Write the mapping information describing our module dependencies and how
4348    // each of those modules were mapped into our own offset/ID space, so that
4349    // the reader can build the appropriate mapping to its own offset/ID space.
4350    // The map consists solely of a blob with the following format:
4351    // *(module-name-len:i16 module-name:len*i8
4352    //   source-location-offset:i32
4353    //   identifier-id:i32
4354    //   preprocessed-entity-id:i32
4355    //   macro-definition-id:i32
4356    //   submodule-id:i32
4357    //   selector-id:i32
4358    //   declaration-id:i32
4359    //   c++-base-specifiers-id:i32
4360    //   type-id:i32)
4361    //
4362    auto *Abbrev = new BitCodeAbbrev();
4363    Abbrev->Add(BitCodeAbbrevOp(MODULE_OFFSET_MAP));
4364    Abbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob));
4365    unsigned ModuleOffsetMapAbbrev = Stream.EmitAbbrev(Abbrev);
4366    SmallString<2048> Buffer;
4367    {
4368      llvm::raw_svector_ostream Out(Buffer);
4369      for (ModuleFile *M : Chain->ModuleMgr) {
4370        using namespace llvm::support;
4371        endian::Writer<little> LE(Out);
4372        StringRef FileName = M->FileName;
4373        LE.write<uint16_t>(FileName.size());
4374        Out.write(FileName.data(), FileName.size());
4375
4376        // Note: if a base ID was uint max, it would not be possible to load
4377        // another module after it or have more than one entity inside it.
4378        uint32_t None = std::numeric_limits<uint32_t>::max();
4379
4380        auto writeBaseIDOrNone = [&](uint32_t BaseID, bool ShouldWrite) {
4381          assert(BaseID < std::numeric_limits<uint32_t>::max() && "base id too high");
4382          if (ShouldWrite)
4383            LE.write<uint32_t>(BaseID);
4384          else
4385            LE.write<uint32_t>(None);
4386        };
4387
4388        // These values should be unique within a chain, since they will be read
4389        // as keys into ContinuousRangeMaps.
4390        writeBaseIDOrNone(M->SLocEntryBaseOffset, M->LocalNumSLocEntries);
4391        writeBaseIDOrNone(M->BaseIdentifierID, M->LocalNumIdentifiers);
4392        writeBaseIDOrNone(M->BaseMacroID, M->LocalNumMacros);
4393        writeBaseIDOrNone(M->BasePreprocessedEntityID,
4394                          M->NumPreprocessedEntities);
4395        writeBaseIDOrNone(M->BaseSubmoduleID, M->LocalNumSubmodules);
4396        writeBaseIDOrNone(M->BaseSelectorID, M->LocalNumSelectors);
4397        writeBaseIDOrNone(M->BaseDeclID, M->LocalNumDecls);
4398        writeBaseIDOrNone(M->BaseTypeIndex, M->LocalNumTypes);
4399      }
4400    }
4401    RecordData::value_type Record[] = {MODULE_OFFSET_MAP};
4402    Stream.EmitRecordWithBlob(ModuleOffsetMapAbbrev, Record,
4403                              Buffer.data(), Buffer.size());
4404  }
4405
4406  RecordData DeclUpdatesOffsetsRecord;
4407
4408  // Keep writing types, declarations, and declaration update records
4409  // until we've emitted all of them.
4410  Stream.EnterSubblock(DECLTYPES_BLOCK_ID, /*bits for abbreviations*/5);
4411  WriteTypeAbbrevs();
4412  WriteDeclAbbrevs();
4413  do {
4414    WriteDeclUpdatesBlocks(DeclUpdatesOffsetsRecord);
4415    while (!DeclTypesToEmit.empty()) {
4416      DeclOrType DOT = DeclTypesToEmit.front();
4417      DeclTypesToEmit.pop();
4418      if (DOT.isType())
4419        WriteType(DOT.getType());
4420      else
4421        WriteDecl(Context, DOT.getDecl());
4422    }
4423  } while (!DeclUpdates.empty());
4424  Stream.ExitBlock();
4425
4426  DoneWritingDeclsAndTypes = true;
4427
4428  // These things can only be done once we've written out decls and types.
4429  WriteTypeDeclOffsets();
4430  if (!DeclUpdatesOffsetsRecord.empty())
4431    Stream.EmitRecord(DECL_UPDATE_OFFSETS, DeclUpdatesOffsetsRecord);
4432  WriteCXXBaseSpecifiersOffsets();
4433  WriteCXXCtorInitializersOffsets();
4434  WriteFileDeclIDsMap();
4435  WriteSourceManagerBlock(Context.getSourceManager(), PP);
4436  WriteComments();
4437  WritePreprocessor(PP, isModule);
4438  WriteHeaderSearch(PP.getHeaderSearchInfo());
4439  WriteSelectors(SemaRef);
4440  WriteReferencedSelectorsPool(SemaRef);
4441  WriteLateParsedTemplates(SemaRef);
4442  WriteIdentifierTable(PP, SemaRef.IdResolver, isModule);
4443  WriteFPPragmaOptions(SemaRef.getFPOptions());
4444  WriteOpenCLExtensions(SemaRef);
4445  WritePragmaDiagnosticMappings(Context.getDiagnostics(), isModule);
4446
4447  // If we're emitting a module, write out the submodule information.
4448  if (WritingModule)
4449    WriteSubmodules(WritingModule);
4450
4451  Stream.EmitRecord(SPECIAL_TYPES, SpecialTypes);
4452
4453  // Write the record containing external, unnamed definitions.
4454  if (!EagerlyDeserializedDecls.empty())
4455    Stream.EmitRecord(EAGERLY_DESERIALIZED_DECLS, EagerlyDeserializedDecls);
4456
4457  // Write the record containing tentative definitions.
4458  if (!TentativeDefinitions.empty())
4459    Stream.EmitRecord(TENTATIVE_DEFINITIONS, TentativeDefinitions);
4460
4461  // Write the record containing unused file scoped decls.
4462  if (!UnusedFileScopedDecls.empty())
4463    Stream.EmitRecord(UNUSED_FILESCOPED_DECLS, UnusedFileScopedDecls);
4464
4465  // Write the record containing weak undeclared identifiers.
4466  if (!WeakUndeclaredIdentifiers.empty())
4467    Stream.EmitRecord(WEAK_UNDECLARED_IDENTIFIERS,
4468                      WeakUndeclaredIdentifiers);
4469
4470  // Write the record containing ext_vector type names.
4471  if (!ExtVectorDecls.empty())
4472    Stream.EmitRecord(EXT_VECTOR_DECLS, ExtVectorDecls);
4473
4474  // Write the record containing VTable uses information.
4475  if (!VTableUses.empty())
4476    Stream.EmitRecord(VTABLE_USES, VTableUses);
4477
4478  // Write the record containing potentially unused local typedefs.
4479  if (!UnusedLocalTypedefNameCandidates.empty())
4480    Stream.EmitRecord(UNUSED_LOCAL_TYPEDEF_NAME_CANDIDATES,
4481                      UnusedLocalTypedefNameCandidates);
4482
4483  // Write the record containing pending implicit instantiations.
4484  if (!PendingInstantiations.empty())
4485    Stream.EmitRecord(PENDING_IMPLICIT_INSTANTIATIONS, PendingInstantiations);
4486
4487  // Write the record containing declaration references of Sema.
4488  if (!SemaDeclRefs.empty())
4489    Stream.EmitRecord(SEMA_DECL_REFS, SemaDeclRefs);
4490
4491  // Write the record containing CUDA-specific declaration references.
4492  if (!CUDASpecialDeclRefs.empty())
4493    Stream.EmitRecord(CUDA_SPECIAL_DECL_REFS, CUDASpecialDeclRefs);
4494
4495  // Write the delegating constructors.
4496  if (!DelegatingCtorDecls.empty())
4497    Stream.EmitRecord(DELEGATING_CTORS, DelegatingCtorDecls);
4498
4499  // Write the known namespaces.
4500  if (!KnownNamespaces.empty())
4501    Stream.EmitRecord(KNOWN_NAMESPACES, KnownNamespaces);
4502
4503  // Write the undefined internal functions and variables, and inline functions.
4504  if (!UndefinedButUsed.empty())
4505    Stream.EmitRecord(UNDEFINED_BUT_USED, UndefinedButUsed);
4506
4507  if (!DeleteExprsToAnalyze.empty())
4508    Stream.EmitRecord(DELETE_EXPRS_TO_ANALYZE, DeleteExprsToAnalyze);
4509
4510  // Write the visible updates to DeclContexts.
4511  for (auto *DC : UpdatedDeclContexts)
4512    WriteDeclContextVisibleUpdate(DC);
4513
4514  if (!WritingModule) {
4515    // Write the submodules that were imported, if any.
4516    struct ModuleInfo {
4517      uint64_t ID;
4518      Module *M;
4519      ModuleInfo(uint64_t ID, Module *M) : ID(ID), M(M) {}
4520    };
4521    llvm::SmallVector<ModuleInfo, 64> Imports;
4522    for (const auto *I : Context.local_imports()) {
4523      assert(SubmoduleIDs.find(I->getImportedModule()) != SubmoduleIDs.end());
4524      Imports.push_back(ModuleInfo(SubmoduleIDs[I->getImportedModule()],
4525                         I->getImportedModule()));
4526    }
4527
4528    if (!Imports.empty()) {
4529      auto Cmp = [](const ModuleInfo &A, const ModuleInfo &B) {
4530        return A.ID < B.ID;
4531      };
4532      auto Eq = [](const ModuleInfo &A, const ModuleInfo &B) {
4533        return A.ID == B.ID;
4534      };
4535
4536      // Sort and deduplicate module IDs.
4537      std::sort(Imports.begin(), Imports.end(), Cmp);
4538      Imports.erase(std::unique(Imports.begin(), Imports.end(), Eq),
4539                    Imports.end());
4540
4541      RecordData ImportedModules;
4542      for (const auto &Import : Imports) {
4543        ImportedModules.push_back(Import.ID);
4544        // FIXME: If the module has macros imported then later has declarations
4545        // imported, this location won't be the right one as a location for the
4546        // declaration imports.
4547        AddSourceLocation(PP.getModuleImportLoc(Import.M), ImportedModules);
4548      }
4549
4550      Stream.EmitRecord(IMPORTED_MODULES, ImportedModules);
4551    }
4552  }
4553
4554  WriteDeclReplacementsBlock();
4555  WriteObjCCategories();
4556  if(!WritingModule)
4557    WriteOptimizePragmaOptions(SemaRef);
4558
4559  // Some simple statistics
4560  RecordData::value_type Record[] = {
4561      NumStatements, NumMacros, NumLexicalDeclContexts, NumVisibleDeclContexts};
4562  Stream.EmitRecord(STATISTICS, Record);
4563  Stream.ExitBlock();
4564
4565  // Write the module file extension blocks.
4566  for (const auto &ExtWriter : ModuleFileExtensionWriters)
4567    WriteModuleFileExtension(SemaRef, *ExtWriter);
4568
4569  return Signature;
4570}
4571
4572void ASTWriter::WriteDeclUpdatesBlocks(RecordDataImpl &OffsetsRecord) {
4573  if (DeclUpdates.empty())
4574    return;
4575
4576  DeclUpdateMap LocalUpdates;
4577  LocalUpdates.swap(DeclUpdates);
4578
4579  for (auto &DeclUpdate : LocalUpdates) {
4580    const Decl *D = DeclUpdate.first;
4581
4582    bool HasUpdatedBody = false;
4583    RecordData Record;
4584    for (auto &Update : DeclUpdate.second) {
4585      DeclUpdateKind Kind = (DeclUpdateKind)Update.getKind();
4586
4587      Record.push_back(Kind);
4588      switch (Kind) {
4589      case UPD_CXX_ADDED_IMPLICIT_MEMBER:
4590      case UPD_CXX_ADDED_TEMPLATE_SPECIALIZATION:
4591      case UPD_CXX_ADDED_ANONYMOUS_NAMESPACE:
4592        assert(Update.getDecl() && "no decl to add?");
4593        Record.push_back(GetDeclRef(Update.getDecl()));
4594        break;
4595
4596      case UPD_CXX_ADDED_FUNCTION_DEFINITION:
4597        // An updated body is emitted last, so that the reader doesn't need
4598        // to skip over the lazy body to reach statements for other records.
4599        Record.pop_back();
4600        HasUpdatedBody = true;
4601        break;
4602
4603      case UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER:
4604        AddSourceLocation(Update.getLoc(), Record);
4605        break;
4606
4607      case UPD_CXX_INSTANTIATED_CLASS_DEFINITION: {
4608        auto *RD = cast<CXXRecordDecl>(D);
4609        UpdatedDeclContexts.insert(RD->getPrimaryContext());
4610        AddCXXDefinitionData(RD, Record);
4611        Record.push_back(WriteDeclContextLexicalBlock(
4612            *Context, const_cast<CXXRecordDecl *>(RD)));
4613
4614        // This state is sometimes updated by template instantiation, when we
4615        // switch from the specialization referring to the template declaration
4616        // to it referring to the template definition.
4617        if (auto *MSInfo = RD->getMemberSpecializationInfo()) {
4618          Record.push_back(MSInfo->getTemplateSpecializationKind());
4619          AddSourceLocation(MSInfo->getPointOfInstantiation(), Record);
4620        } else {
4621          auto *Spec = cast<ClassTemplateSpecializationDecl>(RD);
4622          Record.push_back(Spec->getTemplateSpecializationKind());
4623          AddSourceLocation(Spec->getPointOfInstantiation(), Record);
4624
4625          // The instantiation might have been resolved to a partial
4626          // specialization. If so, record which one.
4627          auto From = Spec->getInstantiatedFrom();
4628          if (auto PartialSpec =
4629                From.dyn_cast<ClassTemplatePartialSpecializationDecl*>()) {
4630            Record.push_back(true);
4631            AddDeclRef(PartialSpec, Record);
4632            AddTemplateArgumentList(&Spec->getTemplateInstantiationArgs(),
4633                                    Record);
4634          } else {
4635            Record.push_back(false);
4636          }
4637        }
4638        Record.push_back(RD->getTagKind());
4639        AddSourceLocation(RD->getLocation(), Record);
4640        AddSourceLocation(RD->getLocStart(), Record);
4641        AddSourceLocation(RD->getRBraceLoc(), Record);
4642
4643        // Instantiation may change attributes; write them all out afresh.
4644        Record.push_back(D->hasAttrs());
4645        if (Record.back())
4646          WriteAttributes(llvm::makeArrayRef(D->getAttrs().begin(),
4647                                             D->getAttrs().size()), Record);
4648
4649        // FIXME: Ensure we don't get here for explicit instantiations.
4650        break;
4651      }
4652
4653      case UPD_CXX_RESOLVED_DTOR_DELETE:
4654        AddDeclRef(Update.getDecl(), Record);
4655        break;
4656
4657      case UPD_CXX_RESOLVED_EXCEPTION_SPEC:
4658        addExceptionSpec(
4659            *this,
4660            cast<FunctionDecl>(D)->getType()->castAs<FunctionProtoType>(),
4661            Record);
4662        break;
4663
4664      case UPD_CXX_DEDUCED_RETURN_TYPE:
4665        Record.push_back(GetOrCreateTypeID(Update.getType()));
4666        break;
4667
4668      case UPD_DECL_MARKED_USED:
4669        break;
4670
4671      case UPD_MANGLING_NUMBER:
4672      case UPD_STATIC_LOCAL_NUMBER:
4673        Record.push_back(Update.getNumber());
4674        break;
4675
4676      case UPD_DECL_MARKED_OPENMP_THREADPRIVATE:
4677        AddSourceRange(D->getAttr<OMPThreadPrivateDeclAttr>()->getRange(),
4678                       Record);
4679        break;
4680
4681      case UPD_DECL_EXPORTED:
4682        Record.push_back(getSubmoduleID(Update.getModule()));
4683        break;
4684
4685      case UPD_ADDED_ATTR_TO_RECORD:
4686        WriteAttributes(llvm::makeArrayRef(Update.getAttr()), Record);
4687        break;
4688      }
4689    }
4690
4691    if (HasUpdatedBody) {
4692      const auto *Def = cast<FunctionDecl>(D);
4693      Record.push_back(UPD_CXX_ADDED_FUNCTION_DEFINITION);
4694      Record.push_back(Def->isInlined());
4695      AddSourceLocation(Def->getInnerLocStart(), Record);
4696      AddFunctionDefinition(Def, Record);
4697    }
4698
4699    OffsetsRecord.push_back(GetDeclRef(D));
4700    OffsetsRecord.push_back(Stream.GetCurrentBitNo());
4701
4702    Stream.EmitRecord(DECL_UPDATES, Record);
4703
4704    FlushPendingAfterDecl();
4705  }
4706}
4707
4708void ASTWriter::WriteDeclReplacementsBlock() {
4709  if (ReplacedDecls.empty())
4710    return;
4711
4712  RecordData Record;
4713  for (const auto &I : ReplacedDecls) {
4714    Record.push_back(I.ID);
4715    Record.push_back(I.Offset);
4716    Record.push_back(I.Loc);
4717  }
4718  Stream.EmitRecord(DECL_REPLACEMENTS, Record);
4719}
4720
4721void ASTWriter::AddSourceLocation(SourceLocation Loc, RecordDataImpl &Record) {
4722  Record.push_back(Loc.getRawEncoding());
4723}
4724
4725void ASTWriter::AddSourceRange(SourceRange Range, RecordDataImpl &Record) {
4726  AddSourceLocation(Range.getBegin(), Record);
4727  AddSourceLocation(Range.getEnd(), Record);
4728}
4729
4730void ASTWriter::AddAPInt(const llvm::APInt &Value, RecordDataImpl &Record) {
4731  Record.push_back(Value.getBitWidth());
4732  const uint64_t *Words = Value.getRawData();
4733  Record.append(Words, Words + Value.getNumWords());
4734}
4735
4736void ASTWriter::AddAPSInt(const llvm::APSInt &Value, RecordDataImpl &Record) {
4737  Record.push_back(Value.isUnsigned());
4738  AddAPInt(Value, Record);
4739}
4740
4741void ASTWriter::AddAPFloat(const llvm::APFloat &Value, RecordDataImpl &Record) {
4742  AddAPInt(Value.bitcastToAPInt(), Record);
4743}
4744
4745void ASTWriter::AddIdentifierRef(const IdentifierInfo *II, RecordDataImpl &Record) {
4746  Record.push_back(getIdentifierRef(II));
4747}
4748
4749IdentID ASTWriter::getIdentifierRef(const IdentifierInfo *II) {
4750  if (!II)
4751    return 0;
4752
4753  IdentID &ID = IdentifierIDs[II];
4754  if (ID == 0)
4755    ID = NextIdentID++;
4756  return ID;
4757}
4758
4759MacroID ASTWriter::getMacroRef(MacroInfo *MI, const IdentifierInfo *Name) {
4760  // Don't emit builtin macros like __LINE__ to the AST file unless they
4761  // have been redefined by the header (in which case they are not
4762  // isBuiltinMacro).
4763  if (!MI || MI->isBuiltinMacro())
4764    return 0;
4765
4766  MacroID &ID = MacroIDs[MI];
4767  if (ID == 0) {
4768    ID = NextMacroID++;
4769    MacroInfoToEmitData Info = { Name, MI, ID };
4770    MacroInfosToEmit.push_back(Info);
4771  }
4772  return ID;
4773}
4774
4775MacroID ASTWriter::getMacroID(MacroInfo *MI) {
4776  if (!MI || MI->isBuiltinMacro())
4777    return 0;
4778
4779  assert(MacroIDs.find(MI) != MacroIDs.end() && "Macro not emitted!");
4780  return MacroIDs[MI];
4781}
4782
4783uint64_t ASTWriter::getMacroDirectivesOffset(const IdentifierInfo *Name) {
4784  return IdentMacroDirectivesOffsetMap.lookup(Name);
4785}
4786
4787void ASTWriter::AddSelectorRef(const Selector SelRef, RecordDataImpl &Record) {
4788  Record.push_back(getSelectorRef(SelRef));
4789}
4790
4791SelectorID ASTWriter::getSelectorRef(Selector Sel) {
4792  if (Sel.getAsOpaquePtr() == nullptr) {
4793    return 0;
4794  }
4795
4796  SelectorID SID = SelectorIDs[Sel];
4797  if (SID == 0 && Chain) {
4798    // This might trigger a ReadSelector callback, which will set the ID for
4799    // this selector.
4800    Chain->LoadSelector(Sel);
4801    SID = SelectorIDs[Sel];
4802  }
4803  if (SID == 0) {
4804    SID = NextSelectorID++;
4805    SelectorIDs[Sel] = SID;
4806  }
4807  return SID;
4808}
4809
4810void ASTWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordDataImpl &Record) {
4811  AddDeclRef(Temp->getDestructor(), Record);
4812}
4813
4814void ASTWriter::AddCXXCtorInitializersRef(ArrayRef<CXXCtorInitializer *> Inits,
4815                                          RecordDataImpl &Record) {
4816  assert(!Inits.empty() && "Empty ctor initializer sets are not recorded");
4817  CXXCtorInitializersToWrite.push_back(
4818      QueuedCXXCtorInitializers(NextCXXCtorInitializersID, Inits));
4819  Record.push_back(NextCXXCtorInitializersID++);
4820}
4821
4822void ASTWriter::AddCXXBaseSpecifiersRef(CXXBaseSpecifier const *Bases,
4823                                        CXXBaseSpecifier const *BasesEnd,
4824                                        RecordDataImpl &Record) {
4825  assert(Bases != BasesEnd && "Empty base-specifier sets are not recorded");
4826  CXXBaseSpecifiersToWrite.push_back(
4827                                QueuedCXXBaseSpecifiers(NextCXXBaseSpecifiersID,
4828                                                        Bases, BasesEnd));
4829  Record.push_back(NextCXXBaseSpecifiersID++);
4830}
4831
4832void ASTWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind,
4833                                           const TemplateArgumentLocInfo &Arg,
4834                                           RecordDataImpl &Record) {
4835  switch (Kind) {
4836  case TemplateArgument::Expression:
4837    AddStmt(Arg.getAsExpr());
4838    break;
4839  case TemplateArgument::Type:
4840    AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record);
4841    break;
4842  case TemplateArgument::Template:
4843    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
4844    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
4845    break;
4846  case TemplateArgument::TemplateExpansion:
4847    AddNestedNameSpecifierLoc(Arg.getTemplateQualifierLoc(), Record);
4848    AddSourceLocation(Arg.getTemplateNameLoc(), Record);
4849    AddSourceLocation(Arg.getTemplateEllipsisLoc(), Record);
4850    break;
4851  case TemplateArgument::Null:
4852  case TemplateArgument::Integral:
4853  case TemplateArgument::Declaration:
4854  case TemplateArgument::NullPtr:
4855  case TemplateArgument::Pack:
4856    // FIXME: Is this right?
4857    break;
4858  }
4859}
4860
4861void ASTWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg,
4862                                       RecordDataImpl &Record) {
4863  AddTemplateArgument(Arg.getArgument(), Record);
4864
4865  if (Arg.getArgument().getKind() == TemplateArgument::Expression) {
4866    bool InfoHasSameExpr
4867      = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr();
4868    Record.push_back(InfoHasSameExpr);
4869    if (InfoHasSameExpr)
4870      return; // Avoid storing the same expr twice.
4871  }
4872  AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(),
4873                             Record);
4874}
4875
4876void ASTWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo,
4877                                  RecordDataImpl &Record) {
4878  if (!TInfo) {
4879    AddTypeRef(QualType(), Record);
4880    return;
4881  }
4882
4883  AddTypeLoc(TInfo->getTypeLoc(), Record);
4884}
4885
4886void ASTWriter::AddTypeLoc(TypeLoc TL, RecordDataImpl &Record) {
4887  AddTypeRef(TL.getType(), Record);
4888
4889  TypeLocWriter TLW(*this, Record);
4890  for (; !TL.isNull(); TL = TL.getNextTypeLoc())
4891    TLW.Visit(TL);
4892}
4893
4894void ASTWriter::AddTypeRef(QualType T, RecordDataImpl &Record) {
4895  Record.push_back(GetOrCreateTypeID(T));
4896}
4897
4898TypeID ASTWriter::GetOrCreateTypeID(QualType T) {
4899  assert(Context);
4900  return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4901    if (T.isNull())
4902      return TypeIdx();
4903    assert(!T.getLocalFastQualifiers());
4904
4905    TypeIdx &Idx = TypeIdxs[T];
4906    if (Idx.getIndex() == 0) {
4907      if (DoneWritingDeclsAndTypes) {
4908        assert(0 && "New type seen after serializing all the types to emit!");
4909        return TypeIdx();
4910      }
4911
4912      // We haven't seen this type before. Assign it a new ID and put it
4913      // into the queue of types to emit.
4914      Idx = TypeIdx(NextTypeID++);
4915      DeclTypesToEmit.push(T);
4916    }
4917    return Idx;
4918  });
4919}
4920
4921TypeID ASTWriter::getTypeID(QualType T) const {
4922  assert(Context);
4923  return MakeTypeID(*Context, T, [&](QualType T) -> TypeIdx {
4924    if (T.isNull())
4925      return TypeIdx();
4926    assert(!T.getLocalFastQualifiers());
4927
4928    TypeIdxMap::const_iterator I = TypeIdxs.find(T);
4929    assert(I != TypeIdxs.end() && "Type not emitted!");
4930    return I->second;
4931  });
4932}
4933
4934void ASTWriter::AddDeclRef(const Decl *D, RecordDataImpl &Record) {
4935  Record.push_back(GetDeclRef(D));
4936}
4937
4938DeclID ASTWriter::GetDeclRef(const Decl *D) {
4939  assert(WritingAST && "Cannot request a declaration ID before AST writing");
4940
4941  if (!D) {
4942    return 0;
4943  }
4944
4945  // If D comes from an AST file, its declaration ID is already known and
4946  // fixed.
4947  if (D->isFromASTFile())
4948    return D->getGlobalID();
4949
4950  assert(!(reinterpret_cast<uintptr_t>(D) & 0x01) && "Invalid decl pointer");
4951  DeclID &ID = DeclIDs[D];
4952  if (ID == 0) {
4953    if (DoneWritingDeclsAndTypes) {
4954      assert(0 && "New decl seen after serializing all the decls to emit!");
4955      return 0;
4956    }
4957
4958    // We haven't seen this declaration before. Give it a new ID and
4959    // enqueue it in the list of declarations to emit.
4960    ID = NextDeclID++;
4961    DeclTypesToEmit.push(const_cast<Decl *>(D));
4962  }
4963
4964  return ID;
4965}
4966
4967DeclID ASTWriter::getDeclID(const Decl *D) {
4968  if (!D)
4969    return 0;
4970
4971  // If D comes from an AST file, its declaration ID is already known and
4972  // fixed.
4973  if (D->isFromASTFile())
4974    return D->getGlobalID();
4975
4976  assert(DeclIDs.find(D) != DeclIDs.end() && "Declaration not emitted!");
4977  return DeclIDs[D];
4978}
4979
4980void ASTWriter::associateDeclWithFile(const Decl *D, DeclID ID) {
4981  assert(ID);
4982  assert(D);
4983
4984  SourceLocation Loc = D->getLocation();
4985  if (Loc.isInvalid())
4986    return;
4987
4988  // We only keep track of the file-level declarations of each file.
4989  if (!D->getLexicalDeclContext()->isFileContext())
4990    return;
4991  // FIXME: ParmVarDecls that are part of a function type of a parameter of
4992  // a function/objc method, should not have TU as lexical context.
4993  if (isa<ParmVarDecl>(D))
4994    return;
4995
4996  SourceManager &SM = Context->getSourceManager();
4997  SourceLocation FileLoc = SM.getFileLoc(Loc);
4998  assert(SM.isLocalSourceLocation(FileLoc));
4999  FileID FID;
5000  unsigned Offset;
5001  std::tie(FID, Offset) = SM.getDecomposedLoc(FileLoc);
5002  if (FID.isInvalid())
5003    return;
5004  assert(SM.getSLocEntry(FID).isFile());
5005
5006  DeclIDInFileInfo *&Info = FileDeclIDs[FID];
5007  if (!Info)
5008    Info = new DeclIDInFileInfo();
5009
5010  std::pair<unsigned, serialization::DeclID> LocDecl(Offset, ID);
5011  LocDeclIDsTy &Decls = Info->DeclIDs;
5012
5013  if (Decls.empty() || Decls.back().first <= Offset) {
5014    Decls.push_back(LocDecl);
5015    return;
5016  }
5017
5018  LocDeclIDsTy::iterator I =
5019      std::upper_bound(Decls.begin(), Decls.end(), LocDecl, llvm::less_first());
5020
5021  Decls.insert(I, LocDecl);
5022}
5023
5024void ASTWriter::AddDeclarationName(DeclarationName Name, RecordDataImpl &Record) {
5025  // FIXME: Emit a stable enum for NameKind.  0 = Identifier etc.
5026  Record.push_back(Name.getNameKind());
5027  switch (Name.getNameKind()) {
5028  case DeclarationName::Identifier:
5029    AddIdentifierRef(Name.getAsIdentifierInfo(), Record);
5030    break;
5031
5032  case DeclarationName::ObjCZeroArgSelector:
5033  case DeclarationName::ObjCOneArgSelector:
5034  case DeclarationName::ObjCMultiArgSelector:
5035    AddSelectorRef(Name.getObjCSelector(), Record);
5036    break;
5037
5038  case DeclarationName::CXXConstructorName:
5039  case DeclarationName::CXXDestructorName:
5040  case DeclarationName::CXXConversionFunctionName:
5041    AddTypeRef(Name.getCXXNameType(), Record);
5042    break;
5043
5044  case DeclarationName::CXXOperatorName:
5045    Record.push_back(Name.getCXXOverloadedOperator());
5046    break;
5047
5048  case DeclarationName::CXXLiteralOperatorName:
5049    AddIdentifierRef(Name.getCXXLiteralIdentifier(), Record);
5050    break;
5051
5052  case DeclarationName::CXXUsingDirective:
5053    // No extra data to emit
5054    break;
5055  }
5056}
5057
5058unsigned ASTWriter::getAnonymousDeclarationNumber(const NamedDecl *D) {
5059  assert(needsAnonymousDeclarationNumber(D) &&
5060         "expected an anonymous declaration");
5061
5062  // Number the anonymous declarations within this context, if we've not
5063  // already done so.
5064  auto It = AnonymousDeclarationNumbers.find(D);
5065  if (It == AnonymousDeclarationNumbers.end()) {
5066    auto *DC = D->getLexicalDeclContext();
5067    numberAnonymousDeclsWithin(DC, [&](const NamedDecl *ND, unsigned Number) {
5068      AnonymousDeclarationNumbers[ND] = Number;
5069    });
5070
5071    It = AnonymousDeclarationNumbers.find(D);
5072    assert(It != AnonymousDeclarationNumbers.end() &&
5073           "declaration not found within its lexical context");
5074  }
5075
5076  return It->second;
5077}
5078
5079void ASTWriter::AddDeclarationNameLoc(const DeclarationNameLoc &DNLoc,
5080                                     DeclarationName Name, RecordDataImpl &Record) {
5081  switch (Name.getNameKind()) {
5082  case DeclarationName::CXXConstructorName:
5083  case DeclarationName::CXXDestructorName:
5084  case DeclarationName::CXXConversionFunctionName:
5085    AddTypeSourceInfo(DNLoc.NamedType.TInfo, Record);
5086    break;
5087
5088  case DeclarationName::CXXOperatorName:
5089    AddSourceLocation(
5090       SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.BeginOpNameLoc),
5091       Record);
5092    AddSourceLocation(
5093        SourceLocation::getFromRawEncoding(DNLoc.CXXOperatorName.EndOpNameLoc),
5094        Record);
5095    break;
5096
5097  case DeclarationName::CXXLiteralOperatorName:
5098    AddSourceLocation(
5099     SourceLocation::getFromRawEncoding(DNLoc.CXXLiteralOperatorName.OpNameLoc),
5100     Record);
5101    break;
5102
5103  case DeclarationName::Identifier:
5104  case DeclarationName::ObjCZeroArgSelector:
5105  case DeclarationName::ObjCOneArgSelector:
5106  case DeclarationName::ObjCMultiArgSelector:
5107  case DeclarationName::CXXUsingDirective:
5108    break;
5109  }
5110}
5111
5112void ASTWriter::AddDeclarationNameInfo(const DeclarationNameInfo &NameInfo,
5113                                       RecordDataImpl &Record) {
5114  AddDeclarationName(NameInfo.getName(), Record);
5115  AddSourceLocation(NameInfo.getLoc(), Record);
5116  AddDeclarationNameLoc(NameInfo.getInfo(), NameInfo.getName(), Record);
5117}
5118
5119void ASTWriter::AddQualifierInfo(const QualifierInfo &Info,
5120                                 RecordDataImpl &Record) {
5121  AddNestedNameSpecifierLoc(Info.QualifierLoc, Record);
5122  Record.push_back(Info.NumTemplParamLists);
5123  for (unsigned i=0, e=Info.NumTemplParamLists; i != e; ++i)
5124    AddTemplateParameterList(Info.TemplParamLists[i], Record);
5125}
5126
5127void ASTWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS,
5128                                       RecordDataImpl &Record) {
5129  // Nested name specifiers usually aren't too long. I think that 8 would
5130  // typically accommodate the vast majority.
5131  SmallVector<NestedNameSpecifier *, 8> NestedNames;
5132
5133  // Push each of the NNS's onto a stack for serialization in reverse order.
5134  while (NNS) {
5135    NestedNames.push_back(NNS);
5136    NNS = NNS->getPrefix();
5137  }
5138
5139  Record.push_back(NestedNames.size());
5140  while(!NestedNames.empty()) {
5141    NNS = NestedNames.pop_back_val();
5142    NestedNameSpecifier::SpecifierKind Kind = NNS->getKind();
5143    Record.push_back(Kind);
5144    switch (Kind) {
5145    case NestedNameSpecifier::Identifier:
5146      AddIdentifierRef(NNS->getAsIdentifier(), Record);
5147      break;
5148
5149    case NestedNameSpecifier::Namespace:
5150      AddDeclRef(NNS->getAsNamespace(), Record);
5151      break;
5152
5153    case NestedNameSpecifier::NamespaceAlias:
5154      AddDeclRef(NNS->getAsNamespaceAlias(), Record);
5155      break;
5156
5157    case NestedNameSpecifier::TypeSpec:
5158    case NestedNameSpecifier::TypeSpecWithTemplate:
5159      AddTypeRef(QualType(NNS->getAsType(), 0), Record);
5160      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5161      break;
5162
5163    case NestedNameSpecifier::Global:
5164      // Don't need to write an associated value.
5165      break;
5166
5167    case NestedNameSpecifier::Super:
5168      AddDeclRef(NNS->getAsRecordDecl(), Record);
5169      break;
5170    }
5171  }
5172}
5173
5174void ASTWriter::AddNestedNameSpecifierLoc(NestedNameSpecifierLoc NNS,
5175                                          RecordDataImpl &Record) {
5176  // Nested name specifiers usually aren't too long. I think that 8 would
5177  // typically accommodate the vast majority.
5178  SmallVector<NestedNameSpecifierLoc , 8> NestedNames;
5179
5180  // Push each of the nested-name-specifiers's onto a stack for
5181  // serialization in reverse order.
5182  while (NNS) {
5183    NestedNames.push_back(NNS);
5184    NNS = NNS.getPrefix();
5185  }
5186
5187  Record.push_back(NestedNames.size());
5188  while(!NestedNames.empty()) {
5189    NNS = NestedNames.pop_back_val();
5190    NestedNameSpecifier::SpecifierKind Kind
5191      = NNS.getNestedNameSpecifier()->getKind();
5192    Record.push_back(Kind);
5193    switch (Kind) {
5194    case NestedNameSpecifier::Identifier:
5195      AddIdentifierRef(NNS.getNestedNameSpecifier()->getAsIdentifier(), Record);
5196      AddSourceRange(NNS.getLocalSourceRange(), Record);
5197      break;
5198
5199    case NestedNameSpecifier::Namespace:
5200      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespace(), Record);
5201      AddSourceRange(NNS.getLocalSourceRange(), Record);
5202      break;
5203
5204    case NestedNameSpecifier::NamespaceAlias:
5205      AddDeclRef(NNS.getNestedNameSpecifier()->getAsNamespaceAlias(), Record);
5206      AddSourceRange(NNS.getLocalSourceRange(), Record);
5207      break;
5208
5209    case NestedNameSpecifier::TypeSpec:
5210    case NestedNameSpecifier::TypeSpecWithTemplate:
5211      Record.push_back(Kind == NestedNameSpecifier::TypeSpecWithTemplate);
5212      AddTypeLoc(NNS.getTypeLoc(), Record);
5213      AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5214      break;
5215
5216    case NestedNameSpecifier::Global:
5217      AddSourceLocation(NNS.getLocalSourceRange().getEnd(), Record);
5218      break;
5219
5220    case NestedNameSpecifier::Super:
5221      AddDeclRef(NNS.getNestedNameSpecifier()->getAsRecordDecl(), Record);
5222      AddSourceRange(NNS.getLocalSourceRange(), Record);
5223      break;
5224    }
5225  }
5226}
5227
5228void ASTWriter::AddTemplateName(TemplateName Name, RecordDataImpl &Record) {
5229  TemplateName::NameKind Kind = Name.getKind();
5230  Record.push_back(Kind);
5231  switch (Kind) {
5232  case TemplateName::Template:
5233    AddDeclRef(Name.getAsTemplateDecl(), Record);
5234    break;
5235
5236  case TemplateName::OverloadedTemplate: {
5237    OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate();
5238    Record.push_back(OvT->size());
5239    for (const auto &I : *OvT)
5240      AddDeclRef(I, Record);
5241    break;
5242  }
5243
5244  case TemplateName::QualifiedTemplate: {
5245    QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName();
5246    AddNestedNameSpecifier(QualT->getQualifier(), Record);
5247    Record.push_back(QualT->hasTemplateKeyword());
5248    AddDeclRef(QualT->getTemplateDecl(), Record);
5249    break;
5250  }
5251
5252  case TemplateName::DependentTemplate: {
5253    DependentTemplateName *DepT = Name.getAsDependentTemplateName();
5254    AddNestedNameSpecifier(DepT->getQualifier(), Record);
5255    Record.push_back(DepT->isIdentifier());
5256    if (DepT->isIdentifier())
5257      AddIdentifierRef(DepT->getIdentifier(), Record);
5258    else
5259      Record.push_back(DepT->getOperator());
5260    break;
5261  }
5262
5263  case TemplateName::SubstTemplateTemplateParm: {
5264    SubstTemplateTemplateParmStorage *subst
5265      = Name.getAsSubstTemplateTemplateParm();
5266    AddDeclRef(subst->getParameter(), Record);
5267    AddTemplateName(subst->getReplacement(), Record);
5268    break;
5269  }
5270
5271  case TemplateName::SubstTemplateTemplateParmPack: {
5272    SubstTemplateTemplateParmPackStorage *SubstPack
5273      = Name.getAsSubstTemplateTemplateParmPack();
5274    AddDeclRef(SubstPack->getParameterPack(), Record);
5275    AddTemplateArgument(SubstPack->getArgumentPack(), Record);
5276    break;
5277  }
5278  }
5279}
5280
5281void ASTWriter::AddTemplateArgument(const TemplateArgument &Arg,
5282                                    RecordDataImpl &Record) {
5283  Record.push_back(Arg.getKind());
5284  switch (Arg.getKind()) {
5285  case TemplateArgument::Null:
5286    break;
5287  case TemplateArgument::Type:
5288    AddTypeRef(Arg.getAsType(), Record);
5289    break;
5290  case TemplateArgument::Declaration:
5291    AddDeclRef(Arg.getAsDecl(), Record);
5292    AddTypeRef(Arg.getParamTypeForDecl(), Record);
5293    break;
5294  case TemplateArgument::NullPtr:
5295    AddTypeRef(Arg.getNullPtrType(), Record);
5296    break;
5297  case TemplateArgument::Integral:
5298    AddAPSInt(Arg.getAsIntegral(), Record);
5299    AddTypeRef(Arg.getIntegralType(), Record);
5300    break;
5301  case TemplateArgument::Template:
5302    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5303    break;
5304  case TemplateArgument::TemplateExpansion:
5305    AddTemplateName(Arg.getAsTemplateOrTemplatePattern(), Record);
5306    if (Optional<unsigned> NumExpansions = Arg.getNumTemplateExpansions())
5307      Record.push_back(*NumExpansions + 1);
5308    else
5309      Record.push_back(0);
5310    break;
5311  case TemplateArgument::Expression:
5312    AddStmt(Arg.getAsExpr());
5313    break;
5314  case TemplateArgument::Pack:
5315    Record.push_back(Arg.pack_size());
5316    for (const auto &P : Arg.pack_elements())
5317      AddTemplateArgument(P, Record);
5318    break;
5319  }
5320}
5321
5322void
5323ASTWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams,
5324                                    RecordDataImpl &Record) {
5325  assert(TemplateParams && "No TemplateParams!");
5326  AddSourceLocation(TemplateParams->getTemplateLoc(), Record);
5327  AddSourceLocation(TemplateParams->getLAngleLoc(), Record);
5328  AddSourceLocation(TemplateParams->getRAngleLoc(), Record);
5329  Record.push_back(TemplateParams->size());
5330  for (const auto &P : *TemplateParams)
5331    AddDeclRef(P, Record);
5332}
5333
5334/// \brief Emit a template argument list.
5335void
5336ASTWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs,
5337                                   RecordDataImpl &Record) {
5338  assert(TemplateArgs && "No TemplateArgs!");
5339  Record.push_back(TemplateArgs->size());
5340  for (int i=0, e = TemplateArgs->size(); i != e; ++i)
5341    AddTemplateArgument(TemplateArgs->get(i), Record);
5342}
5343
5344void
5345ASTWriter::AddASTTemplateArgumentListInfo
5346(const ASTTemplateArgumentListInfo *ASTTemplArgList, RecordDataImpl &Record) {
5347  assert(ASTTemplArgList && "No ASTTemplArgList!");
5348  AddSourceLocation(ASTTemplArgList->LAngleLoc, Record);
5349  AddSourceLocation(ASTTemplArgList->RAngleLoc, Record);
5350  Record.push_back(ASTTemplArgList->NumTemplateArgs);
5351  const TemplateArgumentLoc *TemplArgs = ASTTemplArgList->getTemplateArgs();
5352  for (int i=0, e = ASTTemplArgList->NumTemplateArgs; i != e; ++i)
5353    AddTemplateArgumentLoc(TemplArgs[i], Record);
5354}
5355
5356void
5357ASTWriter::AddUnresolvedSet(const ASTUnresolvedSet &Set, RecordDataImpl &Record) {
5358  Record.push_back(Set.size());
5359  for (ASTUnresolvedSet::const_iterator
5360         I = Set.begin(), E = Set.end(); I != E; ++I) {
5361    AddDeclRef(I.getDecl(), Record);
5362    Record.push_back(I.getAccess());
5363  }
5364}
5365
5366void ASTWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base,
5367                                    RecordDataImpl &Record) {
5368  Record.push_back(Base.isVirtual());
5369  Record.push_back(Base.isBaseOfClass());
5370  Record.push_back(Base.getAccessSpecifierAsWritten());
5371  Record.push_back(Base.getInheritConstructors());
5372  AddTypeSourceInfo(Base.getTypeSourceInfo(), Record);
5373  AddSourceRange(Base.getSourceRange(), Record);
5374  AddSourceLocation(Base.isPackExpansion()? Base.getEllipsisLoc()
5375                                          : SourceLocation(),
5376                    Record);
5377}
5378
5379void ASTWriter::FlushCXXBaseSpecifiers() {
5380  RecordData Record;
5381  unsigned N = CXXBaseSpecifiersToWrite.size();
5382  for (unsigned I = 0; I != N; ++I) {
5383    Record.clear();
5384
5385    // Record the offset of this base-specifier set.
5386    unsigned Index = CXXBaseSpecifiersToWrite[I].ID - 1;
5387    if (Index == CXXBaseSpecifiersOffsets.size())
5388      CXXBaseSpecifiersOffsets.push_back(Stream.GetCurrentBitNo());
5389    else {
5390      if (Index > CXXBaseSpecifiersOffsets.size())
5391        CXXBaseSpecifiersOffsets.resize(Index + 1);
5392      CXXBaseSpecifiersOffsets[Index] = Stream.GetCurrentBitNo();
5393    }
5394
5395    const CXXBaseSpecifier *B = CXXBaseSpecifiersToWrite[I].Bases,
5396                        *BEnd = CXXBaseSpecifiersToWrite[I].BasesEnd;
5397    Record.push_back(BEnd - B);
5398    for (; B != BEnd; ++B)
5399      AddCXXBaseSpecifier(*B, Record);
5400    Stream.EmitRecord(serialization::DECL_CXX_BASE_SPECIFIERS, Record);
5401
5402    // Flush any expressions that were written as part of the base specifiers.
5403    FlushStmts();
5404  }
5405
5406  assert(N == CXXBaseSpecifiersToWrite.size() &&
5407         "added more base specifiers while writing base specifiers");
5408  CXXBaseSpecifiersToWrite.clear();
5409}
5410
5411void ASTWriter::AddCXXCtorInitializers(
5412                             const CXXCtorInitializer * const *CtorInitializers,
5413                             unsigned NumCtorInitializers,
5414                             RecordDataImpl &Record) {
5415  Record.push_back(NumCtorInitializers);
5416  for (unsigned i=0; i != NumCtorInitializers; ++i) {
5417    const CXXCtorInitializer *Init = CtorInitializers[i];
5418
5419    if (Init->isBaseInitializer()) {
5420      Record.push_back(CTOR_INITIALIZER_BASE);
5421      AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5422      Record.push_back(Init->isBaseVirtual());
5423    } else if (Init->isDelegatingInitializer()) {
5424      Record.push_back(CTOR_INITIALIZER_DELEGATING);
5425      AddTypeSourceInfo(Init->getTypeSourceInfo(), Record);
5426    } else if (Init->isMemberInitializer()){
5427      Record.push_back(CTOR_INITIALIZER_MEMBER);
5428      AddDeclRef(Init->getMember(), Record);
5429    } else {
5430      Record.push_back(CTOR_INITIALIZER_INDIRECT_MEMBER);
5431      AddDeclRef(Init->getIndirectMember(), Record);
5432    }
5433
5434    AddSourceLocation(Init->getMemberLocation(), Record);
5435    AddStmt(Init->getInit());
5436    AddSourceLocation(Init->getLParenLoc(), Record);
5437    AddSourceLocation(Init->getRParenLoc(), Record);
5438    Record.push_back(Init->isWritten());
5439    if (Init->isWritten()) {
5440      Record.push_back(Init->getSourceOrder());
5441    } else {
5442      Record.push_back(Init->getNumArrayIndices());
5443      for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i)
5444        AddDeclRef(Init->getArrayIndex(i), Record);
5445    }
5446  }
5447}
5448
5449void ASTWriter::FlushCXXCtorInitializers() {
5450  RecordData Record;
5451
5452  unsigned N = CXXCtorInitializersToWrite.size();
5453  (void)N; // Silence unused warning in non-assert builds.
5454  for (auto &Init : CXXCtorInitializersToWrite) {
5455    Record.clear();
5456
5457    // Record the offset of this mem-initializer list.
5458    unsigned Index = Init.ID - 1;
5459    if (Index == CXXCtorInitializersOffsets.size())
5460      CXXCtorInitializersOffsets.push_back(Stream.GetCurrentBitNo());
5461    else {
5462      if (Index > CXXCtorInitializersOffsets.size())
5463        CXXCtorInitializersOffsets.resize(Index + 1);
5464      CXXCtorInitializersOffsets[Index] = Stream.GetCurrentBitNo();
5465    }
5466
5467    AddCXXCtorInitializers(Init.Inits.data(), Init.Inits.size(), Record);
5468    Stream.EmitRecord(serialization::DECL_CXX_CTOR_INITIALIZERS, Record);
5469
5470    // Flush any expressions that were written as part of the initializers.
5471    FlushStmts();
5472  }
5473
5474  assert(N == CXXCtorInitializersToWrite.size() &&
5475         "added more ctor initializers while writing ctor initializers");
5476  CXXCtorInitializersToWrite.clear();
5477}
5478
5479void ASTWriter::AddCXXDefinitionData(const CXXRecordDecl *D, RecordDataImpl &Record) {
5480  auto &Data = D->data();
5481  Record.push_back(Data.IsLambda);
5482  Record.push_back(Data.UserDeclaredConstructor);
5483  Record.push_back(Data.UserDeclaredSpecialMembers);
5484  Record.push_back(Data.Aggregate);
5485  Record.push_back(Data.PlainOldData);
5486  Record.push_back(Data.Empty);
5487  Record.push_back(Data.Polymorphic);
5488  Record.push_back(Data.Abstract);
5489  Record.push_back(Data.IsStandardLayout);
5490  Record.push_back(Data.HasNoNonEmptyBases);
5491  Record.push_back(Data.HasPrivateFields);
5492  Record.push_back(Data.HasProtectedFields);
5493  Record.push_back(Data.HasPublicFields);
5494  Record.push_back(Data.HasMutableFields);
5495  Record.push_back(Data.HasVariantMembers);
5496  Record.push_back(Data.HasOnlyCMembers);
5497  Record.push_back(Data.HasInClassInitializer);
5498  Record.push_back(Data.HasUninitializedReferenceMember);
5499  Record.push_back(Data.NeedOverloadResolutionForMoveConstructor);
5500  Record.push_back(Data.NeedOverloadResolutionForMoveAssignment);
5501  Record.push_back(Data.NeedOverloadResolutionForDestructor);
5502  Record.push_back(Data.DefaultedMoveConstructorIsDeleted);
5503  Record.push_back(Data.DefaultedMoveAssignmentIsDeleted);
5504  Record.push_back(Data.DefaultedDestructorIsDeleted);
5505  Record.push_back(Data.HasTrivialSpecialMembers);
5506  Record.push_back(Data.DeclaredNonTrivialSpecialMembers);
5507  Record.push_back(Data.HasIrrelevantDestructor);
5508  Record.push_back(Data.HasConstexprNonCopyMoveConstructor);
5509  Record.push_back(Data.DefaultedDefaultConstructorIsConstexpr);
5510  Record.push_back(Data.HasConstexprDefaultConstructor);
5511  Record.push_back(Data.HasNonLiteralTypeFieldsOrBases);
5512  Record.push_back(Data.ComputedVisibleConversions);
5513  Record.push_back(Data.UserProvidedDefaultConstructor);
5514  Record.push_back(Data.DeclaredSpecialMembers);
5515  Record.push_back(Data.ImplicitCopyConstructorHasConstParam);
5516  Record.push_back(Data.ImplicitCopyAssignmentHasConstParam);
5517  Record.push_back(Data.HasDeclaredCopyConstructorWithConstParam);
5518  Record.push_back(Data.HasDeclaredCopyAssignmentWithConstParam);
5519  // IsLambda bit is already saved.
5520
5521  Record.push_back(Data.NumBases);
5522  if (Data.NumBases > 0)
5523    AddCXXBaseSpecifiersRef(Data.getBases(), Data.getBases() + Data.NumBases,
5524                            Record);
5525
5526  // FIXME: Make VBases lazily computed when needed to avoid storing them.
5527  Record.push_back(Data.NumVBases);
5528  if (Data.NumVBases > 0)
5529    AddCXXBaseSpecifiersRef(Data.getVBases(), Data.getVBases() + Data.NumVBases,
5530                            Record);
5531
5532  AddUnresolvedSet(Data.Conversions.get(*Context), Record);
5533  AddUnresolvedSet(Data.VisibleConversions.get(*Context), Record);
5534  // Data.Definition is the owning decl, no need to write it.
5535  AddDeclRef(D->getFirstFriend(), Record);
5536
5537  // Add lambda-specific data.
5538  if (Data.IsLambda) {
5539    auto &Lambda = D->getLambdaData();
5540    Record.push_back(Lambda.Dependent);
5541    Record.push_back(Lambda.IsGenericLambda);
5542    Record.push_back(Lambda.CaptureDefault);
5543    Record.push_back(Lambda.NumCaptures);
5544    Record.push_back(Lambda.NumExplicitCaptures);
5545    Record.push_back(Lambda.ManglingNumber);
5546    AddDeclRef(Lambda.ContextDecl, Record);
5547    AddTypeSourceInfo(Lambda.MethodTyInfo, Record);
5548    for (unsigned I = 0, N = Lambda.NumCaptures; I != N; ++I) {
5549      const LambdaCapture &Capture = Lambda.Captures[I];
5550      AddSourceLocation(Capture.getLocation(), Record);
5551      Record.push_back(Capture.isImplicit());
5552      Record.push_back(Capture.getCaptureKind());
5553      switch (Capture.getCaptureKind()) {
5554      case LCK_This:
5555      case LCK_VLAType:
5556        break;
5557      case LCK_ByCopy:
5558      case LCK_ByRef:
5559        VarDecl *Var =
5560            Capture.capturesVariable() ? Capture.getCapturedVar() : nullptr;
5561        AddDeclRef(Var, Record);
5562        AddSourceLocation(Capture.isPackExpansion() ? Capture.getEllipsisLoc()
5563                                                    : SourceLocation(),
5564                          Record);
5565        break;
5566      }
5567    }
5568  }
5569}
5570
5571void ASTWriter::ReaderInitialized(ASTReader *Reader) {
5572  assert(Reader && "Cannot remove chain");
5573  assert((!Chain || Chain == Reader) && "Cannot replace chain");
5574  assert(FirstDeclID == NextDeclID &&
5575         FirstTypeID == NextTypeID &&
5576         FirstIdentID == NextIdentID &&
5577         FirstMacroID == NextMacroID &&
5578         FirstSubmoduleID == NextSubmoduleID &&
5579         FirstSelectorID == NextSelectorID &&
5580         "Setting chain after writing has started.");
5581
5582  Chain = Reader;
5583
5584  // Note, this will get called multiple times, once one the reader starts up
5585  // and again each time it's done reading a PCH or module.
5586  FirstDeclID = NUM_PREDEF_DECL_IDS + Chain->getTotalNumDecls();
5587  FirstTypeID = NUM_PREDEF_TYPE_IDS + Chain->getTotalNumTypes();
5588  FirstIdentID = NUM_PREDEF_IDENT_IDS + Chain->getTotalNumIdentifiers();
5589  FirstMacroID = NUM_PREDEF_MACRO_IDS + Chain->getTotalNumMacros();
5590  FirstSubmoduleID = NUM_PREDEF_SUBMODULE_IDS + Chain->getTotalNumSubmodules();
5591  FirstSelectorID = NUM_PREDEF_SELECTOR_IDS + Chain->getTotalNumSelectors();
5592  NextDeclID = FirstDeclID;
5593  NextTypeID = FirstTypeID;
5594  NextIdentID = FirstIdentID;
5595  NextMacroID = FirstMacroID;
5596  NextSelectorID = FirstSelectorID;
5597  NextSubmoduleID = FirstSubmoduleID;
5598}
5599
5600void ASTWriter::IdentifierRead(IdentID ID, IdentifierInfo *II) {
5601  // Always keep the highest ID. See \p TypeRead() for more information.
5602  IdentID &StoredID = IdentifierIDs[II];
5603  if (ID > StoredID)
5604    StoredID = ID;
5605}
5606
5607void ASTWriter::MacroRead(serialization::MacroID ID, MacroInfo *MI) {
5608  // Always keep the highest ID. See \p TypeRead() for more information.
5609  MacroID &StoredID = MacroIDs[MI];
5610  if (ID > StoredID)
5611    StoredID = ID;
5612}
5613
5614void ASTWriter::TypeRead(TypeIdx Idx, QualType T) {
5615  // Always take the highest-numbered type index. This copes with an interesting
5616  // case for chained AST writing where we schedule writing the type and then,
5617  // later, deserialize the type from another AST. In this case, we want to
5618  // keep the higher-numbered entry so that we can properly write it out to
5619  // the AST file.
5620  TypeIdx &StoredIdx = TypeIdxs[T];
5621  if (Idx.getIndex() >= StoredIdx.getIndex())
5622    StoredIdx = Idx;
5623}
5624
5625void ASTWriter::SelectorRead(SelectorID ID, Selector S) {
5626  // Always keep the highest ID. See \p TypeRead() for more information.
5627  SelectorID &StoredID = SelectorIDs[S];
5628  if (ID > StoredID)
5629    StoredID = ID;
5630}
5631
5632void ASTWriter::MacroDefinitionRead(serialization::PreprocessedEntityID ID,
5633                                    MacroDefinitionRecord *MD) {
5634  assert(MacroDefinitions.find(MD) == MacroDefinitions.end());
5635  MacroDefinitions[MD] = ID;
5636}
5637
5638void ASTWriter::ModuleRead(serialization::SubmoduleID ID, Module *Mod) {
5639  assert(SubmoduleIDs.find(Mod) == SubmoduleIDs.end());
5640  SubmoduleIDs[Mod] = ID;
5641}
5642
5643void ASTWriter::CompletedTagDefinition(const TagDecl *D) {
5644  assert(D->isCompleteDefinition());
5645  assert(!WritingAST && "Already writing the AST!");
5646  if (auto *RD = dyn_cast<CXXRecordDecl>(D)) {
5647    // We are interested when a PCH decl is modified.
5648    if (RD->isFromASTFile()) {
5649      // A forward reference was mutated into a definition. Rewrite it.
5650      // FIXME: This happens during template instantiation, should we
5651      // have created a new definition decl instead ?
5652      assert(isTemplateInstantiation(RD->getTemplateSpecializationKind()) &&
5653             "completed a tag from another module but not by instantiation?");
5654      DeclUpdates[RD].push_back(
5655          DeclUpdate(UPD_CXX_INSTANTIATED_CLASS_DEFINITION));
5656    }
5657  }
5658}
5659
5660static bool isImportedDeclContext(ASTReader *Chain, const Decl *D) {
5661  if (D->isFromASTFile())
5662    return true;
5663
5664  // If we've not loaded any modules, this can't be imported.
5665  if (!Chain || !Chain->getModuleManager().size())
5666    return false;
5667
5668  // The predefined __va_list_tag struct is imported if we imported any decls.
5669  // FIXME: This is a gross hack.
5670  return D == D->getASTContext().getVaListTagDecl();
5671}
5672
5673void ASTWriter::AddedVisibleDecl(const DeclContext *DC, const Decl *D) {
5674  // TU and namespaces are handled elsewhere.
5675  if (isa<TranslationUnitDecl>(DC) || isa<NamespaceDecl>(DC))
5676    return;
5677
5678  // We're only interested in cases where a local declaration is added to an
5679  // imported context.
5680  if (D->isFromASTFile() || !isImportedDeclContext(Chain, cast<Decl>(DC)))
5681    return;
5682
5683  assert(DC == DC->getPrimaryContext() && "added to non-primary context");
5684  assert(!getDefinitiveDeclContext(DC) && "DeclContext not definitive!");
5685  assert(!WritingAST && "Already writing the AST!");
5686  if (UpdatedDeclContexts.insert(DC) && !cast<Decl>(DC)->isFromASTFile()) {
5687    // We're adding a visible declaration to a predefined decl context. Ensure
5688    // that we write out all of its lookup results so we don't get a nasty
5689    // surprise when we try to emit its lookup table.
5690    for (auto *Child : DC->decls())
5691      UpdatingVisibleDecls.push_back(Child);
5692  }
5693  UpdatingVisibleDecls.push_back(D);
5694}
5695
5696void ASTWriter::AddedCXXImplicitMember(const CXXRecordDecl *RD, const Decl *D) {
5697  assert(D->isImplicit());
5698
5699  // We're only interested in cases where a local declaration is added to an
5700  // imported context.
5701  if (D->isFromASTFile() || !isImportedDeclContext(Chain, RD))
5702    return;
5703
5704  if (!isa<CXXMethodDecl>(D))
5705    return;
5706
5707  // A decl coming from PCH was modified.
5708  assert(RD->isCompleteDefinition());
5709  assert(!WritingAST && "Already writing the AST!");
5710  DeclUpdates[RD].push_back(DeclUpdate(UPD_CXX_ADDED_IMPLICIT_MEMBER, D));
5711}
5712
5713void ASTWriter::ResolvedExceptionSpec(const FunctionDecl *FD) {
5714  assert(!DoneWritingDeclsAndTypes && "Already done writing updates!");
5715  if (!Chain) return;
5716  Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
5717    // If we don't already know the exception specification for this redecl
5718    // chain, add an update record for it.
5719    if (isUnresolvedExceptionSpec(cast<FunctionDecl>(D)
5720                                      ->getType()
5721                                      ->castAs<FunctionProtoType>()
5722                                      ->getExceptionSpecType()))
5723      DeclUpdates[D].push_back(UPD_CXX_RESOLVED_EXCEPTION_SPEC);
5724  });
5725}
5726
5727void ASTWriter::DeducedReturnType(const FunctionDecl *FD, QualType ReturnType) {
5728  assert(!WritingAST && "Already writing the AST!");
5729  if (!Chain) return;
5730  Chain->forEachImportedKeyDecl(FD, [&](const Decl *D) {
5731    DeclUpdates[D].push_back(
5732        DeclUpdate(UPD_CXX_DEDUCED_RETURN_TYPE, ReturnType));
5733  });
5734}
5735
5736void ASTWriter::ResolvedOperatorDelete(const CXXDestructorDecl *DD,
5737                                       const FunctionDecl *Delete) {
5738  assert(!WritingAST && "Already writing the AST!");
5739  assert(Delete && "Not given an operator delete");
5740  if (!Chain) return;
5741  Chain->forEachImportedKeyDecl(DD, [&](const Decl *D) {
5742    DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_RESOLVED_DTOR_DELETE, Delete));
5743  });
5744}
5745
5746void ASTWriter::CompletedImplicitDefinition(const FunctionDecl *D) {
5747  assert(!WritingAST && "Already writing the AST!");
5748  if (!D->isFromASTFile())
5749    return; // Declaration not imported from PCH.
5750
5751  // Implicit function decl from a PCH was defined.
5752  DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5753}
5754
5755void ASTWriter::FunctionDefinitionInstantiated(const FunctionDecl *D) {
5756  assert(!WritingAST && "Already writing the AST!");
5757  if (!D->isFromASTFile())
5758    return;
5759
5760  DeclUpdates[D].push_back(DeclUpdate(UPD_CXX_ADDED_FUNCTION_DEFINITION));
5761}
5762
5763void ASTWriter::StaticDataMemberInstantiated(const VarDecl *D) {
5764  assert(!WritingAST && "Already writing the AST!");
5765  if (!D->isFromASTFile())
5766    return;
5767
5768  // Since the actual instantiation is delayed, this really means that we need
5769  // to update the instantiation location.
5770  DeclUpdates[D].push_back(
5771      DeclUpdate(UPD_CXX_INSTANTIATED_STATIC_DATA_MEMBER,
5772       D->getMemberSpecializationInfo()->getPointOfInstantiation()));
5773}
5774
5775void ASTWriter::AddedObjCCategoryToInterface(const ObjCCategoryDecl *CatD,
5776                                             const ObjCInterfaceDecl *IFD) {
5777  assert(!WritingAST && "Already writing the AST!");
5778  if (!IFD->isFromASTFile())
5779    return; // Declaration not imported from PCH.
5780
5781  assert(IFD->getDefinition() && "Category on a class without a definition?");
5782  ObjCClassesWithCategories.insert(
5783    const_cast<ObjCInterfaceDecl *>(IFD->getDefinition()));
5784}
5785
5786void ASTWriter::DeclarationMarkedUsed(const Decl *D) {
5787  assert(!WritingAST && "Already writing the AST!");
5788  if (!D->isFromASTFile())
5789    return;
5790
5791  DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_USED));
5792}
5793
5794void ASTWriter::DeclarationMarkedOpenMPThreadPrivate(const Decl *D) {
5795  assert(!WritingAST && "Already writing the AST!");
5796  if (!D->isFromASTFile())
5797    return;
5798
5799  DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_MARKED_OPENMP_THREADPRIVATE));
5800}
5801
5802void ASTWriter::RedefinedHiddenDefinition(const NamedDecl *D, Module *M) {
5803  assert(!WritingAST && "Already writing the AST!");
5804  assert(D->isHidden() && "expected a hidden declaration");
5805  DeclUpdates[D].push_back(DeclUpdate(UPD_DECL_EXPORTED, M));
5806}
5807
5808void ASTWriter::AddedAttributeToRecord(const Attr *Attr,
5809                                       const RecordDecl *Record) {
5810  assert(!WritingAST && "Already writing the AST!");
5811  if (!Record->isFromASTFile())
5812    return;
5813  DeclUpdates[Record].push_back(DeclUpdate(UPD_ADDED_ATTR_TO_RECORD, Attr));
5814}
5815