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