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