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