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